1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application boot time services
5 * Copyright (c) 2016 Alexander Graf
10 #include <efi_loader.h>
11 #include <environment.h>
13 #include <linux/libfdt_env.h>
14 #include <u-boot/crc.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 /* Task priority level */
22 static efi_uintn_t efi_tpl = TPL_APPLICATION;
24 /* This list contains all the EFI objects our payload has access to */
25 LIST_HEAD(efi_obj_list);
27 /* List of all events */
28 LIST_HEAD(efi_events);
30 /* Handle of the currently executing image */
31 static efi_handle_t current_image;
34 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
35 * we need to do trickery with caches. Since we don't want to break the EFI
36 * aware boot path, only apply hacks when loading exiting directly (breaking
37 * direct Linux EFI booting along the way - oh well).
39 static bool efi_is_direct_boot = true;
43 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
44 * fixed when compiling U-Boot. However, the payload does not know about that
45 * restriction so we need to manually swap its and our view of that register on
46 * EFI callback entry/exit.
48 static volatile void *efi_gd, *app_gd;
51 /* 1 if inside U-Boot code, 0 if inside EFI payload code */
52 static int entry_count = 1;
53 static int nesting_level;
54 /* GUID of the device tree table */
55 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
56 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
57 const efi_guid_t efi_guid_driver_binding_protocol =
58 EFI_DRIVER_BINDING_PROTOCOL_GUID;
60 /* event group ExitBootServices() invoked */
61 const efi_guid_t efi_guid_event_group_exit_boot_services =
62 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
63 /* event group SetVirtualAddressMap() invoked */
64 const efi_guid_t efi_guid_event_group_virtual_address_change =
65 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
66 /* event group memory map changed */
67 const efi_guid_t efi_guid_event_group_memory_map_change =
68 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
69 /* event group boot manager about to boot */
70 const efi_guid_t efi_guid_event_group_ready_to_boot =
71 EFI_EVENT_GROUP_READY_TO_BOOT;
72 /* event group ResetSystem() invoked (before ExitBootServices) */
73 const efi_guid_t efi_guid_event_group_reset_system =
74 EFI_EVENT_GROUP_RESET_SYSTEM;
76 static efi_status_t EFIAPI efi_disconnect_controller(
77 efi_handle_t controller_handle,
78 efi_handle_t driver_image_handle,
79 efi_handle_t child_handle);
81 /* Called on every callback entry */
82 int __efi_entry_check(void)
84 int ret = entry_count++ == 0;
93 /* Called on every callback exit */
94 int __efi_exit_check(void)
96 int ret = --entry_count == 0;
103 /* Called from do_bootefi_exec() */
104 void efi_save_gd(void)
112 * Special case handler for error/abort that just forces things back to u-boot
113 * world so we can dump out an abort message, without any care about returning
114 * back to UEFI world.
116 void efi_restore_gd(void)
119 /* Only restore if we're already in EFI context */
127 * indent_string() - returns a string for indenting with two spaces per level
128 * @level: indent level
130 * A maximum of ten indent levels is supported. Higher indent levels will be
133 * Return: A string for indenting with two spaces per level is
136 static const char *indent_string(int level)
138 const char *indent = " ";
139 const int max = strlen(indent);
141 level = min(max, level * 2);
142 return &indent[max - level];
145 const char *__efi_nesting(void)
147 return indent_string(nesting_level);
150 const char *__efi_nesting_inc(void)
152 return indent_string(nesting_level++);
155 const char *__efi_nesting_dec(void)
157 return indent_string(--nesting_level);
161 * efi_queue_event() - queue an EFI event
162 * @event: event to signal
163 * @check_tpl: check the TPL level
165 * This function queues the notification function of the event for future
168 * The notification function is called if the task priority level of the event
169 * is higher than the current task priority level.
171 * For the SignalEvent service see efi_signal_event_ext.
174 static void efi_queue_event(struct efi_event *event, bool check_tpl)
176 if (event->notify_function) {
177 event->is_queued = true;
179 if (check_tpl && efi_tpl >= event->notify_tpl)
181 EFI_CALL_VOID(event->notify_function(event,
182 event->notify_context));
184 event->is_queued = false;
188 * is_valid_tpl() - check if the task priority level is valid
190 * @tpl: TPL level to check
191 * Return: status code
193 efi_status_t is_valid_tpl(efi_uintn_t tpl)
196 case TPL_APPLICATION:
202 return EFI_INVALID_PARAMETER;
207 * efi_signal_event() - signal an EFI event
208 * @event: event to signal
209 * @check_tpl: check the TPL level
211 * This function signals an event. If the event belongs to an event group all
212 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
213 * their notification function is queued.
215 * For the SignalEvent service see efi_signal_event_ext.
217 void efi_signal_event(struct efi_event *event, bool check_tpl)
220 struct efi_event *evt;
223 * The signaled state has to set before executing any
224 * notification function
226 list_for_each_entry(evt, &efi_events, link) {
227 if (!evt->group || guidcmp(evt->group, event->group))
229 if (evt->is_signaled)
231 evt->is_signaled = true;
232 if (evt->type & EVT_NOTIFY_SIGNAL &&
233 evt->notify_function)
234 evt->is_queued = true;
236 list_for_each_entry(evt, &efi_events, link) {
237 if (!evt->group || guidcmp(evt->group, event->group))
240 efi_queue_event(evt, check_tpl);
243 event->is_signaled = true;
244 if (event->type & EVT_NOTIFY_SIGNAL)
245 efi_queue_event(event, check_tpl);
250 * efi_raise_tpl() - raise the task priority level
251 * @new_tpl: new value of the task priority level
253 * This function implements the RaiseTpl service.
255 * See the Unified Extensible Firmware Interface (UEFI) specification for
258 * Return: old value of the task priority level
260 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
262 efi_uintn_t old_tpl = efi_tpl;
264 EFI_ENTRY("0x%zx", new_tpl);
266 if (new_tpl < efi_tpl)
267 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
269 if (efi_tpl > TPL_HIGH_LEVEL)
270 efi_tpl = TPL_HIGH_LEVEL;
272 EFI_EXIT(EFI_SUCCESS);
277 * efi_restore_tpl() - lower the task priority level
278 * @old_tpl: value of the task priority level to be restored
280 * This function implements the RestoreTpl service.
282 * See the Unified Extensible Firmware Interface (UEFI) specification for
285 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
287 EFI_ENTRY("0x%zx", old_tpl);
289 if (old_tpl > efi_tpl)
290 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
292 if (efi_tpl > TPL_HIGH_LEVEL)
293 efi_tpl = TPL_HIGH_LEVEL;
296 * Lowering the TPL may have made queued events eligible for execution.
300 EFI_EXIT(EFI_SUCCESS);
304 * efi_allocate_pages_ext() - allocate memory pages
305 * @type: type of allocation to be performed
306 * @memory_type: usage type of the allocated memory
307 * @pages: number of pages to be allocated
308 * @memory: allocated memory
310 * This function implements the AllocatePages service.
312 * See the Unified Extensible Firmware Interface (UEFI) specification for
315 * Return: status code
317 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
323 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
324 r = efi_allocate_pages(type, memory_type, pages, memory);
329 * efi_free_pages_ext() - Free memory pages.
330 * @memory: start of the memory area to be freed
331 * @pages: number of pages to be freed
333 * This function implements the FreePages service.
335 * See the Unified Extensible Firmware Interface (UEFI) specification for
338 * Return: status code
340 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
345 EFI_ENTRY("%llx, 0x%zx", memory, pages);
346 r = efi_free_pages(memory, pages);
351 * efi_get_memory_map_ext() - get map describing memory usage
352 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
353 * on exit the size of the copied memory map
354 * @memory_map: buffer to which the memory map is written
355 * @map_key: key for the memory map
356 * @descriptor_size: size of an individual memory descriptor
357 * @descriptor_version: version number of the memory descriptor structure
359 * This function implements the GetMemoryMap service.
361 * See the Unified Extensible Firmware Interface (UEFI) specification for
364 * Return: status code
366 static efi_status_t EFIAPI efi_get_memory_map_ext(
367 efi_uintn_t *memory_map_size,
368 struct efi_mem_desc *memory_map,
369 efi_uintn_t *map_key,
370 efi_uintn_t *descriptor_size,
371 uint32_t *descriptor_version)
375 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
376 map_key, descriptor_size, descriptor_version);
377 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
378 descriptor_size, descriptor_version);
383 * efi_allocate_pool_ext() - allocate memory from pool
384 * @pool_type: type of the pool from which memory is to be allocated
385 * @size: number of bytes to be allocated
386 * @buffer: allocated memory
388 * This function implements the AllocatePool service.
390 * See the Unified Extensible Firmware Interface (UEFI) specification for
393 * Return: status code
395 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
401 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
402 r = efi_allocate_pool(pool_type, size, buffer);
407 * efi_free_pool_ext() - free memory from pool
408 * @buffer: start of memory to be freed
410 * This function implements the FreePool service.
412 * See the Unified Extensible Firmware Interface (UEFI) specification for
415 * Return: status code
417 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
421 EFI_ENTRY("%p", buffer);
422 r = efi_free_pool(buffer);
427 * efi_add_handle() - add a new handle to the object list
429 * @handle: handle to be added
431 * The protocols list is initialized. The handle is added to the list of known
434 void efi_add_handle(efi_handle_t handle)
438 INIT_LIST_HEAD(&handle->protocols);
439 list_add_tail(&handle->link, &efi_obj_list);
443 * efi_create_handle() - create handle
444 * @handle: new handle
446 * Return: status code
448 efi_status_t efi_create_handle(efi_handle_t *handle)
450 struct efi_object *obj;
452 obj = calloc(1, sizeof(struct efi_object));
454 return EFI_OUT_OF_RESOURCES;
463 * efi_search_protocol() - find a protocol on a handle.
465 * @protocol_guid: GUID of the protocol
466 * @handler: reference to the protocol
468 * Return: status code
470 efi_status_t efi_search_protocol(const efi_handle_t handle,
471 const efi_guid_t *protocol_guid,
472 struct efi_handler **handler)
474 struct efi_object *efiobj;
475 struct list_head *lhandle;
477 if (!handle || !protocol_guid)
478 return EFI_INVALID_PARAMETER;
479 efiobj = efi_search_obj(handle);
481 return EFI_INVALID_PARAMETER;
482 list_for_each(lhandle, &efiobj->protocols) {
483 struct efi_handler *protocol;
485 protocol = list_entry(lhandle, struct efi_handler, link);
486 if (!guidcmp(protocol->guid, protocol_guid)) {
492 return EFI_NOT_FOUND;
496 * efi_remove_protocol() - delete protocol from a handle
497 * @handle: handle from which the protocol shall be deleted
498 * @protocol: GUID of the protocol to be deleted
499 * @protocol_interface: interface of the protocol implementation
501 * Return: status code
503 efi_status_t efi_remove_protocol(const efi_handle_t handle,
504 const efi_guid_t *protocol,
505 void *protocol_interface)
507 struct efi_handler *handler;
510 ret = efi_search_protocol(handle, protocol, &handler);
511 if (ret != EFI_SUCCESS)
513 if (guidcmp(handler->guid, protocol))
514 return EFI_INVALID_PARAMETER;
515 if (handler->protocol_interface != protocol_interface)
516 return EFI_INVALID_PARAMETER;
517 list_del(&handler->link);
523 * efi_remove_all_protocols() - delete all protocols from a handle
524 * @handle: handle from which the protocols shall be deleted
526 * Return: status code
528 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
530 struct efi_object *efiobj;
531 struct efi_handler *protocol;
532 struct efi_handler *pos;
534 efiobj = efi_search_obj(handle);
536 return EFI_INVALID_PARAMETER;
537 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
540 ret = efi_remove_protocol(handle, protocol->guid,
541 protocol->protocol_interface);
542 if (ret != EFI_SUCCESS)
549 * efi_delete_handle() - delete handle
551 * @obj: handle to delete
553 void efi_delete_handle(efi_handle_t handle)
557 efi_remove_all_protocols(handle);
558 list_del(&handle->link);
563 * efi_is_event() - check if a pointer is a valid event
564 * @event: pointer to check
566 * Return: status code
568 static efi_status_t efi_is_event(const struct efi_event *event)
570 const struct efi_event *evt;
573 return EFI_INVALID_PARAMETER;
574 list_for_each_entry(evt, &efi_events, link) {
578 return EFI_INVALID_PARAMETER;
582 * efi_create_event() - create an event
583 * @type: type of the event to create
584 * @notify_tpl: task priority level of the event
585 * @notify_function: notification function of the event
586 * @notify_context: pointer passed to the notification function
587 * @group: event group
588 * @event: created event
590 * This function is used inside U-Boot code to create an event.
592 * For the API function implementing the CreateEvent service see
593 * efi_create_event_ext.
595 * Return: status code
597 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
598 void (EFIAPI *notify_function) (
599 struct efi_event *event,
601 void *notify_context, efi_guid_t *group,
602 struct efi_event **event)
604 struct efi_event *evt;
607 return EFI_INVALID_PARAMETER;
612 case EVT_NOTIFY_SIGNAL:
613 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
614 case EVT_NOTIFY_WAIT:
615 case EVT_TIMER | EVT_NOTIFY_WAIT:
616 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
617 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
620 return EFI_INVALID_PARAMETER;
623 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
624 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
625 return EFI_INVALID_PARAMETER;
627 evt = calloc(1, sizeof(struct efi_event));
629 return EFI_OUT_OF_RESOURCES;
631 evt->notify_tpl = notify_tpl;
632 evt->notify_function = notify_function;
633 evt->notify_context = notify_context;
635 /* Disable timers on boot up */
636 evt->trigger_next = -1ULL;
637 evt->is_queued = false;
638 evt->is_signaled = false;
639 list_add_tail(&evt->link, &efi_events);
645 * efi_create_event_ex() - create an event in a group
646 * @type: type of the event to create
647 * @notify_tpl: task priority level of the event
648 * @notify_function: notification function of the event
649 * @notify_context: pointer passed to the notification function
650 * @event: created event
651 * @event_group: event group
653 * This function implements the CreateEventEx service.
655 * See the Unified Extensible Firmware Interface (UEFI) specification for
658 * Return: status code
660 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
661 void (EFIAPI *notify_function) (
662 struct efi_event *event,
664 void *notify_context,
665 efi_guid_t *event_group,
666 struct efi_event **event)
670 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
671 notify_context, event_group);
674 * The allowable input parameters are the same as in CreateEvent()
675 * except for the following two disallowed event types.
678 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
679 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
680 ret = EFI_INVALID_PARAMETER;
684 ret = efi_create_event(type, notify_tpl, notify_function,
685 notify_context, event_group, event);
687 return EFI_EXIT(ret);
691 * efi_create_event_ext() - create an event
692 * @type: type of the event to create
693 * @notify_tpl: task priority level of the event
694 * @notify_function: notification function of the event
695 * @notify_context: pointer passed to the notification function
696 * @event: created event
698 * This function implements the CreateEvent service.
700 * See the Unified Extensible Firmware Interface (UEFI) specification for
703 * Return: status code
705 static efi_status_t EFIAPI efi_create_event_ext(
706 uint32_t type, efi_uintn_t notify_tpl,
707 void (EFIAPI *notify_function) (
708 struct efi_event *event,
710 void *notify_context, struct efi_event **event)
712 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
714 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
715 notify_context, NULL, event));
719 * efi_timer_check() - check if a timer event has occurred
721 * Check if a timer event has occurred or a queued notification function should
724 * Our timers have to work without interrupts, so we check whenever keyboard
725 * input or disk accesses happen if enough time elapsed for them to fire.
727 void efi_timer_check(void)
729 struct efi_event *evt;
730 u64 now = timer_get_us();
732 list_for_each_entry(evt, &efi_events, link) {
734 efi_queue_event(evt, true);
735 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
737 switch (evt->trigger_type) {
738 case EFI_TIMER_RELATIVE:
739 evt->trigger_type = EFI_TIMER_STOP;
741 case EFI_TIMER_PERIODIC:
742 evt->trigger_next += evt->trigger_time;
747 evt->is_signaled = false;
748 efi_signal_event(evt, true);
754 * efi_set_timer() - set the trigger time for a timer event or stop the event
755 * @event: event for which the timer is set
756 * @type: type of the timer
757 * @trigger_time: trigger period in multiples of 100 ns
759 * This is the function for internal usage in U-Boot. For the API function
760 * implementing the SetTimer service see efi_set_timer_ext.
762 * Return: status code
764 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
765 uint64_t trigger_time)
767 /* Check that the event is valid */
768 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
769 return EFI_INVALID_PARAMETER;
772 * The parameter defines a multiple of 100 ns.
773 * We use multiples of 1000 ns. So divide by 10.
775 do_div(trigger_time, 10);
779 event->trigger_next = -1ULL;
781 case EFI_TIMER_PERIODIC:
782 case EFI_TIMER_RELATIVE:
783 event->trigger_next = timer_get_us() + trigger_time;
786 return EFI_INVALID_PARAMETER;
788 event->trigger_type = type;
789 event->trigger_time = trigger_time;
790 event->is_signaled = false;
795 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
797 * @event: event for which the timer is set
798 * @type: type of the timer
799 * @trigger_time: trigger period in multiples of 100 ns
801 * This function implements the SetTimer service.
803 * See the Unified Extensible Firmware Interface (UEFI) specification for
807 * Return: status code
809 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
810 enum efi_timer_delay type,
811 uint64_t trigger_time)
813 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
814 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
818 * efi_wait_for_event() - wait for events to be signaled
819 * @num_events: number of events to be waited for
820 * @event: events to be waited for
821 * @index: index of the event that was signaled
823 * This function implements the WaitForEvent service.
825 * See the Unified Extensible Firmware Interface (UEFI) specification for
828 * Return: status code
830 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
831 struct efi_event **event,
836 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
838 /* Check parameters */
839 if (!num_events || !event)
840 return EFI_EXIT(EFI_INVALID_PARAMETER);
842 if (efi_tpl != TPL_APPLICATION)
843 return EFI_EXIT(EFI_UNSUPPORTED);
844 for (i = 0; i < num_events; ++i) {
845 if (efi_is_event(event[i]) != EFI_SUCCESS)
846 return EFI_EXIT(EFI_INVALID_PARAMETER);
847 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
848 return EFI_EXIT(EFI_INVALID_PARAMETER);
849 if (!event[i]->is_signaled)
850 efi_queue_event(event[i], true);
853 /* Wait for signal */
855 for (i = 0; i < num_events; ++i) {
856 if (event[i]->is_signaled)
859 /* Allow events to occur. */
865 * Reset the signal which is passed to the caller to allow periodic
868 event[i]->is_signaled = false;
872 return EFI_EXIT(EFI_SUCCESS);
876 * efi_signal_event_ext() - signal an EFI event
877 * @event: event to signal
879 * This function implements the SignalEvent service.
881 * See the Unified Extensible Firmware Interface (UEFI) specification for
884 * This functions sets the signaled state of the event and queues the
885 * notification function for execution.
887 * Return: status code
889 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
891 EFI_ENTRY("%p", event);
892 if (efi_is_event(event) != EFI_SUCCESS)
893 return EFI_EXIT(EFI_INVALID_PARAMETER);
894 efi_signal_event(event, true);
895 return EFI_EXIT(EFI_SUCCESS);
899 * efi_close_event() - close an EFI event
900 * @event: event to close
902 * This function implements the CloseEvent service.
904 * See the Unified Extensible Firmware Interface (UEFI) specification for
907 * Return: status code
909 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
911 EFI_ENTRY("%p", event);
912 if (efi_is_event(event) != EFI_SUCCESS)
913 return EFI_EXIT(EFI_INVALID_PARAMETER);
914 list_del(&event->link);
916 return EFI_EXIT(EFI_SUCCESS);
920 * efi_check_event() - check if an event is signaled
921 * @event: event to check
923 * This function implements the CheckEvent service.
925 * See the Unified Extensible Firmware Interface (UEFI) specification for
928 * If an event is not signaled yet, the notification function is queued. The
929 * signaled state is cleared.
931 * Return: status code
933 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
935 EFI_ENTRY("%p", event);
937 if (efi_is_event(event) != EFI_SUCCESS ||
938 event->type & EVT_NOTIFY_SIGNAL)
939 return EFI_EXIT(EFI_INVALID_PARAMETER);
940 if (!event->is_signaled)
941 efi_queue_event(event, true);
942 if (event->is_signaled) {
943 event->is_signaled = false;
944 return EFI_EXIT(EFI_SUCCESS);
946 return EFI_EXIT(EFI_NOT_READY);
950 * efi_search_obj() - find the internal EFI object for a handle
951 * @handle: handle to find
955 struct efi_object *efi_search_obj(const efi_handle_t handle)
957 struct efi_object *efiobj;
959 list_for_each_entry(efiobj, &efi_obj_list, link) {
960 if (efiobj == handle)
968 * efi_open_protocol_info_entry() - create open protocol info entry and add it
970 * @handler: handler of a protocol
972 * Return: open protocol info entry
974 static struct efi_open_protocol_info_entry *efi_create_open_info(
975 struct efi_handler *handler)
977 struct efi_open_protocol_info_item *item;
979 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
982 /* Append the item to the open protocol info list. */
983 list_add_tail(&item->link, &handler->open_infos);
989 * efi_delete_open_info() - remove an open protocol info entry from a protocol
990 * @item: open protocol info entry to delete
992 * Return: status code
994 static efi_status_t efi_delete_open_info(
995 struct efi_open_protocol_info_item *item)
997 list_del(&item->link);
1003 * efi_add_protocol() - install new protocol on a handle
1004 * @handle: handle on which the protocol shall be installed
1005 * @protocol: GUID of the protocol to be installed
1006 * @protocol_interface: interface of the protocol implementation
1008 * Return: status code
1010 efi_status_t efi_add_protocol(const efi_handle_t handle,
1011 const efi_guid_t *protocol,
1012 void *protocol_interface)
1014 struct efi_object *efiobj;
1015 struct efi_handler *handler;
1018 efiobj = efi_search_obj(handle);
1020 return EFI_INVALID_PARAMETER;
1021 ret = efi_search_protocol(handle, protocol, NULL);
1022 if (ret != EFI_NOT_FOUND)
1023 return EFI_INVALID_PARAMETER;
1024 handler = calloc(1, sizeof(struct efi_handler));
1026 return EFI_OUT_OF_RESOURCES;
1027 handler->guid = protocol;
1028 handler->protocol_interface = protocol_interface;
1029 INIT_LIST_HEAD(&handler->open_infos);
1030 list_add_tail(&handler->link, &efiobj->protocols);
1031 if (!guidcmp(&efi_guid_device_path, protocol))
1032 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1037 * efi_install_protocol_interface() - install protocol interface
1038 * @handle: handle on which the protocol shall be installed
1039 * @protocol: GUID of the protocol to be installed
1040 * @protocol_interface_type: type of the interface to be installed,
1041 * always EFI_NATIVE_INTERFACE
1042 * @protocol_interface: interface of the protocol implementation
1044 * This function implements the InstallProtocolInterface service.
1046 * See the Unified Extensible Firmware Interface (UEFI) specification for
1049 * Return: status code
1051 static efi_status_t EFIAPI efi_install_protocol_interface(
1052 efi_handle_t *handle, const efi_guid_t *protocol,
1053 int protocol_interface_type, void *protocol_interface)
1057 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1058 protocol_interface);
1060 if (!handle || !protocol ||
1061 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1062 r = EFI_INVALID_PARAMETER;
1066 /* Create new handle if requested. */
1068 r = efi_create_handle(handle);
1069 if (r != EFI_SUCCESS)
1071 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1074 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1077 /* Add new protocol */
1078 r = efi_add_protocol(*handle, protocol, protocol_interface);
1084 * efi_get_drivers() - get all drivers associated to a controller
1085 * @handle: handle of the controller
1086 * @protocol: protocol GUID (optional)
1087 * @number_of_drivers: number of child controllers
1088 * @driver_handle_buffer: handles of the the drivers
1090 * The allocated buffer has to be freed with free().
1092 * Return: status code
1094 static efi_status_t efi_get_drivers(efi_handle_t handle,
1095 const efi_guid_t *protocol,
1096 efi_uintn_t *number_of_drivers,
1097 efi_handle_t **driver_handle_buffer)
1099 struct efi_handler *handler;
1100 struct efi_open_protocol_info_item *item;
1101 efi_uintn_t count = 0, i;
1104 /* Count all driver associations */
1105 list_for_each_entry(handler, &handle->protocols, link) {
1106 if (protocol && guidcmp(handler->guid, protocol))
1108 list_for_each_entry(item, &handler->open_infos, link) {
1109 if (item->info.attributes &
1110 EFI_OPEN_PROTOCOL_BY_DRIVER)
1115 * Create buffer. In case of duplicate driver assignments the buffer
1116 * will be too large. But that does not harm.
1118 *number_of_drivers = 0;
1119 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1120 if (!*driver_handle_buffer)
1121 return EFI_OUT_OF_RESOURCES;
1122 /* Collect unique driver handles */
1123 list_for_each_entry(handler, &handle->protocols, link) {
1124 if (protocol && guidcmp(handler->guid, protocol))
1126 list_for_each_entry(item, &handler->open_infos, link) {
1127 if (item->info.attributes &
1128 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1129 /* Check this is a new driver */
1131 for (i = 0; i < *number_of_drivers; ++i) {
1132 if ((*driver_handle_buffer)[i] ==
1133 item->info.agent_handle)
1136 /* Copy handle to buffer */
1138 i = (*number_of_drivers)++;
1139 (*driver_handle_buffer)[i] =
1140 item->info.agent_handle;
1149 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1150 * @handle: handle of the controller
1151 * @protocol: protocol GUID (optional)
1152 * @child_handle: handle of the child to destroy
1154 * This function implements the DisconnectController service.
1156 * See the Unified Extensible Firmware Interface (UEFI) specification for
1159 * Return: status code
1161 static efi_status_t efi_disconnect_all_drivers
1162 (efi_handle_t handle,
1163 const efi_guid_t *protocol,
1164 efi_handle_t child_handle)
1166 efi_uintn_t number_of_drivers;
1167 efi_handle_t *driver_handle_buffer;
1168 efi_status_t r, ret;
1170 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
1171 &driver_handle_buffer);
1172 if (ret != EFI_SUCCESS)
1175 ret = EFI_NOT_FOUND;
1176 while (number_of_drivers) {
1177 r = EFI_CALL(efi_disconnect_controller(
1179 driver_handle_buffer[--number_of_drivers],
1181 if (r == EFI_SUCCESS)
1184 free(driver_handle_buffer);
1189 * efi_uninstall_protocol() - uninstall protocol interface
1191 * @handle: handle from which the protocol shall be removed
1192 * @protocol: GUID of the protocol to be removed
1193 * @protocol_interface: interface to be removed
1195 * This function DOES NOT delete a handle without installed protocol.
1197 * Return: status code
1199 static efi_status_t efi_uninstall_protocol
1200 (efi_handle_t handle, const efi_guid_t *protocol,
1201 void *protocol_interface)
1203 struct efi_object *efiobj;
1204 struct efi_handler *handler;
1205 struct efi_open_protocol_info_item *item;
1206 struct efi_open_protocol_info_item *pos;
1210 efiobj = efi_search_obj(handle);
1212 r = EFI_INVALID_PARAMETER;
1215 /* Find the protocol on the handle */
1216 r = efi_search_protocol(handle, protocol, &handler);
1217 if (r != EFI_SUCCESS)
1219 /* Disconnect controllers */
1220 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1221 if (!list_empty(&handler->open_infos)) {
1222 r = EFI_ACCESS_DENIED;
1225 /* Close protocol */
1226 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1227 if (item->info.attributes ==
1228 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1229 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1230 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1231 list_del(&item->link);
1233 if (!list_empty(&handler->open_infos)) {
1234 r = EFI_ACCESS_DENIED;
1237 r = efi_remove_protocol(handle, protocol, protocol_interface);
1243 * efi_uninstall_protocol_interface() - uninstall protocol interface
1244 * @handle: handle from which the protocol shall be removed
1245 * @protocol: GUID of the protocol to be removed
1246 * @protocol_interface: interface to be removed
1248 * This function implements the UninstallProtocolInterface service.
1250 * See the Unified Extensible Firmware Interface (UEFI) specification for
1253 * Return: status code
1255 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1256 (efi_handle_t handle, const efi_guid_t *protocol,
1257 void *protocol_interface)
1261 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1263 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1264 if (ret != EFI_SUCCESS)
1267 /* If the last protocol has been removed, delete the handle. */
1268 if (list_empty(&handle->protocols)) {
1269 list_del(&handle->link);
1273 return EFI_EXIT(ret);
1277 * efi_register_protocol_notify() - register an event for notification when a
1278 * protocol is installed.
1279 * @protocol: GUID of the protocol whose installation shall be notified
1280 * @event: event to be signaled upon installation of the protocol
1281 * @registration: key for retrieving the registration information
1283 * This function implements the RegisterProtocolNotify service.
1284 * See the Unified Extensible Firmware Interface (UEFI) specification
1287 * Return: status code
1289 static efi_status_t EFIAPI efi_register_protocol_notify(
1290 const efi_guid_t *protocol,
1291 struct efi_event *event,
1292 void **registration)
1294 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1295 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1299 * efi_search() - determine if an EFI handle implements a protocol
1300 * @search_type: selection criterion
1301 * @protocol: GUID of the protocol
1302 * @search_key: registration key
1305 * See the documentation of the LocateHandle service in the UEFI specification.
1307 * Return: 0 if the handle implements the protocol
1309 static int efi_search(enum efi_locate_search_type search_type,
1310 const efi_guid_t *protocol, void *search_key,
1311 efi_handle_t handle)
1315 switch (search_type) {
1318 case BY_REGISTER_NOTIFY:
1319 /* TODO: RegisterProtocolNotify is not implemented yet */
1322 ret = efi_search_protocol(handle, protocol, NULL);
1323 return (ret != EFI_SUCCESS);
1325 /* Invalid search type */
1331 * efi_locate_handle() - locate handles implementing a protocol
1332 * @search_type: selection criterion
1333 * @protocol: GUID of the protocol
1334 * @search_key: registration key
1335 * @buffer_size: size of the buffer to receive the handles in bytes
1336 * @buffer: buffer to receive the relevant handles
1338 * This function is meant for U-Boot internal calls. For the API implementation
1339 * of the LocateHandle service see efi_locate_handle_ext.
1341 * Return: status code
1343 static efi_status_t efi_locate_handle(
1344 enum efi_locate_search_type search_type,
1345 const efi_guid_t *protocol, void *search_key,
1346 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1348 struct efi_object *efiobj;
1349 efi_uintn_t size = 0;
1351 /* Check parameters */
1352 switch (search_type) {
1355 case BY_REGISTER_NOTIFY:
1357 return EFI_INVALID_PARAMETER;
1358 /* RegisterProtocolNotify is not implemented yet */
1359 return EFI_UNSUPPORTED;
1362 return EFI_INVALID_PARAMETER;
1365 return EFI_INVALID_PARAMETER;
1368 /* Count how much space we need */
1369 list_for_each_entry(efiobj, &efi_obj_list, link) {
1370 if (!efi_search(search_type, protocol, search_key, efiobj))
1371 size += sizeof(void *);
1375 return EFI_NOT_FOUND;
1378 return EFI_INVALID_PARAMETER;
1380 if (*buffer_size < size) {
1381 *buffer_size = size;
1382 return EFI_BUFFER_TOO_SMALL;
1385 *buffer_size = size;
1387 /* The buffer size is sufficient but there is not buffer */
1389 return EFI_INVALID_PARAMETER;
1391 /* Then fill the array */
1392 list_for_each_entry(efiobj, &efi_obj_list, link) {
1393 if (!efi_search(search_type, protocol, search_key, efiobj))
1401 * efi_locate_handle_ext() - locate handles implementing a protocol.
1402 * @search_type: selection criterion
1403 * @protocol: GUID of the protocol
1404 * @search_key: registration key
1405 * @buffer_size: size of the buffer to receive the handles in bytes
1406 * @buffer: buffer to receive the relevant handles
1408 * This function implements the LocateHandle service.
1410 * See the Unified Extensible Firmware Interface (UEFI) specification for
1413 * Return: 0 if the handle implements the protocol
1415 static efi_status_t EFIAPI efi_locate_handle_ext(
1416 enum efi_locate_search_type search_type,
1417 const efi_guid_t *protocol, void *search_key,
1418 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1420 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1421 buffer_size, buffer);
1423 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1424 buffer_size, buffer));
1428 * efi_remove_configuration_table() - collapses configuration table entries,
1431 * @i: index of the table entry to be removed
1433 static void efi_remove_configuration_table(int i)
1435 struct efi_configuration_table *this = &systab.tables[i];
1436 struct efi_configuration_table *next = &systab.tables[i + 1];
1437 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1439 memmove(this, next, (ulong)end - (ulong)next);
1444 * efi_install_configuration_table() - adds, updates, or removes a
1445 * configuration table
1446 * @guid: GUID of the installed table
1447 * @table: table to be installed
1449 * This function is used for internal calls. For the API implementation of the
1450 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1452 * Return: status code
1454 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1457 struct efi_event *evt;
1461 return EFI_INVALID_PARAMETER;
1463 /* Check for GUID override */
1464 for (i = 0; i < systab.nr_tables; i++) {
1465 if (!guidcmp(guid, &systab.tables[i].guid)) {
1467 systab.tables[i].table = table;
1469 efi_remove_configuration_table(i);
1475 return EFI_NOT_FOUND;
1477 /* No override, check for overflow */
1478 if (i >= EFI_MAX_CONFIGURATION_TABLES)
1479 return EFI_OUT_OF_RESOURCES;
1481 /* Add a new entry */
1482 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1483 systab.tables[i].table = table;
1484 systab.nr_tables = i + 1;
1487 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1488 efi_update_table_header_crc32(&systab.hdr);
1490 /* Notify that the configuration table was changed */
1491 list_for_each_entry(evt, &efi_events, link) {
1492 if (evt->group && !guidcmp(evt->group, guid)) {
1493 efi_signal_event(evt, false);
1502 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1503 * configuration table.
1504 * @guid: GUID of the installed table
1505 * @table: table to be installed
1507 * This function implements the InstallConfigurationTable service.
1509 * See the Unified Extensible Firmware Interface (UEFI) specification for
1512 * Return: status code
1514 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1517 EFI_ENTRY("%pUl, %p", guid, table);
1518 return EFI_EXIT(efi_install_configuration_table(guid, table));
1522 * efi_setup_loaded_image() - initialize a loaded image
1524 * Initialize a loaded_image_info and loaded_image_info object with correct
1525 * protocols, boot-device, etc.
1527 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1530 * @device_path: device path of the loaded image
1531 * @file_path: file path of the loaded image
1532 * @handle_ptr: handle of the loaded image
1533 * @info_ptr: loaded image protocol
1534 * Return: status code
1536 efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1537 struct efi_device_path *file_path,
1538 struct efi_loaded_image_obj **handle_ptr,
1539 struct efi_loaded_image **info_ptr)
1542 struct efi_loaded_image *info = NULL;
1543 struct efi_loaded_image_obj *obj = NULL;
1544 struct efi_device_path *dp;
1546 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1550 info = calloc(1, sizeof(*info));
1552 return EFI_OUT_OF_RESOURCES;
1553 obj = calloc(1, sizeof(*obj));
1556 return EFI_OUT_OF_RESOURCES;
1558 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
1560 /* Add internal object to object list */
1561 efi_add_handle(&obj->header);
1563 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1564 info->file_path = file_path;
1565 info->system_table = &systab;
1568 info->device_handle = efi_dp_find_obj(device_path, NULL);
1570 dp = efi_dp_append(device_path, file_path);
1572 ret = EFI_OUT_OF_RESOURCES;
1578 ret = efi_add_protocol(&obj->header,
1579 &efi_guid_loaded_image_device_path, dp);
1580 if (ret != EFI_SUCCESS)
1584 * When asking for the loaded_image interface, just
1585 * return handle which points to loaded_image_info
1587 ret = efi_add_protocol(&obj->header,
1588 &efi_guid_loaded_image, info);
1589 if (ret != EFI_SUCCESS)
1597 printf("ERROR: Failure to install protocols for loaded image\n");
1598 efi_delete_handle(&obj->header);
1604 * efi_load_image_from_path() - load an image using a file path
1606 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1607 * callers obligation to update the memory type as needed.
1609 * @file_path: the path of the image to load
1610 * @buffer: buffer containing the loaded image
1611 * @size: size of the loaded image
1612 * Return: status code
1615 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1616 void **buffer, efi_uintn_t *size)
1618 struct efi_file_info *info = NULL;
1619 struct efi_file_handle *f;
1620 static efi_status_t ret;
1624 /* In case of failure nothing is returned */
1629 f = efi_file_from_path(file_path);
1631 return EFI_DEVICE_ERROR;
1635 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1637 if (ret != EFI_BUFFER_TOO_SMALL) {
1638 ret = EFI_DEVICE_ERROR;
1643 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1645 if (ret != EFI_SUCCESS)
1649 * When reading the file we do not yet know if it contains an
1650 * application, a boottime driver, or a runtime driver. So here we
1651 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1652 * update the reservation according to the image type.
1654 bs = info->file_size;
1655 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1656 EFI_BOOT_SERVICES_DATA,
1657 efi_size_in_pages(bs), &addr);
1658 if (ret != EFI_SUCCESS) {
1659 ret = EFI_OUT_OF_RESOURCES;
1664 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1665 if (ret != EFI_SUCCESS)
1666 efi_free_pages(addr, efi_size_in_pages(bs));
1667 *buffer = (void *)(uintptr_t)addr;
1670 EFI_CALL(f->close(f));
1676 * efi_load_image() - load an EFI image into memory
1677 * @boot_policy: true for request originating from the boot manager
1678 * @parent_image: the caller's image handle
1679 * @file_path: the path of the image to load
1680 * @source_buffer: memory location from which the image is installed
1681 * @source_size: size of the memory area from which the image is installed
1682 * @image_handle: handle for the newly installed image
1684 * This function implements the LoadImage service.
1686 * See the Unified Extensible Firmware Interface (UEFI) specification
1689 * Return: status code
1691 efi_status_t EFIAPI efi_load_image(bool boot_policy,
1692 efi_handle_t parent_image,
1693 struct efi_device_path *file_path,
1694 void *source_buffer,
1695 efi_uintn_t source_size,
1696 efi_handle_t *image_handle)
1698 struct efi_device_path *dp, *fp;
1699 struct efi_loaded_image *info = NULL;
1700 struct efi_loaded_image_obj **image_obj =
1701 (struct efi_loaded_image_obj **)image_handle;
1705 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1706 file_path, source_buffer, source_size, image_handle);
1708 if (!image_handle || !parent_image) {
1709 ret = EFI_INVALID_PARAMETER;
1713 if (!source_buffer && !file_path) {
1714 ret = EFI_NOT_FOUND;
1718 if (!source_buffer) {
1719 ret = efi_load_image_from_path(file_path, &dest_buffer,
1721 if (ret != EFI_SUCCESS)
1724 dest_buffer = source_buffer;
1726 /* split file_path which contains both the device and file parts */
1727 efi_dp_split_file_path(file_path, &dp, &fp);
1728 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1729 if (ret == EFI_SUCCESS)
1730 ret = efi_load_pe(*image_obj, dest_buffer, info);
1732 /* Release buffer to which file was loaded */
1733 efi_free_pages((uintptr_t)dest_buffer,
1734 efi_size_in_pages(source_size));
1735 if (ret == EFI_SUCCESS) {
1736 info->system_table = &systab;
1737 info->parent_handle = parent_image;
1739 /* The image is invalid. Release all associated resources. */
1740 efi_delete_handle(*image_handle);
1741 *image_handle = NULL;
1745 return EFI_EXIT(ret);
1749 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1751 static void efi_exit_caches(void)
1753 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1755 * Grub on 32bit ARM needs to have caches disabled before jumping into
1756 * a zImage, but does not know of all cache layers. Give it a hand.
1758 if (efi_is_direct_boot)
1759 cleanup_before_linux();
1764 * efi_exit_boot_services() - stop all boot services
1765 * @image_handle: handle of the loaded image
1766 * @map_key: key of the memory map
1768 * This function implements the ExitBootServices service.
1770 * See the Unified Extensible Firmware Interface (UEFI) specification
1773 * All timer events are disabled. For exit boot services events the
1774 * notification function is called. The boot services are disabled in the
1777 * Return: status code
1779 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1780 unsigned long map_key)
1782 struct efi_event *evt;
1784 EFI_ENTRY("%p, %ld", image_handle, map_key);
1786 /* Check that the caller has read the current memory map */
1787 if (map_key != efi_memory_map_key)
1788 return EFI_INVALID_PARAMETER;
1790 /* Make sure that notification functions are not called anymore */
1791 efi_tpl = TPL_HIGH_LEVEL;
1793 /* Check if ExitBootServices has already been called */
1794 if (!systab.boottime)
1795 return EFI_EXIT(EFI_SUCCESS);
1797 /* Add related events to the event group */
1798 list_for_each_entry(evt, &efi_events, link) {
1799 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1800 evt->group = &efi_guid_event_group_exit_boot_services;
1802 /* Notify that ExitBootServices is invoked. */
1803 list_for_each_entry(evt, &efi_events, link) {
1805 !guidcmp(evt->group,
1806 &efi_guid_event_group_exit_boot_services)) {
1807 efi_signal_event(evt, false);
1812 /* TODO: Should persist EFI variables here */
1814 board_quiesce_devices();
1816 /* Fix up caches for EFI payloads if necessary */
1819 /* This stops all lingering devices */
1820 bootm_disable_interrupts();
1822 /* Disable boot time services */
1823 systab.con_in_handle = NULL;
1824 systab.con_in = NULL;
1825 systab.con_out_handle = NULL;
1826 systab.con_out = NULL;
1827 systab.stderr_handle = NULL;
1828 systab.std_err = NULL;
1829 systab.boottime = NULL;
1831 /* Recalculate CRC32 */
1832 efi_update_table_header_crc32(&systab.hdr);
1834 /* Give the payload some time to boot */
1835 efi_set_watchdog(0);
1838 return EFI_EXIT(EFI_SUCCESS);
1842 * efi_get_next_monotonic_count() - get next value of the counter
1843 * @count: returned value of the counter
1845 * This function implements the NextMonotonicCount service.
1847 * See the Unified Extensible Firmware Interface (UEFI) specification for
1850 * Return: status code
1852 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1854 static uint64_t mono;
1856 EFI_ENTRY("%p", count);
1858 return EFI_EXIT(EFI_SUCCESS);
1862 * efi_stall() - sleep
1863 * @microseconds: period to sleep in microseconds
1865 * This function implements the Stall service.
1867 * See the Unified Extensible Firmware Interface (UEFI) specification for
1870 * Return: status code
1872 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1874 EFI_ENTRY("%ld", microseconds);
1875 udelay(microseconds);
1876 return EFI_EXIT(EFI_SUCCESS);
1880 * efi_set_watchdog_timer() - reset the watchdog timer
1881 * @timeout: seconds before reset by watchdog
1882 * @watchdog_code: code to be logged when resetting
1883 * @data_size: size of buffer in bytes
1884 * @watchdog_data: buffer with data describing the reset reason
1886 * This function implements the SetWatchdogTimer service.
1888 * See the Unified Extensible Firmware Interface (UEFI) specification for
1891 * Return: status code
1893 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1894 uint64_t watchdog_code,
1895 unsigned long data_size,
1896 uint16_t *watchdog_data)
1898 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
1899 data_size, watchdog_data);
1900 return EFI_EXIT(efi_set_watchdog(timeout));
1904 * efi_close_protocol() - close a protocol
1905 * @handle: handle on which the protocol shall be closed
1906 * @protocol: GUID of the protocol to close
1907 * @agent_handle: handle of the driver
1908 * @controller_handle: handle of the controller
1910 * This function implements the CloseProtocol service.
1912 * See the Unified Extensible Firmware Interface (UEFI) specification for
1915 * Return: status code
1917 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1918 const efi_guid_t *protocol,
1919 efi_handle_t agent_handle,
1920 efi_handle_t controller_handle)
1922 struct efi_handler *handler;
1923 struct efi_open_protocol_info_item *item;
1924 struct efi_open_protocol_info_item *pos;
1927 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1930 if (!agent_handle) {
1931 r = EFI_INVALID_PARAMETER;
1934 r = efi_search_protocol(handle, protocol, &handler);
1935 if (r != EFI_SUCCESS)
1939 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1940 if (item->info.agent_handle == agent_handle &&
1941 item->info.controller_handle == controller_handle) {
1942 efi_delete_open_info(item);
1952 * efi_open_protocol_information() - provide information about then open status
1953 * of a protocol on a handle
1954 * @handle: handle for which the information shall be retrieved
1955 * @protocol: GUID of the protocol
1956 * @entry_buffer: buffer to receive the open protocol information
1957 * @entry_count: number of entries available in the buffer
1959 * This function implements the OpenProtocolInformation service.
1961 * See the Unified Extensible Firmware Interface (UEFI) specification for
1964 * Return: status code
1966 static efi_status_t EFIAPI efi_open_protocol_information(
1967 efi_handle_t handle, const efi_guid_t *protocol,
1968 struct efi_open_protocol_info_entry **entry_buffer,
1969 efi_uintn_t *entry_count)
1971 unsigned long buffer_size;
1972 unsigned long count;
1973 struct efi_handler *handler;
1974 struct efi_open_protocol_info_item *item;
1977 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1980 /* Check parameters */
1981 if (!entry_buffer) {
1982 r = EFI_INVALID_PARAMETER;
1985 r = efi_search_protocol(handle, protocol, &handler);
1986 if (r != EFI_SUCCESS)
1991 list_for_each_entry(item, &handler->open_infos, link) {
1992 if (item->info.open_count)
1995 *entry_count = count;
1996 *entry_buffer = NULL;
2003 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2004 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2005 (void **)entry_buffer);
2006 if (r != EFI_SUCCESS)
2008 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2009 if (item->info.open_count)
2010 (*entry_buffer)[--count] = item->info;
2017 * efi_protocols_per_handle() - get protocols installed on a handle
2018 * @handle: handle for which the information is retrieved
2019 * @protocol_buffer: buffer with protocol GUIDs
2020 * @protocol_buffer_count: number of entries in the buffer
2022 * This function implements the ProtocolsPerHandleService.
2024 * See the Unified Extensible Firmware Interface (UEFI) specification for
2027 * Return: status code
2029 static efi_status_t EFIAPI efi_protocols_per_handle(
2030 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2031 efi_uintn_t *protocol_buffer_count)
2033 unsigned long buffer_size;
2034 struct efi_object *efiobj;
2035 struct list_head *protocol_handle;
2038 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2039 protocol_buffer_count);
2041 if (!handle || !protocol_buffer || !protocol_buffer_count)
2042 return EFI_EXIT(EFI_INVALID_PARAMETER);
2044 *protocol_buffer = NULL;
2045 *protocol_buffer_count = 0;
2047 efiobj = efi_search_obj(handle);
2049 return EFI_EXIT(EFI_INVALID_PARAMETER);
2051 /* Count protocols */
2052 list_for_each(protocol_handle, &efiobj->protocols) {
2053 ++*protocol_buffer_count;
2057 if (*protocol_buffer_count) {
2060 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2061 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2062 (void **)protocol_buffer);
2063 if (r != EFI_SUCCESS)
2065 list_for_each(protocol_handle, &efiobj->protocols) {
2066 struct efi_handler *protocol;
2068 protocol = list_entry(protocol_handle,
2069 struct efi_handler, link);
2070 (*protocol_buffer)[j] = (void *)protocol->guid;
2075 return EFI_EXIT(EFI_SUCCESS);
2079 * efi_locate_handle_buffer() - locate handles implementing a protocol
2080 * @search_type: selection criterion
2081 * @protocol: GUID of the protocol
2082 * @search_key: registration key
2083 * @no_handles: number of returned handles
2084 * @buffer: buffer with the returned handles
2086 * This function implements the LocateHandleBuffer service.
2088 * See the Unified Extensible Firmware Interface (UEFI) specification for
2091 * Return: status code
2093 static efi_status_t EFIAPI efi_locate_handle_buffer(
2094 enum efi_locate_search_type search_type,
2095 const efi_guid_t *protocol, void *search_key,
2096 efi_uintn_t *no_handles, efi_handle_t **buffer)
2099 efi_uintn_t buffer_size = 0;
2101 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2102 no_handles, buffer);
2104 if (!no_handles || !buffer) {
2105 r = EFI_INVALID_PARAMETER;
2110 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2112 if (r != EFI_BUFFER_TOO_SMALL)
2114 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2116 if (r != EFI_SUCCESS)
2118 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2120 if (r == EFI_SUCCESS)
2121 *no_handles = buffer_size / sizeof(efi_handle_t);
2127 * efi_locate_protocol() - find an interface implementing a protocol
2128 * @protocol: GUID of the protocol
2129 * @registration: registration key passed to the notification function
2130 * @protocol_interface: interface implementing the protocol
2132 * This function implements the LocateProtocol service.
2134 * See the Unified Extensible Firmware Interface (UEFI) specification for
2137 * Return: status code
2139 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2141 void **protocol_interface)
2143 struct list_head *lhandle;
2146 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2148 if (!protocol || !protocol_interface)
2149 return EFI_EXIT(EFI_INVALID_PARAMETER);
2151 list_for_each(lhandle, &efi_obj_list) {
2152 struct efi_object *efiobj;
2153 struct efi_handler *handler;
2155 efiobj = list_entry(lhandle, struct efi_object, link);
2157 ret = efi_search_protocol(efiobj, protocol, &handler);
2158 if (ret == EFI_SUCCESS) {
2159 *protocol_interface = handler->protocol_interface;
2160 return EFI_EXIT(EFI_SUCCESS);
2163 *protocol_interface = NULL;
2165 return EFI_EXIT(EFI_NOT_FOUND);
2169 * efi_locate_device_path() - Get the device path and handle of an device
2170 * implementing a protocol
2171 * @protocol: GUID of the protocol
2172 * @device_path: device path
2173 * @device: handle of the device
2175 * This function implements the LocateDevicePath service.
2177 * See the Unified Extensible Firmware Interface (UEFI) specification for
2180 * Return: status code
2182 static efi_status_t EFIAPI efi_locate_device_path(
2183 const efi_guid_t *protocol,
2184 struct efi_device_path **device_path,
2185 efi_handle_t *device)
2187 struct efi_device_path *dp;
2189 struct efi_handler *handler;
2190 efi_handle_t *handles;
2192 size_t len_best = 0;
2193 efi_uintn_t no_handles;
2197 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2199 if (!protocol || !device_path || !*device_path || !device) {
2200 ret = EFI_INVALID_PARAMETER;
2204 /* Find end of device path */
2205 len = efi_dp_instance_size(*device_path);
2207 /* Get all handles implementing the protocol */
2208 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2209 &no_handles, &handles));
2210 if (ret != EFI_SUCCESS)
2213 for (i = 0; i < no_handles; ++i) {
2214 /* Find the device path protocol */
2215 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2217 if (ret != EFI_SUCCESS)
2219 dp = (struct efi_device_path *)handler->protocol_interface;
2220 len_dp = efi_dp_instance_size(dp);
2222 * This handle can only be a better fit
2223 * if its device path length is longer than the best fit and
2224 * if its device path length is shorter of equal the searched
2227 if (len_dp <= len_best || len_dp > len)
2229 /* Check if dp is a subpath of device_path */
2230 if (memcmp(*device_path, dp, len_dp))
2232 *device = handles[i];
2236 remainder = (u8 *)*device_path + len_best;
2237 *device_path = (struct efi_device_path *)remainder;
2240 ret = EFI_NOT_FOUND;
2243 return EFI_EXIT(ret);
2247 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2249 * @handle: handle on which the protocol interfaces shall be installed
2250 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2253 * This function implements the MultipleProtocolInterfaces service.
2255 * See the Unified Extensible Firmware Interface (UEFI) specification for
2258 * Return: status code
2260 efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2261 (efi_handle_t *handle, ...)
2263 EFI_ENTRY("%p", handle);
2266 const efi_guid_t *protocol;
2267 void *protocol_interface;
2268 efi_status_t r = EFI_SUCCESS;
2272 return EFI_EXIT(EFI_INVALID_PARAMETER);
2274 efi_va_start(argptr, handle);
2276 protocol = efi_va_arg(argptr, efi_guid_t*);
2279 protocol_interface = efi_va_arg(argptr, void*);
2280 r = EFI_CALL(efi_install_protocol_interface(
2282 EFI_NATIVE_INTERFACE,
2283 protocol_interface));
2284 if (r != EFI_SUCCESS)
2289 if (r == EFI_SUCCESS)
2292 /* If an error occurred undo all changes. */
2293 efi_va_start(argptr, handle);
2295 protocol = efi_va_arg(argptr, efi_guid_t*);
2296 protocol_interface = efi_va_arg(argptr, void*);
2297 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2298 protocol_interface));
2306 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2308 * @handle: handle from which the protocol interfaces shall be removed
2309 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2312 * This function implements the UninstallMultipleProtocolInterfaces service.
2314 * See the Unified Extensible Firmware Interface (UEFI) specification for
2317 * Return: status code
2319 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2320 efi_handle_t handle, ...)
2322 EFI_ENTRY("%p", handle);
2325 const efi_guid_t *protocol;
2326 void *protocol_interface;
2327 efi_status_t r = EFI_SUCCESS;
2331 return EFI_EXIT(EFI_INVALID_PARAMETER);
2333 efi_va_start(argptr, handle);
2335 protocol = efi_va_arg(argptr, efi_guid_t*);
2338 protocol_interface = efi_va_arg(argptr, void*);
2339 r = efi_uninstall_protocol(handle, protocol,
2340 protocol_interface);
2341 if (r != EFI_SUCCESS)
2346 if (r == EFI_SUCCESS) {
2347 /* If the last protocol has been removed, delete the handle. */
2348 if (list_empty(&handle->protocols)) {
2349 list_del(&handle->link);
2355 /* If an error occurred undo all changes. */
2356 efi_va_start(argptr, handle);
2358 protocol = efi_va_arg(argptr, efi_guid_t*);
2359 protocol_interface = efi_va_arg(argptr, void*);
2360 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2361 EFI_NATIVE_INTERFACE,
2362 protocol_interface));
2366 /* In case of an error always return EFI_INVALID_PARAMETER */
2367 return EFI_EXIT(EFI_INVALID_PARAMETER);
2371 * efi_calculate_crc32() - calculate cyclic redundancy code
2372 * @data: buffer with data
2373 * @data_size: size of buffer in bytes
2374 * @crc32_p: cyclic redundancy code
2376 * This function implements the CalculateCrc32 service.
2378 * See the Unified Extensible Firmware Interface (UEFI) specification for
2381 * Return: status code
2383 static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2384 efi_uintn_t data_size,
2387 EFI_ENTRY("%p, %zu", data, data_size);
2388 *crc32_p = crc32(0, data, data_size);
2389 return EFI_EXIT(EFI_SUCCESS);
2393 * efi_copy_mem() - copy memory
2394 * @destination: destination of the copy operation
2395 * @source: source of the copy operation
2396 * @length: number of bytes to copy
2398 * This function implements the CopyMem service.
2400 * See the Unified Extensible Firmware Interface (UEFI) specification for
2403 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2406 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2407 memmove(destination, source, length);
2408 EFI_EXIT(EFI_SUCCESS);
2412 * efi_set_mem() - Fill memory with a byte value.
2413 * @buffer: buffer to fill
2414 * @size: size of buffer in bytes
2415 * @value: byte to copy to the buffer
2417 * This function implements the SetMem service.
2419 * See the Unified Extensible Firmware Interface (UEFI) specification for
2422 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2424 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2425 memset(buffer, value, size);
2426 EFI_EXIT(EFI_SUCCESS);
2430 * efi_protocol_open() - open protocol interface on a handle
2431 * @handler: handler of a protocol
2432 * @protocol_interface: interface implementing the protocol
2433 * @agent_handle: handle of the driver
2434 * @controller_handle: handle of the controller
2435 * @attributes: attributes indicating how to open the protocol
2437 * Return: status code
2439 static efi_status_t efi_protocol_open(
2440 struct efi_handler *handler,
2441 void **protocol_interface, void *agent_handle,
2442 void *controller_handle, uint32_t attributes)
2444 struct efi_open_protocol_info_item *item;
2445 struct efi_open_protocol_info_entry *match = NULL;
2446 bool opened_by_driver = false;
2447 bool opened_exclusive = false;
2449 /* If there is no agent, only return the interface */
2453 /* For TEST_PROTOCOL ignore interface attribute */
2454 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2455 *protocol_interface = NULL;
2458 * Check if the protocol is already opened by a driver with the same
2459 * attributes or opened exclusively
2461 list_for_each_entry(item, &handler->open_infos, link) {
2462 if (item->info.agent_handle == agent_handle) {
2463 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2464 (item->info.attributes == attributes))
2465 return EFI_ALREADY_STARTED;
2467 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2468 opened_exclusive = true;
2471 /* Only one controller can open the protocol exclusively */
2472 if (opened_exclusive && attributes &
2473 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2474 return EFI_ACCESS_DENIED;
2476 /* Prepare exclusive opening */
2477 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2478 /* Try to disconnect controllers */
2479 list_for_each_entry(item, &handler->open_infos, link) {
2480 if (item->info.attributes ==
2481 EFI_OPEN_PROTOCOL_BY_DRIVER)
2482 EFI_CALL(efi_disconnect_controller(
2483 item->info.controller_handle,
2484 item->info.agent_handle,
2487 opened_by_driver = false;
2488 /* Check if all controllers are disconnected */
2489 list_for_each_entry(item, &handler->open_infos, link) {
2490 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2491 opened_by_driver = true;
2493 /* Only one controller can be connected */
2494 if (opened_by_driver)
2495 return EFI_ACCESS_DENIED;
2498 /* Find existing entry */
2499 list_for_each_entry(item, &handler->open_infos, link) {
2500 if (item->info.agent_handle == agent_handle &&
2501 item->info.controller_handle == controller_handle)
2502 match = &item->info;
2504 /* None found, create one */
2506 match = efi_create_open_info(handler);
2508 return EFI_OUT_OF_RESOURCES;
2511 match->agent_handle = agent_handle;
2512 match->controller_handle = controller_handle;
2513 match->attributes = attributes;
2514 match->open_count++;
2517 /* For TEST_PROTOCOL ignore interface attribute. */
2518 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2519 *protocol_interface = handler->protocol_interface;
2525 * efi_open_protocol() - open protocol interface on a handle
2526 * @handle: handle on which the protocol shall be opened
2527 * @protocol: GUID of the protocol
2528 * @protocol_interface: interface implementing the protocol
2529 * @agent_handle: handle of the driver
2530 * @controller_handle: handle of the controller
2531 * @attributes: attributes indicating how to open the protocol
2533 * This function implements the OpenProtocol interface.
2535 * See the Unified Extensible Firmware Interface (UEFI) specification for
2538 * Return: status code
2540 static efi_status_t EFIAPI efi_open_protocol
2541 (efi_handle_t handle, const efi_guid_t *protocol,
2542 void **protocol_interface, efi_handle_t agent_handle,
2543 efi_handle_t controller_handle, uint32_t attributes)
2545 struct efi_handler *handler;
2546 efi_status_t r = EFI_INVALID_PARAMETER;
2548 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2549 protocol_interface, agent_handle, controller_handle,
2552 if (!handle || !protocol ||
2553 (!protocol_interface && attributes !=
2554 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2558 switch (attributes) {
2559 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2560 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2561 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2563 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2564 if (controller_handle == handle)
2567 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2568 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2569 /* Check that the controller handle is valid */
2570 if (!efi_search_obj(controller_handle))
2573 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2574 /* Check that the agent handle is valid */
2575 if (!efi_search_obj(agent_handle))
2582 r = efi_search_protocol(handle, protocol, &handler);
2583 if (r != EFI_SUCCESS)
2586 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2587 controller_handle, attributes);
2593 * efi_start_image() - call the entry point of an image
2594 * @image_handle: handle of the image
2595 * @exit_data_size: size of the buffer
2596 * @exit_data: buffer to receive the exit data of the called image
2598 * This function implements the StartImage service.
2600 * See the Unified Extensible Firmware Interface (UEFI) specification for
2603 * Return: status code
2605 efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2606 efi_uintn_t *exit_data_size,
2609 struct efi_loaded_image_obj *image_obj =
2610 (struct efi_loaded_image_obj *)image_handle;
2613 efi_handle_t parent_image = current_image;
2615 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2617 /* Check parameters */
2618 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2620 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2621 if (ret != EFI_SUCCESS)
2622 return EFI_EXIT(EFI_INVALID_PARAMETER);
2624 efi_is_direct_boot = false;
2626 image_obj->exit_data_size = exit_data_size;
2627 image_obj->exit_data = exit_data;
2629 /* call the image! */
2630 if (setjmp(&image_obj->exit_jmp)) {
2632 * We called the entry point of the child image with EFI_CALL
2633 * in the lines below. The child image called the Exit() boot
2634 * service efi_exit() which executed the long jump that brought
2635 * us to the current line. This implies that the second half
2636 * of the EFI_CALL macro has not been executed.
2640 * efi_exit() called efi_restore_gd(). We have to undo this
2641 * otherwise __efi_entry_check() will put the wrong value into
2647 * To get ready to call EFI_EXIT below we have to execute the
2648 * missed out steps of EFI_CALL.
2650 assert(__efi_entry_check());
2651 debug("%sEFI: %lu returned by started image\n",
2652 __efi_nesting_dec(),
2653 (unsigned long)((uintptr_t)image_obj->exit_status &
2655 current_image = parent_image;
2656 return EFI_EXIT(image_obj->exit_status);
2659 current_image = image_handle;
2660 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
2661 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
2662 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2665 * Usually UEFI applications call Exit() instead of returning.
2666 * But because the world doesn't consist of ponies and unicorns,
2667 * we're happy to emulate that behavior on behalf of a payload
2670 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2674 * efi_delete_image() - delete loaded image from memory)
2676 * @image_obj: handle of the loaded image
2677 * @loaded_image_protocol: loaded image protocol
2679 static void efi_delete_image(struct efi_loaded_image_obj *image_obj,
2680 struct efi_loaded_image *loaded_image_protocol)
2682 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2683 efi_size_in_pages(loaded_image_protocol->image_size));
2684 efi_delete_handle(&image_obj->header);
2688 * efi_unload_image() - unload an EFI image
2689 * @image_handle: handle of the image to be unloaded
2691 * This function implements the UnloadImage service.
2693 * See the Unified Extensible Firmware Interface (UEFI) specification for
2696 * Return: status code
2698 efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
2700 efi_status_t ret = EFI_SUCCESS;
2701 struct efi_object *efiobj;
2702 struct efi_loaded_image *loaded_image_protocol;
2704 EFI_ENTRY("%p", image_handle);
2706 efiobj = efi_search_obj(image_handle);
2708 ret = EFI_INVALID_PARAMETER;
2711 /* Find the loaded image protocol */
2712 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2713 (void **)&loaded_image_protocol,
2715 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2716 if (ret != EFI_SUCCESS) {
2717 ret = EFI_INVALID_PARAMETER;
2720 switch (efiobj->type) {
2721 case EFI_OBJECT_TYPE_STARTED_IMAGE:
2722 /* Call the unload function */
2723 if (!loaded_image_protocol->unload) {
2724 ret = EFI_UNSUPPORTED;
2727 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
2728 if (ret != EFI_SUCCESS)
2731 case EFI_OBJECT_TYPE_LOADED_IMAGE:
2734 ret = EFI_INVALID_PARAMETER;
2737 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
2738 loaded_image_protocol);
2740 return EFI_EXIT(ret);
2744 * efi_update_exit_data() - fill exit data parameters of StartImage()
2746 * @image_obj image handle
2747 * @exit_data_size size of the exit data buffer
2748 * @exit_data buffer with data returned by UEFI payload
2749 * Return: status code
2751 static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
2752 efi_uintn_t exit_data_size,
2758 * If exit_data is not provided to StartImage(), exit_data_size must be
2761 if (!image_obj->exit_data)
2763 if (image_obj->exit_data_size)
2764 *image_obj->exit_data_size = exit_data_size;
2765 if (exit_data_size && exit_data) {
2766 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
2768 (void **)image_obj->exit_data);
2769 if (ret != EFI_SUCCESS)
2771 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
2773 image_obj->exit_data = NULL;
2779 * efi_exit() - leave an EFI application or driver
2780 * @image_handle: handle of the application or driver that is exiting
2781 * @exit_status: status code
2782 * @exit_data_size: size of the buffer in bytes
2783 * @exit_data: buffer with data describing an error
2785 * This function implements the Exit service.
2787 * See the Unified Extensible Firmware Interface (UEFI) specification for
2790 * Return: status code
2792 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
2793 efi_status_t exit_status,
2794 efi_uintn_t exit_data_size,
2798 * TODO: We should call the unload procedure of the loaded
2802 struct efi_loaded_image *loaded_image_protocol;
2803 struct efi_loaded_image_obj *image_obj =
2804 (struct efi_loaded_image_obj *)image_handle;
2806 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
2807 exit_data_size, exit_data);
2809 /* Check parameters */
2810 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2811 (void **)&loaded_image_protocol,
2813 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2814 if (ret != EFI_SUCCESS) {
2815 ret = EFI_INVALID_PARAMETER;
2819 /* Unloading of unstarted images */
2820 switch (image_obj->header.type) {
2821 case EFI_OBJECT_TYPE_STARTED_IMAGE:
2823 case EFI_OBJECT_TYPE_LOADED_IMAGE:
2824 efi_delete_image(image_obj, loaded_image_protocol);
2828 /* Handle does not refer to loaded image */
2829 ret = EFI_INVALID_PARAMETER;
2832 /* A started image can only be unloaded it is the last one started. */
2833 if (image_handle != current_image) {
2834 ret = EFI_INVALID_PARAMETER;
2838 /* Exit data is only foreseen in case of failure. */
2839 if (exit_status != EFI_SUCCESS) {
2840 ret = efi_update_exit_data(image_obj, exit_data_size,
2842 /* Exiting has priority. Don't return error to caller. */
2843 if (ret != EFI_SUCCESS)
2844 EFI_PRINT("%s: out of memory\n", __func__);
2846 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
2847 exit_status != EFI_SUCCESS)
2848 efi_delete_image(image_obj, loaded_image_protocol);
2850 /* Make sure entry/exit counts for EFI world cross-overs match */
2851 EFI_EXIT(exit_status);
2854 * But longjmp out with the U-Boot gd, not the application's, as
2855 * the other end is a setjmp call inside EFI context.
2859 image_obj->exit_status = exit_status;
2860 longjmp(&image_obj->exit_jmp, 1);
2862 panic("EFI application exited");
2864 return EFI_EXIT(ret);
2868 * efi_handle_protocol() - get interface of a protocol on a handle
2869 * @handle: handle on which the protocol shall be opened
2870 * @protocol: GUID of the protocol
2871 * @protocol_interface: interface implementing the protocol
2873 * This function implements the HandleProtocol service.
2875 * See the Unified Extensible Firmware Interface (UEFI) specification for
2878 * Return: status code
2880 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2881 const efi_guid_t *protocol,
2882 void **protocol_interface)
2884 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2885 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2889 * efi_bind_controller() - bind a single driver to a controller
2890 * @controller_handle: controller handle
2891 * @driver_image_handle: driver handle
2892 * @remain_device_path: remaining path
2894 * Return: status code
2896 static efi_status_t efi_bind_controller(
2897 efi_handle_t controller_handle,
2898 efi_handle_t driver_image_handle,
2899 struct efi_device_path *remain_device_path)
2901 struct efi_driver_binding_protocol *binding_protocol;
2904 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2905 &efi_guid_driver_binding_protocol,
2906 (void **)&binding_protocol,
2907 driver_image_handle, NULL,
2908 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2909 if (r != EFI_SUCCESS)
2911 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2913 remain_device_path));
2914 if (r == EFI_SUCCESS)
2915 r = EFI_CALL(binding_protocol->start(binding_protocol,
2917 remain_device_path));
2918 EFI_CALL(efi_close_protocol(driver_image_handle,
2919 &efi_guid_driver_binding_protocol,
2920 driver_image_handle, NULL));
2925 * efi_connect_single_controller() - connect a single driver to a controller
2926 * @controller_handle: controller
2927 * @driver_image_handle: driver
2928 * @remain_device_path: remaining path
2930 * Return: status code
2932 static efi_status_t efi_connect_single_controller(
2933 efi_handle_t controller_handle,
2934 efi_handle_t *driver_image_handle,
2935 struct efi_device_path *remain_device_path)
2937 efi_handle_t *buffer;
2941 size_t connected = 0;
2943 /* Get buffer with all handles with driver binding protocol */
2944 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2945 &efi_guid_driver_binding_protocol,
2946 NULL, &count, &buffer));
2947 if (r != EFI_SUCCESS)
2950 /* Context Override */
2951 if (driver_image_handle) {
2952 for (; *driver_image_handle; ++driver_image_handle) {
2953 for (i = 0; i < count; ++i) {
2954 if (buffer[i] == *driver_image_handle) {
2956 r = efi_bind_controller(
2958 *driver_image_handle,
2959 remain_device_path);
2961 * For drivers that do not support the
2962 * controller or are already connected
2963 * we receive an error code here.
2965 if (r == EFI_SUCCESS)
2973 * TODO: Some overrides are not yet implemented:
2974 * - Platform Driver Override
2975 * - Driver Family Override Search
2976 * - Bus Specific Driver Override
2979 /* Driver Binding Search */
2980 for (i = 0; i < count; ++i) {
2982 r = efi_bind_controller(controller_handle,
2984 remain_device_path);
2985 if (r == EFI_SUCCESS)
2990 efi_free_pool(buffer);
2992 return EFI_NOT_FOUND;
2997 * efi_connect_controller() - connect a controller to a driver
2998 * @controller_handle: handle of the controller
2999 * @driver_image_handle: handle of the driver
3000 * @remain_device_path: device path of a child controller
3001 * @recursive: true to connect all child controllers
3003 * This function implements the ConnectController service.
3005 * See the Unified Extensible Firmware Interface (UEFI) specification for
3008 * First all driver binding protocol handles are tried for binding drivers.
3009 * Afterwards all handles that have opened a protocol of the controller
3010 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3012 * Return: status code
3014 static efi_status_t EFIAPI efi_connect_controller(
3015 efi_handle_t controller_handle,
3016 efi_handle_t *driver_image_handle,
3017 struct efi_device_path *remain_device_path,
3021 efi_status_t ret = EFI_NOT_FOUND;
3022 struct efi_object *efiobj;
3024 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
3025 remain_device_path, recursive);
3027 efiobj = efi_search_obj(controller_handle);
3029 ret = EFI_INVALID_PARAMETER;
3033 r = efi_connect_single_controller(controller_handle,
3034 driver_image_handle,
3035 remain_device_path);
3036 if (r == EFI_SUCCESS)
3039 struct efi_handler *handler;
3040 struct efi_open_protocol_info_item *item;
3042 list_for_each_entry(handler, &efiobj->protocols, link) {
3043 list_for_each_entry(item, &handler->open_infos, link) {
3044 if (item->info.attributes &
3045 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3046 r = EFI_CALL(efi_connect_controller(
3047 item->info.controller_handle,
3048 driver_image_handle,
3051 if (r == EFI_SUCCESS)
3057 /* Check for child controller specified by end node */
3058 if (ret != EFI_SUCCESS && remain_device_path &&
3059 remain_device_path->type == DEVICE_PATH_TYPE_END)
3062 return EFI_EXIT(ret);
3066 * efi_reinstall_protocol_interface() - reinstall protocol interface
3067 * @handle: handle on which the protocol shall be reinstalled
3068 * @protocol: GUID of the protocol to be installed
3069 * @old_interface: interface to be removed
3070 * @new_interface: interface to be installed
3072 * This function implements the ReinstallProtocolInterface service.
3074 * See the Unified Extensible Firmware Interface (UEFI) specification for
3077 * The old interface is uninstalled. The new interface is installed.
3078 * Drivers are connected.
3080 * Return: status code
3082 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3083 efi_handle_t handle, const efi_guid_t *protocol,
3084 void *old_interface, void *new_interface)
3088 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3091 /* Uninstall protocol but do not delete handle */
3092 ret = efi_uninstall_protocol(handle, protocol, old_interface);
3093 if (ret != EFI_SUCCESS)
3096 /* Install the new protocol */
3097 ret = efi_add_protocol(handle, protocol, new_interface);
3099 * The UEFI spec does not specify what should happen to the handle
3100 * if in case of an error no protocol interface remains on the handle.
3101 * So let's do nothing here.
3103 if (ret != EFI_SUCCESS)
3106 * The returned status code has to be ignored.
3107 * Do not create an error if no suitable driver for the handle exists.
3109 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3111 return EFI_EXIT(ret);
3115 * efi_get_child_controllers() - get all child controllers associated to a driver
3116 * @efiobj: handle of the controller
3117 * @driver_handle: handle of the driver
3118 * @number_of_children: number of child controllers
3119 * @child_handle_buffer: handles of the the child controllers
3121 * The allocated buffer has to be freed with free().
3123 * Return: status code
3125 static efi_status_t efi_get_child_controllers(
3126 struct efi_object *efiobj,
3127 efi_handle_t driver_handle,
3128 efi_uintn_t *number_of_children,
3129 efi_handle_t **child_handle_buffer)
3131 struct efi_handler *handler;
3132 struct efi_open_protocol_info_item *item;
3133 efi_uintn_t count = 0, i;
3136 /* Count all child controller associations */
3137 list_for_each_entry(handler, &efiobj->protocols, link) {
3138 list_for_each_entry(item, &handler->open_infos, link) {
3139 if (item->info.agent_handle == driver_handle &&
3140 item->info.attributes &
3141 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3146 * Create buffer. In case of duplicate child controller assignments
3147 * the buffer will be too large. But that does not harm.
3149 *number_of_children = 0;
3150 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3151 if (!*child_handle_buffer)
3152 return EFI_OUT_OF_RESOURCES;
3153 /* Copy unique child handles */
3154 list_for_each_entry(handler, &efiobj->protocols, link) {
3155 list_for_each_entry(item, &handler->open_infos, link) {
3156 if (item->info.agent_handle == driver_handle &&
3157 item->info.attributes &
3158 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3159 /* Check this is a new child controller */
3161 for (i = 0; i < *number_of_children; ++i) {
3162 if ((*child_handle_buffer)[i] ==
3163 item->info.controller_handle)
3166 /* Copy handle to buffer */
3168 i = (*number_of_children)++;
3169 (*child_handle_buffer)[i] =
3170 item->info.controller_handle;
3179 * efi_disconnect_controller() - disconnect a controller from a driver
3180 * @controller_handle: handle of the controller
3181 * @driver_image_handle: handle of the driver
3182 * @child_handle: handle of the child to destroy
3184 * This function implements the DisconnectController service.
3186 * See the Unified Extensible Firmware Interface (UEFI) specification for
3189 * Return: status code
3191 static efi_status_t EFIAPI efi_disconnect_controller(
3192 efi_handle_t controller_handle,
3193 efi_handle_t driver_image_handle,
3194 efi_handle_t child_handle)
3196 struct efi_driver_binding_protocol *binding_protocol;
3197 efi_handle_t *child_handle_buffer = NULL;
3198 size_t number_of_children = 0;
3200 size_t stop_count = 0;
3201 struct efi_object *efiobj;
3203 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3206 efiobj = efi_search_obj(controller_handle);
3208 r = EFI_INVALID_PARAMETER;
3212 if (child_handle && !efi_search_obj(child_handle)) {
3213 r = EFI_INVALID_PARAMETER;
3217 /* If no driver handle is supplied, disconnect all drivers */
3218 if (!driver_image_handle) {
3219 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3223 /* Create list of child handles */
3225 number_of_children = 1;
3226 child_handle_buffer = &child_handle;
3228 efi_get_child_controllers(efiobj,
3229 driver_image_handle,
3230 &number_of_children,
3231 &child_handle_buffer);
3234 /* Get the driver binding protocol */
3235 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3236 &efi_guid_driver_binding_protocol,
3237 (void **)&binding_protocol,
3238 driver_image_handle, NULL,
3239 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3240 if (r != EFI_SUCCESS)
3242 /* Remove the children */
3243 if (number_of_children) {
3244 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3247 child_handle_buffer));
3248 if (r == EFI_SUCCESS)
3251 /* Remove the driver */
3253 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3256 if (r == EFI_SUCCESS)
3258 EFI_CALL(efi_close_protocol(driver_image_handle,
3259 &efi_guid_driver_binding_protocol,
3260 driver_image_handle, NULL));
3268 free(child_handle_buffer);
3272 static struct efi_boot_services efi_boot_services = {
3274 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3275 .revision = EFI_SPECIFICATION_VERSION,
3276 .headersize = sizeof(struct efi_boot_services),
3278 .raise_tpl = efi_raise_tpl,
3279 .restore_tpl = efi_restore_tpl,
3280 .allocate_pages = efi_allocate_pages_ext,
3281 .free_pages = efi_free_pages_ext,
3282 .get_memory_map = efi_get_memory_map_ext,
3283 .allocate_pool = efi_allocate_pool_ext,
3284 .free_pool = efi_free_pool_ext,
3285 .create_event = efi_create_event_ext,
3286 .set_timer = efi_set_timer_ext,
3287 .wait_for_event = efi_wait_for_event,
3288 .signal_event = efi_signal_event_ext,
3289 .close_event = efi_close_event,
3290 .check_event = efi_check_event,
3291 .install_protocol_interface = efi_install_protocol_interface,
3292 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3293 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3294 .handle_protocol = efi_handle_protocol,
3296 .register_protocol_notify = efi_register_protocol_notify,
3297 .locate_handle = efi_locate_handle_ext,
3298 .locate_device_path = efi_locate_device_path,
3299 .install_configuration_table = efi_install_configuration_table_ext,
3300 .load_image = efi_load_image,
3301 .start_image = efi_start_image,
3303 .unload_image = efi_unload_image,
3304 .exit_boot_services = efi_exit_boot_services,
3305 .get_next_monotonic_count = efi_get_next_monotonic_count,
3307 .set_watchdog_timer = efi_set_watchdog_timer,
3308 .connect_controller = efi_connect_controller,
3309 .disconnect_controller = efi_disconnect_controller,
3310 .open_protocol = efi_open_protocol,
3311 .close_protocol = efi_close_protocol,
3312 .open_protocol_information = efi_open_protocol_information,
3313 .protocols_per_handle = efi_protocols_per_handle,
3314 .locate_handle_buffer = efi_locate_handle_buffer,
3315 .locate_protocol = efi_locate_protocol,
3316 .install_multiple_protocol_interfaces =
3317 efi_install_multiple_protocol_interfaces,
3318 .uninstall_multiple_protocol_interfaces =
3319 efi_uninstall_multiple_protocol_interfaces,
3320 .calculate_crc32 = efi_calculate_crc32,
3321 .copy_mem = efi_copy_mem,
3322 .set_mem = efi_set_mem,
3323 .create_event_ex = efi_create_event_ex,
3326 static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3328 struct efi_system_table __efi_runtime_data systab = {
3330 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3331 .revision = EFI_SPECIFICATION_VERSION,
3332 .headersize = sizeof(struct efi_system_table),
3334 .fw_vendor = firmware_vendor,
3335 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3336 .con_in = (void *)&efi_con_in,
3337 .con_out = (void *)&efi_con_out,
3338 .std_err = (void *)&efi_con_out,
3339 .runtime = (void *)&efi_runtime_services,
3340 .boottime = (void *)&efi_boot_services,
3346 * efi_initialize_system_table() - Initialize system table
3348 * Return: status code
3350 efi_status_t efi_initialize_system_table(void)
3354 /* Allocate configuration table array */
3355 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3356 EFI_MAX_CONFIGURATION_TABLES *
3357 sizeof(struct efi_configuration_table),
3358 (void **)&systab.tables);
3360 /* Set CRC32 field in table headers */
3361 efi_update_table_header_crc32(&systab.hdr);
3362 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3363 efi_update_table_header_crc32(&efi_boot_services.hdr);