History

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.

Ard Biesheuvel committed 6 years ago

Commit 2732ea01 parent a8147db3 files · +20 −16Browse files

Unified · Split

3 files
  1. arch/x86/boot/compressed/eboot.c +5 −9
  2. drivers/firmware/efi/libstub/gop.c +2 −7
  3. include/linux/efi.h +13 −0
arch/x86/boot/compressed/eboot.c+5−9
@@ -135,6 +135,7 @@ static void setup_efi_pci(struct boot_params *params) · 134 unmodified lines
135 unsigned long size = 0;135 unsigned long size = 0;
136 unsigned long nr_pci;136 unsigned long nr_pci;
137 struct setup_data *data;137 struct setup_data *data;
138 efi_handle_t h;
138 int i;139 int i;
139 140
140 status = efi_call_early(locate_handle,141 status = efi_call_early(locate_handle,
@@ -164,14 +165,11 @@ static void setup_efi_pci(struct boot_params *params) · 23 unmodified lines
164 while (data && data->next)165 while (data && data->next)
165 data = (struct setup_data *)(unsigned long)data->next;166 data = (struct setup_data *)(unsigned long)data->next;
166 167
167 nr_pci = size / (efi_is_64bit() ? sizeof(u64) : sizeof(u32));168 for_each_efi_handle(h, pci_handle, size, i) {
168 for (i = 0; i < nr_pci; i++) {
169 efi_pci_io_protocol_t *pci = NULL;169 efi_pci_io_protocol_t *pci = NULL;
170 struct pci_setup_rom *rom;170 struct pci_setup_rom *rom;
171 171
172 status = efi_call_early(handle_protocol,172 status = efi_call_early(handle_protocol, h,
173 efi_is_64bit() ? ((u64 *)pci_handle)[i]
174 : ((u32 *)pci_handle)[i],
175 &pci_proto, (void **)&pci);173 &pci_proto, (void **)&pci);
176 if (status != EFI_SUCCESS || !pci)174 if (status != EFI_SUCCESS || !pci)
177 continue;175 continue;
@@ -266,6 +264,7 @@ setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size) · 88 unmodified lines
266 void **uga_handle = NULL;264 void **uga_handle = NULL;
267 efi_uga_draw_protocol_t *uga = NULL, *first_uga;265 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
268 unsigned long nr_ugas;266 unsigned long nr_ugas;
267 efi_handle_t handle;
269 int i;268 int i;
270 269
271 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,270 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) · 11 unmodified lines
283 width = 0;282 width = 0;
284 283
285 first_uga = NULL;284 first_uga = NULL;
286 nr_ugas = size / (efi_is_64bit() ? sizeof(u64) : sizeof(u32));285 for_each_efi_handle(handle, uga_handle, size, i) {
287 for (i = 0; i < nr_ugas; i++) {
288 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;286 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
289 u32 w, h, depth, refresh;287 u32 w, h, depth, refresh;
290 void *pciio;288 void *pciio;
291 unsigned long handle = efi_is_64bit() ? ((u64 *)uga_handle)[i]
292 : ((u32 *)uga_handle)[i];
293 289
294 status = efi_call_early(handle_protocol, handle,290 status = efi_call_early(handle_protocol, handle,
295 uga_proto, (void **)&uga);291 uga_proto, (void **)&uga);
drivers/firmware/efi/libstub/gop.c+2−7
@@ -91,7 +91,6 @@ setup_gop(efi_system_table_t *sys_table_arg, struct screen_info *si, · 90 unmodified lines
91 efi_guid_t *proto, unsigned long size, void **handles)91 efi_guid_t *proto, unsigned long size, void **handles)
92{92{
93 efi_graphics_output_protocol_t *gop, *first_gop;93 efi_graphics_output_protocol_t *gop, *first_gop;
94 unsigned long nr_gops;
95 u16 width, height;94 u16 width, height;
96 u32 pixels_per_scan_line;95 u32 pixels_per_scan_line;
97 u32 ext_lfb_base;96 u32 ext_lfb_base;
@@ -99,22 +98,18 @@ setup_gop(efi_system_table_t *sys_table_arg, struct screen_info *si, · 1 unmodified line
99 efi_pixel_bitmask_t pixel_info;98 efi_pixel_bitmask_t pixel_info;
100 int pixel_format;99 int pixel_format;
101 efi_status_t status;100 efi_status_t status;
101 efi_handle_t h;
102 int i;102 int i;
103 bool is64 = efi_is_64bit();
104 103
105 first_gop = NULL;104 first_gop = NULL;
106 gop = NULL;105 gop = NULL;
107 106
108 nr_gops = size / (is64 ? sizeof(u64) : sizeof(u32));107 for_each_efi_handle(h, handles, size, i) {
109 for (i = 0; i < nr_gops; i++) {
110 efi_graphics_output_protocol_mode_t *mode;108 efi_graphics_output_protocol_mode_t *mode;
111 efi_graphics_output_mode_info_t *info = NULL;109 efi_graphics_output_mode_info_t *info = NULL;
112 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;110 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
113 bool conout_found = false;111 bool conout_found = false;
114 void *dummy = NULL;112 void *dummy = NULL;
115 efi_handle_t h = (efi_handle_t)(unsigned long)
116 (is64 ? ((u64 *)handles)[i]
117 : ((u32 *)handles)[i]);
118 efi_physical_addr_t current_fb_base;113 efi_physical_addr_t current_fb_base;
119 114
120 status = efi_call_early(handle_protocol, h,115 status = efi_call_early(handle_protocol, h,
include/linux/efi.h+13−0
@@ -48,6 +48,19 @@ typedef u16 efi_char16_t; /* UNICODE character */ · 47 unmodified lines
48typedef u64 efi_physical_addr_t;48typedef u64 efi_physical_addr_t;
49typedef void *efi_handle_t;49typedef void *efi_handle_t;
50 50
51#define efi_get_handle_at(array, idx) \
52 (efi_is_64bit() ? (efi_handle_t)(unsigned long)((u64 *)(array))[idx] \
53 : (efi_handle_t)(unsigned long)((u32 *)(array))[idx])
54
55#define efi_get_handle_num(size) \
56 ((size) / (efi_is_64bit() ? sizeof(u64) : sizeof(u32)))
57
58#define for_each_efi_handle(handle, array, size, i) \
59 for (i = 0; \
60 i < efi_get_handle_num(size) && \
61 ((handle = efi_get_handle_at((array), i)) || true); \
62 i++)
63
51/*64/*
52 * The UEFI spec and EDK2 reference implementation both define EFI_GUID as65 * The UEFI spec and EDK2 reference implementation both define EFI_GUID as
53 * struct { u32 a; u16; b; u16 c; u8 d[8]; }; and so the implied alignment66 * struct { u32 a; u16; b; u16 c; u8 d[8]; }; and so the implied alignment