[PATCH 1/2] drivers: misc: external_state: add a barebox external state.
chalianis1 at gmail.com
chalianis1 at gmail.com
Sun Nov 2 20:38:32 PST 2025
From: Chali Anis <chalianis1 at 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 at 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 at 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
More information about the barebox
mailing list