[PATCH v2 04/10] efi/libstub: Capture loaded image information in struct efi_image_info
Ard Biesheuvel
ardb at kernel.org
Fri Nov 13 08:15:29 PST 2026
On Fri, 13 Nov 2026, at 16:10, Jason Gunthorpe wrote:
>> [ ... 131 lines skipped ... ]
>> @@ -80,7 +87,8 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
>>
>> 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_addr, cmdline_ptr,
>> + efistub_get_image_info());
>
> efistub_get_image_info() always returns the pointer from the EFI stubs
> location, but at this point the kernel has been relocated and now we
> are passing an info pointer that is outside image to everything else
> while the zboot path passes an info inside the image.
>
> It would be better if info is always pointing inside the final
> image. Then you can use info to always get a pointer inside the
> ultimate kernel, which is what all the DRTM values need.
>
> Having efistub_get_image_info() take in image_base, and then using
> weak to overlay it with zboot's version would give a consistent
> interface.
>
> The adjustment can be calculated like this:
>
> return (const void *)(image_base - efistub_image_info._text_offset);
>
> That would allow cleaning up the primary_entry() flow to just use
> info, and remove the weak functions:
>
> 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) =
> efistub_kernel_ptr(image_info, entry);
> enter_kernel(fdt_addr, 0, 0, 0);
> }
>
> (see below for macro)
>
> You could do the same by passing image_base and image_info everywhere,
> but that seems less nice.
>
I'd prefer to handle this by adding another offset, and an API to update
it after moving the image. That way, the EFI stub code can still grab the
info struct pointer without threading it through the API, while it will
always remain valid.
>> [ ... 20 lines skipped ... ]
>> +++ b/drivers/firmware/efi/libstub/efistub.h
>> @@ -1148,11 +1148,53 @@ efi_status_t handle_kernel_image(unsigned long *image_addr,
>> efi_loaded_image_t *image,
>> efi_handle_t image_handle);
>>
>> +struct efi_image_info {
>> + s32 _text_offset;
>> + s32 __inittext_end_offset;
>> + s32 _edata_offset;
>> + s32 _end_offset;
>> + s32 entry_offset;
>> +};
>
> This ends up in a cross-arch header.. It seems OK, the x86 voffset.h
> parts can fit cross-arch like this too.
>
> For DRTM I will do it like this:
>
> #ifdef CONFIG_EFI_STUB_DRTM
> s32 __drtm_measured_start_offset;
> s32 __drtm_measured_end_offset;
> s32 drtm_entry_offset;
> s32 drtm_handoff_offset;
> #endif
>
> Which should be fine, every DRTM will need those four things at least.
>
I think that is fine. We could add an arch substruct once we need it
but that seems premature at this point.
>> +
>> +const struct efi_image_info *efistub_get_image_info(void);
>> +const struct efi_image_info *zboot_get_image_info(unsigned long image_base);
>> +
>> +static inline
>> +unsigned long efistub_kernel_base(const struct efi_image_info *image_info)
>> +{
>> + return (unsigned long)offset_to_ptr(&image_info->_text_offset);
>> +}
>> +
>> +static inline
>> +unsigned long efistub_kernel_size(const struct efi_image_info *image_info)
>> +{
>> + return image_info->_edata_offset - image_info->_text_offset;
>> +}
>> +
>> +static inline
>> +unsigned long efistub_kernel_memsize(const struct efi_image_info *image_info)
>> +{
>> + return image_info->_end_offset - image_info->_text_offset;
>> +}
>> +
>> +static inline
>> +unsigned long efistub_kernel_codesize(const struct efi_image_info *image_info)
>> +{
>> + return image_info->__inittext_end_offset - image_info->_text_offset;
>> +}
>> +
>> +static inline
>> +unsigned long efistub_kernel_entry_offset(const struct efi_image_info *image_info)
>> +{
>> + return image_info->entry_offset - image_info->_text_offset;
>> +}
>
> As above a little helper to get a void * would be useful:
>
> static inline void *
> __efistub_kernel_entry_ptr(const struct efi_image_info *image_info,
> s32 offset)
> {
> return (void *)((offset - image_info->_text_offset) +
> efistub_kernel_base(image_info));
> }
> #define efistub_kernel_ptr(image_info, ptr) \
> __efistub_kernel_entry_ptr(image_info, (image_info)->ptr##_offset)
>
> If the rework above is done this would replace
> efistub_kernel_entry_offset()
>
>> [ ... 15 lines skipped ... ]
>> +++ b/drivers/firmware/efi/libstub/image_info.S
>> @@ -0,0 +1,12 @@
>> +/* SPDX-License-Identifier: GPL-2.0 */
>> +
>> +#define REL_REF(sym) .long sym - 0b
>
> The code could consistently use the offset_to_ptr() macro if the label was
> included in the macro?
>
> #define REL_REF(sym) 0: .long sym - 0b
>
> It would make the efistub_kernel_ptr() simpler, and the three size
> calculations would look like:
>
> return efistub_kernel_ptr(image_info, __inittext_end) -
> efistub_kernel_ptr(image_info, _text);
>
Again, probably cleaner to just refer to the copy of the struct once
it exists.
>> +
>> + .globl __efistub_efistub_image_info
>> + .p2align 3
>> +__efistub_efistub_image_info:
>> +0: REL_REF(_text)
>> + REL_REF(__inittext_end)
>> + REL_REF(_edata)
>> + REL_REF(_end)
>> + REL_REF(efi_primary_entry)
>
> Can you put a
>
> + .section ".init.rodata", "a"
>
> Here?
>
Ack
> The DRTM patch points this to various things in .init. sections and
> modpost will complain. I think nothing outside the efi stub should
> touch this stuct and it should be freed as init data right?
>
Yes.
>> [ ... 18 lines skipped ... ]
>> @@ -93,9 +100,15 @@ 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);
>
> auto is allowed in the kernel now?
>
Yes.
More information about the linux-riscv
mailing list