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 <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 * Two spaces per indent level, maxing out at 10.. which ought to be
115 * enough for anyone ;-)
117 static const char *indent_string(int level)
119 const char *indent = " ";
120 const int max = strlen(indent);
121 level = min(max, level * 2);
122 return &indent[max - level];
125 const char *__efi_nesting(void)
127 return indent_string(nesting_level);
130 const char *__efi_nesting_inc(void)
132 return indent_string(nesting_level++);
135 const char *__efi_nesting_dec(void)
137 return indent_string(--nesting_level);
141 * Queue an EFI event.
143 * This function queues the notification function of the event for future
146 * The notification function is called if the task priority level of the
147 * event is higher than the current task priority level.
149 * For the SignalEvent service see efi_signal_event_ext.
151 * @event event to signal
153 void efi_signal_event(struct efi_event *event)
155 if (event->notify_function) {
156 event->is_queued = true;
158 if (efi_tpl >= event->notify_tpl)
160 EFI_CALL_VOID(event->notify_function(event,
161 event->notify_context));
163 event->is_queued = false;
167 * Raise the task priority level.
169 * This function implements the RaiseTpl service.
170 * See the Unified Extensible Firmware Interface (UEFI) specification
173 * @new_tpl new value of the task priority level
174 * @return old value of the task priority level
176 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
178 efi_uintn_t old_tpl = efi_tpl;
180 EFI_ENTRY("0x%zx", new_tpl);
182 if (new_tpl < efi_tpl)
183 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
185 if (efi_tpl > TPL_HIGH_LEVEL)
186 efi_tpl = TPL_HIGH_LEVEL;
188 EFI_EXIT(EFI_SUCCESS);
193 * Lower the task priority level.
195 * This function implements the RestoreTpl service.
196 * See the Unified Extensible Firmware Interface (UEFI) specification
199 * @old_tpl value of the task priority level to be restored
201 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
203 EFI_ENTRY("0x%zx", old_tpl);
205 if (old_tpl > efi_tpl)
206 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
208 if (efi_tpl > TPL_HIGH_LEVEL)
209 efi_tpl = TPL_HIGH_LEVEL;
211 EFI_EXIT(EFI_SUCCESS);
215 * Allocate memory pages.
217 * This function implements the AllocatePages service.
218 * See the Unified Extensible Firmware Interface (UEFI) specification
221 * @type type of allocation to be performed
222 * @memory_type usage type of the allocated memory
223 * @pages number of pages to be allocated
224 * @memory allocated memory
225 * @return status code
227 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
233 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
234 r = efi_allocate_pages(type, memory_type, pages, memory);
241 * This function implements the FreePages service.
242 * See the Unified Extensible Firmware Interface (UEFI) specification
245 * @memory start of the memory area to be freed
246 * @pages number of pages to be freed
247 * @return status code
249 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
254 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
255 r = efi_free_pages(memory, pages);
260 * Get map describing memory usage.
262 * This function implements the GetMemoryMap service.
263 * See the Unified Extensible Firmware Interface (UEFI) specification
266 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
267 * on exit the size of the copied memory map
268 * @memory_map buffer to which the memory map is written
269 * @map_key key for the memory map
270 * @descriptor_size size of an individual memory descriptor
271 * @descriptor_version version number of the memory descriptor structure
272 * @return status code
274 static efi_status_t EFIAPI efi_get_memory_map_ext(
275 efi_uintn_t *memory_map_size,
276 struct efi_mem_desc *memory_map,
277 efi_uintn_t *map_key,
278 efi_uintn_t *descriptor_size,
279 uint32_t *descriptor_version)
283 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
284 map_key, descriptor_size, descriptor_version);
285 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
286 descriptor_size, descriptor_version);
291 * Allocate memory from pool.
293 * This function implements the AllocatePool service.
294 * See the Unified Extensible Firmware Interface (UEFI) specification
297 * @pool_type type of the pool from which memory is to be allocated
298 * @size number of bytes to be allocated
299 * @buffer allocated memory
300 * @return status code
302 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
308 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
309 r = efi_allocate_pool(pool_type, size, buffer);
314 * Free memory from pool.
316 * This function implements the FreePool service.
317 * See the Unified Extensible Firmware Interface (UEFI) specification
320 * @buffer start of memory to be freed
321 * @return status code
323 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
327 EFI_ENTRY("%p", buffer);
328 r = efi_free_pool(buffer);
333 * Add a new object to the object list.
335 * The protocols list is initialized.
336 * The object handle is set.
338 * @obj object to be added
340 void efi_add_handle(struct efi_object *obj)
344 INIT_LIST_HEAD(&obj->protocols);
346 list_add_tail(&obj->link, &efi_obj_list);
353 * @return status code
355 efi_status_t efi_create_handle(efi_handle_t *handle)
357 struct efi_object *obj;
360 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
361 sizeof(struct efi_object),
363 if (r != EFI_SUCCESS)
366 *handle = obj->handle;
371 * Find a protocol on a handle.
374 * @protocol_guid GUID of the protocol
375 * @handler reference to the protocol
376 * @return status code
378 efi_status_t efi_search_protocol(const efi_handle_t handle,
379 const efi_guid_t *protocol_guid,
380 struct efi_handler **handler)
382 struct efi_object *efiobj;
383 struct list_head *lhandle;
385 if (!handle || !protocol_guid)
386 return EFI_INVALID_PARAMETER;
387 efiobj = efi_search_obj(handle);
389 return EFI_INVALID_PARAMETER;
390 list_for_each(lhandle, &efiobj->protocols) {
391 struct efi_handler *protocol;
393 protocol = list_entry(lhandle, struct efi_handler, link);
394 if (!guidcmp(protocol->guid, protocol_guid)) {
400 return EFI_NOT_FOUND;
404 * Delete protocol from a handle.
406 * @handle handle from which the protocol shall be deleted
407 * @protocol GUID of the protocol to be deleted
408 * @protocol_interface interface of the protocol implementation
409 * @return status code
411 efi_status_t efi_remove_protocol(const efi_handle_t handle,
412 const efi_guid_t *protocol,
413 void *protocol_interface)
415 struct efi_handler *handler;
418 ret = efi_search_protocol(handle, protocol, &handler);
419 if (ret != EFI_SUCCESS)
421 if (guidcmp(handler->guid, protocol))
422 return EFI_INVALID_PARAMETER;
423 list_del(&handler->link);
429 * Delete all protocols from a handle.
431 * @handle handle from which the protocols shall be deleted
432 * @return status code
434 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
436 struct efi_object *efiobj;
437 struct efi_handler *protocol;
438 struct efi_handler *pos;
440 efiobj = efi_search_obj(handle);
442 return EFI_INVALID_PARAMETER;
443 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
446 ret = efi_remove_protocol(handle, protocol->guid,
447 protocol->protocol_interface);
448 if (ret != EFI_SUCCESS)
457 * @handle handle to delete
459 void efi_delete_handle(struct efi_object *obj)
463 efi_remove_all_protocols(obj->handle);
464 list_del(&obj->link);
469 * Our event capabilities are very limited. Only a small limited
470 * number of events is allowed to coexist.
472 static struct efi_event efi_events[16];
477 * This function is used inside U-Boot code to create an event.
479 * For the API function implementing the CreateEvent service see
480 * efi_create_event_ext.
482 * @type type of the event to create
483 * @notify_tpl task priority level of the event
484 * @notify_function notification function of the event
485 * @notify_context pointer passed to the notification function
486 * @event created event
487 * @return status code
489 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
490 void (EFIAPI *notify_function) (
491 struct efi_event *event,
493 void *notify_context, struct efi_event **event)
498 return EFI_INVALID_PARAMETER;
500 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
501 return EFI_INVALID_PARAMETER;
503 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
504 notify_function == NULL)
505 return EFI_INVALID_PARAMETER;
507 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
508 if (efi_events[i].type)
510 efi_events[i].type = type;
511 efi_events[i].notify_tpl = notify_tpl;
512 efi_events[i].notify_function = notify_function;
513 efi_events[i].notify_context = notify_context;
514 /* Disable timers on bootup */
515 efi_events[i].trigger_next = -1ULL;
516 efi_events[i].is_queued = false;
517 efi_events[i].is_signaled = false;
518 *event = &efi_events[i];
521 return EFI_OUT_OF_RESOURCES;
527 * This function implements the CreateEvent service.
528 * See the Unified Extensible Firmware Interface (UEFI) specification
531 * @type type of the event to create
532 * @notify_tpl task priority level of the event
533 * @notify_function notification function of the event
534 * @notify_context pointer passed to the notification function
535 * @event created event
536 * @return status code
538 static efi_status_t EFIAPI efi_create_event_ext(
539 uint32_t type, efi_uintn_t notify_tpl,
540 void (EFIAPI *notify_function) (
541 struct efi_event *event,
543 void *notify_context, struct efi_event **event)
545 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
547 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
548 notify_context, event));
553 * Check if a timer event has occurred or a queued notification function should
556 * Our timers have to work without interrupts, so we check whenever keyboard
557 * input or disk accesses happen if enough time elapsed for them to fire.
559 void efi_timer_check(void)
562 u64 now = timer_get_us();
564 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
565 if (!efi_events[i].type)
567 if (efi_events[i].is_queued)
568 efi_signal_event(&efi_events[i]);
569 if (!(efi_events[i].type & EVT_TIMER) ||
570 now < efi_events[i].trigger_next)
572 switch (efi_events[i].trigger_type) {
573 case EFI_TIMER_RELATIVE:
574 efi_events[i].trigger_type = EFI_TIMER_STOP;
576 case EFI_TIMER_PERIODIC:
577 efi_events[i].trigger_next +=
578 efi_events[i].trigger_time;
583 efi_events[i].is_signaled = true;
584 efi_signal_event(&efi_events[i]);
590 * Set the trigger time for a timer event or stop the event.
592 * This is the function for internal usage in U-Boot. For the API function
593 * implementing the SetTimer service see efi_set_timer_ext.
595 * @event event for which the timer is set
596 * @type type of the timer
597 * @trigger_time trigger period in multiples of 100ns
598 * @return status code
600 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
601 uint64_t trigger_time)
606 * The parameter defines a multiple of 100ns.
607 * We use multiples of 1000ns. So divide by 10.
609 do_div(trigger_time, 10);
611 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
612 if (event != &efi_events[i])
615 if (!(event->type & EVT_TIMER))
619 event->trigger_next = -1ULL;
621 case EFI_TIMER_PERIODIC:
622 case EFI_TIMER_RELATIVE:
623 event->trigger_next =
624 timer_get_us() + trigger_time;
627 return EFI_INVALID_PARAMETER;
629 event->trigger_type = type;
630 event->trigger_time = trigger_time;
631 event->is_signaled = false;
634 return EFI_INVALID_PARAMETER;
638 * Set the trigger time for a timer event or stop the event.
640 * This function implements the SetTimer service.
641 * See the Unified Extensible Firmware Interface (UEFI) specification
644 * @event event for which the timer is set
645 * @type type of the timer
646 * @trigger_time trigger period in multiples of 100ns
647 * @return status code
649 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
650 enum efi_timer_delay type,
651 uint64_t trigger_time)
653 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
654 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
658 * Wait for events to be signaled.
660 * This function implements the WaitForEvent service.
661 * See the Unified Extensible Firmware Interface (UEFI) specification
664 * @num_events number of events to be waited for
665 * @events events to be waited for
666 * @index index of the event that was signaled
667 * @return status code
669 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
670 struct efi_event **event,
675 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
677 /* Check parameters */
678 if (!num_events || !event)
679 return EFI_EXIT(EFI_INVALID_PARAMETER);
681 if (efi_tpl != TPL_APPLICATION)
682 return EFI_EXIT(EFI_UNSUPPORTED);
683 for (i = 0; i < num_events; ++i) {
684 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
685 if (event[i] == &efi_events[j])
688 return EFI_EXIT(EFI_INVALID_PARAMETER);
690 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
691 return EFI_EXIT(EFI_INVALID_PARAMETER);
692 if (!event[i]->is_signaled)
693 efi_signal_event(event[i]);
696 /* Wait for signal */
698 for (i = 0; i < num_events; ++i) {
699 if (event[i]->is_signaled)
702 /* Allow events to occur. */
708 * Reset the signal which is passed to the caller to allow periodic
711 event[i]->is_signaled = false;
715 return EFI_EXIT(EFI_SUCCESS);
719 * Signal an EFI event.
721 * This function implements the SignalEvent service.
722 * See the Unified Extensible Firmware Interface (UEFI) specification
725 * This functions sets the signaled state of the event and queues the
726 * notification function for execution.
728 * @event event to signal
729 * @return status code
731 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
735 EFI_ENTRY("%p", event);
736 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
737 if (event != &efi_events[i])
739 if (event->is_signaled)
741 event->is_signaled = true;
742 if (event->type & EVT_NOTIFY_SIGNAL)
743 efi_signal_event(event);
746 return EFI_EXIT(EFI_SUCCESS);
750 * Close an EFI event.
752 * This function implements the CloseEvent service.
753 * See the Unified Extensible Firmware Interface (UEFI) specification
756 * @event event to close
757 * @return status code
759 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
763 EFI_ENTRY("%p", event);
764 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
765 if (event == &efi_events[i]) {
767 event->trigger_next = -1ULL;
768 event->is_queued = false;
769 event->is_signaled = false;
770 return EFI_EXIT(EFI_SUCCESS);
773 return EFI_EXIT(EFI_INVALID_PARAMETER);
777 * Check if an event is signaled.
779 * This function implements the CheckEvent service.
780 * See the Unified Extensible Firmware Interface (UEFI) specification
783 * If an event is not signaled yet the notification function is queued.
785 * @event event to check
786 * @return status code
788 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
792 EFI_ENTRY("%p", event);
794 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
795 if (event != &efi_events[i])
797 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
799 if (!event->is_signaled)
800 efi_signal_event(event);
801 if (event->is_signaled)
802 return EFI_EXIT(EFI_SUCCESS);
803 return EFI_EXIT(EFI_NOT_READY);
805 return EFI_EXIT(EFI_INVALID_PARAMETER);
809 * Find the internal EFI object for a handle.
811 * @handle handle to find
814 struct efi_object *efi_search_obj(const efi_handle_t handle)
816 struct efi_object *efiobj;
818 list_for_each_entry(efiobj, &efi_obj_list, link) {
819 if (efiobj->handle == handle)
827 * Create open protocol info entry and add it to a protocol.
829 * @handler handler of a protocol
830 * @return open protocol info entry
832 static struct efi_open_protocol_info_entry *efi_create_open_info(
833 struct efi_handler *handler)
835 struct efi_open_protocol_info_item *item;
837 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
840 /* Append the item to the open protocol info list. */
841 list_add_tail(&item->link, &handler->open_infos);
847 * Remove an open protocol info entry from a protocol.
849 * @handler handler of a protocol
850 * @return status code
852 static efi_status_t efi_delete_open_info(
853 struct efi_open_protocol_info_item *item)
855 list_del(&item->link);
861 * Install new protocol on a handle.
863 * @handle handle on which the protocol shall be installed
864 * @protocol GUID of the protocol to be installed
865 * @protocol_interface interface of the protocol implementation
866 * @return status code
868 efi_status_t efi_add_protocol(const efi_handle_t handle,
869 const efi_guid_t *protocol,
870 void *protocol_interface)
872 struct efi_object *efiobj;
873 struct efi_handler *handler;
876 efiobj = efi_search_obj(handle);
878 return EFI_INVALID_PARAMETER;
879 ret = efi_search_protocol(handle, protocol, NULL);
880 if (ret != EFI_NOT_FOUND)
881 return EFI_INVALID_PARAMETER;
882 handler = calloc(1, sizeof(struct efi_handler));
884 return EFI_OUT_OF_RESOURCES;
885 handler->guid = protocol;
886 handler->protocol_interface = protocol_interface;
887 INIT_LIST_HEAD(&handler->open_infos);
888 list_add_tail(&handler->link, &efiobj->protocols);
889 if (!guidcmp(&efi_guid_device_path, protocol))
890 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
895 * Install protocol interface.
897 * This function implements the InstallProtocolInterface service.
898 * See the Unified Extensible Firmware Interface (UEFI) specification
901 * @handle handle on which the protocol shall be installed
902 * @protocol GUID of the protocol to be installed
903 * @protocol_interface_type type of the interface to be installed,
904 * always EFI_NATIVE_INTERFACE
905 * @protocol_interface interface of the protocol implementation
906 * @return status code
908 static efi_status_t EFIAPI efi_install_protocol_interface(
909 void **handle, const efi_guid_t *protocol,
910 int protocol_interface_type, void *protocol_interface)
914 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
917 if (!handle || !protocol ||
918 protocol_interface_type != EFI_NATIVE_INTERFACE) {
919 r = EFI_INVALID_PARAMETER;
923 /* Create new handle if requested. */
925 r = efi_create_handle(handle);
926 if (r != EFI_SUCCESS)
928 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
931 debug("%sEFI: handle %p\n", indent_string(nesting_level),
934 /* Add new protocol */
935 r = efi_add_protocol(*handle, protocol, protocol_interface);
941 * Reinstall protocol interface.
943 * This function implements the ReinstallProtocolInterface service.
944 * See the Unified Extensible Firmware Interface (UEFI) specification
947 * @handle handle on which the protocol shall be
949 * @protocol GUID of the protocol to be installed
950 * @old_interface interface to be removed
951 * @new_interface interface to be installed
952 * @return status code
954 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
955 efi_handle_t handle, const efi_guid_t *protocol,
956 void *old_interface, void *new_interface)
958 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
960 return EFI_EXIT(EFI_ACCESS_DENIED);
964 * Get all drivers associated to a controller.
965 * The allocated buffer has to be freed with free().
967 * @efiobj handle of the controller
968 * @protocol protocol guid (optional)
969 * @number_of_drivers number of child controllers
970 * @driver_handle_buffer handles of the the drivers
971 * @return status code
973 static efi_status_t efi_get_drivers(struct efi_object *efiobj,
974 const efi_guid_t *protocol,
975 efi_uintn_t *number_of_drivers,
976 efi_handle_t **driver_handle_buffer)
978 struct efi_handler *handler;
979 struct efi_open_protocol_info_item *item;
980 efi_uintn_t count = 0, i;
983 /* Count all driver associations */
984 list_for_each_entry(handler, &efiobj->protocols, link) {
985 if (protocol && guidcmp(handler->guid, protocol))
987 list_for_each_entry(item, &handler->open_infos, link) {
988 if (item->info.attributes &
989 EFI_OPEN_PROTOCOL_BY_DRIVER)
994 * Create buffer. In case of duplicate driver assignments the buffer
995 * will be too large. But that does not harm.
997 *number_of_drivers = 0;
998 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
999 if (!*driver_handle_buffer)
1000 return EFI_OUT_OF_RESOURCES;
1001 /* Collect unique driver handles */
1002 list_for_each_entry(handler, &efiobj->protocols, link) {
1003 if (protocol && guidcmp(handler->guid, protocol))
1005 list_for_each_entry(item, &handler->open_infos, link) {
1006 if (item->info.attributes &
1007 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1008 /* Check this is a new driver */
1010 for (i = 0; i < *number_of_drivers; ++i) {
1011 if ((*driver_handle_buffer)[i] ==
1012 item->info.agent_handle)
1015 /* Copy handle to buffer */
1017 i = (*number_of_drivers)++;
1018 (*driver_handle_buffer)[i] =
1019 item->info.agent_handle;
1028 * Disconnect all drivers from a controller.
1030 * This function implements the DisconnectController service.
1031 * See the Unified Extensible Firmware Interface (UEFI) specification
1034 * @efiobj handle of the controller
1035 * @protocol protocol guid (optional)
1036 * @child_handle handle of the child to destroy
1037 * @return status code
1039 static efi_status_t efi_disconnect_all_drivers(
1040 struct efi_object *efiobj,
1041 const efi_guid_t *protocol,
1042 efi_handle_t child_handle)
1044 efi_uintn_t number_of_drivers;
1045 efi_handle_t *driver_handle_buffer;
1046 efi_status_t r, ret;
1048 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1049 &driver_handle_buffer);
1050 if (ret != EFI_SUCCESS)
1053 ret = EFI_NOT_FOUND;
1054 while (number_of_drivers) {
1055 r = EFI_CALL(efi_disconnect_controller(
1057 driver_handle_buffer[--number_of_drivers],
1059 if (r == EFI_SUCCESS)
1062 free(driver_handle_buffer);
1067 * Uninstall protocol interface.
1069 * This function implements the UninstallProtocolInterface service.
1070 * See the Unified Extensible Firmware Interface (UEFI) specification
1073 * @handle handle from which the protocol shall be removed
1074 * @protocol GUID of the protocol to be removed
1075 * @protocol_interface interface to be removed
1076 * @return status code
1078 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
1079 efi_handle_t handle, const efi_guid_t *protocol,
1080 void *protocol_interface)
1082 struct efi_object *efiobj;
1083 struct efi_handler *handler;
1084 struct efi_open_protocol_info_item *item;
1085 struct efi_open_protocol_info_item *pos;
1088 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1091 efiobj = efi_search_obj(handle);
1093 r = EFI_INVALID_PARAMETER;
1096 /* Find the protocol on the handle */
1097 r = efi_search_protocol(handle, protocol, &handler);
1098 if (r != EFI_SUCCESS)
1100 /* Disconnect controllers */
1101 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1102 if (!list_empty(&handler->open_infos)) {
1103 r = EFI_ACCESS_DENIED;
1106 /* Close protocol */
1107 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1108 if (item->info.attributes ==
1109 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1110 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1111 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1112 list_del(&item->link);
1114 if (!list_empty(&handler->open_infos)) {
1115 r = EFI_ACCESS_DENIED;
1118 r = efi_remove_protocol(handle, protocol, protocol_interface);
1124 * Register an event for notification when a protocol is installed.
1126 * This function implements the RegisterProtocolNotify service.
1127 * See the Unified Extensible Firmware Interface (UEFI) specification
1130 * @protocol GUID of the protocol whose installation shall be
1132 * @event event to be signaled upon installation of the protocol
1133 * @registration key for retrieving the registration information
1134 * @return status code
1136 static efi_status_t EFIAPI efi_register_protocol_notify(
1137 const efi_guid_t *protocol,
1138 struct efi_event *event,
1139 void **registration)
1141 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1142 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1146 * Determine if an EFI handle implements a protocol.
1148 * See the documentation of the LocateHandle service in the UEFI specification.
1150 * @search_type selection criterion
1151 * @protocol GUID of the protocol
1152 * @search_key registration key
1154 * @return 0 if the handle implements the protocol
1156 static int efi_search(enum efi_locate_search_type search_type,
1157 const efi_guid_t *protocol, void *search_key,
1158 struct efi_object *efiobj)
1162 switch (search_type) {
1165 case BY_REGISTER_NOTIFY:
1166 /* TODO: RegisterProtocolNotify is not implemented yet */
1169 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1170 return (ret != EFI_SUCCESS);
1172 /* Invalid search type */
1178 * Locate handles implementing a protocol.
1180 * This function is meant for U-Boot internal calls. For the API implementation
1181 * of the LocateHandle service see efi_locate_handle_ext.
1183 * @search_type selection criterion
1184 * @protocol GUID of the protocol
1185 * @search_key registration key
1186 * @buffer_size size of the buffer to receive the handles in bytes
1187 * @buffer buffer to receive the relevant handles
1188 * @return status code
1190 static efi_status_t efi_locate_handle(
1191 enum efi_locate_search_type search_type,
1192 const efi_guid_t *protocol, void *search_key,
1193 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1195 struct efi_object *efiobj;
1196 efi_uintn_t size = 0;
1198 /* Check parameters */
1199 switch (search_type) {
1202 case BY_REGISTER_NOTIFY:
1204 return EFI_INVALID_PARAMETER;
1205 /* RegisterProtocolNotify is not implemented yet */
1206 return EFI_UNSUPPORTED;
1209 return EFI_INVALID_PARAMETER;
1212 return EFI_INVALID_PARAMETER;
1216 * efi_locate_handle_buffer uses this function for
1217 * the calculation of the necessary buffer size.
1218 * So do not require a buffer for buffersize == 0.
1220 if (!buffer_size || (*buffer_size && !buffer))
1221 return EFI_INVALID_PARAMETER;
1223 /* Count how much space we need */
1224 list_for_each_entry(efiobj, &efi_obj_list, link) {
1225 if (!efi_search(search_type, protocol, search_key, efiobj))
1226 size += sizeof(void*);
1229 if (*buffer_size < size) {
1230 *buffer_size = size;
1231 return EFI_BUFFER_TOO_SMALL;
1234 *buffer_size = size;
1236 return EFI_NOT_FOUND;
1238 /* Then fill the array */
1239 list_for_each_entry(efiobj, &efi_obj_list, link) {
1240 if (!efi_search(search_type, protocol, search_key, efiobj))
1241 *buffer++ = efiobj->handle;
1248 * Locate handles implementing a protocol.
1250 * This function implements the LocateHandle service.
1251 * See the Unified Extensible Firmware Interface (UEFI) specification
1254 * @search_type selection criterion
1255 * @protocol GUID of the protocol
1256 * @search_key registration key
1257 * @buffer_size size of the buffer to receive the handles in bytes
1258 * @buffer buffer to receive the relevant handles
1259 * @return 0 if the handle implements the protocol
1261 static efi_status_t EFIAPI efi_locate_handle_ext(
1262 enum efi_locate_search_type search_type,
1263 const efi_guid_t *protocol, void *search_key,
1264 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1266 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1267 buffer_size, buffer);
1269 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1270 buffer_size, buffer));
1273 /* Collapses configuration table entries, removing index i */
1274 static void efi_remove_configuration_table(int i)
1276 struct efi_configuration_table *this = &efi_conf_table[i];
1277 struct efi_configuration_table *next = &efi_conf_table[i+1];
1278 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1280 memmove(this, next, (ulong)end - (ulong)next);
1285 * Adds, updates, or removes a configuration table.
1287 * This function is used for internal calls. For the API implementation of the
1288 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1290 * @guid GUID of the installed table
1291 * @table table to be installed
1292 * @return status code
1294 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
1298 /* Check for guid override */
1299 for (i = 0; i < systab.nr_tables; i++) {
1300 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1302 efi_conf_table[i].table = table;
1304 efi_remove_configuration_table(i);
1310 return EFI_NOT_FOUND;
1312 /* No override, check for overflow */
1313 if (i >= ARRAY_SIZE(efi_conf_table))
1314 return EFI_OUT_OF_RESOURCES;
1316 /* Add a new entry */
1317 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1318 efi_conf_table[i].table = table;
1319 systab.nr_tables = i + 1;
1325 * Adds, updates, or removes a configuration table.
1327 * This function implements the InstallConfigurationTable service.
1328 * See the Unified Extensible Firmware Interface (UEFI) specification
1331 * @guid GUID of the installed table
1332 * @table table to be installed
1333 * @return status code
1335 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1338 EFI_ENTRY("%pUl, %p", guid, table);
1339 return EFI_EXIT(efi_install_configuration_table(guid, table));
1343 * Initialize a loaded_image_info + loaded_image_info object with correct
1344 * protocols, boot-device, etc.
1346 * @info loaded image info to be passed to the entry point of the
1348 * @obj internal object associated with the loaded image
1349 * @device_path device path of the loaded image
1350 * @file_path file path of the loaded image
1351 * @return status code
1353 efi_status_t efi_setup_loaded_image(
1354 struct efi_loaded_image *info, struct efi_object *obj,
1355 struct efi_device_path *device_path,
1356 struct efi_device_path *file_path)
1360 /* Add internal object to object list */
1361 efi_add_handle(obj);
1362 /* efi_exit() assumes that the handle points to the info */
1365 info->file_path = file_path;
1367 info->device_handle = efi_dp_find_obj(device_path, NULL);
1370 * When asking for the device path interface, return
1371 * bootefi_device_path
1373 ret = efi_add_protocol(obj->handle, &efi_guid_device_path, device_path);
1374 if (ret != EFI_SUCCESS)
1378 * When asking for the loaded_image interface, just
1379 * return handle which points to loaded_image_info
1381 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1382 if (ret != EFI_SUCCESS)
1385 ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1386 (void *)&efi_console_control);
1387 if (ret != EFI_SUCCESS)
1390 ret = efi_add_protocol(obj->handle,
1391 &efi_guid_device_path_to_text_protocol,
1392 (void *)&efi_device_path_to_text);
1393 if (ret != EFI_SUCCESS)
1398 printf("ERROR: Failure to install protocols for loaded image\n");
1403 * Load an image using a file path.
1405 * @file_path the path of the image to load
1406 * @buffer buffer containing the loaded image
1407 * @return status code
1409 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1412 struct efi_file_info *info = NULL;
1413 struct efi_file_handle *f;
1414 static efi_status_t ret;
1417 f = efi_file_from_path(file_path);
1419 return EFI_DEVICE_ERROR;
1422 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1424 if (ret == EFI_BUFFER_TOO_SMALL) {
1426 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1429 if (ret != EFI_SUCCESS)
1432 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1436 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1440 EFI_CALL(f->close(f));
1442 if (ret != EFI_SUCCESS) {
1443 efi_free_pool(*buffer);
1451 * Load an EFI image into memory.
1453 * This function implements the LoadImage service.
1454 * See the Unified Extensible Firmware Interface (UEFI) specification
1457 * @boot_policy true for request originating from the boot manager
1458 * @parent_image the calles's image handle
1459 * @file_path the path of the image to load
1460 * @source_buffer memory location from which the image is installed
1461 * @source_size size of the memory area from which the image is
1463 * @image_handle handle for the newly installed image
1464 * @return status code
1466 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1467 efi_handle_t parent_image,
1468 struct efi_device_path *file_path,
1469 void *source_buffer,
1470 unsigned long source_size,
1471 efi_handle_t *image_handle)
1473 struct efi_loaded_image *info;
1474 struct efi_object *obj;
1477 EFI_ENTRY("%d, %p, %pD, %p, %ld, %p", boot_policy, parent_image,
1478 file_path, source_buffer, source_size, image_handle);
1480 info = calloc(1, sizeof(*info));
1481 obj = calloc(1, sizeof(*obj));
1483 if (!source_buffer) {
1484 struct efi_device_path *dp, *fp;
1486 ret = efi_load_image_from_path(file_path, &source_buffer);
1487 if (ret != EFI_SUCCESS)
1490 * split file_path which contains both the device and
1493 efi_dp_split_file_path(file_path, &dp, &fp);
1494 ret = efi_setup_loaded_image(info, obj, dp, fp);
1495 if (ret != EFI_SUCCESS)
1498 /* In this case, file_path is the "device" path, ie.
1499 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1501 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1502 if (ret != EFI_SUCCESS)
1505 info->reserved = efi_load_pe(source_buffer, info);
1506 if (!info->reserved) {
1507 ret = EFI_UNSUPPORTED;
1510 info->system_table = &systab;
1511 info->parent_handle = parent_image;
1512 *image_handle = obj->handle;
1513 return EFI_EXIT(EFI_SUCCESS);
1516 efi_delete_handle(obj);
1517 return EFI_EXIT(ret);
1521 * Call the entry point of an image.
1523 * This function implements the StartImage service.
1524 * See the Unified Extensible Firmware Interface (UEFI) specification
1527 * @image_handle handle of the image
1528 * @exit_data_size size of the buffer
1529 * @exit_data buffer to receive the exit data of the called image
1530 * @return status code
1532 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1533 unsigned long *exit_data_size,
1536 asmlinkage ulong (*entry)(efi_handle_t image_handle,
1537 struct efi_system_table *st);
1538 struct efi_loaded_image *info = image_handle;
1541 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1542 entry = info->reserved;
1544 efi_is_direct_boot = false;
1546 /* call the image! */
1547 if (setjmp(&info->exit_jmp)) {
1549 * We called the entry point of the child image with EFI_CALL
1550 * in the lines below. The child image called the Exit() boot
1551 * service efi_exit() which executed the long jump that brought
1552 * us to the current line. This implies that the second half
1553 * of the EFI_CALL macro has not been executed.
1557 * efi_exit() called efi_restore_gd(). We have to undo this
1558 * otherwise __efi_entry_check() will put the wrong value into
1564 * To get ready to call EFI_EXIT below we have to execute the
1565 * missed out steps of EFI_CALL.
1567 assert(__efi_entry_check());
1568 debug("%sEFI: %lu returned by started image\n",
1569 __efi_nesting_dec(),
1570 (unsigned long)((uintptr_t)info->exit_status &
1572 return EFI_EXIT(info->exit_status);
1575 ret = EFI_CALL(entry(image_handle, &systab));
1577 /* Should usually never get here */
1578 return EFI_EXIT(ret);
1582 * Leave an EFI application or driver.
1584 * This function implements the Exit service.
1585 * See the Unified Extensible Firmware Interface (UEFI) specification
1588 * @image_handle handle of the application or driver that is exiting
1589 * @exit_status status code
1590 * @exit_data_size size of the buffer in bytes
1591 * @exit_data buffer with data describing an error
1592 * @return status code
1594 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1595 efi_status_t exit_status, unsigned long exit_data_size,
1599 * We require that the handle points to the original loaded
1600 * image protocol interface.
1602 * For getting the longjmp address this is safer than locating
1603 * the protocol because the protocol may have been reinstalled
1604 * pointing to another memory location.
1606 * TODO: We should call the unload procedure of the loaded
1609 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1611 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1612 exit_data_size, exit_data);
1614 /* Make sure entry/exit counts for EFI world cross-overs match */
1615 EFI_EXIT(exit_status);
1618 * But longjmp out with the U-Boot gd, not the application's, as
1619 * the other end is a setjmp call inside EFI context.
1623 loaded_image_info->exit_status = exit_status;
1624 longjmp(&loaded_image_info->exit_jmp, 1);
1626 panic("EFI application exited");
1630 * Unload an EFI image.
1632 * This function implements the UnloadImage service.
1633 * See the Unified Extensible Firmware Interface (UEFI) specification
1636 * @image_handle handle of the image to be unloaded
1637 * @return status code
1639 static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1641 struct efi_object *efiobj;
1643 EFI_ENTRY("%p", image_handle);
1644 efiobj = efi_search_obj(image_handle);
1646 list_del(&efiobj->link);
1648 return EFI_EXIT(EFI_SUCCESS);
1652 * Fix up caches for EFI payloads if necessary.
1654 static void efi_exit_caches(void)
1656 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1658 * Grub on 32bit ARM needs to have caches disabled before jumping into
1659 * a zImage, but does not know of all cache layers. Give it a hand.
1661 if (efi_is_direct_boot)
1662 cleanup_before_linux();
1667 * Stop boot services.
1669 * This function implements the ExitBootServices service.
1670 * See the Unified Extensible Firmware Interface (UEFI) specification
1673 * @image_handle handle of the loaded image
1674 * @map_key key of the memory map
1675 * @return status code
1677 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1678 unsigned long map_key)
1682 EFI_ENTRY("%p, %ld", image_handle, map_key);
1684 /* Notify that ExitBootServices is invoked. */
1685 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1686 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1688 efi_signal_event(&efi_events[i]);
1690 /* Make sure that notification functions are not called anymore */
1691 efi_tpl = TPL_HIGH_LEVEL;
1693 /* XXX Should persist EFI variables here */
1695 board_quiesce_devices();
1697 /* Fix up caches for EFI payloads if necessary */
1700 /* This stops all lingering devices */
1701 bootm_disable_interrupts();
1703 /* Give the payload some time to boot */
1704 efi_set_watchdog(0);
1707 return EFI_EXIT(EFI_SUCCESS);
1711 * Get next value of the counter.
1713 * This function implements the NextMonotonicCount service.
1714 * See the Unified Extensible Firmware Interface (UEFI) specification
1717 * @count returned value of the counter
1718 * @return status code
1720 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1722 static uint64_t mono = 0;
1723 EFI_ENTRY("%p", count);
1725 return EFI_EXIT(EFI_SUCCESS);
1731 * This function implements the Stall sercive.
1732 * See the Unified Extensible Firmware Interface (UEFI) specification
1735 * @microseconds period to sleep in microseconds
1736 * @return status code
1738 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1740 EFI_ENTRY("%ld", microseconds);
1741 udelay(microseconds);
1742 return EFI_EXIT(EFI_SUCCESS);
1746 * Reset the watchdog timer.
1748 * This function implements the SetWatchdogTimer service.
1749 * See the Unified Extensible Firmware Interface (UEFI) specification
1752 * @timeout seconds before reset by watchdog
1753 * @watchdog_code code to be logged when resetting
1754 * @data_size size of buffer in bytes
1755 * @watchdog_data buffer with data describing the reset reason
1756 * @return status code
1758 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1759 uint64_t watchdog_code,
1760 unsigned long data_size,
1761 uint16_t *watchdog_data)
1763 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1764 data_size, watchdog_data);
1765 return EFI_EXIT(efi_set_watchdog(timeout));
1771 * This function implements the CloseProtocol service.
1772 * See the Unified Extensible Firmware Interface (UEFI) specification
1775 * @handle handle on which the protocol shall be closed
1776 * @protocol GUID of the protocol to close
1777 * @agent_handle handle of the driver
1778 * @controller_handle handle of the controller
1779 * @return status code
1781 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1782 const efi_guid_t *protocol,
1783 efi_handle_t agent_handle,
1784 efi_handle_t controller_handle)
1786 struct efi_handler *handler;
1787 struct efi_open_protocol_info_item *item;
1788 struct efi_open_protocol_info_item *pos;
1791 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1794 if (!agent_handle) {
1795 r = EFI_INVALID_PARAMETER;
1798 r = efi_search_protocol(handle, protocol, &handler);
1799 if (r != EFI_SUCCESS)
1803 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1804 if (item->info.agent_handle == agent_handle &&
1805 item->info.controller_handle == controller_handle) {
1806 efi_delete_open_info(item);
1816 * Provide information about then open status of a protocol on a handle
1818 * This function implements the OpenProtocolInformation service.
1819 * See the Unified Extensible Firmware Interface (UEFI) specification
1822 * @handle handle for which the information shall be retrieved
1823 * @protocol GUID of the protocol
1824 * @entry_buffer buffer to receive the open protocol information
1825 * @entry_count number of entries available in the buffer
1826 * @return status code
1828 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1829 const efi_guid_t *protocol,
1830 struct efi_open_protocol_info_entry **entry_buffer,
1831 efi_uintn_t *entry_count)
1833 unsigned long buffer_size;
1834 unsigned long count;
1835 struct efi_handler *handler;
1836 struct efi_open_protocol_info_item *item;
1839 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1842 /* Check parameters */
1843 if (!entry_buffer) {
1844 r = EFI_INVALID_PARAMETER;
1847 r = efi_search_protocol(handle, protocol, &handler);
1848 if (r != EFI_SUCCESS)
1853 list_for_each_entry(item, &handler->open_infos, link) {
1854 if (item->info.open_count)
1857 *entry_count = count;
1858 *entry_buffer = NULL;
1865 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
1866 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1867 (void **)entry_buffer);
1868 if (r != EFI_SUCCESS)
1870 list_for_each_entry_reverse(item, &handler->open_infos, link) {
1871 if (item->info.open_count)
1872 (*entry_buffer)[--count] = item->info;
1879 * Get protocols installed on a handle.
1881 * This function implements the ProtocolsPerHandleService.
1882 * See the Unified Extensible Firmware Interface (UEFI) specification
1885 * @handle handle for which the information is retrieved
1886 * @protocol_buffer buffer with protocol GUIDs
1887 * @protocol_buffer_count number of entries in the buffer
1888 * @return status code
1890 static efi_status_t EFIAPI efi_protocols_per_handle(
1891 efi_handle_t handle, efi_guid_t ***protocol_buffer,
1892 efi_uintn_t *protocol_buffer_count)
1894 unsigned long buffer_size;
1895 struct efi_object *efiobj;
1896 struct list_head *protocol_handle;
1899 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1900 protocol_buffer_count);
1902 if (!handle || !protocol_buffer || !protocol_buffer_count)
1903 return EFI_EXIT(EFI_INVALID_PARAMETER);
1905 *protocol_buffer = NULL;
1906 *protocol_buffer_count = 0;
1908 efiobj = efi_search_obj(handle);
1910 return EFI_EXIT(EFI_INVALID_PARAMETER);
1912 /* Count protocols */
1913 list_for_each(protocol_handle, &efiobj->protocols) {
1914 ++*protocol_buffer_count;
1918 if (*protocol_buffer_count) {
1921 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1922 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1923 (void **)protocol_buffer);
1924 if (r != EFI_SUCCESS)
1926 list_for_each(protocol_handle, &efiobj->protocols) {
1927 struct efi_handler *protocol;
1929 protocol = list_entry(protocol_handle,
1930 struct efi_handler, link);
1931 (*protocol_buffer)[j] = (void *)protocol->guid;
1936 return EFI_EXIT(EFI_SUCCESS);
1940 * Locate handles implementing a protocol.
1942 * This function implements the LocateHandleBuffer service.
1943 * See the Unified Extensible Firmware Interface (UEFI) specification
1946 * @search_type selection criterion
1947 * @protocol GUID of the protocol
1948 * @search_key registration key
1949 * @no_handles number of returned handles
1950 * @buffer buffer with the returned handles
1951 * @return status code
1953 static efi_status_t EFIAPI efi_locate_handle_buffer(
1954 enum efi_locate_search_type search_type,
1955 const efi_guid_t *protocol, void *search_key,
1956 efi_uintn_t *no_handles, efi_handle_t **buffer)
1959 efi_uintn_t buffer_size = 0;
1961 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1962 no_handles, buffer);
1964 if (!no_handles || !buffer) {
1965 r = EFI_INVALID_PARAMETER;
1970 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1972 if (r != EFI_BUFFER_TOO_SMALL)
1974 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1976 if (r != EFI_SUCCESS)
1978 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1980 if (r == EFI_SUCCESS)
1981 *no_handles = buffer_size / sizeof(efi_handle_t);
1987 * Find an interface implementing a protocol.
1989 * This function implements the LocateProtocol service.
1990 * See the Unified Extensible Firmware Interface (UEFI) specification
1993 * @protocol GUID of the protocol
1994 * @registration registration key passed to the notification function
1995 * @protocol_interface interface implementing the protocol
1996 * @return status code
1998 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2000 void **protocol_interface)
2002 struct list_head *lhandle;
2005 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2007 if (!protocol || !protocol_interface)
2008 return EFI_EXIT(EFI_INVALID_PARAMETER);
2010 list_for_each(lhandle, &efi_obj_list) {
2011 struct efi_object *efiobj;
2012 struct efi_handler *handler;
2014 efiobj = list_entry(lhandle, struct efi_object, link);
2016 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2017 if (ret == EFI_SUCCESS) {
2018 *protocol_interface = handler->protocol_interface;
2019 return EFI_EXIT(EFI_SUCCESS);
2022 *protocol_interface = NULL;
2024 return EFI_EXIT(EFI_NOT_FOUND);
2028 * Get the device path and handle of an device implementing a protocol.
2030 * This function implements the LocateDevicePath service.
2031 * See the Unified Extensible Firmware Interface (UEFI) specification
2034 * @protocol GUID of the protocol
2035 * @device_path device path
2036 * @device handle of the device
2037 * @return status code
2039 static efi_status_t EFIAPI efi_locate_device_path(
2040 const efi_guid_t *protocol,
2041 struct efi_device_path **device_path,
2042 efi_handle_t *device)
2044 struct efi_device_path *dp;
2046 struct efi_handler *handler;
2047 efi_handle_t *handles;
2049 size_t len_best = 0;
2050 efi_uintn_t no_handles;
2054 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2056 if (!protocol || !device_path || !*device_path || !device) {
2057 ret = EFI_INVALID_PARAMETER;
2061 /* Find end of device path */
2062 len = efi_dp_size(*device_path);
2064 /* Get all handles implementing the protocol */
2065 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2066 &no_handles, &handles));
2067 if (ret != EFI_SUCCESS)
2070 for (i = 0; i < no_handles; ++i) {
2071 /* Find the device path protocol */
2072 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2074 if (ret != EFI_SUCCESS)
2076 dp = (struct efi_device_path *)handler->protocol_interface;
2077 len_dp = efi_dp_size(dp);
2079 * This handle can only be a better fit
2080 * if its device path length is longer than the best fit and
2081 * if its device path length is shorter of equal the searched
2084 if (len_dp <= len_best || len_dp > len)
2086 /* Check if dp is a subpath of device_path */
2087 if (memcmp(*device_path, dp, len_dp))
2089 *device = handles[i];
2093 remainder = (u8 *)*device_path + len_best;
2094 *device_path = (struct efi_device_path *)remainder;
2097 ret = EFI_NOT_FOUND;
2100 return EFI_EXIT(ret);
2104 * Install multiple protocol interfaces.
2106 * This function implements the MultipleProtocolInterfaces service.
2107 * See the Unified Extensible Firmware Interface (UEFI) specification
2110 * @handle handle on which the protocol interfaces shall be installed
2111 * @... NULL terminated argument list with pairs of protocol GUIDS and
2113 * @return status code
2115 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2118 EFI_ENTRY("%p", handle);
2121 const efi_guid_t *protocol;
2122 void *protocol_interface;
2123 efi_status_t r = EFI_SUCCESS;
2127 return EFI_EXIT(EFI_INVALID_PARAMETER);
2129 va_start(argptr, handle);
2131 protocol = va_arg(argptr, efi_guid_t*);
2134 protocol_interface = va_arg(argptr, void*);
2135 r = EFI_CALL(efi_install_protocol_interface(
2137 EFI_NATIVE_INTERFACE,
2138 protocol_interface));
2139 if (r != EFI_SUCCESS)
2144 if (r == EFI_SUCCESS)
2147 /* If an error occurred undo all changes. */
2148 va_start(argptr, handle);
2150 protocol = va_arg(argptr, efi_guid_t*);
2151 protocol_interface = va_arg(argptr, void*);
2152 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2153 protocol_interface));
2161 * Uninstall multiple protocol interfaces.
2163 * This function implements the UninstallMultipleProtocolInterfaces service.
2164 * See the Unified Extensible Firmware Interface (UEFI) specification
2167 * @handle handle from which the protocol interfaces shall be removed
2168 * @... NULL terminated argument list with pairs of protocol GUIDS and
2170 * @return status code
2172 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2175 EFI_ENTRY("%p", handle);
2178 const efi_guid_t *protocol;
2179 void *protocol_interface;
2180 efi_status_t r = EFI_SUCCESS;
2184 return EFI_EXIT(EFI_INVALID_PARAMETER);
2186 va_start(argptr, handle);
2188 protocol = va_arg(argptr, efi_guid_t*);
2191 protocol_interface = va_arg(argptr, void*);
2192 r = EFI_CALL(efi_uninstall_protocol_interface(
2194 protocol_interface));
2195 if (r != EFI_SUCCESS)
2200 if (r == EFI_SUCCESS)
2203 /* If an error occurred undo all changes. */
2204 va_start(argptr, handle);
2206 protocol = va_arg(argptr, efi_guid_t*);
2207 protocol_interface = va_arg(argptr, void*);
2208 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2209 EFI_NATIVE_INTERFACE,
2210 protocol_interface));
2218 * Calculate cyclic redundancy code.
2220 * This function implements the CalculateCrc32 service.
2221 * See the Unified Extensible Firmware Interface (UEFI) specification
2224 * @data buffer with data
2225 * @data_size size of buffer in bytes
2226 * @crc32_p cyclic redundancy code
2227 * @return status code
2229 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2230 unsigned long data_size,
2233 EFI_ENTRY("%p, %ld", data, data_size);
2234 *crc32_p = crc32(0, data, data_size);
2235 return EFI_EXIT(EFI_SUCCESS);
2241 * This function implements the CopyMem service.
2242 * See the Unified Extensible Firmware Interface (UEFI) specification
2245 * @destination destination of the copy operation
2246 * @source source of the copy operation
2247 * @length number of bytes to copy
2249 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2252 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2253 memcpy(destination, source, length);
2254 EFI_EXIT(EFI_SUCCESS);
2258 * Fill memory with a byte value.
2260 * This function implements the SetMem service.
2261 * See the Unified Extensible Firmware Interface (UEFI) specification
2264 * @buffer buffer to fill
2265 * @size size of buffer in bytes
2266 * @value byte to copy to the buffer
2268 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2270 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2271 memset(buffer, value, size);
2272 EFI_EXIT(EFI_SUCCESS);
2276 * Open protocol interface on a handle.
2278 * @handler handler of a protocol
2279 * @protocol_interface interface implementing the protocol
2280 * @agent_handle handle of the driver
2281 * @controller_handle handle of the controller
2282 * @attributes attributes indicating how to open the protocol
2283 * @return status code
2285 static efi_status_t efi_protocol_open(
2286 struct efi_handler *handler,
2287 void **protocol_interface, void *agent_handle,
2288 void *controller_handle, uint32_t attributes)
2290 struct efi_open_protocol_info_item *item;
2291 struct efi_open_protocol_info_entry *match = NULL;
2292 bool opened_by_driver = false;
2293 bool opened_exclusive = false;
2295 /* If there is no agent, only return the interface */
2299 /* For TEST_PROTOCOL ignore interface attribute */
2300 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2301 *protocol_interface = NULL;
2304 * Check if the protocol is already opened by a driver with the same
2305 * attributes or opened exclusively
2307 list_for_each_entry(item, &handler->open_infos, link) {
2308 if (item->info.agent_handle == agent_handle) {
2309 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2310 (item->info.attributes == attributes))
2311 return EFI_ALREADY_STARTED;
2313 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2314 opened_exclusive = true;
2317 /* Only one controller can open the protocol exclusively */
2318 if (opened_exclusive && attributes &
2319 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2320 return EFI_ACCESS_DENIED;
2322 /* Prepare exclusive opening */
2323 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2324 /* Try to disconnect controllers */
2325 list_for_each_entry(item, &handler->open_infos, link) {
2326 if (item->info.attributes ==
2327 EFI_OPEN_PROTOCOL_BY_DRIVER)
2328 EFI_CALL(efi_disconnect_controller(
2329 item->info.controller_handle,
2330 item->info.agent_handle,
2333 opened_by_driver = false;
2334 /* Check if all controllers are disconnected */
2335 list_for_each_entry(item, &handler->open_infos, link) {
2336 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2337 opened_by_driver = true;
2339 /* Only one controller can be conncected */
2340 if (opened_by_driver)
2341 return EFI_ACCESS_DENIED;
2344 /* Find existing entry */
2345 list_for_each_entry(item, &handler->open_infos, link) {
2346 if (item->info.agent_handle == agent_handle &&
2347 item->info.controller_handle == controller_handle)
2348 match = &item->info;
2350 /* None found, create one */
2352 match = efi_create_open_info(handler);
2354 return EFI_OUT_OF_RESOURCES;
2357 match->agent_handle = agent_handle;
2358 match->controller_handle = controller_handle;
2359 match->attributes = attributes;
2360 match->open_count++;
2363 /* For TEST_PROTOCOL ignore interface attribute. */
2364 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2365 *protocol_interface = handler->protocol_interface;
2371 * Open protocol interface on a handle.
2373 * This function implements the OpenProtocol interface.
2374 * See the Unified Extensible Firmware Interface (UEFI) specification
2377 * @handle handle on which the protocol shall be opened
2378 * @protocol GUID of the protocol
2379 * @protocol_interface interface implementing the protocol
2380 * @agent_handle handle of the driver
2381 * @controller_handle handle of the controller
2382 * @attributes attributes indicating how to open the protocol
2383 * @return status code
2385 static efi_status_t EFIAPI efi_open_protocol(
2386 void *handle, const efi_guid_t *protocol,
2387 void **protocol_interface, void *agent_handle,
2388 void *controller_handle, uint32_t attributes)
2390 struct efi_handler *handler;
2391 efi_status_t r = EFI_INVALID_PARAMETER;
2393 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2394 protocol_interface, agent_handle, controller_handle,
2397 if (!handle || !protocol ||
2398 (!protocol_interface && attributes !=
2399 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2403 switch (attributes) {
2404 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2405 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2406 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2408 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2409 if (controller_handle == handle)
2412 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2413 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2414 /* Check that the controller handle is valid */
2415 if (!efi_search_obj(controller_handle))
2418 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2419 /* Check that the agent handle is valid */
2420 if (!efi_search_obj(agent_handle))
2427 r = efi_search_protocol(handle, protocol, &handler);
2428 if (r != EFI_SUCCESS)
2431 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2432 controller_handle, attributes);
2438 * Get interface of a protocol on a handle.
2440 * This function implements the HandleProtocol service.
2441 * See the Unified Extensible Firmware Interface (UEFI) specification
2444 * @handle handle on which the protocol shall be opened
2445 * @protocol GUID of the protocol
2446 * @protocol_interface interface implementing the protocol
2447 * @return status code
2449 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2450 const efi_guid_t *protocol,
2451 void **protocol_interface)
2453 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2454 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2457 static efi_status_t efi_bind_controller(
2458 efi_handle_t controller_handle,
2459 efi_handle_t driver_image_handle,
2460 struct efi_device_path *remain_device_path)
2462 struct efi_driver_binding_protocol *binding_protocol;
2465 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2466 &efi_guid_driver_binding_protocol,
2467 (void **)&binding_protocol,
2468 driver_image_handle, NULL,
2469 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2470 if (r != EFI_SUCCESS)
2472 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2474 remain_device_path));
2475 if (r == EFI_SUCCESS)
2476 r = EFI_CALL(binding_protocol->start(binding_protocol,
2478 remain_device_path));
2479 EFI_CALL(efi_close_protocol(driver_image_handle,
2480 &efi_guid_driver_binding_protocol,
2481 driver_image_handle, NULL));
2485 static efi_status_t efi_connect_single_controller(
2486 efi_handle_t controller_handle,
2487 efi_handle_t *driver_image_handle,
2488 struct efi_device_path *remain_device_path)
2490 efi_handle_t *buffer;
2494 size_t connected = 0;
2496 /* Get buffer with all handles with driver binding protocol */
2497 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2498 &efi_guid_driver_binding_protocol,
2499 NULL, &count, &buffer));
2500 if (r != EFI_SUCCESS)
2503 /* Context Override */
2504 if (driver_image_handle) {
2505 for (; *driver_image_handle; ++driver_image_handle) {
2506 for (i = 0; i < count; ++i) {
2507 if (buffer[i] == *driver_image_handle) {
2509 r = efi_bind_controller(
2511 *driver_image_handle,
2512 remain_device_path);
2514 * For drivers that do not support the
2515 * controller or are already connected
2516 * we receive an error code here.
2518 if (r == EFI_SUCCESS)
2526 * TODO: Some overrides are not yet implemented:
2527 * - Platform Driver Override
2528 * - Driver Family Override Search
2529 * - Bus Specific Driver Override
2532 /* Driver Binding Search */
2533 for (i = 0; i < count; ++i) {
2535 r = efi_bind_controller(controller_handle,
2537 remain_device_path);
2538 if (r == EFI_SUCCESS)
2543 efi_free_pool(buffer);
2545 return EFI_NOT_FOUND;
2550 * Connect a controller to a driver.
2552 * This function implements the ConnectController service.
2553 * See the Unified Extensible Firmware Interface (UEFI) specification
2556 * First all driver binding protocol handles are tried for binding drivers.
2557 * Afterwards all handles that have openened a protocol of the controller
2558 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2560 * @controller_handle handle of the controller
2561 * @driver_image_handle handle of the driver
2562 * @remain_device_path device path of a child controller
2563 * @recursive true to connect all child controllers
2564 * @return status code
2566 static efi_status_t EFIAPI efi_connect_controller(
2567 efi_handle_t controller_handle,
2568 efi_handle_t *driver_image_handle,
2569 struct efi_device_path *remain_device_path,
2573 efi_status_t ret = EFI_NOT_FOUND;
2574 struct efi_object *efiobj;
2576 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2577 remain_device_path, recursive);
2579 efiobj = efi_search_obj(controller_handle);
2581 ret = EFI_INVALID_PARAMETER;
2585 r = efi_connect_single_controller(controller_handle,
2586 driver_image_handle,
2587 remain_device_path);
2588 if (r == EFI_SUCCESS)
2591 struct efi_handler *handler;
2592 struct efi_open_protocol_info_item *item;
2594 list_for_each_entry(handler, &efiobj->protocols, link) {
2595 list_for_each_entry(item, &handler->open_infos, link) {
2596 if (item->info.attributes &
2597 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2598 r = EFI_CALL(efi_connect_controller(
2599 item->info.controller_handle,
2600 driver_image_handle,
2603 if (r == EFI_SUCCESS)
2609 /* Check for child controller specified by end node */
2610 if (ret != EFI_SUCCESS && remain_device_path &&
2611 remain_device_path->type == DEVICE_PATH_TYPE_END)
2614 return EFI_EXIT(ret);
2618 * Get all child controllers associated to a driver.
2619 * The allocated buffer has to be freed with free().
2621 * @efiobj handle of the controller
2622 * @driver_handle handle of the driver
2623 * @number_of_children number of child controllers
2624 * @child_handle_buffer handles of the the child controllers
2626 static efi_status_t efi_get_child_controllers(
2627 struct efi_object *efiobj,
2628 efi_handle_t driver_handle,
2629 efi_uintn_t *number_of_children,
2630 efi_handle_t **child_handle_buffer)
2632 struct efi_handler *handler;
2633 struct efi_open_protocol_info_item *item;
2634 efi_uintn_t count = 0, i;
2637 /* Count all child controller associations */
2638 list_for_each_entry(handler, &efiobj->protocols, link) {
2639 list_for_each_entry(item, &handler->open_infos, link) {
2640 if (item->info.agent_handle == driver_handle &&
2641 item->info.attributes &
2642 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2647 * Create buffer. In case of duplicate child controller assignments
2648 * the buffer will be too large. But that does not harm.
2650 *number_of_children = 0;
2651 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2652 if (!*child_handle_buffer)
2653 return EFI_OUT_OF_RESOURCES;
2654 /* Copy unique child handles */
2655 list_for_each_entry(handler, &efiobj->protocols, link) {
2656 list_for_each_entry(item, &handler->open_infos, link) {
2657 if (item->info.agent_handle == driver_handle &&
2658 item->info.attributes &
2659 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2660 /* Check this is a new child controller */
2662 for (i = 0; i < *number_of_children; ++i) {
2663 if ((*child_handle_buffer)[i] ==
2664 item->info.controller_handle)
2667 /* Copy handle to buffer */
2669 i = (*number_of_children)++;
2670 (*child_handle_buffer)[i] =
2671 item->info.controller_handle;
2680 * Disconnect a controller from a driver.
2682 * This function implements the DisconnectController service.
2683 * See the Unified Extensible Firmware Interface (UEFI) specification
2686 * @controller_handle handle of the controller
2687 * @driver_image_handle handle of the driver
2688 * @child_handle handle of the child to destroy
2689 * @return status code
2691 static efi_status_t EFIAPI efi_disconnect_controller(
2692 efi_handle_t controller_handle,
2693 efi_handle_t driver_image_handle,
2694 efi_handle_t child_handle)
2696 struct efi_driver_binding_protocol *binding_protocol;
2697 efi_handle_t *child_handle_buffer = NULL;
2698 size_t number_of_children = 0;
2700 size_t stop_count = 0;
2701 struct efi_object *efiobj;
2703 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2706 efiobj = efi_search_obj(controller_handle);
2708 r = EFI_INVALID_PARAMETER;
2712 if (child_handle && !efi_search_obj(child_handle)) {
2713 r = EFI_INVALID_PARAMETER;
2717 /* If no driver handle is supplied, disconnect all drivers */
2718 if (!driver_image_handle) {
2719 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2723 /* Create list of child handles */
2725 number_of_children = 1;
2726 child_handle_buffer = &child_handle;
2728 efi_get_child_controllers(efiobj,
2729 driver_image_handle,
2730 &number_of_children,
2731 &child_handle_buffer);
2734 /* Get the driver binding protocol */
2735 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2736 &efi_guid_driver_binding_protocol,
2737 (void **)&binding_protocol,
2738 driver_image_handle, NULL,
2739 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2740 if (r != EFI_SUCCESS)
2742 /* Remove the children */
2743 if (number_of_children) {
2744 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2747 child_handle_buffer));
2748 if (r == EFI_SUCCESS)
2751 /* Remove the driver */
2753 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2756 if (r == EFI_SUCCESS)
2758 EFI_CALL(efi_close_protocol(driver_image_handle,
2759 &efi_guid_driver_binding_protocol,
2760 driver_image_handle, NULL));
2768 free(child_handle_buffer);
2772 static const struct efi_boot_services efi_boot_services = {
2774 .headersize = sizeof(struct efi_table_hdr),
2776 .raise_tpl = efi_raise_tpl,
2777 .restore_tpl = efi_restore_tpl,
2778 .allocate_pages = efi_allocate_pages_ext,
2779 .free_pages = efi_free_pages_ext,
2780 .get_memory_map = efi_get_memory_map_ext,
2781 .allocate_pool = efi_allocate_pool_ext,
2782 .free_pool = efi_free_pool_ext,
2783 .create_event = efi_create_event_ext,
2784 .set_timer = efi_set_timer_ext,
2785 .wait_for_event = efi_wait_for_event,
2786 .signal_event = efi_signal_event_ext,
2787 .close_event = efi_close_event,
2788 .check_event = efi_check_event,
2789 .install_protocol_interface = efi_install_protocol_interface,
2790 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2791 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2792 .handle_protocol = efi_handle_protocol,
2794 .register_protocol_notify = efi_register_protocol_notify,
2795 .locate_handle = efi_locate_handle_ext,
2796 .locate_device_path = efi_locate_device_path,
2797 .install_configuration_table = efi_install_configuration_table_ext,
2798 .load_image = efi_load_image,
2799 .start_image = efi_start_image,
2801 .unload_image = efi_unload_image,
2802 .exit_boot_services = efi_exit_boot_services,
2803 .get_next_monotonic_count = efi_get_next_monotonic_count,
2805 .set_watchdog_timer = efi_set_watchdog_timer,
2806 .connect_controller = efi_connect_controller,
2807 .disconnect_controller = efi_disconnect_controller,
2808 .open_protocol = efi_open_protocol,
2809 .close_protocol = efi_close_protocol,
2810 .open_protocol_information = efi_open_protocol_information,
2811 .protocols_per_handle = efi_protocols_per_handle,
2812 .locate_handle_buffer = efi_locate_handle_buffer,
2813 .locate_protocol = efi_locate_protocol,
2814 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2815 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2816 .calculate_crc32 = efi_calculate_crc32,
2817 .copy_mem = efi_copy_mem,
2818 .set_mem = efi_set_mem,
2822 static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
2824 struct efi_system_table __efi_runtime_data systab = {
2826 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2827 .revision = 0x20005, /* 2.5 */
2828 .headersize = sizeof(struct efi_table_hdr),
2830 .fw_vendor = (long)firmware_vendor,
2831 .con_in = (void*)&efi_con_in,
2832 .con_out = (void*)&efi_con_out,
2833 .std_err = (void*)&efi_con_out,
2834 .runtime = (void*)&efi_runtime_services,
2835 .boottime = (void*)&efi_boot_services,
2837 .tables = (void*)efi_conf_table,