[PATCH 2/3] env: set fdtfile environment variable from /chosen/fdtfile

Casey Connolly casey.connolly at linaro.org
Tue Jun 10 18:23:34 CEST 2025


If /chosen/fdtfile exists in the devicetree and the fdtfile variable
isn't set in the default environment then populate it. This allows for
U-Boot to load the devicetree from the boot partition or ESP that should
more closely match the OS being booted.

Signed-off-by: Casey Connolly <casey.connolly at linaro.org>
---
 common/board_r.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/common/board_r.c b/common/board_r.c
index 41c8dec8d49ef21a12fd41272581dd94484dfa08..594c7827c39cfca5f4561b4699e5b66de54e11c2 100644
--- a/common/board_r.c
+++ b/common/board_r.c
@@ -448,8 +448,41 @@ static int should_load_env(void)
 
 	return 1;
 }
 
+/**
+ * fdtdec_setup_fdtfile - set fdtfile variable from /chosen/fdtfile
+ *
+ * This function will look for a string property named "fdtfile" under
+ * the chosen node and if found set the "fdtfile" environment variable.
+ *
+ * This devicetree property is used to identify the path to the FDT in
+ * use so that the same path can be checked in the dtbs directory on
+ * the boot partition or ESP.
+ */
+int setup_fdtfile(void)
+{
+	ofnode node;
+	const char *path;
+
+	if (env_get("fdtfile"))
+		return -EINVAL;
+
+	node = ofnode_path("/chosen");
+	if (!ofnode_valid(node))
+		return -ENOENT;
+
+	path = ofnode_read_string(node, "fdtfile");
+	if (!path)
+		return -ENOENT;
+
+	printf("Setting fdtfile to %s\n", path);
+	if (env_set("fdtfile", path))
+		return -EIO;
+
+	return 0;
+}
+
 static int initr_env(void)
 {
 	/* initialize environment */
 	if (should_load_env())
@@ -458,8 +491,10 @@ static int initr_env(void)
 		env_set_default(NULL, 0);
 
 	env_import_fdt();
 
+	setup_fdtfile();
+
 	if (IS_ENABLED(CONFIG_OF_CONTROL))
 		env_set_hex("fdtcontroladdr",
 			    (unsigned long)map_to_sysmem(gd->fdt_blob));
 

-- 
2.49.0



More information about the U-Boot mailing list