mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] efi: payload: inform users on attempt to boot non EFI-stubbed kernel
@ 2024-06-20  7:16 Ahmad Fatoum
  2024-06-20  7:39 ` Michael Taubert
  2024-07-01 12:48 ` Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2024-06-20  7:16 UTC (permalink / raw)
  To: barebox; +Cc: cybin, Ahmad Fatoum

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




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] efi: payload: inform users on attempt to boot non EFI-stubbed kernel
  2024-06-20  7:16 [PATCH] efi: payload: inform users on attempt to boot non EFI-stubbed kernel Ahmad Fatoum
@ 2024-06-20  7:39 ` Michael Taubert
  2024-07-01 12:48 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Michael Taubert @ 2024-06-20  7:39 UTC (permalink / raw)
  To: Ahmad Fatoum, barebox

Tested-by: Michael Taubert <info@arachnodroid.de>

Am 20.06.24 um 09:16 schrieb Ahmad Fatoum:
> 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);



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] efi: payload: inform users on attempt to boot non EFI-stubbed kernel
  2024-06-20  7:16 [PATCH] efi: payload: inform users on attempt to boot non EFI-stubbed kernel Ahmad Fatoum
  2024-06-20  7:39 ` Michael Taubert
@ 2024-07-01 12:48 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2024-07-01 12:48 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum; +Cc: cybin


On Thu, 20 Jun 2024 09:16:16 +0200, Ahmad Fatoum wrote:
> 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).
> 
> [...]

Applied, thanks!

[1/1] efi: payload: inform users on attempt to boot non EFI-stubbed kernel
      https://git.pengutronix.de/cgit/barebox/commit/?id=d9ec1ad67442 (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-07-01 12:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-20  7:16 [PATCH] efi: payload: inform users on attempt to boot non EFI-stubbed kernel Ahmad Fatoum
2024-06-20  7:39 ` Michael Taubert
2024-07-01 12:48 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox