1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application boot time services
5 * Copyright (c) 2016 Alexander Graf
10 #include <efi_loader.h>
11 #include <environment.h>
13 #include <linux/libfdt_env.h>
14 #include <u-boot/crc.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 /* Task priority level */
21 static efi_uintn_t efi_tpl = TPL_APPLICATION;
23 /* This list contains all the EFI objects our payload has access to */
24 LIST_HEAD(efi_obj_list);
26 /* List of all events */
27 LIST_HEAD(efi_events);
30 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31 * we need to do trickery with caches. Since we don't want to break the EFI
32 * aware boot path, only apply hacks when loading exiting directly (breaking
33 * direct Linux EFI booting along the way - oh well).
35 static bool efi_is_direct_boot = true;
39 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
40 * fixed when compiling U-Boot. However, the payload does not know about that
41 * restriction so we need to manually swap its and our view of that register on
42 * EFI callback entry/exit.
44 static volatile void *efi_gd, *app_gd;
47 /* 1 if inside U-Boot code, 0 if inside EFI payload code */
48 static int entry_count = 1;
49 static int nesting_level;
50 /* GUID of the device tree table */
51 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
52 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
53 const efi_guid_t efi_guid_driver_binding_protocol =
54 EFI_DRIVER_BINDING_PROTOCOL_GUID;
56 /* event group ExitBootServices() invoked */
57 const efi_guid_t efi_guid_event_group_exit_boot_services =
58 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
59 /* event group SetVirtualAddressMap() invoked */
60 const efi_guid_t efi_guid_event_group_virtual_address_change =
61 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
62 /* event group memory map changed */
63 const efi_guid_t efi_guid_event_group_memory_map_change =
64 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
65 /* event group boot manager about to boot */
66 const efi_guid_t efi_guid_event_group_ready_to_boot =
67 EFI_EVENT_GROUP_READY_TO_BOOT;
68 /* event group ResetSystem() invoked (before ExitBootServices) */
69 const efi_guid_t efi_guid_event_group_reset_system =
70 EFI_EVENT_GROUP_RESET_SYSTEM;
72 static efi_status_t EFIAPI efi_disconnect_controller(
73 efi_handle_t controller_handle,
74 efi_handle_t driver_image_handle,
75 efi_handle_t child_handle);
77 /* Called on every callback entry */
78 int __efi_entry_check(void)
80 int ret = entry_count++ == 0;
89 /* Called on every callback exit */
90 int __efi_exit_check(void)
92 int ret = --entry_count == 0;
99 /* Called from do_bootefi_exec() */
100 void efi_save_gd(void)
108 * Special case handler for error/abort that just forces things back to u-boot
109 * world so we can dump out an abort message, without any care about returning
110 * back to UEFI world.
112 void efi_restore_gd(void)
115 /* Only restore if we're already in EFI context */
123 * indent_string() - returns a string for indenting with two spaces per level
124 * @level: indent level
126 * A maximum of ten indent levels is supported. Higher indent levels will be
129 * Return: A string for indenting with two spaces per level is
132 static const char *indent_string(int level)
134 const char *indent = " ";
135 const int max = strlen(indent);
137 level = min(max, level * 2);
138 return &indent[max - level];
141 const char *__efi_nesting(void)
143 return indent_string(nesting_level);
146 const char *__efi_nesting_inc(void)
148 return indent_string(nesting_level++);
151 const char *__efi_nesting_dec(void)
153 return indent_string(--nesting_level);
157 * efi_queue_event() - queue an EFI event
158 * @event: event to signal
159 * @check_tpl: check the TPL level
161 * This function queues the notification function of the event for future
164 * The notification function is called if the task priority level of the event
165 * is higher than the current task priority level.
167 * For the SignalEvent service see efi_signal_event_ext.
170 static void efi_queue_event(struct efi_event *event, bool check_tpl)
172 if (event->notify_function) {
173 event->is_queued = true;
175 if (check_tpl && efi_tpl >= event->notify_tpl)
177 EFI_CALL_VOID(event->notify_function(event,
178 event->notify_context));
180 event->is_queued = false;
184 * is_valid_tpl() - check if the task priority level is valid
186 * @tpl: TPL level to check
187 * Return: status code
189 efi_status_t is_valid_tpl(efi_uintn_t tpl)
192 case TPL_APPLICATION:
198 return EFI_INVALID_PARAMETER;
203 * efi_signal_event() - signal an EFI event
204 * @event: event to signal
205 * @check_tpl: check the TPL level
207 * This function signals an event. If the event belongs to an event group all
208 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
209 * their notification function is queued.
211 * For the SignalEvent service see efi_signal_event_ext.
213 void efi_signal_event(struct efi_event *event, bool check_tpl)
216 struct efi_event *evt;
219 * The signaled state has to set before executing any
220 * notification function
222 list_for_each_entry(evt, &efi_events, link) {
223 if (!evt->group || guidcmp(evt->group, event->group))
225 if (evt->is_signaled)
227 evt->is_signaled = true;
228 if (evt->type & EVT_NOTIFY_SIGNAL &&
229 evt->notify_function)
230 evt->is_queued = true;
232 list_for_each_entry(evt, &efi_events, link) {
233 if (!evt->group || guidcmp(evt->group, event->group))
236 efi_queue_event(evt, check_tpl);
238 } else if (!event->is_signaled) {
239 event->is_signaled = true;
240 if (event->type & EVT_NOTIFY_SIGNAL)
241 efi_queue_event(event, check_tpl);
246 * efi_raise_tpl() - raise the task priority level
247 * @new_tpl: new value of the task priority level
249 * This function implements the RaiseTpl service.
251 * See the Unified Extensible Firmware Interface (UEFI) specification for
254 * Return: old value of the task priority level
256 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
258 efi_uintn_t old_tpl = efi_tpl;
260 EFI_ENTRY("0x%zx", new_tpl);
262 if (new_tpl < efi_tpl)
263 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
265 if (efi_tpl > TPL_HIGH_LEVEL)
266 efi_tpl = TPL_HIGH_LEVEL;
268 EFI_EXIT(EFI_SUCCESS);
273 * efi_restore_tpl() - lower the task priority level
274 * @old_tpl: value of the task priority level to be restored
276 * This function implements the RestoreTpl service.
278 * See the Unified Extensible Firmware Interface (UEFI) specification for
281 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
283 EFI_ENTRY("0x%zx", old_tpl);
285 if (old_tpl > efi_tpl)
286 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
288 if (efi_tpl > TPL_HIGH_LEVEL)
289 efi_tpl = TPL_HIGH_LEVEL;
292 * Lowering the TPL may have made queued events eligible for execution.
296 EFI_EXIT(EFI_SUCCESS);
300 * efi_allocate_pages_ext() - allocate memory pages
301 * @type: type of allocation to be performed
302 * @memory_type: usage type of the allocated memory
303 * @pages: number of pages to be allocated
304 * @memory: allocated memory
306 * This function implements the AllocatePages service.
308 * See the Unified Extensible Firmware Interface (UEFI) specification for
311 * Return: status code
313 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
319 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
320 r = efi_allocate_pages(type, memory_type, pages, memory);
325 * efi_free_pages_ext() - Free memory pages.
326 * @memory: start of the memory area to be freed
327 * @pages: number of pages to be freed
329 * This function implements the FreePages service.
331 * See the Unified Extensible Firmware Interface (UEFI) specification for
334 * Return: status code
336 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
341 EFI_ENTRY("%llx, 0x%zx", memory, pages);
342 r = efi_free_pages(memory, pages);
347 * efi_get_memory_map_ext() - get map describing memory usage
348 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
349 * on exit the size of the copied memory map
350 * @memory_map: buffer to which the memory map is written
351 * @map_key: key for the memory map
352 * @descriptor_size: size of an individual memory descriptor
353 * @descriptor_version: version number of the memory descriptor structure
355 * This function implements the GetMemoryMap service.
357 * See the Unified Extensible Firmware Interface (UEFI) specification for
360 * Return: status code
362 static efi_status_t EFIAPI efi_get_memory_map_ext(
363 efi_uintn_t *memory_map_size,
364 struct efi_mem_desc *memory_map,
365 efi_uintn_t *map_key,
366 efi_uintn_t *descriptor_size,
367 uint32_t *descriptor_version)
371 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
372 map_key, descriptor_size, descriptor_version);
373 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
374 descriptor_size, descriptor_version);
379 * efi_allocate_pool_ext() - allocate memory from pool
380 * @pool_type: type of the pool from which memory is to be allocated
381 * @size: number of bytes to be allocated
382 * @buffer: allocated memory
384 * This function implements the AllocatePool service.
386 * See the Unified Extensible Firmware Interface (UEFI) specification for
389 * Return: status code
391 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
397 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
398 r = efi_allocate_pool(pool_type, size, buffer);
403 * efi_free_pool_ext() - free memory from pool
404 * @buffer: start of memory to be freed
406 * This function implements the FreePool service.
408 * See the Unified Extensible Firmware Interface (UEFI) specification for
411 * Return: status code
413 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
417 EFI_ENTRY("%p", buffer);
418 r = efi_free_pool(buffer);
423 * efi_add_handle() - add a new object to the object list
424 * @obj: object to be added
426 * The protocols list is initialized. The object handle is set.
428 void efi_add_handle(efi_handle_t handle)
432 INIT_LIST_HEAD(&handle->protocols);
433 list_add_tail(&handle->link, &efi_obj_list);
437 * efi_create_handle() - create handle
438 * @handle: new handle
440 * Return: status code
442 efi_status_t efi_create_handle(efi_handle_t *handle)
444 struct efi_object *obj;
446 obj = calloc(1, sizeof(struct efi_object));
448 return EFI_OUT_OF_RESOURCES;
457 * efi_search_protocol() - find a protocol on a handle.
459 * @protocol_guid: GUID of the protocol
460 * @handler: reference to the protocol
462 * Return: status code
464 efi_status_t efi_search_protocol(const efi_handle_t handle,
465 const efi_guid_t *protocol_guid,
466 struct efi_handler **handler)
468 struct efi_object *efiobj;
469 struct list_head *lhandle;
471 if (!handle || !protocol_guid)
472 return EFI_INVALID_PARAMETER;
473 efiobj = efi_search_obj(handle);
475 return EFI_INVALID_PARAMETER;
476 list_for_each(lhandle, &efiobj->protocols) {
477 struct efi_handler *protocol;
479 protocol = list_entry(lhandle, struct efi_handler, link);
480 if (!guidcmp(protocol->guid, protocol_guid)) {
486 return EFI_NOT_FOUND;
490 * efi_remove_protocol() - delete protocol from a handle
491 * @handle: handle from which the protocol shall be deleted
492 * @protocol: GUID of the protocol to be deleted
493 * @protocol_interface: interface of the protocol implementation
495 * Return: status code
497 efi_status_t efi_remove_protocol(const efi_handle_t handle,
498 const efi_guid_t *protocol,
499 void *protocol_interface)
501 struct efi_handler *handler;
504 ret = efi_search_protocol(handle, protocol, &handler);
505 if (ret != EFI_SUCCESS)
507 if (guidcmp(handler->guid, protocol))
508 return EFI_INVALID_PARAMETER;
509 if (handler->protocol_interface != protocol_interface)
510 return EFI_INVALID_PARAMETER;
511 list_del(&handler->link);
517 * efi_remove_all_protocols() - delete all protocols from a handle
518 * @handle: handle from which the protocols shall be deleted
520 * Return: status code
522 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
524 struct efi_object *efiobj;
525 struct efi_handler *protocol;
526 struct efi_handler *pos;
528 efiobj = efi_search_obj(handle);
530 return EFI_INVALID_PARAMETER;
531 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
534 ret = efi_remove_protocol(handle, protocol->guid,
535 protocol->protocol_interface);
536 if (ret != EFI_SUCCESS)
543 * efi_delete_handle() - delete handle
545 * @obj: handle to delete
547 void efi_delete_handle(efi_handle_t handle)
551 efi_remove_all_protocols(handle);
552 list_del(&handle->link);
557 * efi_is_event() - check if a pointer is a valid event
558 * @event: pointer to check
560 * Return: status code
562 static efi_status_t efi_is_event(const struct efi_event *event)
564 const struct efi_event *evt;
567 return EFI_INVALID_PARAMETER;
568 list_for_each_entry(evt, &efi_events, link) {
572 return EFI_INVALID_PARAMETER;
576 * efi_create_event() - create an event
577 * @type: type of the event to create
578 * @notify_tpl: task priority level of the event
579 * @notify_function: notification function of the event
580 * @notify_context: pointer passed to the notification function
581 * @group: event group
582 * @event: created event
584 * This function is used inside U-Boot code to create an event.
586 * For the API function implementing the CreateEvent service see
587 * efi_create_event_ext.
589 * Return: status code
591 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
592 void (EFIAPI *notify_function) (
593 struct efi_event *event,
595 void *notify_context, efi_guid_t *group,
596 struct efi_event **event)
598 struct efi_event *evt;
601 return EFI_INVALID_PARAMETER;
606 case EVT_NOTIFY_SIGNAL:
607 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
608 case EVT_NOTIFY_WAIT:
609 case EVT_TIMER | EVT_NOTIFY_WAIT:
610 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
611 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
614 return EFI_INVALID_PARAMETER;
617 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
618 (is_valid_tpl(notify_tpl) != EFI_SUCCESS))
619 return EFI_INVALID_PARAMETER;
621 evt = calloc(1, sizeof(struct efi_event));
623 return EFI_OUT_OF_RESOURCES;
625 evt->notify_tpl = notify_tpl;
626 evt->notify_function = notify_function;
627 evt->notify_context = notify_context;
629 /* Disable timers on boot up */
630 evt->trigger_next = -1ULL;
631 evt->is_queued = false;
632 evt->is_signaled = false;
633 list_add_tail(&evt->link, &efi_events);
639 * efi_create_event_ex() - create an event in a group
640 * @type: type of the event to create
641 * @notify_tpl: task priority level of the event
642 * @notify_function: notification function of the event
643 * @notify_context: pointer passed to the notification function
644 * @event: created event
645 * @event_group: event group
647 * This function implements the CreateEventEx service.
649 * See the Unified Extensible Firmware Interface (UEFI) specification for
652 * Return: status code
654 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
655 void (EFIAPI *notify_function) (
656 struct efi_event *event,
658 void *notify_context,
659 efi_guid_t *event_group,
660 struct efi_event **event)
662 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
663 notify_context, event_group);
664 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
665 notify_context, event_group, event));
669 * efi_create_event_ext() - create an event
670 * @type: type of the event to create
671 * @notify_tpl: task priority level of the event
672 * @notify_function: notification function of the event
673 * @notify_context: pointer passed to the notification function
674 * @event: created event
676 * This function implements the CreateEvent service.
678 * See the Unified Extensible Firmware Interface (UEFI) specification for
681 * Return: status code
683 static efi_status_t EFIAPI efi_create_event_ext(
684 uint32_t type, efi_uintn_t notify_tpl,
685 void (EFIAPI *notify_function) (
686 struct efi_event *event,
688 void *notify_context, struct efi_event **event)
690 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
692 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
693 notify_context, NULL, event));
697 * efi_timer_check() - check if a timer event has occurred
699 * Check if a timer event has occurred or a queued notification function should
702 * Our timers have to work without interrupts, so we check whenever keyboard
703 * input or disk accesses happen if enough time elapsed for them to fire.
705 void efi_timer_check(void)
707 struct efi_event *evt;
708 u64 now = timer_get_us();
710 list_for_each_entry(evt, &efi_events, link) {
712 efi_queue_event(evt, true);
713 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
715 switch (evt->trigger_type) {
716 case EFI_TIMER_RELATIVE:
717 evt->trigger_type = EFI_TIMER_STOP;
719 case EFI_TIMER_PERIODIC:
720 evt->trigger_next += evt->trigger_time;
725 evt->is_signaled = false;
726 efi_signal_event(evt, true);
732 * efi_set_timer() - set the trigger time for a timer event or stop the event
733 * @event: event for which the timer is set
734 * @type: type of the timer
735 * @trigger_time: trigger period in multiples of 100 ns
737 * This is the function for internal usage in U-Boot. For the API function
738 * implementing the SetTimer service see efi_set_timer_ext.
740 * Return: status code
742 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
743 uint64_t trigger_time)
745 /* Check that the event is valid */
746 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
747 return EFI_INVALID_PARAMETER;
750 * The parameter defines a multiple of 100 ns.
751 * We use multiples of 1000 ns. So divide by 10.
753 do_div(trigger_time, 10);
757 event->trigger_next = -1ULL;
759 case EFI_TIMER_PERIODIC:
760 case EFI_TIMER_RELATIVE:
761 event->trigger_next = timer_get_us() + trigger_time;
764 return EFI_INVALID_PARAMETER;
766 event->trigger_type = type;
767 event->trigger_time = trigger_time;
768 event->is_signaled = false;
773 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
775 * @event: event for which the timer is set
776 * @type: type of the timer
777 * @trigger_time: trigger period in multiples of 100 ns
779 * This function implements the SetTimer service.
781 * See the Unified Extensible Firmware Interface (UEFI) specification for
785 * Return: status code
787 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
788 enum efi_timer_delay type,
789 uint64_t trigger_time)
791 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
792 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
796 * efi_wait_for_event() - wait for events to be signaled
797 * @num_events: number of events to be waited for
798 * @event: events to be waited for
799 * @index: index of the event that was signaled
801 * This function implements the WaitForEvent service.
803 * See the Unified Extensible Firmware Interface (UEFI) specification for
806 * Return: status code
808 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
809 struct efi_event **event,
814 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
816 /* Check parameters */
817 if (!num_events || !event)
818 return EFI_EXIT(EFI_INVALID_PARAMETER);
820 if (efi_tpl != TPL_APPLICATION)
821 return EFI_EXIT(EFI_UNSUPPORTED);
822 for (i = 0; i < num_events; ++i) {
823 if (efi_is_event(event[i]) != EFI_SUCCESS)
824 return EFI_EXIT(EFI_INVALID_PARAMETER);
825 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
826 return EFI_EXIT(EFI_INVALID_PARAMETER);
827 if (!event[i]->is_signaled)
828 efi_queue_event(event[i], true);
831 /* Wait for signal */
833 for (i = 0; i < num_events; ++i) {
834 if (event[i]->is_signaled)
837 /* Allow events to occur. */
843 * Reset the signal which is passed to the caller to allow periodic
846 event[i]->is_signaled = false;
850 return EFI_EXIT(EFI_SUCCESS);
854 * efi_signal_event_ext() - signal an EFI event
855 * @event: event to signal
857 * This function implements the SignalEvent service.
859 * See the Unified Extensible Firmware Interface (UEFI) specification for
862 * This functions sets the signaled state of the event and queues the
863 * notification function for execution.
865 * Return: status code
867 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
869 EFI_ENTRY("%p", event);
870 if (efi_is_event(event) != EFI_SUCCESS)
871 return EFI_EXIT(EFI_INVALID_PARAMETER);
872 efi_signal_event(event, true);
873 return EFI_EXIT(EFI_SUCCESS);
877 * efi_close_event() - close an EFI event
878 * @event: event to close
880 * This function implements the CloseEvent service.
882 * See the Unified Extensible Firmware Interface (UEFI) specification for
885 * Return: status code
887 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
889 EFI_ENTRY("%p", event);
890 if (efi_is_event(event) != EFI_SUCCESS)
891 return EFI_EXIT(EFI_INVALID_PARAMETER);
892 list_del(&event->link);
894 return EFI_EXIT(EFI_SUCCESS);
898 * efi_check_event() - check if an event is signaled
899 * @event: event to check
901 * This function implements the CheckEvent service.
903 * See the Unified Extensible Firmware Interface (UEFI) specification for
906 * If an event is not signaled yet, the notification function is queued. The
907 * signaled state is cleared.
909 * Return: status code
911 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
913 EFI_ENTRY("%p", event);
915 if (efi_is_event(event) != EFI_SUCCESS ||
916 event->type & EVT_NOTIFY_SIGNAL)
917 return EFI_EXIT(EFI_INVALID_PARAMETER);
918 if (!event->is_signaled)
919 efi_queue_event(event, true);
920 if (event->is_signaled) {
921 event->is_signaled = false;
922 return EFI_EXIT(EFI_SUCCESS);
924 return EFI_EXIT(EFI_NOT_READY);
928 * efi_search_obj() - find the internal EFI object for a handle
929 * @handle: handle to find
933 struct efi_object *efi_search_obj(const efi_handle_t handle)
935 struct efi_object *efiobj;
937 list_for_each_entry(efiobj, &efi_obj_list, link) {
938 if (efiobj == handle)
946 * efi_open_protocol_info_entry() - create open protocol info entry and add it
948 * @handler: handler of a protocol
950 * Return: open protocol info entry
952 static struct efi_open_protocol_info_entry *efi_create_open_info(
953 struct efi_handler *handler)
955 struct efi_open_protocol_info_item *item;
957 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
960 /* Append the item to the open protocol info list. */
961 list_add_tail(&item->link, &handler->open_infos);
967 * efi_delete_open_info() - remove an open protocol info entry from a protocol
968 * @item: open protocol info entry to delete
970 * Return: status code
972 static efi_status_t efi_delete_open_info(
973 struct efi_open_protocol_info_item *item)
975 list_del(&item->link);
981 * efi_add_protocol() - install new protocol on a handle
982 * @handle: handle on which the protocol shall be installed
983 * @protocol: GUID of the protocol to be installed
984 * @protocol_interface: interface of the protocol implementation
986 * Return: status code
988 efi_status_t efi_add_protocol(const efi_handle_t handle,
989 const efi_guid_t *protocol,
990 void *protocol_interface)
992 struct efi_object *efiobj;
993 struct efi_handler *handler;
996 efiobj = efi_search_obj(handle);
998 return EFI_INVALID_PARAMETER;
999 ret = efi_search_protocol(handle, protocol, NULL);
1000 if (ret != EFI_NOT_FOUND)
1001 return EFI_INVALID_PARAMETER;
1002 handler = calloc(1, sizeof(struct efi_handler));
1004 return EFI_OUT_OF_RESOURCES;
1005 handler->guid = protocol;
1006 handler->protocol_interface = protocol_interface;
1007 INIT_LIST_HEAD(&handler->open_infos);
1008 list_add_tail(&handler->link, &efiobj->protocols);
1009 if (!guidcmp(&efi_guid_device_path, protocol))
1010 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1015 * efi_install_protocol_interface() - install protocol interface
1016 * @handle: handle on which the protocol shall be installed
1017 * @protocol: GUID of the protocol to be installed
1018 * @protocol_interface_type: type of the interface to be installed,
1019 * always EFI_NATIVE_INTERFACE
1020 * @protocol_interface: interface of the protocol implementation
1022 * This function implements the InstallProtocolInterface service.
1024 * See the Unified Extensible Firmware Interface (UEFI) specification for
1027 * Return: status code
1029 static efi_status_t EFIAPI efi_install_protocol_interface(
1030 efi_handle_t *handle, const efi_guid_t *protocol,
1031 int protocol_interface_type, void *protocol_interface)
1035 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1036 protocol_interface);
1038 if (!handle || !protocol ||
1039 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1040 r = EFI_INVALID_PARAMETER;
1044 /* Create new handle if requested. */
1046 r = efi_create_handle(handle);
1047 if (r != EFI_SUCCESS)
1049 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1052 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1055 /* Add new protocol */
1056 r = efi_add_protocol(*handle, protocol, protocol_interface);
1062 * efi_get_drivers() - get all drivers associated to a controller
1063 * @handle: handle of the controller
1064 * @protocol: protocol GUID (optional)
1065 * @number_of_drivers: number of child controllers
1066 * @driver_handle_buffer: handles of the the drivers
1068 * The allocated buffer has to be freed with free().
1070 * Return: status code
1072 static efi_status_t efi_get_drivers(efi_handle_t handle,
1073 const efi_guid_t *protocol,
1074 efi_uintn_t *number_of_drivers,
1075 efi_handle_t **driver_handle_buffer)
1077 struct efi_handler *handler;
1078 struct efi_open_protocol_info_item *item;
1079 efi_uintn_t count = 0, i;
1082 /* Count all driver associations */
1083 list_for_each_entry(handler, &handle->protocols, link) {
1084 if (protocol && guidcmp(handler->guid, protocol))
1086 list_for_each_entry(item, &handler->open_infos, link) {
1087 if (item->info.attributes &
1088 EFI_OPEN_PROTOCOL_BY_DRIVER)
1093 * Create buffer. In case of duplicate driver assignments the buffer
1094 * will be too large. But that does not harm.
1096 *number_of_drivers = 0;
1097 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1098 if (!*driver_handle_buffer)
1099 return EFI_OUT_OF_RESOURCES;
1100 /* Collect unique driver handles */
1101 list_for_each_entry(handler, &handle->protocols, link) {
1102 if (protocol && guidcmp(handler->guid, protocol))
1104 list_for_each_entry(item, &handler->open_infos, link) {
1105 if (item->info.attributes &
1106 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1107 /* Check this is a new driver */
1109 for (i = 0; i < *number_of_drivers; ++i) {
1110 if ((*driver_handle_buffer)[i] ==
1111 item->info.agent_handle)
1114 /* Copy handle to buffer */
1116 i = (*number_of_drivers)++;
1117 (*driver_handle_buffer)[i] =
1118 item->info.agent_handle;
1127 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1128 * @handle: handle of the controller
1129 * @protocol: protocol GUID (optional)
1130 * @child_handle: handle of the child to destroy
1132 * This function implements the DisconnectController service.
1134 * See the Unified Extensible Firmware Interface (UEFI) specification for
1137 * Return: status code
1139 static efi_status_t efi_disconnect_all_drivers
1140 (efi_handle_t handle,
1141 const efi_guid_t *protocol,
1142 efi_handle_t child_handle)
1144 efi_uintn_t number_of_drivers;
1145 efi_handle_t *driver_handle_buffer;
1146 efi_status_t r, ret;
1148 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
1149 &driver_handle_buffer);
1150 if (ret != EFI_SUCCESS)
1153 ret = EFI_NOT_FOUND;
1154 while (number_of_drivers) {
1155 r = EFI_CALL(efi_disconnect_controller(
1157 driver_handle_buffer[--number_of_drivers],
1159 if (r == EFI_SUCCESS)
1162 free(driver_handle_buffer);
1167 * efi_uninstall_protocol() - uninstall protocol interface
1169 * @handle: handle from which the protocol shall be removed
1170 * @protocol: GUID of the protocol to be removed
1171 * @protocol_interface: interface to be removed
1173 * This function DOES NOT delete a handle without installed protocol.
1175 * Return: status code
1177 static efi_status_t efi_uninstall_protocol
1178 (efi_handle_t handle, const efi_guid_t *protocol,
1179 void *protocol_interface)
1181 struct efi_object *efiobj;
1182 struct efi_handler *handler;
1183 struct efi_open_protocol_info_item *item;
1184 struct efi_open_protocol_info_item *pos;
1188 efiobj = efi_search_obj(handle);
1190 r = EFI_INVALID_PARAMETER;
1193 /* Find the protocol on the handle */
1194 r = efi_search_protocol(handle, protocol, &handler);
1195 if (r != EFI_SUCCESS)
1197 /* Disconnect controllers */
1198 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1199 if (!list_empty(&handler->open_infos)) {
1200 r = EFI_ACCESS_DENIED;
1203 /* Close protocol */
1204 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1205 if (item->info.attributes ==
1206 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1207 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1208 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1209 list_del(&item->link);
1211 if (!list_empty(&handler->open_infos)) {
1212 r = EFI_ACCESS_DENIED;
1215 r = efi_remove_protocol(handle, protocol, protocol_interface);
1221 * efi_uninstall_protocol_interface() - uninstall protocol interface
1222 * @handle: handle from which the protocol shall be removed
1223 * @protocol: GUID of the protocol to be removed
1224 * @protocol_interface: interface to be removed
1226 * This function implements the UninstallProtocolInterface service.
1228 * See the Unified Extensible Firmware Interface (UEFI) specification for
1231 * Return: status code
1233 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1234 (efi_handle_t handle, const efi_guid_t *protocol,
1235 void *protocol_interface)
1239 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1241 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1242 if (ret != EFI_SUCCESS)
1245 /* If the last protocol has been removed, delete the handle. */
1246 if (list_empty(&handle->protocols)) {
1247 list_del(&handle->link);
1251 return EFI_EXIT(ret);
1255 * efi_register_protocol_notify() - register an event for notification when a
1256 * protocol is installed.
1257 * @protocol: GUID of the protocol whose installation shall be notified
1258 * @event: event to be signaled upon installation of the protocol
1259 * @registration: key for retrieving the registration information
1261 * This function implements the RegisterProtocolNotify service.
1262 * See the Unified Extensible Firmware Interface (UEFI) specification
1265 * Return: status code
1267 static efi_status_t EFIAPI efi_register_protocol_notify(
1268 const efi_guid_t *protocol,
1269 struct efi_event *event,
1270 void **registration)
1272 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1273 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1277 * efi_search() - determine if an EFI handle implements a protocol
1278 * @search_type: selection criterion
1279 * @protocol: GUID of the protocol
1280 * @search_key: registration key
1283 * See the documentation of the LocateHandle service in the UEFI specification.
1285 * Return: 0 if the handle implements the protocol
1287 static int efi_search(enum efi_locate_search_type search_type,
1288 const efi_guid_t *protocol, void *search_key,
1289 efi_handle_t handle)
1293 switch (search_type) {
1296 case BY_REGISTER_NOTIFY:
1297 /* TODO: RegisterProtocolNotify is not implemented yet */
1300 ret = efi_search_protocol(handle, protocol, NULL);
1301 return (ret != EFI_SUCCESS);
1303 /* Invalid search type */
1309 * efi_locate_handle() - locate handles implementing a protocol
1310 * @search_type: selection criterion
1311 * @protocol: GUID of the protocol
1312 * @search_key: registration key
1313 * @buffer_size: size of the buffer to receive the handles in bytes
1314 * @buffer: buffer to receive the relevant handles
1316 * This function is meant for U-Boot internal calls. For the API implementation
1317 * of the LocateHandle service see efi_locate_handle_ext.
1319 * Return: status code
1321 static efi_status_t efi_locate_handle(
1322 enum efi_locate_search_type search_type,
1323 const efi_guid_t *protocol, void *search_key,
1324 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1326 struct efi_object *efiobj;
1327 efi_uintn_t size = 0;
1329 /* Check parameters */
1330 switch (search_type) {
1333 case BY_REGISTER_NOTIFY:
1335 return EFI_INVALID_PARAMETER;
1336 /* RegisterProtocolNotify is not implemented yet */
1337 return EFI_UNSUPPORTED;
1340 return EFI_INVALID_PARAMETER;
1343 return EFI_INVALID_PARAMETER;
1347 * efi_locate_handle_buffer uses this function for
1348 * the calculation of the necessary buffer size.
1349 * So do not require a buffer for buffersize == 0.
1351 if (!buffer_size || (*buffer_size && !buffer))
1352 return EFI_INVALID_PARAMETER;
1354 /* Count how much space we need */
1355 list_for_each_entry(efiobj, &efi_obj_list, link) {
1356 if (!efi_search(search_type, protocol, search_key, efiobj))
1357 size += sizeof(void *);
1360 if (*buffer_size < size) {
1361 *buffer_size = size;
1362 return EFI_BUFFER_TOO_SMALL;
1365 *buffer_size = size;
1367 return EFI_NOT_FOUND;
1369 /* Then fill the array */
1370 list_for_each_entry(efiobj, &efi_obj_list, link) {
1371 if (!efi_search(search_type, protocol, search_key, efiobj))
1379 * efi_locate_handle_ext() - locate handles implementing a protocol.
1380 * @search_type: selection criterion
1381 * @protocol: GUID of the protocol
1382 * @search_key: registration key
1383 * @buffer_size: size of the buffer to receive the handles in bytes
1384 * @buffer: buffer to receive the relevant handles
1386 * This function implements the LocateHandle service.
1388 * See the Unified Extensible Firmware Interface (UEFI) specification for
1391 * Return: 0 if the handle implements the protocol
1393 static efi_status_t EFIAPI efi_locate_handle_ext(
1394 enum efi_locate_search_type search_type,
1395 const efi_guid_t *protocol, void *search_key,
1396 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1398 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1399 buffer_size, buffer);
1401 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1402 buffer_size, buffer));
1406 * efi_remove_configuration_table() - collapses configuration table entries,
1409 * @i: index of the table entry to be removed
1411 static void efi_remove_configuration_table(int i)
1413 struct efi_configuration_table *this = &systab.tables[i];
1414 struct efi_configuration_table *next = &systab.tables[i + 1];
1415 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1417 memmove(this, next, (ulong)end - (ulong)next);
1422 * efi_install_configuration_table() - adds, updates, or removes a
1423 * configuration table
1424 * @guid: GUID of the installed table
1425 * @table: table to be installed
1427 * This function is used for internal calls. For the API implementation of the
1428 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1430 * Return: status code
1432 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1435 struct efi_event *evt;
1439 return EFI_INVALID_PARAMETER;
1441 /* Check for GUID override */
1442 for (i = 0; i < systab.nr_tables; i++) {
1443 if (!guidcmp(guid, &systab.tables[i].guid)) {
1445 systab.tables[i].table = table;
1447 efi_remove_configuration_table(i);
1453 return EFI_NOT_FOUND;
1455 /* No override, check for overflow */
1456 if (i >= EFI_MAX_CONFIGURATION_TABLES)
1457 return EFI_OUT_OF_RESOURCES;
1459 /* Add a new entry */
1460 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1461 systab.tables[i].table = table;
1462 systab.nr_tables = i + 1;
1465 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1466 efi_update_table_header_crc32(&systab.hdr);
1468 /* Notify that the configuration table was changed */
1469 list_for_each_entry(evt, &efi_events, link) {
1470 if (evt->group && !guidcmp(evt->group, guid)) {
1471 efi_signal_event(evt, false);
1480 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1481 * configuration table.
1482 * @guid: GUID of the installed table
1483 * @table: table to be installed
1485 * This function implements the InstallConfigurationTable service.
1487 * See the Unified Extensible Firmware Interface (UEFI) specification for
1490 * Return: status code
1492 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1495 EFI_ENTRY("%pUl, %p", guid, table);
1496 return EFI_EXIT(efi_install_configuration_table(guid, table));
1500 * efi_setup_loaded_image() - initialize a loaded image
1502 * Initialize a loaded_image_info and loaded_image_info object with correct
1503 * protocols, boot-device, etc.
1505 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1508 * @device_path: device path of the loaded image
1509 * @file_path: file path of the loaded image
1510 * @handle_ptr: handle of the loaded image
1511 * @info_ptr: loaded image protocol
1512 * Return: status code
1514 efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1515 struct efi_device_path *file_path,
1516 struct efi_loaded_image_obj **handle_ptr,
1517 struct efi_loaded_image **info_ptr)
1520 struct efi_loaded_image *info = NULL;
1521 struct efi_loaded_image_obj *obj = NULL;
1523 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1527 info = calloc(1, sizeof(*info));
1529 return EFI_OUT_OF_RESOURCES;
1530 obj = calloc(1, sizeof(*obj));
1533 return EFI_OUT_OF_RESOURCES;
1536 /* Add internal object to object list */
1537 efi_add_handle(&obj->header);
1539 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1540 info->file_path = file_path;
1541 info->system_table = &systab;
1544 info->device_handle = efi_dp_find_obj(device_path, NULL);
1546 * When asking for the device path interface, return
1547 * bootefi_device_path
1549 ret = efi_add_protocol(&obj->header,
1550 &efi_guid_device_path, device_path);
1551 if (ret != EFI_SUCCESS)
1556 * When asking for the loaded_image interface, just
1557 * return handle which points to loaded_image_info
1559 ret = efi_add_protocol(&obj->header,
1560 &efi_guid_loaded_image, info);
1561 if (ret != EFI_SUCCESS)
1564 #if CONFIG_IS_ENABLED(EFI_LOADER_HII)
1565 ret = efi_add_protocol(&obj->header,
1566 &efi_guid_hii_string_protocol,
1567 (void *)&efi_hii_string);
1568 if (ret != EFI_SUCCESS)
1571 ret = efi_add_protocol(&obj->header,
1572 &efi_guid_hii_database_protocol,
1573 (void *)&efi_hii_database);
1574 if (ret != EFI_SUCCESS)
1577 ret = efi_add_protocol(&obj->header,
1578 &efi_guid_hii_config_routing_protocol,
1579 (void *)&efi_hii_config_routing);
1580 if (ret != EFI_SUCCESS)
1589 printf("ERROR: Failure to install protocols for loaded image\n");
1590 efi_delete_handle(&obj->header);
1596 * efi_load_image_from_path() - load an image using a file path
1598 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1599 * callers obligation to update the memory type as needed.
1601 * @file_path: the path of the image to load
1602 * @buffer: buffer containing the loaded image
1603 * @size: size of the loaded image
1604 * Return: status code
1606 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1607 void **buffer, efi_uintn_t *size)
1609 struct efi_file_info *info = NULL;
1610 struct efi_file_handle *f;
1611 static efi_status_t ret;
1615 /* In case of failure nothing is returned */
1620 f = efi_file_from_path(file_path);
1622 return EFI_DEVICE_ERROR;
1626 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1628 if (ret != EFI_BUFFER_TOO_SMALL) {
1629 ret = EFI_DEVICE_ERROR;
1634 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1636 if (ret != EFI_SUCCESS)
1640 * When reading the file we do not yet know if it contains an
1641 * application, a boottime driver, or a runtime driver. So here we
1642 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1643 * update the reservation according to the image type.
1645 bs = info->file_size;
1646 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1647 EFI_BOOT_SERVICES_DATA,
1648 efi_size_in_pages(bs), &addr);
1649 if (ret != EFI_SUCCESS) {
1650 ret = EFI_OUT_OF_RESOURCES;
1655 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1656 if (ret != EFI_SUCCESS)
1657 efi_free_pages(addr, efi_size_in_pages(bs));
1658 *buffer = (void *)(uintptr_t)addr;
1661 EFI_CALL(f->close(f));
1667 * efi_load_image() - load an EFI image into memory
1668 * @boot_policy: true for request originating from the boot manager
1669 * @parent_image: the caller's image handle
1670 * @file_path: the path of the image to load
1671 * @source_buffer: memory location from which the image is installed
1672 * @source_size: size of the memory area from which the image is installed
1673 * @image_handle: handle for the newly installed image
1675 * This function implements the LoadImage service.
1677 * See the Unified Extensible Firmware Interface (UEFI) specification
1680 * Return: status code
1682 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1683 efi_handle_t parent_image,
1684 struct efi_device_path *file_path,
1685 void *source_buffer,
1686 efi_uintn_t source_size,
1687 efi_handle_t *image_handle)
1689 struct efi_device_path *dp, *fp;
1690 struct efi_loaded_image *info = NULL;
1691 struct efi_loaded_image_obj **image_obj =
1692 (struct efi_loaded_image_obj **)image_handle;
1696 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1697 file_path, source_buffer, source_size, image_handle);
1699 if (!image_handle || !parent_image) {
1700 ret = EFI_INVALID_PARAMETER;
1704 if (!source_buffer && !file_path) {
1705 ret = EFI_NOT_FOUND;
1709 if (!source_buffer) {
1710 ret = efi_load_image_from_path(file_path, &dest_buffer,
1712 if (ret != EFI_SUCCESS)
1715 * split file_path which contains both the device and
1718 efi_dp_split_file_path(file_path, &dp, &fp);
1720 /* In this case, file_path is the "device" path, i.e.
1721 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1723 dest_buffer = source_buffer;
1727 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1728 if (ret == EFI_SUCCESS)
1729 ret = efi_load_pe(*image_obj, dest_buffer, info);
1731 /* Release buffer to which file was loaded */
1732 efi_free_pages((uintptr_t)dest_buffer,
1733 efi_size_in_pages(source_size));
1734 if (ret == EFI_SUCCESS) {
1735 info->system_table = &systab;
1736 info->parent_handle = parent_image;
1738 /* The image is invalid. Release all associated resources. */
1739 efi_delete_handle(*image_handle);
1740 *image_handle = NULL;
1744 return EFI_EXIT(ret);
1748 * efi_start_image() - call the entry point of an image
1749 * @image_handle: handle of the image
1750 * @exit_data_size: size of the buffer
1751 * @exit_data: buffer to receive the exit data of the called image
1753 * This function implements the StartImage service.
1755 * See the Unified Extensible Firmware Interface (UEFI) specification for
1758 * Return: status code
1760 efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1761 efi_uintn_t *exit_data_size,
1764 struct efi_loaded_image_obj *image_obj =
1765 (struct efi_loaded_image_obj *)image_handle;
1768 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1770 efi_is_direct_boot = false;
1772 /* call the image! */
1773 if (setjmp(&image_obj->exit_jmp)) {
1775 * We called the entry point of the child image with EFI_CALL
1776 * in the lines below. The child image called the Exit() boot
1777 * service efi_exit() which executed the long jump that brought
1778 * us to the current line. This implies that the second half
1779 * of the EFI_CALL macro has not been executed.
1783 * efi_exit() called efi_restore_gd(). We have to undo this
1784 * otherwise __efi_entry_check() will put the wrong value into
1790 * To get ready to call EFI_EXIT below we have to execute the
1791 * missed out steps of EFI_CALL.
1793 assert(__efi_entry_check());
1794 debug("%sEFI: %lu returned by started image\n",
1795 __efi_nesting_dec(),
1796 (unsigned long)((uintptr_t)image_obj->exit_status &
1798 return EFI_EXIT(image_obj->exit_status);
1801 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
1804 * Usually UEFI applications call Exit() instead of returning.
1805 * But because the world doesn't consist of ponies and unicorns,
1806 * we're happy to emulate that behavior on behalf of a payload
1809 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
1813 * efi_exit() - leave an EFI application or driver
1814 * @image_handle: handle of the application or driver that is exiting
1815 * @exit_status: status code
1816 * @exit_data_size: size of the buffer in bytes
1817 * @exit_data: buffer with data describing an error
1819 * This function implements the Exit service.
1821 * See the Unified Extensible Firmware Interface (UEFI) specification for
1824 * Return: status code
1826 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1827 efi_status_t exit_status,
1828 efi_uintn_t exit_data_size,
1832 * TODO: We should call the unload procedure of the loaded
1835 struct efi_loaded_image_obj *image_obj =
1836 (struct efi_loaded_image_obj *)image_handle;
1838 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
1839 exit_data_size, exit_data);
1841 /* Make sure entry/exit counts for EFI world cross-overs match */
1842 EFI_EXIT(exit_status);
1845 * But longjmp out with the U-Boot gd, not the application's, as
1846 * the other end is a setjmp call inside EFI context.
1850 image_obj->exit_status = exit_status;
1851 longjmp(&image_obj->exit_jmp, 1);
1853 panic("EFI application exited");
1857 * efi_unload_image() - unload an EFI image
1858 * @image_handle: handle of the image to be unloaded
1860 * This function implements the UnloadImage service.
1862 * See the Unified Extensible Firmware Interface (UEFI) specification for
1865 * Return: status code
1867 static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1869 struct efi_object *efiobj;
1871 EFI_ENTRY("%p", image_handle);
1872 efiobj = efi_search_obj(image_handle);
1874 list_del(&efiobj->link);
1876 return EFI_EXIT(EFI_SUCCESS);
1880 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1882 static void efi_exit_caches(void)
1884 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1886 * Grub on 32bit ARM needs to have caches disabled before jumping into
1887 * a zImage, but does not know of all cache layers. Give it a hand.
1889 if (efi_is_direct_boot)
1890 cleanup_before_linux();
1895 * efi_exit_boot_services() - stop all boot services
1896 * @image_handle: handle of the loaded image
1897 * @map_key: key of the memory map
1899 * This function implements the ExitBootServices service.
1901 * See the Unified Extensible Firmware Interface (UEFI) specification
1904 * All timer events are disabled. For exit boot services events the
1905 * notification function is called. The boot services are disabled in the
1908 * Return: status code
1910 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1911 unsigned long map_key)
1913 struct efi_event *evt;
1915 EFI_ENTRY("%p, %ld", image_handle, map_key);
1917 /* Check that the caller has read the current memory map */
1918 if (map_key != efi_memory_map_key)
1919 return EFI_INVALID_PARAMETER;
1921 /* Make sure that notification functions are not called anymore */
1922 efi_tpl = TPL_HIGH_LEVEL;
1924 /* Check if ExitBootServices has already been called */
1925 if (!systab.boottime)
1926 return EFI_EXIT(EFI_SUCCESS);
1928 /* Add related events to the event group */
1929 list_for_each_entry(evt, &efi_events, link) {
1930 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1931 evt->group = &efi_guid_event_group_exit_boot_services;
1933 /* Notify that ExitBootServices is invoked. */
1934 list_for_each_entry(evt, &efi_events, link) {
1936 !guidcmp(evt->group,
1937 &efi_guid_event_group_exit_boot_services)) {
1938 efi_signal_event(evt, false);
1943 /* TODO: Should persist EFI variables here */
1945 board_quiesce_devices();
1947 /* Fix up caches for EFI payloads if necessary */
1950 /* This stops all lingering devices */
1951 bootm_disable_interrupts();
1953 /* Disable boot time services */
1954 systab.con_in_handle = NULL;
1955 systab.con_in = NULL;
1956 systab.con_out_handle = NULL;
1957 systab.con_out = NULL;
1958 systab.stderr_handle = NULL;
1959 systab.std_err = NULL;
1960 systab.boottime = NULL;
1962 /* Recalculate CRC32 */
1963 efi_update_table_header_crc32(&systab.hdr);
1965 /* Give the payload some time to boot */
1966 efi_set_watchdog(0);
1969 return EFI_EXIT(EFI_SUCCESS);
1973 * efi_get_next_monotonic_count() - get next value of the counter
1974 * @count: returned value of the counter
1976 * This function implements the NextMonotonicCount service.
1978 * See the Unified Extensible Firmware Interface (UEFI) specification for
1981 * Return: status code
1983 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1985 static uint64_t mono;
1987 EFI_ENTRY("%p", count);
1989 return EFI_EXIT(EFI_SUCCESS);
1993 * efi_stall() - sleep
1994 * @microseconds: period to sleep in microseconds
1996 * This function implements the Stall service.
1998 * See the Unified Extensible Firmware Interface (UEFI) specification for
2001 * Return: status code
2003 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2005 EFI_ENTRY("%ld", microseconds);
2006 udelay(microseconds);
2007 return EFI_EXIT(EFI_SUCCESS);
2011 * efi_set_watchdog_timer() - reset the watchdog timer
2012 * @timeout: seconds before reset by watchdog
2013 * @watchdog_code: code to be logged when resetting
2014 * @data_size: size of buffer in bytes
2015 * @watchdog_data: buffer with data describing the reset reason
2017 * This function implements the SetWatchdogTimer service.
2019 * See the Unified Extensible Firmware Interface (UEFI) specification for
2022 * Return: status code
2024 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2025 uint64_t watchdog_code,
2026 unsigned long data_size,
2027 uint16_t *watchdog_data)
2029 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
2030 data_size, watchdog_data);
2031 return EFI_EXIT(efi_set_watchdog(timeout));
2035 * efi_close_protocol() - close a protocol
2036 * @handle: handle on which the protocol shall be closed
2037 * @protocol: GUID of the protocol to close
2038 * @agent_handle: handle of the driver
2039 * @controller_handle: handle of the controller
2041 * This function implements the CloseProtocol service.
2043 * See the Unified Extensible Firmware Interface (UEFI) specification for
2046 * Return: status code
2048 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2049 const efi_guid_t *protocol,
2050 efi_handle_t agent_handle,
2051 efi_handle_t controller_handle)
2053 struct efi_handler *handler;
2054 struct efi_open_protocol_info_item *item;
2055 struct efi_open_protocol_info_item *pos;
2058 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
2061 if (!agent_handle) {
2062 r = EFI_INVALID_PARAMETER;
2065 r = efi_search_protocol(handle, protocol, &handler);
2066 if (r != EFI_SUCCESS)
2070 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2071 if (item->info.agent_handle == agent_handle &&
2072 item->info.controller_handle == controller_handle) {
2073 efi_delete_open_info(item);
2083 * efi_open_protocol_information() - provide information about then open status
2084 * of a protocol on a handle
2085 * @handle: handle for which the information shall be retrieved
2086 * @protocol: GUID of the protocol
2087 * @entry_buffer: buffer to receive the open protocol information
2088 * @entry_count: number of entries available in the buffer
2090 * This function implements the OpenProtocolInformation service.
2092 * See the Unified Extensible Firmware Interface (UEFI) specification for
2095 * Return: status code
2097 static efi_status_t EFIAPI efi_open_protocol_information(
2098 efi_handle_t handle, const efi_guid_t *protocol,
2099 struct efi_open_protocol_info_entry **entry_buffer,
2100 efi_uintn_t *entry_count)
2102 unsigned long buffer_size;
2103 unsigned long count;
2104 struct efi_handler *handler;
2105 struct efi_open_protocol_info_item *item;
2108 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2111 /* Check parameters */
2112 if (!entry_buffer) {
2113 r = EFI_INVALID_PARAMETER;
2116 r = efi_search_protocol(handle, protocol, &handler);
2117 if (r != EFI_SUCCESS)
2122 list_for_each_entry(item, &handler->open_infos, link) {
2123 if (item->info.open_count)
2126 *entry_count = count;
2127 *entry_buffer = NULL;
2134 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2135 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2136 (void **)entry_buffer);
2137 if (r != EFI_SUCCESS)
2139 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2140 if (item->info.open_count)
2141 (*entry_buffer)[--count] = item->info;
2148 * efi_protocols_per_handle() - get protocols installed on a handle
2149 * @handle: handle for which the information is retrieved
2150 * @protocol_buffer: buffer with protocol GUIDs
2151 * @protocol_buffer_count: number of entries in the buffer
2153 * This function implements the ProtocolsPerHandleService.
2155 * See the Unified Extensible Firmware Interface (UEFI) specification for
2158 * Return: status code
2160 static efi_status_t EFIAPI efi_protocols_per_handle(
2161 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2162 efi_uintn_t *protocol_buffer_count)
2164 unsigned long buffer_size;
2165 struct efi_object *efiobj;
2166 struct list_head *protocol_handle;
2169 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2170 protocol_buffer_count);
2172 if (!handle || !protocol_buffer || !protocol_buffer_count)
2173 return EFI_EXIT(EFI_INVALID_PARAMETER);
2175 *protocol_buffer = NULL;
2176 *protocol_buffer_count = 0;
2178 efiobj = efi_search_obj(handle);
2180 return EFI_EXIT(EFI_INVALID_PARAMETER);
2182 /* Count protocols */
2183 list_for_each(protocol_handle, &efiobj->protocols) {
2184 ++*protocol_buffer_count;
2188 if (*protocol_buffer_count) {
2191 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2192 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2193 (void **)protocol_buffer);
2194 if (r != EFI_SUCCESS)
2196 list_for_each(protocol_handle, &efiobj->protocols) {
2197 struct efi_handler *protocol;
2199 protocol = list_entry(protocol_handle,
2200 struct efi_handler, link);
2201 (*protocol_buffer)[j] = (void *)protocol->guid;
2206 return EFI_EXIT(EFI_SUCCESS);
2210 * efi_locate_handle_buffer() - locate handles implementing a protocol
2211 * @search_type: selection criterion
2212 * @protocol: GUID of the protocol
2213 * @search_key: registration key
2214 * @no_handles: number of returned handles
2215 * @buffer: buffer with the returned handles
2217 * This function implements the LocateHandleBuffer service.
2219 * See the Unified Extensible Firmware Interface (UEFI) specification for
2222 * Return: status code
2224 static efi_status_t EFIAPI efi_locate_handle_buffer(
2225 enum efi_locate_search_type search_type,
2226 const efi_guid_t *protocol, void *search_key,
2227 efi_uintn_t *no_handles, efi_handle_t **buffer)
2230 efi_uintn_t buffer_size = 0;
2232 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2233 no_handles, buffer);
2235 if (!no_handles || !buffer) {
2236 r = EFI_INVALID_PARAMETER;
2241 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2243 if (r != EFI_BUFFER_TOO_SMALL)
2245 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2247 if (r != EFI_SUCCESS)
2249 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2251 if (r == EFI_SUCCESS)
2252 *no_handles = buffer_size / sizeof(efi_handle_t);
2258 * efi_locate_protocol() - find an interface implementing a protocol
2259 * @protocol: GUID of the protocol
2260 * @registration: registration key passed to the notification function
2261 * @protocol_interface: interface implementing the protocol
2263 * This function implements the LocateProtocol service.
2265 * See the Unified Extensible Firmware Interface (UEFI) specification for
2268 * Return: status code
2270 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2272 void **protocol_interface)
2274 struct list_head *lhandle;
2277 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2279 if (!protocol || !protocol_interface)
2280 return EFI_EXIT(EFI_INVALID_PARAMETER);
2282 list_for_each(lhandle, &efi_obj_list) {
2283 struct efi_object *efiobj;
2284 struct efi_handler *handler;
2286 efiobj = list_entry(lhandle, struct efi_object, link);
2288 ret = efi_search_protocol(efiobj, protocol, &handler);
2289 if (ret == EFI_SUCCESS) {
2290 *protocol_interface = handler->protocol_interface;
2291 return EFI_EXIT(EFI_SUCCESS);
2294 *protocol_interface = NULL;
2296 return EFI_EXIT(EFI_NOT_FOUND);
2300 * efi_locate_device_path() - Get the device path and handle of an device
2301 * implementing a protocol
2302 * @protocol: GUID of the protocol
2303 * @device_path: device path
2304 * @device: handle of the device
2306 * This function implements the LocateDevicePath service.
2308 * See the Unified Extensible Firmware Interface (UEFI) specification for
2311 * Return: status code
2313 static efi_status_t EFIAPI efi_locate_device_path(
2314 const efi_guid_t *protocol,
2315 struct efi_device_path **device_path,
2316 efi_handle_t *device)
2318 struct efi_device_path *dp;
2320 struct efi_handler *handler;
2321 efi_handle_t *handles;
2323 size_t len_best = 0;
2324 efi_uintn_t no_handles;
2328 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2330 if (!protocol || !device_path || !*device_path || !device) {
2331 ret = EFI_INVALID_PARAMETER;
2335 /* Find end of device path */
2336 len = efi_dp_instance_size(*device_path);
2338 /* Get all handles implementing the protocol */
2339 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2340 &no_handles, &handles));
2341 if (ret != EFI_SUCCESS)
2344 for (i = 0; i < no_handles; ++i) {
2345 /* Find the device path protocol */
2346 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2348 if (ret != EFI_SUCCESS)
2350 dp = (struct efi_device_path *)handler->protocol_interface;
2351 len_dp = efi_dp_instance_size(dp);
2353 * This handle can only be a better fit
2354 * if its device path length is longer than the best fit and
2355 * if its device path length is shorter of equal the searched
2358 if (len_dp <= len_best || len_dp > len)
2360 /* Check if dp is a subpath of device_path */
2361 if (memcmp(*device_path, dp, len_dp))
2363 *device = handles[i];
2367 remainder = (u8 *)*device_path + len_best;
2368 *device_path = (struct efi_device_path *)remainder;
2371 ret = EFI_NOT_FOUND;
2374 return EFI_EXIT(ret);
2378 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2380 * @handle: handle on which the protocol interfaces shall be installed
2381 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2384 * This function implements the MultipleProtocolInterfaces service.
2386 * See the Unified Extensible Firmware Interface (UEFI) specification for
2389 * Return: status code
2391 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2392 (efi_handle_t *handle, ...)
2394 EFI_ENTRY("%p", handle);
2397 const efi_guid_t *protocol;
2398 void *protocol_interface;
2399 efi_status_t r = EFI_SUCCESS;
2403 return EFI_EXIT(EFI_INVALID_PARAMETER);
2405 efi_va_start(argptr, handle);
2407 protocol = efi_va_arg(argptr, efi_guid_t*);
2410 protocol_interface = efi_va_arg(argptr, void*);
2411 r = EFI_CALL(efi_install_protocol_interface(
2413 EFI_NATIVE_INTERFACE,
2414 protocol_interface));
2415 if (r != EFI_SUCCESS)
2420 if (r == EFI_SUCCESS)
2423 /* If an error occurred undo all changes. */
2424 efi_va_start(argptr, handle);
2426 protocol = efi_va_arg(argptr, efi_guid_t*);
2427 protocol_interface = efi_va_arg(argptr, void*);
2428 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2429 protocol_interface));
2437 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2439 * @handle: handle from which the protocol interfaces shall be removed
2440 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2443 * This function implements the UninstallMultipleProtocolInterfaces service.
2445 * See the Unified Extensible Firmware Interface (UEFI) specification for
2448 * Return: status code
2450 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2451 efi_handle_t handle, ...)
2453 EFI_ENTRY("%p", handle);
2456 const efi_guid_t *protocol;
2457 void *protocol_interface;
2458 efi_status_t r = EFI_SUCCESS;
2462 return EFI_EXIT(EFI_INVALID_PARAMETER);
2464 efi_va_start(argptr, handle);
2466 protocol = efi_va_arg(argptr, efi_guid_t*);
2469 protocol_interface = efi_va_arg(argptr, void*);
2470 r = efi_uninstall_protocol(handle, protocol,
2471 protocol_interface);
2472 if (r != EFI_SUCCESS)
2477 if (r == EFI_SUCCESS) {
2478 /* If the last protocol has been removed, delete the handle. */
2479 if (list_empty(&handle->protocols)) {
2480 list_del(&handle->link);
2486 /* If an error occurred undo all changes. */
2487 efi_va_start(argptr, handle);
2489 protocol = efi_va_arg(argptr, efi_guid_t*);
2490 protocol_interface = efi_va_arg(argptr, void*);
2491 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2492 EFI_NATIVE_INTERFACE,
2493 protocol_interface));
2497 /* In case of an error always return EFI_INVALID_PARAMETER */
2498 return EFI_EXIT(EFI_INVALID_PARAMETER);
2502 * efi_calculate_crc32() - calculate cyclic redundancy code
2503 * @data: buffer with data
2504 * @data_size: size of buffer in bytes
2505 * @crc32_p: cyclic redundancy code
2507 * This function implements the CalculateCrc32 service.
2509 * See the Unified Extensible Firmware Interface (UEFI) specification for
2512 * Return: status code
2514 static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2515 efi_uintn_t data_size,
2518 EFI_ENTRY("%p, %zu", data, data_size);
2519 *crc32_p = crc32(0, data, data_size);
2520 return EFI_EXIT(EFI_SUCCESS);
2524 * efi_copy_mem() - copy memory
2525 * @destination: destination of the copy operation
2526 * @source: source of the copy operation
2527 * @length: number of bytes to copy
2529 * This function implements the CopyMem service.
2531 * See the Unified Extensible Firmware Interface (UEFI) specification for
2534 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2537 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2538 memmove(destination, source, length);
2539 EFI_EXIT(EFI_SUCCESS);
2543 * efi_set_mem() - Fill memory with a byte value.
2544 * @buffer: buffer to fill
2545 * @size: size of buffer in bytes
2546 * @value: byte to copy to the buffer
2548 * This function implements the SetMem service.
2550 * See the Unified Extensible Firmware Interface (UEFI) specification for
2553 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2555 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2556 memset(buffer, value, size);
2557 EFI_EXIT(EFI_SUCCESS);
2561 * efi_protocol_open() - open protocol interface on a handle
2562 * @handler: handler of a protocol
2563 * @protocol_interface: interface implementing the protocol
2564 * @agent_handle: handle of the driver
2565 * @controller_handle: handle of the controller
2566 * @attributes: attributes indicating how to open the protocol
2568 * Return: status code
2570 static efi_status_t efi_protocol_open(
2571 struct efi_handler *handler,
2572 void **protocol_interface, void *agent_handle,
2573 void *controller_handle, uint32_t attributes)
2575 struct efi_open_protocol_info_item *item;
2576 struct efi_open_protocol_info_entry *match = NULL;
2577 bool opened_by_driver = false;
2578 bool opened_exclusive = false;
2580 /* If there is no agent, only return the interface */
2584 /* For TEST_PROTOCOL ignore interface attribute */
2585 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2586 *protocol_interface = NULL;
2589 * Check if the protocol is already opened by a driver with the same
2590 * attributes or opened exclusively
2592 list_for_each_entry(item, &handler->open_infos, link) {
2593 if (item->info.agent_handle == agent_handle) {
2594 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2595 (item->info.attributes == attributes))
2596 return EFI_ALREADY_STARTED;
2598 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2599 opened_exclusive = true;
2602 /* Only one controller can open the protocol exclusively */
2603 if (opened_exclusive && attributes &
2604 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2605 return EFI_ACCESS_DENIED;
2607 /* Prepare exclusive opening */
2608 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2609 /* Try to disconnect controllers */
2610 list_for_each_entry(item, &handler->open_infos, link) {
2611 if (item->info.attributes ==
2612 EFI_OPEN_PROTOCOL_BY_DRIVER)
2613 EFI_CALL(efi_disconnect_controller(
2614 item->info.controller_handle,
2615 item->info.agent_handle,
2618 opened_by_driver = false;
2619 /* Check if all controllers are disconnected */
2620 list_for_each_entry(item, &handler->open_infos, link) {
2621 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2622 opened_by_driver = true;
2624 /* Only one controller can be connected */
2625 if (opened_by_driver)
2626 return EFI_ACCESS_DENIED;
2629 /* Find existing entry */
2630 list_for_each_entry(item, &handler->open_infos, link) {
2631 if (item->info.agent_handle == agent_handle &&
2632 item->info.controller_handle == controller_handle)
2633 match = &item->info;
2635 /* None found, create one */
2637 match = efi_create_open_info(handler);
2639 return EFI_OUT_OF_RESOURCES;
2642 match->agent_handle = agent_handle;
2643 match->controller_handle = controller_handle;
2644 match->attributes = attributes;
2645 match->open_count++;
2648 /* For TEST_PROTOCOL ignore interface attribute. */
2649 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2650 *protocol_interface = handler->protocol_interface;
2656 * efi_open_protocol() - open protocol interface on a handle
2657 * @handle: handle on which the protocol shall be opened
2658 * @protocol: GUID of the protocol
2659 * @protocol_interface: interface implementing the protocol
2660 * @agent_handle: handle of the driver
2661 * @controller_handle: handle of the controller
2662 * @attributes: attributes indicating how to open the protocol
2664 * This function implements the OpenProtocol interface.
2666 * See the Unified Extensible Firmware Interface (UEFI) specification for
2669 * Return: status code
2671 static efi_status_t EFIAPI efi_open_protocol
2672 (efi_handle_t handle, const efi_guid_t *protocol,
2673 void **protocol_interface, efi_handle_t agent_handle,
2674 efi_handle_t controller_handle, uint32_t attributes)
2676 struct efi_handler *handler;
2677 efi_status_t r = EFI_INVALID_PARAMETER;
2679 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2680 protocol_interface, agent_handle, controller_handle,
2683 if (!handle || !protocol ||
2684 (!protocol_interface && attributes !=
2685 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2689 switch (attributes) {
2690 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2691 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2692 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2694 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2695 if (controller_handle == handle)
2698 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2699 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2700 /* Check that the controller handle is valid */
2701 if (!efi_search_obj(controller_handle))
2704 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2705 /* Check that the agent handle is valid */
2706 if (!efi_search_obj(agent_handle))
2713 r = efi_search_protocol(handle, protocol, &handler);
2714 if (r != EFI_SUCCESS)
2717 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2718 controller_handle, attributes);
2724 * efi_handle_protocol() - get interface of a protocol on a handle
2725 * @handle: handle on which the protocol shall be opened
2726 * @protocol: GUID of the protocol
2727 * @protocol_interface: interface implementing the protocol
2729 * This function implements the HandleProtocol service.
2731 * See the Unified Extensible Firmware Interface (UEFI) specification for
2734 * Return: status code
2736 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2737 const efi_guid_t *protocol,
2738 void **protocol_interface)
2740 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2741 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2745 * efi_bind_controller() - bind a single driver to a controller
2746 * @controller_handle: controller handle
2747 * @driver_image_handle: driver handle
2748 * @remain_device_path: remaining path
2750 * Return: status code
2752 static efi_status_t efi_bind_controller(
2753 efi_handle_t controller_handle,
2754 efi_handle_t driver_image_handle,
2755 struct efi_device_path *remain_device_path)
2757 struct efi_driver_binding_protocol *binding_protocol;
2760 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2761 &efi_guid_driver_binding_protocol,
2762 (void **)&binding_protocol,
2763 driver_image_handle, NULL,
2764 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2765 if (r != EFI_SUCCESS)
2767 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2769 remain_device_path));
2770 if (r == EFI_SUCCESS)
2771 r = EFI_CALL(binding_protocol->start(binding_protocol,
2773 remain_device_path));
2774 EFI_CALL(efi_close_protocol(driver_image_handle,
2775 &efi_guid_driver_binding_protocol,
2776 driver_image_handle, NULL));
2781 * efi_connect_single_controller() - connect a single driver to a controller
2782 * @controller_handle: controller
2783 * @driver_image_handle: driver
2784 * @remain_device_path: remaining path
2786 * Return: status code
2788 static efi_status_t efi_connect_single_controller(
2789 efi_handle_t controller_handle,
2790 efi_handle_t *driver_image_handle,
2791 struct efi_device_path *remain_device_path)
2793 efi_handle_t *buffer;
2797 size_t connected = 0;
2799 /* Get buffer with all handles with driver binding protocol */
2800 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2801 &efi_guid_driver_binding_protocol,
2802 NULL, &count, &buffer));
2803 if (r != EFI_SUCCESS)
2806 /* Context Override */
2807 if (driver_image_handle) {
2808 for (; *driver_image_handle; ++driver_image_handle) {
2809 for (i = 0; i < count; ++i) {
2810 if (buffer[i] == *driver_image_handle) {
2812 r = efi_bind_controller(
2814 *driver_image_handle,
2815 remain_device_path);
2817 * For drivers that do not support the
2818 * controller or are already connected
2819 * we receive an error code here.
2821 if (r == EFI_SUCCESS)
2829 * TODO: Some overrides are not yet implemented:
2830 * - Platform Driver Override
2831 * - Driver Family Override Search
2832 * - Bus Specific Driver Override
2835 /* Driver Binding Search */
2836 for (i = 0; i < count; ++i) {
2838 r = efi_bind_controller(controller_handle,
2840 remain_device_path);
2841 if (r == EFI_SUCCESS)
2846 efi_free_pool(buffer);
2848 return EFI_NOT_FOUND;
2853 * efi_connect_controller() - connect a controller to a driver
2854 * @controller_handle: handle of the controller
2855 * @driver_image_handle: handle of the driver
2856 * @remain_device_path: device path of a child controller
2857 * @recursive: true to connect all child controllers
2859 * This function implements the ConnectController service.
2861 * See the Unified Extensible Firmware Interface (UEFI) specification for
2864 * First all driver binding protocol handles are tried for binding drivers.
2865 * Afterwards all handles that have opened a protocol of the controller
2866 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2868 * Return: status code
2870 static efi_status_t EFIAPI efi_connect_controller(
2871 efi_handle_t controller_handle,
2872 efi_handle_t *driver_image_handle,
2873 struct efi_device_path *remain_device_path,
2877 efi_status_t ret = EFI_NOT_FOUND;
2878 struct efi_object *efiobj;
2880 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
2881 remain_device_path, recursive);
2883 efiobj = efi_search_obj(controller_handle);
2885 ret = EFI_INVALID_PARAMETER;
2889 r = efi_connect_single_controller(controller_handle,
2890 driver_image_handle,
2891 remain_device_path);
2892 if (r == EFI_SUCCESS)
2895 struct efi_handler *handler;
2896 struct efi_open_protocol_info_item *item;
2898 list_for_each_entry(handler, &efiobj->protocols, link) {
2899 list_for_each_entry(item, &handler->open_infos, link) {
2900 if (item->info.attributes &
2901 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2902 r = EFI_CALL(efi_connect_controller(
2903 item->info.controller_handle,
2904 driver_image_handle,
2907 if (r == EFI_SUCCESS)
2913 /* Check for child controller specified by end node */
2914 if (ret != EFI_SUCCESS && remain_device_path &&
2915 remain_device_path->type == DEVICE_PATH_TYPE_END)
2918 return EFI_EXIT(ret);
2922 * efi_reinstall_protocol_interface() - reinstall protocol interface
2923 * @handle: handle on which the protocol shall be reinstalled
2924 * @protocol: GUID of the protocol to be installed
2925 * @old_interface: interface to be removed
2926 * @new_interface: interface to be installed
2928 * This function implements the ReinstallProtocolInterface service.
2930 * See the Unified Extensible Firmware Interface (UEFI) specification for
2933 * The old interface is uninstalled. The new interface is installed.
2934 * Drivers are connected.
2936 * Return: status code
2938 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2939 efi_handle_t handle, const efi_guid_t *protocol,
2940 void *old_interface, void *new_interface)
2944 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2947 /* Uninstall protocol but do not delete handle */
2948 ret = efi_uninstall_protocol(handle, protocol, old_interface);
2949 if (ret != EFI_SUCCESS)
2952 /* Install the new protocol */
2953 ret = efi_add_protocol(handle, protocol, new_interface);
2955 * The UEFI spec does not specify what should happen to the handle
2956 * if in case of an error no protocol interface remains on the handle.
2957 * So let's do nothing here.
2959 if (ret != EFI_SUCCESS)
2962 * The returned status code has to be ignored.
2963 * Do not create an error if no suitable driver for the handle exists.
2965 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2967 return EFI_EXIT(ret);
2971 * efi_get_child_controllers() - get all child controllers associated to a driver
2972 * @efiobj: handle of the controller
2973 * @driver_handle: handle of the driver
2974 * @number_of_children: number of child controllers
2975 * @child_handle_buffer: handles of the the child controllers
2977 * The allocated buffer has to be freed with free().
2979 * Return: status code
2981 static efi_status_t efi_get_child_controllers(
2982 struct efi_object *efiobj,
2983 efi_handle_t driver_handle,
2984 efi_uintn_t *number_of_children,
2985 efi_handle_t **child_handle_buffer)
2987 struct efi_handler *handler;
2988 struct efi_open_protocol_info_item *item;
2989 efi_uintn_t count = 0, i;
2992 /* Count all child controller associations */
2993 list_for_each_entry(handler, &efiobj->protocols, link) {
2994 list_for_each_entry(item, &handler->open_infos, link) {
2995 if (item->info.agent_handle == driver_handle &&
2996 item->info.attributes &
2997 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3002 * Create buffer. In case of duplicate child controller assignments
3003 * the buffer will be too large. But that does not harm.
3005 *number_of_children = 0;
3006 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3007 if (!*child_handle_buffer)
3008 return EFI_OUT_OF_RESOURCES;
3009 /* Copy unique child handles */
3010 list_for_each_entry(handler, &efiobj->protocols, link) {
3011 list_for_each_entry(item, &handler->open_infos, link) {
3012 if (item->info.agent_handle == driver_handle &&
3013 item->info.attributes &
3014 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3015 /* Check this is a new child controller */
3017 for (i = 0; i < *number_of_children; ++i) {
3018 if ((*child_handle_buffer)[i] ==
3019 item->info.controller_handle)
3022 /* Copy handle to buffer */
3024 i = (*number_of_children)++;
3025 (*child_handle_buffer)[i] =
3026 item->info.controller_handle;
3035 * efi_disconnect_controller() - disconnect a controller from a driver
3036 * @controller_handle: handle of the controller
3037 * @driver_image_handle: handle of the driver
3038 * @child_handle: handle of the child to destroy
3040 * This function implements the DisconnectController service.
3042 * See the Unified Extensible Firmware Interface (UEFI) specification for
3045 * Return: status code
3047 static efi_status_t EFIAPI efi_disconnect_controller(
3048 efi_handle_t controller_handle,
3049 efi_handle_t driver_image_handle,
3050 efi_handle_t child_handle)
3052 struct efi_driver_binding_protocol *binding_protocol;
3053 efi_handle_t *child_handle_buffer = NULL;
3054 size_t number_of_children = 0;
3056 size_t stop_count = 0;
3057 struct efi_object *efiobj;
3059 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3062 efiobj = efi_search_obj(controller_handle);
3064 r = EFI_INVALID_PARAMETER;
3068 if (child_handle && !efi_search_obj(child_handle)) {
3069 r = EFI_INVALID_PARAMETER;
3073 /* If no driver handle is supplied, disconnect all drivers */
3074 if (!driver_image_handle) {
3075 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3079 /* Create list of child handles */
3081 number_of_children = 1;
3082 child_handle_buffer = &child_handle;
3084 efi_get_child_controllers(efiobj,
3085 driver_image_handle,
3086 &number_of_children,
3087 &child_handle_buffer);
3090 /* Get the driver binding protocol */
3091 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3092 &efi_guid_driver_binding_protocol,
3093 (void **)&binding_protocol,
3094 driver_image_handle, NULL,
3095 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3096 if (r != EFI_SUCCESS)
3098 /* Remove the children */
3099 if (number_of_children) {
3100 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3103 child_handle_buffer));
3104 if (r == EFI_SUCCESS)
3107 /* Remove the driver */
3109 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3112 if (r == EFI_SUCCESS)
3114 EFI_CALL(efi_close_protocol(driver_image_handle,
3115 &efi_guid_driver_binding_protocol,
3116 driver_image_handle, NULL));
3124 free(child_handle_buffer);
3128 static struct efi_boot_services efi_boot_services = {
3130 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3131 .revision = EFI_SPECIFICATION_VERSION,
3132 .headersize = sizeof(struct efi_boot_services),
3134 .raise_tpl = efi_raise_tpl,
3135 .restore_tpl = efi_restore_tpl,
3136 .allocate_pages = efi_allocate_pages_ext,
3137 .free_pages = efi_free_pages_ext,
3138 .get_memory_map = efi_get_memory_map_ext,
3139 .allocate_pool = efi_allocate_pool_ext,
3140 .free_pool = efi_free_pool_ext,
3141 .create_event = efi_create_event_ext,
3142 .set_timer = efi_set_timer_ext,
3143 .wait_for_event = efi_wait_for_event,
3144 .signal_event = efi_signal_event_ext,
3145 .close_event = efi_close_event,
3146 .check_event = efi_check_event,
3147 .install_protocol_interface = efi_install_protocol_interface,
3148 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3149 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3150 .handle_protocol = efi_handle_protocol,
3152 .register_protocol_notify = efi_register_protocol_notify,
3153 .locate_handle = efi_locate_handle_ext,
3154 .locate_device_path = efi_locate_device_path,
3155 .install_configuration_table = efi_install_configuration_table_ext,
3156 .load_image = efi_load_image,
3157 .start_image = efi_start_image,
3159 .unload_image = efi_unload_image,
3160 .exit_boot_services = efi_exit_boot_services,
3161 .get_next_monotonic_count = efi_get_next_monotonic_count,
3163 .set_watchdog_timer = efi_set_watchdog_timer,
3164 .connect_controller = efi_connect_controller,
3165 .disconnect_controller = efi_disconnect_controller,
3166 .open_protocol = efi_open_protocol,
3167 .close_protocol = efi_close_protocol,
3168 .open_protocol_information = efi_open_protocol_information,
3169 .protocols_per_handle = efi_protocols_per_handle,
3170 .locate_handle_buffer = efi_locate_handle_buffer,
3171 .locate_protocol = efi_locate_protocol,
3172 .install_multiple_protocol_interfaces =
3173 efi_install_multiple_protocol_interfaces,
3174 .uninstall_multiple_protocol_interfaces =
3175 efi_uninstall_multiple_protocol_interfaces,
3176 .calculate_crc32 = efi_calculate_crc32,
3177 .copy_mem = efi_copy_mem,
3178 .set_mem = efi_set_mem,
3179 .create_event_ex = efi_create_event_ex,
3182 static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3184 struct efi_system_table __efi_runtime_data systab = {
3186 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3187 .revision = EFI_SPECIFICATION_VERSION,
3188 .headersize = sizeof(struct efi_system_table),
3190 .fw_vendor = firmware_vendor,
3191 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3192 .con_in = (void *)&efi_con_in,
3193 .con_out = (void *)&efi_con_out,
3194 .std_err = (void *)&efi_con_out,
3195 .runtime = (void *)&efi_runtime_services,
3196 .boottime = (void *)&efi_boot_services,
3202 * efi_initialize_system_table() - Initialize system table
3204 * Return: status code
3206 efi_status_t efi_initialize_system_table(void)
3210 /* Allocate configuration table array */
3211 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3212 EFI_MAX_CONFIGURATION_TABLES *
3213 sizeof(struct efi_configuration_table),
3214 (void **)&systab.tables);
3216 /* Set CRC32 field in table headers */
3217 efi_update_table_header_crc32(&systab.hdr);
3218 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3219 efi_update_table_header_crc32(&efi_boot_services.hdr);