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 /* Flag to disable timer activity in ExitBootServices() */
31 static bool timers_enabled = true;
33 /* List of all events registered by RegisterProtocolNotify() */
34 LIST_HEAD(efi_register_notify_events);
36 /* Handle of the currently executing image */
37 static efi_handle_t current_image;
40 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
41 * we need to do trickery with caches. Since we don't want to break the EFI
42 * aware boot path, only apply hacks when loading exiting directly (breaking
43 * direct Linux EFI booting along the way - oh well).
45 static bool efi_is_direct_boot = true;
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 /* 1 if inside U-Boot code, 0 if inside EFI payload code */
58 static int entry_count = 1;
59 static int nesting_level;
60 /* GUID of the device tree table */
61 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
62 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
63 const efi_guid_t efi_guid_driver_binding_protocol =
64 EFI_DRIVER_BINDING_PROTOCOL_GUID;
66 /* event group ExitBootServices() invoked */
67 const efi_guid_t efi_guid_event_group_exit_boot_services =
68 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
69 /* event group SetVirtualAddressMap() invoked */
70 const efi_guid_t efi_guid_event_group_virtual_address_change =
71 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
72 /* event group memory map changed */
73 const efi_guid_t efi_guid_event_group_memory_map_change =
74 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
75 /* event group boot manager about to boot */
76 const efi_guid_t efi_guid_event_group_ready_to_boot =
77 EFI_EVENT_GROUP_READY_TO_BOOT;
78 /* event group ResetSystem() invoked (before ExitBootServices) */
79 const efi_guid_t efi_guid_event_group_reset_system =
80 EFI_EVENT_GROUP_RESET_SYSTEM;
82 static efi_status_t EFIAPI efi_disconnect_controller(
83 efi_handle_t controller_handle,
84 efi_handle_t driver_image_handle,
85 efi_handle_t child_handle);
87 /* Called on every callback entry */
88 int __efi_entry_check(void)
90 int ret = entry_count++ == 0;
99 /* Called on every callback exit */
100 int __efi_exit_check(void)
102 int ret = --entry_count == 0;
109 /* Called from do_bootefi_exec() */
110 void efi_save_gd(void)
118 * Special case handler for error/abort that just forces things back to u-boot
119 * world so we can dump out an abort message, without any care about returning
120 * back to UEFI world.
122 void efi_restore_gd(void)
125 /* Only restore if we're already in EFI context */
133 * indent_string() - returns a string for indenting with two spaces per level
134 * @level: indent level
136 * A maximum of ten indent levels is supported. Higher indent levels will be
139 * Return: A string for indenting with two spaces per level is
142 static const char *indent_string(int level)
144 const char *indent = " ";
145 const int max = strlen(indent);
147 level = min(max, level * 2);
148 return &indent[max - level];
151 const char *__efi_nesting(void)
153 return indent_string(nesting_level);
156 const char *__efi_nesting_inc(void)
158 return indent_string(nesting_level++);
161 const char *__efi_nesting_dec(void)
163 return indent_string(--nesting_level);
167 * efi_queue_event() - queue an EFI event
168 * @event: event to signal
170 * This function queues the notification function of the event for future
173 * The notification function is called if the task priority level of the event
174 * is higher than the current task priority level.
176 * For the SignalEvent service see efi_signal_event_ext.
179 static void efi_queue_event(struct efi_event *event)
181 if (event->notify_function) {
182 event->is_queued = true;
184 if (efi_tpl >= event->notify_tpl)
186 event->is_queued = false;
187 EFI_CALL_VOID(event->notify_function(event,
188 event->notify_context));
190 event->is_queued = false;
195 * is_valid_tpl() - check if the task priority level is valid
197 * @tpl: TPL level to check
198 * Return: status code
200 efi_status_t is_valid_tpl(efi_uintn_t tpl)
203 case TPL_APPLICATION:
209 return EFI_INVALID_PARAMETER;
214 * efi_signal_event() - signal an EFI event
215 * @event: event to signal
217 * This function signals an event. If the event belongs to an event group all
218 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
219 * their notification function is queued.
221 * For the SignalEvent service see efi_signal_event_ext.
223 void efi_signal_event(struct efi_event *event)
225 if (event->is_signaled)
228 struct efi_event *evt;
231 * The signaled state has to set before executing any
232 * notification function
234 list_for_each_entry(evt, &efi_events, link) {
235 if (!evt->group || guidcmp(evt->group, event->group))
237 if (evt->is_signaled)
239 evt->is_signaled = true;
240 if (evt->type & EVT_NOTIFY_SIGNAL &&
241 evt->notify_function)
242 evt->is_queued = true;
244 list_for_each_entry(evt, &efi_events, link) {
245 if (!evt->group || guidcmp(evt->group, event->group))
248 efi_queue_event(evt);
251 event->is_signaled = true;
252 if (event->type & EVT_NOTIFY_SIGNAL)
253 efi_queue_event(event);
258 * efi_raise_tpl() - raise the task priority level
259 * @new_tpl: new value of the task priority level
261 * This function implements the RaiseTpl service.
263 * See the Unified Extensible Firmware Interface (UEFI) specification for
266 * Return: old value of the task priority level
268 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
270 efi_uintn_t old_tpl = efi_tpl;
272 EFI_ENTRY("0x%zx", new_tpl);
274 if (new_tpl < efi_tpl)
275 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
277 if (efi_tpl > TPL_HIGH_LEVEL)
278 efi_tpl = TPL_HIGH_LEVEL;
280 EFI_EXIT(EFI_SUCCESS);
285 * efi_restore_tpl() - lower the task priority level
286 * @old_tpl: value of the task priority level to be restored
288 * This function implements the RestoreTpl service.
290 * See the Unified Extensible Firmware Interface (UEFI) specification for
293 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
295 EFI_ENTRY("0x%zx", old_tpl);
297 if (old_tpl > efi_tpl)
298 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
300 if (efi_tpl > TPL_HIGH_LEVEL)
301 efi_tpl = TPL_HIGH_LEVEL;
304 * Lowering the TPL may have made queued events eligible for execution.
308 EFI_EXIT(EFI_SUCCESS);
312 * efi_allocate_pages_ext() - allocate memory pages
313 * @type: type of allocation to be performed
314 * @memory_type: usage type of the allocated memory
315 * @pages: number of pages to be allocated
316 * @memory: allocated memory
318 * This function implements the AllocatePages service.
320 * See the Unified Extensible Firmware Interface (UEFI) specification for
323 * Return: status code
325 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
331 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
332 r = efi_allocate_pages(type, memory_type, pages, memory);
337 * efi_free_pages_ext() - Free memory pages.
338 * @memory: start of the memory area to be freed
339 * @pages: number of pages to be freed
341 * This function implements the FreePages service.
343 * See the Unified Extensible Firmware Interface (UEFI) specification for
346 * Return: status code
348 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
353 EFI_ENTRY("%llx, 0x%zx", memory, pages);
354 r = efi_free_pages(memory, pages);
359 * efi_get_memory_map_ext() - get map describing memory usage
360 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
361 * on exit the size of the copied memory map
362 * @memory_map: buffer to which the memory map is written
363 * @map_key: key for the memory map
364 * @descriptor_size: size of an individual memory descriptor
365 * @descriptor_version: version number of the memory descriptor structure
367 * This function implements the GetMemoryMap service.
369 * See the Unified Extensible Firmware Interface (UEFI) specification for
372 * Return: status code
374 static efi_status_t EFIAPI efi_get_memory_map_ext(
375 efi_uintn_t *memory_map_size,
376 struct efi_mem_desc *memory_map,
377 efi_uintn_t *map_key,
378 efi_uintn_t *descriptor_size,
379 uint32_t *descriptor_version)
383 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
384 map_key, descriptor_size, descriptor_version);
385 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
386 descriptor_size, descriptor_version);
391 * efi_allocate_pool_ext() - allocate memory from pool
392 * @pool_type: type of the pool from which memory is to be allocated
393 * @size: number of bytes to be allocated
394 * @buffer: allocated memory
396 * This function implements the AllocatePool service.
398 * See the Unified Extensible Firmware Interface (UEFI) specification for
401 * Return: status code
403 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
409 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
410 r = efi_allocate_pool(pool_type, size, buffer);
415 * efi_free_pool_ext() - free memory from pool
416 * @buffer: start of memory to be freed
418 * This function implements the FreePool service.
420 * See the Unified Extensible Firmware Interface (UEFI) specification for
423 * Return: status code
425 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
429 EFI_ENTRY("%p", buffer);
430 r = efi_free_pool(buffer);
435 * efi_add_handle() - add a new handle to the object list
437 * @handle: handle to be added
439 * The protocols list is initialized. The handle is added to the list of known
442 void efi_add_handle(efi_handle_t handle)
446 INIT_LIST_HEAD(&handle->protocols);
447 list_add_tail(&handle->link, &efi_obj_list);
451 * efi_create_handle() - create handle
452 * @handle: new handle
454 * Return: status code
456 efi_status_t efi_create_handle(efi_handle_t *handle)
458 struct efi_object *obj;
460 obj = calloc(1, sizeof(struct efi_object));
462 return EFI_OUT_OF_RESOURCES;
471 * efi_search_protocol() - find a protocol on a handle.
473 * @protocol_guid: GUID of the protocol
474 * @handler: reference to the protocol
476 * Return: status code
478 efi_status_t efi_search_protocol(const efi_handle_t handle,
479 const efi_guid_t *protocol_guid,
480 struct efi_handler **handler)
482 struct efi_object *efiobj;
483 struct list_head *lhandle;
485 if (!handle || !protocol_guid)
486 return EFI_INVALID_PARAMETER;
487 efiobj = efi_search_obj(handle);
489 return EFI_INVALID_PARAMETER;
490 list_for_each(lhandle, &efiobj->protocols) {
491 struct efi_handler *protocol;
493 protocol = list_entry(lhandle, struct efi_handler, link);
494 if (!guidcmp(protocol->guid, protocol_guid)) {
500 return EFI_NOT_FOUND;
504 * efi_remove_protocol() - delete protocol from a handle
505 * @handle: handle from which the protocol shall be deleted
506 * @protocol: GUID of the protocol to be deleted
507 * @protocol_interface: interface of the protocol implementation
509 * Return: status code
511 efi_status_t efi_remove_protocol(const efi_handle_t handle,
512 const efi_guid_t *protocol,
513 void *protocol_interface)
515 struct efi_handler *handler;
518 ret = efi_search_protocol(handle, protocol, &handler);
519 if (ret != EFI_SUCCESS)
521 if (handler->protocol_interface != protocol_interface)
522 return EFI_NOT_FOUND;
523 list_del(&handler->link);
529 * efi_remove_all_protocols() - delete all protocols from a handle
530 * @handle: handle from which the protocols shall be deleted
532 * Return: status code
534 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
536 struct efi_object *efiobj;
537 struct efi_handler *protocol;
538 struct efi_handler *pos;
540 efiobj = efi_search_obj(handle);
542 return EFI_INVALID_PARAMETER;
543 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
546 ret = efi_remove_protocol(handle, protocol->guid,
547 protocol->protocol_interface);
548 if (ret != EFI_SUCCESS)
555 * efi_delete_handle() - delete handle
557 * @obj: handle to delete
559 void efi_delete_handle(efi_handle_t handle)
563 efi_remove_all_protocols(handle);
564 list_del(&handle->link);
569 * efi_is_event() - check if a pointer is a valid event
570 * @event: pointer to check
572 * Return: status code
574 static efi_status_t efi_is_event(const struct efi_event *event)
576 const struct efi_event *evt;
579 return EFI_INVALID_PARAMETER;
580 list_for_each_entry(evt, &efi_events, link) {
584 return EFI_INVALID_PARAMETER;
588 * efi_create_event() - create an event
589 * @type: type of the event to create
590 * @notify_tpl: task priority level of the event
591 * @notify_function: notification function of the event
592 * @notify_context: pointer passed to the notification function
593 * @group: event group
594 * @event: created event
596 * This function is used inside U-Boot code to create an event.
598 * For the API function implementing the CreateEvent service see
599 * efi_create_event_ext.
601 * Return: status code
603 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
604 void (EFIAPI *notify_function) (
605 struct efi_event *event,
607 void *notify_context, efi_guid_t *group,
608 struct efi_event **event)
610 struct efi_event *evt;
613 return EFI_INVALID_PARAMETER;
618 case EVT_NOTIFY_SIGNAL:
619 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
620 case EVT_NOTIFY_WAIT:
621 case EVT_TIMER | EVT_NOTIFY_WAIT:
622 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
623 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
626 return EFI_INVALID_PARAMETER;
629 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
630 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
631 return EFI_INVALID_PARAMETER;
633 evt = calloc(1, sizeof(struct efi_event));
635 return EFI_OUT_OF_RESOURCES;
637 evt->notify_tpl = notify_tpl;
638 evt->notify_function = notify_function;
639 evt->notify_context = notify_context;
641 /* Disable timers on boot up */
642 evt->trigger_next = -1ULL;
643 evt->is_queued = false;
644 evt->is_signaled = false;
645 list_add_tail(&evt->link, &efi_events);
651 * efi_create_event_ex() - create an event in a group
652 * @type: type of the event to create
653 * @notify_tpl: task priority level of the event
654 * @notify_function: notification function of the event
655 * @notify_context: pointer passed to the notification function
656 * @event: created event
657 * @event_group: event group
659 * This function implements the CreateEventEx service.
661 * See the Unified Extensible Firmware Interface (UEFI) specification for
664 * Return: status code
666 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
667 void (EFIAPI *notify_function) (
668 struct efi_event *event,
670 void *notify_context,
671 efi_guid_t *event_group,
672 struct efi_event **event)
676 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
677 notify_context, event_group);
680 * The allowable input parameters are the same as in CreateEvent()
681 * except for the following two disallowed event types.
684 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
685 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
686 ret = EFI_INVALID_PARAMETER;
690 ret = efi_create_event(type, notify_tpl, notify_function,
691 notify_context, event_group, event);
693 return EFI_EXIT(ret);
697 * efi_create_event_ext() - create an event
698 * @type: type of the event to create
699 * @notify_tpl: task priority level of the event
700 * @notify_function: notification function of the event
701 * @notify_context: pointer passed to the notification function
702 * @event: created event
704 * This function implements the CreateEvent service.
706 * See the Unified Extensible Firmware Interface (UEFI) specification for
709 * Return: status code
711 static efi_status_t EFIAPI efi_create_event_ext(
712 uint32_t type, efi_uintn_t notify_tpl,
713 void (EFIAPI *notify_function) (
714 struct efi_event *event,
716 void *notify_context, struct efi_event **event)
718 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
720 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
721 notify_context, NULL, event));
725 * efi_timer_check() - check if a timer event has occurred
727 * Check if a timer event has occurred or a queued notification function should
730 * Our timers have to work without interrupts, so we check whenever keyboard
731 * input or disk accesses happen if enough time elapsed for them to fire.
733 void efi_timer_check(void)
735 struct efi_event *evt;
736 u64 now = timer_get_us();
738 list_for_each_entry(evt, &efi_events, link) {
740 efi_queue_event(evt);
743 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
745 switch (evt->trigger_type) {
746 case EFI_TIMER_RELATIVE:
747 evt->trigger_type = EFI_TIMER_STOP;
749 case EFI_TIMER_PERIODIC:
750 evt->trigger_next += evt->trigger_time;
755 evt->is_signaled = false;
756 efi_signal_event(evt);
762 * efi_set_timer() - set the trigger time for a timer event or stop the event
763 * @event: event for which the timer is set
764 * @type: type of the timer
765 * @trigger_time: trigger period in multiples of 100 ns
767 * This is the function for internal usage in U-Boot. For the API function
768 * implementing the SetTimer service see efi_set_timer_ext.
770 * Return: status code
772 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
773 uint64_t trigger_time)
775 /* Check that the event is valid */
776 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
777 return EFI_INVALID_PARAMETER;
780 * The parameter defines a multiple of 100 ns.
781 * We use multiples of 1000 ns. So divide by 10.
783 do_div(trigger_time, 10);
787 event->trigger_next = -1ULL;
789 case EFI_TIMER_PERIODIC:
790 case EFI_TIMER_RELATIVE:
791 event->trigger_next = timer_get_us() + trigger_time;
794 return EFI_INVALID_PARAMETER;
796 event->trigger_type = type;
797 event->trigger_time = trigger_time;
798 event->is_signaled = false;
803 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
805 * @event: event for which the timer is set
806 * @type: type of the timer
807 * @trigger_time: trigger period in multiples of 100 ns
809 * This function implements the SetTimer service.
811 * See the Unified Extensible Firmware Interface (UEFI) specification for
815 * Return: status code
817 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
818 enum efi_timer_delay type,
819 uint64_t trigger_time)
821 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
822 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
826 * efi_wait_for_event() - wait for events to be signaled
827 * @num_events: number of events to be waited for
828 * @event: events to be waited for
829 * @index: index of the event that was signaled
831 * This function implements the WaitForEvent service.
833 * See the Unified Extensible Firmware Interface (UEFI) specification for
836 * Return: status code
838 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
839 struct efi_event **event,
844 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
846 /* Check parameters */
847 if (!num_events || !event)
848 return EFI_EXIT(EFI_INVALID_PARAMETER);
850 if (efi_tpl != TPL_APPLICATION)
851 return EFI_EXIT(EFI_UNSUPPORTED);
852 for (i = 0; i < num_events; ++i) {
853 if (efi_is_event(event[i]) != EFI_SUCCESS)
854 return EFI_EXIT(EFI_INVALID_PARAMETER);
855 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
856 return EFI_EXIT(EFI_INVALID_PARAMETER);
857 if (!event[i]->is_signaled)
858 efi_queue_event(event[i]);
861 /* Wait for signal */
863 for (i = 0; i < num_events; ++i) {
864 if (event[i]->is_signaled)
867 /* Allow events to occur. */
873 * Reset the signal which is passed to the caller to allow periodic
876 event[i]->is_signaled = false;
880 return EFI_EXIT(EFI_SUCCESS);
884 * efi_signal_event_ext() - signal an EFI event
885 * @event: event to signal
887 * This function implements the SignalEvent service.
889 * See the Unified Extensible Firmware Interface (UEFI) specification for
892 * This functions sets the signaled state of the event and queues the
893 * notification function for execution.
895 * Return: status code
897 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
899 EFI_ENTRY("%p", event);
900 if (efi_is_event(event) != EFI_SUCCESS)
901 return EFI_EXIT(EFI_INVALID_PARAMETER);
902 efi_signal_event(event);
903 return EFI_EXIT(EFI_SUCCESS);
907 * efi_close_event() - close an EFI event
908 * @event: event to close
910 * This function implements the CloseEvent service.
912 * See the Unified Extensible Firmware Interface (UEFI) specification for
915 * Return: status code
917 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
919 struct efi_register_notify_event *item, *next;
921 EFI_ENTRY("%p", event);
922 if (efi_is_event(event) != EFI_SUCCESS)
923 return EFI_EXIT(EFI_INVALID_PARAMETER);
925 /* Remove protocol notify registrations for the event */
926 list_for_each_entry_safe(item, next, &efi_register_notify_events,
928 if (event == item->event) {
929 struct efi_protocol_notification *hitem, *hnext;
931 /* Remove signaled handles */
932 list_for_each_entry_safe(hitem, hnext, &item->handles,
934 list_del(&hitem->link);
937 list_del(&item->link);
942 list_del(&event->link);
944 return EFI_EXIT(EFI_SUCCESS);
948 * efi_check_event() - check if an event is signaled
949 * @event: event to check
951 * This function implements the CheckEvent service.
953 * See the Unified Extensible Firmware Interface (UEFI) specification for
956 * If an event is not signaled yet, the notification function is queued. The
957 * signaled state is cleared.
959 * Return: status code
961 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
963 EFI_ENTRY("%p", event);
965 if (efi_is_event(event) != EFI_SUCCESS ||
966 event->type & EVT_NOTIFY_SIGNAL)
967 return EFI_EXIT(EFI_INVALID_PARAMETER);
968 if (!event->is_signaled)
969 efi_queue_event(event);
970 if (event->is_signaled) {
971 event->is_signaled = false;
972 return EFI_EXIT(EFI_SUCCESS);
974 return EFI_EXIT(EFI_NOT_READY);
978 * efi_search_obj() - find the internal EFI object for a handle
979 * @handle: handle to find
983 struct efi_object *efi_search_obj(const efi_handle_t handle)
985 struct efi_object *efiobj;
990 list_for_each_entry(efiobj, &efi_obj_list, link) {
991 if (efiobj == handle)
998 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1000 * @handler: handler of a protocol
1002 * Return: open protocol info entry
1004 static struct efi_open_protocol_info_entry *efi_create_open_info(
1005 struct efi_handler *handler)
1007 struct efi_open_protocol_info_item *item;
1009 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1012 /* Append the item to the open protocol info list. */
1013 list_add_tail(&item->link, &handler->open_infos);
1019 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1020 * @item: open protocol info entry to delete
1022 * Return: status code
1024 static efi_status_t efi_delete_open_info(
1025 struct efi_open_protocol_info_item *item)
1027 list_del(&item->link);
1033 * efi_add_protocol() - install new protocol on a handle
1034 * @handle: handle on which the protocol shall be installed
1035 * @protocol: GUID of the protocol to be installed
1036 * @protocol_interface: interface of the protocol implementation
1038 * Return: status code
1040 efi_status_t efi_add_protocol(const efi_handle_t handle,
1041 const efi_guid_t *protocol,
1042 void *protocol_interface)
1044 struct efi_object *efiobj;
1045 struct efi_handler *handler;
1047 struct efi_register_notify_event *event;
1049 efiobj = efi_search_obj(handle);
1051 return EFI_INVALID_PARAMETER;
1052 ret = efi_search_protocol(handle, protocol, NULL);
1053 if (ret != EFI_NOT_FOUND)
1054 return EFI_INVALID_PARAMETER;
1055 handler = calloc(1, sizeof(struct efi_handler));
1057 return EFI_OUT_OF_RESOURCES;
1058 handler->guid = protocol;
1059 handler->protocol_interface = protocol_interface;
1060 INIT_LIST_HEAD(&handler->open_infos);
1061 list_add_tail(&handler->link, &efiobj->protocols);
1063 /* Notify registered events */
1064 list_for_each_entry(event, &efi_register_notify_events, link) {
1065 if (!guidcmp(protocol, &event->protocol)) {
1066 struct efi_protocol_notification *notif;
1068 notif = calloc(1, sizeof(*notif));
1070 list_del(&handler->link);
1072 return EFI_OUT_OF_RESOURCES;
1074 notif->handle = handle;
1075 list_add_tail(¬if->link, &event->handles);
1076 event->event->is_signaled = false;
1077 efi_signal_event(event->event);
1081 if (!guidcmp(&efi_guid_device_path, protocol))
1082 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1087 * efi_install_protocol_interface() - install protocol interface
1088 * @handle: handle on which the protocol shall be installed
1089 * @protocol: GUID of the protocol to be installed
1090 * @protocol_interface_type: type of the interface to be installed,
1091 * always EFI_NATIVE_INTERFACE
1092 * @protocol_interface: interface of the protocol implementation
1094 * This function implements the InstallProtocolInterface service.
1096 * See the Unified Extensible Firmware Interface (UEFI) specification for
1099 * Return: status code
1101 static efi_status_t EFIAPI efi_install_protocol_interface(
1102 efi_handle_t *handle, const efi_guid_t *protocol,
1103 int protocol_interface_type, void *protocol_interface)
1107 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1108 protocol_interface);
1110 if (!handle || !protocol ||
1111 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1112 r = EFI_INVALID_PARAMETER;
1116 /* Create new handle if requested. */
1118 r = efi_create_handle(handle);
1119 if (r != EFI_SUCCESS)
1121 EFI_PRINT("new handle %p\n", *handle);
1123 EFI_PRINT("handle %p\n", *handle);
1125 /* Add new protocol */
1126 r = efi_add_protocol(*handle, protocol, protocol_interface);
1132 * efi_get_drivers() - get all drivers associated to a controller
1133 * @handle: handle of the controller
1134 * @protocol: protocol GUID (optional)
1135 * @number_of_drivers: number of child controllers
1136 * @driver_handle_buffer: handles of the the drivers
1138 * The allocated buffer has to be freed with free().
1140 * Return: status code
1142 static efi_status_t efi_get_drivers(efi_handle_t handle,
1143 const efi_guid_t *protocol,
1144 efi_uintn_t *number_of_drivers,
1145 efi_handle_t **driver_handle_buffer)
1147 struct efi_handler *handler;
1148 struct efi_open_protocol_info_item *item;
1149 efi_uintn_t count = 0, i;
1152 /* Count all driver associations */
1153 list_for_each_entry(handler, &handle->protocols, link) {
1154 if (protocol && guidcmp(handler->guid, protocol))
1156 list_for_each_entry(item, &handler->open_infos, link) {
1157 if (item->info.attributes &
1158 EFI_OPEN_PROTOCOL_BY_DRIVER)
1162 *number_of_drivers = 0;
1164 *driver_handle_buffer = NULL;
1168 * Create buffer. In case of duplicate driver assignments the buffer
1169 * will be too large. But that does not harm.
1171 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1172 if (!*driver_handle_buffer)
1173 return EFI_OUT_OF_RESOURCES;
1174 /* Collect unique driver handles */
1175 list_for_each_entry(handler, &handle->protocols, link) {
1176 if (protocol && guidcmp(handler->guid, protocol))
1178 list_for_each_entry(item, &handler->open_infos, link) {
1179 if (item->info.attributes &
1180 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1181 /* Check this is a new driver */
1183 for (i = 0; i < *number_of_drivers; ++i) {
1184 if ((*driver_handle_buffer)[i] ==
1185 item->info.agent_handle)
1188 /* Copy handle to buffer */
1190 i = (*number_of_drivers)++;
1191 (*driver_handle_buffer)[i] =
1192 item->info.agent_handle;
1201 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1202 * @handle: handle of the controller
1203 * @protocol: protocol GUID (optional)
1204 * @child_handle: handle of the child to destroy
1206 * This function implements the DisconnectController service.
1208 * See the Unified Extensible Firmware Interface (UEFI) specification for
1211 * Return: status code
1213 static efi_status_t efi_disconnect_all_drivers
1214 (efi_handle_t handle,
1215 const efi_guid_t *protocol,
1216 efi_handle_t child_handle)
1218 efi_uintn_t number_of_drivers;
1219 efi_handle_t *driver_handle_buffer;
1220 efi_status_t r, ret;
1222 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
1223 &driver_handle_buffer);
1224 if (ret != EFI_SUCCESS)
1226 if (!number_of_drivers)
1228 ret = EFI_NOT_FOUND;
1229 while (number_of_drivers) {
1230 r = EFI_CALL(efi_disconnect_controller(
1232 driver_handle_buffer[--number_of_drivers],
1234 if (r == EFI_SUCCESS)
1237 free(driver_handle_buffer);
1242 * efi_uninstall_protocol() - uninstall protocol interface
1244 * @handle: handle from which the protocol shall be removed
1245 * @protocol: GUID of the protocol to be removed
1246 * @protocol_interface: interface to be removed
1248 * This function DOES NOT delete a handle without installed protocol.
1250 * Return: status code
1252 static efi_status_t efi_uninstall_protocol
1253 (efi_handle_t handle, const efi_guid_t *protocol,
1254 void *protocol_interface)
1256 struct efi_object *efiobj;
1257 struct efi_handler *handler;
1258 struct efi_open_protocol_info_item *item;
1259 struct efi_open_protocol_info_item *pos;
1263 efiobj = efi_search_obj(handle);
1265 r = EFI_INVALID_PARAMETER;
1268 /* Find the protocol on the handle */
1269 r = efi_search_protocol(handle, protocol, &handler);
1270 if (r != EFI_SUCCESS)
1272 /* Disconnect controllers */
1273 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1274 /* Close protocol */
1275 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1276 if (item->info.attributes ==
1277 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1278 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1279 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1280 list_del(&item->link);
1282 if (!list_empty(&handler->open_infos)) {
1283 r = EFI_ACCESS_DENIED;
1286 r = efi_remove_protocol(handle, protocol, protocol_interface);
1292 * efi_uninstall_protocol_interface() - uninstall protocol interface
1293 * @handle: handle from which the protocol shall be removed
1294 * @protocol: GUID of the protocol to be removed
1295 * @protocol_interface: interface to be removed
1297 * This function implements the UninstallProtocolInterface service.
1299 * See the Unified Extensible Firmware Interface (UEFI) specification for
1302 * Return: status code
1304 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1305 (efi_handle_t handle, const efi_guid_t *protocol,
1306 void *protocol_interface)
1310 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1312 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1313 if (ret != EFI_SUCCESS)
1316 /* If the last protocol has been removed, delete the handle. */
1317 if (list_empty(&handle->protocols)) {
1318 list_del(&handle->link);
1322 return EFI_EXIT(ret);
1326 * efi_register_protocol_notify() - register an event for notification when a
1327 * protocol is installed.
1328 * @protocol: GUID of the protocol whose installation shall be notified
1329 * @event: event to be signaled upon installation of the protocol
1330 * @registration: key for retrieving the registration information
1332 * This function implements the RegisterProtocolNotify service.
1333 * See the Unified Extensible Firmware Interface (UEFI) specification
1336 * Return: status code
1338 static efi_status_t EFIAPI efi_register_protocol_notify(
1339 const efi_guid_t *protocol,
1340 struct efi_event *event,
1341 void **registration)
1343 struct efi_register_notify_event *item;
1344 efi_status_t ret = EFI_SUCCESS;
1346 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1348 if (!protocol || !event || !registration) {
1349 ret = EFI_INVALID_PARAMETER;
1353 item = calloc(1, sizeof(struct efi_register_notify_event));
1355 ret = EFI_OUT_OF_RESOURCES;
1359 item->event = event;
1360 memcpy(&item->protocol, protocol, sizeof(efi_guid_t));
1361 INIT_LIST_HEAD(&item->handles);
1363 list_add_tail(&item->link, &efi_register_notify_events);
1365 *registration = item;
1367 return EFI_EXIT(ret);
1371 * efi_search() - determine if an EFI handle implements a protocol
1372 * @search_type: selection criterion
1373 * @protocol: GUID of the protocol
1374 * @search_key: registration key
1377 * See the documentation of the LocateHandle service in the UEFI specification.
1379 * Return: 0 if the handle implements the protocol
1381 static int efi_search(enum efi_locate_search_type search_type,
1382 const efi_guid_t *protocol, efi_handle_t handle)
1386 switch (search_type) {
1390 ret = efi_search_protocol(handle, protocol, NULL);
1391 return (ret != EFI_SUCCESS);
1393 /* Invalid search type */
1399 * efi_check_register_notify_event() - check if registration key is valid
1401 * Check that a pointer is a valid registration key as returned by
1402 * RegisterProtocolNotify().
1404 * @key: registration key
1405 * Return: valid registration key or NULL
1407 static struct efi_register_notify_event *efi_check_register_notify_event
1410 struct efi_register_notify_event *event;
1412 list_for_each_entry(event, &efi_register_notify_events, link) {
1413 if (event == (struct efi_register_notify_event *)key)
1420 * efi_locate_handle() - locate handles implementing a protocol
1422 * @search_type: selection criterion
1423 * @protocol: GUID of the protocol
1424 * @search_key: registration key
1425 * @buffer_size: size of the buffer to receive the handles in bytes
1426 * @buffer: buffer to receive the relevant handles
1428 * This function is meant for U-Boot internal calls. For the API implementation
1429 * of the LocateHandle service see efi_locate_handle_ext.
1431 * Return: status code
1433 static efi_status_t efi_locate_handle(
1434 enum efi_locate_search_type search_type,
1435 const efi_guid_t *protocol, void *search_key,
1436 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1438 struct efi_object *efiobj;
1439 efi_uintn_t size = 0;
1440 struct efi_register_notify_event *event;
1441 struct efi_protocol_notification *handle = NULL;
1443 /* Check parameters */
1444 switch (search_type) {
1447 case BY_REGISTER_NOTIFY:
1449 return EFI_INVALID_PARAMETER;
1450 /* Check that the registration key is valid */
1451 event = efi_check_register_notify_event(search_key);
1453 return EFI_INVALID_PARAMETER;
1457 return EFI_INVALID_PARAMETER;
1460 return EFI_INVALID_PARAMETER;
1463 /* Count how much space we need */
1464 if (search_type == BY_REGISTER_NOTIFY) {
1465 if (list_empty(&event->handles))
1466 return EFI_NOT_FOUND;
1467 handle = list_first_entry(&event->handles,
1468 struct efi_protocol_notification,
1470 efiobj = handle->handle;
1471 size += sizeof(void *);
1473 list_for_each_entry(efiobj, &efi_obj_list, link) {
1474 if (!efi_search(search_type, protocol, efiobj))
1475 size += sizeof(void *);
1478 return EFI_NOT_FOUND;
1482 return EFI_INVALID_PARAMETER;
1484 if (*buffer_size < size) {
1485 *buffer_size = size;
1486 return EFI_BUFFER_TOO_SMALL;
1489 *buffer_size = size;
1491 /* The buffer size is sufficient but there is no buffer */
1493 return EFI_INVALID_PARAMETER;
1495 /* Then fill the array */
1496 if (search_type == BY_REGISTER_NOTIFY) {
1498 list_del(&handle->link);
1500 list_for_each_entry(efiobj, &efi_obj_list, link) {
1501 if (!efi_search(search_type, protocol, efiobj))
1510 * efi_locate_handle_ext() - locate handles implementing a protocol.
1511 * @search_type: selection criterion
1512 * @protocol: GUID of the protocol
1513 * @search_key: registration key
1514 * @buffer_size: size of the buffer to receive the handles in bytes
1515 * @buffer: buffer to receive the relevant handles
1517 * This function implements the LocateHandle service.
1519 * See the Unified Extensible Firmware Interface (UEFI) specification for
1522 * Return: 0 if the handle implements the protocol
1524 static efi_status_t EFIAPI efi_locate_handle_ext(
1525 enum efi_locate_search_type search_type,
1526 const efi_guid_t *protocol, void *search_key,
1527 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1529 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1530 buffer_size, buffer);
1532 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1533 buffer_size, buffer));
1537 * efi_remove_configuration_table() - collapses configuration table entries,
1540 * @i: index of the table entry to be removed
1542 static void efi_remove_configuration_table(int i)
1544 struct efi_configuration_table *this = &systab.tables[i];
1545 struct efi_configuration_table *next = &systab.tables[i + 1];
1546 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1548 memmove(this, next, (ulong)end - (ulong)next);
1553 * efi_install_configuration_table() - adds, updates, or removes a
1554 * configuration table
1555 * @guid: GUID of the installed table
1556 * @table: table to be installed
1558 * This function is used for internal calls. For the API implementation of the
1559 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1561 * Return: status code
1563 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1566 struct efi_event *evt;
1570 return EFI_INVALID_PARAMETER;
1572 /* Check for GUID override */
1573 for (i = 0; i < systab.nr_tables; i++) {
1574 if (!guidcmp(guid, &systab.tables[i].guid)) {
1576 systab.tables[i].table = table;
1578 efi_remove_configuration_table(i);
1584 return EFI_NOT_FOUND;
1586 /* No override, check for overflow */
1587 if (i >= EFI_MAX_CONFIGURATION_TABLES)
1588 return EFI_OUT_OF_RESOURCES;
1590 /* Add a new entry */
1591 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1592 systab.tables[i].table = table;
1593 systab.nr_tables = i + 1;
1596 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1597 efi_update_table_header_crc32(&systab.hdr);
1599 /* Notify that the configuration table was changed */
1600 list_for_each_entry(evt, &efi_events, link) {
1601 if (evt->group && !guidcmp(evt->group, guid)) {
1602 efi_signal_event(evt);
1611 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1612 * configuration table.
1613 * @guid: GUID of the installed table
1614 * @table: table to be installed
1616 * This function implements the InstallConfigurationTable service.
1618 * See the Unified Extensible Firmware Interface (UEFI) specification for
1621 * Return: status code
1623 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1626 EFI_ENTRY("%pUl, %p", guid, table);
1627 return EFI_EXIT(efi_install_configuration_table(guid, table));
1631 * efi_setup_loaded_image() - initialize a loaded image
1633 * Initialize a loaded_image_info and loaded_image_info object with correct
1634 * protocols, boot-device, etc.
1636 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1639 * @device_path: device path of the loaded image
1640 * @file_path: file path of the loaded image
1641 * @handle_ptr: handle of the loaded image
1642 * @info_ptr: loaded image protocol
1643 * Return: status code
1645 efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1646 struct efi_device_path *file_path,
1647 struct efi_loaded_image_obj **handle_ptr,
1648 struct efi_loaded_image **info_ptr)
1651 struct efi_loaded_image *info = NULL;
1652 struct efi_loaded_image_obj *obj = NULL;
1653 struct efi_device_path *dp;
1655 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1659 info = calloc(1, sizeof(*info));
1661 return EFI_OUT_OF_RESOURCES;
1662 obj = calloc(1, sizeof(*obj));
1665 return EFI_OUT_OF_RESOURCES;
1667 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
1669 /* Add internal object to object list */
1670 efi_add_handle(&obj->header);
1672 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1673 info->file_path = file_path;
1674 info->system_table = &systab;
1677 info->device_handle = efi_dp_find_obj(device_path, NULL);
1679 dp = efi_dp_append(device_path, file_path);
1681 ret = EFI_OUT_OF_RESOURCES;
1687 ret = efi_add_protocol(&obj->header,
1688 &efi_guid_loaded_image_device_path, dp);
1689 if (ret != EFI_SUCCESS)
1693 * When asking for the loaded_image interface, just
1694 * return handle which points to loaded_image_info
1696 ret = efi_add_protocol(&obj->header,
1697 &efi_guid_loaded_image, info);
1698 if (ret != EFI_SUCCESS)
1706 printf("ERROR: Failure to install protocols for loaded image\n");
1707 efi_delete_handle(&obj->header);
1713 * efi_load_image_from_path() - load an image using a file path
1715 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1716 * callers obligation to update the memory type as needed.
1718 * @file_path: the path of the image to load
1719 * @buffer: buffer containing the loaded image
1720 * @size: size of the loaded image
1721 * Return: status code
1724 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1725 void **buffer, efi_uintn_t *size)
1727 struct efi_file_info *info = NULL;
1728 struct efi_file_handle *f;
1729 static efi_status_t ret;
1733 /* In case of failure nothing is returned */
1738 f = efi_file_from_path(file_path);
1740 return EFI_DEVICE_ERROR;
1744 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1746 if (ret != EFI_BUFFER_TOO_SMALL) {
1747 ret = EFI_DEVICE_ERROR;
1752 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1754 if (ret != EFI_SUCCESS)
1758 * When reading the file we do not yet know if it contains an
1759 * application, a boottime driver, or a runtime driver. So here we
1760 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1761 * update the reservation according to the image type.
1763 bs = info->file_size;
1764 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1765 EFI_BOOT_SERVICES_DATA,
1766 efi_size_in_pages(bs), &addr);
1767 if (ret != EFI_SUCCESS) {
1768 ret = EFI_OUT_OF_RESOURCES;
1773 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1774 if (ret != EFI_SUCCESS)
1775 efi_free_pages(addr, efi_size_in_pages(bs));
1776 *buffer = (void *)(uintptr_t)addr;
1779 EFI_CALL(f->close(f));
1785 * efi_load_image() - load an EFI image into memory
1786 * @boot_policy: true for request originating from the boot manager
1787 * @parent_image: the caller's image handle
1788 * @file_path: the path of the image to load
1789 * @source_buffer: memory location from which the image is installed
1790 * @source_size: size of the memory area from which the image is installed
1791 * @image_handle: handle for the newly installed image
1793 * This function implements the LoadImage service.
1795 * See the Unified Extensible Firmware Interface (UEFI) specification
1798 * Return: status code
1800 efi_status_t EFIAPI efi_load_image(bool boot_policy,
1801 efi_handle_t parent_image,
1802 struct efi_device_path *file_path,
1803 void *source_buffer,
1804 efi_uintn_t source_size,
1805 efi_handle_t *image_handle)
1807 struct efi_device_path *dp, *fp;
1808 struct efi_loaded_image *info = NULL;
1809 struct efi_loaded_image_obj **image_obj =
1810 (struct efi_loaded_image_obj **)image_handle;
1814 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1815 file_path, source_buffer, source_size, image_handle);
1817 if (!image_handle || !efi_search_obj(parent_image)) {
1818 ret = EFI_INVALID_PARAMETER;
1822 if (!source_buffer && !file_path) {
1823 ret = EFI_NOT_FOUND;
1826 /* The parent image handle must refer to a loaded image */
1827 if (!parent_image->type) {
1828 ret = EFI_INVALID_PARAMETER;
1832 if (!source_buffer) {
1833 ret = efi_load_image_from_path(file_path, &dest_buffer,
1835 if (ret != EFI_SUCCESS)
1839 ret = EFI_LOAD_ERROR;
1842 dest_buffer = source_buffer;
1844 /* split file_path which contains both the device and file parts */
1845 efi_dp_split_file_path(file_path, &dp, &fp);
1846 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1847 if (ret == EFI_SUCCESS)
1848 ret = efi_load_pe(*image_obj, dest_buffer, info);
1850 /* Release buffer to which file was loaded */
1851 efi_free_pages((uintptr_t)dest_buffer,
1852 efi_size_in_pages(source_size));
1853 if (ret == EFI_SUCCESS) {
1854 info->system_table = &systab;
1855 info->parent_handle = parent_image;
1857 /* The image is invalid. Release all associated resources. */
1858 efi_delete_handle(*image_handle);
1859 *image_handle = NULL;
1863 return EFI_EXIT(ret);
1867 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1869 static void efi_exit_caches(void)
1871 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1873 * Grub on 32bit ARM needs to have caches disabled before jumping into
1874 * a zImage, but does not know of all cache layers. Give it a hand.
1876 if (efi_is_direct_boot)
1877 cleanup_before_linux();
1882 * efi_exit_boot_services() - stop all boot services
1883 * @image_handle: handle of the loaded image
1884 * @map_key: key of the memory map
1886 * This function implements the ExitBootServices service.
1888 * See the Unified Extensible Firmware Interface (UEFI) specification
1891 * All timer events are disabled. For exit boot services events the
1892 * notification function is called. The boot services are disabled in the
1895 * Return: status code
1897 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1898 efi_uintn_t map_key)
1900 struct efi_event *evt;
1902 EFI_ENTRY("%p, %zx", image_handle, map_key);
1904 /* Check that the caller has read the current memory map */
1905 if (map_key != efi_memory_map_key)
1906 return EFI_INVALID_PARAMETER;
1908 /* Check if ExitBootServices has already been called */
1909 if (!systab.boottime)
1910 return EFI_EXIT(EFI_SUCCESS);
1912 /* Stop all timer related activities */
1913 timers_enabled = false;
1915 /* Add related events to the event group */
1916 list_for_each_entry(evt, &efi_events, link) {
1917 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1918 evt->group = &efi_guid_event_group_exit_boot_services;
1920 /* Notify that ExitBootServices is invoked. */
1921 list_for_each_entry(evt, &efi_events, link) {
1923 !guidcmp(evt->group,
1924 &efi_guid_event_group_exit_boot_services)) {
1925 efi_signal_event(evt);
1930 /* Make sure that notification functions are not called anymore */
1931 efi_tpl = TPL_HIGH_LEVEL;
1933 /* TODO: Should persist EFI variables here */
1935 board_quiesce_devices();
1937 /* Fix up caches for EFI payloads if necessary */
1940 /* This stops all lingering devices */
1941 bootm_disable_interrupts();
1943 /* Disable boot time services */
1944 systab.con_in_handle = NULL;
1945 systab.con_in = NULL;
1946 systab.con_out_handle = NULL;
1947 systab.con_out = NULL;
1948 systab.stderr_handle = NULL;
1949 systab.std_err = NULL;
1950 systab.boottime = NULL;
1952 /* Recalculate CRC32 */
1953 efi_update_table_header_crc32(&systab.hdr);
1955 /* Give the payload some time to boot */
1956 efi_set_watchdog(0);
1959 return EFI_EXIT(EFI_SUCCESS);
1963 * efi_get_next_monotonic_count() - get next value of the counter
1964 * @count: returned value of the counter
1966 * This function implements the NextMonotonicCount service.
1968 * See the Unified Extensible Firmware Interface (UEFI) specification for
1971 * Return: status code
1973 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1975 static uint64_t mono;
1978 EFI_ENTRY("%p", count);
1980 ret = EFI_INVALID_PARAMETER;
1986 return EFI_EXIT(ret);
1990 * efi_stall() - sleep
1991 * @microseconds: period to sleep in microseconds
1993 * This function implements the Stall service.
1995 * See the Unified Extensible Firmware Interface (UEFI) specification for
1998 * Return: status code
2000 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2004 EFI_ENTRY("%ld", microseconds);
2006 end_tick = get_ticks() + usec_to_tick(microseconds);
2007 while (get_ticks() < end_tick)
2010 return EFI_EXIT(EFI_SUCCESS);
2014 * efi_set_watchdog_timer() - reset the watchdog timer
2015 * @timeout: seconds before reset by watchdog
2016 * @watchdog_code: code to be logged when resetting
2017 * @data_size: size of buffer in bytes
2018 * @watchdog_data: buffer with data describing the reset reason
2020 * This function implements the SetWatchdogTimer service.
2022 * See the Unified Extensible Firmware Interface (UEFI) specification for
2025 * Return: status code
2027 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2028 uint64_t watchdog_code,
2029 unsigned long data_size,
2030 uint16_t *watchdog_data)
2032 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
2033 data_size, watchdog_data);
2034 return EFI_EXIT(efi_set_watchdog(timeout));
2038 * efi_close_protocol() - close a protocol
2039 * @handle: handle on which the protocol shall be closed
2040 * @protocol: GUID of the protocol to close
2041 * @agent_handle: handle of the driver
2042 * @controller_handle: handle of the controller
2044 * This function implements the CloseProtocol service.
2046 * See the Unified Extensible Firmware Interface (UEFI) specification for
2049 * Return: status code
2051 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2052 const efi_guid_t *protocol,
2053 efi_handle_t agent_handle,
2054 efi_handle_t controller_handle)
2056 struct efi_handler *handler;
2057 struct efi_open_protocol_info_item *item;
2058 struct efi_open_protocol_info_item *pos;
2061 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
2064 if (!efi_search_obj(agent_handle) ||
2065 (controller_handle && !efi_search_obj(controller_handle))) {
2066 r = EFI_INVALID_PARAMETER;
2069 r = efi_search_protocol(handle, protocol, &handler);
2070 if (r != EFI_SUCCESS)
2074 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2075 if (item->info.agent_handle == agent_handle &&
2076 item->info.controller_handle == controller_handle) {
2077 efi_delete_open_info(item);
2086 * efi_open_protocol_information() - provide information about then open status
2087 * of a protocol on a handle
2088 * @handle: handle for which the information shall be retrieved
2089 * @protocol: GUID of the protocol
2090 * @entry_buffer: buffer to receive the open protocol information
2091 * @entry_count: number of entries available in the buffer
2093 * This function implements the OpenProtocolInformation service.
2095 * See the Unified Extensible Firmware Interface (UEFI) specification for
2098 * Return: status code
2100 static efi_status_t EFIAPI efi_open_protocol_information(
2101 efi_handle_t handle, const efi_guid_t *protocol,
2102 struct efi_open_protocol_info_entry **entry_buffer,
2103 efi_uintn_t *entry_count)
2105 unsigned long buffer_size;
2106 unsigned long count;
2107 struct efi_handler *handler;
2108 struct efi_open_protocol_info_item *item;
2111 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2114 /* Check parameters */
2115 if (!entry_buffer) {
2116 r = EFI_INVALID_PARAMETER;
2119 r = efi_search_protocol(handle, protocol, &handler);
2120 if (r != EFI_SUCCESS)
2125 list_for_each_entry(item, &handler->open_infos, link) {
2126 if (item->info.open_count)
2129 *entry_count = count;
2130 *entry_buffer = NULL;
2137 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2138 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2139 (void **)entry_buffer);
2140 if (r != EFI_SUCCESS)
2142 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2143 if (item->info.open_count)
2144 (*entry_buffer)[--count] = item->info;
2151 * efi_protocols_per_handle() - get protocols installed on a handle
2152 * @handle: handle for which the information is retrieved
2153 * @protocol_buffer: buffer with protocol GUIDs
2154 * @protocol_buffer_count: number of entries in the buffer
2156 * This function implements the ProtocolsPerHandleService.
2158 * See the Unified Extensible Firmware Interface (UEFI) specification for
2161 * Return: status code
2163 static efi_status_t EFIAPI efi_protocols_per_handle(
2164 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2165 efi_uintn_t *protocol_buffer_count)
2167 unsigned long buffer_size;
2168 struct efi_object *efiobj;
2169 struct list_head *protocol_handle;
2172 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2173 protocol_buffer_count);
2175 if (!handle || !protocol_buffer || !protocol_buffer_count)
2176 return EFI_EXIT(EFI_INVALID_PARAMETER);
2178 *protocol_buffer = NULL;
2179 *protocol_buffer_count = 0;
2181 efiobj = efi_search_obj(handle);
2183 return EFI_EXIT(EFI_INVALID_PARAMETER);
2185 /* Count protocols */
2186 list_for_each(protocol_handle, &efiobj->protocols) {
2187 ++*protocol_buffer_count;
2191 if (*protocol_buffer_count) {
2194 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2195 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2196 (void **)protocol_buffer);
2197 if (r != EFI_SUCCESS)
2199 list_for_each(protocol_handle, &efiobj->protocols) {
2200 struct efi_handler *protocol;
2202 protocol = list_entry(protocol_handle,
2203 struct efi_handler, link);
2204 (*protocol_buffer)[j] = (void *)protocol->guid;
2209 return EFI_EXIT(EFI_SUCCESS);
2213 * efi_locate_handle_buffer() - locate handles implementing a protocol
2214 * @search_type: selection criterion
2215 * @protocol: GUID of the protocol
2216 * @search_key: registration key
2217 * @no_handles: number of returned handles
2218 * @buffer: buffer with the returned handles
2220 * This function implements the LocateHandleBuffer service.
2222 * See the Unified Extensible Firmware Interface (UEFI) specification for
2225 * Return: status code
2227 static efi_status_t EFIAPI efi_locate_handle_buffer(
2228 enum efi_locate_search_type search_type,
2229 const efi_guid_t *protocol, void *search_key,
2230 efi_uintn_t *no_handles, efi_handle_t **buffer)
2233 efi_uintn_t buffer_size = 0;
2235 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2236 no_handles, buffer);
2238 if (!no_handles || !buffer) {
2239 r = EFI_INVALID_PARAMETER;
2244 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2246 if (r != EFI_BUFFER_TOO_SMALL)
2248 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2250 if (r != EFI_SUCCESS)
2252 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2254 if (r == EFI_SUCCESS)
2255 *no_handles = buffer_size / sizeof(efi_handle_t);
2261 * efi_locate_protocol() - find an interface implementing a protocol
2262 * @protocol: GUID of the protocol
2263 * @registration: registration key passed to the notification function
2264 * @protocol_interface: interface implementing the protocol
2266 * This function implements the LocateProtocol service.
2268 * See the Unified Extensible Firmware Interface (UEFI) specification for
2271 * Return: status code
2273 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2275 void **protocol_interface)
2277 struct efi_handler *handler;
2279 struct efi_object *efiobj;
2281 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2284 * The UEFI spec explicitly requires a protocol even if a registration
2285 * key is provided. This differs from the logic in LocateHandle().
2287 if (!protocol || !protocol_interface)
2288 return EFI_EXIT(EFI_INVALID_PARAMETER);
2291 struct efi_register_notify_event *event;
2292 struct efi_protocol_notification *handle;
2294 event = efi_check_register_notify_event(registration);
2296 return EFI_EXIT(EFI_INVALID_PARAMETER);
2298 * The UEFI spec requires to return EFI_NOT_FOUND if no
2299 * protocol instance matches protocol and registration.
2300 * So let's do the same for a mismatch between protocol and
2303 if (guidcmp(&event->protocol, protocol))
2305 if (list_empty(&event->handles))
2307 handle = list_first_entry(&event->handles,
2308 struct efi_protocol_notification,
2310 efiobj = handle->handle;
2311 list_del(&handle->link);
2313 ret = efi_search_protocol(efiobj, protocol, &handler);
2314 if (ret == EFI_SUCCESS)
2317 list_for_each_entry(efiobj, &efi_obj_list, link) {
2318 ret = efi_search_protocol(efiobj, protocol, &handler);
2319 if (ret == EFI_SUCCESS)
2324 *protocol_interface = NULL;
2325 return EFI_EXIT(EFI_NOT_FOUND);
2327 *protocol_interface = handler->protocol_interface;
2328 return EFI_EXIT(EFI_SUCCESS);
2332 * efi_locate_device_path() - Get the device path and handle of an device
2333 * implementing a protocol
2334 * @protocol: GUID of the protocol
2335 * @device_path: device path
2336 * @device: handle of the device
2338 * This function implements the LocateDevicePath service.
2340 * See the Unified Extensible Firmware Interface (UEFI) specification for
2343 * Return: status code
2345 static efi_status_t EFIAPI efi_locate_device_path(
2346 const efi_guid_t *protocol,
2347 struct efi_device_path **device_path,
2348 efi_handle_t *device)
2350 struct efi_device_path *dp;
2352 struct efi_handler *handler;
2353 efi_handle_t *handles;
2355 size_t len_best = 0;
2356 efi_uintn_t no_handles;
2360 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2362 if (!protocol || !device_path || !*device_path) {
2363 ret = EFI_INVALID_PARAMETER;
2367 /* Find end of device path */
2368 len = efi_dp_instance_size(*device_path);
2370 /* Get all handles implementing the protocol */
2371 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2372 &no_handles, &handles));
2373 if (ret != EFI_SUCCESS)
2376 for (i = 0; i < no_handles; ++i) {
2377 /* Find the device path protocol */
2378 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2380 if (ret != EFI_SUCCESS)
2382 dp = (struct efi_device_path *)handler->protocol_interface;
2383 len_dp = efi_dp_instance_size(dp);
2385 * This handle can only be a better fit
2386 * if its device path length is longer than the best fit and
2387 * if its device path length is shorter of equal the searched
2390 if (len_dp <= len_best || len_dp > len)
2392 /* Check if dp is a subpath of device_path */
2393 if (memcmp(*device_path, dp, len_dp))
2396 ret = EFI_INVALID_PARAMETER;
2399 *device = handles[i];
2403 remainder = (u8 *)*device_path + len_best;
2404 *device_path = (struct efi_device_path *)remainder;
2407 ret = EFI_NOT_FOUND;
2410 return EFI_EXIT(ret);
2414 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2416 * @handle: handle on which the protocol interfaces shall be installed
2417 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2420 * This function implements the MultipleProtocolInterfaces service.
2422 * See the Unified Extensible Firmware Interface (UEFI) specification for
2425 * Return: status code
2427 efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2428 (efi_handle_t *handle, ...)
2430 EFI_ENTRY("%p", handle);
2433 const efi_guid_t *protocol;
2434 void *protocol_interface;
2435 efi_handle_t old_handle;
2436 efi_status_t r = EFI_SUCCESS;
2440 return EFI_EXIT(EFI_INVALID_PARAMETER);
2442 efi_va_start(argptr, handle);
2444 protocol = efi_va_arg(argptr, efi_guid_t*);
2447 protocol_interface = efi_va_arg(argptr, void*);
2448 /* Check that a device path has not been installed before */
2449 if (!guidcmp(protocol, &efi_guid_device_path)) {
2450 struct efi_device_path *dp = protocol_interface;
2452 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2454 if (r == EFI_SUCCESS &&
2455 dp->type == DEVICE_PATH_TYPE_END) {
2456 EFI_PRINT("Path %pD already installed\n",
2457 protocol_interface);
2458 r = EFI_ALREADY_STARTED;
2462 r = EFI_CALL(efi_install_protocol_interface(
2464 EFI_NATIVE_INTERFACE,
2465 protocol_interface));
2466 if (r != EFI_SUCCESS)
2471 if (r == EFI_SUCCESS)
2474 /* If an error occurred undo all changes. */
2475 efi_va_start(argptr, handle);
2477 protocol = efi_va_arg(argptr, efi_guid_t*);
2478 protocol_interface = efi_va_arg(argptr, void*);
2479 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2480 protocol_interface));
2488 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2490 * @handle: handle from which the protocol interfaces shall be removed
2491 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2494 * This function implements the UninstallMultipleProtocolInterfaces service.
2496 * See the Unified Extensible Firmware Interface (UEFI) specification for
2499 * Return: status code
2501 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2502 efi_handle_t handle, ...)
2504 EFI_ENTRY("%p", handle);
2507 const efi_guid_t *protocol;
2508 void *protocol_interface;
2509 efi_status_t r = EFI_SUCCESS;
2513 return EFI_EXIT(EFI_INVALID_PARAMETER);
2515 efi_va_start(argptr, handle);
2517 protocol = efi_va_arg(argptr, efi_guid_t*);
2520 protocol_interface = efi_va_arg(argptr, void*);
2521 r = efi_uninstall_protocol(handle, protocol,
2522 protocol_interface);
2523 if (r != EFI_SUCCESS)
2528 if (r == EFI_SUCCESS) {
2529 /* If the last protocol has been removed, delete the handle. */
2530 if (list_empty(&handle->protocols)) {
2531 list_del(&handle->link);
2537 /* If an error occurred undo all changes. */
2538 efi_va_start(argptr, handle);
2540 protocol = efi_va_arg(argptr, efi_guid_t*);
2541 protocol_interface = efi_va_arg(argptr, void*);
2542 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2543 EFI_NATIVE_INTERFACE,
2544 protocol_interface));
2548 /* In case of an error always return EFI_INVALID_PARAMETER */
2549 return EFI_EXIT(EFI_INVALID_PARAMETER);
2553 * efi_calculate_crc32() - calculate cyclic redundancy code
2554 * @data: buffer with data
2555 * @data_size: size of buffer in bytes
2556 * @crc32_p: cyclic redundancy code
2558 * This function implements the CalculateCrc32 service.
2560 * See the Unified Extensible Firmware Interface (UEFI) specification for
2563 * Return: status code
2565 static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2566 efi_uintn_t data_size,
2569 efi_status_t ret = EFI_SUCCESS;
2571 EFI_ENTRY("%p, %zu", data, data_size);
2572 if (!data || !data_size || !crc32_p) {
2573 ret = EFI_INVALID_PARAMETER;
2576 *crc32_p = crc32(0, data, data_size);
2578 return EFI_EXIT(ret);
2582 * efi_copy_mem() - copy memory
2583 * @destination: destination of the copy operation
2584 * @source: source of the copy operation
2585 * @length: number of bytes to copy
2587 * This function implements the CopyMem service.
2589 * See the Unified Extensible Firmware Interface (UEFI) specification for
2592 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2595 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2596 memmove(destination, source, length);
2597 EFI_EXIT(EFI_SUCCESS);
2601 * efi_set_mem() - Fill memory with a byte value.
2602 * @buffer: buffer to fill
2603 * @size: size of buffer in bytes
2604 * @value: byte to copy to the buffer
2606 * This function implements the SetMem service.
2608 * See the Unified Extensible Firmware Interface (UEFI) specification for
2611 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2613 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2614 memset(buffer, value, size);
2615 EFI_EXIT(EFI_SUCCESS);
2619 * efi_protocol_open() - open protocol interface on a handle
2620 * @handler: handler of a protocol
2621 * @protocol_interface: interface implementing the protocol
2622 * @agent_handle: handle of the driver
2623 * @controller_handle: handle of the controller
2624 * @attributes: attributes indicating how to open the protocol
2626 * Return: status code
2628 static efi_status_t efi_protocol_open(
2629 struct efi_handler *handler,
2630 void **protocol_interface, void *agent_handle,
2631 void *controller_handle, uint32_t attributes)
2633 struct efi_open_protocol_info_item *item;
2634 struct efi_open_protocol_info_entry *match = NULL;
2635 bool opened_by_driver = false;
2636 bool opened_exclusive = false;
2638 /* If there is no agent, only return the interface */
2642 /* For TEST_PROTOCOL ignore interface attribute */
2643 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2644 *protocol_interface = NULL;
2647 * Check if the protocol is already opened by a driver with the same
2648 * attributes or opened exclusively
2650 list_for_each_entry(item, &handler->open_infos, link) {
2651 if (item->info.agent_handle == agent_handle) {
2652 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2653 (item->info.attributes == attributes))
2654 return EFI_ALREADY_STARTED;
2656 if (item->info.attributes &
2657 EFI_OPEN_PROTOCOL_BY_DRIVER)
2658 opened_by_driver = true;
2660 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2661 opened_exclusive = true;
2664 /* Only one controller can open the protocol exclusively */
2665 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2666 if (opened_exclusive)
2667 return EFI_ACCESS_DENIED;
2668 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2669 if (opened_exclusive || opened_by_driver)
2670 return EFI_ACCESS_DENIED;
2673 /* Prepare exclusive opening */
2674 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2675 /* Try to disconnect controllers */
2677 opened_by_driver = false;
2678 list_for_each_entry(item, &handler->open_infos, link) {
2681 if (item->info.attributes ==
2682 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2683 ret = EFI_CALL(efi_disconnect_controller(
2684 item->info.controller_handle,
2685 item->info.agent_handle,
2687 if (ret == EFI_SUCCESS)
2689 * Child controllers may have been
2690 * removed from the open_infos list. So
2691 * let's restart the loop.
2693 goto disconnect_next;
2695 opened_by_driver = true;
2698 /* Only one driver can be connected */
2699 if (opened_by_driver)
2700 return EFI_ACCESS_DENIED;
2703 /* Find existing entry */
2704 list_for_each_entry(item, &handler->open_infos, link) {
2705 if (item->info.agent_handle == agent_handle &&
2706 item->info.controller_handle == controller_handle &&
2707 item->info.attributes == attributes)
2708 match = &item->info;
2710 /* None found, create one */
2712 match = efi_create_open_info(handler);
2714 return EFI_OUT_OF_RESOURCES;
2717 match->agent_handle = agent_handle;
2718 match->controller_handle = controller_handle;
2719 match->attributes = attributes;
2720 match->open_count++;
2723 /* For TEST_PROTOCOL ignore interface attribute. */
2724 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2725 *protocol_interface = handler->protocol_interface;
2731 * efi_open_protocol() - open protocol interface on a handle
2732 * @handle: handle on which the protocol shall be opened
2733 * @protocol: GUID of the protocol
2734 * @protocol_interface: interface implementing the protocol
2735 * @agent_handle: handle of the driver
2736 * @controller_handle: handle of the controller
2737 * @attributes: attributes indicating how to open the protocol
2739 * This function implements the OpenProtocol interface.
2741 * See the Unified Extensible Firmware Interface (UEFI) specification for
2744 * Return: status code
2746 static efi_status_t EFIAPI efi_open_protocol
2747 (efi_handle_t handle, const efi_guid_t *protocol,
2748 void **protocol_interface, efi_handle_t agent_handle,
2749 efi_handle_t controller_handle, uint32_t attributes)
2751 struct efi_handler *handler;
2752 efi_status_t r = EFI_INVALID_PARAMETER;
2754 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2755 protocol_interface, agent_handle, controller_handle,
2758 if (!handle || !protocol ||
2759 (!protocol_interface && attributes !=
2760 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2764 switch (attributes) {
2765 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2766 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2767 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2769 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2770 if (controller_handle == handle)
2773 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2774 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2775 /* Check that the controller handle is valid */
2776 if (!efi_search_obj(controller_handle))
2779 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2780 /* Check that the agent handle is valid */
2781 if (!efi_search_obj(agent_handle))
2788 r = efi_search_protocol(handle, protocol, &handler);
2793 r = EFI_UNSUPPORTED;
2799 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2800 controller_handle, attributes);
2806 * efi_start_image() - call the entry point of an image
2807 * @image_handle: handle of the image
2808 * @exit_data_size: size of the buffer
2809 * @exit_data: buffer to receive the exit data of the called image
2811 * This function implements the StartImage service.
2813 * See the Unified Extensible Firmware Interface (UEFI) specification for
2816 * Return: status code
2818 efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2819 efi_uintn_t *exit_data_size,
2822 struct efi_loaded_image_obj *image_obj =
2823 (struct efi_loaded_image_obj *)image_handle;
2826 efi_handle_t parent_image = current_image;
2828 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2830 /* Check parameters */
2831 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2833 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2834 if (ret != EFI_SUCCESS)
2835 return EFI_EXIT(EFI_INVALID_PARAMETER);
2837 efi_is_direct_boot = false;
2839 image_obj->exit_data_size = exit_data_size;
2840 image_obj->exit_data = exit_data;
2842 /* call the image! */
2843 if (setjmp(&image_obj->exit_jmp)) {
2845 * We called the entry point of the child image with EFI_CALL
2846 * in the lines below. The child image called the Exit() boot
2847 * service efi_exit() which executed the long jump that brought
2848 * us to the current line. This implies that the second half
2849 * of the EFI_CALL macro has not been executed.
2853 * efi_exit() called efi_restore_gd(). We have to undo this
2854 * otherwise __efi_entry_check() will put the wrong value into
2860 * To get ready to call EFI_EXIT below we have to execute the
2861 * missed out steps of EFI_CALL.
2863 assert(__efi_entry_check());
2864 EFI_PRINT("%lu returned by started image\n",
2865 (unsigned long)((uintptr_t)image_obj->exit_status &
2867 current_image = parent_image;
2868 return EFI_EXIT(image_obj->exit_status);
2871 current_image = image_handle;
2872 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
2873 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
2874 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2877 * Usually UEFI applications call Exit() instead of returning.
2878 * But because the world doesn't consist of ponies and unicorns,
2879 * we're happy to emulate that behavior on behalf of a payload
2882 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2886 * efi_delete_image() - delete loaded image from memory)
2888 * @image_obj: handle of the loaded image
2889 * @loaded_image_protocol: loaded image protocol
2891 static efi_status_t efi_delete_image
2892 (struct efi_loaded_image_obj *image_obj,
2893 struct efi_loaded_image *loaded_image_protocol)
2895 struct efi_object *efiobj;
2896 efi_status_t r, ret = EFI_SUCCESS;
2899 list_for_each_entry(efiobj, &efi_obj_list, link) {
2900 struct efi_handler *protocol;
2902 list_for_each_entry(protocol, &efiobj->protocols, link) {
2903 struct efi_open_protocol_info_item *info;
2905 list_for_each_entry(info, &protocol->open_infos, link) {
2906 if (info->info.agent_handle !=
2907 (efi_handle_t)image_obj)
2909 r = EFI_CALL(efi_close_protocol
2910 (efiobj, protocol->guid,
2911 info->info.agent_handle,
2912 info->info.controller_handle
2914 if (r != EFI_SUCCESS)
2917 * Closing protocols may results in further
2918 * items being deleted. To play it safe loop
2919 * over all elements again.
2926 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2927 efi_size_in_pages(loaded_image_protocol->image_size));
2928 efi_delete_handle(&image_obj->header);
2934 * efi_unload_image() - unload an EFI image
2935 * @image_handle: handle of the image to be unloaded
2937 * This function implements the UnloadImage service.
2939 * See the Unified Extensible Firmware Interface (UEFI) specification for
2942 * Return: status code
2944 efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
2946 efi_status_t ret = EFI_SUCCESS;
2947 struct efi_object *efiobj;
2948 struct efi_loaded_image *loaded_image_protocol;
2950 EFI_ENTRY("%p", image_handle);
2952 efiobj = efi_search_obj(image_handle);
2954 ret = EFI_INVALID_PARAMETER;
2957 /* Find the loaded image protocol */
2958 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2959 (void **)&loaded_image_protocol,
2961 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2962 if (ret != EFI_SUCCESS) {
2963 ret = EFI_INVALID_PARAMETER;
2966 switch (efiobj->type) {
2967 case EFI_OBJECT_TYPE_STARTED_IMAGE:
2968 /* Call the unload function */
2969 if (!loaded_image_protocol->unload) {
2970 ret = EFI_UNSUPPORTED;
2973 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
2974 if (ret != EFI_SUCCESS)
2977 case EFI_OBJECT_TYPE_LOADED_IMAGE:
2980 ret = EFI_INVALID_PARAMETER;
2983 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
2984 loaded_image_protocol);
2986 return EFI_EXIT(ret);
2990 * efi_update_exit_data() - fill exit data parameters of StartImage()
2992 * @image_obj image handle
2993 * @exit_data_size size of the exit data buffer
2994 * @exit_data buffer with data returned by UEFI payload
2995 * Return: status code
2997 static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
2998 efi_uintn_t exit_data_size,
3004 * If exit_data is not provided to StartImage(), exit_data_size must be
3007 if (!image_obj->exit_data)
3009 if (image_obj->exit_data_size)
3010 *image_obj->exit_data_size = exit_data_size;
3011 if (exit_data_size && exit_data) {
3012 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3014 (void **)image_obj->exit_data);
3015 if (ret != EFI_SUCCESS)
3017 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3019 image_obj->exit_data = NULL;
3025 * efi_exit() - leave an EFI application or driver
3026 * @image_handle: handle of the application or driver that is exiting
3027 * @exit_status: status code
3028 * @exit_data_size: size of the buffer in bytes
3029 * @exit_data: buffer with data describing an error
3031 * This function implements the Exit service.
3033 * See the Unified Extensible Firmware Interface (UEFI) specification for
3036 * Return: status code
3038 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3039 efi_status_t exit_status,
3040 efi_uintn_t exit_data_size,
3044 * TODO: We should call the unload procedure of the loaded
3048 struct efi_loaded_image *loaded_image_protocol;
3049 struct efi_loaded_image_obj *image_obj =
3050 (struct efi_loaded_image_obj *)image_handle;
3052 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3053 exit_data_size, exit_data);
3055 /* Check parameters */
3056 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3057 (void **)&loaded_image_protocol,
3059 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3060 if (ret != EFI_SUCCESS) {
3061 ret = EFI_INVALID_PARAMETER;
3065 /* Unloading of unstarted images */
3066 switch (image_obj->header.type) {
3067 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3069 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3070 efi_delete_image(image_obj, loaded_image_protocol);
3074 /* Handle does not refer to loaded image */
3075 ret = EFI_INVALID_PARAMETER;
3078 /* A started image can only be unloaded it is the last one started. */
3079 if (image_handle != current_image) {
3080 ret = EFI_INVALID_PARAMETER;
3084 /* Exit data is only foreseen in case of failure. */
3085 if (exit_status != EFI_SUCCESS) {
3086 ret = efi_update_exit_data(image_obj, exit_data_size,
3088 /* Exiting has priority. Don't return error to caller. */
3089 if (ret != EFI_SUCCESS)
3090 EFI_PRINT("%s: out of memory\n", __func__);
3092 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3093 exit_status != EFI_SUCCESS)
3094 efi_delete_image(image_obj, loaded_image_protocol);
3096 /* Make sure entry/exit counts for EFI world cross-overs match */
3097 EFI_EXIT(exit_status);
3100 * But longjmp out with the U-Boot gd, not the application's, as
3101 * the other end is a setjmp call inside EFI context.
3105 image_obj->exit_status = exit_status;
3106 longjmp(&image_obj->exit_jmp, 1);
3108 panic("EFI application exited");
3110 return EFI_EXIT(ret);
3114 * efi_handle_protocol() - get interface of a protocol on a handle
3115 * @handle: handle on which the protocol shall be opened
3116 * @protocol: GUID of the protocol
3117 * @protocol_interface: interface implementing the protocol
3119 * This function implements the HandleProtocol service.
3121 * See the Unified Extensible Firmware Interface (UEFI) specification for
3124 * Return: status code
3126 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3127 const efi_guid_t *protocol,
3128 void **protocol_interface)
3130 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
3131 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
3135 * efi_bind_controller() - bind a single driver to a controller
3136 * @controller_handle: controller handle
3137 * @driver_image_handle: driver handle
3138 * @remain_device_path: remaining path
3140 * Return: status code
3142 static efi_status_t efi_bind_controller(
3143 efi_handle_t controller_handle,
3144 efi_handle_t driver_image_handle,
3145 struct efi_device_path *remain_device_path)
3147 struct efi_driver_binding_protocol *binding_protocol;
3150 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3151 &efi_guid_driver_binding_protocol,
3152 (void **)&binding_protocol,
3153 driver_image_handle, NULL,
3154 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3155 if (r != EFI_SUCCESS)
3157 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3159 remain_device_path));
3160 if (r == EFI_SUCCESS)
3161 r = EFI_CALL(binding_protocol->start(binding_protocol,
3163 remain_device_path));
3164 EFI_CALL(efi_close_protocol(driver_image_handle,
3165 &efi_guid_driver_binding_protocol,
3166 driver_image_handle, NULL));
3171 * efi_connect_single_controller() - connect a single driver to a controller
3172 * @controller_handle: controller
3173 * @driver_image_handle: driver
3174 * @remain_device_path: remaining path
3176 * Return: status code
3178 static efi_status_t efi_connect_single_controller(
3179 efi_handle_t controller_handle,
3180 efi_handle_t *driver_image_handle,
3181 struct efi_device_path *remain_device_path)
3183 efi_handle_t *buffer;
3187 size_t connected = 0;
3189 /* Get buffer with all handles with driver binding protocol */
3190 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3191 &efi_guid_driver_binding_protocol,
3192 NULL, &count, &buffer));
3193 if (r != EFI_SUCCESS)
3196 /* Context Override */
3197 if (driver_image_handle) {
3198 for (; *driver_image_handle; ++driver_image_handle) {
3199 for (i = 0; i < count; ++i) {
3200 if (buffer[i] == *driver_image_handle) {
3202 r = efi_bind_controller(
3204 *driver_image_handle,
3205 remain_device_path);
3207 * For drivers that do not support the
3208 * controller or are already connected
3209 * we receive an error code here.
3211 if (r == EFI_SUCCESS)
3219 * TODO: Some overrides are not yet implemented:
3220 * - Platform Driver Override
3221 * - Driver Family Override Search
3222 * - Bus Specific Driver Override
3225 /* Driver Binding Search */
3226 for (i = 0; i < count; ++i) {
3228 r = efi_bind_controller(controller_handle,
3230 remain_device_path);
3231 if (r == EFI_SUCCESS)
3236 efi_free_pool(buffer);
3238 return EFI_NOT_FOUND;
3243 * efi_connect_controller() - connect a controller to a driver
3244 * @controller_handle: handle of the controller
3245 * @driver_image_handle: handle of the driver
3246 * @remain_device_path: device path of a child controller
3247 * @recursive: true to connect all child controllers
3249 * This function implements the ConnectController service.
3251 * See the Unified Extensible Firmware Interface (UEFI) specification for
3254 * First all driver binding protocol handles are tried for binding drivers.
3255 * Afterwards all handles that have opened a protocol of the controller
3256 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3258 * Return: status code
3260 static efi_status_t EFIAPI efi_connect_controller(
3261 efi_handle_t controller_handle,
3262 efi_handle_t *driver_image_handle,
3263 struct efi_device_path *remain_device_path,
3267 efi_status_t ret = EFI_NOT_FOUND;
3268 struct efi_object *efiobj;
3270 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
3271 remain_device_path, recursive);
3273 efiobj = efi_search_obj(controller_handle);
3275 ret = EFI_INVALID_PARAMETER;
3279 r = efi_connect_single_controller(controller_handle,
3280 driver_image_handle,
3281 remain_device_path);
3282 if (r == EFI_SUCCESS)
3285 struct efi_handler *handler;
3286 struct efi_open_protocol_info_item *item;
3288 list_for_each_entry(handler, &efiobj->protocols, link) {
3289 list_for_each_entry(item, &handler->open_infos, link) {
3290 if (item->info.attributes &
3291 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3292 r = EFI_CALL(efi_connect_controller(
3293 item->info.controller_handle,
3294 driver_image_handle,
3297 if (r == EFI_SUCCESS)
3303 /* Check for child controller specified by end node */
3304 if (ret != EFI_SUCCESS && remain_device_path &&
3305 remain_device_path->type == DEVICE_PATH_TYPE_END)
3308 return EFI_EXIT(ret);
3312 * efi_reinstall_protocol_interface() - reinstall protocol interface
3313 * @handle: handle on which the protocol shall be reinstalled
3314 * @protocol: GUID of the protocol to be installed
3315 * @old_interface: interface to be removed
3316 * @new_interface: interface to be installed
3318 * This function implements the ReinstallProtocolInterface service.
3320 * See the Unified Extensible Firmware Interface (UEFI) specification for
3323 * The old interface is uninstalled. The new interface is installed.
3324 * Drivers are connected.
3326 * Return: status code
3328 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3329 efi_handle_t handle, const efi_guid_t *protocol,
3330 void *old_interface, void *new_interface)
3334 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3337 /* Uninstall protocol but do not delete handle */
3338 ret = efi_uninstall_protocol(handle, protocol, old_interface);
3339 if (ret != EFI_SUCCESS)
3342 /* Install the new protocol */
3343 ret = efi_add_protocol(handle, protocol, new_interface);
3345 * The UEFI spec does not specify what should happen to the handle
3346 * if in case of an error no protocol interface remains on the handle.
3347 * So let's do nothing here.
3349 if (ret != EFI_SUCCESS)
3352 * The returned status code has to be ignored.
3353 * Do not create an error if no suitable driver for the handle exists.
3355 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3357 return EFI_EXIT(ret);
3361 * efi_get_child_controllers() - get all child controllers associated to a driver
3362 * @efiobj: handle of the controller
3363 * @driver_handle: handle of the driver
3364 * @number_of_children: number of child controllers
3365 * @child_handle_buffer: handles of the the child controllers
3367 * The allocated buffer has to be freed with free().
3369 * Return: status code
3371 static efi_status_t efi_get_child_controllers(
3372 struct efi_object *efiobj,
3373 efi_handle_t driver_handle,
3374 efi_uintn_t *number_of_children,
3375 efi_handle_t **child_handle_buffer)
3377 struct efi_handler *handler;
3378 struct efi_open_protocol_info_item *item;
3379 efi_uintn_t count = 0, i;
3382 /* Count all child controller associations */
3383 list_for_each_entry(handler, &efiobj->protocols, link) {
3384 list_for_each_entry(item, &handler->open_infos, link) {
3385 if (item->info.agent_handle == driver_handle &&
3386 item->info.attributes &
3387 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3392 * Create buffer. In case of duplicate child controller assignments
3393 * the buffer will be too large. But that does not harm.
3395 *number_of_children = 0;
3396 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3397 if (!*child_handle_buffer)
3398 return EFI_OUT_OF_RESOURCES;
3399 /* Copy unique child handles */
3400 list_for_each_entry(handler, &efiobj->protocols, link) {
3401 list_for_each_entry(item, &handler->open_infos, link) {
3402 if (item->info.agent_handle == driver_handle &&
3403 item->info.attributes &
3404 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3405 /* Check this is a new child controller */
3407 for (i = 0; i < *number_of_children; ++i) {
3408 if ((*child_handle_buffer)[i] ==
3409 item->info.controller_handle)
3412 /* Copy handle to buffer */
3414 i = (*number_of_children)++;
3415 (*child_handle_buffer)[i] =
3416 item->info.controller_handle;
3425 * efi_disconnect_controller() - disconnect a controller from a driver
3426 * @controller_handle: handle of the controller
3427 * @driver_image_handle: handle of the driver
3428 * @child_handle: handle of the child to destroy
3430 * This function implements the DisconnectController service.
3432 * See the Unified Extensible Firmware Interface (UEFI) specification for
3435 * Return: status code
3437 static efi_status_t EFIAPI efi_disconnect_controller(
3438 efi_handle_t controller_handle,
3439 efi_handle_t driver_image_handle,
3440 efi_handle_t child_handle)
3442 struct efi_driver_binding_protocol *binding_protocol;
3443 efi_handle_t *child_handle_buffer = NULL;
3444 size_t number_of_children = 0;
3446 size_t stop_count = 0;
3447 struct efi_object *efiobj;
3449 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3452 efiobj = efi_search_obj(controller_handle);
3454 r = EFI_INVALID_PARAMETER;
3458 if (child_handle && !efi_search_obj(child_handle)) {
3459 r = EFI_INVALID_PARAMETER;
3463 /* If no driver handle is supplied, disconnect all drivers */
3464 if (!driver_image_handle) {
3465 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3469 /* Create list of child handles */
3471 number_of_children = 1;
3472 child_handle_buffer = &child_handle;
3474 efi_get_child_controllers(efiobj,
3475 driver_image_handle,
3476 &number_of_children,
3477 &child_handle_buffer);
3480 /* Get the driver binding protocol */
3481 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3482 &efi_guid_driver_binding_protocol,
3483 (void **)&binding_protocol,
3484 driver_image_handle, NULL,
3485 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3486 if (r != EFI_SUCCESS)
3488 /* Remove the children */
3489 if (number_of_children) {
3490 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3493 child_handle_buffer));
3494 if (r == EFI_SUCCESS)
3497 /* Remove the driver */
3499 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3502 if (r == EFI_SUCCESS)
3504 EFI_CALL(efi_close_protocol(driver_image_handle,
3505 &efi_guid_driver_binding_protocol,
3506 driver_image_handle, NULL));
3514 free(child_handle_buffer);
3518 static struct efi_boot_services efi_boot_services = {
3520 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3521 .revision = EFI_SPECIFICATION_VERSION,
3522 .headersize = sizeof(struct efi_boot_services),
3524 .raise_tpl = efi_raise_tpl,
3525 .restore_tpl = efi_restore_tpl,
3526 .allocate_pages = efi_allocate_pages_ext,
3527 .free_pages = efi_free_pages_ext,
3528 .get_memory_map = efi_get_memory_map_ext,
3529 .allocate_pool = efi_allocate_pool_ext,
3530 .free_pool = efi_free_pool_ext,
3531 .create_event = efi_create_event_ext,
3532 .set_timer = efi_set_timer_ext,
3533 .wait_for_event = efi_wait_for_event,
3534 .signal_event = efi_signal_event_ext,
3535 .close_event = efi_close_event,
3536 .check_event = efi_check_event,
3537 .install_protocol_interface = efi_install_protocol_interface,
3538 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3539 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3540 .handle_protocol = efi_handle_protocol,
3542 .register_protocol_notify = efi_register_protocol_notify,
3543 .locate_handle = efi_locate_handle_ext,
3544 .locate_device_path = efi_locate_device_path,
3545 .install_configuration_table = efi_install_configuration_table_ext,
3546 .load_image = efi_load_image,
3547 .start_image = efi_start_image,
3549 .unload_image = efi_unload_image,
3550 .exit_boot_services = efi_exit_boot_services,
3551 .get_next_monotonic_count = efi_get_next_monotonic_count,
3553 .set_watchdog_timer = efi_set_watchdog_timer,
3554 .connect_controller = efi_connect_controller,
3555 .disconnect_controller = efi_disconnect_controller,
3556 .open_protocol = efi_open_protocol,
3557 .close_protocol = efi_close_protocol,
3558 .open_protocol_information = efi_open_protocol_information,
3559 .protocols_per_handle = efi_protocols_per_handle,
3560 .locate_handle_buffer = efi_locate_handle_buffer,
3561 .locate_protocol = efi_locate_protocol,
3562 .install_multiple_protocol_interfaces =
3563 efi_install_multiple_protocol_interfaces,
3564 .uninstall_multiple_protocol_interfaces =
3565 efi_uninstall_multiple_protocol_interfaces,
3566 .calculate_crc32 = efi_calculate_crc32,
3567 .copy_mem = efi_copy_mem,
3568 .set_mem = efi_set_mem,
3569 .create_event_ex = efi_create_event_ex,
3572 static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3574 struct efi_system_table __efi_runtime_data systab = {
3576 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3577 .revision = EFI_SPECIFICATION_VERSION,
3578 .headersize = sizeof(struct efi_system_table),
3580 .fw_vendor = firmware_vendor,
3581 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3582 .con_in = (void *)&efi_con_in,
3583 .con_out = (void *)&efi_con_out,
3584 .std_err = (void *)&efi_con_out,
3585 .runtime = (void *)&efi_runtime_services,
3586 .boottime = (void *)&efi_boot_services,
3592 * efi_initialize_system_table() - Initialize system table
3594 * Return: status code
3596 efi_status_t efi_initialize_system_table(void)
3600 /* Allocate configuration table array */
3601 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3602 EFI_MAX_CONFIGURATION_TABLES *
3603 sizeof(struct efi_configuration_table),
3604 (void **)&systab.tables);
3606 /* Set CRC32 field in table headers */
3607 efi_update_table_header_crc32(&systab.hdr);
3608 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3609 efi_update_table_header_crc32(&efi_boot_services.hdr);