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);
29 /* List of all events */
30 static LIST_HEAD(efi_events);
33 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
34 * we need to do trickery with caches. Since we don't want to break the EFI
35 * aware boot path, only apply hacks when loading exiting directly (breaking
36 * direct Linux EFI booting along the way - oh well).
38 static bool efi_is_direct_boot = true;
41 * EFI can pass arbitrary additional "tables" containing vendor specific
42 * information to the payload. One such table is the FDT table which contains
43 * a pointer to a flattened device tree blob.
45 * In most cases we want to pass an FDT to the payload, so reserve one slot of
46 * config table space for it. The pointer gets populated by do_bootefi_exec().
48 static struct efi_configuration_table __efi_runtime_data efi_conf_table[2];
52 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
53 * fixed when compiling U-Boot. However, the payload does not know about that
54 * restriction so we need to manually swap its and our view of that register on
55 * EFI callback entry/exit.
57 static volatile void *efi_gd, *app_gd;
60 static int entry_count;
61 static int nesting_level;
62 /* GUID of the device tree table */
63 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
64 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
65 const efi_guid_t efi_guid_driver_binding_protocol =
66 EFI_DRIVER_BINDING_PROTOCOL_GUID;
68 static efi_status_t EFIAPI efi_disconnect_controller(
69 efi_handle_t controller_handle,
70 efi_handle_t driver_image_handle,
71 efi_handle_t child_handle);
73 /* Called on every callback entry */
74 int __efi_entry_check(void)
76 int ret = entry_count++ == 0;
85 /* Called on every callback exit */
86 int __efi_exit_check(void)
88 int ret = --entry_count == 0;
95 /* Called from do_bootefi_exec() */
96 void efi_save_gd(void)
104 * Special case handler for error/abort that just forces things back
105 * to u-boot world so we can dump out an abort msg, without any care
106 * about returning back to UEFI world.
108 void efi_restore_gd(void)
111 /* Only restore if we're already in EFI context */
119 * Return a string for indenting with two spaces per level. A maximum of ten
120 * indent levels is supported. Higher indent levels will be truncated.
122 * @level indent level
123 * @return indent string
125 static const char *indent_string(int level)
127 const char *indent = " ";
128 const int max = strlen(indent);
130 level = min(max, level * 2);
131 return &indent[max - level];
134 const char *__efi_nesting(void)
136 return indent_string(nesting_level);
139 const char *__efi_nesting_inc(void)
141 return indent_string(nesting_level++);
144 const char *__efi_nesting_dec(void)
146 return indent_string(--nesting_level);
150 * Queue an EFI event.
152 * This function queues the notification function of the event for future
155 * The notification function is called if the task priority level of the
156 * event is higher than the current task priority level.
158 * For the SignalEvent service see efi_signal_event_ext.
160 * @event event to signal
161 * @check_tpl check the TPL level
163 void efi_signal_event(struct efi_event *event, bool check_tpl)
165 if (event->notify_function) {
166 event->is_queued = true;
168 if (check_tpl && efi_tpl >= event->notify_tpl)
170 EFI_CALL_VOID(event->notify_function(event,
171 event->notify_context));
173 event->is_queued = false;
177 * Raise the task priority level.
179 * This function implements the RaiseTpl service.
180 * See the Unified Extensible Firmware Interface (UEFI) specification
183 * @new_tpl new value of the task priority level
184 * @return old value of the task priority level
186 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
188 efi_uintn_t old_tpl = efi_tpl;
190 EFI_ENTRY("0x%zx", new_tpl);
192 if (new_tpl < efi_tpl)
193 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
195 if (efi_tpl > TPL_HIGH_LEVEL)
196 efi_tpl = TPL_HIGH_LEVEL;
198 EFI_EXIT(EFI_SUCCESS);
203 * Lower the task priority level.
205 * This function implements the RestoreTpl service.
206 * See the Unified Extensible Firmware Interface (UEFI) specification
209 * @old_tpl value of the task priority level to be restored
211 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
213 EFI_ENTRY("0x%zx", old_tpl);
215 if (old_tpl > efi_tpl)
216 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
218 if (efi_tpl > TPL_HIGH_LEVEL)
219 efi_tpl = TPL_HIGH_LEVEL;
221 EFI_EXIT(EFI_SUCCESS);
225 * Allocate memory pages.
227 * This function implements the AllocatePages service.
228 * See the Unified Extensible Firmware Interface (UEFI) specification
231 * @type type of allocation to be performed
232 * @memory_type usage type of the allocated memory
233 * @pages number of pages to be allocated
234 * @memory allocated memory
235 * @return status code
237 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
243 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
244 r = efi_allocate_pages(type, memory_type, pages, memory);
251 * This function implements the FreePages service.
252 * See the Unified Extensible Firmware Interface (UEFI) specification
255 * @memory start of the memory area to be freed
256 * @pages number of pages to be freed
257 * @return status code
259 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
264 EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
265 r = efi_free_pages(memory, pages);
270 * Get map describing memory usage.
272 * This function implements the GetMemoryMap service.
273 * See the Unified Extensible Firmware Interface (UEFI) specification
276 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
277 * on exit the size of the copied memory map
278 * @memory_map buffer to which the memory map is written
279 * @map_key key for the memory map
280 * @descriptor_size size of an individual memory descriptor
281 * @descriptor_version version number of the memory descriptor structure
282 * @return status code
284 static efi_status_t EFIAPI efi_get_memory_map_ext(
285 efi_uintn_t *memory_map_size,
286 struct efi_mem_desc *memory_map,
287 efi_uintn_t *map_key,
288 efi_uintn_t *descriptor_size,
289 uint32_t *descriptor_version)
293 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
294 map_key, descriptor_size, descriptor_version);
295 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
296 descriptor_size, descriptor_version);
301 * Allocate memory from pool.
303 * This function implements the AllocatePool service.
304 * See the Unified Extensible Firmware Interface (UEFI) specification
307 * @pool_type type of the pool from which memory is to be allocated
308 * @size number of bytes to be allocated
309 * @buffer allocated memory
310 * @return status code
312 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
318 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
319 r = efi_allocate_pool(pool_type, size, buffer);
324 * Free memory from pool.
326 * This function implements the FreePool service.
327 * See the Unified Extensible Firmware Interface (UEFI) specification
330 * @buffer start of memory to be freed
331 * @return status code
333 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
337 EFI_ENTRY("%p", buffer);
338 r = efi_free_pool(buffer);
343 * Add a new object to the object list.
345 * The protocols list is initialized.
346 * The object handle is set.
348 * @obj object to be added
350 void efi_add_handle(struct efi_object *obj)
354 INIT_LIST_HEAD(&obj->protocols);
356 list_add_tail(&obj->link, &efi_obj_list);
363 * @return status code
365 efi_status_t efi_create_handle(efi_handle_t *handle)
367 struct efi_object *obj;
370 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
371 sizeof(struct efi_object),
373 if (r != EFI_SUCCESS)
376 *handle = obj->handle;
381 * Find a protocol on a handle.
384 * @protocol_guid GUID of the protocol
385 * @handler reference to the protocol
386 * @return status code
388 efi_status_t efi_search_protocol(const efi_handle_t handle,
389 const efi_guid_t *protocol_guid,
390 struct efi_handler **handler)
392 struct efi_object *efiobj;
393 struct list_head *lhandle;
395 if (!handle || !protocol_guid)
396 return EFI_INVALID_PARAMETER;
397 efiobj = efi_search_obj(handle);
399 return EFI_INVALID_PARAMETER;
400 list_for_each(lhandle, &efiobj->protocols) {
401 struct efi_handler *protocol;
403 protocol = list_entry(lhandle, struct efi_handler, link);
404 if (!guidcmp(protocol->guid, protocol_guid)) {
410 return EFI_NOT_FOUND;
414 * Delete protocol from a handle.
416 * @handle handle from which the protocol shall be deleted
417 * @protocol GUID of the protocol to be deleted
418 * @protocol_interface interface of the protocol implementation
419 * @return status code
421 efi_status_t efi_remove_protocol(const efi_handle_t handle,
422 const efi_guid_t *protocol,
423 void *protocol_interface)
425 struct efi_handler *handler;
428 ret = efi_search_protocol(handle, protocol, &handler);
429 if (ret != EFI_SUCCESS)
431 if (guidcmp(handler->guid, protocol))
432 return EFI_INVALID_PARAMETER;
433 list_del(&handler->link);
439 * Delete all protocols from a handle.
441 * @handle handle from which the protocols shall be deleted
442 * @return status code
444 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
446 struct efi_object *efiobj;
447 struct efi_handler *protocol;
448 struct efi_handler *pos;
450 efiobj = efi_search_obj(handle);
452 return EFI_INVALID_PARAMETER;
453 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
456 ret = efi_remove_protocol(handle, protocol->guid,
457 protocol->protocol_interface);
458 if (ret != EFI_SUCCESS)
467 * @handle handle to delete
469 void efi_delete_handle(struct efi_object *obj)
473 efi_remove_all_protocols(obj->handle);
474 list_del(&obj->link);
479 * Check if a pointer is a valid event.
481 * @event pointer to check
482 * @return status code
484 static efi_status_t efi_is_event(const struct efi_event *event)
486 const struct efi_event *evt;
489 return EFI_INVALID_PARAMETER;
490 list_for_each_entry(evt, &efi_events, link) {
494 return EFI_INVALID_PARAMETER;
500 * This function is used inside U-Boot code to create an event.
502 * For the API function implementing the CreateEvent service see
503 * efi_create_event_ext.
505 * @type type of the event to create
506 * @notify_tpl task priority level of the event
507 * @notify_function notification function of the event
508 * @notify_context pointer passed to the notification function
509 * @event created event
510 * @return status code
512 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
513 void (EFIAPI *notify_function) (
514 struct efi_event *event,
516 void *notify_context, struct efi_event **event)
518 struct efi_event *evt;
521 return EFI_INVALID_PARAMETER;
523 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
524 return EFI_INVALID_PARAMETER;
526 if ((type & (EVT_NOTIFY_SIGNAL | EVT_NOTIFY_WAIT)) &&
527 notify_function == NULL)
528 return EFI_INVALID_PARAMETER;
530 evt = calloc(1, sizeof(struct efi_event));
532 return EFI_OUT_OF_RESOURCES;
534 evt->notify_tpl = notify_tpl;
535 evt->notify_function = notify_function;
536 evt->notify_context = notify_context;
537 /* Disable timers on bootup */
538 evt->trigger_next = -1ULL;
539 evt->is_queued = false;
540 evt->is_signaled = false;
541 list_add_tail(&evt->link, &efi_events);
547 * Create an event in a group.
549 * This function implements the CreateEventEx service.
550 * See the Unified Extensible Firmware Interface (UEFI) specification
552 * TODO: Support event groups
554 * @type type of the event to create
555 * @notify_tpl task priority level of the event
556 * @notify_function notification function of the event
557 * @notify_context pointer passed to the notification function
558 * @event created event
559 * @event_group event group
560 * @return status code
562 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
563 void (EFIAPI *notify_function) (
564 struct efi_event *event,
566 void *notify_context,
567 efi_guid_t *event_group,
568 struct efi_event **event)
570 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
571 notify_context, event_group);
573 return EFI_EXIT(EFI_UNSUPPORTED);
574 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
575 notify_context, event));
581 * This function implements the CreateEvent service.
582 * See the Unified Extensible Firmware Interface (UEFI) specification
585 * @type type of the event to create
586 * @notify_tpl task priority level of the event
587 * @notify_function notification function of the event
588 * @notify_context pointer passed to the notification function
589 * @event created event
590 * @return status code
592 static efi_status_t EFIAPI efi_create_event_ext(
593 uint32_t type, efi_uintn_t notify_tpl,
594 void (EFIAPI *notify_function) (
595 struct efi_event *event,
597 void *notify_context, struct efi_event **event)
599 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
601 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
602 notify_context, event));
606 * Check if a timer event has occurred or a queued notification function should
609 * Our timers have to work without interrupts, so we check whenever keyboard
610 * input or disk accesses happen if enough time elapsed for them to fire.
612 void efi_timer_check(void)
614 struct efi_event *evt;
615 u64 now = timer_get_us();
617 list_for_each_entry(evt, &efi_events, link) {
619 efi_signal_event(evt, true);
620 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
622 switch (evt->trigger_type) {
623 case EFI_TIMER_RELATIVE:
624 evt->trigger_type = EFI_TIMER_STOP;
626 case EFI_TIMER_PERIODIC:
627 evt->trigger_next += evt->trigger_time;
632 evt->is_signaled = true;
633 efi_signal_event(evt, true);
639 * Set the trigger time for a timer event or stop the event.
641 * This is the function for internal usage in U-Boot. For the API function
642 * implementing the SetTimer service see efi_set_timer_ext.
644 * @event event for which the timer is set
645 * @type type of the timer
646 * @trigger_time trigger period in multiples of 100ns
647 * @return status code
649 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
650 uint64_t trigger_time)
652 /* Check that the event is valid */
653 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
654 return EFI_INVALID_PARAMETER;
657 * The parameter defines a multiple of 100ns.
658 * We use multiples of 1000ns. So divide by 10.
660 do_div(trigger_time, 10);
664 event->trigger_next = -1ULL;
666 case EFI_TIMER_PERIODIC:
667 case EFI_TIMER_RELATIVE:
668 event->trigger_next = timer_get_us() + trigger_time;
671 return EFI_INVALID_PARAMETER;
673 event->trigger_type = type;
674 event->trigger_time = trigger_time;
675 event->is_signaled = false;
680 * Set the trigger time for a timer event or stop the event.
682 * This function implements the SetTimer service.
683 * See the Unified Extensible Firmware Interface (UEFI) specification
686 * @event event for which the timer is set
687 * @type type of the timer
688 * @trigger_time trigger period in multiples of 100ns
689 * @return status code
691 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
692 enum efi_timer_delay type,
693 uint64_t trigger_time)
695 EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
696 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
700 * Wait for events to be signaled.
702 * This function implements the WaitForEvent service.
703 * See the Unified Extensible Firmware Interface (UEFI) specification
706 * @num_events number of events to be waited for
707 * @events events to be waited for
708 * @index index of the event that was signaled
709 * @return status code
711 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
712 struct efi_event **event,
717 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
719 /* Check parameters */
720 if (!num_events || !event)
721 return EFI_EXIT(EFI_INVALID_PARAMETER);
723 if (efi_tpl != TPL_APPLICATION)
724 return EFI_EXIT(EFI_UNSUPPORTED);
725 for (i = 0; i < num_events; ++i) {
726 if (efi_is_event(event[i]) != EFI_SUCCESS)
727 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)
771 EFI_ENTRY("%p", event);
772 if (efi_is_event(event) != EFI_SUCCESS)
773 return EFI_EXIT(EFI_INVALID_PARAMETER);
774 if (!event->is_signaled) {
775 event->is_signaled = true;
776 if (event->type & EVT_NOTIFY_SIGNAL)
777 efi_signal_event(event, true);
779 return EFI_EXIT(EFI_SUCCESS);
783 * Close an EFI event.
785 * This function implements the CloseEvent service.
786 * See the Unified Extensible Firmware Interface (UEFI) specification
789 * @event event to close
790 * @return status code
792 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
794 EFI_ENTRY("%p", event);
795 if (efi_is_event(event) != EFI_SUCCESS)
796 return EFI_EXIT(EFI_INVALID_PARAMETER);
797 list_del(&event->link);
799 return EFI_EXIT(EFI_SUCCESS);
803 * Check if an event is signaled.
805 * This function implements the CheckEvent service.
806 * See the Unified Extensible Firmware Interface (UEFI) specification
809 * If an event is not signaled yet, the notification function is queued.
810 * The signaled state is cleared.
812 * @event event to check
813 * @return status code
815 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
817 EFI_ENTRY("%p", event);
819 if (efi_is_event(event) != EFI_SUCCESS ||
820 event->type & EVT_NOTIFY_SIGNAL)
821 return EFI_EXIT(EFI_INVALID_PARAMETER);
822 if (!event->is_signaled)
823 efi_signal_event(event, true);
824 if (event->is_signaled) {
825 event->is_signaled = false;
826 return EFI_EXIT(EFI_SUCCESS);
828 return EFI_EXIT(EFI_NOT_READY);
832 * Find the internal EFI object for a handle.
834 * @handle handle to find
837 struct efi_object *efi_search_obj(const efi_handle_t handle)
839 struct efi_object *efiobj;
841 list_for_each_entry(efiobj, &efi_obj_list, link) {
842 if (efiobj->handle == handle)
850 * Create open protocol info entry and add it to a protocol.
852 * @handler handler of a protocol
853 * @return open protocol info entry
855 static struct efi_open_protocol_info_entry *efi_create_open_info(
856 struct efi_handler *handler)
858 struct efi_open_protocol_info_item *item;
860 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
863 /* Append the item to the open protocol info list. */
864 list_add_tail(&item->link, &handler->open_infos);
870 * Remove an open protocol info entry from a protocol.
872 * @handler handler of a protocol
873 * @return status code
875 static efi_status_t efi_delete_open_info(
876 struct efi_open_protocol_info_item *item)
878 list_del(&item->link);
884 * Install new protocol on a handle.
886 * @handle handle on which the protocol shall be installed
887 * @protocol GUID of the protocol to be installed
888 * @protocol_interface interface of the protocol implementation
889 * @return status code
891 efi_status_t efi_add_protocol(const efi_handle_t handle,
892 const efi_guid_t *protocol,
893 void *protocol_interface)
895 struct efi_object *efiobj;
896 struct efi_handler *handler;
899 efiobj = efi_search_obj(handle);
901 return EFI_INVALID_PARAMETER;
902 ret = efi_search_protocol(handle, protocol, NULL);
903 if (ret != EFI_NOT_FOUND)
904 return EFI_INVALID_PARAMETER;
905 handler = calloc(1, sizeof(struct efi_handler));
907 return EFI_OUT_OF_RESOURCES;
908 handler->guid = protocol;
909 handler->protocol_interface = protocol_interface;
910 INIT_LIST_HEAD(&handler->open_infos);
911 list_add_tail(&handler->link, &efiobj->protocols);
912 if (!guidcmp(&efi_guid_device_path, protocol))
913 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
918 * Install protocol interface.
920 * This function implements the InstallProtocolInterface service.
921 * See the Unified Extensible Firmware Interface (UEFI) specification
924 * @handle handle on which the protocol shall be installed
925 * @protocol GUID of the protocol to be installed
926 * @protocol_interface_type type of the interface to be installed,
927 * always EFI_NATIVE_INTERFACE
928 * @protocol_interface interface of the protocol implementation
929 * @return status code
931 static efi_status_t EFIAPI efi_install_protocol_interface(
932 void **handle, const efi_guid_t *protocol,
933 int protocol_interface_type, void *protocol_interface)
937 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
940 if (!handle || !protocol ||
941 protocol_interface_type != EFI_NATIVE_INTERFACE) {
942 r = EFI_INVALID_PARAMETER;
946 /* Create new handle if requested. */
948 r = efi_create_handle(handle);
949 if (r != EFI_SUCCESS)
951 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
954 debug("%sEFI: handle %p\n", indent_string(nesting_level),
957 /* Add new protocol */
958 r = efi_add_protocol(*handle, protocol, protocol_interface);
964 * Reinstall protocol interface.
966 * This function implements the ReinstallProtocolInterface service.
967 * See the Unified Extensible Firmware Interface (UEFI) specification
970 * @handle handle on which the protocol shall be
972 * @protocol GUID of the protocol to be installed
973 * @old_interface interface to be removed
974 * @new_interface interface to be installed
975 * @return status code
977 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
978 efi_handle_t handle, const efi_guid_t *protocol,
979 void *old_interface, void *new_interface)
981 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
983 return EFI_EXIT(EFI_ACCESS_DENIED);
987 * Get all drivers associated to a controller.
988 * The allocated buffer has to be freed with free().
990 * @efiobj handle of the controller
991 * @protocol protocol guid (optional)
992 * @number_of_drivers number of child controllers
993 * @driver_handle_buffer handles of the the drivers
994 * @return status code
996 static efi_status_t efi_get_drivers(struct efi_object *efiobj,
997 const efi_guid_t *protocol,
998 efi_uintn_t *number_of_drivers,
999 efi_handle_t **driver_handle_buffer)
1001 struct efi_handler *handler;
1002 struct efi_open_protocol_info_item *item;
1003 efi_uintn_t count = 0, i;
1006 /* Count all driver associations */
1007 list_for_each_entry(handler, &efiobj->protocols, link) {
1008 if (protocol && guidcmp(handler->guid, protocol))
1010 list_for_each_entry(item, &handler->open_infos, link) {
1011 if (item->info.attributes &
1012 EFI_OPEN_PROTOCOL_BY_DRIVER)
1017 * Create buffer. In case of duplicate driver assignments the buffer
1018 * will be too large. But that does not harm.
1020 *number_of_drivers = 0;
1021 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1022 if (!*driver_handle_buffer)
1023 return EFI_OUT_OF_RESOURCES;
1024 /* Collect unique driver handles */
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) {
1031 /* Check this is a new driver */
1033 for (i = 0; i < *number_of_drivers; ++i) {
1034 if ((*driver_handle_buffer)[i] ==
1035 item->info.agent_handle)
1038 /* Copy handle to buffer */
1040 i = (*number_of_drivers)++;
1041 (*driver_handle_buffer)[i] =
1042 item->info.agent_handle;
1051 * Disconnect all drivers from a controller.
1053 * This function implements the DisconnectController service.
1054 * See the Unified Extensible Firmware Interface (UEFI) specification
1057 * @efiobj handle of the controller
1058 * @protocol protocol guid (optional)
1059 * @child_handle handle of the child to destroy
1060 * @return status code
1062 static efi_status_t efi_disconnect_all_drivers(
1063 struct efi_object *efiobj,
1064 const efi_guid_t *protocol,
1065 efi_handle_t child_handle)
1067 efi_uintn_t number_of_drivers;
1068 efi_handle_t *driver_handle_buffer;
1069 efi_status_t r, ret;
1071 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1072 &driver_handle_buffer);
1073 if (ret != EFI_SUCCESS)
1076 ret = EFI_NOT_FOUND;
1077 while (number_of_drivers) {
1078 r = EFI_CALL(efi_disconnect_controller(
1080 driver_handle_buffer[--number_of_drivers],
1082 if (r == EFI_SUCCESS)
1085 free(driver_handle_buffer);
1090 * Uninstall protocol interface.
1092 * This function implements the UninstallProtocolInterface service.
1093 * See the Unified Extensible Firmware Interface (UEFI) specification
1096 * @handle handle from which the protocol shall be removed
1097 * @protocol GUID of the protocol to be removed
1098 * @protocol_interface interface to be removed
1099 * @return status code
1101 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
1102 efi_handle_t handle, const efi_guid_t *protocol,
1103 void *protocol_interface)
1105 struct efi_object *efiobj;
1106 struct efi_handler *handler;
1107 struct efi_open_protocol_info_item *item;
1108 struct efi_open_protocol_info_item *pos;
1111 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1114 efiobj = efi_search_obj(handle);
1116 r = EFI_INVALID_PARAMETER;
1119 /* Find the protocol on the handle */
1120 r = efi_search_protocol(handle, protocol, &handler);
1121 if (r != EFI_SUCCESS)
1123 /* Disconnect controllers */
1124 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1125 if (!list_empty(&handler->open_infos)) {
1126 r = EFI_ACCESS_DENIED;
1129 /* Close protocol */
1130 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1131 if (item->info.attributes ==
1132 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1133 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1134 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1135 list_del(&item->link);
1137 if (!list_empty(&handler->open_infos)) {
1138 r = EFI_ACCESS_DENIED;
1141 r = efi_remove_protocol(handle, protocol, protocol_interface);
1147 * Register an event for notification when a protocol is installed.
1149 * This function implements the RegisterProtocolNotify service.
1150 * See the Unified Extensible Firmware Interface (UEFI) specification
1153 * @protocol GUID of the protocol whose installation shall be
1155 * @event event to be signaled upon installation of the protocol
1156 * @registration key for retrieving the registration information
1157 * @return status code
1159 static efi_status_t EFIAPI efi_register_protocol_notify(
1160 const efi_guid_t *protocol,
1161 struct efi_event *event,
1162 void **registration)
1164 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1165 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1169 * Determine if an EFI handle implements a protocol.
1171 * See the documentation of the LocateHandle service in the UEFI specification.
1173 * @search_type selection criterion
1174 * @protocol GUID of the protocol
1175 * @search_key registration key
1177 * @return 0 if the handle implements the protocol
1179 static int efi_search(enum efi_locate_search_type search_type,
1180 const efi_guid_t *protocol, void *search_key,
1181 struct efi_object *efiobj)
1185 switch (search_type) {
1188 case BY_REGISTER_NOTIFY:
1189 /* TODO: RegisterProtocolNotify is not implemented yet */
1192 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1193 return (ret != EFI_SUCCESS);
1195 /* Invalid search type */
1201 * Locate handles implementing a protocol.
1203 * This function is meant for U-Boot internal calls. For the API implementation
1204 * of the LocateHandle service see efi_locate_handle_ext.
1206 * @search_type selection criterion
1207 * @protocol GUID of the protocol
1208 * @search_key registration key
1209 * @buffer_size size of the buffer to receive the handles in bytes
1210 * @buffer buffer to receive the relevant handles
1211 * @return status code
1213 static efi_status_t efi_locate_handle(
1214 enum efi_locate_search_type search_type,
1215 const efi_guid_t *protocol, void *search_key,
1216 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1218 struct efi_object *efiobj;
1219 efi_uintn_t size = 0;
1221 /* Check parameters */
1222 switch (search_type) {
1225 case BY_REGISTER_NOTIFY:
1227 return EFI_INVALID_PARAMETER;
1228 /* RegisterProtocolNotify is not implemented yet */
1229 return EFI_UNSUPPORTED;
1232 return EFI_INVALID_PARAMETER;
1235 return EFI_INVALID_PARAMETER;
1239 * efi_locate_handle_buffer uses this function for
1240 * the calculation of the necessary buffer size.
1241 * So do not require a buffer for buffersize == 0.
1243 if (!buffer_size || (*buffer_size && !buffer))
1244 return EFI_INVALID_PARAMETER;
1246 /* Count how much space we need */
1247 list_for_each_entry(efiobj, &efi_obj_list, link) {
1248 if (!efi_search(search_type, protocol, search_key, efiobj))
1249 size += sizeof(void *);
1252 if (*buffer_size < size) {
1253 *buffer_size = size;
1254 return EFI_BUFFER_TOO_SMALL;
1257 *buffer_size = size;
1259 return EFI_NOT_FOUND;
1261 /* Then fill the array */
1262 list_for_each_entry(efiobj, &efi_obj_list, link) {
1263 if (!efi_search(search_type, protocol, search_key, efiobj))
1264 *buffer++ = efiobj->handle;
1271 * Locate handles implementing a protocol.
1273 * This function implements the LocateHandle service.
1274 * See the Unified Extensible Firmware Interface (UEFI) specification
1277 * @search_type selection criterion
1278 * @protocol GUID of the protocol
1279 * @search_key registration key
1280 * @buffer_size size of the buffer to receive the handles in bytes
1281 * @buffer buffer to receive the relevant handles
1282 * @return 0 if the handle implements the protocol
1284 static efi_status_t EFIAPI efi_locate_handle_ext(
1285 enum efi_locate_search_type search_type,
1286 const efi_guid_t *protocol, void *search_key,
1287 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1289 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1290 buffer_size, buffer);
1292 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1293 buffer_size, buffer));
1296 /* Collapses configuration table entries, removing index i */
1297 static void efi_remove_configuration_table(int i)
1299 struct efi_configuration_table *this = &efi_conf_table[i];
1300 struct efi_configuration_table *next = &efi_conf_table[i + 1];
1301 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1303 memmove(this, next, (ulong)end - (ulong)next);
1308 * Adds, updates, or removes a configuration table.
1310 * This function is used for internal calls. For the API implementation of the
1311 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1313 * @guid GUID of the installed table
1314 * @table table to be installed
1315 * @return status code
1317 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1323 return EFI_INVALID_PARAMETER;
1325 /* Check for guid override */
1326 for (i = 0; i < systab.nr_tables; i++) {
1327 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1329 efi_conf_table[i].table = table;
1331 efi_remove_configuration_table(i);
1337 return EFI_NOT_FOUND;
1339 /* No override, check for overflow */
1340 if (i >= ARRAY_SIZE(efi_conf_table))
1341 return EFI_OUT_OF_RESOURCES;
1343 /* Add a new entry */
1344 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1345 efi_conf_table[i].table = table;
1346 systab.nr_tables = i + 1;
1352 * Adds, updates, or removes a configuration table.
1354 * This function implements the InstallConfigurationTable service.
1355 * See the Unified Extensible Firmware Interface (UEFI) specification
1358 * @guid GUID of the installed table
1359 * @table table to be installed
1360 * @return status code
1362 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1365 EFI_ENTRY("%pUl, %p", guid, table);
1366 return EFI_EXIT(efi_install_configuration_table(guid, table));
1370 * Initialize a loaded_image_info + loaded_image_info object with correct
1371 * protocols, boot-device, etc.
1373 * @info loaded image info to be passed to the entry point of the
1375 * @obj internal object associated with the loaded image
1376 * @device_path device path of the loaded image
1377 * @file_path file path of the loaded image
1378 * @return status code
1380 efi_status_t efi_setup_loaded_image(
1381 struct efi_loaded_image *info, struct efi_object *obj,
1382 struct efi_device_path *device_path,
1383 struct efi_device_path *file_path)
1387 /* Add internal object to object list */
1388 efi_add_handle(obj);
1389 /* efi_exit() assumes that the handle points to the info */
1392 info->file_path = file_path;
1395 info->device_handle = efi_dp_find_obj(device_path, NULL);
1397 * When asking for the device path interface, return
1398 * bootefi_device_path
1400 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1402 if (ret != EFI_SUCCESS)
1407 * When asking for the loaded_image interface, just
1408 * return handle which points to loaded_image_info
1410 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1411 if (ret != EFI_SUCCESS)
1414 ret = efi_add_protocol(obj->handle,
1415 &efi_guid_device_path_to_text_protocol,
1416 (void *)&efi_device_path_to_text);
1417 if (ret != EFI_SUCCESS)
1420 ret = efi_add_protocol(obj->handle,
1421 &efi_guid_device_path_utilities_protocol,
1422 (void *)&efi_device_path_utilities);
1423 if (ret != EFI_SUCCESS)
1428 printf("ERROR: Failure to install protocols for loaded image\n");
1433 * Load an image using a file path.
1435 * @file_path the path of the image to load
1436 * @buffer buffer containing the loaded image
1437 * @return status code
1439 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1442 struct efi_file_info *info = NULL;
1443 struct efi_file_handle *f;
1444 static efi_status_t ret;
1447 f = efi_file_from_path(file_path);
1449 return EFI_DEVICE_ERROR;
1452 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1454 if (ret == EFI_BUFFER_TOO_SMALL) {
1456 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1459 if (ret != EFI_SUCCESS)
1462 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1466 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1470 EFI_CALL(f->close(f));
1472 if (ret != EFI_SUCCESS) {
1473 efi_free_pool(*buffer);
1481 * Load an EFI image into memory.
1483 * This function implements the LoadImage service.
1484 * See the Unified Extensible Firmware Interface (UEFI) specification
1487 * @boot_policy true for request originating from the boot manager
1488 * @parent_image the caller's image handle
1489 * @file_path the path of the image to load
1490 * @source_buffer memory location from which the image is installed
1491 * @source_size size of the memory area from which the image is
1493 * @image_handle handle for the newly installed image
1494 * @return status code
1496 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1497 efi_handle_t parent_image,
1498 struct efi_device_path *file_path,
1499 void *source_buffer,
1500 unsigned long source_size,
1501 efi_handle_t *image_handle)
1503 struct efi_loaded_image *info;
1504 struct efi_object *obj;
1507 EFI_ENTRY("%d, %p, %pD, %p, %ld, %p", boot_policy, parent_image,
1508 file_path, source_buffer, source_size, image_handle);
1510 if (!image_handle || !parent_image) {
1511 ret = EFI_INVALID_PARAMETER;
1515 if (!source_buffer && !file_path) {
1516 ret = EFI_NOT_FOUND;
1520 info = calloc(1, sizeof(*info));
1522 ret = EFI_OUT_OF_RESOURCES;
1525 obj = calloc(1, sizeof(*obj));
1528 ret = EFI_OUT_OF_RESOURCES;
1532 if (!source_buffer) {
1533 struct efi_device_path *dp, *fp;
1535 ret = efi_load_image_from_path(file_path, &source_buffer);
1536 if (ret != EFI_SUCCESS)
1539 * split file_path which contains both the device and
1542 efi_dp_split_file_path(file_path, &dp, &fp);
1543 ret = efi_setup_loaded_image(info, obj, dp, fp);
1544 if (ret != EFI_SUCCESS)
1547 /* In this case, file_path is the "device" path, ie.
1548 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1550 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1551 if (ret != EFI_SUCCESS)
1554 info->reserved = efi_load_pe(source_buffer, info);
1555 if (!info->reserved) {
1556 ret = EFI_UNSUPPORTED;
1559 info->system_table = &systab;
1560 info->parent_handle = parent_image;
1561 *image_handle = obj->handle;
1562 return EFI_EXIT(EFI_SUCCESS);
1565 efi_delete_handle(obj);
1567 return EFI_EXIT(ret);
1571 * Call the entry point of an image.
1573 * This function implements the StartImage service.
1574 * See the Unified Extensible Firmware Interface (UEFI) specification
1577 * @image_handle handle of the image
1578 * @exit_data_size size of the buffer
1579 * @exit_data buffer to receive the exit data of the called image
1580 * @return status code
1582 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1583 unsigned long *exit_data_size,
1586 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1587 struct efi_system_table *st);
1588 struct efi_loaded_image *info = image_handle;
1591 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1592 entry = info->reserved;
1594 efi_is_direct_boot = false;
1596 /* call the image! */
1597 if (setjmp(&info->exit_jmp)) {
1599 * We called the entry point of the child image with EFI_CALL
1600 * in the lines below. The child image called the Exit() boot
1601 * service efi_exit() which executed the long jump that brought
1602 * us to the current line. This implies that the second half
1603 * of the EFI_CALL macro has not been executed.
1607 * efi_exit() called efi_restore_gd(). We have to undo this
1608 * otherwise __efi_entry_check() will put the wrong value into
1614 * To get ready to call EFI_EXIT below we have to execute the
1615 * missed out steps of EFI_CALL.
1617 assert(__efi_entry_check());
1618 debug("%sEFI: %lu returned by started image\n",
1619 __efi_nesting_dec(),
1620 (unsigned long)((uintptr_t)info->exit_status &
1622 return EFI_EXIT(info->exit_status);
1625 ret = EFI_CALL(entry(image_handle, &systab));
1628 * Usually UEFI applications call Exit() instead of returning.
1629 * But because the world doesn not consist of ponies and unicorns,
1630 * we're happy to emulate that behavior on behalf of a payload
1633 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
1637 * Leave an EFI application or driver.
1639 * This function implements the Exit service.
1640 * See the Unified Extensible Firmware Interface (UEFI) specification
1643 * @image_handle handle of the application or driver that is exiting
1644 * @exit_status status code
1645 * @exit_data_size size of the buffer in bytes
1646 * @exit_data buffer with data describing an error
1647 * @return status code
1649 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1650 efi_status_t exit_status,
1651 unsigned long exit_data_size,
1655 * We require that the handle points to the original loaded
1656 * image protocol interface.
1658 * For getting the longjmp address this is safer than locating
1659 * the protocol because the protocol may have been reinstalled
1660 * pointing to another memory location.
1662 * TODO: We should call the unload procedure of the loaded
1665 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
1667 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1668 exit_data_size, exit_data);
1670 /* Make sure entry/exit counts for EFI world cross-overs match */
1671 EFI_EXIT(exit_status);
1674 * But longjmp out with the U-Boot gd, not the application's, as
1675 * the other end is a setjmp call inside EFI context.
1679 loaded_image_info->exit_status = exit_status;
1680 longjmp(&loaded_image_info->exit_jmp, 1);
1682 panic("EFI application exited");
1686 * Unload an EFI image.
1688 * This function implements the UnloadImage service.
1689 * See the Unified Extensible Firmware Interface (UEFI) specification
1692 * @image_handle handle of the image to be unloaded
1693 * @return status code
1695 static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1697 struct efi_object *efiobj;
1699 EFI_ENTRY("%p", image_handle);
1700 efiobj = efi_search_obj(image_handle);
1702 list_del(&efiobj->link);
1704 return EFI_EXIT(EFI_SUCCESS);
1708 * Fix up caches for EFI payloads if necessary.
1710 static void efi_exit_caches(void)
1712 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1714 * Grub on 32bit ARM needs to have caches disabled before jumping into
1715 * a zImage, but does not know of all cache layers. Give it a hand.
1717 if (efi_is_direct_boot)
1718 cleanup_before_linux();
1723 * Stop all boot services.
1725 * This function implements the ExitBootServices service.
1726 * See the Unified Extensible Firmware Interface (UEFI) specification
1729 * All timer events are disabled.
1730 * For exit boot services events the notification function is called.
1731 * The boot services are disabled in the system table.
1733 * @image_handle handle of the loaded image
1734 * @map_key key of the memory map
1735 * @return status code
1737 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1738 unsigned long map_key)
1740 struct efi_event *evt;
1742 EFI_ENTRY("%p, %ld", image_handle, map_key);
1744 /* Make sure that notification functions are not called anymore */
1745 efi_tpl = TPL_HIGH_LEVEL;
1747 /* Check if ExitBootServices has already been called */
1748 if (!systab.boottime)
1749 return EFI_EXIT(EFI_SUCCESS);
1751 /* Notify that ExitBootServices is invoked. */
1752 list_for_each_entry(evt, &efi_events, link) {
1753 if (evt->type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1755 evt->is_signaled = true;
1756 efi_signal_event(evt, false);
1759 /* TODO Should persist EFI variables here */
1761 board_quiesce_devices();
1763 /* Fix up caches for EFI payloads if necessary */
1766 /* This stops all lingering devices */
1767 bootm_disable_interrupts();
1769 /* Disable boottime services */
1770 systab.con_in_handle = NULL;
1771 systab.con_in = NULL;
1772 systab.con_out_handle = NULL;
1773 systab.con_out = NULL;
1774 systab.stderr_handle = NULL;
1775 systab.std_err = NULL;
1776 systab.boottime = NULL;
1778 /* Recalculate CRC32 */
1779 systab.hdr.crc32 = 0;
1780 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1781 sizeof(struct efi_system_table));
1783 /* Give the payload some time to boot */
1784 efi_set_watchdog(0);
1787 return EFI_EXIT(EFI_SUCCESS);
1791 * Get next value of the counter.
1793 * This function implements the NextMonotonicCount service.
1794 * See the Unified Extensible Firmware Interface (UEFI) specification
1797 * @count returned value of the counter
1798 * @return status code
1800 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1802 static uint64_t mono;
1804 EFI_ENTRY("%p", count);
1806 return EFI_EXIT(EFI_SUCCESS);
1812 * This function implements the Stall sercive.
1813 * See the Unified Extensible Firmware Interface (UEFI) specification
1816 * @microseconds period to sleep in microseconds
1817 * @return status code
1819 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1821 EFI_ENTRY("%ld", microseconds);
1822 udelay(microseconds);
1823 return EFI_EXIT(EFI_SUCCESS);
1827 * Reset the watchdog timer.
1829 * This function implements the SetWatchdogTimer service.
1830 * See the Unified Extensible Firmware Interface (UEFI) specification
1833 * @timeout seconds before reset by watchdog
1834 * @watchdog_code code to be logged when resetting
1835 * @data_size size of buffer in bytes
1836 * @watchdog_data buffer with data describing the reset reason
1837 * @return status code
1839 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1840 uint64_t watchdog_code,
1841 unsigned long data_size,
1842 uint16_t *watchdog_data)
1844 EFI_ENTRY("%ld, 0x%" PRIx64 ", %ld, %p", timeout, watchdog_code,
1845 data_size, watchdog_data);
1846 return EFI_EXIT(efi_set_watchdog(timeout));
1852 * This function implements the CloseProtocol service.
1853 * See the Unified Extensible Firmware Interface (UEFI) specification
1856 * @handle handle on which the protocol shall be closed
1857 * @protocol GUID of the protocol to close
1858 * @agent_handle handle of the driver
1859 * @controller_handle handle of the controller
1860 * @return status code
1862 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1863 const efi_guid_t *protocol,
1864 efi_handle_t agent_handle,
1865 efi_handle_t controller_handle)
1867 struct efi_handler *handler;
1868 struct efi_open_protocol_info_item *item;
1869 struct efi_open_protocol_info_item *pos;
1872 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1875 if (!agent_handle) {
1876 r = EFI_INVALID_PARAMETER;
1879 r = efi_search_protocol(handle, protocol, &handler);
1880 if (r != EFI_SUCCESS)
1884 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1885 if (item->info.agent_handle == agent_handle &&
1886 item->info.controller_handle == controller_handle) {
1887 efi_delete_open_info(item);
1897 * Provide information about then open status of a protocol on a handle
1899 * This function implements the OpenProtocolInformation service.
1900 * See the Unified Extensible Firmware Interface (UEFI) specification
1903 * @handle handle for which the information shall be retrieved
1904 * @protocol GUID of the protocol
1905 * @entry_buffer buffer to receive the open protocol information
1906 * @entry_count number of entries available in the buffer
1907 * @return status code
1909 static efi_status_t EFIAPI efi_open_protocol_information(
1910 efi_handle_t handle, const efi_guid_t *protocol,
1911 struct efi_open_protocol_info_entry **entry_buffer,
1912 efi_uintn_t *entry_count)
1914 unsigned long buffer_size;
1915 unsigned long count;
1916 struct efi_handler *handler;
1917 struct efi_open_protocol_info_item *item;
1920 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1923 /* Check parameters */
1924 if (!entry_buffer) {
1925 r = EFI_INVALID_PARAMETER;
1928 r = efi_search_protocol(handle, protocol, &handler);
1929 if (r != EFI_SUCCESS)
1934 list_for_each_entry(item, &handler->open_infos, link) {
1935 if (item->info.open_count)
1938 *entry_count = count;
1939 *entry_buffer = NULL;
1946 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
1947 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1948 (void **)entry_buffer);
1949 if (r != EFI_SUCCESS)
1951 list_for_each_entry_reverse(item, &handler->open_infos, link) {
1952 if (item->info.open_count)
1953 (*entry_buffer)[--count] = item->info;
1960 * Get protocols installed on a handle.
1962 * This function implements the ProtocolsPerHandleService.
1963 * See the Unified Extensible Firmware Interface (UEFI) specification
1966 * @handle handle for which the information is retrieved
1967 * @protocol_buffer buffer with protocol GUIDs
1968 * @protocol_buffer_count number of entries in the buffer
1969 * @return status code
1971 static efi_status_t EFIAPI efi_protocols_per_handle(
1972 efi_handle_t handle, efi_guid_t ***protocol_buffer,
1973 efi_uintn_t *protocol_buffer_count)
1975 unsigned long buffer_size;
1976 struct efi_object *efiobj;
1977 struct list_head *protocol_handle;
1980 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1981 protocol_buffer_count);
1983 if (!handle || !protocol_buffer || !protocol_buffer_count)
1984 return EFI_EXIT(EFI_INVALID_PARAMETER);
1986 *protocol_buffer = NULL;
1987 *protocol_buffer_count = 0;
1989 efiobj = efi_search_obj(handle);
1991 return EFI_EXIT(EFI_INVALID_PARAMETER);
1993 /* Count protocols */
1994 list_for_each(protocol_handle, &efiobj->protocols) {
1995 ++*protocol_buffer_count;
1999 if (*protocol_buffer_count) {
2002 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2003 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2004 (void **)protocol_buffer);
2005 if (r != EFI_SUCCESS)
2007 list_for_each(protocol_handle, &efiobj->protocols) {
2008 struct efi_handler *protocol;
2010 protocol = list_entry(protocol_handle,
2011 struct efi_handler, link);
2012 (*protocol_buffer)[j] = (void *)protocol->guid;
2017 return EFI_EXIT(EFI_SUCCESS);
2021 * Locate handles implementing a protocol.
2023 * This function implements the LocateHandleBuffer service.
2024 * See the Unified Extensible Firmware Interface (UEFI) specification
2027 * @search_type selection criterion
2028 * @protocol GUID of the protocol
2029 * @search_key registration key
2030 * @no_handles number of returned handles
2031 * @buffer buffer with the returned handles
2032 * @return status code
2034 static efi_status_t EFIAPI efi_locate_handle_buffer(
2035 enum efi_locate_search_type search_type,
2036 const efi_guid_t *protocol, void *search_key,
2037 efi_uintn_t *no_handles, efi_handle_t **buffer)
2040 efi_uintn_t buffer_size = 0;
2042 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2043 no_handles, buffer);
2045 if (!no_handles || !buffer) {
2046 r = EFI_INVALID_PARAMETER;
2051 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2053 if (r != EFI_BUFFER_TOO_SMALL)
2055 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2057 if (r != EFI_SUCCESS)
2059 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2061 if (r == EFI_SUCCESS)
2062 *no_handles = buffer_size / sizeof(efi_handle_t);
2068 * Find an interface implementing a protocol.
2070 * This function implements the LocateProtocol service.
2071 * See the Unified Extensible Firmware Interface (UEFI) specification
2074 * @protocol GUID of the protocol
2075 * @registration registration key passed to the notification function
2076 * @protocol_interface interface implementing the protocol
2077 * @return status code
2079 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2081 void **protocol_interface)
2083 struct list_head *lhandle;
2086 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2088 if (!protocol || !protocol_interface)
2089 return EFI_EXIT(EFI_INVALID_PARAMETER);
2091 list_for_each(lhandle, &efi_obj_list) {
2092 struct efi_object *efiobj;
2093 struct efi_handler *handler;
2095 efiobj = list_entry(lhandle, struct efi_object, link);
2097 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2098 if (ret == EFI_SUCCESS) {
2099 *protocol_interface = handler->protocol_interface;
2100 return EFI_EXIT(EFI_SUCCESS);
2103 *protocol_interface = NULL;
2105 return EFI_EXIT(EFI_NOT_FOUND);
2109 * Get the device path and handle of an device implementing a protocol.
2111 * This function implements the LocateDevicePath service.
2112 * See the Unified Extensible Firmware Interface (UEFI) specification
2115 * @protocol GUID of the protocol
2116 * @device_path device path
2117 * @device handle of the device
2118 * @return status code
2120 static efi_status_t EFIAPI efi_locate_device_path(
2121 const efi_guid_t *protocol,
2122 struct efi_device_path **device_path,
2123 efi_handle_t *device)
2125 struct efi_device_path *dp;
2127 struct efi_handler *handler;
2128 efi_handle_t *handles;
2130 size_t len_best = 0;
2131 efi_uintn_t no_handles;
2135 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2137 if (!protocol || !device_path || !*device_path || !device) {
2138 ret = EFI_INVALID_PARAMETER;
2142 /* Find end of device path */
2143 len = efi_dp_size(*device_path);
2145 /* Get all handles implementing the protocol */
2146 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2147 &no_handles, &handles));
2148 if (ret != EFI_SUCCESS)
2151 for (i = 0; i < no_handles; ++i) {
2152 /* Find the device path protocol */
2153 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2155 if (ret != EFI_SUCCESS)
2157 dp = (struct efi_device_path *)handler->protocol_interface;
2158 len_dp = efi_dp_size(dp);
2160 * This handle can only be a better fit
2161 * if its device path length is longer than the best fit and
2162 * if its device path length is shorter of equal the searched
2165 if (len_dp <= len_best || len_dp > len)
2167 /* Check if dp is a subpath of device_path */
2168 if (memcmp(*device_path, dp, len_dp))
2170 *device = handles[i];
2174 remainder = (u8 *)*device_path + len_best;
2175 *device_path = (struct efi_device_path *)remainder;
2178 ret = EFI_NOT_FOUND;
2181 return EFI_EXIT(ret);
2185 * Install multiple protocol interfaces.
2187 * This function implements the MultipleProtocolInterfaces service.
2188 * See the Unified Extensible Firmware Interface (UEFI) specification
2191 * @handle handle on which the protocol interfaces shall be installed
2192 * @... NULL terminated argument list with pairs of protocol GUIDS and
2194 * @return status code
2196 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2199 EFI_ENTRY("%p", handle);
2202 const efi_guid_t *protocol;
2203 void *protocol_interface;
2204 efi_status_t r = EFI_SUCCESS;
2208 return EFI_EXIT(EFI_INVALID_PARAMETER);
2210 va_start(argptr, handle);
2212 protocol = va_arg(argptr, efi_guid_t*);
2215 protocol_interface = va_arg(argptr, void*);
2216 r = EFI_CALL(efi_install_protocol_interface(
2218 EFI_NATIVE_INTERFACE,
2219 protocol_interface));
2220 if (r != EFI_SUCCESS)
2225 if (r == EFI_SUCCESS)
2228 /* If an error occurred undo all changes. */
2229 va_start(argptr, handle);
2231 protocol = va_arg(argptr, efi_guid_t*);
2232 protocol_interface = va_arg(argptr, void*);
2233 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2234 protocol_interface));
2242 * Uninstall multiple protocol interfaces.
2244 * This function implements the UninstallMultipleProtocolInterfaces service.
2245 * See the Unified Extensible Firmware Interface (UEFI) specification
2248 * @handle handle from which the protocol interfaces shall be removed
2249 * @... NULL terminated argument list with pairs of protocol GUIDS and
2251 * @return status code
2253 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2256 EFI_ENTRY("%p", handle);
2259 const efi_guid_t *protocol;
2260 void *protocol_interface;
2261 efi_status_t r = EFI_SUCCESS;
2265 return EFI_EXIT(EFI_INVALID_PARAMETER);
2267 va_start(argptr, handle);
2269 protocol = va_arg(argptr, efi_guid_t*);
2272 protocol_interface = va_arg(argptr, void*);
2273 r = EFI_CALL(efi_uninstall_protocol_interface(
2275 protocol_interface));
2276 if (r != EFI_SUCCESS)
2281 if (r == EFI_SUCCESS)
2284 /* If an error occurred undo all changes. */
2285 va_start(argptr, handle);
2287 protocol = va_arg(argptr, efi_guid_t*);
2288 protocol_interface = va_arg(argptr, void*);
2289 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2290 EFI_NATIVE_INTERFACE,
2291 protocol_interface));
2299 * Calculate cyclic redundancy code.
2301 * This function implements the CalculateCrc32 service.
2302 * See the Unified Extensible Firmware Interface (UEFI) specification
2305 * @data buffer with data
2306 * @data_size size of buffer in bytes
2307 * @crc32_p cyclic redundancy code
2308 * @return status code
2310 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2311 unsigned long data_size,
2314 EFI_ENTRY("%p, %ld", data, data_size);
2315 *crc32_p = crc32(0, data, data_size);
2316 return EFI_EXIT(EFI_SUCCESS);
2322 * This function implements the CopyMem service.
2323 * See the Unified Extensible Firmware Interface (UEFI) specification
2326 * @destination destination of the copy operation
2327 * @source source of the copy operation
2328 * @length number of bytes to copy
2330 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2333 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2334 memcpy(destination, source, length);
2335 EFI_EXIT(EFI_SUCCESS);
2339 * Fill memory with a byte value.
2341 * This function implements the SetMem service.
2342 * See the Unified Extensible Firmware Interface (UEFI) specification
2345 * @buffer buffer to fill
2346 * @size size of buffer in bytes
2347 * @value byte to copy to the buffer
2349 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2351 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2352 memset(buffer, value, size);
2353 EFI_EXIT(EFI_SUCCESS);
2357 * Open protocol interface on a handle.
2359 * @handler handler of a protocol
2360 * @protocol_interface interface implementing the protocol
2361 * @agent_handle handle of the driver
2362 * @controller_handle handle of the controller
2363 * @attributes attributes indicating how to open the protocol
2364 * @return status code
2366 static efi_status_t efi_protocol_open(
2367 struct efi_handler *handler,
2368 void **protocol_interface, void *agent_handle,
2369 void *controller_handle, uint32_t attributes)
2371 struct efi_open_protocol_info_item *item;
2372 struct efi_open_protocol_info_entry *match = NULL;
2373 bool opened_by_driver = false;
2374 bool opened_exclusive = false;
2376 /* If there is no agent, only return the interface */
2380 /* For TEST_PROTOCOL ignore interface attribute */
2381 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2382 *protocol_interface = NULL;
2385 * Check if the protocol is already opened by a driver with the same
2386 * attributes or opened exclusively
2388 list_for_each_entry(item, &handler->open_infos, link) {
2389 if (item->info.agent_handle == agent_handle) {
2390 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2391 (item->info.attributes == attributes))
2392 return EFI_ALREADY_STARTED;
2394 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2395 opened_exclusive = true;
2398 /* Only one controller can open the protocol exclusively */
2399 if (opened_exclusive && attributes &
2400 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2401 return EFI_ACCESS_DENIED;
2403 /* Prepare exclusive opening */
2404 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2405 /* Try to disconnect controllers */
2406 list_for_each_entry(item, &handler->open_infos, link) {
2407 if (item->info.attributes ==
2408 EFI_OPEN_PROTOCOL_BY_DRIVER)
2409 EFI_CALL(efi_disconnect_controller(
2410 item->info.controller_handle,
2411 item->info.agent_handle,
2414 opened_by_driver = false;
2415 /* Check if all controllers are disconnected */
2416 list_for_each_entry(item, &handler->open_infos, link) {
2417 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2418 opened_by_driver = true;
2420 /* Only one controller can be conncected */
2421 if (opened_by_driver)
2422 return EFI_ACCESS_DENIED;
2425 /* Find existing entry */
2426 list_for_each_entry(item, &handler->open_infos, link) {
2427 if (item->info.agent_handle == agent_handle &&
2428 item->info.controller_handle == controller_handle)
2429 match = &item->info;
2431 /* None found, create one */
2433 match = efi_create_open_info(handler);
2435 return EFI_OUT_OF_RESOURCES;
2438 match->agent_handle = agent_handle;
2439 match->controller_handle = controller_handle;
2440 match->attributes = attributes;
2441 match->open_count++;
2444 /* For TEST_PROTOCOL ignore interface attribute. */
2445 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2446 *protocol_interface = handler->protocol_interface;
2452 * Open protocol interface on a handle.
2454 * This function implements the OpenProtocol interface.
2455 * See the Unified Extensible Firmware Interface (UEFI) specification
2458 * @handle handle on which the protocol shall be opened
2459 * @protocol GUID of the protocol
2460 * @protocol_interface interface implementing the protocol
2461 * @agent_handle handle of the driver
2462 * @controller_handle handle of the controller
2463 * @attributes attributes indicating how to open the protocol
2464 * @return status code
2466 static efi_status_t EFIAPI efi_open_protocol(
2467 void *handle, const efi_guid_t *protocol,
2468 void **protocol_interface, void *agent_handle,
2469 void *controller_handle, uint32_t attributes)
2471 struct efi_handler *handler;
2472 efi_status_t r = EFI_INVALID_PARAMETER;
2474 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2475 protocol_interface, agent_handle, controller_handle,
2478 if (!handle || !protocol ||
2479 (!protocol_interface && attributes !=
2480 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2484 switch (attributes) {
2485 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2486 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2487 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2489 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2490 if (controller_handle == handle)
2493 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2494 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2495 /* Check that the controller handle is valid */
2496 if (!efi_search_obj(controller_handle))
2499 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2500 /* Check that the agent handle is valid */
2501 if (!efi_search_obj(agent_handle))
2508 r = efi_search_protocol(handle, protocol, &handler);
2509 if (r != EFI_SUCCESS)
2512 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2513 controller_handle, attributes);
2519 * Get interface of a protocol on a handle.
2521 * This function implements the HandleProtocol service.
2522 * See the Unified Extensible Firmware Interface (UEFI) specification
2525 * @handle handle on which the protocol shall be opened
2526 * @protocol GUID of the protocol
2527 * @protocol_interface interface implementing the protocol
2528 * @return status code
2530 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2531 const efi_guid_t *protocol,
2532 void **protocol_interface)
2534 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2535 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2538 static efi_status_t efi_bind_controller(
2539 efi_handle_t controller_handle,
2540 efi_handle_t driver_image_handle,
2541 struct efi_device_path *remain_device_path)
2543 struct efi_driver_binding_protocol *binding_protocol;
2546 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2547 &efi_guid_driver_binding_protocol,
2548 (void **)&binding_protocol,
2549 driver_image_handle, NULL,
2550 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2551 if (r != EFI_SUCCESS)
2553 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2555 remain_device_path));
2556 if (r == EFI_SUCCESS)
2557 r = EFI_CALL(binding_protocol->start(binding_protocol,
2559 remain_device_path));
2560 EFI_CALL(efi_close_protocol(driver_image_handle,
2561 &efi_guid_driver_binding_protocol,
2562 driver_image_handle, NULL));
2566 static efi_status_t efi_connect_single_controller(
2567 efi_handle_t controller_handle,
2568 efi_handle_t *driver_image_handle,
2569 struct efi_device_path *remain_device_path)
2571 efi_handle_t *buffer;
2575 size_t connected = 0;
2577 /* Get buffer with all handles with driver binding protocol */
2578 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2579 &efi_guid_driver_binding_protocol,
2580 NULL, &count, &buffer));
2581 if (r != EFI_SUCCESS)
2584 /* Context Override */
2585 if (driver_image_handle) {
2586 for (; *driver_image_handle; ++driver_image_handle) {
2587 for (i = 0; i < count; ++i) {
2588 if (buffer[i] == *driver_image_handle) {
2590 r = efi_bind_controller(
2592 *driver_image_handle,
2593 remain_device_path);
2595 * For drivers that do not support the
2596 * controller or are already connected
2597 * we receive an error code here.
2599 if (r == EFI_SUCCESS)
2607 * TODO: Some overrides are not yet implemented:
2608 * - Platform Driver Override
2609 * - Driver Family Override Search
2610 * - Bus Specific Driver Override
2613 /* Driver Binding Search */
2614 for (i = 0; i < count; ++i) {
2616 r = efi_bind_controller(controller_handle,
2618 remain_device_path);
2619 if (r == EFI_SUCCESS)
2624 efi_free_pool(buffer);
2626 return EFI_NOT_FOUND;
2631 * Connect a controller to a driver.
2633 * This function implements the ConnectController service.
2634 * See the Unified Extensible Firmware Interface (UEFI) specification
2637 * First all driver binding protocol handles are tried for binding drivers.
2638 * Afterwards all handles that have openened a protocol of the controller
2639 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2641 * @controller_handle handle of the controller
2642 * @driver_image_handle handle of the driver
2643 * @remain_device_path device path of a child controller
2644 * @recursive true to connect all child controllers
2645 * @return status code
2647 static efi_status_t EFIAPI efi_connect_controller(
2648 efi_handle_t controller_handle,
2649 efi_handle_t *driver_image_handle,
2650 struct efi_device_path *remain_device_path,
2654 efi_status_t ret = EFI_NOT_FOUND;
2655 struct efi_object *efiobj;
2657 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2658 remain_device_path, recursive);
2660 efiobj = efi_search_obj(controller_handle);
2662 ret = EFI_INVALID_PARAMETER;
2666 r = efi_connect_single_controller(controller_handle,
2667 driver_image_handle,
2668 remain_device_path);
2669 if (r == EFI_SUCCESS)
2672 struct efi_handler *handler;
2673 struct efi_open_protocol_info_item *item;
2675 list_for_each_entry(handler, &efiobj->protocols, link) {
2676 list_for_each_entry(item, &handler->open_infos, link) {
2677 if (item->info.attributes &
2678 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2679 r = EFI_CALL(efi_connect_controller(
2680 item->info.controller_handle,
2681 driver_image_handle,
2684 if (r == EFI_SUCCESS)
2690 /* Check for child controller specified by end node */
2691 if (ret != EFI_SUCCESS && remain_device_path &&
2692 remain_device_path->type == DEVICE_PATH_TYPE_END)
2695 return EFI_EXIT(ret);
2699 * Get all child controllers associated to a driver.
2700 * The allocated buffer has to be freed with free().
2702 * @efiobj handle of the controller
2703 * @driver_handle handle of the driver
2704 * @number_of_children number of child controllers
2705 * @child_handle_buffer handles of the the child controllers
2707 static efi_status_t efi_get_child_controllers(
2708 struct efi_object *efiobj,
2709 efi_handle_t driver_handle,
2710 efi_uintn_t *number_of_children,
2711 efi_handle_t **child_handle_buffer)
2713 struct efi_handler *handler;
2714 struct efi_open_protocol_info_item *item;
2715 efi_uintn_t count = 0, i;
2718 /* Count all child controller associations */
2719 list_for_each_entry(handler, &efiobj->protocols, link) {
2720 list_for_each_entry(item, &handler->open_infos, link) {
2721 if (item->info.agent_handle == driver_handle &&
2722 item->info.attributes &
2723 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2728 * Create buffer. In case of duplicate child controller assignments
2729 * the buffer will be too large. But that does not harm.
2731 *number_of_children = 0;
2732 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2733 if (!*child_handle_buffer)
2734 return EFI_OUT_OF_RESOURCES;
2735 /* Copy unique child handles */
2736 list_for_each_entry(handler, &efiobj->protocols, link) {
2737 list_for_each_entry(item, &handler->open_infos, link) {
2738 if (item->info.agent_handle == driver_handle &&
2739 item->info.attributes &
2740 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2741 /* Check this is a new child controller */
2743 for (i = 0; i < *number_of_children; ++i) {
2744 if ((*child_handle_buffer)[i] ==
2745 item->info.controller_handle)
2748 /* Copy handle to buffer */
2750 i = (*number_of_children)++;
2751 (*child_handle_buffer)[i] =
2752 item->info.controller_handle;
2761 * Disconnect a controller from a driver.
2763 * This function implements the DisconnectController service.
2764 * See the Unified Extensible Firmware Interface (UEFI) specification
2767 * @controller_handle handle of the controller
2768 * @driver_image_handle handle of the driver
2769 * @child_handle handle of the child to destroy
2770 * @return status code
2772 static efi_status_t EFIAPI efi_disconnect_controller(
2773 efi_handle_t controller_handle,
2774 efi_handle_t driver_image_handle,
2775 efi_handle_t child_handle)
2777 struct efi_driver_binding_protocol *binding_protocol;
2778 efi_handle_t *child_handle_buffer = NULL;
2779 size_t number_of_children = 0;
2781 size_t stop_count = 0;
2782 struct efi_object *efiobj;
2784 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2787 efiobj = efi_search_obj(controller_handle);
2789 r = EFI_INVALID_PARAMETER;
2793 if (child_handle && !efi_search_obj(child_handle)) {
2794 r = EFI_INVALID_PARAMETER;
2798 /* If no driver handle is supplied, disconnect all drivers */
2799 if (!driver_image_handle) {
2800 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2804 /* Create list of child handles */
2806 number_of_children = 1;
2807 child_handle_buffer = &child_handle;
2809 efi_get_child_controllers(efiobj,
2810 driver_image_handle,
2811 &number_of_children,
2812 &child_handle_buffer);
2815 /* Get the driver binding protocol */
2816 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2817 &efi_guid_driver_binding_protocol,
2818 (void **)&binding_protocol,
2819 driver_image_handle, NULL,
2820 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2821 if (r != EFI_SUCCESS)
2823 /* Remove the children */
2824 if (number_of_children) {
2825 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2828 child_handle_buffer));
2829 if (r == EFI_SUCCESS)
2832 /* Remove the driver */
2834 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2837 if (r == EFI_SUCCESS)
2839 EFI_CALL(efi_close_protocol(driver_image_handle,
2840 &efi_guid_driver_binding_protocol,
2841 driver_image_handle, NULL));
2849 free(child_handle_buffer);
2853 static const struct efi_boot_services efi_boot_services = {
2855 .headersize = sizeof(struct efi_table_hdr),
2857 .raise_tpl = efi_raise_tpl,
2858 .restore_tpl = efi_restore_tpl,
2859 .allocate_pages = efi_allocate_pages_ext,
2860 .free_pages = efi_free_pages_ext,
2861 .get_memory_map = efi_get_memory_map_ext,
2862 .allocate_pool = efi_allocate_pool_ext,
2863 .free_pool = efi_free_pool_ext,
2864 .create_event = efi_create_event_ext,
2865 .set_timer = efi_set_timer_ext,
2866 .wait_for_event = efi_wait_for_event,
2867 .signal_event = efi_signal_event_ext,
2868 .close_event = efi_close_event,
2869 .check_event = efi_check_event,
2870 .install_protocol_interface = efi_install_protocol_interface,
2871 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2872 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2873 .handle_protocol = efi_handle_protocol,
2875 .register_protocol_notify = efi_register_protocol_notify,
2876 .locate_handle = efi_locate_handle_ext,
2877 .locate_device_path = efi_locate_device_path,
2878 .install_configuration_table = efi_install_configuration_table_ext,
2879 .load_image = efi_load_image,
2880 .start_image = efi_start_image,
2882 .unload_image = efi_unload_image,
2883 .exit_boot_services = efi_exit_boot_services,
2884 .get_next_monotonic_count = efi_get_next_monotonic_count,
2886 .set_watchdog_timer = efi_set_watchdog_timer,
2887 .connect_controller = efi_connect_controller,
2888 .disconnect_controller = efi_disconnect_controller,
2889 .open_protocol = efi_open_protocol,
2890 .close_protocol = efi_close_protocol,
2891 .open_protocol_information = efi_open_protocol_information,
2892 .protocols_per_handle = efi_protocols_per_handle,
2893 .locate_handle_buffer = efi_locate_handle_buffer,
2894 .locate_protocol = efi_locate_protocol,
2895 .install_multiple_protocol_interfaces =
2896 efi_install_multiple_protocol_interfaces,
2897 .uninstall_multiple_protocol_interfaces =
2898 efi_uninstall_multiple_protocol_interfaces,
2899 .calculate_crc32 = efi_calculate_crc32,
2900 .copy_mem = efi_copy_mem,
2901 .set_mem = efi_set_mem,
2902 .create_event_ex = efi_create_event_ex,
2905 static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
2907 struct efi_system_table __efi_runtime_data systab = {
2909 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2910 .revision = 2 << 16 | 70, /* 2.7 */
2911 .headersize = sizeof(struct efi_table_hdr),
2913 .fw_vendor = (long)firmware_vendor,
2914 .con_in = (void *)&efi_con_in,
2915 .con_out = (void *)&efi_con_out,
2916 .std_err = (void *)&efi_con_out,
2917 .runtime = (void *)&efi_runtime_services,
2918 .boottime = (void *)&efi_boot_services,
2920 .tables = (void *)efi_conf_table,