All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrick Delaunay <patrick.delaunay@st.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v2 3/8] sandbox: add option show_of_embedded for spl test
Date: Mon, 20 May 2019 15:00:02 +0200	[thread overview]
Message-ID: <1558357207-7345-4-git-send-email-patrick.delaunay@st.com> (raw)
In-Reply-To: <1558357207-7345-1-git-send-email-patrick.delaunay@st.com>

Add an option show_of_embedded used in SPL to dump the used device tree.

Signed-off-by: Patrick Delaunay <patrick.delaunay@st.com>
---

Changes in v2:
- add new option for spl test: show embedded dtb

 arch/sandbox/cpu/spl.c           | 188 +++++++++++++++++++++++++++++++++++++++
 arch/sandbox/cpu/start.c         |   9 ++
 arch/sandbox/include/asm/state.h |   1 +
 3 files changed, 198 insertions(+)

diff --git a/arch/sandbox/cpu/spl.c b/arch/sandbox/cpu/spl.c
index 2ca4cd6..d3d9b08 100644
--- a/arch/sandbox/cpu/spl.c
+++ b/arch/sandbox/cpu/spl.c
@@ -46,6 +46,190 @@ static int spl_board_load_image(struct spl_image_info *spl_image,
 }
 SPL_LOAD_IMAGE_METHOD("sandbox", 0, BOOT_DEVICE_BOARD, spl_board_load_image);
 
+#ifdef CONFIG_OF_EMBED
+/****************************************************************************/
+/* the next functions mainly copyied from cmd fdt for SPL sandbox test */
+
+#include <linux/ctype.h>
+
+#define CMD_FDT_MAX_DUMP 64
+#define MAX_LEVEL	32		/* how deeply nested we will go */
+
+/*
+ * Heuristic to guess if this is a string or concatenated strings.
+ */
+
+static int is_printable_string(const void *data, int len)
+{
+	const char *s = data;
+
+	/* zero length is not */
+	if (len == 0)
+		return 0;
+
+	/* must terminate with zero or '\n' */
+	if (s[len - 1] != '\0' && s[len - 1] != '\n')
+		return 0;
+
+	/* printable or a null byte (concatenated strings) */
+	while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
+		/*
+		 * If we see a null, there are three possibilities:
+		 * 1) If len == 1, it is the end of the string, printable
+		 * 2) Next character also a null, not printable.
+		 * 3) Next character not a null, continue to check.
+		 */
+		if (s[0] == '\0') {
+			if (len == 1)
+				return 1;
+			if (s[1] == '\0')
+				return 0;
+		}
+		s++;
+		len--;
+	}
+
+	/* Not the null termination, or not done yet: not printable */
+	if (*s != '\0' || len != 0)
+		return 0;
+
+	return 1;
+}
+
+/*
+ * Print the property in the best format, a heuristic guess.  Print as
+ * a string, concatenated strings, a byte, word, double word, or (if all
+ * else fails) it is printed as a stream of bytes.
+ */
+static void print_data(const void *data, int len)
+{
+	int j;
+
+	/* no data, don't print */
+	if (len == 0)
+		return;
+
+	/*
+	 * It is a string, but it may have multiple strings (embedded '\0's).
+	 */
+	if (is_printable_string(data, len)) {
+		puts("\"");
+		j = 0;
+		while (j < len) {
+			if (j > 0)
+				puts("\", \"");
+			puts(data);
+			j    += strlen(data) + 1;
+			data += strlen(data) + 1;
+		}
+		puts("\"");
+		return;
+	}
+
+	if ((len % 4) == 0) {
+		if (len > CMD_FDT_MAX_DUMP) {
+			printf("* 0x%p [0x%08x]", data, len);
+		} else {
+			const __be32 *p;
+
+			printf("<");
+			for (j = 0, p = data; j < len / 4; j++)
+				printf("0x%08x%s", fdt32_to_cpu(p[j]),
+				       j < (len / 4 - 1) ? " " : "");
+			printf(">");
+		}
+	} else { /* anything else... hexdump */
+		if (len > CMD_FDT_MAX_DUMP) {
+			printf("* 0x%p [0x%08x]", data, len);
+		} else {
+			const u8 *s;
+
+			printf("[");
+			for (j = 0, s = data; j < len; j++)
+				printf("%02x%s", s[j], j < len - 1 ? " " : "");
+			printf("]");
+		}
+	}
+}
+
+/*
+ * Recursively print the working_fdt.
+ */
+static int fdt_print(const struct fdt_header *working_fdt)
+{
+	static char tabs[MAX_LEVEL + 1] =
+		"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
+		"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
+	const void *nodep;	/* property node pointer */
+	int  nodeoffset = 0;	/* node offset from libfdt */
+	int  nextoffset;	/* next node offset from libfdt */
+	u32  tag;		/* tag */
+	int  len;		/* length of the property */
+	int  level = 0;		/* keep track of nesting level */
+	const struct fdt_property *fdt_prop;
+	const char *pathp;
+
+	/* print the node and all subnodes. */
+	while (level >= 0) {
+		tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
+		switch (tag) {
+		case FDT_BEGIN_NODE:
+			pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
+			if (!pathp)
+				pathp = "/* NULL pointer error */";
+			if (*pathp == '\0')
+				pathp = "/";	/* root is nameless */
+			printf("%s%s {\n",
+			       &tabs[MAX_LEVEL - level], pathp);
+			level++;
+			break;
+		case FDT_END_NODE:
+			level--;
+			printf("%s};\n", &tabs[MAX_LEVEL - level]);
+			if (level == 0)
+				level = -1;		/* exit the loop */
+			break;
+		case FDT_PROP:
+			fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
+						  sizeof(*fdt_prop));
+			pathp    = fdt_string(working_fdt,
+					      fdt32_to_cpu(fdt_prop->nameoff));
+			len      = fdt32_to_cpu(fdt_prop->len);
+			nodep    = fdt_prop->data;
+			if (len < 0) {
+				printf("libfdt fdt_getprop(): %s\n",
+				       fdt_strerror(len));
+				return 1;
+			} else if (len == 0) {
+				/* the property has no value */
+				printf("%s%s;\n",
+				       &tabs[MAX_LEVEL - level],
+				       pathp);
+			} else {
+				printf("%s%s = ",
+				       &tabs[MAX_LEVEL - level],
+				       pathp);
+				print_data(nodep, len);
+				printf(";\n");
+			}
+			break;
+		case FDT_NOP:
+			printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
+			break;
+		case FDT_END:
+			return 1;
+		default:
+			return 1;
+		}
+		nodeoffset = nextoffset;
+	}
+
+	return 0;
+}
+
+/***************************************************************************/
+#endif
+
 void spl_board_init(void)
 {
 	struct sandbox_state *state = state_get_current();
@@ -63,6 +247,10 @@ void spl_board_init(void)
 		     uclass_next_device(&dev))
 			;
 	}
+#ifdef CONFIG_OF_EMBED
+	if (state->show_of_embedded)
+		fdt_print(gd->fdt_blob);
+#endif
 }
 
 void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c
index 82828f0..8116e3c 100644
--- a/arch/sandbox/cpu/start.c
+++ b/arch/sandbox/cpu/start.c
@@ -293,6 +293,15 @@ static int sandbox_cmdline_cb_show_of_platdata(struct sandbox_state *state,
 }
 SANDBOX_CMDLINE_OPT(show_of_platdata, 0, "Show of-platdata in SPL");
 
+static int sandbox_cmdline_cb_show_of_embedded(struct sandbox_state *state,
+					       const char *arg)
+{
+	state->show_of_embedded = true;
+
+	return 0;
+}
+SANDBOX_CMDLINE_OPT(show_of_embedded, 0, "Show of-embedded in SPL");
+
 int board_run_command(const char *cmdline)
 {
 	printf("## Commands are disabled. Please enable CONFIG_CMDLINE.\n");
diff --git a/arch/sandbox/include/asm/state.h b/arch/sandbox/include/asm/state.h
index 2d773d3..c8142c8 100644
--- a/arch/sandbox/include/asm/state.h
+++ b/arch/sandbox/include/asm/state.h
@@ -90,6 +90,7 @@ struct sandbox_state {
 	bool show_test_output;		/* Don't suppress stdout in tests */
 	int default_log_level;		/* Default log level for sandbox */
 	bool show_of_platdata;		/* Show of-platdata in SPL */
+	bool show_of_embedded;		/* Show of-embedded in SPL */
 	bool ram_buf_read;		/* true if we read the RAM buffer */
 
 	/* Pointer to information for each SPI bus/cs */
-- 
2.7.4

  parent reply	other threads:[~2019-05-20 13:00 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-20 12:59 [U-Boot] [PATCH v2 0/8] fdt: Allow indicating a node is for U-Boot proper only Patrick Delaunay
2019-05-20 13:00 ` [U-Boot] [PATCH v2 1/8] sandbox: add config for SPL boot with devicetree Patrick Delaunay
2019-05-20 15:35   ` Simon Glass
2019-05-20 13:00 ` [U-Boot] [PATCH v2 2/8] fdt: Allow to use EMBEDDED device tree for SPL Patrick Delaunay
2019-05-20 15:35   ` Simon Glass
2019-05-20 13:00 ` Patrick Delaunay [this message]
2019-05-20 15:35   ` [U-Boot] [PATCH v2 3/8] sandbox: add option show_of_embedded for spl test Simon Glass
2019-05-20 13:00 ` [U-Boot] [PATCH v2 4/8] test: py: add option to select device tree used during compilation Patrick Delaunay
2019-05-21 16:21   ` Stephen Warren
2019-05-21 18:32     ` Patrick DELAUNAY
2019-05-21 21:10       ` Stephen Warren
2019-05-20 13:00 ` [U-Boot] [PATCH v2 5/8] test: move SPL test nodes in test device tree Patrick Delaunay
2019-05-20 15:35   ` Simon Glass
2019-05-20 13:00 ` [U-Boot] [PATCH v2 6/8] test: check u-boot properties in SPL " Patrick Delaunay
2019-05-20 15:35   ` Simon Glass
2019-05-20 13:00 ` [U-Boot] [PATCH v2 7/8] fdt: Allow indicating a node is for U-Boot proper only Patrick Delaunay
2019-05-20 13:00 ` [U-Boot] [PATCH v2 8/8] dm: doc: add documentation for pre-reloc properties in SPL and TPL Patrick Delaunay
2019-05-20 16:09   ` Simon Glass
2019-05-20 15:35 ` [U-Boot] [PATCH v2 0/8] fdt: Allow indicating a node is for U-Boot proper only Simon Glass
2019-05-21 16:07   ` Patrick DELAUNAY
2019-05-22  0:53     ` 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=1558357207-7345-4-git-send-email-patrick.delaunay@st.com \
    --to=patrick.delaunay@st.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.