1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application boot time services
5 * Copyright (c) 2016 Alexander Graf
10 #include <efi_loader.h>
11 #include <environment.h>
13 #include <linux/libfdt_env.h>
14 #include <u-boot/crc.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 /* Task priority level */
21 static efi_uintn_t efi_tpl = TPL_APPLICATION;
23 /* This list contains all the EFI objects our payload has access to */
24 LIST_HEAD(efi_obj_list);
26 /* List of all events */
27 LIST_HEAD(efi_events);
29 /* Handle of the currently executing image */
30 static efi_handle_t current_image;
33 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
34 * we need to do trickery with caches. Since we don't want to break the EFI
35 * aware boot path, only apply hacks when loading exiting directly (breaking
36 * direct Linux EFI booting along the way - oh well).
38 static bool efi_is_direct_boot = true;
42 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
43 * fixed when compiling U-Boot. However, the payload does not know about that
44 * restriction so we need to manually swap its and our view of that register on
45 * EFI callback entry/exit.
47 static volatile void *efi_gd, *app_gd;
50 /* 1 if inside U-Boot code, 0 if inside EFI payload code */
51 static int entry_count = 1;
52 static int nesting_level;
53 /* GUID of the device tree table */
54 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
55 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
56 const efi_guid_t efi_guid_driver_binding_protocol =
57 EFI_DRIVER_BINDING_PROTOCOL_GUID;
59 /* event group ExitBootServices() invoked */
60 const efi_guid_t efi_guid_event_group_exit_boot_services =
61 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
62 /* event group SetVirtualAddressMap() invoked */
63 const efi_guid_t efi_guid_event_group_virtual_address_change =
64 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
65 /* event group memory map changed */
66 const efi_guid_t efi_guid_event_group_memory_map_change =
67 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
68 /* event group boot manager about to boot */
69 const efi_guid_t efi_guid_event_group_ready_to_boot =
70 EFI_EVENT_GROUP_READY_TO_BOOT;
71 /* event group ResetSystem() invoked (before ExitBootServices) */
72 const efi_guid_t efi_guid_event_group_reset_system =
73 EFI_EVENT_GROUP_RESET_SYSTEM;
75 static efi_status_t EFIAPI efi_disconnect_controller(
76 efi_handle_t controller_handle,
77 efi_handle_t driver_image_handle,
78 efi_handle_t child_handle);
80 /* Called on every callback entry */
81 int __efi_entry_check(void)
83 int ret = entry_count++ == 0;
92 /* Called on every callback exit */
93 int __efi_exit_check(void)
95 int ret = --entry_count == 0;
102 /* Called from do_bootefi_exec() */
103 void efi_save_gd(void)
111 * Special case handler for error/abort that just forces things back to u-boot
112 * world so we can dump out an abort message, without any care about returning
113 * back to UEFI world.
115 void efi_restore_gd(void)
118 /* Only restore if we're already in EFI context */
126 * indent_string() - returns a string for indenting with two spaces per level
127 * @level: indent level
129 * A maximum of ten indent levels is supported. Higher indent levels will be
132 * Return: A string for indenting with two spaces per level is
135 static const char *indent_string(int level)
137 const char *indent = " ";
138 const int max = strlen(indent);
140 level = min(max, level * 2);
141 return &indent[max - level];
144 const char *__efi_nesting(void)
146 return indent_string(nesting_level);
149 const char *__efi_nesting_inc(void)
151 return indent_string(nesting_level++);
154 const char *__efi_nesting_dec(void)
156 return indent_string(--nesting_level);
160 * efi_queue_event() - queue an EFI event
161 * @event: event to signal
162 * @check_tpl: check the TPL level
164 * This function queues the notification function of the event for future
167 * The notification function is called if the task priority level of the event
168 * is higher than the current task priority level.
170 * For the SignalEvent service see efi_signal_event_ext.
173 static void efi_queue_event(struct efi_event *event, bool check_tpl)
175 if (event->notify_function) {
176 event->is_queued = true;
178 if (check_tpl && efi_tpl >= event->notify_tpl)
180 EFI_CALL_VOID(event->notify_function(event,
181 event->notify_context));
183 event->is_queued = false;
187 * is_valid_tpl() - check if the task priority level is valid
189 * @tpl: TPL level to check
190 * Return: status code
192 efi_status_t is_valid_tpl(efi_uintn_t tpl)
195 case TPL_APPLICATION:
201 return EFI_INVALID_PARAMETER;
206 * efi_signal_event() - signal an EFI event
207 * @event: event to signal
208 * @check_tpl: check the TPL level
210 * This function signals an event. If the event belongs to an event group all
211 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
212 * their notification function is queued.
214 * For the SignalEvent service see efi_signal_event_ext.
216 void efi_signal_event(struct efi_event *event, bool check_tpl)
219 struct efi_event *evt;
222 * The signaled state has to set before executing any
223 * notification function
225 list_for_each_entry(evt, &efi_events, link) {
226 if (!evt->group || guidcmp(evt->group, event->group))
228 if (evt->is_signaled)
230 evt->is_signaled = true;
231 if (evt->type & EVT_NOTIFY_SIGNAL &&
232 evt->notify_function)
233 evt->is_queued = true;
235 list_for_each_entry(evt, &efi_events, link) {
236 if (!evt->group || guidcmp(evt->group, event->group))
239 efi_queue_event(evt, check_tpl);
241 } else if (!event->is_signaled) {
242 event->is_signaled = true;
243 if (event->type & EVT_NOTIFY_SIGNAL)
244 efi_queue_event(event, check_tpl);
249 * efi_raise_tpl() - raise the task priority level
250 * @new_tpl: new value of the task priority level
252 * This function implements the RaiseTpl service.
254 * See the Unified Extensible Firmware Interface (UEFI) specification for
257 * Return: old value of the task priority level
259 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
261 efi_uintn_t old_tpl = efi_tpl;
263 EFI_ENTRY("0x%zx", new_tpl);
265 if (new_tpl < efi_tpl)
266 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
268 if (efi_tpl > TPL_HIGH_LEVEL)
269 efi_tpl = TPL_HIGH_LEVEL;
271 EFI_EXIT(EFI_SUCCESS);
276 * efi_restore_tpl() - lower the task priority level
277 * @old_tpl: value of the task priority level to be restored
279 * This function implements the RestoreTpl service.
281 * See the Unified Extensible Firmware Interface (UEFI) specification for
284 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
286 EFI_ENTRY("0x%zx", old_tpl);
288 if (old_tpl > efi_tpl)
289 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
291 if (efi_tpl > TPL_HIGH_LEVEL)
292 efi_tpl = TPL_HIGH_LEVEL;
295 * Lowering the TPL may have made queued events eligible for execution.
299 EFI_EXIT(EFI_SUCCESS);
303 * efi_allocate_pages_ext() - allocate memory pages
304 * @type: type of allocation to be performed
305 * @memory_type: usage type of the allocated memory
306 * @pages: number of pages to be allocated
307 * @memory: allocated memory
309 * This function implements the AllocatePages service.
311 * See the Unified Extensible Firmware Interface (UEFI) specification for
314 * Return: status code
316 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
322 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
323 r = efi_allocate_pages(type, memory_type, pages, memory);
328 * efi_free_pages_ext() - Free memory pages.
329 * @memory: start of the memory area to be freed
330 * @pages: number of pages to be freed
332 * This function implements the FreePages service.
334 * See the Unified Extensible Firmware Interface (UEFI) specification for
337 * Return: status code
339 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
344 EFI_ENTRY("%llx, 0x%zx", memory, pages);
345 r = efi_free_pages(memory, pages);
350 * efi_get_memory_map_ext() - get map describing memory usage
351 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
352 * on exit the size of the copied memory map
353 * @memory_map: buffer to which the memory map is written
354 * @map_key: key for the memory map
355 * @descriptor_size: size of an individual memory descriptor
356 * @descriptor_version: version number of the memory descriptor structure
358 * This function implements the GetMemoryMap service.
360 * See the Unified Extensible Firmware Interface (UEFI) specification for
363 * Return: status code
365 static efi_status_t EFIAPI efi_get_memory_map_ext(
366 efi_uintn_t *memory_map_size,
367 struct efi_mem_desc *memory_map,
368 efi_uintn_t *map_key,
369 efi_uintn_t *descriptor_size,
370 uint32_t *descriptor_version)
374 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
375 map_key, descriptor_size, descriptor_version);
376 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
377 descriptor_size, descriptor_version);
382 * efi_allocate_pool_ext() - allocate memory from pool
383 * @pool_type: type of the pool from which memory is to be allocated
384 * @size: number of bytes to be allocated
385 * @buffer: allocated memory
387 * This function implements the AllocatePool service.
389 * See the Unified Extensible Firmware Interface (UEFI) specification for
392 * Return: status code
394 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
400 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
401 r = efi_allocate_pool(pool_type, size, buffer);
406 * efi_free_pool_ext() - free memory from pool
407 * @buffer: start of memory to be freed
409 * This function implements the FreePool service.
411 * See the Unified Extensible Firmware Interface (UEFI) specification for
414 * Return: status code
416 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
420 EFI_ENTRY("%p", buffer);
421 r = efi_free_pool(buffer);
426 * efi_add_handle() - add a new handle to the object list
428 * @handle: handle to be added
430 * The protocols list is initialized. The handle is added to the list of known
433 void efi_add_handle(efi_handle_t handle)
437 INIT_LIST_HEAD(&handle->protocols);
438 list_add_tail(&handle->link, &efi_obj_list);
442 * efi_create_handle() - create handle
443 * @handle: new handle
445 * Return: status code
447 efi_status_t efi_create_handle(efi_handle_t *handle)
449 struct efi_object *obj;
451 obj = calloc(1, sizeof(struct efi_object));
453 return EFI_OUT_OF_RESOURCES;
462 * efi_search_protocol() - find a protocol on a handle.
464 * @protocol_guid: GUID of the protocol
465 * @handler: reference to the protocol
467 * Return: status code
469 efi_status_t efi_search_protocol(const efi_handle_t handle,
470 const efi_guid_t *protocol_guid,
471 struct efi_handler **handler)
473 struct efi_object *efiobj;
474 struct list_head *lhandle;
476 if (!handle || !protocol_guid)
477 return EFI_INVALID_PARAMETER;
478 efiobj = efi_search_obj(handle);
480 return EFI_INVALID_PARAMETER;
481 list_for_each(lhandle, &efiobj->protocols) {
482 struct efi_handler *protocol;
484 protocol = list_entry(lhandle, struct efi_handler, link);
485 if (!guidcmp(protocol->guid, protocol_guid)) {
491 return EFI_NOT_FOUND;
495 * efi_remove_protocol() - delete protocol from a handle
496 * @handle: handle from which the protocol shall be deleted
497 * @protocol: GUID of the protocol to be deleted
498 * @protocol_interface: interface of the protocol implementation
500 * Return: status code
502 efi_status_t efi_remove_protocol(const efi_handle_t handle,
503 const efi_guid_t *protocol,
504 void *protocol_interface)
506 struct efi_handler *handler;
509 ret = efi_search_protocol(handle, protocol, &handler);
510 if (ret != EFI_SUCCESS)
512 if (guidcmp(handler->guid, protocol))
513 return EFI_INVALID_PARAMETER;
514 if (handler->protocol_interface != protocol_interface)
515 return EFI_INVALID_PARAMETER;
516 list_del(&handler->link);
522 * efi_remove_all_protocols() - delete all protocols from a handle
523 * @handle: handle from which the protocols shall be deleted
525 * Return: status code
527 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
529 struct efi_object *efiobj;
530 struct efi_handler *protocol;
531 struct efi_handler *pos;
533 efiobj = efi_search_obj(handle);
535 return EFI_INVALID_PARAMETER;
536 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
539 ret = efi_remove_protocol(handle, protocol->guid,
540 protocol->protocol_interface);
541 if (ret != EFI_SUCCESS)
548 * efi_delete_handle() - delete handle
550 * @obj: handle to delete
552 void efi_delete_handle(efi_handle_t handle)
556 efi_remove_all_protocols(handle);
557 list_del(&handle->link);
562 * efi_is_event() - check if a pointer is a valid event
563 * @event: pointer to check
565 * Return: status code
567 static efi_status_t efi_is_event(const struct efi_event *event)
569 const struct efi_event *evt;
572 return EFI_INVALID_PARAMETER;
573 list_for_each_entry(evt, &efi_events, link) {
577 return EFI_INVALID_PARAMETER;
581 * efi_create_event() - create an event
582 * @type: type of the event to create
583 * @notify_tpl: task priority level of the event
584 * @notify_function: notification function of the event
585 * @notify_context: pointer passed to the notification function
586 * @group: event group
587 * @event: created event
589 * This function is used inside U-Boot code to create an event.
591 * For the API function implementing the CreateEvent service see
592 * efi_create_event_ext.
594 * Return: status code
596 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
597 void (EFIAPI *notify_function) (
598 struct efi_event *event,
600 void *notify_context, efi_guid_t *group,
601 struct efi_event **event)
603 struct efi_event *evt;
606 return EFI_INVALID_PARAMETER;
611 case EVT_NOTIFY_SIGNAL:
612 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
613 case EVT_NOTIFY_WAIT:
614 case EVT_TIMER | EVT_NOTIFY_WAIT:
615 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
616 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
619 return EFI_INVALID_PARAMETER;
622 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
623 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
624 return EFI_INVALID_PARAMETER;
626 evt = calloc(1, sizeof(struct efi_event));
628 return EFI_OUT_OF_RESOURCES;
630 evt->notify_tpl = notify_tpl;
631 evt->notify_function = notify_function;
632 evt->notify_context = notify_context;
634 /* Disable timers on boot up */
635 evt->trigger_next = -1ULL;
636 evt->is_queued = false;
637 evt->is_signaled = false;
638 list_add_tail(&evt->link, &efi_events);
644 * efi_create_event_ex() - create an event in a group
645 * @type: type of the event to create
646 * @notify_tpl: task priority level of the event
647 * @notify_function: notification function of the event
648 * @notify_context: pointer passed to the notification function
649 * @event: created event
650 * @event_group: event group
652 * This function implements the CreateEventEx service.
654 * See the Unified Extensible Firmware Interface (UEFI) specification for
657 * Return: status code
659 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
660 void (EFIAPI *notify_function) (
661 struct efi_event *event,
663 void *notify_context,
664 efi_guid_t *event_group,
665 struct efi_event **event)
669 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
670 notify_context, event_group);
673 * The allowable input parameters are the same as in CreateEvent()
674 * except for the following two disallowed event types.
677 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
678 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
679 ret = EFI_INVALID_PARAMETER;
683 ret = efi_create_event(type, notify_tpl, notify_function,
684 notify_context, event_group, event);
686 return EFI_EXIT(ret);
690 * efi_create_event_ext() - create an event
691 * @type: type of the event to create
692 * @notify_tpl: task priority level of the event
693 * @notify_function: notification function of the event
694 * @notify_context: pointer passed to the notification function
695 * @event: created event
697 * This function implements the CreateEvent service.
699 * See the Unified Extensible Firmware Interface (UEFI) specification for
702 * Return: status code
704 static efi_status_t EFIAPI efi_create_event_ext(
705 uint32_t type, efi_uintn_t notify_tpl,
706 void (EFIAPI *notify_function) (
707 struct efi_event *event,
709 void *notify_context, struct efi_event **event)
711 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
713 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
714 notify_context, NULL, event));
718 * efi_timer_check() - check if a timer event has occurred
720 * Check if a timer event has occurred or a queued notification function should
723 * Our timers have to work without interrupts, so we check whenever keyboard
724 * input or disk accesses happen if enough time elapsed for them to fire.
726 void efi_timer_check(void)
728 struct efi_event *evt;
729 u64 now = timer_get_us();
731 list_for_each_entry(evt, &efi_events, link) {
733 efi_queue_event(evt, true);
734 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
736 switch (evt->trigger_type) {
737 case EFI_TIMER_RELATIVE:
738 evt->trigger_type = EFI_TIMER_STOP;
740 case EFI_TIMER_PERIODIC:
741 evt->trigger_next += evt->trigger_time;
746 evt->is_signaled = false;
747 efi_signal_event(evt, true);
753 * efi_set_timer() - set the trigger time for a timer event or stop the event
754 * @event: event for which the timer is set
755 * @type: type of the timer
756 * @trigger_time: trigger period in multiples of 100 ns
758 * This is the function for internal usage in U-Boot. For the API function
759 * implementing the SetTimer service see efi_set_timer_ext.
761 * Return: status code
763 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
764 uint64_t trigger_time)
766 /* Check that the event is valid */
767 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
768 return EFI_INVALID_PARAMETER;
771 * The parameter defines a multiple of 100 ns.
772 * We use multiples of 1000 ns. So divide by 10.
774 do_div(trigger_time, 10);
778 event->trigger_next = -1ULL;
780 case EFI_TIMER_PERIODIC:
781 case EFI_TIMER_RELATIVE:
782 event->trigger_next = timer_get_us() + trigger_time;
785 return EFI_INVALID_PARAMETER;
787 event->trigger_type = type;
788 event->trigger_time = trigger_time;
789 event->is_signaled = false;
794 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
796 * @event: event for which the timer is set
797 * @type: type of the timer
798 * @trigger_time: trigger period in multiples of 100 ns
800 * This function implements the SetTimer service.
802 * See the Unified Extensible Firmware Interface (UEFI) specification for
806 * Return: status code
808 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
809 enum efi_timer_delay type,
810 uint64_t trigger_time)
812 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
813 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
817 * efi_wait_for_event() - wait for events to be signaled
818 * @num_events: number of events to be waited for
819 * @event: events to be waited for
820 * @index: index of the event that was signaled
822 * This function implements the WaitForEvent service.
824 * See the Unified Extensible Firmware Interface (UEFI) specification for
827 * Return: status code
829 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
830 struct efi_event **event,
835 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
837 /* Check parameters */
838 if (!num_events || !event)
839 return EFI_EXIT(EFI_INVALID_PARAMETER);
841 if (efi_tpl != TPL_APPLICATION)
842 return EFI_EXIT(EFI_UNSUPPORTED);
843 for (i = 0; i < num_events; ++i) {
844 if (efi_is_event(event[i]) != EFI_SUCCESS)
845 return EFI_EXIT(EFI_INVALID_PARAMETER);
846 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
847 return EFI_EXIT(EFI_INVALID_PARAMETER);
848 if (!event[i]->is_signaled)
849 efi_queue_event(event[i], true);
852 /* Wait for signal */
854 for (i = 0; i < num_events; ++i) {
855 if (event[i]->is_signaled)
858 /* Allow events to occur. */
864 * Reset the signal which is passed to the caller to allow periodic
867 event[i]->is_signaled = false;
871 return EFI_EXIT(EFI_SUCCESS);
875 * efi_signal_event_ext() - signal an EFI event
876 * @event: event to signal
878 * This function implements the SignalEvent service.
880 * See the Unified Extensible Firmware Interface (UEFI) specification for
883 * This functions sets the signaled state of the event and queues the
884 * notification function for execution.
886 * Return: status code
888 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
890 EFI_ENTRY("%p", event);
891 if (efi_is_event(event) != EFI_SUCCESS)
892 return EFI_EXIT(EFI_INVALID_PARAMETER);
893 efi_signal_event(event, true);
894 return EFI_EXIT(EFI_SUCCESS);
898 * efi_close_event() - close an EFI event
899 * @event: event to close
901 * This function implements the CloseEvent service.
903 * See the Unified Extensible Firmware Interface (UEFI) specification for
906 * Return: status code
908 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
910 EFI_ENTRY("%p", event);
911 if (efi_is_event(event) != EFI_SUCCESS)
912 return EFI_EXIT(EFI_INVALID_PARAMETER);
913 list_del(&event->link);
915 return EFI_EXIT(EFI_SUCCESS);
919 * efi_check_event() - check if an event is signaled
920 * @event: event to check
922 * This function implements the CheckEvent service.
924 * See the Unified Extensible Firmware Interface (UEFI) specification for
927 * If an event is not signaled yet, the notification function is queued. The
928 * signaled state is cleared.
930 * Return: status code
932 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
934 EFI_ENTRY("%p", event);
936 if (efi_is_event(event) != EFI_SUCCESS ||
937 event->type & EVT_NOTIFY_SIGNAL)
938 return EFI_EXIT(EFI_INVALID_PARAMETER);
939 if (!event->is_signaled)
940 efi_queue_event(event, true);
941 if (event->is_signaled) {
942 event->is_signaled = false;
943 return EFI_EXIT(EFI_SUCCESS);
945 return EFI_EXIT(EFI_NOT_READY);
949 * efi_search_obj() - find the internal EFI object for a handle
950 * @handle: handle to find
954 struct efi_object *efi_search_obj(const efi_handle_t handle)
956 struct efi_object *efiobj;
958 list_for_each_entry(efiobj, &efi_obj_list, link) {
959 if (efiobj == handle)
967 * efi_open_protocol_info_entry() - create open protocol info entry and add it
969 * @handler: handler of a protocol
971 * Return: open protocol info entry
973 static struct efi_open_protocol_info_entry *efi_create_open_info(
974 struct efi_handler *handler)
976 struct efi_open_protocol_info_item *item;
978 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
981 /* Append the item to the open protocol info list. */
982 list_add_tail(&item->link, &handler->open_infos);
988 * efi_delete_open_info() - remove an open protocol info entry from a protocol
989 * @item: open protocol info entry to delete
991 * Return: status code
993 static efi_status_t efi_delete_open_info(
994 struct efi_open_protocol_info_item *item)
996 list_del(&item->link);
1002 * efi_add_protocol() - install new protocol on a handle
1003 * @handle: handle on which the protocol shall be installed
1004 * @protocol: GUID of the protocol to be installed
1005 * @protocol_interface: interface of the protocol implementation
1007 * Return: status code
1009 efi_status_t efi_add_protocol(const efi_handle_t handle,
1010 const efi_guid_t *protocol,
1011 void *protocol_interface)
1013 struct efi_object *efiobj;
1014 struct efi_handler *handler;
1017 efiobj = efi_search_obj(handle);
1019 return EFI_INVALID_PARAMETER;
1020 ret = efi_search_protocol(handle, protocol, NULL);
1021 if (ret != EFI_NOT_FOUND)
1022 return EFI_INVALID_PARAMETER;
1023 handler = calloc(1, sizeof(struct efi_handler));
1025 return EFI_OUT_OF_RESOURCES;
1026 handler->guid = protocol;
1027 handler->protocol_interface = protocol_interface;
1028 INIT_LIST_HEAD(&handler->open_infos);
1029 list_add_tail(&handler->link, &efiobj->protocols);
1030 if (!guidcmp(&efi_guid_device_path, protocol))
1031 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1036 * efi_install_protocol_interface() - install protocol interface
1037 * @handle: handle on which the protocol shall be installed
1038 * @protocol: GUID of the protocol to be installed
1039 * @protocol_interface_type: type of the interface to be installed,
1040 * always EFI_NATIVE_INTERFACE
1041 * @protocol_interface: interface of the protocol implementation
1043 * This function implements the InstallProtocolInterface service.
1045 * See the Unified Extensible Firmware Interface (UEFI) specification for
1048 * Return: status code
1050 static efi_status_t EFIAPI efi_install_protocol_interface(
1051 efi_handle_t *handle, const efi_guid_t *protocol,
1052 int protocol_interface_type, void *protocol_interface)
1056 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1057 protocol_interface);
1059 if (!handle || !protocol ||
1060 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1061 r = EFI_INVALID_PARAMETER;
1065 /* Create new handle if requested. */
1067 r = efi_create_handle(handle);
1068 if (r != EFI_SUCCESS)
1070 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1073 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1076 /* Add new protocol */
1077 r = efi_add_protocol(*handle, protocol, protocol_interface);
1083 * efi_get_drivers() - get all drivers associated to a controller
1084 * @handle: handle of the controller
1085 * @protocol: protocol GUID (optional)
1086 * @number_of_drivers: number of child controllers
1087 * @driver_handle_buffer: handles of the the drivers
1089 * The allocated buffer has to be freed with free().
1091 * Return: status code
1093 static efi_status_t efi_get_drivers(efi_handle_t handle,
1094 const efi_guid_t *protocol,
1095 efi_uintn_t *number_of_drivers,
1096 efi_handle_t **driver_handle_buffer)
1098 struct efi_handler *handler;
1099 struct efi_open_protocol_info_item *item;
1100 efi_uintn_t count = 0, i;
1103 /* Count all driver associations */
1104 list_for_each_entry(handler, &handle->protocols, link) {
1105 if (protocol && guidcmp(handler->guid, protocol))
1107 list_for_each_entry(item, &handler->open_infos, link) {
1108 if (item->info.attributes &
1109 EFI_OPEN_PROTOCOL_BY_DRIVER)
1114 * Create buffer. In case of duplicate driver assignments the buffer
1115 * will be too large. But that does not harm.
1117 *number_of_drivers = 0;
1118 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1119 if (!*driver_handle_buffer)
1120 return EFI_OUT_OF_RESOURCES;
1121 /* Collect unique driver handles */
1122 list_for_each_entry(handler, &handle->protocols, link) {
1123 if (protocol && guidcmp(handler->guid, protocol))
1125 list_for_each_entry(item, &handler->open_infos, link) {
1126 if (item->info.attributes &
1127 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1128 /* Check this is a new driver */
1130 for (i = 0; i < *number_of_drivers; ++i) {
1131 if ((*driver_handle_buffer)[i] ==
1132 item->info.agent_handle)
1135 /* Copy handle to buffer */
1137 i = (*number_of_drivers)++;
1138 (*driver_handle_buffer)[i] =
1139 item->info.agent_handle;
1148 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1149 * @handle: handle of the controller
1150 * @protocol: protocol GUID (optional)
1151 * @child_handle: handle of the child to destroy
1153 * This function implements the DisconnectController service.
1155 * See the Unified Extensible Firmware Interface (UEFI) specification for
1158 * Return: status code
1160 static efi_status_t efi_disconnect_all_drivers
1161 (efi_handle_t handle,
1162 const efi_guid_t *protocol,
1163 efi_handle_t child_handle)
1165 efi_uintn_t number_of_drivers;
1166 efi_handle_t *driver_handle_buffer;
1167 efi_status_t r, ret;
1169 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
1170 &driver_handle_buffer);
1171 if (ret != EFI_SUCCESS)
1174 ret = EFI_NOT_FOUND;
1175 while (number_of_drivers) {
1176 r = EFI_CALL(efi_disconnect_controller(
1178 driver_handle_buffer[--number_of_drivers],
1180 if (r == EFI_SUCCESS)
1183 free(driver_handle_buffer);
1188 * efi_uninstall_protocol() - uninstall protocol interface
1190 * @handle: handle from which the protocol shall be removed
1191 * @protocol: GUID of the protocol to be removed
1192 * @protocol_interface: interface to be removed
1194 * This function DOES NOT delete a handle without installed protocol.
1196 * Return: status code
1198 static efi_status_t efi_uninstall_protocol
1199 (efi_handle_t handle, const efi_guid_t *protocol,
1200 void *protocol_interface)
1202 struct efi_object *efiobj;
1203 struct efi_handler *handler;
1204 struct efi_open_protocol_info_item *item;
1205 struct efi_open_protocol_info_item *pos;
1209 efiobj = efi_search_obj(handle);
1211 r = EFI_INVALID_PARAMETER;
1214 /* Find the protocol on the handle */
1215 r = efi_search_protocol(handle, protocol, &handler);
1216 if (r != EFI_SUCCESS)
1218 /* Disconnect controllers */
1219 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1220 if (!list_empty(&handler->open_infos)) {
1221 r = EFI_ACCESS_DENIED;
1224 /* Close protocol */
1225 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1226 if (item->info.attributes ==
1227 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1228 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1229 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1230 list_del(&item->link);
1232 if (!list_empty(&handler->open_infos)) {
1233 r = EFI_ACCESS_DENIED;
1236 r = efi_remove_protocol(handle, protocol, protocol_interface);
1242 * efi_uninstall_protocol_interface() - uninstall protocol interface
1243 * @handle: handle from which the protocol shall be removed
1244 * @protocol: GUID of the protocol to be removed
1245 * @protocol_interface: interface to be removed
1247 * This function implements the UninstallProtocolInterface service.
1249 * See the Unified Extensible Firmware Interface (UEFI) specification for
1252 * Return: status code
1254 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1255 (efi_handle_t handle, const efi_guid_t *protocol,
1256 void *protocol_interface)
1260 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1262 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1263 if (ret != EFI_SUCCESS)
1266 /* If the last protocol has been removed, delete the handle. */
1267 if (list_empty(&handle->protocols)) {
1268 list_del(&handle->link);
1272 return EFI_EXIT(ret);
1276 * efi_register_protocol_notify() - register an event for notification when a
1277 * protocol is installed.
1278 * @protocol: GUID of the protocol whose installation shall be notified
1279 * @event: event to be signaled upon installation of the protocol
1280 * @registration: key for retrieving the registration information
1282 * This function implements the RegisterProtocolNotify service.
1283 * See the Unified Extensible Firmware Interface (UEFI) specification
1286 * Return: status code
1288 static efi_status_t EFIAPI efi_register_protocol_notify(
1289 const efi_guid_t *protocol,
1290 struct efi_event *event,
1291 void **registration)
1293 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1294 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1298 * efi_search() - determine if an EFI handle implements a protocol
1299 * @search_type: selection criterion
1300 * @protocol: GUID of the protocol
1301 * @search_key: registration key
1304 * See the documentation of the LocateHandle service in the UEFI specification.
1306 * Return: 0 if the handle implements the protocol
1308 static int efi_search(enum efi_locate_search_type search_type,
1309 const efi_guid_t *protocol, void *search_key,
1310 efi_handle_t handle)
1314 switch (search_type) {
1317 case BY_REGISTER_NOTIFY:
1318 /* TODO: RegisterProtocolNotify is not implemented yet */
1321 ret = efi_search_protocol(handle, protocol, NULL);
1322 return (ret != EFI_SUCCESS);
1324 /* Invalid search type */
1330 * efi_locate_handle() - locate handles implementing a protocol
1331 * @search_type: selection criterion
1332 * @protocol: GUID of the protocol
1333 * @search_key: registration key
1334 * @buffer_size: size of the buffer to receive the handles in bytes
1335 * @buffer: buffer to receive the relevant handles
1337 * This function is meant for U-Boot internal calls. For the API implementation
1338 * of the LocateHandle service see efi_locate_handle_ext.
1340 * Return: status code
1342 static efi_status_t efi_locate_handle(
1343 enum efi_locate_search_type search_type,
1344 const efi_guid_t *protocol, void *search_key,
1345 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1347 struct efi_object *efiobj;
1348 efi_uintn_t size = 0;
1350 /* Check parameters */
1351 switch (search_type) {
1354 case BY_REGISTER_NOTIFY:
1356 return EFI_INVALID_PARAMETER;
1357 /* RegisterProtocolNotify is not implemented yet */
1358 return EFI_UNSUPPORTED;
1361 return EFI_INVALID_PARAMETER;
1364 return EFI_INVALID_PARAMETER;
1367 /* Count how much space we need */
1368 list_for_each_entry(efiobj, &efi_obj_list, link) {
1369 if (!efi_search(search_type, protocol, search_key, efiobj))
1370 size += sizeof(void *);
1374 return EFI_NOT_FOUND;
1377 return EFI_INVALID_PARAMETER;
1379 if (*buffer_size < size) {
1380 *buffer_size = size;
1381 return EFI_BUFFER_TOO_SMALL;
1384 *buffer_size = size;
1386 /* The buffer size is sufficient but there is not buffer */
1388 return EFI_INVALID_PARAMETER;
1390 /* Then fill the array */
1391 list_for_each_entry(efiobj, &efi_obj_list, link) {
1392 if (!efi_search(search_type, protocol, search_key, efiobj))
1400 * efi_locate_handle_ext() - locate handles implementing a protocol.
1401 * @search_type: selection criterion
1402 * @protocol: GUID of the protocol
1403 * @search_key: registration key
1404 * @buffer_size: size of the buffer to receive the handles in bytes
1405 * @buffer: buffer to receive the relevant handles
1407 * This function implements the LocateHandle service.
1409 * See the Unified Extensible Firmware Interface (UEFI) specification for
1412 * Return: 0 if the handle implements the protocol
1414 static efi_status_t EFIAPI efi_locate_handle_ext(
1415 enum efi_locate_search_type search_type,
1416 const efi_guid_t *protocol, void *search_key,
1417 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1419 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1420 buffer_size, buffer);
1422 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1423 buffer_size, buffer));
1427 * efi_remove_configuration_table() - collapses configuration table entries,
1430 * @i: index of the table entry to be removed
1432 static void efi_remove_configuration_table(int i)
1434 struct efi_configuration_table *this = &systab.tables[i];
1435 struct efi_configuration_table *next = &systab.tables[i + 1];
1436 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1438 memmove(this, next, (ulong)end - (ulong)next);
1443 * efi_install_configuration_table() - adds, updates, or removes a
1444 * configuration table
1445 * @guid: GUID of the installed table
1446 * @table: table to be installed
1448 * This function is used for internal calls. For the API implementation of the
1449 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1451 * Return: status code
1453 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1456 struct efi_event *evt;
1460 return EFI_INVALID_PARAMETER;
1462 /* Check for GUID override */
1463 for (i = 0; i < systab.nr_tables; i++) {
1464 if (!guidcmp(guid, &systab.tables[i].guid)) {
1466 systab.tables[i].table = table;
1468 efi_remove_configuration_table(i);
1474 return EFI_NOT_FOUND;
1476 /* No override, check for overflow */
1477 if (i >= EFI_MAX_CONFIGURATION_TABLES)
1478 return EFI_OUT_OF_RESOURCES;
1480 /* Add a new entry */
1481 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1482 systab.tables[i].table = table;
1483 systab.nr_tables = i + 1;
1486 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1487 efi_update_table_header_crc32(&systab.hdr);
1489 /* Notify that the configuration table was changed */
1490 list_for_each_entry(evt, &efi_events, link) {
1491 if (evt->group && !guidcmp(evt->group, guid)) {
1492 efi_signal_event(evt, false);
1501 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1502 * configuration table.
1503 * @guid: GUID of the installed table
1504 * @table: table to be installed
1506 * This function implements the InstallConfigurationTable service.
1508 * See the Unified Extensible Firmware Interface (UEFI) specification for
1511 * Return: status code
1513 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1516 EFI_ENTRY("%pUl, %p", guid, table);
1517 return EFI_EXIT(efi_install_configuration_table(guid, table));
1521 * efi_setup_loaded_image() - initialize a loaded image
1523 * Initialize a loaded_image_info and loaded_image_info object with correct
1524 * protocols, boot-device, etc.
1526 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1529 * @device_path: device path of the loaded image
1530 * @file_path: file path of the loaded image
1531 * @handle_ptr: handle of the loaded image
1532 * @info_ptr: loaded image protocol
1533 * Return: status code
1535 efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1536 struct efi_device_path *file_path,
1537 struct efi_loaded_image_obj **handle_ptr,
1538 struct efi_loaded_image **info_ptr)
1541 struct efi_loaded_image *info = NULL;
1542 struct efi_loaded_image_obj *obj = NULL;
1543 struct efi_device_path *dp;
1545 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1549 info = calloc(1, sizeof(*info));
1551 return EFI_OUT_OF_RESOURCES;
1552 obj = calloc(1, sizeof(*obj));
1555 return EFI_OUT_OF_RESOURCES;
1557 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
1559 /* Add internal object to object list */
1560 efi_add_handle(&obj->header);
1562 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1563 info->file_path = file_path;
1564 info->system_table = &systab;
1567 info->device_handle = efi_dp_find_obj(device_path, NULL);
1569 dp = efi_dp_append(device_path, file_path);
1571 ret = EFI_OUT_OF_RESOURCES;
1577 ret = efi_add_protocol(&obj->header,
1578 &efi_guid_loaded_image_device_path, dp);
1579 if (ret != EFI_SUCCESS)
1583 * When asking for the loaded_image interface, just
1584 * return handle which points to loaded_image_info
1586 ret = efi_add_protocol(&obj->header,
1587 &efi_guid_loaded_image, info);
1588 if (ret != EFI_SUCCESS)
1596 printf("ERROR: Failure to install protocols for loaded image\n");
1597 efi_delete_handle(&obj->header);
1603 * efi_load_image_from_path() - load an image using a file path
1605 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1606 * callers obligation to update the memory type as needed.
1608 * @file_path: the path of the image to load
1609 * @buffer: buffer containing the loaded image
1610 * @size: size of the loaded image
1611 * Return: status code
1614 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1615 void **buffer, efi_uintn_t *size)
1617 struct efi_file_info *info = NULL;
1618 struct efi_file_handle *f;
1619 static efi_status_t ret;
1623 /* In case of failure nothing is returned */
1628 f = efi_file_from_path(file_path);
1630 return EFI_DEVICE_ERROR;
1634 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1636 if (ret != EFI_BUFFER_TOO_SMALL) {
1637 ret = EFI_DEVICE_ERROR;
1642 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1644 if (ret != EFI_SUCCESS)
1648 * When reading the file we do not yet know if it contains an
1649 * application, a boottime driver, or a runtime driver. So here we
1650 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1651 * update the reservation according to the image type.
1653 bs = info->file_size;
1654 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1655 EFI_BOOT_SERVICES_DATA,
1656 efi_size_in_pages(bs), &addr);
1657 if (ret != EFI_SUCCESS) {
1658 ret = EFI_OUT_OF_RESOURCES;
1663 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1664 if (ret != EFI_SUCCESS)
1665 efi_free_pages(addr, efi_size_in_pages(bs));
1666 *buffer = (void *)(uintptr_t)addr;
1669 EFI_CALL(f->close(f));
1675 * efi_load_image() - load an EFI image into memory
1676 * @boot_policy: true for request originating from the boot manager
1677 * @parent_image: the caller's image handle
1678 * @file_path: the path of the image to load
1679 * @source_buffer: memory location from which the image is installed
1680 * @source_size: size of the memory area from which the image is installed
1681 * @image_handle: handle for the newly installed image
1683 * This function implements the LoadImage service.
1685 * See the Unified Extensible Firmware Interface (UEFI) specification
1688 * Return: status code
1690 efi_status_t EFIAPI efi_load_image(bool boot_policy,
1691 efi_handle_t parent_image,
1692 struct efi_device_path *file_path,
1693 void *source_buffer,
1694 efi_uintn_t source_size,
1695 efi_handle_t *image_handle)
1697 struct efi_device_path *dp, *fp;
1698 struct efi_loaded_image *info = NULL;
1699 struct efi_loaded_image_obj **image_obj =
1700 (struct efi_loaded_image_obj **)image_handle;
1704 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1705 file_path, source_buffer, source_size, image_handle);
1707 if (!image_handle || !parent_image) {
1708 ret = EFI_INVALID_PARAMETER;
1712 if (!source_buffer && !file_path) {
1713 ret = EFI_NOT_FOUND;
1717 if (!source_buffer) {
1718 ret = efi_load_image_from_path(file_path, &dest_buffer,
1720 if (ret != EFI_SUCCESS)
1723 dest_buffer = source_buffer;
1725 /* split file_path which contains both the device and file parts */
1726 efi_dp_split_file_path(file_path, &dp, &fp);
1727 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1728 if (ret == EFI_SUCCESS)
1729 ret = efi_load_pe(*image_obj, dest_buffer, info);
1731 /* Release buffer to which file was loaded */
1732 efi_free_pages((uintptr_t)dest_buffer,
1733 efi_size_in_pages(source_size));
1734 if (ret == EFI_SUCCESS) {
1735 info->system_table = &systab;
1736 info->parent_handle = parent_image;
1738 /* The image is invalid. Release all associated resources. */
1739 efi_delete_handle(*image_handle);
1740 *image_handle = NULL;
1744 return EFI_EXIT(ret);
1748 * efi_unload_image() - unload an EFI image
1749 * @image_handle: handle of the image to be unloaded
1751 * This function implements the UnloadImage service.
1753 * See the Unified Extensible Firmware Interface (UEFI) specification for
1756 * Return: status code
1758 efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1760 struct efi_object *efiobj;
1762 EFI_ENTRY("%p", image_handle);
1763 efiobj = efi_search_obj(image_handle);
1765 list_del(&efiobj->link);
1767 return EFI_EXIT(EFI_SUCCESS);
1771 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1773 static void efi_exit_caches(void)
1775 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1777 * Grub on 32bit ARM needs to have caches disabled before jumping into
1778 * a zImage, but does not know of all cache layers. Give it a hand.
1780 if (efi_is_direct_boot)
1781 cleanup_before_linux();
1786 * efi_exit_boot_services() - stop all boot services
1787 * @image_handle: handle of the loaded image
1788 * @map_key: key of the memory map
1790 * This function implements the ExitBootServices service.
1792 * See the Unified Extensible Firmware Interface (UEFI) specification
1795 * All timer events are disabled. For exit boot services events the
1796 * notification function is called. The boot services are disabled in the
1799 * Return: status code
1801 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1802 unsigned long map_key)
1804 struct efi_event *evt;
1806 EFI_ENTRY("%p, %ld", image_handle, map_key);
1808 /* Check that the caller has read the current memory map */
1809 if (map_key != efi_memory_map_key)
1810 return EFI_INVALID_PARAMETER;
1812 /* Make sure that notification functions are not called anymore */
1813 efi_tpl = TPL_HIGH_LEVEL;
1815 /* Check if ExitBootServices has already been called */
1816 if (!systab.boottime)
1817 return EFI_EXIT(EFI_SUCCESS);
1819 /* Add related events to the event group */
1820 list_for_each_entry(evt, &efi_events, link) {
1821 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1822 evt->group = &efi_guid_event_group_exit_boot_services;
1824 /* Notify that ExitBootServices is invoked. */
1825 list_for_each_entry(evt, &efi_events, link) {
1827 !guidcmp(evt->group,
1828 &efi_guid_event_group_exit_boot_services)) {
1829 efi_signal_event(evt, false);
1834 /* TODO: Should persist EFI variables here */
1836 board_quiesce_devices();
1838 /* Fix up caches for EFI payloads if necessary */
1841 /* This stops all lingering devices */
1842 bootm_disable_interrupts();
1844 /* Disable boot time services */
1845 systab.con_in_handle = NULL;
1846 systab.con_in = NULL;
1847 systab.con_out_handle = NULL;
1848 systab.con_out = NULL;
1849 systab.stderr_handle = NULL;
1850 systab.std_err = NULL;
1851 systab.boottime = NULL;
1853 /* Recalculate CRC32 */
1854 efi_update_table_header_crc32(&systab.hdr);
1856 /* Give the payload some time to boot */
1857 efi_set_watchdog(0);
1860 return EFI_EXIT(EFI_SUCCESS);
1864 * efi_get_next_monotonic_count() - get next value of the counter
1865 * @count: returned value of the counter
1867 * This function implements the NextMonotonicCount service.
1869 * See the Unified Extensible Firmware Interface (UEFI) specification for
1872 * Return: status code
1874 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1876 static uint64_t mono;
1878 EFI_ENTRY("%p", count);
1880 return EFI_EXIT(EFI_SUCCESS);
1884 * efi_stall() - sleep
1885 * @microseconds: period to sleep in microseconds
1887 * This function implements the Stall service.
1889 * See the Unified Extensible Firmware Interface (UEFI) specification for
1892 * Return: status code
1894 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1896 EFI_ENTRY("%ld", microseconds);
1897 udelay(microseconds);
1898 return EFI_EXIT(EFI_SUCCESS);
1902 * efi_set_watchdog_timer() - reset the watchdog timer
1903 * @timeout: seconds before reset by watchdog
1904 * @watchdog_code: code to be logged when resetting
1905 * @data_size: size of buffer in bytes
1906 * @watchdog_data: buffer with data describing the reset reason
1908 * This function implements the SetWatchdogTimer service.
1910 * See the Unified Extensible Firmware Interface (UEFI) specification for
1913 * Return: status code
1915 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1916 uint64_t watchdog_code,
1917 unsigned long data_size,
1918 uint16_t *watchdog_data)
1920 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
1921 data_size, watchdog_data);
1922 return EFI_EXIT(efi_set_watchdog(timeout));
1926 * efi_close_protocol() - close a protocol
1927 * @handle: handle on which the protocol shall be closed
1928 * @protocol: GUID of the protocol to close
1929 * @agent_handle: handle of the driver
1930 * @controller_handle: handle of the controller
1932 * This function implements the CloseProtocol service.
1934 * See the Unified Extensible Firmware Interface (UEFI) specification for
1937 * Return: status code
1939 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1940 const efi_guid_t *protocol,
1941 efi_handle_t agent_handle,
1942 efi_handle_t controller_handle)
1944 struct efi_handler *handler;
1945 struct efi_open_protocol_info_item *item;
1946 struct efi_open_protocol_info_item *pos;
1949 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1952 if (!agent_handle) {
1953 r = EFI_INVALID_PARAMETER;
1956 r = efi_search_protocol(handle, protocol, &handler);
1957 if (r != EFI_SUCCESS)
1961 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1962 if (item->info.agent_handle == agent_handle &&
1963 item->info.controller_handle == controller_handle) {
1964 efi_delete_open_info(item);
1974 * efi_open_protocol_information() - provide information about then open status
1975 * of a protocol on a handle
1976 * @handle: handle for which the information shall be retrieved
1977 * @protocol: GUID of the protocol
1978 * @entry_buffer: buffer to receive the open protocol information
1979 * @entry_count: number of entries available in the buffer
1981 * This function implements the OpenProtocolInformation service.
1983 * See the Unified Extensible Firmware Interface (UEFI) specification for
1986 * Return: status code
1988 static efi_status_t EFIAPI efi_open_protocol_information(
1989 efi_handle_t handle, const efi_guid_t *protocol,
1990 struct efi_open_protocol_info_entry **entry_buffer,
1991 efi_uintn_t *entry_count)
1993 unsigned long buffer_size;
1994 unsigned long count;
1995 struct efi_handler *handler;
1996 struct efi_open_protocol_info_item *item;
1999 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2002 /* Check parameters */
2003 if (!entry_buffer) {
2004 r = EFI_INVALID_PARAMETER;
2007 r = efi_search_protocol(handle, protocol, &handler);
2008 if (r != EFI_SUCCESS)
2013 list_for_each_entry(item, &handler->open_infos, link) {
2014 if (item->info.open_count)
2017 *entry_count = count;
2018 *entry_buffer = NULL;
2025 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2026 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2027 (void **)entry_buffer);
2028 if (r != EFI_SUCCESS)
2030 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2031 if (item->info.open_count)
2032 (*entry_buffer)[--count] = item->info;
2039 * efi_protocols_per_handle() - get protocols installed on a handle
2040 * @handle: handle for which the information is retrieved
2041 * @protocol_buffer: buffer with protocol GUIDs
2042 * @protocol_buffer_count: number of entries in the buffer
2044 * This function implements the ProtocolsPerHandleService.
2046 * See the Unified Extensible Firmware Interface (UEFI) specification for
2049 * Return: status code
2051 static efi_status_t EFIAPI efi_protocols_per_handle(
2052 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2053 efi_uintn_t *protocol_buffer_count)
2055 unsigned long buffer_size;
2056 struct efi_object *efiobj;
2057 struct list_head *protocol_handle;
2060 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2061 protocol_buffer_count);
2063 if (!handle || !protocol_buffer || !protocol_buffer_count)
2064 return EFI_EXIT(EFI_INVALID_PARAMETER);
2066 *protocol_buffer = NULL;
2067 *protocol_buffer_count = 0;
2069 efiobj = efi_search_obj(handle);
2071 return EFI_EXIT(EFI_INVALID_PARAMETER);
2073 /* Count protocols */
2074 list_for_each(protocol_handle, &efiobj->protocols) {
2075 ++*protocol_buffer_count;
2079 if (*protocol_buffer_count) {
2082 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2083 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2084 (void **)protocol_buffer);
2085 if (r != EFI_SUCCESS)
2087 list_for_each(protocol_handle, &efiobj->protocols) {
2088 struct efi_handler *protocol;
2090 protocol = list_entry(protocol_handle,
2091 struct efi_handler, link);
2092 (*protocol_buffer)[j] = (void *)protocol->guid;
2097 return EFI_EXIT(EFI_SUCCESS);
2101 * efi_locate_handle_buffer() - locate handles implementing a protocol
2102 * @search_type: selection criterion
2103 * @protocol: GUID of the protocol
2104 * @search_key: registration key
2105 * @no_handles: number of returned handles
2106 * @buffer: buffer with the returned handles
2108 * This function implements the LocateHandleBuffer service.
2110 * See the Unified Extensible Firmware Interface (UEFI) specification for
2113 * Return: status code
2115 static efi_status_t EFIAPI efi_locate_handle_buffer(
2116 enum efi_locate_search_type search_type,
2117 const efi_guid_t *protocol, void *search_key,
2118 efi_uintn_t *no_handles, efi_handle_t **buffer)
2121 efi_uintn_t buffer_size = 0;
2123 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2124 no_handles, buffer);
2126 if (!no_handles || !buffer) {
2127 r = EFI_INVALID_PARAMETER;
2132 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2134 if (r != EFI_BUFFER_TOO_SMALL)
2136 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2138 if (r != EFI_SUCCESS)
2140 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2142 if (r == EFI_SUCCESS)
2143 *no_handles = buffer_size / sizeof(efi_handle_t);
2149 * efi_locate_protocol() - find an interface implementing a protocol
2150 * @protocol: GUID of the protocol
2151 * @registration: registration key passed to the notification function
2152 * @protocol_interface: interface implementing the protocol
2154 * This function implements the LocateProtocol service.
2156 * See the Unified Extensible Firmware Interface (UEFI) specification for
2159 * Return: status code
2161 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2163 void **protocol_interface)
2165 struct list_head *lhandle;
2168 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2170 if (!protocol || !protocol_interface)
2171 return EFI_EXIT(EFI_INVALID_PARAMETER);
2173 list_for_each(lhandle, &efi_obj_list) {
2174 struct efi_object *efiobj;
2175 struct efi_handler *handler;
2177 efiobj = list_entry(lhandle, struct efi_object, link);
2179 ret = efi_search_protocol(efiobj, protocol, &handler);
2180 if (ret == EFI_SUCCESS) {
2181 *protocol_interface = handler->protocol_interface;
2182 return EFI_EXIT(EFI_SUCCESS);
2185 *protocol_interface = NULL;
2187 return EFI_EXIT(EFI_NOT_FOUND);
2191 * efi_locate_device_path() - Get the device path and handle of an device
2192 * implementing a protocol
2193 * @protocol: GUID of the protocol
2194 * @device_path: device path
2195 * @device: handle of the device
2197 * This function implements the LocateDevicePath service.
2199 * See the Unified Extensible Firmware Interface (UEFI) specification for
2202 * Return: status code
2204 static efi_status_t EFIAPI efi_locate_device_path(
2205 const efi_guid_t *protocol,
2206 struct efi_device_path **device_path,
2207 efi_handle_t *device)
2209 struct efi_device_path *dp;
2211 struct efi_handler *handler;
2212 efi_handle_t *handles;
2214 size_t len_best = 0;
2215 efi_uintn_t no_handles;
2219 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2221 if (!protocol || !device_path || !*device_path || !device) {
2222 ret = EFI_INVALID_PARAMETER;
2226 /* Find end of device path */
2227 len = efi_dp_instance_size(*device_path);
2229 /* Get all handles implementing the protocol */
2230 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2231 &no_handles, &handles));
2232 if (ret != EFI_SUCCESS)
2235 for (i = 0; i < no_handles; ++i) {
2236 /* Find the device path protocol */
2237 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2239 if (ret != EFI_SUCCESS)
2241 dp = (struct efi_device_path *)handler->protocol_interface;
2242 len_dp = efi_dp_instance_size(dp);
2244 * This handle can only be a better fit
2245 * if its device path length is longer than the best fit and
2246 * if its device path length is shorter of equal the searched
2249 if (len_dp <= len_best || len_dp > len)
2251 /* Check if dp is a subpath of device_path */
2252 if (memcmp(*device_path, dp, len_dp))
2254 *device = handles[i];
2258 remainder = (u8 *)*device_path + len_best;
2259 *device_path = (struct efi_device_path *)remainder;
2262 ret = EFI_NOT_FOUND;
2265 return EFI_EXIT(ret);
2269 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2271 * @handle: handle on which the protocol interfaces shall be installed
2272 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2275 * This function implements the MultipleProtocolInterfaces service.
2277 * See the Unified Extensible Firmware Interface (UEFI) specification for
2280 * Return: status code
2282 efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2283 (efi_handle_t *handle, ...)
2285 EFI_ENTRY("%p", handle);
2288 const efi_guid_t *protocol;
2289 void *protocol_interface;
2290 efi_status_t r = EFI_SUCCESS;
2294 return EFI_EXIT(EFI_INVALID_PARAMETER);
2296 efi_va_start(argptr, handle);
2298 protocol = efi_va_arg(argptr, efi_guid_t*);
2301 protocol_interface = efi_va_arg(argptr, void*);
2302 r = EFI_CALL(efi_install_protocol_interface(
2304 EFI_NATIVE_INTERFACE,
2305 protocol_interface));
2306 if (r != EFI_SUCCESS)
2311 if (r == EFI_SUCCESS)
2314 /* If an error occurred undo all changes. */
2315 efi_va_start(argptr, handle);
2317 protocol = efi_va_arg(argptr, efi_guid_t*);
2318 protocol_interface = efi_va_arg(argptr, void*);
2319 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2320 protocol_interface));
2328 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2330 * @handle: handle from which the protocol interfaces shall be removed
2331 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2334 * This function implements the UninstallMultipleProtocolInterfaces service.
2336 * See the Unified Extensible Firmware Interface (UEFI) specification for
2339 * Return: status code
2341 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2342 efi_handle_t handle, ...)
2344 EFI_ENTRY("%p", handle);
2347 const efi_guid_t *protocol;
2348 void *protocol_interface;
2349 efi_status_t r = EFI_SUCCESS;
2353 return EFI_EXIT(EFI_INVALID_PARAMETER);
2355 efi_va_start(argptr, handle);
2357 protocol = efi_va_arg(argptr, efi_guid_t*);
2360 protocol_interface = efi_va_arg(argptr, void*);
2361 r = efi_uninstall_protocol(handle, protocol,
2362 protocol_interface);
2363 if (r != EFI_SUCCESS)
2368 if (r == EFI_SUCCESS) {
2369 /* If the last protocol has been removed, delete the handle. */
2370 if (list_empty(&handle->protocols)) {
2371 list_del(&handle->link);
2377 /* If an error occurred undo all changes. */
2378 efi_va_start(argptr, handle);
2380 protocol = efi_va_arg(argptr, efi_guid_t*);
2381 protocol_interface = efi_va_arg(argptr, void*);
2382 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2383 EFI_NATIVE_INTERFACE,
2384 protocol_interface));
2388 /* In case of an error always return EFI_INVALID_PARAMETER */
2389 return EFI_EXIT(EFI_INVALID_PARAMETER);
2393 * efi_calculate_crc32() - calculate cyclic redundancy code
2394 * @data: buffer with data
2395 * @data_size: size of buffer in bytes
2396 * @crc32_p: cyclic redundancy code
2398 * This function implements the CalculateCrc32 service.
2400 * See the Unified Extensible Firmware Interface (UEFI) specification for
2403 * Return: status code
2405 static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2406 efi_uintn_t data_size,
2409 EFI_ENTRY("%p, %zu", data, data_size);
2410 *crc32_p = crc32(0, data, data_size);
2411 return EFI_EXIT(EFI_SUCCESS);
2415 * efi_copy_mem() - copy memory
2416 * @destination: destination of the copy operation
2417 * @source: source of the copy operation
2418 * @length: number of bytes to copy
2420 * This function implements the CopyMem service.
2422 * See the Unified Extensible Firmware Interface (UEFI) specification for
2425 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2428 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2429 memmove(destination, source, length);
2430 EFI_EXIT(EFI_SUCCESS);
2434 * efi_set_mem() - Fill memory with a byte value.
2435 * @buffer: buffer to fill
2436 * @size: size of buffer in bytes
2437 * @value: byte to copy to the buffer
2439 * This function implements the SetMem service.
2441 * See the Unified Extensible Firmware Interface (UEFI) specification for
2444 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2446 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2447 memset(buffer, value, size);
2448 EFI_EXIT(EFI_SUCCESS);
2452 * efi_protocol_open() - open protocol interface on a handle
2453 * @handler: handler of a protocol
2454 * @protocol_interface: interface implementing the protocol
2455 * @agent_handle: handle of the driver
2456 * @controller_handle: handle of the controller
2457 * @attributes: attributes indicating how to open the protocol
2459 * Return: status code
2461 static efi_status_t efi_protocol_open(
2462 struct efi_handler *handler,
2463 void **protocol_interface, void *agent_handle,
2464 void *controller_handle, uint32_t attributes)
2466 struct efi_open_protocol_info_item *item;
2467 struct efi_open_protocol_info_entry *match = NULL;
2468 bool opened_by_driver = false;
2469 bool opened_exclusive = false;
2471 /* If there is no agent, only return the interface */
2475 /* For TEST_PROTOCOL ignore interface attribute */
2476 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2477 *protocol_interface = NULL;
2480 * Check if the protocol is already opened by a driver with the same
2481 * attributes or opened exclusively
2483 list_for_each_entry(item, &handler->open_infos, link) {
2484 if (item->info.agent_handle == agent_handle) {
2485 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2486 (item->info.attributes == attributes))
2487 return EFI_ALREADY_STARTED;
2489 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2490 opened_exclusive = true;
2493 /* Only one controller can open the protocol exclusively */
2494 if (opened_exclusive && attributes &
2495 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2496 return EFI_ACCESS_DENIED;
2498 /* Prepare exclusive opening */
2499 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2500 /* Try to disconnect controllers */
2501 list_for_each_entry(item, &handler->open_infos, link) {
2502 if (item->info.attributes ==
2503 EFI_OPEN_PROTOCOL_BY_DRIVER)
2504 EFI_CALL(efi_disconnect_controller(
2505 item->info.controller_handle,
2506 item->info.agent_handle,
2509 opened_by_driver = false;
2510 /* Check if all controllers are disconnected */
2511 list_for_each_entry(item, &handler->open_infos, link) {
2512 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2513 opened_by_driver = true;
2515 /* Only one controller can be connected */
2516 if (opened_by_driver)
2517 return EFI_ACCESS_DENIED;
2520 /* Find existing entry */
2521 list_for_each_entry(item, &handler->open_infos, link) {
2522 if (item->info.agent_handle == agent_handle &&
2523 item->info.controller_handle == controller_handle)
2524 match = &item->info;
2526 /* None found, create one */
2528 match = efi_create_open_info(handler);
2530 return EFI_OUT_OF_RESOURCES;
2533 match->agent_handle = agent_handle;
2534 match->controller_handle = controller_handle;
2535 match->attributes = attributes;
2536 match->open_count++;
2539 /* For TEST_PROTOCOL ignore interface attribute. */
2540 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2541 *protocol_interface = handler->protocol_interface;
2547 * efi_open_protocol() - open protocol interface on a handle
2548 * @handle: handle on which the protocol shall be opened
2549 * @protocol: GUID of the protocol
2550 * @protocol_interface: interface implementing the protocol
2551 * @agent_handle: handle of the driver
2552 * @controller_handle: handle of the controller
2553 * @attributes: attributes indicating how to open the protocol
2555 * This function implements the OpenProtocol interface.
2557 * See the Unified Extensible Firmware Interface (UEFI) specification for
2560 * Return: status code
2562 static efi_status_t EFIAPI efi_open_protocol
2563 (efi_handle_t handle, const efi_guid_t *protocol,
2564 void **protocol_interface, efi_handle_t agent_handle,
2565 efi_handle_t controller_handle, uint32_t attributes)
2567 struct efi_handler *handler;
2568 efi_status_t r = EFI_INVALID_PARAMETER;
2570 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2571 protocol_interface, agent_handle, controller_handle,
2574 if (!handle || !protocol ||
2575 (!protocol_interface && attributes !=
2576 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2580 switch (attributes) {
2581 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2582 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2583 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2585 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2586 if (controller_handle == handle)
2589 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2590 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2591 /* Check that the controller handle is valid */
2592 if (!efi_search_obj(controller_handle))
2595 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2596 /* Check that the agent handle is valid */
2597 if (!efi_search_obj(agent_handle))
2604 r = efi_search_protocol(handle, protocol, &handler);
2605 if (r != EFI_SUCCESS)
2608 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2609 controller_handle, attributes);
2615 * efi_start_image() - call the entry point of an image
2616 * @image_handle: handle of the image
2617 * @exit_data_size: size of the buffer
2618 * @exit_data: buffer to receive the exit data of the called image
2620 * This function implements the StartImage service.
2622 * See the Unified Extensible Firmware Interface (UEFI) specification for
2625 * Return: status code
2627 efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2628 efi_uintn_t *exit_data_size,
2631 struct efi_loaded_image_obj *image_obj =
2632 (struct efi_loaded_image_obj *)image_handle;
2635 efi_handle_t parent_image = current_image;
2637 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2639 /* Check parameters */
2640 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2642 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2643 if (ret != EFI_SUCCESS)
2644 return EFI_EXIT(EFI_INVALID_PARAMETER);
2646 efi_is_direct_boot = false;
2648 image_obj->exit_data_size = exit_data_size;
2649 image_obj->exit_data = exit_data;
2651 /* call the image! */
2652 if (setjmp(&image_obj->exit_jmp)) {
2654 * We called the entry point of the child image with EFI_CALL
2655 * in the lines below. The child image called the Exit() boot
2656 * service efi_exit() which executed the long jump that brought
2657 * us to the current line. This implies that the second half
2658 * of the EFI_CALL macro has not been executed.
2662 * efi_exit() called efi_restore_gd(). We have to undo this
2663 * otherwise __efi_entry_check() will put the wrong value into
2669 * To get ready to call EFI_EXIT below we have to execute the
2670 * missed out steps of EFI_CALL.
2672 assert(__efi_entry_check());
2673 debug("%sEFI: %lu returned by started image\n",
2674 __efi_nesting_dec(),
2675 (unsigned long)((uintptr_t)image_obj->exit_status &
2677 current_image = parent_image;
2678 return EFI_EXIT(image_obj->exit_status);
2681 current_image = image_handle;
2682 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
2683 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
2684 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2687 * Usually UEFI applications call Exit() instead of returning.
2688 * But because the world doesn't consist of ponies and unicorns,
2689 * we're happy to emulate that behavior on behalf of a payload
2692 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2696 * efi_update_exit_data() - fill exit data parameters of StartImage()
2698 * @image_obj image handle
2699 * @exit_data_size size of the exit data buffer
2700 * @exit_data buffer with data returned by UEFI payload
2701 * Return: status code
2703 static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
2704 efi_uintn_t exit_data_size,
2710 * If exit_data is not provided to StartImage(), exit_data_size must be
2713 if (!image_obj->exit_data)
2715 if (image_obj->exit_data_size)
2716 *image_obj->exit_data_size = exit_data_size;
2717 if (exit_data_size && exit_data) {
2718 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
2720 (void **)image_obj->exit_data);
2721 if (ret != EFI_SUCCESS)
2723 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
2725 image_obj->exit_data = NULL;
2731 * efi_exit() - leave an EFI application or driver
2732 * @image_handle: handle of the application or driver that is exiting
2733 * @exit_status: status code
2734 * @exit_data_size: size of the buffer in bytes
2735 * @exit_data: buffer with data describing an error
2737 * This function implements the Exit service.
2739 * See the Unified Extensible Firmware Interface (UEFI) specification for
2742 * Return: status code
2744 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
2745 efi_status_t exit_status,
2746 efi_uintn_t exit_data_size,
2750 * TODO: We should call the unload procedure of the loaded
2755 struct efi_loaded_image_obj *image_obj =
2756 (struct efi_loaded_image_obj *)image_handle;
2758 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
2759 exit_data_size, exit_data);
2761 /* Check parameters */
2762 if (image_handle != current_image)
2764 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2766 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2767 if (ret != EFI_SUCCESS)
2770 /* Exit data is only foreseen in case of failure. */
2771 if (exit_status != EFI_SUCCESS) {
2772 ret = efi_update_exit_data(image_obj, exit_data_size,
2774 /* Exiting has priority. Don't return error to caller. */
2775 if (ret != EFI_SUCCESS)
2776 EFI_PRINT("%s: out of memory\n", __func__);
2779 /* Make sure entry/exit counts for EFI world cross-overs match */
2780 EFI_EXIT(exit_status);
2783 * But longjmp out with the U-Boot gd, not the application's, as
2784 * the other end is a setjmp call inside EFI context.
2788 image_obj->exit_status = exit_status;
2789 longjmp(&image_obj->exit_jmp, 1);
2791 panic("EFI application exited");
2793 return EFI_EXIT(EFI_INVALID_PARAMETER);
2797 * efi_handle_protocol() - get interface of a protocol on a handle
2798 * @handle: handle on which the protocol shall be opened
2799 * @protocol: GUID of the protocol
2800 * @protocol_interface: interface implementing the protocol
2802 * This function implements the HandleProtocol service.
2804 * See the Unified Extensible Firmware Interface (UEFI) specification for
2807 * Return: status code
2809 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2810 const efi_guid_t *protocol,
2811 void **protocol_interface)
2813 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2814 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2818 * efi_bind_controller() - bind a single driver to a controller
2819 * @controller_handle: controller handle
2820 * @driver_image_handle: driver handle
2821 * @remain_device_path: remaining path
2823 * Return: status code
2825 static efi_status_t efi_bind_controller(
2826 efi_handle_t controller_handle,
2827 efi_handle_t driver_image_handle,
2828 struct efi_device_path *remain_device_path)
2830 struct efi_driver_binding_protocol *binding_protocol;
2833 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2834 &efi_guid_driver_binding_protocol,
2835 (void **)&binding_protocol,
2836 driver_image_handle, NULL,
2837 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2838 if (r != EFI_SUCCESS)
2840 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2842 remain_device_path));
2843 if (r == EFI_SUCCESS)
2844 r = EFI_CALL(binding_protocol->start(binding_protocol,
2846 remain_device_path));
2847 EFI_CALL(efi_close_protocol(driver_image_handle,
2848 &efi_guid_driver_binding_protocol,
2849 driver_image_handle, NULL));
2854 * efi_connect_single_controller() - connect a single driver to a controller
2855 * @controller_handle: controller
2856 * @driver_image_handle: driver
2857 * @remain_device_path: remaining path
2859 * Return: status code
2861 static efi_status_t efi_connect_single_controller(
2862 efi_handle_t controller_handle,
2863 efi_handle_t *driver_image_handle,
2864 struct efi_device_path *remain_device_path)
2866 efi_handle_t *buffer;
2870 size_t connected = 0;
2872 /* Get buffer with all handles with driver binding protocol */
2873 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2874 &efi_guid_driver_binding_protocol,
2875 NULL, &count, &buffer));
2876 if (r != EFI_SUCCESS)
2879 /* Context Override */
2880 if (driver_image_handle) {
2881 for (; *driver_image_handle; ++driver_image_handle) {
2882 for (i = 0; i < count; ++i) {
2883 if (buffer[i] == *driver_image_handle) {
2885 r = efi_bind_controller(
2887 *driver_image_handle,
2888 remain_device_path);
2890 * For drivers that do not support the
2891 * controller or are already connected
2892 * we receive an error code here.
2894 if (r == EFI_SUCCESS)
2902 * TODO: Some overrides are not yet implemented:
2903 * - Platform Driver Override
2904 * - Driver Family Override Search
2905 * - Bus Specific Driver Override
2908 /* Driver Binding Search */
2909 for (i = 0; i < count; ++i) {
2911 r = efi_bind_controller(controller_handle,
2913 remain_device_path);
2914 if (r == EFI_SUCCESS)
2919 efi_free_pool(buffer);
2921 return EFI_NOT_FOUND;
2926 * efi_connect_controller() - connect a controller to a driver
2927 * @controller_handle: handle of the controller
2928 * @driver_image_handle: handle of the driver
2929 * @remain_device_path: device path of a child controller
2930 * @recursive: true to connect all child controllers
2932 * This function implements the ConnectController service.
2934 * See the Unified Extensible Firmware Interface (UEFI) specification for
2937 * First all driver binding protocol handles are tried for binding drivers.
2938 * Afterwards all handles that have opened a protocol of the controller
2939 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2941 * Return: status code
2943 static efi_status_t EFIAPI efi_connect_controller(
2944 efi_handle_t controller_handle,
2945 efi_handle_t *driver_image_handle,
2946 struct efi_device_path *remain_device_path,
2950 efi_status_t ret = EFI_NOT_FOUND;
2951 struct efi_object *efiobj;
2953 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
2954 remain_device_path, recursive);
2956 efiobj = efi_search_obj(controller_handle);
2958 ret = EFI_INVALID_PARAMETER;
2962 r = efi_connect_single_controller(controller_handle,
2963 driver_image_handle,
2964 remain_device_path);
2965 if (r == EFI_SUCCESS)
2968 struct efi_handler *handler;
2969 struct efi_open_protocol_info_item *item;
2971 list_for_each_entry(handler, &efiobj->protocols, link) {
2972 list_for_each_entry(item, &handler->open_infos, link) {
2973 if (item->info.attributes &
2974 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2975 r = EFI_CALL(efi_connect_controller(
2976 item->info.controller_handle,
2977 driver_image_handle,
2980 if (r == EFI_SUCCESS)
2986 /* Check for child controller specified by end node */
2987 if (ret != EFI_SUCCESS && remain_device_path &&
2988 remain_device_path->type == DEVICE_PATH_TYPE_END)
2991 return EFI_EXIT(ret);
2995 * efi_reinstall_protocol_interface() - reinstall protocol interface
2996 * @handle: handle on which the protocol shall be reinstalled
2997 * @protocol: GUID of the protocol to be installed
2998 * @old_interface: interface to be removed
2999 * @new_interface: interface to be installed
3001 * This function implements the ReinstallProtocolInterface service.
3003 * See the Unified Extensible Firmware Interface (UEFI) specification for
3006 * The old interface is uninstalled. The new interface is installed.
3007 * Drivers are connected.
3009 * Return: status code
3011 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3012 efi_handle_t handle, const efi_guid_t *protocol,
3013 void *old_interface, void *new_interface)
3017 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3020 /* Uninstall protocol but do not delete handle */
3021 ret = efi_uninstall_protocol(handle, protocol, old_interface);
3022 if (ret != EFI_SUCCESS)
3025 /* Install the new protocol */
3026 ret = efi_add_protocol(handle, protocol, new_interface);
3028 * The UEFI spec does not specify what should happen to the handle
3029 * if in case of an error no protocol interface remains on the handle.
3030 * So let's do nothing here.
3032 if (ret != EFI_SUCCESS)
3035 * The returned status code has to be ignored.
3036 * Do not create an error if no suitable driver for the handle exists.
3038 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3040 return EFI_EXIT(ret);
3044 * efi_get_child_controllers() - get all child controllers associated to a driver
3045 * @efiobj: handle of the controller
3046 * @driver_handle: handle of the driver
3047 * @number_of_children: number of child controllers
3048 * @child_handle_buffer: handles of the the child controllers
3050 * The allocated buffer has to be freed with free().
3052 * Return: status code
3054 static efi_status_t efi_get_child_controllers(
3055 struct efi_object *efiobj,
3056 efi_handle_t driver_handle,
3057 efi_uintn_t *number_of_children,
3058 efi_handle_t **child_handle_buffer)
3060 struct efi_handler *handler;
3061 struct efi_open_protocol_info_item *item;
3062 efi_uintn_t count = 0, i;
3065 /* Count all child controller associations */
3066 list_for_each_entry(handler, &efiobj->protocols, link) {
3067 list_for_each_entry(item, &handler->open_infos, link) {
3068 if (item->info.agent_handle == driver_handle &&
3069 item->info.attributes &
3070 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3075 * Create buffer. In case of duplicate child controller assignments
3076 * the buffer will be too large. But that does not harm.
3078 *number_of_children = 0;
3079 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3080 if (!*child_handle_buffer)
3081 return EFI_OUT_OF_RESOURCES;
3082 /* Copy unique child handles */
3083 list_for_each_entry(handler, &efiobj->protocols, link) {
3084 list_for_each_entry(item, &handler->open_infos, link) {
3085 if (item->info.agent_handle == driver_handle &&
3086 item->info.attributes &
3087 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3088 /* Check this is a new child controller */
3090 for (i = 0; i < *number_of_children; ++i) {
3091 if ((*child_handle_buffer)[i] ==
3092 item->info.controller_handle)
3095 /* Copy handle to buffer */
3097 i = (*number_of_children)++;
3098 (*child_handle_buffer)[i] =
3099 item->info.controller_handle;
3108 * efi_disconnect_controller() - disconnect a controller from a driver
3109 * @controller_handle: handle of the controller
3110 * @driver_image_handle: handle of the driver
3111 * @child_handle: handle of the child to destroy
3113 * This function implements the DisconnectController service.
3115 * See the Unified Extensible Firmware Interface (UEFI) specification for
3118 * Return: status code
3120 static efi_status_t EFIAPI efi_disconnect_controller(
3121 efi_handle_t controller_handle,
3122 efi_handle_t driver_image_handle,
3123 efi_handle_t child_handle)
3125 struct efi_driver_binding_protocol *binding_protocol;
3126 efi_handle_t *child_handle_buffer = NULL;
3127 size_t number_of_children = 0;
3129 size_t stop_count = 0;
3130 struct efi_object *efiobj;
3132 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3135 efiobj = efi_search_obj(controller_handle);
3137 r = EFI_INVALID_PARAMETER;
3141 if (child_handle && !efi_search_obj(child_handle)) {
3142 r = EFI_INVALID_PARAMETER;
3146 /* If no driver handle is supplied, disconnect all drivers */
3147 if (!driver_image_handle) {
3148 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3152 /* Create list of child handles */
3154 number_of_children = 1;
3155 child_handle_buffer = &child_handle;
3157 efi_get_child_controllers(efiobj,
3158 driver_image_handle,
3159 &number_of_children,
3160 &child_handle_buffer);
3163 /* Get the driver binding protocol */
3164 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3165 &efi_guid_driver_binding_protocol,
3166 (void **)&binding_protocol,
3167 driver_image_handle, NULL,
3168 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3169 if (r != EFI_SUCCESS)
3171 /* Remove the children */
3172 if (number_of_children) {
3173 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3176 child_handle_buffer));
3177 if (r == EFI_SUCCESS)
3180 /* Remove the driver */
3182 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3185 if (r == EFI_SUCCESS)
3187 EFI_CALL(efi_close_protocol(driver_image_handle,
3188 &efi_guid_driver_binding_protocol,
3189 driver_image_handle, NULL));
3197 free(child_handle_buffer);
3201 static struct efi_boot_services efi_boot_services = {
3203 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3204 .revision = EFI_SPECIFICATION_VERSION,
3205 .headersize = sizeof(struct efi_boot_services),
3207 .raise_tpl = efi_raise_tpl,
3208 .restore_tpl = efi_restore_tpl,
3209 .allocate_pages = efi_allocate_pages_ext,
3210 .free_pages = efi_free_pages_ext,
3211 .get_memory_map = efi_get_memory_map_ext,
3212 .allocate_pool = efi_allocate_pool_ext,
3213 .free_pool = efi_free_pool_ext,
3214 .create_event = efi_create_event_ext,
3215 .set_timer = efi_set_timer_ext,
3216 .wait_for_event = efi_wait_for_event,
3217 .signal_event = efi_signal_event_ext,
3218 .close_event = efi_close_event,
3219 .check_event = efi_check_event,
3220 .install_protocol_interface = efi_install_protocol_interface,
3221 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3222 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3223 .handle_protocol = efi_handle_protocol,
3225 .register_protocol_notify = efi_register_protocol_notify,
3226 .locate_handle = efi_locate_handle_ext,
3227 .locate_device_path = efi_locate_device_path,
3228 .install_configuration_table = efi_install_configuration_table_ext,
3229 .load_image = efi_load_image,
3230 .start_image = efi_start_image,
3232 .unload_image = efi_unload_image,
3233 .exit_boot_services = efi_exit_boot_services,
3234 .get_next_monotonic_count = efi_get_next_monotonic_count,
3236 .set_watchdog_timer = efi_set_watchdog_timer,
3237 .connect_controller = efi_connect_controller,
3238 .disconnect_controller = efi_disconnect_controller,
3239 .open_protocol = efi_open_protocol,
3240 .close_protocol = efi_close_protocol,
3241 .open_protocol_information = efi_open_protocol_information,
3242 .protocols_per_handle = efi_protocols_per_handle,
3243 .locate_handle_buffer = efi_locate_handle_buffer,
3244 .locate_protocol = efi_locate_protocol,
3245 .install_multiple_protocol_interfaces =
3246 efi_install_multiple_protocol_interfaces,
3247 .uninstall_multiple_protocol_interfaces =
3248 efi_uninstall_multiple_protocol_interfaces,
3249 .calculate_crc32 = efi_calculate_crc32,
3250 .copy_mem = efi_copy_mem,
3251 .set_mem = efi_set_mem,
3252 .create_event_ex = efi_create_event_ex,
3255 static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3257 struct efi_system_table __efi_runtime_data systab = {
3259 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3260 .revision = EFI_SPECIFICATION_VERSION,
3261 .headersize = sizeof(struct efi_system_table),
3263 .fw_vendor = firmware_vendor,
3264 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3265 .con_in = (void *)&efi_con_in,
3266 .con_out = (void *)&efi_con_out,
3267 .std_err = (void *)&efi_con_out,
3268 .runtime = (void *)&efi_runtime_services,
3269 .boottime = (void *)&efi_boot_services,
3275 * efi_initialize_system_table() - Initialize system table
3277 * Return: status code
3279 efi_status_t efi_initialize_system_table(void)
3283 /* Allocate configuration table array */
3284 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3285 EFI_MAX_CONFIGURATION_TABLES *
3286 sizeof(struct efi_configuration_table),
3287 (void **)&systab.tables);
3289 /* Set CRC32 field in table headers */
3290 efi_update_table_header_crc32(&systab.hdr);
3291 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3292 efi_update_table_header_crc32(&efi_boot_services.hdr);