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 list_del(&handler->link);
502 * Delete all protocols from a handle.
504 * @handle handle from which the protocols shall be deleted
505 * @return status code
507 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
509 struct efi_object *efiobj;
510 struct efi_handler *protocol;
511 struct efi_handler *pos;
513 efiobj = efi_search_obj(handle);
515 return EFI_INVALID_PARAMETER;
516 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
519 ret = efi_remove_protocol(handle, protocol->guid,
520 protocol->protocol_interface);
521 if (ret != EFI_SUCCESS)
530 * @handle handle to delete
532 void efi_delete_handle(struct efi_object *obj)
536 efi_remove_all_protocols(obj->handle);
537 list_del(&obj->link);
542 * Check if a pointer is a valid event.
544 * @event pointer to check
545 * @return status code
547 static efi_status_t efi_is_event(const struct efi_event *event)
549 const struct efi_event *evt;
552 return EFI_INVALID_PARAMETER;
553 list_for_each_entry(evt, &efi_events, link) {
557 return EFI_INVALID_PARAMETER;
563 * This function is used inside U-Boot code to create an event.
565 * For the API function implementing the CreateEvent service see
566 * efi_create_event_ext.
568 * @type type of the event to create
569 * @notify_tpl task priority level of the event
570 * @notify_function notification function of the event
571 * @notify_context pointer passed to the notification function
572 * @event created event
573 * @return status code
575 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
576 void (EFIAPI *notify_function) (
577 struct efi_event *event,
579 void *notify_context, efi_guid_t *group,
580 struct efi_event **event)
582 struct efi_event *evt;
585 return EFI_INVALID_PARAMETER;
587 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
588 return EFI_INVALID_PARAMETER;
590 if ((type & (EVT_NOTIFY_SIGNAL | EVT_NOTIFY_WAIT)) &&
591 notify_function == NULL)
592 return EFI_INVALID_PARAMETER;
594 evt = calloc(1, sizeof(struct efi_event));
596 return EFI_OUT_OF_RESOURCES;
598 evt->notify_tpl = notify_tpl;
599 evt->notify_function = notify_function;
600 evt->notify_context = notify_context;
602 /* Disable timers on bootup */
603 evt->trigger_next = -1ULL;
604 evt->is_queued = false;
605 evt->is_signaled = false;
606 list_add_tail(&evt->link, &efi_events);
612 * Create an event in a group.
614 * This function implements the CreateEventEx service.
615 * See the Unified Extensible Firmware Interface (UEFI) specification
617 * TODO: Support event groups
619 * @type type of the event to create
620 * @notify_tpl task priority level of the event
621 * @notify_function notification function of the event
622 * @notify_context pointer passed to the notification function
623 * @event created event
624 * @event_group event group
625 * @return status code
627 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
628 void (EFIAPI *notify_function) (
629 struct efi_event *event,
631 void *notify_context,
632 efi_guid_t *event_group,
633 struct efi_event **event)
635 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
636 notify_context, event_group);
637 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
638 notify_context, event_group, event));
644 * This function implements the CreateEvent service.
645 * See the Unified Extensible Firmware Interface (UEFI) specification
648 * @type type of the event to create
649 * @notify_tpl task priority level of the event
650 * @notify_function notification function of the event
651 * @notify_context pointer passed to the notification function
652 * @event created event
653 * @return status code
655 static efi_status_t EFIAPI efi_create_event_ext(
656 uint32_t type, efi_uintn_t notify_tpl,
657 void (EFIAPI *notify_function) (
658 struct efi_event *event,
660 void *notify_context, struct efi_event **event)
662 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
664 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
665 notify_context, NULL, event));
669 * Check if a timer event has occurred or a queued notification function should
672 * Our timers have to work without interrupts, so we check whenever keyboard
673 * input or disk accesses happen if enough time elapsed for them to fire.
675 void efi_timer_check(void)
677 struct efi_event *evt;
678 u64 now = timer_get_us();
680 list_for_each_entry(evt, &efi_events, link) {
682 efi_queue_event(evt, true);
683 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
685 switch (evt->trigger_type) {
686 case EFI_TIMER_RELATIVE:
687 evt->trigger_type = EFI_TIMER_STOP;
689 case EFI_TIMER_PERIODIC:
690 evt->trigger_next += evt->trigger_time;
695 evt->is_signaled = false;
696 efi_signal_event(evt, true);
702 * Set the trigger time for a timer event or stop the event.
704 * This is the function for internal usage in U-Boot. For the API function
705 * implementing the SetTimer service see efi_set_timer_ext.
707 * @event event for which the timer is set
708 * @type type of the timer
709 * @trigger_time trigger period in multiples of 100ns
710 * @return status code
712 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
713 uint64_t trigger_time)
715 /* Check that the event is valid */
716 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
717 return EFI_INVALID_PARAMETER;
720 * The parameter defines a multiple of 100ns.
721 * We use multiples of 1000ns. So divide by 10.
723 do_div(trigger_time, 10);
727 event->trigger_next = -1ULL;
729 case EFI_TIMER_PERIODIC:
730 case EFI_TIMER_RELATIVE:
731 event->trigger_next = timer_get_us() + trigger_time;
734 return EFI_INVALID_PARAMETER;
736 event->trigger_type = type;
737 event->trigger_time = trigger_time;
738 event->is_signaled = false;
743 * Set the trigger time for a timer event or stop the event.
745 * This function implements the SetTimer service.
746 * See the Unified Extensible Firmware Interface (UEFI) specification
749 * @event event for which the timer is set
750 * @type type of the timer
751 * @trigger_time trigger period in multiples of 100ns
752 * @return status code
754 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
755 enum efi_timer_delay type,
756 uint64_t trigger_time)
758 EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
759 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
763 * Wait for events to be signaled.
765 * This function implements the WaitForEvent service.
766 * See the Unified Extensible Firmware Interface (UEFI) specification
769 * @num_events number of events to be waited for
770 * @events events to be waited for
771 * @index index of the event that was signaled
772 * @return status code
774 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
775 struct efi_event **event,
780 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
782 /* Check parameters */
783 if (!num_events || !event)
784 return EFI_EXIT(EFI_INVALID_PARAMETER);
786 if (efi_tpl != TPL_APPLICATION)
787 return EFI_EXIT(EFI_UNSUPPORTED);
788 for (i = 0; i < num_events; ++i) {
789 if (efi_is_event(event[i]) != EFI_SUCCESS)
790 return EFI_EXIT(EFI_INVALID_PARAMETER);
791 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
792 return EFI_EXIT(EFI_INVALID_PARAMETER);
793 if (!event[i]->is_signaled)
794 efi_queue_event(event[i], true);
797 /* Wait for signal */
799 for (i = 0; i < num_events; ++i) {
800 if (event[i]->is_signaled)
803 /* Allow events to occur. */
809 * Reset the signal which is passed to the caller to allow periodic
812 event[i]->is_signaled = false;
816 return EFI_EXIT(EFI_SUCCESS);
820 * Signal an EFI event.
822 * This function implements the SignalEvent service.
823 * See the Unified Extensible Firmware Interface (UEFI) specification
826 * This functions sets the signaled state of the event and queues the
827 * notification function for execution.
829 * @event event to signal
830 * @return status code
832 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
834 EFI_ENTRY("%p", event);
835 if (efi_is_event(event) != EFI_SUCCESS)
836 return EFI_EXIT(EFI_INVALID_PARAMETER);
837 efi_signal_event(event, true);
838 return EFI_EXIT(EFI_SUCCESS);
842 * Close an EFI event.
844 * This function implements the CloseEvent service.
845 * See the Unified Extensible Firmware Interface (UEFI) specification
848 * @event event to close
849 * @return status code
851 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
853 EFI_ENTRY("%p", event);
854 if (efi_is_event(event) != EFI_SUCCESS)
855 return EFI_EXIT(EFI_INVALID_PARAMETER);
856 list_del(&event->link);
858 return EFI_EXIT(EFI_SUCCESS);
862 * Check if an event is signaled.
864 * This function implements the CheckEvent service.
865 * See the Unified Extensible Firmware Interface (UEFI) specification
868 * If an event is not signaled yet, the notification function is queued.
869 * The signaled state is cleared.
871 * @event event to check
872 * @return status code
874 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
876 EFI_ENTRY("%p", event);
878 if (efi_is_event(event) != EFI_SUCCESS ||
879 event->type & EVT_NOTIFY_SIGNAL)
880 return EFI_EXIT(EFI_INVALID_PARAMETER);
881 if (!event->is_signaled)
882 efi_queue_event(event, true);
883 if (event->is_signaled) {
884 event->is_signaled = false;
885 return EFI_EXIT(EFI_SUCCESS);
887 return EFI_EXIT(EFI_NOT_READY);
891 * Find the internal EFI object for a handle.
893 * @handle handle to find
896 struct efi_object *efi_search_obj(const efi_handle_t handle)
898 struct efi_object *efiobj;
900 list_for_each_entry(efiobj, &efi_obj_list, link) {
901 if (efiobj->handle == handle)
909 * Create open protocol info entry and add it to a protocol.
911 * @handler handler of a protocol
912 * @return open protocol info entry
914 static struct efi_open_protocol_info_entry *efi_create_open_info(
915 struct efi_handler *handler)
917 struct efi_open_protocol_info_item *item;
919 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
922 /* Append the item to the open protocol info list. */
923 list_add_tail(&item->link, &handler->open_infos);
929 * Remove an open protocol info entry from a protocol.
931 * @handler handler of a protocol
932 * @return status code
934 static efi_status_t efi_delete_open_info(
935 struct efi_open_protocol_info_item *item)
937 list_del(&item->link);
943 * Install new protocol on a handle.
945 * @handle handle on which the protocol shall be installed
946 * @protocol GUID of the protocol to be installed
947 * @protocol_interface interface of the protocol implementation
948 * @return status code
950 efi_status_t efi_add_protocol(const efi_handle_t handle,
951 const efi_guid_t *protocol,
952 void *protocol_interface)
954 struct efi_object *efiobj;
955 struct efi_handler *handler;
958 efiobj = efi_search_obj(handle);
960 return EFI_INVALID_PARAMETER;
961 ret = efi_search_protocol(handle, protocol, NULL);
962 if (ret != EFI_NOT_FOUND)
963 return EFI_INVALID_PARAMETER;
964 handler = calloc(1, sizeof(struct efi_handler));
966 return EFI_OUT_OF_RESOURCES;
967 handler->guid = protocol;
968 handler->protocol_interface = protocol_interface;
969 INIT_LIST_HEAD(&handler->open_infos);
970 list_add_tail(&handler->link, &efiobj->protocols);
971 if (!guidcmp(&efi_guid_device_path, protocol))
972 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
977 * Install protocol interface.
979 * This function implements the InstallProtocolInterface service.
980 * See the Unified Extensible Firmware Interface (UEFI) specification
983 * @handle handle on which the protocol shall be installed
984 * @protocol GUID of the protocol to be installed
985 * @protocol_interface_type type of the interface to be installed,
986 * always EFI_NATIVE_INTERFACE
987 * @protocol_interface interface of the protocol implementation
988 * @return status code
990 static efi_status_t EFIAPI efi_install_protocol_interface(
991 void **handle, const efi_guid_t *protocol,
992 int protocol_interface_type, void *protocol_interface)
996 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
999 if (!handle || !protocol ||
1000 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1001 r = EFI_INVALID_PARAMETER;
1005 /* Create new handle if requested. */
1007 r = efi_create_handle(handle);
1008 if (r != EFI_SUCCESS)
1010 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1013 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1016 /* Add new protocol */
1017 r = efi_add_protocol(*handle, protocol, protocol_interface);
1023 * Reinstall protocol interface.
1025 * This function implements the ReinstallProtocolInterface service.
1026 * See the Unified Extensible Firmware Interface (UEFI) specification
1029 * @handle handle on which the protocol shall be
1031 * @protocol GUID of the protocol to be installed
1032 * @old_interface interface to be removed
1033 * @new_interface interface to be installed
1034 * @return status code
1036 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
1037 efi_handle_t handle, const efi_guid_t *protocol,
1038 void *old_interface, void *new_interface)
1040 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
1042 return EFI_EXIT(EFI_ACCESS_DENIED);
1046 * Get all drivers associated to a controller.
1047 * The allocated buffer has to be freed with free().
1049 * @efiobj handle of the controller
1050 * @protocol protocol guid (optional)
1051 * @number_of_drivers number of child controllers
1052 * @driver_handle_buffer handles of the the drivers
1053 * @return status code
1055 static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1056 const efi_guid_t *protocol,
1057 efi_uintn_t *number_of_drivers,
1058 efi_handle_t **driver_handle_buffer)
1060 struct efi_handler *handler;
1061 struct efi_open_protocol_info_item *item;
1062 efi_uintn_t count = 0, i;
1065 /* Count all driver associations */
1066 list_for_each_entry(handler, &efiobj->protocols, link) {
1067 if (protocol && guidcmp(handler->guid, protocol))
1069 list_for_each_entry(item, &handler->open_infos, link) {
1070 if (item->info.attributes &
1071 EFI_OPEN_PROTOCOL_BY_DRIVER)
1076 * Create buffer. In case of duplicate driver assignments the buffer
1077 * will be too large. But that does not harm.
1079 *number_of_drivers = 0;
1080 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1081 if (!*driver_handle_buffer)
1082 return EFI_OUT_OF_RESOURCES;
1083 /* Collect unique driver handles */
1084 list_for_each_entry(handler, &efiobj->protocols, link) {
1085 if (protocol && guidcmp(handler->guid, protocol))
1087 list_for_each_entry(item, &handler->open_infos, link) {
1088 if (item->info.attributes &
1089 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1090 /* Check this is a new driver */
1092 for (i = 0; i < *number_of_drivers; ++i) {
1093 if ((*driver_handle_buffer)[i] ==
1094 item->info.agent_handle)
1097 /* Copy handle to buffer */
1099 i = (*number_of_drivers)++;
1100 (*driver_handle_buffer)[i] =
1101 item->info.agent_handle;
1110 * Disconnect all drivers from a controller.
1112 * This function implements the DisconnectController service.
1113 * See the Unified Extensible Firmware Interface (UEFI) specification
1116 * @efiobj handle of the controller
1117 * @protocol protocol guid (optional)
1118 * @child_handle handle of the child to destroy
1119 * @return status code
1121 static efi_status_t efi_disconnect_all_drivers(
1122 struct efi_object *efiobj,
1123 const efi_guid_t *protocol,
1124 efi_handle_t child_handle)
1126 efi_uintn_t number_of_drivers;
1127 efi_handle_t *driver_handle_buffer;
1128 efi_status_t r, ret;
1130 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1131 &driver_handle_buffer);
1132 if (ret != EFI_SUCCESS)
1135 ret = EFI_NOT_FOUND;
1136 while (number_of_drivers) {
1137 r = EFI_CALL(efi_disconnect_controller(
1139 driver_handle_buffer[--number_of_drivers],
1141 if (r == EFI_SUCCESS)
1144 free(driver_handle_buffer);
1149 * Uninstall protocol interface.
1151 * This function implements the UninstallProtocolInterface service.
1152 * See the Unified Extensible Firmware Interface (UEFI) specification
1155 * @handle handle from which the protocol shall be removed
1156 * @protocol GUID of the protocol to be removed
1157 * @protocol_interface interface to be removed
1158 * @return status code
1160 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
1161 efi_handle_t handle, const efi_guid_t *protocol,
1162 void *protocol_interface)
1164 struct efi_object *efiobj;
1165 struct efi_handler *handler;
1166 struct efi_open_protocol_info_item *item;
1167 struct efi_open_protocol_info_item *pos;
1170 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1173 efiobj = efi_search_obj(handle);
1175 r = EFI_INVALID_PARAMETER;
1178 /* Find the protocol on the handle */
1179 r = efi_search_protocol(handle, protocol, &handler);
1180 if (r != EFI_SUCCESS)
1182 /* Disconnect controllers */
1183 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1184 if (!list_empty(&handler->open_infos)) {
1185 r = EFI_ACCESS_DENIED;
1188 /* Close protocol */
1189 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1190 if (item->info.attributes ==
1191 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1192 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1193 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1194 list_del(&item->link);
1196 if (!list_empty(&handler->open_infos)) {
1197 r = EFI_ACCESS_DENIED;
1200 r = efi_remove_protocol(handle, protocol, protocol_interface);
1206 * Register an event for notification when a protocol is installed.
1208 * This function implements the RegisterProtocolNotify service.
1209 * See the Unified Extensible Firmware Interface (UEFI) specification
1212 * @protocol GUID of the protocol whose installation shall be
1214 * @event event to be signaled upon installation of the protocol
1215 * @registration key for retrieving the registration information
1216 * @return status code
1218 static efi_status_t EFIAPI efi_register_protocol_notify(
1219 const efi_guid_t *protocol,
1220 struct efi_event *event,
1221 void **registration)
1223 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1224 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1228 * Determine if an EFI handle implements a protocol.
1230 * See the documentation of the LocateHandle service in the UEFI specification.
1232 * @search_type selection criterion
1233 * @protocol GUID of the protocol
1234 * @search_key registration key
1236 * @return 0 if the handle implements the protocol
1238 static int efi_search(enum efi_locate_search_type search_type,
1239 const efi_guid_t *protocol, void *search_key,
1240 struct efi_object *efiobj)
1244 switch (search_type) {
1247 case BY_REGISTER_NOTIFY:
1248 /* TODO: RegisterProtocolNotify is not implemented yet */
1251 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1252 return (ret != EFI_SUCCESS);
1254 /* Invalid search type */
1260 * Locate handles implementing a protocol.
1262 * This function is meant for U-Boot internal calls. For the API implementation
1263 * of the LocateHandle service see efi_locate_handle_ext.
1265 * @search_type selection criterion
1266 * @protocol GUID of the protocol
1267 * @search_key registration key
1268 * @buffer_size size of the buffer to receive the handles in bytes
1269 * @buffer buffer to receive the relevant handles
1270 * @return status code
1272 static efi_status_t efi_locate_handle(
1273 enum efi_locate_search_type search_type,
1274 const efi_guid_t *protocol, void *search_key,
1275 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1277 struct efi_object *efiobj;
1278 efi_uintn_t size = 0;
1280 /* Check parameters */
1281 switch (search_type) {
1284 case BY_REGISTER_NOTIFY:
1286 return EFI_INVALID_PARAMETER;
1287 /* RegisterProtocolNotify is not implemented yet */
1288 return EFI_UNSUPPORTED;
1291 return EFI_INVALID_PARAMETER;
1294 return EFI_INVALID_PARAMETER;
1298 * efi_locate_handle_buffer uses this function for
1299 * the calculation of the necessary buffer size.
1300 * So do not require a buffer for buffersize == 0.
1302 if (!buffer_size || (*buffer_size && !buffer))
1303 return EFI_INVALID_PARAMETER;
1305 /* Count how much space we need */
1306 list_for_each_entry(efiobj, &efi_obj_list, link) {
1307 if (!efi_search(search_type, protocol, search_key, efiobj))
1308 size += sizeof(void *);
1311 if (*buffer_size < size) {
1312 *buffer_size = size;
1313 return EFI_BUFFER_TOO_SMALL;
1316 *buffer_size = size;
1318 return EFI_NOT_FOUND;
1320 /* Then fill the array */
1321 list_for_each_entry(efiobj, &efi_obj_list, link) {
1322 if (!efi_search(search_type, protocol, search_key, efiobj))
1323 *buffer++ = efiobj->handle;
1330 * Locate handles implementing a protocol.
1332 * This function implements the LocateHandle service.
1333 * See the Unified Extensible Firmware Interface (UEFI) specification
1336 * @search_type selection criterion
1337 * @protocol GUID of the protocol
1338 * @search_key registration key
1339 * @buffer_size size of the buffer to receive the handles in bytes
1340 * @buffer buffer to receive the relevant handles
1341 * @return 0 if the handle implements the protocol
1343 static efi_status_t EFIAPI efi_locate_handle_ext(
1344 enum efi_locate_search_type search_type,
1345 const efi_guid_t *protocol, void *search_key,
1346 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1348 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1349 buffer_size, buffer);
1351 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1352 buffer_size, buffer));
1355 /* Collapses configuration table entries, removing index i */
1356 static void efi_remove_configuration_table(int i)
1358 struct efi_configuration_table *this = &efi_conf_table[i];
1359 struct efi_configuration_table *next = &efi_conf_table[i + 1];
1360 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1362 memmove(this, next, (ulong)end - (ulong)next);
1367 * Adds, updates, or removes a configuration table.
1369 * This function is used for internal calls. For the API implementation of the
1370 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1372 * @guid GUID of the installed table
1373 * @table table to be installed
1374 * @return status code
1376 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1379 struct efi_event *evt;
1383 return EFI_INVALID_PARAMETER;
1385 /* Check for guid override */
1386 for (i = 0; i < systab.nr_tables; i++) {
1387 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1389 efi_conf_table[i].table = table;
1391 efi_remove_configuration_table(i);
1397 return EFI_NOT_FOUND;
1399 /* No override, check for overflow */
1400 if (i >= ARRAY_SIZE(efi_conf_table))
1401 return EFI_OUT_OF_RESOURCES;
1403 /* Add a new entry */
1404 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1405 efi_conf_table[i].table = table;
1406 systab.nr_tables = i + 1;
1409 /* Notify that the configuration table was changed */
1410 list_for_each_entry(evt, &efi_events, link) {
1411 if (evt->group && !guidcmp(evt->group, guid)) {
1412 efi_signal_event(evt, false);
1421 * Adds, updates, or removes a configuration table.
1423 * This function implements the InstallConfigurationTable service.
1424 * See the Unified Extensible Firmware Interface (UEFI) specification
1427 * @guid GUID of the installed table
1428 * @table table to be installed
1429 * @return status code
1431 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1434 EFI_ENTRY("%pUl, %p", guid, table);
1435 return EFI_EXIT(efi_install_configuration_table(guid, table));
1439 * Initialize a loaded_image_info + loaded_image_info object with correct
1440 * protocols, boot-device, etc.
1442 * @info loaded image info to be passed to the entry point of the
1444 * @obj internal object associated with the loaded image
1445 * @device_path device path of the loaded image
1446 * @file_path file path of the loaded image
1447 * @return status code
1449 efi_status_t efi_setup_loaded_image(
1450 struct efi_loaded_image *info, struct efi_object *obj,
1451 struct efi_device_path *device_path,
1452 struct efi_device_path *file_path)
1456 /* Add internal object to object list */
1457 efi_add_handle(obj);
1458 /* efi_exit() assumes that the handle points to the info */
1461 info->file_path = file_path;
1464 info->device_handle = efi_dp_find_obj(device_path, NULL);
1466 * When asking for the device path interface, return
1467 * bootefi_device_path
1469 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1471 if (ret != EFI_SUCCESS)
1476 * When asking for the loaded_image interface, just
1477 * return handle which points to loaded_image_info
1479 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1480 if (ret != EFI_SUCCESS)
1483 ret = efi_add_protocol(obj->handle,
1484 &efi_guid_device_path_to_text_protocol,
1485 (void *)&efi_device_path_to_text);
1486 if (ret != EFI_SUCCESS)
1489 ret = efi_add_protocol(obj->handle,
1490 &efi_guid_device_path_utilities_protocol,
1491 (void *)&efi_device_path_utilities);
1492 if (ret != EFI_SUCCESS)
1497 printf("ERROR: Failure to install protocols for loaded image\n");
1502 * Load an image using a file path.
1504 * @file_path the path of the image to load
1505 * @buffer buffer containing the loaded image
1506 * @return status code
1508 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1511 struct efi_file_info *info = NULL;
1512 struct efi_file_handle *f;
1513 static efi_status_t ret;
1516 f = efi_file_from_path(file_path);
1518 return EFI_DEVICE_ERROR;
1521 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1523 if (ret == EFI_BUFFER_TOO_SMALL) {
1525 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1528 if (ret != EFI_SUCCESS)
1531 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1535 bs = info->file_size;
1536 EFI_CALL(ret = f->read(f, &bs, *buffer));
1540 EFI_CALL(f->close(f));
1542 if (ret != EFI_SUCCESS) {
1543 efi_free_pool(*buffer);
1551 * Load an EFI image into memory.
1553 * This function implements the LoadImage service.
1554 * See the Unified Extensible Firmware Interface (UEFI) specification
1557 * @boot_policy true for request originating from the boot manager
1558 * @parent_image the caller's image handle
1559 * @file_path the path of the image to load
1560 * @source_buffer memory location from which the image is installed
1561 * @source_size size of the memory area from which the image is
1563 * @image_handle handle for the newly installed image
1564 * @return status code
1566 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1567 efi_handle_t parent_image,
1568 struct efi_device_path *file_path,
1569 void *source_buffer,
1570 efi_uintn_t source_size,
1571 efi_handle_t *image_handle)
1573 struct efi_loaded_image *info;
1574 struct efi_object *obj;
1577 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1578 file_path, source_buffer, source_size, image_handle);
1580 if (!image_handle || !parent_image) {
1581 ret = EFI_INVALID_PARAMETER;
1585 if (!source_buffer && !file_path) {
1586 ret = EFI_NOT_FOUND;
1590 info = calloc(1, sizeof(*info));
1592 ret = EFI_OUT_OF_RESOURCES;
1595 obj = calloc(1, sizeof(*obj));
1598 ret = EFI_OUT_OF_RESOURCES;
1602 if (!source_buffer) {
1603 struct efi_device_path *dp, *fp;
1605 ret = efi_load_image_from_path(file_path, &source_buffer);
1606 if (ret != EFI_SUCCESS)
1609 * split file_path which contains both the device and
1612 efi_dp_split_file_path(file_path, &dp, &fp);
1613 ret = efi_setup_loaded_image(info, obj, dp, fp);
1614 if (ret != EFI_SUCCESS)
1617 /* In this case, file_path is the "device" path, ie.
1618 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1620 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1621 if (ret != EFI_SUCCESS)
1624 info->reserved = efi_load_pe(source_buffer, info);
1625 if (!info->reserved) {
1626 ret = EFI_UNSUPPORTED;
1629 info->system_table = &systab;
1630 info->parent_handle = parent_image;
1631 *image_handle = obj->handle;
1632 return EFI_EXIT(EFI_SUCCESS);
1635 efi_delete_handle(obj);
1637 return EFI_EXIT(ret);
1641 * Call the entry point of an image.
1643 * This function implements the StartImage service.
1644 * See the Unified Extensible Firmware Interface (UEFI) specification
1647 * @image_handle handle of the image
1648 * @exit_data_size size of the buffer
1649 * @exit_data buffer to receive the exit data of the called image
1650 * @return status code
1652 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1653 unsigned long *exit_data_size,
1656 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1657 struct efi_system_table *st);
1658 struct efi_loaded_image *info = image_handle;
1661 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1662 entry = info->reserved;
1664 efi_is_direct_boot = false;
1666 /* call the image! */
1667 if (setjmp(&info->exit_jmp)) {
1669 * We called the entry point of the child image with EFI_CALL
1670 * in the lines below. The child image called the Exit() boot
1671 * service efi_exit() which executed the long jump that brought
1672 * us to the current line. This implies that the second half
1673 * of the EFI_CALL macro has not been executed.
1677 * efi_exit() called efi_restore_gd(). We have to undo this
1678 * otherwise __efi_entry_check() will put the wrong value into
1684 * To get ready to call EFI_EXIT below we have to execute the
1685 * missed out steps of EFI_CALL.
1687 assert(__efi_entry_check());
1688 debug("%sEFI: %lu returned by started image\n",
1689 __efi_nesting_dec(),
1690 (unsigned long)((uintptr_t)info->exit_status &
1692 return EFI_EXIT(info->exit_status);
1695 ret = EFI_CALL(entry(image_handle, &systab));
1698 * Usually UEFI applications call Exit() instead of returning.
1699 * But because the world doesn not consist of ponies and unicorns,
1700 * we're happy to emulate that behavior on behalf of a payload
1703 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
1707 * Leave an EFI application or driver.
1709 * This function implements the Exit service.
1710 * See the Unified Extensible Firmware Interface (UEFI) specification
1713 * @image_handle handle of the application or driver that is exiting
1714 * @exit_status status code
1715 * @exit_data_size size of the buffer in bytes
1716 * @exit_data buffer with data describing an error
1717 * @return status code
1719 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1720 efi_status_t exit_status,
1721 unsigned long exit_data_size,
1725 * We require that the handle points to the original loaded
1726 * image protocol interface.
1728 * For getting the longjmp address this is safer than locating
1729 * the protocol because the protocol may have been reinstalled
1730 * pointing to another memory location.
1732 * TODO: We should call the unload procedure of the loaded
1735 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
1737 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1738 exit_data_size, exit_data);
1740 /* Make sure entry/exit counts for EFI world cross-overs match */
1741 EFI_EXIT(exit_status);
1744 * But longjmp out with the U-Boot gd, not the application's, as
1745 * the other end is a setjmp call inside EFI context.
1749 loaded_image_info->exit_status = exit_status;
1750 longjmp(&loaded_image_info->exit_jmp, 1);
1752 panic("EFI application exited");
1756 * Unload an EFI image.
1758 * This function implements the UnloadImage service.
1759 * See the Unified Extensible Firmware Interface (UEFI) specification
1762 * @image_handle handle of the image to be unloaded
1763 * @return status code
1765 static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1767 struct efi_object *efiobj;
1769 EFI_ENTRY("%p", image_handle);
1770 efiobj = efi_search_obj(image_handle);
1772 list_del(&efiobj->link);
1774 return EFI_EXIT(EFI_SUCCESS);
1778 * Fix up caches for EFI payloads if necessary.
1780 static void efi_exit_caches(void)
1782 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1784 * Grub on 32bit ARM needs to have caches disabled before jumping into
1785 * a zImage, but does not know of all cache layers. Give it a hand.
1787 if (efi_is_direct_boot)
1788 cleanup_before_linux();
1793 * Stop all boot services.
1795 * This function implements the ExitBootServices service.
1796 * See the Unified Extensible Firmware Interface (UEFI) specification
1799 * All timer events are disabled.
1800 * For exit boot services events the notification function is called.
1801 * The boot services are disabled in the system table.
1803 * @image_handle handle of the loaded image
1804 * @map_key key of the memory map
1805 * @return status code
1807 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1808 unsigned long map_key)
1810 struct efi_event *evt;
1812 EFI_ENTRY("%p, %ld", image_handle, map_key);
1814 /* Make sure that notification functions are not called anymore */
1815 efi_tpl = TPL_HIGH_LEVEL;
1817 /* Check if ExitBootServices has already been called */
1818 if (!systab.boottime)
1819 return EFI_EXIT(EFI_SUCCESS);
1821 /* Add related events to the event group */
1822 list_for_each_entry(evt, &efi_events, link) {
1823 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1824 evt->group = &efi_guid_event_group_exit_boot_services;
1826 /* Notify that ExitBootServices is invoked. */
1827 list_for_each_entry(evt, &efi_events, link) {
1829 !guidcmp(evt->group,
1830 &efi_guid_event_group_exit_boot_services)) {
1831 efi_signal_event(evt, false);
1836 /* TODO Should persist EFI variables here */
1838 board_quiesce_devices();
1840 /* Fix up caches for EFI payloads if necessary */
1843 /* This stops all lingering devices */
1844 bootm_disable_interrupts();
1846 /* Disable boottime services */
1847 systab.con_in_handle = NULL;
1848 systab.con_in = NULL;
1849 systab.con_out_handle = NULL;
1850 systab.con_out = NULL;
1851 systab.stderr_handle = NULL;
1852 systab.std_err = NULL;
1853 systab.boottime = NULL;
1855 /* Recalculate CRC32 */
1856 systab.hdr.crc32 = 0;
1857 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1858 sizeof(struct efi_system_table));
1860 /* Give the payload some time to boot */
1861 efi_set_watchdog(0);
1864 return EFI_EXIT(EFI_SUCCESS);
1868 * Get next value of the counter.
1870 * This function implements the NextMonotonicCount service.
1871 * See the Unified Extensible Firmware Interface (UEFI) specification
1874 * @count returned value of the counter
1875 * @return status code
1877 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1879 static uint64_t mono;
1881 EFI_ENTRY("%p", count);
1883 return EFI_EXIT(EFI_SUCCESS);
1889 * This function implements the Stall sercive.
1890 * See the Unified Extensible Firmware Interface (UEFI) specification
1893 * @microseconds period to sleep in microseconds
1894 * @return status code
1896 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1898 EFI_ENTRY("%ld", microseconds);
1899 udelay(microseconds);
1900 return EFI_EXIT(EFI_SUCCESS);
1904 * Reset the watchdog timer.
1906 * This function implements the SetWatchdogTimer service.
1907 * See the Unified Extensible Firmware Interface (UEFI) specification
1910 * @timeout seconds before reset by watchdog
1911 * @watchdog_code code to be logged when resetting
1912 * @data_size size of buffer in bytes
1913 * @watchdog_data buffer with data describing the reset reason
1914 * @return status code
1916 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1917 uint64_t watchdog_code,
1918 unsigned long data_size,
1919 uint16_t *watchdog_data)
1921 EFI_ENTRY("%ld, 0x%" PRIx64 ", %ld, %p", timeout, watchdog_code,
1922 data_size, watchdog_data);
1923 return EFI_EXIT(efi_set_watchdog(timeout));
1929 * This function implements the CloseProtocol service.
1930 * See the Unified Extensible Firmware Interface (UEFI) specification
1933 * @handle handle on which the protocol shall be closed
1934 * @protocol GUID of the protocol to close
1935 * @agent_handle handle of the driver
1936 * @controller_handle handle of the controller
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 * Provide information about then open status of a protocol on a handle
1976 * This function implements the OpenProtocolInformation service.
1977 * See the Unified Extensible Firmware Interface (UEFI) specification
1980 * @handle handle for which the information shall be retrieved
1981 * @protocol GUID of the protocol
1982 * @entry_buffer buffer to receive the open protocol information
1983 * @entry_count number of entries available in the buffer
1984 * @return status code
1986 static efi_status_t EFIAPI efi_open_protocol_information(
1987 efi_handle_t handle, const efi_guid_t *protocol,
1988 struct efi_open_protocol_info_entry **entry_buffer,
1989 efi_uintn_t *entry_count)
1991 unsigned long buffer_size;
1992 unsigned long count;
1993 struct efi_handler *handler;
1994 struct efi_open_protocol_info_item *item;
1997 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2000 /* Check parameters */
2001 if (!entry_buffer) {
2002 r = EFI_INVALID_PARAMETER;
2005 r = efi_search_protocol(handle, protocol, &handler);
2006 if (r != EFI_SUCCESS)
2011 list_for_each_entry(item, &handler->open_infos, link) {
2012 if (item->info.open_count)
2015 *entry_count = count;
2016 *entry_buffer = NULL;
2023 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2024 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2025 (void **)entry_buffer);
2026 if (r != EFI_SUCCESS)
2028 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2029 if (item->info.open_count)
2030 (*entry_buffer)[--count] = item->info;
2037 * Get protocols installed on a handle.
2039 * This function implements the ProtocolsPerHandleService.
2040 * See the Unified Extensible Firmware Interface (UEFI) specification
2043 * @handle handle for which the information is retrieved
2044 * @protocol_buffer buffer with protocol GUIDs
2045 * @protocol_buffer_count number of entries in the buffer
2046 * @return status code
2048 static efi_status_t EFIAPI efi_protocols_per_handle(
2049 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2050 efi_uintn_t *protocol_buffer_count)
2052 unsigned long buffer_size;
2053 struct efi_object *efiobj;
2054 struct list_head *protocol_handle;
2057 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2058 protocol_buffer_count);
2060 if (!handle || !protocol_buffer || !protocol_buffer_count)
2061 return EFI_EXIT(EFI_INVALID_PARAMETER);
2063 *protocol_buffer = NULL;
2064 *protocol_buffer_count = 0;
2066 efiobj = efi_search_obj(handle);
2068 return EFI_EXIT(EFI_INVALID_PARAMETER);
2070 /* Count protocols */
2071 list_for_each(protocol_handle, &efiobj->protocols) {
2072 ++*protocol_buffer_count;
2076 if (*protocol_buffer_count) {
2079 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2080 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2081 (void **)protocol_buffer);
2082 if (r != EFI_SUCCESS)
2084 list_for_each(protocol_handle, &efiobj->protocols) {
2085 struct efi_handler *protocol;
2087 protocol = list_entry(protocol_handle,
2088 struct efi_handler, link);
2089 (*protocol_buffer)[j] = (void *)protocol->guid;
2094 return EFI_EXIT(EFI_SUCCESS);
2098 * Locate handles implementing a protocol.
2100 * This function implements the LocateHandleBuffer service.
2101 * See the Unified Extensible Firmware Interface (UEFI) specification
2104 * @search_type selection criterion
2105 * @protocol GUID of the protocol
2106 * @search_key registration key
2107 * @no_handles number of returned handles
2108 * @buffer buffer with the returned handles
2109 * @return status code
2111 static efi_status_t EFIAPI efi_locate_handle_buffer(
2112 enum efi_locate_search_type search_type,
2113 const efi_guid_t *protocol, void *search_key,
2114 efi_uintn_t *no_handles, efi_handle_t **buffer)
2117 efi_uintn_t buffer_size = 0;
2119 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2120 no_handles, buffer);
2122 if (!no_handles || !buffer) {
2123 r = EFI_INVALID_PARAMETER;
2128 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2130 if (r != EFI_BUFFER_TOO_SMALL)
2132 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2134 if (r != EFI_SUCCESS)
2136 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2138 if (r == EFI_SUCCESS)
2139 *no_handles = buffer_size / sizeof(efi_handle_t);
2145 * Find an interface implementing a protocol.
2147 * This function implements the LocateProtocol service.
2148 * See the Unified Extensible Firmware Interface (UEFI) specification
2151 * @protocol GUID of the protocol
2152 * @registration registration key passed to the notification function
2153 * @protocol_interface interface implementing the protocol
2154 * @return status code
2156 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2158 void **protocol_interface)
2160 struct list_head *lhandle;
2163 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2165 if (!protocol || !protocol_interface)
2166 return EFI_EXIT(EFI_INVALID_PARAMETER);
2168 list_for_each(lhandle, &efi_obj_list) {
2169 struct efi_object *efiobj;
2170 struct efi_handler *handler;
2172 efiobj = list_entry(lhandle, struct efi_object, link);
2174 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2175 if (ret == EFI_SUCCESS) {
2176 *protocol_interface = handler->protocol_interface;
2177 return EFI_EXIT(EFI_SUCCESS);
2180 *protocol_interface = NULL;
2182 return EFI_EXIT(EFI_NOT_FOUND);
2186 * Get the device path and handle of an device implementing a protocol.
2188 * This function implements the LocateDevicePath service.
2189 * See the Unified Extensible Firmware Interface (UEFI) specification
2192 * @protocol GUID of the protocol
2193 * @device_path device path
2194 * @device handle of the device
2195 * @return status code
2197 static efi_status_t EFIAPI efi_locate_device_path(
2198 const efi_guid_t *protocol,
2199 struct efi_device_path **device_path,
2200 efi_handle_t *device)
2202 struct efi_device_path *dp;
2204 struct efi_handler *handler;
2205 efi_handle_t *handles;
2207 size_t len_best = 0;
2208 efi_uintn_t no_handles;
2212 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2214 if (!protocol || !device_path || !*device_path || !device) {
2215 ret = EFI_INVALID_PARAMETER;
2219 /* Find end of device path */
2220 len = efi_dp_instance_size(*device_path);
2222 /* Get all handles implementing the protocol */
2223 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2224 &no_handles, &handles));
2225 if (ret != EFI_SUCCESS)
2228 for (i = 0; i < no_handles; ++i) {
2229 /* Find the device path protocol */
2230 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2232 if (ret != EFI_SUCCESS)
2234 dp = (struct efi_device_path *)handler->protocol_interface;
2235 len_dp = efi_dp_instance_size(dp);
2237 * This handle can only be a better fit
2238 * if its device path length is longer than the best fit and
2239 * if its device path length is shorter of equal the searched
2242 if (len_dp <= len_best || len_dp > len)
2244 /* Check if dp is a subpath of device_path */
2245 if (memcmp(*device_path, dp, len_dp))
2247 *device = handles[i];
2251 remainder = (u8 *)*device_path + len_best;
2252 *device_path = (struct efi_device_path *)remainder;
2255 ret = EFI_NOT_FOUND;
2258 return EFI_EXIT(ret);
2262 * Install multiple protocol interfaces.
2264 * This function implements the MultipleProtocolInterfaces service.
2265 * See the Unified Extensible Firmware Interface (UEFI) specification
2268 * @handle handle on which the protocol interfaces shall be installed
2269 * @... NULL terminated argument list with pairs of protocol GUIDS and
2271 * @return status code
2273 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2276 EFI_ENTRY("%p", handle);
2279 const efi_guid_t *protocol;
2280 void *protocol_interface;
2281 efi_status_t r = EFI_SUCCESS;
2285 return EFI_EXIT(EFI_INVALID_PARAMETER);
2287 va_start(argptr, handle);
2289 protocol = va_arg(argptr, efi_guid_t*);
2292 protocol_interface = va_arg(argptr, void*);
2293 r = EFI_CALL(efi_install_protocol_interface(
2295 EFI_NATIVE_INTERFACE,
2296 protocol_interface));
2297 if (r != EFI_SUCCESS)
2302 if (r == EFI_SUCCESS)
2305 /* If an error occurred undo all changes. */
2306 va_start(argptr, handle);
2308 protocol = va_arg(argptr, efi_guid_t*);
2309 protocol_interface = va_arg(argptr, void*);
2310 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2311 protocol_interface));
2319 * Uninstall multiple protocol interfaces.
2321 * This function implements the UninstallMultipleProtocolInterfaces service.
2322 * See the Unified Extensible Firmware Interface (UEFI) specification
2325 * @handle handle from which the protocol interfaces shall be removed
2326 * @... NULL terminated argument list with pairs of protocol GUIDS and
2328 * @return status code
2330 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2333 EFI_ENTRY("%p", handle);
2336 const efi_guid_t *protocol;
2337 void *protocol_interface;
2338 efi_status_t r = EFI_SUCCESS;
2342 return EFI_EXIT(EFI_INVALID_PARAMETER);
2344 va_start(argptr, handle);
2346 protocol = va_arg(argptr, efi_guid_t*);
2349 protocol_interface = va_arg(argptr, void*);
2350 r = EFI_CALL(efi_uninstall_protocol_interface(
2352 protocol_interface));
2353 if (r != EFI_SUCCESS)
2358 if (r == EFI_SUCCESS)
2361 /* If an error occurred undo all changes. */
2362 va_start(argptr, handle);
2364 protocol = va_arg(argptr, efi_guid_t*);
2365 protocol_interface = va_arg(argptr, void*);
2366 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2367 EFI_NATIVE_INTERFACE,
2368 protocol_interface));
2376 * Calculate cyclic redundancy code.
2378 * This function implements the CalculateCrc32 service.
2379 * See the Unified Extensible Firmware Interface (UEFI) specification
2382 * @data buffer with data
2383 * @data_size size of buffer in bytes
2384 * @crc32_p cyclic redundancy code
2385 * @return status code
2387 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2388 unsigned long data_size,
2391 EFI_ENTRY("%p, %ld", data, data_size);
2392 *crc32_p = crc32(0, data, data_size);
2393 return EFI_EXIT(EFI_SUCCESS);
2399 * This function implements the CopyMem service.
2400 * See the Unified Extensible Firmware Interface (UEFI) specification
2403 * @destination destination of the copy operation
2404 * @source source of the copy operation
2405 * @length number of bytes to copy
2407 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2410 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2411 memcpy(destination, source, length);
2412 EFI_EXIT(EFI_SUCCESS);
2416 * Fill memory with a byte value.
2418 * This function implements the SetMem service.
2419 * See the Unified Extensible Firmware Interface (UEFI) specification
2422 * @buffer buffer to fill
2423 * @size size of buffer in bytes
2424 * @value byte to copy to the buffer
2426 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2428 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2429 memset(buffer, value, size);
2430 EFI_EXIT(EFI_SUCCESS);
2434 * Open protocol interface on a handle.
2436 * @handler handler of a protocol
2437 * @protocol_interface interface implementing the protocol
2438 * @agent_handle handle of the driver
2439 * @controller_handle handle of the controller
2440 * @attributes attributes indicating how to open the protocol
2441 * @return status code
2443 static efi_status_t efi_protocol_open(
2444 struct efi_handler *handler,
2445 void **protocol_interface, void *agent_handle,
2446 void *controller_handle, uint32_t attributes)
2448 struct efi_open_protocol_info_item *item;
2449 struct efi_open_protocol_info_entry *match = NULL;
2450 bool opened_by_driver = false;
2451 bool opened_exclusive = false;
2453 /* If there is no agent, only return the interface */
2457 /* For TEST_PROTOCOL ignore interface attribute */
2458 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2459 *protocol_interface = NULL;
2462 * Check if the protocol is already opened by a driver with the same
2463 * attributes or opened exclusively
2465 list_for_each_entry(item, &handler->open_infos, link) {
2466 if (item->info.agent_handle == agent_handle) {
2467 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2468 (item->info.attributes == attributes))
2469 return EFI_ALREADY_STARTED;
2471 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2472 opened_exclusive = true;
2475 /* Only one controller can open the protocol exclusively */
2476 if (opened_exclusive && attributes &
2477 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2478 return EFI_ACCESS_DENIED;
2480 /* Prepare exclusive opening */
2481 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2482 /* Try to disconnect controllers */
2483 list_for_each_entry(item, &handler->open_infos, link) {
2484 if (item->info.attributes ==
2485 EFI_OPEN_PROTOCOL_BY_DRIVER)
2486 EFI_CALL(efi_disconnect_controller(
2487 item->info.controller_handle,
2488 item->info.agent_handle,
2491 opened_by_driver = false;
2492 /* Check if all controllers are disconnected */
2493 list_for_each_entry(item, &handler->open_infos, link) {
2494 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2495 opened_by_driver = true;
2497 /* Only one controller can be conncected */
2498 if (opened_by_driver)
2499 return EFI_ACCESS_DENIED;
2502 /* Find existing entry */
2503 list_for_each_entry(item, &handler->open_infos, link) {
2504 if (item->info.agent_handle == agent_handle &&
2505 item->info.controller_handle == controller_handle)
2506 match = &item->info;
2508 /* None found, create one */
2510 match = efi_create_open_info(handler);
2512 return EFI_OUT_OF_RESOURCES;
2515 match->agent_handle = agent_handle;
2516 match->controller_handle = controller_handle;
2517 match->attributes = attributes;
2518 match->open_count++;
2521 /* For TEST_PROTOCOL ignore interface attribute. */
2522 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2523 *protocol_interface = handler->protocol_interface;
2529 * Open protocol interface on a handle.
2531 * This function implements the OpenProtocol interface.
2532 * See the Unified Extensible Firmware Interface (UEFI) specification
2535 * @handle handle on which the protocol shall be opened
2536 * @protocol GUID of the protocol
2537 * @protocol_interface interface implementing the protocol
2538 * @agent_handle handle of the driver
2539 * @controller_handle handle of the controller
2540 * @attributes attributes indicating how to open the protocol
2541 * @return status code
2543 static efi_status_t EFIAPI efi_open_protocol(
2544 void *handle, const efi_guid_t *protocol,
2545 void **protocol_interface, void *agent_handle,
2546 void *controller_handle, uint32_t attributes)
2548 struct efi_handler *handler;
2549 efi_status_t r = EFI_INVALID_PARAMETER;
2551 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2552 protocol_interface, agent_handle, controller_handle,
2555 if (!handle || !protocol ||
2556 (!protocol_interface && attributes !=
2557 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2561 switch (attributes) {
2562 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2563 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2564 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2566 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2567 if (controller_handle == handle)
2570 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2571 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2572 /* Check that the controller handle is valid */
2573 if (!efi_search_obj(controller_handle))
2576 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2577 /* Check that the agent handle is valid */
2578 if (!efi_search_obj(agent_handle))
2585 r = efi_search_protocol(handle, protocol, &handler);
2586 if (r != EFI_SUCCESS)
2589 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2590 controller_handle, attributes);
2596 * Get interface of a protocol on a handle.
2598 * This function implements the HandleProtocol service.
2599 * See the Unified Extensible Firmware Interface (UEFI) specification
2602 * @handle handle on which the protocol shall be opened
2603 * @protocol GUID of the protocol
2604 * @protocol_interface interface implementing the protocol
2605 * @return status code
2607 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2608 const efi_guid_t *protocol,
2609 void **protocol_interface)
2611 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2612 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2615 static efi_status_t efi_bind_controller(
2616 efi_handle_t controller_handle,
2617 efi_handle_t driver_image_handle,
2618 struct efi_device_path *remain_device_path)
2620 struct efi_driver_binding_protocol *binding_protocol;
2623 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2624 &efi_guid_driver_binding_protocol,
2625 (void **)&binding_protocol,
2626 driver_image_handle, NULL,
2627 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2628 if (r != EFI_SUCCESS)
2630 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2632 remain_device_path));
2633 if (r == EFI_SUCCESS)
2634 r = EFI_CALL(binding_protocol->start(binding_protocol,
2636 remain_device_path));
2637 EFI_CALL(efi_close_protocol(driver_image_handle,
2638 &efi_guid_driver_binding_protocol,
2639 driver_image_handle, NULL));
2643 static efi_status_t efi_connect_single_controller(
2644 efi_handle_t controller_handle,
2645 efi_handle_t *driver_image_handle,
2646 struct efi_device_path *remain_device_path)
2648 efi_handle_t *buffer;
2652 size_t connected = 0;
2654 /* Get buffer with all handles with driver binding protocol */
2655 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2656 &efi_guid_driver_binding_protocol,
2657 NULL, &count, &buffer));
2658 if (r != EFI_SUCCESS)
2661 /* Context Override */
2662 if (driver_image_handle) {
2663 for (; *driver_image_handle; ++driver_image_handle) {
2664 for (i = 0; i < count; ++i) {
2665 if (buffer[i] == *driver_image_handle) {
2667 r = efi_bind_controller(
2669 *driver_image_handle,
2670 remain_device_path);
2672 * For drivers that do not support the
2673 * controller or are already connected
2674 * we receive an error code here.
2676 if (r == EFI_SUCCESS)
2684 * TODO: Some overrides are not yet implemented:
2685 * - Platform Driver Override
2686 * - Driver Family Override Search
2687 * - Bus Specific Driver Override
2690 /* Driver Binding Search */
2691 for (i = 0; i < count; ++i) {
2693 r = efi_bind_controller(controller_handle,
2695 remain_device_path);
2696 if (r == EFI_SUCCESS)
2701 efi_free_pool(buffer);
2703 return EFI_NOT_FOUND;
2708 * Connect a controller to a driver.
2710 * This function implements the ConnectController service.
2711 * See the Unified Extensible Firmware Interface (UEFI) specification
2714 * First all driver binding protocol handles are tried for binding drivers.
2715 * Afterwards all handles that have openened a protocol of the controller
2716 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2718 * @controller_handle handle of the controller
2719 * @driver_image_handle handle of the driver
2720 * @remain_device_path device path of a child controller
2721 * @recursive true to connect all child controllers
2722 * @return status code
2724 static efi_status_t EFIAPI efi_connect_controller(
2725 efi_handle_t controller_handle,
2726 efi_handle_t *driver_image_handle,
2727 struct efi_device_path *remain_device_path,
2731 efi_status_t ret = EFI_NOT_FOUND;
2732 struct efi_object *efiobj;
2734 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2735 remain_device_path, recursive);
2737 efiobj = efi_search_obj(controller_handle);
2739 ret = EFI_INVALID_PARAMETER;
2743 r = efi_connect_single_controller(controller_handle,
2744 driver_image_handle,
2745 remain_device_path);
2746 if (r == EFI_SUCCESS)
2749 struct efi_handler *handler;
2750 struct efi_open_protocol_info_item *item;
2752 list_for_each_entry(handler, &efiobj->protocols, link) {
2753 list_for_each_entry(item, &handler->open_infos, link) {
2754 if (item->info.attributes &
2755 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2756 r = EFI_CALL(efi_connect_controller(
2757 item->info.controller_handle,
2758 driver_image_handle,
2761 if (r == EFI_SUCCESS)
2767 /* Check for child controller specified by end node */
2768 if (ret != EFI_SUCCESS && remain_device_path &&
2769 remain_device_path->type == DEVICE_PATH_TYPE_END)
2772 return EFI_EXIT(ret);
2776 * Get all child controllers associated to a driver.
2777 * The allocated buffer has to be freed with free().
2779 * @efiobj handle of the controller
2780 * @driver_handle handle of the driver
2781 * @number_of_children number of child controllers
2782 * @child_handle_buffer handles of the the child controllers
2784 static efi_status_t efi_get_child_controllers(
2785 struct efi_object *efiobj,
2786 efi_handle_t driver_handle,
2787 efi_uintn_t *number_of_children,
2788 efi_handle_t **child_handle_buffer)
2790 struct efi_handler *handler;
2791 struct efi_open_protocol_info_item *item;
2792 efi_uintn_t count = 0, i;
2795 /* Count all child controller associations */
2796 list_for_each_entry(handler, &efiobj->protocols, link) {
2797 list_for_each_entry(item, &handler->open_infos, link) {
2798 if (item->info.agent_handle == driver_handle &&
2799 item->info.attributes &
2800 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2805 * Create buffer. In case of duplicate child controller assignments
2806 * the buffer will be too large. But that does not harm.
2808 *number_of_children = 0;
2809 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2810 if (!*child_handle_buffer)
2811 return EFI_OUT_OF_RESOURCES;
2812 /* Copy unique child handles */
2813 list_for_each_entry(handler, &efiobj->protocols, link) {
2814 list_for_each_entry(item, &handler->open_infos, link) {
2815 if (item->info.agent_handle == driver_handle &&
2816 item->info.attributes &
2817 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2818 /* Check this is a new child controller */
2820 for (i = 0; i < *number_of_children; ++i) {
2821 if ((*child_handle_buffer)[i] ==
2822 item->info.controller_handle)
2825 /* Copy handle to buffer */
2827 i = (*number_of_children)++;
2828 (*child_handle_buffer)[i] =
2829 item->info.controller_handle;
2838 * Disconnect a controller from a driver.
2840 * This function implements the DisconnectController service.
2841 * See the Unified Extensible Firmware Interface (UEFI) specification
2844 * @controller_handle handle of the controller
2845 * @driver_image_handle handle of the driver
2846 * @child_handle handle of the child to destroy
2847 * @return status code
2849 static efi_status_t EFIAPI efi_disconnect_controller(
2850 efi_handle_t controller_handle,
2851 efi_handle_t driver_image_handle,
2852 efi_handle_t child_handle)
2854 struct efi_driver_binding_protocol *binding_protocol;
2855 efi_handle_t *child_handle_buffer = NULL;
2856 size_t number_of_children = 0;
2858 size_t stop_count = 0;
2859 struct efi_object *efiobj;
2861 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2864 efiobj = efi_search_obj(controller_handle);
2866 r = EFI_INVALID_PARAMETER;
2870 if (child_handle && !efi_search_obj(child_handle)) {
2871 r = EFI_INVALID_PARAMETER;
2875 /* If no driver handle is supplied, disconnect all drivers */
2876 if (!driver_image_handle) {
2877 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2881 /* Create list of child handles */
2883 number_of_children = 1;
2884 child_handle_buffer = &child_handle;
2886 efi_get_child_controllers(efiobj,
2887 driver_image_handle,
2888 &number_of_children,
2889 &child_handle_buffer);
2892 /* Get the driver binding protocol */
2893 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2894 &efi_guid_driver_binding_protocol,
2895 (void **)&binding_protocol,
2896 driver_image_handle, NULL,
2897 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2898 if (r != EFI_SUCCESS)
2900 /* Remove the children */
2901 if (number_of_children) {
2902 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2905 child_handle_buffer));
2906 if (r == EFI_SUCCESS)
2909 /* Remove the driver */
2911 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2914 if (r == EFI_SUCCESS)
2916 EFI_CALL(efi_close_protocol(driver_image_handle,
2917 &efi_guid_driver_binding_protocol,
2918 driver_image_handle, NULL));
2926 free(child_handle_buffer);
2930 static const struct efi_boot_services efi_boot_services = {
2932 .headersize = sizeof(struct efi_table_hdr),
2934 .raise_tpl = efi_raise_tpl,
2935 .restore_tpl = efi_restore_tpl,
2936 .allocate_pages = efi_allocate_pages_ext,
2937 .free_pages = efi_free_pages_ext,
2938 .get_memory_map = efi_get_memory_map_ext,
2939 .allocate_pool = efi_allocate_pool_ext,
2940 .free_pool = efi_free_pool_ext,
2941 .create_event = efi_create_event_ext,
2942 .set_timer = efi_set_timer_ext,
2943 .wait_for_event = efi_wait_for_event,
2944 .signal_event = efi_signal_event_ext,
2945 .close_event = efi_close_event,
2946 .check_event = efi_check_event,
2947 .install_protocol_interface = efi_install_protocol_interface,
2948 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2949 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2950 .handle_protocol = efi_handle_protocol,
2952 .register_protocol_notify = efi_register_protocol_notify,
2953 .locate_handle = efi_locate_handle_ext,
2954 .locate_device_path = efi_locate_device_path,
2955 .install_configuration_table = efi_install_configuration_table_ext,
2956 .load_image = efi_load_image,
2957 .start_image = efi_start_image,
2959 .unload_image = efi_unload_image,
2960 .exit_boot_services = efi_exit_boot_services,
2961 .get_next_monotonic_count = efi_get_next_monotonic_count,
2963 .set_watchdog_timer = efi_set_watchdog_timer,
2964 .connect_controller = efi_connect_controller,
2965 .disconnect_controller = efi_disconnect_controller,
2966 .open_protocol = efi_open_protocol,
2967 .close_protocol = efi_close_protocol,
2968 .open_protocol_information = efi_open_protocol_information,
2969 .protocols_per_handle = efi_protocols_per_handle,
2970 .locate_handle_buffer = efi_locate_handle_buffer,
2971 .locate_protocol = efi_locate_protocol,
2972 .install_multiple_protocol_interfaces =
2973 efi_install_multiple_protocol_interfaces,
2974 .uninstall_multiple_protocol_interfaces =
2975 efi_uninstall_multiple_protocol_interfaces,
2976 .calculate_crc32 = efi_calculate_crc32,
2977 .copy_mem = efi_copy_mem,
2978 .set_mem = efi_set_mem,
2979 .create_event_ex = efi_create_event_ex,
2982 static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
2984 struct efi_system_table __efi_runtime_data systab = {
2986 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2987 .revision = 2 << 16 | 70, /* 2.7 */
2988 .headersize = sizeof(struct efi_table_hdr),
2990 .fw_vendor = (long)firmware_vendor,
2991 .con_in = (void *)&efi_con_in,
2992 .con_out = (void *)&efi_con_out,
2993 .std_err = (void *)&efi_con_out,
2994 .runtime = (void *)&efi_runtime_services,
2995 .boottime = (void *)&efi_boot_services,
2997 .tables = (void *)efi_conf_table,