Skip to content

Commit 90534e6

Browse files
committed
efi/libstub: Simplify PCI I/O handle buffer traversal
Use LocateHandleBuffer() and a __free() cleanup helper to simplify the PCI I/O handle buffer traversal code. Signed-off-by: Ard Biesheuvel <[email protected]>
1 parent b52587c commit 90534e6

File tree

2 files changed

+13
-49
lines changed

2 files changed

+13
-49
lines changed

drivers/firmware/efi/libstub/pci.c

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,20 @@
1616
void efi_pci_disable_bridge_busmaster(void)
1717
{
1818
efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
19-
unsigned long pci_handle_size = 0;
20-
efi_handle_t *pci_handle = NULL;
19+
efi_handle_t *pci_handle __free(efi_pool) = NULL;
20+
unsigned long pci_handle_num;
2121
efi_handle_t handle;
2222
efi_status_t status;
2323
u16 class, command;
2424

25-
status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, &pci_proto,
26-
NULL, &pci_handle_size, NULL);
27-
28-
if (status != EFI_BUFFER_TOO_SMALL) {
29-
if (status != EFI_SUCCESS && status != EFI_NOT_FOUND)
30-
efi_err("Failed to locate PCI I/O handles'\n");
31-
return;
32-
}
33-
34-
status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, pci_handle_size,
35-
(void **)&pci_handle);
25+
status = efi_bs_call(locate_handle_buffer, EFI_LOCATE_BY_PROTOCOL,
26+
&pci_proto, NULL, &pci_handle_num, &pci_handle);
3627
if (status != EFI_SUCCESS) {
37-
efi_err("Failed to allocate memory for 'pci_handle'\n");
28+
efi_err("Failed to locate PCI I/O handles\n");
3829
return;
3930
}
4031

41-
status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, &pci_proto,
42-
NULL, &pci_handle_size, pci_handle);
43-
if (status != EFI_SUCCESS) {
44-
efi_err("Failed to locate PCI I/O handles'\n");
45-
goto free_handle;
46-
}
47-
48-
for_each_efi_handle(handle, pci_handle, efi_get_handle_num(pci_handle_size)) {
32+
for_each_efi_handle(handle, pci_handle, pci_handle_num) {
4933
efi_pci_io_protocol_t *pci;
5034
unsigned long segment_nr, bus_nr, device_nr, func_nr;
5135

@@ -81,7 +65,7 @@ void efi_pci_disable_bridge_busmaster(void)
8165
efi_bs_call(disconnect_controller, handle, NULL, NULL);
8266
}
8367

84-
for_each_efi_handle(handle, pci_handle, efi_get_handle_num(pci_handle_size)) {
68+
for_each_efi_handle(handle, pci_handle, pci_handle_num) {
8569
efi_pci_io_protocol_t *pci;
8670

8771
status = efi_bs_call(handle_protocol, handle, &pci_proto,
@@ -107,7 +91,4 @@ void efi_pci_disable_bridge_busmaster(void)
10791
if (status != EFI_SUCCESS)
10892
efi_err("Failed to disable PCI busmastering\n");
10993
}
110-
111-
free_handle:
112-
efi_bs_call(free_pool, pci_handle);
11394
}

drivers/firmware/efi/libstub/x86-stub.c

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -119,37 +119,23 @@ preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
119119
static void setup_efi_pci(struct boot_params *params)
120120
{
121121
efi_status_t status;
122-
void **pci_handle = NULL;
122+
efi_handle_t *pci_handle __free(efi_pool) = NULL;
123123
efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
124-
unsigned long size = 0;
125124
struct setup_data *data;
125+
unsigned long num;
126126
efi_handle_t h;
127127

128-
status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
129-
&pci_proto, NULL, &size, pci_handle);
130-
131-
if (status == EFI_BUFFER_TOO_SMALL) {
132-
status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
133-
(void **)&pci_handle);
134-
135-
if (status != EFI_SUCCESS) {
136-
efi_err("Failed to allocate memory for 'pci_handle'\n");
137-
return;
138-
}
139-
140-
status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
141-
&pci_proto, NULL, &size, pci_handle);
142-
}
143-
128+
status = efi_bs_call(locate_handle_buffer, EFI_LOCATE_BY_PROTOCOL,
129+
&pci_proto, NULL, &num, &pci_handle);
144130
if (status != EFI_SUCCESS)
145-
goto free_handle;
131+
return;
146132

147133
data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
148134

149135
while (data && data->next)
150136
data = (struct setup_data *)(unsigned long)data->next;
151137

152-
for_each_efi_handle(h, pci_handle, efi_get_handle_num(size)) {
138+
for_each_efi_handle(h, pci_handle, num) {
153139
efi_pci_io_protocol_t *pci = NULL;
154140
struct pci_setup_rom *rom;
155141

@@ -169,9 +155,6 @@ static void setup_efi_pci(struct boot_params *params)
169155

170156
data = (struct setup_data *)rom;
171157
}
172-
173-
free_handle:
174-
efi_bs_call(free_pool, pci_handle);
175158
}
176159

177160
static void retrieve_apple_device_properties(struct boot_params *boot_params)

0 commit comments

Comments
 (0)