]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/bootstage.c
fw_env: calculate default number of env sectors
[karo-tx-uboot.git] / common / bootstage.c
index 9ea0128aa6e0b6d837f11051043ab187feb21aeb..35bce3d881a5b4151bc68382feae60d0fb2720f5 100644 (file)
@@ -1,23 +1,7 @@
 /*
  * Copyright (c) 2011, Google Inc. All rights reserved.
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 
@@ -30,6 +14,8 @@
 
 #include <common.h>
 #include <libfdt.h>
+#include <malloc.h>
+#include <linux/compiler.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -44,6 +30,34 @@ struct bootstage_record {
 static struct bootstage_record record[BOOTSTAGE_ID_COUNT] = { {1} };
 static int next_id = BOOTSTAGE_ID_USER;
 
+enum {
+       BOOTSTAGE_VERSION       = 0,
+       BOOTSTAGE_MAGIC         = 0xb00757a3,
+       BOOTSTAGE_DIGITS        = 9,
+};
+
+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 */
+};
+
+int bootstage_relocate(void)
+{
+       int i;
+
+       /*
+        * Duplicate all strings.  They may point to an old location in the
+        * program .text section that can eventually get trashed.
+        */
+       for (i = 0; i < BOOTSTAGE_ID_COUNT; i++)
+               if (record[i].name)
+                       record[i].name = strdup(record[i].name);
+
+       return 0;
+}
+
 ulong bootstage_add_record(enum bootstage_id id, const char *name,
                           int flags, ulong mark)
 {
@@ -90,6 +104,33 @@ ulong bootstage_mark_name(enum bootstage_id id, const char *name)
        return bootstage_add_record(id, name, flags, timer_get_boot_us());
 }
 
+ulong bootstage_mark_code(const char *file, const char *func, int linenum)
+{
+       char *str, *p;
+       __maybe_unused char *end;
+       int len = 0;
+
+       /* First work out the length we need to allocate */
+       if (linenum != -1)
+               len = 11;
+       if (func)
+               len += strlen(func);
+       if (file)
+               len += strlen(file);
+
+       str = malloc(len + 1);
+       p = str;
+       end = p + len;
+       if (file)
+               p += snprintf(p, end - p, "%s,", file);
+       if (linenum != -1)
+               p += snprintf(p, end - p, "%d", linenum);
+       if (func)
+               p += snprintf(p, end - p, ": %s", func);
+
+       return bootstage_mark_name(BOOTSTAGE_ID_ALLOC, str);
+}
+
 uint32_t bootstage_start(enum bootstage_id id, const char *name)
 {
        struct bootstage_record *rec = &record[id];
@@ -109,21 +150,6 @@ uint32_t bootstage_accum(enum bootstage_id id)
        return duration;
 }
 
-static void print_time(unsigned long us_time)
-{
-       char str[15], *s;
-       int grab = 3;
-
-       /* We don't seem to have %'d in U-Boot */
-       sprintf(str, "%12lu", us_time);
-       for (s = str + 3; *s; s += grab) {
-               if (s != str + 3)
-                       putc(s[-1] != ' ' ? ',' : ' ');
-               printf("%.*s", grab, s);
-               grab = 3;
-       }
-}
-
 /**
  * Get a record name as a printable string
  *
@@ -152,10 +178,10 @@ static uint32_t print_time_record(enum bootstage_id id,
 
        if (prev == -1U) {
                printf("%11s", "");
-               print_time(rec->time_us);
+               print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
        } else {
-               print_time(rec->time_us);
-               print_time(rec->time_us - prev);
+               print_grouped_ull(rec->time_us, BOOTSTAGE_DIGITS);
+               print_grouped_ull(rec->time_us - prev, BOOTSTAGE_DIGITS);
        }
        printf("  %s\n", get_record_name(buf, sizeof(buf), rec));
 
@@ -282,3 +308,150 @@ ulong __timer_get_boot_us(void)
 
 ulong timer_get_boot_us(void)
        __attribute__((weak, alias("__timer_get_boot_us")));
+
+/**
+ * Append data to a memory buffer
+ *
+ * Write data to the buffer if there is space. Whether there is space or not,
+ * the buffer pointer is incremented.
+ *
+ * @param ptrp Pointer to buffer, updated by this function
+ * @param end  Pointer to end of buffer
+ * @param data Data to write to buffer
+ * @param size Size of data
+ */
+static void append_data(char **ptrp, char *end, const void *data, int size)
+{
+       char *ptr = *ptrp;
+
+       *ptrp += size;
+       if (*ptrp > end)
+               return;
+
+       memcpy(ptr, data, size);
+}
+
+int bootstage_stash(void *base, int size)
+{
+       struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
+       struct bootstage_record *rec;
+       char buf[20];
+       char *ptr = base, *end = ptr + size;
+       uint32_t count;
+       int id;
+
+       if (hdr + 1 > (struct bootstage_hdr *)end) {
+               debug("%s: Not enough space for bootstage hdr\n", __func__);
+               return -1;
+       }
+
+       /* Write an arbitrary version number */
+       hdr->version = BOOTSTAGE_VERSION;
+
+       /* Count the number of records, and write that value first */
+       for (rec = record, id = count = 0; id < BOOTSTAGE_ID_COUNT;
+                       id++, rec++) {
+               if (rec->time_us != 0)
+                       count++;
+       }
+       hdr->count = count;
+       hdr->size = 0;
+       hdr->magic = BOOTSTAGE_MAGIC;
+       ptr += sizeof(*hdr);
+
+       /* Write the records, silently stopping when we run out of space */
+       for (rec = record, id = 0; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
+               if (rec->time_us != 0)
+                       append_data(&ptr, end, rec, sizeof(*rec));
+       }
+
+       /* Write the name strings */
+       for (rec = record, id = 0; id < BOOTSTAGE_ID_COUNT; id++, rec++) {
+               if (rec->time_us != 0) {
+                       const char *name;
+
+                       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;
+       }
+
+       /* Update total data size */
+       hdr->size = ptr - (char *)base;
+       printf("Stashed %d records\n", hdr->count);
+
+       return 0;
+}
+
+int bootstage_unstash(void *base, int size)
+{
+       struct bootstage_hdr *hdr = (struct bootstage_hdr *)base;
+       struct bootstage_record *rec;
+       char *ptr = base, *end = ptr + size;
+       uint rec_size;
+       int id;
+
+       if (size == -1)
+               end = (char *)(~(uintptr_t)0);
+
+       if (hdr + 1 > (struct bootstage_hdr *)end) {
+               debug("%s: Not enough space for bootstage hdr\n", __func__);
+               return -1;
+       }
+
+       if (hdr->magic != BOOTSTAGE_MAGIC) {
+               debug("%s: Invalid bootstage magic\n", __func__);
+               return -1;
+       }
+
+       if (ptr + hdr->size > end) {
+               debug("%s: Bootstage data runs past buffer end\n", __func__);
+               return -1;
+       }
+
+       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;
+       }
+
+       if (hdr->version != BOOTSTAGE_VERSION) {
+               debug("%s: Bootstage data version %#0x unrecognised\n",
+                     __func__, hdr->version);
+               return -1;
+       }
+
+       if (next_id + hdr->count > BOOTSTAGE_ID_COUNT) {
+               debug("%s: Bootstage has %d records, we have space for %d\n"
+                       "- please increase CONFIG_BOOTSTAGE_USER_COUNT\n",
+                     __func__, hdr->count, BOOTSTAGE_ID_COUNT - next_id);
+               return -1;
+       }
+
+       ptr += sizeof(*hdr);
+
+       /* Read the records */
+       rec_size = hdr->count * sizeof(*record);
+       memcpy(record + next_id, ptr, rec_size);
+
+       /* Read the name strings */
+       ptr += rec_size;
+       for (rec = record + next_id, id = 0; id < hdr->count; id++, rec++) {
+               rec->name = ptr;
+
+               /* Assume no data corruption here */
+               ptr += strlen(ptr) + 1;
+       }
+
+       /* Mark the records as read */
+       next_id += hdr->count;
+       printf("Unstashed %d records\n", hdr->count);
+
+       return 0;
+}