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 /* Called on every callback entry */
64 int __efi_entry_check(void)
66 int ret = entry_count++ == 0;
75 /* Called on every callback exit */
76 int __efi_exit_check(void)
78 int ret = --entry_count == 0;
85 /* Called from do_bootefi_exec() */
86 void efi_save_gd(void)
94 * Special case handler for error/abort that just forces things back
95 * to u-boot world so we can dump out an abort msg, without any care
96 * about returning back to UEFI world.
98 void efi_restore_gd(void)
101 /* Only restore if we're already in EFI context */
109 * Two spaces per indent level, maxing out at 10.. which ought to be
110 * enough for anyone ;-)
112 static const char *indent_string(int level)
114 const char *indent = " ";
115 const int max = strlen(indent);
116 level = min(max, level * 2);
117 return &indent[max - level];
120 const char *__efi_nesting(void)
122 return indent_string(nesting_level);
125 const char *__efi_nesting_inc(void)
127 return indent_string(nesting_level++);
130 const char *__efi_nesting_dec(void)
132 return indent_string(--nesting_level);
136 * Queue an EFI event.
138 * This function queues the notification function of the event for future
141 * The notification function is called if the task priority level of the
142 * event is higher than the current task priority level.
144 * For the SignalEvent service see efi_signal_event_ext.
146 * @event event to signal
148 void efi_signal_event(struct efi_event *event)
150 if (event->notify_function) {
151 event->is_queued = true;
153 if (efi_tpl >= event->notify_tpl)
155 EFI_CALL_VOID(event->notify_function(event,
156 event->notify_context));
158 event->is_queued = false;
162 * Raise the task priority level.
164 * This function implements the RaiseTpl service.
165 * See the Unified Extensible Firmware Interface (UEFI) specification
168 * @new_tpl new value of the task priority level
169 * @return old value of the task priority level
171 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
173 efi_uintn_t old_tpl = efi_tpl;
175 EFI_ENTRY("0x%zx", new_tpl);
177 if (new_tpl < efi_tpl)
178 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
180 if (efi_tpl > TPL_HIGH_LEVEL)
181 efi_tpl = TPL_HIGH_LEVEL;
183 EFI_EXIT(EFI_SUCCESS);
188 * Lower the task priority level.
190 * This function implements the RestoreTpl service.
191 * See the Unified Extensible Firmware Interface (UEFI) specification
194 * @old_tpl value of the task priority level to be restored
196 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
198 EFI_ENTRY("0x%zx", old_tpl);
200 if (old_tpl > efi_tpl)
201 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
203 if (efi_tpl > TPL_HIGH_LEVEL)
204 efi_tpl = TPL_HIGH_LEVEL;
206 EFI_EXIT(EFI_SUCCESS);
210 * Allocate memory pages.
212 * This function implements the AllocatePages service.
213 * See the Unified Extensible Firmware Interface (UEFI) specification
216 * @type type of allocation to be performed
217 * @memory_type usage type of the allocated memory
218 * @pages number of pages to be allocated
219 * @memory allocated memory
220 * @return status code
222 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
228 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
229 r = efi_allocate_pages(type, memory_type, pages, memory);
236 * This function implements the FreePages service.
237 * See the Unified Extensible Firmware Interface (UEFI) specification
240 * @memory start of the memory area to be freed
241 * @pages number of pages to be freed
242 * @return status code
244 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
249 EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages);
250 r = efi_free_pages(memory, pages);
255 * Get map describing memory usage.
257 * This function implements the GetMemoryMap service.
258 * See the Unified Extensible Firmware Interface (UEFI) specification
261 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
262 * on exit the size of the copied memory map
263 * @memory_map buffer to which the memory map is written
264 * @map_key key for the memory map
265 * @descriptor_size size of an individual memory descriptor
266 * @descriptor_version version number of the memory descriptor structure
267 * @return status code
269 static efi_status_t EFIAPI efi_get_memory_map_ext(
270 efi_uintn_t *memory_map_size,
271 struct efi_mem_desc *memory_map,
272 efi_uintn_t *map_key,
273 efi_uintn_t *descriptor_size,
274 uint32_t *descriptor_version)
278 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
279 map_key, descriptor_size, descriptor_version);
280 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
281 descriptor_size, descriptor_version);
286 * Allocate memory from pool.
288 * This function implements the AllocatePool service.
289 * See the Unified Extensible Firmware Interface (UEFI) specification
292 * @pool_type type of the pool from which memory is to be allocated
293 * @size number of bytes to be allocated
294 * @buffer allocated memory
295 * @return status code
297 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
303 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
304 r = efi_allocate_pool(pool_type, size, buffer);
309 * Free memory from pool.
311 * This function implements the FreePool service.
312 * See the Unified Extensible Firmware Interface (UEFI) specification
315 * @buffer start of memory to be freed
316 * @return status code
318 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
322 EFI_ENTRY("%p", buffer);
323 r = efi_free_pool(buffer);
328 * Add a new object to the object list.
330 * The protocols list is initialized.
331 * The object handle is set.
333 * @obj object to be added
335 void efi_add_handle(struct efi_object *obj)
339 INIT_LIST_HEAD(&obj->protocols);
341 list_add_tail(&obj->link, &efi_obj_list);
348 * @return status code
350 efi_status_t efi_create_handle(void **handle)
352 struct efi_object *obj;
355 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
356 sizeof(struct efi_object),
358 if (r != EFI_SUCCESS)
361 *handle = obj->handle;
366 * Find a protocol on a handle.
369 * @protocol_guid GUID of the protocol
370 * @handler reference to the protocol
371 * @return status code
373 efi_status_t efi_search_protocol(const void *handle,
374 const efi_guid_t *protocol_guid,
375 struct efi_handler **handler)
377 struct efi_object *efiobj;
378 struct list_head *lhandle;
380 if (!handle || !protocol_guid)
381 return EFI_INVALID_PARAMETER;
382 efiobj = efi_search_obj(handle);
384 return EFI_INVALID_PARAMETER;
385 list_for_each(lhandle, &efiobj->protocols) {
386 struct efi_handler *protocol;
388 protocol = list_entry(lhandle, struct efi_handler, link);
389 if (!guidcmp(protocol->guid, protocol_guid)) {
395 return EFI_NOT_FOUND;
399 * Delete protocol from a handle.
401 * @handle handle from which the protocol shall be deleted
402 * @protocol GUID of the protocol to be deleted
403 * @protocol_interface interface of the protocol implementation
404 * @return status code
406 efi_status_t efi_remove_protocol(const void *handle, const efi_guid_t *protocol,
407 void *protocol_interface)
409 struct efi_handler *handler;
412 ret = efi_search_protocol(handle, protocol, &handler);
413 if (ret != EFI_SUCCESS)
415 if (guidcmp(handler->guid, protocol))
416 return EFI_INVALID_PARAMETER;
417 list_del(&handler->link);
423 * Delete all protocols from a handle.
425 * @handle handle from which the protocols shall be deleted
426 * @return status code
428 efi_status_t efi_remove_all_protocols(const void *handle)
430 struct efi_object *efiobj;
431 struct efi_handler *protocol;
432 struct efi_handler *pos;
434 efiobj = efi_search_obj(handle);
436 return EFI_INVALID_PARAMETER;
437 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
440 ret = efi_remove_protocol(handle, protocol->guid,
441 protocol->protocol_interface);
442 if (ret != EFI_SUCCESS)
451 * @handle handle to delete
453 void efi_delete_handle(struct efi_object *obj)
457 efi_remove_all_protocols(obj->handle);
458 list_del(&obj->link);
463 * Our event capabilities are very limited. Only a small limited
464 * number of events is allowed to coexist.
466 static struct efi_event efi_events[16];
471 * This function is used inside U-Boot code to create an event.
473 * For the API function implementing the CreateEvent service see
474 * efi_create_event_ext.
476 * @type type of the event to create
477 * @notify_tpl task priority level of the event
478 * @notify_function notification function of the event
479 * @notify_context pointer passed to the notification function
480 * @event created event
481 * @return status code
483 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
484 void (EFIAPI *notify_function) (
485 struct efi_event *event,
487 void *notify_context, struct efi_event **event)
492 return EFI_INVALID_PARAMETER;
494 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
495 return EFI_INVALID_PARAMETER;
497 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
498 notify_function == NULL)
499 return EFI_INVALID_PARAMETER;
501 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
502 if (efi_events[i].type)
504 efi_events[i].type = type;
505 efi_events[i].notify_tpl = notify_tpl;
506 efi_events[i].notify_function = notify_function;
507 efi_events[i].notify_context = notify_context;
508 /* Disable timers on bootup */
509 efi_events[i].trigger_next = -1ULL;
510 efi_events[i].is_queued = false;
511 efi_events[i].is_signaled = false;
512 *event = &efi_events[i];
515 return EFI_OUT_OF_RESOURCES;
521 * This function implements the CreateEvent service.
522 * See the Unified Extensible Firmware Interface (UEFI) specification
525 * @type type of the event to create
526 * @notify_tpl task priority level of the event
527 * @notify_function notification function of the event
528 * @notify_context pointer passed to the notification function
529 * @event created event
530 * @return status code
532 static efi_status_t EFIAPI efi_create_event_ext(
533 uint32_t type, efi_uintn_t notify_tpl,
534 void (EFIAPI *notify_function) (
535 struct efi_event *event,
537 void *notify_context, struct efi_event **event)
539 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
541 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
542 notify_context, event));
547 * Check if a timer event has occurred or a queued notification function should
550 * Our timers have to work without interrupts, so we check whenever keyboard
551 * input or disk accesses happen if enough time elapsed for them to fire.
553 void efi_timer_check(void)
556 u64 now = timer_get_us();
558 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
559 if (!efi_events[i].type)
561 if (efi_events[i].is_queued)
562 efi_signal_event(&efi_events[i]);
563 if (!(efi_events[i].type & EVT_TIMER) ||
564 now < efi_events[i].trigger_next)
566 switch (efi_events[i].trigger_type) {
567 case EFI_TIMER_RELATIVE:
568 efi_events[i].trigger_type = EFI_TIMER_STOP;
570 case EFI_TIMER_PERIODIC:
571 efi_events[i].trigger_next +=
572 efi_events[i].trigger_time;
577 efi_events[i].is_signaled = true;
578 efi_signal_event(&efi_events[i]);
584 * Set the trigger time for a timer event or stop the event.
586 * This is the function for internal usage in U-Boot. For the API function
587 * implementing the SetTimer service see efi_set_timer_ext.
589 * @event event for which the timer is set
590 * @type type of the timer
591 * @trigger_time trigger period in multiples of 100ns
592 * @return status code
594 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
595 uint64_t trigger_time)
600 * The parameter defines a multiple of 100ns.
601 * We use multiples of 1000ns. So divide by 10.
603 do_div(trigger_time, 10);
605 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
606 if (event != &efi_events[i])
609 if (!(event->type & EVT_TIMER))
613 event->trigger_next = -1ULL;
615 case EFI_TIMER_PERIODIC:
616 case EFI_TIMER_RELATIVE:
617 event->trigger_next =
618 timer_get_us() + trigger_time;
621 return EFI_INVALID_PARAMETER;
623 event->trigger_type = type;
624 event->trigger_time = trigger_time;
625 event->is_signaled = false;
628 return EFI_INVALID_PARAMETER;
632 * Set the trigger time for a timer event or stop the event.
634 * This function implements the SetTimer service.
635 * See the Unified Extensible Firmware Interface (UEFI) specification
638 * @event event for which the timer is set
639 * @type type of the timer
640 * @trigger_time trigger period in multiples of 100ns
641 * @return status code
643 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
644 enum efi_timer_delay type,
645 uint64_t trigger_time)
647 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
648 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
652 * Wait for events to be signaled.
654 * This function implements the WaitForEvent service.
655 * See the Unified Extensible Firmware Interface (UEFI) specification
658 * @num_events number of events to be waited for
659 * @events events to be waited for
660 * @index index of the event that was signaled
661 * @return status code
663 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
664 struct efi_event **event,
669 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
671 /* Check parameters */
672 if (!num_events || !event)
673 return EFI_EXIT(EFI_INVALID_PARAMETER);
675 if (efi_tpl != TPL_APPLICATION)
676 return EFI_EXIT(EFI_UNSUPPORTED);
677 for (i = 0; i < num_events; ++i) {
678 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
679 if (event[i] == &efi_events[j])
682 return EFI_EXIT(EFI_INVALID_PARAMETER);
684 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
685 return EFI_EXIT(EFI_INVALID_PARAMETER);
686 if (!event[i]->is_signaled)
687 efi_signal_event(event[i]);
690 /* Wait for signal */
692 for (i = 0; i < num_events; ++i) {
693 if (event[i]->is_signaled)
696 /* Allow events to occur. */
702 * Reset the signal which is passed to the caller to allow periodic
705 event[i]->is_signaled = false;
709 return EFI_EXIT(EFI_SUCCESS);
713 * Signal an EFI event.
715 * This function implements the SignalEvent service.
716 * See the Unified Extensible Firmware Interface (UEFI) specification
719 * This functions sets the signaled state of the event and queues the
720 * notification function for execution.
722 * @event event to signal
723 * @return status code
725 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
729 EFI_ENTRY("%p", event);
730 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
731 if (event != &efi_events[i])
733 if (event->is_signaled)
735 event->is_signaled = true;
736 if (event->type & EVT_NOTIFY_SIGNAL)
737 efi_signal_event(event);
740 return EFI_EXIT(EFI_SUCCESS);
744 * Close an EFI event.
746 * This function implements the CloseEvent service.
747 * See the Unified Extensible Firmware Interface (UEFI) specification
750 * @event event to close
751 * @return status code
753 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
757 EFI_ENTRY("%p", event);
758 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
759 if (event == &efi_events[i]) {
761 event->trigger_next = -1ULL;
762 event->is_queued = false;
763 event->is_signaled = false;
764 return EFI_EXIT(EFI_SUCCESS);
767 return EFI_EXIT(EFI_INVALID_PARAMETER);
771 * Check if an event is signaled.
773 * This function implements the CheckEvent service.
774 * See the Unified Extensible Firmware Interface (UEFI) specification
777 * If an event is not signaled yet the notification function is queued.
779 * @event event to check
780 * @return status code
782 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
786 EFI_ENTRY("%p", event);
788 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
789 if (event != &efi_events[i])
791 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
793 if (!event->is_signaled)
794 efi_signal_event(event);
795 if (event->is_signaled)
796 return EFI_EXIT(EFI_SUCCESS);
797 return EFI_EXIT(EFI_NOT_READY);
799 return EFI_EXIT(EFI_INVALID_PARAMETER);
803 * Find the internal EFI object for a handle.
805 * @handle handle to find
808 struct efi_object *efi_search_obj(const void *handle)
810 struct efi_object *efiobj;
812 list_for_each_entry(efiobj, &efi_obj_list, link) {
813 if (efiobj->handle == handle)
821 * Create open protocol info entry and add it to a protocol.
823 * @handler handler of a protocol
824 * @return open protocol info entry
826 static struct efi_open_protocol_info_entry *efi_create_open_info(
827 struct efi_handler *handler)
829 struct efi_open_protocol_info_item *item;
831 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
834 /* Append the item to the open protocol info list. */
835 list_add_tail(&item->link, &handler->open_infos);
841 * Remove an open protocol info entry from a protocol.
843 * @handler handler of a protocol
844 * @return status code
846 static efi_status_t efi_delete_open_info(
847 struct efi_open_protocol_info_item *item)
849 list_del(&item->link);
855 * Install new protocol on a handle.
857 * @handle handle on which the protocol shall be installed
858 * @protocol GUID of the protocol to be installed
859 * @protocol_interface interface of the protocol implementation
860 * @return status code
862 efi_status_t efi_add_protocol(const void *handle, const efi_guid_t *protocol,
863 void *protocol_interface)
865 struct efi_object *efiobj;
866 struct efi_handler *handler;
869 efiobj = efi_search_obj(handle);
871 return EFI_INVALID_PARAMETER;
872 ret = efi_search_protocol(handle, protocol, NULL);
873 if (ret != EFI_NOT_FOUND)
874 return EFI_INVALID_PARAMETER;
875 handler = calloc(1, sizeof(struct efi_handler));
877 return EFI_OUT_OF_RESOURCES;
878 handler->guid = protocol;
879 handler->protocol_interface = protocol_interface;
880 INIT_LIST_HEAD(&handler->open_infos);
881 list_add_tail(&handler->link, &efiobj->protocols);
882 if (!guidcmp(&efi_guid_device_path, protocol))
883 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
888 * Install protocol interface.
890 * This function implements the InstallProtocolInterface service.
891 * See the Unified Extensible Firmware Interface (UEFI) specification
894 * @handle handle on which the protocol shall be installed
895 * @protocol GUID of the protocol to be installed
896 * @protocol_interface_type type of the interface to be installed,
897 * always EFI_NATIVE_INTERFACE
898 * @protocol_interface interface of the protocol implementation
899 * @return status code
901 static efi_status_t EFIAPI efi_install_protocol_interface(
902 void **handle, const efi_guid_t *protocol,
903 int protocol_interface_type, void *protocol_interface)
907 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
910 if (!handle || !protocol ||
911 protocol_interface_type != EFI_NATIVE_INTERFACE) {
912 r = EFI_INVALID_PARAMETER;
916 /* Create new handle if requested. */
918 r = efi_create_handle(handle);
919 if (r != EFI_SUCCESS)
921 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
924 debug("%sEFI: handle %p\n", indent_string(nesting_level),
927 /* Add new protocol */
928 r = efi_add_protocol(*handle, protocol, protocol_interface);
934 * Reinstall protocol interface.
936 * This function implements the ReinstallProtocolInterface service.
937 * See the Unified Extensible Firmware Interface (UEFI) specification
940 * @handle handle on which the protocol shall be
942 * @protocol GUID of the protocol to be installed
943 * @old_interface interface to be removed
944 * @new_interface interface to be installed
945 * @return status code
947 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
948 const efi_guid_t *protocol, void *old_interface,
951 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
953 return EFI_EXIT(EFI_ACCESS_DENIED);
957 * Uninstall protocol interface.
959 * This function implements the UninstallProtocolInterface service.
960 * See the Unified Extensible Firmware Interface (UEFI) specification
963 * @handle handle from which the protocol shall be removed
964 * @protocol GUID of the protocol to be removed
965 * @protocol_interface interface to be removed
966 * @return status code
968 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
969 void *handle, const efi_guid_t *protocol,
970 void *protocol_interface)
972 struct efi_handler *handler;
975 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
977 if (!handle || !protocol) {
978 r = EFI_INVALID_PARAMETER;
982 /* Find the protocol on the handle */
983 r = efi_search_protocol(handle, protocol, &handler);
984 if (r != EFI_SUCCESS)
986 if (handler->protocol_interface) {
987 /* TODO disconnect controllers */
988 r = EFI_ACCESS_DENIED;
990 r = efi_remove_protocol(handle, protocol, protocol_interface);
997 * Register an event for notification when a protocol is installed.
999 * This function implements the RegisterProtocolNotify service.
1000 * See the Unified Extensible Firmware Interface (UEFI) specification
1003 * @protocol GUID of the protocol whose installation shall be
1005 * @event event to be signaled upon installation of the protocol
1006 * @registration key for retrieving the registration information
1007 * @return status code
1009 static efi_status_t EFIAPI efi_register_protocol_notify(
1010 const efi_guid_t *protocol,
1011 struct efi_event *event,
1012 void **registration)
1014 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1015 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1019 * Determine if an EFI handle implements a protocol.
1021 * See the documentation of the LocateHandle service in the UEFI specification.
1023 * @search_type selection criterion
1024 * @protocol GUID of the protocol
1025 * @search_key registration key
1027 * @return 0 if the handle implements the protocol
1029 static int efi_search(enum efi_locate_search_type search_type,
1030 const efi_guid_t *protocol, void *search_key,
1031 struct efi_object *efiobj)
1035 switch (search_type) {
1038 case BY_REGISTER_NOTIFY:
1039 /* TODO: RegisterProtocolNotify is not implemented yet */
1042 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1043 return (ret != EFI_SUCCESS);
1045 /* Invalid search type */
1051 * Locate handles implementing a protocol.
1053 * This function is meant for U-Boot internal calls. For the API implementation
1054 * of the LocateHandle service see efi_locate_handle_ext.
1056 * @search_type selection criterion
1057 * @protocol GUID of the protocol
1058 * @search_key registration key
1059 * @buffer_size size of the buffer to receive the handles in bytes
1060 * @buffer buffer to receive the relevant handles
1061 * @return status code
1063 static efi_status_t efi_locate_handle(
1064 enum efi_locate_search_type search_type,
1065 const efi_guid_t *protocol, void *search_key,
1066 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1068 struct efi_object *efiobj;
1069 efi_uintn_t size = 0;
1071 /* Check parameters */
1072 switch (search_type) {
1075 case BY_REGISTER_NOTIFY:
1077 return EFI_INVALID_PARAMETER;
1078 /* RegisterProtocolNotify is not implemented yet */
1079 return EFI_UNSUPPORTED;
1082 return EFI_INVALID_PARAMETER;
1085 return EFI_INVALID_PARAMETER;
1089 * efi_locate_handle_buffer uses this function for
1090 * the calculation of the necessary buffer size.
1091 * So do not require a buffer for buffersize == 0.
1093 if (!buffer_size || (*buffer_size && !buffer))
1094 return EFI_INVALID_PARAMETER;
1096 /* Count how much space we need */
1097 list_for_each_entry(efiobj, &efi_obj_list, link) {
1098 if (!efi_search(search_type, protocol, search_key, efiobj))
1099 size += sizeof(void*);
1102 if (*buffer_size < size) {
1103 *buffer_size = size;
1104 return EFI_BUFFER_TOO_SMALL;
1107 *buffer_size = size;
1109 return EFI_NOT_FOUND;
1111 /* Then fill the array */
1112 list_for_each_entry(efiobj, &efi_obj_list, link) {
1113 if (!efi_search(search_type, protocol, search_key, efiobj))
1114 *buffer++ = efiobj->handle;
1121 * Locate handles implementing a protocol.
1123 * This function implements the LocateHandle service.
1124 * See the Unified Extensible Firmware Interface (UEFI) specification
1127 * @search_type selection criterion
1128 * @protocol GUID of the protocol
1129 * @search_key registration key
1130 * @buffer_size size of the buffer to receive the handles in bytes
1131 * @buffer buffer to receive the relevant handles
1132 * @return 0 if the handle implements the protocol
1134 static efi_status_t EFIAPI efi_locate_handle_ext(
1135 enum efi_locate_search_type search_type,
1136 const efi_guid_t *protocol, void *search_key,
1137 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1139 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1140 buffer_size, buffer);
1142 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1143 buffer_size, buffer));
1146 /* Collapses configuration table entries, removing index i */
1147 static void efi_remove_configuration_table(int i)
1149 struct efi_configuration_table *this = &efi_conf_table[i];
1150 struct efi_configuration_table *next = &efi_conf_table[i+1];
1151 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1153 memmove(this, next, (ulong)end - (ulong)next);
1158 * Adds, updates, or removes a configuration table.
1160 * This function is used for internal calls. For the API implementation of the
1161 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1163 * @guid GUID of the installed table
1164 * @table table to be installed
1165 * @return status code
1167 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
1171 /* Check for guid override */
1172 for (i = 0; i < systab.nr_tables; i++) {
1173 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1175 efi_conf_table[i].table = table;
1177 efi_remove_configuration_table(i);
1183 return EFI_NOT_FOUND;
1185 /* No override, check for overflow */
1186 if (i >= ARRAY_SIZE(efi_conf_table))
1187 return EFI_OUT_OF_RESOURCES;
1189 /* Add a new entry */
1190 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1191 efi_conf_table[i].table = table;
1192 systab.nr_tables = i + 1;
1198 * Adds, updates, or removes a configuration table.
1200 * This function implements the InstallConfigurationTable service.
1201 * See the Unified Extensible Firmware Interface (UEFI) specification
1204 * @guid GUID of the installed table
1205 * @table table to be installed
1206 * @return status code
1208 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1211 EFI_ENTRY("%pUl, %p", guid, table);
1212 return EFI_EXIT(efi_install_configuration_table(guid, table));
1216 * Initialize a loaded_image_info + loaded_image_info object with correct
1217 * protocols, boot-device, etc.
1219 * @info loaded image info to be passed to the entry point of the
1221 * @obj internal object associated with the loaded image
1222 * @device_path device path of the loaded image
1223 * @file_path file path of the loaded image
1224 * @return status code
1226 efi_status_t efi_setup_loaded_image(
1227 struct efi_loaded_image *info, struct efi_object *obj,
1228 struct efi_device_path *device_path,
1229 struct efi_device_path *file_path)
1233 /* Add internal object to object list */
1234 efi_add_handle(obj);
1235 /* efi_exit() assumes that the handle points to the info */
1238 info->file_path = file_path;
1240 info->device_handle = efi_dp_find_obj(device_path, NULL);
1243 * When asking for the device path interface, return
1244 * bootefi_device_path
1246 ret = efi_add_protocol(obj->handle, &efi_guid_device_path, device_path);
1247 if (ret != EFI_SUCCESS)
1251 * When asking for the loaded_image interface, just
1252 * return handle which points to loaded_image_info
1254 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1255 if (ret != EFI_SUCCESS)
1258 ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1259 (void *)&efi_console_control);
1260 if (ret != EFI_SUCCESS)
1263 ret = efi_add_protocol(obj->handle,
1264 &efi_guid_device_path_to_text_protocol,
1265 (void *)&efi_device_path_to_text);
1266 if (ret != EFI_SUCCESS)
1271 printf("ERROR: Failure to install protocols for loaded image\n");
1276 * Load an image using a file path.
1278 * @file_path the path of the image to load
1279 * @buffer buffer containing the loaded image
1280 * @return status code
1282 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1285 struct efi_file_info *info = NULL;
1286 struct efi_file_handle *f;
1287 static efi_status_t ret;
1290 f = efi_file_from_path(file_path);
1292 return EFI_DEVICE_ERROR;
1295 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1297 if (ret == EFI_BUFFER_TOO_SMALL) {
1299 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1302 if (ret != EFI_SUCCESS)
1305 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1309 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1313 EFI_CALL(f->close(f));
1315 if (ret != EFI_SUCCESS) {
1316 efi_free_pool(*buffer);
1324 * Load an EFI image into memory.
1326 * This function implements the LoadImage service.
1327 * See the Unified Extensible Firmware Interface (UEFI) specification
1330 * @boot_policy true for request originating from the boot manager
1331 * @parent_image the calles's image handle
1332 * @file_path the path of the image to load
1333 * @source_buffer memory location from which the image is installed
1334 * @source_size size of the memory area from which the image is
1336 * @image_handle handle for the newly installed image
1337 * @return status code
1339 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1340 efi_handle_t parent_image,
1341 struct efi_device_path *file_path,
1342 void *source_buffer,
1343 unsigned long source_size,
1344 efi_handle_t *image_handle)
1346 struct efi_loaded_image *info;
1347 struct efi_object *obj;
1350 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1351 file_path, source_buffer, source_size, image_handle);
1353 info = calloc(1, sizeof(*info));
1354 obj = calloc(1, sizeof(*obj));
1356 if (!source_buffer) {
1357 struct efi_device_path *dp, *fp;
1359 ret = efi_load_image_from_path(file_path, &source_buffer);
1360 if (ret != EFI_SUCCESS)
1363 * split file_path which contains both the device and
1366 efi_dp_split_file_path(file_path, &dp, &fp);
1367 ret = efi_setup_loaded_image(info, obj, dp, fp);
1368 if (ret != EFI_SUCCESS)
1371 /* In this case, file_path is the "device" path, ie.
1372 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1374 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1375 if (ret != EFI_SUCCESS)
1378 info->reserved = efi_load_pe(source_buffer, info);
1379 if (!info->reserved) {
1380 ret = EFI_UNSUPPORTED;
1383 info->system_table = &systab;
1384 info->parent_handle = parent_image;
1385 *image_handle = obj->handle;
1386 return EFI_EXIT(EFI_SUCCESS);
1389 efi_delete_handle(obj);
1390 return EFI_EXIT(ret);
1394 * Call the entry point of an image.
1396 * This function implements the StartImage service.
1397 * See the Unified Extensible Firmware Interface (UEFI) specification
1400 * @image_handle handle of the image
1401 * @exit_data_size size of the buffer
1402 * @exit_data buffer to receive the exit data of the called image
1403 * @return status code
1405 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1406 unsigned long *exit_data_size,
1409 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1410 struct efi_loaded_image *info = image_handle;
1413 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1414 entry = info->reserved;
1416 efi_is_direct_boot = false;
1418 /* call the image! */
1419 if (setjmp(&info->exit_jmp)) {
1421 * We called the entry point of the child image with EFI_CALL
1422 * in the lines below. The child image called the Exit() boot
1423 * service efi_exit() which executed the long jump that brought
1424 * us to the current line. This implies that the second half
1425 * of the EFI_CALL macro has not been executed.
1429 * efi_exit() called efi_restore_gd(). We have to undo this
1430 * otherwise __efi_entry_check() will put the wrong value into
1436 * To get ready to call EFI_EXIT below we have to execute the
1437 * missed out steps of EFI_CALL.
1439 assert(__efi_entry_check());
1440 debug("%sEFI: %lu returned by started image\n",
1441 __efi_nesting_dec(),
1442 (unsigned long)((uintptr_t)info->exit_status &
1444 return EFI_EXIT(info->exit_status);
1447 ret = EFI_CALL(entry(image_handle, &systab));
1449 /* Should usually never get here */
1450 return EFI_EXIT(ret);
1454 * Leave an EFI application or driver.
1456 * This function implements the Exit service.
1457 * See the Unified Extensible Firmware Interface (UEFI) specification
1460 * @image_handle handle of the application or driver that is exiting
1461 * @exit_status status code
1462 * @exit_data_size size of the buffer in bytes
1463 * @exit_data buffer with data describing an error
1464 * @return status code
1466 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1467 efi_status_t exit_status, unsigned long exit_data_size,
1471 * We require that the handle points to the original loaded
1472 * image protocol interface.
1474 * For getting the longjmp address this is safer than locating
1475 * the protocol because the protocol may have been reinstalled
1476 * pointing to another memory location.
1478 * TODO: We should call the unload procedure of the loaded
1481 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1483 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1484 exit_data_size, exit_data);
1486 /* Make sure entry/exit counts for EFI world cross-overs match */
1487 EFI_EXIT(exit_status);
1490 * But longjmp out with the U-Boot gd, not the application's, as
1491 * the other end is a setjmp call inside EFI context.
1495 loaded_image_info->exit_status = exit_status;
1496 longjmp(&loaded_image_info->exit_jmp, 1);
1498 panic("EFI application exited");
1502 * Unload an EFI image.
1504 * This function implements the UnloadImage service.
1505 * See the Unified Extensible Firmware Interface (UEFI) specification
1508 * @image_handle handle of the image to be unloaded
1509 * @return status code
1511 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1513 struct efi_object *efiobj;
1515 EFI_ENTRY("%p", image_handle);
1516 efiobj = efi_search_obj(image_handle);
1518 list_del(&efiobj->link);
1520 return EFI_EXIT(EFI_SUCCESS);
1524 * Fix up caches for EFI payloads if necessary.
1526 static void efi_exit_caches(void)
1528 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1530 * Grub on 32bit ARM needs to have caches disabled before jumping into
1531 * a zImage, but does not know of all cache layers. Give it a hand.
1533 if (efi_is_direct_boot)
1534 cleanup_before_linux();
1539 * Stop boot services.
1541 * This function implements the ExitBootServices service.
1542 * See the Unified Extensible Firmware Interface (UEFI) specification
1545 * @image_handle handle of the loaded image
1546 * @map_key key of the memory map
1547 * @return status code
1549 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1550 unsigned long map_key)
1554 EFI_ENTRY("%p, %ld", image_handle, map_key);
1556 /* Notify that ExitBootServices is invoked. */
1557 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1558 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1560 efi_signal_event(&efi_events[i]);
1562 /* Make sure that notification functions are not called anymore */
1563 efi_tpl = TPL_HIGH_LEVEL;
1565 /* XXX Should persist EFI variables here */
1567 board_quiesce_devices();
1569 /* Fix up caches for EFI payloads if necessary */
1572 /* This stops all lingering devices */
1573 bootm_disable_interrupts();
1575 /* Give the payload some time to boot */
1576 efi_set_watchdog(0);
1579 return EFI_EXIT(EFI_SUCCESS);
1583 * Get next value of the counter.
1585 * This function implements the NextMonotonicCount service.
1586 * See the Unified Extensible Firmware Interface (UEFI) specification
1589 * @count returned value of the counter
1590 * @return status code
1592 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1594 static uint64_t mono = 0;
1595 EFI_ENTRY("%p", count);
1597 return EFI_EXIT(EFI_SUCCESS);
1603 * This function implements the Stall sercive.
1604 * See the Unified Extensible Firmware Interface (UEFI) specification
1607 * @microseconds period to sleep in microseconds
1608 * @return status code
1610 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1612 EFI_ENTRY("%ld", microseconds);
1613 udelay(microseconds);
1614 return EFI_EXIT(EFI_SUCCESS);
1618 * Reset the watchdog timer.
1620 * This function implements the SetWatchdogTimer service.
1621 * See the Unified Extensible Firmware Interface (UEFI) specification
1624 * @timeout seconds before reset by watchdog
1625 * @watchdog_code code to be logged when resetting
1626 * @data_size size of buffer in bytes
1627 * @watchdog_data buffer with data describing the reset reason
1628 * @return status code
1630 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1631 uint64_t watchdog_code,
1632 unsigned long data_size,
1633 uint16_t *watchdog_data)
1635 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1636 data_size, watchdog_data);
1637 return EFI_EXIT(efi_set_watchdog(timeout));
1641 * Disconnect a controller from a driver.
1643 * This function implements the DisconnectController service.
1644 * See the Unified Extensible Firmware Interface (UEFI) specification
1647 * @controller_handle handle of the controller
1648 * @driver_image_handle handle of the driver
1649 * @child_handle handle of the child to destroy
1650 * @return status code
1652 static efi_status_t EFIAPI efi_disconnect_controller(
1653 efi_handle_t controller_handle,
1654 efi_handle_t driver_image_handle,
1655 efi_handle_t child_handle)
1657 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1659 return EFI_EXIT(EFI_INVALID_PARAMETER);
1665 * This function implements the CloseProtocol service.
1666 * See the Unified Extensible Firmware Interface (UEFI) specification
1669 * @handle handle on which the protocol shall be closed
1670 * @protocol GUID of the protocol to close
1671 * @agent_handle handle of the driver
1672 * @controller_handle handle of the controller
1673 * @return status code
1675 static efi_status_t EFIAPI efi_close_protocol(void *handle,
1676 const efi_guid_t *protocol,
1678 void *controller_handle)
1680 struct efi_handler *handler;
1681 struct efi_open_protocol_info_item *item;
1682 struct efi_open_protocol_info_item *pos;
1685 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1688 if (!agent_handle) {
1689 r = EFI_INVALID_PARAMETER;
1692 r = efi_search_protocol(handle, protocol, &handler);
1693 if (r != EFI_SUCCESS)
1697 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1698 if (item->info.agent_handle == agent_handle &&
1699 item->info.controller_handle == controller_handle) {
1700 efi_delete_open_info(item);
1710 * Provide information about then open status of a protocol on a handle
1712 * This function implements the OpenProtocolInformation service.
1713 * See the Unified Extensible Firmware Interface (UEFI) specification
1716 * @handle handle for which the information shall be retrieved
1717 * @protocol GUID of the protocol
1718 * @entry_buffer buffer to receive the open protocol information
1719 * @entry_count number of entries available in the buffer
1720 * @return status code
1722 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1723 const efi_guid_t *protocol,
1724 struct efi_open_protocol_info_entry **entry_buffer,
1725 efi_uintn_t *entry_count)
1727 unsigned long buffer_size;
1728 unsigned long count;
1729 struct efi_handler *handler;
1730 struct efi_open_protocol_info_item *item;
1733 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1736 /* Check parameters */
1737 if (!entry_buffer) {
1738 r = EFI_INVALID_PARAMETER;
1741 r = efi_search_protocol(handle, protocol, &handler);
1742 if (r != EFI_SUCCESS)
1747 list_for_each_entry(item, &handler->open_infos, link) {
1748 if (item->info.open_count)
1751 *entry_count = count;
1752 *entry_buffer = NULL;
1759 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
1760 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1761 (void **)entry_buffer);
1762 if (r != EFI_SUCCESS)
1764 list_for_each_entry_reverse(item, &handler->open_infos, link) {
1765 if (item->info.open_count)
1766 (*entry_buffer)[--count] = item->info;
1773 * Get protocols installed on a handle.
1775 * This function implements the ProtocolsPerHandleService.
1776 * See the Unified Extensible Firmware Interface (UEFI) specification
1779 * @handle handle for which the information is retrieved
1780 * @protocol_buffer buffer with protocol GUIDs
1781 * @protocol_buffer_count number of entries in the buffer
1782 * @return status code
1784 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1785 efi_guid_t ***protocol_buffer,
1786 efi_uintn_t *protocol_buffer_count)
1788 unsigned long buffer_size;
1789 struct efi_object *efiobj;
1790 struct list_head *protocol_handle;
1793 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1794 protocol_buffer_count);
1796 if (!handle || !protocol_buffer || !protocol_buffer_count)
1797 return EFI_EXIT(EFI_INVALID_PARAMETER);
1799 *protocol_buffer = NULL;
1800 *protocol_buffer_count = 0;
1802 efiobj = efi_search_obj(handle);
1804 return EFI_EXIT(EFI_INVALID_PARAMETER);
1806 /* Count protocols */
1807 list_for_each(protocol_handle, &efiobj->protocols) {
1808 ++*protocol_buffer_count;
1812 if (*protocol_buffer_count) {
1815 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1816 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1817 (void **)protocol_buffer);
1818 if (r != EFI_SUCCESS)
1820 list_for_each(protocol_handle, &efiobj->protocols) {
1821 struct efi_handler *protocol;
1823 protocol = list_entry(protocol_handle,
1824 struct efi_handler, link);
1825 (*protocol_buffer)[j] = (void *)protocol->guid;
1830 return EFI_EXIT(EFI_SUCCESS);
1834 * Locate handles implementing a protocol.
1836 * This function implements the LocateHandleBuffer service.
1837 * See the Unified Extensible Firmware Interface (UEFI) specification
1840 * @search_type selection criterion
1841 * @protocol GUID of the protocol
1842 * @search_key registration key
1843 * @no_handles number of returned handles
1844 * @buffer buffer with the returned handles
1845 * @return status code
1847 static efi_status_t EFIAPI efi_locate_handle_buffer(
1848 enum efi_locate_search_type search_type,
1849 const efi_guid_t *protocol, void *search_key,
1850 efi_uintn_t *no_handles, efi_handle_t **buffer)
1853 efi_uintn_t buffer_size = 0;
1855 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1856 no_handles, buffer);
1858 if (!no_handles || !buffer) {
1859 r = EFI_INVALID_PARAMETER;
1864 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1866 if (r != EFI_BUFFER_TOO_SMALL)
1868 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1870 if (r != EFI_SUCCESS)
1872 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1874 if (r == EFI_SUCCESS)
1875 *no_handles = buffer_size / sizeof(void *);
1881 * Find an interface implementing a protocol.
1883 * This function implements the LocateProtocol service.
1884 * See the Unified Extensible Firmware Interface (UEFI) specification
1887 * @protocol GUID of the protocol
1888 * @registration registration key passed to the notification function
1889 * @protocol_interface interface implementing the protocol
1890 * @return status code
1892 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
1894 void **protocol_interface)
1896 struct list_head *lhandle;
1899 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
1901 if (!protocol || !protocol_interface)
1902 return EFI_EXIT(EFI_INVALID_PARAMETER);
1904 list_for_each(lhandle, &efi_obj_list) {
1905 struct efi_object *efiobj;
1906 struct efi_handler *handler;
1908 efiobj = list_entry(lhandle, struct efi_object, link);
1910 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
1911 if (ret == EFI_SUCCESS) {
1912 *protocol_interface = handler->protocol_interface;
1913 return EFI_EXIT(EFI_SUCCESS);
1916 *protocol_interface = NULL;
1918 return EFI_EXIT(EFI_NOT_FOUND);
1922 * Get the device path and handle of an device implementing a protocol.
1924 * This function implements the LocateDevicePath service.
1925 * See the Unified Extensible Firmware Interface (UEFI) specification
1928 * @protocol GUID of the protocol
1929 * @device_path device path
1930 * @device handle of the device
1931 * @return status code
1933 static efi_status_t EFIAPI efi_locate_device_path(
1934 const efi_guid_t *protocol,
1935 struct efi_device_path **device_path,
1936 efi_handle_t *device)
1938 struct efi_device_path *dp;
1940 struct efi_handler *handler;
1941 efi_handle_t *handles;
1943 size_t len_best = 0;
1944 efi_uintn_t no_handles;
1948 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1950 if (!protocol || !device_path || !*device_path || !device) {
1951 ret = EFI_INVALID_PARAMETER;
1955 /* Find end of device path */
1956 len = efi_dp_size(*device_path);
1958 /* Get all handles implementing the protocol */
1959 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1960 &no_handles, &handles));
1961 if (ret != EFI_SUCCESS)
1964 for (i = 0; i < no_handles; ++i) {
1965 /* Find the device path protocol */
1966 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1968 if (ret != EFI_SUCCESS)
1970 dp = (struct efi_device_path *)handler->protocol_interface;
1971 len_dp = efi_dp_size(dp);
1973 * This handle can only be a better fit
1974 * if its device path length is longer than the best fit and
1975 * if its device path length is shorter of equal the searched
1978 if (len_dp <= len_best || len_dp > len)
1980 /* Check if dp is a subpath of device_path */
1981 if (memcmp(*device_path, dp, len_dp))
1983 *device = handles[i];
1987 remainder = (u8 *)*device_path + len_best;
1988 *device_path = (struct efi_device_path *)remainder;
1991 ret = EFI_NOT_FOUND;
1994 return EFI_EXIT(ret);
1998 * Install multiple protocol interfaces.
2000 * This function implements the MultipleProtocolInterfaces service.
2001 * See the Unified Extensible Firmware Interface (UEFI) specification
2004 * @handle handle on which the protocol interfaces shall be installed
2005 * @... NULL terminated argument list with pairs of protocol GUIDS and
2007 * @return status code
2009 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2012 EFI_ENTRY("%p", handle);
2015 const efi_guid_t *protocol;
2016 void *protocol_interface;
2017 efi_status_t r = EFI_SUCCESS;
2021 return EFI_EXIT(EFI_INVALID_PARAMETER);
2023 va_start(argptr, handle);
2025 protocol = va_arg(argptr, efi_guid_t*);
2028 protocol_interface = va_arg(argptr, void*);
2029 r = EFI_CALL(efi_install_protocol_interface(
2031 EFI_NATIVE_INTERFACE,
2032 protocol_interface));
2033 if (r != EFI_SUCCESS)
2038 if (r == EFI_SUCCESS)
2041 /* If an error occurred undo all changes. */
2042 va_start(argptr, handle);
2044 protocol = va_arg(argptr, efi_guid_t*);
2045 protocol_interface = va_arg(argptr, void*);
2046 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2047 protocol_interface));
2055 * Uninstall multiple protocol interfaces.
2057 * This function implements the UninstallMultipleProtocolInterfaces service.
2058 * See the Unified Extensible Firmware Interface (UEFI) specification
2061 * @handle handle from which the protocol interfaces shall be removed
2062 * @... NULL terminated argument list with pairs of protocol GUIDS and
2064 * @return status code
2066 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2069 EFI_ENTRY("%p", handle);
2072 const efi_guid_t *protocol;
2073 void *protocol_interface;
2074 efi_status_t r = EFI_SUCCESS;
2078 return EFI_EXIT(EFI_INVALID_PARAMETER);
2080 va_start(argptr, handle);
2082 protocol = va_arg(argptr, efi_guid_t*);
2085 protocol_interface = va_arg(argptr, void*);
2086 r = EFI_CALL(efi_uninstall_protocol_interface(
2088 protocol_interface));
2089 if (r != EFI_SUCCESS)
2094 if (r == EFI_SUCCESS)
2097 /* If an error occurred undo all changes. */
2098 va_start(argptr, handle);
2100 protocol = va_arg(argptr, efi_guid_t*);
2101 protocol_interface = va_arg(argptr, void*);
2102 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2103 EFI_NATIVE_INTERFACE,
2104 protocol_interface));
2112 * Calculate cyclic redundancy code.
2114 * This function implements the CalculateCrc32 service.
2115 * See the Unified Extensible Firmware Interface (UEFI) specification
2118 * @data buffer with data
2119 * @data_size size of buffer in bytes
2120 * @crc32_p cyclic redundancy code
2121 * @return status code
2123 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2124 unsigned long data_size,
2127 EFI_ENTRY("%p, %ld", data, data_size);
2128 *crc32_p = crc32(0, data, data_size);
2129 return EFI_EXIT(EFI_SUCCESS);
2135 * This function implements the CopyMem service.
2136 * See the Unified Extensible Firmware Interface (UEFI) specification
2139 * @destination destination of the copy operation
2140 * @source source of the copy operation
2141 * @length number of bytes to copy
2143 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2146 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2147 memcpy(destination, source, length);
2148 EFI_EXIT(EFI_SUCCESS);
2152 * Fill memory with a byte value.
2154 * This function implements the SetMem service.
2155 * See the Unified Extensible Firmware Interface (UEFI) specification
2158 * @buffer buffer to fill
2159 * @size size of buffer in bytes
2160 * @value byte to copy to the buffer
2162 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2164 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2165 memset(buffer, value, size);
2166 EFI_EXIT(EFI_SUCCESS);
2170 * Open protocol interface on a handle.
2172 * @handler handler of a protocol
2173 * @protocol_interface interface implementing the protocol
2174 * @agent_handle handle of the driver
2175 * @controller_handle handle of the controller
2176 * @attributes attributes indicating how to open the protocol
2177 * @return status code
2179 static efi_status_t efi_protocol_open(
2180 struct efi_handler *handler,
2181 void **protocol_interface, void *agent_handle,
2182 void *controller_handle, uint32_t attributes)
2184 struct efi_open_protocol_info_item *item;
2185 struct efi_open_protocol_info_entry *match = NULL;
2186 bool opened_by_driver = false;
2187 bool opened_exclusive = false;
2189 /* If there is no agent, only return the interface */
2193 /* For TEST_PROTOCOL ignore interface attribute */
2194 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2195 *protocol_interface = NULL;
2198 * Check if the protocol is already opened by a driver with the same
2199 * attributes or opened exclusively
2201 list_for_each_entry(item, &handler->open_infos, link) {
2202 if (item->info.agent_handle == agent_handle) {
2203 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2204 (item->info.attributes == attributes))
2205 return EFI_ALREADY_STARTED;
2207 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2208 opened_exclusive = true;
2211 /* Only one controller can open the protocol exclusively */
2212 if (opened_exclusive && attributes &
2213 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2214 return EFI_ACCESS_DENIED;
2216 /* Prepare exclusive opening */
2217 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2218 /* Try to disconnect controllers */
2219 list_for_each_entry(item, &handler->open_infos, link) {
2220 if (item->info.attributes ==
2221 EFI_OPEN_PROTOCOL_BY_DRIVER)
2222 EFI_CALL(efi_disconnect_controller(
2223 item->info.controller_handle,
2224 item->info.agent_handle,
2227 opened_by_driver = false;
2228 /* Check if all controllers are disconnected */
2229 list_for_each_entry(item, &handler->open_infos, link) {
2230 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2231 opened_by_driver = true;
2233 /* Only one controller can be conncected */
2234 if (opened_by_driver)
2235 return EFI_ACCESS_DENIED;
2238 /* Find existing entry */
2239 list_for_each_entry(item, &handler->open_infos, link) {
2240 if (item->info.agent_handle == agent_handle &&
2241 item->info.controller_handle == controller_handle)
2242 match = &item->info;
2244 /* None found, create one */
2246 match = efi_create_open_info(handler);
2248 return EFI_OUT_OF_RESOURCES;
2251 match->agent_handle = agent_handle;
2252 match->controller_handle = controller_handle;
2253 match->attributes = attributes;
2254 match->open_count++;
2257 /* For TEST_PROTOCOL ignore interface attribute. */
2258 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2259 *protocol_interface = handler->protocol_interface;
2265 * Open protocol interface on a handle.
2267 * This function implements the OpenProtocol interface.
2268 * See the Unified Extensible Firmware Interface (UEFI) specification
2271 * @handle handle on which the protocol shall be opened
2272 * @protocol GUID of the protocol
2273 * @protocol_interface interface implementing the protocol
2274 * @agent_handle handle of the driver
2275 * @controller_handle handle of the controller
2276 * @attributes attributes indicating how to open the protocol
2277 * @return status code
2279 static efi_status_t EFIAPI efi_open_protocol(
2280 void *handle, const efi_guid_t *protocol,
2281 void **protocol_interface, void *agent_handle,
2282 void *controller_handle, uint32_t attributes)
2284 struct efi_handler *handler;
2285 efi_status_t r = EFI_INVALID_PARAMETER;
2287 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2288 protocol_interface, agent_handle, controller_handle,
2291 if (!handle || !protocol ||
2292 (!protocol_interface && attributes !=
2293 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2297 switch (attributes) {
2298 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2299 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2300 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2302 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2303 if (controller_handle == handle)
2306 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2307 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2308 /* Check that the controller handle is valid */
2309 if (!efi_search_obj(controller_handle))
2312 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2313 /* Check that the agent handle is valid */
2314 if (!efi_search_obj(agent_handle))
2321 r = efi_search_protocol(handle, protocol, &handler);
2322 if (r != EFI_SUCCESS)
2325 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2326 controller_handle, attributes);
2332 * Get interface of a protocol on a handle.
2334 * This function implements the HandleProtocol service.
2335 * See the Unified Extensible Firmware Interface (UEFI) specification
2338 * @handle handle on which the protocol shall be opened
2339 * @protocol GUID of the protocol
2340 * @protocol_interface interface implementing the protocol
2341 * @return status code
2343 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
2344 const efi_guid_t *protocol,
2345 void **protocol_interface)
2347 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2348 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2351 static efi_status_t efi_bind_controller(
2352 efi_handle_t controller_handle,
2353 efi_handle_t driver_image_handle,
2354 struct efi_device_path *remain_device_path)
2356 struct efi_driver_binding_protocol *binding_protocol;
2359 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2360 &efi_guid_driver_binding_protocol,
2361 (void **)&binding_protocol,
2362 driver_image_handle, NULL,
2363 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2364 if (r != EFI_SUCCESS)
2366 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2368 remain_device_path));
2369 if (r == EFI_SUCCESS)
2370 r = EFI_CALL(binding_protocol->start(binding_protocol,
2372 remain_device_path));
2373 EFI_CALL(efi_close_protocol(driver_image_handle,
2374 &efi_guid_driver_binding_protocol,
2375 driver_image_handle, NULL));
2379 static efi_status_t efi_connect_single_controller(
2380 efi_handle_t controller_handle,
2381 efi_handle_t *driver_image_handle,
2382 struct efi_device_path *remain_device_path)
2384 efi_handle_t *buffer;
2388 size_t connected = 0;
2390 /* Get buffer with all handles with driver binding protocol */
2391 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2392 &efi_guid_driver_binding_protocol,
2393 NULL, &count, &buffer));
2394 if (r != EFI_SUCCESS)
2397 /* Context Override */
2398 if (driver_image_handle) {
2399 for (; *driver_image_handle; ++driver_image_handle) {
2400 for (i = 0; i < count; ++i) {
2401 if (buffer[i] == *driver_image_handle) {
2403 r = efi_bind_controller(
2405 *driver_image_handle,
2406 remain_device_path);
2408 * For drivers that do not support the
2409 * controller or are already connected
2410 * we receive an error code here.
2412 if (r == EFI_SUCCESS)
2420 * TODO: Some overrides are not yet implemented:
2421 * - Platform Driver Override
2422 * - Driver Family Override Search
2423 * - Bus Specific Driver Override
2426 /* Driver Binding Search */
2427 for (i = 0; i < count; ++i) {
2429 r = efi_bind_controller(controller_handle,
2431 remain_device_path);
2432 if (r == EFI_SUCCESS)
2437 efi_free_pool(buffer);
2439 return EFI_NOT_FOUND;
2444 * Connect a controller to a driver.
2446 * This function implements the ConnectController service.
2447 * See the Unified Extensible Firmware Interface (UEFI) specification
2450 * First all driver binding protocol handles are tried for binding drivers.
2451 * Afterwards all handles that have openened a protocol of the controller
2452 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2454 * @controller_handle handle of the controller
2455 * @driver_image_handle handle of the driver
2456 * @remain_device_path device path of a child controller
2457 * @recursive true to connect all child controllers
2458 * @return status code
2460 static efi_status_t EFIAPI efi_connect_controller(
2461 efi_handle_t controller_handle,
2462 efi_handle_t *driver_image_handle,
2463 struct efi_device_path *remain_device_path,
2467 efi_status_t ret = EFI_NOT_FOUND;
2468 struct efi_object *efiobj;
2470 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2471 remain_device_path, recursive);
2473 efiobj = efi_search_obj(controller_handle);
2475 ret = EFI_INVALID_PARAMETER;
2479 r = efi_connect_single_controller(controller_handle,
2480 driver_image_handle,
2481 remain_device_path);
2482 if (r == EFI_SUCCESS)
2485 struct efi_handler *handler;
2486 struct efi_open_protocol_info_item *item;
2488 list_for_each_entry(handler, &efiobj->protocols, link) {
2489 list_for_each_entry(item, &handler->open_infos, link) {
2490 if (item->info.attributes &
2491 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2492 r = EFI_CALL(efi_connect_controller(
2493 item->info.controller_handle,
2494 driver_image_handle,
2497 if (r == EFI_SUCCESS)
2503 /* Check for child controller specified by end node */
2504 if (ret != EFI_SUCCESS && remain_device_path &&
2505 remain_device_path->type == DEVICE_PATH_TYPE_END)
2508 return EFI_EXIT(ret);
2511 static const struct efi_boot_services efi_boot_services = {
2513 .headersize = sizeof(struct efi_table_hdr),
2515 .raise_tpl = efi_raise_tpl,
2516 .restore_tpl = efi_restore_tpl,
2517 .allocate_pages = efi_allocate_pages_ext,
2518 .free_pages = efi_free_pages_ext,
2519 .get_memory_map = efi_get_memory_map_ext,
2520 .allocate_pool = efi_allocate_pool_ext,
2521 .free_pool = efi_free_pool_ext,
2522 .create_event = efi_create_event_ext,
2523 .set_timer = efi_set_timer_ext,
2524 .wait_for_event = efi_wait_for_event,
2525 .signal_event = efi_signal_event_ext,
2526 .close_event = efi_close_event,
2527 .check_event = efi_check_event,
2528 .install_protocol_interface = efi_install_protocol_interface,
2529 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2530 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2531 .handle_protocol = efi_handle_protocol,
2533 .register_protocol_notify = efi_register_protocol_notify,
2534 .locate_handle = efi_locate_handle_ext,
2535 .locate_device_path = efi_locate_device_path,
2536 .install_configuration_table = efi_install_configuration_table_ext,
2537 .load_image = efi_load_image,
2538 .start_image = efi_start_image,
2540 .unload_image = efi_unload_image,
2541 .exit_boot_services = efi_exit_boot_services,
2542 .get_next_monotonic_count = efi_get_next_monotonic_count,
2544 .set_watchdog_timer = efi_set_watchdog_timer,
2545 .connect_controller = efi_connect_controller,
2546 .disconnect_controller = efi_disconnect_controller,
2547 .open_protocol = efi_open_protocol,
2548 .close_protocol = efi_close_protocol,
2549 .open_protocol_information = efi_open_protocol_information,
2550 .protocols_per_handle = efi_protocols_per_handle,
2551 .locate_handle_buffer = efi_locate_handle_buffer,
2552 .locate_protocol = efi_locate_protocol,
2553 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2554 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2555 .calculate_crc32 = efi_calculate_crc32,
2556 .copy_mem = efi_copy_mem,
2557 .set_mem = efi_set_mem,
2561 static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
2563 struct efi_system_table __efi_runtime_data systab = {
2565 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2566 .revision = 0x20005, /* 2.5 */
2567 .headersize = sizeof(struct efi_table_hdr),
2569 .fw_vendor = (long)firmware_vendor,
2570 .con_in = (void*)&efi_con_in,
2571 .con_out = (void*)&efi_con_out,
2572 .std_err = (void*)&efi_con_out,
2573 .runtime = (void*)&efi_runtime_services,
2574 .boottime = (void*)&efi_boot_services,
2576 .tables = (void*)efi_conf_table,