Commit 2732ea0d authored by Ard Biesheuvel's avatar Ard Biesheuvel Committed by Ingo Molnar

efi/libstub: Use a helper to iterate over a EFI handle array

Iterating over a EFI handle array is a bit finicky, since we have
to take mixed mode into account, where handles are only 32-bit
while the native efi_handle_t type is 64-bit.

So introduce a helper, and replace the various occurrences of
this pattern.
Signed-off-by: default avatarArd Biesheuvel <ardb@kernel.org>
Cc: Arvind Sankar <nivedita@alum.mit.edu>
Cc: Borislav Petkov <bp@alien8.de>
Cc: James Morse <james.morse@arm.com>
Cc: Matt Fleming <matt@codeblueprint.co.uk>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-efi@vger.kernel.org
Link: https://lkml.kernel.org/r/20191224151025.32482-8-ardb@kernel.orgSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
parent a8147dba
...@@ -135,6 +135,7 @@ static void setup_efi_pci(struct boot_params *params) ...@@ -135,6 +135,7 @@ static void setup_efi_pci(struct boot_params *params)
unsigned long size = 0; unsigned long size = 0;
unsigned long nr_pci; unsigned long nr_pci;
struct setup_data *data; struct setup_data *data;
efi_handle_t h;
int i; int i;
status = efi_call_early(locate_handle, status = efi_call_early(locate_handle,
...@@ -164,14 +165,11 @@ static void setup_efi_pci(struct boot_params *params) ...@@ -164,14 +165,11 @@ static void setup_efi_pci(struct boot_params *params)
while (data && data->next) while (data && data->next)
data = (struct setup_data *)(unsigned long)data->next; data = (struct setup_data *)(unsigned long)data->next;
nr_pci = size / (efi_is_64bit() ? sizeof(u64) : sizeof(u32)); for_each_efi_handle(h, pci_handle, size, i) {
for (i = 0; i < nr_pci; i++) {
efi_pci_io_protocol_t *pci = NULL; efi_pci_io_protocol_t *pci = NULL;
struct pci_setup_rom *rom; struct pci_setup_rom *rom;
status = efi_call_early(handle_protocol, status = efi_call_early(handle_protocol, h,
efi_is_64bit() ? ((u64 *)pci_handle)[i]
: ((u32 *)pci_handle)[i],
&pci_proto, (void **)&pci); &pci_proto, (void **)&pci);
if (status != EFI_SUCCESS || !pci) if (status != EFI_SUCCESS || !pci)
continue; continue;
...@@ -266,6 +264,7 @@ setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size) ...@@ -266,6 +264,7 @@ setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
void **uga_handle = NULL; void **uga_handle = NULL;
efi_uga_draw_protocol_t *uga = NULL, *first_uga; efi_uga_draw_protocol_t *uga = NULL, *first_uga;
unsigned long nr_ugas; unsigned long nr_ugas;
efi_handle_t handle;
int i; int i;
status = efi_call_early(allocate_pool, EFI_LOADER_DATA, status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
...@@ -283,13 +282,10 @@ setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size) ...@@ -283,13 +282,10 @@ setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
width = 0; width = 0;
first_uga = NULL; first_uga = NULL;
nr_ugas = size / (efi_is_64bit() ? sizeof(u64) : sizeof(u32)); for_each_efi_handle(handle, uga_handle, size, i) {
for (i = 0; i < nr_ugas; i++) {
efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
u32 w, h, depth, refresh; u32 w, h, depth, refresh;
void *pciio; void *pciio;
unsigned long handle = efi_is_64bit() ? ((u64 *)uga_handle)[i]
: ((u32 *)uga_handle)[i];
status = efi_call_early(handle_protocol, handle, status = efi_call_early(handle_protocol, handle,
uga_proto, (void **)&uga); uga_proto, (void **)&uga);
......
...@@ -91,7 +91,6 @@ setup_gop(efi_system_table_t *sys_table_arg, struct screen_info *si, ...@@ -91,7 +91,6 @@ setup_gop(efi_system_table_t *sys_table_arg, struct screen_info *si,
efi_guid_t *proto, unsigned long size, void **handles) efi_guid_t *proto, unsigned long size, void **handles)
{ {
efi_graphics_output_protocol_t *gop, *first_gop; efi_graphics_output_protocol_t *gop, *first_gop;
unsigned long nr_gops;
u16 width, height; u16 width, height;
u32 pixels_per_scan_line; u32 pixels_per_scan_line;
u32 ext_lfb_base; u32 ext_lfb_base;
...@@ -99,22 +98,18 @@ setup_gop(efi_system_table_t *sys_table_arg, struct screen_info *si, ...@@ -99,22 +98,18 @@ setup_gop(efi_system_table_t *sys_table_arg, struct screen_info *si,
efi_pixel_bitmask_t pixel_info; efi_pixel_bitmask_t pixel_info;
int pixel_format; int pixel_format;
efi_status_t status; efi_status_t status;
efi_handle_t h;
int i; int i;
bool is64 = efi_is_64bit();
first_gop = NULL; first_gop = NULL;
gop = NULL; gop = NULL;
nr_gops = size / (is64 ? sizeof(u64) : sizeof(u32)); for_each_efi_handle(h, handles, size, i) {
for (i = 0; i < nr_gops; i++) {
efi_graphics_output_protocol_mode_t *mode; efi_graphics_output_protocol_mode_t *mode;
efi_graphics_output_mode_info_t *info = NULL; efi_graphics_output_mode_info_t *info = NULL;
efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID; efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
bool conout_found = false; bool conout_found = false;
void *dummy = NULL; void *dummy = NULL;
efi_handle_t h = (efi_handle_t)(unsigned long)
(is64 ? ((u64 *)handles)[i]
: ((u32 *)handles)[i]);
efi_physical_addr_t current_fb_base; efi_physical_addr_t current_fb_base;
status = efi_call_early(handle_protocol, h, status = efi_call_early(handle_protocol, h,
......
...@@ -48,6 +48,19 @@ typedef u16 efi_char16_t; /* UNICODE character */ ...@@ -48,6 +48,19 @@ typedef u16 efi_char16_t; /* UNICODE character */
typedef u64 efi_physical_addr_t; typedef u64 efi_physical_addr_t;
typedef void *efi_handle_t; typedef void *efi_handle_t;
#define efi_get_handle_at(array, idx) \
(efi_is_64bit() ? (efi_handle_t)(unsigned long)((u64 *)(array))[idx] \
: (efi_handle_t)(unsigned long)((u32 *)(array))[idx])
#define efi_get_handle_num(size) \
((size) / (efi_is_64bit() ? sizeof(u64) : 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++)
/* /*
* The UEFI spec and EDK2 reference implementation both define EFI_GUID as * The UEFI spec and EDK2 reference implementation both define EFI_GUID as
* struct { u32 a; u16; b; u16 c; u8 d[8]; }; and so the implied alignment * struct { u32 a; u16; b; u16 c; u8 d[8]; }; and so the implied alignment
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment