From: Chali Anis <[email protected]>

When a "barebox,state" node is already reachable via barebox's live
devicetree (statically compiled in, or injected by
CONFIG_STATE_OVERLAY), render its fully resolved description - backend
phandle included - with of_state_fixup() and publish it as a
"BareboxState" UEFI variable, so an OS-side consumer can locate the
state layout without needing a separate state.dtb file on the ESP.

Since a state node reachable this way makes the standalone
/boot/EFI/barebox/state.dtb loading path redundant, skip it whenever
a "state" alias is already present in the live devicetree.

Assisted-by: Claude Sonnet 5
Signed-off-by: Chali Anis <[email protected]>
---
 efi/payload/init.c | 46 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)

diff --git a/efi/payload/init.c b/efi/payload/init.c
index f0ce2a82cefc..eabebafb3774 100644
--- a/efi/payload/init.c
+++ b/efi/payload/init.c
@@ -287,6 +287,50 @@ core_efi_initcall(efi_register_firmware_nodes_fixup);
 #define EFI_LOADER_FEATURE_SECUREBOOT_ENROLL       (1LL << 11)
 #define EFI_LOADER_FEATURE_RETAIN_SHIM             (1LL << 12)
 
+static int state_to_efivars_export(void)
+{
+       struct device_node *np, *state_root;
+       struct state *state;
+       void *fdt;
+       size_t size;
+       int ret;
+
+       state_root = of_find_node_by_alias(NULL, "state");
+       if (!IS_ENABLED(CONFIG_STATE) || !state_root)
+               return 0;
+
+       state = state_by_node(state_root);
+       if (!state)
+               return -ENODEV;
+
+       np = of_new_node(NULL, NULL);
+       if (!np)
+               return -ENOMEM;
+
+       ret = of_state_fixup(np, state);
+       if (ret)
+               goto out;
+
+       fdt = of_flatten_dtb(np);
+       if (!fdt) {
+               ret = -ENOMEM;
+               goto out;
+       }
+
+       size = fdt_totalsize(fdt);
+
+       efi_set_variable("BareboxState", &efi_systemd_vendor_guid,
+                         EFI_VARIABLE_BOOTSERVICE_ACCESS |
+                         EFI_VARIABLE_RUNTIME_ACCESS,
+                         fdt, size);
+
+       free(fdt);
+       ret = 0;
+out:
+       of_delete_node(np);
+       return ret;
+}
+late_efi_initcall(state_to_efivars_export);
 
 static int efi_postcore_init(void)
 {
@@ -356,7 +400,7 @@ static int efi_late_init(void)
        void *fdt;
        int ret;
 
-       if (!IS_ENABLED(CONFIG_STATE))
+       if (!IS_ENABLED(CONFIG_STATE) || of_find_node_by_alias(NULL, "state"))
                return 0;
 
        if (!get_mounted_path("/boot")) {

Reply via email to