From: Ahmad Fatoum <[email protected]>

Linux kernels built with CONFIG_EFI_ZBOOT are EFI applications that
decompress the actual kernel and hand it to the EFI stub. They carry
"zimg" at offset 4 and LINUX_PE_MAGIC at offset 0x38, but none of the
architecture-specific magics barebox looks for, so they have been
detected as plain MS-DOS executables so far.

bootm thus started them like any other EFI application: The Linux
bootargs weren't passed as load options and, as EFI payload, barebox
provided neither initrd nor device tree and didn't shut itself down.

Let's add a filetype for them, count it as Linux EFI image and register
image handlers and a binfmt hook, so they are booted the same way as the
EFI-stubbed kernels barebox already knows.

Tested with an arm64 Linux v7.2 zboot kernel and initrd on barebox as
EFI loader and as EFI payload running on top of it.

Assisted-by: Claude:opus-5.5
Signed-off-by: Ahmad Fatoum <[email protected]>
---
 common/filetype.c   | 5 ++++-
 efi/loader/bootm.c  | 7 +++++++
 efi/payload/bootm.c | 7 +++++++
 efi/payload/image.c | 6 ++++++
 include/filetype.h  | 8 ++++++++
 5 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/common/filetype.c b/common/filetype.c
index a25be6e2fc7e..439f0529be69 100644
--- a/common/filetype.c
+++ b/common/filetype.c
@@ -92,6 +92,7 @@ static const struct filetype_str filetype_str[] = {
        [filetype_x86_linux_image] = { "x86 Linux image", "x86-linux" },
        [filetype_x86_efi_linux_image] = { "x86 Linux/EFI image", 
"x86-efi-linux" },
        [filetype_pxa_ntim] = { "Marvell PXA3xx NTIM image", "pxa-ntim" },
+       [filetype_zboot_efi_linux_image] = { "Linux/EFI zboot image", 
"zboot-efi-linux" },
 };
 
 static const char *file_type_to_nr_string(enum filetype f)
@@ -460,7 +461,8 @@ enum filetype file_detect_type(const void *_buf, size_t 
bufsize)
                return is_dos_exe(buf8) ? filetype_arm_efi_zimage : 
filetype_arm_zimage;
 
        if (is_dos_exe(buf8))
-               return filetype_exe;
+               return is_zboot_efi_linux_image(buf) ?
+                       filetype_zboot_efi_linux_image : filetype_exe;
 
        if (bufsize < 256)
                return filetype_unknown;
@@ -605,6 +607,7 @@ bool filetype_is_linux_efi_image(enum filetype ft)
        case filetype_arm64_efi_linux_image:
        case filetype_riscv_efi_linux_image:
        case filetype_x86_efi_linux_image:
+       case filetype_zboot_efi_linux_image:
                return true;
        default:
                return false;
diff --git a/efi/loader/bootm.c b/efi/loader/bootm.c
index a68db742e173..4129441a47de 100644
--- a/efi/loader/bootm.c
+++ b/efi/loader/bootm.c
@@ -353,6 +353,12 @@ static struct image_handler arm32_linux_efi_handler = {
        .filetype = filetype_arm_efi_zimage,
 };
 
+static struct image_handler zboot_linux_efi_handler = {
+       .name = "Linux/EFI zboot image",
+       .bootm = efi_loader_bootm,
+       .filetype = filetype_zboot_efi_linux_image,
+};
+
 static struct image_handler efi_image_loader = {
        .name = "EFI Application",
        .bootm = efi_loader_bootm,
@@ -367,6 +373,7 @@ static int efiloader_bootm_register(void)
                register_image_handler(&aarch64_linux_efi_handler);
        if (IS_ENABLED(CONFIG_RISCV))
                register_image_handler(&riscv_linux_efi_handler);
+       register_image_handler(&zboot_linux_efi_handler);
        return register_image_handler(&efi_image_loader);
 }
 late_initcall(efiloader_bootm_register);
diff --git a/efi/payload/bootm.c b/efi/payload/bootm.c
index 2f9cc3cbf76b..d074709e3975 100644
--- a/efi/payload/bootm.c
+++ b/efi/payload/bootm.c
@@ -258,12 +258,19 @@ static struct image_handler efi_arm64_handle_tr = {
        .filetype = filetype_arm64_efi_linux_image,
 };
 
+static struct image_handler efi_zboot_handle_tr = {
+       .name = "EFI zboot Linux kernel",
+       .bootm = do_bootm_efi_stub,
+       .filetype = filetype_zboot_efi_linux_image,
+};
+
 BAREBOX_MAGICVAR(global.linux.efi.handover,
                 "Use legacy x86 handover protocol instead of StartImage 
BootService");
 
 static int efi_register_bootm_handler(void)
 {
        register_image_handler(&efi_app_handle_tr);
+       register_image_handler(&efi_zboot_handle_tr);
 
        if (IS_ENABLED(CONFIG_X86)) {
                if (IS_ENABLED(CONFIG_EFI_HANDOVER_PROTOCOL))
diff --git a/efi/payload/image.c b/efi/payload/image.c
index c5a04d7967a9..76aca1c43cec 100644
--- a/efi/payload/image.c
+++ b/efi/payload/image.c
@@ -200,9 +200,15 @@ static struct binfmt_hook binfmt_x86_efi_hook = {
        .hook = efi_execute,
 };
 
+static struct binfmt_hook binfmt_zboot_efi_hook = {
+       .type = filetype_zboot_efi_linux_image,
+       .hook = efi_execute,
+};
+
 static int efi_register_image_handler(void)
 {
        binfmt_register(&binfmt_efi_hook);
+       binfmt_register(&binfmt_zboot_efi_hook);
 
        if (IS_ENABLED(CONFIG_X86)) {
                register_image_handler(&non_efi_handle_linux_x86);
diff --git a/include/filetype.h b/include/filetype.h
index 91dc37b71c13..281f467abcb2 100644
--- a/include/filetype.h
+++ b/include/filetype.h
@@ -73,6 +73,7 @@ enum filetype {
        filetype_x86_linux_image,
        filetype_x86_efi_linux_image,
        filetype_pxa_ntim,
+       filetype_zboot_efi_linux_image,
        filetype_max,
 };
 
@@ -182,6 +183,13 @@ static inline bool is_barebox_riscv_head(const void 
*header)
               !memcmp(header + 48, "barebox", 8);
 }
 
+/* "zimg" image type and LINUX_PE_MAGIC, see Linux zboot-header.S */
+static inline bool is_zboot_efi_linux_image(const void *header)
+{
+       return !memcmp(header + 4, "zimg", 4) &&
+              le32_to_cpup(header + 56) == 0x818223cd;
+}
+
 static inline bool is_x86_linux_bootimage(const void *header)
 {
        return get_unaligned_le32(header + 0x202) == 0x53726448;
-- 
2.47.3


Reply via email to