diff mbox series

[1/2] env: Add function to import environment variables from FIT conf node

Message ID 20240702064834.9543-2-lukas.funke-oss@weidmueller.com
State New
Delegated to: Simon Glass
Headers show
Series Import environment variables from FIT configuration | expand

Commit Message

Lukas Funke July 2, 2024, 6:48 a.m. UTC
From: Lukas Funke <lukas.funke@weidmueller.com>

Add function which reads properties from FIT conf node prefixed with
"env,". Import property name (without 'env,') and it's value as
runtime environment variables.

Note: this only works with string properties

Example:

    configurations {
        default = "conf-1";
            conf-1 {
                kernel = "kernel-1";
                fdt = "fdt-1";
                env,foo = "somevalue";
                env,bar = "someothervalue";
            };
    };

=> env print foo
foo=somevalue

Signed-off-by: Lukas Funke <lukas.funke@weidmueller.com>
---

 boot/image-fit.c |  4 ++++
 env/Kconfig      | 10 ++++++++++
 env/common.c     | 28 ++++++++++++++++++++++++++++
 include/env.h    | 11 +++++++++++
 4 files changed, 53 insertions(+)
diff mbox series

Patch

diff --git a/boot/image-fit.c b/boot/image-fit.c
index 89e377563ce..0ca31c5f851 100644
--- a/boot/image-fit.c
+++ b/boot/image-fit.c
@@ -34,6 +34,7 @@ 
 DECLARE_GLOBAL_DATA_PTR;
 #endif /* !USE_HOSTCC*/
 
+#include <env.h>
 #include <bootm.h>
 #include <image.h>
 #include <bootstage.h>
@@ -2128,6 +2129,9 @@  int fit_image_load(struct bootm_headers *images, ulong addr,
 			puts("OK\n");
 		}
 
+#if !defined(USE_HOSTCC)
+		env_import_fit_conf(fit, cfg_noffset);
+#endif
 		bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
 
 		noffset = fit_conf_get_prop_node(fit, cfg_noffset, prop_name,
diff --git a/env/Kconfig b/env/Kconfig
index 1f8e90af55e..01b802e54b9 100644
--- a/env/Kconfig
+++ b/env/Kconfig
@@ -748,6 +748,16 @@  config ENV_FDT_PATH
 	help
 	  The initial value of the env_fdt_path variable.
 
+config ENV_IMPORT_FIT_CONF
+	bool "Amend environment by FIT configuration node properties"
+	depends on OF_CONTROL
+	help
+	  If selected, after the environment has been loaded from its
+	  persistent location, the "env,*" properties in the conf-node
+	  of FIT image are used to update the run-time environment. This
+	  can be useful in order to transport signed environment variables
+	  to the kernel cmdline.
+
 config ENV_APPEND
 	bool "Always append the environment with new data"
 	help
diff --git a/env/common.c b/env/common.c
index 48a565107c1..c8aa59447e9 100644
--- a/env/common.c
+++ b/env/common.c
@@ -24,6 +24,7 @@ 
 #include <dm/ofnode.h>
 #include <net.h>
 #include <watchdog.h>
+#include <fdt_support.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -661,3 +662,30 @@  void env_import_fdt(void)
 	}
 }
 #endif
+
+#define FIT_CONF_ENV_PROPERTY_PREFIX "env,"
+void env_import_fit_conf(const void *fdt, int conf_node)
+{
+	int offset, len;
+	const char *name;
+	const void *value;
+	const struct fdt_property *property;
+
+	if (!CONFIG_IS_ENABLED(ENV_IMPORT_FIT_CONF))
+		return;
+
+	fdt_for_each_property_offset(offset, fdt, conf_node) {
+		property = fdt_get_property_by_offset(fdt, offset, NULL);
+
+		name = fdt_get_string(fdt, fdt32_to_cpu(property->nameoff), NULL);
+		if (strncmp(name, FIT_CONF_ENV_PROPERTY_PREFIX,
+			    sizeof(FIT_CONF_ENV_PROPERTY_PREFIX) - 1))
+			continue;
+
+		value = fdt_getprop(fdt, conf_node, name, &len);
+		/* Get the actual variable name "env,somename" -> "somename" */
+		name += sizeof(FIT_CONF_ENV_PROPERTY_PREFIX) - 1;
+
+		env_set(name, value);
+	}
+}
diff --git a/include/env.h b/include/env.h
index d2a5954ded8..fa4c67056e7 100644
--- a/include/env.h
+++ b/include/env.h
@@ -382,4 +382,15 @@  void env_import_fdt(void);
 static inline void env_import_fdt(void) {}
 #endif
 
+/**
+ * env_import_fit_conf() - Import environment values from FIT configuration node
+ *
+ * This imports environment variables from FIT configuration node. Each
+ * property name starting with an "env,"-prefix is imported as variable where
+ * the variable name is the suffix of the property name.
+ *
+ * Example: env,somevalue = "foobar" --> somevalue=foobar
+ */
+void env_import_fit_conf(const void *fdt, int conf_node);
+
 #endif