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);
31 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
32 * we need to do trickery with caches. Since we don't want to break the EFI
33 * aware boot path, only apply hacks when loading exiting directly (breaking
34 * direct Linux EFI booting along the way - oh well).
36 static bool efi_is_direct_boot = true;
39 * EFI can pass arbitrary additional "tables" containing vendor specific
40 * information to the payload. One such table is the FDT table which contains
41 * a pointer to a flattened device tree blob.
43 * In most cases we want to pass an FDT to the payload, so reserve one slot of
44 * config table space for it. The pointer gets populated by do_bootefi_exec().
46 static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
50 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
51 * fixed when compiling U-Boot. However, the payload does not know about that
52 * restriction so we need to manually swap its and our view of that register on
53 * EFI callback entry/exit.
55 static volatile void *efi_gd, *app_gd;
58 static int entry_count;
59 static int nesting_level;
60 /* GUID of the device tree table */
61 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
62 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
63 const efi_guid_t efi_guid_driver_binding_protocol =
64 EFI_DRIVER_BINDING_PROTOCOL_GUID;
66 /* event group ExitBootServices() invoked */
67 const efi_guid_t efi_guid_event_group_exit_boot_services =
68 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
69 /* event group SetVirtualAddressMap() invoked */
70 const efi_guid_t efi_guid_event_group_virtual_address_change =
71 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
72 /* event group memory map changed */
73 const efi_guid_t efi_guid_event_group_memory_map_change =
74 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
75 /* event group boot manager about to boot */
76 const efi_guid_t efi_guid_event_group_ready_to_boot =
77 EFI_EVENT_GROUP_READY_TO_BOOT;
78 /* event group ResetSystem() invoked (before ExitBootServices) */
79 const efi_guid_t efi_guid_event_group_reset_system =
80 EFI_EVENT_GROUP_RESET_SYSTEM;
82 static efi_status_t EFIAPI efi_disconnect_controller(
83 efi_handle_t controller_handle,
84 efi_handle_t driver_image_handle,
85 efi_handle_t child_handle);
87 /* Called on every callback entry */
88 int __efi_entry_check(void)
90 int ret = entry_count++ == 0;
99 /* Called on every callback exit */
100 int __efi_exit_check(void)
102 int ret = --entry_count == 0;
109 /* Called from do_bootefi_exec() */
110 void efi_save_gd(void)
118 * Special case handler for error/abort that just forces things back
119 * to u-boot world so we can dump out an abort msg, without any care
120 * about returning back to UEFI world.
122 void efi_restore_gd(void)
125 /* Only restore if we're already in EFI context */
133 * Return a string for indenting with two spaces per level. A maximum of ten
134 * indent levels is supported. Higher indent levels will be truncated.
136 * @level indent level
137 * @return indent string
139 static const char *indent_string(int level)
141 const char *indent = " ";
142 const int max = strlen(indent);
144 level = min(max, level * 2);
145 return &indent[max - level];
148 const char *__efi_nesting(void)
150 return indent_string(nesting_level);
153 const char *__efi_nesting_inc(void)
155 return indent_string(nesting_level++);
158 const char *__efi_nesting_dec(void)
160 return indent_string(--nesting_level);
164 * Queue an EFI event.
166 * This function queues the notification function of the event for future
169 * The notification function is called if the task priority level of the
170 * event is higher than the current task priority level.
172 * For the SignalEvent service see efi_signal_event_ext.
174 * @event event to signal
175 * @check_tpl check the TPL level
177 static void efi_queue_event(struct efi_event *event, bool check_tpl)
179 if (event->notify_function) {
180 event->is_queued = true;
182 if (check_tpl && efi_tpl >= event->notify_tpl)
184 EFI_CALL_VOID(event->notify_function(event,
185 event->notify_context));
187 event->is_queued = false;
191 * Signal an EFI event.
193 * This function signals an event. If the event belongs to an event group
194 * all events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
195 * their notification function is queued.
197 * For the SignalEvent service see efi_signal_event_ext.
199 * @event event to signal
200 * @check_tpl check the TPL level
202 void efi_signal_event(struct efi_event *event, bool check_tpl)
205 struct efi_event *evt;
208 * The signaled state has to set before executing any
209 * notification function
211 list_for_each_entry(evt, &efi_events, link) {
212 if (!evt->group || guidcmp(evt->group, event->group))
214 if (evt->is_signaled)
216 evt->is_signaled = true;
217 if (evt->type & EVT_NOTIFY_SIGNAL &&
218 evt->notify_function)
219 evt->is_queued = true;
221 list_for_each_entry(evt, &efi_events, link) {
222 if (!evt->group || guidcmp(evt->group, event->group))
225 efi_queue_event(evt, check_tpl);
227 } else if (!event->is_signaled) {
228 event->is_signaled = true;
229 if (event->type & EVT_NOTIFY_SIGNAL)
230 efi_queue_event(event, check_tpl);
235 * Raise the task priority level.
237 * This function implements the RaiseTpl service.
238 * See the Unified Extensible Firmware Interface (UEFI) specification
241 * @new_tpl new value of the task priority level
242 * @return old value of the task priority level
244 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
246 efi_uintn_t old_tpl = efi_tpl;
248 EFI_ENTRY("0x%zx", new_tpl);
250 if (new_tpl < efi_tpl)
251 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
253 if (efi_tpl > TPL_HIGH_LEVEL)
254 efi_tpl = TPL_HIGH_LEVEL;
256 EFI_EXIT(EFI_SUCCESS);
261 * Lower the task priority level.
263 * This function implements the RestoreTpl service.
264 * See the Unified Extensible Firmware Interface (UEFI) specification
267 * @old_tpl value of the task priority level to be restored
269 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
271 EFI_ENTRY("0x%zx", old_tpl);
273 if (old_tpl > efi_tpl)
274 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
276 if (efi_tpl > TPL_HIGH_LEVEL)
277 efi_tpl = TPL_HIGH_LEVEL;
280 * Lowering the TPL may have made queued events eligible for execution.
284 EFI_EXIT(EFI_SUCCESS);
288 * Allocate memory pages.
290 * This function implements the AllocatePages service.
291 * See the Unified Extensible Firmware Interface (UEFI) specification
294 * @type type of allocation to be performed
295 * @memory_type usage type of the allocated memory
296 * @pages number of pages to be allocated
297 * @memory allocated memory
298 * @return status code
300 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
306 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
307 r = efi_allocate_pages(type, memory_type, pages, memory);
314 * This function implements the FreePages service.
315 * See the Unified Extensible Firmware Interface (UEFI) specification
318 * @memory start of the memory area to be freed
319 * @pages number of pages to be freed
320 * @return status code
322 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
327 EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
328 r = efi_free_pages(memory, pages);
333 * Get map describing memory usage.
335 * This function implements the GetMemoryMap service.
336 * See the Unified Extensible Firmware Interface (UEFI) specification
339 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
340 * on exit the size of the copied memory map
341 * @memory_map buffer to which the memory map is written
342 * @map_key key for the memory map
343 * @descriptor_size size of an individual memory descriptor
344 * @descriptor_version version number of the memory descriptor structure
345 * @return status code
347 static efi_status_t EFIAPI efi_get_memory_map_ext(
348 efi_uintn_t *memory_map_size,
349 struct efi_mem_desc *memory_map,
350 efi_uintn_t *map_key,
351 efi_uintn_t *descriptor_size,
352 uint32_t *descriptor_version)
356 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
357 map_key, descriptor_size, descriptor_version);
358 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
359 descriptor_size, descriptor_version);
364 * Allocate memory from pool.
366 * This function implements the AllocatePool service.
367 * See the Unified Extensible Firmware Interface (UEFI) specification
370 * @pool_type type of the pool from which memory is to be allocated
371 * @size number of bytes to be allocated
372 * @buffer allocated memory
373 * @return status code
375 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
381 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
382 r = efi_allocate_pool(pool_type, size, buffer);
387 * Free memory from pool.
389 * This function implements the FreePool service.
390 * See the Unified Extensible Firmware Interface (UEFI) specification
393 * @buffer start of memory to be freed
394 * @return status code
396 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
400 EFI_ENTRY("%p", buffer);
401 r = efi_free_pool(buffer);
406 * Add a new object to the object list.
408 * The protocols list is initialized.
409 * The object handle is set.
411 * @obj object to be added
413 void efi_add_handle(struct efi_object *obj)
417 INIT_LIST_HEAD(&obj->protocols);
419 list_add_tail(&obj->link, &efi_obj_list);
426 * @return status code
428 efi_status_t efi_create_handle(efi_handle_t *handle)
430 struct efi_object *obj;
433 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
434 sizeof(struct efi_object),
436 if (r != EFI_SUCCESS)
439 *handle = obj->handle;
444 * Find a protocol on a handle.
447 * @protocol_guid GUID of the protocol
448 * @handler reference to the protocol
449 * @return status code
451 efi_status_t efi_search_protocol(const efi_handle_t handle,
452 const efi_guid_t *protocol_guid,
453 struct efi_handler **handler)
455 struct efi_object *efiobj;
456 struct list_head *lhandle;
458 if (!handle || !protocol_guid)
459 return EFI_INVALID_PARAMETER;
460 efiobj = efi_search_obj(handle);
462 return EFI_INVALID_PARAMETER;
463 list_for_each(lhandle, &efiobj->protocols) {
464 struct efi_handler *protocol;
466 protocol = list_entry(lhandle, struct efi_handler, link);
467 if (!guidcmp(protocol->guid, protocol_guid)) {
473 return EFI_NOT_FOUND;
477 * Delete protocol from a handle.
479 * @handle handle from which the protocol shall be deleted
480 * @protocol GUID of the protocol to be deleted
481 * @protocol_interface interface of the protocol implementation
482 * @return status code
484 efi_status_t efi_remove_protocol(const efi_handle_t handle,
485 const efi_guid_t *protocol,
486 void *protocol_interface)
488 struct efi_handler *handler;
491 ret = efi_search_protocol(handle, protocol, &handler);
492 if (ret != EFI_SUCCESS)
494 if (guidcmp(handler->guid, protocol))
495 return EFI_INVALID_PARAMETER;
496 if (handler->protocol_interface != protocol_interface)
497 return EFI_INVALID_PARAMETER;
498 list_del(&handler->link);
504 * Delete all protocols from a handle.
506 * @handle handle from which the protocols shall be deleted
507 * @return status code
509 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
511 struct efi_object *efiobj;
512 struct efi_handler *protocol;
513 struct efi_handler *pos;
515 efiobj = efi_search_obj(handle);
517 return EFI_INVALID_PARAMETER;
518 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
521 ret = efi_remove_protocol(handle, protocol->guid,
522 protocol->protocol_interface);
523 if (ret != EFI_SUCCESS)
532 * @handle handle to delete
534 void efi_delete_handle(struct efi_object *obj)
538 efi_remove_all_protocols(obj->handle);
539 list_del(&obj->link);
544 * Check if a pointer is a valid event.
546 * @event pointer to check
547 * @return status code
549 static efi_status_t efi_is_event(const struct efi_event *event)
551 const struct efi_event *evt;
554 return EFI_INVALID_PARAMETER;
555 list_for_each_entry(evt, &efi_events, link) {
559 return EFI_INVALID_PARAMETER;
565 * This function is used inside U-Boot code to create an event.
567 * For the API function implementing the CreateEvent service see
568 * efi_create_event_ext.
570 * @type type of the event to create
571 * @notify_tpl task priority level of the event
572 * @notify_function notification function of the event
573 * @notify_context pointer passed to the notification function
574 * @event created event
575 * @return status code
577 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
578 void (EFIAPI *notify_function) (
579 struct efi_event *event,
581 void *notify_context, efi_guid_t *group,
582 struct efi_event **event)
584 struct efi_event *evt;
587 return EFI_INVALID_PARAMETER;
589 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
590 return EFI_INVALID_PARAMETER;
592 if ((type & (EVT_NOTIFY_SIGNAL | EVT_NOTIFY_WAIT)) &&
593 notify_function == NULL)
594 return EFI_INVALID_PARAMETER;
596 evt = calloc(1, sizeof(struct efi_event));
598 return EFI_OUT_OF_RESOURCES;
600 evt->notify_tpl = notify_tpl;
601 evt->notify_function = notify_function;
602 evt->notify_context = notify_context;
604 /* Disable timers on bootup */
605 evt->trigger_next = -1ULL;
606 evt->is_queued = false;
607 evt->is_signaled = false;
608 list_add_tail(&evt->link, &efi_events);
614 * Create an event in a group.
616 * This function implements the CreateEventEx service.
617 * See the Unified Extensible Firmware Interface (UEFI) specification
619 * TODO: Support event groups
621 * @type type of the event to create
622 * @notify_tpl task priority level of the event
623 * @notify_function notification function of the event
624 * @notify_context pointer passed to the notification function
625 * @event created event
626 * @event_group event group
627 * @return status code
629 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
630 void (EFIAPI *notify_function) (
631 struct efi_event *event,
633 void *notify_context,
634 efi_guid_t *event_group,
635 struct efi_event **event)
637 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
638 notify_context, event_group);
639 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
640 notify_context, event_group, event));
646 * This function implements the CreateEvent service.
647 * See the Unified Extensible Firmware Interface (UEFI) specification
650 * @type type of the event to create
651 * @notify_tpl task priority level of the event
652 * @notify_function notification function of the event
653 * @notify_context pointer passed to the notification function
654 * @event created event
655 * @return status code
657 static efi_status_t EFIAPI efi_create_event_ext(
658 uint32_t type, efi_uintn_t notify_tpl,
659 void (EFIAPI *notify_function) (
660 struct efi_event *event,
662 void *notify_context, struct efi_event **event)
664 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
666 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
667 notify_context, NULL, event));
671 * Check if a timer event has occurred or a queued notification function should
674 * Our timers have to work without interrupts, so we check whenever keyboard
675 * input or disk accesses happen if enough time elapsed for them to fire.
677 void efi_timer_check(void)
679 struct efi_event *evt;
680 u64 now = timer_get_us();
682 list_for_each_entry(evt, &efi_events, link) {
684 efi_queue_event(evt, true);
685 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
687 switch (evt->trigger_type) {
688 case EFI_TIMER_RELATIVE:
689 evt->trigger_type = EFI_TIMER_STOP;
691 case EFI_TIMER_PERIODIC:
692 evt->trigger_next += evt->trigger_time;
697 evt->is_signaled = false;
698 efi_signal_event(evt, true);
704 * Set the trigger time for a timer event or stop the event.
706 * This is the function for internal usage in U-Boot. For the API function
707 * implementing the SetTimer service see efi_set_timer_ext.
709 * @event event for which the timer is set
710 * @type type of the timer
711 * @trigger_time trigger period in multiples of 100ns
712 * @return status code
714 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
715 uint64_t trigger_time)
717 /* Check that the event is valid */
718 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
719 return EFI_INVALID_PARAMETER;
722 * The parameter defines a multiple of 100ns.
723 * We use multiples of 1000ns. So divide by 10.
725 do_div(trigger_time, 10);
729 event->trigger_next = -1ULL;
731 case EFI_TIMER_PERIODIC:
732 case EFI_TIMER_RELATIVE:
733 event->trigger_next = timer_get_us() + trigger_time;
736 return EFI_INVALID_PARAMETER;
738 event->trigger_type = type;
739 event->trigger_time = trigger_time;
740 event->is_signaled = false;
745 * Set the trigger time for a timer event or stop the event.
747 * This function implements the SetTimer service.
748 * See the Unified Extensible Firmware Interface (UEFI) specification
751 * @event event for which the timer is set
752 * @type type of the timer
753 * @trigger_time trigger period in multiples of 100ns
754 * @return status code
756 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
757 enum efi_timer_delay type,
758 uint64_t trigger_time)
760 EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
761 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
765 * Wait for events to be signaled.
767 * This function implements the WaitForEvent service.
768 * See the Unified Extensible Firmware Interface (UEFI) specification
771 * @num_events number of events to be waited for
772 * @events events to be waited for
773 * @index index of the event that was signaled
774 * @return status code
776 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
777 struct efi_event **event,
782 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
784 /* Check parameters */
785 if (!num_events || !event)
786 return EFI_EXIT(EFI_INVALID_PARAMETER);
788 if (efi_tpl != TPL_APPLICATION)
789 return EFI_EXIT(EFI_UNSUPPORTED);
790 for (i = 0; i < num_events; ++i) {
791 if (efi_is_event(event[i]) != EFI_SUCCESS)
792 return EFI_EXIT(EFI_INVALID_PARAMETER);
793 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
794 return EFI_EXIT(EFI_INVALID_PARAMETER);
795 if (!event[i]->is_signaled)
796 efi_queue_event(event[i], true);
799 /* Wait for signal */
801 for (i = 0; i < num_events; ++i) {
802 if (event[i]->is_signaled)
805 /* Allow events to occur. */
811 * Reset the signal which is passed to the caller to allow periodic
814 event[i]->is_signaled = false;
818 return EFI_EXIT(EFI_SUCCESS);
822 * Signal an EFI event.
824 * This function implements the SignalEvent service.
825 * See the Unified Extensible Firmware Interface (UEFI) specification
828 * This functions sets the signaled state of the event and queues the
829 * notification function for execution.
831 * @event event to signal
832 * @return status code
834 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
836 EFI_ENTRY("%p", event);
837 if (efi_is_event(event) != EFI_SUCCESS)
838 return EFI_EXIT(EFI_INVALID_PARAMETER);
839 efi_signal_event(event, true);
840 return EFI_EXIT(EFI_SUCCESS);
844 * Close an EFI event.
846 * This function implements the CloseEvent service.
847 * See the Unified Extensible Firmware Interface (UEFI) specification
850 * @event event to close
851 * @return status code
853 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
855 EFI_ENTRY("%p", event);
856 if (efi_is_event(event) != EFI_SUCCESS)
857 return EFI_EXIT(EFI_INVALID_PARAMETER);
858 list_del(&event->link);
860 return EFI_EXIT(EFI_SUCCESS);
864 * Check if an event is signaled.
866 * This function implements the CheckEvent service.
867 * See the Unified Extensible Firmware Interface (UEFI) specification
870 * If an event is not signaled yet, the notification function is queued.
871 * The signaled state is cleared.
873 * @event event to check
874 * @return status code
876 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
878 EFI_ENTRY("%p", event);
880 if (efi_is_event(event) != EFI_SUCCESS ||
881 event->type & EVT_NOTIFY_SIGNAL)
882 return EFI_EXIT(EFI_INVALID_PARAMETER);
883 if (!event->is_signaled)
884 efi_queue_event(event, true);
885 if (event->is_signaled) {
886 event->is_signaled = false;
887 return EFI_EXIT(EFI_SUCCESS);
889 return EFI_EXIT(EFI_NOT_READY);
893 * Find the internal EFI object for a handle.
895 * @handle handle to find
898 struct efi_object *efi_search_obj(const efi_handle_t handle)
900 struct efi_object *efiobj;
902 list_for_each_entry(efiobj, &efi_obj_list, link) {
903 if (efiobj->handle == handle)
911 * Create open protocol info entry and add it to a protocol.
913 * @handler handler of a protocol
914 * @return open protocol info entry
916 static struct efi_open_protocol_info_entry *efi_create_open_info(
917 struct efi_handler *handler)
919 struct efi_open_protocol_info_item *item;
921 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
924 /* Append the item to the open protocol info list. */
925 list_add_tail(&item->link, &handler->open_infos);
931 * Remove an open protocol info entry from a protocol.
933 * @handler handler of a protocol
934 * @return status code
936 static efi_status_t efi_delete_open_info(
937 struct efi_open_protocol_info_item *item)
939 list_del(&item->link);
945 * Install new protocol on a handle.
947 * @handle handle on which the protocol shall be installed
948 * @protocol GUID of the protocol to be installed
949 * @protocol_interface interface of the protocol implementation
950 * @return status code
952 efi_status_t efi_add_protocol(const efi_handle_t handle,
953 const efi_guid_t *protocol,
954 void *protocol_interface)
956 struct efi_object *efiobj;
957 struct efi_handler *handler;
960 efiobj = efi_search_obj(handle);
962 return EFI_INVALID_PARAMETER;
963 ret = efi_search_protocol(handle, protocol, NULL);
964 if (ret != EFI_NOT_FOUND)
965 return EFI_INVALID_PARAMETER;
966 handler = calloc(1, sizeof(struct efi_handler));
968 return EFI_OUT_OF_RESOURCES;
969 handler->guid = protocol;
970 handler->protocol_interface = protocol_interface;
971 INIT_LIST_HEAD(&handler->open_infos);
972 list_add_tail(&handler->link, &efiobj->protocols);
973 if (!guidcmp(&efi_guid_device_path, protocol))
974 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
979 * Install protocol interface.
981 * This function implements the InstallProtocolInterface service.
982 * See the Unified Extensible Firmware Interface (UEFI) specification
985 * @handle handle on which the protocol shall be installed
986 * @protocol GUID of the protocol to be installed
987 * @protocol_interface_type type of the interface to be installed,
988 * always EFI_NATIVE_INTERFACE
989 * @protocol_interface interface of the protocol implementation
990 * @return status code
992 static efi_status_t EFIAPI efi_install_protocol_interface(
993 void **handle, const efi_guid_t *protocol,
994 int protocol_interface_type, void *protocol_interface)
998 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1001 if (!handle || !protocol ||
1002 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1003 r = EFI_INVALID_PARAMETER;
1007 /* Create new handle if requested. */
1009 r = efi_create_handle(handle);
1010 if (r != EFI_SUCCESS)
1012 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1015 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1018 /* Add new protocol */
1019 r = efi_add_protocol(*handle, protocol, protocol_interface);
1025 * Get all drivers associated to a controller.
1026 * The allocated buffer has to be freed with free().
1028 * @efiobj handle of the controller
1029 * @protocol protocol guid (optional)
1030 * @number_of_drivers number of child controllers
1031 * @driver_handle_buffer handles of the the drivers
1032 * @return status code
1034 static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1035 const efi_guid_t *protocol,
1036 efi_uintn_t *number_of_drivers,
1037 efi_handle_t **driver_handle_buffer)
1039 struct efi_handler *handler;
1040 struct efi_open_protocol_info_item *item;
1041 efi_uintn_t count = 0, i;
1044 /* Count all driver associations */
1045 list_for_each_entry(handler, &efiobj->protocols, link) {
1046 if (protocol && guidcmp(handler->guid, protocol))
1048 list_for_each_entry(item, &handler->open_infos, link) {
1049 if (item->info.attributes &
1050 EFI_OPEN_PROTOCOL_BY_DRIVER)
1055 * Create buffer. In case of duplicate driver assignments the buffer
1056 * will be too large. But that does not harm.
1058 *number_of_drivers = 0;
1059 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1060 if (!*driver_handle_buffer)
1061 return EFI_OUT_OF_RESOURCES;
1062 /* Collect unique driver handles */
1063 list_for_each_entry(handler, &efiobj->protocols, link) {
1064 if (protocol && guidcmp(handler->guid, protocol))
1066 list_for_each_entry(item, &handler->open_infos, link) {
1067 if (item->info.attributes &
1068 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1069 /* Check this is a new driver */
1071 for (i = 0; i < *number_of_drivers; ++i) {
1072 if ((*driver_handle_buffer)[i] ==
1073 item->info.agent_handle)
1076 /* Copy handle to buffer */
1078 i = (*number_of_drivers)++;
1079 (*driver_handle_buffer)[i] =
1080 item->info.agent_handle;
1089 * Disconnect all drivers from a controller.
1091 * This function implements the DisconnectController service.
1092 * See the Unified Extensible Firmware Interface (UEFI) specification
1095 * @efiobj handle of the controller
1096 * @protocol protocol guid (optional)
1097 * @child_handle handle of the child to destroy
1098 * @return status code
1100 static efi_status_t efi_disconnect_all_drivers(
1101 struct efi_object *efiobj,
1102 const efi_guid_t *protocol,
1103 efi_handle_t child_handle)
1105 efi_uintn_t number_of_drivers;
1106 efi_handle_t *driver_handle_buffer;
1107 efi_status_t r, ret;
1109 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1110 &driver_handle_buffer);
1111 if (ret != EFI_SUCCESS)
1114 ret = EFI_NOT_FOUND;
1115 while (number_of_drivers) {
1116 r = EFI_CALL(efi_disconnect_controller(
1118 driver_handle_buffer[--number_of_drivers],
1120 if (r == EFI_SUCCESS)
1123 free(driver_handle_buffer);
1128 * Uninstall protocol interface.
1130 * This function implements the UninstallProtocolInterface service.
1131 * See the Unified Extensible Firmware Interface (UEFI) specification
1134 * @handle handle from which the protocol shall be removed
1135 * @protocol GUID of the protocol to be removed
1136 * @protocol_interface interface to be removed
1137 * @return status code
1139 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
1140 efi_handle_t handle, const efi_guid_t *protocol,
1141 void *protocol_interface)
1143 struct efi_object *efiobj;
1144 struct efi_handler *handler;
1145 struct efi_open_protocol_info_item *item;
1146 struct efi_open_protocol_info_item *pos;
1149 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1152 efiobj = efi_search_obj(handle);
1154 r = EFI_INVALID_PARAMETER;
1157 /* Find the protocol on the handle */
1158 r = efi_search_protocol(handle, protocol, &handler);
1159 if (r != EFI_SUCCESS)
1161 /* Disconnect controllers */
1162 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1163 if (!list_empty(&handler->open_infos)) {
1164 r = EFI_ACCESS_DENIED;
1167 /* Close protocol */
1168 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1169 if (item->info.attributes ==
1170 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1171 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1172 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1173 list_del(&item->link);
1175 if (!list_empty(&handler->open_infos)) {
1176 r = EFI_ACCESS_DENIED;
1179 r = efi_remove_protocol(handle, protocol, protocol_interface);
1185 * Register an event for notification when a protocol is installed.
1187 * This function implements the RegisterProtocolNotify service.
1188 * See the Unified Extensible Firmware Interface (UEFI) specification
1191 * @protocol GUID of the protocol whose installation shall be
1193 * @event event to be signaled upon installation of the protocol
1194 * @registration key for retrieving the registration information
1195 * @return status code
1197 static efi_status_t EFIAPI efi_register_protocol_notify(
1198 const efi_guid_t *protocol,
1199 struct efi_event *event,
1200 void **registration)
1202 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1203 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1207 * Determine if an EFI handle implements a protocol.
1209 * See the documentation of the LocateHandle service in the UEFI specification.
1211 * @search_type selection criterion
1212 * @protocol GUID of the protocol
1213 * @search_key registration key
1215 * @return 0 if the handle implements the protocol
1217 static int efi_search(enum efi_locate_search_type search_type,
1218 const efi_guid_t *protocol, void *search_key,
1219 struct efi_object *efiobj)
1223 switch (search_type) {
1226 case BY_REGISTER_NOTIFY:
1227 /* TODO: RegisterProtocolNotify is not implemented yet */
1230 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1231 return (ret != EFI_SUCCESS);
1233 /* Invalid search type */
1239 * Locate handles implementing a protocol.
1241 * This function is meant for U-Boot internal calls. For the API implementation
1242 * of the LocateHandle service see efi_locate_handle_ext.
1244 * @search_type selection criterion
1245 * @protocol GUID of the protocol
1246 * @search_key registration key
1247 * @buffer_size size of the buffer to receive the handles in bytes
1248 * @buffer buffer to receive the relevant handles
1249 * @return status code
1251 static efi_status_t efi_locate_handle(
1252 enum efi_locate_search_type search_type,
1253 const efi_guid_t *protocol, void *search_key,
1254 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1256 struct efi_object *efiobj;
1257 efi_uintn_t size = 0;
1259 /* Check parameters */
1260 switch (search_type) {
1263 case BY_REGISTER_NOTIFY:
1265 return EFI_INVALID_PARAMETER;
1266 /* RegisterProtocolNotify is not implemented yet */
1267 return EFI_UNSUPPORTED;
1270 return EFI_INVALID_PARAMETER;
1273 return EFI_INVALID_PARAMETER;
1277 * efi_locate_handle_buffer uses this function for
1278 * the calculation of the necessary buffer size.
1279 * So do not require a buffer for buffersize == 0.
1281 if (!buffer_size || (*buffer_size && !buffer))
1282 return EFI_INVALID_PARAMETER;
1284 /* Count how much space we need */
1285 list_for_each_entry(efiobj, &efi_obj_list, link) {
1286 if (!efi_search(search_type, protocol, search_key, efiobj))
1287 size += sizeof(void *);
1290 if (*buffer_size < size) {
1291 *buffer_size = size;
1292 return EFI_BUFFER_TOO_SMALL;
1295 *buffer_size = size;
1297 return EFI_NOT_FOUND;
1299 /* Then fill the array */
1300 list_for_each_entry(efiobj, &efi_obj_list, link) {
1301 if (!efi_search(search_type, protocol, search_key, efiobj))
1302 *buffer++ = efiobj->handle;
1309 * Locate handles implementing a protocol.
1311 * This function implements the LocateHandle service.
1312 * See the Unified Extensible Firmware Interface (UEFI) specification
1315 * @search_type selection criterion
1316 * @protocol GUID of the protocol
1317 * @search_key registration key
1318 * @buffer_size size of the buffer to receive the handles in bytes
1319 * @buffer buffer to receive the relevant handles
1320 * @return 0 if the handle implements the protocol
1322 static efi_status_t EFIAPI efi_locate_handle_ext(
1323 enum efi_locate_search_type search_type,
1324 const efi_guid_t *protocol, void *search_key,
1325 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1327 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1328 buffer_size, buffer);
1330 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1331 buffer_size, buffer));
1334 /* Collapses configuration table entries, removing index i */
1335 static void efi_remove_configuration_table(int i)
1337 struct efi_configuration_table *this = &efi_conf_table[i];
1338 struct efi_configuration_table *next = &efi_conf_table[i + 1];
1339 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1341 memmove(this, next, (ulong)end - (ulong)next);
1346 * Adds, updates, or removes a configuration table.
1348 * This function is used for internal calls. For the API implementation of the
1349 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1351 * @guid GUID of the installed table
1352 * @table table to be installed
1353 * @return status code
1355 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1358 struct efi_event *evt;
1362 return EFI_INVALID_PARAMETER;
1364 /* Check for guid override */
1365 for (i = 0; i < systab.nr_tables; i++) {
1366 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1368 efi_conf_table[i].table = table;
1370 efi_remove_configuration_table(i);
1376 return EFI_NOT_FOUND;
1378 /* No override, check for overflow */
1379 if (i >= ARRAY_SIZE(efi_conf_table))
1380 return EFI_OUT_OF_RESOURCES;
1382 /* Add a new entry */
1383 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1384 efi_conf_table[i].table = table;
1385 systab.nr_tables = i + 1;
1388 /* Notify that the configuration table was changed */
1389 list_for_each_entry(evt, &efi_events, link) {
1390 if (evt->group && !guidcmp(evt->group, guid)) {
1391 efi_signal_event(evt, false);
1400 * Adds, updates, or removes a configuration table.
1402 * This function implements the InstallConfigurationTable service.
1403 * See the Unified Extensible Firmware Interface (UEFI) specification
1406 * @guid GUID of the installed table
1407 * @table table to be installed
1408 * @return status code
1410 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1413 EFI_ENTRY("%pUl, %p", guid, table);
1414 return EFI_EXIT(efi_install_configuration_table(guid, table));
1418 * Initialize a loaded_image_info + loaded_image_info object with correct
1419 * protocols, boot-device, etc.
1421 * @info loaded image info to be passed to the entry point of the
1423 * @obj internal object associated with the loaded image
1424 * @device_path device path of the loaded image
1425 * @file_path file path of the loaded image
1426 * @return status code
1428 efi_status_t efi_setup_loaded_image(
1429 struct efi_loaded_image *info, struct efi_object *obj,
1430 struct efi_device_path *device_path,
1431 struct efi_device_path *file_path)
1435 /* Add internal object to object list */
1436 efi_add_handle(obj);
1437 /* efi_exit() assumes that the handle points to the info */
1440 info->file_path = file_path;
1443 info->device_handle = efi_dp_find_obj(device_path, NULL);
1445 * When asking for the device path interface, return
1446 * bootefi_device_path
1448 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1450 if (ret != EFI_SUCCESS)
1455 * When asking for the loaded_image interface, just
1456 * return handle which points to loaded_image_info
1458 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1459 if (ret != EFI_SUCCESS)
1462 ret = efi_add_protocol(obj->handle,
1463 &efi_guid_device_path_to_text_protocol,
1464 (void *)&efi_device_path_to_text);
1465 if (ret != EFI_SUCCESS)
1468 ret = efi_add_protocol(obj->handle,
1469 &efi_guid_device_path_utilities_protocol,
1470 (void *)&efi_device_path_utilities);
1471 if (ret != EFI_SUCCESS)
1476 printf("ERROR: Failure to install protocols for loaded image\n");
1481 * Load an image using a file path.
1483 * @file_path the path of the image to load
1484 * @buffer buffer containing the loaded image
1485 * @return status code
1487 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1490 struct efi_file_info *info = NULL;
1491 struct efi_file_handle *f;
1492 static efi_status_t ret;
1495 f = efi_file_from_path(file_path);
1497 return EFI_DEVICE_ERROR;
1500 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1502 if (ret == EFI_BUFFER_TOO_SMALL) {
1504 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1507 if (ret != EFI_SUCCESS)
1510 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1514 bs = info->file_size;
1515 EFI_CALL(ret = f->read(f, &bs, *buffer));
1519 EFI_CALL(f->close(f));
1521 if (ret != EFI_SUCCESS) {
1522 efi_free_pool(*buffer);
1530 * Load an EFI image into memory.
1532 * This function implements the LoadImage service.
1533 * See the Unified Extensible Firmware Interface (UEFI) specification
1536 * @boot_policy true for request originating from the boot manager
1537 * @parent_image the caller's image handle
1538 * @file_path the path of the image to load
1539 * @source_buffer memory location from which the image is installed
1540 * @source_size size of the memory area from which the image is
1542 * @image_handle handle for the newly installed image
1543 * @return status code
1545 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1546 efi_handle_t parent_image,
1547 struct efi_device_path *file_path,
1548 void *source_buffer,
1549 efi_uintn_t source_size,
1550 efi_handle_t *image_handle)
1552 struct efi_loaded_image *info;
1553 struct efi_object *obj;
1556 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1557 file_path, source_buffer, source_size, image_handle);
1559 if (!image_handle || !parent_image) {
1560 ret = EFI_INVALID_PARAMETER;
1564 if (!source_buffer && !file_path) {
1565 ret = EFI_NOT_FOUND;
1569 info = calloc(1, sizeof(*info));
1571 ret = EFI_OUT_OF_RESOURCES;
1574 obj = calloc(1, sizeof(*obj));
1577 ret = EFI_OUT_OF_RESOURCES;
1581 if (!source_buffer) {
1582 struct efi_device_path *dp, *fp;
1584 ret = efi_load_image_from_path(file_path, &source_buffer);
1585 if (ret != EFI_SUCCESS)
1588 * split file_path which contains both the device and
1591 efi_dp_split_file_path(file_path, &dp, &fp);
1592 ret = efi_setup_loaded_image(info, obj, dp, fp);
1593 if (ret != EFI_SUCCESS)
1596 /* In this case, file_path is the "device" path, ie.
1597 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1599 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1600 if (ret != EFI_SUCCESS)
1603 info->reserved = efi_load_pe(source_buffer, info);
1604 if (!info->reserved) {
1605 ret = EFI_UNSUPPORTED;
1608 info->system_table = &systab;
1609 info->parent_handle = parent_image;
1610 *image_handle = obj->handle;
1611 return EFI_EXIT(EFI_SUCCESS);
1614 efi_delete_handle(obj);
1616 return EFI_EXIT(ret);
1620 * Call the entry point of an image.
1622 * This function implements the StartImage service.
1623 * See the Unified Extensible Firmware Interface (UEFI) specification
1626 * @image_handle handle of the image
1627 * @exit_data_size size of the buffer
1628 * @exit_data buffer to receive the exit data of the called image
1629 * @return status code
1631 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1632 unsigned long *exit_data_size,
1635 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1636 struct efi_system_table *st);
1637 struct efi_loaded_image *info = image_handle;
1640 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1641 entry = info->reserved;
1643 efi_is_direct_boot = false;
1645 /* call the image! */
1646 if (setjmp(&info->exit_jmp)) {
1648 * We called the entry point of the child image with EFI_CALL
1649 * in the lines below. The child image called the Exit() boot
1650 * service efi_exit() which executed the long jump that brought
1651 * us to the current line. This implies that the second half
1652 * of the EFI_CALL macro has not been executed.
1656 * efi_exit() called efi_restore_gd(). We have to undo this
1657 * otherwise __efi_entry_check() will put the wrong value into
1663 * To get ready to call EFI_EXIT below we have to execute the
1664 * missed out steps of EFI_CALL.
1666 assert(__efi_entry_check());
1667 debug("%sEFI: %lu returned by started image\n",
1668 __efi_nesting_dec(),
1669 (unsigned long)((uintptr_t)info->exit_status &
1671 return EFI_EXIT(info->exit_status);
1674 ret = EFI_CALL(entry(image_handle, &systab));
1677 * Usually UEFI applications call Exit() instead of returning.
1678 * But because the world doesn not consist of ponies and unicorns,
1679 * we're happy to emulate that behavior on behalf of a payload
1682 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
1686 * Leave an EFI application or driver.
1688 * This function implements the Exit service.
1689 * See the Unified Extensible Firmware Interface (UEFI) specification
1692 * @image_handle handle of the application or driver that is exiting
1693 * @exit_status status code
1694 * @exit_data_size size of the buffer in bytes
1695 * @exit_data buffer with data describing an error
1696 * @return status code
1698 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1699 efi_status_t exit_status,
1700 unsigned long exit_data_size,
1704 * We require that the handle points to the original loaded
1705 * image protocol interface.
1707 * For getting the longjmp address this is safer than locating
1708 * the protocol because the protocol may have been reinstalled
1709 * pointing to another memory location.
1711 * TODO: We should call the unload procedure of the loaded
1714 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
1716 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1717 exit_data_size, exit_data);
1719 /* Make sure entry/exit counts for EFI world cross-overs match */
1720 EFI_EXIT(exit_status);
1723 * But longjmp out with the U-Boot gd, not the application's, as
1724 * the other end is a setjmp call inside EFI context.
1728 loaded_image_info->exit_status = exit_status;
1729 longjmp(&loaded_image_info->exit_jmp, 1);
1731 panic("EFI application exited");
1735 * Unload an EFI image.
1737 * This function implements the UnloadImage service.
1738 * See the Unified Extensible Firmware Interface (UEFI) specification
1741 * @image_handle handle of the image to be unloaded
1742 * @return status code
1744 static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1746 struct efi_object *efiobj;
1748 EFI_ENTRY("%p", image_handle);
1749 efiobj = efi_search_obj(image_handle);
1751 list_del(&efiobj->link);
1753 return EFI_EXIT(EFI_SUCCESS);
1757 * Fix up caches for EFI payloads if necessary.
1759 static void efi_exit_caches(void)
1761 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1763 * Grub on 32bit ARM needs to have caches disabled before jumping into
1764 * a zImage, but does not know of all cache layers. Give it a hand.
1766 if (efi_is_direct_boot)
1767 cleanup_before_linux();
1772 * Stop all boot services.
1774 * This function implements the ExitBootServices service.
1775 * See the Unified Extensible Firmware Interface (UEFI) specification
1778 * All timer events are disabled.
1779 * For exit boot services events the notification function is called.
1780 * The boot services are disabled in the system table.
1782 * @image_handle handle of the loaded image
1783 * @map_key key of the memory map
1784 * @return status code
1786 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1787 unsigned long map_key)
1789 struct efi_event *evt;
1791 EFI_ENTRY("%p, %ld", image_handle, map_key);
1793 /* Make sure that notification functions are not called anymore */
1794 efi_tpl = TPL_HIGH_LEVEL;
1796 /* Check if ExitBootServices has already been called */
1797 if (!systab.boottime)
1798 return EFI_EXIT(EFI_SUCCESS);
1800 /* Add related events to the event group */
1801 list_for_each_entry(evt, &efi_events, link) {
1802 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1803 evt->group = &efi_guid_event_group_exit_boot_services;
1805 /* Notify that ExitBootServices is invoked. */
1806 list_for_each_entry(evt, &efi_events, link) {
1808 !guidcmp(evt->group,
1809 &efi_guid_event_group_exit_boot_services)) {
1810 efi_signal_event(evt, false);
1815 /* TODO Should persist EFI variables here */
1817 board_quiesce_devices();
1819 /* Fix up caches for EFI payloads if necessary */
1822 /* This stops all lingering devices */
1823 bootm_disable_interrupts();
1825 /* Disable boottime services */
1826 systab.con_in_handle = NULL;
1827 systab.con_in = NULL;
1828 systab.con_out_handle = NULL;
1829 systab.con_out = NULL;
1830 systab.stderr_handle = NULL;
1831 systab.std_err = NULL;
1832 systab.boottime = NULL;
1834 /* Recalculate CRC32 */
1835 systab.hdr.crc32 = 0;
1836 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1837 sizeof(struct efi_system_table));
1839 /* Give the payload some time to boot */
1840 efi_set_watchdog(0);
1843 return EFI_EXIT(EFI_SUCCESS);
1847 * Get next value of the counter.
1849 * This function implements the NextMonotonicCount service.
1850 * See the Unified Extensible Firmware Interface (UEFI) specification
1853 * @count returned value of the counter
1854 * @return status code
1856 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1858 static uint64_t mono;
1860 EFI_ENTRY("%p", count);
1862 return EFI_EXIT(EFI_SUCCESS);
1868 * This function implements the Stall sercive.
1869 * See the Unified Extensible Firmware Interface (UEFI) specification
1872 * @microseconds period to sleep in microseconds
1873 * @return status code
1875 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1877 EFI_ENTRY("%ld", microseconds);
1878 udelay(microseconds);
1879 return EFI_EXIT(EFI_SUCCESS);
1883 * Reset the watchdog timer.
1885 * This function implements the SetWatchdogTimer service.
1886 * See the Unified Extensible Firmware Interface (UEFI) specification
1889 * @timeout seconds before reset by watchdog
1890 * @watchdog_code code to be logged when resetting
1891 * @data_size size of buffer in bytes
1892 * @watchdog_data buffer with data describing the reset reason
1893 * @return status code
1895 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1896 uint64_t watchdog_code,
1897 unsigned long data_size,
1898 uint16_t *watchdog_data)
1900 EFI_ENTRY("%ld, 0x%" PRIx64 ", %ld, %p", timeout, watchdog_code,
1901 data_size, watchdog_data);
1902 return EFI_EXIT(efi_set_watchdog(timeout));
1908 * This function implements the CloseProtocol service.
1909 * See the Unified Extensible Firmware Interface (UEFI) specification
1912 * @handle handle on which the protocol shall be closed
1913 * @protocol GUID of the protocol to close
1914 * @agent_handle handle of the driver
1915 * @controller_handle handle of the controller
1916 * @return status code
1918 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1919 const efi_guid_t *protocol,
1920 efi_handle_t agent_handle,
1921 efi_handle_t controller_handle)
1923 struct efi_handler *handler;
1924 struct efi_open_protocol_info_item *item;
1925 struct efi_open_protocol_info_item *pos;
1928 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1931 if (!agent_handle) {
1932 r = EFI_INVALID_PARAMETER;
1935 r = efi_search_protocol(handle, protocol, &handler);
1936 if (r != EFI_SUCCESS)
1940 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1941 if (item->info.agent_handle == agent_handle &&
1942 item->info.controller_handle == controller_handle) {
1943 efi_delete_open_info(item);
1953 * Provide information about then open status of a protocol on a handle
1955 * This function implements the OpenProtocolInformation service.
1956 * See the Unified Extensible Firmware Interface (UEFI) specification
1959 * @handle handle for which the information shall be retrieved
1960 * @protocol GUID of the protocol
1961 * @entry_buffer buffer to receive the open protocol information
1962 * @entry_count number of entries available in the buffer
1963 * @return status code
1965 static efi_status_t EFIAPI efi_open_protocol_information(
1966 efi_handle_t handle, const efi_guid_t *protocol,
1967 struct efi_open_protocol_info_entry **entry_buffer,
1968 efi_uintn_t *entry_count)
1970 unsigned long buffer_size;
1971 unsigned long count;
1972 struct efi_handler *handler;
1973 struct efi_open_protocol_info_item *item;
1976 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1979 /* Check parameters */
1980 if (!entry_buffer) {
1981 r = EFI_INVALID_PARAMETER;
1984 r = efi_search_protocol(handle, protocol, &handler);
1985 if (r != EFI_SUCCESS)
1990 list_for_each_entry(item, &handler->open_infos, link) {
1991 if (item->info.open_count)
1994 *entry_count = count;
1995 *entry_buffer = NULL;
2002 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2003 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2004 (void **)entry_buffer);
2005 if (r != EFI_SUCCESS)
2007 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2008 if (item->info.open_count)
2009 (*entry_buffer)[--count] = item->info;
2016 * Get protocols installed on a handle.
2018 * This function implements the ProtocolsPerHandleService.
2019 * See the Unified Extensible Firmware Interface (UEFI) specification
2022 * @handle handle for which the information is retrieved
2023 * @protocol_buffer buffer with protocol GUIDs
2024 * @protocol_buffer_count number of entries in the buffer
2025 * @return status code
2027 static efi_status_t EFIAPI efi_protocols_per_handle(
2028 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2029 efi_uintn_t *protocol_buffer_count)
2031 unsigned long buffer_size;
2032 struct efi_object *efiobj;
2033 struct list_head *protocol_handle;
2036 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2037 protocol_buffer_count);
2039 if (!handle || !protocol_buffer || !protocol_buffer_count)
2040 return EFI_EXIT(EFI_INVALID_PARAMETER);
2042 *protocol_buffer = NULL;
2043 *protocol_buffer_count = 0;
2045 efiobj = efi_search_obj(handle);
2047 return EFI_EXIT(EFI_INVALID_PARAMETER);
2049 /* Count protocols */
2050 list_for_each(protocol_handle, &efiobj->protocols) {
2051 ++*protocol_buffer_count;
2055 if (*protocol_buffer_count) {
2058 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2059 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2060 (void **)protocol_buffer);
2061 if (r != EFI_SUCCESS)
2063 list_for_each(protocol_handle, &efiobj->protocols) {
2064 struct efi_handler *protocol;
2066 protocol = list_entry(protocol_handle,
2067 struct efi_handler, link);
2068 (*protocol_buffer)[j] = (void *)protocol->guid;
2073 return EFI_EXIT(EFI_SUCCESS);
2077 * Locate handles implementing a protocol.
2079 * This function implements the LocateHandleBuffer service.
2080 * See the Unified Extensible Firmware Interface (UEFI) specification
2083 * @search_type selection criterion
2084 * @protocol GUID of the protocol
2085 * @search_key registration key
2086 * @no_handles number of returned handles
2087 * @buffer buffer with the returned handles
2088 * @return status code
2090 static efi_status_t EFIAPI efi_locate_handle_buffer(
2091 enum efi_locate_search_type search_type,
2092 const efi_guid_t *protocol, void *search_key,
2093 efi_uintn_t *no_handles, efi_handle_t **buffer)
2096 efi_uintn_t buffer_size = 0;
2098 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2099 no_handles, buffer);
2101 if (!no_handles || !buffer) {
2102 r = EFI_INVALID_PARAMETER;
2107 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2109 if (r != EFI_BUFFER_TOO_SMALL)
2111 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2113 if (r != EFI_SUCCESS)
2115 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2117 if (r == EFI_SUCCESS)
2118 *no_handles = buffer_size / sizeof(efi_handle_t);
2124 * Find an interface implementing a protocol.
2126 * This function implements the LocateProtocol service.
2127 * See the Unified Extensible Firmware Interface (UEFI) specification
2130 * @protocol GUID of the protocol
2131 * @registration registration key passed to the notification function
2132 * @protocol_interface interface implementing the protocol
2133 * @return status code
2135 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2137 void **protocol_interface)
2139 struct list_head *lhandle;
2142 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2144 if (!protocol || !protocol_interface)
2145 return EFI_EXIT(EFI_INVALID_PARAMETER);
2147 list_for_each(lhandle, &efi_obj_list) {
2148 struct efi_object *efiobj;
2149 struct efi_handler *handler;
2151 efiobj = list_entry(lhandle, struct efi_object, link);
2153 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2154 if (ret == EFI_SUCCESS) {
2155 *protocol_interface = handler->protocol_interface;
2156 return EFI_EXIT(EFI_SUCCESS);
2159 *protocol_interface = NULL;
2161 return EFI_EXIT(EFI_NOT_FOUND);
2165 * Get the device path and handle of an device implementing a protocol.
2167 * This function implements the LocateDevicePath service.
2168 * See the Unified Extensible Firmware Interface (UEFI) specification
2171 * @protocol GUID of the protocol
2172 * @device_path device path
2173 * @device handle of the device
2174 * @return status code
2176 static efi_status_t EFIAPI efi_locate_device_path(
2177 const efi_guid_t *protocol,
2178 struct efi_device_path **device_path,
2179 efi_handle_t *device)
2181 struct efi_device_path *dp;
2183 struct efi_handler *handler;
2184 efi_handle_t *handles;
2186 size_t len_best = 0;
2187 efi_uintn_t no_handles;
2191 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2193 if (!protocol || !device_path || !*device_path || !device) {
2194 ret = EFI_INVALID_PARAMETER;
2198 /* Find end of device path */
2199 len = efi_dp_instance_size(*device_path);
2201 /* Get all handles implementing the protocol */
2202 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2203 &no_handles, &handles));
2204 if (ret != EFI_SUCCESS)
2207 for (i = 0; i < no_handles; ++i) {
2208 /* Find the device path protocol */
2209 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2211 if (ret != EFI_SUCCESS)
2213 dp = (struct efi_device_path *)handler->protocol_interface;
2214 len_dp = efi_dp_instance_size(dp);
2216 * This handle can only be a better fit
2217 * if its device path length is longer than the best fit and
2218 * if its device path length is shorter of equal the searched
2221 if (len_dp <= len_best || len_dp > len)
2223 /* Check if dp is a subpath of device_path */
2224 if (memcmp(*device_path, dp, len_dp))
2226 *device = handles[i];
2230 remainder = (u8 *)*device_path + len_best;
2231 *device_path = (struct efi_device_path *)remainder;
2234 ret = EFI_NOT_FOUND;
2237 return EFI_EXIT(ret);
2241 * Install multiple protocol interfaces.
2243 * This function implements the MultipleProtocolInterfaces service.
2244 * See the Unified Extensible Firmware Interface (UEFI) specification
2247 * @handle handle on which the protocol interfaces shall be installed
2248 * @... NULL terminated argument list with pairs of protocol GUIDS and
2250 * @return status code
2252 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2255 EFI_ENTRY("%p", handle);
2258 const efi_guid_t *protocol;
2259 void *protocol_interface;
2260 efi_status_t r = EFI_SUCCESS;
2264 return EFI_EXIT(EFI_INVALID_PARAMETER);
2266 va_start(argptr, handle);
2268 protocol = va_arg(argptr, efi_guid_t*);
2271 protocol_interface = va_arg(argptr, void*);
2272 r = EFI_CALL(efi_install_protocol_interface(
2274 EFI_NATIVE_INTERFACE,
2275 protocol_interface));
2276 if (r != EFI_SUCCESS)
2281 if (r == EFI_SUCCESS)
2284 /* If an error occurred undo all changes. */
2285 va_start(argptr, handle);
2287 protocol = va_arg(argptr, efi_guid_t*);
2288 protocol_interface = va_arg(argptr, void*);
2289 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2290 protocol_interface));
2298 * Uninstall multiple protocol interfaces.
2300 * This function implements the UninstallMultipleProtocolInterfaces service.
2301 * See the Unified Extensible Firmware Interface (UEFI) specification
2304 * @handle handle from which the protocol interfaces shall be removed
2305 * @... NULL terminated argument list with pairs of protocol GUIDS and
2307 * @return status code
2309 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2312 EFI_ENTRY("%p", handle);
2315 const efi_guid_t *protocol;
2316 void *protocol_interface;
2317 efi_status_t r = EFI_SUCCESS;
2321 return EFI_EXIT(EFI_INVALID_PARAMETER);
2323 va_start(argptr, handle);
2325 protocol = va_arg(argptr, efi_guid_t*);
2328 protocol_interface = va_arg(argptr, void*);
2329 r = EFI_CALL(efi_uninstall_protocol_interface(
2331 protocol_interface));
2332 if (r != EFI_SUCCESS)
2337 if (r == EFI_SUCCESS)
2340 /* If an error occurred undo all changes. */
2341 va_start(argptr, handle);
2343 protocol = va_arg(argptr, efi_guid_t*);
2344 protocol_interface = va_arg(argptr, void*);
2345 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2346 EFI_NATIVE_INTERFACE,
2347 protocol_interface));
2355 * Calculate cyclic redundancy code.
2357 * This function implements the CalculateCrc32 service.
2358 * See the Unified Extensible Firmware Interface (UEFI) specification
2361 * @data buffer with data
2362 * @data_size size of buffer in bytes
2363 * @crc32_p cyclic redundancy code
2364 * @return status code
2366 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2367 unsigned long data_size,
2370 EFI_ENTRY("%p, %ld", data, data_size);
2371 *crc32_p = crc32(0, data, data_size);
2372 return EFI_EXIT(EFI_SUCCESS);
2378 * This function implements the CopyMem service.
2379 * See the Unified Extensible Firmware Interface (UEFI) specification
2382 * @destination destination of the copy operation
2383 * @source source of the copy operation
2384 * @length number of bytes to copy
2386 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2389 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2390 memcpy(destination, source, length);
2391 EFI_EXIT(EFI_SUCCESS);
2395 * Fill memory with a byte value.
2397 * This function implements the SetMem service.
2398 * See the Unified Extensible Firmware Interface (UEFI) specification
2401 * @buffer buffer to fill
2402 * @size size of buffer in bytes
2403 * @value byte to copy to the buffer
2405 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2407 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2408 memset(buffer, value, size);
2409 EFI_EXIT(EFI_SUCCESS);
2413 * Open protocol interface on a handle.
2415 * @handler handler of a protocol
2416 * @protocol_interface interface implementing the protocol
2417 * @agent_handle handle of the driver
2418 * @controller_handle handle of the controller
2419 * @attributes attributes indicating how to open the protocol
2420 * @return status code
2422 static efi_status_t efi_protocol_open(
2423 struct efi_handler *handler,
2424 void **protocol_interface, void *agent_handle,
2425 void *controller_handle, uint32_t attributes)
2427 struct efi_open_protocol_info_item *item;
2428 struct efi_open_protocol_info_entry *match = NULL;
2429 bool opened_by_driver = false;
2430 bool opened_exclusive = false;
2432 /* If there is no agent, only return the interface */
2436 /* For TEST_PROTOCOL ignore interface attribute */
2437 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2438 *protocol_interface = NULL;
2441 * Check if the protocol is already opened by a driver with the same
2442 * attributes or opened exclusively
2444 list_for_each_entry(item, &handler->open_infos, link) {
2445 if (item->info.agent_handle == agent_handle) {
2446 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2447 (item->info.attributes == attributes))
2448 return EFI_ALREADY_STARTED;
2450 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2451 opened_exclusive = true;
2454 /* Only one controller can open the protocol exclusively */
2455 if (opened_exclusive && attributes &
2456 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2457 return EFI_ACCESS_DENIED;
2459 /* Prepare exclusive opening */
2460 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2461 /* Try to disconnect controllers */
2462 list_for_each_entry(item, &handler->open_infos, link) {
2463 if (item->info.attributes ==
2464 EFI_OPEN_PROTOCOL_BY_DRIVER)
2465 EFI_CALL(efi_disconnect_controller(
2466 item->info.controller_handle,
2467 item->info.agent_handle,
2470 opened_by_driver = false;
2471 /* Check if all controllers are disconnected */
2472 list_for_each_entry(item, &handler->open_infos, link) {
2473 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2474 opened_by_driver = true;
2476 /* Only one controller can be conncected */
2477 if (opened_by_driver)
2478 return EFI_ACCESS_DENIED;
2481 /* Find existing entry */
2482 list_for_each_entry(item, &handler->open_infos, link) {
2483 if (item->info.agent_handle == agent_handle &&
2484 item->info.controller_handle == controller_handle)
2485 match = &item->info;
2487 /* None found, create one */
2489 match = efi_create_open_info(handler);
2491 return EFI_OUT_OF_RESOURCES;
2494 match->agent_handle = agent_handle;
2495 match->controller_handle = controller_handle;
2496 match->attributes = attributes;
2497 match->open_count++;
2500 /* For TEST_PROTOCOL ignore interface attribute. */
2501 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2502 *protocol_interface = handler->protocol_interface;
2508 * Open protocol interface on a handle.
2510 * This function implements the OpenProtocol interface.
2511 * See the Unified Extensible Firmware Interface (UEFI) specification
2514 * @handle handle on which the protocol shall be opened
2515 * @protocol GUID of the protocol
2516 * @protocol_interface interface implementing the protocol
2517 * @agent_handle handle of the driver
2518 * @controller_handle handle of the controller
2519 * @attributes attributes indicating how to open the protocol
2520 * @return status code
2522 static efi_status_t EFIAPI efi_open_protocol(
2523 void *handle, const efi_guid_t *protocol,
2524 void **protocol_interface, void *agent_handle,
2525 void *controller_handle, uint32_t attributes)
2527 struct efi_handler *handler;
2528 efi_status_t r = EFI_INVALID_PARAMETER;
2530 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2531 protocol_interface, agent_handle, controller_handle,
2534 if (!handle || !protocol ||
2535 (!protocol_interface && attributes !=
2536 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2540 switch (attributes) {
2541 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2542 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2543 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2545 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2546 if (controller_handle == handle)
2549 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2550 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2551 /* Check that the controller handle is valid */
2552 if (!efi_search_obj(controller_handle))
2555 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2556 /* Check that the agent handle is valid */
2557 if (!efi_search_obj(agent_handle))
2564 r = efi_search_protocol(handle, protocol, &handler);
2565 if (r != EFI_SUCCESS)
2568 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2569 controller_handle, attributes);
2575 * Get interface of a protocol on a handle.
2577 * This function implements the HandleProtocol service.
2578 * See the Unified Extensible Firmware Interface (UEFI) specification
2581 * @handle handle on which the protocol shall be opened
2582 * @protocol GUID of the protocol
2583 * @protocol_interface interface implementing the protocol
2584 * @return status code
2586 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2587 const efi_guid_t *protocol,
2588 void **protocol_interface)
2590 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2591 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2594 static efi_status_t efi_bind_controller(
2595 efi_handle_t controller_handle,
2596 efi_handle_t driver_image_handle,
2597 struct efi_device_path *remain_device_path)
2599 struct efi_driver_binding_protocol *binding_protocol;
2602 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2603 &efi_guid_driver_binding_protocol,
2604 (void **)&binding_protocol,
2605 driver_image_handle, NULL,
2606 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2607 if (r != EFI_SUCCESS)
2609 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2611 remain_device_path));
2612 if (r == EFI_SUCCESS)
2613 r = EFI_CALL(binding_protocol->start(binding_protocol,
2615 remain_device_path));
2616 EFI_CALL(efi_close_protocol(driver_image_handle,
2617 &efi_guid_driver_binding_protocol,
2618 driver_image_handle, NULL));
2622 static efi_status_t efi_connect_single_controller(
2623 efi_handle_t controller_handle,
2624 efi_handle_t *driver_image_handle,
2625 struct efi_device_path *remain_device_path)
2627 efi_handle_t *buffer;
2631 size_t connected = 0;
2633 /* Get buffer with all handles with driver binding protocol */
2634 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2635 &efi_guid_driver_binding_protocol,
2636 NULL, &count, &buffer));
2637 if (r != EFI_SUCCESS)
2640 /* Context Override */
2641 if (driver_image_handle) {
2642 for (; *driver_image_handle; ++driver_image_handle) {
2643 for (i = 0; i < count; ++i) {
2644 if (buffer[i] == *driver_image_handle) {
2646 r = efi_bind_controller(
2648 *driver_image_handle,
2649 remain_device_path);
2651 * For drivers that do not support the
2652 * controller or are already connected
2653 * we receive an error code here.
2655 if (r == EFI_SUCCESS)
2663 * TODO: Some overrides are not yet implemented:
2664 * - Platform Driver Override
2665 * - Driver Family Override Search
2666 * - Bus Specific Driver Override
2669 /* Driver Binding Search */
2670 for (i = 0; i < count; ++i) {
2672 r = efi_bind_controller(controller_handle,
2674 remain_device_path);
2675 if (r == EFI_SUCCESS)
2680 efi_free_pool(buffer);
2682 return EFI_NOT_FOUND;
2687 * Connect a controller to a driver.
2689 * This function implements the ConnectController service.
2690 * See the Unified Extensible Firmware Interface (UEFI) specification
2693 * First all driver binding protocol handles are tried for binding drivers.
2694 * Afterwards all handles that have openened a protocol of the controller
2695 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2697 * @controller_handle handle of the controller
2698 * @driver_image_handle handle of the driver
2699 * @remain_device_path device path of a child controller
2700 * @recursive true to connect all child controllers
2701 * @return status code
2703 static efi_status_t EFIAPI efi_connect_controller(
2704 efi_handle_t controller_handle,
2705 efi_handle_t *driver_image_handle,
2706 struct efi_device_path *remain_device_path,
2710 efi_status_t ret = EFI_NOT_FOUND;
2711 struct efi_object *efiobj;
2713 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2714 remain_device_path, recursive);
2716 efiobj = efi_search_obj(controller_handle);
2718 ret = EFI_INVALID_PARAMETER;
2722 r = efi_connect_single_controller(controller_handle,
2723 driver_image_handle,
2724 remain_device_path);
2725 if (r == EFI_SUCCESS)
2728 struct efi_handler *handler;
2729 struct efi_open_protocol_info_item *item;
2731 list_for_each_entry(handler, &efiobj->protocols, link) {
2732 list_for_each_entry(item, &handler->open_infos, link) {
2733 if (item->info.attributes &
2734 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2735 r = EFI_CALL(efi_connect_controller(
2736 item->info.controller_handle,
2737 driver_image_handle,
2740 if (r == EFI_SUCCESS)
2746 /* Check for child controller specified by end node */
2747 if (ret != EFI_SUCCESS && remain_device_path &&
2748 remain_device_path->type == DEVICE_PATH_TYPE_END)
2751 return EFI_EXIT(ret);
2755 * Reinstall protocol interface.
2757 * This function implements the ReinstallProtocolInterface service.
2758 * See the Unified Extensible Firmware Interface (UEFI) specification
2761 * The old interface is uninstalled. The new interface is installed.
2762 * Drivers are connected.
2764 * @handle handle on which the protocol shall be
2766 * @protocol GUID of the protocol to be installed
2767 * @old_interface interface to be removed
2768 * @new_interface interface to be installed
2769 * @return status code
2771 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2772 efi_handle_t handle, const efi_guid_t *protocol,
2773 void *old_interface, void *new_interface)
2777 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2779 ret = EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2781 if (ret != EFI_SUCCESS)
2783 ret = EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2784 EFI_NATIVE_INTERFACE,
2786 if (ret != EFI_SUCCESS)
2789 * The returned status code has to be ignored.
2790 * Do not create an error if no suitable driver for the handle exists.
2792 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2794 return EFI_EXIT(ret);
2798 * Get all child controllers associated to a driver.
2799 * The allocated buffer has to be freed with free().
2801 * @efiobj handle of the controller
2802 * @driver_handle handle of the driver
2803 * @number_of_children number of child controllers
2804 * @child_handle_buffer handles of the the child controllers
2806 static efi_status_t efi_get_child_controllers(
2807 struct efi_object *efiobj,
2808 efi_handle_t driver_handle,
2809 efi_uintn_t *number_of_children,
2810 efi_handle_t **child_handle_buffer)
2812 struct efi_handler *handler;
2813 struct efi_open_protocol_info_item *item;
2814 efi_uintn_t count = 0, i;
2817 /* Count all child controller associations */
2818 list_for_each_entry(handler, &efiobj->protocols, link) {
2819 list_for_each_entry(item, &handler->open_infos, link) {
2820 if (item->info.agent_handle == driver_handle &&
2821 item->info.attributes &
2822 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2827 * Create buffer. In case of duplicate child controller assignments
2828 * the buffer will be too large. But that does not harm.
2830 *number_of_children = 0;
2831 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2832 if (!*child_handle_buffer)
2833 return EFI_OUT_OF_RESOURCES;
2834 /* Copy unique child handles */
2835 list_for_each_entry(handler, &efiobj->protocols, link) {
2836 list_for_each_entry(item, &handler->open_infos, link) {
2837 if (item->info.agent_handle == driver_handle &&
2838 item->info.attributes &
2839 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2840 /* Check this is a new child controller */
2842 for (i = 0; i < *number_of_children; ++i) {
2843 if ((*child_handle_buffer)[i] ==
2844 item->info.controller_handle)
2847 /* Copy handle to buffer */
2849 i = (*number_of_children)++;
2850 (*child_handle_buffer)[i] =
2851 item->info.controller_handle;
2860 * Disconnect a controller from a driver.
2862 * This function implements the DisconnectController service.
2863 * See the Unified Extensible Firmware Interface (UEFI) specification
2866 * @controller_handle handle of the controller
2867 * @driver_image_handle handle of the driver
2868 * @child_handle handle of the child to destroy
2869 * @return status code
2871 static efi_status_t EFIAPI efi_disconnect_controller(
2872 efi_handle_t controller_handle,
2873 efi_handle_t driver_image_handle,
2874 efi_handle_t child_handle)
2876 struct efi_driver_binding_protocol *binding_protocol;
2877 efi_handle_t *child_handle_buffer = NULL;
2878 size_t number_of_children = 0;
2880 size_t stop_count = 0;
2881 struct efi_object *efiobj;
2883 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2886 efiobj = efi_search_obj(controller_handle);
2888 r = EFI_INVALID_PARAMETER;
2892 if (child_handle && !efi_search_obj(child_handle)) {
2893 r = EFI_INVALID_PARAMETER;
2897 /* If no driver handle is supplied, disconnect all drivers */
2898 if (!driver_image_handle) {
2899 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2903 /* Create list of child handles */
2905 number_of_children = 1;
2906 child_handle_buffer = &child_handle;
2908 efi_get_child_controllers(efiobj,
2909 driver_image_handle,
2910 &number_of_children,
2911 &child_handle_buffer);
2914 /* Get the driver binding protocol */
2915 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2916 &efi_guid_driver_binding_protocol,
2917 (void **)&binding_protocol,
2918 driver_image_handle, NULL,
2919 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2920 if (r != EFI_SUCCESS)
2922 /* Remove the children */
2923 if (number_of_children) {
2924 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2927 child_handle_buffer));
2928 if (r == EFI_SUCCESS)
2931 /* Remove the driver */
2933 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2936 if (r == EFI_SUCCESS)
2938 EFI_CALL(efi_close_protocol(driver_image_handle,
2939 &efi_guid_driver_binding_protocol,
2940 driver_image_handle, NULL));
2948 free(child_handle_buffer);
2952 static const struct efi_boot_services efi_boot_services = {
2954 .headersize = sizeof(struct efi_table_hdr),
2956 .raise_tpl = efi_raise_tpl,
2957 .restore_tpl = efi_restore_tpl,
2958 .allocate_pages = efi_allocate_pages_ext,
2959 .free_pages = efi_free_pages_ext,
2960 .get_memory_map = efi_get_memory_map_ext,
2961 .allocate_pool = efi_allocate_pool_ext,
2962 .free_pool = efi_free_pool_ext,
2963 .create_event = efi_create_event_ext,
2964 .set_timer = efi_set_timer_ext,
2965 .wait_for_event = efi_wait_for_event,
2966 .signal_event = efi_signal_event_ext,
2967 .close_event = efi_close_event,
2968 .check_event = efi_check_event,
2969 .install_protocol_interface = efi_install_protocol_interface,
2970 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2971 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2972 .handle_protocol = efi_handle_protocol,
2974 .register_protocol_notify = efi_register_protocol_notify,
2975 .locate_handle = efi_locate_handle_ext,
2976 .locate_device_path = efi_locate_device_path,
2977 .install_configuration_table = efi_install_configuration_table_ext,
2978 .load_image = efi_load_image,
2979 .start_image = efi_start_image,
2981 .unload_image = efi_unload_image,
2982 .exit_boot_services = efi_exit_boot_services,
2983 .get_next_monotonic_count = efi_get_next_monotonic_count,
2985 .set_watchdog_timer = efi_set_watchdog_timer,
2986 .connect_controller = efi_connect_controller,
2987 .disconnect_controller = efi_disconnect_controller,
2988 .open_protocol = efi_open_protocol,
2989 .close_protocol = efi_close_protocol,
2990 .open_protocol_information = efi_open_protocol_information,
2991 .protocols_per_handle = efi_protocols_per_handle,
2992 .locate_handle_buffer = efi_locate_handle_buffer,
2993 .locate_protocol = efi_locate_protocol,
2994 .install_multiple_protocol_interfaces =
2995 efi_install_multiple_protocol_interfaces,
2996 .uninstall_multiple_protocol_interfaces =
2997 efi_uninstall_multiple_protocol_interfaces,
2998 .calculate_crc32 = efi_calculate_crc32,
2999 .copy_mem = efi_copy_mem,
3000 .set_mem = efi_set_mem,
3001 .create_event_ex = efi_create_event_ex,
3004 static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3006 struct efi_system_table __efi_runtime_data systab = {
3008 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3009 .revision = 2 << 16 | 70, /* 2.7 */
3010 .headersize = sizeof(struct efi_table_hdr),
3012 .fw_vendor = (long)firmware_vendor,
3013 .con_in = (void *)&efi_con_in,
3014 .con_out = (void *)&efi_con_out,
3015 .std_err = (void *)&efi_con_out,
3016 .runtime = (void *)&efi_runtime_services,
3017 .boottime = (void *)&efi_boot_services,
3019 .tables = (void *)efi_conf_table,