Skip to content

Commit

Permalink
efi/libstub: Use C99-style for loop to traverse handle buffer
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ardbiesheuvel committed Jan 14, 2025
1 parent 144d52d commit c14bca3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
9 changes: 4 additions & 5 deletions drivers/firmware/efi/libstub/efistub.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,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)
Expand Down
3 changes: 1 addition & 2 deletions drivers/firmware/efi/libstub/gop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions drivers/firmware/efi/libstub/pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions drivers/firmware/efi/libstub/x86-stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;

Expand Down

0 comments on commit c14bca3

Please sign in to comment.