]>
Commit | Line | Data |
---|---|---|
f739fcd8 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
bee91169 | 2 | /* |
359a699a | 3 | * EFI application boot time services |
bee91169 | 4 | * |
359a699a | 5 | * Copyright (c) 2016 Alexander Graf |
bee91169 AG |
6 | */ |
7 | ||
bee91169 | 8 | #include <common.h> |
19763ea0 | 9 | #include <bootm.h> |
7d963323 | 10 | #include <div64.h> |
529441ca IA |
11 | #include <dm/device.h> |
12 | #include <dm/root.h> | |
bee91169 | 13 | #include <efi_loader.h> |
36bf446b | 14 | #include <irq_func.h> |
f7ae49fc | 15 | #include <log.h> |
bee91169 | 16 | #include <malloc.h> |
126a43f1 | 17 | #include <pe.h> |
19763ea0 | 18 | #include <time.h> |
3db71108 | 19 | #include <u-boot/crc.h> |
529441ca | 20 | #include <usb.h> |
bee91169 | 21 | #include <watchdog.h> |
401d1c4f | 22 | #include <asm/global_data.h> |
19763ea0 | 23 | #include <linux/libfdt_env.h> |
bee91169 AG |
24 | |
25 | DECLARE_GLOBAL_DATA_PTR; | |
26 | ||
32f4b2f8 | 27 | /* Task priority level */ |
152cade3 | 28 | static efi_uintn_t efi_tpl = TPL_APPLICATION; |
32f4b2f8 | 29 | |
bee91169 AG |
30 | /* This list contains all the EFI objects our payload has access to */ |
31 | LIST_HEAD(efi_obj_list); | |
32 | ||
43bce442 | 33 | /* List of all events */ |
14b40487 | 34 | __efi_runtime_data LIST_HEAD(efi_events); |
43bce442 | 35 | |
7a69e97b HS |
36 | /* List of queued events */ |
37 | LIST_HEAD(efi_event_queue); | |
38 | ||
7eaa900e HS |
39 | /* Flag to disable timer activity in ExitBootServices() */ |
40 | static bool timers_enabled = true; | |
41 | ||
fccd3d9c HS |
42 | /* Flag used by the selftest to avoid detaching devices in ExitBootServices() */ |
43 | bool efi_st_keep_devices; | |
44 | ||
ab15d414 HS |
45 | /* List of all events registered by RegisterProtocolNotify() */ |
46 | LIST_HEAD(efi_register_notify_events); | |
47 | ||
bb31c3f7 HS |
48 | /* Handle of the currently executing image */ |
49 | static efi_handle_t current_image; | |
50 | ||
d68d7f47 | 51 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
bee91169 | 52 | /* |
d68d7f47 | 53 | * The "gd" pointer lives in a register on ARM and RISC-V that we declare |
bee91169 AG |
54 | * fixed when compiling U-Boot. However, the payload does not know about that |
55 | * restriction so we need to manually swap its and our view of that register on | |
56 | * EFI callback entry/exit. | |
57 | */ | |
4f7dc5f6 | 58 | static volatile gd_t *efi_gd, *app_gd; |
65e4c0b1 | 59 | #endif |
bee91169 | 60 | |
914df75b HS |
61 | /* 1 if inside U-Boot code, 0 if inside EFI payload code */ |
62 | static int entry_count = 1; | |
af65db85 | 63 | static int nesting_level; |
bc4f9133 HS |
64 | /* GUID of the device tree table */ |
65 | const efi_guid_t efi_guid_fdt = EFI_FDT_GUID; | |
f0959dbe HS |
66 | /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */ |
67 | const efi_guid_t efi_guid_driver_binding_protocol = | |
68 | EFI_DRIVER_BINDING_PROTOCOL_GUID; | |
c160d2f5 | 69 | |
a3a28f5f HS |
70 | /* event group ExitBootServices() invoked */ |
71 | const efi_guid_t efi_guid_event_group_exit_boot_services = | |
72 | EFI_EVENT_GROUP_EXIT_BOOT_SERVICES; | |
73 | /* event group SetVirtualAddressMap() invoked */ | |
74 | const efi_guid_t efi_guid_event_group_virtual_address_change = | |
75 | EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE; | |
76 | /* event group memory map changed */ | |
77 | const efi_guid_t efi_guid_event_group_memory_map_change = | |
78 | EFI_EVENT_GROUP_MEMORY_MAP_CHANGE; | |
79 | /* event group boot manager about to boot */ | |
80 | const efi_guid_t efi_guid_event_group_ready_to_boot = | |
81 | EFI_EVENT_GROUP_READY_TO_BOOT; | |
82 | /* event group ResetSystem() invoked (before ExitBootServices) */ | |
83 | const efi_guid_t efi_guid_event_group_reset_system = | |
84 | EFI_EVENT_GROUP_RESET_SYSTEM; | |
b6f11098 HS |
85 | /* GUIDs of the Load File and Load File2 protocols */ |
86 | const efi_guid_t efi_guid_load_file_protocol = EFI_LOAD_FILE_PROTOCOL_GUID; | |
87 | const efi_guid_t efi_guid_load_file2_protocol = EFI_LOAD_FILE2_PROTOCOL_GUID; | |
a3a28f5f | 88 | |
2074f700 HS |
89 | static efi_status_t EFIAPI efi_disconnect_controller( |
90 | efi_handle_t controller_handle, | |
91 | efi_handle_t driver_image_handle, | |
92 | efi_handle_t child_handle); | |
3f9b0042 | 93 | |
c160d2f5 RC |
94 | /* Called on every callback entry */ |
95 | int __efi_entry_check(void) | |
96 | { | |
97 | int ret = entry_count++ == 0; | |
d68d7f47 | 98 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
c160d2f5 RC |
99 | assert(efi_gd); |
100 | app_gd = gd; | |
4f7dc5f6 | 101 | set_gd(efi_gd); |
c160d2f5 RC |
102 | #endif |
103 | return ret; | |
104 | } | |
105 | ||
106 | /* Called on every callback exit */ | |
107 | int __efi_exit_check(void) | |
108 | { | |
109 | int ret = --entry_count == 0; | |
d68d7f47 | 110 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
4f7dc5f6 | 111 | set_gd(app_gd); |
c160d2f5 RC |
112 | #endif |
113 | return ret; | |
114 | } | |
115 | ||
e7d64065 HS |
116 | /** |
117 | * efi_save_gd() - save global data register | |
118 | * | |
d68d7f47 | 119 | * On the ARM and RISC-V architectures gd is mapped to a fixed register. |
e7d64065 HS |
120 | * As this register may be overwritten by an EFI payload we save it here |
121 | * and restore it on every callback entered. | |
122 | * | |
123 | * This function is called after relocation from initr_reloc_global_data(). | |
124 | */ | |
bee91169 AG |
125 | void efi_save_gd(void) |
126 | { | |
d68d7f47 | 127 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
bee91169 | 128 | efi_gd = gd; |
65e4c0b1 | 129 | #endif |
bee91169 AG |
130 | } |
131 | ||
e7d64065 HS |
132 | /** |
133 | * efi_restore_gd() - restore global data register | |
134 | * | |
d68d7f47 | 135 | * On the ARM and RISC-V architectures gd is mapped to a fixed register. |
e7d64065 HS |
136 | * Restore it after returning from the UEFI world to the value saved via |
137 | * efi_save_gd(). | |
c160d2f5 | 138 | */ |
bee91169 AG |
139 | void efi_restore_gd(void) |
140 | { | |
d68d7f47 | 141 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
bee91169 AG |
142 | /* Only restore if we're already in EFI context */ |
143 | if (!efi_gd) | |
144 | return; | |
4f7dc5f6 | 145 | set_gd(efi_gd); |
65e4c0b1 | 146 | #endif |
bee91169 AG |
147 | } |
148 | ||
6b03cd10 | 149 | /** |
78a88f79 MS |
150 | * indent_string() - returns a string for indenting with two spaces per level |
151 | * @level: indent level | |
6b03cd10 HS |
152 | * |
153 | * A maximum of ten indent levels is supported. Higher indent levels will be | |
154 | * truncated. | |
c8df80c5 | 155 | * |
78a88f79 MS |
156 | * Return: A string for indenting with two spaces per level is |
157 | * returned. | |
af65db85 RC |
158 | */ |
159 | static const char *indent_string(int level) | |
160 | { | |
161 | const char *indent = " "; | |
162 | const int max = strlen(indent); | |
ab9efa97 | 163 | |
af65db85 RC |
164 | level = min(max, level * 2); |
165 | return &indent[max - level]; | |
166 | } | |
167 | ||
ae0bd3a9 HS |
168 | const char *__efi_nesting(void) |
169 | { | |
170 | return indent_string(nesting_level); | |
171 | } | |
172 | ||
af65db85 RC |
173 | const char *__efi_nesting_inc(void) |
174 | { | |
175 | return indent_string(nesting_level++); | |
176 | } | |
177 | ||
178 | const char *__efi_nesting_dec(void) | |
179 | { | |
180 | return indent_string(--nesting_level); | |
181 | } | |
182 | ||
7a69e97b HS |
183 | /** |
184 | * efi_event_is_queued() - check if an event is queued | |
185 | * | |
186 | * @event: event | |
187 | * Return: true if event is queued | |
188 | */ | |
189 | static bool efi_event_is_queued(struct efi_event *event) | |
190 | { | |
191 | return !!event->queue_link.next; | |
192 | } | |
193 | ||
194 | /** | |
195 | * efi_process_event_queue() - process event queue | |
196 | */ | |
197 | static void efi_process_event_queue(void) | |
198 | { | |
199 | while (!list_empty(&efi_event_queue)) { | |
200 | struct efi_event *event; | |
201 | efi_uintn_t old_tpl; | |
202 | ||
203 | event = list_first_entry(&efi_event_queue, struct efi_event, | |
204 | queue_link); | |
205 | if (efi_tpl >= event->notify_tpl) | |
206 | return; | |
207 | list_del(&event->queue_link); | |
208 | event->queue_link.next = NULL; | |
209 | event->queue_link.prev = NULL; | |
210 | /* Events must be executed at the event's TPL */ | |
211 | old_tpl = efi_tpl; | |
212 | efi_tpl = event->notify_tpl; | |
213 | EFI_CALL_VOID(event->notify_function(event, | |
214 | event->notify_context)); | |
215 | efi_tpl = old_tpl; | |
216 | if (event->type == EVT_NOTIFY_SIGNAL) | |
217 | event->is_signaled = 0; | |
218 | } | |
219 | } | |
220 | ||
6b03cd10 | 221 | /** |
78a88f79 MS |
222 | * efi_queue_event() - queue an EFI event |
223 | * @event: event to signal | |
332468f7 HS |
224 | * |
225 | * This function queues the notification function of the event for future | |
226 | * execution. | |
227 | * | |
332468f7 | 228 | */ |
7eaa900e | 229 | static void efi_queue_event(struct efi_event *event) |
c6841592 | 230 | { |
2b8568f4 | 231 | struct efi_event *item; |
7a69e97b HS |
232 | |
233 | if (!event->notify_function) | |
234 | return; | |
235 | ||
236 | if (!efi_event_is_queued(event)) { | |
237 | /* | |
238 | * Events must be notified in order of decreasing task priority | |
239 | * level. Insert the new event accordingly. | |
240 | */ | |
241 | list_for_each_entry(item, &efi_event_queue, queue_link) { | |
242 | if (item->notify_tpl < event->notify_tpl) { | |
243 | list_add_tail(&event->queue_link, | |
244 | &item->queue_link); | |
245 | event = NULL; | |
246 | break; | |
247 | } | |
248 | } | |
249 | if (event) | |
250 | list_add_tail(&event->queue_link, &efi_event_queue); | |
b7d186f0 | 251 | efi_process_event_queue(); |
c6841592 HS |
252 | } |
253 | } | |
254 | ||
21b3edfc HS |
255 | /** |
256 | * is_valid_tpl() - check if the task priority level is valid | |
257 | * | |
258 | * @tpl: TPL level to check | |
b72aaa87 | 259 | * Return: status code |
21b3edfc HS |
260 | */ |
261 | efi_status_t is_valid_tpl(efi_uintn_t tpl) | |
262 | { | |
263 | switch (tpl) { | |
264 | case TPL_APPLICATION: | |
265 | case TPL_CALLBACK: | |
266 | case TPL_NOTIFY: | |
21b3edfc HS |
267 | return EFI_SUCCESS; |
268 | default: | |
269 | return EFI_INVALID_PARAMETER; | |
270 | } | |
271 | } | |
272 | ||
6b03cd10 | 273 | /** |
78a88f79 MS |
274 | * efi_signal_event() - signal an EFI event |
275 | * @event: event to signal | |
b095f3c8 | 276 | * |
2a0f80f0 HS |
277 | * This function signals an event. If the event belongs to an event group, all |
278 | * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL, | |
b095f3c8 HS |
279 | * their notification function is queued. |
280 | * | |
281 | * For the SignalEvent service see efi_signal_event_ext. | |
b095f3c8 | 282 | */ |
7eaa900e | 283 | void efi_signal_event(struct efi_event *event) |
b095f3c8 | 284 | { |
dfa306e4 HS |
285 | if (event->is_signaled) |
286 | return; | |
b095f3c8 HS |
287 | if (event->group) { |
288 | struct efi_event *evt; | |
289 | ||
290 | /* | |
291 | * The signaled state has to set before executing any | |
292 | * notification function | |
293 | */ | |
294 | list_for_each_entry(evt, &efi_events, link) { | |
295 | if (!evt->group || guidcmp(evt->group, event->group)) | |
296 | continue; | |
297 | if (evt->is_signaled) | |
298 | continue; | |
299 | evt->is_signaled = true; | |
b095f3c8 HS |
300 | } |
301 | list_for_each_entry(evt, &efi_events, link) { | |
302 | if (!evt->group || guidcmp(evt->group, event->group)) | |
303 | continue; | |
7a69e97b | 304 | efi_queue_event(evt); |
b095f3c8 | 305 | } |
3626e539 | 306 | } else { |
b095f3c8 | 307 | event->is_signaled = true; |
7a69e97b | 308 | efi_queue_event(event); |
b095f3c8 HS |
309 | } |
310 | } | |
311 | ||
6b03cd10 | 312 | /** |
78a88f79 MS |
313 | * efi_raise_tpl() - raise the task priority level |
314 | * @new_tpl: new value of the task priority level | |
332468f7 HS |
315 | * |
316 | * This function implements the RaiseTpl service. | |
332468f7 | 317 | * |
78a88f79 MS |
318 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
319 | * details. | |
320 | * | |
321 | * Return: old value of the task priority level | |
332468f7 | 322 | */ |
152cade3 | 323 | static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl) |
bee91169 | 324 | { |
152cade3 | 325 | efi_uintn_t old_tpl = efi_tpl; |
32f4b2f8 | 326 | |
503f2695 | 327 | EFI_ENTRY("0x%zx", new_tpl); |
32f4b2f8 HS |
328 | |
329 | if (new_tpl < efi_tpl) | |
529886a0 | 330 | EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__); |
32f4b2f8 HS |
331 | efi_tpl = new_tpl; |
332 | if (efi_tpl > TPL_HIGH_LEVEL) | |
333 | efi_tpl = TPL_HIGH_LEVEL; | |
334 | ||
335 | EFI_EXIT(EFI_SUCCESS); | |
336 | return old_tpl; | |
bee91169 AG |
337 | } |
338 | ||
6b03cd10 | 339 | /** |
78a88f79 MS |
340 | * efi_restore_tpl() - lower the task priority level |
341 | * @old_tpl: value of the task priority level to be restored | |
332468f7 HS |
342 | * |
343 | * This function implements the RestoreTpl service. | |
332468f7 | 344 | * |
78a88f79 MS |
345 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
346 | * details. | |
332468f7 | 347 | */ |
152cade3 | 348 | static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl) |
bee91169 | 349 | { |
503f2695 | 350 | EFI_ENTRY("0x%zx", old_tpl); |
32f4b2f8 HS |
351 | |
352 | if (old_tpl > efi_tpl) | |
529886a0 | 353 | EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__); |
32f4b2f8 HS |
354 | efi_tpl = old_tpl; |
355 | if (efi_tpl > TPL_HIGH_LEVEL) | |
356 | efi_tpl = TPL_HIGH_LEVEL; | |
357 | ||
0f7fcc72 HS |
358 | /* |
359 | * Lowering the TPL may have made queued events eligible for execution. | |
360 | */ | |
361 | efi_timer_check(); | |
362 | ||
32f4b2f8 | 363 | EFI_EXIT(EFI_SUCCESS); |
bee91169 AG |
364 | } |
365 | ||
6b03cd10 | 366 | /** |
78a88f79 MS |
367 | * efi_allocate_pages_ext() - allocate memory pages |
368 | * @type: type of allocation to be performed | |
369 | * @memory_type: usage type of the allocated memory | |
370 | * @pages: number of pages to be allocated | |
371 | * @memory: allocated memory | |
332468f7 HS |
372 | * |
373 | * This function implements the AllocatePages service. | |
332468f7 | 374 | * |
78a88f79 MS |
375 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
376 | * details. | |
377 | * | |
378 | * Return: status code | |
332468f7 | 379 | */ |
6e0bf8d8 | 380 | static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type, |
f5a2a938 | 381 | efi_uintn_t pages, |
6e0bf8d8 | 382 | uint64_t *memory) |
bee91169 AG |
383 | { |
384 | efi_status_t r; | |
385 | ||
f5a2a938 | 386 | EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory); |
bee91169 AG |
387 | r = efi_allocate_pages(type, memory_type, pages, memory); |
388 | return EFI_EXIT(r); | |
389 | } | |
390 | ||
6b03cd10 | 391 | /** |
78a88f79 MS |
392 | * efi_free_pages_ext() - Free memory pages. |
393 | * @memory: start of the memory area to be freed | |
394 | * @pages: number of pages to be freed | |
332468f7 HS |
395 | * |
396 | * This function implements the FreePages service. | |
332468f7 | 397 | * |
78a88f79 MS |
398 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
399 | * details. | |
400 | * | |
401 | * Return: status code | |
332468f7 | 402 | */ |
6e0bf8d8 | 403 | static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory, |
f5a2a938 | 404 | efi_uintn_t pages) |
bee91169 AG |
405 | { |
406 | efi_status_t r; | |
407 | ||
dee37fc9 | 408 | EFI_ENTRY("%llx, 0x%zx", memory, pages); |
bee91169 AG |
409 | r = efi_free_pages(memory, pages); |
410 | return EFI_EXIT(r); | |
411 | } | |
412 | ||
6b03cd10 | 413 | /** |
78a88f79 MS |
414 | * efi_get_memory_map_ext() - get map describing memory usage |
415 | * @memory_map_size: on entry the size, in bytes, of the memory map buffer, | |
416 | * on exit the size of the copied memory map | |
417 | * @memory_map: buffer to which the memory map is written | |
418 | * @map_key: key for the memory map | |
419 | * @descriptor_size: size of an individual memory descriptor | |
420 | * @descriptor_version: version number of the memory descriptor structure | |
332468f7 HS |
421 | * |
422 | * This function implements the GetMemoryMap service. | |
332468f7 | 423 | * |
78a88f79 MS |
424 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
425 | * details. | |
426 | * | |
427 | * Return: status code | |
332468f7 | 428 | */ |
6e0bf8d8 | 429 | static efi_status_t EFIAPI efi_get_memory_map_ext( |
f5a2a938 | 430 | efi_uintn_t *memory_map_size, |
6e0bf8d8 | 431 | struct efi_mem_desc *memory_map, |
f5a2a938 HS |
432 | efi_uintn_t *map_key, |
433 | efi_uintn_t *descriptor_size, | |
6e0bf8d8 | 434 | uint32_t *descriptor_version) |
bee91169 AG |
435 | { |
436 | efi_status_t r; | |
437 | ||
438 | EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map, | |
439 | map_key, descriptor_size, descriptor_version); | |
440 | r = efi_get_memory_map(memory_map_size, memory_map, map_key, | |
441 | descriptor_size, descriptor_version); | |
442 | return EFI_EXIT(r); | |
443 | } | |
444 | ||
6b03cd10 | 445 | /** |
78a88f79 MS |
446 | * efi_allocate_pool_ext() - allocate memory from pool |
447 | * @pool_type: type of the pool from which memory is to be allocated | |
448 | * @size: number of bytes to be allocated | |
449 | * @buffer: allocated memory | |
332468f7 HS |
450 | * |
451 | * This function implements the AllocatePool service. | |
332468f7 | 452 | * |
78a88f79 MS |
453 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
454 | * details. | |
455 | * | |
456 | * Return: status code | |
332468f7 | 457 | */ |
ead1274b | 458 | static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type, |
f5a2a938 | 459 | efi_uintn_t size, |
ead1274b | 460 | void **buffer) |
bee91169 | 461 | { |
1cd29f0a AG |
462 | efi_status_t r; |
463 | ||
f5a2a938 | 464 | EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer); |
ead1274b | 465 | r = efi_allocate_pool(pool_type, size, buffer); |
1cd29f0a | 466 | return EFI_EXIT(r); |
bee91169 AG |
467 | } |
468 | ||
6b03cd10 | 469 | /** |
78a88f79 MS |
470 | * efi_free_pool_ext() - free memory from pool |
471 | * @buffer: start of memory to be freed | |
332468f7 HS |
472 | * |
473 | * This function implements the FreePool service. | |
332468f7 | 474 | * |
78a88f79 MS |
475 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
476 | * details. | |
477 | * | |
478 | * Return: status code | |
332468f7 | 479 | */ |
42417bc8 | 480 | static efi_status_t EFIAPI efi_free_pool_ext(void *buffer) |
bee91169 | 481 | { |
1cd29f0a AG |
482 | efi_status_t r; |
483 | ||
484 | EFI_ENTRY("%p", buffer); | |
42417bc8 | 485 | r = efi_free_pool(buffer); |
1cd29f0a | 486 | return EFI_EXIT(r); |
bee91169 AG |
487 | } |
488 | ||
6b03cd10 | 489 | /** |
e6023be4 | 490 | * efi_add_handle() - add a new handle to the object list |
44549d62 | 491 | * |
e6023be4 HS |
492 | * @handle: handle to be added |
493 | * | |
494 | * The protocols list is initialized. The handle is added to the list of known | |
495 | * UEFI objects. | |
44549d62 | 496 | */ |
fae0118e | 497 | void efi_add_handle(efi_handle_t handle) |
44549d62 | 498 | { |
fae0118e | 499 | if (!handle) |
44549d62 | 500 | return; |
fae0118e HS |
501 | INIT_LIST_HEAD(&handle->protocols); |
502 | list_add_tail(&handle->link, &efi_obj_list); | |
44549d62 HS |
503 | } |
504 | ||
6b03cd10 | 505 | /** |
78a88f79 MS |
506 | * efi_create_handle() - create handle |
507 | * @handle: new handle | |
2edab5e2 | 508 | * |
78a88f79 | 509 | * Return: status code |
2edab5e2 | 510 | */ |
2074f700 | 511 | efi_status_t efi_create_handle(efi_handle_t *handle) |
3cc6e3fe HS |
512 | { |
513 | struct efi_object *obj; | |
3cc6e3fe | 514 | |
d29e7824 HS |
515 | obj = calloc(1, sizeof(struct efi_object)); |
516 | if (!obj) | |
517 | return EFI_OUT_OF_RESOURCES; | |
518 | ||
44549d62 | 519 | efi_add_handle(obj); |
fae0118e | 520 | *handle = obj; |
d29e7824 HS |
521 | |
522 | return EFI_SUCCESS; | |
3cc6e3fe HS |
523 | } |
524 | ||
6b03cd10 | 525 | /** |
78a88f79 MS |
526 | * efi_search_protocol() - find a protocol on a handle. |
527 | * @handle: handle | |
528 | * @protocol_guid: GUID of the protocol | |
529 | * @handler: reference to the protocol | |
678e03a0 | 530 | * |
78a88f79 | 531 | * Return: status code |
678e03a0 | 532 | */ |
2074f700 | 533 | efi_status_t efi_search_protocol(const efi_handle_t handle, |
678e03a0 HS |
534 | const efi_guid_t *protocol_guid, |
535 | struct efi_handler **handler) | |
536 | { | |
537 | struct efi_object *efiobj; | |
538 | struct list_head *lhandle; | |
539 | ||
540 | if (!handle || !protocol_guid) | |
541 | return EFI_INVALID_PARAMETER; | |
542 | efiobj = efi_search_obj(handle); | |
543 | if (!efiobj) | |
544 | return EFI_INVALID_PARAMETER; | |
545 | list_for_each(lhandle, &efiobj->protocols) { | |
546 | struct efi_handler *protocol; | |
547 | ||
548 | protocol = list_entry(lhandle, struct efi_handler, link); | |
549 | if (!guidcmp(protocol->guid, protocol_guid)) { | |
550 | if (handler) | |
551 | *handler = protocol; | |
552 | return EFI_SUCCESS; | |
553 | } | |
554 | } | |
555 | return EFI_NOT_FOUND; | |
556 | } | |
557 | ||
6b03cd10 | 558 | /** |
78a88f79 MS |
559 | * efi_remove_protocol() - delete protocol from a handle |
560 | * @handle: handle from which the protocol shall be deleted | |
561 | * @protocol: GUID of the protocol to be deleted | |
562 | * @protocol_interface: interface of the protocol implementation | |
678e03a0 | 563 | * |
78a88f79 | 564 | * Return: status code |
678e03a0 | 565 | */ |
2074f700 HS |
566 | efi_status_t efi_remove_protocol(const efi_handle_t handle, |
567 | const efi_guid_t *protocol, | |
678e03a0 HS |
568 | void *protocol_interface) |
569 | { | |
570 | struct efi_handler *handler; | |
571 | efi_status_t ret; | |
572 | ||
573 | ret = efi_search_protocol(handle, protocol, &handler); | |
574 | if (ret != EFI_SUCCESS) | |
575 | return ret; | |
1f470e17 | 576 | if (handler->protocol_interface != protocol_interface) |
96aa99cd | 577 | return EFI_NOT_FOUND; |
678e03a0 HS |
578 | list_del(&handler->link); |
579 | free(handler); | |
580 | return EFI_SUCCESS; | |
581 | } | |
582 | ||
6b03cd10 | 583 | /** |
78a88f79 MS |
584 | * efi_remove_all_protocols() - delete all protocols from a handle |
585 | * @handle: handle from which the protocols shall be deleted | |
678e03a0 | 586 | * |
78a88f79 | 587 | * Return: status code |
678e03a0 | 588 | */ |
2074f700 | 589 | efi_status_t efi_remove_all_protocols(const efi_handle_t handle) |
678e03a0 HS |
590 | { |
591 | struct efi_object *efiobj; | |
32e6fed6 HS |
592 | struct efi_handler *protocol; |
593 | struct efi_handler *pos; | |
678e03a0 HS |
594 | |
595 | efiobj = efi_search_obj(handle); | |
596 | if (!efiobj) | |
597 | return EFI_INVALID_PARAMETER; | |
32e6fed6 | 598 | list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) { |
678e03a0 HS |
599 | efi_status_t ret; |
600 | ||
678e03a0 HS |
601 | ret = efi_remove_protocol(handle, protocol->guid, |
602 | protocol->protocol_interface); | |
603 | if (ret != EFI_SUCCESS) | |
604 | return ret; | |
605 | } | |
606 | return EFI_SUCCESS; | |
607 | } | |
608 | ||
6b03cd10 | 609 | /** |
78a88f79 | 610 | * efi_delete_handle() - delete handle |
678e03a0 | 611 | * |
6631ca59 | 612 | * @handle: handle to delete |
678e03a0 | 613 | */ |
fae0118e | 614 | void efi_delete_handle(efi_handle_t handle) |
678e03a0 | 615 | { |
fae0118e | 616 | if (!handle) |
678e03a0 | 617 | return; |
fae0118e HS |
618 | efi_remove_all_protocols(handle); |
619 | list_del(&handle->link); | |
620 | free(handle); | |
678e03a0 HS |
621 | } |
622 | ||
6b03cd10 | 623 | /** |
78a88f79 MS |
624 | * efi_is_event() - check if a pointer is a valid event |
625 | * @event: pointer to check | |
43bce442 | 626 | * |
78a88f79 | 627 | * Return: status code |
bee91169 | 628 | */ |
43bce442 HS |
629 | static efi_status_t efi_is_event(const struct efi_event *event) |
630 | { | |
631 | const struct efi_event *evt; | |
632 | ||
633 | if (!event) | |
634 | return EFI_INVALID_PARAMETER; | |
635 | list_for_each_entry(evt, &efi_events, link) { | |
636 | if (evt == event) | |
637 | return EFI_SUCCESS; | |
638 | } | |
639 | return EFI_INVALID_PARAMETER; | |
640 | } | |
bee91169 | 641 | |
6b03cd10 | 642 | /** |
78a88f79 | 643 | * efi_create_event() - create an event |
6631ca59 | 644 | * |
78a88f79 MS |
645 | * @type: type of the event to create |
646 | * @notify_tpl: task priority level of the event | |
647 | * @notify_function: notification function of the event | |
648 | * @notify_context: pointer passed to the notification function | |
649 | * @group: event group | |
650 | * @event: created event | |
332468f7 HS |
651 | * |
652 | * This function is used inside U-Boot code to create an event. | |
653 | * | |
654 | * For the API function implementing the CreateEvent service see | |
655 | * efi_create_event_ext. | |
656 | * | |
78a88f79 | 657 | * Return: status code |
332468f7 | 658 | */ |
152cade3 | 659 | efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl, |
49deb455 | 660 | void (EFIAPI *notify_function) ( |
2fd945fe HS |
661 | struct efi_event *event, |
662 | void *context), | |
b095f3c8 HS |
663 | void *notify_context, efi_guid_t *group, |
664 | struct efi_event **event) | |
bee91169 | 665 | { |
43bce442 | 666 | struct efi_event *evt; |
14b40487 HS |
667 | efi_status_t ret; |
668 | int pool_type; | |
c6841592 | 669 | |
a95343b8 | 670 | if (event == NULL) |
49deb455 | 671 | return EFI_INVALID_PARAMETER; |
a95343b8 | 672 | |
21b3edfc HS |
673 | switch (type) { |
674 | case 0: | |
675 | case EVT_TIMER: | |
676 | case EVT_NOTIFY_SIGNAL: | |
677 | case EVT_TIMER | EVT_NOTIFY_SIGNAL: | |
678 | case EVT_NOTIFY_WAIT: | |
679 | case EVT_TIMER | EVT_NOTIFY_WAIT: | |
680 | case EVT_SIGNAL_EXIT_BOOT_SERVICES: | |
14b40487 HS |
681 | pool_type = EFI_BOOT_SERVICES_DATA; |
682 | break; | |
21b3edfc | 683 | case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE: |
14b40487 | 684 | pool_type = EFI_RUNTIME_SERVICES_DATA; |
21b3edfc HS |
685 | break; |
686 | default: | |
49deb455 | 687 | return EFI_INVALID_PARAMETER; |
21b3edfc | 688 | } |
a95343b8 | 689 | |
2cfb68fd HS |
690 | /* |
691 | * The UEFI specification requires event notification levels to be | |
692 | * > TPL_APPLICATION and <= TPL_HIGH_LEVEL. | |
693 | * | |
694 | * Parameter NotifyTpl should not be checked if it is not used. | |
695 | */ | |
3748ed90 | 696 | if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) && |
2cfb68fd HS |
697 | (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS || |
698 | notify_tpl == TPL_APPLICATION)) | |
49deb455 | 699 | return EFI_INVALID_PARAMETER; |
a95343b8 | 700 | |
14b40487 HS |
701 | ret = efi_allocate_pool(pool_type, sizeof(struct efi_event), |
702 | (void **)&evt); | |
703 | if (ret != EFI_SUCCESS) | |
704 | return ret; | |
705 | memset(evt, 0, sizeof(struct efi_event)); | |
43bce442 HS |
706 | evt->type = type; |
707 | evt->notify_tpl = notify_tpl; | |
708 | evt->notify_function = notify_function; | |
709 | evt->notify_context = notify_context; | |
b095f3c8 | 710 | evt->group = group; |
b72aaa87 | 711 | /* Disable timers on boot up */ |
43bce442 | 712 | evt->trigger_next = -1ULL; |
43bce442 HS |
713 | list_add_tail(&evt->link, &efi_events); |
714 | *event = evt; | |
715 | return EFI_SUCCESS; | |
bee91169 AG |
716 | } |
717 | ||
9f0930e5 | 718 | /* |
78a88f79 MS |
719 | * efi_create_event_ex() - create an event in a group |
720 | * @type: type of the event to create | |
721 | * @notify_tpl: task priority level of the event | |
722 | * @notify_function: notification function of the event | |
723 | * @notify_context: pointer passed to the notification function | |
724 | * @event: created event | |
725 | * @event_group: event group | |
9f0930e5 HS |
726 | * |
727 | * This function implements the CreateEventEx service. | |
9f0930e5 | 728 | * |
78a88f79 MS |
729 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
730 | * details. | |
731 | * | |
732 | * Return: status code | |
9f0930e5 HS |
733 | */ |
734 | efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl, | |
735 | void (EFIAPI *notify_function) ( | |
736 | struct efi_event *event, | |
737 | void *context), | |
738 | void *notify_context, | |
739 | efi_guid_t *event_group, | |
740 | struct efi_event **event) | |
741 | { | |
1884512b HS |
742 | efi_status_t ret; |
743 | ||
9f0930e5 HS |
744 | EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function, |
745 | notify_context, event_group); | |
1884512b HS |
746 | |
747 | /* | |
748 | * The allowable input parameters are the same as in CreateEvent() | |
749 | * except for the following two disallowed event types. | |
750 | */ | |
751 | switch (type) { | |
752 | case EVT_SIGNAL_EXIT_BOOT_SERVICES: | |
753 | case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE: | |
754 | ret = EFI_INVALID_PARAMETER; | |
755 | goto out; | |
756 | } | |
757 | ||
758 | ret = efi_create_event(type, notify_tpl, notify_function, | |
759 | notify_context, event_group, event); | |
760 | out: | |
761 | return EFI_EXIT(ret); | |
9f0930e5 HS |
762 | } |
763 | ||
6b03cd10 | 764 | /** |
78a88f79 MS |
765 | * efi_create_event_ext() - create an event |
766 | * @type: type of the event to create | |
767 | * @notify_tpl: task priority level of the event | |
768 | * @notify_function: notification function of the event | |
769 | * @notify_context: pointer passed to the notification function | |
770 | * @event: created event | |
332468f7 HS |
771 | * |
772 | * This function implements the CreateEvent service. | |
332468f7 | 773 | * |
78a88f79 MS |
774 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
775 | * details. | |
776 | * | |
777 | * Return: status code | |
332468f7 | 778 | */ |
49deb455 | 779 | static efi_status_t EFIAPI efi_create_event_ext( |
152cade3 | 780 | uint32_t type, efi_uintn_t notify_tpl, |
49deb455 HS |
781 | void (EFIAPI *notify_function) ( |
782 | struct efi_event *event, | |
783 | void *context), | |
784 | void *notify_context, struct efi_event **event) | |
785 | { | |
786 | EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function, | |
787 | notify_context); | |
788 | return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function, | |
b095f3c8 | 789 | notify_context, NULL, event)); |
49deb455 HS |
790 | } |
791 | ||
6b03cd10 | 792 | /** |
78a88f79 | 793 | * efi_timer_check() - check if a timer event has occurred |
6b03cd10 | 794 | * |
332468f7 HS |
795 | * Check if a timer event has occurred or a queued notification function should |
796 | * be called. | |
797 | * | |
bee91169 | 798 | * Our timers have to work without interrupts, so we check whenever keyboard |
332468f7 | 799 | * input or disk accesses happen if enough time elapsed for them to fire. |
bee91169 AG |
800 | */ |
801 | void efi_timer_check(void) | |
802 | { | |
43bce442 | 803 | struct efi_event *evt; |
bee91169 AG |
804 | u64 now = timer_get_us(); |
805 | ||
43bce442 | 806 | list_for_each_entry(evt, &efi_events, link) { |
7eaa900e HS |
807 | if (!timers_enabled) |
808 | continue; | |
43bce442 | 809 | if (!(evt->type & EVT_TIMER) || now < evt->trigger_next) |
ca62a4f5 | 810 | continue; |
43bce442 | 811 | switch (evt->trigger_type) { |
ca62a4f5 | 812 | case EFI_TIMER_RELATIVE: |
43bce442 | 813 | evt->trigger_type = EFI_TIMER_STOP; |
ca62a4f5 HS |
814 | break; |
815 | case EFI_TIMER_PERIODIC: | |
43bce442 | 816 | evt->trigger_next += evt->trigger_time; |
ca62a4f5 HS |
817 | break; |
818 | default: | |
819 | continue; | |
c6841592 | 820 | } |
b095f3c8 | 821 | evt->is_signaled = false; |
7eaa900e | 822 | efi_signal_event(evt); |
bee91169 | 823 | } |
7a69e97b | 824 | efi_process_event_queue(); |
bee91169 AG |
825 | WATCHDOG_RESET(); |
826 | } | |
827 | ||
6b03cd10 | 828 | /** |
78a88f79 MS |
829 | * efi_set_timer() - set the trigger time for a timer event or stop the event |
830 | * @event: event for which the timer is set | |
831 | * @type: type of the timer | |
b72aaa87 | 832 | * @trigger_time: trigger period in multiples of 100 ns |
332468f7 HS |
833 | * |
834 | * This is the function for internal usage in U-Boot. For the API function | |
835 | * implementing the SetTimer service see efi_set_timer_ext. | |
836 | * | |
78a88f79 | 837 | * Return: status code |
332468f7 | 838 | */ |
b521d29e | 839 | efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type, |
bfc72462 | 840 | uint64_t trigger_time) |
bee91169 | 841 | { |
43bce442 HS |
842 | /* Check that the event is valid */ |
843 | if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER)) | |
844 | return EFI_INVALID_PARAMETER; | |
bee91169 | 845 | |
8787b02e | 846 | /* |
b72aaa87 HS |
847 | * The parameter defines a multiple of 100 ns. |
848 | * We use multiples of 1000 ns. So divide by 10. | |
8787b02e | 849 | */ |
7d963323 | 850 | do_div(trigger_time, 10); |
bee91169 | 851 | |
43bce442 HS |
852 | switch (type) { |
853 | case EFI_TIMER_STOP: | |
854 | event->trigger_next = -1ULL; | |
855 | break; | |
856 | case EFI_TIMER_PERIODIC: | |
857 | case EFI_TIMER_RELATIVE: | |
858 | event->trigger_next = timer_get_us() + trigger_time; | |
859 | break; | |
860 | default: | |
861 | return EFI_INVALID_PARAMETER; | |
bee91169 | 862 | } |
43bce442 HS |
863 | event->trigger_type = type; |
864 | event->trigger_time = trigger_time; | |
865 | event->is_signaled = false; | |
866 | return EFI_SUCCESS; | |
bfc72462 HS |
867 | } |
868 | ||
6b03cd10 | 869 | /** |
78a88f79 MS |
870 | * efi_set_timer_ext() - Set the trigger time for a timer event or stop the |
871 | * event | |
872 | * @event: event for which the timer is set | |
873 | * @type: type of the timer | |
b72aaa87 | 874 | * @trigger_time: trigger period in multiples of 100 ns |
332468f7 HS |
875 | * |
876 | * This function implements the SetTimer service. | |
332468f7 | 877 | * |
78a88f79 MS |
878 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
879 | * details. | |
880 | * | |
881 | * | |
882 | * Return: status code | |
332468f7 | 883 | */ |
b521d29e HS |
884 | static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event, |
885 | enum efi_timer_delay type, | |
886 | uint64_t trigger_time) | |
bfc72462 | 887 | { |
dee37fc9 | 888 | EFI_ENTRY("%p, %d, %llx", event, type, trigger_time); |
bfc72462 | 889 | return EFI_EXIT(efi_set_timer(event, type, trigger_time)); |
bee91169 AG |
890 | } |
891 | ||
6b03cd10 | 892 | /** |
78a88f79 MS |
893 | * efi_wait_for_event() - wait for events to be signaled |
894 | * @num_events: number of events to be waited for | |
895 | * @event: events to be waited for | |
896 | * @index: index of the event that was signaled | |
332468f7 HS |
897 | * |
898 | * This function implements the WaitForEvent service. | |
332468f7 | 899 | * |
78a88f79 MS |
900 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
901 | * details. | |
902 | * | |
903 | * Return: status code | |
332468f7 | 904 | */ |
f5a2a938 | 905 | static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events, |
2fd945fe | 906 | struct efi_event **event, |
f5a2a938 | 907 | efi_uintn_t *index) |
bee91169 | 908 | { |
43bce442 | 909 | int i; |
bee91169 | 910 | |
f5a2a938 | 911 | EFI_ENTRY("%zd, %p, %p", num_events, event, index); |
bee91169 | 912 | |
c6841592 HS |
913 | /* Check parameters */ |
914 | if (!num_events || !event) | |
915 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
32f4b2f8 HS |
916 | /* Check TPL */ |
917 | if (efi_tpl != TPL_APPLICATION) | |
918 | return EFI_EXIT(EFI_UNSUPPORTED); | |
c6841592 | 919 | for (i = 0; i < num_events; ++i) { |
43bce442 HS |
920 | if (efi_is_event(event[i]) != EFI_SUCCESS) |
921 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
c6841592 HS |
922 | if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL) |
923 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
e190e897 | 924 | if (!event[i]->is_signaled) |
7eaa900e | 925 | efi_queue_event(event[i]); |
c6841592 HS |
926 | } |
927 | ||
928 | /* Wait for signal */ | |
929 | for (;;) { | |
930 | for (i = 0; i < num_events; ++i) { | |
e190e897 | 931 | if (event[i]->is_signaled) |
c6841592 HS |
932 | goto out; |
933 | } | |
934 | /* Allow events to occur. */ | |
935 | efi_timer_check(); | |
936 | } | |
937 | ||
938 | out: | |
939 | /* | |
940 | * Reset the signal which is passed to the caller to allow periodic | |
941 | * events to occur. | |
942 | */ | |
e190e897 | 943 | event[i]->is_signaled = false; |
c6841592 HS |
944 | if (index) |
945 | *index = i; | |
bee91169 AG |
946 | |
947 | return EFI_EXIT(EFI_SUCCESS); | |
948 | } | |
949 | ||
6b03cd10 | 950 | /** |
78a88f79 MS |
951 | * efi_signal_event_ext() - signal an EFI event |
952 | * @event: event to signal | |
332468f7 HS |
953 | * |
954 | * This function implements the SignalEvent service. | |
78a88f79 MS |
955 | * |
956 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
957 | * details. | |
332468f7 HS |
958 | * |
959 | * This functions sets the signaled state of the event and queues the | |
960 | * notification function for execution. | |
961 | * | |
78a88f79 | 962 | * Return: status code |
332468f7 | 963 | */ |
c6841592 | 964 | static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event) |
bee91169 AG |
965 | { |
966 | EFI_ENTRY("%p", event); | |
43bce442 HS |
967 | if (efi_is_event(event) != EFI_SUCCESS) |
968 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
7eaa900e | 969 | efi_signal_event(event); |
bee91169 AG |
970 | return EFI_EXIT(EFI_SUCCESS); |
971 | } | |
972 | ||
6b03cd10 | 973 | /** |
78a88f79 MS |
974 | * efi_close_event() - close an EFI event |
975 | * @event: event to close | |
332468f7 HS |
976 | * |
977 | * This function implements the CloseEvent service. | |
332468f7 | 978 | * |
78a88f79 MS |
979 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
980 | * details. | |
981 | * | |
982 | * Return: status code | |
332468f7 | 983 | */ |
2fd945fe | 984 | static efi_status_t EFIAPI efi_close_event(struct efi_event *event) |
bee91169 | 985 | { |
ab15d414 HS |
986 | struct efi_register_notify_event *item, *next; |
987 | ||
bee91169 | 988 | EFI_ENTRY("%p", event); |
43bce442 HS |
989 | if (efi_is_event(event) != EFI_SUCCESS) |
990 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
ab15d414 HS |
991 | |
992 | /* Remove protocol notify registrations for the event */ | |
993 | list_for_each_entry_safe(item, next, &efi_register_notify_events, | |
994 | link) { | |
995 | if (event == item->event) { | |
f09cea36 HS |
996 | struct efi_protocol_notification *hitem, *hnext; |
997 | ||
998 | /* Remove signaled handles */ | |
999 | list_for_each_entry_safe(hitem, hnext, &item->handles, | |
1000 | link) { | |
1001 | list_del(&hitem->link); | |
1002 | free(hitem); | |
1003 | } | |
ab15d414 HS |
1004 | list_del(&item->link); |
1005 | free(item); | |
1006 | } | |
1007 | } | |
7a69e97b HS |
1008 | /* Remove event from queue */ |
1009 | if (efi_event_is_queued(event)) | |
1010 | list_del(&event->queue_link); | |
ab15d414 | 1011 | |
43bce442 | 1012 | list_del(&event->link); |
14b40487 | 1013 | efi_free_pool(event); |
43bce442 | 1014 | return EFI_EXIT(EFI_SUCCESS); |
bee91169 AG |
1015 | } |
1016 | ||
6b03cd10 | 1017 | /** |
78a88f79 MS |
1018 | * efi_check_event() - check if an event is signaled |
1019 | * @event: event to check | |
332468f7 HS |
1020 | * |
1021 | * This function implements the CheckEvent service. | |
332468f7 | 1022 | * |
78a88f79 MS |
1023 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
1024 | * details. | |
1025 | * | |
1026 | * If an event is not signaled yet, the notification function is queued. The | |
1027 | * signaled state is cleared. | |
332468f7 | 1028 | * |
78a88f79 | 1029 | * Return: status code |
332468f7 | 1030 | */ |
2fd945fe | 1031 | static efi_status_t EFIAPI efi_check_event(struct efi_event *event) |
bee91169 AG |
1032 | { |
1033 | EFI_ENTRY("%p", event); | |
c6841592 | 1034 | efi_timer_check(); |
43bce442 HS |
1035 | if (efi_is_event(event) != EFI_SUCCESS || |
1036 | event->type & EVT_NOTIFY_SIGNAL) | |
1037 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
1038 | if (!event->is_signaled) | |
7eaa900e | 1039 | efi_queue_event(event); |
43bce442 HS |
1040 | if (event->is_signaled) { |
1041 | event->is_signaled = false; | |
1042 | return EFI_EXIT(EFI_SUCCESS); | |
c6841592 | 1043 | } |
43bce442 | 1044 | return EFI_EXIT(EFI_NOT_READY); |
bee91169 AG |
1045 | } |
1046 | ||
6b03cd10 | 1047 | /** |
78a88f79 MS |
1048 | * efi_search_obj() - find the internal EFI object for a handle |
1049 | * @handle: handle to find | |
7b9f8ad7 | 1050 | * |
78a88f79 | 1051 | * Return: EFI object |
7b9f8ad7 | 1052 | */ |
2074f700 | 1053 | struct efi_object *efi_search_obj(const efi_handle_t handle) |
7b9f8ad7 | 1054 | { |
1b68153a | 1055 | struct efi_object *efiobj; |
7b9f8ad7 | 1056 | |
ec163faa HS |
1057 | if (!handle) |
1058 | return NULL; | |
1059 | ||
1b68153a | 1060 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
fae0118e | 1061 | if (efiobj == handle) |
7b9f8ad7 HS |
1062 | return efiobj; |
1063 | } | |
7b9f8ad7 HS |
1064 | return NULL; |
1065 | } | |
1066 | ||
6b03cd10 | 1067 | /** |
78a88f79 MS |
1068 | * efi_open_protocol_info_entry() - create open protocol info entry and add it |
1069 | * to a protocol | |
1070 | * @handler: handler of a protocol | |
fe1599da | 1071 | * |
78a88f79 | 1072 | * Return: open protocol info entry |
fe1599da HS |
1073 | */ |
1074 | static struct efi_open_protocol_info_entry *efi_create_open_info( | |
1075 | struct efi_handler *handler) | |
1076 | { | |
1077 | struct efi_open_protocol_info_item *item; | |
1078 | ||
1079 | item = calloc(1, sizeof(struct efi_open_protocol_info_item)); | |
1080 | if (!item) | |
1081 | return NULL; | |
1082 | /* Append the item to the open protocol info list. */ | |
1083 | list_add_tail(&item->link, &handler->open_infos); | |
1084 | ||
1085 | return &item->info; | |
1086 | } | |
1087 | ||
6b03cd10 | 1088 | /** |
78a88f79 MS |
1089 | * efi_delete_open_info() - remove an open protocol info entry from a protocol |
1090 | * @item: open protocol info entry to delete | |
fe1599da | 1091 | * |
78a88f79 | 1092 | * Return: status code |
fe1599da HS |
1093 | */ |
1094 | static efi_status_t efi_delete_open_info( | |
1095 | struct efi_open_protocol_info_item *item) | |
1096 | { | |
1097 | list_del(&item->link); | |
1098 | free(item); | |
1099 | return EFI_SUCCESS; | |
1100 | } | |
1101 | ||
6b03cd10 | 1102 | /** |
78a88f79 MS |
1103 | * efi_add_protocol() - install new protocol on a handle |
1104 | * @handle: handle on which the protocol shall be installed | |
1105 | * @protocol: GUID of the protocol to be installed | |
1106 | * @protocol_interface: interface of the protocol implementation | |
3f79a2b5 | 1107 | * |
78a88f79 | 1108 | * Return: status code |
3f79a2b5 | 1109 | */ |
2074f700 HS |
1110 | efi_status_t efi_add_protocol(const efi_handle_t handle, |
1111 | const efi_guid_t *protocol, | |
3f79a2b5 HS |
1112 | void *protocol_interface) |
1113 | { | |
1114 | struct efi_object *efiobj; | |
1115 | struct efi_handler *handler; | |
1116 | efi_status_t ret; | |
ab15d414 | 1117 | struct efi_register_notify_event *event; |
3f79a2b5 HS |
1118 | |
1119 | efiobj = efi_search_obj(handle); | |
1120 | if (!efiobj) | |
1121 | return EFI_INVALID_PARAMETER; | |
1122 | ret = efi_search_protocol(handle, protocol, NULL); | |
1123 | if (ret != EFI_NOT_FOUND) | |
1124 | return EFI_INVALID_PARAMETER; | |
1125 | handler = calloc(1, sizeof(struct efi_handler)); | |
1126 | if (!handler) | |
1127 | return EFI_OUT_OF_RESOURCES; | |
69fb6b1a HS |
1128 | handler->guid = protocol; |
1129 | handler->protocol_interface = protocol_interface; | |
fe1599da | 1130 | INIT_LIST_HEAD(&handler->open_infos); |
69fb6b1a | 1131 | list_add_tail(&handler->link, &efiobj->protocols); |
ab15d414 HS |
1132 | |
1133 | /* Notify registered events */ | |
1134 | list_for_each_entry(event, &efi_register_notify_events, link) { | |
f09cea36 HS |
1135 | if (!guidcmp(protocol, &event->protocol)) { |
1136 | struct efi_protocol_notification *notif; | |
1137 | ||
1138 | notif = calloc(1, sizeof(*notif)); | |
1139 | if (!notif) { | |
1140 | list_del(&handler->link); | |
1141 | free(handler); | |
1142 | return EFI_OUT_OF_RESOURCES; | |
1143 | } | |
1144 | notif->handle = handle; | |
1145 | list_add_tail(¬if->link, &event->handles); | |
daa3f847 | 1146 | event->event->is_signaled = false; |
7eaa900e | 1147 | efi_signal_event(event->event); |
f09cea36 | 1148 | } |
ab15d414 HS |
1149 | } |
1150 | ||
d5504144 HS |
1151 | if (!guidcmp(&efi_guid_device_path, protocol)) |
1152 | EFI_PRINT("installed device path '%pD'\n", protocol_interface); | |
69fb6b1a | 1153 | return EFI_SUCCESS; |
3f79a2b5 HS |
1154 | } |
1155 | ||
6b03cd10 | 1156 | /** |
78a88f79 MS |
1157 | * efi_install_protocol_interface() - install protocol interface |
1158 | * @handle: handle on which the protocol shall be installed | |
1159 | * @protocol: GUID of the protocol to be installed | |
1160 | * @protocol_interface_type: type of the interface to be installed, | |
1161 | * always EFI_NATIVE_INTERFACE | |
1162 | * @protocol_interface: interface of the protocol implementation | |
332468f7 | 1163 | * |
1760ef57 | 1164 | * This function implements the InstallProtocolInterface service. |
332468f7 | 1165 | * |
78a88f79 MS |
1166 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
1167 | * details. | |
1168 | * | |
1169 | * Return: status code | |
332468f7 | 1170 | */ |
1760ef57 | 1171 | static efi_status_t EFIAPI efi_install_protocol_interface( |
faea1041 | 1172 | efi_handle_t *handle, const efi_guid_t *protocol, |
1760ef57 | 1173 | int protocol_interface_type, void *protocol_interface) |
bee91169 | 1174 | { |
e0549f8a HS |
1175 | efi_status_t r; |
1176 | ||
1760ef57 HS |
1177 | EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type, |
1178 | protocol_interface); | |
1179 | ||
e0549f8a HS |
1180 | if (!handle || !protocol || |
1181 | protocol_interface_type != EFI_NATIVE_INTERFACE) { | |
1182 | r = EFI_INVALID_PARAMETER; | |
1183 | goto out; | |
1184 | } | |
1185 | ||
1186 | /* Create new handle if requested. */ | |
1187 | if (!*handle) { | |
3cc6e3fe HS |
1188 | r = efi_create_handle(handle); |
1189 | if (r != EFI_SUCCESS) | |
1190 | goto out; | |
529886a0 | 1191 | EFI_PRINT("new handle %p\n", *handle); |
af1408e0 | 1192 | } else { |
529886a0 | 1193 | EFI_PRINT("handle %p\n", *handle); |
e0549f8a | 1194 | } |
1202530d HS |
1195 | /* Add new protocol */ |
1196 | r = efi_add_protocol(*handle, protocol, protocol_interface); | |
e0549f8a | 1197 | out: |
1760ef57 | 1198 | return EFI_EXIT(r); |
bee91169 | 1199 | } |
e0549f8a | 1200 | |
6b03cd10 | 1201 | /** |
78a88f79 | 1202 | * efi_get_drivers() - get all drivers associated to a controller |
fae0118e | 1203 | * @handle: handle of the controller |
b72aaa87 | 1204 | * @protocol: protocol GUID (optional) |
78a88f79 MS |
1205 | * @number_of_drivers: number of child controllers |
1206 | * @driver_handle_buffer: handles of the the drivers | |
6b03cd10 | 1207 | * |
3f9b0042 HS |
1208 | * The allocated buffer has to be freed with free(). |
1209 | * | |
78a88f79 | 1210 | * Return: status code |
3f9b0042 | 1211 | */ |
fae0118e | 1212 | static efi_status_t efi_get_drivers(efi_handle_t handle, |
3f9b0042 HS |
1213 | const efi_guid_t *protocol, |
1214 | efi_uintn_t *number_of_drivers, | |
1215 | efi_handle_t **driver_handle_buffer) | |
1216 | { | |
1217 | struct efi_handler *handler; | |
1218 | struct efi_open_protocol_info_item *item; | |
1219 | efi_uintn_t count = 0, i; | |
1220 | bool duplicate; | |
1221 | ||
1222 | /* Count all driver associations */ | |
fae0118e | 1223 | list_for_each_entry(handler, &handle->protocols, link) { |
3f9b0042 HS |
1224 | if (protocol && guidcmp(handler->guid, protocol)) |
1225 | continue; | |
1226 | list_for_each_entry(item, &handler->open_infos, link) { | |
1227 | if (item->info.attributes & | |
1228 | EFI_OPEN_PROTOCOL_BY_DRIVER) | |
1229 | ++count; | |
1230 | } | |
1231 | } | |
66ca24a9 HS |
1232 | *number_of_drivers = 0; |
1233 | if (!count) { | |
1234 | *driver_handle_buffer = NULL; | |
1235 | return EFI_SUCCESS; | |
1236 | } | |
3f9b0042 HS |
1237 | /* |
1238 | * Create buffer. In case of duplicate driver assignments the buffer | |
1239 | * will be too large. But that does not harm. | |
1240 | */ | |
3f9b0042 HS |
1241 | *driver_handle_buffer = calloc(count, sizeof(efi_handle_t)); |
1242 | if (!*driver_handle_buffer) | |
1243 | return EFI_OUT_OF_RESOURCES; | |
1244 | /* Collect unique driver handles */ | |
fae0118e | 1245 | list_for_each_entry(handler, &handle->protocols, link) { |
3f9b0042 HS |
1246 | if (protocol && guidcmp(handler->guid, protocol)) |
1247 | continue; | |
1248 | list_for_each_entry(item, &handler->open_infos, link) { | |
1249 | if (item->info.attributes & | |
1250 | EFI_OPEN_PROTOCOL_BY_DRIVER) { | |
1251 | /* Check this is a new driver */ | |
1252 | duplicate = false; | |
1253 | for (i = 0; i < *number_of_drivers; ++i) { | |
1254 | if ((*driver_handle_buffer)[i] == | |
1255 | item->info.agent_handle) | |
1256 | duplicate = true; | |
1257 | } | |
1258 | /* Copy handle to buffer */ | |
1259 | if (!duplicate) { | |
1260 | i = (*number_of_drivers)++; | |
1261 | (*driver_handle_buffer)[i] = | |
1262 | item->info.agent_handle; | |
1263 | } | |
1264 | } | |
1265 | } | |
1266 | } | |
1267 | return EFI_SUCCESS; | |
1268 | } | |
1269 | ||
6b03cd10 | 1270 | /** |
78a88f79 | 1271 | * efi_disconnect_all_drivers() - disconnect all drivers from a controller |
fae0118e | 1272 | * @handle: handle of the controller |
b72aaa87 | 1273 | * @protocol: protocol GUID (optional) |
78a88f79 | 1274 | * @child_handle: handle of the child to destroy |
3f9b0042 HS |
1275 | * |
1276 | * This function implements the DisconnectController service. | |
3f9b0042 | 1277 | * |
78a88f79 MS |
1278 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
1279 | * details. | |
1280 | * | |
1281 | * Return: status code | |
3f9b0042 | 1282 | */ |
fae0118e HS |
1283 | static efi_status_t efi_disconnect_all_drivers |
1284 | (efi_handle_t handle, | |
1285 | const efi_guid_t *protocol, | |
1286 | efi_handle_t child_handle) | |
3f9b0042 HS |
1287 | { |
1288 | efi_uintn_t number_of_drivers; | |
1289 | efi_handle_t *driver_handle_buffer; | |
1290 | efi_status_t r, ret; | |
1291 | ||
fae0118e | 1292 | ret = efi_get_drivers(handle, protocol, &number_of_drivers, |
3f9b0042 HS |
1293 | &driver_handle_buffer); |
1294 | if (ret != EFI_SUCCESS) | |
1295 | return ret; | |
66ca24a9 HS |
1296 | if (!number_of_drivers) |
1297 | return EFI_SUCCESS; | |
3f9b0042 HS |
1298 | ret = EFI_NOT_FOUND; |
1299 | while (number_of_drivers) { | |
1300 | r = EFI_CALL(efi_disconnect_controller( | |
fae0118e | 1301 | handle, |
3f9b0042 HS |
1302 | driver_handle_buffer[--number_of_drivers], |
1303 | child_handle)); | |
1304 | if (r == EFI_SUCCESS) | |
1305 | ret = r; | |
1306 | } | |
1307 | free(driver_handle_buffer); | |
1308 | return ret; | |
1309 | } | |
1310 | ||
6b03cd10 | 1311 | /** |
9b47f13b HS |
1312 | * efi_uninstall_protocol() - uninstall protocol interface |
1313 | * | |
78a88f79 MS |
1314 | * @handle: handle from which the protocol shall be removed |
1315 | * @protocol: GUID of the protocol to be removed | |
1316 | * @protocol_interface: interface to be removed | |
332468f7 | 1317 | * |
9b47f13b | 1318 | * This function DOES NOT delete a handle without installed protocol. |
78a88f79 MS |
1319 | * |
1320 | * Return: status code | |
332468f7 | 1321 | */ |
9b47f13b HS |
1322 | static efi_status_t efi_uninstall_protocol |
1323 | (efi_handle_t handle, const efi_guid_t *protocol, | |
1324 | void *protocol_interface) | |
bee91169 | 1325 | { |
ad97373b | 1326 | struct efi_object *efiobj; |
5810511d | 1327 | struct efi_handler *handler; |
ad97373b HS |
1328 | struct efi_open_protocol_info_item *item; |
1329 | struct efi_open_protocol_info_item *pos; | |
5810511d | 1330 | efi_status_t r; |
4b6ed0d7 | 1331 | |
ad97373b HS |
1332 | /* Check handle */ |
1333 | efiobj = efi_search_obj(handle); | |
1334 | if (!efiobj) { | |
4b6ed0d7 HS |
1335 | r = EFI_INVALID_PARAMETER; |
1336 | goto out; | |
1337 | } | |
5810511d HS |
1338 | /* Find the protocol on the handle */ |
1339 | r = efi_search_protocol(handle, protocol, &handler); | |
1340 | if (r != EFI_SUCCESS) | |
1341 | goto out; | |
ad97373b HS |
1342 | /* Disconnect controllers */ |
1343 | efi_disconnect_all_drivers(efiobj, protocol, NULL); | |
ad97373b HS |
1344 | /* Close protocol */ |
1345 | list_for_each_entry_safe(item, pos, &handler->open_infos, link) { | |
1346 | if (item->info.attributes == | |
1347 | EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL || | |
1348 | item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL || | |
1349 | item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL) | |
1350 | list_del(&item->link); | |
1351 | } | |
1352 | if (!list_empty(&handler->open_infos)) { | |
1353 | r = EFI_ACCESS_DENIED; | |
1354 | goto out; | |
4b6ed0d7 | 1355 | } |
ad97373b | 1356 | r = efi_remove_protocol(handle, protocol, protocol_interface); |
4b6ed0d7 | 1357 | out: |
9b47f13b HS |
1358 | return r; |
1359 | } | |
1360 | ||
1361 | /** | |
1362 | * efi_uninstall_protocol_interface() - uninstall protocol interface | |
1363 | * @handle: handle from which the protocol shall be removed | |
1364 | * @protocol: GUID of the protocol to be removed | |
1365 | * @protocol_interface: interface to be removed | |
1366 | * | |
1367 | * This function implements the UninstallProtocolInterface service. | |
1368 | * | |
1369 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
1370 | * details. | |
1371 | * | |
1372 | * Return: status code | |
1373 | */ | |
1374 | static efi_status_t EFIAPI efi_uninstall_protocol_interface | |
1375 | (efi_handle_t handle, const efi_guid_t *protocol, | |
1376 | void *protocol_interface) | |
1377 | { | |
1378 | efi_status_t ret; | |
1379 | ||
1380 | EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface); | |
1381 | ||
1382 | ret = efi_uninstall_protocol(handle, protocol, protocol_interface); | |
1383 | if (ret != EFI_SUCCESS) | |
1384 | goto out; | |
1385 | ||
1386 | /* If the last protocol has been removed, delete the handle. */ | |
1387 | if (list_empty(&handle->protocols)) { | |
1388 | list_del(&handle->link); | |
1389 | free(handle); | |
1390 | } | |
1391 | out: | |
1392 | return EFI_EXIT(ret); | |
bee91169 AG |
1393 | } |
1394 | ||
6b03cd10 | 1395 | /** |
78a88f79 MS |
1396 | * efi_register_protocol_notify() - register an event for notification when a |
1397 | * protocol is installed. | |
1398 | * @protocol: GUID of the protocol whose installation shall be notified | |
1399 | * @event: event to be signaled upon installation of the protocol | |
1400 | * @registration: key for retrieving the registration information | |
332468f7 HS |
1401 | * |
1402 | * This function implements the RegisterProtocolNotify service. | |
1403 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1404 | * for details. | |
1405 | * | |
78a88f79 | 1406 | * Return: status code |
332468f7 | 1407 | */ |
64a8aae1 JM |
1408 | efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol, |
1409 | struct efi_event *event, | |
1410 | void **registration) | |
bee91169 | 1411 | { |
ab15d414 HS |
1412 | struct efi_register_notify_event *item; |
1413 | efi_status_t ret = EFI_SUCCESS; | |
1414 | ||
778e6af8 | 1415 | EFI_ENTRY("%pUl, %p, %p", protocol, event, registration); |
ab15d414 HS |
1416 | |
1417 | if (!protocol || !event || !registration) { | |
1418 | ret = EFI_INVALID_PARAMETER; | |
1419 | goto out; | |
1420 | } | |
1421 | ||
1422 | item = calloc(1, sizeof(struct efi_register_notify_event)); | |
1423 | if (!item) { | |
1424 | ret = EFI_OUT_OF_RESOURCES; | |
1425 | goto out; | |
1426 | } | |
1427 | ||
1428 | item->event = event; | |
61e42d94 | 1429 | guidcpy(&item->protocol, protocol); |
f09cea36 | 1430 | INIT_LIST_HEAD(&item->handles); |
ab15d414 HS |
1431 | |
1432 | list_add_tail(&item->link, &efi_register_notify_events); | |
1433 | ||
1434 | *registration = item; | |
1435 | out: | |
1436 | return EFI_EXIT(ret); | |
bee91169 AG |
1437 | } |
1438 | ||
6b03cd10 | 1439 | /** |
78a88f79 | 1440 | * efi_search() - determine if an EFI handle implements a protocol |
6631ca59 | 1441 | * |
78a88f79 MS |
1442 | * @search_type: selection criterion |
1443 | * @protocol: GUID of the protocol | |
fae0118e | 1444 | * @handle: handle |
332468f7 HS |
1445 | * |
1446 | * See the documentation of the LocateHandle service in the UEFI specification. | |
1447 | * | |
78a88f79 | 1448 | * Return: 0 if the handle implements the protocol |
332468f7 | 1449 | */ |
bee91169 | 1450 | static int efi_search(enum efi_locate_search_type search_type, |
ab15d414 | 1451 | const efi_guid_t *protocol, efi_handle_t handle) |
bee91169 | 1452 | { |
42cf1242 | 1453 | efi_status_t ret; |
bee91169 AG |
1454 | |
1455 | switch (search_type) { | |
9f0770ff | 1456 | case ALL_HANDLES: |
bee91169 | 1457 | return 0; |
9f0770ff | 1458 | case BY_PROTOCOL: |
fae0118e | 1459 | ret = efi_search_protocol(handle, protocol, NULL); |
42cf1242 HS |
1460 | return (ret != EFI_SUCCESS); |
1461 | default: | |
1462 | /* Invalid search type */ | |
bee91169 AG |
1463 | return -1; |
1464 | } | |
bee91169 AG |
1465 | } |
1466 | ||
b8abd743 HS |
1467 | /** |
1468 | * efi_check_register_notify_event() - check if registration key is valid | |
1469 | * | |
1470 | * Check that a pointer is a valid registration key as returned by | |
1471 | * RegisterProtocolNotify(). | |
1472 | * | |
1473 | * @key: registration key | |
1474 | * Return: valid registration key or NULL | |
1475 | */ | |
1476 | static struct efi_register_notify_event *efi_check_register_notify_event | |
1477 | (void *key) | |
1478 | { | |
1479 | struct efi_register_notify_event *event; | |
1480 | ||
1481 | list_for_each_entry(event, &efi_register_notify_events, link) { | |
1482 | if (event == (struct efi_register_notify_event *)key) | |
1483 | return event; | |
1484 | } | |
1485 | return NULL; | |
1486 | } | |
1487 | ||
6b03cd10 | 1488 | /** |
78a88f79 | 1489 | * efi_locate_handle() - locate handles implementing a protocol |
ab15d414 HS |
1490 | * |
1491 | * @search_type: selection criterion | |
1492 | * @protocol: GUID of the protocol | |
1493 | * @search_key: registration key | |
1494 | * @buffer_size: size of the buffer to receive the handles in bytes | |
1495 | * @buffer: buffer to receive the relevant handles | |
332468f7 HS |
1496 | * |
1497 | * This function is meant for U-Boot internal calls. For the API implementation | |
1498 | * of the LocateHandle service see efi_locate_handle_ext. | |
1499 | * | |
78a88f79 | 1500 | * Return: status code |
332468f7 | 1501 | */ |
ebf199b9 | 1502 | static efi_status_t efi_locate_handle( |
bee91169 | 1503 | enum efi_locate_search_type search_type, |
5a9682d0 | 1504 | const efi_guid_t *protocol, void *search_key, |
f5a2a938 | 1505 | efi_uintn_t *buffer_size, efi_handle_t *buffer) |
bee91169 | 1506 | { |
caf864e4 | 1507 | struct efi_object *efiobj; |
f5a2a938 | 1508 | efi_uintn_t size = 0; |
b8abd743 | 1509 | struct efi_register_notify_event *event; |
f09cea36 | 1510 | struct efi_protocol_notification *handle = NULL; |
bee91169 | 1511 | |
caf864e4 HS |
1512 | /* Check parameters */ |
1513 | switch (search_type) { | |
1514 | case ALL_HANDLES: | |
1515 | break; | |
1516 | case BY_REGISTER_NOTIFY: | |
1517 | if (!search_key) | |
1518 | return EFI_INVALID_PARAMETER; | |
ab15d414 | 1519 | /* Check that the registration key is valid */ |
b8abd743 | 1520 | event = efi_check_register_notify_event(search_key); |
ab15d414 HS |
1521 | if (!event) |
1522 | return EFI_INVALID_PARAMETER; | |
ab15d414 | 1523 | break; |
caf864e4 HS |
1524 | case BY_PROTOCOL: |
1525 | if (!protocol) | |
1526 | return EFI_INVALID_PARAMETER; | |
1527 | break; | |
1528 | default: | |
1529 | return EFI_INVALID_PARAMETER; | |
1530 | } | |
1531 | ||
bee91169 | 1532 | /* Count how much space we need */ |
f09cea36 HS |
1533 | if (search_type == BY_REGISTER_NOTIFY) { |
1534 | if (list_empty(&event->handles)) | |
1535 | return EFI_NOT_FOUND; | |
1536 | handle = list_first_entry(&event->handles, | |
1537 | struct efi_protocol_notification, | |
1538 | link); | |
1539 | efiobj = handle->handle; | |
1540 | size += sizeof(void *); | |
1541 | } else { | |
1542 | list_for_each_entry(efiobj, &efi_obj_list, link) { | |
1543 | if (!efi_search(search_type, protocol, efiobj)) | |
1544 | size += sizeof(void *); | |
1545 | } | |
1546 | if (size == 0) | |
1547 | return EFI_NOT_FOUND; | |
bee91169 AG |
1548 | } |
1549 | ||
8dfb5e6e HS |
1550 | if (!buffer_size) |
1551 | return EFI_INVALID_PARAMETER; | |
1552 | ||
bee91169 AG |
1553 | if (*buffer_size < size) { |
1554 | *buffer_size = size; | |
26329584 | 1555 | return EFI_BUFFER_TOO_SMALL; |
bee91169 AG |
1556 | } |
1557 | ||
796a78cb | 1558 | *buffer_size = size; |
8dfb5e6e | 1559 | |
0a843196 | 1560 | /* The buffer size is sufficient but there is no buffer */ |
8dfb5e6e HS |
1561 | if (!buffer) |
1562 | return EFI_INVALID_PARAMETER; | |
796a78cb | 1563 | |
bee91169 | 1564 | /* Then fill the array */ |
f09cea36 HS |
1565 | if (search_type == BY_REGISTER_NOTIFY) { |
1566 | *buffer = efiobj; | |
1567 | list_del(&handle->link); | |
1568 | } else { | |
1569 | list_for_each_entry(efiobj, &efi_obj_list, link) { | |
1570 | if (!efi_search(search_type, protocol, efiobj)) | |
1571 | *buffer++ = efiobj; | |
1572 | } | |
bee91169 AG |
1573 | } |
1574 | ||
26329584 HS |
1575 | return EFI_SUCCESS; |
1576 | } | |
1577 | ||
6b03cd10 | 1578 | /** |
78a88f79 MS |
1579 | * efi_locate_handle_ext() - locate handles implementing a protocol. |
1580 | * @search_type: selection criterion | |
1581 | * @protocol: GUID of the protocol | |
1582 | * @search_key: registration key | |
1583 | * @buffer_size: size of the buffer to receive the handles in bytes | |
1584 | * @buffer: buffer to receive the relevant handles | |
332468f7 HS |
1585 | * |
1586 | * This function implements the LocateHandle service. | |
332468f7 | 1587 | * |
78a88f79 MS |
1588 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
1589 | * details. | |
1590 | * | |
1591 | * Return: 0 if the handle implements the protocol | |
332468f7 | 1592 | */ |
26329584 HS |
1593 | static efi_status_t EFIAPI efi_locate_handle_ext( |
1594 | enum efi_locate_search_type search_type, | |
5a9682d0 | 1595 | const efi_guid_t *protocol, void *search_key, |
f5a2a938 | 1596 | efi_uintn_t *buffer_size, efi_handle_t *buffer) |
26329584 | 1597 | { |
778e6af8 | 1598 | EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key, |
26329584 HS |
1599 | buffer_size, buffer); |
1600 | ||
1601 | return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key, | |
1602 | buffer_size, buffer)); | |
bee91169 AG |
1603 | } |
1604 | ||
6b03cd10 | 1605 | /** |
78a88f79 MS |
1606 | * efi_remove_configuration_table() - collapses configuration table entries, |
1607 | * removing index i | |
6b03cd10 | 1608 | * |
78a88f79 | 1609 | * @i: index of the table entry to be removed |
6b03cd10 | 1610 | */ |
d98cdf6a AG |
1611 | static void efi_remove_configuration_table(int i) |
1612 | { | |
4182a129 HS |
1613 | struct efi_configuration_table *this = &systab.tables[i]; |
1614 | struct efi_configuration_table *next = &systab.tables[i + 1]; | |
1615 | struct efi_configuration_table *end = &systab.tables[systab.nr_tables]; | |
d98cdf6a AG |
1616 | |
1617 | memmove(this, next, (ulong)end - (ulong)next); | |
1618 | systab.nr_tables--; | |
1619 | } | |
1620 | ||
6b03cd10 | 1621 | /** |
78a88f79 MS |
1622 | * efi_install_configuration_table() - adds, updates, or removes a |
1623 | * configuration table | |
1624 | * @guid: GUID of the installed table | |
1625 | * @table: table to be installed | |
332468f7 HS |
1626 | * |
1627 | * This function is used for internal calls. For the API implementation of the | |
1628 | * InstallConfigurationTable service see efi_install_configuration_table_ext. | |
1629 | * | |
78a88f79 | 1630 | * Return: status code |
332468f7 | 1631 | */ |
ab9efa97 HS |
1632 | efi_status_t efi_install_configuration_table(const efi_guid_t *guid, |
1633 | void *table) | |
bee91169 | 1634 | { |
b095f3c8 | 1635 | struct efi_event *evt; |
bee91169 AG |
1636 | int i; |
1637 | ||
eb68b4ef HS |
1638 | if (!guid) |
1639 | return EFI_INVALID_PARAMETER; | |
1640 | ||
b72aaa87 | 1641 | /* Check for GUID override */ |
bee91169 | 1642 | for (i = 0; i < systab.nr_tables; i++) { |
4182a129 | 1643 | if (!guidcmp(guid, &systab.tables[i].guid)) { |
d98cdf6a | 1644 | if (table) |
4182a129 | 1645 | systab.tables[i].table = table; |
d98cdf6a AG |
1646 | else |
1647 | efi_remove_configuration_table(i); | |
b095f3c8 | 1648 | goto out; |
bee91169 AG |
1649 | } |
1650 | } | |
1651 | ||
d98cdf6a AG |
1652 | if (!table) |
1653 | return EFI_NOT_FOUND; | |
1654 | ||
bee91169 | 1655 | /* No override, check for overflow */ |
4182a129 | 1656 | if (i >= EFI_MAX_CONFIGURATION_TABLES) |
488bf12d | 1657 | return EFI_OUT_OF_RESOURCES; |
bee91169 AG |
1658 | |
1659 | /* Add a new entry */ | |
61e42d94 | 1660 | guidcpy(&systab.tables[i].guid, guid); |
4182a129 | 1661 | systab.tables[i].table = table; |
aba5e919 | 1662 | systab.nr_tables = i + 1; |
bee91169 | 1663 | |
b095f3c8 | 1664 | out: |
b72aaa87 | 1665 | /* systab.nr_tables may have changed. So we need to update the CRC32 */ |
55d8ee3b HS |
1666 | efi_update_table_header_crc32(&systab.hdr); |
1667 | ||
b095f3c8 HS |
1668 | /* Notify that the configuration table was changed */ |
1669 | list_for_each_entry(evt, &efi_events, link) { | |
1670 | if (evt->group && !guidcmp(evt->group, guid)) { | |
7eaa900e | 1671 | efi_signal_event(evt); |
b095f3c8 HS |
1672 | break; |
1673 | } | |
1674 | } | |
1675 | ||
488bf12d AG |
1676 | return EFI_SUCCESS; |
1677 | } | |
1678 | ||
6b03cd10 | 1679 | /** |
78a88f79 MS |
1680 | * efi_install_configuration_table_ex() - Adds, updates, or removes a |
1681 | * configuration table. | |
1682 | * @guid: GUID of the installed table | |
1683 | * @table: table to be installed | |
332468f7 HS |
1684 | * |
1685 | * This function implements the InstallConfigurationTable service. | |
332468f7 | 1686 | * |
78a88f79 MS |
1687 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
1688 | * details. | |
1689 | * | |
1690 | * Return: status code | |
332468f7 | 1691 | */ |
488bf12d AG |
1692 | static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid, |
1693 | void *table) | |
1694 | { | |
778e6af8 | 1695 | EFI_ENTRY("%pUl, %p", guid, table); |
488bf12d | 1696 | return EFI_EXIT(efi_install_configuration_table(guid, table)); |
bee91169 AG |
1697 | } |
1698 | ||
6b03cd10 | 1699 | /** |
78a88f79 | 1700 | * efi_setup_loaded_image() - initialize a loaded image |
6b03cd10 HS |
1701 | * |
1702 | * Initialize a loaded_image_info and loaded_image_info object with correct | |
95c5553e | 1703 | * protocols, boot-device, etc. |
332468f7 | 1704 | * |
6631ca59 | 1705 | * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error |
16112f9f HS |
1706 | * code is returned. |
1707 | * | |
1708 | * @device_path: device path of the loaded image | |
1709 | * @file_path: file path of the loaded image | |
1710 | * @handle_ptr: handle of the loaded image | |
1711 | * @info_ptr: loaded image protocol | |
1712 | * Return: status code | |
95c5553e | 1713 | */ |
c982874e HS |
1714 | efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path, |
1715 | struct efi_device_path *file_path, | |
1716 | struct efi_loaded_image_obj **handle_ptr, | |
1717 | struct efi_loaded_image **info_ptr) | |
95c5553e | 1718 | { |
48b66230 | 1719 | efi_status_t ret; |
16112f9f HS |
1720 | struct efi_loaded_image *info = NULL; |
1721 | struct efi_loaded_image_obj *obj = NULL; | |
bc8fc328 | 1722 | struct efi_device_path *dp; |
16112f9f HS |
1723 | |
1724 | /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */ | |
1725 | *handle_ptr = NULL; | |
1726 | *info_ptr = NULL; | |
c982874e HS |
1727 | |
1728 | info = calloc(1, sizeof(*info)); | |
1729 | if (!info) | |
1730 | return EFI_OUT_OF_RESOURCES; | |
1731 | obj = calloc(1, sizeof(*obj)); | |
1732 | if (!obj) { | |
1733 | free(info); | |
1734 | return EFI_OUT_OF_RESOURCES; | |
1735 | } | |
cd73aba6 | 1736 | obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE; |
48b66230 | 1737 | |
44549d62 | 1738 | /* Add internal object to object list */ |
d39646a3 | 1739 | efi_add_handle(&obj->header); |
c982874e | 1740 | |
95147313 | 1741 | info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION; |
48b66230 | 1742 | info->file_path = file_path; |
7e1effce | 1743 | info->system_table = &systab; |
48b66230 | 1744 | |
7df5af6f HS |
1745 | if (device_path) { |
1746 | info->device_handle = efi_dp_find_obj(device_path, NULL); | |
bc8fc328 AT |
1747 | |
1748 | dp = efi_dp_append(device_path, file_path); | |
1749 | if (!dp) { | |
1750 | ret = EFI_OUT_OF_RESOURCES; | |
7df5af6f | 1751 | goto failure; |
bc8fc328 AT |
1752 | } |
1753 | } else { | |
1754 | dp = NULL; | |
7df5af6f | 1755 | } |
bc8fc328 AT |
1756 | ret = efi_add_protocol(&obj->header, |
1757 | &efi_guid_loaded_image_device_path, dp); | |
1758 | if (ret != EFI_SUCCESS) | |
1759 | goto failure; | |
95c5553e RC |
1760 | |
1761 | /* | |
1762 | * When asking for the loaded_image interface, just | |
1763 | * return handle which points to loaded_image_info | |
1764 | */ | |
d39646a3 | 1765 | ret = efi_add_protocol(&obj->header, |
c982874e | 1766 | &efi_guid_loaded_image, info); |
48b66230 HS |
1767 | if (ret != EFI_SUCCESS) |
1768 | goto failure; | |
95c5553e | 1769 | |
d5974af7 HS |
1770 | *info_ptr = info; |
1771 | *handle_ptr = obj; | |
16112f9f | 1772 | |
56d92888 | 1773 | return ret; |
48b66230 HS |
1774 | failure: |
1775 | printf("ERROR: Failure to install protocols for loaded image\n"); | |
16112f9f HS |
1776 | efi_delete_handle(&obj->header); |
1777 | free(info); | |
56d92888 | 1778 | return ret; |
95c5553e RC |
1779 | } |
1780 | ||
0e9d2d7b HS |
1781 | /** |
1782 | * efi_locate_device_path() - Get the device path and handle of an device | |
1783 | * implementing a protocol | |
1784 | * @protocol: GUID of the protocol | |
1785 | * @device_path: device path | |
1786 | * @device: handle of the device | |
1787 | * | |
1788 | * This function implements the LocateDevicePath service. | |
1789 | * | |
1790 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
1791 | * details. | |
1792 | * | |
1793 | * Return: status code | |
1794 | */ | |
1795 | static efi_status_t EFIAPI efi_locate_device_path( | |
1796 | const efi_guid_t *protocol, | |
1797 | struct efi_device_path **device_path, | |
1798 | efi_handle_t *device) | |
1799 | { | |
1800 | struct efi_device_path *dp; | |
1801 | size_t i; | |
1802 | struct efi_handler *handler; | |
1803 | efi_handle_t *handles; | |
1804 | size_t len, len_dp; | |
1805 | size_t len_best = 0; | |
1806 | efi_uintn_t no_handles; | |
1807 | u8 *remainder; | |
1808 | efi_status_t ret; | |
1809 | ||
1810 | EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device); | |
1811 | ||
1812 | if (!protocol || !device_path || !*device_path) { | |
1813 | ret = EFI_INVALID_PARAMETER; | |
1814 | goto out; | |
1815 | } | |
1816 | ||
1817 | /* Find end of device path */ | |
1818 | len = efi_dp_instance_size(*device_path); | |
1819 | ||
1820 | /* Get all handles implementing the protocol */ | |
1821 | ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL, | |
1822 | &no_handles, &handles)); | |
1823 | if (ret != EFI_SUCCESS) | |
1824 | goto out; | |
1825 | ||
1826 | for (i = 0; i < no_handles; ++i) { | |
1827 | /* Find the device path protocol */ | |
1828 | ret = efi_search_protocol(handles[i], &efi_guid_device_path, | |
1829 | &handler); | |
1830 | if (ret != EFI_SUCCESS) | |
1831 | continue; | |
1832 | dp = (struct efi_device_path *)handler->protocol_interface; | |
1833 | len_dp = efi_dp_instance_size(dp); | |
1834 | /* | |
1835 | * This handle can only be a better fit | |
1836 | * if its device path length is longer than the best fit and | |
1837 | * if its device path length is shorter of equal the searched | |
1838 | * device path. | |
1839 | */ | |
1840 | if (len_dp <= len_best || len_dp > len) | |
1841 | continue; | |
1842 | /* Check if dp is a subpath of device_path */ | |
1843 | if (memcmp(*device_path, dp, len_dp)) | |
1844 | continue; | |
1845 | if (!device) { | |
1846 | ret = EFI_INVALID_PARAMETER; | |
1847 | goto out; | |
1848 | } | |
1849 | *device = handles[i]; | |
1850 | len_best = len_dp; | |
1851 | } | |
1852 | if (len_best) { | |
1853 | remainder = (u8 *)*device_path + len_best; | |
1854 | *device_path = (struct efi_device_path *)remainder; | |
1855 | ret = EFI_SUCCESS; | |
1856 | } else { | |
1857 | ret = EFI_NOT_FOUND; | |
1858 | } | |
1859 | out: | |
1860 | return EFI_EXIT(ret); | |
1861 | } | |
1862 | ||
6b03cd10 | 1863 | /** |
0e074d12 | 1864 | * efi_load_image_from_file() - load an image from file system |
332468f7 | 1865 | * |
0e18f584 HS |
1866 | * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the |
1867 | * callers obligation to update the memory type as needed. | |
1868 | * | |
c06c55b1 HS |
1869 | * @file_path: the path of the image to load |
1870 | * @buffer: buffer containing the loaded image | |
1871 | * @size: size of the loaded image | |
1872 | * Return: status code | |
332468f7 | 1873 | */ |
6b95b38c | 1874 | static |
0e074d12 | 1875 | efi_status_t efi_load_image_from_file(struct efi_device_path *file_path, |
0e18f584 | 1876 | void **buffer, efi_uintn_t *size) |
838ee4b4 | 1877 | { |
838ee4b4 | 1878 | struct efi_file_handle *f; |
0e074d12 | 1879 | efi_status_t ret; |
0e18f584 | 1880 | u64 addr; |
b6dd5777 | 1881 | efi_uintn_t bs; |
838ee4b4 | 1882 | |
0e18f584 | 1883 | /* Open file */ |
838ee4b4 RC |
1884 | f = efi_file_from_path(file_path); |
1885 | if (!f) | |
20000038 | 1886 | return EFI_NOT_FOUND; |
838ee4b4 | 1887 | |
ac30aad2 | 1888 | ret = efi_file_size(f, &bs); |
0e18f584 | 1889 | if (ret != EFI_SUCCESS) |
838ee4b4 RC |
1890 | goto error; |
1891 | ||
0e18f584 HS |
1892 | /* |
1893 | * When reading the file we do not yet know if it contains an | |
1894 | * application, a boottime driver, or a runtime driver. So here we | |
1895 | * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to | |
1896 | * update the reservation according to the image type. | |
1897 | */ | |
0e18f584 HS |
1898 | ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, |
1899 | EFI_BOOT_SERVICES_DATA, | |
1900 | efi_size_in_pages(bs), &addr); | |
838ee4b4 | 1901 | if (ret != EFI_SUCCESS) { |
0e18f584 HS |
1902 | ret = EFI_OUT_OF_RESOURCES; |
1903 | goto error; | |
838ee4b4 RC |
1904 | } |
1905 | ||
0e18f584 HS |
1906 | /* Read file */ |
1907 | EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr)); | |
1908 | if (ret != EFI_SUCCESS) | |
1909 | efi_free_pages(addr, efi_size_in_pages(bs)); | |
1910 | *buffer = (void *)(uintptr_t)addr; | |
1911 | *size = bs; | |
1912 | error: | |
1913 | EFI_CALL(f->close(f)); | |
838ee4b4 RC |
1914 | return ret; |
1915 | } | |
1916 | ||
0e074d12 HS |
1917 | /** |
1918 | * efi_load_image_from_path() - load an image using a file path | |
1919 | * | |
1920 | * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the | |
1921 | * callers obligation to update the memory type as needed. | |
1922 | * | |
1923 | * @boot_policy: true for request originating from the boot manager | |
1924 | * @file_path: the path of the image to load | |
1925 | * @buffer: buffer containing the loaded image | |
1926 | * @size: size of the loaded image | |
1927 | * Return: status code | |
1928 | */ | |
1929 | static | |
1930 | efi_status_t efi_load_image_from_path(bool boot_policy, | |
1931 | struct efi_device_path *file_path, | |
1932 | void **buffer, efi_uintn_t *size) | |
1933 | { | |
1934 | efi_handle_t device; | |
1935 | efi_status_t ret; | |
1936 | struct efi_device_path *dp; | |
3da0b285 HS |
1937 | struct efi_load_file_protocol *load_file_protocol = NULL; |
1938 | efi_uintn_t buffer_size; | |
1939 | uint64_t addr, pages; | |
1940 | const efi_guid_t *guid; | |
0e074d12 HS |
1941 | |
1942 | /* In case of failure nothing is returned */ | |
1943 | *buffer = NULL; | |
1944 | *size = 0; | |
1945 | ||
1946 | dp = file_path; | |
1947 | ret = EFI_CALL(efi_locate_device_path( | |
1948 | &efi_simple_file_system_protocol_guid, &dp, &device)); | |
1949 | if (ret == EFI_SUCCESS) | |
1950 | return efi_load_image_from_file(file_path, buffer, size); | |
3da0b285 HS |
1951 | |
1952 | ret = EFI_CALL(efi_locate_device_path( | |
1953 | &efi_guid_load_file_protocol, &dp, &device)); | |
1954 | if (ret == EFI_SUCCESS) { | |
1955 | guid = &efi_guid_load_file_protocol; | |
1956 | } else if (!boot_policy) { | |
1957 | guid = &efi_guid_load_file2_protocol; | |
1958 | ret = EFI_CALL(efi_locate_device_path(guid, &dp, &device)); | |
1959 | } | |
1960 | if (ret != EFI_SUCCESS) | |
1961 | return EFI_NOT_FOUND; | |
1962 | ret = EFI_CALL(efi_handle_protocol(device, guid, | |
1963 | (void **)&load_file_protocol)); | |
1964 | if (ret != EFI_SUCCESS) | |
1965 | return EFI_NOT_FOUND; | |
1966 | buffer_size = 0; | |
1967 | ret = load_file_protocol->load_file(load_file_protocol, dp, | |
1968 | boot_policy, &buffer_size, | |
1969 | NULL); | |
1970 | if (ret != EFI_BUFFER_TOO_SMALL) | |
1971 | goto out; | |
1972 | pages = efi_size_in_pages(buffer_size); | |
1973 | ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA, | |
1974 | pages, &addr); | |
1975 | if (ret != EFI_SUCCESS) { | |
1976 | ret = EFI_OUT_OF_RESOURCES; | |
1977 | goto out; | |
1978 | } | |
1979 | ret = EFI_CALL(load_file_protocol->load_file( | |
1980 | load_file_protocol, dp, boot_policy, | |
1981 | &buffer_size, (void *)(uintptr_t)addr)); | |
1982 | if (ret != EFI_SUCCESS) | |
1983 | efi_free_pages(addr, pages); | |
1984 | out: | |
6e8c28cf | 1985 | EFI_CALL(efi_close_protocol(device, guid, efi_root, NULL)); |
3da0b285 HS |
1986 | if (ret == EFI_SUCCESS) { |
1987 | *buffer = (void *)(uintptr_t)addr; | |
1988 | *size = buffer_size; | |
1989 | } | |
1990 | ||
1991 | return ret; | |
0e074d12 HS |
1992 | } |
1993 | ||
6b03cd10 | 1994 | /** |
78a88f79 MS |
1995 | * efi_load_image() - load an EFI image into memory |
1996 | * @boot_policy: true for request originating from the boot manager | |
1997 | * @parent_image: the caller's image handle | |
1998 | * @file_path: the path of the image to load | |
1999 | * @source_buffer: memory location from which the image is installed | |
2000 | * @source_size: size of the memory area from which the image is installed | |
2001 | * @image_handle: handle for the newly installed image | |
332468f7 HS |
2002 | * |
2003 | * This function implements the LoadImage service. | |
78a88f79 | 2004 | * |
332468f7 HS |
2005 | * See the Unified Extensible Firmware Interface (UEFI) specification |
2006 | * for details. | |
2007 | * | |
78a88f79 | 2008 | * Return: status code |
332468f7 | 2009 | */ |
d7e0b010 AT |
2010 | efi_status_t EFIAPI efi_load_image(bool boot_policy, |
2011 | efi_handle_t parent_image, | |
2012 | struct efi_device_path *file_path, | |
2013 | void *source_buffer, | |
2014 | efi_uintn_t source_size, | |
2015 | efi_handle_t *image_handle) | |
bee91169 | 2016 | { |
0e18f584 | 2017 | struct efi_device_path *dp, *fp; |
1c3b2f4a | 2018 | struct efi_loaded_image *info = NULL; |
c982874e HS |
2019 | struct efi_loaded_image_obj **image_obj = |
2020 | (struct efi_loaded_image_obj **)image_handle; | |
b9b17598 | 2021 | efi_status_t ret; |
b0c3c346 | 2022 | void *dest_buffer; |
bee91169 | 2023 | |
7fb96a10 | 2024 | EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image, |
bee91169 | 2025 | file_path, source_buffer, source_size, image_handle); |
838ee4b4 | 2026 | |
e4afcb28 HS |
2027 | if (!image_handle || (!source_buffer && !file_path) || |
2028 | !efi_search_obj(parent_image) || | |
2029 | /* The parent image handle must refer to a loaded image */ | |
2030 | !parent_image->type) { | |
84a918e8 HS |
2031 | ret = EFI_INVALID_PARAMETER; |
2032 | goto error; | |
2033 | } | |
28a4fd46 | 2034 | |
838ee4b4 | 2035 | if (!source_buffer) { |
c06c55b1 HS |
2036 | ret = efi_load_image_from_path(boot_policy, file_path, |
2037 | &dest_buffer, &source_size); | |
b9b17598 | 2038 | if (ret != EFI_SUCCESS) |
0e18f584 | 2039 | goto error; |
838ee4b4 | 2040 | } else { |
b0c3c346 | 2041 | dest_buffer = source_buffer; |
838ee4b4 | 2042 | } |
1e15a9cb HS |
2043 | /* split file_path which contains both the device and file parts */ |
2044 | efi_dp_split_file_path(file_path, &dp, &fp); | |
0e18f584 | 2045 | ret = efi_setup_loaded_image(dp, fp, image_obj, &info); |
b0c3c346 | 2046 | if (ret == EFI_SUCCESS) |
4540dabd | 2047 | ret = efi_load_pe(*image_obj, dest_buffer, source_size, info); |
b0c3c346 HS |
2048 | if (!source_buffer) |
2049 | /* Release buffer to which file was loaded */ | |
2050 | efi_free_pages((uintptr_t)dest_buffer, | |
2051 | efi_size_in_pages(source_size)); | |
4540dabd | 2052 | if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) { |
b0c3c346 HS |
2053 | info->system_table = &systab; |
2054 | info->parent_handle = parent_image; | |
2055 | } else { | |
2056 | /* The image is invalid. Release all associated resources. */ | |
2057 | efi_delete_handle(*image_handle); | |
2058 | *image_handle = NULL; | |
2059 | free(info); | |
2060 | } | |
28a4fd46 | 2061 | error: |
b9b17598 | 2062 | return EFI_EXIT(ret); |
bee91169 AG |
2063 | } |
2064 | ||
f31239ac AG |
2065 | /** |
2066 | * efi_exit_caches() - fix up caches for EFI payloads if necessary | |
2067 | */ | |
2068 | static void efi_exit_caches(void) | |
2069 | { | |
6f3badb6 | 2070 | #if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND) |
f31239ac | 2071 | /* |
6f3badb6 HS |
2072 | * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if |
2073 | * caches are enabled. | |
2074 | * | |
2075 | * TODO: | |
2076 | * According to the UEFI spec caches that can be managed via CP15 | |
2077 | * operations should be enabled. Caches requiring platform information | |
2078 | * to manage should be disabled. This should not happen in | |
2079 | * ExitBootServices() but before invoking any UEFI binary is invoked. | |
2080 | * | |
2081 | * We want to keep the current workaround while GRUB prior to version | |
2082 | * 2.04 is still in use. | |
f31239ac | 2083 | */ |
6f3badb6 | 2084 | cleanup_before_linux(); |
f31239ac AG |
2085 | #endif |
2086 | } | |
2087 | ||
6b03cd10 | 2088 | /** |
78a88f79 MS |
2089 | * efi_exit_boot_services() - stop all boot services |
2090 | * @image_handle: handle of the loaded image | |
2091 | * @map_key: key of the memory map | |
332468f7 HS |
2092 | * |
2093 | * This function implements the ExitBootServices service. | |
78a88f79 | 2094 | * |
332468f7 HS |
2095 | * See the Unified Extensible Firmware Interface (UEFI) specification |
2096 | * for details. | |
2097 | * | |
78a88f79 MS |
2098 | * All timer events are disabled. For exit boot services events the |
2099 | * notification function is called. The boot services are disabled in the | |
2100 | * system table. | |
cc20ed03 | 2101 | * |
78a88f79 | 2102 | * Return: status code |
332468f7 | 2103 | */ |
2074f700 | 2104 | static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle, |
b015ab57 | 2105 | efi_uintn_t map_key) |
bee91169 | 2106 | { |
14b40487 | 2107 | struct efi_event *evt, *next_event; |
98967379 | 2108 | efi_status_t ret = EFI_SUCCESS; |
152a263c | 2109 | |
b015ab57 | 2110 | EFI_ENTRY("%p, %zx", image_handle, map_key); |
bee91169 | 2111 | |
1fcb7ea2 | 2112 | /* Check that the caller has read the current memory map */ |
98967379 HS |
2113 | if (map_key != efi_memory_map_key) { |
2114 | ret = EFI_INVALID_PARAMETER; | |
2115 | goto out; | |
2116 | } | |
1fcb7ea2 | 2117 | |
cc20ed03 HS |
2118 | /* Check if ExitBootServices has already been called */ |
2119 | if (!systab.boottime) | |
98967379 | 2120 | goto out; |
cc20ed03 | 2121 | |
7eaa900e HS |
2122 | /* Stop all timer related activities */ |
2123 | timers_enabled = false; | |
2124 | ||
b095f3c8 HS |
2125 | /* Add related events to the event group */ |
2126 | list_for_each_entry(evt, &efi_events, link) { | |
2127 | if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES) | |
2128 | evt->group = &efi_guid_event_group_exit_boot_services; | |
2129 | } | |
152a263c | 2130 | /* Notify that ExitBootServices is invoked. */ |
43bce442 | 2131 | list_for_each_entry(evt, &efi_events, link) { |
b095f3c8 HS |
2132 | if (evt->group && |
2133 | !guidcmp(evt->group, | |
2134 | &efi_guid_event_group_exit_boot_services)) { | |
7eaa900e | 2135 | efi_signal_event(evt); |
b095f3c8 HS |
2136 | break; |
2137 | } | |
152a263c | 2138 | } |
152a263c | 2139 | |
7eaa900e HS |
2140 | /* Make sure that notification functions are not called anymore */ |
2141 | efi_tpl = TPL_HIGH_LEVEL; | |
2142 | ||
29018abb HS |
2143 | /* Notify variable services */ |
2144 | efi_variables_boot_exit_notify(); | |
ad644e7c | 2145 | |
14b40487 HS |
2146 | /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */ |
2147 | list_for_each_entry_safe(evt, next_event, &efi_events, link) { | |
2148 | if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) | |
2149 | list_del(&evt->link); | |
2150 | } | |
2151 | ||
fccd3d9c | 2152 | if (!efi_st_keep_devices) { |
db6288de | 2153 | if (IS_ENABLED(CONFIG_USB_DEVICE)) |
fccd3d9c HS |
2154 | udc_disconnect(); |
2155 | board_quiesce_devices(); | |
2156 | dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); | |
2157 | } | |
b7b8410a | 2158 | |
7f95104d HS |
2159 | /* Patch out unsupported runtime function */ |
2160 | efi_runtime_detach(); | |
2161 | ||
f31239ac AG |
2162 | /* Fix up caches for EFI payloads if necessary */ |
2163 | efi_exit_caches(); | |
2164 | ||
bee91169 AG |
2165 | /* This stops all lingering devices */ |
2166 | bootm_disable_interrupts(); | |
2167 | ||
b72aaa87 | 2168 | /* Disable boot time services */ |
cc20ed03 HS |
2169 | systab.con_in_handle = NULL; |
2170 | systab.con_in = NULL; | |
2171 | systab.con_out_handle = NULL; | |
2172 | systab.con_out = NULL; | |
2173 | systab.stderr_handle = NULL; | |
2174 | systab.std_err = NULL; | |
2175 | systab.boottime = NULL; | |
2176 | ||
2177 | /* Recalculate CRC32 */ | |
640adadf | 2178 | efi_update_table_header_crc32(&systab.hdr); |
cc20ed03 | 2179 | |
bee91169 | 2180 | /* Give the payload some time to boot */ |
b3d60900 | 2181 | efi_set_watchdog(0); |
bee91169 | 2182 | WATCHDOG_RESET(); |
98967379 HS |
2183 | out: |
2184 | return EFI_EXIT(ret); | |
bee91169 AG |
2185 | } |
2186 | ||
6b03cd10 | 2187 | /** |
78a88f79 MS |
2188 | * efi_get_next_monotonic_count() - get next value of the counter |
2189 | * @count: returned value of the counter | |
332468f7 HS |
2190 | * |
2191 | * This function implements the NextMonotonicCount service. | |
332468f7 | 2192 | * |
78a88f79 MS |
2193 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2194 | * details. | |
2195 | * | |
2196 | * Return: status code | |
332468f7 | 2197 | */ |
bee91169 AG |
2198 | static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count) |
2199 | { | |
ab9efa97 | 2200 | static uint64_t mono; |
1344f7d0 | 2201 | efi_status_t ret; |
ab9efa97 | 2202 | |
bee91169 | 2203 | EFI_ENTRY("%p", count); |
1344f7d0 HS |
2204 | if (!count) { |
2205 | ret = EFI_INVALID_PARAMETER; | |
2206 | goto out; | |
2207 | } | |
bee91169 | 2208 | *count = mono++; |
1344f7d0 HS |
2209 | ret = EFI_SUCCESS; |
2210 | out: | |
2211 | return EFI_EXIT(ret); | |
bee91169 AG |
2212 | } |
2213 | ||
6b03cd10 | 2214 | /** |
78a88f79 MS |
2215 | * efi_stall() - sleep |
2216 | * @microseconds: period to sleep in microseconds | |
332468f7 | 2217 | * |
78a88f79 | 2218 | * This function implements the Stall service. |
332468f7 | 2219 | * |
78a88f79 MS |
2220 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2221 | * details. | |
2222 | * | |
2223 | * Return: status code | |
332468f7 | 2224 | */ |
bee91169 AG |
2225 | static efi_status_t EFIAPI efi_stall(unsigned long microseconds) |
2226 | { | |
22f23db4 HS |
2227 | u64 end_tick; |
2228 | ||
bee91169 | 2229 | EFI_ENTRY("%ld", microseconds); |
22f23db4 HS |
2230 | |
2231 | end_tick = get_ticks() + usec_to_tick(microseconds); | |
2232 | while (get_ticks() < end_tick) | |
2233 | efi_timer_check(); | |
2234 | ||
bee91169 AG |
2235 | return EFI_EXIT(EFI_SUCCESS); |
2236 | } | |
2237 | ||
6b03cd10 | 2238 | /** |
78a88f79 MS |
2239 | * efi_set_watchdog_timer() - reset the watchdog timer |
2240 | * @timeout: seconds before reset by watchdog | |
2241 | * @watchdog_code: code to be logged when resetting | |
2242 | * @data_size: size of buffer in bytes | |
2243 | * @watchdog_data: buffer with data describing the reset reason | |
332468f7 | 2244 | * |
b3d60900 | 2245 | * This function implements the SetWatchdogTimer service. |
332468f7 | 2246 | * |
78a88f79 MS |
2247 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2248 | * details. | |
2249 | * | |
2250 | * Return: status code | |
332468f7 | 2251 | */ |
bee91169 AG |
2252 | static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout, |
2253 | uint64_t watchdog_code, | |
2254 | unsigned long data_size, | |
2255 | uint16_t *watchdog_data) | |
2256 | { | |
dee37fc9 | 2257 | EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code, |
bee91169 | 2258 | data_size, watchdog_data); |
b3d60900 | 2259 | return EFI_EXIT(efi_set_watchdog(timeout)); |
bee91169 AG |
2260 | } |
2261 | ||
6b03cd10 | 2262 | /** |
78a88f79 MS |
2263 | * efi_close_protocol() - close a protocol |
2264 | * @handle: handle on which the protocol shall be closed | |
2265 | * @protocol: GUID of the protocol to close | |
2266 | * @agent_handle: handle of the driver | |
2267 | * @controller_handle: handle of the controller | |
332468f7 HS |
2268 | * |
2269 | * This function implements the CloseProtocol service. | |
332468f7 | 2270 | * |
78a88f79 MS |
2271 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2272 | * details. | |
2273 | * | |
2274 | * Return: status code | |
332468f7 | 2275 | */ |
b51ec639 AT |
2276 | efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle, |
2277 | const efi_guid_t *protocol, | |
2278 | efi_handle_t agent_handle, | |
2279 | efi_handle_t controller_handle) | |
bee91169 | 2280 | { |
3b8a489c HS |
2281 | struct efi_handler *handler; |
2282 | struct efi_open_protocol_info_item *item; | |
2283 | struct efi_open_protocol_info_item *pos; | |
2284 | efi_status_t r; | |
2285 | ||
778e6af8 | 2286 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle, |
bee91169 | 2287 | controller_handle); |
3b8a489c | 2288 | |
ec163faa HS |
2289 | if (!efi_search_obj(agent_handle) || |
2290 | (controller_handle && !efi_search_obj(controller_handle))) { | |
3b8a489c HS |
2291 | r = EFI_INVALID_PARAMETER; |
2292 | goto out; | |
2293 | } | |
2294 | r = efi_search_protocol(handle, protocol, &handler); | |
2295 | if (r != EFI_SUCCESS) | |
2296 | goto out; | |
2297 | ||
2298 | r = EFI_NOT_FOUND; | |
2299 | list_for_each_entry_safe(item, pos, &handler->open_infos, link) { | |
2300 | if (item->info.agent_handle == agent_handle && | |
2301 | item->info.controller_handle == controller_handle) { | |
2302 | efi_delete_open_info(item); | |
2303 | r = EFI_SUCCESS; | |
3b8a489c HS |
2304 | } |
2305 | } | |
2306 | out: | |
2307 | return EFI_EXIT(r); | |
bee91169 AG |
2308 | } |
2309 | ||
6b03cd10 | 2310 | /** |
78a88f79 MS |
2311 | * efi_open_protocol_information() - provide information about then open status |
2312 | * of a protocol on a handle | |
2313 | * @handle: handle for which the information shall be retrieved | |
2314 | * @protocol: GUID of the protocol | |
2315 | * @entry_buffer: buffer to receive the open protocol information | |
2316 | * @entry_count: number of entries available in the buffer | |
332468f7 HS |
2317 | * |
2318 | * This function implements the OpenProtocolInformation service. | |
332468f7 | 2319 | * |
78a88f79 MS |
2320 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2321 | * details. | |
2322 | * | |
2323 | * Return: status code | |
332468f7 | 2324 | */ |
ab9efa97 HS |
2325 | static efi_status_t EFIAPI efi_open_protocol_information( |
2326 | efi_handle_t handle, const efi_guid_t *protocol, | |
bee91169 | 2327 | struct efi_open_protocol_info_entry **entry_buffer, |
f5a2a938 | 2328 | efi_uintn_t *entry_count) |
bee91169 | 2329 | { |
e3fbbc36 HS |
2330 | unsigned long buffer_size; |
2331 | unsigned long count; | |
2332 | struct efi_handler *handler; | |
2333 | struct efi_open_protocol_info_item *item; | |
2334 | efi_status_t r; | |
2335 | ||
778e6af8 | 2336 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer, |
bee91169 | 2337 | entry_count); |
e3fbbc36 HS |
2338 | |
2339 | /* Check parameters */ | |
2340 | if (!entry_buffer) { | |
2341 | r = EFI_INVALID_PARAMETER; | |
2342 | goto out; | |
2343 | } | |
2344 | r = efi_search_protocol(handle, protocol, &handler); | |
2345 | if (r != EFI_SUCCESS) | |
2346 | goto out; | |
2347 | ||
2348 | /* Count entries */ | |
2349 | count = 0; | |
2350 | list_for_each_entry(item, &handler->open_infos, link) { | |
2351 | if (item->info.open_count) | |
2352 | ++count; | |
2353 | } | |
2354 | *entry_count = count; | |
2355 | *entry_buffer = NULL; | |
2356 | if (!count) { | |
2357 | r = EFI_SUCCESS; | |
2358 | goto out; | |
2359 | } | |
2360 | ||
2361 | /* Copy entries */ | |
2362 | buffer_size = count * sizeof(struct efi_open_protocol_info_entry); | |
eee6530e | 2363 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
e3fbbc36 HS |
2364 | (void **)entry_buffer); |
2365 | if (r != EFI_SUCCESS) | |
2366 | goto out; | |
2367 | list_for_each_entry_reverse(item, &handler->open_infos, link) { | |
2368 | if (item->info.open_count) | |
2369 | (*entry_buffer)[--count] = item->info; | |
2370 | } | |
2371 | out: | |
2372 | return EFI_EXIT(r); | |
bee91169 AG |
2373 | } |
2374 | ||
6b03cd10 | 2375 | /** |
78a88f79 MS |
2376 | * efi_protocols_per_handle() - get protocols installed on a handle |
2377 | * @handle: handle for which the information is retrieved | |
2378 | * @protocol_buffer: buffer with protocol GUIDs | |
2379 | * @protocol_buffer_count: number of entries in the buffer | |
332468f7 HS |
2380 | * |
2381 | * This function implements the ProtocolsPerHandleService. | |
332468f7 | 2382 | * |
78a88f79 MS |
2383 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2384 | * details. | |
2385 | * | |
2386 | * Return: status code | |
332468f7 | 2387 | */ |
2074f700 HS |
2388 | static efi_status_t EFIAPI efi_protocols_per_handle( |
2389 | efi_handle_t handle, efi_guid_t ***protocol_buffer, | |
f5a2a938 | 2390 | efi_uintn_t *protocol_buffer_count) |
bee91169 | 2391 | { |
c0ebfc86 HS |
2392 | unsigned long buffer_size; |
2393 | struct efi_object *efiobj; | |
69fb6b1a | 2394 | struct list_head *protocol_handle; |
c0ebfc86 HS |
2395 | efi_status_t r; |
2396 | ||
bee91169 AG |
2397 | EFI_ENTRY("%p, %p, %p", handle, protocol_buffer, |
2398 | protocol_buffer_count); | |
c0ebfc86 HS |
2399 | |
2400 | if (!handle || !protocol_buffer || !protocol_buffer_count) | |
2401 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2402 | ||
2403 | *protocol_buffer = NULL; | |
661c8327 | 2404 | *protocol_buffer_count = 0; |
c0ebfc86 | 2405 | |
69fb6b1a HS |
2406 | efiobj = efi_search_obj(handle); |
2407 | if (!efiobj) | |
2408 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
c0ebfc86 | 2409 | |
69fb6b1a HS |
2410 | /* Count protocols */ |
2411 | list_for_each(protocol_handle, &efiobj->protocols) { | |
2412 | ++*protocol_buffer_count; | |
2413 | } | |
2414 | ||
b72aaa87 | 2415 | /* Copy GUIDs */ |
69fb6b1a HS |
2416 | if (*protocol_buffer_count) { |
2417 | size_t j = 0; | |
2418 | ||
2419 | buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count; | |
eee6530e | 2420 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
69fb6b1a HS |
2421 | (void **)protocol_buffer); |
2422 | if (r != EFI_SUCCESS) | |
2423 | return EFI_EXIT(r); | |
2424 | list_for_each(protocol_handle, &efiobj->protocols) { | |
2425 | struct efi_handler *protocol; | |
2426 | ||
2427 | protocol = list_entry(protocol_handle, | |
2428 | struct efi_handler, link); | |
2429 | (*protocol_buffer)[j] = (void *)protocol->guid; | |
2430 | ++j; | |
c0ebfc86 | 2431 | } |
c0ebfc86 HS |
2432 | } |
2433 | ||
2434 | return EFI_EXIT(EFI_SUCCESS); | |
bee91169 AG |
2435 | } |
2436 | ||
6b03cd10 | 2437 | /** |
78a88f79 MS |
2438 | * efi_locate_handle_buffer() - locate handles implementing a protocol |
2439 | * @search_type: selection criterion | |
2440 | * @protocol: GUID of the protocol | |
2441 | * @search_key: registration key | |
2442 | * @no_handles: number of returned handles | |
2443 | * @buffer: buffer with the returned handles | |
332468f7 HS |
2444 | * |
2445 | * This function implements the LocateHandleBuffer service. | |
332468f7 | 2446 | * |
78a88f79 MS |
2447 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2448 | * details. | |
2449 | * | |
2450 | * Return: status code | |
332468f7 | 2451 | */ |
b51ec639 | 2452 | efi_status_t EFIAPI efi_locate_handle_buffer( |
bee91169 | 2453 | enum efi_locate_search_type search_type, |
5a9682d0 | 2454 | const efi_guid_t *protocol, void *search_key, |
f5a2a938 | 2455 | efi_uintn_t *no_handles, efi_handle_t **buffer) |
bee91169 | 2456 | { |
c2e703f9 | 2457 | efi_status_t r; |
f5a2a938 | 2458 | efi_uintn_t buffer_size = 0; |
c2e703f9 | 2459 | |
778e6af8 | 2460 | EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key, |
bee91169 | 2461 | no_handles, buffer); |
c2e703f9 HS |
2462 | |
2463 | if (!no_handles || !buffer) { | |
2464 | r = EFI_INVALID_PARAMETER; | |
2465 | goto out; | |
2466 | } | |
2467 | *no_handles = 0; | |
2468 | *buffer = NULL; | |
2469 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, | |
2470 | *buffer); | |
2471 | if (r != EFI_BUFFER_TOO_SMALL) | |
2472 | goto out; | |
eee6530e | 2473 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
c2e703f9 HS |
2474 | (void **)buffer); |
2475 | if (r != EFI_SUCCESS) | |
2476 | goto out; | |
2477 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, | |
2478 | *buffer); | |
2479 | if (r == EFI_SUCCESS) | |
2074f700 | 2480 | *no_handles = buffer_size / sizeof(efi_handle_t); |
c2e703f9 HS |
2481 | out: |
2482 | return EFI_EXIT(r); | |
bee91169 AG |
2483 | } |
2484 | ||
6b03cd10 | 2485 | /** |
78a88f79 MS |
2486 | * efi_locate_protocol() - find an interface implementing a protocol |
2487 | * @protocol: GUID of the protocol | |
2488 | * @registration: registration key passed to the notification function | |
2489 | * @protocol_interface: interface implementing the protocol | |
332468f7 HS |
2490 | * |
2491 | * This function implements the LocateProtocol service. | |
332468f7 | 2492 | * |
78a88f79 MS |
2493 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2494 | * details. | |
2495 | * | |
2496 | * Return: status code | |
332468f7 | 2497 | */ |
5a9682d0 | 2498 | static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol, |
bee91169 AG |
2499 | void *registration, |
2500 | void **protocol_interface) | |
2501 | { | |
e31b3b16 | 2502 | struct efi_handler *handler; |
9172cd91 | 2503 | efi_status_t ret; |
e31b3b16 | 2504 | struct efi_object *efiobj; |
bee91169 | 2505 | |
778e6af8 | 2506 | EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface); |
88adae5e | 2507 | |
e31b3b16 HS |
2508 | /* |
2509 | * The UEFI spec explicitly requires a protocol even if a registration | |
2510 | * key is provided. This differs from the logic in LocateHandle(). | |
2511 | */ | |
88adae5e HS |
2512 | if (!protocol || !protocol_interface) |
2513 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2514 | ||
e31b3b16 HS |
2515 | if (registration) { |
2516 | struct efi_register_notify_event *event; | |
2517 | struct efi_protocol_notification *handle; | |
9172cd91 | 2518 | |
e31b3b16 HS |
2519 | event = efi_check_register_notify_event(registration); |
2520 | if (!event) | |
2521 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2522 | /* | |
2523 | * The UEFI spec requires to return EFI_NOT_FOUND if no | |
2524 | * protocol instance matches protocol and registration. | |
2525 | * So let's do the same for a mismatch between protocol and | |
2526 | * registration. | |
2527 | */ | |
2528 | if (guidcmp(&event->protocol, protocol)) | |
2529 | goto not_found; | |
2530 | if (list_empty(&event->handles)) | |
2531 | goto not_found; | |
2532 | handle = list_first_entry(&event->handles, | |
2533 | struct efi_protocol_notification, | |
2534 | link); | |
2535 | efiobj = handle->handle; | |
2536 | list_del(&handle->link); | |
2537 | free(handle); | |
fae0118e | 2538 | ret = efi_search_protocol(efiobj, protocol, &handler); |
e31b3b16 HS |
2539 | if (ret == EFI_SUCCESS) |
2540 | goto found; | |
2541 | } else { | |
2542 | list_for_each_entry(efiobj, &efi_obj_list, link) { | |
2543 | ret = efi_search_protocol(efiobj, protocol, &handler); | |
2544 | if (ret == EFI_SUCCESS) | |
2545 | goto found; | |
bee91169 AG |
2546 | } |
2547 | } | |
e31b3b16 | 2548 | not_found: |
88adae5e | 2549 | *protocol_interface = NULL; |
bee91169 | 2550 | return EFI_EXIT(EFI_NOT_FOUND); |
e31b3b16 HS |
2551 | found: |
2552 | *protocol_interface = handler->protocol_interface; | |
2553 | return EFI_EXIT(EFI_SUCCESS); | |
bee91169 AG |
2554 | } |
2555 | ||
6b03cd10 | 2556 | /** |
78a88f79 MS |
2557 | * efi_install_multiple_protocol_interfaces() - Install multiple protocol |
2558 | * interfaces | |
2559 | * @handle: handle on which the protocol interfaces shall be installed | |
2560 | * @...: NULL terminated argument list with pairs of protocol GUIDS and | |
2561 | * interfaces | |
332468f7 HS |
2562 | * |
2563 | * This function implements the MultipleProtocolInterfaces service. | |
332468f7 | 2564 | * |
78a88f79 MS |
2565 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2566 | * details. | |
2567 | * | |
2568 | * Return: status code | |
332468f7 | 2569 | */ |
7657ae12 | 2570 | efi_status_t EFIAPI efi_install_multiple_protocol_interfaces |
faea1041 | 2571 | (efi_handle_t *handle, ...) |
bee91169 AG |
2572 | { |
2573 | EFI_ENTRY("%p", handle); | |
58b83586 | 2574 | |
beb077a2 | 2575 | efi_va_list argptr; |
5a9682d0 | 2576 | const efi_guid_t *protocol; |
58b83586 | 2577 | void *protocol_interface; |
226cddbe | 2578 | efi_handle_t old_handle; |
58b83586 HS |
2579 | efi_status_t r = EFI_SUCCESS; |
2580 | int i = 0; | |
2581 | ||
2582 | if (!handle) | |
2583 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2584 | ||
beb077a2 | 2585 | efi_va_start(argptr, handle); |
58b83586 | 2586 | for (;;) { |
beb077a2 | 2587 | protocol = efi_va_arg(argptr, efi_guid_t*); |
58b83586 HS |
2588 | if (!protocol) |
2589 | break; | |
beb077a2 | 2590 | protocol_interface = efi_va_arg(argptr, void*); |
226cddbe HS |
2591 | /* Check that a device path has not been installed before */ |
2592 | if (!guidcmp(protocol, &efi_guid_device_path)) { | |
2593 | struct efi_device_path *dp = protocol_interface; | |
2594 | ||
2595 | r = EFI_CALL(efi_locate_device_path(protocol, &dp, | |
2596 | &old_handle)); | |
20562893 HS |
2597 | if (r == EFI_SUCCESS && |
2598 | dp->type == DEVICE_PATH_TYPE_END) { | |
2599 | EFI_PRINT("Path %pD already installed\n", | |
2600 | protocol_interface); | |
226cddbe HS |
2601 | r = EFI_ALREADY_STARTED; |
2602 | break; | |
2603 | } | |
2604 | } | |
1760ef57 HS |
2605 | r = EFI_CALL(efi_install_protocol_interface( |
2606 | handle, protocol, | |
2607 | EFI_NATIVE_INTERFACE, | |
2608 | protocol_interface)); | |
58b83586 HS |
2609 | if (r != EFI_SUCCESS) |
2610 | break; | |
2611 | i++; | |
2612 | } | |
beb077a2 | 2613 | efi_va_end(argptr); |
58b83586 HS |
2614 | if (r == EFI_SUCCESS) |
2615 | return EFI_EXIT(r); | |
2616 | ||
62471e46 | 2617 | /* If an error occurred undo all changes. */ |
beb077a2 | 2618 | efi_va_start(argptr, handle); |
58b83586 | 2619 | for (; i; --i) { |
beb077a2 AG |
2620 | protocol = efi_va_arg(argptr, efi_guid_t*); |
2621 | protocol_interface = efi_va_arg(argptr, void*); | |
faea1041 | 2622 | EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol, |
cd534083 | 2623 | protocol_interface)); |
58b83586 | 2624 | } |
beb077a2 | 2625 | efi_va_end(argptr); |
58b83586 HS |
2626 | |
2627 | return EFI_EXIT(r); | |
bee91169 AG |
2628 | } |
2629 | ||
6b03cd10 | 2630 | /** |
78a88f79 MS |
2631 | * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol |
2632 | * interfaces | |
2633 | * @handle: handle from which the protocol interfaces shall be removed | |
2634 | * @...: NULL terminated argument list with pairs of protocol GUIDS and | |
2635 | * interfaces | |
332468f7 HS |
2636 | * |
2637 | * This function implements the UninstallMultipleProtocolInterfaces service. | |
332468f7 | 2638 | * |
78a88f79 MS |
2639 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2640 | * details. | |
2641 | * | |
2642 | * Return: status code | |
332468f7 | 2643 | */ |
bee91169 | 2644 | static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces( |
faea1041 | 2645 | efi_handle_t handle, ...) |
bee91169 AG |
2646 | { |
2647 | EFI_ENTRY("%p", handle); | |
843ce54c | 2648 | |
beb077a2 | 2649 | efi_va_list argptr; |
843ce54c HS |
2650 | const efi_guid_t *protocol; |
2651 | void *protocol_interface; | |
2652 | efi_status_t r = EFI_SUCCESS; | |
2653 | size_t i = 0; | |
2654 | ||
2655 | if (!handle) | |
2656 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2657 | ||
beb077a2 | 2658 | efi_va_start(argptr, handle); |
843ce54c | 2659 | for (;;) { |
beb077a2 | 2660 | protocol = efi_va_arg(argptr, efi_guid_t*); |
843ce54c HS |
2661 | if (!protocol) |
2662 | break; | |
beb077a2 | 2663 | protocol_interface = efi_va_arg(argptr, void*); |
9b47f13b HS |
2664 | r = efi_uninstall_protocol(handle, protocol, |
2665 | protocol_interface); | |
843ce54c HS |
2666 | if (r != EFI_SUCCESS) |
2667 | break; | |
2668 | i++; | |
2669 | } | |
beb077a2 | 2670 | efi_va_end(argptr); |
9b47f13b HS |
2671 | if (r == EFI_SUCCESS) { |
2672 | /* If the last protocol has been removed, delete the handle. */ | |
2673 | if (list_empty(&handle->protocols)) { | |
2674 | list_del(&handle->link); | |
2675 | free(handle); | |
2676 | } | |
843ce54c | 2677 | return EFI_EXIT(r); |
9b47f13b | 2678 | } |
843ce54c HS |
2679 | |
2680 | /* If an error occurred undo all changes. */ | |
beb077a2 | 2681 | efi_va_start(argptr, handle); |
843ce54c | 2682 | for (; i; --i) { |
beb077a2 AG |
2683 | protocol = efi_va_arg(argptr, efi_guid_t*); |
2684 | protocol_interface = efi_va_arg(argptr, void*); | |
843ce54c HS |
2685 | EFI_CALL(efi_install_protocol_interface(&handle, protocol, |
2686 | EFI_NATIVE_INTERFACE, | |
2687 | protocol_interface)); | |
2688 | } | |
beb077a2 | 2689 | efi_va_end(argptr); |
843ce54c | 2690 | |
e2373021 HS |
2691 | /* In case of an error always return EFI_INVALID_PARAMETER */ |
2692 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
bee91169 AG |
2693 | } |
2694 | ||
6b03cd10 | 2695 | /** |
78a88f79 MS |
2696 | * efi_calculate_crc32() - calculate cyclic redundancy code |
2697 | * @data: buffer with data | |
2698 | * @data_size: size of buffer in bytes | |
2699 | * @crc32_p: cyclic redundancy code | |
332468f7 HS |
2700 | * |
2701 | * This function implements the CalculateCrc32 service. | |
332468f7 | 2702 | * |
78a88f79 MS |
2703 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2704 | * details. | |
2705 | * | |
2706 | * Return: status code | |
332468f7 | 2707 | */ |
8aa8360e HS |
2708 | static efi_status_t EFIAPI efi_calculate_crc32(const void *data, |
2709 | efi_uintn_t data_size, | |
2710 | u32 *crc32_p) | |
bee91169 | 2711 | { |
db80fe38 HS |
2712 | efi_status_t ret = EFI_SUCCESS; |
2713 | ||
8aa8360e | 2714 | EFI_ENTRY("%p, %zu", data, data_size); |
db80fe38 HS |
2715 | if (!data || !data_size || !crc32_p) { |
2716 | ret = EFI_INVALID_PARAMETER; | |
2717 | goto out; | |
2718 | } | |
bee91169 | 2719 | *crc32_p = crc32(0, data, data_size); |
db80fe38 HS |
2720 | out: |
2721 | return EFI_EXIT(ret); | |
bee91169 AG |
2722 | } |
2723 | ||
6b03cd10 | 2724 | /** |
78a88f79 MS |
2725 | * efi_copy_mem() - copy memory |
2726 | * @destination: destination of the copy operation | |
2727 | * @source: source of the copy operation | |
2728 | * @length: number of bytes to copy | |
332468f7 HS |
2729 | * |
2730 | * This function implements the CopyMem service. | |
332468f7 | 2731 | * |
78a88f79 MS |
2732 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2733 | * details. | |
332468f7 | 2734 | */ |
fc05a959 HS |
2735 | static void EFIAPI efi_copy_mem(void *destination, const void *source, |
2736 | size_t length) | |
bee91169 | 2737 | { |
fc05a959 | 2738 | EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length); |
0bc81a71 | 2739 | memmove(destination, source, length); |
f7c78176 | 2740 | EFI_EXIT(EFI_SUCCESS); |
bee91169 AG |
2741 | } |
2742 | ||
6b03cd10 | 2743 | /** |
78a88f79 MS |
2744 | * efi_set_mem() - Fill memory with a byte value. |
2745 | * @buffer: buffer to fill | |
2746 | * @size: size of buffer in bytes | |
2747 | * @value: byte to copy to the buffer | |
332468f7 HS |
2748 | * |
2749 | * This function implements the SetMem service. | |
332468f7 | 2750 | * |
78a88f79 MS |
2751 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2752 | * details. | |
332468f7 | 2753 | */ |
fc05a959 | 2754 | static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value) |
bee91169 | 2755 | { |
fc05a959 | 2756 | EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value); |
bee91169 | 2757 | memset(buffer, value, size); |
f7c78176 | 2758 | EFI_EXIT(EFI_SUCCESS); |
bee91169 AG |
2759 | } |
2760 | ||
6b03cd10 | 2761 | /** |
78a88f79 MS |
2762 | * efi_protocol_open() - open protocol interface on a handle |
2763 | * @handler: handler of a protocol | |
2764 | * @protocol_interface: interface implementing the protocol | |
2765 | * @agent_handle: handle of the driver | |
2766 | * @controller_handle: handle of the controller | |
2767 | * @attributes: attributes indicating how to open the protocol | |
191a41cc | 2768 | * |
78a88f79 | 2769 | * Return: status code |
191a41cc | 2770 | */ |
f9ad240e | 2771 | efi_status_t efi_protocol_open( |
191a41cc HS |
2772 | struct efi_handler *handler, |
2773 | void **protocol_interface, void *agent_handle, | |
2774 | void *controller_handle, uint32_t attributes) | |
2775 | { | |
2776 | struct efi_open_protocol_info_item *item; | |
2777 | struct efi_open_protocol_info_entry *match = NULL; | |
2778 | bool opened_by_driver = false; | |
2779 | bool opened_exclusive = false; | |
2780 | ||
2781 | /* If there is no agent, only return the interface */ | |
2782 | if (!agent_handle) | |
2783 | goto out; | |
2784 | ||
2785 | /* For TEST_PROTOCOL ignore interface attribute */ | |
2786 | if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL) | |
2787 | *protocol_interface = NULL; | |
2788 | ||
2789 | /* | |
2790 | * Check if the protocol is already opened by a driver with the same | |
2791 | * attributes or opened exclusively | |
2792 | */ | |
2793 | list_for_each_entry(item, &handler->open_infos, link) { | |
2794 | if (item->info.agent_handle == agent_handle) { | |
2795 | if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) && | |
2796 | (item->info.attributes == attributes)) | |
2797 | return EFI_ALREADY_STARTED; | |
399a39e3 HS |
2798 | } else { |
2799 | if (item->info.attributes & | |
2800 | EFI_OPEN_PROTOCOL_BY_DRIVER) | |
2801 | opened_by_driver = true; | |
191a41cc HS |
2802 | } |
2803 | if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) | |
2804 | opened_exclusive = true; | |
2805 | } | |
2806 | ||
2807 | /* Only one controller can open the protocol exclusively */ | |
399a39e3 HS |
2808 | if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) { |
2809 | if (opened_exclusive) | |
2810 | return EFI_ACCESS_DENIED; | |
2811 | } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) { | |
2812 | if (opened_exclusive || opened_by_driver) | |
2813 | return EFI_ACCESS_DENIED; | |
2814 | } | |
191a41cc HS |
2815 | |
2816 | /* Prepare exclusive opening */ | |
2817 | if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) { | |
2818 | /* Try to disconnect controllers */ | |
dae7ce45 HS |
2819 | disconnect_next: |
2820 | opened_by_driver = false; | |
191a41cc | 2821 | list_for_each_entry(item, &handler->open_infos, link) { |
dae7ce45 HS |
2822 | efi_status_t ret; |
2823 | ||
191a41cc | 2824 | if (item->info.attributes == |
dae7ce45 HS |
2825 | EFI_OPEN_PROTOCOL_BY_DRIVER) { |
2826 | ret = EFI_CALL(efi_disconnect_controller( | |
191a41cc HS |
2827 | item->info.controller_handle, |
2828 | item->info.agent_handle, | |
2829 | NULL)); | |
dae7ce45 HS |
2830 | if (ret == EFI_SUCCESS) |
2831 | /* | |
2832 | * Child controllers may have been | |
2833 | * removed from the open_infos list. So | |
2834 | * let's restart the loop. | |
2835 | */ | |
2836 | goto disconnect_next; | |
2837 | else | |
2838 | opened_by_driver = true; | |
2839 | } | |
191a41cc | 2840 | } |
dae7ce45 | 2841 | /* Only one driver can be connected */ |
191a41cc HS |
2842 | if (opened_by_driver) |
2843 | return EFI_ACCESS_DENIED; | |
2844 | } | |
2845 | ||
2846 | /* Find existing entry */ | |
2847 | list_for_each_entry(item, &handler->open_infos, link) { | |
2848 | if (item->info.agent_handle == agent_handle && | |
b4863baa HS |
2849 | item->info.controller_handle == controller_handle && |
2850 | item->info.attributes == attributes) | |
191a41cc HS |
2851 | match = &item->info; |
2852 | } | |
2853 | /* None found, create one */ | |
2854 | if (!match) { | |
2855 | match = efi_create_open_info(handler); | |
2856 | if (!match) | |
2857 | return EFI_OUT_OF_RESOURCES; | |
2858 | } | |
2859 | ||
2860 | match->agent_handle = agent_handle; | |
2861 | match->controller_handle = controller_handle; | |
2862 | match->attributes = attributes; | |
2863 | match->open_count++; | |
2864 | ||
2865 | out: | |
2866 | /* For TEST_PROTOCOL ignore interface attribute. */ | |
2867 | if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL) | |
2868 | *protocol_interface = handler->protocol_interface; | |
2869 | ||
2870 | return EFI_SUCCESS; | |
2871 | } | |
2872 | ||
6b03cd10 | 2873 | /** |
78a88f79 MS |
2874 | * efi_open_protocol() - open protocol interface on a handle |
2875 | * @handle: handle on which the protocol shall be opened | |
2876 | * @protocol: GUID of the protocol | |
2877 | * @protocol_interface: interface implementing the protocol | |
2878 | * @agent_handle: handle of the driver | |
2879 | * @controller_handle: handle of the controller | |
2880 | * @attributes: attributes indicating how to open the protocol | |
332468f7 HS |
2881 | * |
2882 | * This function implements the OpenProtocol interface. | |
332468f7 | 2883 | * |
78a88f79 MS |
2884 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2885 | * details. | |
2886 | * | |
2887 | * Return: status code | |
332468f7 | 2888 | */ |
faea1041 HS |
2889 | static efi_status_t EFIAPI efi_open_protocol |
2890 | (efi_handle_t handle, const efi_guid_t *protocol, | |
2891 | void **protocol_interface, efi_handle_t agent_handle, | |
2892 | efi_handle_t controller_handle, uint32_t attributes) | |
bee91169 | 2893 | { |
80286e8f | 2894 | struct efi_handler *handler; |
69baec67 | 2895 | efi_status_t r = EFI_INVALID_PARAMETER; |
bee91169 | 2896 | |
778e6af8 | 2897 | EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol, |
bee91169 AG |
2898 | protocol_interface, agent_handle, controller_handle, |
2899 | attributes); | |
b5349f74 | 2900 | |
69baec67 HS |
2901 | if (!handle || !protocol || |
2902 | (!protocol_interface && attributes != | |
2903 | EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) { | |
2904 | goto out; | |
2905 | } | |
2906 | ||
2907 | switch (attributes) { | |
2908 | case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL: | |
2909 | case EFI_OPEN_PROTOCOL_GET_PROTOCOL: | |
2910 | case EFI_OPEN_PROTOCOL_TEST_PROTOCOL: | |
2911 | break; | |
2912 | case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER: | |
2913 | if (controller_handle == handle) | |
2914 | goto out; | |
191a41cc | 2915 | /* fall-through */ |
69baec67 HS |
2916 | case EFI_OPEN_PROTOCOL_BY_DRIVER: |
2917 | case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE: | |
191a41cc HS |
2918 | /* Check that the controller handle is valid */ |
2919 | if (!efi_search_obj(controller_handle)) | |
69baec67 | 2920 | goto out; |
191a41cc | 2921 | /* fall-through */ |
69baec67 | 2922 | case EFI_OPEN_PROTOCOL_EXCLUSIVE: |
191a41cc HS |
2923 | /* Check that the agent handle is valid */ |
2924 | if (!efi_search_obj(agent_handle)) | |
69baec67 HS |
2925 | goto out; |
2926 | break; | |
2927 | default: | |
b5349f74 HS |
2928 | goto out; |
2929 | } | |
2930 | ||
80286e8f | 2931 | r = efi_search_protocol(handle, protocol, &handler); |
e7c3cd6b HS |
2932 | switch (r) { |
2933 | case EFI_SUCCESS: | |
2934 | break; | |
2935 | case EFI_NOT_FOUND: | |
2936 | r = EFI_UNSUPPORTED; | |
80286e8f | 2937 | goto out; |
e7c3cd6b HS |
2938 | default: |
2939 | goto out; | |
2940 | } | |
bee91169 | 2941 | |
191a41cc HS |
2942 | r = efi_protocol_open(handler, protocol_interface, agent_handle, |
2943 | controller_handle, attributes); | |
bee91169 AG |
2944 | out: |
2945 | return EFI_EXIT(r); | |
2946 | } | |
2947 | ||
a115d565 HS |
2948 | /** |
2949 | * efi_start_image() - call the entry point of an image | |
2950 | * @image_handle: handle of the image | |
2951 | * @exit_data_size: size of the buffer | |
2952 | * @exit_data: buffer to receive the exit data of the called image | |
2953 | * | |
2954 | * This function implements the StartImage service. | |
2955 | * | |
2956 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
2957 | * details. | |
2958 | * | |
2959 | * Return: status code | |
2960 | */ | |
2961 | efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle, | |
2962 | efi_uintn_t *exit_data_size, | |
2963 | u16 **exit_data) | |
2964 | { | |
2965 | struct efi_loaded_image_obj *image_obj = | |
2966 | (struct efi_loaded_image_obj *)image_handle; | |
2967 | efi_status_t ret; | |
bb31c3f7 HS |
2968 | void *info; |
2969 | efi_handle_t parent_image = current_image; | |
f8212f09 HS |
2970 | efi_status_t exit_status; |
2971 | struct jmp_buf_data exit_jmp; | |
a115d565 HS |
2972 | |
2973 | EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data); | |
2974 | ||
4540dabd AT |
2975 | if (!efi_search_obj(image_handle)) |
2976 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2977 | ||
bb31c3f7 | 2978 | /* Check parameters */ |
1d3e8dc7 HS |
2979 | if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE) |
2980 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2981 | ||
4540dabd AT |
2982 | if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED) |
2983 | return EFI_EXIT(EFI_SECURITY_VIOLATION); | |
2984 | ||
bb31c3f7 HS |
2985 | ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image, |
2986 | &info, NULL, NULL, | |
2987 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); | |
2988 | if (ret != EFI_SUCCESS) | |
2989 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2990 | ||
556d8dc9 HS |
2991 | image_obj->exit_data_size = exit_data_size; |
2992 | image_obj->exit_data = exit_data; | |
f8212f09 HS |
2993 | image_obj->exit_status = &exit_status; |
2994 | image_obj->exit_jmp = &exit_jmp; | |
556d8dc9 | 2995 | |
a115d565 | 2996 | /* call the image! */ |
f8212f09 | 2997 | if (setjmp(&exit_jmp)) { |
a115d565 HS |
2998 | /* |
2999 | * We called the entry point of the child image with EFI_CALL | |
3000 | * in the lines below. The child image called the Exit() boot | |
3001 | * service efi_exit() which executed the long jump that brought | |
3002 | * us to the current line. This implies that the second half | |
3003 | * of the EFI_CALL macro has not been executed. | |
3004 | */ | |
d68d7f47 | 3005 | #if defined(CONFIG_ARM) || defined(CONFIG_RISCV) |
a115d565 HS |
3006 | /* |
3007 | * efi_exit() called efi_restore_gd(). We have to undo this | |
3008 | * otherwise __efi_entry_check() will put the wrong value into | |
3009 | * app_gd. | |
3010 | */ | |
4f7dc5f6 | 3011 | set_gd(app_gd); |
a115d565 HS |
3012 | #endif |
3013 | /* | |
3014 | * To get ready to call EFI_EXIT below we have to execute the | |
3015 | * missed out steps of EFI_CALL. | |
3016 | */ | |
3017 | assert(__efi_entry_check()); | |
529886a0 | 3018 | EFI_PRINT("%lu returned by started image\n", |
f8212f09 | 3019 | (unsigned long)((uintptr_t)exit_status & |
529886a0 | 3020 | ~EFI_ERROR_MASK)); |
bb31c3f7 | 3021 | current_image = parent_image; |
f8212f09 | 3022 | return EFI_EXIT(exit_status); |
a115d565 HS |
3023 | } |
3024 | ||
bb31c3f7 | 3025 | current_image = image_handle; |
cd73aba6 | 3026 | image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE; |
6b95b38c | 3027 | EFI_PRINT("Jumping into 0x%p\n", image_obj->entry); |
a115d565 HS |
3028 | ret = EFI_CALL(image_obj->entry(image_handle, &systab)); |
3029 | ||
3030 | /* | |
55111c50 HS |
3031 | * Control is returned from a started UEFI image either by calling |
3032 | * Exit() (where exit data can be provided) or by simply returning from | |
3033 | * the entry point. In the latter case call Exit() on behalf of the | |
3034 | * image. | |
a115d565 HS |
3035 | */ |
3036 | return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL)); | |
3037 | } | |
3038 | ||
df116e81 HS |
3039 | /** |
3040 | * efi_delete_image() - delete loaded image from memory) | |
3041 | * | |
3042 | * @image_obj: handle of the loaded image | |
3043 | * @loaded_image_protocol: loaded image protocol | |
3044 | */ | |
120ff7ba HS |
3045 | static efi_status_t efi_delete_image |
3046 | (struct efi_loaded_image_obj *image_obj, | |
3047 | struct efi_loaded_image *loaded_image_protocol) | |
df116e81 | 3048 | { |
120ff7ba HS |
3049 | struct efi_object *efiobj; |
3050 | efi_status_t r, ret = EFI_SUCCESS; | |
3051 | ||
3052 | close_next: | |
3053 | list_for_each_entry(efiobj, &efi_obj_list, link) { | |
3054 | struct efi_handler *protocol; | |
3055 | ||
3056 | list_for_each_entry(protocol, &efiobj->protocols, link) { | |
3057 | struct efi_open_protocol_info_item *info; | |
3058 | ||
3059 | list_for_each_entry(info, &protocol->open_infos, link) { | |
3060 | if (info->info.agent_handle != | |
3061 | (efi_handle_t)image_obj) | |
3062 | continue; | |
3063 | r = EFI_CALL(efi_close_protocol | |
3064 | (efiobj, protocol->guid, | |
3065 | info->info.agent_handle, | |
3066 | info->info.controller_handle | |
3067 | )); | |
3068 | if (r != EFI_SUCCESS) | |
3069 | ret = r; | |
3070 | /* | |
3071 | * Closing protocols may results in further | |
3072 | * items being deleted. To play it safe loop | |
3073 | * over all elements again. | |
3074 | */ | |
3075 | goto close_next; | |
3076 | } | |
3077 | } | |
3078 | } | |
3079 | ||
df116e81 HS |
3080 | efi_free_pages((uintptr_t)loaded_image_protocol->image_base, |
3081 | efi_size_in_pages(loaded_image_protocol->image_size)); | |
3082 | efi_delete_handle(&image_obj->header); | |
120ff7ba HS |
3083 | |
3084 | return ret; | |
df116e81 HS |
3085 | } |
3086 | ||
46e99a9c HS |
3087 | /** |
3088 | * efi_unload_image() - unload an EFI image | |
3089 | * @image_handle: handle of the image to be unloaded | |
3090 | * | |
3091 | * This function implements the UnloadImage service. | |
3092 | * | |
3093 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
3094 | * details. | |
3095 | * | |
3096 | * Return: status code | |
3097 | */ | |
3098 | efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle) | |
3099 | { | |
df116e81 | 3100 | efi_status_t ret = EFI_SUCCESS; |
46e99a9c | 3101 | struct efi_object *efiobj; |
df116e81 | 3102 | struct efi_loaded_image *loaded_image_protocol; |
46e99a9c HS |
3103 | |
3104 | EFI_ENTRY("%p", image_handle); | |
46e99a9c | 3105 | |
df116e81 HS |
3106 | efiobj = efi_search_obj(image_handle); |
3107 | if (!efiobj) { | |
3108 | ret = EFI_INVALID_PARAMETER; | |
3109 | goto out; | |
3110 | } | |
3111 | /* Find the loaded image protocol */ | |
3112 | ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image, | |
3113 | (void **)&loaded_image_protocol, | |
3114 | NULL, NULL, | |
3115 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); | |
3116 | if (ret != EFI_SUCCESS) { | |
3117 | ret = EFI_INVALID_PARAMETER; | |
3118 | goto out; | |
3119 | } | |
3120 | switch (efiobj->type) { | |
3121 | case EFI_OBJECT_TYPE_STARTED_IMAGE: | |
3122 | /* Call the unload function */ | |
3123 | if (!loaded_image_protocol->unload) { | |
3124 | ret = EFI_UNSUPPORTED; | |
3125 | goto out; | |
3126 | } | |
3127 | ret = EFI_CALL(loaded_image_protocol->unload(image_handle)); | |
3128 | if (ret != EFI_SUCCESS) | |
3129 | goto out; | |
3130 | break; | |
3131 | case EFI_OBJECT_TYPE_LOADED_IMAGE: | |
3132 | break; | |
3133 | default: | |
3134 | ret = EFI_INVALID_PARAMETER; | |
3135 | goto out; | |
3136 | } | |
3137 | efi_delete_image((struct efi_loaded_image_obj *)efiobj, | |
3138 | loaded_image_protocol); | |
3139 | out: | |
3140 | return EFI_EXIT(ret); | |
46e99a9c HS |
3141 | } |
3142 | ||
556d8dc9 HS |
3143 | /** |
3144 | * efi_update_exit_data() - fill exit data parameters of StartImage() | |
3145 | * | |
9ce91275 HS |
3146 | * @image_obj: image handle |
3147 | * @exit_data_size: size of the exit data buffer | |
3148 | * @exit_data: buffer with data returned by UEFI payload | |
556d8dc9 HS |
3149 | * Return: status code |
3150 | */ | |
3151 | static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj, | |
3152 | efi_uintn_t exit_data_size, | |
3153 | u16 *exit_data) | |
3154 | { | |
3155 | efi_status_t ret; | |
3156 | ||
3157 | /* | |
3158 | * If exit_data is not provided to StartImage(), exit_data_size must be | |
3159 | * ignored. | |
3160 | */ | |
3161 | if (!image_obj->exit_data) | |
3162 | return EFI_SUCCESS; | |
3163 | if (image_obj->exit_data_size) | |
3164 | *image_obj->exit_data_size = exit_data_size; | |
3165 | if (exit_data_size && exit_data) { | |
3166 | ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, | |
3167 | exit_data_size, | |
3168 | (void **)image_obj->exit_data); | |
3169 | if (ret != EFI_SUCCESS) | |
3170 | return ret; | |
3171 | memcpy(*image_obj->exit_data, exit_data, exit_data_size); | |
3172 | } else { | |
3173 | image_obj->exit_data = NULL; | |
3174 | } | |
3175 | return EFI_SUCCESS; | |
3176 | } | |
3177 | ||
a115d565 HS |
3178 | /** |
3179 | * efi_exit() - leave an EFI application or driver | |
3180 | * @image_handle: handle of the application or driver that is exiting | |
3181 | * @exit_status: status code | |
3182 | * @exit_data_size: size of the buffer in bytes | |
3183 | * @exit_data: buffer with data describing an error | |
3184 | * | |
3185 | * This function implements the Exit service. | |
3186 | * | |
3187 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
3188 | * details. | |
3189 | * | |
3190 | * Return: status code | |
3191 | */ | |
3192 | static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle, | |
3193 | efi_status_t exit_status, | |
3194 | efi_uintn_t exit_data_size, | |
3195 | u16 *exit_data) | |
3196 | { | |
3197 | /* | |
3198 | * TODO: We should call the unload procedure of the loaded | |
3199 | * image protocol. | |
3200 | */ | |
bb31c3f7 | 3201 | efi_status_t ret; |
126a43f1 | 3202 | struct efi_loaded_image *loaded_image_protocol; |
a115d565 HS |
3203 | struct efi_loaded_image_obj *image_obj = |
3204 | (struct efi_loaded_image_obj *)image_handle; | |
f8212f09 | 3205 | struct jmp_buf_data *exit_jmp; |
a115d565 HS |
3206 | |
3207 | EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status, | |
3208 | exit_data_size, exit_data); | |
3209 | ||
bb31c3f7 | 3210 | /* Check parameters */ |
bb31c3f7 | 3211 | ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image, |
126a43f1 HS |
3212 | (void **)&loaded_image_protocol, |
3213 | NULL, NULL, | |
bb31c3f7 | 3214 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
126a43f1 HS |
3215 | if (ret != EFI_SUCCESS) { |
3216 | ret = EFI_INVALID_PARAMETER; | |
bb31c3f7 | 3217 | goto out; |
126a43f1 HS |
3218 | } |
3219 | ||
3220 | /* Unloading of unstarted images */ | |
3221 | switch (image_obj->header.type) { | |
3222 | case EFI_OBJECT_TYPE_STARTED_IMAGE: | |
3223 | break; | |
3224 | case EFI_OBJECT_TYPE_LOADED_IMAGE: | |
3225 | efi_delete_image(image_obj, loaded_image_protocol); | |
3226 | ret = EFI_SUCCESS; | |
3227 | goto out; | |
3228 | default: | |
3229 | /* Handle does not refer to loaded image */ | |
3230 | ret = EFI_INVALID_PARAMETER; | |
3231 | goto out; | |
3232 | } | |
3233 | /* A started image can only be unloaded it is the last one started. */ | |
3234 | if (image_handle != current_image) { | |
3235 | ret = EFI_INVALID_PARAMETER; | |
3236 | goto out; | |
3237 | } | |
bb31c3f7 | 3238 | |
556d8dc9 HS |
3239 | /* Exit data is only foreseen in case of failure. */ |
3240 | if (exit_status != EFI_SUCCESS) { | |
3241 | ret = efi_update_exit_data(image_obj, exit_data_size, | |
3242 | exit_data); | |
3243 | /* Exiting has priority. Don't return error to caller. */ | |
3244 | if (ret != EFI_SUCCESS) | |
3245 | EFI_PRINT("%s: out of memory\n", __func__); | |
3246 | } | |
f8212f09 HS |
3247 | /* efi_delete_image() frees image_obj. Copy before the call. */ |
3248 | exit_jmp = image_obj->exit_jmp; | |
3249 | *image_obj->exit_status = exit_status; | |
126a43f1 HS |
3250 | if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION || |
3251 | exit_status != EFI_SUCCESS) | |
3252 | efi_delete_image(image_obj, loaded_image_protocol); | |
556d8dc9 | 3253 | |
a115d565 HS |
3254 | /* Make sure entry/exit counts for EFI world cross-overs match */ |
3255 | EFI_EXIT(exit_status); | |
3256 | ||
3257 | /* | |
3258 | * But longjmp out with the U-Boot gd, not the application's, as | |
3259 | * the other end is a setjmp call inside EFI context. | |
3260 | */ | |
3261 | efi_restore_gd(); | |
3262 | ||
f8212f09 | 3263 | longjmp(exit_jmp, 1); |
a115d565 HS |
3264 | |
3265 | panic("EFI application exited"); | |
bb31c3f7 | 3266 | out: |
126a43f1 | 3267 | return EFI_EXIT(ret); |
a115d565 HS |
3268 | } |
3269 | ||
6b03cd10 | 3270 | /** |
78a88f79 MS |
3271 | * efi_handle_protocol() - get interface of a protocol on a handle |
3272 | * @handle: handle on which the protocol shall be opened | |
3273 | * @protocol: GUID of the protocol | |
3274 | * @protocol_interface: interface implementing the protocol | |
332468f7 HS |
3275 | * |
3276 | * This function implements the HandleProtocol service. | |
332468f7 | 3277 | * |
78a88f79 MS |
3278 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
3279 | * details. | |
3280 | * | |
3281 | * Return: status code | |
332468f7 | 3282 | */ |
b51ec639 AT |
3283 | efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle, |
3284 | const efi_guid_t *protocol, | |
3285 | void **protocol_interface) | |
bee91169 | 3286 | { |
755d42d4 | 3287 | return efi_open_protocol(handle, protocol, protocol_interface, efi_root, |
8e1d329f | 3288 | NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL); |
bee91169 AG |
3289 | } |
3290 | ||
6b03cd10 | 3291 | /** |
78a88f79 MS |
3292 | * efi_bind_controller() - bind a single driver to a controller |
3293 | * @controller_handle: controller handle | |
3294 | * @driver_image_handle: driver handle | |
3295 | * @remain_device_path: remaining path | |
6b03cd10 | 3296 | * |
78a88f79 | 3297 | * Return: status code |
6b03cd10 | 3298 | */ |
f0959dbe HS |
3299 | static efi_status_t efi_bind_controller( |
3300 | efi_handle_t controller_handle, | |
3301 | efi_handle_t driver_image_handle, | |
3302 | struct efi_device_path *remain_device_path) | |
3303 | { | |
3304 | struct efi_driver_binding_protocol *binding_protocol; | |
3305 | efi_status_t r; | |
3306 | ||
3307 | r = EFI_CALL(efi_open_protocol(driver_image_handle, | |
3308 | &efi_guid_driver_binding_protocol, | |
3309 | (void **)&binding_protocol, | |
3310 | driver_image_handle, NULL, | |
3311 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); | |
3312 | if (r != EFI_SUCCESS) | |
3313 | return r; | |
3314 | r = EFI_CALL(binding_protocol->supported(binding_protocol, | |
3315 | controller_handle, | |
3316 | remain_device_path)); | |
3317 | if (r == EFI_SUCCESS) | |
3318 | r = EFI_CALL(binding_protocol->start(binding_protocol, | |
3319 | controller_handle, | |
3320 | remain_device_path)); | |
3321 | EFI_CALL(efi_close_protocol(driver_image_handle, | |
3322 | &efi_guid_driver_binding_protocol, | |
3323 | driver_image_handle, NULL)); | |
3324 | return r; | |
3325 | } | |
3326 | ||
6b03cd10 | 3327 | /** |
78a88f79 MS |
3328 | * efi_connect_single_controller() - connect a single driver to a controller |
3329 | * @controller_handle: controller | |
3330 | * @driver_image_handle: driver | |
b72aaa87 | 3331 | * @remain_device_path: remaining path |
6b03cd10 | 3332 | * |
78a88f79 | 3333 | * Return: status code |
6b03cd10 | 3334 | */ |
f0959dbe HS |
3335 | static efi_status_t efi_connect_single_controller( |
3336 | efi_handle_t controller_handle, | |
3337 | efi_handle_t *driver_image_handle, | |
3338 | struct efi_device_path *remain_device_path) | |
3339 | { | |
3340 | efi_handle_t *buffer; | |
3341 | size_t count; | |
3342 | size_t i; | |
3343 | efi_status_t r; | |
3344 | size_t connected = 0; | |
3345 | ||
3346 | /* Get buffer with all handles with driver binding protocol */ | |
3347 | r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, | |
3348 | &efi_guid_driver_binding_protocol, | |
3349 | NULL, &count, &buffer)); | |
3350 | if (r != EFI_SUCCESS) | |
3351 | return r; | |
3352 | ||
359a699a | 3353 | /* Context Override */ |
f0959dbe HS |
3354 | if (driver_image_handle) { |
3355 | for (; *driver_image_handle; ++driver_image_handle) { | |
3356 | for (i = 0; i < count; ++i) { | |
3357 | if (buffer[i] == *driver_image_handle) { | |
3358 | buffer[i] = NULL; | |
3359 | r = efi_bind_controller( | |
3360 | controller_handle, | |
3361 | *driver_image_handle, | |
3362 | remain_device_path); | |
3363 | /* | |
3364 | * For drivers that do not support the | |
3365 | * controller or are already connected | |
3366 | * we receive an error code here. | |
3367 | */ | |
3368 | if (r == EFI_SUCCESS) | |
3369 | ++connected; | |
3370 | } | |
3371 | } | |
3372 | } | |
3373 | } | |
3374 | ||
3375 | /* | |
3376 | * TODO: Some overrides are not yet implemented: | |
3377 | * - Platform Driver Override | |
3378 | * - Driver Family Override Search | |
3379 | * - Bus Specific Driver Override | |
3380 | */ | |
3381 | ||
3382 | /* Driver Binding Search */ | |
3383 | for (i = 0; i < count; ++i) { | |
3384 | if (buffer[i]) { | |
3385 | r = efi_bind_controller(controller_handle, | |
3386 | buffer[i], | |
3387 | remain_device_path); | |
3388 | if (r == EFI_SUCCESS) | |
3389 | ++connected; | |
3390 | } | |
3391 | } | |
3392 | ||
3393 | efi_free_pool(buffer); | |
3394 | if (!connected) | |
3395 | return EFI_NOT_FOUND; | |
3396 | return EFI_SUCCESS; | |
3397 | } | |
3398 | ||
6b03cd10 | 3399 | /** |
78a88f79 MS |
3400 | * efi_connect_controller() - connect a controller to a driver |
3401 | * @controller_handle: handle of the controller | |
3402 | * @driver_image_handle: handle of the driver | |
3403 | * @remain_device_path: device path of a child controller | |
3404 | * @recursive: true to connect all child controllers | |
f0959dbe HS |
3405 | * |
3406 | * This function implements the ConnectController service. | |
78a88f79 MS |
3407 | * |
3408 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
3409 | * details. | |
f0959dbe HS |
3410 | * |
3411 | * First all driver binding protocol handles are tried for binding drivers. | |
b72aaa87 | 3412 | * Afterwards all handles that have opened a protocol of the controller |
f0959dbe HS |
3413 | * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers. |
3414 | * | |
78a88f79 | 3415 | * Return: status code |
f0959dbe HS |
3416 | */ |
3417 | static efi_status_t EFIAPI efi_connect_controller( | |
3418 | efi_handle_t controller_handle, | |
3419 | efi_handle_t *driver_image_handle, | |
3420 | struct efi_device_path *remain_device_path, | |
3421 | bool recursive) | |
3422 | { | |
3423 | efi_status_t r; | |
3424 | efi_status_t ret = EFI_NOT_FOUND; | |
3425 | struct efi_object *efiobj; | |
3426 | ||
d178836b | 3427 | EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle, |
f0959dbe HS |
3428 | remain_device_path, recursive); |
3429 | ||
3430 | efiobj = efi_search_obj(controller_handle); | |
3431 | if (!efiobj) { | |
3432 | ret = EFI_INVALID_PARAMETER; | |
3433 | goto out; | |
3434 | } | |
3435 | ||
3436 | r = efi_connect_single_controller(controller_handle, | |
3437 | driver_image_handle, | |
3438 | remain_device_path); | |
3439 | if (r == EFI_SUCCESS) | |
3440 | ret = EFI_SUCCESS; | |
3441 | if (recursive) { | |
3442 | struct efi_handler *handler; | |
3443 | struct efi_open_protocol_info_item *item; | |
3444 | ||
3445 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
3446 | list_for_each_entry(item, &handler->open_infos, link) { | |
3447 | if (item->info.attributes & | |
3448 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) { | |
3449 | r = EFI_CALL(efi_connect_controller( | |
3450 | item->info.controller_handle, | |
3451 | driver_image_handle, | |
3452 | remain_device_path, | |
3453 | recursive)); | |
3454 | if (r == EFI_SUCCESS) | |
3455 | ret = EFI_SUCCESS; | |
3456 | } | |
3457 | } | |
3458 | } | |
3459 | } | |
359a699a | 3460 | /* Check for child controller specified by end node */ |
f0959dbe HS |
3461 | if (ret != EFI_SUCCESS && remain_device_path && |
3462 | remain_device_path->type == DEVICE_PATH_TYPE_END) | |
3463 | ret = EFI_SUCCESS; | |
3464 | out: | |
3465 | return EFI_EXIT(ret); | |
3466 | } | |
3467 | ||
6b03cd10 | 3468 | /** |
78a88f79 MS |
3469 | * efi_reinstall_protocol_interface() - reinstall protocol interface |
3470 | * @handle: handle on which the protocol shall be reinstalled | |
3471 | * @protocol: GUID of the protocol to be installed | |
3472 | * @old_interface: interface to be removed | |
3473 | * @new_interface: interface to be installed | |
e861a120 HS |
3474 | * |
3475 | * This function implements the ReinstallProtocolInterface service. | |
78a88f79 MS |
3476 | * |
3477 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
3478 | * details. | |
e861a120 HS |
3479 | * |
3480 | * The old interface is uninstalled. The new interface is installed. | |
3481 | * Drivers are connected. | |
3482 | * | |
78a88f79 | 3483 | * Return: status code |
e861a120 HS |
3484 | */ |
3485 | static efi_status_t EFIAPI efi_reinstall_protocol_interface( | |
3486 | efi_handle_t handle, const efi_guid_t *protocol, | |
3487 | void *old_interface, void *new_interface) | |
3488 | { | |
3489 | efi_status_t ret; | |
3490 | ||
3491 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface, | |
3492 | new_interface); | |
9b47f13b HS |
3493 | |
3494 | /* Uninstall protocol but do not delete handle */ | |
3495 | ret = efi_uninstall_protocol(handle, protocol, old_interface); | |
e861a120 HS |
3496 | if (ret != EFI_SUCCESS) |
3497 | goto out; | |
9b47f13b HS |
3498 | |
3499 | /* Install the new protocol */ | |
3500 | ret = efi_add_protocol(handle, protocol, new_interface); | |
3501 | /* | |
3502 | * The UEFI spec does not specify what should happen to the handle | |
3503 | * if in case of an error no protocol interface remains on the handle. | |
3504 | * So let's do nothing here. | |
3505 | */ | |
e861a120 HS |
3506 | if (ret != EFI_SUCCESS) |
3507 | goto out; | |
3508 | /* | |
3509 | * The returned status code has to be ignored. | |
3510 | * Do not create an error if no suitable driver for the handle exists. | |
3511 | */ | |
3512 | EFI_CALL(efi_connect_controller(handle, NULL, NULL, true)); | |
3513 | out: | |
3514 | return EFI_EXIT(ret); | |
3515 | } | |
3516 | ||
6b03cd10 | 3517 | /** |
78a88f79 MS |
3518 | * efi_get_child_controllers() - get all child controllers associated to a driver |
3519 | * @efiobj: handle of the controller | |
3520 | * @driver_handle: handle of the driver | |
3521 | * @number_of_children: number of child controllers | |
3522 | * @child_handle_buffer: handles of the the child controllers | |
6b03cd10 | 3523 | * |
3f9b0042 HS |
3524 | * The allocated buffer has to be freed with free(). |
3525 | * | |
78a88f79 | 3526 | * Return: status code |
3f9b0042 HS |
3527 | */ |
3528 | static efi_status_t efi_get_child_controllers( | |
3529 | struct efi_object *efiobj, | |
3530 | efi_handle_t driver_handle, | |
3531 | efi_uintn_t *number_of_children, | |
3532 | efi_handle_t **child_handle_buffer) | |
3533 | { | |
3534 | struct efi_handler *handler; | |
3535 | struct efi_open_protocol_info_item *item; | |
3536 | efi_uintn_t count = 0, i; | |
3537 | bool duplicate; | |
3538 | ||
3539 | /* Count all child controller associations */ | |
3540 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
3541 | list_for_each_entry(item, &handler->open_infos, link) { | |
3542 | if (item->info.agent_handle == driver_handle && | |
3543 | item->info.attributes & | |
3544 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) | |
3545 | ++count; | |
3546 | } | |
3547 | } | |
3548 | /* | |
3549 | * Create buffer. In case of duplicate child controller assignments | |
3550 | * the buffer will be too large. But that does not harm. | |
3551 | */ | |
3552 | *number_of_children = 0; | |
1047c6e2 HS |
3553 | if (!count) |
3554 | return EFI_SUCCESS; | |
3f9b0042 HS |
3555 | *child_handle_buffer = calloc(count, sizeof(efi_handle_t)); |
3556 | if (!*child_handle_buffer) | |
3557 | return EFI_OUT_OF_RESOURCES; | |
3558 | /* Copy unique child handles */ | |
3559 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
3560 | list_for_each_entry(item, &handler->open_infos, link) { | |
3561 | if (item->info.agent_handle == driver_handle && | |
3562 | item->info.attributes & | |
3563 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) { | |
3564 | /* Check this is a new child controller */ | |
3565 | duplicate = false; | |
3566 | for (i = 0; i < *number_of_children; ++i) { | |
3567 | if ((*child_handle_buffer)[i] == | |
3568 | item->info.controller_handle) | |
3569 | duplicate = true; | |
3570 | } | |
3571 | /* Copy handle to buffer */ | |
3572 | if (!duplicate) { | |
3573 | i = (*number_of_children)++; | |
3574 | (*child_handle_buffer)[i] = | |
3575 | item->info.controller_handle; | |
3576 | } | |
3577 | } | |
3578 | } | |
3579 | } | |
3580 | return EFI_SUCCESS; | |
3581 | } | |
3582 | ||
6b03cd10 | 3583 | /** |
78a88f79 MS |
3584 | * efi_disconnect_controller() - disconnect a controller from a driver |
3585 | * @controller_handle: handle of the controller | |
3586 | * @driver_image_handle: handle of the driver | |
3587 | * @child_handle: handle of the child to destroy | |
3f9b0042 HS |
3588 | * |
3589 | * This function implements the DisconnectController service. | |
3f9b0042 | 3590 | * |
78a88f79 MS |
3591 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
3592 | * details. | |
3593 | * | |
3594 | * Return: status code | |
3f9b0042 HS |
3595 | */ |
3596 | static efi_status_t EFIAPI efi_disconnect_controller( | |
3597 | efi_handle_t controller_handle, | |
3598 | efi_handle_t driver_image_handle, | |
3599 | efi_handle_t child_handle) | |
3600 | { | |
3601 | struct efi_driver_binding_protocol *binding_protocol; | |
3602 | efi_handle_t *child_handle_buffer = NULL; | |
3603 | size_t number_of_children = 0; | |
3604 | efi_status_t r; | |
3f9b0042 | 3605 | struct efi_object *efiobj; |
314bed6c | 3606 | bool sole_child; |
3f9b0042 HS |
3607 | |
3608 | EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle, | |
3609 | child_handle); | |
3610 | ||
3611 | efiobj = efi_search_obj(controller_handle); | |
3612 | if (!efiobj) { | |
3613 | r = EFI_INVALID_PARAMETER; | |
3614 | goto out; | |
3615 | } | |
3616 | ||
3617 | if (child_handle && !efi_search_obj(child_handle)) { | |
3618 | r = EFI_INVALID_PARAMETER; | |
3619 | goto out; | |
3620 | } | |
3621 | ||
3622 | /* If no driver handle is supplied, disconnect all drivers */ | |
3623 | if (!driver_image_handle) { | |
3624 | r = efi_disconnect_all_drivers(efiobj, NULL, child_handle); | |
3625 | goto out; | |
3626 | } | |
3627 | ||
3628 | /* Create list of child handles */ | |
314bed6c HS |
3629 | r = efi_get_child_controllers(efiobj, |
3630 | driver_image_handle, | |
3631 | &number_of_children, | |
3632 | &child_handle_buffer); | |
3633 | if (r != EFI_SUCCESS) | |
3634 | return r; | |
3635 | sole_child = (number_of_children == 1); | |
3636 | ||
3f9b0042 HS |
3637 | if (child_handle) { |
3638 | number_of_children = 1; | |
314bed6c | 3639 | free(child_handle_buffer); |
3f9b0042 | 3640 | child_handle_buffer = &child_handle; |
3f9b0042 HS |
3641 | } |
3642 | ||
3643 | /* Get the driver binding protocol */ | |
3644 | r = EFI_CALL(efi_open_protocol(driver_image_handle, | |
3645 | &efi_guid_driver_binding_protocol, | |
3646 | (void **)&binding_protocol, | |
3647 | driver_image_handle, NULL, | |
3648 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); | |
7dc10c93 HS |
3649 | if (r != EFI_SUCCESS) { |
3650 | r = EFI_INVALID_PARAMETER; | |
3f9b0042 | 3651 | goto out; |
7dc10c93 | 3652 | } |
3f9b0042 HS |
3653 | /* Remove the children */ |
3654 | if (number_of_children) { | |
3655 | r = EFI_CALL(binding_protocol->stop(binding_protocol, | |
3656 | controller_handle, | |
3657 | number_of_children, | |
3658 | child_handle_buffer)); | |
7dc10c93 HS |
3659 | if (r != EFI_SUCCESS) { |
3660 | r = EFI_DEVICE_ERROR; | |
3661 | goto out; | |
3662 | } | |
3f9b0042 HS |
3663 | } |
3664 | /* Remove the driver */ | |
314bed6c | 3665 | if (!child_handle || sole_child) { |
3f9b0042 HS |
3666 | r = EFI_CALL(binding_protocol->stop(binding_protocol, |
3667 | controller_handle, | |
3668 | 0, NULL)); | |
7dc10c93 HS |
3669 | if (r != EFI_SUCCESS) { |
3670 | r = EFI_DEVICE_ERROR; | |
3671 | goto out; | |
3672 | } | |
3673 | } | |
3f9b0042 HS |
3674 | EFI_CALL(efi_close_protocol(driver_image_handle, |
3675 | &efi_guid_driver_binding_protocol, | |
3676 | driver_image_handle, NULL)); | |
7dc10c93 | 3677 | r = EFI_SUCCESS; |
3f9b0042 HS |
3678 | out: |
3679 | if (!child_handle) | |
3680 | free(child_handle_buffer); | |
3681 | return EFI_EXIT(r); | |
3682 | } | |
3683 | ||
640adadf | 3684 | static struct efi_boot_services efi_boot_services = { |
bee91169 | 3685 | .hdr = { |
112f2430 HS |
3686 | .signature = EFI_BOOT_SERVICES_SIGNATURE, |
3687 | .revision = EFI_SPECIFICATION_VERSION, | |
71c846ab | 3688 | .headersize = sizeof(struct efi_boot_services), |
bee91169 AG |
3689 | }, |
3690 | .raise_tpl = efi_raise_tpl, | |
3691 | .restore_tpl = efi_restore_tpl, | |
3692 | .allocate_pages = efi_allocate_pages_ext, | |
3693 | .free_pages = efi_free_pages_ext, | |
3694 | .get_memory_map = efi_get_memory_map_ext, | |
ead1274b | 3695 | .allocate_pool = efi_allocate_pool_ext, |
42417bc8 | 3696 | .free_pool = efi_free_pool_ext, |
49deb455 | 3697 | .create_event = efi_create_event_ext, |
bfc72462 | 3698 | .set_timer = efi_set_timer_ext, |
bee91169 | 3699 | .wait_for_event = efi_wait_for_event, |
c6841592 | 3700 | .signal_event = efi_signal_event_ext, |
bee91169 AG |
3701 | .close_event = efi_close_event, |
3702 | .check_event = efi_check_event, | |
1760ef57 | 3703 | .install_protocol_interface = efi_install_protocol_interface, |
bee91169 | 3704 | .reinstall_protocol_interface = efi_reinstall_protocol_interface, |
cd534083 | 3705 | .uninstall_protocol_interface = efi_uninstall_protocol_interface, |
bee91169 AG |
3706 | .handle_protocol = efi_handle_protocol, |
3707 | .reserved = NULL, | |
3708 | .register_protocol_notify = efi_register_protocol_notify, | |
26329584 | 3709 | .locate_handle = efi_locate_handle_ext, |
bee91169 | 3710 | .locate_device_path = efi_locate_device_path, |
488bf12d | 3711 | .install_configuration_table = efi_install_configuration_table_ext, |
bee91169 AG |
3712 | .load_image = efi_load_image, |
3713 | .start_image = efi_start_image, | |
a86aeaf2 | 3714 | .exit = efi_exit, |
bee91169 AG |
3715 | .unload_image = efi_unload_image, |
3716 | .exit_boot_services = efi_exit_boot_services, | |
3717 | .get_next_monotonic_count = efi_get_next_monotonic_count, | |
3718 | .stall = efi_stall, | |
3719 | .set_watchdog_timer = efi_set_watchdog_timer, | |
3720 | .connect_controller = efi_connect_controller, | |
3721 | .disconnect_controller = efi_disconnect_controller, | |
3722 | .open_protocol = efi_open_protocol, | |
3723 | .close_protocol = efi_close_protocol, | |
3724 | .open_protocol_information = efi_open_protocol_information, | |
3725 | .protocols_per_handle = efi_protocols_per_handle, | |
3726 | .locate_handle_buffer = efi_locate_handle_buffer, | |
3727 | .locate_protocol = efi_locate_protocol, | |
ab9efa97 HS |
3728 | .install_multiple_protocol_interfaces = |
3729 | efi_install_multiple_protocol_interfaces, | |
3730 | .uninstall_multiple_protocol_interfaces = | |
3731 | efi_uninstall_multiple_protocol_interfaces, | |
bee91169 AG |
3732 | .calculate_crc32 = efi_calculate_crc32, |
3733 | .copy_mem = efi_copy_mem, | |
3734 | .set_mem = efi_set_mem, | |
9f0930e5 | 3735 | .create_event_ex = efi_create_event_ex, |
bee91169 AG |
3736 | }; |
3737 | ||
0b386537 | 3738 | static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot"; |
bee91169 | 3739 | |
3c63db9c | 3740 | struct efi_system_table __efi_runtime_data systab = { |
bee91169 AG |
3741 | .hdr = { |
3742 | .signature = EFI_SYSTEM_TABLE_SIGNATURE, | |
112f2430 | 3743 | .revision = EFI_SPECIFICATION_VERSION, |
71c846ab | 3744 | .headersize = sizeof(struct efi_system_table), |
bee91169 | 3745 | }, |
0b386537 HS |
3746 | .fw_vendor = firmware_vendor, |
3747 | .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8, | |
3c783bfb | 3748 | .runtime = &efi_runtime_services, |
bee91169 | 3749 | .nr_tables = 0, |
4182a129 | 3750 | .tables = NULL, |
bee91169 | 3751 | }; |
640adadf HS |
3752 | |
3753 | /** | |
3754 | * efi_initialize_system_table() - Initialize system table | |
3755 | * | |
0414359a | 3756 | * Return: status code |
640adadf HS |
3757 | */ |
3758 | efi_status_t efi_initialize_system_table(void) | |
3759 | { | |
4182a129 HS |
3760 | efi_status_t ret; |
3761 | ||
3762 | /* Allocate configuration table array */ | |
3763 | ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA, | |
3764 | EFI_MAX_CONFIGURATION_TABLES * | |
3765 | sizeof(struct efi_configuration_table), | |
3766 | (void **)&systab.tables); | |
3767 | ||
93148eba HS |
3768 | /* |
3769 | * These entries will be set to NULL in ExitBootServices(). To avoid | |
3770 | * relocation in SetVirtualAddressMap(), set them dynamically. | |
3771 | */ | |
3772 | systab.con_in = &efi_con_in; | |
3773 | systab.con_out = &efi_con_out; | |
3774 | systab.std_err = &efi_con_out; | |
3775 | systab.boottime = &efi_boot_services; | |
3776 | ||
b72aaa87 | 3777 | /* Set CRC32 field in table headers */ |
640adadf HS |
3778 | efi_update_table_header_crc32(&systab.hdr); |
3779 | efi_update_table_header_crc32(&efi_runtime_services.hdr); | |
3780 | efi_update_table_header_crc32(&efi_boot_services.hdr); | |
4182a129 HS |
3781 | |
3782 | return ret; | |
640adadf | 3783 | } |