Skip to contentSkip to conversation

x86: Multiboot PE support

PATCH Superseded Ross Lagerwall 1 participant 5 messages Mar 13, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86: Multiboot PE support

Hi,

This patches series implements support for xen.gz being a gzipped multiboot-capable PE binary rather than an ELF binary. The purpose of this is to allow the same xen.gz to be booted using BIOS, UEFI, and UEFI with Secure Boot verification just like it can be done with a Linux kernel. It also means that it is possible to enable Secure Boot while still retaining the flexibility of a full bootloader like GRUB2 - not currently possible when using xen.efi.

This requires some (backwards-compatible) extensions to the multiboot2 protocol which will be sent in a separate series along with the GRUB changes which implement the extensions.

Ross

Ross Lagerwall (4):
multiboot2: Advertise the load type
x86: Add support for building a multiboot2 PE binary
x86: Hand-edit coff flags to remove RELOCS_STRIPPED flag
x86: Call Shim Verify in the multiboot2 path

 .gitignore                        |  1 +
 xen/Makefile                      |  1 +
 xen/arch/x86/Kconfig              |  6 +++
 xen/arch/x86/Makefile             | 50 +++++++++++++++++++
 xen/arch/x86/boot/head.S          | 40 ++++++++++++++-
 xen/arch/x86/efi/efi-boot.h       | 65 +++++++++++++++++++++++-
 xen/arch/x86/efi/set-coff-flags.c | 83 +++++++++++++++++++++++++++++++
 xen/include/xen/multiboot2.h      |  6 +++
 8 files changed, 250 insertions(+), 2 deletions(-)
 create mode 100644 xen/arch/x86/efi/set-coff-flags.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/4 multiboot2: Advertise the load type

Advertise the type of the multiboot2 binary we're building. For now, it's always ELF.

---
 xen/arch/x86/boot/head.S     | 3 +++
 xen/include/xen/multiboot2.h | 6 ++++++
 2 files changed, 9 insertions(+)
xen/arch/x86/boot/head.S +3 -0
index d8ac0f0494db..015023915a72 100644
@@ -90,6 +90,9 @@ multiboot2_header:
                    0x200000, /* Load address alignment (2 MiB). */ \
                    MULTIBOOT2_LOAD_PREFERENCE_HIGH
 
+        mb2ht_init MB2_HT(LOAD_TYPE), MB2_HT(OPTIONAL), \
+                   MULTIBOOT2_LOAD_TYPE_ELF
+
         /* Console flags tag. */
         mb2ht_init MB2_HT(CONSOLE_FLAGS), MB2_HT(OPTIONAL), \
                    MULTIBOOT2_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED
xen/include/xen/multiboot2.h +6 -0
index f79204020535..c3a46ad66184 100644
@@ -60,6 +60,7 @@
 #define MULTIBOOT2_HEADER_TAG_ENTRY_ADDRESS_EFI32       8
 #define MULTIBOOT2_HEADER_TAG_ENTRY_ADDRESS_EFI64       9
 #define MULTIBOOT2_HEADER_TAG_RELOCATABLE               10
+#define MULTIBOOT2_HEADER_TAG_LOAD_TYPE                 11
 
 /* Header tag flags. */
 #define MULTIBOOT2_HEADER_TAG_REQUIRED                  0
@@ -113,6 +114,11 @@
 #define MULTIBOOT2_FRAMEBUFFER_TYPE_RGB                 1
 #define MULTIBOOT2_FRAMEBUFFER_TYPE_EGA_TEXT            2
 
+/* Load types. */
+#define MULTIBOOT2_LOAD_TYPE_ADDRESS                    0
+#define MULTIBOOT2_LOAD_TYPE_ELF                        1
+#define MULTIBOOT2_LOAD_TYPE_PE                         2
+
 #ifndef __ASSEMBLY__
 typedef struct {
     u32 total_size;
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/4 x86: Add support for building a multiboot2 PE binary

In reply to Ross Lagerwall

Add a new config option, CONFIG_MULTIBOOT_PE, that when set changes xen.gz to be a compressed PE binary instead of a compressed ELF binary. This requires use with a multiboot2 loader that supports the PE load type.

Using this option allows the binary to be signed and verified by Shim. This means the same xen.gz can then be used for BIOS boot, UEFI Boot and UEFI boot with Secure Boot verification (all with the convenience of GRUB2 as a bootloader).

---
 xen/Makefile             |  1 +
 xen/arch/x86/Kconfig     |  6 +++++
 xen/arch/x86/Makefile    | 48 ++++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/boot/head.S | 33 +++++++++++++++++++++++++++
 4 files changed, 88 insertions(+)
xen/Makefile +1 -0
index 21832d640225..c9461c9778cc 100644
@@ -581,6 +581,7 @@ _clean:
 		-o -name ".*.cmd" -o -name "lib.a" \) -exec rm -f {} \;
 	rm -f include/asm $(TARGET) $(TARGET).gz $(TARGET)-syms $(TARGET)-syms.map
 	rm -f $(TARGET).efi $(TARGET).efi.map $(TARGET).efi.elf $(TARGET).efi.stripped
+	rm -f $(TARGET).elf $(TARGET).map
 	rm -f asm-offsets.s arch/*/include/asm/asm-offsets.h
 	rm -f .banner .allconfig.tmp include/xen/compile.h
 	rm -rf $(objtree)/arch/*/include/generated
xen/arch/x86/Kconfig +6 -0
index 1acdffc51c22..2d8f6e687e58 100644
@@ -348,6 +348,12 @@ config REQUIRE_NX
 	  was unavailable. However, if enabled, Xen will no longer boot on
 	  any CPU which is lacking NX support.
 
+config MULTIBOOT_PE
+	bool "Build the multiboot binary as PE"
+	help
+	  Build the multiboot xen.gz binary as a PE binary rather than ELF.
+	  This allows it to be signed and verified when using Secure Boot
+	  with Shim and a bootloader.
 endmenu
 
 source "common/Kconfig"
xen/arch/x86/Makefile +48 -0

60 lines not on this page · raw message

xen/arch/x86/boot/head.S +33 -0
index 015023915a72..84dc8b76b61d 100644
@@ -44,6 +44,25 @@
 .Lmb2ht_init_end\@:
         .endm
 
+        .macro mb2ht_qargs arg:req, args:vararg
+        .quad \arg
+        .ifnb \args
+        mb2ht_args \args
+        .endif
+        .endm
+
+        .macro mb2ht_qinit type:req, req:req, args:vararg
+        .balign MULTIBOOT2_TAG_ALIGN, 0xc2 /* Avoid padding with long nops. */
+.Lmb2ht_init_start\@:
+        .short \type
+        .short \req
+        .long .Lmb2ht_init_end\@ - .Lmb2ht_init_start\@
+        .ifnb \args
+        mb2ht_qargs \args
+        .endif
+.Lmb2ht_init_end\@:
+        .endm
+
 ENTRY(start)
         jmp     __start
 
@@ -90,8 +109,14 @@ multiboot2_header:
                    0x200000, /* Load address alignment (2 MiB). */ \
                    MULTIBOOT2_LOAD_PREFERENCE_HIGH
 
+        /* Load type */
+#ifdef CONFIG_MULTIBOOT_PE
+        mb2ht_init MB2_HT(LOAD_TYPE), MB2_HT(OPTIONAL), \
+                   MULTIBOOT2_LOAD_TYPE_PE
+#else
         mb2ht_init MB2_HT(LOAD_TYPE), MB2_HT(OPTIONAL), \
                    MULTIBOOT2_LOAD_TYPE_ELF
+#endif
 
         /* Console flags tag. */
         mb2ht_init MB2_HT(CONSOLE_FLAGS), MB2_HT(OPTIONAL), \
@@ -107,8 +132,16 @@ multiboot2_header:
         mb2ht_init MB2_HT(EFI_BS), MB2_HT(OPTIONAL)
 
         /* EFI64 Multiboot2 entry point. */
+#ifdef CONFIG_MULTIBOOT_PE
+        mb2ht_qinit MB2_HT(ENTRY_ADDRESS_EFI64), MB2_HT(OPTIONAL), \
+                   __efi64_mb2_start
+
+        mb2ht_qinit MB2_HT(ENTRY_ADDRESS), MB2_HT(OPTIONAL), \
+                   start
+#else
         mb2ht_init MB2_HT(ENTRY_ADDRESS_EFI64), MB2_HT(OPTIONAL), \
                    sym_offs(__efi64_mb2_start)
+#endif
 
         /* Multiboot2 header end tag. */
         mb2ht_init MB2_HT(END), MB2_HT(REQUIRED)
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/4 x86: Hand-edit coff flags to remove RELOCS_STRIPPED flag

In reply to Ross Lagerwall

The generated PE binary is generated without relocations since it does not need them. However, the linker sets the IMAGE_FILE_RELOCS_STRIPPED flag which implies the binary is not relocatable and this causes some tools to refuse to handle the binary (e.g. Shim). As there is no option to remove this flag using the linker, post-process the binary to do it.

---
 .gitignore                        |  1 +
 xen/arch/x86/Makefile             |  2 +
 xen/arch/x86/efi/set-coff-flags.c | 83 +++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+)
 create mode 100644 xen/arch/x86/efi/set-coff-flags.c
.gitignore +1 -0
index d8b57e32f888..ab0b18352a0f 100644
@@ -256,6 +256,7 @@ xen/arch/x86/boot/*.lnk
 xen/arch/x86/efi.lds
 xen/arch/x86/efi/check.efi
 xen/arch/x86/efi/mkreloc
+xen/arch/x86/efi/set-coff-flags
 xen/arch/x86/include/asm/asm-macros.h
 xen/arch/*/xen.lds
 xen/arch/*/efi/boot.c
xen/arch/x86/Makefile +2 -0
index e26b1cb35036..73b16ddeb976 100644
@@ -86,6 +86,7 @@ extra-y += xen.lds
 
 hostprogs-y += boot/mkelf32
 hostprogs-y += efi/mkreloc
+hostprogs-y += efi/set-coff-flags
 
 # Allows usercopy.c to include itself
 $(obj)/usercopy.o: CFLAGS-y += -iquote .
@@ -157,6 +158,7 @@ endif
 		{ echo "No Multiboot1 header found" >&2; false; }
 	od -t x4 -N 32768 [email protected] | grep e85250d6 > /dev/null || \
 		{ echo "No Multiboot2 header found" >&2; false; }
+	$(obj)/efi/set-coff-flags [email protected]
 	mv [email protected] $(TARGET)
 	$(NM) -pa --format=sysv $@ \
 		| $(objtree)/tools/symbols --all-symbols --xensyms --sysv --sort \
xen/arch/x86/efi/set-coff-flags.c new file +83 -0

83 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/4 x86: Call Shim Verify in the multiboot2 path

In reply to Ross Lagerwall

Now that the multiboot2 binary can be verified by Shim, ensure that the dom0 kernel is verified when using the multiboot2 path. If the Shim protocol is not available and the SecureBoot variable is not set to 0 (or the state cannot be determined), abort the boot.

---
 xen/arch/x86/boot/head.S    |  4 ++-
 xen/arch/x86/efi/efi-boot.h | 65 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 67 insertions(+), 2 deletions(-)
xen/arch/x86/boot/head.S +3 -1
index 84dc8b76b61d..7b6e5d3882ad 100644
@@ -385,10 +385,12 @@ __efi64_mb2_start:
         /* Keep the stack aligned. Do not pop a single item off it. */
         mov     (%rsp),%rdi
 
+        mov     %rbx, %rcx
+
         /*
          * efi_multiboot2() is called according to System V AMD64 ABI:
          *   - IN:  %rdi - EFI ImageHandle, %rsi - EFI SystemTable,
-         *          %rdx - MB2 cmdline
+         *          %rdx - MB2 cmdline, %rcx - Multiboot information.
          */
         call    efi_multiboot2
 
xen/arch/x86/efi/efi-boot.h +64 -1

85 lines not on this page · raw message

Signature · 1 line
2.43.0