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 * Our event capabilities are very limited. Only a small limited
364 * number of events is allowed to coexist.
366 static struct efi_event efi_events[16];
371 * This function is used inside U-Boot code to create an event.
373 * For the API function implementing the CreateEvent service see
374 * efi_create_event_ext.
376 * @type type of the event to create
377 * @notify_tpl task priority level of the event
378 * @notify_function notification function of the event
379 * @notify_context pointer passed to the notification function
380 * @event created event
381 * @return status code
383 efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
384 void (EFIAPI *notify_function) (
385 struct efi_event *event,
387 void *notify_context, struct efi_event **event)
392 return EFI_INVALID_PARAMETER;
394 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
395 return EFI_INVALID_PARAMETER;
397 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
398 notify_function == NULL)
399 return EFI_INVALID_PARAMETER;
401 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
402 if (efi_events[i].type)
404 efi_events[i].type = type;
405 efi_events[i].notify_tpl = notify_tpl;
406 efi_events[i].notify_function = notify_function;
407 efi_events[i].notify_context = notify_context;
408 /* Disable timers on bootup */
409 efi_events[i].trigger_next = -1ULL;
410 efi_events[i].is_queued = false;
411 efi_events[i].is_signaled = false;
412 *event = &efi_events[i];
415 return EFI_OUT_OF_RESOURCES;
421 * This function implements the CreateEvent service.
422 * See the Unified Extensible Firmware Interface (UEFI) specification
425 * @type type of the event to create
426 * @notify_tpl task priority level of the event
427 * @notify_function notification function of the event
428 * @notify_context pointer passed to the notification function
429 * @event created event
430 * @return status code
432 static efi_status_t EFIAPI efi_create_event_ext(
433 uint32_t type, efi_uintn_t notify_tpl,
434 void (EFIAPI *notify_function) (
435 struct efi_event *event,
437 void *notify_context, struct efi_event **event)
439 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
441 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
442 notify_context, event));
447 * Check if a timer event has occurred or a queued notification function should
450 * Our timers have to work without interrupts, so we check whenever keyboard
451 * input or disk accesses happen if enough time elapsed for them to fire.
453 void efi_timer_check(void)
456 u64 now = timer_get_us();
458 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
459 if (!efi_events[i].type)
461 if (efi_events[i].is_queued)
462 efi_signal_event(&efi_events[i]);
463 if (!(efi_events[i].type & EVT_TIMER) ||
464 now < efi_events[i].trigger_next)
466 switch (efi_events[i].trigger_type) {
467 case EFI_TIMER_RELATIVE:
468 efi_events[i].trigger_type = EFI_TIMER_STOP;
470 case EFI_TIMER_PERIODIC:
471 efi_events[i].trigger_next +=
472 efi_events[i].trigger_time;
477 efi_events[i].is_signaled = true;
478 efi_signal_event(&efi_events[i]);
484 * Set the trigger time for a timer event or stop the event.
486 * This is the function for internal usage in U-Boot. For the API function
487 * implementing the SetTimer service see efi_set_timer_ext.
489 * @event event for which the timer is set
490 * @type type of the timer
491 * @trigger_time trigger period in multiples of 100ns
492 * @return status code
494 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
495 uint64_t trigger_time)
500 * The parameter defines a multiple of 100ns.
501 * We use multiples of 1000ns. So divide by 10.
503 do_div(trigger_time, 10);
505 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
506 if (event != &efi_events[i])
509 if (!(event->type & EVT_TIMER))
513 event->trigger_next = -1ULL;
515 case EFI_TIMER_PERIODIC:
516 case EFI_TIMER_RELATIVE:
517 event->trigger_next =
518 timer_get_us() + trigger_time;
521 return EFI_INVALID_PARAMETER;
523 event->trigger_type = type;
524 event->trigger_time = trigger_time;
525 event->is_signaled = false;
528 return EFI_INVALID_PARAMETER;
532 * Set the trigger time for a timer event or stop the event.
534 * This function implements the SetTimer service.
535 * See the Unified Extensible Firmware Interface (UEFI) specification
538 * @event event for which the timer is set
539 * @type type of the timer
540 * @trigger_time trigger period in multiples of 100ns
541 * @return status code
543 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
544 enum efi_timer_delay type,
545 uint64_t trigger_time)
547 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
548 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
552 * Wait for events to be signaled.
554 * This function implements the WaitForEvent service.
555 * See the Unified Extensible Firmware Interface (UEFI) specification
558 * @num_events number of events to be waited for
559 * @events events to be waited for
560 * @index index of the event that was signaled
561 * @return status code
563 static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
564 struct efi_event **event,
569 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
571 /* Check parameters */
572 if (!num_events || !event)
573 return EFI_EXIT(EFI_INVALID_PARAMETER);
575 if (efi_tpl != TPL_APPLICATION)
576 return EFI_EXIT(EFI_UNSUPPORTED);
577 for (i = 0; i < num_events; ++i) {
578 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
579 if (event[i] == &efi_events[j])
582 return EFI_EXIT(EFI_INVALID_PARAMETER);
584 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
585 return EFI_EXIT(EFI_INVALID_PARAMETER);
586 if (!event[i]->is_signaled)
587 efi_signal_event(event[i]);
590 /* Wait for signal */
592 for (i = 0; i < num_events; ++i) {
593 if (event[i]->is_signaled)
596 /* Allow events to occur. */
602 * Reset the signal which is passed to the caller to allow periodic
605 event[i]->is_signaled = false;
609 return EFI_EXIT(EFI_SUCCESS);
613 * Signal an EFI event.
615 * This function implements the SignalEvent service.
616 * See the Unified Extensible Firmware Interface (UEFI) specification
619 * This functions sets the signaled state of the event and queues the
620 * notification function for execution.
622 * @event event to signal
623 * @return status code
625 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
629 EFI_ENTRY("%p", event);
630 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
631 if (event != &efi_events[i])
633 if (event->is_signaled)
635 event->is_signaled = true;
636 if (event->type & EVT_NOTIFY_SIGNAL)
637 efi_signal_event(event);
640 return EFI_EXIT(EFI_SUCCESS);
644 * Close an EFI event.
646 * This function implements the CloseEvent service.
647 * See the Unified Extensible Firmware Interface (UEFI) specification
650 * @event event to close
651 * @return status code
653 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
657 EFI_ENTRY("%p", event);
658 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
659 if (event == &efi_events[i]) {
661 event->trigger_next = -1ULL;
662 event->is_queued = false;
663 event->is_signaled = false;
664 return EFI_EXIT(EFI_SUCCESS);
667 return EFI_EXIT(EFI_INVALID_PARAMETER);
671 * Check if an event is signaled.
673 * This function implements the CheckEvent service.
674 * See the Unified Extensible Firmware Interface (UEFI) specification
677 * If an event is not signaled yet the notification function is queued.
679 * @event event to check
680 * @return status code
682 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
686 EFI_ENTRY("%p", event);
688 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
689 if (event != &efi_events[i])
691 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
693 if (!event->is_signaled)
694 efi_signal_event(event);
695 if (event->is_signaled)
696 return EFI_EXIT(EFI_SUCCESS);
697 return EFI_EXIT(EFI_NOT_READY);
699 return EFI_EXIT(EFI_INVALID_PARAMETER);
703 * Find the internal EFI object for a handle.
705 * @handle handle to find
708 struct efi_object *efi_search_obj(const void *handle)
710 struct efi_object *efiobj;
712 list_for_each_entry(efiobj, &efi_obj_list, link) {
713 if (efiobj->handle == handle)
721 * Find a protocol on a handle.
724 * @protocol_guid GUID of the protocol
725 * @handler reference to the protocol
726 * @return status code
728 efi_status_t efi_search_protocol(const void *handle,
729 const efi_guid_t *protocol_guid,
730 struct efi_handler **handler)
732 struct efi_object *efiobj;
733 struct list_head *lhandle;
735 if (!handle || !protocol_guid)
736 return EFI_INVALID_PARAMETER;
737 efiobj = efi_search_obj(handle);
739 return EFI_INVALID_PARAMETER;
740 list_for_each(lhandle, &efiobj->protocols) {
741 struct efi_handler *protocol;
743 protocol = list_entry(lhandle, struct efi_handler, link);
744 if (!guidcmp(protocol->guid, protocol_guid)) {
750 return EFI_NOT_FOUND;
754 * Install new protocol on a handle.
756 * @handle handle on which the protocol shall be installed
757 * @protocol GUID of the protocol to be installed
758 * @protocol_interface interface of the protocol implementation
759 * @return status code
761 efi_status_t efi_add_protocol(const void *handle, const efi_guid_t *protocol,
762 void *protocol_interface)
764 struct efi_object *efiobj;
765 struct efi_handler *handler;
768 efiobj = efi_search_obj(handle);
770 return EFI_INVALID_PARAMETER;
771 ret = efi_search_protocol(handle, protocol, NULL);
772 if (ret != EFI_NOT_FOUND)
773 return EFI_INVALID_PARAMETER;
774 handler = calloc(1, sizeof(struct efi_handler));
776 return EFI_OUT_OF_RESOURCES;
777 handler->guid = protocol;
778 handler->protocol_interface = protocol_interface;
779 list_add_tail(&handler->link, &efiobj->protocols);
784 * Delete protocol from a handle.
786 * @handle handle from which the protocol shall be deleted
787 * @protocol GUID of the protocol to be deleted
788 * @protocol_interface interface of the protocol implementation
789 * @return status code
791 efi_status_t efi_remove_protocol(const void *handle, const efi_guid_t *protocol,
792 void *protocol_interface)
794 struct efi_handler *handler;
797 ret = efi_search_protocol(handle, protocol, &handler);
798 if (ret != EFI_SUCCESS)
800 if (guidcmp(handler->guid, protocol))
801 return EFI_INVALID_PARAMETER;
802 list_del(&handler->link);
808 * Delete all protocols from a handle.
810 * @handle handle from which the protocols shall be deleted
811 * @return status code
813 efi_status_t efi_remove_all_protocols(const void *handle)
815 struct efi_object *efiobj;
816 struct list_head *lhandle;
817 struct list_head *pos;
819 efiobj = efi_search_obj(handle);
821 return EFI_INVALID_PARAMETER;
822 list_for_each_safe(lhandle, pos, &efiobj->protocols) {
823 struct efi_handler *protocol;
826 protocol = list_entry(lhandle, struct efi_handler, link);
828 ret = efi_remove_protocol(handle, protocol->guid,
829 protocol->protocol_interface);
830 if (ret != EFI_SUCCESS)
837 * Install protocol interface.
839 * This function implements the InstallProtocolInterface service.
840 * See the Unified Extensible Firmware Interface (UEFI) specification
843 * @handle handle on which the protocol shall be installed
844 * @protocol GUID of the protocol to be installed
845 * @protocol_interface_type type of the interface to be installed,
846 * always EFI_NATIVE_INTERFACE
847 * @protocol_interface interface of the protocol implementation
848 * @return status code
850 static efi_status_t EFIAPI efi_install_protocol_interface(
851 void **handle, const efi_guid_t *protocol,
852 int protocol_interface_type, void *protocol_interface)
856 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
859 if (!handle || !protocol ||
860 protocol_interface_type != EFI_NATIVE_INTERFACE) {
861 r = EFI_INVALID_PARAMETER;
865 /* Create new handle if requested. */
867 r = efi_create_handle(handle);
868 if (r != EFI_SUCCESS)
870 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
873 debug("%sEFI: handle %p\n", indent_string(nesting_level),
876 /* Add new protocol */
877 r = efi_add_protocol(*handle, protocol, protocol_interface);
883 * Reinstall protocol interface.
885 * This function implements the ReinstallProtocolInterface service.
886 * See the Unified Extensible Firmware Interface (UEFI) specification
889 * @handle handle on which the protocol shall be
891 * @protocol GUID of the protocol to be installed
892 * @old_interface interface to be removed
893 * @new_interface interface to be installed
894 * @return status code
896 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
897 const efi_guid_t *protocol, void *old_interface,
900 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
902 return EFI_EXIT(EFI_ACCESS_DENIED);
906 * Uninstall protocol interface.
908 * This function implements the UninstallProtocolInterface service.
909 * See the Unified Extensible Firmware Interface (UEFI) specification
912 * @handle handle from which the protocol shall be removed
913 * @protocol GUID of the protocol to be removed
914 * @protocol_interface interface to be removed
915 * @return status code
917 static efi_status_t EFIAPI efi_uninstall_protocol_interface(
918 void *handle, const efi_guid_t *protocol,
919 void *protocol_interface)
921 struct efi_handler *handler;
924 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
926 if (!handle || !protocol) {
927 r = EFI_INVALID_PARAMETER;
931 /* Find the protocol on the handle */
932 r = efi_search_protocol(handle, protocol, &handler);
933 if (r != EFI_SUCCESS)
935 if (handler->protocol_interface) {
936 /* TODO disconnect controllers */
937 r = EFI_ACCESS_DENIED;
939 r = efi_remove_protocol(handle, protocol, protocol_interface);
946 * Register an event for notification when a protocol is installed.
948 * This function implements the RegisterProtocolNotify service.
949 * See the Unified Extensible Firmware Interface (UEFI) specification
952 * @protocol GUID of the protocol whose installation shall be
954 * @event event to be signaled upon installation of the protocol
955 * @registration key for retrieving the registration information
956 * @return status code
958 static efi_status_t EFIAPI efi_register_protocol_notify(
959 const efi_guid_t *protocol,
960 struct efi_event *event,
963 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
964 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
968 * Determine if an EFI handle implements a protocol.
970 * See the documentation of the LocateHandle service in the UEFI specification.
972 * @search_type selection criterion
973 * @protocol GUID of the protocol
974 * @search_key registration key
976 * @return 0 if the handle implements the protocol
978 static int efi_search(enum efi_locate_search_type search_type,
979 const efi_guid_t *protocol, void *search_key,
980 struct efi_object *efiobj)
984 switch (search_type) {
987 case BY_REGISTER_NOTIFY:
988 /* TODO: RegisterProtocolNotify is not implemented yet */
991 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
992 return (ret != EFI_SUCCESS);
994 /* Invalid search type */
1000 * Locate handles implementing a protocol.
1002 * This function is meant for U-Boot internal calls. For the API implementation
1003 * of the LocateHandle service see efi_locate_handle_ext.
1005 * @search_type selection criterion
1006 * @protocol GUID of the protocol
1007 * @search_key registration key
1008 * @buffer_size size of the buffer to receive the handles in bytes
1009 * @buffer buffer to receive the relevant handles
1010 * @return status code
1012 static efi_status_t efi_locate_handle(
1013 enum efi_locate_search_type search_type,
1014 const efi_guid_t *protocol, void *search_key,
1015 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1017 struct efi_object *efiobj;
1018 efi_uintn_t size = 0;
1020 /* Check parameters */
1021 switch (search_type) {
1024 case BY_REGISTER_NOTIFY:
1026 return EFI_INVALID_PARAMETER;
1027 /* RegisterProtocolNotify is not implemented yet */
1028 return EFI_UNSUPPORTED;
1031 return EFI_INVALID_PARAMETER;
1034 return EFI_INVALID_PARAMETER;
1038 * efi_locate_handle_buffer uses this function for
1039 * the calculation of the necessary buffer size.
1040 * So do not require a buffer for buffersize == 0.
1042 if (!buffer_size || (*buffer_size && !buffer))
1043 return EFI_INVALID_PARAMETER;
1045 /* Count how much space we need */
1046 list_for_each_entry(efiobj, &efi_obj_list, link) {
1047 if (!efi_search(search_type, protocol, search_key, efiobj))
1048 size += sizeof(void*);
1051 if (*buffer_size < size) {
1052 *buffer_size = size;
1053 return EFI_BUFFER_TOO_SMALL;
1056 *buffer_size = size;
1058 return EFI_NOT_FOUND;
1060 /* Then fill the array */
1061 list_for_each_entry(efiobj, &efi_obj_list, link) {
1062 if (!efi_search(search_type, protocol, search_key, efiobj))
1063 *buffer++ = efiobj->handle;
1070 * Locate handles implementing a protocol.
1072 * This function implements the LocateHandle service.
1073 * See the Unified Extensible Firmware Interface (UEFI) specification
1076 * @search_type selection criterion
1077 * @protocol GUID of the protocol
1078 * @search_key registration key
1079 * @buffer_size size of the buffer to receive the handles in bytes
1080 * @buffer buffer to receive the relevant handles
1081 * @return 0 if the handle implements the protocol
1083 static efi_status_t EFIAPI efi_locate_handle_ext(
1084 enum efi_locate_search_type search_type,
1085 const efi_guid_t *protocol, void *search_key,
1086 efi_uintn_t *buffer_size, efi_handle_t *buffer)
1088 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1089 buffer_size, buffer);
1091 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1092 buffer_size, buffer));
1095 /* Collapses configuration table entries, removing index i */
1096 static void efi_remove_configuration_table(int i)
1098 struct efi_configuration_table *this = &efi_conf_table[i];
1099 struct efi_configuration_table *next = &efi_conf_table[i+1];
1100 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1102 memmove(this, next, (ulong)end - (ulong)next);
1107 * Adds, updates, or removes a configuration table.
1109 * This function is used for internal calls. For the API implementation of the
1110 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1112 * @guid GUID of the installed table
1113 * @table table to be installed
1114 * @return status code
1116 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
1120 /* Check for guid override */
1121 for (i = 0; i < systab.nr_tables; i++) {
1122 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1124 efi_conf_table[i].table = table;
1126 efi_remove_configuration_table(i);
1132 return EFI_NOT_FOUND;
1134 /* No override, check for overflow */
1135 if (i >= ARRAY_SIZE(efi_conf_table))
1136 return EFI_OUT_OF_RESOURCES;
1138 /* Add a new entry */
1139 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1140 efi_conf_table[i].table = table;
1141 systab.nr_tables = i + 1;
1147 * Adds, updates, or removes a configuration table.
1149 * This function implements the InstallConfigurationTable service.
1150 * See the Unified Extensible Firmware Interface (UEFI) specification
1153 * @guid GUID of the installed table
1154 * @table table to be installed
1155 * @return status code
1157 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1160 EFI_ENTRY("%pUl, %p", guid, table);
1161 return EFI_EXIT(efi_install_configuration_table(guid, table));
1165 * Initialize a loaded_image_info + loaded_image_info object with correct
1166 * protocols, boot-device, etc.
1168 * @info loaded image info to be passed to the entry point of the
1170 * @obj internal object associated with the loaded image
1171 * @device_path device path of the loaded image
1172 * @file_path file path of the loaded image
1174 void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
1175 struct efi_device_path *device_path,
1176 struct efi_device_path *file_path)
1180 /* Add internal object to object list */
1181 efi_add_handle(obj);
1182 /* efi_exit() assumes that the handle points to the info */
1185 info->file_path = file_path;
1187 info->device_handle = efi_dp_find_obj(device_path, NULL);
1190 * When asking for the device path interface, return
1191 * bootefi_device_path
1193 ret = efi_add_protocol(obj->handle, &efi_guid_device_path, device_path);
1194 if (ret != EFI_SUCCESS)
1198 * When asking for the loaded_image interface, just
1199 * return handle which points to loaded_image_info
1201 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1202 if (ret != EFI_SUCCESS)
1205 ret = efi_add_protocol(obj->handle, &efi_guid_console_control,
1206 (void *)&efi_console_control);
1207 if (ret != EFI_SUCCESS)
1210 ret = efi_add_protocol(obj->handle,
1211 &efi_guid_device_path_to_text_protocol,
1212 (void *)&efi_device_path_to_text);
1213 if (ret != EFI_SUCCESS)
1218 printf("ERROR: Failure to install protocols for loaded image\n");
1222 * Load an image using a file path.
1224 * @file_path the path of the image to load
1225 * @buffer buffer containing the loaded image
1226 * @return status code
1228 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1231 struct efi_file_info *info = NULL;
1232 struct efi_file_handle *f;
1233 static efi_status_t ret;
1236 f = efi_file_from_path(file_path);
1238 return EFI_DEVICE_ERROR;
1241 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1243 if (ret == EFI_BUFFER_TOO_SMALL) {
1245 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1248 if (ret != EFI_SUCCESS)
1251 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1255 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1259 EFI_CALL(f->close(f));
1261 if (ret != EFI_SUCCESS) {
1262 efi_free_pool(*buffer);
1270 * Load an EFI image into memory.
1272 * This function implements the LoadImage service.
1273 * See the Unified Extensible Firmware Interface (UEFI) specification
1276 * @boot_policy true for request originating from the boot manager
1277 * @parent_image the calles's image handle
1278 * @file_path the path of the image to load
1279 * @source_buffer memory location from which the image is installed
1280 * @source_size size of the memory area from which the image is
1282 * @image_handle handle for the newly installed image
1283 * @return status code
1285 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1286 efi_handle_t parent_image,
1287 struct efi_device_path *file_path,
1288 void *source_buffer,
1289 unsigned long source_size,
1290 efi_handle_t *image_handle)
1292 struct efi_loaded_image *info;
1293 struct efi_object *obj;
1295 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1296 file_path, source_buffer, source_size, image_handle);
1298 info = calloc(1, sizeof(*info));
1299 obj = calloc(1, sizeof(*obj));
1301 if (!source_buffer) {
1302 struct efi_device_path *dp, *fp;
1305 ret = efi_load_image_from_path(file_path, &source_buffer);
1306 if (ret != EFI_SUCCESS) {
1309 return EFI_EXIT(ret);
1313 * split file_path which contains both the device and
1316 efi_dp_split_file_path(file_path, &dp, &fp);
1318 efi_setup_loaded_image(info, obj, dp, fp);
1320 /* In this case, file_path is the "device" path, ie.
1321 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1323 efi_setup_loaded_image(info, obj, file_path, NULL);
1326 info->reserved = efi_load_pe(source_buffer, info);
1327 if (!info->reserved) {
1330 return EFI_EXIT(EFI_UNSUPPORTED);
1333 info->system_table = &systab;
1334 info->parent_handle = parent_image;
1335 *image_handle = obj->handle;
1337 return EFI_EXIT(EFI_SUCCESS);
1341 * Call the entry point of an image.
1343 * This function implements the StartImage service.
1344 * See the Unified Extensible Firmware Interface (UEFI) specification
1347 * @image_handle handle of the image
1348 * @exit_data_size size of the buffer
1349 * @exit_data buffer to receive the exit data of the called image
1350 * @return status code
1352 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1353 unsigned long *exit_data_size,
1356 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1357 struct efi_loaded_image *info = image_handle;
1359 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1360 entry = info->reserved;
1362 efi_is_direct_boot = false;
1364 /* call the image! */
1365 if (setjmp(&info->exit_jmp)) {
1366 /* We returned from the child image */
1367 return EFI_EXIT(info->exit_status);
1370 __efi_nesting_dec();
1372 entry(image_handle, &systab);
1373 __efi_entry_check();
1374 __efi_nesting_inc();
1376 /* Should usually never get here */
1377 return EFI_EXIT(EFI_SUCCESS);
1381 * Leave an EFI application or driver.
1383 * This function implements the Exit service.
1384 * See the Unified Extensible Firmware Interface (UEFI) specification
1387 * @image_handle handle of the application or driver that is exiting
1388 * @exit_status status code
1389 * @exit_data_size size of the buffer in bytes
1390 * @exit_data buffer with data describing an error
1391 * @return status code
1393 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1394 efi_status_t exit_status, unsigned long exit_data_size,
1398 * We require that the handle points to the original loaded
1399 * image protocol interface.
1401 * For getting the longjmp address this is safer than locating
1402 * the protocol because the protocol may have been reinstalled
1403 * pointing to another memory location.
1405 * TODO: We should call the unload procedure of the loaded
1408 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1410 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1411 exit_data_size, exit_data);
1413 /* Make sure entry/exit counts for EFI world cross-overs match */
1417 * But longjmp out with the U-Boot gd, not the application's, as
1418 * the other end is a setjmp call inside EFI context.
1422 loaded_image_info->exit_status = exit_status;
1423 longjmp(&loaded_image_info->exit_jmp, 1);
1425 panic("EFI application exited");
1429 * Unload an EFI image.
1431 * This function implements the UnloadImage service.
1432 * See the Unified Extensible Firmware Interface (UEFI) specification
1435 * @image_handle handle of the image to be unloaded
1436 * @return status code
1438 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1440 struct efi_object *efiobj;
1442 EFI_ENTRY("%p", image_handle);
1443 efiobj = efi_search_obj(image_handle);
1445 list_del(&efiobj->link);
1447 return EFI_EXIT(EFI_SUCCESS);
1451 * Fix up caches for EFI payloads if necessary.
1453 static void efi_exit_caches(void)
1455 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1457 * Grub on 32bit ARM needs to have caches disabled before jumping into
1458 * a zImage, but does not know of all cache layers. Give it a hand.
1460 if (efi_is_direct_boot)
1461 cleanup_before_linux();
1466 * Stop boot services.
1468 * This function implements the ExitBootServices service.
1469 * See the Unified Extensible Firmware Interface (UEFI) specification
1472 * @image_handle handle of the loaded image
1473 * @map_key key of the memory map
1474 * @return status code
1476 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1477 unsigned long map_key)
1481 EFI_ENTRY("%p, %ld", image_handle, map_key);
1483 /* Notify that ExitBootServices is invoked. */
1484 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1485 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1487 efi_signal_event(&efi_events[i]);
1489 /* Make sure that notification functions are not called anymore */
1490 efi_tpl = TPL_HIGH_LEVEL;
1492 /* XXX Should persist EFI variables here */
1494 board_quiesce_devices();
1496 /* Fix up caches for EFI payloads if necessary */
1499 /* This stops all lingering devices */
1500 bootm_disable_interrupts();
1502 /* Give the payload some time to boot */
1503 efi_set_watchdog(0);
1506 return EFI_EXIT(EFI_SUCCESS);
1510 * Get next value of the counter.
1512 * This function implements the NextMonotonicCount service.
1513 * See the Unified Extensible Firmware Interface (UEFI) specification
1516 * @count returned value of the counter
1517 * @return status code
1519 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1521 static uint64_t mono = 0;
1522 EFI_ENTRY("%p", count);
1524 return EFI_EXIT(EFI_SUCCESS);
1530 * This function implements the Stall sercive.
1531 * See the Unified Extensible Firmware Interface (UEFI) specification
1534 * @microseconds period to sleep in microseconds
1535 * @return status code
1537 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1539 EFI_ENTRY("%ld", microseconds);
1540 udelay(microseconds);
1541 return EFI_EXIT(EFI_SUCCESS);
1545 * Reset the watchdog timer.
1547 * This function implements the SetWatchdogTimer service.
1548 * See the Unified Extensible Firmware Interface (UEFI) specification
1551 * @timeout seconds before reset by watchdog
1552 * @watchdog_code code to be logged when resetting
1553 * @data_size size of buffer in bytes
1554 * @watchdog_data buffer with data describing the reset reason
1555 * @return status code
1557 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1558 uint64_t watchdog_code,
1559 unsigned long data_size,
1560 uint16_t *watchdog_data)
1562 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1563 data_size, watchdog_data);
1564 return EFI_EXIT(efi_set_watchdog(timeout));
1568 * Connect a controller to a driver.
1570 * This function implements the ConnectController service.
1571 * See the Unified Extensible Firmware Interface (UEFI) specification
1574 * @controller_handle handle of the controller
1575 * @driver_image_handle handle of the driver
1576 * @remain_device_path device path of a child controller
1577 * @recursive true to connect all child controllers
1578 * @return status code
1580 static efi_status_t EFIAPI efi_connect_controller(
1581 efi_handle_t controller_handle,
1582 efi_handle_t *driver_image_handle,
1583 struct efi_device_path *remain_device_path,
1586 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1587 remain_device_path, recursive);
1588 return EFI_EXIT(EFI_NOT_FOUND);
1592 * Disconnect a controller from a driver.
1594 * This function implements the DisconnectController service.
1595 * See the Unified Extensible Firmware Interface (UEFI) specification
1598 * @controller_handle handle of the controller
1599 * @driver_image_handle handle of the driver
1600 * @child_handle handle of the child to destroy
1601 * @return status code
1603 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1604 void *driver_image_handle,
1607 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1609 return EFI_EXIT(EFI_INVALID_PARAMETER);
1615 * This function implements the CloseProtocol service.
1616 * See the Unified Extensible Firmware Interface (UEFI) specification
1619 * @handle handle on which the protocol shall be closed
1620 * @protocol GUID of the protocol to close
1621 * @agent_handle handle of the driver
1622 * @controller_handle handle of the controller
1623 * @return status code
1625 static efi_status_t EFIAPI efi_close_protocol(void *handle,
1626 const efi_guid_t *protocol,
1628 void *controller_handle)
1630 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1632 return EFI_EXIT(EFI_NOT_FOUND);
1636 * Provide information about then open status of a protocol on a handle
1638 * This function implements the OpenProtocolInformation service.
1639 * See the Unified Extensible Firmware Interface (UEFI) specification
1642 * @handle handle for which the information shall be retrieved
1643 * @protocol GUID of the protocol
1644 * @entry_buffer buffer to receive the open protocol information
1645 * @entry_count number of entries available in the buffer
1646 * @return status code
1648 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1649 const efi_guid_t *protocol,
1650 struct efi_open_protocol_info_entry **entry_buffer,
1651 efi_uintn_t *entry_count)
1653 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1655 return EFI_EXIT(EFI_NOT_FOUND);
1659 * Get protocols installed on a handle.
1661 * This function implements the ProtocolsPerHandleService.
1662 * See the Unified Extensible Firmware Interface (UEFI) specification
1665 * @handle handle for which the information is retrieved
1666 * @protocol_buffer buffer with protocol GUIDs
1667 * @protocol_buffer_count number of entries in the buffer
1668 * @return status code
1670 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1671 efi_guid_t ***protocol_buffer,
1672 efi_uintn_t *protocol_buffer_count)
1674 unsigned long buffer_size;
1675 struct efi_object *efiobj;
1676 struct list_head *protocol_handle;
1679 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1680 protocol_buffer_count);
1682 if (!handle || !protocol_buffer || !protocol_buffer_count)
1683 return EFI_EXIT(EFI_INVALID_PARAMETER);
1685 *protocol_buffer = NULL;
1686 *protocol_buffer_count = 0;
1688 efiobj = efi_search_obj(handle);
1690 return EFI_EXIT(EFI_INVALID_PARAMETER);
1692 /* Count protocols */
1693 list_for_each(protocol_handle, &efiobj->protocols) {
1694 ++*protocol_buffer_count;
1698 if (*protocol_buffer_count) {
1701 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
1702 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1703 (void **)protocol_buffer);
1704 if (r != EFI_SUCCESS)
1706 list_for_each(protocol_handle, &efiobj->protocols) {
1707 struct efi_handler *protocol;
1709 protocol = list_entry(protocol_handle,
1710 struct efi_handler, link);
1711 (*protocol_buffer)[j] = (void *)protocol->guid;
1716 return EFI_EXIT(EFI_SUCCESS);
1720 * Locate handles implementing a protocol.
1722 * This function implements the LocateHandleBuffer service.
1723 * See the Unified Extensible Firmware Interface (UEFI) specification
1726 * @search_type selection criterion
1727 * @protocol GUID of the protocol
1728 * @search_key registration key
1729 * @no_handles number of returned handles
1730 * @buffer buffer with the returned handles
1731 * @return status code
1733 static efi_status_t EFIAPI efi_locate_handle_buffer(
1734 enum efi_locate_search_type search_type,
1735 const efi_guid_t *protocol, void *search_key,
1736 efi_uintn_t *no_handles, efi_handle_t **buffer)
1739 efi_uintn_t buffer_size = 0;
1741 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1742 no_handles, buffer);
1744 if (!no_handles || !buffer) {
1745 r = EFI_INVALID_PARAMETER;
1750 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1752 if (r != EFI_BUFFER_TOO_SMALL)
1754 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1756 if (r != EFI_SUCCESS)
1758 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1760 if (r == EFI_SUCCESS)
1761 *no_handles = buffer_size / sizeof(void *);
1767 * Find an interface implementing a protocol.
1769 * This function implements the LocateProtocol service.
1770 * See the Unified Extensible Firmware Interface (UEFI) specification
1773 * @protocol GUID of the protocol
1774 * @registration registration key passed to the notification function
1775 * @protocol_interface interface implementing the protocol
1776 * @return status code
1778 static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
1780 void **protocol_interface)
1782 struct list_head *lhandle;
1785 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
1787 if (!protocol || !protocol_interface)
1788 return EFI_EXIT(EFI_INVALID_PARAMETER);
1790 list_for_each(lhandle, &efi_obj_list) {
1791 struct efi_object *efiobj;
1792 struct efi_handler *handler;
1794 efiobj = list_entry(lhandle, struct efi_object, link);
1796 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
1797 if (ret == EFI_SUCCESS) {
1798 *protocol_interface = handler->protocol_interface;
1799 return EFI_EXIT(EFI_SUCCESS);
1802 *protocol_interface = NULL;
1804 return EFI_EXIT(EFI_NOT_FOUND);
1808 * Get the device path and handle of an device implementing a protocol.
1810 * This function implements the LocateDevicePath service.
1811 * See the Unified Extensible Firmware Interface (UEFI) specification
1814 * @protocol GUID of the protocol
1815 * @device_path device path
1816 * @device handle of the device
1817 * @return status code
1819 static efi_status_t EFIAPI efi_locate_device_path(
1820 const efi_guid_t *protocol,
1821 struct efi_device_path **device_path,
1822 efi_handle_t *device)
1824 struct efi_device_path *dp;
1826 struct efi_handler *handler;
1827 efi_handle_t *handles;
1829 size_t len_best = 0;
1830 efi_uintn_t no_handles;
1834 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1836 if (!protocol || !device_path || !*device_path || !device) {
1837 ret = EFI_INVALID_PARAMETER;
1841 /* Find end of device path */
1842 len = efi_dp_size(*device_path);
1844 /* Get all handles implementing the protocol */
1845 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1846 &no_handles, &handles));
1847 if (ret != EFI_SUCCESS)
1850 for (i = 0; i < no_handles; ++i) {
1851 /* Find the device path protocol */
1852 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1854 if (ret != EFI_SUCCESS)
1856 dp = (struct efi_device_path *)handler->protocol_interface;
1857 len_dp = efi_dp_size(dp);
1859 * This handle can only be a better fit
1860 * if its device path length is longer than the best fit and
1861 * if its device path length is shorter of equal the searched
1864 if (len_dp <= len_best || len_dp > len)
1866 /* Check if dp is a subpath of device_path */
1867 if (memcmp(*device_path, dp, len_dp))
1869 *device = handles[i];
1873 remainder = (u8 *)*device_path + len_best;
1874 *device_path = (struct efi_device_path *)remainder;
1877 ret = EFI_NOT_FOUND;
1880 return EFI_EXIT(ret);
1884 * Install multiple protocol interfaces.
1886 * This function implements the MultipleProtocolInterfaces service.
1887 * See the Unified Extensible Firmware Interface (UEFI) specification
1890 * @handle handle on which the protocol interfaces shall be installed
1891 * @... NULL terminated argument list with pairs of protocol GUIDS and
1893 * @return status code
1895 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1898 EFI_ENTRY("%p", handle);
1901 const efi_guid_t *protocol;
1902 void *protocol_interface;
1903 efi_status_t r = EFI_SUCCESS;
1907 return EFI_EXIT(EFI_INVALID_PARAMETER);
1909 va_start(argptr, handle);
1911 protocol = va_arg(argptr, efi_guid_t*);
1914 protocol_interface = va_arg(argptr, void*);
1915 r = EFI_CALL(efi_install_protocol_interface(
1917 EFI_NATIVE_INTERFACE,
1918 protocol_interface));
1919 if (r != EFI_SUCCESS)
1924 if (r == EFI_SUCCESS)
1927 /* If an error occurred undo all changes. */
1928 va_start(argptr, handle);
1930 protocol = va_arg(argptr, efi_guid_t*);
1931 protocol_interface = va_arg(argptr, void*);
1932 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
1933 protocol_interface));
1941 * Uninstall multiple protocol interfaces.
1943 * This function implements the UninstallMultipleProtocolInterfaces service.
1944 * See the Unified Extensible Firmware Interface (UEFI) specification
1947 * @handle handle from which the protocol interfaces shall be removed
1948 * @... NULL terminated argument list with pairs of protocol GUIDS and
1950 * @return status code
1952 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1955 EFI_ENTRY("%p", handle);
1958 const efi_guid_t *protocol;
1959 void *protocol_interface;
1960 efi_status_t r = EFI_SUCCESS;
1964 return EFI_EXIT(EFI_INVALID_PARAMETER);
1966 va_start(argptr, handle);
1968 protocol = va_arg(argptr, efi_guid_t*);
1971 protocol_interface = va_arg(argptr, void*);
1972 r = EFI_CALL(efi_uninstall_protocol_interface(
1974 protocol_interface));
1975 if (r != EFI_SUCCESS)
1980 if (r == EFI_SUCCESS)
1983 /* If an error occurred undo all changes. */
1984 va_start(argptr, handle);
1986 protocol = va_arg(argptr, efi_guid_t*);
1987 protocol_interface = va_arg(argptr, void*);
1988 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
1989 EFI_NATIVE_INTERFACE,
1990 protocol_interface));
1998 * Calculate cyclic redundancy code.
2000 * This function implements the CalculateCrc32 service.
2001 * See the Unified Extensible Firmware Interface (UEFI) specification
2004 * @data buffer with data
2005 * @data_size size of buffer in bytes
2006 * @crc32_p cyclic redundancy code
2007 * @return status code
2009 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2010 unsigned long data_size,
2013 EFI_ENTRY("%p, %ld", data, data_size);
2014 *crc32_p = crc32(0, data, data_size);
2015 return EFI_EXIT(EFI_SUCCESS);
2021 * This function implements the CopyMem service.
2022 * See the Unified Extensible Firmware Interface (UEFI) specification
2025 * @destination destination of the copy operation
2026 * @source source of the copy operation
2027 * @length number of bytes to copy
2029 static void EFIAPI efi_copy_mem(void *destination, const void *source,
2032 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
2033 memcpy(destination, source, length);
2034 EFI_EXIT(EFI_SUCCESS);
2038 * Fill memory with a byte value.
2040 * This function implements the SetMem service.
2041 * See the Unified Extensible Firmware Interface (UEFI) specification
2044 * @buffer buffer to fill
2045 * @size size of buffer in bytes
2046 * @value byte to copy to the buffer
2048 static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
2050 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
2051 memset(buffer, value, size);
2052 EFI_EXIT(EFI_SUCCESS);
2056 * Open protocol interface on a handle.
2058 * This function implements the OpenProtocol interface.
2059 * See the Unified Extensible Firmware Interface (UEFI) specification
2062 * @handle handle on which the protocol shall be opened
2063 * @protocol GUID of the protocol
2064 * @protocol_interface interface implementing the protocol
2065 * @agent_handle handle of the driver
2066 * @controller_handle handle of the controller
2067 * @attributes attributes indicating how to open the protocol
2068 * @return status code
2070 static efi_status_t EFIAPI efi_open_protocol(
2071 void *handle, const efi_guid_t *protocol,
2072 void **protocol_interface, void *agent_handle,
2073 void *controller_handle, uint32_t attributes)
2075 struct efi_handler *handler;
2076 efi_status_t r = EFI_INVALID_PARAMETER;
2078 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
2079 protocol_interface, agent_handle, controller_handle,
2082 if (!handle || !protocol ||
2083 (!protocol_interface && attributes !=
2084 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2088 switch (attributes) {
2089 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2090 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2091 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2093 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2094 if (controller_handle == handle)
2096 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2097 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
2098 if (controller_handle == NULL)
2100 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
2101 if (agent_handle == NULL)
2108 r = efi_search_protocol(handle, protocol, &handler);
2109 if (r != EFI_SUCCESS)
2112 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2113 *protocol_interface = handler->protocol_interface;
2119 * Get interface of a protocol on a handle.
2121 * This function implements the HandleProtocol service.
2122 * See the Unified Extensible Firmware Interface (UEFI) specification
2125 * @handle handle on which the protocol shall be opened
2126 * @protocol GUID of the protocol
2127 * @protocol_interface interface implementing the protocol
2128 * @return status code
2130 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
2131 const efi_guid_t *protocol,
2132 void **protocol_interface)
2134 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2135 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
2138 static const struct efi_boot_services efi_boot_services = {
2140 .headersize = sizeof(struct efi_table_hdr),
2142 .raise_tpl = efi_raise_tpl,
2143 .restore_tpl = efi_restore_tpl,
2144 .allocate_pages = efi_allocate_pages_ext,
2145 .free_pages = efi_free_pages_ext,
2146 .get_memory_map = efi_get_memory_map_ext,
2147 .allocate_pool = efi_allocate_pool_ext,
2148 .free_pool = efi_free_pool_ext,
2149 .create_event = efi_create_event_ext,
2150 .set_timer = efi_set_timer_ext,
2151 .wait_for_event = efi_wait_for_event,
2152 .signal_event = efi_signal_event_ext,
2153 .close_event = efi_close_event,
2154 .check_event = efi_check_event,
2155 .install_protocol_interface = efi_install_protocol_interface,
2156 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2157 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
2158 .handle_protocol = efi_handle_protocol,
2160 .register_protocol_notify = efi_register_protocol_notify,
2161 .locate_handle = efi_locate_handle_ext,
2162 .locate_device_path = efi_locate_device_path,
2163 .install_configuration_table = efi_install_configuration_table_ext,
2164 .load_image = efi_load_image,
2165 .start_image = efi_start_image,
2167 .unload_image = efi_unload_image,
2168 .exit_boot_services = efi_exit_boot_services,
2169 .get_next_monotonic_count = efi_get_next_monotonic_count,
2171 .set_watchdog_timer = efi_set_watchdog_timer,
2172 .connect_controller = efi_connect_controller,
2173 .disconnect_controller = efi_disconnect_controller,
2174 .open_protocol = efi_open_protocol,
2175 .close_protocol = efi_close_protocol,
2176 .open_protocol_information = efi_open_protocol_information,
2177 .protocols_per_handle = efi_protocols_per_handle,
2178 .locate_handle_buffer = efi_locate_handle_buffer,
2179 .locate_protocol = efi_locate_protocol,
2180 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2181 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2182 .calculate_crc32 = efi_calculate_crc32,
2183 .copy_mem = efi_copy_mem,
2184 .set_mem = efi_set_mem,
2188 static uint16_t __efi_runtime_data firmware_vendor[] =
2189 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2191 struct efi_system_table __efi_runtime_data systab = {
2193 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2194 .revision = 0x20005, /* 2.5 */
2195 .headersize = sizeof(struct efi_table_hdr),
2197 .fw_vendor = (long)firmware_vendor,
2198 .con_in = (void*)&efi_con_in,
2199 .con_out = (void*)&efi_con_out,
2200 .std_err = (void*)&efi_con_out,
2201 .runtime = (void*)&efi_runtime_services,
2202 .boottime = (void*)&efi_boot_services,
2204 .tables = (void*)efi_conf_table,