1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application boot time services
5 * Copyright (c) 2016 Alexander Graf
10 #include <dm/device.h>
12 #include <efi_loader.h>
18 #include <u-boot/crc.h>
21 #include <asm/global_data.h>
22 #include <asm/setjmp.h>
23 #include <linux/libfdt_env.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 /* Task priority level */
28 static efi_uintn_t efi_tpl = TPL_APPLICATION;
30 /* This list contains all the EFI objects our payload has access to */
31 LIST_HEAD(efi_obj_list);
33 /* List of all events */
34 __efi_runtime_data LIST_HEAD(efi_events);
36 /* List of queued events */
37 static LIST_HEAD(efi_event_queue);
39 /* Flag to disable timer activity in ExitBootServices() */
40 static bool timers_enabled = true;
42 /* Flag used by the selftest to avoid detaching devices in ExitBootServices() */
43 bool efi_st_keep_devices;
45 /* List of all events registered by RegisterProtocolNotify() */
46 static LIST_HEAD(efi_register_notify_events);
48 /* Handle of the currently executing image */
49 static efi_handle_t current_image;
51 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
53 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
54 * fixed when compiling U-Boot. However, the payload does not know about that
55 * restriction so we need to manually swap its and our view of that register on
56 * EFI callback entry/exit.
58 static volatile gd_t *efi_gd, *app_gd;
61 static efi_status_t efi_uninstall_protocol
62 (efi_handle_t handle, const efi_guid_t *protocol,
63 void *protocol_interface, bool preserve);
65 /* 1 if inside U-Boot code, 0 if inside EFI payload code */
66 static int entry_count = 1;
67 static int nesting_level;
68 /* GUID of the device tree table */
69 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
70 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
71 const efi_guid_t efi_guid_driver_binding_protocol =
72 EFI_DRIVER_BINDING_PROTOCOL_GUID;
74 /* event group ExitBootServices() invoked */
75 const efi_guid_t efi_guid_event_group_exit_boot_services =
76 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
77 /* event group before ExitBootServices() invoked */
78 const efi_guid_t efi_guid_event_group_before_exit_boot_services =
79 EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES;
80 /* event group SetVirtualAddressMap() invoked */
81 const efi_guid_t efi_guid_event_group_virtual_address_change =
82 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
83 /* event group memory map changed */
84 const efi_guid_t efi_guid_event_group_memory_map_change =
85 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
86 /* event group boot manager about to boot */
87 const efi_guid_t efi_guid_event_group_ready_to_boot =
88 EFI_EVENT_GROUP_READY_TO_BOOT;
89 /* event group ResetSystem() invoked (before ExitBootServices) */
90 const efi_guid_t efi_guid_event_group_reset_system =
91 EFI_EVENT_GROUP_RESET_SYSTEM;
92 /* event group return to efibootmgr */
93 const efi_guid_t efi_guid_event_group_return_to_efibootmgr =
94 EFI_EVENT_GROUP_RETURN_TO_EFIBOOTMGR;
95 /* GUIDs of the Load File and Load File2 protocols */
96 const efi_guid_t efi_guid_load_file_protocol = EFI_LOAD_FILE_PROTOCOL_GUID;
97 const efi_guid_t efi_guid_load_file2_protocol = EFI_LOAD_FILE2_PROTOCOL_GUID;
98 /* GUID of the SMBIOS table */
99 const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
101 static efi_status_t EFIAPI efi_disconnect_controller(
102 efi_handle_t controller_handle,
103 efi_handle_t driver_image_handle,
104 efi_handle_t child_handle);
107 efi_status_t EFIAPI efi_connect_controller(efi_handle_t controller_handle,
108 efi_handle_t *driver_image_handle,
109 struct efi_device_path *remain_device_path,
112 /* Called on every callback entry */
113 int __efi_entry_check(void)
115 int ret = entry_count++ == 0;
116 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
124 /* Called on every callback exit */
125 int __efi_exit_check(void)
127 int ret = --entry_count == 0;
128 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
135 * efi_save_gd() - save global data register
137 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
138 * As this register may be overwritten by an EFI payload we save it here
139 * and restore it on every callback entered.
141 * This function is called after relocation from initr_reloc_global_data().
143 void efi_save_gd(void)
145 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
151 * efi_restore_gd() - restore global data register
153 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
154 * Restore it after returning from the UEFI world to the value saved via
157 void efi_restore_gd(void)
159 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
160 /* Only restore if we're already in EFI context */
168 * indent_string() - returns a string for indenting with two spaces per level
169 * @level: indent level
171 * A maximum of ten indent levels is supported. Higher indent levels will be
174 * Return: A string for indenting with two spaces per level is
177 static const char *indent_string(int level)
179 const char *indent = " ";
180 const int max = strlen(indent);
182 level = min(max, level * 2);
183 return &indent[max - level];
186 const char *__efi_nesting(void)
188 return indent_string(nesting_level);
191 const char *__efi_nesting_inc(void)
193 return indent_string(nesting_level++);
196 const char *__efi_nesting_dec(void)
198 return indent_string(--nesting_level);
202 * efi_event_is_queued() - check if an event is queued
205 * Return: true if event is queued
207 static bool efi_event_is_queued(struct efi_event *event)
209 return !!event->queue_link.next;
213 * efi_purge_handle() - Clean the deleted handle from the various lists
214 * @handle: handle to remove
216 * Return: status code
218 static efi_status_t efi_purge_handle(efi_handle_t handle)
220 struct efi_register_notify_event *item;
222 if (!list_empty(&handle->protocols))
223 return EFI_ACCESS_DENIED;
224 /* The handle is about to be freed. Remove it from events */
225 list_for_each_entry(item, &efi_register_notify_events, link) {
226 struct efi_protocol_notification *hitem, *hnext;
228 list_for_each_entry_safe(hitem, hnext, &item->handles, link) {
229 if (handle == hitem->handle) {
230 list_del(&hitem->link);
235 /* The last protocol has been removed, delete the handle. */
236 list_del(&handle->link);
243 * efi_process_event_queue() - process event queue
245 static void efi_process_event_queue(void)
247 while (!list_empty(&efi_event_queue)) {
248 struct efi_event *event;
251 event = list_first_entry(&efi_event_queue, struct efi_event,
253 if (efi_tpl >= event->notify_tpl)
255 list_del(&event->queue_link);
256 event->queue_link.next = NULL;
257 event->queue_link.prev = NULL;
258 /* Events must be executed at the event's TPL */
260 efi_tpl = event->notify_tpl;
261 EFI_CALL_VOID(event->notify_function(event,
262 event->notify_context));
264 if (event->type == EVT_NOTIFY_SIGNAL)
265 event->is_signaled = 0;
270 * efi_queue_event() - queue an EFI event
271 * @event: event to signal
273 * This function queues the notification function of the event for future
277 static void efi_queue_event(struct efi_event *event)
279 struct efi_event *item;
281 if (!event->notify_function)
284 if (!efi_event_is_queued(event)) {
286 * Events must be notified in order of decreasing task priority
287 * level. Insert the new event accordingly.
289 list_for_each_entry(item, &efi_event_queue, queue_link) {
290 if (item->notify_tpl < event->notify_tpl) {
291 list_add_tail(&event->queue_link,
298 list_add_tail(&event->queue_link, &efi_event_queue);
299 efi_process_event_queue();
304 * is_valid_tpl() - check if the task priority level is valid
306 * @tpl: TPL level to check
307 * Return: status code
309 static efi_status_t is_valid_tpl(efi_uintn_t tpl)
312 case TPL_APPLICATION:
317 return EFI_INVALID_PARAMETER;
322 * efi_signal_event() - signal an EFI event
323 * @event: event to signal
325 * This function signals an event. If the event belongs to an event group, all
326 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL,
327 * their notification function is queued.
329 * For the SignalEvent service see efi_signal_event_ext.
331 void efi_signal_event(struct efi_event *event)
333 if (event->is_signaled)
336 struct efi_event *evt;
339 * The signaled state has to set before executing any
340 * notification function
342 list_for_each_entry(evt, &efi_events, link) {
343 if (!evt->group || guidcmp(evt->group, event->group))
345 if (evt->is_signaled)
347 evt->is_signaled = true;
349 list_for_each_entry(evt, &efi_events, link) {
350 if (!evt->group || guidcmp(evt->group, event->group))
352 efi_queue_event(evt);
355 event->is_signaled = true;
356 efi_queue_event(event);
361 * efi_raise_tpl() - raise the task priority level
362 * @new_tpl: new value of the task priority level
364 * This function implements the RaiseTpl service.
366 * See the Unified Extensible Firmware Interface (UEFI) specification for
369 * Return: old value of the task priority level
371 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
373 efi_uintn_t old_tpl = efi_tpl;
375 EFI_ENTRY("0x%zx", new_tpl);
377 if (new_tpl < efi_tpl)
378 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
380 if (efi_tpl > TPL_HIGH_LEVEL)
381 efi_tpl = TPL_HIGH_LEVEL;
383 EFI_EXIT(EFI_SUCCESS);
388 * efi_restore_tpl() - lower the task priority level
389 * @old_tpl: value of the task priority level to be restored
391 * This function implements the RestoreTpl service.
393 * See the Unified Extensible Firmware Interface (UEFI) specification for
396 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
398 EFI_ENTRY("0x%zx", old_tpl);
400 if (old_tpl > efi_tpl)
401 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
403 if (efi_tpl > TPL_HIGH_LEVEL)
404 efi_tpl = TPL_HIGH_LEVEL;
407 * Lowering the TPL may have made queued events eligible for execution.
411 EFI_EXIT(EFI_SUCCESS);
415 * efi_allocate_pages_ext() - allocate memory pages
416 * @type: type of allocation to be performed
417 * @memory_type: usage type of the allocated memory
418 * @pages: number of pages to be allocated
419 * @memory: allocated memory
421 * This function implements the AllocatePages service.
423 * See the Unified Extensible Firmware Interface (UEFI) specification for
426 * Return: status code
428 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
434 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
435 r = efi_allocate_pages(type, memory_type, pages, memory);
440 * efi_free_pages_ext() - Free memory pages.
441 * @memory: start of the memory area to be freed
442 * @pages: number of pages to be freed
444 * This function implements the FreePages service.
446 * See the Unified Extensible Firmware Interface (UEFI) specification for
449 * Return: status code
451 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
456 EFI_ENTRY("%llx, 0x%zx", memory, pages);
457 r = efi_free_pages(memory, pages);
462 * efi_get_memory_map_ext() - get map describing memory usage
463 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
464 * on exit the size of the copied memory map
465 * @memory_map: buffer to which the memory map is written
466 * @map_key: key for the memory map
467 * @descriptor_size: size of an individual memory descriptor
468 * @descriptor_version: version number of the memory descriptor structure
470 * This function implements the GetMemoryMap service.
472 * See the Unified Extensible Firmware Interface (UEFI) specification for
475 * Return: status code
477 static efi_status_t EFIAPI efi_get_memory_map_ext(
478 efi_uintn_t *memory_map_size,
479 struct efi_mem_desc *memory_map,
480 efi_uintn_t *map_key,
481 efi_uintn_t *descriptor_size,
482 uint32_t *descriptor_version)
486 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
487 map_key, descriptor_size, descriptor_version);
488 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
489 descriptor_size, descriptor_version);
494 * efi_allocate_pool_ext() - allocate memory from pool
495 * @pool_type: type of the pool from which memory is to be allocated
496 * @size: number of bytes to be allocated
497 * @buffer: allocated memory
499 * This function implements the AllocatePool service.
501 * See the Unified Extensible Firmware Interface (UEFI) specification for
504 * Return: status code
506 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
512 EFI_ENTRY("%d, %zu, %p", pool_type, size, buffer);
513 r = efi_allocate_pool(pool_type, size, buffer);
518 * efi_free_pool_ext() - free memory from pool
519 * @buffer: start of memory to be freed
521 * This function implements the FreePool service.
523 * See the Unified Extensible Firmware Interface (UEFI) specification for
526 * Return: status code
528 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
532 EFI_ENTRY("%p", buffer);
533 r = efi_free_pool(buffer);
538 * efi_add_handle() - add a new handle to the object list
540 * @handle: handle to be added
542 * The protocols list is initialized. The handle is added to the list of known
545 void efi_add_handle(efi_handle_t handle)
549 INIT_LIST_HEAD(&handle->protocols);
550 list_add_tail(&handle->link, &efi_obj_list);
554 * efi_create_handle() - create handle
555 * @handle: new handle
557 * Return: status code
559 efi_status_t efi_create_handle(efi_handle_t *handle)
561 struct efi_object *obj;
563 obj = calloc(1, sizeof(struct efi_object));
565 return EFI_OUT_OF_RESOURCES;
574 * efi_search_protocol() - find a protocol on a handle.
576 * @protocol_guid: GUID of the protocol
577 * @handler: reference to the protocol
579 * Return: status code
581 efi_status_t efi_search_protocol(const efi_handle_t handle,
582 const efi_guid_t *protocol_guid,
583 struct efi_handler **handler)
585 struct efi_object *efiobj;
586 struct list_head *lhandle;
588 if (!handle || !protocol_guid)
589 return EFI_INVALID_PARAMETER;
590 efiobj = efi_search_obj(handle);
592 return EFI_INVALID_PARAMETER;
593 list_for_each(lhandle, &efiobj->protocols) {
594 struct efi_handler *protocol;
596 protocol = list_entry(lhandle, struct efi_handler, link);
597 if (!guidcmp(&protocol->guid, protocol_guid)) {
603 return EFI_NOT_FOUND;
607 * efi_remove_protocol() - delete protocol from a handle
608 * @handle: handle from which the protocol shall be deleted
609 * @protocol: GUID of the protocol to be deleted
610 * @protocol_interface: interface of the protocol implementation
612 * Return: status code
614 static efi_status_t efi_remove_protocol(const efi_handle_t handle,
615 const efi_guid_t *protocol,
616 void *protocol_interface)
618 struct efi_handler *handler;
621 ret = efi_search_protocol(handle, protocol, &handler);
622 if (ret != EFI_SUCCESS)
624 if (handler->protocol_interface != protocol_interface)
625 return EFI_NOT_FOUND;
626 list_del(&handler->link);
632 * efi_remove_all_protocols() - delete all protocols from a handle
633 * @handle: handle from which the protocols shall be deleted
635 * Return: status code
637 static efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
639 struct efi_object *efiobj;
640 struct efi_handler *protocol;
641 struct efi_handler *pos;
643 efiobj = efi_search_obj(handle);
645 return EFI_INVALID_PARAMETER;
646 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
649 ret = efi_uninstall_protocol(handle, &protocol->guid,
650 protocol->protocol_interface, true);
651 if (ret != EFI_SUCCESS)
658 * efi_delete_handle() - delete handle
660 * @handle: handle to delete
662 * Return: status code
664 efi_status_t efi_delete_handle(efi_handle_t handle)
668 ret = efi_remove_all_protocols(handle);
669 if (ret != EFI_SUCCESS) {
670 log_err("Handle %p has protocols installed. Unable to delete\n", handle);
674 return efi_purge_handle(handle);
678 * efi_is_event() - check if a pointer is a valid event
679 * @event: pointer to check
681 * Return: status code
683 static efi_status_t efi_is_event(const struct efi_event *event)
685 const struct efi_event *evt;
688 return EFI_INVALID_PARAMETER;
689 list_for_each_entry(evt, &efi_events, link) {
693 return EFI_INVALID_PARAMETER;
697 * efi_create_event() - create an event
699 * @type: type of the event to create
700 * @notify_tpl: task priority level of the event
701 * @notify_function: notification function of the event
702 * @notify_context: pointer passed to the notification function
703 * @group: event group
704 * @event: created event
706 * This function is used inside U-Boot code to create an event.
708 * For the API function implementing the CreateEvent service see
709 * efi_create_event_ext.
711 * Return: status code
713 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
714 void (EFIAPI *notify_function) (
715 struct efi_event *event,
717 void *notify_context, const efi_guid_t *group,
718 struct efi_event **event)
720 struct efi_event *evt;
725 return EFI_INVALID_PARAMETER;
730 case EVT_NOTIFY_SIGNAL:
731 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
732 case EVT_NOTIFY_WAIT:
733 case EVT_TIMER | EVT_NOTIFY_WAIT:
734 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
735 pool_type = EFI_BOOT_SERVICES_DATA;
737 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
738 pool_type = EFI_RUNTIME_SERVICES_DATA;
741 return EFI_INVALID_PARAMETER;
745 * The UEFI specification requires event notification levels to be
746 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
748 * Parameter NotifyTpl should not be checked if it is not used.
750 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
751 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
752 notify_tpl == TPL_APPLICATION))
753 return EFI_INVALID_PARAMETER;
755 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
757 if (ret != EFI_SUCCESS)
759 memset(evt, 0, sizeof(struct efi_event));
761 evt->notify_tpl = notify_tpl;
762 evt->notify_function = notify_function;
763 evt->notify_context = notify_context;
765 /* Disable timers on boot up */
766 evt->trigger_next = -1ULL;
767 list_add_tail(&evt->link, &efi_events);
773 * efi_create_event_ex() - create an event in a group
775 * @type: type of the event to create
776 * @notify_tpl: task priority level of the event
777 * @notify_function: notification function of the event
778 * @notify_context: pointer passed to the notification function
779 * @event: created event
780 * @event_group: event group
782 * This function implements the CreateEventEx service.
784 * See the Unified Extensible Firmware Interface (UEFI) specification for
787 * Return: status code
790 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
791 void (EFIAPI *notify_function) (
792 struct efi_event *event,
794 void *notify_context,
795 const efi_guid_t *event_group,
796 struct efi_event **event)
800 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUs", type, notify_tpl, notify_function,
801 notify_context, event_group);
804 * The allowable input parameters are the same as in CreateEvent()
805 * except for the following two disallowed event types.
808 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
809 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
810 ret = EFI_INVALID_PARAMETER;
814 ret = efi_create_event(type, notify_tpl, notify_function,
815 notify_context, event_group, event);
817 return EFI_EXIT(ret);
821 * efi_create_event_ext() - create an event
822 * @type: type of the event to create
823 * @notify_tpl: task priority level of the event
824 * @notify_function: notification function of the event
825 * @notify_context: pointer passed to the notification function
826 * @event: created event
828 * This function implements the CreateEvent service.
830 * See the Unified Extensible Firmware Interface (UEFI) specification for
833 * Return: status code
835 static efi_status_t EFIAPI efi_create_event_ext(
836 uint32_t type, efi_uintn_t notify_tpl,
837 void (EFIAPI *notify_function) (
838 struct efi_event *event,
840 void *notify_context, struct efi_event **event)
842 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
844 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
845 notify_context, NULL, event));
849 * efi_timer_check() - check if a timer event has occurred
851 * Check if a timer event has occurred or a queued notification function should
854 * Our timers have to work without interrupts, so we check whenever keyboard
855 * input or disk accesses happen if enough time elapsed for them to fire.
857 void efi_timer_check(void)
859 struct efi_event *evt;
860 u64 now = timer_get_us();
862 list_for_each_entry(evt, &efi_events, link) {
865 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
867 switch (evt->trigger_type) {
868 case EFI_TIMER_RELATIVE:
869 evt->trigger_type = EFI_TIMER_STOP;
871 case EFI_TIMER_PERIODIC:
872 evt->trigger_next += evt->trigger_time;
877 evt->is_signaled = false;
878 efi_signal_event(evt);
880 efi_process_event_queue();
885 * efi_set_timer() - set the trigger time for a timer event or stop the event
886 * @event: event for which the timer is set
887 * @type: type of the timer
888 * @trigger_time: trigger period in multiples of 100 ns
890 * This is the function for internal usage in U-Boot. For the API function
891 * implementing the SetTimer service see efi_set_timer_ext.
893 * Return: status code
895 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
896 uint64_t trigger_time)
898 /* Check that the event is valid */
899 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
900 return EFI_INVALID_PARAMETER;
903 * The parameter defines a multiple of 100 ns.
904 * We use multiples of 1000 ns. So divide by 10.
906 do_div(trigger_time, 10);
910 event->trigger_next = -1ULL;
912 case EFI_TIMER_PERIODIC:
913 case EFI_TIMER_RELATIVE:
914 event->trigger_next = timer_get_us() + trigger_time;
917 return EFI_INVALID_PARAMETER;
919 event->trigger_type = type;
920 event->trigger_time = trigger_time;
921 event->is_signaled = false;
926 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
928 * @event: event for which the timer is set
929 * @type: type of the timer
930 * @trigger_time: trigger period in multiples of 100 ns
932 * This function implements the SetTimer service.
934 * See the Unified Extensible Firmware Interface (UEFI) specification for
938 * Return: status code
940 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
941 enum efi_timer_delay type,
942 uint64_t trigger_time)
944 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
945 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
949 * efi_wait_for_event() - wait for events to be signaled
950 * @num_events: number of events to be waited for
951 * @event: events to be waited for
952 * @index: index of the event that was signaled
954 * This function implements the WaitForEvent service.
956 * See the Unified Extensible Firmware Interface (UEFI) specification for
959 * Return: status code
961 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
962 struct efi_event **event,
967 EFI_ENTRY("%zu, %p, %p", num_events, event, index);
969 /* Check parameters */
970 if (!num_events || !event)
971 return EFI_EXIT(EFI_INVALID_PARAMETER);
973 if (efi_tpl != TPL_APPLICATION)
974 return EFI_EXIT(EFI_UNSUPPORTED);
975 for (i = 0; i < num_events; ++i) {
976 if (efi_is_event(event[i]) != EFI_SUCCESS)
977 return EFI_EXIT(EFI_INVALID_PARAMETER);
978 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
979 return EFI_EXIT(EFI_INVALID_PARAMETER);
980 if (!event[i]->is_signaled)
981 efi_queue_event(event[i]);
984 /* Wait for signal */
986 for (i = 0; i < num_events; ++i) {
987 if (event[i]->is_signaled)
990 /* Allow events to occur. */
996 * Reset the signal which is passed to the caller to allow periodic
999 event[i]->is_signaled = false;
1003 return EFI_EXIT(EFI_SUCCESS);
1007 * efi_signal_event_ext() - signal an EFI event
1008 * @event: event to signal
1010 * This function implements the SignalEvent service.
1012 * See the Unified Extensible Firmware Interface (UEFI) specification for
1015 * This functions sets the signaled state of the event and queues the
1016 * notification function for execution.
1018 * Return: status code
1020 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
1022 EFI_ENTRY("%p", event);
1023 if (efi_is_event(event) != EFI_SUCCESS)
1024 return EFI_EXIT(EFI_INVALID_PARAMETER);
1025 efi_signal_event(event);
1026 return EFI_EXIT(EFI_SUCCESS);
1030 * efi_close_event() - close an EFI event
1031 * @event: event to close
1033 * This function implements the CloseEvent service.
1035 * See the Unified Extensible Firmware Interface (UEFI) specification for
1038 * Return: status code
1040 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
1042 struct efi_register_notify_event *item, *next;
1044 EFI_ENTRY("%p", event);
1045 if (efi_is_event(event) != EFI_SUCCESS)
1046 return EFI_EXIT(EFI_INVALID_PARAMETER);
1048 /* Remove protocol notify registrations for the event */
1049 list_for_each_entry_safe(item, next, &efi_register_notify_events,
1051 if (event == item->event) {
1052 struct efi_protocol_notification *hitem, *hnext;
1054 /* Remove signaled handles */
1055 list_for_each_entry_safe(hitem, hnext, &item->handles,
1057 list_del(&hitem->link);
1060 list_del(&item->link);
1064 /* Remove event from queue */
1065 if (efi_event_is_queued(event))
1066 list_del(&event->queue_link);
1068 list_del(&event->link);
1069 efi_free_pool(event);
1070 return EFI_EXIT(EFI_SUCCESS);
1074 * efi_check_event() - check if an event is signaled
1075 * @event: event to check
1077 * This function implements the CheckEvent service.
1079 * See the Unified Extensible Firmware Interface (UEFI) specification for
1082 * If an event is not signaled yet, the notification function is queued. The
1083 * signaled state is cleared.
1085 * Return: status code
1087 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
1089 EFI_ENTRY("%p", event);
1091 if (efi_is_event(event) != EFI_SUCCESS ||
1092 event->type & EVT_NOTIFY_SIGNAL)
1093 return EFI_EXIT(EFI_INVALID_PARAMETER);
1094 if (!event->is_signaled)
1095 efi_queue_event(event);
1096 if (event->is_signaled) {
1097 event->is_signaled = false;
1098 return EFI_EXIT(EFI_SUCCESS);
1100 return EFI_EXIT(EFI_NOT_READY);
1104 * efi_search_obj() - find the internal EFI object for a handle
1105 * @handle: handle to find
1107 * Return: EFI object
1109 struct efi_object *efi_search_obj(const efi_handle_t handle)
1111 struct efi_object *efiobj;
1116 list_for_each_entry(efiobj, &efi_obj_list, link) {
1117 if (efiobj == handle)
1124 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1126 * @handler: handler of a protocol
1128 * Return: open protocol info entry
1130 static struct efi_open_protocol_info_entry *efi_create_open_info(
1131 struct efi_handler *handler)
1133 struct efi_open_protocol_info_item *item;
1135 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1138 /* Append the item to the open protocol info list. */
1139 list_add_tail(&item->link, &handler->open_infos);
1145 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1146 * @item: open protocol info entry to delete
1148 * Return: status code
1150 static efi_status_t efi_delete_open_info(
1151 struct efi_open_protocol_info_item *item)
1153 list_del(&item->link);
1159 * efi_add_protocol() - install new protocol on a handle
1160 * @handle: handle on which the protocol shall be installed
1161 * @protocol: GUID of the protocol to be installed
1162 * @protocol_interface: interface of the protocol implementation
1164 * Return: status code
1166 efi_status_t efi_add_protocol(const efi_handle_t handle,
1167 const efi_guid_t *protocol,
1168 void *protocol_interface)
1170 struct efi_object *efiobj;
1171 struct efi_handler *handler;
1173 struct efi_register_notify_event *event;
1175 efiobj = efi_search_obj(handle);
1177 return EFI_INVALID_PARAMETER;
1178 ret = efi_search_protocol(handle, protocol, NULL);
1179 if (ret != EFI_NOT_FOUND)
1180 return EFI_INVALID_PARAMETER;
1181 handler = calloc(1, sizeof(struct efi_handler));
1183 return EFI_OUT_OF_RESOURCES;
1184 memcpy((void *)&handler->guid, protocol, sizeof(efi_guid_t));
1185 handler->protocol_interface = protocol_interface;
1186 INIT_LIST_HEAD(&handler->open_infos);
1187 list_add_tail(&handler->link, &efiobj->protocols);
1189 /* Notify registered events */
1190 list_for_each_entry(event, &efi_register_notify_events, link) {
1191 if (!guidcmp(protocol, &event->protocol)) {
1192 struct efi_protocol_notification *notif;
1194 notif = calloc(1, sizeof(*notif));
1196 list_del(&handler->link);
1198 return EFI_OUT_OF_RESOURCES;
1200 notif->handle = handle;
1201 list_add_tail(¬if->link, &event->handles);
1202 event->event->is_signaled = false;
1203 efi_signal_event(event->event);
1207 if (!guidcmp(&efi_guid_device_path, protocol))
1208 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1213 * efi_install_protocol_interface() - install protocol interface
1214 * @handle: handle on which the protocol shall be installed
1215 * @protocol: GUID of the protocol to be installed
1216 * @protocol_interface_type: type of the interface to be installed,
1217 * always EFI_NATIVE_INTERFACE
1218 * @protocol_interface: interface of the protocol implementation
1220 * This function implements the InstallProtocolInterface service.
1222 * See the Unified Extensible Firmware Interface (UEFI) specification for
1225 * Return: status code
1227 static efi_status_t EFIAPI efi_install_protocol_interface(
1228 efi_handle_t *handle, const efi_guid_t *protocol,
1229 int protocol_interface_type, void *protocol_interface)
1233 EFI_ENTRY("%p, %pUs, %d, %p", handle, protocol, protocol_interface_type,
1234 protocol_interface);
1236 if (!handle || !protocol ||
1237 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1238 r = EFI_INVALID_PARAMETER;
1242 /* Create new handle if requested. */
1244 r = efi_create_handle(handle);
1245 if (r != EFI_SUCCESS)
1247 EFI_PRINT("new handle %p\n", *handle);
1249 EFI_PRINT("handle %p\n", *handle);
1251 /* Add new protocol */
1252 r = efi_add_protocol(*handle, protocol, protocol_interface);
1258 * efi_get_drivers() - get all drivers associated to a controller
1259 * @handle: handle of the controller
1260 * @protocol: protocol GUID (optional)
1261 * @number_of_drivers: number of child controllers
1262 * @driver_handle_buffer: handles of the the drivers
1264 * The allocated buffer has to be freed with free().
1266 * Return: status code
1268 static efi_status_t efi_get_drivers(efi_handle_t handle,
1269 const efi_guid_t *protocol,
1270 efi_uintn_t *number_of_drivers,
1271 efi_handle_t **driver_handle_buffer)
1273 struct efi_handler *handler;
1274 struct efi_open_protocol_info_item *item;
1275 efi_uintn_t count = 0, i;
1278 /* Count all driver associations */
1279 list_for_each_entry(handler, &handle->protocols, link) {
1280 if (protocol && guidcmp(&handler->guid, protocol))
1282 list_for_each_entry(item, &handler->open_infos, link) {
1283 if (item->info.attributes &
1284 EFI_OPEN_PROTOCOL_BY_DRIVER)
1288 *number_of_drivers = 0;
1290 *driver_handle_buffer = NULL;
1294 * Create buffer. In case of duplicate driver assignments the buffer
1295 * will be too large. But that does not harm.
1297 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1298 if (!*driver_handle_buffer)
1299 return EFI_OUT_OF_RESOURCES;
1300 /* Collect unique driver handles */
1301 list_for_each_entry(handler, &handle->protocols, link) {
1302 if (protocol && guidcmp(&handler->guid, protocol))
1304 list_for_each_entry(item, &handler->open_infos, link) {
1305 if (item->info.attributes &
1306 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1307 /* Check this is a new driver */
1309 for (i = 0; i < *number_of_drivers; ++i) {
1310 if ((*driver_handle_buffer)[i] ==
1311 item->info.agent_handle)
1314 /* Copy handle to buffer */
1316 i = (*number_of_drivers)++;
1317 (*driver_handle_buffer)[i] =
1318 item->info.agent_handle;
1327 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1328 * @handle: handle of the controller
1329 * @protocol: protocol GUID (optional)
1330 * @child_handle: handle of the child to destroy
1332 * This function implements the DisconnectController service.
1334 * See the Unified Extensible Firmware Interface (UEFI) specification for
1337 * Return: status code
1339 static efi_status_t efi_disconnect_all_drivers
1340 (efi_handle_t handle,
1341 const efi_guid_t *protocol,
1342 efi_handle_t child_handle)
1344 efi_uintn_t number_of_drivers;
1345 efi_handle_t *driver_handle_buffer;
1346 efi_status_t r, ret;
1348 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
1349 &driver_handle_buffer);
1350 if (ret != EFI_SUCCESS)
1352 if (!number_of_drivers)
1355 while (number_of_drivers) {
1356 r = EFI_CALL(efi_disconnect_controller(
1358 driver_handle_buffer[--number_of_drivers],
1360 if (r != EFI_SUCCESS)
1364 free(driver_handle_buffer);
1369 * efi_uninstall_protocol() - uninstall protocol interface
1371 * @handle: handle from which the protocol shall be removed
1372 * @protocol: GUID of the protocol to be removed
1373 * @protocol_interface: interface to be removed
1374 * @preserve: preserve or delete the handle and remove it from any
1375 * list it participates if no protocols remain
1377 * This function DOES NOT delete a handle without installed protocol.
1379 * Return: status code
1381 static efi_status_t efi_uninstall_protocol
1382 (efi_handle_t handle, const efi_guid_t *protocol,
1383 void *protocol_interface, bool preserve)
1385 struct efi_handler *handler;
1386 struct efi_open_protocol_info_item *item;
1387 struct efi_open_protocol_info_item *pos;
1390 /* Find the protocol on the handle */
1391 r = efi_search_protocol(handle, protocol, &handler);
1392 if (r != EFI_SUCCESS)
1394 if (handler->protocol_interface != protocol_interface)
1395 return EFI_NOT_FOUND;
1396 /* Disconnect controllers */
1397 r = efi_disconnect_all_drivers(handle, protocol, NULL);
1398 if (r != EFI_SUCCESS) {
1399 r = EFI_ACCESS_DENIED;
1401 * This will reconnect all controllers of the handle, even ones
1402 * that were not connected before. This can be done better
1403 * but we are following the EDKII implementation on this for
1406 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
1409 /* Close protocol */
1410 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1411 if (item->info.attributes ==
1412 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1413 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1414 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1415 efi_delete_open_info(item);
1417 /* if agents didn't close the protocols properly */
1418 if (!list_empty(&handler->open_infos)) {
1419 r = EFI_ACCESS_DENIED;
1420 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
1423 r = efi_remove_protocol(handle, protocol, protocol_interface);
1424 if (r != EFI_SUCCESS)
1427 * We don't care about the return value here since the
1428 * handle might have more protocols installed
1431 efi_purge_handle(handle);
1437 * efi_uninstall_protocol_interface() - uninstall protocol interface
1438 * @handle: handle from which the protocol shall be removed
1439 * @protocol: GUID of the protocol to be removed
1440 * @protocol_interface: interface to be removed
1442 * This function implements the UninstallProtocolInterface service.
1444 * See the Unified Extensible Firmware Interface (UEFI) specification for
1447 * Return: status code
1449 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1450 (efi_handle_t handle, const efi_guid_t *protocol,
1451 void *protocol_interface)
1455 EFI_ENTRY("%p, %pUs, %p", handle, protocol, protocol_interface);
1457 ret = efi_uninstall_protocol(handle, protocol, protocol_interface, false);
1458 if (ret != EFI_SUCCESS)
1462 return EFI_EXIT(ret);
1466 * efi_register_protocol_notify() - register an event for notification when a
1467 * protocol is installed.
1468 * @protocol: GUID of the protocol whose installation shall be notified
1469 * @event: event to be signaled upon installation of the protocol
1470 * @registration: key for retrieving the registration information
1472 * This function implements the RegisterProtocolNotify service.
1473 * See the Unified Extensible Firmware Interface (UEFI) specification
1476 * Return: status code
1478 efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1479 struct efi_event *event,
1480 void **registration)
1482 struct efi_register_notify_event *item;
1483 efi_status_t ret = EFI_SUCCESS;
1485 EFI_ENTRY("%pUs, %p, %p", protocol, event, registration);
1487 if (!protocol || !event || !registration) {
1488 ret = EFI_INVALID_PARAMETER;
1492 item = calloc(1, sizeof(struct efi_register_notify_event));
1494 ret = EFI_OUT_OF_RESOURCES;
1498 item->event = event;
1499 guidcpy(&item->protocol, protocol);
1500 INIT_LIST_HEAD(&item->handles);
1502 list_add_tail(&item->link, &efi_register_notify_events);
1504 *registration = item;
1506 return EFI_EXIT(ret);
1510 * efi_search() - determine if an EFI handle implements a protocol
1512 * @search_type: selection criterion
1513 * @protocol: GUID of the protocol
1516 * See the documentation of the LocateHandle service in the UEFI specification.
1518 * Return: 0 if the handle implements the protocol
1520 static int efi_search(enum efi_locate_search_type search_type,
1521 const efi_guid_t *protocol, efi_handle_t handle)
1525 switch (search_type) {
1529 ret = efi_search_protocol(handle, protocol, NULL);
1530 return (ret != EFI_SUCCESS);
1532 /* Invalid search type */
1538 * efi_check_register_notify_event() - check if registration key is valid
1540 * Check that a pointer is a valid registration key as returned by
1541 * RegisterProtocolNotify().
1543 * @key: registration key
1544 * Return: valid registration key or NULL
1546 static struct efi_register_notify_event *efi_check_register_notify_event
1549 struct efi_register_notify_event *event;
1551 list_for_each_entry(event, &efi_register_notify_events, link) {
1552 if (event == (struct efi_register_notify_event *)key)
1559 * efi_locate_handle() - locate handles implementing a protocol
1561 * @search_type: selection criterion
1562 * @protocol: GUID of the protocol
1563 * @search_key: registration key
1564 * @buffer_size: size of the buffer to receive the handles in bytes
1565 * @buffer: buffer to receive the relevant handles
1567 * This function is meant for U-Boot internal calls. For the API implementation
1568 * of the LocateHandle service see efi_locate_handle_ext.
1570 * Return: status code
1572 static efi_status_t efi_locate_handle(
1573 enum efi_locate_search_type search_type,
1574 const efi_guid_t *protocol, void *search_key,
1575 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1577 struct efi_object *efiobj;
1578 efi_uintn_t size = 0;
1579 struct efi_register_notify_event *event;
1580 struct efi_protocol_notification *handle = NULL;
1582 /* Check parameters */
1583 switch (search_type) {
1586 case BY_REGISTER_NOTIFY:
1588 return EFI_INVALID_PARAMETER;
1589 /* Check that the registration key is valid */
1590 event = efi_check_register_notify_event(search_key);
1592 return EFI_INVALID_PARAMETER;
1596 return EFI_INVALID_PARAMETER;
1599 return EFI_INVALID_PARAMETER;
1602 /* Count how much space we need */
1603 if (search_type == BY_REGISTER_NOTIFY) {
1604 if (list_empty(&event->handles))
1605 return EFI_NOT_FOUND;
1606 handle = list_first_entry(&event->handles,
1607 struct efi_protocol_notification,
1609 efiobj = handle->handle;
1610 size += sizeof(void *);
1612 list_for_each_entry(efiobj, &efi_obj_list, link) {
1613 if (!efi_search(search_type, protocol, efiobj))
1614 size += sizeof(void *);
1617 return EFI_NOT_FOUND;
1621 return EFI_INVALID_PARAMETER;
1623 if (*buffer_size < size) {
1624 *buffer_size = size;
1625 return EFI_BUFFER_TOO_SMALL;
1628 *buffer_size = size;
1630 /* The buffer size is sufficient but there is no buffer */
1632 return EFI_INVALID_PARAMETER;
1634 /* Then fill the array */
1635 if (search_type == BY_REGISTER_NOTIFY) {
1637 list_del(&handle->link);
1639 list_for_each_entry(efiobj, &efi_obj_list, link) {
1640 if (!efi_search(search_type, protocol, efiobj))
1649 * efi_locate_handle_ext() - locate handles implementing a protocol.
1650 * @search_type: selection criterion
1651 * @protocol: GUID of the protocol
1652 * @search_key: registration key
1653 * @buffer_size: size of the buffer to receive the handles in bytes
1654 * @buffer: buffer to receive the relevant handles
1656 * This function implements the LocateHandle service.
1658 * See the Unified Extensible Firmware Interface (UEFI) specification for
1661 * Return: 0 if the handle implements the protocol
1663 static efi_status_t EFIAPI efi_locate_handle_ext(
1664 enum efi_locate_search_type search_type,
1665 const efi_guid_t *protocol, void *search_key,
1666 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1668 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
1669 buffer_size, buffer);
1671 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1672 buffer_size, buffer));
1676 * efi_remove_configuration_table() - collapses configuration table entries,
1679 * @i: index of the table entry to be removed
1681 static void efi_remove_configuration_table(int i)
1683 struct efi_configuration_table *this = &systab.tables[i];
1684 struct efi_configuration_table *next = &systab.tables[i + 1];
1685 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1687 memmove(this, next, (ulong)end - (ulong)next);
1692 * efi_install_configuration_table() - adds, updates, or removes a
1693 * configuration table
1694 * @guid: GUID of the installed table
1695 * @table: table to be installed
1697 * This function is used for internal calls. For the API implementation of the
1698 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1700 * Return: status code
1702 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1705 struct efi_event *evt;
1709 return EFI_INVALID_PARAMETER;
1711 /* Check for GUID override */
1712 for (i = 0; i < systab.nr_tables; i++) {
1713 if (!guidcmp(guid, &systab.tables[i].guid)) {
1715 systab.tables[i].table = table;
1717 efi_remove_configuration_table(i);
1723 return EFI_NOT_FOUND;
1725 /* No override, check for overflow */
1726 if (i >= EFI_MAX_CONFIGURATION_TABLES)
1727 return EFI_OUT_OF_RESOURCES;
1729 /* Add a new entry */
1730 guidcpy(&systab.tables[i].guid, guid);
1731 systab.tables[i].table = table;
1732 systab.nr_tables = i + 1;
1735 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1736 efi_update_table_header_crc32(&systab.hdr);
1738 /* Notify that the configuration table was changed */
1739 list_for_each_entry(evt, &efi_events, link) {
1740 if (evt->group && !guidcmp(evt->group, guid)) {
1741 efi_signal_event(evt);
1750 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1751 * configuration table.
1752 * @guid: GUID of the installed table
1753 * @table: table to be installed
1755 * This function implements the InstallConfigurationTable service.
1757 * See the Unified Extensible Firmware Interface (UEFI) specification for
1760 * Return: status code
1763 EFIAPI efi_install_configuration_table_ext(const efi_guid_t *guid,
1766 EFI_ENTRY("%pUs, %p", guid, table);
1767 return EFI_EXIT(efi_install_configuration_table(guid, table));
1771 * efi_setup_loaded_image() - initialize a loaded image
1773 * Initialize a loaded_image_info and loaded_image_info object with correct
1774 * protocols, boot-device, etc.
1776 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
1779 * @device_path: device path of the loaded image
1780 * @file_path: file path of the loaded image
1781 * @handle_ptr: handle of the loaded image
1782 * @info_ptr: loaded image protocol
1783 * Return: status code
1785 efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1786 struct efi_device_path *file_path,
1787 struct efi_loaded_image_obj **handle_ptr,
1788 struct efi_loaded_image **info_ptr)
1791 struct efi_loaded_image *info = NULL;
1792 struct efi_loaded_image_obj *obj = NULL;
1793 struct efi_device_path *dp;
1795 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1799 info = calloc(1, sizeof(*info));
1801 return EFI_OUT_OF_RESOURCES;
1802 obj = calloc(1, sizeof(*obj));
1805 return EFI_OUT_OF_RESOURCES;
1807 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
1809 /* Add internal object to object list */
1810 efi_add_handle(&obj->header);
1812 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1813 info->file_path = file_path;
1814 info->system_table = &systab;
1817 info->device_handle = efi_dp_find_obj(device_path, NULL, NULL);
1819 dp = efi_dp_concat(device_path, file_path, 0);
1821 ret = EFI_OUT_OF_RESOURCES;
1827 ret = efi_add_protocol(&obj->header,
1828 &efi_guid_loaded_image_device_path, dp);
1829 if (ret != EFI_SUCCESS)
1833 * When asking for the loaded_image interface, just
1834 * return handle which points to loaded_image_info
1836 ret = efi_add_protocol(&obj->header,
1837 &efi_guid_loaded_image, info);
1838 if (ret != EFI_SUCCESS)
1846 printf("ERROR: Failure to install protocols for loaded image\n");
1847 efi_delete_handle(&obj->header);
1853 * efi_locate_device_path() - Get the device path and handle of an device
1854 * implementing a protocol
1855 * @protocol: GUID of the protocol
1856 * @device_path: device path
1857 * @device: handle of the device
1859 * This function implements the LocateDevicePath service.
1861 * See the Unified Extensible Firmware Interface (UEFI) specification for
1864 * Return: status code
1866 efi_status_t EFIAPI efi_locate_device_path(const efi_guid_t *protocol,
1867 struct efi_device_path **device_path,
1868 efi_handle_t *device)
1870 struct efi_device_path *dp;
1872 struct efi_handler *handler;
1873 efi_handle_t *handles;
1875 size_t len_best = 0;
1876 efi_uintn_t no_handles;
1880 EFI_ENTRY("%pUs, %p, %p", protocol, device_path, device);
1882 if (!protocol || !device_path || !*device_path) {
1883 ret = EFI_INVALID_PARAMETER;
1887 /* Find end of device path */
1888 len = efi_dp_instance_size(*device_path);
1890 /* Get all handles implementing the protocol */
1891 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1892 &no_handles, &handles));
1893 if (ret != EFI_SUCCESS)
1896 for (i = 0; i < no_handles; ++i) {
1897 /* Find the device path protocol */
1898 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1900 if (ret != EFI_SUCCESS)
1902 dp = (struct efi_device_path *)handler->protocol_interface;
1903 len_dp = efi_dp_instance_size(dp);
1905 * This handle can only be a better fit
1906 * if its device path length is longer than the best fit and
1907 * if its device path length is shorter of equal the searched
1910 if (len_dp <= len_best || len_dp > len)
1912 /* Check if dp is a subpath of device_path */
1913 if (memcmp(*device_path, dp, len_dp))
1916 ret = EFI_INVALID_PARAMETER;
1919 *device = handles[i];
1923 remainder = (u8 *)*device_path + len_best;
1924 *device_path = (struct efi_device_path *)remainder;
1927 ret = EFI_NOT_FOUND;
1930 return EFI_EXIT(ret);
1934 * efi_load_image_from_file() - load an image from file system
1936 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1937 * callers obligation to update the memory type as needed.
1939 * @file_path: the path of the image to load
1940 * @buffer: buffer containing the loaded image
1941 * @size: size of the loaded image
1942 * Return: status code
1945 efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
1946 void **buffer, efi_uintn_t *size)
1948 struct efi_file_handle *f;
1954 f = efi_file_from_path(file_path);
1956 return EFI_NOT_FOUND;
1958 ret = efi_file_size(f, &bs);
1959 if (ret != EFI_SUCCESS)
1963 * When reading the file we do not yet know if it contains an
1964 * application, a boottime driver, or a runtime driver. So here we
1965 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1966 * update the reservation according to the image type.
1968 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1969 EFI_BOOT_SERVICES_DATA,
1970 efi_size_in_pages(bs), &addr);
1971 if (ret != EFI_SUCCESS) {
1972 ret = EFI_OUT_OF_RESOURCES;
1977 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1978 if (ret != EFI_SUCCESS)
1979 efi_free_pages(addr, efi_size_in_pages(bs));
1980 *buffer = (void *)(uintptr_t)addr;
1983 EFI_CALL(f->close(f));
1988 * efi_load_image_from_path() - load an image using a file path
1990 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1991 * callers obligation to update the memory type as needed.
1993 * @boot_policy: true for request originating from the boot manager
1994 * @file_path: the path of the image to load
1995 * @buffer: buffer containing the loaded image
1996 * @size: size of the loaded image
1997 * Return: status code
1999 efi_status_t efi_load_image_from_path(bool boot_policy,
2000 struct efi_device_path *file_path,
2001 void **buffer, efi_uintn_t *size)
2003 efi_handle_t device;
2005 struct efi_device_path *dp, *rem;
2006 struct efi_load_file_protocol *load_file_protocol = NULL;
2007 efi_uintn_t buffer_size;
2008 uint64_t addr, pages;
2009 const efi_guid_t *guid;
2010 struct efi_handler *handler;
2012 /* In case of failure nothing is returned */
2017 device = efi_dp_find_obj(dp, NULL, &rem);
2018 ret = efi_search_protocol(device, &efi_simple_file_system_protocol_guid,
2020 if (ret == EFI_SUCCESS)
2021 return efi_load_image_from_file(file_path, buffer, size);
2023 ret = efi_search_protocol(device, &efi_guid_load_file_protocol, NULL);
2024 if (ret == EFI_SUCCESS) {
2025 guid = &efi_guid_load_file_protocol;
2026 } else if (!boot_policy) {
2027 guid = &efi_guid_load_file2_protocol;
2028 ret = efi_search_protocol(device, guid, NULL);
2030 if (ret != EFI_SUCCESS)
2031 return EFI_NOT_FOUND;
2032 ret = efi_search_protocol(device, guid, &handler);
2033 if (ret != EFI_SUCCESS)
2034 return EFI_NOT_FOUND;
2036 load_file_protocol = handler->protocol_interface;
2037 ret = EFI_CALL(load_file_protocol->load_file(
2038 load_file_protocol, rem, boot_policy,
2039 &buffer_size, NULL));
2040 if (ret != EFI_BUFFER_TOO_SMALL)
2042 pages = efi_size_in_pages(buffer_size);
2043 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
2045 if (ret != EFI_SUCCESS) {
2046 ret = EFI_OUT_OF_RESOURCES;
2049 ret = EFI_CALL(load_file_protocol->load_file(
2050 load_file_protocol, rem, boot_policy,
2051 &buffer_size, (void *)(uintptr_t)addr));
2052 if (ret != EFI_SUCCESS)
2053 efi_free_pages(addr, pages);
2055 efi_close_protocol(device, guid, efi_root, NULL);
2056 if (ret == EFI_SUCCESS) {
2057 *buffer = (void *)(uintptr_t)addr;
2058 *size = buffer_size;
2065 * efi_load_image() - load an EFI image into memory
2066 * @boot_policy: true for request originating from the boot manager
2067 * @parent_image: the caller's image handle
2068 * @file_path: the path of the image to load
2069 * @source_buffer: memory location from which the image is installed
2070 * @source_size: size of the memory area from which the image is installed
2071 * @image_handle: handle for the newly installed image
2073 * This function implements the LoadImage service.
2075 * See the Unified Extensible Firmware Interface (UEFI) specification
2078 * Return: status code
2080 efi_status_t EFIAPI efi_load_image(bool boot_policy,
2081 efi_handle_t parent_image,
2082 struct efi_device_path *file_path,
2083 void *source_buffer,
2084 efi_uintn_t source_size,
2085 efi_handle_t *image_handle)
2087 struct efi_device_path *dp, *fp;
2088 struct efi_loaded_image *info = NULL;
2089 struct efi_loaded_image_obj **image_obj =
2090 (struct efi_loaded_image_obj **)image_handle;
2094 EFI_ENTRY("%d, %p, %pD, %p, %zu, %p", boot_policy, parent_image,
2095 file_path, source_buffer, source_size, image_handle);
2097 if (!image_handle || (!source_buffer && !file_path) ||
2098 !efi_search_obj(parent_image) ||
2099 /* The parent image handle must refer to a loaded image */
2100 !parent_image->type) {
2101 ret = EFI_INVALID_PARAMETER;
2105 if (!source_buffer) {
2106 ret = efi_load_image_from_path(boot_policy, file_path,
2107 &dest_buffer, &source_size);
2108 if (ret != EFI_SUCCESS)
2111 dest_buffer = source_buffer;
2113 /* split file_path which contains both the device and file parts */
2114 efi_dp_split_file_path(file_path, &dp, &fp);
2115 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
2116 if (ret == EFI_SUCCESS)
2117 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
2119 /* Release buffer to which file was loaded */
2120 efi_free_pages((uintptr_t)dest_buffer,
2121 efi_size_in_pages(source_size));
2122 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
2123 info->system_table = &systab;
2124 info->parent_handle = parent_image;
2126 /* The image is invalid. Release all associated resources. */
2127 efi_delete_handle(*image_handle);
2128 *image_handle = NULL;
2132 return EFI_EXIT(ret);
2136 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2138 static void efi_exit_caches(void)
2140 #if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
2142 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2143 * caches are enabled.
2146 * According to the UEFI spec caches that can be managed via CP15
2147 * operations should be enabled. Caches requiring platform information
2148 * to manage should be disabled. This should not happen in
2149 * ExitBootServices() but before invoking any UEFI binary is invoked.
2151 * We want to keep the current workaround while GRUB prior to version
2152 * 2.04 is still in use.
2154 cleanup_before_linux();
2159 * efi_exit_boot_services() - stop all boot services
2160 * @image_handle: handle of the loaded image
2161 * @map_key: key of the memory map
2163 * This function implements the ExitBootServices service.
2165 * See the Unified Extensible Firmware Interface (UEFI) specification
2168 * All timer events are disabled. For exit boot services events the
2169 * notification function is called. The boot services are disabled in the
2172 * Return: status code
2174 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
2175 efi_uintn_t map_key)
2177 struct efi_event *evt, *next_event;
2178 efi_status_t ret = EFI_SUCCESS;
2180 EFI_ENTRY("%p, %zx", image_handle, map_key);
2182 /* Check that the caller has read the current memory map */
2183 if (map_key != efi_memory_map_key) {
2184 ret = EFI_INVALID_PARAMETER;
2188 /* Check if ExitBootServices has already been called */
2189 if (!systab.boottime)
2192 /* Notify EFI_EVENT_GROUP_BEFORE_EXIT_BOOT_SERVICES event group. */
2193 list_for_each_entry(evt, &efi_events, link) {
2195 !guidcmp(evt->group,
2196 &efi_guid_event_group_before_exit_boot_services)) {
2197 efi_signal_event(evt);
2202 /* Stop all timer related activities */
2203 timers_enabled = false;
2205 /* Add related events to the event group */
2206 list_for_each_entry(evt, &efi_events, link) {
2207 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2208 evt->group = &efi_guid_event_group_exit_boot_services;
2210 /* Notify that ExitBootServices is invoked. */
2211 list_for_each_entry(evt, &efi_events, link) {
2213 !guidcmp(evt->group,
2214 &efi_guid_event_group_exit_boot_services)) {
2215 efi_signal_event(evt);
2220 /* Make sure that notification functions are not called anymore */
2221 efi_tpl = TPL_HIGH_LEVEL;
2223 /* Notify variable services */
2224 efi_variables_boot_exit_notify();
2226 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2227 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2228 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2229 list_del(&evt->link);
2232 if (!efi_st_keep_devices) {
2233 bootm_disable_interrupts();
2234 if (IS_ENABLED(CONFIG_USB_DEVICE))
2236 board_quiesce_devices();
2237 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2240 /* Patch out unsupported runtime function */
2241 efi_runtime_detach();
2243 /* Fix up caches for EFI payloads if necessary */
2246 /* Disable boot time services */
2247 systab.con_in_handle = NULL;
2248 systab.con_in = NULL;
2249 systab.con_out_handle = NULL;
2250 systab.con_out = NULL;
2251 systab.stderr_handle = NULL;
2252 systab.std_err = NULL;
2253 systab.boottime = NULL;
2255 /* Recalculate CRC32 */
2256 efi_update_table_header_crc32(&systab.hdr);
2258 /* Give the payload some time to boot */
2259 efi_set_watchdog(0);
2262 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
2263 if (ret != EFI_SUCCESS)
2264 efi_tcg2_notify_exit_boot_services_failed();
2267 return EFI_EXIT(ret);
2271 * efi_get_next_monotonic_count() - get next value of the counter
2272 * @count: returned value of the counter
2274 * This function implements the NextMonotonicCount service.
2276 * See the Unified Extensible Firmware Interface (UEFI) specification for
2279 * Return: status code
2281 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2283 static uint64_t mono;
2286 EFI_ENTRY("%p", count);
2288 ret = EFI_INVALID_PARAMETER;
2294 return EFI_EXIT(ret);
2298 * efi_stall() - sleep
2299 * @microseconds: period to sleep in microseconds
2301 * This function implements the Stall service.
2303 * See the Unified Extensible Firmware Interface (UEFI) specification for
2306 * Return: status code
2308 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2312 EFI_ENTRY("%ld", microseconds);
2314 end_tick = get_ticks() + usec_to_tick(microseconds);
2315 while (get_ticks() < end_tick)
2318 return EFI_EXIT(EFI_SUCCESS);
2322 * efi_set_watchdog_timer() - reset the watchdog timer
2323 * @timeout: seconds before reset by watchdog
2324 * @watchdog_code: code to be logged when resetting
2325 * @data_size: size of buffer in bytes
2326 * @watchdog_data: buffer with data describing the reset reason
2328 * This function implements the SetWatchdogTimer service.
2330 * See the Unified Extensible Firmware Interface (UEFI) specification for
2333 * Return: status code
2335 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2336 uint64_t watchdog_code,
2337 unsigned long data_size,
2338 uint16_t *watchdog_data)
2340 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
2341 data_size, watchdog_data);
2342 return EFI_EXIT(efi_set_watchdog(timeout));
2346 * efi_close_protocol() - close a protocol
2347 * @handle: handle on which the protocol shall be closed
2348 * @protocol: GUID of the protocol to close
2349 * @agent_handle: handle of the driver
2350 * @controller_handle: handle of the controller
2352 * This is the function implementing the CloseProtocol service is for internal
2353 * usage in U-Boot. For API usage wrapper efi_close_protocol_ext() is provided.
2355 * See the Unified Extensible Firmware Interface (UEFI) specification for
2358 * Return: status code
2360 efi_status_t efi_close_protocol(efi_handle_t handle, const efi_guid_t *protocol,
2361 efi_handle_t agent_handle,
2362 efi_handle_t controller_handle)
2364 struct efi_handler *handler;
2365 struct efi_open_protocol_info_item *item;
2366 struct efi_open_protocol_info_item *pos;
2369 if (!efi_search_obj(agent_handle) ||
2370 (controller_handle && !efi_search_obj(controller_handle)))
2371 return EFI_INVALID_PARAMETER;
2372 ret = efi_search_protocol(handle, protocol, &handler);
2373 if (ret != EFI_SUCCESS)
2376 ret = EFI_NOT_FOUND;
2377 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2378 if (item->info.agent_handle == agent_handle &&
2379 item->info.controller_handle == controller_handle) {
2380 efi_delete_open_info(item);
2389 * efi_close_protocol_ext() - close a protocol
2390 * @handle: handle on which the protocol shall be closed
2391 * @protocol: GUID of the protocol to close
2392 * @agent_handle: handle of the driver
2393 * @controller_handle: handle of the controller
2395 * This function implements the CloseProtocol service.
2397 * See the Unified Extensible Firmware Interface (UEFI) specification for
2400 * Return: status code
2402 static efi_status_t EFIAPI
2403 efi_close_protocol_ext(efi_handle_t handle, const efi_guid_t *protocol,
2404 efi_handle_t agent_handle,
2405 efi_handle_t controller_handle)
2409 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, agent_handle,
2412 ret = efi_close_protocol(handle, protocol,
2413 agent_handle, controller_handle);
2415 return EFI_EXIT(ret);
2419 * efi_open_protocol_information() - provide information about then open status
2420 * of a protocol on a handle
2421 * @handle: handle for which the information shall be retrieved
2422 * @protocol: GUID of the protocol
2423 * @entry_buffer: buffer to receive the open protocol information
2424 * @entry_count: number of entries available in the buffer
2426 * This function implements the OpenProtocolInformation service.
2428 * See the Unified Extensible Firmware Interface (UEFI) specification for
2431 * Return: status code
2433 static efi_status_t EFIAPI efi_open_protocol_information(
2434 efi_handle_t handle, const efi_guid_t *protocol,
2435 struct efi_open_protocol_info_entry **entry_buffer,
2436 efi_uintn_t *entry_count)
2438 unsigned long buffer_size;
2439 unsigned long count;
2440 struct efi_handler *handler;
2441 struct efi_open_protocol_info_item *item;
2444 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, entry_buffer,
2447 /* Check parameters */
2448 if (!entry_buffer) {
2449 r = EFI_INVALID_PARAMETER;
2452 r = efi_search_protocol(handle, protocol, &handler);
2453 if (r != EFI_SUCCESS)
2458 list_for_each_entry(item, &handler->open_infos, link) {
2459 if (item->info.open_count)
2462 *entry_count = count;
2463 *entry_buffer = NULL;
2470 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2471 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2472 (void **)entry_buffer);
2473 if (r != EFI_SUCCESS)
2475 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2476 if (item->info.open_count)
2477 (*entry_buffer)[--count] = item->info;
2484 * efi_protocols_per_handle() - get protocols installed on a handle
2485 * @handle: handle for which the information is retrieved
2486 * @protocol_buffer: buffer with protocol GUIDs
2487 * @protocol_buffer_count: number of entries in the buffer
2489 * This function implements the ProtocolsPerHandleService.
2491 * See the Unified Extensible Firmware Interface (UEFI) specification for
2494 * Return: status code
2496 static efi_status_t EFIAPI efi_protocols_per_handle(
2497 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2498 efi_uintn_t *protocol_buffer_count)
2500 unsigned long buffer_size;
2501 struct efi_object *efiobj;
2502 struct list_head *protocol_handle;
2505 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2506 protocol_buffer_count);
2508 if (!handle || !protocol_buffer || !protocol_buffer_count)
2509 return EFI_EXIT(EFI_INVALID_PARAMETER);
2511 *protocol_buffer = NULL;
2513 efiobj = efi_search_obj(handle);
2515 return EFI_EXIT(EFI_INVALID_PARAMETER);
2517 *protocol_buffer_count = list_count_nodes(&efiobj->protocols);
2520 if (*protocol_buffer_count) {
2523 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2524 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2525 (void **)protocol_buffer);
2526 if (r != EFI_SUCCESS)
2528 list_for_each(protocol_handle, &efiobj->protocols) {
2529 struct efi_handler *protocol;
2531 protocol = list_entry(protocol_handle,
2532 struct efi_handler, link);
2533 (*protocol_buffer)[j] = (void *)&protocol->guid;
2538 return EFI_EXIT(EFI_SUCCESS);
2541 efi_status_t efi_locate_handle_buffer_int(enum efi_locate_search_type search_type,
2542 const efi_guid_t *protocol, void *search_key,
2543 efi_uintn_t *no_handles, efi_handle_t **buffer)
2546 efi_uintn_t buffer_size = 0;
2548 if (!no_handles || !buffer) {
2549 r = EFI_INVALID_PARAMETER;
2554 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2556 if (r != EFI_BUFFER_TOO_SMALL)
2558 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2560 if (r != EFI_SUCCESS)
2562 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2564 if (r == EFI_SUCCESS)
2565 *no_handles = buffer_size / sizeof(efi_handle_t);
2571 * efi_locate_handle_buffer() - locate handles implementing a protocol
2572 * @search_type: selection criterion
2573 * @protocol: GUID of the protocol
2574 * @search_key: registration key
2575 * @no_handles: number of returned handles
2576 * @buffer: buffer with the returned handles
2578 * This function implements the LocateHandleBuffer service.
2580 * See the Unified Extensible Firmware Interface (UEFI) specification for
2583 * Return: status code
2585 efi_status_t EFIAPI efi_locate_handle_buffer(
2586 enum efi_locate_search_type search_type,
2587 const efi_guid_t *protocol, void *search_key,
2588 efi_uintn_t *no_handles, efi_handle_t **buffer)
2592 EFI_ENTRY("%d, %pUs, %p, %p, %p", search_type, protocol, search_key,
2593 no_handles, buffer);
2595 r = efi_locate_handle_buffer_int(search_type, protocol, search_key,
2596 no_handles, buffer);
2602 * efi_locate_protocol() - find an interface implementing a protocol
2603 * @protocol: GUID of the protocol
2604 * @registration: registration key passed to the notification function
2605 * @protocol_interface: interface implementing the protocol
2607 * This function implements the LocateProtocol service.
2609 * See the Unified Extensible Firmware Interface (UEFI) specification for
2612 * Return: status code
2614 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2616 void **protocol_interface)
2618 struct efi_handler *handler;
2620 struct efi_object *efiobj;
2622 EFI_ENTRY("%pUs, %p, %p", protocol, registration, protocol_interface);
2625 * The UEFI spec explicitly requires a protocol even if a registration
2626 * key is provided. This differs from the logic in LocateHandle().
2628 if (!protocol || !protocol_interface)
2629 return EFI_EXIT(EFI_INVALID_PARAMETER);
2632 struct efi_register_notify_event *event;
2633 struct efi_protocol_notification *handle;
2635 event = efi_check_register_notify_event(registration);
2637 return EFI_EXIT(EFI_INVALID_PARAMETER);
2639 * The UEFI spec requires to return EFI_NOT_FOUND if no
2640 * protocol instance matches protocol and registration.
2641 * So let's do the same for a mismatch between protocol and
2644 if (guidcmp(&event->protocol, protocol))
2646 if (list_empty(&event->handles))
2648 handle = list_first_entry(&event->handles,
2649 struct efi_protocol_notification,
2651 efiobj = handle->handle;
2652 list_del(&handle->link);
2654 ret = efi_search_protocol(efiobj, protocol, &handler);
2655 if (ret == EFI_SUCCESS)
2658 list_for_each_entry(efiobj, &efi_obj_list, link) {
2659 ret = efi_search_protocol(efiobj, protocol, &handler);
2660 if (ret == EFI_SUCCESS)
2665 *protocol_interface = NULL;
2666 return EFI_EXIT(EFI_NOT_FOUND);
2668 *protocol_interface = handler->protocol_interface;
2669 return EFI_EXIT(EFI_SUCCESS);
2673 * efi_install_multiple_protocol_interfaces_int() - Install multiple protocol
2675 * @handle: handle on which the protocol interfaces shall be installed
2676 * @argptr: va_list of args
2678 * Core functionality of efi_install_multiple_protocol_interfaces
2679 * Must not be called directly
2681 * Return: status code
2683 static efi_status_t EFIAPI
2684 efi_install_multiple_protocol_interfaces_int(efi_handle_t *handle,
2687 const efi_guid_t *protocol;
2688 void *protocol_interface;
2689 efi_handle_t old_handle;
2690 efi_status_t ret = EFI_SUCCESS;
2692 efi_va_list argptr_copy;
2695 return EFI_INVALID_PARAMETER;
2697 efi_va_copy(argptr_copy, argptr);
2699 protocol = efi_va_arg(argptr, efi_guid_t*);
2702 protocol_interface = efi_va_arg(argptr, void*);
2703 /* Check that a device path has not been installed before */
2704 if (!guidcmp(protocol, &efi_guid_device_path)) {
2705 struct efi_device_path *dp = protocol_interface;
2707 ret = EFI_CALL(efi_locate_device_path(protocol, &dp,
2709 if (ret == EFI_SUCCESS &&
2710 dp->type == DEVICE_PATH_TYPE_END) {
2711 EFI_PRINT("Path %pD already installed\n",
2712 protocol_interface);
2713 ret = EFI_ALREADY_STARTED;
2717 ret = EFI_CALL(efi_install_protocol_interface(handle, protocol,
2718 EFI_NATIVE_INTERFACE,
2719 protocol_interface));
2720 if (ret != EFI_SUCCESS)
2724 if (ret == EFI_SUCCESS)
2727 /* If an error occurred undo all changes. */
2729 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2730 protocol_interface = efi_va_arg(argptr_copy, void*);
2731 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2732 protocol_interface));
2736 efi_va_end(argptr_copy);
2742 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2744 * @handle: handle on which the protocol interfaces shall be installed
2745 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2749 * This is the function for internal usage in U-Boot. For the API function
2750 * implementing the InstallMultipleProtocol service see
2751 * efi_install_multiple_protocol_interfaces_ext()
2753 * Return: status code
2756 efi_install_multiple_protocol_interfaces(efi_handle_t *handle, ...)
2761 efi_va_start(argptr, handle);
2762 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2768 * efi_install_multiple_protocol_interfaces_ext() - Install multiple protocol
2770 * @handle: handle on which the protocol interfaces shall be installed
2771 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2774 * This function implements the MultipleProtocolInterfaces service.
2776 * See the Unified Extensible Firmware Interface (UEFI) specification for
2779 * Return: status code
2781 static efi_status_t EFIAPI
2782 efi_install_multiple_protocol_interfaces_ext(efi_handle_t *handle, ...)
2784 EFI_ENTRY("%p", handle);
2788 efi_va_start(argptr, handle);
2789 ret = efi_install_multiple_protocol_interfaces_int(handle, argptr);
2791 return EFI_EXIT(ret);
2795 * efi_uninstall_multiple_protocol_interfaces_int() - wrapper for uninstall
2798 * @handle: handle from which the protocol interfaces shall be removed
2799 * @argptr: va_list of args
2801 * Core functionality of efi_uninstall_multiple_protocol_interfaces
2802 * Must not be called directly
2804 * Return: status code
2806 static efi_status_t EFIAPI
2807 efi_uninstall_multiple_protocol_interfaces_int(efi_handle_t handle,
2810 const efi_guid_t *protocol, *next_protocol;
2811 void *protocol_interface;
2812 efi_status_t ret = EFI_SUCCESS;
2814 efi_va_list argptr_copy;
2817 return EFI_INVALID_PARAMETER;
2819 efi_va_copy(argptr_copy, argptr);
2820 protocol = efi_va_arg(argptr, efi_guid_t*);
2823 * If efi_uninstall_protocol() fails we need to be able to
2824 * reinstall the previously uninstalled protocols on the same
2826 * Instead of calling efi_uninstall_protocol(...,..., false)
2827 * and potentially removing the handle, only allow the handle
2828 * removal on the last protocol that we requested to uninstall.
2829 * That way we can preserve the handle in case the latter fails
2831 bool preserve = true;
2835 protocol_interface = efi_va_arg(argptr, void*);
2836 next_protocol = efi_va_arg(argptr, efi_guid_t*);
2839 ret = efi_uninstall_protocol(handle, protocol,
2840 protocol_interface, preserve);
2841 if (ret != EFI_SUCCESS)
2844 protocol = next_protocol;
2846 if (ret == EFI_SUCCESS)
2849 /* If an error occurred undo all changes. */
2851 protocol = efi_va_arg(argptr_copy, efi_guid_t*);
2852 protocol_interface = efi_va_arg(argptr_copy, void*);
2853 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2854 EFI_NATIVE_INTERFACE,
2855 protocol_interface));
2858 * If any errors are generated while the protocol interfaces are being
2859 * uninstalled, then the protocols uninstalled prior to the error will
2860 * be reinstalled using InstallProtocolInterface() and the status code
2861 * EFI_INVALID_PARAMETER is returned.
2863 ret = EFI_INVALID_PARAMETER;
2866 efi_va_end(argptr_copy);
2871 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2873 * @handle: handle from which the protocol interfaces shall be removed
2874 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2877 * This function implements the UninstallMultipleProtocolInterfaces service.
2879 * This is the function for internal usage in U-Boot. For the API function
2880 * implementing the UninstallMultipleProtocolInterfaces service see
2881 * efi_uninstall_multiple_protocol_interfaces_ext()
2883 * Return: status code
2886 efi_uninstall_multiple_protocol_interfaces(efi_handle_t handle, ...)
2891 efi_va_start(argptr, handle);
2892 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
2898 * efi_uninstall_multiple_protocol_interfaces_ext() - uninstall multiple protocol
2900 * @handle: handle from which the protocol interfaces shall be removed
2901 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2904 * This function implements the UninstallMultipleProtocolInterfaces service.
2906 * See the Unified Extensible Firmware Interface (UEFI) specification for
2909 * Return: status code
2911 static efi_status_t EFIAPI
2912 efi_uninstall_multiple_protocol_interfaces_ext(efi_handle_t handle, ...)
2914 EFI_ENTRY("%p", handle);
2918 efi_va_start(argptr, handle);
2919 ret = efi_uninstall_multiple_protocol_interfaces_int(handle, argptr);
2921 return EFI_EXIT(ret);
2925 * efi_calculate_crc32() - calculate cyclic redundancy code
2926 * @data: buffer with data
2927 * @data_size: size of buffer in bytes
2928 * @crc32_p: cyclic redundancy code
2930 * This function implements the CalculateCrc32 service.
2932 * See the Unified Extensible Firmware Interface (UEFI) specification for
2935 * Return: status code
2937 static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2938 efi_uintn_t data_size,
2941 efi_status_t ret = EFI_SUCCESS;
2943 EFI_ENTRY("%p, %zu", data, data_size);
2944 if (!data || !data_size || !crc32_p) {
2945 ret = EFI_INVALID_PARAMETER;
2948 *crc32_p = crc32(0, data, data_size);
2950 return EFI_EXIT(ret);
2954 * efi_copy_mem() - copy memory
2955 * @destination: destination of the copy operation
2956 * @source: source of the copy operation
2957 * @length: number of bytes to copy
2959 * This function implements the CopyMem service.
2961 * See the Unified Extensible Firmware Interface (UEFI) specification for
2964 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2967 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2968 memmove(destination, source, length);
2969 EFI_EXIT(EFI_SUCCESS);
2973 * efi_set_mem() - Fill memory with a byte value.
2974 * @buffer: buffer to fill
2975 * @size: size of buffer in bytes
2976 * @value: byte to copy to the buffer
2978 * This function implements the SetMem service.
2980 * See the Unified Extensible Firmware Interface (UEFI) specification for
2983 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2985 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2986 memset(buffer, value, size);
2987 EFI_EXIT(EFI_SUCCESS);
2991 * efi_protocol_open() - open protocol interface on a handle
2992 * @handler: handler of a protocol
2993 * @protocol_interface: interface implementing the protocol
2994 * @agent_handle: handle of the driver
2995 * @controller_handle: handle of the controller
2996 * @attributes: attributes indicating how to open the protocol
2998 * Return: status code
3000 efi_status_t efi_protocol_open(
3001 struct efi_handler *handler,
3002 void **protocol_interface, void *agent_handle,
3003 void *controller_handle, uint32_t attributes)
3005 struct efi_open_protocol_info_item *item;
3006 struct efi_open_protocol_info_entry *match = NULL;
3007 bool opened_by_driver = false;
3008 bool opened_exclusive = false;
3010 /* If there is no agent, only return the interface */
3014 /* For TEST_PROTOCOL ignore interface attribute */
3015 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3016 *protocol_interface = NULL;
3019 * Check if the protocol is already opened by a driver with the same
3020 * attributes or opened exclusively
3022 list_for_each_entry(item, &handler->open_infos, link) {
3023 if (item->info.agent_handle == agent_handle) {
3024 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
3025 (item->info.attributes == attributes))
3026 return EFI_ALREADY_STARTED;
3028 if (item->info.attributes &
3029 EFI_OPEN_PROTOCOL_BY_DRIVER)
3030 opened_by_driver = true;
3032 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
3033 opened_exclusive = true;
3036 /* Only one controller can open the protocol exclusively */
3037 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3038 if (opened_exclusive)
3039 return EFI_ACCESS_DENIED;
3040 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
3041 if (opened_exclusive || opened_by_driver)
3042 return EFI_ACCESS_DENIED;
3045 /* Prepare exclusive opening */
3046 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
3047 /* Try to disconnect controllers */
3049 opened_by_driver = false;
3050 list_for_each_entry(item, &handler->open_infos, link) {
3053 if (item->info.attributes ==
3054 EFI_OPEN_PROTOCOL_BY_DRIVER) {
3055 ret = EFI_CALL(efi_disconnect_controller(
3056 item->info.controller_handle,
3057 item->info.agent_handle,
3059 if (ret == EFI_SUCCESS)
3061 * Child controllers may have been
3062 * removed from the open_infos list. So
3063 * let's restart the loop.
3065 goto disconnect_next;
3067 opened_by_driver = true;
3070 /* Only one driver can be connected */
3071 if (opened_by_driver)
3072 return EFI_ACCESS_DENIED;
3075 /* Find existing entry */
3076 list_for_each_entry(item, &handler->open_infos, link) {
3077 if (item->info.agent_handle == agent_handle &&
3078 item->info.controller_handle == controller_handle &&
3079 item->info.attributes == attributes)
3080 match = &item->info;
3082 /* None found, create one */
3084 match = efi_create_open_info(handler);
3086 return EFI_OUT_OF_RESOURCES;
3089 match->agent_handle = agent_handle;
3090 match->controller_handle = controller_handle;
3091 match->attributes = attributes;
3092 match->open_count++;
3095 /* For TEST_PROTOCOL ignore interface attribute. */
3096 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
3097 *protocol_interface = handler->protocol_interface;
3103 * efi_open_protocol() - open protocol interface on a handle
3104 * @handle: handle on which the protocol shall be opened
3105 * @protocol: GUID of the protocol
3106 * @protocol_interface: interface implementing the protocol
3107 * @agent_handle: handle of the driver
3108 * @controller_handle: handle of the controller
3109 * @attributes: attributes indicating how to open the protocol
3111 * This function implements the OpenProtocol interface.
3113 * See the Unified Extensible Firmware Interface (UEFI) specification for
3116 * Return: status code
3118 static efi_status_t EFIAPI efi_open_protocol
3119 (efi_handle_t handle, const efi_guid_t *protocol,
3120 void **protocol_interface, efi_handle_t agent_handle,
3121 efi_handle_t controller_handle, uint32_t attributes)
3123 struct efi_handler *handler;
3124 efi_status_t r = EFI_INVALID_PARAMETER;
3126 EFI_ENTRY("%p, %pUs, %p, %p, %p, 0x%x", handle, protocol,
3127 protocol_interface, agent_handle, controller_handle,
3130 if (!handle || !protocol ||
3131 (!protocol_interface && attributes !=
3132 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
3136 switch (attributes) {
3137 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
3138 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
3139 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
3141 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
3142 if (controller_handle == handle)
3145 case EFI_OPEN_PROTOCOL_BY_DRIVER:
3146 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
3147 /* Check that the controller handle is valid */
3148 if (!efi_search_obj(controller_handle))
3151 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
3152 /* Check that the agent handle is valid */
3153 if (!efi_search_obj(agent_handle))
3160 r = efi_search_protocol(handle, protocol, &handler);
3165 r = EFI_UNSUPPORTED;
3171 r = efi_protocol_open(handler, protocol_interface, agent_handle,
3172 controller_handle, attributes);
3178 * efi_start_image() - call the entry point of an image
3179 * @image_handle: handle of the image
3180 * @exit_data_size: size of the buffer
3181 * @exit_data: buffer to receive the exit data of the called image
3183 * This function implements the StartImage service.
3185 * See the Unified Extensible Firmware Interface (UEFI) specification for
3188 * Return: status code
3190 efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
3191 efi_uintn_t *exit_data_size,
3194 struct efi_loaded_image_obj *image_obj =
3195 (struct efi_loaded_image_obj *)image_handle;
3198 efi_handle_t parent_image = current_image;
3199 efi_status_t exit_status;
3200 struct jmp_buf_data exit_jmp;
3202 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
3204 if (!efi_search_obj(image_handle))
3205 return EFI_EXIT(EFI_INVALID_PARAMETER);
3207 /* Check parameters */
3208 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
3209 return EFI_EXIT(EFI_INVALID_PARAMETER);
3211 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
3212 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3214 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3216 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3217 if (ret != EFI_SUCCESS)
3218 return EFI_EXIT(EFI_INVALID_PARAMETER);
3220 image_obj->exit_data_size = exit_data_size;
3221 image_obj->exit_data = exit_data;
3222 image_obj->exit_status = &exit_status;
3223 image_obj->exit_jmp = &exit_jmp;
3225 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3226 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3227 ret = efi_tcg2_measure_efi_app_invocation(image_obj);
3228 if (ret == EFI_SECURITY_VIOLATION) {
3230 * TCG2 Protocol is installed but no TPM device found,
3231 * this is not expected.
3233 return EFI_EXIT(EFI_SECURITY_VIOLATION);
3238 /* call the image! */
3239 if (setjmp(&exit_jmp)) {
3241 * We called the entry point of the child image with EFI_CALL
3242 * in the lines below. The child image called the Exit() boot
3243 * service efi_exit() which executed the long jump that brought
3244 * us to the current line. This implies that the second half
3245 * of the EFI_CALL macro has not been executed.
3247 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
3249 * efi_exit() called efi_restore_gd(). We have to undo this
3250 * otherwise __efi_entry_check() will put the wrong value into
3256 * To get ready to call EFI_EXIT below we have to execute the
3257 * missed out steps of EFI_CALL.
3259 assert(__efi_entry_check());
3260 EFI_PRINT("%lu returned by started image\n",
3261 (unsigned long)((uintptr_t)exit_status &
3263 current_image = parent_image;
3264 return EFI_EXIT(exit_status);
3267 current_image = image_handle;
3268 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
3269 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
3270 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3273 * Control is returned from a started UEFI image either by calling
3274 * Exit() (where exit data can be provided) or by simply returning from
3275 * the entry point. In the latter case call Exit() on behalf of the
3278 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3282 * efi_delete_image() - delete loaded image from memory)
3284 * @image_obj: handle of the loaded image
3285 * @loaded_image_protocol: loaded image protocol
3287 static efi_status_t efi_delete_image
3288 (struct efi_loaded_image_obj *image_obj,
3289 struct efi_loaded_image *loaded_image_protocol)
3291 struct efi_object *efiobj;
3292 efi_status_t r, ret = EFI_SUCCESS;
3295 list_for_each_entry(efiobj, &efi_obj_list, link) {
3296 struct efi_handler *protocol;
3298 list_for_each_entry(protocol, &efiobj->protocols, link) {
3299 struct efi_open_protocol_info_item *info;
3301 list_for_each_entry(info, &protocol->open_infos, link) {
3302 if (info->info.agent_handle !=
3303 (efi_handle_t)image_obj)
3305 r = efi_close_protocol(
3306 efiobj, &protocol->guid,
3307 info->info.agent_handle,
3308 info->info.controller_handle);
3309 if (r != EFI_SUCCESS)
3312 * Closing protocols may results in further
3313 * items being deleted. To play it safe loop
3314 * over all elements again.
3321 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3322 efi_size_in_pages(loaded_image_protocol->image_size));
3323 efi_delete_handle(&image_obj->header);
3329 * efi_unload_image() - unload an EFI image
3330 * @image_handle: handle of the image to be unloaded
3332 * This function implements the UnloadImage service.
3334 * See the Unified Extensible Firmware Interface (UEFI) specification for
3337 * Return: status code
3339 efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3341 efi_status_t ret = EFI_SUCCESS;
3342 struct efi_object *efiobj;
3343 struct efi_loaded_image *loaded_image_protocol;
3345 EFI_ENTRY("%p", image_handle);
3347 efiobj = efi_search_obj(image_handle);
3349 ret = EFI_INVALID_PARAMETER;
3352 /* Find the loaded image protocol */
3353 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3354 (void **)&loaded_image_protocol,
3356 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3357 if (ret != EFI_SUCCESS) {
3358 ret = EFI_INVALID_PARAMETER;
3361 switch (efiobj->type) {
3362 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3363 /* Call the unload function */
3364 if (!loaded_image_protocol->unload) {
3365 ret = EFI_UNSUPPORTED;
3368 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3369 if (ret != EFI_SUCCESS)
3372 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3375 ret = EFI_INVALID_PARAMETER;
3378 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3379 loaded_image_protocol);
3381 return EFI_EXIT(ret);
3385 * efi_update_exit_data() - fill exit data parameters of StartImage()
3387 * @image_obj: image handle
3388 * @exit_data_size: size of the exit data buffer
3389 * @exit_data: buffer with data returned by UEFI payload
3390 * Return: status code
3392 static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3393 efi_uintn_t exit_data_size,
3399 * If exit_data is not provided to StartImage(), exit_data_size must be
3402 if (!image_obj->exit_data)
3404 if (image_obj->exit_data_size)
3405 *image_obj->exit_data_size = exit_data_size;
3406 if (exit_data_size && exit_data) {
3407 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3409 (void **)image_obj->exit_data);
3410 if (ret != EFI_SUCCESS)
3412 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3414 image_obj->exit_data = NULL;
3420 * efi_exit() - leave an EFI application or driver
3421 * @image_handle: handle of the application or driver that is exiting
3422 * @exit_status: status code
3423 * @exit_data_size: size of the buffer in bytes
3424 * @exit_data: buffer with data describing an error
3426 * This function implements the Exit service.
3428 * See the Unified Extensible Firmware Interface (UEFI) specification for
3431 * Return: status code
3433 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3434 efi_status_t exit_status,
3435 efi_uintn_t exit_data_size,
3439 * TODO: We should call the unload procedure of the loaded
3443 struct efi_loaded_image *loaded_image_protocol;
3444 struct efi_loaded_image_obj *image_obj =
3445 (struct efi_loaded_image_obj *)image_handle;
3446 struct jmp_buf_data *exit_jmp;
3448 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3449 exit_data_size, exit_data);
3451 /* Check parameters */
3452 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3453 (void **)&loaded_image_protocol,
3455 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3456 if (ret != EFI_SUCCESS) {
3457 ret = EFI_INVALID_PARAMETER;
3461 /* Unloading of unstarted images */
3462 switch (image_obj->header.type) {
3463 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3465 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3466 efi_delete_image(image_obj, loaded_image_protocol);
3470 /* Handle does not refer to loaded image */
3471 ret = EFI_INVALID_PARAMETER;
3474 /* A started image can only be unloaded it is the last one started. */
3475 if (image_handle != current_image) {
3476 ret = EFI_INVALID_PARAMETER;
3480 /* Exit data is only foreseen in case of failure. */
3481 if (exit_status != EFI_SUCCESS) {
3482 ret = efi_update_exit_data(image_obj, exit_data_size,
3484 /* Exiting has priority. Don't return error to caller. */
3485 if (ret != EFI_SUCCESS)
3486 EFI_PRINT("%s: out of memory\n", __func__);
3488 /* efi_delete_image() frees image_obj. Copy before the call. */
3489 exit_jmp = image_obj->exit_jmp;
3490 *image_obj->exit_status = exit_status;
3491 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3492 exit_status != EFI_SUCCESS)
3493 efi_delete_image(image_obj, loaded_image_protocol);
3495 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
3496 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION) {
3497 ret = efi_tcg2_measure_efi_app_exit();
3498 if (ret != EFI_SUCCESS) {
3499 log_warning("tcg2 measurement fails(0x%lx)\n",
3505 /* Make sure entry/exit counts for EFI world cross-overs match */
3506 EFI_EXIT(exit_status);
3509 * But longjmp out with the U-Boot gd, not the application's, as
3510 * the other end is a setjmp call inside EFI context.
3514 longjmp(exit_jmp, 1);
3516 panic("EFI application exited");
3518 return EFI_EXIT(ret);
3522 * efi_handle_protocol() - get interface of a protocol on a handle
3523 * @handle: handle on which the protocol shall be opened
3524 * @protocol: GUID of the protocol
3525 * @protocol_interface: interface implementing the protocol
3527 * This function implements the HandleProtocol service.
3529 * See the Unified Extensible Firmware Interface (UEFI) specification for
3532 * Return: status code
3534 efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3535 const efi_guid_t *protocol,
3536 void **protocol_interface)
3538 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
3539 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
3543 * efi_bind_controller() - bind a single driver to a controller
3544 * @controller_handle: controller handle
3545 * @driver_image_handle: driver handle
3546 * @remain_device_path: remaining path
3548 * Return: status code
3550 static efi_status_t efi_bind_controller(
3551 efi_handle_t controller_handle,
3552 efi_handle_t driver_image_handle,
3553 struct efi_device_path *remain_device_path)
3555 struct efi_driver_binding_protocol *binding_protocol;
3558 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3559 &efi_guid_driver_binding_protocol,
3560 (void **)&binding_protocol,
3561 driver_image_handle, NULL,
3562 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3563 if (r != EFI_SUCCESS)
3565 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3567 remain_device_path));
3568 if (r == EFI_SUCCESS)
3569 r = EFI_CALL(binding_protocol->start(binding_protocol,
3571 remain_device_path));
3572 efi_close_protocol(driver_image_handle,
3573 &efi_guid_driver_binding_protocol,
3574 driver_image_handle, NULL);
3579 * efi_connect_single_controller() - connect a single driver to a controller
3580 * @controller_handle: controller
3581 * @driver_image_handle: driver
3582 * @remain_device_path: remaining path
3584 * Return: status code
3586 static efi_status_t efi_connect_single_controller(
3587 efi_handle_t controller_handle,
3588 efi_handle_t *driver_image_handle,
3589 struct efi_device_path *remain_device_path)
3591 efi_handle_t *buffer;
3595 size_t connected = 0;
3597 /* Get buffer with all handles with driver binding protocol */
3598 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3599 &efi_guid_driver_binding_protocol,
3600 NULL, &count, &buffer));
3601 if (r != EFI_SUCCESS)
3604 /* Context Override */
3605 if (driver_image_handle) {
3606 for (; *driver_image_handle; ++driver_image_handle) {
3607 for (i = 0; i < count; ++i) {
3608 if (buffer[i] == *driver_image_handle) {
3610 r = efi_bind_controller(
3612 *driver_image_handle,
3613 remain_device_path);
3615 * For drivers that do not support the
3616 * controller or are already connected
3617 * we receive an error code here.
3619 if (r == EFI_SUCCESS)
3627 * TODO: Some overrides are not yet implemented:
3628 * - Platform Driver Override
3629 * - Driver Family Override Search
3630 * - Bus Specific Driver Override
3633 /* Driver Binding Search */
3634 for (i = 0; i < count; ++i) {
3636 r = efi_bind_controller(controller_handle,
3638 remain_device_path);
3639 if (r == EFI_SUCCESS)
3644 efi_free_pool(buffer);
3646 return EFI_NOT_FOUND;
3651 * efi_connect_controller() - connect a controller to a driver
3652 * @controller_handle: handle of the controller
3653 * @driver_image_handle: handle of the driver
3654 * @remain_device_path: device path of a child controller
3655 * @recursive: true to connect all child controllers
3657 * This function implements the ConnectController service.
3659 * See the Unified Extensible Firmware Interface (UEFI) specification for
3662 * First all driver binding protocol handles are tried for binding drivers.
3663 * Afterwards all handles that have opened a protocol of the controller
3664 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3666 * Return: status code
3668 static efi_status_t EFIAPI efi_connect_controller(
3669 efi_handle_t controller_handle,
3670 efi_handle_t *driver_image_handle,
3671 struct efi_device_path *remain_device_path,
3675 efi_status_t ret = EFI_NOT_FOUND;
3676 struct efi_object *efiobj;
3678 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
3679 remain_device_path, recursive);
3681 efiobj = efi_search_obj(controller_handle);
3683 ret = EFI_INVALID_PARAMETER;
3687 r = efi_connect_single_controller(controller_handle,
3688 driver_image_handle,
3689 remain_device_path);
3690 if (r == EFI_SUCCESS)
3693 struct efi_handler *handler;
3694 struct efi_open_protocol_info_item *item;
3696 list_for_each_entry(handler, &efiobj->protocols, link) {
3697 list_for_each_entry(item, &handler->open_infos, link) {
3698 if (item->info.attributes &
3699 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3700 r = EFI_CALL(efi_connect_controller(
3701 item->info.controller_handle,
3702 driver_image_handle,
3705 if (r == EFI_SUCCESS)
3711 /* Check for child controller specified by end node */
3712 if (ret != EFI_SUCCESS && remain_device_path &&
3713 remain_device_path->type == DEVICE_PATH_TYPE_END)
3716 return EFI_EXIT(ret);
3720 * efi_reinstall_protocol_interface() - reinstall protocol interface
3721 * @handle: handle on which the protocol shall be reinstalled
3722 * @protocol: GUID of the protocol to be installed
3723 * @old_interface: interface to be removed
3724 * @new_interface: interface to be installed
3726 * This function implements the ReinstallProtocolInterface service.
3728 * See the Unified Extensible Firmware Interface (UEFI) specification for
3731 * The old interface is uninstalled. The new interface is installed.
3732 * Drivers are connected.
3734 * Return: status code
3736 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3737 efi_handle_t handle, const efi_guid_t *protocol,
3738 void *old_interface, void *new_interface)
3742 EFI_ENTRY("%p, %pUs, %p, %p", handle, protocol, old_interface,
3745 /* Uninstall protocol but do not delete handle */
3746 ret = efi_uninstall_protocol(handle, protocol, old_interface, true);
3747 if (ret != EFI_SUCCESS)
3750 /* Install the new protocol */
3751 ret = efi_add_protocol(handle, protocol, new_interface);
3753 * The UEFI spec does not specify what should happen to the handle
3754 * if in case of an error no protocol interface remains on the handle.
3755 * So let's do nothing here.
3757 if (ret != EFI_SUCCESS)
3760 * The returned status code has to be ignored.
3761 * Do not create an error if no suitable driver for the handle exists.
3763 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3765 return EFI_EXIT(ret);
3769 * efi_get_child_controllers() - get all child controllers associated to a driver
3770 * @efiobj: handle of the controller
3771 * @driver_handle: handle of the driver
3772 * @number_of_children: number of child controllers
3773 * @child_handle_buffer: handles of the the child controllers
3775 * The allocated buffer has to be freed with free().
3777 * Return: status code
3779 static efi_status_t efi_get_child_controllers(
3780 struct efi_object *efiobj,
3781 efi_handle_t driver_handle,
3782 efi_uintn_t *number_of_children,
3783 efi_handle_t **child_handle_buffer)
3785 struct efi_handler *handler;
3786 struct efi_open_protocol_info_item *item;
3787 efi_uintn_t count = 0, i;
3790 /* Count all child controller associations */
3791 list_for_each_entry(handler, &efiobj->protocols, link) {
3792 list_for_each_entry(item, &handler->open_infos, link) {
3793 if (item->info.agent_handle == driver_handle &&
3794 item->info.attributes &
3795 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3800 * Create buffer. In case of duplicate child controller assignments
3801 * the buffer will be too large. But that does not harm.
3803 *number_of_children = 0;
3806 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3807 if (!*child_handle_buffer)
3808 return EFI_OUT_OF_RESOURCES;
3809 /* Copy unique child handles */
3810 list_for_each_entry(handler, &efiobj->protocols, link) {
3811 list_for_each_entry(item, &handler->open_infos, link) {
3812 if (item->info.agent_handle == driver_handle &&
3813 item->info.attributes &
3814 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3815 /* Check this is a new child controller */
3817 for (i = 0; i < *number_of_children; ++i) {
3818 if ((*child_handle_buffer)[i] ==
3819 item->info.controller_handle)
3822 /* Copy handle to buffer */
3824 i = (*number_of_children)++;
3825 (*child_handle_buffer)[i] =
3826 item->info.controller_handle;
3835 * efi_disconnect_controller() - disconnect a controller from a driver
3836 * @controller_handle: handle of the controller
3837 * @driver_image_handle: handle of the driver
3838 * @child_handle: handle of the child to destroy
3840 * This function implements the DisconnectController service.
3842 * See the Unified Extensible Firmware Interface (UEFI) specification for
3845 * Return: status code
3847 static efi_status_t EFIAPI efi_disconnect_controller(
3848 efi_handle_t controller_handle,
3849 efi_handle_t driver_image_handle,
3850 efi_handle_t child_handle)
3852 struct efi_driver_binding_protocol *binding_protocol;
3853 efi_handle_t *child_handle_buffer = NULL;
3854 size_t number_of_children = 0;
3856 struct efi_object *efiobj;
3859 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3862 efiobj = efi_search_obj(controller_handle);
3864 r = EFI_INVALID_PARAMETER;
3868 if (child_handle && !efi_search_obj(child_handle)) {
3869 r = EFI_INVALID_PARAMETER;
3873 /* If no driver handle is supplied, disconnect all drivers */
3874 if (!driver_image_handle) {
3875 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3879 /* Create list of child handles */
3880 r = efi_get_child_controllers(efiobj,
3881 driver_image_handle,
3882 &number_of_children,
3883 &child_handle_buffer);
3884 if (r != EFI_SUCCESS)
3886 sole_child = (number_of_children == 1);
3889 number_of_children = 1;
3890 free(child_handle_buffer);
3891 child_handle_buffer = &child_handle;
3894 /* Get the driver binding protocol */
3895 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3896 &efi_guid_driver_binding_protocol,
3897 (void **)&binding_protocol,
3898 driver_image_handle, NULL,
3899 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3900 if (r != EFI_SUCCESS) {
3901 r = EFI_INVALID_PARAMETER;
3904 /* Remove the children */
3905 if (number_of_children) {
3906 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3909 child_handle_buffer));
3910 if (r != EFI_SUCCESS) {
3911 r = EFI_DEVICE_ERROR;
3915 /* Remove the driver */
3916 if (!child_handle || sole_child) {
3917 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3920 if (r != EFI_SUCCESS) {
3921 r = EFI_DEVICE_ERROR;
3925 efi_close_protocol(driver_image_handle,
3926 &efi_guid_driver_binding_protocol,
3927 driver_image_handle, NULL);
3931 free(child_handle_buffer);
3935 static struct efi_boot_services efi_boot_services = {
3937 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3938 .revision = EFI_SPECIFICATION_VERSION,
3939 .headersize = sizeof(struct efi_boot_services),
3941 .raise_tpl = efi_raise_tpl,
3942 .restore_tpl = efi_restore_tpl,
3943 .allocate_pages = efi_allocate_pages_ext,
3944 .free_pages = efi_free_pages_ext,
3945 .get_memory_map = efi_get_memory_map_ext,
3946 .allocate_pool = efi_allocate_pool_ext,
3947 .free_pool = efi_free_pool_ext,
3948 .create_event = efi_create_event_ext,
3949 .set_timer = efi_set_timer_ext,
3950 .wait_for_event = efi_wait_for_event,
3951 .signal_event = efi_signal_event_ext,
3952 .close_event = efi_close_event,
3953 .check_event = efi_check_event,
3954 .install_protocol_interface = efi_install_protocol_interface,
3955 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3956 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3957 .handle_protocol = efi_handle_protocol,
3959 .register_protocol_notify = efi_register_protocol_notify,
3960 .locate_handle = efi_locate_handle_ext,
3961 .locate_device_path = efi_locate_device_path,
3962 .install_configuration_table = efi_install_configuration_table_ext,
3963 .load_image = efi_load_image,
3964 .start_image = efi_start_image,
3966 .unload_image = efi_unload_image,
3967 .exit_boot_services = efi_exit_boot_services,
3968 .get_next_monotonic_count = efi_get_next_monotonic_count,
3970 .set_watchdog_timer = efi_set_watchdog_timer,
3971 .connect_controller = efi_connect_controller,
3972 .disconnect_controller = efi_disconnect_controller,
3973 .open_protocol = efi_open_protocol,
3974 .close_protocol = efi_close_protocol_ext,
3975 .open_protocol_information = efi_open_protocol_information,
3976 .protocols_per_handle = efi_protocols_per_handle,
3977 .locate_handle_buffer = efi_locate_handle_buffer,
3978 .locate_protocol = efi_locate_protocol,
3979 .install_multiple_protocol_interfaces =
3980 efi_install_multiple_protocol_interfaces_ext,
3981 .uninstall_multiple_protocol_interfaces =
3982 efi_uninstall_multiple_protocol_interfaces_ext,
3983 .calculate_crc32 = efi_calculate_crc32,
3984 .copy_mem = efi_copy_mem,
3985 .set_mem = efi_set_mem,
3986 .create_event_ex = efi_create_event_ex,
3989 static u16 __efi_runtime_data firmware_vendor[] = u"Das U-Boot";
3991 struct efi_system_table __efi_runtime_data systab = {
3993 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3994 .revision = EFI_SPECIFICATION_VERSION,
3995 .headersize = sizeof(struct efi_system_table),
3997 .fw_vendor = firmware_vendor,
3998 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3999 .runtime = &efi_runtime_services,
4005 * efi_initialize_system_table() - Initialize system table
4007 * Return: status code
4009 efi_status_t efi_initialize_system_table(void)
4013 /* Allocate configuration table array */
4014 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
4015 EFI_MAX_CONFIGURATION_TABLES *
4016 sizeof(struct efi_configuration_table),
4017 (void **)&systab.tables);
4020 * These entries will be set to NULL in ExitBootServices(). To avoid
4021 * relocation in SetVirtualAddressMap(), set them dynamically.
4023 systab.con_in_handle = efi_root;
4024 systab.con_in = &efi_con_in;
4025 systab.con_out_handle = efi_root;
4026 systab.con_out = &efi_con_out;
4027 systab.stderr_handle = efi_root;
4028 systab.std_err = &efi_con_out;
4029 systab.boottime = &efi_boot_services;
4031 /* Set CRC32 field in table headers */
4032 efi_update_table_header_crc32(&systab.hdr);
4033 efi_update_table_header_crc32(&efi_runtime_services.hdr);
4034 efi_update_table_header_crc32(&efi_boot_services.hdr);