2 * EFI application boot time services
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <efi_loader.h>
12 #include <environment.h>
14 #include <asm/global_data.h>
15 #include <linux/libfdt_env.h>
16 #include <u-boot/crc.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 /* Task priority level */
24 static efi_uintn_t efi_tpl = TPL_APPLICATION;
26 /* This list contains all the EFI objects our payload has access to */
27 LIST_HEAD(efi_obj_list);
30 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31 * we need to do trickery with caches. Since we don't want to break the EFI
32 * aware boot path, only apply hacks when loading exiting directly (breaking
33 * direct Linux EFI booting along the way - oh well).
35 static bool efi_is_direct_boot = true;
38 * EFI can pass arbitrary additional "tables" containing vendor specific
39 * information to the payload. One such table is the FDT table which contains
40 * a pointer to a flattened device tree blob.
42 * In most cases we want to pass an FDT to the payload, so reserve one slot of
43 * config table space for it. The pointer gets populated by do_bootefi_exec().
45 static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
49 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
50 * fixed when compiling U-Boot. However, the payload does not know about that
51 * restriction so we need to manually swap its and our view of that register on
52 * EFI callback entry/exit.
54 static volatile void *efi_gd, *app_gd;
57 static int entry_count;
58 static int nesting_level;
59 /* GUID of the device tree table */
60 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
61 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
62 const efi_guid_t efi_guid_driver_binding_protocol =
63 EFI_DRIVER_BINDING_PROTOCOL_GUID;
65 static efi_status_t EFIAPI efi_disconnect_controller(
66 efi_handle_t controller_handle,
67 efi_handle_t driver_image_handle,
68 efi_handle_t child_handle);
70 /* Called on every callback entry */
71 int __efi_entry_check(void)
73 int ret = entry_count++ == 0;
82 /* Called on every callback exit */
83 int __efi_exit_check(void)
85 int ret = --entry_count == 0;
92 /* Called from do_bootefi_exec() */
93 void efi_save_gd(void)
101 * Special case handler for error/abort that just forces things back
102 * to u-boot world so we can dump out an abort msg, without any care
103 * about returning back to UEFI world.
105 void efi_restore_gd(void)
108 /* Only restore if we're already in EFI context */
116 * Return a string for indenting with two spaces per level. A maximum of ten
117 * indent levels is supported. Higher indent levels will be truncated.
119 * @level indent level
120 * @return indent string
122 static const char *indent_string(int level)
124 const char *indent = " ";
125 const int max = strlen(indent);
127 level = min(max, level * 2);
128 return &indent[max - level];
131 const char *__efi_nesting(void)
133 return indent_string(nesting_level);
136 const char *__efi_nesting_inc(void)
138 return indent_string(nesting_level++);
141 const char *__efi_nesting_dec(void)
143 return indent_string(--nesting_level);
147 * Queue an EFI event.
149 * This function queues the notification function of the event for future
152 * The notification function is called if the task priority level of the
153 * event is higher than the current task priority level.
155 * For the SignalEvent service see efi_signal_event_ext.
157 * @event event to signal
158 * @check_tpl check the TPL level
160 void efi_signal_event(struct efi_event *event, bool check_tpl)
162 if (event->notify_function) {
163 event->is_queued = true;
165 if (check_tpl && efi_tpl >= event->notify_tpl)
167 EFI_CALL_VOID(event->notify_function(event,
168 event->notify_context));
170 event->is_queued = false;
174 * Raise the task priority level.
176 * This function implements the RaiseTpl service.
177 * See the Unified Extensible Firmware Interface (UEFI) specification
180 * @new_tpl new value of the task priority level
181 * @return old value of the task priority level
183 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
185 efi_uintn_t old_tpl = efi_tpl;
187 EFI_ENTRY("0x%zx", new_tpl);
189 if (new_tpl < efi_tpl)
190 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
192 if (efi_tpl > TPL_HIGH_LEVEL)
193 efi_tpl = TPL_HIGH_LEVEL;
195 EFI_EXIT(EFI_SUCCESS);
200 * Lower the task priority level.
202 * This function implements the RestoreTpl service.
203 * See the Unified Extensible Firmware Interface (UEFI) specification
206 * @old_tpl value of the task priority level to be restored
208 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
210 EFI_ENTRY("0x%zx", old_tpl);
212 if (old_tpl > efi_tpl)
213 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
215 if (efi_tpl > TPL_HIGH_LEVEL)
216 efi_tpl = TPL_HIGH_LEVEL;
218 EFI_EXIT(EFI_SUCCESS);
222 * Allocate memory pages.
224 * This function implements the AllocatePages service.
225 * See the Unified Extensible Firmware Interface (UEFI) specification
228 * @type type of allocation to be performed
229 * @memory_type usage type of the allocated memory
230 * @pages number of pages to be allocated
231 * @memory allocated memory
232 * @return status code
234 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
240 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
241 r = efi_allocate_pages(type, memory_type, pages, memory);
248 * This function implements the FreePages service.
249 * See the Unified Extensible Firmware Interface (UEFI) specification
252 * @memory start of the memory area to be freed
253 * @pages number of pages to be freed
254 * @return status code
256 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
261 EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
262 r = efi_free_pages(memory, pages);
267 * Get map describing memory usage.
269 * This function implements the GetMemoryMap service.
270 * See the Unified Extensible Firmware Interface (UEFI) specification
273 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
274 * on exit the size of the copied memory map
275 * @memory_map buffer to which the memory map is written
276 * @map_key key for the memory map
277 * @descriptor_size size of an individual memory descriptor
278 * @descriptor_version version number of the memory descriptor structure
279 * @return status code
281 static efi_status_t EFIAPI efi_get_memory_map_ext(
282 efi_uintn_t *memory_map_size,
283 struct efi_mem_desc *memory_map,
284 efi_uintn_t *map_key,
285 efi_uintn_t *descriptor_size,
286 uint32_t *descriptor_version)
290 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
291 map_key, descriptor_size, descriptor_version);
292 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
293 descriptor_size, descriptor_version);
298 * Allocate memory from pool.
300 * This function implements the AllocatePool service.
301 * See the Unified Extensible Firmware Interface (UEFI) specification
304 * @pool_type type of the pool from which memory is to be allocated
305 * @size number of bytes to be allocated
306 * @buffer allocated memory
307 * @return status code
309 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
315 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
316 r = efi_allocate_pool(pool_type, size, buffer);
321 * Free memory from pool.
323 * This function implements the FreePool service.
324 * See the Unified Extensible Firmware Interface (UEFI) specification
327 * @buffer start of memory to be freed
328 * @return status code
330 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
334 EFI_ENTRY("%p", buffer);
335 r = efi_free_pool(buffer);
340 * Add a new object to the object list.
342 * The protocols list is initialized.
343 * The object handle is set.
345 * @obj object to be added
347 void efi_add_handle(struct efi_object *obj)
351 INIT_LIST_HEAD(&obj->protocols);
353 list_add_tail(&obj->link, &efi_obj_list);
360 * @return status code
362 efi_status_t efi_create_handle(efi_handle_t *handle)
364 struct efi_object *obj;
367 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
368 sizeof(struct efi_object),
370 if (r != EFI_SUCCESS)
373 *handle = obj->handle;
378 * Find a protocol on a handle.
381 * @protocol_guid GUID of the protocol
382 * @handler reference to the protocol
383 * @return status code
385 efi_status_t efi_search_protocol(const efi_handle_t handle,
386 const efi_guid_t *protocol_guid,
387 struct efi_handler **handler)
389 struct efi_object *efiobj;
390 struct list_head *lhandle;
392 if (!handle || !protocol_guid)
393 return EFI_INVALID_PARAMETER;
394 efiobj = efi_search_obj(handle);
396 return EFI_INVALID_PARAMETER;
397 list_for_each(lhandle, &efiobj->protocols) {
398 struct efi_handler *protocol;
400 protocol = list_entry(lhandle, struct efi_handler, link);
401 if (!guidcmp(protocol->guid, protocol_guid)) {
407 return EFI_NOT_FOUND;
411 * Delete protocol from a handle.
413 * @handle handle from which the protocol shall be deleted
414 * @protocol GUID of the protocol to be deleted
415 * @protocol_interface interface of the protocol implementation
416 * @return status code
418 efi_status_t efi_remove_protocol(const efi_handle_t handle,
419 const efi_guid_t *protocol,
420 void *protocol_interface)
422 struct efi_handler *handler;
425 ret = efi_search_protocol(handle, protocol, &handler);
426 if (ret != EFI_SUCCESS)
428 if (guidcmp(handler->guid, protocol))
429 return EFI_INVALID_PARAMETER;
430 list_del(&handler->link);
436 * Delete all protocols from a handle.
438 * @handle handle from which the protocols shall be deleted
439 * @return status code
441 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
443 struct efi_object *efiobj;
444 struct efi_handler *protocol;
445 struct efi_handler *pos;
447 efiobj = efi_search_obj(handle);
449 return EFI_INVALID_PARAMETER;
450 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
453 ret = efi_remove_protocol(handle, protocol->guid,
454 protocol->protocol_interface);
455 if (ret != EFI_SUCCESS)
464 * @handle handle to delete
466 void efi_delete_handle(struct efi_object *obj)
470 efi_remove_all_protocols(obj->handle);
471 list_del(&obj->link);
476 * Our event capabilities are very limited. Only a small limited
477 * number of events is allowed to coexist.
479 static struct efi_event efi_events[16];
484 * This function is used inside U-Boot code to create an event.
486 * For the API function implementing the CreateEvent service see
487 * efi_create_event_ext.
489 * @type type of the event to create
490 * @notify_tpl task priority level of the event
491 * @notify_function notification function of the event
492 * @notify_context pointer passed to the notification function
493 * @event created event
494 * @return status code
496 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
497 void (EFIAPI *notify_function) (
498 struct efi_event *event,
500 void *notify_context, struct efi_event **event)
505 return EFI_INVALID_PARAMETER;
507 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
508 return EFI_INVALID_PARAMETER;
510 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
511 notify_function == NULL)
512 return EFI_INVALID_PARAMETER;
514 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
515 if (efi_events[i].type)
517 efi_events[i].type = type;
518 efi_events[i].notify_tpl = notify_tpl;
519 efi_events[i].notify_function = notify_function;
520 efi_events[i].notify_context = notify_context;
521 /* Disable timers on bootup */
522 efi_events[i].trigger_next = -1ULL;
523 efi_events[i].is_queued = false;
524 efi_events[i].is_signaled = false;
525 *event = &efi_events[i];
528 return EFI_OUT_OF_RESOURCES;
532 * Create an event in a group.
534 * This function implements the CreateEventEx service.
535 * See the Unified Extensible Firmware Interface (UEFI) specification
537 * TODO: Support event groups
539 * @type type of the event to create
540 * @notify_tpl task priority level of the event
541 * @notify_function notification function of the event
542 * @notify_context pointer passed to the notification function
543 * @event created event
544 * @event_group event group
545 * @return status code
547 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
548 void (EFIAPI *notify_function) (
549 struct efi_event *event,
551 void *notify_context,
552 efi_guid_t *event_group,
553 struct efi_event **event)
555 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
556 notify_context, event_group);
558 return EFI_EXIT(EFI_UNSUPPORTED);
559 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
560 notify_context, event));
566 * This function implements the CreateEvent service.
567 * See the Unified Extensible Firmware Interface (UEFI) specification
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 static efi_status_t EFIAPI efi_create_event_ext(
578 uint32_t type, efi_uintn_t notify_tpl,
579 void (EFIAPI *notify_function) (
580 struct efi_event *event,
582 void *notify_context, struct efi_event **event)
584 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
586 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
587 notify_context, event));
591 * Check if a timer event has occurred or a queued notification function should
594 * Our timers have to work without interrupts, so we check whenever keyboard
595 * input or disk accesses happen if enough time elapsed for them to fire.
597 void efi_timer_check(void)
600 u64 now = timer_get_us();
602 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
603 if (!efi_events[i].type)
605 if (efi_events[i].is_queued)
606 efi_signal_event(&efi_events[i], true);
607 if (!(efi_events[i].type & EVT_TIMER) ||
608 now < efi_events[i].trigger_next)
610 switch (efi_events[i].trigger_type) {
611 case EFI_TIMER_RELATIVE:
612 efi_events[i].trigger_type = EFI_TIMER_STOP;
614 case EFI_TIMER_PERIODIC:
615 efi_events[i].trigger_next +=
616 efi_events[i].trigger_time;
621 efi_events[i].is_signaled = true;
622 efi_signal_event(&efi_events[i], true);
628 * Set the trigger time for a timer event or stop the event.
630 * This is the function for internal usage in U-Boot. For the API function
631 * implementing the SetTimer service see efi_set_timer_ext.
633 * @event event for which the timer is set
634 * @type type of the timer
635 * @trigger_time trigger period in multiples of 100ns
636 * @return status code
638 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
639 uint64_t trigger_time)
644 * The parameter defines a multiple of 100ns.
645 * We use multiples of 1000ns. So divide by 10.
647 do_div(trigger_time, 10);
649 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
650 if (event != &efi_events[i])
653 if (!(event->type & EVT_TIMER))
657 event->trigger_next = -1ULL;
659 case EFI_TIMER_PERIODIC:
660 case EFI_TIMER_RELATIVE:
661 event->trigger_next =
662 timer_get_us() + trigger_time;
665 return EFI_INVALID_PARAMETER;
667 event->trigger_type = type;
668 event->trigger_time = trigger_time;
669 event->is_signaled = false;
672 return EFI_INVALID_PARAMETER;
676 * Set the trigger time for a timer event or stop the event.
678 * This function implements the SetTimer service.
679 * See the Unified Extensible Firmware Interface (UEFI) specification
682 * @event event for which the timer is set
683 * @type type of the timer
684 * @trigger_time trigger period in multiples of 100ns
685 * @return status code
687 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
688 enum efi_timer_delay type,
689 uint64_t trigger_time)
691 EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
692 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
696 * Wait for events to be signaled.
698 * This function implements the WaitForEvent service.
699 * See the Unified Extensible Firmware Interface (UEFI) specification
702 * @num_events number of events to be waited for
703 * @events events to be waited for
704 * @index index of the event that was signaled
705 * @return status code
707 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
708 struct efi_event **event,
713 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
715 /* Check parameters */
716 if (!num_events || !event)
717 return EFI_EXIT(EFI_INVALID_PARAMETER);
719 if (efi_tpl != TPL_APPLICATION)
720 return EFI_EXIT(EFI_UNSUPPORTED);
721 for (i = 0; i < num_events; ++i) {
722 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
723 if (event[i] == &efi_events[j])
726 return EFI_EXIT(EFI_INVALID_PARAMETER);
728 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
729 return EFI_EXIT(EFI_INVALID_PARAMETER);
730 if (!event[i]->is_signaled)
731 efi_signal_event(event[i], true);
734 /* Wait for signal */
736 for (i = 0; i < num_events; ++i) {
737 if (event[i]->is_signaled)
740 /* Allow events to occur. */
746 * Reset the signal which is passed to the caller to allow periodic
749 event[i]->is_signaled = false;
753 return EFI_EXIT(EFI_SUCCESS);
757 * Signal an EFI event.
759 * This function implements the SignalEvent service.
760 * See the Unified Extensible Firmware Interface (UEFI) specification
763 * This functions sets the signaled state of the event and queues the
764 * notification function for execution.
766 * @event event to signal
767 * @return status code
769 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
773 EFI_ENTRY("%p", event);
774 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
775 if (event != &efi_events[i])
777 if (event->is_signaled)
779 event->is_signaled = true;
780 if (event->type & EVT_NOTIFY_SIGNAL)
781 efi_signal_event(event, true);
784 return EFI_EXIT(EFI_SUCCESS);
788 * Close an EFI event.
790 * This function implements the CloseEvent service.
791 * See the Unified Extensible Firmware Interface (UEFI) specification
794 * @event event to close
795 * @return status code
797 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
801 EFI_ENTRY("%p", event);
802 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
803 if (event == &efi_events[i]) {
805 event->trigger_next = -1ULL;
806 event->is_queued = false;
807 event->is_signaled = false;
808 return EFI_EXIT(EFI_SUCCESS);
811 return EFI_EXIT(EFI_INVALID_PARAMETER);
815 * Check if an event is signaled.
817 * This function implements the CheckEvent service.
818 * See the Unified Extensible Firmware Interface (UEFI) specification
821 * If an event is not signaled yet, the notification function is queued.
822 * The signaled state is cleared.
824 * @event event to check
825 * @return status code
827 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
831 EFI_ENTRY("%p", event);
833 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
834 if (event != &efi_events[i])
836 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
838 if (!event->is_signaled)
839 efi_signal_event(event, true);
840 if (event->is_signaled) {
841 event->is_signaled = false;
842 return EFI_EXIT(EFI_SUCCESS);
844 return EFI_EXIT(EFI_NOT_READY);
846 return EFI_EXIT(EFI_INVALID_PARAMETER);
850 * Find the internal EFI object for a handle.
852 * @handle handle to find
855 struct efi_object *efi_search_obj(const efi_handle_t handle)
857 struct efi_object *efiobj;
859 list_for_each_entry(efiobj, &efi_obj_list, link) {
860 if (efiobj->handle == handle)
868 * Create open protocol info entry and add it to a protocol.
870 * @handler handler of a protocol
871 * @return open protocol info entry
873 static struct efi_open_protocol_info_entry *efi_create_open_info(
874 struct efi_handler *handler)
876 struct efi_open_protocol_info_item *item;
878 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
881 /* Append the item to the open protocol info list. */
882 list_add_tail(&item->link, &handler->open_infos);
888 * Remove an open protocol info entry from a protocol.
890 * @handler handler of a protocol
891 * @return status code
893 static efi_status_t efi_delete_open_info(
894 struct efi_open_protocol_info_item *item)
896 list_del(&item->link);
902 * Install new protocol on a handle.
904 * @handle handle on which the protocol shall be installed
905 * @protocol GUID of the protocol to be installed
906 * @protocol_interface interface of the protocol implementation
907 * @return status code
909 efi_status_t efi_add_protocol(const efi_handle_t handle,
910 const efi_guid_t *protocol,
911 void *protocol_interface)
913 struct efi_object *efiobj;
914 struct efi_handler *handler;
917 efiobj = efi_search_obj(handle);
919 return EFI_INVALID_PARAMETER;
920 ret = efi_search_protocol(handle, protocol, NULL);
921 if (ret != EFI_NOT_FOUND)
922 return EFI_INVALID_PARAMETER;
923 handler = calloc(1, sizeof(struct efi_handler));
925 return EFI_OUT_OF_RESOURCES;
926 handler->guid = protocol;
927 handler->protocol_interface = protocol_interface;
928 INIT_LIST_HEAD(&handler->open_infos);
929 list_add_tail(&handler->link, &efiobj->protocols);
930 if (!guidcmp(&efi_guid_device_path, protocol))
931 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
936 * Install protocol interface.
938 * This function implements the InstallProtocolInterface service.
939 * See the Unified Extensible Firmware Interface (UEFI) specification
942 * @handle handle on which the protocol shall be installed
943 * @protocol GUID of the protocol to be installed
944 * @protocol_interface_type type of the interface to be installed,
945 * always EFI_NATIVE_INTERFACE
946 * @protocol_interface interface of the protocol implementation
947 * @return status code
949 static efi_status_t EFIAPI efi_install_protocol_interface(
950 void **handle, const efi_guid_t *protocol,
951 int protocol_interface_type, void *protocol_interface)
955 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
958 if (!handle || !protocol ||
959 protocol_interface_type != EFI_NATIVE_INTERFACE) {
960 r = EFI_INVALID_PARAMETER;
964 /* Create new handle if requested. */
966 r = efi_create_handle(handle);
967 if (r != EFI_SUCCESS)
969 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
972 debug("%sEFI: handle %p\n", indent_string(nesting_level),
975 /* Add new protocol */
976 r = efi_add_protocol(*handle, protocol, protocol_interface);
982 * Reinstall protocol interface.
984 * This function implements the ReinstallProtocolInterface service.
985 * See the Unified Extensible Firmware Interface (UEFI) specification
988 * @handle handle on which the protocol shall be
990 * @protocol GUID of the protocol to be installed
991 * @old_interface interface to be removed
992 * @new_interface interface to be installed
993 * @return status code
995 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
996 efi_handle_t handle, const efi_guid_t *protocol,
997 void *old_interface, void *new_interface)
999 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
1001 return EFI_EXIT(EFI_ACCESS_DENIED);
1005 * Get all drivers associated to a controller.
1006 * The allocated buffer has to be freed with free().
1008 * @efiobj handle of the controller
1009 * @protocol protocol guid (optional)
1010 * @number_of_drivers number of child controllers
1011 * @driver_handle_buffer handles of the the drivers
1012 * @return status code
1014 static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1015 const efi_guid_t *protocol,
1016 efi_uintn_t *number_of_drivers,
1017 efi_handle_t **driver_handle_buffer)
1019 struct efi_handler *handler;
1020 struct efi_open_protocol_info_item *item;
1021 efi_uintn_t count = 0, i;
1024 /* Count all driver associations */
1025 list_for_each_entry(handler, &efiobj->protocols, link) {
1026 if (protocol && guidcmp(handler->guid, protocol))
1028 list_for_each_entry(item, &handler->open_infos, link) {
1029 if (item->info.attributes &
1030 EFI_OPEN_PROTOCOL_BY_DRIVER)
1035 * Create buffer. In case of duplicate driver assignments the buffer
1036 * will be too large. But that does not harm.
1038 *number_of_drivers = 0;
1039 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1040 if (!*driver_handle_buffer)
1041 return EFI_OUT_OF_RESOURCES;
1042 /* Collect unique driver handles */
1043 list_for_each_entry(handler, &efiobj->protocols, link) {
1044 if (protocol && guidcmp(handler->guid, protocol))
1046 list_for_each_entry(item, &handler->open_infos, link) {
1047 if (item->info.attributes &
1048 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1049 /* Check this is a new driver */
1051 for (i = 0; i < *number_of_drivers; ++i) {
1052 if ((*driver_handle_buffer)[i] ==
1053 item->info.agent_handle)
1056 /* Copy handle to buffer */
1058 i = (*number_of_drivers)++;
1059 (*driver_handle_buffer)[i] =
1060 item->info.agent_handle;
1069 * Disconnect all drivers from a controller.
1071 * This function implements the DisconnectController service.
1072 * See the Unified Extensible Firmware Interface (UEFI) specification
1075 * @efiobj handle of the controller
1076 * @protocol protocol guid (optional)
1077 * @child_handle handle of the child to destroy
1078 * @return status code
1080 static efi_status_t efi_disconnect_all_drivers(
1081 struct efi_object *efiobj,
1082 const efi_guid_t *protocol,
1083 efi_handle_t child_handle)
1085 efi_uintn_t number_of_drivers;
1086 efi_handle_t *driver_handle_buffer;
1087 efi_status_t r, ret;
1089 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1090 &driver_handle_buffer);
1091 if (ret != EFI_SUCCESS)
1094 ret = EFI_NOT_FOUND;
1095 while (number_of_drivers) {
1096 r = EFI_CALL(efi_disconnect_controller(
1098 driver_handle_buffer[--number_of_drivers],
1100 if (r == EFI_SUCCESS)
1103 free(driver_handle_buffer);
1108 * Uninstall protocol interface.
1110 * This function implements the UninstallProtocolInterface service.
1111 * See the Unified Extensible Firmware Interface (UEFI) specification
1114 * @handle handle from which the protocol shall be removed
1115 * @protocol GUID of the protocol to be removed
1116 * @protocol_interface interface to be removed
1117 * @return status code
1119 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
1120 efi_handle_t handle, const efi_guid_t *protocol,
1121 void *protocol_interface)
1123 struct efi_object *efiobj;
1124 struct efi_handler *handler;
1125 struct efi_open_protocol_info_item *item;
1126 struct efi_open_protocol_info_item *pos;
1129 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1132 efiobj = efi_search_obj(handle);
1134 r = EFI_INVALID_PARAMETER;
1137 /* Find the protocol on the handle */
1138 r = efi_search_protocol(handle, protocol, &handler);
1139 if (r != EFI_SUCCESS)
1141 /* Disconnect controllers */
1142 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1143 if (!list_empty(&handler->open_infos)) {
1144 r = EFI_ACCESS_DENIED;
1147 /* Close protocol */
1148 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1149 if (item->info.attributes ==
1150 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1151 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1152 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1153 list_del(&item->link);
1155 if (!list_empty(&handler->open_infos)) {
1156 r = EFI_ACCESS_DENIED;
1159 r = efi_remove_protocol(handle, protocol, protocol_interface);
1165 * Register an event for notification when a protocol is installed.
1167 * This function implements the RegisterProtocolNotify service.
1168 * See the Unified Extensible Firmware Interface (UEFI) specification
1171 * @protocol GUID of the protocol whose installation shall be
1173 * @event event to be signaled upon installation of the protocol
1174 * @registration key for retrieving the registration information
1175 * @return status code
1177 static efi_status_t EFIAPI efi_register_protocol_notify(
1178 const efi_guid_t *protocol,
1179 struct efi_event *event,
1180 void **registration)
1182 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1183 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1187 * Determine if an EFI handle implements a protocol.
1189 * See the documentation of the LocateHandle service in the UEFI specification.
1191 * @search_type selection criterion
1192 * @protocol GUID of the protocol
1193 * @search_key registration key
1195 * @return 0 if the handle implements the protocol
1197 static int efi_search(enum efi_locate_search_type search_type,
1198 const efi_guid_t *protocol, void *search_key,
1199 struct efi_object *efiobj)
1203 switch (search_type) {
1206 case BY_REGISTER_NOTIFY:
1207 /* TODO: RegisterProtocolNotify is not implemented yet */
1210 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1211 return (ret != EFI_SUCCESS);
1213 /* Invalid search type */
1219 * Locate handles implementing a protocol.
1221 * This function is meant for U-Boot internal calls. For the API implementation
1222 * of the LocateHandle service see efi_locate_handle_ext.
1224 * @search_type selection criterion
1225 * @protocol GUID of the protocol
1226 * @search_key registration key
1227 * @buffer_size size of the buffer to receive the handles in bytes
1228 * @buffer buffer to receive the relevant handles
1229 * @return status code
1231 static efi_status_t efi_locate_handle(
1232 enum efi_locate_search_type search_type,
1233 const efi_guid_t *protocol, void *search_key,
1234 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1236 struct efi_object *efiobj;
1237 efi_uintn_t size = 0;
1239 /* Check parameters */
1240 switch (search_type) {
1243 case BY_REGISTER_NOTIFY:
1245 return EFI_INVALID_PARAMETER;
1246 /* RegisterProtocolNotify is not implemented yet */
1247 return EFI_UNSUPPORTED;
1250 return EFI_INVALID_PARAMETER;
1253 return EFI_INVALID_PARAMETER;
1257 * efi_locate_handle_buffer uses this function for
1258 * the calculation of the necessary buffer size.
1259 * So do not require a buffer for buffersize == 0.
1261 if (!buffer_size || (*buffer_size && !buffer))
1262 return EFI_INVALID_PARAMETER;
1264 /* Count how much space we need */
1265 list_for_each_entry(efiobj, &efi_obj_list, link) {
1266 if (!efi_search(search_type, protocol, search_key, efiobj))
1267 size += sizeof(void *);
1270 if (*buffer_size < size) {
1271 *buffer_size = size;
1272 return EFI_BUFFER_TOO_SMALL;
1275 *buffer_size = size;
1277 return EFI_NOT_FOUND;
1279 /* Then fill the array */
1280 list_for_each_entry(efiobj, &efi_obj_list, link) {
1281 if (!efi_search(search_type, protocol, search_key, efiobj))
1282 *buffer++ = efiobj->handle;
1289 * Locate handles implementing a protocol.
1291 * This function implements the LocateHandle service.
1292 * See the Unified Extensible Firmware Interface (UEFI) specification
1295 * @search_type selection criterion
1296 * @protocol GUID of the protocol
1297 * @search_key registration key
1298 * @buffer_size size of the buffer to receive the handles in bytes
1299 * @buffer buffer to receive the relevant handles
1300 * @return 0 if the handle implements the protocol
1302 static efi_status_t EFIAPI efi_locate_handle_ext(
1303 enum efi_locate_search_type search_type,
1304 const efi_guid_t *protocol, void *search_key,
1305 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1307 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1308 buffer_size, buffer);
1310 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1311 buffer_size, buffer));
1314 /* Collapses configuration table entries, removing index i */
1315 static void efi_remove_configuration_table(int i)
1317 struct efi_configuration_table *this = &efi_conf_table[i];
1318 struct efi_configuration_table *next = &efi_conf_table[i + 1];
1319 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1321 memmove(this, next, (ulong)end - (ulong)next);
1326 * Adds, updates, or removes a configuration table.
1328 * This function is used for internal calls. For the API implementation of the
1329 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1331 * @guid GUID of the installed table
1332 * @table table to be installed
1333 * @return status code
1335 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1341 return EFI_INVALID_PARAMETER;
1343 /* Check for guid override */
1344 for (i = 0; i < systab.nr_tables; i++) {
1345 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1347 efi_conf_table[i].table = table;
1349 efi_remove_configuration_table(i);
1355 return EFI_NOT_FOUND;
1357 /* No override, check for overflow */
1358 if (i >= ARRAY_SIZE(efi_conf_table))
1359 return EFI_OUT_OF_RESOURCES;
1361 /* Add a new entry */
1362 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1363 efi_conf_table[i].table = table;
1364 systab.nr_tables = i + 1;
1370 * Adds, updates, or removes a configuration table.
1372 * This function implements the InstallConfigurationTable service.
1373 * See the Unified Extensible Firmware Interface (UEFI) specification
1376 * @guid GUID of the installed table
1377 * @table table to be installed
1378 * @return status code
1380 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1383 EFI_ENTRY("%pUl, %p", guid, table);
1384 return EFI_EXIT(efi_install_configuration_table(guid, table));
1388 * Initialize a loaded_image_info + loaded_image_info object with correct
1389 * protocols, boot-device, etc.
1391 * @info loaded image info to be passed to the entry point of the
1393 * @obj internal object associated with the loaded image
1394 * @device_path device path of the loaded image
1395 * @file_path file path of the loaded image
1396 * @return status code
1398 efi_status_t efi_setup_loaded_image(
1399 struct efi_loaded_image *info, struct efi_object *obj,
1400 struct efi_device_path *device_path,
1401 struct efi_device_path *file_path)
1405 /* Add internal object to object list */
1406 efi_add_handle(obj);
1407 /* efi_exit() assumes that the handle points to the info */
1410 info->file_path = file_path;
1413 info->device_handle = efi_dp_find_obj(device_path, NULL);
1415 * When asking for the device path interface, return
1416 * bootefi_device_path
1418 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1420 if (ret != EFI_SUCCESS)
1425 * When asking for the loaded_image interface, just
1426 * return handle which points to loaded_image_info
1428 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1429 if (ret != EFI_SUCCESS)
1432 ret = efi_add_protocol(obj->handle,
1433 &efi_guid_device_path_to_text_protocol,
1434 (void *)&efi_device_path_to_text);
1435 if (ret != EFI_SUCCESS)
1438 ret = efi_add_protocol(obj->handle,
1439 &efi_guid_device_path_utilities_protocol,
1440 (void *)&efi_device_path_utilities);
1441 if (ret != EFI_SUCCESS)
1446 printf("ERROR: Failure to install protocols for loaded image\n");
1451 * Load an image using a file path.
1453 * @file_path the path of the image to load
1454 * @buffer buffer containing the loaded image
1455 * @return status code
1457 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1460 struct efi_file_info *info = NULL;
1461 struct efi_file_handle *f;
1462 static efi_status_t ret;
1465 f = efi_file_from_path(file_path);
1467 return EFI_DEVICE_ERROR;
1470 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1472 if (ret == EFI_BUFFER_TOO_SMALL) {
1474 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1477 if (ret != EFI_SUCCESS)
1480 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1484 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1488 EFI_CALL(f->close(f));
1490 if (ret != EFI_SUCCESS) {
1491 efi_free_pool(*buffer);
1499 * Load an EFI image into memory.
1501 * This function implements the LoadImage service.
1502 * See the Unified Extensible Firmware Interface (UEFI) specification
1505 * @boot_policy true for request originating from the boot manager
1506 * @parent_image the caller's image handle
1507 * @file_path the path of the image to load
1508 * @source_buffer memory location from which the image is installed
1509 * @source_size size of the memory area from which the image is
1511 * @image_handle handle for the newly installed image
1512 * @return status code
1514 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1515 efi_handle_t parent_image,
1516 struct efi_device_path *file_path,
1517 void *source_buffer,
1518 unsigned long source_size,
1519 efi_handle_t *image_handle)
1521 struct efi_loaded_image *info;
1522 struct efi_object *obj;
1525 EFI_ENTRY("%d, %p, %pD, %p, %ld, %p", boot_policy, parent_image,
1526 file_path, source_buffer, source_size, image_handle);
1528 if (!image_handle || !parent_image) {
1529 ret = EFI_INVALID_PARAMETER;
1533 if (!source_buffer && !file_path) {
1534 ret = EFI_NOT_FOUND;
1538 info = calloc(1, sizeof(*info));
1540 ret = EFI_OUT_OF_RESOURCES;
1543 obj = calloc(1, sizeof(*obj));
1546 ret = EFI_OUT_OF_RESOURCES;
1550 if (!source_buffer) {
1551 struct efi_device_path *dp, *fp;
1553 ret = efi_load_image_from_path(file_path, &source_buffer);
1554 if (ret != EFI_SUCCESS)
1557 * split file_path which contains both the device and
1560 efi_dp_split_file_path(file_path, &dp, &fp);
1561 ret = efi_setup_loaded_image(info, obj, dp, fp);
1562 if (ret != EFI_SUCCESS)
1565 /* In this case, file_path is the "device" path, ie.
1566 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1568 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1569 if (ret != EFI_SUCCESS)
1572 info->reserved = efi_load_pe(source_buffer, info);
1573 if (!info->reserved) {
1574 ret = EFI_UNSUPPORTED;
1577 info->system_table = &systab;
1578 info->parent_handle = parent_image;
1579 *image_handle = obj->handle;
1580 return EFI_EXIT(EFI_SUCCESS);
1583 efi_delete_handle(obj);
1585 return EFI_EXIT(ret);
1589 * Call the entry point of an image.
1591 * This function implements the StartImage service.
1592 * See the Unified Extensible Firmware Interface (UEFI) specification
1595 * @image_handle handle of the image
1596 * @exit_data_size size of the buffer
1597 * @exit_data buffer to receive the exit data of the called image
1598 * @return status code
1600 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1601 unsigned long *exit_data_size,
1604 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1605 struct efi_system_table *st);
1606 struct efi_loaded_image *info = image_handle;
1609 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1610 entry = info->reserved;
1612 efi_is_direct_boot = false;
1614 /* call the image! */
1615 if (setjmp(&info->exit_jmp)) {
1617 * We called the entry point of the child image with EFI_CALL
1618 * in the lines below. The child image called the Exit() boot
1619 * service efi_exit() which executed the long jump that brought
1620 * us to the current line. This implies that the second half
1621 * of the EFI_CALL macro has not been executed.
1625 * efi_exit() called efi_restore_gd(). We have to undo this
1626 * otherwise __efi_entry_check() will put the wrong value into
1632 * To get ready to call EFI_EXIT below we have to execute the
1633 * missed out steps of EFI_CALL.
1635 assert(__efi_entry_check());
1636 debug("%sEFI: %lu returned by started image\n",
1637 __efi_nesting_dec(),
1638 (unsigned long)((uintptr_t)info->exit_status &
1640 return EFI_EXIT(info->exit_status);
1643 ret = EFI_CALL(entry(image_handle, &systab));
1646 * Usually UEFI applications call Exit() instead of returning.
1647 * But because the world doesn not consist of ponies and unicorns,
1648 * we're happy to emulate that behavior on behalf of a payload
1651 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
1655 * Leave an EFI application or driver.
1657 * This function implements the Exit service.
1658 * See the Unified Extensible Firmware Interface (UEFI) specification
1661 * @image_handle handle of the application or driver that is exiting
1662 * @exit_status status code
1663 * @exit_data_size size of the buffer in bytes
1664 * @exit_data buffer with data describing an error
1665 * @return status code
1667 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1668 efi_status_t exit_status,
1669 unsigned long exit_data_size,
1673 * We require that the handle points to the original loaded
1674 * image protocol interface.
1676 * For getting the longjmp address this is safer than locating
1677 * the protocol because the protocol may have been reinstalled
1678 * pointing to another memory location.
1680 * TODO: We should call the unload procedure of the loaded
1683 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
1685 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1686 exit_data_size, exit_data);
1688 /* Make sure entry/exit counts for EFI world cross-overs match */
1689 EFI_EXIT(exit_status);
1692 * But longjmp out with the U-Boot gd, not the application's, as
1693 * the other end is a setjmp call inside EFI context.
1697 loaded_image_info->exit_status = exit_status;
1698 longjmp(&loaded_image_info->exit_jmp, 1);
1700 panic("EFI application exited");
1704 * Unload an EFI image.
1706 * This function implements the UnloadImage service.
1707 * See the Unified Extensible Firmware Interface (UEFI) specification
1710 * @image_handle handle of the image to be unloaded
1711 * @return status code
1713 static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1715 struct efi_object *efiobj;
1717 EFI_ENTRY("%p", image_handle);
1718 efiobj = efi_search_obj(image_handle);
1720 list_del(&efiobj->link);
1722 return EFI_EXIT(EFI_SUCCESS);
1726 * Fix up caches for EFI payloads if necessary.
1728 static void efi_exit_caches(void)
1730 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1732 * Grub on 32bit ARM needs to have caches disabled before jumping into
1733 * a zImage, but does not know of all cache layers. Give it a hand.
1735 if (efi_is_direct_boot)
1736 cleanup_before_linux();
1741 * Stop all boot services.
1743 * This function implements the ExitBootServices service.
1744 * See the Unified Extensible Firmware Interface (UEFI) specification
1747 * All timer events are disabled.
1748 * For exit boot services events the notification function is called.
1749 * The boot services are disabled in the system table.
1751 * @image_handle handle of the loaded image
1752 * @map_key key of the memory map
1753 * @return status code
1755 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1756 unsigned long map_key)
1760 EFI_ENTRY("%p, %ld", image_handle, map_key);
1762 /* Make sure that notification functions are not called anymore */
1763 efi_tpl = TPL_HIGH_LEVEL;
1765 /* Check if ExitBootServices has already been called */
1766 if (!systab.boottime)
1767 return EFI_EXIT(EFI_SUCCESS);
1769 /* Notify that ExitBootServices is invoked. */
1770 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1771 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1773 efi_events[i].is_signaled = true;
1774 efi_signal_event(&efi_events[i], false);
1777 /* TODO Should persist EFI variables here */
1779 board_quiesce_devices();
1781 /* Fix up caches for EFI payloads if necessary */
1784 /* This stops all lingering devices */
1785 bootm_disable_interrupts();
1787 /* Disable boottime services */
1788 systab.con_in_handle = NULL;
1789 systab.con_in = NULL;
1790 systab.con_out_handle = NULL;
1791 systab.con_out = NULL;
1792 systab.stderr_handle = NULL;
1793 systab.std_err = NULL;
1794 systab.boottime = NULL;
1796 /* Recalculate CRC32 */
1797 systab.hdr.crc32 = 0;
1798 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1799 sizeof(struct efi_system_table));
1801 /* Give the payload some time to boot */
1802 efi_set_watchdog(0);
1805 return EFI_EXIT(EFI_SUCCESS);
1809 * Get next value of the counter.
1811 * This function implements the NextMonotonicCount service.
1812 * See the Unified Extensible Firmware Interface (UEFI) specification
1815 * @count returned value of the counter
1816 * @return status code
1818 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1820 static uint64_t mono;
1822 EFI_ENTRY("%p", count);
1824 return EFI_EXIT(EFI_SUCCESS);
1830 * This function implements the Stall sercive.
1831 * See the Unified Extensible Firmware Interface (UEFI) specification
1834 * @microseconds period to sleep in microseconds
1835 * @return status code
1837 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1839 EFI_ENTRY("%ld", microseconds);
1840 udelay(microseconds);
1841 return EFI_EXIT(EFI_SUCCESS);
1845 * Reset the watchdog timer.
1847 * This function implements the SetWatchdogTimer service.
1848 * See the Unified Extensible Firmware Interface (UEFI) specification
1851 * @timeout seconds before reset by watchdog
1852 * @watchdog_code code to be logged when resetting
1853 * @data_size size of buffer in bytes
1854 * @watchdog_data buffer with data describing the reset reason
1855 * @return status code
1857 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1858 uint64_t watchdog_code,
1859 unsigned long data_size,
1860 uint16_t *watchdog_data)
1862 EFI_ENTRY("%ld, 0x%" PRIx64 ", %ld, %p", timeout, watchdog_code,
1863 data_size, watchdog_data);
1864 return EFI_EXIT(efi_set_watchdog(timeout));
1870 * This function implements the CloseProtocol service.
1871 * See the Unified Extensible Firmware Interface (UEFI) specification
1874 * @handle handle on which the protocol shall be closed
1875 * @protocol GUID of the protocol to close
1876 * @agent_handle handle of the driver
1877 * @controller_handle handle of the controller
1878 * @return status code
1880 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1881 const efi_guid_t *protocol,
1882 efi_handle_t agent_handle,
1883 efi_handle_t controller_handle)
1885 struct efi_handler *handler;
1886 struct efi_open_protocol_info_item *item;
1887 struct efi_open_protocol_info_item *pos;
1890 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1893 if (!agent_handle) {
1894 r = EFI_INVALID_PARAMETER;
1897 r = efi_search_protocol(handle, protocol, &handler);
1898 if (r != EFI_SUCCESS)
1902 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1903 if (item->info.agent_handle == agent_handle &&
1904 item->info.controller_handle == controller_handle) {
1905 efi_delete_open_info(item);
1915 * Provide information about then open status of a protocol on a handle
1917 * This function implements the OpenProtocolInformation service.
1918 * See the Unified Extensible Firmware Interface (UEFI) specification
1921 * @handle handle for which the information shall be retrieved
1922 * @protocol GUID of the protocol
1923 * @entry_buffer buffer to receive the open protocol information
1924 * @entry_count number of entries available in the buffer
1925 * @return status code
1927 static efi_status_t EFIAPI efi_open_protocol_information(
1928 efi_handle_t handle, const efi_guid_t *protocol,
1929 struct efi_open_protocol_info_entry **entry_buffer,
1930 efi_uintn_t *entry_count)
1932 unsigned long buffer_size;
1933 unsigned long count;
1934 struct efi_handler *handler;
1935 struct efi_open_protocol_info_item *item;
1938 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1941 /* Check parameters */
1942 if (!entry_buffer) {
1943 r = EFI_INVALID_PARAMETER;
1946 r = efi_search_protocol(handle, protocol, &handler);
1947 if (r != EFI_SUCCESS)
1952 list_for_each_entry(item, &handler->open_infos, link) {
1953 if (item->info.open_count)
1956 *entry_count = count;
1957 *entry_buffer = NULL;
1964 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
1965 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1966 (void **)entry_buffer);
1967 if (r != EFI_SUCCESS)
1969 list_for_each_entry_reverse(item, &handler->open_infos, link) {
1970 if (item->info.open_count)
1971 (*entry_buffer)[--count] = item->info;
1978 * Get protocols installed on a handle.
1980 * This function implements the ProtocolsPerHandleService.
1981 * See the Unified Extensible Firmware Interface (UEFI) specification
1984 * @handle handle for which the information is retrieved
1985 * @protocol_buffer buffer with protocol GUIDs
1986 * @protocol_buffer_count number of entries in the buffer
1987 * @return status code
1989 static efi_status_t EFIAPI efi_protocols_per_handle(
1990 efi_handle_t handle, efi_guid_t ***protocol_buffer,
1991 efi_uintn_t *protocol_buffer_count)
1993 unsigned long buffer_size;
1994 struct efi_object *efiobj;
1995 struct list_head *protocol_handle;
1998 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1999 protocol_buffer_count);
2001 if (!handle || !protocol_buffer || !protocol_buffer_count)
2002 return EFI_EXIT(EFI_INVALID_PARAMETER);
2004 *protocol_buffer = NULL;
2005 *protocol_buffer_count = 0;
2007 efiobj = efi_search_obj(handle);
2009 return EFI_EXIT(EFI_INVALID_PARAMETER);
2011 /* Count protocols */
2012 list_for_each(protocol_handle, &efiobj->protocols) {
2013 ++*protocol_buffer_count;
2017 if (*protocol_buffer_count) {
2020 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2021 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2022 (void **)protocol_buffer);
2023 if (r != EFI_SUCCESS)
2025 list_for_each(protocol_handle, &efiobj->protocols) {
2026 struct efi_handler *protocol;
2028 protocol = list_entry(protocol_handle,
2029 struct efi_handler, link);
2030 (*protocol_buffer)[j] = (void *)protocol->guid;
2035 return EFI_EXIT(EFI_SUCCESS);
2039 * Locate handles implementing a protocol.
2041 * This function implements the LocateHandleBuffer service.
2042 * See the Unified Extensible Firmware Interface (UEFI) specification
2045 * @search_type selection criterion
2046 * @protocol GUID of the protocol
2047 * @search_key registration key
2048 * @no_handles number of returned handles
2049 * @buffer buffer with the returned handles
2050 * @return status code
2052 static efi_status_t EFIAPI efi_locate_handle_buffer(
2053 enum efi_locate_search_type search_type,
2054 const efi_guid_t *protocol, void *search_key,
2055 efi_uintn_t *no_handles, efi_handle_t **buffer)
2058 efi_uintn_t buffer_size = 0;
2060 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2061 no_handles, buffer);
2063 if (!no_handles || !buffer) {
2064 r = EFI_INVALID_PARAMETER;
2069 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2071 if (r != EFI_BUFFER_TOO_SMALL)
2073 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2075 if (r != EFI_SUCCESS)
2077 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2079 if (r == EFI_SUCCESS)
2080 *no_handles = buffer_size / sizeof(efi_handle_t);
2086 * Find an interface implementing a protocol.
2088 * This function implements the LocateProtocol service.
2089 * See the Unified Extensible Firmware Interface (UEFI) specification
2092 * @protocol GUID of the protocol
2093 * @registration registration key passed to the notification function
2094 * @protocol_interface interface implementing the protocol
2095 * @return status code
2097 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2099 void **protocol_interface)
2101 struct list_head *lhandle;
2104 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2106 if (!protocol || !protocol_interface)
2107 return EFI_EXIT(EFI_INVALID_PARAMETER);
2109 list_for_each(lhandle, &efi_obj_list) {
2110 struct efi_object *efiobj;
2111 struct efi_handler *handler;
2113 efiobj = list_entry(lhandle, struct efi_object, link);
2115 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2116 if (ret == EFI_SUCCESS) {
2117 *protocol_interface = handler->protocol_interface;
2118 return EFI_EXIT(EFI_SUCCESS);
2121 *protocol_interface = NULL;
2123 return EFI_EXIT(EFI_NOT_FOUND);
2127 * Get the device path and handle of an device implementing a protocol.
2129 * This function implements the LocateDevicePath service.
2130 * See the Unified Extensible Firmware Interface (UEFI) specification
2133 * @protocol GUID of the protocol
2134 * @device_path device path
2135 * @device handle of the device
2136 * @return status code
2138 static efi_status_t EFIAPI efi_locate_device_path(
2139 const efi_guid_t *protocol,
2140 struct efi_device_path **device_path,
2141 efi_handle_t *device)
2143 struct efi_device_path *dp;
2145 struct efi_handler *handler;
2146 efi_handle_t *handles;
2148 size_t len_best = 0;
2149 efi_uintn_t no_handles;
2153 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2155 if (!protocol || !device_path || !*device_path || !device) {
2156 ret = EFI_INVALID_PARAMETER;
2160 /* Find end of device path */
2161 len = efi_dp_size(*device_path);
2163 /* Get all handles implementing the protocol */
2164 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2165 &no_handles, &handles));
2166 if (ret != EFI_SUCCESS)
2169 for (i = 0; i < no_handles; ++i) {
2170 /* Find the device path protocol */
2171 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2173 if (ret != EFI_SUCCESS)
2175 dp = (struct efi_device_path *)handler->protocol_interface;
2176 len_dp = efi_dp_size(dp);
2178 * This handle can only be a better fit
2179 * if its device path length is longer than the best fit and
2180 * if its device path length is shorter of equal the searched
2183 if (len_dp <= len_best || len_dp > len)
2185 /* Check if dp is a subpath of device_path */
2186 if (memcmp(*device_path, dp, len_dp))
2188 *device = handles[i];
2192 remainder = (u8 *)*device_path + len_best;
2193 *device_path = (struct efi_device_path *)remainder;
2196 ret = EFI_NOT_FOUND;
2199 return EFI_EXIT(ret);
2203 * Install multiple protocol interfaces.
2205 * This function implements the MultipleProtocolInterfaces service.
2206 * See the Unified Extensible Firmware Interface (UEFI) specification
2209 * @handle handle on which the protocol interfaces shall be installed
2210 * @... NULL terminated argument list with pairs of protocol GUIDS and
2212 * @return status code
2214 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2217 EFI_ENTRY("%p", handle);
2220 const efi_guid_t *protocol;
2221 void *protocol_interface;
2222 efi_status_t r = EFI_SUCCESS;
2226 return EFI_EXIT(EFI_INVALID_PARAMETER);
2228 va_start(argptr, handle);
2230 protocol = va_arg(argptr, efi_guid_t*);
2233 protocol_interface = va_arg(argptr, void*);
2234 r = EFI_CALL(efi_install_protocol_interface(
2236 EFI_NATIVE_INTERFACE,
2237 protocol_interface));
2238 if (r != EFI_SUCCESS)
2243 if (r == EFI_SUCCESS)
2246 /* If an error occurred undo all changes. */
2247 va_start(argptr, handle);
2249 protocol = va_arg(argptr, efi_guid_t*);
2250 protocol_interface = va_arg(argptr, void*);
2251 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2252 protocol_interface));
2260 * Uninstall multiple protocol interfaces.
2262 * This function implements the UninstallMultipleProtocolInterfaces service.
2263 * See the Unified Extensible Firmware Interface (UEFI) specification
2266 * @handle handle from which the protocol interfaces shall be removed
2267 * @... NULL terminated argument list with pairs of protocol GUIDS and
2269 * @return status code
2271 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2274 EFI_ENTRY("%p", handle);
2277 const efi_guid_t *protocol;
2278 void *protocol_interface;
2279 efi_status_t r = EFI_SUCCESS;
2283 return EFI_EXIT(EFI_INVALID_PARAMETER);
2285 va_start(argptr, handle);
2287 protocol = va_arg(argptr, efi_guid_t*);
2290 protocol_interface = va_arg(argptr, void*);
2291 r = EFI_CALL(efi_uninstall_protocol_interface(
2293 protocol_interface));
2294 if (r != EFI_SUCCESS)
2299 if (r == EFI_SUCCESS)
2302 /* If an error occurred undo all changes. */
2303 va_start(argptr, handle);
2305 protocol = va_arg(argptr, efi_guid_t*);
2306 protocol_interface = va_arg(argptr, void*);
2307 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2308 EFI_NATIVE_INTERFACE,
2309 protocol_interface));
2317 * Calculate cyclic redundancy code.
2319 * This function implements the CalculateCrc32 service.
2320 * See the Unified Extensible Firmware Interface (UEFI) specification
2323 * @data buffer with data
2324 * @data_size size of buffer in bytes
2325 * @crc32_p cyclic redundancy code
2326 * @return status code
2328 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2329 unsigned long data_size,
2332 EFI_ENTRY("%p, %ld", data, data_size);
2333 *crc32_p = crc32(0, data, data_size);
2334 return EFI_EXIT(EFI_SUCCESS);
2340 * This function implements the CopyMem service.
2341 * See the Unified Extensible Firmware Interface (UEFI) specification
2344 * @destination destination of the copy operation
2345 * @source source of the copy operation
2346 * @length number of bytes to copy
2348 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2351 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2352 memcpy(destination, source, length);
2353 EFI_EXIT(EFI_SUCCESS);
2357 * Fill memory with a byte value.
2359 * This function implements the SetMem service.
2360 * See the Unified Extensible Firmware Interface (UEFI) specification
2363 * @buffer buffer to fill
2364 * @size size of buffer in bytes
2365 * @value byte to copy to the buffer
2367 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2369 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2370 memset(buffer, value, size);
2371 EFI_EXIT(EFI_SUCCESS);
2375 * Open protocol interface on a handle.
2377 * @handler handler of a protocol
2378 * @protocol_interface interface implementing the protocol
2379 * @agent_handle handle of the driver
2380 * @controller_handle handle of the controller
2381 * @attributes attributes indicating how to open the protocol
2382 * @return status code
2384 static efi_status_t efi_protocol_open(
2385 struct efi_handler *handler,
2386 void **protocol_interface, void *agent_handle,
2387 void *controller_handle, uint32_t attributes)
2389 struct efi_open_protocol_info_item *item;
2390 struct efi_open_protocol_info_entry *match = NULL;
2391 bool opened_by_driver = false;
2392 bool opened_exclusive = false;
2394 /* If there is no agent, only return the interface */
2398 /* For TEST_PROTOCOL ignore interface attribute */
2399 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2400 *protocol_interface = NULL;
2403 * Check if the protocol is already opened by a driver with the same
2404 * attributes or opened exclusively
2406 list_for_each_entry(item, &handler->open_infos, link) {
2407 if (item->info.agent_handle == agent_handle) {
2408 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2409 (item->info.attributes == attributes))
2410 return EFI_ALREADY_STARTED;
2412 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2413 opened_exclusive = true;
2416 /* Only one controller can open the protocol exclusively */
2417 if (opened_exclusive && attributes &
2418 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2419 return EFI_ACCESS_DENIED;
2421 /* Prepare exclusive opening */
2422 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2423 /* Try to disconnect controllers */
2424 list_for_each_entry(item, &handler->open_infos, link) {
2425 if (item->info.attributes ==
2426 EFI_OPEN_PROTOCOL_BY_DRIVER)
2427 EFI_CALL(efi_disconnect_controller(
2428 item->info.controller_handle,
2429 item->info.agent_handle,
2432 opened_by_driver = false;
2433 /* Check if all controllers are disconnected */
2434 list_for_each_entry(item, &handler->open_infos, link) {
2435 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2436 opened_by_driver = true;
2438 /* Only one controller can be conncected */
2439 if (opened_by_driver)
2440 return EFI_ACCESS_DENIED;
2443 /* Find existing entry */
2444 list_for_each_entry(item, &handler->open_infos, link) {
2445 if (item->info.agent_handle == agent_handle &&
2446 item->info.controller_handle == controller_handle)
2447 match = &item->info;
2449 /* None found, create one */
2451 match = efi_create_open_info(handler);
2453 return EFI_OUT_OF_RESOURCES;
2456 match->agent_handle = agent_handle;
2457 match->controller_handle = controller_handle;
2458 match->attributes = attributes;
2459 match->open_count++;
2462 /* For TEST_PROTOCOL ignore interface attribute. */
2463 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2464 *protocol_interface = handler->protocol_interface;
2470 * Open protocol interface on a handle.
2472 * This function implements the OpenProtocol interface.
2473 * See the Unified Extensible Firmware Interface (UEFI) specification
2476 * @handle handle on which the protocol shall be opened
2477 * @protocol GUID of the protocol
2478 * @protocol_interface interface implementing the protocol
2479 * @agent_handle handle of the driver
2480 * @controller_handle handle of the controller
2481 * @attributes attributes indicating how to open the protocol
2482 * @return status code
2484 static efi_status_t EFIAPI efi_open_protocol(
2485 void *handle, const efi_guid_t *protocol,
2486 void **protocol_interface, void *agent_handle,
2487 void *controller_handle, uint32_t attributes)
2489 struct efi_handler *handler;
2490 efi_status_t r = EFI_INVALID_PARAMETER;
2492 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2493 protocol_interface, agent_handle, controller_handle,
2496 if (!handle || !protocol ||
2497 (!protocol_interface && attributes !=
2498 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2502 switch (attributes) {
2503 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2504 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2505 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2507 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2508 if (controller_handle == handle)
2511 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2512 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2513 /* Check that the controller handle is valid */
2514 if (!efi_search_obj(controller_handle))
2517 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2518 /* Check that the agent handle is valid */
2519 if (!efi_search_obj(agent_handle))
2526 r = efi_search_protocol(handle, protocol, &handler);
2527 if (r != EFI_SUCCESS)
2530 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2531 controller_handle, attributes);
2537 * Get interface of a protocol on a handle.
2539 * This function implements the HandleProtocol service.
2540 * See the Unified Extensible Firmware Interface (UEFI) specification
2543 * @handle handle on which the protocol shall be opened
2544 * @protocol GUID of the protocol
2545 * @protocol_interface interface implementing the protocol
2546 * @return status code
2548 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2549 const efi_guid_t *protocol,
2550 void **protocol_interface)
2552 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2553 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2556 static efi_status_t efi_bind_controller(
2557 efi_handle_t controller_handle,
2558 efi_handle_t driver_image_handle,
2559 struct efi_device_path *remain_device_path)
2561 struct efi_driver_binding_protocol *binding_protocol;
2564 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2565 &efi_guid_driver_binding_protocol,
2566 (void **)&binding_protocol,
2567 driver_image_handle, NULL,
2568 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2569 if (r != EFI_SUCCESS)
2571 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2573 remain_device_path));
2574 if (r == EFI_SUCCESS)
2575 r = EFI_CALL(binding_protocol->start(binding_protocol,
2577 remain_device_path));
2578 EFI_CALL(efi_close_protocol(driver_image_handle,
2579 &efi_guid_driver_binding_protocol,
2580 driver_image_handle, NULL));
2584 static efi_status_t efi_connect_single_controller(
2585 efi_handle_t controller_handle,
2586 efi_handle_t *driver_image_handle,
2587 struct efi_device_path *remain_device_path)
2589 efi_handle_t *buffer;
2593 size_t connected = 0;
2595 /* Get buffer with all handles with driver binding protocol */
2596 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2597 &efi_guid_driver_binding_protocol,
2598 NULL, &count, &buffer));
2599 if (r != EFI_SUCCESS)
2602 /* Context Override */
2603 if (driver_image_handle) {
2604 for (; *driver_image_handle; ++driver_image_handle) {
2605 for (i = 0; i < count; ++i) {
2606 if (buffer[i] == *driver_image_handle) {
2608 r = efi_bind_controller(
2610 *driver_image_handle,
2611 remain_device_path);
2613 * For drivers that do not support the
2614 * controller or are already connected
2615 * we receive an error code here.
2617 if (r == EFI_SUCCESS)
2625 * TODO: Some overrides are not yet implemented:
2626 * - Platform Driver Override
2627 * - Driver Family Override Search
2628 * - Bus Specific Driver Override
2631 /* Driver Binding Search */
2632 for (i = 0; i < count; ++i) {
2634 r = efi_bind_controller(controller_handle,
2636 remain_device_path);
2637 if (r == EFI_SUCCESS)
2642 efi_free_pool(buffer);
2644 return EFI_NOT_FOUND;
2649 * Connect a controller to a driver.
2651 * This function implements the ConnectController service.
2652 * See the Unified Extensible Firmware Interface (UEFI) specification
2655 * First all driver binding protocol handles are tried for binding drivers.
2656 * Afterwards all handles that have openened a protocol of the controller
2657 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2659 * @controller_handle handle of the controller
2660 * @driver_image_handle handle of the driver
2661 * @remain_device_path device path of a child controller
2662 * @recursive true to connect all child controllers
2663 * @return status code
2665 static efi_status_t EFIAPI efi_connect_controller(
2666 efi_handle_t controller_handle,
2667 efi_handle_t *driver_image_handle,
2668 struct efi_device_path *remain_device_path,
2672 efi_status_t ret = EFI_NOT_FOUND;
2673 struct efi_object *efiobj;
2675 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2676 remain_device_path, recursive);
2678 efiobj = efi_search_obj(controller_handle);
2680 ret = EFI_INVALID_PARAMETER;
2684 r = efi_connect_single_controller(controller_handle,
2685 driver_image_handle,
2686 remain_device_path);
2687 if (r == EFI_SUCCESS)
2690 struct efi_handler *handler;
2691 struct efi_open_protocol_info_item *item;
2693 list_for_each_entry(handler, &efiobj->protocols, link) {
2694 list_for_each_entry(item, &handler->open_infos, link) {
2695 if (item->info.attributes &
2696 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2697 r = EFI_CALL(efi_connect_controller(
2698 item->info.controller_handle,
2699 driver_image_handle,
2702 if (r == EFI_SUCCESS)
2708 /* Check for child controller specified by end node */
2709 if (ret != EFI_SUCCESS && remain_device_path &&
2710 remain_device_path->type == DEVICE_PATH_TYPE_END)
2713 return EFI_EXIT(ret);
2717 * Get all child controllers associated to a driver.
2718 * The allocated buffer has to be freed with free().
2720 * @efiobj handle of the controller
2721 * @driver_handle handle of the driver
2722 * @number_of_children number of child controllers
2723 * @child_handle_buffer handles of the the child controllers
2725 static efi_status_t efi_get_child_controllers(
2726 struct efi_object *efiobj,
2727 efi_handle_t driver_handle,
2728 efi_uintn_t *number_of_children,
2729 efi_handle_t **child_handle_buffer)
2731 struct efi_handler *handler;
2732 struct efi_open_protocol_info_item *item;
2733 efi_uintn_t count = 0, i;
2736 /* Count all child controller associations */
2737 list_for_each_entry(handler, &efiobj->protocols, link) {
2738 list_for_each_entry(item, &handler->open_infos, link) {
2739 if (item->info.agent_handle == driver_handle &&
2740 item->info.attributes &
2741 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2746 * Create buffer. In case of duplicate child controller assignments
2747 * the buffer will be too large. But that does not harm.
2749 *number_of_children = 0;
2750 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2751 if (!*child_handle_buffer)
2752 return EFI_OUT_OF_RESOURCES;
2753 /* Copy unique child handles */
2754 list_for_each_entry(handler, &efiobj->protocols, link) {
2755 list_for_each_entry(item, &handler->open_infos, link) {
2756 if (item->info.agent_handle == driver_handle &&
2757 item->info.attributes &
2758 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2759 /* Check this is a new child controller */
2761 for (i = 0; i < *number_of_children; ++i) {
2762 if ((*child_handle_buffer)[i] ==
2763 item->info.controller_handle)
2766 /* Copy handle to buffer */
2768 i = (*number_of_children)++;
2769 (*child_handle_buffer)[i] =
2770 item->info.controller_handle;
2779 * Disconnect a controller from a driver.
2781 * This function implements the DisconnectController service.
2782 * See the Unified Extensible Firmware Interface (UEFI) specification
2785 * @controller_handle handle of the controller
2786 * @driver_image_handle handle of the driver
2787 * @child_handle handle of the child to destroy
2788 * @return status code
2790 static efi_status_t EFIAPI efi_disconnect_controller(
2791 efi_handle_t controller_handle,
2792 efi_handle_t driver_image_handle,
2793 efi_handle_t child_handle)
2795 struct efi_driver_binding_protocol *binding_protocol;
2796 efi_handle_t *child_handle_buffer = NULL;
2797 size_t number_of_children = 0;
2799 size_t stop_count = 0;
2800 struct efi_object *efiobj;
2802 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2805 efiobj = efi_search_obj(controller_handle);
2807 r = EFI_INVALID_PARAMETER;
2811 if (child_handle && !efi_search_obj(child_handle)) {
2812 r = EFI_INVALID_PARAMETER;
2816 /* If no driver handle is supplied, disconnect all drivers */
2817 if (!driver_image_handle) {
2818 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2822 /* Create list of child handles */
2824 number_of_children = 1;
2825 child_handle_buffer = &child_handle;
2827 efi_get_child_controllers(efiobj,
2828 driver_image_handle,
2829 &number_of_children,
2830 &child_handle_buffer);
2833 /* Get the driver binding protocol */
2834 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2835 &efi_guid_driver_binding_protocol,
2836 (void **)&binding_protocol,
2837 driver_image_handle, NULL,
2838 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2839 if (r != EFI_SUCCESS)
2841 /* Remove the children */
2842 if (number_of_children) {
2843 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2846 child_handle_buffer));
2847 if (r == EFI_SUCCESS)
2850 /* Remove the driver */
2852 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2855 if (r == EFI_SUCCESS)
2857 EFI_CALL(efi_close_protocol(driver_image_handle,
2858 &efi_guid_driver_binding_protocol,
2859 driver_image_handle, NULL));
2867 free(child_handle_buffer);
2871 static const struct efi_boot_services efi_boot_services = {
2873 .headersize = sizeof(struct efi_table_hdr),
2875 .raise_tpl = efi_raise_tpl,
2876 .restore_tpl = efi_restore_tpl,
2877 .allocate_pages = efi_allocate_pages_ext,
2878 .free_pages = efi_free_pages_ext,
2879 .get_memory_map = efi_get_memory_map_ext,
2880 .allocate_pool = efi_allocate_pool_ext,
2881 .free_pool = efi_free_pool_ext,
2882 .create_event = efi_create_event_ext,
2883 .set_timer = efi_set_timer_ext,
2884 .wait_for_event = efi_wait_for_event,
2885 .signal_event = efi_signal_event_ext,
2886 .close_event = efi_close_event,
2887 .check_event = efi_check_event,
2888 .install_protocol_interface = efi_install_protocol_interface,
2889 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2890 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2891 .handle_protocol = efi_handle_protocol,
2893 .register_protocol_notify = efi_register_protocol_notify,
2894 .locate_handle = efi_locate_handle_ext,
2895 .locate_device_path = efi_locate_device_path,
2896 .install_configuration_table = efi_install_configuration_table_ext,
2897 .load_image = efi_load_image,
2898 .start_image = efi_start_image,
2900 .unload_image = efi_unload_image,
2901 .exit_boot_services = efi_exit_boot_services,
2902 .get_next_monotonic_count = efi_get_next_monotonic_count,
2904 .set_watchdog_timer = efi_set_watchdog_timer,
2905 .connect_controller = efi_connect_controller,
2906 .disconnect_controller = efi_disconnect_controller,
2907 .open_protocol = efi_open_protocol,
2908 .close_protocol = efi_close_protocol,
2909 .open_protocol_information = efi_open_protocol_information,
2910 .protocols_per_handle = efi_protocols_per_handle,
2911 .locate_handle_buffer = efi_locate_handle_buffer,
2912 .locate_protocol = efi_locate_protocol,
2913 .install_multiple_protocol_interfaces =
2914 efi_install_multiple_protocol_interfaces,
2915 .uninstall_multiple_protocol_interfaces =
2916 efi_uninstall_multiple_protocol_interfaces,
2917 .calculate_crc32 = efi_calculate_crc32,
2918 .copy_mem = efi_copy_mem,
2919 .set_mem = efi_set_mem,
2920 .create_event_ex = efi_create_event_ex,
2923 static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
2925 struct efi_system_table __efi_runtime_data systab = {
2927 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2928 .revision = 2 << 16 | 70, /* 2.7 */
2929 .headersize = sizeof(struct efi_table_hdr),
2931 .fw_vendor = (long)firmware_vendor,
2932 .con_in = (void *)&efi_con_in,
2933 .con_out = (void *)&efi_con_out,
2934 .std_err = (void *)&efi_con_out,
2935 .runtime = (void *)&efi_runtime_services,
2936 .boottime = (void *)&efi_boot_services,
2938 .tables = (void *)efi_conf_table,