Skip to contentSkip to conversation

efi: Expose loaded image properties via info struct

PATCH In review Ard Biesheuvel 4 participants 34 messages Nov 16

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

v3 cover efi: Expose loaded image properties via info struct

From: Ard Biesheuvel <[email protected]>

Clean up the arm64 build script and linker hacks that zboot uses to obtain properties of the loaded image (which it cannot access directly via a relative reference like the ordinary EFI stub).

Instead, define a generic struct efi_image_info, and expose its offset in the loaded image in a similar manner, but without relying on explicit linker script exports.

Populate the struct with section markers that all architecture use for loading the kernel image at boot, so that all the linker exports can be dropped for them.

Changes since v2:
- Add some additional fixes for RISC-V, which deviates from other architectures wrt its section markers. These patches have been sent out separately as well [0] as they will likely be taken through the RISC-V tree.
- Pass an efi_image_pointer to efi_stub_common() that points to the version inside the loaded image, so that its relative references accurately describe vmlinux in memory before boot.
- Avoid weak linkage hacks to derive the kernel entrypoint, and instead, always use the entrypoint passed via efi_image_info
- Use efi_image_info for sysfb_display_info so that efifb earlycon works much earlier on zboot
- Drop changes related to the previous bullet that are now no longer needed
- Add acks from Jason

Changes since v1:
- drop arm64 image header changes
- move efi_image_info out of __efistub_ scope so it can access vmlinux symbols directly
- implement in a generic manner for all architectures that support zboot
- address Sashiko comments

[0] https://lore.kernel.org/all/[email protected]/

Ard Biesheuvel (15):
efi/zboot: arm64: Add missing barrier after cache maintenance
riscv: Use _stext not _text to denote the start of kernel code
riscv: Make _text point to the start of the kernel image efi/riscv: Rename __init_text_end to __inittext_end like other archs efi/libstub: Avoid _end section marker in library code efi/libstub: Capture loaded image information in struct efi_image_info efi/libstub: Pass kernel entrypoint down to efi_enter_kernel() efi/libstub: arm64: Use struct efi_image_info to access image
properties
efi/libstub: loongarch: Use struct efi_image_info to access image
properties
efi/libstub: riscv: Use struct efi_image_info to access image
properties
efi/libstub: Make efi_remap_image() return efi_status_t efi/zboot: Disentangle efi_remap_image() from efi_cache_sync_image() efi/libstub: arm64: Drop zboot code_size hack
efi: Make config table based primary display handover ARM-only again Revert "efi: earlycon: Reprobe after parsing config tables"

 arch/arm/boot/compressed/head.S                      |  2 +-
 arch/arm/kernel/efi.c                                | 27 ++++++++
 arch/arm64/boot/Makefile                             |  3 -
 arch/arm64/kernel/efi-header.S                       |  2 +
 arch/arm64/kernel/image-vars.h                       | 14 ----
 arch/loongarch/kernel/efi.c                          | 20 +-----
 arch/loongarch/kernel/head.S                         |  1 +
 arch/loongarch/kernel/image-vars.h                   |  4 --
 arch/riscv/include/asm/sections.h                    |  2 +-
 arch/riscv/kernel/head.S                             |  1 +
 arch/riscv/kernel/image-vars.h                       |  8 ---
 arch/riscv/kernel/setup.c                            |  2 +-
 arch/riscv/kernel/vmlinux.lds.S                      |  6 +-
 drivers/firmware/efi/earlycon.c                      | 16 +----
 drivers/firmware/efi/efi-init.c                      | 24 ++-----
 drivers/firmware/efi/efi.c                           |  5 --
 drivers/firmware/efi/libstub/Makefile                |  3 +-
 drivers/firmware/efi/libstub/Makefile.zboot          | 11 ++-
 drivers/firmware/efi/libstub/arm32-stub.c            | 48 ++++++++++++-
 drivers/firmware/efi/libstub/arm64-stub.c            | 44 ++++++------
 drivers/firmware/efi/libstub/arm64.c                 | 23 ++-----
 drivers/firmware/efi/libstub/efi-stub-entry.c        | 27 ++------
 drivers/firmware/efi/libstub/efi-stub-helper.c       | 15 +++--
 drivers/firmware/efi/libstub/efi-stub.c              | 26 +++++--
 drivers/firmware/efi/libstub/efistub.h               | 71 +++++++++++++++++---
 drivers/firmware/efi/libstub/fdt.c                   |  5 +-
 drivers/firmware/efi/libstub/image_info.S            | 19 ++++++
 drivers/firmware/efi/libstub/kaslr.c                 |  2 +-
 drivers/firmware/efi/libstub/loongarch-stub.c        | 12 +---
 drivers/firmware/efi/libstub/loongarch.c             | 10 +--
 drivers/firmware/efi/libstub/primary_display.c       | 56 ---------------
 drivers/firmware/efi/libstub/riscv-stub.c            | 27 +++-----
 drivers/firmware/efi/libstub/riscv.c                 | 16 +----
 drivers/firmware/efi/libstub/zboot-decompress-gzip.c |  2 -
 drivers/firmware/efi/libstub/zboot-decompress-zstd.c |  2 -
 drivers/firmware/efi/libstub/zboot.c                 | 28 ++++++--
 drivers/firmware/efi/libstub/zboot.lds               |  6 +-
 include/linux/efi.h                                  |  1 -
 38 files changed, 289 insertions(+), 302 deletions(-)
 create mode 100644 drivers/firmware/efi/libstub/image_info.S
 delete mode 100644 drivers/firmware/efi/libstub/primary_display.c

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.56.0.611.g1030f0b940-goog
#1 lore raw

PATCH v3 1/15 efi/zboot: arm64: Add missing barrier after cache maintenance

From: Ard Biesheuvel <[email protected]>

Prevent the I-cache from fetching stale data by issuing a barrier after the data cache maintenance completes but before performing the I-cache maintenance.

---
 drivers/firmware/efi/libstub/arm64.c | 1 +
 1 file changed, 1 insertion(+)
drivers/firmware/efi/libstub/arm64.c +1 -0
index e57cd3de0a00..d35c6dd76601 100644
@@ -109,6 +109,7 @@ void efi_cache_sync_image(unsigned long image_base,
 		} while (size >= lsize);
 	}
 
+	dsb(ish);
 	asm("ic ialluis");
 	dsb(ish);
 	isb();
Signature · 1 line
2.56.0.611.g1030f0b940-goog
#2 lore raw

Re: [PATCH v3 01/15] efi/zboot: arm64: Add missing barrier after cache maintenance

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] efi/zboot: arm64: missing Fixes trailer on efi_cache_sync_image() fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] efi/zboot: arm64: missing Fixes trailer on efi_cache_sync_image() fix
--

commit 05b2747c96f7a33b4d57118d40c0532c9c8babee
Author: Ard Biesheuvel <[email protected]>

Quoted 8 lines

efi/zboot: arm64: Add missing barrier after cache maintenance

Prevent the I-cache from fetching stale data by issuing a barrier after the data cache maintenance completes but before performing the I-cache maintenance.

[Severity: Low]
efi_cache_sync_image() was introduced without this barrier in commit d9ffe524a538 ("efi/arm64: libstub: Split off kernel image relocation for builtin stub"). Should this include a Fixes: tag such as:

Fixes d9ffe524a538 ("efi/arm64: libstub: Split off kernel image relocation for builtin stub")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v3 2/15 riscv: Use _stext not _text to denote the start of kernel code

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

On RISC-V, _text and _stext currently refer to the same location, i.e., the start of the .text output section, which follows the image header (.head.text) emitted by HEAD_TEXT_SECTION. _start is the symbol that refers to the actual start of the image.

On other architectures such as arm64, _text refers to the start of the image, and _stext to the start of the kernel code, and generic code (such as the EFI stub) may start relying on this distinction.

So switch the only user of _text in arch/riscv, i.e., the "Kernel code" resource that is exposed via /proc/iomem (and from which the "Kernel image" resource inherits its start address), over to _stext. This is a no-op given that both symbols currently resolve to the same address, but it permits _text to be moved to the start of the image, making it an alias of _start.

---
 arch/riscv/kernel/setup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/setup.c +1 -1
index d79f99b86033..e6112d424fde 100644
@@ -91,7 +91,7 @@ static int __init add_kernel_resources(void)
 	 * child nodes.
 	 */
 
-	code_res.start = __pa_symbol(_text);
+	code_res.start = __pa_symbol(_stext);
 	code_res.end = __pa_symbol(_etext) - 1;
 	code_res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
 
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Reviewed-by on v3 2/15
#4 lore raw

Re: [PATCH v3 02/15] riscv: Use _stext not _text to denote the start of kernel code

Quoted 10 lines · Ard Biesheuvel

On RISC-V, _text and _stext currently refer to the same location, i.e., the start of the .text output section, which follows the image header (.head.text) emitted by HEAD_TEXT_SECTION. _start is the symbol that refers to the actual start of the image.

On other architectures such as arm64, _text refers to the start of the image, and _stext to the start of the kernel code, and generic code (such as the EFI stub) may start relying on this distinction.

[...]

Signature · 6 lines
Jason

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#5 lore raw

PATCH v3 3/15 riscv: Make _text point to the start of the kernel image

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

On RISC-V, _text currently refers to the start of the .text output section, which is placed after the image header (.head.text), and is therefore equal to _stext. On other architectures such as arm64, _text refers to the start of the image, and is used as such by generic code, e.g., to describe the kernel mapping in /proc/kcore and in the ELF headers of the vmcore, and by the EFI stub.

Now that arch/riscv no longer uses _text to refer to the start of the kernel code, move it to the start of the image so that it is equal to _start, and the distinction between _text and _stext is the same as on other architectures.

This also means that the image header is no longer omitted from the /proc/kcore and vmcore kernel text mappings. Other users of _text, such as kallsyms and ftrace, only use it as a reference point to calculate relative offsets, and are not affected by this change.

---
 arch/riscv/kernel/vmlinux.lds.S | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/vmlinux.lds.S +1 -1
index 1f4f8496941a..9849a3e4d102 100644
@@ -28,11 +28,11 @@ SECTIONS
 	/* Beginning of code and text segment */
 	. = LOAD_OFFSET;
 	_start = .;
+	_text = .;
 	HEAD_TEXT_SECTION
 	. = ALIGN(PAGE_SIZE);
 
 	.text : {
-		_text = .;
 		_stext = .;
 		TEXT_TEXT
 		SCHED_TEXT
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Reviewed-by on v3 3/15
#6 lore raw

Re: [PATCH v3 03/15] riscv: Make _text point to the start of the kernel image

Quoted 8 lines · Ard Biesheuvel

On RISC-V, _text currently refers to the start of the .text output section, which is placed after the image header (.head.text), and is therefore equal to _stext. On other architectures such as arm64, _text refers to the start of the image, and is used as such by generic code, e.g., to describe the kernel mapping in /proc/kcore and in the ELF headers of the vmcore, and by the EFI stub.

[...]

Signature · 1 line
Jason
#7 lore raw

PATCH v3 4/15 efi/riscv: Rename __init_text_end to __inittext_end like other archs

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Align RISC-V with arm64, LoongArch and x86, and drop the underscore between 'init' and 'text' in the section markers. This allows the value to be captured by generic EFI stub code that can be shared between all those architectures.

---
 arch/riscv/include/asm/sections.h         | 2 +-
 arch/riscv/kernel/image-vars.h            | 2 +-
 arch/riscv/kernel/vmlinux.lds.S           | 4 ++--
 drivers/firmware/efi/libstub/riscv-stub.c | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)
arch/riscv/include/asm/sections.h +1 -1
index a393d5035c54..4a882a28f414 100644
@@ -11,7 +11,7 @@
 extern char _start[];
 extern char _start_kernel[];
 extern char __init_data_begin[], __init_data_end[];
-extern char __init_text_begin[], __init_text_end[];
+extern char __inittext_begin[], __inittext_end[];
 extern char __alt_start[], __alt_end[];
 extern char __exittext_begin[], __exittext_end[];
 
arch/riscv/kernel/image-vars.h +1 -1
index 7b44b94f1283..bf703eba3fa5 100644
@@ -27,7 +27,7 @@ __efistub__start		= _start;
 __efistub__start_kernel		= _start_kernel;
 __efistub__end			= _end;
 __efistub__edata		= _edata;
-__efistub___init_text_end	= __init_text_end;
+__efistub___inittext_end	= __inittext_end;
 #if defined(CONFIG_EFI_EARLYCON) || defined(CONFIG_SYSFB)
 __efistub_sysfb_primary_display	= sysfb_primary_display;
 #endif
arch/riscv/kernel/vmlinux.lds.S +2 -2
index 9849a3e4d102..ba939c7365b2 100644
@@ -46,7 +46,7 @@ SECTIONS
 
 	. = ALIGN(SECTION_ALIGN);
 	__init_begin = .;
-	__init_text_begin = .;
+	__inittext_begin = .;
 	.init.text : AT(ADDR(.init.text) - LOAD_OFFSET) ALIGN(SECTION_ALIGN) { \
 		_sinittext = .;						\
 		INIT_TEXT						\
@@ -72,7 +72,7 @@ SECTIONS
 	}
 	__exittext_end = .;
 
-	__init_text_end = .;
+	__inittext_end = .;
 	. = ALIGN(SECTION_ALIGN);
 #ifdef CONFIG_EFI
 	. = ALIGN(PECOFF_SECTION_ALIGNMENT);
drivers/firmware/efi/libstub/riscv-stub.c +1 -1
index 725b634c5179..5cbc50a814e1 100644
@@ -34,7 +34,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	efi_status_t status;
 
 	kernel_size = _edata - _start;
-	kernel_codesize = __init_text_end - _start;
+	kernel_codesize = __inittext_end - _start;
 	kernel_memsize = kernel_size + (_end - _edata);
 	*image_addr = (unsigned long)_start;
 	*reserve_size = kernel_memsize;
Signature · 1 line
2.56.0.611.g1030f0b940-goog
#8 lore raw

PATCH v3 5/15 efi/libstub: Avoid _end section marker in library code

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

The _end marker could point to the end of the kernel image being loaded, or to the _end of the zboot decompressor image, and so it should only be used in code that is obviously only intended for the former case.

So instead, use kernel_memsize and the image address to obtain the same quantity.

---
 drivers/firmware/efi/libstub/kaslr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/efi/libstub/kaslr.c +1 -1
index 8f8dc335b3b3..b34e4599f0f6 100644
@@ -125,7 +125,7 @@ efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
 		if (!check_image_region(*image_addr, kernel_memsize)) {
 			efi_err("FIRMWARE BUG: Image BSS overlaps adjacent EFI memory region\n");
 		} else if (IS_ALIGNED(*image_addr, min_kimg_align) &&
-			   (unsigned long)_end < EFI_ALLOC_LIMIT) {
+			   (*image_addr + kernel_memsize) < EFI_ALLOC_LIMIT) {
 			/*
 			 * Just execute from wherever we were loaded by the
 			 * UEFI PE/COFF loader if the placement is suitable.
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Reviewed-by on v3 5/15
#9 lore raw

Re: [PATCH v3 05/15] efi/libstub: Avoid _end section marker in library code

Quoted 8 lines · Ard Biesheuvel

The _end marker could point to the end of the kernel image being loaded, or to the _end of the zboot decompressor image, and so it should only be used in code that is obviously only intended for the former case.

So instead, use kernel_memsize and the image address to obtain the same quantity.

[...]

Signature · 6 lines
Jason

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#10 lore raw

PATCH v3 6/15 efi/libstub: Capture loaded image information in struct efi_image_info

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Define a struct efi_image_info that carries properties of the loaded kernel image, and pass a pointer to it to efi_stub_common() so that EFI stub code can access these properties in a consistent manner for both the uncompressed kernel and the EFI zboot image.

Note that zboot can only access this information after decompressing the image, whereas code that is part of the EFI stub that gets integrated into Linux could just grab the efi_image_info pointer as needed.

However, in order to avoid future surprises, pass the efi_image_info pointer down via the calls into the shared stub code, replacing the image base address field, whose value can be derived from image_info as well as long as it is taken from the copy of the image that will ultimately be used to boot the kernel.

ARM implements neither, but -like x86- incorporates the EFI stub into its own decompressor and cannot incorporate struct efi_image_info as defined here, as it refers to global symbols in vmlinux. It does not really have a need for this either, so just define an empty struct for it.

---
 arch/arm64/kernel/efi-header.S                |  2 +
 arch/loongarch/kernel/head.S                  |  1 +
 arch/riscv/kernel/head.S                      |  1 +
 drivers/firmware/efi/libstub/Makefile         |  1 +
 drivers/firmware/efi/libstub/Makefile.zboot   | 11 +++-
 drivers/firmware/efi/libstub/arm32-stub.c     |  5 +-
 drivers/firmware/efi/libstub/arm64-stub.c     | 17 ++++--
 drivers/firmware/efi/libstub/efi-stub-entry.c | 22 +++-----
 drivers/firmware/efi/libstub/efi-stub.c       | 10 ++--
 drivers/firmware/efi/libstub/efistub.h        | 57 ++++++++++++++++++--
 drivers/firmware/efi/libstub/image_info.S     | 19 +++++++
 drivers/firmware/efi/libstub/loongarch-stub.c |  4 +-
 drivers/firmware/efi/libstub/riscv-stub.c     |  4 +-
 drivers/firmware/efi/libstub/zboot.c          | 22 ++++++--
 drivers/firmware/efi/libstub/zboot.lds        |  2 +
 15 files changed, 145 insertions(+), 33 deletions(-)
arch/arm64/kernel/efi-header.S +2 -0
index 329e8df9215f..54ca61d03403 100644
@@ -7,6 +7,8 @@
 #include <linux/pe.h>
 #include <linux/sizes.h>
 
+SYM_FUNC_ALIAS(efi_primary_entry, primary_entry)
+
 	.macro	efi_signature_nop
 #ifdef CONFIG_EFI
 .L_head:
arch/loongarch/kernel/head.S +1 -0
index 601ddd78b233..69c31b0b65c5 100644
@@ -35,6 +35,7 @@ pe_header:
 SYM_DATA(kernel_asize, .long _kernel_asize);
 SYM_DATA(kernel_fsize, .long _kernel_fsize);
 
+SYM_FUNC_ALIAS(efi_primary_entry, kernel_entry)
 #endif
 
 	__REF
arch/riscv/kernel/head.S +1 -0
index 1631fd6bc786..6c81f32be70b 100644
@@ -28,6 +28,7 @@ SYM_CODE_START(_start)
 	 * that expects this header format!!
 	 */
 #ifdef CONFIG_EFI
+SYM_FUNC_ALIAS(efi_primary_entry, _start_kernel)
 	/*
 	 * This instruction decodes to "MZ" ASCII required by UEFI.
 	 */
drivers/firmware/efi/libstub/Makefile +1 -0
index bbab12b8eb4b..9dc6a707fd79 100644
@@ -107,6 +107,7 @@ lib-$(CONFIG_UNACCEPTED_MEMORY) += unaccepted_memory.o bitmap.o find.o
 
 targets				:= $(lib-y)
 lib-y				:= $(patsubst %.o,%.stub.o,$(lib-y))
+lib-$(CONFIG_EFI_GENERIC_STUB)	+= image_info.o
 
 # Even when -mbranch-protection=none is set, Clang will generate a
 # .note.gnu.property for code-less object files (like lib/ctype.c),
drivers/firmware/efi/libstub/Makefile.zboot +10 -1
index 52baacf3e9d9..e2769bbec963 100644
@@ -26,9 +26,18 @@ zboot-size-len-$(CONFIG_KERNEL_ZSTD)	:= 4
 $(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
 	+$(call if_changed,$(zboot-method-y))
 
+# subtract the two symbol values to obtain the offset of struct efi_image_info
+get_image_info_offset = $(shell $(NM) -n $(objtree)/vmlinux |\
+			grep -E ' _text$$| __efistub_efistub_image_info$$' |\
+			awk 'NR == 1 { a = toupper($$1) } \
+			     NR == 2 { print "obase=16; ibase=16; " toupper($$1) "-" a }' |\
+			bc)
+
 # avoid eager evaluation to prevent references to non-existent build artifacts
 OBJCOPYFLAGS_vmlinuz.o = -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \
-			  --rename-section .data=.gzdata,load,alloc,readonly,contents
+			 --rename-section .data=.gzdata,load,alloc,readonly,contents \
+			 --add-symbol efistub_image_info_offset=0x$(get_image_info_offset)
+
 $(obj)/vmlinuz.o: $(obj)/vmlinuz FORCE
 	$(call if_changed,objcopy)
 
drivers/firmware/efi/libstub/arm32-stub.c +4 -1
index 1073dd947516..f7151a4915ba 100644
@@ -76,12 +76,15 @@ void efi_handle_post_ebs_state(void)
 		      &efi_entry_state->sctlr_after_ebs);
 }
 
+struct efi_image_info efistub_image_info = {};
+
 efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 unsigned long *image_size,
 				 unsigned long *reserve_addr,
 				 unsigned long *reserve_size,
 				 efi_loaded_image_t *image,
-				 efi_handle_t image_handle)
+				 efi_handle_t image_handle,
+				 const struct efi_image_info **image_info)
 {
 	const int slack = TEXT_OFFSET - 5 * PAGE_SIZE;
 	int alloc_size = MAX_UNCOMP_KERNEL_SIZE + EFI_PHYS_ALIGN;
drivers/firmware/efi/libstub/arm64-stub.c +13 -4
index 2c3869356147..c93ab8b6e9d3 100644
@@ -19,9 +19,11 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 unsigned long *reserve_addr,
 				 unsigned long *reserve_size,
 				 efi_loaded_image_t *image,
-				 efi_handle_t image_handle)
+				 efi_handle_t image_handle,
+				 const struct efi_image_info **image_info)
 {
 	unsigned long kernel_size, kernel_codesize, kernel_memsize;
+	efi_status_t status;
 
 	if (image->image_base != _text) {
 		efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
@@ -38,9 +40,16 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	*reserve_size = kernel_memsize;
 	*image_addr = (unsigned long)_text;
 
-	return efi_kaslr_relocate_kernel(image_addr, reserve_addr, reserve_size,
-					 kernel_size, kernel_codesize, kernel_memsize,
-					 efi_kaslr_get_phys_seed(image_handle));
+	status = efi_kaslr_relocate_kernel(image_addr, reserve_addr,
+					   reserve_size, kernel_size,
+					   kernel_codesize, kernel_memsize,
+					   efi_kaslr_get_phys_seed(image_handle));
+
+	if (status != EFI_SUCCESS)
+		return status;
+
+	*(unsigned long *)image_info += *image_addr - (unsigned long)_text;
+	return EFI_SUCCESS;
 }
 
 asmlinkage void primary_entry(void);
drivers/firmware/efi/libstub/efi-stub-entry.c +8 -14
index efa3ab8ad1c6..36ea6aa2cfcc 100644
@@ -7,22 +7,17 @@
 
 #include "efistub.h"
 
-static unsigned long kernel_image_offset;
+extern const struct efi_image_info efistub_image_info;
 
-static void *kernel_image_addr(void *addr)
-{
-	return addr + kernel_image_offset;
-}
-
-struct sysfb_display_info *alloc_primary_display(void)
+struct sysfb_display_info *
+alloc_primary_display(const struct efi_image_info *image_info)
 {
 	if (IS_ENABLED(CONFIG_ARM))
 		return __alloc_primary_display();
 
-	if (IS_ENABLED(CONFIG_X86) ||
-	    IS_ENABLED(CONFIG_EFI_EARLYCON) ||
+	if (IS_ENABLED(CONFIG_EFI_EARLYCON) ||
 	    IS_ENABLED(CONFIG_SYSFB))
-		return kernel_image_addr(&sysfb_primary_display);
+		return efistub_sysfb_display_info(image_info);
 
 	return NULL;
 }
@@ -36,6 +31,7 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 				   efi_system_table_t *systab)
 {
 	static efi_guid_t loaded_image_proto = LOADED_IMAGE_PROTOCOL_GUID;
+	const struct efi_image_info *image_info = &efistub_image_info;
 	efi_loaded_image_t *image;
 	efi_status_t status;
 	unsigned long image_addr;
@@ -72,15 +68,13 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 	status = handle_kernel_image(&image_addr, &image_size,
 				     &reserve_addr,
 				     &reserve_size,
-				     image, handle);
+				     image, handle, &image_info);
 	if (status != EFI_SUCCESS) {
 		efi_err("Failed to relocate kernel\n");
 		return status;
 	}
 
-	kernel_image_offset = image_addr - (unsigned long)image->image_base;
-
-	status = efi_stub_common(handle, image, image_addr, cmdline_ptr);
+	status = efi_stub_common(handle, image, image_info, cmdline_ptr);
 
 	efi_free(image_size, image_addr);
 	efi_free(reserve_size, reserve_addr);
drivers/firmware/efi/libstub/efi-stub.c +6 -4
index 6db10590f89b..bf9a9f53a95c 100644
@@ -51,14 +51,15 @@ static bool flat_va_mapping = (EFI_RT_VIRTUAL_OFFSET != 0);
 void __weak free_primary_display(struct sysfb_display_info *dpy)
 { }
 
-static struct sysfb_display_info *setup_primary_display(void)
+static struct sysfb_display_info *
+setup_primary_display(const struct efi_image_info *image_info)
 {
 	struct sysfb_display_info *dpy;
 	struct screen_info *screen = NULL;
 	struct edid_info *edid = NULL;
 	efi_status_t status;
 
-	dpy = alloc_primary_display();
+	dpy = alloc_primary_display(image_info);
 	if (!dpy)
 		return NULL;
 	screen = &dpy->screen;
@@ -160,9 +161,10 @@ efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr)
 
 efi_status_t efi_stub_common(efi_handle_t handle,
 			     efi_loaded_image_t *image,
-			     unsigned long image_addr,
+			     const struct efi_image_info *image_info,
 			     char *cmdline_ptr)
 {
+	unsigned long image_addr = efistub_kernel_base(image_info);
 	struct sysfb_display_info *dpy;
 	efi_status_t status;
 
@@ -170,7 +172,7 @@ efi_status_t efi_stub_common(efi_handle_t handle,
 	if (status != EFI_SUCCESS)
 		return status;
 
-	dpy = setup_primary_display();
+	dpy = setup_primary_display(image_info);
 
 	efi_retrieve_eventlog();
 	efi_bli_set_variables(image);
drivers/firmware/efi/libstub/efistub.h +54 -3

79 lines not on this page · raw message

drivers/firmware/efi/libstub/image_info.S new file +19 -0
new file mode 100644
index 000000000000..587a3eaff1c3
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include <linux/init.h>
+
+	__INITRODATA
+
+	.p2align	3
+	.globl		__efistub_efistub_image_info
+__efistub_efistub_image_info:
+0:	.long	_text - .
+	.long	__inittext_end - 0b
+	.long	_edata - 0b
+	.long	_end - 0b
+	.long	efi_primary_entry - .
+#if defined(CONFIG_EFI_EARLYCON) || defined(CONFIG_SYSFB)
+	.long	sysfb_primary_display - .
+#else
+	.long	0
+#endif
drivers/firmware/efi/libstub/loongarch-stub.c +3 -1
index c87ac7025107..d57e3d3557a5 100644
@@ -99,7 +99,8 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 unsigned long *reserve_addr,
 				 unsigned long *reserve_size,
 				 efi_loaded_image_t *image,
-				 efi_handle_t image_handle)
+				 efi_handle_t image_handle,
+				 const struct efi_image_info **image_info)
 {
 	efi_status_t status;
 	unsigned long kernel_addr = 0;
@@ -112,6 +113,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	*image_addr = kernel_addr;
 	*image_size = kernel_asize;
 
+	*(unsigned long *)image_info += *image_addr - (unsigned long)image->image_base;
 	return status;
 }
 
drivers/firmware/efi/libstub/riscv-stub.c +3 -1
index 5cbc50a814e1..0ddbe7cc8628 100644
@@ -28,7 +28,8 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 unsigned long *reserve_addr,
 				 unsigned long *reserve_size,
 				 efi_loaded_image_t *image,
-				 efi_handle_t image_handle)
+				 efi_handle_t image_handle,
+				 const struct efi_image_info **image_info)
 {
 	unsigned long kernel_size, kernel_codesize, kernel_memsize;
 	efi_status_t status;
@@ -46,6 +47,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	if (status != EFI_SUCCESS)
 		efi_err("Failed to relocate kernel\n");
 
+	*(unsigned long *)image_info += *image_addr - (unsigned long)_start;
 	return status;
 }
 
drivers/firmware/efi/libstub/zboot.c +18 -4
index 4a5ffe164873..4cff756902ad 100644
@@ -26,9 +26,18 @@ void __weak efi_cache_sync_image(unsigned long image_base,
 	// executable code loaded into memory to be safe for execution.
 }
 
-struct sysfb_display_info *alloc_primary_display(void)
+struct sysfb_display_info *
+alloc_primary_display(const struct efi_image_info *image_info)
 {
-	return __alloc_primary_display();
+	return efistub_sysfb_display_info(image_info);
+}
+
+static
+const struct efi_image_info *zboot_get_image_info(unsigned long image_base)
+{
+	extern const u32 efistub_image_info_offset;
+
+	return (void *)image_base + efistub_image_info_offset;
 }
 
 asmlinkage efi_status_t __efiapi
@@ -93,9 +102,14 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 	}
 
 	// Decompress the payload into the newly allocated buffer
-	status = efi_zboot_decompress((void *)image_base, alloc_size) ?:
-	         efi_stub_common(handle, image, image_base, cmdline_ptr);
+	status = efi_zboot_decompress((void *)image_base, alloc_size);
+	if (status != EFI_SUCCESS)
+		goto error;
+
+	auto image_info = zboot_get_image_info(image_base);
+	status = efi_stub_common(handle, image, image_info, cmdline_ptr);
 
+error:
 	efi_free(alloc_size, image_base);
 	return status;
 }
drivers/firmware/efi/libstub/zboot.lds +2 -0
index 367907eb7d86..e1365907f805 100644
@@ -24,6 +24,8 @@ SECTIONS
 		. = ALIGN(4);
 		__efistub_code_size = .;
 		LONG(zboot_code_size);
+		__efistub_efistub_image_info_offset = .;
+		LONG(efistub_image_info_offset);
 
 		_etext = ALIGN(4096);
 		. = _etext;
Signature · 1 line
2.56.0.611.g1030f0b940-goog
#11 lore raw

Re: [PATCH v3 06/15] efi/libstub: Capture loaded image information in struct efi_image_info

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] efi/libstub: broken kernel base address in efi_stub_common() on 32-bit ARM --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] efi/libstub: broken kernel base address in efi_stub_common() on 32-bit ARM
--

commit 10e52dbaf9d058d012ab6c3df52814b120d5f9e3
Author: Ard Biesheuvel <[email protected]>

efi/libstub: Capture loaded image information in struct efi_image_info

This commit introduces struct efi_image_info to hold loaded kernel image offsets and passes it to efi_stub_common() in place of the raw image base address.

Quoted 18 lines
drivers/firmware/efi/libstub/arm32-stub.c +4 -1
index 1073dd9475160..f7151a4915ba2 100644
@@ -76,12 +76,15 @@ void efi_handle_post_ebs_state(void)
 		      &efi_entry_state->sctlr_after_ebs);
 }
 
+struct efi_image_info efistub_image_info = {};
+
 efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 unsigned long *image_size,
 				 unsigned long *reserve_addr,
 				 unsigned long *reserve_size,
 				 efi_loaded_image_t *image,
-				 efi_handle_t image_handle)
+				 efi_handle_t image_handle,
+				 const struct efi_image_info **image_info)

[ ... ]

drivers/firmware/efi/libstub/efi-stub.c efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr) · line 167

Quoted 13 lines
drivers/firmware/efi/libstub/efi-stub.c +2 -1
index 6db10590f89b2..bf9a9f53a95cc 100644
@@ -160,9 +161,10 @@ efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr)
 
 efi_status_t efi_stub_common(efi_handle_t handle,
 			     efi_loaded_image_t *image,
-			     unsigned long image_addr,
+			     const struct efi_image_info *image_info,
 			     char *cmdline_ptr)
 {
+	unsigned long image_addr = efistub_kernel_base(image_info);

[Severity: High]
Does efistub_kernel_base(image_info) compute a bogus image_addr on 32-bit ARM?

In arm32-stub.c, efistub_image_info is zero-initialized and handle_kernel_image() writes kernel_base + TEXT_OFFSET only to *image_addr, which efi_pe_entry() no longer passes to efi_stub_common(). As a result, efistub_kernel_base(image_info) evaluates offset_to_ptr(&image_info->_text_offset) to &efistub_image_info inside the running zImage decompressor instead of kernel_base + TEXT_OFFSET. Passing that address to efi_get_max_initrd_addr() and efi_boot_kernel() -> efi_enter_kernel() causes the decompressor to overwrite itself and crash boot.

Even with the follow-up commit "efi/libstub: Pass kernel entrypoint down to efi_enter_kernel()" setting efistub_image_info._text_offset to the absolute address kernel_base + TEXT_OFFSET, offset_to_ptr(&image_info->_text_offset) still adds &image_info->_text_offset to that absolute address, passing a corrupted image_addr to efi_get_max_initrd_addr(image_addr).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Reviewed-by on v3 6/15
#12 lore raw

Re: [PATCH v3 06/15] efi/libstub: Capture loaded image information in struct efi_image_info

In reply to Ard Biesheuvel
Quoted 10 lines

[ ... 115 lines skipped ... ]

drivers/firmware/efi/libstub/arm32-stub.c +2 -0
@@ -76,12 +76,15 @@ void efi_handle_post_ebs_state(void)
 		      &efi_entry_state->sctlr_after_ebs);
 }
 
+struct efi_image_info efistub_image_info = {};
+
 efi_status_t handle_kernel_image(unsigned long *image_addr,

drivers/firmware/efi/libstub/arm32-stub.c void efi_handle_post_ebs_state(void) · line 90

Quoted 10 lines
drivers/firmware/efi/libstub/arm32-stub.c +2 -1
 				 unsigned long *image_size,
 				 unsigned long *reserve_addr,
 				 unsigned long *reserve_size,
 				 efi_loaded_image_t *image,
-				 efi_handle_t image_handle)
+				 efi_handle_t image_handle,
+				 const struct efi_image_info **image_info)
 {
 	const int slack = TEXT_OFFSET - 5 * PAGE_SIZE;
 	int alloc_size = MAX_UNCOMP_KERNEL_SIZE + EFI_PHYS_ALIGN;

The hunk from "efi/libstub: Pass kernel entrypoint down to efi_enter_kernel()" should probably go here, and Sashiko is right it needs a little fix:

*image_addr = kernel_base + TEXT_OFFSET;
efistub_image_info._text_offset =

	kernel_base - (unsigned long)&efistub_image_info._text_offset;
Quoted 21 lines

[ ... 3 lines skipped ... ]
+++ b/drivers/firmware/efi/libstub/arm64-stub.c

drivers/firmware/efi/libstub/arm32-stub.c +3 -3
@@ -19,9 +19,11 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 unsigned long *reserve_addr,
 				 unsigned long *reserve_size,
 				 efi_loaded_image_t *image,
-				 efi_handle_t image_handle)
+				 efi_handle_t image_handle,
+				 const struct efi_image_info **image_info)
 {
 	unsigned long kernel_size, kernel_codesize, kernel_memsize;
+	efi_status_t status;
 
 	if (image->image_base != _text) {
 		efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
@@ -38,9 +40,16 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	*reserve_size = kernel_memsize;
 	*image_addr = (unsigned long)_text;
 
-	return efi_kaslr_relocate_kernel(image_addr, reserve_addr, reserve_size,
-					 kernel_size, kernel_codesize, kernel_memsize,

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

Quoted 10 lines
drivers/firmware/efi/libstub/arm32-stub.c +9 -1
-					 efi_kaslr_get_phys_seed(image_handle));
+	status = efi_kaslr_relocate_kernel(image_addr, reserve_addr,
+					   reserve_size, kernel_size,
+					   kernel_codesize, kernel_memsize,
+					   efi_kaslr_get_phys_seed(image_handle));
+
+	if (status != EFI_SUCCESS)
+		return status;
+
+	*(unsigned long *)image_info += *image_addr - (unsigned long)_text;

A little helper inline to do this adjustment would be a nice touch.

Since it used to be done with

- kernel_image_offset = image_addr - (unsigned long)image->image_base;

Every arch can use this?
+ *(unsigned long *)image_info += *image_addr - (unsigned long)image->image_base;

Minor things, but this all looks fine to me and works easily for the DRTM series.

Signature · 1 line
Jason
#13 lore raw

PATCH v3 7/15 efi/libstub: Pass kernel entrypoint down to efi_enter_kernel()

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Update the EFI stub code shared by arm64 and RISC-V so that the kernel entry point is taken from struct efi_image_info rather than from opaque arithmetic performed on linker symbols.

Notably, this results in EFI zboot to enter via the actual kernel entrypoint rather than byte 0x0 of the loaded image on these architectures. Beyond that, it is mostly a cleanup that will allow special linker exports for global symbols to be dropped in subsequent patches.

ARM is a special case here, as it combines the EFI stub with its legacy decompressor, and so the generic image_info carrying direct references to vmlinux symbols cannot be used there. Instead, it provides an empty struct efi_image_info, which is passed directly to its implementation of efi_enter_kernel(). So on ARM, populate this efi_image_info with the base address that the callee expects.

---
 arch/arm/boot/compressed/head.S           |  2 +-
 drivers/firmware/efi/libstub/arm32-stub.c |  2 +-
 drivers/firmware/efi/libstub/arm64.c      | 16 ++--------------
 drivers/firmware/efi/libstub/efi-stub.c   |  2 +-
 drivers/firmware/efi/libstub/efistub.h    |  9 +++++----
 drivers/firmware/efi/libstub/fdt.c        |  5 +++--
 drivers/firmware/efi/libstub/loongarch.c  |  3 ++-
 drivers/firmware/efi/libstub/riscv.c      | 16 +++-------------
 8 files changed, 18 insertions(+), 37 deletions(-)
arch/arm/boot/compressed/head.S +1 -1
index 9f406e9c0ea6..a7f58dfe41bc 100644
@@ -1450,7 +1450,7 @@ __enter_kernel_from_hyp:
 		b	__enter_kernel
 
 ENTRY(efi_enter_kernel)
-		mov	r4, r0			@ preserve image base
+		ldr	r4, [r0]		@ preserve image base
 		mov	r8, r1			@ preserve DT pointer
 
 		adr_l	r0, call_cache_fn
drivers/firmware/efi/libstub/arm32-stub.c +1 -1
index f7151a4915ba..b73a0b4fcd10 100644
@@ -127,7 +127,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	efi_bs_call(free_pages, *reserve_addr + MAX_UNCOMP_KERNEL_SIZE,
 		    (alloc_size - MAX_UNCOMP_KERNEL_SIZE) / EFI_PAGE_SIZE);
 
-	*image_addr = kernel_base + TEXT_OFFSET;
+	*image_addr = efistub_image_info._text_offset = kernel_base + TEXT_OFFSET;
 	*image_size = 0;
 
 	efi_debug("image addr == 0x%lx, reserve_addr == 0x%lx\n",
drivers/firmware/efi/libstub/arm64.c +2 -14
index d35c6dd76601..e892302f4189 100644
@@ -117,24 +117,12 @@ void efi_cache_sync_image(unsigned long image_base,
 	efi_remap_image(image_base, alloc_size, code_size);
 }
 
-unsigned long __weak primary_entry_offset(void)
-{
-	/*
-	 * By default, we can invoke the kernel via the branch instruction in
-	 * the image header, so offset #0. This will be overridden by the EFI
-	 * stub build that is linked into the core kernel, as in that case, the
-	 * image header may not have been loaded into memory, or may be mapped
-	 * with non-executable permissions.
-	 */
-       return 0;
-}
-
-void __noreturn efi_enter_kernel(unsigned long entrypoint,
+void __noreturn efi_enter_kernel(const struct efi_image_info *image_info,
 				 unsigned long fdt_addr,
 				 unsigned long fdt_size)
 {
 	void (* __noreturn enter_kernel)(u64, u64, u64, u64);
 
-	enter_kernel = (void *)entrypoint + primary_entry_offset();
+	enter_kernel = efistub_kernel_entry(image_info);
 	enter_kernel(fdt_addr, 0, 0, 0);
 }
drivers/firmware/efi/libstub/efi-stub.c +1 -1
index bf9a9f53a95c..55bf7ed50c81 100644
@@ -191,7 +191,7 @@ efi_status_t efi_stub_common(efi_handle_t handle,
 
 	install_memreserve_table();
 
-	status = efi_boot_kernel(handle, image, image_addr, cmdline_ptr);
+	status = efi_boot_kernel(handle, image, image_info, cmdline_ptr);
 
 	free_primary_display(dpy);
 
drivers/firmware/efi/libstub/efistub.h +5 -4
index bb8e360fee7b..b985ababbe66 100644
@@ -1054,9 +1054,6 @@ typedef efi_status_t (*efi_exit_boot_map_processing)(
 efi_status_t efi_exit_boot_services(void *handle, void *priv,
 				    efi_exit_boot_map_processing priv_func);
 
-efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
-			     unsigned long kernel_addr, char *cmdline_ptr);
-
 void *get_fdt(unsigned long *fdt_size);
 
 efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap,
@@ -1189,6 +1186,10 @@ void *efistub_kernel_entry(const struct efi_image_info *image_info)
 	return offset_to_ptr(&image_info->entry_offset);
 }
 
+efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
+			     const struct efi_image_info *image_info,
+			     char *cmdline_ptr);
+
 static inline
 void *efistub_sysfb_display_info(const struct efi_image_info *image_info)
 {
@@ -1205,7 +1206,7 @@ efi_status_t efi_stub_common(efi_handle_t handle,
 
 efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr);
 
-asmlinkage void __noreturn efi_enter_kernel(unsigned long entrypoint,
+asmlinkage void __noreturn efi_enter_kernel(const struct efi_image_info *image_info,
 					    unsigned long fdt_addr,
 					    unsigned long fdt_size);
 
drivers/firmware/efi/libstub/fdt.c +3 -2
index f01450b49975..09e9e5bc831e 100644
@@ -344,7 +344,8 @@ efi_status_t allocate_new_fdt_and_exit_boot(void *handle,
 }
 
 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
-			     unsigned long kernel_addr, char *cmdline_ptr)
+			     const struct efi_image_info *image_info,
+			     char *cmdline_ptr)
 {
 	unsigned long fdt_addr;
 	efi_status_t status;
@@ -359,7 +360,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
 	if (IS_ENABLED(CONFIG_ARM))
 		efi_handle_post_ebs_state();
 
-	efi_enter_kernel(kernel_addr, fdt_addr, fdt_totalsize((void *)fdt_addr));
+	efi_enter_kernel(image_info, fdt_addr, fdt_totalsize((void *)fdt_addr));
 	/* not reached */
 }
 
drivers/firmware/efi/libstub/loongarch.c +2 -1
index 2b0c87dc9908..7f21e79384d3 100644
@@ -66,8 +66,9 @@ unsigned long __weak kernel_entry_address(unsigned long kernel_addr,
 }
 
 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
-			     unsigned long kernel_addr, char *cmdline_ptr)
+			     const struct efi_image_info *image_info, char *cmdline_ptr)
 {
+	unsigned long kernel_addr = efistub_kernel_base(image_info);
 	kernel_entry_t real_kernel_entry;
 	struct exit_boot_struct priv;
 	unsigned long desc_size;
drivers/firmware/efi/libstub/riscv.c +3 -13
index 49db47c05bfe..7be2774dea90 100644
@@ -72,20 +72,10 @@ efi_status_t check_platform_features(void)
 	return EFI_SUCCESS;
 }
 
-unsigned long __weak stext_offset(void)
+void __noreturn efi_enter_kernel(const struct efi_image_info *image_info,
+				 unsigned long fdt, unsigned long fdt_size)
 {
-	/*
-	 * This fallback definition is used by the EFI zboot stub, which loads
-	 * the entire image so it can branch via the image header at offset #0.
-	 */
-	return 0;
-}
-
-void __noreturn efi_enter_kernel(unsigned long entrypoint, unsigned long fdt,
-				 unsigned long fdt_size)
-{
-	unsigned long kernel_entry = entrypoint + stext_offset();
-	jump_kernel_func jump_kernel = (jump_kernel_func)kernel_entry;
+	jump_kernel_func jump_kernel = efistub_kernel_entry(image_info);
 
 	/*
 	 * Jump to real kernel here with following constraints.
Signature · 1 line
2.56.0.611.g1030f0b940-goog
#14 lore raw

Re: [PATCH v3 07/15] efi/libstub: Pass kernel entrypoint down to efi_enter_kernel()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] efi/libstub: bogus efistub_kernel_base() address on 32-bit ARM --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] efi/libstub: bogus efistub_kernel_base() address on 32-bit ARM --

commit 452448a07bb84c8f25cb8af4964ea6a4db8f2543
Author: Ard Biesheuvel <[email protected]>

efi/libstub: Pass kernel entrypoint down to efi_enter_kernel()

This commit passes struct efi_image_info down to efi_boot_kernel() and efi_enter_kernel() so that the kernel entrypoint is obtained from image_info rather than calculated from linker symbols.

Quoted 18 lines
arch/arm/boot/compressed/head.S +1 -1
index 9f406e9c0ea6f..a7f58dfe41bc3 100644
@@ -1450,7 +1450,7 @@ __enter_kernel_from_hyp:
 		b	__enter_kernel
 
 ENTRY(efi_enter_kernel)
-		mov	r4, r0			@ preserve image base
+		ldr	r4, [r0]		@ preserve image base
 		mov	r8, r1			@ preserve DT pointer
 
 		adr_l	r0, call_cache_fn
drivers/firmware/efi/libstub/arm32-stub.c +0 -0
index f7151a4915ba2..b73a0b4fcd101 100644

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

Quoted 8 lines
drivers/firmware/efi/libstub/arm32-stub.c +1 -1
@@ -127,7 +127,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	efi_bs_call(free_pages, *reserve_addr + MAX_UNCOMP_KERNEL_SIZE,
 		    (alloc_size - MAX_UNCOMP_KERNEL_SIZE) / EFI_PAGE_SIZE);
 
-	*image_addr = kernel_base + TEXT_OFFSET;
+	*image_addr = efistub_image_info._text_offset = kernel_base + TEXT_OFFSET;
 	*image_size = 0;
 
 	efi_debug("image addr == 0x%lx, reserve_addr == 0x%lx\n",

[Severity: High]
Does storing the absolute address in efistub_image_info._text_offset break efistub_kernel_base() on 32-bit ARM?

Before efi_enter_kernel() runs, efi_stub_common() derives image_addr via efistub_kernel_base(image_info) and passes it to efi_get_max_initrd_addr(image_addr):

drivers/firmware/efi/libstub/efistub.h:efistub_kernel_base() {
	return (unsigned long)offset_to_ptr(&image_info->_text_offset);
}
drivers/firmware/efi/libstub/efi-stub.c:efi_stub_common() {
	unsigned long image_addr = efistub_kernel_base(image_info);
	...
	efi_load_initrd(image, ULONG_MAX, efi_get_max_initrd_addr(image_addr),
			NULL);

Because offset_to_ptr() adds &image_info->_text_offset to the value stored in _text_offset, image_addr becomes the sum of two physical addresses. That corrupts the initrd allocation limit (round_down(image_addr, SZ_4M) + SZ_512M), either wrapping the 32-bit limit below DRAM so efi_load_initrd() fails, or shifting it past the 512 MiB lowmem window.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Reviewed-by on v3 7/15
#15 lore raw

Re: [PATCH v3 07/15] efi/libstub: Pass kernel entrypoint down to efi_enter_kernel()

In reply to Ard Biesheuvel
Quoted 10 lines

Update the EFI stub code shared by arm64 and RISC-V so that the kernel entry point is taken from struct efi_image_info rather than from opaque arithmetic performed on linker symbols.

Notably, this results in EFI zboot to enter via the actual kernel entrypoint rather than byte 0x0 of the loaded image on these architectures. Beyond that, it is mostly a cleanup that will allow special linker exports for global symbols to be dropped in subsequent patches.

Quoted 6 lines

ARM is a special case here, as it combines the EFI stub with its legacy decompressor, and so the generic image_info carrying direct references to vmlinux symbols cannot be used there. Instead, it provides an empty struct efi_image_info, which is passed directly to its implementation of efi_enter_kernel(). So on ARM, populate this efi_image_info with the base address that the callee expects.

Does this mean that the EFI stub never sees a decompressed image on ARM and jumps into the legacy decompressor?

Signature · 1 line
Jason
maintainer
#16 lore raw

Re: [PATCH v3 07/15] efi/libstub: Pass kernel entrypoint down to efi_enter_kernel()

Quoted 17 lines · Jason Gunthorpe
Quoted 10 lines

Update the EFI stub code shared by arm64 and RISC-V so that the kernel entry point is taken from struct efi_image_info rather than from opaque arithmetic performed on linker symbols.

Notably, this results in EFI zboot to enter via the actual kernel entrypoint rather than byte 0x0 of the loaded image on these architectures. Beyond that, it is mostly a cleanup that will allow special linker exports for global symbols to be dropped in subsequent patches.

Quoted 6 lines

ARM is a special case here, as it combines the EFI stub with its legacy decompressor, and so the generic image_info carrying direct references to vmlinux symbols cannot be used there. Instead, it provides an empty struct efi_image_info, which is passed directly to its implementation of efi_enter_kernel(). So on ARM, populate this efi_image_info with the base address that the callee expects.

Quoted 3 lines

Does this mean that the EFI stub never sees a decompressed image on ARM and jumps into the legacy decompressor?

Exactly. x86 used to do the same, but I fixed that a while ago for other reasons, so there this efi_image_info approach is still feasible as well.

Quoted 2 lines

Thanks.

#17 lore raw

PATCH v3 8/15 efi/libstub: arm64: Use struct efi_image_info to access image properties

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Instead of exposing individual symbols to the EFI stub explicitly via the linker script, use the generic struct efi_image_info, which accesses the original symbols via PC-relative references from the stub itself.

This improves code reuse across architectures, and makes it easier to add new references in a generic manner.

---
 arch/arm64/kernel/image-vars.h            | 10 ------
 drivers/firmware/efi/libstub/arm64-stub.c | 33 ++++++--------------
 2 files changed, 9 insertions(+), 34 deletions(-)
arch/arm64/kernel/image-vars.h +0 -10
index 19d602e45523..329d5e214b36 100644
@@ -20,8 +20,6 @@
 	PROVIDE(pisym = sym);		\
 	ASSERT((sym - KIMAGE_VADDR) < (__bss_start - KIMAGE_VADDR), #msg)
 
-PROVIDE(__efistub_primary_entry		= primary_entry);
-
 /*
  * The EFI stub has its own symbol namespace prefixed by __efistub_, to
  * isolate it from the kernel proper. The following symbols are legally
@@ -32,14 +30,6 @@ PROVIDE(__efistub_primary_entry		= primary_entry);
  * position independent manner
  */
 PROVIDE(__efistub_caches_clean_inval_pou = __pi_caches_clean_inval_pou);
-
-PROVIDE(__efistub__text			= _text);
-PROVIDE(__efistub__end			= _end);
-PROVIDE(__efistub___inittext_end       	= __inittext_end);
-PROVIDE(__efistub__edata		= _edata);
-#if defined(CONFIG_EFI_EARLYCON) || defined(CONFIG_SYSFB)
-PROVIDE(__efistub_sysfb_primary_display	= sysfb_primary_display);
-#endif
 PROVIDE(__efistub__ctype		= _ctype);
 
 PROVIDE(__pi___memcpy			= __pi_memcpy);
drivers/firmware/efi/libstub/arm64-stub.c +9 -24
index c93ab8b6e9d3..8ff8f8651504 100644
@@ -10,7 +10,6 @@
 #include <linux/efi.h>
 #include <asm/efi.h>
 #include <asm/memory.h>
-#include <asm/sections.h>
 
 #include "efistub.h"
 
@@ -22,23 +21,23 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 efi_handle_t image_handle,
 				 const struct efi_image_info **image_info)
 {
-	unsigned long kernel_size, kernel_codesize, kernel_memsize;
+	unsigned long kernel_base	= efistub_kernel_base(*image_info);
+	unsigned long kernel_size	= efistub_kernel_size(*image_info);
+	unsigned long kernel_codesize	= efistub_kernel_codesize(*image_info);
+	unsigned long kernel_memsize	= efistub_kernel_memsize(*image_info);
 	efi_status_t status;
 
-	if (image->image_base != _text) {
+	if (image->image_base != (void *)kernel_base) {
 		efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
-		image->image_base = _text;
+		image->image_base = (void *)kernel_base;
 	}
 
-	if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN))
+	if (!IS_ALIGNED(kernel_base, SEGMENT_ALIGN))
 		efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
 			SEGMENT_ALIGN >> 10);
 
-	kernel_size = _edata - _text;
-	kernel_codesize = __inittext_end - _text;
-	kernel_memsize = kernel_size + (_end - _edata);
 	*reserve_size = kernel_memsize;
-	*image_addr = (unsigned long)_text;
+	*image_addr = kernel_base;
 
 	status = efi_kaslr_relocate_kernel(image_addr, reserve_addr,
 					   reserve_size, kernel_size,
@@ -48,24 +47,10 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	if (status != EFI_SUCCESS)
 		return status;
 
-	*(unsigned long *)image_info += *image_addr - (unsigned long)_text;
+	*(unsigned long *)image_info += *image_addr - kernel_base;
 	return EFI_SUCCESS;
 }
 
-asmlinkage void primary_entry(void);
-
-unsigned long primary_entry_offset(void)
-{
-	/*
-	 * When built as part of the kernel, the EFI stub cannot branch to the
-	 * kernel proper via the image header, as the PE/COFF header is
-	 * strictly not part of the in-memory presentation of the image, only
-	 * of the file representation. So instead, we need to jump to the
-	 * actual entrypoint in the .text region of the image.
-	 */
-	return (char *)primary_entry - _text;
-}
-
 void efi_icache_sync(unsigned long start, unsigned long end)
 {
 	caches_clean_inval_pou(start, end);
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Reviewed-by on v3 8/15
#18 lore raw

Re: [PATCH v3 08/15] efi/libstub: arm64: Use struct efi_image_info to access image properties

Quoted 23 lines

[ ... 53 lines skipped ... ]

drivers/firmware/efi/libstub/arm64-stub.c +7 -4
@@ -22,23 +21,23 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 efi_handle_t image_handle,
 				 const struct efi_image_info **image_info)
 {
-	unsigned long kernel_size, kernel_codesize, kernel_memsize;
+	unsigned long kernel_base	= efistub_kernel_base(*image_info);
+	unsigned long kernel_size	= efistub_kernel_size(*image_info);
+	unsigned long kernel_codesize	= efistub_kernel_codesize(*image_info);
+	unsigned long kernel_memsize	= efistub_kernel_memsize(*image_info);
 	efi_status_t status;
 
-	if (image->image_base != _text) {
+	if (image->image_base != (void *)kernel_base) {
 		efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
-		image->image_base = _text;
+		image->image_base = (void *)kernel_base;
 	}
 
-	if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN))
+	if (!IS_ALIGNED(kernel_base, SEGMENT_ALIGN))
 		efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
 			SEGMENT_ALIGN >> 10);

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

Quoted 10 lines
drivers/firmware/efi/libstub/arm64-stub.c +1 -4
 
-	kernel_size = _edata - _text;
-	kernel_codesize = __inittext_end - _text;
-	kernel_memsize = kernel_size + (_end - _edata);
 	*reserve_size = kernel_memsize;
-	*image_addr = (unsigned long)_text;
+	*image_addr = kernel_base;
 
 	status = efi_kaslr_relocate_kernel(image_addr, reserve_addr,
 					   reserve_size, kernel_size,

There is a further consolidation here, the two places that call efi_kaslr_relocate_kernel() get the args from image_info, so we could just pass image_info directly and remove the duplicated extraction and extra parameters.

It could then handle the image_info adjustment too instead of open coding it twice.

I wonder if riscv should have the segment_align and image_base checks too? Maybe the whole function is just a call to efi_kaslr_relocate_kernel() ?

Quoted 4 lines

[ ... 9 lines skipped ... ]

drivers/firmware/efi/libstub/arm64-stub.c +0 -3
-asmlinkage void primary_entry(void);
-
-unsigned long primary_entry_offset(void)

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

Quoted 10 lines
drivers/firmware/efi/libstub/arm64-stub.c +0 -10
-{
-	/*
-	 * When built as part of the kernel, the EFI stub cannot branch to the
-	 * kernel proper via the image header, as the PE/COFF header is
-	 * strictly not part of the in-memory presentation of the image, only
-	 * of the file representation. So instead, we need to jump to the
-	 * actual entrypoint in the .text region of the image.
-	 */
-	return (char *)primary_entry - _text;
-}

I think this hunk belongs to the previous patch which removed the caller?

Anyhow, the logic looks fine to me

Signature · 1 line
Jason
maintainer
#19 lore raw

Re: [PATCH v3 08/15] efi/libstub: arm64: Use struct efi_image_info to access image properties

Quoted 39 lines · Jason Gunthorpe
Quoted 23 lines

[ ... 53 lines skipped ... ]

drivers/firmware/efi/libstub/arm64-stub.c +7 -4
@@ -22,23 +21,23 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 efi_handle_t image_handle,
 				 const struct efi_image_info **image_info)
 {
-	unsigned long kernel_size, kernel_codesize, kernel_memsize;
+	unsigned long kernel_base	= efistub_kernel_base(*image_info);
+	unsigned long kernel_size	= efistub_kernel_size(*image_info);
+	unsigned long kernel_codesize	= efistub_kernel_codesize(*image_info);
+	unsigned long kernel_memsize	= efistub_kernel_memsize(*image_info);
 	efi_status_t status;
 
-	if (image->image_base != _text) {
+	if (image->image_base != (void *)kernel_base) {
 		efi_err("FIRMWARE BUG: efi_loaded_image_t::image_base has bogus value\n");
-		image->image_base = _text;
+		image->image_base = (void *)kernel_base;
 	}
 
-	if (!IS_ALIGNED((u64)_text, SEGMENT_ALIGN))
+	if (!IS_ALIGNED(kernel_base, SEGMENT_ALIGN))
 		efi_err("FIRMWARE BUG: kernel image not aligned on %dk boundary\n",
 			SEGMENT_ALIGN >> 10);

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

Quoted 10 lines
drivers/firmware/efi/libstub/arm64-stub.c +1 -4
 
-	kernel_size = _edata - _text;
-	kernel_codesize = __inittext_end - _text;
-	kernel_memsize = kernel_size + (_end - _edata);
 	*reserve_size = kernel_memsize;
-	*image_addr = (unsigned long)_text;
+	*image_addr = kernel_base;
 
 	status = efi_kaslr_relocate_kernel(image_addr, reserve_addr,
 					   reserve_size, kernel_size,

There is a further consolidation here, the two places that call efi_kaslr_relocate_kernel() get the args from image_info, so we could just pass image_info directly and remove the duplicated extraction and extra parameters.

Quoted 3 lines

It could then handle the image_info adjustment too instead of open coding it twice.

Indeed.

Quoted 4 lines

I wonder if riscv should have the segment_align and image_base checks too? Maybe the whole function is just a call to efi_kaslr_relocate_kernel() ?

IIRC they were added specifically for RedHat's spectacularly buggy GRUB fork at the the time, so maybe by now we can just remove those.

Quoted 9 lines
Quoted 9 lines

[ ... 9 lines skipped ... ]

drivers/firmware/efi/libstub/arm64-stub.c +0 -8
-asmlinkage void primary_entry(void);
-
-unsigned long primary_entry_offset(void)
-{
-	/*
-	 * When built as part of the kernel, the EFI stub cannot branch to the
-	 * kernel proper via the image header, as the PE/COFF header is
-	 * strictly not part of the in-memory presentation of the image, only
Quoted 9 lines

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

Quoted 5 lines
drivers/firmware/efi/libstub/arm64-stub.c +0 -5
-	 * of the file representation. So instead, we need to jump to the
-	 * actual entrypoint in the .text region of the image.
-	 */
-	return (char *)primary_entry - _text;
-}

I think this hunk belongs to the previous patch which removed the caller?

Indeed.

Quoted 4 lines

Anyhow, the logic looks fine to me

Thanks.

#20 lore raw

PATCH v3 9/15 efi/libstub: loongarch: Use struct efi_image_info to access image properties

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Instead of exposing individual symbols to the EFI stub explicitly via the linker script, use the generic struct efi_image_info, which accesses the original symbols via PC-relative references from the stub itself.

This improves code reuse across architectures, and makes it easier to add new references in a generic manner.

---
 arch/loongarch/kernel/image-vars.h            | 4 ----
 drivers/firmware/efi/libstub/loongarch-stub.c | 8 --------
 drivers/firmware/efi/libstub/loongarch.c      | 9 +--------
 3 files changed, 1 insertion(+), 20 deletions(-)
arch/loongarch/kernel/image-vars.h +0 -4
index e557ebd46c2b..b01022ce6bb8 100644
@@ -8,12 +8,8 @@
 #ifdef CONFIG_EFI_STUB
 
 __efistub_strcmp		= strcmp;
-__efistub_kernel_entry		= kernel_entry;
 __efistub_kernel_asize		= kernel_asize;
 __efistub_kernel_fsize		= kernel_fsize;
-#if defined(CONFIG_EFI_EARLYCON) || defined(CONFIG_SYSFB)
-__efistub_sysfb_primary_display	= sysfb_primary_display;
-#endif
 
 #endif
 
drivers/firmware/efi/libstub/loongarch-stub.c +0 -8
index d57e3d3557a5..6126b05e99fb 100644
@@ -116,11 +116,3 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	*(unsigned long *)image_info += *image_addr - (unsigned long)image->image_base;
 	return status;
 }
-
-unsigned long kernel_entry_address(unsigned long kernel_addr,
-		efi_loaded_image_t *image)
-{
-	unsigned long base = (unsigned long)image->image_base;
-
-	return (unsigned long)&kernel_entry - base + kernel_addr;
-}
drivers/firmware/efi/libstub/loongarch.c +1 -8
index 7f21e79384d3..dd4bdf4b08cc 100644
@@ -59,16 +59,9 @@ static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)
 	return EFI_SUCCESS;
 }
 
-unsigned long __weak kernel_entry_address(unsigned long kernel_addr,
-		efi_loaded_image_t *image)
-{
-	return *(unsigned long *)(kernel_addr + 8) - PHYSADDR(VMLINUX_LOAD_ADDRESS) + kernel_addr;
-}
-
 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
 			     const struct efi_image_info *image_info, char *cmdline_ptr)
 {
-	unsigned long kernel_addr = efistub_kernel_base(image_info);
 	kernel_entry_t real_kernel_entry;
 	struct exit_boot_struct priv;
 	unsigned long desc_size;
@@ -99,7 +92,7 @@ efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image,
 	csr_write(CSR_DMW2_INIT, LOONGARCH_CSR_DMWIN2);
 	csr_write(CSR_DMW3_INIT, LOONGARCH_CSR_DMWIN3);
 
-	real_kernel_entry = (void *)kernel_entry_address(kernel_addr, image);
+	real_kernel_entry = efistub_kernel_entry(image_info);
 
 	real_kernel_entry(true, (unsigned long)cmdline_ptr,
 			  (unsigned long)efi_system_table);
Signature · 1 line
2.56.0.611.g1030f0b940-goog
#21 lore raw

Re: [PATCH v3 09/15] efi/libstub: loongarch: Use struct efi_image_info to access image properties

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] efi/libstub: loongarch: dead declarations left after image_info conversion --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] efi/libstub: loongarch: dead declarations left after image_info conversion
--

commit d80068564b9cb3b54d5e07eb729d7302295ab088
Author: Ard Biesheuvel <[email protected]>

efi/libstub: loongarch: Use struct efi_image_info to access image properties

This commit switches the LoongArch EFI stub to resolve the kernel entry point through struct efi_image_info and drops kernel_entry_address() and unused linker script symbol aliases.

Quoted 23 lines
arch/loongarch/kernel/image-vars.h +0 -4
index e557ebd46c2b3..b01022ce6bb8a 100644
@@ -8,12 +8,8 @@
 #ifdef CONFIG_EFI_STUB
 
 __efistub_strcmp		= strcmp;
-__efistub_kernel_entry		= kernel_entry;
 __efistub_kernel_asize		= kernel_asize;
 __efistub_kernel_fsize		= kernel_fsize;
-#if defined(CONFIG_EFI_EARLYCON) || defined(CONFIG_SYSFB)
-__efistub_sysfb_primary_display	= sysfb_primary_display;
-#endif
 
 #endif
 
drivers/firmware/efi/libstub/loongarch-stub.c +0 -0
index d57e3d3557a54..6126b05e99fbe 100644
@@ -116,11 +116,3 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	*(unsigned long *)image_info += *image_addr - (unsigned long)image->image_base;

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

Quoted 10 lines
drivers/firmware/efi/libstub/loongarch-stub.c +0 -8
 	return status;
 }
-
-unsigned long kernel_entry_address(unsigned long kernel_addr,
-		efi_loaded_image_t *image)
-{
-	unsigned long base = (unsigned long)image->image_base;
-
-	return (unsigned long)&kernel_entry - base + kernel_addr;
-}

[Severity: Low]
This isn't a bug, but removing kernel_entry_address() and __efistub_kernel_entry leaves behind "extern int kernel_entry;" in drivers/firmware/efi/libstub/loongarch-stub.c as well as drivers/firmware/efi/libstub/loongarch-stub.h (which only declares kernel_entry_address()) and its #include directives in loongarch-stub.c and loongarch.c.

Also, should handle_kernel_image() use efistub_kernel_size(*image_info) and efistub_kernel_memsize(*image_info) so that __efistub_kernel_fsize and __efistub_kernel_asize can be dropped from arch/loongarch/kernel/image-vars.h as well?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Reviewed-by on v3 9/15
#22 lore raw

Re: [PATCH v3 09/15] efi/libstub: loongarch: Use struct efi_image_info to access image properties

In reply to Ard Biesheuvel
Quoted 8 lines · Ard Biesheuvel

Instead of exposing individual symbols to the EFI stub explicitly via the linker script, use the generic struct efi_image_info, which accesses the original symbols via PC-relative references from the stub itself.

This improves code reuse across architectures, and makes it easier to add new references in a generic manner.

[...]

Signature · 6 lines
Jason

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#23 lore raw

PATCH v3 10/15 efi/libstub: riscv: Use struct efi_image_info to access image properties

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Instead of exposing individual symbols to the EFI stub explicitly via the linker script, use the generic struct efi_image_info, which accesses the original symbols via PC-relative references from the stub itself.

This improves code reuse across architectures, and makes it easier to add new references in a generic manner.

---
 arch/riscv/kernel/image-vars.h            |  8 -------
 drivers/firmware/efi/libstub/riscv-stub.c | 25 +++++---------------
 2 files changed, 6 insertions(+), 27 deletions(-)
arch/riscv/kernel/image-vars.h +0 -8
index bf703eba3fa5..c18b24792a29 100644
@@ -23,14 +23,6 @@
  * linked at. The routines below are all implemented in assembler in a
  * position independent manner
  */
-__efistub__start		= _start;
-__efistub__start_kernel		= _start_kernel;
-__efistub__end			= _end;
-__efistub__edata		= _edata;
-__efistub___inittext_end	= __inittext_end;
-#if defined(CONFIG_EFI_EARLYCON) || defined(CONFIG_SYSFB)
-__efistub_sysfb_primary_display	= sysfb_primary_display;
-#endif
 
 /*
  * These double-word integer shifts are used by the library code, and
drivers/firmware/efi/libstub/riscv-stub.c +6 -19
index 0ddbe7cc8628..58581ccb4cc0 100644
@@ -6,23 +6,10 @@
 #include <linux/efi.h>
 
 #include <asm/efi.h>
-#include <asm/sections.h>
 #include <linux/unaligned.h>
 
 #include "efistub.h"
 
-unsigned long stext_offset(void)
-{
-	/*
-	 * When built as part of the kernel, the EFI stub cannot branch to the
-	 * kernel proper via the image header, as the PE/COFF header is
-	 * strictly not part of the in-memory presentation of the image, only
-	 * of the file representation. So instead, we need to jump to the
-	 * actual entrypoint in the .text region of the image.
-	 */
-	return _start_kernel - _start;
-}
-
 efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 unsigned long *image_size,
 				 unsigned long *reserve_addr,
@@ -31,13 +18,13 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 				 efi_handle_t image_handle,
 				 const struct efi_image_info **image_info)
 {
-	unsigned long kernel_size, kernel_codesize, kernel_memsize;
+	unsigned long kernel_base	= efistub_kernel_base(*image_info);
+	unsigned long kernel_size	= efistub_kernel_size(*image_info);
+	unsigned long kernel_codesize	= efistub_kernel_codesize(*image_info);
+	unsigned long kernel_memsize	= efistub_kernel_memsize(*image_info);
 	efi_status_t status;
 
-	kernel_size = _edata - _start;
-	kernel_codesize = __inittext_end - _start;
-	kernel_memsize = kernel_size + (_end - _edata);
-	*image_addr = (unsigned long)_start;
+	*image_addr = kernel_base;
 	*reserve_size = kernel_memsize;
 
 	status = efi_kaslr_relocate_kernel(image_addr,
@@ -47,7 +34,7 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
 	if (status != EFI_SUCCESS)
 		efi_err("Failed to relocate kernel\n");
 
-	*(unsigned long *)image_info += *image_addr - (unsigned long)_start;
+	*(unsigned long *)image_info += *image_addr - kernel_base;
 	return status;
 }
 
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Reviewed-by on v3 10/15
#24 lore raw

Re: [PATCH v3 10/15] efi/libstub: riscv: Use struct efi_image_info to access image properties

Quoted 8 lines · Ard Biesheuvel

Instead of exposing individual symbols to the EFI stub explicitly via the linker script, use the generic struct efi_image_info, which accesses the original symbols via PC-relative references from the stub itself.

This improves code reuse across architectures, and makes it easier to add new references in a generic manner.

[...]

Signature · 1 line
Jason
#25 lore raw

PATCH v3 11/15 efi/libstub: Make efi_remap_image() return efi_status_t

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

efi_remap_image() uses the EFI Memory Attributes Protocol to remap code and data regions with the appropriate permissions. This could fail for a variety of regions, all of which are currently ignored.

So relay the failure status code if any occur when invoking the protocol. Locating the protocol itself is allowed to fail, as remapping the image is likely unnecessary in the first place if the protocol does not exist.

---
 drivers/firmware/efi/libstub/efi-stub-helper.c | 15 ++++++++++-----
 drivers/firmware/efi/libstub/efistub.h         |  4 ++--
 2 files changed, 12 insertions(+), 7 deletions(-)
drivers/firmware/efi/libstub/efi-stub-helper.c +10 -5
index 8e43eb3f418b..e50deec99686 100644
@@ -697,9 +697,11 @@ efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)
  * region of the loaded image read-only/executable, and the remainder
  * read-write/non-executable. The code region is assumed to start at the base
  * of the image, and will therefore cover the PE/COFF header as well.
+ *
+ * Return: EFI_SUCCESS on success, other EFI status codes on failure.
  */
-void efi_remap_image(unsigned long image_base, unsigned alloc_size,
-		     unsigned long code_size)
+efi_status_t efi_remap_image(unsigned long image_base, unsigned alloc_size,
+			     unsigned long code_size)
 {
 	static efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
 	efi_memory_attribute_protocol_t *memattr;
@@ -710,10 +712,12 @@ void efi_remap_image(unsigned long image_base, unsigned alloc_size,
 	 * If the firmware implements the EFI_MEMORY_ATTRIBUTE_PROTOCOL, let's
 	 * invoke it to remap the text/rodata region of the decompressed image
 	 * as read-only and the data/bss region as non-executable.
+	 * Don't bother if the protocol does not exist: it means all memory is
+	 * read-write-exec anyway.
 	 */
 	status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
 	if (status != EFI_SUCCESS)
-		return;
+		return EFI_SUCCESS;
 
 	// Get the current attributes for the entire region
 	status = memattr->get_memory_attributes(memattr, image_base,
@@ -721,7 +725,7 @@ void efi_remap_image(unsigned long image_base, unsigned alloc_size,
 	if (status != EFI_SUCCESS) {
 		efi_warn("Failed to retrieve memory attributes for image region: 0x%lx\n",
 			 status);
-		return;
+		return status;
 	}
 
 	// Mark the code region as read-only
@@ -729,7 +733,7 @@ void efi_remap_image(unsigned long image_base, unsigned alloc_size,
 						EFI_MEMORY_RO);
 	if (status != EFI_SUCCESS) {
 		efi_warn("Failed to remap code region read-only\n");
-		return;
+		return status;
 	}
 
 	// If the entire region was already mapped as non-exec, clear the
@@ -749,4 +753,5 @@ void efi_remap_image(unsigned long image_base, unsigned alloc_size,
 		if (status != EFI_SUCCESS)
 			efi_warn("Failed to remap data region non-executable\n");
 	}
+	return status;
 }
drivers/firmware/efi/libstub/efistub.h +2 -2
index b985ababbe66..634e52aa6dcc 100644
@@ -1293,8 +1293,8 @@ struct efi_smbios_type4_record {
 const u8 *__efi_get_smbios_string(const struct efi_smbios_record *record,
 				  const u8 *offset);
 
-void efi_remap_image(unsigned long image_base, unsigned alloc_size,
-		     unsigned long code_size);
+efi_status_t efi_remap_image(unsigned long image_base, unsigned alloc_size,
+			     unsigned long code_size);
 efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr,
 				       unsigned long *reserve_addr,
 				       unsigned long *reserve_size,
Signature · 1 line
2.56.0.611.g1030f0b940-goog
#26 lore raw

PATCH v3 12/15 efi/zboot: Disentangle efi_remap_image() from efi_cache_sync_image()

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Refactor efi_cache_sync_image() so that it is not in charge of calling efi_remap_image(), which is really the wrong way around.

Instead, call efi_remap_image() from the primary zboot procedure, where it is more straight-forward to obtain the code size from the loaded image.

---
 drivers/firmware/efi/libstub/arm64.c                 |  2 --
 drivers/firmware/efi/libstub/zboot-decompress-gzip.c |  2 --
 drivers/firmware/efi/libstub/zboot-decompress-zstd.c |  2 --
 drivers/firmware/efi/libstub/zboot.c                 | 14 +++++++++++++-
 4 files changed, 13 insertions(+), 7 deletions(-)
drivers/firmware/efi/libstub/arm64.c +0 -2
index e892302f4189..f8bb890b9631 100644
@@ -113,8 +113,6 @@ void efi_cache_sync_image(unsigned long image_base,
 	asm("ic ialluis");
 	dsb(ish);
 	isb();
-
-	efi_remap_image(image_base, alloc_size, code_size);
 }
 
 void __noreturn efi_enter_kernel(const struct efi_image_info *image_info,
drivers/firmware/efi/libstub/zboot-decompress-gzip.c +0 -2
index e97a7e9d3c98..79cf8c48b033 100644
@@ -62,7 +62,5 @@ efi_status_t efi_zboot_decompress(u8 *out, unsigned long outlen)
 		return EFI_LOAD_ERROR;
 	}
 
-	efi_cache_sync_image((unsigned long)out, outlen);
-
 	return EFI_SUCCESS;
 }
drivers/firmware/efi/libstub/zboot-decompress-zstd.c +0 -2
index bde9d94dd2e3..dd7f11238154 100644
@@ -43,7 +43,5 @@ efi_status_t efi_zboot_decompress(u8 *out, unsigned long outlen)
 		return EFI_LOAD_ERROR;
 	}
 
-	efi_cache_sync_image((unsigned long)out, outlen);
-
 	return EFI_SUCCESS;
 }
drivers/firmware/efi/libstub/zboot.c +13 -1
index 4cff756902ad..9c7de225b39e 100644
@@ -40,6 +40,17 @@ const struct efi_image_info *zboot_get_image_info(unsigned long image_base)
 	return (void *)image_base + efistub_image_info_offset;
 }
 
+static efi_status_t efi_zboot_remap_image(const struct efi_image_info *image_info,
+					  unsigned long image_base,
+					  unsigned long image_size)
+{
+	unsigned long code_size = efistub_kernel_codesize(image_info);
+
+	efi_cache_sync_image(image_base, code_size);
+
+	return efi_remap_image(image_base, image_size, code_size);
+}
+
 asmlinkage efi_status_t __efiapi
 efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 {
@@ -107,7 +118,8 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 		goto error;
 
 	auto image_info = zboot_get_image_info(image_base);
-	status = efi_stub_common(handle, image, image_info, cmdline_ptr);
+	status = efi_zboot_remap_image(image_info, image_base, alloc_size) ?:
+		 efi_stub_common(handle, image, image_info, cmdline_ptr);
 
 error:
 	efi_free(alloc_size, image_base);
Signature · 1 line
2.56.0.611.g1030f0b940-goog
#27 lore raw

Re: [PATCH v3 12/15] efi/zboot: Disentangle efi_remap_image() from efi_cache_sync_image()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] efi/zboot: unaligned code_size in efi_zboot_remap_image() on RISC-V --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] efi/zboot: unaligned code_size in efi_zboot_remap_image() on RISC-V
--

commit f296d871a4059f81fdcf9e8fdf0200c1435b74e5
Author: Ard Biesheuvel <[email protected]>

efi/zboot: Disentangle efi_remap_image() from efi_cache_sync_image()

This commit moves the efi_cache_sync_image() and efi_remap_image() calls out of the decompressors and arm64's cache sync helper into efi_zboot_entry() via a new efi_zboot_remap_image() helper.

Quoted 23 lines
drivers/firmware/efi/libstub/zboot.c +11 -0
index 4cff756902ad4..9c7de225b39e5 100644
@@ -40,6 +40,17 @@ const struct efi_image_info *zboot_get_image_info(unsigned long image_base)
 	return (void *)image_base + efistub_image_info_offset;
 }
 
+static efi_status_t efi_zboot_remap_image(const struct efi_image_info *image_info,
+					  unsigned long image_base,
+					  unsigned long image_size)
+{
+	unsigned long code_size = efistub_kernel_codesize(image_info);
+
+	efi_cache_sync_image(image_base, code_size);
+
+	return efi_remap_image(image_base, image_size, code_size);
+}
+
 asmlinkage efi_status_t __efiapi
 efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 {

drivers/firmware/efi/libstub/zboot.c efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab) · line 122

Quoted 6 lines
drivers/firmware/efi/libstub/zboot.c +2 -1
@@ -107,7 +118,8 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab)
 		goto error;
 
 	auto image_info = zboot_get_image_info(image_base);
-	status = efi_stub_common(handle, image, image_info, cmdline_ptr);
+	status = efi_zboot_remap_image(image_info, image_base, alloc_size) ?:
+		 efi_stub_common(handle, image, image_info, cmdline_ptr);

[Severity: High]
Can efi_zboot_remap_image() pass a non-page-aligned code_size to efi_remap_image() on RISC-V and abort the boot?

Previously, efi_remap_image() was only called from arm64's efi_cache_sync_image() implementation. Calling it from efi_zboot_entry() now runs it on all zboot architectures and aborts the boot if it returns an error.

efistub_kernel_codesize() returns __inittext_end - _text. Unlike arm64 and LoongArch, RISC-V places __inittext_end before the section and PE/COFF alignment directives:

arch/riscv/kernel/vmlinux.lds.S:

	__inittext_end = .;
	. = ALIGN(SECTION_ALIGN);
#ifdef CONFIG_EFI
	. = ALIGN(PECOFF_SECTION_ALIGNMENT);
	__pecoff_text_end = .;

#endif

Because __inittext_end is only instruction-aligned on RISC-V, code_size (along with image_base + code_size and alloc_size - code_size) is not a multiple of EFI_PAGE_SIZE (4 KiB). On UEFI firmware implementing EFI_MEMORY_ATTRIBUTE_PROTOCOL, set_memory_attributes() and clear_memory_attributes() reject the unaligned range with EFI_INVALID_PARAMETER, causing efi_zboot_entry() to skip efi_stub_common() and fail the boot.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#28 lore raw

PATCH v3 13/15 efi/libstub: arm64: Drop zboot code_size hack

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

Now that the code size can be obtained via the generic image_info struct, there is no longer a need to inject it explicitly during the build.

---
 arch/arm64/boot/Makefile               | 3 ---
 arch/arm64/kernel/image-vars.h         | 4 ----
 drivers/firmware/efi/libstub/arm64.c   | 4 +---
 drivers/firmware/efi/libstub/zboot.lds | 4 ----
 4 files changed, 1 insertion(+), 14 deletions(-)
arch/arm64/boot/Makefile +0 -3
index d42d07667822..509b59518818 100644
@@ -51,7 +51,4 @@ EFI_ZBOOT_BFD_TARGET	:= elf64-littleaarch64
 EFI_ZBOOT_MACH_TYPE	:= ARM64
 EFI_ZBOOT_FORWARD_CFI	:= $(CONFIG_ARM64_BTI_KERNEL)
 
-EFI_ZBOOT_OBJCOPY_FLAGS	= --add-symbol zboot_code_size=0x$$( \
-				$(NM) vmlinux|grep _kernel_codesize|cut -d' ' -f1)
-
 include $(srctree)/drivers/firmware/efi/libstub/Makefile.zboot
arch/arm64/kernel/image-vars.h +0 -4
index 329d5e214b36..ca634cedcf63 100644
@@ -140,10 +140,6 @@ KVM_NVHE_ALIAS(kvm_protected_mode_initialized);
 
 #endif /* CONFIG_KVM */
 
-#ifdef CONFIG_EFI_ZBOOT
-_kernel_codesize = ABSOLUTE(__inittext_end - _text);
-#endif
-
 /*
  * LLD will occasionally error out with a '__init_end does not converge' error
  * if INIT_IDMAP_DIR_SIZE is defined in terms of _end, as this results in a
drivers/firmware/efi/libstub/arm64.c +1 -3
index f8bb890b9631..8a051412814d 100644
@@ -88,8 +88,6 @@ efi_status_t check_platform_features(void)
 #define DCTYPE	"cvau"
 #endif
 
-u32 __weak code_size;
-
 void efi_cache_sync_image(unsigned long image_base,
 			  unsigned long alloc_size)
 {
@@ -100,7 +98,7 @@ void efi_cache_sync_image(unsigned long image_base,
 	/* only perform the cache maintenance if needed for I/D coherency */
 	if (!(ctr & BIT(CTR_EL0_IDC_SHIFT))) {
 		unsigned long base = image_base;
-		unsigned long size = code_size;
+		unsigned long size = alloc_size;
 
 		do {
 			asm("dc " DCTYPE ", %0" :: "r"(base));
drivers/firmware/efi/libstub/zboot.lds +0 -4
index e1365907f805..e0f0bf005854 100644
@@ -2,8 +2,6 @@
 
 ENTRY(__efistub_efi_zboot_header);
 
-PROVIDE(zboot_code_size = ABSOLUTE(0));
-
 SECTIONS
 {
 	.head : ALIGN(4096) {
@@ -22,8 +20,6 @@ SECTIONS
 		*(.rodata* .init.rodata* .srodata*)
 
 		. = ALIGN(4);
-		__efistub_code_size = .;
-		LONG(zboot_code_size);
 		__efistub_efistub_image_info_offset = .;
 		LONG(efistub_image_info_offset);
 
Signature · 1 line
2.56.0.611.g1030f0b940-goog
#29 lore raw

PATCH v3 14/15 efi: Make config table based primary display handover ARM-only again

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

This reverts the effect of commit 732ea9db9d8a ("efi: libstub: Move screen_info handling to common code"), taking into account the changes that were made to the code since.

That commit moved all architectures to the ARM approach of passing the primary display information (formerly struct screen_info) from the EFI stub to the core kernel via an EFI configuration table, so that the zboot decompressor, which is not linked into the core kernel image, could pass this information as well.

Now that the EFI stub and the zboot decompressor both have access to the location of sysfb_primary_display inside the loaded kernel image via struct efi_image_info, they can populate it directly, and the configuration table is only needed on ARM, whose decompressor has no such information.

So make the configuration table based handover ARM-only again:
- move the config table based alloc/free routines into the ARM stub, and provide __weak defaults in the generic stub that return a pointer to the kernel image's copy of sysfb_primary_display;
- drop the primary display table from the common config table list, and move the code that copies it into sysfb_primary_display into the ARM arch code;
- on the other architectures, revert to simply reserving the framebuffer memory if the stub populated sysfb_primary_display.

This makes the EFI framebuffer earlycon available right away when booting via zboot, rather than only after the EFI config tables have been parsed.

---
 arch/arm/kernel/efi.c                          | 27 ++++++++++
 arch/loongarch/kernel/efi.c                    | 20 ++-----
 drivers/firmware/efi/efi-init.c                | 24 ++-------
 drivers/firmware/efi/efi.c                     |  5 --
 drivers/firmware/efi/libstub/Makefile          |  2 +-
 drivers/firmware/efi/libstub/arm32-stub.c      | 41 ++++++++++++++
 drivers/firmware/efi/libstub/efi-stub-entry.c  | 13 -----
 drivers/firmware/efi/libstub/efi-stub.c        | 14 +++++
 drivers/firmware/efi/libstub/efistub.h         |  1 -
 drivers/firmware/efi/libstub/primary_display.c | 56 --------------------
 drivers/firmware/efi/libstub/zboot.c           |  6 ---
 11 files changed, 90 insertions(+), 119 deletions(-)
arch/arm/kernel/efi.c +27 -0
index c14275257a8a..84d516b84848 100644
@@ -7,6 +7,7 @@
 #include <linux/memblock.h>
 #include <linux/screen_info.h>
 #include <linux/sizes.h>
+#include <linux/sysfb.h>
 
 #include <asm/efi.h>
 #include <asm/mach/map.h>
@@ -91,13 +92,36 @@ int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
 	return 0;
 }
 
+static unsigned long __initdata primary_display_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_PRIMARY_DISPLAY_TABLE_GUID, &primary_display_table},
 	{LINUX_EFI_ARM_CPU_STATE_TABLE_GUID, &cpu_state_table},
 	{}
 };
 
+static void __init load_primary_display_table(void)
+{
+	struct sysfb_display_info *dpy;
+
+	if (primary_display_table == EFI_INVALID_TABLE_ADDR)
+		return;
+
+	dpy = early_memremap(primary_display_table, sizeof(*dpy));
+	if (!dpy) {
+		pr_err("Could not map primary_display config table\n");
+		return;
+	}
+	sysfb_primary_display = *dpy;
+	memset(dpy, 0, sizeof(*dpy));
+	early_memunmap(dpy, sizeof(*dpy));
+
+	if (memblock_is_map_memory(sysfb_primary_display.screen.lfb_base))
+		memblock_mark_nomap(sysfb_primary_display.screen.lfb_base,
+				    sysfb_primary_display.screen.lfb_size);
+}
+
 static void __init load_cpu_state_table(void)
 {
 	if (cpu_state_table != EFI_INVALID_TABLE_ADDR) {
@@ -136,6 +160,9 @@ void __init arm_efi_init(void)
 {
 	efi_init();
 
+	if (IS_ENABLED(CONFIG_SYSFB) || IS_ENABLED(CONFIG_FIRMWARE_EDID))
+		load_primary_display_table();
+
 	/* ARM does not permit early mappings to persist across paging_init() */
 	efi_memmap_unmap();
 
arch/loongarch/kernel/efi.c +3 -17
index 69dd83f8082f..c914aabcd282 100644
@@ -72,8 +72,6 @@ bool efi_poweroff_required(void)
 		(acpi_gbl_reduced_hardware || acpi_no_s5);
 }
 
-unsigned long __initdata primary_display_table = EFI_INVALID_TABLE_ADDR;
-
 #if defined(CONFIG_SYSFB) || defined(CONFIG_EFI_EARLYCON)
 struct sysfb_display_info sysfb_primary_display __section(".data");
 EXPORT_SYMBOL_GPL(sysfb_primary_display);
@@ -81,22 +79,10 @@ EXPORT_SYMBOL_GPL(sysfb_primary_display);
 
 static void __init init_primary_display(void)
 {
-	struct sysfb_display_info *dpy;
-
-	if (primary_display_table == EFI_INVALID_TABLE_ADDR)
-		return;
-
-	dpy = early_memremap(primary_display_table, sizeof(*dpy));
-	if (!dpy) {
-		pr_err("Could not map primary_display config table\n");
-		return;
-	}
-	sysfb_primary_display = *dpy;
-	memset(dpy, 0, sizeof(*dpy));
-	early_memunmap(dpy, sizeof(*dpy));
+	struct screen_info *si = &sysfb_primary_display.screen;
 
-	memblock_reserve(__screen_info_lfb_base(&sysfb_primary_display.screen),
-			 sysfb_primary_display.screen.lfb_size);
+	if (si->orig_video_isVGA == VIDEO_TYPE_EFI)
+		memblock_reserve(__screen_info_lfb_base(si), si->lfb_size);
 }
 
 void __init efi_init(void)
drivers/firmware/efi/efi-init.c +4 -20
index 9f09b0d2daa0..25a5497ebb2e 100644
@@ -23,8 +23,6 @@
 
 #include <asm/efi.h>
 
-unsigned long __initdata primary_display_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))
@@ -67,25 +65,11 @@ EXPORT_SYMBOL_GPL(sysfb_primary_display);
 
 static void __init init_primary_display(void)
 {
-	struct sysfb_display_info *dpy;
-
-	if (primary_display_table != EFI_INVALID_TABLE_ADDR) {
-		dpy = early_memremap(primary_display_table, sizeof(*dpy));
-		if (!dpy) {
-			pr_err("Could not map primary_display config table\n");
-			return;
-		}
-		sysfb_primary_display = *dpy;
-		memset(dpy, 0, sizeof(*dpy));
-		early_memunmap(dpy, sizeof(*dpy));
+	struct screen_info *si = &sysfb_primary_display.screen;
 
-		if (memblock_is_map_memory(sysfb_primary_display.screen.lfb_base))
-			memblock_mark_nomap(sysfb_primary_display.screen.lfb_base,
-					    sysfb_primary_display.screen.lfb_size);
-
-		if (IS_ENABLED(CONFIG_EFI_EARLYCON))
-			efi_earlycon_reprobe();
-	}
+	if (si->orig_video_isVGA == VIDEO_TYPE_EFI &&
+	    memblock_is_map_memory(si->lfb_base))
+		memblock_mark_nomap(si->lfb_base, si->lfb_size);
 }
 
 static int __init uefi_init(u64 efi_system_table)
drivers/firmware/efi/efi.c +0 -5
index 9bc22de0d67e..90c421211ddc 100644
@@ -57,8 +57,6 @@ unsigned long __ro_after_init efi_rng_seed = 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 primary_display_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),
@@ -674,9 +672,6 @@ static const efi_config_table_type_t common_tables[] __initconst = {
 #endif
 #ifdef CONFIG_UNACCEPTED_MEMORY
 	{LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID,	&efi.unaccepted,	"Unaccepted"	},
-#endif
-#ifdef CONFIG_EFI_GENERIC_STUB
-	{LINUX_EFI_PRIMARY_DISPLAY_TABLE_GUID,	&primary_display_table			},
 #endif
 	{},
 };
drivers/firmware/efi/libstub/Makefile +1 -1
index 9dc6a707fd79..0c5fbc21d2a2 100644
@@ -81,7 +81,7 @@ $(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 \
-				   primary_display.o efi-stub-entry.o
+				   efi-stub-entry.o
 
 lib-$(CONFIG_ARM)		+= arm32-stub.o
 lib-$(CONFIG_ARM64)		+= kaslr.o arm64.o arm64-stub.o smbios.o
drivers/firmware/efi/libstub/arm32-stub.c +41 -0

53 lines not on this page · raw message

drivers/firmware/efi/libstub/efi-stub-entry.c +0 -13
index 36ea6aa2cfcc..fb1a4a175717 100644
@@ -9,19 +9,6 @@
 
 extern const struct efi_image_info efistub_image_info;
 
-struct sysfb_display_info *
-alloc_primary_display(const struct efi_image_info *image_info)
-{
-	if (IS_ENABLED(CONFIG_ARM))
-		return __alloc_primary_display();
-
-	if (IS_ENABLED(CONFIG_EFI_EARLYCON) ||
-	    IS_ENABLED(CONFIG_SYSFB))
-		return efistub_sysfb_display_info(image_info);
-
-	return NULL;
-}
-
 /*
  * EFI entry point for the generic EFI stub used by ARM, arm64, RISC-V and
  * LoongArch. This is the entrypoint that is described in the PE/COFF header
drivers/firmware/efi/libstub/efi-stub.c +14 -0
index 55bf7ed50c81..a824b2ba3026 100644
@@ -48,6 +48,20 @@
 static u64 virtmap_base = EFI_RT_VIRTUAL_BASE;
 static bool flat_va_mapping = (EFI_RT_VIRTUAL_OFFSET != 0);
 
+/*
+ * By default, populate the core kernel's copy of sysfb_primary_display
+ * directly. This works for both the EFI stub that is incorporated into the
+ * core kernel and for the zboot decompressor, as both have access to the
+ * location of the variable inside the loaded kernel image via image_info.
+ *
+ * ARM overrides these, as it uses a configuration table instead.
+ */
+struct sysfb_display_info * __weak
+alloc_primary_display(const struct efi_image_info *image_info)
+{
+	return efistub_sysfb_display_info(image_info);
+}
+
 void __weak free_primary_display(struct sysfb_display_info *dpy)
 { }
 
drivers/firmware/efi/libstub/efistub.h +0 -1
index 634e52aa6dcc..082ce2961dda 100644
@@ -1226,7 +1226,6 @@ void efi_retrieve_eventlog(void);
 struct sysfb_display_info *
 alloc_primary_display(const struct efi_image_info *image_info);
 
-struct sysfb_display_info *__alloc_primary_display(void);
 void free_primary_display(struct sysfb_display_info *dpy);
 
 void efi_cache_sync_image(unsigned long image_base,
drivers/firmware/efi/libstub/primary_display.c deleted +0 -56

56 lines not on this page · raw message

drivers/firmware/efi/libstub/zboot.c +0 -6
index 9c7de225b39e..4089a29068ca 100644
@@ -26,12 +26,6 @@ void __weak efi_cache_sync_image(unsigned long image_base,
 	// executable code loaded into memory to be safe for execution.
 }
 
-struct sysfb_display_info *
-alloc_primary_display(const struct efi_image_info *image_info)
-{
-	return efistub_sysfb_display_info(image_info);
-}
-
 static
 const struct efi_image_info *zboot_get_image_info(unsigned long image_base)
 {
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Reviewed-by on v3 14/15
#30 lore raw

Re: [PATCH v3 14/15] efi: Make config table based primary display handover ARM-only again

Quoted 11 lines · Ard Biesheuvel

This reverts the effect of commit 732ea9db9d8a ("efi: libstub: Move screen_info handling to common code"), taking into account the changes that were made to the code since.

That commit moved all architectures to the ARM approach of passing the primary display information (formerly struct screen_info) from the EFI stub to the core kernel via an EFI configuration table, so that the zboot decompressor, which is not linked into the core kernel image, could pass this information as well.

[...]

Signature · 1 line
Jason
#31 lore raw

PATCH v3 15/15 Revert "efi: earlycon: Reprobe after parsing config tables"

In reply to Ard Biesheuvel

From: Ard Biesheuvel <[email protected]>

This reverts commit 8b3a149db461d3286d1e211112de3b44ccaeaf71.

That commit added logic to probe the EFI framebuffer earlycon again after the EFI configuration tables have been parsed, as the primary display information may not have been available yet when the earlycon= early parameter was processed.

Now that only ARM uses a configuration table to pass the primary display information to the core kernel, and the EFI framebuffer earlycon is not supported on ARM, the reprobe logic no longer has any callers. So revert it.

---
 drivers/firmware/efi/earlycon.c | 16 +++-------------
 include/linux/efi.h             |  1 -
 2 files changed, 3 insertions(+), 14 deletions(-)
drivers/firmware/efi/earlycon.c +3 -13
index 3d060d59968c..997f69ce46cb 100644
@@ -216,14 +216,6 @@ efi_earlycon_write(struct console *con, const char *str, unsigned int num)
 	}
 }
 
-static bool __initdata fb_probed;
-
-void __init efi_earlycon_reprobe(void)
-{
-	if (fb_probed)
-		setup_earlycon("efifb");
-}
-
 static int __init efi_earlycon_setup(struct earlycon_device *device,
 				     const char *opt)
 {
@@ -231,17 +223,15 @@ static int __init efi_earlycon_setup(struct earlycon_device *device,
 	u16 xres, yres;
 	u32 i;
 
-	fb_wb = opt && !strcmp(opt, "ram");
-
-	if (si->orig_video_isVGA != VIDEO_TYPE_EFI) {
-		fb_probed = true;
+	if (si->orig_video_isVGA != VIDEO_TYPE_EFI)
 		return -ENODEV;
-	}
 
 	fb_base = si->lfb_base;
 	if (si->capabilities & VIDEO_CAPABILITY_64BIT_BASE)
 		fb_base |= (u64)si->ext_lfb_base << 32;
 
+	fb_wb = opt && !strcmp(opt, "ram");
+
 	xres = si->lfb_width;
 	yres = si->lfb_height;
 
include/linux/efi.h +0 -1
index cc1df1577d5f..4a809c6ae724 100644
@@ -719,7 +719,6 @@ efi_guid_to_str(const efi_guid_t *guid, char *out)
 }
 
 extern void efi_init (void);
-extern void efi_earlycon_reprobe(void);
 #ifdef CONFIG_EFI
 extern void efi_enter_virtual_mode (void);	/* switch EFI to virtual mode, if possible */
 #else
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Reviewed-by on v3 15/15
#32 lore raw

Re: [PATCH v3 15/15] Revert "efi: earlycon: Reprobe after parsing config tables"

Quoted 8 lines · Ard Biesheuvel

This reverts commit 8b3a149db461d3286d1e211112de3b44ccaeaf71.

That commit added logic to probe the EFI framebuffer earlycon again after the EFI configuration tables have been parsed, as the primary display information may not have been available yet when the earlycon= early parameter was processed.

[...]

Signature · 1 line
Jason
Tested-by on the v3 series
#33 lore raw
In reply to Ard Biesheuvel
Quoted 10 lines

From: Ard Biesheuvel <[email protected]>

Clean up the arm64 build script and linker hacks that zboot uses to obtain properties of the loaded image (which it cannot access directly via a relative reference like the ordinary EFI stub).

Instead, define a generic struct efi_image_info, and expose its offset in the loaded image in a similar manner, but without relying on explicit linker script exports.

Quoted 5 lines

Populate the struct with section markers that all architecture use for loading the kernel image at boot, so that all the linker exports can be dropped for them.

Changes since v2:

Still works with my DRTM series just fine on ARM64

Signature · 1 line
Jason