2 * EFI application boot time services
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <efi_loader.h>
12 #include <environment.h>
14 #include <asm/global_data.h>
15 #include <libfdt_env.h>
16 #include <u-boot/crc.h>
21 DECLARE_GLOBAL_DATA_PTR
;
23 /* Task priority level */
24 static efi_uintn_t efi_tpl
= TPL_APPLICATION
;
26 /* This list contains all the EFI objects our payload has access to */
27 LIST_HEAD(efi_obj_list
);
30 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31 * we need to do trickery with caches. Since we don't want to break the EFI
32 * aware boot path, only apply hacks when loading exiting directly (breaking
33 * direct Linux EFI booting along the way - oh well).
35 static bool efi_is_direct_boot
= true;
38 * EFI can pass arbitrary additional "tables" containing vendor specific
39 * information to the payload. One such table is the FDT table which contains
40 * a pointer to a flattened device tree blob.
42 * In most cases we want to pass an FDT to the payload, so reserve one slot of
43 * config table space for it. The pointer gets populated by do_bootefi_exec().
45 static struct efi_configuration_table __efi_runtime_data efi_conf_table
[2];
49 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
50 * fixed when compiling U-Boot. However, the payload does not know about that
51 * restriction so we need to manually swap its and our view of that register on
52 * EFI callback entry/exit.
54 static volatile void *efi_gd
, *app_gd
;
57 static int entry_count
;
58 static int nesting_level
;
60 /* Called on every callback entry */
61 int __efi_entry_check(void)
63 int ret
= entry_count
++ == 0;
72 /* Called on every callback exit */
73 int __efi_exit_check(void)
75 int ret
= --entry_count
== 0;
82 /* Called from do_bootefi_exec() */
83 void efi_save_gd(void)
91 * Special case handler for error/abort that just forces things back
92 * to u-boot world so we can dump out an abort msg, without any care
93 * about returning back to UEFI world.
95 void efi_restore_gd(void)
98 /* Only restore if we're already in EFI context */
106 * Two spaces per indent level, maxing out at 10.. which ought to be
107 * enough for anyone ;-)
109 static const char *indent_string(int level
)
111 const char *indent
= " ";
112 const int max
= strlen(indent
);
113 level
= min(max
, level
* 2);
114 return &indent
[max
- level
];
117 const char *__efi_nesting(void)
119 return indent_string(nesting_level
);
122 const char *__efi_nesting_inc(void)
124 return indent_string(nesting_level
++);
127 const char *__efi_nesting_dec(void)
129 return indent_string(--nesting_level
);
133 * Queue an EFI event.
135 * This function queues the notification function of the event for future
138 * The notification function is called if the task priority level of the
139 * event is higher than the current task priority level.
141 * For the SignalEvent service see efi_signal_event_ext.
143 * @event event to signal
145 void efi_signal_event(struct efi_event
*event
)
147 if (event
->notify_function
) {
148 event
->is_queued
= true;
150 if (efi_tpl
>= event
->notify_tpl
)
152 EFI_CALL_VOID(event
->notify_function(event
,
153 event
->notify_context
));
155 event
->is_queued
= false;
159 * Raise the task priority level.
161 * This function implements the RaiseTpl service.
162 * See the Unified Extensible Firmware Interface (UEFI) specification
165 * @new_tpl new value of the task priority level
166 * @return old value of the task priority level
168 static unsigned long EFIAPI
efi_raise_tpl(efi_uintn_t new_tpl
)
170 efi_uintn_t old_tpl
= efi_tpl
;
172 EFI_ENTRY("0x%zx", new_tpl
);
174 if (new_tpl
< efi_tpl
)
175 debug("WARNING: new_tpl < current_tpl in %s\n", __func__
);
177 if (efi_tpl
> TPL_HIGH_LEVEL
)
178 efi_tpl
= TPL_HIGH_LEVEL
;
180 EFI_EXIT(EFI_SUCCESS
);
185 * Lower the task priority level.
187 * This function implements the RestoreTpl service.
188 * See the Unified Extensible Firmware Interface (UEFI) specification
191 * @old_tpl value of the task priority level to be restored
193 static void EFIAPI
efi_restore_tpl(efi_uintn_t old_tpl
)
195 EFI_ENTRY("0x%zx", old_tpl
);
197 if (old_tpl
> efi_tpl
)
198 debug("WARNING: old_tpl > current_tpl in %s\n", __func__
);
200 if (efi_tpl
> TPL_HIGH_LEVEL
)
201 efi_tpl
= TPL_HIGH_LEVEL
;
203 EFI_EXIT(EFI_SUCCESS
);
207 * Allocate memory pages.
209 * This function implements the AllocatePages service.
210 * See the Unified Extensible Firmware Interface (UEFI) specification
213 * @type type of allocation to be performed
214 * @memory_type usage type of the allocated memory
215 * @pages number of pages to be allocated
216 * @memory allocated memory
217 * @return status code
219 static efi_status_t EFIAPI
efi_allocate_pages_ext(int type
, int memory_type
,
225 EFI_ENTRY("%d, %d, 0x%zx, %p", type
, memory_type
, pages
, memory
);
226 r
= efi_allocate_pages(type
, memory_type
, pages
, memory
);
233 * This function implements the FreePages service.
234 * See the Unified Extensible Firmware Interface (UEFI) specification
237 * @memory start of the memory area to be freed
238 * @pages number of pages to be freed
239 * @return status code
241 static efi_status_t EFIAPI
efi_free_pages_ext(uint64_t memory
,
246 EFI_ENTRY("%"PRIx64
", 0x%zx", memory
, pages
);
247 r
= efi_free_pages(memory
, pages
);
252 * Get map describing memory usage.
254 * This function implements the GetMemoryMap service.
255 * See the Unified Extensible Firmware Interface (UEFI) specification
258 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
259 * on exit the size of the copied memory map
260 * @memory_map buffer to which the memory map is written
261 * @map_key key for the memory map
262 * @descriptor_size size of an individual memory descriptor
263 * @descriptor_version version number of the memory descriptor structure
264 * @return status code
266 static efi_status_t EFIAPI
efi_get_memory_map_ext(
267 efi_uintn_t
*memory_map_size
,
268 struct efi_mem_desc
*memory_map
,
269 efi_uintn_t
*map_key
,
270 efi_uintn_t
*descriptor_size
,
271 uint32_t *descriptor_version
)
275 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size
, memory_map
,
276 map_key
, descriptor_size
, descriptor_version
);
277 r
= efi_get_memory_map(memory_map_size
, memory_map
, map_key
,
278 descriptor_size
, descriptor_version
);
283 * Allocate memory from pool.
285 * This function implements the AllocatePool service.
286 * See the Unified Extensible Firmware Interface (UEFI) specification
289 * @pool_type type of the pool from which memory is to be allocated
290 * @size number of bytes to be allocated
291 * @buffer allocated memory
292 * @return status code
294 static efi_status_t EFIAPI
efi_allocate_pool_ext(int pool_type
,
300 EFI_ENTRY("%d, %zd, %p", pool_type
, size
, buffer
);
301 r
= efi_allocate_pool(pool_type
, size
, buffer
);
306 * Free memory from pool.
308 * This function implements the FreePool service.
309 * See the Unified Extensible Firmware Interface (UEFI) specification
312 * @buffer start of memory to be freed
313 * @return status code
315 static efi_status_t EFIAPI
efi_free_pool_ext(void *buffer
)
319 EFI_ENTRY("%p", buffer
);
320 r
= efi_free_pool(buffer
);
328 * @return status code
330 efi_status_t
efi_create_handle(void **handle
)
332 struct efi_object
*obj
;
335 r
= efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES
,
336 sizeof(struct efi_object
),
338 if (r
!= EFI_SUCCESS
)
340 memset(obj
, 0, sizeof(struct efi_object
));
342 INIT_LIST_HEAD(&obj
->protocols
);
343 list_add_tail(&obj
->link
, &efi_obj_list
);
349 * Our event capabilities are very limited. Only a small limited
350 * number of events is allowed to coexist.
352 static struct efi_event efi_events
[16];
357 * This function is used inside U-Boot code to create an event.
359 * For the API function implementing the CreateEvent service see
360 * efi_create_event_ext.
362 * @type type of the event to create
363 * @notify_tpl task priority level of the event
364 * @notify_function notification function of the event
365 * @notify_context pointer passed to the notification function
366 * @event created event
367 * @return status code
369 efi_status_t
efi_create_event(uint32_t type
, efi_uintn_t notify_tpl
,
370 void (EFIAPI
*notify_function
) (
371 struct efi_event
*event
,
373 void *notify_context
, struct efi_event
**event
)
378 return EFI_INVALID_PARAMETER
;
380 if ((type
& EVT_NOTIFY_SIGNAL
) && (type
& EVT_NOTIFY_WAIT
))
381 return EFI_INVALID_PARAMETER
;
383 if ((type
& (EVT_NOTIFY_SIGNAL
|EVT_NOTIFY_WAIT
)) &&
384 notify_function
== NULL
)
385 return EFI_INVALID_PARAMETER
;
387 for (i
= 0; i
< ARRAY_SIZE(efi_events
); ++i
) {
388 if (efi_events
[i
].type
)
390 efi_events
[i
].type
= type
;
391 efi_events
[i
].notify_tpl
= notify_tpl
;
392 efi_events
[i
].notify_function
= notify_function
;
393 efi_events
[i
].notify_context
= notify_context
;
394 /* Disable timers on bootup */
395 efi_events
[i
].trigger_next
= -1ULL;
396 efi_events
[i
].is_queued
= false;
397 efi_events
[i
].is_signaled
= false;
398 *event
= &efi_events
[i
];
401 return EFI_OUT_OF_RESOURCES
;
407 * This function implements the CreateEvent service.
408 * See the Unified Extensible Firmware Interface (UEFI) specification
411 * @type type of the event to create
412 * @notify_tpl task priority level of the event
413 * @notify_function notification function of the event
414 * @notify_context pointer passed to the notification function
415 * @event created event
416 * @return status code
418 static efi_status_t EFIAPI
efi_create_event_ext(
419 uint32_t type
, efi_uintn_t notify_tpl
,
420 void (EFIAPI
*notify_function
) (
421 struct efi_event
*event
,
423 void *notify_context
, struct efi_event
**event
)
425 EFI_ENTRY("%d, 0x%zx, %p, %p", type
, notify_tpl
, notify_function
,
427 return EFI_EXIT(efi_create_event(type
, notify_tpl
, notify_function
,
428 notify_context
, event
));
433 * Check if a timer event has occurred or a queued notification function should
436 * Our timers have to work without interrupts, so we check whenever keyboard
437 * input or disk accesses happen if enough time elapsed for them to fire.
439 void efi_timer_check(void)
442 u64 now
= timer_get_us();
444 for (i
= 0; i
< ARRAY_SIZE(efi_events
); ++i
) {
445 if (!efi_events
[i
].type
)
447 if (efi_events
[i
].is_queued
)
448 efi_signal_event(&efi_events
[i
]);
449 if (!(efi_events
[i
].type
& EVT_TIMER
) ||
450 now
< efi_events
[i
].trigger_next
)
452 switch (efi_events
[i
].trigger_type
) {
453 case EFI_TIMER_RELATIVE
:
454 efi_events
[i
].trigger_type
= EFI_TIMER_STOP
;
456 case EFI_TIMER_PERIODIC
:
457 efi_events
[i
].trigger_next
+=
458 efi_events
[i
].trigger_time
;
463 efi_events
[i
].is_signaled
= true;
464 efi_signal_event(&efi_events
[i
]);
470 * Set the trigger time for a timer event or stop the event.
472 * This is the function for internal usage in U-Boot. For the API function
473 * implementing the SetTimer service see efi_set_timer_ext.
475 * @event event for which the timer is set
476 * @type type of the timer
477 * @trigger_time trigger period in multiples of 100ns
478 * @return status code
480 efi_status_t
efi_set_timer(struct efi_event
*event
, enum efi_timer_delay type
,
481 uint64_t trigger_time
)
486 * The parameter defines a multiple of 100ns.
487 * We use multiples of 1000ns. So divide by 10.
489 do_div(trigger_time
, 10);
491 for (i
= 0; i
< ARRAY_SIZE(efi_events
); ++i
) {
492 if (event
!= &efi_events
[i
])
495 if (!(event
->type
& EVT_TIMER
))
499 event
->trigger_next
= -1ULL;
501 case EFI_TIMER_PERIODIC
:
502 case EFI_TIMER_RELATIVE
:
503 event
->trigger_next
=
504 timer_get_us() + trigger_time
;
507 return EFI_INVALID_PARAMETER
;
509 event
->trigger_type
= type
;
510 event
->trigger_time
= trigger_time
;
511 event
->is_signaled
= false;
514 return EFI_INVALID_PARAMETER
;
518 * Set the trigger time for a timer event or stop the event.
520 * This function implements the SetTimer service.
521 * See the Unified Extensible Firmware Interface (UEFI) specification
524 * @event event for which the timer is set
525 * @type type of the timer
526 * @trigger_time trigger period in multiples of 100ns
527 * @return status code
529 static efi_status_t EFIAPI
efi_set_timer_ext(struct efi_event
*event
,
530 enum efi_timer_delay type
,
531 uint64_t trigger_time
)
533 EFI_ENTRY("%p, %d, %"PRIx64
, event
, type
, trigger_time
);
534 return EFI_EXIT(efi_set_timer(event
, type
, trigger_time
));
538 * Wait for events to be signaled.
540 * This function implements the WaitForEvent service.
541 * See the Unified Extensible Firmware Interface (UEFI) specification
544 * @num_events number of events to be waited for
545 * @events events to be waited for
546 * @index index of the event that was signaled
547 * @return status code
549 static efi_status_t EFIAPI
efi_wait_for_event(efi_uintn_t num_events
,
550 struct efi_event
**event
,
555 EFI_ENTRY("%zd, %p, %p", num_events
, event
, index
);
557 /* Check parameters */
558 if (!num_events
|| !event
)
559 return EFI_EXIT(EFI_INVALID_PARAMETER
);
561 if (efi_tpl
!= TPL_APPLICATION
)
562 return EFI_EXIT(EFI_UNSUPPORTED
);
563 for (i
= 0; i
< num_events
; ++i
) {
564 for (j
= 0; j
< ARRAY_SIZE(efi_events
); ++j
) {
565 if (event
[i
] == &efi_events
[j
])
568 return EFI_EXIT(EFI_INVALID_PARAMETER
);
570 if (!event
[i
]->type
|| event
[i
]->type
& EVT_NOTIFY_SIGNAL
)
571 return EFI_EXIT(EFI_INVALID_PARAMETER
);
572 if (!event
[i
]->is_signaled
)
573 efi_signal_event(event
[i
]);
576 /* Wait for signal */
578 for (i
= 0; i
< num_events
; ++i
) {
579 if (event
[i
]->is_signaled
)
582 /* Allow events to occur. */
588 * Reset the signal which is passed to the caller to allow periodic
591 event
[i
]->is_signaled
= false;
595 return EFI_EXIT(EFI_SUCCESS
);
599 * Signal an EFI event.
601 * This function implements the SignalEvent service.
602 * See the Unified Extensible Firmware Interface (UEFI) specification
605 * This functions sets the signaled state of the event and queues the
606 * notification function for execution.
608 * @event event to signal
609 * @return status code
611 static efi_status_t EFIAPI
efi_signal_event_ext(struct efi_event
*event
)
615 EFI_ENTRY("%p", event
);
616 for (i
= 0; i
< ARRAY_SIZE(efi_events
); ++i
) {
617 if (event
!= &efi_events
[i
])
619 if (event
->is_signaled
)
621 event
->is_signaled
= true;
622 if (event
->type
& EVT_NOTIFY_SIGNAL
)
623 efi_signal_event(event
);
626 return EFI_EXIT(EFI_SUCCESS
);
630 * Close an EFI event.
632 * This function implements the CloseEvent service.
633 * See the Unified Extensible Firmware Interface (UEFI) specification
636 * @event event to close
637 * @return status code
639 static efi_status_t EFIAPI
efi_close_event(struct efi_event
*event
)
643 EFI_ENTRY("%p", event
);
644 for (i
= 0; i
< ARRAY_SIZE(efi_events
); ++i
) {
645 if (event
== &efi_events
[i
]) {
647 event
->trigger_next
= -1ULL;
648 event
->is_queued
= false;
649 event
->is_signaled
= false;
650 return EFI_EXIT(EFI_SUCCESS
);
653 return EFI_EXIT(EFI_INVALID_PARAMETER
);
657 * Check if an event is signaled.
659 * This function implements the CheckEvent service.
660 * See the Unified Extensible Firmware Interface (UEFI) specification
663 * If an event is not signaled yet the notification function is queued.
665 * @event event to check
666 * @return status code
668 static efi_status_t EFIAPI
efi_check_event(struct efi_event
*event
)
672 EFI_ENTRY("%p", event
);
674 for (i
= 0; i
< ARRAY_SIZE(efi_events
); ++i
) {
675 if (event
!= &efi_events
[i
])
677 if (!event
->type
|| event
->type
& EVT_NOTIFY_SIGNAL
)
679 if (!event
->is_signaled
)
680 efi_signal_event(event
);
681 if (event
->is_signaled
)
682 return EFI_EXIT(EFI_SUCCESS
);
683 return EFI_EXIT(EFI_NOT_READY
);
685 return EFI_EXIT(EFI_INVALID_PARAMETER
);
689 * Find the internal EFI object for a handle.
691 * @handle handle to find
694 struct efi_object
*efi_search_obj(const void *handle
)
696 struct efi_object
*efiobj
;
698 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
699 if (efiobj
->handle
== handle
)
707 * Find a protocol on a handle.
710 * @protocol_guid GUID of the protocol
711 * @handler reference to the protocol
712 * @return status code
714 efi_status_t
efi_search_protocol(const void *handle
,
715 const efi_guid_t
*protocol_guid
,
716 struct efi_handler
**handler
)
718 struct efi_object
*efiobj
;
719 struct list_head
*lhandle
;
721 if (!handle
|| !protocol_guid
)
722 return EFI_INVALID_PARAMETER
;
723 efiobj
= efi_search_obj(handle
);
725 return EFI_INVALID_PARAMETER
;
726 list_for_each(lhandle
, &efiobj
->protocols
) {
727 struct efi_handler
*protocol
;
729 protocol
= list_entry(lhandle
, struct efi_handler
, link
);
730 if (!guidcmp(protocol
->guid
, protocol_guid
)) {
736 return EFI_NOT_FOUND
;
740 * Install new protocol on a handle.
742 * @handle handle on which the protocol shall be installed
743 * @protocol GUID of the protocol to be installed
744 * @protocol_interface interface of the protocol implementation
745 * @return status code
747 efi_status_t
efi_add_protocol(const void *handle
, const efi_guid_t
*protocol
,
748 void *protocol_interface
)
750 struct efi_object
*efiobj
;
751 struct efi_handler
*handler
;
754 efiobj
= efi_search_obj(handle
);
756 return EFI_INVALID_PARAMETER
;
757 ret
= efi_search_protocol(handle
, protocol
, NULL
);
758 if (ret
!= EFI_NOT_FOUND
)
759 return EFI_INVALID_PARAMETER
;
760 handler
= calloc(1, sizeof(struct efi_handler
));
762 return EFI_OUT_OF_RESOURCES
;
763 handler
->guid
= protocol
;
764 handler
->protocol_interface
= protocol_interface
;
765 list_add_tail(&handler
->link
, &efiobj
->protocols
);
770 * Delete protocol from a handle.
772 * @handle handle from which the protocol shall be deleted
773 * @protocol GUID of the protocol to be deleted
774 * @protocol_interface interface of the protocol implementation
775 * @return status code
777 efi_status_t
efi_remove_protocol(const void *handle
, const efi_guid_t
*protocol
,
778 void *protocol_interface
)
780 struct efi_handler
*handler
;
783 ret
= efi_search_protocol(handle
, protocol
, &handler
);
784 if (ret
!= EFI_SUCCESS
)
786 if (guidcmp(handler
->guid
, protocol
))
787 return EFI_INVALID_PARAMETER
;
788 list_del(&handler
->link
);
794 * Delete all protocols from a handle.
796 * @handle handle from which the protocols shall be deleted
797 * @return status code
799 efi_status_t
efi_remove_all_protocols(const void *handle
)
801 struct efi_object
*efiobj
;
802 struct list_head
*lhandle
;
803 struct list_head
*pos
;
805 efiobj
= efi_search_obj(handle
);
807 return EFI_INVALID_PARAMETER
;
808 list_for_each_safe(lhandle
, pos
, &efiobj
->protocols
) {
809 struct efi_handler
*protocol
;
812 protocol
= list_entry(lhandle
, struct efi_handler
, link
);
814 ret
= efi_remove_protocol(handle
, protocol
->guid
,
815 protocol
->protocol_interface
);
816 if (ret
!= EFI_SUCCESS
)
823 * Install protocol interface.
825 * This function implements the InstallProtocolInterface service.
826 * See the Unified Extensible Firmware Interface (UEFI) specification
829 * @handle handle on which the protocol shall be installed
830 * @protocol GUID of the protocol to be installed
831 * @protocol_interface_type type of the interface to be installed,
832 * always EFI_NATIVE_INTERFACE
833 * @protocol_interface interface of the protocol implementation
834 * @return status code
836 static efi_status_t EFIAPI
efi_install_protocol_interface(
837 void **handle
, const efi_guid_t
*protocol
,
838 int protocol_interface_type
, void *protocol_interface
)
842 EFI_ENTRY("%p, %pUl, %d, %p", handle
, protocol
, protocol_interface_type
,
845 if (!handle
|| !protocol
||
846 protocol_interface_type
!= EFI_NATIVE_INTERFACE
) {
847 r
= EFI_INVALID_PARAMETER
;
851 /* Create new handle if requested. */
853 r
= efi_create_handle(handle
);
854 if (r
!= EFI_SUCCESS
)
856 debug("%sEFI: new handle %p\n", indent_string(nesting_level
),
859 debug("%sEFI: handle %p\n", indent_string(nesting_level
),
862 /* Add new protocol */
863 r
= efi_add_protocol(*handle
, protocol
, protocol_interface
);
869 * Reinstall protocol interface.
871 * This function implements the ReinstallProtocolInterface service.
872 * See the Unified Extensible Firmware Interface (UEFI) specification
875 * @handle handle on which the protocol shall be
877 * @protocol GUID of the protocol to be installed
878 * @old_interface interface to be removed
879 * @new_interface interface to be installed
880 * @return status code
882 static efi_status_t EFIAPI
efi_reinstall_protocol_interface(void *handle
,
883 const efi_guid_t
*protocol
, void *old_interface
,
886 EFI_ENTRY("%p, %pUl, %p, %p", handle
, protocol
, old_interface
,
888 return EFI_EXIT(EFI_ACCESS_DENIED
);
892 * Uninstall protocol interface.
894 * This function implements the UninstallProtocolInterface service.
895 * See the Unified Extensible Firmware Interface (UEFI) specification
898 * @handle handle from which the protocol shall be removed
899 * @protocol GUID of the protocol to be removed
900 * @protocol_interface interface to be removed
901 * @return status code
903 static efi_status_t EFIAPI
efi_uninstall_protocol_interface(
904 void *handle
, const efi_guid_t
*protocol
,
905 void *protocol_interface
)
907 struct efi_handler
*handler
;
910 EFI_ENTRY("%p, %pUl, %p", handle
, protocol
, protocol_interface
);
912 if (!handle
|| !protocol
) {
913 r
= EFI_INVALID_PARAMETER
;
917 /* Find the protocol on the handle */
918 r
= efi_search_protocol(handle
, protocol
, &handler
);
919 if (r
!= EFI_SUCCESS
)
921 if (handler
->protocol_interface
) {
922 /* TODO disconnect controllers */
923 r
= EFI_ACCESS_DENIED
;
925 r
= efi_remove_protocol(handle
, protocol
, protocol_interface
);
932 * Register an event for notification when a protocol is installed.
934 * This function implements the RegisterProtocolNotify service.
935 * See the Unified Extensible Firmware Interface (UEFI) specification
938 * @protocol GUID of the protocol whose installation shall be
940 * @event event to be signaled upon installation of the protocol
941 * @registration key for retrieving the registration information
942 * @return status code
944 static efi_status_t EFIAPI
efi_register_protocol_notify(
945 const efi_guid_t
*protocol
,
946 struct efi_event
*event
,
949 EFI_ENTRY("%pUl, %p, %p", protocol
, event
, registration
);
950 return EFI_EXIT(EFI_OUT_OF_RESOURCES
);
954 * Determine if an EFI handle implements a protocol.
956 * See the documentation of the LocateHandle service in the UEFI specification.
958 * @search_type selection criterion
959 * @protocol GUID of the protocol
960 * @search_key registration key
962 * @return 0 if the handle implements the protocol
964 static int efi_search(enum efi_locate_search_type search_type
,
965 const efi_guid_t
*protocol
, void *search_key
,
966 struct efi_object
*efiobj
)
970 switch (search_type
) {
973 case BY_REGISTER_NOTIFY
:
974 /* TODO: RegisterProtocolNotify is not implemented yet */
977 ret
= efi_search_protocol(efiobj
->handle
, protocol
, NULL
);
978 return (ret
!= EFI_SUCCESS
);
980 /* Invalid search type */
986 * Locate handles implementing a protocol.
988 * This function is meant for U-Boot internal calls. For the API implementation
989 * of the LocateHandle service see efi_locate_handle_ext.
991 * @search_type selection criterion
992 * @protocol GUID of the protocol
993 * @search_key registration key
994 * @buffer_size size of the buffer to receive the handles in bytes
995 * @buffer buffer to receive the relevant handles
996 * @return status code
998 static efi_status_t
efi_locate_handle(
999 enum efi_locate_search_type search_type
,
1000 const efi_guid_t
*protocol
, void *search_key
,
1001 efi_uintn_t
*buffer_size
, efi_handle_t
*buffer
)
1003 struct efi_object
*efiobj
;
1004 efi_uintn_t size
= 0;
1006 /* Check parameters */
1007 switch (search_type
) {
1010 case BY_REGISTER_NOTIFY
:
1012 return EFI_INVALID_PARAMETER
;
1013 /* RegisterProtocolNotify is not implemented yet */
1014 return EFI_UNSUPPORTED
;
1017 return EFI_INVALID_PARAMETER
;
1020 return EFI_INVALID_PARAMETER
;
1024 * efi_locate_handle_buffer uses this function for
1025 * the calculation of the necessary buffer size.
1026 * So do not require a buffer for buffersize == 0.
1028 if (!buffer_size
|| (*buffer_size
&& !buffer
))
1029 return EFI_INVALID_PARAMETER
;
1031 /* Count how much space we need */
1032 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
1033 if (!efi_search(search_type
, protocol
, search_key
, efiobj
))
1034 size
+= sizeof(void*);
1037 if (*buffer_size
< size
) {
1038 *buffer_size
= size
;
1039 return EFI_BUFFER_TOO_SMALL
;
1042 *buffer_size
= size
;
1044 return EFI_NOT_FOUND
;
1046 /* Then fill the array */
1047 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
1048 if (!efi_search(search_type
, protocol
, search_key
, efiobj
))
1049 *buffer
++ = efiobj
->handle
;
1056 * Locate handles implementing a protocol.
1058 * This function implements the LocateHandle service.
1059 * See the Unified Extensible Firmware Interface (UEFI) specification
1062 * @search_type selection criterion
1063 * @protocol GUID of the protocol
1064 * @search_key registration key
1065 * @buffer_size size of the buffer to receive the handles in bytes
1066 * @buffer buffer to receive the relevant handles
1067 * @return 0 if the handle implements the protocol
1069 static efi_status_t EFIAPI
efi_locate_handle_ext(
1070 enum efi_locate_search_type search_type
,
1071 const efi_guid_t
*protocol
, void *search_key
,
1072 efi_uintn_t
*buffer_size
, efi_handle_t
*buffer
)
1074 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type
, protocol
, search_key
,
1075 buffer_size
, buffer
);
1077 return EFI_EXIT(efi_locate_handle(search_type
, protocol
, search_key
,
1078 buffer_size
, buffer
));
1081 /* Collapses configuration table entries, removing index i */
1082 static void efi_remove_configuration_table(int i
)
1084 struct efi_configuration_table
*this = &efi_conf_table
[i
];
1085 struct efi_configuration_table
*next
= &efi_conf_table
[i
+1];
1086 struct efi_configuration_table
*end
= &efi_conf_table
[systab
.nr_tables
];
1088 memmove(this, next
, (ulong
)end
- (ulong
)next
);
1093 * Adds, updates, or removes a configuration table.
1095 * This function is used for internal calls. For the API implementation of the
1096 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1098 * @guid GUID of the installed table
1099 * @table table to be installed
1100 * @return status code
1102 efi_status_t
efi_install_configuration_table(const efi_guid_t
*guid
, void *table
)
1106 /* Check for guid override */
1107 for (i
= 0; i
< systab
.nr_tables
; i
++) {
1108 if (!guidcmp(guid
, &efi_conf_table
[i
].guid
)) {
1110 efi_conf_table
[i
].table
= table
;
1112 efi_remove_configuration_table(i
);
1118 return EFI_NOT_FOUND
;
1120 /* No override, check for overflow */
1121 if (i
>= ARRAY_SIZE(efi_conf_table
))
1122 return EFI_OUT_OF_RESOURCES
;
1124 /* Add a new entry */
1125 memcpy(&efi_conf_table
[i
].guid
, guid
, sizeof(*guid
));
1126 efi_conf_table
[i
].table
= table
;
1127 systab
.nr_tables
= i
+ 1;
1133 * Adds, updates, or removes a configuration table.
1135 * This function implements the InstallConfigurationTable service.
1136 * See the Unified Extensible Firmware Interface (UEFI) specification
1139 * @guid GUID of the installed table
1140 * @table table to be installed
1141 * @return status code
1143 static efi_status_t EFIAPI
efi_install_configuration_table_ext(efi_guid_t
*guid
,
1146 EFI_ENTRY("%pUl, %p", guid
, table
);
1147 return EFI_EXIT(efi_install_configuration_table(guid
, table
));
1151 * Initialize a loaded_image_info + loaded_image_info object with correct
1152 * protocols, boot-device, etc.
1154 * @info loaded image info to be passed to the entry point of the
1156 * @obj internal object associated with the loaded image
1157 * @device_path device path of the loaded image
1158 * @file_path file path of the loaded image
1160 void efi_setup_loaded_image(struct efi_loaded_image
*info
, struct efi_object
*obj
,
1161 struct efi_device_path
*device_path
,
1162 struct efi_device_path
*file_path
)
1168 info
->file_path
= file_path
;
1170 info
->device_handle
= efi_dp_find_obj(device_path
, NULL
);
1172 INIT_LIST_HEAD(&obj
->protocols
);
1173 list_add_tail(&obj
->link
, &efi_obj_list
);
1175 * When asking for the device path interface, return
1176 * bootefi_device_path
1178 ret
= efi_add_protocol(obj
->handle
, &efi_guid_device_path
, device_path
);
1179 if (ret
!= EFI_SUCCESS
)
1183 * When asking for the loaded_image interface, just
1184 * return handle which points to loaded_image_info
1186 ret
= efi_add_protocol(obj
->handle
, &efi_guid_loaded_image
, info
);
1187 if (ret
!= EFI_SUCCESS
)
1190 ret
= efi_add_protocol(obj
->handle
, &efi_guid_console_control
,
1191 (void *)&efi_console_control
);
1192 if (ret
!= EFI_SUCCESS
)
1195 ret
= efi_add_protocol(obj
->handle
,
1196 &efi_guid_device_path_to_text_protocol
,
1197 (void *)&efi_device_path_to_text
);
1198 if (ret
!= EFI_SUCCESS
)
1203 printf("ERROR: Failure to install protocols for loaded image\n");
1207 * Load an image using a file path.
1209 * @file_path the path of the image to load
1210 * @buffer buffer containing the loaded image
1211 * @return status code
1213 efi_status_t
efi_load_image_from_path(struct efi_device_path
*file_path
,
1216 struct efi_file_info
*info
= NULL
;
1217 struct efi_file_handle
*f
;
1218 static efi_status_t ret
;
1221 f
= efi_file_from_path(file_path
);
1223 return EFI_DEVICE_ERROR
;
1226 EFI_CALL(ret
= f
->getinfo(f
, (efi_guid_t
*)&efi_file_info_guid
,
1228 if (ret
== EFI_BUFFER_TOO_SMALL
) {
1230 EFI_CALL(ret
= f
->getinfo(f
, (efi_guid_t
*)&efi_file_info_guid
,
1233 if (ret
!= EFI_SUCCESS
)
1236 ret
= efi_allocate_pool(EFI_LOADER_DATA
, info
->file_size
, buffer
);
1240 EFI_CALL(ret
= f
->read(f
, &info
->file_size
, *buffer
));
1244 EFI_CALL(f
->close(f
));
1246 if (ret
!= EFI_SUCCESS
) {
1247 efi_free_pool(*buffer
);
1255 * Load an EFI image into memory.
1257 * This function implements the LoadImage service.
1258 * See the Unified Extensible Firmware Interface (UEFI) specification
1261 * @boot_policy true for request originating from the boot manager
1262 * @parent_image the calles's image handle
1263 * @file_path the path of the image to load
1264 * @source_buffer memory location from which the image is installed
1265 * @source_size size of the memory area from which the image is
1267 * @image_handle handle for the newly installed image
1268 * @return status code
1270 static efi_status_t EFIAPI
efi_load_image(bool boot_policy
,
1271 efi_handle_t parent_image
,
1272 struct efi_device_path
*file_path
,
1273 void *source_buffer
,
1274 unsigned long source_size
,
1275 efi_handle_t
*image_handle
)
1277 struct efi_loaded_image
*info
;
1278 struct efi_object
*obj
;
1280 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy
, parent_image
,
1281 file_path
, source_buffer
, source_size
, image_handle
);
1283 info
= calloc(1, sizeof(*info
));
1284 obj
= calloc(1, sizeof(*obj
));
1286 if (!source_buffer
) {
1287 struct efi_device_path
*dp
, *fp
;
1290 ret
= efi_load_image_from_path(file_path
, &source_buffer
);
1291 if (ret
!= EFI_SUCCESS
) {
1294 return EFI_EXIT(ret
);
1298 * split file_path which contains both the device and
1301 efi_dp_split_file_path(file_path
, &dp
, &fp
);
1303 efi_setup_loaded_image(info
, obj
, dp
, fp
);
1305 /* In this case, file_path is the "device" path, ie.
1306 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1308 efi_setup_loaded_image(info
, obj
, file_path
, NULL
);
1311 info
->reserved
= efi_load_pe(source_buffer
, info
);
1312 if (!info
->reserved
) {
1315 return EFI_EXIT(EFI_UNSUPPORTED
);
1318 info
->system_table
= &systab
;
1319 info
->parent_handle
= parent_image
;
1320 *image_handle
= info
;
1322 return EFI_EXIT(EFI_SUCCESS
);
1326 * Call the entry point of an image.
1328 * This function implements the StartImage service.
1329 * See the Unified Extensible Firmware Interface (UEFI) specification
1332 * @image_handle handle of the image
1333 * @exit_data_size size of the buffer
1334 * @exit_data buffer to receive the exit data of the called image
1335 * @return status code
1337 static efi_status_t EFIAPI
efi_start_image(efi_handle_t image_handle
,
1338 unsigned long *exit_data_size
,
1341 ulong (*entry
)(void *image_handle
, struct efi_system_table
*st
);
1342 struct efi_loaded_image
*info
= image_handle
;
1344 EFI_ENTRY("%p, %p, %p", image_handle
, exit_data_size
, exit_data
);
1345 entry
= info
->reserved
;
1347 efi_is_direct_boot
= false;
1349 /* call the image! */
1350 if (setjmp(&info
->exit_jmp
)) {
1351 /* We returned from the child image */
1352 return EFI_EXIT(info
->exit_status
);
1355 __efi_nesting_dec();
1357 entry(image_handle
, &systab
);
1358 __efi_entry_check();
1359 __efi_nesting_inc();
1361 /* Should usually never get here */
1362 return EFI_EXIT(EFI_SUCCESS
);
1366 * Leave an EFI application or driver.
1368 * This function implements the Exit service.
1369 * See the Unified Extensible Firmware Interface (UEFI) specification
1372 * @image_handle handle of the application or driver that is exiting
1373 * @exit_status status code
1374 * @exit_data_size size of the buffer in bytes
1375 * @exit_data buffer with data describing an error
1376 * @return status code
1378 static efi_status_t EFIAPI
efi_exit(efi_handle_t image_handle
,
1379 efi_status_t exit_status
, unsigned long exit_data_size
,
1382 struct efi_loaded_image
*loaded_image_info
= (void*)image_handle
;
1384 EFI_ENTRY("%p, %ld, %ld, %p", image_handle
, exit_status
,
1385 exit_data_size
, exit_data
);
1387 /* Make sure entry/exit counts for EFI world cross-overs match */
1391 * But longjmp out with the U-Boot gd, not the application's, as
1392 * the other end is a setjmp call inside EFI context.
1396 loaded_image_info
->exit_status
= exit_status
;
1397 longjmp(&loaded_image_info
->exit_jmp
, 1);
1399 panic("EFI application exited");
1403 * Unload an EFI image.
1405 * This function implements the UnloadImage service.
1406 * See the Unified Extensible Firmware Interface (UEFI) specification
1409 * @image_handle handle of the image to be unloaded
1410 * @return status code
1412 static efi_status_t EFIAPI
efi_unload_image(void *image_handle
)
1414 struct efi_object
*efiobj
;
1416 EFI_ENTRY("%p", image_handle
);
1417 efiobj
= efi_search_obj(image_handle
);
1419 list_del(&efiobj
->link
);
1421 return EFI_EXIT(EFI_SUCCESS
);
1425 * Fix up caches for EFI payloads if necessary.
1427 static void efi_exit_caches(void)
1429 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1431 * Grub on 32bit ARM needs to have caches disabled before jumping into
1432 * a zImage, but does not know of all cache layers. Give it a hand.
1434 if (efi_is_direct_boot
)
1435 cleanup_before_linux();
1440 * Stop boot services.
1442 * This function implements the ExitBootServices service.
1443 * See the Unified Extensible Firmware Interface (UEFI) specification
1446 * @image_handle handle of the loaded image
1447 * @map_key key of the memory map
1448 * @return status code
1450 static efi_status_t EFIAPI
efi_exit_boot_services(void *image_handle
,
1451 unsigned long map_key
)
1455 EFI_ENTRY("%p, %ld", image_handle
, map_key
);
1457 /* Notify that ExitBootServices is invoked. */
1458 for (i
= 0; i
< ARRAY_SIZE(efi_events
); ++i
) {
1459 if (efi_events
[i
].type
!= EVT_SIGNAL_EXIT_BOOT_SERVICES
)
1461 efi_signal_event(&efi_events
[i
]);
1463 /* Make sure that notification functions are not called anymore */
1464 efi_tpl
= TPL_HIGH_LEVEL
;
1466 /* XXX Should persist EFI variables here */
1468 board_quiesce_devices();
1470 /* Fix up caches for EFI payloads if necessary */
1473 /* This stops all lingering devices */
1474 bootm_disable_interrupts();
1476 /* Give the payload some time to boot */
1477 efi_set_watchdog(0);
1480 return EFI_EXIT(EFI_SUCCESS
);
1484 * Get next value of the counter.
1486 * This function implements the NextMonotonicCount service.
1487 * See the Unified Extensible Firmware Interface (UEFI) specification
1490 * @count returned value of the counter
1491 * @return status code
1493 static efi_status_t EFIAPI
efi_get_next_monotonic_count(uint64_t *count
)
1495 static uint64_t mono
= 0;
1496 EFI_ENTRY("%p", count
);
1498 return EFI_EXIT(EFI_SUCCESS
);
1504 * This function implements the Stall sercive.
1505 * See the Unified Extensible Firmware Interface (UEFI) specification
1508 * @microseconds period to sleep in microseconds
1509 * @return status code
1511 static efi_status_t EFIAPI
efi_stall(unsigned long microseconds
)
1513 EFI_ENTRY("%ld", microseconds
);
1514 udelay(microseconds
);
1515 return EFI_EXIT(EFI_SUCCESS
);
1519 * Reset the watchdog timer.
1521 * This function implements the SetWatchdogTimer service.
1522 * See the Unified Extensible Firmware Interface (UEFI) specification
1525 * @timeout seconds before reset by watchdog
1526 * @watchdog_code code to be logged when resetting
1527 * @data_size size of buffer in bytes
1528 * @watchdog_data buffer with data describing the reset reason
1529 * @return status code
1531 static efi_status_t EFIAPI
efi_set_watchdog_timer(unsigned long timeout
,
1532 uint64_t watchdog_code
,
1533 unsigned long data_size
,
1534 uint16_t *watchdog_data
)
1536 EFI_ENTRY("%ld, 0x%"PRIx64
", %ld, %p", timeout
, watchdog_code
,
1537 data_size
, watchdog_data
);
1538 return EFI_EXIT(efi_set_watchdog(timeout
));
1542 * Connect a controller to a driver.
1544 * This function implements the ConnectController service.
1545 * See the Unified Extensible Firmware Interface (UEFI) specification
1548 * @controller_handle handle of the controller
1549 * @driver_image_handle handle of the driver
1550 * @remain_device_path device path of a child controller
1551 * @recursive true to connect all child controllers
1552 * @return status code
1554 static efi_status_t EFIAPI
efi_connect_controller(
1555 efi_handle_t controller_handle
,
1556 efi_handle_t
*driver_image_handle
,
1557 struct efi_device_path
*remain_device_path
,
1560 EFI_ENTRY("%p, %p, %p, %d", controller_handle
, driver_image_handle
,
1561 remain_device_path
, recursive
);
1562 return EFI_EXIT(EFI_NOT_FOUND
);
1566 * Disconnect a controller from a driver.
1568 * This function implements the DisconnectController service.
1569 * See the Unified Extensible Firmware Interface (UEFI) specification
1572 * @controller_handle handle of the controller
1573 * @driver_image_handle handle of the driver
1574 * @child_handle handle of the child to destroy
1575 * @return status code
1577 static efi_status_t EFIAPI
efi_disconnect_controller(void *controller_handle
,
1578 void *driver_image_handle
,
1581 EFI_ENTRY("%p, %p, %p", controller_handle
, driver_image_handle
,
1583 return EFI_EXIT(EFI_INVALID_PARAMETER
);
1589 * This function implements the CloseProtocol service.
1590 * See the Unified Extensible Firmware Interface (UEFI) specification
1593 * @handle handle on which the protocol shall be closed
1594 * @protocol GUID of the protocol to close
1595 * @agent_handle handle of the driver
1596 * @controller_handle handle of the controller
1597 * @return status code
1599 static efi_status_t EFIAPI
efi_close_protocol(void *handle
,
1600 const efi_guid_t
*protocol
,
1602 void *controller_handle
)
1604 EFI_ENTRY("%p, %pUl, %p, %p", handle
, protocol
, agent_handle
,
1606 return EFI_EXIT(EFI_NOT_FOUND
);
1610 * Provide information about then open status of a protocol on a handle
1612 * This function implements the OpenProtocolInformation service.
1613 * See the Unified Extensible Firmware Interface (UEFI) specification
1616 * @handle handle for which the information shall be retrieved
1617 * @protocol GUID of the protocol
1618 * @entry_buffer buffer to receive the open protocol information
1619 * @entry_count number of entries available in the buffer
1620 * @return status code
1622 static efi_status_t EFIAPI
efi_open_protocol_information(efi_handle_t handle
,
1623 const efi_guid_t
*protocol
,
1624 struct efi_open_protocol_info_entry
**entry_buffer
,
1625 efi_uintn_t
*entry_count
)
1627 EFI_ENTRY("%p, %pUl, %p, %p", handle
, protocol
, entry_buffer
,
1629 return EFI_EXIT(EFI_NOT_FOUND
);
1633 * Get protocols installed on a handle.
1635 * This function implements the ProtocolsPerHandleService.
1636 * See the Unified Extensible Firmware Interface (UEFI) specification
1639 * @handle handle for which the information is retrieved
1640 * @protocol_buffer buffer with protocol GUIDs
1641 * @protocol_buffer_count number of entries in the buffer
1642 * @return status code
1644 static efi_status_t EFIAPI
efi_protocols_per_handle(void *handle
,
1645 efi_guid_t
***protocol_buffer
,
1646 efi_uintn_t
*protocol_buffer_count
)
1648 unsigned long buffer_size
;
1649 struct efi_object
*efiobj
;
1650 struct list_head
*protocol_handle
;
1653 EFI_ENTRY("%p, %p, %p", handle
, protocol_buffer
,
1654 protocol_buffer_count
);
1656 if (!handle
|| !protocol_buffer
|| !protocol_buffer_count
)
1657 return EFI_EXIT(EFI_INVALID_PARAMETER
);
1659 *protocol_buffer
= NULL
;
1660 *protocol_buffer_count
= 0;
1662 efiobj
= efi_search_obj(handle
);
1664 return EFI_EXIT(EFI_INVALID_PARAMETER
);
1666 /* Count protocols */
1667 list_for_each(protocol_handle
, &efiobj
->protocols
) {
1668 ++*protocol_buffer_count
;
1672 if (*protocol_buffer_count
) {
1675 buffer_size
= sizeof(efi_guid_t
*) * *protocol_buffer_count
;
1676 r
= efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES
, buffer_size
,
1677 (void **)protocol_buffer
);
1678 if (r
!= EFI_SUCCESS
)
1680 list_for_each(protocol_handle
, &efiobj
->protocols
) {
1681 struct efi_handler
*protocol
;
1683 protocol
= list_entry(protocol_handle
,
1684 struct efi_handler
, link
);
1685 (*protocol_buffer
)[j
] = (void *)protocol
->guid
;
1690 return EFI_EXIT(EFI_SUCCESS
);
1694 * Locate handles implementing a protocol.
1696 * This function implements the LocateHandleBuffer service.
1697 * See the Unified Extensible Firmware Interface (UEFI) specification
1700 * @search_type selection criterion
1701 * @protocol GUID of the protocol
1702 * @search_key registration key
1703 * @no_handles number of returned handles
1704 * @buffer buffer with the returned handles
1705 * @return status code
1707 static efi_status_t EFIAPI
efi_locate_handle_buffer(
1708 enum efi_locate_search_type search_type
,
1709 const efi_guid_t
*protocol
, void *search_key
,
1710 efi_uintn_t
*no_handles
, efi_handle_t
**buffer
)
1713 efi_uintn_t buffer_size
= 0;
1715 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type
, protocol
, search_key
,
1716 no_handles
, buffer
);
1718 if (!no_handles
|| !buffer
) {
1719 r
= EFI_INVALID_PARAMETER
;
1724 r
= efi_locate_handle(search_type
, protocol
, search_key
, &buffer_size
,
1726 if (r
!= EFI_BUFFER_TOO_SMALL
)
1728 r
= efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES
, buffer_size
,
1730 if (r
!= EFI_SUCCESS
)
1732 r
= efi_locate_handle(search_type
, protocol
, search_key
, &buffer_size
,
1734 if (r
== EFI_SUCCESS
)
1735 *no_handles
= buffer_size
/ sizeof(void *);
1741 * Find an interface implementing a protocol.
1743 * This function implements the LocateProtocol service.
1744 * See the Unified Extensible Firmware Interface (UEFI) specification
1747 * @protocol GUID of the protocol
1748 * @registration registration key passed to the notification function
1749 * @protocol_interface interface implementing the protocol
1750 * @return status code
1752 static efi_status_t EFIAPI
efi_locate_protocol(const efi_guid_t
*protocol
,
1754 void **protocol_interface
)
1756 struct list_head
*lhandle
;
1759 EFI_ENTRY("%pUl, %p, %p", protocol
, registration
, protocol_interface
);
1761 if (!protocol
|| !protocol_interface
)
1762 return EFI_EXIT(EFI_INVALID_PARAMETER
);
1764 list_for_each(lhandle
, &efi_obj_list
) {
1765 struct efi_object
*efiobj
;
1766 struct efi_handler
*handler
;
1768 efiobj
= list_entry(lhandle
, struct efi_object
, link
);
1770 ret
= efi_search_protocol(efiobj
->handle
, protocol
, &handler
);
1771 if (ret
== EFI_SUCCESS
) {
1772 *protocol_interface
= handler
->protocol_interface
;
1773 return EFI_EXIT(EFI_SUCCESS
);
1776 *protocol_interface
= NULL
;
1778 return EFI_EXIT(EFI_NOT_FOUND
);
1782 * Get the device path and handle of an device implementing a protocol.
1784 * This function implements the LocateDevicePath service.
1785 * See the Unified Extensible Firmware Interface (UEFI) specification
1788 * @protocol GUID of the protocol
1789 * @device_path device path
1790 * @device handle of the device
1791 * @return status code
1793 static efi_status_t EFIAPI
efi_locate_device_path(
1794 const efi_guid_t
*protocol
,
1795 struct efi_device_path
**device_path
,
1796 efi_handle_t
*device
)
1798 struct efi_device_path
*dp
;
1800 struct efi_handler
*handler
;
1801 efi_handle_t
*handles
;
1803 size_t len_best
= 0;
1804 efi_uintn_t no_handles
;
1808 EFI_ENTRY("%pUl, %p, %p", protocol
, device_path
, device
);
1810 if (!protocol
|| !device_path
|| !*device_path
|| !device
) {
1811 ret
= EFI_INVALID_PARAMETER
;
1815 /* Find end of device path */
1816 len
= efi_dp_size(*device_path
);
1818 /* Get all handles implementing the protocol */
1819 ret
= EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL
, protocol
, NULL
,
1820 &no_handles
, &handles
));
1821 if (ret
!= EFI_SUCCESS
)
1824 for (i
= 0; i
< no_handles
; ++i
) {
1825 /* Find the device path protocol */
1826 ret
= efi_search_protocol(handles
[i
], &efi_guid_device_path
,
1828 if (ret
!= EFI_SUCCESS
)
1830 dp
= (struct efi_device_path
*)handler
->protocol_interface
;
1831 len_dp
= efi_dp_size(dp
);
1833 * This handle can only be a better fit
1834 * if its device path length is longer than the best fit and
1835 * if its device path length is shorter of equal the searched
1838 if (len_dp
<= len_best
|| len_dp
> len
)
1840 /* Check if dp is a subpath of device_path */
1841 if (memcmp(*device_path
, dp
, len_dp
))
1843 *device
= handles
[i
];
1847 remainder
= (u8
*)*device_path
+ len_best
;
1848 *device_path
= (struct efi_device_path
*)remainder
;
1851 ret
= EFI_NOT_FOUND
;
1854 return EFI_EXIT(ret
);
1858 * Install multiple protocol interfaces.
1860 * This function implements the MultipleProtocolInterfaces service.
1861 * See the Unified Extensible Firmware Interface (UEFI) specification
1864 * @handle handle on which the protocol interfaces shall be installed
1865 * @... NULL terminated argument list with pairs of protocol GUIDS and
1867 * @return status code
1869 static efi_status_t EFIAPI
efi_install_multiple_protocol_interfaces(
1872 EFI_ENTRY("%p", handle
);
1875 const efi_guid_t
*protocol
;
1876 void *protocol_interface
;
1877 efi_status_t r
= EFI_SUCCESS
;
1881 return EFI_EXIT(EFI_INVALID_PARAMETER
);
1883 va_start(argptr
, handle
);
1885 protocol
= va_arg(argptr
, efi_guid_t
*);
1888 protocol_interface
= va_arg(argptr
, void*);
1889 r
= EFI_CALL(efi_install_protocol_interface(
1891 EFI_NATIVE_INTERFACE
,
1892 protocol_interface
));
1893 if (r
!= EFI_SUCCESS
)
1898 if (r
== EFI_SUCCESS
)
1901 /* If an error occurred undo all changes. */
1902 va_start(argptr
, handle
);
1904 protocol
= va_arg(argptr
, efi_guid_t
*);
1905 protocol_interface
= va_arg(argptr
, void*);
1906 EFI_CALL(efi_uninstall_protocol_interface(handle
, protocol
,
1907 protocol_interface
));
1915 * Uninstall multiple protocol interfaces.
1917 * This function implements the UninstallMultipleProtocolInterfaces service.
1918 * See the Unified Extensible Firmware Interface (UEFI) specification
1921 * @handle handle from which the protocol interfaces shall be removed
1922 * @... NULL terminated argument list with pairs of protocol GUIDS and
1924 * @return status code
1926 static efi_status_t EFIAPI
efi_uninstall_multiple_protocol_interfaces(
1929 EFI_ENTRY("%p", handle
);
1932 const efi_guid_t
*protocol
;
1933 void *protocol_interface
;
1934 efi_status_t r
= EFI_SUCCESS
;
1938 return EFI_EXIT(EFI_INVALID_PARAMETER
);
1940 va_start(argptr
, handle
);
1942 protocol
= va_arg(argptr
, efi_guid_t
*);
1945 protocol_interface
= va_arg(argptr
, void*);
1946 r
= EFI_CALL(efi_uninstall_protocol_interface(
1948 protocol_interface
));
1949 if (r
!= EFI_SUCCESS
)
1954 if (r
== EFI_SUCCESS
)
1957 /* If an error occurred undo all changes. */
1958 va_start(argptr
, handle
);
1960 protocol
= va_arg(argptr
, efi_guid_t
*);
1961 protocol_interface
= va_arg(argptr
, void*);
1962 EFI_CALL(efi_install_protocol_interface(&handle
, protocol
,
1963 EFI_NATIVE_INTERFACE
,
1964 protocol_interface
));
1972 * Calculate cyclic redundancy code.
1974 * This function implements the CalculateCrc32 service.
1975 * See the Unified Extensible Firmware Interface (UEFI) specification
1978 * @data buffer with data
1979 * @data_size size of buffer in bytes
1980 * @crc32_p cyclic redundancy code
1981 * @return status code
1983 static efi_status_t EFIAPI
efi_calculate_crc32(void *data
,
1984 unsigned long data_size
,
1987 EFI_ENTRY("%p, %ld", data
, data_size
);
1988 *crc32_p
= crc32(0, data
, data_size
);
1989 return EFI_EXIT(EFI_SUCCESS
);
1995 * This function implements the CopyMem service.
1996 * See the Unified Extensible Firmware Interface (UEFI) specification
1999 * @destination destination of the copy operation
2000 * @source source of the copy operation
2001 * @length number of bytes to copy
2003 static void EFIAPI
efi_copy_mem(void *destination
, const void *source
,
2006 EFI_ENTRY("%p, %p, %ld", destination
, source
, (unsigned long)length
);
2007 memcpy(destination
, source
, length
);
2008 EFI_EXIT(EFI_SUCCESS
);
2012 * Fill memory with a byte value.
2014 * This function implements the SetMem service.
2015 * See the Unified Extensible Firmware Interface (UEFI) specification
2018 * @buffer buffer to fill
2019 * @size size of buffer in bytes
2020 * @value byte to copy to the buffer
2022 static void EFIAPI
efi_set_mem(void *buffer
, size_t size
, uint8_t value
)
2024 EFI_ENTRY("%p, %ld, 0x%x", buffer
, (unsigned long)size
, value
);
2025 memset(buffer
, value
, size
);
2026 EFI_EXIT(EFI_SUCCESS
);
2030 * Open protocol interface on a handle.
2032 * This function implements the OpenProtocol interface.
2033 * See the Unified Extensible Firmware Interface (UEFI) specification
2036 * @handle handle on which the protocol shall be opened
2037 * @protocol GUID of the protocol
2038 * @protocol_interface interface implementing the protocol
2039 * @agent_handle handle of the driver
2040 * @controller_handle handle of the controller
2041 * @attributes attributes indicating how to open the protocol
2042 * @return status code
2044 static efi_status_t EFIAPI
efi_open_protocol(
2045 void *handle
, const efi_guid_t
*protocol
,
2046 void **protocol_interface
, void *agent_handle
,
2047 void *controller_handle
, uint32_t attributes
)
2049 struct efi_handler
*handler
;
2050 efi_status_t r
= EFI_INVALID_PARAMETER
;
2052 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle
, protocol
,
2053 protocol_interface
, agent_handle
, controller_handle
,
2056 if (!handle
|| !protocol
||
2057 (!protocol_interface
&& attributes
!=
2058 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)) {
2062 switch (attributes
) {
2063 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
:
2064 case EFI_OPEN_PROTOCOL_GET_PROTOCOL
:
2065 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL
:
2067 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
:
2068 if (controller_handle
== handle
)
2070 case EFI_OPEN_PROTOCOL_BY_DRIVER
:
2071 case EFI_OPEN_PROTOCOL_BY_DRIVER
| EFI_OPEN_PROTOCOL_EXCLUSIVE
:
2072 if (controller_handle
== NULL
)
2074 case EFI_OPEN_PROTOCOL_EXCLUSIVE
:
2075 if (agent_handle
== NULL
)
2082 r
= efi_search_protocol(handle
, protocol
, &handler
);
2083 if (r
!= EFI_SUCCESS
)
2086 if (attributes
!= EFI_OPEN_PROTOCOL_TEST_PROTOCOL
)
2087 *protocol_interface
= handler
->protocol_interface
;
2093 * Get interface of a protocol on a handle.
2095 * This function implements the HandleProtocol service.
2096 * See the Unified Extensible Firmware Interface (UEFI) specification
2099 * @handle handle on which the protocol shall be opened
2100 * @protocol GUID of the protocol
2101 * @protocol_interface interface implementing the protocol
2102 * @return status code
2104 static efi_status_t EFIAPI
efi_handle_protocol(void *handle
,
2105 const efi_guid_t
*protocol
,
2106 void **protocol_interface
)
2108 return efi_open_protocol(handle
, protocol
, protocol_interface
, NULL
,
2109 NULL
, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL
);
2112 static const struct efi_boot_services efi_boot_services
= {
2114 .headersize
= sizeof(struct efi_table_hdr
),
2116 .raise_tpl
= efi_raise_tpl
,
2117 .restore_tpl
= efi_restore_tpl
,
2118 .allocate_pages
= efi_allocate_pages_ext
,
2119 .free_pages
= efi_free_pages_ext
,
2120 .get_memory_map
= efi_get_memory_map_ext
,
2121 .allocate_pool
= efi_allocate_pool_ext
,
2122 .free_pool
= efi_free_pool_ext
,
2123 .create_event
= efi_create_event_ext
,
2124 .set_timer
= efi_set_timer_ext
,
2125 .wait_for_event
= efi_wait_for_event
,
2126 .signal_event
= efi_signal_event_ext
,
2127 .close_event
= efi_close_event
,
2128 .check_event
= efi_check_event
,
2129 .install_protocol_interface
= efi_install_protocol_interface
,
2130 .reinstall_protocol_interface
= efi_reinstall_protocol_interface
,
2131 .uninstall_protocol_interface
= efi_uninstall_protocol_interface
,
2132 .handle_protocol
= efi_handle_protocol
,
2134 .register_protocol_notify
= efi_register_protocol_notify
,
2135 .locate_handle
= efi_locate_handle_ext
,
2136 .locate_device_path
= efi_locate_device_path
,
2137 .install_configuration_table
= efi_install_configuration_table_ext
,
2138 .load_image
= efi_load_image
,
2139 .start_image
= efi_start_image
,
2141 .unload_image
= efi_unload_image
,
2142 .exit_boot_services
= efi_exit_boot_services
,
2143 .get_next_monotonic_count
= efi_get_next_monotonic_count
,
2145 .set_watchdog_timer
= efi_set_watchdog_timer
,
2146 .connect_controller
= efi_connect_controller
,
2147 .disconnect_controller
= efi_disconnect_controller
,
2148 .open_protocol
= efi_open_protocol
,
2149 .close_protocol
= efi_close_protocol
,
2150 .open_protocol_information
= efi_open_protocol_information
,
2151 .protocols_per_handle
= efi_protocols_per_handle
,
2152 .locate_handle_buffer
= efi_locate_handle_buffer
,
2153 .locate_protocol
= efi_locate_protocol
,
2154 .install_multiple_protocol_interfaces
= efi_install_multiple_protocol_interfaces
,
2155 .uninstall_multiple_protocol_interfaces
= efi_uninstall_multiple_protocol_interfaces
,
2156 .calculate_crc32
= efi_calculate_crc32
,
2157 .copy_mem
= efi_copy_mem
,
2158 .set_mem
= efi_set_mem
,
2162 static uint16_t __efi_runtime_data firmware_vendor
[] =
2163 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2165 struct efi_system_table __efi_runtime_data systab
= {
2167 .signature
= EFI_SYSTEM_TABLE_SIGNATURE
,
2168 .revision
= 0x20005, /* 2.5 */
2169 .headersize
= sizeof(struct efi_table_hdr
),
2171 .fw_vendor
= (long)firmware_vendor
,
2172 .con_in
= (void*)&efi_con_in
,
2173 .con_out
= (void*)&efi_con_out
,
2174 .std_err
= (void*)&efi_con_out
,
2175 .runtime
= (void*)&efi_runtime_services
,
2176 .boottime
= (void*)&efi_boot_services
,
2178 .tables
= (void*)efi_conf_table
,