mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH v3 01/12] efi: treat EFI zboot Linux images like other EFI-stubbed kernels
Date: Mon, 26 Oct 2026 13:09:52 +0100	[thread overview]
Message-ID: <20261026121150.455510-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20261026121150.455510-1-a.fatoum@pengutronix.de>

From: Ahmad Fatoum <a.fatoum@barebox.org>

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 <a.fatoum@barebox.org>
---
 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	other threads:[~2026-10-26 12:13 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-26 12:09 [PATCH v3 00/12] efi: payload: allow passing arguments to UKIs Ahmad Fatoum
2026-10-26 12:09 ` Ahmad Fatoum [this message]
2026-10-26 12:09 ` [PATCH v3 02/12] commands: devlookup: honour global.bootm.root_param for -k Ahmad Fatoum
2026-10-26 12:09 ` [PATCH v3 03/12] common: bootargs: don't leave linux_bootargs dangling after free Ahmad Fatoum
2026-10-26 12:09 ` [PATCH v3 04/12] common: bootargs: drop legacy bootargs fallback with FLEXIBLE_BOOTARGS Ahmad Fatoum
2026-10-26 12:09 ` [PATCH v3 05/12] globalvar: skip empty variables in globalvar_get_match() Ahmad Fatoum
2026-10-26 12:09 ` [PATCH v3 06/12] efi: payload: honour bootm dryrun in the EFI application handler Ahmad Fatoum
2026-10-26 12:09 ` [PATCH v3 07/12] efi: payload: always shutdown barebox when booting Ahmad Fatoum
2026-10-26 12:09 ` [PATCH v3 08/12] efi: payload: pass shell arguments as load options to executed images Ahmad Fatoum
2026-10-26 12:10 ` [PATCH v3 09/12] efi: add global.efi.bootargs for bootm'd EFI applications Ahmad Fatoum
2026-10-26 12:10 ` [PATCH v3 10/12] Documentation: efi: describe load options handling Ahmad Fatoum
2026-10-26 12:10 ` [PATCH v3 11/12] test: py: efiloader: check global.efi.bootargs reaches the kernel Ahmad Fatoum
2026-10-26 12:10 ` [PATCH v3 12/12] boot: report exit status of failing boot scripts Ahmad Fatoum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261026121150.455510-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox