Skip to contentSkip to conversation

drivers: misc: external_state: add a barebox external state.

PATCH In review [email protected] 2 participants 10 messages Nov 3, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 drivers: misc: external_state: add a barebox external state.

From: Chali Anis <[email protected]>

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.

---
 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
drivers/misc/Kconfig +13 -0
index e235646ee551..91075788b5b8 100644
@@ -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)"
 
drivers/misc/Makefile +1 -0
index c4b61de7b8b5..eba19f8c22c3 100644
@@ -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
drivers/misc/external_state.c new file +58 -0

58 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 efi: payload: refactor to use the external barebox state driver.

From: Chali Anis <[email protected]>

use the external state config to pass the barebox state.

---
 arch/arm/configs/efi_v8_defconfig |  2 ++
 efi/payload/init.c                | 50 -------------------------------
 2 files changed, 2 insertions(+), 50 deletions(-)
arch/arm/configs/efi_v8_defconfig +2 -0
index 5f946dd51d4a..26682aab304a 100644
@@ -21,6 +21,8 @@ CONFIG_CONSOLE_RATP=y
 CONFIG_PARTITION_DISK_EFI=y
 CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW=y
 CONFIG_STATE=y
+CONFIG_EXTERNAL_STATE=y
+CONFIG_EXTERNAL_STATE_DTB_PATH="/boot/EFI/barebox/state.dtb"
 CONFIG_BOOTCHOOSER=y
 CONFIG_RESET_SOURCE=y
 CONFIG_MACHINE_ID=y
efi/payload/init.c +0 -50

56 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 2/2] efi: payload: refactor to use the external barebox state driver.

Hi,

Quoted 8 lines · On 03.11.25 05:38, [email protected] wrote:

From: Chali Anis <[email protected]>

use the external state config to pass the barebox state.

---
 arch/arm/configs/efi_v8_defconfig |  2 ++
 efi/payload/init.c                | 50 -------------------------------

This breaks state for x86-efi, the original user of efi_late_init().

Quoted 76 lines
 2 files changed, 2 insertions(+), 50 deletions(-)
arch/arm/configs/efi_v8_defconfig +2 -0
index 5f946dd51d4a..26682aab304a 100644
@@ -21,6 +21,8 @@ CONFIG_CONSOLE_RATP=y
 CONFIG_PARTITION_DISK_EFI=y
 CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW=y
 CONFIG_STATE=y
+CONFIG_EXTERNAL_STATE=y
+CONFIG_EXTERNAL_STATE_DTB_PATH="/boot/EFI/barebox/state.dtb"
 CONFIG_BOOTCHOOSER=y
 CONFIG_RESET_SOURCE=y
 CONFIG_MACHINE_ID=y
efi/payload/init.c +0 -50
index 5b827c57ed1f..63d62395cf3f 100644
@@ -376,56 +376,6 @@ static int efi_postcore_init(void)
 }
 postcore_efi_initcall(efi_postcore_init);
 
-static int efi_late_init(void)
-{
-	const char *state_desc = "/boot/EFI/barebox/state.dtb";
-	struct device_node *state_root = NULL;
-	size_t size;
-	void *fdt;
-	int ret;
-
-	if (!IS_ENABLED(CONFIG_STATE))
-		return 0;
-
-	if (!get_mounted_path("/boot")) {
-		pr_warn("boot device couldn't be determined%s\n",
-			IS_ENABLED(CONFIG_FS_EFI) ? "" : " without CONFIG_FS_EFI");
-		return 0;
-	}
-
-	fdt = read_file(state_desc, &size);
-	if (!fdt) {
-		pr_info("unable to read %s: %m\n", state_desc);
-		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 0;
-}
-late_efi_initcall(efi_late_init);
-
 static int do_efiexit(int argc, char *argv[])
 {
 	if (!BS)
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
author
#3 lore raw

Re: [PATCH 2/2] efi: payload: refactor to use the external barebox state driver.

Quoted 1 line
Quoted 1 line

This breaks state for x86-efi, the original user of efi_late_init().

Was not sure to add it to the efi_defconfig

Quoted 98 lines · Ahmad Fatoum

Hi,

Quoted 8 lines · On 03.11.25 05:38, [email protected] wrote:

From: Chali Anis <[email protected]>

use the external state config to pass the barebox state.

---
 arch/arm/configs/efi_v8_defconfig |  2 ++
 efi/payload/init.c                | 50 -------------------------------

This breaks state for x86-efi, the original user of efi_late_init().

Quoted 66 lines
 2 files changed, 2 insertions(+), 50 deletions(-)
arch/arm/configs/efi_v8_defconfig +2 -0
index 5f946dd51d4a..26682aab304a 100644
@@ -21,6 +21,8 @@ CONFIG_CONSOLE_RATP=y
 CONFIG_PARTITION_DISK_EFI=y
 CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW=y
 CONFIG_STATE=y
+CONFIG_EXTERNAL_STATE=y
+CONFIG_EXTERNAL_STATE_DTB_PATH="/boot/EFI/barebox/state.dtb"
 CONFIG_BOOTCHOOSER=y
 CONFIG_RESET_SOURCE=y
 CONFIG_MACHINE_ID=y
efi/payload/init.c +0 -43
index 5b827c57ed1f..63d62395cf3f 100644
@@ -376,56 +376,6 @@ static int efi_postcore_init(void)
 }
 postcore_efi_initcall(efi_postcore_init);
 
-static int efi_late_init(void)
-{
-     const char *state_desc = "/boot/EFI/barebox/state.dtb";
-     struct device_node *state_root = NULL;
-     size_t size;
-     void *fdt;
-     int ret;
-
-     if (!IS_ENABLED(CONFIG_STATE))
-             return 0;
-
-     if (!get_mounted_path("/boot")) {
-             pr_warn("boot device couldn't be determined%s\n",
-                     IS_ENABLED(CONFIG_FS_EFI) ? "" : " without CONFIG_FS_EFI");
-             return 0;
-     }
-
-     fdt = read_file(state_desc, &size);
-     if (!fdt) {
-             pr_info("unable to read %s: %m\n", state_desc);
-             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);
-

efi/payload/init.c static int efi_postcore_init(void) · line 381

Quoted 10 lines
efi/payload/init.c +0 -7
-             return 0;
-     }
-
-     return 0;
-}
-late_efi_initcall(efi_late_init);
-
 static int do_efiexit(int argc, char *argv[])
 {
      if (!BS)

--

Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#4 lore raw

Re: [PATCH 2/2] efi: payload: refactor to use the external barebox state driver.

Hi,

Quoted 3 lines · anis chali
Quoted 1 line
Quoted 1 line

This breaks state for x86-efi, the original user of efi_late_init().

Was not sure to add it to the efi_defconfig

generally speaking, we try not to break existing users and if we do, an entry should be added to the migration guide explaining what backward incompatibility was introduced.

Cheers,
Ahmad

Quoted 101 lines · Ahmad Fatoum
Quoted 98 lines

Hi,

Quoted 8 lines · On 03.11.25 05:38, [email protected] wrote:

From: Chali Anis <[email protected]>

use the external state config to pass the barebox state.

---
 arch/arm/configs/efi_v8_defconfig |  2 ++
 efi/payload/init.c                | 50 -------------------------------

This breaks state for x86-efi, the original user of efi_late_init().

Quoted 66 lines
 2 files changed, 2 insertions(+), 50 deletions(-)
arch/arm/configs/efi_v8_defconfig +2 -0
index 5f946dd51d4a..26682aab304a 100644
@@ -21,6 +21,8 @@ CONFIG_CONSOLE_RATP=y
 CONFIG_PARTITION_DISK_EFI=y
 CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW=y
 CONFIG_STATE=y
+CONFIG_EXTERNAL_STATE=y
+CONFIG_EXTERNAL_STATE_DTB_PATH="/boot/EFI/barebox/state.dtb"
 CONFIG_BOOTCHOOSER=y
 CONFIG_RESET_SOURCE=y
 CONFIG_MACHINE_ID=y
efi/payload/init.c +0 -43
index 5b827c57ed1f..63d62395cf3f 100644
@@ -376,56 +376,6 @@ static int efi_postcore_init(void)
 }
 postcore_efi_initcall(efi_postcore_init);
 
-static int efi_late_init(void)
-{
-     const char *state_desc = "/boot/EFI/barebox/state.dtb";
-     struct device_node *state_root = NULL;
-     size_t size;
-     void *fdt;
-     int ret;
-
-     if (!IS_ENABLED(CONFIG_STATE))
-             return 0;
-
-     if (!get_mounted_path("/boot")) {
-             pr_warn("boot device couldn't be determined%s\n",
-                     IS_ENABLED(CONFIG_FS_EFI) ? "" : " without CONFIG_FS_EFI");
-             return 0;
-     }
-
-     fdt = read_file(state_desc, &size);
-     if (!fdt) {
-             pr_info("unable to read %s: %m\n", state_desc);
-             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);
-

efi/payload/init.c static int efi_postcore_init(void) · line 381

Quoted 10 lines
efi/payload/init.c +0 -7
-             return 0;
-     }
-
-     return 0;
-}
-late_efi_initcall(efi_late_init);
-
 static int do_efiexit(int argc, char *argv[])
 {
      if (!BS)

--

Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
Signature · 4 lines
Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |
author
#5 lore raw

Re: [PATCH 2/2] efi: payload: refactor to use the external barebox state driver.

Quoted 3 lines

generally speaking, we try not to break existing users and if we do, an entry should be added to the migration guide explaining what backward incompatibility was introduced.

Okay I understand, for people using the default efi_defconfig or just adds fragments on top of it, it will be transparent but otherwise yes there will be a breakage.

Thanks.

Cheers.
Anis.

Quoted 123 lines · Ahmad Fatoum

Hi,

Quoted 3 lines · anis chali
Quoted 1 line
Quoted 1 line

This breaks state for x86-efi, the original user of efi_late_init().

Was not sure to add it to the efi_defconfig

generally speaking, we try not to break existing users and if we do, an entry should be added to the migration guide explaining what backward incompatibility was introduced.

Cheers,
Ahmad

Quoted 92 lines · Ahmad Fatoum
Quoted 90 lines

Hi,

Quoted 8 lines · On 03.11.25 05:38, [email protected] wrote:

From: Chali Anis <[email protected]>

use the external state config to pass the barebox state.

---
 arch/arm/configs/efi_v8_defconfig |  2 ++
 efi/payload/init.c                | 50 -------------------------------

This breaks state for x86-efi, the original user of efi_late_init().

Quoted 75 lines
 2 files changed, 2 insertions(+), 50 deletions(-)
arch/arm/configs/efi_v8_defconfig +2 -0
index 5f946dd51d4a..26682aab304a 100644
@@ -21,6 +21,8 @@ CONFIG_CONSOLE_RATP=y
 CONFIG_PARTITION_DISK_EFI=y
 CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW=y
 CONFIG_STATE=y
+CONFIG_EXTERNAL_STATE=y
+CONFIG_EXTERNAL_STATE_DTB_PATH="/boot/EFI/barebox/state.dtb"
 CONFIG_BOOTCHOOSER=y
 CONFIG_RESET_SOURCE=y
 CONFIG_MACHINE_ID=y
efi/payload/init.c +0 -50
index 5b827c57ed1f..63d62395cf3f 100644
@@ -376,56 +376,6 @@ static int efi_postcore_init(void)
 }
 postcore_efi_initcall(efi_postcore_init);
 
-static int efi_late_init(void)
-{
-     const char *state_desc = "/boot/EFI/barebox/state.dtb";
-     struct device_node *state_root = NULL;
-     size_t size;
-     void *fdt;
-     int ret;
-
-     if (!IS_ENABLED(CONFIG_STATE))
-             return 0;
-
-     if (!get_mounted_path("/boot")) {
-             pr_warn("boot device couldn't be determined%s\n",
-                     IS_ENABLED(CONFIG_FS_EFI) ? "" : " without CONFIG_FS_EFI");
-             return 0;
-     }
-
-     fdt = read_file(state_desc, &size);
-     if (!fdt) {
-             pr_info("unable to read %s: %m\n", state_desc);
-             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 0;
-}
-late_efi_initcall(efi_late_init);
-
 static int do_efiexit(int argc, char *argv[])
 {
Quoted 9 lines
Quoted 8 lines

efi/payload/init.c static int efi_postcore_init(void) · line 381

Quoted 1 line
efi/payload/init.c +0 -0
      if (!BS)

--

Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

--

Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |
#6 lore raw
In reply to [email protected]

Hi,

Quoted 6 lines · On 03.11.25 05:38, [email protected] wrote:

From: Chali Anis <[email protected]>

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.

We already had code that did this unconditionally for EFI. Can you explain why this needs to be customizable?

drivers/misc/Kconfig config STATE_DRV · line 29

Quoted 6 lines
drivers/misc/Kconfig +6 -0
+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

external

drivers/misc/Kconfig config STATE_DRV · line 30

Quoted 1 line
drivers/misc/Kconfig +1 -0
+	  which permits to dynamicly at compile time specify

dynamically

drivers/misc/Kconfig config STATE_DRV · line 35

Quoted 5 lines
drivers/misc/Kconfig +5 -0
+	  an external blob vi EXTERNAL_STATE_DTB_PATH
+
+config EXTERNAL_STATE_DTB_PATH
+	string "the external barebox state dtb path"
+	depends on EXTERNAL_STATE

Really needs help text as EXTERNAL makes me think of a file on the build host.

drivers/misc/external_state.c line 24

Quoted 10 lines
drivers/misc/external_state.c new file +10 -0
+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;

strlen can't return negative numbers.

The original function had a number of safety checks that are missing here:

- It only ran the code when in EFI payload mode, as the same barebox binary could be chainloaded without EFI

- It checked that /boot is mounted and gave a useful error message to hint at e.g., EFIFS missing, which we are missing out on here.

I am not yet convinced this is strictly an improvement.

If you have the need to customize where your state comes from, how about adding a state -f /path/to/state.dtb command option and calling it from your init script?

Cheers,
Ahmad

Quoted 34 lines
drivers/misc/external_state.c new file +34 -0
+
+	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);
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
author
#7 lore raw
Quoted 1 line
Quoted 1 line

strlen can't return negative numbers.

ack, right it is size_t (unsigned)

Quoted 9 lines
Quoted 9 lines

The original function had a number of safety checks that are missing here:

- It only ran the code when in EFI payload mode, as the same barebox binary could be chainloaded without EFI

- It checked that /boot is mounted and gave a useful error message to hint at e.g., EFIFS missing, which we are missing out on here.

I am not yet convinced this is strictly an improvement.

Yes you are right but I didn't reintegrate the code in generic driver since it will just use Kconfigs to reference a state dtb file. In case this code was accepted there is no context to do the check, but I ensured that barebox gives a behaves in way that the user will quickly see that there is no /boot directory with the message telling No such file or directory, anyway you suggestion to add -f to state command
is more elegant, I will do some experimentations. the aim of this patch is to support a state.dtb given in runtime which permits userspace interaction with
native board because I didn't figure out how barebox state command could communicate with a barebox intree state.

Quoted 2 lines
Quoted 2 lines

If you have the need to customize where your state comes from, how about adding a state -f /path/to/state.dtb command option and calling it from your init script?

right, more elegant suggestion, I will try it.

Thank's for feedback

Anis.

Quoted 107 lines · Ahmad Fatoum

Hi,

Quoted 6 lines · On 03.11.25 05:38, [email protected] wrote:

From: Chali Anis <[email protected]>

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.

We already had code that did this unconditionally for EFI. Can you explain why this needs to be customizable?

drivers/misc/Kconfig config STATE_DRV · line 29

Quoted 6 lines
drivers/misc/Kconfig +6 -0
+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

external

Quoted 1 line

+ which permits to dynamicly at compile time specify

dynamically

drivers/misc/Kconfig config STATE_DRV · line 35

Quoted 5 lines
drivers/misc/Kconfig +5 -0
+       an external blob vi EXTERNAL_STATE_DTB_PATH
+
+config EXTERNAL_STATE_DTB_PATH
+     string "the external barebox state dtb path"
+     depends on EXTERNAL_STATE

Really needs help text as EXTERNAL makes me think of a file on the build host.

drivers/misc/external_state.c line 24

Quoted 10 lines
drivers/misc/external_state.c new file +10 -0
+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;

strlen can't return negative numbers.

The original function had a number of safety checks that are missing here:

- It only ran the code when in EFI payload mode, as the same barebox binary could be chainloaded without EFI

- It checked that /boot is mounted and gave a useful error message to hint at e.g., EFIFS missing, which we are missing out on here.

I am not yet convinced this is strictly an improvement.

If you have the need to customize where your state comes from, how about adding a state -f /path/to/state.dtb command option and calling it from your init script?

Cheers,
Ahmad

Quoted 24 lines
drivers/misc/external_state.c new file +24 -0
+
+     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)

drivers/misc/external_state.c line 58

Quoted 10 lines
drivers/misc/external_state.c new file +10 -0
+                     pr_warn("Failed to load persistent state, continuing with defaults, %d\n",
+                             ret);
+
+             return 0;
+     }
+
+     return -EINVAL;
+}
+
+late_initcall(state_external_init);

--

Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#8 lore raw

Hi Anis,

Quoted 13 lines · anis chali
Quoted 1 line
Quoted 1 line

strlen can't return negative numbers.

ack, right it is size_t (unsigned)

Quoted 9 lines
Quoted 9 lines

The original function had a number of safety checks that are missing here:

- It only ran the code when in EFI payload mode, as the same barebox binary could be chainloaded without EFI

- It checked that /boot is mounted and gave a useful error message to hint at e.g., EFIFS missing, which we are missing out on here.

I am not yet convinced this is strictly an improvement.

Quoted 12 lines

Yes you are right but I didn't reintegrate the code in generic driver since it will just use Kconfigs to reference a state dtb file. In case this code was accepted there is no context to do the check, but I ensured that barebox gives a behaves in way that the user will quickly see that there is no /boot directory with the message telling No such file or directory, anyway you suggestion to add -f to state command
is more elegant, I will do some experimentations. the aim of this patch is to support a state.dtb given in runtime which permits userspace interaction with
native board because I didn't figure out how barebox state command could communicate with a barebox intree state.

I didn't quite understand what you are hoping to achieve with this patch set, but if the command would serve the purpose, then that would be best, I think.

Cheers,
Ahmad

Quoted 118 lines
Quoted 3 lines
Quoted 3 lines

If you have the need to customize where your state comes from, how about adding a state -f /path/to/state.dtb command option and calling it from your init script?

right, more elegant suggestion, I will try it.

Thank's for feedback

Anis.

Quoted 107 lines · Ahmad Fatoum

Hi,

Quoted 6 lines · On 03.11.25 05:38, [email protected] wrote:

From: Chali Anis <[email protected]>

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.

We already had code that did this unconditionally for EFI. Can you explain why this needs to be customizable?

drivers/misc/Kconfig config STATE_DRV · line 29

Quoted 6 lines
drivers/misc/Kconfig +6 -0
+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

external

Quoted 1 line

+ which permits to dynamicly at compile time specify

dynamically

drivers/misc/Kconfig config STATE_DRV · line 35

Quoted 5 lines
drivers/misc/Kconfig +5 -0
+       an external blob vi EXTERNAL_STATE_DTB_PATH
+
+config EXTERNAL_STATE_DTB_PATH
+     string "the external barebox state dtb path"
+     depends on EXTERNAL_STATE

Really needs help text as EXTERNAL makes me think of a file on the build host.

drivers/misc/external_state.c line 24

Quoted 10 lines
drivers/misc/external_state.c new file +10 -0
+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;

strlen can't return negative numbers.

The original function had a number of safety checks that are missing here:

- It only ran the code when in EFI payload mode, as the same barebox binary could be chainloaded without EFI

- It checked that /boot is mounted and gave a useful error message to hint at e.g., EFIFS missing, which we are missing out on here.

I am not yet convinced this is strictly an improvement.

If you have the need to customize where your state comes from, how about adding a state -f /path/to/state.dtb command option and calling it from your init script?

Cheers,
Ahmad

Quoted 24 lines
drivers/misc/external_state.c new file +24 -0
+
+     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)

drivers/misc/external_state.c line 58

Quoted 10 lines
drivers/misc/external_state.c new file +10 -0
+                     pr_warn("Failed to load persistent state, continuing with defaults, %d\n",
+                             ret);
+
+             return 0;
+     }
+
+     return -EINVAL;
+}
+
+late_initcall(state_external_init);

--

Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
Signature · 4 lines
Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |
author
#9 lore raw
Quoted 2 lines

I didn't quite understand what you are hoping to achieve with this patch set, but if the command would serve the purpose, then that would be

Okay, I will come back with something soon.

Thank's for support

Quoted 162 lines · Ahmad Fatoum

Hi Anis,

Quoted 13 lines · anis chali
Quoted 1 line
Quoted 1 line

strlen can't return negative numbers.

ack, right it is size_t (unsigned)

Quoted 9 lines
Quoted 9 lines

The original function had a number of safety checks that are missing here:

- It only ran the code when in EFI payload mode, as the same barebox binary could be chainloaded without EFI

- It checked that /boot is mounted and gave a useful error message to hint at e.g., EFIFS missing, which we are missing out on here.

I am not yet convinced this is strictly an improvement.

Quoted 12 lines

Yes you are right but I didn't reintegrate the code in generic driver since it will just use Kconfigs to reference a state dtb file. In case this code was accepted there is no context to do the check, but I ensured that barebox gives a behaves in way that the user will quickly see that there is no /boot directory with the message telling No such file or directory, anyway you suggestion to add -f to state command
is more elegant, I will do some experimentations. the aim of this patch is to support a state.dtb given in runtime which permits userspace interaction with
native board because I didn't figure out how barebox state command could communicate with a barebox intree state.

I didn't quite understand what you are hoping to achieve with this patch set, but if the command would serve the purpose, then that would be best, I think.

Cheers,
Ahmad

Quoted 118 lines
Quoted 3 lines
Quoted 3 lines

If you have the need to customize where your state comes from, how about adding a state -f /path/to/state.dtb command option and calling it from your init script?

right, more elegant suggestion, I will try it.

Thank's for feedback

Anis.

Quoted 107 lines · Ahmad Fatoum

Hi,

Quoted 6 lines · On 03.11.25 05:38, [email protected] wrote:

From: Chali Anis <[email protected]>

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.

We already had code that did this unconditionally for EFI. Can you explain why this needs to be customizable?

drivers/misc/Kconfig config STATE_DRV · line 29

Quoted 6 lines
drivers/misc/Kconfig +6 -0
+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

external

Quoted 1 line

+ which permits to dynamicly at compile time specify

dynamically

drivers/misc/Kconfig config STATE_DRV · line 35

Quoted 5 lines
drivers/misc/Kconfig +5 -0
+       an external blob vi EXTERNAL_STATE_DTB_PATH
+
+config EXTERNAL_STATE_DTB_PATH
+     string "the external barebox state dtb path"
+     depends on EXTERNAL_STATE

Really needs help text as EXTERNAL makes me think of a file on the build host.

drivers/misc/external_state.c line 24

Quoted 10 lines
drivers/misc/external_state.c new file +10 -0
+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;

strlen can't return negative numbers.

The original function had a number of safety checks that are missing here:

- It only ran the code when in EFI payload mode, as the same barebox binary could be chainloaded without EFI

- It checked that /boot is mounted and gave a useful error message to hint at e.g., EFIFS missing, which we are missing out on here.

I am not yet convinced this is strictly an improvement.

If you have the need to customize where your state comes from, how about adding a state -f /path/to/state.dtb command option and calling it from your init script?

Cheers,
Ahmad

Quoted 24 lines
drivers/misc/external_state.c new file +24 -0
+
+     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)

drivers/misc/external_state.c line 58

Quoted 10 lines
drivers/misc/external_state.c new file +10 -0
+                     pr_warn("Failed to load persistent state, continuing with defaults, %d\n",
+                             ret);
+
+             return 0;
+     }
+
+     return -EINVAL;
+}
+
+late_initcall(state_external_init);

--

Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

--

Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |