public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 4/6] bootstage: Add feature to stash/unstash bootstage info
Date: Fri, 28 Sep 2012 11:56:38 -0700	[thread overview]
Message-ID: <1348858600-7095-5-git-send-email-sjg@chromium.org> (raw)
In-Reply-To: <1348858600-7095-1-git-send-email-sjg@chromium.org>

It is useful to be able to write the bootstage information to memory for
use by a later utility, or the Linux kernel. Provide a function to do
this as well as a function to read bootstage information back and incorporate
it into the current table.

This also makes it possible for U-Boot to chain to another U-Boot and pass
on its bootstage information.

BRANCH=snow,link
Signed-off-by: Simon Glass <sjg@chromium.org>
---
 common/bootstage.c  |  159 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/bootstage.h |   29 +++++++++
 2 files changed, 188 insertions(+), 0 deletions(-)

diff --git a/common/bootstage.c b/common/bootstage.c
index 9ea0128..a1e0939 100644
--- a/common/bootstage.c
+++ b/common/bootstage.c
@@ -44,6 +44,18 @@ 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,
+};
+
+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 */
+};
+
 ulong bootstage_add_record(enum bootstage_id id, const char *name,
 			   int flags, ulong mark)
 {
@@ -282,3 +294,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 %d bytes, but "
+			"only %d bytes is available\n", __func__, hdr->count,
+		      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;
+}
diff --git a/include/bootstage.h b/include/bootstage.h
index 9113852..d6b4e7b 100644
--- a/include/bootstage.h
+++ b/include/bootstage.h
@@ -284,6 +284,27 @@ void bootstage_report(void);
  */
 int bootstage_fdt_add_report(void);
 
+/*
+ * Stash bootstage data into memory
+ *
+ * @param base	Base address of memory buffer
+ * @param size	Size of memory buffer
+ * @return 0 if stashed ok, -1 if out of space
+ */
+int bootstage_stash(void *base, int size);
+
+/**
+ * Read bootstage data from memory
+ *
+ * Bootstage data is read from memory and placed in the bootstage table
+ * in the user records.
+ *
+ * @param base	Base address of memory buffer
+ * @param size	Size of memory buffer (-1 if unknown)
+ * @return 0 if unstashed ok, -1 if bootstage info not found, or out of space
+ */
+int bootstage_unstash(void *base, int size);
+
 #else
 /*
  * This is a dummy implementation which just calls show_boot_progress(),
@@ -307,7 +328,15 @@ static inline ulong bootstage_mark_name(enum bootstage_id id, const char *name)
 	return 0;
 }
 
+static inline int bootstage_stash(void *base, int size)
+{
+	return 0;	/* Pretend to succeed */
+}
 
+static inline int bootstage_unstash(void *base, int size)
+{
+	return 0;	/* Pretend to succeed */
+}
 #endif /* CONFIG_BOOTSTAGE */
 
 #endif
-- 
1.7.7.3

  parent reply	other threads:[~2012-09-28 18:56 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-28 18:56 [U-Boot] [PATCH 0/6] bootstage: Add a number of new features Simon Glass
2012-09-28 18:56 ` [U-Boot] [PATCH 1/6] bootstage: Export bootstage_add_record() function Simon Glass
2012-09-28 18:56 ` [U-Boot] [PATCH 2/6] bootstage: Add time accumulation feature Simon Glass
2012-09-28 18:56 ` [U-Boot] [PATCH 3/6] bootstage: Store boot timings in device tree Simon Glass
2012-09-28 18:56 ` Simon Glass [this message]
2012-09-28 18:56 ` [U-Boot] [PATCH 5/6] bootstage: Add bootstage command Simon Glass
2012-09-28 18:56 ` [U-Boot] [PATCH 6/6] bootstage: Add new bootstage IDs for board, LCD Simon Glass
2012-10-02 23:02 ` [U-Boot] [PATCH 0/6] bootstage: Add a number of new features Anatolij Gustschin
2012-10-03  0:07   ` Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1348858600-7095-5-git-send-email-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox