1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application boot time services
5 * Copyright (c) 2016 Alexander Graf
10 #include <efi_loader.h>
12 #include <linux/libfdt_env.h>
13 #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 __efi_runtime_data
LIST_HEAD(efi_events
);
29 /* List of queued events */
30 LIST_HEAD(efi_event_queue
);
32 /* Flag to disable timer activity in ExitBootServices() */
33 static bool timers_enabled
= true;
35 /* List of all events registered by RegisterProtocolNotify() */
36 LIST_HEAD(efi_register_notify_events
);
38 /* Handle of the currently executing image */
39 static efi_handle_t current_image
;
43 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
44 * fixed when compiling U-Boot. However, the payload does not know about that
45 * restriction so we need to manually swap its and our view of that register on
46 * EFI callback entry/exit.
48 static volatile void *efi_gd
, *app_gd
;
51 /* 1 if inside U-Boot code, 0 if inside EFI payload code */
52 static int entry_count
= 1;
53 static int nesting_level
;
54 /* GUID of the device tree table */
55 const efi_guid_t efi_guid_fdt
= EFI_FDT_GUID
;
56 /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
57 const efi_guid_t efi_guid_driver_binding_protocol
=
58 EFI_DRIVER_BINDING_PROTOCOL_GUID
;
60 /* event group ExitBootServices() invoked */
61 const efi_guid_t efi_guid_event_group_exit_boot_services
=
62 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES
;
63 /* event group SetVirtualAddressMap() invoked */
64 const efi_guid_t efi_guid_event_group_virtual_address_change
=
65 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE
;
66 /* event group memory map changed */
67 const efi_guid_t efi_guid_event_group_memory_map_change
=
68 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE
;
69 /* event group boot manager about to boot */
70 const efi_guid_t efi_guid_event_group_ready_to_boot
=
71 EFI_EVENT_GROUP_READY_TO_BOOT
;
72 /* event group ResetSystem() invoked (before ExitBootServices) */
73 const efi_guid_t efi_guid_event_group_reset_system
=
74 EFI_EVENT_GROUP_RESET_SYSTEM
;
76 static efi_status_t EFIAPI
efi_disconnect_controller(
77 efi_handle_t controller_handle
,
78 efi_handle_t driver_image_handle
,
79 efi_handle_t child_handle
);
81 /* Called on every callback entry */
82 int __efi_entry_check(void)
84 int ret
= entry_count
++ == 0;
93 /* Called on every callback exit */
94 int __efi_exit_check(void)
96 int ret
= --entry_count
== 0;
103 /* Called from do_bootefi_exec() */
104 void efi_save_gd(void)
112 * Special case handler for error/abort that just forces things back to u-boot
113 * world so we can dump out an abort message, without any care about returning
114 * back to UEFI world.
116 void efi_restore_gd(void)
119 /* Only restore if we're already in EFI context */
127 * indent_string() - returns a string for indenting with two spaces per level
128 * @level: indent level
130 * A maximum of ten indent levels is supported. Higher indent levels will be
133 * Return: A string for indenting with two spaces per level is
136 static const char *indent_string(int level
)
138 const char *indent
= " ";
139 const int max
= strlen(indent
);
141 level
= min(max
, level
* 2);
142 return &indent
[max
- level
];
145 const char *__efi_nesting(void)
147 return indent_string(nesting_level
);
150 const char *__efi_nesting_inc(void)
152 return indent_string(nesting_level
++);
155 const char *__efi_nesting_dec(void)
157 return indent_string(--nesting_level
);
161 * efi_event_is_queued() - check if an event is queued
164 * Return: true if event is queued
166 static bool efi_event_is_queued(struct efi_event
*event
)
168 return !!event
->queue_link
.next
;
172 * efi_process_event_queue() - process event queue
174 static void efi_process_event_queue(void)
176 while (!list_empty(&efi_event_queue
)) {
177 struct efi_event
*event
;
180 event
= list_first_entry(&efi_event_queue
, struct efi_event
,
182 if (efi_tpl
>= event
->notify_tpl
)
184 list_del(&event
->queue_link
);
185 event
->queue_link
.next
= NULL
;
186 event
->queue_link
.prev
= NULL
;
187 /* Events must be executed at the event's TPL */
189 efi_tpl
= event
->notify_tpl
;
190 EFI_CALL_VOID(event
->notify_function(event
,
191 event
->notify_context
));
193 if (event
->type
== EVT_NOTIFY_SIGNAL
)
194 event
->is_signaled
= 0;
199 * efi_queue_event() - queue an EFI event
200 * @event: event to signal
202 * This function queues the notification function of the event for future
206 static void efi_queue_event(struct efi_event
*event
)
208 struct efi_event
*item
= NULL
;
210 if (!event
->notify_function
)
213 if (!efi_event_is_queued(event
)) {
215 * Events must be notified in order of decreasing task priority
216 * level. Insert the new event accordingly.
218 list_for_each_entry(item
, &efi_event_queue
, queue_link
) {
219 if (item
->notify_tpl
< event
->notify_tpl
) {
220 list_add_tail(&event
->queue_link
,
227 list_add_tail(&event
->queue_link
, &efi_event_queue
);
229 efi_process_event_queue();
233 * is_valid_tpl() - check if the task priority level is valid
235 * @tpl: TPL level to check
236 * Return: status code
238 efi_status_t
is_valid_tpl(efi_uintn_t tpl
)
241 case TPL_APPLICATION
:
247 return EFI_INVALID_PARAMETER
;
252 * efi_signal_event() - signal an EFI event
253 * @event: event to signal
255 * This function signals an event. If the event belongs to an event group all
256 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
257 * their notification function is queued.
259 * For the SignalEvent service see efi_signal_event_ext.
261 void efi_signal_event(struct efi_event
*event
)
263 if (event
->is_signaled
)
266 struct efi_event
*evt
;
269 * The signaled state has to set before executing any
270 * notification function
272 list_for_each_entry(evt
, &efi_events
, link
) {
273 if (!evt
->group
|| guidcmp(evt
->group
, event
->group
))
275 if (evt
->is_signaled
)
277 evt
->is_signaled
= true;
279 list_for_each_entry(evt
, &efi_events
, link
) {
280 if (!evt
->group
|| guidcmp(evt
->group
, event
->group
))
282 efi_queue_event(evt
);
285 event
->is_signaled
= true;
286 efi_queue_event(event
);
291 * efi_raise_tpl() - raise the task priority level
292 * @new_tpl: new value of the task priority level
294 * This function implements the RaiseTpl service.
296 * See the Unified Extensible Firmware Interface (UEFI) specification for
299 * Return: old value of the task priority level
301 static unsigned long EFIAPI
efi_raise_tpl(efi_uintn_t new_tpl
)
303 efi_uintn_t old_tpl
= efi_tpl
;
305 EFI_ENTRY("0x%zx", new_tpl
);
307 if (new_tpl
< efi_tpl
)
308 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__
);
310 if (efi_tpl
> TPL_HIGH_LEVEL
)
311 efi_tpl
= TPL_HIGH_LEVEL
;
313 EFI_EXIT(EFI_SUCCESS
);
318 * efi_restore_tpl() - lower the task priority level
319 * @old_tpl: value of the task priority level to be restored
321 * This function implements the RestoreTpl service.
323 * See the Unified Extensible Firmware Interface (UEFI) specification for
326 static void EFIAPI
efi_restore_tpl(efi_uintn_t old_tpl
)
328 EFI_ENTRY("0x%zx", old_tpl
);
330 if (old_tpl
> efi_tpl
)
331 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__
);
333 if (efi_tpl
> TPL_HIGH_LEVEL
)
334 efi_tpl
= TPL_HIGH_LEVEL
;
337 * Lowering the TPL may have made queued events eligible for execution.
341 EFI_EXIT(EFI_SUCCESS
);
345 * efi_allocate_pages_ext() - allocate memory pages
346 * @type: type of allocation to be performed
347 * @memory_type: usage type of the allocated memory
348 * @pages: number of pages to be allocated
349 * @memory: allocated memory
351 * This function implements the AllocatePages service.
353 * See the Unified Extensible Firmware Interface (UEFI) specification for
356 * Return: status code
358 static efi_status_t EFIAPI
efi_allocate_pages_ext(int type
, int memory_type
,
364 EFI_ENTRY("%d, %d, 0x%zx, %p", type
, memory_type
, pages
, memory
);
365 r
= efi_allocate_pages(type
, memory_type
, pages
, memory
);
370 * efi_free_pages_ext() - Free memory pages.
371 * @memory: start of the memory area to be freed
372 * @pages: number of pages to be freed
374 * This function implements the FreePages service.
376 * See the Unified Extensible Firmware Interface (UEFI) specification for
379 * Return: status code
381 static efi_status_t EFIAPI
efi_free_pages_ext(uint64_t memory
,
386 EFI_ENTRY("%llx, 0x%zx", memory
, pages
);
387 r
= efi_free_pages(memory
, pages
);
392 * efi_get_memory_map_ext() - get map describing memory usage
393 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
394 * on exit the size of the copied memory map
395 * @memory_map: buffer to which the memory map is written
396 * @map_key: key for the memory map
397 * @descriptor_size: size of an individual memory descriptor
398 * @descriptor_version: version number of the memory descriptor structure
400 * This function implements the GetMemoryMap service.
402 * See the Unified Extensible Firmware Interface (UEFI) specification for
405 * Return: status code
407 static efi_status_t EFIAPI
efi_get_memory_map_ext(
408 efi_uintn_t
*memory_map_size
,
409 struct efi_mem_desc
*memory_map
,
410 efi_uintn_t
*map_key
,
411 efi_uintn_t
*descriptor_size
,
412 uint32_t *descriptor_version
)
416 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size
, memory_map
,
417 map_key
, descriptor_size
, descriptor_version
);
418 r
= efi_get_memory_map(memory_map_size
, memory_map
, map_key
,
419 descriptor_size
, descriptor_version
);
424 * efi_allocate_pool_ext() - allocate memory from pool
425 * @pool_type: type of the pool from which memory is to be allocated
426 * @size: number of bytes to be allocated
427 * @buffer: allocated memory
429 * This function implements the AllocatePool service.
431 * See the Unified Extensible Firmware Interface (UEFI) specification for
434 * Return: status code
436 static efi_status_t EFIAPI
efi_allocate_pool_ext(int pool_type
,
442 EFI_ENTRY("%d, %zd, %p", pool_type
, size
, buffer
);
443 r
= efi_allocate_pool(pool_type
, size
, buffer
);
448 * efi_free_pool_ext() - free memory from pool
449 * @buffer: start of memory to be freed
451 * This function implements the FreePool service.
453 * See the Unified Extensible Firmware Interface (UEFI) specification for
456 * Return: status code
458 static efi_status_t EFIAPI
efi_free_pool_ext(void *buffer
)
462 EFI_ENTRY("%p", buffer
);
463 r
= efi_free_pool(buffer
);
468 * efi_add_handle() - add a new handle to the object list
470 * @handle: handle to be added
472 * The protocols list is initialized. The handle is added to the list of known
475 void efi_add_handle(efi_handle_t handle
)
479 INIT_LIST_HEAD(&handle
->protocols
);
480 list_add_tail(&handle
->link
, &efi_obj_list
);
484 * efi_create_handle() - create handle
485 * @handle: new handle
487 * Return: status code
489 efi_status_t
efi_create_handle(efi_handle_t
*handle
)
491 struct efi_object
*obj
;
493 obj
= calloc(1, sizeof(struct efi_object
));
495 return EFI_OUT_OF_RESOURCES
;
504 * efi_search_protocol() - find a protocol on a handle.
506 * @protocol_guid: GUID of the protocol
507 * @handler: reference to the protocol
509 * Return: status code
511 efi_status_t
efi_search_protocol(const efi_handle_t handle
,
512 const efi_guid_t
*protocol_guid
,
513 struct efi_handler
**handler
)
515 struct efi_object
*efiobj
;
516 struct list_head
*lhandle
;
518 if (!handle
|| !protocol_guid
)
519 return EFI_INVALID_PARAMETER
;
520 efiobj
= efi_search_obj(handle
);
522 return EFI_INVALID_PARAMETER
;
523 list_for_each(lhandle
, &efiobj
->protocols
) {
524 struct efi_handler
*protocol
;
526 protocol
= list_entry(lhandle
, struct efi_handler
, link
);
527 if (!guidcmp(protocol
->guid
, protocol_guid
)) {
533 return EFI_NOT_FOUND
;
537 * efi_remove_protocol() - delete protocol from a handle
538 * @handle: handle from which the protocol shall be deleted
539 * @protocol: GUID of the protocol to be deleted
540 * @protocol_interface: interface of the protocol implementation
542 * Return: status code
544 efi_status_t
efi_remove_protocol(const efi_handle_t handle
,
545 const efi_guid_t
*protocol
,
546 void *protocol_interface
)
548 struct efi_handler
*handler
;
551 ret
= efi_search_protocol(handle
, protocol
, &handler
);
552 if (ret
!= EFI_SUCCESS
)
554 if (handler
->protocol_interface
!= protocol_interface
)
555 return EFI_NOT_FOUND
;
556 list_del(&handler
->link
);
562 * efi_remove_all_protocols() - delete all protocols from a handle
563 * @handle: handle from which the protocols shall be deleted
565 * Return: status code
567 efi_status_t
efi_remove_all_protocols(const efi_handle_t handle
)
569 struct efi_object
*efiobj
;
570 struct efi_handler
*protocol
;
571 struct efi_handler
*pos
;
573 efiobj
= efi_search_obj(handle
);
575 return EFI_INVALID_PARAMETER
;
576 list_for_each_entry_safe(protocol
, pos
, &efiobj
->protocols
, link
) {
579 ret
= efi_remove_protocol(handle
, protocol
->guid
,
580 protocol
->protocol_interface
);
581 if (ret
!= EFI_SUCCESS
)
588 * efi_delete_handle() - delete handle
590 * @handle: handle to delete
592 void efi_delete_handle(efi_handle_t handle
)
596 efi_remove_all_protocols(handle
);
597 list_del(&handle
->link
);
602 * efi_is_event() - check if a pointer is a valid event
603 * @event: pointer to check
605 * Return: status code
607 static efi_status_t
efi_is_event(const struct efi_event
*event
)
609 const struct efi_event
*evt
;
612 return EFI_INVALID_PARAMETER
;
613 list_for_each_entry(evt
, &efi_events
, link
) {
617 return EFI_INVALID_PARAMETER
;
621 * efi_create_event() - create an event
623 * @type: type of the event to create
624 * @notify_tpl: task priority level of the event
625 * @notify_function: notification function of the event
626 * @notify_context: pointer passed to the notification function
627 * @group: event group
628 * @event: created event
630 * This function is used inside U-Boot code to create an event.
632 * For the API function implementing the CreateEvent service see
633 * efi_create_event_ext.
635 * Return: status code
637 efi_status_t
efi_create_event(uint32_t type
, efi_uintn_t notify_tpl
,
638 void (EFIAPI
*notify_function
) (
639 struct efi_event
*event
,
641 void *notify_context
, efi_guid_t
*group
,
642 struct efi_event
**event
)
644 struct efi_event
*evt
;
649 return EFI_INVALID_PARAMETER
;
654 case EVT_NOTIFY_SIGNAL
:
655 case EVT_TIMER
| EVT_NOTIFY_SIGNAL
:
656 case EVT_NOTIFY_WAIT
:
657 case EVT_TIMER
| EVT_NOTIFY_WAIT
:
658 case EVT_SIGNAL_EXIT_BOOT_SERVICES
:
659 pool_type
= EFI_BOOT_SERVICES_DATA
;
661 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
:
662 pool_type
= EFI_RUNTIME_SERVICES_DATA
;
665 return EFI_INVALID_PARAMETER
;
668 if ((type
& (EVT_NOTIFY_WAIT
| EVT_NOTIFY_SIGNAL
)) &&
669 (!notify_function
|| is_valid_tpl(notify_tpl
) != EFI_SUCCESS
))
670 return EFI_INVALID_PARAMETER
;
672 ret
= efi_allocate_pool(pool_type
, sizeof(struct efi_event
),
674 if (ret
!= EFI_SUCCESS
)
676 memset(evt
, 0, sizeof(struct efi_event
));
678 evt
->notify_tpl
= notify_tpl
;
679 evt
->notify_function
= notify_function
;
680 evt
->notify_context
= notify_context
;
682 /* Disable timers on boot up */
683 evt
->trigger_next
= -1ULL;
684 list_add_tail(&evt
->link
, &efi_events
);
690 * efi_create_event_ex() - create an event in a group
691 * @type: type of the event to create
692 * @notify_tpl: task priority level of the event
693 * @notify_function: notification function of the event
694 * @notify_context: pointer passed to the notification function
695 * @event: created event
696 * @event_group: event group
698 * This function implements the CreateEventEx service.
700 * See the Unified Extensible Firmware Interface (UEFI) specification for
703 * Return: status code
705 efi_status_t EFIAPI
efi_create_event_ex(uint32_t type
, efi_uintn_t notify_tpl
,
706 void (EFIAPI
*notify_function
) (
707 struct efi_event
*event
,
709 void *notify_context
,
710 efi_guid_t
*event_group
,
711 struct efi_event
**event
)
715 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type
, notify_tpl
, notify_function
,
716 notify_context
, event_group
);
719 * The allowable input parameters are the same as in CreateEvent()
720 * except for the following two disallowed event types.
723 case EVT_SIGNAL_EXIT_BOOT_SERVICES
:
724 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
:
725 ret
= EFI_INVALID_PARAMETER
;
729 ret
= efi_create_event(type
, notify_tpl
, notify_function
,
730 notify_context
, event_group
, event
);
732 return EFI_EXIT(ret
);
736 * efi_create_event_ext() - create an event
737 * @type: type of the event to create
738 * @notify_tpl: task priority level of the event
739 * @notify_function: notification function of the event
740 * @notify_context: pointer passed to the notification function
741 * @event: created event
743 * This function implements the CreateEvent service.
745 * See the Unified Extensible Firmware Interface (UEFI) specification for
748 * Return: status code
750 static efi_status_t EFIAPI
efi_create_event_ext(
751 uint32_t type
, efi_uintn_t notify_tpl
,
752 void (EFIAPI
*notify_function
) (
753 struct efi_event
*event
,
755 void *notify_context
, struct efi_event
**event
)
757 EFI_ENTRY("%d, 0x%zx, %p, %p", type
, notify_tpl
, notify_function
,
759 return EFI_EXIT(efi_create_event(type
, notify_tpl
, notify_function
,
760 notify_context
, NULL
, event
));
764 * efi_timer_check() - check if a timer event has occurred
766 * Check if a timer event has occurred or a queued notification function should
769 * Our timers have to work without interrupts, so we check whenever keyboard
770 * input or disk accesses happen if enough time elapsed for them to fire.
772 void efi_timer_check(void)
774 struct efi_event
*evt
;
775 u64 now
= timer_get_us();
777 list_for_each_entry(evt
, &efi_events
, link
) {
780 if (!(evt
->type
& EVT_TIMER
) || now
< evt
->trigger_next
)
782 switch (evt
->trigger_type
) {
783 case EFI_TIMER_RELATIVE
:
784 evt
->trigger_type
= EFI_TIMER_STOP
;
786 case EFI_TIMER_PERIODIC
:
787 evt
->trigger_next
+= evt
->trigger_time
;
792 evt
->is_signaled
= false;
793 efi_signal_event(evt
);
795 efi_process_event_queue();
800 * efi_set_timer() - set the trigger time for a timer event or stop the event
801 * @event: event for which the timer is set
802 * @type: type of the timer
803 * @trigger_time: trigger period in multiples of 100 ns
805 * This is the function for internal usage in U-Boot. For the API function
806 * implementing the SetTimer service see efi_set_timer_ext.
808 * Return: status code
810 efi_status_t
efi_set_timer(struct efi_event
*event
, enum efi_timer_delay type
,
811 uint64_t trigger_time
)
813 /* Check that the event is valid */
814 if (efi_is_event(event
) != EFI_SUCCESS
|| !(event
->type
& EVT_TIMER
))
815 return EFI_INVALID_PARAMETER
;
818 * The parameter defines a multiple of 100 ns.
819 * We use multiples of 1000 ns. So divide by 10.
821 do_div(trigger_time
, 10);
825 event
->trigger_next
= -1ULL;
827 case EFI_TIMER_PERIODIC
:
828 case EFI_TIMER_RELATIVE
:
829 event
->trigger_next
= timer_get_us() + trigger_time
;
832 return EFI_INVALID_PARAMETER
;
834 event
->trigger_type
= type
;
835 event
->trigger_time
= trigger_time
;
836 event
->is_signaled
= false;
841 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
843 * @event: event for which the timer is set
844 * @type: type of the timer
845 * @trigger_time: trigger period in multiples of 100 ns
847 * This function implements the SetTimer service.
849 * See the Unified Extensible Firmware Interface (UEFI) specification for
853 * Return: status code
855 static efi_status_t EFIAPI
efi_set_timer_ext(struct efi_event
*event
,
856 enum efi_timer_delay type
,
857 uint64_t trigger_time
)
859 EFI_ENTRY("%p, %d, %llx", event
, type
, trigger_time
);
860 return EFI_EXIT(efi_set_timer(event
, type
, trigger_time
));
864 * efi_wait_for_event() - wait for events to be signaled
865 * @num_events: number of events to be waited for
866 * @event: events to be waited for
867 * @index: index of the event that was signaled
869 * This function implements the WaitForEvent service.
871 * See the Unified Extensible Firmware Interface (UEFI) specification for
874 * Return: status code
876 static efi_status_t EFIAPI
efi_wait_for_event(efi_uintn_t num_events
,
877 struct efi_event
**event
,
882 EFI_ENTRY("%zd, %p, %p", num_events
, event
, index
);
884 /* Check parameters */
885 if (!num_events
|| !event
)
886 return EFI_EXIT(EFI_INVALID_PARAMETER
);
888 if (efi_tpl
!= TPL_APPLICATION
)
889 return EFI_EXIT(EFI_UNSUPPORTED
);
890 for (i
= 0; i
< num_events
; ++i
) {
891 if (efi_is_event(event
[i
]) != EFI_SUCCESS
)
892 return EFI_EXIT(EFI_INVALID_PARAMETER
);
893 if (!event
[i
]->type
|| event
[i
]->type
& EVT_NOTIFY_SIGNAL
)
894 return EFI_EXIT(EFI_INVALID_PARAMETER
);
895 if (!event
[i
]->is_signaled
)
896 efi_queue_event(event
[i
]);
899 /* Wait for signal */
901 for (i
= 0; i
< num_events
; ++i
) {
902 if (event
[i
]->is_signaled
)
905 /* Allow events to occur. */
911 * Reset the signal which is passed to the caller to allow periodic
914 event
[i
]->is_signaled
= false;
918 return EFI_EXIT(EFI_SUCCESS
);
922 * efi_signal_event_ext() - signal an EFI event
923 * @event: event to signal
925 * This function implements the SignalEvent service.
927 * See the Unified Extensible Firmware Interface (UEFI) specification for
930 * This functions sets the signaled state of the event and queues the
931 * notification function for execution.
933 * Return: status code
935 static efi_status_t EFIAPI
efi_signal_event_ext(struct efi_event
*event
)
937 EFI_ENTRY("%p", event
);
938 if (efi_is_event(event
) != EFI_SUCCESS
)
939 return EFI_EXIT(EFI_INVALID_PARAMETER
);
940 efi_signal_event(event
);
941 return EFI_EXIT(EFI_SUCCESS
);
945 * efi_close_event() - close an EFI event
946 * @event: event to close
948 * This function implements the CloseEvent service.
950 * See the Unified Extensible Firmware Interface (UEFI) specification for
953 * Return: status code
955 static efi_status_t EFIAPI
efi_close_event(struct efi_event
*event
)
957 struct efi_register_notify_event
*item
, *next
;
959 EFI_ENTRY("%p", event
);
960 if (efi_is_event(event
) != EFI_SUCCESS
)
961 return EFI_EXIT(EFI_INVALID_PARAMETER
);
963 /* Remove protocol notify registrations for the event */
964 list_for_each_entry_safe(item
, next
, &efi_register_notify_events
,
966 if (event
== item
->event
) {
967 struct efi_protocol_notification
*hitem
, *hnext
;
969 /* Remove signaled handles */
970 list_for_each_entry_safe(hitem
, hnext
, &item
->handles
,
972 list_del(&hitem
->link
);
975 list_del(&item
->link
);
979 /* Remove event from queue */
980 if (efi_event_is_queued(event
))
981 list_del(&event
->queue_link
);
983 list_del(&event
->link
);
984 efi_free_pool(event
);
985 return EFI_EXIT(EFI_SUCCESS
);
989 * efi_check_event() - check if an event is signaled
990 * @event: event to check
992 * This function implements the CheckEvent service.
994 * See the Unified Extensible Firmware Interface (UEFI) specification for
997 * If an event is not signaled yet, the notification function is queued. The
998 * signaled state is cleared.
1000 * Return: status code
1002 static efi_status_t EFIAPI
efi_check_event(struct efi_event
*event
)
1004 EFI_ENTRY("%p", event
);
1006 if (efi_is_event(event
) != EFI_SUCCESS
||
1007 event
->type
& EVT_NOTIFY_SIGNAL
)
1008 return EFI_EXIT(EFI_INVALID_PARAMETER
);
1009 if (!event
->is_signaled
)
1010 efi_queue_event(event
);
1011 if (event
->is_signaled
) {
1012 event
->is_signaled
= false;
1013 return EFI_EXIT(EFI_SUCCESS
);
1015 return EFI_EXIT(EFI_NOT_READY
);
1019 * efi_search_obj() - find the internal EFI object for a handle
1020 * @handle: handle to find
1022 * Return: EFI object
1024 struct efi_object
*efi_search_obj(const efi_handle_t handle
)
1026 struct efi_object
*efiobj
;
1031 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
1032 if (efiobj
== handle
)
1039 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1041 * @handler: handler of a protocol
1043 * Return: open protocol info entry
1045 static struct efi_open_protocol_info_entry
*efi_create_open_info(
1046 struct efi_handler
*handler
)
1048 struct efi_open_protocol_info_item
*item
;
1050 item
= calloc(1, sizeof(struct efi_open_protocol_info_item
));
1053 /* Append the item to the open protocol info list. */
1054 list_add_tail(&item
->link
, &handler
->open_infos
);
1060 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1061 * @item: open protocol info entry to delete
1063 * Return: status code
1065 static efi_status_t
efi_delete_open_info(
1066 struct efi_open_protocol_info_item
*item
)
1068 list_del(&item
->link
);
1074 * efi_add_protocol() - install new protocol on a handle
1075 * @handle: handle on which the protocol shall be installed
1076 * @protocol: GUID of the protocol to be installed
1077 * @protocol_interface: interface of the protocol implementation
1079 * Return: status code
1081 efi_status_t
efi_add_protocol(const efi_handle_t handle
,
1082 const efi_guid_t
*protocol
,
1083 void *protocol_interface
)
1085 struct efi_object
*efiobj
;
1086 struct efi_handler
*handler
;
1088 struct efi_register_notify_event
*event
;
1090 efiobj
= efi_search_obj(handle
);
1092 return EFI_INVALID_PARAMETER
;
1093 ret
= efi_search_protocol(handle
, protocol
, NULL
);
1094 if (ret
!= EFI_NOT_FOUND
)
1095 return EFI_INVALID_PARAMETER
;
1096 handler
= calloc(1, sizeof(struct efi_handler
));
1098 return EFI_OUT_OF_RESOURCES
;
1099 handler
->guid
= protocol
;
1100 handler
->protocol_interface
= protocol_interface
;
1101 INIT_LIST_HEAD(&handler
->open_infos
);
1102 list_add_tail(&handler
->link
, &efiobj
->protocols
);
1104 /* Notify registered events */
1105 list_for_each_entry(event
, &efi_register_notify_events
, link
) {
1106 if (!guidcmp(protocol
, &event
->protocol
)) {
1107 struct efi_protocol_notification
*notif
;
1109 notif
= calloc(1, sizeof(*notif
));
1111 list_del(&handler
->link
);
1113 return EFI_OUT_OF_RESOURCES
;
1115 notif
->handle
= handle
;
1116 list_add_tail(¬if
->link
, &event
->handles
);
1117 event
->event
->is_signaled
= false;
1118 efi_signal_event(event
->event
);
1122 if (!guidcmp(&efi_guid_device_path
, protocol
))
1123 EFI_PRINT("installed device path '%pD'\n", protocol_interface
);
1128 * efi_install_protocol_interface() - install protocol interface
1129 * @handle: handle on which the protocol shall be installed
1130 * @protocol: GUID of the protocol to be installed
1131 * @protocol_interface_type: type of the interface to be installed,
1132 * always EFI_NATIVE_INTERFACE
1133 * @protocol_interface: interface of the protocol implementation
1135 * This function implements the InstallProtocolInterface service.
1137 * See the Unified Extensible Firmware Interface (UEFI) specification for
1140 * Return: status code
1142 static efi_status_t EFIAPI
efi_install_protocol_interface(
1143 efi_handle_t
*handle
, const efi_guid_t
*protocol
,
1144 int protocol_interface_type
, void *protocol_interface
)
1148 EFI_ENTRY("%p, %pUl, %d, %p", handle
, protocol
, protocol_interface_type
,
1149 protocol_interface
);
1151 if (!handle
|| !protocol
||
1152 protocol_interface_type
!= EFI_NATIVE_INTERFACE
) {
1153 r
= EFI_INVALID_PARAMETER
;
1157 /* Create new handle if requested. */
1159 r
= efi_create_handle(handle
);
1160 if (r
!= EFI_SUCCESS
)
1162 EFI_PRINT("new handle %p\n", *handle
);
1164 EFI_PRINT("handle %p\n", *handle
);
1166 /* Add new protocol */
1167 r
= efi_add_protocol(*handle
, protocol
, protocol_interface
);
1173 * efi_get_drivers() - get all drivers associated to a controller
1174 * @handle: handle of the controller
1175 * @protocol: protocol GUID (optional)
1176 * @number_of_drivers: number of child controllers
1177 * @driver_handle_buffer: handles of the the drivers
1179 * The allocated buffer has to be freed with free().
1181 * Return: status code
1183 static efi_status_t
efi_get_drivers(efi_handle_t handle
,
1184 const efi_guid_t
*protocol
,
1185 efi_uintn_t
*number_of_drivers
,
1186 efi_handle_t
**driver_handle_buffer
)
1188 struct efi_handler
*handler
;
1189 struct efi_open_protocol_info_item
*item
;
1190 efi_uintn_t count
= 0, i
;
1193 /* Count all driver associations */
1194 list_for_each_entry(handler
, &handle
->protocols
, link
) {
1195 if (protocol
&& guidcmp(handler
->guid
, protocol
))
1197 list_for_each_entry(item
, &handler
->open_infos
, link
) {
1198 if (item
->info
.attributes
&
1199 EFI_OPEN_PROTOCOL_BY_DRIVER
)
1203 *number_of_drivers
= 0;
1205 *driver_handle_buffer
= NULL
;
1209 * Create buffer. In case of duplicate driver assignments the buffer
1210 * will be too large. But that does not harm.
1212 *driver_handle_buffer
= calloc(count
, sizeof(efi_handle_t
));
1213 if (!*driver_handle_buffer
)
1214 return EFI_OUT_OF_RESOURCES
;
1215 /* Collect unique driver handles */
1216 list_for_each_entry(handler
, &handle
->protocols
, link
) {
1217 if (protocol
&& guidcmp(handler
->guid
, protocol
))
1219 list_for_each_entry(item
, &handler
->open_infos
, link
) {
1220 if (item
->info
.attributes
&
1221 EFI_OPEN_PROTOCOL_BY_DRIVER
) {
1222 /* Check this is a new driver */
1224 for (i
= 0; i
< *number_of_drivers
; ++i
) {
1225 if ((*driver_handle_buffer
)[i
] ==
1226 item
->info
.agent_handle
)
1229 /* Copy handle to buffer */
1231 i
= (*number_of_drivers
)++;
1232 (*driver_handle_buffer
)[i
] =
1233 item
->info
.agent_handle
;
1242 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1243 * @handle: handle of the controller
1244 * @protocol: protocol GUID (optional)
1245 * @child_handle: handle of the child to destroy
1247 * This function implements the DisconnectController service.
1249 * See the Unified Extensible Firmware Interface (UEFI) specification for
1252 * Return: status code
1254 static efi_status_t efi_disconnect_all_drivers
1255 (efi_handle_t handle
,
1256 const efi_guid_t
*protocol
,
1257 efi_handle_t child_handle
)
1259 efi_uintn_t number_of_drivers
;
1260 efi_handle_t
*driver_handle_buffer
;
1261 efi_status_t r
, ret
;
1263 ret
= efi_get_drivers(handle
, protocol
, &number_of_drivers
,
1264 &driver_handle_buffer
);
1265 if (ret
!= EFI_SUCCESS
)
1267 if (!number_of_drivers
)
1269 ret
= EFI_NOT_FOUND
;
1270 while (number_of_drivers
) {
1271 r
= EFI_CALL(efi_disconnect_controller(
1273 driver_handle_buffer
[--number_of_drivers
],
1275 if (r
== EFI_SUCCESS
)
1278 free(driver_handle_buffer
);
1283 * efi_uninstall_protocol() - uninstall protocol interface
1285 * @handle: handle from which the protocol shall be removed
1286 * @protocol: GUID of the protocol to be removed
1287 * @protocol_interface: interface to be removed
1289 * This function DOES NOT delete a handle without installed protocol.
1291 * Return: status code
1293 static efi_status_t efi_uninstall_protocol
1294 (efi_handle_t handle
, const efi_guid_t
*protocol
,
1295 void *protocol_interface
)
1297 struct efi_object
*efiobj
;
1298 struct efi_handler
*handler
;
1299 struct efi_open_protocol_info_item
*item
;
1300 struct efi_open_protocol_info_item
*pos
;
1304 efiobj
= efi_search_obj(handle
);
1306 r
= EFI_INVALID_PARAMETER
;
1309 /* Find the protocol on the handle */
1310 r
= efi_search_protocol(handle
, protocol
, &handler
);
1311 if (r
!= EFI_SUCCESS
)
1313 /* Disconnect controllers */
1314 efi_disconnect_all_drivers(efiobj
, protocol
, NULL
);
1315 /* Close protocol */
1316 list_for_each_entry_safe(item
, pos
, &handler
->open_infos
, link
) {
1317 if (item
->info
.attributes
==
1318 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
||
1319 item
->info
.attributes
== EFI_OPEN_PROTOCOL_GET_PROTOCOL
||
1320 item
->info
.attributes
== EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)
1321 list_del(&item
->link
);
1323 if (!list_empty(&handler
->open_infos
)) {
1324 r
= EFI_ACCESS_DENIED
;
1327 r
= efi_remove_protocol(handle
, protocol
, protocol_interface
);
1333 * efi_uninstall_protocol_interface() - uninstall protocol interface
1334 * @handle: handle from which the protocol shall be removed
1335 * @protocol: GUID of the protocol to be removed
1336 * @protocol_interface: interface to be removed
1338 * This function implements the UninstallProtocolInterface service.
1340 * See the Unified Extensible Firmware Interface (UEFI) specification for
1343 * Return: status code
1345 static efi_status_t EFIAPI efi_uninstall_protocol_interface
1346 (efi_handle_t handle
, const efi_guid_t
*protocol
,
1347 void *protocol_interface
)
1351 EFI_ENTRY("%p, %pUl, %p", handle
, protocol
, protocol_interface
);
1353 ret
= efi_uninstall_protocol(handle
, protocol
, protocol_interface
);
1354 if (ret
!= EFI_SUCCESS
)
1357 /* If the last protocol has been removed, delete the handle. */
1358 if (list_empty(&handle
->protocols
)) {
1359 list_del(&handle
->link
);
1363 return EFI_EXIT(ret
);
1367 * efi_register_protocol_notify() - register an event for notification when a
1368 * protocol is installed.
1369 * @protocol: GUID of the protocol whose installation shall be notified
1370 * @event: event to be signaled upon installation of the protocol
1371 * @registration: key for retrieving the registration information
1373 * This function implements the RegisterProtocolNotify service.
1374 * See the Unified Extensible Firmware Interface (UEFI) specification
1377 * Return: status code
1379 static efi_status_t EFIAPI
efi_register_protocol_notify(
1380 const efi_guid_t
*protocol
,
1381 struct efi_event
*event
,
1382 void **registration
)
1384 struct efi_register_notify_event
*item
;
1385 efi_status_t ret
= EFI_SUCCESS
;
1387 EFI_ENTRY("%pUl, %p, %p", protocol
, event
, registration
);
1389 if (!protocol
|| !event
|| !registration
) {
1390 ret
= EFI_INVALID_PARAMETER
;
1394 item
= calloc(1, sizeof(struct efi_register_notify_event
));
1396 ret
= EFI_OUT_OF_RESOURCES
;
1400 item
->event
= event
;
1401 memcpy(&item
->protocol
, protocol
, sizeof(efi_guid_t
));
1402 INIT_LIST_HEAD(&item
->handles
);
1404 list_add_tail(&item
->link
, &efi_register_notify_events
);
1406 *registration
= item
;
1408 return EFI_EXIT(ret
);
1412 * efi_search() - determine if an EFI handle implements a protocol
1414 * @search_type: selection criterion
1415 * @protocol: GUID of the protocol
1418 * See the documentation of the LocateHandle service in the UEFI specification.
1420 * Return: 0 if the handle implements the protocol
1422 static int efi_search(enum efi_locate_search_type search_type
,
1423 const efi_guid_t
*protocol
, efi_handle_t handle
)
1427 switch (search_type
) {
1431 ret
= efi_search_protocol(handle
, protocol
, NULL
);
1432 return (ret
!= EFI_SUCCESS
);
1434 /* Invalid search type */
1440 * efi_check_register_notify_event() - check if registration key is valid
1442 * Check that a pointer is a valid registration key as returned by
1443 * RegisterProtocolNotify().
1445 * @key: registration key
1446 * Return: valid registration key or NULL
1448 static struct efi_register_notify_event
*efi_check_register_notify_event
1451 struct efi_register_notify_event
*event
;
1453 list_for_each_entry(event
, &efi_register_notify_events
, link
) {
1454 if (event
== (struct efi_register_notify_event
*)key
)
1461 * efi_locate_handle() - locate handles implementing a protocol
1463 * @search_type: selection criterion
1464 * @protocol: GUID of the protocol
1465 * @search_key: registration key
1466 * @buffer_size: size of the buffer to receive the handles in bytes
1467 * @buffer: buffer to receive the relevant handles
1469 * This function is meant for U-Boot internal calls. For the API implementation
1470 * of the LocateHandle service see efi_locate_handle_ext.
1472 * Return: status code
1474 static efi_status_t
efi_locate_handle(
1475 enum efi_locate_search_type search_type
,
1476 const efi_guid_t
*protocol
, void *search_key
,
1477 efi_uintn_t
*buffer_size
, efi_handle_t
*buffer
)
1479 struct efi_object
*efiobj
;
1480 efi_uintn_t size
= 0;
1481 struct efi_register_notify_event
*event
;
1482 struct efi_protocol_notification
*handle
= NULL
;
1484 /* Check parameters */
1485 switch (search_type
) {
1488 case BY_REGISTER_NOTIFY
:
1490 return EFI_INVALID_PARAMETER
;
1491 /* Check that the registration key is valid */
1492 event
= efi_check_register_notify_event(search_key
);
1494 return EFI_INVALID_PARAMETER
;
1498 return EFI_INVALID_PARAMETER
;
1501 return EFI_INVALID_PARAMETER
;
1504 /* Count how much space we need */
1505 if (search_type
== BY_REGISTER_NOTIFY
) {
1506 if (list_empty(&event
->handles
))
1507 return EFI_NOT_FOUND
;
1508 handle
= list_first_entry(&event
->handles
,
1509 struct efi_protocol_notification
,
1511 efiobj
= handle
->handle
;
1512 size
+= sizeof(void *);
1514 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
1515 if (!efi_search(search_type
, protocol
, efiobj
))
1516 size
+= sizeof(void *);
1519 return EFI_NOT_FOUND
;
1523 return EFI_INVALID_PARAMETER
;
1525 if (*buffer_size
< size
) {
1526 *buffer_size
= size
;
1527 return EFI_BUFFER_TOO_SMALL
;
1530 *buffer_size
= size
;
1532 /* The buffer size is sufficient but there is no buffer */
1534 return EFI_INVALID_PARAMETER
;
1536 /* Then fill the array */
1537 if (search_type
== BY_REGISTER_NOTIFY
) {
1539 list_del(&handle
->link
);
1541 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
1542 if (!efi_search(search_type
, protocol
, efiobj
))
1551 * efi_locate_handle_ext() - locate handles implementing a protocol.
1552 * @search_type: selection criterion
1553 * @protocol: GUID of the protocol
1554 * @search_key: registration key
1555 * @buffer_size: size of the buffer to receive the handles in bytes
1556 * @buffer: buffer to receive the relevant handles
1558 * This function implements the LocateHandle service.
1560 * See the Unified Extensible Firmware Interface (UEFI) specification for
1563 * Return: 0 if the handle implements the protocol
1565 static efi_status_t EFIAPI
efi_locate_handle_ext(
1566 enum efi_locate_search_type search_type
,
1567 const efi_guid_t
*protocol
, void *search_key
,
1568 efi_uintn_t
*buffer_size
, efi_handle_t
*buffer
)
1570 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type
, protocol
, search_key
,
1571 buffer_size
, buffer
);
1573 return EFI_EXIT(efi_locate_handle(search_type
, protocol
, search_key
,
1574 buffer_size
, buffer
));
1578 * efi_remove_configuration_table() - collapses configuration table entries,
1581 * @i: index of the table entry to be removed
1583 static void efi_remove_configuration_table(int i
)
1585 struct efi_configuration_table
*this = &systab
.tables
[i
];
1586 struct efi_configuration_table
*next
= &systab
.tables
[i
+ 1];
1587 struct efi_configuration_table
*end
= &systab
.tables
[systab
.nr_tables
];
1589 memmove(this, next
, (ulong
)end
- (ulong
)next
);
1594 * efi_install_configuration_table() - adds, updates, or removes a
1595 * configuration table
1596 * @guid: GUID of the installed table
1597 * @table: table to be installed
1599 * This function is used for internal calls. For the API implementation of the
1600 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1602 * Return: status code
1604 efi_status_t
efi_install_configuration_table(const efi_guid_t
*guid
,
1607 struct efi_event
*evt
;
1611 return EFI_INVALID_PARAMETER
;
1613 /* Check for GUID override */
1614 for (i
= 0; i
< systab
.nr_tables
; i
++) {
1615 if (!guidcmp(guid
, &systab
.tables
[i
].guid
)) {
1617 systab
.tables
[i
].table
= table
;
1619 efi_remove_configuration_table(i
);
1625 return EFI_NOT_FOUND
;
1627 /* No override, check for overflow */
1628 if (i
>= EFI_MAX_CONFIGURATION_TABLES
)
1629 return EFI_OUT_OF_RESOURCES
;
1631 /* Add a new entry */
1632 memcpy(&systab
.tables
[i
].guid
, guid
, sizeof(*guid
));
1633 systab
.tables
[i
].table
= table
;
1634 systab
.nr_tables
= i
+ 1;
1637 /* systab.nr_tables may have changed. So we need to update the CRC32 */
1638 efi_update_table_header_crc32(&systab
.hdr
);
1640 /* Notify that the configuration table was changed */
1641 list_for_each_entry(evt
, &efi_events
, link
) {
1642 if (evt
->group
&& !guidcmp(evt
->group
, guid
)) {
1643 efi_signal_event(evt
);
1652 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1653 * configuration table.
1654 * @guid: GUID of the installed table
1655 * @table: table to be installed
1657 * This function implements the InstallConfigurationTable service.
1659 * See the Unified Extensible Firmware Interface (UEFI) specification for
1662 * Return: status code
1664 static efi_status_t EFIAPI
efi_install_configuration_table_ext(efi_guid_t
*guid
,
1667 EFI_ENTRY("%pUl, %p", guid
, table
);
1668 return EFI_EXIT(efi_install_configuration_table(guid
, table
));
1672 * efi_setup_loaded_image() - initialize a loaded image
1674 * Initialize a loaded_image_info and loaded_image_info object with correct
1675 * protocols, boot-device, etc.
1677 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
1680 * @device_path: device path of the loaded image
1681 * @file_path: file path of the loaded image
1682 * @handle_ptr: handle of the loaded image
1683 * @info_ptr: loaded image protocol
1684 * Return: status code
1686 efi_status_t
efi_setup_loaded_image(struct efi_device_path
*device_path
,
1687 struct efi_device_path
*file_path
,
1688 struct efi_loaded_image_obj
**handle_ptr
,
1689 struct efi_loaded_image
**info_ptr
)
1692 struct efi_loaded_image
*info
= NULL
;
1693 struct efi_loaded_image_obj
*obj
= NULL
;
1694 struct efi_device_path
*dp
;
1696 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1700 info
= calloc(1, sizeof(*info
));
1702 return EFI_OUT_OF_RESOURCES
;
1703 obj
= calloc(1, sizeof(*obj
));
1706 return EFI_OUT_OF_RESOURCES
;
1708 obj
->header
.type
= EFI_OBJECT_TYPE_LOADED_IMAGE
;
1710 /* Add internal object to object list */
1711 efi_add_handle(&obj
->header
);
1713 info
->revision
= EFI_LOADED_IMAGE_PROTOCOL_REVISION
;
1714 info
->file_path
= file_path
;
1715 info
->system_table
= &systab
;
1718 info
->device_handle
= efi_dp_find_obj(device_path
, NULL
);
1720 dp
= efi_dp_append(device_path
, file_path
);
1722 ret
= EFI_OUT_OF_RESOURCES
;
1728 ret
= efi_add_protocol(&obj
->header
,
1729 &efi_guid_loaded_image_device_path
, dp
);
1730 if (ret
!= EFI_SUCCESS
)
1734 * When asking for the loaded_image interface, just
1735 * return handle which points to loaded_image_info
1737 ret
= efi_add_protocol(&obj
->header
,
1738 &efi_guid_loaded_image
, info
);
1739 if (ret
!= EFI_SUCCESS
)
1747 printf("ERROR: Failure to install protocols for loaded image\n");
1748 efi_delete_handle(&obj
->header
);
1754 * efi_load_image_from_path() - load an image using a file path
1756 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1757 * callers obligation to update the memory type as needed.
1759 * @file_path: the path of the image to load
1760 * @buffer: buffer containing the loaded image
1761 * @size: size of the loaded image
1762 * Return: status code
1765 efi_status_t
efi_load_image_from_path(struct efi_device_path
*file_path
,
1766 void **buffer
, efi_uintn_t
*size
)
1768 struct efi_file_info
*info
= NULL
;
1769 struct efi_file_handle
*f
;
1770 static efi_status_t ret
;
1774 /* In case of failure nothing is returned */
1779 f
= efi_file_from_path(file_path
);
1781 return EFI_NOT_FOUND
;
1785 EFI_CALL(ret
= f
->getinfo(f
, (efi_guid_t
*)&efi_file_info_guid
,
1787 if (ret
!= EFI_BUFFER_TOO_SMALL
) {
1788 ret
= EFI_DEVICE_ERROR
;
1793 EFI_CALL(ret
= f
->getinfo(f
, (efi_guid_t
*)&efi_file_info_guid
, &bs
,
1795 if (ret
!= EFI_SUCCESS
)
1799 * When reading the file we do not yet know if it contains an
1800 * application, a boottime driver, or a runtime driver. So here we
1801 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1802 * update the reservation according to the image type.
1804 bs
= info
->file_size
;
1805 ret
= efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES
,
1806 EFI_BOOT_SERVICES_DATA
,
1807 efi_size_in_pages(bs
), &addr
);
1808 if (ret
!= EFI_SUCCESS
) {
1809 ret
= EFI_OUT_OF_RESOURCES
;
1814 EFI_CALL(ret
= f
->read(f
, &bs
, (void *)(uintptr_t)addr
));
1815 if (ret
!= EFI_SUCCESS
)
1816 efi_free_pages(addr
, efi_size_in_pages(bs
));
1817 *buffer
= (void *)(uintptr_t)addr
;
1820 EFI_CALL(f
->close(f
));
1826 * efi_load_image() - load an EFI image into memory
1827 * @boot_policy: true for request originating from the boot manager
1828 * @parent_image: the caller's image handle
1829 * @file_path: the path of the image to load
1830 * @source_buffer: memory location from which the image is installed
1831 * @source_size: size of the memory area from which the image is installed
1832 * @image_handle: handle for the newly installed image
1834 * This function implements the LoadImage service.
1836 * See the Unified Extensible Firmware Interface (UEFI) specification
1839 * Return: status code
1841 efi_status_t EFIAPI
efi_load_image(bool boot_policy
,
1842 efi_handle_t parent_image
,
1843 struct efi_device_path
*file_path
,
1844 void *source_buffer
,
1845 efi_uintn_t source_size
,
1846 efi_handle_t
*image_handle
)
1848 struct efi_device_path
*dp
, *fp
;
1849 struct efi_loaded_image
*info
= NULL
;
1850 struct efi_loaded_image_obj
**image_obj
=
1851 (struct efi_loaded_image_obj
**)image_handle
;
1855 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy
, parent_image
,
1856 file_path
, source_buffer
, source_size
, image_handle
);
1858 if (!image_handle
|| (!source_buffer
&& !file_path
) ||
1859 !efi_search_obj(parent_image
) ||
1860 /* The parent image handle must refer to a loaded image */
1861 !parent_image
->type
) {
1862 ret
= EFI_INVALID_PARAMETER
;
1866 if (!source_buffer
) {
1867 ret
= efi_load_image_from_path(file_path
, &dest_buffer
,
1869 if (ret
!= EFI_SUCCESS
)
1873 ret
= EFI_LOAD_ERROR
;
1876 dest_buffer
= source_buffer
;
1878 /* split file_path which contains both the device and file parts */
1879 efi_dp_split_file_path(file_path
, &dp
, &fp
);
1880 ret
= efi_setup_loaded_image(dp
, fp
, image_obj
, &info
);
1881 if (ret
== EFI_SUCCESS
)
1882 ret
= efi_load_pe(*image_obj
, dest_buffer
, info
);
1884 /* Release buffer to which file was loaded */
1885 efi_free_pages((uintptr_t)dest_buffer
,
1886 efi_size_in_pages(source_size
));
1887 if (ret
== EFI_SUCCESS
) {
1888 info
->system_table
= &systab
;
1889 info
->parent_handle
= parent_image
;
1891 /* The image is invalid. Release all associated resources. */
1892 efi_delete_handle(*image_handle
);
1893 *image_handle
= NULL
;
1897 return EFI_EXIT(ret
);
1901 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1903 static void efi_exit_caches(void)
1905 #if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
1907 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
1908 * caches are enabled.
1911 * According to the UEFI spec caches that can be managed via CP15
1912 * operations should be enabled. Caches requiring platform information
1913 * to manage should be disabled. This should not happen in
1914 * ExitBootServices() but before invoking any UEFI binary is invoked.
1916 * We want to keep the current workaround while GRUB prior to version
1917 * 2.04 is still in use.
1919 cleanup_before_linux();
1924 * efi_exit_boot_services() - stop all boot services
1925 * @image_handle: handle of the loaded image
1926 * @map_key: key of the memory map
1928 * This function implements the ExitBootServices service.
1930 * See the Unified Extensible Firmware Interface (UEFI) specification
1933 * All timer events are disabled. For exit boot services events the
1934 * notification function is called. The boot services are disabled in the
1937 * Return: status code
1939 static efi_status_t EFIAPI
efi_exit_boot_services(efi_handle_t image_handle
,
1940 efi_uintn_t map_key
)
1942 struct efi_event
*evt
, *next_event
;
1943 efi_status_t ret
= EFI_SUCCESS
;
1945 EFI_ENTRY("%p, %zx", image_handle
, map_key
);
1947 /* Check that the caller has read the current memory map */
1948 if (map_key
!= efi_memory_map_key
) {
1949 ret
= EFI_INVALID_PARAMETER
;
1953 /* Check if ExitBootServices has already been called */
1954 if (!systab
.boottime
)
1957 /* Stop all timer related activities */
1958 timers_enabled
= false;
1960 /* Add related events to the event group */
1961 list_for_each_entry(evt
, &efi_events
, link
) {
1962 if (evt
->type
== EVT_SIGNAL_EXIT_BOOT_SERVICES
)
1963 evt
->group
= &efi_guid_event_group_exit_boot_services
;
1965 /* Notify that ExitBootServices is invoked. */
1966 list_for_each_entry(evt
, &efi_events
, link
) {
1968 !guidcmp(evt
->group
,
1969 &efi_guid_event_group_exit_boot_services
)) {
1970 efi_signal_event(evt
);
1975 /* Make sure that notification functions are not called anymore */
1976 efi_tpl
= TPL_HIGH_LEVEL
;
1978 /* Notify variable services */
1979 efi_variables_boot_exit_notify();
1981 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
1982 list_for_each_entry_safe(evt
, next_event
, &efi_events
, link
) {
1983 if (evt
->type
!= EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
)
1984 list_del(&evt
->link
);
1987 board_quiesce_devices();
1989 /* Patch out unsupported runtime function */
1990 efi_runtime_detach();
1992 /* Fix up caches for EFI payloads if necessary */
1995 /* This stops all lingering devices */
1996 bootm_disable_interrupts();
1998 /* Disable boot time services */
1999 systab
.con_in_handle
= NULL
;
2000 systab
.con_in
= NULL
;
2001 systab
.con_out_handle
= NULL
;
2002 systab
.con_out
= NULL
;
2003 systab
.stderr_handle
= NULL
;
2004 systab
.std_err
= NULL
;
2005 systab
.boottime
= NULL
;
2007 /* Recalculate CRC32 */
2008 efi_update_table_header_crc32(&systab
.hdr
);
2010 /* Give the payload some time to boot */
2011 efi_set_watchdog(0);
2014 return EFI_EXIT(ret
);
2018 * efi_get_next_monotonic_count() - get next value of the counter
2019 * @count: returned value of the counter
2021 * This function implements the NextMonotonicCount service.
2023 * See the Unified Extensible Firmware Interface (UEFI) specification for
2026 * Return: status code
2028 static efi_status_t EFIAPI
efi_get_next_monotonic_count(uint64_t *count
)
2030 static uint64_t mono
;
2033 EFI_ENTRY("%p", count
);
2035 ret
= EFI_INVALID_PARAMETER
;
2041 return EFI_EXIT(ret
);
2045 * efi_stall() - sleep
2046 * @microseconds: period to sleep in microseconds
2048 * This function implements the Stall service.
2050 * See the Unified Extensible Firmware Interface (UEFI) specification for
2053 * Return: status code
2055 static efi_status_t EFIAPI
efi_stall(unsigned long microseconds
)
2059 EFI_ENTRY("%ld", microseconds
);
2061 end_tick
= get_ticks() + usec_to_tick(microseconds
);
2062 while (get_ticks() < end_tick
)
2065 return EFI_EXIT(EFI_SUCCESS
);
2069 * efi_set_watchdog_timer() - reset the watchdog timer
2070 * @timeout: seconds before reset by watchdog
2071 * @watchdog_code: code to be logged when resetting
2072 * @data_size: size of buffer in bytes
2073 * @watchdog_data: buffer with data describing the reset reason
2075 * This function implements the SetWatchdogTimer service.
2077 * See the Unified Extensible Firmware Interface (UEFI) specification for
2080 * Return: status code
2082 static efi_status_t EFIAPI
efi_set_watchdog_timer(unsigned long timeout
,
2083 uint64_t watchdog_code
,
2084 unsigned long data_size
,
2085 uint16_t *watchdog_data
)
2087 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout
, watchdog_code
,
2088 data_size
, watchdog_data
);
2089 return EFI_EXIT(efi_set_watchdog(timeout
));
2093 * efi_close_protocol() - close a protocol
2094 * @handle: handle on which the protocol shall be closed
2095 * @protocol: GUID of the protocol to close
2096 * @agent_handle: handle of the driver
2097 * @controller_handle: handle of the controller
2099 * This function implements the CloseProtocol service.
2101 * See the Unified Extensible Firmware Interface (UEFI) specification for
2104 * Return: status code
2106 static efi_status_t EFIAPI
efi_close_protocol(efi_handle_t handle
,
2107 const efi_guid_t
*protocol
,
2108 efi_handle_t agent_handle
,
2109 efi_handle_t controller_handle
)
2111 struct efi_handler
*handler
;
2112 struct efi_open_protocol_info_item
*item
;
2113 struct efi_open_protocol_info_item
*pos
;
2116 EFI_ENTRY("%p, %pUl, %p, %p", handle
, protocol
, agent_handle
,
2119 if (!efi_search_obj(agent_handle
) ||
2120 (controller_handle
&& !efi_search_obj(controller_handle
))) {
2121 r
= EFI_INVALID_PARAMETER
;
2124 r
= efi_search_protocol(handle
, protocol
, &handler
);
2125 if (r
!= EFI_SUCCESS
)
2129 list_for_each_entry_safe(item
, pos
, &handler
->open_infos
, link
) {
2130 if (item
->info
.agent_handle
== agent_handle
&&
2131 item
->info
.controller_handle
== controller_handle
) {
2132 efi_delete_open_info(item
);
2141 * efi_open_protocol_information() - provide information about then open status
2142 * of a protocol on a handle
2143 * @handle: handle for which the information shall be retrieved
2144 * @protocol: GUID of the protocol
2145 * @entry_buffer: buffer to receive the open protocol information
2146 * @entry_count: number of entries available in the buffer
2148 * This function implements the OpenProtocolInformation service.
2150 * See the Unified Extensible Firmware Interface (UEFI) specification for
2153 * Return: status code
2155 static efi_status_t EFIAPI
efi_open_protocol_information(
2156 efi_handle_t handle
, const efi_guid_t
*protocol
,
2157 struct efi_open_protocol_info_entry
**entry_buffer
,
2158 efi_uintn_t
*entry_count
)
2160 unsigned long buffer_size
;
2161 unsigned long count
;
2162 struct efi_handler
*handler
;
2163 struct efi_open_protocol_info_item
*item
;
2166 EFI_ENTRY("%p, %pUl, %p, %p", handle
, protocol
, entry_buffer
,
2169 /* Check parameters */
2170 if (!entry_buffer
) {
2171 r
= EFI_INVALID_PARAMETER
;
2174 r
= efi_search_protocol(handle
, protocol
, &handler
);
2175 if (r
!= EFI_SUCCESS
)
2180 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2181 if (item
->info
.open_count
)
2184 *entry_count
= count
;
2185 *entry_buffer
= NULL
;
2192 buffer_size
= count
* sizeof(struct efi_open_protocol_info_entry
);
2193 r
= efi_allocate_pool(EFI_BOOT_SERVICES_DATA
, buffer_size
,
2194 (void **)entry_buffer
);
2195 if (r
!= EFI_SUCCESS
)
2197 list_for_each_entry_reverse(item
, &handler
->open_infos
, link
) {
2198 if (item
->info
.open_count
)
2199 (*entry_buffer
)[--count
] = item
->info
;
2206 * efi_protocols_per_handle() - get protocols installed on a handle
2207 * @handle: handle for which the information is retrieved
2208 * @protocol_buffer: buffer with protocol GUIDs
2209 * @protocol_buffer_count: number of entries in the buffer
2211 * This function implements the ProtocolsPerHandleService.
2213 * See the Unified Extensible Firmware Interface (UEFI) specification for
2216 * Return: status code
2218 static efi_status_t EFIAPI
efi_protocols_per_handle(
2219 efi_handle_t handle
, efi_guid_t
***protocol_buffer
,
2220 efi_uintn_t
*protocol_buffer_count
)
2222 unsigned long buffer_size
;
2223 struct efi_object
*efiobj
;
2224 struct list_head
*protocol_handle
;
2227 EFI_ENTRY("%p, %p, %p", handle
, protocol_buffer
,
2228 protocol_buffer_count
);
2230 if (!handle
|| !protocol_buffer
|| !protocol_buffer_count
)
2231 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2233 *protocol_buffer
= NULL
;
2234 *protocol_buffer_count
= 0;
2236 efiobj
= efi_search_obj(handle
);
2238 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2240 /* Count protocols */
2241 list_for_each(protocol_handle
, &efiobj
->protocols
) {
2242 ++*protocol_buffer_count
;
2246 if (*protocol_buffer_count
) {
2249 buffer_size
= sizeof(efi_guid_t
*) * *protocol_buffer_count
;
2250 r
= efi_allocate_pool(EFI_BOOT_SERVICES_DATA
, buffer_size
,
2251 (void **)protocol_buffer
);
2252 if (r
!= EFI_SUCCESS
)
2254 list_for_each(protocol_handle
, &efiobj
->protocols
) {
2255 struct efi_handler
*protocol
;
2257 protocol
= list_entry(protocol_handle
,
2258 struct efi_handler
, link
);
2259 (*protocol_buffer
)[j
] = (void *)protocol
->guid
;
2264 return EFI_EXIT(EFI_SUCCESS
);
2268 * efi_locate_handle_buffer() - locate handles implementing a protocol
2269 * @search_type: selection criterion
2270 * @protocol: GUID of the protocol
2271 * @search_key: registration key
2272 * @no_handles: number of returned handles
2273 * @buffer: buffer with the returned handles
2275 * This function implements the LocateHandleBuffer service.
2277 * See the Unified Extensible Firmware Interface (UEFI) specification for
2280 * Return: status code
2282 static efi_status_t EFIAPI
efi_locate_handle_buffer(
2283 enum efi_locate_search_type search_type
,
2284 const efi_guid_t
*protocol
, void *search_key
,
2285 efi_uintn_t
*no_handles
, efi_handle_t
**buffer
)
2288 efi_uintn_t buffer_size
= 0;
2290 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type
, protocol
, search_key
,
2291 no_handles
, buffer
);
2293 if (!no_handles
|| !buffer
) {
2294 r
= EFI_INVALID_PARAMETER
;
2299 r
= efi_locate_handle(search_type
, protocol
, search_key
, &buffer_size
,
2301 if (r
!= EFI_BUFFER_TOO_SMALL
)
2303 r
= efi_allocate_pool(EFI_BOOT_SERVICES_DATA
, buffer_size
,
2305 if (r
!= EFI_SUCCESS
)
2307 r
= efi_locate_handle(search_type
, protocol
, search_key
, &buffer_size
,
2309 if (r
== EFI_SUCCESS
)
2310 *no_handles
= buffer_size
/ sizeof(efi_handle_t
);
2316 * efi_locate_protocol() - find an interface implementing a protocol
2317 * @protocol: GUID of the protocol
2318 * @registration: registration key passed to the notification function
2319 * @protocol_interface: interface implementing the protocol
2321 * This function implements the LocateProtocol service.
2323 * See the Unified Extensible Firmware Interface (UEFI) specification for
2326 * Return: status code
2328 static efi_status_t EFIAPI
efi_locate_protocol(const efi_guid_t
*protocol
,
2330 void **protocol_interface
)
2332 struct efi_handler
*handler
;
2334 struct efi_object
*efiobj
;
2336 EFI_ENTRY("%pUl, %p, %p", protocol
, registration
, protocol_interface
);
2339 * The UEFI spec explicitly requires a protocol even if a registration
2340 * key is provided. This differs from the logic in LocateHandle().
2342 if (!protocol
|| !protocol_interface
)
2343 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2346 struct efi_register_notify_event
*event
;
2347 struct efi_protocol_notification
*handle
;
2349 event
= efi_check_register_notify_event(registration
);
2351 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2353 * The UEFI spec requires to return EFI_NOT_FOUND if no
2354 * protocol instance matches protocol and registration.
2355 * So let's do the same for a mismatch between protocol and
2358 if (guidcmp(&event
->protocol
, protocol
))
2360 if (list_empty(&event
->handles
))
2362 handle
= list_first_entry(&event
->handles
,
2363 struct efi_protocol_notification
,
2365 efiobj
= handle
->handle
;
2366 list_del(&handle
->link
);
2368 ret
= efi_search_protocol(efiobj
, protocol
, &handler
);
2369 if (ret
== EFI_SUCCESS
)
2372 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
2373 ret
= efi_search_protocol(efiobj
, protocol
, &handler
);
2374 if (ret
== EFI_SUCCESS
)
2379 *protocol_interface
= NULL
;
2380 return EFI_EXIT(EFI_NOT_FOUND
);
2382 *protocol_interface
= handler
->protocol_interface
;
2383 return EFI_EXIT(EFI_SUCCESS
);
2387 * efi_locate_device_path() - Get the device path and handle of an device
2388 * implementing a protocol
2389 * @protocol: GUID of the protocol
2390 * @device_path: device path
2391 * @device: handle of the device
2393 * This function implements the LocateDevicePath service.
2395 * See the Unified Extensible Firmware Interface (UEFI) specification for
2398 * Return: status code
2400 static efi_status_t EFIAPI
efi_locate_device_path(
2401 const efi_guid_t
*protocol
,
2402 struct efi_device_path
**device_path
,
2403 efi_handle_t
*device
)
2405 struct efi_device_path
*dp
;
2407 struct efi_handler
*handler
;
2408 efi_handle_t
*handles
;
2410 size_t len_best
= 0;
2411 efi_uintn_t no_handles
;
2415 EFI_ENTRY("%pUl, %p, %p", protocol
, device_path
, device
);
2417 if (!protocol
|| !device_path
|| !*device_path
) {
2418 ret
= EFI_INVALID_PARAMETER
;
2422 /* Find end of device path */
2423 len
= efi_dp_instance_size(*device_path
);
2425 /* Get all handles implementing the protocol */
2426 ret
= EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL
, protocol
, NULL
,
2427 &no_handles
, &handles
));
2428 if (ret
!= EFI_SUCCESS
)
2431 for (i
= 0; i
< no_handles
; ++i
) {
2432 /* Find the device path protocol */
2433 ret
= efi_search_protocol(handles
[i
], &efi_guid_device_path
,
2435 if (ret
!= EFI_SUCCESS
)
2437 dp
= (struct efi_device_path
*)handler
->protocol_interface
;
2438 len_dp
= efi_dp_instance_size(dp
);
2440 * This handle can only be a better fit
2441 * if its device path length is longer than the best fit and
2442 * if its device path length is shorter of equal the searched
2445 if (len_dp
<= len_best
|| len_dp
> len
)
2447 /* Check if dp is a subpath of device_path */
2448 if (memcmp(*device_path
, dp
, len_dp
))
2451 ret
= EFI_INVALID_PARAMETER
;
2454 *device
= handles
[i
];
2458 remainder
= (u8
*)*device_path
+ len_best
;
2459 *device_path
= (struct efi_device_path
*)remainder
;
2462 ret
= EFI_NOT_FOUND
;
2465 return EFI_EXIT(ret
);
2469 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2471 * @handle: handle on which the protocol interfaces shall be installed
2472 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2475 * This function implements the MultipleProtocolInterfaces service.
2477 * See the Unified Extensible Firmware Interface (UEFI) specification for
2480 * Return: status code
2482 efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2483 (efi_handle_t
*handle
, ...)
2485 EFI_ENTRY("%p", handle
);
2488 const efi_guid_t
*protocol
;
2489 void *protocol_interface
;
2490 efi_handle_t old_handle
;
2491 efi_status_t r
= EFI_SUCCESS
;
2495 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2497 efi_va_start(argptr
, handle
);
2499 protocol
= efi_va_arg(argptr
, efi_guid_t
*);
2502 protocol_interface
= efi_va_arg(argptr
, void*);
2503 /* Check that a device path has not been installed before */
2504 if (!guidcmp(protocol
, &efi_guid_device_path
)) {
2505 struct efi_device_path
*dp
= protocol_interface
;
2507 r
= EFI_CALL(efi_locate_device_path(protocol
, &dp
,
2509 if (r
== EFI_SUCCESS
&&
2510 dp
->type
== DEVICE_PATH_TYPE_END
) {
2511 EFI_PRINT("Path %pD already installed\n",
2512 protocol_interface
);
2513 r
= EFI_ALREADY_STARTED
;
2517 r
= EFI_CALL(efi_install_protocol_interface(
2519 EFI_NATIVE_INTERFACE
,
2520 protocol_interface
));
2521 if (r
!= EFI_SUCCESS
)
2526 if (r
== EFI_SUCCESS
)
2529 /* If an error occurred undo all changes. */
2530 efi_va_start(argptr
, handle
);
2532 protocol
= efi_va_arg(argptr
, efi_guid_t
*);
2533 protocol_interface
= efi_va_arg(argptr
, void*);
2534 EFI_CALL(efi_uninstall_protocol_interface(*handle
, protocol
,
2535 protocol_interface
));
2543 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2545 * @handle: handle from which the protocol interfaces shall be removed
2546 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2549 * This function implements the UninstallMultipleProtocolInterfaces service.
2551 * See the Unified Extensible Firmware Interface (UEFI) specification for
2554 * Return: status code
2556 static efi_status_t EFIAPI
efi_uninstall_multiple_protocol_interfaces(
2557 efi_handle_t handle
, ...)
2559 EFI_ENTRY("%p", handle
);
2562 const efi_guid_t
*protocol
;
2563 void *protocol_interface
;
2564 efi_status_t r
= EFI_SUCCESS
;
2568 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2570 efi_va_start(argptr
, handle
);
2572 protocol
= efi_va_arg(argptr
, efi_guid_t
*);
2575 protocol_interface
= efi_va_arg(argptr
, void*);
2576 r
= efi_uninstall_protocol(handle
, protocol
,
2577 protocol_interface
);
2578 if (r
!= EFI_SUCCESS
)
2583 if (r
== EFI_SUCCESS
) {
2584 /* If the last protocol has been removed, delete the handle. */
2585 if (list_empty(&handle
->protocols
)) {
2586 list_del(&handle
->link
);
2592 /* If an error occurred undo all changes. */
2593 efi_va_start(argptr
, handle
);
2595 protocol
= efi_va_arg(argptr
, efi_guid_t
*);
2596 protocol_interface
= efi_va_arg(argptr
, void*);
2597 EFI_CALL(efi_install_protocol_interface(&handle
, protocol
,
2598 EFI_NATIVE_INTERFACE
,
2599 protocol_interface
));
2603 /* In case of an error always return EFI_INVALID_PARAMETER */
2604 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2608 * efi_calculate_crc32() - calculate cyclic redundancy code
2609 * @data: buffer with data
2610 * @data_size: size of buffer in bytes
2611 * @crc32_p: cyclic redundancy code
2613 * This function implements the CalculateCrc32 service.
2615 * See the Unified Extensible Firmware Interface (UEFI) specification for
2618 * Return: status code
2620 static efi_status_t EFIAPI
efi_calculate_crc32(const void *data
,
2621 efi_uintn_t data_size
,
2624 efi_status_t ret
= EFI_SUCCESS
;
2626 EFI_ENTRY("%p, %zu", data
, data_size
);
2627 if (!data
|| !data_size
|| !crc32_p
) {
2628 ret
= EFI_INVALID_PARAMETER
;
2631 *crc32_p
= crc32(0, data
, data_size
);
2633 return EFI_EXIT(ret
);
2637 * efi_copy_mem() - copy memory
2638 * @destination: destination of the copy operation
2639 * @source: source of the copy operation
2640 * @length: number of bytes to copy
2642 * This function implements the CopyMem service.
2644 * See the Unified Extensible Firmware Interface (UEFI) specification for
2647 static void EFIAPI
efi_copy_mem(void *destination
, const void *source
,
2650 EFI_ENTRY("%p, %p, %ld", destination
, source
, (unsigned long)length
);
2651 memmove(destination
, source
, length
);
2652 EFI_EXIT(EFI_SUCCESS
);
2656 * efi_set_mem() - Fill memory with a byte value.
2657 * @buffer: buffer to fill
2658 * @size: size of buffer in bytes
2659 * @value: byte to copy to the buffer
2661 * This function implements the SetMem service.
2663 * See the Unified Extensible Firmware Interface (UEFI) specification for
2666 static void EFIAPI
efi_set_mem(void *buffer
, size_t size
, uint8_t value
)
2668 EFI_ENTRY("%p, %ld, 0x%x", buffer
, (unsigned long)size
, value
);
2669 memset(buffer
, value
, size
);
2670 EFI_EXIT(EFI_SUCCESS
);
2674 * efi_protocol_open() - open protocol interface on a handle
2675 * @handler: handler of a protocol
2676 * @protocol_interface: interface implementing the protocol
2677 * @agent_handle: handle of the driver
2678 * @controller_handle: handle of the controller
2679 * @attributes: attributes indicating how to open the protocol
2681 * Return: status code
2683 static efi_status_t
efi_protocol_open(
2684 struct efi_handler
*handler
,
2685 void **protocol_interface
, void *agent_handle
,
2686 void *controller_handle
, uint32_t attributes
)
2688 struct efi_open_protocol_info_item
*item
;
2689 struct efi_open_protocol_info_entry
*match
= NULL
;
2690 bool opened_by_driver
= false;
2691 bool opened_exclusive
= false;
2693 /* If there is no agent, only return the interface */
2697 /* For TEST_PROTOCOL ignore interface attribute */
2698 if (attributes
!= EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)
2699 *protocol_interface
= NULL
;
2702 * Check if the protocol is already opened by a driver with the same
2703 * attributes or opened exclusively
2705 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2706 if (item
->info
.agent_handle
== agent_handle
) {
2707 if ((attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) &&
2708 (item
->info
.attributes
== attributes
))
2709 return EFI_ALREADY_STARTED
;
2711 if (item
->info
.attributes
&
2712 EFI_OPEN_PROTOCOL_BY_DRIVER
)
2713 opened_by_driver
= true;
2715 if (item
->info
.attributes
& EFI_OPEN_PROTOCOL_EXCLUSIVE
)
2716 opened_exclusive
= true;
2719 /* Only one controller can open the protocol exclusively */
2720 if (attributes
& EFI_OPEN_PROTOCOL_EXCLUSIVE
) {
2721 if (opened_exclusive
)
2722 return EFI_ACCESS_DENIED
;
2723 } else if (attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) {
2724 if (opened_exclusive
|| opened_by_driver
)
2725 return EFI_ACCESS_DENIED
;
2728 /* Prepare exclusive opening */
2729 if (attributes
& EFI_OPEN_PROTOCOL_EXCLUSIVE
) {
2730 /* Try to disconnect controllers */
2732 opened_by_driver
= false;
2733 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2736 if (item
->info
.attributes
==
2737 EFI_OPEN_PROTOCOL_BY_DRIVER
) {
2738 ret
= EFI_CALL(efi_disconnect_controller(
2739 item
->info
.controller_handle
,
2740 item
->info
.agent_handle
,
2742 if (ret
== EFI_SUCCESS
)
2744 * Child controllers may have been
2745 * removed from the open_infos list. So
2746 * let's restart the loop.
2748 goto disconnect_next
;
2750 opened_by_driver
= true;
2753 /* Only one driver can be connected */
2754 if (opened_by_driver
)
2755 return EFI_ACCESS_DENIED
;
2758 /* Find existing entry */
2759 list_for_each_entry(item
, &handler
->open_infos
, link
) {
2760 if (item
->info
.agent_handle
== agent_handle
&&
2761 item
->info
.controller_handle
== controller_handle
&&
2762 item
->info
.attributes
== attributes
)
2763 match
= &item
->info
;
2765 /* None found, create one */
2767 match
= efi_create_open_info(handler
);
2769 return EFI_OUT_OF_RESOURCES
;
2772 match
->agent_handle
= agent_handle
;
2773 match
->controller_handle
= controller_handle
;
2774 match
->attributes
= attributes
;
2775 match
->open_count
++;
2778 /* For TEST_PROTOCOL ignore interface attribute. */
2779 if (attributes
!= EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)
2780 *protocol_interface
= handler
->protocol_interface
;
2786 * efi_open_protocol() - open protocol interface on a handle
2787 * @handle: handle on which the protocol shall be opened
2788 * @protocol: GUID of the protocol
2789 * @protocol_interface: interface implementing the protocol
2790 * @agent_handle: handle of the driver
2791 * @controller_handle: handle of the controller
2792 * @attributes: attributes indicating how to open the protocol
2794 * This function implements the OpenProtocol interface.
2796 * See the Unified Extensible Firmware Interface (UEFI) specification for
2799 * Return: status code
2801 static efi_status_t EFIAPI efi_open_protocol
2802 (efi_handle_t handle
, const efi_guid_t
*protocol
,
2803 void **protocol_interface
, efi_handle_t agent_handle
,
2804 efi_handle_t controller_handle
, uint32_t attributes
)
2806 struct efi_handler
*handler
;
2807 efi_status_t r
= EFI_INVALID_PARAMETER
;
2809 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle
, protocol
,
2810 protocol_interface
, agent_handle
, controller_handle
,
2813 if (!handle
|| !protocol
||
2814 (!protocol_interface
&& attributes
!=
2815 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)) {
2819 switch (attributes
) {
2820 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
:
2821 case EFI_OPEN_PROTOCOL_GET_PROTOCOL
:
2822 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL
:
2824 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
:
2825 if (controller_handle
== handle
)
2828 case EFI_OPEN_PROTOCOL_BY_DRIVER
:
2829 case EFI_OPEN_PROTOCOL_BY_DRIVER
| EFI_OPEN_PROTOCOL_EXCLUSIVE
:
2830 /* Check that the controller handle is valid */
2831 if (!efi_search_obj(controller_handle
))
2834 case EFI_OPEN_PROTOCOL_EXCLUSIVE
:
2835 /* Check that the agent handle is valid */
2836 if (!efi_search_obj(agent_handle
))
2843 r
= efi_search_protocol(handle
, protocol
, &handler
);
2848 r
= EFI_UNSUPPORTED
;
2854 r
= efi_protocol_open(handler
, protocol_interface
, agent_handle
,
2855 controller_handle
, attributes
);
2861 * efi_start_image() - call the entry point of an image
2862 * @image_handle: handle of the image
2863 * @exit_data_size: size of the buffer
2864 * @exit_data: buffer to receive the exit data of the called image
2866 * This function implements the StartImage service.
2868 * See the Unified Extensible Firmware Interface (UEFI) specification for
2871 * Return: status code
2873 efi_status_t EFIAPI
efi_start_image(efi_handle_t image_handle
,
2874 efi_uintn_t
*exit_data_size
,
2877 struct efi_loaded_image_obj
*image_obj
=
2878 (struct efi_loaded_image_obj
*)image_handle
;
2881 efi_handle_t parent_image
= current_image
;
2883 EFI_ENTRY("%p, %p, %p", image_handle
, exit_data_size
, exit_data
);
2885 /* Check parameters */
2886 if (image_obj
->header
.type
!= EFI_OBJECT_TYPE_LOADED_IMAGE
)
2887 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2889 ret
= EFI_CALL(efi_open_protocol(image_handle
, &efi_guid_loaded_image
,
2891 EFI_OPEN_PROTOCOL_GET_PROTOCOL
));
2892 if (ret
!= EFI_SUCCESS
)
2893 return EFI_EXIT(EFI_INVALID_PARAMETER
);
2895 image_obj
->exit_data_size
= exit_data_size
;
2896 image_obj
->exit_data
= exit_data
;
2898 /* call the image! */
2899 if (setjmp(&image_obj
->exit_jmp
)) {
2901 * We called the entry point of the child image with EFI_CALL
2902 * in the lines below. The child image called the Exit() boot
2903 * service efi_exit() which executed the long jump that brought
2904 * us to the current line. This implies that the second half
2905 * of the EFI_CALL macro has not been executed.
2909 * efi_exit() called efi_restore_gd(). We have to undo this
2910 * otherwise __efi_entry_check() will put the wrong value into
2916 * To get ready to call EFI_EXIT below we have to execute the
2917 * missed out steps of EFI_CALL.
2919 assert(__efi_entry_check());
2920 EFI_PRINT("%lu returned by started image\n",
2921 (unsigned long)((uintptr_t)image_obj
->exit_status
&
2923 current_image
= parent_image
;
2924 return EFI_EXIT(image_obj
->exit_status
);
2927 current_image
= image_handle
;
2928 image_obj
->header
.type
= EFI_OBJECT_TYPE_STARTED_IMAGE
;
2929 EFI_PRINT("Jumping into 0x%p\n", image_obj
->entry
);
2930 ret
= EFI_CALL(image_obj
->entry(image_handle
, &systab
));
2933 * Usually UEFI applications call Exit() instead of returning.
2934 * But because the world doesn't consist of ponies and unicorns,
2935 * we're happy to emulate that behavior on behalf of a payload
2938 return EFI_CALL(systab
.boottime
->exit(image_handle
, ret
, 0, NULL
));
2942 * efi_delete_image() - delete loaded image from memory)
2944 * @image_obj: handle of the loaded image
2945 * @loaded_image_protocol: loaded image protocol
2947 static efi_status_t efi_delete_image
2948 (struct efi_loaded_image_obj
*image_obj
,
2949 struct efi_loaded_image
*loaded_image_protocol
)
2951 struct efi_object
*efiobj
;
2952 efi_status_t r
, ret
= EFI_SUCCESS
;
2955 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
2956 struct efi_handler
*protocol
;
2958 list_for_each_entry(protocol
, &efiobj
->protocols
, link
) {
2959 struct efi_open_protocol_info_item
*info
;
2961 list_for_each_entry(info
, &protocol
->open_infos
, link
) {
2962 if (info
->info
.agent_handle
!=
2963 (efi_handle_t
)image_obj
)
2965 r
= EFI_CALL(efi_close_protocol
2966 (efiobj
, protocol
->guid
,
2967 info
->info
.agent_handle
,
2968 info
->info
.controller_handle
2970 if (r
!= EFI_SUCCESS
)
2973 * Closing protocols may results in further
2974 * items being deleted. To play it safe loop
2975 * over all elements again.
2982 efi_free_pages((uintptr_t)loaded_image_protocol
->image_base
,
2983 efi_size_in_pages(loaded_image_protocol
->image_size
));
2984 efi_delete_handle(&image_obj
->header
);
2990 * efi_unload_image() - unload an EFI image
2991 * @image_handle: handle of the image to be unloaded
2993 * This function implements the UnloadImage service.
2995 * See the Unified Extensible Firmware Interface (UEFI) specification for
2998 * Return: status code
3000 efi_status_t EFIAPI
efi_unload_image(efi_handle_t image_handle
)
3002 efi_status_t ret
= EFI_SUCCESS
;
3003 struct efi_object
*efiobj
;
3004 struct efi_loaded_image
*loaded_image_protocol
;
3006 EFI_ENTRY("%p", image_handle
);
3008 efiobj
= efi_search_obj(image_handle
);
3010 ret
= EFI_INVALID_PARAMETER
;
3013 /* Find the loaded image protocol */
3014 ret
= EFI_CALL(efi_open_protocol(image_handle
, &efi_guid_loaded_image
,
3015 (void **)&loaded_image_protocol
,
3017 EFI_OPEN_PROTOCOL_GET_PROTOCOL
));
3018 if (ret
!= EFI_SUCCESS
) {
3019 ret
= EFI_INVALID_PARAMETER
;
3022 switch (efiobj
->type
) {
3023 case EFI_OBJECT_TYPE_STARTED_IMAGE
:
3024 /* Call the unload function */
3025 if (!loaded_image_protocol
->unload
) {
3026 ret
= EFI_UNSUPPORTED
;
3029 ret
= EFI_CALL(loaded_image_protocol
->unload(image_handle
));
3030 if (ret
!= EFI_SUCCESS
)
3033 case EFI_OBJECT_TYPE_LOADED_IMAGE
:
3036 ret
= EFI_INVALID_PARAMETER
;
3039 efi_delete_image((struct efi_loaded_image_obj
*)efiobj
,
3040 loaded_image_protocol
);
3042 return EFI_EXIT(ret
);
3046 * efi_update_exit_data() - fill exit data parameters of StartImage()
3048 * @image_obj: image handle
3049 * @exit_data_size: size of the exit data buffer
3050 * @exit_data: buffer with data returned by UEFI payload
3051 * Return: status code
3053 static efi_status_t
efi_update_exit_data(struct efi_loaded_image_obj
*image_obj
,
3054 efi_uintn_t exit_data_size
,
3060 * If exit_data is not provided to StartImage(), exit_data_size must be
3063 if (!image_obj
->exit_data
)
3065 if (image_obj
->exit_data_size
)
3066 *image_obj
->exit_data_size
= exit_data_size
;
3067 if (exit_data_size
&& exit_data
) {
3068 ret
= efi_allocate_pool(EFI_BOOT_SERVICES_DATA
,
3070 (void **)image_obj
->exit_data
);
3071 if (ret
!= EFI_SUCCESS
)
3073 memcpy(*image_obj
->exit_data
, exit_data
, exit_data_size
);
3075 image_obj
->exit_data
= NULL
;
3081 * efi_exit() - leave an EFI application or driver
3082 * @image_handle: handle of the application or driver that is exiting
3083 * @exit_status: status code
3084 * @exit_data_size: size of the buffer in bytes
3085 * @exit_data: buffer with data describing an error
3087 * This function implements the Exit service.
3089 * See the Unified Extensible Firmware Interface (UEFI) specification for
3092 * Return: status code
3094 static efi_status_t EFIAPI
efi_exit(efi_handle_t image_handle
,
3095 efi_status_t exit_status
,
3096 efi_uintn_t exit_data_size
,
3100 * TODO: We should call the unload procedure of the loaded
3104 struct efi_loaded_image
*loaded_image_protocol
;
3105 struct efi_loaded_image_obj
*image_obj
=
3106 (struct efi_loaded_image_obj
*)image_handle
;
3108 EFI_ENTRY("%p, %ld, %zu, %p", image_handle
, exit_status
,
3109 exit_data_size
, exit_data
);
3111 /* Check parameters */
3112 ret
= EFI_CALL(efi_open_protocol(image_handle
, &efi_guid_loaded_image
,
3113 (void **)&loaded_image_protocol
,
3115 EFI_OPEN_PROTOCOL_GET_PROTOCOL
));
3116 if (ret
!= EFI_SUCCESS
) {
3117 ret
= EFI_INVALID_PARAMETER
;
3121 /* Unloading of unstarted images */
3122 switch (image_obj
->header
.type
) {
3123 case EFI_OBJECT_TYPE_STARTED_IMAGE
:
3125 case EFI_OBJECT_TYPE_LOADED_IMAGE
:
3126 efi_delete_image(image_obj
, loaded_image_protocol
);
3130 /* Handle does not refer to loaded image */
3131 ret
= EFI_INVALID_PARAMETER
;
3134 /* A started image can only be unloaded it is the last one started. */
3135 if (image_handle
!= current_image
) {
3136 ret
= EFI_INVALID_PARAMETER
;
3140 /* Exit data is only foreseen in case of failure. */
3141 if (exit_status
!= EFI_SUCCESS
) {
3142 ret
= efi_update_exit_data(image_obj
, exit_data_size
,
3144 /* Exiting has priority. Don't return error to caller. */
3145 if (ret
!= EFI_SUCCESS
)
3146 EFI_PRINT("%s: out of memory\n", __func__
);
3148 if (image_obj
->image_type
== IMAGE_SUBSYSTEM_EFI_APPLICATION
||
3149 exit_status
!= EFI_SUCCESS
)
3150 efi_delete_image(image_obj
, loaded_image_protocol
);
3152 /* Make sure entry/exit counts for EFI world cross-overs match */
3153 EFI_EXIT(exit_status
);
3156 * But longjmp out with the U-Boot gd, not the application's, as
3157 * the other end is a setjmp call inside EFI context.
3161 image_obj
->exit_status
= exit_status
;
3162 longjmp(&image_obj
->exit_jmp
, 1);
3164 panic("EFI application exited");
3166 return EFI_EXIT(ret
);
3170 * efi_handle_protocol() - get interface of a protocol on a handle
3171 * @handle: handle on which the protocol shall be opened
3172 * @protocol: GUID of the protocol
3173 * @protocol_interface: interface implementing the protocol
3175 * This function implements the HandleProtocol service.
3177 * See the Unified Extensible Firmware Interface (UEFI) specification for
3180 * Return: status code
3182 static efi_status_t EFIAPI
efi_handle_protocol(efi_handle_t handle
,
3183 const efi_guid_t
*protocol
,
3184 void **protocol_interface
)
3186 return efi_open_protocol(handle
, protocol
, protocol_interface
, efi_root
,
3187 NULL
, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
);
3191 * efi_bind_controller() - bind a single driver to a controller
3192 * @controller_handle: controller handle
3193 * @driver_image_handle: driver handle
3194 * @remain_device_path: remaining path
3196 * Return: status code
3198 static efi_status_t
efi_bind_controller(
3199 efi_handle_t controller_handle
,
3200 efi_handle_t driver_image_handle
,
3201 struct efi_device_path
*remain_device_path
)
3203 struct efi_driver_binding_protocol
*binding_protocol
;
3206 r
= EFI_CALL(efi_open_protocol(driver_image_handle
,
3207 &efi_guid_driver_binding_protocol
,
3208 (void **)&binding_protocol
,
3209 driver_image_handle
, NULL
,
3210 EFI_OPEN_PROTOCOL_GET_PROTOCOL
));
3211 if (r
!= EFI_SUCCESS
)
3213 r
= EFI_CALL(binding_protocol
->supported(binding_protocol
,
3215 remain_device_path
));
3216 if (r
== EFI_SUCCESS
)
3217 r
= EFI_CALL(binding_protocol
->start(binding_protocol
,
3219 remain_device_path
));
3220 EFI_CALL(efi_close_protocol(driver_image_handle
,
3221 &efi_guid_driver_binding_protocol
,
3222 driver_image_handle
, NULL
));
3227 * efi_connect_single_controller() - connect a single driver to a controller
3228 * @controller_handle: controller
3229 * @driver_image_handle: driver
3230 * @remain_device_path: remaining path
3232 * Return: status code
3234 static efi_status_t
efi_connect_single_controller(
3235 efi_handle_t controller_handle
,
3236 efi_handle_t
*driver_image_handle
,
3237 struct efi_device_path
*remain_device_path
)
3239 efi_handle_t
*buffer
;
3243 size_t connected
= 0;
3245 /* Get buffer with all handles with driver binding protocol */
3246 r
= EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL
,
3247 &efi_guid_driver_binding_protocol
,
3248 NULL
, &count
, &buffer
));
3249 if (r
!= EFI_SUCCESS
)
3252 /* Context Override */
3253 if (driver_image_handle
) {
3254 for (; *driver_image_handle
; ++driver_image_handle
) {
3255 for (i
= 0; i
< count
; ++i
) {
3256 if (buffer
[i
] == *driver_image_handle
) {
3258 r
= efi_bind_controller(
3260 *driver_image_handle
,
3261 remain_device_path
);
3263 * For drivers that do not support the
3264 * controller or are already connected
3265 * we receive an error code here.
3267 if (r
== EFI_SUCCESS
)
3275 * TODO: Some overrides are not yet implemented:
3276 * - Platform Driver Override
3277 * - Driver Family Override Search
3278 * - Bus Specific Driver Override
3281 /* Driver Binding Search */
3282 for (i
= 0; i
< count
; ++i
) {
3284 r
= efi_bind_controller(controller_handle
,
3286 remain_device_path
);
3287 if (r
== EFI_SUCCESS
)
3292 efi_free_pool(buffer
);
3294 return EFI_NOT_FOUND
;
3299 * efi_connect_controller() - connect a controller to a driver
3300 * @controller_handle: handle of the controller
3301 * @driver_image_handle: handle of the driver
3302 * @remain_device_path: device path of a child controller
3303 * @recursive: true to connect all child controllers
3305 * This function implements the ConnectController service.
3307 * See the Unified Extensible Firmware Interface (UEFI) specification for
3310 * First all driver binding protocol handles are tried for binding drivers.
3311 * Afterwards all handles that have opened a protocol of the controller
3312 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3314 * Return: status code
3316 static efi_status_t EFIAPI
efi_connect_controller(
3317 efi_handle_t controller_handle
,
3318 efi_handle_t
*driver_image_handle
,
3319 struct efi_device_path
*remain_device_path
,
3323 efi_status_t ret
= EFI_NOT_FOUND
;
3324 struct efi_object
*efiobj
;
3326 EFI_ENTRY("%p, %p, %pD, %d", controller_handle
, driver_image_handle
,
3327 remain_device_path
, recursive
);
3329 efiobj
= efi_search_obj(controller_handle
);
3331 ret
= EFI_INVALID_PARAMETER
;
3335 r
= efi_connect_single_controller(controller_handle
,
3336 driver_image_handle
,
3337 remain_device_path
);
3338 if (r
== EFI_SUCCESS
)
3341 struct efi_handler
*handler
;
3342 struct efi_open_protocol_info_item
*item
;
3344 list_for_each_entry(handler
, &efiobj
->protocols
, link
) {
3345 list_for_each_entry(item
, &handler
->open_infos
, link
) {
3346 if (item
->info
.attributes
&
3347 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) {
3348 r
= EFI_CALL(efi_connect_controller(
3349 item
->info
.controller_handle
,
3350 driver_image_handle
,
3353 if (r
== EFI_SUCCESS
)
3359 /* Check for child controller specified by end node */
3360 if (ret
!= EFI_SUCCESS
&& remain_device_path
&&
3361 remain_device_path
->type
== DEVICE_PATH_TYPE_END
)
3364 return EFI_EXIT(ret
);
3368 * efi_reinstall_protocol_interface() - reinstall protocol interface
3369 * @handle: handle on which the protocol shall be reinstalled
3370 * @protocol: GUID of the protocol to be installed
3371 * @old_interface: interface to be removed
3372 * @new_interface: interface to be installed
3374 * This function implements the ReinstallProtocolInterface service.
3376 * See the Unified Extensible Firmware Interface (UEFI) specification for
3379 * The old interface is uninstalled. The new interface is installed.
3380 * Drivers are connected.
3382 * Return: status code
3384 static efi_status_t EFIAPI
efi_reinstall_protocol_interface(
3385 efi_handle_t handle
, const efi_guid_t
*protocol
,
3386 void *old_interface
, void *new_interface
)
3390 EFI_ENTRY("%p, %pUl, %p, %p", handle
, protocol
, old_interface
,
3393 /* Uninstall protocol but do not delete handle */
3394 ret
= efi_uninstall_protocol(handle
, protocol
, old_interface
);
3395 if (ret
!= EFI_SUCCESS
)
3398 /* Install the new protocol */
3399 ret
= efi_add_protocol(handle
, protocol
, new_interface
);
3401 * The UEFI spec does not specify what should happen to the handle
3402 * if in case of an error no protocol interface remains on the handle.
3403 * So let's do nothing here.
3405 if (ret
!= EFI_SUCCESS
)
3408 * The returned status code has to be ignored.
3409 * Do not create an error if no suitable driver for the handle exists.
3411 EFI_CALL(efi_connect_controller(handle
, NULL
, NULL
, true));
3413 return EFI_EXIT(ret
);
3417 * efi_get_child_controllers() - get all child controllers associated to a driver
3418 * @efiobj: handle of the controller
3419 * @driver_handle: handle of the driver
3420 * @number_of_children: number of child controllers
3421 * @child_handle_buffer: handles of the the child controllers
3423 * The allocated buffer has to be freed with free().
3425 * Return: status code
3427 static efi_status_t
efi_get_child_controllers(
3428 struct efi_object
*efiobj
,
3429 efi_handle_t driver_handle
,
3430 efi_uintn_t
*number_of_children
,
3431 efi_handle_t
**child_handle_buffer
)
3433 struct efi_handler
*handler
;
3434 struct efi_open_protocol_info_item
*item
;
3435 efi_uintn_t count
= 0, i
;
3438 /* Count all child controller associations */
3439 list_for_each_entry(handler
, &efiobj
->protocols
, link
) {
3440 list_for_each_entry(item
, &handler
->open_infos
, link
) {
3441 if (item
->info
.agent_handle
== driver_handle
&&
3442 item
->info
.attributes
&
3443 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
)
3448 * Create buffer. In case of duplicate child controller assignments
3449 * the buffer will be too large. But that does not harm.
3451 *number_of_children
= 0;
3452 *child_handle_buffer
= calloc(count
, sizeof(efi_handle_t
));
3453 if (!*child_handle_buffer
)
3454 return EFI_OUT_OF_RESOURCES
;
3455 /* Copy unique child handles */
3456 list_for_each_entry(handler
, &efiobj
->protocols
, link
) {
3457 list_for_each_entry(item
, &handler
->open_infos
, link
) {
3458 if (item
->info
.agent_handle
== driver_handle
&&
3459 item
->info
.attributes
&
3460 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
) {
3461 /* Check this is a new child controller */
3463 for (i
= 0; i
< *number_of_children
; ++i
) {
3464 if ((*child_handle_buffer
)[i
] ==
3465 item
->info
.controller_handle
)
3468 /* Copy handle to buffer */
3470 i
= (*number_of_children
)++;
3471 (*child_handle_buffer
)[i
] =
3472 item
->info
.controller_handle
;
3481 * efi_disconnect_controller() - disconnect a controller from a driver
3482 * @controller_handle: handle of the controller
3483 * @driver_image_handle: handle of the driver
3484 * @child_handle: handle of the child to destroy
3486 * This function implements the DisconnectController service.
3488 * See the Unified Extensible Firmware Interface (UEFI) specification for
3491 * Return: status code
3493 static efi_status_t EFIAPI
efi_disconnect_controller(
3494 efi_handle_t controller_handle
,
3495 efi_handle_t driver_image_handle
,
3496 efi_handle_t child_handle
)
3498 struct efi_driver_binding_protocol
*binding_protocol
;
3499 efi_handle_t
*child_handle_buffer
= NULL
;
3500 size_t number_of_children
= 0;
3502 size_t stop_count
= 0;
3503 struct efi_object
*efiobj
;
3505 EFI_ENTRY("%p, %p, %p", controller_handle
, driver_image_handle
,
3508 efiobj
= efi_search_obj(controller_handle
);
3510 r
= EFI_INVALID_PARAMETER
;
3514 if (child_handle
&& !efi_search_obj(child_handle
)) {
3515 r
= EFI_INVALID_PARAMETER
;
3519 /* If no driver handle is supplied, disconnect all drivers */
3520 if (!driver_image_handle
) {
3521 r
= efi_disconnect_all_drivers(efiobj
, NULL
, child_handle
);
3525 /* Create list of child handles */
3527 number_of_children
= 1;
3528 child_handle_buffer
= &child_handle
;
3530 efi_get_child_controllers(efiobj
,
3531 driver_image_handle
,
3532 &number_of_children
,
3533 &child_handle_buffer
);
3536 /* Get the driver binding protocol */
3537 r
= EFI_CALL(efi_open_protocol(driver_image_handle
,
3538 &efi_guid_driver_binding_protocol
,
3539 (void **)&binding_protocol
,
3540 driver_image_handle
, NULL
,
3541 EFI_OPEN_PROTOCOL_GET_PROTOCOL
));
3542 if (r
!= EFI_SUCCESS
)
3544 /* Remove the children */
3545 if (number_of_children
) {
3546 r
= EFI_CALL(binding_protocol
->stop(binding_protocol
,
3549 child_handle_buffer
));
3550 if (r
== EFI_SUCCESS
)
3553 /* Remove the driver */
3555 r
= EFI_CALL(binding_protocol
->stop(binding_protocol
,
3558 if (r
== EFI_SUCCESS
)
3560 EFI_CALL(efi_close_protocol(driver_image_handle
,
3561 &efi_guid_driver_binding_protocol
,
3562 driver_image_handle
, NULL
));
3570 free(child_handle_buffer
);
3574 static struct efi_boot_services efi_boot_services
= {
3576 .signature
= EFI_BOOT_SERVICES_SIGNATURE
,
3577 .revision
= EFI_SPECIFICATION_VERSION
,
3578 .headersize
= sizeof(struct efi_boot_services
),
3580 .raise_tpl
= efi_raise_tpl
,
3581 .restore_tpl
= efi_restore_tpl
,
3582 .allocate_pages
= efi_allocate_pages_ext
,
3583 .free_pages
= efi_free_pages_ext
,
3584 .get_memory_map
= efi_get_memory_map_ext
,
3585 .allocate_pool
= efi_allocate_pool_ext
,
3586 .free_pool
= efi_free_pool_ext
,
3587 .create_event
= efi_create_event_ext
,
3588 .set_timer
= efi_set_timer_ext
,
3589 .wait_for_event
= efi_wait_for_event
,
3590 .signal_event
= efi_signal_event_ext
,
3591 .close_event
= efi_close_event
,
3592 .check_event
= efi_check_event
,
3593 .install_protocol_interface
= efi_install_protocol_interface
,
3594 .reinstall_protocol_interface
= efi_reinstall_protocol_interface
,
3595 .uninstall_protocol_interface
= efi_uninstall_protocol_interface
,
3596 .handle_protocol
= efi_handle_protocol
,
3598 .register_protocol_notify
= efi_register_protocol_notify
,
3599 .locate_handle
= efi_locate_handle_ext
,
3600 .locate_device_path
= efi_locate_device_path
,
3601 .install_configuration_table
= efi_install_configuration_table_ext
,
3602 .load_image
= efi_load_image
,
3603 .start_image
= efi_start_image
,
3605 .unload_image
= efi_unload_image
,
3606 .exit_boot_services
= efi_exit_boot_services
,
3607 .get_next_monotonic_count
= efi_get_next_monotonic_count
,
3609 .set_watchdog_timer
= efi_set_watchdog_timer
,
3610 .connect_controller
= efi_connect_controller
,
3611 .disconnect_controller
= efi_disconnect_controller
,
3612 .open_protocol
= efi_open_protocol
,
3613 .close_protocol
= efi_close_protocol
,
3614 .open_protocol_information
= efi_open_protocol_information
,
3615 .protocols_per_handle
= efi_protocols_per_handle
,
3616 .locate_handle_buffer
= efi_locate_handle_buffer
,
3617 .locate_protocol
= efi_locate_protocol
,
3618 .install_multiple_protocol_interfaces
=
3619 efi_install_multiple_protocol_interfaces
,
3620 .uninstall_multiple_protocol_interfaces
=
3621 efi_uninstall_multiple_protocol_interfaces
,
3622 .calculate_crc32
= efi_calculate_crc32
,
3623 .copy_mem
= efi_copy_mem
,
3624 .set_mem
= efi_set_mem
,
3625 .create_event_ex
= efi_create_event_ex
,
3628 static u16 __efi_runtime_data firmware_vendor
[] = L
"Das U-Boot";
3630 struct efi_system_table __efi_runtime_data systab
= {
3632 .signature
= EFI_SYSTEM_TABLE_SIGNATURE
,
3633 .revision
= EFI_SPECIFICATION_VERSION
,
3634 .headersize
= sizeof(struct efi_system_table
),
3636 .fw_vendor
= firmware_vendor
,
3637 .fw_revision
= FW_VERSION
<< 16 | FW_PATCHLEVEL
<< 8,
3638 .runtime
= &efi_runtime_services
,
3644 * efi_initialize_system_table() - Initialize system table
3646 * Return: status code
3648 efi_status_t
efi_initialize_system_table(void)
3652 /* Allocate configuration table array */
3653 ret
= efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA
,
3654 EFI_MAX_CONFIGURATION_TABLES
*
3655 sizeof(struct efi_configuration_table
),
3656 (void **)&systab
.tables
);
3659 * These entries will be set to NULL in ExitBootServices(). To avoid
3660 * relocation in SetVirtualAddressMap(), set them dynamically.
3662 systab
.con_in
= &efi_con_in
;
3663 systab
.con_out
= &efi_con_out
;
3664 systab
.std_err
= &efi_con_out
;
3665 systab
.boottime
= &efi_boot_services
;
3667 /* Set CRC32 field in table headers */
3668 efi_update_table_header_crc32(&systab
.hdr
);
3669 efi_update_table_header_crc32(&efi_runtime_services
.hdr
);
3670 efi_update_table_header_crc32(&efi_boot_services
.hdr
);