In preparation for allowing the state nodes to ship inside the barebox
binary (as we do for non-EFI-payload already), add provisions to skip
the external file if barebox already has a populated DT.

Also for the case that a state had already been set up, skip manual
creation of the state node.

Assisted-by: Claude:opus-5
Signed-off-by: Ahmad Fatoum <[email protected]>
---
 efi/payload/init.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/efi/payload/init.c b/efi/payload/init.c
index 1e5f2eb8cf56..db4bc25682a3 100644
--- a/efi/payload/init.c
+++ b/efi/payload/init.c
@@ -284,6 +284,7 @@ static int efi_late_init(void)
 {
        const char *state_desc = "/boot/EFI/barebox/state.dtb";
        struct device_node *state_root = NULL;
+       struct device_node *root;
        size_t size;
        void *fdt;
        int ret;
@@ -303,6 +304,17 @@ static int efi_late_init(void)
                return 0;
        }
 
+       /*
+        * A device tree built into barebox takes precedence: any state it
+        * describes has been instantiated by the state driver already.
+        */
+       root = of_get_root_node();
+       if (root && !list_empty(&root->children)) {
+               pr_warn("device tree is populated, ignoring %s\n", state_desc);
+               free(fdt);
+               return 0;
+       }
+
        state_root = of_unflatten_dtb(fdt, size);
        free(fdt);
 
@@ -320,6 +332,10 @@ static int efi_late_init(void)
                        return 0;
                }
 
+               /* probing the registered tree may have instantiated it already 
*/
+               if (state_by_node(np))
+                       return 0;
+
                state = state_new_from_node(np, false);
                if (IS_ERR(state))
                        return PTR_ERR(state);
-- 
2.47.3


Reply via email to