Skip to contentSkip to conversation

efi: implement generic compressed boot support

RFC PATCH Superseded Ard Biesheuvel 1 participant 5 messages May 2, 2022

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Ard Biesheuvel

author maintainer
#0 lore raw

cover efi: implement generic compressed boot support

Relatively modern architectures such as arm64 or RISC-V don't implement a self-decompressing kernel, and leave it up to the bootloader to decompress the compressed image before executing it. Not doing so makes sense for bare metal boot, as it essentially duplicates a lot of fiddly preparation work to create a 1:1 mapping and set up the C runtime, and to discover or infer where DRAM lives from device trees or other firmware tables.

For EFI boot, the situation is a bit different: the EFI entrypoint is called with a 1:1 mapping covering all of DRAM already active, and with a stack, a heap, a memory map and boot services to load and start images. This means it is rather trivial to implement a self-decompressing wrapper for EFI boot in a generic manner, and reuse it across architectures that implement EFI boot.

The downside is that two signatures are needed for UEFI secure boot: one for the decompressed image and one for the compressed images, as they are both PE/COFF EFI executables that are launched by the firmware. But that is actually the whole point, so it is simply the other side of the same coin.

Related discussion going on here:
https://lore.kernel.org/all/[email protected]/

Cc Arnd Bergmann <[email protected]>
Cc Huacai Chen <[email protected]>

Ard Biesheuvel (4):
efi: stub: add prototypes for load_image and start_image
efi: stub: split off printk() routines
efi: stub: implement generic EFI zboot
arm64: efi: enable generic EFI compressed boot

 arch/arm64/Makefile                            |   5 +
 arch/arm64/boot/Makefile                       |  17 ++-
 drivers/firmware/efi/Kconfig                   |   6 +
 drivers/firmware/efi/libstub/Makefile          |   4 +-
 drivers/firmware/efi/libstub/efi-stub-helper.c | 141 -----------------
 drivers/firmware/efi/libstub/efistub.h         |   8 +-
 drivers/firmware/efi/libstub/printk.c          | 158 ++++++++++++++++++++
 drivers/firmware/efi/libstub/zboot-header.S    |  88 +++++++++++
 drivers/firmware/efi/libstub/zboot.c           |  86 +++++++++++
 drivers/firmware/efi/libstub/zboot.lds         |  44 ++++++
 10 files changed, 412 insertions(+), 145 deletions(-)
 create mode 100644 drivers/firmware/efi/libstub/printk.c
 create mode 100644 drivers/firmware/efi/libstub/zboot-header.S
 create mode 100644 drivers/firmware/efi/libstub/zboot.c
 create mode 100644 drivers/firmware/efi/libstub/zboot.lds
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

RFC PATCH 1/4 efi: stub: add prototypes for load_image and start_image

Define the correct prototypes for the load_image and start_image boot service pointers so we can call them from the EFI zboot code.

---
 drivers/firmware/efi/libstub/efistub.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/firmware/efi/libstub/efistub.h +6 -2
index a0477afaa55f..af51ccc01ce2 100644
@@ -254,8 +254,12 @@ union efi_boot_services {
 							    efi_handle_t *);
 		efi_status_t (__efiapi *install_configuration_table)(efi_guid_t *,
 								     void *);
-		void *load_image;
-		void *start_image;
+		efi_status_t (__efiapi *load_image)(bool, efi_handle_t,
+						    efi_device_path_protocol_t *,
+						    void *, unsigned long,
+						    efi_handle_t *);
+		efi_status_t (__efiapi *start_image)(efi_handle_t, unsigned long *,
+						     efi_char16_t **);
 		efi_status_t __noreturn (__efiapi *exit)(efi_handle_t,
 							 efi_status_t,
 							 unsigned long,
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

RFC PATCH 2/4 efi: stub: split off printk() routines

In reply to Ard Biesheuvel

We will be using the libstub static library also to build the EFI zboot decompressor, which is a separate binary and does not carry all the dependencies of efi-stub-helper.c, which is where efi_printk() currently lives.

So split it off into printk.c so we can incorporate it separately.

---
 drivers/firmware/efi/libstub/Makefile          |   2 +-
 drivers/firmware/efi/libstub/efi-stub-helper.c | 141 -----------------
 drivers/firmware/efi/libstub/printk.c          | 158 ++++++++++++++++++++
 3 files changed, 159 insertions(+), 142 deletions(-)
drivers/firmware/efi/libstub/Makefile +1 -1
index d0537573501e..475224be08e1 100644
@@ -55,7 +55,7 @@ KCOV_INSTRUMENT			:= n
 lib-y				:= efi-stub-helper.o gop.o secureboot.o tpm.o \
 				   file.o mem.o random.o randomalloc.o pci.o \
 				   skip_spaces.o lib-cmdline.o lib-ctype.o \
-				   alignedmem.o relocate.o vsprintf.o
+				   alignedmem.o relocate.o vsprintf.o printk.o
 
 # include the stub's generic dependencies from lib/ when building for ARM/arm64
 efi-deps-y := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c fdt_empty_tree.c fdt_sw.c
drivers/firmware/efi/libstub/efi-stub-helper.c +0 -141

153 lines not on this page · raw message

drivers/firmware/efi/libstub/printk.c new file +158 -0

158 lines not on this page · raw message

Signature · 1 line
2.30.2
author maintainer
#3 lore raw

RFC PATCH 3/4 efi: stub: implement generic EFI zboot

In reply to Ard Biesheuvel

Implement a minimal EFI app that decompresses the real kernel image and launches it using the firmware's LoadImage and StartImage boot services. This removes the need for any arch-specific hacks.

---
 drivers/firmware/efi/Kconfig                |  6 ++
 drivers/firmware/efi/libstub/Makefile       |  2 +
 drivers/firmware/efi/libstub/zboot-header.S | 88 ++++++++++++++++++++
 drivers/firmware/efi/libstub/zboot.c        | 86 +++++++++++++++++++
 drivers/firmware/efi/libstub/zboot.lds      | 44 ++++++++++
 5 files changed, 226 insertions(+)
drivers/firmware/efi/Kconfig +6 -0
index 4720ba98cec3..5fc986f66505 100644
@@ -116,6 +116,12 @@ config EFI_RUNTIME_WRAPPERS
 config EFI_GENERIC_STUB
 	bool
 
+config EFI_ZBOOT
+	bool "Enable the generic EFI decompressor"
+	depends on EFI_GENERIC_STUB
+	depends on !ARM && !X86
+	select HAVE_KERNEL_GZIP
+
 config EFI_ARMSTUB_DTB_LOADER
 	bool "Enable the DTB loader"
 	depends on EFI_GENERIC_STUB && !RISCV
drivers/firmware/efi/libstub/Makefile +2 -0
index 475224be08e1..71bd877077b1 100644
@@ -111,8 +111,10 @@ STUBCOPY_RELOC-$(CONFIG_ARM)	:= R_ARM_ABS
 # a verification pass to see if any absolute relocations exist in any of the
 # object files.
 #
+lib-$(CONFIG_EFI_ZBOOT)		+= zboot.o
 extra-y				:= $(lib-y)
 lib-y				:= $(patsubst %.o,%.stub.o,$(lib-y))
+lib-$(CONFIG_EFI_ZBOOT)		+= zboot-header.o
 
 STUBCOPY_FLAGS-$(CONFIG_ARM64)	+= --prefix-alloc-sections=.init \
 				   --prefix-symbols=__efistub_
drivers/firmware/efi/libstub/zboot-header.S new file +88 -0

88 lines not on this page · raw message

drivers/firmware/efi/libstub/zboot.c new file +86 -0

86 lines not on this page · raw message

drivers/firmware/efi/libstub/zboot.lds new file +44 -0

44 lines not on this page · raw message

Signature · 1 line
2.30.2
author maintainer
#4 lore raw

RFC PATCH 4/4 arm64: efi: enable generic EFI compressed boot

In reply to Ard Biesheuvel

Wire up the generic EFI zboot support for arm64.

---
 arch/arm64/Makefile      |  5 +++++
 arch/arm64/boot/Makefile | 17 ++++++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)
arch/arm64/Makefile +5 -0
index 2f1de88651e6..c9580f78439f 100644
@@ -162,6 +162,11 @@ Image: vmlinux
 Image.%: Image
 	$(Q)$(MAKE) $(build)=$(boot) $(boot)/$@
 
+ifneq ($(CONFIG_EFI_ZBOOT),)
+zImage.efi: Image
+	$(Q)$(MAKE) $(build)=$(boot) $(boot)/$@
+endif
+
 install: install-image := Image
 zinstall: install-image := Image.gz
 install zinstall:
arch/arm64/boot/Makefile +16 -1
index ebe80faab883..81500267ab79 100644
@@ -16,7 +16,7 @@
 
 OBJCOPYFLAGS_Image :=-O binary -R .note -R .note.gnu.build-id -R .comment -S
 
-targets := Image Image.bz2 Image.gz Image.lz4 Image.lzma Image.lzo
+targets := Image Image.bz2 Image.gz Image.lz4 Image.lzma Image.lzo zImage.efi
 
 $(obj)/Image: vmlinux FORCE
 	$(call if_changed,objcopy)
@@ -35,3 +35,18 @@ $(obj)/Image.lzma: $(obj)/Image FORCE
 
 $(obj)/Image.lzo: $(obj)/Image FORCE
 	$(call if_changed,lzo)
+
+OBJCOPYFLAGS_Image.gz.o := -O elf64-littleaarch64 -I binary \
+			   --rename-section .data=.gzdata,load,alloc,readonly,contents
+$(obj)/Image.gz.o: $(obj)/Image.gz FORCE
+	$(call if_changed,objcopy)
+
+ZBOOT_DEPS := memcpy.o memset.o strnlen.o
+$(obj)/zImage.efi.elf: $(obj)/Image.gz.o $(addprefix $(objtree)/arch/arm64/lib/,$(ZBOOT_DEPS))
+	$(Q)$(LD) -T $(srctree)/drivers/firmware/efi/libstub/zboot.lds \
+		-o $@ $^ $(objtree)/drivers/firmware/efi/libstub/lib.a \
+		--defsym=pe_machine_type=0xaa64
+
+$(obj)/zImage.efi: OBJCOPYFLAGS := -O binary
+$(obj)/zImage.efi: $(obj)/zImage.efi.elf FORCE
+	$(call if_changed,objcopy)
Signature · 1 line
2.30.2