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: cybin <info@arachnodroid.de>, Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH] efi: payload: inform users on attempt to boot non EFI-stubbed kernel
Date: Thu, 20 Jun 2024 09:16:16 +0200	[thread overview]
Message-ID: <20240620071616.3228015-1-a.fatoum@pengutronix.de> (raw)

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@arachnodroid.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@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




             reply	other threads:[~2024-06-20  7:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-20  7:16 Ahmad Fatoum [this message]
2024-06-20  7:39 ` Michael Taubert
2024-07-01 12:48 ` Sascha Hauer

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=20240620071616.3228015-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=info@arachnodroid.de \
    /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