[PATCH] efi: payload: inform users on attempt to boot non EFI-stubbed kernel

Ahmad Fatoum a.fatoum at pengutronix.de
Thu Jun 20 00:16:16 PDT 2024


Calling bootm on an x86 kernel image that lacks an EFI stub will result
in a "no image handler found for image type MBR sector", which can be
very confusing.

Improve upon this by directly suggesting that the kernel's
CONFIG_EFI_STUB option needs to be enabled, so the image has the
expected PE magic (and the EFI stub code that comes with it).

Cc: cybin <info at arachnodroid.de>
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 efi/payload/image.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/efi/payload/image.c b/efi/payload/image.c
index 0795010cd9d2..6a41338f2a2f 100644
--- a/efi/payload/image.c
+++ b/efi/payload/image.c
@@ -337,6 +337,26 @@ static struct binfmt_hook binfmt_efi_hook = {
 	.hook = efi_execute,
 };
 
+static int do_bootm_mbr(struct image_data *data)
+{
+	/* On x86, Linux kernel images have a MBR magic at the end of
+	 * the first 512 byte sector and a PE magic if they're EFI-stubbed.
+	 * The PE magic has precedence over the MBR, so if we arrive in
+	 * this boot handler, the kernel has no EFI stub.
+	 *
+	 * Print a descriptive error message instead of "no image handler
+	 * found for image type MBR sector".
+	 */
+	pr_err("Can't boot MBR sector: Is CONFIG_EFI_STUB disabled in your Linux kernel config?\n");
+	return -ENOSYS;
+}
+
+static struct image_handler non_efi_handle_linux_x86 = {
+	.name = "non-EFI x86 Linux Image",
+	.bootm = do_bootm_mbr,
+	.filetype = filetype_mbr,
+};
+
 static struct binfmt_hook binfmt_arm64_efi_hook = {
 	.type = filetype_arm64_efi_linux_image,
 	.hook = efi_execute,
@@ -350,6 +370,9 @@ static int efi_register_image_handler(void)
 	if (IS_ENABLED(CONFIG_CPU_V8))
 		binfmt_register(&binfmt_arm64_efi_hook);
 
+	if (IS_ENABLED(CONFIG_X86))
+		register_image_handler(&non_efi_handle_linux_x86);
+
 	return 0;
 }
 late_efi_initcall(efi_register_image_handler);
-- 
2.39.2




More information about the barebox mailing list