1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application boot time services
5 * Copyright (c) 2016 Alexander Graf
10 #include <efi_loader.h>
14 #include <linux/libfdt_env.h>
15 #include <u-boot/crc.h>
18 #include <u-boot/crc.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 /* Task priority level */
24 static efi_uintn_t efi_tpl = TPL_APPLICATION;
26 /* This list contains all the EFI objects our payload has access to */
27 LIST_HEAD(efi_obj_list);
29 /* List of all events */
30 __efi_runtime_data LIST_HEAD(efi_events);
32 /* List of queued events */
33 LIST_HEAD(efi_event_queue);
35 /* Flag to disable timer activity in ExitBootServices() */
36 static bool timers_enabled = true;
38 /* List of all events registered by RegisterProtocolNotify() */
39 LIST_HEAD(efi_register_notify_events);
41 /* Handle of the currently executing image */
42 static efi_handle_t current_image;
46 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
47 * fixed when compiling U-Boot. However, the payload does not know about that
48 * restriction so we need to manually swap its and our view of that register on
49 * EFI callback entry/exit.
51 static volatile void *efi_gd, *app_gd;
54 /* 1 if inside U-Boot code, 0 if inside EFI payload code */
55 static int entry_count = 1;
56 static int nesting_level;
57 /* GUID of the device tree table */
58 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
59 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
60 const efi_guid_t efi_guid_driver_binding_protocol =
61 EFI_DRIVER_BINDING_PROTOCOL_GUID;
63 /* event group ExitBootServices() invoked */
64 const efi_guid_t efi_guid_event_group_exit_boot_services =
65 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
66 /* event group SetVirtualAddressMap() invoked */
67 const efi_guid_t efi_guid_event_group_virtual_address_change =
68 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
69 /* event group memory map changed */
70 const efi_guid_t efi_guid_event_group_memory_map_change =
71 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
72 /* event group boot manager about to boot */
73 const efi_guid_t efi_guid_event_group_ready_to_boot =
74 EFI_EVENT_GROUP_READY_TO_BOOT;
75 /* event group ResetSystem() invoked (before ExitBootServices) */
76 const efi_guid_t efi_guid_event_group_reset_system =
77 EFI_EVENT_GROUP_RESET_SYSTEM;
79 static efi_status_t EFIAPI efi_disconnect_controller(
80 efi_handle_t controller_handle,
81 efi_handle_t driver_image_handle,
82 efi_handle_t child_handle);
84 /* Called on every callback entry */
85 int __efi_entry_check(void)
87 int ret = entry_count++ == 0;
96 /* Called on every callback exit */
97 int __efi_exit_check(void)
99 int ret = --entry_count == 0;
106 /* Called from do_bootefi_exec() */
107 void efi_save_gd(void)
115 * Special case handler for error/abort that just forces things back to u-boot
116 * world so we can dump out an abort message, without any care about returning
117 * back to UEFI world.
119 void efi_restore_gd(void)
122 /* Only restore if we're already in EFI context */
130 * indent_string() - returns a string for indenting with two spaces per level
131 * @level: indent level
133 * A maximum of ten indent levels is supported. Higher indent levels will be
136 * Return: A string for indenting with two spaces per level is
139 static const char *indent_string(int level)
141 const char *indent = " ";
142 const int max = strlen(indent);
144 level = min(max, level * 2);
145 return &indent[max - level];
148 const char *__efi_nesting(void)
150 return indent_string(nesting_level);
153 const char *__efi_nesting_inc(void)
155 return indent_string(nesting_level++);
158 const char *__efi_nesting_dec(void)
160 return indent_string(--nesting_level);
164 * efi_event_is_queued() - check if an event is queued
167 * Return: true if event is queued
169 static bool efi_event_is_queued(struct efi_event *event)
171 return !!event->queue_link.next;
175 * efi_process_event_queue() - process event queue
177 static void efi_process_event_queue(void)
179 while (!list_empty(&efi_event_queue)) {
180 struct efi_event *event;
183 event = list_first_entry(&efi_event_queue, struct efi_event,
185 if (efi_tpl >= event->notify_tpl)
187 list_del(&event->queue_link);
188 event->queue_link.next = NULL;
189 event->queue_link.prev = NULL;
190 /* Events must be executed at the event's TPL */
192 efi_tpl = event->notify_tpl;
193 EFI_CALL_VOID(event->notify_function(event,
194 event->notify_context));
196 if (event->type == EVT_NOTIFY_SIGNAL)
197 event->is_signaled = 0;
202 * efi_queue_event() - queue an EFI event
203 * @event: event to signal
205 * This function queues the notification function of the event for future
209 static void efi_queue_event(struct efi_event *event)
211 struct efi_event *item;
213 if (!event->notify_function)
216 if (!efi_event_is_queued(event)) {
218 * Events must be notified in order of decreasing task priority
219 * level. Insert the new event accordingly.
221 list_for_each_entry(item, &efi_event_queue, queue_link) {
222 if (item->notify_tpl < event->notify_tpl) {
223 list_add_tail(&event->queue_link,
230 list_add_tail(&event->queue_link, &efi_event_queue);
232 efi_process_event_queue();
236 * is_valid_tpl() - check if the task priority level is valid
238 * @tpl: TPL level to check
239 * Return: status code
241 efi_status_t is_valid_tpl(efi_uintn_t tpl)
244 case TPL_APPLICATION:
250 return EFI_INVALID_PARAMETER;
255 * efi_signal_event() - signal an EFI event
256 * @event: event to signal
258 * This function signals an event. If the event belongs to an event group all
259 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
260 * their notification function is queued.
262 * For the SignalEvent service see efi_signal_event_ext.
264 void efi_signal_event(struct efi_event *event)
266 if (event->is_signaled)
269 struct efi_event *evt;
272 * The signaled state has to set before executing any
273 * notification function
275 list_for_each_entry(evt, &efi_events, link) {
276 if (!evt->group || guidcmp(evt->group, event->group))
278 if (evt->is_signaled)
280 evt->is_signaled = true;
282 list_for_each_entry(evt, &efi_events, link) {
283 if (!evt->group || guidcmp(evt->group, event->group))
285 efi_queue_event(evt);
288 event->is_signaled = true;
289 efi_queue_event(event);
294 * efi_raise_tpl() - raise the task priority level
295 * @new_tpl: new value of the task priority level
297 * This function implements the RaiseTpl service.
299 * See the Unified Extensible Firmware Interface (UEFI) specification for
302 * Return: old value of the task priority level
304 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
306 efi_uintn_t old_tpl = efi_tpl;
308 EFI_ENTRY("0x%zx", new_tpl);
310 if (new_tpl < efi_tpl)
311 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
313 if (efi_tpl > TPL_HIGH_LEVEL)
314 efi_tpl = TPL_HIGH_LEVEL;
316 EFI_EXIT(EFI_SUCCESS);
321 * efi_restore_tpl() - lower the task priority level
322 * @old_tpl: value of the task priority level to be restored
324 * This function implements the RestoreTpl service.
326 * See the Unified Extensible Firmware Interface (UEFI) specification for
329 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
331 EFI_ENTRY("0x%zx", old_tpl);
333 if (old_tpl > efi_tpl)
334 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
336 if (efi_tpl > TPL_HIGH_LEVEL)
337 efi_tpl = TPL_HIGH_LEVEL;
340 * Lowering the TPL may have made queued events eligible for execution.
344 EFI_EXIT(EFI_SUCCESS);
348 * efi_allocate_pages_ext() - allocate memory pages
349 * @type: type of allocation to be performed
350 * @memory_type: usage type of the allocated memory
351 * @pages: number of pages to be allocated
352 * @memory: allocated memory
354 * This function implements the AllocatePages service.
356 * See the Unified Extensible Firmware Interface (UEFI) specification for
359 * Return: status code
361 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
367 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
368 r = efi_allocate_pages(type, memory_type, pages, memory);
373 * efi_free_pages_ext() - Free memory pages.
374 * @memory: start of the memory area to be freed
375 * @pages: number of pages to be freed
377 * This function implements the FreePages service.
379 * See the Unified Extensible Firmware Interface (UEFI) specification for
382 * Return: status code
384 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
389 EFI_ENTRY("%llx, 0x%zx", memory, pages);
390 r = efi_free_pages(memory, pages);
395 * efi_get_memory_map_ext() - get map describing memory usage
396 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
397 * on exit the size of the copied memory map
398 * @memory_map: buffer to which the memory map is written
399 * @map_key: key for the memory map
400 * @descriptor_size: size of an individual memory descriptor
401 * @descriptor_version: version number of the memory descriptor structure
403 * This function implements the GetMemoryMap service.
405 * See the Unified Extensible Firmware Interface (UEFI) specification for
408 * Return: status code
410 static efi_status_t EFIAPI efi_get_memory_map_ext(
411 efi_uintn_t *memory_map_size,
412 struct efi_mem_desc *memory_map,
413 efi_uintn_t *map_key,
414 efi_uintn_t *descriptor_size,
415 uint32_t *descriptor_version)
419 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
420 map_key, descriptor_size, descriptor_version);
421 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
422 descriptor_size, descriptor_version);
427 * efi_allocate_pool_ext() - allocate memory from pool
428 * @pool_type: type of the pool from which memory is to be allocated
429 * @size: number of bytes to be allocated
430 * @buffer: allocated memory
432 * This function implements the AllocatePool service.
434 * See the Unified Extensible Firmware Interface (UEFI) specification for
437 * Return: status code
439 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
445 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
446 r = efi_allocate_pool(pool_type, size, buffer);
451 * efi_free_pool_ext() - free memory from pool
452 * @buffer: start of memory to be freed
454 * This function implements the FreePool service.
456 * See the Unified Extensible Firmware Interface (UEFI) specification for
459 * Return: status code
461 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
465 EFI_ENTRY("%p", buffer);
466 r = efi_free_pool(buffer);
471 * efi_add_handle() - add a new handle to the object list
473 * @handle: handle to be added
475 * The protocols list is initialized. The handle is added to the list of known
478 void efi_add_handle(efi_handle_t handle)
482 INIT_LIST_HEAD(&handle->protocols);
483 list_add_tail(&handle->link, &efi_obj_list);
487 * efi_create_handle() - create handle
488 * @handle: new handle
490 * Return: status code
492 efi_status_t efi_create_handle(efi_handle_t *handle)
494 struct efi_object *obj;
496 obj = calloc(1, sizeof(struct efi_object));
498 return EFI_OUT_OF_RESOURCES;
507 * efi_search_protocol() - find a protocol on a handle.
509 * @protocol_guid: GUID of the protocol
510 * @handler: reference to the protocol
512 * Return: status code
514 efi_status_t efi_search_protocol(const efi_handle_t handle,
515 const efi_guid_t *protocol_guid,
516 struct efi_handler **handler)
518 struct efi_object *efiobj;
519 struct list_head *lhandle;
521 if (!handle || !protocol_guid)
522 return EFI_INVALID_PARAMETER;
523 efiobj = efi_search_obj(handle);
525 return EFI_INVALID_PARAMETER;
526 list_for_each(lhandle, &efiobj->protocols) {
527 struct efi_handler *protocol;
529 protocol = list_entry(lhandle, struct efi_handler, link);
530 if (!guidcmp(protocol->guid, protocol_guid)) {
536 return EFI_NOT_FOUND;
540 * efi_remove_protocol() - delete protocol from a handle
541 * @handle: handle from which the protocol shall be deleted
542 * @protocol: GUID of the protocol to be deleted
543 * @protocol_interface: interface of the protocol implementation
545 * Return: status code
547 efi_status_t efi_remove_protocol(const efi_handle_t handle,
548 const efi_guid_t *protocol,
549 void *protocol_interface)
551 struct efi_handler *handler;
554 ret = efi_search_protocol(handle, protocol, &handler);
555 if (ret != EFI_SUCCESS)
557 if (handler->protocol_interface != protocol_interface)
558 return EFI_NOT_FOUND;
559 list_del(&handler->link);
565 * efi_remove_all_protocols() - delete all protocols from a handle
566 * @handle: handle from which the protocols shall be deleted
568 * Return: status code
570 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
572 struct efi_object *efiobj;
573 struct efi_handler *protocol;
574 struct efi_handler *pos;
576 efiobj = efi_search_obj(handle);
578 return EFI_INVALID_PARAMETER;
579 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
582 ret = efi_remove_protocol(handle, protocol->guid,
583 protocol->protocol_interface);
584 if (ret != EFI_SUCCESS)
591 * efi_delete_handle() - delete handle
593 * @handle: handle to delete
595 void efi_delete_handle(efi_handle_t handle)
599 efi_remove_all_protocols(handle);
600 list_del(&handle->link);
605 * efi_is_event() - check if a pointer is a valid event
606 * @event: pointer to check
608 * Return: status code
610 static efi_status_t efi_is_event(const struct efi_event *event)
612 const struct efi_event *evt;
615 return EFI_INVALID_PARAMETER;
616 list_for_each_entry(evt, &efi_events, link) {
620 return EFI_INVALID_PARAMETER;
624 * efi_create_event() - create an event
626 * @type: type of the event to create
627 * @notify_tpl: task priority level of the event
628 * @notify_function: notification function of the event
629 * @notify_context: pointer passed to the notification function
630 * @group: event group
631 * @event: created event
633 * This function is used inside U-Boot code to create an event.
635 * For the API function implementing the CreateEvent service see
636 * efi_create_event_ext.
638 * Return: status code
640 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
641 void (EFIAPI *notify_function) (
642 struct efi_event *event,
644 void *notify_context, efi_guid_t *group,
645 struct efi_event **event)
647 struct efi_event *evt;
652 return EFI_INVALID_PARAMETER;
657 case EVT_NOTIFY_SIGNAL:
658 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
659 case EVT_NOTIFY_WAIT:
660 case EVT_TIMER | EVT_NOTIFY_WAIT:
661 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
662 pool_type = EFI_BOOT_SERVICES_DATA;
664 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
665 pool_type = EFI_RUNTIME_SERVICES_DATA;
668 return EFI_INVALID_PARAMETER;
671 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
672 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
673 return EFI_INVALID_PARAMETER;
675 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
677 if (ret != EFI_SUCCESS)
679 memset(evt, 0, sizeof(struct efi_event));
681 evt->notify_tpl = notify_tpl;
682 evt->notify_function = notify_function;
683 evt->notify_context = notify_context;
685 /* Disable timers on boot up */
686 evt->trigger_next = -1ULL;
687 list_add_tail(&evt->link, &efi_events);
693 * efi_create_event_ex() - create an event in a group
694 * @type: type of the event to create
695 * @notify_tpl: task priority level of the event
696 * @notify_function: notification function of the event
697 * @notify_context: pointer passed to the notification function
698 * @event: created event
699 * @event_group: event group
701 * This function implements the CreateEventEx service.
703 * See the Unified Extensible Firmware Interface (UEFI) specification for
706 * Return: status code
708 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
709 void (EFIAPI *notify_function) (
710 struct efi_event *event,
712 void *notify_context,
713 efi_guid_t *event_group,
714 struct efi_event **event)
718 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
719 notify_context, event_group);
722 * The allowable input parameters are the same as in CreateEvent()
723 * except for the following two disallowed event types.
726 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
727 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
728 ret = EFI_INVALID_PARAMETER;
732 ret = efi_create_event(type, notify_tpl, notify_function,
733 notify_context, event_group, event);
735 return EFI_EXIT(ret);
739 * efi_create_event_ext() - create an event
740 * @type: type of the event to create
741 * @notify_tpl: task priority level of the event
742 * @notify_function: notification function of the event
743 * @notify_context: pointer passed to the notification function
744 * @event: created event
746 * This function implements the CreateEvent service.
748 * See the Unified Extensible Firmware Interface (UEFI) specification for
751 * Return: status code
753 static efi_status_t EFIAPI efi_create_event_ext(
754 uint32_t type, efi_uintn_t notify_tpl,
755 void (EFIAPI *notify_function) (
756 struct efi_event *event,
758 void *notify_context, struct efi_event **event)
760 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
762 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
763 notify_context, NULL, event));
767 * efi_timer_check() - check if a timer event has occurred
769 * Check if a timer event has occurred or a queued notification function should
772 * Our timers have to work without interrupts, so we check whenever keyboard
773 * input or disk accesses happen if enough time elapsed for them to fire.
775 void efi_timer_check(void)
777 struct efi_event *evt;
778 u64 now = timer_get_us();
780 list_for_each_entry(evt, &efi_events, link) {
783 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
785 switch (evt->trigger_type) {
786 case EFI_TIMER_RELATIVE:
787 evt->trigger_type = EFI_TIMER_STOP;
789 case EFI_TIMER_PERIODIC:
790 evt->trigger_next += evt->trigger_time;
795 evt->is_signaled = false;
796 efi_signal_event(evt);
798 efi_process_event_queue();
803 * efi_set_timer() - set the trigger time for a timer event or stop the event
804 * @event: event for which the timer is set
805 * @type: type of the timer
806 * @trigger_time: trigger period in multiples of 100 ns
808 * This is the function for internal usage in U-Boot. For the API function
809 * implementing the SetTimer service see efi_set_timer_ext.
811 * Return: status code
813 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
814 uint64_t trigger_time)
816 /* Check that the event is valid */
817 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
818 return EFI_INVALID_PARAMETER;
821 * The parameter defines a multiple of 100 ns.
822 * We use multiples of 1000 ns. So divide by 10.
824 do_div(trigger_time, 10);
828 event->trigger_next = -1ULL;
830 case EFI_TIMER_PERIODIC:
831 case EFI_TIMER_RELATIVE:
832 event->trigger_next = timer_get_us() + trigger_time;
835 return EFI_INVALID_PARAMETER;
837 event->trigger_type = type;
838 event->trigger_time = trigger_time;
839 event->is_signaled = false;
844 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
846 * @event: event for which the timer is set
847 * @type: type of the timer
848 * @trigger_time: trigger period in multiples of 100 ns
850 * This function implements the SetTimer service.
852 * See the Unified Extensible Firmware Interface (UEFI) specification for
856 * Return: status code
858 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
859 enum efi_timer_delay type,
860 uint64_t trigger_time)
862 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
863 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
867 * efi_wait_for_event() - wait for events to be signaled
868 * @num_events: number of events to be waited for
869 * @event: events to be waited for
870 * @index: index of the event that was signaled
872 * This function implements the WaitForEvent service.
874 * See the Unified Extensible Firmware Interface (UEFI) specification for
877 * Return: status code
879 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
880 struct efi_event **event,
885 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
887 /* Check parameters */
888 if (!num_events || !event)
889 return EFI_EXIT(EFI_INVALID_PARAMETER);
891 if (efi_tpl != TPL_APPLICATION)
892 return EFI_EXIT(EFI_UNSUPPORTED);
893 for (i = 0; i < num_events; ++i) {
894 if (efi_is_event(event[i]) != EFI_SUCCESS)
895 return EFI_EXIT(EFI_INVALID_PARAMETER);
896 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
897 return EFI_EXIT(EFI_INVALID_PARAMETER);
898 if (!event[i]->is_signaled)
899 efi_queue_event(event[i]);
902 /* Wait for signal */
904 for (i = 0; i < num_events; ++i) {
905 if (event[i]->is_signaled)
908 /* Allow events to occur. */
914 * Reset the signal which is passed to the caller to allow periodic
917 event[i]->is_signaled = false;
921 return EFI_EXIT(EFI_SUCCESS);
925 * efi_signal_event_ext() - signal an EFI event
926 * @event: event to signal
928 * This function implements the SignalEvent service.
930 * See the Unified Extensible Firmware Interface (UEFI) specification for
933 * This functions sets the signaled state of the event and queues the
934 * notification function for execution.
936 * Return: status code
938 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
940 EFI_ENTRY("%p", event);
941 if (efi_is_event(event) != EFI_SUCCESS)
942 return EFI_EXIT(EFI_INVALID_PARAMETER);
943 efi_signal_event(event);
944 return EFI_EXIT(EFI_SUCCESS);
948 * efi_close_event() - close an EFI event
949 * @event: event to close
951 * This function implements the CloseEvent service.
953 * See the Unified Extensible Firmware Interface (UEFI) specification for
956 * Return: status code
958 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
960 struct efi_register_notify_event *item, *next;
962 EFI_ENTRY("%p", event);
963 if (efi_is_event(event) != EFI_SUCCESS)
964 return EFI_EXIT(EFI_INVALID_PARAMETER);
966 /* Remove protocol notify registrations for the event */
967 list_for_each_entry_safe(item, next, &efi_register_notify_events,
969 if (event == item->event) {
970 struct efi_protocol_notification *hitem, *hnext;
972 /* Remove signaled handles */
973 list_for_each_entry_safe(hitem, hnext, &item->handles,
975 list_del(&hitem->link);
978 list_del(&item->link);
982 /* Remove event from queue */
983 if (efi_event_is_queued(event))
984 list_del(&event->queue_link);
986 list_del(&event->link);
987 efi_free_pool(event);
988 return EFI_EXIT(EFI_SUCCESS);
992 * efi_check_event() - check if an event is signaled
993 * @event: event to check
995 * This function implements the CheckEvent service.
997 * See the Unified Extensible Firmware Interface (UEFI) specification for
1000 * If an event is not signaled yet, the notification function is queued. The
1001 * signaled state is cleared.
1003 * Return: status code
1005 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
1007 EFI_ENTRY("%p", event);
1009 if (efi_is_event(event) != EFI_SUCCESS ||
1010 event->type & EVT_NOTIFY_SIGNAL)
1011 return EFI_EXIT(EFI_INVALID_PARAMETER);
1012 if (!event->is_signaled)
1013 efi_queue_event(event);
1014 if (event->is_signaled) {
1015 event->is_signaled = false;
1016 return EFI_EXIT(EFI_SUCCESS);
1018 return EFI_EXIT(EFI_NOT_READY);
1022 * efi_search_obj() - find the internal EFI object for a handle
1023 * @handle: handle to find
1025 * Return: EFI object
1027 struct efi_object *efi_search_obj(const efi_handle_t handle)
1029 struct efi_object *efiobj;
1034 list_for_each_entry(efiobj, &efi_obj_list, link) {
1035 if (efiobj == handle)
1042 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1044 * @handler: handler of a protocol
1046 * Return: open protocol info entry
1048 static struct efi_open_protocol_info_entry *efi_create_open_info(
1049 struct efi_handler *handler)
1051 struct efi_open_protocol_info_item *item;
1053 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1056 /* Append the item to the open protocol info list. */
1057 list_add_tail(&item->link, &handler->open_infos);
1063 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1064 * @item: open protocol info entry to delete
1066 * Return: status code
1068 static efi_status_t efi_delete_open_info(
1069 struct efi_open_protocol_info_item *item)
1071 list_del(&item->link);
1077 * efi_add_protocol() - install new protocol on a handle
1078 * @handle: handle on which the protocol shall be installed
1079 * @protocol: GUID of the protocol to be installed
1080 * @protocol_interface: interface of the protocol implementation
1082 * Return: status code
1084 efi_status_t efi_add_protocol(const efi_handle_t handle,
1085 const efi_guid_t *protocol,
1086 void *protocol_interface)
1088 struct efi_object *efiobj;
1089 struct efi_handler *handler;
1091 struct efi_register_notify_event *event;
1093 efiobj = efi_search_obj(handle);
1095 return EFI_INVALID_PARAMETER;
1096 ret = efi_search_protocol(handle, protocol, NULL);
1097 if (ret != EFI_NOT_FOUND)
1098 return EFI_INVALID_PARAMETER;
1099 handler = calloc(1, sizeof(struct efi_handler));
1101 return EFI_OUT_OF_RESOURCES;
1102 handler->guid = protocol;
1103 handler->protocol_interface = protocol_interface;
1104 INIT_LIST_HEAD(&handler->open_infos);
1105 list_add_tail(&handler->link, &efiobj->protocols);
1107 /* Notify registered events */
1108 list_for_each_entry(event, &efi_register_notify_events, link) {
1109 if (!guidcmp(protocol, &event->protocol)) {
1110 struct efi_protocol_notification *notif;
1112 notif = calloc(1, sizeof(*notif));
1114 list_del(&handler->link);
1116 return EFI_OUT_OF_RESOURCES;
1118 notif->handle = handle;
1119 list_add_tail(¬if->link, &event->handles);
1120 event->event->is_signaled = false;
1121 efi_signal_event(event->event);
1125 if (!guidcmp(&efi_guid_device_path, protocol))
1126 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1131 * efi_install_protocol_interface() - install protocol interface
1132 * @handle: handle on which the protocol shall be installed
1133 * @protocol: GUID of the protocol to be installed
1134 * @protocol_interface_type: type of the interface to be installed,
1135 * always EFI_NATIVE_INTERFACE
1136 * @protocol_interface: interface of the protocol implementation
1138 * This function implements the InstallProtocolInterface service.
1140 * See the Unified Extensible Firmware Interface (UEFI) specification for
1143 * Return: status code
1145 static efi_status_t EFIAPI efi_install_protocol_interface(
1146 efi_handle_t *handle, const efi_guid_t *protocol,
1147 int protocol_interface_type, void *protocol_interface)
1151 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1152 protocol_interface);
1154 if (!handle || !protocol ||
1155 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1156 r = EFI_INVALID_PARAMETER;
1160 /* Create new handle if requested. */
1162 r = efi_create_handle(handle);
1163 if (r != EFI_SUCCESS)
1165 EFI_PRINT("new handle %p\n", *handle);
1167 EFI_PRINT("handle %p\n", *handle);
1169 /* Add new protocol */
1170 r = efi_add_protocol(*handle, protocol, protocol_interface);
1176 * efi_get_drivers() - get all drivers associated to a controller
1177 * @handle: handle of the controller
1178 * @protocol: protocol GUID (optional)
1179 * @number_of_drivers: number of child controllers
1180 * @driver_handle_buffer: handles of the the drivers
1182 * The allocated buffer has to be freed with free().
1184 * Return: status code
1186 static efi_status_t efi_get_drivers(efi_handle_t handle,
1187 const efi_guid_t *protocol,
1188 efi_uintn_t *number_of_drivers,
1189 efi_handle_t **driver_handle_buffer)
1191 struct efi_handler *handler;
1192 struct efi_open_protocol_info_item *item;
1193 efi_uintn_t count = 0, i;
1196 /* Count all driver associations */
1197 list_for_each_entry(handler, &handle->protocols, link) {
1198 if (protocol && guidcmp(handler->guid, protocol))
1200 list_for_each_entry(item, &handler->open_infos, link) {
1201 if (item->info.attributes &
1202 EFI_OPEN_PROTOCOL_BY_DRIVER)
1206 *number_of_drivers = 0;
1208 *driver_handle_buffer = NULL;
1212 * Create buffer. In case of duplicate driver assignments the buffer
1213 * will be too large. But that does not harm.
1215 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1216 if (!*driver_handle_buffer)
1217 return EFI_OUT_OF_RESOURCES;
1218 /* Collect unique driver handles */
1219 list_for_each_entry(handler, &handle->protocols, link) {
1220 if (protocol && guidcmp(handler->guid, protocol))
1222 list_for_each_entry(item, &handler->open_infos, link) {
1223 if (item->info.attributes &
1224 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1225 /* Check this is a new driver */
1227 for (i = 0; i < *number_of_drivers; ++i) {
1228 if ((*driver_handle_buffer)[i] ==
1229 item->info.agent_handle)
1232 /* Copy handle to buffer */
1234 i = (*number_of_drivers)++;
1235 (*driver_handle_buffer)[i] =
1236 item->info.agent_handle;
1245 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1246 * @handle: handle of the controller
1247 * @protocol: protocol GUID (optional)
1248 * @child_handle: handle of the child to destroy
1250 * This function implements the DisconnectController service.
1252 * See the Unified Extensible Firmware Interface (UEFI) specification for
1255 * Return: status code
1257 static efi_status_t efi_disconnect_all_drivers
1258 (efi_handle_t handle,
1259 const efi_guid_t *protocol,
1260 efi_handle_t child_handle)
1262 efi_uintn_t number_of_drivers;
1263 efi_handle_t *driver_handle_buffer;
1264 efi_status_t r, ret;
1266 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
1267 &driver_handle_buffer);
1268 if (ret != EFI_SUCCESS)
1270 if (!number_of_drivers)
1272 ret = EFI_NOT_FOUND;
1273 while (number_of_drivers) {
1274 r = EFI_CALL(efi_disconnect_controller(
1276 driver_handle_buffer[--number_of_drivers],
1278 if (r == EFI_SUCCESS)
1281 free(driver_handle_buffer);
1286 * efi_uninstall_protocol() - uninstall protocol interface
1288 * @handle: handle from which the protocol shall be removed
1289 * @protocol: GUID of the protocol to be removed
1290 * @protocol_interface: interface to be removed
1292 * This function DOES NOT delete a handle without installed protocol.
1294 * Return: status code
1296 static efi_status_t efi_uninstall_protocol
1297 (efi_handle_t handle, const efi_guid_t *protocol,
1298 void *protocol_interface)
1300 struct efi_object *efiobj;
1301 struct efi_handler *handler;
1302 struct efi_open_protocol_info_item *item;
1303 struct efi_open_protocol_info_item *pos;
1307 efiobj = efi_search_obj(handle);
1309 r = EFI_INVALID_PARAMETER;
1312 /* Find the protocol on the handle */
1313 r = efi_search_protocol(handle, protocol, &handler);
1314 if (r != EFI_SUCCESS)
1316 /* Disconnect controllers */
1317 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1318 /* Close protocol */
1319 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1320 if (item->info.attributes ==
1321 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1322 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1323 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1324 list_del(&item->link);
1326 if (!list_empty(&handler->open_infos)) {
1327 r = EFI_ACCESS_DENIED;
1330 r = efi_remove_protocol(handle, protocol, protocol_interface);
1336 * efi_uninstall_protocol_interface() - uninstall protocol interface
1337 * @handle: handle from which the protocol shall be removed
1338 * @protocol: GUID of the protocol to be removed
1339 * @protocol_interface: interface to be removed
1341 * This function implements the UninstallProtocolInterface service.
1343 * See the Unified Extensible Firmware Interface (UEFI) specification for
1346 * Return: status code
1348 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1349 (efi_handle_t handle, const efi_guid_t *protocol,
1350 void *protocol_interface)
1354 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1356 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1357 if (ret != EFI_SUCCESS)
1360 /* If the last protocol has been removed, delete the handle. */
1361 if (list_empty(&handle->protocols)) {
1362 list_del(&handle->link);
1366 return EFI_EXIT(ret);
1370 * efi_register_protocol_notify() - register an event for notification when a
1371 * protocol is installed.
1372 * @protocol: GUID of the protocol whose installation shall be notified
1373 * @event: event to be signaled upon installation of the protocol
1374 * @registration: key for retrieving the registration information
1376 * This function implements the RegisterProtocolNotify service.
1377 * See the Unified Extensible Firmware Interface (UEFI) specification
1380 * Return: status code
1382 static efi_status_t EFIAPI efi_register_protocol_notify(
1383 const efi_guid_t *protocol,
1384 struct efi_event *event,
1385 void **registration)
1387 struct efi_register_notify_event *item;
1388 efi_status_t ret = EFI_SUCCESS;
1390 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1392 if (!protocol || !event || !registration) {
1393 ret = EFI_INVALID_PARAMETER;
1397 item = calloc(1, sizeof(struct efi_register_notify_event));
1399 ret = EFI_OUT_OF_RESOURCES;
1403 item->event = event;
1404 guidcpy(&item->protocol, protocol);
1405 INIT_LIST_HEAD(&item->handles);
1407 list_add_tail(&item->link, &efi_register_notify_events);
1409 *registration = item;
1411 return EFI_EXIT(ret);
1415 * efi_search() - determine if an EFI handle implements a protocol
1417 * @search_type: selection criterion
1418 * @protocol: GUID of the protocol
1421 * See the documentation of the LocateHandle service in the UEFI specification.
1423 * Return: 0 if the handle implements the protocol
1425 static int efi_search(enum efi_locate_search_type search_type,
1426 const efi_guid_t *protocol, efi_handle_t handle)
1430 switch (search_type) {
1434 ret = efi_search_protocol(handle, protocol, NULL);
1435 return (ret != EFI_SUCCESS);
1437 /* Invalid search type */
1443 * efi_check_register_notify_event() - check if registration key is valid
1445 * Check that a pointer is a valid registration key as returned by
1446 * RegisterProtocolNotify().
1448 * @key: registration key
1449 * Return: valid registration key or NULL
1451 static struct efi_register_notify_event *efi_check_register_notify_event
1454 struct efi_register_notify_event *event;
1456 list_for_each_entry(event, &efi_register_notify_events, link) {
1457 if (event == (struct efi_register_notify_event *)key)
1464 * efi_locate_handle() - locate handles implementing a protocol
1466 * @search_type: selection criterion
1467 * @protocol: GUID of the protocol
1468 * @search_key: registration key
1469 * @buffer_size: size of the buffer to receive the handles in bytes
1470 * @buffer: buffer to receive the relevant handles
1472 * This function is meant for U-Boot internal calls. For the API implementation
1473 * of the LocateHandle service see efi_locate_handle_ext.
1475 * Return: status code
1477 static efi_status_t efi_locate_handle(
1478 enum efi_locate_search_type search_type,
1479 const efi_guid_t *protocol, void *search_key,
1480 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1482 struct efi_object *efiobj;
1483 efi_uintn_t size = 0;
1484 struct efi_register_notify_event *event;
1485 struct efi_protocol_notification *handle = NULL;
1487 /* Check parameters */
1488 switch (search_type) {
1491 case BY_REGISTER_NOTIFY:
1493 return EFI_INVALID_PARAMETER;
1494 /* Check that the registration key is valid */
1495 event = efi_check_register_notify_event(search_key);
1497 return EFI_INVALID_PARAMETER;
1501 return EFI_INVALID_PARAMETER;
1504 return EFI_INVALID_PARAMETER;
1507 /* Count how much space we need */
1508 if (search_type == BY_REGISTER_NOTIFY) {
1509 if (list_empty(&event->handles))
1510 return EFI_NOT_FOUND;
1511 handle = list_first_entry(&event->handles,
1512 struct efi_protocol_notification,
1514 efiobj = handle->handle;
1515 size += sizeof(void *);
1517 list_for_each_entry(efiobj, &efi_obj_list, link) {
1518 if (!efi_search(search_type, protocol, efiobj))
1519 size += sizeof(void *);
1522 return EFI_NOT_FOUND;
1526 return EFI_INVALID_PARAMETER;
1528 if (*buffer_size < size) {
1529 *buffer_size = size;
1530 return EFI_BUFFER_TOO_SMALL;
1533 *buffer_size = size;
1535 /* The buffer size is sufficient but there is no buffer */
1537 return EFI_INVALID_PARAMETER;
1539 /* Then fill the array */
1540 if (search_type == BY_REGISTER_NOTIFY) {
1542 list_del(&handle->link);
1544 list_for_each_entry(efiobj, &efi_obj_list, link) {
1545 if (!efi_search(search_type, protocol, efiobj))
1554 * efi_locate_handle_ext() - locate handles implementing a protocol.
1555 * @search_type: selection criterion
1556 * @protocol: GUID of the protocol
1557 * @search_key: registration key
1558 * @buffer_size: size of the buffer to receive the handles in bytes
1559 * @buffer: buffer to receive the relevant handles
1561 * This function implements the LocateHandle service.
1563 * See the Unified Extensible Firmware Interface (UEFI) specification for
1566 * Return: 0 if the handle implements the protocol
1568 static efi_status_t EFIAPI efi_locate_handle_ext(
1569 enum efi_locate_search_type search_type,
1570 const efi_guid_t *protocol, void *search_key,
1571 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1573 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1574 buffer_size, buffer);
1576 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1577 buffer_size, buffer));
1581 * efi_remove_configuration_table() - collapses configuration table entries,
1584 * @i: index of the table entry to be removed
1586 static void efi_remove_configuration_table(int i)
1588 struct efi_configuration_table *this = &systab.tables[i];
1589 struct efi_configuration_table *next = &systab.tables[i + 1];
1590 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1592 memmove(this, next, (ulong)end - (ulong)next);
1597 * efi_install_configuration_table() - adds, updates, or removes a
1598 * configuration table
1599 * @guid: GUID of the installed table
1600 * @table: table to be installed
1602 * This function is used for internal calls. For the API implementation of the
1603 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1605 * Return: status code
1607 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1610 struct efi_event *evt;
1614 return EFI_INVALID_PARAMETER;
1616 /* Check for GUID override */
1617 for (i = 0; i < systab.nr_tables; i++) {
1618 if (!guidcmp(guid, &systab.tables[i].guid)) {
1620 systab.tables[i].table = table;
1622 efi_remove_configuration_table(i);
1628 return EFI_NOT_FOUND;
1630 /* No override, check for overflow */
1631 if (i >= EFI_MAX_CONFIGURATION_TABLES)
1632 return EFI_OUT_OF_RESOURCES;
1634 /* Add a new entry */
1635 guidcpy(&systab.tables[i].guid, guid);
1636 systab.tables[i].table = table;
1637 systab.nr_tables = i + 1;
1640 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1641 efi_update_table_header_crc32(&systab.hdr);
1643 /* Notify that the configuration table was changed */
1644 list_for_each_entry(evt, &efi_events, link) {
1645 if (evt->group && !guidcmp(evt->group, guid)) {
1646 efi_signal_event(evt);
1655 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1656 * configuration table.
1657 * @guid: GUID of the installed table
1658 * @table: table to be installed
1660 * This function implements the InstallConfigurationTable service.
1662 * See the Unified Extensible Firmware Interface (UEFI) specification for
1665 * Return: status code
1667 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1670 EFI_ENTRY("%pUl, %p", guid, table);
1671 return EFI_EXIT(efi_install_configuration_table(guid, table));
1675 * efi_setup_loaded_image() - initialize a loaded image
1677 * Initialize a loaded_image_info and loaded_image_info object with correct
1678 * protocols, boot-device, etc.
1680 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
1683 * @device_path: device path of the loaded image
1684 * @file_path: file path of the loaded image
1685 * @handle_ptr: handle of the loaded image
1686 * @info_ptr: loaded image protocol
1687 * Return: status code
1689 efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1690 struct efi_device_path *file_path,
1691 struct efi_loaded_image_obj **handle_ptr,
1692 struct efi_loaded_image **info_ptr)
1695 struct efi_loaded_image *info = NULL;
1696 struct efi_loaded_image_obj *obj = NULL;
1697 struct efi_device_path *dp;
1699 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1703 info = calloc(1, sizeof(*info));
1705 return EFI_OUT_OF_RESOURCES;
1706 obj = calloc(1, sizeof(*obj));
1709 return EFI_OUT_OF_RESOURCES;
1711 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
1713 /* Add internal object to object list */
1714 efi_add_handle(&obj->header);
1716 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1717 info->file_path = file_path;
1718 info->system_table = &systab;
1721 info->device_handle = efi_dp_find_obj(device_path, NULL);
1723 dp = efi_dp_append(device_path, file_path);
1725 ret = EFI_OUT_OF_RESOURCES;
1731 ret = efi_add_protocol(&obj->header,
1732 &efi_guid_loaded_image_device_path, dp);
1733 if (ret != EFI_SUCCESS)
1737 * When asking for the loaded_image interface, just
1738 * return handle which points to loaded_image_info
1740 ret = efi_add_protocol(&obj->header,
1741 &efi_guid_loaded_image, info);
1742 if (ret != EFI_SUCCESS)
1750 printf("ERROR: Failure to install protocols for loaded image\n");
1751 efi_delete_handle(&obj->header);
1757 * efi_load_image_from_path() - load an image using a file path
1759 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1760 * callers obligation to update the memory type as needed.
1762 * @file_path: the path of the image to load
1763 * @buffer: buffer containing the loaded image
1764 * @size: size of the loaded image
1765 * Return: status code
1768 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1769 void **buffer, efi_uintn_t *size)
1771 struct efi_file_info *info = NULL;
1772 struct efi_file_handle *f;
1773 static efi_status_t ret;
1777 /* In case of failure nothing is returned */
1782 f = efi_file_from_path(file_path);
1784 return EFI_NOT_FOUND;
1788 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1790 if (ret != EFI_BUFFER_TOO_SMALL) {
1791 ret = EFI_DEVICE_ERROR;
1796 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1798 if (ret != EFI_SUCCESS)
1802 * When reading the file we do not yet know if it contains an
1803 * application, a boottime driver, or a runtime driver. So here we
1804 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1805 * update the reservation according to the image type.
1807 bs = info->file_size;
1808 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1809 EFI_BOOT_SERVICES_DATA,
1810 efi_size_in_pages(bs), &addr);
1811 if (ret != EFI_SUCCESS) {
1812 ret = EFI_OUT_OF_RESOURCES;
1817 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1818 if (ret != EFI_SUCCESS)
1819 efi_free_pages(addr, efi_size_in_pages(bs));
1820 *buffer = (void *)(uintptr_t)addr;
1823 EFI_CALL(f->close(f));
1829 * efi_load_image() - load an EFI image into memory
1830 * @boot_policy: true for request originating from the boot manager
1831 * @parent_image: the caller's image handle
1832 * @file_path: the path of the image to load
1833 * @source_buffer: memory location from which the image is installed
1834 * @source_size: size of the memory area from which the image is installed
1835 * @image_handle: handle for the newly installed image
1837 * This function implements the LoadImage service.
1839 * See the Unified Extensible Firmware Interface (UEFI) specification
1842 * Return: status code
1844 efi_status_t EFIAPI efi_load_image(bool boot_policy,
1845 efi_handle_t parent_image,
1846 struct efi_device_path *file_path,
1847 void *source_buffer,
1848 efi_uintn_t source_size,
1849 efi_handle_t *image_handle)
1851 struct efi_device_path *dp, *fp;
1852 struct efi_loaded_image *info = NULL;
1853 struct efi_loaded_image_obj **image_obj =
1854 (struct efi_loaded_image_obj **)image_handle;
1858 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1859 file_path, source_buffer, source_size, image_handle);
1861 if (!image_handle || (!source_buffer && !file_path) ||
1862 !efi_search_obj(parent_image) ||
1863 /* The parent image handle must refer to a loaded image */
1864 !parent_image->type) {
1865 ret = EFI_INVALID_PARAMETER;
1869 if (!source_buffer) {
1870 ret = efi_load_image_from_path(file_path, &dest_buffer,
1872 if (ret != EFI_SUCCESS)
1876 ret = EFI_LOAD_ERROR;
1879 dest_buffer = source_buffer;
1881 /* split file_path which contains both the device and file parts */
1882 efi_dp_split_file_path(file_path, &dp, &fp);
1883 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1884 if (ret == EFI_SUCCESS)
1885 ret = efi_load_pe(*image_obj, dest_buffer, info);
1887 /* Release buffer to which file was loaded */
1888 efi_free_pages((uintptr_t)dest_buffer,
1889 efi_size_in_pages(source_size));
1890 if (ret == EFI_SUCCESS) {
1891 info->system_table = &systab;
1892 info->parent_handle = parent_image;
1894 /* The image is invalid. Release all associated resources. */
1895 efi_delete_handle(*image_handle);
1896 *image_handle = NULL;
1900 return EFI_EXIT(ret);
1904 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1906 static void efi_exit_caches(void)
1908 #if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
1910 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
1911 * caches are enabled.
1914 * According to the UEFI spec caches that can be managed via CP15
1915 * operations should be enabled. Caches requiring platform information
1916 * to manage should be disabled. This should not happen in
1917 * ExitBootServices() but before invoking any UEFI binary is invoked.
1919 * We want to keep the current workaround while GRUB prior to version
1920 * 2.04 is still in use.
1922 cleanup_before_linux();
1927 * efi_exit_boot_services() - stop all boot services
1928 * @image_handle: handle of the loaded image
1929 * @map_key: key of the memory map
1931 * This function implements the ExitBootServices service.
1933 * See the Unified Extensible Firmware Interface (UEFI) specification
1936 * All timer events are disabled. For exit boot services events the
1937 * notification function is called. The boot services are disabled in the
1940 * Return: status code
1942 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1943 efi_uintn_t map_key)
1945 struct efi_event *evt, *next_event;
1946 efi_status_t ret = EFI_SUCCESS;
1948 EFI_ENTRY("%p, %zx", image_handle, map_key);
1950 /* Check that the caller has read the current memory map */
1951 if (map_key != efi_memory_map_key) {
1952 ret = EFI_INVALID_PARAMETER;
1956 /* Check if ExitBootServices has already been called */
1957 if (!systab.boottime)
1960 /* Stop all timer related activities */
1961 timers_enabled = false;
1963 /* Add related events to the event group */
1964 list_for_each_entry(evt, &efi_events, link) {
1965 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1966 evt->group = &efi_guid_event_group_exit_boot_services;
1968 /* Notify that ExitBootServices is invoked. */
1969 list_for_each_entry(evt, &efi_events, link) {
1971 !guidcmp(evt->group,
1972 &efi_guid_event_group_exit_boot_services)) {
1973 efi_signal_event(evt);
1978 /* Make sure that notification functions are not called anymore */
1979 efi_tpl = TPL_HIGH_LEVEL;
1981 /* Notify variable services */
1982 efi_variables_boot_exit_notify();
1984 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
1985 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
1986 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
1987 list_del(&evt->link);
1990 board_quiesce_devices();
1992 /* Patch out unsupported runtime function */
1993 efi_runtime_detach();
1995 /* Fix up caches for EFI payloads if necessary */
1998 /* This stops all lingering devices */
1999 bootm_disable_interrupts();
2001 /* Disable boot time services */
2002 systab.con_in_handle = NULL;
2003 systab.con_in = NULL;
2004 systab.con_out_handle = NULL;
2005 systab.con_out = NULL;
2006 systab.stderr_handle = NULL;
2007 systab.std_err = NULL;
2008 systab.boottime = NULL;
2010 /* Recalculate CRC32 */
2011 efi_update_table_header_crc32(&systab.hdr);
2013 /* Give the payload some time to boot */
2014 efi_set_watchdog(0);
2017 return EFI_EXIT(ret);
2021 * efi_get_next_monotonic_count() - get next value of the counter
2022 * @count: returned value of the counter
2024 * This function implements the NextMonotonicCount service.
2026 * See the Unified Extensible Firmware Interface (UEFI) specification for
2029 * Return: status code
2031 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2033 static uint64_t mono;
2036 EFI_ENTRY("%p", count);
2038 ret = EFI_INVALID_PARAMETER;
2044 return EFI_EXIT(ret);
2048 * efi_stall() - sleep
2049 * @microseconds: period to sleep in microseconds
2051 * This function implements the Stall service.
2053 * See the Unified Extensible Firmware Interface (UEFI) specification for
2056 * Return: status code
2058 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2062 EFI_ENTRY("%ld", microseconds);
2064 end_tick = get_ticks() + usec_to_tick(microseconds);
2065 while (get_ticks() < end_tick)
2068 return EFI_EXIT(EFI_SUCCESS);
2072 * efi_set_watchdog_timer() - reset the watchdog timer
2073 * @timeout: seconds before reset by watchdog
2074 * @watchdog_code: code to be logged when resetting
2075 * @data_size: size of buffer in bytes
2076 * @watchdog_data: buffer with data describing the reset reason
2078 * This function implements the SetWatchdogTimer service.
2080 * See the Unified Extensible Firmware Interface (UEFI) specification for
2083 * Return: status code
2085 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2086 uint64_t watchdog_code,
2087 unsigned long data_size,
2088 uint16_t *watchdog_data)
2090 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
2091 data_size, watchdog_data);
2092 return EFI_EXIT(efi_set_watchdog(timeout));
2096 * efi_close_protocol() - close a protocol
2097 * @handle: handle on which the protocol shall be closed
2098 * @protocol: GUID of the protocol to close
2099 * @agent_handle: handle of the driver
2100 * @controller_handle: handle of the controller
2102 * This function implements the CloseProtocol service.
2104 * See the Unified Extensible Firmware Interface (UEFI) specification for
2107 * Return: status code
2109 efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2110 const efi_guid_t *protocol,
2111 efi_handle_t agent_handle,
2112 efi_handle_t controller_handle)
2114 struct efi_handler *handler;
2115 struct efi_open_protocol_info_item *item;
2116 struct efi_open_protocol_info_item *pos;
2119 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
2122 if (!efi_search_obj(agent_handle) ||
2123 (controller_handle && !efi_search_obj(controller_handle))) {
2124 r = EFI_INVALID_PARAMETER;
2127 r = efi_search_protocol(handle, protocol, &handler);
2128 if (r != EFI_SUCCESS)
2132 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2133 if (item->info.agent_handle == agent_handle &&
2134 item->info.controller_handle == controller_handle) {
2135 efi_delete_open_info(item);
2144 * efi_open_protocol_information() - provide information about then open status
2145 * of a protocol on a handle
2146 * @handle: handle for which the information shall be retrieved
2147 * @protocol: GUID of the protocol
2148 * @entry_buffer: buffer to receive the open protocol information
2149 * @entry_count: number of entries available in the buffer
2151 * This function implements the OpenProtocolInformation service.
2153 * See the Unified Extensible Firmware Interface (UEFI) specification for
2156 * Return: status code
2158 static efi_status_t EFIAPI efi_open_protocol_information(
2159 efi_handle_t handle, const efi_guid_t *protocol,
2160 struct efi_open_protocol_info_entry **entry_buffer,
2161 efi_uintn_t *entry_count)
2163 unsigned long buffer_size;
2164 unsigned long count;
2165 struct efi_handler *handler;
2166 struct efi_open_protocol_info_item *item;
2169 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2172 /* Check parameters */
2173 if (!entry_buffer) {
2174 r = EFI_INVALID_PARAMETER;
2177 r = efi_search_protocol(handle, protocol, &handler);
2178 if (r != EFI_SUCCESS)
2183 list_for_each_entry(item, &handler->open_infos, link) {
2184 if (item->info.open_count)
2187 *entry_count = count;
2188 *entry_buffer = NULL;
2195 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2196 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2197 (void **)entry_buffer);
2198 if (r != EFI_SUCCESS)
2200 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2201 if (item->info.open_count)
2202 (*entry_buffer)[--count] = item->info;
2209 * efi_protocols_per_handle() - get protocols installed on a handle
2210 * @handle: handle for which the information is retrieved
2211 * @protocol_buffer: buffer with protocol GUIDs
2212 * @protocol_buffer_count: number of entries in the buffer
2214 * This function implements the ProtocolsPerHandleService.
2216 * See the Unified Extensible Firmware Interface (UEFI) specification for
2219 * Return: status code
2221 static efi_status_t EFIAPI efi_protocols_per_handle(
2222 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2223 efi_uintn_t *protocol_buffer_count)
2225 unsigned long buffer_size;
2226 struct efi_object *efiobj;
2227 struct list_head *protocol_handle;
2230 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2231 protocol_buffer_count);
2233 if (!handle || !protocol_buffer || !protocol_buffer_count)
2234 return EFI_EXIT(EFI_INVALID_PARAMETER);
2236 *protocol_buffer = NULL;
2237 *protocol_buffer_count = 0;
2239 efiobj = efi_search_obj(handle);
2241 return EFI_EXIT(EFI_INVALID_PARAMETER);
2243 /* Count protocols */
2244 list_for_each(protocol_handle, &efiobj->protocols) {
2245 ++*protocol_buffer_count;
2249 if (*protocol_buffer_count) {
2252 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2253 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2254 (void **)protocol_buffer);
2255 if (r != EFI_SUCCESS)
2257 list_for_each(protocol_handle, &efiobj->protocols) {
2258 struct efi_handler *protocol;
2260 protocol = list_entry(protocol_handle,
2261 struct efi_handler, link);
2262 (*protocol_buffer)[j] = (void *)protocol->guid;
2267 return EFI_EXIT(EFI_SUCCESS);
2271 * efi_locate_handle_buffer() - locate handles implementing a protocol
2272 * @search_type: selection criterion
2273 * @protocol: GUID of the protocol
2274 * @search_key: registration key
2275 * @no_handles: number of returned handles
2276 * @buffer: buffer with the returned handles
2278 * This function implements the LocateHandleBuffer service.
2280 * See the Unified Extensible Firmware Interface (UEFI) specification for
2283 * Return: status code
2285 efi_status_t EFIAPI efi_locate_handle_buffer(
2286 enum efi_locate_search_type search_type,
2287 const efi_guid_t *protocol, void *search_key,
2288 efi_uintn_t *no_handles, efi_handle_t **buffer)
2291 efi_uintn_t buffer_size = 0;
2293 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2294 no_handles, buffer);
2296 if (!no_handles || !buffer) {
2297 r = EFI_INVALID_PARAMETER;
2302 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2304 if (r != EFI_BUFFER_TOO_SMALL)
2306 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2308 if (r != EFI_SUCCESS)
2310 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2312 if (r == EFI_SUCCESS)
2313 *no_handles = buffer_size / sizeof(efi_handle_t);
2319 * efi_locate_protocol() - find an interface implementing a protocol
2320 * @protocol: GUID of the protocol
2321 * @registration: registration key passed to the notification function
2322 * @protocol_interface: interface implementing the protocol
2324 * This function implements the LocateProtocol service.
2326 * See the Unified Extensible Firmware Interface (UEFI) specification for
2329 * Return: status code
2331 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2333 void **protocol_interface)
2335 struct efi_handler *handler;
2337 struct efi_object *efiobj;
2339 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2342 * The UEFI spec explicitly requires a protocol even if a registration
2343 * key is provided. This differs from the logic in LocateHandle().
2345 if (!protocol || !protocol_interface)
2346 return EFI_EXIT(EFI_INVALID_PARAMETER);
2349 struct efi_register_notify_event *event;
2350 struct efi_protocol_notification *handle;
2352 event = efi_check_register_notify_event(registration);
2354 return EFI_EXIT(EFI_INVALID_PARAMETER);
2356 * The UEFI spec requires to return EFI_NOT_FOUND if no
2357 * protocol instance matches protocol and registration.
2358 * So let's do the same for a mismatch between protocol and
2361 if (guidcmp(&event->protocol, protocol))
2363 if (list_empty(&event->handles))
2365 handle = list_first_entry(&event->handles,
2366 struct efi_protocol_notification,
2368 efiobj = handle->handle;
2369 list_del(&handle->link);
2371 ret = efi_search_protocol(efiobj, protocol, &handler);
2372 if (ret == EFI_SUCCESS)
2375 list_for_each_entry(efiobj, &efi_obj_list, link) {
2376 ret = efi_search_protocol(efiobj, protocol, &handler);
2377 if (ret == EFI_SUCCESS)
2382 *protocol_interface = NULL;
2383 return EFI_EXIT(EFI_NOT_FOUND);
2385 *protocol_interface = handler->protocol_interface;
2386 return EFI_EXIT(EFI_SUCCESS);
2390 * efi_locate_device_path() - Get the device path and handle of an device
2391 * implementing a protocol
2392 * @protocol: GUID of the protocol
2393 * @device_path: device path
2394 * @device: handle of the device
2396 * This function implements the LocateDevicePath service.
2398 * See the Unified Extensible Firmware Interface (UEFI) specification for
2401 * Return: status code
2403 static efi_status_t EFIAPI efi_locate_device_path(
2404 const efi_guid_t *protocol,
2405 struct efi_device_path **device_path,
2406 efi_handle_t *device)
2408 struct efi_device_path *dp;
2410 struct efi_handler *handler;
2411 efi_handle_t *handles;
2413 size_t len_best = 0;
2414 efi_uintn_t no_handles;
2418 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2420 if (!protocol || !device_path || !*device_path) {
2421 ret = EFI_INVALID_PARAMETER;
2425 /* Find end of device path */
2426 len = efi_dp_instance_size(*device_path);
2428 /* Get all handles implementing the protocol */
2429 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2430 &no_handles, &handles));
2431 if (ret != EFI_SUCCESS)
2434 for (i = 0; i < no_handles; ++i) {
2435 /* Find the device path protocol */
2436 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2438 if (ret != EFI_SUCCESS)
2440 dp = (struct efi_device_path *)handler->protocol_interface;
2441 len_dp = efi_dp_instance_size(dp);
2443 * This handle can only be a better fit
2444 * if its device path length is longer than the best fit and
2445 * if its device path length is shorter of equal the searched
2448 if (len_dp <= len_best || len_dp > len)
2450 /* Check if dp is a subpath of device_path */
2451 if (memcmp(*device_path, dp, len_dp))
2454 ret = EFI_INVALID_PARAMETER;
2457 *device = handles[i];
2461 remainder = (u8 *)*device_path + len_best;
2462 *device_path = (struct efi_device_path *)remainder;
2465 ret = EFI_NOT_FOUND;
2468 return EFI_EXIT(ret);
2472 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2474 * @handle: handle on which the protocol interfaces shall be installed
2475 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2478 * This function implements the MultipleProtocolInterfaces service.
2480 * See the Unified Extensible Firmware Interface (UEFI) specification for
2483 * Return: status code
2485 efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2486 (efi_handle_t *handle, ...)
2488 EFI_ENTRY("%p", handle);
2491 const efi_guid_t *protocol;
2492 void *protocol_interface;
2493 efi_handle_t old_handle;
2494 efi_status_t r = EFI_SUCCESS;
2498 return EFI_EXIT(EFI_INVALID_PARAMETER);
2500 efi_va_start(argptr, handle);
2502 protocol = efi_va_arg(argptr, efi_guid_t*);
2505 protocol_interface = efi_va_arg(argptr, void*);
2506 /* Check that a device path has not been installed before */
2507 if (!guidcmp(protocol, &efi_guid_device_path)) {
2508 struct efi_device_path *dp = protocol_interface;
2510 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2512 if (r == EFI_SUCCESS &&
2513 dp->type == DEVICE_PATH_TYPE_END) {
2514 EFI_PRINT("Path %pD already installed\n",
2515 protocol_interface);
2516 r = EFI_ALREADY_STARTED;
2520 r = EFI_CALL(efi_install_protocol_interface(
2522 EFI_NATIVE_INTERFACE,
2523 protocol_interface));
2524 if (r != EFI_SUCCESS)
2529 if (r == EFI_SUCCESS)
2532 /* If an error occurred undo all changes. */
2533 efi_va_start(argptr, handle);
2535 protocol = efi_va_arg(argptr, efi_guid_t*);
2536 protocol_interface = efi_va_arg(argptr, void*);
2537 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2538 protocol_interface));
2546 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2548 * @handle: handle from which the protocol interfaces shall be removed
2549 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2552 * This function implements the UninstallMultipleProtocolInterfaces service.
2554 * See the Unified Extensible Firmware Interface (UEFI) specification for
2557 * Return: status code
2559 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2560 efi_handle_t handle, ...)
2562 EFI_ENTRY("%p", handle);
2565 const efi_guid_t *protocol;
2566 void *protocol_interface;
2567 efi_status_t r = EFI_SUCCESS;
2571 return EFI_EXIT(EFI_INVALID_PARAMETER);
2573 efi_va_start(argptr, handle);
2575 protocol = efi_va_arg(argptr, efi_guid_t*);
2578 protocol_interface = efi_va_arg(argptr, void*);
2579 r = efi_uninstall_protocol(handle, protocol,
2580 protocol_interface);
2581 if (r != EFI_SUCCESS)
2586 if (r == EFI_SUCCESS) {
2587 /* If the last protocol has been removed, delete the handle. */
2588 if (list_empty(&handle->protocols)) {
2589 list_del(&handle->link);
2595 /* If an error occurred undo all changes. */
2596 efi_va_start(argptr, handle);
2598 protocol = efi_va_arg(argptr, efi_guid_t*);
2599 protocol_interface = efi_va_arg(argptr, void*);
2600 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2601 EFI_NATIVE_INTERFACE,
2602 protocol_interface));
2606 /* In case of an error always return EFI_INVALID_PARAMETER */
2607 return EFI_EXIT(EFI_INVALID_PARAMETER);
2611 * efi_calculate_crc32() - calculate cyclic redundancy code
2612 * @data: buffer with data
2613 * @data_size: size of buffer in bytes
2614 * @crc32_p: cyclic redundancy code
2616 * This function implements the CalculateCrc32 service.
2618 * See the Unified Extensible Firmware Interface (UEFI) specification for
2621 * Return: status code
2623 static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2624 efi_uintn_t data_size,
2627 efi_status_t ret = EFI_SUCCESS;
2629 EFI_ENTRY("%p, %zu", data, data_size);
2630 if (!data || !data_size || !crc32_p) {
2631 ret = EFI_INVALID_PARAMETER;
2634 *crc32_p = crc32(0, data, data_size);
2636 return EFI_EXIT(ret);
2640 * efi_copy_mem() - copy memory
2641 * @destination: destination of the copy operation
2642 * @source: source of the copy operation
2643 * @length: number of bytes to copy
2645 * This function implements the CopyMem service.
2647 * See the Unified Extensible Firmware Interface (UEFI) specification for
2650 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2653 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2654 memmove(destination, source, length);
2655 EFI_EXIT(EFI_SUCCESS);
2659 * efi_set_mem() - Fill memory with a byte value.
2660 * @buffer: buffer to fill
2661 * @size: size of buffer in bytes
2662 * @value: byte to copy to the buffer
2664 * This function implements the SetMem service.
2666 * See the Unified Extensible Firmware Interface (UEFI) specification for
2669 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2671 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2672 memset(buffer, value, size);
2673 EFI_EXIT(EFI_SUCCESS);
2677 * efi_protocol_open() - open protocol interface on a handle
2678 * @handler: handler of a protocol
2679 * @protocol_interface: interface implementing the protocol
2680 * @agent_handle: handle of the driver
2681 * @controller_handle: handle of the controller
2682 * @attributes: attributes indicating how to open the protocol
2684 * Return: status code
2686 static efi_status_t efi_protocol_open(
2687 struct efi_handler *handler,
2688 void **protocol_interface, void *agent_handle,
2689 void *controller_handle, uint32_t attributes)
2691 struct efi_open_protocol_info_item *item;
2692 struct efi_open_protocol_info_entry *match = NULL;
2693 bool opened_by_driver = false;
2694 bool opened_exclusive = false;
2696 /* If there is no agent, only return the interface */
2700 /* For TEST_PROTOCOL ignore interface attribute */
2701 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2702 *protocol_interface = NULL;
2705 * Check if the protocol is already opened by a driver with the same
2706 * attributes or opened exclusively
2708 list_for_each_entry(item, &handler->open_infos, link) {
2709 if (item->info.agent_handle == agent_handle) {
2710 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2711 (item->info.attributes == attributes))
2712 return EFI_ALREADY_STARTED;
2714 if (item->info.attributes &
2715 EFI_OPEN_PROTOCOL_BY_DRIVER)
2716 opened_by_driver = true;
2718 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2719 opened_exclusive = true;
2722 /* Only one controller can open the protocol exclusively */
2723 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2724 if (opened_exclusive)
2725 return EFI_ACCESS_DENIED;
2726 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2727 if (opened_exclusive || opened_by_driver)
2728 return EFI_ACCESS_DENIED;
2731 /* Prepare exclusive opening */
2732 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2733 /* Try to disconnect controllers */
2735 opened_by_driver = false;
2736 list_for_each_entry(item, &handler->open_infos, link) {
2739 if (item->info.attributes ==
2740 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2741 ret = EFI_CALL(efi_disconnect_controller(
2742 item->info.controller_handle,
2743 item->info.agent_handle,
2745 if (ret == EFI_SUCCESS)
2747 * Child controllers may have been
2748 * removed from the open_infos list. So
2749 * let's restart the loop.
2751 goto disconnect_next;
2753 opened_by_driver = true;
2756 /* Only one driver can be connected */
2757 if (opened_by_driver)
2758 return EFI_ACCESS_DENIED;
2761 /* Find existing entry */
2762 list_for_each_entry(item, &handler->open_infos, link) {
2763 if (item->info.agent_handle == agent_handle &&
2764 item->info.controller_handle == controller_handle &&
2765 item->info.attributes == attributes)
2766 match = &item->info;
2768 /* None found, create one */
2770 match = efi_create_open_info(handler);
2772 return EFI_OUT_OF_RESOURCES;
2775 match->agent_handle = agent_handle;
2776 match->controller_handle = controller_handle;
2777 match->attributes = attributes;
2778 match->open_count++;
2781 /* For TEST_PROTOCOL ignore interface attribute. */
2782 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2783 *protocol_interface = handler->protocol_interface;
2789 * efi_open_protocol() - open protocol interface on a handle
2790 * @handle: handle on which the protocol shall be opened
2791 * @protocol: GUID of the protocol
2792 * @protocol_interface: interface implementing the protocol
2793 * @agent_handle: handle of the driver
2794 * @controller_handle: handle of the controller
2795 * @attributes: attributes indicating how to open the protocol
2797 * This function implements the OpenProtocol interface.
2799 * See the Unified Extensible Firmware Interface (UEFI) specification for
2802 * Return: status code
2804 static efi_status_t EFIAPI efi_open_protocol
2805 (efi_handle_t handle, const efi_guid_t *protocol,
2806 void **protocol_interface, efi_handle_t agent_handle,
2807 efi_handle_t controller_handle, uint32_t attributes)
2809 struct efi_handler *handler;
2810 efi_status_t r = EFI_INVALID_PARAMETER;
2812 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2813 protocol_interface, agent_handle, controller_handle,
2816 if (!handle || !protocol ||
2817 (!protocol_interface && attributes !=
2818 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2822 switch (attributes) {
2823 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2824 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2825 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2827 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2828 if (controller_handle == handle)
2831 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2832 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2833 /* Check that the controller handle is valid */
2834 if (!efi_search_obj(controller_handle))
2837 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2838 /* Check that the agent handle is valid */
2839 if (!efi_search_obj(agent_handle))
2846 r = efi_search_protocol(handle, protocol, &handler);
2851 r = EFI_UNSUPPORTED;
2857 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2858 controller_handle, attributes);
2864 * efi_start_image() - call the entry point of an image
2865 * @image_handle: handle of the image
2866 * @exit_data_size: size of the buffer
2867 * @exit_data: buffer to receive the exit data of the called image
2869 * This function implements the StartImage service.
2871 * See the Unified Extensible Firmware Interface (UEFI) specification for
2874 * Return: status code
2876 efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2877 efi_uintn_t *exit_data_size,
2880 struct efi_loaded_image_obj *image_obj =
2881 (struct efi_loaded_image_obj *)image_handle;
2884 efi_handle_t parent_image = current_image;
2886 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2888 /* Check parameters */
2889 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2890 return EFI_EXIT(EFI_INVALID_PARAMETER);
2892 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2894 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2895 if (ret != EFI_SUCCESS)
2896 return EFI_EXIT(EFI_INVALID_PARAMETER);
2898 image_obj->exit_data_size = exit_data_size;
2899 image_obj->exit_data = exit_data;
2901 /* call the image! */
2902 if (setjmp(&image_obj->exit_jmp)) {
2904 * We called the entry point of the child image with EFI_CALL
2905 * in the lines below. The child image called the Exit() boot
2906 * service efi_exit() which executed the long jump that brought
2907 * us to the current line. This implies that the second half
2908 * of the EFI_CALL macro has not been executed.
2912 * efi_exit() called efi_restore_gd(). We have to undo this
2913 * otherwise __efi_entry_check() will put the wrong value into
2919 * To get ready to call EFI_EXIT below we have to execute the
2920 * missed out steps of EFI_CALL.
2922 assert(__efi_entry_check());
2923 EFI_PRINT("%lu returned by started image\n",
2924 (unsigned long)((uintptr_t)image_obj->exit_status &
2926 current_image = parent_image;
2927 return EFI_EXIT(image_obj->exit_status);
2930 current_image = image_handle;
2931 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
2932 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
2933 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2936 * Control is returned from a started UEFI image either by calling
2937 * Exit() (where exit data can be provided) or by simply returning from
2938 * the entry point. In the latter case call Exit() on behalf of the
2941 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2945 * efi_delete_image() - delete loaded image from memory)
2947 * @image_obj: handle of the loaded image
2948 * @loaded_image_protocol: loaded image protocol
2950 static efi_status_t efi_delete_image
2951 (struct efi_loaded_image_obj *image_obj,
2952 struct efi_loaded_image *loaded_image_protocol)
2954 struct efi_object *efiobj;
2955 efi_status_t r, ret = EFI_SUCCESS;
2958 list_for_each_entry(efiobj, &efi_obj_list, link) {
2959 struct efi_handler *protocol;
2961 list_for_each_entry(protocol, &efiobj->protocols, link) {
2962 struct efi_open_protocol_info_item *info;
2964 list_for_each_entry(info, &protocol->open_infos, link) {
2965 if (info->info.agent_handle !=
2966 (efi_handle_t)image_obj)
2968 r = EFI_CALL(efi_close_protocol
2969 (efiobj, protocol->guid,
2970 info->info.agent_handle,
2971 info->info.controller_handle
2973 if (r != EFI_SUCCESS)
2976 * Closing protocols may results in further
2977 * items being deleted. To play it safe loop
2978 * over all elements again.
2985 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2986 efi_size_in_pages(loaded_image_protocol->image_size));
2987 efi_delete_handle(&image_obj->header);
2993 * efi_unload_image() - unload an EFI image
2994 * @image_handle: handle of the image to be unloaded
2996 * This function implements the UnloadImage service.
2998 * See the Unified Extensible Firmware Interface (UEFI) specification for
3001 * Return: status code
3003 efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3005 efi_status_t ret = EFI_SUCCESS;
3006 struct efi_object *efiobj;
3007 struct efi_loaded_image *loaded_image_protocol;
3009 EFI_ENTRY("%p", image_handle);
3011 efiobj = efi_search_obj(image_handle);
3013 ret = EFI_INVALID_PARAMETER;
3016 /* Find the loaded image protocol */
3017 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3018 (void **)&loaded_image_protocol,
3020 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3021 if (ret != EFI_SUCCESS) {
3022 ret = EFI_INVALID_PARAMETER;
3025 switch (efiobj->type) {
3026 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3027 /* Call the unload function */
3028 if (!loaded_image_protocol->unload) {
3029 ret = EFI_UNSUPPORTED;
3032 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3033 if (ret != EFI_SUCCESS)
3036 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3039 ret = EFI_INVALID_PARAMETER;
3042 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3043 loaded_image_protocol);
3045 return EFI_EXIT(ret);
3049 * efi_update_exit_data() - fill exit data parameters of StartImage()
3051 * @image_obj: image handle
3052 * @exit_data_size: size of the exit data buffer
3053 * @exit_data: buffer with data returned by UEFI payload
3054 * Return: status code
3056 static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3057 efi_uintn_t exit_data_size,
3063 * If exit_data is not provided to StartImage(), exit_data_size must be
3066 if (!image_obj->exit_data)
3068 if (image_obj->exit_data_size)
3069 *image_obj->exit_data_size = exit_data_size;
3070 if (exit_data_size && exit_data) {
3071 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3073 (void **)image_obj->exit_data);
3074 if (ret != EFI_SUCCESS)
3076 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3078 image_obj->exit_data = NULL;
3084 * efi_exit() - leave an EFI application or driver
3085 * @image_handle: handle of the application or driver that is exiting
3086 * @exit_status: status code
3087 * @exit_data_size: size of the buffer in bytes
3088 * @exit_data: buffer with data describing an error
3090 * This function implements the Exit service.
3092 * See the Unified Extensible Firmware Interface (UEFI) specification for
3095 * Return: status code
3097 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3098 efi_status_t exit_status,
3099 efi_uintn_t exit_data_size,
3103 * TODO: We should call the unload procedure of the loaded
3107 struct efi_loaded_image *loaded_image_protocol;
3108 struct efi_loaded_image_obj *image_obj =
3109 (struct efi_loaded_image_obj *)image_handle;
3111 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3112 exit_data_size, exit_data);
3114 /* Check parameters */
3115 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3116 (void **)&loaded_image_protocol,
3118 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3119 if (ret != EFI_SUCCESS) {
3120 ret = EFI_INVALID_PARAMETER;
3124 /* Unloading of unstarted images */
3125 switch (image_obj->header.type) {
3126 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3128 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3129 efi_delete_image(image_obj, loaded_image_protocol);
3133 /* Handle does not refer to loaded image */
3134 ret = EFI_INVALID_PARAMETER;
3137 /* A started image can only be unloaded it is the last one started. */
3138 if (image_handle != current_image) {
3139 ret = EFI_INVALID_PARAMETER;
3143 /* Exit data is only foreseen in case of failure. */
3144 if (exit_status != EFI_SUCCESS) {
3145 ret = efi_update_exit_data(image_obj, exit_data_size,
3147 /* Exiting has priority. Don't return error to caller. */
3148 if (ret != EFI_SUCCESS)
3149 EFI_PRINT("%s: out of memory\n", __func__);
3151 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3152 exit_status != EFI_SUCCESS)
3153 efi_delete_image(image_obj, loaded_image_protocol);
3155 /* Make sure entry/exit counts for EFI world cross-overs match */
3156 EFI_EXIT(exit_status);
3159 * But longjmp out with the U-Boot gd, not the application's, as
3160 * the other end is a setjmp call inside EFI context.
3164 image_obj->exit_status = exit_status;
3165 longjmp(&image_obj->exit_jmp, 1);
3167 panic("EFI application exited");
3169 return EFI_EXIT(ret);
3173 * efi_handle_protocol() - get interface of a protocol on a handle
3174 * @handle: handle on which the protocol shall be opened
3175 * @protocol: GUID of the protocol
3176 * @protocol_interface: interface implementing the protocol
3178 * This function implements the HandleProtocol service.
3180 * See the Unified Extensible Firmware Interface (UEFI) specification for
3183 * Return: status code
3185 efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3186 const efi_guid_t *protocol,
3187 void **protocol_interface)
3189 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
3190 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
3194 * efi_bind_controller() - bind a single driver to a controller
3195 * @controller_handle: controller handle
3196 * @driver_image_handle: driver handle
3197 * @remain_device_path: remaining path
3199 * Return: status code
3201 static efi_status_t efi_bind_controller(
3202 efi_handle_t controller_handle,
3203 efi_handle_t driver_image_handle,
3204 struct efi_device_path *remain_device_path)
3206 struct efi_driver_binding_protocol *binding_protocol;
3209 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3210 &efi_guid_driver_binding_protocol,
3211 (void **)&binding_protocol,
3212 driver_image_handle, NULL,
3213 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3214 if (r != EFI_SUCCESS)
3216 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3218 remain_device_path));
3219 if (r == EFI_SUCCESS)
3220 r = EFI_CALL(binding_protocol->start(binding_protocol,
3222 remain_device_path));
3223 EFI_CALL(efi_close_protocol(driver_image_handle,
3224 &efi_guid_driver_binding_protocol,
3225 driver_image_handle, NULL));
3230 * efi_connect_single_controller() - connect a single driver to a controller
3231 * @controller_handle: controller
3232 * @driver_image_handle: driver
3233 * @remain_device_path: remaining path
3235 * Return: status code
3237 static efi_status_t efi_connect_single_controller(
3238 efi_handle_t controller_handle,
3239 efi_handle_t *driver_image_handle,
3240 struct efi_device_path *remain_device_path)
3242 efi_handle_t *buffer;
3246 size_t connected = 0;
3248 /* Get buffer with all handles with driver binding protocol */
3249 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3250 &efi_guid_driver_binding_protocol,
3251 NULL, &count, &buffer));
3252 if (r != EFI_SUCCESS)
3255 /* Context Override */
3256 if (driver_image_handle) {
3257 for (; *driver_image_handle; ++driver_image_handle) {
3258 for (i = 0; i < count; ++i) {
3259 if (buffer[i] == *driver_image_handle) {
3261 r = efi_bind_controller(
3263 *driver_image_handle,
3264 remain_device_path);
3266 * For drivers that do not support the
3267 * controller or are already connected
3268 * we receive an error code here.
3270 if (r == EFI_SUCCESS)
3278 * TODO: Some overrides are not yet implemented:
3279 * - Platform Driver Override
3280 * - Driver Family Override Search
3281 * - Bus Specific Driver Override
3284 /* Driver Binding Search */
3285 for (i = 0; i < count; ++i) {
3287 r = efi_bind_controller(controller_handle,
3289 remain_device_path);
3290 if (r == EFI_SUCCESS)
3295 efi_free_pool(buffer);
3297 return EFI_NOT_FOUND;
3302 * efi_connect_controller() - connect a controller to a driver
3303 * @controller_handle: handle of the controller
3304 * @driver_image_handle: handle of the driver
3305 * @remain_device_path: device path of a child controller
3306 * @recursive: true to connect all child controllers
3308 * This function implements the ConnectController service.
3310 * See the Unified Extensible Firmware Interface (UEFI) specification for
3313 * First all driver binding protocol handles are tried for binding drivers.
3314 * Afterwards all handles that have opened a protocol of the controller
3315 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3317 * Return: status code
3319 static efi_status_t EFIAPI efi_connect_controller(
3320 efi_handle_t controller_handle,
3321 efi_handle_t *driver_image_handle,
3322 struct efi_device_path *remain_device_path,
3326 efi_status_t ret = EFI_NOT_FOUND;
3327 struct efi_object *efiobj;
3329 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
3330 remain_device_path, recursive);
3332 efiobj = efi_search_obj(controller_handle);
3334 ret = EFI_INVALID_PARAMETER;
3338 r = efi_connect_single_controller(controller_handle,
3339 driver_image_handle,
3340 remain_device_path);
3341 if (r == EFI_SUCCESS)
3344 struct efi_handler *handler;
3345 struct efi_open_protocol_info_item *item;
3347 list_for_each_entry(handler, &efiobj->protocols, link) {
3348 list_for_each_entry(item, &handler->open_infos, link) {
3349 if (item->info.attributes &
3350 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3351 r = EFI_CALL(efi_connect_controller(
3352 item->info.controller_handle,
3353 driver_image_handle,
3356 if (r == EFI_SUCCESS)
3362 /* Check for child controller specified by end node */
3363 if (ret != EFI_SUCCESS && remain_device_path &&
3364 remain_device_path->type == DEVICE_PATH_TYPE_END)
3367 return EFI_EXIT(ret);
3371 * efi_reinstall_protocol_interface() - reinstall protocol interface
3372 * @handle: handle on which the protocol shall be reinstalled
3373 * @protocol: GUID of the protocol to be installed
3374 * @old_interface: interface to be removed
3375 * @new_interface: interface to be installed
3377 * This function implements the ReinstallProtocolInterface service.
3379 * See the Unified Extensible Firmware Interface (UEFI) specification for
3382 * The old interface is uninstalled. The new interface is installed.
3383 * Drivers are connected.
3385 * Return: status code
3387 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3388 efi_handle_t handle, const efi_guid_t *protocol,
3389 void *old_interface, void *new_interface)
3393 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3396 /* Uninstall protocol but do not delete handle */
3397 ret = efi_uninstall_protocol(handle, protocol, old_interface);
3398 if (ret != EFI_SUCCESS)
3401 /* Install the new protocol */
3402 ret = efi_add_protocol(handle, protocol, new_interface);
3404 * The UEFI spec does not specify what should happen to the handle
3405 * if in case of an error no protocol interface remains on the handle.
3406 * So let's do nothing here.
3408 if (ret != EFI_SUCCESS)
3411 * The returned status code has to be ignored.
3412 * Do not create an error if no suitable driver for the handle exists.
3414 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3416 return EFI_EXIT(ret);
3420 * efi_get_child_controllers() - get all child controllers associated to a driver
3421 * @efiobj: handle of the controller
3422 * @driver_handle: handle of the driver
3423 * @number_of_children: number of child controllers
3424 * @child_handle_buffer: handles of the the child controllers
3426 * The allocated buffer has to be freed with free().
3428 * Return: status code
3430 static efi_status_t efi_get_child_controllers(
3431 struct efi_object *efiobj,
3432 efi_handle_t driver_handle,
3433 efi_uintn_t *number_of_children,
3434 efi_handle_t **child_handle_buffer)
3436 struct efi_handler *handler;
3437 struct efi_open_protocol_info_item *item;
3438 efi_uintn_t count = 0, i;
3441 /* Count all child controller associations */
3442 list_for_each_entry(handler, &efiobj->protocols, link) {
3443 list_for_each_entry(item, &handler->open_infos, link) {
3444 if (item->info.agent_handle == driver_handle &&
3445 item->info.attributes &
3446 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3451 * Create buffer. In case of duplicate child controller assignments
3452 * the buffer will be too large. But that does not harm.
3454 *number_of_children = 0;
3455 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3456 if (!*child_handle_buffer)
3457 return EFI_OUT_OF_RESOURCES;
3458 /* Copy unique child handles */
3459 list_for_each_entry(handler, &efiobj->protocols, link) {
3460 list_for_each_entry(item, &handler->open_infos, link) {
3461 if (item->info.agent_handle == driver_handle &&
3462 item->info.attributes &
3463 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3464 /* Check this is a new child controller */
3466 for (i = 0; i < *number_of_children; ++i) {
3467 if ((*child_handle_buffer)[i] ==
3468 item->info.controller_handle)
3471 /* Copy handle to buffer */
3473 i = (*number_of_children)++;
3474 (*child_handle_buffer)[i] =
3475 item->info.controller_handle;
3484 * efi_disconnect_controller() - disconnect a controller from a driver
3485 * @controller_handle: handle of the controller
3486 * @driver_image_handle: handle of the driver
3487 * @child_handle: handle of the child to destroy
3489 * This function implements the DisconnectController service.
3491 * See the Unified Extensible Firmware Interface (UEFI) specification for
3494 * Return: status code
3496 static efi_status_t EFIAPI efi_disconnect_controller(
3497 efi_handle_t controller_handle,
3498 efi_handle_t driver_image_handle,
3499 efi_handle_t child_handle)
3501 struct efi_driver_binding_protocol *binding_protocol;
3502 efi_handle_t *child_handle_buffer = NULL;
3503 size_t number_of_children = 0;
3505 struct efi_object *efiobj;
3507 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3510 efiobj = efi_search_obj(controller_handle);
3512 r = EFI_INVALID_PARAMETER;
3516 if (child_handle && !efi_search_obj(child_handle)) {
3517 r = EFI_INVALID_PARAMETER;
3521 /* If no driver handle is supplied, disconnect all drivers */
3522 if (!driver_image_handle) {
3523 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3527 /* Create list of child handles */
3529 number_of_children = 1;
3530 child_handle_buffer = &child_handle;
3532 efi_get_child_controllers(efiobj,
3533 driver_image_handle,
3534 &number_of_children,
3535 &child_handle_buffer);
3538 /* Get the driver binding protocol */
3539 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3540 &efi_guid_driver_binding_protocol,
3541 (void **)&binding_protocol,
3542 driver_image_handle, NULL,
3543 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3544 if (r != EFI_SUCCESS) {
3545 r = EFI_INVALID_PARAMETER;
3548 /* Remove the children */
3549 if (number_of_children) {
3550 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3553 child_handle_buffer));
3554 if (r != EFI_SUCCESS) {
3555 r = EFI_DEVICE_ERROR;
3559 /* Remove the driver */
3560 if (!child_handle) {
3561 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3564 if (r != EFI_SUCCESS) {
3565 r = EFI_DEVICE_ERROR;
3569 EFI_CALL(efi_close_protocol(driver_image_handle,
3570 &efi_guid_driver_binding_protocol,
3571 driver_image_handle, NULL));
3575 free(child_handle_buffer);
3579 static struct efi_boot_services efi_boot_services = {
3581 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3582 .revision = EFI_SPECIFICATION_VERSION,
3583 .headersize = sizeof(struct efi_boot_services),
3585 .raise_tpl = efi_raise_tpl,
3586 .restore_tpl = efi_restore_tpl,
3587 .allocate_pages = efi_allocate_pages_ext,
3588 .free_pages = efi_free_pages_ext,
3589 .get_memory_map = efi_get_memory_map_ext,
3590 .allocate_pool = efi_allocate_pool_ext,
3591 .free_pool = efi_free_pool_ext,
3592 .create_event = efi_create_event_ext,
3593 .set_timer = efi_set_timer_ext,
3594 .wait_for_event = efi_wait_for_event,
3595 .signal_event = efi_signal_event_ext,
3596 .close_event = efi_close_event,
3597 .check_event = efi_check_event,
3598 .install_protocol_interface = efi_install_protocol_interface,
3599 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3600 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3601 .handle_protocol = efi_handle_protocol,
3603 .register_protocol_notify = efi_register_protocol_notify,
3604 .locate_handle = efi_locate_handle_ext,
3605 .locate_device_path = efi_locate_device_path,
3606 .install_configuration_table = efi_install_configuration_table_ext,
3607 .load_image = efi_load_image,
3608 .start_image = efi_start_image,
3610 .unload_image = efi_unload_image,
3611 .exit_boot_services = efi_exit_boot_services,
3612 .get_next_monotonic_count = efi_get_next_monotonic_count,
3614 .set_watchdog_timer = efi_set_watchdog_timer,
3615 .connect_controller = efi_connect_controller,
3616 .disconnect_controller = efi_disconnect_controller,
3617 .open_protocol = efi_open_protocol,
3618 .close_protocol = efi_close_protocol,
3619 .open_protocol_information = efi_open_protocol_information,
3620 .protocols_per_handle = efi_protocols_per_handle,
3621 .locate_handle_buffer = efi_locate_handle_buffer,
3622 .locate_protocol = efi_locate_protocol,
3623 .install_multiple_protocol_interfaces =
3624 efi_install_multiple_protocol_interfaces,
3625 .uninstall_multiple_protocol_interfaces =
3626 efi_uninstall_multiple_protocol_interfaces,
3627 .calculate_crc32 = efi_calculate_crc32,
3628 .copy_mem = efi_copy_mem,
3629 .set_mem = efi_set_mem,
3630 .create_event_ex = efi_create_event_ex,
3633 static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3635 struct efi_system_table __efi_runtime_data systab = {
3637 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3638 .revision = EFI_SPECIFICATION_VERSION,
3639 .headersize = sizeof(struct efi_system_table),
3641 .fw_vendor = firmware_vendor,
3642 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3643 .runtime = &efi_runtime_services,
3649 * efi_initialize_system_table() - Initialize system table
3651 * Return: status code
3653 efi_status_t efi_initialize_system_table(void)
3657 /* Allocate configuration table array */
3658 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3659 EFI_MAX_CONFIGURATION_TABLES *
3660 sizeof(struct efi_configuration_table),
3661 (void **)&systab.tables);
3664 * These entries will be set to NULL in ExitBootServices(). To avoid
3665 * relocation in SetVirtualAddressMap(), set them dynamically.
3667 systab.con_in = &efi_con_in;
3668 systab.con_out = &efi_con_out;
3669 systab.std_err = &efi_con_out;
3670 systab.boottime = &efi_boot_services;
3672 /* Set CRC32 field in table headers */
3673 efi_update_table_header_crc32(&systab.hdr);
3674 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3675 efi_update_table_header_crc32(&efi_boot_services.hdr);