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 2/2] bootsource: add optional read of /chosen/bootsource
Date: Wed,  9 Apr 2025 15:19:26 +0200	[thread overview]
Message-ID: <20250409131926.1949805-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250409131926.1949805-1-a.fatoum@pengutronix.de>

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@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




  reply	other threads:[~2025-04-09 14:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-09 13:19 [PATCH 1/2] bootsource: retire bootsource_set_alias_name Ahmad Fatoum
2025-04-09 13:19 ` Ahmad Fatoum [this message]
2025-04-10  6:34 ` Sascha Hauer

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=20250409131926.1949805-2-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