From: chalianis1@gmail.com
To: s.hauer@pengutronix.de
Cc: barebox@lists.infradead.org, Chali Anis <chalianis1@gmail.com>
Subject: [PATCH 1/2] drivers: misc: external_state: add a barebox external state.
Date: Sun,  2 Nov 2025 23:38:32 -0500	[thread overview]
Message-ID: <20251103043833.149013-1-chalianis1@gmail.com> (raw)
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
next             reply	other threads:[~2025-11-03  4:39 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-03  4:38 chalianis1 [this message]
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
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=20251103043833.149013-1-chalianis1@gmail.com \
    --to=chalianis1@gmail.com \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /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