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
);
29 /* Handle of the currently executing image */
30 static efi_handle_t current_image
;
33 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
34 * we need to do trickery with caches. Since we don't want to break the EFI
35 * aware boot path, only apply hacks when loading exiting directly (breaking
36 * direct Linux EFI booting along the way - oh well).
38 static bool efi_is_direct_boot
= true;
42 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
43 * fixed when compiling U-Boot. However, the payload does not know about that
44 * restriction so we need to manually swap its and our view of that register on
45 * EFI callback entry/exit.
47 static volatile void *efi_gd
, *app_gd
;
50 /* 1 if inside U-Boot code, 0 if inside EFI payload code */
51 static int entry_count
= 1;
52 static int nesting_level
;
53 /* GUID of the device tree table */
54 const efi_guid_t efi_guid_fdt
= EFI_FDT_GUID
;
55 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
56 const efi_guid_t efi_guid_driver_binding_protocol
=
57 EFI_DRIVER_BINDING_PROTOCOL_GUID
;
59 /* event group ExitBootServices() invoked */
60 const efi_guid_t efi_guid_event_group_exit_boot_services
=
61 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES
;
62 /* event group SetVirtualAddressMap() invoked */
63 const efi_guid_t efi_guid_event_group_virtual_address_change
=
64 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE
;
65 /* event group memory map changed */
66 const efi_guid_t efi_guid_event_group_memory_map_change
=
67 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE
;
68 /* event group boot manager about to boot */
69 const efi_guid_t efi_guid_event_group_ready_to_boot
=
70 EFI_EVENT_GROUP_READY_TO_BOOT
;
71 /* event group ResetSystem() invoked (before ExitBootServices) */
72 const efi_guid_t efi_guid_event_group_reset_system
=
73 EFI_EVENT_GROUP_RESET_SYSTEM
;
75 static efi_status_t EFIAPI
efi_disconnect_controller(
76 efi_handle_t controller_handle
,
77 efi_handle_t driver_image_handle
,
78 efi_handle_t child_handle
);
80 /* Called on every callback entry */
81 int __efi_entry_check(void)
83 int ret
= entry_count
++ == 0;
92 /* Called on every callback exit */
93 int __efi_exit_check(void)
95 int ret
= --entry_count
== 0;
102 /* Called from do_bootefi_exec() */
103 void efi_save_gd(void)
111 * Special case handler for error/abort that just forces things back to u-boot
112 * world so we can dump out an abort message, without any care about returning
113 * back to UEFI world.
115 void efi_restore_gd(void)
118 /* Only restore if we're already in EFI context */
126 * indent_string() - returns a string for indenting with two spaces per level
127 * @level: indent level
129 * A maximum of ten indent levels is supported. Higher indent levels will be
132 * Return: A string for indenting with two spaces per level is
135 static const char *indent_string(int level
)
137 const char *indent
= " ";
138 const int max
= strlen(indent
);
140 level
= min(max
, level
* 2);
141 return &indent
[max
- level
];
144 const char *__efi_nesting(void)
146 return indent_string(nesting_level
);
149 const char *__efi_nesting_inc(void)
151 return indent_string(nesting_level
++);
154 const char *__efi_nesting_dec(void)
156 return indent_string(--nesting_level
);
160 * efi_queue_event() - queue an EFI event
161 * @event: event to signal
162 * @check_tpl: check the TPL level
164 * This function queues the notification function of the event for future
167 * The notification function is called if the task priority level of the event
168 * is higher than the current task priority level.
170 * For the SignalEvent service see efi_signal_event_ext.
173 static void efi_queue_event(struct efi_event
*event
, bool check_tpl
)
175 if (event
->notify_function
) {
176 event
->is_queued
= true;
178 if (check_tpl
&& efi_tpl
>= event
->notify_tpl
)
180 EFI_CALL_VOID(event
->notify_function(event
,
181 event
->notify_context
));
183 event
->is_queued
= false;
187 * is_valid_tpl() - check if the task priority level is valid
189 * @tpl: TPL level to check
190 * Return: status code
192 efi_status_t
is_valid_tpl(efi_uintn_t tpl
)
195 case TPL_APPLICATION
:
201 return EFI_INVALID_PARAMETER
;
206 * efi_signal_event() - signal an EFI event
207 * @event: event to signal
208 * @check_tpl: check the TPL level
210 * This function signals an event. If the event belongs to an event group all
211 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
212 * their notification function is queued.
214 * For the SignalEvent service see efi_signal_event_ext.
216 void efi_signal_event(struct efi_event
*event
, bool check_tpl
)
219 struct efi_event
*evt
;
222 * The signaled state has to set before executing any
223 * notification function
225 list_for_each_entry(evt
, &efi_events
, link
) {
226 if (!evt
->group
|| guidcmp(evt
->group
, event
->group
))
228 if (evt
->is_signaled
)
230 evt
->is_signaled
= true;
231 if (evt
->type
& EVT_NOTIFY_SIGNAL
&&
232 evt
->notify_function
)
233 evt
->is_queued
= true;
235 list_for_each_entry(evt
, &efi_events
, link
) {
236 if (!evt
->group
|| guidcmp(evt
->group
, event
->group
))
239 efi_queue_event(evt
, check_tpl
);
241 } else if (!event
->is_signaled
) {
242 event
->is_signaled
= true;
243 if (event
->type
& EVT_NOTIFY_SIGNAL
)
244 efi_queue_event(event
, check_tpl
);
249 * efi_raise_tpl() - raise the task priority level
250 * @new_tpl: new value of the task priority level
252 * This function implements the RaiseTpl service.
254 * See the Unified Extensible Firmware Interface (UEFI) specification for
257 * Return: old value of the task priority level
259 static unsigned long EFIAPI
efi_raise_tpl(efi_uintn_t new_tpl
)
261 efi_uintn_t old_tpl
= efi_tpl
;
263 EFI_ENTRY("0x%zx", new_tpl
);
265 if (new_tpl
< efi_tpl
)
266 debug("WARNING: new_tpl < current_tpl in %s\n", __func__
);
268 if (efi_tpl
> TPL_HIGH_LEVEL
)
269 efi_tpl
= TPL_HIGH_LEVEL
;
271 EFI_EXIT(EFI_SUCCESS
);
276 * efi_restore_tpl() - lower the task priority level
277 * @old_tpl: value of the task priority level to be restored
279 * This function implements the RestoreTpl service.
281 * See the Unified Extensible Firmware Interface (UEFI) specification for
284 static void EFIAPI
efi_restore_tpl(efi_uintn_t old_tpl
)
286 EFI_ENTRY("0x%zx", old_tpl
);
288 if (old_tpl
> efi_tpl
)
289 debug("WARNING: old_tpl > current_tpl in %s\n", __func__
);
291 if (efi_tpl
> TPL_HIGH_LEVEL
)
292 efi_tpl
= TPL_HIGH_LEVEL
;
295 * Lowering the TPL may have made queued events eligible for execution.
299 EFI_EXIT(EFI_SUCCESS
);
303 * efi_allocate_pages_ext() - allocate memory pages
304 * @type: type of allocation to be performed
305 * @memory_type: usage type of the allocated memory
306 * @pages: number of pages to be allocated
307 * @memory: allocated memory
309 * This function implements the AllocatePages service.
311 * See the Unified Extensible Firmware Interface (UEFI) specification for
314 * Return: status code
316 static efi_status_t EFIAPI
efi_allocate_pages_ext(int type
, int memory_type
,
322 EFI_ENTRY("%d, %d, 0x%zx, %p", type
, memory_type
, pages
, memory
);
323 r
= efi_allocate_pages(type
, memory_type
, pages
, memory
);
328 * efi_free_pages_ext() - Free memory pages.
329 * @memory: start of the memory area to be freed
330 * @pages: number of pages to be freed
332 * This function implements the FreePages service.
334 * See the Unified Extensible Firmware Interface (UEFI) specification for
337 * Return: status code
339 static efi_status_t EFIAPI
efi_free_pages_ext(uint64_t memory
,
344 EFI_ENTRY("%llx, 0x%zx", memory
, pages
);
345 r
= efi_free_pages(memory
, pages
);
350 * efi_get_memory_map_ext() - get map describing memory usage
351 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
352 * on exit the size of the copied memory map
353 * @memory_map: buffer to which the memory map is written
354 * @map_key: key for the memory map
355 * @descriptor_size: size of an individual memory descriptor
356 * @descriptor_version: version number of the memory descriptor structure
358 * This function implements the GetMemoryMap service.
360 * See the Unified Extensible Firmware Interface (UEFI) specification for
363 * Return: status code
365 static efi_status_t EFIAPI
efi_get_memory_map_ext(
366 efi_uintn_t
*memory_map_size
,
367 struct efi_mem_desc
*memory_map
,
368 efi_uintn_t
*map_key
,
369 efi_uintn_t
*descriptor_size
,
370 uint32_t *descriptor_version
)
374 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size
, memory_map
,
375 map_key
, descriptor_size
, descriptor_version
);
376 r
= efi_get_memory_map(memory_map_size
, memory_map
, map_key
,
377 descriptor_size
, descriptor_version
);
382 * efi_allocate_pool_ext() - allocate memory from pool
383 * @pool_type: type of the pool from which memory is to be allocated
384 * @size: number of bytes to be allocated
385 * @buffer: allocated memory
387 * This function implements the AllocatePool service.
389 * See the Unified Extensible Firmware Interface (UEFI) specification for
392 * Return: status code
394 static efi_status_t EFIAPI
efi_allocate_pool_ext(int pool_type
,
400 EFI_ENTRY("%d, %zd, %p", pool_type
, size
, buffer
);
401 r
= efi_allocate_pool(pool_type
, size
, buffer
);
406 * efi_free_pool_ext() - free memory from pool
407 * @buffer: start of memory to be freed
409 * This function implements the FreePool service.
411 * See the Unified Extensible Firmware Interface (UEFI) specification for
414 * Return: status code
416 static efi_status_t EFIAPI
efi_free_pool_ext(void *buffer
)
420 EFI_ENTRY("%p", buffer
);
421 r
= efi_free_pool(buffer
);
426 * efi_add_handle() - add a new object to the object list
427 * @obj: object to be added
429 * The protocols list is initialized. The object handle is set.
431 void efi_add_handle(efi_handle_t handle
)
435 INIT_LIST_HEAD(&handle
->protocols
);
436 list_add_tail(&handle
->link
, &efi_obj_list
);
440 * efi_create_handle() - create handle
441 * @handle: new handle
443 * Return: status code
445 efi_status_t
efi_create_handle(efi_handle_t
*handle
)
447 struct efi_object
*obj
;
449 obj
= calloc(1, sizeof(struct efi_object
));
451 return EFI_OUT_OF_RESOURCES
;
460 * efi_search_protocol() - find a protocol on a handle.
462 * @protocol_guid: GUID of the protocol
463 * @handler: reference to the protocol
465 * Return: status code
467 efi_status_t
efi_search_protocol(const efi_handle_t handle
,
468 const efi_guid_t
*protocol_guid
,
469 struct efi_handler
**handler
)
471 struct efi_object
*efiobj
;
472 struct list_head
*lhandle
;
474 if (!handle
|| !protocol_guid
)
475 return EFI_INVALID_PARAMETER
;
476 efiobj
= efi_search_obj(handle
);
478 return EFI_INVALID_PARAMETER
;
479 list_for_each(lhandle
, &efiobj
->protocols
) {
480 struct efi_handler
*protocol
;
482 protocol
= list_entry(lhandle
, struct efi_handler
, link
);
483 if (!guidcmp(protocol
->guid
, protocol_guid
)) {
489 return EFI_NOT_FOUND
;
493 * efi_remove_protocol() - delete protocol from a handle
494 * @handle: handle from which the protocol shall be deleted
495 * @protocol: GUID of the protocol to be deleted
496 * @protocol_interface: interface of the protocol implementation
498 * Return: status code
500 efi_status_t
efi_remove_protocol(const efi_handle_t handle
,
501 const efi_guid_t
*protocol
,
502 void *protocol_interface
)
504 struct efi_handler
*handler
;
507 ret
= efi_search_protocol(handle
, protocol
, &handler
);
508 if (ret
!= EFI_SUCCESS
)
510 if (guidcmp(handler
->guid
, protocol
))
511 return EFI_INVALID_PARAMETER
;
512 if (handler
->protocol_interface
!= protocol_interface
)
513 return EFI_INVALID_PARAMETER
;
514 list_del(&handler
->link
);
520 * efi_remove_all_protocols() - delete all protocols from a handle
521 * @handle: handle from which the protocols shall be deleted
523 * Return: status code
525 efi_status_t
efi_remove_all_protocols(const efi_handle_t handle
)
527 struct efi_object
*efiobj
;
528 struct efi_handler
*protocol
;
529 struct efi_handler
*pos
;
531 efiobj
= efi_search_obj(handle
);
533 return EFI_INVALID_PARAMETER
;
534 list_for_each_entry_safe(protocol
, pos
, &efiobj
->protocols
, link
) {
537 ret
= efi_remove_protocol(handle
, protocol
->guid
,
538 protocol
->protocol_interface
);
539 if (ret
!= EFI_SUCCESS
)
546 * efi_delete_handle() - delete handle
548 * @obj: handle to delete
550 void efi_delete_handle(efi_handle_t handle
)
554 efi_remove_all_protocols(handle
);
555 list_del(&handle
->link
);
560 * efi_is_event() - check if a pointer is a valid event
561 * @event: pointer to check
563 * Return: status code
565 static efi_status_t
efi_is_event(const struct efi_event
*event
)
567 const struct efi_event
*evt
;
570 return EFI_INVALID_PARAMETER
;
571 list_for_each_entry(evt
, &efi_events
, link
) {
575 return EFI_INVALID_PARAMETER
;
579 * efi_create_event() - create an event
580 * @type: type of the event to create
581 * @notify_tpl: task priority level of the event
582 * @notify_function: notification function of the event
583 * @notify_context: pointer passed to the notification function
584 * @group: event group
585 * @event: created event
587 * This function is used inside U-Boot code to create an event.
589 * For the API function implementing the CreateEvent service see
590 * efi_create_event_ext.
592 * Return: status code
594 efi_status_t
efi_create_event(uint32_t type
, efi_uintn_t notify_tpl
,
595 void (EFIAPI
*notify_function
) (
596 struct efi_event
*event
,
598 void *notify_context
, efi_guid_t
*group
,
599 struct efi_event
**event
)
601 struct efi_event
*evt
;
604 return EFI_INVALID_PARAMETER
;
609 case EVT_NOTIFY_SIGNAL
:
610 case EVT_TIMER
| EVT_NOTIFY_SIGNAL
:
611 case EVT_NOTIFY_WAIT
:
612 case EVT_TIMER
| EVT_NOTIFY_WAIT
:
613 case EVT_SIGNAL_EXIT_BOOT_SERVICES
:
614 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
:
617 return EFI_INVALID_PARAMETER
;
620 if ((type
& (EVT_NOTIFY_WAIT
| EVT_NOTIFY_SIGNAL
)) &&
621 (is_valid_tpl(notify_tpl
) != EFI_SUCCESS
))
622 return EFI_INVALID_PARAMETER
;
624 evt
= calloc(1, sizeof(struct efi_event
));
626 return EFI_OUT_OF_RESOURCES
;
628 evt
->notify_tpl
= notify_tpl
;
629 evt
->notify_function
= notify_function
;
630 evt
->notify_context
= notify_context
;
632 /* Disable timers on boot up */
633 evt
->trigger_next
= -1ULL;
634 evt
->is_queued
= false;
635 evt
->is_signaled
= false;
636 list_add_tail(&evt
->link
, &efi_events
);
642 * efi_create_event_ex() - create an event in a group
643 * @type: type of the event to create
644 * @notify_tpl: task priority level of the event
645 * @notify_function: notification function of the event
646 * @notify_context: pointer passed to the notification function
647 * @event: created event
648 * @event_group: event group
650 * This function implements the CreateEventEx service.
652 * See the Unified Extensible Firmware Interface (UEFI) specification for
655 * Return: status code
657 efi_status_t EFIAPI
efi_create_event_ex(uint32_t type
, efi_uintn_t notify_tpl
,
658 void (EFIAPI
*notify_function
) (
659 struct efi_event
*event
,
661 void *notify_context
,
662 efi_guid_t
*event_group
,
663 struct efi_event
**event
)
665 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type
, notify_tpl
, notify_function
,
666 notify_context
, event_group
);
667 return EFI_EXIT(efi_create_event(type
, notify_tpl
, notify_function
,
668 notify_context
, event_group
, event
));
672 * efi_create_event_ext() - create an event
673 * @type: type of the event to create
674 * @notify_tpl: task priority level of the event
675 * @notify_function: notification function of the event
676 * @notify_context: pointer passed to the notification function
677 * @event: created event
679 * This function implements the CreateEvent service.
681 * See the Unified Extensible Firmware Interface (UEFI) specification for
684 * Return: status code
686 static efi_status_t EFIAPI
efi_create_event_ext(
687 uint32_t type
, efi_uintn_t notify_tpl
,
688 void (EFIAPI
*notify_function
) (
689 struct efi_event
*event
,
691 void *notify_context
, struct efi_event
**event
)
693 EFI_ENTRY("%d, 0x%zx, %p, %p", type
, notify_tpl
, notify_function
,
695 return EFI_EXIT(efi_create_event(type
, notify_tpl
, notify_function
,
696 notify_context
, NULL
, event
));
700 * efi_timer_check() - check if a timer event has occurred
702 * Check if a timer event has occurred or a queued notification function should
705 * Our timers have to work without interrupts, so we check whenever keyboard
706 * input or disk accesses happen if enough time elapsed for them to fire.
708 void efi_timer_check(void)
710 struct efi_event
*evt
;
711 u64 now
= timer_get_us();
713 list_for_each_entry(evt
, &efi_events
, link
) {
715 efi_queue_event(evt
, true);
716 if (!(evt
->type
& EVT_TIMER
) || now
< evt
->trigger_next
)
718 switch (evt
->trigger_type
) {
719 case EFI_TIMER_RELATIVE
:
720 evt
->trigger_type
= EFI_TIMER_STOP
;
722 case EFI_TIMER_PERIODIC
:
723 evt
->trigger_next
+= evt
->trigger_time
;
728 evt
->is_signaled
= false;
729 efi_signal_event(evt
, true);
735 * efi_set_timer() - set the trigger time for a timer event or stop the event
736 * @event: event for which the timer is set
737 * @type: type of the timer
738 * @trigger_time: trigger period in multiples of 100 ns
740 * This is the function for internal usage in U-Boot. For the API function
741 * implementing the SetTimer service see efi_set_timer_ext.
743 * Return: status code
745 efi_status_t
efi_set_timer(struct efi_event
*event
, enum efi_timer_delay type
,
746 uint64_t trigger_time
)
748 /* Check that the event is valid */
749 if (efi_is_event(event
) != EFI_SUCCESS
|| !(event
->type
& EVT_TIMER
))
750 return EFI_INVALID_PARAMETER
;
753 * The parameter defines a multiple of 100 ns.
754 * We use multiples of 1000 ns. So divide by 10.
756 do_div(trigger_time
, 10);
760 event
->trigger_next
= -1ULL;
762 case EFI_TIMER_PERIODIC
:
763 case EFI_TIMER_RELATIVE
:
764 event
->trigger_next
= timer_get_us() + trigger_time
;
767 return EFI_INVALID_PARAMETER
;
769 event
->trigger_type
= type
;
770 event
->trigger_time
= trigger_time
;
771 event
->is_signaled
= false;
776 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
778 * @event: event for which the timer is set
779 * @type: type of the timer
780 * @trigger_time: trigger period in multiples of 100 ns
782 * This function implements the SetTimer service.
784 * See the Unified Extensible Firmware Interface (UEFI) specification for
788 * Return: status code
790 static efi_status_t EFIAPI
efi_set_timer_ext(struct efi_event
*event
,
791 enum efi_timer_delay type
,
792 uint64_t trigger_time
)
794 EFI_ENTRY("%p, %d, %llx", event
, type
, trigger_time
);
795 return EFI_EXIT(efi_set_timer(event
, type
, trigger_time
));
799 * efi_wait_for_event() - wait for events to be signaled
800 * @num_events: number of events to be waited for
801 * @event: events to be waited for
802 * @index: index of the event that was signaled
804 * This function implements the WaitForEvent service.
806 * See the Unified Extensible Firmware Interface (UEFI) specification for
809 * Return: status code
811 static efi_status_t EFIAPI
efi_wait_for_event(efi_uintn_t num_events
,
812 struct efi_event
**event
,
817 EFI_ENTRY("%zd, %p, %p", num_events
, event
, index
);
819 /* Check parameters */
820 if (!num_events
|| !event
)
821 return EFI_EXIT(EFI_INVALID_PARAMETER
);
823 if (efi_tpl
!= TPL_APPLICATION
)
824 return EFI_EXIT(EFI_UNSUPPORTED
);
825 for (i
= 0; i
< num_events
; ++i
) {
826 if (efi_is_event(event
[i
]) != EFI_SUCCESS
)
827 return EFI_EXIT(EFI_INVALID_PARAMETER
);
828 if (!event
[i
]->type
|| event
[i
]->type
& EVT_NOTIFY_SIGNAL
)
829 return EFI_EXIT(EFI_INVALID_PARAMETER
);
830 if (!event
[i
]->is_signaled
)
831 efi_queue_event(event
[i
], true);
834 /* Wait for signal */
836 for (i
= 0; i
< num_events
; ++i
) {
837 if (event
[i
]->is_signaled
)
840 /* Allow events to occur. */
846 * Reset the signal which is passed to the caller to allow periodic
849 event
[i
]->is_signaled
= false;
853 return EFI_EXIT(EFI_SUCCESS
);
857 * efi_signal_event_ext() - signal an EFI event
858 * @event: event to signal
860 * This function implements the SignalEvent service.
862 * See the Unified Extensible Firmware Interface (UEFI) specification for
865 * This functions sets the signaled state of the event and queues the
866 * notification function for execution.
868 * Return: status code
870 static efi_status_t EFIAPI
efi_signal_event_ext(struct efi_event
*event
)
872 EFI_ENTRY("%p", event
);
873 if (efi_is_event(event
) != EFI_SUCCESS
)
874 return EFI_EXIT(EFI_INVALID_PARAMETER
);
875 efi_signal_event(event
, true);
876 return EFI_EXIT(EFI_SUCCESS
);
880 * efi_close_event() - close an EFI event
881 * @event: event to close
883 * This function implements the CloseEvent service.
885 * See the Unified Extensible Firmware Interface (UEFI) specification for
888 * Return: status code
890 static efi_status_t EFIAPI
efi_close_event(struct efi_event
*event
)
892 EFI_ENTRY("%p", event
);
893 if (efi_is_event(event
) != EFI_SUCCESS
)
894 return EFI_EXIT(EFI_INVALID_PARAMETER
);
895 list_del(&event
->link
);
897 return EFI_EXIT(EFI_SUCCESS
);
901 * efi_check_event() - check if an event is signaled
902 * @event: event to check
904 * This function implements the CheckEvent service.
906 * See the Unified Extensible Firmware Interface (UEFI) specification for
909 * If an event is not signaled yet, the notification function is queued. The
910 * signaled state is cleared.
912 * Return: status code
914 static efi_status_t EFIAPI
efi_check_event(struct efi_event
*event
)
916 EFI_ENTRY("%p", event
);
918 if (efi_is_event(event
) != EFI_SUCCESS
||
919 event
->type
& EVT_NOTIFY_SIGNAL
)
920 return EFI_EXIT(EFI_INVALID_PARAMETER
);
921 if (!event
->is_signaled
)
922 efi_queue_event(event
, true);
923 if (event
->is_signaled
) {
924 event
->is_signaled
= false;
925 return EFI_EXIT(EFI_SUCCESS
);
927 return EFI_EXIT(EFI_NOT_READY
);
931 * efi_search_obj() - find the internal EFI object for a handle
932 * @handle: handle to find
936 struct efi_object
*efi_search_obj(const efi_handle_t handle
)
938 struct efi_object
*efiobj
;
940 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
941 if (efiobj
== handle
)
949 * efi_open_protocol_info_entry() - create open protocol info entry and add it
951 * @handler: handler of a protocol
953 * Return: open protocol info entry
955 static struct efi_open_protocol_info_entry
*efi_create_open_info(
956 struct efi_handler
*handler
)
958 struct efi_open_protocol_info_item
*item
;
960 item
= calloc(1, sizeof(struct efi_open_protocol_info_item
));
963 /* Append the item to the open protocol info list. */
964 list_add_tail(&item
->link
, &handler
->open_infos
);
970 * efi_delete_open_info() - remove an open protocol info entry from a protocol
971 * @item: open protocol info entry to delete
973 * Return: status code
975 static efi_status_t
efi_delete_open_info(
976 struct efi_open_protocol_info_item
*item
)
978 list_del(&item
->link
);
984 * efi_add_protocol() - install new protocol on a handle
985 * @handle: handle on which the protocol shall be installed
986 * @protocol: GUID of the protocol to be installed
987 * @protocol_interface: interface of the protocol implementation
989 * Return: status code
991 efi_status_t
efi_add_protocol(const efi_handle_t handle
,
992 const efi_guid_t
*protocol
,
993 void *protocol_interface
)
995 struct efi_object
*efiobj
;
996 struct efi_handler
*handler
;
999 efiobj
= efi_search_obj(handle
);
1001 return EFI_INVALID_PARAMETER
;
1002 ret
= efi_search_protocol(handle
, protocol
, NULL
);
1003 if (ret
!= EFI_NOT_FOUND
)
1004 return EFI_INVALID_PARAMETER
;
1005 handler
= calloc(1, sizeof(struct efi_handler
));
1007 return EFI_OUT_OF_RESOURCES
;
1008 handler
->guid
= protocol
;
1009 handler
->protocol_interface
= protocol_interface
;
1010 INIT_LIST_HEAD(&handler
->open_infos
);
1011 list_add_tail(&handler
->link
, &efiobj
->protocols
);
1012 if (!guidcmp(&efi_guid_device_path
, protocol
))
1013 EFI_PRINT("installed device path '%pD'\n", protocol_interface
);
1018 * efi_install_protocol_interface() - install protocol interface
1019 * @handle: handle on which the protocol shall be installed
1020 * @protocol: GUID of the protocol to be installed
1021 * @protocol_interface_type: type of the interface to be installed,
1022 * always EFI_NATIVE_INTERFACE
1023 * @protocol_interface: interface of the protocol implementation
1025 * This function implements the InstallProtocolInterface service.
1027 * See the Unified Extensible Firmware Interface (UEFI) specification for
1030 * Return: status code
1032 static efi_status_t EFIAPI
efi_install_protocol_interface(
1033 efi_handle_t
*handle
, const efi_guid_t
*protocol
,
1034 int protocol_interface_type
, void *protocol_interface
)
1038 EFI_ENTRY("%p, %pUl, %d, %p", handle
, protocol
, protocol_interface_type
,
1039 protocol_interface
);
1041 if (!handle
|| !protocol
||
1042 protocol_interface_type
!= EFI_NATIVE_INTERFACE
) {
1043 r
= EFI_INVALID_PARAMETER
;
1047 /* Create new handle if requested. */
1049 r
= efi_create_handle(handle
);
1050 if (r
!= EFI_SUCCESS
)
1052 debug("%sEFI: new handle %p\n", indent_string(nesting_level
),
1055 debug("%sEFI: handle %p\n", indent_string(nesting_level
),
1058 /* Add new protocol */
1059 r
= efi_add_protocol(*handle
, protocol
, protocol_interface
);
1065 * efi_get_drivers() - get all drivers associated to a controller
1066 * @handle: handle of the controller
1067 * @protocol: protocol GUID (optional)
1068 * @number_of_drivers: number of child controllers
1069 * @driver_handle_buffer: handles of the the drivers
1071 * The allocated buffer has to be freed with free().
1073 * Return: status code
1075 static efi_status_t
efi_get_drivers(efi_handle_t handle
,
1076 const efi_guid_t
*protocol
,
1077 efi_uintn_t
*number_of_drivers
,
1078 efi_handle_t
**driver_handle_buffer
)
1080 struct efi_handler
*handler
;
1081 struct efi_open_protocol_info_item
*item
;
1082 efi_uintn_t count
= 0, i
;
1085 /* Count all driver associations */
1086 list_for_each_entry(handler
, &handle
->protocols
, link
) {
1087 if (protocol
&& guidcmp(handler
->guid
, protocol
))
1089 list_for_each_entry(item
, &handler
->open_infos
, link
) {
1090 if (item
->info
.attributes
&
1091 EFI_OPEN_PROTOCOL_BY_DRIVER
)
1096 * Create buffer. In case of duplicate driver assignments the buffer
1097 * will be too large. But that does not harm.
1099 *number_of_drivers
= 0;
1100 *driver_handle_buffer
= calloc(count
, sizeof(efi_handle_t
));
1101 if (!*driver_handle_buffer
)
1102 return EFI_OUT_OF_RESOURCES
;
1103 /* Collect unique driver handles */
1104 list_for_each_entry(handler
, &handle
->protocols
, link
) {
1105 if (protocol
&& guidcmp(handler
->guid
, protocol
))
1107 list_for_each_entry(item
, &handler
->open_infos
, link
) {
1108 if (item
->info
.attributes
&
1109 EFI_OPEN_PROTOCOL_BY_DRIVER
) {
1110 /* Check this is a new driver */
1112 for (i
= 0; i
< *number_of_drivers
; ++i
) {
1113 if ((*driver_handle_buffer
)[i
] ==
1114 item
->info
.agent_handle
)
1117 /* Copy handle to buffer */
1119 i
= (*number_of_drivers
)++;
1120 (*driver_handle_buffer
)[i
] =
1121 item
->info
.agent_handle
;
1130 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1131 * @handle: handle of the controller
1132 * @protocol: protocol GUID (optional)
1133 * @child_handle: handle of the child to destroy
1135 * This function implements the DisconnectController service.
1137 * See the Unified Extensible Firmware Interface (UEFI) specification for
1140 * Return: status code
1142 static efi_status_t efi_disconnect_all_drivers
1143 (efi_handle_t handle
,
1144 const efi_guid_t
*protocol
,
1145 efi_handle_t child_handle
)
1147 efi_uintn_t number_of_drivers
;
1148 efi_handle_t
*driver_handle_buffer
;
1149 efi_status_t r
, ret
;
1151 ret
= efi_get_drivers(handle
, protocol
, &number_of_drivers
,
1152 &driver_handle_buffer
);
1153 if (ret
!= EFI_SUCCESS
)
1156 ret
= EFI_NOT_FOUND
;
1157 while (number_of_drivers
) {
1158 r
= EFI_CALL(efi_disconnect_controller(
1160 driver_handle_buffer
[--number_of_drivers
],
1162 if (r
== EFI_SUCCESS
)
1165 free(driver_handle_buffer
);
1170 * efi_uninstall_protocol() - uninstall protocol interface
1172 * @handle: handle from which the protocol shall be removed
1173 * @protocol: GUID of the protocol to be removed
1174 * @protocol_interface: interface to be removed
1176 * This function DOES NOT delete a handle without installed protocol.
1178 * Return: status code
1180 static efi_status_t efi_uninstall_protocol
1181 (efi_handle_t handle
, const efi_guid_t
*protocol
,
1182 void *protocol_interface
)
1184 struct efi_object
*efiobj
;
1185 struct efi_handler
*handler
;
1186 struct efi_open_protocol_info_item
*item
;
1187 struct efi_open_protocol_info_item
*pos
;
1191 efiobj
= efi_search_obj(handle
);
1193 r
= EFI_INVALID_PARAMETER
;
1196 /* Find the protocol on the handle */
1197 r
= efi_search_protocol(handle
, protocol
, &handler
);
1198 if (r
!= EFI_SUCCESS
)
1200 /* Disconnect controllers */
1201 efi_disconnect_all_drivers(efiobj
, protocol
, NULL
);
1202 if (!list_empty(&handler
->open_infos
)) {
1203 r
= EFI_ACCESS_DENIED
;
1206 /* Close protocol */
1207 list_for_each_entry_safe(item
, pos
, &handler
->open_infos
, link
) {
1208 if (item
->info
.attributes
==
1209 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
||
1210 item
->info
.attributes
== EFI_OPEN_PROTOCOL_GET_PROTOCOL
||
1211 item
->info
.attributes
== EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)
1212 list_del(&item
->link
);
1214 if (!list_empty(&handler
->open_infos
)) {
1215 r
= EFI_ACCESS_DENIED
;
1218 r
= efi_remove_protocol(handle
, protocol
, protocol_interface
);
1224 * efi_uninstall_protocol_interface() - uninstall protocol interface
1225 * @handle: handle from which the protocol shall be removed
1226 * @protocol: GUID of the protocol to be removed
1227 * @protocol_interface: interface to be removed
1229 * This function implements the UninstallProtocolInterface service.
1231 * See the Unified Extensible Firmware Interface (UEFI) specification for
1234 * Return: status code
1236 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1237 (efi_handle_t handle
, const efi_guid_t
*protocol
,
1238 void *protocol_interface
)
1242 EFI_ENTRY("%p, %pUl, %p", handle
, protocol
, protocol_interface
);
1244 ret
= efi_uninstall_protocol(handle
, protocol
, protocol_interface
);
1245 if (ret
!= EFI_SUCCESS
)
1248 /* If the last protocol has been removed, delete the handle. */
1249 if (list_empty(&handle
->protocols
)) {
1250 list_del(&handle
->link
);
1254 return EFI_EXIT(ret
);
1258 * efi_register_protocol_notify() - register an event for notification when a
1259 * protocol is installed.
1260 * @protocol: GUID of the protocol whose installation shall be notified
1261 * @event: event to be signaled upon installation of the protocol
1262 * @registration: key for retrieving the registration information
1264 * This function implements the RegisterProtocolNotify service.
1265 * See the Unified Extensible Firmware Interface (UEFI) specification
1268 * Return: status code
1270 static efi_status_t EFIAPI
efi_register_protocol_notify(
1271 const efi_guid_t
*protocol
,
1272 struct efi_event
*event
,
1273 void **registration
)
1275 EFI_ENTRY("%pUl, %p, %p", protocol
, event
, registration
);
1276 return EFI_EXIT(EFI_OUT_OF_RESOURCES
);
1280 * efi_search() - determine if an EFI handle implements a protocol
1281 * @search_type: selection criterion
1282 * @protocol: GUID of the protocol
1283 * @search_key: registration key
1286 * See the documentation of the LocateHandle service in the UEFI specification.
1288 * Return: 0 if the handle implements the protocol
1290 static int efi_search(enum efi_locate_search_type search_type
,
1291 const efi_guid_t
*protocol
, void *search_key
,
1292 efi_handle_t handle
)
1296 switch (search_type
) {
1299 case BY_REGISTER_NOTIFY
:
1300 /* TODO: RegisterProtocolNotify is not implemented yet */
1303 ret
= efi_search_protocol(handle
, protocol
, NULL
);
1304 return (ret
!= EFI_SUCCESS
);
1306 /* Invalid search type */
1312 * efi_locate_handle() - locate handles implementing a protocol
1313 * @search_type: selection criterion
1314 * @protocol: GUID of the protocol
1315 * @search_key: registration key
1316 * @buffer_size: size of the buffer to receive the handles in bytes
1317 * @buffer: buffer to receive the relevant handles
1319 * This function is meant for U-Boot internal calls. For the API implementation
1320 * of the LocateHandle service see efi_locate_handle_ext.
1322 * Return: status code
1324 static efi_status_t
efi_locate_handle(
1325 enum efi_locate_search_type search_type
,
1326 const efi_guid_t
*protocol
, void *search_key
,
1327 efi_uintn_t
*buffer_size
, efi_handle_t
*buffer
)
1329 struct efi_object
*efiobj
;
1330 efi_uintn_t size
= 0;
1332 /* Check parameters */
1333 switch (search_type
) {
1336 case BY_REGISTER_NOTIFY
:
1338 return EFI_INVALID_PARAMETER
;
1339 /* RegisterProtocolNotify is not implemented yet */
1340 return EFI_UNSUPPORTED
;
1343 return EFI_INVALID_PARAMETER
;
1346 return EFI_INVALID_PARAMETER
;
1350 * efi_locate_handle_buffer uses this function for
1351 * the calculation of the necessary buffer size.
1352 * So do not require a buffer for buffersize == 0.
1354 if (!buffer_size
|| (*buffer_size
&& !buffer
))
1355 return EFI_INVALID_PARAMETER
;
1357 /* Count how much space we need */
1358 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
1359 if (!efi_search(search_type
, protocol
, search_key
, efiobj
))
1360 size
+= sizeof(void *);
1363 if (*buffer_size
< size
) {
1364 *buffer_size
= size
;
1365 return EFI_BUFFER_TOO_SMALL
;
1368 *buffer_size
= size
;
1370 return EFI_NOT_FOUND
;
1372 /* Then fill the array */
1373 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
1374 if (!efi_search(search_type
, protocol
, search_key
, efiobj
))
1382 * efi_locate_handle_ext() - locate handles implementing a protocol.
1383 * @search_type: selection criterion
1384 * @protocol: GUID of the protocol
1385 * @search_key: registration key
1386 * @buffer_size: size of the buffer to receive the handles in bytes
1387 * @buffer: buffer to receive the relevant handles
1389 * This function implements the LocateHandle service.
1391 * See the Unified Extensible Firmware Interface (UEFI) specification for
1394 * Return: 0 if the handle implements the protocol
1396 static efi_status_t EFIAPI
efi_locate_handle_ext(
1397 enum efi_locate_search_type search_type
,
1398 const efi_guid_t
*protocol
, void *search_key
,
1399 efi_uintn_t
*buffer_size
, efi_handle_t
*buffer
)
1401 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type
, protocol
, search_key
,
1402 buffer_size
, buffer
);
1404 return EFI_EXIT(efi_locate_handle(search_type
, protocol
, search_key
,
1405 buffer_size
, buffer
));
1409 * efi_remove_configuration_table() - collapses configuration table entries,
1412 * @i: index of the table entry to be removed
1414 static void efi_remove_configuration_table(int i
)
1416 struct efi_configuration_table
*this = &systab
.tables
[i
];
1417 struct efi_configuration_table
*next
= &systab
.tables
[i
+ 1];
1418 struct efi_configuration_table
*end
= &systab
.tables
[systab
.nr_tables
];
1420 memmove(this, next
, (ulong
)end
- (ulong
)next
);
1425 * efi_install_configuration_table() - adds, updates, or removes a
1426 * configuration table
1427 * @guid: GUID of the installed table
1428 * @table: table to be installed
1430 * This function is used for internal calls. For the API implementation of the
1431 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1433 * Return: status code
1435 efi_status_t
efi_install_configuration_table(const efi_guid_t
*guid
,
1438 struct efi_event
*evt
;
1442 return EFI_INVALID_PARAMETER
;
1444 /* Check for GUID override */
1445 for (i
= 0; i
< systab
.nr_tables
; i
++) {
1446 if (!guidcmp(guid
, &systab
.tables
[i
].guid
)) {
1448 systab
.tables
[i
].table
= table
;
1450 efi_remove_configuration_table(i
);
1456 return EFI_NOT_FOUND
;
1458 /* No override, check for overflow */
1459 if (i
>= EFI_MAX_CONFIGURATION_TABLES
)
1460 return EFI_OUT_OF_RESOURCES
;
1462 /* Add a new entry */
1463 memcpy(&systab
.tables
[i
].guid
, guid
, sizeof(*guid
));
1464 systab
.tables
[i
].table
= table
;
1465 systab
.nr_tables
= i
+ 1;
1468 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1469 efi_update_table_header_crc32(&systab
.hdr
);
1471 /* Notify that the configuration table was changed */
1472 list_for_each_entry(evt
, &efi_events
, link
) {
1473 if (evt
->group
&& !guidcmp(evt
->group
, guid
)) {
1474 efi_signal_event(evt
, false);
1483 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1484 * configuration table.
1485 * @guid: GUID of the installed table
1486 * @table: table to be installed
1488 * This function implements the InstallConfigurationTable service.
1490 * See the Unified Extensible Firmware Interface (UEFI) specification for
1493 * Return: status code
1495 static efi_status_t EFIAPI
efi_install_configuration_table_ext(efi_guid_t
*guid
,
1498 EFI_ENTRY("%pUl, %p", guid
, table
);
1499 return EFI_EXIT(efi_install_configuration_table(guid
, table
));
1503 * efi_setup_loaded_image() - initialize a loaded image
1505 * Initialize a loaded_image_info and loaded_image_info object with correct
1506 * protocols, boot-device, etc.
1508 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1511 * @device_path: device path of the loaded image
1512 * @file_path: file path of the loaded image
1513 * @handle_ptr: handle of the loaded image
1514 * @info_ptr: loaded image protocol
1515 * Return: status code
1517 efi_status_t
efi_setup_loaded_image(struct efi_device_path
*device_path
,
1518 struct efi_device_path
*file_path
,
1519 struct efi_loaded_image_obj
**handle_ptr
,
1520 struct efi_loaded_image
**info_ptr
)
1523 struct efi_loaded_image
*info
= NULL
;
1524 struct efi_loaded_image_obj
*obj
= NULL
;
1525 struct efi_device_path
*dp
;
1527 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1531 info
= calloc(1, sizeof(*info
));
1533 return EFI_OUT_OF_RESOURCES
;
1534 obj
= calloc(1, sizeof(*obj
));
1537 return EFI_OUT_OF_RESOURCES
;
1540 /* Add internal object to object list */
1541 efi_add_handle(&obj
->header
);
1543 info
->revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
1544 info
->file_path
= file_path
;
1545 info
->system_table
= &systab
;
1548 info
->device_handle
= efi_dp_find_obj(device_path
, NULL
);
1550 dp
= efi_dp_append(device_path
, file_path
);
1552 ret
= EFI_OUT_OF_RESOURCES
;
1558 ret
= efi_add_protocol(&obj
->header
,
1559 &efi_guid_loaded_image_device_path
, dp
);
1560 if (ret
!= EFI_SUCCESS
)
1564 * When asking for the loaded_image interface, just
1565 * return handle which points to loaded_image_info
1567 ret
= efi_add_protocol(&obj
->header
,
1568 &efi_guid_loaded_image
, info
);
1569 if (ret
!= EFI_SUCCESS
)
1572 #if CONFIG_IS_ENABLED(EFI_LOADER_HII)
1573 ret
= efi_add_protocol(&obj
->header
,
1574 &efi_guid_hii_string_protocol
,
1575 (void *)&efi_hii_string
);
1576 if (ret
!= EFI_SUCCESS
)
1579 ret
= efi_add_protocol(&obj
->header
,
1580 &efi_guid_hii_database_protocol
,
1581 (void *)&efi_hii_database
);
1582 if (ret
!= EFI_SUCCESS
)
1585 ret
= efi_add_protocol(&obj
->header
,
1586 &efi_guid_hii_config_routing_protocol
,
1587 (void *)&efi_hii_config_routing
);
1588 if (ret
!= EFI_SUCCESS
)
1597 printf("ERROR: Failure to install protocols for loaded image\n");
1598 efi_delete_handle(&obj
->header
);
1604 * efi_load_image_from_path() - load an image using a file path
1606 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1607 * callers obligation to update the memory type as needed.
1609 * @file_path: the path of the image to load
1610 * @buffer: buffer containing the loaded image
1611 * @size: size of the loaded image
1612 * Return: status code
1614 efi_status_t
efi_load_image_from_path(struct efi_device_path
*file_path
,
1615 void **buffer
, efi_uintn_t
*size
)
1617 struct efi_file_info
*info
= NULL
;
1618 struct efi_file_handle
*f
;
1619 static efi_status_t ret
;
1623 /* In case of failure nothing is returned */
1628 f
= efi_file_from_path(file_path
);
1630 return EFI_DEVICE_ERROR
;
1634 EFI_CALL(ret
= f
->getinfo(f
, (efi_guid_t
*)&efi_file_info_guid
,
1636 if (ret
!= EFI_BUFFER_TOO_SMALL
) {
1637 ret
= EFI_DEVICE_ERROR
;
1642 EFI_CALL(ret
= f
->getinfo(f
, (efi_guid_t
*)&efi_file_info_guid
, &bs
,
1644 if (ret
!= EFI_SUCCESS
)
1648 * When reading the file we do not yet know if it contains an
1649 * application, a boottime driver, or a runtime driver. So here we
1650 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1651 * update the reservation according to the image type.
1653 bs
= info
->file_size
;
1654 ret
= efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES
,
1655 EFI_BOOT_SERVICES_DATA
,
1656 efi_size_in_pages(bs
), &addr
);
1657 if (ret
!= EFI_SUCCESS
) {
1658 ret
= EFI_OUT_OF_RESOURCES
;
1663 EFI_CALL(ret
= f
->read(f
, &bs
, (void *)(uintptr_t)addr
));
1664 if (ret
!= EFI_SUCCESS
)
1665 efi_free_pages(addr
, efi_size_in_pages(bs
));
1666 *buffer
= (void *)(uintptr_t)addr
;
1669 EFI_CALL(f
->close(f
));
1675 * efi_load_image() - load an EFI image into memory
1676 * @boot_policy: true for request originating from the boot manager
1677 * @parent_image: the caller's image handle
1678 * @file_path: the path of the image to load
1679 * @source_buffer: memory location from which the image is installed
1680 * @source_size: size of the memory area from which the image is installed
1681 * @image_handle: handle for the newly installed image
1683 * This function implements the LoadImage service.
1685 * See the Unified Extensible Firmware Interface (UEFI) specification
1688 * Return: status code
1690 efi_status_t EFIAPI
efi_load_image(bool boot_policy
,
1691 efi_handle_t parent_image
,
1692 struct efi_device_path
*file_path
,
1693 void *source_buffer
,
1694 efi_uintn_t source_size
,
1695 efi_handle_t
*image_handle
)
1697 struct efi_device_path
*dp
, *fp
;
1698 struct efi_loaded_image
*info
= NULL
;
1699 struct efi_loaded_image_obj
**image_obj
=
1700 (struct efi_loaded_image_obj
**)image_handle
;
1704 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy
, parent_image
,
1705 file_path
, source_buffer
, source_size
, image_handle
);
1707 if (!image_handle
|| !parent_image
) {
1708 ret
= EFI_INVALID_PARAMETER
;
1712 if (!source_buffer
&& !file_path
) {
1713 ret
= EFI_NOT_FOUND
;
1717 if (!source_buffer
) {
1718 ret
= efi_load_image_from_path(file_path
, &dest_buffer
,
1720 if (ret
!= EFI_SUCCESS
)
1723 * split file_path which contains both the device and
1726 efi_dp_split_file_path(file_path
, &dp
, &fp
);
1728 /* In this case, file_path is the "device" path, i.e.
1729 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1731 dest_buffer
= source_buffer
;
1735 ret
= efi_setup_loaded_image(dp
, fp
, image_obj
, &info
);
1736 if (ret
== EFI_SUCCESS
)
1737 ret
= efi_load_pe(*image_obj
, dest_buffer
, info
);
1739 /* Release buffer to which file was loaded */
1740 efi_free_pages((uintptr_t)dest_buffer
,
1741 efi_size_in_pages(source_size
));
1742 if (ret
== EFI_SUCCESS
) {
1743 info
->system_table
= &systab
;
1744 info
->parent_handle
= parent_image
;
1746 /* The image is invalid. Release all associated resources. */
1747 efi_delete_handle(*image_handle
);
1748 *image_handle
= NULL
;
1752 return EFI_EXIT(ret
);
1756 * efi_unload_image() - unload an EFI image
1757 * @image_handle: handle of the image to be unloaded
1759 * This function implements the UnloadImage service.
1761 * See the Unified Extensible Firmware Interface (UEFI) specification for
1764 * Return: status code
1766 efi_status_t EFIAPI
efi_unload_image(efi_handle_t image_handle
)
1768 struct efi_object
*efiobj
;
1770 EFI_ENTRY("%p", image_handle
);
1771 efiobj
= efi_search_obj(image_handle
);
1773 list_del(&efiobj
->link
);
1775 return EFI_EXIT(EFI_SUCCESS
);
1779 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1781 static void efi_exit_caches(void)
1783 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1785 * Grub on 32bit ARM needs to have caches disabled before jumping into
1786 * a zImage, but does not know of all cache layers. Give it a hand.
1788 if (efi_is_direct_boot
)
1789 cleanup_before_linux();
1794 * efi_exit_boot_services() - stop all boot services
1795 * @image_handle: handle of the loaded image
1796 * @map_key: key of the memory map
1798 * This function implements the ExitBootServices service.
1800 * See the Unified Extensible Firmware Interface (UEFI) specification
1803 * All timer events are disabled. For exit boot services events the
1804 * notification function is called. The boot services are disabled in the
1807 * Return: status code
1809 static efi_status_t EFIAPI
efi_exit_boot_services(efi_handle_t image_handle
,
1810 unsigned long map_key
)
1812 struct efi_event
*evt
;
1814 EFI_ENTRY("%p, %ld", image_handle
, map_key
);
1816 /* Check that the caller has read the current memory map */
1817 if (map_key
!= efi_memory_map_key
)
1818 return EFI_INVALID_PARAMETER
;
1820 /* Make sure that notification functions are not called anymore */
1821 efi_tpl
= TPL_HIGH_LEVEL
;
1823 /* Check if ExitBootServices has already been called */
1824 if (!systab
.boottime
)
1825 return EFI_EXIT(EFI_SUCCESS
);
1827 /* Add related events to the event group */
1828 list_for_each_entry(evt
, &efi_events
, link
) {
1829 if (evt
->type
== EVT_SIGNAL_EXIT_BOOT_SERVICES
)
1830 evt
->group
= &efi_guid_event_group_exit_boot_services
;
1832 /* Notify that ExitBootServices is invoked. */
1833 list_for_each_entry(evt
, &efi_events
, link
) {
1835 !guidcmp(evt
->group
,
1836 &efi_guid_event_group_exit_boot_services
)) {
1837 efi_signal_event(evt
, false);
1842 /* TODO: Should persist EFI variables here */
1844 board_quiesce_devices();
1846 /* Fix up caches for EFI payloads if necessary */
1849 /* This stops all lingering devices */
1850 bootm_disable_interrupts();
1852 /* Disable boot time services */
1853 systab
.con_in_handle
= NULL
;
1854 systab
.con_in
= NULL
;
1855 systab
.con_out_handle
= NULL
;
1856 systab
.con_out
= NULL
;
1857 systab
.stderr_handle
= NULL
;
1858 systab
.std_err
= NULL
;
1859 systab
.boottime
= NULL
;
1861 /* Recalculate CRC32 */
1862 efi_update_table_header_crc32(&systab
.hdr
);
1864 /* Give the payload some time to boot */
1865 efi_set_watchdog(0);
1868 return EFI_EXIT(EFI_SUCCESS
);
1872 * efi_get_next_monotonic_count() - get next value of the counter
1873 * @count: returned value of the counter
1875 * This function implements the NextMonotonicCount service.
1877 * See the Unified Extensible Firmware Interface (UEFI) specification for
1880 * Return: status code
1882 static efi_status_t EFIAPI
efi_get_next_monotonic_count(uint64_t *count
)
1884 static uint64_t mono
;
1886 EFI_ENTRY("%p", count
);
1888 return EFI_EXIT(EFI_SUCCESS
);
1892 * efi_stall() - sleep
1893 * @microseconds: period to sleep in microseconds
1895 * This function implements the Stall service.
1897 * See the Unified Extensible Firmware Interface (UEFI) specification for
1900 * Return: status code
1902 static efi_status_t EFIAPI
efi_stall(unsigned long microseconds
)
1904 EFI_ENTRY("%ld", microseconds
);
1905 udelay(microseconds
);
1906 return EFI_EXIT(EFI_SUCCESS
);
1910 * efi_set_watchdog_timer() - reset the watchdog timer
1911 * @timeout: seconds before reset by watchdog
1912 * @watchdog_code: code to be logged when resetting
1913 * @data_size: size of buffer in bytes
1914 * @watchdog_data: buffer with data describing the reset reason
1916 * This function implements the SetWatchdogTimer service.
1918 * See the Unified Extensible Firmware Interface (UEFI) specification for
1921 * Return: status code
1923 static efi_status_t EFIAPI
efi_set_watchdog_timer(unsigned long timeout
,
1924 uint64_t watchdog_code
,
1925 unsigned long data_size
,
1926 uint16_t *watchdog_data
)
1928 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout
, watchdog_code
,
1929 data_size
, watchdog_data
);
1930 return EFI_EXIT(efi_set_watchdog(timeout
));
1934 * efi_close_protocol() - close a protocol
1935 * @handle: handle on which the protocol shall be closed
1936 * @protocol: GUID of the protocol to close
1937 * @agent_handle: handle of the driver
1938 * @controller_handle: handle of the controller
1940 * This function implements the CloseProtocol service.
1942 * See the Unified Extensible Firmware Interface (UEFI) specification for
1945 * Return: status code
1947 static efi_status_t EFIAPI
efi_close_protocol(efi_handle_t handle
,
1948 const efi_guid_t
*protocol
,
1949 efi_handle_t agent_handle
,
1950 efi_handle_t controller_handle
)
1952 struct efi_handler
*handler
;
1953 struct efi_open_protocol_info_item
*item
;
1954 struct efi_open_protocol_info_item
*pos
;
1957 EFI_ENTRY("%p, %pUl, %p, %p", handle
, protocol
, agent_handle
,
1960 if (!agent_handle
) {
1961 r
= EFI_INVALID_PARAMETER
;
1964 r
= efi_search_protocol(handle
, protocol
, &handler
);
1965 if (r
!= EFI_SUCCESS
)
1969 list_for_each_entry_safe(item
, pos
, &handler
->open_infos
, link
) {
1970 if (item
->info
.agent_handle
== agent_handle
&&
1971 item
->info
.controller_handle
== controller_handle
) {
1972 efi_delete_open_info(item
);
1982 * efi_open_protocol_information() - provide information about then open status
1983 * of a protocol on a handle
1984 * @handle: handle for which the information shall be retrieved
1985 * @protocol: GUID of the protocol
1986 * @entry_buffer: buffer to receive the open protocol information
1987 * @entry_count: number of entries available in the buffer
1989 * This function implements the OpenProtocolInformation service.
1991 * See the Unified Extensible Firmware Interface (UEFI) specification for
1994 * Return: status code
1996 static efi_status_t EFIAPI
efi_open_protocol_information(
1997 efi_handle_t handle
, const efi_guid_t
*protocol
,
1998 struct efi_open_protocol_info_entry
**entry_buffer
,
1999 efi_uintn_t
*entry_count
)
2001 unsigned long buffer_size
;
2002 unsigned long count
;
2003 struct efi_handler
*handler
;
2004 struct efi_open_protocol_info_item
*item
;
2007 EFI_ENTRY("%p, %pUl, %p, %p", handle
, protocol
, entry_buffer
,
2010 /* Check parameters */
2011 if (!entry_buffer
) {
2012 r
= EFI_INVALID_PARAMETER
;
2015 r
= efi_search_protocol(handle
, protocol
, &handler
);
2016 if (r
!= EFI_SUCCESS
)
2021 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2022 if (item
->info
.open_count
)
2025 *entry_count
= count
;
2026 *entry_buffer
= NULL
;
2033 buffer_size
= count
* sizeof(struct efi_open_protocol_info_entry
);
2034 r
= efi_allocate_pool(EFI_BOOT_SERVICES_DATA
, buffer_size
,
2035 (void **)entry_buffer
);
2036 if (r
!= EFI_SUCCESS
)
2038 list_for_each_entry_reverse(item
, &handler
->open_infos
, link
) {
2039 if (item
->info
.open_count
)
2040 (*entry_buffer
)[--count
] = item
->info
;
2047 * efi_protocols_per_handle() - get protocols installed on a handle
2048 * @handle: handle for which the information is retrieved
2049 * @protocol_buffer: buffer with protocol GUIDs
2050 * @protocol_buffer_count: number of entries in the buffer
2052 * This function implements the ProtocolsPerHandleService.
2054 * See the Unified Extensible Firmware Interface (UEFI) specification for
2057 * Return: status code
2059 static efi_status_t EFIAPI
efi_protocols_per_handle(
2060 efi_handle_t handle
, efi_guid_t
***protocol_buffer
,
2061 efi_uintn_t
*protocol_buffer_count
)
2063 unsigned long buffer_size
;
2064 struct efi_object
*efiobj
;
2065 struct list_head
*protocol_handle
;
2068 EFI_ENTRY("%p, %p, %p", handle
, protocol_buffer
,
2069 protocol_buffer_count
);
2071 if (!handle
|| !protocol_buffer
|| !protocol_buffer_count
)
2072 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2074 *protocol_buffer
= NULL
;
2075 *protocol_buffer_count
= 0;
2077 efiobj
= efi_search_obj(handle
);
2079 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2081 /* Count protocols */
2082 list_for_each(protocol_handle
, &efiobj
->protocols
) {
2083 ++*protocol_buffer_count
;
2087 if (*protocol_buffer_count
) {
2090 buffer_size
= sizeof(efi_guid_t
*) * *protocol_buffer_count
;
2091 r
= efi_allocate_pool(EFI_BOOT_SERVICES_DATA
, buffer_size
,
2092 (void **)protocol_buffer
);
2093 if (r
!= EFI_SUCCESS
)
2095 list_for_each(protocol_handle
, &efiobj
->protocols
) {
2096 struct efi_handler
*protocol
;
2098 protocol
= list_entry(protocol_handle
,
2099 struct efi_handler
, link
);
2100 (*protocol_buffer
)[j
] = (void *)protocol
->guid
;
2105 return EFI_EXIT(EFI_SUCCESS
);
2109 * efi_locate_handle_buffer() - locate handles implementing a protocol
2110 * @search_type: selection criterion
2111 * @protocol: GUID of the protocol
2112 * @search_key: registration key
2113 * @no_handles: number of returned handles
2114 * @buffer: buffer with the returned handles
2116 * This function implements the LocateHandleBuffer service.
2118 * See the Unified Extensible Firmware Interface (UEFI) specification for
2121 * Return: status code
2123 static efi_status_t EFIAPI
efi_locate_handle_buffer(
2124 enum efi_locate_search_type search_type
,
2125 const efi_guid_t
*protocol
, void *search_key
,
2126 efi_uintn_t
*no_handles
, efi_handle_t
**buffer
)
2129 efi_uintn_t buffer_size
= 0;
2131 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type
, protocol
, search_key
,
2132 no_handles
, buffer
);
2134 if (!no_handles
|| !buffer
) {
2135 r
= EFI_INVALID_PARAMETER
;
2140 r
= efi_locate_handle(search_type
, protocol
, search_key
, &buffer_size
,
2142 if (r
!= EFI_BUFFER_TOO_SMALL
)
2144 r
= efi_allocate_pool(EFI_BOOT_SERVICES_DATA
, buffer_size
,
2146 if (r
!= EFI_SUCCESS
)
2148 r
= efi_locate_handle(search_type
, protocol
, search_key
, &buffer_size
,
2150 if (r
== EFI_SUCCESS
)
2151 *no_handles
= buffer_size
/ sizeof(efi_handle_t
);
2157 * efi_locate_protocol() - find an interface implementing a protocol
2158 * @protocol: GUID of the protocol
2159 * @registration: registration key passed to the notification function
2160 * @protocol_interface: interface implementing the protocol
2162 * This function implements the LocateProtocol service.
2164 * See the Unified Extensible Firmware Interface (UEFI) specification for
2167 * Return: status code
2169 static efi_status_t EFIAPI
efi_locate_protocol(const efi_guid_t
*protocol
,
2171 void **protocol_interface
)
2173 struct list_head
*lhandle
;
2176 EFI_ENTRY("%pUl, %p, %p", protocol
, registration
, protocol_interface
);
2178 if (!protocol
|| !protocol_interface
)
2179 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2181 list_for_each(lhandle
, &efi_obj_list
) {
2182 struct efi_object
*efiobj
;
2183 struct efi_handler
*handler
;
2185 efiobj
= list_entry(lhandle
, struct efi_object
, link
);
2187 ret
= efi_search_protocol(efiobj
, protocol
, &handler
);
2188 if (ret
== EFI_SUCCESS
) {
2189 *protocol_interface
= handler
->protocol_interface
;
2190 return EFI_EXIT(EFI_SUCCESS
);
2193 *protocol_interface
= NULL
;
2195 return EFI_EXIT(EFI_NOT_FOUND
);
2199 * efi_locate_device_path() - Get the device path and handle of an device
2200 * implementing a protocol
2201 * @protocol: GUID of the protocol
2202 * @device_path: device path
2203 * @device: handle of the device
2205 * This function implements the LocateDevicePath service.
2207 * See the Unified Extensible Firmware Interface (UEFI) specification for
2210 * Return: status code
2212 static efi_status_t EFIAPI
efi_locate_device_path(
2213 const efi_guid_t
*protocol
,
2214 struct efi_device_path
**device_path
,
2215 efi_handle_t
*device
)
2217 struct efi_device_path
*dp
;
2219 struct efi_handler
*handler
;
2220 efi_handle_t
*handles
;
2222 size_t len_best
= 0;
2223 efi_uintn_t no_handles
;
2227 EFI_ENTRY("%pUl, %p, %p", protocol
, device_path
, device
);
2229 if (!protocol
|| !device_path
|| !*device_path
|| !device
) {
2230 ret
= EFI_INVALID_PARAMETER
;
2234 /* Find end of device path */
2235 len
= efi_dp_instance_size(*device_path
);
2237 /* Get all handles implementing the protocol */
2238 ret
= EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL
, protocol
, NULL
,
2239 &no_handles
, &handles
));
2240 if (ret
!= EFI_SUCCESS
)
2243 for (i
= 0; i
< no_handles
; ++i
) {
2244 /* Find the device path protocol */
2245 ret
= efi_search_protocol(handles
[i
], &efi_guid_device_path
,
2247 if (ret
!= EFI_SUCCESS
)
2249 dp
= (struct efi_device_path
*)handler
->protocol_interface
;
2250 len_dp
= efi_dp_instance_size(dp
);
2252 * This handle can only be a better fit
2253 * if its device path length is longer than the best fit and
2254 * if its device path length is shorter of equal the searched
2257 if (len_dp
<= len_best
|| len_dp
> len
)
2259 /* Check if dp is a subpath of device_path */
2260 if (memcmp(*device_path
, dp
, len_dp
))
2262 *device
= handles
[i
];
2266 remainder
= (u8
*)*device_path
+ len_best
;
2267 *device_path
= (struct efi_device_path
*)remainder
;
2270 ret
= EFI_NOT_FOUND
;
2273 return EFI_EXIT(ret
);
2277 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2279 * @handle: handle on which the protocol interfaces shall be installed
2280 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2283 * This function implements the MultipleProtocolInterfaces service.
2285 * See the Unified Extensible Firmware Interface (UEFI) specification for
2288 * Return: status code
2290 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2291 (efi_handle_t
*handle
, ...)
2293 EFI_ENTRY("%p", handle
);
2296 const efi_guid_t
*protocol
;
2297 void *protocol_interface
;
2298 efi_status_t r
= EFI_SUCCESS
;
2302 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2304 efi_va_start(argptr
, handle
);
2306 protocol
= efi_va_arg(argptr
, efi_guid_t
*);
2309 protocol_interface
= efi_va_arg(argptr
, void*);
2310 r
= EFI_CALL(efi_install_protocol_interface(
2312 EFI_NATIVE_INTERFACE
,
2313 protocol_interface
));
2314 if (r
!= EFI_SUCCESS
)
2319 if (r
== EFI_SUCCESS
)
2322 /* If an error occurred undo all changes. */
2323 efi_va_start(argptr
, handle
);
2325 protocol
= efi_va_arg(argptr
, efi_guid_t
*);
2326 protocol_interface
= efi_va_arg(argptr
, void*);
2327 EFI_CALL(efi_uninstall_protocol_interface(*handle
, protocol
,
2328 protocol_interface
));
2336 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2338 * @handle: handle from which the protocol interfaces shall be removed
2339 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2342 * This function implements the UninstallMultipleProtocolInterfaces service.
2344 * See the Unified Extensible Firmware Interface (UEFI) specification for
2347 * Return: status code
2349 static efi_status_t EFIAPI
efi_uninstall_multiple_protocol_interfaces(
2350 efi_handle_t handle
, ...)
2352 EFI_ENTRY("%p", handle
);
2355 const efi_guid_t
*protocol
;
2356 void *protocol_interface
;
2357 efi_status_t r
= EFI_SUCCESS
;
2361 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2363 efi_va_start(argptr
, handle
);
2365 protocol
= efi_va_arg(argptr
, efi_guid_t
*);
2368 protocol_interface
= efi_va_arg(argptr
, void*);
2369 r
= efi_uninstall_protocol(handle
, protocol
,
2370 protocol_interface
);
2371 if (r
!= EFI_SUCCESS
)
2376 if (r
== EFI_SUCCESS
) {
2377 /* If the last protocol has been removed, delete the handle. */
2378 if (list_empty(&handle
->protocols
)) {
2379 list_del(&handle
->link
);
2385 /* If an error occurred undo all changes. */
2386 efi_va_start(argptr
, handle
);
2388 protocol
= efi_va_arg(argptr
, efi_guid_t
*);
2389 protocol_interface
= efi_va_arg(argptr
, void*);
2390 EFI_CALL(efi_install_protocol_interface(&handle
, protocol
,
2391 EFI_NATIVE_INTERFACE
,
2392 protocol_interface
));
2396 /* In case of an error always return EFI_INVALID_PARAMETER */
2397 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2401 * efi_calculate_crc32() - calculate cyclic redundancy code
2402 * @data: buffer with data
2403 * @data_size: size of buffer in bytes
2404 * @crc32_p: cyclic redundancy code
2406 * This function implements the CalculateCrc32 service.
2408 * See the Unified Extensible Firmware Interface (UEFI) specification for
2411 * Return: status code
2413 static efi_status_t EFIAPI
efi_calculate_crc32(const void *data
,
2414 efi_uintn_t data_size
,
2417 EFI_ENTRY("%p, %zu", data
, data_size
);
2418 *crc32_p
= crc32(0, data
, data_size
);
2419 return EFI_EXIT(EFI_SUCCESS
);
2423 * efi_copy_mem() - copy memory
2424 * @destination: destination of the copy operation
2425 * @source: source of the copy operation
2426 * @length: number of bytes to copy
2428 * This function implements the CopyMem service.
2430 * See the Unified Extensible Firmware Interface (UEFI) specification for
2433 static void EFIAPI
efi_copy_mem(void *destination
, const void *source
,
2436 EFI_ENTRY("%p, %p, %ld", destination
, source
, (unsigned long)length
);
2437 memmove(destination
, source
, length
);
2438 EFI_EXIT(EFI_SUCCESS
);
2442 * efi_set_mem() - Fill memory with a byte value.
2443 * @buffer: buffer to fill
2444 * @size: size of buffer in bytes
2445 * @value: byte to copy to the buffer
2447 * This function implements the SetMem service.
2449 * See the Unified Extensible Firmware Interface (UEFI) specification for
2452 static void EFIAPI
efi_set_mem(void *buffer
, size_t size
, uint8_t value
)
2454 EFI_ENTRY("%p, %ld, 0x%x", buffer
, (unsigned long)size
, value
);
2455 memset(buffer
, value
, size
);
2456 EFI_EXIT(EFI_SUCCESS
);
2460 * efi_protocol_open() - open protocol interface on a handle
2461 * @handler: handler of a protocol
2462 * @protocol_interface: interface implementing the protocol
2463 * @agent_handle: handle of the driver
2464 * @controller_handle: handle of the controller
2465 * @attributes: attributes indicating how to open the protocol
2467 * Return: status code
2469 static efi_status_t
efi_protocol_open(
2470 struct efi_handler
*handler
,
2471 void **protocol_interface
, void *agent_handle
,
2472 void *controller_handle
, uint32_t attributes
)
2474 struct efi_open_protocol_info_item
*item
;
2475 struct efi_open_protocol_info_entry
*match
= NULL
;
2476 bool opened_by_driver
= false;
2477 bool opened_exclusive
= false;
2479 /* If there is no agent, only return the interface */
2483 /* For TEST_PROTOCOL ignore interface attribute */
2484 if (attributes
!= EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)
2485 *protocol_interface
= NULL
;
2488 * Check if the protocol is already opened by a driver with the same
2489 * attributes or opened exclusively
2491 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2492 if (item
->info
.agent_handle
== agent_handle
) {
2493 if ((attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) &&
2494 (item
->info
.attributes
== attributes
))
2495 return EFI_ALREADY_STARTED
;
2497 if (item
->info
.attributes
& EFI_OPEN_PROTOCOL_EXCLUSIVE
)
2498 opened_exclusive
= true;
2501 /* Only one controller can open the protocol exclusively */
2502 if (opened_exclusive
&& attributes
&
2503 (EFI_OPEN_PROTOCOL_EXCLUSIVE
| EFI_OPEN_PROTOCOL_BY_DRIVER
))
2504 return EFI_ACCESS_DENIED
;
2506 /* Prepare exclusive opening */
2507 if (attributes
& EFI_OPEN_PROTOCOL_EXCLUSIVE
) {
2508 /* Try to disconnect controllers */
2509 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2510 if (item
->info
.attributes
==
2511 EFI_OPEN_PROTOCOL_BY_DRIVER
)
2512 EFI_CALL(efi_disconnect_controller(
2513 item
->info
.controller_handle
,
2514 item
->info
.agent_handle
,
2517 opened_by_driver
= false;
2518 /* Check if all controllers are disconnected */
2519 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2520 if (item
->info
.attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
)
2521 opened_by_driver
= true;
2523 /* Only one controller can be connected */
2524 if (opened_by_driver
)
2525 return EFI_ACCESS_DENIED
;
2528 /* Find existing entry */
2529 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2530 if (item
->info
.agent_handle
== agent_handle
&&
2531 item
->info
.controller_handle
== controller_handle
)
2532 match
= &item
->info
;
2534 /* None found, create one */
2536 match
= efi_create_open_info(handler
);
2538 return EFI_OUT_OF_RESOURCES
;
2541 match
->agent_handle
= agent_handle
;
2542 match
->controller_handle
= controller_handle
;
2543 match
->attributes
= attributes
;
2544 match
->open_count
++;
2547 /* For TEST_PROTOCOL ignore interface attribute. */
2548 if (attributes
!= EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)
2549 *protocol_interface
= handler
->protocol_interface
;
2555 * efi_open_protocol() - open protocol interface on a handle
2556 * @handle: handle on which the protocol shall be opened
2557 * @protocol: GUID of the protocol
2558 * @protocol_interface: interface implementing the protocol
2559 * @agent_handle: handle of the driver
2560 * @controller_handle: handle of the controller
2561 * @attributes: attributes indicating how to open the protocol
2563 * This function implements the OpenProtocol interface.
2565 * See the Unified Extensible Firmware Interface (UEFI) specification for
2568 * Return: status code
2570 static efi_status_t EFIAPI efi_open_protocol
2571 (efi_handle_t handle
, const efi_guid_t
*protocol
,
2572 void **protocol_interface
, efi_handle_t agent_handle
,
2573 efi_handle_t controller_handle
, uint32_t attributes
)
2575 struct efi_handler
*handler
;
2576 efi_status_t r
= EFI_INVALID_PARAMETER
;
2578 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle
, protocol
,
2579 protocol_interface
, agent_handle
, controller_handle
,
2582 if (!handle
|| !protocol
||
2583 (!protocol_interface
&& attributes
!=
2584 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)) {
2588 switch (attributes
) {
2589 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
:
2590 case EFI_OPEN_PROTOCOL_GET_PROTOCOL
:
2591 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL
:
2593 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
:
2594 if (controller_handle
== handle
)
2597 case EFI_OPEN_PROTOCOL_BY_DRIVER
:
2598 case EFI_OPEN_PROTOCOL_BY_DRIVER
| EFI_OPEN_PROTOCOL_EXCLUSIVE
:
2599 /* Check that the controller handle is valid */
2600 if (!efi_search_obj(controller_handle
))
2603 case EFI_OPEN_PROTOCOL_EXCLUSIVE
:
2604 /* Check that the agent handle is valid */
2605 if (!efi_search_obj(agent_handle
))
2612 r
= efi_search_protocol(handle
, protocol
, &handler
);
2613 if (r
!= EFI_SUCCESS
)
2616 r
= efi_protocol_open(handler
, protocol_interface
, agent_handle
,
2617 controller_handle
, attributes
);
2623 * efi_start_image() - call the entry point of an image
2624 * @image_handle: handle of the image
2625 * @exit_data_size: size of the buffer
2626 * @exit_data: buffer to receive the exit data of the called image
2628 * This function implements the StartImage service.
2630 * See the Unified Extensible Firmware Interface (UEFI) specification for
2633 * Return: status code
2635 efi_status_t EFIAPI
efi_start_image(efi_handle_t image_handle
,
2636 efi_uintn_t
*exit_data_size
,
2639 struct efi_loaded_image_obj
*image_obj
=
2640 (struct efi_loaded_image_obj
*)image_handle
;
2643 efi_handle_t parent_image
= current_image
;
2645 EFI_ENTRY("%p, %p, %p", image_handle
, exit_data_size
, exit_data
);
2647 /* Check parameters */
2648 ret
= EFI_CALL(efi_open_protocol(image_handle
, &efi_guid_loaded_image
,
2650 EFI_OPEN_PROTOCOL_GET_PROTOCOL
));
2651 if (ret
!= EFI_SUCCESS
)
2652 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2654 efi_is_direct_boot
= false;
2656 /* call the image! */
2657 if (setjmp(&image_obj
->exit_jmp
)) {
2659 * We called the entry point of the child image with EFI_CALL
2660 * in the lines below. The child image called the Exit() boot
2661 * service efi_exit() which executed the long jump that brought
2662 * us to the current line. This implies that the second half
2663 * of the EFI_CALL macro has not been executed.
2667 * efi_exit() called efi_restore_gd(). We have to undo this
2668 * otherwise __efi_entry_check() will put the wrong value into
2674 * To get ready to call EFI_EXIT below we have to execute the
2675 * missed out steps of EFI_CALL.
2677 assert(__efi_entry_check());
2678 debug("%sEFI: %lu returned by started image\n",
2679 __efi_nesting_dec(),
2680 (unsigned long)((uintptr_t)image_obj
->exit_status
&
2682 current_image
= parent_image
;
2683 return EFI_EXIT(image_obj
->exit_status
);
2686 current_image
= image_handle
;
2687 ret
= EFI_CALL(image_obj
->entry(image_handle
, &systab
));
2690 * Usually UEFI applications call Exit() instead of returning.
2691 * But because the world doesn't consist of ponies and unicorns,
2692 * we're happy to emulate that behavior on behalf of a payload
2695 return EFI_CALL(systab
.boottime
->exit(image_handle
, ret
, 0, NULL
));
2699 * efi_exit() - leave an EFI application or driver
2700 * @image_handle: handle of the application or driver that is exiting
2701 * @exit_status: status code
2702 * @exit_data_size: size of the buffer in bytes
2703 * @exit_data: buffer with data describing an error
2705 * This function implements the Exit service.
2707 * See the Unified Extensible Firmware Interface (UEFI) specification for
2710 * Return: status code
2712 static efi_status_t EFIAPI
efi_exit(efi_handle_t image_handle
,
2713 efi_status_t exit_status
,
2714 efi_uintn_t exit_data_size
,
2718 * TODO: We should call the unload procedure of the loaded
2723 struct efi_loaded_image_obj
*image_obj
=
2724 (struct efi_loaded_image_obj
*)image_handle
;
2726 EFI_ENTRY("%p, %ld, %zu, %p", image_handle
, exit_status
,
2727 exit_data_size
, exit_data
);
2729 /* Check parameters */
2730 if (image_handle
!= current_image
)
2732 ret
= EFI_CALL(efi_open_protocol(image_handle
, &efi_guid_loaded_image
,
2734 EFI_OPEN_PROTOCOL_GET_PROTOCOL
));
2735 if (ret
!= EFI_SUCCESS
)
2738 /* Make sure entry/exit counts for EFI world cross-overs match */
2739 EFI_EXIT(exit_status
);
2742 * But longjmp out with the U-Boot gd, not the application's, as
2743 * the other end is a setjmp call inside EFI context.
2747 image_obj
->exit_status
= exit_status
;
2748 longjmp(&image_obj
->exit_jmp
, 1);
2750 panic("EFI application exited");
2752 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2756 * efi_handle_protocol() - get interface of a protocol on a handle
2757 * @handle: handle on which the protocol shall be opened
2758 * @protocol: GUID of the protocol
2759 * @protocol_interface: interface implementing the protocol
2761 * This function implements the HandleProtocol service.
2763 * See the Unified Extensible Firmware Interface (UEFI) specification for
2766 * Return: status code
2768 static efi_status_t EFIAPI
efi_handle_protocol(efi_handle_t handle
,
2769 const efi_guid_t
*protocol
,
2770 void **protocol_interface
)
2772 return efi_open_protocol(handle
, protocol
, protocol_interface
, NULL
,
2773 NULL
, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
);
2777 * efi_bind_controller() - bind a single driver to a controller
2778 * @controller_handle: controller handle
2779 * @driver_image_handle: driver handle
2780 * @remain_device_path: remaining path
2782 * Return: status code
2784 static efi_status_t
efi_bind_controller(
2785 efi_handle_t controller_handle
,
2786 efi_handle_t driver_image_handle
,
2787 struct efi_device_path
*remain_device_path
)
2789 struct efi_driver_binding_protocol
*binding_protocol
;
2792 r
= EFI_CALL(efi_open_protocol(driver_image_handle
,
2793 &efi_guid_driver_binding_protocol
,
2794 (void **)&binding_protocol
,
2795 driver_image_handle
, NULL
,
2796 EFI_OPEN_PROTOCOL_GET_PROTOCOL
));
2797 if (r
!= EFI_SUCCESS
)
2799 r
= EFI_CALL(binding_protocol
->supported(binding_protocol
,
2801 remain_device_path
));
2802 if (r
== EFI_SUCCESS
)
2803 r
= EFI_CALL(binding_protocol
->start(binding_protocol
,
2805 remain_device_path
));
2806 EFI_CALL(efi_close_protocol(driver_image_handle
,
2807 &efi_guid_driver_binding_protocol
,
2808 driver_image_handle
, NULL
));
2813 * efi_connect_single_controller() - connect a single driver to a controller
2814 * @controller_handle: controller
2815 * @driver_image_handle: driver
2816 * @remain_device_path: remaining path
2818 * Return: status code
2820 static efi_status_t
efi_connect_single_controller(
2821 efi_handle_t controller_handle
,
2822 efi_handle_t
*driver_image_handle
,
2823 struct efi_device_path
*remain_device_path
)
2825 efi_handle_t
*buffer
;
2829 size_t connected
= 0;
2831 /* Get buffer with all handles with driver binding protocol */
2832 r
= EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL
,
2833 &efi_guid_driver_binding_protocol
,
2834 NULL
, &count
, &buffer
));
2835 if (r
!= EFI_SUCCESS
)
2838 /* Context Override */
2839 if (driver_image_handle
) {
2840 for (; *driver_image_handle
; ++driver_image_handle
) {
2841 for (i
= 0; i
< count
; ++i
) {
2842 if (buffer
[i
] == *driver_image_handle
) {
2844 r
= efi_bind_controller(
2846 *driver_image_handle
,
2847 remain_device_path
);
2849 * For drivers that do not support the
2850 * controller or are already connected
2851 * we receive an error code here.
2853 if (r
== EFI_SUCCESS
)
2861 * TODO: Some overrides are not yet implemented:
2862 * - Platform Driver Override
2863 * - Driver Family Override Search
2864 * - Bus Specific Driver Override
2867 /* Driver Binding Search */
2868 for (i
= 0; i
< count
; ++i
) {
2870 r
= efi_bind_controller(controller_handle
,
2872 remain_device_path
);
2873 if (r
== EFI_SUCCESS
)
2878 efi_free_pool(buffer
);
2880 return EFI_NOT_FOUND
;
2885 * efi_connect_controller() - connect a controller to a driver
2886 * @controller_handle: handle of the controller
2887 * @driver_image_handle: handle of the driver
2888 * @remain_device_path: device path of a child controller
2889 * @recursive: true to connect all child controllers
2891 * This function implements the ConnectController service.
2893 * See the Unified Extensible Firmware Interface (UEFI) specification for
2896 * First all driver binding protocol handles are tried for binding drivers.
2897 * Afterwards all handles that have opened a protocol of the controller
2898 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2900 * Return: status code
2902 static efi_status_t EFIAPI
efi_connect_controller(
2903 efi_handle_t controller_handle
,
2904 efi_handle_t
*driver_image_handle
,
2905 struct efi_device_path
*remain_device_path
,
2909 efi_status_t ret
= EFI_NOT_FOUND
;
2910 struct efi_object
*efiobj
;
2912 EFI_ENTRY("%p, %p, %pD, %d", controller_handle
, driver_image_handle
,
2913 remain_device_path
, recursive
);
2915 efiobj
= efi_search_obj(controller_handle
);
2917 ret
= EFI_INVALID_PARAMETER
;
2921 r
= efi_connect_single_controller(controller_handle
,
2922 driver_image_handle
,
2923 remain_device_path
);
2924 if (r
== EFI_SUCCESS
)
2927 struct efi_handler
*handler
;
2928 struct efi_open_protocol_info_item
*item
;
2930 list_for_each_entry(handler
, &efiobj
->protocols
, link
) {
2931 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2932 if (item
->info
.attributes
&
2933 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) {
2934 r
= EFI_CALL(efi_connect_controller(
2935 item
->info
.controller_handle
,
2936 driver_image_handle
,
2939 if (r
== EFI_SUCCESS
)
2945 /* Check for child controller specified by end node */
2946 if (ret
!= EFI_SUCCESS
&& remain_device_path
&&
2947 remain_device_path
->type
== DEVICE_PATH_TYPE_END
)
2950 return EFI_EXIT(ret
);
2954 * efi_reinstall_protocol_interface() - reinstall protocol interface
2955 * @handle: handle on which the protocol shall be reinstalled
2956 * @protocol: GUID of the protocol to be installed
2957 * @old_interface: interface to be removed
2958 * @new_interface: interface to be installed
2960 * This function implements the ReinstallProtocolInterface service.
2962 * See the Unified Extensible Firmware Interface (UEFI) specification for
2965 * The old interface is uninstalled. The new interface is installed.
2966 * Drivers are connected.
2968 * Return: status code
2970 static efi_status_t EFIAPI
efi_reinstall_protocol_interface(
2971 efi_handle_t handle
, const efi_guid_t
*protocol
,
2972 void *old_interface
, void *new_interface
)
2976 EFI_ENTRY("%p, %pUl, %p, %p", handle
, protocol
, old_interface
,
2979 /* Uninstall protocol but do not delete handle */
2980 ret
= efi_uninstall_protocol(handle
, protocol
, old_interface
);
2981 if (ret
!= EFI_SUCCESS
)
2984 /* Install the new protocol */
2985 ret
= efi_add_protocol(handle
, protocol
, new_interface
);
2987 * The UEFI spec does not specify what should happen to the handle
2988 * if in case of an error no protocol interface remains on the handle.
2989 * So let's do nothing here.
2991 if (ret
!= EFI_SUCCESS
)
2994 * The returned status code has to be ignored.
2995 * Do not create an error if no suitable driver for the handle exists.
2997 EFI_CALL(efi_connect_controller(handle
, NULL
, NULL
, true));
2999 return EFI_EXIT(ret
);
3003 * efi_get_child_controllers() - get all child controllers associated to a driver
3004 * @efiobj: handle of the controller
3005 * @driver_handle: handle of the driver
3006 * @number_of_children: number of child controllers
3007 * @child_handle_buffer: handles of the the child controllers
3009 * The allocated buffer has to be freed with free().
3011 * Return: status code
3013 static efi_status_t
efi_get_child_controllers(
3014 struct efi_object
*efiobj
,
3015 efi_handle_t driver_handle
,
3016 efi_uintn_t
*number_of_children
,
3017 efi_handle_t
**child_handle_buffer
)
3019 struct efi_handler
*handler
;
3020 struct efi_open_protocol_info_item
*item
;
3021 efi_uintn_t count
= 0, i
;
3024 /* Count all child controller associations */
3025 list_for_each_entry(handler
, &efiobj
->protocols
, link
) {
3026 list_for_each_entry(item
, &handler
->open_infos
, link
) {
3027 if (item
->info
.agent_handle
== driver_handle
&&
3028 item
->info
.attributes
&
3029 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
)
3034 * Create buffer. In case of duplicate child controller assignments
3035 * the buffer will be too large. But that does not harm.
3037 *number_of_children
= 0;
3038 *child_handle_buffer
= calloc(count
, sizeof(efi_handle_t
));
3039 if (!*child_handle_buffer
)
3040 return EFI_OUT_OF_RESOURCES
;
3041 /* Copy unique child handles */
3042 list_for_each_entry(handler
, &efiobj
->protocols
, link
) {
3043 list_for_each_entry(item
, &handler
->open_infos
, link
) {
3044 if (item
->info
.agent_handle
== driver_handle
&&
3045 item
->info
.attributes
&
3046 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) {
3047 /* Check this is a new child controller */
3049 for (i
= 0; i
< *number_of_children
; ++i
) {
3050 if ((*child_handle_buffer
)[i
] ==
3051 item
->info
.controller_handle
)
3054 /* Copy handle to buffer */
3056 i
= (*number_of_children
)++;
3057 (*child_handle_buffer
)[i
] =
3058 item
->info
.controller_handle
;
3067 * efi_disconnect_controller() - disconnect a controller from a driver
3068 * @controller_handle: handle of the controller
3069 * @driver_image_handle: handle of the driver
3070 * @child_handle: handle of the child to destroy
3072 * This function implements the DisconnectController service.
3074 * See the Unified Extensible Firmware Interface (UEFI) specification for
3077 * Return: status code
3079 static efi_status_t EFIAPI
efi_disconnect_controller(
3080 efi_handle_t controller_handle
,
3081 efi_handle_t driver_image_handle
,
3082 efi_handle_t child_handle
)
3084 struct efi_driver_binding_protocol
*binding_protocol
;
3085 efi_handle_t
*child_handle_buffer
= NULL
;
3086 size_t number_of_children
= 0;
3088 size_t stop_count
= 0;
3089 struct efi_object
*efiobj
;
3091 EFI_ENTRY("%p, %p, %p", controller_handle
, driver_image_handle
,
3094 efiobj
= efi_search_obj(controller_handle
);
3096 r
= EFI_INVALID_PARAMETER
;
3100 if (child_handle
&& !efi_search_obj(child_handle
)) {
3101 r
= EFI_INVALID_PARAMETER
;
3105 /* If no driver handle is supplied, disconnect all drivers */
3106 if (!driver_image_handle
) {
3107 r
= efi_disconnect_all_drivers(efiobj
, NULL
, child_handle
);
3111 /* Create list of child handles */
3113 number_of_children
= 1;
3114 child_handle_buffer
= &child_handle
;
3116 efi_get_child_controllers(efiobj
,
3117 driver_image_handle
,
3118 &number_of_children
,
3119 &child_handle_buffer
);
3122 /* Get the driver binding protocol */
3123 r
= EFI_CALL(efi_open_protocol(driver_image_handle
,
3124 &efi_guid_driver_binding_protocol
,
3125 (void **)&binding_protocol
,
3126 driver_image_handle
, NULL
,
3127 EFI_OPEN_PROTOCOL_GET_PROTOCOL
));
3128 if (r
!= EFI_SUCCESS
)
3130 /* Remove the children */
3131 if (number_of_children
) {
3132 r
= EFI_CALL(binding_protocol
->stop(binding_protocol
,
3135 child_handle_buffer
));
3136 if (r
== EFI_SUCCESS
)
3139 /* Remove the driver */
3141 r
= EFI_CALL(binding_protocol
->stop(binding_protocol
,
3144 if (r
== EFI_SUCCESS
)
3146 EFI_CALL(efi_close_protocol(driver_image_handle
,
3147 &efi_guid_driver_binding_protocol
,
3148 driver_image_handle
, NULL
));
3156 free(child_handle_buffer
);
3160 static struct efi_boot_services efi_boot_services
= {
3162 .signature
= EFI_BOOT_SERVICES_SIGNATURE
,
3163 .revision
= EFI_SPECIFICATION_VERSION
,
3164 .headersize
= sizeof(struct efi_boot_services
),
3166 .raise_tpl
= efi_raise_tpl
,
3167 .restore_tpl
= efi_restore_tpl
,
3168 .allocate_pages
= efi_allocate_pages_ext
,
3169 .free_pages
= efi_free_pages_ext
,
3170 .get_memory_map
= efi_get_memory_map_ext
,
3171 .allocate_pool
= efi_allocate_pool_ext
,
3172 .free_pool
= efi_free_pool_ext
,
3173 .create_event
= efi_create_event_ext
,
3174 .set_timer
= efi_set_timer_ext
,
3175 .wait_for_event
= efi_wait_for_event
,
3176 .signal_event
= efi_signal_event_ext
,
3177 .close_event
= efi_close_event
,
3178 .check_event
= efi_check_event
,
3179 .install_protocol_interface
= efi_install_protocol_interface
,
3180 .reinstall_protocol_interface
= efi_reinstall_protocol_interface
,
3181 .uninstall_protocol_interface
= efi_uninstall_protocol_interface
,
3182 .handle_protocol
= efi_handle_protocol
,
3184 .register_protocol_notify
= efi_register_protocol_notify
,
3185 .locate_handle
= efi_locate_handle_ext
,
3186 .locate_device_path
= efi_locate_device_path
,
3187 .install_configuration_table
= efi_install_configuration_table_ext
,
3188 .load_image
= efi_load_image
,
3189 .start_image
= efi_start_image
,
3191 .unload_image
= efi_unload_image
,
3192 .exit_boot_services
= efi_exit_boot_services
,
3193 .get_next_monotonic_count
= efi_get_next_monotonic_count
,
3195 .set_watchdog_timer
= efi_set_watchdog_timer
,
3196 .connect_controller
= efi_connect_controller
,
3197 .disconnect_controller
= efi_disconnect_controller
,
3198 .open_protocol
= efi_open_protocol
,
3199 .close_protocol
= efi_close_protocol
,
3200 .open_protocol_information
= efi_open_protocol_information
,
3201 .protocols_per_handle
= efi_protocols_per_handle
,
3202 .locate_handle_buffer
= efi_locate_handle_buffer
,
3203 .locate_protocol
= efi_locate_protocol
,
3204 .install_multiple_protocol_interfaces
=
3205 efi_install_multiple_protocol_interfaces
,
3206 .uninstall_multiple_protocol_interfaces
=
3207 efi_uninstall_multiple_protocol_interfaces
,
3208 .calculate_crc32
= efi_calculate_crc32
,
3209 .copy_mem
= efi_copy_mem
,
3210 .set_mem
= efi_set_mem
,
3211 .create_event_ex
= efi_create_event_ex
,
3214 static u16 __efi_runtime_data firmware_vendor
[] = L
"Das U-Boot";
3216 struct efi_system_table __efi_runtime_data systab
= {
3218 .signature
= EFI_SYSTEM_TABLE_SIGNATURE
,
3219 .revision
= EFI_SPECIFICATION_VERSION
,
3220 .headersize
= sizeof(struct efi_system_table
),
3222 .fw_vendor
= firmware_vendor
,
3223 .fw_revision
= FW_VERSION
<< 16 | FW_PATCHLEVEL
<< 8,
3224 .con_in
= (void *)&efi_con_in
,
3225 .con_out
= (void *)&efi_con_out
,
3226 .std_err
= (void *)&efi_con_out
,
3227 .runtime
= (void *)&efi_runtime_services
,
3228 .boottime
= (void *)&efi_boot_services
,
3234 * efi_initialize_system_table() - Initialize system table
3236 * Return: status code
3238 efi_status_t
efi_initialize_system_table(void)
3242 /* Allocate configuration table array */
3243 ret
= efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA
,
3244 EFI_MAX_CONFIGURATION_TABLES
*
3245 sizeof(struct efi_configuration_table
),
3246 (void **)&systab
.tables
);
3248 /* Set CRC32 field in table headers */
3249 efi_update_table_header_crc32(&systab
.hdr
);
3250 efi_update_table_header_crc32(&efi_runtime_services
.hdr
);
3251 efi_update_table_header_crc32(&efi_boot_services
.hdr
);