EFI stub cleanup work
cover EFI stub cleanup work
From: Ard Biesheuvel <[email protected]>
Some code cleanup for the EFI stub, to drop obsolete code, and to switch to newer and more ergonomic APIs for managing pool allocations and EFI handle buffers.
No functional changes intended, except for the UGA removal.
Ard Biesheuvel (7):
x86/efistub: Drop long obsolete UGA support
efi/libstub: Use C99-style for loop to traverse handle buffer efi/libstub: Simplify GOP handling code
efi/libstub: Refactor and cleanup GOP resolution picker code efi/libstub: Simplify PCI I/O handle buffer traversal efi/libstub: Use cleanup helpers for freeing copies of the memory map efi/libstub: Use __free() helper for pool deallocations
arch/x86/include/asm/efi.h | 3 + arch/x86/platform/efi/efi.c | 2 - drivers/firmware/efi/libstub/efi-stub-helper.c | 9 +- drivers/firmware/efi/libstub/efi-stub.c | 49 ++- drivers/firmware/efi/libstub/efistub.h | 16 +- drivers/firmware/efi/libstub/gop.c | 323 ++++++++------------ drivers/firmware/efi/libstub/kaslr.c | 4 +- drivers/firmware/efi/libstub/mem.c | 20 +- drivers/firmware/efi/libstub/pci.c | 34 +-- drivers/firmware/efi/libstub/randomalloc.c | 4 +- drivers/firmware/efi/libstub/relocate.c | 10 +- drivers/firmware/efi/libstub/x86-stub.c | 164 ++-------- include/linux/efi.h | 2 - 13 files changed, 198 insertions(+), 442 deletions(-)
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
PATCH 1/7 x86/efistub: Drop long obsolete UGA support
From: Ard Biesheuvel <[email protected]>
UGA is the EFI graphical output protocol that preceded GOP, and has been long obsolete. Drop support for it from the x86 implementation of the EFI stub - other architectures never bothered to implement it (save for ia64)
--- arch/x86/platform/efi/efi.c | 2 - drivers/firmware/efi/libstub/x86-stub.c | 90 -------------------- include/linux/efi.h | 2 - 3 files changed, 94 deletions(-)
arch/x86/platform/efi/efi.c +0 -2
@@ -60,7 +60,6 @@ static unsigned long efi_runtime, efi_nr_tables;unsigned long efi_fw_vendor, efi_config_table;static const efi_config_table_type_t arch_tables[]__initconst = { - {UGA_IO_PROTOCOL_GUID , &uga_phys,"UGA" }, #ifdef CONFIG_X86_UV {UV_SYSTEM_TABLE_GUID , &uv_systab_phys,"UVsystab" }, #endif
@@ -72,7 +71,6 @@ static const unsigned long * const efi_tables[] = { &efi.acpi20, &efi.smbios, &efi.smbios3, - &uga_phys, #ifdef CONFIG_X86_UV &uv_systab_phys, #endif
drivers/firmware/efi/libstub/x86-stub.c +0 -90
109 lines not on this page · raw message
include/linux/efi.h +0 -2
@@ -363,7 +363,6 @@ void efi_native_runtime_setup(void);#define ACPI_20_TABLE_GUID EFI_GUID(0x8868e871, 0xe4f1, 0x11d3, 0xbc, 0x22, 0x00, 0x80, 0xc7, 0x3c, 0x88, 0x81)#define SMBIOS_TABLE_GUID EFI_GUID(0xeb9d2d31, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d)#define SMBIOS3_TABLE_GUID EFI_GUID(0xf2fd1544, 0x9794, 0x4a2c, 0x99, 0x2e, 0xe5, 0xbb, 0xcf, 0x20, 0xe3, 0x94) -#define UGA_IO_PROTOCOL_GUID EFI_GUID(0x61a4d49e, 0x6f68, 0x4f1b, 0xb9, 0x22, 0xa8, 0x6e, 0xed, 0x0b, 0x07, 0xa2)#define EFI_GLOBAL_VARIABLE_GUID EFI_GUID(0x8be4df61, 0x93ca, 0x11d2, 0xaa, 0x0d, 0x00, 0xe0, 0x98, 0x03, 0x2b, 0x8c)#define UV_SYSTEM_TABLE_GUID EFI_GUID(0x3b13a7d4, 0x633e, 0x11dd, 0x93, 0xec, 0xda, 0x25, 0x56, 0xd8, 0x95, 0x93)#define LINUX_EFI_CRASH_GUID EFI_GUID(0xcfc8fc79, 0xbe2e, 0x4ddc, 0x97, 0xf0, 0x9f, 0x98, 0xbf, 0xe2, 0x98, 0xa0)
@@ -373,7 +372,6 @@ void efi_native_runtime_setup(void);#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)#define EFI_FILE_INFO_ID EFI_GUID(0x09576e92, 0x6d3f, 0x11d2, 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)#define EFI_SYSTEM_RESOURCE_TABLE_GUID EFI_GUID(0xb122a263, 0x3661, 0x4f68, 0x99, 0x29, 0x78, 0xf8, 0xb0, 0xd6, 0x21, 0x80)
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
PATCH 2/7 efi/libstub: Use C99-style for loop to traverse handle buffer
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Tweak the for_each_efi_handle() macro in order to avoid the need on the part of the caller to provide a loop counter variable.
Also move efi_get_handle_num() to the callers, so that each occurrence can be replaced with the actual number returned by the simplified LocateHandleBuffer API.
--- drivers/firmware/efi/libstub/efistub.h | 9 ++++----- drivers/firmware/efi/libstub/gop.c | 3 +-- drivers/firmware/efi/libstub/pci.c | 5 ++--- drivers/firmware/efi/libstub/x86-stub.c | 3 +-- 4 files changed, 8 insertions(+), 12 deletions(-)
drivers/firmware/efi/libstub/efistub.h +4 -5
@@ -123,11 +123,10 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,#define efi_get_handle_num (size) \ ((size) / (efi_is_native() ? sizeof(efi_handle_t) : sizeof(u32))) -#define for_each_efi_handle (handle, array, size, i) \ - for (i = 0; \ - i < efi_get_handle_num(size) && \ - ((handle = efi_get_handle_at((array), i)) || true); \ - i++) +#define for_each_efi_handle (handle, array, num) \ + for (int __i = 0; __i < (num) && \ + ((handle = efi_get_handle_at((array), __i)) || true); \ + __i++)static inline void efi_set_u64_split (u64 data,u32 *lo,u32 *hi)
drivers/firmware/efi/libstub/gop.c +1 -2
@@ -466,11 +466,10 @@ find_gop(efi_guid_t *proto, unsigned long size, void **handles) {efi_graphics_output_protocol_t *first_gop;efi_handle_t h; -int i; first_gop =NULL ; -for_each_efi_handle (h, handles, size, i) { +for_each_efi_handle (h, handles,efi_get_handle_num (size)) {efi_status_t status;efi_graphics_output_protocol_t *gop;
drivers/firmware/efi/libstub/pci.c +2 -3
@@ -21,7 +21,6 @@ void efi_pci_disable_bridge_busmaster(void)efi_handle_t handle;efi_status_t status;u16 class, command; -int i; status =efi_bs_call (locate_handle,EFI_LOCATE_BY_PROTOCOL , &pci_proto,NULL , &pci_handle_size,NULL );
@@ -46,7 +45,7 @@ void efi_pci_disable_bridge_busmaster(void) goto free_handle; } -for_each_efi_handle (handle, pci_handle, pci_handle_size, i) { +for_each_efi_handle (handle, pci_handle,efi_get_handle_num (pci_handle_size)) {efi_pci_io_protocol_t *pci;unsigned long segment_nr, bus_nr, device_nr, func_nr;
@@ -82,7 +81,7 @@ void efi_pci_disable_bridge_busmaster(void)efi_bs_call (disconnect_controller, handle,NULL ,NULL ); } -for_each_efi_handle (handle, pci_handle, pci_handle_size, i) { +for_each_efi_handle (handle, pci_handle,efi_get_handle_num (pci_handle_size)) {efi_pci_io_protocol_t *pci; status = efi_bs_call(handle_protocol, handle, &pci_proto,
drivers/firmware/efi/libstub/x86-stub.c +1 -2
@@ -124,7 +124,6 @@ static void setup_efi_pci(struct boot_params *params)unsigned long size =0 ;struct setup_data *data;efi_handle_t h; -int i; status =efi_bs_call (locate_handle,EFI_LOCATE_BY_PROTOCOL , &pci_proto,NULL , &size, pci_handle);
@@ -150,7 +149,7 @@ static void setup_efi_pci(struct boot_params *params)while (data && data->next) data = (struct setup_data *)(unsigned long )data->next; -for_each_efi_handle (h, pci_handle, size, i) { +for_each_efi_handle (h, pci_handle,efi_get_handle_num (size)) {efi_pci_io_protocol_t *pci =NULL ;struct pci_setup_rom *rom;
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
PATCH 3/7 efi/libstub: Simplify GOP handling code
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Use the LocateHandleBuffer() API and a __free() function to simplify the logic that allocates a handle buffer to iterate over all GOP protocols in the EFI database.
--- arch/x86/include/asm/efi.h | 3 ++ drivers/firmware/efi/libstub/efi-stub.c | 28 ++++------ drivers/firmware/efi/libstub/efistub.h | 7 +-- drivers/firmware/efi/libstub/gop.c | 57 +++++++------------- drivers/firmware/efi/libstub/x86-stub.c | 17 +----- 5 files changed, 38 insertions(+), 74 deletions(-)
arch/x86/include/asm/efi.h +3 -0
@@ -250,6 +250,9 @@ static inline u32 efi64_convert_status(efi_status_t status)#define __efi64_argmap_allocate_pool (type, size, buffer) \ ((type), (size), efi64_zero_upper(buffer)) +#define __efi64_argmap_locate_handle_buffer (type, proto, key, num, buf) \ + ((type), (proto), (key), efi64_zero_upper(num), efi64_zero_upper(buf)) +#define __efi64_argmap_create_event (type, tpl, f, c, event) \ ((type), (tpl), (f), (c), efi64_zero_upper(event))
drivers/firmware/efi/libstub/efi-stub.c +10 -18
@@ -10,6 +10,7 @@*/ #include <linux/efi.h> +#include <linux/screen_info.h> #include <asm/efi.h> #include "efistub.h"
@@ -53,25 +54,16 @@ void __weak free_screen_info(struct screen_info *si)static struct screen_info *setup_graphics (void ) { -efi_guid_t gop_proto =EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID ; -efi_status_t status; -unsigned long size; -void **gop_handle =NULL ; -struct screen_info *si =NULL ; +struct screen_info *si, tmp = {}; - size =0 ; - status =efi_bs_call (locate_handle,EFI_LOCATE_BY_PROTOCOL , - &gop_proto,NULL , &size, gop_handle); -if (status ==EFI_BUFFER_TOO_SMALL ) { - si =alloc_screen_info (); -if (!si) -return NULL ; - status =efi_setup_gop (si, &gop_proto, size); -if (status !=EFI_SUCCESS ) { -free_screen_info (si); -return NULL ; - } - } +if (efi_setup_gop (&tmp) !=EFI_SUCCESS ) +return NULL ; + + si =alloc_screen_info (); +if (!si) +return NULL ; + + *si = tmp;return si; }
drivers/firmware/efi/libstub/efistub.h +4 -3
@@ -314,7 +314,9 @@ union efi_boot_services {void *close_protocol;void *open_protocol_information;void *protocols_per_handle; -void *locate_handle_buffer; + efi_status_t (__efiapi *locate_handle_buffer)(int, efi_guid_t *, + void *, unsigned long *, + efi_handle_t *); efi_status_t (__efiapi *locate_protocol)(efi_guid_t *, void *, void **); efi_status_t (__efiapi *install_multiple_protocol_interfaces)(efi_handle_t *, ...);
@@ -1083,8 +1085,7 @@ efi_status_t efi_parse_options(char const *cmdline);void efi_parse_option_graphics (char *option); -efi_status_t efi_setup_gop (struct screen_info *si,efi_guid_t *proto, -unsigned long size); +efi_status_t efi_setup_gop (struct screen_info *si); efi_status_t handle_cmdline_files(efi_loaded_image_t *image, const efi_char16_t *optstr,
drivers/firmware/efi/libstub/gop.c +19 -38
96 lines not on this page · raw message
drivers/firmware/efi/libstub/x86-stub.c +2 -15
@@ -406,24 +406,11 @@ static void setup_quirks(struct boot_params *boot_params)static void setup_graphics (struct boot_params *boot_params) { -efi_guid_t graphics_proto =EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID ; -struct screen_info *si; -efi_status_t status; -unsigned long size; -void **gop_handle =NULL ; - - si = &boot_params->screen_info; -memset (si,0 ,sizeof (*si)); - - size =0 ; - status =efi_bs_call (locate_handle,EFI_LOCATE_BY_PROTOCOL , - &graphics_proto,NULL , &size, gop_handle); -if (status ==EFI_BUFFER_TOO_SMALL ) - status =efi_setup_gop (si, &graphics_proto, size); +struct screen_info *si =memset (&boot_params->screen_info,0 ,sizeof (*si)); +efi_setup_gop (si); } -static void __noreturn efi_exit (efi_handle_t handle,efi_status_t status) {efi_bs_call (exit, handle, status,0 ,NULL );
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
Re: [PATCH 3/7] efi/libstub: Simplify GOP handling code
Quoted 3 lines · Ard Biesheuvel
Use the
LocateHandleBuffer()API and a__free()function to simplify the logic that allocates a handle buffer to iterate over all GOP protocols in the EFI database.
You previously rejected use of LocateHandleBuffer because you saw a risk of regressing older EFI versions, so that's a surprising move:
https://lore.kernel.org/r/CAKv+Gu_kfnHfiBH__Wnvh39KMPj_-s39YyY=pT3roNv7iPPzrA@mail.gmail.com/
Thanks,
Lukas
Ard Biesheuvel
maintainerRe: [PATCH 3/7] efi/libstub: Simplify GOP handling code
Quoted 11 lines · Lukas Wunner
Quoted 3 lines · Ard Biesheuvel
Use the
LocateHandleBuffer()API and a__free()function to simplify the logic that allocates a handle buffer to iterate over all GOP protocols in the EFI database.You previously rejected use of LocateHandleBuffer because you saw a risk of regressing older EFI versions, so that's a surprising move:
https://lore.kernel.org/r/CAKv+Gu_kfnHfiBH__Wnvh39KMPj_-s39YyY=pT3roNv7iPPzrA@mail.gmail.com/
'previously' being more than 8 years ago ...
PATCH 4/7 efi/libstub: Refactor and cleanup GOP resolution picker code
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The EFI stub implements various ways of setting the resolution of the EFI framebuffer at boot, which duplicate a lot of boilerplate for iterating over the supported modes and extracting the resolution and color depth.
Refactor this into a single helper that takes a callback, and use it for the 'auto', 'list' and 'res' selection methods.
--- drivers/firmware/efi/libstub/gop.c | 265 ++++++++------------ 1 file changed, 103 insertions(+), 162 deletions(-)
drivers/firmware/efi/libstub/gop.c +103 -162
@@ -133,13 +133,11 @@ void efi_parse_option_graphics(char *option)static u32 choose_mode_modenum (efi_graphics_output_protocol_t *gop) { -efi_status_t status; - + efi_graphics_output_mode_info_t *info __free(efi_pool) = NULL;efi_graphics_output_protocol_mode_t *mode; -efi_graphics_output_mode_info_t *info;unsigned long info_size; -u32 max_mode, cur_mode; +efi_status_t status;int pf; mode =efi_table_attr (gop, mode);
@@ -154,17 +152,13 @@ static u32 choose_mode_modenum(efi_graphics_output_protocol_t *gop)return cur_mode; } - status =efi_call_proto (gop, query_mode, cmdline.mode, - &info_size, &info); + status =efi_call_proto (gop, query_mode, cmdline.mode, &info_size, &info);if (status !=EFI_SUCCESS ) {efi_err ("Couldn't get mode information\n" );return cur_mode; } pf = info->pixel_format; - -efi_bs_call (free_pool, info); -if (pf ==PIXEL_BLT_ONLY || pf >=PIXEL_FORMAT_MAX ) {efi_err ("Invalid PixelFormat\n" );return cur_mode;
@@ -173,6 +167,28 @@ static u32 choose_mode_modenum(efi_graphics_output_protocol_t *gop)return cmdline.mode; } +static u32 choose_mode (efi_graphics_output_protocol_t *gop, +bool (*match)(const efi_graphics_output_mode_info_t *,u32 ,void *), +void *ctx) +{ +efi_graphics_output_protocol_mode_t *mode =efi_table_attr (gop, mode); +u32 max_mode =efi_table_attr (mode, max_mode); + +for (u32 m =0 ; m < max_mode; m++) { + efi_graphics_output_mode_info_t *info __free(efi_pool) = NULL; +unsigned long info_size; +efi_status_t status; + + status =efi_call_proto (gop, query_mode, m, &info_size, &info); +if (status !=EFI_SUCCESS ) +continue ; + +if (match (info, m, ctx)) +return m; + } +return (unsigned long )ctx; +} +static u8 pixel_bpp (int pixel_format,efi_pixel_bitmask_t pixel_info) {if (pixel_format ==PIXEL_BIT_MASK ) {
@@ -185,192 +201,117 @@ static u8 pixel_bpp(int pixel_format, efi_pixel_bitmask_t pixel_info)return 32 ; } -static u32 choose_mode_res (efi_graphics_output_protocol_t *gop) +static bool match_res (const efi_graphics_output_mode_info_t *info,u32 mode,void *ctx) { -efi_status_t status; +efi_pixel_bitmask_t pi = info->pixel_information; +int pf = info->pixel_format; -efi_graphics_output_protocol_mode_t *mode; -efi_graphics_output_mode_info_t *info; -unsigned long info_size; - -u32 max_mode, cur_mode; -int pf; -efi_pixel_bitmask_t pi; -u32 m, w, h; +if (pf ==PIXEL_BLT_ONLY || pf >=PIXEL_FORMAT_MAX ) +return false; - mode =efi_table_attr (gop, mode); +return cmdline.res.width == info->horizontal_resolution && + cmdline.res.height == info->vertical_resolution && + (cmdline.res.format <0 || cmdline.res.format == pf) && + (!cmdline.res.depth || cmdline.res.depth ==pixel_bpp (pf, pi)); +} - cur_mode =efi_table_attr (mode, mode); - info =efi_table_attr (mode, info); - pf = info->pixel_format; - pi = info->pixel_information; - w = info->horizontal_resolution; - h = info->vertical_resolution; +static u32 choose_mode_res (efi_graphics_output_protocol_t *gop) +{ +efi_graphics_output_protocol_mode_t *mode =efi_table_attr (gop, mode); +unsigned long cur_mode =efi_table_attr (mode, mode); -if (w == cmdline.res.width && h == cmdline.res.height && - (cmdline.res.format <0 || cmdline.res.format == pf) && - (!cmdline.res.depth || cmdline.res.depth ==pixel_bpp (pf, pi))) +if (match_res (efi_table_attr (mode, info), cur_mode,NULL ))return cur_mode; - max_mode =efi_table_attr (mode, max_mode); - -for (m =0 ; m < max_mode; m++) { -if (m == cur_mode) -continue ; - - status =efi_call_proto (gop, query_mode, m, - &info_size, &info); -if (status !=EFI_SUCCESS ) -continue ; +return choose_mode (gop, match_res, (void *)cur_mode); +} - pf = info->pixel_format; - pi = info->pixel_information; - w = info->horizontal_resolution; - h = info->vertical_resolution; +struct match { +u32 mode; +u32 area; +u8 depth; +}; -efi_bs_call (free_pool, info); +static bool match_auto (const efi_graphics_output_mode_info_t *info,u32 mode,void *ctx) +{ +u32 area = info->horizontal_resolution * info->vertical_resolution; +efi_pixel_bitmask_t pi = info->pixel_information; +int pf = info->pixel_format; +u8 depth =pixel_bpp (pf, pi); +struct match *m = ctx; -if (pf ==PIXEL_BLT_ONLY || pf >=PIXEL_FORMAT_MAX ) -continue ; -if (w == cmdline.res.width && h == cmdline.res.height && - (cmdline.res.format <0 || cmdline.res.format == pf) && - (!cmdline.res.depth || cmdline.res.depth ==pixel_bpp (pf, pi))) -return m; - } +if (pf ==PIXEL_BLT_ONLY || pf >=PIXEL_FORMAT_MAX ) +return false; -efi_err ("Couldn't find requested mode\n" ); +if (area > m->area || (area == m->area && depth > m->depth)) + *m = (struct match ){ mode, area, depth }; -return cur_mode; +return false; }static u32 choose_mode_auto (efi_graphics_output_protocol_t *gop) { -efi_status_t status; +struct match match = {}; -efi_graphics_output_protocol_mode_t *mode; -efi_graphics_output_mode_info_t *info; -unsigned long info_size; +choose_mode (gop, match_auto, &match); -u32 max_mode, cur_mode, best_mode, area; -u8 depth; -int pf; -efi_pixel_bitmask_t pi; -u32 m, w, h, a; -u8 d; - - mode =efi_table_attr (gop, mode); - - cur_mode =efi_table_attr (mode, mode); - max_mode =efi_table_attr (mode, max_mode); - - info =efi_table_attr (mode, info); - - pf = info->pixel_format; - pi = info->pixel_information; - w = info->horizontal_resolution; - h = info->vertical_resolution; - - best_mode = cur_mode; - area = w * h; - depth =pixel_bpp (pf, pi); - -for (m =0 ; m < max_mode; m++) { -if (m == cur_mode) -continue ; - - status =efi_call_proto (gop, query_mode, m, - &info_size, &info); -if (status !=EFI_SUCCESS ) -continue ; +return match.mode; +} - pf = info->pixel_format; - pi = info->pixel_information; - w = info->horizontal_resolution; - h = info->vertical_resolution; +static bool match_list (const efi_graphics_output_mode_info_t *info,u32 mode,void *ctx) +{ +efi_pixel_bitmask_t pi = info->pixel_information; +u32 cur_mode = (unsigned long )ctx; +int pf = info->pixel_format; +const char *dstr; +bool valid; +u8 depth; -efi_bs_call (free_pool, info); + valid = !(pf ==PIXEL_BLT_ONLY || pf >=PIXEL_FORMAT_MAX ); -if (pf ==PIXEL_BLT_ONLY || pf >=PIXEL_FORMAT_MAX ) -continue ; - a = w * h; -if (a < area) -continue ; - d =pixel_bpp (pf, pi); -if (a > area || d > depth) { - best_mode = m; - area = a; - depth = d; - } +switch (pf) { +case PIXEL_RGB_RESERVED_8BIT_PER_COLOR : + dstr ="rgb" ; +break ; +case PIXEL_BGR_RESERVED_8BIT_PER_COLOR : + dstr ="bgr" ; +break ; +case PIXEL_BIT_MASK : + dstr ="" ; + depth =pixel_bpp (pf, pi); +break ; +case PIXEL_BLT_ONLY : + dstr ="blt" ; +break ; +default : + dstr ="xxx" ; +break ; } -return best_mode; +efi_printk ("Mode %3u %c%c: Resolution %ux%u-%s%.0hhu\n" , + mode, + (mode == cur_mode) ?'*' :' ' , + !valid ?'-' :' ' , + info->horizontal_resolution, + info->vertical_resolution, + dstr, depth); + +return false; }static u32 choose_mode_list (efi_graphics_output_protocol_t *gop) { -efi_status_t status; - -efi_graphics_output_protocol_mode_t *mode; -efi_graphics_output_mode_info_t *info; -unsigned long info_size; - -u32 max_mode, cur_mode; -int pf; -efi_pixel_bitmask_t pi; -u32 m, w, h; -u8 d; -const char *dstr; -bool valid; +efi_graphics_output_protocol_mode_t *mode =efi_table_attr (gop, mode); +unsigned long cur_mode =efi_table_attr (mode, mode); +u32 max_mode =efi_table_attr (mode, max_mode);efi_input_key_t key; - - mode =efi_table_attr (gop, mode); - - cur_mode =efi_table_attr (mode, mode); - max_mode =efi_table_attr (mode, max_mode); +efi_status_t status;efi_printk ("Available graphics modes are 0-%u\n" , max_mode-1 );efi_puts (" * = current mode\n" " - = unusable mode\n" ); -for (m =0 ; m < max_mode; m++) { - status =efi_call_proto (gop, query_mode, m, - &info_size, &info); -if (status !=EFI_SUCCESS ) -continue ; - pf = info->pixel_format; - pi = info->pixel_information; - w = info->horizontal_resolution; - h = info->vertical_resolution; - -efi_bs_call (free_pool, info); - - valid = !(pf ==PIXEL_BLT_ONLY || pf >=PIXEL_FORMAT_MAX ); - d =0 ; -switch (pf) { -case PIXEL_RGB_RESERVED_8BIT_PER_COLOR : - dstr ="rgb" ; -break ; -case PIXEL_BGR_RESERVED_8BIT_PER_COLOR : - dstr ="bgr" ; -break ; -case PIXEL_BIT_MASK : - dstr ="" ; - d =pixel_bpp (pf, pi); -break ; -case PIXEL_BLT_ONLY : - dstr ="blt" ; -break ; -default : - dstr ="xxx" ; -break ; - } - -efi_printk ("Mode %3u %c%c: Resolution %ux%u-%s%.0hhu\n" , - m, - m == cur_mode ?'*' :' ' , - !valid ?'-' :' ' , - w, h, dstr, d); - } +choose_mode (gop, match_list, (void *)cur_mode);efi_puts ("\nPress any key to continue (or wait 10 seconds)\n" ); status =efi_wait_for_key (10 *EFI_USEC_PER_SEC , &key);
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
PATCH 5/7 efi/libstub: Simplify PCI I/O handle buffer traversal
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Use LocateHandleBuffer() and a __free() cleanup helper to simplify the PCI I/O handle buffer traversal code.
--- drivers/firmware/efi/libstub/pci.c | 33 +++++--------------- drivers/firmware/efi/libstub/x86-stub.c | 29 ++++------------- 2 files changed, 13 insertions(+), 49 deletions(-)
drivers/firmware/efi/libstub/pci.c +7 -26
@@ -16,36 +16,20 @@void efi_pci_disable_bridge_busmaster (void ) {efi_guid_t pci_proto =EFI_PCI_IO_PROTOCOL_GUID ; -unsigned long pci_handle_size =0 ; -efi_handle_t *pci_handle =NULL ; + efi_handle_t *pci_handle __free(efi_pool) = NULL; +unsigned long pci_handle_num;efi_handle_t handle;efi_status_t status;u16 class, command; - status =efi_bs_call (locate_handle,EFI_LOCATE_BY_PROTOCOL , &pci_proto, -NULL , &pci_handle_size,NULL ); - -if (status !=EFI_BUFFER_TOO_SMALL ) { -if (status !=EFI_SUCCESS && status !=EFI_NOT_FOUND ) -efi_err ("Failed to locate PCI I/O handles'\n" ); -return ; - } - - status =efi_bs_call (allocate_pool,EFI_LOADER_DATA , pci_handle_size, - (void **)&pci_handle); -if (status !=EFI_SUCCESS ) { -efi_err ("Failed to allocate memory for 'pci_handle'\n" ); -return ; - } - - status =efi_bs_call (locate_handle,EFI_LOCATE_BY_PROTOCOL , &pci_proto, -NULL , &pci_handle_size, pci_handle); + status =efi_bs_call (locate_handle_buffer,EFI_LOCATE_BY_PROTOCOL , + &pci_proto,NULL , &pci_handle_num, pci_handle);if (status !=EFI_SUCCESS ) {efi_err ("Failed to locate PCI I/O handles'\n" ); - goto free_handle; +return ; } -for_each_efi_handle (handle, pci_handle,efi_get_handle_num (pci_handle_size)) { +for_each_efi_handle (handle, pci_handle, pci_handle_num) {efi_pci_io_protocol_t *pci;unsigned long segment_nr, bus_nr, device_nr, func_nr;
@@ -81,7 +65,7 @@ void efi_pci_disable_bridge_busmaster(void)efi_bs_call (disconnect_controller, handle,NULL ,NULL ); } -for_each_efi_handle (handle, pci_handle,efi_get_handle_num (pci_handle_size)) { +for_each_efi_handle (handle, pci_handle, pci_handle_num) {efi_pci_io_protocol_t *pci; status = efi_bs_call(handle_protocol, handle, &pci_proto,
@@ -107,7 +91,4 @@ void efi_pci_disable_bridge_busmaster(void)if (status !=EFI_SUCCESS )efi_err ("Failed to disable PCI busmastering\n" ); } - -free_handle: -efi_bs_call (free_pool, pci_handle); }
drivers/firmware/efi/libstub/x86-stub.c +6 -23
@@ -119,37 +119,23 @@ preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)static void setup_efi_pci (struct boot_params *params) {efi_status_t status; -void **pci_handle =NULL ; + efi_handle_t *pci_handle __free(efi_pool) = NULL;efi_guid_t pci_proto =EFI_PCI_IO_PROTOCOL_GUID ; -unsigned long size =0 ;struct setup_data *data; +unsigned long num;efi_handle_t h; - status =efi_bs_call (locate_handle,EFI_LOCATE_BY_PROTOCOL , - &pci_proto,NULL , &size, pci_handle); - -if (status ==EFI_BUFFER_TOO_SMALL ) { - status =efi_bs_call (allocate_pool,EFI_LOADER_DATA , size, - (void **)&pci_handle); - -if (status !=EFI_SUCCESS ) { -efi_err ("Failed to allocate memory for 'pci_handle'\n" ); -return ; - } - - status =efi_bs_call (locate_handle,EFI_LOCATE_BY_PROTOCOL , - &pci_proto,NULL , &size, pci_handle); - } - + status =efi_bs_call (locate_handle_buffer,EFI_LOCATE_BY_PROTOCOL , + &pci_proto,NULL , &num, pci_handle);if (status !=EFI_SUCCESS ) - goto free_handle; +return ; data = (struct setup_data *)(unsigned long )params->hdr.setup_data;while (data && data->next) data = (struct setup_data *)(unsigned long )data->next; -for_each_efi_handle (h, pci_handle,efi_get_handle_num (size)) { +for_each_efi_handle (h, pci_handle, num) {efi_pci_io_protocol_t *pci =NULL ;struct pci_setup_rom *rom;
@@ -169,9 +155,6 @@ static void setup_efi_pci(struct boot_params *params) data = (struct setup_data *)rom; } - -free_handle: -efi_bs_call (free_pool, pci_handle); }static void retrieve_apple_device_properties (struct boot_params *boot_params)
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
PATCH 6/7 efi/libstub: Use cleanup helpers for freeing copies of the memory map
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The EFI stub may obtain the memory map from the firmware numerous times, and this involves doing a EFI pool allocation first, which needs to be freed after use.
Streamline this using a cleanup helper, which makes the code easier to follow.
--- drivers/firmware/efi/libstub/kaslr.c | 4 +--- drivers/firmware/efi/libstub/mem.c | 20 ++++++++------------ drivers/firmware/efi/libstub/randomalloc.c | 4 +--- drivers/firmware/efi/libstub/relocate.c | 10 ++++------ drivers/firmware/efi/libstub/x86-stub.c | 11 ++++++----- 5 files changed, 20 insertions(+), 29 deletions(-)
drivers/firmware/efi/libstub/kaslr.c +1 -3
@@ -57,7 +57,7 @@ u32 efi_kaslr_get_phys_seed(efi_handle_t image_handle)*/ static bool check_image_region (u64 base,u64 size) { -struct efi_boot_memmap *map; + struct efi_boot_memmap *map __free(efi_pool) = NULL;efi_status_t status;bool ret = false;int map_offset;
@@ -80,8 +80,6 @@ static bool check_image_region(u64 base, u64 size) } } -efi_bs_call (free_pool, map); -return ret; }
drivers/firmware/efi/libstub/mem.c +8 -12
@@ -20,10 +20,10 @@efi_status_t efi_get_memory_map (struct efi_boot_memmap **map,bool install_cfg_tbl) { + struct efi_boot_memmap tmp, *m __free(efi_pool) = NULL;int memtype = install_cfg_tbl ?EFI_ACPI_RECLAIM_MEMORY :EFI_LOADER_DATA ;efi_guid_t tbl_guid =LINUX_EFI_BOOT_MEMMAP_GUID ; -struct efi_boot_memmap *m, tmp;efi_status_t status;unsigned long size;
@@ -48,24 +48,20 @@ efi_status_t efi_get_memory_map(struct efi_boot_memmap **map,*/ status =efi_bs_call (install_configuration_table, &tbl_guid, m);if (status !=EFI_SUCCESS ) - goto free_map; +return status; } m->buff_size = m->map_size = size; status =efi_bs_call (get_memory_map, &m->map_size, m->map, &m->map_key, &m->desc_size, &m->desc_ver); -if (status !=EFI_SUCCESS ) - goto uninstall_table; +if (status !=EFI_SUCCESS ) { +if (install_cfg_tbl) +efi_bs_call (install_configuration_table, &tbl_guid,NULL ); +return status; + } - *map = m; + *map =no_free_ptr (m);return EFI_SUCCESS ; - -uninstall_table: -if (install_cfg_tbl) -efi_bs_call (install_configuration_table, &tbl_guid,NULL ); -free_map: -efi_bs_call (free_pool, m); -return status; }/**
drivers/firmware/efi/libstub/randomalloc.c +1 -3
@@ -59,9 +59,9 @@ efi_status_t efi_random_alloc(unsigned long size,unsigned long alloc_min, unsigned long alloc_max) { + struct efi_boot_memmap *map __free(efi_pool) = NULL;unsigned long total_slots =0 , target_slot;unsigned long total_mirrored_slots =0 ; -struct efi_boot_memmap *map;efi_status_t status;int map_offset;
@@ -130,7 +130,5 @@ efi_status_t efi_random_alloc(unsigned long size,break ; } -efi_bs_call (free_pool, map); -return status; }
drivers/firmware/efi/libstub/relocate.c +4 -6
@@ -23,14 +23,14 @@efi_status_t efi_low_alloc_above (unsigned long size,unsigned long align,unsigned long *addr,unsigned long min) { -struct efi_boot_memmap *map; + struct efi_boot_memmap *map __free(efi_pool) = NULL;efi_status_t status;unsigned long nr_pages;int i; status =efi_get_memory_map (&map, false);if (status !=EFI_SUCCESS ) - goto fail; +return status;/* * Enforce minimum alignment that EFI or Linux requires when
@@ -79,11 +79,9 @@ efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align, }if (i == map->map_size / map->desc_size) - status =EFI_NOT_FOUND ; +return EFI_NOT_FOUND ; -efi_bs_call (free_pool, map); -fail: -return status; +return EFI_SUCCESS ; }/**
drivers/firmware/efi/libstub/x86-stub.c +6 -5
@@ -616,7 +616,7 @@ static efi_status_t allocate_e820(struct boot_params *params,struct setup_data **e820ext, u32 *e820ext_size) { - struct efi_boot_memmap *map; + struct efi_boot_memmap *map __free(efi_pool) = NULL;efi_status_t status;__u32 nr_desc;
@@ -630,13 +630,14 @@ static efi_status_t allocate_e820(struct boot_params *params,EFI_MMAP_NR_SLACK_SLOTS ; status =alloc_e820ext (nr_e820ext, e820ext, e820ext_size); +if (status !=EFI_SUCCESS ) +return status; } -if (IS_ENABLED (CONFIG_UNACCEPTED_MEMORY ) && status ==EFI_SUCCESS ) - status =allocate_unaccepted_bitmap (nr_desc, map); +if (IS_ENABLED (CONFIG_UNACCEPTED_MEMORY )) +return allocate_unaccepted_bitmap (nr_desc, map); -efi_bs_call (free_pool, map); -return status; +return EFI_SUCCESS ; }struct exit_boot_struct {
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
PATCH 7/7 efi/libstub: Use __free() helper for pool deallocations
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Annotate some local buffer allocations as __free(efi_pool) and simplify the associated error handling accordingly. This removes a couple of gotos and simplifies the code.
--- drivers/firmware/efi/libstub/efi-stub-helper.c | 9 ++++----- drivers/firmware/efi/libstub/efi-stub.c | 21 ++++++++++---------- drivers/firmware/efi/libstub/x86-stub.c | 16 ++++++--------- 3 files changed, 20 insertions(+), 26 deletions(-)
drivers/firmware/efi/libstub/efi-stub-helper.c +4 -5
@@ -47,9 +47,10 @@ bool __pure __efi_soft_reserve_enabled(void)*/ efi_status_t efi_parse_options (char const *cmdline) { -size_t len; + char *buf __free(efi_pool) = NULL;efi_status_t status; -char *str, *buf; +size_t len; +char *str;if (!cmdline)return EFI_SUCCESS ;
@@ -102,7 +103,6 @@ efi_status_t efi_parse_options(char const *cmdline)efi_parse_option_graphics (val +strlen ("efifb:" )); } } -efi_bs_call (free_pool, buf);return EFI_SUCCESS ; }
@@ -250,7 +250,7 @@ static efi_status_t efi_measure_tagged_event(unsigned long load_addr, u64, const union efistub_event *);struct {u32 hash_log_extend_event; } mixed_mode; } method; -struct efistub_measured_event *evt; + struct efistub_measured_event *evt __free(efi_pool) = NULL;int size =struct_size (evt, tagged_event.tagged_event_data, events[event].event_data_len);efi_guid_t tcg2_guid =EFI_TCG2_PROTOCOL_GUID ;
@@ -312,7 +312,6 @@ static efi_status_t efi_measure_tagged_event(unsigned long load_addr, status =efi_fn_call (&method, hash_log_extend_event, protocol,0 , load_addr, load_size, &evt->event_data); -efi_bs_call (free_pool, evt);if (status ==EFI_SUCCESS )return EFI_SUCCESS ;
drivers/firmware/efi/libstub/efi-stub.c +10 -11
@@ -104,8 +104,8 @@ static u32 get_supported_rt_services(void)efi_status_t efi_handle_cmdline (efi_loaded_image_t *image,char **cmdline_ptr) { + char *cmdline __free(efi_pool) = NULL;efi_status_t status; -char *cmdline;/* * Get the command line from EFI, using the LOADED_IMAGE
@@ -120,25 +120,24 @@ efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr)if (!IS_ENABLED (CONFIG_CMDLINE_FORCE )) { status =efi_parse_options (cmdline); -if (status !=EFI_SUCCESS ) - goto fail_free_cmdline; +if (status !=EFI_SUCCESS ) { +efi_err ("Failed to parse EFI load options\n" ); +return status; + } }if (IS_ENABLED (CONFIG_CMDLINE_EXTEND ) ||IS_ENABLED (CONFIG_CMDLINE_FORCE ) || cmdline[0 ] ==0 ) { status =efi_parse_options (CONFIG_CMDLINE ); -if (status !=EFI_SUCCESS ) - goto fail_free_cmdline; +if (status !=EFI_SUCCESS ) { +efi_err ("Failed to parse built-in command line\n" ); +return status; + } } - *cmdline_ptr = cmdline; + *cmdline_ptr =no_free_ptr (cmdline);return EFI_SUCCESS ; - -fail_free_cmdline: -efi_err ("Failed to parse options\n" ); -efi_bs_call (free_pool, cmdline); -return status; } efi_status_t efi_stub_common(efi_handle_t handle,
drivers/firmware/efi/libstub/x86-stub.c +6 -10
@@ -42,7 +42,7 @@ union sev_memory_acceptance_protocol {static efi_status_t preserve_pci_rom_image (efi_pci_io_protocol_t *pci,struct pci_setup_rom **__rom) { -struct pci_setup_rom *rom =NULL ; + struct pci_setup_rom *rom __free(efi_pool) = NULL;efi_status_t status;unsigned long size;uint64_t romsize;
@@ -75,14 +75,13 @@ preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom) rom->data.len = size -sizeof (struct setup_data ); rom->data.next =0 ; rom->pcilen = romsize; - *__rom = rom; status =efi_call_proto (pci, pci.read, EfiPciIoWidthUint16,PCI_VENDOR_ID ,1 , &rom->vendor);if (status !=EFI_SUCCESS ) {efi_err ("Failed to read rom->vendor\n" ); - goto free_struct; +return status; } status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
@@ -90,21 +89,18 @@ preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)if (status !=EFI_SUCCESS ) {efi_err ("Failed to read rom->devid\n" ); - goto free_struct; +return status; } status =efi_call_proto (pci, get_location, &rom->segment, &rom->bus, &rom->device, &rom->function);if (status !=EFI_SUCCESS ) - goto free_struct; +return status;memcpy (rom->romdata, romimage, romsize); -return status; - -free_struct: -efi_bs_call (free_pool, rom); -return status; + *__rom =no_free_ptr (rom); +return EFI_SUCCESS ; }/*
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog