Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

efi/libstub: Use C99-style for loop to traverse handle buffer

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.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>

+8 -12
+4 -5
drivers/firmware/efi/libstub/efistub.h
··· 122 122 #define efi_get_handle_num(size) \ 123 123 ((size) / (efi_is_native() ? sizeof(efi_handle_t) : sizeof(u32))) 124 124 125 - #define for_each_efi_handle(handle, array, size, i) \ 126 - for (i = 0; \ 127 - i < efi_get_handle_num(size) && \ 128 - ((handle = efi_get_handle_at((array), i)) || true); \ 129 - i++) 125 + #define for_each_efi_handle(handle, array, num) \ 126 + for (int __i = 0; __i < (num) && \ 127 + ((handle = efi_get_handle_at((array), __i)) || true); \ 128 + __i++) 130 129 131 130 static inline 132 131 void efi_set_u64_split(u64 data, u32 *lo, u32 *hi)
+1 -2
drivers/firmware/efi/libstub/gop.c
··· 466 466 { 467 467 efi_graphics_output_protocol_t *first_gop; 468 468 efi_handle_t h; 469 - int i; 470 469 471 470 first_gop = NULL; 472 471 473 - for_each_efi_handle(h, handles, size, i) { 472 + for_each_efi_handle(h, handles, efi_get_handle_num(size)) { 474 473 efi_status_t status; 475 474 476 475 efi_graphics_output_protocol_t *gop;
+2 -3
drivers/firmware/efi/libstub/pci.c
··· 21 21 efi_handle_t handle; 22 22 efi_status_t status; 23 23 u16 class, command; 24 - int i; 25 24 26 25 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, &pci_proto, 27 26 NULL, &pci_handle_size, NULL); ··· 45 46 goto free_handle; 46 47 } 47 48 48 - for_each_efi_handle(handle, pci_handle, pci_handle_size, i) { 49 + for_each_efi_handle(handle, pci_handle, efi_get_handle_num(pci_handle_size)) { 49 50 efi_pci_io_protocol_t *pci; 50 51 unsigned long segment_nr, bus_nr, device_nr, func_nr; 51 52 ··· 81 82 efi_bs_call(disconnect_controller, handle, NULL, NULL); 82 83 } 83 84 84 - for_each_efi_handle(handle, pci_handle, pci_handle_size, i) { 85 + for_each_efi_handle(handle, pci_handle, efi_get_handle_num(pci_handle_size)) { 85 86 efi_pci_io_protocol_t *pci; 86 87 87 88 status = efi_bs_call(handle_protocol, handle, &pci_proto,
+1 -2
drivers/firmware/efi/libstub/x86-stub.c
··· 124 124 unsigned long size = 0; 125 125 struct setup_data *data; 126 126 efi_handle_t h; 127 - int i; 128 127 129 128 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 130 129 &pci_proto, NULL, &size, pci_handle); ··· 149 150 while (data && data->next) 150 151 data = (struct setup_data *)(unsigned long)data->next; 151 152 152 - for_each_efi_handle(h, pci_handle, size, i) { 153 + for_each_efi_handle(h, pci_handle, efi_get_handle_num(size)) { 153 154 efi_pci_io_protocol_t *pci = NULL; 154 155 struct pci_setup_rom *rom; 155 156