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 __efi_runtime_data LIST_HEAD(efi_events);
30 /* List of queued events */
31 LIST_HEAD(efi_event_queue);
33 /* Flag to disable timer activity in ExitBootServices() */
34 static bool timers_enabled = true;
36 /* List of all events registered by RegisterProtocolNotify() */
37 LIST_HEAD(efi_register_notify_events);
39 /* Handle of the currently executing image */
40 static efi_handle_t current_image;
44 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
45 * fixed when compiling U-Boot. However, the payload does not know about that
46 * restriction so we need to manually swap its and our view of that register on
47 * EFI callback entry/exit.
49 static volatile void *efi_gd, *app_gd;
52 /* 1 if inside U-Boot code, 0 if inside EFI payload code */
53 static int entry_count = 1;
54 static int nesting_level;
55 /* GUID of the device tree table */
56 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
57 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
58 const efi_guid_t efi_guid_driver_binding_protocol =
59 EFI_DRIVER_BINDING_PROTOCOL_GUID;
61 /* event group ExitBootServices() invoked */
62 const efi_guid_t efi_guid_event_group_exit_boot_services =
63 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
64 /* event group SetVirtualAddressMap() invoked */
65 const efi_guid_t efi_guid_event_group_virtual_address_change =
66 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
67 /* event group memory map changed */
68 const efi_guid_t efi_guid_event_group_memory_map_change =
69 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
70 /* event group boot manager about to boot */
71 const efi_guid_t efi_guid_event_group_ready_to_boot =
72 EFI_EVENT_GROUP_READY_TO_BOOT;
73 /* event group ResetSystem() invoked (before ExitBootServices) */
74 const efi_guid_t efi_guid_event_group_reset_system =
75 EFI_EVENT_GROUP_RESET_SYSTEM;
77 static efi_status_t EFIAPI efi_disconnect_controller(
78 efi_handle_t controller_handle,
79 efi_handle_t driver_image_handle,
80 efi_handle_t child_handle);
82 /* Called on every callback entry */
83 int __efi_entry_check(void)
85 int ret = entry_count++ == 0;
94 /* Called on every callback exit */
95 int __efi_exit_check(void)
97 int ret = --entry_count == 0;
104 /* Called from do_bootefi_exec() */
105 void efi_save_gd(void)
113 * Special case handler for error/abort that just forces things back to u-boot
114 * world so we can dump out an abort message, without any care about returning
115 * back to UEFI world.
117 void efi_restore_gd(void)
120 /* Only restore if we're already in EFI context */
128 * indent_string() - returns a string for indenting with two spaces per level
129 * @level: indent level
131 * A maximum of ten indent levels is supported. Higher indent levels will be
134 * Return: A string for indenting with two spaces per level is
137 static const char *indent_string(int level)
139 const char *indent = " ";
140 const int max = strlen(indent);
142 level = min(max, level * 2);
143 return &indent[max - level];
146 const char *__efi_nesting(void)
148 return indent_string(nesting_level);
151 const char *__efi_nesting_inc(void)
153 return indent_string(nesting_level++);
156 const char *__efi_nesting_dec(void)
158 return indent_string(--nesting_level);
162 * efi_event_is_queued() - check if an event is queued
165 * Return: true if event is queued
167 static bool efi_event_is_queued(struct efi_event *event)
169 return !!event->queue_link.next;
173 * efi_process_event_queue() - process event queue
175 static void efi_process_event_queue(void)
177 while (!list_empty(&efi_event_queue)) {
178 struct efi_event *event;
181 event = list_first_entry(&efi_event_queue, struct efi_event,
183 if (efi_tpl >= event->notify_tpl)
185 list_del(&event->queue_link);
186 event->queue_link.next = NULL;
187 event->queue_link.prev = NULL;
188 /* Events must be executed at the event's TPL */
190 efi_tpl = event->notify_tpl;
191 EFI_CALL_VOID(event->notify_function(event,
192 event->notify_context));
194 if (event->type == EVT_NOTIFY_SIGNAL)
195 event->is_signaled = 0;
200 * efi_queue_event() - queue an EFI event
201 * @event: event to signal
203 * This function queues the notification function of the event for future
207 static void efi_queue_event(struct efi_event *event)
209 struct efi_event *item = NULL;
211 if (!event->notify_function)
214 if (!efi_event_is_queued(event)) {
216 * Events must be notified in order of decreasing task priority
217 * level. Insert the new event accordingly.
219 list_for_each_entry(item, &efi_event_queue, queue_link) {
220 if (item->notify_tpl < event->notify_tpl) {
221 list_add_tail(&event->queue_link,
228 list_add_tail(&event->queue_link, &efi_event_queue);
230 efi_process_event_queue();
234 * is_valid_tpl() - check if the task priority level is valid
236 * @tpl: TPL level to check
237 * Return: status code
239 efi_status_t is_valid_tpl(efi_uintn_t tpl)
242 case TPL_APPLICATION:
248 return EFI_INVALID_PARAMETER;
253 * efi_signal_event() - signal an EFI event
254 * @event: event to signal
256 * This function signals an event. If the event belongs to an event group all
257 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
258 * their notification function is queued.
260 * For the SignalEvent service see efi_signal_event_ext.
262 void efi_signal_event(struct efi_event *event)
264 if (event->is_signaled)
267 struct efi_event *evt;
270 * The signaled state has to set before executing any
271 * notification function
273 list_for_each_entry(evt, &efi_events, link) {
274 if (!evt->group || guidcmp(evt->group, event->group))
276 if (evt->is_signaled)
278 evt->is_signaled = true;
280 list_for_each_entry(evt, &efi_events, link) {
281 if (!evt->group || guidcmp(evt->group, event->group))
283 efi_queue_event(evt);
286 event->is_signaled = true;
287 efi_queue_event(event);
292 * efi_raise_tpl() - raise the task priority level
293 * @new_tpl: new value of the task priority level
295 * This function implements the RaiseTpl service.
297 * See the Unified Extensible Firmware Interface (UEFI) specification for
300 * Return: old value of the task priority level
302 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
304 efi_uintn_t old_tpl = efi_tpl;
306 EFI_ENTRY("0x%zx", new_tpl);
308 if (new_tpl < efi_tpl)
309 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
311 if (efi_tpl > TPL_HIGH_LEVEL)
312 efi_tpl = TPL_HIGH_LEVEL;
314 EFI_EXIT(EFI_SUCCESS);
319 * efi_restore_tpl() - lower the task priority level
320 * @old_tpl: value of the task priority level to be restored
322 * This function implements the RestoreTpl service.
324 * See the Unified Extensible Firmware Interface (UEFI) specification for
327 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
329 EFI_ENTRY("0x%zx", old_tpl);
331 if (old_tpl > efi_tpl)
332 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
334 if (efi_tpl > TPL_HIGH_LEVEL)
335 efi_tpl = TPL_HIGH_LEVEL;
338 * Lowering the TPL may have made queued events eligible for execution.
342 EFI_EXIT(EFI_SUCCESS);
346 * efi_allocate_pages_ext() - allocate memory pages
347 * @type: type of allocation to be performed
348 * @memory_type: usage type of the allocated memory
349 * @pages: number of pages to be allocated
350 * @memory: allocated memory
352 * This function implements the AllocatePages service.
354 * See the Unified Extensible Firmware Interface (UEFI) specification for
357 * Return: status code
359 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
365 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
366 r = efi_allocate_pages(type, memory_type, pages, memory);
371 * efi_free_pages_ext() - Free memory pages.
372 * @memory: start of the memory area to be freed
373 * @pages: number of pages to be freed
375 * This function implements the FreePages service.
377 * See the Unified Extensible Firmware Interface (UEFI) specification for
380 * Return: status code
382 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
387 EFI_ENTRY("%llx, 0x%zx", memory, pages);
388 r = efi_free_pages(memory, pages);
393 * efi_get_memory_map_ext() - get map describing memory usage
394 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
395 * on exit the size of the copied memory map
396 * @memory_map: buffer to which the memory map is written
397 * @map_key: key for the memory map
398 * @descriptor_size: size of an individual memory descriptor
399 * @descriptor_version: version number of the memory descriptor structure
401 * This function implements the GetMemoryMap service.
403 * See the Unified Extensible Firmware Interface (UEFI) specification for
406 * Return: status code
408 static efi_status_t EFIAPI efi_get_memory_map_ext(
409 efi_uintn_t *memory_map_size,
410 struct efi_mem_desc *memory_map,
411 efi_uintn_t *map_key,
412 efi_uintn_t *descriptor_size,
413 uint32_t *descriptor_version)
417 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
418 map_key, descriptor_size, descriptor_version);
419 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
420 descriptor_size, descriptor_version);
425 * efi_allocate_pool_ext() - allocate memory from pool
426 * @pool_type: type of the pool from which memory is to be allocated
427 * @size: number of bytes to be allocated
428 * @buffer: allocated memory
430 * This function implements the AllocatePool service.
432 * See the Unified Extensible Firmware Interface (UEFI) specification for
435 * Return: status code
437 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
443 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
444 r = efi_allocate_pool(pool_type, size, buffer);
449 * efi_free_pool_ext() - free memory from pool
450 * @buffer: start of memory to be freed
452 * This function implements the FreePool service.
454 * See the Unified Extensible Firmware Interface (UEFI) specification for
457 * Return: status code
459 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
463 EFI_ENTRY("%p", buffer);
464 r = efi_free_pool(buffer);
469 * efi_add_handle() - add a new handle to the object list
471 * @handle: handle to be added
473 * The protocols list is initialized. The handle is added to the list of known
476 void efi_add_handle(efi_handle_t handle)
480 INIT_LIST_HEAD(&handle->protocols);
481 list_add_tail(&handle->link, &efi_obj_list);
485 * efi_create_handle() - create handle
486 * @handle: new handle
488 * Return: status code
490 efi_status_t efi_create_handle(efi_handle_t *handle)
492 struct efi_object *obj;
494 obj = calloc(1, sizeof(struct efi_object));
496 return EFI_OUT_OF_RESOURCES;
505 * efi_search_protocol() - find a protocol on a handle.
507 * @protocol_guid: GUID of the protocol
508 * @handler: reference to the protocol
510 * Return: status code
512 efi_status_t efi_search_protocol(const efi_handle_t handle,
513 const efi_guid_t *protocol_guid,
514 struct efi_handler **handler)
516 struct efi_object *efiobj;
517 struct list_head *lhandle;
519 if (!handle || !protocol_guid)
520 return EFI_INVALID_PARAMETER;
521 efiobj = efi_search_obj(handle);
523 return EFI_INVALID_PARAMETER;
524 list_for_each(lhandle, &efiobj->protocols) {
525 struct efi_handler *protocol;
527 protocol = list_entry(lhandle, struct efi_handler, link);
528 if (!guidcmp(protocol->guid, protocol_guid)) {
534 return EFI_NOT_FOUND;
538 * efi_remove_protocol() - delete protocol from a handle
539 * @handle: handle from which the protocol shall be deleted
540 * @protocol: GUID of the protocol to be deleted
541 * @protocol_interface: interface of the protocol implementation
543 * Return: status code
545 efi_status_t efi_remove_protocol(const efi_handle_t handle,
546 const efi_guid_t *protocol,
547 void *protocol_interface)
549 struct efi_handler *handler;
552 ret = efi_search_protocol(handle, protocol, &handler);
553 if (ret != EFI_SUCCESS)
555 if (handler->protocol_interface != protocol_interface)
556 return EFI_NOT_FOUND;
557 list_del(&handler->link);
563 * efi_remove_all_protocols() - delete all protocols from a handle
564 * @handle: handle from which the protocols shall be deleted
566 * Return: status code
568 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
570 struct efi_object *efiobj;
571 struct efi_handler *protocol;
572 struct efi_handler *pos;
574 efiobj = efi_search_obj(handle);
576 return EFI_INVALID_PARAMETER;
577 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
580 ret = efi_remove_protocol(handle, protocol->guid,
581 protocol->protocol_interface);
582 if (ret != EFI_SUCCESS)
589 * efi_delete_handle() - delete handle
591 * @handle: handle to delete
593 void efi_delete_handle(efi_handle_t handle)
597 efi_remove_all_protocols(handle);
598 list_del(&handle->link);
603 * efi_is_event() - check if a pointer is a valid event
604 * @event: pointer to check
606 * Return: status code
608 static efi_status_t efi_is_event(const struct efi_event *event)
610 const struct efi_event *evt;
613 return EFI_INVALID_PARAMETER;
614 list_for_each_entry(evt, &efi_events, link) {
618 return EFI_INVALID_PARAMETER;
622 * efi_create_event() - create an event
624 * @type: type of the event to create
625 * @notify_tpl: task priority level of the event
626 * @notify_function: notification function of the event
627 * @notify_context: pointer passed to the notification function
628 * @group: event group
629 * @event: created event
631 * This function is used inside U-Boot code to create an event.
633 * For the API function implementing the CreateEvent service see
634 * efi_create_event_ext.
636 * Return: status code
638 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
639 void (EFIAPI *notify_function) (
640 struct efi_event *event,
642 void *notify_context, efi_guid_t *group,
643 struct efi_event **event)
645 struct efi_event *evt;
650 return EFI_INVALID_PARAMETER;
655 case EVT_NOTIFY_SIGNAL:
656 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
657 case EVT_NOTIFY_WAIT:
658 case EVT_TIMER | EVT_NOTIFY_WAIT:
659 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
660 pool_type = EFI_BOOT_SERVICES_DATA;
662 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
663 pool_type = EFI_RUNTIME_SERVICES_DATA;
666 return EFI_INVALID_PARAMETER;
669 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
670 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
671 return EFI_INVALID_PARAMETER;
673 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
675 if (ret != EFI_SUCCESS)
677 memset(evt, 0, sizeof(struct efi_event));
679 evt->notify_tpl = notify_tpl;
680 evt->notify_function = notify_function;
681 evt->notify_context = notify_context;
683 /* Disable timers on boot up */
684 evt->trigger_next = -1ULL;
685 list_add_tail(&evt->link, &efi_events);
691 * efi_create_event_ex() - create an event in a group
692 * @type: type of the event to create
693 * @notify_tpl: task priority level of the event
694 * @notify_function: notification function of the event
695 * @notify_context: pointer passed to the notification function
696 * @event: created event
697 * @event_group: event group
699 * This function implements the CreateEventEx service.
701 * See the Unified Extensible Firmware Interface (UEFI) specification for
704 * Return: status code
706 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
707 void (EFIAPI *notify_function) (
708 struct efi_event *event,
710 void *notify_context,
711 efi_guid_t *event_group,
712 struct efi_event **event)
716 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
717 notify_context, event_group);
720 * The allowable input parameters are the same as in CreateEvent()
721 * except for the following two disallowed event types.
724 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
725 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
726 ret = EFI_INVALID_PARAMETER;
730 ret = efi_create_event(type, notify_tpl, notify_function,
731 notify_context, event_group, event);
733 return EFI_EXIT(ret);
737 * efi_create_event_ext() - create an event
738 * @type: type of the event to create
739 * @notify_tpl: task priority level of the event
740 * @notify_function: notification function of the event
741 * @notify_context: pointer passed to the notification function
742 * @event: created event
744 * This function implements the CreateEvent service.
746 * See the Unified Extensible Firmware Interface (UEFI) specification for
749 * Return: status code
751 static efi_status_t EFIAPI efi_create_event_ext(
752 uint32_t type, efi_uintn_t notify_tpl,
753 void (EFIAPI *notify_function) (
754 struct efi_event *event,
756 void *notify_context, struct efi_event **event)
758 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
760 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
761 notify_context, NULL, event));
765 * efi_timer_check() - check if a timer event has occurred
767 * Check if a timer event has occurred or a queued notification function should
770 * Our timers have to work without interrupts, so we check whenever keyboard
771 * input or disk accesses happen if enough time elapsed for them to fire.
773 void efi_timer_check(void)
775 struct efi_event *evt;
776 u64 now = timer_get_us();
778 list_for_each_entry(evt, &efi_events, link) {
781 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
783 switch (evt->trigger_type) {
784 case EFI_TIMER_RELATIVE:
785 evt->trigger_type = EFI_TIMER_STOP;
787 case EFI_TIMER_PERIODIC:
788 evt->trigger_next += evt->trigger_time;
793 evt->is_signaled = false;
794 efi_signal_event(evt);
796 efi_process_event_queue();
801 * efi_set_timer() - set the trigger time for a timer event or stop the event
802 * @event: event for which the timer is set
803 * @type: type of the timer
804 * @trigger_time: trigger period in multiples of 100 ns
806 * This is the function for internal usage in U-Boot. For the API function
807 * implementing the SetTimer service see efi_set_timer_ext.
809 * Return: status code
811 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
812 uint64_t trigger_time)
814 /* Check that the event is valid */
815 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
816 return EFI_INVALID_PARAMETER;
819 * The parameter defines a multiple of 100 ns.
820 * We use multiples of 1000 ns. So divide by 10.
822 do_div(trigger_time, 10);
826 event->trigger_next = -1ULL;
828 case EFI_TIMER_PERIODIC:
829 case EFI_TIMER_RELATIVE:
830 event->trigger_next = timer_get_us() + trigger_time;
833 return EFI_INVALID_PARAMETER;
835 event->trigger_type = type;
836 event->trigger_time = trigger_time;
837 event->is_signaled = false;
842 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
844 * @event: event for which the timer is set
845 * @type: type of the timer
846 * @trigger_time: trigger period in multiples of 100 ns
848 * This function implements the SetTimer service.
850 * See the Unified Extensible Firmware Interface (UEFI) specification for
854 * Return: status code
856 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
857 enum efi_timer_delay type,
858 uint64_t trigger_time)
860 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
861 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
865 * efi_wait_for_event() - wait for events to be signaled
866 * @num_events: number of events to be waited for
867 * @event: events to be waited for
868 * @index: index of the event that was signaled
870 * This function implements the WaitForEvent service.
872 * See the Unified Extensible Firmware Interface (UEFI) specification for
875 * Return: status code
877 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
878 struct efi_event **event,
883 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
885 /* Check parameters */
886 if (!num_events || !event)
887 return EFI_EXIT(EFI_INVALID_PARAMETER);
889 if (efi_tpl != TPL_APPLICATION)
890 return EFI_EXIT(EFI_UNSUPPORTED);
891 for (i = 0; i < num_events; ++i) {
892 if (efi_is_event(event[i]) != EFI_SUCCESS)
893 return EFI_EXIT(EFI_INVALID_PARAMETER);
894 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
895 return EFI_EXIT(EFI_INVALID_PARAMETER);
896 if (!event[i]->is_signaled)
897 efi_queue_event(event[i]);
900 /* Wait for signal */
902 for (i = 0; i < num_events; ++i) {
903 if (event[i]->is_signaled)
906 /* Allow events to occur. */
912 * Reset the signal which is passed to the caller to allow periodic
915 event[i]->is_signaled = false;
919 return EFI_EXIT(EFI_SUCCESS);
923 * efi_signal_event_ext() - signal an EFI event
924 * @event: event to signal
926 * This function implements the SignalEvent service.
928 * See the Unified Extensible Firmware Interface (UEFI) specification for
931 * This functions sets the signaled state of the event and queues the
932 * notification function for execution.
934 * Return: status code
936 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
938 EFI_ENTRY("%p", event);
939 if (efi_is_event(event) != EFI_SUCCESS)
940 return EFI_EXIT(EFI_INVALID_PARAMETER);
941 efi_signal_event(event);
942 return EFI_EXIT(EFI_SUCCESS);
946 * efi_close_event() - close an EFI event
947 * @event: event to close
949 * This function implements the CloseEvent service.
951 * See the Unified Extensible Firmware Interface (UEFI) specification for
954 * Return: status code
956 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
958 struct efi_register_notify_event *item, *next;
960 EFI_ENTRY("%p", event);
961 if (efi_is_event(event) != EFI_SUCCESS)
962 return EFI_EXIT(EFI_INVALID_PARAMETER);
964 /* Remove protocol notify registrations for the event */
965 list_for_each_entry_safe(item, next, &efi_register_notify_events,
967 if (event == item->event) {
968 struct efi_protocol_notification *hitem, *hnext;
970 /* Remove signaled handles */
971 list_for_each_entry_safe(hitem, hnext, &item->handles,
973 list_del(&hitem->link);
976 list_del(&item->link);
980 /* Remove event from queue */
981 if (efi_event_is_queued(event))
982 list_del(&event->queue_link);
984 list_del(&event->link);
985 efi_free_pool(event);
986 return EFI_EXIT(EFI_SUCCESS);
990 * efi_check_event() - check if an event is signaled
991 * @event: event to check
993 * This function implements the CheckEvent service.
995 * See the Unified Extensible Firmware Interface (UEFI) specification for
998 * If an event is not signaled yet, the notification function is queued. The
999 * signaled state is cleared.
1001 * Return: status code
1003 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
1005 EFI_ENTRY("%p", event);
1007 if (efi_is_event(event) != EFI_SUCCESS ||
1008 event->type & EVT_NOTIFY_SIGNAL)
1009 return EFI_EXIT(EFI_INVALID_PARAMETER);
1010 if (!event->is_signaled)
1011 efi_queue_event(event);
1012 if (event->is_signaled) {
1013 event->is_signaled = false;
1014 return EFI_EXIT(EFI_SUCCESS);
1016 return EFI_EXIT(EFI_NOT_READY);
1020 * efi_search_obj() - find the internal EFI object for a handle
1021 * @handle: handle to find
1023 * Return: EFI object
1025 struct efi_object *efi_search_obj(const efi_handle_t handle)
1027 struct efi_object *efiobj;
1032 list_for_each_entry(efiobj, &efi_obj_list, link) {
1033 if (efiobj == handle)
1040 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1042 * @handler: handler of a protocol
1044 * Return: open protocol info entry
1046 static struct efi_open_protocol_info_entry *efi_create_open_info(
1047 struct efi_handler *handler)
1049 struct efi_open_protocol_info_item *item;
1051 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1054 /* Append the item to the open protocol info list. */
1055 list_add_tail(&item->link, &handler->open_infos);
1061 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1062 * @item: open protocol info entry to delete
1064 * Return: status code
1066 static efi_status_t efi_delete_open_info(
1067 struct efi_open_protocol_info_item *item)
1069 list_del(&item->link);
1075 * efi_add_protocol() - install new protocol on a handle
1076 * @handle: handle on which the protocol shall be installed
1077 * @protocol: GUID of the protocol to be installed
1078 * @protocol_interface: interface of the protocol implementation
1080 * Return: status code
1082 efi_status_t efi_add_protocol(const efi_handle_t handle,
1083 const efi_guid_t *protocol,
1084 void *protocol_interface)
1086 struct efi_object *efiobj;
1087 struct efi_handler *handler;
1089 struct efi_register_notify_event *event;
1091 efiobj = efi_search_obj(handle);
1093 return EFI_INVALID_PARAMETER;
1094 ret = efi_search_protocol(handle, protocol, NULL);
1095 if (ret != EFI_NOT_FOUND)
1096 return EFI_INVALID_PARAMETER;
1097 handler = calloc(1, sizeof(struct efi_handler));
1099 return EFI_OUT_OF_RESOURCES;
1100 handler->guid = protocol;
1101 handler->protocol_interface = protocol_interface;
1102 INIT_LIST_HEAD(&handler->open_infos);
1103 list_add_tail(&handler->link, &efiobj->protocols);
1105 /* Notify registered events */
1106 list_for_each_entry(event, &efi_register_notify_events, link) {
1107 if (!guidcmp(protocol, &event->protocol)) {
1108 struct efi_protocol_notification *notif;
1110 notif = calloc(1, sizeof(*notif));
1112 list_del(&handler->link);
1114 return EFI_OUT_OF_RESOURCES;
1116 notif->handle = handle;
1117 list_add_tail(¬if->link, &event->handles);
1118 event->event->is_signaled = false;
1119 efi_signal_event(event->event);
1123 if (!guidcmp(&efi_guid_device_path, protocol))
1124 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1129 * efi_install_protocol_interface() - install protocol interface
1130 * @handle: handle on which the protocol shall be installed
1131 * @protocol: GUID of the protocol to be installed
1132 * @protocol_interface_type: type of the interface to be installed,
1133 * always EFI_NATIVE_INTERFACE
1134 * @protocol_interface: interface of the protocol implementation
1136 * This function implements the InstallProtocolInterface service.
1138 * See the Unified Extensible Firmware Interface (UEFI) specification for
1141 * Return: status code
1143 static efi_status_t EFIAPI efi_install_protocol_interface(
1144 efi_handle_t *handle, const efi_guid_t *protocol,
1145 int protocol_interface_type, void *protocol_interface)
1149 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1150 protocol_interface);
1152 if (!handle || !protocol ||
1153 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1154 r = EFI_INVALID_PARAMETER;
1158 /* Create new handle if requested. */
1160 r = efi_create_handle(handle);
1161 if (r != EFI_SUCCESS)
1163 EFI_PRINT("new handle %p\n", *handle);
1165 EFI_PRINT("handle %p\n", *handle);
1167 /* Add new protocol */
1168 r = efi_add_protocol(*handle, protocol, protocol_interface);
1174 * efi_get_drivers() - get all drivers associated to a controller
1175 * @handle: handle of the controller
1176 * @protocol: protocol GUID (optional)
1177 * @number_of_drivers: number of child controllers
1178 * @driver_handle_buffer: handles of the the drivers
1180 * The allocated buffer has to be freed with free().
1182 * Return: status code
1184 static efi_status_t efi_get_drivers(efi_handle_t handle,
1185 const efi_guid_t *protocol,
1186 efi_uintn_t *number_of_drivers,
1187 efi_handle_t **driver_handle_buffer)
1189 struct efi_handler *handler;
1190 struct efi_open_protocol_info_item *item;
1191 efi_uintn_t count = 0, i;
1194 /* Count all driver associations */
1195 list_for_each_entry(handler, &handle->protocols, link) {
1196 if (protocol && guidcmp(handler->guid, protocol))
1198 list_for_each_entry(item, &handler->open_infos, link) {
1199 if (item->info.attributes &
1200 EFI_OPEN_PROTOCOL_BY_DRIVER)
1204 *number_of_drivers = 0;
1206 *driver_handle_buffer = NULL;
1210 * Create buffer. In case of duplicate driver assignments the buffer
1211 * will be too large. But that does not harm.
1213 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1214 if (!*driver_handle_buffer)
1215 return EFI_OUT_OF_RESOURCES;
1216 /* Collect unique driver handles */
1217 list_for_each_entry(handler, &handle->protocols, link) {
1218 if (protocol && guidcmp(handler->guid, protocol))
1220 list_for_each_entry(item, &handler->open_infos, link) {
1221 if (item->info.attributes &
1222 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1223 /* Check this is a new driver */
1225 for (i = 0; i < *number_of_drivers; ++i) {
1226 if ((*driver_handle_buffer)[i] ==
1227 item->info.agent_handle)
1230 /* Copy handle to buffer */
1232 i = (*number_of_drivers)++;
1233 (*driver_handle_buffer)[i] =
1234 item->info.agent_handle;
1243 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1244 * @handle: handle of the controller
1245 * @protocol: protocol GUID (optional)
1246 * @child_handle: handle of the child to destroy
1248 * This function implements the DisconnectController service.
1250 * See the Unified Extensible Firmware Interface (UEFI) specification for
1253 * Return: status code
1255 static efi_status_t efi_disconnect_all_drivers
1256 (efi_handle_t handle,
1257 const efi_guid_t *protocol,
1258 efi_handle_t child_handle)
1260 efi_uintn_t number_of_drivers;
1261 efi_handle_t *driver_handle_buffer;
1262 efi_status_t r, ret;
1264 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
1265 &driver_handle_buffer);
1266 if (ret != EFI_SUCCESS)
1268 if (!number_of_drivers)
1270 ret = EFI_NOT_FOUND;
1271 while (number_of_drivers) {
1272 r = EFI_CALL(efi_disconnect_controller(
1274 driver_handle_buffer[--number_of_drivers],
1276 if (r == EFI_SUCCESS)
1279 free(driver_handle_buffer);
1284 * efi_uninstall_protocol() - uninstall protocol interface
1286 * @handle: handle from which the protocol shall be removed
1287 * @protocol: GUID of the protocol to be removed
1288 * @protocol_interface: interface to be removed
1290 * This function DOES NOT delete a handle without installed protocol.
1292 * Return: status code
1294 static efi_status_t efi_uninstall_protocol
1295 (efi_handle_t handle, const efi_guid_t *protocol,
1296 void *protocol_interface)
1298 struct efi_object *efiobj;
1299 struct efi_handler *handler;
1300 struct efi_open_protocol_info_item *item;
1301 struct efi_open_protocol_info_item *pos;
1305 efiobj = efi_search_obj(handle);
1307 r = EFI_INVALID_PARAMETER;
1310 /* Find the protocol on the handle */
1311 r = efi_search_protocol(handle, protocol, &handler);
1312 if (r != EFI_SUCCESS)
1314 /* Disconnect controllers */
1315 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1316 /* Close protocol */
1317 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1318 if (item->info.attributes ==
1319 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1320 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1321 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1322 list_del(&item->link);
1324 if (!list_empty(&handler->open_infos)) {
1325 r = EFI_ACCESS_DENIED;
1328 r = efi_remove_protocol(handle, protocol, protocol_interface);
1334 * efi_uninstall_protocol_interface() - uninstall protocol interface
1335 * @handle: handle from which the protocol shall be removed
1336 * @protocol: GUID of the protocol to be removed
1337 * @protocol_interface: interface to be removed
1339 * This function implements the UninstallProtocolInterface service.
1341 * See the Unified Extensible Firmware Interface (UEFI) specification for
1344 * Return: status code
1346 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1347 (efi_handle_t handle, const efi_guid_t *protocol,
1348 void *protocol_interface)
1352 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1354 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1355 if (ret != EFI_SUCCESS)
1358 /* If the last protocol has been removed, delete the handle. */
1359 if (list_empty(&handle->protocols)) {
1360 list_del(&handle->link);
1364 return EFI_EXIT(ret);
1368 * efi_register_protocol_notify() - register an event for notification when a
1369 * protocol is installed.
1370 * @protocol: GUID of the protocol whose installation shall be notified
1371 * @event: event to be signaled upon installation of the protocol
1372 * @registration: key for retrieving the registration information
1374 * This function implements the RegisterProtocolNotify service.
1375 * See the Unified Extensible Firmware Interface (UEFI) specification
1378 * Return: status code
1380 static efi_status_t EFIAPI efi_register_protocol_notify(
1381 const efi_guid_t *protocol,
1382 struct efi_event *event,
1383 void **registration)
1385 struct efi_register_notify_event *item;
1386 efi_status_t ret = EFI_SUCCESS;
1388 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1390 if (!protocol || !event || !registration) {
1391 ret = EFI_INVALID_PARAMETER;
1395 item = calloc(1, sizeof(struct efi_register_notify_event));
1397 ret = EFI_OUT_OF_RESOURCES;
1401 item->event = event;
1402 memcpy(&item->protocol, protocol, sizeof(efi_guid_t));
1403 INIT_LIST_HEAD(&item->handles);
1405 list_add_tail(&item->link, &efi_register_notify_events);
1407 *registration = item;
1409 return EFI_EXIT(ret);
1413 * efi_search() - determine if an EFI handle implements a protocol
1415 * @search_type: selection criterion
1416 * @protocol: GUID of the protocol
1419 * See the documentation of the LocateHandle service in the UEFI specification.
1421 * Return: 0 if the handle implements the protocol
1423 static int efi_search(enum efi_locate_search_type search_type,
1424 const efi_guid_t *protocol, efi_handle_t handle)
1428 switch (search_type) {
1432 ret = efi_search_protocol(handle, protocol, NULL);
1433 return (ret != EFI_SUCCESS);
1435 /* Invalid search type */
1441 * efi_check_register_notify_event() - check if registration key is valid
1443 * Check that a pointer is a valid registration key as returned by
1444 * RegisterProtocolNotify().
1446 * @key: registration key
1447 * Return: valid registration key or NULL
1449 static struct efi_register_notify_event *efi_check_register_notify_event
1452 struct efi_register_notify_event *event;
1454 list_for_each_entry(event, &efi_register_notify_events, link) {
1455 if (event == (struct efi_register_notify_event *)key)
1462 * efi_locate_handle() - locate handles implementing a protocol
1464 * @search_type: selection criterion
1465 * @protocol: GUID of the protocol
1466 * @search_key: registration key
1467 * @buffer_size: size of the buffer to receive the handles in bytes
1468 * @buffer: buffer to receive the relevant handles
1470 * This function is meant for U-Boot internal calls. For the API implementation
1471 * of the LocateHandle service see efi_locate_handle_ext.
1473 * Return: status code
1475 static efi_status_t efi_locate_handle(
1476 enum efi_locate_search_type search_type,
1477 const efi_guid_t *protocol, void *search_key,
1478 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1480 struct efi_object *efiobj;
1481 efi_uintn_t size = 0;
1482 struct efi_register_notify_event *event;
1483 struct efi_protocol_notification *handle = NULL;
1485 /* Check parameters */
1486 switch (search_type) {
1489 case BY_REGISTER_NOTIFY:
1491 return EFI_INVALID_PARAMETER;
1492 /* Check that the registration key is valid */
1493 event = efi_check_register_notify_event(search_key);
1495 return EFI_INVALID_PARAMETER;
1499 return EFI_INVALID_PARAMETER;
1502 return EFI_INVALID_PARAMETER;
1505 /* Count how much space we need */
1506 if (search_type == BY_REGISTER_NOTIFY) {
1507 if (list_empty(&event->handles))
1508 return EFI_NOT_FOUND;
1509 handle = list_first_entry(&event->handles,
1510 struct efi_protocol_notification,
1512 efiobj = handle->handle;
1513 size += sizeof(void *);
1515 list_for_each_entry(efiobj, &efi_obj_list, link) {
1516 if (!efi_search(search_type, protocol, efiobj))
1517 size += sizeof(void *);
1520 return EFI_NOT_FOUND;
1524 return EFI_INVALID_PARAMETER;
1526 if (*buffer_size < size) {
1527 *buffer_size = size;
1528 return EFI_BUFFER_TOO_SMALL;
1531 *buffer_size = size;
1533 /* The buffer size is sufficient but there is no buffer */
1535 return EFI_INVALID_PARAMETER;
1537 /* Then fill the array */
1538 if (search_type == BY_REGISTER_NOTIFY) {
1540 list_del(&handle->link);
1542 list_for_each_entry(efiobj, &efi_obj_list, link) {
1543 if (!efi_search(search_type, protocol, efiobj))
1552 * efi_locate_handle_ext() - locate handles implementing a protocol.
1553 * @search_type: selection criterion
1554 * @protocol: GUID of the protocol
1555 * @search_key: registration key
1556 * @buffer_size: size of the buffer to receive the handles in bytes
1557 * @buffer: buffer to receive the relevant handles
1559 * This function implements the LocateHandle service.
1561 * See the Unified Extensible Firmware Interface (UEFI) specification for
1564 * Return: 0 if the handle implements the protocol
1566 static efi_status_t EFIAPI efi_locate_handle_ext(
1567 enum efi_locate_search_type search_type,
1568 const efi_guid_t *protocol, void *search_key,
1569 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1571 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1572 buffer_size, buffer);
1574 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1575 buffer_size, buffer));
1579 * efi_remove_configuration_table() - collapses configuration table entries,
1582 * @i: index of the table entry to be removed
1584 static void efi_remove_configuration_table(int i)
1586 struct efi_configuration_table *this = &systab.tables[i];
1587 struct efi_configuration_table *next = &systab.tables[i + 1];
1588 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1590 memmove(this, next, (ulong)end - (ulong)next);
1595 * efi_install_configuration_table() - adds, updates, or removes a
1596 * configuration table
1597 * @guid: GUID of the installed table
1598 * @table: table to be installed
1600 * This function is used for internal calls. For the API implementation of the
1601 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1603 * Return: status code
1605 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1608 struct efi_event *evt;
1612 return EFI_INVALID_PARAMETER;
1614 /* Check for GUID override */
1615 for (i = 0; i < systab.nr_tables; i++) {
1616 if (!guidcmp(guid, &systab.tables[i].guid)) {
1618 systab.tables[i].table = table;
1620 efi_remove_configuration_table(i);
1626 return EFI_NOT_FOUND;
1628 /* No override, check for overflow */
1629 if (i >= EFI_MAX_CONFIGURATION_TABLES)
1630 return EFI_OUT_OF_RESOURCES;
1632 /* Add a new entry */
1633 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1634 systab.tables[i].table = table;
1635 systab.nr_tables = i + 1;
1638 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1639 efi_update_table_header_crc32(&systab.hdr);
1641 /* Notify that the configuration table was changed */
1642 list_for_each_entry(evt, &efi_events, link) {
1643 if (evt->group && !guidcmp(evt->group, guid)) {
1644 efi_signal_event(evt);
1653 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1654 * configuration table.
1655 * @guid: GUID of the installed table
1656 * @table: table to be installed
1658 * This function implements the InstallConfigurationTable service.
1660 * See the Unified Extensible Firmware Interface (UEFI) specification for
1663 * Return: status code
1665 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1668 EFI_ENTRY("%pUl, %p", guid, table);
1669 return EFI_EXIT(efi_install_configuration_table(guid, table));
1673 * efi_setup_loaded_image() - initialize a loaded image
1675 * Initialize a loaded_image_info and loaded_image_info object with correct
1676 * protocols, boot-device, etc.
1678 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
1681 * @device_path: device path of the loaded image
1682 * @file_path: file path of the loaded image
1683 * @handle_ptr: handle of the loaded image
1684 * @info_ptr: loaded image protocol
1685 * Return: status code
1687 efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1688 struct efi_device_path *file_path,
1689 struct efi_loaded_image_obj **handle_ptr,
1690 struct efi_loaded_image **info_ptr)
1693 struct efi_loaded_image *info = NULL;
1694 struct efi_loaded_image_obj *obj = NULL;
1695 struct efi_device_path *dp;
1697 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1701 info = calloc(1, sizeof(*info));
1703 return EFI_OUT_OF_RESOURCES;
1704 obj = calloc(1, sizeof(*obj));
1707 return EFI_OUT_OF_RESOURCES;
1709 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
1711 /* Add internal object to object list */
1712 efi_add_handle(&obj->header);
1714 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1715 info->file_path = file_path;
1716 info->system_table = &systab;
1719 info->device_handle = efi_dp_find_obj(device_path, NULL);
1721 dp = efi_dp_append(device_path, file_path);
1723 ret = EFI_OUT_OF_RESOURCES;
1729 ret = efi_add_protocol(&obj->header,
1730 &efi_guid_loaded_image_device_path, dp);
1731 if (ret != EFI_SUCCESS)
1735 * When asking for the loaded_image interface, just
1736 * return handle which points to loaded_image_info
1738 ret = efi_add_protocol(&obj->header,
1739 &efi_guid_loaded_image, info);
1740 if (ret != EFI_SUCCESS)
1748 printf("ERROR: Failure to install protocols for loaded image\n");
1749 efi_delete_handle(&obj->header);
1755 * efi_load_image_from_path() - load an image using a file path
1757 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1758 * callers obligation to update the memory type as needed.
1760 * @file_path: the path of the image to load
1761 * @buffer: buffer containing the loaded image
1762 * @size: size of the loaded image
1763 * Return: status code
1766 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1767 void **buffer, efi_uintn_t *size)
1769 struct efi_file_info *info = NULL;
1770 struct efi_file_handle *f;
1771 static efi_status_t ret;
1775 /* In case of failure nothing is returned */
1780 f = efi_file_from_path(file_path);
1782 return EFI_NOT_FOUND;
1786 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1788 if (ret != EFI_BUFFER_TOO_SMALL) {
1789 ret = EFI_DEVICE_ERROR;
1794 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1796 if (ret != EFI_SUCCESS)
1800 * When reading the file we do not yet know if it contains an
1801 * application, a boottime driver, or a runtime driver. So here we
1802 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1803 * update the reservation according to the image type.
1805 bs = info->file_size;
1806 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1807 EFI_BOOT_SERVICES_DATA,
1808 efi_size_in_pages(bs), &addr);
1809 if (ret != EFI_SUCCESS) {
1810 ret = EFI_OUT_OF_RESOURCES;
1815 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1816 if (ret != EFI_SUCCESS)
1817 efi_free_pages(addr, efi_size_in_pages(bs));
1818 *buffer = (void *)(uintptr_t)addr;
1821 EFI_CALL(f->close(f));
1827 * efi_load_image() - load an EFI image into memory
1828 * @boot_policy: true for request originating from the boot manager
1829 * @parent_image: the caller's image handle
1830 * @file_path: the path of the image to load
1831 * @source_buffer: memory location from which the image is installed
1832 * @source_size: size of the memory area from which the image is installed
1833 * @image_handle: handle for the newly installed image
1835 * This function implements the LoadImage service.
1837 * See the Unified Extensible Firmware Interface (UEFI) specification
1840 * Return: status code
1842 efi_status_t EFIAPI efi_load_image(bool boot_policy,
1843 efi_handle_t parent_image,
1844 struct efi_device_path *file_path,
1845 void *source_buffer,
1846 efi_uintn_t source_size,
1847 efi_handle_t *image_handle)
1849 struct efi_device_path *dp, *fp;
1850 struct efi_loaded_image *info = NULL;
1851 struct efi_loaded_image_obj **image_obj =
1852 (struct efi_loaded_image_obj **)image_handle;
1856 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1857 file_path, source_buffer, source_size, image_handle);
1859 if (!image_handle || (!source_buffer && !file_path) ||
1860 !efi_search_obj(parent_image) ||
1861 /* The parent image handle must refer to a loaded image */
1862 !parent_image->type) {
1863 ret = EFI_INVALID_PARAMETER;
1867 if (!source_buffer) {
1868 ret = efi_load_image_from_path(file_path, &dest_buffer,
1870 if (ret != EFI_SUCCESS)
1874 ret = EFI_LOAD_ERROR;
1877 dest_buffer = source_buffer;
1879 /* split file_path which contains both the device and file parts */
1880 efi_dp_split_file_path(file_path, &dp, &fp);
1881 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1882 if (ret == EFI_SUCCESS)
1883 ret = efi_load_pe(*image_obj, dest_buffer, info);
1885 /* Release buffer to which file was loaded */
1886 efi_free_pages((uintptr_t)dest_buffer,
1887 efi_size_in_pages(source_size));
1888 if (ret == EFI_SUCCESS) {
1889 info->system_table = &systab;
1890 info->parent_handle = parent_image;
1892 /* The image is invalid. Release all associated resources. */
1893 efi_delete_handle(*image_handle);
1894 *image_handle = NULL;
1898 return EFI_EXIT(ret);
1902 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1904 static void efi_exit_caches(void)
1906 #if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
1908 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
1909 * caches are enabled.
1912 * According to the UEFI spec caches that can be managed via CP15
1913 * operations should be enabled. Caches requiring platform information
1914 * to manage should be disabled. This should not happen in
1915 * ExitBootServices() but before invoking any UEFI binary is invoked.
1917 * We want to keep the current workaround while GRUB prior to version
1918 * 2.04 is still in use.
1920 cleanup_before_linux();
1925 * efi_exit_boot_services() - stop all boot services
1926 * @image_handle: handle of the loaded image
1927 * @map_key: key of the memory map
1929 * This function implements the ExitBootServices service.
1931 * See the Unified Extensible Firmware Interface (UEFI) specification
1934 * All timer events are disabled. For exit boot services events the
1935 * notification function is called. The boot services are disabled in the
1938 * Return: status code
1940 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1941 efi_uintn_t map_key)
1943 struct efi_event *evt, *next_event;
1944 efi_status_t ret = EFI_SUCCESS;
1946 EFI_ENTRY("%p, %zx", image_handle, map_key);
1948 /* Check that the caller has read the current memory map */
1949 if (map_key != efi_memory_map_key) {
1950 ret = EFI_INVALID_PARAMETER;
1954 /* Check if ExitBootServices has already been called */
1955 if (!systab.boottime)
1958 /* Stop all timer related activities */
1959 timers_enabled = false;
1961 /* Add related events to the event group */
1962 list_for_each_entry(evt, &efi_events, link) {
1963 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1964 evt->group = &efi_guid_event_group_exit_boot_services;
1966 /* Notify that ExitBootServices is invoked. */
1967 list_for_each_entry(evt, &efi_events, link) {
1969 !guidcmp(evt->group,
1970 &efi_guid_event_group_exit_boot_services)) {
1971 efi_signal_event(evt);
1976 /* Make sure that notification functions are not called anymore */
1977 efi_tpl = TPL_HIGH_LEVEL;
1979 /* Notify variable services */
1980 efi_variables_boot_exit_notify();
1982 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
1983 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
1984 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
1985 list_del(&evt->link);
1988 board_quiesce_devices();
1990 /* Patch out unsupported runtime function */
1991 efi_runtime_detach();
1993 /* Fix up caches for EFI payloads if necessary */
1996 /* This stops all lingering devices */
1997 bootm_disable_interrupts();
1999 /* Disable boot time services */
2000 systab.con_in_handle = NULL;
2001 systab.con_in = NULL;
2002 systab.con_out_handle = NULL;
2003 systab.con_out = NULL;
2004 systab.stderr_handle = NULL;
2005 systab.std_err = NULL;
2006 systab.boottime = NULL;
2008 /* Recalculate CRC32 */
2009 efi_update_table_header_crc32(&systab.hdr);
2011 /* Give the payload some time to boot */
2012 efi_set_watchdog(0);
2015 return EFI_EXIT(ret);
2019 * efi_get_next_monotonic_count() - get next value of the counter
2020 * @count: returned value of the counter
2022 * This function implements the NextMonotonicCount service.
2024 * See the Unified Extensible Firmware Interface (UEFI) specification for
2027 * Return: status code
2029 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2031 static uint64_t mono;
2034 EFI_ENTRY("%p", count);
2036 ret = EFI_INVALID_PARAMETER;
2042 return EFI_EXIT(ret);
2046 * efi_stall() - sleep
2047 * @microseconds: period to sleep in microseconds
2049 * This function implements the Stall service.
2051 * See the Unified Extensible Firmware Interface (UEFI) specification for
2054 * Return: status code
2056 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2060 EFI_ENTRY("%ld", microseconds);
2062 end_tick = get_ticks() + usec_to_tick(microseconds);
2063 while (get_ticks() < end_tick)
2066 return EFI_EXIT(EFI_SUCCESS);
2070 * efi_set_watchdog_timer() - reset the watchdog timer
2071 * @timeout: seconds before reset by watchdog
2072 * @watchdog_code: code to be logged when resetting
2073 * @data_size: size of buffer in bytes
2074 * @watchdog_data: buffer with data describing the reset reason
2076 * This function implements the SetWatchdogTimer service.
2078 * See the Unified Extensible Firmware Interface (UEFI) specification for
2081 * Return: status code
2083 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2084 uint64_t watchdog_code,
2085 unsigned long data_size,
2086 uint16_t *watchdog_data)
2088 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
2089 data_size, watchdog_data);
2090 return EFI_EXIT(efi_set_watchdog(timeout));
2094 * efi_close_protocol() - close a protocol
2095 * @handle: handle on which the protocol shall be closed
2096 * @protocol: GUID of the protocol to close
2097 * @agent_handle: handle of the driver
2098 * @controller_handle: handle of the controller
2100 * This function implements the CloseProtocol service.
2102 * See the Unified Extensible Firmware Interface (UEFI) specification for
2105 * Return: status code
2107 static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2108 const efi_guid_t *protocol,
2109 efi_handle_t agent_handle,
2110 efi_handle_t controller_handle)
2112 struct efi_handler *handler;
2113 struct efi_open_protocol_info_item *item;
2114 struct efi_open_protocol_info_item *pos;
2117 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
2120 if (!efi_search_obj(agent_handle) ||
2121 (controller_handle && !efi_search_obj(controller_handle))) {
2122 r = EFI_INVALID_PARAMETER;
2125 r = efi_search_protocol(handle, protocol, &handler);
2126 if (r != EFI_SUCCESS)
2130 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2131 if (item->info.agent_handle == agent_handle &&
2132 item->info.controller_handle == controller_handle) {
2133 efi_delete_open_info(item);
2142 * efi_open_protocol_information() - provide information about then open status
2143 * of a protocol on a handle
2144 * @handle: handle for which the information shall be retrieved
2145 * @protocol: GUID of the protocol
2146 * @entry_buffer: buffer to receive the open protocol information
2147 * @entry_count: number of entries available in the buffer
2149 * This function implements the OpenProtocolInformation service.
2151 * See the Unified Extensible Firmware Interface (UEFI) specification for
2154 * Return: status code
2156 static efi_status_t EFIAPI efi_open_protocol_information(
2157 efi_handle_t handle, const efi_guid_t *protocol,
2158 struct efi_open_protocol_info_entry **entry_buffer,
2159 efi_uintn_t *entry_count)
2161 unsigned long buffer_size;
2162 unsigned long count;
2163 struct efi_handler *handler;
2164 struct efi_open_protocol_info_item *item;
2167 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2170 /* Check parameters */
2171 if (!entry_buffer) {
2172 r = EFI_INVALID_PARAMETER;
2175 r = efi_search_protocol(handle, protocol, &handler);
2176 if (r != EFI_SUCCESS)
2181 list_for_each_entry(item, &handler->open_infos, link) {
2182 if (item->info.open_count)
2185 *entry_count = count;
2186 *entry_buffer = NULL;
2193 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2194 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2195 (void **)entry_buffer);
2196 if (r != EFI_SUCCESS)
2198 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2199 if (item->info.open_count)
2200 (*entry_buffer)[--count] = item->info;
2207 * efi_protocols_per_handle() - get protocols installed on a handle
2208 * @handle: handle for which the information is retrieved
2209 * @protocol_buffer: buffer with protocol GUIDs
2210 * @protocol_buffer_count: number of entries in the buffer
2212 * This function implements the ProtocolsPerHandleService.
2214 * See the Unified Extensible Firmware Interface (UEFI) specification for
2217 * Return: status code
2219 static efi_status_t EFIAPI efi_protocols_per_handle(
2220 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2221 efi_uintn_t *protocol_buffer_count)
2223 unsigned long buffer_size;
2224 struct efi_object *efiobj;
2225 struct list_head *protocol_handle;
2228 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2229 protocol_buffer_count);
2231 if (!handle || !protocol_buffer || !protocol_buffer_count)
2232 return EFI_EXIT(EFI_INVALID_PARAMETER);
2234 *protocol_buffer = NULL;
2235 *protocol_buffer_count = 0;
2237 efiobj = efi_search_obj(handle);
2239 return EFI_EXIT(EFI_INVALID_PARAMETER);
2241 /* Count protocols */
2242 list_for_each(protocol_handle, &efiobj->protocols) {
2243 ++*protocol_buffer_count;
2247 if (*protocol_buffer_count) {
2250 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2251 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2252 (void **)protocol_buffer);
2253 if (r != EFI_SUCCESS)
2255 list_for_each(protocol_handle, &efiobj->protocols) {
2256 struct efi_handler *protocol;
2258 protocol = list_entry(protocol_handle,
2259 struct efi_handler, link);
2260 (*protocol_buffer)[j] = (void *)protocol->guid;
2265 return EFI_EXIT(EFI_SUCCESS);
2269 * efi_locate_handle_buffer() - locate handles implementing a protocol
2270 * @search_type: selection criterion
2271 * @protocol: GUID of the protocol
2272 * @search_key: registration key
2273 * @no_handles: number of returned handles
2274 * @buffer: buffer with the returned handles
2276 * This function implements the LocateHandleBuffer service.
2278 * See the Unified Extensible Firmware Interface (UEFI) specification for
2281 * Return: status code
2283 static efi_status_t EFIAPI efi_locate_handle_buffer(
2284 enum efi_locate_search_type search_type,
2285 const efi_guid_t *protocol, void *search_key,
2286 efi_uintn_t *no_handles, efi_handle_t **buffer)
2289 efi_uintn_t buffer_size = 0;
2291 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2292 no_handles, buffer);
2294 if (!no_handles || !buffer) {
2295 r = EFI_INVALID_PARAMETER;
2300 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2302 if (r != EFI_BUFFER_TOO_SMALL)
2304 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2306 if (r != EFI_SUCCESS)
2308 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2310 if (r == EFI_SUCCESS)
2311 *no_handles = buffer_size / sizeof(efi_handle_t);
2317 * efi_locate_protocol() - find an interface implementing a protocol
2318 * @protocol: GUID of the protocol
2319 * @registration: registration key passed to the notification function
2320 * @protocol_interface: interface implementing the protocol
2322 * This function implements the LocateProtocol service.
2324 * See the Unified Extensible Firmware Interface (UEFI) specification for
2327 * Return: status code
2329 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2331 void **protocol_interface)
2333 struct efi_handler *handler;
2335 struct efi_object *efiobj;
2337 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2340 * The UEFI spec explicitly requires a protocol even if a registration
2341 * key is provided. This differs from the logic in LocateHandle().
2343 if (!protocol || !protocol_interface)
2344 return EFI_EXIT(EFI_INVALID_PARAMETER);
2347 struct efi_register_notify_event *event;
2348 struct efi_protocol_notification *handle;
2350 event = efi_check_register_notify_event(registration);
2352 return EFI_EXIT(EFI_INVALID_PARAMETER);
2354 * The UEFI spec requires to return EFI_NOT_FOUND if no
2355 * protocol instance matches protocol and registration.
2356 * So let's do the same for a mismatch between protocol and
2359 if (guidcmp(&event->protocol, protocol))
2361 if (list_empty(&event->handles))
2363 handle = list_first_entry(&event->handles,
2364 struct efi_protocol_notification,
2366 efiobj = handle->handle;
2367 list_del(&handle->link);
2369 ret = efi_search_protocol(efiobj, protocol, &handler);
2370 if (ret == EFI_SUCCESS)
2373 list_for_each_entry(efiobj, &efi_obj_list, link) {
2374 ret = efi_search_protocol(efiobj, protocol, &handler);
2375 if (ret == EFI_SUCCESS)
2380 *protocol_interface = NULL;
2381 return EFI_EXIT(EFI_NOT_FOUND);
2383 *protocol_interface = handler->protocol_interface;
2384 return EFI_EXIT(EFI_SUCCESS);
2388 * efi_locate_device_path() - Get the device path and handle of an device
2389 * implementing a protocol
2390 * @protocol: GUID of the protocol
2391 * @device_path: device path
2392 * @device: handle of the device
2394 * This function implements the LocateDevicePath service.
2396 * See the Unified Extensible Firmware Interface (UEFI) specification for
2399 * Return: status code
2401 static efi_status_t EFIAPI efi_locate_device_path(
2402 const efi_guid_t *protocol,
2403 struct efi_device_path **device_path,
2404 efi_handle_t *device)
2406 struct efi_device_path *dp;
2408 struct efi_handler *handler;
2409 efi_handle_t *handles;
2411 size_t len_best = 0;
2412 efi_uintn_t no_handles;
2416 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2418 if (!protocol || !device_path || !*device_path) {
2419 ret = EFI_INVALID_PARAMETER;
2423 /* Find end of device path */
2424 len = efi_dp_instance_size(*device_path);
2426 /* Get all handles implementing the protocol */
2427 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2428 &no_handles, &handles));
2429 if (ret != EFI_SUCCESS)
2432 for (i = 0; i < no_handles; ++i) {
2433 /* Find the device path protocol */
2434 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2436 if (ret != EFI_SUCCESS)
2438 dp = (struct efi_device_path *)handler->protocol_interface;
2439 len_dp = efi_dp_instance_size(dp);
2441 * This handle can only be a better fit
2442 * if its device path length is longer than the best fit and
2443 * if its device path length is shorter of equal the searched
2446 if (len_dp <= len_best || len_dp > len)
2448 /* Check if dp is a subpath of device_path */
2449 if (memcmp(*device_path, dp, len_dp))
2452 ret = EFI_INVALID_PARAMETER;
2455 *device = handles[i];
2459 remainder = (u8 *)*device_path + len_best;
2460 *device_path = (struct efi_device_path *)remainder;
2463 ret = EFI_NOT_FOUND;
2466 return EFI_EXIT(ret);
2470 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2472 * @handle: handle on which the protocol interfaces shall be installed
2473 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2476 * This function implements the MultipleProtocolInterfaces service.
2478 * See the Unified Extensible Firmware Interface (UEFI) specification for
2481 * Return: status code
2483 efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2484 (efi_handle_t *handle, ...)
2486 EFI_ENTRY("%p", handle);
2489 const efi_guid_t *protocol;
2490 void *protocol_interface;
2491 efi_handle_t old_handle;
2492 efi_status_t r = EFI_SUCCESS;
2496 return EFI_EXIT(EFI_INVALID_PARAMETER);
2498 efi_va_start(argptr, handle);
2500 protocol = efi_va_arg(argptr, efi_guid_t*);
2503 protocol_interface = efi_va_arg(argptr, void*);
2504 /* Check that a device path has not been installed before */
2505 if (!guidcmp(protocol, &efi_guid_device_path)) {
2506 struct efi_device_path *dp = protocol_interface;
2508 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2510 if (r == EFI_SUCCESS &&
2511 dp->type == DEVICE_PATH_TYPE_END) {
2512 EFI_PRINT("Path %pD already installed\n",
2513 protocol_interface);
2514 r = EFI_ALREADY_STARTED;
2518 r = EFI_CALL(efi_install_protocol_interface(
2520 EFI_NATIVE_INTERFACE,
2521 protocol_interface));
2522 if (r != EFI_SUCCESS)
2527 if (r == EFI_SUCCESS)
2530 /* If an error occurred undo all changes. */
2531 efi_va_start(argptr, handle);
2533 protocol = efi_va_arg(argptr, efi_guid_t*);
2534 protocol_interface = efi_va_arg(argptr, void*);
2535 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2536 protocol_interface));
2544 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2546 * @handle: handle from which the protocol interfaces shall be removed
2547 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2550 * This function implements the UninstallMultipleProtocolInterfaces service.
2552 * See the Unified Extensible Firmware Interface (UEFI) specification for
2555 * Return: status code
2557 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2558 efi_handle_t handle, ...)
2560 EFI_ENTRY("%p", handle);
2563 const efi_guid_t *protocol;
2564 void *protocol_interface;
2565 efi_status_t r = EFI_SUCCESS;
2569 return EFI_EXIT(EFI_INVALID_PARAMETER);
2571 efi_va_start(argptr, handle);
2573 protocol = efi_va_arg(argptr, efi_guid_t*);
2576 protocol_interface = efi_va_arg(argptr, void*);
2577 r = efi_uninstall_protocol(handle, protocol,
2578 protocol_interface);
2579 if (r != EFI_SUCCESS)
2584 if (r == EFI_SUCCESS) {
2585 /* If the last protocol has been removed, delete the handle. */
2586 if (list_empty(&handle->protocols)) {
2587 list_del(&handle->link);
2593 /* If an error occurred undo all changes. */
2594 efi_va_start(argptr, handle);
2596 protocol = efi_va_arg(argptr, efi_guid_t*);
2597 protocol_interface = efi_va_arg(argptr, void*);
2598 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2599 EFI_NATIVE_INTERFACE,
2600 protocol_interface));
2604 /* In case of an error always return EFI_INVALID_PARAMETER */
2605 return EFI_EXIT(EFI_INVALID_PARAMETER);
2609 * efi_calculate_crc32() - calculate cyclic redundancy code
2610 * @data: buffer with data
2611 * @data_size: size of buffer in bytes
2612 * @crc32_p: cyclic redundancy code
2614 * This function implements the CalculateCrc32 service.
2616 * See the Unified Extensible Firmware Interface (UEFI) specification for
2619 * Return: status code
2621 static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2622 efi_uintn_t data_size,
2625 efi_status_t ret = EFI_SUCCESS;
2627 EFI_ENTRY("%p, %zu", data, data_size);
2628 if (!data || !data_size || !crc32_p) {
2629 ret = EFI_INVALID_PARAMETER;
2632 *crc32_p = crc32(0, data, data_size);
2634 return EFI_EXIT(ret);
2638 * efi_copy_mem() - copy memory
2639 * @destination: destination of the copy operation
2640 * @source: source of the copy operation
2641 * @length: number of bytes to copy
2643 * This function implements the CopyMem service.
2645 * See the Unified Extensible Firmware Interface (UEFI) specification for
2648 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2651 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2652 memmove(destination, source, length);
2653 EFI_EXIT(EFI_SUCCESS);
2657 * efi_set_mem() - Fill memory with a byte value.
2658 * @buffer: buffer to fill
2659 * @size: size of buffer in bytes
2660 * @value: byte to copy to the buffer
2662 * This function implements the SetMem service.
2664 * See the Unified Extensible Firmware Interface (UEFI) specification for
2667 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2669 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2670 memset(buffer, value, size);
2671 EFI_EXIT(EFI_SUCCESS);
2675 * efi_protocol_open() - open protocol interface on a handle
2676 * @handler: handler of a protocol
2677 * @protocol_interface: interface implementing the protocol
2678 * @agent_handle: handle of the driver
2679 * @controller_handle: handle of the controller
2680 * @attributes: attributes indicating how to open the protocol
2682 * Return: status code
2684 static efi_status_t efi_protocol_open(
2685 struct efi_handler *handler,
2686 void **protocol_interface, void *agent_handle,
2687 void *controller_handle, uint32_t attributes)
2689 struct efi_open_protocol_info_item *item;
2690 struct efi_open_protocol_info_entry *match = NULL;
2691 bool opened_by_driver = false;
2692 bool opened_exclusive = false;
2694 /* If there is no agent, only return the interface */
2698 /* For TEST_PROTOCOL ignore interface attribute */
2699 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2700 *protocol_interface = NULL;
2703 * Check if the protocol is already opened by a driver with the same
2704 * attributes or opened exclusively
2706 list_for_each_entry(item, &handler->open_infos, link) {
2707 if (item->info.agent_handle == agent_handle) {
2708 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2709 (item->info.attributes == attributes))
2710 return EFI_ALREADY_STARTED;
2712 if (item->info.attributes &
2713 EFI_OPEN_PROTOCOL_BY_DRIVER)
2714 opened_by_driver = true;
2716 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2717 opened_exclusive = true;
2720 /* Only one controller can open the protocol exclusively */
2721 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2722 if (opened_exclusive)
2723 return EFI_ACCESS_DENIED;
2724 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2725 if (opened_exclusive || opened_by_driver)
2726 return EFI_ACCESS_DENIED;
2729 /* Prepare exclusive opening */
2730 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2731 /* Try to disconnect controllers */
2733 opened_by_driver = false;
2734 list_for_each_entry(item, &handler->open_infos, link) {
2737 if (item->info.attributes ==
2738 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2739 ret = EFI_CALL(efi_disconnect_controller(
2740 item->info.controller_handle,
2741 item->info.agent_handle,
2743 if (ret == EFI_SUCCESS)
2745 * Child controllers may have been
2746 * removed from the open_infos list. So
2747 * let's restart the loop.
2749 goto disconnect_next;
2751 opened_by_driver = true;
2754 /* Only one driver can be connected */
2755 if (opened_by_driver)
2756 return EFI_ACCESS_DENIED;
2759 /* Find existing entry */
2760 list_for_each_entry(item, &handler->open_infos, link) {
2761 if (item->info.agent_handle == agent_handle &&
2762 item->info.controller_handle == controller_handle &&
2763 item->info.attributes == attributes)
2764 match = &item->info;
2766 /* None found, create one */
2768 match = efi_create_open_info(handler);
2770 return EFI_OUT_OF_RESOURCES;
2773 match->agent_handle = agent_handle;
2774 match->controller_handle = controller_handle;
2775 match->attributes = attributes;
2776 match->open_count++;
2779 /* For TEST_PROTOCOL ignore interface attribute. */
2780 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2781 *protocol_interface = handler->protocol_interface;
2787 * efi_open_protocol() - open protocol interface on a handle
2788 * @handle: handle on which the protocol shall be opened
2789 * @protocol: GUID of the protocol
2790 * @protocol_interface: interface implementing the protocol
2791 * @agent_handle: handle of the driver
2792 * @controller_handle: handle of the controller
2793 * @attributes: attributes indicating how to open the protocol
2795 * This function implements the OpenProtocol interface.
2797 * See the Unified Extensible Firmware Interface (UEFI) specification for
2800 * Return: status code
2802 static efi_status_t EFIAPI efi_open_protocol
2803 (efi_handle_t handle, const efi_guid_t *protocol,
2804 void **protocol_interface, efi_handle_t agent_handle,
2805 efi_handle_t controller_handle, uint32_t attributes)
2807 struct efi_handler *handler;
2808 efi_status_t r = EFI_INVALID_PARAMETER;
2810 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2811 protocol_interface, agent_handle, controller_handle,
2814 if (!handle || !protocol ||
2815 (!protocol_interface && attributes !=
2816 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2820 switch (attributes) {
2821 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2822 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2823 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2825 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2826 if (controller_handle == handle)
2829 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2830 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2831 /* Check that the controller handle is valid */
2832 if (!efi_search_obj(controller_handle))
2835 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2836 /* Check that the agent handle is valid */
2837 if (!efi_search_obj(agent_handle))
2844 r = efi_search_protocol(handle, protocol, &handler);
2849 r = EFI_UNSUPPORTED;
2855 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2856 controller_handle, attributes);
2862 * efi_start_image() - call the entry point of an image
2863 * @image_handle: handle of the image
2864 * @exit_data_size: size of the buffer
2865 * @exit_data: buffer to receive the exit data of the called image
2867 * This function implements the StartImage service.
2869 * See the Unified Extensible Firmware Interface (UEFI) specification for
2872 * Return: status code
2874 efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2875 efi_uintn_t *exit_data_size,
2878 struct efi_loaded_image_obj *image_obj =
2879 (struct efi_loaded_image_obj *)image_handle;
2882 efi_handle_t parent_image = current_image;
2884 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2886 /* Check parameters */
2887 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2888 return EFI_EXIT(EFI_INVALID_PARAMETER);
2890 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2892 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2893 if (ret != EFI_SUCCESS)
2894 return EFI_EXIT(EFI_INVALID_PARAMETER);
2896 image_obj->exit_data_size = exit_data_size;
2897 image_obj->exit_data = exit_data;
2899 /* call the image! */
2900 if (setjmp(&image_obj->exit_jmp)) {
2902 * We called the entry point of the child image with EFI_CALL
2903 * in the lines below. The child image called the Exit() boot
2904 * service efi_exit() which executed the long jump that brought
2905 * us to the current line. This implies that the second half
2906 * of the EFI_CALL macro has not been executed.
2910 * efi_exit() called efi_restore_gd(). We have to undo this
2911 * otherwise __efi_entry_check() will put the wrong value into
2917 * To get ready to call EFI_EXIT below we have to execute the
2918 * missed out steps of EFI_CALL.
2920 assert(__efi_entry_check());
2921 EFI_PRINT("%lu returned by started image\n",
2922 (unsigned long)((uintptr_t)image_obj->exit_status &
2924 current_image = parent_image;
2925 return EFI_EXIT(image_obj->exit_status);
2928 current_image = image_handle;
2929 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
2930 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
2931 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2934 * Usually UEFI applications call Exit() instead of returning.
2935 * But because the world doesn't consist of ponies and unicorns,
2936 * we're happy to emulate that behavior on behalf of a payload
2939 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2943 * efi_delete_image() - delete loaded image from memory)
2945 * @image_obj: handle of the loaded image
2946 * @loaded_image_protocol: loaded image protocol
2948 static efi_status_t efi_delete_image
2949 (struct efi_loaded_image_obj *image_obj,
2950 struct efi_loaded_image *loaded_image_protocol)
2952 struct efi_object *efiobj;
2953 efi_status_t r, ret = EFI_SUCCESS;
2956 list_for_each_entry(efiobj, &efi_obj_list, link) {
2957 struct efi_handler *protocol;
2959 list_for_each_entry(protocol, &efiobj->protocols, link) {
2960 struct efi_open_protocol_info_item *info;
2962 list_for_each_entry(info, &protocol->open_infos, link) {
2963 if (info->info.agent_handle !=
2964 (efi_handle_t)image_obj)
2966 r = EFI_CALL(efi_close_protocol
2967 (efiobj, protocol->guid,
2968 info->info.agent_handle,
2969 info->info.controller_handle
2971 if (r != EFI_SUCCESS)
2974 * Closing protocols may results in further
2975 * items being deleted. To play it safe loop
2976 * over all elements again.
2983 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2984 efi_size_in_pages(loaded_image_protocol->image_size));
2985 efi_delete_handle(&image_obj->header);
2991 * efi_unload_image() - unload an EFI image
2992 * @image_handle: handle of the image to be unloaded
2994 * This function implements the UnloadImage service.
2996 * See the Unified Extensible Firmware Interface (UEFI) specification for
2999 * Return: status code
3001 efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3003 efi_status_t ret = EFI_SUCCESS;
3004 struct efi_object *efiobj;
3005 struct efi_loaded_image *loaded_image_protocol;
3007 EFI_ENTRY("%p", image_handle);
3009 efiobj = efi_search_obj(image_handle);
3011 ret = EFI_INVALID_PARAMETER;
3014 /* Find the loaded image protocol */
3015 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3016 (void **)&loaded_image_protocol,
3018 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3019 if (ret != EFI_SUCCESS) {
3020 ret = EFI_INVALID_PARAMETER;
3023 switch (efiobj->type) {
3024 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3025 /* Call the unload function */
3026 if (!loaded_image_protocol->unload) {
3027 ret = EFI_UNSUPPORTED;
3030 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3031 if (ret != EFI_SUCCESS)
3034 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3037 ret = EFI_INVALID_PARAMETER;
3040 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3041 loaded_image_protocol);
3043 return EFI_EXIT(ret);
3047 * efi_update_exit_data() - fill exit data parameters of StartImage()
3049 * @image_obj: image handle
3050 * @exit_data_size: size of the exit data buffer
3051 * @exit_data: buffer with data returned by UEFI payload
3052 * Return: status code
3054 static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3055 efi_uintn_t exit_data_size,
3061 * If exit_data is not provided to StartImage(), exit_data_size must be
3064 if (!image_obj->exit_data)
3066 if (image_obj->exit_data_size)
3067 *image_obj->exit_data_size = exit_data_size;
3068 if (exit_data_size && exit_data) {
3069 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3071 (void **)image_obj->exit_data);
3072 if (ret != EFI_SUCCESS)
3074 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3076 image_obj->exit_data = NULL;
3082 * efi_exit() - leave an EFI application or driver
3083 * @image_handle: handle of the application or driver that is exiting
3084 * @exit_status: status code
3085 * @exit_data_size: size of the buffer in bytes
3086 * @exit_data: buffer with data describing an error
3088 * This function implements the Exit service.
3090 * See the Unified Extensible Firmware Interface (UEFI) specification for
3093 * Return: status code
3095 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3096 efi_status_t exit_status,
3097 efi_uintn_t exit_data_size,
3101 * TODO: We should call the unload procedure of the loaded
3105 struct efi_loaded_image *loaded_image_protocol;
3106 struct efi_loaded_image_obj *image_obj =
3107 (struct efi_loaded_image_obj *)image_handle;
3109 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3110 exit_data_size, exit_data);
3112 /* Check parameters */
3113 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3114 (void **)&loaded_image_protocol,
3116 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3117 if (ret != EFI_SUCCESS) {
3118 ret = EFI_INVALID_PARAMETER;
3122 /* Unloading of unstarted images */
3123 switch (image_obj->header.type) {
3124 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3126 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3127 efi_delete_image(image_obj, loaded_image_protocol);
3131 /* Handle does not refer to loaded image */
3132 ret = EFI_INVALID_PARAMETER;
3135 /* A started image can only be unloaded it is the last one started. */
3136 if (image_handle != current_image) {
3137 ret = EFI_INVALID_PARAMETER;
3141 /* Exit data is only foreseen in case of failure. */
3142 if (exit_status != EFI_SUCCESS) {
3143 ret = efi_update_exit_data(image_obj, exit_data_size,
3145 /* Exiting has priority. Don't return error to caller. */
3146 if (ret != EFI_SUCCESS)
3147 EFI_PRINT("%s: out of memory\n", __func__);
3149 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3150 exit_status != EFI_SUCCESS)
3151 efi_delete_image(image_obj, loaded_image_protocol);
3153 /* Make sure entry/exit counts for EFI world cross-overs match */
3154 EFI_EXIT(exit_status);
3157 * But longjmp out with the U-Boot gd, not the application's, as
3158 * the other end is a setjmp call inside EFI context.
3162 image_obj->exit_status = exit_status;
3163 longjmp(&image_obj->exit_jmp, 1);
3165 panic("EFI application exited");
3167 return EFI_EXIT(ret);
3171 * efi_handle_protocol() - get interface of a protocol on a handle
3172 * @handle: handle on which the protocol shall be opened
3173 * @protocol: GUID of the protocol
3174 * @protocol_interface: interface implementing the protocol
3176 * This function implements the HandleProtocol service.
3178 * See the Unified Extensible Firmware Interface (UEFI) specification for
3181 * Return: status code
3183 static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3184 const efi_guid_t *protocol,
3185 void **protocol_interface)
3187 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
3188 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
3192 * efi_bind_controller() - bind a single driver to a controller
3193 * @controller_handle: controller handle
3194 * @driver_image_handle: driver handle
3195 * @remain_device_path: remaining path
3197 * Return: status code
3199 static efi_status_t efi_bind_controller(
3200 efi_handle_t controller_handle,
3201 efi_handle_t driver_image_handle,
3202 struct efi_device_path *remain_device_path)
3204 struct efi_driver_binding_protocol *binding_protocol;
3207 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3208 &efi_guid_driver_binding_protocol,
3209 (void **)&binding_protocol,
3210 driver_image_handle, NULL,
3211 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3212 if (r != EFI_SUCCESS)
3214 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3216 remain_device_path));
3217 if (r == EFI_SUCCESS)
3218 r = EFI_CALL(binding_protocol->start(binding_protocol,
3220 remain_device_path));
3221 EFI_CALL(efi_close_protocol(driver_image_handle,
3222 &efi_guid_driver_binding_protocol,
3223 driver_image_handle, NULL));
3228 * efi_connect_single_controller() - connect a single driver to a controller
3229 * @controller_handle: controller
3230 * @driver_image_handle: driver
3231 * @remain_device_path: remaining path
3233 * Return: status code
3235 static efi_status_t efi_connect_single_controller(
3236 efi_handle_t controller_handle,
3237 efi_handle_t *driver_image_handle,
3238 struct efi_device_path *remain_device_path)
3240 efi_handle_t *buffer;
3244 size_t connected = 0;
3246 /* Get buffer with all handles with driver binding protocol */
3247 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3248 &efi_guid_driver_binding_protocol,
3249 NULL, &count, &buffer));
3250 if (r != EFI_SUCCESS)
3253 /* Context Override */
3254 if (driver_image_handle) {
3255 for (; *driver_image_handle; ++driver_image_handle) {
3256 for (i = 0; i < count; ++i) {
3257 if (buffer[i] == *driver_image_handle) {
3259 r = efi_bind_controller(
3261 *driver_image_handle,
3262 remain_device_path);
3264 * For drivers that do not support the
3265 * controller or are already connected
3266 * we receive an error code here.
3268 if (r == EFI_SUCCESS)
3276 * TODO: Some overrides are not yet implemented:
3277 * - Platform Driver Override
3278 * - Driver Family Override Search
3279 * - Bus Specific Driver Override
3282 /* Driver Binding Search */
3283 for (i = 0; i < count; ++i) {
3285 r = efi_bind_controller(controller_handle,
3287 remain_device_path);
3288 if (r == EFI_SUCCESS)
3293 efi_free_pool(buffer);
3295 return EFI_NOT_FOUND;
3300 * efi_connect_controller() - connect a controller to a driver
3301 * @controller_handle: handle of the controller
3302 * @driver_image_handle: handle of the driver
3303 * @remain_device_path: device path of a child controller
3304 * @recursive: true to connect all child controllers
3306 * This function implements the ConnectController service.
3308 * See the Unified Extensible Firmware Interface (UEFI) specification for
3311 * First all driver binding protocol handles are tried for binding drivers.
3312 * Afterwards all handles that have opened a protocol of the controller
3313 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3315 * Return: status code
3317 static efi_status_t EFIAPI efi_connect_controller(
3318 efi_handle_t controller_handle,
3319 efi_handle_t *driver_image_handle,
3320 struct efi_device_path *remain_device_path,
3324 efi_status_t ret = EFI_NOT_FOUND;
3325 struct efi_object *efiobj;
3327 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
3328 remain_device_path, recursive);
3330 efiobj = efi_search_obj(controller_handle);
3332 ret = EFI_INVALID_PARAMETER;
3336 r = efi_connect_single_controller(controller_handle,
3337 driver_image_handle,
3338 remain_device_path);
3339 if (r == EFI_SUCCESS)
3342 struct efi_handler *handler;
3343 struct efi_open_protocol_info_item *item;
3345 list_for_each_entry(handler, &efiobj->protocols, link) {
3346 list_for_each_entry(item, &handler->open_infos, link) {
3347 if (item->info.attributes &
3348 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3349 r = EFI_CALL(efi_connect_controller(
3350 item->info.controller_handle,
3351 driver_image_handle,
3354 if (r == EFI_SUCCESS)
3360 /* Check for child controller specified by end node */
3361 if (ret != EFI_SUCCESS && remain_device_path &&
3362 remain_device_path->type == DEVICE_PATH_TYPE_END)
3365 return EFI_EXIT(ret);
3369 * efi_reinstall_protocol_interface() - reinstall protocol interface
3370 * @handle: handle on which the protocol shall be reinstalled
3371 * @protocol: GUID of the protocol to be installed
3372 * @old_interface: interface to be removed
3373 * @new_interface: interface to be installed
3375 * This function implements the ReinstallProtocolInterface service.
3377 * See the Unified Extensible Firmware Interface (UEFI) specification for
3380 * The old interface is uninstalled. The new interface is installed.
3381 * Drivers are connected.
3383 * Return: status code
3385 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3386 efi_handle_t handle, const efi_guid_t *protocol,
3387 void *old_interface, void *new_interface)
3391 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3394 /* Uninstall protocol but do not delete handle */
3395 ret = efi_uninstall_protocol(handle, protocol, old_interface);
3396 if (ret != EFI_SUCCESS)
3399 /* Install the new protocol */
3400 ret = efi_add_protocol(handle, protocol, new_interface);
3402 * The UEFI spec does not specify what should happen to the handle
3403 * if in case of an error no protocol interface remains on the handle.
3404 * So let's do nothing here.
3406 if (ret != EFI_SUCCESS)
3409 * The returned status code has to be ignored.
3410 * Do not create an error if no suitable driver for the handle exists.
3412 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3414 return EFI_EXIT(ret);
3418 * efi_get_child_controllers() - get all child controllers associated to a driver
3419 * @efiobj: handle of the controller
3420 * @driver_handle: handle of the driver
3421 * @number_of_children: number of child controllers
3422 * @child_handle_buffer: handles of the the child controllers
3424 * The allocated buffer has to be freed with free().
3426 * Return: status code
3428 static efi_status_t efi_get_child_controllers(
3429 struct efi_object *efiobj,
3430 efi_handle_t driver_handle,
3431 efi_uintn_t *number_of_children,
3432 efi_handle_t **child_handle_buffer)
3434 struct efi_handler *handler;
3435 struct efi_open_protocol_info_item *item;
3436 efi_uintn_t count = 0, i;
3439 /* Count all child controller associations */
3440 list_for_each_entry(handler, &efiobj->protocols, link) {
3441 list_for_each_entry(item, &handler->open_infos, link) {
3442 if (item->info.agent_handle == driver_handle &&
3443 item->info.attributes &
3444 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3449 * Create buffer. In case of duplicate child controller assignments
3450 * the buffer will be too large. But that does not harm.
3452 *number_of_children = 0;
3453 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3454 if (!*child_handle_buffer)
3455 return EFI_OUT_OF_RESOURCES;
3456 /* Copy unique child handles */
3457 list_for_each_entry(handler, &efiobj->protocols, link) {
3458 list_for_each_entry(item, &handler->open_infos, link) {
3459 if (item->info.agent_handle == driver_handle &&
3460 item->info.attributes &
3461 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3462 /* Check this is a new child controller */
3464 for (i = 0; i < *number_of_children; ++i) {
3465 if ((*child_handle_buffer)[i] ==
3466 item->info.controller_handle)
3469 /* Copy handle to buffer */
3471 i = (*number_of_children)++;
3472 (*child_handle_buffer)[i] =
3473 item->info.controller_handle;
3482 * efi_disconnect_controller() - disconnect a controller from a driver
3483 * @controller_handle: handle of the controller
3484 * @driver_image_handle: handle of the driver
3485 * @child_handle: handle of the child to destroy
3487 * This function implements the DisconnectController service.
3489 * See the Unified Extensible Firmware Interface (UEFI) specification for
3492 * Return: status code
3494 static efi_status_t EFIAPI efi_disconnect_controller(
3495 efi_handle_t controller_handle,
3496 efi_handle_t driver_image_handle,
3497 efi_handle_t child_handle)
3499 struct efi_driver_binding_protocol *binding_protocol;
3500 efi_handle_t *child_handle_buffer = NULL;
3501 size_t number_of_children = 0;
3503 size_t stop_count = 0;
3504 struct efi_object *efiobj;
3506 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3509 efiobj = efi_search_obj(controller_handle);
3511 r = EFI_INVALID_PARAMETER;
3515 if (child_handle && !efi_search_obj(child_handle)) {
3516 r = EFI_INVALID_PARAMETER;
3520 /* If no driver handle is supplied, disconnect all drivers */
3521 if (!driver_image_handle) {
3522 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3526 /* Create list of child handles */
3528 number_of_children = 1;
3529 child_handle_buffer = &child_handle;
3531 efi_get_child_controllers(efiobj,
3532 driver_image_handle,
3533 &number_of_children,
3534 &child_handle_buffer);
3537 /* Get the driver binding protocol */
3538 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3539 &efi_guid_driver_binding_protocol,
3540 (void **)&binding_protocol,
3541 driver_image_handle, NULL,
3542 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3543 if (r != EFI_SUCCESS)
3545 /* Remove the children */
3546 if (number_of_children) {
3547 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3550 child_handle_buffer));
3551 if (r == EFI_SUCCESS)
3554 /* Remove the driver */
3556 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3559 if (r == EFI_SUCCESS)
3561 EFI_CALL(efi_close_protocol(driver_image_handle,
3562 &efi_guid_driver_binding_protocol,
3563 driver_image_handle, NULL));
3571 free(child_handle_buffer);
3575 static struct efi_boot_services efi_boot_services = {
3577 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3578 .revision = EFI_SPECIFICATION_VERSION,
3579 .headersize = sizeof(struct efi_boot_services),
3581 .raise_tpl = efi_raise_tpl,
3582 .restore_tpl = efi_restore_tpl,
3583 .allocate_pages = efi_allocate_pages_ext,
3584 .free_pages = efi_free_pages_ext,
3585 .get_memory_map = efi_get_memory_map_ext,
3586 .allocate_pool = efi_allocate_pool_ext,
3587 .free_pool = efi_free_pool_ext,
3588 .create_event = efi_create_event_ext,
3589 .set_timer = efi_set_timer_ext,
3590 .wait_for_event = efi_wait_for_event,
3591 .signal_event = efi_signal_event_ext,
3592 .close_event = efi_close_event,
3593 .check_event = efi_check_event,
3594 .install_protocol_interface = efi_install_protocol_interface,
3595 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3596 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3597 .handle_protocol = efi_handle_protocol,
3599 .register_protocol_notify = efi_register_protocol_notify,
3600 .locate_handle = efi_locate_handle_ext,
3601 .locate_device_path = efi_locate_device_path,
3602 .install_configuration_table = efi_install_configuration_table_ext,
3603 .load_image = efi_load_image,
3604 .start_image = efi_start_image,
3606 .unload_image = efi_unload_image,
3607 .exit_boot_services = efi_exit_boot_services,
3608 .get_next_monotonic_count = efi_get_next_monotonic_count,
3610 .set_watchdog_timer = efi_set_watchdog_timer,
3611 .connect_controller = efi_connect_controller,
3612 .disconnect_controller = efi_disconnect_controller,
3613 .open_protocol = efi_open_protocol,
3614 .close_protocol = efi_close_protocol,
3615 .open_protocol_information = efi_open_protocol_information,
3616 .protocols_per_handle = efi_protocols_per_handle,
3617 .locate_handle_buffer = efi_locate_handle_buffer,
3618 .locate_protocol = efi_locate_protocol,
3619 .install_multiple_protocol_interfaces =
3620 efi_install_multiple_protocol_interfaces,
3621 .uninstall_multiple_protocol_interfaces =
3622 efi_uninstall_multiple_protocol_interfaces,
3623 .calculate_crc32 = efi_calculate_crc32,
3624 .copy_mem = efi_copy_mem,
3625 .set_mem = efi_set_mem,
3626 .create_event_ex = efi_create_event_ex,
3629 static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3631 struct efi_system_table __efi_runtime_data systab = {
3633 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3634 .revision = EFI_SPECIFICATION_VERSION,
3635 .headersize = sizeof(struct efi_system_table),
3637 .fw_vendor = firmware_vendor,
3638 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3639 .runtime = &efi_runtime_services,
3645 * efi_initialize_system_table() - Initialize system table
3647 * Return: status code
3649 efi_status_t efi_initialize_system_table(void)
3653 /* Allocate configuration table array */
3654 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3655 EFI_MAX_CONFIGURATION_TABLES *
3656 sizeof(struct efi_configuration_table),
3657 (void **)&systab.tables);
3660 * These entries will be set to NULL in ExitBootServices(). To avoid
3661 * relocation in SetVirtualAddressMap(), set them dynamically.
3663 systab.con_in = &efi_con_in;
3664 systab.con_out = &efi_con_out;
3665 systab.std_err = &efi_con_out;
3666 systab.boottime = &efi_boot_services;
3668 /* Set CRC32 field in table headers */
3669 efi_update_table_header_crc32(&systab.hdr);
3670 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3671 efi_update_table_header_crc32(&efi_boot_services.hdr);