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