Skip to contentSkip to conversation

efi: Add generic magic number in header

PATCH In review Ard Biesheuvel 5 participants 13 messages Nov 29, 2022 – Dec 7, 2022

Subsystems (3)
lore
Share & tools
lore

Ard Biesheuvel

author maintainer
#0 lore raw

v2 cover efi: Add generic magic number in header

This is a minimal respin of the RFC patch I sent out a few week ago. No changes were applied except the /0x30/0x38 typo fix and some additional wording in the commit log. I also added a patch to make the command line initrd loader always built-in.

The purpose of all of this is to create *and document* a common baseline for EFI booting of Linux. (I know I promised some documentation myself, but I simply don't have time for that, so if anyone feels so inclined, please go ahead).

The idea is that EFI images with the LINUX_PE_MAGIC number are guaranteed to support:
- initrd loading via LoadFile2
- initrd loading via the command line
- (on x86) mixed mode boot via the .compat entry point
- other things I missed?

Architectures such as arm64 and RISC-V already have their own magic numbers, in which case the PE/COFF major/minor image version should be inspected, where 1.1 corresponds with the set described above.

If other architectures want to create hybrid images as well, it would be better to use a different offset to store the magic number, so that the generic EFI one can be carried as well. The reason for deviating from this for arm64 and RISC-V is the simple fact that existing images already exist, so for those architectures, we have to consider both anyway.

Cc Huacai Chen <[email protected]>
Cc Atish Patra <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Daniel Kiper <[email protected]>
Cc Leif Lindholm <[email protected]>

Ard Biesheuvel (2):
efi: libstub: Always enable initrd command line loader and bump
version
efi: Put Linux specific magic number in the DOS header

 arch/loongarch/kernel/head.S                   |  3 ++-
 arch/x86/boot/header.S                         |  3 ++-
 drivers/firmware/efi/Kconfig                   | 15 ---------------
 drivers/firmware/efi/libstub/efi-stub-helper.c |  3 +--
 drivers/firmware/efi/libstub/zboot-header.S    |  3 ++-
 include/linux/pe.h                             |  9 ++++++++-
 6 files changed, 15 insertions(+), 21 deletions(-)
Signature · 1 line
2.35.1
author maintainer
#1 lore raw

PATCH v2 1/2 efi: libstub: Always enable initrd command line loader and bump version

In preparation for setting a cross-architecture baseline for EFI boot support, remove the Kconfig option that permits the command line initrd loader to be disabled. Also, bump the minor version so that any image built with the new version can be identified as supporting this.

---
 drivers/firmware/efi/Kconfig                   | 15 ---------------
 drivers/firmware/efi/libstub/efi-stub-helper.c |  3 +--
 include/linux/pe.h                             |  2 +-
 3 files changed, 2 insertions(+), 18 deletions(-)
drivers/firmware/efi/Kconfig +0 -15
index 08ed88e49ea307b0..043ca31c114ebf2a 100644
@@ -106,21 +106,6 @@ config EFI_ARMSTUB_DTB_LOADER
 	  functionality for bootloaders that do not have such support
 	  this option is necessary.
 
-config EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER
-	bool "Enable the command line initrd loader" if !X86
-	depends on EFI_STUB && (EFI_GENERIC_STUB || X86)
-	default y if X86
-	help
-	  Select this config option to add support for the initrd= command
-	  line parameter, allowing an initrd to be loaded into memory that
-	  resides on a file system backed by an implementation of
-	  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.
-
-	  This method has been superseded by the simpler LoadFile2 based
-	  initrd loading method, but the initrd= loader is retained as it
-	  can be used from the UEFI Shell or other generic loaders that
-	  don't implement the Linux specific LoadFile2 method.
-
 config EFI_BOOTLOADER_CONTROL
 	tristate "EFI Bootloader Control"
 	select UCS2_STRING
drivers/firmware/efi/libstub/efi-stub-helper.c +1 -2
index 739454d8063af8b1..f5a4bdacac642846 100644
@@ -538,8 +538,7 @@ efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
 				     unsigned long soft_limit,
 				     unsigned long hard_limit)
 {
-	if (!IS_ENABLED(CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER) ||
-	    (IS_ENABLED(CONFIG_X86) && image == NULL))
+	if (image == NULL)
 		return EFI_UNSUPPORTED;
 
 	return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
include/linux/pe.h +1 -1
index 1d3836ef9d92dcd8..056a1762de904fc1 100644
@@ -29,7 +29,7 @@
  * handover_offset and xloadflags fields in the bootparams structure.
  */
 #define LINUX_EFISTUB_MAJOR_VERSION		0x1
-#define LINUX_EFISTUB_MINOR_VERSION		0x0
+#define LINUX_EFISTUB_MINOR_VERSION		0x1
 
 #define MZ_MAGIC	0x5a4d	/* "MZ" */
 
Signature · 1 line
2.35.1
author maintainer
#2 lore raw

PATCH v2 2/2 efi: Put Linux specific magic number in the DOS header

In reply to Ard Biesheuvel

GRUB currently relies on the magic number in the image header of ARM and arm64 EFI kernel images to decide whether or not the image in question is a bootable kernel.

However, the purpose of the magic number is to identify the image as one that implements the bare metal boot protocol, and so GRUB, which only does EFI boot, can only boot images that could potentially be booted in a non-EFI manner as well.

This is problematic for the new zboot decompressor image format, as it can only boot in EFI mode, and must therefore not use the bare metal boot magic number in its header.

For this reason, the strict magic number was dropped from GRUB, to permit essentially any kind of EFI executable to be booted via the 'linux' command, blurring the line between the linux loader and the chainloader.

So let's use the same field in the DOS header that RISC-V and arm64 already use for their 'bare metal' magic numbers to store a 'generic Linux kernel' magic number, which can be used to identify bootable kernel images in PE format which don't necessarily implement a bare metal boot protocol in the same binary. Note that, in the context of EFI, the MSDOS header is only described in terms of the fields that it shares with the hybrid PE/COFF image format, (i.e., the magic number at offset #0 and the PE header offset at byte offset #0x3c). Since we aim for compatibility with EFI only, and not with MS-DOS or MS-Windows, we can use the remaining space in the MS-DOS header however we want.

Let's set the generic magic number for x86 images as well: existing bootloaders already have their own methods to identify x86 Linux images that can be booted in a non-EFI manner, and having the magic number in place there will ease any future transitions in loader implementations to merge the x86 and non-x86 EFI boot paths.

Note that 32-bit ARM already uses the same location in the header for a different purpose, but the ARM support is already widely implemented and the EFI zboot decompressor is not available on ARM anyway, so we just disregard it here.

Cc Huacai Chen <[email protected]>
Cc Atish Patra <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Daniel Kiper <[email protected]>
Cc Leif Lindholm <[email protected]>
---
 arch/loongarch/kernel/head.S                | 3 ++-
 arch/x86/boot/header.S                      | 3 ++-
 drivers/firmware/efi/libstub/zboot-header.S | 3 ++-
 include/linux/pe.h                          | 7 +++++++
 4 files changed, 13 insertions(+), 3 deletions(-)
arch/loongarch/kernel/head.S +2 -1
index 84970e2666588963..caa74439700eee93 100644
@@ -25,7 +25,8 @@ _head:
 	.dword	kernel_entry		/* Kernel entry point */
 	.dword	_end - _text		/* Kernel image effective size */
 	.quad	0			/* Kernel image load offset from start of RAM */
-	.org	0x3c			/* 0x20 ~ 0x3b reserved */
+	.org	0x38			/* 0x20 ~ 0x38 reserved */
+	.long	LINUX_PE_MAGIC
 	.long	pe_header - _head	/* Offset to the PE header */
 
 pe_header:
arch/x86/boot/header.S +2 -1
index f912d777013052ea..be8f78a7ee325475 100644
@@ -80,10 +80,11 @@ bs_die:
 	ljmp	$0xf000,$0xfff0
 
 #ifdef CONFIG_EFI_STUB
-	.org	0x3c
+	.org	0x38
 	#
 	# Offset to the PE header.
 	#
+	.long	LINUX_PE_MAGIC
 	.long	pe_header
 #endif /* CONFIG_EFI_STUB */
 
drivers/firmware/efi/libstub/zboot-header.S +2 -1
index bc2d7750d7f14174..ec4525d40e0cf6d6 100644
@@ -20,7 +20,8 @@ __efistub_efi_zboot_header:
 	.long		__efistub__gzdata_size - 12		// payload size
 	.long		0, 0					// reserved
 	.asciz		COMP_TYPE				// compression type
-	.org		.Ldoshdr + 0x3c
+	.org		.Ldoshdr + 0x38
+	.long		LINUX_PE_MAGIC
 	.long		.Lpehdr - .Ldoshdr			// PE header offset
 
 .Lpehdr:
include/linux/pe.h +7 -0
index 056a1762de904fc1..1db4c944efd78f51 100644
@@ -31,6 +31,13 @@
 #define LINUX_EFISTUB_MAJOR_VERSION		0x1
 #define LINUX_EFISTUB_MINOR_VERSION		0x1
 
+/*
+ * LINUX_PE_MAGIC appears at offset 0x38 into the MSDOS header of EFI bootable
+ * Linux kernel images that target the architecture as specified by the PE/COFF
+ * header machine type field.
+ */
+#define LINUX_PE_MAGIC	0x818223cd
+
 #define MZ_MAGIC	0x5a4d	/* "MZ" */
 
 #define PE_MAGIC		0x00004550	/* "PE\0\0" */
Signature · 1 line
2.35.1
Reviewed-by on v2 2/2
#3 lore raw

Re: [PATCH v2 2/2] efi: Put Linux specific magic number in the DOS header

Quoted 18 lines · Ard Biesheuvel

GRUB currently relies on the magic number in the image header of ARM and arm64 EFI kernel images to decide whether or not the image in question is a bootable kernel.

However, the purpose of the magic number is to identify the image as one that implements the bare metal boot protocol, and so GRUB, which only does EFI boot, can only boot images that could potentially be booted in a non-EFI manner as well.

This is problematic for the new zboot decompressor image format, as it can only boot in EFI mode, and must therefore not use the bare metal boot magic number in its header.

For this reason, the strict magic number was dropped from GRUB, to permit essentially any kind of EFI executable to be booted via the 'linux' command, blurring the line between the linux loader and the chainloader.

Quoted 6 lines

So let's use the same field in the DOS header that RISC-V and arm64 already use for their 'bare metal' magic numbers to store a 'generic Linux kernel' magic number, which can be used to identify bootable kernel images in PE format which don't necessarily implement a bare metal boot protocol in the same binary. Note that, in the context of EFI, the MSDOS header is only described in terms of the fields that it

s/MSDOS/MS-DOS/ to be consistent with other places in the patch...

Quoted 2 lines

shares with the hybrid PE/COFF image format, (i.e., the magic number at offset #0 and the PE header offset at byte offset #0x3c). Since we aim

s/the magic number at offset #0/MS-DOS EXE magic number at offset #0/?

"the magic number at offset #0" itself is confusing in the context of number of "magic number" phrases in the patch... :-)

Quoted 77 lines

for compatibility with EFI only, and not with MS-DOS or MS-Windows, we can use the remaining space in the MS-DOS header however we want.

Let's set the generic magic number for x86 images as well: existing bootloaders already have their own methods to identify x86 Linux images that can be booted in a non-EFI manner, and having the magic number in place there will ease any future transitions in loader implementations to merge the x86 and non-x86 EFI boot paths.

Note that 32-bit ARM already uses the same location in the header for a different purpose, but the ARM support is already widely implemented and the EFI zboot decompressor is not available on ARM anyway, so we just disregard it here.

Cc Huacai Chen <[email protected]>
Cc Atish Patra <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Daniel Kiper <[email protected]>
Cc Leif Lindholm <[email protected]>
---
 arch/loongarch/kernel/head.S                | 3 ++-
 arch/x86/boot/header.S                      | 3 ++-
 drivers/firmware/efi/libstub/zboot-header.S | 3 ++-
 include/linux/pe.h                          | 7 +++++++
 4 files changed, 13 insertions(+), 3 deletions(-)
arch/loongarch/kernel/head.S +2 -1
index 84970e2666588963..caa74439700eee93 100644
@@ -25,7 +25,8 @@ _head:
 	.dword	kernel_entry		/* Kernel entry point */
 	.dword	_end - _text		/* Kernel image effective size */
 	.quad	0			/* Kernel image load offset from start of RAM */
-	.org	0x3c			/* 0x20 ~ 0x3b reserved */
+	.org	0x38			/* 0x20 ~ 0x38 reserved */
+	.long	LINUX_PE_MAGIC
 	.long	pe_header - _head	/* Offset to the PE header */
 
 pe_header:
arch/x86/boot/header.S +2 -1
index f912d777013052ea..be8f78a7ee325475 100644
@@ -80,10 +80,11 @@ bs_die:
 	ljmp	$0xf000,$0xfff0
 
 #ifdef CONFIG_EFI_STUB
-	.org	0x3c
+	.org	0x38
 	#
 	# Offset to the PE header.
 	#
+	.long	LINUX_PE_MAGIC
 	.long	pe_header
 #endif /* CONFIG_EFI_STUB */
 
drivers/firmware/efi/libstub/zboot-header.S +2 -1
index bc2d7750d7f14174..ec4525d40e0cf6d6 100644
@@ -20,7 +20,8 @@ __efistub_efi_zboot_header:
 	.long		__efistub__gzdata_size - 12		// payload size
 	.long		0, 0					// reserved
 	.asciz		COMP_TYPE				// compression type
-	.org		.Ldoshdr + 0x3c
+	.org		.Ldoshdr + 0x38
+	.long		LINUX_PE_MAGIC
 	.long		.Lpehdr - .Ldoshdr			// PE header offset
 
 .Lpehdr:
include/linux/pe.h +0 -0
index 056a1762de904fc1..1db4c944efd78f51 100644

include/linux/pe.h line 35

Quoted 5 lines
include/linux/pe.h +2 -0
@@ -31,6 +31,13 @@
 #define LINUX_EFISTUB_MAJOR_VERSION		0x1
 #define LINUX_EFISTUB_MINOR_VERSION		0x1
 
+/*
+ * LINUX_PE_MAGIC appears at offset 0x38 into the MSDOS header of EFI bootable

s/MSDOS/MS-DOS/

include/linux/pe.h line 43

Quoted 8 lines
include/linux/pe.h +5 -0
+ * Linux kernel images that target the architecture as specified by the PE/COFF
+ * header machine type field.
+ */
+#define LINUX_PE_MAGIC	0x818223cd
+
 #define MZ_MAGIC	0x5a4d	/* "MZ" */
 
 #define PE_MAGIC		0x00004550	/* "PE\0\0" */

If you take into account these minor things mentioned above you can add

Atish, could you respin your GRUB "Unify ARM64 & RISC-V Linux Loader" patch set when both Ard's patches are in at least "efi" Linux kernel branch? Hmmm... I am not sure it will go through "efi" branch. Ard?

Daniel

author maintainer
#4 lore raw

Re: [PATCH v2 2/2] efi: Put Linux specific magic number in the DOS header

Quoted 129 lines · Daniel Kiper
Quoted 18 lines · Ard Biesheuvel

GRUB currently relies on the magic number in the image header of ARM and arm64 EFI kernel images to decide whether or not the image in question is a bootable kernel.

However, the purpose of the magic number is to identify the image as one that implements the bare metal boot protocol, and so GRUB, which only does EFI boot, can only boot images that could potentially be booted in a non-EFI manner as well.

This is problematic for the new zboot decompressor image format, as it can only boot in EFI mode, and must therefore not use the bare metal boot magic number in its header.

For this reason, the strict magic number was dropped from GRUB, to permit essentially any kind of EFI executable to be booted via the 'linux' command, blurring the line between the linux loader and the chainloader.

Quoted 6 lines

So let's use the same field in the DOS header that RISC-V and arm64 already use for their 'bare metal' magic numbers to store a 'generic Linux kernel' magic number, which can be used to identify bootable kernel images in PE format which don't necessarily implement a bare metal boot protocol in the same binary. Note that, in the context of EFI, the MSDOS header is only described in terms of the fields that it

s/MSDOS/MS-DOS/ to be consistent with other places in the patch...

Quoted 2 lines

shares with the hybrid PE/COFF image format, (i.e., the magic number at offset #0 and the PE header offset at byte offset #0x3c). Since we aim

s/the magic number at offset #0/MS-DOS EXE magic number at offset #0/?

"the magic number at offset #0" itself is confusing in the context of number of "magic number" phrases in the patch... :-)

Quoted 77 lines

for compatibility with EFI only, and not with MS-DOS or MS-Windows, we can use the remaining space in the MS-DOS header however we want.

Let's set the generic magic number for x86 images as well: existing bootloaders already have their own methods to identify x86 Linux images that can be booted in a non-EFI manner, and having the magic number in place there will ease any future transitions in loader implementations to merge the x86 and non-x86 EFI boot paths.

Note that 32-bit ARM already uses the same location in the header for a different purpose, but the ARM support is already widely implemented and the EFI zboot decompressor is not available on ARM anyway, so we just disregard it here.

Cc Huacai Chen <[email protected]>
Cc Atish Patra <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Daniel Kiper <[email protected]>
Cc Leif Lindholm <[email protected]>
---
 arch/loongarch/kernel/head.S                | 3 ++-
 arch/x86/boot/header.S                      | 3 ++-
 drivers/firmware/efi/libstub/zboot-header.S | 3 ++-
 include/linux/pe.h                          | 7 +++++++
 4 files changed, 13 insertions(+), 3 deletions(-)
arch/loongarch/kernel/head.S +2 -1
index 84970e2666588963..caa74439700eee93 100644
@@ -25,7 +25,8 @@ _head:
      .dword  kernel_entry            /* Kernel entry point */
      .dword  _end - _text            /* Kernel image effective size */
      .quad   0                       /* Kernel image load offset from start of RAM */
-     .org    0x3c                    /* 0x20 ~ 0x3b reserved */
+     .org    0x38                    /* 0x20 ~ 0x38 reserved */
+     .long   LINUX_PE_MAGIC
      .long   pe_header - _head       /* Offset to the PE header */
 
 pe_header:
arch/x86/boot/header.S +2 -1
index f912d777013052ea..be8f78a7ee325475 100644
@@ -80,10 +80,11 @@ bs_die:
      ljmp    $0xf000,$0xfff0
 
 #ifdef CONFIG_EFI_STUB
-     .org    0x3c
+     .org    0x38
      #
      # Offset to the PE header.
      #
+     .long   LINUX_PE_MAGIC
      .long   pe_header
 #endif /* CONFIG_EFI_STUB */
 
drivers/firmware/efi/libstub/zboot-header.S +2 -1
index bc2d7750d7f14174..ec4525d40e0cf6d6 100644
@@ -20,7 +20,8 @@ __efistub_efi_zboot_header:
      .long           __efistub__gzdata_size - 12             // payload size
      .long           0, 0                                    // reserved
      .asciz          COMP_TYPE                               // compression type
-     .org            .Ldoshdr + 0x3c
+     .org            .Ldoshdr + 0x38
+     .long           LINUX_PE_MAGIC
      .long           .Lpehdr - .Ldoshdr                      // PE header offset
 
 .Lpehdr:
include/linux/pe.h +0 -0
index 056a1762de904fc1..1db4c944efd78f51 100644

include/linux/pe.h line 35

Quoted 5 lines
include/linux/pe.h +2 -0
@@ -31,6 +31,13 @@
 #define LINUX_EFISTUB_MAJOR_VERSION          0x1
 #define LINUX_EFISTUB_MINOR_VERSION          0x1
 
+/*
+ * LINUX_PE_MAGIC appears at offset 0x38 into the MSDOS header of EFI bootable

s/MSDOS/MS-DOS/

Quoted 7 lines
include/linux/pe.h +5 -0
+ * Linux kernel images that target the architecture as specified by the PE/COFF
+ * header machine type field.
+ */
+#define LINUX_PE_MAGIC       0x818223cd
+
 #define MZ_MAGIC     0x5a4d  /* "MZ" */
 
Quoted 5 lines

include/linux/pe.h line 43

Quoted 1 line
include/linux/pe.h +0 -0
 #define PE_MAGIC             0x00004550      /* "PE\0\0" */

If you take into account these minor things mentioned above you can add

Thanks

Quoted 4 lines

Atish, could you respin your GRUB "Unify ARM64 & RISC-V Linux Loader" patch set when both Ard's patches are in at least "efi" Linux kernel branch? Hmmm... I am not sure it will go through "efi" branch. Ard?

Yes, I already pushed a version, but I will update the patch to take your feedback into account.

The patch is queued up for v6.2, so it should be in mainline by the end of the year.

#5 lore raw

Re: [PATCH v2 2/2] efi: Put Linux specific magic number in the DOS header

Quoted 129 lines · Ard Biesheuvel
Quoted 128 lines · Daniel Kiper
Quoted 18 lines · Ard Biesheuvel

GRUB currently relies on the magic number in the image header of ARM and arm64 EFI kernel images to decide whether or not the image in question is a bootable kernel.

However, the purpose of the magic number is to identify the image as one that implements the bare metal boot protocol, and so GRUB, which only does EFI boot, can only boot images that could potentially be booted in a non-EFI manner as well.

This is problematic for the new zboot decompressor image format, as it can only boot in EFI mode, and must therefore not use the bare metal boot magic number in its header.

For this reason, the strict magic number was dropped from GRUB, to permit essentially any kind of EFI executable to be booted via the 'linux' command, blurring the line between the linux loader and the chainloader.

Quoted 6 lines

So let's use the same field in the DOS header that RISC-V and arm64 already use for their 'bare metal' magic numbers to store a 'generic Linux kernel' magic number, which can be used to identify bootable kernel images in PE format which don't necessarily implement a bare metal boot protocol in the same binary. Note that, in the context of EFI, the MSDOS header is only described in terms of the fields that it

s/MSDOS/MS-DOS/ to be consistent with other places in the patch...

Quoted 2 lines

shares with the hybrid PE/COFF image format, (i.e., the magic number at offset #0 and the PE header offset at byte offset #0x3c). Since we aim

s/the magic number at offset #0/MS-DOS EXE magic number at offset #0/?

"the magic number at offset #0" itself is confusing in the context of number of "magic number" phrases in the patch... :-)

Quoted 77 lines

for compatibility with EFI only, and not with MS-DOS or MS-Windows, we can use the remaining space in the MS-DOS header however we want.

Let's set the generic magic number for x86 images as well: existing bootloaders already have their own methods to identify x86 Linux images that can be booted in a non-EFI manner, and having the magic number in place there will ease any future transitions in loader implementations to merge the x86 and non-x86 EFI boot paths.

Note that 32-bit ARM already uses the same location in the header for a different purpose, but the ARM support is already widely implemented and the EFI zboot decompressor is not available on ARM anyway, so we just disregard it here.

Cc Huacai Chen <[email protected]>
Cc Atish Patra <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Daniel Kiper <[email protected]>
Cc Leif Lindholm <[email protected]>
---
 arch/loongarch/kernel/head.S                | 3 ++-
 arch/x86/boot/header.S                      | 3 ++-
 drivers/firmware/efi/libstub/zboot-header.S | 3 ++-
 include/linux/pe.h                          | 7 +++++++
 4 files changed, 13 insertions(+), 3 deletions(-)
arch/loongarch/kernel/head.S +2 -1
index 84970e2666588963..caa74439700eee93 100644
@@ -25,7 +25,8 @@ _head:
      .dword  kernel_entry            /* Kernel entry point */
      .dword  _end - _text            /* Kernel image effective size */
      .quad   0                       /* Kernel image load offset from start of RAM */
-     .org    0x3c                    /* 0x20 ~ 0x3b reserved */
+     .org    0x38                    /* 0x20 ~ 0x38 reserved */
+     .long   LINUX_PE_MAGIC
      .long   pe_header - _head       /* Offset to the PE header */
 
 pe_header:
arch/x86/boot/header.S +2 -1
index f912d777013052ea..be8f78a7ee325475 100644
@@ -80,10 +80,11 @@ bs_die:
      ljmp    $0xf000,$0xfff0
 
 #ifdef CONFIG_EFI_STUB
-     .org    0x3c
+     .org    0x38
      #
      # Offset to the PE header.
      #
+     .long   LINUX_PE_MAGIC
      .long   pe_header
 #endif /* CONFIG_EFI_STUB */
 
drivers/firmware/efi/libstub/zboot-header.S +2 -1
index bc2d7750d7f14174..ec4525d40e0cf6d6 100644
@@ -20,7 +20,8 @@ __efistub_efi_zboot_header:
      .long           __efistub__gzdata_size - 12             // payload size
      .long           0, 0                                    // reserved
      .asciz          COMP_TYPE                               // compression type
-     .org            .Ldoshdr + 0x3c
+     .org            .Ldoshdr + 0x38
+     .long           LINUX_PE_MAGIC
      .long           .Lpehdr - .Ldoshdr                      // PE header offset
 
 .Lpehdr:
include/linux/pe.h +0 -0
index 056a1762de904fc1..1db4c944efd78f51 100644

include/linux/pe.h line 35

Quoted 5 lines
include/linux/pe.h +2 -0
@@ -31,6 +31,13 @@
 #define LINUX_EFISTUB_MAJOR_VERSION          0x1
 #define LINUX_EFISTUB_MINOR_VERSION          0x1
 
+/*
+ * LINUX_PE_MAGIC appears at offset 0x38 into the MSDOS header of EFI bootable

s/MSDOS/MS-DOS/

Quoted 7 lines
include/linux/pe.h +5 -0
+ * Linux kernel images that target the architecture as specified by the PE/COFF
+ * header machine type field.
+ */
+#define LINUX_PE_MAGIC       0x818223cd
+
 #define MZ_MAGIC     0x5a4d  /* "MZ" */
 
Quoted 6 lines
Quoted 4 lines

include/linux/pe.h line 43

Quoted 1 line
include/linux/pe.h +0 -0
 #define PE_MAGIC             0x00004550      /* "PE\0\0" */

If you take into account these minor things mentioned above you can add

Thanks

You are welcome!

Quoted 6 lines
Quoted 3 lines

Atish, could you respin your GRUB "Unify ARM64 & RISC-V Linux Loader" patch set when both Ard's patches are in at least "efi" Linux kernel branch? Hmmm... I am not sure it will go through "efi" branch. Ard?

Yes, I already pushed a version, but I will update the patch to take your feedback into account.

Great!

Quoted 2 lines

The patch is queued up for v6.2, so it should be in mainline by the end of the year.

Cool! Thanks a lot!

Daniel

#6 lore raw

Re: [PATCH v2 2/2] efi: Put Linux specific magic number in the DOS header

In reply to Ard Biesheuvel
Quoted 65 lines · Ard Biesheuvel
Quoted 64 lines · Daniel Kiper
Quoted 18 lines · Ard Biesheuvel

GRUB currently relies on the magic number in the image header of ARM and arm64 EFI kernel images to decide whether or not the image in question is a bootable kernel.

However, the purpose of the magic number is to identify the image as one that implements the bare metal boot protocol, and so GRUB, which only does EFI boot, can only boot images that could potentially be booted in a non-EFI manner as well.

This is problematic for the new zboot decompressor image format, as it can only boot in EFI mode, and must therefore not use the bare metal boot magic number in its header.

For this reason, the strict magic number was dropped from GRUB, to permit essentially any kind of EFI executable to be booted via the 'linux' command, blurring the line between the linux loader and the chainloader.

Quoted 6 lines

So let's use the same field in the DOS header that RISC-V and arm64 already use for their 'bare metal' magic numbers to store a 'generic Linux kernel' magic number, which can be used to identify bootable kernel images in PE format which don't necessarily implement a bare metal boot protocol in the same binary. Note that, in the context of EFI, the MSDOS header is only described in terms of the fields that it

s/MSDOS/MS-DOS/ to be consistent with other places in the patch...

Quoted 2 lines

shares with the hybrid PE/COFF image format, (i.e., the magic number at offset #0 and the PE header offset at byte offset #0x3c). Since we aim

s/the magic number at offset #0/MS-DOS EXE magic number at offset #0/?

"the magic number at offset #0" itself is confusing in the context of number of "magic number" phrases in the patch... :-)

Quoted 27 lines

for compatibility with EFI only, and not with MS-DOS or MS-Windows, we can use the remaining space in the MS-DOS header however we want.

Let's set the generic magic number for x86 images as well: existing bootloaders already have their own methods to identify x86 Linux images that can be booted in a non-EFI manner, and having the magic number in place there will ease any future transitions in loader implementations to merge the x86 and non-x86 EFI boot paths.

Note that 32-bit ARM already uses the same location in the header for a different purpose, but the ARM support is already widely implemented and the EFI zboot decompressor is not available on ARM anyway, so we just disregard it here.

Cc Huacai Chen <[email protected]>
Cc Atish Patra <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Daniel Kiper <[email protected]>
Cc Leif Lindholm <[email protected]>
---
 arch/loongarch/kernel/head.S                | 3 ++-
 arch/x86/boot/header.S                      | 3 ++-
 drivers/firmware/efi/libstub/zboot-header.S | 3 ++-
 include/linux/pe.h                          | 7 +++++++
 4 files changed, 13 insertions(+), 3 deletions(-)
Quoted 10 lines
Quoted 10 lines

arch/loongarch/kernel/head.S _head: · line 28

Quoted 10 lines
arch/loongarch/kernel/head.S +1 -1
index 84970e2666588963..caa74439700eee93 100644
@@ -25,7 +25,8 @@ _head:
      .dword  kernel_entry            /* Kernel entry point */
      .dword  _end - _text            /* Kernel image effective size */
      .quad   0                       /* Kernel image load offset from start of RAM */
-     .org    0x3c                    /* 0x20 ~ 0x3b reserved */
+     .org    0x38                    /* 0x20 ~ 0x38 reserved */

Sadly one more thing... :-(

s/0x20 ~ 0x38 reserved/0x20 ~ 0x37 reserved/

Daniel

#7 lore raw

Re: [PATCH v2 2/2] efi: Put Linux specific magic number in the DOS header

In reply to Daniel Kiper
Quoted 129 lines · Daniel Kiper
Quoted 18 lines · Ard Biesheuvel

GRUB currently relies on the magic number in the image header of ARM and arm64 EFI kernel images to decide whether or not the image in question is a bootable kernel.

However, the purpose of the magic number is to identify the image as one that implements the bare metal boot protocol, and so GRUB, which only does EFI boot, can only boot images that could potentially be booted in a non-EFI manner as well.

This is problematic for the new zboot decompressor image format, as it can only boot in EFI mode, and must therefore not use the bare metal boot magic number in its header.

For this reason, the strict magic number was dropped from GRUB, to permit essentially any kind of EFI executable to be booted via the 'linux' command, blurring the line between the linux loader and the chainloader.

Quoted 6 lines

So let's use the same field in the DOS header that RISC-V and arm64 already use for their 'bare metal' magic numbers to store a 'generic Linux kernel' magic number, which can be used to identify bootable kernel images in PE format which don't necessarily implement a bare metal boot protocol in the same binary. Note that, in the context of EFI, the MSDOS header is only described in terms of the fields that it

s/MSDOS/MS-DOS/ to be consistent with other places in the patch...

Quoted 2 lines

shares with the hybrid PE/COFF image format, (i.e., the magic number at offset #0 and the PE header offset at byte offset #0x3c). Since we aim

s/the magic number at offset #0/MS-DOS EXE magic number at offset #0/?

"the magic number at offset #0" itself is confusing in the context of number of "magic number" phrases in the patch... :-)

Quoted 77 lines

for compatibility with EFI only, and not with MS-DOS or MS-Windows, we can use the remaining space in the MS-DOS header however we want.

Let's set the generic magic number for x86 images as well: existing bootloaders already have their own methods to identify x86 Linux images that can be booted in a non-EFI manner, and having the magic number in place there will ease any future transitions in loader implementations to merge the x86 and non-x86 EFI boot paths.

Note that 32-bit ARM already uses the same location in the header for a different purpose, but the ARM support is already widely implemented and the EFI zboot decompressor is not available on ARM anyway, so we just disregard it here.

Cc Huacai Chen <[email protected]>
Cc Atish Patra <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Daniel Kiper <[email protected]>
Cc Leif Lindholm <[email protected]>
---
 arch/loongarch/kernel/head.S                | 3 ++-
 arch/x86/boot/header.S                      | 3 ++-
 drivers/firmware/efi/libstub/zboot-header.S | 3 ++-
 include/linux/pe.h                          | 7 +++++++
 4 files changed, 13 insertions(+), 3 deletions(-)
arch/loongarch/kernel/head.S +2 -1
index 84970e2666588963..caa74439700eee93 100644
@@ -25,7 +25,8 @@ _head:
      .dword  kernel_entry            /* Kernel entry point */
      .dword  _end - _text            /* Kernel image effective size */
      .quad   0                       /* Kernel image load offset from start of RAM */
-     .org    0x3c                    /* 0x20 ~ 0x3b reserved */
+     .org    0x38                    /* 0x20 ~ 0x38 reserved */
+     .long   LINUX_PE_MAGIC
      .long   pe_header - _head       /* Offset to the PE header */
 
 pe_header:
arch/x86/boot/header.S +2 -1
index f912d777013052ea..be8f78a7ee325475 100644
@@ -80,10 +80,11 @@ bs_die:
      ljmp    $0xf000,$0xfff0
 
 #ifdef CONFIG_EFI_STUB
-     .org    0x3c
+     .org    0x38
      #
      # Offset to the PE header.
      #
+     .long   LINUX_PE_MAGIC
      .long   pe_header
 #endif /* CONFIG_EFI_STUB */
 
drivers/firmware/efi/libstub/zboot-header.S +2 -1
index bc2d7750d7f14174..ec4525d40e0cf6d6 100644
@@ -20,7 +20,8 @@ __efistub_efi_zboot_header:
      .long           __efistub__gzdata_size - 12             // payload size
      .long           0, 0                                    // reserved
      .asciz          COMP_TYPE                               // compression type
-     .org            .Ldoshdr + 0x3c
+     .org            .Ldoshdr + 0x38
+     .long           LINUX_PE_MAGIC
      .long           .Lpehdr - .Ldoshdr                      // PE header offset
 
 .Lpehdr:
include/linux/pe.h +0 -0
index 056a1762de904fc1..1db4c944efd78f51 100644

include/linux/pe.h line 35

Quoted 5 lines
include/linux/pe.h +2 -0
@@ -31,6 +31,13 @@
 #define LINUX_EFISTUB_MAJOR_VERSION          0x1
 #define LINUX_EFISTUB_MINOR_VERSION          0x1
 
+/*
+ * LINUX_PE_MAGIC appears at offset 0x38 into the MSDOS header of EFI bootable

s/MSDOS/MS-DOS/

Quoted 7 lines
include/linux/pe.h +5 -0
+ * Linux kernel images that target the architecture as specified by the PE/COFF
+ * header machine type field.
+ */
+#define LINUX_PE_MAGIC       0x818223cd
+
 #define MZ_MAGIC     0x5a4d  /* "MZ" */
 
Quoted 9 lines

include/linux/pe.h line 43

Quoted 1 line
include/linux/pe.h +0 -0
 #define PE_MAGIC             0x00004550      /* "PE\0\0" */

If you take into account these minor things mentioned above you can add

Atish, could you respin your GRUB "Unify ARM64 & RISC-V Linux Loader" patch set when both Ard's patches are in at least "efi" Linux kernel branch? Hmmm... I am not sure it will go through "efi" branch. Ard?

Sure. We need to remove the arch specific header files in the next version as well.
I am planning to revise the series before/during the holidays. Hope that's fine w.r.t Grub release.
Otherwise, please let me know.

Quoted 1 line

Daniel

Signature · 2 lines
Regards,
Atish
#8 lore raw

Re: [PATCH v2 2/2] efi: Put Linux specific magic number in the DOS header

In reply to Ard Biesheuvel
Quoted 137 lines · 在 2022-11-29星期二的 18:56 +0100,Ard Biesheuvel写道:

GRUB currently relies on the magic number in the image header of ARM and
arm64 EFI kernel images to decide whether or not the image in question
is a bootable kernel.

However, the purpose of the magic number is to identify the image as one
that implements the bare metal boot protocol, and so GRUB, which only does EFI boot, can only boot images that could potentially be booted in
a non-EFI manner as well.

This is problematic for the new zboot decompressor image format, as it
can only boot in EFI mode, and must therefore not use the bare metal boot magic number in its header.

For this reason, the strict magic number was dropped from GRUB, to permit essentially any kind of EFI executable to be booted via the 'linux' command, blurring the line between the linux loader and the chainloader.

So let's use the same field in the DOS header that RISC-V and arm64 already use for their 'bare metal' magic numbers to store a 'generic Linux kernel' magic number, which can be used to identify bootable kernel images in PE format which don't necessarily implement a bare metal boot protocol in the same binary. Note that, in the context of EFI, the MSDOS header is only described in terms of the fields that it
shares with the hybrid PE/COFF image format, (i.e., the magic number at
offset #0 and the PE header offset at byte offset #0x3c). Since we aim
for compatibility with EFI only, and not with MS-DOS or MS-Windows, we
can use the remaining space in the MS-DOS header however we want.

Let's set the generic magic number for x86 images as well: existing bootloaders already have their own methods to identify x86 Linux images
that can be booted in a non-EFI manner, and having the magic number in
place there will ease any future transitions in loader implementations
to merge the x86 and non-x86 EFI boot paths.

Note that 32-bit ARM already uses the same location in the header for a
different purpose, but the ARM support is already widely implemented and
the EFI zboot decompressor is not available on ARM anyway, so we just disregard it here.

Cc Huacai Chen <[email protected]>
Cc Atish Patra <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Daniel Kiper <[email protected]>
Cc Leif Lindholm <[email protected]>

---
 arch/loongarch/kernel/head.S                | 3 ++- arch/x86/boot/header.S                      | 3 ++- drivers/firmware/efi/libstub/zboot-header.S | 3 ++- include/linux/pe.h                          | 7 +++++++

 4 files changed, 13 insertions(+), 3 deletions(-)
arch/loongarch/kernel/head.S +0 -0
index 84970e2666588963..caa74439700eee93 100644
@@ -25,7 +25,8 @@ _head:

        .dword  kernel_entry            /* Kernel entry point */ .dword  _end - _text            /* Kernel image effective size */
        .quad   0                       /* Kernel image load offset from start of RAM */
-       .org    0x3c                    /* 0x20 ~ 0x3b reserved */ +       .org    0x38                    /* 0x20 ~ 0x38 reserved */ +       .long   LINUX_PE_MAGIC
        .long   pe_header - _head       /* Offset to the PE header */
 pe_header:

arch/x86/boot/header.S +0 -0
index f912d777013052ea..be8f78a7ee325475 100644
@@ -80,10 +80,11 @@ bs_die:

        ljmp    $0xf000,$0xfff0

 #ifdef CONFIG_EFI_STUB
-       .org    0x3c
+       .org    0x38
        #
        # Offset to the PE header.
        #
+       .long   LINUX_PE_MAGIC
        .long   pe_header
 #endif /* CONFIG_EFI_STUB */

drivers/firmware/efi/libstub/zboot-header.S +0 -0
index bc2d7750d7f14174..ec4525d40e0cf6d6 100644
@@ -20,7 +20,8 @@ __efistub_efi_zboot_header:

        .long           __efistub__gzdata_size - 12             // payload size
        .long           0, 0                                    // reserved
        .asciz          COMP_TYPE                               // compression type
-       .org            .Ldoshdr + 0x3c
+       .org            .Ldoshdr + 0x38
+       .long           LINUX_PE_MAGIC
        .long           .Lpehdr - .Ldoshdr                      // PE header offset

 .Lpehdr:

include/linux/pe.h +0 -0
index 056a1762de904fc1..1db4c944efd78f51 100644
@@ -31,6 +31,13 @@
  #define LINUX_EFISTUB_MAJOR_VERSION            0x1
  #define LINUX_EFISTUB_MINOR_VERSION            0x1


+/*
+ * LINUX_PE_MAGIC appears at offset 0x38 into the MSDOS header of EFI bootable
+ * Linux kernel images that target the architecture as specified by the PE/COFF
+ * header machine type field.
+ */

include/linux/pe.h +2 -0
+#define LINUX_PE_MAGIC 0x818223cd
+
Quoted 3 lines

 #define MZ_MAGIC       0x5a4d  /* "MZ" */

 #define PE_MAGIC               0x00004550      /* "PE\0\0" */

As far as I know, Archlinux automatically generates initramfs according to the version number in the kernel file. The latest generic compressed EFI designs do not seem to provide kernel version number information. This may change the usage habits of Archlinux users. Is it possible to add the kernel version number to vmlinuz.efi?

https://gitlab.archlinux.org/archlinux/mkinitcpio/mkinitcpio/-/blob/master/functions#L209

Signature · 2 lines
Best Regards
Xiaotian Wu
author maintainer
#9 lore raw

Re: [PATCH v2 2/2] efi: Put Linux specific magic number in the DOS header

Quoted 141 lines · Xiaotian Wu
Quoted 139 lines · 在 2022-11-29星期二的 18:56 +0100,Ard Biesheuvel写道:

GRUB currently relies on the magic number in the image header of ARM and
arm64 EFI kernel images to decide whether or not the image in question
is a bootable kernel.

However, the purpose of the magic number is to identify the image as one
that implements the bare metal boot protocol, and so GRUB, which only does EFI boot, can only boot images that could potentially be booted in
a non-EFI manner as well.

This is problematic for the new zboot decompressor image format, as it
can only boot in EFI mode, and must therefore not use the bare metal boot magic number in its header.

For this reason, the strict magic number was dropped from GRUB, to permit essentially any kind of EFI executable to be booted via the 'linux' command, blurring the line between the linux loader and the chainloader.

So let's use the same field in the DOS header that RISC-V and arm64 already use for their 'bare metal' magic numbers to store a 'generic Linux kernel' magic number, which can be used to identify bootable kernel images in PE format which don't necessarily implement a bare metal boot protocol in the same binary. Note that, in the context of EFI, the MSDOS header is only described in terms of the fields that it
shares with the hybrid PE/COFF image format, (i.e., the magic number at
offset #0 and the PE header offset at byte offset #0x3c). Since we aim
for compatibility with EFI only, and not with MS-DOS or MS-Windows, we
can use the remaining space in the MS-DOS header however we want.

Let's set the generic magic number for x86 images as well: existing bootloaders already have their own methods to identify x86 Linux images
that can be booted in a non-EFI manner, and having the magic number in
place there will ease any future transitions in loader implementations
to merge the x86 and non-x86 EFI boot paths.

Note that 32-bit ARM already uses the same location in the header for a
different purpose, but the ARM support is already widely implemented and
the EFI zboot decompressor is not available on ARM anyway, so we just disregard it here.

Cc Huacai Chen <[email protected]>
Cc Atish Patra <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Daniel Kiper <[email protected]>
Cc Leif Lindholm <[email protected]>
---
 arch/loongarch/kernel/head.S                | 3 ++-
 arch/x86/boot/header.S                      | 3 ++-
 drivers/firmware/efi/libstub/zboot-header.S | 3 ++-
 include/linux/pe.h                          | 7 +++++++
 4 files changed, 13 insertions(+), 3 deletions(-)
arch/loongarch/kernel/head.S +0 -0
index 84970e2666588963..caa74439700eee93 100644
@@ -25,7 +25,8 @@ _head:
        .dword  kernel_entry            /* Kernel entry point */
        .dword  _end - _text            /* Kernel image effective

size */
.quad 0 /* Kernel image load offset
from start of RAM */

arch/loongarch/kernel/head.S +2 -1
-       .org    0x3c                    /* 0x20 ~ 0x3b reserved */
+       .org    0x38                    /* 0x20 ~ 0x38 reserved */
+       .long   LINUX_PE_MAGIC
        .long   pe_header - _head       /* Offset to the PE header */
 
 pe_header:
arch/x86/boot/header.S +2 -1
index f912d777013052ea..be8f78a7ee325475 100644
@@ -80,10 +80,11 @@ bs_die:
        ljmp    $0xf000,$0xfff0
 
 #ifdef CONFIG_EFI_STUB
-       .org    0x3c
+       .org    0x38
        #
        # Offset to the PE header.
        #
+       .long   LINUX_PE_MAGIC
        .long   pe_header
 #endif /* CONFIG_EFI_STUB */
 
drivers/firmware/efi/libstub/zboot-header.S +0 -0
index bc2d7750d7f14174..ec4525d40e0cf6d6 100644
@@ -20,7 +20,8 @@ __efistub_efi_zboot_header:
        .long           __efistub__gzdata_size - 12             //

payload size
.long 0, 0 //
reserved
.asciz COMP_TYPE //
compression type

drivers/firmware/efi/libstub/zboot-header.S +2 -1
-       .org            .Ldoshdr + 0x3c
+       .org            .Ldoshdr + 0x38
+       .long           LINUX_PE_MAGIC
        .long           .Lpehdr - .Ldoshdr                      // PE

header offset

.Lpehdr:

include/linux/pe.h +2 -0
index 056a1762de904fc1..1db4c944efd78f51 100644
@@ -31,6 +31,13 @@
 #define LINUX_EFISTUB_MAJOR_VERSION            0x1
 #define LINUX_EFISTUB_MINOR_VERSION            0x1
 
+/*
+ * LINUX_PE_MAGIC appears at offset 0x38 into the MSDOS header of

EFI bootable
+ * Linux kernel images that target the architecture as specified by the PE/COFF
+ * header machine type field.
+ */

include/linux/pe.h +2 -0
+#define LINUX_PE_MAGIC 0x818223cd
+
 #define MZ_MAGIC       0x5a4d  /* "MZ" */
Quoted 6 lines
Quoted 1 line

#define PE_MAGIC 0x00004550 /* "PE\0\0" */

As far as I know, Archlinux automatically generates initramfs according to the version number in the kernel file. The latest generic compressed EFI designs do not seem to provide kernel version number information.

No, but nor do the gzip'ed images arm64 uses today, and the script seems take care of that.

The EFI zboot image has metadata in the header, so the script can decompress it and grep for the version number it it needs to.

Quoted 5 lines

This may change the usage habits of Archlinux users. Is it possible to add the kernel version number to vmlinuz.efi?

https://gitlab.archlinux.org/archlinux/mkinitcpio/mkinitcpio/-/blob/master/functions#L209

The current header doesn't have that so I don't think EFI zboot should be adding it either.

#10 lore raw

Re: [PATCH v2 2/2] efi: Put Linux specific magic number in the DOS header

Quoted 186 lines · 在 2022-12-07星期三的 09:06 +0100,Ard Biesheuvel写道:
Quoted 174 lines · Xiaotian Wu
Quoted 172 lines · 在 2022-11-29星期二的 18:56 +0100,Ard Biesheuvel写道:

GRUB currently relies on the magic number in the image header of ARM
and
arm64 EFI kernel images to decide whether or not the image in question
is a bootable kernel.

However, the purpose of the magic number is to identify the image as
one
that implements the bare metal boot protocol, and so GRUB, which only
does EFI boot, can only boot images that could potentially be booted
in
a non-EFI manner as well.

This is problematic for the new zboot decompressor image format, as
it
can only boot in EFI mode, and must therefore not use the bare metal
boot magic number in its header.

For this reason, the strict magic number was dropped from GRUB, to
permit essentially any kind of EFI executable to be booted via the
'linux' command, blurring the line between the linux loader and the
chainloader.

So let's use the same field in the DOS header that RISC-V and arm64
already use for their 'bare metal' magic numbers to store a 'generic
Linux kernel' magic number, which can be used to identify bootable
kernel images in PE format which don't necessarily implement a bare
metal boot protocol in the same binary. Note that, in the context of
EFI, the MSDOS header is only described in terms of the fields that
it
shares with the hybrid PE/COFF image format, (i.e., the magic number
at
offset #0 and the PE header offset at byte offset #0x3c). Since we
aim
for compatibility with EFI only, and not with MS-DOS or MS- Windows,
we
can use the remaining space in the MS-DOS header however we want.

Let's set the generic magic number for x86 images as well:
existing
bootloaders already have their own methods to identify x86 Linux images
that can be booted in a non-EFI manner, and having the magic number
in
place there will ease any future transitions in loader implementations
to merge the x86 and non-x86 EFI boot paths.

Note that 32-bit ARM already uses the same location in the header for
a
different purpose, but the ARM support is already widely implemented
and
the EFI zboot decompressor is not available on ARM anyway, so we just
disregard it here.

Cc Huacai Chen <[email protected]>
Cc Atish Patra <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Daniel Kiper <[email protected]>
Cc Leif Lindholm <[email protected]>

---
 arch/loongarch/kernel/head.S                | 3 ++- arch/x86/boot/header.S                      | 3 ++- drivers/firmware/efi/libstub/zboot-header.S | 3 ++- include/linux/pe.h                          | 7 +++++++

 4 files changed, 13 insertions(+), 3 deletions(-)
arch/loongarch/kernel/head.S +0 -0
index 84970e2666588963..caa74439700eee93 100644
@@ -25,7 +25,8 @@ _head:

        .dword  kernel_entry            /* Kernel entry point */ .dword  _end - _text            /* Kernel image effective size */
        .quad   0                       /* Kernel image load offset
from start of RAM */
-       .org    0x3c                    /* 0x20 ~ 0x3b reserved */
+       .org    0x38                    /* 0x20 ~ 0x38 reserved */
+       .long   LINUX_PE_MAGIC
        .long   pe_header - _head       /* Offset to the PE header */

 pe_header:

arch/x86/boot/header.S +0 -0
index f912d777013052ea..be8f78a7ee325475 100644
@@ -80,10 +80,11 @@ bs_die:

        ljmp    $0xf000,$0xfff0

 #ifdef CONFIG_EFI_STUB
-       .org    0x3c
+       .org    0x38
        #
        # Offset to the PE header.
        #
+       .long   LINUX_PE_MAGIC
        .long   pe_header
 #endif /* CONFIG_EFI_STUB */

drivers/firmware/efi/libstub/zboot-header.S +0 -0
index bc2d7750d7f14174..ec4525d40e0cf6d6 100644
@@ -20,7 +20,8 @@ __efistub_efi_zboot_header:

        .long           __efistub__gzdata_size - 12 //
payload size
        .long           0, 0
//
reserved
        .asciz          COMP_TYPE
//
compression type
-       .org            .Ldoshdr + 0x3c
+       .org            .Ldoshdr + 0x38
+       .long           LINUX_PE_MAGIC .long           .Lpehdr - .Ldoshdr // PE
header offset

 .Lpehdr:

include/linux/pe.h +2 -0
index 056a1762de904fc1..1db4c944efd78f51 100644
@@ -31,6 +31,13 @@
  #define LINUX_EFISTUB_MAJOR_VERSION            0x1
  #define LINUX_EFISTUB_MINOR_VERSION            0x1
 
+/*
+ * LINUX_PE_MAGIC appears at offset 0x38 into the MSDOS header

of
EFI bootable
+ * Linux kernel images that target the architecture as specified by
the PE/COFF
+ * header machine type field.
+ */

include/linux/pe.h +2 -0
+#define LINUX_PE_MAGIC 0x818223cd
+

 #define MZ_MAGIC       0x5a4d  /* "MZ" */

Quoted 9 lines
Quoted 1 line

 #define PE_MAGIC               0x00004550      /* "PE\0\0" */

As far as I know, Archlinux automatically generates initramfs according
to the version number in the kernel file. The latest generic compressed
EFI designs do not seem to provide kernel version number information.

Quoted 5 lines

No, but nor do the gzip'ed images arm64 uses today, and the script seems take care of that.

The EFI zboot image has metadata in the header, so the script can decompress it and grep for the version number it it needs to.

I tested it, and the decompression can be completed, thank you.

Quoted 16 lines
Quoted 7 lines

This may change the usage habits of Archlinux users. Is it possible to
add the kernel version number to vmlinuz.efi?

https://gitlab.archlinux.org/archlinux/mkinitcpio/mkinitcpio/-/blob/master/functions#L209

The current header doesn't have that so I don't think EFI zboot should
be adding it either.

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 2 lines
Best Regards
Xiaotian Wu
author maintainer
#11 lore raw

Re: [PATCH v2 2/2] efi: Put Linux specific magic number in the DOS header

Quoted 188 lines · Xiaotian Wu
Quoted 186 lines · 在 2022-12-07星期三的 09:06 +0100,Ard Biesheuvel写道:
Quoted 183 lines · Xiaotian Wu
Quoted 168 lines · 在 2022-11-29星期二的 18:56 +0100,Ard Biesheuvel写道:

GRUB currently relies on the magic number in the image header of ARM
and
arm64 EFI kernel images to decide whether or not the image in question
is a bootable kernel.

However, the purpose of the magic number is to identify the image as
one
that implements the bare metal boot protocol, and so GRUB, which only
does EFI boot, can only boot images that could potentially be booted
in
a non-EFI manner as well.

This is problematic for the new zboot decompressor image format, as
it
can only boot in EFI mode, and must therefore not use the bare metal
boot magic number in its header.

For this reason, the strict magic number was dropped from GRUB, to
permit essentially any kind of EFI executable to be booted via the
'linux' command, blurring the line between the linux loader and the
chainloader.

So let's use the same field in the DOS header that RISC-V and arm64
already use for their 'bare metal' magic numbers to store a 'generic
Linux kernel' magic number, which can be used to identify bootable
kernel images in PE format which don't necessarily implement a bare
metal boot protocol in the same binary. Note that, in the context of
EFI, the MSDOS header is only described in terms of the fields that
it
shares with the hybrid PE/COFF image format, (i.e., the magic number
at
offset #0 and the PE header offset at byte offset #0x3c). Since we
aim
for compatibility with EFI only, and not with MS-DOS or MS- Windows,
we
can use the remaining space in the MS-DOS header however we want.

Let's set the generic magic number for x86 images as well:
existing
bootloaders already have their own methods to identify x86 Linux images
that can be booted in a non-EFI manner, and having the magic number
in
place there will ease any future transitions in loader implementations
to merge the x86 and non-x86 EFI boot paths.

Note that 32-bit ARM already uses the same location in the header for
a
different purpose, but the ARM support is already widely implemented
and
the EFI zboot decompressor is not available on ARM anyway, so we just
disregard it here.

Cc Huacai Chen <[email protected]>
Cc Atish Patra <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Daniel Kiper <[email protected]>
Cc Leif Lindholm <[email protected]>
---
 arch/loongarch/kernel/head.S                | 3 ++-
 arch/x86/boot/header.S                      | 3 ++-
 drivers/firmware/efi/libstub/zboot-header.S | 3 ++-
 include/linux/pe.h                          | 7 +++++++
 4 files changed, 13 insertions(+), 3 deletions(-)
arch/loongarch/kernel/head.S +0 -0
index 84970e2666588963..caa74439700eee93 100644
@@ -25,7 +25,8 @@ _head:
        .dword  kernel_entry            /* Kernel entry point */
        .dword  _end - _text            /* Kernel image effective

size */
.quad 0 /* Kernel image load
offset
from start of RAM */
- .org 0x3c /* 0x20 ~ 0x3b reserved
*/
+ .org 0x38 /* 0x20 ~ 0x38 reserved
*/
+ .long LINUX_PE_MAGIC
.long pe_header - _head /* Offset to the PE
header */

pe_header:

arch/x86/boot/header.S +2 -1
index f912d777013052ea..be8f78a7ee325475 100644
@@ -80,10 +80,11 @@ bs_die:
        ljmp    $0xf000,$0xfff0
 
 #ifdef CONFIG_EFI_STUB
-       .org    0x3c
+       .org    0x38
        #
        # Offset to the PE header.
        #
+       .long   LINUX_PE_MAGIC
        .long   pe_header
 #endif /* CONFIG_EFI_STUB */
 
drivers/firmware/efi/libstub/zboot-header.S +0 -0
index bc2d7750d7f14174..ec4525d40e0cf6d6 100644
@@ -20,7 +20,8 @@ __efistub_efi_zboot_header:
        .long           __efistub__gzdata_size - 12
 //

payload size
.long 0, 0
//
reserved
.asciz COMP_TYPE
//
compression type

drivers/firmware/efi/libstub/zboot-header.S +2 -1
-       .org            .Ldoshdr + 0x3c
+       .org            .Ldoshdr + 0x38
+       .long           LINUX_PE_MAGIC
        .long           .Lpehdr - .Ldoshdr

// PE
header offset

.Lpehdr:

include/linux/pe.h +2 -0
index 056a1762de904fc1..1db4c944efd78f51 100644
@@ -31,6 +31,13 @@
 #define LINUX_EFISTUB_MAJOR_VERSION            0x1
 #define LINUX_EFISTUB_MINOR_VERSION            0x1
 
+/*
+ * LINUX_PE_MAGIC appears at offset 0x38 into the MSDOS header

of
EFI bootable
+ * Linux kernel images that target the architecture as specified by
the PE/COFF
+ * header machine type field.
+ */

include/linux/pe.h line 43

Quoted 5 lines
include/linux/pe.h +2 -0
+#define LINUX_PE_MAGIC 0x818223cd
+
 #define MZ_MAGIC       0x5a4d  /* "MZ" */
 
 #define PE_MAGIC               0x00004550      /* "PE\0\0" */

As far as I know, Archlinux automatically generates initramfs according
to the version number in the kernel file. The latest generic compressed
EFI designs do not seem to provide kernel version number information.

Quoted 7 lines
Quoted 5 lines

No, but nor do the gzip'ed images arm64 uses today, and the script seems take care of that.

The EFI zboot image has metadata in the header, so the script can decompress it and grep for the version number it it needs to.

I tested it, and the decompression can be completed, thank you.

Excellent! Thanks for confirming.

Acked-by on the v2 series
#12 lore raw
In reply to Ard Biesheuvel
Quoted 28 lines · Ard Biesheuvel

This is a minimal respin of the RFC patch I sent out a few week ago. No changes were applied except the /0x30/0x38 typo fix and some additional wording in the commit log. I also added a patch to make the command line initrd loader always built-in.

The purpose of all of this is to create *and document* a common baseline for EFI booting of Linux. (I know I promised some documentation myself, but I simply don't have time for that, so if anyone feels so inclined, please go ahead).

The idea is that EFI images with the LINUX_PE_MAGIC number are guaranteed to support:
- initrd loading via LoadFile2
- initrd loading via the command line
- (on x86) mixed mode boot via the .compat entry point
- other things I missed?

Architectures such as arm64 and RISC-V already have their own magic numbers, in which case the PE/COFF major/minor image version should be inspected, where 1.1 corresponds with the set described above.

If other architectures want to create hybrid images as well, it would be better to use a different offset to store the magic number, so that the generic EFI one can be carried as well. The reason for deviating from this for arm64 and RISC-V is the simple fact that existing images already exist, so for those architectures, we have to consider both anyway.

Quoted 5 lines
Cc Huacai Chen <[email protected]>
Cc Atish Patra <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Daniel Kiper <[email protected]>
Cc Leif Lindholm <[email protected]>

For the series:

Thanks!

Quoted 17 lines

Ard Biesheuvel (2):
efi: libstub: Always enable initrd command line loader and bump
version
efi: Put Linux specific magic number in the DOS header

 arch/loongarch/kernel/head.S                   |  3 ++-
 arch/x86/boot/header.S                         |  3 ++-
 drivers/firmware/efi/Kconfig                   | 15 ---------------
 drivers/firmware/efi/libstub/efi-stub-helper.c |  3 +--
 drivers/firmware/efi/libstub/zboot-header.S    |  3 ++-
 include/linux/pe.h                             |  9 ++++++++-
 6 files changed, 15 insertions(+), 21 deletions(-)
Signature · 1 line
2.35.1