mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 09/10] commands: oftree: add option to return device tree without fixups
Date: Wed, 28 May 2025 12:50:45 +0200	[thread overview]
Message-ID: <20250528105046.3270397-10-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250528105046.3270397-1-a.fatoum@pengutronix.de>

When extracting the barebox device tree to use as argument to bootm when
BOOTM_OFTREE_FALLBACK is disabled, we will not want to apply fixups
twice, so add to oftree a -S option that just flattens the barebox
device tree as-is.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/Kconfig  |  8 ++++----
 commands/oftree.c | 14 +++++++++-----
 common/oftree.c   | 22 ++++++++++++----------
 include/of.h      | 14 ++++++++++++--
 4 files changed, 37 insertions(+), 21 deletions(-)

diff --git a/commands/Kconfig b/commands/Kconfig
index 0f3155d123ab..7b84ef70e875 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -2530,13 +2530,13 @@ config CMD_OFTREE
 	help
 	  oftree - handle device trees
 
-	  Usage: oftree [-lspf] [DTB]
+	  Usage: oftree [-lsSp]
 
 	  Options:
-		  -l		Load DTB to internal device tree
-		  -s		save internal device tree to DTB
+		  -l <DTB>	Load <DTB> to internal device tree
+		  -s <DTB>	save internal device tree with fixups to <DTB>
+		  -S <DTB>	save internal device tree without fixups to <DTB>
 		  -p		probe devices from stored device tree
-		  -f		free stored device tree
 
 config CMD_TIME
 	bool "time"
diff --git a/commands/oftree.c b/commands/oftree.c
index 3adc660a77e0..b172aee9e074 100644
--- a/commands/oftree.c
+++ b/commands/oftree.c
@@ -32,13 +32,13 @@ static int do_oftree(int argc, char *argv[])
 {
 	struct fdt_header *fdt = NULL;
 	int opt;
-	int probe = 0;
+	int probe = 0, fixup = 0;
 	char *load = NULL;
 	char *save = NULL;
 	int ret;
 	struct device_node *root;
 
-	while ((opt = getopt(argc, argv, "pfl:s:")) > 0) {
+	while ((opt = getopt(argc, argv, "pfl:s:S:")) > 0) {
 		switch (opt) {
 		case 'l':
 			load = optarg;
@@ -52,6 +52,9 @@ static int do_oftree(int argc, char *argv[])
 			}
 			break;
 		case 's':
+			fixup = 1;
+			fallthrough;
+		case 'S':
 			save = optarg;
 			break;
 		}
@@ -61,7 +64,7 @@ static int do_oftree(int argc, char *argv[])
 		return COMMAND_ERROR_USAGE;
 
 	if (save) {
-		fdt = of_get_fixed_tree(NULL);
+		fdt = of_get_flattened_tree(NULL, fixup);
 		if (!fdt) {
 			printf("no devicetree available\n");
 			ret = -EINVAL;
@@ -102,14 +105,15 @@ static int do_oftree(int argc, char *argv[])
 BAREBOX_CMD_HELP_START(oftree)
 BAREBOX_CMD_HELP_TEXT("Options:")
 BAREBOX_CMD_HELP_OPT ("-l <DTB>",  "Load <DTB> to internal devicetree")
-BAREBOX_CMD_HELP_OPT ("-s <DTB>",  "save internal devicetree to <DTB>")
+BAREBOX_CMD_HELP_OPT ("-s <DTB>",  "save internal devicetree after fixups to <DTB>")
+BAREBOX_CMD_HELP_OPT ("-S <DTB>",  "save internal devicetree without fixups to <DTB>")
 BAREBOX_CMD_HELP_OPT ("-p",  "probe devices from stored device tree")
 BAREBOX_CMD_HELP_END
 
 BAREBOX_CMD_START(oftree)
 	.cmd		= do_oftree,
 	BAREBOX_CMD_DESC("handle device trees")
-	BAREBOX_CMD_OPTS("[-lsp]")
+	BAREBOX_CMD_OPTS("[-lsSp]")
 	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
 	BAREBOX_CMD_HELP(cmd_oftree_help)
 BAREBOX_CMD_END
diff --git a/common/oftree.c b/common/oftree.c
index b9a854c786f6..58e96c39d897 100644
--- a/common/oftree.c
+++ b/common/oftree.c
@@ -413,15 +413,14 @@ void of_fix_tree(struct device_node *node)
 }
 
 /*
- * Get the fixed fdt. This function uses the fdt input pointer
+ * Get the fdt. This function uses the fdt input pointer
  * if provided or the barebox internal devicetree if not.
- * It increases the size of the tree and applies the registered
- * fixups.
  */
-struct fdt_header *of_get_fixed_tree(const struct device_node *node)
+struct fdt_header *of_get_flattened_tree(const struct device_node *node,
+					 bool fixup)
 {
 	struct fdt_header *fdt = NULL;
-	struct device_node *np;
+	struct device_node *np = NULL;
 
 	if (!node) {
 		node = of_get_root_node();
@@ -429,16 +428,19 @@ struct fdt_header *of_get_fixed_tree(const struct device_node *node)
 			return NULL;
 	}
 
-	np = of_dup(node);
+	if (fixup)
+		node = np = of_dup(node);
 
-	if (!np)
+	if (!node)
 		return NULL;
 
-	of_fix_tree(np);
+	if (fixup)
+		of_fix_tree(np);
 
-	fdt = of_flatten_dtb(np);
+	fdt = of_flatten_dtb((struct device_node *)node);
 
-	of_delete_node(np);
+	if (fixup)
+		of_delete_node(np);
 
 	return fdt;
 }
diff --git a/include/of.h b/include/of.h
index a9bd37fdca2f..f08ac8ccca69 100644
--- a/include/of.h
+++ b/include/of.h
@@ -323,7 +323,7 @@ extern const char *of_parse_phandle_and_get_alias_from(struct device_node *root,
 						       int index);
 
 extern struct device_node *of_get_root_node(void);
-extern struct fdt_header *of_get_fixed_tree(const struct device_node *node);
+extern struct fdt_header *of_get_flattened_tree(const struct device_node *node, bool fixup);
 extern int of_set_root_node(struct device_node *node);
 extern int barebox_register_of(struct device_node *root);
 extern int barebox_register_fdt(const void *dtb);
@@ -466,7 +466,7 @@ static inline struct device_node *of_get_root_node(void)
 	return NULL;
 }
 
-static inline struct fdt_header *of_get_fixed_tree(const struct device_node *node)
+static inline struct fdt_header *of_get_flattened_tree(const struct device_node *node, bool fixup)
 {
 	return NULL;
 }
@@ -1358,6 +1358,16 @@ static inline struct device_node *of_find_root_node(struct device_node *node)
 	return node;
 }
 
+/*
+ * Get the fixed fdt. This function uses the fdt input pointer
+ * if provided or the barebox internal devicetree if not.
+ * It increases the size of the tree and applies the registered
+ * fixups.
+ */
+static inline struct fdt_header *of_get_fixed_tree(const struct device_node *node)
+{
+	return of_get_flattened_tree(node, true);
+}
 
 static inline struct fdt_header *of_get_fixed_tree_for_boot(const struct device_node *node)
 {
-- 
2.39.5




  parent reply	other threads:[~2025-05-28 11:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-28 10:50 [PATCH 00/10] ARM32: allow disabling ATAGS and DT fallback Ahmad Fatoum
2025-05-28 10:50 ` [PATCH 01/10] arch: add new HAVE_ARCH_BOOTM_OFTREE symbol Ahmad Fatoum
2025-05-28 10:50 ` [PATCH 02/10] bootm: enable CONFIG_BOOTM_OFTREE by default Ahmad Fatoum
2025-05-28 10:50 ` [PATCH 03/10] ARM: lib32: disable ATAGS support " Ahmad Fatoum
2025-05-28 10:50 ` [PATCH 04/10] kvx: bootm: enforce existence of device tree Ahmad Fatoum
2025-05-28 10:50 ` [PATCH 05/10] bootm: booti: make device tree optional Ahmad Fatoum
2025-05-28 10:50 ` [PATCH 06/10] filetype: add new file type for 0-sized files Ahmad Fatoum
2025-05-28 10:50 ` [PATCH 07/10] bootm: have bootm_get_devicetree return NULL if passed empty DT Ahmad Fatoum
2025-05-28 10:50 ` [PATCH 08/10] bootm: make fallback to barebox internal tree optional Ahmad Fatoum
2025-05-28 10:50 ` Ahmad Fatoum [this message]
2025-05-28 10:50 ` [PATCH 10/10] of: drop undefined fdt_get_tree declaration Ahmad Fatoum

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=20250528105046.3270397-10-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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