1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application boot time services
5 * Copyright (c) 2016 Alexander Graf
10 #include <efi_loader.h>
15 #include <linux/libfdt_env.h>
16 #include <u-boot/crc.h>
19 #include <u-boot/crc.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 /* Task priority level */
25 static efi_uintn_t efi_tpl = TPL_APPLICATION;
27 /* This list contains all the EFI objects our payload has access to */
28 LIST_HEAD(efi_obj_list);
30 /* List of all events */
31 __efi_runtime_data LIST_HEAD(efi_events);
33 /* List of queued events */
34 LIST_HEAD(efi_event_queue);
36 /* Flag to disable timer activity in ExitBootServices() */
37 static bool timers_enabled = true;
39 /* List of all events registered by RegisterProtocolNotify() */
40 LIST_HEAD(efi_register_notify_events);
42 /* Handle of the currently executing image */
43 static efi_handle_t current_image;
47 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
48 * fixed when compiling U-Boot. However, the payload does not know about that
49 * restriction so we need to manually swap its and our view of that register on
50 * EFI callback entry/exit.
52 static volatile gd_t *efi_gd, *app_gd;
55 /* 1 if inside U-Boot code, 0 if inside EFI payload code */
56 static int entry_count = 1;
57 static int nesting_level;
58 /* GUID of the device tree table */
59 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
60 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
61 const efi_guid_t efi_guid_driver_binding_protocol =
62 EFI_DRIVER_BINDING_PROTOCOL_GUID;
64 /* event group ExitBootServices() invoked */
65 const efi_guid_t efi_guid_event_group_exit_boot_services =
66 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
67 /* event group SetVirtualAddressMap() invoked */
68 const efi_guid_t efi_guid_event_group_virtual_address_change =
69 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
70 /* event group memory map changed */
71 const efi_guid_t efi_guid_event_group_memory_map_change =
72 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
73 /* event group boot manager about to boot */
74 const efi_guid_t efi_guid_event_group_ready_to_boot =
75 EFI_EVENT_GROUP_READY_TO_BOOT;
76 /* event group ResetSystem() invoked (before ExitBootServices) */
77 const efi_guid_t efi_guid_event_group_reset_system =
78 EFI_EVENT_GROUP_RESET_SYSTEM;
80 static efi_status_t EFIAPI efi_disconnect_controller(
81 efi_handle_t controller_handle,
82 efi_handle_t driver_image_handle,
83 efi_handle_t child_handle);
85 /* Called on every callback entry */
86 int __efi_entry_check(void)
88 int ret = entry_count++ == 0;
97 /* Called on every callback exit */
98 int __efi_exit_check(void)
100 int ret = --entry_count == 0;
108 * efi_save_gd() - save global data register
110 * On the ARM architecture gd is mapped to a fixed register (r9 or x18).
111 * As this register may be overwritten by an EFI payload we save it here
112 * and restore it on every callback entered.
114 * This function is called after relocation from initr_reloc_global_data().
116 void efi_save_gd(void)
124 * efi_restore_gd() - restore global data register
126 * On the ARM architecture gd is mapped to a fixed register (r9 or x18).
127 * Restore it after returning from the UEFI world to the value saved via
130 void efi_restore_gd(void)
133 /* Only restore if we're already in EFI context */
141 * indent_string() - returns a string for indenting with two spaces per level
142 * @level: indent level
144 * A maximum of ten indent levels is supported. Higher indent levels will be
147 * Return: A string for indenting with two spaces per level is
150 static const char *indent_string(int level)
152 const char *indent = " ";
153 const int max = strlen(indent);
155 level = min(max, level * 2);
156 return &indent[max - level];
159 const char *__efi_nesting(void)
161 return indent_string(nesting_level);
164 const char *__efi_nesting_inc(void)
166 return indent_string(nesting_level++);
169 const char *__efi_nesting_dec(void)
171 return indent_string(--nesting_level);
175 * efi_event_is_queued() - check if an event is queued
178 * Return: true if event is queued
180 static bool efi_event_is_queued(struct efi_event *event)
182 return !!event->queue_link.next;
186 * efi_process_event_queue() - process event queue
188 static void efi_process_event_queue(void)
190 while (!list_empty(&efi_event_queue)) {
191 struct efi_event *event;
194 event = list_first_entry(&efi_event_queue, struct efi_event,
196 if (efi_tpl >= event->notify_tpl)
198 list_del(&event->queue_link);
199 event->queue_link.next = NULL;
200 event->queue_link.prev = NULL;
201 /* Events must be executed at the event's TPL */
203 efi_tpl = event->notify_tpl;
204 EFI_CALL_VOID(event->notify_function(event,
205 event->notify_context));
207 if (event->type == EVT_NOTIFY_SIGNAL)
208 event->is_signaled = 0;
213 * efi_queue_event() - queue an EFI event
214 * @event: event to signal
216 * This function queues the notification function of the event for future
220 static void efi_queue_event(struct efi_event *event)
222 struct efi_event *item;
224 if (!event->notify_function)
227 if (!efi_event_is_queued(event)) {
229 * Events must be notified in order of decreasing task priority
230 * level. Insert the new event accordingly.
232 list_for_each_entry(item, &efi_event_queue, queue_link) {
233 if (item->notify_tpl < event->notify_tpl) {
234 list_add_tail(&event->queue_link,
241 list_add_tail(&event->queue_link, &efi_event_queue);
243 efi_process_event_queue();
247 * is_valid_tpl() - check if the task priority level is valid
249 * @tpl: TPL level to check
250 * Return: status code
252 efi_status_t is_valid_tpl(efi_uintn_t tpl)
255 case TPL_APPLICATION:
261 return EFI_INVALID_PARAMETER;
266 * efi_signal_event() - signal an EFI event
267 * @event: event to signal
269 * This function signals an event. If the event belongs to an event group all
270 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
271 * their notification function is queued.
273 * For the SignalEvent service see efi_signal_event_ext.
275 void efi_signal_event(struct efi_event *event)
277 if (event->is_signaled)
280 struct efi_event *evt;
283 * The signaled state has to set before executing any
284 * notification function
286 list_for_each_entry(evt, &efi_events, link) {
287 if (!evt->group || guidcmp(evt->group, event->group))
289 if (evt->is_signaled)
291 evt->is_signaled = true;
293 list_for_each_entry(evt, &efi_events, link) {
294 if (!evt->group || guidcmp(evt->group, event->group))
296 efi_queue_event(evt);
299 event->is_signaled = true;
300 efi_queue_event(event);
305 * efi_raise_tpl() - raise the task priority level
306 * @new_tpl: new value of the task priority level
308 * This function implements the RaiseTpl service.
310 * See the Unified Extensible Firmware Interface (UEFI) specification for
313 * Return: old value of the task priority level
315 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
317 efi_uintn_t old_tpl = efi_tpl;
319 EFI_ENTRY("0x%zx", new_tpl);
321 if (new_tpl < efi_tpl)
322 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
324 if (efi_tpl > TPL_HIGH_LEVEL)
325 efi_tpl = TPL_HIGH_LEVEL;
327 EFI_EXIT(EFI_SUCCESS);
332 * efi_restore_tpl() - lower the task priority level
333 * @old_tpl: value of the task priority level to be restored
335 * This function implements the RestoreTpl service.
337 * See the Unified Extensible Firmware Interface (UEFI) specification for
340 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
342 EFI_ENTRY("0x%zx", old_tpl);
344 if (old_tpl > efi_tpl)
345 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
347 if (efi_tpl > TPL_HIGH_LEVEL)
348 efi_tpl = TPL_HIGH_LEVEL;
351 * Lowering the TPL may have made queued events eligible for execution.
355 EFI_EXIT(EFI_SUCCESS);
359 * efi_allocate_pages_ext() - allocate memory pages
360 * @type: type of allocation to be performed
361 * @memory_type: usage type of the allocated memory
362 * @pages: number of pages to be allocated
363 * @memory: allocated memory
365 * This function implements the AllocatePages service.
367 * See the Unified Extensible Firmware Interface (UEFI) specification for
370 * Return: status code
372 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
378 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
379 r = efi_allocate_pages(type, memory_type, pages, memory);
384 * efi_free_pages_ext() - Free memory pages.
385 * @memory: start of the memory area to be freed
386 * @pages: number of pages to be freed
388 * This function implements the FreePages service.
390 * See the Unified Extensible Firmware Interface (UEFI) specification for
393 * Return: status code
395 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
400 EFI_ENTRY("%llx, 0x%zx", memory, pages);
401 r = efi_free_pages(memory, pages);
406 * efi_get_memory_map_ext() - get map describing memory usage
407 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
408 * on exit the size of the copied memory map
409 * @memory_map: buffer to which the memory map is written
410 * @map_key: key for the memory map
411 * @descriptor_size: size of an individual memory descriptor
412 * @descriptor_version: version number of the memory descriptor structure
414 * This function implements the GetMemoryMap service.
416 * See the Unified Extensible Firmware Interface (UEFI) specification for
419 * Return: status code
421 static efi_status_t EFIAPI efi_get_memory_map_ext(
422 efi_uintn_t *memory_map_size,
423 struct efi_mem_desc *memory_map,
424 efi_uintn_t *map_key,
425 efi_uintn_t *descriptor_size,
426 uint32_t *descriptor_version)
430 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
431 map_key, descriptor_size, descriptor_version);
432 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
433 descriptor_size, descriptor_version);
438 * efi_allocate_pool_ext() - allocate memory from pool
439 * @pool_type: type of the pool from which memory is to be allocated
440 * @size: number of bytes to be allocated
441 * @buffer: allocated memory
443 * This function implements the AllocatePool service.
445 * See the Unified Extensible Firmware Interface (UEFI) specification for
448 * Return: status code
450 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
456 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
457 r = efi_allocate_pool(pool_type, size, buffer);
462 * efi_free_pool_ext() - free memory from pool
463 * @buffer: start of memory to be freed
465 * This function implements the FreePool service.
467 * See the Unified Extensible Firmware Interface (UEFI) specification for
470 * Return: status code
472 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
476 EFI_ENTRY("%p", buffer);
477 r = efi_free_pool(buffer);
482 * efi_add_handle() - add a new handle to the object list
484 * @handle: handle to be added
486 * The protocols list is initialized. The handle is added to the list of known
489 void efi_add_handle(efi_handle_t handle)
493 INIT_LIST_HEAD(&handle->protocols);
494 list_add_tail(&handle->link, &efi_obj_list);
498 * efi_create_handle() - create handle
499 * @handle: new handle
501 * Return: status code
503 efi_status_t efi_create_handle(efi_handle_t *handle)
505 struct efi_object *obj;
507 obj = calloc(1, sizeof(struct efi_object));
509 return EFI_OUT_OF_RESOURCES;
518 * efi_search_protocol() - find a protocol on a handle.
520 * @protocol_guid: GUID of the protocol
521 * @handler: reference to the protocol
523 * Return: status code
525 efi_status_t efi_search_protocol(const efi_handle_t handle,
526 const efi_guid_t *protocol_guid,
527 struct efi_handler **handler)
529 struct efi_object *efiobj;
530 struct list_head *lhandle;
532 if (!handle || !protocol_guid)
533 return EFI_INVALID_PARAMETER;
534 efiobj = efi_search_obj(handle);
536 return EFI_INVALID_PARAMETER;
537 list_for_each(lhandle, &efiobj->protocols) {
538 struct efi_handler *protocol;
540 protocol = list_entry(lhandle, struct efi_handler, link);
541 if (!guidcmp(protocol->guid, protocol_guid)) {
547 return EFI_NOT_FOUND;
551 * efi_remove_protocol() - delete protocol from a handle
552 * @handle: handle from which the protocol shall be deleted
553 * @protocol: GUID of the protocol to be deleted
554 * @protocol_interface: interface of the protocol implementation
556 * Return: status code
558 efi_status_t efi_remove_protocol(const efi_handle_t handle,
559 const efi_guid_t *protocol,
560 void *protocol_interface)
562 struct efi_handler *handler;
565 ret = efi_search_protocol(handle, protocol, &handler);
566 if (ret != EFI_SUCCESS)
568 if (handler->protocol_interface != protocol_interface)
569 return EFI_NOT_FOUND;
570 list_del(&handler->link);
576 * efi_remove_all_protocols() - delete all protocols from a handle
577 * @handle: handle from which the protocols shall be deleted
579 * Return: status code
581 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
583 struct efi_object *efiobj;
584 struct efi_handler *protocol;
585 struct efi_handler *pos;
587 efiobj = efi_search_obj(handle);
589 return EFI_INVALID_PARAMETER;
590 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
593 ret = efi_remove_protocol(handle, protocol->guid,
594 protocol->protocol_interface);
595 if (ret != EFI_SUCCESS)
602 * efi_delete_handle() - delete handle
604 * @handle: handle to delete
606 void efi_delete_handle(efi_handle_t handle)
610 efi_remove_all_protocols(handle);
611 list_del(&handle->link);
616 * efi_is_event() - check if a pointer is a valid event
617 * @event: pointer to check
619 * Return: status code
621 static efi_status_t efi_is_event(const struct efi_event *event)
623 const struct efi_event *evt;
626 return EFI_INVALID_PARAMETER;
627 list_for_each_entry(evt, &efi_events, link) {
631 return EFI_INVALID_PARAMETER;
635 * efi_create_event() - create an event
637 * @type: type of the event to create
638 * @notify_tpl: task priority level of the event
639 * @notify_function: notification function of the event
640 * @notify_context: pointer passed to the notification function
641 * @group: event group
642 * @event: created event
644 * This function is used inside U-Boot code to create an event.
646 * For the API function implementing the CreateEvent service see
647 * efi_create_event_ext.
649 * Return: status code
651 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
652 void (EFIAPI *notify_function) (
653 struct efi_event *event,
655 void *notify_context, efi_guid_t *group,
656 struct efi_event **event)
658 struct efi_event *evt;
663 return EFI_INVALID_PARAMETER;
668 case EVT_NOTIFY_SIGNAL:
669 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
670 case EVT_NOTIFY_WAIT:
671 case EVT_TIMER | EVT_NOTIFY_WAIT:
672 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
673 pool_type = EFI_BOOT_SERVICES_DATA;
675 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
676 pool_type = EFI_RUNTIME_SERVICES_DATA;
679 return EFI_INVALID_PARAMETER;
682 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
683 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
684 return EFI_INVALID_PARAMETER;
686 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
688 if (ret != EFI_SUCCESS)
690 memset(evt, 0, sizeof(struct efi_event));
692 evt->notify_tpl = notify_tpl;
693 evt->notify_function = notify_function;
694 evt->notify_context = notify_context;
696 /* Disable timers on boot up */
697 evt->trigger_next = -1ULL;
698 list_add_tail(&evt->link, &efi_events);
704 * efi_create_event_ex() - create an event in a group
705 * @type: type of the event to create
706 * @notify_tpl: task priority level of the event
707 * @notify_function: notification function of the event
708 * @notify_context: pointer passed to the notification function
709 * @event: created event
710 * @event_group: event group
712 * This function implements the CreateEventEx service.
714 * See the Unified Extensible Firmware Interface (UEFI) specification for
717 * Return: status code
719 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
720 void (EFIAPI *notify_function) (
721 struct efi_event *event,
723 void *notify_context,
724 efi_guid_t *event_group,
725 struct efi_event **event)
729 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
730 notify_context, event_group);
733 * The allowable input parameters are the same as in CreateEvent()
734 * except for the following two disallowed event types.
737 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
738 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
739 ret = EFI_INVALID_PARAMETER;
743 ret = efi_create_event(type, notify_tpl, notify_function,
744 notify_context, event_group, event);
746 return EFI_EXIT(ret);
750 * efi_create_event_ext() - create an event
751 * @type: type of the event to create
752 * @notify_tpl: task priority level of the event
753 * @notify_function: notification function of the event
754 * @notify_context: pointer passed to the notification function
755 * @event: created event
757 * This function implements the CreateEvent service.
759 * See the Unified Extensible Firmware Interface (UEFI) specification for
762 * Return: status code
764 static efi_status_t EFIAPI efi_create_event_ext(
765 uint32_t type, efi_uintn_t notify_tpl,
766 void (EFIAPI *notify_function) (
767 struct efi_event *event,
769 void *notify_context, struct efi_event **event)
771 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
773 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
774 notify_context, NULL, event));
778 * efi_timer_check() - check if a timer event has occurred
780 * Check if a timer event has occurred or a queued notification function should
783 * Our timers have to work without interrupts, so we check whenever keyboard
784 * input or disk accesses happen if enough time elapsed for them to fire.
786 void efi_timer_check(void)
788 struct efi_event *evt;
789 u64 now = timer_get_us();
791 list_for_each_entry(evt, &efi_events, link) {
794 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
796 switch (evt->trigger_type) {
797 case EFI_TIMER_RELATIVE:
798 evt->trigger_type = EFI_TIMER_STOP;
800 case EFI_TIMER_PERIODIC:
801 evt->trigger_next += evt->trigger_time;
806 evt->is_signaled = false;
807 efi_signal_event(evt);
809 efi_process_event_queue();
814 * efi_set_timer() - set the trigger time for a timer event or stop the event
815 * @event: event for which the timer is set
816 * @type: type of the timer
817 * @trigger_time: trigger period in multiples of 100 ns
819 * This is the function for internal usage in U-Boot. For the API function
820 * implementing the SetTimer service see efi_set_timer_ext.
822 * Return: status code
824 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
825 uint64_t trigger_time)
827 /* Check that the event is valid */
828 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
829 return EFI_INVALID_PARAMETER;
832 * The parameter defines a multiple of 100 ns.
833 * We use multiples of 1000 ns. So divide by 10.
835 do_div(trigger_time, 10);
839 event->trigger_next = -1ULL;
841 case EFI_TIMER_PERIODIC:
842 case EFI_TIMER_RELATIVE:
843 event->trigger_next = timer_get_us() + trigger_time;
846 return EFI_INVALID_PARAMETER;
848 event->trigger_type = type;
849 event->trigger_time = trigger_time;
850 event->is_signaled = false;
855 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
857 * @event: event for which the timer is set
858 * @type: type of the timer
859 * @trigger_time: trigger period in multiples of 100 ns
861 * This function implements the SetTimer service.
863 * See the Unified Extensible Firmware Interface (UEFI) specification for
867 * Return: status code
869 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
870 enum efi_timer_delay type,
871 uint64_t trigger_time)
873 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
874 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
878 * efi_wait_for_event() - wait for events to be signaled
879 * @num_events: number of events to be waited for
880 * @event: events to be waited for
881 * @index: index of the event that was signaled
883 * This function implements the WaitForEvent service.
885 * See the Unified Extensible Firmware Interface (UEFI) specification for
888 * Return: status code
890 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
891 struct efi_event **event,
896 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
898 /* Check parameters */
899 if (!num_events || !event)
900 return EFI_EXIT(EFI_INVALID_PARAMETER);
902 if (efi_tpl != TPL_APPLICATION)
903 return EFI_EXIT(EFI_UNSUPPORTED);
904 for (i = 0; i < num_events; ++i) {
905 if (efi_is_event(event[i]) != EFI_SUCCESS)
906 return EFI_EXIT(EFI_INVALID_PARAMETER);
907 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
908 return EFI_EXIT(EFI_INVALID_PARAMETER);
909 if (!event[i]->is_signaled)
910 efi_queue_event(event[i]);
913 /* Wait for signal */
915 for (i = 0; i < num_events; ++i) {
916 if (event[i]->is_signaled)
919 /* Allow events to occur. */
925 * Reset the signal which is passed to the caller to allow periodic
928 event[i]->is_signaled = false;
932 return EFI_EXIT(EFI_SUCCESS);
936 * efi_signal_event_ext() - signal an EFI event
937 * @event: event to signal
939 * This function implements the SignalEvent service.
941 * See the Unified Extensible Firmware Interface (UEFI) specification for
944 * This functions sets the signaled state of the event and queues the
945 * notification function for execution.
947 * Return: status code
949 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
951 EFI_ENTRY("%p", event);
952 if (efi_is_event(event) != EFI_SUCCESS)
953 return EFI_EXIT(EFI_INVALID_PARAMETER);
954 efi_signal_event(event);
955 return EFI_EXIT(EFI_SUCCESS);
959 * efi_close_event() - close an EFI event
960 * @event: event to close
962 * This function implements the CloseEvent service.
964 * See the Unified Extensible Firmware Interface (UEFI) specification for
967 * Return: status code
969 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
971 struct efi_register_notify_event *item, *next;
973 EFI_ENTRY("%p", event);
974 if (efi_is_event(event) != EFI_SUCCESS)
975 return EFI_EXIT(EFI_INVALID_PARAMETER);
977 /* Remove protocol notify registrations for the event */
978 list_for_each_entry_safe(item, next, &efi_register_notify_events,
980 if (event == item->event) {
981 struct efi_protocol_notification *hitem, *hnext;
983 /* Remove signaled handles */
984 list_for_each_entry_safe(hitem, hnext, &item->handles,
986 list_del(&hitem->link);
989 list_del(&item->link);
993 /* Remove event from queue */
994 if (efi_event_is_queued(event))
995 list_del(&event->queue_link);
997 list_del(&event->link);
998 efi_free_pool(event);
999 return EFI_EXIT(EFI_SUCCESS);
1003 * efi_check_event() - check if an event is signaled
1004 * @event: event to check
1006 * This function implements the CheckEvent service.
1008 * See the Unified Extensible Firmware Interface (UEFI) specification for
1011 * If an event is not signaled yet, the notification function is queued. The
1012 * signaled state is cleared.
1014 * Return: status code
1016 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
1018 EFI_ENTRY("%p", event);
1020 if (efi_is_event(event) != EFI_SUCCESS ||
1021 event->type & EVT_NOTIFY_SIGNAL)
1022 return EFI_EXIT(EFI_INVALID_PARAMETER);
1023 if (!event->is_signaled)
1024 efi_queue_event(event);
1025 if (event->is_signaled) {
1026 event->is_signaled = false;
1027 return EFI_EXIT(EFI_SUCCESS);
1029 return EFI_EXIT(EFI_NOT_READY);
1033 * efi_search_obj() - find the internal EFI object for a handle
1034 * @handle: handle to find
1036 * Return: EFI object
1038 struct efi_object *efi_search_obj(const efi_handle_t handle)
1040 struct efi_object *efiobj;
1045 list_for_each_entry(efiobj, &efi_obj_list, link) {
1046 if (efiobj == handle)
1053 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1055 * @handler: handler of a protocol
1057 * Return: open protocol info entry
1059 static struct efi_open_protocol_info_entry *efi_create_open_info(
1060 struct efi_handler *handler)
1062 struct efi_open_protocol_info_item *item;
1064 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1067 /* Append the item to the open protocol info list. */
1068 list_add_tail(&item->link, &handler->open_infos);
1074 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1075 * @item: open protocol info entry to delete
1077 * Return: status code
1079 static efi_status_t efi_delete_open_info(
1080 struct efi_open_protocol_info_item *item)
1082 list_del(&item->link);
1088 * efi_add_protocol() - install new protocol on a handle
1089 * @handle: handle on which the protocol shall be installed
1090 * @protocol: GUID of the protocol to be installed
1091 * @protocol_interface: interface of the protocol implementation
1093 * Return: status code
1095 efi_status_t efi_add_protocol(const efi_handle_t handle,
1096 const efi_guid_t *protocol,
1097 void *protocol_interface)
1099 struct efi_object *efiobj;
1100 struct efi_handler *handler;
1102 struct efi_register_notify_event *event;
1104 efiobj = efi_search_obj(handle);
1106 return EFI_INVALID_PARAMETER;
1107 ret = efi_search_protocol(handle, protocol, NULL);
1108 if (ret != EFI_NOT_FOUND)
1109 return EFI_INVALID_PARAMETER;
1110 handler = calloc(1, sizeof(struct efi_handler));
1112 return EFI_OUT_OF_RESOURCES;
1113 handler->guid = protocol;
1114 handler->protocol_interface = protocol_interface;
1115 INIT_LIST_HEAD(&handler->open_infos);
1116 list_add_tail(&handler->link, &efiobj->protocols);
1118 /* Notify registered events */
1119 list_for_each_entry(event, &efi_register_notify_events, link) {
1120 if (!guidcmp(protocol, &event->protocol)) {
1121 struct efi_protocol_notification *notif;
1123 notif = calloc(1, sizeof(*notif));
1125 list_del(&handler->link);
1127 return EFI_OUT_OF_RESOURCES;
1129 notif->handle = handle;
1130 list_add_tail(¬if->link, &event->handles);
1131 event->event->is_signaled = false;
1132 efi_signal_event(event->event);
1136 if (!guidcmp(&efi_guid_device_path, protocol))
1137 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1142 * efi_install_protocol_interface() - install protocol interface
1143 * @handle: handle on which the protocol shall be installed
1144 * @protocol: GUID of the protocol to be installed
1145 * @protocol_interface_type: type of the interface to be installed,
1146 * always EFI_NATIVE_INTERFACE
1147 * @protocol_interface: interface of the protocol implementation
1149 * This function implements the InstallProtocolInterface service.
1151 * See the Unified Extensible Firmware Interface (UEFI) specification for
1154 * Return: status code
1156 static efi_status_t EFIAPI efi_install_protocol_interface(
1157 efi_handle_t *handle, const efi_guid_t *protocol,
1158 int protocol_interface_type, void *protocol_interface)
1162 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1163 protocol_interface);
1165 if (!handle || !protocol ||
1166 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1167 r = EFI_INVALID_PARAMETER;
1171 /* Create new handle if requested. */
1173 r = efi_create_handle(handle);
1174 if (r != EFI_SUCCESS)
1176 EFI_PRINT("new handle %p\n", *handle);
1178 EFI_PRINT("handle %p\n", *handle);
1180 /* Add new protocol */
1181 r = efi_add_protocol(*handle, protocol, protocol_interface);
1187 * efi_get_drivers() - get all drivers associated to a controller
1188 * @handle: handle of the controller
1189 * @protocol: protocol GUID (optional)
1190 * @number_of_drivers: number of child controllers
1191 * @driver_handle_buffer: handles of the the drivers
1193 * The allocated buffer has to be freed with free().
1195 * Return: status code
1197 static efi_status_t efi_get_drivers(efi_handle_t handle,
1198 const efi_guid_t *protocol,
1199 efi_uintn_t *number_of_drivers,
1200 efi_handle_t **driver_handle_buffer)
1202 struct efi_handler *handler;
1203 struct efi_open_protocol_info_item *item;
1204 efi_uintn_t count = 0, i;
1207 /* Count all driver associations */
1208 list_for_each_entry(handler, &handle->protocols, link) {
1209 if (protocol && guidcmp(handler->guid, protocol))
1211 list_for_each_entry(item, &handler->open_infos, link) {
1212 if (item->info.attributes &
1213 EFI_OPEN_PROTOCOL_BY_DRIVER)
1217 *number_of_drivers = 0;
1219 *driver_handle_buffer = NULL;
1223 * Create buffer. In case of duplicate driver assignments the buffer
1224 * will be too large. But that does not harm.
1226 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1227 if (!*driver_handle_buffer)
1228 return EFI_OUT_OF_RESOURCES;
1229 /* Collect unique driver handles */
1230 list_for_each_entry(handler, &handle->protocols, link) {
1231 if (protocol && guidcmp(handler->guid, protocol))
1233 list_for_each_entry(item, &handler->open_infos, link) {
1234 if (item->info.attributes &
1235 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1236 /* Check this is a new driver */
1238 for (i = 0; i < *number_of_drivers; ++i) {
1239 if ((*driver_handle_buffer)[i] ==
1240 item->info.agent_handle)
1243 /* Copy handle to buffer */
1245 i = (*number_of_drivers)++;
1246 (*driver_handle_buffer)[i] =
1247 item->info.agent_handle;
1256 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1257 * @handle: handle of the controller
1258 * @protocol: protocol GUID (optional)
1259 * @child_handle: handle of the child to destroy
1261 * This function implements the DisconnectController service.
1263 * See the Unified Extensible Firmware Interface (UEFI) specification for
1266 * Return: status code
1268 static efi_status_t efi_disconnect_all_drivers
1269 (efi_handle_t handle,
1270 const efi_guid_t *protocol,
1271 efi_handle_t child_handle)
1273 efi_uintn_t number_of_drivers;
1274 efi_handle_t *driver_handle_buffer;
1275 efi_status_t r, ret;
1277 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
1278 &driver_handle_buffer);
1279 if (ret != EFI_SUCCESS)
1281 if (!number_of_drivers)
1283 ret = EFI_NOT_FOUND;
1284 while (number_of_drivers) {
1285 r = EFI_CALL(efi_disconnect_controller(
1287 driver_handle_buffer[--number_of_drivers],
1289 if (r == EFI_SUCCESS)
1292 free(driver_handle_buffer);
1297 * efi_uninstall_protocol() - uninstall protocol interface
1299 * @handle: handle from which the protocol shall be removed
1300 * @protocol: GUID of the protocol to be removed
1301 * @protocol_interface: interface to be removed
1303 * This function DOES NOT delete a handle without installed protocol.
1305 * Return: status code
1307 static efi_status_t efi_uninstall_protocol
1308 (efi_handle_t handle, const efi_guid_t *protocol,
1309 void *protocol_interface)
1311 struct efi_object *efiobj;
1312 struct efi_handler *handler;
1313 struct efi_open_protocol_info_item *item;
1314 struct efi_open_protocol_info_item *pos;
1318 efiobj = efi_search_obj(handle);
1320 r = EFI_INVALID_PARAMETER;
1323 /* Find the protocol on the handle */
1324 r = efi_search_protocol(handle, protocol, &handler);
1325 if (r != EFI_SUCCESS)
1327 /* Disconnect controllers */
1328 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1329 /* Close protocol */
1330 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1331 if (item->info.attributes ==
1332 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1333 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1334 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1335 list_del(&item->link);
1337 if (!list_empty(&handler->open_infos)) {
1338 r = EFI_ACCESS_DENIED;
1341 r = efi_remove_protocol(handle, protocol, protocol_interface);
1347 * efi_uninstall_protocol_interface() - uninstall protocol interface
1348 * @handle: handle from which the protocol shall be removed
1349 * @protocol: GUID of the protocol to be removed
1350 * @protocol_interface: interface to be removed
1352 * This function implements the UninstallProtocolInterface service.
1354 * See the Unified Extensible Firmware Interface (UEFI) specification for
1357 * Return: status code
1359 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1360 (efi_handle_t handle, const efi_guid_t *protocol,
1361 void *protocol_interface)
1365 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1367 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1368 if (ret != EFI_SUCCESS)
1371 /* If the last protocol has been removed, delete the handle. */
1372 if (list_empty(&handle->protocols)) {
1373 list_del(&handle->link);
1377 return EFI_EXIT(ret);
1381 * efi_register_protocol_notify() - register an event for notification when a
1382 * protocol is installed.
1383 * @protocol: GUID of the protocol whose installation shall be notified
1384 * @event: event to be signaled upon installation of the protocol
1385 * @registration: key for retrieving the registration information
1387 * This function implements the RegisterProtocolNotify service.
1388 * See the Unified Extensible Firmware Interface (UEFI) specification
1391 * Return: status code
1393 static efi_status_t EFIAPI efi_register_protocol_notify(
1394 const efi_guid_t *protocol,
1395 struct efi_event *event,
1396 void **registration)
1398 struct efi_register_notify_event *item;
1399 efi_status_t ret = EFI_SUCCESS;
1401 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1403 if (!protocol || !event || !registration) {
1404 ret = EFI_INVALID_PARAMETER;
1408 item = calloc(1, sizeof(struct efi_register_notify_event));
1410 ret = EFI_OUT_OF_RESOURCES;
1414 item->event = event;
1415 guidcpy(&item->protocol, protocol);
1416 INIT_LIST_HEAD(&item->handles);
1418 list_add_tail(&item->link, &efi_register_notify_events);
1420 *registration = item;
1422 return EFI_EXIT(ret);
1426 * efi_search() - determine if an EFI handle implements a protocol
1428 * @search_type: selection criterion
1429 * @protocol: GUID of the protocol
1432 * See the documentation of the LocateHandle service in the UEFI specification.
1434 * Return: 0 if the handle implements the protocol
1436 static int efi_search(enum efi_locate_search_type search_type,
1437 const efi_guid_t *protocol, efi_handle_t handle)
1441 switch (search_type) {
1445 ret = efi_search_protocol(handle, protocol, NULL);
1446 return (ret != EFI_SUCCESS);
1448 /* Invalid search type */
1454 * efi_check_register_notify_event() - check if registration key is valid
1456 * Check that a pointer is a valid registration key as returned by
1457 * RegisterProtocolNotify().
1459 * @key: registration key
1460 * Return: valid registration key or NULL
1462 static struct efi_register_notify_event *efi_check_register_notify_event
1465 struct efi_register_notify_event *event;
1467 list_for_each_entry(event, &efi_register_notify_events, link) {
1468 if (event == (struct efi_register_notify_event *)key)
1475 * efi_locate_handle() - locate handles implementing a protocol
1477 * @search_type: selection criterion
1478 * @protocol: GUID of the protocol
1479 * @search_key: registration key
1480 * @buffer_size: size of the buffer to receive the handles in bytes
1481 * @buffer: buffer to receive the relevant handles
1483 * This function is meant for U-Boot internal calls. For the API implementation
1484 * of the LocateHandle service see efi_locate_handle_ext.
1486 * Return: status code
1488 static efi_status_t efi_locate_handle(
1489 enum efi_locate_search_type search_type,
1490 const efi_guid_t *protocol, void *search_key,
1491 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1493 struct efi_object *efiobj;
1494 efi_uintn_t size = 0;
1495 struct efi_register_notify_event *event;
1496 struct efi_protocol_notification *handle = NULL;
1498 /* Check parameters */
1499 switch (search_type) {
1502 case BY_REGISTER_NOTIFY:
1504 return EFI_INVALID_PARAMETER;
1505 /* Check that the registration key is valid */
1506 event = efi_check_register_notify_event(search_key);
1508 return EFI_INVALID_PARAMETER;
1512 return EFI_INVALID_PARAMETER;
1515 return EFI_INVALID_PARAMETER;
1518 /* Count how much space we need */
1519 if (search_type == BY_REGISTER_NOTIFY) {
1520 if (list_empty(&event->handles))
1521 return EFI_NOT_FOUND;
1522 handle = list_first_entry(&event->handles,
1523 struct efi_protocol_notification,
1525 efiobj = handle->handle;
1526 size += sizeof(void *);
1528 list_for_each_entry(efiobj, &efi_obj_list, link) {
1529 if (!efi_search(search_type, protocol, efiobj))
1530 size += sizeof(void *);
1533 return EFI_NOT_FOUND;
1537 return EFI_INVALID_PARAMETER;
1539 if (*buffer_size < size) {
1540 *buffer_size = size;
1541 return EFI_BUFFER_TOO_SMALL;
1544 *buffer_size = size;
1546 /* The buffer size is sufficient but there is no buffer */
1548 return EFI_INVALID_PARAMETER;
1550 /* Then fill the array */
1551 if (search_type == BY_REGISTER_NOTIFY) {
1553 list_del(&handle->link);
1555 list_for_each_entry(efiobj, &efi_obj_list, link) {
1556 if (!efi_search(search_type, protocol, efiobj))
1565 * efi_locate_handle_ext() - locate handles implementing a protocol.
1566 * @search_type: selection criterion
1567 * @protocol: GUID of the protocol
1568 * @search_key: registration key
1569 * @buffer_size: size of the buffer to receive the handles in bytes
1570 * @buffer: buffer to receive the relevant handles
1572 * This function implements the LocateHandle service.
1574 * See the Unified Extensible Firmware Interface (UEFI) specification for
1577 * Return: 0 if the handle implements the protocol
1579 static efi_status_t EFIAPI efi_locate_handle_ext(
1580 enum efi_locate_search_type search_type,
1581 const efi_guid_t *protocol, void *search_key,
1582 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1584 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1585 buffer_size, buffer);
1587 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1588 buffer_size, buffer));
1592 * efi_remove_configuration_table() - collapses configuration table entries,
1595 * @i: index of the table entry to be removed
1597 static void efi_remove_configuration_table(int i)
1599 struct efi_configuration_table *this = &systab.tables[i];
1600 struct efi_configuration_table *next = &systab.tables[i + 1];
1601 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1603 memmove(this, next, (ulong)end - (ulong)next);
1608 * efi_install_configuration_table() - adds, updates, or removes a
1609 * configuration table
1610 * @guid: GUID of the installed table
1611 * @table: table to be installed
1613 * This function is used for internal calls. For the API implementation of the
1614 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1616 * Return: status code
1618 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1621 struct efi_event *evt;
1625 return EFI_INVALID_PARAMETER;
1627 /* Check for GUID override */
1628 for (i = 0; i < systab.nr_tables; i++) {
1629 if (!guidcmp(guid, &systab.tables[i].guid)) {
1631 systab.tables[i].table = table;
1633 efi_remove_configuration_table(i);
1639 return EFI_NOT_FOUND;
1641 /* No override, check for overflow */
1642 if (i >= EFI_MAX_CONFIGURATION_TABLES)
1643 return EFI_OUT_OF_RESOURCES;
1645 /* Add a new entry */
1646 guidcpy(&systab.tables[i].guid, guid);
1647 systab.tables[i].table = table;
1648 systab.nr_tables = i + 1;
1651 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1652 efi_update_table_header_crc32(&systab.hdr);
1654 /* Notify that the configuration table was changed */
1655 list_for_each_entry(evt, &efi_events, link) {
1656 if (evt->group && !guidcmp(evt->group, guid)) {
1657 efi_signal_event(evt);
1666 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1667 * configuration table.
1668 * @guid: GUID of the installed table
1669 * @table: table to be installed
1671 * This function implements the InstallConfigurationTable service.
1673 * See the Unified Extensible Firmware Interface (UEFI) specification for
1676 * Return: status code
1678 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1681 EFI_ENTRY("%pUl, %p", guid, table);
1682 return EFI_EXIT(efi_install_configuration_table(guid, table));
1686 * efi_setup_loaded_image() - initialize a loaded image
1688 * Initialize a loaded_image_info and loaded_image_info object with correct
1689 * protocols, boot-device, etc.
1691 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
1694 * @device_path: device path of the loaded image
1695 * @file_path: file path of the loaded image
1696 * @handle_ptr: handle of the loaded image
1697 * @info_ptr: loaded image protocol
1698 * Return: status code
1700 efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1701 struct efi_device_path *file_path,
1702 struct efi_loaded_image_obj **handle_ptr,
1703 struct efi_loaded_image **info_ptr)
1706 struct efi_loaded_image *info = NULL;
1707 struct efi_loaded_image_obj *obj = NULL;
1708 struct efi_device_path *dp;
1710 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1714 info = calloc(1, sizeof(*info));
1716 return EFI_OUT_OF_RESOURCES;
1717 obj = calloc(1, sizeof(*obj));
1720 return EFI_OUT_OF_RESOURCES;
1722 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
1724 /* Add internal object to object list */
1725 efi_add_handle(&obj->header);
1727 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1728 info->file_path = file_path;
1729 info->system_table = &systab;
1732 info->device_handle = efi_dp_find_obj(device_path, NULL);
1734 dp = efi_dp_append(device_path, file_path);
1736 ret = EFI_OUT_OF_RESOURCES;
1742 ret = efi_add_protocol(&obj->header,
1743 &efi_guid_loaded_image_device_path, dp);
1744 if (ret != EFI_SUCCESS)
1748 * When asking for the loaded_image interface, just
1749 * return handle which points to loaded_image_info
1751 ret = efi_add_protocol(&obj->header,
1752 &efi_guid_loaded_image, info);
1753 if (ret != EFI_SUCCESS)
1761 printf("ERROR: Failure to install protocols for loaded image\n");
1762 efi_delete_handle(&obj->header);
1768 * efi_load_image_from_path() - load an image using a file path
1770 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1771 * callers obligation to update the memory type as needed.
1773 * @file_path: the path of the image to load
1774 * @buffer: buffer containing the loaded image
1775 * @size: size of the loaded image
1776 * Return: status code
1779 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1780 void **buffer, efi_uintn_t *size)
1782 struct efi_file_info *info = NULL;
1783 struct efi_file_handle *f;
1784 static efi_status_t ret;
1788 /* In case of failure nothing is returned */
1793 f = efi_file_from_path(file_path);
1795 return EFI_NOT_FOUND;
1799 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1801 if (ret != EFI_BUFFER_TOO_SMALL) {
1802 ret = EFI_DEVICE_ERROR;
1807 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1809 if (ret != EFI_SUCCESS)
1813 * When reading the file we do not yet know if it contains an
1814 * application, a boottime driver, or a runtime driver. So here we
1815 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1816 * update the reservation according to the image type.
1818 bs = info->file_size;
1819 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1820 EFI_BOOT_SERVICES_DATA,
1821 efi_size_in_pages(bs), &addr);
1822 if (ret != EFI_SUCCESS) {
1823 ret = EFI_OUT_OF_RESOURCES;
1828 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1829 if (ret != EFI_SUCCESS)
1830 efi_free_pages(addr, efi_size_in_pages(bs));
1831 *buffer = (void *)(uintptr_t)addr;
1834 EFI_CALL(f->close(f));
1840 * efi_load_image() - load an EFI image into memory
1841 * @boot_policy: true for request originating from the boot manager
1842 * @parent_image: the caller's image handle
1843 * @file_path: the path of the image to load
1844 * @source_buffer: memory location from which the image is installed
1845 * @source_size: size of the memory area from which the image is installed
1846 * @image_handle: handle for the newly installed image
1848 * This function implements the LoadImage service.
1850 * See the Unified Extensible Firmware Interface (UEFI) specification
1853 * Return: status code
1855 efi_status_t EFIAPI efi_load_image(bool boot_policy,
1856 efi_handle_t parent_image,
1857 struct efi_device_path *file_path,
1858 void *source_buffer,
1859 efi_uintn_t source_size,
1860 efi_handle_t *image_handle)
1862 struct efi_device_path *dp, *fp;
1863 struct efi_loaded_image *info = NULL;
1864 struct efi_loaded_image_obj **image_obj =
1865 (struct efi_loaded_image_obj **)image_handle;
1869 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1870 file_path, source_buffer, source_size, image_handle);
1872 if (!image_handle || (!source_buffer && !file_path) ||
1873 !efi_search_obj(parent_image) ||
1874 /* The parent image handle must refer to a loaded image */
1875 !parent_image->type) {
1876 ret = EFI_INVALID_PARAMETER;
1880 if (!source_buffer) {
1881 ret = efi_load_image_from_path(file_path, &dest_buffer,
1883 if (ret != EFI_SUCCESS)
1887 ret = EFI_LOAD_ERROR;
1890 dest_buffer = source_buffer;
1892 /* split file_path which contains both the device and file parts */
1893 efi_dp_split_file_path(file_path, &dp, &fp);
1894 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1895 if (ret == EFI_SUCCESS)
1896 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
1898 /* Release buffer to which file was loaded */
1899 efi_free_pages((uintptr_t)dest_buffer,
1900 efi_size_in_pages(source_size));
1901 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
1902 info->system_table = &systab;
1903 info->parent_handle = parent_image;
1905 /* The image is invalid. Release all associated resources. */
1906 efi_delete_handle(*image_handle);
1907 *image_handle = NULL;
1911 return EFI_EXIT(ret);
1915 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1917 static void efi_exit_caches(void)
1919 #if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
1921 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
1922 * caches are enabled.
1925 * According to the UEFI spec caches that can be managed via CP15
1926 * operations should be enabled. Caches requiring platform information
1927 * to manage should be disabled. This should not happen in
1928 * ExitBootServices() but before invoking any UEFI binary is invoked.
1930 * We want to keep the current workaround while GRUB prior to version
1931 * 2.04 is still in use.
1933 cleanup_before_linux();
1938 * efi_exit_boot_services() - stop all boot services
1939 * @image_handle: handle of the loaded image
1940 * @map_key: key of the memory map
1942 * This function implements the ExitBootServices service.
1944 * See the Unified Extensible Firmware Interface (UEFI) specification
1947 * All timer events are disabled. For exit boot services events the
1948 * notification function is called. The boot services are disabled in the
1951 * Return: status code
1953 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1954 efi_uintn_t map_key)
1956 struct efi_event *evt, *next_event;
1957 efi_status_t ret = EFI_SUCCESS;
1959 EFI_ENTRY("%p, %zx", image_handle, map_key);
1961 /* Check that the caller has read the current memory map */
1962 if (map_key != efi_memory_map_key) {
1963 ret = EFI_INVALID_PARAMETER;
1967 /* Check if ExitBootServices has already been called */
1968 if (!systab.boottime)
1971 /* Stop all timer related activities */
1972 timers_enabled = false;
1974 /* Add related events to the event group */
1975 list_for_each_entry(evt, &efi_events, link) {
1976 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1977 evt->group = &efi_guid_event_group_exit_boot_services;
1979 /* Notify that ExitBootServices is invoked. */
1980 list_for_each_entry(evt, &efi_events, link) {
1982 !guidcmp(evt->group,
1983 &efi_guid_event_group_exit_boot_services)) {
1984 efi_signal_event(evt);
1989 /* Make sure that notification functions are not called anymore */
1990 efi_tpl = TPL_HIGH_LEVEL;
1992 /* Notify variable services */
1993 efi_variables_boot_exit_notify();
1995 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
1996 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
1997 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
1998 list_del(&evt->link);
2001 board_quiesce_devices();
2003 /* Patch out unsupported runtime function */
2004 efi_runtime_detach();
2006 /* Fix up caches for EFI payloads if necessary */
2009 /* This stops all lingering devices */
2010 bootm_disable_interrupts();
2012 /* Disable boot time services */
2013 systab.con_in_handle = NULL;
2014 systab.con_in = NULL;
2015 systab.con_out_handle = NULL;
2016 systab.con_out = NULL;
2017 systab.stderr_handle = NULL;
2018 systab.std_err = NULL;
2019 systab.boottime = NULL;
2021 /* Recalculate CRC32 */
2022 efi_update_table_header_crc32(&systab.hdr);
2024 /* Give the payload some time to boot */
2025 efi_set_watchdog(0);
2028 return EFI_EXIT(ret);
2032 * efi_get_next_monotonic_count() - get next value of the counter
2033 * @count: returned value of the counter
2035 * This function implements the NextMonotonicCount service.
2037 * See the Unified Extensible Firmware Interface (UEFI) specification for
2040 * Return: status code
2042 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2044 static uint64_t mono;
2047 EFI_ENTRY("%p", count);
2049 ret = EFI_INVALID_PARAMETER;
2055 return EFI_EXIT(ret);
2059 * efi_stall() - sleep
2060 * @microseconds: period to sleep in microseconds
2062 * This function implements the Stall service.
2064 * See the Unified Extensible Firmware Interface (UEFI) specification for
2067 * Return: status code
2069 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2073 EFI_ENTRY("%ld", microseconds);
2075 end_tick = get_ticks() + usec_to_tick(microseconds);
2076 while (get_ticks() < end_tick)
2079 return EFI_EXIT(EFI_SUCCESS);
2083 * efi_set_watchdog_timer() - reset the watchdog timer
2084 * @timeout: seconds before reset by watchdog
2085 * @watchdog_code: code to be logged when resetting
2086 * @data_size: size of buffer in bytes
2087 * @watchdog_data: buffer with data describing the reset reason
2089 * This function implements the SetWatchdogTimer service.
2091 * See the Unified Extensible Firmware Interface (UEFI) specification for
2094 * Return: status code
2096 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2097 uint64_t watchdog_code,
2098 unsigned long data_size,
2099 uint16_t *watchdog_data)
2101 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
2102 data_size, watchdog_data);
2103 return EFI_EXIT(efi_set_watchdog(timeout));
2107 * efi_close_protocol() - close a protocol
2108 * @handle: handle on which the protocol shall be closed
2109 * @protocol: GUID of the protocol to close
2110 * @agent_handle: handle of the driver
2111 * @controller_handle: handle of the controller
2113 * This function implements the CloseProtocol service.
2115 * See the Unified Extensible Firmware Interface (UEFI) specification for
2118 * Return: status code
2120 efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2121 const efi_guid_t *protocol,
2122 efi_handle_t agent_handle,
2123 efi_handle_t controller_handle)
2125 struct efi_handler *handler;
2126 struct efi_open_protocol_info_item *item;
2127 struct efi_open_protocol_info_item *pos;
2130 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
2133 if (!efi_search_obj(agent_handle) ||
2134 (controller_handle && !efi_search_obj(controller_handle))) {
2135 r = EFI_INVALID_PARAMETER;
2138 r = efi_search_protocol(handle, protocol, &handler);
2139 if (r != EFI_SUCCESS)
2143 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2144 if (item->info.agent_handle == agent_handle &&
2145 item->info.controller_handle == controller_handle) {
2146 efi_delete_open_info(item);
2155 * efi_open_protocol_information() - provide information about then open status
2156 * of a protocol on a handle
2157 * @handle: handle for which the information shall be retrieved
2158 * @protocol: GUID of the protocol
2159 * @entry_buffer: buffer to receive the open protocol information
2160 * @entry_count: number of entries available in the buffer
2162 * This function implements the OpenProtocolInformation service.
2164 * See the Unified Extensible Firmware Interface (UEFI) specification for
2167 * Return: status code
2169 static efi_status_t EFIAPI efi_open_protocol_information(
2170 efi_handle_t handle, const efi_guid_t *protocol,
2171 struct efi_open_protocol_info_entry **entry_buffer,
2172 efi_uintn_t *entry_count)
2174 unsigned long buffer_size;
2175 unsigned long count;
2176 struct efi_handler *handler;
2177 struct efi_open_protocol_info_item *item;
2180 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2183 /* Check parameters */
2184 if (!entry_buffer) {
2185 r = EFI_INVALID_PARAMETER;
2188 r = efi_search_protocol(handle, protocol, &handler);
2189 if (r != EFI_SUCCESS)
2194 list_for_each_entry(item, &handler->open_infos, link) {
2195 if (item->info.open_count)
2198 *entry_count = count;
2199 *entry_buffer = NULL;
2206 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2207 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2208 (void **)entry_buffer);
2209 if (r != EFI_SUCCESS)
2211 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2212 if (item->info.open_count)
2213 (*entry_buffer)[--count] = item->info;
2220 * efi_protocols_per_handle() - get protocols installed on a handle
2221 * @handle: handle for which the information is retrieved
2222 * @protocol_buffer: buffer with protocol GUIDs
2223 * @protocol_buffer_count: number of entries in the buffer
2225 * This function implements the ProtocolsPerHandleService.
2227 * See the Unified Extensible Firmware Interface (UEFI) specification for
2230 * Return: status code
2232 static efi_status_t EFIAPI efi_protocols_per_handle(
2233 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2234 efi_uintn_t *protocol_buffer_count)
2236 unsigned long buffer_size;
2237 struct efi_object *efiobj;
2238 struct list_head *protocol_handle;
2241 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2242 protocol_buffer_count);
2244 if (!handle || !protocol_buffer || !protocol_buffer_count)
2245 return EFI_EXIT(EFI_INVALID_PARAMETER);
2247 *protocol_buffer = NULL;
2248 *protocol_buffer_count = 0;
2250 efiobj = efi_search_obj(handle);
2252 return EFI_EXIT(EFI_INVALID_PARAMETER);
2254 /* Count protocols */
2255 list_for_each(protocol_handle, &efiobj->protocols) {
2256 ++*protocol_buffer_count;
2260 if (*protocol_buffer_count) {
2263 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2264 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2265 (void **)protocol_buffer);
2266 if (r != EFI_SUCCESS)
2268 list_for_each(protocol_handle, &efiobj->protocols) {
2269 struct efi_handler *protocol;
2271 protocol = list_entry(protocol_handle,
2272 struct efi_handler, link);
2273 (*protocol_buffer)[j] = (void *)protocol->guid;
2278 return EFI_EXIT(EFI_SUCCESS);
2282 * efi_locate_handle_buffer() - locate handles implementing a protocol
2283 * @search_type: selection criterion
2284 * @protocol: GUID of the protocol
2285 * @search_key: registration key
2286 * @no_handles: number of returned handles
2287 * @buffer: buffer with the returned handles
2289 * This function implements the LocateHandleBuffer service.
2291 * See the Unified Extensible Firmware Interface (UEFI) specification for
2294 * Return: status code
2296 efi_status_t EFIAPI efi_locate_handle_buffer(
2297 enum efi_locate_search_type search_type,
2298 const efi_guid_t *protocol, void *search_key,
2299 efi_uintn_t *no_handles, efi_handle_t **buffer)
2302 efi_uintn_t buffer_size = 0;
2304 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2305 no_handles, buffer);
2307 if (!no_handles || !buffer) {
2308 r = EFI_INVALID_PARAMETER;
2313 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2315 if (r != EFI_BUFFER_TOO_SMALL)
2317 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2319 if (r != EFI_SUCCESS)
2321 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2323 if (r == EFI_SUCCESS)
2324 *no_handles = buffer_size / sizeof(efi_handle_t);
2330 * efi_locate_protocol() - find an interface implementing a protocol
2331 * @protocol: GUID of the protocol
2332 * @registration: registration key passed to the notification function
2333 * @protocol_interface: interface implementing the protocol
2335 * This function implements the LocateProtocol service.
2337 * See the Unified Extensible Firmware Interface (UEFI) specification for
2340 * Return: status code
2342 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2344 void **protocol_interface)
2346 struct efi_handler *handler;
2348 struct efi_object *efiobj;
2350 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2353 * The UEFI spec explicitly requires a protocol even if a registration
2354 * key is provided. This differs from the logic in LocateHandle().
2356 if (!protocol || !protocol_interface)
2357 return EFI_EXIT(EFI_INVALID_PARAMETER);
2360 struct efi_register_notify_event *event;
2361 struct efi_protocol_notification *handle;
2363 event = efi_check_register_notify_event(registration);
2365 return EFI_EXIT(EFI_INVALID_PARAMETER);
2367 * The UEFI spec requires to return EFI_NOT_FOUND if no
2368 * protocol instance matches protocol and registration.
2369 * So let's do the same for a mismatch between protocol and
2372 if (guidcmp(&event->protocol, protocol))
2374 if (list_empty(&event->handles))
2376 handle = list_first_entry(&event->handles,
2377 struct efi_protocol_notification,
2379 efiobj = handle->handle;
2380 list_del(&handle->link);
2382 ret = efi_search_protocol(efiobj, protocol, &handler);
2383 if (ret == EFI_SUCCESS)
2386 list_for_each_entry(efiobj, &efi_obj_list, link) {
2387 ret = efi_search_protocol(efiobj, protocol, &handler);
2388 if (ret == EFI_SUCCESS)
2393 *protocol_interface = NULL;
2394 return EFI_EXIT(EFI_NOT_FOUND);
2396 *protocol_interface = handler->protocol_interface;
2397 return EFI_EXIT(EFI_SUCCESS);
2401 * efi_locate_device_path() - Get the device path and handle of an device
2402 * implementing a protocol
2403 * @protocol: GUID of the protocol
2404 * @device_path: device path
2405 * @device: handle of the device
2407 * This function implements the LocateDevicePath service.
2409 * See the Unified Extensible Firmware Interface (UEFI) specification for
2412 * Return: status code
2414 static efi_status_t EFIAPI efi_locate_device_path(
2415 const efi_guid_t *protocol,
2416 struct efi_device_path **device_path,
2417 efi_handle_t *device)
2419 struct efi_device_path *dp;
2421 struct efi_handler *handler;
2422 efi_handle_t *handles;
2424 size_t len_best = 0;
2425 efi_uintn_t no_handles;
2429 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2431 if (!protocol || !device_path || !*device_path) {
2432 ret = EFI_INVALID_PARAMETER;
2436 /* Find end of device path */
2437 len = efi_dp_instance_size(*device_path);
2439 /* Get all handles implementing the protocol */
2440 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2441 &no_handles, &handles));
2442 if (ret != EFI_SUCCESS)
2445 for (i = 0; i < no_handles; ++i) {
2446 /* Find the device path protocol */
2447 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2449 if (ret != EFI_SUCCESS)
2451 dp = (struct efi_device_path *)handler->protocol_interface;
2452 len_dp = efi_dp_instance_size(dp);
2454 * This handle can only be a better fit
2455 * if its device path length is longer than the best fit and
2456 * if its device path length is shorter of equal the searched
2459 if (len_dp <= len_best || len_dp > len)
2461 /* Check if dp is a subpath of device_path */
2462 if (memcmp(*device_path, dp, len_dp))
2465 ret = EFI_INVALID_PARAMETER;
2468 *device = handles[i];
2472 remainder = (u8 *)*device_path + len_best;
2473 *device_path = (struct efi_device_path *)remainder;
2476 ret = EFI_NOT_FOUND;
2479 return EFI_EXIT(ret);
2483 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2485 * @handle: handle on which the protocol interfaces shall be installed
2486 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2489 * This function implements the MultipleProtocolInterfaces service.
2491 * See the Unified Extensible Firmware Interface (UEFI) specification for
2494 * Return: status code
2496 efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2497 (efi_handle_t *handle, ...)
2499 EFI_ENTRY("%p", handle);
2502 const efi_guid_t *protocol;
2503 void *protocol_interface;
2504 efi_handle_t old_handle;
2505 efi_status_t r = EFI_SUCCESS;
2509 return EFI_EXIT(EFI_INVALID_PARAMETER);
2511 efi_va_start(argptr, handle);
2513 protocol = efi_va_arg(argptr, efi_guid_t*);
2516 protocol_interface = efi_va_arg(argptr, void*);
2517 /* Check that a device path has not been installed before */
2518 if (!guidcmp(protocol, &efi_guid_device_path)) {
2519 struct efi_device_path *dp = protocol_interface;
2521 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2523 if (r == EFI_SUCCESS &&
2524 dp->type == DEVICE_PATH_TYPE_END) {
2525 EFI_PRINT("Path %pD already installed\n",
2526 protocol_interface);
2527 r = EFI_ALREADY_STARTED;
2531 r = EFI_CALL(efi_install_protocol_interface(
2533 EFI_NATIVE_INTERFACE,
2534 protocol_interface));
2535 if (r != EFI_SUCCESS)
2540 if (r == EFI_SUCCESS)
2543 /* If an error occurred undo all changes. */
2544 efi_va_start(argptr, handle);
2546 protocol = efi_va_arg(argptr, efi_guid_t*);
2547 protocol_interface = efi_va_arg(argptr, void*);
2548 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2549 protocol_interface));
2557 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2559 * @handle: handle from which the protocol interfaces shall be removed
2560 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2563 * This function implements the UninstallMultipleProtocolInterfaces service.
2565 * See the Unified Extensible Firmware Interface (UEFI) specification for
2568 * Return: status code
2570 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2571 efi_handle_t handle, ...)
2573 EFI_ENTRY("%p", handle);
2576 const efi_guid_t *protocol;
2577 void *protocol_interface;
2578 efi_status_t r = EFI_SUCCESS;
2582 return EFI_EXIT(EFI_INVALID_PARAMETER);
2584 efi_va_start(argptr, handle);
2586 protocol = efi_va_arg(argptr, efi_guid_t*);
2589 protocol_interface = efi_va_arg(argptr, void*);
2590 r = efi_uninstall_protocol(handle, protocol,
2591 protocol_interface);
2592 if (r != EFI_SUCCESS)
2597 if (r == EFI_SUCCESS) {
2598 /* If the last protocol has been removed, delete the handle. */
2599 if (list_empty(&handle->protocols)) {
2600 list_del(&handle->link);
2606 /* If an error occurred undo all changes. */
2607 efi_va_start(argptr, handle);
2609 protocol = efi_va_arg(argptr, efi_guid_t*);
2610 protocol_interface = efi_va_arg(argptr, void*);
2611 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2612 EFI_NATIVE_INTERFACE,
2613 protocol_interface));
2617 /* In case of an error always return EFI_INVALID_PARAMETER */
2618 return EFI_EXIT(EFI_INVALID_PARAMETER);
2622 * efi_calculate_crc32() - calculate cyclic redundancy code
2623 * @data: buffer with data
2624 * @data_size: size of buffer in bytes
2625 * @crc32_p: cyclic redundancy code
2627 * This function implements the CalculateCrc32 service.
2629 * See the Unified Extensible Firmware Interface (UEFI) specification for
2632 * Return: status code
2634 static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2635 efi_uintn_t data_size,
2638 efi_status_t ret = EFI_SUCCESS;
2640 EFI_ENTRY("%p, %zu", data, data_size);
2641 if (!data || !data_size || !crc32_p) {
2642 ret = EFI_INVALID_PARAMETER;
2645 *crc32_p = crc32(0, data, data_size);
2647 return EFI_EXIT(ret);
2651 * efi_copy_mem() - copy memory
2652 * @destination: destination of the copy operation
2653 * @source: source of the copy operation
2654 * @length: number of bytes to copy
2656 * This function implements the CopyMem service.
2658 * See the Unified Extensible Firmware Interface (UEFI) specification for
2661 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2664 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2665 memmove(destination, source, length);
2666 EFI_EXIT(EFI_SUCCESS);
2670 * efi_set_mem() - Fill memory with a byte value.
2671 * @buffer: buffer to fill
2672 * @size: size of buffer in bytes
2673 * @value: byte to copy to the buffer
2675 * This function implements the SetMem service.
2677 * See the Unified Extensible Firmware Interface (UEFI) specification for
2680 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2682 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2683 memset(buffer, value, size);
2684 EFI_EXIT(EFI_SUCCESS);
2688 * efi_protocol_open() - open protocol interface on a handle
2689 * @handler: handler of a protocol
2690 * @protocol_interface: interface implementing the protocol
2691 * @agent_handle: handle of the driver
2692 * @controller_handle: handle of the controller
2693 * @attributes: attributes indicating how to open the protocol
2695 * Return: status code
2697 static efi_status_t efi_protocol_open(
2698 struct efi_handler *handler,
2699 void **protocol_interface, void *agent_handle,
2700 void *controller_handle, uint32_t attributes)
2702 struct efi_open_protocol_info_item *item;
2703 struct efi_open_protocol_info_entry *match = NULL;
2704 bool opened_by_driver = false;
2705 bool opened_exclusive = false;
2707 /* If there is no agent, only return the interface */
2711 /* For TEST_PROTOCOL ignore interface attribute */
2712 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2713 *protocol_interface = NULL;
2716 * Check if the protocol is already opened by a driver with the same
2717 * attributes or opened exclusively
2719 list_for_each_entry(item, &handler->open_infos, link) {
2720 if (item->info.agent_handle == agent_handle) {
2721 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2722 (item->info.attributes == attributes))
2723 return EFI_ALREADY_STARTED;
2725 if (item->info.attributes &
2726 EFI_OPEN_PROTOCOL_BY_DRIVER)
2727 opened_by_driver = true;
2729 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2730 opened_exclusive = true;
2733 /* Only one controller can open the protocol exclusively */
2734 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2735 if (opened_exclusive)
2736 return EFI_ACCESS_DENIED;
2737 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2738 if (opened_exclusive || opened_by_driver)
2739 return EFI_ACCESS_DENIED;
2742 /* Prepare exclusive opening */
2743 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2744 /* Try to disconnect controllers */
2746 opened_by_driver = false;
2747 list_for_each_entry(item, &handler->open_infos, link) {
2750 if (item->info.attributes ==
2751 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2752 ret = EFI_CALL(efi_disconnect_controller(
2753 item->info.controller_handle,
2754 item->info.agent_handle,
2756 if (ret == EFI_SUCCESS)
2758 * Child controllers may have been
2759 * removed from the open_infos list. So
2760 * let's restart the loop.
2762 goto disconnect_next;
2764 opened_by_driver = true;
2767 /* Only one driver can be connected */
2768 if (opened_by_driver)
2769 return EFI_ACCESS_DENIED;
2772 /* Find existing entry */
2773 list_for_each_entry(item, &handler->open_infos, link) {
2774 if (item->info.agent_handle == agent_handle &&
2775 item->info.controller_handle == controller_handle &&
2776 item->info.attributes == attributes)
2777 match = &item->info;
2779 /* None found, create one */
2781 match = efi_create_open_info(handler);
2783 return EFI_OUT_OF_RESOURCES;
2786 match->agent_handle = agent_handle;
2787 match->controller_handle = controller_handle;
2788 match->attributes = attributes;
2789 match->open_count++;
2792 /* For TEST_PROTOCOL ignore interface attribute. */
2793 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2794 *protocol_interface = handler->protocol_interface;
2800 * efi_open_protocol() - open protocol interface on a handle
2801 * @handle: handle on which the protocol shall be opened
2802 * @protocol: GUID of the protocol
2803 * @protocol_interface: interface implementing the protocol
2804 * @agent_handle: handle of the driver
2805 * @controller_handle: handle of the controller
2806 * @attributes: attributes indicating how to open the protocol
2808 * This function implements the OpenProtocol interface.
2810 * See the Unified Extensible Firmware Interface (UEFI) specification for
2813 * Return: status code
2815 static efi_status_t EFIAPI efi_open_protocol
2816 (efi_handle_t handle, const efi_guid_t *protocol,
2817 void **protocol_interface, efi_handle_t agent_handle,
2818 efi_handle_t controller_handle, uint32_t attributes)
2820 struct efi_handler *handler;
2821 efi_status_t r = EFI_INVALID_PARAMETER;
2823 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2824 protocol_interface, agent_handle, controller_handle,
2827 if (!handle || !protocol ||
2828 (!protocol_interface && attributes !=
2829 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2833 switch (attributes) {
2834 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2835 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2836 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2838 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2839 if (controller_handle == handle)
2842 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2843 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2844 /* Check that the controller handle is valid */
2845 if (!efi_search_obj(controller_handle))
2848 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2849 /* Check that the agent handle is valid */
2850 if (!efi_search_obj(agent_handle))
2857 r = efi_search_protocol(handle, protocol, &handler);
2862 r = EFI_UNSUPPORTED;
2868 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2869 controller_handle, attributes);
2875 * efi_start_image() - call the entry point of an image
2876 * @image_handle: handle of the image
2877 * @exit_data_size: size of the buffer
2878 * @exit_data: buffer to receive the exit data of the called image
2880 * This function implements the StartImage service.
2882 * See the Unified Extensible Firmware Interface (UEFI) specification for
2885 * Return: status code
2887 efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2888 efi_uintn_t *exit_data_size,
2891 struct efi_loaded_image_obj *image_obj =
2892 (struct efi_loaded_image_obj *)image_handle;
2895 efi_handle_t parent_image = current_image;
2897 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2899 if (!efi_search_obj(image_handle))
2900 return EFI_EXIT(EFI_INVALID_PARAMETER);
2902 /* Check parameters */
2903 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2904 return EFI_EXIT(EFI_INVALID_PARAMETER);
2906 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
2907 return EFI_EXIT(EFI_SECURITY_VIOLATION);
2909 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2911 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2912 if (ret != EFI_SUCCESS)
2913 return EFI_EXIT(EFI_INVALID_PARAMETER);
2915 image_obj->exit_data_size = exit_data_size;
2916 image_obj->exit_data = exit_data;
2918 /* call the image! */
2919 if (setjmp(&image_obj->exit_jmp)) {
2921 * We called the entry point of the child image with EFI_CALL
2922 * in the lines below. The child image called the Exit() boot
2923 * service efi_exit() which executed the long jump that brought
2924 * us to the current line. This implies that the second half
2925 * of the EFI_CALL macro has not been executed.
2929 * efi_exit() called efi_restore_gd(). We have to undo this
2930 * otherwise __efi_entry_check() will put the wrong value into
2936 * To get ready to call EFI_EXIT below we have to execute the
2937 * missed out steps of EFI_CALL.
2939 assert(__efi_entry_check());
2940 EFI_PRINT("%lu returned by started image\n",
2941 (unsigned long)((uintptr_t)image_obj->exit_status &
2943 current_image = parent_image;
2944 return EFI_EXIT(image_obj->exit_status);
2947 current_image = image_handle;
2948 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
2949 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
2950 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2953 * Control is returned from a started UEFI image either by calling
2954 * Exit() (where exit data can be provided) or by simply returning from
2955 * the entry point. In the latter case call Exit() on behalf of the
2958 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2962 * efi_delete_image() - delete loaded image from memory)
2964 * @image_obj: handle of the loaded image
2965 * @loaded_image_protocol: loaded image protocol
2967 static efi_status_t efi_delete_image
2968 (struct efi_loaded_image_obj *image_obj,
2969 struct efi_loaded_image *loaded_image_protocol)
2971 struct efi_object *efiobj;
2972 efi_status_t r, ret = EFI_SUCCESS;
2975 list_for_each_entry(efiobj, &efi_obj_list, link) {
2976 struct efi_handler *protocol;
2978 list_for_each_entry(protocol, &efiobj->protocols, link) {
2979 struct efi_open_protocol_info_item *info;
2981 list_for_each_entry(info, &protocol->open_infos, link) {
2982 if (info->info.agent_handle !=
2983 (efi_handle_t)image_obj)
2985 r = EFI_CALL(efi_close_protocol
2986 (efiobj, protocol->guid,
2987 info->info.agent_handle,
2988 info->info.controller_handle
2990 if (r != EFI_SUCCESS)
2993 * Closing protocols may results in further
2994 * items being deleted. To play it safe loop
2995 * over all elements again.
3002 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3003 efi_size_in_pages(loaded_image_protocol->image_size));
3004 efi_delete_handle(&image_obj->header);
3010 * efi_unload_image() - unload an EFI image
3011 * @image_handle: handle of the image to be unloaded
3013 * This function implements the UnloadImage service.
3015 * See the Unified Extensible Firmware Interface (UEFI) specification for
3018 * Return: status code
3020 efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3022 efi_status_t ret = EFI_SUCCESS;
3023 struct efi_object *efiobj;
3024 struct efi_loaded_image *loaded_image_protocol;
3026 EFI_ENTRY("%p", image_handle);
3028 efiobj = efi_search_obj(image_handle);
3030 ret = EFI_INVALID_PARAMETER;
3033 /* Find the loaded image protocol */
3034 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3035 (void **)&loaded_image_protocol,
3037 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3038 if (ret != EFI_SUCCESS) {
3039 ret = EFI_INVALID_PARAMETER;
3042 switch (efiobj->type) {
3043 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3044 /* Call the unload function */
3045 if (!loaded_image_protocol->unload) {
3046 ret = EFI_UNSUPPORTED;
3049 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3050 if (ret != EFI_SUCCESS)
3053 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3056 ret = EFI_INVALID_PARAMETER;
3059 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3060 loaded_image_protocol);
3062 return EFI_EXIT(ret);
3066 * efi_update_exit_data() - fill exit data parameters of StartImage()
3068 * @image_obj: image handle
3069 * @exit_data_size: size of the exit data buffer
3070 * @exit_data: buffer with data returned by UEFI payload
3071 * Return: status code
3073 static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3074 efi_uintn_t exit_data_size,
3080 * If exit_data is not provided to StartImage(), exit_data_size must be
3083 if (!image_obj->exit_data)
3085 if (image_obj->exit_data_size)
3086 *image_obj->exit_data_size = exit_data_size;
3087 if (exit_data_size && exit_data) {
3088 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3090 (void **)image_obj->exit_data);
3091 if (ret != EFI_SUCCESS)
3093 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3095 image_obj->exit_data = NULL;
3101 * efi_exit() - leave an EFI application or driver
3102 * @image_handle: handle of the application or driver that is exiting
3103 * @exit_status: status code
3104 * @exit_data_size: size of the buffer in bytes
3105 * @exit_data: buffer with data describing an error
3107 * This function implements the Exit service.
3109 * See the Unified Extensible Firmware Interface (UEFI) specification for
3112 * Return: status code
3114 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3115 efi_status_t exit_status,
3116 efi_uintn_t exit_data_size,
3120 * TODO: We should call the unload procedure of the loaded
3124 struct efi_loaded_image *loaded_image_protocol;
3125 struct efi_loaded_image_obj *image_obj =
3126 (struct efi_loaded_image_obj *)image_handle;
3128 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3129 exit_data_size, exit_data);
3131 /* Check parameters */
3132 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3133 (void **)&loaded_image_protocol,
3135 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3136 if (ret != EFI_SUCCESS) {
3137 ret = EFI_INVALID_PARAMETER;
3141 /* Unloading of unstarted images */
3142 switch (image_obj->header.type) {
3143 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3145 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3146 efi_delete_image(image_obj, loaded_image_protocol);
3150 /* Handle does not refer to loaded image */
3151 ret = EFI_INVALID_PARAMETER;
3154 /* A started image can only be unloaded it is the last one started. */
3155 if (image_handle != current_image) {
3156 ret = EFI_INVALID_PARAMETER;
3160 /* Exit data is only foreseen in case of failure. */
3161 if (exit_status != EFI_SUCCESS) {
3162 ret = efi_update_exit_data(image_obj, exit_data_size,
3164 /* Exiting has priority. Don't return error to caller. */
3165 if (ret != EFI_SUCCESS)
3166 EFI_PRINT("%s: out of memory\n", __func__);
3168 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3169 exit_status != EFI_SUCCESS)
3170 efi_delete_image(image_obj, loaded_image_protocol);
3172 /* Make sure entry/exit counts for EFI world cross-overs match */
3173 EFI_EXIT(exit_status);
3176 * But longjmp out with the U-Boot gd, not the application's, as
3177 * the other end is a setjmp call inside EFI context.
3181 image_obj->exit_status = exit_status;
3182 longjmp(&image_obj->exit_jmp, 1);
3184 panic("EFI application exited");
3186 return EFI_EXIT(ret);
3190 * efi_handle_protocol() - get interface of a protocol on a handle
3191 * @handle: handle on which the protocol shall be opened
3192 * @protocol: GUID of the protocol
3193 * @protocol_interface: interface implementing the protocol
3195 * This function implements the HandleProtocol service.
3197 * See the Unified Extensible Firmware Interface (UEFI) specification for
3200 * Return: status code
3202 efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3203 const efi_guid_t *protocol,
3204 void **protocol_interface)
3206 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
3207 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
3211 * efi_bind_controller() - bind a single driver to a controller
3212 * @controller_handle: controller handle
3213 * @driver_image_handle: driver handle
3214 * @remain_device_path: remaining path
3216 * Return: status code
3218 static efi_status_t efi_bind_controller(
3219 efi_handle_t controller_handle,
3220 efi_handle_t driver_image_handle,
3221 struct efi_device_path *remain_device_path)
3223 struct efi_driver_binding_protocol *binding_protocol;
3226 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3227 &efi_guid_driver_binding_protocol,
3228 (void **)&binding_protocol,
3229 driver_image_handle, NULL,
3230 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3231 if (r != EFI_SUCCESS)
3233 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3235 remain_device_path));
3236 if (r == EFI_SUCCESS)
3237 r = EFI_CALL(binding_protocol->start(binding_protocol,
3239 remain_device_path));
3240 EFI_CALL(efi_close_protocol(driver_image_handle,
3241 &efi_guid_driver_binding_protocol,
3242 driver_image_handle, NULL));
3247 * efi_connect_single_controller() - connect a single driver to a controller
3248 * @controller_handle: controller
3249 * @driver_image_handle: driver
3250 * @remain_device_path: remaining path
3252 * Return: status code
3254 static efi_status_t efi_connect_single_controller(
3255 efi_handle_t controller_handle,
3256 efi_handle_t *driver_image_handle,
3257 struct efi_device_path *remain_device_path)
3259 efi_handle_t *buffer;
3263 size_t connected = 0;
3265 /* Get buffer with all handles with driver binding protocol */
3266 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3267 &efi_guid_driver_binding_protocol,
3268 NULL, &count, &buffer));
3269 if (r != EFI_SUCCESS)
3272 /* Context Override */
3273 if (driver_image_handle) {
3274 for (; *driver_image_handle; ++driver_image_handle) {
3275 for (i = 0; i < count; ++i) {
3276 if (buffer[i] == *driver_image_handle) {
3278 r = efi_bind_controller(
3280 *driver_image_handle,
3281 remain_device_path);
3283 * For drivers that do not support the
3284 * controller or are already connected
3285 * we receive an error code here.
3287 if (r == EFI_SUCCESS)
3295 * TODO: Some overrides are not yet implemented:
3296 * - Platform Driver Override
3297 * - Driver Family Override Search
3298 * - Bus Specific Driver Override
3301 /* Driver Binding Search */
3302 for (i = 0; i < count; ++i) {
3304 r = efi_bind_controller(controller_handle,
3306 remain_device_path);
3307 if (r == EFI_SUCCESS)
3312 efi_free_pool(buffer);
3314 return EFI_NOT_FOUND;
3319 * efi_connect_controller() - connect a controller to a driver
3320 * @controller_handle: handle of the controller
3321 * @driver_image_handle: handle of the driver
3322 * @remain_device_path: device path of a child controller
3323 * @recursive: true to connect all child controllers
3325 * This function implements the ConnectController service.
3327 * See the Unified Extensible Firmware Interface (UEFI) specification for
3330 * First all driver binding protocol handles are tried for binding drivers.
3331 * Afterwards all handles that have opened a protocol of the controller
3332 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3334 * Return: status code
3336 static efi_status_t EFIAPI efi_connect_controller(
3337 efi_handle_t controller_handle,
3338 efi_handle_t *driver_image_handle,
3339 struct efi_device_path *remain_device_path,
3343 efi_status_t ret = EFI_NOT_FOUND;
3344 struct efi_object *efiobj;
3346 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
3347 remain_device_path, recursive);
3349 efiobj = efi_search_obj(controller_handle);
3351 ret = EFI_INVALID_PARAMETER;
3355 r = efi_connect_single_controller(controller_handle,
3356 driver_image_handle,
3357 remain_device_path);
3358 if (r == EFI_SUCCESS)
3361 struct efi_handler *handler;
3362 struct efi_open_protocol_info_item *item;
3364 list_for_each_entry(handler, &efiobj->protocols, link) {
3365 list_for_each_entry(item, &handler->open_infos, link) {
3366 if (item->info.attributes &
3367 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3368 r = EFI_CALL(efi_connect_controller(
3369 item->info.controller_handle,
3370 driver_image_handle,
3373 if (r == EFI_SUCCESS)
3379 /* Check for child controller specified by end node */
3380 if (ret != EFI_SUCCESS && remain_device_path &&
3381 remain_device_path->type == DEVICE_PATH_TYPE_END)
3384 return EFI_EXIT(ret);
3388 * efi_reinstall_protocol_interface() - reinstall protocol interface
3389 * @handle: handle on which the protocol shall be reinstalled
3390 * @protocol: GUID of the protocol to be installed
3391 * @old_interface: interface to be removed
3392 * @new_interface: interface to be installed
3394 * This function implements the ReinstallProtocolInterface service.
3396 * See the Unified Extensible Firmware Interface (UEFI) specification for
3399 * The old interface is uninstalled. The new interface is installed.
3400 * Drivers are connected.
3402 * Return: status code
3404 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3405 efi_handle_t handle, const efi_guid_t *protocol,
3406 void *old_interface, void *new_interface)
3410 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3413 /* Uninstall protocol but do not delete handle */
3414 ret = efi_uninstall_protocol(handle, protocol, old_interface);
3415 if (ret != EFI_SUCCESS)
3418 /* Install the new protocol */
3419 ret = efi_add_protocol(handle, protocol, new_interface);
3421 * The UEFI spec does not specify what should happen to the handle
3422 * if in case of an error no protocol interface remains on the handle.
3423 * So let's do nothing here.
3425 if (ret != EFI_SUCCESS)
3428 * The returned status code has to be ignored.
3429 * Do not create an error if no suitable driver for the handle exists.
3431 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3433 return EFI_EXIT(ret);
3437 * efi_get_child_controllers() - get all child controllers associated to a driver
3438 * @efiobj: handle of the controller
3439 * @driver_handle: handle of the driver
3440 * @number_of_children: number of child controllers
3441 * @child_handle_buffer: handles of the the child controllers
3443 * The allocated buffer has to be freed with free().
3445 * Return: status code
3447 static efi_status_t efi_get_child_controllers(
3448 struct efi_object *efiobj,
3449 efi_handle_t driver_handle,
3450 efi_uintn_t *number_of_children,
3451 efi_handle_t **child_handle_buffer)
3453 struct efi_handler *handler;
3454 struct efi_open_protocol_info_item *item;
3455 efi_uintn_t count = 0, i;
3458 /* Count all child controller associations */
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)
3468 * Create buffer. In case of duplicate child controller assignments
3469 * the buffer will be too large. But that does not harm.
3471 *number_of_children = 0;
3474 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3475 if (!*child_handle_buffer)
3476 return EFI_OUT_OF_RESOURCES;
3477 /* Copy unique child handles */
3478 list_for_each_entry(handler, &efiobj->protocols, link) {
3479 list_for_each_entry(item, &handler->open_infos, link) {
3480 if (item->info.agent_handle == driver_handle &&
3481 item->info.attributes &
3482 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3483 /* Check this is a new child controller */
3485 for (i = 0; i < *number_of_children; ++i) {
3486 if ((*child_handle_buffer)[i] ==
3487 item->info.controller_handle)
3490 /* Copy handle to buffer */
3492 i = (*number_of_children)++;
3493 (*child_handle_buffer)[i] =
3494 item->info.controller_handle;
3503 * efi_disconnect_controller() - disconnect a controller from a driver
3504 * @controller_handle: handle of the controller
3505 * @driver_image_handle: handle of the driver
3506 * @child_handle: handle of the child to destroy
3508 * This function implements the DisconnectController service.
3510 * See the Unified Extensible Firmware Interface (UEFI) specification for
3513 * Return: status code
3515 static efi_status_t EFIAPI efi_disconnect_controller(
3516 efi_handle_t controller_handle,
3517 efi_handle_t driver_image_handle,
3518 efi_handle_t child_handle)
3520 struct efi_driver_binding_protocol *binding_protocol;
3521 efi_handle_t *child_handle_buffer = NULL;
3522 size_t number_of_children = 0;
3524 struct efi_object *efiobj;
3526 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3529 efiobj = efi_search_obj(controller_handle);
3531 r = EFI_INVALID_PARAMETER;
3535 if (child_handle && !efi_search_obj(child_handle)) {
3536 r = EFI_INVALID_PARAMETER;
3540 /* If no driver handle is supplied, disconnect all drivers */
3541 if (!driver_image_handle) {
3542 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3546 /* Create list of child handles */
3548 number_of_children = 1;
3549 child_handle_buffer = &child_handle;
3551 r = efi_get_child_controllers(efiobj,
3552 driver_image_handle,
3553 &number_of_children,
3554 &child_handle_buffer);
3555 if (r != EFI_SUCCESS)
3559 /* Get the driver binding protocol */
3560 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3561 &efi_guid_driver_binding_protocol,
3562 (void **)&binding_protocol,
3563 driver_image_handle, NULL,
3564 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3565 if (r != EFI_SUCCESS) {
3566 r = EFI_INVALID_PARAMETER;
3569 /* Remove the children */
3570 if (number_of_children) {
3571 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3574 child_handle_buffer));
3575 if (r != EFI_SUCCESS) {
3576 r = EFI_DEVICE_ERROR;
3580 /* Remove the driver */
3581 if (!child_handle) {
3582 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3585 if (r != EFI_SUCCESS) {
3586 r = EFI_DEVICE_ERROR;
3590 EFI_CALL(efi_close_protocol(driver_image_handle,
3591 &efi_guid_driver_binding_protocol,
3592 driver_image_handle, NULL));
3596 free(child_handle_buffer);
3600 static struct efi_boot_services efi_boot_services = {
3602 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3603 .revision = EFI_SPECIFICATION_VERSION,
3604 .headersize = sizeof(struct efi_boot_services),
3606 .raise_tpl = efi_raise_tpl,
3607 .restore_tpl = efi_restore_tpl,
3608 .allocate_pages = efi_allocate_pages_ext,
3609 .free_pages = efi_free_pages_ext,
3610 .get_memory_map = efi_get_memory_map_ext,
3611 .allocate_pool = efi_allocate_pool_ext,
3612 .free_pool = efi_free_pool_ext,
3613 .create_event = efi_create_event_ext,
3614 .set_timer = efi_set_timer_ext,
3615 .wait_for_event = efi_wait_for_event,
3616 .signal_event = efi_signal_event_ext,
3617 .close_event = efi_close_event,
3618 .check_event = efi_check_event,
3619 .install_protocol_interface = efi_install_protocol_interface,
3620 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3621 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3622 .handle_protocol = efi_handle_protocol,
3624 .register_protocol_notify = efi_register_protocol_notify,
3625 .locate_handle = efi_locate_handle_ext,
3626 .locate_device_path = efi_locate_device_path,
3627 .install_configuration_table = efi_install_configuration_table_ext,
3628 .load_image = efi_load_image,
3629 .start_image = efi_start_image,
3631 .unload_image = efi_unload_image,
3632 .exit_boot_services = efi_exit_boot_services,
3633 .get_next_monotonic_count = efi_get_next_monotonic_count,
3635 .set_watchdog_timer = efi_set_watchdog_timer,
3636 .connect_controller = efi_connect_controller,
3637 .disconnect_controller = efi_disconnect_controller,
3638 .open_protocol = efi_open_protocol,
3639 .close_protocol = efi_close_protocol,
3640 .open_protocol_information = efi_open_protocol_information,
3641 .protocols_per_handle = efi_protocols_per_handle,
3642 .locate_handle_buffer = efi_locate_handle_buffer,
3643 .locate_protocol = efi_locate_protocol,
3644 .install_multiple_protocol_interfaces =
3645 efi_install_multiple_protocol_interfaces,
3646 .uninstall_multiple_protocol_interfaces =
3647 efi_uninstall_multiple_protocol_interfaces,
3648 .calculate_crc32 = efi_calculate_crc32,
3649 .copy_mem = efi_copy_mem,
3650 .set_mem = efi_set_mem,
3651 .create_event_ex = efi_create_event_ex,
3654 static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3656 struct efi_system_table __efi_runtime_data systab = {
3658 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3659 .revision = EFI_SPECIFICATION_VERSION,
3660 .headersize = sizeof(struct efi_system_table),
3662 .fw_vendor = firmware_vendor,
3663 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3664 .runtime = &efi_runtime_services,
3670 * efi_initialize_system_table() - Initialize system table
3672 * Return: status code
3674 efi_status_t efi_initialize_system_table(void)
3678 /* Allocate configuration table array */
3679 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3680 EFI_MAX_CONFIGURATION_TABLES *
3681 sizeof(struct efi_configuration_table),
3682 (void **)&systab.tables);
3685 * These entries will be set to NULL in ExitBootServices(). To avoid
3686 * relocation in SetVirtualAddressMap(), set them dynamically.
3688 systab.con_in = &efi_con_in;
3689 systab.con_out = &efi_con_out;
3690 systab.std_err = &efi_con_out;
3691 systab.boottime = &efi_boot_services;
3693 /* Set CRC32 field in table headers */
3694 efi_update_table_header_crc32(&systab.hdr);
3695 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3696 efi_update_table_header_crc32(&efi_boot_services.hdr);