mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] drivers: misc: external_state: add a barebox external state.
@ 2025-11-03  4:38 chalianis1
  2025-11-03  4:38 ` [PATCH 2/2] efi: payload: refactor to use the external barebox state driver chalianis1
  2025-11-03  6:46 ` [PATCH 1/2] drivers: misc: external_state: add a barebox external state Ahmad Fatoum
  0 siblings, 2 replies; 10+ messages in thread
From: chalianis1 @ 2025-11-03  4:38 UTC (permalink / raw)
  To: s.hauer; +Cc: barebox, Chali Anis

From: Chali Anis <chalianis1@gmail.com>

Add a driver to use an external state dtb, gives the ability to define
an external state at compile time. useful for yocto or buildroot defining
a state.dtb that will be passed to barebox at compile time vi a defconfig
fragment.

Signed-off-by: Chali Anis <chalianis1@gmail.com>
---
 drivers/misc/Kconfig          | 13 ++++++++
 drivers/misc/Makefile         |  1 +
 drivers/misc/external_state.c | 58 +++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+)
 create mode 100644 drivers/misc/external_state.c

diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index e235646ee551..91075788b5b8 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -21,6 +21,19 @@ config STATE_DRV
 	depends on OFDEVICE
 	depends on STATE
 
+config EXTERNAL_STATE
+	tristate "Use external barebox state dtb"
+	depends on OFDEVICE
+	depends on STATE
+	help
+	  This permits the use of an extranl dtb state blob
+	  which permits to dynamicly at compile time specify
+	  an external blob vi EXTERNAL_STATE_DTB_PATH
+
+config EXTERNAL_STATE_DTB_PATH
+	string "the external barebox state dtb path"
+	depends on EXTERNAL_STATE
+
 config DEV_MEM
         bool "Generic memory I/O device (/dev/mem)"
 
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index c4b61de7b8b5..eba19f8c22c3 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -6,6 +6,7 @@
 obj-$(CONFIG_JTAG)		+= jtag.o
 obj-$(CONFIG_SRAM)		+= sram.o
 obj-$(CONFIG_STATE_DRV)		+= state.o
+obj-$(CONFIG_EXTERNAL_STATE) += external_state.o
 obj-$(CONFIG_DEV_MEM)		+= mem.o
 obj-$(CONFIG_DEV_PORT)		+= port.o
 obj-$(CONFIG_UBOOTVAR)		+= ubootvar.o
diff --git a/drivers/misc/external_state.c b/drivers/misc/external_state.c
new file mode 100644
index 000000000000..d392133d3370
--- /dev/null
+++ b/drivers/misc/external_state.c
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2014 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
+ * Copyright (C) 2025 Anis Chali <anis.chali#ro-main.com>, Ro-Main
+ */
+
+#include <common.h>
+#include <driver.h>
+#include <init.h>
+#include <state.h>
+#include <libfile.h>
+
+#include <linux/err.h>
+
+static int state_external_init(void)
+{
+	const char *dt_path = CONFIG_EXTERNAL_STATE_DTB_PATH;
+	struct device_node *state_root = NULL;
+	size_t size;
+	void *fdt;
+	int ret;
+
+	if (strlen(dt_path) <= 0)
+		return -EINVAL;
+
+	fdt = read_file(dt_path, &size);
+	if (!fdt) {
+		pr_info("unable to read %s: %m\n", dt_path);
+		return 0;
+	}
+
+	state_root = of_unflatten_dtb(fdt, size);
+	if (!IS_ERR(state_root)) {
+		struct device_node *np = NULL;
+		struct state *state;
+
+		ret = barebox_register_of(state_root);
+		if (ret)
+			pr_warn("Failed to register device-tree: %pe\n", ERR_PTR(ret));
+
+		np = of_find_node_by_alias(state_root, "state");
+
+		state = state_new_from_node(np, false);
+		if (IS_ERR(state))
+			return PTR_ERR(state);
+
+		ret = state_load(state);
+		if (ret != -ENOMEDIUM)
+			pr_warn("Failed to load persistent state, continuing with defaults, %d\n",
+				ret);
+
+		return 0;
+	}
+
+	return -EINVAL;
+}
+
+late_initcall(state_external_init);
-- 
2.34.1




^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2025-11-03 15:29 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-03  4:38 [PATCH 1/2] drivers: misc: external_state: add a barebox external state chalianis1
2025-11-03  4:38 ` [PATCH 2/2] efi: payload: refactor to use the external barebox state driver chalianis1
2025-11-03  6:40   ` Ahmad Fatoum
2025-11-03 14:10     ` anis chali
2025-11-03 14:24       ` Ahmad Fatoum
2025-11-03 15:28         ` anis chali
2025-11-03  6:46 ` [PATCH 1/2] drivers: misc: external_state: add a barebox external state Ahmad Fatoum
2025-11-03 14:09   ` anis chali
2025-11-03 14:22     ` Ahmad Fatoum
2025-11-03 15:26       ` anis chali

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox