1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application boot time services
5 * Copyright (c) 2016 Alexander Graf
10 #include <efi_loader.h>
11 #include <environment.h>
13 #include <linux/libfdt_env.h>
14 #include <u-boot/crc.h>
18 DECLARE_GLOBAL_DATA_PTR
;
20 /* Task priority level */
21 static efi_uintn_t efi_tpl
= TPL_APPLICATION
;
23 /* This list contains all the EFI objects our payload has access to */
24 LIST_HEAD(efi_obj_list
);
26 /* List of all events */
27 LIST_HEAD(efi_events
);
30 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31 * we need to do trickery with caches. Since we don't want to break the EFI
32 * aware boot path, only apply hacks when loading exiting directly (breaking
33 * direct Linux EFI booting along the way - oh well).
35 static bool efi_is_direct_boot
= true;
39 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
40 * fixed when compiling U-Boot. However, the payload does not know about that
41 * restriction so we need to manually swap its and our view of that register on
42 * EFI callback entry/exit.
44 static volatile void *efi_gd
, *app_gd
;
47 static int entry_count
;
48 static int nesting_level
;
49 /* GUID of the device tree table */
50 const efi_guid_t efi_guid_fdt
= EFI_FDT_GUID
;
51 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
52 const efi_guid_t efi_guid_driver_binding_protocol
=
53 EFI_DRIVER_BINDING_PROTOCOL_GUID
;
55 /* event group ExitBootServices() invoked */
56 const efi_guid_t efi_guid_event_group_exit_boot_services
=
57 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES
;
58 /* event group SetVirtualAddressMap() invoked */
59 const efi_guid_t efi_guid_event_group_virtual_address_change
=
60 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE
;
61 /* event group memory map changed */
62 const efi_guid_t efi_guid_event_group_memory_map_change
=
63 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE
;
64 /* event group boot manager about to boot */
65 const efi_guid_t efi_guid_event_group_ready_to_boot
=
66 EFI_EVENT_GROUP_READY_TO_BOOT
;
67 /* event group ResetSystem() invoked (before ExitBootServices) */
68 const efi_guid_t efi_guid_event_group_reset_system
=
69 EFI_EVENT_GROUP_RESET_SYSTEM
;
71 static efi_status_t EFIAPI
efi_disconnect_controller(
72 efi_handle_t controller_handle
,
73 efi_handle_t driver_image_handle
,
74 efi_handle_t child_handle
);
76 /* Called on every callback entry */
77 int __efi_entry_check(void)
79 int ret
= entry_count
++ == 0;
88 /* Called on every callback exit */
89 int __efi_exit_check(void)
91 int ret
= --entry_count
== 0;
98 /* Called from do_bootefi_exec() */
99 void efi_save_gd(void)
107 * Special case handler for error/abort that just forces things back to u-boot
108 * world so we can dump out an abort message, without any care about returning
109 * back to UEFI world.
111 void efi_restore_gd(void)
114 /* Only restore if we're already in EFI context */
122 * indent_string() - returns a string for indenting with two spaces per level
123 * @level: indent level
125 * A maximum of ten indent levels is supported. Higher indent levels will be
128 * Return: A string for indenting with two spaces per level is
131 static const char *indent_string(int level
)
133 const char *indent
= " ";
134 const int max
= strlen(indent
);
136 level
= min(max
, level
* 2);
137 return &indent
[max
- level
];
140 const char *__efi_nesting(void)
142 return indent_string(nesting_level
);
145 const char *__efi_nesting_inc(void)
147 return indent_string(nesting_level
++);
150 const char *__efi_nesting_dec(void)
152 return indent_string(--nesting_level
);
156 * efi_queue_event() - queue an EFI event
157 * @event: event to signal
158 * @check_tpl: check the TPL level
160 * This function queues the notification function of the event for future
163 * The notification function is called if the task priority level of the event
164 * is higher than the current task priority level.
166 * For the SignalEvent service see efi_signal_event_ext.
169 static void efi_queue_event(struct efi_event
*event
, bool check_tpl
)
171 if (event
->notify_function
) {
172 event
->is_queued
= true;
174 if (check_tpl
&& efi_tpl
>= event
->notify_tpl
)
176 EFI_CALL_VOID(event
->notify_function(event
,
177 event
->notify_context
));
179 event
->is_queued
= false;
183 * is_valid_tpl() - check if the task priority level is valid
185 * @tpl: TPL level to check
186 * Return: status code
188 efi_status_t
is_valid_tpl(efi_uintn_t tpl
)
191 case TPL_APPLICATION
:
197 return EFI_INVALID_PARAMETER
;
202 * efi_signal_event() - signal an EFI event
203 * @event: event to signal
204 * @check_tpl: check the TPL level
206 * This function signals an event. If the event belongs to an event group all
207 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
208 * their notification function is queued.
210 * For the SignalEvent service see efi_signal_event_ext.
212 void efi_signal_event(struct efi_event
*event
, bool check_tpl
)
215 struct efi_event
*evt
;
218 * The signaled state has to set before executing any
219 * notification function
221 list_for_each_entry(evt
, &efi_events
, link
) {
222 if (!evt
->group
|| guidcmp(evt
->group
, event
->group
))
224 if (evt
->is_signaled
)
226 evt
->is_signaled
= true;
227 if (evt
->type
& EVT_NOTIFY_SIGNAL
&&
228 evt
->notify_function
)
229 evt
->is_queued
= true;
231 list_for_each_entry(evt
, &efi_events
, link
) {
232 if (!evt
->group
|| guidcmp(evt
->group
, event
->group
))
235 efi_queue_event(evt
, check_tpl
);
237 } else if (!event
->is_signaled
) {
238 event
->is_signaled
= true;
239 if (event
->type
& EVT_NOTIFY_SIGNAL
)
240 efi_queue_event(event
, check_tpl
);
245 * efi_raise_tpl() - raise the task priority level
246 * @new_tpl: new value of the task priority level
248 * This function implements the RaiseTpl service.
250 * See the Unified Extensible Firmware Interface (UEFI) specification for
253 * Return: old value of the task priority level
255 static unsigned long EFIAPI
efi_raise_tpl(efi_uintn_t new_tpl
)
257 efi_uintn_t old_tpl
= efi_tpl
;
259 EFI_ENTRY("0x%zx", new_tpl
);
261 if (new_tpl
< efi_tpl
)
262 debug("WARNING: new_tpl < current_tpl in %s\n", __func__
);
264 if (efi_tpl
> TPL_HIGH_LEVEL
)
265 efi_tpl
= TPL_HIGH_LEVEL
;
267 EFI_EXIT(EFI_SUCCESS
);
272 * efi_restore_tpl() - lower the task priority level
273 * @old_tpl: value of the task priority level to be restored
275 * This function implements the RestoreTpl service.
277 * See the Unified Extensible Firmware Interface (UEFI) specification for
280 static void EFIAPI
efi_restore_tpl(efi_uintn_t old_tpl
)
282 EFI_ENTRY("0x%zx", old_tpl
);
284 if (old_tpl
> efi_tpl
)
285 debug("WARNING: old_tpl > current_tpl in %s\n", __func__
);
287 if (efi_tpl
> TPL_HIGH_LEVEL
)
288 efi_tpl
= TPL_HIGH_LEVEL
;
291 * Lowering the TPL may have made queued events eligible for execution.
295 EFI_EXIT(EFI_SUCCESS
);
299 * efi_allocate_pages_ext() - allocate memory pages
300 * @type: type of allocation to be performed
301 * @memory_type: usage type of the allocated memory
302 * @pages: number of pages to be allocated
303 * @memory: allocated memory
305 * This function implements the AllocatePages service.
307 * See the Unified Extensible Firmware Interface (UEFI) specification for
310 * Return: status code
312 static efi_status_t EFIAPI
efi_allocate_pages_ext(int type
, int memory_type
,
318 EFI_ENTRY("%d, %d, 0x%zx, %p", type
, memory_type
, pages
, memory
);
319 r
= efi_allocate_pages(type
, memory_type
, pages
, memory
);
324 * efi_free_pages_ext() - Free memory pages.
325 * @memory: start of the memory area to be freed
326 * @pages: number of pages to be freed
328 * This function implements the FreePages service.
330 * See the Unified Extensible Firmware Interface (UEFI) specification for
333 * Return: status code
335 static efi_status_t EFIAPI
efi_free_pages_ext(uint64_t memory
,
340 EFI_ENTRY("%llx, 0x%zx", memory
, pages
);
341 r
= efi_free_pages(memory
, pages
);
346 * efi_get_memory_map_ext() - get map describing memory usage
347 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
348 * on exit the size of the copied memory map
349 * @memory_map: buffer to which the memory map is written
350 * @map_key: key for the memory map
351 * @descriptor_size: size of an individual memory descriptor
352 * @descriptor_version: version number of the memory descriptor structure
354 * This function implements the GetMemoryMap service.
356 * See the Unified Extensible Firmware Interface (UEFI) specification for
359 * Return: status code
361 static efi_status_t EFIAPI
efi_get_memory_map_ext(
362 efi_uintn_t
*memory_map_size
,
363 struct efi_mem_desc
*memory_map
,
364 efi_uintn_t
*map_key
,
365 efi_uintn_t
*descriptor_size
,
366 uint32_t *descriptor_version
)
370 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size
, memory_map
,
371 map_key
, descriptor_size
, descriptor_version
);
372 r
= efi_get_memory_map(memory_map_size
, memory_map
, map_key
,
373 descriptor_size
, descriptor_version
);
378 * efi_allocate_pool_ext() - allocate memory from pool
379 * @pool_type: type of the pool from which memory is to be allocated
380 * @size: number of bytes to be allocated
381 * @buffer: allocated memory
383 * This function implements the AllocatePool service.
385 * See the Unified Extensible Firmware Interface (UEFI) specification for
388 * Return: status code
390 static efi_status_t EFIAPI
efi_allocate_pool_ext(int pool_type
,
396 EFI_ENTRY("%d, %zd, %p", pool_type
, size
, buffer
);
397 r
= efi_allocate_pool(pool_type
, size
, buffer
);
402 * efi_free_pool_ext() - free memory from pool
403 * @buffer: start of memory to be freed
405 * This function implements the FreePool service.
407 * See the Unified Extensible Firmware Interface (UEFI) specification for
410 * Return: status code
412 static efi_status_t EFIAPI
efi_free_pool_ext(void *buffer
)
416 EFI_ENTRY("%p", buffer
);
417 r
= efi_free_pool(buffer
);
422 * efi_add_handle() - add a new object to the object list
423 * @obj: object to be added
425 * The protocols list is initialized. The object handle is set.
427 void efi_add_handle(efi_handle_t handle
)
431 INIT_LIST_HEAD(&handle
->protocols
);
432 list_add_tail(&handle
->link
, &efi_obj_list
);
436 * efi_create_handle() - create handle
437 * @handle: new handle
439 * Return: status code
441 efi_status_t
efi_create_handle(efi_handle_t
*handle
)
443 struct efi_object
*obj
;
445 obj
= calloc(1, sizeof(struct efi_object
));
447 return EFI_OUT_OF_RESOURCES
;
456 * efi_search_protocol() - find a protocol on a handle.
458 * @protocol_guid: GUID of the protocol
459 * @handler: reference to the protocol
461 * Return: status code
463 efi_status_t
efi_search_protocol(const efi_handle_t handle
,
464 const efi_guid_t
*protocol_guid
,
465 struct efi_handler
**handler
)
467 struct efi_object
*efiobj
;
468 struct list_head
*lhandle
;
470 if (!handle
|| !protocol_guid
)
471 return EFI_INVALID_PARAMETER
;
472 efiobj
= efi_search_obj(handle
);
474 return EFI_INVALID_PARAMETER
;
475 list_for_each(lhandle
, &efiobj
->protocols
) {
476 struct efi_handler
*protocol
;
478 protocol
= list_entry(lhandle
, struct efi_handler
, link
);
479 if (!guidcmp(protocol
->guid
, protocol_guid
)) {
485 return EFI_NOT_FOUND
;
489 * efi_remove_protocol() - delete protocol from a handle
490 * @handle: handle from which the protocol shall be deleted
491 * @protocol: GUID of the protocol to be deleted
492 * @protocol_interface: interface of the protocol implementation
494 * Return: status code
496 efi_status_t
efi_remove_protocol(const efi_handle_t handle
,
497 const efi_guid_t
*protocol
,
498 void *protocol_interface
)
500 struct efi_handler
*handler
;
503 ret
= efi_search_protocol(handle
, protocol
, &handler
);
504 if (ret
!= EFI_SUCCESS
)
506 if (guidcmp(handler
->guid
, protocol
))
507 return EFI_INVALID_PARAMETER
;
508 if (handler
->protocol_interface
!= protocol_interface
)
509 return EFI_INVALID_PARAMETER
;
510 list_del(&handler
->link
);
516 * efi_remove_all_protocols() - delete all protocols from a handle
517 * @handle: handle from which the protocols shall be deleted
519 * Return: status code
521 efi_status_t
efi_remove_all_protocols(const efi_handle_t handle
)
523 struct efi_object
*efiobj
;
524 struct efi_handler
*protocol
;
525 struct efi_handler
*pos
;
527 efiobj
= efi_search_obj(handle
);
529 return EFI_INVALID_PARAMETER
;
530 list_for_each_entry_safe(protocol
, pos
, &efiobj
->protocols
, link
) {
533 ret
= efi_remove_protocol(handle
, protocol
->guid
,
534 protocol
->protocol_interface
);
535 if (ret
!= EFI_SUCCESS
)
542 * efi_delete_handle() - delete handle
544 * @obj: handle to delete
546 void efi_delete_handle(efi_handle_t handle
)
550 efi_remove_all_protocols(handle
);
551 list_del(&handle
->link
);
556 * efi_is_event() - check if a pointer is a valid event
557 * @event: pointer to check
559 * Return: status code
561 static efi_status_t
efi_is_event(const struct efi_event
*event
)
563 const struct efi_event
*evt
;
566 return EFI_INVALID_PARAMETER
;
567 list_for_each_entry(evt
, &efi_events
, link
) {
571 return EFI_INVALID_PARAMETER
;
575 * efi_create_event() - create an event
576 * @type: type of the event to create
577 * @notify_tpl: task priority level of the event
578 * @notify_function: notification function of the event
579 * @notify_context: pointer passed to the notification function
580 * @group: event group
581 * @event: created event
583 * This function is used inside U-Boot code to create an event.
585 * For the API function implementing the CreateEvent service see
586 * efi_create_event_ext.
588 * Return: status code
590 efi_status_t
efi_create_event(uint32_t type
, efi_uintn_t notify_tpl
,
591 void (EFIAPI
*notify_function
) (
592 struct efi_event
*event
,
594 void *notify_context
, efi_guid_t
*group
,
595 struct efi_event
**event
)
597 struct efi_event
*evt
;
600 return EFI_INVALID_PARAMETER
;
605 case EVT_NOTIFY_SIGNAL
:
606 case EVT_TIMER
| EVT_NOTIFY_SIGNAL
:
607 case EVT_NOTIFY_WAIT
:
608 case EVT_TIMER
| EVT_NOTIFY_WAIT
:
609 case EVT_SIGNAL_EXIT_BOOT_SERVICES
:
610 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
:
613 return EFI_INVALID_PARAMETER
;
616 if ((type
& (EVT_NOTIFY_WAIT
| EVT_NOTIFY_SIGNAL
)) &&
617 (is_valid_tpl(notify_tpl
) != EFI_SUCCESS
))
618 return EFI_INVALID_PARAMETER
;
620 evt
= calloc(1, sizeof(struct efi_event
));
622 return EFI_OUT_OF_RESOURCES
;
624 evt
->notify_tpl
= notify_tpl
;
625 evt
->notify_function
= notify_function
;
626 evt
->notify_context
= notify_context
;
628 /* Disable timers on boot up */
629 evt
->trigger_next
= -1ULL;
630 evt
->is_queued
= false;
631 evt
->is_signaled
= false;
632 list_add_tail(&evt
->link
, &efi_events
);
638 * efi_create_event_ex() - create an event in a group
639 * @type: type of the event to create
640 * @notify_tpl: task priority level of the event
641 * @notify_function: notification function of the event
642 * @notify_context: pointer passed to the notification function
643 * @event: created event
644 * @event_group: event group
646 * This function implements the CreateEventEx service.
648 * See the Unified Extensible Firmware Interface (UEFI) specification for
651 * Return: status code
653 efi_status_t EFIAPI
efi_create_event_ex(uint32_t type
, efi_uintn_t notify_tpl
,
654 void (EFIAPI
*notify_function
) (
655 struct efi_event
*event
,
657 void *notify_context
,
658 efi_guid_t
*event_group
,
659 struct efi_event
**event
)
661 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type
, notify_tpl
, notify_function
,
662 notify_context
, event_group
);
663 return EFI_EXIT(efi_create_event(type
, notify_tpl
, notify_function
,
664 notify_context
, event_group
, event
));
668 * efi_create_event_ext() - create an event
669 * @type: type of the event to create
670 * @notify_tpl: task priority level of the event
671 * @notify_function: notification function of the event
672 * @notify_context: pointer passed to the notification function
673 * @event: created event
675 * This function implements the CreateEvent service.
677 * See the Unified Extensible Firmware Interface (UEFI) specification for
680 * Return: status code
682 static efi_status_t EFIAPI
efi_create_event_ext(
683 uint32_t type
, efi_uintn_t notify_tpl
,
684 void (EFIAPI
*notify_function
) (
685 struct efi_event
*event
,
687 void *notify_context
, struct efi_event
**event
)
689 EFI_ENTRY("%d, 0x%zx, %p, %p", type
, notify_tpl
, notify_function
,
691 return EFI_EXIT(efi_create_event(type
, notify_tpl
, notify_function
,
692 notify_context
, NULL
, event
));
696 * efi_timer_check() - check if a timer event has occurred
698 * Check if a timer event has occurred or a queued notification function should
701 * Our timers have to work without interrupts, so we check whenever keyboard
702 * input or disk accesses happen if enough time elapsed for them to fire.
704 void efi_timer_check(void)
706 struct efi_event
*evt
;
707 u64 now
= timer_get_us();
709 list_for_each_entry(evt
, &efi_events
, link
) {
711 efi_queue_event(evt
, true);
712 if (!(evt
->type
& EVT_TIMER
) || now
< evt
->trigger_next
)
714 switch (evt
->trigger_type
) {
715 case EFI_TIMER_RELATIVE
:
716 evt
->trigger_type
= EFI_TIMER_STOP
;
718 case EFI_TIMER_PERIODIC
:
719 evt
->trigger_next
+= evt
->trigger_time
;
724 evt
->is_signaled
= false;
725 efi_signal_event(evt
, true);
731 * efi_set_timer() - set the trigger time for a timer event or stop the event
732 * @event: event for which the timer is set
733 * @type: type of the timer
734 * @trigger_time: trigger period in multiples of 100 ns
736 * This is the function for internal usage in U-Boot. For the API function
737 * implementing the SetTimer service see efi_set_timer_ext.
739 * Return: status code
741 efi_status_t
efi_set_timer(struct efi_event
*event
, enum efi_timer_delay type
,
742 uint64_t trigger_time
)
744 /* Check that the event is valid */
745 if (efi_is_event(event
) != EFI_SUCCESS
|| !(event
->type
& EVT_TIMER
))
746 return EFI_INVALID_PARAMETER
;
749 * The parameter defines a multiple of 100 ns.
750 * We use multiples of 1000 ns. So divide by 10.
752 do_div(trigger_time
, 10);
756 event
->trigger_next
= -1ULL;
758 case EFI_TIMER_PERIODIC
:
759 case EFI_TIMER_RELATIVE
:
760 event
->trigger_next
= timer_get_us() + trigger_time
;
763 return EFI_INVALID_PARAMETER
;
765 event
->trigger_type
= type
;
766 event
->trigger_time
= trigger_time
;
767 event
->is_signaled
= false;
772 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
774 * @event: event for which the timer is set
775 * @type: type of the timer
776 * @trigger_time: trigger period in multiples of 100 ns
778 * This function implements the SetTimer service.
780 * See the Unified Extensible Firmware Interface (UEFI) specification for
784 * Return: status code
786 static efi_status_t EFIAPI
efi_set_timer_ext(struct efi_event
*event
,
787 enum efi_timer_delay type
,
788 uint64_t trigger_time
)
790 EFI_ENTRY("%p, %d, %llx", event
, type
, trigger_time
);
791 return EFI_EXIT(efi_set_timer(event
, type
, trigger_time
));
795 * efi_wait_for_event() - wait for events to be signaled
796 * @num_events: number of events to be waited for
797 * @event: events to be waited for
798 * @index: index of the event that was signaled
800 * This function implements the WaitForEvent service.
802 * See the Unified Extensible Firmware Interface (UEFI) specification for
805 * Return: status code
807 static efi_status_t EFIAPI
efi_wait_for_event(efi_uintn_t num_events
,
808 struct efi_event
**event
,
813 EFI_ENTRY("%zd, %p, %p", num_events
, event
, index
);
815 /* Check parameters */
816 if (!num_events
|| !event
)
817 return EFI_EXIT(EFI_INVALID_PARAMETER
);
819 if (efi_tpl
!= TPL_APPLICATION
)
820 return EFI_EXIT(EFI_UNSUPPORTED
);
821 for (i
= 0; i
< num_events
; ++i
) {
822 if (efi_is_event(event
[i
]) != EFI_SUCCESS
)
823 return EFI_EXIT(EFI_INVALID_PARAMETER
);
824 if (!event
[i
]->type
|| event
[i
]->type
& EVT_NOTIFY_SIGNAL
)
825 return EFI_EXIT(EFI_INVALID_PARAMETER
);
826 if (!event
[i
]->is_signaled
)
827 efi_queue_event(event
[i
], true);
830 /* Wait for signal */
832 for (i
= 0; i
< num_events
; ++i
) {
833 if (event
[i
]->is_signaled
)
836 /* Allow events to occur. */
842 * Reset the signal which is passed to the caller to allow periodic
845 event
[i
]->is_signaled
= false;
849 return EFI_EXIT(EFI_SUCCESS
);
853 * efi_signal_event_ext() - signal an EFI event
854 * @event: event to signal
856 * This function implements the SignalEvent service.
858 * See the Unified Extensible Firmware Interface (UEFI) specification for
861 * This functions sets the signaled state of the event and queues the
862 * notification function for execution.
864 * Return: status code
866 static efi_status_t EFIAPI
efi_signal_event_ext(struct efi_event
*event
)
868 EFI_ENTRY("%p", event
);
869 if (efi_is_event(event
) != EFI_SUCCESS
)
870 return EFI_EXIT(EFI_INVALID_PARAMETER
);
871 efi_signal_event(event
, true);
872 return EFI_EXIT(EFI_SUCCESS
);
876 * efi_close_event() - close an EFI event
877 * @event: event to close
879 * This function implements the CloseEvent service.
881 * See the Unified Extensible Firmware Interface (UEFI) specification for
884 * Return: status code
886 static efi_status_t EFIAPI
efi_close_event(struct efi_event
*event
)
888 EFI_ENTRY("%p", event
);
889 if (efi_is_event(event
) != EFI_SUCCESS
)
890 return EFI_EXIT(EFI_INVALID_PARAMETER
);
891 list_del(&event
->link
);
893 return EFI_EXIT(EFI_SUCCESS
);
897 * efi_check_event() - check if an event is signaled
898 * @event: event to check
900 * This function implements the CheckEvent service.
902 * See the Unified Extensible Firmware Interface (UEFI) specification for
905 * If an event is not signaled yet, the notification function is queued. The
906 * signaled state is cleared.
908 * Return: status code
910 static efi_status_t EFIAPI
efi_check_event(struct efi_event
*event
)
912 EFI_ENTRY("%p", event
);
914 if (efi_is_event(event
) != EFI_SUCCESS
||
915 event
->type
& EVT_NOTIFY_SIGNAL
)
916 return EFI_EXIT(EFI_INVALID_PARAMETER
);
917 if (!event
->is_signaled
)
918 efi_queue_event(event
, true);
919 if (event
->is_signaled
) {
920 event
->is_signaled
= false;
921 return EFI_EXIT(EFI_SUCCESS
);
923 return EFI_EXIT(EFI_NOT_READY
);
927 * efi_search_obj() - find the internal EFI object for a handle
928 * @handle: handle to find
932 struct efi_object
*efi_search_obj(const efi_handle_t handle
)
934 struct efi_object
*efiobj
;
936 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
937 if (efiobj
== handle
)
945 * efi_open_protocol_info_entry() - create open protocol info entry and add it
947 * @handler: handler of a protocol
949 * Return: open protocol info entry
951 static struct efi_open_protocol_info_entry
*efi_create_open_info(
952 struct efi_handler
*handler
)
954 struct efi_open_protocol_info_item
*item
;
956 item
= calloc(1, sizeof(struct efi_open_protocol_info_item
));
959 /* Append the item to the open protocol info list. */
960 list_add_tail(&item
->link
, &handler
->open_infos
);
966 * efi_delete_open_info() - remove an open protocol info entry from a protocol
967 * @item: open protocol info entry to delete
969 * Return: status code
971 static efi_status_t
efi_delete_open_info(
972 struct efi_open_protocol_info_item
*item
)
974 list_del(&item
->link
);
980 * efi_add_protocol() - install new protocol on a handle
981 * @handle: handle on which the protocol shall be installed
982 * @protocol: GUID of the protocol to be installed
983 * @protocol_interface: interface of the protocol implementation
985 * Return: status code
987 efi_status_t
efi_add_protocol(const efi_handle_t handle
,
988 const efi_guid_t
*protocol
,
989 void *protocol_interface
)
991 struct efi_object
*efiobj
;
992 struct efi_handler
*handler
;
995 efiobj
= efi_search_obj(handle
);
997 return EFI_INVALID_PARAMETER
;
998 ret
= efi_search_protocol(handle
, protocol
, NULL
);
999 if (ret
!= EFI_NOT_FOUND
)
1000 return EFI_INVALID_PARAMETER
;
1001 handler
= calloc(1, sizeof(struct efi_handler
));
1003 return EFI_OUT_OF_RESOURCES
;
1004 handler
->guid
= protocol
;
1005 handler
->protocol_interface
= protocol_interface
;
1006 INIT_LIST_HEAD(&handler
->open_infos
);
1007 list_add_tail(&handler
->link
, &efiobj
->protocols
);
1008 if (!guidcmp(&efi_guid_device_path
, protocol
))
1009 EFI_PRINT("installed device path '%pD'\n", protocol_interface
);
1014 * efi_install_protocol_interface() - install protocol interface
1015 * @handle: handle on which the protocol shall be installed
1016 * @protocol: GUID of the protocol to be installed
1017 * @protocol_interface_type: type of the interface to be installed,
1018 * always EFI_NATIVE_INTERFACE
1019 * @protocol_interface: interface of the protocol implementation
1021 * This function implements the InstallProtocolInterface service.
1023 * See the Unified Extensible Firmware Interface (UEFI) specification for
1026 * Return: status code
1028 static efi_status_t EFIAPI
efi_install_protocol_interface(
1029 efi_handle_t
*handle
, const efi_guid_t
*protocol
,
1030 int protocol_interface_type
, void *protocol_interface
)
1034 EFI_ENTRY("%p, %pUl, %d, %p", handle
, protocol
, protocol_interface_type
,
1035 protocol_interface
);
1037 if (!handle
|| !protocol
||
1038 protocol_interface_type
!= EFI_NATIVE_INTERFACE
) {
1039 r
= EFI_INVALID_PARAMETER
;
1043 /* Create new handle if requested. */
1045 r
= efi_create_handle(handle
);
1046 if (r
!= EFI_SUCCESS
)
1048 debug("%sEFI: new handle %p\n", indent_string(nesting_level
),
1051 debug("%sEFI: handle %p\n", indent_string(nesting_level
),
1054 /* Add new protocol */
1055 r
= efi_add_protocol(*handle
, protocol
, protocol_interface
);
1061 * efi_get_drivers() - get all drivers associated to a controller
1062 * @handle: handle of the controller
1063 * @protocol: protocol GUID (optional)
1064 * @number_of_drivers: number of child controllers
1065 * @driver_handle_buffer: handles of the the drivers
1067 * The allocated buffer has to be freed with free().
1069 * Return: status code
1071 static efi_status_t
efi_get_drivers(efi_handle_t handle
,
1072 const efi_guid_t
*protocol
,
1073 efi_uintn_t
*number_of_drivers
,
1074 efi_handle_t
**driver_handle_buffer
)
1076 struct efi_handler
*handler
;
1077 struct efi_open_protocol_info_item
*item
;
1078 efi_uintn_t count
= 0, i
;
1081 /* Count all driver associations */
1082 list_for_each_entry(handler
, &handle
->protocols
, link
) {
1083 if (protocol
&& guidcmp(handler
->guid
, protocol
))
1085 list_for_each_entry(item
, &handler
->open_infos
, link
) {
1086 if (item
->info
.attributes
&
1087 EFI_OPEN_PROTOCOL_BY_DRIVER
)
1092 * Create buffer. In case of duplicate driver assignments the buffer
1093 * will be too large. But that does not harm.
1095 *number_of_drivers
= 0;
1096 *driver_handle_buffer
= calloc(count
, sizeof(efi_handle_t
));
1097 if (!*driver_handle_buffer
)
1098 return EFI_OUT_OF_RESOURCES
;
1099 /* Collect unique driver handles */
1100 list_for_each_entry(handler
, &handle
->protocols
, link
) {
1101 if (protocol
&& guidcmp(handler
->guid
, protocol
))
1103 list_for_each_entry(item
, &handler
->open_infos
, link
) {
1104 if (item
->info
.attributes
&
1105 EFI_OPEN_PROTOCOL_BY_DRIVER
) {
1106 /* Check this is a new driver */
1108 for (i
= 0; i
< *number_of_drivers
; ++i
) {
1109 if ((*driver_handle_buffer
)[i
] ==
1110 item
->info
.agent_handle
)
1113 /* Copy handle to buffer */
1115 i
= (*number_of_drivers
)++;
1116 (*driver_handle_buffer
)[i
] =
1117 item
->info
.agent_handle
;
1126 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1127 * @handle: handle of the controller
1128 * @protocol: protocol GUID (optional)
1129 * @child_handle: handle of the child to destroy
1131 * This function implements the DisconnectController service.
1133 * See the Unified Extensible Firmware Interface (UEFI) specification for
1136 * Return: status code
1138 static efi_status_t efi_disconnect_all_drivers
1139 (efi_handle_t handle
,
1140 const efi_guid_t
*protocol
,
1141 efi_handle_t child_handle
)
1143 efi_uintn_t number_of_drivers
;
1144 efi_handle_t
*driver_handle_buffer
;
1145 efi_status_t r
, ret
;
1147 ret
= efi_get_drivers(handle
, protocol
, &number_of_drivers
,
1148 &driver_handle_buffer
);
1149 if (ret
!= EFI_SUCCESS
)
1152 ret
= EFI_NOT_FOUND
;
1153 while (number_of_drivers
) {
1154 r
= EFI_CALL(efi_disconnect_controller(
1156 driver_handle_buffer
[--number_of_drivers
],
1158 if (r
== EFI_SUCCESS
)
1161 free(driver_handle_buffer
);
1166 * efi_uninstall_protocol() - uninstall protocol interface
1168 * @handle: handle from which the protocol shall be removed
1169 * @protocol: GUID of the protocol to be removed
1170 * @protocol_interface: interface to be removed
1172 * This function DOES NOT delete a handle without installed protocol.
1174 * Return: status code
1176 static efi_status_t efi_uninstall_protocol
1177 (efi_handle_t handle
, const efi_guid_t
*protocol
,
1178 void *protocol_interface
)
1180 struct efi_object
*efiobj
;
1181 struct efi_handler
*handler
;
1182 struct efi_open_protocol_info_item
*item
;
1183 struct efi_open_protocol_info_item
*pos
;
1187 efiobj
= efi_search_obj(handle
);
1189 r
= EFI_INVALID_PARAMETER
;
1192 /* Find the protocol on the handle */
1193 r
= efi_search_protocol(handle
, protocol
, &handler
);
1194 if (r
!= EFI_SUCCESS
)
1196 /* Disconnect controllers */
1197 efi_disconnect_all_drivers(efiobj
, protocol
, NULL
);
1198 if (!list_empty(&handler
->open_infos
)) {
1199 r
= EFI_ACCESS_DENIED
;
1202 /* Close protocol */
1203 list_for_each_entry_safe(item
, pos
, &handler
->open_infos
, link
) {
1204 if (item
->info
.attributes
==
1205 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
||
1206 item
->info
.attributes
== EFI_OPEN_PROTOCOL_GET_PROTOCOL
||
1207 item
->info
.attributes
== EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)
1208 list_del(&item
->link
);
1210 if (!list_empty(&handler
->open_infos
)) {
1211 r
= EFI_ACCESS_DENIED
;
1214 r
= efi_remove_protocol(handle
, protocol
, protocol_interface
);
1220 * efi_uninstall_protocol_interface() - uninstall protocol interface
1221 * @handle: handle from which the protocol shall be removed
1222 * @protocol: GUID of the protocol to be removed
1223 * @protocol_interface: interface to be removed
1225 * This function implements the UninstallProtocolInterface service.
1227 * See the Unified Extensible Firmware Interface (UEFI) specification for
1230 * Return: status code
1232 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1233 (efi_handle_t handle
, const efi_guid_t
*protocol
,
1234 void *protocol_interface
)
1238 EFI_ENTRY("%p, %pUl, %p", handle
, protocol
, protocol_interface
);
1240 ret
= efi_uninstall_protocol(handle
, protocol
, protocol_interface
);
1241 if (ret
!= EFI_SUCCESS
)
1244 /* If the last protocol has been removed, delete the handle. */
1245 if (list_empty(&handle
->protocols
)) {
1246 list_del(&handle
->link
);
1250 return EFI_EXIT(ret
);
1254 * efi_register_protocol_notify() - register an event for notification when a
1255 * protocol is installed.
1256 * @protocol: GUID of the protocol whose installation shall be notified
1257 * @event: event to be signaled upon installation of the protocol
1258 * @registration: key for retrieving the registration information
1260 * This function implements the RegisterProtocolNotify service.
1261 * See the Unified Extensible Firmware Interface (UEFI) specification
1264 * Return: status code
1266 static efi_status_t EFIAPI
efi_register_protocol_notify(
1267 const efi_guid_t
*protocol
,
1268 struct efi_event
*event
,
1269 void **registration
)
1271 EFI_ENTRY("%pUl, %p, %p", protocol
, event
, registration
);
1272 return EFI_EXIT(EFI_OUT_OF_RESOURCES
);
1276 * efi_search() - determine if an EFI handle implements a protocol
1277 * @search_type: selection criterion
1278 * @protocol: GUID of the protocol
1279 * @search_key: registration key
1282 * See the documentation of the LocateHandle service in the UEFI specification.
1284 * Return: 0 if the handle implements the protocol
1286 static int efi_search(enum efi_locate_search_type search_type
,
1287 const efi_guid_t
*protocol
, void *search_key
,
1288 efi_handle_t handle
)
1292 switch (search_type
) {
1295 case BY_REGISTER_NOTIFY
:
1296 /* TODO: RegisterProtocolNotify is not implemented yet */
1299 ret
= efi_search_protocol(handle
, protocol
, NULL
);
1300 return (ret
!= EFI_SUCCESS
);
1302 /* Invalid search type */
1308 * efi_locate_handle() - locate handles implementing a protocol
1309 * @search_type: selection criterion
1310 * @protocol: GUID of the protocol
1311 * @search_key: registration key
1312 * @buffer_size: size of the buffer to receive the handles in bytes
1313 * @buffer: buffer to receive the relevant handles
1315 * This function is meant for U-Boot internal calls. For the API implementation
1316 * of the LocateHandle service see efi_locate_handle_ext.
1318 * Return: status code
1320 static efi_status_t
efi_locate_handle(
1321 enum efi_locate_search_type search_type
,
1322 const efi_guid_t
*protocol
, void *search_key
,
1323 efi_uintn_t
*buffer_size
, efi_handle_t
*buffer
)
1325 struct efi_object
*efiobj
;
1326 efi_uintn_t size
= 0;
1328 /* Check parameters */
1329 switch (search_type
) {
1332 case BY_REGISTER_NOTIFY
:
1334 return EFI_INVALID_PARAMETER
;
1335 /* RegisterProtocolNotify is not implemented yet */
1336 return EFI_UNSUPPORTED
;
1339 return EFI_INVALID_PARAMETER
;
1342 return EFI_INVALID_PARAMETER
;
1346 * efi_locate_handle_buffer uses this function for
1347 * the calculation of the necessary buffer size.
1348 * So do not require a buffer for buffersize == 0.
1350 if (!buffer_size
|| (*buffer_size
&& !buffer
))
1351 return EFI_INVALID_PARAMETER
;
1353 /* Count how much space we need */
1354 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
1355 if (!efi_search(search_type
, protocol
, search_key
, efiobj
))
1356 size
+= sizeof(void *);
1359 if (*buffer_size
< size
) {
1360 *buffer_size
= size
;
1361 return EFI_BUFFER_TOO_SMALL
;
1364 *buffer_size
= size
;
1366 return EFI_NOT_FOUND
;
1368 /* Then fill the array */
1369 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
1370 if (!efi_search(search_type
, protocol
, search_key
, efiobj
))
1378 * efi_locate_handle_ext() - locate handles implementing a protocol.
1379 * @search_type: selection criterion
1380 * @protocol: GUID of the protocol
1381 * @search_key: registration key
1382 * @buffer_size: size of the buffer to receive the handles in bytes
1383 * @buffer: buffer to receive the relevant handles
1385 * This function implements the LocateHandle service.
1387 * See the Unified Extensible Firmware Interface (UEFI) specification for
1390 * Return: 0 if the handle implements the protocol
1392 static efi_status_t EFIAPI
efi_locate_handle_ext(
1393 enum efi_locate_search_type search_type
,
1394 const efi_guid_t
*protocol
, void *search_key
,
1395 efi_uintn_t
*buffer_size
, efi_handle_t
*buffer
)
1397 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type
, protocol
, search_key
,
1398 buffer_size
, buffer
);
1400 return EFI_EXIT(efi_locate_handle(search_type
, protocol
, search_key
,
1401 buffer_size
, buffer
));
1405 * efi_remove_configuration_table() - collapses configuration table entries,
1408 * @i: index of the table entry to be removed
1410 static void efi_remove_configuration_table(int i
)
1412 struct efi_configuration_table
*this = &systab
.tables
[i
];
1413 struct efi_configuration_table
*next
= &systab
.tables
[i
+ 1];
1414 struct efi_configuration_table
*end
= &systab
.tables
[systab
.nr_tables
];
1416 memmove(this, next
, (ulong
)end
- (ulong
)next
);
1421 * efi_install_configuration_table() - adds, updates, or removes a
1422 * configuration table
1423 * @guid: GUID of the installed table
1424 * @table: table to be installed
1426 * This function is used for internal calls. For the API implementation of the
1427 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1429 * Return: status code
1431 efi_status_t
efi_install_configuration_table(const efi_guid_t
*guid
,
1434 struct efi_event
*evt
;
1438 return EFI_INVALID_PARAMETER
;
1440 /* Check for GUID override */
1441 for (i
= 0; i
< systab
.nr_tables
; i
++) {
1442 if (!guidcmp(guid
, &systab
.tables
[i
].guid
)) {
1444 systab
.tables
[i
].table
= table
;
1446 efi_remove_configuration_table(i
);
1452 return EFI_NOT_FOUND
;
1454 /* No override, check for overflow */
1455 if (i
>= EFI_MAX_CONFIGURATION_TABLES
)
1456 return EFI_OUT_OF_RESOURCES
;
1458 /* Add a new entry */
1459 memcpy(&systab
.tables
[i
].guid
, guid
, sizeof(*guid
));
1460 systab
.tables
[i
].table
= table
;
1461 systab
.nr_tables
= i
+ 1;
1464 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1465 efi_update_table_header_crc32(&systab
.hdr
);
1467 /* Notify that the configuration table was changed */
1468 list_for_each_entry(evt
, &efi_events
, link
) {
1469 if (evt
->group
&& !guidcmp(evt
->group
, guid
)) {
1470 efi_signal_event(evt
, false);
1479 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1480 * configuration table.
1481 * @guid: GUID of the installed table
1482 * @table: table to be installed
1484 * This function implements the InstallConfigurationTable service.
1486 * See the Unified Extensible Firmware Interface (UEFI) specification for
1489 * Return: status code
1491 static efi_status_t EFIAPI
efi_install_configuration_table_ext(efi_guid_t
*guid
,
1494 EFI_ENTRY("%pUl, %p", guid
, table
);
1495 return EFI_EXIT(efi_install_configuration_table(guid
, table
));
1499 * efi_setup_loaded_image() - initialize a loaded image
1500 * @info: loaded image info to be passed to the entry point of the image
1501 * @obj: internal object associated with the loaded image
1502 * @device_path: device path of the loaded image
1503 * @file_path: file path of the loaded image
1505 * Initialize a loaded_image_info and loaded_image_info object with correct
1506 * protocols, boot-device, etc.
1508 * Return: status code
1510 efi_status_t
efi_setup_loaded_image(struct efi_device_path
*device_path
,
1511 struct efi_device_path
*file_path
,
1512 struct efi_loaded_image_obj
**handle_ptr
,
1513 struct efi_loaded_image
**info_ptr
)
1516 struct efi_loaded_image
*info
;
1517 struct efi_loaded_image_obj
*obj
;
1519 info
= calloc(1, sizeof(*info
));
1521 return EFI_OUT_OF_RESOURCES
;
1522 obj
= calloc(1, sizeof(*obj
));
1525 return EFI_OUT_OF_RESOURCES
;
1528 /* Add internal object to object list */
1529 efi_add_handle(&obj
->header
);
1536 info
->revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
1537 info
->file_path
= file_path
;
1538 info
->system_table
= &systab
;
1541 info
->device_handle
= efi_dp_find_obj(device_path
, NULL
);
1543 * When asking for the device path interface, return
1544 * bootefi_device_path
1546 ret
= efi_add_protocol(&obj
->header
,
1547 &efi_guid_device_path
, device_path
);
1548 if (ret
!= EFI_SUCCESS
)
1553 * When asking for the loaded_image interface, just
1554 * return handle which points to loaded_image_info
1556 ret
= efi_add_protocol(&obj
->header
,
1557 &efi_guid_loaded_image
, info
);
1558 if (ret
!= EFI_SUCCESS
)
1561 ret
= efi_add_protocol(&obj
->header
,
1562 &efi_guid_hii_string_protocol
,
1563 (void *)&efi_hii_string
);
1564 if (ret
!= EFI_SUCCESS
)
1567 ret
= efi_add_protocol(&obj
->header
,
1568 &efi_guid_hii_database_protocol
,
1569 (void *)&efi_hii_database
);
1570 if (ret
!= EFI_SUCCESS
)
1575 printf("ERROR: Failure to install protocols for loaded image\n");
1580 * efi_load_image_from_path() - load an image using a file path
1581 * @file_path: the path of the image to load
1582 * @buffer: buffer containing the loaded image
1584 * Return: status code
1586 efi_status_t
efi_load_image_from_path(struct efi_device_path
*file_path
,
1589 struct efi_file_info
*info
= NULL
;
1590 struct efi_file_handle
*f
;
1591 static efi_status_t ret
;
1594 f
= efi_file_from_path(file_path
);
1596 return EFI_DEVICE_ERROR
;
1599 EFI_CALL(ret
= f
->getinfo(f
, (efi_guid_t
*)&efi_file_info_guid
,
1601 if (ret
== EFI_BUFFER_TOO_SMALL
) {
1603 EFI_CALL(ret
= f
->getinfo(f
, (efi_guid_t
*)&efi_file_info_guid
,
1606 if (ret
!= EFI_SUCCESS
)
1609 ret
= efi_allocate_pool(EFI_LOADER_DATA
, info
->file_size
, buffer
);
1613 bs
= info
->file_size
;
1614 EFI_CALL(ret
= f
->read(f
, &bs
, *buffer
));
1618 EFI_CALL(f
->close(f
));
1620 if (ret
!= EFI_SUCCESS
) {
1621 efi_free_pool(*buffer
);
1629 * efi_load_image() - load an EFI image into memory
1630 * @boot_policy: true for request originating from the boot manager
1631 * @parent_image: the caller's image handle
1632 * @file_path: the path of the image to load
1633 * @source_buffer: memory location from which the image is installed
1634 * @source_size: size of the memory area from which the image is installed
1635 * @image_handle: handle for the newly installed image
1637 * This function implements the LoadImage service.
1639 * See the Unified Extensible Firmware Interface (UEFI) specification
1642 * Return: status code
1644 static efi_status_t EFIAPI
efi_load_image(bool boot_policy
,
1645 efi_handle_t parent_image
,
1646 struct efi_device_path
*file_path
,
1647 void *source_buffer
,
1648 efi_uintn_t source_size
,
1649 efi_handle_t
*image_handle
)
1651 struct efi_loaded_image
*info
= NULL
;
1652 struct efi_loaded_image_obj
**image_obj
=
1653 (struct efi_loaded_image_obj
**)image_handle
;
1656 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy
, parent_image
,
1657 file_path
, source_buffer
, source_size
, image_handle
);
1659 if (!image_handle
|| !parent_image
) {
1660 ret
= EFI_INVALID_PARAMETER
;
1664 if (!source_buffer
&& !file_path
) {
1665 ret
= EFI_NOT_FOUND
;
1669 if (!source_buffer
) {
1670 struct efi_device_path
*dp
, *fp
;
1672 ret
= efi_load_image_from_path(file_path
, &source_buffer
);
1673 if (ret
!= EFI_SUCCESS
)
1676 * split file_path which contains both the device and
1679 efi_dp_split_file_path(file_path
, &dp
, &fp
);
1680 ret
= efi_setup_loaded_image(dp
, fp
, image_obj
, &info
);
1681 if (ret
!= EFI_SUCCESS
)
1684 /* In this case, file_path is the "device" path, i.e.
1685 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1687 ret
= efi_setup_loaded_image(file_path
, NULL
, image_obj
, &info
);
1688 if (ret
!= EFI_SUCCESS
)
1691 (*image_obj
)->entry
= efi_load_pe(*image_obj
, source_buffer
, info
);
1692 if (!(*image_obj
)->entry
) {
1693 ret
= EFI_UNSUPPORTED
;
1696 info
->system_table
= &systab
;
1697 info
->parent_handle
= parent_image
;
1698 return EFI_EXIT(EFI_SUCCESS
);
1700 efi_delete_handle(*image_handle
);
1701 *image_handle
= NULL
;
1704 return EFI_EXIT(ret
);
1708 * efi_start_image() - call the entry point of an image
1709 * @image_handle: handle of the image
1710 * @exit_data_size: size of the buffer
1711 * @exit_data: buffer to receive the exit data of the called image
1713 * This function implements the StartImage service.
1715 * See the Unified Extensible Firmware Interface (UEFI) specification for
1718 * Return: status code
1720 static efi_status_t EFIAPI
efi_start_image(efi_handle_t image_handle
,
1721 efi_uintn_t
*exit_data_size
,
1724 struct efi_loaded_image_obj
*image_obj
=
1725 (struct efi_loaded_image_obj
*)image_handle
;
1728 EFI_ENTRY("%p, %p, %p", image_handle
, exit_data_size
, exit_data
);
1730 efi_is_direct_boot
= false;
1732 /* call the image! */
1733 if (setjmp(&image_obj
->exit_jmp
)) {
1735 * We called the entry point of the child image with EFI_CALL
1736 * in the lines below. The child image called the Exit() boot
1737 * service efi_exit() which executed the long jump that brought
1738 * us to the current line. This implies that the second half
1739 * of the EFI_CALL macro has not been executed.
1743 * efi_exit() called efi_restore_gd(). We have to undo this
1744 * otherwise __efi_entry_check() will put the wrong value into
1750 * To get ready to call EFI_EXIT below we have to execute the
1751 * missed out steps of EFI_CALL.
1753 assert(__efi_entry_check());
1754 debug("%sEFI: %lu returned by started image\n",
1755 __efi_nesting_dec(),
1756 (unsigned long)((uintptr_t)image_obj
->exit_status
&
1758 return EFI_EXIT(image_obj
->exit_status
);
1761 ret
= EFI_CALL(image_obj
->entry(image_handle
, &systab
));
1764 * Usually UEFI applications call Exit() instead of returning.
1765 * But because the world doesn't consist of ponies and unicorns,
1766 * we're happy to emulate that behavior on behalf of a payload
1769 return EFI_CALL(systab
.boottime
->exit(image_handle
, ret
, 0, NULL
));
1773 * efi_exit() - leave an EFI application or driver
1774 * @image_handle: handle of the application or driver that is exiting
1775 * @exit_status: status code
1776 * @exit_data_size: size of the buffer in bytes
1777 * @exit_data: buffer with data describing an error
1779 * This function implements the Exit service.
1781 * See the Unified Extensible Firmware Interface (UEFI) specification for
1784 * Return: status code
1786 static efi_status_t EFIAPI
efi_exit(efi_handle_t image_handle
,
1787 efi_status_t exit_status
,
1788 efi_uintn_t exit_data_size
,
1792 * TODO: We should call the unload procedure of the loaded
1795 struct efi_loaded_image_obj
*image_obj
=
1796 (struct efi_loaded_image_obj
*)image_handle
;
1798 EFI_ENTRY("%p, %ld, %zu, %p", image_handle
, exit_status
,
1799 exit_data_size
, exit_data
);
1801 /* Make sure entry/exit counts for EFI world cross-overs match */
1802 EFI_EXIT(exit_status
);
1805 * But longjmp out with the U-Boot gd, not the application's, as
1806 * the other end is a setjmp call inside EFI context.
1810 image_obj
->exit_status
= exit_status
;
1811 longjmp(&image_obj
->exit_jmp
, 1);
1813 panic("EFI application exited");
1817 * efi_unload_image() - unload an EFI image
1818 * @image_handle: handle of the image to be unloaded
1820 * This function implements the UnloadImage service.
1822 * See the Unified Extensible Firmware Interface (UEFI) specification for
1825 * Return: status code
1827 static efi_status_t EFIAPI
efi_unload_image(efi_handle_t image_handle
)
1829 struct efi_object
*efiobj
;
1831 EFI_ENTRY("%p", image_handle
);
1832 efiobj
= efi_search_obj(image_handle
);
1834 list_del(&efiobj
->link
);
1836 return EFI_EXIT(EFI_SUCCESS
);
1840 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1842 static void efi_exit_caches(void)
1844 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1846 * Grub on 32bit ARM needs to have caches disabled before jumping into
1847 * a zImage, but does not know of all cache layers. Give it a hand.
1849 if (efi_is_direct_boot
)
1850 cleanup_before_linux();
1855 * efi_exit_boot_services() - stop all boot services
1856 * @image_handle: handle of the loaded image
1857 * @map_key: key of the memory map
1859 * This function implements the ExitBootServices service.
1861 * See the Unified Extensible Firmware Interface (UEFI) specification
1864 * All timer events are disabled. For exit boot services events the
1865 * notification function is called. The boot services are disabled in the
1868 * Return: status code
1870 static efi_status_t EFIAPI
efi_exit_boot_services(efi_handle_t image_handle
,
1871 unsigned long map_key
)
1873 struct efi_event
*evt
;
1875 EFI_ENTRY("%p, %ld", image_handle
, map_key
);
1877 /* Check that the caller has read the current memory map */
1878 if (map_key
!= efi_memory_map_key
)
1879 return EFI_INVALID_PARAMETER
;
1881 /* Make sure that notification functions are not called anymore */
1882 efi_tpl
= TPL_HIGH_LEVEL
;
1884 /* Check if ExitBootServices has already been called */
1885 if (!systab
.boottime
)
1886 return EFI_EXIT(EFI_SUCCESS
);
1888 /* Add related events to the event group */
1889 list_for_each_entry(evt
, &efi_events
, link
) {
1890 if (evt
->type
== EVT_SIGNAL_EXIT_BOOT_SERVICES
)
1891 evt
->group
= &efi_guid_event_group_exit_boot_services
;
1893 /* Notify that ExitBootServices is invoked. */
1894 list_for_each_entry(evt
, &efi_events
, link
) {
1896 !guidcmp(evt
->group
,
1897 &efi_guid_event_group_exit_boot_services
)) {
1898 efi_signal_event(evt
, false);
1903 /* TODO: Should persist EFI variables here */
1905 board_quiesce_devices();
1907 /* Fix up caches for EFI payloads if necessary */
1910 /* This stops all lingering devices */
1911 bootm_disable_interrupts();
1913 /* Disable boot time services */
1914 systab
.con_in_handle
= NULL
;
1915 systab
.con_in
= NULL
;
1916 systab
.con_out_handle
= NULL
;
1917 systab
.con_out
= NULL
;
1918 systab
.stderr_handle
= NULL
;
1919 systab
.std_err
= NULL
;
1920 systab
.boottime
= NULL
;
1922 /* Recalculate CRC32 */
1923 efi_update_table_header_crc32(&systab
.hdr
);
1925 /* Give the payload some time to boot */
1926 efi_set_watchdog(0);
1929 return EFI_EXIT(EFI_SUCCESS
);
1933 * efi_get_next_monotonic_count() - get next value of the counter
1934 * @count: returned value of the counter
1936 * This function implements the NextMonotonicCount service.
1938 * See the Unified Extensible Firmware Interface (UEFI) specification for
1941 * Return: status code
1943 static efi_status_t EFIAPI
efi_get_next_monotonic_count(uint64_t *count
)
1945 static uint64_t mono
;
1947 EFI_ENTRY("%p", count
);
1949 return EFI_EXIT(EFI_SUCCESS
);
1953 * efi_stall() - sleep
1954 * @microseconds: period to sleep in microseconds
1956 * This function implements the Stall service.
1958 * See the Unified Extensible Firmware Interface (UEFI) specification for
1961 * Return: status code
1963 static efi_status_t EFIAPI
efi_stall(unsigned long microseconds
)
1965 EFI_ENTRY("%ld", microseconds
);
1966 udelay(microseconds
);
1967 return EFI_EXIT(EFI_SUCCESS
);
1971 * efi_set_watchdog_timer() - reset the watchdog timer
1972 * @timeout: seconds before reset by watchdog
1973 * @watchdog_code: code to be logged when resetting
1974 * @data_size: size of buffer in bytes
1975 * @watchdog_data: buffer with data describing the reset reason
1977 * This function implements the SetWatchdogTimer service.
1979 * See the Unified Extensible Firmware Interface (UEFI) specification for
1982 * Return: status code
1984 static efi_status_t EFIAPI
efi_set_watchdog_timer(unsigned long timeout
,
1985 uint64_t watchdog_code
,
1986 unsigned long data_size
,
1987 uint16_t *watchdog_data
)
1989 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout
, watchdog_code
,
1990 data_size
, watchdog_data
);
1991 return EFI_EXIT(efi_set_watchdog(timeout
));
1995 * efi_close_protocol() - close a protocol
1996 * @handle: handle on which the protocol shall be closed
1997 * @protocol: GUID of the protocol to close
1998 * @agent_handle: handle of the driver
1999 * @controller_handle: handle of the controller
2001 * This function implements the CloseProtocol service.
2003 * See the Unified Extensible Firmware Interface (UEFI) specification for
2006 * Return: status code
2008 static efi_status_t EFIAPI
efi_close_protocol(efi_handle_t handle
,
2009 const efi_guid_t
*protocol
,
2010 efi_handle_t agent_handle
,
2011 efi_handle_t controller_handle
)
2013 struct efi_handler
*handler
;
2014 struct efi_open_protocol_info_item
*item
;
2015 struct efi_open_protocol_info_item
*pos
;
2018 EFI_ENTRY("%p, %pUl, %p, %p", handle
, protocol
, agent_handle
,
2021 if (!agent_handle
) {
2022 r
= EFI_INVALID_PARAMETER
;
2025 r
= efi_search_protocol(handle
, protocol
, &handler
);
2026 if (r
!= EFI_SUCCESS
)
2030 list_for_each_entry_safe(item
, pos
, &handler
->open_infos
, link
) {
2031 if (item
->info
.agent_handle
== agent_handle
&&
2032 item
->info
.controller_handle
== controller_handle
) {
2033 efi_delete_open_info(item
);
2043 * efi_open_protocol_information() - provide information about then open status
2044 * of a protocol on a handle
2045 * @handle: handle for which the information shall be retrieved
2046 * @protocol: GUID of the protocol
2047 * @entry_buffer: buffer to receive the open protocol information
2048 * @entry_count: number of entries available in the buffer
2050 * This function implements the OpenProtocolInformation service.
2052 * See the Unified Extensible Firmware Interface (UEFI) specification for
2055 * Return: status code
2057 static efi_status_t EFIAPI
efi_open_protocol_information(
2058 efi_handle_t handle
, const efi_guid_t
*protocol
,
2059 struct efi_open_protocol_info_entry
**entry_buffer
,
2060 efi_uintn_t
*entry_count
)
2062 unsigned long buffer_size
;
2063 unsigned long count
;
2064 struct efi_handler
*handler
;
2065 struct efi_open_protocol_info_item
*item
;
2068 EFI_ENTRY("%p, %pUl, %p, %p", handle
, protocol
, entry_buffer
,
2071 /* Check parameters */
2072 if (!entry_buffer
) {
2073 r
= EFI_INVALID_PARAMETER
;
2076 r
= efi_search_protocol(handle
, protocol
, &handler
);
2077 if (r
!= EFI_SUCCESS
)
2082 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2083 if (item
->info
.open_count
)
2086 *entry_count
= count
;
2087 *entry_buffer
= NULL
;
2094 buffer_size
= count
* sizeof(struct efi_open_protocol_info_entry
);
2095 r
= efi_allocate_pool(EFI_BOOT_SERVICES_DATA
, buffer_size
,
2096 (void **)entry_buffer
);
2097 if (r
!= EFI_SUCCESS
)
2099 list_for_each_entry_reverse(item
, &handler
->open_infos
, link
) {
2100 if (item
->info
.open_count
)
2101 (*entry_buffer
)[--count
] = item
->info
;
2108 * efi_protocols_per_handle() - get protocols installed on a handle
2109 * @handle: handle for which the information is retrieved
2110 * @protocol_buffer: buffer with protocol GUIDs
2111 * @protocol_buffer_count: number of entries in the buffer
2113 * This function implements the ProtocolsPerHandleService.
2115 * See the Unified Extensible Firmware Interface (UEFI) specification for
2118 * Return: status code
2120 static efi_status_t EFIAPI
efi_protocols_per_handle(
2121 efi_handle_t handle
, efi_guid_t
***protocol_buffer
,
2122 efi_uintn_t
*protocol_buffer_count
)
2124 unsigned long buffer_size
;
2125 struct efi_object
*efiobj
;
2126 struct list_head
*protocol_handle
;
2129 EFI_ENTRY("%p, %p, %p", handle
, protocol_buffer
,
2130 protocol_buffer_count
);
2132 if (!handle
|| !protocol_buffer
|| !protocol_buffer_count
)
2133 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2135 *protocol_buffer
= NULL
;
2136 *protocol_buffer_count
= 0;
2138 efiobj
= efi_search_obj(handle
);
2140 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2142 /* Count protocols */
2143 list_for_each(protocol_handle
, &efiobj
->protocols
) {
2144 ++*protocol_buffer_count
;
2148 if (*protocol_buffer_count
) {
2151 buffer_size
= sizeof(efi_guid_t
*) * *protocol_buffer_count
;
2152 r
= efi_allocate_pool(EFI_BOOT_SERVICES_DATA
, buffer_size
,
2153 (void **)protocol_buffer
);
2154 if (r
!= EFI_SUCCESS
)
2156 list_for_each(protocol_handle
, &efiobj
->protocols
) {
2157 struct efi_handler
*protocol
;
2159 protocol
= list_entry(protocol_handle
,
2160 struct efi_handler
, link
);
2161 (*protocol_buffer
)[j
] = (void *)protocol
->guid
;
2166 return EFI_EXIT(EFI_SUCCESS
);
2170 * efi_locate_handle_buffer() - locate handles implementing a protocol
2171 * @search_type: selection criterion
2172 * @protocol: GUID of the protocol
2173 * @search_key: registration key
2174 * @no_handles: number of returned handles
2175 * @buffer: buffer with the returned handles
2177 * This function implements the LocateHandleBuffer service.
2179 * See the Unified Extensible Firmware Interface (UEFI) specification for
2182 * Return: status code
2184 static efi_status_t EFIAPI
efi_locate_handle_buffer(
2185 enum efi_locate_search_type search_type
,
2186 const efi_guid_t
*protocol
, void *search_key
,
2187 efi_uintn_t
*no_handles
, efi_handle_t
**buffer
)
2190 efi_uintn_t buffer_size
= 0;
2192 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type
, protocol
, search_key
,
2193 no_handles
, buffer
);
2195 if (!no_handles
|| !buffer
) {
2196 r
= EFI_INVALID_PARAMETER
;
2201 r
= efi_locate_handle(search_type
, protocol
, search_key
, &buffer_size
,
2203 if (r
!= EFI_BUFFER_TOO_SMALL
)
2205 r
= efi_allocate_pool(EFI_BOOT_SERVICES_DATA
, buffer_size
,
2207 if (r
!= EFI_SUCCESS
)
2209 r
= efi_locate_handle(search_type
, protocol
, search_key
, &buffer_size
,
2211 if (r
== EFI_SUCCESS
)
2212 *no_handles
= buffer_size
/ sizeof(efi_handle_t
);
2218 * efi_locate_protocol() - find an interface implementing a protocol
2219 * @protocol: GUID of the protocol
2220 * @registration: registration key passed to the notification function
2221 * @protocol_interface: interface implementing the protocol
2223 * This function implements the LocateProtocol service.
2225 * See the Unified Extensible Firmware Interface (UEFI) specification for
2228 * Return: status code
2230 static efi_status_t EFIAPI
efi_locate_protocol(const efi_guid_t
*protocol
,
2232 void **protocol_interface
)
2234 struct list_head
*lhandle
;
2237 EFI_ENTRY("%pUl, %p, %p", protocol
, registration
, protocol_interface
);
2239 if (!protocol
|| !protocol_interface
)
2240 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2242 list_for_each(lhandle
, &efi_obj_list
) {
2243 struct efi_object
*efiobj
;
2244 struct efi_handler
*handler
;
2246 efiobj
= list_entry(lhandle
, struct efi_object
, link
);
2248 ret
= efi_search_protocol(efiobj
, protocol
, &handler
);
2249 if (ret
== EFI_SUCCESS
) {
2250 *protocol_interface
= handler
->protocol_interface
;
2251 return EFI_EXIT(EFI_SUCCESS
);
2254 *protocol_interface
= NULL
;
2256 return EFI_EXIT(EFI_NOT_FOUND
);
2260 * efi_locate_device_path() - Get the device path and handle of an device
2261 * implementing a protocol
2262 * @protocol: GUID of the protocol
2263 * @device_path: device path
2264 * @device: handle of the device
2266 * This function implements the LocateDevicePath service.
2268 * See the Unified Extensible Firmware Interface (UEFI) specification for
2271 * Return: status code
2273 static efi_status_t EFIAPI
efi_locate_device_path(
2274 const efi_guid_t
*protocol
,
2275 struct efi_device_path
**device_path
,
2276 efi_handle_t
*device
)
2278 struct efi_device_path
*dp
;
2280 struct efi_handler
*handler
;
2281 efi_handle_t
*handles
;
2283 size_t len_best
= 0;
2284 efi_uintn_t no_handles
;
2288 EFI_ENTRY("%pUl, %p, %p", protocol
, device_path
, device
);
2290 if (!protocol
|| !device_path
|| !*device_path
|| !device
) {
2291 ret
= EFI_INVALID_PARAMETER
;
2295 /* Find end of device path */
2296 len
= efi_dp_instance_size(*device_path
);
2298 /* Get all handles implementing the protocol */
2299 ret
= EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL
, protocol
, NULL
,
2300 &no_handles
, &handles
));
2301 if (ret
!= EFI_SUCCESS
)
2304 for (i
= 0; i
< no_handles
; ++i
) {
2305 /* Find the device path protocol */
2306 ret
= efi_search_protocol(handles
[i
], &efi_guid_device_path
,
2308 if (ret
!= EFI_SUCCESS
)
2310 dp
= (struct efi_device_path
*)handler
->protocol_interface
;
2311 len_dp
= efi_dp_instance_size(dp
);
2313 * This handle can only be a better fit
2314 * if its device path length is longer than the best fit and
2315 * if its device path length is shorter of equal the searched
2318 if (len_dp
<= len_best
|| len_dp
> len
)
2320 /* Check if dp is a subpath of device_path */
2321 if (memcmp(*device_path
, dp
, len_dp
))
2323 *device
= handles
[i
];
2327 remainder
= (u8
*)*device_path
+ len_best
;
2328 *device_path
= (struct efi_device_path
*)remainder
;
2331 ret
= EFI_NOT_FOUND
;
2334 return EFI_EXIT(ret
);
2338 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2340 * @handle: handle on which the protocol interfaces shall be installed
2341 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2344 * This function implements the MultipleProtocolInterfaces service.
2346 * See the Unified Extensible Firmware Interface (UEFI) specification for
2349 * Return: status code
2351 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2352 (efi_handle_t
*handle
, ...)
2354 EFI_ENTRY("%p", handle
);
2357 const efi_guid_t
*protocol
;
2358 void *protocol_interface
;
2359 efi_status_t r
= EFI_SUCCESS
;
2363 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2365 efi_va_start(argptr
, handle
);
2367 protocol
= efi_va_arg(argptr
, efi_guid_t
*);
2370 protocol_interface
= efi_va_arg(argptr
, void*);
2371 r
= EFI_CALL(efi_install_protocol_interface(
2373 EFI_NATIVE_INTERFACE
,
2374 protocol_interface
));
2375 if (r
!= EFI_SUCCESS
)
2380 if (r
== EFI_SUCCESS
)
2383 /* If an error occurred undo all changes. */
2384 efi_va_start(argptr
, handle
);
2386 protocol
= efi_va_arg(argptr
, efi_guid_t
*);
2387 protocol_interface
= efi_va_arg(argptr
, void*);
2388 EFI_CALL(efi_uninstall_protocol_interface(*handle
, protocol
,
2389 protocol_interface
));
2397 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2399 * @handle: handle from which the protocol interfaces shall be removed
2400 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2403 * This function implements the UninstallMultipleProtocolInterfaces service.
2405 * See the Unified Extensible Firmware Interface (UEFI) specification for
2408 * Return: status code
2410 static efi_status_t EFIAPI
efi_uninstall_multiple_protocol_interfaces(
2411 efi_handle_t handle
, ...)
2413 EFI_ENTRY("%p", handle
);
2416 const efi_guid_t
*protocol
;
2417 void *protocol_interface
;
2418 efi_status_t r
= EFI_SUCCESS
;
2422 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2424 efi_va_start(argptr
, handle
);
2426 protocol
= efi_va_arg(argptr
, efi_guid_t
*);
2429 protocol_interface
= efi_va_arg(argptr
, void*);
2430 r
= efi_uninstall_protocol(handle
, protocol
,
2431 protocol_interface
);
2432 if (r
!= EFI_SUCCESS
)
2437 if (r
== EFI_SUCCESS
) {
2438 /* If the last protocol has been removed, delete the handle. */
2439 if (list_empty(&handle
->protocols
)) {
2440 list_del(&handle
->link
);
2446 /* If an error occurred undo all changes. */
2447 efi_va_start(argptr
, handle
);
2449 protocol
= efi_va_arg(argptr
, efi_guid_t
*);
2450 protocol_interface
= efi_va_arg(argptr
, void*);
2451 EFI_CALL(efi_install_protocol_interface(&handle
, protocol
,
2452 EFI_NATIVE_INTERFACE
,
2453 protocol_interface
));
2457 /* In case of an error always return EFI_INVALID_PARAMETER */
2458 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2462 * efi_calculate_crc32() - calculate cyclic redundancy code
2463 * @data: buffer with data
2464 * @data_size: size of buffer in bytes
2465 * @crc32_p: cyclic redundancy code
2467 * This function implements the CalculateCrc32 service.
2469 * See the Unified Extensible Firmware Interface (UEFI) specification for
2472 * Return: status code
2474 static efi_status_t EFIAPI
efi_calculate_crc32(const void *data
,
2475 efi_uintn_t data_size
,
2478 EFI_ENTRY("%p, %zu", data
, data_size
);
2479 *crc32_p
= crc32(0, data
, data_size
);
2480 return EFI_EXIT(EFI_SUCCESS
);
2484 * efi_copy_mem() - copy memory
2485 * @destination: destination of the copy operation
2486 * @source: source of the copy operation
2487 * @length: number of bytes to copy
2489 * This function implements the CopyMem service.
2491 * See the Unified Extensible Firmware Interface (UEFI) specification for
2494 static void EFIAPI
efi_copy_mem(void *destination
, const void *source
,
2497 EFI_ENTRY("%p, %p, %ld", destination
, source
, (unsigned long)length
);
2498 memmove(destination
, source
, length
);
2499 EFI_EXIT(EFI_SUCCESS
);
2503 * efi_set_mem() - Fill memory with a byte value.
2504 * @buffer: buffer to fill
2505 * @size: size of buffer in bytes
2506 * @value: byte to copy to the buffer
2508 * This function implements the SetMem service.
2510 * See the Unified Extensible Firmware Interface (UEFI) specification for
2513 static void EFIAPI
efi_set_mem(void *buffer
, size_t size
, uint8_t value
)
2515 EFI_ENTRY("%p, %ld, 0x%x", buffer
, (unsigned long)size
, value
);
2516 memset(buffer
, value
, size
);
2517 EFI_EXIT(EFI_SUCCESS
);
2521 * efi_protocol_open() - open protocol interface on a handle
2522 * @handler: handler of a protocol
2523 * @protocol_interface: interface implementing the protocol
2524 * @agent_handle: handle of the driver
2525 * @controller_handle: handle of the controller
2526 * @attributes: attributes indicating how to open the protocol
2528 * Return: status code
2530 static efi_status_t
efi_protocol_open(
2531 struct efi_handler
*handler
,
2532 void **protocol_interface
, void *agent_handle
,
2533 void *controller_handle
, uint32_t attributes
)
2535 struct efi_open_protocol_info_item
*item
;
2536 struct efi_open_protocol_info_entry
*match
= NULL
;
2537 bool opened_by_driver
= false;
2538 bool opened_exclusive
= false;
2540 /* If there is no agent, only return the interface */
2544 /* For TEST_PROTOCOL ignore interface attribute */
2545 if (attributes
!= EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)
2546 *protocol_interface
= NULL
;
2549 * Check if the protocol is already opened by a driver with the same
2550 * attributes or opened exclusively
2552 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2553 if (item
->info
.agent_handle
== agent_handle
) {
2554 if ((attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) &&
2555 (item
->info
.attributes
== attributes
))
2556 return EFI_ALREADY_STARTED
;
2558 if (item
->info
.attributes
& EFI_OPEN_PROTOCOL_EXCLUSIVE
)
2559 opened_exclusive
= true;
2562 /* Only one controller can open the protocol exclusively */
2563 if (opened_exclusive
&& attributes
&
2564 (EFI_OPEN_PROTOCOL_EXCLUSIVE
| EFI_OPEN_PROTOCOL_BY_DRIVER
))
2565 return EFI_ACCESS_DENIED
;
2567 /* Prepare exclusive opening */
2568 if (attributes
& EFI_OPEN_PROTOCOL_EXCLUSIVE
) {
2569 /* Try to disconnect controllers */
2570 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2571 if (item
->info
.attributes
==
2572 EFI_OPEN_PROTOCOL_BY_DRIVER
)
2573 EFI_CALL(efi_disconnect_controller(
2574 item
->info
.controller_handle
,
2575 item
->info
.agent_handle
,
2578 opened_by_driver
= false;
2579 /* Check if all controllers are disconnected */
2580 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2581 if (item
->info
.attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
)
2582 opened_by_driver
= true;
2584 /* Only one controller can be connected */
2585 if (opened_by_driver
)
2586 return EFI_ACCESS_DENIED
;
2589 /* Find existing entry */
2590 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2591 if (item
->info
.agent_handle
== agent_handle
&&
2592 item
->info
.controller_handle
== controller_handle
)
2593 match
= &item
->info
;
2595 /* None found, create one */
2597 match
= efi_create_open_info(handler
);
2599 return EFI_OUT_OF_RESOURCES
;
2602 match
->agent_handle
= agent_handle
;
2603 match
->controller_handle
= controller_handle
;
2604 match
->attributes
= attributes
;
2605 match
->open_count
++;
2608 /* For TEST_PROTOCOL ignore interface attribute. */
2609 if (attributes
!= EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)
2610 *protocol_interface
= handler
->protocol_interface
;
2616 * efi_open_protocol() - open protocol interface on a handle
2617 * @handle: handle on which the protocol shall be opened
2618 * @protocol: GUID of the protocol
2619 * @protocol_interface: interface implementing the protocol
2620 * @agent_handle: handle of the driver
2621 * @controller_handle: handle of the controller
2622 * @attributes: attributes indicating how to open the protocol
2624 * This function implements the OpenProtocol interface.
2626 * See the Unified Extensible Firmware Interface (UEFI) specification for
2629 * Return: status code
2631 static efi_status_t EFIAPI efi_open_protocol
2632 (efi_handle_t handle
, const efi_guid_t
*protocol
,
2633 void **protocol_interface
, efi_handle_t agent_handle
,
2634 efi_handle_t controller_handle
, uint32_t attributes
)
2636 struct efi_handler
*handler
;
2637 efi_status_t r
= EFI_INVALID_PARAMETER
;
2639 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle
, protocol
,
2640 protocol_interface
, agent_handle
, controller_handle
,
2643 if (!handle
|| !protocol
||
2644 (!protocol_interface
&& attributes
!=
2645 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)) {
2649 switch (attributes
) {
2650 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
:
2651 case EFI_OPEN_PROTOCOL_GET_PROTOCOL
:
2652 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL
:
2654 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
:
2655 if (controller_handle
== handle
)
2658 case EFI_OPEN_PROTOCOL_BY_DRIVER
:
2659 case EFI_OPEN_PROTOCOL_BY_DRIVER
| EFI_OPEN_PROTOCOL_EXCLUSIVE
:
2660 /* Check that the controller handle is valid */
2661 if (!efi_search_obj(controller_handle
))
2664 case EFI_OPEN_PROTOCOL_EXCLUSIVE
:
2665 /* Check that the agent handle is valid */
2666 if (!efi_search_obj(agent_handle
))
2673 r
= efi_search_protocol(handle
, protocol
, &handler
);
2674 if (r
!= EFI_SUCCESS
)
2677 r
= efi_protocol_open(handler
, protocol_interface
, agent_handle
,
2678 controller_handle
, attributes
);
2684 * efi_handle_protocol() - get interface of a protocol on a handle
2685 * @handle: handle on which the protocol shall be opened
2686 * @protocol: GUID of the protocol
2687 * @protocol_interface: interface implementing the protocol
2689 * This function implements the HandleProtocol service.
2691 * See the Unified Extensible Firmware Interface (UEFI) specification for
2694 * Return: status code
2696 static efi_status_t EFIAPI
efi_handle_protocol(efi_handle_t handle
,
2697 const efi_guid_t
*protocol
,
2698 void **protocol_interface
)
2700 return efi_open_protocol(handle
, protocol
, protocol_interface
, NULL
,
2701 NULL
, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
);
2705 * efi_bind_controller() - bind a single driver to a controller
2706 * @controller_handle: controller handle
2707 * @driver_image_handle: driver handle
2708 * @remain_device_path: remaining path
2710 * Return: status code
2712 static efi_status_t
efi_bind_controller(
2713 efi_handle_t controller_handle
,
2714 efi_handle_t driver_image_handle
,
2715 struct efi_device_path
*remain_device_path
)
2717 struct efi_driver_binding_protocol
*binding_protocol
;
2720 r
= EFI_CALL(efi_open_protocol(driver_image_handle
,
2721 &efi_guid_driver_binding_protocol
,
2722 (void **)&binding_protocol
,
2723 driver_image_handle
, NULL
,
2724 EFI_OPEN_PROTOCOL_GET_PROTOCOL
));
2725 if (r
!= EFI_SUCCESS
)
2727 r
= EFI_CALL(binding_protocol
->supported(binding_protocol
,
2729 remain_device_path
));
2730 if (r
== EFI_SUCCESS
)
2731 r
= EFI_CALL(binding_protocol
->start(binding_protocol
,
2733 remain_device_path
));
2734 EFI_CALL(efi_close_protocol(driver_image_handle
,
2735 &efi_guid_driver_binding_protocol
,
2736 driver_image_handle
, NULL
));
2741 * efi_connect_single_controller() - connect a single driver to a controller
2742 * @controller_handle: controller
2743 * @driver_image_handle: driver
2744 * @remain_device_path: remaining path
2746 * Return: status code
2748 static efi_status_t
efi_connect_single_controller(
2749 efi_handle_t controller_handle
,
2750 efi_handle_t
*driver_image_handle
,
2751 struct efi_device_path
*remain_device_path
)
2753 efi_handle_t
*buffer
;
2757 size_t connected
= 0;
2759 /* Get buffer with all handles with driver binding protocol */
2760 r
= EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL
,
2761 &efi_guid_driver_binding_protocol
,
2762 NULL
, &count
, &buffer
));
2763 if (r
!= EFI_SUCCESS
)
2766 /* Context Override */
2767 if (driver_image_handle
) {
2768 for (; *driver_image_handle
; ++driver_image_handle
) {
2769 for (i
= 0; i
< count
; ++i
) {
2770 if (buffer
[i
] == *driver_image_handle
) {
2772 r
= efi_bind_controller(
2774 *driver_image_handle
,
2775 remain_device_path
);
2777 * For drivers that do not support the
2778 * controller or are already connected
2779 * we receive an error code here.
2781 if (r
== EFI_SUCCESS
)
2789 * TODO: Some overrides are not yet implemented:
2790 * - Platform Driver Override
2791 * - Driver Family Override Search
2792 * - Bus Specific Driver Override
2795 /* Driver Binding Search */
2796 for (i
= 0; i
< count
; ++i
) {
2798 r
= efi_bind_controller(controller_handle
,
2800 remain_device_path
);
2801 if (r
== EFI_SUCCESS
)
2806 efi_free_pool(buffer
);
2808 return EFI_NOT_FOUND
;
2813 * efi_connect_controller() - connect a controller to a driver
2814 * @controller_handle: handle of the controller
2815 * @driver_image_handle: handle of the driver
2816 * @remain_device_path: device path of a child controller
2817 * @recursive: true to connect all child controllers
2819 * This function implements the ConnectController service.
2821 * See the Unified Extensible Firmware Interface (UEFI) specification for
2824 * First all driver binding protocol handles are tried for binding drivers.
2825 * Afterwards all handles that have opened a protocol of the controller
2826 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2828 * Return: status code
2830 static efi_status_t EFIAPI
efi_connect_controller(
2831 efi_handle_t controller_handle
,
2832 efi_handle_t
*driver_image_handle
,
2833 struct efi_device_path
*remain_device_path
,
2837 efi_status_t ret
= EFI_NOT_FOUND
;
2838 struct efi_object
*efiobj
;
2840 EFI_ENTRY("%p, %p, %pD, %d", controller_handle
, driver_image_handle
,
2841 remain_device_path
, recursive
);
2843 efiobj
= efi_search_obj(controller_handle
);
2845 ret
= EFI_INVALID_PARAMETER
;
2849 r
= efi_connect_single_controller(controller_handle
,
2850 driver_image_handle
,
2851 remain_device_path
);
2852 if (r
== EFI_SUCCESS
)
2855 struct efi_handler
*handler
;
2856 struct efi_open_protocol_info_item
*item
;
2858 list_for_each_entry(handler
, &efiobj
->protocols
, link
) {
2859 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2860 if (item
->info
.attributes
&
2861 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) {
2862 r
= EFI_CALL(efi_connect_controller(
2863 item
->info
.controller_handle
,
2864 driver_image_handle
,
2867 if (r
== EFI_SUCCESS
)
2873 /* Check for child controller specified by end node */
2874 if (ret
!= EFI_SUCCESS
&& remain_device_path
&&
2875 remain_device_path
->type
== DEVICE_PATH_TYPE_END
)
2878 return EFI_EXIT(ret
);
2882 * efi_reinstall_protocol_interface() - reinstall protocol interface
2883 * @handle: handle on which the protocol shall be reinstalled
2884 * @protocol: GUID of the protocol to be installed
2885 * @old_interface: interface to be removed
2886 * @new_interface: interface to be installed
2888 * This function implements the ReinstallProtocolInterface service.
2890 * See the Unified Extensible Firmware Interface (UEFI) specification for
2893 * The old interface is uninstalled. The new interface is installed.
2894 * Drivers are connected.
2896 * Return: status code
2898 static efi_status_t EFIAPI
efi_reinstall_protocol_interface(
2899 efi_handle_t handle
, const efi_guid_t
*protocol
,
2900 void *old_interface
, void *new_interface
)
2904 EFI_ENTRY("%p, %pUl, %p, %p", handle
, protocol
, old_interface
,
2907 /* Uninstall protocol but do not delete handle */
2908 ret
= efi_uninstall_protocol(handle
, protocol
, old_interface
);
2909 if (ret
!= EFI_SUCCESS
)
2912 /* Install the new protocol */
2913 ret
= efi_add_protocol(handle
, protocol
, new_interface
);
2915 * The UEFI spec does not specify what should happen to the handle
2916 * if in case of an error no protocol interface remains on the handle.
2917 * So let's do nothing here.
2919 if (ret
!= EFI_SUCCESS
)
2922 * The returned status code has to be ignored.
2923 * Do not create an error if no suitable driver for the handle exists.
2925 EFI_CALL(efi_connect_controller(handle
, NULL
, NULL
, true));
2927 return EFI_EXIT(ret
);
2931 * efi_get_child_controllers() - get all child controllers associated to a driver
2932 * @efiobj: handle of the controller
2933 * @driver_handle: handle of the driver
2934 * @number_of_children: number of child controllers
2935 * @child_handle_buffer: handles of the the child controllers
2937 * The allocated buffer has to be freed with free().
2939 * Return: status code
2941 static efi_status_t
efi_get_child_controllers(
2942 struct efi_object
*efiobj
,
2943 efi_handle_t driver_handle
,
2944 efi_uintn_t
*number_of_children
,
2945 efi_handle_t
**child_handle_buffer
)
2947 struct efi_handler
*handler
;
2948 struct efi_open_protocol_info_item
*item
;
2949 efi_uintn_t count
= 0, i
;
2952 /* Count all child controller associations */
2953 list_for_each_entry(handler
, &efiobj
->protocols
, link
) {
2954 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2955 if (item
->info
.agent_handle
== driver_handle
&&
2956 item
->info
.attributes
&
2957 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
)
2962 * Create buffer. In case of duplicate child controller assignments
2963 * the buffer will be too large. But that does not harm.
2965 *number_of_children
= 0;
2966 *child_handle_buffer
= calloc(count
, sizeof(efi_handle_t
));
2967 if (!*child_handle_buffer
)
2968 return EFI_OUT_OF_RESOURCES
;
2969 /* Copy unique child handles */
2970 list_for_each_entry(handler
, &efiobj
->protocols
, link
) {
2971 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2972 if (item
->info
.agent_handle
== driver_handle
&&
2973 item
->info
.attributes
&
2974 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) {
2975 /* Check this is a new child controller */
2977 for (i
= 0; i
< *number_of_children
; ++i
) {
2978 if ((*child_handle_buffer
)[i
] ==
2979 item
->info
.controller_handle
)
2982 /* Copy handle to buffer */
2984 i
= (*number_of_children
)++;
2985 (*child_handle_buffer
)[i
] =
2986 item
->info
.controller_handle
;
2995 * efi_disconnect_controller() - disconnect a controller from a driver
2996 * @controller_handle: handle of the controller
2997 * @driver_image_handle: handle of the driver
2998 * @child_handle: handle of the child to destroy
3000 * This function implements the DisconnectController service.
3002 * See the Unified Extensible Firmware Interface (UEFI) specification for
3005 * Return: status code
3007 static efi_status_t EFIAPI
efi_disconnect_controller(
3008 efi_handle_t controller_handle
,
3009 efi_handle_t driver_image_handle
,
3010 efi_handle_t child_handle
)
3012 struct efi_driver_binding_protocol
*binding_protocol
;
3013 efi_handle_t
*child_handle_buffer
= NULL
;
3014 size_t number_of_children
= 0;
3016 size_t stop_count
= 0;
3017 struct efi_object
*efiobj
;
3019 EFI_ENTRY("%p, %p, %p", controller_handle
, driver_image_handle
,
3022 efiobj
= efi_search_obj(controller_handle
);
3024 r
= EFI_INVALID_PARAMETER
;
3028 if (child_handle
&& !efi_search_obj(child_handle
)) {
3029 r
= EFI_INVALID_PARAMETER
;
3033 /* If no driver handle is supplied, disconnect all drivers */
3034 if (!driver_image_handle
) {
3035 r
= efi_disconnect_all_drivers(efiobj
, NULL
, child_handle
);
3039 /* Create list of child handles */
3041 number_of_children
= 1;
3042 child_handle_buffer
= &child_handle
;
3044 efi_get_child_controllers(efiobj
,
3045 driver_image_handle
,
3046 &number_of_children
,
3047 &child_handle_buffer
);
3050 /* Get the driver binding protocol */
3051 r
= EFI_CALL(efi_open_protocol(driver_image_handle
,
3052 &efi_guid_driver_binding_protocol
,
3053 (void **)&binding_protocol
,
3054 driver_image_handle
, NULL
,
3055 EFI_OPEN_PROTOCOL_GET_PROTOCOL
));
3056 if (r
!= EFI_SUCCESS
)
3058 /* Remove the children */
3059 if (number_of_children
) {
3060 r
= EFI_CALL(binding_protocol
->stop(binding_protocol
,
3063 child_handle_buffer
));
3064 if (r
== EFI_SUCCESS
)
3067 /* Remove the driver */
3069 r
= EFI_CALL(binding_protocol
->stop(binding_protocol
,
3072 if (r
== EFI_SUCCESS
)
3074 EFI_CALL(efi_close_protocol(driver_image_handle
,
3075 &efi_guid_driver_binding_protocol
,
3076 driver_image_handle
, NULL
));
3084 free(child_handle_buffer
);
3088 static struct efi_boot_services efi_boot_services
= {
3090 .signature
= EFI_BOOT_SERVICES_SIGNATURE
,
3091 .revision
= EFI_SPECIFICATION_VERSION
,
3092 .headersize
= sizeof(struct efi_boot_services
),
3094 .raise_tpl
= efi_raise_tpl
,
3095 .restore_tpl
= efi_restore_tpl
,
3096 .allocate_pages
= efi_allocate_pages_ext
,
3097 .free_pages
= efi_free_pages_ext
,
3098 .get_memory_map
= efi_get_memory_map_ext
,
3099 .allocate_pool
= efi_allocate_pool_ext
,
3100 .free_pool
= efi_free_pool_ext
,
3101 .create_event
= efi_create_event_ext
,
3102 .set_timer
= efi_set_timer_ext
,
3103 .wait_for_event
= efi_wait_for_event
,
3104 .signal_event
= efi_signal_event_ext
,
3105 .close_event
= efi_close_event
,
3106 .check_event
= efi_check_event
,
3107 .install_protocol_interface
= efi_install_protocol_interface
,
3108 .reinstall_protocol_interface
= efi_reinstall_protocol_interface
,
3109 .uninstall_protocol_interface
= efi_uninstall_protocol_interface
,
3110 .handle_protocol
= efi_handle_protocol
,
3112 .register_protocol_notify
= efi_register_protocol_notify
,
3113 .locate_handle
= efi_locate_handle_ext
,
3114 .locate_device_path
= efi_locate_device_path
,
3115 .install_configuration_table
= efi_install_configuration_table_ext
,
3116 .load_image
= efi_load_image
,
3117 .start_image
= efi_start_image
,
3119 .unload_image
= efi_unload_image
,
3120 .exit_boot_services
= efi_exit_boot_services
,
3121 .get_next_monotonic_count
= efi_get_next_monotonic_count
,
3123 .set_watchdog_timer
= efi_set_watchdog_timer
,
3124 .connect_controller
= efi_connect_controller
,
3125 .disconnect_controller
= efi_disconnect_controller
,
3126 .open_protocol
= efi_open_protocol
,
3127 .close_protocol
= efi_close_protocol
,
3128 .open_protocol_information
= efi_open_protocol_information
,
3129 .protocols_per_handle
= efi_protocols_per_handle
,
3130 .locate_handle_buffer
= efi_locate_handle_buffer
,
3131 .locate_protocol
= efi_locate_protocol
,
3132 .install_multiple_protocol_interfaces
=
3133 efi_install_multiple_protocol_interfaces
,
3134 .uninstall_multiple_protocol_interfaces
=
3135 efi_uninstall_multiple_protocol_interfaces
,
3136 .calculate_crc32
= efi_calculate_crc32
,
3137 .copy_mem
= efi_copy_mem
,
3138 .set_mem
= efi_set_mem
,
3139 .create_event_ex
= efi_create_event_ex
,
3142 static u16 __efi_runtime_data firmware_vendor
[] = L
"Das U-Boot";
3144 struct efi_system_table __efi_runtime_data systab
= {
3146 .signature
= EFI_SYSTEM_TABLE_SIGNATURE
,
3147 .revision
= EFI_SPECIFICATION_VERSION
,
3148 .headersize
= sizeof(struct efi_system_table
),
3150 .fw_vendor
= firmware_vendor
,
3151 .fw_revision
= FW_VERSION
<< 16 | FW_PATCHLEVEL
<< 8,
3152 .con_in
= (void *)&efi_con_in
,
3153 .con_out
= (void *)&efi_con_out
,
3154 .std_err
= (void *)&efi_con_out
,
3155 .runtime
= (void *)&efi_runtime_services
,
3156 .boottime
= (void *)&efi_boot_services
,
3162 * efi_initialize_system_table() - Initialize system table
3164 * Return: status code
3166 efi_status_t
efi_initialize_system_table(void)
3170 /* Allocate configuration table array */
3171 ret
= efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA
,
3172 EFI_MAX_CONFIGURATION_TABLES
*
3173 sizeof(struct efi_configuration_table
),
3174 (void **)&systab
.tables
);
3176 /* Set CRC32 field in table headers */
3177 efi_update_table_header_crc32(&systab
.hdr
);
3178 efi_update_table_header_crc32(&efi_runtime_services
.hdr
);
3179 efi_update_table_header_crc32(&efi_boot_services
.hdr
);