[PATCH 2/2] bootsource: add optional read of /chosen/bootsource

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Apr 9 06:19:26 PDT 2025


barebox currently /chosen properties like bootsource and earlycon,
although they can be useful in a second-stage boot scenario.

For example, on the STM32MP15, the TAMP register used to communicate
boot source from the BootROM is also used to override the reboot mode
prior to a warm reboot. As reboot modes are automatically cleared after
readout, this means that a chainloaded barebox will not be able to
determine the bootsource if the reboot mode driver is enabled.

To address this limitation, teach barebox to consume /chosen/bootsource.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 common/Kconfig       | 17 +++++++++++++++++
 common/bootsource.c  | 28 ++++++++++++++++++++++++++++
 drivers/of/base.c    | 21 +++++++++++++++++++--
 include/bootsource.h |  1 +
 4 files changed, 65 insertions(+), 2 deletions(-)

diff --git a/common/Kconfig b/common/Kconfig
index 8134e13bbeb2..1e04d0403ad0 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -1210,6 +1210,23 @@ config BOOT_DEFAULTS
 	  target, which will expand to the device that barebox has
 	  booted from if that could be determined.
 
+config BAREBOX_DT_2ND
+	bool "Consume DT bindings relevant only when barebox is booted by barebox"
+	depends on OFDEVICE
+	default BOARD_GENERIC_DT
+	help
+	  Barebox fixes up a number of device tree properties into the kernel
+	  device tree that it does not itself consume.
+	  Say y here for barebox to not only provide, but also consume
+	  these device tree bindings.
+
+	  This is currently:
+
+	    /chosen/bootsource: Maintain the bootsource, even if SoC
+	    bootsource info is lost.
+
+	  If unsure, say n.
+
 config RESET_SOURCE
 	bool "detect Reset cause"
 	depends on GLOBALVAR
diff --git a/common/bootsource.c b/common/bootsource.c
index 14e6565e3789..f608019bb758 100644
--- a/common/bootsource.c
+++ b/common/bootsource.c
@@ -9,6 +9,7 @@
 #include <bootsource.h>
 #include <environment.h>
 #include <magicvar.h>
+#include <string.h>
 #include <init.h>
 
 static const char *bootsource_str[BOOTSOURCE_MAX] = {
@@ -153,6 +154,33 @@ void bootsource_set_raw_instance(int instance)
 		pr_setenv("bootsource_instance", "%d", instance);
 }
 
+int bootsource_of_node_set(struct device_node *np)
+{
+	const char *alias;
+
+	alias = of_alias_get(np);
+	if (!alias)
+		return -EINVAL;
+
+	for (int bootsrc = 0; bootsrc < ARRAY_SIZE(bootsource_str); bootsrc++) {
+		int ret, instance;
+		size_t prefixlen;
+
+		prefixlen = str_has_prefix(alias, bootsource_str[bootsrc]);
+		if (!prefixlen)
+			continue;
+
+		ret = kstrtoint(alias + prefixlen, 10, &instance);
+		if (ret)
+			return ret;
+
+		bootsource_set_raw(bootsrc, instance);
+		return 0;
+	}
+
+	return -ENODEV;
+}
+
 int bootsource_of_alias_xlate(enum bootsource src, int instance)
 {
 	char chosen[sizeof("barebox,bootsource-harddisk4294967295")];
diff --git a/drivers/of/base.c b/drivers/of/base.c
index dfe4861c9eb6..324565068771 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -14,6 +14,7 @@
 #include <malloc.h>
 #include <init.h>
 #include <memory.h>
+#include <bootsource.h>
 #include <linux/sizes.h>
 #include <of_graph.h>
 #include <string.h>
@@ -3591,13 +3592,29 @@ const char *of_get_machine_compatible(void)
 }
 EXPORT_SYMBOL(of_get_machine_compatible);
 
-static int of_init_hostname(void)
+static void of_init_bootsource(void)
+{
+	struct device_node *bootsource;
+
+	if (!IS_ENABLED(CONFIG_BAREBOX_DT_2ND))
+		return;
+
+	bootsource = of_find_node_by_chosen("bootsource", NULL);
+	if (!bootsource)
+		return;
+
+	bootsource_of_node_set(bootsource);
+}
+
+static int of_init_vars(void)
 {
 	const char *name;
 
 	name = of_get_machine_compatible();
 	barebox_set_hostname_no_overwrite(name ?: "barebox");
 
+	of_init_bootsource();
+
 	return 0;
 }
-late_initcall(of_init_hostname);
+late_initcall(of_init_vars);
diff --git a/include/bootsource.h b/include/bootsource.h
index 1b3df84f4ddb..ce0c92d0fef9 100644
--- a/include/bootsource.h
+++ b/include/bootsource.h
@@ -33,6 +33,7 @@ const char *bootsource_to_string(enum bootsource src);
 const char *bootsource_get_alias_stem(enum bootsource bs);
 int bootsource_of_alias_xlate(enum bootsource bs, int instance);
 struct device_node *bootsource_of_node_get(struct device_node *root);
+int bootsource_of_node_set(struct device_node *np);
 struct cdev *bootsource_of_cdev_find(void);
 
 /**
-- 
2.39.5




More information about the barebox mailing list