sifive: fu540: Add U-Boot proper sector start
[oweals/u-boot.git] / common / bootstage.c
index 8cd2fb5e1bbf260c2dc88c1e611bb655600e6f7d..5f87358fd8544d13b851f488023102a78d745095 100644 (file)
@@ -1,7 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (c) 2011, Google Inc. All rights reserved.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 
  */
 
 #include <common.h>
-#include <libfdt.h>
+#include <bootstage.h>
+#include <hang.h>
+#include <log.h>
 #include <malloc.h>
+#include <sort.h>
+#include <spl.h>
 #include <linux/compiler.h>
+#include <linux/libfdt.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
 enum {
-       RECORD_COUNT = CONFIG_BOOTSTAGE_RECORD_COUNT,
+       RECORD_COUNT = CONFIG_VAL(BOOTSTAGE_RECORD_COUNT),
 };
 
 struct bootstage_record {
@@ -42,24 +46,34 @@ enum {
 };
 
 struct bootstage_hdr {
-       uint32_t version;       /* BOOTSTAGE_VERSION */
-       uint32_t count;         /* Number of records */
-       uint32_t size;          /* Total data size (non-zero if valid) */
-       uint32_t magic;         /* Unused */
+       u32 version;            /* BOOTSTAGE_VERSION */
+       u32 count;              /* Number of records */
+       u32 size;               /* Total data size (non-zero if valid) */
+       u32 magic;              /* Magic number */
+       u32 next_id;            /* Next ID to use for bootstage */
 };
 
 int bootstage_relocate(void)
 {
        struct bootstage_data *data = gd->bootstage;
        int i;
+       char *ptr;
+
+       /* Figure out where to relocate the strings to */
+       ptr = (char *)(data + 1);
 
        /*
         * Duplicate all strings.  They may point to an old location in the
         * program .text section that can eventually get trashed.
         */
        debug("Relocating %d records\n", data->rec_count);
-       for (i = 0; i < data->rec_count; i++)
-               data->record[i].name = strdup(data->record[i].name);
+       for (i = 0; i < data->rec_count; i++) {
+               const char *from = data->record[i].name;
+
+               strcpy(ptr, from);
+               data->record[i].name = ptr;
+               ptr += strlen(ptr) + 1;
+       }
 
        return 0;
 }
@@ -100,6 +114,13 @@ ulong bootstage_add_record(enum bootstage_id id, const char *name,
        struct bootstage_data *data = gd->bootstage;
        struct bootstage_record *rec;
 
+       /*
+        * initf_bootstage() is called very early during boot but since hang()
+        * calls bootstage_error() we can be called before bootstage is set up.
+        * Add a check to avoid this.
+        */
+       if (!data)
+               return mark;
        if (flags & BOOTSTAGEF_ALLOC)
                id = data->next_id++;
 
@@ -205,7 +226,7 @@ uint32_t bootstage_accum(enum bootstage_id id)
  * @return pointer to name, either from the record or pointing to buf.
  */
 static const char *get_record_name(char *buf, int len,
-                                  struct bootstage_record *rec)
+                                  const struct bootstage_record *rec)
 {
        if (rec->name)
                return rec->name;
@@ -264,7 +285,7 @@ static int add_bootstages_devicetree(struct fdt_header *blob)
         */
        bootstage = fdt_add_subnode(blob, 0, "bootstage");
        if (bootstage < 0)
-               return -1;
+               return -EINVAL;
 
        /*
         * Insert the timings to the device tree in the reverse order so
@@ -284,13 +305,13 @@ static int add_bootstages_devicetree(struct fdt_header *blob)
                /* add properties to the node. */
                if (fdt_setprop_string(blob, node, "name",
                                       get_record_name(buf, sizeof(buf), rec)))
-                       return -1;
+                       return -EINVAL;
 
                /* Check if this is a 'mark' or 'accum' record */
                if (fdt_setprop_cell(blob, node,
                                rec->start_us ? "accum" : "mark",
                                rec->time_us))
-                       return -1;
+                       return -EINVAL;
        }
 
        return 0;
@@ -327,7 +348,7 @@ void bootstage_report(void)
        }
        if (data->rec_count > RECORD_COUNT)
                printf("Overflowed internal boot id table by %d entries\n"
-                      "- please increase CONFIG_BOOTSTAGE_RECORD_COUNT\n",
+                      "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
                       data->rec_count - RECORD_COUNT);
 
        puts("\nAccumulated time:\n");
@@ -361,68 +382,58 @@ static void append_data(char **ptrp, char *end, const void *data, int size)
 
 int bootstage_stash(void *base, int size)
 {
-       struct bootstage_data *data = gd->bootstage;
+       const struct bootstage_data *data = gd->bootstage;
        struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
-       struct bootstage_record *rec;
+       const struct bootstage_record *rec;
        char buf[20];
        char *ptr = base, *end = ptr + size;
-       uint32_t count;
        int i;
 
        if (hdr + 1 > (struct bootstage_hdr *)end) {
                debug("%s: Not enough space for bootstage hdr\n", __func__);
-               return -1;
+               return -ENOSPC;
        }
 
        /* Write an arbitrary version number */
        hdr->version = BOOTSTAGE_VERSION;
 
-       /* Count the number of records, and write that value first */
-       for (rec = data->record, i = count = 0; i < data->rec_count;
-            i++, rec++) {
-               if (rec->id != 0)
-                       count++;
-       }
-       hdr->count = count;
+       hdr->count = data->rec_count;
        hdr->size = 0;
        hdr->magic = BOOTSTAGE_MAGIC;
+       hdr->next_id = data->next_id;
        ptr += sizeof(*hdr);
 
        /* Write the records, silently stopping when we run out of space */
-       for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
-               if (rec->time_us != 0)
-                       append_data(&ptr, end, rec, sizeof(*rec));
-       }
+       for (rec = data->record, i = 0; i < data->rec_count; i++, rec++)
+               append_data(&ptr, end, rec, sizeof(*rec));
 
        /* Write the name strings */
        for (rec = data->record, i = 0; i < data->rec_count; i++, rec++) {
-               if (rec->time_us != 0) {
-                       const char *name;
+               const char *name;
 
-                       name = get_record_name(buf, sizeof(buf), rec);
-                       append_data(&ptr, end, name, strlen(name) + 1);
-               }
+               name = get_record_name(buf, sizeof(buf), rec);
+               append_data(&ptr, end, name, strlen(name) + 1);
        }
 
        /* Check for buffer overflow */
        if (ptr > end) {
                debug("%s: Not enough space for bootstage stash\n", __func__);
-               return -1;
+               return -ENOSPC;
        }
 
        /* Update total data size */
        hdr->size = ptr - (char *)base;
-       printf("Stashed %d records\n", hdr->count);
+       debug("Stashed %d records\n", hdr->count);
 
        return 0;
 }
 
-int bootstage_unstash(void *base, int size)
+int bootstage_unstash(const void *base, int size)
 {
+       const struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
        struct bootstage_data *data = gd->bootstage;
-       struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
+       const char *ptr = base, *end = ptr + size;
        struct bootstage_record *rec;
-       char *ptr = base, *end = ptr + size;
        uint rec_size;
        int i;
 
@@ -431,37 +442,37 @@ int bootstage_unstash(void *base, int size)
 
        if (hdr + 1 > (struct bootstage_hdr *)end) {
                debug("%s: Not enough space for bootstage hdr\n", __func__);
-               return -1;
+               return -EPERM;
        }
 
        if (hdr->magic != BOOTSTAGE_MAGIC) {
                debug("%s: Invalid bootstage magic\n", __func__);
-               return -1;
+               return -ENOENT;
        }
 
        if (ptr + hdr->size > end) {
                debug("%s: Bootstage data runs past buffer end\n", __func__);
-               return -1;
+               return -ENOSPC;
        }
 
        if (hdr->count * sizeof(*rec) > hdr->size) {
                debug("%s: Bootstage has %d records needing %lu bytes, but "
                        "only %d bytes is available\n", __func__, hdr->count,
                      (ulong)hdr->count * sizeof(*rec), hdr->size);
-               return -1;
+               return -ENOSPC;
        }
 
        if (hdr->version != BOOTSTAGE_VERSION) {
                debug("%s: Bootstage data version %#0x unrecognised\n",
                      __func__, hdr->version);
-               return -1;
+               return -EINVAL;
        }
 
        if (data->rec_count + hdr->count > RECORD_COUNT) {
                debug("%s: Bootstage has %d records, we have space for %d\n"
-                       "- please increase CONFIG_BOOTSTAGE_USER_COUNT\n",
+                       "Please increase CONFIG_(SPL_)BOOTSTAGE_RECORD_COUNT\n",
                      __func__, hdr->count, RECORD_COUNT - data->rec_count);
-               return -1;
+               return -ENOSPC;
        }
 
        ptr += sizeof(*hdr);
@@ -475,6 +486,8 @@ int bootstage_unstash(void *base, int size)
        for (rec = data->record + data->next_id, i = 0; i < hdr->count;
             i++, rec++) {
                rec->name = ptr;
+               if (spl_phase() == PHASE_SPL)
+                       rec->name = strdup(ptr);
 
                /* Assume no data corruption here */
                ptr += strlen(ptr) + 1;
@@ -482,11 +495,27 @@ int bootstage_unstash(void *base, int size)
 
        /* Mark the records as read */
        data->rec_count += hdr->count;
-       printf("Unstashed %d records\n", hdr->count);
+       data->next_id = hdr->next_id;
+       debug("Unstashed %d records\n", hdr->count);
 
        return 0;
 }
 
+int bootstage_get_size(void)
+{
+       struct bootstage_data *data = gd->bootstage;
+       struct bootstage_record *rec;
+       int size;
+       int i;
+
+       size = sizeof(struct bootstage_data);
+       for (rec = data->record, i = 0; i < data->rec_count;
+            i++, rec++)
+               size += strlen(rec->name) + 1;
+
+       return size;
+}
+
 int bootstage_init(bool first)
 {
        struct bootstage_data *data;