1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application boot time services
5 * Copyright (c) 2016 Alexander Graf
11 #include <efi_loader.h>
17 #include <u-boot/crc.h>
19 #include <linux/libfdt_env.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 /* Task priority level */
24 static efi_uintn_t efi_tpl = TPL_APPLICATION;
26 /* This list contains all the EFI objects our payload has access to */
27 LIST_HEAD(efi_obj_list);
29 /* List of all events */
30 __efi_runtime_data LIST_HEAD(efi_events);
32 /* List of queued events */
33 LIST_HEAD(efi_event_queue);
35 /* Flag to disable timer activity in ExitBootServices() */
36 static bool timers_enabled = true;
38 /* List of all events registered by RegisterProtocolNotify() */
39 LIST_HEAD(efi_register_notify_events);
41 /* Handle of the currently executing image */
42 static efi_handle_t current_image;
44 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
46 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
47 * fixed when compiling U-Boot. However, the payload does not know about that
48 * restriction so we need to manually swap its and our view of that register on
49 * EFI callback entry/exit.
51 static volatile gd_t *efi_gd, *app_gd;
54 /* 1 if inside U-Boot code, 0 if inside EFI payload code */
55 static int entry_count = 1;
56 static int nesting_level;
57 /* GUID of the device tree table */
58 const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
59 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
60 const efi_guid_t efi_guid_driver_binding_protocol =
61 EFI_DRIVER_BINDING_PROTOCOL_GUID;
63 /* event group ExitBootServices() invoked */
64 const efi_guid_t efi_guid_event_group_exit_boot_services =
65 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
66 /* event group SetVirtualAddressMap() invoked */
67 const efi_guid_t efi_guid_event_group_virtual_address_change =
68 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
69 /* event group memory map changed */
70 const efi_guid_t efi_guid_event_group_memory_map_change =
71 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
72 /* event group boot manager about to boot */
73 const efi_guid_t efi_guid_event_group_ready_to_boot =
74 EFI_EVENT_GROUP_READY_TO_BOOT;
75 /* event group ResetSystem() invoked (before ExitBootServices) */
76 const efi_guid_t efi_guid_event_group_reset_system =
77 EFI_EVENT_GROUP_RESET_SYSTEM;
79 static efi_status_t EFIAPI efi_disconnect_controller(
80 efi_handle_t controller_handle,
81 efi_handle_t driver_image_handle,
82 efi_handle_t child_handle);
84 /* Called on every callback entry */
85 int __efi_entry_check(void)
87 int ret = entry_count++ == 0;
88 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
96 /* Called on every callback exit */
97 int __efi_exit_check(void)
99 int ret = --entry_count == 0;
100 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
107 * efi_save_gd() - save global data register
109 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
110 * As this register may be overwritten by an EFI payload we save it here
111 * and restore it on every callback entered.
113 * This function is called after relocation from initr_reloc_global_data().
115 void efi_save_gd(void)
117 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
123 * efi_restore_gd() - restore global data register
125 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
126 * Restore it after returning from the UEFI world to the value saved via
129 void efi_restore_gd(void)
131 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
132 /* Only restore if we're already in EFI context */
140 * indent_string() - returns a string for indenting with two spaces per level
141 * @level: indent level
143 * A maximum of ten indent levels is supported. Higher indent levels will be
146 * Return: A string for indenting with two spaces per level is
149 static const char *indent_string(int level)
151 const char *indent = " ";
152 const int max = strlen(indent);
154 level = min(max, level * 2);
155 return &indent[max - level];
158 const char *__efi_nesting(void)
160 return indent_string(nesting_level);
163 const char *__efi_nesting_inc(void)
165 return indent_string(nesting_level++);
168 const char *__efi_nesting_dec(void)
170 return indent_string(--nesting_level);
174 * efi_event_is_queued() - check if an event is queued
177 * Return: true if event is queued
179 static bool efi_event_is_queued(struct efi_event *event)
181 return !!event->queue_link.next;
185 * efi_process_event_queue() - process event queue
187 static void efi_process_event_queue(void)
189 while (!list_empty(&efi_event_queue)) {
190 struct efi_event *event;
193 event = list_first_entry(&efi_event_queue, struct efi_event,
195 if (efi_tpl >= event->notify_tpl)
197 list_del(&event->queue_link);
198 event->queue_link.next = NULL;
199 event->queue_link.prev = NULL;
200 /* Events must be executed at the event's TPL */
202 efi_tpl = event->notify_tpl;
203 EFI_CALL_VOID(event->notify_function(event,
204 event->notify_context));
206 if (event->type == EVT_NOTIFY_SIGNAL)
207 event->is_signaled = 0;
212 * efi_queue_event() - queue an EFI event
213 * @event: event to signal
215 * This function queues the notification function of the event for future
219 static void efi_queue_event(struct efi_event *event)
221 struct efi_event *item;
223 if (!event->notify_function)
226 if (!efi_event_is_queued(event)) {
228 * Events must be notified in order of decreasing task priority
229 * level. Insert the new event accordingly.
231 list_for_each_entry(item, &efi_event_queue, queue_link) {
232 if (item->notify_tpl < event->notify_tpl) {
233 list_add_tail(&event->queue_link,
240 list_add_tail(&event->queue_link, &efi_event_queue);
242 efi_process_event_queue();
246 * is_valid_tpl() - check if the task priority level is valid
248 * @tpl: TPL level to check
249 * Return: status code
251 efi_status_t is_valid_tpl(efi_uintn_t tpl)
254 case TPL_APPLICATION:
260 return EFI_INVALID_PARAMETER;
265 * efi_signal_event() - signal an EFI event
266 * @event: event to signal
268 * This function signals an event. If the event belongs to an event group all
269 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
270 * their notification function is queued.
272 * For the SignalEvent service see efi_signal_event_ext.
274 void efi_signal_event(struct efi_event *event)
276 if (event->is_signaled)
279 struct efi_event *evt;
282 * The signaled state has to set before executing any
283 * notification function
285 list_for_each_entry(evt, &efi_events, link) {
286 if (!evt->group || guidcmp(evt->group, event->group))
288 if (evt->is_signaled)
290 evt->is_signaled = true;
292 list_for_each_entry(evt, &efi_events, link) {
293 if (!evt->group || guidcmp(evt->group, event->group))
295 efi_queue_event(evt);
298 event->is_signaled = true;
299 efi_queue_event(event);
304 * efi_raise_tpl() - raise the task priority level
305 * @new_tpl: new value of the task priority level
307 * This function implements the RaiseTpl service.
309 * See the Unified Extensible Firmware Interface (UEFI) specification for
312 * Return: old value of the task priority level
314 static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
316 efi_uintn_t old_tpl = efi_tpl;
318 EFI_ENTRY("0x%zx", new_tpl);
320 if (new_tpl < efi_tpl)
321 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
323 if (efi_tpl > TPL_HIGH_LEVEL)
324 efi_tpl = TPL_HIGH_LEVEL;
326 EFI_EXIT(EFI_SUCCESS);
331 * efi_restore_tpl() - lower the task priority level
332 * @old_tpl: value of the task priority level to be restored
334 * This function implements the RestoreTpl service.
336 * See the Unified Extensible Firmware Interface (UEFI) specification for
339 static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
341 EFI_ENTRY("0x%zx", old_tpl);
343 if (old_tpl > efi_tpl)
344 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
346 if (efi_tpl > TPL_HIGH_LEVEL)
347 efi_tpl = TPL_HIGH_LEVEL;
350 * Lowering the TPL may have made queued events eligible for execution.
354 EFI_EXIT(EFI_SUCCESS);
358 * efi_allocate_pages_ext() - allocate memory pages
359 * @type: type of allocation to be performed
360 * @memory_type: usage type of the allocated memory
361 * @pages: number of pages to be allocated
362 * @memory: allocated memory
364 * This function implements the AllocatePages service.
366 * See the Unified Extensible Firmware Interface (UEFI) specification for
369 * Return: status code
371 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
377 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
378 r = efi_allocate_pages(type, memory_type, pages, memory);
383 * efi_free_pages_ext() - Free memory pages.
384 * @memory: start of the memory area to be freed
385 * @pages: number of pages to be freed
387 * This function implements the FreePages service.
389 * See the Unified Extensible Firmware Interface (UEFI) specification for
392 * Return: status code
394 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
399 EFI_ENTRY("%llx, 0x%zx", memory, pages);
400 r = efi_free_pages(memory, pages);
405 * efi_get_memory_map_ext() - get map describing memory usage
406 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
407 * on exit the size of the copied memory map
408 * @memory_map: buffer to which the memory map is written
409 * @map_key: key for the memory map
410 * @descriptor_size: size of an individual memory descriptor
411 * @descriptor_version: version number of the memory descriptor structure
413 * This function implements the GetMemoryMap service.
415 * See the Unified Extensible Firmware Interface (UEFI) specification for
418 * Return: status code
420 static efi_status_t EFIAPI efi_get_memory_map_ext(
421 efi_uintn_t *memory_map_size,
422 struct efi_mem_desc *memory_map,
423 efi_uintn_t *map_key,
424 efi_uintn_t *descriptor_size,
425 uint32_t *descriptor_version)
429 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
430 map_key, descriptor_size, descriptor_version);
431 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
432 descriptor_size, descriptor_version);
437 * efi_allocate_pool_ext() - allocate memory from pool
438 * @pool_type: type of the pool from which memory is to be allocated
439 * @size: number of bytes to be allocated
440 * @buffer: allocated memory
442 * This function implements the AllocatePool service.
444 * See the Unified Extensible Firmware Interface (UEFI) specification for
447 * Return: status code
449 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
455 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
456 r = efi_allocate_pool(pool_type, size, buffer);
461 * efi_free_pool_ext() - free memory from pool
462 * @buffer: start of memory to be freed
464 * This function implements the FreePool service.
466 * See the Unified Extensible Firmware Interface (UEFI) specification for
469 * Return: status code
471 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
475 EFI_ENTRY("%p", buffer);
476 r = efi_free_pool(buffer);
481 * efi_add_handle() - add a new handle to the object list
483 * @handle: handle to be added
485 * The protocols list is initialized. The handle is added to the list of known
488 void efi_add_handle(efi_handle_t handle)
492 INIT_LIST_HEAD(&handle->protocols);
493 list_add_tail(&handle->link, &efi_obj_list);
497 * efi_create_handle() - create handle
498 * @handle: new handle
500 * Return: status code
502 efi_status_t efi_create_handle(efi_handle_t *handle)
504 struct efi_object *obj;
506 obj = calloc(1, sizeof(struct efi_object));
508 return EFI_OUT_OF_RESOURCES;
517 * efi_search_protocol() - find a protocol on a handle.
519 * @protocol_guid: GUID of the protocol
520 * @handler: reference to the protocol
522 * Return: status code
524 efi_status_t efi_search_protocol(const efi_handle_t handle,
525 const efi_guid_t *protocol_guid,
526 struct efi_handler **handler)
528 struct efi_object *efiobj;
529 struct list_head *lhandle;
531 if (!handle || !protocol_guid)
532 return EFI_INVALID_PARAMETER;
533 efiobj = efi_search_obj(handle);
535 return EFI_INVALID_PARAMETER;
536 list_for_each(lhandle, &efiobj->protocols) {
537 struct efi_handler *protocol;
539 protocol = list_entry(lhandle, struct efi_handler, link);
540 if (!guidcmp(protocol->guid, protocol_guid)) {
546 return EFI_NOT_FOUND;
550 * efi_remove_protocol() - delete protocol from a handle
551 * @handle: handle from which the protocol shall be deleted
552 * @protocol: GUID of the protocol to be deleted
553 * @protocol_interface: interface of the protocol implementation
555 * Return: status code
557 efi_status_t efi_remove_protocol(const efi_handle_t handle,
558 const efi_guid_t *protocol,
559 void *protocol_interface)
561 struct efi_handler *handler;
564 ret = efi_search_protocol(handle, protocol, &handler);
565 if (ret != EFI_SUCCESS)
567 if (handler->protocol_interface != protocol_interface)
568 return EFI_NOT_FOUND;
569 list_del(&handler->link);
575 * efi_remove_all_protocols() - delete all protocols from a handle
576 * @handle: handle from which the protocols shall be deleted
578 * Return: status code
580 efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
582 struct efi_object *efiobj;
583 struct efi_handler *protocol;
584 struct efi_handler *pos;
586 efiobj = efi_search_obj(handle);
588 return EFI_INVALID_PARAMETER;
589 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
592 ret = efi_remove_protocol(handle, protocol->guid,
593 protocol->protocol_interface);
594 if (ret != EFI_SUCCESS)
601 * efi_delete_handle() - delete handle
603 * @handle: handle to delete
605 void efi_delete_handle(efi_handle_t handle)
609 efi_remove_all_protocols(handle);
610 list_del(&handle->link);
615 * efi_is_event() - check if a pointer is a valid event
616 * @event: pointer to check
618 * Return: status code
620 static efi_status_t efi_is_event(const struct efi_event *event)
622 const struct efi_event *evt;
625 return EFI_INVALID_PARAMETER;
626 list_for_each_entry(evt, &efi_events, link) {
630 return EFI_INVALID_PARAMETER;
634 * efi_create_event() - create an event
636 * @type: type of the event to create
637 * @notify_tpl: task priority level of the event
638 * @notify_function: notification function of the event
639 * @notify_context: pointer passed to the notification function
640 * @group: event group
641 * @event: created event
643 * This function is used inside U-Boot code to create an event.
645 * For the API function implementing the CreateEvent service see
646 * efi_create_event_ext.
648 * Return: status code
650 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
651 void (EFIAPI *notify_function) (
652 struct efi_event *event,
654 void *notify_context, efi_guid_t *group,
655 struct efi_event **event)
657 struct efi_event *evt;
662 return EFI_INVALID_PARAMETER;
667 case EVT_NOTIFY_SIGNAL:
668 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
669 case EVT_NOTIFY_WAIT:
670 case EVT_TIMER | EVT_NOTIFY_WAIT:
671 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
672 pool_type = EFI_BOOT_SERVICES_DATA;
674 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
675 pool_type = EFI_RUNTIME_SERVICES_DATA;
678 return EFI_INVALID_PARAMETER;
681 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
682 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
683 return EFI_INVALID_PARAMETER;
685 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
687 if (ret != EFI_SUCCESS)
689 memset(evt, 0, sizeof(struct efi_event));
691 evt->notify_tpl = notify_tpl;
692 evt->notify_function = notify_function;
693 evt->notify_context = notify_context;
695 /* Disable timers on boot up */
696 evt->trigger_next = -1ULL;
697 list_add_tail(&evt->link, &efi_events);
703 * efi_create_event_ex() - create an event in a group
704 * @type: type of the event to create
705 * @notify_tpl: task priority level of the event
706 * @notify_function: notification function of the event
707 * @notify_context: pointer passed to the notification function
708 * @event: created event
709 * @event_group: event group
711 * This function implements the CreateEventEx service.
713 * See the Unified Extensible Firmware Interface (UEFI) specification for
716 * Return: status code
718 efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
719 void (EFIAPI *notify_function) (
720 struct efi_event *event,
722 void *notify_context,
723 efi_guid_t *event_group,
724 struct efi_event **event)
728 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
729 notify_context, event_group);
732 * The allowable input parameters are the same as in CreateEvent()
733 * except for the following two disallowed event types.
736 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
737 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
738 ret = EFI_INVALID_PARAMETER;
742 ret = efi_create_event(type, notify_tpl, notify_function,
743 notify_context, event_group, event);
745 return EFI_EXIT(ret);
749 * efi_create_event_ext() - create an event
750 * @type: type of the event to create
751 * @notify_tpl: task priority level of the event
752 * @notify_function: notification function of the event
753 * @notify_context: pointer passed to the notification function
754 * @event: created event
756 * This function implements the CreateEvent service.
758 * See the Unified Extensible Firmware Interface (UEFI) specification for
761 * Return: status code
763 static efi_status_t EFIAPI efi_create_event_ext(
764 uint32_t type, efi_uintn_t notify_tpl,
765 void (EFIAPI *notify_function) (
766 struct efi_event *event,
768 void *notify_context, struct efi_event **event)
770 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
772 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
773 notify_context, NULL, event));
777 * efi_timer_check() - check if a timer event has occurred
779 * Check if a timer event has occurred or a queued notification function should
782 * Our timers have to work without interrupts, so we check whenever keyboard
783 * input or disk accesses happen if enough time elapsed for them to fire.
785 void efi_timer_check(void)
787 struct efi_event *evt;
788 u64 now = timer_get_us();
790 list_for_each_entry(evt, &efi_events, link) {
793 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
795 switch (evt->trigger_type) {
796 case EFI_TIMER_RELATIVE:
797 evt->trigger_type = EFI_TIMER_STOP;
799 case EFI_TIMER_PERIODIC:
800 evt->trigger_next += evt->trigger_time;
805 evt->is_signaled = false;
806 efi_signal_event(evt);
808 efi_process_event_queue();
813 * efi_set_timer() - set the trigger time for a timer event or stop the event
814 * @event: event for which the timer is set
815 * @type: type of the timer
816 * @trigger_time: trigger period in multiples of 100 ns
818 * This is the function for internal usage in U-Boot. For the API function
819 * implementing the SetTimer service see efi_set_timer_ext.
821 * Return: status code
823 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
824 uint64_t trigger_time)
826 /* Check that the event is valid */
827 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
828 return EFI_INVALID_PARAMETER;
831 * The parameter defines a multiple of 100 ns.
832 * We use multiples of 1000 ns. So divide by 10.
834 do_div(trigger_time, 10);
838 event->trigger_next = -1ULL;
840 case EFI_TIMER_PERIODIC:
841 case EFI_TIMER_RELATIVE:
842 event->trigger_next = timer_get_us() + trigger_time;
845 return EFI_INVALID_PARAMETER;
847 event->trigger_type = type;
848 event->trigger_time = trigger_time;
849 event->is_signaled = false;
854 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
856 * @event: event for which the timer is set
857 * @type: type of the timer
858 * @trigger_time: trigger period in multiples of 100 ns
860 * This function implements the SetTimer service.
862 * See the Unified Extensible Firmware Interface (UEFI) specification for
866 * Return: status code
868 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
869 enum efi_timer_delay type,
870 uint64_t trigger_time)
872 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
873 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
877 * efi_wait_for_event() - wait for events to be signaled
878 * @num_events: number of events to be waited for
879 * @event: events to be waited for
880 * @index: index of the event that was signaled
882 * This function implements the WaitForEvent service.
884 * See the Unified Extensible Firmware Interface (UEFI) specification for
887 * Return: status code
889 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
890 struct efi_event **event,
895 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
897 /* Check parameters */
898 if (!num_events || !event)
899 return EFI_EXIT(EFI_INVALID_PARAMETER);
901 if (efi_tpl != TPL_APPLICATION)
902 return EFI_EXIT(EFI_UNSUPPORTED);
903 for (i = 0; i < num_events; ++i) {
904 if (efi_is_event(event[i]) != EFI_SUCCESS)
905 return EFI_EXIT(EFI_INVALID_PARAMETER);
906 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
907 return EFI_EXIT(EFI_INVALID_PARAMETER);
908 if (!event[i]->is_signaled)
909 efi_queue_event(event[i]);
912 /* Wait for signal */
914 for (i = 0; i < num_events; ++i) {
915 if (event[i]->is_signaled)
918 /* Allow events to occur. */
924 * Reset the signal which is passed to the caller to allow periodic
927 event[i]->is_signaled = false;
931 return EFI_EXIT(EFI_SUCCESS);
935 * efi_signal_event_ext() - signal an EFI event
936 * @event: event to signal
938 * This function implements the SignalEvent service.
940 * See the Unified Extensible Firmware Interface (UEFI) specification for
943 * This functions sets the signaled state of the event and queues the
944 * notification function for execution.
946 * Return: status code
948 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
950 EFI_ENTRY("%p", event);
951 if (efi_is_event(event) != EFI_SUCCESS)
952 return EFI_EXIT(EFI_INVALID_PARAMETER);
953 efi_signal_event(event);
954 return EFI_EXIT(EFI_SUCCESS);
958 * efi_close_event() - close an EFI event
959 * @event: event to close
961 * This function implements the CloseEvent service.
963 * See the Unified Extensible Firmware Interface (UEFI) specification for
966 * Return: status code
968 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
970 struct efi_register_notify_event *item, *next;
972 EFI_ENTRY("%p", event);
973 if (efi_is_event(event) != EFI_SUCCESS)
974 return EFI_EXIT(EFI_INVALID_PARAMETER);
976 /* Remove protocol notify registrations for the event */
977 list_for_each_entry_safe(item, next, &efi_register_notify_events,
979 if (event == item->event) {
980 struct efi_protocol_notification *hitem, *hnext;
982 /* Remove signaled handles */
983 list_for_each_entry_safe(hitem, hnext, &item->handles,
985 list_del(&hitem->link);
988 list_del(&item->link);
992 /* Remove event from queue */
993 if (efi_event_is_queued(event))
994 list_del(&event->queue_link);
996 list_del(&event->link);
997 efi_free_pool(event);
998 return EFI_EXIT(EFI_SUCCESS);
1002 * efi_check_event() - check if an event is signaled
1003 * @event: event to check
1005 * This function implements the CheckEvent service.
1007 * See the Unified Extensible Firmware Interface (UEFI) specification for
1010 * If an event is not signaled yet, the notification function is queued. The
1011 * signaled state is cleared.
1013 * Return: status code
1015 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
1017 EFI_ENTRY("%p", event);
1019 if (efi_is_event(event) != EFI_SUCCESS ||
1020 event->type & EVT_NOTIFY_SIGNAL)
1021 return EFI_EXIT(EFI_INVALID_PARAMETER);
1022 if (!event->is_signaled)
1023 efi_queue_event(event);
1024 if (event->is_signaled) {
1025 event->is_signaled = false;
1026 return EFI_EXIT(EFI_SUCCESS);
1028 return EFI_EXIT(EFI_NOT_READY);
1032 * efi_search_obj() - find the internal EFI object for a handle
1033 * @handle: handle to find
1035 * Return: EFI object
1037 struct efi_object *efi_search_obj(const efi_handle_t handle)
1039 struct efi_object *efiobj;
1044 list_for_each_entry(efiobj, &efi_obj_list, link) {
1045 if (efiobj == handle)
1052 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1054 * @handler: handler of a protocol
1056 * Return: open protocol info entry
1058 static struct efi_open_protocol_info_entry *efi_create_open_info(
1059 struct efi_handler *handler)
1061 struct efi_open_protocol_info_item *item;
1063 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1066 /* Append the item to the open protocol info list. */
1067 list_add_tail(&item->link, &handler->open_infos);
1073 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1074 * @item: open protocol info entry to delete
1076 * Return: status code
1078 static efi_status_t efi_delete_open_info(
1079 struct efi_open_protocol_info_item *item)
1081 list_del(&item->link);
1087 * efi_add_protocol() - install new protocol on a handle
1088 * @handle: handle on which the protocol shall be installed
1089 * @protocol: GUID of the protocol to be installed
1090 * @protocol_interface: interface of the protocol implementation
1092 * Return: status code
1094 efi_status_t efi_add_protocol(const efi_handle_t handle,
1095 const efi_guid_t *protocol,
1096 void *protocol_interface)
1098 struct efi_object *efiobj;
1099 struct efi_handler *handler;
1101 struct efi_register_notify_event *event;
1103 efiobj = efi_search_obj(handle);
1105 return EFI_INVALID_PARAMETER;
1106 ret = efi_search_protocol(handle, protocol, NULL);
1107 if (ret != EFI_NOT_FOUND)
1108 return EFI_INVALID_PARAMETER;
1109 handler = calloc(1, sizeof(struct efi_handler));
1111 return EFI_OUT_OF_RESOURCES;
1112 handler->guid = protocol;
1113 handler->protocol_interface = protocol_interface;
1114 INIT_LIST_HEAD(&handler->open_infos);
1115 list_add_tail(&handler->link, &efiobj->protocols);
1117 /* Notify registered events */
1118 list_for_each_entry(event, &efi_register_notify_events, link) {
1119 if (!guidcmp(protocol, &event->protocol)) {
1120 struct efi_protocol_notification *notif;
1122 notif = calloc(1, sizeof(*notif));
1124 list_del(&handler->link);
1126 return EFI_OUT_OF_RESOURCES;
1128 notif->handle = handle;
1129 list_add_tail(¬if->link, &event->handles);
1130 event->event->is_signaled = false;
1131 efi_signal_event(event->event);
1135 if (!guidcmp(&efi_guid_device_path, protocol))
1136 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
1141 * efi_install_protocol_interface() - install protocol interface
1142 * @handle: handle on which the protocol shall be installed
1143 * @protocol: GUID of the protocol to be installed
1144 * @protocol_interface_type: type of the interface to be installed,
1145 * always EFI_NATIVE_INTERFACE
1146 * @protocol_interface: interface of the protocol implementation
1148 * This function implements the InstallProtocolInterface service.
1150 * See the Unified Extensible Firmware Interface (UEFI) specification for
1153 * Return: status code
1155 static efi_status_t EFIAPI efi_install_protocol_interface(
1156 efi_handle_t *handle, const efi_guid_t *protocol,
1157 int protocol_interface_type, void *protocol_interface)
1161 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1162 protocol_interface);
1164 if (!handle || !protocol ||
1165 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1166 r = EFI_INVALID_PARAMETER;
1170 /* Create new handle if requested. */
1172 r = efi_create_handle(handle);
1173 if (r != EFI_SUCCESS)
1175 EFI_PRINT("new handle %p\n", *handle);
1177 EFI_PRINT("handle %p\n", *handle);
1179 /* Add new protocol */
1180 r = efi_add_protocol(*handle, protocol, protocol_interface);
1186 * efi_get_drivers() - get all drivers associated to a controller
1187 * @handle: handle of the controller
1188 * @protocol: protocol GUID (optional)
1189 * @number_of_drivers: number of child controllers
1190 * @driver_handle_buffer: handles of the the drivers
1192 * The allocated buffer has to be freed with free().
1194 * Return: status code
1196 static efi_status_t efi_get_drivers(efi_handle_t handle,
1197 const efi_guid_t *protocol,
1198 efi_uintn_t *number_of_drivers,
1199 efi_handle_t **driver_handle_buffer)
1201 struct efi_handler *handler;
1202 struct efi_open_protocol_info_item *item;
1203 efi_uintn_t count = 0, i;
1206 /* Count all driver associations */
1207 list_for_each_entry(handler, &handle->protocols, link) {
1208 if (protocol && guidcmp(handler->guid, protocol))
1210 list_for_each_entry(item, &handler->open_infos, link) {
1211 if (item->info.attributes &
1212 EFI_OPEN_PROTOCOL_BY_DRIVER)
1216 *number_of_drivers = 0;
1218 *driver_handle_buffer = NULL;
1222 * Create buffer. In case of duplicate driver assignments the buffer
1223 * will be too large. But that does not harm.
1225 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1226 if (!*driver_handle_buffer)
1227 return EFI_OUT_OF_RESOURCES;
1228 /* Collect unique driver handles */
1229 list_for_each_entry(handler, &handle->protocols, link) {
1230 if (protocol && guidcmp(handler->guid, protocol))
1232 list_for_each_entry(item, &handler->open_infos, link) {
1233 if (item->info.attributes &
1234 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1235 /* Check this is a new driver */
1237 for (i = 0; i < *number_of_drivers; ++i) {
1238 if ((*driver_handle_buffer)[i] ==
1239 item->info.agent_handle)
1242 /* Copy handle to buffer */
1244 i = (*number_of_drivers)++;
1245 (*driver_handle_buffer)[i] =
1246 item->info.agent_handle;
1255 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1256 * @handle: handle of the controller
1257 * @protocol: protocol GUID (optional)
1258 * @child_handle: handle of the child to destroy
1260 * This function implements the DisconnectController service.
1262 * See the Unified Extensible Firmware Interface (UEFI) specification for
1265 * Return: status code
1267 static efi_status_t efi_disconnect_all_drivers
1268 (efi_handle_t handle,
1269 const efi_guid_t *protocol,
1270 efi_handle_t child_handle)
1272 efi_uintn_t number_of_drivers;
1273 efi_handle_t *driver_handle_buffer;
1274 efi_status_t r, ret;
1276 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
1277 &driver_handle_buffer);
1278 if (ret != EFI_SUCCESS)
1280 if (!number_of_drivers)
1282 ret = EFI_NOT_FOUND;
1283 while (number_of_drivers) {
1284 r = EFI_CALL(efi_disconnect_controller(
1286 driver_handle_buffer[--number_of_drivers],
1288 if (r == EFI_SUCCESS)
1291 free(driver_handle_buffer);
1296 * efi_uninstall_protocol() - uninstall protocol interface
1298 * @handle: handle from which the protocol shall be removed
1299 * @protocol: GUID of the protocol to be removed
1300 * @protocol_interface: interface to be removed
1302 * This function DOES NOT delete a handle without installed protocol.
1304 * Return: status code
1306 static efi_status_t efi_uninstall_protocol
1307 (efi_handle_t handle, const efi_guid_t *protocol,
1308 void *protocol_interface)
1310 struct efi_object *efiobj;
1311 struct efi_handler *handler;
1312 struct efi_open_protocol_info_item *item;
1313 struct efi_open_protocol_info_item *pos;
1317 efiobj = efi_search_obj(handle);
1319 r = EFI_INVALID_PARAMETER;
1322 /* Find the protocol on the handle */
1323 r = efi_search_protocol(handle, protocol, &handler);
1324 if (r != EFI_SUCCESS)
1326 /* Disconnect controllers */
1327 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1328 /* Close protocol */
1329 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1330 if (item->info.attributes ==
1331 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1332 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1333 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1334 list_del(&item->link);
1336 if (!list_empty(&handler->open_infos)) {
1337 r = EFI_ACCESS_DENIED;
1340 r = efi_remove_protocol(handle, protocol, protocol_interface);
1346 * efi_uninstall_protocol_interface() - uninstall protocol interface
1347 * @handle: handle from which the protocol shall be removed
1348 * @protocol: GUID of the protocol to be removed
1349 * @protocol_interface: interface to be removed
1351 * This function implements the UninstallProtocolInterface service.
1353 * See the Unified Extensible Firmware Interface (UEFI) specification for
1356 * Return: status code
1358 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1359 (efi_handle_t handle, const efi_guid_t *protocol,
1360 void *protocol_interface)
1364 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1366 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1367 if (ret != EFI_SUCCESS)
1370 /* If the last protocol has been removed, delete the handle. */
1371 if (list_empty(&handle->protocols)) {
1372 list_del(&handle->link);
1376 return EFI_EXIT(ret);
1380 * efi_register_protocol_notify() - register an event for notification when a
1381 * protocol is installed.
1382 * @protocol: GUID of the protocol whose installation shall be notified
1383 * @event: event to be signaled upon installation of the protocol
1384 * @registration: key for retrieving the registration information
1386 * This function implements the RegisterProtocolNotify service.
1387 * See the Unified Extensible Firmware Interface (UEFI) specification
1390 * Return: status code
1392 static efi_status_t EFIAPI efi_register_protocol_notify(
1393 const efi_guid_t *protocol,
1394 struct efi_event *event,
1395 void **registration)
1397 struct efi_register_notify_event *item;
1398 efi_status_t ret = EFI_SUCCESS;
1400 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
1402 if (!protocol || !event || !registration) {
1403 ret = EFI_INVALID_PARAMETER;
1407 item = calloc(1, sizeof(struct efi_register_notify_event));
1409 ret = EFI_OUT_OF_RESOURCES;
1413 item->event = event;
1414 guidcpy(&item->protocol, protocol);
1415 INIT_LIST_HEAD(&item->handles);
1417 list_add_tail(&item->link, &efi_register_notify_events);
1419 *registration = item;
1421 return EFI_EXIT(ret);
1425 * efi_search() - determine if an EFI handle implements a protocol
1427 * @search_type: selection criterion
1428 * @protocol: GUID of the protocol
1431 * See the documentation of the LocateHandle service in the UEFI specification.
1433 * Return: 0 if the handle implements the protocol
1435 static int efi_search(enum efi_locate_search_type search_type,
1436 const efi_guid_t *protocol, efi_handle_t handle)
1440 switch (search_type) {
1444 ret = efi_search_protocol(handle, protocol, NULL);
1445 return (ret != EFI_SUCCESS);
1447 /* Invalid search type */
1453 * efi_check_register_notify_event() - check if registration key is valid
1455 * Check that a pointer is a valid registration key as returned by
1456 * RegisterProtocolNotify().
1458 * @key: registration key
1459 * Return: valid registration key or NULL
1461 static struct efi_register_notify_event *efi_check_register_notify_event
1464 struct efi_register_notify_event *event;
1466 list_for_each_entry(event, &efi_register_notify_events, link) {
1467 if (event == (struct efi_register_notify_event *)key)
1474 * efi_locate_handle() - locate handles implementing a protocol
1476 * @search_type: selection criterion
1477 * @protocol: GUID of the protocol
1478 * @search_key: registration key
1479 * @buffer_size: size of the buffer to receive the handles in bytes
1480 * @buffer: buffer to receive the relevant handles
1482 * This function is meant for U-Boot internal calls. For the API implementation
1483 * of the LocateHandle service see efi_locate_handle_ext.
1485 * Return: status code
1487 static efi_status_t efi_locate_handle(
1488 enum efi_locate_search_type search_type,
1489 const efi_guid_t *protocol, void *search_key,
1490 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1492 struct efi_object *efiobj;
1493 efi_uintn_t size = 0;
1494 struct efi_register_notify_event *event;
1495 struct efi_protocol_notification *handle = NULL;
1497 /* Check parameters */
1498 switch (search_type) {
1501 case BY_REGISTER_NOTIFY:
1503 return EFI_INVALID_PARAMETER;
1504 /* Check that the registration key is valid */
1505 event = efi_check_register_notify_event(search_key);
1507 return EFI_INVALID_PARAMETER;
1511 return EFI_INVALID_PARAMETER;
1514 return EFI_INVALID_PARAMETER;
1517 /* Count how much space we need */
1518 if (search_type == BY_REGISTER_NOTIFY) {
1519 if (list_empty(&event->handles))
1520 return EFI_NOT_FOUND;
1521 handle = list_first_entry(&event->handles,
1522 struct efi_protocol_notification,
1524 efiobj = handle->handle;
1525 size += sizeof(void *);
1527 list_for_each_entry(efiobj, &efi_obj_list, link) {
1528 if (!efi_search(search_type, protocol, efiobj))
1529 size += sizeof(void *);
1532 return EFI_NOT_FOUND;
1536 return EFI_INVALID_PARAMETER;
1538 if (*buffer_size < size) {
1539 *buffer_size = size;
1540 return EFI_BUFFER_TOO_SMALL;
1543 *buffer_size = size;
1545 /* The buffer size is sufficient but there is no buffer */
1547 return EFI_INVALID_PARAMETER;
1549 /* Then fill the array */
1550 if (search_type == BY_REGISTER_NOTIFY) {
1552 list_del(&handle->link);
1554 list_for_each_entry(efiobj, &efi_obj_list, link) {
1555 if (!efi_search(search_type, protocol, efiobj))
1564 * efi_locate_handle_ext() - locate handles implementing a protocol.
1565 * @search_type: selection criterion
1566 * @protocol: GUID of the protocol
1567 * @search_key: registration key
1568 * @buffer_size: size of the buffer to receive the handles in bytes
1569 * @buffer: buffer to receive the relevant handles
1571 * This function implements the LocateHandle service.
1573 * See the Unified Extensible Firmware Interface (UEFI) specification for
1576 * Return: 0 if the handle implements the protocol
1578 static efi_status_t EFIAPI efi_locate_handle_ext(
1579 enum efi_locate_search_type search_type,
1580 const efi_guid_t *protocol, void *search_key,
1581 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1583 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1584 buffer_size, buffer);
1586 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1587 buffer_size, buffer));
1591 * efi_remove_configuration_table() - collapses configuration table entries,
1594 * @i: index of the table entry to be removed
1596 static void efi_remove_configuration_table(int i)
1598 struct efi_configuration_table *this = &systab.tables[i];
1599 struct efi_configuration_table *next = &systab.tables[i + 1];
1600 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
1602 memmove(this, next, (ulong)end - (ulong)next);
1607 * efi_install_configuration_table() - adds, updates, or removes a
1608 * configuration table
1609 * @guid: GUID of the installed table
1610 * @table: table to be installed
1612 * This function is used for internal calls. For the API implementation of the
1613 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1615 * Return: status code
1617 efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1620 struct efi_event *evt;
1624 return EFI_INVALID_PARAMETER;
1626 /* Check for GUID override */
1627 for (i = 0; i < systab.nr_tables; i++) {
1628 if (!guidcmp(guid, &systab.tables[i].guid)) {
1630 systab.tables[i].table = table;
1632 efi_remove_configuration_table(i);
1638 return EFI_NOT_FOUND;
1640 /* No override, check for overflow */
1641 if (i >= EFI_MAX_CONFIGURATION_TABLES)
1642 return EFI_OUT_OF_RESOURCES;
1644 /* Add a new entry */
1645 guidcpy(&systab.tables[i].guid, guid);
1646 systab.tables[i].table = table;
1647 systab.nr_tables = i + 1;
1650 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1651 efi_update_table_header_crc32(&systab.hdr);
1653 /* Notify that the configuration table was changed */
1654 list_for_each_entry(evt, &efi_events, link) {
1655 if (evt->group && !guidcmp(evt->group, guid)) {
1656 efi_signal_event(evt);
1665 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1666 * configuration table.
1667 * @guid: GUID of the installed table
1668 * @table: table to be installed
1670 * This function implements the InstallConfigurationTable service.
1672 * See the Unified Extensible Firmware Interface (UEFI) specification for
1675 * Return: status code
1677 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1680 EFI_ENTRY("%pUl, %p", guid, table);
1681 return EFI_EXIT(efi_install_configuration_table(guid, table));
1685 * efi_setup_loaded_image() - initialize a loaded image
1687 * Initialize a loaded_image_info and loaded_image_info object with correct
1688 * protocols, boot-device, etc.
1690 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
1693 * @device_path: device path of the loaded image
1694 * @file_path: file path of the loaded image
1695 * @handle_ptr: handle of the loaded image
1696 * @info_ptr: loaded image protocol
1697 * Return: status code
1699 efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1700 struct efi_device_path *file_path,
1701 struct efi_loaded_image_obj **handle_ptr,
1702 struct efi_loaded_image **info_ptr)
1705 struct efi_loaded_image *info = NULL;
1706 struct efi_loaded_image_obj *obj = NULL;
1707 struct efi_device_path *dp;
1709 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1713 info = calloc(1, sizeof(*info));
1715 return EFI_OUT_OF_RESOURCES;
1716 obj = calloc(1, sizeof(*obj));
1719 return EFI_OUT_OF_RESOURCES;
1721 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
1723 /* Add internal object to object list */
1724 efi_add_handle(&obj->header);
1726 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
1727 info->file_path = file_path;
1728 info->system_table = &systab;
1731 info->device_handle = efi_dp_find_obj(device_path, NULL);
1733 dp = efi_dp_append(device_path, file_path);
1735 ret = EFI_OUT_OF_RESOURCES;
1741 ret = efi_add_protocol(&obj->header,
1742 &efi_guid_loaded_image_device_path, dp);
1743 if (ret != EFI_SUCCESS)
1747 * When asking for the loaded_image interface, just
1748 * return handle which points to loaded_image_info
1750 ret = efi_add_protocol(&obj->header,
1751 &efi_guid_loaded_image, info);
1752 if (ret != EFI_SUCCESS)
1760 printf("ERROR: Failure to install protocols for loaded image\n");
1761 efi_delete_handle(&obj->header);
1767 * efi_load_image_from_path() - load an image using a file path
1769 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1770 * callers obligation to update the memory type as needed.
1772 * @file_path: the path of the image to load
1773 * @buffer: buffer containing the loaded image
1774 * @size: size of the loaded image
1775 * Return: status code
1778 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1779 void **buffer, efi_uintn_t *size)
1781 struct efi_file_info *info = NULL;
1782 struct efi_file_handle *f;
1783 static efi_status_t ret;
1787 /* In case of failure nothing is returned */
1792 f = efi_file_from_path(file_path);
1794 return EFI_NOT_FOUND;
1798 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1800 if (ret != EFI_BUFFER_TOO_SMALL) {
1801 ret = EFI_DEVICE_ERROR;
1806 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1808 if (ret != EFI_SUCCESS)
1812 * When reading the file we do not yet know if it contains an
1813 * application, a boottime driver, or a runtime driver. So here we
1814 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1815 * update the reservation according to the image type.
1817 bs = info->file_size;
1818 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1819 EFI_BOOT_SERVICES_DATA,
1820 efi_size_in_pages(bs), &addr);
1821 if (ret != EFI_SUCCESS) {
1822 ret = EFI_OUT_OF_RESOURCES;
1827 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1828 if (ret != EFI_SUCCESS)
1829 efi_free_pages(addr, efi_size_in_pages(bs));
1830 *buffer = (void *)(uintptr_t)addr;
1833 EFI_CALL(f->close(f));
1839 * efi_load_image() - load an EFI image into memory
1840 * @boot_policy: true for request originating from the boot manager
1841 * @parent_image: the caller's image handle
1842 * @file_path: the path of the image to load
1843 * @source_buffer: memory location from which the image is installed
1844 * @source_size: size of the memory area from which the image is installed
1845 * @image_handle: handle for the newly installed image
1847 * This function implements the LoadImage service.
1849 * See the Unified Extensible Firmware Interface (UEFI) specification
1852 * Return: status code
1854 efi_status_t EFIAPI efi_load_image(bool boot_policy,
1855 efi_handle_t parent_image,
1856 struct efi_device_path *file_path,
1857 void *source_buffer,
1858 efi_uintn_t source_size,
1859 efi_handle_t *image_handle)
1861 struct efi_device_path *dp, *fp;
1862 struct efi_loaded_image *info = NULL;
1863 struct efi_loaded_image_obj **image_obj =
1864 (struct efi_loaded_image_obj **)image_handle;
1868 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
1869 file_path, source_buffer, source_size, image_handle);
1871 if (!image_handle || (!source_buffer && !file_path) ||
1872 !efi_search_obj(parent_image) ||
1873 /* The parent image handle must refer to a loaded image */
1874 !parent_image->type) {
1875 ret = EFI_INVALID_PARAMETER;
1879 if (!source_buffer) {
1880 ret = efi_load_image_from_path(file_path, &dest_buffer,
1882 if (ret != EFI_SUCCESS)
1885 dest_buffer = source_buffer;
1887 /* split file_path which contains both the device and file parts */
1888 efi_dp_split_file_path(file_path, &dp, &fp);
1889 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1890 if (ret == EFI_SUCCESS)
1891 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
1893 /* Release buffer to which file was loaded */
1894 efi_free_pages((uintptr_t)dest_buffer,
1895 efi_size_in_pages(source_size));
1896 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
1897 info->system_table = &systab;
1898 info->parent_handle = parent_image;
1900 /* The image is invalid. Release all associated resources. */
1901 efi_delete_handle(*image_handle);
1902 *image_handle = NULL;
1906 return EFI_EXIT(ret);
1910 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1912 static void efi_exit_caches(void)
1914 #if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
1916 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
1917 * caches are enabled.
1920 * According to the UEFI spec caches that can be managed via CP15
1921 * operations should be enabled. Caches requiring platform information
1922 * to manage should be disabled. This should not happen in
1923 * ExitBootServices() but before invoking any UEFI binary is invoked.
1925 * We want to keep the current workaround while GRUB prior to version
1926 * 2.04 is still in use.
1928 cleanup_before_linux();
1933 * efi_exit_boot_services() - stop all boot services
1934 * @image_handle: handle of the loaded image
1935 * @map_key: key of the memory map
1937 * This function implements the ExitBootServices service.
1939 * See the Unified Extensible Firmware Interface (UEFI) specification
1942 * All timer events are disabled. For exit boot services events the
1943 * notification function is called. The boot services are disabled in the
1946 * Return: status code
1948 static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
1949 efi_uintn_t map_key)
1951 struct efi_event *evt, *next_event;
1952 efi_status_t ret = EFI_SUCCESS;
1954 EFI_ENTRY("%p, %zx", image_handle, map_key);
1956 /* Check that the caller has read the current memory map */
1957 if (map_key != efi_memory_map_key) {
1958 ret = EFI_INVALID_PARAMETER;
1962 /* Check if ExitBootServices has already been called */
1963 if (!systab.boottime)
1966 /* Stop all timer related activities */
1967 timers_enabled = false;
1969 /* Add related events to the event group */
1970 list_for_each_entry(evt, &efi_events, link) {
1971 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1972 evt->group = &efi_guid_event_group_exit_boot_services;
1974 /* Notify that ExitBootServices is invoked. */
1975 list_for_each_entry(evt, &efi_events, link) {
1977 !guidcmp(evt->group,
1978 &efi_guid_event_group_exit_boot_services)) {
1979 efi_signal_event(evt);
1984 /* Make sure that notification functions are not called anymore */
1985 efi_tpl = TPL_HIGH_LEVEL;
1987 /* Notify variable services */
1988 efi_variables_boot_exit_notify();
1990 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
1991 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
1992 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
1993 list_del(&evt->link);
1996 board_quiesce_devices();
1998 /* Patch out unsupported runtime function */
1999 efi_runtime_detach();
2001 /* Fix up caches for EFI payloads if necessary */
2004 /* This stops all lingering devices */
2005 bootm_disable_interrupts();
2007 /* Disable boot time services */
2008 systab.con_in_handle = NULL;
2009 systab.con_in = NULL;
2010 systab.con_out_handle = NULL;
2011 systab.con_out = NULL;
2012 systab.stderr_handle = NULL;
2013 systab.std_err = NULL;
2014 systab.boottime = NULL;
2016 /* Recalculate CRC32 */
2017 efi_update_table_header_crc32(&systab.hdr);
2019 /* Give the payload some time to boot */
2020 efi_set_watchdog(0);
2023 return EFI_EXIT(ret);
2027 * efi_get_next_monotonic_count() - get next value of the counter
2028 * @count: returned value of the counter
2030 * This function implements the NextMonotonicCount service.
2032 * See the Unified Extensible Firmware Interface (UEFI) specification for
2035 * Return: status code
2037 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2039 static uint64_t mono;
2042 EFI_ENTRY("%p", count);
2044 ret = EFI_INVALID_PARAMETER;
2050 return EFI_EXIT(ret);
2054 * efi_stall() - sleep
2055 * @microseconds: period to sleep in microseconds
2057 * This function implements the Stall service.
2059 * See the Unified Extensible Firmware Interface (UEFI) specification for
2062 * Return: status code
2064 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2068 EFI_ENTRY("%ld", microseconds);
2070 end_tick = get_ticks() + usec_to_tick(microseconds);
2071 while (get_ticks() < end_tick)
2074 return EFI_EXIT(EFI_SUCCESS);
2078 * efi_set_watchdog_timer() - reset the watchdog timer
2079 * @timeout: seconds before reset by watchdog
2080 * @watchdog_code: code to be logged when resetting
2081 * @data_size: size of buffer in bytes
2082 * @watchdog_data: buffer with data describing the reset reason
2084 * This function implements the SetWatchdogTimer service.
2086 * See the Unified Extensible Firmware Interface (UEFI) specification for
2089 * Return: status code
2091 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2092 uint64_t watchdog_code,
2093 unsigned long data_size,
2094 uint16_t *watchdog_data)
2096 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
2097 data_size, watchdog_data);
2098 return EFI_EXIT(efi_set_watchdog(timeout));
2102 * efi_close_protocol() - close a protocol
2103 * @handle: handle on which the protocol shall be closed
2104 * @protocol: GUID of the protocol to close
2105 * @agent_handle: handle of the driver
2106 * @controller_handle: handle of the controller
2108 * This function implements the CloseProtocol service.
2110 * See the Unified Extensible Firmware Interface (UEFI) specification for
2113 * Return: status code
2115 efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2116 const efi_guid_t *protocol,
2117 efi_handle_t agent_handle,
2118 efi_handle_t controller_handle)
2120 struct efi_handler *handler;
2121 struct efi_open_protocol_info_item *item;
2122 struct efi_open_protocol_info_item *pos;
2125 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
2128 if (!efi_search_obj(agent_handle) ||
2129 (controller_handle && !efi_search_obj(controller_handle))) {
2130 r = EFI_INVALID_PARAMETER;
2133 r = efi_search_protocol(handle, protocol, &handler);
2134 if (r != EFI_SUCCESS)
2138 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2139 if (item->info.agent_handle == agent_handle &&
2140 item->info.controller_handle == controller_handle) {
2141 efi_delete_open_info(item);
2150 * efi_open_protocol_information() - provide information about then open status
2151 * of a protocol on a handle
2152 * @handle: handle for which the information shall be retrieved
2153 * @protocol: GUID of the protocol
2154 * @entry_buffer: buffer to receive the open protocol information
2155 * @entry_count: number of entries available in the buffer
2157 * This function implements the OpenProtocolInformation service.
2159 * See the Unified Extensible Firmware Interface (UEFI) specification for
2162 * Return: status code
2164 static efi_status_t EFIAPI efi_open_protocol_information(
2165 efi_handle_t handle, const efi_guid_t *protocol,
2166 struct efi_open_protocol_info_entry **entry_buffer,
2167 efi_uintn_t *entry_count)
2169 unsigned long buffer_size;
2170 unsigned long count;
2171 struct efi_handler *handler;
2172 struct efi_open_protocol_info_item *item;
2175 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
2178 /* Check parameters */
2179 if (!entry_buffer) {
2180 r = EFI_INVALID_PARAMETER;
2183 r = efi_search_protocol(handle, protocol, &handler);
2184 if (r != EFI_SUCCESS)
2189 list_for_each_entry(item, &handler->open_infos, link) {
2190 if (item->info.open_count)
2193 *entry_count = count;
2194 *entry_buffer = NULL;
2201 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2202 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2203 (void **)entry_buffer);
2204 if (r != EFI_SUCCESS)
2206 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2207 if (item->info.open_count)
2208 (*entry_buffer)[--count] = item->info;
2215 * efi_protocols_per_handle() - get protocols installed on a handle
2216 * @handle: handle for which the information is retrieved
2217 * @protocol_buffer: buffer with protocol GUIDs
2218 * @protocol_buffer_count: number of entries in the buffer
2220 * This function implements the ProtocolsPerHandleService.
2222 * See the Unified Extensible Firmware Interface (UEFI) specification for
2225 * Return: status code
2227 static efi_status_t EFIAPI efi_protocols_per_handle(
2228 efi_handle_t handle, efi_guid_t ***protocol_buffer,
2229 efi_uintn_t *protocol_buffer_count)
2231 unsigned long buffer_size;
2232 struct efi_object *efiobj;
2233 struct list_head *protocol_handle;
2236 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2237 protocol_buffer_count);
2239 if (!handle || !protocol_buffer || !protocol_buffer_count)
2240 return EFI_EXIT(EFI_INVALID_PARAMETER);
2242 *protocol_buffer = NULL;
2243 *protocol_buffer_count = 0;
2245 efiobj = efi_search_obj(handle);
2247 return EFI_EXIT(EFI_INVALID_PARAMETER);
2249 /* Count protocols */
2250 list_for_each(protocol_handle, &efiobj->protocols) {
2251 ++*protocol_buffer_count;
2255 if (*protocol_buffer_count) {
2258 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2259 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2260 (void **)protocol_buffer);
2261 if (r != EFI_SUCCESS)
2263 list_for_each(protocol_handle, &efiobj->protocols) {
2264 struct efi_handler *protocol;
2266 protocol = list_entry(protocol_handle,
2267 struct efi_handler, link);
2268 (*protocol_buffer)[j] = (void *)protocol->guid;
2273 return EFI_EXIT(EFI_SUCCESS);
2277 * efi_locate_handle_buffer() - locate handles implementing a protocol
2278 * @search_type: selection criterion
2279 * @protocol: GUID of the protocol
2280 * @search_key: registration key
2281 * @no_handles: number of returned handles
2282 * @buffer: buffer with the returned handles
2284 * This function implements the LocateHandleBuffer service.
2286 * See the Unified Extensible Firmware Interface (UEFI) specification for
2289 * Return: status code
2291 efi_status_t EFIAPI efi_locate_handle_buffer(
2292 enum efi_locate_search_type search_type,
2293 const efi_guid_t *protocol, void *search_key,
2294 efi_uintn_t *no_handles, efi_handle_t **buffer)
2297 efi_uintn_t buffer_size = 0;
2299 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
2300 no_handles, buffer);
2302 if (!no_handles || !buffer) {
2303 r = EFI_INVALID_PARAMETER;
2308 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2310 if (r != EFI_BUFFER_TOO_SMALL)
2312 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
2314 if (r != EFI_SUCCESS)
2316 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2318 if (r == EFI_SUCCESS)
2319 *no_handles = buffer_size / sizeof(efi_handle_t);
2325 * efi_locate_protocol() - find an interface implementing a protocol
2326 * @protocol: GUID of the protocol
2327 * @registration: registration key passed to the notification function
2328 * @protocol_interface: interface implementing the protocol
2330 * This function implements the LocateProtocol service.
2332 * See the Unified Extensible Firmware Interface (UEFI) specification for
2335 * Return: status code
2337 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
2339 void **protocol_interface)
2341 struct efi_handler *handler;
2343 struct efi_object *efiobj;
2345 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
2348 * The UEFI spec explicitly requires a protocol even if a registration
2349 * key is provided. This differs from the logic in LocateHandle().
2351 if (!protocol || !protocol_interface)
2352 return EFI_EXIT(EFI_INVALID_PARAMETER);
2355 struct efi_register_notify_event *event;
2356 struct efi_protocol_notification *handle;
2358 event = efi_check_register_notify_event(registration);
2360 return EFI_EXIT(EFI_INVALID_PARAMETER);
2362 * The UEFI spec requires to return EFI_NOT_FOUND if no
2363 * protocol instance matches protocol and registration.
2364 * So let's do the same for a mismatch between protocol and
2367 if (guidcmp(&event->protocol, protocol))
2369 if (list_empty(&event->handles))
2371 handle = list_first_entry(&event->handles,
2372 struct efi_protocol_notification,
2374 efiobj = handle->handle;
2375 list_del(&handle->link);
2377 ret = efi_search_protocol(efiobj, protocol, &handler);
2378 if (ret == EFI_SUCCESS)
2381 list_for_each_entry(efiobj, &efi_obj_list, link) {
2382 ret = efi_search_protocol(efiobj, protocol, &handler);
2383 if (ret == EFI_SUCCESS)
2388 *protocol_interface = NULL;
2389 return EFI_EXIT(EFI_NOT_FOUND);
2391 *protocol_interface = handler->protocol_interface;
2392 return EFI_EXIT(EFI_SUCCESS);
2396 * efi_locate_device_path() - Get the device path and handle of an device
2397 * implementing a protocol
2398 * @protocol: GUID of the protocol
2399 * @device_path: device path
2400 * @device: handle of the device
2402 * This function implements the LocateDevicePath service.
2404 * See the Unified Extensible Firmware Interface (UEFI) specification for
2407 * Return: status code
2409 static efi_status_t EFIAPI efi_locate_device_path(
2410 const efi_guid_t *protocol,
2411 struct efi_device_path **device_path,
2412 efi_handle_t *device)
2414 struct efi_device_path *dp;
2416 struct efi_handler *handler;
2417 efi_handle_t *handles;
2419 size_t len_best = 0;
2420 efi_uintn_t no_handles;
2424 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2426 if (!protocol || !device_path || !*device_path) {
2427 ret = EFI_INVALID_PARAMETER;
2431 /* Find end of device path */
2432 len = efi_dp_instance_size(*device_path);
2434 /* Get all handles implementing the protocol */
2435 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2436 &no_handles, &handles));
2437 if (ret != EFI_SUCCESS)
2440 for (i = 0; i < no_handles; ++i) {
2441 /* Find the device path protocol */
2442 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2444 if (ret != EFI_SUCCESS)
2446 dp = (struct efi_device_path *)handler->protocol_interface;
2447 len_dp = efi_dp_instance_size(dp);
2449 * This handle can only be a better fit
2450 * if its device path length is longer than the best fit and
2451 * if its device path length is shorter of equal the searched
2454 if (len_dp <= len_best || len_dp > len)
2456 /* Check if dp is a subpath of device_path */
2457 if (memcmp(*device_path, dp, len_dp))
2460 ret = EFI_INVALID_PARAMETER;
2463 *device = handles[i];
2467 remainder = (u8 *)*device_path + len_best;
2468 *device_path = (struct efi_device_path *)remainder;
2471 ret = EFI_NOT_FOUND;
2474 return EFI_EXIT(ret);
2478 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2480 * @handle: handle on which the protocol interfaces shall be installed
2481 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2484 * This function implements the MultipleProtocolInterfaces service.
2486 * See the Unified Extensible Firmware Interface (UEFI) specification for
2489 * Return: status code
2491 efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2492 (efi_handle_t *handle, ...)
2494 EFI_ENTRY("%p", handle);
2497 const efi_guid_t *protocol;
2498 void *protocol_interface;
2499 efi_handle_t old_handle;
2500 efi_status_t r = EFI_SUCCESS;
2504 return EFI_EXIT(EFI_INVALID_PARAMETER);
2506 efi_va_start(argptr, handle);
2508 protocol = efi_va_arg(argptr, efi_guid_t*);
2511 protocol_interface = efi_va_arg(argptr, void*);
2512 /* Check that a device path has not been installed before */
2513 if (!guidcmp(protocol, &efi_guid_device_path)) {
2514 struct efi_device_path *dp = protocol_interface;
2516 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2518 if (r == EFI_SUCCESS &&
2519 dp->type == DEVICE_PATH_TYPE_END) {
2520 EFI_PRINT("Path %pD already installed\n",
2521 protocol_interface);
2522 r = EFI_ALREADY_STARTED;
2526 r = EFI_CALL(efi_install_protocol_interface(
2528 EFI_NATIVE_INTERFACE,
2529 protocol_interface));
2530 if (r != EFI_SUCCESS)
2535 if (r == EFI_SUCCESS)
2538 /* If an error occurred undo all changes. */
2539 efi_va_start(argptr, handle);
2541 protocol = efi_va_arg(argptr, efi_guid_t*);
2542 protocol_interface = efi_va_arg(argptr, void*);
2543 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
2544 protocol_interface));
2552 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2554 * @handle: handle from which the protocol interfaces shall be removed
2555 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2558 * This function implements the UninstallMultipleProtocolInterfaces service.
2560 * See the Unified Extensible Firmware Interface (UEFI) specification for
2563 * Return: status code
2565 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2566 efi_handle_t handle, ...)
2568 EFI_ENTRY("%p", handle);
2571 const efi_guid_t *protocol;
2572 void *protocol_interface;
2573 efi_status_t r = EFI_SUCCESS;
2577 return EFI_EXIT(EFI_INVALID_PARAMETER);
2579 efi_va_start(argptr, handle);
2581 protocol = efi_va_arg(argptr, efi_guid_t*);
2584 protocol_interface = efi_va_arg(argptr, void*);
2585 r = efi_uninstall_protocol(handle, protocol,
2586 protocol_interface);
2587 if (r != EFI_SUCCESS)
2592 if (r == EFI_SUCCESS) {
2593 /* If the last protocol has been removed, delete the handle. */
2594 if (list_empty(&handle->protocols)) {
2595 list_del(&handle->link);
2601 /* If an error occurred undo all changes. */
2602 efi_va_start(argptr, handle);
2604 protocol = efi_va_arg(argptr, efi_guid_t*);
2605 protocol_interface = efi_va_arg(argptr, void*);
2606 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2607 EFI_NATIVE_INTERFACE,
2608 protocol_interface));
2612 /* In case of an error always return EFI_INVALID_PARAMETER */
2613 return EFI_EXIT(EFI_INVALID_PARAMETER);
2617 * efi_calculate_crc32() - calculate cyclic redundancy code
2618 * @data: buffer with data
2619 * @data_size: size of buffer in bytes
2620 * @crc32_p: cyclic redundancy code
2622 * This function implements the CalculateCrc32 service.
2624 * See the Unified Extensible Firmware Interface (UEFI) specification for
2627 * Return: status code
2629 static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2630 efi_uintn_t data_size,
2633 efi_status_t ret = EFI_SUCCESS;
2635 EFI_ENTRY("%p, %zu", data, data_size);
2636 if (!data || !data_size || !crc32_p) {
2637 ret = EFI_INVALID_PARAMETER;
2640 *crc32_p = crc32(0, data, data_size);
2642 return EFI_EXIT(ret);
2646 * efi_copy_mem() - copy memory
2647 * @destination: destination of the copy operation
2648 * @source: source of the copy operation
2649 * @length: number of bytes to copy
2651 * This function implements the CopyMem service.
2653 * See the Unified Extensible Firmware Interface (UEFI) specification for
2656 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2659 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2660 memmove(destination, source, length);
2661 EFI_EXIT(EFI_SUCCESS);
2665 * efi_set_mem() - Fill memory with a byte value.
2666 * @buffer: buffer to fill
2667 * @size: size of buffer in bytes
2668 * @value: byte to copy to the buffer
2670 * This function implements the SetMem service.
2672 * See the Unified Extensible Firmware Interface (UEFI) specification for
2675 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2677 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2678 memset(buffer, value, size);
2679 EFI_EXIT(EFI_SUCCESS);
2683 * efi_protocol_open() - open protocol interface on a handle
2684 * @handler: handler of a protocol
2685 * @protocol_interface: interface implementing the protocol
2686 * @agent_handle: handle of the driver
2687 * @controller_handle: handle of the controller
2688 * @attributes: attributes indicating how to open the protocol
2690 * Return: status code
2692 static efi_status_t efi_protocol_open(
2693 struct efi_handler *handler,
2694 void **protocol_interface, void *agent_handle,
2695 void *controller_handle, uint32_t attributes)
2697 struct efi_open_protocol_info_item *item;
2698 struct efi_open_protocol_info_entry *match = NULL;
2699 bool opened_by_driver = false;
2700 bool opened_exclusive = false;
2702 /* If there is no agent, only return the interface */
2706 /* For TEST_PROTOCOL ignore interface attribute */
2707 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2708 *protocol_interface = NULL;
2711 * Check if the protocol is already opened by a driver with the same
2712 * attributes or opened exclusively
2714 list_for_each_entry(item, &handler->open_infos, link) {
2715 if (item->info.agent_handle == agent_handle) {
2716 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2717 (item->info.attributes == attributes))
2718 return EFI_ALREADY_STARTED;
2720 if (item->info.attributes &
2721 EFI_OPEN_PROTOCOL_BY_DRIVER)
2722 opened_by_driver = true;
2724 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2725 opened_exclusive = true;
2728 /* Only one controller can open the protocol exclusively */
2729 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2730 if (opened_exclusive)
2731 return EFI_ACCESS_DENIED;
2732 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2733 if (opened_exclusive || opened_by_driver)
2734 return EFI_ACCESS_DENIED;
2737 /* Prepare exclusive opening */
2738 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2739 /* Try to disconnect controllers */
2741 opened_by_driver = false;
2742 list_for_each_entry(item, &handler->open_infos, link) {
2745 if (item->info.attributes ==
2746 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2747 ret = EFI_CALL(efi_disconnect_controller(
2748 item->info.controller_handle,
2749 item->info.agent_handle,
2751 if (ret == EFI_SUCCESS)
2753 * Child controllers may have been
2754 * removed from the open_infos list. So
2755 * let's restart the loop.
2757 goto disconnect_next;
2759 opened_by_driver = true;
2762 /* Only one driver can be connected */
2763 if (opened_by_driver)
2764 return EFI_ACCESS_DENIED;
2767 /* Find existing entry */
2768 list_for_each_entry(item, &handler->open_infos, link) {
2769 if (item->info.agent_handle == agent_handle &&
2770 item->info.controller_handle == controller_handle &&
2771 item->info.attributes == attributes)
2772 match = &item->info;
2774 /* None found, create one */
2776 match = efi_create_open_info(handler);
2778 return EFI_OUT_OF_RESOURCES;
2781 match->agent_handle = agent_handle;
2782 match->controller_handle = controller_handle;
2783 match->attributes = attributes;
2784 match->open_count++;
2787 /* For TEST_PROTOCOL ignore interface attribute. */
2788 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2789 *protocol_interface = handler->protocol_interface;
2795 * efi_open_protocol() - open protocol interface on a handle
2796 * @handle: handle on which the protocol shall be opened
2797 * @protocol: GUID of the protocol
2798 * @protocol_interface: interface implementing the protocol
2799 * @agent_handle: handle of the driver
2800 * @controller_handle: handle of the controller
2801 * @attributes: attributes indicating how to open the protocol
2803 * This function implements the OpenProtocol interface.
2805 * See the Unified Extensible Firmware Interface (UEFI) specification for
2808 * Return: status code
2810 static efi_status_t EFIAPI efi_open_protocol
2811 (efi_handle_t handle, const efi_guid_t *protocol,
2812 void **protocol_interface, efi_handle_t agent_handle,
2813 efi_handle_t controller_handle, uint32_t attributes)
2815 struct efi_handler *handler;
2816 efi_status_t r = EFI_INVALID_PARAMETER;
2818 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2819 protocol_interface, agent_handle, controller_handle,
2822 if (!handle || !protocol ||
2823 (!protocol_interface && attributes !=
2824 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2828 switch (attributes) {
2829 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2830 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2831 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2833 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2834 if (controller_handle == handle)
2837 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2838 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2839 /* Check that the controller handle is valid */
2840 if (!efi_search_obj(controller_handle))
2843 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2844 /* Check that the agent handle is valid */
2845 if (!efi_search_obj(agent_handle))
2852 r = efi_search_protocol(handle, protocol, &handler);
2857 r = EFI_UNSUPPORTED;
2863 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2864 controller_handle, attributes);
2870 * efi_start_image() - call the entry point of an image
2871 * @image_handle: handle of the image
2872 * @exit_data_size: size of the buffer
2873 * @exit_data: buffer to receive the exit data of the called image
2875 * This function implements the StartImage service.
2877 * See the Unified Extensible Firmware Interface (UEFI) specification for
2880 * Return: status code
2882 efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2883 efi_uintn_t *exit_data_size,
2886 struct efi_loaded_image_obj *image_obj =
2887 (struct efi_loaded_image_obj *)image_handle;
2890 efi_handle_t parent_image = current_image;
2892 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2894 if (!efi_search_obj(image_handle))
2895 return EFI_EXIT(EFI_INVALID_PARAMETER);
2897 /* Check parameters */
2898 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2899 return EFI_EXIT(EFI_INVALID_PARAMETER);
2901 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
2902 return EFI_EXIT(EFI_SECURITY_VIOLATION);
2904 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2906 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2907 if (ret != EFI_SUCCESS)
2908 return EFI_EXIT(EFI_INVALID_PARAMETER);
2910 image_obj->exit_data_size = exit_data_size;
2911 image_obj->exit_data = exit_data;
2913 /* call the image! */
2914 if (setjmp(&image_obj->exit_jmp)) {
2916 * We called the entry point of the child image with EFI_CALL
2917 * in the lines below. The child image called the Exit() boot
2918 * service efi_exit() which executed the long jump that brought
2919 * us to the current line. This implies that the second half
2920 * of the EFI_CALL macro has not been executed.
2922 #if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
2924 * efi_exit() called efi_restore_gd(). We have to undo this
2925 * otherwise __efi_entry_check() will put the wrong value into
2931 * To get ready to call EFI_EXIT below we have to execute the
2932 * missed out steps of EFI_CALL.
2934 assert(__efi_entry_check());
2935 EFI_PRINT("%lu returned by started image\n",
2936 (unsigned long)((uintptr_t)image_obj->exit_status &
2938 current_image = parent_image;
2939 return EFI_EXIT(image_obj->exit_status);
2942 current_image = image_handle;
2943 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
2944 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
2945 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2948 * Control is returned from a started UEFI image either by calling
2949 * Exit() (where exit data can be provided) or by simply returning from
2950 * the entry point. In the latter case call Exit() on behalf of the
2953 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2957 * efi_delete_image() - delete loaded image from memory)
2959 * @image_obj: handle of the loaded image
2960 * @loaded_image_protocol: loaded image protocol
2962 static efi_status_t efi_delete_image
2963 (struct efi_loaded_image_obj *image_obj,
2964 struct efi_loaded_image *loaded_image_protocol)
2966 struct efi_object *efiobj;
2967 efi_status_t r, ret = EFI_SUCCESS;
2970 list_for_each_entry(efiobj, &efi_obj_list, link) {
2971 struct efi_handler *protocol;
2973 list_for_each_entry(protocol, &efiobj->protocols, link) {
2974 struct efi_open_protocol_info_item *info;
2976 list_for_each_entry(info, &protocol->open_infos, link) {
2977 if (info->info.agent_handle !=
2978 (efi_handle_t)image_obj)
2980 r = EFI_CALL(efi_close_protocol
2981 (efiobj, protocol->guid,
2982 info->info.agent_handle,
2983 info->info.controller_handle
2985 if (r != EFI_SUCCESS)
2988 * Closing protocols may results in further
2989 * items being deleted. To play it safe loop
2990 * over all elements again.
2997 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2998 efi_size_in_pages(loaded_image_protocol->image_size));
2999 efi_delete_handle(&image_obj->header);
3005 * efi_unload_image() - unload an EFI image
3006 * @image_handle: handle of the image to be unloaded
3008 * This function implements the UnloadImage service.
3010 * See the Unified Extensible Firmware Interface (UEFI) specification for
3013 * Return: status code
3015 efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3017 efi_status_t ret = EFI_SUCCESS;
3018 struct efi_object *efiobj;
3019 struct efi_loaded_image *loaded_image_protocol;
3021 EFI_ENTRY("%p", image_handle);
3023 efiobj = efi_search_obj(image_handle);
3025 ret = EFI_INVALID_PARAMETER;
3028 /* Find the loaded image protocol */
3029 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3030 (void **)&loaded_image_protocol,
3032 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3033 if (ret != EFI_SUCCESS) {
3034 ret = EFI_INVALID_PARAMETER;
3037 switch (efiobj->type) {
3038 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3039 /* Call the unload function */
3040 if (!loaded_image_protocol->unload) {
3041 ret = EFI_UNSUPPORTED;
3044 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3045 if (ret != EFI_SUCCESS)
3048 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3051 ret = EFI_INVALID_PARAMETER;
3054 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3055 loaded_image_protocol);
3057 return EFI_EXIT(ret);
3061 * efi_update_exit_data() - fill exit data parameters of StartImage()
3063 * @image_obj: image handle
3064 * @exit_data_size: size of the exit data buffer
3065 * @exit_data: buffer with data returned by UEFI payload
3066 * Return: status code
3068 static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3069 efi_uintn_t exit_data_size,
3075 * If exit_data is not provided to StartImage(), exit_data_size must be
3078 if (!image_obj->exit_data)
3080 if (image_obj->exit_data_size)
3081 *image_obj->exit_data_size = exit_data_size;
3082 if (exit_data_size && exit_data) {
3083 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3085 (void **)image_obj->exit_data);
3086 if (ret != EFI_SUCCESS)
3088 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3090 image_obj->exit_data = NULL;
3096 * efi_exit() - leave an EFI application or driver
3097 * @image_handle: handle of the application or driver that is exiting
3098 * @exit_status: status code
3099 * @exit_data_size: size of the buffer in bytes
3100 * @exit_data: buffer with data describing an error
3102 * This function implements the Exit service.
3104 * See the Unified Extensible Firmware Interface (UEFI) specification for
3107 * Return: status code
3109 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3110 efi_status_t exit_status,
3111 efi_uintn_t exit_data_size,
3115 * TODO: We should call the unload procedure of the loaded
3119 struct efi_loaded_image *loaded_image_protocol;
3120 struct efi_loaded_image_obj *image_obj =
3121 (struct efi_loaded_image_obj *)image_handle;
3123 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3124 exit_data_size, exit_data);
3126 /* Check parameters */
3127 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3128 (void **)&loaded_image_protocol,
3130 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3131 if (ret != EFI_SUCCESS) {
3132 ret = EFI_INVALID_PARAMETER;
3136 /* Unloading of unstarted images */
3137 switch (image_obj->header.type) {
3138 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3140 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3141 efi_delete_image(image_obj, loaded_image_protocol);
3145 /* Handle does not refer to loaded image */
3146 ret = EFI_INVALID_PARAMETER;
3149 /* A started image can only be unloaded it is the last one started. */
3150 if (image_handle != current_image) {
3151 ret = EFI_INVALID_PARAMETER;
3155 /* Exit data is only foreseen in case of failure. */
3156 if (exit_status != EFI_SUCCESS) {
3157 ret = efi_update_exit_data(image_obj, exit_data_size,
3159 /* Exiting has priority. Don't return error to caller. */
3160 if (ret != EFI_SUCCESS)
3161 EFI_PRINT("%s: out of memory\n", __func__);
3163 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3164 exit_status != EFI_SUCCESS)
3165 efi_delete_image(image_obj, loaded_image_protocol);
3167 /* Make sure entry/exit counts for EFI world cross-overs match */
3168 EFI_EXIT(exit_status);
3171 * But longjmp out with the U-Boot gd, not the application's, as
3172 * the other end is a setjmp call inside EFI context.
3176 image_obj->exit_status = exit_status;
3177 longjmp(&image_obj->exit_jmp, 1);
3179 panic("EFI application exited");
3181 return EFI_EXIT(ret);
3185 * efi_handle_protocol() - get interface of a protocol on a handle
3186 * @handle: handle on which the protocol shall be opened
3187 * @protocol: GUID of the protocol
3188 * @protocol_interface: interface implementing the protocol
3190 * This function implements the HandleProtocol service.
3192 * See the Unified Extensible Firmware Interface (UEFI) specification for
3195 * Return: status code
3197 efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3198 const efi_guid_t *protocol,
3199 void **protocol_interface)
3201 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
3202 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
3206 * efi_bind_controller() - bind a single driver to a controller
3207 * @controller_handle: controller handle
3208 * @driver_image_handle: driver handle
3209 * @remain_device_path: remaining path
3211 * Return: status code
3213 static efi_status_t efi_bind_controller(
3214 efi_handle_t controller_handle,
3215 efi_handle_t driver_image_handle,
3216 struct efi_device_path *remain_device_path)
3218 struct efi_driver_binding_protocol *binding_protocol;
3221 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3222 &efi_guid_driver_binding_protocol,
3223 (void **)&binding_protocol,
3224 driver_image_handle, NULL,
3225 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3226 if (r != EFI_SUCCESS)
3228 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3230 remain_device_path));
3231 if (r == EFI_SUCCESS)
3232 r = EFI_CALL(binding_protocol->start(binding_protocol,
3234 remain_device_path));
3235 EFI_CALL(efi_close_protocol(driver_image_handle,
3236 &efi_guid_driver_binding_protocol,
3237 driver_image_handle, NULL));
3242 * efi_connect_single_controller() - connect a single driver to a controller
3243 * @controller_handle: controller
3244 * @driver_image_handle: driver
3245 * @remain_device_path: remaining path
3247 * Return: status code
3249 static efi_status_t efi_connect_single_controller(
3250 efi_handle_t controller_handle,
3251 efi_handle_t *driver_image_handle,
3252 struct efi_device_path *remain_device_path)
3254 efi_handle_t *buffer;
3258 size_t connected = 0;
3260 /* Get buffer with all handles with driver binding protocol */
3261 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3262 &efi_guid_driver_binding_protocol,
3263 NULL, &count, &buffer));
3264 if (r != EFI_SUCCESS)
3267 /* Context Override */
3268 if (driver_image_handle) {
3269 for (; *driver_image_handle; ++driver_image_handle) {
3270 for (i = 0; i < count; ++i) {
3271 if (buffer[i] == *driver_image_handle) {
3273 r = efi_bind_controller(
3275 *driver_image_handle,
3276 remain_device_path);
3278 * For drivers that do not support the
3279 * controller or are already connected
3280 * we receive an error code here.
3282 if (r == EFI_SUCCESS)
3290 * TODO: Some overrides are not yet implemented:
3291 * - Platform Driver Override
3292 * - Driver Family Override Search
3293 * - Bus Specific Driver Override
3296 /* Driver Binding Search */
3297 for (i = 0; i < count; ++i) {
3299 r = efi_bind_controller(controller_handle,
3301 remain_device_path);
3302 if (r == EFI_SUCCESS)
3307 efi_free_pool(buffer);
3309 return EFI_NOT_FOUND;
3314 * efi_connect_controller() - connect a controller to a driver
3315 * @controller_handle: handle of the controller
3316 * @driver_image_handle: handle of the driver
3317 * @remain_device_path: device path of a child controller
3318 * @recursive: true to connect all child controllers
3320 * This function implements the ConnectController service.
3322 * See the Unified Extensible Firmware Interface (UEFI) specification for
3325 * First all driver binding protocol handles are tried for binding drivers.
3326 * Afterwards all handles that have opened a protocol of the controller
3327 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3329 * Return: status code
3331 static efi_status_t EFIAPI efi_connect_controller(
3332 efi_handle_t controller_handle,
3333 efi_handle_t *driver_image_handle,
3334 struct efi_device_path *remain_device_path,
3338 efi_status_t ret = EFI_NOT_FOUND;
3339 struct efi_object *efiobj;
3341 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
3342 remain_device_path, recursive);
3344 efiobj = efi_search_obj(controller_handle);
3346 ret = EFI_INVALID_PARAMETER;
3350 r = efi_connect_single_controller(controller_handle,
3351 driver_image_handle,
3352 remain_device_path);
3353 if (r == EFI_SUCCESS)
3356 struct efi_handler *handler;
3357 struct efi_open_protocol_info_item *item;
3359 list_for_each_entry(handler, &efiobj->protocols, link) {
3360 list_for_each_entry(item, &handler->open_infos, link) {
3361 if (item->info.attributes &
3362 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3363 r = EFI_CALL(efi_connect_controller(
3364 item->info.controller_handle,
3365 driver_image_handle,
3368 if (r == EFI_SUCCESS)
3374 /* Check for child controller specified by end node */
3375 if (ret != EFI_SUCCESS && remain_device_path &&
3376 remain_device_path->type == DEVICE_PATH_TYPE_END)
3379 return EFI_EXIT(ret);
3383 * efi_reinstall_protocol_interface() - reinstall protocol interface
3384 * @handle: handle on which the protocol shall be reinstalled
3385 * @protocol: GUID of the protocol to be installed
3386 * @old_interface: interface to be removed
3387 * @new_interface: interface to be installed
3389 * This function implements the ReinstallProtocolInterface service.
3391 * See the Unified Extensible Firmware Interface (UEFI) specification for
3394 * The old interface is uninstalled. The new interface is installed.
3395 * Drivers are connected.
3397 * Return: status code
3399 static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3400 efi_handle_t handle, const efi_guid_t *protocol,
3401 void *old_interface, void *new_interface)
3405 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3408 /* Uninstall protocol but do not delete handle */
3409 ret = efi_uninstall_protocol(handle, protocol, old_interface);
3410 if (ret != EFI_SUCCESS)
3413 /* Install the new protocol */
3414 ret = efi_add_protocol(handle, protocol, new_interface);
3416 * The UEFI spec does not specify what should happen to the handle
3417 * if in case of an error no protocol interface remains on the handle.
3418 * So let's do nothing here.
3420 if (ret != EFI_SUCCESS)
3423 * The returned status code has to be ignored.
3424 * Do not create an error if no suitable driver for the handle exists.
3426 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3428 return EFI_EXIT(ret);
3432 * efi_get_child_controllers() - get all child controllers associated to a driver
3433 * @efiobj: handle of the controller
3434 * @driver_handle: handle of the driver
3435 * @number_of_children: number of child controllers
3436 * @child_handle_buffer: handles of the the child controllers
3438 * The allocated buffer has to be freed with free().
3440 * Return: status code
3442 static efi_status_t efi_get_child_controllers(
3443 struct efi_object *efiobj,
3444 efi_handle_t driver_handle,
3445 efi_uintn_t *number_of_children,
3446 efi_handle_t **child_handle_buffer)
3448 struct efi_handler *handler;
3449 struct efi_open_protocol_info_item *item;
3450 efi_uintn_t count = 0, i;
3453 /* Count all child controller associations */
3454 list_for_each_entry(handler, &efiobj->protocols, link) {
3455 list_for_each_entry(item, &handler->open_infos, link) {
3456 if (item->info.agent_handle == driver_handle &&
3457 item->info.attributes &
3458 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3463 * Create buffer. In case of duplicate child controller assignments
3464 * the buffer will be too large. But that does not harm.
3466 *number_of_children = 0;
3469 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3470 if (!*child_handle_buffer)
3471 return EFI_OUT_OF_RESOURCES;
3472 /* Copy unique child handles */
3473 list_for_each_entry(handler, &efiobj->protocols, link) {
3474 list_for_each_entry(item, &handler->open_infos, link) {
3475 if (item->info.agent_handle == driver_handle &&
3476 item->info.attributes &
3477 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3478 /* Check this is a new child controller */
3480 for (i = 0; i < *number_of_children; ++i) {
3481 if ((*child_handle_buffer)[i] ==
3482 item->info.controller_handle)
3485 /* Copy handle to buffer */
3487 i = (*number_of_children)++;
3488 (*child_handle_buffer)[i] =
3489 item->info.controller_handle;
3498 * efi_disconnect_controller() - disconnect a controller from a driver
3499 * @controller_handle: handle of the controller
3500 * @driver_image_handle: handle of the driver
3501 * @child_handle: handle of the child to destroy
3503 * This function implements the DisconnectController service.
3505 * See the Unified Extensible Firmware Interface (UEFI) specification for
3508 * Return: status code
3510 static efi_status_t EFIAPI efi_disconnect_controller(
3511 efi_handle_t controller_handle,
3512 efi_handle_t driver_image_handle,
3513 efi_handle_t child_handle)
3515 struct efi_driver_binding_protocol *binding_protocol;
3516 efi_handle_t *child_handle_buffer = NULL;
3517 size_t number_of_children = 0;
3519 struct efi_object *efiobj;
3521 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3524 efiobj = efi_search_obj(controller_handle);
3526 r = EFI_INVALID_PARAMETER;
3530 if (child_handle && !efi_search_obj(child_handle)) {
3531 r = EFI_INVALID_PARAMETER;
3535 /* If no driver handle is supplied, disconnect all drivers */
3536 if (!driver_image_handle) {
3537 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3541 /* Create list of child handles */
3543 number_of_children = 1;
3544 child_handle_buffer = &child_handle;
3546 r = efi_get_child_controllers(efiobj,
3547 driver_image_handle,
3548 &number_of_children,
3549 &child_handle_buffer);
3550 if (r != EFI_SUCCESS)
3554 /* Get the driver binding protocol */
3555 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3556 &efi_guid_driver_binding_protocol,
3557 (void **)&binding_protocol,
3558 driver_image_handle, NULL,
3559 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3560 if (r != EFI_SUCCESS) {
3561 r = EFI_INVALID_PARAMETER;
3564 /* Remove the children */
3565 if (number_of_children) {
3566 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3569 child_handle_buffer));
3570 if (r != EFI_SUCCESS) {
3571 r = EFI_DEVICE_ERROR;
3575 /* Remove the driver */
3576 if (!child_handle) {
3577 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3580 if (r != EFI_SUCCESS) {
3581 r = EFI_DEVICE_ERROR;
3585 EFI_CALL(efi_close_protocol(driver_image_handle,
3586 &efi_guid_driver_binding_protocol,
3587 driver_image_handle, NULL));
3591 free(child_handle_buffer);
3595 static struct efi_boot_services efi_boot_services = {
3597 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3598 .revision = EFI_SPECIFICATION_VERSION,
3599 .headersize = sizeof(struct efi_boot_services),
3601 .raise_tpl = efi_raise_tpl,
3602 .restore_tpl = efi_restore_tpl,
3603 .allocate_pages = efi_allocate_pages_ext,
3604 .free_pages = efi_free_pages_ext,
3605 .get_memory_map = efi_get_memory_map_ext,
3606 .allocate_pool = efi_allocate_pool_ext,
3607 .free_pool = efi_free_pool_ext,
3608 .create_event = efi_create_event_ext,
3609 .set_timer = efi_set_timer_ext,
3610 .wait_for_event = efi_wait_for_event,
3611 .signal_event = efi_signal_event_ext,
3612 .close_event = efi_close_event,
3613 .check_event = efi_check_event,
3614 .install_protocol_interface = efi_install_protocol_interface,
3615 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
3616 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
3617 .handle_protocol = efi_handle_protocol,
3619 .register_protocol_notify = efi_register_protocol_notify,
3620 .locate_handle = efi_locate_handle_ext,
3621 .locate_device_path = efi_locate_device_path,
3622 .install_configuration_table = efi_install_configuration_table_ext,
3623 .load_image = efi_load_image,
3624 .start_image = efi_start_image,
3626 .unload_image = efi_unload_image,
3627 .exit_boot_services = efi_exit_boot_services,
3628 .get_next_monotonic_count = efi_get_next_monotonic_count,
3630 .set_watchdog_timer = efi_set_watchdog_timer,
3631 .connect_controller = efi_connect_controller,
3632 .disconnect_controller = efi_disconnect_controller,
3633 .open_protocol = efi_open_protocol,
3634 .close_protocol = efi_close_protocol,
3635 .open_protocol_information = efi_open_protocol_information,
3636 .protocols_per_handle = efi_protocols_per_handle,
3637 .locate_handle_buffer = efi_locate_handle_buffer,
3638 .locate_protocol = efi_locate_protocol,
3639 .install_multiple_protocol_interfaces =
3640 efi_install_multiple_protocol_interfaces,
3641 .uninstall_multiple_protocol_interfaces =
3642 efi_uninstall_multiple_protocol_interfaces,
3643 .calculate_crc32 = efi_calculate_crc32,
3644 .copy_mem = efi_copy_mem,
3645 .set_mem = efi_set_mem,
3646 .create_event_ex = efi_create_event_ex,
3649 static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
3651 struct efi_system_table __efi_runtime_data systab = {
3653 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
3654 .revision = EFI_SPECIFICATION_VERSION,
3655 .headersize = sizeof(struct efi_system_table),
3657 .fw_vendor = firmware_vendor,
3658 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
3659 .runtime = &efi_runtime_services,
3665 * efi_initialize_system_table() - Initialize system table
3667 * Return: status code
3669 efi_status_t efi_initialize_system_table(void)
3673 /* Allocate configuration table array */
3674 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3675 EFI_MAX_CONFIGURATION_TABLES *
3676 sizeof(struct efi_configuration_table),
3677 (void **)&systab.tables);
3680 * These entries will be set to NULL in ExitBootServices(). To avoid
3681 * relocation in SetVirtualAddressMap(), set them dynamically.
3683 systab.con_in = &efi_con_in;
3684 systab.con_out = &efi_con_out;
3685 systab.std_err = &efi_con_out;
3686 systab.boottime = &efi_boot_services;
3688 /* Set CRC32 field in table headers */
3689 efi_update_table_header_crc32(&systab.hdr);
3690 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3691 efi_update_table_header_crc32(&efi_boot_services.hdr);