]> git.ipfire.org Git - thirdparty/u-boot.git/blame - lib/efi_loader/efi_boottime.c
efi_selftest: correct efi_selftest_manageprotocols
[thirdparty/u-boot.git] / lib / efi_loader / efi_boottime.c
CommitLineData
f739fcd8 1// SPDX-License-Identifier: GPL-2.0+
bee91169
AG
2/*
3 * EFI application boot time services
4 *
5 * Copyright (c) 2016 Alexander Graf
bee91169
AG
6 */
7
bee91169 8#include <common.h>
7d963323 9#include <div64.h>
bee91169 10#include <efi_loader.h>
ad644e7c 11#include <environment.h>
bee91169 12#include <malloc.h>
b08c8c48 13#include <linux/libfdt_env.h>
bee91169
AG
14#include <u-boot/crc.h>
15#include <bootm.h>
16#include <inttypes.h>
17#include <watchdog.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
32f4b2f8 21/* Task priority level */
152cade3 22static efi_uintn_t efi_tpl = TPL_APPLICATION;
32f4b2f8 23
bee91169
AG
24/* This list contains all the EFI objects our payload has access to */
25LIST_HEAD(efi_obj_list);
26
43bce442 27/* List of all events */
b095f3c8 28LIST_HEAD(efi_events);
43bce442 29
bee91169
AG
30/*
31 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
32 * we need to do trickery with caches. Since we don't want to break the EFI
33 * aware boot path, only apply hacks when loading exiting directly (breaking
34 * direct Linux EFI booting along the way - oh well).
35 */
36static bool efi_is_direct_boot = true;
37
38/*
39 * EFI can pass arbitrary additional "tables" containing vendor specific
40 * information to the payload. One such table is the FDT table which contains
41 * a pointer to a flattened device tree blob.
42 *
43 * In most cases we want to pass an FDT to the payload, so reserve one slot of
44 * config table space for it. The pointer gets populated by do_bootefi_exec().
45 */
3c63db9c 46static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
bee91169 47
65e4c0b1 48#ifdef CONFIG_ARM
bee91169
AG
49/*
50 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
51 * fixed when compiling U-Boot. However, the payload does not know about that
52 * restriction so we need to manually swap its and our view of that register on
53 * EFI callback entry/exit.
54 */
55static volatile void *efi_gd, *app_gd;
65e4c0b1 56#endif
bee91169 57
c160d2f5 58static int entry_count;
af65db85 59static int nesting_level;
bc4f9133
HS
60/* GUID of the device tree table */
61const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
f0959dbe
HS
62/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
63const efi_guid_t efi_guid_driver_binding_protocol =
64 EFI_DRIVER_BINDING_PROTOCOL_GUID;
c160d2f5 65
a3a28f5f
HS
66/* event group ExitBootServices() invoked */
67const efi_guid_t efi_guid_event_group_exit_boot_services =
68 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
69/* event group SetVirtualAddressMap() invoked */
70const efi_guid_t efi_guid_event_group_virtual_address_change =
71 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
72/* event group memory map changed */
73const efi_guid_t efi_guid_event_group_memory_map_change =
74 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
75/* event group boot manager about to boot */
76const efi_guid_t efi_guid_event_group_ready_to_boot =
77 EFI_EVENT_GROUP_READY_TO_BOOT;
78/* event group ResetSystem() invoked (before ExitBootServices) */
79const efi_guid_t efi_guid_event_group_reset_system =
80 EFI_EVENT_GROUP_RESET_SYSTEM;
81
2074f700
HS
82static efi_status_t EFIAPI efi_disconnect_controller(
83 efi_handle_t controller_handle,
84 efi_handle_t driver_image_handle,
85 efi_handle_t child_handle);
3f9b0042 86
c160d2f5
RC
87/* Called on every callback entry */
88int __efi_entry_check(void)
89{
90 int ret = entry_count++ == 0;
91#ifdef CONFIG_ARM
92 assert(efi_gd);
93 app_gd = gd;
94 gd = efi_gd;
95#endif
96 return ret;
97}
98
99/* Called on every callback exit */
100int __efi_exit_check(void)
101{
102 int ret = --entry_count == 0;
103#ifdef CONFIG_ARM
104 gd = app_gd;
105#endif
106 return ret;
107}
108
bee91169
AG
109/* Called from do_bootefi_exec() */
110void efi_save_gd(void)
111{
65e4c0b1 112#ifdef CONFIG_ARM
bee91169 113 efi_gd = gd;
65e4c0b1 114#endif
bee91169
AG
115}
116
c160d2f5
RC
117/*
118 * Special case handler for error/abort that just forces things back
119 * to u-boot world so we can dump out an abort msg, without any care
120 * about returning back to UEFI world.
121 */
bee91169
AG
122void efi_restore_gd(void)
123{
65e4c0b1 124#ifdef CONFIG_ARM
bee91169
AG
125 /* Only restore if we're already in EFI context */
126 if (!efi_gd)
127 return;
bee91169 128 gd = efi_gd;
65e4c0b1 129#endif
bee91169
AG
130}
131
af65db85 132/*
c8df80c5
HS
133 * Return a string for indenting with two spaces per level. A maximum of ten
134 * indent levels is supported. Higher indent levels will be truncated.
135 *
136 * @level indent level
137 * @return indent string
af65db85
RC
138 */
139static const char *indent_string(int level)
140{
141 const char *indent = " ";
142 const int max = strlen(indent);
ab9efa97 143
af65db85
RC
144 level = min(max, level * 2);
145 return &indent[max - level];
146}
147
ae0bd3a9
HS
148const char *__efi_nesting(void)
149{
150 return indent_string(nesting_level);
151}
152
af65db85
RC
153const char *__efi_nesting_inc(void)
154{
155 return indent_string(nesting_level++);
156}
157
158const char *__efi_nesting_dec(void)
159{
160 return indent_string(--nesting_level);
161}
162
332468f7
HS
163/*
164 * Queue an EFI event.
165 *
166 * This function queues the notification function of the event for future
167 * execution.
168 *
169 * The notification function is called if the task priority level of the
170 * event is higher than the current task priority level.
171 *
172 * For the SignalEvent service see efi_signal_event_ext.
173 *
174 * @event event to signal
9bc9664d 175 * @check_tpl check the TPL level
332468f7 176 */
b095f3c8 177static void efi_queue_event(struct efi_event *event, bool check_tpl)
c6841592 178{
ca62a4f5 179 if (event->notify_function) {
e190e897 180 event->is_queued = true;
32f4b2f8 181 /* Check TPL */
9bc9664d 182 if (check_tpl && efi_tpl >= event->notify_tpl)
32f4b2f8 183 return;
ea630ce9
HS
184 EFI_CALL_VOID(event->notify_function(event,
185 event->notify_context));
c6841592 186 }
e190e897 187 event->is_queued = false;
c6841592
HS
188}
189
b095f3c8
HS
190/*
191 * Signal an EFI event.
192 *
193 * This function signals an event. If the event belongs to an event group
194 * all events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
195 * their notification function is queued.
196 *
197 * For the SignalEvent service see efi_signal_event_ext.
198 *
199 * @event event to signal
200 * @check_tpl check the TPL level
201 */
202void efi_signal_event(struct efi_event *event, bool check_tpl)
203{
204 if (event->group) {
205 struct efi_event *evt;
206
207 /*
208 * The signaled state has to set before executing any
209 * notification function
210 */
211 list_for_each_entry(evt, &efi_events, link) {
212 if (!evt->group || guidcmp(evt->group, event->group))
213 continue;
214 if (evt->is_signaled)
215 continue;
216 evt->is_signaled = true;
217 if (evt->type & EVT_NOTIFY_SIGNAL &&
218 evt->notify_function)
219 evt->is_queued = true;
220 }
221 list_for_each_entry(evt, &efi_events, link) {
222 if (!evt->group || guidcmp(evt->group, event->group))
223 continue;
224 if (evt->is_queued)
225 efi_queue_event(evt, check_tpl);
226 }
227 } else if (!event->is_signaled) {
228 event->is_signaled = true;
229 if (event->type & EVT_NOTIFY_SIGNAL)
230 efi_queue_event(event, check_tpl);
231 }
232}
233
332468f7
HS
234/*
235 * Raise the task priority level.
236 *
237 * This function implements the RaiseTpl service.
238 * See the Unified Extensible Firmware Interface (UEFI) specification
239 * for details.
240 *
241 * @new_tpl new value of the task priority level
242 * @return old value of the task priority level
243 */
152cade3 244static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
bee91169 245{
152cade3 246 efi_uintn_t old_tpl = efi_tpl;
32f4b2f8 247
503f2695 248 EFI_ENTRY("0x%zx", new_tpl);
32f4b2f8
HS
249
250 if (new_tpl < efi_tpl)
251 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
252 efi_tpl = new_tpl;
253 if (efi_tpl > TPL_HIGH_LEVEL)
254 efi_tpl = TPL_HIGH_LEVEL;
255
256 EFI_EXIT(EFI_SUCCESS);
257 return old_tpl;
bee91169
AG
258}
259
332468f7
HS
260/*
261 * Lower the task priority level.
262 *
263 * This function implements the RestoreTpl service.
264 * See the Unified Extensible Firmware Interface (UEFI) specification
265 * for details.
266 *
267 * @old_tpl value of the task priority level to be restored
268 */
152cade3 269static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
bee91169 270{
503f2695 271 EFI_ENTRY("0x%zx", old_tpl);
32f4b2f8
HS
272
273 if (old_tpl > efi_tpl)
274 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
275 efi_tpl = old_tpl;
276 if (efi_tpl > TPL_HIGH_LEVEL)
277 efi_tpl = TPL_HIGH_LEVEL;
278
0f7fcc72
HS
279 /*
280 * Lowering the TPL may have made queued events eligible for execution.
281 */
282 efi_timer_check();
283
32f4b2f8 284 EFI_EXIT(EFI_SUCCESS);
bee91169
AG
285}
286
332468f7
HS
287/*
288 * Allocate memory pages.
289 *
290 * This function implements the AllocatePages service.
291 * See the Unified Extensible Firmware Interface (UEFI) specification
292 * for details.
293 *
294 * @type type of allocation to be performed
295 * @memory_type usage type of the allocated memory
296 * @pages number of pages to be allocated
297 * @memory allocated memory
298 * @return status code
299 */
6e0bf8d8 300static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
f5a2a938 301 efi_uintn_t pages,
6e0bf8d8 302 uint64_t *memory)
bee91169
AG
303{
304 efi_status_t r;
305
f5a2a938 306 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
bee91169
AG
307 r = efi_allocate_pages(type, memory_type, pages, memory);
308 return EFI_EXIT(r);
309}
310
332468f7
HS
311/*
312 * Free memory pages.
313 *
314 * This function implements the FreePages service.
315 * See the Unified Extensible Firmware Interface (UEFI) specification
316 * for details.
317 *
318 * @memory start of the memory area to be freed
319 * @pages number of pages to be freed
320 * @return status code
321 */
6e0bf8d8 322static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
f5a2a938 323 efi_uintn_t pages)
bee91169
AG
324{
325 efi_status_t r;
326
ab9efa97 327 EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
bee91169
AG
328 r = efi_free_pages(memory, pages);
329 return EFI_EXIT(r);
330}
331
332468f7
HS
332/*
333 * Get map describing memory usage.
334 *
335 * This function implements the GetMemoryMap service.
336 * See the Unified Extensible Firmware Interface (UEFI) specification
337 * for details.
338 *
339 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
340 * on exit the size of the copied memory map
341 * @memory_map buffer to which the memory map is written
342 * @map_key key for the memory map
343 * @descriptor_size size of an individual memory descriptor
344 * @descriptor_version version number of the memory descriptor structure
345 * @return status code
346 */
6e0bf8d8 347static efi_status_t EFIAPI efi_get_memory_map_ext(
f5a2a938 348 efi_uintn_t *memory_map_size,
6e0bf8d8 349 struct efi_mem_desc *memory_map,
f5a2a938
HS
350 efi_uintn_t *map_key,
351 efi_uintn_t *descriptor_size,
6e0bf8d8 352 uint32_t *descriptor_version)
bee91169
AG
353{
354 efi_status_t r;
355
356 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
357 map_key, descriptor_size, descriptor_version);
358 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
359 descriptor_size, descriptor_version);
360 return EFI_EXIT(r);
361}
362
332468f7
HS
363/*
364 * Allocate memory from pool.
365 *
366 * This function implements the AllocatePool service.
367 * See the Unified Extensible Firmware Interface (UEFI) specification
368 * for details.
369 *
370 * @pool_type type of the pool from which memory is to be allocated
371 * @size number of bytes to be allocated
372 * @buffer allocated memory
373 * @return status code
374 */
ead1274b 375static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
f5a2a938 376 efi_uintn_t size,
ead1274b 377 void **buffer)
bee91169 378{
1cd29f0a
AG
379 efi_status_t r;
380
f5a2a938 381 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
ead1274b 382 r = efi_allocate_pool(pool_type, size, buffer);
1cd29f0a 383 return EFI_EXIT(r);
bee91169
AG
384}
385
332468f7
HS
386/*
387 * Free memory from pool.
388 *
389 * This function implements the FreePool service.
390 * See the Unified Extensible Firmware Interface (UEFI) specification
391 * for details.
392 *
393 * @buffer start of memory to be freed
394 * @return status code
395 */
42417bc8 396static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
bee91169 397{
1cd29f0a
AG
398 efi_status_t r;
399
400 EFI_ENTRY("%p", buffer);
42417bc8 401 r = efi_free_pool(buffer);
1cd29f0a 402 return EFI_EXIT(r);
bee91169
AG
403}
404
44549d62
HS
405/*
406 * Add a new object to the object list.
407 *
408 * The protocols list is initialized.
409 * The object handle is set.
410 *
411 * @obj object to be added
412 */
413void efi_add_handle(struct efi_object *obj)
414{
415 if (!obj)
416 return;
417 INIT_LIST_HEAD(&obj->protocols);
418 obj->handle = obj;
419 list_add_tail(&obj->link, &efi_obj_list);
420}
421
2edab5e2
HS
422/*
423 * Create handle.
424 *
425 * @handle new handle
426 * @return status code
427 */
2074f700 428efi_status_t efi_create_handle(efi_handle_t *handle)
3cc6e3fe
HS
429{
430 struct efi_object *obj;
431 efi_status_t r;
432
433 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
434 sizeof(struct efi_object),
435 (void **)&obj);
436 if (r != EFI_SUCCESS)
437 return r;
44549d62
HS
438 efi_add_handle(obj);
439 *handle = obj->handle;
3cc6e3fe
HS
440 return r;
441}
442
678e03a0
HS
443/*
444 * Find a protocol on a handle.
445 *
446 * @handle handle
447 * @protocol_guid GUID of the protocol
448 * @handler reference to the protocol
449 * @return status code
450 */
2074f700 451efi_status_t efi_search_protocol(const efi_handle_t handle,
678e03a0
HS
452 const efi_guid_t *protocol_guid,
453 struct efi_handler **handler)
454{
455 struct efi_object *efiobj;
456 struct list_head *lhandle;
457
458 if (!handle || !protocol_guid)
459 return EFI_INVALID_PARAMETER;
460 efiobj = efi_search_obj(handle);
461 if (!efiobj)
462 return EFI_INVALID_PARAMETER;
463 list_for_each(lhandle, &efiobj->protocols) {
464 struct efi_handler *protocol;
465
466 protocol = list_entry(lhandle, struct efi_handler, link);
467 if (!guidcmp(protocol->guid, protocol_guid)) {
468 if (handler)
469 *handler = protocol;
470 return EFI_SUCCESS;
471 }
472 }
473 return EFI_NOT_FOUND;
474}
475
476/*
477 * Delete protocol from a handle.
478 *
479 * @handle handle from which the protocol shall be deleted
480 * @protocol GUID of the protocol to be deleted
481 * @protocol_interface interface of the protocol implementation
482 * @return status code
483 */
2074f700
HS
484efi_status_t efi_remove_protocol(const efi_handle_t handle,
485 const efi_guid_t *protocol,
678e03a0
HS
486 void *protocol_interface)
487{
488 struct efi_handler *handler;
489 efi_status_t ret;
490
491 ret = efi_search_protocol(handle, protocol, &handler);
492 if (ret != EFI_SUCCESS)
493 return ret;
494 if (guidcmp(handler->guid, protocol))
495 return EFI_INVALID_PARAMETER;
496 list_del(&handler->link);
497 free(handler);
498 return EFI_SUCCESS;
499}
500
501/*
502 * Delete all protocols from a handle.
503 *
504 * @handle handle from which the protocols shall be deleted
505 * @return status code
506 */
2074f700 507efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
678e03a0
HS
508{
509 struct efi_object *efiobj;
32e6fed6
HS
510 struct efi_handler *protocol;
511 struct efi_handler *pos;
678e03a0
HS
512
513 efiobj = efi_search_obj(handle);
514 if (!efiobj)
515 return EFI_INVALID_PARAMETER;
32e6fed6 516 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
678e03a0
HS
517 efi_status_t ret;
518
678e03a0
HS
519 ret = efi_remove_protocol(handle, protocol->guid,
520 protocol->protocol_interface);
521 if (ret != EFI_SUCCESS)
522 return ret;
523 }
524 return EFI_SUCCESS;
525}
526
527/*
528 * Delete handle.
529 *
530 * @handle handle to delete
531 */
532void efi_delete_handle(struct efi_object *obj)
533{
534 if (!obj)
535 return;
536 efi_remove_all_protocols(obj->handle);
537 list_del(&obj->link);
538 free(obj);
539}
540
bee91169 541/*
43bce442
HS
542 * Check if a pointer is a valid event.
543 *
544 * @event pointer to check
545 * @return status code
bee91169 546 */
43bce442
HS
547static efi_status_t efi_is_event(const struct efi_event *event)
548{
549 const struct efi_event *evt;
550
551 if (!event)
552 return EFI_INVALID_PARAMETER;
553 list_for_each_entry(evt, &efi_events, link) {
554 if (evt == event)
555 return EFI_SUCCESS;
556 }
557 return EFI_INVALID_PARAMETER;
558}
bee91169 559
332468f7
HS
560/*
561 * Create an event.
562 *
563 * This function is used inside U-Boot code to create an event.
564 *
565 * For the API function implementing the CreateEvent service see
566 * efi_create_event_ext.
567 *
568 * @type type of the event to create
569 * @notify_tpl task priority level of the event
570 * @notify_function notification function of the event
571 * @notify_context pointer passed to the notification function
572 * @event created event
573 * @return status code
574 */
152cade3 575efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
49deb455 576 void (EFIAPI *notify_function) (
2fd945fe
HS
577 struct efi_event *event,
578 void *context),
b095f3c8
HS
579 void *notify_context, efi_guid_t *group,
580 struct efi_event **event)
bee91169 581{
43bce442 582 struct efi_event *evt;
c6841592 583
a95343b8 584 if (event == NULL)
49deb455 585 return EFI_INVALID_PARAMETER;
a95343b8
JG
586
587 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
49deb455 588 return EFI_INVALID_PARAMETER;
a95343b8 589
43bce442 590 if ((type & (EVT_NOTIFY_SIGNAL | EVT_NOTIFY_WAIT)) &&
a95343b8 591 notify_function == NULL)
49deb455 592 return EFI_INVALID_PARAMETER;
a95343b8 593
43bce442
HS
594 evt = calloc(1, sizeof(struct efi_event));
595 if (!evt)
596 return EFI_OUT_OF_RESOURCES;
597 evt->type = type;
598 evt->notify_tpl = notify_tpl;
599 evt->notify_function = notify_function;
600 evt->notify_context = notify_context;
b095f3c8 601 evt->group = group;
43bce442
HS
602 /* Disable timers on bootup */
603 evt->trigger_next = -1ULL;
604 evt->is_queued = false;
605 evt->is_signaled = false;
606 list_add_tail(&evt->link, &efi_events);
607 *event = evt;
608 return EFI_SUCCESS;
bee91169
AG
609}
610
9f0930e5
HS
611/*
612 * Create an event in a group.
613 *
614 * This function implements the CreateEventEx service.
615 * See the Unified Extensible Firmware Interface (UEFI) specification
616 * for details.
617 * TODO: Support event groups
618 *
619 * @type type of the event to create
620 * @notify_tpl task priority level of the event
621 * @notify_function notification function of the event
622 * @notify_context pointer passed to the notification function
623 * @event created event
624 * @event_group event group
625 * @return status code
626 */
627efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
628 void (EFIAPI *notify_function) (
629 struct efi_event *event,
630 void *context),
631 void *notify_context,
632 efi_guid_t *event_group,
633 struct efi_event **event)
634{
635 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
636 notify_context, event_group);
9f0930e5 637 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
b095f3c8 638 notify_context, event_group, event));
9f0930e5
HS
639}
640
332468f7
HS
641/*
642 * Create an event.
643 *
644 * This function implements the CreateEvent service.
645 * See the Unified Extensible Firmware Interface (UEFI) specification
646 * for details.
647 *
648 * @type type of the event to create
649 * @notify_tpl task priority level of the event
650 * @notify_function notification function of the event
651 * @notify_context pointer passed to the notification function
652 * @event created event
653 * @return status code
654 */
49deb455 655static efi_status_t EFIAPI efi_create_event_ext(
152cade3 656 uint32_t type, efi_uintn_t notify_tpl,
49deb455
HS
657 void (EFIAPI *notify_function) (
658 struct efi_event *event,
659 void *context),
660 void *notify_context, struct efi_event **event)
661{
662 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
663 notify_context);
664 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
b095f3c8 665 notify_context, NULL, event));
49deb455
HS
666}
667
bee91169 668/*
332468f7
HS
669 * Check if a timer event has occurred or a queued notification function should
670 * be called.
671 *
bee91169 672 * Our timers have to work without interrupts, so we check whenever keyboard
332468f7 673 * input or disk accesses happen if enough time elapsed for them to fire.
bee91169
AG
674 */
675void efi_timer_check(void)
676{
43bce442 677 struct efi_event *evt;
bee91169
AG
678 u64 now = timer_get_us();
679
43bce442
HS
680 list_for_each_entry(evt, &efi_events, link) {
681 if (evt->is_queued)
b095f3c8 682 efi_queue_event(evt, true);
43bce442 683 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
ca62a4f5 684 continue;
43bce442 685 switch (evt->trigger_type) {
ca62a4f5 686 case EFI_TIMER_RELATIVE:
43bce442 687 evt->trigger_type = EFI_TIMER_STOP;
ca62a4f5
HS
688 break;
689 case EFI_TIMER_PERIODIC:
43bce442 690 evt->trigger_next += evt->trigger_time;
ca62a4f5
HS
691 break;
692 default:
693 continue;
c6841592 694 }
b095f3c8 695 evt->is_signaled = false;
43bce442 696 efi_signal_event(evt, true);
bee91169 697 }
bee91169
AG
698 WATCHDOG_RESET();
699}
700
332468f7
HS
701/*
702 * Set the trigger time for a timer event or stop the event.
703 *
704 * This is the function for internal usage in U-Boot. For the API function
705 * implementing the SetTimer service see efi_set_timer_ext.
706 *
707 * @event event for which the timer is set
708 * @type type of the timer
709 * @trigger_time trigger period in multiples of 100ns
710 * @return status code
711 */
b521d29e 712efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
bfc72462 713 uint64_t trigger_time)
bee91169 714{
43bce442
HS
715 /* Check that the event is valid */
716 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
717 return EFI_INVALID_PARAMETER;
bee91169 718
8787b02e
HS
719 /*
720 * The parameter defines a multiple of 100ns.
721 * We use multiples of 1000ns. So divide by 10.
722 */
7d963323 723 do_div(trigger_time, 10);
bee91169 724
43bce442
HS
725 switch (type) {
726 case EFI_TIMER_STOP:
727 event->trigger_next = -1ULL;
728 break;
729 case EFI_TIMER_PERIODIC:
730 case EFI_TIMER_RELATIVE:
731 event->trigger_next = timer_get_us() + trigger_time;
732 break;
733 default:
734 return EFI_INVALID_PARAMETER;
bee91169 735 }
43bce442
HS
736 event->trigger_type = type;
737 event->trigger_time = trigger_time;
738 event->is_signaled = false;
739 return EFI_SUCCESS;
bfc72462
HS
740}
741
332468f7
HS
742/*
743 * Set the trigger time for a timer event or stop the event.
744 *
745 * This function implements the SetTimer service.
746 * See the Unified Extensible Firmware Interface (UEFI) specification
747 * for details.
748 *
749 * @event event for which the timer is set
750 * @type type of the timer
751 * @trigger_time trigger period in multiples of 100ns
752 * @return status code
753 */
b521d29e
HS
754static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
755 enum efi_timer_delay type,
756 uint64_t trigger_time)
bfc72462 757{
ab9efa97 758 EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
bfc72462 759 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
bee91169
AG
760}
761
332468f7
HS
762/*
763 * Wait for events to be signaled.
764 *
765 * This function implements the WaitForEvent service.
766 * See the Unified Extensible Firmware Interface (UEFI) specification
767 * for details.
768 *
769 * @num_events number of events to be waited for
770 * @events events to be waited for
771 * @index index of the event that was signaled
772 * @return status code
773 */
f5a2a938 774static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
2fd945fe 775 struct efi_event **event,
f5a2a938 776 efi_uintn_t *index)
bee91169 777{
43bce442 778 int i;
bee91169 779
f5a2a938 780 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
bee91169 781
c6841592
HS
782 /* Check parameters */
783 if (!num_events || !event)
784 return EFI_EXIT(EFI_INVALID_PARAMETER);
32f4b2f8
HS
785 /* Check TPL */
786 if (efi_tpl != TPL_APPLICATION)
787 return EFI_EXIT(EFI_UNSUPPORTED);
c6841592 788 for (i = 0; i < num_events; ++i) {
43bce442
HS
789 if (efi_is_event(event[i]) != EFI_SUCCESS)
790 return EFI_EXIT(EFI_INVALID_PARAMETER);
c6841592
HS
791 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
792 return EFI_EXIT(EFI_INVALID_PARAMETER);
e190e897 793 if (!event[i]->is_signaled)
b095f3c8 794 efi_queue_event(event[i], true);
c6841592
HS
795 }
796
797 /* Wait for signal */
798 for (;;) {
799 for (i = 0; i < num_events; ++i) {
e190e897 800 if (event[i]->is_signaled)
c6841592
HS
801 goto out;
802 }
803 /* Allow events to occur. */
804 efi_timer_check();
805 }
806
807out:
808 /*
809 * Reset the signal which is passed to the caller to allow periodic
810 * events to occur.
811 */
e190e897 812 event[i]->is_signaled = false;
c6841592
HS
813 if (index)
814 *index = i;
bee91169
AG
815
816 return EFI_EXIT(EFI_SUCCESS);
817}
818
332468f7
HS
819/*
820 * Signal an EFI event.
821 *
822 * This function implements the SignalEvent service.
823 * See the Unified Extensible Firmware Interface (UEFI) specification
824 * for details.
825 *
826 * This functions sets the signaled state of the event and queues the
827 * notification function for execution.
828 *
829 * @event event to signal
10a08c4e 830 * @return status code
332468f7 831 */
c6841592 832static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
bee91169
AG
833{
834 EFI_ENTRY("%p", event);
43bce442
HS
835 if (efi_is_event(event) != EFI_SUCCESS)
836 return EFI_EXIT(EFI_INVALID_PARAMETER);
b095f3c8 837 efi_signal_event(event, true);
bee91169
AG
838 return EFI_EXIT(EFI_SUCCESS);
839}
840
332468f7
HS
841/*
842 * Close an EFI event.
843 *
844 * This function implements the CloseEvent service.
845 * See the Unified Extensible Firmware Interface (UEFI) specification
846 * for details.
847 *
848 * @event event to close
849 * @return status code
850 */
2fd945fe 851static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
bee91169
AG
852{
853 EFI_ENTRY("%p", event);
43bce442
HS
854 if (efi_is_event(event) != EFI_SUCCESS)
855 return EFI_EXIT(EFI_INVALID_PARAMETER);
856 list_del(&event->link);
857 free(event);
858 return EFI_EXIT(EFI_SUCCESS);
bee91169
AG
859}
860
332468f7
HS
861/*
862 * Check if an event is signaled.
863 *
864 * This function implements the CheckEvent service.
865 * See the Unified Extensible Firmware Interface (UEFI) specification
866 * for details.
867 *
7069515e
HS
868 * If an event is not signaled yet, the notification function is queued.
869 * The signaled state is cleared.
332468f7
HS
870 *
871 * @event event to check
872 * @return status code
873 */
2fd945fe 874static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
bee91169
AG
875{
876 EFI_ENTRY("%p", event);
c6841592 877 efi_timer_check();
43bce442
HS
878 if (efi_is_event(event) != EFI_SUCCESS ||
879 event->type & EVT_NOTIFY_SIGNAL)
880 return EFI_EXIT(EFI_INVALID_PARAMETER);
881 if (!event->is_signaled)
b095f3c8 882 efi_queue_event(event, true);
43bce442
HS
883 if (event->is_signaled) {
884 event->is_signaled = false;
885 return EFI_EXIT(EFI_SUCCESS);
c6841592 886 }
43bce442 887 return EFI_EXIT(EFI_NOT_READY);
bee91169
AG
888}
889
7b9f8ad7
HS
890/*
891 * Find the internal EFI object for a handle.
892 *
893 * @handle handle to find
894 * @return EFI object
895 */
2074f700 896struct efi_object *efi_search_obj(const efi_handle_t handle)
7b9f8ad7 897{
1b68153a 898 struct efi_object *efiobj;
7b9f8ad7 899
1b68153a 900 list_for_each_entry(efiobj, &efi_obj_list, link) {
7b9f8ad7
HS
901 if (efiobj->handle == handle)
902 return efiobj;
903 }
904
905 return NULL;
906}
907
fe1599da
HS
908/*
909 * Create open protocol info entry and add it to a protocol.
910 *
911 * @handler handler of a protocol
912 * @return open protocol info entry
913 */
914static struct efi_open_protocol_info_entry *efi_create_open_info(
915 struct efi_handler *handler)
916{
917 struct efi_open_protocol_info_item *item;
918
919 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
920 if (!item)
921 return NULL;
922 /* Append the item to the open protocol info list. */
923 list_add_tail(&item->link, &handler->open_infos);
924
925 return &item->info;
926}
927
928/*
929 * Remove an open protocol info entry from a protocol.
930 *
931 * @handler handler of a protocol
932 * @return status code
933 */
934static efi_status_t efi_delete_open_info(
935 struct efi_open_protocol_info_item *item)
936{
937 list_del(&item->link);
938 free(item);
939 return EFI_SUCCESS;
940}
941
3f79a2b5
HS
942/*
943 * Install new protocol on a handle.
944 *
945 * @handle handle on which the protocol shall be installed
946 * @protocol GUID of the protocol to be installed
947 * @protocol_interface interface of the protocol implementation
948 * @return status code
949 */
2074f700
HS
950efi_status_t efi_add_protocol(const efi_handle_t handle,
951 const efi_guid_t *protocol,
3f79a2b5
HS
952 void *protocol_interface)
953{
954 struct efi_object *efiobj;
955 struct efi_handler *handler;
956 efi_status_t ret;
3f79a2b5
HS
957
958 efiobj = efi_search_obj(handle);
959 if (!efiobj)
960 return EFI_INVALID_PARAMETER;
961 ret = efi_search_protocol(handle, protocol, NULL);
962 if (ret != EFI_NOT_FOUND)
963 return EFI_INVALID_PARAMETER;
964 handler = calloc(1, sizeof(struct efi_handler));
965 if (!handler)
966 return EFI_OUT_OF_RESOURCES;
69fb6b1a
HS
967 handler->guid = protocol;
968 handler->protocol_interface = protocol_interface;
fe1599da 969 INIT_LIST_HEAD(&handler->open_infos);
69fb6b1a 970 list_add_tail(&handler->link, &efiobj->protocols);
d5504144
HS
971 if (!guidcmp(&efi_guid_device_path, protocol))
972 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
69fb6b1a 973 return EFI_SUCCESS;
3f79a2b5
HS
974}
975
332468f7
HS
976/*
977 * Install protocol interface.
978 *
1760ef57
HS
979 * This function implements the InstallProtocolInterface service.
980 * See the Unified Extensible Firmware Interface (UEFI) specification
981 * for details.
332468f7
HS
982 *
983 * @handle handle on which the protocol shall be installed
984 * @protocol GUID of the protocol to be installed
985 * @protocol_interface_type type of the interface to be installed,
986 * always EFI_NATIVE_INTERFACE
987 * @protocol_interface interface of the protocol implementation
988 * @return status code
989 */
1760ef57
HS
990static efi_status_t EFIAPI efi_install_protocol_interface(
991 void **handle, const efi_guid_t *protocol,
992 int protocol_interface_type, void *protocol_interface)
bee91169 993{
e0549f8a
HS
994 efi_status_t r;
995
1760ef57
HS
996 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
997 protocol_interface);
998
e0549f8a
HS
999 if (!handle || !protocol ||
1000 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1001 r = EFI_INVALID_PARAMETER;
1002 goto out;
1003 }
1004
1005 /* Create new handle if requested. */
1006 if (!*handle) {
3cc6e3fe
HS
1007 r = efi_create_handle(handle);
1008 if (r != EFI_SUCCESS)
1009 goto out;
af1408e0
HS
1010 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1011 *handle);
1012 } else {
1013 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1014 *handle);
e0549f8a 1015 }
1202530d
HS
1016 /* Add new protocol */
1017 r = efi_add_protocol(*handle, protocol, protocol_interface);
e0549f8a 1018out:
1760ef57 1019 return EFI_EXIT(r);
bee91169 1020}
e0549f8a 1021
332468f7
HS
1022/*
1023 * Reinstall protocol interface.
1024 *
1025 * This function implements the ReinstallProtocolInterface service.
1026 * See the Unified Extensible Firmware Interface (UEFI) specification
1027 * for details.
1028 *
1029 * @handle handle on which the protocol shall be
1030 * reinstalled
1031 * @protocol GUID of the protocol to be installed
1032 * @old_interface interface to be removed
1033 * @new_interface interface to be installed
1034 * @return status code
1035 */
2074f700
HS
1036static efi_status_t EFIAPI efi_reinstall_protocol_interface(
1037 efi_handle_t handle, const efi_guid_t *protocol,
1038 void *old_interface, void *new_interface)
bee91169 1039{
778e6af8 1040 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
bee91169
AG
1041 new_interface);
1042 return EFI_EXIT(EFI_ACCESS_DENIED);
1043}
1044
3f9b0042
HS
1045/*
1046 * Get all drivers associated to a controller.
1047 * The allocated buffer has to be freed with free().
1048 *
1049 * @efiobj handle of the controller
1050 * @protocol protocol guid (optional)
1051 * @number_of_drivers number of child controllers
1052 * @driver_handle_buffer handles of the the drivers
1053 * @return status code
1054 */
1055static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1056 const efi_guid_t *protocol,
1057 efi_uintn_t *number_of_drivers,
1058 efi_handle_t **driver_handle_buffer)
1059{
1060 struct efi_handler *handler;
1061 struct efi_open_protocol_info_item *item;
1062 efi_uintn_t count = 0, i;
1063 bool duplicate;
1064
1065 /* Count all driver associations */
1066 list_for_each_entry(handler, &efiobj->protocols, link) {
1067 if (protocol && guidcmp(handler->guid, protocol))
1068 continue;
1069 list_for_each_entry(item, &handler->open_infos, link) {
1070 if (item->info.attributes &
1071 EFI_OPEN_PROTOCOL_BY_DRIVER)
1072 ++count;
1073 }
1074 }
1075 /*
1076 * Create buffer. In case of duplicate driver assignments the buffer
1077 * will be too large. But that does not harm.
1078 */
1079 *number_of_drivers = 0;
1080 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1081 if (!*driver_handle_buffer)
1082 return EFI_OUT_OF_RESOURCES;
1083 /* Collect unique driver handles */
1084 list_for_each_entry(handler, &efiobj->protocols, link) {
1085 if (protocol && guidcmp(handler->guid, protocol))
1086 continue;
1087 list_for_each_entry(item, &handler->open_infos, link) {
1088 if (item->info.attributes &
1089 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1090 /* Check this is a new driver */
1091 duplicate = false;
1092 for (i = 0; i < *number_of_drivers; ++i) {
1093 if ((*driver_handle_buffer)[i] ==
1094 item->info.agent_handle)
1095 duplicate = true;
1096 }
1097 /* Copy handle to buffer */
1098 if (!duplicate) {
1099 i = (*number_of_drivers)++;
1100 (*driver_handle_buffer)[i] =
1101 item->info.agent_handle;
1102 }
1103 }
1104 }
1105 }
1106 return EFI_SUCCESS;
1107}
1108
1109/*
1110 * Disconnect all drivers from a controller.
1111 *
1112 * This function implements the DisconnectController service.
1113 * See the Unified Extensible Firmware Interface (UEFI) specification
1114 * for details.
1115 *
1116 * @efiobj handle of the controller
1117 * @protocol protocol guid (optional)
1118 * @child_handle handle of the child to destroy
1119 * @return status code
1120 */
1121static efi_status_t efi_disconnect_all_drivers(
1122 struct efi_object *efiobj,
1123 const efi_guid_t *protocol,
1124 efi_handle_t child_handle)
1125{
1126 efi_uintn_t number_of_drivers;
1127 efi_handle_t *driver_handle_buffer;
1128 efi_status_t r, ret;
1129
1130 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1131 &driver_handle_buffer);
1132 if (ret != EFI_SUCCESS)
1133 return ret;
1134
1135 ret = EFI_NOT_FOUND;
1136 while (number_of_drivers) {
1137 r = EFI_CALL(efi_disconnect_controller(
1138 efiobj->handle,
1139 driver_handle_buffer[--number_of_drivers],
1140 child_handle));
1141 if (r == EFI_SUCCESS)
1142 ret = r;
1143 }
1144 free(driver_handle_buffer);
1145 return ret;
1146}
1147
332468f7
HS
1148/*
1149 * Uninstall protocol interface.
1150 *
cd534083
HS
1151 * This function implements the UninstallProtocolInterface service.
1152 * See the Unified Extensible Firmware Interface (UEFI) specification
1153 * for details.
332468f7
HS
1154 *
1155 * @handle handle from which the protocol shall be removed
1156 * @protocol GUID of the protocol to be removed
1157 * @protocol_interface interface to be removed
1158 * @return status code
1159 */
cd534083 1160static efi_status_t EFIAPI efi_uninstall_protocol_interface(
2074f700 1161 efi_handle_t handle, const efi_guid_t *protocol,
cd534083 1162 void *protocol_interface)
bee91169 1163{
ad97373b 1164 struct efi_object *efiobj;
5810511d 1165 struct efi_handler *handler;
ad97373b
HS
1166 struct efi_open_protocol_info_item *item;
1167 struct efi_open_protocol_info_item *pos;
5810511d 1168 efi_status_t r;
4b6ed0d7 1169
cd534083
HS
1170 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1171
ad97373b
HS
1172 /* Check handle */
1173 efiobj = efi_search_obj(handle);
1174 if (!efiobj) {
4b6ed0d7
HS
1175 r = EFI_INVALID_PARAMETER;
1176 goto out;
1177 }
5810511d
HS
1178 /* Find the protocol on the handle */
1179 r = efi_search_protocol(handle, protocol, &handler);
1180 if (r != EFI_SUCCESS)
1181 goto out;
ad97373b
HS
1182 /* Disconnect controllers */
1183 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1184 if (!list_empty(&handler->open_infos)) {
5810511d 1185 r = EFI_ACCESS_DENIED;
ad97373b
HS
1186 goto out;
1187 }
1188 /* Close protocol */
1189 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1190 if (item->info.attributes ==
1191 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1192 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1193 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1194 list_del(&item->link);
1195 }
1196 if (!list_empty(&handler->open_infos)) {
1197 r = EFI_ACCESS_DENIED;
1198 goto out;
4b6ed0d7 1199 }
ad97373b 1200 r = efi_remove_protocol(handle, protocol, protocol_interface);
4b6ed0d7 1201out:
cd534083 1202 return EFI_EXIT(r);
bee91169
AG
1203}
1204
332468f7
HS
1205/*
1206 * Register an event for notification when a protocol is installed.
1207 *
1208 * This function implements the RegisterProtocolNotify service.
1209 * See the Unified Extensible Firmware Interface (UEFI) specification
1210 * for details.
1211 *
1212 * @protocol GUID of the protocol whose installation shall be
1213 * notified
1214 * @event event to be signaled upon installation of the protocol
1215 * @registration key for retrieving the registration information
1216 * @return status code
1217 */
5a9682d0
HS
1218static efi_status_t EFIAPI efi_register_protocol_notify(
1219 const efi_guid_t *protocol,
1220 struct efi_event *event,
1221 void **registration)
bee91169 1222{
778e6af8 1223 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
bee91169
AG
1224 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1225}
1226
332468f7
HS
1227/*
1228 * Determine if an EFI handle implements a protocol.
1229 *
1230 * See the documentation of the LocateHandle service in the UEFI specification.
1231 *
1232 * @search_type selection criterion
1233 * @protocol GUID of the protocol
1234 * @search_key registration key
1235 * @efiobj handle
1236 * @return 0 if the handle implements the protocol
1237 */
bee91169 1238static int efi_search(enum efi_locate_search_type search_type,
5a9682d0 1239 const efi_guid_t *protocol, void *search_key,
bee91169
AG
1240 struct efi_object *efiobj)
1241{
42cf1242 1242 efi_status_t ret;
bee91169
AG
1243
1244 switch (search_type) {
9f0770ff 1245 case ALL_HANDLES:
bee91169 1246 return 0;
9f0770ff 1247 case BY_REGISTER_NOTIFY:
42cf1242 1248 /* TODO: RegisterProtocolNotify is not implemented yet */
bee91169 1249 return -1;
9f0770ff 1250 case BY_PROTOCOL:
42cf1242
HS
1251 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1252 return (ret != EFI_SUCCESS);
1253 default:
1254 /* Invalid search type */
bee91169
AG
1255 return -1;
1256 }
bee91169
AG
1257}
1258
332468f7
HS
1259/*
1260 * Locate handles implementing a protocol.
1261 *
1262 * This function is meant for U-Boot internal calls. For the API implementation
1263 * of the LocateHandle service see efi_locate_handle_ext.
1264 *
1265 * @search_type selection criterion
1266 * @protocol GUID of the protocol
1267 * @search_key registration key
1268 * @buffer_size size of the buffer to receive the handles in bytes
1269 * @buffer buffer to receive the relevant handles
1270 * @return status code
1271 */
ebf199b9 1272static efi_status_t efi_locate_handle(
bee91169 1273 enum efi_locate_search_type search_type,
5a9682d0 1274 const efi_guid_t *protocol, void *search_key,
f5a2a938 1275 efi_uintn_t *buffer_size, efi_handle_t *buffer)
bee91169 1276{
caf864e4 1277 struct efi_object *efiobj;
f5a2a938 1278 efi_uintn_t size = 0;
bee91169 1279
caf864e4
HS
1280 /* Check parameters */
1281 switch (search_type) {
1282 case ALL_HANDLES:
1283 break;
1284 case BY_REGISTER_NOTIFY:
1285 if (!search_key)
1286 return EFI_INVALID_PARAMETER;
1287 /* RegisterProtocolNotify is not implemented yet */
1288 return EFI_UNSUPPORTED;
1289 case BY_PROTOCOL:
1290 if (!protocol)
1291 return EFI_INVALID_PARAMETER;
1292 break;
1293 default:
1294 return EFI_INVALID_PARAMETER;
1295 }
1296
1297 /*
1298 * efi_locate_handle_buffer uses this function for
1299 * the calculation of the necessary buffer size.
1300 * So do not require a buffer for buffersize == 0.
1301 */
1302 if (!buffer_size || (*buffer_size && !buffer))
1303 return EFI_INVALID_PARAMETER;
1304
bee91169 1305 /* Count how much space we need */
caf864e4
HS
1306 list_for_each_entry(efiobj, &efi_obj_list, link) {
1307 if (!efi_search(search_type, protocol, search_key, efiobj))
ab9efa97 1308 size += sizeof(void *);
bee91169
AG
1309 }
1310
1311 if (*buffer_size < size) {
1312 *buffer_size = size;
26329584 1313 return EFI_BUFFER_TOO_SMALL;
bee91169
AG
1314 }
1315
796a78cb
RC
1316 *buffer_size = size;
1317 if (size == 0)
1318 return EFI_NOT_FOUND;
1319
bee91169 1320 /* Then fill the array */
caf864e4
HS
1321 list_for_each_entry(efiobj, &efi_obj_list, link) {
1322 if (!efi_search(search_type, protocol, search_key, efiobj))
1323 *buffer++ = efiobj->handle;
bee91169
AG
1324 }
1325
26329584
HS
1326 return EFI_SUCCESS;
1327}
1328
332468f7
HS
1329/*
1330 * Locate handles implementing a protocol.
1331 *
1332 * This function implements the LocateHandle service.
1333 * See the Unified Extensible Firmware Interface (UEFI) specification
1334 * for details.
1335 *
1336 * @search_type selection criterion
1337 * @protocol GUID of the protocol
1338 * @search_key registration key
1339 * @buffer_size size of the buffer to receive the handles in bytes
1340 * @buffer buffer to receive the relevant handles
1341 * @return 0 if the handle implements the protocol
1342 */
26329584
HS
1343static efi_status_t EFIAPI efi_locate_handle_ext(
1344 enum efi_locate_search_type search_type,
5a9682d0 1345 const efi_guid_t *protocol, void *search_key,
f5a2a938 1346 efi_uintn_t *buffer_size, efi_handle_t *buffer)
26329584 1347{
778e6af8 1348 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
26329584
HS
1349 buffer_size, buffer);
1350
1351 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1352 buffer_size, buffer));
bee91169
AG
1353}
1354
d98cdf6a
AG
1355/* Collapses configuration table entries, removing index i */
1356static void efi_remove_configuration_table(int i)
1357{
1358 struct efi_configuration_table *this = &efi_conf_table[i];
ab9efa97 1359 struct efi_configuration_table *next = &efi_conf_table[i + 1];
d98cdf6a
AG
1360 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1361
1362 memmove(this, next, (ulong)end - (ulong)next);
1363 systab.nr_tables--;
1364}
1365
332468f7
HS
1366/*
1367 * Adds, updates, or removes a configuration table.
1368 *
1369 * This function is used for internal calls. For the API implementation of the
1370 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1371 *
1372 * @guid GUID of the installed table
1373 * @table table to be installed
1374 * @return status code
1375 */
ab9efa97
HS
1376efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1377 void *table)
bee91169 1378{
b095f3c8 1379 struct efi_event *evt;
bee91169
AG
1380 int i;
1381
eb68b4ef
HS
1382 if (!guid)
1383 return EFI_INVALID_PARAMETER;
1384
bee91169
AG
1385 /* Check for guid override */
1386 for (i = 0; i < systab.nr_tables; i++) {
1387 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
d98cdf6a
AG
1388 if (table)
1389 efi_conf_table[i].table = table;
1390 else
1391 efi_remove_configuration_table(i);
b095f3c8 1392 goto out;
bee91169
AG
1393 }
1394 }
1395
d98cdf6a
AG
1396 if (!table)
1397 return EFI_NOT_FOUND;
1398
bee91169
AG
1399 /* No override, check for overflow */
1400 if (i >= ARRAY_SIZE(efi_conf_table))
488bf12d 1401 return EFI_OUT_OF_RESOURCES;
bee91169
AG
1402
1403 /* Add a new entry */
1404 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1405 efi_conf_table[i].table = table;
aba5e919 1406 systab.nr_tables = i + 1;
bee91169 1407
b095f3c8
HS
1408out:
1409 /* Notify that the configuration table was changed */
1410 list_for_each_entry(evt, &efi_events, link) {
1411 if (evt->group && !guidcmp(evt->group, guid)) {
1412 efi_signal_event(evt, false);
1413 break;
1414 }
1415 }
1416
488bf12d
AG
1417 return EFI_SUCCESS;
1418}
1419
332468f7
HS
1420/*
1421 * Adds, updates, or removes a configuration table.
1422 *
1423 * This function implements the InstallConfigurationTable service.
1424 * See the Unified Extensible Firmware Interface (UEFI) specification
1425 * for details.
1426 *
1427 * @guid GUID of the installed table
1428 * @table table to be installed
1429 * @return status code
1430 */
488bf12d
AG
1431static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1432 void *table)
1433{
778e6af8 1434 EFI_ENTRY("%pUl, %p", guid, table);
488bf12d 1435 return EFI_EXIT(efi_install_configuration_table(guid, table));
bee91169
AG
1436}
1437
332468f7
HS
1438/*
1439 * Initialize a loaded_image_info + loaded_image_info object with correct
95c5553e 1440 * protocols, boot-device, etc.
332468f7 1441 *
10a08c4e 1442 * @info loaded image info to be passed to the entry point of the
332468f7
HS
1443 * image
1444 * @obj internal object associated with the loaded image
1445 * @device_path device path of the loaded image
1446 * @file_path file path of the loaded image
56d92888 1447 * @return status code
95c5553e 1448 */
56d92888
HS
1449efi_status_t efi_setup_loaded_image(
1450 struct efi_loaded_image *info, struct efi_object *obj,
1451 struct efi_device_path *device_path,
1452 struct efi_device_path *file_path)
95c5553e 1453{
48b66230
HS
1454 efi_status_t ret;
1455
44549d62
HS
1456 /* Add internal object to object list */
1457 efi_add_handle(obj);
1458 /* efi_exit() assumes that the handle points to the info */
95c5553e
RC
1459 obj->handle = info;
1460
48b66230 1461 info->file_path = file_path;
48b66230 1462
7df5af6f
HS
1463 if (device_path) {
1464 info->device_handle = efi_dp_find_obj(device_path, NULL);
1465 /*
1466 * When asking for the device path interface, return
1467 * bootefi_device_path
1468 */
1469 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1470 device_path);
1471 if (ret != EFI_SUCCESS)
1472 goto failure;
1473 }
95c5553e
RC
1474
1475 /*
1476 * When asking for the loaded_image interface, just
1477 * return handle which points to loaded_image_info
1478 */
48b66230
HS
1479 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1480 if (ret != EFI_SUCCESS)
1481 goto failure;
95c5553e 1482
48b66230
HS
1483 ret = efi_add_protocol(obj->handle,
1484 &efi_guid_device_path_to_text_protocol,
1485 (void *)&efi_device_path_to_text);
1486 if (ret != EFI_SUCCESS)
1487 goto failure;
95c5553e 1488
e70f8dfa
LL
1489 ret = efi_add_protocol(obj->handle,
1490 &efi_guid_device_path_utilities_protocol,
1491 (void *)&efi_device_path_utilities);
1492 if (ret != EFI_SUCCESS)
1493 goto failure;
1494
56d92888 1495 return ret;
48b66230
HS
1496failure:
1497 printf("ERROR: Failure to install protocols for loaded image\n");
56d92888 1498 return ret;
95c5553e
RC
1499}
1500
332468f7
HS
1501/*
1502 * Load an image using a file path.
1503 *
1504 * @file_path the path of the image to load
1505 * @buffer buffer containing the loaded image
10a08c4e 1506 * @return status code
332468f7 1507 */
9975fe96
RC
1508efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1509 void **buffer)
838ee4b4
RC
1510{
1511 struct efi_file_info *info = NULL;
1512 struct efi_file_handle *f;
1513 static efi_status_t ret;
b6dd5777 1514 efi_uintn_t bs;
838ee4b4
RC
1515
1516 f = efi_file_from_path(file_path);
1517 if (!f)
1518 return EFI_DEVICE_ERROR;
1519
1520 bs = 0;
1521 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1522 &bs, info));
1523 if (ret == EFI_BUFFER_TOO_SMALL) {
1524 info = malloc(bs);
1525 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1526 &bs, info));
1527 }
1528 if (ret != EFI_SUCCESS)
1529 goto error;
1530
1531 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1532 if (ret)
1533 goto error;
1534
b6dd5777
HS
1535 bs = info->file_size;
1536 EFI_CALL(ret = f->read(f, &bs, *buffer));
838ee4b4
RC
1537
1538error:
1539 free(info);
1540 EFI_CALL(f->close(f));
1541
1542 if (ret != EFI_SUCCESS) {
1543 efi_free_pool(*buffer);
1544 *buffer = NULL;
1545 }
1546
1547 return ret;
1548}
1549
332468f7
HS
1550/*
1551 * Load an EFI image into memory.
1552 *
1553 * This function implements the LoadImage service.
1554 * See the Unified Extensible Firmware Interface (UEFI) specification
1555 * for details.
1556 *
1557 * @boot_policy true for request originating from the boot manager
c8df80c5 1558 * @parent_image the caller's image handle
332468f7
HS
1559 * @file_path the path of the image to load
1560 * @source_buffer memory location from which the image is installed
1561 * @source_size size of the memory area from which the image is
1562 * installed
1563 * @image_handle handle for the newly installed image
1564 * @return status code
1565 */
bee91169
AG
1566static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1567 efi_handle_t parent_image,
1568 struct efi_device_path *file_path,
1569 void *source_buffer,
7fb96a10 1570 efi_uintn_t source_size,
bee91169
AG
1571 efi_handle_t *image_handle)
1572{
bee91169
AG
1573 struct efi_loaded_image *info;
1574 struct efi_object *obj;
b9b17598 1575 efi_status_t ret;
bee91169 1576
7fb96a10 1577 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
bee91169 1578 file_path, source_buffer, source_size, image_handle);
838ee4b4 1579
28a4fd46
HS
1580 if (!image_handle || !parent_image) {
1581 ret = EFI_INVALID_PARAMETER;
1582 goto error;
1583 }
1584
1585 if (!source_buffer && !file_path) {
1586 ret = EFI_NOT_FOUND;
1587 goto error;
1588 }
1589
838ee4b4 1590 info = calloc(1, sizeof(*info));
28a4fd46
HS
1591 if (!info) {
1592 ret = EFI_OUT_OF_RESOURCES;
1593 goto error;
1594 }
838ee4b4 1595 obj = calloc(1, sizeof(*obj));
28a4fd46
HS
1596 if (!obj) {
1597 free(info);
1598 ret = EFI_OUT_OF_RESOURCES;
1599 goto error;
1600 }
838ee4b4
RC
1601
1602 if (!source_buffer) {
1603 struct efi_device_path *dp, *fp;
838ee4b4 1604
9975fe96 1605 ret = efi_load_image_from_path(file_path, &source_buffer);
b9b17598
HS
1606 if (ret != EFI_SUCCESS)
1607 goto failure;
838ee4b4
RC
1608 /*
1609 * split file_path which contains both the device and
1610 * file parts:
1611 */
1612 efi_dp_split_file_path(file_path, &dp, &fp);
b9b17598
HS
1613 ret = efi_setup_loaded_image(info, obj, dp, fp);
1614 if (ret != EFI_SUCCESS)
1615 goto failure;
838ee4b4
RC
1616 } else {
1617 /* In this case, file_path is the "device" path, ie.
1618 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1619 */
b9b17598
HS
1620 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1621 if (ret != EFI_SUCCESS)
1622 goto failure;
838ee4b4 1623 }
bee91169
AG
1624 info->reserved = efi_load_pe(source_buffer, info);
1625 if (!info->reserved) {
b9b17598
HS
1626 ret = EFI_UNSUPPORTED;
1627 goto failure;
bee91169 1628 }
32fc2ac3
HS
1629 info->system_table = &systab;
1630 info->parent_handle = parent_image;
ea54ad59 1631 *image_handle = obj->handle;
bee91169 1632 return EFI_EXIT(EFI_SUCCESS);
b9b17598
HS
1633failure:
1634 free(info);
1635 efi_delete_handle(obj);
28a4fd46 1636error:
b9b17598 1637 return EFI_EXIT(ret);
bee91169
AG
1638}
1639
332468f7
HS
1640/*
1641 * Call the entry point of an image.
1642 *
1643 * This function implements the StartImage service.
1644 * See the Unified Extensible Firmware Interface (UEFI) specification
1645 * for details.
1646 *
1647 * @image_handle handle of the image
1648 * @exit_data_size size of the buffer
1649 * @exit_data buffer to receive the exit data of the called image
10a08c4e 1650 * @return status code
332468f7 1651 */
bee91169
AG
1652static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1653 unsigned long *exit_data_size,
1654 s16 **exit_data)
1655{
c6fa5df6
AG
1656 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1657 struct efi_system_table *st);
bee91169 1658 struct efi_loaded_image *info = image_handle;
727a1afb 1659 efi_status_t ret;
bee91169
AG
1660
1661 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1662 entry = info->reserved;
1663
1664 efi_is_direct_boot = false;
1665
1666 /* call the image! */
a86aeaf2 1667 if (setjmp(&info->exit_jmp)) {
727a1afb
HS
1668 /*
1669 * We called the entry point of the child image with EFI_CALL
1670 * in the lines below. The child image called the Exit() boot
1671 * service efi_exit() which executed the long jump that brought
1672 * us to the current line. This implies that the second half
1673 * of the EFI_CALL macro has not been executed.
1674 */
1675#ifdef CONFIG_ARM
1676 /*
1677 * efi_exit() called efi_restore_gd(). We have to undo this
1678 * otherwise __efi_entry_check() will put the wrong value into
1679 * app_gd.
1680 */
1681 gd = app_gd;
1682#endif
1683 /*
1684 * To get ready to call EFI_EXIT below we have to execute the
1685 * missed out steps of EFI_CALL.
1686 */
1687 assert(__efi_entry_check());
1688 debug("%sEFI: %lu returned by started image\n",
1689 __efi_nesting_dec(),
1690 (unsigned long)((uintptr_t)info->exit_status &
1691 ~EFI_ERROR_MASK));
a86aeaf2
AG
1692 return EFI_EXIT(info->exit_status);
1693 }
1694
727a1afb 1695 ret = EFI_CALL(entry(image_handle, &systab));
bee91169 1696
56672bf5
AG
1697 /*
1698 * Usually UEFI applications call Exit() instead of returning.
1699 * But because the world doesn not consist of ponies and unicorns,
1700 * we're happy to emulate that behavior on behalf of a payload
1701 * that forgot.
1702 */
1703 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
bee91169
AG
1704}
1705
332468f7
HS
1706/*
1707 * Leave an EFI application or driver.
1708 *
1709 * This function implements the Exit service.
1710 * See the Unified Extensible Firmware Interface (UEFI) specification
1711 * for details.
1712 *
1713 * @image_handle handle of the application or driver that is exiting
1714 * @exit_status status code
1715 * @exit_data_size size of the buffer in bytes
1716 * @exit_data buffer with data describing an error
10a08c4e 1717 * @return status code
332468f7 1718 */
a86aeaf2 1719static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
ab9efa97
HS
1720 efi_status_t exit_status,
1721 unsigned long exit_data_size,
1722 int16_t *exit_data)
bee91169 1723{
44549d62
HS
1724 /*
1725 * We require that the handle points to the original loaded
1726 * image protocol interface.
1727 *
1728 * For getting the longjmp address this is safer than locating
1729 * the protocol because the protocol may have been reinstalled
1730 * pointing to another memory location.
1731 *
1732 * TODO: We should call the unload procedure of the loaded
1733 * image protocol.
1734 */
ab9efa97 1735 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
a86aeaf2 1736
bee91169
AG
1737 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1738 exit_data_size, exit_data);
a86aeaf2 1739
a148920e 1740 /* Make sure entry/exit counts for EFI world cross-overs match */
727a1afb 1741 EFI_EXIT(exit_status);
da94073b 1742
a148920e
AG
1743 /*
1744 * But longjmp out with the U-Boot gd, not the application's, as
1745 * the other end is a setjmp call inside EFI context.
1746 */
1747 efi_restore_gd();
1748
a86aeaf2 1749 loaded_image_info->exit_status = exit_status;
692fcdd8 1750 longjmp(&loaded_image_info->exit_jmp, 1);
a86aeaf2
AG
1751
1752 panic("EFI application exited");
bee91169
AG
1753}
1754
332468f7
HS
1755/*
1756 * Unload an EFI image.
1757 *
1758 * This function implements the UnloadImage service.
1759 * See the Unified Extensible Firmware Interface (UEFI) specification
1760 * for details.
1761 *
1762 * @image_handle handle of the image to be unloaded
1763 * @return status code
1764 */
2074f700 1765static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
bee91169
AG
1766{
1767 struct efi_object *efiobj;
1768
1769 EFI_ENTRY("%p", image_handle);
1770 efiobj = efi_search_obj(image_handle);
1771 if (efiobj)
1772 list_del(&efiobj->link);
1773
1774 return EFI_EXIT(EFI_SUCCESS);
1775}
1776
332468f7
HS
1777/*
1778 * Fix up caches for EFI payloads if necessary.
1779 */
bee91169
AG
1780static void efi_exit_caches(void)
1781{
1782#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1783 /*
1784 * Grub on 32bit ARM needs to have caches disabled before jumping into
1785 * a zImage, but does not know of all cache layers. Give it a hand.
1786 */
1787 if (efi_is_direct_boot)
1788 cleanup_before_linux();
1789#endif
1790}
1791
332468f7 1792/*
cc20ed03 1793 * Stop all boot services.
332468f7
HS
1794 *
1795 * This function implements the ExitBootServices service.
1796 * See the Unified Extensible Firmware Interface (UEFI) specification
1797 * for details.
1798 *
cc20ed03
HS
1799 * All timer events are disabled.
1800 * For exit boot services events the notification function is called.
1801 * The boot services are disabled in the system table.
1802 *
332468f7
HS
1803 * @image_handle handle of the loaded image
1804 * @map_key key of the memory map
1805 * @return status code
1806 */
2074f700 1807static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
bee91169
AG
1808 unsigned long map_key)
1809{
43bce442 1810 struct efi_event *evt;
152a263c 1811
bee91169
AG
1812 EFI_ENTRY("%p, %ld", image_handle, map_key);
1813
cc20ed03
HS
1814 /* Make sure that notification functions are not called anymore */
1815 efi_tpl = TPL_HIGH_LEVEL;
1816
1817 /* Check if ExitBootServices has already been called */
1818 if (!systab.boottime)
1819 return EFI_EXIT(EFI_SUCCESS);
1820
b095f3c8
HS
1821 /* Add related events to the event group */
1822 list_for_each_entry(evt, &efi_events, link) {
1823 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1824 evt->group = &efi_guid_event_group_exit_boot_services;
1825 }
152a263c 1826 /* Notify that ExitBootServices is invoked. */
43bce442 1827 list_for_each_entry(evt, &efi_events, link) {
b095f3c8
HS
1828 if (evt->group &&
1829 !guidcmp(evt->group,
1830 &efi_guid_event_group_exit_boot_services)) {
1831 efi_signal_event(evt, false);
1832 break;
1833 }
152a263c 1834 }
152a263c 1835
cc20ed03 1836 /* TODO Should persist EFI variables here */
ad644e7c 1837
b7b8410a
AG
1838 board_quiesce_devices();
1839
bee91169
AG
1840 /* Fix up caches for EFI payloads if necessary */
1841 efi_exit_caches();
1842
1843 /* This stops all lingering devices */
1844 bootm_disable_interrupts();
1845
cc20ed03
HS
1846 /* Disable boottime services */
1847 systab.con_in_handle = NULL;
1848 systab.con_in = NULL;
1849 systab.con_out_handle = NULL;
1850 systab.con_out = NULL;
1851 systab.stderr_handle = NULL;
1852 systab.std_err = NULL;
1853 systab.boottime = NULL;
1854
1855 /* Recalculate CRC32 */
1856 systab.hdr.crc32 = 0;
1857 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1858 sizeof(struct efi_system_table));
1859
bee91169 1860 /* Give the payload some time to boot */
b3d60900 1861 efi_set_watchdog(0);
bee91169
AG
1862 WATCHDOG_RESET();
1863
1864 return EFI_EXIT(EFI_SUCCESS);
1865}
1866
332468f7
HS
1867/*
1868 * Get next value of the counter.
1869 *
1870 * This function implements the NextMonotonicCount service.
1871 * See the Unified Extensible Firmware Interface (UEFI) specification
1872 * for details.
1873 *
1874 * @count returned value of the counter
1875 * @return status code
1876 */
bee91169
AG
1877static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1878{
ab9efa97
HS
1879 static uint64_t mono;
1880
bee91169
AG
1881 EFI_ENTRY("%p", count);
1882 *count = mono++;
1883 return EFI_EXIT(EFI_SUCCESS);
1884}
1885
332468f7
HS
1886/*
1887 * Sleep.
1888 *
1889 * This function implements the Stall sercive.
1890 * See the Unified Extensible Firmware Interface (UEFI) specification
1891 * for details.
1892 *
1893 * @microseconds period to sleep in microseconds
1894 * @return status code
1895 */
bee91169
AG
1896static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1897{
1898 EFI_ENTRY("%ld", microseconds);
1899 udelay(microseconds);
1900 return EFI_EXIT(EFI_SUCCESS);
1901}
1902
332468f7
HS
1903/*
1904 * Reset the watchdog timer.
1905 *
b3d60900 1906 * This function implements the SetWatchdogTimer service.
332468f7
HS
1907 * See the Unified Extensible Firmware Interface (UEFI) specification
1908 * for details.
1909 *
1910 * @timeout seconds before reset by watchdog
1911 * @watchdog_code code to be logged when resetting
1912 * @data_size size of buffer in bytes
1913 * @watchdog_data buffer with data describing the reset reason
10a08c4e 1914 * @return status code
332468f7 1915 */
bee91169
AG
1916static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1917 uint64_t watchdog_code,
1918 unsigned long data_size,
1919 uint16_t *watchdog_data)
1920{
ab9efa97 1921 EFI_ENTRY("%ld, 0x%" PRIx64 ", %ld, %p", timeout, watchdog_code,
bee91169 1922 data_size, watchdog_data);
b3d60900 1923 return EFI_EXIT(efi_set_watchdog(timeout));
bee91169
AG
1924}
1925
332468f7
HS
1926/*
1927 * Close a protocol.
1928 *
1929 * This function implements the CloseProtocol service.
1930 * See the Unified Extensible Firmware Interface (UEFI) specification
1931 * for details.
1932 *
1933 * @handle handle on which the protocol shall be closed
1934 * @protocol GUID of the protocol to close
1935 * @agent_handle handle of the driver
1936 * @controller_handle handle of the controller
1937 * @return status code
1938 */
2074f700 1939static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
5a9682d0 1940 const efi_guid_t *protocol,
2074f700
HS
1941 efi_handle_t agent_handle,
1942 efi_handle_t controller_handle)
bee91169 1943{
3b8a489c
HS
1944 struct efi_handler *handler;
1945 struct efi_open_protocol_info_item *item;
1946 struct efi_open_protocol_info_item *pos;
1947 efi_status_t r;
1948
778e6af8 1949 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
bee91169 1950 controller_handle);
3b8a489c
HS
1951
1952 if (!agent_handle) {
1953 r = EFI_INVALID_PARAMETER;
1954 goto out;
1955 }
1956 r = efi_search_protocol(handle, protocol, &handler);
1957 if (r != EFI_SUCCESS)
1958 goto out;
1959
1960 r = EFI_NOT_FOUND;
1961 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1962 if (item->info.agent_handle == agent_handle &&
1963 item->info.controller_handle == controller_handle) {
1964 efi_delete_open_info(item);
1965 r = EFI_SUCCESS;
1966 break;
1967 }
1968 }
1969out:
1970 return EFI_EXIT(r);
bee91169
AG
1971}
1972
332468f7
HS
1973/*
1974 * Provide information about then open status of a protocol on a handle
1975 *
1976 * This function implements the OpenProtocolInformation service.
1977 * See the Unified Extensible Firmware Interface (UEFI) specification
1978 * for details.
1979 *
1980 * @handle handle for which the information shall be retrieved
1981 * @protocol GUID of the protocol
1982 * @entry_buffer buffer to receive the open protocol information
1983 * @entry_count number of entries available in the buffer
1984 * @return status code
1985 */
ab9efa97
HS
1986static efi_status_t EFIAPI efi_open_protocol_information(
1987 efi_handle_t handle, const efi_guid_t *protocol,
bee91169 1988 struct efi_open_protocol_info_entry **entry_buffer,
f5a2a938 1989 efi_uintn_t *entry_count)
bee91169 1990{
e3fbbc36
HS
1991 unsigned long buffer_size;
1992 unsigned long count;
1993 struct efi_handler *handler;
1994 struct efi_open_protocol_info_item *item;
1995 efi_status_t r;
1996
778e6af8 1997 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
bee91169 1998 entry_count);
e3fbbc36
HS
1999
2000 /* Check parameters */
2001 if (!entry_buffer) {
2002 r = EFI_INVALID_PARAMETER;
2003 goto out;
2004 }
2005 r = efi_search_protocol(handle, protocol, &handler);
2006 if (r != EFI_SUCCESS)
2007 goto out;
2008
2009 /* Count entries */
2010 count = 0;
2011 list_for_each_entry(item, &handler->open_infos, link) {
2012 if (item->info.open_count)
2013 ++count;
2014 }
2015 *entry_count = count;
2016 *entry_buffer = NULL;
2017 if (!count) {
2018 r = EFI_SUCCESS;
2019 goto out;
2020 }
2021
2022 /* Copy entries */
2023 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2024 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2025 (void **)entry_buffer);
2026 if (r != EFI_SUCCESS)
2027 goto out;
2028 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2029 if (item->info.open_count)
2030 (*entry_buffer)[--count] = item->info;
2031 }
2032out:
2033 return EFI_EXIT(r);
bee91169
AG
2034}
2035
332468f7
HS
2036/*
2037 * Get protocols installed on a handle.
2038 *
2039 * This function implements the ProtocolsPerHandleService.
2040 * See the Unified Extensible Firmware Interface (UEFI) specification
2041 * for details.
2042 *
2043 * @handle handle for which the information is retrieved
2044 * @protocol_buffer buffer with protocol GUIDs
2045 * @protocol_buffer_count number of entries in the buffer
10a08c4e 2046 * @return status code
332468f7 2047 */
2074f700
HS
2048static efi_status_t EFIAPI efi_protocols_per_handle(
2049 efi_handle_t handle, efi_guid_t ***protocol_buffer,
f5a2a938 2050 efi_uintn_t *protocol_buffer_count)
bee91169 2051{
c0ebfc86
HS
2052 unsigned long buffer_size;
2053 struct efi_object *efiobj;
69fb6b1a 2054 struct list_head *protocol_handle;
c0ebfc86
HS
2055 efi_status_t r;
2056
bee91169
AG
2057 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2058 protocol_buffer_count);
c0ebfc86
HS
2059
2060 if (!handle || !protocol_buffer || !protocol_buffer_count)
2061 return EFI_EXIT(EFI_INVALID_PARAMETER);
2062
2063 *protocol_buffer = NULL;
661c8327 2064 *protocol_buffer_count = 0;
c0ebfc86 2065
69fb6b1a
HS
2066 efiobj = efi_search_obj(handle);
2067 if (!efiobj)
2068 return EFI_EXIT(EFI_INVALID_PARAMETER);
c0ebfc86 2069
69fb6b1a
HS
2070 /* Count protocols */
2071 list_for_each(protocol_handle, &efiobj->protocols) {
2072 ++*protocol_buffer_count;
2073 }
2074
2075 /* Copy guids */
2076 if (*protocol_buffer_count) {
2077 size_t j = 0;
2078
2079 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2080 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2081 (void **)protocol_buffer);
2082 if (r != EFI_SUCCESS)
2083 return EFI_EXIT(r);
2084 list_for_each(protocol_handle, &efiobj->protocols) {
2085 struct efi_handler *protocol;
2086
2087 protocol = list_entry(protocol_handle,
2088 struct efi_handler, link);
2089 (*protocol_buffer)[j] = (void *)protocol->guid;
2090 ++j;
c0ebfc86 2091 }
c0ebfc86
HS
2092 }
2093
2094 return EFI_EXIT(EFI_SUCCESS);
bee91169
AG
2095}
2096
332468f7
HS
2097/*
2098 * Locate handles implementing a protocol.
2099 *
2100 * This function implements the LocateHandleBuffer service.
2101 * See the Unified Extensible Firmware Interface (UEFI) specification
2102 * for details.
2103 *
2104 * @search_type selection criterion
2105 * @protocol GUID of the protocol
2106 * @search_key registration key
2107 * @no_handles number of returned handles
2108 * @buffer buffer with the returned handles
2109 * @return status code
2110 */
bee91169
AG
2111static efi_status_t EFIAPI efi_locate_handle_buffer(
2112 enum efi_locate_search_type search_type,
5a9682d0 2113 const efi_guid_t *protocol, void *search_key,
f5a2a938 2114 efi_uintn_t *no_handles, efi_handle_t **buffer)
bee91169 2115{
c2e703f9 2116 efi_status_t r;
f5a2a938 2117 efi_uintn_t buffer_size = 0;
c2e703f9 2118
778e6af8 2119 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
bee91169 2120 no_handles, buffer);
c2e703f9
HS
2121
2122 if (!no_handles || !buffer) {
2123 r = EFI_INVALID_PARAMETER;
2124 goto out;
2125 }
2126 *no_handles = 0;
2127 *buffer = NULL;
2128 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2129 *buffer);
2130 if (r != EFI_BUFFER_TOO_SMALL)
2131 goto out;
2132 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2133 (void **)buffer);
2134 if (r != EFI_SUCCESS)
2135 goto out;
2136 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2137 *buffer);
2138 if (r == EFI_SUCCESS)
2074f700 2139 *no_handles = buffer_size / sizeof(efi_handle_t);
c2e703f9
HS
2140out:
2141 return EFI_EXIT(r);
bee91169
AG
2142}
2143
332468f7
HS
2144/*
2145 * Find an interface implementing a protocol.
2146 *
2147 * This function implements the LocateProtocol service.
2148 * See the Unified Extensible Firmware Interface (UEFI) specification
2149 * for details.
2150 *
2151 * @protocol GUID of the protocol
2152 * @registration registration key passed to the notification function
2153 * @protocol_interface interface implementing the protocol
10a08c4e 2154 * @return status code
332468f7 2155 */
5a9682d0 2156static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
bee91169
AG
2157 void *registration,
2158 void **protocol_interface)
2159{
88adae5e 2160 struct list_head *lhandle;
9172cd91 2161 efi_status_t ret;
bee91169 2162
778e6af8 2163 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
88adae5e
HS
2164
2165 if (!protocol || !protocol_interface)
2166 return EFI_EXIT(EFI_INVALID_PARAMETER);
2167
2168 list_for_each(lhandle, &efi_obj_list) {
2169 struct efi_object *efiobj;
9172cd91 2170 struct efi_handler *handler;
88adae5e
HS
2171
2172 efiobj = list_entry(lhandle, struct efi_object, link);
9172cd91
HS
2173
2174 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2175 if (ret == EFI_SUCCESS) {
2176 *protocol_interface = handler->protocol_interface;
2177 return EFI_EXIT(EFI_SUCCESS);
bee91169
AG
2178 }
2179 }
88adae5e 2180 *protocol_interface = NULL;
bee91169
AG
2181
2182 return EFI_EXIT(EFI_NOT_FOUND);
2183}
2184
ae2c85c1
HS
2185/*
2186 * Get the device path and handle of an device implementing a protocol.
2187 *
2188 * This function implements the LocateDevicePath service.
2189 * See the Unified Extensible Firmware Interface (UEFI) specification
2190 * for details.
2191 *
2192 * @protocol GUID of the protocol
2193 * @device_path device path
2194 * @device handle of the device
2195 * @return status code
2196 */
2197static efi_status_t EFIAPI efi_locate_device_path(
2198 const efi_guid_t *protocol,
2199 struct efi_device_path **device_path,
2200 efi_handle_t *device)
2201{
2202 struct efi_device_path *dp;
2203 size_t i;
2204 struct efi_handler *handler;
2205 efi_handle_t *handles;
2206 size_t len, len_dp;
2207 size_t len_best = 0;
2208 efi_uintn_t no_handles;
2209 u8 *remainder;
2210 efi_status_t ret;
2211
2212 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2213
2214 if (!protocol || !device_path || !*device_path || !device) {
2215 ret = EFI_INVALID_PARAMETER;
2216 goto out;
2217 }
2218
2219 /* Find end of device path */
f6dd3f35 2220 len = efi_dp_instance_size(*device_path);
ae2c85c1
HS
2221
2222 /* Get all handles implementing the protocol */
2223 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2224 &no_handles, &handles));
2225 if (ret != EFI_SUCCESS)
2226 goto out;
2227
2228 for (i = 0; i < no_handles; ++i) {
2229 /* Find the device path protocol */
2230 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2231 &handler);
2232 if (ret != EFI_SUCCESS)
2233 continue;
2234 dp = (struct efi_device_path *)handler->protocol_interface;
f6dd3f35 2235 len_dp = efi_dp_instance_size(dp);
ae2c85c1
HS
2236 /*
2237 * This handle can only be a better fit
2238 * if its device path length is longer than the best fit and
2239 * if its device path length is shorter of equal the searched
2240 * device path.
2241 */
2242 if (len_dp <= len_best || len_dp > len)
2243 continue;
2244 /* Check if dp is a subpath of device_path */
2245 if (memcmp(*device_path, dp, len_dp))
2246 continue;
2247 *device = handles[i];
2248 len_best = len_dp;
2249 }
2250 if (len_best) {
2251 remainder = (u8 *)*device_path + len_best;
2252 *device_path = (struct efi_device_path *)remainder;
2253 ret = EFI_SUCCESS;
2254 } else {
2255 ret = EFI_NOT_FOUND;
2256 }
2257out:
2258 return EFI_EXIT(ret);
2259}
2260
332468f7
HS
2261/*
2262 * Install multiple protocol interfaces.
2263 *
2264 * This function implements the MultipleProtocolInterfaces service.
2265 * See the Unified Extensible Firmware Interface (UEFI) specification
2266 * for details.
2267 *
2268 * @handle handle on which the protocol interfaces shall be installed
2269 * @... NULL terminated argument list with pairs of protocol GUIDS and
2270 * interfaces
2271 * @return status code
2272 */
bee91169
AG
2273static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2274 void **handle, ...)
2275{
2276 EFI_ENTRY("%p", handle);
58b83586
HS
2277
2278 va_list argptr;
5a9682d0 2279 const efi_guid_t *protocol;
58b83586
HS
2280 void *protocol_interface;
2281 efi_status_t r = EFI_SUCCESS;
2282 int i = 0;
2283
2284 if (!handle)
2285 return EFI_EXIT(EFI_INVALID_PARAMETER);
2286
2287 va_start(argptr, handle);
2288 for (;;) {
2289 protocol = va_arg(argptr, efi_guid_t*);
2290 if (!protocol)
2291 break;
2292 protocol_interface = va_arg(argptr, void*);
1760ef57
HS
2293 r = EFI_CALL(efi_install_protocol_interface(
2294 handle, protocol,
2295 EFI_NATIVE_INTERFACE,
2296 protocol_interface));
58b83586
HS
2297 if (r != EFI_SUCCESS)
2298 break;
2299 i++;
2300 }
2301 va_end(argptr);
2302 if (r == EFI_SUCCESS)
2303 return EFI_EXIT(r);
2304
62471e46 2305 /* If an error occurred undo all changes. */
58b83586
HS
2306 va_start(argptr, handle);
2307 for (; i; --i) {
2308 protocol = va_arg(argptr, efi_guid_t*);
2309 protocol_interface = va_arg(argptr, void*);
cd534083
HS
2310 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2311 protocol_interface));
58b83586
HS
2312 }
2313 va_end(argptr);
2314
2315 return EFI_EXIT(r);
bee91169
AG
2316}
2317
332468f7
HS
2318/*
2319 * Uninstall multiple protocol interfaces.
2320 *
2321 * This function implements the UninstallMultipleProtocolInterfaces service.
2322 * See the Unified Extensible Firmware Interface (UEFI) specification
2323 * for details.
2324 *
2325 * @handle handle from which the protocol interfaces shall be removed
2326 * @... NULL terminated argument list with pairs of protocol GUIDS and
2327 * interfaces
2328 * @return status code
2329 */
bee91169
AG
2330static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2331 void *handle, ...)
2332{
2333 EFI_ENTRY("%p", handle);
843ce54c
HS
2334
2335 va_list argptr;
2336 const efi_guid_t *protocol;
2337 void *protocol_interface;
2338 efi_status_t r = EFI_SUCCESS;
2339 size_t i = 0;
2340
2341 if (!handle)
2342 return EFI_EXIT(EFI_INVALID_PARAMETER);
2343
2344 va_start(argptr, handle);
2345 for (;;) {
2346 protocol = va_arg(argptr, efi_guid_t*);
2347 if (!protocol)
2348 break;
2349 protocol_interface = va_arg(argptr, void*);
2350 r = EFI_CALL(efi_uninstall_protocol_interface(
2351 handle, protocol,
2352 protocol_interface));
2353 if (r != EFI_SUCCESS)
2354 break;
2355 i++;
2356 }
2357 va_end(argptr);
2358 if (r == EFI_SUCCESS)
2359 return EFI_EXIT(r);
2360
2361 /* If an error occurred undo all changes. */
2362 va_start(argptr, handle);
2363 for (; i; --i) {
2364 protocol = va_arg(argptr, efi_guid_t*);
2365 protocol_interface = va_arg(argptr, void*);
2366 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2367 EFI_NATIVE_INTERFACE,
2368 protocol_interface));
2369 }
2370 va_end(argptr);
2371
2372 return EFI_EXIT(r);
bee91169
AG
2373}
2374
332468f7
HS
2375/*
2376 * Calculate cyclic redundancy code.
2377 *
2378 * This function implements the CalculateCrc32 service.
2379 * See the Unified Extensible Firmware Interface (UEFI) specification
2380 * for details.
2381 *
2382 * @data buffer with data
2383 * @data_size size of buffer in bytes
2384 * @crc32_p cyclic redundancy code
2385 * @return status code
2386 */
bee91169
AG
2387static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2388 unsigned long data_size,
2389 uint32_t *crc32_p)
2390{
2391 EFI_ENTRY("%p, %ld", data, data_size);
2392 *crc32_p = crc32(0, data, data_size);
2393 return EFI_EXIT(EFI_SUCCESS);
2394}
2395
332468f7
HS
2396/*
2397 * Copy memory.
2398 *
2399 * This function implements the CopyMem service.
2400 * See the Unified Extensible Firmware Interface (UEFI) specification
2401 * for details.
2402 *
2403 * @destination destination of the copy operation
2404 * @source source of the copy operation
2405 * @length number of bytes to copy
2406 */
fc05a959
HS
2407static void EFIAPI efi_copy_mem(void *destination, const void *source,
2408 size_t length)
bee91169 2409{
fc05a959 2410 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
bee91169 2411 memcpy(destination, source, length);
f7c78176 2412 EFI_EXIT(EFI_SUCCESS);
bee91169
AG
2413}
2414
332468f7
HS
2415/*
2416 * Fill memory with a byte value.
2417 *
2418 * This function implements the SetMem service.
2419 * See the Unified Extensible Firmware Interface (UEFI) specification
2420 * for details.
2421 *
2422 * @buffer buffer to fill
2423 * @size size of buffer in bytes
2424 * @value byte to copy to the buffer
2425 */
fc05a959 2426static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
bee91169 2427{
fc05a959 2428 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
bee91169 2429 memset(buffer, value, size);
f7c78176 2430 EFI_EXIT(EFI_SUCCESS);
bee91169
AG
2431}
2432
191a41cc
HS
2433/*
2434 * Open protocol interface on a handle.
2435 *
2436 * @handler handler of a protocol
2437 * @protocol_interface interface implementing the protocol
2438 * @agent_handle handle of the driver
2439 * @controller_handle handle of the controller
2440 * @attributes attributes indicating how to open the protocol
2441 * @return status code
2442 */
2443static efi_status_t efi_protocol_open(
2444 struct efi_handler *handler,
2445 void **protocol_interface, void *agent_handle,
2446 void *controller_handle, uint32_t attributes)
2447{
2448 struct efi_open_protocol_info_item *item;
2449 struct efi_open_protocol_info_entry *match = NULL;
2450 bool opened_by_driver = false;
2451 bool opened_exclusive = false;
2452
2453 /* If there is no agent, only return the interface */
2454 if (!agent_handle)
2455 goto out;
2456
2457 /* For TEST_PROTOCOL ignore interface attribute */
2458 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2459 *protocol_interface = NULL;
2460
2461 /*
2462 * Check if the protocol is already opened by a driver with the same
2463 * attributes or opened exclusively
2464 */
2465 list_for_each_entry(item, &handler->open_infos, link) {
2466 if (item->info.agent_handle == agent_handle) {
2467 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2468 (item->info.attributes == attributes))
2469 return EFI_ALREADY_STARTED;
2470 }
2471 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2472 opened_exclusive = true;
2473 }
2474
2475 /* Only one controller can open the protocol exclusively */
2476 if (opened_exclusive && attributes &
2477 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2478 return EFI_ACCESS_DENIED;
2479
2480 /* Prepare exclusive opening */
2481 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2482 /* Try to disconnect controllers */
2483 list_for_each_entry(item, &handler->open_infos, link) {
2484 if (item->info.attributes ==
2485 EFI_OPEN_PROTOCOL_BY_DRIVER)
2486 EFI_CALL(efi_disconnect_controller(
2487 item->info.controller_handle,
2488 item->info.agent_handle,
2489 NULL));
2490 }
2491 opened_by_driver = false;
2492 /* Check if all controllers are disconnected */
2493 list_for_each_entry(item, &handler->open_infos, link) {
2494 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2495 opened_by_driver = true;
2496 }
2497 /* Only one controller can be conncected */
2498 if (opened_by_driver)
2499 return EFI_ACCESS_DENIED;
2500 }
2501
2502 /* Find existing entry */
2503 list_for_each_entry(item, &handler->open_infos, link) {
2504 if (item->info.agent_handle == agent_handle &&
2505 item->info.controller_handle == controller_handle)
2506 match = &item->info;
2507 }
2508 /* None found, create one */
2509 if (!match) {
2510 match = efi_create_open_info(handler);
2511 if (!match)
2512 return EFI_OUT_OF_RESOURCES;
2513 }
2514
2515 match->agent_handle = agent_handle;
2516 match->controller_handle = controller_handle;
2517 match->attributes = attributes;
2518 match->open_count++;
2519
2520out:
2521 /* For TEST_PROTOCOL ignore interface attribute. */
2522 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2523 *protocol_interface = handler->protocol_interface;
2524
2525 return EFI_SUCCESS;
2526}
2527
332468f7
HS
2528/*
2529 * Open protocol interface on a handle.
2530 *
2531 * This function implements the OpenProtocol interface.
2532 * See the Unified Extensible Firmware Interface (UEFI) specification
2533 * for details.
2534 *
2535 * @handle handle on which the protocol shall be opened
2536 * @protocol GUID of the protocol
2537 * @protocol_interface interface implementing the protocol
2538 * @agent_handle handle of the driver
2539 * @controller_handle handle of the controller
2540 * @attributes attributes indicating how to open the protocol
2541 * @return status code
2542 */
bee91169 2543static efi_status_t EFIAPI efi_open_protocol(
5a9682d0 2544 void *handle, const efi_guid_t *protocol,
bee91169
AG
2545 void **protocol_interface, void *agent_handle,
2546 void *controller_handle, uint32_t attributes)
2547{
80286e8f 2548 struct efi_handler *handler;
69baec67 2549 efi_status_t r = EFI_INVALID_PARAMETER;
bee91169 2550
778e6af8 2551 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
bee91169
AG
2552 protocol_interface, agent_handle, controller_handle,
2553 attributes);
b5349f74 2554
69baec67
HS
2555 if (!handle || !protocol ||
2556 (!protocol_interface && attributes !=
2557 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2558 goto out;
2559 }
2560
2561 switch (attributes) {
2562 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2563 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2564 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2565 break;
2566 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2567 if (controller_handle == handle)
2568 goto out;
191a41cc 2569 /* fall-through */
69baec67
HS
2570 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2571 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
191a41cc
HS
2572 /* Check that the controller handle is valid */
2573 if (!efi_search_obj(controller_handle))
69baec67 2574 goto out;
191a41cc 2575 /* fall-through */
69baec67 2576 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
191a41cc
HS
2577 /* Check that the agent handle is valid */
2578 if (!efi_search_obj(agent_handle))
69baec67
HS
2579 goto out;
2580 break;
2581 default:
b5349f74
HS
2582 goto out;
2583 }
2584
80286e8f
HS
2585 r = efi_search_protocol(handle, protocol, &handler);
2586 if (r != EFI_SUCCESS)
2587 goto out;
bee91169 2588
191a41cc
HS
2589 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2590 controller_handle, attributes);
bee91169
AG
2591out:
2592 return EFI_EXIT(r);
2593}
2594
332468f7
HS
2595/*
2596 * Get interface of a protocol on a handle.
2597 *
2598 * This function implements the HandleProtocol service.
2599 * See the Unified Extensible Firmware Interface (UEFI) specification
2600 * for details.
2601 *
2602 * @handle handle on which the protocol shall be opened
2603 * @protocol GUID of the protocol
2604 * @protocol_interface interface implementing the protocol
2605 * @return status code
2606 */
2074f700 2607static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
5a9682d0 2608 const efi_guid_t *protocol,
bee91169
AG
2609 void **protocol_interface)
2610{
8e1d329f
HS
2611 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2612 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
bee91169
AG
2613}
2614
f0959dbe
HS
2615static efi_status_t efi_bind_controller(
2616 efi_handle_t controller_handle,
2617 efi_handle_t driver_image_handle,
2618 struct efi_device_path *remain_device_path)
2619{
2620 struct efi_driver_binding_protocol *binding_protocol;
2621 efi_status_t r;
2622
2623 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2624 &efi_guid_driver_binding_protocol,
2625 (void **)&binding_protocol,
2626 driver_image_handle, NULL,
2627 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2628 if (r != EFI_SUCCESS)
2629 return r;
2630 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2631 controller_handle,
2632 remain_device_path));
2633 if (r == EFI_SUCCESS)
2634 r = EFI_CALL(binding_protocol->start(binding_protocol,
2635 controller_handle,
2636 remain_device_path));
2637 EFI_CALL(efi_close_protocol(driver_image_handle,
2638 &efi_guid_driver_binding_protocol,
2639 driver_image_handle, NULL));
2640 return r;
2641}
2642
2643static efi_status_t efi_connect_single_controller(
2644 efi_handle_t controller_handle,
2645 efi_handle_t *driver_image_handle,
2646 struct efi_device_path *remain_device_path)
2647{
2648 efi_handle_t *buffer;
2649 size_t count;
2650 size_t i;
2651 efi_status_t r;
2652 size_t connected = 0;
2653
2654 /* Get buffer with all handles with driver binding protocol */
2655 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2656 &efi_guid_driver_binding_protocol,
2657 NULL, &count, &buffer));
2658 if (r != EFI_SUCCESS)
2659 return r;
2660
2661 /* Context Override */
2662 if (driver_image_handle) {
2663 for (; *driver_image_handle; ++driver_image_handle) {
2664 for (i = 0; i < count; ++i) {
2665 if (buffer[i] == *driver_image_handle) {
2666 buffer[i] = NULL;
2667 r = efi_bind_controller(
2668 controller_handle,
2669 *driver_image_handle,
2670 remain_device_path);
2671 /*
2672 * For drivers that do not support the
2673 * controller or are already connected
2674 * we receive an error code here.
2675 */
2676 if (r == EFI_SUCCESS)
2677 ++connected;
2678 }
2679 }
2680 }
2681 }
2682
2683 /*
2684 * TODO: Some overrides are not yet implemented:
2685 * - Platform Driver Override
2686 * - Driver Family Override Search
2687 * - Bus Specific Driver Override
2688 */
2689
2690 /* Driver Binding Search */
2691 for (i = 0; i < count; ++i) {
2692 if (buffer[i]) {
2693 r = efi_bind_controller(controller_handle,
2694 buffer[i],
2695 remain_device_path);
2696 if (r == EFI_SUCCESS)
2697 ++connected;
2698 }
2699 }
2700
2701 efi_free_pool(buffer);
2702 if (!connected)
2703 return EFI_NOT_FOUND;
2704 return EFI_SUCCESS;
2705}
2706
2707/*
2708 * Connect a controller to a driver.
2709 *
2710 * This function implements the ConnectController service.
2711 * See the Unified Extensible Firmware Interface (UEFI) specification
2712 * for details.
2713 *
2714 * First all driver binding protocol handles are tried for binding drivers.
2715 * Afterwards all handles that have openened a protocol of the controller
2716 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2717 *
2718 * @controller_handle handle of the controller
2719 * @driver_image_handle handle of the driver
2720 * @remain_device_path device path of a child controller
2721 * @recursive true to connect all child controllers
2722 * @return status code
2723 */
2724static efi_status_t EFIAPI efi_connect_controller(
2725 efi_handle_t controller_handle,
2726 efi_handle_t *driver_image_handle,
2727 struct efi_device_path *remain_device_path,
2728 bool recursive)
2729{
2730 efi_status_t r;
2731 efi_status_t ret = EFI_NOT_FOUND;
2732 struct efi_object *efiobj;
2733
2734 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2735 remain_device_path, recursive);
2736
2737 efiobj = efi_search_obj(controller_handle);
2738 if (!efiobj) {
2739 ret = EFI_INVALID_PARAMETER;
2740 goto out;
2741 }
2742
2743 r = efi_connect_single_controller(controller_handle,
2744 driver_image_handle,
2745 remain_device_path);
2746 if (r == EFI_SUCCESS)
2747 ret = EFI_SUCCESS;
2748 if (recursive) {
2749 struct efi_handler *handler;
2750 struct efi_open_protocol_info_item *item;
2751
2752 list_for_each_entry(handler, &efiobj->protocols, link) {
2753 list_for_each_entry(item, &handler->open_infos, link) {
2754 if (item->info.attributes &
2755 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2756 r = EFI_CALL(efi_connect_controller(
2757 item->info.controller_handle,
2758 driver_image_handle,
2759 remain_device_path,
2760 recursive));
2761 if (r == EFI_SUCCESS)
2762 ret = EFI_SUCCESS;
2763 }
2764 }
2765 }
2766 }
2767 /* Check for child controller specified by end node */
2768 if (ret != EFI_SUCCESS && remain_device_path &&
2769 remain_device_path->type == DEVICE_PATH_TYPE_END)
2770 ret = EFI_SUCCESS;
2771out:
2772 return EFI_EXIT(ret);
2773}
2774
3f9b0042
HS
2775/*
2776 * Get all child controllers associated to a driver.
2777 * The allocated buffer has to be freed with free().
2778 *
2779 * @efiobj handle of the controller
2780 * @driver_handle handle of the driver
2781 * @number_of_children number of child controllers
2782 * @child_handle_buffer handles of the the child controllers
2783 */
2784static efi_status_t efi_get_child_controllers(
2785 struct efi_object *efiobj,
2786 efi_handle_t driver_handle,
2787 efi_uintn_t *number_of_children,
2788 efi_handle_t **child_handle_buffer)
2789{
2790 struct efi_handler *handler;
2791 struct efi_open_protocol_info_item *item;
2792 efi_uintn_t count = 0, i;
2793 bool duplicate;
2794
2795 /* Count all child controller associations */
2796 list_for_each_entry(handler, &efiobj->protocols, link) {
2797 list_for_each_entry(item, &handler->open_infos, link) {
2798 if (item->info.agent_handle == driver_handle &&
2799 item->info.attributes &
2800 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2801 ++count;
2802 }
2803 }
2804 /*
2805 * Create buffer. In case of duplicate child controller assignments
2806 * the buffer will be too large. But that does not harm.
2807 */
2808 *number_of_children = 0;
2809 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2810 if (!*child_handle_buffer)
2811 return EFI_OUT_OF_RESOURCES;
2812 /* Copy unique child handles */
2813 list_for_each_entry(handler, &efiobj->protocols, link) {
2814 list_for_each_entry(item, &handler->open_infos, link) {
2815 if (item->info.agent_handle == driver_handle &&
2816 item->info.attributes &
2817 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2818 /* Check this is a new child controller */
2819 duplicate = false;
2820 for (i = 0; i < *number_of_children; ++i) {
2821 if ((*child_handle_buffer)[i] ==
2822 item->info.controller_handle)
2823 duplicate = true;
2824 }
2825 /* Copy handle to buffer */
2826 if (!duplicate) {
2827 i = (*number_of_children)++;
2828 (*child_handle_buffer)[i] =
2829 item->info.controller_handle;
2830 }
2831 }
2832 }
2833 }
2834 return EFI_SUCCESS;
2835}
2836
2837/*
2838 * Disconnect a controller from a driver.
2839 *
2840 * This function implements the DisconnectController service.
2841 * See the Unified Extensible Firmware Interface (UEFI) specification
2842 * for details.
2843 *
2844 * @controller_handle handle of the controller
2845 * @driver_image_handle handle of the driver
2846 * @child_handle handle of the child to destroy
2847 * @return status code
2848 */
2849static efi_status_t EFIAPI efi_disconnect_controller(
2850 efi_handle_t controller_handle,
2851 efi_handle_t driver_image_handle,
2852 efi_handle_t child_handle)
2853{
2854 struct efi_driver_binding_protocol *binding_protocol;
2855 efi_handle_t *child_handle_buffer = NULL;
2856 size_t number_of_children = 0;
2857 efi_status_t r;
2858 size_t stop_count = 0;
2859 struct efi_object *efiobj;
2860
2861 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2862 child_handle);
2863
2864 efiobj = efi_search_obj(controller_handle);
2865 if (!efiobj) {
2866 r = EFI_INVALID_PARAMETER;
2867 goto out;
2868 }
2869
2870 if (child_handle && !efi_search_obj(child_handle)) {
2871 r = EFI_INVALID_PARAMETER;
2872 goto out;
2873 }
2874
2875 /* If no driver handle is supplied, disconnect all drivers */
2876 if (!driver_image_handle) {
2877 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2878 goto out;
2879 }
2880
2881 /* Create list of child handles */
2882 if (child_handle) {
2883 number_of_children = 1;
2884 child_handle_buffer = &child_handle;
2885 } else {
2886 efi_get_child_controllers(efiobj,
2887 driver_image_handle,
2888 &number_of_children,
2889 &child_handle_buffer);
2890 }
2891
2892 /* Get the driver binding protocol */
2893 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2894 &efi_guid_driver_binding_protocol,
2895 (void **)&binding_protocol,
2896 driver_image_handle, NULL,
2897 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2898 if (r != EFI_SUCCESS)
2899 goto out;
2900 /* Remove the children */
2901 if (number_of_children) {
2902 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2903 controller_handle,
2904 number_of_children,
2905 child_handle_buffer));
2906 if (r == EFI_SUCCESS)
2907 ++stop_count;
2908 }
2909 /* Remove the driver */
2910 if (!child_handle)
2911 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2912 controller_handle,
2913 0, NULL));
2914 if (r == EFI_SUCCESS)
2915 ++stop_count;
2916 EFI_CALL(efi_close_protocol(driver_image_handle,
2917 &efi_guid_driver_binding_protocol,
2918 driver_image_handle, NULL));
2919
2920 if (stop_count)
2921 r = EFI_SUCCESS;
2922 else
2923 r = EFI_NOT_FOUND;
2924out:
2925 if (!child_handle)
2926 free(child_handle_buffer);
2927 return EFI_EXIT(r);
2928}
2929
bee91169
AG
2930static const struct efi_boot_services efi_boot_services = {
2931 .hdr = {
2932 .headersize = sizeof(struct efi_table_hdr),
2933 },
2934 .raise_tpl = efi_raise_tpl,
2935 .restore_tpl = efi_restore_tpl,
2936 .allocate_pages = efi_allocate_pages_ext,
2937 .free_pages = efi_free_pages_ext,
2938 .get_memory_map = efi_get_memory_map_ext,
ead1274b 2939 .allocate_pool = efi_allocate_pool_ext,
42417bc8 2940 .free_pool = efi_free_pool_ext,
49deb455 2941 .create_event = efi_create_event_ext,
bfc72462 2942 .set_timer = efi_set_timer_ext,
bee91169 2943 .wait_for_event = efi_wait_for_event,
c6841592 2944 .signal_event = efi_signal_event_ext,
bee91169
AG
2945 .close_event = efi_close_event,
2946 .check_event = efi_check_event,
1760ef57 2947 .install_protocol_interface = efi_install_protocol_interface,
bee91169 2948 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
cd534083 2949 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
bee91169
AG
2950 .handle_protocol = efi_handle_protocol,
2951 .reserved = NULL,
2952 .register_protocol_notify = efi_register_protocol_notify,
26329584 2953 .locate_handle = efi_locate_handle_ext,
bee91169 2954 .locate_device_path = efi_locate_device_path,
488bf12d 2955 .install_configuration_table = efi_install_configuration_table_ext,
bee91169
AG
2956 .load_image = efi_load_image,
2957 .start_image = efi_start_image,
a86aeaf2 2958 .exit = efi_exit,
bee91169
AG
2959 .unload_image = efi_unload_image,
2960 .exit_boot_services = efi_exit_boot_services,
2961 .get_next_monotonic_count = efi_get_next_monotonic_count,
2962 .stall = efi_stall,
2963 .set_watchdog_timer = efi_set_watchdog_timer,
2964 .connect_controller = efi_connect_controller,
2965 .disconnect_controller = efi_disconnect_controller,
2966 .open_protocol = efi_open_protocol,
2967 .close_protocol = efi_close_protocol,
2968 .open_protocol_information = efi_open_protocol_information,
2969 .protocols_per_handle = efi_protocols_per_handle,
2970 .locate_handle_buffer = efi_locate_handle_buffer,
2971 .locate_protocol = efi_locate_protocol,
ab9efa97
HS
2972 .install_multiple_protocol_interfaces =
2973 efi_install_multiple_protocol_interfaces,
2974 .uninstall_multiple_protocol_interfaces =
2975 efi_uninstall_multiple_protocol_interfaces,
bee91169
AG
2976 .calculate_crc32 = efi_calculate_crc32,
2977 .copy_mem = efi_copy_mem,
2978 .set_mem = efi_set_mem,
9f0930e5 2979 .create_event_ex = efi_create_event_ex,
bee91169
AG
2980};
2981
05b6f56e 2982static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
bee91169 2983
3c63db9c 2984struct efi_system_table __efi_runtime_data systab = {
bee91169
AG
2985 .hdr = {
2986 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
f19a95a4 2987 .revision = 2 << 16 | 70, /* 2.7 */
bee91169
AG
2988 .headersize = sizeof(struct efi_table_hdr),
2989 },
2990 .fw_vendor = (long)firmware_vendor,
ab9efa97
HS
2991 .con_in = (void *)&efi_con_in,
2992 .con_out = (void *)&efi_con_out,
2993 .std_err = (void *)&efi_con_out,
2994 .runtime = (void *)&efi_runtime_services,
2995 .boottime = (void *)&efi_boot_services,
bee91169 2996 .nr_tables = 0,
ab9efa97 2997 .tables = (void *)efi_conf_table,
bee91169 2998};