Skip to contentSkip to conversation

efi: Combine stub functionality with zboot decompressor

PATCH In review Ard Biesheuvel 2 participants 28 messages Oct 17, 2022 – Oct 18, 2022

Subsystems (5)
Arm (32-bit) RISC-V arm64 EFI +1 more subsystems
lore
Share & tools
lore

Ard Biesheuvel

author maintainer
#0 lore raw

cover efi: Combine stub functionality with zboot decompressor

The EFI zboot decompression code that has been merged into v6.1-rc1 is fully generic, and relies on the EFI stub inside the encapsulated image to implement the actual EFI boot sequence. While this works fine, it has some drawbacks that we might prefer to fix:
- executing the EFI stub of the inner image requires that it is signed if secure boot is enabled, which is a bit of a hassle, given that signing the image must occur during the build;
- decompressing a PE/COFF image and calling LoadImage() on it means that it gets copied again, and potentially yet another time if the placement does not meet per-arch requirements.

Given that the zboot decompressor and the EFI stub are built from the same set of objects in the libstub static library, we can make things a bit simpler, by incorporating everything the stub does into the zboot decompressor, and only handing off to the decompressed image after ExitBootServices(). This removes the need for signing the inner image, and allows us to decompress the image directly into the intended location in memory.

This involves some refactoring, to remove the dependency on symbols that are only defined when linking directly to vmlinux, such as string and memory compare routines, and section boundaries of the core kernel.

While at it, remove some functionality if it's not worth the effort making it work on both code paths, such as the EFI properties table, and the randomization of the UEFI runtime regions.

Since image signing no longer needs to occur during the build, let's also drop the support for invoking sbsign during the build on both the inner image and the decompressor. (I intend to send that patch as a fix for v6.1 so we don't add zombie Kconfig symbols to a LTS kernel)

Cc Matthew Garrett <[email protected]>
Cc Peter Jones <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Atish Patra <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Huacai Chen <[email protected]>
Cc Xi Ruoyao <[email protected]>
Cc Lennart Poettering <[email protected]>
Cc Jeremy Linton <[email protected]>
Cc Will Deacon <[email protected]>
Cc Catalin Marinas <[email protected]>

Ard Biesheuvel (21):
arm64: efi: Move dcache cleaning of loaded image out of
efi_enter_kernel()
arm64: efi: Avoid dcache_clean_poc() altogether in efi_enter_kernel()
arm64: efi: Move efi-entry.S into the libstub source directory
efi: libstub: Remove zboot signing from build options
efi: libstub: Drop randomization of runtime memory map
efi: libstub: Drop handling of EFI properties table
efi: libstub: Deduplicate ftrace command line argument filtering
efi: libstub: Use local strncmp() implementation unconditionally
efi: libstub: Clone memcmp() into the stub
efi: libstub: Enable efi_printk() in zboot decompressor
efi: loongarch: Drop exports of unused string routines
efi: libstub: Move screen_info handling to common code
efi: libstub: Provide local implementations of strrchr() and memchr()
efi: libstub: Factor out EFI stub entrypoint into separate file
efi: libstub: Add image code and data size to the zimage metadata
efi: libstub: Factor out min alignment and preferred kernel load
address
efi/riscv: libstub: Split off kernel image relocation for builtin stub efi/arm64: libstub: Split off kernel image relocation for builtin stub efi/loongarch: Don't jump to kernel entry via the old image efi/loongarch: libstub: Split off kernel image relocation for builtin
stub
efi: libstub: Merge zboot decompressor with the ordinary stub

 arch/arm/include/asm/efi.h                     |   3 -
 arch/arm/kernel/efi.c                          |  31 +-
 arch/arm64/include/asm/efi.h                   |  15 +-
 arch/arm64/kernel/Makefile                     |   9 +-
 arch/arm64/kernel/efi-entry.S                  |  69 -----
 arch/arm64/kernel/image-vars.h                 |   8 -
 arch/loongarch/include/asm/efi.h               |  14 +-
 arch/loongarch/kernel/efi.c                    |  24 +-
 arch/loongarch/kernel/image-vars.h             |   8 -
 arch/riscv/include/asm/efi.h                   |  13 +-
 arch/riscv/kernel/image-vars.h                 |   6 -
 drivers/firmware/efi/Kconfig                   |  22 --
 drivers/firmware/efi/efi-init.c                |  21 +-
 drivers/firmware/efi/efi.c                     |   5 +
 drivers/firmware/efi/libstub/Makefile          |  30 +-
 drivers/firmware/efi/libstub/Makefile.zboot    |  53 +---
 drivers/firmware/efi/libstub/arm32-stub.c      |  37 ---
 drivers/firmware/efi/libstub/arm64-entry.S     |  69 +++++
 drivers/firmware/efi/libstub/arm64-stub.c      |  49 +---
 drivers/firmware/efi/libstub/arm64.c           |  61 ++++
 drivers/firmware/efi/libstub/efi-stub-entry.c  |  65 +++++
 drivers/firmware/efi/libstub/efi-stub-helper.c | 143 ---------
 drivers/firmware/efi/libstub/efi-stub.c        | 140 +++------
 drivers/firmware/efi/libstub/efistub.h         |  15 +
 drivers/firmware/efi/libstub/file.c            |  18 --
 drivers/firmware/efi/libstub/intrinsics.c      |  18 ++
 drivers/firmware/efi/libstub/loongarch-stub.c  |  89 ++----
 drivers/firmware/efi/libstub/loongarch.c       |  80 +++++
 drivers/firmware/efi/libstub/printk.c          | 154 ++++++++++
 drivers/firmware/efi/libstub/riscv-stub.c      |  96 +-----
 drivers/firmware/efi/libstub/riscv.c           |  98 +++++++
 drivers/firmware/efi/libstub/screen_info.c     |  56 ++++
 drivers/firmware/efi/libstub/string.c          |  95 +++++-
 drivers/firmware/efi/libstub/zboot-header.S    |   2 +-
 drivers/firmware/efi/libstub/zboot.c           | 307 +++++---------------
 include/linux/efi.h                            |   2 +-
 36 files changed, 972 insertions(+), 953 deletions(-)
 delete mode 100644 arch/arm64/kernel/efi-entry.S
 create mode 100644 drivers/firmware/efi/libstub/arm64-entry.S
 create mode 100644 drivers/firmware/efi/libstub/arm64.c
 create mode 100644 drivers/firmware/efi/libstub/efi-stub-entry.c
 create mode 100644 drivers/firmware/efi/libstub/loongarch.c
 create mode 100644 drivers/firmware/efi/libstub/printk.c
 create mode 100644 drivers/firmware/efi/libstub/riscv.c
 create mode 100644 drivers/firmware/efi/libstub/screen_info.c
Signature · 1 line
2.35.1
author maintainer
#1 lore raw

PATCH 1/21 arm64: efi: Move dcache cleaning of loaded image out of efi_enter_kernel()

The efi_enter_kernel() routine will be shared between the existing EFI stub and the zboot decompressor, and the version of dcache_clean_to_poc() that the core kernel exports to the stub will not be available in the latter case.

So move the handling into the .c file which will remain part of the stub build that integrates directly with the kernel proper.

---
 arch/arm64/kernel/efi-entry.S             |  9 ---------
 arch/arm64/kernel/image-vars.h            |  1 -
 drivers/firmware/efi/libstub/arm64-stub.c | 10 +++++++++-
 3 files changed, 9 insertions(+), 11 deletions(-)
arch/arm64/kernel/efi-entry.S +0 -9
index 61a87fa1c305..1c1be004a271 100644
@@ -23,15 +23,6 @@ SYM_CODE_START(efi_enter_kernel)
 	add	x19, x0, x2		// relocated Image entrypoint
 	mov	x20, x1			// DTB address
 
-	/*
-	 * Clean the copied Image to the PoC, and ensure it is not shadowed by
-	 * stale icache entries from before relocation.
-	 */
-	ldr	w1, =kernel_size
-	add	x1, x0, x1
-	bl	dcache_clean_poc
-	ic	ialluis
-
 	/*
 	 * Clean the remainder of this routine to the PoC
 	 * so that we can safely disable the MMU and caches.
arch/arm64/kernel/image-vars.h +0 -1
index 8151412653de..74d20835cf91 100644
@@ -10,7 +10,6 @@
 #error This file should only be included in vmlinux.lds.S
 #endif
 
-PROVIDE(__efistub_kernel_size		= _edata - _text);
 PROVIDE(__efistub_primary_entry_offset	= primary_entry - _text);
 
 /*
drivers/firmware/efi/libstub/arm64-stub.c +9 -1
index 598c76c4bbaa..e767a5ac8c3d 100644
@@ -157,7 +157,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 			 */
 			*image_addr = (u64)_text;
 			*reserve_size = 0;
-			return EFI_SUCCESS;
+			goto clean_image_to_poc;
 		}
 
 		status = efi_allocate_pages_aligned(*reserve_size, reserve_addr,
@@ -174,5 +174,13 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	*image_addr = *reserve_addr;
 	memcpy((void *)*image_addr, _text, kernel_size);
 
+clean_image_to_poc:
+	/*
+	 * Clean the copied Image to the PoC, and ensure it is not shadowed by
+	 * stale icache entries from before relocation.
+	 */
+	dcache_clean_poc(*image_addr, *image_addr + kernel_size);
+	asm("ic ialluis");
+
 	return EFI_SUCCESS;
 }
Signature · 1 line
2.35.1
maintainer
#2 lore raw

Re: [PATCH 01/21] arm64: efi: Move dcache cleaning of loaded image out of efi_enter_kernel()

Quoted 20 lines · Ard Biesheuvel
arch/arm64/kernel/efi-entry.S +0 -9
index 61a87fa1c305..1c1be004a271 100644
@@ -23,15 +23,6 @@ SYM_CODE_START(efi_enter_kernel)
 	add	x19, x0, x2		// relocated Image entrypoint
 	mov	x20, x1			// DTB address
 
-	/*
-	 * Clean the copied Image to the PoC, and ensure it is not shadowed by
-	 * stale icache entries from before relocation.
-	 */
-	ldr	w1, =kernel_size
-	add	x1, x0, x1
-	bl	dcache_clean_poc
-	ic	ialluis
-
 	/*
 	 * Clean the remainder of this routine to the PoC
 	 * so that we can safely disable the MMU and caches.

[...]

Quoted 4 lines
drivers/firmware/efi/libstub/arm64-stub.c +0 -0
index 598c76c4bbaa..e767a5ac8c3d 100644

[...]

drivers/firmware/efi/libstub/arm64-stub.c efi_status_t handle_kernel_image(unsigned long *image_addr, · line 183

Quoted 11 lines
drivers/firmware/efi/libstub/arm64-stub.c +7 -0
@@ -174,5 +174,13 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	*image_addr = *reserve_addr;
 	memcpy((void *)*image_addr, _text, kernel_size);
 
+clean_image_to_poc:
+	/*
+	 * Clean the copied Image to the PoC, and ensure it is not shadowed by
+	 * stale icache entries from before relocation.
+	 */
+	dcache_clean_poc(*image_addr, *image_addr + kernel_size);
+	asm("ic ialluis");

Does this need some barriers, at least a DSB? The original code had DSB and ISB, though not immediately after the IC instruction.

Signature · 1 line
Catalin
author maintainer
#3 lore raw

Re: [PATCH 01/21] arm64: efi: Move dcache cleaning of loaded image out of efi_enter_kernel()

Quoted 32 lines · Catalin Marinas
Quoted 20 lines · Ard Biesheuvel
arch/arm64/kernel/efi-entry.S +0 -9
index 61a87fa1c305..1c1be004a271 100644
@@ -23,15 +23,6 @@ SYM_CODE_START(efi_enter_kernel)
      add     x19, x0, x2             // relocated Image entrypoint
      mov     x20, x1                 // DTB address
 
-     /*
-      * Clean the copied Image to the PoC, and ensure it is not shadowed by
-      * stale icache entries from before relocation.
-      */
-     ldr     w1, =kernel_size
-     add     x1, x0, x1
-     bl      dcache_clean_poc
-     ic      ialluis
-
      /*
       * Clean the remainder of this routine to the PoC
       * so that we can safely disable the MMU and caches.

[...]

Quoted 4 lines
drivers/firmware/efi/libstub/arm64-stub.c +0 -0
index 598c76c4bbaa..e767a5ac8c3d 100644

[...]

Quoted 4 lines
drivers/firmware/efi/libstub/arm64-stub.c +0 -0
@@ -174,5 +174,13 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
      *image_addr = *reserve_addr;
      memcpy((void *)*image_addr, _text, kernel_size);
 
Quoted 11 lines

drivers/firmware/efi/libstub/arm64-stub.c efi_status_t handle_kernel_image(unsigned long *image_addr, · line 183

Quoted 7 lines
drivers/firmware/efi/libstub/arm64-stub.c +7 -0
+clean_image_to_poc:
+     /*
+      * Clean the copied Image to the PoC, and ensure it is not shadowed by
+      * stale icache entries from before relocation.
+      */
+     dcache_clean_poc(*image_addr, *image_addr + kernel_size);
+     asm("ic ialluis");

Does this need some barriers, at least a DSB? The original code had DSB and ISB, though not immediately after the IC instruction.

We are still relying on the implicit DSB done by the subsequent call to dcache_clean_to_poc() call in efi_enter_kernel(), which executes much later than this code.

maintainer Acked-by
#4 lore raw

Re: [PATCH 01/21] arm64: efi: Move dcache cleaning of loaded image out of efi_enter_kernel()

Quoted 43 lines · Ard Biesheuvel
Quoted 31 lines · Catalin Marinas
Quoted 20 lines · Ard Biesheuvel
arch/arm64/kernel/efi-entry.S +0 -9
index 61a87fa1c305..1c1be004a271 100644
@@ -23,15 +23,6 @@ SYM_CODE_START(efi_enter_kernel)
      add     x19, x0, x2             // relocated Image entrypoint
      mov     x20, x1                 // DTB address
 
-     /*
-      * Clean the copied Image to the PoC, and ensure it is not shadowed by
-      * stale icache entries from before relocation.
-      */
-     ldr     w1, =kernel_size
-     add     x1, x0, x1
-     bl      dcache_clean_poc
-     ic      ialluis
-
      /*
       * Clean the remainder of this routine to the PoC
       * so that we can safely disable the MMU and caches.

[...]

Quoted 4 lines
drivers/firmware/efi/libstub/arm64-stub.c +0 -0
index 598c76c4bbaa..e767a5ac8c3d 100644

[...]

Quoted 4 lines
drivers/firmware/efi/libstub/arm64-stub.c +0 -0
@@ -174,5 +174,13 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
      *image_addr = *reserve_addr;
      memcpy((void *)*image_addr, _text, kernel_size);
 
Quoted 10 lines

drivers/firmware/efi/libstub/arm64-stub.c efi_status_t handle_kernel_image(unsigned long *image_addr, · line 183

Quoted 7 lines
drivers/firmware/efi/libstub/arm64-stub.c +7 -0
+clean_image_to_poc:
+     /*
+      * Clean the copied Image to the PoC, and ensure it is not shadowed by
+      * stale icache entries from before relocation.
+      */
+     dcache_clean_poc(*image_addr, *image_addr + kernel_size);
+     asm("ic ialluis");

Does this need some barriers, at least a DSB? The original code had DSB and ISB, though not immediately after the IC instruction.

Quoted 3 lines

We are still relying on the implicit DSB done by the subsequent call to dcache_clean_to_poc() call in efi_enter_kernel(), which executes much later than this code.

Ah, ok.

author maintainer
#5 lore raw

PATCH 2/21 arm64: efi: Avoid dcache_clean_poc() altogether in efi_enter_kernel()

In reply to Ard Biesheuvel

To allow efi_enter_kernel() to be shared with the EFI zboot decompressor build, drop another reference to dcache_clean_poc() and replace it with a single DC CVAC instruction. To ensure that it covers the remainder of efi_enter_kernel() as intended, reorganize the code a bit so it fits in a 32-byte cacheline, and align it to 32 bytes. (Even though the architecture defines 16 as the minimum D-cache line size, even the chosen value of 32 is highly unlikely to ever be encountered on real hardware, and this works with any line size >= 32)

---
 arch/arm64/kernel/efi-entry.S | 57 ++++++++++----------
 1 file changed, 29 insertions(+), 28 deletions(-)
arch/arm64/kernel/efi-entry.S +29 -28
index 1c1be004a271..8bce13356e29 100644
@@ -21,40 +21,41 @@ SYM_CODE_START(efi_enter_kernel)
 	 */
 	ldr	w2, =primary_entry_offset
 	add	x19, x0, x2		// relocated Image entrypoint
-	mov	x20, x1			// DTB address
+
+	mov	x0, x1			// DTB address
+	mov	x1, xzr
+	mov	x2, xzr
+	mov	x3, xzr
 
 	/*
 	 * Clean the remainder of this routine to the PoC
 	 * so that we can safely disable the MMU and caches.
 	 */
-	adr	x0, 0f
-	adr	x1, 3f
-	bl	dcache_clean_poc
-0:
+	adr	x4, 1f
+	dc	civac, x4
+	dsb	sy
+
 	/* Turn off Dcache and MMU */
-	mrs	x0, CurrentEL
-	cmp	x0, #CurrentEL_EL2
-	b.ne	1f
-	mrs	x0, sctlr_el2
-	bic	x0, x0, #1 << 0	// clear SCTLR.M
-	bic	x0, x0, #1 << 2	// clear SCTLR.C
-	pre_disable_mmu_workaround
-	msr	sctlr_el2, x0
-	isb
+	mrs	x4, CurrentEL
+	cmp	x4, #CurrentEL_EL2
+	mrs	x4, sctlr_el1
+	b.ne	0f
+	mrs	x4, sctlr_el2
+0:	bic	x4, x4, #SCTLR_ELx_M
+	bic	x4, x4, #SCTLR_ELx_C
+	b.eq	1f
 	b	2f
-1:
-	mrs	x0, sctlr_el1
-	bic	x0, x0, #1 << 0	// clear SCTLR.M
-	bic	x0, x0, #1 << 2	// clear SCTLR.C
-	pre_disable_mmu_workaround
-	msr	sctlr_el1, x0
+
+	.balign	32
+1:	pre_disable_mmu_workaround
+	msr	sctlr_el2, x4
 	isb
-2:
-	/* Jump to kernel entry point */
-	mov	x0, x20
-	mov	x1, xzr
-	mov	x2, xzr
-	mov	x3, xzr
-	br	x19
-3:
+	br	x19		// jump to kernel entrypoint
+
+2:	pre_disable_mmu_workaround
+	msr	sctlr_el1, x4
+	isb
+	br	x19		// jump to kernel entrypoint
+
+	.org	1b + 32
 SYM_CODE_END(efi_enter_kernel)
Signature · 1 line
2.35.1
maintainer Acked-by on v1 2/21
#6 lore raw

Re: [PATCH 02/21] arm64: efi: Avoid dcache_clean_poc() altogether in efi_enter_kernel()

Quoted 8 lines · Ard Biesheuvel

To allow efi_enter_kernel() to be shared with the EFI zboot decompressor build, drop another reference to dcache_clean_poc() and replace it with a single DC CVAC instruction. To ensure that it covers the remainder of efi_enter_kernel() as intended, reorganize the code a bit so it fits in a 32-byte cacheline, and align it to 32 bytes. (Even though the architecture defines 16 as the minimum D-cache line size, even the chosen value of 32 is highly unlikely to ever be encountered on real hardware, and this works with any line size >= 32)

This should do.

arch/arm64/kernel/efi-entry.S SYM_CODE_START(efi_enter_kernel) · line 35

Quoted 3 lines

-0:

arch/arm64/kernel/efi-entry.S +2 -0
+	adr	x4, 1f
+	dc	civac, x4

s/civac/cvac/ ?

Otherwise,

author maintainer
#7 lore raw

Re: [PATCH 02/21] arm64: efi: Avoid dcache_clean_poc() altogether in efi_enter_kernel()

Quoted 11 lines · Catalin Marinas
Quoted 8 lines · Ard Biesheuvel

To allow efi_enter_kernel() to be shared with the EFI zboot decompressor build, drop another reference to dcache_clean_poc() and replace it with a single DC CVAC instruction. To ensure that it covers the remainder of efi_enter_kernel() as intended, reorganize the code a bit so it fits in a 32-byte cacheline, and align it to 32 bytes. (Even though the architecture defines 16 as the minimum D-cache line size, even the chosen value of 32 is highly unlikely to ever be encountered on real hardware, and this works with any line size >= 32)

Quoted 8 lines

This should do.

arch/arm64/kernel/efi-entry.S SYM_CODE_START(efi_enter_kernel) · line 35

Quoted 3 lines

-0:

arch/arm64/kernel/efi-entry.S +2 -0
+     adr     x4, 1f
+     dc      civac, x4

s/civac/cvac/ ?

Ah yes, I changed that last minute because of
ARM64_WORKAROUND_CLEAN_CACHE - I'll add a note.

Quoted 3 lines

Otherwise,

Thanks

author maintainer
#8 lore raw

PATCH 3/21 arm64: efi: Move efi-entry.S into the libstub source directory

In reply to Ard Biesheuvel

We will be sharing efi-entry.S with the zboot decompressor build, which does not link against vmlinux directly. So move it into the libstub source directory so we can include in the libstub static library.

---
 arch/arm64/kernel/Makefile                                                  | 9 +--------
 drivers/firmware/efi/libstub/Makefile                                       | 4 ++--
 arch/arm64/kernel/efi-entry.S => drivers/firmware/efi/libstub/arm64-entry.S | 4 ----
 3 files changed, 3 insertions(+), 14 deletions(-)
arch/arm64/kernel/Makefile +1 -8
index 2f361a883d8c..2a3b0e4950f2 100644
@@ -36,12 +36,6 @@ obj-y			:= debug-monitors.o entry.o irq.o fpsimd.o		\
 			   syscall.o proton-pack.o idreg-override.o idle.o	\
 			   patching.o
 
-targets			+= efi-entry.o
-
-OBJCOPYFLAGS := --prefix-symbols=__efistub_
-$(obj)/%.stub.o: $(obj)/%.o FORCE
-	$(call if_changed,objcopy)
-
 obj-$(CONFIG_COMPAT)			+= sys32.o signal32.o			\
 					   sys_compat.o
 obj-$(CONFIG_COMPAT)			+= sigreturn32.o
@@ -57,8 +51,7 @@ obj-$(CONFIG_CPU_PM)			+= sleep.o suspend.o
 obj-$(CONFIG_CPU_IDLE)			+= cpuidle.o
 obj-$(CONFIG_JUMP_LABEL)		+= jump_label.o
 obj-$(CONFIG_KGDB)			+= kgdb.o
-obj-$(CONFIG_EFI)			+= efi.o efi-entry.stub.o		\
-					   efi-rt-wrapper.o
+obj-$(CONFIG_EFI)			+= efi.o efi-rt-wrapper.o
 obj-$(CONFIG_PCI)			+= pci.o
 obj-$(CONFIG_ARMV8_DEPRECATED)		+= armv8_deprecated.o
 obj-$(CONFIG_ACPI)			+= acpi.o
drivers/firmware/efi/libstub/Makefile +2 -2
index b1601aad7e1a..efc8cefab889 100644
@@ -82,7 +82,7 @@ $(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
 lib-$(CONFIG_EFI_GENERIC_STUB)	+= efi-stub.o string.o intrinsics.o systable.o
 
 lib-$(CONFIG_ARM)		+= arm32-stub.o
-lib-$(CONFIG_ARM64)		+= arm64-stub.o
+lib-$(CONFIG_ARM64)		+= arm64-stub.o arm64-entry.o
 lib-$(CONFIG_X86)		+= x86-stub.o
 lib-$(CONFIG_RISCV)		+= riscv-stub.o
 lib-$(CONFIG_LOONGARCH)		+= loongarch-stub.o
@@ -136,7 +136,7 @@ STUBCOPY_RELOC-$(CONFIG_ARM)	:= R_ARM_ABS
 #
 STUBCOPY_FLAGS-$(CONFIG_ARM64)	+= --prefix-alloc-sections=.init \
 				   --prefix-symbols=__efistub_
-STUBCOPY_RELOC-$(CONFIG_ARM64)	:= R_AARCH64_ABS
+STUBCOPY_RELOC-$(CONFIG_ARM64)	:= R_AARCH64_ABS64
 
 # For RISC-V, we don't need anything special other than arm64. Keep all the
 # symbols in .init section and make sure that no absolute symbols references
drivers/firmware/efi/libstub/arm64-entry.S renamed from arch/arm64/kernel/efi-entry.S +0 -4
similarity index 97%
rename from arch/arm64/kernel/efi-entry.S
rename to drivers/firmware/efi/libstub/arm64-entry.S
index 8bce13356e29..87082b222a87 100644
@@ -6,12 +6,8 @@
  * Author: Mark Salter <[email protected]>
  */
 #include <linux/linkage.h>
-#include <linux/init.h>
-
 #include <asm/assembler.h>
 
-	__INIT
-
 SYM_CODE_START(efi_enter_kernel)
 	/*
 	 * efi_pe_entry() will have copied the kernel image if necessary and we
Signature · 1 line
2.35.1
maintainer Acked-by on v1 3/21
#9 lore raw

Re: [PATCH 03/21] arm64: efi: Move efi-entry.S into the libstub source directory

Quoted 5 lines · Ard Biesheuvel

We will be sharing efi-entry.S with the zboot decompressor build, which does not link against vmlinux directly. So move it into the libstub source directory so we can include in the libstub static library.

author maintainer
#10 lore raw

PATCH 4/21 efi: libstub: Remove zboot signing from build options

In reply to Ard Biesheuvel

The zboot decompressor series introduced a feature to sign the PE/COFF kernel image for secure boot as part of the kernel build. This was necessary because there are actually two images that need to be signed:
the kernel with the EFI stub attached, and the decompressor application.

This is a bit of a burden, because it means that the images must be signed on the the same system that performs the build, and this is not realistic for distros.

During the next cycle, we will introduce changes to the zboot code so that the inner image no longer needs to be signed. This means that the outer PE/COFF image can be handled as usual, and be signed later in the release process.

Let's remove the associated Kconfig options now so that they don't end up in a LTS release while already being deprecated.

---
 drivers/firmware/efi/Kconfig                | 22 ---------------
 drivers/firmware/efi/libstub/Makefile.zboot | 29 +++-----------------
 2 files changed, 4 insertions(+), 47 deletions(-)
drivers/firmware/efi/Kconfig +0 -22
index fceeea74522e..0d5201e49841 100644
@@ -102,28 +102,6 @@ config EFI_ZBOOT
 	  is supported by the encapsulated image. (The compression algorithm
 	  used is described in the zboot image header)
 
-config EFI_ZBOOT_SIGNED
-	def_bool y
-	depends on EFI_ZBOOT_SIGNING_CERT != ""
-	depends on EFI_ZBOOT_SIGNING_KEY != ""
-
-config EFI_ZBOOT_SIGNING
-	bool "Sign the EFI decompressor for UEFI secure boot"
-	depends on EFI_ZBOOT
-	help
-	  Use the 'sbsign' command line tool (which must exist on the host
-	  path) to sign both the EFI decompressor PE/COFF image, as well as the
-	  encapsulated PE/COFF image, which is subsequently compressed and
-	  wrapped by the former image.
-
-config EFI_ZBOOT_SIGNING_CERT
-	string "Certificate to use for signing the compressed EFI boot image"
-	depends on EFI_ZBOOT_SIGNING
-
-config EFI_ZBOOT_SIGNING_KEY
-	string "Private key to use for signing the compressed EFI boot image"
-	depends on EFI_ZBOOT_SIGNING
-
 config EFI_ARMSTUB_DTB_LOADER
 	bool "Enable the DTB loader"
 	depends on EFI_GENERIC_STUB && !RISCV && !LOONGARCH
drivers/firmware/efi/libstub/Makefile.zboot +4 -25
index 35f234ad8738..3340b385a05b 100644
@@ -20,22 +20,11 @@ zboot-size-len-y			:= 4
 zboot-method-$(CONFIG_KERNEL_GZIP)	:= gzip
 zboot-size-len-$(CONFIG_KERNEL_GZIP)	:= 0
 
-quiet_cmd_sbsign = SBSIGN  $@
-      cmd_sbsign = sbsign --out $@ $< \
-		   --key $(CONFIG_EFI_ZBOOT_SIGNING_KEY) \
-		   --cert $(CONFIG_EFI_ZBOOT_SIGNING_CERT)
-
-$(obj)/$(EFI_ZBOOT_PAYLOAD).signed: $(obj)/$(EFI_ZBOOT_PAYLOAD) FORCE
-	$(call if_changed,sbsign)
-
-ZBOOT_PAYLOAD-y				 := $(EFI_ZBOOT_PAYLOAD)
-ZBOOT_PAYLOAD-$(CONFIG_EFI_ZBOOT_SIGNED) := $(EFI_ZBOOT_PAYLOAD).signed
-
-$(obj)/vmlinuz: $(obj)/$(ZBOOT_PAYLOAD-y) FORCE
+$(obj)/vmlinuz: $(obj)/$(EFI_ZBOOT_PAYLOAD) FORCE
 	$(call if_changed,$(zboot-method-y))
 
 OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) \
-			 --rename-section .data=.gzdata,load,alloc,readonly,contents
+			  --rename-section .data=.gzdata,load,alloc,readonly,contents
 $(obj)/vmlinuz.o: $(obj)/vmlinuz FORCE
 	$(call if_changed,objcopy)
 
@@ -53,18 +42,8 @@ LDFLAGS_vmlinuz.efi.elf := -T $(srctree)/drivers/firmware/efi/libstub/zboot.lds
 $(obj)/vmlinuz.efi.elf: $(obj)/vmlinuz.o $(ZBOOT_DEPS) FORCE
 	$(call if_changed,ld)
 
-ZBOOT_EFI-y				:= vmlinuz.efi
-ZBOOT_EFI-$(CONFIG_EFI_ZBOOT_SIGNED)	:= vmlinuz.efi.unsigned
-
-OBJCOPYFLAGS_$(ZBOOT_EFI-y) := -O binary
-$(obj)/$(ZBOOT_EFI-y): $(obj)/vmlinuz.efi.elf FORCE
+OBJCOPYFLAGS_vmlinuz.efi := -O binary
+$(obj)/vmlinuz.efi: $(obj)/vmlinuz.efi.elf FORCE
 	$(call if_changed,objcopy)
 
 targets += zboot-header.o vmlinuz vmlinuz.o vmlinuz.efi.elf vmlinuz.efi
-
-ifneq ($(CONFIG_EFI_ZBOOT_SIGNED),)
-$(obj)/vmlinuz.efi: $(obj)/vmlinuz.efi.unsigned FORCE
-	$(call if_changed,sbsign)
-endif
-
-targets += $(EFI_ZBOOT_PAYLOAD).signed vmlinuz.efi.unsigned
Signature · 1 line
2.35.1
author maintainer
#11 lore raw

PATCH 5/21 efi: libstub: Drop randomization of runtime memory map

In reply to Ard Biesheuvel

Randomizing the UEFI runtime memory map requires the use of the SetVirtualAddressMap() EFI boot service, which we prefer to avoid. So let's drop randomization, which was already problematic in combination with hibernation, which means that distro kernels never enabled it in the first place.

---
 drivers/firmware/efi/libstub/efi-stub.c | 29 --------------------
 1 file changed, 29 deletions(-)
drivers/firmware/efi/libstub/efi-stub.c +0 -29
index cf474f0dd261..0ebfa2d9628a 100644
@@ -35,15 +35,6 @@
  * as well to minimize the code churn.
  */
 #define EFI_RT_VIRTUAL_BASE	SZ_512M
-#define EFI_RT_VIRTUAL_SIZE	SZ_512M
-
-#ifdef CONFIG_ARM64
-# define EFI_RT_VIRTUAL_LIMIT	DEFAULT_MAP_WINDOW_64
-#elif defined(CONFIG_RISCV) || defined(CONFIG_LOONGARCH)
-# define EFI_RT_VIRTUAL_LIMIT	TASK_SIZE_MIN
-#else /* Only if TASK_SIZE is a constant */
-# define EFI_RT_VIRTUAL_LIMIT	TASK_SIZE
-#endif
 
 /*
  * Some architectures map the EFI regions into the kernel's linear map using a
@@ -230,26 +221,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 	efi_novamap |= !(get_supported_rt_services() &
 			 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP);
 
-	/* hibernation expects the runtime regions to stay in the same place */
-	if (!IS_ENABLED(CONFIG_HIBERNATION) && !efi_nokaslr && !flat_va_mapping) {
-		/*
-		 * Randomize the base of the UEFI runtime services region.
-		 * Preserve the 2 MB alignment of the region by taking a
-		 * shift of 21 bit positions into account when scaling
-		 * the headroom value using a 32-bit random value.
-		 */
-		static const u64 headroom = EFI_RT_VIRTUAL_LIMIT -
-					    EFI_RT_VIRTUAL_BASE -
-					    EFI_RT_VIRTUAL_SIZE;
-		u32 rnd;
-
-		status = efi_get_random_bytes(sizeof(rnd), (u8 *)&rnd);
-		if (status == EFI_SUCCESS) {
-			virtmap_base = EFI_RT_VIRTUAL_BASE +
-				       (((headroom >> 21) * rnd) >> (32 - 21));
-		}
-	}
-
 	install_memreserve_table();
 
 	status = efi_boot_kernel(handle, image, image_addr, cmdline_ptr);
Signature · 1 line
2.35.1
author maintainer
#12 lore raw

PATCH 6/21 efi: libstub: Drop handling of EFI properties table

In reply to Ard Biesheuvel

The EFI properties table was a short lived experiment that never saw the light of day on non-x86 (if at all) so let's drop the handling of it.

---
 drivers/firmware/efi/libstub/efi-stub.c | 13 -------------
 1 file changed, 13 deletions(-)
drivers/firmware/efi/libstub/efi-stub.c +0 -13
index 0ebfa2d9628a..b55d1009d4c8 100644
@@ -126,7 +126,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 	unsigned long reserve_addr = 0;
 	unsigned long reserve_size = 0;
 	struct screen_info *si;
-	efi_properties_table_t *prop_tbl;
 
 	efi_system_table = sys_table_arg;
 
@@ -205,18 +204,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 
 	efi_random_get_seed();
 
-	/*
-	 * If the NX PE data feature is enabled in the properties table, we
-	 * should take care not to create a virtual mapping that changes the
-	 * relative placement of runtime services code and data regions, as
-	 * they may belong to the same PE/COFF executable image in memory.
-	 * The easiest way to achieve that is to simply use a 1:1 mapping.
-	 */
-	prop_tbl = get_efi_config_table(EFI_PROPERTIES_TABLE_GUID);
-	flat_va_mapping |= prop_tbl &&
-			   (prop_tbl->memory_protection_attribute &
-			   EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA);
-
 	/* force efi_novamap if SetVirtualAddressMap() is unsupported */
 	efi_novamap |= !(get_supported_rt_services() &
 			 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP);
Signature · 1 line
2.35.1
author maintainer
#13 lore raw

PATCH 7/21 efi: libstub: Deduplicate ftrace command line argument filtering

In reply to Ard Biesheuvel

No need for the same pattern to be used four times for each architecture individually if we can just apply it once later.

---
 drivers/firmware/efi/libstub/Makefile | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/firmware/efi/libstub/Makefile +6 -9
index efc8cefab889..5246d69d9abf 100644
@@ -18,20 +18,17 @@ cflags-$(CONFIG_X86)		+= -m$(BITS) -D__KERNEL__ \
 
 # arm64 uses the full KBUILD_CFLAGS so it's necessary to explicitly
 # disable the stackleak plugin
-cflags-$(CONFIG_ARM64)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
-				   -fpie $(DISABLE_STACKLEAK_PLUGIN) \
+cflags-$(CONFIG_ARM64)		:= -fpie $(DISABLE_STACKLEAK_PLUGIN) \
 				   $(call cc-option,-mbranch-protection=none)
-cflags-$(CONFIG_ARM)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
-				   -fno-builtin -fpic \
+cflags-$(CONFIG_ARM)		:= -fno-builtin -fpic \
 				   $(call cc-option,-mno-single-pic-base)
-cflags-$(CONFIG_RISCV)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
-				   -fpic
-cflags-$(CONFIG_LOONGARCH)	:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
-				   -fpie
+cflags-$(CONFIG_RISCV)		:= -fpic
+cflags-$(CONFIG_LOONGARCH)	:= -fpie
 
 cflags-$(CONFIG_EFI_PARAMS_FROM_FDT)	+= -I$(srctree)/scripts/dtc/libfdt
 
-KBUILD_CFLAGS			:= $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \
+KBUILD_CFLAGS			:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
+				   $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \
 				   -include $(srctree)/include/linux/hidden.h \
 				   -D__NO_FORTIFY \
 				   -ffreestanding \
Signature · 1 line
2.35.1
author maintainer
#14 lore raw

PATCH 8/21 efi: libstub: Use local strncmp() implementation unconditionally

In reply to Ard Biesheuvel

In preparation for moving the EFI stub functionality into the zboot decompressor, switch to the stub's implementation of strncmp() unconditionally.

---
 arch/arm64/kernel/image-vars.h        | 1 -
 arch/riscv/kernel/image-vars.h        | 1 -
 drivers/firmware/efi/libstub/string.c | 2 --
 3 files changed, 4 deletions(-)
arch/arm64/kernel/image-vars.h +0 -1
index 74d20835cf91..5a12ca2d7158 100644
@@ -26,7 +26,6 @@ PROVIDE(__efistub_memchr		= __pi_memchr);
 PROVIDE(__efistub_strlen		= __pi_strlen);
 PROVIDE(__efistub_strnlen		= __pi_strnlen);
 PROVIDE(__efistub_strcmp		= __pi_strcmp);
-PROVIDE(__efistub_strncmp		= __pi_strncmp);
 PROVIDE(__efistub_strrchr		= __pi_strrchr);
 PROVIDE(__efistub_dcache_clean_poc	= __pi_dcache_clean_poc);
 
arch/riscv/kernel/image-vars.h +0 -1
index d6e5f739905e..b46322cb5864 100644
@@ -28,7 +28,6 @@ __efistub_memchr		= memchr;
 __efistub_strlen		= strlen;
 __efistub_strnlen		= strnlen;
 __efistub_strcmp		= strcmp;
-__efistub_strncmp		= strncmp;
 __efistub_strrchr		= strrchr;
 
 __efistub__start		= _start;
drivers/firmware/efi/libstub/string.c +0 -2
index 5d13e43869ee..9f5810d86646 100644
@@ -35,7 +35,6 @@ char *strstr(const char *s1, const char *s2)
 }
 #endif
 
-#ifndef __HAVE_ARCH_STRNCMP
 /**
  * strncmp - Compare two length-limited strings
  * @cs: One string
@@ -57,7 +56,6 @@ int strncmp(const char *cs, const char *ct, size_t count)
 	}
 	return 0;
 }
-#endif
 
 /* Works only for digits and letters, but small and fast */
 #define TOLOWER(x) ((x) | 0x20)
Signature · 1 line
2.35.1
author maintainer
#15 lore raw

PATCH 9/21 efi: libstub: Clone memcmp() into the stub

In reply to Ard Biesheuvel

We will no longer be able to call into the kernel image once we merge the decompressor with the EFI stub, so we need our own implementation of memcmp(). Let's add the one from lib/string.c and simplify it.

---
 arch/arm64/kernel/image-vars.h            |  1 -
 arch/loongarch/kernel/image-vars.h        |  1 -
 arch/riscv/kernel/image-vars.h            |  1 -
 drivers/firmware/efi/libstub/intrinsics.c | 18 ++++++++++++++++++
 drivers/firmware/efi/libstub/zboot.c      | 11 +----------
 5 files changed, 19 insertions(+), 13 deletions(-)
arch/arm64/kernel/image-vars.h +0 -1
index 5a12ca2d7158..f7c8cdf0c302 100644
@@ -21,7 +21,6 @@ PROVIDE(__efistub_primary_entry_offset	= primary_entry - _text);
  * linked at. The routines below are all implemented in assembler in a
  * position independent manner
  */
-PROVIDE(__efistub_memcmp		= __pi_memcmp);
 PROVIDE(__efistub_memchr		= __pi_memchr);
 PROVIDE(__efistub_strlen		= __pi_strlen);
 PROVIDE(__efistub_strnlen		= __pi_strnlen);
arch/loongarch/kernel/image-vars.h +0 -1
index 88f5d81702df..5b6c7f079942 100644
@@ -7,7 +7,6 @@
 
 #ifdef CONFIG_EFI_STUB
 
-__efistub_memcmp		= memcmp;
 __efistub_memchr		= memchr;
 __efistub_strcat		= strcat;
 __efistub_strcmp		= strcmp;
arch/riscv/kernel/image-vars.h +0 -1
index b46322cb5864..9229cfe0754d 100644
@@ -23,7 +23,6 @@
  * linked at. The routines below are all implemented in assembler in a
  * position independent manner
  */
-__efistub_memcmp		= memcmp;
 __efistub_memchr		= memchr;
 __efistub_strlen		= strlen;
 __efistub_strnlen		= strnlen;
drivers/firmware/efi/libstub/intrinsics.c +18 -0
index a04ab39292b6..965e734f6f98 100644
@@ -28,3 +28,21 @@ void *memset(void *dst, int c, size_t len)
 	efi_bs_call(set_mem, dst, len, c & U8_MAX);
 	return dst;
 }
+
+/**
+ * memcmp - Compare two areas of memory
+ * @cs: One area of memory
+ * @ct: Another area of memory
+ * @count: The size of the area.
+ */
+#undef memcmp
+int memcmp(const void *cs, const void *ct, size_t count)
+{
+	const unsigned char *su1, *su2;
+	int res = 0;
+
+	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
+		if ((res = *su1 - *su2) != 0)
+			break;
+	return res;
+}
drivers/firmware/efi/libstub/zboot.c +1 -10
index ea72c8f27da6..38173ec29cd5 100644
@@ -47,15 +47,6 @@ static void error(char *x)
 	log(L"error() called from decompressor library\n");
 }
 
-// Local version to avoid pulling in memcmp()
-static bool guids_eq(const efi_guid_t *a, const efi_guid_t *b)
-{
-	const u32 *l = (u32 *)a;
-	const u32 *r = (u32 *)b;
-
-	return l[0] == r[0] && l[1] == r[1] && l[2] == r[2] && l[3] == r[3];
-}
-
 static efi_status_t __efiapi
 load_file(efi_load_file_protocol_t *this, efi_device_path_protocol_t *rem,
 	  bool boot_policy, unsigned long *bufsize, void *buffer)
@@ -76,7 +67,7 @@ load_file(efi_load_file_protocol_t *this, efi_device_path_protocol_t *rem,
 	if (rem->type == EFI_DEV_MEDIA &&
 	    rem->sub_type == EFI_DEV_MEDIA_VENDOR) {
 		vendor_dp = container_of(rem, struct efi_vendor_dev_path, header);
-		if (!guids_eq(&vendor_dp->vendorguid, &LINUX_EFI_ZBOOT_MEDIA_GUID))
+		if (efi_guidcmp(vendor_dp->vendorguid, LINUX_EFI_ZBOOT_MEDIA_GUID))
 			return EFI_NOT_FOUND;
 
 		decompress = true;
Signature · 1 line
2.35.1
author maintainer
#16 lore raw

PATCH 10/21 efi: libstub: Enable efi_printk() in zboot decompressor

In reply to Ard Biesheuvel

Split the efi_printk() routine into its own source file, and provide local implementations of strlen() and strnlen() so that the standalone zboot app can efi_err and efi_info etc.

---
 arch/arm64/kernel/image-vars.h                 |   2 -
 arch/loongarch/kernel/image-vars.h             |   2 -
 arch/riscv/kernel/image-vars.h                 |   2 -
 drivers/firmware/efi/libstub/Makefile          |   5 +-
 drivers/firmware/efi/libstub/efi-stub-helper.c | 143 ------------------
 drivers/firmware/efi/libstub/printk.c          | 154 ++++++++++++++++++++
 drivers/firmware/efi/libstub/string.c          |  33 ++++-
 drivers/firmware/efi/libstub/zboot.c           |  30 ++--
 8 files changed, 198 insertions(+), 173 deletions(-)
arch/arm64/kernel/image-vars.h +0 -2
index f7c8cdf0c302..ace584e0b065 100644
@@ -22,8 +22,6 @@ PROVIDE(__efistub_primary_entry_offset	= primary_entry - _text);
  * position independent manner
  */
 PROVIDE(__efistub_memchr		= __pi_memchr);
-PROVIDE(__efistub_strlen		= __pi_strlen);
-PROVIDE(__efistub_strnlen		= __pi_strnlen);
 PROVIDE(__efistub_strcmp		= __pi_strcmp);
 PROVIDE(__efistub_strrchr		= __pi_strrchr);
 PROVIDE(__efistub_dcache_clean_poc	= __pi_dcache_clean_poc);
arch/loongarch/kernel/image-vars.h +0 -2
index 5b6c7f079942..28bc632ec296 100644
@@ -10,10 +10,8 @@
 __efistub_memchr		= memchr;
 __efistub_strcat		= strcat;
 __efistub_strcmp		= strcmp;
-__efistub_strlen		= strlen;
 __efistub_strncat		= strncat;
 __efistub_strnstr		= strnstr;
-__efistub_strnlen		= strnlen;
 __efistub_strrchr		= strrchr;
 __efistub_kernel_entry		= kernel_entry;
 __efistub_kernel_asize		= kernel_asize;
arch/riscv/kernel/image-vars.h +0 -2
index 9229cfe0754d..4913abd6dd10 100644
@@ -24,8 +24,6 @@
  * position independent manner
  */
 __efistub_memchr		= memchr;
-__efistub_strlen		= strlen;
-__efistub_strnlen		= strnlen;
 __efistub_strcmp		= strcmp;
 __efistub_strrchr		= strrchr;
 
drivers/firmware/efi/libstub/Makefile +3 -2
index 5246d69d9abf..cb600c7a14de 100644
@@ -20,7 +20,8 @@ cflags-$(CONFIG_X86)		+= -m$(BITS) -D__KERNEL__ \
 # disable the stackleak plugin
 cflags-$(CONFIG_ARM64)		:= -fpie $(DISABLE_STACKLEAK_PLUGIN) \
 				   $(call cc-option,-mbranch-protection=none)
-cflags-$(CONFIG_ARM)		:= -fno-builtin -fpic \
+cflags-$(CONFIG_ARM)		:= -DEFI_HAVE_STRLEN -DEFI_HAVE_STRNLEN \
+				   -fno-builtin -fpic \
 				   $(call cc-option,-mno-single-pic-base)
 cflags-$(CONFIG_RISCV)		:= -fpic
 cflags-$(CONFIG_LOONGARCH)	:= -fpie
@@ -64,7 +65,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 printk.o vsprintf.o
 
 # include the stub's libfdt dependencies from lib/ when needed
 libfdt-deps			:= fdt_rw.c fdt_ro.c fdt_wip.c fdt.c \
drivers/firmware/efi/libstub/efi-stub-helper.c +0 -143

163 lines not on this page · raw message

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

154 lines not on this page · raw message

drivers/firmware/efi/libstub/string.c +31 -2
index 9f5810d86646..5a1519d435bf 100644
@@ -11,7 +11,37 @@
 #include <linux/types.h>
 #include <linux/string.h>
 
-#ifndef __HAVE_ARCH_STRSTR
+#ifndef EFI_HAVE_STRLEN
+/**
+ * strlen - Find the length of a string
+ * @s: The string to be sized
+ */
+size_t strlen(const char *s)
+{
+	const char *sc;
+
+	for (sc = s; *sc != '\0'; ++sc)
+		/* nothing */;
+	return sc - s;
+}
+#endif
+
+#ifndef EFI_HAVE_STRNLEN
+/**
+ * strnlen - Find the length of a length-limited string
+ * @s: The string to be sized
+ * @count: The maximum number of bytes to search
+ */
+size_t strnlen(const char *s, size_t count)
+{
+	const char *sc;
+
+	for (sc = s; count-- && *sc != '\0'; ++sc)
+		/* nothing */;
+	return sc - s;
+}
+#endif
+
 /**
  * strstr - Find the first substring in a %NUL terminated string
  * @s1: The string to be searched
@@ -33,7 +63,6 @@ char *strstr(const char *s1, const char *s2)
 	}
 	return NULL;
 }
-#endif
 
 /**
  * strncmp - Compare two length-limited strings
drivers/firmware/efi/libstub/zboot.c +10 -20
index 38173ec29cd5..5f41a5b17d6e 100644
@@ -32,19 +32,9 @@ static unsigned long free_mem_ptr, free_mem_end_ptr;
 extern char efi_zboot_header[];
 extern char _gzdata_start[], _gzdata_end[];
 
-static void log(efi_char16_t str[])
-{
-	efi_call_proto(efi_table_attr(efi_system_table, con_out),
-		       output_string, L"EFI decompressor: ");
-	efi_call_proto(efi_table_attr(efi_system_table, con_out),
-		       output_string, str);
-	efi_call_proto(efi_table_attr(efi_system_table, con_out),
-		       output_string, L"\n");
-}
-
 static void error(char *x)
 {
-	log(L"error() called from decompressor library\n");
+	efi_err("EFI decompressor: %s\n", x);
 }
 
 static efi_status_t __efiapi
@@ -91,7 +81,7 @@ load_file(efi_load_file_protocol_t *this, efi_device_path_protocol_t *rem,
 		ret = __decompress(_gzdata_start, compressed_size, NULL, NULL,
 				   buffer, size, NULL, error);
 		if (ret	< 0) {
-			log(L"Decompression failed");
+			error("Decompression failed");
 			return EFI_DEVICE_ERROR;
 		}
 	} else {
@@ -180,7 +170,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 	status = efi_bs_call(handle_protocol, handle,
 			     &LOADED_IMAGE_PROTOCOL_GUID, (void **)&parent);
 	if (status != EFI_SUCCESS) {
-		log(L"Failed to locate parent's loaded image protocol");
+		error("Failed to locate parent's loaded image protocol");
 		return status;
 	}
 
@@ -207,7 +197,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 			     sizeof(struct efi_vendor_dev_path),
 			     (void **)&dp_alloc);
 	if (status != EFI_SUCCESS) {
-		log(L"Failed to allocate device path pool memory");
+		error("Failed to allocate device path pool memory");
 		return status;
 	}
 
@@ -236,21 +226,21 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 			     &EFI_LOAD_FILE2_PROTOCOL_GUID, &zboot_load_file2,
 			     NULL);
 	if (status != EFI_SUCCESS) {
-		log(L"Failed to install LoadFile2 protocol and device path");
+		error("Failed to install LoadFile2 protocol and device path");
 		goto free_dpalloc;
 	}
 
 	status = efi_bs_call(load_image, false, handle, li_dp, NULL, 0,
 			     &child_handle);
 	if (status != EFI_SUCCESS) {
-		log(L"Failed to load image");
+		error("Failed to load image");
 		goto uninstall_lf2;
 	}
 
 	status = efi_bs_call(handle_protocol, child_handle,
 			     &LOADED_IMAGE_PROTOCOL_GUID, (void **)&child);
 	if (status != EFI_SUCCESS) {
-		log(L"Failed to locate child's loaded image protocol");
+		error("Failed to locate child's loaded image protocol");
 		goto unload_image;
 	}
 
@@ -261,9 +251,9 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 	status = efi_bs_call(start_image, child_handle, &exit_data_size,
 			     &exit_data);
 	if (status != EFI_SUCCESS) {
-		log(L"StartImage() returned with error");
+		error("StartImage() returned with error:");
 		if (exit_data_size > 0)
-			log(exit_data);
+			efi_err("%ls\n", exit_data);
 
 		// If StartImage() returns EFI_SECURITY_VIOLATION, the image is
 		// not unloaded so we need to do it by hand.
@@ -286,7 +276,7 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 
 	// Free ExitData in case Exit() returned with a failure code,
 	// but return the original status code.
-	log(L"Exit() returned with failure code");
+	error("Exit() returned with failure code");
 	if (exit_data != NULL)
 		efi_bs_call(free_pool, exit_data);
 	return status;
Signature · 1 line
2.35.1
author maintainer
#17 lore raw

PATCH 11/21 efi: loongarch: Drop exports of unused string routines

In reply to Ard Biesheuvel

Drop the __efistub_ prefixed exports of various routines that the EFI stub on LoongArch does not even use.

---
 arch/loongarch/kernel/image-vars.h | 5 -----
 1 file changed, 5 deletions(-)
arch/loongarch/kernel/image-vars.h +0 -5
index 28bc632ec296..e561989d02de 100644
@@ -7,12 +7,7 @@
 
 #ifdef CONFIG_EFI_STUB
 
-__efistub_memchr		= memchr;
-__efistub_strcat		= strcat;
 __efistub_strcmp		= strcmp;
-__efistub_strncat		= strncat;
-__efistub_strnstr		= strnstr;
-__efistub_strrchr		= strrchr;
 __efistub_kernel_entry		= kernel_entry;
 __efistub_kernel_asize		= kernel_asize;
 __efistub_kernel_fsize		= kernel_fsize;
Signature · 1 line
2.35.1
author maintainer
#18 lore raw

PATCH 12/21 efi: libstub: Move screen_info handling to common code

In reply to Ard Biesheuvel

Currently, arm64, RISC-V and LoongArch rely on the fact that struct screen_info can be accessed directly, due to the fact that the EFI stub and the core kernel are part of the same image. This will change after a future patch, so let's ensure that the screen_info handling is able to deal with this, by adopting the arm32 approach of passing it as a configuration table. While at it, switch to ACPI reclaim memory to hold the screen_info data, which is more appropriate for this kind of allocation.

---
 arch/arm/include/asm/efi.h                 |  3 --
 arch/arm/kernel/efi.c                      | 31 ++---------
 arch/arm64/include/asm/efi.h               |  6 ---
 arch/loongarch/include/asm/efi.h           |  9 ----
 arch/loongarch/kernel/efi.c                | 24 ++++++++-
 arch/riscv/include/asm/efi.h               |  6 ---
 drivers/firmware/efi/efi-init.c            | 21 ++++++--
 drivers/firmware/efi/efi.c                 |  5 ++
 drivers/firmware/efi/libstub/Makefile      |  3 +-
 drivers/firmware/efi/libstub/arm32-stub.c  | 37 -------------
 drivers/firmware/efi/libstub/efi-stub.c    |  9 ++++
 drivers/firmware/efi/libstub/efistub.h     |  3 ++
 drivers/firmware/efi/libstub/screen_info.c | 56 ++++++++++++++++++++
 include/linux/efi.h                        |  2 +-
 14 files changed, 121 insertions(+), 94 deletions(-)
arch/arm/include/asm/efi.h +0 -3
index 4bdd930167c0..b95241b1ca65 100644
@@ -43,9 +43,6 @@ void efi_virtmap_unload(void);
 
 /* arch specific definitions used by the stub code */
 
-struct screen_info *alloc_screen_info(void);
-void free_screen_info(struct screen_info *si);
-
 /*
  * A reasonable upper bound for the uncompressed kernel size is 32 MBytes,
  * so we will reserve that amount of memory. We have no easy way to tell what
arch/arm/kernel/efi.c +5 -26
index e50ad7eefc02..882104f43b3b 100644
@@ -75,38 +75,13 @@ int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
 	return 0;
 }
 
-static unsigned long __initdata screen_info_table = EFI_INVALID_TABLE_ADDR;
 static unsigned long __initdata cpu_state_table = EFI_INVALID_TABLE_ADDR;
 
 const efi_config_table_type_t efi_arch_tables[] __initconst = {
-	{LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID, &screen_info_table},
 	{LINUX_EFI_ARM_CPU_STATE_TABLE_GUID, &cpu_state_table},
 	{}
 };
 
-static void __init load_screen_info_table(void)
-{
-	struct screen_info *si;
-
-	if (screen_info_table != EFI_INVALID_TABLE_ADDR) {
-		si = early_memremap_ro(screen_info_table, sizeof(*si));
-		if (!si) {
-			pr_err("Could not map screen_info config table\n");
-			return;
-		}
-		screen_info = *si;
-		early_memunmap(si, sizeof(*si));
-
-		/* dummycon on ARM needs non-zero values for columns/lines */
-		screen_info.orig_video_cols = 80;
-		screen_info.orig_video_lines = 25;
-
-		if (memblock_is_map_memory(screen_info.lfb_base))
-			memblock_mark_nomap(screen_info.lfb_base,
-					    screen_info.lfb_size);
-	}
-}
-
 static void __init load_cpu_state_table(void)
 {
 	if (cpu_state_table != EFI_INVALID_TABLE_ADDR) {
@@ -145,7 +120,11 @@ void __init arm_efi_init(void)
 {
 	efi_init();
 
-	load_screen_info_table();
+	if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI) {
+		/* dummycon on ARM needs non-zero values for columns/lines */
+		screen_info.orig_video_cols = 80;
+		screen_info.orig_video_lines = 25;
+	}
 
 	/* ARM does not permit early mappings to persist across paging_init() */
 	efi_memmap_unmap();
arch/arm64/include/asm/efi.h +0 -6
index 439e2bc5d5d8..af35ad671754 100644
@@ -76,12 +76,6 @@ static inline unsigned long efi_get_max_initrd_addr(unsigned long image_addr)
 	return (image_addr & ~(SZ_1G - 1UL)) + (1UL << (VA_BITS_MIN - 1));
 }
 
-#define alloc_screen_info(x...)		&screen_info
-
-static inline void free_screen_info(struct screen_info *si)
-{
-}
-
 #define EFI_ALLOC_ALIGN		SZ_64K
 
 /*
arch/loongarch/include/asm/efi.h +0 -9
index 174567b00ddb..60d6a170c18d 100644
@@ -19,15 +19,6 @@ void efifb_setup_from_dmi(struct screen_info *si, const char *opt);
 #define EFI_ALLOC_ALIGN		SZ_64K
 #define EFI_RT_VIRTUAL_OFFSET	CSR_DMW0_BASE
 
-static inline struct screen_info *alloc_screen_info(void)
-{
-	return &screen_info;
-}
-
-static inline void free_screen_info(struct screen_info *si)
-{
-}
-
 static inline unsigned long efi_get_max_initrd_addr(unsigned long image_addr)
 {
 	return ULONG_MAX;
arch/loongarch/kernel/efi.c +22 -2
index a31329971133..d75ce73e8ff8 100644
@@ -52,6 +52,27 @@ void __init efi_runtime_init(void)
 	set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
 }
 
+unsigned long __initdata screen_info_table = EFI_INVALID_TABLE_ADDR;
+
+static void __init init_screen_info(void)
+{
+	struct screen_info *si;
+
+	if (screen_info_table == EFI_INVALID_TABLE_ADDR)
+		return;
+
+	si = early_memremap(screen_info_table, sizeof(*si));
+	if (!si) {
+		pr_err("Could not map screen_info config table\n");
+		return;
+	}
+	screen_info = *si;
+	memset(si, 0, sizeof(*si));
+	early_memunmap(si, sizeof(*si));
+
+	memblock_reserve(screen_info.lfb_base, screen_info.lfb_size);
+}
+
 void __init efi_init(void)
 {
 	int size;
@@ -80,8 +101,7 @@ void __init efi_init(void)
 
 	set_bit(EFI_CONFIG_TABLES, &efi.flags);
 
-	if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI)
-		memblock_reserve(screen_info.lfb_base, screen_info.lfb_size);
+	init_screen_info();
 
 	if (boot_memmap == EFI_INVALID_TABLE_ADDR)
 		return;
arch/riscv/include/asm/efi.h +0 -6
index f74879a8f1ea..d0570936cb8c 100644
@@ -31,12 +31,6 @@ static inline unsigned long efi_get_max_initrd_addr(unsigned long image_addr)
 	return ULONG_MAX;
 }
 
-#define alloc_screen_info(x...)		(&screen_info)
-
-static inline void free_screen_info(struct screen_info *si)
-{
-}
-
 void efi_virtmap_load(void);
 void efi_virtmap_unload(void);
 
drivers/firmware/efi/efi-init.c +18 -3
index 2fd770b499a3..1639159493e3 100644
@@ -22,6 +22,8 @@
 
 #include <asm/efi.h>
 
+unsigned long __initdata screen_info_table = EFI_INVALID_TABLE_ADDR;
+
 static int __init is_memory(efi_memory_desc_t *md)
 {
 	if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC))
@@ -55,9 +57,22 @@ extern __weak const efi_config_table_type_t efi_arch_tables[];
 
 static void __init init_screen_info(void)
 {
-	if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI &&
-	    memblock_is_map_memory(screen_info.lfb_base))
-		memblock_mark_nomap(screen_info.lfb_base, screen_info.lfb_size);
+	struct screen_info *si;
+
+	if (screen_info_table != EFI_INVALID_TABLE_ADDR) {
+		si = early_memremap(screen_info_table, sizeof(*si));
+		if (!si) {
+			pr_err("Could not map screen_info config table\n");
+			return;
+		}
+		screen_info = *si;
+		memset(si, 0, sizeof(*si));
+		early_memunmap(si, sizeof(*si));
+
+		if (memblock_is_map_memory(screen_info.lfb_base))
+			memblock_mark_nomap(screen_info.lfb_base,
+					    screen_info.lfb_size);
+	}
 }
 
 static int __init uefi_init(u64 efi_system_table)
drivers/firmware/efi/efi.c +5 -0
index 9624735f1575..84a3e52fe888 100644
@@ -58,6 +58,8 @@ static unsigned long __initdata mem_reserve = EFI_INVALID_TABLE_ADDR;
 static unsigned long __initdata rt_prop = EFI_INVALID_TABLE_ADDR;
 static unsigned long __initdata initrd = EFI_INVALID_TABLE_ADDR;
 
+extern unsigned long screen_info_table;
+
 struct mm_struct efi_mm = {
 	.mm_mt			= MTREE_INIT_EXT(mm_mt, MM_MT_FLAGS, efi_mm.mmap_lock),
 	.mm_users		= ATOMIC_INIT(2),
@@ -544,6 +546,9 @@ static const efi_config_table_type_t common_tables[] __initconst = {
 #endif
 #ifdef CONFIG_EFI_COCO_SECRET
 	{LINUX_EFI_COCO_SECRET_AREA_GUID,	&efi.coco_secret,	"CocoSecret"	},
+#endif
+#ifdef CONFIG_EFI_GENERIC_STUB
+	{LINUX_EFI_SCREEN_INFO_TABLE_GUID,	&screen_info_table			},
 #endif
 	{},
 };
drivers/firmware/efi/libstub/Makefile +2 -1
index cb600c7a14de..53304e9c6778 100644
@@ -77,7 +77,8 @@ lib-$(CONFIG_EFI_PARAMS_FROM_FDT) += fdt.o \
 $(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
 	$(call if_changed_rule,cc_o_c)
 
-lib-$(CONFIG_EFI_GENERIC_STUB)	+= efi-stub.o string.o intrinsics.o systable.o
+lib-$(CONFIG_EFI_GENERIC_STUB)	+= efi-stub.o string.o intrinsics.o systable.o \
+				   screen_info.o
 
 lib-$(CONFIG_ARM)		+= arm32-stub.o
 lib-$(CONFIG_ARM64)		+= arm64-stub.o arm64-entry.o
drivers/firmware/efi/libstub/arm32-stub.c +0 -37
index 0131e3aaa605..1073dd947516 100644
@@ -76,43 +76,6 @@ void efi_handle_post_ebs_state(void)
 		      &efi_entry_state->sctlr_after_ebs);
 }
 
-static efi_guid_t screen_info_guid = LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID;
-
-struct screen_info *alloc_screen_info(void)
-{
-	struct screen_info *si;
-	efi_status_t status;
-
-	/*
-	 * Unlike on arm64, where we can directly fill out the screen_info
-	 * structure from the stub, we need to allocate a buffer to hold
-	 * its contents while we hand over to the kernel proper from the
-	 * decompressor.
-	 */
-	status = efi_bs_call(allocate_pool, EFI_RUNTIME_SERVICES_DATA,
-			     sizeof(*si), (void **)&si);
-
-	if (status != EFI_SUCCESS)
-		return NULL;
-
-	status = efi_bs_call(install_configuration_table,
-			     &screen_info_guid, si);
-	if (status == EFI_SUCCESS)
-		return si;
-
-	efi_bs_call(free_pool, si);
-	return NULL;
-}
-
-void free_screen_info(struct screen_info *si)
-{
-	if (!si)
-		return;
-
-	efi_bs_call(install_configuration_table, &screen_info_guid, NULL);
-	efi_bs_call(free_pool, si);
-}
-
 efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 unsigned long *image_size,
 				 unsigned long *reserve_addr,
drivers/firmware/efi/libstub/efi-stub.c +9 -0
index b55d1009d4c8..8521dc09c6ae 100644
@@ -47,6 +47,15 @@
 static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
 static bool flat_va_mapping = (EFI_RT_VIRTUAL_OFFSET != 0);
 
+struct screen_info * __weak alloc_screen_info(void)
+{
+	return &screen_info;
+}
+
+void __weak free_screen_info(struct screen_info *si)
+{
+}
+
 static struct screen_info *setup_graphics(void)
 {
 	efi_guid_t gop_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
drivers/firmware/efi/libstub/efistub.h +3 -0
index 313647b1c196..215186a29ab9 100644
@@ -1027,4 +1027,7 @@ efi_enable_reset_attack_mitigation(void) { }
 
 void efi_retrieve_tpm2_eventlog(void);
 
+struct screen_info *alloc_screen_info(void);
+void free_screen_info(struct screen_info *si);
+
 #endif
drivers/firmware/efi/libstub/screen_info.c new file +56 -0

56 lines not on this page · raw message

include/linux/efi.h +1 -1
index 256e70e42114..ca3278077670 100644
@@ -404,7 +404,7 @@ void efi_native_runtime_setup(void);
  * structure that was populated by the stub based on the GOP protocol instance
  * associated with ConOut
  */
-#define LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID	EFI_GUID(0xe03fc20a, 0x85dc, 0x406e,  0xb9, 0x0e, 0x4a, 0xb5, 0x02, 0x37, 0x1d, 0x95)
+#define LINUX_EFI_SCREEN_INFO_TABLE_GUID	EFI_GUID(0xe03fc20a, 0x85dc, 0x406e,  0xb9, 0x0e, 0x4a, 0xb5, 0x02, 0x37, 0x1d, 0x95)
 #define LINUX_EFI_ARM_CPU_STATE_TABLE_GUID	EFI_GUID(0xef79e4aa, 0x3c3d, 0x4989,  0xb9, 0x02, 0x07, 0xa9, 0x43, 0xe5, 0x50, 0xd2)
 #define LINUX_EFI_LOADER_ENTRY_GUID		EFI_GUID(0x4a67b082, 0x0a4c, 0x41cf,  0xb6, 0xc7, 0x44, 0x0b, 0x29, 0xbb, 0x8c, 0x4f)
 #define LINUX_EFI_RANDOM_SEED_TABLE_GUID	EFI_GUID(0x1ce1e5bc, 0x7ceb, 0x42f2,  0x81, 0xe5, 0x8a, 0xad, 0xf1, 0x80, 0xf5, 0x7b)
Signature · 1 line
2.35.1
author maintainer
#19 lore raw

PATCH 13/21 efi: libstub: Provide local implementations of strrchr() and memchr()

In reply to Ard Biesheuvel

Clone the implementations of strrchr() and memchr() in lib/string.c so we can use them in the standalone zboot decompressor app. These routines are used by the FDT handling code.

---
 arch/arm64/kernel/image-vars.h        |  3 -
 arch/riscv/kernel/image-vars.h        |  2 -
 drivers/firmware/efi/libstub/Makefile |  2 +
 drivers/firmware/efi/libstub/string.c | 62 ++++++++++++++++++++
 4 files changed, 64 insertions(+), 5 deletions(-)
arch/arm64/kernel/image-vars.h +0 -3
index ace584e0b065..f31130ba0233 100644
@@ -21,9 +21,6 @@ PROVIDE(__efistub_primary_entry_offset	= primary_entry - _text);
  * linked at. The routines below are all implemented in assembler in a
  * position independent manner
  */
-PROVIDE(__efistub_memchr		= __pi_memchr);
-PROVIDE(__efistub_strcmp		= __pi_strcmp);
-PROVIDE(__efistub_strrchr		= __pi_strrchr);
 PROVIDE(__efistub_dcache_clean_poc	= __pi_dcache_clean_poc);
 
 PROVIDE(__efistub__text			= _text);
arch/riscv/kernel/image-vars.h +0 -2
index 4913abd6dd10..7e2962ef73f9 100644
@@ -23,9 +23,7 @@
  * linked at. The routines below are all implemented in assembler in a
  * position independent manner
  */
-__efistub_memchr		= memchr;
 __efistub_strcmp		= strcmp;
-__efistub_strrchr		= strrchr;
 
 __efistub__start		= _start;
 __efistub__start_kernel		= _start_kernel;
drivers/firmware/efi/libstub/Makefile +2 -0
index 53304e9c6778..6824ee5a4ae6 100644
@@ -21,6 +21,8 @@ cflags-$(CONFIG_X86)		+= -m$(BITS) -D__KERNEL__ \
 cflags-$(CONFIG_ARM64)		:= -fpie $(DISABLE_STACKLEAK_PLUGIN) \
 				   $(call cc-option,-mbranch-protection=none)
 cflags-$(CONFIG_ARM)		:= -DEFI_HAVE_STRLEN -DEFI_HAVE_STRNLEN \
+				   -DEFI_HAVE_MEMCHR -DEFI_HAVE_STRRCHR \
+				   -DEFI_HAVE_STRCMP \
 				   -fno-builtin -fpic \
 				   $(call cc-option,-mno-single-pic-base)
 cflags-$(CONFIG_RISCV)		:= -fpic
drivers/firmware/efi/libstub/string.c +62 -0

71 lines not on this page · raw message

Signature · 1 line
2.35.1
author maintainer
#20 lore raw

PATCH 14/21 efi: libstub: Factor out EFI stub entrypoint into separate file

In reply to Ard Biesheuvel

In preparation for allowing the EFI zboot decompressor to reuse most of the EFI stub machinery, factor out the actual EFI PE/COFF entrypoint into a separate file.

---
 drivers/firmware/efi/libstub/Makefile         |  3 +-
 drivers/firmware/efi/libstub/efi-stub-entry.c | 65 ++++++++++++++
 drivers/firmware/efi/libstub/efi-stub.c       | 89 ++++++--------------
 drivers/firmware/efi/libstub/efistub.h        |  8 ++
 4 files changed, 101 insertions(+), 64 deletions(-)
drivers/firmware/efi/libstub/Makefile +2 -1
index 6824ee5a4ae6..d5d6c6239f9c 100644
@@ -67,7 +67,8 @@ 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 printk.o vsprintf.o
+				   alignedmem.o relocate.o printk.o vsprintf.o \
+				   efi-stub-entry.o
 
 # include the stub's libfdt dependencies from lib/ when needed
 libfdt-deps			:= fdt_rw.c fdt_ro.c fdt_wip.c fdt.c \
drivers/firmware/efi/libstub/efi-stub-entry.c new file +65 -0

65 lines not on this page · raw message

drivers/firmware/efi/libstub/efi-stub.c +26 -63

125 lines not on this page · raw message

drivers/firmware/efi/libstub/efistub.h +8 -0
index 215186a29ab9..a2fc0727880a 100644
@@ -1010,6 +1010,14 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 efi_loaded_image_t *image,
 				 efi_handle_t image_handle);
 
+/* shared entrypoint between the normal stub and the zboot stub */
+efi_status_t efi_stub_common(efi_handle_t handle,
+			     efi_loaded_image_t *image,
+			     unsigned long image_addr,
+			     char *cmdline_ptr);
+
+efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr);
+
 asmlinkage void __noreturn efi_enter_kernel(unsigned long entrypoint,
 					    unsigned long fdt_addr,
 					    unsigned long fdt_size);
Signature · 1 line
2.35.1
author maintainer
#21 lore raw

PATCH 15/21 efi: libstub: Add image code and data size to the zimage metadata

In reply to Ard Biesheuvel

In order to be able to switch from LoadImage() [which treats the supplied PE/COFF image as file input only, and reconstructs the memory image based on the section descriptors] to a mode where we allocate the memory directly, and invoke the image in place, we need to now how much memory to allocate beyond the end of the image. So copy this information from the payload's PE/COFF header to the end of the compressed version of the payload, so that the decompressor app can access it before performing the decompression itself.

We'll also need to size of the code region once we switch arm64 to jumping to the kernel proper with MMU and caches enabled, so let's capture that information as well. Note that SizeOfCode does not account for the header, so we need SizeOfHeaders as well.

---
 drivers/firmware/efi/libstub/Makefile.zboot | 26 +++++++++-----------
 drivers/firmware/efi/libstub/zboot-header.S |  2 +-
 2 files changed, 13 insertions(+), 15 deletions(-)
drivers/firmware/efi/libstub/Makefile.zboot +12 -14
index 3340b385a05b..c5d11473ef80 100644
@@ -10,18 +10,17 @@ comp-type-$(CONFIG_KERNEL_LZO)		:= lzo
 comp-type-$(CONFIG_KERNEL_XZ)		:= xzkern
 comp-type-$(CONFIG_KERNEL_ZSTD)		:= zstd22
 
-# in GZIP, the appended le32 carrying the uncompressed size is part of the
-# format, but in other cases, we just append it at the end for convenience,
-# causing the original tools to complain when checking image integrity.
-# So disregard it when calculating the payload size in the zimage header.
-zboot-method-y				:= $(comp-type-y)_with_size
-zboot-size-len-y			:= 4
-
-zboot-method-$(CONFIG_KERNEL_GZIP)	:= gzip
-zboot-size-len-$(CONFIG_KERNEL_GZIP)	:= 0
-
-$(obj)/vmlinuz: $(obj)/$(EFI_ZBOOT_PAYLOAD) FORCE
-	$(call if_changed,$(zboot-method-y))
+# Copy the SizeOfHeaders, SizeOfCode and SizeOfImage fields from the payload to
+# the end of the compressed image. Note that this presupposes a PE header
+# offset of 64 bytes, which is what arm64, RISC-V and LoongArch use.
+quiet_cmd_pesize = PESIZE  $@
+      cmd_pesize = ( dd status=none if=$< bs=1 count=4 skip=148 ; \
+		     dd status=none if=$< bs=1 count=4 skip=92 ; \
+		     dd status=none if=$< bs=1 count=4 skip=144 ) >> $@
+
+$(obj)/vmlinuz: $(obj)/$(EFI_ZBOOT_PAYLOAD)
+	$(call cmd,$(comp-type-y))
+	$(call cmd,pesize)
 
 OBJCOPYFLAGS_vmlinuz.o := -I binary -O $(EFI_ZBOOT_BFD_TARGET) \
 			  --rename-section .data=.gzdata,load,alloc,readonly,contents
@@ -30,7 +29,6 @@ $(obj)/vmlinuz.o: $(obj)/vmlinuz FORCE
 
 AFLAGS_zboot-header.o += -DMACHINE_TYPE=IMAGE_FILE_MACHINE_$(EFI_ZBOOT_MACH_TYPE) \
 			 -DZBOOT_EFI_PATH="\"$(realpath $(obj)/vmlinuz.efi.elf)\"" \
-			 -DZBOOT_SIZE_LEN=$(zboot-size-len-y) \
 			 -DCOMP_TYPE="\"$(comp-type-y)\""
 
 $(obj)/zboot-header.o: $(srctree)/drivers/firmware/efi/libstub/zboot-header.S FORCE
@@ -46,4 +44,4 @@ OBJCOPYFLAGS_vmlinuz.efi := -O binary
 $(obj)/vmlinuz.efi: $(obj)/vmlinuz.efi.elf FORCE
 	$(call if_changed,objcopy)
 
-targets += zboot-header.o vmlinuz vmlinuz.o vmlinuz.efi.elf vmlinuz.efi
+targets += zboot-header.o vmlinuz.o vmlinuz.efi.elf vmlinuz.efi
drivers/firmware/efi/libstub/zboot-header.S +1 -1
index 9e6fe061ab07..bc2d7750d7f1 100644
@@ -17,7 +17,7 @@ __efistub_efi_zboot_header:
 	.long		MZ_MAGIC
 	.ascii		"zimg"					// image type
 	.long		__efistub__gzdata_start - .Ldoshdr	// payload offset
-	.long		__efistub__gzdata_size - ZBOOT_SIZE_LEN	// payload size
+	.long		__efistub__gzdata_size - 12		// payload size
 	.long		0, 0					// reserved
 	.asciz		COMP_TYPE				// compression type
 	.org		.Ldoshdr + 0x3c
Signature · 1 line
2.35.1
author maintainer
#22 lore raw

PATCH 16/21 efi: libstub: Factor out min alignment and preferred kernel load address

In reply to Ard Biesheuvel

Factor out the expressions that describe the preferred placement of the loaded image as well as the minimum alignment so we can reuse them in the decompressor.

---
 arch/arm64/include/asm/efi.h                  | 15 +++++++++++++++
 arch/loongarch/include/asm/efi.h              |  7 +++++++
 arch/riscv/include/asm/efi.h                  | 11 +++++++++++
 drivers/firmware/efi/libstub/arm64-stub.c     | 11 +----------
 drivers/firmware/efi/libstub/loongarch-stub.c |  3 ++-
 drivers/firmware/efi/libstub/riscv-stub.c     | 15 +++------------
 6 files changed, 39 insertions(+), 23 deletions(-)
arch/arm64/include/asm/efi.h +15 -0
index af35ad671754..108b115dbf5b 100644
@@ -76,6 +76,21 @@ static inline unsigned long efi_get_max_initrd_addr(unsigned long image_addr)
 	return (image_addr & ~(SZ_1G - 1UL)) + (1UL << (VA_BITS_MIN - 1));
 }
 
+static inline unsigned long efi_get_kimg_min_align(void)
+{
+	extern bool efi_nokaslr;
+
+	/*
+	 * Although relocatable kernels can fix up the misalignment with
+	 * respect to MIN_KIMG_ALIGN, the resulting virtual text addresses are
+	 * subtly out of sync with those recorded in the vmlinux when kaslr is
+	 * disabled but the image required relocation anyway. Therefore retain
+	 * 2M alignment if KASLR was explicitly disabled, even if it was not
+	 * going to be activated to begin with.
+	 */
+	return efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
+}
+
 #define EFI_ALLOC_ALIGN		SZ_64K
 
 /*
arch/loongarch/include/asm/efi.h +7 -0
index 60d6a170c18d..5a470c8d2bbc 100644
@@ -24,4 +24,11 @@ static inline unsigned long efi_get_max_initrd_addr(unsigned long image_addr)
 	return ULONG_MAX;
 }
 
+static inline unsigned long efi_get_kimg_min_align(void)
+{
+	return SZ_2M;
+}
+
+#define EFI_KIMG_PREFERRED_ADDRESS	PHYSADDR(VMLINUX_LOAD_ADDRESS)
+
 #endif /* _ASM_LOONGARCH_EFI_H */
arch/riscv/include/asm/efi.h +11 -0
index d0570936cb8c..a742868eb23c 100644
@@ -31,6 +31,17 @@ static inline unsigned long efi_get_max_initrd_addr(unsigned long image_addr)
 	return ULONG_MAX;
 }
 
+static inline unsigned long efi_get_kimg_min_align(void)
+{
+	/*
+	 * RISC-V requires the kernel image to placed 2 MB aligned base for 64
+	 * bit and 4MB for 32 bit.
+	 */
+	return IS_ENABLED(CONFIG_64BIT) ? SZ_2M : SZ_4M;
+}
+
+#define EFI_KIMG_PREFERRED_ADDRESS	efi_get_kimg_min_align()
+
 void efi_virtmap_load(void);
 void efi_virtmap_unload(void);
 
drivers/firmware/efi/libstub/arm64-stub.c +1 -10
index e767a5ac8c3d..dd21b7b7309a 100644
@@ -88,16 +88,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	efi_status_t status;
 	unsigned long kernel_size, kernel_memsize = 0;
 	u32 phys_seed = 0;
-
-	/*
-	 * Although relocatable kernels can fix up the misalignment with
-	 * respect to MIN_KIMG_ALIGN, the resulting virtual text addresses are
-	 * subtly out of sync with those recorded in the vmlinux when kaslr is
-	 * disabled but the image required relocation anyway. Therefore retain
-	 * 2M alignment if KASLR was explicitly disabled, even if it was not
-	 * going to be activated to begin with.
-	 */
-	u64 min_kimg_align = efi_nokaslr ? MIN_KIMG_ALIGN : EFI_KIMG_ALIGN;
+	u64 min_kimg_align = efi_get_kimg_min_align();
 
 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
 		efi_guid_t li_fixed_proto = LINUX_EFI_LOADED_IMAGE_FIXED_GUID;
drivers/firmware/efi/libstub/loongarch-stub.c +2 -1
index 32329f2a92f9..a2e55e5c4003 100644
@@ -35,7 +35,8 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	kernel_addr = (unsigned long)&kernel_offset - kernel_offset;
 
 	status = efi_relocate_kernel(&kernel_addr, kernel_fsize, kernel_asize,
-				     PHYSADDR(VMLINUX_LOAD_ADDRESS), SZ_2M, 0x0);
+				     EFI_KIMG_PREFERRED_ADDRESS,
+				     efi_get_kimg_min_align(), 0x0);
 
 	*image_addr = kernel_addr;
 	*image_size = kernel_asize;
drivers/firmware/efi/libstub/riscv-stub.c +3 -12
index b450ebf95977..c5a551f69a7f 100644
@@ -12,16 +12,6 @@
 
 #include "efistub.h"
 
-/*
- * RISC-V requires the kernel image to placed 2 MB aligned base for 64 bit and
- * 4MB for 32 bit.
- */
-#ifdef CONFIG_64BIT
-#define MIN_KIMG_ALIGN		SZ_2M
-#else
-#define MIN_KIMG_ALIGN		SZ_4M
-#endif
-
 typedef void __noreturn (*jump_kernel_func)(unsigned long, unsigned long);
 
 static unsigned long hartid;
@@ -125,9 +115,10 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	 * lowest possible memory region as long as the address and size meets
 	 * the alignment constraints.
 	 */
-	preferred_addr = MIN_KIMG_ALIGN;
+	preferred_addr = EFI_KIMG_PREFERRED_ADDRESS;
 	status = efi_relocate_kernel(image_addr, kernel_size, *image_size,
-				     preferred_addr, MIN_KIMG_ALIGN, 0x0);
+				     preferred_addr, efi_get_kimg_min_align(),
+				     0x0);
 
 	if (status != EFI_SUCCESS) {
 		efi_err("Failed to relocate kernel\n");
Signature · 1 line
2.35.1
author maintainer
#23 lore raw

PATCH 17/21 efi/riscv: libstub: Split off kernel image relocation for builtin stub

In reply to Ard Biesheuvel

The RISC-V build of the EFI stub is part of the core kernel image, and therefore accesses section markers directly when it needs to figure out the size of the various section.

The zboot decompressor does not have access to those symbols, but doesn't really need that either. So let's move handle_kernel_image() into a separate file (or rather, move everything else into a separate file) so that the zboot build does not pull in unused code that links to symbols that it does not define.

---
 arch/riscv/include/asm/efi.h              |  2 +
 drivers/firmware/efi/libstub/Makefile     |  2 +-
 drivers/firmware/efi/libstub/riscv-stub.c | 81 ++--------------
 drivers/firmware/efi/libstub/riscv.c      | 98 ++++++++++++++++++++
 4 files changed, 108 insertions(+), 75 deletions(-)
arch/riscv/include/asm/efi.h +2 -0
index a742868eb23c..6cbc7437d886 100644
@@ -45,4 +45,6 @@ static inline unsigned long efi_get_kimg_min_align(void)
 void efi_virtmap_load(void);
 void efi_virtmap_unload(void);
 
+unsigned long stext_offset(void);
+
 #endif /* _ASM_EFI_H */
drivers/firmware/efi/libstub/Makefile +1 -1
index d5d6c6239f9c..28c8300c6a61 100644
@@ -86,7 +86,7 @@ lib-$(CONFIG_EFI_GENERIC_STUB)	+= efi-stub.o string.o intrinsics.o systable.o \
 lib-$(CONFIG_ARM)		+= arm32-stub.o
 lib-$(CONFIG_ARM64)		+= arm64-stub.o arm64-entry.o
 lib-$(CONFIG_X86)		+= x86-stub.o
-lib-$(CONFIG_RISCV)		+= riscv-stub.o
+lib-$(CONFIG_RISCV)		+= riscv.o riscv-stub.o
 lib-$(CONFIG_LOONGARCH)		+= loongarch-stub.o
 
 CFLAGS_arm32-stub.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
drivers/firmware/efi/libstub/riscv-stub.c +7 -74

96 lines not on this page · raw message

drivers/firmware/efi/libstub/riscv.c new file +98 -0

98 lines not on this page · raw message

Signature · 1 line
2.35.1
author maintainer
#24 lore raw

PATCH 18/21 efi/arm64: libstub: Split off kernel image relocation for builtin stub

In reply to Ard Biesheuvel

The arm64 build of the EFI stub is part of the core kernel image, and therefore accesses section markers directly when it needs to figure out the size of the various section.

The zboot decompressor does not have access to those symbols, but doesn't really need that either. So let's move handle_kernel_image() into a separate file (or rather, move everything else into a separate file) so that the zboot build does not pull in unused code that links to symbols that it does not define.

While at it, introduce a helper routine that the generic zboot loader will need to invoke after decompressing the image but before invoking it, to ensure that the I-side view of memory is consistent.

---
 drivers/firmware/efi/libstub/Makefile      |  2 +-
 drivers/firmware/efi/libstub/arm64-entry.S | 12 ++++
 drivers/firmware/efi/libstub/arm64-stub.c  | 28 ---------
 drivers/firmware/efi/libstub/arm64.c       | 61 ++++++++++++++++++++
 drivers/firmware/efi/libstub/efistub.h     |  4 ++
 5 files changed, 78 insertions(+), 29 deletions(-)
drivers/firmware/efi/libstub/Makefile +1 -1
index 28c8300c6a61..ee24b222489c 100644
@@ -84,7 +84,7 @@ lib-$(CONFIG_EFI_GENERIC_STUB)	+= efi-stub.o string.o intrinsics.o systable.o \
 				   screen_info.o
 
 lib-$(CONFIG_ARM)		+= arm32-stub.o
-lib-$(CONFIG_ARM64)		+= arm64-stub.o arm64-entry.o
+lib-$(CONFIG_ARM64)		+= arm64.o arm64-stub.o arm64-entry.o
 lib-$(CONFIG_X86)		+= x86-stub.o
 lib-$(CONFIG_RISCV)		+= riscv.o riscv-stub.o
 lib-$(CONFIG_LOONGARCH)		+= loongarch-stub.o
drivers/firmware/efi/libstub/arm64-entry.S +12 -0
index 87082b222a87..ec5492a738fe 100644
@@ -8,6 +8,18 @@
 #include <linux/linkage.h>
 #include <asm/assembler.h>
 
+	/*
+	 * The entrypoint of a arm64 bare metal image is at offset #0 of the
+	 * image, so this is a reasonable default for primary_entry_offset.
+	 * Only when the EFI stub is integrated into the core kernel, it is not
+	 * guaranteed that the PE/COFF header has been copied to memory too, so
+	 * in this case, primary_entry_offset should be overridden by the
+	 * linker and point to primary_entry() directly.
+	 */
+	.globl	primary_entry_offset
+	.weak	primary_entry_offset
+	.set	primary_entry_offset, 0
+
 SYM_CODE_START(efi_enter_kernel)
 	/*
 	 * efi_pe_entry() will have copied the kernel image if necessary and we
drivers/firmware/efi/libstub/arm64-stub.c +0 -28
index dd21b7b7309a..7f0aab3a8ab3 100644
@@ -11,37 +11,9 @@
 #include <asm/efi.h>
 #include <asm/memory.h>
 #include <asm/sections.h>
-#include <asm/sysreg.h>
 
 #include "efistub.h"
 
-efi_status_t check_platform_features(void)
-{
-	u64 tg;
-
-	/*
-	 * If we have 48 bits of VA space for TTBR0 mappings, we can map the
-	 * UEFI runtime regions 1:1 and so calling SetVirtualAddressMap() is
-	 * unnecessary.
-	 */
-	if (VA_BITS_MIN >= 48)
-		efi_novamap = true;
-
-	/* UEFI mandates support for 4 KB granularity, no need to check */
-	if (IS_ENABLED(CONFIG_ARM64_4K_PAGES))
-		return EFI_SUCCESS;
-
-	tg = (read_cpuid(ID_AA64MMFR0_EL1) >> ID_AA64MMFR0_EL1_TGRAN_SHIFT) & 0xf;
-	if (tg < ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MIN || tg > ID_AA64MMFR0_EL1_TGRAN_SUPPORTED_MAX) {
-		if (IS_ENABLED(CONFIG_ARM64_64K_PAGES))
-			efi_err("This 64 KB granular kernel is not supported by your CPU\n");
-		else
-			efi_err("This 16 KB granular kernel is not supported by your CPU\n");
-		return EFI_UNSUPPORTED;
-	}
-	return EFI_SUCCESS;
-}
-
 /*
  * Distro versions of GRUB may ignore the BSS allocation entirely (i.e., fail
  * to provide space, and fail to zero it). Check for this condition by double
drivers/firmware/efi/libstub/arm64.c new file +61 -0

61 lines not on this page · raw message

drivers/firmware/efi/libstub/efistub.h +4 -0
index a2fc0727880a..576e5cfec980 100644
@@ -1038,4 +1038,8 @@ void efi_retrieve_tpm2_eventlog(void);
 struct screen_info *alloc_screen_info(void);
 void free_screen_info(struct screen_info *si);
 
+void efi_cache_sync_image(unsigned long image_base,
+			  unsigned long alloc_size,
+			  unsigned long code_size);
+
 #endif
Signature · 1 line
2.35.1
author maintainer
#25 lore raw

PATCH 19/21 efi/loongarch: Don't jump to kernel entry via the old image

In reply to Ard Biesheuvel

Currently, the EFI entry code for LoongArch is set up to copy the executable image to the preferred offset, but instead of branching directly into that image, it branches to the local copy of kernel_entry, and relies on the logic in that function to switch to the link time address instead.

This is a bit sloppy, and not something we can support once we merge the EFI decompressor with the EFI stub. So let's clean this up a bit, by adding a helper that computes the offset of kernel_entry from the start of the image, and simply adding the result to VMLINUX_LOAD_ADDRESS.

And considering that we cannot execute from anywhere else anyway, let's avoid efi_relocate_kernel() and just allocate the pages instead.

---
 arch/loongarch/include/asm/efi.h              |  2 ++
 drivers/firmware/efi/libstub/loongarch-stub.c | 33 ++++++++++++++------
 2 files changed, 26 insertions(+), 9 deletions(-)
arch/loongarch/include/asm/efi.h +2 -0
index 5a470c8d2bbc..97f16e60c6ff 100644
@@ -31,4 +31,6 @@ static inline unsigned long efi_get_kimg_min_align(void)
 
 #define EFI_KIMG_PREFERRED_ADDRESS	PHYSADDR(VMLINUX_LOAD_ADDRESS)
 
+unsigned long kernel_entry_address(void);
+
 #endif /* _ASM_LOONGARCH_EFI_H */
drivers/firmware/efi/libstub/loongarch-stub.c +24 -9
index a2e55e5c4003..6b30fc281458 100644
@@ -29,18 +29,27 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 efi_loaded_image_t *image,
 				 efi_handle_t image_handle)
 {
+	int nr_pages = round_up(kernel_asize, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
+	efi_physical_addr_t kernel_addr = EFI_KIMG_PREFERRED_ADDRESS;
 	efi_status_t status;
-	unsigned long kernel_addr = 0;
 
-	kernel_addr = (unsigned long)&kernel_offset - kernel_offset;
-
-	status = efi_relocate_kernel(&kernel_addr, kernel_fsize, kernel_asize,
-				     EFI_KIMG_PREFERRED_ADDRESS,
-				     efi_get_kimg_min_align(), 0x0);
+	/*
+	 * Allocate space for the kernel image at the preferred offset. This is
+	 * the only location in memory from where we can execute the image, so
+	 * no point in falling back to another allocation.
+	 */
+	status = efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRESS,
+			     EFI_LOADER_DATA, nr_pages, &kernel_addr);
+	if (status != EFI_SUCCESS)
+		return status;
 
-	*image_addr = kernel_addr;
+	*image_addr = EFI_KIMG_PREFERRED_ADDRESS;
 	*image_size = kernel_asize;
 
+	memcpy((void *)EFI_KIMG_PREFERRED_ADDRESS,
+	       (void *)&kernel_offset - kernel_offset,
+	       kernel_fsize);
+
 	return status;
 }
 
@@ -64,6 +73,13 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)
 	return EFI_SUCCESS;
 }
 
+unsigned long kernel_entry_address(void)
+{
+	unsigned long base = (unsigned long)&kernel_offset - kernel_offset;
+
+	return (unsigned long)&kernel_entry - base + VMLINUX_LOAD_ADDRESS;
+}
+
 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
 			     unsigned long kernel_addr, char *cmdline_ptr)
 {
@@ -95,8 +111,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
 	csr_write64(CSR_DMW0_INIT, LOONGARCH_CSR_DMWIN0);
 	csr_write64(CSR_DMW1_INIT, LOONGARCH_CSR_DMWIN1);
 
-	real_kernel_entry = (kernel_entry_t)
-		((unsigned long)&kernel_entry - kernel_addr + VMLINUX_LOAD_ADDRESS);
+	real_kernel_entry = (void *)kernel_entry_address();
 
 	real_kernel_entry(true, (unsigned long)cmdline_ptr,
 			  (unsigned long)efi_system_table);
Signature · 1 line
2.35.1
author maintainer
#26 lore raw

PATCH 20/21 efi/loongarch: libstub: Split off kernel image relocation for builtin stub

In reply to Ard Biesheuvel

The LoongArch build of the EFI stub is part of the core kernel image, and therefore accesses section markers directly when it needs to figure out the size of the various section.

The zboot decompressor does not have access to those symbols, but doesn't really need that either. So let's move handle_kernel_image() into a separate file (or rather, move everything else into a separate file) so that the zboot build does not pull in unused code that links to symbols that it does not define.

---
 drivers/firmware/efi/libstub/Makefile         |  2 +-
 drivers/firmware/efi/libstub/loongarch-stub.c | 67 +---------------
 drivers/firmware/efi/libstub/loongarch.c      | 80 ++++++++++++++++++++
 3 files changed, 82 insertions(+), 67 deletions(-)
drivers/firmware/efi/libstub/Makefile +1 -1
index ee24b222489c..37e23e4ab68d 100644
@@ -87,7 +87,7 @@ lib-$(CONFIG_ARM)		+= arm32-stub.o
 lib-$(CONFIG_ARM64)		+= arm64.o arm64-stub.o arm64-entry.o
 lib-$(CONFIG_X86)		+= x86-stub.o
 lib-$(CONFIG_RISCV)		+= riscv.o riscv-stub.o
-lib-$(CONFIG_LOONGARCH)		+= loongarch-stub.o
+lib-$(CONFIG_LOONGARCH)		+= loongarch.o loongarch-stub.o
 
 CFLAGS_arm32-stub.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
 
drivers/firmware/efi/libstub/loongarch-stub.c +1 -66

85 lines not on this page · raw message

drivers/firmware/efi/libstub/loongarch.c new file +80 -0

80 lines not on this page · raw message

Signature · 1 line
2.35.1
author maintainer
#27 lore raw

PATCH 21/21 efi: libstub: Merge zboot decompressor with the ordinary stub

In reply to Ard Biesheuvel

Even though our EFI zboot decompressor is pedantically spec compliant and idiomatic for EFI image loaders, calling LoadImage() and StartImage() for the nested image is a bit of a burden. Not only does it create workflow issues for the distros (as both the inner and outer PE/COFF images need to be signed for secure boot), it also copies the image around in memory numerous times:
- first, the image is decompressed into a buffer;
- the buffer is consumed by LoadImage(), which copies the sections into a newly allocated memory region to hold the executable image;
- once the EFI stub is invoked by StartImage(), it will also move the image in memory in case of KASLR, mirrored memory or if the image must execute from a certain a priori defined address.

There are only two EFI spec compliant ways to load code into memory and execute it:
- use LoadImage() and StartImage(),
- call ExitBootServices() and take ownership of the entire system, after which anything goes.

Given that the EFI zboot decompressor always invokes the EFI stub, and given that both are built from the same set of objects, let's merge the two, so that we can avoid LoadImage()/StartImage but still load our image into memory without breaking the above rules.

This also means we can decompress the image directly into its final location, which could be randomized or meet other platform specific constraints that LoadImage() does not know how to adhere to. It also means that, even if the encapsulated image still has the EFI stub incorporated as well, it does not need to be signed for secure boot when wrapping it in the EFI zboot decompressor.

In the future, we might decide to retire the EFI stub attached to the decompressed image, but for the time being, they can happily coexist.

---
 drivers/firmware/efi/libstub/file.c  |  18 --
 drivers/firmware/efi/libstub/zboot.c | 284 +++++---------------
 2 files changed, 74 insertions(+), 228 deletions(-)
drivers/firmware/efi/libstub/file.c +0 -18
index 995d9d823519..d6a025df07dc 100644
@@ -74,28 +74,10 @@ static efi_status_t efi_open_file(efi_file_protocol_t *volume,
 static efi_status_t efi_open_volume(efi_loaded_image_t *image,
 				    efi_file_protocol_t **fh)
 {
-	struct efi_vendor_dev_path *dp = efi_table_attr(image, file_path);
-	efi_guid_t li_proto = LOADED_IMAGE_PROTOCOL_GUID;
 	efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
 	efi_simple_file_system_protocol_t *io;
 	efi_status_t status;
 
-	// If we are using EFI zboot, we should look for the file system
-	// protocol on the parent image's handle instead
-	if (IS_ENABLED(CONFIG_EFI_ZBOOT) &&
-	    image->parent_handle != NULL &&
-	    dp != NULL &&
-	    dp->header.type == EFI_DEV_MEDIA &&
-	    dp->header.sub_type == EFI_DEV_MEDIA_VENDOR &&
-	    !efi_guidcmp(dp->vendorguid, LINUX_EFI_ZBOOT_MEDIA_GUID)) {
-		status = efi_bs_call(handle_protocol, image->parent_handle,
-				     &li_proto, (void *)&image);
-		if (status != EFI_SUCCESS) {
-			efi_err("Failed to locate parent image handle\n");
-			return status;
-		}
-	}
-
 	status = efi_bs_call(handle_protocol, efi_table_attr(image, device_handle),
 			     &fs_proto, (void **)&io);
 	if (status != EFI_SUCCESS) {
drivers/firmware/efi/libstub/zboot.c +74 -210

321 lines not on this page · raw message

Signature · 1 line
2.35.1