efi: Improve command line initrd loader support
Ard Biesheuvel
maintainercover efi: Improve command line initrd loader support
Given that the command line initrd loader can only load files from the same simple FS volume from which the kernel itself was loaded, it was not flexible enough for use in loaders such as GRUB that don't even implement the FS volume abstraction, and load images as anonymous buffers in memory. As x86 mixed mode was based on a GRUB specific EFI handover protocol, and the simple FS protocol method prototypes were not mixed mode safe, the initrd command line loader did not work for mixed mode at all.
For this reason, we support arch specific methods (bootparam, DT) as well, and have added support for a generic method based on the LoadFile2 protocol, which is much easier to implement and use.
However, there are cases where the command line loader is useful, especially when using fully generic loaders such as the UEFI shell. Here, it is generally not possible to use Linux specific structs or data structures, or implement the Linux specific LoadFile2 protocol implementation for initrd loading.
So let's fix the issues with the initrd command line loader:
- add the ability to use fully qualified device paths as the initrd= argument, so that the initrd image can reside on any simple FS volume, even if the kernel itself was not loaded from one;
- fix the mixed mode issues, by using the mixed mode wrappers for the protocol invocations, and defining some recipes for the prototypes that cannot be marshalled 1:1
The above means that mixed mode loaders that boot via the compat entry point (e.g., systemd-boot) can use the initrd= command line option as before to pass the initrd, if desired.
With those fixes in place, let's remove the deprecation notice, and permit the feature to be enabled on LoongArch and RISC-V as well.
Note that LoadFile2 is still preferred, and will continue to take precedence. It is also the only generic method that cannot be compiled out.
Ard Biesheuvel (4):
efi: libstub: Implement devicepath support for initrd commandline
loader
efi: libstub: Permit mixed mode return types other than efi_status_t
efi: libstub: Add mixed mode support to command line initrd loader
efi: libstub: Undeprecate the command line initrd loader
arch/x86/boot/compressed/efi_thunk_64.S | 6 -- arch/x86/include/asm/efi.h | 49 +++++---- drivers/firmware/efi/Kconfig | 13 ++- drivers/firmware/efi/libstub/efi-stub-helper.c | 2 +- drivers/firmware/efi/libstub/efistub.h | 112 ++++++++++++++------ drivers/firmware/efi/libstub/file.c | 105 +++++++++++++----- include/linux/efi.h | 6 ++ 7 files changed, 202 insertions(+), 91 deletions(-)
Signature · 1 line
2.35.1
Ard Biesheuvel
maintainerPATCH 1/4 efi: libstub: Implement devicepath support for initrd commandline loader
Currently, the initrd= command line option to the EFI stub only supports loading files that reside on the same volume as the loaded image, which is not workable for loaders like GRUB that don't even implement the volume abstraction (EFI_SIMPLE_FILE_SYSTEM_PROTOCOL), and load the kernel from an anonymous buffer in memory. For this reason, another method was devised that relies on the LoadFile2 protocol.
However, the command line loader is rather useful when using the UEFI shell or other generic loaders that have no awareness of Linux specific protocols so let's make it a bit more flexible, by permitting textual device paths to be provided to initrd= as well, provided that they refer to a file hosted on a EFI_SIMPLE_FILE_SYSTEM_PROTOCOL volume. E.g.,
initrd=PciRoot(0x0)/Pci(0x3,0x0)/HD(1,MBR,0xBE1AFDFA,0x3F,0xFBFC1)/rootfs.cpio.gz
--- drivers/firmware/efi/libstub/efistub.h | 15 ++++ drivers/firmware/efi/libstub/file.c | 77 +++++++++++++++++--- include/linux/efi.h | 6 ++ 3 files changed, 87 insertions(+), 11 deletions(-)
drivers/firmware/efi/libstub/efistub.h +15 -0
@@ -179,6 +179,21 @@ union efi_device_path_to_text_protocol {typedef union efi_device_path_to_text_protocol efi_device_path_to_text_protocol_t ; +union efi_device_path_from_text_protocol { +struct { +efi_device_path_protocol_t * + (__efiapi *convert_text_to_device_node)(const efi_char16_t *); +efi_device_path_protocol_t * + (__efiapi *convert_text_to_device_path)(const efi_char16_t *); + }; +struct { +u32 convert_text_to_device_node; +u32 convert_text_to_device_path; + } mixed_mode; +}; + +typedef union efi_device_path_from_text_protocol efi_device_path_from_text_protocol_t ; +typedef void *efi_event_t ;/* Note that notifications won't work in mixed mode */ typedef void (__efiapi *efi_event_notify_t)(efi_event_t, void *);
drivers/firmware/efi/libstub/file.c +66 -11
118 lines not on this page · raw message
include/linux/efi.h +6 -0
@@ -371,6 +371,7 @@ void efi_native_runtime_setup(void);#define LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID EFI_GUID(0xbc62157e, 0x3e33, 0x4fec, 0x99, 0x20, 0x2d, 0x3b, 0x36, 0xd7, 0x50, 0xdf)#define EFI_DEVICE_PATH_PROTOCOL_GUID EFI_GUID(0x09576e91, 0x6d3f, 0x11d2, 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)#define EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID EFI_GUID(0x8b843e20, 0x8132, 0x4852, 0x90, 0xcc, 0x55, 0x1a, 0x4e, 0x4a, 0x7f, 0x1c) +#define EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL_GUID EFI_GUID(0x05c99a21, 0xc70f, 0x4ad2, 0x8a, 0x5f, 0x35, 0xdf, 0x33, 0x43, 0xf5, 0x1e)#define EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID EFI_GUID(0x9042a9de, 0x23dc, 0x4a38, 0x96, 0xfb, 0x7a, 0xde, 0xd0, 0x80, 0x51, 0x6a)#define EFI_UGA_PROTOCOL_GUID EFI_GUID(0x982c298b, 0xf4fa, 0x41cb, 0xb8, 0x38, 0x77, 0xaa, 0x68, 0x8f, 0xb8, 0x39)#define EFI_PCI_IO_PROTOCOL_GUID EFI_GUID(0x4cf5b200, 0x68b8, 0x4ca5, 0x9e, 0xec, 0xb2, 0x3e, 0x3f, 0x50, 0x02, 0x9a)
@@ -1011,6 +1012,11 @@ struct efi_mem_mapped_dev_path {u64 ending_addr; } __packed; +struct efi_file_path_dev_path { +struct efi_generic_dev_path header; +efi_char16_t filename[]; +} __packed; +struct efi_dev_path {union {struct efi_generic_dev_path header;
Signature · 1 line
2.35.1
Ard Biesheuvel
maintainerPATCH 2/4 efi: libstub: Permit mixed mode return types other than efi_status_t
In reply to Ard BiesheuvelRework the EFI stub macro wrappers around protocol method calls and other indirect calls in order to allow return types other than efi_status_t. This means the widening should be conditional on whether or not the return type is efi_status_t, and should be omitted otherwise.
Note that this does not take into account that unsigned long types might exist that are not efi_status_t, but that can be addressed when it becomes an issue.
--- arch/x86/boot/compressed/efi_thunk_64.S | 6 ---- arch/x86/include/asm/efi.h | 38 ++++++++------------ drivers/firmware/efi/libstub/efistub.h | 16 ++++++--- 3 files changed, 26 insertions(+), 34 deletions(-)
arch/x86/boot/compressed/efi_thunk_64.S +0 -6
@@ -93,12 +93,6 @@ SYM_FUNC_START(__efi64_thunk) movl %ebx, %fs movl %ebx, %gs - /* - * Convert 32-bit status code into 64-bit. - */ - roll $1, %eax - rorq $1, %rax - pop %rbx pop %rbp RET
arch/x86/include/asm/efi.h +15 -23
@@ -178,7 +178,7 @@ struct efi_setup_data {extern u64 efi_setup;#ifdef CONFIG_EFI -extern efi_status_t __efi64_thunk (u32 , ...); +extern u64 __efi64_thunk (u32 , ...);#define efi64_thunk (...) ({ \ u64 __pad[3];/* must have space for 3 args on the stack */ \
@@ -344,31 +344,23 @@ static inline u32 efi64_convert_status(efi_status_t status)#define __efi_eat (...)#define __efi_eval (...) __VA_ARGS__ -/* The three macros below handle dispatching via the thunk if needed */ - -#define efi_call_proto (inst, func, ...) \ - (efi_is_native() \ - ? inst->func(inst, ##__VA_ARGS__) \ - : __efi64_thunk_map(inst, func, inst, ##__VA_ARGS__)) - -#define efi_bs_call (func, ...) \ - (efi_is_native() \ - ? efi_system_table->boottime->func(__VA_ARGS__) \ - : __efi64_thunk_map(efi_table_attr(efi_system_table, \ - boottime), \ - func, __VA_ARGS__)) +static inline efi_status_t __efi64_widen_efi_status (u64 status) +{ +return status ? status ^0x8000000080000000ULL :0 ; +} -#define efi_rt_call (func, ...) \ - (efi_is_native() \ - ? efi_system_table->runtime->func(__VA_ARGS__) \ - : __efi64_thunk_map(efi_table_attr(efi_system_table, \ - runtime), \ - func, __VA_ARGS__)) +/* The macro below handles dispatching via the thunk if needed */ -#define efi_dxe_call (func, ...) \ +#define efi_fn_call (inst, func, ...) \ (efi_is_native() \ - ? efi_dxe_table->func(__VA_ARGS__) \ - : __efi64_thunk_map(efi_dxe_table, func, __VA_ARGS__)) + ? inst->func(__VA_ARGS__) \ + : __builtin_choose_expr( \ + __builtin_types_compatible_p(efi_status_t, \ + __typeof__(inst->func(__VA_ARGS__))), \ + __efi64_widen_efi_status( \ + __efi64_thunk_map(inst, func, ##__VA_ARGS__)), \ + (__typeof__(inst->func(__VA_ARGS__))) \ + __efi64_thunk_map(inst, func, ##__VA_ARGS__)))#else /* CONFIG_EFI_MIXED */
drivers/firmware/efi/libstub/efistub.h +11 -5
@@ -44,15 +44,21 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,#ifndef ARCH_HAS_EFISTUB_WRAPPERS -#define efi_is_native () (true) -#define efi_bs_call (func, ...) efi_system_table->boottime->func(__VA_ARGS__) -#define efi_rt_call (func, ...) efi_system_table->runtime->func(__VA_ARGS__) -#define efi_dxe_call (func, ...) efi_dxe_table->func(__VA_ARGS__) +#define efi_is_native () (true)#define efi_table_attr (inst, attr) (inst->attr) -#define efi_call_proto (inst, func, ...) inst->func(inst, ##__VA_ARGS__) +#define efi_fn_call (inst, func, ...) inst->func(__VA_ARGS__)#endif +#define efi_call_proto (inst, func, ...) \ + efi_fn_call(inst, func, inst, ##__VA_ARGS__) +#define efi_bs_call (func, ...) \ + efi_fn_call(efi_table_attr(efi_system_table, boottime), func, ##__VA_ARGS__) +#define efi_rt_call (func, ...) \ + efi_fn_call(efi_table_attr(efi_system_table, runtime), func, ##__VA_ARGS__) +#define efi_dxe_call (func, ...) \ + efi_fn_call(efi_dxe_table, func, ##__VA_ARGS__) +#define efi_info (fmt, ...) \ efi_printk(KERN_INFO fmt, ##__VA_ARGS__)#define efi_warn (fmt, ...) \
Signature · 1 line
2.35.1
Ard Biesheuvel
maintainerPATCH 3/4 efi: libstub: Add mixed mode support to command line initrd loader
In reply to Ard BiesheuvelNow that we have support for calling protocols that need additional marshalling for mixed mode, wire up the initrd command line loader.
--- arch/x86/include/asm/efi.h | 11 +++ drivers/firmware/efi/libstub/efi-stub-helper.c | 2 +- drivers/firmware/efi/libstub/efistub.h | 81 +++++++++++++------- drivers/firmware/efi/libstub/file.c | 34 ++++---- 4 files changed, 84 insertions(+), 44 deletions(-)
arch/x86/include/asm/efi.h +11 -0
@@ -325,6 +325,17 @@ static inline u32 efi64_convert_status(efi_status_t status)#define __efi64_argmap_set_memory_space_attributes (phys, size, flags) \ (__efi64_split(phys), __efi64_split(size), __efi64_split(flags)) +/* file protocol */ +#define __efi64_argmap_open (prot, newh, fname, mode, attr) \ + ((prot), efi64_zero_upper(newh), (fname), __efi64_split(mode), \ + __efi64_split(attr)) + +#define __efi64_argmap_set_position (pos) (__efi64_split(pos)) + +/* file system protocol */ +#define __efi64_argmap_open_volume (prot, file) \ + ((prot), efi64_zero_upper(file)) +/* * The macros below handle the plumbing for the argument mapping. To add a * mapping for a specific EFI method, simply define a macro
drivers/firmware/efi/libstub/efi-stub-helper.c +1 -1
@@ -682,7 +682,7 @@ efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image, unsigned long hard_limit) {if (!IS_ENABLED (CONFIG_EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER ) || - (IS_ENABLED (CONFIG_X86 ) && (!efi_is_native () || image ==NULL ))) + (IS_ENABLED (CONFIG_X86 ) && image ==NULL ))return EFI_UNSUPPORTED ; return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
drivers/firmware/efi/libstub/efistub.h +54 -27
90 lines not on this page · raw message
drivers/firmware/efi/libstub/file.c +18 -16
@@ -51,17 +51,18 @@ static efi_status_t efi_open_file(efi_file_protocol_t *volume, *c =L'\\' ; } - status = volume->open (volume, &fh, fi->filename,EFI_FILE_MODE_READ ,0 ); + status =efi_call_proto (volume, open, &fh, fi->filename, +EFI_FILE_MODE_READ ,0 );if (status !=EFI_SUCCESS ) {efi_err ("Failed to open file: %ls\n" , fi->filename);return status; } info_sz =sizeof (struct finfo ); - status = fh->get_info (fh, &info_guid, &info_sz, fi); + status =efi_call_proto (fh, get_info, &info_guid, &info_sz, fi);if (status !=EFI_SUCCESS ) {efi_err ("Failed to get file info\n" ); - fh->close (fh); +efi_call_proto (fh, close);return status; }
@@ -73,7 +74,7 @@ static efi_status_t efi_open_file(efi_file_protocol_t *volume,static efi_status_t efi_open_volume (efi_loaded_image_t *image,efi_file_protocol_t **fh) { -struct efi_vendor_dev_path *dp = image->file_path; +struct efi_vendor_dev_path *dp =efi_table_attr (image, file_path);efi_guid_t li_proto =LOADED_IMAGE_PROTOCOL_GUID ;efi_guid_t fs_proto =EFI_FILE_SYSTEM_GUID ;efi_simple_file_system_protocol_t *io;
@@ -95,14 +96,14 @@ static efi_status_t efi_open_volume(efi_loaded_image_t *image, } } - status =efi_bs_call (handle_protocol, image->device_handle, &fs_proto, - (void **)&io); + status =efi_bs_call (handle_protocol,efi_table_attr (image, device_handle), + &fs_proto, (void **)&io);if (status !=EFI_SUCCESS ) {efi_err ("Failed to handle fs_proto\n" );return status; } - status = io->open_volume (io, fh); + status =efi_call_proto (io, open_volume, fh);if (status !=EFI_SUCCESS )efi_err ("Failed to open volume\n" );
@@ -162,7 +163,8 @@ static efi_status_t efi_open_device_path(efi_file_protocol_t **volume,/* Convert the filename wide string into a device path */ - initrd_dp = text_to_dp->convert_text_to_device_path (fi->filename); + initrd_dp =efi_fn_call (text_to_dp, convert_text_to_device_path, + fi->filename);/* Check whether the device path in question implements simple FS */ if ((efi_bs_call(locate_device_path, &fs_proto, &initrd_dp, &handle) ?:
@@ -184,7 +186,7 @@ static efi_status_t efi_open_device_path(efi_file_protocol_t **volume,min (sizeof (fi->filename), fpath->header.length - sizeof(fpath->header))); - status = io->open_volume (io, volume); + status =efi_call_proto (io, open_volume, volume);if (status !=EFI_SUCCESS )efi_err ("Failed to open volume\n" );
@@ -205,8 +207,8 @@ efi_status_t handle_cmdline_files(efi_loaded_image_t *image,unsigned long *load_addr, unsigned long *load_size) { - const efi_char16_t *cmdline = image->load_options; -u32 cmdline_len = image->load_options_size; + const efi_char16_t *cmdline = efi_table_attr(image, load_options); +u32 cmdline_len =efi_table_attr (image, load_options_size);unsigned long efi_chunk_size =ULONG_MAX ;efi_file_protocol_t *volume =NULL ;efi_file_protocol_t *file;
@@ -294,7 +296,7 @@ efi_status_t handle_cmdline_files(efi_loaded_image_t *image,while (size) {unsigned long chunksize =min (size, efi_chunk_size); - status = file->read (file, &chunksize, addr); + status =efi_call_proto (file, read, &chunksize, addr);if (status !=EFI_SUCCESS ) {efi_err ("Failed to read file\n" ); goto err_close_file;
@@ -302,8 +304,8 @@ efi_status_t handle_cmdline_files(efi_loaded_image_t *image, addr += chunksize; size -= chunksize; } - file->close (file); - volume->close (volume); +efi_call_proto (file, close); +efi_call_proto (volume, close); }while (offset >0 ); *load_addr = alloc_addr;
@@ -312,10 +314,10 @@ efi_status_t handle_cmdline_files(efi_loaded_image_t *image,return EFI_SUCCESS ; err_close_file: - file->close (file); +efi_call_proto (file, close); err_close_volume: - volume->close (volume); +efi_call_proto (volume, close); err_free_alloc:efi_free (alloc_size, alloc_addr);
Signature · 1 line
2.35.1
Ard Biesheuvel
maintainerPATCH 4/4 efi: libstub: Undeprecate the command line initrd loader
In reply to Ard BiesheuvelThe initrd= command line loader can be useful for development, but it was limited to loading files from the same file system as the loaded kernel (and it didn't work on x86 mixed mode).
As both issues have been fixed, and the initrd= can now be used with files residing on any simply file system exposed by the EFI firmware, let's permit it to be enabled on RISC-V and LoongArch, which did not support it up to this point.
Note that LoadFile2 remains the preferred option, as it is much simpler to use and implement, but generic loaders (including the UEFI shell) may not implement this so there, initrd= can now be used as well (if enabled in the build)
--- drivers/firmware/efi/Kconfig | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/firmware/efi/Kconfig +8 -5
@@ -165,13 +165,16 @@ config EFI_GENERIC_STUB_INITRD_CMDLINE_LOADER bool "Enable the command line initrd loader" if !X86 depends on EFI_STUB && (EFI_GENERIC_STUB || X86) default y if X86 - depends on !RISCV && !LOONGARCH help Select this config option to add support for the initrd= command - line parameter, allowing an initrd that resides on the same volume - as the kernel image to be loaded into memory. - - This method is deprecated. + line parameter, allowing an initrd to be loaded into memory that + resides on a file system backed by an implementation of + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL. + + This method has been superseded by the simpler LoadFile2 based + initrd loading method, but the initrd= loader is retained as it + can be used from the UEFI Shell or other generic loaders that + don't implement the Linux specific LoadFile2 method. config EFI_BOOTLOADER_CONTROL tristate "EFI Bootloader Control"
Signature · 1 line
2.35.1