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);
325 * Add a new object to the object list.
327 * The protocols list is initialized.
328 * The object handle is set.
330 * @obj object to be added
332 void efi_add_handle(struct efi_object *obj)
336 INIT_LIST_HEAD(&obj->protocols);
338 list_add_tail(&obj->link, &efi_obj_list);
345 * @return status code
347 efi_status_t efi_create_handle(void **handle)
349 struct efi_object *obj;
352 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
353 sizeof(struct efi_object),
355 if (r != EFI_SUCCESS)
358 *handle = obj->handle;
363 * Find a protocol on a handle.
366 * @protocol_guid GUID of the protocol
367 * @handler reference to the protocol
368 * @return status code
370 efi_status_t efi_search_protocol(const void *handle,
371 const efi_guid_t *protocol_guid,
372 struct efi_handler **handler)
374 struct efi_object *efiobj;
375 struct list_head *lhandle;
377 if (!handle || !protocol_guid)
378 return EFI_INVALID_PARAMETER;
379 efiobj = efi_search_obj(handle);
381 return EFI_INVALID_PARAMETER;
382 list_for_each(lhandle, &efiobj->protocols) {
383 struct efi_handler *protocol;
385 protocol = list_entry(lhandle, struct efi_handler, link);
386 if (!guidcmp(protocol->guid, protocol_guid)) {
392 return EFI_NOT_FOUND;
396 * Delete protocol from a handle.
398 * @handle handle from which the protocol shall be deleted
399 * @protocol GUID of the protocol to be deleted
400 * @protocol_interface interface of the protocol implementation
401 * @return status code
403 efi_status_t efi_remove_protocol(const void *handle, const efi_guid_t *protocol,
404 void *protocol_interface)
406 struct efi_handler *handler;
409 ret = efi_search_protocol(handle, protocol, &handler);
410 if (ret != EFI_SUCCESS)
412 if (guidcmp(handler->guid, protocol))
413 return EFI_INVALID_PARAMETER;
414 list_del(&handler->link);
420 * Delete all protocols from a handle.
422 * @handle handle from which the protocols shall be deleted
423 * @return status code
425 efi_status_t efi_remove_all_protocols(const void *handle)
427 struct efi_object *efiobj;
428 struct list_head *lhandle;
429 struct list_head *pos;
431 efiobj = efi_search_obj(handle);
433 return EFI_INVALID_PARAMETER;
434 list_for_each_safe(lhandle, pos, &efiobj->protocols) {
435 struct efi_handler *protocol;
438 protocol = list_entry(lhandle, struct efi_handler, link);
440 ret = efi_remove_protocol(handle, protocol->guid,
441 protocol->protocol_interface);
442 if (ret != EFI_SUCCESS)
451 * @handle handle to delete
453 void efi_delete_handle(struct efi_object *obj)
457 efi_remove_all_protocols(obj->handle);
458 list_del(&obj->link);
463 * Our event capabilities are very limited. Only a small limited
464 * number of events is allowed to coexist.
466 static struct efi_event efi_events[16];
471 * This function is used inside U-Boot code to create an event.
473 * For the API function implementing the CreateEvent service see
474 * efi_create_event_ext.
476 * @type type of the event to create
477 * @notify_tpl task priority level of the event
478 * @notify_function notification function of the event
479 * @notify_context pointer passed to the notification function
480 * @event created event
481 * @return status code
483 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
484 void (EFIAPI *notify_function) (
485 struct efi_event *event,
487 void *notify_context, struct efi_event **event)
492 return EFI_INVALID_PARAMETER;
494 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
495 return EFI_INVALID_PARAMETER;
497 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
498 notify_function == NULL)
499 return EFI_INVALID_PARAMETER;
501 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
502 if (efi_events[i].type)
504 efi_events[i].type = type;
505 efi_events[i].notify_tpl = notify_tpl;
506 efi_events[i].notify_function = notify_function;
507 efi_events[i].notify_context = notify_context;
508 /* Disable timers on bootup */
509 efi_events[i].trigger_next = -1ULL;
510 efi_events[i].is_queued = false;
511 efi_events[i].is_signaled = false;
512 *event = &efi_events[i];
515 return EFI_OUT_OF_RESOURCES;
521 * This function implements the CreateEvent service.
522 * See the Unified Extensible Firmware Interface (UEFI) specification
525 * @type type of the event to create
526 * @notify_tpl task priority level of the event
527 * @notify_function notification function of the event
528 * @notify_context pointer passed to the notification function
529 * @event created event
530 * @return status code
532 static efi_status_t EFIAPI efi_create_event_ext(
533 uint32_t type, efi_uintn_t notify_tpl,
534 void (EFIAPI *notify_function) (
535 struct efi_event *event,
537 void *notify_context, struct efi_event **event)
539 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
541 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
542 notify_context, event));
547 * Check if a timer event has occurred or a queued notification function should
550 * Our timers have to work without interrupts, so we check whenever keyboard
551 * input or disk accesses happen if enough time elapsed for them to fire.
553 void efi_timer_check(void)
556 u64 now = timer_get_us();
558 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
559 if (!efi_events[i].type)
561 if (efi_events[i].is_queued)
562 efi_signal_event(&efi_events[i]);
563 if (!(efi_events[i].type & EVT_TIMER) ||
564 now < efi_events[i].trigger_next)
566 switch (efi_events[i].trigger_type) {
567 case EFI_TIMER_RELATIVE:
568 efi_events[i].trigger_type = EFI_TIMER_STOP;
570 case EFI_TIMER_PERIODIC:
571 efi_events[i].trigger_next +=
572 efi_events[i].trigger_time;
577 efi_events[i].is_signaled = true;
578 efi_signal_event(&efi_events[i]);
584 * Set the trigger time for a timer event or stop the event.
586 * This is the function for internal usage in U-Boot. For the API function
587 * implementing the SetTimer service see efi_set_timer_ext.
589 * @event event for which the timer is set
590 * @type type of the timer
591 * @trigger_time trigger period in multiples of 100ns
592 * @return status code
594 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
595 uint64_t trigger_time)
600 * The parameter defines a multiple of 100ns.
601 * We use multiples of 1000ns. So divide by 10.
603 do_div(trigger_time, 10);
605 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
606 if (event != &efi_events[i])
609 if (!(event->type & EVT_TIMER))
613 event->trigger_next = -1ULL;
615 case EFI_TIMER_PERIODIC:
616 case EFI_TIMER_RELATIVE:
617 event->trigger_next =
618 timer_get_us() + trigger_time;
621 return EFI_INVALID_PARAMETER;
623 event->trigger_type = type;
624 event->trigger_time = trigger_time;
625 event->is_signaled = false;
628 return EFI_INVALID_PARAMETER;
632 * Set the trigger time for a timer event or stop the event.
634 * This function implements the SetTimer service.
635 * See the Unified Extensible Firmware Interface (UEFI) specification
638 * @event event for which the timer is set
639 * @type type of the timer
640 * @trigger_time trigger period in multiples of 100ns
641 * @return status code
643 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
644 enum efi_timer_delay type,
645 uint64_t trigger_time)
647 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
648 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
652 * Wait for events to be signaled.
654 * This function implements the WaitForEvent service.
655 * See the Unified Extensible Firmware Interface (UEFI) specification
658 * @num_events number of events to be waited for
659 * @events events to be waited for
660 * @index index of the event that was signaled
661 * @return status code
663 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
664 struct efi_event **event,
669 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
671 /* Check parameters */
672 if (!num_events || !event)
673 return EFI_EXIT(EFI_INVALID_PARAMETER);
675 if (efi_tpl != TPL_APPLICATION)
676 return EFI_EXIT(EFI_UNSUPPORTED);
677 for (i = 0; i < num_events; ++i) {
678 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
679 if (event[i] == &efi_events[j])
682 return EFI_EXIT(EFI_INVALID_PARAMETER);
684 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
685 return EFI_EXIT(EFI_INVALID_PARAMETER);
686 if (!event[i]->is_signaled)
687 efi_signal_event(event[i]);
690 /* Wait for signal */
692 for (i = 0; i < num_events; ++i) {
693 if (event[i]->is_signaled)
696 /* Allow events to occur. */
702 * Reset the signal which is passed to the caller to allow periodic
705 event[i]->is_signaled = false;
709 return EFI_EXIT(EFI_SUCCESS);
713 * Signal an EFI event.
715 * This function implements the SignalEvent service.
716 * See the Unified Extensible Firmware Interface (UEFI) specification
719 * This functions sets the signaled state of the event and queues the
720 * notification function for execution.
722 * @event event to signal
723 * @return status code
725 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
729 EFI_ENTRY("%p", event);
730 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
731 if (event != &efi_events[i])
733 if (event->is_signaled)
735 event->is_signaled = true;
736 if (event->type & EVT_NOTIFY_SIGNAL)
737 efi_signal_event(event);
740 return EFI_EXIT(EFI_SUCCESS);
744 * Close an EFI event.
746 * This function implements the CloseEvent service.
747 * See the Unified Extensible Firmware Interface (UEFI) specification
750 * @event event to close
751 * @return status code
753 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
757 EFI_ENTRY("%p", event);
758 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
759 if (event == &efi_events[i]) {
761 event->trigger_next = -1ULL;
762 event->is_queued = false;
763 event->is_signaled = false;
764 return EFI_EXIT(EFI_SUCCESS);
767 return EFI_EXIT(EFI_INVALID_PARAMETER);
771 * Check if an event is signaled.
773 * This function implements the CheckEvent service.
774 * See the Unified Extensible Firmware Interface (UEFI) specification
777 * If an event is not signaled yet the notification function is queued.
779 * @event event to check
780 * @return status code
782 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
786 EFI_ENTRY("%p", event);
788 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
789 if (event != &efi_events[i])
791 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
793 if (!event->is_signaled)
794 efi_signal_event(event);
795 if (event->is_signaled)
796 return EFI_EXIT(EFI_SUCCESS);
797 return EFI_EXIT(EFI_NOT_READY);
799 return EFI_EXIT(EFI_INVALID_PARAMETER);
803 * Find the internal EFI object for a handle.
805 * @handle handle to find
808 struct efi_object *efi_search_obj(const void *handle)
810 struct efi_object *efiobj;
812 list_for_each_entry(efiobj, &efi_obj_list, link) {
813 if (efiobj->handle == handle)
821 * Install new protocol on a handle.
823 * @handle handle on which the protocol shall be installed
824 * @protocol GUID of the protocol to be installed
825 * @protocol_interface interface of the protocol implementation
826 * @return status code
828 efi_status_t efi_add_protocol(const void *handle, const efi_guid_t *protocol,
829 void *protocol_interface)
831 struct efi_object *efiobj;
832 struct efi_handler *handler;
835 efiobj = efi_search_obj(handle);
837 return EFI_INVALID_PARAMETER;
838 ret = efi_search_protocol(handle, protocol, NULL);
839 if (ret != EFI_NOT_FOUND)
840 return EFI_INVALID_PARAMETER;
841 handler = calloc(1, sizeof(struct efi_handler));
843 return EFI_OUT_OF_RESOURCES;
844 handler->guid = protocol;
845 handler->protocol_interface = protocol_interface;
846 list_add_tail(&handler->link, &efiobj->protocols);
851 * Install protocol interface.
853 * This function implements the InstallProtocolInterface service.
854 * See the Unified Extensible Firmware Interface (UEFI) specification
857 * @handle handle on which the protocol shall be installed
858 * @protocol GUID of the protocol to be installed
859 * @protocol_interface_type type of the interface to be installed,
860 * always EFI_NATIVE_INTERFACE
861 * @protocol_interface interface of the protocol implementation
862 * @return status code
864 static efi_status_t EFIAPI efi_install_protocol_interface(
865 void **handle, const efi_guid_t *protocol,
866 int protocol_interface_type, void *protocol_interface)
870 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
873 if (!handle || !protocol ||
874 protocol_interface_type != EFI_NATIVE_INTERFACE) {
875 r = EFI_INVALID_PARAMETER;
879 /* Create new handle if requested. */
881 r = efi_create_handle(handle);
882 if (r != EFI_SUCCESS)
884 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
887 debug("%sEFI: handle %p\n", indent_string(nesting_level),
890 /* Add new protocol */
891 r = efi_add_protocol(*handle, protocol, protocol_interface);
897 * Reinstall protocol interface.
899 * This function implements the ReinstallProtocolInterface service.
900 * See the Unified Extensible Firmware Interface (UEFI) specification
903 * @handle handle on which the protocol shall be
905 * @protocol GUID of the protocol to be installed
906 * @old_interface interface to be removed
907 * @new_interface interface to be installed
908 * @return status code
910 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
911 const efi_guid_t *protocol, void *old_interface,
914 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
916 return EFI_EXIT(EFI_ACCESS_DENIED);
920 * Uninstall protocol interface.
922 * This function implements the UninstallProtocolInterface service.
923 * See the Unified Extensible Firmware Interface (UEFI) specification
926 * @handle handle from which the protocol shall be removed
927 * @protocol GUID of the protocol to be removed
928 * @protocol_interface interface to be removed
929 * @return status code
931 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
932 void *handle, const efi_guid_t *protocol,
933 void *protocol_interface)
935 struct efi_handler *handler;
938 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
940 if (!handle || !protocol) {
941 r = EFI_INVALID_PARAMETER;
945 /* Find the protocol on the handle */
946 r = efi_search_protocol(handle, protocol, &handler);
947 if (r != EFI_SUCCESS)
949 if (handler->protocol_interface) {
950 /* TODO disconnect controllers */
951 r = EFI_ACCESS_DENIED;
953 r = efi_remove_protocol(handle, protocol, protocol_interface);
960 * Register an event for notification when a protocol is installed.
962 * This function implements the RegisterProtocolNotify service.
963 * See the Unified Extensible Firmware Interface (UEFI) specification
966 * @protocol GUID of the protocol whose installation shall be
968 * @event event to be signaled upon installation of the protocol
969 * @registration key for retrieving the registration information
970 * @return status code
972 static efi_status_t EFIAPI efi_register_protocol_notify(
973 const efi_guid_t *protocol,
974 struct efi_event *event,
977 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
978 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
982 * Determine if an EFI handle implements a protocol.
984 * See the documentation of the LocateHandle service in the UEFI specification.
986 * @search_type selection criterion
987 * @protocol GUID of the protocol
988 * @search_key registration key
990 * @return 0 if the handle implements the protocol
992 static int efi_search(enum efi_locate_search_type search_type,
993 const efi_guid_t *protocol, void *search_key,
994 struct efi_object *efiobj)
998 switch (search_type) {
1001 case BY_REGISTER_NOTIFY:
1002 /* TODO: RegisterProtocolNotify is not implemented yet */
1005 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1006 return (ret != EFI_SUCCESS);
1008 /* Invalid search type */
1014 * Locate handles implementing a protocol.
1016 * This function is meant for U-Boot internal calls. For the API implementation
1017 * of the LocateHandle service see efi_locate_handle_ext.
1019 * @search_type selection criterion
1020 * @protocol GUID of the protocol
1021 * @search_key registration key
1022 * @buffer_size size of the buffer to receive the handles in bytes
1023 * @buffer buffer to receive the relevant handles
1024 * @return status code
1026 static efi_status_t efi_locate_handle(
1027 enum efi_locate_search_type search_type,
1028 const efi_guid_t *protocol, void *search_key,
1029 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1031 struct efi_object *efiobj;
1032 efi_uintn_t size = 0;
1034 /* Check parameters */
1035 switch (search_type) {
1038 case BY_REGISTER_NOTIFY:
1040 return EFI_INVALID_PARAMETER;
1041 /* RegisterProtocolNotify is not implemented yet */
1042 return EFI_UNSUPPORTED;
1045 return EFI_INVALID_PARAMETER;
1048 return EFI_INVALID_PARAMETER;
1052 * efi_locate_handle_buffer uses this function for
1053 * the calculation of the necessary buffer size.
1054 * So do not require a buffer for buffersize == 0.
1056 if (!buffer_size || (*buffer_size && !buffer))
1057 return EFI_INVALID_PARAMETER;
1059 /* Count how much space we need */
1060 list_for_each_entry(efiobj, &efi_obj_list, link) {
1061 if (!efi_search(search_type, protocol, search_key, efiobj))
1062 size += sizeof(void*);
1065 if (*buffer_size < size) {
1066 *buffer_size = size;
1067 return EFI_BUFFER_TOO_SMALL;
1070 *buffer_size = size;
1072 return EFI_NOT_FOUND;
1074 /* Then fill the array */
1075 list_for_each_entry(efiobj, &efi_obj_list, link) {
1076 if (!efi_search(search_type, protocol, search_key, efiobj))
1077 *buffer++ = efiobj->handle;
1084 * Locate handles implementing a protocol.
1086 * This function implements the LocateHandle service.
1087 * See the Unified Extensible Firmware Interface (UEFI) specification
1090 * @search_type selection criterion
1091 * @protocol GUID of the protocol
1092 * @search_key registration key
1093 * @buffer_size size of the buffer to receive the handles in bytes
1094 * @buffer buffer to receive the relevant handles
1095 * @return 0 if the handle implements the protocol
1097 static efi_status_t EFIAPI efi_locate_handle_ext(
1098 enum efi_locate_search_type search_type,
1099 const efi_guid_t *protocol, void *search_key,
1100 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1102 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1103 buffer_size, buffer);
1105 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1106 buffer_size, buffer));
1109 /* Collapses configuration table entries, removing index i */
1110 static void efi_remove_configuration_table(int i)
1112 struct efi_configuration_table *this = &efi_conf_table[i];
1113 struct efi_configuration_table *next = &efi_conf_table[i+1];
1114 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1116 memmove(this, next, (ulong)end - (ulong)next);
1121 * Adds, updates, or removes a configuration table.
1123 * This function is used for internal calls. For the API implementation of the
1124 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1126 * @guid GUID of the installed table
1127 * @table table to be installed
1128 * @return status code
1130 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
1134 /* Check for guid override */
1135 for (i = 0; i < systab.nr_tables; i++) {
1136 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1138 efi_conf_table[i].table = table;
1140 efi_remove_configuration_table(i);
1146 return EFI_NOT_FOUND;
1148 /* No override, check for overflow */
1149 if (i >= ARRAY_SIZE(efi_conf_table))
1150 return EFI_OUT_OF_RESOURCES;
1152 /* Add a new entry */
1153 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1154 efi_conf_table[i].table = table;
1155 systab.nr_tables = i + 1;
1161 * Adds, updates, or removes a configuration table.
1163 * This function implements the InstallConfigurationTable service.
1164 * See the Unified Extensible Firmware Interface (UEFI) specification
1167 * @guid GUID of the installed table
1168 * @table table to be installed
1169 * @return status code
1171 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1174 EFI_ENTRY("%pUl, %p", guid, table);
1175 return EFI_EXIT(efi_install_configuration_table(guid, table));
1179 * Initialize a loaded_image_info + loaded_image_info object with correct
1180 * protocols, boot-device, etc.
1182 * @info loaded image info to be passed to the entry point of the
1184 * @obj internal object associated with the loaded image
1185 * @device_path device path of the loaded image
1186 * @file_path file path of the loaded image
1187 * @return status code
1189 efi_status_t efi_setup_loaded_image(
1190 struct efi_loaded_image *info, struct efi_object *obj,
1191 struct efi_device_path *device_path,
1192 struct efi_device_path *file_path)
1196 /* Add internal object to object list */
1197 efi_add_handle(obj);
1198 /* efi_exit() assumes that the handle points to the info */
1201 info->file_path = file_path;
1203 info->device_handle = efi_dp_find_obj(device_path, NULL);
1206 * When asking for the device path interface, return
1207 * bootefi_device_path
1209 ret = efi_add_protocol(obj->handle, &efi_guid_device_path, device_path);
1210 if (ret != EFI_SUCCESS)
1214 * When asking for the loaded_image interface, just
1215 * return handle which points to loaded_image_info
1217 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1218 if (ret != EFI_SUCCESS)
1221 ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1222 (void *)&efi_console_control);
1223 if (ret != EFI_SUCCESS)
1226 ret = efi_add_protocol(obj->handle,
1227 &efi_guid_device_path_to_text_protocol,
1228 (void *)&efi_device_path_to_text);
1229 if (ret != EFI_SUCCESS)
1234 printf("ERROR: Failure to install protocols for loaded image\n");
1239 * Load an image using a file path.
1241 * @file_path the path of the image to load
1242 * @buffer buffer containing the loaded image
1243 * @return status code
1245 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1248 struct efi_file_info *info = NULL;
1249 struct efi_file_handle *f;
1250 static efi_status_t ret;
1253 f = efi_file_from_path(file_path);
1255 return EFI_DEVICE_ERROR;
1258 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1260 if (ret == EFI_BUFFER_TOO_SMALL) {
1262 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1265 if (ret != EFI_SUCCESS)
1268 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1272 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1276 EFI_CALL(f->close(f));
1278 if (ret != EFI_SUCCESS) {
1279 efi_free_pool(*buffer);
1287 * Load an EFI image into memory.
1289 * This function implements the LoadImage service.
1290 * See the Unified Extensible Firmware Interface (UEFI) specification
1293 * @boot_policy true for request originating from the boot manager
1294 * @parent_image the calles's image handle
1295 * @file_path the path of the image to load
1296 * @source_buffer memory location from which the image is installed
1297 * @source_size size of the memory area from which the image is
1299 * @image_handle handle for the newly installed image
1300 * @return status code
1302 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1303 efi_handle_t parent_image,
1304 struct efi_device_path *file_path,
1305 void *source_buffer,
1306 unsigned long source_size,
1307 efi_handle_t *image_handle)
1309 struct efi_loaded_image *info;
1310 struct efi_object *obj;
1312 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1313 file_path, source_buffer, source_size, image_handle);
1315 info = calloc(1, sizeof(*info));
1316 obj = calloc(1, sizeof(*obj));
1318 if (!source_buffer) {
1319 struct efi_device_path *dp, *fp;
1322 ret = efi_load_image_from_path(file_path, &source_buffer);
1323 if (ret != EFI_SUCCESS) {
1326 return EFI_EXIT(ret);
1330 * split file_path which contains both the device and
1333 efi_dp_split_file_path(file_path, &dp, &fp);
1335 efi_setup_loaded_image(info, obj, dp, fp);
1337 /* In this case, file_path is the "device" path, ie.
1338 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1340 efi_setup_loaded_image(info, obj, file_path, NULL);
1343 info->reserved = efi_load_pe(source_buffer, info);
1344 if (!info->reserved) {
1347 return EFI_EXIT(EFI_UNSUPPORTED);
1350 info->system_table = &systab;
1351 info->parent_handle = parent_image;
1352 *image_handle = obj->handle;
1354 return EFI_EXIT(EFI_SUCCESS);
1358 * Call the entry point of an image.
1360 * This function implements the StartImage service.
1361 * See the Unified Extensible Firmware Interface (UEFI) specification
1364 * @image_handle handle of the image
1365 * @exit_data_size size of the buffer
1366 * @exit_data buffer to receive the exit data of the called image
1367 * @return status code
1369 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1370 unsigned long *exit_data_size,
1373 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1374 struct efi_loaded_image *info = image_handle;
1376 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1377 entry = info->reserved;
1379 efi_is_direct_boot = false;
1381 /* call the image! */
1382 if (setjmp(&info->exit_jmp)) {
1383 /* We returned from the child image */
1384 return EFI_EXIT(info->exit_status);
1387 __efi_nesting_dec();
1389 entry(image_handle, &systab);
1390 __efi_entry_check();
1391 __efi_nesting_inc();
1393 /* Should usually never get here */
1394 return EFI_EXIT(EFI_SUCCESS);
1398 * Leave an EFI application or driver.
1400 * This function implements the Exit service.
1401 * See the Unified Extensible Firmware Interface (UEFI) specification
1404 * @image_handle handle of the application or driver that is exiting
1405 * @exit_status status code
1406 * @exit_data_size size of the buffer in bytes
1407 * @exit_data buffer with data describing an error
1408 * @return status code
1410 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1411 efi_status_t exit_status, unsigned long exit_data_size,
1415 * We require that the handle points to the original loaded
1416 * image protocol interface.
1418 * For getting the longjmp address this is safer than locating
1419 * the protocol because the protocol may have been reinstalled
1420 * pointing to another memory location.
1422 * TODO: We should call the unload procedure of the loaded
1425 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1427 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1428 exit_data_size, exit_data);
1430 /* Make sure entry/exit counts for EFI world cross-overs match */
1434 * But longjmp out with the U-Boot gd, not the application's, as
1435 * the other end is a setjmp call inside EFI context.
1439 loaded_image_info->exit_status = exit_status;
1440 longjmp(&loaded_image_info->exit_jmp, 1);
1442 panic("EFI application exited");
1446 * Unload an EFI image.
1448 * This function implements the UnloadImage service.
1449 * See the Unified Extensible Firmware Interface (UEFI) specification
1452 * @image_handle handle of the image to be unloaded
1453 * @return status code
1455 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1457 struct efi_object *efiobj;
1459 EFI_ENTRY("%p", image_handle);
1460 efiobj = efi_search_obj(image_handle);
1462 list_del(&efiobj->link);
1464 return EFI_EXIT(EFI_SUCCESS);
1468 * Fix up caches for EFI payloads if necessary.
1470 static void efi_exit_caches(void)
1472 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1474 * Grub on 32bit ARM needs to have caches disabled before jumping into
1475 * a zImage, but does not know of all cache layers. Give it a hand.
1477 if (efi_is_direct_boot)
1478 cleanup_before_linux();
1483 * Stop boot services.
1485 * This function implements the ExitBootServices service.
1486 * See the Unified Extensible Firmware Interface (UEFI) specification
1489 * @image_handle handle of the loaded image
1490 * @map_key key of the memory map
1491 * @return status code
1493 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1494 unsigned long map_key)
1498 EFI_ENTRY("%p, %ld", image_handle, map_key);
1500 /* Notify that ExitBootServices is invoked. */
1501 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1502 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1504 efi_signal_event(&efi_events[i]);
1506 /* Make sure that notification functions are not called anymore */
1507 efi_tpl = TPL_HIGH_LEVEL;
1509 /* XXX Should persist EFI variables here */
1511 board_quiesce_devices();
1513 /* Fix up caches for EFI payloads if necessary */
1516 /* This stops all lingering devices */
1517 bootm_disable_interrupts();
1519 /* Give the payload some time to boot */
1520 efi_set_watchdog(0);
1523 return EFI_EXIT(EFI_SUCCESS);
1527 * Get next value of the counter.
1529 * This function implements the NextMonotonicCount service.
1530 * See the Unified Extensible Firmware Interface (UEFI) specification
1533 * @count returned value of the counter
1534 * @return status code
1536 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1538 static uint64_t mono = 0;
1539 EFI_ENTRY("%p", count);
1541 return EFI_EXIT(EFI_SUCCESS);
1547 * This function implements the Stall sercive.
1548 * See the Unified Extensible Firmware Interface (UEFI) specification
1551 * @microseconds period to sleep in microseconds
1552 * @return status code
1554 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1556 EFI_ENTRY("%ld", microseconds);
1557 udelay(microseconds);
1558 return EFI_EXIT(EFI_SUCCESS);
1562 * Reset the watchdog timer.
1564 * This function implements the SetWatchdogTimer service.
1565 * See the Unified Extensible Firmware Interface (UEFI) specification
1568 * @timeout seconds before reset by watchdog
1569 * @watchdog_code code to be logged when resetting
1570 * @data_size size of buffer in bytes
1571 * @watchdog_data buffer with data describing the reset reason
1572 * @return status code
1574 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1575 uint64_t watchdog_code,
1576 unsigned long data_size,
1577 uint16_t *watchdog_data)
1579 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1580 data_size, watchdog_data);
1581 return EFI_EXIT(efi_set_watchdog(timeout));
1585 * Connect a controller to a driver.
1587 * This function implements the ConnectController service.
1588 * See the Unified Extensible Firmware Interface (UEFI) specification
1591 * @controller_handle handle of the controller
1592 * @driver_image_handle handle of the driver
1593 * @remain_device_path device path of a child controller
1594 * @recursive true to connect all child controllers
1595 * @return status code
1597 static efi_status_t EFIAPI efi_connect_controller(
1598 efi_handle_t controller_handle,
1599 efi_handle_t *driver_image_handle,
1600 struct efi_device_path *remain_device_path,
1603 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1604 remain_device_path, recursive);
1605 return EFI_EXIT(EFI_NOT_FOUND);
1609 * Disconnect a controller from a driver.
1611 * This function implements the DisconnectController service.
1612 * See the Unified Extensible Firmware Interface (UEFI) specification
1615 * @controller_handle handle of the controller
1616 * @driver_image_handle handle of the driver
1617 * @child_handle handle of the child to destroy
1618 * @return status code
1620 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1621 void *driver_image_handle,
1624 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1626 return EFI_EXIT(EFI_INVALID_PARAMETER);
1632 * This function implements the CloseProtocol service.
1633 * See the Unified Extensible Firmware Interface (UEFI) specification
1636 * @handle handle on which the protocol shall be closed
1637 * @protocol GUID of the protocol to close
1638 * @agent_handle handle of the driver
1639 * @controller_handle handle of the controller
1640 * @return status code
1642 static efi_status_t EFIAPI efi_close_protocol(void *handle,
1643 const efi_guid_t *protocol,
1645 void *controller_handle)
1647 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1649 return EFI_EXIT(EFI_NOT_FOUND);
1653 * Provide information about then open status of a protocol on a handle
1655 * This function implements the OpenProtocolInformation service.
1656 * See the Unified Extensible Firmware Interface (UEFI) specification
1659 * @handle handle for which the information shall be retrieved
1660 * @protocol GUID of the protocol
1661 * @entry_buffer buffer to receive the open protocol information
1662 * @entry_count number of entries available in the buffer
1663 * @return status code
1665 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1666 const efi_guid_t *protocol,
1667 struct efi_open_protocol_info_entry **entry_buffer,
1668 efi_uintn_t *entry_count)
1670 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1672 return EFI_EXIT(EFI_NOT_FOUND);
1676 * Get protocols installed on a handle.
1678 * This function implements the ProtocolsPerHandleService.
1679 * See the Unified Extensible Firmware Interface (UEFI) specification
1682 * @handle handle for which the information is retrieved
1683 * @protocol_buffer buffer with protocol GUIDs
1684 * @protocol_buffer_count number of entries in the buffer
1685 * @return status code
1687 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1688 efi_guid_t ***protocol_buffer,
1689 efi_uintn_t *protocol_buffer_count)
1691 unsigned long buffer_size;
1692 struct efi_object *efiobj;
1693 struct list_head *protocol_handle;
1696 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1697 protocol_buffer_count);
1699 if (!handle || !protocol_buffer || !protocol_buffer_count)
1700 return EFI_EXIT(EFI_INVALID_PARAMETER);
1702 *protocol_buffer = NULL;
1703 *protocol_buffer_count = 0;
1705 efiobj = efi_search_obj(handle);
1707 return EFI_EXIT(EFI_INVALID_PARAMETER);
1709 /* Count protocols */
1710 list_for_each(protocol_handle, &efiobj->protocols) {
1711 ++*protocol_buffer_count;
1715 if (*protocol_buffer_count) {
1718 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1719 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1720 (void **)protocol_buffer);
1721 if (r != EFI_SUCCESS)
1723 list_for_each(protocol_handle, &efiobj->protocols) {
1724 struct efi_handler *protocol;
1726 protocol = list_entry(protocol_handle,
1727 struct efi_handler, link);
1728 (*protocol_buffer)[j] = (void *)protocol->guid;
1733 return EFI_EXIT(EFI_SUCCESS);
1737 * Locate handles implementing a protocol.
1739 * This function implements the LocateHandleBuffer service.
1740 * See the Unified Extensible Firmware Interface (UEFI) specification
1743 * @search_type selection criterion
1744 * @protocol GUID of the protocol
1745 * @search_key registration key
1746 * @no_handles number of returned handles
1747 * @buffer buffer with the returned handles
1748 * @return status code
1750 static efi_status_t EFIAPI efi_locate_handle_buffer(
1751 enum efi_locate_search_type search_type,
1752 const efi_guid_t *protocol, void *search_key,
1753 efi_uintn_t *no_handles, efi_handle_t **buffer)
1756 efi_uintn_t buffer_size = 0;
1758 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1759 no_handles, buffer);
1761 if (!no_handles || !buffer) {
1762 r = EFI_INVALID_PARAMETER;
1767 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1769 if (r != EFI_BUFFER_TOO_SMALL)
1771 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1773 if (r != EFI_SUCCESS)
1775 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1777 if (r == EFI_SUCCESS)
1778 *no_handles = buffer_size / sizeof(void *);
1784 * Find an interface implementing a protocol.
1786 * This function implements the LocateProtocol service.
1787 * See the Unified Extensible Firmware Interface (UEFI) specification
1790 * @protocol GUID of the protocol
1791 * @registration registration key passed to the notification function
1792 * @protocol_interface interface implementing the protocol
1793 * @return status code
1795 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
1797 void **protocol_interface)
1799 struct list_head *lhandle;
1802 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
1804 if (!protocol || !protocol_interface)
1805 return EFI_EXIT(EFI_INVALID_PARAMETER);
1807 list_for_each(lhandle, &efi_obj_list) {
1808 struct efi_object *efiobj;
1809 struct efi_handler *handler;
1811 efiobj = list_entry(lhandle, struct efi_object, link);
1813 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
1814 if (ret == EFI_SUCCESS) {
1815 *protocol_interface = handler->protocol_interface;
1816 return EFI_EXIT(EFI_SUCCESS);
1819 *protocol_interface = NULL;
1821 return EFI_EXIT(EFI_NOT_FOUND);
1825 * Get the device path and handle of an device implementing a protocol.
1827 * This function implements the LocateDevicePath service.
1828 * See the Unified Extensible Firmware Interface (UEFI) specification
1831 * @protocol GUID of the protocol
1832 * @device_path device path
1833 * @device handle of the device
1834 * @return status code
1836 static efi_status_t EFIAPI efi_locate_device_path(
1837 const efi_guid_t *protocol,
1838 struct efi_device_path **device_path,
1839 efi_handle_t *device)
1841 struct efi_device_path *dp;
1843 struct efi_handler *handler;
1844 efi_handle_t *handles;
1846 size_t len_best = 0;
1847 efi_uintn_t no_handles;
1851 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1853 if (!protocol || !device_path || !*device_path || !device) {
1854 ret = EFI_INVALID_PARAMETER;
1858 /* Find end of device path */
1859 len = efi_dp_size(*device_path);
1861 /* Get all handles implementing the protocol */
1862 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1863 &no_handles, &handles));
1864 if (ret != EFI_SUCCESS)
1867 for (i = 0; i < no_handles; ++i) {
1868 /* Find the device path protocol */
1869 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1871 if (ret != EFI_SUCCESS)
1873 dp = (struct efi_device_path *)handler->protocol_interface;
1874 len_dp = efi_dp_size(dp);
1876 * This handle can only be a better fit
1877 * if its device path length is longer than the best fit and
1878 * if its device path length is shorter of equal the searched
1881 if (len_dp <= len_best || len_dp > len)
1883 /* Check if dp is a subpath of device_path */
1884 if (memcmp(*device_path, dp, len_dp))
1886 *device = handles[i];
1890 remainder = (u8 *)*device_path + len_best;
1891 *device_path = (struct efi_device_path *)remainder;
1894 ret = EFI_NOT_FOUND;
1897 return EFI_EXIT(ret);
1901 * Install multiple protocol interfaces.
1903 * This function implements the MultipleProtocolInterfaces service.
1904 * See the Unified Extensible Firmware Interface (UEFI) specification
1907 * @handle handle on which the protocol interfaces shall be installed
1908 * @... NULL terminated argument list with pairs of protocol GUIDS and
1910 * @return status code
1912 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1915 EFI_ENTRY("%p", handle);
1918 const efi_guid_t *protocol;
1919 void *protocol_interface;
1920 efi_status_t r = EFI_SUCCESS;
1924 return EFI_EXIT(EFI_INVALID_PARAMETER);
1926 va_start(argptr, handle);
1928 protocol = va_arg(argptr, efi_guid_t*);
1931 protocol_interface = va_arg(argptr, void*);
1932 r = EFI_CALL(efi_install_protocol_interface(
1934 EFI_NATIVE_INTERFACE,
1935 protocol_interface));
1936 if (r != EFI_SUCCESS)
1941 if (r == EFI_SUCCESS)
1944 /* If an error occurred undo all changes. */
1945 va_start(argptr, handle);
1947 protocol = va_arg(argptr, efi_guid_t*);
1948 protocol_interface = va_arg(argptr, void*);
1949 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1950 protocol_interface));
1958 * Uninstall multiple protocol interfaces.
1960 * This function implements the UninstallMultipleProtocolInterfaces service.
1961 * See the Unified Extensible Firmware Interface (UEFI) specification
1964 * @handle handle from which the protocol interfaces shall be removed
1965 * @... NULL terminated argument list with pairs of protocol GUIDS and
1967 * @return status code
1969 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1972 EFI_ENTRY("%p", handle);
1975 const efi_guid_t *protocol;
1976 void *protocol_interface;
1977 efi_status_t r = EFI_SUCCESS;
1981 return EFI_EXIT(EFI_INVALID_PARAMETER);
1983 va_start(argptr, handle);
1985 protocol = va_arg(argptr, efi_guid_t*);
1988 protocol_interface = va_arg(argptr, void*);
1989 r = EFI_CALL(efi_uninstall_protocol_interface(
1991 protocol_interface));
1992 if (r != EFI_SUCCESS)
1997 if (r == EFI_SUCCESS)
2000 /* If an error occurred undo all changes. */
2001 va_start(argptr, handle);
2003 protocol = va_arg(argptr, efi_guid_t*);
2004 protocol_interface = va_arg(argptr, void*);
2005 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2006 EFI_NATIVE_INTERFACE,
2007 protocol_interface));
2015 * Calculate cyclic redundancy code.
2017 * This function implements the CalculateCrc32 service.
2018 * See the Unified Extensible Firmware Interface (UEFI) specification
2021 * @data buffer with data
2022 * @data_size size of buffer in bytes
2023 * @crc32_p cyclic redundancy code
2024 * @return status code
2026 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2027 unsigned long data_size,
2030 EFI_ENTRY("%p, %ld", data, data_size);
2031 *crc32_p = crc32(0, data, data_size);
2032 return EFI_EXIT(EFI_SUCCESS);
2038 * This function implements the CopyMem service.
2039 * See the Unified Extensible Firmware Interface (UEFI) specification
2042 * @destination destination of the copy operation
2043 * @source source of the copy operation
2044 * @length number of bytes to copy
2046 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2049 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2050 memcpy(destination, source, length);
2051 EFI_EXIT(EFI_SUCCESS);
2055 * Fill memory with a byte value.
2057 * This function implements the SetMem service.
2058 * See the Unified Extensible Firmware Interface (UEFI) specification
2061 * @buffer buffer to fill
2062 * @size size of buffer in bytes
2063 * @value byte to copy to the buffer
2065 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2067 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2068 memset(buffer, value, size);
2069 EFI_EXIT(EFI_SUCCESS);
2073 * Open protocol interface on a handle.
2075 * This function implements the OpenProtocol interface.
2076 * See the Unified Extensible Firmware Interface (UEFI) specification
2079 * @handle handle on which the protocol shall be opened
2080 * @protocol GUID of the protocol
2081 * @protocol_interface interface implementing the protocol
2082 * @agent_handle handle of the driver
2083 * @controller_handle handle of the controller
2084 * @attributes attributes indicating how to open the protocol
2085 * @return status code
2087 static efi_status_t EFIAPI efi_open_protocol(
2088 void *handle, const efi_guid_t *protocol,
2089 void **protocol_interface, void *agent_handle,
2090 void *controller_handle, uint32_t attributes)
2092 struct efi_handler *handler;
2093 efi_status_t r = EFI_INVALID_PARAMETER;
2095 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2096 protocol_interface, agent_handle, controller_handle,
2099 if (!handle || !protocol ||
2100 (!protocol_interface && attributes !=
2101 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2105 switch (attributes) {
2106 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2107 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2108 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2110 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2111 if (controller_handle == handle)
2113 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2114 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2115 if (controller_handle == NULL)
2117 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2118 if (agent_handle == NULL)
2125 r = efi_search_protocol(handle, protocol, &handler);
2126 if (r != EFI_SUCCESS)
2129 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2130 *protocol_interface = handler->protocol_interface;
2136 * Get interface of a protocol on a handle.
2138 * This function implements the HandleProtocol service.
2139 * See the Unified Extensible Firmware Interface (UEFI) specification
2142 * @handle handle on which the protocol shall be opened
2143 * @protocol GUID of the protocol
2144 * @protocol_interface interface implementing the protocol
2145 * @return status code
2147 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
2148 const efi_guid_t *protocol,
2149 void **protocol_interface)
2151 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2152 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2155 static const struct efi_boot_services efi_boot_services = {
2157 .headersize = sizeof(struct efi_table_hdr),
2159 .raise_tpl = efi_raise_tpl,
2160 .restore_tpl = efi_restore_tpl,
2161 .allocate_pages = efi_allocate_pages_ext,
2162 .free_pages = efi_free_pages_ext,
2163 .get_memory_map = efi_get_memory_map_ext,
2164 .allocate_pool = efi_allocate_pool_ext,
2165 .free_pool = efi_free_pool_ext,
2166 .create_event = efi_create_event_ext,
2167 .set_timer = efi_set_timer_ext,
2168 .wait_for_event = efi_wait_for_event,
2169 .signal_event = efi_signal_event_ext,
2170 .close_event = efi_close_event,
2171 .check_event = efi_check_event,
2172 .install_protocol_interface = efi_install_protocol_interface,
2173 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2174 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2175 .handle_protocol = efi_handle_protocol,
2177 .register_protocol_notify = efi_register_protocol_notify,
2178 .locate_handle = efi_locate_handle_ext,
2179 .locate_device_path = efi_locate_device_path,
2180 .install_configuration_table = efi_install_configuration_table_ext,
2181 .load_image = efi_load_image,
2182 .start_image = efi_start_image,
2184 .unload_image = efi_unload_image,
2185 .exit_boot_services = efi_exit_boot_services,
2186 .get_next_monotonic_count = efi_get_next_monotonic_count,
2188 .set_watchdog_timer = efi_set_watchdog_timer,
2189 .connect_controller = efi_connect_controller,
2190 .disconnect_controller = efi_disconnect_controller,
2191 .open_protocol = efi_open_protocol,
2192 .close_protocol = efi_close_protocol,
2193 .open_protocol_information = efi_open_protocol_information,
2194 .protocols_per_handle = efi_protocols_per_handle,
2195 .locate_handle_buffer = efi_locate_handle_buffer,
2196 .locate_protocol = efi_locate_protocol,
2197 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2198 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2199 .calculate_crc32 = efi_calculate_crc32,
2200 .copy_mem = efi_copy_mem,
2201 .set_mem = efi_set_mem,
2205 static uint16_t __efi_runtime_data firmware_vendor[] =
2206 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2208 struct efi_system_table __efi_runtime_data systab = {
2210 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2211 .revision = 0x20005, /* 2.5 */
2212 .headersize = sizeof(struct efi_table_hdr),
2214 .fw_vendor = (long)firmware_vendor,
2215 .con_in = (void*)&efi_con_in,
2216 .con_out = (void*)&efi_con_out,
2217 .std_err = (void*)&efi_con_out,
2218 .runtime = (void*)&efi_runtime_services,
2219 .boottime = (void*)&efi_boot_services,
2221 .tables = (void*)efi_conf_table,