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 EFI_DRIVER_BINDING_PROTOCOL */
60 const efi_guid_t efi_guid_driver_binding_protocol =
61 EFI_DRIVER_BINDING_PROTOCOL_GUID;
63 static efi_status_t EFIAPI efi_disconnect_controller(
64 efi_handle_t controller_handle,
65 efi_handle_t driver_image_handle,
66 efi_handle_t child_handle);
68 /* Called on every callback entry */
69 int __efi_entry_check(void)
71 int ret = entry_count++ == 0;
80 /* Called on every callback exit */
81 int __efi_exit_check(void)
83 int ret = --entry_count == 0;
90 /* Called from do_bootefi_exec() */
91 void efi_save_gd(void)
99 * Special case handler for error/abort that just forces things back
100 * to u-boot world so we can dump out an abort msg, without any care
101 * about returning back to UEFI world.
103 void efi_restore_gd(void)
106 /* Only restore if we're already in EFI context */
114 * Return a string for indenting with two spaces per level. A maximum of ten
115 * indent levels is supported. Higher indent levels will be truncated.
117 * @level indent level
118 * @return indent string
120 static const char *indent_string(int level)
122 const char *indent = " ";
123 const int max = strlen(indent);
124 level = min(max, level * 2);
125 return &indent[max - level];
128 const char *__efi_nesting(void)
130 return indent_string(nesting_level);
133 const char *__efi_nesting_inc(void)
135 return indent_string(nesting_level++);
138 const char *__efi_nesting_dec(void)
140 return indent_string(--nesting_level);
144 * Queue an EFI event.
146 * This function queues the notification function of the event for future
149 * The notification function is called if the task priority level of the
150 * event is higher than the current task priority level.
152 * For the SignalEvent service see efi_signal_event_ext.
154 * @event event to signal
155 * @check_tpl check the TPL level
157 void efi_signal_event(struct efi_event *event, bool check_tpl)
159 if (event->notify_function) {
160 event->is_queued = true;
162 if (check_tpl && efi_tpl >= event->notify_tpl)
164 EFI_CALL_VOID(event->notify_function(event,
165 event->notify_context));
167 event->is_queued = false;
171 * Raise the task priority level.
173 * This function implements the RaiseTpl service.
174 * See the Unified Extensible Firmware Interface (UEFI) specification
177 * @new_tpl new value of the task priority level
178 * @return old value of the task priority level
180 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
182 efi_uintn_t old_tpl = efi_tpl;
184 EFI_ENTRY("0x%zx", new_tpl);
186 if (new_tpl < efi_tpl)
187 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
189 if (efi_tpl > TPL_HIGH_LEVEL)
190 efi_tpl = TPL_HIGH_LEVEL;
192 EFI_EXIT(EFI_SUCCESS);
197 * Lower the task priority level.
199 * This function implements the RestoreTpl service.
200 * See the Unified Extensible Firmware Interface (UEFI) specification
203 * @old_tpl value of the task priority level to be restored
205 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
207 EFI_ENTRY("0x%zx", old_tpl);
209 if (old_tpl > efi_tpl)
210 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
212 if (efi_tpl > TPL_HIGH_LEVEL)
213 efi_tpl = TPL_HIGH_LEVEL;
215 EFI_EXIT(EFI_SUCCESS);
219 * Allocate memory pages.
221 * This function implements the AllocatePages service.
222 * See the Unified Extensible Firmware Interface (UEFI) specification
225 * @type type of allocation to be performed
226 * @memory_type usage type of the allocated memory
227 * @pages number of pages to be allocated
228 * @memory allocated memory
229 * @return status code
231 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
237 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
238 r = efi_allocate_pages(type, memory_type, pages, memory);
245 * This function implements the FreePages service.
246 * See the Unified Extensible Firmware Interface (UEFI) specification
249 * @memory start of the memory area to be freed
250 * @pages number of pages to be freed
251 * @return status code
253 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
258 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
259 r = efi_free_pages(memory, pages);
264 * Get map describing memory usage.
266 * This function implements the GetMemoryMap service.
267 * See the Unified Extensible Firmware Interface (UEFI) specification
270 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
271 * on exit the size of the copied memory map
272 * @memory_map buffer to which the memory map is written
273 * @map_key key for the memory map
274 * @descriptor_size size of an individual memory descriptor
275 * @descriptor_version version number of the memory descriptor structure
276 * @return status code
278 static efi_status_t EFIAPI efi_get_memory_map_ext(
279 efi_uintn_t *memory_map_size,
280 struct efi_mem_desc *memory_map,
281 efi_uintn_t *map_key,
282 efi_uintn_t *descriptor_size,
283 uint32_t *descriptor_version)
287 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
288 map_key, descriptor_size, descriptor_version);
289 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
290 descriptor_size, descriptor_version);
295 * Allocate memory from pool.
297 * This function implements the AllocatePool service.
298 * See the Unified Extensible Firmware Interface (UEFI) specification
301 * @pool_type type of the pool from which memory is to be allocated
302 * @size number of bytes to be allocated
303 * @buffer allocated memory
304 * @return status code
306 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
312 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
313 r = efi_allocate_pool(pool_type, size, buffer);
318 * Free memory from pool.
320 * This function implements the FreePool service.
321 * See the Unified Extensible Firmware Interface (UEFI) specification
324 * @buffer start of memory to be freed
325 * @return status code
327 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
331 EFI_ENTRY("%p", buffer);
332 r = efi_free_pool(buffer);
337 * Add a new object to the object list.
339 * The protocols list is initialized.
340 * The object handle is set.
342 * @obj object to be added
344 void efi_add_handle(struct efi_object *obj)
348 INIT_LIST_HEAD(&obj->protocols);
350 list_add_tail(&obj->link, &efi_obj_list);
357 * @return status code
359 efi_status_t efi_create_handle(efi_handle_t *handle)
361 struct efi_object *obj;
364 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
365 sizeof(struct efi_object),
367 if (r != EFI_SUCCESS)
370 *handle = obj->handle;
375 * Find a protocol on a handle.
378 * @protocol_guid GUID of the protocol
379 * @handler reference to the protocol
380 * @return status code
382 efi_status_t efi_search_protocol(const efi_handle_t handle,
383 const efi_guid_t *protocol_guid,
384 struct efi_handler **handler)
386 struct efi_object *efiobj;
387 struct list_head *lhandle;
389 if (!handle || !protocol_guid)
390 return EFI_INVALID_PARAMETER;
391 efiobj = efi_search_obj(handle);
393 return EFI_INVALID_PARAMETER;
394 list_for_each(lhandle, &efiobj->protocols) {
395 struct efi_handler *protocol;
397 protocol = list_entry(lhandle, struct efi_handler, link);
398 if (!guidcmp(protocol->guid, protocol_guid)) {
404 return EFI_NOT_FOUND;
408 * Delete protocol from a handle.
410 * @handle handle from which the protocol shall be deleted
411 * @protocol GUID of the protocol to be deleted
412 * @protocol_interface interface of the protocol implementation
413 * @return status code
415 efi_status_t efi_remove_protocol(const efi_handle_t handle,
416 const efi_guid_t *protocol,
417 void *protocol_interface)
419 struct efi_handler *handler;
422 ret = efi_search_protocol(handle, protocol, &handler);
423 if (ret != EFI_SUCCESS)
425 if (guidcmp(handler->guid, protocol))
426 return EFI_INVALID_PARAMETER;
427 list_del(&handler->link);
433 * Delete all protocols from a handle.
435 * @handle handle from which the protocols shall be deleted
436 * @return status code
438 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
440 struct efi_object *efiobj;
441 struct efi_handler *protocol;
442 struct efi_handler *pos;
444 efiobj = efi_search_obj(handle);
446 return EFI_INVALID_PARAMETER;
447 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
450 ret = efi_remove_protocol(handle, protocol->guid,
451 protocol->protocol_interface);
452 if (ret != EFI_SUCCESS)
461 * @handle handle to delete
463 void efi_delete_handle(struct efi_object *obj)
467 efi_remove_all_protocols(obj->handle);
468 list_del(&obj->link);
473 * Our event capabilities are very limited. Only a small limited
474 * number of events is allowed to coexist.
476 static struct efi_event efi_events[16];
481 * This function is used inside U-Boot code to create an event.
483 * For the API function implementing the CreateEvent service see
484 * efi_create_event_ext.
486 * @type type of the event to create
487 * @notify_tpl task priority level of the event
488 * @notify_function notification function of the event
489 * @notify_context pointer passed to the notification function
490 * @event created event
491 * @return status code
493 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
494 void (EFIAPI *notify_function) (
495 struct efi_event *event,
497 void *notify_context, struct efi_event **event)
502 return EFI_INVALID_PARAMETER;
504 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
505 return EFI_INVALID_PARAMETER;
507 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
508 notify_function == NULL)
509 return EFI_INVALID_PARAMETER;
511 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
512 if (efi_events[i].type)
514 efi_events[i].type = type;
515 efi_events[i].notify_tpl = notify_tpl;
516 efi_events[i].notify_function = notify_function;
517 efi_events[i].notify_context = notify_context;
518 /* Disable timers on bootup */
519 efi_events[i].trigger_next = -1ULL;
520 efi_events[i].is_queued = false;
521 efi_events[i].is_signaled = false;
522 *event = &efi_events[i];
525 return EFI_OUT_OF_RESOURCES;
529 * Create an event in a group.
531 * This function implements the CreateEventEx service.
532 * See the Unified Extensible Firmware Interface (UEFI) specification
534 * TODO: Support event groups
536 * @type type of the event to create
537 * @notify_tpl task priority level of the event
538 * @notify_function notification function of the event
539 * @notify_context pointer passed to the notification function
540 * @event created event
541 * @event_group event group
542 * @return status code
544 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
545 void (EFIAPI *notify_function) (
546 struct efi_event *event,
548 void *notify_context,
549 efi_guid_t *event_group,
550 struct efi_event **event)
552 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
553 notify_context, event_group);
555 return EFI_EXIT(EFI_UNSUPPORTED);
556 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
557 notify_context, event));
563 * This function implements the CreateEvent service.
564 * See the Unified Extensible Firmware Interface (UEFI) specification
567 * @type type of the event to create
568 * @notify_tpl task priority level of the event
569 * @notify_function notification function of the event
570 * @notify_context pointer passed to the notification function
571 * @event created event
572 * @return status code
574 static efi_status_t EFIAPI efi_create_event_ext(
575 uint32_t type, efi_uintn_t notify_tpl,
576 void (EFIAPI *notify_function) (
577 struct efi_event *event,
579 void *notify_context, struct efi_event **event)
581 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
583 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
584 notify_context, event));
589 * Check if a timer event has occurred or a queued notification function should
592 * Our timers have to work without interrupts, so we check whenever keyboard
593 * input or disk accesses happen if enough time elapsed for them to fire.
595 void efi_timer_check(void)
598 u64 now = timer_get_us();
600 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
601 if (!efi_events[i].type)
603 if (efi_events[i].is_queued)
604 efi_signal_event(&efi_events[i], true);
605 if (!(efi_events[i].type & EVT_TIMER) ||
606 now < efi_events[i].trigger_next)
608 switch (efi_events[i].trigger_type) {
609 case EFI_TIMER_RELATIVE:
610 efi_events[i].trigger_type = EFI_TIMER_STOP;
612 case EFI_TIMER_PERIODIC:
613 efi_events[i].trigger_next +=
614 efi_events[i].trigger_time;
619 efi_events[i].is_signaled = true;
620 efi_signal_event(&efi_events[i], true);
626 * Set the trigger time for a timer event or stop the event.
628 * This is the function for internal usage in U-Boot. For the API function
629 * implementing the SetTimer service see efi_set_timer_ext.
631 * @event event for which the timer is set
632 * @type type of the timer
633 * @trigger_time trigger period in multiples of 100ns
634 * @return status code
636 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
637 uint64_t trigger_time)
642 * The parameter defines a multiple of 100ns.
643 * We use multiples of 1000ns. So divide by 10.
645 do_div(trigger_time, 10);
647 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
648 if (event != &efi_events[i])
651 if (!(event->type & EVT_TIMER))
655 event->trigger_next = -1ULL;
657 case EFI_TIMER_PERIODIC:
658 case EFI_TIMER_RELATIVE:
659 event->trigger_next =
660 timer_get_us() + trigger_time;
663 return EFI_INVALID_PARAMETER;
665 event->trigger_type = type;
666 event->trigger_time = trigger_time;
667 event->is_signaled = false;
670 return EFI_INVALID_PARAMETER;
674 * Set the trigger time for a timer event or stop the event.
676 * This function implements the SetTimer service.
677 * See the Unified Extensible Firmware Interface (UEFI) specification
680 * @event event for which the timer is set
681 * @type type of the timer
682 * @trigger_time trigger period in multiples of 100ns
683 * @return status code
685 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
686 enum efi_timer_delay type,
687 uint64_t trigger_time)
689 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
690 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
694 * Wait for events to be signaled.
696 * This function implements the WaitForEvent service.
697 * See the Unified Extensible Firmware Interface (UEFI) specification
700 * @num_events number of events to be waited for
701 * @events events to be waited for
702 * @index index of the event that was signaled
703 * @return status code
705 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
706 struct efi_event **event,
711 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
713 /* Check parameters */
714 if (!num_events || !event)
715 return EFI_EXIT(EFI_INVALID_PARAMETER);
717 if (efi_tpl != TPL_APPLICATION)
718 return EFI_EXIT(EFI_UNSUPPORTED);
719 for (i = 0; i < num_events; ++i) {
720 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
721 if (event[i] == &efi_events[j])
724 return EFI_EXIT(EFI_INVALID_PARAMETER);
726 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
727 return EFI_EXIT(EFI_INVALID_PARAMETER);
728 if (!event[i]->is_signaled)
729 efi_signal_event(event[i], true);
732 /* Wait for signal */
734 for (i = 0; i < num_events; ++i) {
735 if (event[i]->is_signaled)
738 /* Allow events to occur. */
744 * Reset the signal which is passed to the caller to allow periodic
747 event[i]->is_signaled = false;
751 return EFI_EXIT(EFI_SUCCESS);
755 * Signal an EFI event.
757 * This function implements the SignalEvent service.
758 * See the Unified Extensible Firmware Interface (UEFI) specification
761 * This functions sets the signaled state of the event and queues the
762 * notification function for execution.
764 * @event event to signal
765 * @return status code
767 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
771 EFI_ENTRY("%p", event);
772 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
773 if (event != &efi_events[i])
775 if (event->is_signaled)
777 event->is_signaled = true;
778 if (event->type & EVT_NOTIFY_SIGNAL)
779 efi_signal_event(event, true);
782 return EFI_EXIT(EFI_SUCCESS);
786 * Close an EFI event.
788 * This function implements the CloseEvent service.
789 * See the Unified Extensible Firmware Interface (UEFI) specification
792 * @event event to close
793 * @return status code
795 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
799 EFI_ENTRY("%p", event);
800 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
801 if (event == &efi_events[i]) {
803 event->trigger_next = -1ULL;
804 event->is_queued = false;
805 event->is_signaled = false;
806 return EFI_EXIT(EFI_SUCCESS);
809 return EFI_EXIT(EFI_INVALID_PARAMETER);
813 * Check if an event is signaled.
815 * This function implements the CheckEvent service.
816 * See the Unified Extensible Firmware Interface (UEFI) specification
819 * If an event is not signaled yet the notification function is queued.
821 * @event event to check
822 * @return status code
824 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
828 EFI_ENTRY("%p", event);
830 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
831 if (event != &efi_events[i])
833 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
835 if (!event->is_signaled)
836 efi_signal_event(event, true);
837 if (event->is_signaled)
838 return EFI_EXIT(EFI_SUCCESS);
839 return EFI_EXIT(EFI_NOT_READY);
841 return EFI_EXIT(EFI_INVALID_PARAMETER);
845 * Find the internal EFI object for a handle.
847 * @handle handle to find
850 struct efi_object *efi_search_obj(const efi_handle_t handle)
852 struct efi_object *efiobj;
854 list_for_each_entry(efiobj, &efi_obj_list, link) {
855 if (efiobj->handle == handle)
863 * Create open protocol info entry and add it to a protocol.
865 * @handler handler of a protocol
866 * @return open protocol info entry
868 static struct efi_open_protocol_info_entry *efi_create_open_info(
869 struct efi_handler *handler)
871 struct efi_open_protocol_info_item *item;
873 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
876 /* Append the item to the open protocol info list. */
877 list_add_tail(&item->link, &handler->open_infos);
883 * Remove an open protocol info entry from a protocol.
885 * @handler handler of a protocol
886 * @return status code
888 static efi_status_t efi_delete_open_info(
889 struct efi_open_protocol_info_item *item)
891 list_del(&item->link);
897 * Install new protocol on a handle.
899 * @handle handle on which the protocol shall be installed
900 * @protocol GUID of the protocol to be installed
901 * @protocol_interface interface of the protocol implementation
902 * @return status code
904 efi_status_t efi_add_protocol(const efi_handle_t handle,
905 const efi_guid_t *protocol,
906 void *protocol_interface)
908 struct efi_object *efiobj;
909 struct efi_handler *handler;
912 efiobj = efi_search_obj(handle);
914 return EFI_INVALID_PARAMETER;
915 ret = efi_search_protocol(handle, protocol, NULL);
916 if (ret != EFI_NOT_FOUND)
917 return EFI_INVALID_PARAMETER;
918 handler = calloc(1, sizeof(struct efi_handler));
920 return EFI_OUT_OF_RESOURCES;
921 handler->guid = protocol;
922 handler->protocol_interface = protocol_interface;
923 INIT_LIST_HEAD(&handler->open_infos);
924 list_add_tail(&handler->link, &efiobj->protocols);
925 if (!guidcmp(&efi_guid_device_path, protocol))
926 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
931 * Install protocol interface.
933 * This function implements the InstallProtocolInterface service.
934 * See the Unified Extensible Firmware Interface (UEFI) specification
937 * @handle handle on which the protocol shall be installed
938 * @protocol GUID of the protocol to be installed
939 * @protocol_interface_type type of the interface to be installed,
940 * always EFI_NATIVE_INTERFACE
941 * @protocol_interface interface of the protocol implementation
942 * @return status code
944 static efi_status_t EFIAPI efi_install_protocol_interface(
945 void **handle, const efi_guid_t *protocol,
946 int protocol_interface_type, void *protocol_interface)
950 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
953 if (!handle || !protocol ||
954 protocol_interface_type != EFI_NATIVE_INTERFACE) {
955 r = EFI_INVALID_PARAMETER;
959 /* Create new handle if requested. */
961 r = efi_create_handle(handle);
962 if (r != EFI_SUCCESS)
964 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
967 debug("%sEFI: handle %p\n", indent_string(nesting_level),
970 /* Add new protocol */
971 r = efi_add_protocol(*handle, protocol, protocol_interface);
977 * Reinstall protocol interface.
979 * This function implements the ReinstallProtocolInterface service.
980 * See the Unified Extensible Firmware Interface (UEFI) specification
983 * @handle handle on which the protocol shall be
985 * @protocol GUID of the protocol to be installed
986 * @old_interface interface to be removed
987 * @new_interface interface to be installed
988 * @return status code
990 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
991 efi_handle_t handle, const efi_guid_t *protocol,
992 void *old_interface, void *new_interface)
994 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
996 return EFI_EXIT(EFI_ACCESS_DENIED);
1000 * Get all drivers associated to a controller.
1001 * The allocated buffer has to be freed with free().
1003 * @efiobj handle of the controller
1004 * @protocol protocol guid (optional)
1005 * @number_of_drivers number of child controllers
1006 * @driver_handle_buffer handles of the the drivers
1007 * @return status code
1009 static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1010 const efi_guid_t *protocol,
1011 efi_uintn_t *number_of_drivers,
1012 efi_handle_t **driver_handle_buffer)
1014 struct efi_handler *handler;
1015 struct efi_open_protocol_info_item *item;
1016 efi_uintn_t count = 0, i;
1019 /* Count all driver associations */
1020 list_for_each_entry(handler, &efiobj->protocols, link) {
1021 if (protocol && guidcmp(handler->guid, protocol))
1023 list_for_each_entry(item, &handler->open_infos, link) {
1024 if (item->info.attributes &
1025 EFI_OPEN_PROTOCOL_BY_DRIVER)
1030 * Create buffer. In case of duplicate driver assignments the buffer
1031 * will be too large. But that does not harm.
1033 *number_of_drivers = 0;
1034 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1035 if (!*driver_handle_buffer)
1036 return EFI_OUT_OF_RESOURCES;
1037 /* Collect unique driver handles */
1038 list_for_each_entry(handler, &efiobj->protocols, link) {
1039 if (protocol && guidcmp(handler->guid, protocol))
1041 list_for_each_entry(item, &handler->open_infos, link) {
1042 if (item->info.attributes &
1043 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1044 /* Check this is a new driver */
1046 for (i = 0; i < *number_of_drivers; ++i) {
1047 if ((*driver_handle_buffer)[i] ==
1048 item->info.agent_handle)
1051 /* Copy handle to buffer */
1053 i = (*number_of_drivers)++;
1054 (*driver_handle_buffer)[i] =
1055 item->info.agent_handle;
1064 * Disconnect all drivers from a controller.
1066 * This function implements the DisconnectController service.
1067 * See the Unified Extensible Firmware Interface (UEFI) specification
1070 * @efiobj handle of the controller
1071 * @protocol protocol guid (optional)
1072 * @child_handle handle of the child to destroy
1073 * @return status code
1075 static efi_status_t efi_disconnect_all_drivers(
1076 struct efi_object *efiobj,
1077 const efi_guid_t *protocol,
1078 efi_handle_t child_handle)
1080 efi_uintn_t number_of_drivers;
1081 efi_handle_t *driver_handle_buffer;
1082 efi_status_t r, ret;
1084 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1085 &driver_handle_buffer);
1086 if (ret != EFI_SUCCESS)
1089 ret = EFI_NOT_FOUND;
1090 while (number_of_drivers) {
1091 r = EFI_CALL(efi_disconnect_controller(
1093 driver_handle_buffer[--number_of_drivers],
1095 if (r == EFI_SUCCESS)
1098 free(driver_handle_buffer);
1103 * Uninstall protocol interface.
1105 * This function implements the UninstallProtocolInterface service.
1106 * See the Unified Extensible Firmware Interface (UEFI) specification
1109 * @handle handle from which the protocol shall be removed
1110 * @protocol GUID of the protocol to be removed
1111 * @protocol_interface interface to be removed
1112 * @return status code
1114 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
1115 efi_handle_t handle, const efi_guid_t *protocol,
1116 void *protocol_interface)
1118 struct efi_object *efiobj;
1119 struct efi_handler *handler;
1120 struct efi_open_protocol_info_item *item;
1121 struct efi_open_protocol_info_item *pos;
1124 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1127 efiobj = efi_search_obj(handle);
1129 r = EFI_INVALID_PARAMETER;
1132 /* Find the protocol on the handle */
1133 r = efi_search_protocol(handle, protocol, &handler);
1134 if (r != EFI_SUCCESS)
1136 /* Disconnect controllers */
1137 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1138 if (!list_empty(&handler->open_infos)) {
1139 r = EFI_ACCESS_DENIED;
1142 /* Close protocol */
1143 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1144 if (item->info.attributes ==
1145 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1146 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1147 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1148 list_del(&item->link);
1150 if (!list_empty(&handler->open_infos)) {
1151 r = EFI_ACCESS_DENIED;
1154 r = efi_remove_protocol(handle, protocol, protocol_interface);
1160 * Register an event for notification when a protocol is installed.
1162 * This function implements the RegisterProtocolNotify service.
1163 * See the Unified Extensible Firmware Interface (UEFI) specification
1166 * @protocol GUID of the protocol whose installation shall be
1168 * @event event to be signaled upon installation of the protocol
1169 * @registration key for retrieving the registration information
1170 * @return status code
1172 static efi_status_t EFIAPI efi_register_protocol_notify(
1173 const efi_guid_t *protocol,
1174 struct efi_event *event,
1175 void **registration)
1177 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1178 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1182 * Determine if an EFI handle implements a protocol.
1184 * See the documentation of the LocateHandle service in the UEFI specification.
1186 * @search_type selection criterion
1187 * @protocol GUID of the protocol
1188 * @search_key registration key
1190 * @return 0 if the handle implements the protocol
1192 static int efi_search(enum efi_locate_search_type search_type,
1193 const efi_guid_t *protocol, void *search_key,
1194 struct efi_object *efiobj)
1198 switch (search_type) {
1201 case BY_REGISTER_NOTIFY:
1202 /* TODO: RegisterProtocolNotify is not implemented yet */
1205 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1206 return (ret != EFI_SUCCESS);
1208 /* Invalid search type */
1214 * Locate handles implementing a protocol.
1216 * This function is meant for U-Boot internal calls. For the API implementation
1217 * of the LocateHandle service see efi_locate_handle_ext.
1219 * @search_type selection criterion
1220 * @protocol GUID of the protocol
1221 * @search_key registration key
1222 * @buffer_size size of the buffer to receive the handles in bytes
1223 * @buffer buffer to receive the relevant handles
1224 * @return status code
1226 static efi_status_t efi_locate_handle(
1227 enum efi_locate_search_type search_type,
1228 const efi_guid_t *protocol, void *search_key,
1229 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1231 struct efi_object *efiobj;
1232 efi_uintn_t size = 0;
1234 /* Check parameters */
1235 switch (search_type) {
1238 case BY_REGISTER_NOTIFY:
1240 return EFI_INVALID_PARAMETER;
1241 /* RegisterProtocolNotify is not implemented yet */
1242 return EFI_UNSUPPORTED;
1245 return EFI_INVALID_PARAMETER;
1248 return EFI_INVALID_PARAMETER;
1252 * efi_locate_handle_buffer uses this function for
1253 * the calculation of the necessary buffer size.
1254 * So do not require a buffer for buffersize == 0.
1256 if (!buffer_size || (*buffer_size && !buffer))
1257 return EFI_INVALID_PARAMETER;
1259 /* Count how much space we need */
1260 list_for_each_entry(efiobj, &efi_obj_list, link) {
1261 if (!efi_search(search_type, protocol, search_key, efiobj))
1262 size += sizeof(void*);
1265 if (*buffer_size < size) {
1266 *buffer_size = size;
1267 return EFI_BUFFER_TOO_SMALL;
1270 *buffer_size = size;
1272 return EFI_NOT_FOUND;
1274 /* Then fill the array */
1275 list_for_each_entry(efiobj, &efi_obj_list, link) {
1276 if (!efi_search(search_type, protocol, search_key, efiobj))
1277 *buffer++ = efiobj->handle;
1284 * Locate handles implementing a protocol.
1286 * This function implements the LocateHandle service.
1287 * See the Unified Extensible Firmware Interface (UEFI) specification
1290 * @search_type selection criterion
1291 * @protocol GUID of the protocol
1292 * @search_key registration key
1293 * @buffer_size size of the buffer to receive the handles in bytes
1294 * @buffer buffer to receive the relevant handles
1295 * @return 0 if the handle implements the protocol
1297 static efi_status_t EFIAPI efi_locate_handle_ext(
1298 enum efi_locate_search_type search_type,
1299 const efi_guid_t *protocol, void *search_key,
1300 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1302 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1303 buffer_size, buffer);
1305 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1306 buffer_size, buffer));
1309 /* Collapses configuration table entries, removing index i */
1310 static void efi_remove_configuration_table(int i)
1312 struct efi_configuration_table *this = &efi_conf_table[i];
1313 struct efi_configuration_table *next = &efi_conf_table[i+1];
1314 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1316 memmove(this, next, (ulong)end - (ulong)next);
1321 * Adds, updates, or removes a configuration table.
1323 * This function is used for internal calls. For the API implementation of the
1324 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1326 * @guid GUID of the installed table
1327 * @table table to be installed
1328 * @return status code
1330 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
1334 /* Check for guid override */
1335 for (i = 0; i < systab.nr_tables; i++) {
1336 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1338 efi_conf_table[i].table = table;
1340 efi_remove_configuration_table(i);
1346 return EFI_NOT_FOUND;
1348 /* No override, check for overflow */
1349 if (i >= ARRAY_SIZE(efi_conf_table))
1350 return EFI_OUT_OF_RESOURCES;
1352 /* Add a new entry */
1353 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1354 efi_conf_table[i].table = table;
1355 systab.nr_tables = i + 1;
1361 * Adds, updates, or removes a configuration table.
1363 * This function implements the InstallConfigurationTable service.
1364 * See the Unified Extensible Firmware Interface (UEFI) specification
1367 * @guid GUID of the installed table
1368 * @table table to be installed
1369 * @return status code
1371 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1374 EFI_ENTRY("%pUl, %p", guid, table);
1375 return EFI_EXIT(efi_install_configuration_table(guid, table));
1379 * Initialize a loaded_image_info + loaded_image_info object with correct
1380 * protocols, boot-device, etc.
1382 * @info loaded image info to be passed to the entry point of the
1384 * @obj internal object associated with the loaded image
1385 * @device_path device path of the loaded image
1386 * @file_path file path of the loaded image
1387 * @return status code
1389 efi_status_t efi_setup_loaded_image(
1390 struct efi_loaded_image *info, struct efi_object *obj,
1391 struct efi_device_path *device_path,
1392 struct efi_device_path *file_path)
1396 /* Add internal object to object list */
1397 efi_add_handle(obj);
1398 /* efi_exit() assumes that the handle points to the info */
1401 info->file_path = file_path;
1404 info->device_handle = efi_dp_find_obj(device_path, NULL);
1406 * When asking for the device path interface, return
1407 * bootefi_device_path
1409 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1411 if (ret != EFI_SUCCESS)
1416 * When asking for the loaded_image interface, just
1417 * return handle which points to loaded_image_info
1419 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1420 if (ret != EFI_SUCCESS)
1423 ret = efi_add_protocol(obj->handle,
1424 &efi_guid_device_path_to_text_protocol,
1425 (void *)&efi_device_path_to_text);
1426 if (ret != EFI_SUCCESS)
1431 printf("ERROR: Failure to install protocols for loaded image\n");
1436 * Load an image using a file path.
1438 * @file_path the path of the image to load
1439 * @buffer buffer containing the loaded image
1440 * @return status code
1442 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1445 struct efi_file_info *info = NULL;
1446 struct efi_file_handle *f;
1447 static efi_status_t ret;
1450 f = efi_file_from_path(file_path);
1452 return EFI_DEVICE_ERROR;
1455 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1457 if (ret == EFI_BUFFER_TOO_SMALL) {
1459 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1462 if (ret != EFI_SUCCESS)
1465 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1469 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1473 EFI_CALL(f->close(f));
1475 if (ret != EFI_SUCCESS) {
1476 efi_free_pool(*buffer);
1484 * Load an EFI image into memory.
1486 * This function implements the LoadImage service.
1487 * See the Unified Extensible Firmware Interface (UEFI) specification
1490 * @boot_policy true for request originating from the boot manager
1491 * @parent_image the caller's image handle
1492 * @file_path the path of the image to load
1493 * @source_buffer memory location from which the image is installed
1494 * @source_size size of the memory area from which the image is
1496 * @image_handle handle for the newly installed image
1497 * @return status code
1499 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1500 efi_handle_t parent_image,
1501 struct efi_device_path *file_path,
1502 void *source_buffer,
1503 unsigned long source_size,
1504 efi_handle_t *image_handle)
1506 struct efi_loaded_image *info;
1507 struct efi_object *obj;
1510 EFI_ENTRY("%d, %p, %pD, %p, %ld, %p", boot_policy, parent_image,
1511 file_path, source_buffer, source_size, image_handle);
1513 if (!image_handle || !parent_image) {
1514 ret = EFI_INVALID_PARAMETER;
1518 if (!source_buffer && !file_path) {
1519 ret = EFI_NOT_FOUND;
1523 info = calloc(1, sizeof(*info));
1525 ret = EFI_OUT_OF_RESOURCES;
1528 obj = calloc(1, sizeof(*obj));
1531 ret = EFI_OUT_OF_RESOURCES;
1535 if (!source_buffer) {
1536 struct efi_device_path *dp, *fp;
1538 ret = efi_load_image_from_path(file_path, &source_buffer);
1539 if (ret != EFI_SUCCESS)
1542 * split file_path which contains both the device and
1545 efi_dp_split_file_path(file_path, &dp, &fp);
1546 ret = efi_setup_loaded_image(info, obj, dp, fp);
1547 if (ret != EFI_SUCCESS)
1550 /* In this case, file_path is the "device" path, ie.
1551 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1553 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1554 if (ret != EFI_SUCCESS)
1557 info->reserved = efi_load_pe(source_buffer, info);
1558 if (!info->reserved) {
1559 ret = EFI_UNSUPPORTED;
1562 info->system_table = &systab;
1563 info->parent_handle = parent_image;
1564 *image_handle = obj->handle;
1565 return EFI_EXIT(EFI_SUCCESS);
1568 efi_delete_handle(obj);
1570 return EFI_EXIT(ret);
1574 * Call the entry point of an image.
1576 * This function implements the StartImage service.
1577 * See the Unified Extensible Firmware Interface (UEFI) specification
1580 * @image_handle handle of the image
1581 * @exit_data_size size of the buffer
1582 * @exit_data buffer to receive the exit data of the called image
1583 * @return status code
1585 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1586 unsigned long *exit_data_size,
1589 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1590 struct efi_system_table *st);
1591 struct efi_loaded_image *info = image_handle;
1594 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1595 entry = info->reserved;
1597 efi_is_direct_boot = false;
1599 /* call the image! */
1600 if (setjmp(&info->exit_jmp)) {
1602 * We called the entry point of the child image with EFI_CALL
1603 * in the lines below. The child image called the Exit() boot
1604 * service efi_exit() which executed the long jump that brought
1605 * us to the current line. This implies that the second half
1606 * of the EFI_CALL macro has not been executed.
1610 * efi_exit() called efi_restore_gd(). We have to undo this
1611 * otherwise __efi_entry_check() will put the wrong value into
1617 * To get ready to call EFI_EXIT below we have to execute the
1618 * missed out steps of EFI_CALL.
1620 assert(__efi_entry_check());
1621 debug("%sEFI: %lu returned by started image\n",
1622 __efi_nesting_dec(),
1623 (unsigned long)((uintptr_t)info->exit_status &
1625 return EFI_EXIT(info->exit_status);
1628 ret = EFI_CALL(entry(image_handle, &systab));
1631 * Usually UEFI applications call Exit() instead of returning.
1632 * But because the world doesn not consist of ponies and unicorns,
1633 * we're happy to emulate that behavior on behalf of a payload
1636 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
1640 * Leave an EFI application or driver.
1642 * This function implements the Exit service.
1643 * See the Unified Extensible Firmware Interface (UEFI) specification
1646 * @image_handle handle of the application or driver that is exiting
1647 * @exit_status status code
1648 * @exit_data_size size of the buffer in bytes
1649 * @exit_data buffer with data describing an error
1650 * @return status code
1652 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1653 efi_status_t exit_status, unsigned long exit_data_size,
1657 * We require that the handle points to the original loaded
1658 * image protocol interface.
1660 * For getting the longjmp address this is safer than locating
1661 * the protocol because the protocol may have been reinstalled
1662 * pointing to another memory location.
1664 * TODO: We should call the unload procedure of the loaded
1667 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1669 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1670 exit_data_size, exit_data);
1672 /* Make sure entry/exit counts for EFI world cross-overs match */
1673 EFI_EXIT(exit_status);
1676 * But longjmp out with the U-Boot gd, not the application's, as
1677 * the other end is a setjmp call inside EFI context.
1681 loaded_image_info->exit_status = exit_status;
1682 longjmp(&loaded_image_info->exit_jmp, 1);
1684 panic("EFI application exited");
1688 * Unload an EFI image.
1690 * This function implements the UnloadImage service.
1691 * See the Unified Extensible Firmware Interface (UEFI) specification
1694 * @image_handle handle of the image to be unloaded
1695 * @return status code
1697 static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1699 struct efi_object *efiobj;
1701 EFI_ENTRY("%p", image_handle);
1702 efiobj = efi_search_obj(image_handle);
1704 list_del(&efiobj->link);
1706 return EFI_EXIT(EFI_SUCCESS);
1710 * Fix up caches for EFI payloads if necessary.
1712 static void efi_exit_caches(void)
1714 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1716 * Grub on 32bit ARM needs to have caches disabled before jumping into
1717 * a zImage, but does not know of all cache layers. Give it a hand.
1719 if (efi_is_direct_boot)
1720 cleanup_before_linux();
1725 * Stop all boot services.
1727 * This function implements the ExitBootServices service.
1728 * See the Unified Extensible Firmware Interface (UEFI) specification
1731 * All timer events are disabled.
1732 * For exit boot services events the notification function is called.
1733 * The boot services are disabled in the system table.
1735 * @image_handle handle of the loaded image
1736 * @map_key key of the memory map
1737 * @return status code
1739 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1740 unsigned long map_key)
1744 EFI_ENTRY("%p, %ld", image_handle, map_key);
1746 /* Make sure that notification functions are not called anymore */
1747 efi_tpl = TPL_HIGH_LEVEL;
1749 /* Check if ExitBootServices has already been called */
1750 if (!systab.boottime)
1751 return EFI_EXIT(EFI_SUCCESS);
1753 /* Notify that ExitBootServices is invoked. */
1754 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1755 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1757 efi_events[i].is_signaled = true;
1758 efi_signal_event(&efi_events[i], false);
1761 /* TODO Should persist EFI variables here */
1763 board_quiesce_devices();
1765 /* Fix up caches for EFI payloads if necessary */
1768 /* This stops all lingering devices */
1769 bootm_disable_interrupts();
1771 /* Disable boottime services */
1772 systab.con_in_handle = NULL;
1773 systab.con_in = NULL;
1774 systab.con_out_handle = NULL;
1775 systab.con_out = NULL;
1776 systab.stderr_handle = NULL;
1777 systab.std_err = NULL;
1778 systab.boottime = NULL;
1780 /* Recalculate CRC32 */
1781 systab.hdr.crc32 = 0;
1782 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1783 sizeof(struct efi_system_table));
1785 /* Give the payload some time to boot */
1786 efi_set_watchdog(0);
1789 return EFI_EXIT(EFI_SUCCESS);
1793 * Get next value of the counter.
1795 * This function implements the NextMonotonicCount service.
1796 * See the Unified Extensible Firmware Interface (UEFI) specification
1799 * @count returned value of the counter
1800 * @return status code
1802 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1804 static uint64_t mono = 0;
1805 EFI_ENTRY("%p", count);
1807 return EFI_EXIT(EFI_SUCCESS);
1813 * This function implements the Stall sercive.
1814 * See the Unified Extensible Firmware Interface (UEFI) specification
1817 * @microseconds period to sleep in microseconds
1818 * @return status code
1820 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1822 EFI_ENTRY("%ld", microseconds);
1823 udelay(microseconds);
1824 return EFI_EXIT(EFI_SUCCESS);
1828 * Reset the watchdog timer.
1830 * This function implements the SetWatchdogTimer service.
1831 * See the Unified Extensible Firmware Interface (UEFI) specification
1834 * @timeout seconds before reset by watchdog
1835 * @watchdog_code code to be logged when resetting
1836 * @data_size size of buffer in bytes
1837 * @watchdog_data buffer with data describing the reset reason
1838 * @return status code
1840 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1841 uint64_t watchdog_code,
1842 unsigned long data_size,
1843 uint16_t *watchdog_data)
1845 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1846 data_size, watchdog_data);
1847 return EFI_EXIT(efi_set_watchdog(timeout));
1853 * This function implements the CloseProtocol service.
1854 * See the Unified Extensible Firmware Interface (UEFI) specification
1857 * @handle handle on which the protocol shall be closed
1858 * @protocol GUID of the protocol to close
1859 * @agent_handle handle of the driver
1860 * @controller_handle handle of the controller
1861 * @return status code
1863 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1864 const efi_guid_t *protocol,
1865 efi_handle_t agent_handle,
1866 efi_handle_t controller_handle)
1868 struct efi_handler *handler;
1869 struct efi_open_protocol_info_item *item;
1870 struct efi_open_protocol_info_item *pos;
1873 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1876 if (!agent_handle) {
1877 r = EFI_INVALID_PARAMETER;
1880 r = efi_search_protocol(handle, protocol, &handler);
1881 if (r != EFI_SUCCESS)
1885 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1886 if (item->info.agent_handle == agent_handle &&
1887 item->info.controller_handle == controller_handle) {
1888 efi_delete_open_info(item);
1898 * Provide information about then open status of a protocol on a handle
1900 * This function implements the OpenProtocolInformation service.
1901 * See the Unified Extensible Firmware Interface (UEFI) specification
1904 * @handle handle for which the information shall be retrieved
1905 * @protocol GUID of the protocol
1906 * @entry_buffer buffer to receive the open protocol information
1907 * @entry_count number of entries available in the buffer
1908 * @return status code
1910 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1911 const efi_guid_t *protocol,
1912 struct efi_open_protocol_info_entry **entry_buffer,
1913 efi_uintn_t *entry_count)
1915 unsigned long buffer_size;
1916 unsigned long count;
1917 struct efi_handler *handler;
1918 struct efi_open_protocol_info_item *item;
1921 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1924 /* Check parameters */
1925 if (!entry_buffer) {
1926 r = EFI_INVALID_PARAMETER;
1929 r = efi_search_protocol(handle, protocol, &handler);
1930 if (r != EFI_SUCCESS)
1935 list_for_each_entry(item, &handler->open_infos, link) {
1936 if (item->info.open_count)
1939 *entry_count = count;
1940 *entry_buffer = NULL;
1947 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
1948 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1949 (void **)entry_buffer);
1950 if (r != EFI_SUCCESS)
1952 list_for_each_entry_reverse(item, &handler->open_infos, link) {
1953 if (item->info.open_count)
1954 (*entry_buffer)[--count] = item->info;
1961 * Get protocols installed on a handle.
1963 * This function implements the ProtocolsPerHandleService.
1964 * See the Unified Extensible Firmware Interface (UEFI) specification
1967 * @handle handle for which the information is retrieved
1968 * @protocol_buffer buffer with protocol GUIDs
1969 * @protocol_buffer_count number of entries in the buffer
1970 * @return status code
1972 static efi_status_t EFIAPI efi_protocols_per_handle(
1973 efi_handle_t handle, efi_guid_t ***protocol_buffer,
1974 efi_uintn_t *protocol_buffer_count)
1976 unsigned long buffer_size;
1977 struct efi_object *efiobj;
1978 struct list_head *protocol_handle;
1981 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1982 protocol_buffer_count);
1984 if (!handle || !protocol_buffer || !protocol_buffer_count)
1985 return EFI_EXIT(EFI_INVALID_PARAMETER);
1987 *protocol_buffer = NULL;
1988 *protocol_buffer_count = 0;
1990 efiobj = efi_search_obj(handle);
1992 return EFI_EXIT(EFI_INVALID_PARAMETER);
1994 /* Count protocols */
1995 list_for_each(protocol_handle, &efiobj->protocols) {
1996 ++*protocol_buffer_count;
2000 if (*protocol_buffer_count) {
2003 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2004 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2005 (void **)protocol_buffer);
2006 if (r != EFI_SUCCESS)
2008 list_for_each(protocol_handle, &efiobj->protocols) {
2009 struct efi_handler *protocol;
2011 protocol = list_entry(protocol_handle,
2012 struct efi_handler, link);
2013 (*protocol_buffer)[j] = (void *)protocol->guid;
2018 return EFI_EXIT(EFI_SUCCESS);
2022 * Locate handles implementing a protocol.
2024 * This function implements the LocateHandleBuffer service.
2025 * See the Unified Extensible Firmware Interface (UEFI) specification
2028 * @search_type selection criterion
2029 * @protocol GUID of the protocol
2030 * @search_key registration key
2031 * @no_handles number of returned handles
2032 * @buffer buffer with the returned handles
2033 * @return status code
2035 static efi_status_t EFIAPI efi_locate_handle_buffer(
2036 enum efi_locate_search_type search_type,
2037 const efi_guid_t *protocol, void *search_key,
2038 efi_uintn_t *no_handles, efi_handle_t **buffer)
2041 efi_uintn_t buffer_size = 0;
2043 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2044 no_handles, buffer);
2046 if (!no_handles || !buffer) {
2047 r = EFI_INVALID_PARAMETER;
2052 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2054 if (r != EFI_BUFFER_TOO_SMALL)
2056 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2058 if (r != EFI_SUCCESS)
2060 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2062 if (r == EFI_SUCCESS)
2063 *no_handles = buffer_size / sizeof(efi_handle_t);
2069 * Find an interface implementing a protocol.
2071 * This function implements the LocateProtocol service.
2072 * See the Unified Extensible Firmware Interface (UEFI) specification
2075 * @protocol GUID of the protocol
2076 * @registration registration key passed to the notification function
2077 * @protocol_interface interface implementing the protocol
2078 * @return status code
2080 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2082 void **protocol_interface)
2084 struct list_head *lhandle;
2087 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2089 if (!protocol || !protocol_interface)
2090 return EFI_EXIT(EFI_INVALID_PARAMETER);
2092 list_for_each(lhandle, &efi_obj_list) {
2093 struct efi_object *efiobj;
2094 struct efi_handler *handler;
2096 efiobj = list_entry(lhandle, struct efi_object, link);
2098 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2099 if (ret == EFI_SUCCESS) {
2100 *protocol_interface = handler->protocol_interface;
2101 return EFI_EXIT(EFI_SUCCESS);
2104 *protocol_interface = NULL;
2106 return EFI_EXIT(EFI_NOT_FOUND);
2110 * Get the device path and handle of an device implementing a protocol.
2112 * This function implements the LocateDevicePath service.
2113 * See the Unified Extensible Firmware Interface (UEFI) specification
2116 * @protocol GUID of the protocol
2117 * @device_path device path
2118 * @device handle of the device
2119 * @return status code
2121 static efi_status_t EFIAPI efi_locate_device_path(
2122 const efi_guid_t *protocol,
2123 struct efi_device_path **device_path,
2124 efi_handle_t *device)
2126 struct efi_device_path *dp;
2128 struct efi_handler *handler;
2129 efi_handle_t *handles;
2131 size_t len_best = 0;
2132 efi_uintn_t no_handles;
2136 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2138 if (!protocol || !device_path || !*device_path || !device) {
2139 ret = EFI_INVALID_PARAMETER;
2143 /* Find end of device path */
2144 len = efi_dp_size(*device_path);
2146 /* Get all handles implementing the protocol */
2147 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2148 &no_handles, &handles));
2149 if (ret != EFI_SUCCESS)
2152 for (i = 0; i < no_handles; ++i) {
2153 /* Find the device path protocol */
2154 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2156 if (ret != EFI_SUCCESS)
2158 dp = (struct efi_device_path *)handler->protocol_interface;
2159 len_dp = efi_dp_size(dp);
2161 * This handle can only be a better fit
2162 * if its device path length is longer than the best fit and
2163 * if its device path length is shorter of equal the searched
2166 if (len_dp <= len_best || len_dp > len)
2168 /* Check if dp is a subpath of device_path */
2169 if (memcmp(*device_path, dp, len_dp))
2171 *device = handles[i];
2175 remainder = (u8 *)*device_path + len_best;
2176 *device_path = (struct efi_device_path *)remainder;
2179 ret = EFI_NOT_FOUND;
2182 return EFI_EXIT(ret);
2186 * Install multiple protocol interfaces.
2188 * This function implements the MultipleProtocolInterfaces service.
2189 * See the Unified Extensible Firmware Interface (UEFI) specification
2192 * @handle handle on which the protocol interfaces shall be installed
2193 * @... NULL terminated argument list with pairs of protocol GUIDS and
2195 * @return status code
2197 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2200 EFI_ENTRY("%p", handle);
2203 const efi_guid_t *protocol;
2204 void *protocol_interface;
2205 efi_status_t r = EFI_SUCCESS;
2209 return EFI_EXIT(EFI_INVALID_PARAMETER);
2211 va_start(argptr, handle);
2213 protocol = va_arg(argptr, efi_guid_t*);
2216 protocol_interface = va_arg(argptr, void*);
2217 r = EFI_CALL(efi_install_protocol_interface(
2219 EFI_NATIVE_INTERFACE,
2220 protocol_interface));
2221 if (r != EFI_SUCCESS)
2226 if (r == EFI_SUCCESS)
2229 /* If an error occurred undo all changes. */
2230 va_start(argptr, handle);
2232 protocol = va_arg(argptr, efi_guid_t*);
2233 protocol_interface = va_arg(argptr, void*);
2234 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2235 protocol_interface));
2243 * Uninstall multiple protocol interfaces.
2245 * This function implements the UninstallMultipleProtocolInterfaces service.
2246 * See the Unified Extensible Firmware Interface (UEFI) specification
2249 * @handle handle from which the protocol interfaces shall be removed
2250 * @... NULL terminated argument list with pairs of protocol GUIDS and
2252 * @return status code
2254 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2257 EFI_ENTRY("%p", handle);
2260 const efi_guid_t *protocol;
2261 void *protocol_interface;
2262 efi_status_t r = EFI_SUCCESS;
2266 return EFI_EXIT(EFI_INVALID_PARAMETER);
2268 va_start(argptr, handle);
2270 protocol = va_arg(argptr, efi_guid_t*);
2273 protocol_interface = va_arg(argptr, void*);
2274 r = EFI_CALL(efi_uninstall_protocol_interface(
2276 protocol_interface));
2277 if (r != EFI_SUCCESS)
2282 if (r == EFI_SUCCESS)
2285 /* If an error occurred undo all changes. */
2286 va_start(argptr, handle);
2288 protocol = va_arg(argptr, efi_guid_t*);
2289 protocol_interface = va_arg(argptr, void*);
2290 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2291 EFI_NATIVE_INTERFACE,
2292 protocol_interface));
2300 * Calculate cyclic redundancy code.
2302 * This function implements the CalculateCrc32 service.
2303 * See the Unified Extensible Firmware Interface (UEFI) specification
2306 * @data buffer with data
2307 * @data_size size of buffer in bytes
2308 * @crc32_p cyclic redundancy code
2309 * @return status code
2311 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2312 unsigned long data_size,
2315 EFI_ENTRY("%p, %ld", data, data_size);
2316 *crc32_p = crc32(0, data, data_size);
2317 return EFI_EXIT(EFI_SUCCESS);
2323 * This function implements the CopyMem service.
2324 * See the Unified Extensible Firmware Interface (UEFI) specification
2327 * @destination destination of the copy operation
2328 * @source source of the copy operation
2329 * @length number of bytes to copy
2331 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2334 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2335 memcpy(destination, source, length);
2336 EFI_EXIT(EFI_SUCCESS);
2340 * Fill memory with a byte value.
2342 * This function implements the SetMem service.
2343 * See the Unified Extensible Firmware Interface (UEFI) specification
2346 * @buffer buffer to fill
2347 * @size size of buffer in bytes
2348 * @value byte to copy to the buffer
2350 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2352 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2353 memset(buffer, value, size);
2354 EFI_EXIT(EFI_SUCCESS);
2358 * Open protocol interface on a handle.
2360 * @handler handler of a protocol
2361 * @protocol_interface interface implementing the protocol
2362 * @agent_handle handle of the driver
2363 * @controller_handle handle of the controller
2364 * @attributes attributes indicating how to open the protocol
2365 * @return status code
2367 static efi_status_t efi_protocol_open(
2368 struct efi_handler *handler,
2369 void **protocol_interface, void *agent_handle,
2370 void *controller_handle, uint32_t attributes)
2372 struct efi_open_protocol_info_item *item;
2373 struct efi_open_protocol_info_entry *match = NULL;
2374 bool opened_by_driver = false;
2375 bool opened_exclusive = false;
2377 /* If there is no agent, only return the interface */
2381 /* For TEST_PROTOCOL ignore interface attribute */
2382 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2383 *protocol_interface = NULL;
2386 * Check if the protocol is already opened by a driver with the same
2387 * attributes or opened exclusively
2389 list_for_each_entry(item, &handler->open_infos, link) {
2390 if (item->info.agent_handle == agent_handle) {
2391 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2392 (item->info.attributes == attributes))
2393 return EFI_ALREADY_STARTED;
2395 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2396 opened_exclusive = true;
2399 /* Only one controller can open the protocol exclusively */
2400 if (opened_exclusive && attributes &
2401 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2402 return EFI_ACCESS_DENIED;
2404 /* Prepare exclusive opening */
2405 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2406 /* Try to disconnect controllers */
2407 list_for_each_entry(item, &handler->open_infos, link) {
2408 if (item->info.attributes ==
2409 EFI_OPEN_PROTOCOL_BY_DRIVER)
2410 EFI_CALL(efi_disconnect_controller(
2411 item->info.controller_handle,
2412 item->info.agent_handle,
2415 opened_by_driver = false;
2416 /* Check if all controllers are disconnected */
2417 list_for_each_entry(item, &handler->open_infos, link) {
2418 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2419 opened_by_driver = true;
2421 /* Only one controller can be conncected */
2422 if (opened_by_driver)
2423 return EFI_ACCESS_DENIED;
2426 /* Find existing entry */
2427 list_for_each_entry(item, &handler->open_infos, link) {
2428 if (item->info.agent_handle == agent_handle &&
2429 item->info.controller_handle == controller_handle)
2430 match = &item->info;
2432 /* None found, create one */
2434 match = efi_create_open_info(handler);
2436 return EFI_OUT_OF_RESOURCES;
2439 match->agent_handle = agent_handle;
2440 match->controller_handle = controller_handle;
2441 match->attributes = attributes;
2442 match->open_count++;
2445 /* For TEST_PROTOCOL ignore interface attribute. */
2446 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2447 *protocol_interface = handler->protocol_interface;
2453 * Open protocol interface on a handle.
2455 * This function implements the OpenProtocol interface.
2456 * See the Unified Extensible Firmware Interface (UEFI) specification
2459 * @handle handle on which the protocol shall be opened
2460 * @protocol GUID of the protocol
2461 * @protocol_interface interface implementing the protocol
2462 * @agent_handle handle of the driver
2463 * @controller_handle handle of the controller
2464 * @attributes attributes indicating how to open the protocol
2465 * @return status code
2467 static efi_status_t EFIAPI efi_open_protocol(
2468 void *handle, const efi_guid_t *protocol,
2469 void **protocol_interface, void *agent_handle,
2470 void *controller_handle, uint32_t attributes)
2472 struct efi_handler *handler;
2473 efi_status_t r = EFI_INVALID_PARAMETER;
2475 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2476 protocol_interface, agent_handle, controller_handle,
2479 if (!handle || !protocol ||
2480 (!protocol_interface && attributes !=
2481 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2485 switch (attributes) {
2486 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2487 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2488 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2490 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2491 if (controller_handle == handle)
2494 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2495 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2496 /* Check that the controller handle is valid */
2497 if (!efi_search_obj(controller_handle))
2500 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2501 /* Check that the agent handle is valid */
2502 if (!efi_search_obj(agent_handle))
2509 r = efi_search_protocol(handle, protocol, &handler);
2510 if (r != EFI_SUCCESS)
2513 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2514 controller_handle, attributes);
2520 * Get interface of a protocol on a handle.
2522 * This function implements the HandleProtocol service.
2523 * See the Unified Extensible Firmware Interface (UEFI) specification
2526 * @handle handle on which the protocol shall be opened
2527 * @protocol GUID of the protocol
2528 * @protocol_interface interface implementing the protocol
2529 * @return status code
2531 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2532 const efi_guid_t *protocol,
2533 void **protocol_interface)
2535 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2536 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2539 static efi_status_t efi_bind_controller(
2540 efi_handle_t controller_handle,
2541 efi_handle_t driver_image_handle,
2542 struct efi_device_path *remain_device_path)
2544 struct efi_driver_binding_protocol *binding_protocol;
2547 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2548 &efi_guid_driver_binding_protocol,
2549 (void **)&binding_protocol,
2550 driver_image_handle, NULL,
2551 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2552 if (r != EFI_SUCCESS)
2554 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2556 remain_device_path));
2557 if (r == EFI_SUCCESS)
2558 r = EFI_CALL(binding_protocol->start(binding_protocol,
2560 remain_device_path));
2561 EFI_CALL(efi_close_protocol(driver_image_handle,
2562 &efi_guid_driver_binding_protocol,
2563 driver_image_handle, NULL));
2567 static efi_status_t efi_connect_single_controller(
2568 efi_handle_t controller_handle,
2569 efi_handle_t *driver_image_handle,
2570 struct efi_device_path *remain_device_path)
2572 efi_handle_t *buffer;
2576 size_t connected = 0;
2578 /* Get buffer with all handles with driver binding protocol */
2579 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2580 &efi_guid_driver_binding_protocol,
2581 NULL, &count, &buffer));
2582 if (r != EFI_SUCCESS)
2585 /* Context Override */
2586 if (driver_image_handle) {
2587 for (; *driver_image_handle; ++driver_image_handle) {
2588 for (i = 0; i < count; ++i) {
2589 if (buffer[i] == *driver_image_handle) {
2591 r = efi_bind_controller(
2593 *driver_image_handle,
2594 remain_device_path);
2596 * For drivers that do not support the
2597 * controller or are already connected
2598 * we receive an error code here.
2600 if (r == EFI_SUCCESS)
2608 * TODO: Some overrides are not yet implemented:
2609 * - Platform Driver Override
2610 * - Driver Family Override Search
2611 * - Bus Specific Driver Override
2614 /* Driver Binding Search */
2615 for (i = 0; i < count; ++i) {
2617 r = efi_bind_controller(controller_handle,
2619 remain_device_path);
2620 if (r == EFI_SUCCESS)
2625 efi_free_pool(buffer);
2627 return EFI_NOT_FOUND;
2632 * Connect a controller to a driver.
2634 * This function implements the ConnectController service.
2635 * See the Unified Extensible Firmware Interface (UEFI) specification
2638 * First all driver binding protocol handles are tried for binding drivers.
2639 * Afterwards all handles that have openened a protocol of the controller
2640 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2642 * @controller_handle handle of the controller
2643 * @driver_image_handle handle of the driver
2644 * @remain_device_path device path of a child controller
2645 * @recursive true to connect all child controllers
2646 * @return status code
2648 static efi_status_t EFIAPI efi_connect_controller(
2649 efi_handle_t controller_handle,
2650 efi_handle_t *driver_image_handle,
2651 struct efi_device_path *remain_device_path,
2655 efi_status_t ret = EFI_NOT_FOUND;
2656 struct efi_object *efiobj;
2658 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2659 remain_device_path, recursive);
2661 efiobj = efi_search_obj(controller_handle);
2663 ret = EFI_INVALID_PARAMETER;
2667 r = efi_connect_single_controller(controller_handle,
2668 driver_image_handle,
2669 remain_device_path);
2670 if (r == EFI_SUCCESS)
2673 struct efi_handler *handler;
2674 struct efi_open_protocol_info_item *item;
2676 list_for_each_entry(handler, &efiobj->protocols, link) {
2677 list_for_each_entry(item, &handler->open_infos, link) {
2678 if (item->info.attributes &
2679 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2680 r = EFI_CALL(efi_connect_controller(
2681 item->info.controller_handle,
2682 driver_image_handle,
2685 if (r == EFI_SUCCESS)
2691 /* Check for child controller specified by end node */
2692 if (ret != EFI_SUCCESS && remain_device_path &&
2693 remain_device_path->type == DEVICE_PATH_TYPE_END)
2696 return EFI_EXIT(ret);
2700 * Get all child controllers associated to a driver.
2701 * The allocated buffer has to be freed with free().
2703 * @efiobj handle of the controller
2704 * @driver_handle handle of the driver
2705 * @number_of_children number of child controllers
2706 * @child_handle_buffer handles of the the child controllers
2708 static efi_status_t efi_get_child_controllers(
2709 struct efi_object *efiobj,
2710 efi_handle_t driver_handle,
2711 efi_uintn_t *number_of_children,
2712 efi_handle_t **child_handle_buffer)
2714 struct efi_handler *handler;
2715 struct efi_open_protocol_info_item *item;
2716 efi_uintn_t count = 0, i;
2719 /* Count all child controller associations */
2720 list_for_each_entry(handler, &efiobj->protocols, link) {
2721 list_for_each_entry(item, &handler->open_infos, link) {
2722 if (item->info.agent_handle == driver_handle &&
2723 item->info.attributes &
2724 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2729 * Create buffer. In case of duplicate child controller assignments
2730 * the buffer will be too large. But that does not harm.
2732 *number_of_children = 0;
2733 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2734 if (!*child_handle_buffer)
2735 return EFI_OUT_OF_RESOURCES;
2736 /* Copy unique child handles */
2737 list_for_each_entry(handler, &efiobj->protocols, link) {
2738 list_for_each_entry(item, &handler->open_infos, link) {
2739 if (item->info.agent_handle == driver_handle &&
2740 item->info.attributes &
2741 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2742 /* Check this is a new child controller */
2744 for (i = 0; i < *number_of_children; ++i) {
2745 if ((*child_handle_buffer)[i] ==
2746 item->info.controller_handle)
2749 /* Copy handle to buffer */
2751 i = (*number_of_children)++;
2752 (*child_handle_buffer)[i] =
2753 item->info.controller_handle;
2762 * Disconnect a controller from a driver.
2764 * This function implements the DisconnectController service.
2765 * See the Unified Extensible Firmware Interface (UEFI) specification
2768 * @controller_handle handle of the controller
2769 * @driver_image_handle handle of the driver
2770 * @child_handle handle of the child to destroy
2771 * @return status code
2773 static efi_status_t EFIAPI efi_disconnect_controller(
2774 efi_handle_t controller_handle,
2775 efi_handle_t driver_image_handle,
2776 efi_handle_t child_handle)
2778 struct efi_driver_binding_protocol *binding_protocol;
2779 efi_handle_t *child_handle_buffer = NULL;
2780 size_t number_of_children = 0;
2782 size_t stop_count = 0;
2783 struct efi_object *efiobj;
2785 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2788 efiobj = efi_search_obj(controller_handle);
2790 r = EFI_INVALID_PARAMETER;
2794 if (child_handle && !efi_search_obj(child_handle)) {
2795 r = EFI_INVALID_PARAMETER;
2799 /* If no driver handle is supplied, disconnect all drivers */
2800 if (!driver_image_handle) {
2801 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2805 /* Create list of child handles */
2807 number_of_children = 1;
2808 child_handle_buffer = &child_handle;
2810 efi_get_child_controllers(efiobj,
2811 driver_image_handle,
2812 &number_of_children,
2813 &child_handle_buffer);
2816 /* Get the driver binding protocol */
2817 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2818 &efi_guid_driver_binding_protocol,
2819 (void **)&binding_protocol,
2820 driver_image_handle, NULL,
2821 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2822 if (r != EFI_SUCCESS)
2824 /* Remove the children */
2825 if (number_of_children) {
2826 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2829 child_handle_buffer));
2830 if (r == EFI_SUCCESS)
2833 /* Remove the driver */
2835 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2838 if (r == EFI_SUCCESS)
2840 EFI_CALL(efi_close_protocol(driver_image_handle,
2841 &efi_guid_driver_binding_protocol,
2842 driver_image_handle, NULL));
2850 free(child_handle_buffer);
2854 static const struct efi_boot_services efi_boot_services = {
2856 .headersize = sizeof(struct efi_table_hdr),
2858 .raise_tpl = efi_raise_tpl,
2859 .restore_tpl = efi_restore_tpl,
2860 .allocate_pages = efi_allocate_pages_ext,
2861 .free_pages = efi_free_pages_ext,
2862 .get_memory_map = efi_get_memory_map_ext,
2863 .allocate_pool = efi_allocate_pool_ext,
2864 .free_pool = efi_free_pool_ext,
2865 .create_event = efi_create_event_ext,
2866 .set_timer = efi_set_timer_ext,
2867 .wait_for_event = efi_wait_for_event,
2868 .signal_event = efi_signal_event_ext,
2869 .close_event = efi_close_event,
2870 .check_event = efi_check_event,
2871 .install_protocol_interface = efi_install_protocol_interface,
2872 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2873 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2874 .handle_protocol = efi_handle_protocol,
2876 .register_protocol_notify = efi_register_protocol_notify,
2877 .locate_handle = efi_locate_handle_ext,
2878 .locate_device_path = efi_locate_device_path,
2879 .install_configuration_table = efi_install_configuration_table_ext,
2880 .load_image = efi_load_image,
2881 .start_image = efi_start_image,
2883 .unload_image = efi_unload_image,
2884 .exit_boot_services = efi_exit_boot_services,
2885 .get_next_monotonic_count = efi_get_next_monotonic_count,
2887 .set_watchdog_timer = efi_set_watchdog_timer,
2888 .connect_controller = efi_connect_controller,
2889 .disconnect_controller = efi_disconnect_controller,
2890 .open_protocol = efi_open_protocol,
2891 .close_protocol = efi_close_protocol,
2892 .open_protocol_information = efi_open_protocol_information,
2893 .protocols_per_handle = efi_protocols_per_handle,
2894 .locate_handle_buffer = efi_locate_handle_buffer,
2895 .locate_protocol = efi_locate_protocol,
2896 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2897 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2898 .calculate_crc32 = efi_calculate_crc32,
2899 .copy_mem = efi_copy_mem,
2900 .set_mem = efi_set_mem,
2901 .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,