This code will be equally useful for barebox running as EFI loader, so
move it to a common location and touch it up to be reusable.

While at it, drop unneeded headers.

Signed-off-by: Ahmad Fatoum <[email protected]>
---
 efi/Makefile                           |  1 +
 efi/{payload/efi-initrd.c => initrd.c} | 20 +++++++++++++-------
 efi/payload/Makefile                   |  1 -
 3 files changed, 14 insertions(+), 8 deletions(-)
 rename efi/{payload/efi-initrd.c => initrd.c} (87%)

diff --git a/efi/Makefile b/efi/Makefile
index 6693564f7071..4c35917475c0 100644
--- a/efi/Makefile
+++ b/efi/Makefile
@@ -4,3 +4,4 @@ obj-$(CONFIG_EFI_PAYLOAD)       += payload/
 obj-$(CONFIG_EFI_GUID)         += guid.o
 obj-$(CONFIG_EFI_DEVICEPATH)   += devicepath.o
 obj-y                          += errno.o handle.o efivar.o efivar-filename.o
+obj-y                          += initrd.o
diff --git a/efi/payload/efi-initrd.c b/efi/initrd.c
similarity index 87%
rename from efi/payload/efi-initrd.c
rename to efi/initrd.c
index 708418da62b7..7fd8e021f353 100644
--- a/efi/payload/efi-initrd.c
+++ b/efi/initrd.c
@@ -5,15 +5,14 @@
  * Copyright (c) 2025 Anis Chali <[email protected]>
  * Copyright (C) 2025 Ahmad Fatoum <[email protected]>
  */
-#include <common.h>
-#include <driver.h>
 #include <init.h>
 #include <linux/hw_random.h>
 #include <efi/devicepath.h>
+#include <efi/mode.h>
+#include <efi/services.h>
 #include <efi/protocol/file.h>
-#include <efi/protocol/initrd.h>
+#include <efi/initrd.h>
 #include <efi/guid.h>
-#include <efi/payload.h>
 #include <efi/error.h>
 
 static efi_status_t EFIAPI efi_initrd_load_file2(
@@ -73,6 +72,7 @@ static efi_status_t EFIAPI efi_initrd_load_file2(
 
 int efi_initrd_register(void *initrd_base, size_t initrd_sz)
 {
+       struct efi_boot_services *bs;
        efi_status_t efiret;
        int ret;
 
@@ -80,7 +80,11 @@ int efi_initrd_register(void *initrd_base, size_t initrd_sz)
        initrd.start = initrd_base;
        initrd.size = initrd_sz;
 
-       efiret = BS->install_multiple_protocol_interfaces(
+       bs = efi_get_boot_services();
+       if (!bs)
+               return -EOPNOTSUPP;
+
+       efiret = bs->install_multiple_protocol_interfaces(
                &initrd.lf2_handle, &efi_load_file2_protocol_guid, &initrd.base,
                &efi_device_path_protocol_guid, &initrd_dev_path, NULL);
        if (EFI_ERROR(efiret)) {
@@ -95,10 +99,12 @@ int efi_initrd_register(void *initrd_base, size_t initrd_sz)
 
 void efi_initrd_unregister(void)
 {
-       if (!initrd.base.load_file)
+       struct efi_boot_services *bs = efi_get_boot_services();
+
+       if (!bs || !initrd.base.load_file)
                return;
 
-       BS->uninstall_multiple_protocol_interfaces(
+       bs->uninstall_multiple_protocol_interfaces(
                initrd.lf2_handle, &efi_device_path_protocol_guid, 
&initrd_dev_path,
                &efi_load_file2_protocol_guid, &initrd.base, NULL);
 
diff --git a/efi/payload/Makefile b/efi/payload/Makefile
index 34efe6105d22..6306540ab595 100644
--- a/efi/payload/Makefile
+++ b/efi/payload/Makefile
@@ -4,7 +4,6 @@ obj-y += init.o
 obj-y += image.o
 obj-$(CONFIG_EFI_HANDOVER_PROTOCOL) += handover.o
 obj-y += bootm.o
-obj-y += efi-initrd.o
 obj-$(CONFIG_OFTREE) += fdt.o
 bbenv-y += env-efi
 obj-$(CONFIG_CMD_IOMEM) += iomem.o
-- 
2.47.3


Reply via email to