1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application boot time services
5 * Copyright (c) 2016 Alexander Graf
10 #include <efi_loader.h>
11 #include <environment.h>
13 #include <linux/libfdt_env.h>
14 #include <u-boot/crc.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 /* Task priority level */
22 static efi_uintn_t efi_tpl = TPL_APPLICATION;
24 /* This list contains all the EFI objects our payload has access to */
25 LIST_HEAD(efi_obj_list);
27 /* List of all events */
28 LIST_HEAD(efi_events);
30 /* List of all events registered by RegisterProtocolNotify() */
31 LIST_HEAD(efi_register_notify_events);
33 /* Handle of the currently executing image */
34 static efi_handle_t current_image;
37 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
38 * we need to do trickery with caches. Since we don't want to break the EFI
39 * aware boot path, only apply hacks when loading exiting directly (breaking
40 * direct Linux EFI booting along the way - oh well).
42 static bool efi_is_direct_boot = true;
46 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
47 * fixed when compiling U-Boot. However, the payload does not know about that
48 * restriction so we need to manually swap its and our view of that register on
49 * EFI callback entry/exit.
51 static volatile void *efi_gd, *app_gd;
54 /* 1 if inside U-Boot code, 0 if inside EFI payload code */
55 static int entry_count = 1;
56 static int nesting_level;
57 /* GUID of the device tree table */
58 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
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 /* event group ExitBootServices() invoked */
64 const efi_guid_t efi_guid_event_group_exit_boot_services =
65 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
66 /* event group SetVirtualAddressMap() invoked */
67 const efi_guid_t efi_guid_event_group_virtual_address_change =
68 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
69 /* event group memory map changed */
70 const efi_guid_t efi_guid_event_group_memory_map_change =
71 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
72 /* event group boot manager about to boot */
73 const efi_guid_t efi_guid_event_group_ready_to_boot =
74 EFI_EVENT_GROUP_READY_TO_BOOT;
75 /* event group ResetSystem() invoked (before ExitBootServices) */
76 const efi_guid_t efi_guid_event_group_reset_system =
77 EFI_EVENT_GROUP_RESET_SYSTEM;
79 static efi_status_t EFIAPI efi_disconnect_controller(
80 efi_handle_t controller_handle,
81 efi_handle_t driver_image_handle,
82 efi_handle_t child_handle);
84 /* Called on every callback entry */
85 int __efi_entry_check(void)
87 int ret = entry_count++ == 0;
96 /* Called on every callback exit */
97 int __efi_exit_check(void)
99 int ret = --entry_count == 0;
106 /* Called from do_bootefi_exec() */
107 void efi_save_gd(void)
115 * Special case handler for error/abort that just forces things back to u-boot
116 * world so we can dump out an abort message, without any care about returning
117 * back to UEFI world.
119 void efi_restore_gd(void)
122 /* Only restore if we're already in EFI context */
130 * indent_string() - returns a string for indenting with two spaces per level
131 * @level: indent level
133 * A maximum of ten indent levels is supported. Higher indent levels will be
136 * Return: A string for indenting with two spaces per level is
139 static const char *indent_string(int level)
141 const char *indent = " ";
142 const int max = strlen(indent);
144 level = min(max, level * 2);
145 return &indent[max - level];
148 const char *__efi_nesting(void)
150 return indent_string(nesting_level);
153 const char *__efi_nesting_inc(void)
155 return indent_string(nesting_level++);
158 const char *__efi_nesting_dec(void)
160 return indent_string(--nesting_level);
164 * efi_queue_event() - queue an EFI event
165 * @event: event to signal
166 * @check_tpl: check the TPL level
168 * This function queues the notification function of the event for future
171 * The notification function is called if the task priority level of the event
172 * is higher than the current task priority level.
174 * For the SignalEvent service see efi_signal_event_ext.
177 static void efi_queue_event(struct efi_event *event, bool check_tpl)
179 if (event->notify_function) {
180 event->is_queued = true;
182 if (check_tpl && efi_tpl >= event->notify_tpl)
184 EFI_CALL_VOID(event->notify_function(event,
185 event->notify_context));
187 event->is_queued = false;
191 * is_valid_tpl() - check if the task priority level is valid
193 * @tpl: TPL level to check
194 * Return: status code
196 efi_status_t is_valid_tpl(efi_uintn_t tpl)
199 case TPL_APPLICATION:
205 return EFI_INVALID_PARAMETER;
210 * efi_signal_event() - signal an EFI event
211 * @event: event to signal
212 * @check_tpl: check the TPL level
214 * This function signals an event. If the event belongs to an event group all
215 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
216 * their notification function is queued.
218 * For the SignalEvent service see efi_signal_event_ext.
220 void efi_signal_event(struct efi_event *event, bool check_tpl)
223 struct efi_event *evt;
226 * The signaled state has to set before executing any
227 * notification function
229 list_for_each_entry(evt, &efi_events, link) {
230 if (!evt->group || guidcmp(evt->group, event->group))
232 if (evt->is_signaled)
234 evt->is_signaled = true;
235 if (evt->type & EVT_NOTIFY_SIGNAL &&
236 evt->notify_function)
237 evt->is_queued = true;
239 list_for_each_entry(evt, &efi_events, link) {
240 if (!evt->group || guidcmp(evt->group, event->group))
243 efi_queue_event(evt, check_tpl);
246 event->is_signaled = true;
247 if (event->type & EVT_NOTIFY_SIGNAL)
248 efi_queue_event(event, check_tpl);
253 * efi_raise_tpl() - raise the task priority level
254 * @new_tpl: new value of the task priority level
256 * This function implements the RaiseTpl service.
258 * See the Unified Extensible Firmware Interface (UEFI) specification for
261 * Return: old value of the task priority level
263 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
265 efi_uintn_t old_tpl = efi_tpl;
267 EFI_ENTRY("0x%zx", new_tpl);
269 if (new_tpl < efi_tpl)
270 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
272 if (efi_tpl > TPL_HIGH_LEVEL)
273 efi_tpl = TPL_HIGH_LEVEL;
275 EFI_EXIT(EFI_SUCCESS);
280 * efi_restore_tpl() - lower the task priority level
281 * @old_tpl: value of the task priority level to be restored
283 * This function implements the RestoreTpl service.
285 * See the Unified Extensible Firmware Interface (UEFI) specification for
288 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
290 EFI_ENTRY("0x%zx", old_tpl);
292 if (old_tpl > efi_tpl)
293 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
295 if (efi_tpl > TPL_HIGH_LEVEL)
296 efi_tpl = TPL_HIGH_LEVEL;
299 * Lowering the TPL may have made queued events eligible for execution.
303 EFI_EXIT(EFI_SUCCESS);
307 * efi_allocate_pages_ext() - allocate memory pages
308 * @type: type of allocation to be performed
309 * @memory_type: usage type of the allocated memory
310 * @pages: number of pages to be allocated
311 * @memory: allocated memory
313 * This function implements the AllocatePages service.
315 * See the Unified Extensible Firmware Interface (UEFI) specification for
318 * Return: status code
320 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
326 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
327 r = efi_allocate_pages(type, memory_type, pages, memory);
332 * efi_free_pages_ext() - Free memory pages.
333 * @memory: start of the memory area to be freed
334 * @pages: number of pages to be freed
336 * This function implements the FreePages service.
338 * See the Unified Extensible Firmware Interface (UEFI) specification for
341 * Return: status code
343 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
348 EFI_ENTRY("%llx, 0x%zx", memory, pages);
349 r = efi_free_pages(memory, pages);
354 * efi_get_memory_map_ext() - get map describing memory usage
355 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
356 * on exit the size of the copied memory map
357 * @memory_map: buffer to which the memory map is written
358 * @map_key: key for the memory map
359 * @descriptor_size: size of an individual memory descriptor
360 * @descriptor_version: version number of the memory descriptor structure
362 * This function implements the GetMemoryMap service.
364 * See the Unified Extensible Firmware Interface (UEFI) specification for
367 * Return: status code
369 static efi_status_t EFIAPI efi_get_memory_map_ext(
370 efi_uintn_t *memory_map_size,
371 struct efi_mem_desc *memory_map,
372 efi_uintn_t *map_key,
373 efi_uintn_t *descriptor_size,
374 uint32_t *descriptor_version)
378 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
379 map_key, descriptor_size, descriptor_version);
380 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
381 descriptor_size, descriptor_version);
386 * efi_allocate_pool_ext() - allocate memory from pool
387 * @pool_type: type of the pool from which memory is to be allocated
388 * @size: number of bytes to be allocated
389 * @buffer: allocated memory
391 * This function implements the AllocatePool service.
393 * See the Unified Extensible Firmware Interface (UEFI) specification for
396 * Return: status code
398 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
404 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
405 r = efi_allocate_pool(pool_type, size, buffer);
410 * efi_free_pool_ext() - free memory from pool
411 * @buffer: start of memory to be freed
413 * This function implements the FreePool service.
415 * See the Unified Extensible Firmware Interface (UEFI) specification for
418 * Return: status code
420 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
424 EFI_ENTRY("%p", buffer);
425 r = efi_free_pool(buffer);
430 * efi_add_handle() - add a new handle to the object list
432 * @handle: handle to be added
434 * The protocols list is initialized. The handle is added to the list of known
437 void efi_add_handle(efi_handle_t handle)
441 INIT_LIST_HEAD(&handle->protocols);
442 list_add_tail(&handle->link, &efi_obj_list);
446 * efi_create_handle() - create handle
447 * @handle: new handle
449 * Return: status code
451 efi_status_t efi_create_handle(efi_handle_t *handle)
453 struct efi_object *obj;
455 obj = calloc(1, sizeof(struct efi_object));
457 return EFI_OUT_OF_RESOURCES;
466 * efi_search_protocol() - find a protocol on a handle.
468 * @protocol_guid: GUID of the protocol
469 * @handler: reference to the protocol
471 * Return: status code
473 efi_status_t efi_search_protocol(const efi_handle_t handle,
474 const efi_guid_t *protocol_guid,
475 struct efi_handler **handler)
477 struct efi_object *efiobj;
478 struct list_head *lhandle;
480 if (!handle || !protocol_guid)
481 return EFI_INVALID_PARAMETER;
482 efiobj = efi_search_obj(handle);
484 return EFI_INVALID_PARAMETER;
485 list_for_each(lhandle, &efiobj->protocols) {
486 struct efi_handler *protocol;
488 protocol = list_entry(lhandle, struct efi_handler, link);
489 if (!guidcmp(protocol->guid, protocol_guid)) {
495 return EFI_NOT_FOUND;
499 * efi_remove_protocol() - delete protocol from a handle
500 * @handle: handle from which the protocol shall be deleted
501 * @protocol: GUID of the protocol to be deleted
502 * @protocol_interface: interface of the protocol implementation
504 * Return: status code
506 efi_status_t efi_remove_protocol(const efi_handle_t handle,
507 const efi_guid_t *protocol,
508 void *protocol_interface)
510 struct efi_handler *handler;
513 ret = efi_search_protocol(handle, protocol, &handler);
514 if (ret != EFI_SUCCESS)
516 if (handler->protocol_interface != protocol_interface)
517 return EFI_NOT_FOUND;
518 list_del(&handler->link);
524 * efi_remove_all_protocols() - delete all protocols from a handle
525 * @handle: handle from which the protocols shall be deleted
527 * Return: status code
529 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
531 struct efi_object *efiobj;
532 struct efi_handler *protocol;
533 struct efi_handler *pos;
535 efiobj = efi_search_obj(handle);
537 return EFI_INVALID_PARAMETER;
538 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
541 ret = efi_remove_protocol(handle, protocol->guid,
542 protocol->protocol_interface);
543 if (ret != EFI_SUCCESS)
550 * efi_delete_handle() - delete handle
552 * @obj: handle to delete
554 void efi_delete_handle(efi_handle_t handle)
558 efi_remove_all_protocols(handle);
559 list_del(&handle->link);
564 * efi_is_event() - check if a pointer is a valid event
565 * @event: pointer to check
567 * Return: status code
569 static efi_status_t efi_is_event(const struct efi_event *event)
571 const struct efi_event *evt;
574 return EFI_INVALID_PARAMETER;
575 list_for_each_entry(evt, &efi_events, link) {
579 return EFI_INVALID_PARAMETER;
583 * efi_create_event() - create an event
584 * @type: type of the event to create
585 * @notify_tpl: task priority level of the event
586 * @notify_function: notification function of the event
587 * @notify_context: pointer passed to the notification function
588 * @group: event group
589 * @event: created event
591 * This function is used inside U-Boot code to create an event.
593 * For the API function implementing the CreateEvent service see
594 * efi_create_event_ext.
596 * Return: status code
598 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
599 void (EFIAPI *notify_function) (
600 struct efi_event *event,
602 void *notify_context, efi_guid_t *group,
603 struct efi_event **event)
605 struct efi_event *evt;
608 return EFI_INVALID_PARAMETER;
613 case EVT_NOTIFY_SIGNAL:
614 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
615 case EVT_NOTIFY_WAIT:
616 case EVT_TIMER | EVT_NOTIFY_WAIT:
617 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
618 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
621 return EFI_INVALID_PARAMETER;
624 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
625 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
626 return EFI_INVALID_PARAMETER;
628 evt = calloc(1, sizeof(struct efi_event));
630 return EFI_OUT_OF_RESOURCES;
632 evt->notify_tpl = notify_tpl;
633 evt->notify_function = notify_function;
634 evt->notify_context = notify_context;
636 /* Disable timers on boot up */
637 evt->trigger_next = -1ULL;
638 evt->is_queued = false;
639 evt->is_signaled = false;
640 list_add_tail(&evt->link, &efi_events);
646 * efi_create_event_ex() - create an event in a group
647 * @type: type of the event to create
648 * @notify_tpl: task priority level of the event
649 * @notify_function: notification function of the event
650 * @notify_context: pointer passed to the notification function
651 * @event: created event
652 * @event_group: event group
654 * This function implements the CreateEventEx service.
656 * See the Unified Extensible Firmware Interface (UEFI) specification for
659 * Return: status code
661 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
662 void (EFIAPI *notify_function) (
663 struct efi_event *event,
665 void *notify_context,
666 efi_guid_t *event_group,
667 struct efi_event **event)
671 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
672 notify_context, event_group);
675 * The allowable input parameters are the same as in CreateEvent()
676 * except for the following two disallowed event types.
679 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
680 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
681 ret = EFI_INVALID_PARAMETER;
685 ret = efi_create_event(type, notify_tpl, notify_function,
686 notify_context, event_group, event);
688 return EFI_EXIT(ret);
692 * efi_create_event_ext() - create an event
693 * @type: type of the event to create
694 * @notify_tpl: task priority level of the event
695 * @notify_function: notification function of the event
696 * @notify_context: pointer passed to the notification function
697 * @event: created event
699 * This function implements the CreateEvent service.
701 * See the Unified Extensible Firmware Interface (UEFI) specification for
704 * Return: status code
706 static efi_status_t EFIAPI efi_create_event_ext(
707 uint32_t type, efi_uintn_t notify_tpl,
708 void (EFIAPI *notify_function) (
709 struct efi_event *event,
711 void *notify_context, struct efi_event **event)
713 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
715 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
716 notify_context, NULL, event));
720 * efi_timer_check() - check if a timer event has occurred
722 * Check if a timer event has occurred or a queued notification function should
725 * Our timers have to work without interrupts, so we check whenever keyboard
726 * input or disk accesses happen if enough time elapsed for them to fire.
728 void efi_timer_check(void)
730 struct efi_event *evt;
731 u64 now = timer_get_us();
733 list_for_each_entry(evt, &efi_events, link) {
735 efi_queue_event(evt, true);
736 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
738 switch (evt->trigger_type) {
739 case EFI_TIMER_RELATIVE:
740 evt->trigger_type = EFI_TIMER_STOP;
742 case EFI_TIMER_PERIODIC:
743 evt->trigger_next += evt->trigger_time;
748 evt->is_signaled = false;
749 efi_signal_event(evt, true);
755 * efi_set_timer() - set the trigger time for a timer event or stop the event
756 * @event: event for which the timer is set
757 * @type: type of the timer
758 * @trigger_time: trigger period in multiples of 100 ns
760 * This is the function for internal usage in U-Boot. For the API function
761 * implementing the SetTimer service see efi_set_timer_ext.
763 * Return: status code
765 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
766 uint64_t trigger_time)
768 /* Check that the event is valid */
769 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
770 return EFI_INVALID_PARAMETER;
773 * The parameter defines a multiple of 100 ns.
774 * We use multiples of 1000 ns. So divide by 10.
776 do_div(trigger_time, 10);
780 event->trigger_next = -1ULL;
782 case EFI_TIMER_PERIODIC:
783 case EFI_TIMER_RELATIVE:
784 event->trigger_next = timer_get_us() + trigger_time;
787 return EFI_INVALID_PARAMETER;
789 event->trigger_type = type;
790 event->trigger_time = trigger_time;
791 event->is_signaled = false;
796 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
798 * @event: event for which the timer is set
799 * @type: type of the timer
800 * @trigger_time: trigger period in multiples of 100 ns
802 * This function implements the SetTimer service.
804 * See the Unified Extensible Firmware Interface (UEFI) specification for
808 * Return: status code
810 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
811 enum efi_timer_delay type,
812 uint64_t trigger_time)
814 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
815 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
819 * efi_wait_for_event() - wait for events to be signaled
820 * @num_events: number of events to be waited for
821 * @event: events to be waited for
822 * @index: index of the event that was signaled
824 * This function implements the WaitForEvent service.
826 * See the Unified Extensible Firmware Interface (UEFI) specification for
829 * Return: status code
831 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
832 struct efi_event **event,
837 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
839 /* Check parameters */
840 if (!num_events || !event)
841 return EFI_EXIT(EFI_INVALID_PARAMETER);
843 if (efi_tpl != TPL_APPLICATION)
844 return EFI_EXIT(EFI_UNSUPPORTED);
845 for (i = 0; i < num_events; ++i) {
846 if (efi_is_event(event[i]) != EFI_SUCCESS)
847 return EFI_EXIT(EFI_INVALID_PARAMETER);
848 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
849 return EFI_EXIT(EFI_INVALID_PARAMETER);
850 if (!event[i]->is_signaled)
851 efi_queue_event(event[i], true);
854 /* Wait for signal */
856 for (i = 0; i < num_events; ++i) {
857 if (event[i]->is_signaled)
860 /* Allow events to occur. */
866 * Reset the signal which is passed to the caller to allow periodic
869 event[i]->is_signaled = false;
873 return EFI_EXIT(EFI_SUCCESS);
877 * efi_signal_event_ext() - signal an EFI event
878 * @event: event to signal
880 * This function implements the SignalEvent service.
882 * See the Unified Extensible Firmware Interface (UEFI) specification for
885 * This functions sets the signaled state of the event and queues the
886 * notification function for execution.
888 * Return: status code
890 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
892 EFI_ENTRY("%p", event);
893 if (efi_is_event(event) != EFI_SUCCESS)
894 return EFI_EXIT(EFI_INVALID_PARAMETER);
895 efi_signal_event(event, true);
896 return EFI_EXIT(EFI_SUCCESS);
900 * efi_close_event() - close an EFI event
901 * @event: event to close
903 * This function implements the CloseEvent service.
905 * See the Unified Extensible Firmware Interface (UEFI) specification for
908 * Return: status code
910 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
912 struct efi_register_notify_event *item, *next;
914 EFI_ENTRY("%p", event);
915 if (efi_is_event(event) != EFI_SUCCESS)
916 return EFI_EXIT(EFI_INVALID_PARAMETER);
918 /* Remove protocol notify registrations for the event */
919 list_for_each_entry_safe(item, next, &efi_register_notify_events,
921 if (event == item->event) {
922 list_del(&item->link);
927 list_del(&event->link);
929 return EFI_EXIT(EFI_SUCCESS);
933 * efi_check_event() - check if an event is signaled
934 * @event: event to check
936 * This function implements the CheckEvent service.
938 * See the Unified Extensible Firmware Interface (UEFI) specification for
941 * If an event is not signaled yet, the notification function is queued. The
942 * signaled state is cleared.
944 * Return: status code
946 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
948 EFI_ENTRY("%p", event);
950 if (efi_is_event(event) != EFI_SUCCESS ||
951 event->type & EVT_NOTIFY_SIGNAL)
952 return EFI_EXIT(EFI_INVALID_PARAMETER);
953 if (!event->is_signaled)
954 efi_queue_event(event, true);
955 if (event->is_signaled) {
956 event->is_signaled = false;
957 return EFI_EXIT(EFI_SUCCESS);
959 return EFI_EXIT(EFI_NOT_READY);
963 * efi_search_obj() - find the internal EFI object for a handle
964 * @handle: handle to find
968 struct efi_object *efi_search_obj(const efi_handle_t handle)
970 struct efi_object *efiobj;
975 list_for_each_entry(efiobj, &efi_obj_list, link) {
976 if (efiobj == handle)
983 * efi_open_protocol_info_entry() - create open protocol info entry and add it
985 * @handler: handler of a protocol
987 * Return: open protocol info entry
989 static struct efi_open_protocol_info_entry *efi_create_open_info(
990 struct efi_handler *handler)
992 struct efi_open_protocol_info_item *item;
994 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
997 /* Append the item to the open protocol info list. */
998 list_add_tail(&item->link, &handler->open_infos);
1004 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1005 * @item: open protocol info entry to delete
1007 * Return: status code
1009 static efi_status_t efi_delete_open_info(
1010 struct efi_open_protocol_info_item *item)
1012 list_del(&item->link);
1018 * efi_add_protocol() - install new protocol on a handle
1019 * @handle: handle on which the protocol shall be installed
1020 * @protocol: GUID of the protocol to be installed
1021 * @protocol_interface: interface of the protocol implementation
1023 * Return: status code
1025 efi_status_t efi_add_protocol(const efi_handle_t handle,
1026 const efi_guid_t *protocol,
1027 void *protocol_interface)
1029 struct efi_object *efiobj;
1030 struct efi_handler *handler;
1032 struct efi_register_notify_event *event;
1034 efiobj = efi_search_obj(handle);
1036 return EFI_INVALID_PARAMETER;
1037 ret = efi_search_protocol(handle, protocol, NULL);
1038 if (ret != EFI_NOT_FOUND)
1039 return EFI_INVALID_PARAMETER;
1040 handler = calloc(1, sizeof(struct efi_handler));
1042 return EFI_OUT_OF_RESOURCES;
1043 handler->guid = protocol;
1044 handler->protocol_interface = protocol_interface;
1045 INIT_LIST_HEAD(&handler->open_infos);
1046 list_add_tail(&handler->link, &efiobj->protocols);
1048 /* Notify registered events */
1049 list_for_each_entry(event, &efi_register_notify_events, link) {
1050 if (!guidcmp(protocol, &event->protocol))
1051 efi_signal_event(event->event, true);
1054 if (!guidcmp(&efi_guid_device_path, protocol))
1055 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1060 * efi_install_protocol_interface() - install protocol interface
1061 * @handle: handle on which the protocol shall be installed
1062 * @protocol: GUID of the protocol to be installed
1063 * @protocol_interface_type: type of the interface to be installed,
1064 * always EFI_NATIVE_INTERFACE
1065 * @protocol_interface: interface of the protocol implementation
1067 * This function implements the InstallProtocolInterface service.
1069 * See the Unified Extensible Firmware Interface (UEFI) specification for
1072 * Return: status code
1074 static efi_status_t EFIAPI efi_install_protocol_interface(
1075 efi_handle_t *handle, const efi_guid_t *protocol,
1076 int protocol_interface_type, void *protocol_interface)
1080 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1081 protocol_interface);
1083 if (!handle || !protocol ||
1084 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1085 r = EFI_INVALID_PARAMETER;
1089 /* Create new handle if requested. */
1091 r = efi_create_handle(handle);
1092 if (r != EFI_SUCCESS)
1094 EFI_PRINT("new handle %p\n", *handle);
1096 EFI_PRINT("handle %p\n", *handle);
1098 /* Add new protocol */
1099 r = efi_add_protocol(*handle, protocol, protocol_interface);
1105 * efi_get_drivers() - get all drivers associated to a controller
1106 * @handle: handle of the controller
1107 * @protocol: protocol GUID (optional)
1108 * @number_of_drivers: number of child controllers
1109 * @driver_handle_buffer: handles of the the drivers
1111 * The allocated buffer has to be freed with free().
1113 * Return: status code
1115 static efi_status_t efi_get_drivers(efi_handle_t handle,
1116 const efi_guid_t *protocol,
1117 efi_uintn_t *number_of_drivers,
1118 efi_handle_t **driver_handle_buffer)
1120 struct efi_handler *handler;
1121 struct efi_open_protocol_info_item *item;
1122 efi_uintn_t count = 0, i;
1125 /* Count all driver associations */
1126 list_for_each_entry(handler, &handle->protocols, link) {
1127 if (protocol && guidcmp(handler->guid, protocol))
1129 list_for_each_entry(item, &handler->open_infos, link) {
1130 if (item->info.attributes &
1131 EFI_OPEN_PROTOCOL_BY_DRIVER)
1136 * Create buffer. In case of duplicate driver assignments the buffer
1137 * will be too large. But that does not harm.
1139 *number_of_drivers = 0;
1140 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1141 if (!*driver_handle_buffer)
1142 return EFI_OUT_OF_RESOURCES;
1143 /* Collect unique driver handles */
1144 list_for_each_entry(handler, &handle->protocols, link) {
1145 if (protocol && guidcmp(handler->guid, protocol))
1147 list_for_each_entry(item, &handler->open_infos, link) {
1148 if (item->info.attributes &
1149 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1150 /* Check this is a new driver */
1152 for (i = 0; i < *number_of_drivers; ++i) {
1153 if ((*driver_handle_buffer)[i] ==
1154 item->info.agent_handle)
1157 /* Copy handle to buffer */
1159 i = (*number_of_drivers)++;
1160 (*driver_handle_buffer)[i] =
1161 item->info.agent_handle;
1170 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1171 * @handle: handle of the controller
1172 * @protocol: protocol GUID (optional)
1173 * @child_handle: handle of the child to destroy
1175 * This function implements the DisconnectController service.
1177 * See the Unified Extensible Firmware Interface (UEFI) specification for
1180 * Return: status code
1182 static efi_status_t efi_disconnect_all_drivers
1183 (efi_handle_t handle,
1184 const efi_guid_t *protocol,
1185 efi_handle_t child_handle)
1187 efi_uintn_t number_of_drivers;
1188 efi_handle_t *driver_handle_buffer;
1189 efi_status_t r, ret;
1191 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
1192 &driver_handle_buffer);
1193 if (ret != EFI_SUCCESS)
1196 ret = EFI_NOT_FOUND;
1197 while (number_of_drivers) {
1198 r = EFI_CALL(efi_disconnect_controller(
1200 driver_handle_buffer[--number_of_drivers],
1202 if (r == EFI_SUCCESS)
1205 free(driver_handle_buffer);
1210 * efi_uninstall_protocol() - uninstall protocol interface
1212 * @handle: handle from which the protocol shall be removed
1213 * @protocol: GUID of the protocol to be removed
1214 * @protocol_interface: interface to be removed
1216 * This function DOES NOT delete a handle without installed protocol.
1218 * Return: status code
1220 static efi_status_t efi_uninstall_protocol
1221 (efi_handle_t handle, const efi_guid_t *protocol,
1222 void *protocol_interface)
1224 struct efi_object *efiobj;
1225 struct efi_handler *handler;
1226 struct efi_open_protocol_info_item *item;
1227 struct efi_open_protocol_info_item *pos;
1231 efiobj = efi_search_obj(handle);
1233 r = EFI_INVALID_PARAMETER;
1236 /* Find the protocol on the handle */
1237 r = efi_search_protocol(handle, protocol, &handler);
1238 if (r != EFI_SUCCESS)
1240 /* Disconnect controllers */
1241 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1242 if (!list_empty(&handler->open_infos)) {
1243 r = EFI_ACCESS_DENIED;
1246 /* Close protocol */
1247 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1248 if (item->info.attributes ==
1249 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1250 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1251 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1252 list_del(&item->link);
1254 if (!list_empty(&handler->open_infos)) {
1255 r = EFI_ACCESS_DENIED;
1258 r = efi_remove_protocol(handle, protocol, protocol_interface);
1264 * efi_uninstall_protocol_interface() - uninstall protocol interface
1265 * @handle: handle from which the protocol shall be removed
1266 * @protocol: GUID of the protocol to be removed
1267 * @protocol_interface: interface to be removed
1269 * This function implements the UninstallProtocolInterface service.
1271 * See the Unified Extensible Firmware Interface (UEFI) specification for
1274 * Return: status code
1276 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1277 (efi_handle_t handle, const efi_guid_t *protocol,
1278 void *protocol_interface)
1282 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1284 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1285 if (ret != EFI_SUCCESS)
1288 /* If the last protocol has been removed, delete the handle. */
1289 if (list_empty(&handle->protocols)) {
1290 list_del(&handle->link);
1294 return EFI_EXIT(ret);
1298 * efi_register_protocol_notify() - register an event for notification when a
1299 * protocol is installed.
1300 * @protocol: GUID of the protocol whose installation shall be notified
1301 * @event: event to be signaled upon installation of the protocol
1302 * @registration: key for retrieving the registration information
1304 * This function implements the RegisterProtocolNotify service.
1305 * See the Unified Extensible Firmware Interface (UEFI) specification
1308 * Return: status code
1310 static efi_status_t EFIAPI efi_register_protocol_notify(
1311 const efi_guid_t *protocol,
1312 struct efi_event *event,
1313 void **registration)
1315 struct efi_register_notify_event *item;
1316 efi_status_t ret = EFI_SUCCESS;
1318 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1320 if (!protocol || !event || !registration) {
1321 ret = EFI_INVALID_PARAMETER;
1325 item = calloc(1, sizeof(struct efi_register_notify_event));
1327 ret = EFI_OUT_OF_RESOURCES;
1331 item->event = event;
1332 memcpy(&item->protocol, protocol, sizeof(efi_guid_t));
1334 list_add_tail(&item->link, &efi_register_notify_events);
1336 *registration = item;
1338 return EFI_EXIT(ret);
1342 * efi_search() - determine if an EFI handle implements a protocol
1343 * @search_type: selection criterion
1344 * @protocol: GUID of the protocol
1345 * @search_key: registration key
1348 * See the documentation of the LocateHandle service in the UEFI specification.
1350 * Return: 0 if the handle implements the protocol
1352 static int efi_search(enum efi_locate_search_type search_type,
1353 const efi_guid_t *protocol, efi_handle_t handle)
1357 switch (search_type) {
1360 case BY_REGISTER_NOTIFY:
1362 ret = efi_search_protocol(handle, protocol, NULL);
1363 return (ret != EFI_SUCCESS);
1365 /* Invalid search type */
1371 * efi_locate_handle() - locate handles implementing a protocol
1373 * @search_type: selection criterion
1374 * @protocol: GUID of the protocol
1375 * @search_key: registration key
1376 * @buffer_size: size of the buffer to receive the handles in bytes
1377 * @buffer: buffer to receive the relevant handles
1379 * This function is meant for U-Boot internal calls. For the API implementation
1380 * of the LocateHandle service see efi_locate_handle_ext.
1382 * Return: status code
1384 static efi_status_t efi_locate_handle(
1385 enum efi_locate_search_type search_type,
1386 const efi_guid_t *protocol, void *search_key,
1387 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1389 struct efi_object *efiobj;
1390 efi_uintn_t size = 0;
1391 struct efi_register_notify_event *item, *event = NULL;
1393 /* Check parameters */
1394 switch (search_type) {
1397 case BY_REGISTER_NOTIFY:
1399 return EFI_INVALID_PARAMETER;
1400 /* Check that the registration key is valid */
1401 list_for_each_entry(item, &efi_register_notify_events, link) {
1403 (struct efi_register_notify_event *)search_key) {
1409 return EFI_INVALID_PARAMETER;
1411 protocol = &event->protocol;
1415 return EFI_INVALID_PARAMETER;
1418 return EFI_INVALID_PARAMETER;
1421 /* Count how much space we need */
1422 list_for_each_entry(efiobj, &efi_obj_list, link) {
1423 if (!efi_search(search_type, protocol, efiobj))
1424 size += sizeof(void *);
1428 return EFI_NOT_FOUND;
1431 return EFI_INVALID_PARAMETER;
1433 if (*buffer_size < size) {
1434 *buffer_size = size;
1435 return EFI_BUFFER_TOO_SMALL;
1438 *buffer_size = size;
1440 /* The buffer size is sufficient but there is no buffer */
1442 return EFI_INVALID_PARAMETER;
1444 /* Then fill the array */
1445 list_for_each_entry(efiobj, &efi_obj_list, link) {
1446 if (!efi_search(search_type, protocol, efiobj))
1454 * efi_locate_handle_ext() - locate handles implementing a protocol.
1455 * @search_type: selection criterion
1456 * @protocol: GUID of the protocol
1457 * @search_key: registration key
1458 * @buffer_size: size of the buffer to receive the handles in bytes
1459 * @buffer: buffer to receive the relevant handles
1461 * This function implements the LocateHandle service.
1463 * See the Unified Extensible Firmware Interface (UEFI) specification for
1466 * Return: 0 if the handle implements the protocol
1468 static efi_status_t EFIAPI efi_locate_handle_ext(
1469 enum efi_locate_search_type search_type,
1470 const efi_guid_t *protocol, void *search_key,
1471 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1473 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1474 buffer_size, buffer);
1476 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1477 buffer_size, buffer));
1481 * efi_remove_configuration_table() - collapses configuration table entries,
1484 * @i: index of the table entry to be removed
1486 static void efi_remove_configuration_table(int i)
1488 struct efi_configuration_table *this = &systab.tables[i];
1489 struct efi_configuration_table *next = &systab.tables[i + 1];
1490 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1492 memmove(this, next, (ulong)end - (ulong)next);
1497 * efi_install_configuration_table() - adds, updates, or removes a
1498 * configuration table
1499 * @guid: GUID of the installed table
1500 * @table: table to be installed
1502 * This function is used for internal calls. For the API implementation of the
1503 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1505 * Return: status code
1507 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1510 struct efi_event *evt;
1514 return EFI_INVALID_PARAMETER;
1516 /* Check for GUID override */
1517 for (i = 0; i < systab.nr_tables; i++) {
1518 if (!guidcmp(guid, &systab.tables[i].guid)) {
1520 systab.tables[i].table = table;
1522 efi_remove_configuration_table(i);
1528 return EFI_NOT_FOUND;
1530 /* No override, check for overflow */
1531 if (i >= EFI_MAX_CONFIGURATION_TABLES)
1532 return EFI_OUT_OF_RESOURCES;
1534 /* Add a new entry */
1535 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1536 systab.tables[i].table = table;
1537 systab.nr_tables = i + 1;
1540 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1541 efi_update_table_header_crc32(&systab.hdr);
1543 /* Notify that the configuration table was changed */
1544 list_for_each_entry(evt, &efi_events, link) {
1545 if (evt->group && !guidcmp(evt->group, guid)) {
1546 efi_signal_event(evt, false);
1555 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1556 * configuration table.
1557 * @guid: GUID of the installed table
1558 * @table: table to be installed
1560 * This function implements the InstallConfigurationTable service.
1562 * See the Unified Extensible Firmware Interface (UEFI) specification for
1565 * Return: status code
1567 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1570 EFI_ENTRY("%pUl, %p", guid, table);
1571 return EFI_EXIT(efi_install_configuration_table(guid, table));
1575 * efi_setup_loaded_image() - initialize a loaded image
1577 * Initialize a loaded_image_info and loaded_image_info object with correct
1578 * protocols, boot-device, etc.
1580 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1583 * @device_path: device path of the loaded image
1584 * @file_path: file path of the loaded image
1585 * @handle_ptr: handle of the loaded image
1586 * @info_ptr: loaded image protocol
1587 * Return: status code
1589 efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1590 struct efi_device_path *file_path,
1591 struct efi_loaded_image_obj **handle_ptr,
1592 struct efi_loaded_image **info_ptr)
1595 struct efi_loaded_image *info = NULL;
1596 struct efi_loaded_image_obj *obj = NULL;
1597 struct efi_device_path *dp;
1599 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1603 info = calloc(1, sizeof(*info));
1605 return EFI_OUT_OF_RESOURCES;
1606 obj = calloc(1, sizeof(*obj));
1609 return EFI_OUT_OF_RESOURCES;
1611 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
1613 /* Add internal object to object list */
1614 efi_add_handle(&obj->header);
1616 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1617 info->file_path = file_path;
1618 info->system_table = &systab;
1621 info->device_handle = efi_dp_find_obj(device_path, NULL);
1623 dp = efi_dp_append(device_path, file_path);
1625 ret = EFI_OUT_OF_RESOURCES;
1631 ret = efi_add_protocol(&obj->header,
1632 &efi_guid_loaded_image_device_path, dp);
1633 if (ret != EFI_SUCCESS)
1637 * When asking for the loaded_image interface, just
1638 * return handle which points to loaded_image_info
1640 ret = efi_add_protocol(&obj->header,
1641 &efi_guid_loaded_image, info);
1642 if (ret != EFI_SUCCESS)
1650 printf("ERROR: Failure to install protocols for loaded image\n");
1651 efi_delete_handle(&obj->header);
1657 * efi_load_image_from_path() - load an image using a file path
1659 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1660 * callers obligation to update the memory type as needed.
1662 * @file_path: the path of the image to load
1663 * @buffer: buffer containing the loaded image
1664 * @size: size of the loaded image
1665 * Return: status code
1668 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1669 void **buffer, efi_uintn_t *size)
1671 struct efi_file_info *info = NULL;
1672 struct efi_file_handle *f;
1673 static efi_status_t ret;
1677 /* In case of failure nothing is returned */
1682 f = efi_file_from_path(file_path);
1684 return EFI_DEVICE_ERROR;
1688 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1690 if (ret != EFI_BUFFER_TOO_SMALL) {
1691 ret = EFI_DEVICE_ERROR;
1696 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1698 if (ret != EFI_SUCCESS)
1702 * When reading the file we do not yet know if it contains an
1703 * application, a boottime driver, or a runtime driver. So here we
1704 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1705 * update the reservation according to the image type.
1707 bs = info->file_size;
1708 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1709 EFI_BOOT_SERVICES_DATA,
1710 efi_size_in_pages(bs), &addr);
1711 if (ret != EFI_SUCCESS) {
1712 ret = EFI_OUT_OF_RESOURCES;
1717 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1718 if (ret != EFI_SUCCESS)
1719 efi_free_pages(addr, efi_size_in_pages(bs));
1720 *buffer = (void *)(uintptr_t)addr;
1723 EFI_CALL(f->close(f));
1729 * efi_load_image() - load an EFI image into memory
1730 * @boot_policy: true for request originating from the boot manager
1731 * @parent_image: the caller's image handle
1732 * @file_path: the path of the image to load
1733 * @source_buffer: memory location from which the image is installed
1734 * @source_size: size of the memory area from which the image is installed
1735 * @image_handle: handle for the newly installed image
1737 * This function implements the LoadImage service.
1739 * See the Unified Extensible Firmware Interface (UEFI) specification
1742 * Return: status code
1744 efi_status_t EFIAPI efi_load_image(bool boot_policy,
1745 efi_handle_t parent_image,
1746 struct efi_device_path *file_path,
1747 void *source_buffer,
1748 efi_uintn_t source_size,
1749 efi_handle_t *image_handle)
1751 struct efi_device_path *dp, *fp;
1752 struct efi_loaded_image *info = NULL;
1753 struct efi_loaded_image_obj **image_obj =
1754 (struct efi_loaded_image_obj **)image_handle;
1758 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1759 file_path, source_buffer, source_size, image_handle);
1761 if (!image_handle || !efi_search_obj(parent_image)) {
1762 ret = EFI_INVALID_PARAMETER;
1766 if (!source_buffer && !file_path) {
1767 ret = EFI_NOT_FOUND;
1770 /* The parent image handle must refer to a loaded image */
1771 if (!parent_image->type) {
1772 ret = EFI_INVALID_PARAMETER;
1776 if (!source_buffer) {
1777 ret = efi_load_image_from_path(file_path, &dest_buffer,
1779 if (ret != EFI_SUCCESS)
1783 ret = EFI_LOAD_ERROR;
1786 dest_buffer = source_buffer;
1788 /* split file_path which contains both the device and file parts */
1789 efi_dp_split_file_path(file_path, &dp, &fp);
1790 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1791 if (ret == EFI_SUCCESS)
1792 ret = efi_load_pe(*image_obj, dest_buffer, info);
1794 /* Release buffer to which file was loaded */
1795 efi_free_pages((uintptr_t)dest_buffer,
1796 efi_size_in_pages(source_size));
1797 if (ret == EFI_SUCCESS) {
1798 info->system_table = &systab;
1799 info->parent_handle = parent_image;
1801 /* The image is invalid. Release all associated resources. */
1802 efi_delete_handle(*image_handle);
1803 *image_handle = NULL;
1807 return EFI_EXIT(ret);
1811 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1813 static void efi_exit_caches(void)
1815 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1817 * Grub on 32bit ARM needs to have caches disabled before jumping into
1818 * a zImage, but does not know of all cache layers. Give it a hand.
1820 if (efi_is_direct_boot)
1821 cleanup_before_linux();
1826 * efi_exit_boot_services() - stop all boot services
1827 * @image_handle: handle of the loaded image
1828 * @map_key: key of the memory map
1830 * This function implements the ExitBootServices service.
1832 * See the Unified Extensible Firmware Interface (UEFI) specification
1835 * All timer events are disabled. For exit boot services events the
1836 * notification function is called. The boot services are disabled in the
1839 * Return: status code
1841 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1842 efi_uintn_t map_key)
1844 struct efi_event *evt;
1846 EFI_ENTRY("%p, %zx", image_handle, map_key);
1848 /* Check that the caller has read the current memory map */
1849 if (map_key != efi_memory_map_key)
1850 return EFI_INVALID_PARAMETER;
1852 /* Make sure that notification functions are not called anymore */
1853 efi_tpl = TPL_HIGH_LEVEL;
1855 /* Check if ExitBootServices has already been called */
1856 if (!systab.boottime)
1857 return EFI_EXIT(EFI_SUCCESS);
1859 /* Add related events to the event group */
1860 list_for_each_entry(evt, &efi_events, link) {
1861 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1862 evt->group = &efi_guid_event_group_exit_boot_services;
1864 /* Notify that ExitBootServices is invoked. */
1865 list_for_each_entry(evt, &efi_events, link) {
1867 !guidcmp(evt->group,
1868 &efi_guid_event_group_exit_boot_services)) {
1869 efi_signal_event(evt, false);
1874 /* TODO: Should persist EFI variables here */
1876 board_quiesce_devices();
1878 /* Fix up caches for EFI payloads if necessary */
1881 /* This stops all lingering devices */
1882 bootm_disable_interrupts();
1884 /* Disable boot time services */
1885 systab.con_in_handle = NULL;
1886 systab.con_in = NULL;
1887 systab.con_out_handle = NULL;
1888 systab.con_out = NULL;
1889 systab.stderr_handle = NULL;
1890 systab.std_err = NULL;
1891 systab.boottime = NULL;
1893 /* Recalculate CRC32 */
1894 efi_update_table_header_crc32(&systab.hdr);
1896 /* Give the payload some time to boot */
1897 efi_set_watchdog(0);
1900 return EFI_EXIT(EFI_SUCCESS);
1904 * efi_get_next_monotonic_count() - get next value of the counter
1905 * @count: returned value of the counter
1907 * This function implements the NextMonotonicCount service.
1909 * See the Unified Extensible Firmware Interface (UEFI) specification for
1912 * Return: status code
1914 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1916 static uint64_t mono;
1918 EFI_ENTRY("%p", count);
1920 return EFI_EXIT(EFI_SUCCESS);
1924 * efi_stall() - sleep
1925 * @microseconds: period to sleep in microseconds
1927 * This function implements the Stall service.
1929 * See the Unified Extensible Firmware Interface (UEFI) specification for
1932 * Return: status code
1934 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1936 EFI_ENTRY("%ld", microseconds);
1937 udelay(microseconds);
1938 return EFI_EXIT(EFI_SUCCESS);
1942 * efi_set_watchdog_timer() - reset the watchdog timer
1943 * @timeout: seconds before reset by watchdog
1944 * @watchdog_code: code to be logged when resetting
1945 * @data_size: size of buffer in bytes
1946 * @watchdog_data: buffer with data describing the reset reason
1948 * This function implements the SetWatchdogTimer service.
1950 * See the Unified Extensible Firmware Interface (UEFI) specification for
1953 * Return: status code
1955 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1956 uint64_t watchdog_code,
1957 unsigned long data_size,
1958 uint16_t *watchdog_data)
1960 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
1961 data_size, watchdog_data);
1962 return EFI_EXIT(efi_set_watchdog(timeout));
1966 * efi_close_protocol() - close a protocol
1967 * @handle: handle on which the protocol shall be closed
1968 * @protocol: GUID of the protocol to close
1969 * @agent_handle: handle of the driver
1970 * @controller_handle: handle of the controller
1972 * This function implements the CloseProtocol service.
1974 * See the Unified Extensible Firmware Interface (UEFI) specification for
1977 * Return: status code
1979 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
1980 const efi_guid_t *protocol,
1981 efi_handle_t agent_handle,
1982 efi_handle_t controller_handle)
1984 struct efi_handler *handler;
1985 struct efi_open_protocol_info_item *item;
1986 struct efi_open_protocol_info_item *pos;
1989 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1992 if (!efi_search_obj(agent_handle) ||
1993 (controller_handle && !efi_search_obj(controller_handle))) {
1994 r = EFI_INVALID_PARAMETER;
1997 r = efi_search_protocol(handle, protocol, &handler);
1998 if (r != EFI_SUCCESS)
2002 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2003 if (item->info.agent_handle == agent_handle &&
2004 item->info.controller_handle == controller_handle) {
2005 efi_delete_open_info(item);
2015 * efi_open_protocol_information() - provide information about then open status
2016 * of a protocol on a handle
2017 * @handle: handle for which the information shall be retrieved
2018 * @protocol: GUID of the protocol
2019 * @entry_buffer: buffer to receive the open protocol information
2020 * @entry_count: number of entries available in the buffer
2022 * This function implements the OpenProtocolInformation service.
2024 * See the Unified Extensible Firmware Interface (UEFI) specification for
2027 * Return: status code
2029 static efi_status_t EFIAPI efi_open_protocol_information(
2030 efi_handle_t handle, const efi_guid_t *protocol,
2031 struct efi_open_protocol_info_entry **entry_buffer,
2032 efi_uintn_t *entry_count)
2034 unsigned long buffer_size;
2035 unsigned long count;
2036 struct efi_handler *handler;
2037 struct efi_open_protocol_info_item *item;
2040 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2043 /* Check parameters */
2044 if (!entry_buffer) {
2045 r = EFI_INVALID_PARAMETER;
2048 r = efi_search_protocol(handle, protocol, &handler);
2049 if (r != EFI_SUCCESS)
2054 list_for_each_entry(item, &handler->open_infos, link) {
2055 if (item->info.open_count)
2058 *entry_count = count;
2059 *entry_buffer = NULL;
2066 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2067 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2068 (void **)entry_buffer);
2069 if (r != EFI_SUCCESS)
2071 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2072 if (item->info.open_count)
2073 (*entry_buffer)[--count] = item->info;
2080 * efi_protocols_per_handle() - get protocols installed on a handle
2081 * @handle: handle for which the information is retrieved
2082 * @protocol_buffer: buffer with protocol GUIDs
2083 * @protocol_buffer_count: number of entries in the buffer
2085 * This function implements the ProtocolsPerHandleService.
2087 * See the Unified Extensible Firmware Interface (UEFI) specification for
2090 * Return: status code
2092 static efi_status_t EFIAPI efi_protocols_per_handle(
2093 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2094 efi_uintn_t *protocol_buffer_count)
2096 unsigned long buffer_size;
2097 struct efi_object *efiobj;
2098 struct list_head *protocol_handle;
2101 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2102 protocol_buffer_count);
2104 if (!handle || !protocol_buffer || !protocol_buffer_count)
2105 return EFI_EXIT(EFI_INVALID_PARAMETER);
2107 *protocol_buffer = NULL;
2108 *protocol_buffer_count = 0;
2110 efiobj = efi_search_obj(handle);
2112 return EFI_EXIT(EFI_INVALID_PARAMETER);
2114 /* Count protocols */
2115 list_for_each(protocol_handle, &efiobj->protocols) {
2116 ++*protocol_buffer_count;
2120 if (*protocol_buffer_count) {
2123 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2124 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2125 (void **)protocol_buffer);
2126 if (r != EFI_SUCCESS)
2128 list_for_each(protocol_handle, &efiobj->protocols) {
2129 struct efi_handler *protocol;
2131 protocol = list_entry(protocol_handle,
2132 struct efi_handler, link);
2133 (*protocol_buffer)[j] = (void *)protocol->guid;
2138 return EFI_EXIT(EFI_SUCCESS);
2142 * efi_locate_handle_buffer() - locate handles implementing a protocol
2143 * @search_type: selection criterion
2144 * @protocol: GUID of the protocol
2145 * @search_key: registration key
2146 * @no_handles: number of returned handles
2147 * @buffer: buffer with the returned handles
2149 * This function implements the LocateHandleBuffer service.
2151 * See the Unified Extensible Firmware Interface (UEFI) specification for
2154 * Return: status code
2156 static efi_status_t EFIAPI efi_locate_handle_buffer(
2157 enum efi_locate_search_type search_type,
2158 const efi_guid_t *protocol, void *search_key,
2159 efi_uintn_t *no_handles, efi_handle_t **buffer)
2162 efi_uintn_t buffer_size = 0;
2164 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2165 no_handles, buffer);
2167 if (!no_handles || !buffer) {
2168 r = EFI_INVALID_PARAMETER;
2173 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2175 if (r != EFI_BUFFER_TOO_SMALL)
2177 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2179 if (r != EFI_SUCCESS)
2181 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2183 if (r == EFI_SUCCESS)
2184 *no_handles = buffer_size / sizeof(efi_handle_t);
2190 * efi_locate_protocol() - find an interface implementing a protocol
2191 * @protocol: GUID of the protocol
2192 * @registration: registration key passed to the notification function
2193 * @protocol_interface: interface implementing the protocol
2195 * This function implements the LocateProtocol service.
2197 * See the Unified Extensible Firmware Interface (UEFI) specification for
2200 * Return: status code
2202 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2204 void **protocol_interface)
2206 struct list_head *lhandle;
2209 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2211 if (!protocol || !protocol_interface)
2212 return EFI_EXIT(EFI_INVALID_PARAMETER);
2214 list_for_each(lhandle, &efi_obj_list) {
2215 struct efi_object *efiobj;
2216 struct efi_handler *handler;
2218 efiobj = list_entry(lhandle, struct efi_object, link);
2220 ret = efi_search_protocol(efiobj, protocol, &handler);
2221 if (ret == EFI_SUCCESS) {
2222 *protocol_interface = handler->protocol_interface;
2223 return EFI_EXIT(EFI_SUCCESS);
2226 *protocol_interface = NULL;
2228 return EFI_EXIT(EFI_NOT_FOUND);
2232 * efi_locate_device_path() - Get the device path and handle of an device
2233 * implementing a protocol
2234 * @protocol: GUID of the protocol
2235 * @device_path: device path
2236 * @device: handle of the device
2238 * This function implements the LocateDevicePath service.
2240 * See the Unified Extensible Firmware Interface (UEFI) specification for
2243 * Return: status code
2245 static efi_status_t EFIAPI efi_locate_device_path(
2246 const efi_guid_t *protocol,
2247 struct efi_device_path **device_path,
2248 efi_handle_t *device)
2250 struct efi_device_path *dp;
2252 struct efi_handler *handler;
2253 efi_handle_t *handles;
2255 size_t len_best = 0;
2256 efi_uintn_t no_handles;
2260 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2262 if (!protocol || !device_path || !*device_path) {
2263 ret = EFI_INVALID_PARAMETER;
2267 /* Find end of device path */
2268 len = efi_dp_instance_size(*device_path);
2270 /* Get all handles implementing the protocol */
2271 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2272 &no_handles, &handles));
2273 if (ret != EFI_SUCCESS)
2276 for (i = 0; i < no_handles; ++i) {
2277 /* Find the device path protocol */
2278 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2280 if (ret != EFI_SUCCESS)
2282 dp = (struct efi_device_path *)handler->protocol_interface;
2283 len_dp = efi_dp_instance_size(dp);
2285 * This handle can only be a better fit
2286 * if its device path length is longer than the best fit and
2287 * if its device path length is shorter of equal the searched
2290 if (len_dp <= len_best || len_dp > len)
2292 /* Check if dp is a subpath of device_path */
2293 if (memcmp(*device_path, dp, len_dp))
2296 ret = EFI_INVALID_PARAMETER;
2299 *device = handles[i];
2303 remainder = (u8 *)*device_path + len_best;
2304 *device_path = (struct efi_device_path *)remainder;
2307 ret = EFI_NOT_FOUND;
2310 return EFI_EXIT(ret);
2314 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2316 * @handle: handle on which the protocol interfaces shall be installed
2317 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2320 * This function implements the MultipleProtocolInterfaces service.
2322 * See the Unified Extensible Firmware Interface (UEFI) specification for
2325 * Return: status code
2327 efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2328 (efi_handle_t *handle, ...)
2330 EFI_ENTRY("%p", handle);
2333 const efi_guid_t *protocol;
2334 void *protocol_interface;
2335 efi_status_t r = EFI_SUCCESS;
2339 return EFI_EXIT(EFI_INVALID_PARAMETER);
2341 efi_va_start(argptr, handle);
2343 protocol = efi_va_arg(argptr, efi_guid_t*);
2346 protocol_interface = efi_va_arg(argptr, void*);
2347 r = EFI_CALL(efi_install_protocol_interface(
2349 EFI_NATIVE_INTERFACE,
2350 protocol_interface));
2351 if (r != EFI_SUCCESS)
2356 if (r == EFI_SUCCESS)
2359 /* If an error occurred undo all changes. */
2360 efi_va_start(argptr, handle);
2362 protocol = efi_va_arg(argptr, efi_guid_t*);
2363 protocol_interface = efi_va_arg(argptr, void*);
2364 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2365 protocol_interface));
2373 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2375 * @handle: handle from which the protocol interfaces shall be removed
2376 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2379 * This function implements the UninstallMultipleProtocolInterfaces service.
2381 * See the Unified Extensible Firmware Interface (UEFI) specification for
2384 * Return: status code
2386 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2387 efi_handle_t handle, ...)
2389 EFI_ENTRY("%p", handle);
2392 const efi_guid_t *protocol;
2393 void *protocol_interface;
2394 efi_status_t r = EFI_SUCCESS;
2398 return EFI_EXIT(EFI_INVALID_PARAMETER);
2400 efi_va_start(argptr, handle);
2402 protocol = efi_va_arg(argptr, efi_guid_t*);
2405 protocol_interface = efi_va_arg(argptr, void*);
2406 r = efi_uninstall_protocol(handle, protocol,
2407 protocol_interface);
2408 if (r != EFI_SUCCESS)
2413 if (r == EFI_SUCCESS) {
2414 /* If the last protocol has been removed, delete the handle. */
2415 if (list_empty(&handle->protocols)) {
2416 list_del(&handle->link);
2422 /* If an error occurred undo all changes. */
2423 efi_va_start(argptr, handle);
2425 protocol = efi_va_arg(argptr, efi_guid_t*);
2426 protocol_interface = efi_va_arg(argptr, void*);
2427 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2428 EFI_NATIVE_INTERFACE,
2429 protocol_interface));
2433 /* In case of an error always return EFI_INVALID_PARAMETER */
2434 return EFI_EXIT(EFI_INVALID_PARAMETER);
2438 * efi_calculate_crc32() - calculate cyclic redundancy code
2439 * @data: buffer with data
2440 * @data_size: size of buffer in bytes
2441 * @crc32_p: cyclic redundancy code
2443 * This function implements the CalculateCrc32 service.
2445 * See the Unified Extensible Firmware Interface (UEFI) specification for
2448 * Return: status code
2450 static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2451 efi_uintn_t data_size,
2454 EFI_ENTRY("%p, %zu", data, data_size);
2455 *crc32_p = crc32(0, data, data_size);
2456 return EFI_EXIT(EFI_SUCCESS);
2460 * efi_copy_mem() - copy memory
2461 * @destination: destination of the copy operation
2462 * @source: source of the copy operation
2463 * @length: number of bytes to copy
2465 * This function implements the CopyMem service.
2467 * See the Unified Extensible Firmware Interface (UEFI) specification for
2470 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2473 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2474 memmove(destination, source, length);
2475 EFI_EXIT(EFI_SUCCESS);
2479 * efi_set_mem() - Fill memory with a byte value.
2480 * @buffer: buffer to fill
2481 * @size: size of buffer in bytes
2482 * @value: byte to copy to the buffer
2484 * This function implements the SetMem service.
2486 * See the Unified Extensible Firmware Interface (UEFI) specification for
2489 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2491 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2492 memset(buffer, value, size);
2493 EFI_EXIT(EFI_SUCCESS);
2497 * efi_protocol_open() - open protocol interface on a handle
2498 * @handler: handler of a protocol
2499 * @protocol_interface: interface implementing the protocol
2500 * @agent_handle: handle of the driver
2501 * @controller_handle: handle of the controller
2502 * @attributes: attributes indicating how to open the protocol
2504 * Return: status code
2506 static efi_status_t efi_protocol_open(
2507 struct efi_handler *handler,
2508 void **protocol_interface, void *agent_handle,
2509 void *controller_handle, uint32_t attributes)
2511 struct efi_open_protocol_info_item *item;
2512 struct efi_open_protocol_info_entry *match = NULL;
2513 bool opened_by_driver = false;
2514 bool opened_exclusive = false;
2516 /* If there is no agent, only return the interface */
2520 /* For TEST_PROTOCOL ignore interface attribute */
2521 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2522 *protocol_interface = NULL;
2525 * Check if the protocol is already opened by a driver with the same
2526 * attributes or opened exclusively
2528 list_for_each_entry(item, &handler->open_infos, link) {
2529 if (item->info.agent_handle == agent_handle) {
2530 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2531 (item->info.attributes == attributes))
2532 return EFI_ALREADY_STARTED;
2534 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2535 opened_exclusive = true;
2538 /* Only one controller can open the protocol exclusively */
2539 if (opened_exclusive && attributes &
2540 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2541 return EFI_ACCESS_DENIED;
2543 /* Prepare exclusive opening */
2544 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2545 /* Try to disconnect controllers */
2546 list_for_each_entry(item, &handler->open_infos, link) {
2547 if (item->info.attributes ==
2548 EFI_OPEN_PROTOCOL_BY_DRIVER)
2549 EFI_CALL(efi_disconnect_controller(
2550 item->info.controller_handle,
2551 item->info.agent_handle,
2554 opened_by_driver = false;
2555 /* Check if all controllers are disconnected */
2556 list_for_each_entry(item, &handler->open_infos, link) {
2557 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2558 opened_by_driver = true;
2560 /* Only one controller can be connected */
2561 if (opened_by_driver)
2562 return EFI_ACCESS_DENIED;
2565 /* Find existing entry */
2566 list_for_each_entry(item, &handler->open_infos, link) {
2567 if (item->info.agent_handle == agent_handle &&
2568 item->info.controller_handle == controller_handle)
2569 match = &item->info;
2571 /* None found, create one */
2573 match = efi_create_open_info(handler);
2575 return EFI_OUT_OF_RESOURCES;
2578 match->agent_handle = agent_handle;
2579 match->controller_handle = controller_handle;
2580 match->attributes = attributes;
2581 match->open_count++;
2584 /* For TEST_PROTOCOL ignore interface attribute. */
2585 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2586 *protocol_interface = handler->protocol_interface;
2592 * efi_open_protocol() - open protocol interface on a handle
2593 * @handle: handle on which the protocol shall be opened
2594 * @protocol: GUID of the protocol
2595 * @protocol_interface: interface implementing the protocol
2596 * @agent_handle: handle of the driver
2597 * @controller_handle: handle of the controller
2598 * @attributes: attributes indicating how to open the protocol
2600 * This function implements the OpenProtocol interface.
2602 * See the Unified Extensible Firmware Interface (UEFI) specification for
2605 * Return: status code
2607 static efi_status_t EFIAPI efi_open_protocol
2608 (efi_handle_t handle, const efi_guid_t *protocol,
2609 void **protocol_interface, efi_handle_t agent_handle,
2610 efi_handle_t controller_handle, uint32_t attributes)
2612 struct efi_handler *handler;
2613 efi_status_t r = EFI_INVALID_PARAMETER;
2615 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2616 protocol_interface, agent_handle, controller_handle,
2619 if (!handle || !protocol ||
2620 (!protocol_interface && attributes !=
2621 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2625 switch (attributes) {
2626 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2627 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2628 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2630 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2631 if (controller_handle == handle)
2634 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2635 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2636 /* Check that the controller handle is valid */
2637 if (!efi_search_obj(controller_handle))
2640 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2641 /* Check that the agent handle is valid */
2642 if (!efi_search_obj(agent_handle))
2649 r = efi_search_protocol(handle, protocol, &handler);
2654 r = EFI_UNSUPPORTED;
2660 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2661 controller_handle, attributes);
2667 * efi_start_image() - call the entry point of an image
2668 * @image_handle: handle of the image
2669 * @exit_data_size: size of the buffer
2670 * @exit_data: buffer to receive the exit data of the called image
2672 * This function implements the StartImage service.
2674 * See the Unified Extensible Firmware Interface (UEFI) specification for
2677 * Return: status code
2679 efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2680 efi_uintn_t *exit_data_size,
2683 struct efi_loaded_image_obj *image_obj =
2684 (struct efi_loaded_image_obj *)image_handle;
2687 efi_handle_t parent_image = current_image;
2689 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2691 /* Check parameters */
2692 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2694 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2695 if (ret != EFI_SUCCESS)
2696 return EFI_EXIT(EFI_INVALID_PARAMETER);
2698 efi_is_direct_boot = false;
2700 image_obj->exit_data_size = exit_data_size;
2701 image_obj->exit_data = exit_data;
2703 /* call the image! */
2704 if (setjmp(&image_obj->exit_jmp)) {
2706 * We called the entry point of the child image with EFI_CALL
2707 * in the lines below. The child image called the Exit() boot
2708 * service efi_exit() which executed the long jump that brought
2709 * us to the current line. This implies that the second half
2710 * of the EFI_CALL macro has not been executed.
2714 * efi_exit() called efi_restore_gd(). We have to undo this
2715 * otherwise __efi_entry_check() will put the wrong value into
2721 * To get ready to call EFI_EXIT below we have to execute the
2722 * missed out steps of EFI_CALL.
2724 assert(__efi_entry_check());
2725 EFI_PRINT("%lu returned by started image\n",
2726 (unsigned long)((uintptr_t)image_obj->exit_status &
2728 current_image = parent_image;
2729 return EFI_EXIT(image_obj->exit_status);
2732 current_image = image_handle;
2733 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
2734 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
2735 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2738 * Usually UEFI applications call Exit() instead of returning.
2739 * But because the world doesn't consist of ponies and unicorns,
2740 * we're happy to emulate that behavior on behalf of a payload
2743 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2747 * efi_delete_image() - delete loaded image from memory)
2749 * @image_obj: handle of the loaded image
2750 * @loaded_image_protocol: loaded image protocol
2752 static void efi_delete_image(struct efi_loaded_image_obj *image_obj,
2753 struct efi_loaded_image *loaded_image_protocol)
2755 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2756 efi_size_in_pages(loaded_image_protocol->image_size));
2757 efi_delete_handle(&image_obj->header);
2761 * efi_unload_image() - unload an EFI image
2762 * @image_handle: handle of the image to be unloaded
2764 * This function implements the UnloadImage service.
2766 * See the Unified Extensible Firmware Interface (UEFI) specification for
2769 * Return: status code
2771 efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
2773 efi_status_t ret = EFI_SUCCESS;
2774 struct efi_object *efiobj;
2775 struct efi_loaded_image *loaded_image_protocol;
2777 EFI_ENTRY("%p", image_handle);
2779 efiobj = efi_search_obj(image_handle);
2781 ret = EFI_INVALID_PARAMETER;
2784 /* Find the loaded image protocol */
2785 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2786 (void **)&loaded_image_protocol,
2788 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2789 if (ret != EFI_SUCCESS) {
2790 ret = EFI_INVALID_PARAMETER;
2793 switch (efiobj->type) {
2794 case EFI_OBJECT_TYPE_STARTED_IMAGE:
2795 /* Call the unload function */
2796 if (!loaded_image_protocol->unload) {
2797 ret = EFI_UNSUPPORTED;
2800 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
2801 if (ret != EFI_SUCCESS)
2804 case EFI_OBJECT_TYPE_LOADED_IMAGE:
2807 ret = EFI_INVALID_PARAMETER;
2810 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
2811 loaded_image_protocol);
2813 return EFI_EXIT(ret);
2817 * efi_update_exit_data() - fill exit data parameters of StartImage()
2819 * @image_obj image handle
2820 * @exit_data_size size of the exit data buffer
2821 * @exit_data buffer with data returned by UEFI payload
2822 * Return: status code
2824 static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
2825 efi_uintn_t exit_data_size,
2831 * If exit_data is not provided to StartImage(), exit_data_size must be
2834 if (!image_obj->exit_data)
2836 if (image_obj->exit_data_size)
2837 *image_obj->exit_data_size = exit_data_size;
2838 if (exit_data_size && exit_data) {
2839 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
2841 (void **)image_obj->exit_data);
2842 if (ret != EFI_SUCCESS)
2844 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
2846 image_obj->exit_data = NULL;
2852 * efi_exit() - leave an EFI application or driver
2853 * @image_handle: handle of the application or driver that is exiting
2854 * @exit_status: status code
2855 * @exit_data_size: size of the buffer in bytes
2856 * @exit_data: buffer with data describing an error
2858 * This function implements the Exit service.
2860 * See the Unified Extensible Firmware Interface (UEFI) specification for
2863 * Return: status code
2865 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
2866 efi_status_t exit_status,
2867 efi_uintn_t exit_data_size,
2871 * TODO: We should call the unload procedure of the loaded
2875 struct efi_loaded_image *loaded_image_protocol;
2876 struct efi_loaded_image_obj *image_obj =
2877 (struct efi_loaded_image_obj *)image_handle;
2879 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
2880 exit_data_size, exit_data);
2882 /* Check parameters */
2883 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2884 (void **)&loaded_image_protocol,
2886 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2887 if (ret != EFI_SUCCESS) {
2888 ret = EFI_INVALID_PARAMETER;
2892 /* Unloading of unstarted images */
2893 switch (image_obj->header.type) {
2894 case EFI_OBJECT_TYPE_STARTED_IMAGE:
2896 case EFI_OBJECT_TYPE_LOADED_IMAGE:
2897 efi_delete_image(image_obj, loaded_image_protocol);
2901 /* Handle does not refer to loaded image */
2902 ret = EFI_INVALID_PARAMETER;
2905 /* A started image can only be unloaded it is the last one started. */
2906 if (image_handle != current_image) {
2907 ret = EFI_INVALID_PARAMETER;
2911 /* Exit data is only foreseen in case of failure. */
2912 if (exit_status != EFI_SUCCESS) {
2913 ret = efi_update_exit_data(image_obj, exit_data_size,
2915 /* Exiting has priority. Don't return error to caller. */
2916 if (ret != EFI_SUCCESS)
2917 EFI_PRINT("%s: out of memory\n", __func__);
2919 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
2920 exit_status != EFI_SUCCESS)
2921 efi_delete_image(image_obj, loaded_image_protocol);
2923 /* Make sure entry/exit counts for EFI world cross-overs match */
2924 EFI_EXIT(exit_status);
2927 * But longjmp out with the U-Boot gd, not the application's, as
2928 * the other end is a setjmp call inside EFI context.
2932 image_obj->exit_status = exit_status;
2933 longjmp(&image_obj->exit_jmp, 1);
2935 panic("EFI application exited");
2937 return EFI_EXIT(ret);
2941 * efi_handle_protocol() - get interface of a protocol on a handle
2942 * @handle: handle on which the protocol shall be opened
2943 * @protocol: GUID of the protocol
2944 * @protocol_interface: interface implementing the protocol
2946 * This function implements the HandleProtocol service.
2948 * See the Unified Extensible Firmware Interface (UEFI) specification for
2951 * Return: status code
2953 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
2954 const efi_guid_t *protocol,
2955 void **protocol_interface)
2957 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2958 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2962 * efi_bind_controller() - bind a single driver to a controller
2963 * @controller_handle: controller handle
2964 * @driver_image_handle: driver handle
2965 * @remain_device_path: remaining path
2967 * Return: status code
2969 static efi_status_t efi_bind_controller(
2970 efi_handle_t controller_handle,
2971 efi_handle_t driver_image_handle,
2972 struct efi_device_path *remain_device_path)
2974 struct efi_driver_binding_protocol *binding_protocol;
2977 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2978 &efi_guid_driver_binding_protocol,
2979 (void **)&binding_protocol,
2980 driver_image_handle, NULL,
2981 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2982 if (r != EFI_SUCCESS)
2984 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2986 remain_device_path));
2987 if (r == EFI_SUCCESS)
2988 r = EFI_CALL(binding_protocol->start(binding_protocol,
2990 remain_device_path));
2991 EFI_CALL(efi_close_protocol(driver_image_handle,
2992 &efi_guid_driver_binding_protocol,
2993 driver_image_handle, NULL));
2998 * efi_connect_single_controller() - connect a single driver to a controller
2999 * @controller_handle: controller
3000 * @driver_image_handle: driver
3001 * @remain_device_path: remaining path
3003 * Return: status code
3005 static efi_status_t efi_connect_single_controller(
3006 efi_handle_t controller_handle,
3007 efi_handle_t *driver_image_handle,
3008 struct efi_device_path *remain_device_path)
3010 efi_handle_t *buffer;
3014 size_t connected = 0;
3016 /* Get buffer with all handles with driver binding protocol */
3017 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3018 &efi_guid_driver_binding_protocol,
3019 NULL, &count, &buffer));
3020 if (r != EFI_SUCCESS)
3023 /* Context Override */
3024 if (driver_image_handle) {
3025 for (; *driver_image_handle; ++driver_image_handle) {
3026 for (i = 0; i < count; ++i) {
3027 if (buffer[i] == *driver_image_handle) {
3029 r = efi_bind_controller(
3031 *driver_image_handle,
3032 remain_device_path);
3034 * For drivers that do not support the
3035 * controller or are already connected
3036 * we receive an error code here.
3038 if (r == EFI_SUCCESS)
3046 * TODO: Some overrides are not yet implemented:
3047 * - Platform Driver Override
3048 * - Driver Family Override Search
3049 * - Bus Specific Driver Override
3052 /* Driver Binding Search */
3053 for (i = 0; i < count; ++i) {
3055 r = efi_bind_controller(controller_handle,
3057 remain_device_path);
3058 if (r == EFI_SUCCESS)
3063 efi_free_pool(buffer);
3065 return EFI_NOT_FOUND;
3070 * efi_connect_controller() - connect a controller to a driver
3071 * @controller_handle: handle of the controller
3072 * @driver_image_handle: handle of the driver
3073 * @remain_device_path: device path of a child controller
3074 * @recursive: true to connect all child controllers
3076 * This function implements the ConnectController service.
3078 * See the Unified Extensible Firmware Interface (UEFI) specification for
3081 * First all driver binding protocol handles are tried for binding drivers.
3082 * Afterwards all handles that have opened a protocol of the controller
3083 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3085 * Return: status code
3087 static efi_status_t EFIAPI efi_connect_controller(
3088 efi_handle_t controller_handle,
3089 efi_handle_t *driver_image_handle,
3090 struct efi_device_path *remain_device_path,
3094 efi_status_t ret = EFI_NOT_FOUND;
3095 struct efi_object *efiobj;
3097 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
3098 remain_device_path, recursive);
3100 efiobj = efi_search_obj(controller_handle);
3102 ret = EFI_INVALID_PARAMETER;
3106 r = efi_connect_single_controller(controller_handle,
3107 driver_image_handle,
3108 remain_device_path);
3109 if (r == EFI_SUCCESS)
3112 struct efi_handler *handler;
3113 struct efi_open_protocol_info_item *item;
3115 list_for_each_entry(handler, &efiobj->protocols, link) {
3116 list_for_each_entry(item, &handler->open_infos, link) {
3117 if (item->info.attributes &
3118 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3119 r = EFI_CALL(efi_connect_controller(
3120 item->info.controller_handle,
3121 driver_image_handle,
3124 if (r == EFI_SUCCESS)
3130 /* Check for child controller specified by end node */
3131 if (ret != EFI_SUCCESS && remain_device_path &&
3132 remain_device_path->type == DEVICE_PATH_TYPE_END)
3135 return EFI_EXIT(ret);
3139 * efi_reinstall_protocol_interface() - reinstall protocol interface
3140 * @handle: handle on which the protocol shall be reinstalled
3141 * @protocol: GUID of the protocol to be installed
3142 * @old_interface: interface to be removed
3143 * @new_interface: interface to be installed
3145 * This function implements the ReinstallProtocolInterface service.
3147 * See the Unified Extensible Firmware Interface (UEFI) specification for
3150 * The old interface is uninstalled. The new interface is installed.
3151 * Drivers are connected.
3153 * Return: status code
3155 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3156 efi_handle_t handle, const efi_guid_t *protocol,
3157 void *old_interface, void *new_interface)
3161 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3164 /* Uninstall protocol but do not delete handle */
3165 ret = efi_uninstall_protocol(handle, protocol, old_interface);
3166 if (ret != EFI_SUCCESS)
3169 /* Install the new protocol */
3170 ret = efi_add_protocol(handle, protocol, new_interface);
3172 * The UEFI spec does not specify what should happen to the handle
3173 * if in case of an error no protocol interface remains on the handle.
3174 * So let's do nothing here.
3176 if (ret != EFI_SUCCESS)
3179 * The returned status code has to be ignored.
3180 * Do not create an error if no suitable driver for the handle exists.
3182 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3184 return EFI_EXIT(ret);
3188 * efi_get_child_controllers() - get all child controllers associated to a driver
3189 * @efiobj: handle of the controller
3190 * @driver_handle: handle of the driver
3191 * @number_of_children: number of child controllers
3192 * @child_handle_buffer: handles of the the child controllers
3194 * The allocated buffer has to be freed with free().
3196 * Return: status code
3198 static efi_status_t efi_get_child_controllers(
3199 struct efi_object *efiobj,
3200 efi_handle_t driver_handle,
3201 efi_uintn_t *number_of_children,
3202 efi_handle_t **child_handle_buffer)
3204 struct efi_handler *handler;
3205 struct efi_open_protocol_info_item *item;
3206 efi_uintn_t count = 0, i;
3209 /* Count all child controller associations */
3210 list_for_each_entry(handler, &efiobj->protocols, link) {
3211 list_for_each_entry(item, &handler->open_infos, link) {
3212 if (item->info.agent_handle == driver_handle &&
3213 item->info.attributes &
3214 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3219 * Create buffer. In case of duplicate child controller assignments
3220 * the buffer will be too large. But that does not harm.
3222 *number_of_children = 0;
3223 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3224 if (!*child_handle_buffer)
3225 return EFI_OUT_OF_RESOURCES;
3226 /* Copy unique child handles */
3227 list_for_each_entry(handler, &efiobj->protocols, link) {
3228 list_for_each_entry(item, &handler->open_infos, link) {
3229 if (item->info.agent_handle == driver_handle &&
3230 item->info.attributes &
3231 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3232 /* Check this is a new child controller */
3234 for (i = 0; i < *number_of_children; ++i) {
3235 if ((*child_handle_buffer)[i] ==
3236 item->info.controller_handle)
3239 /* Copy handle to buffer */
3241 i = (*number_of_children)++;
3242 (*child_handle_buffer)[i] =
3243 item->info.controller_handle;
3252 * efi_disconnect_controller() - disconnect a controller from a driver
3253 * @controller_handle: handle of the controller
3254 * @driver_image_handle: handle of the driver
3255 * @child_handle: handle of the child to destroy
3257 * This function implements the DisconnectController service.
3259 * See the Unified Extensible Firmware Interface (UEFI) specification for
3262 * Return: status code
3264 static efi_status_t EFIAPI efi_disconnect_controller(
3265 efi_handle_t controller_handle,
3266 efi_handle_t driver_image_handle,
3267 efi_handle_t child_handle)
3269 struct efi_driver_binding_protocol *binding_protocol;
3270 efi_handle_t *child_handle_buffer = NULL;
3271 size_t number_of_children = 0;
3273 size_t stop_count = 0;
3274 struct efi_object *efiobj;
3276 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3279 efiobj = efi_search_obj(controller_handle);
3281 r = EFI_INVALID_PARAMETER;
3285 if (child_handle && !efi_search_obj(child_handle)) {
3286 r = EFI_INVALID_PARAMETER;
3290 /* If no driver handle is supplied, disconnect all drivers */
3291 if (!driver_image_handle) {
3292 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3296 /* Create list of child handles */
3298 number_of_children = 1;
3299 child_handle_buffer = &child_handle;
3301 efi_get_child_controllers(efiobj,
3302 driver_image_handle,
3303 &number_of_children,
3304 &child_handle_buffer);
3307 /* Get the driver binding protocol */
3308 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3309 &efi_guid_driver_binding_protocol,
3310 (void **)&binding_protocol,
3311 driver_image_handle, NULL,
3312 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3313 if (r != EFI_SUCCESS)
3315 /* Remove the children */
3316 if (number_of_children) {
3317 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3320 child_handle_buffer));
3321 if (r == EFI_SUCCESS)
3324 /* Remove the driver */
3326 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3329 if (r == EFI_SUCCESS)
3331 EFI_CALL(efi_close_protocol(driver_image_handle,
3332 &efi_guid_driver_binding_protocol,
3333 driver_image_handle, NULL));
3341 free(child_handle_buffer);
3345 static struct efi_boot_services efi_boot_services = {
3347 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3348 .revision = EFI_SPECIFICATION_VERSION,
3349 .headersize = sizeof(struct efi_boot_services),
3351 .raise_tpl = efi_raise_tpl,
3352 .restore_tpl = efi_restore_tpl,
3353 .allocate_pages = efi_allocate_pages_ext,
3354 .free_pages = efi_free_pages_ext,
3355 .get_memory_map = efi_get_memory_map_ext,
3356 .allocate_pool = efi_allocate_pool_ext,
3357 .free_pool = efi_free_pool_ext,
3358 .create_event = efi_create_event_ext,
3359 .set_timer = efi_set_timer_ext,
3360 .wait_for_event = efi_wait_for_event,
3361 .signal_event = efi_signal_event_ext,
3362 .close_event = efi_close_event,
3363 .check_event = efi_check_event,
3364 .install_protocol_interface = efi_install_protocol_interface,
3365 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3366 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3367 .handle_protocol = efi_handle_protocol,
3369 .register_protocol_notify = efi_register_protocol_notify,
3370 .locate_handle = efi_locate_handle_ext,
3371 .locate_device_path = efi_locate_device_path,
3372 .install_configuration_table = efi_install_configuration_table_ext,
3373 .load_image = efi_load_image,
3374 .start_image = efi_start_image,
3376 .unload_image = efi_unload_image,
3377 .exit_boot_services = efi_exit_boot_services,
3378 .get_next_monotonic_count = efi_get_next_monotonic_count,
3380 .set_watchdog_timer = efi_set_watchdog_timer,
3381 .connect_controller = efi_connect_controller,
3382 .disconnect_controller = efi_disconnect_controller,
3383 .open_protocol = efi_open_protocol,
3384 .close_protocol = efi_close_protocol,
3385 .open_protocol_information = efi_open_protocol_information,
3386 .protocols_per_handle = efi_protocols_per_handle,
3387 .locate_handle_buffer = efi_locate_handle_buffer,
3388 .locate_protocol = efi_locate_protocol,
3389 .install_multiple_protocol_interfaces =
3390 efi_install_multiple_protocol_interfaces,
3391 .uninstall_multiple_protocol_interfaces =
3392 efi_uninstall_multiple_protocol_interfaces,
3393 .calculate_crc32 = efi_calculate_crc32,
3394 .copy_mem = efi_copy_mem,
3395 .set_mem = efi_set_mem,
3396 .create_event_ex = efi_create_event_ex,
3399 static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3401 struct efi_system_table __efi_runtime_data systab = {
3403 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3404 .revision = EFI_SPECIFICATION_VERSION,
3405 .headersize = sizeof(struct efi_system_table),
3407 .fw_vendor = firmware_vendor,
3408 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3409 .con_in = (void *)&efi_con_in,
3410 .con_out = (void *)&efi_con_out,
3411 .std_err = (void *)&efi_con_out,
3412 .runtime = (void *)&efi_runtime_services,
3413 .boottime = (void *)&efi_boot_services,
3419 * efi_initialize_system_table() - Initialize system table
3421 * Return: status code
3423 efi_status_t efi_initialize_system_table(void)
3427 /* Allocate configuration table array */
3428 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3429 EFI_MAX_CONFIGURATION_TABLES *
3430 sizeof(struct efi_configuration_table),
3431 (void **)&systab.tables);
3433 /* Set CRC32 field in table headers */
3434 efi_update_table_header_crc32(&systab.hdr);
3435 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3436 efi_update_table_header_crc32(&efi_boot_services.hdr);