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);
126 level = min(max, level * 2);
127 return &indent[max - level];
130 const char *__efi_nesting(void)
132 return indent_string(nesting_level);
135 const char *__efi_nesting_inc(void)
137 return indent_string(nesting_level++);
140 const char *__efi_nesting_dec(void)
142 return indent_string(--nesting_level);
146 * Queue an EFI event.
148 * This function queues the notification function of the event for future
151 * The notification function is called if the task priority level of the
152 * event is higher than the current task priority level.
154 * For the SignalEvent service see efi_signal_event_ext.
156 * @event event to signal
157 * @check_tpl check the TPL level
159 void efi_signal_event(struct efi_event *event, bool check_tpl)
161 if (event->notify_function) {
162 event->is_queued = true;
164 if (check_tpl && efi_tpl >= event->notify_tpl)
166 EFI_CALL_VOID(event->notify_function(event,
167 event->notify_context));
169 event->is_queued = false;
173 * Raise the task priority level.
175 * This function implements the RaiseTpl service.
176 * See the Unified Extensible Firmware Interface (UEFI) specification
179 * @new_tpl new value of the task priority level
180 * @return old value of the task priority level
182 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
184 efi_uintn_t old_tpl = efi_tpl;
186 EFI_ENTRY("0x%zx", new_tpl);
188 if (new_tpl < efi_tpl)
189 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
191 if (efi_tpl > TPL_HIGH_LEVEL)
192 efi_tpl = TPL_HIGH_LEVEL;
194 EFI_EXIT(EFI_SUCCESS);
199 * Lower the task priority level.
201 * This function implements the RestoreTpl service.
202 * See the Unified Extensible Firmware Interface (UEFI) specification
205 * @old_tpl value of the task priority level to be restored
207 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
209 EFI_ENTRY("0x%zx", old_tpl);
211 if (old_tpl > efi_tpl)
212 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
214 if (efi_tpl > TPL_HIGH_LEVEL)
215 efi_tpl = TPL_HIGH_LEVEL;
217 EFI_EXIT(EFI_SUCCESS);
221 * Allocate memory pages.
223 * This function implements the AllocatePages service.
224 * See the Unified Extensible Firmware Interface (UEFI) specification
227 * @type type of allocation to be performed
228 * @memory_type usage type of the allocated memory
229 * @pages number of pages to be allocated
230 * @memory allocated memory
231 * @return status code
233 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
239 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
240 r = efi_allocate_pages(type, memory_type, pages, memory);
247 * This function implements the FreePages service.
248 * See the Unified Extensible Firmware Interface (UEFI) specification
251 * @memory start of the memory area to be freed
252 * @pages number of pages to be freed
253 * @return status code
255 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
260 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
261 r = efi_free_pages(memory, pages);
266 * Get map describing memory usage.
268 * This function implements the GetMemoryMap service.
269 * See the Unified Extensible Firmware Interface (UEFI) specification
272 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
273 * on exit the size of the copied memory map
274 * @memory_map buffer to which the memory map is written
275 * @map_key key for the memory map
276 * @descriptor_size size of an individual memory descriptor
277 * @descriptor_version version number of the memory descriptor structure
278 * @return status code
280 static efi_status_t EFIAPI efi_get_memory_map_ext(
281 efi_uintn_t *memory_map_size,
282 struct efi_mem_desc *memory_map,
283 efi_uintn_t *map_key,
284 efi_uintn_t *descriptor_size,
285 uint32_t *descriptor_version)
289 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
290 map_key, descriptor_size, descriptor_version);
291 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
292 descriptor_size, descriptor_version);
297 * Allocate memory from pool.
299 * This function implements the AllocatePool service.
300 * See the Unified Extensible Firmware Interface (UEFI) specification
303 * @pool_type type of the pool from which memory is to be allocated
304 * @size number of bytes to be allocated
305 * @buffer allocated memory
306 * @return status code
308 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
314 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
315 r = efi_allocate_pool(pool_type, size, buffer);
320 * Free memory from pool.
322 * This function implements the FreePool service.
323 * See the Unified Extensible Firmware Interface (UEFI) specification
326 * @buffer start of memory to be freed
327 * @return status code
329 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
333 EFI_ENTRY("%p", buffer);
334 r = efi_free_pool(buffer);
339 * Add a new object to the object list.
341 * The protocols list is initialized.
342 * The object handle is set.
344 * @obj object to be added
346 void efi_add_handle(struct efi_object *obj)
350 INIT_LIST_HEAD(&obj->protocols);
352 list_add_tail(&obj->link, &efi_obj_list);
359 * @return status code
361 efi_status_t efi_create_handle(efi_handle_t *handle)
363 struct efi_object *obj;
366 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
367 sizeof(struct efi_object),
369 if (r != EFI_SUCCESS)
372 *handle = obj->handle;
377 * Find a protocol on a handle.
380 * @protocol_guid GUID of the protocol
381 * @handler reference to the protocol
382 * @return status code
384 efi_status_t efi_search_protocol(const efi_handle_t handle,
385 const efi_guid_t *protocol_guid,
386 struct efi_handler **handler)
388 struct efi_object *efiobj;
389 struct list_head *lhandle;
391 if (!handle || !protocol_guid)
392 return EFI_INVALID_PARAMETER;
393 efiobj = efi_search_obj(handle);
395 return EFI_INVALID_PARAMETER;
396 list_for_each(lhandle, &efiobj->protocols) {
397 struct efi_handler *protocol;
399 protocol = list_entry(lhandle, struct efi_handler, link);
400 if (!guidcmp(protocol->guid, protocol_guid)) {
406 return EFI_NOT_FOUND;
410 * Delete protocol from a handle.
412 * @handle handle from which the protocol shall be deleted
413 * @protocol GUID of the protocol to be deleted
414 * @protocol_interface interface of the protocol implementation
415 * @return status code
417 efi_status_t efi_remove_protocol(const efi_handle_t handle,
418 const efi_guid_t *protocol,
419 void *protocol_interface)
421 struct efi_handler *handler;
424 ret = efi_search_protocol(handle, protocol, &handler);
425 if (ret != EFI_SUCCESS)
427 if (guidcmp(handler->guid, protocol))
428 return EFI_INVALID_PARAMETER;
429 list_del(&handler->link);
435 * Delete all protocols from a handle.
437 * @handle handle from which the protocols shall be deleted
438 * @return status code
440 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
442 struct efi_object *efiobj;
443 struct efi_handler *protocol;
444 struct efi_handler *pos;
446 efiobj = efi_search_obj(handle);
448 return EFI_INVALID_PARAMETER;
449 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
452 ret = efi_remove_protocol(handle, protocol->guid,
453 protocol->protocol_interface);
454 if (ret != EFI_SUCCESS)
463 * @handle handle to delete
465 void efi_delete_handle(struct efi_object *obj)
469 efi_remove_all_protocols(obj->handle);
470 list_del(&obj->link);
475 * Our event capabilities are very limited. Only a small limited
476 * number of events is allowed to coexist.
478 static struct efi_event efi_events[16];
483 * This function is used inside U-Boot code to create an event.
485 * For the API function implementing the CreateEvent service see
486 * efi_create_event_ext.
488 * @type type of the event to create
489 * @notify_tpl task priority level of the event
490 * @notify_function notification function of the event
491 * @notify_context pointer passed to the notification function
492 * @event created event
493 * @return status code
495 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
496 void (EFIAPI *notify_function) (
497 struct efi_event *event,
499 void *notify_context, struct efi_event **event)
504 return EFI_INVALID_PARAMETER;
506 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
507 return EFI_INVALID_PARAMETER;
509 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
510 notify_function == NULL)
511 return EFI_INVALID_PARAMETER;
513 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
514 if (efi_events[i].type)
516 efi_events[i].type = type;
517 efi_events[i].notify_tpl = notify_tpl;
518 efi_events[i].notify_function = notify_function;
519 efi_events[i].notify_context = notify_context;
520 /* Disable timers on bootup */
521 efi_events[i].trigger_next = -1ULL;
522 efi_events[i].is_queued = false;
523 efi_events[i].is_signaled = false;
524 *event = &efi_events[i];
527 return EFI_OUT_OF_RESOURCES;
531 * Create an event in a group.
533 * This function implements the CreateEventEx service.
534 * See the Unified Extensible Firmware Interface (UEFI) specification
536 * TODO: Support event groups
538 * @type type of the event to create
539 * @notify_tpl task priority level of the event
540 * @notify_function notification function of the event
541 * @notify_context pointer passed to the notification function
542 * @event created event
543 * @event_group event group
544 * @return status code
546 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
547 void (EFIAPI *notify_function) (
548 struct efi_event *event,
550 void *notify_context,
551 efi_guid_t *event_group,
552 struct efi_event **event)
554 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
555 notify_context, event_group);
557 return EFI_EXIT(EFI_UNSUPPORTED);
558 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
559 notify_context, event));
565 * This function implements the CreateEvent service.
566 * See the Unified Extensible Firmware Interface (UEFI) specification
569 * @type type of the event to create
570 * @notify_tpl task priority level of the event
571 * @notify_function notification function of the event
572 * @notify_context pointer passed to the notification function
573 * @event created event
574 * @return status code
576 static efi_status_t EFIAPI efi_create_event_ext(
577 uint32_t type, efi_uintn_t notify_tpl,
578 void (EFIAPI *notify_function) (
579 struct efi_event *event,
581 void *notify_context, struct efi_event **event)
583 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
585 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
586 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, void *table)
1340 return EFI_INVALID_PARAMETER;
1342 /* Check for guid override */
1343 for (i = 0; i < systab.nr_tables; i++) {
1344 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1346 efi_conf_table[i].table = table;
1348 efi_remove_configuration_table(i);
1354 return EFI_NOT_FOUND;
1356 /* No override, check for overflow */
1357 if (i >= ARRAY_SIZE(efi_conf_table))
1358 return EFI_OUT_OF_RESOURCES;
1360 /* Add a new entry */
1361 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1362 efi_conf_table[i].table = table;
1363 systab.nr_tables = i + 1;
1369 * Adds, updates, or removes a configuration table.
1371 * This function implements the InstallConfigurationTable service.
1372 * See the Unified Extensible Firmware Interface (UEFI) specification
1375 * @guid GUID of the installed table
1376 * @table table to be installed
1377 * @return status code
1379 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1382 EFI_ENTRY("%pUl, %p", guid, table);
1383 return EFI_EXIT(efi_install_configuration_table(guid, table));
1387 * Initialize a loaded_image_info + loaded_image_info object with correct
1388 * protocols, boot-device, etc.
1390 * @info loaded image info to be passed to the entry point of the
1392 * @obj internal object associated with the loaded image
1393 * @device_path device path of the loaded image
1394 * @file_path file path of the loaded image
1395 * @return status code
1397 efi_status_t efi_setup_loaded_image(
1398 struct efi_loaded_image *info, struct efi_object *obj,
1399 struct efi_device_path *device_path,
1400 struct efi_device_path *file_path)
1404 /* Add internal object to object list */
1405 efi_add_handle(obj);
1406 /* efi_exit() assumes that the handle points to the info */
1409 info->file_path = file_path;
1412 info->device_handle = efi_dp_find_obj(device_path, NULL);
1414 * When asking for the device path interface, return
1415 * bootefi_device_path
1417 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1419 if (ret != EFI_SUCCESS)
1424 * When asking for the loaded_image interface, just
1425 * return handle which points to loaded_image_info
1427 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1428 if (ret != EFI_SUCCESS)
1431 ret = efi_add_protocol(obj->handle,
1432 &efi_guid_device_path_to_text_protocol,
1433 (void *)&efi_device_path_to_text);
1434 if (ret != EFI_SUCCESS)
1437 ret = efi_add_protocol(obj->handle,
1438 &efi_guid_device_path_utilities_protocol,
1439 (void *)&efi_device_path_utilities);
1440 if (ret != EFI_SUCCESS)
1445 printf("ERROR: Failure to install protocols for loaded image\n");
1450 * Load an image using a file path.
1452 * @file_path the path of the image to load
1453 * @buffer buffer containing the loaded image
1454 * @return status code
1456 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1459 struct efi_file_info *info = NULL;
1460 struct efi_file_handle *f;
1461 static efi_status_t ret;
1464 f = efi_file_from_path(file_path);
1466 return EFI_DEVICE_ERROR;
1469 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1471 if (ret == EFI_BUFFER_TOO_SMALL) {
1473 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1476 if (ret != EFI_SUCCESS)
1479 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1483 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1487 EFI_CALL(f->close(f));
1489 if (ret != EFI_SUCCESS) {
1490 efi_free_pool(*buffer);
1498 * Load an EFI image into memory.
1500 * This function implements the LoadImage service.
1501 * See the Unified Extensible Firmware Interface (UEFI) specification
1504 * @boot_policy true for request originating from the boot manager
1505 * @parent_image the caller's image handle
1506 * @file_path the path of the image to load
1507 * @source_buffer memory location from which the image is installed
1508 * @source_size size of the memory area from which the image is
1510 * @image_handle handle for the newly installed image
1511 * @return status code
1513 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1514 efi_handle_t parent_image,
1515 struct efi_device_path *file_path,
1516 void *source_buffer,
1517 unsigned long source_size,
1518 efi_handle_t *image_handle)
1520 struct efi_loaded_image *info;
1521 struct efi_object *obj;
1524 EFI_ENTRY("%d, %p, %pD, %p, %ld, %p", boot_policy, parent_image,
1525 file_path, source_buffer, source_size, image_handle);
1527 if (!image_handle || !parent_image) {
1528 ret = EFI_INVALID_PARAMETER;
1532 if (!source_buffer && !file_path) {
1533 ret = EFI_NOT_FOUND;
1537 info = calloc(1, sizeof(*info));
1539 ret = EFI_OUT_OF_RESOURCES;
1542 obj = calloc(1, sizeof(*obj));
1545 ret = EFI_OUT_OF_RESOURCES;
1549 if (!source_buffer) {
1550 struct efi_device_path *dp, *fp;
1552 ret = efi_load_image_from_path(file_path, &source_buffer);
1553 if (ret != EFI_SUCCESS)
1556 * split file_path which contains both the device and
1559 efi_dp_split_file_path(file_path, &dp, &fp);
1560 ret = efi_setup_loaded_image(info, obj, dp, fp);
1561 if (ret != EFI_SUCCESS)
1564 /* In this case, file_path is the "device" path, ie.
1565 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1567 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1568 if (ret != EFI_SUCCESS)
1571 info->reserved = efi_load_pe(source_buffer, info);
1572 if (!info->reserved) {
1573 ret = EFI_UNSUPPORTED;
1576 info->system_table = &systab;
1577 info->parent_handle = parent_image;
1578 *image_handle = obj->handle;
1579 return EFI_EXIT(EFI_SUCCESS);
1582 efi_delete_handle(obj);
1584 return EFI_EXIT(ret);
1588 * Call the entry point of an image.
1590 * This function implements the StartImage service.
1591 * See the Unified Extensible Firmware Interface (UEFI) specification
1594 * @image_handle handle of the image
1595 * @exit_data_size size of the buffer
1596 * @exit_data buffer to receive the exit data of the called image
1597 * @return status code
1599 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1600 unsigned long *exit_data_size,
1603 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1604 struct efi_system_table *st);
1605 struct efi_loaded_image *info = image_handle;
1608 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1609 entry = info->reserved;
1611 efi_is_direct_boot = false;
1613 /* call the image! */
1614 if (setjmp(&info->exit_jmp)) {
1616 * We called the entry point of the child image with EFI_CALL
1617 * in the lines below. The child image called the Exit() boot
1618 * service efi_exit() which executed the long jump that brought
1619 * us to the current line. This implies that the second half
1620 * of the EFI_CALL macro has not been executed.
1624 * efi_exit() called efi_restore_gd(). We have to undo this
1625 * otherwise __efi_entry_check() will put the wrong value into
1631 * To get ready to call EFI_EXIT below we have to execute the
1632 * missed out steps of EFI_CALL.
1634 assert(__efi_entry_check());
1635 debug("%sEFI: %lu returned by started image\n",
1636 __efi_nesting_dec(),
1637 (unsigned long)((uintptr_t)info->exit_status &
1639 return EFI_EXIT(info->exit_status);
1642 ret = EFI_CALL(entry(image_handle, &systab));
1645 * Usually UEFI applications call Exit() instead of returning.
1646 * But because the world doesn not consist of ponies and unicorns,
1647 * we're happy to emulate that behavior on behalf of a payload
1650 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
1654 * Leave an EFI application or driver.
1656 * This function implements the Exit service.
1657 * See the Unified Extensible Firmware Interface (UEFI) specification
1660 * @image_handle handle of the application or driver that is exiting
1661 * @exit_status status code
1662 * @exit_data_size size of the buffer in bytes
1663 * @exit_data buffer with data describing an error
1664 * @return status code
1666 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1667 efi_status_t exit_status, unsigned long exit_data_size,
1671 * We require that the handle points to the original loaded
1672 * image protocol interface.
1674 * For getting the longjmp address this is safer than locating
1675 * the protocol because the protocol may have been reinstalled
1676 * pointing to another memory location.
1678 * TODO: We should call the unload procedure of the loaded
1681 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1683 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1684 exit_data_size, exit_data);
1686 /* Make sure entry/exit counts for EFI world cross-overs match */
1687 EFI_EXIT(exit_status);
1690 * But longjmp out with the U-Boot gd, not the application's, as
1691 * the other end is a setjmp call inside EFI context.
1695 loaded_image_info->exit_status = exit_status;
1696 longjmp(&loaded_image_info->exit_jmp, 1);
1698 panic("EFI application exited");
1702 * Unload an EFI image.
1704 * This function implements the UnloadImage service.
1705 * See the Unified Extensible Firmware Interface (UEFI) specification
1708 * @image_handle handle of the image to be unloaded
1709 * @return status code
1711 static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1713 struct efi_object *efiobj;
1715 EFI_ENTRY("%p", image_handle);
1716 efiobj = efi_search_obj(image_handle);
1718 list_del(&efiobj->link);
1720 return EFI_EXIT(EFI_SUCCESS);
1724 * Fix up caches for EFI payloads if necessary.
1726 static void efi_exit_caches(void)
1728 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1730 * Grub on 32bit ARM needs to have caches disabled before jumping into
1731 * a zImage, but does not know of all cache layers. Give it a hand.
1733 if (efi_is_direct_boot)
1734 cleanup_before_linux();
1739 * Stop all boot services.
1741 * This function implements the ExitBootServices service.
1742 * See the Unified Extensible Firmware Interface (UEFI) specification
1745 * All timer events are disabled.
1746 * For exit boot services events the notification function is called.
1747 * The boot services are disabled in the system table.
1749 * @image_handle handle of the loaded image
1750 * @map_key key of the memory map
1751 * @return status code
1753 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1754 unsigned long map_key)
1758 EFI_ENTRY("%p, %ld", image_handle, map_key);
1760 /* Make sure that notification functions are not called anymore */
1761 efi_tpl = TPL_HIGH_LEVEL;
1763 /* Check if ExitBootServices has already been called */
1764 if (!systab.boottime)
1765 return EFI_EXIT(EFI_SUCCESS);
1767 /* Notify that ExitBootServices is invoked. */
1768 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1769 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1771 efi_events[i].is_signaled = true;
1772 efi_signal_event(&efi_events[i], false);
1775 /* TODO Should persist EFI variables here */
1777 board_quiesce_devices();
1779 /* Fix up caches for EFI payloads if necessary */
1782 /* This stops all lingering devices */
1783 bootm_disable_interrupts();
1785 /* Disable boottime services */
1786 systab.con_in_handle = NULL;
1787 systab.con_in = NULL;
1788 systab.con_out_handle = NULL;
1789 systab.con_out = NULL;
1790 systab.stderr_handle = NULL;
1791 systab.std_err = NULL;
1792 systab.boottime = NULL;
1794 /* Recalculate CRC32 */
1795 systab.hdr.crc32 = 0;
1796 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1797 sizeof(struct efi_system_table));
1799 /* Give the payload some time to boot */
1800 efi_set_watchdog(0);
1803 return EFI_EXIT(EFI_SUCCESS);
1807 * Get next value of the counter.
1809 * This function implements the NextMonotonicCount service.
1810 * See the Unified Extensible Firmware Interface (UEFI) specification
1813 * @count returned value of the counter
1814 * @return status code
1816 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1818 static uint64_t mono = 0;
1819 EFI_ENTRY("%p", count);
1821 return EFI_EXIT(EFI_SUCCESS);
1827 * This function implements the Stall sercive.
1828 * See the Unified Extensible Firmware Interface (UEFI) specification
1831 * @microseconds period to sleep in microseconds
1832 * @return status code
1834 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1836 EFI_ENTRY("%ld", microseconds);
1837 udelay(microseconds);
1838 return EFI_EXIT(EFI_SUCCESS);
1842 * Reset the watchdog timer.
1844 * This function implements the SetWatchdogTimer service.
1845 * See the Unified Extensible Firmware Interface (UEFI) specification
1848 * @timeout seconds before reset by watchdog
1849 * @watchdog_code code to be logged when resetting
1850 * @data_size size of buffer in bytes
1851 * @watchdog_data buffer with data describing the reset reason
1852 * @return status code
1854 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1855 uint64_t watchdog_code,
1856 unsigned long data_size,
1857 uint16_t *watchdog_data)
1859 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1860 data_size, watchdog_data);
1861 return EFI_EXIT(efi_set_watchdog(timeout));
1867 * This function implements the CloseProtocol service.
1868 * See the Unified Extensible Firmware Interface (UEFI) specification
1871 * @handle handle on which the protocol shall be closed
1872 * @protocol GUID of the protocol to close
1873 * @agent_handle handle of the driver
1874 * @controller_handle handle of the controller
1875 * @return status code
1877 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1878 const efi_guid_t *protocol,
1879 efi_handle_t agent_handle,
1880 efi_handle_t controller_handle)
1882 struct efi_handler *handler;
1883 struct efi_open_protocol_info_item *item;
1884 struct efi_open_protocol_info_item *pos;
1887 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1890 if (!agent_handle) {
1891 r = EFI_INVALID_PARAMETER;
1894 r = efi_search_protocol(handle, protocol, &handler);
1895 if (r != EFI_SUCCESS)
1899 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1900 if (item->info.agent_handle == agent_handle &&
1901 item->info.controller_handle == controller_handle) {
1902 efi_delete_open_info(item);
1912 * Provide information about then open status of a protocol on a handle
1914 * This function implements the OpenProtocolInformation service.
1915 * See the Unified Extensible Firmware Interface (UEFI) specification
1918 * @handle handle for which the information shall be retrieved
1919 * @protocol GUID of the protocol
1920 * @entry_buffer buffer to receive the open protocol information
1921 * @entry_count number of entries available in the buffer
1922 * @return status code
1924 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1925 const efi_guid_t *protocol,
1926 struct efi_open_protocol_info_entry **entry_buffer,
1927 efi_uintn_t *entry_count)
1929 unsigned long buffer_size;
1930 unsigned long count;
1931 struct efi_handler *handler;
1932 struct efi_open_protocol_info_item *item;
1935 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1938 /* Check parameters */
1939 if (!entry_buffer) {
1940 r = EFI_INVALID_PARAMETER;
1943 r = efi_search_protocol(handle, protocol, &handler);
1944 if (r != EFI_SUCCESS)
1949 list_for_each_entry(item, &handler->open_infos, link) {
1950 if (item->info.open_count)
1953 *entry_count = count;
1954 *entry_buffer = NULL;
1961 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
1962 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1963 (void **)entry_buffer);
1964 if (r != EFI_SUCCESS)
1966 list_for_each_entry_reverse(item, &handler->open_infos, link) {
1967 if (item->info.open_count)
1968 (*entry_buffer)[--count] = item->info;
1975 * Get protocols installed on a handle.
1977 * This function implements the ProtocolsPerHandleService.
1978 * See the Unified Extensible Firmware Interface (UEFI) specification
1981 * @handle handle for which the information is retrieved
1982 * @protocol_buffer buffer with protocol GUIDs
1983 * @protocol_buffer_count number of entries in the buffer
1984 * @return status code
1986 static efi_status_t EFIAPI efi_protocols_per_handle(
1987 efi_handle_t handle, efi_guid_t ***protocol_buffer,
1988 efi_uintn_t *protocol_buffer_count)
1990 unsigned long buffer_size;
1991 struct efi_object *efiobj;
1992 struct list_head *protocol_handle;
1995 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1996 protocol_buffer_count);
1998 if (!handle || !protocol_buffer || !protocol_buffer_count)
1999 return EFI_EXIT(EFI_INVALID_PARAMETER);
2001 *protocol_buffer = NULL;
2002 *protocol_buffer_count = 0;
2004 efiobj = efi_search_obj(handle);
2006 return EFI_EXIT(EFI_INVALID_PARAMETER);
2008 /* Count protocols */
2009 list_for_each(protocol_handle, &efiobj->protocols) {
2010 ++*protocol_buffer_count;
2014 if (*protocol_buffer_count) {
2017 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2018 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2019 (void **)protocol_buffer);
2020 if (r != EFI_SUCCESS)
2022 list_for_each(protocol_handle, &efiobj->protocols) {
2023 struct efi_handler *protocol;
2025 protocol = list_entry(protocol_handle,
2026 struct efi_handler, link);
2027 (*protocol_buffer)[j] = (void *)protocol->guid;
2032 return EFI_EXIT(EFI_SUCCESS);
2036 * Locate handles implementing a protocol.
2038 * This function implements the LocateHandleBuffer service.
2039 * See the Unified Extensible Firmware Interface (UEFI) specification
2042 * @search_type selection criterion
2043 * @protocol GUID of the protocol
2044 * @search_key registration key
2045 * @no_handles number of returned handles
2046 * @buffer buffer with the returned handles
2047 * @return status code
2049 static efi_status_t EFIAPI efi_locate_handle_buffer(
2050 enum efi_locate_search_type search_type,
2051 const efi_guid_t *protocol, void *search_key,
2052 efi_uintn_t *no_handles, efi_handle_t **buffer)
2055 efi_uintn_t buffer_size = 0;
2057 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2058 no_handles, buffer);
2060 if (!no_handles || !buffer) {
2061 r = EFI_INVALID_PARAMETER;
2066 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2068 if (r != EFI_BUFFER_TOO_SMALL)
2070 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2072 if (r != EFI_SUCCESS)
2074 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2076 if (r == EFI_SUCCESS)
2077 *no_handles = buffer_size / sizeof(efi_handle_t);
2083 * Find an interface implementing a protocol.
2085 * This function implements the LocateProtocol service.
2086 * See the Unified Extensible Firmware Interface (UEFI) specification
2089 * @protocol GUID of the protocol
2090 * @registration registration key passed to the notification function
2091 * @protocol_interface interface implementing the protocol
2092 * @return status code
2094 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2096 void **protocol_interface)
2098 struct list_head *lhandle;
2101 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2103 if (!protocol || !protocol_interface)
2104 return EFI_EXIT(EFI_INVALID_PARAMETER);
2106 list_for_each(lhandle, &efi_obj_list) {
2107 struct efi_object *efiobj;
2108 struct efi_handler *handler;
2110 efiobj = list_entry(lhandle, struct efi_object, link);
2112 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2113 if (ret == EFI_SUCCESS) {
2114 *protocol_interface = handler->protocol_interface;
2115 return EFI_EXIT(EFI_SUCCESS);
2118 *protocol_interface = NULL;
2120 return EFI_EXIT(EFI_NOT_FOUND);
2124 * Get the device path and handle of an device implementing a protocol.
2126 * This function implements the LocateDevicePath service.
2127 * See the Unified Extensible Firmware Interface (UEFI) specification
2130 * @protocol GUID of the protocol
2131 * @device_path device path
2132 * @device handle of the device
2133 * @return status code
2135 static efi_status_t EFIAPI efi_locate_device_path(
2136 const efi_guid_t *protocol,
2137 struct efi_device_path **device_path,
2138 efi_handle_t *device)
2140 struct efi_device_path *dp;
2142 struct efi_handler *handler;
2143 efi_handle_t *handles;
2145 size_t len_best = 0;
2146 efi_uintn_t no_handles;
2150 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2152 if (!protocol || !device_path || !*device_path || !device) {
2153 ret = EFI_INVALID_PARAMETER;
2157 /* Find end of device path */
2158 len = efi_dp_size(*device_path);
2160 /* Get all handles implementing the protocol */
2161 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2162 &no_handles, &handles));
2163 if (ret != EFI_SUCCESS)
2166 for (i = 0; i < no_handles; ++i) {
2167 /* Find the device path protocol */
2168 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2170 if (ret != EFI_SUCCESS)
2172 dp = (struct efi_device_path *)handler->protocol_interface;
2173 len_dp = efi_dp_size(dp);
2175 * This handle can only be a better fit
2176 * if its device path length is longer than the best fit and
2177 * if its device path length is shorter of equal the searched
2180 if (len_dp <= len_best || len_dp > len)
2182 /* Check if dp is a subpath of device_path */
2183 if (memcmp(*device_path, dp, len_dp))
2185 *device = handles[i];
2189 remainder = (u8 *)*device_path + len_best;
2190 *device_path = (struct efi_device_path *)remainder;
2193 ret = EFI_NOT_FOUND;
2196 return EFI_EXIT(ret);
2200 * Install multiple protocol interfaces.
2202 * This function implements the MultipleProtocolInterfaces service.
2203 * See the Unified Extensible Firmware Interface (UEFI) specification
2206 * @handle handle on which the protocol interfaces shall be installed
2207 * @... NULL terminated argument list with pairs of protocol GUIDS and
2209 * @return status code
2211 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2214 EFI_ENTRY("%p", handle);
2217 const efi_guid_t *protocol;
2218 void *protocol_interface;
2219 efi_status_t r = EFI_SUCCESS;
2223 return EFI_EXIT(EFI_INVALID_PARAMETER);
2225 va_start(argptr, handle);
2227 protocol = va_arg(argptr, efi_guid_t*);
2230 protocol_interface = va_arg(argptr, void*);
2231 r = EFI_CALL(efi_install_protocol_interface(
2233 EFI_NATIVE_INTERFACE,
2234 protocol_interface));
2235 if (r != EFI_SUCCESS)
2240 if (r == EFI_SUCCESS)
2243 /* If an error occurred undo all changes. */
2244 va_start(argptr, handle);
2246 protocol = va_arg(argptr, efi_guid_t*);
2247 protocol_interface = va_arg(argptr, void*);
2248 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2249 protocol_interface));
2257 * Uninstall multiple protocol interfaces.
2259 * This function implements the UninstallMultipleProtocolInterfaces service.
2260 * See the Unified Extensible Firmware Interface (UEFI) specification
2263 * @handle handle from which the protocol interfaces shall be removed
2264 * @... NULL terminated argument list with pairs of protocol GUIDS and
2266 * @return status code
2268 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2271 EFI_ENTRY("%p", handle);
2274 const efi_guid_t *protocol;
2275 void *protocol_interface;
2276 efi_status_t r = EFI_SUCCESS;
2280 return EFI_EXIT(EFI_INVALID_PARAMETER);
2282 va_start(argptr, handle);
2284 protocol = va_arg(argptr, efi_guid_t*);
2287 protocol_interface = va_arg(argptr, void*);
2288 r = EFI_CALL(efi_uninstall_protocol_interface(
2290 protocol_interface));
2291 if (r != EFI_SUCCESS)
2296 if (r == EFI_SUCCESS)
2299 /* If an error occurred undo all changes. */
2300 va_start(argptr, handle);
2302 protocol = va_arg(argptr, efi_guid_t*);
2303 protocol_interface = va_arg(argptr, void*);
2304 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2305 EFI_NATIVE_INTERFACE,
2306 protocol_interface));
2314 * Calculate cyclic redundancy code.
2316 * This function implements the CalculateCrc32 service.
2317 * See the Unified Extensible Firmware Interface (UEFI) specification
2320 * @data buffer with data
2321 * @data_size size of buffer in bytes
2322 * @crc32_p cyclic redundancy code
2323 * @return status code
2325 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2326 unsigned long data_size,
2329 EFI_ENTRY("%p, %ld", data, data_size);
2330 *crc32_p = crc32(0, data, data_size);
2331 return EFI_EXIT(EFI_SUCCESS);
2337 * This function implements the CopyMem service.
2338 * See the Unified Extensible Firmware Interface (UEFI) specification
2341 * @destination destination of the copy operation
2342 * @source source of the copy operation
2343 * @length number of bytes to copy
2345 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2348 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2349 memcpy(destination, source, length);
2350 EFI_EXIT(EFI_SUCCESS);
2354 * Fill memory with a byte value.
2356 * This function implements the SetMem service.
2357 * See the Unified Extensible Firmware Interface (UEFI) specification
2360 * @buffer buffer to fill
2361 * @size size of buffer in bytes
2362 * @value byte to copy to the buffer
2364 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2366 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2367 memset(buffer, value, size);
2368 EFI_EXIT(EFI_SUCCESS);
2372 * Open protocol interface on a handle.
2374 * @handler handler of a protocol
2375 * @protocol_interface interface implementing the protocol
2376 * @agent_handle handle of the driver
2377 * @controller_handle handle of the controller
2378 * @attributes attributes indicating how to open the protocol
2379 * @return status code
2381 static efi_status_t efi_protocol_open(
2382 struct efi_handler *handler,
2383 void **protocol_interface, void *agent_handle,
2384 void *controller_handle, uint32_t attributes)
2386 struct efi_open_protocol_info_item *item;
2387 struct efi_open_protocol_info_entry *match = NULL;
2388 bool opened_by_driver = false;
2389 bool opened_exclusive = false;
2391 /* If there is no agent, only return the interface */
2395 /* For TEST_PROTOCOL ignore interface attribute */
2396 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2397 *protocol_interface = NULL;
2400 * Check if the protocol is already opened by a driver with the same
2401 * attributes or opened exclusively
2403 list_for_each_entry(item, &handler->open_infos, link) {
2404 if (item->info.agent_handle == agent_handle) {
2405 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2406 (item->info.attributes == attributes))
2407 return EFI_ALREADY_STARTED;
2409 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2410 opened_exclusive = true;
2413 /* Only one controller can open the protocol exclusively */
2414 if (opened_exclusive && attributes &
2415 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2416 return EFI_ACCESS_DENIED;
2418 /* Prepare exclusive opening */
2419 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2420 /* Try to disconnect controllers */
2421 list_for_each_entry(item, &handler->open_infos, link) {
2422 if (item->info.attributes ==
2423 EFI_OPEN_PROTOCOL_BY_DRIVER)
2424 EFI_CALL(efi_disconnect_controller(
2425 item->info.controller_handle,
2426 item->info.agent_handle,
2429 opened_by_driver = false;
2430 /* Check if all controllers are disconnected */
2431 list_for_each_entry(item, &handler->open_infos, link) {
2432 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2433 opened_by_driver = true;
2435 /* Only one controller can be conncected */
2436 if (opened_by_driver)
2437 return EFI_ACCESS_DENIED;
2440 /* Find existing entry */
2441 list_for_each_entry(item, &handler->open_infos, link) {
2442 if (item->info.agent_handle == agent_handle &&
2443 item->info.controller_handle == controller_handle)
2444 match = &item->info;
2446 /* None found, create one */
2448 match = efi_create_open_info(handler);
2450 return EFI_OUT_OF_RESOURCES;
2453 match->agent_handle = agent_handle;
2454 match->controller_handle = controller_handle;
2455 match->attributes = attributes;
2456 match->open_count++;
2459 /* For TEST_PROTOCOL ignore interface attribute. */
2460 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2461 *protocol_interface = handler->protocol_interface;
2467 * Open protocol interface on a handle.
2469 * This function implements the OpenProtocol interface.
2470 * See the Unified Extensible Firmware Interface (UEFI) specification
2473 * @handle handle on which the protocol shall be opened
2474 * @protocol GUID of the protocol
2475 * @protocol_interface interface implementing the protocol
2476 * @agent_handle handle of the driver
2477 * @controller_handle handle of the controller
2478 * @attributes attributes indicating how to open the protocol
2479 * @return status code
2481 static efi_status_t EFIAPI efi_open_protocol(
2482 void *handle, const efi_guid_t *protocol,
2483 void **protocol_interface, void *agent_handle,
2484 void *controller_handle, uint32_t attributes)
2486 struct efi_handler *handler;
2487 efi_status_t r = EFI_INVALID_PARAMETER;
2489 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2490 protocol_interface, agent_handle, controller_handle,
2493 if (!handle || !protocol ||
2494 (!protocol_interface && attributes !=
2495 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2499 switch (attributes) {
2500 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2501 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2502 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2504 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2505 if (controller_handle == handle)
2508 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2509 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2510 /* Check that the controller handle is valid */
2511 if (!efi_search_obj(controller_handle))
2514 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2515 /* Check that the agent handle is valid */
2516 if (!efi_search_obj(agent_handle))
2523 r = efi_search_protocol(handle, protocol, &handler);
2524 if (r != EFI_SUCCESS)
2527 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2528 controller_handle, attributes);
2534 * Get interface of a protocol on a handle.
2536 * This function implements the HandleProtocol service.
2537 * See the Unified Extensible Firmware Interface (UEFI) specification
2540 * @handle handle on which the protocol shall be opened
2541 * @protocol GUID of the protocol
2542 * @protocol_interface interface implementing the protocol
2543 * @return status code
2545 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2546 const efi_guid_t *protocol,
2547 void **protocol_interface)
2549 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2550 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2553 static efi_status_t efi_bind_controller(
2554 efi_handle_t controller_handle,
2555 efi_handle_t driver_image_handle,
2556 struct efi_device_path *remain_device_path)
2558 struct efi_driver_binding_protocol *binding_protocol;
2561 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2562 &efi_guid_driver_binding_protocol,
2563 (void **)&binding_protocol,
2564 driver_image_handle, NULL,
2565 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2566 if (r != EFI_SUCCESS)
2568 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2570 remain_device_path));
2571 if (r == EFI_SUCCESS)
2572 r = EFI_CALL(binding_protocol->start(binding_protocol,
2574 remain_device_path));
2575 EFI_CALL(efi_close_protocol(driver_image_handle,
2576 &efi_guid_driver_binding_protocol,
2577 driver_image_handle, NULL));
2581 static efi_status_t efi_connect_single_controller(
2582 efi_handle_t controller_handle,
2583 efi_handle_t *driver_image_handle,
2584 struct efi_device_path *remain_device_path)
2586 efi_handle_t *buffer;
2590 size_t connected = 0;
2592 /* Get buffer with all handles with driver binding protocol */
2593 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2594 &efi_guid_driver_binding_protocol,
2595 NULL, &count, &buffer));
2596 if (r != EFI_SUCCESS)
2599 /* Context Override */
2600 if (driver_image_handle) {
2601 for (; *driver_image_handle; ++driver_image_handle) {
2602 for (i = 0; i < count; ++i) {
2603 if (buffer[i] == *driver_image_handle) {
2605 r = efi_bind_controller(
2607 *driver_image_handle,
2608 remain_device_path);
2610 * For drivers that do not support the
2611 * controller or are already connected
2612 * we receive an error code here.
2614 if (r == EFI_SUCCESS)
2622 * TODO: Some overrides are not yet implemented:
2623 * - Platform Driver Override
2624 * - Driver Family Override Search
2625 * - Bus Specific Driver Override
2628 /* Driver Binding Search */
2629 for (i = 0; i < count; ++i) {
2631 r = efi_bind_controller(controller_handle,
2633 remain_device_path);
2634 if (r == EFI_SUCCESS)
2639 efi_free_pool(buffer);
2641 return EFI_NOT_FOUND;
2646 * Connect a controller to a driver.
2648 * This function implements the ConnectController service.
2649 * See the Unified Extensible Firmware Interface (UEFI) specification
2652 * First all driver binding protocol handles are tried for binding drivers.
2653 * Afterwards all handles that have openened a protocol of the controller
2654 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2656 * @controller_handle handle of the controller
2657 * @driver_image_handle handle of the driver
2658 * @remain_device_path device path of a child controller
2659 * @recursive true to connect all child controllers
2660 * @return status code
2662 static efi_status_t EFIAPI efi_connect_controller(
2663 efi_handle_t controller_handle,
2664 efi_handle_t *driver_image_handle,
2665 struct efi_device_path *remain_device_path,
2669 efi_status_t ret = EFI_NOT_FOUND;
2670 struct efi_object *efiobj;
2672 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2673 remain_device_path, recursive);
2675 efiobj = efi_search_obj(controller_handle);
2677 ret = EFI_INVALID_PARAMETER;
2681 r = efi_connect_single_controller(controller_handle,
2682 driver_image_handle,
2683 remain_device_path);
2684 if (r == EFI_SUCCESS)
2687 struct efi_handler *handler;
2688 struct efi_open_protocol_info_item *item;
2690 list_for_each_entry(handler, &efiobj->protocols, link) {
2691 list_for_each_entry(item, &handler->open_infos, link) {
2692 if (item->info.attributes &
2693 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2694 r = EFI_CALL(efi_connect_controller(
2695 item->info.controller_handle,
2696 driver_image_handle,
2699 if (r == EFI_SUCCESS)
2705 /* Check for child controller specified by end node */
2706 if (ret != EFI_SUCCESS && remain_device_path &&
2707 remain_device_path->type == DEVICE_PATH_TYPE_END)
2710 return EFI_EXIT(ret);
2714 * Get all child controllers associated to a driver.
2715 * The allocated buffer has to be freed with free().
2717 * @efiobj handle of the controller
2718 * @driver_handle handle of the driver
2719 * @number_of_children number of child controllers
2720 * @child_handle_buffer handles of the the child controllers
2722 static efi_status_t efi_get_child_controllers(
2723 struct efi_object *efiobj,
2724 efi_handle_t driver_handle,
2725 efi_uintn_t *number_of_children,
2726 efi_handle_t **child_handle_buffer)
2728 struct efi_handler *handler;
2729 struct efi_open_protocol_info_item *item;
2730 efi_uintn_t count = 0, i;
2733 /* Count all child controller associations */
2734 list_for_each_entry(handler, &efiobj->protocols, link) {
2735 list_for_each_entry(item, &handler->open_infos, link) {
2736 if (item->info.agent_handle == driver_handle &&
2737 item->info.attributes &
2738 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2743 * Create buffer. In case of duplicate child controller assignments
2744 * the buffer will be too large. But that does not harm.
2746 *number_of_children = 0;
2747 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2748 if (!*child_handle_buffer)
2749 return EFI_OUT_OF_RESOURCES;
2750 /* Copy unique child handles */
2751 list_for_each_entry(handler, &efiobj->protocols, link) {
2752 list_for_each_entry(item, &handler->open_infos, link) {
2753 if (item->info.agent_handle == driver_handle &&
2754 item->info.attributes &
2755 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2756 /* Check this is a new child controller */
2758 for (i = 0; i < *number_of_children; ++i) {
2759 if ((*child_handle_buffer)[i] ==
2760 item->info.controller_handle)
2763 /* Copy handle to buffer */
2765 i = (*number_of_children)++;
2766 (*child_handle_buffer)[i] =
2767 item->info.controller_handle;
2776 * Disconnect a controller from a driver.
2778 * This function implements the DisconnectController service.
2779 * See the Unified Extensible Firmware Interface (UEFI) specification
2782 * @controller_handle handle of the controller
2783 * @driver_image_handle handle of the driver
2784 * @child_handle handle of the child to destroy
2785 * @return status code
2787 static efi_status_t EFIAPI efi_disconnect_controller(
2788 efi_handle_t controller_handle,
2789 efi_handle_t driver_image_handle,
2790 efi_handle_t child_handle)
2792 struct efi_driver_binding_protocol *binding_protocol;
2793 efi_handle_t *child_handle_buffer = NULL;
2794 size_t number_of_children = 0;
2796 size_t stop_count = 0;
2797 struct efi_object *efiobj;
2799 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2802 efiobj = efi_search_obj(controller_handle);
2804 r = EFI_INVALID_PARAMETER;
2808 if (child_handle && !efi_search_obj(child_handle)) {
2809 r = EFI_INVALID_PARAMETER;
2813 /* If no driver handle is supplied, disconnect all drivers */
2814 if (!driver_image_handle) {
2815 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2819 /* Create list of child handles */
2821 number_of_children = 1;
2822 child_handle_buffer = &child_handle;
2824 efi_get_child_controllers(efiobj,
2825 driver_image_handle,
2826 &number_of_children,
2827 &child_handle_buffer);
2830 /* Get the driver binding protocol */
2831 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2832 &efi_guid_driver_binding_protocol,
2833 (void **)&binding_protocol,
2834 driver_image_handle, NULL,
2835 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2836 if (r != EFI_SUCCESS)
2838 /* Remove the children */
2839 if (number_of_children) {
2840 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2843 child_handle_buffer));
2844 if (r == EFI_SUCCESS)
2847 /* Remove the driver */
2849 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2852 if (r == EFI_SUCCESS)
2854 EFI_CALL(efi_close_protocol(driver_image_handle,
2855 &efi_guid_driver_binding_protocol,
2856 driver_image_handle, NULL));
2864 free(child_handle_buffer);
2868 static const struct efi_boot_services efi_boot_services = {
2870 .headersize = sizeof(struct efi_table_hdr),
2872 .raise_tpl = efi_raise_tpl,
2873 .restore_tpl = efi_restore_tpl,
2874 .allocate_pages = efi_allocate_pages_ext,
2875 .free_pages = efi_free_pages_ext,
2876 .get_memory_map = efi_get_memory_map_ext,
2877 .allocate_pool = efi_allocate_pool_ext,
2878 .free_pool = efi_free_pool_ext,
2879 .create_event = efi_create_event_ext,
2880 .set_timer = efi_set_timer_ext,
2881 .wait_for_event = efi_wait_for_event,
2882 .signal_event = efi_signal_event_ext,
2883 .close_event = efi_close_event,
2884 .check_event = efi_check_event,
2885 .install_protocol_interface = efi_install_protocol_interface,
2886 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2887 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2888 .handle_protocol = efi_handle_protocol,
2890 .register_protocol_notify = efi_register_protocol_notify,
2891 .locate_handle = efi_locate_handle_ext,
2892 .locate_device_path = efi_locate_device_path,
2893 .install_configuration_table = efi_install_configuration_table_ext,
2894 .load_image = efi_load_image,
2895 .start_image = efi_start_image,
2897 .unload_image = efi_unload_image,
2898 .exit_boot_services = efi_exit_boot_services,
2899 .get_next_monotonic_count = efi_get_next_monotonic_count,
2901 .set_watchdog_timer = efi_set_watchdog_timer,
2902 .connect_controller = efi_connect_controller,
2903 .disconnect_controller = efi_disconnect_controller,
2904 .open_protocol = efi_open_protocol,
2905 .close_protocol = efi_close_protocol,
2906 .open_protocol_information = efi_open_protocol_information,
2907 .protocols_per_handle = efi_protocols_per_handle,
2908 .locate_handle_buffer = efi_locate_handle_buffer,
2909 .locate_protocol = efi_locate_protocol,
2910 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2911 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2912 .calculate_crc32 = efi_calculate_crc32,
2913 .copy_mem = efi_copy_mem,
2914 .set_mem = efi_set_mem,
2915 .create_event_ex = efi_create_event_ex,
2919 static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
2921 struct efi_system_table __efi_runtime_data systab = {
2923 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2924 .revision = 2 << 16 | 70, /* 2.7 */
2925 .headersize = sizeof(struct efi_table_hdr),
2927 .fw_vendor = (long)firmware_vendor,
2928 .con_in = (void*)&efi_con_in,
2929 .con_out = (void*)&efi_con_out,
2930 .std_err = (void*)&efi_con_out,
2931 .runtime = (void*)&efi_runtime_services,
2932 .boottime = (void*)&efi_boot_services,
2934 .tables = (void*)efi_conf_table,