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, %p, %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 ulong (*entry)(efi_handle_t image_handle, struct efi_system_table *st);
1537 struct efi_loaded_image *info = image_handle;
1540 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1541 entry = info->reserved;
1543 efi_is_direct_boot = false;
1545 /* call the image! */
1546 if (setjmp(&info->exit_jmp)) {
1548 * We called the entry point of the child image with EFI_CALL
1549 * in the lines below. The child image called the Exit() boot
1550 * service efi_exit() which executed the long jump that brought
1551 * us to the current line. This implies that the second half
1552 * of the EFI_CALL macro has not been executed.
1556 * efi_exit() called efi_restore_gd(). We have to undo this
1557 * otherwise __efi_entry_check() will put the wrong value into
1563 * To get ready to call EFI_EXIT below we have to execute the
1564 * missed out steps of EFI_CALL.
1566 assert(__efi_entry_check());
1567 debug("%sEFI: %lu returned by started image\n",
1568 __efi_nesting_dec(),
1569 (unsigned long)((uintptr_t)info->exit_status &
1571 return EFI_EXIT(info->exit_status);
1574 ret = EFI_CALL(entry(image_handle, &systab));
1576 /* Should usually never get here */
1577 return EFI_EXIT(ret);
1581 * Leave an EFI application or driver.
1583 * This function implements the Exit service.
1584 * See the Unified Extensible Firmware Interface (UEFI) specification
1587 * @image_handle handle of the application or driver that is exiting
1588 * @exit_status status code
1589 * @exit_data_size size of the buffer in bytes
1590 * @exit_data buffer with data describing an error
1591 * @return status code
1593 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1594 efi_status_t exit_status, unsigned long exit_data_size,
1598 * We require that the handle points to the original loaded
1599 * image protocol interface.
1601 * For getting the longjmp address this is safer than locating
1602 * the protocol because the protocol may have been reinstalled
1603 * pointing to another memory location.
1605 * TODO: We should call the unload procedure of the loaded
1608 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1610 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1611 exit_data_size, exit_data);
1613 /* Make sure entry/exit counts for EFI world cross-overs match */
1614 EFI_EXIT(exit_status);
1617 * But longjmp out with the U-Boot gd, not the application's, as
1618 * the other end is a setjmp call inside EFI context.
1622 loaded_image_info->exit_status = exit_status;
1623 longjmp(&loaded_image_info->exit_jmp, 1);
1625 panic("EFI application exited");
1629 * Unload an EFI image.
1631 * This function implements the UnloadImage service.
1632 * See the Unified Extensible Firmware Interface (UEFI) specification
1635 * @image_handle handle of the image to be unloaded
1636 * @return status code
1638 static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
1640 struct efi_object *efiobj;
1642 EFI_ENTRY("%p", image_handle);
1643 efiobj = efi_search_obj(image_handle);
1645 list_del(&efiobj->link);
1647 return EFI_EXIT(EFI_SUCCESS);
1651 * Fix up caches for EFI payloads if necessary.
1653 static void efi_exit_caches(void)
1655 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1657 * Grub on 32bit ARM needs to have caches disabled before jumping into
1658 * a zImage, but does not know of all cache layers. Give it a hand.
1660 if (efi_is_direct_boot)
1661 cleanup_before_linux();
1666 * Stop boot services.
1668 * This function implements the ExitBootServices service.
1669 * See the Unified Extensible Firmware Interface (UEFI) specification
1672 * @image_handle handle of the loaded image
1673 * @map_key key of the memory map
1674 * @return status code
1676 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1677 unsigned long map_key)
1681 EFI_ENTRY("%p, %ld", image_handle, map_key);
1683 /* Notify that ExitBootServices is invoked. */
1684 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1685 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1687 efi_signal_event(&efi_events[i]);
1689 /* Make sure that notification functions are not called anymore */
1690 efi_tpl = TPL_HIGH_LEVEL;
1692 /* XXX Should persist EFI variables here */
1694 board_quiesce_devices();
1696 /* Fix up caches for EFI payloads if necessary */
1699 /* This stops all lingering devices */
1700 bootm_disable_interrupts();
1702 /* Give the payload some time to boot */
1703 efi_set_watchdog(0);
1706 return EFI_EXIT(EFI_SUCCESS);
1710 * Get next value of the counter.
1712 * This function implements the NextMonotonicCount service.
1713 * See the Unified Extensible Firmware Interface (UEFI) specification
1716 * @count returned value of the counter
1717 * @return status code
1719 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1721 static uint64_t mono = 0;
1722 EFI_ENTRY("%p", count);
1724 return EFI_EXIT(EFI_SUCCESS);
1730 * This function implements the Stall sercive.
1731 * See the Unified Extensible Firmware Interface (UEFI) specification
1734 * @microseconds period to sleep in microseconds
1735 * @return status code
1737 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1739 EFI_ENTRY("%ld", microseconds);
1740 udelay(microseconds);
1741 return EFI_EXIT(EFI_SUCCESS);
1745 * Reset the watchdog timer.
1747 * This function implements the SetWatchdogTimer service.
1748 * See the Unified Extensible Firmware Interface (UEFI) specification
1751 * @timeout seconds before reset by watchdog
1752 * @watchdog_code code to be logged when resetting
1753 * @data_size size of buffer in bytes
1754 * @watchdog_data buffer with data describing the reset reason
1755 * @return status code
1757 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1758 uint64_t watchdog_code,
1759 unsigned long data_size,
1760 uint16_t *watchdog_data)
1762 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1763 data_size, watchdog_data);
1764 return EFI_EXIT(efi_set_watchdog(timeout));
1770 * This function implements the CloseProtocol service.
1771 * See the Unified Extensible Firmware Interface (UEFI) specification
1774 * @handle handle on which the protocol shall be closed
1775 * @protocol GUID of the protocol to close
1776 * @agent_handle handle of the driver
1777 * @controller_handle handle of the controller
1778 * @return status code
1780 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1781 const efi_guid_t *protocol,
1782 efi_handle_t agent_handle,
1783 efi_handle_t controller_handle)
1785 struct efi_handler *handler;
1786 struct efi_open_protocol_info_item *item;
1787 struct efi_open_protocol_info_item *pos;
1790 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1793 if (!agent_handle) {
1794 r = EFI_INVALID_PARAMETER;
1797 r = efi_search_protocol(handle, protocol, &handler);
1798 if (r != EFI_SUCCESS)
1802 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1803 if (item->info.agent_handle == agent_handle &&
1804 item->info.controller_handle == controller_handle) {
1805 efi_delete_open_info(item);
1815 * Provide information about then open status of a protocol on a handle
1817 * This function implements the OpenProtocolInformation service.
1818 * See the Unified Extensible Firmware Interface (UEFI) specification
1821 * @handle handle for which the information shall be retrieved
1822 * @protocol GUID of the protocol
1823 * @entry_buffer buffer to receive the open protocol information
1824 * @entry_count number of entries available in the buffer
1825 * @return status code
1827 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1828 const efi_guid_t *protocol,
1829 struct efi_open_protocol_info_entry **entry_buffer,
1830 efi_uintn_t *entry_count)
1832 unsigned long buffer_size;
1833 unsigned long count;
1834 struct efi_handler *handler;
1835 struct efi_open_protocol_info_item *item;
1838 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1841 /* Check parameters */
1842 if (!entry_buffer) {
1843 r = EFI_INVALID_PARAMETER;
1846 r = efi_search_protocol(handle, protocol, &handler);
1847 if (r != EFI_SUCCESS)
1852 list_for_each_entry(item, &handler->open_infos, link) {
1853 if (item->info.open_count)
1856 *entry_count = count;
1857 *entry_buffer = NULL;
1864 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
1865 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1866 (void **)entry_buffer);
1867 if (r != EFI_SUCCESS)
1869 list_for_each_entry_reverse(item, &handler->open_infos, link) {
1870 if (item->info.open_count)
1871 (*entry_buffer)[--count] = item->info;
1878 * Get protocols installed on a handle.
1880 * This function implements the ProtocolsPerHandleService.
1881 * See the Unified Extensible Firmware Interface (UEFI) specification
1884 * @handle handle for which the information is retrieved
1885 * @protocol_buffer buffer with protocol GUIDs
1886 * @protocol_buffer_count number of entries in the buffer
1887 * @return status code
1889 static efi_status_t EFIAPI efi_protocols_per_handle(
1890 efi_handle_t handle, efi_guid_t ***protocol_buffer,
1891 efi_uintn_t *protocol_buffer_count)
1893 unsigned long buffer_size;
1894 struct efi_object *efiobj;
1895 struct list_head *protocol_handle;
1898 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1899 protocol_buffer_count);
1901 if (!handle || !protocol_buffer || !protocol_buffer_count)
1902 return EFI_EXIT(EFI_INVALID_PARAMETER);
1904 *protocol_buffer = NULL;
1905 *protocol_buffer_count = 0;
1907 efiobj = efi_search_obj(handle);
1909 return EFI_EXIT(EFI_INVALID_PARAMETER);
1911 /* Count protocols */
1912 list_for_each(protocol_handle, &efiobj->protocols) {
1913 ++*protocol_buffer_count;
1917 if (*protocol_buffer_count) {
1920 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1921 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1922 (void **)protocol_buffer);
1923 if (r != EFI_SUCCESS)
1925 list_for_each(protocol_handle, &efiobj->protocols) {
1926 struct efi_handler *protocol;
1928 protocol = list_entry(protocol_handle,
1929 struct efi_handler, link);
1930 (*protocol_buffer)[j] = (void *)protocol->guid;
1935 return EFI_EXIT(EFI_SUCCESS);
1939 * Locate handles implementing a protocol.
1941 * This function implements the LocateHandleBuffer service.
1942 * See the Unified Extensible Firmware Interface (UEFI) specification
1945 * @search_type selection criterion
1946 * @protocol GUID of the protocol
1947 * @search_key registration key
1948 * @no_handles number of returned handles
1949 * @buffer buffer with the returned handles
1950 * @return status code
1952 static efi_status_t EFIAPI efi_locate_handle_buffer(
1953 enum efi_locate_search_type search_type,
1954 const efi_guid_t *protocol, void *search_key,
1955 efi_uintn_t *no_handles, efi_handle_t **buffer)
1958 efi_uintn_t buffer_size = 0;
1960 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1961 no_handles, buffer);
1963 if (!no_handles || !buffer) {
1964 r = EFI_INVALID_PARAMETER;
1969 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1971 if (r != EFI_BUFFER_TOO_SMALL)
1973 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1975 if (r != EFI_SUCCESS)
1977 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1979 if (r == EFI_SUCCESS)
1980 *no_handles = buffer_size / sizeof(efi_handle_t);
1986 * Find an interface implementing a protocol.
1988 * This function implements the LocateProtocol service.
1989 * See the Unified Extensible Firmware Interface (UEFI) specification
1992 * @protocol GUID of the protocol
1993 * @registration registration key passed to the notification function
1994 * @protocol_interface interface implementing the protocol
1995 * @return status code
1997 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
1999 void **protocol_interface)
2001 struct list_head *lhandle;
2004 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2006 if (!protocol || !protocol_interface)
2007 return EFI_EXIT(EFI_INVALID_PARAMETER);
2009 list_for_each(lhandle, &efi_obj_list) {
2010 struct efi_object *efiobj;
2011 struct efi_handler *handler;
2013 efiobj = list_entry(lhandle, struct efi_object, link);
2015 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2016 if (ret == EFI_SUCCESS) {
2017 *protocol_interface = handler->protocol_interface;
2018 return EFI_EXIT(EFI_SUCCESS);
2021 *protocol_interface = NULL;
2023 return EFI_EXIT(EFI_NOT_FOUND);
2027 * Get the device path and handle of an device implementing a protocol.
2029 * This function implements the LocateDevicePath service.
2030 * See the Unified Extensible Firmware Interface (UEFI) specification
2033 * @protocol GUID of the protocol
2034 * @device_path device path
2035 * @device handle of the device
2036 * @return status code
2038 static efi_status_t EFIAPI efi_locate_device_path(
2039 const efi_guid_t *protocol,
2040 struct efi_device_path **device_path,
2041 efi_handle_t *device)
2043 struct efi_device_path *dp;
2045 struct efi_handler *handler;
2046 efi_handle_t *handles;
2048 size_t len_best = 0;
2049 efi_uintn_t no_handles;
2053 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2055 if (!protocol || !device_path || !*device_path || !device) {
2056 ret = EFI_INVALID_PARAMETER;
2060 /* Find end of device path */
2061 len = efi_dp_size(*device_path);
2063 /* Get all handles implementing the protocol */
2064 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2065 &no_handles, &handles));
2066 if (ret != EFI_SUCCESS)
2069 for (i = 0; i < no_handles; ++i) {
2070 /* Find the device path protocol */
2071 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2073 if (ret != EFI_SUCCESS)
2075 dp = (struct efi_device_path *)handler->protocol_interface;
2076 len_dp = efi_dp_size(dp);
2078 * This handle can only be a better fit
2079 * if its device path length is longer than the best fit and
2080 * if its device path length is shorter of equal the searched
2083 if (len_dp <= len_best || len_dp > len)
2085 /* Check if dp is a subpath of device_path */
2086 if (memcmp(*device_path, dp, len_dp))
2088 *device = handles[i];
2092 remainder = (u8 *)*device_path + len_best;
2093 *device_path = (struct efi_device_path *)remainder;
2096 ret = EFI_NOT_FOUND;
2099 return EFI_EXIT(ret);
2103 * Install multiple protocol interfaces.
2105 * This function implements the MultipleProtocolInterfaces service.
2106 * See the Unified Extensible Firmware Interface (UEFI) specification
2109 * @handle handle on which the protocol interfaces shall be installed
2110 * @... NULL terminated argument list with pairs of protocol GUIDS and
2112 * @return status code
2114 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2117 EFI_ENTRY("%p", handle);
2120 const efi_guid_t *protocol;
2121 void *protocol_interface;
2122 efi_status_t r = EFI_SUCCESS;
2126 return EFI_EXIT(EFI_INVALID_PARAMETER);
2128 va_start(argptr, handle);
2130 protocol = va_arg(argptr, efi_guid_t*);
2133 protocol_interface = va_arg(argptr, void*);
2134 r = EFI_CALL(efi_install_protocol_interface(
2136 EFI_NATIVE_INTERFACE,
2137 protocol_interface));
2138 if (r != EFI_SUCCESS)
2143 if (r == EFI_SUCCESS)
2146 /* If an error occurred undo all changes. */
2147 va_start(argptr, handle);
2149 protocol = va_arg(argptr, efi_guid_t*);
2150 protocol_interface = va_arg(argptr, void*);
2151 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2152 protocol_interface));
2160 * Uninstall multiple protocol interfaces.
2162 * This function implements the UninstallMultipleProtocolInterfaces service.
2163 * See the Unified Extensible Firmware Interface (UEFI) specification
2166 * @handle handle from which the protocol interfaces shall be removed
2167 * @... NULL terminated argument list with pairs of protocol GUIDS and
2169 * @return status code
2171 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2174 EFI_ENTRY("%p", handle);
2177 const efi_guid_t *protocol;
2178 void *protocol_interface;
2179 efi_status_t r = EFI_SUCCESS;
2183 return EFI_EXIT(EFI_INVALID_PARAMETER);
2185 va_start(argptr, handle);
2187 protocol = va_arg(argptr, efi_guid_t*);
2190 protocol_interface = va_arg(argptr, void*);
2191 r = EFI_CALL(efi_uninstall_protocol_interface(
2193 protocol_interface));
2194 if (r != EFI_SUCCESS)
2199 if (r == EFI_SUCCESS)
2202 /* If an error occurred undo all changes. */
2203 va_start(argptr, handle);
2205 protocol = va_arg(argptr, efi_guid_t*);
2206 protocol_interface = va_arg(argptr, void*);
2207 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2208 EFI_NATIVE_INTERFACE,
2209 protocol_interface));
2217 * Calculate cyclic redundancy code.
2219 * This function implements the CalculateCrc32 service.
2220 * See the Unified Extensible Firmware Interface (UEFI) specification
2223 * @data buffer with data
2224 * @data_size size of buffer in bytes
2225 * @crc32_p cyclic redundancy code
2226 * @return status code
2228 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2229 unsigned long data_size,
2232 EFI_ENTRY("%p, %ld", data, data_size);
2233 *crc32_p = crc32(0, data, data_size);
2234 return EFI_EXIT(EFI_SUCCESS);
2240 * This function implements the CopyMem service.
2241 * See the Unified Extensible Firmware Interface (UEFI) specification
2244 * @destination destination of the copy operation
2245 * @source source of the copy operation
2246 * @length number of bytes to copy
2248 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2251 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2252 memcpy(destination, source, length);
2253 EFI_EXIT(EFI_SUCCESS);
2257 * Fill memory with a byte value.
2259 * This function implements the SetMem service.
2260 * See the Unified Extensible Firmware Interface (UEFI) specification
2263 * @buffer buffer to fill
2264 * @size size of buffer in bytes
2265 * @value byte to copy to the buffer
2267 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2269 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2270 memset(buffer, value, size);
2271 EFI_EXIT(EFI_SUCCESS);
2275 * Open protocol interface on a handle.
2277 * @handler handler of a protocol
2278 * @protocol_interface interface implementing the protocol
2279 * @agent_handle handle of the driver
2280 * @controller_handle handle of the controller
2281 * @attributes attributes indicating how to open the protocol
2282 * @return status code
2284 static efi_status_t efi_protocol_open(
2285 struct efi_handler *handler,
2286 void **protocol_interface, void *agent_handle,
2287 void *controller_handle, uint32_t attributes)
2289 struct efi_open_protocol_info_item *item;
2290 struct efi_open_protocol_info_entry *match = NULL;
2291 bool opened_by_driver = false;
2292 bool opened_exclusive = false;
2294 /* If there is no agent, only return the interface */
2298 /* For TEST_PROTOCOL ignore interface attribute */
2299 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2300 *protocol_interface = NULL;
2303 * Check if the protocol is already opened by a driver with the same
2304 * attributes or opened exclusively
2306 list_for_each_entry(item, &handler->open_infos, link) {
2307 if (item->info.agent_handle == agent_handle) {
2308 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2309 (item->info.attributes == attributes))
2310 return EFI_ALREADY_STARTED;
2312 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2313 opened_exclusive = true;
2316 /* Only one controller can open the protocol exclusively */
2317 if (opened_exclusive && attributes &
2318 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2319 return EFI_ACCESS_DENIED;
2321 /* Prepare exclusive opening */
2322 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2323 /* Try to disconnect controllers */
2324 list_for_each_entry(item, &handler->open_infos, link) {
2325 if (item->info.attributes ==
2326 EFI_OPEN_PROTOCOL_BY_DRIVER)
2327 EFI_CALL(efi_disconnect_controller(
2328 item->info.controller_handle,
2329 item->info.agent_handle,
2332 opened_by_driver = false;
2333 /* Check if all controllers are disconnected */
2334 list_for_each_entry(item, &handler->open_infos, link) {
2335 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2336 opened_by_driver = true;
2338 /* Only one controller can be conncected */
2339 if (opened_by_driver)
2340 return EFI_ACCESS_DENIED;
2343 /* Find existing entry */
2344 list_for_each_entry(item, &handler->open_infos, link) {
2345 if (item->info.agent_handle == agent_handle &&
2346 item->info.controller_handle == controller_handle)
2347 match = &item->info;
2349 /* None found, create one */
2351 match = efi_create_open_info(handler);
2353 return EFI_OUT_OF_RESOURCES;
2356 match->agent_handle = agent_handle;
2357 match->controller_handle = controller_handle;
2358 match->attributes = attributes;
2359 match->open_count++;
2362 /* For TEST_PROTOCOL ignore interface attribute. */
2363 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2364 *protocol_interface = handler->protocol_interface;
2370 * Open protocol interface on a handle.
2372 * This function implements the OpenProtocol interface.
2373 * See the Unified Extensible Firmware Interface (UEFI) specification
2376 * @handle handle on which the protocol shall be opened
2377 * @protocol GUID of the protocol
2378 * @protocol_interface interface implementing the protocol
2379 * @agent_handle handle of the driver
2380 * @controller_handle handle of the controller
2381 * @attributes attributes indicating how to open the protocol
2382 * @return status code
2384 static efi_status_t EFIAPI efi_open_protocol(
2385 void *handle, const efi_guid_t *protocol,
2386 void **protocol_interface, void *agent_handle,
2387 void *controller_handle, uint32_t attributes)
2389 struct efi_handler *handler;
2390 efi_status_t r = EFI_INVALID_PARAMETER;
2392 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2393 protocol_interface, agent_handle, controller_handle,
2396 if (!handle || !protocol ||
2397 (!protocol_interface && attributes !=
2398 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2402 switch (attributes) {
2403 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2404 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2405 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2407 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2408 if (controller_handle == handle)
2411 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2412 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2413 /* Check that the controller handle is valid */
2414 if (!efi_search_obj(controller_handle))
2417 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2418 /* Check that the agent handle is valid */
2419 if (!efi_search_obj(agent_handle))
2426 r = efi_search_protocol(handle, protocol, &handler);
2427 if (r != EFI_SUCCESS)
2430 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2431 controller_handle, attributes);
2437 * Get interface of a protocol on a handle.
2439 * This function implements the HandleProtocol service.
2440 * See the Unified Extensible Firmware Interface (UEFI) specification
2443 * @handle handle on which the protocol shall be opened
2444 * @protocol GUID of the protocol
2445 * @protocol_interface interface implementing the protocol
2446 * @return status code
2448 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2449 const efi_guid_t *protocol,
2450 void **protocol_interface)
2452 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2453 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2456 static efi_status_t efi_bind_controller(
2457 efi_handle_t controller_handle,
2458 efi_handle_t driver_image_handle,
2459 struct efi_device_path *remain_device_path)
2461 struct efi_driver_binding_protocol *binding_protocol;
2464 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2465 &efi_guid_driver_binding_protocol,
2466 (void **)&binding_protocol,
2467 driver_image_handle, NULL,
2468 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2469 if (r != EFI_SUCCESS)
2471 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2473 remain_device_path));
2474 if (r == EFI_SUCCESS)
2475 r = EFI_CALL(binding_protocol->start(binding_protocol,
2477 remain_device_path));
2478 EFI_CALL(efi_close_protocol(driver_image_handle,
2479 &efi_guid_driver_binding_protocol,
2480 driver_image_handle, NULL));
2484 static efi_status_t efi_connect_single_controller(
2485 efi_handle_t controller_handle,
2486 efi_handle_t *driver_image_handle,
2487 struct efi_device_path *remain_device_path)
2489 efi_handle_t *buffer;
2493 size_t connected = 0;
2495 /* Get buffer with all handles with driver binding protocol */
2496 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2497 &efi_guid_driver_binding_protocol,
2498 NULL, &count, &buffer));
2499 if (r != EFI_SUCCESS)
2502 /* Context Override */
2503 if (driver_image_handle) {
2504 for (; *driver_image_handle; ++driver_image_handle) {
2505 for (i = 0; i < count; ++i) {
2506 if (buffer[i] == *driver_image_handle) {
2508 r = efi_bind_controller(
2510 *driver_image_handle,
2511 remain_device_path);
2513 * For drivers that do not support the
2514 * controller or are already connected
2515 * we receive an error code here.
2517 if (r == EFI_SUCCESS)
2525 * TODO: Some overrides are not yet implemented:
2526 * - Platform Driver Override
2527 * - Driver Family Override Search
2528 * - Bus Specific Driver Override
2531 /* Driver Binding Search */
2532 for (i = 0; i < count; ++i) {
2534 r = efi_bind_controller(controller_handle,
2536 remain_device_path);
2537 if (r == EFI_SUCCESS)
2542 efi_free_pool(buffer);
2544 return EFI_NOT_FOUND;
2549 * Connect a controller to a driver.
2551 * This function implements the ConnectController service.
2552 * See the Unified Extensible Firmware Interface (UEFI) specification
2555 * First all driver binding protocol handles are tried for binding drivers.
2556 * Afterwards all handles that have openened a protocol of the controller
2557 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2559 * @controller_handle handle of the controller
2560 * @driver_image_handle handle of the driver
2561 * @remain_device_path device path of a child controller
2562 * @recursive true to connect all child controllers
2563 * @return status code
2565 static efi_status_t EFIAPI efi_connect_controller(
2566 efi_handle_t controller_handle,
2567 efi_handle_t *driver_image_handle,
2568 struct efi_device_path *remain_device_path,
2572 efi_status_t ret = EFI_NOT_FOUND;
2573 struct efi_object *efiobj;
2575 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2576 remain_device_path, recursive);
2578 efiobj = efi_search_obj(controller_handle);
2580 ret = EFI_INVALID_PARAMETER;
2584 r = efi_connect_single_controller(controller_handle,
2585 driver_image_handle,
2586 remain_device_path);
2587 if (r == EFI_SUCCESS)
2590 struct efi_handler *handler;
2591 struct efi_open_protocol_info_item *item;
2593 list_for_each_entry(handler, &efiobj->protocols, link) {
2594 list_for_each_entry(item, &handler->open_infos, link) {
2595 if (item->info.attributes &
2596 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2597 r = EFI_CALL(efi_connect_controller(
2598 item->info.controller_handle,
2599 driver_image_handle,
2602 if (r == EFI_SUCCESS)
2608 /* Check for child controller specified by end node */
2609 if (ret != EFI_SUCCESS && remain_device_path &&
2610 remain_device_path->type == DEVICE_PATH_TYPE_END)
2613 return EFI_EXIT(ret);
2617 * Get all child controllers associated to a driver.
2618 * The allocated buffer has to be freed with free().
2620 * @efiobj handle of the controller
2621 * @driver_handle handle of the driver
2622 * @number_of_children number of child controllers
2623 * @child_handle_buffer handles of the the child controllers
2625 static efi_status_t efi_get_child_controllers(
2626 struct efi_object *efiobj,
2627 efi_handle_t driver_handle,
2628 efi_uintn_t *number_of_children,
2629 efi_handle_t **child_handle_buffer)
2631 struct efi_handler *handler;
2632 struct efi_open_protocol_info_item *item;
2633 efi_uintn_t count = 0, i;
2636 /* Count all child controller associations */
2637 list_for_each_entry(handler, &efiobj->protocols, link) {
2638 list_for_each_entry(item, &handler->open_infos, link) {
2639 if (item->info.agent_handle == driver_handle &&
2640 item->info.attributes &
2641 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2646 * Create buffer. In case of duplicate child controller assignments
2647 * the buffer will be too large. But that does not harm.
2649 *number_of_children = 0;
2650 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2651 if (!*child_handle_buffer)
2652 return EFI_OUT_OF_RESOURCES;
2653 /* Copy unique child handles */
2654 list_for_each_entry(handler, &efiobj->protocols, link) {
2655 list_for_each_entry(item, &handler->open_infos, link) {
2656 if (item->info.agent_handle == driver_handle &&
2657 item->info.attributes &
2658 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2659 /* Check this is a new child controller */
2661 for (i = 0; i < *number_of_children; ++i) {
2662 if ((*child_handle_buffer)[i] ==
2663 item->info.controller_handle)
2666 /* Copy handle to buffer */
2668 i = (*number_of_children)++;
2669 (*child_handle_buffer)[i] =
2670 item->info.controller_handle;
2679 * Disconnect a controller from a driver.
2681 * This function implements the DisconnectController service.
2682 * See the Unified Extensible Firmware Interface (UEFI) specification
2685 * @controller_handle handle of the controller
2686 * @driver_image_handle handle of the driver
2687 * @child_handle handle of the child to destroy
2688 * @return status code
2690 static efi_status_t EFIAPI efi_disconnect_controller(
2691 efi_handle_t controller_handle,
2692 efi_handle_t driver_image_handle,
2693 efi_handle_t child_handle)
2695 struct efi_driver_binding_protocol *binding_protocol;
2696 efi_handle_t *child_handle_buffer = NULL;
2697 size_t number_of_children = 0;
2699 size_t stop_count = 0;
2700 struct efi_object *efiobj;
2702 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2705 efiobj = efi_search_obj(controller_handle);
2707 r = EFI_INVALID_PARAMETER;
2711 if (child_handle && !efi_search_obj(child_handle)) {
2712 r = EFI_INVALID_PARAMETER;
2716 /* If no driver handle is supplied, disconnect all drivers */
2717 if (!driver_image_handle) {
2718 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2722 /* Create list of child handles */
2724 number_of_children = 1;
2725 child_handle_buffer = &child_handle;
2727 efi_get_child_controllers(efiobj,
2728 driver_image_handle,
2729 &number_of_children,
2730 &child_handle_buffer);
2733 /* Get the driver binding protocol */
2734 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2735 &efi_guid_driver_binding_protocol,
2736 (void **)&binding_protocol,
2737 driver_image_handle, NULL,
2738 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2739 if (r != EFI_SUCCESS)
2741 /* Remove the children */
2742 if (number_of_children) {
2743 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2746 child_handle_buffer));
2747 if (r == EFI_SUCCESS)
2750 /* Remove the driver */
2752 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2755 if (r == EFI_SUCCESS)
2757 EFI_CALL(efi_close_protocol(driver_image_handle,
2758 &efi_guid_driver_binding_protocol,
2759 driver_image_handle, NULL));
2767 free(child_handle_buffer);
2771 static const struct efi_boot_services efi_boot_services = {
2773 .headersize = sizeof(struct efi_table_hdr),
2775 .raise_tpl = efi_raise_tpl,
2776 .restore_tpl = efi_restore_tpl,
2777 .allocate_pages = efi_allocate_pages_ext,
2778 .free_pages = efi_free_pages_ext,
2779 .get_memory_map = efi_get_memory_map_ext,
2780 .allocate_pool = efi_allocate_pool_ext,
2781 .free_pool = efi_free_pool_ext,
2782 .create_event = efi_create_event_ext,
2783 .set_timer = efi_set_timer_ext,
2784 .wait_for_event = efi_wait_for_event,
2785 .signal_event = efi_signal_event_ext,
2786 .close_event = efi_close_event,
2787 .check_event = efi_check_event,
2788 .install_protocol_interface = efi_install_protocol_interface,
2789 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2790 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2791 .handle_protocol = efi_handle_protocol,
2793 .register_protocol_notify = efi_register_protocol_notify,
2794 .locate_handle = efi_locate_handle_ext,
2795 .locate_device_path = efi_locate_device_path,
2796 .install_configuration_table = efi_install_configuration_table_ext,
2797 .load_image = efi_load_image,
2798 .start_image = efi_start_image,
2800 .unload_image = efi_unload_image,
2801 .exit_boot_services = efi_exit_boot_services,
2802 .get_next_monotonic_count = efi_get_next_monotonic_count,
2804 .set_watchdog_timer = efi_set_watchdog_timer,
2805 .connect_controller = efi_connect_controller,
2806 .disconnect_controller = efi_disconnect_controller,
2807 .open_protocol = efi_open_protocol,
2808 .close_protocol = efi_close_protocol,
2809 .open_protocol_information = efi_open_protocol_information,
2810 .protocols_per_handle = efi_protocols_per_handle,
2811 .locate_handle_buffer = efi_locate_handle_buffer,
2812 .locate_protocol = efi_locate_protocol,
2813 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2814 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2815 .calculate_crc32 = efi_calculate_crc32,
2816 .copy_mem = efi_copy_mem,
2817 .set_mem = efi_set_mem,
2821 static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
2823 struct efi_system_table __efi_runtime_data systab = {
2825 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2826 .revision = 0x20005, /* 2.5 */
2827 .headersize = sizeof(struct efi_table_hdr),
2829 .fw_vendor = (long)firmware_vendor,
2830 .con_in = (void*)&efi_con_in,
2831 .con_out = (void*)&efi_con_out,
2832 .std_err = (void*)&efi_con_out,
2833 .runtime = (void*)&efi_runtime_services,
2834 .boottime = (void*)&efi_boot_services,
2836 .tables = (void*)efi_conf_table,