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 UINTN 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 * Write a debug message for an EPI API service that is not implemented yet.
161 * @funcname function that is not yet implemented
162 * @return EFI_UNSUPPORTED
164 static efi_status_t efi_unsupported(const char *funcname)
166 debug("EFI: App called into unimplemented function %s\n", funcname);
167 return EFI_EXIT(EFI_UNSUPPORTED);
171 * Raise the task priority level.
173 * This function implements the RaiseTpl service.
174 * See the Unified Extensible Firmware Interface (UEFI) specification
177 * @new_tpl new value of the task priority level
178 * @return old value of the task priority level
180 static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl)
182 UINTN old_tpl = efi_tpl;
184 EFI_ENTRY("0x%zx", new_tpl);
186 if (new_tpl < efi_tpl)
187 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
189 if (efi_tpl > TPL_HIGH_LEVEL)
190 efi_tpl = TPL_HIGH_LEVEL;
192 EFI_EXIT(EFI_SUCCESS);
197 * Lower the task priority level.
199 * This function implements the RestoreTpl service.
200 * See the Unified Extensible Firmware Interface (UEFI) specification
203 * @old_tpl value of the task priority level to be restored
205 static void EFIAPI efi_restore_tpl(UINTN old_tpl)
207 EFI_ENTRY("0x%zx", old_tpl);
209 if (old_tpl > efi_tpl)
210 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
212 if (efi_tpl > TPL_HIGH_LEVEL)
213 efi_tpl = TPL_HIGH_LEVEL;
215 EFI_EXIT(EFI_SUCCESS);
219 * Allocate memory pages.
221 * This function implements the AllocatePages service.
222 * See the Unified Extensible Firmware Interface (UEFI) specification
225 * @type type of allocation to be performed
226 * @memory_type usage type of the allocated memory
227 * @pages number of pages to be allocated
228 * @memory allocated memory
229 * @return status code
231 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
237 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory);
238 r = efi_allocate_pages(type, memory_type, pages, memory);
245 * This function implements the FreePages service.
246 * See the Unified Extensible Firmware Interface (UEFI) specification
249 * @memory start of the memory area to be freed
250 * @pages number of pages to be freed
251 * @return status code
253 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
258 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages);
259 r = efi_free_pages(memory, pages);
264 * Get map describing memory usage.
266 * This function implements the GetMemoryMap service.
267 * See the Unified Extensible Firmware Interface (UEFI) specification
270 * @memory_map_size on entry the size, in bytes, of the memory map buffer,
271 * on exit the size of the copied memory map
272 * @memory_map buffer to which the memory map is written
273 * @map_key key for the memory map
274 * @descriptor_size size of an individual memory descriptor
275 * @descriptor_version version number of the memory descriptor structure
276 * @return status code
278 static efi_status_t EFIAPI efi_get_memory_map_ext(
279 unsigned long *memory_map_size,
280 struct efi_mem_desc *memory_map,
281 unsigned long *map_key,
282 unsigned long *descriptor_size,
283 uint32_t *descriptor_version)
287 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
288 map_key, descriptor_size, descriptor_version);
289 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
290 descriptor_size, descriptor_version);
295 * Allocate memory from pool.
297 * This function implements the AllocatePool service.
298 * See the Unified Extensible Firmware Interface (UEFI) specification
301 * @pool_type type of the pool from which memory is to be allocated
302 * @size number of bytes to be allocated
303 * @buffer allocated memory
304 * @return status code
306 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
312 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
313 r = efi_allocate_pool(pool_type, size, buffer);
318 * Free memory from pool.
320 * This function implements the FreePool service.
321 * See the Unified Extensible Firmware Interface (UEFI) specification
324 * @buffer start of memory to be freed
325 * @return status code
327 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
331 EFI_ENTRY("%p", buffer);
332 r = efi_free_pool(buffer);
336 static efi_status_t efi_create_handle(void **handle)
338 struct efi_object *obj;
341 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
342 sizeof(struct efi_object),
344 if (r != EFI_SUCCESS)
346 memset(obj, 0, sizeof(struct efi_object));
348 list_add_tail(&obj->link, &efi_obj_list);
354 * Our event capabilities are very limited. Only a small limited
355 * number of events is allowed to coexist.
357 static struct efi_event efi_events[16];
362 * This function is used inside U-Boot code to create an event.
364 * For the API function implementing the CreateEvent service see
365 * efi_create_event_ext.
367 * @type type of the event to create
368 * @notify_tpl task priority level of the event
369 * @notify_function notification function of the event
370 * @notify_context pointer passed to the notification function
371 * @event created event
372 * @return status code
374 efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl,
375 void (EFIAPI *notify_function) (
376 struct efi_event *event,
378 void *notify_context, struct efi_event **event)
383 return EFI_INVALID_PARAMETER;
385 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
386 return EFI_INVALID_PARAMETER;
388 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) &&
389 notify_function == NULL)
390 return EFI_INVALID_PARAMETER;
392 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
393 if (efi_events[i].type)
395 efi_events[i].type = type;
396 efi_events[i].notify_tpl = notify_tpl;
397 efi_events[i].notify_function = notify_function;
398 efi_events[i].notify_context = notify_context;
399 /* Disable timers on bootup */
400 efi_events[i].trigger_next = -1ULL;
401 efi_events[i].is_queued = false;
402 efi_events[i].is_signaled = false;
403 *event = &efi_events[i];
406 return EFI_OUT_OF_RESOURCES;
412 * This function implements the CreateEvent service.
413 * See the Unified Extensible Firmware Interface (UEFI) specification
416 * @type type of the event to create
417 * @notify_tpl task priority level of the event
418 * @notify_function notification function of the event
419 * @notify_context pointer passed to the notification function
420 * @event created event
421 * @return status code
423 static efi_status_t EFIAPI efi_create_event_ext(
424 uint32_t type, UINTN notify_tpl,
425 void (EFIAPI *notify_function) (
426 struct efi_event *event,
428 void *notify_context, struct efi_event **event)
430 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
432 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
433 notify_context, event));
438 * Check if a timer event has occurred or a queued notification function should
441 * Our timers have to work without interrupts, so we check whenever keyboard
442 * input or disk accesses happen if enough time elapsed for them to fire.
444 void efi_timer_check(void)
447 u64 now = timer_get_us();
449 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
450 if (!efi_events[i].type)
452 if (efi_events[i].is_queued)
453 efi_signal_event(&efi_events[i]);
454 if (!(efi_events[i].type & EVT_TIMER) ||
455 now < efi_events[i].trigger_next)
457 switch (efi_events[i].trigger_type) {
458 case EFI_TIMER_RELATIVE:
459 efi_events[i].trigger_type = EFI_TIMER_STOP;
461 case EFI_TIMER_PERIODIC:
462 efi_events[i].trigger_next +=
463 efi_events[i].trigger_time;
468 efi_events[i].is_signaled = true;
469 efi_signal_event(&efi_events[i]);
475 * Set the trigger time for a timer event or stop the event.
477 * This is the function for internal usage in U-Boot. For the API function
478 * implementing the SetTimer service see efi_set_timer_ext.
480 * @event event for which the timer is set
481 * @type type of the timer
482 * @trigger_time trigger period in multiples of 100ns
483 * @return status code
485 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
486 uint64_t trigger_time)
491 * The parameter defines a multiple of 100ns.
492 * We use multiples of 1000ns. So divide by 10.
494 do_div(trigger_time, 10);
496 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
497 if (event != &efi_events[i])
500 if (!(event->type & EVT_TIMER))
504 event->trigger_next = -1ULL;
506 case EFI_TIMER_PERIODIC:
507 case EFI_TIMER_RELATIVE:
508 event->trigger_next =
509 timer_get_us() + trigger_time;
512 return EFI_INVALID_PARAMETER;
514 event->trigger_type = type;
515 event->trigger_time = trigger_time;
516 event->is_signaled = false;
519 return EFI_INVALID_PARAMETER;
523 * Set the trigger time for a timer event or stop the event.
525 * This function implements the SetTimer service.
526 * See the Unified Extensible Firmware Interface (UEFI) specification
529 * @event event for which the timer is set
530 * @type type of the timer
531 * @trigger_time trigger period in multiples of 100ns
532 * @return status code
534 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
535 enum efi_timer_delay type,
536 uint64_t trigger_time)
538 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time);
539 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
543 * Wait for events to be signaled.
545 * This function implements the WaitForEvent service.
546 * See the Unified Extensible Firmware Interface (UEFI) specification
549 * @num_events number of events to be waited for
550 * @events events to be waited for
551 * @index index of the event that was signaled
552 * @return status code
554 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events,
555 struct efi_event **event,
556 unsigned long *index)
560 EFI_ENTRY("%ld, %p, %p", num_events, event, index);
562 /* Check parameters */
563 if (!num_events || !event)
564 return EFI_EXIT(EFI_INVALID_PARAMETER);
566 if (efi_tpl != TPL_APPLICATION)
567 return EFI_EXIT(EFI_UNSUPPORTED);
568 for (i = 0; i < num_events; ++i) {
569 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) {
570 if (event[i] == &efi_events[j])
573 return EFI_EXIT(EFI_INVALID_PARAMETER);
575 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
576 return EFI_EXIT(EFI_INVALID_PARAMETER);
577 if (!event[i]->is_signaled)
578 efi_signal_event(event[i]);
581 /* Wait for signal */
583 for (i = 0; i < num_events; ++i) {
584 if (event[i]->is_signaled)
587 /* Allow events to occur. */
593 * Reset the signal which is passed to the caller to allow periodic
596 event[i]->is_signaled = false;
600 return EFI_EXIT(EFI_SUCCESS);
604 * Signal an EFI event.
606 * This function implements the SignalEvent service.
607 * See the Unified Extensible Firmware Interface (UEFI) specification
610 * This functions sets the signaled state of the event and queues the
611 * notification function for execution.
613 * @event event to signal
615 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
619 EFI_ENTRY("%p", event);
620 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
621 if (event != &efi_events[i])
623 if (event->is_signaled)
625 event->is_signaled = true;
626 if (event->type & EVT_NOTIFY_SIGNAL)
627 efi_signal_event(event);
630 return EFI_EXIT(EFI_SUCCESS);
634 * Close an EFI event.
636 * This function implements the CloseEvent service.
637 * See the Unified Extensible Firmware Interface (UEFI) specification
640 * @event event to close
641 * @return status code
643 static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
647 EFI_ENTRY("%p", event);
648 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
649 if (event == &efi_events[i]) {
651 event->trigger_next = -1ULL;
652 event->is_queued = false;
653 event->is_signaled = false;
654 return EFI_EXIT(EFI_SUCCESS);
657 return EFI_EXIT(EFI_INVALID_PARAMETER);
661 * Check if an event is signaled.
663 * This function implements the CheckEvent service.
664 * See the Unified Extensible Firmware Interface (UEFI) specification
667 * If an event is not signaled yet the notification function is queued.
669 * @event event to check
670 * @return status code
672 static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
676 EFI_ENTRY("%p", event);
678 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
679 if (event != &efi_events[i])
681 if (!event->type || event->type & EVT_NOTIFY_SIGNAL)
683 if (!event->is_signaled)
684 efi_signal_event(event);
685 if (event->is_signaled)
686 return EFI_EXIT(EFI_SUCCESS);
687 return EFI_EXIT(EFI_NOT_READY);
689 return EFI_EXIT(EFI_INVALID_PARAMETER);
693 * Install protocol interface.
695 * This is the function for internal calls. For the API implementation of the
696 * InstallProtocolInterface service see function
697 * efi_install_protocol_interface_ext.
699 * @handle handle on which the protocol shall be installed
700 * @protocol GUID of the protocol to be installed
701 * @protocol_interface_type type of the interface to be installed,
702 * always EFI_NATIVE_INTERFACE
703 * @protocol_interface interface of the protocol implementation
704 * @return status code
706 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle,
707 efi_guid_t *protocol, int protocol_interface_type,
708 void *protocol_interface)
710 struct list_head *lhandle;
714 if (!handle || !protocol ||
715 protocol_interface_type != EFI_NATIVE_INTERFACE) {
716 r = EFI_INVALID_PARAMETER;
720 /* Create new handle if requested. */
722 r = efi_create_handle(handle);
723 if (r != EFI_SUCCESS)
727 list_for_each(lhandle, &efi_obj_list) {
728 struct efi_object *efiobj;
729 efiobj = list_entry(lhandle, struct efi_object, link);
731 if (efiobj->handle != *handle)
733 /* Check if protocol is already installed on the handle. */
734 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
735 struct efi_handler *handler = &efiobj->protocols[i];
739 if (!guidcmp(handler->guid, protocol)) {
740 r = EFI_INVALID_PARAMETER;
744 /* Install protocol in first empty slot. */
745 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
746 struct efi_handler *handler = &efiobj->protocols[i];
751 handler->guid = protocol;
752 handler->protocol_interface = protocol_interface;
756 r = EFI_OUT_OF_RESOURCES;
759 r = EFI_INVALID_PARAMETER;
765 * Install protocol interface.
767 * This function implements the InstallProtocolInterface service.
768 * See the Unified Extensible Firmware Interface (UEFI) specification
771 * @handle handle on which the protocol shall be installed
772 * @protocol GUID of the protocol to be installed
773 * @protocol_interface_type type of the interface to be installed,
774 * always EFI_NATIVE_INTERFACE
775 * @protocol_interface interface of the protocol implementation
776 * @return status code
778 static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle,
779 efi_guid_t *protocol, int protocol_interface_type,
780 void *protocol_interface)
782 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
785 return EFI_EXIT(efi_install_protocol_interface(handle, protocol,
786 protocol_interface_type,
787 protocol_interface));
791 * Reinstall protocol interface.
793 * This function implements the ReinstallProtocolInterface service.
794 * See the Unified Extensible Firmware Interface (UEFI) specification
797 * @handle handle on which the protocol shall be
799 * @protocol GUID of the protocol to be installed
800 * @old_interface interface to be removed
801 * @new_interface interface to be installed
802 * @return status code
804 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle,
805 efi_guid_t *protocol, void *old_interface,
808 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
810 return EFI_EXIT(EFI_ACCESS_DENIED);
814 * Uninstall protocol interface.
816 * This is the function for internal calls. For the API implementation of the
817 * UninstallProtocolInterface service see function
818 * efi_uninstall_protocol_interface_ext.
820 * @handle handle from which the protocol shall be removed
821 * @protocol GUID of the protocol to be removed
822 * @protocol_interface interface to be removed
823 * @return status code
825 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle,
826 efi_guid_t *protocol, void *protocol_interface)
828 struct list_head *lhandle;
830 efi_status_t r = EFI_NOT_FOUND;
832 if (!handle || !protocol) {
833 r = EFI_INVALID_PARAMETER;
837 list_for_each(lhandle, &efi_obj_list) {
838 struct efi_object *efiobj;
839 efiobj = list_entry(lhandle, struct efi_object, link);
841 if (efiobj->handle != handle)
844 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
845 struct efi_handler *handler = &efiobj->protocols[i];
846 const efi_guid_t *hprotocol = handler->guid;
850 if (!guidcmp(hprotocol, protocol)) {
851 if (handler->protocol_interface) {
852 r = EFI_ACCESS_DENIED;
867 * Uninstall protocol interface.
869 * This function implements the UninstallProtocolInterface service.
870 * See the Unified Extensible Firmware Interface (UEFI) specification
873 * @handle handle from which the protocol shall be removed
874 * @protocol GUID of the protocol to be removed
875 * @protocol_interface interface to be removed
876 * @return status code
878 static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle,
879 efi_guid_t *protocol, void *protocol_interface)
881 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
883 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol,
884 protocol_interface));
888 * Register an event for notification when a protocol is installed.
890 * This function implements the RegisterProtocolNotify service.
891 * See the Unified Extensible Firmware Interface (UEFI) specification
894 * @protocol GUID of the protocol whose installation shall be
896 * @event event to be signaled upon installation of the protocol
897 * @registration key for retrieving the registration information
898 * @return status code
900 static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol,
901 struct efi_event *event,
904 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
905 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
909 * Determine if an EFI handle implements a protocol.
911 * See the documentation of the LocateHandle service in the UEFI specification.
913 * @search_type selection criterion
914 * @protocol GUID of the protocol
915 * @search_key registration key
917 * @return 0 if the handle implements the protocol
919 static int efi_search(enum efi_locate_search_type search_type,
920 efi_guid_t *protocol, void *search_key,
921 struct efi_object *efiobj)
925 switch (search_type) {
928 case by_register_notify:
931 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
932 const efi_guid_t *guid = efiobj->protocols[i].guid;
933 if (guid && !guidcmp(guid, protocol))
943 * Locate handles implementing a protocol.
945 * This function is meant for U-Boot internal calls. For the API implementation
946 * of the LocateHandle service see efi_locate_handle_ext.
948 * @search_type selection criterion
949 * @protocol GUID of the protocol
950 * @search_key registration key
951 * @buffer_size size of the buffer to receive the handles in bytes
952 * @buffer buffer to receive the relevant handles
953 * @return status code
955 static efi_status_t efi_locate_handle(
956 enum efi_locate_search_type search_type,
957 efi_guid_t *protocol, void *search_key,
958 unsigned long *buffer_size, efi_handle_t *buffer)
960 struct list_head *lhandle;
961 unsigned long size = 0;
963 /* Count how much space we need */
964 list_for_each(lhandle, &efi_obj_list) {
965 struct efi_object *efiobj;
966 efiobj = list_entry(lhandle, struct efi_object, link);
967 if (!efi_search(search_type, protocol, search_key, efiobj)) {
968 size += sizeof(void*);
972 if (*buffer_size < size) {
974 return EFI_BUFFER_TOO_SMALL;
979 return EFI_NOT_FOUND;
981 /* Then fill the array */
982 list_for_each(lhandle, &efi_obj_list) {
983 struct efi_object *efiobj;
984 efiobj = list_entry(lhandle, struct efi_object, link);
985 if (!efi_search(search_type, protocol, search_key, efiobj)) {
986 *(buffer++) = efiobj->handle;
994 * Locate handles implementing a protocol.
996 * This function implements the LocateHandle service.
997 * See the Unified Extensible Firmware Interface (UEFI) specification
1000 * @search_type selection criterion
1001 * @protocol GUID of the protocol
1002 * @search_key registration key
1003 * @buffer_size size of the buffer to receive the handles in bytes
1004 * @buffer buffer to receive the relevant handles
1005 * @return 0 if the handle implements the protocol
1007 static efi_status_t EFIAPI efi_locate_handle_ext(
1008 enum efi_locate_search_type search_type,
1009 efi_guid_t *protocol, void *search_key,
1010 unsigned long *buffer_size, efi_handle_t *buffer)
1012 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1013 buffer_size, buffer);
1015 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1016 buffer_size, buffer));
1020 * Get the device path and handle of an device implementing a protocol.
1022 * This function implements the LocateDevicePath service.
1023 * See the Unified Extensible Firmware Interface (UEFI) specification
1026 * @protocol GUID of the protocol
1027 * @device_path device path
1028 * @device handle of the device
1029 * @return status code
1031 static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol,
1032 struct efi_device_path **device_path,
1033 efi_handle_t *device)
1035 struct efi_object *efiobj;
1037 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1039 efiobj = efi_dp_find_obj(*device_path, device_path);
1041 return EFI_EXIT(EFI_NOT_FOUND);
1043 *device = efiobj->handle;
1045 return EFI_EXIT(EFI_SUCCESS);
1048 /* Collapses configuration table entries, removing index i */
1049 static void efi_remove_configuration_table(int i)
1051 struct efi_configuration_table *this = &efi_conf_table[i];
1052 struct efi_configuration_table *next = &efi_conf_table[i+1];
1053 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1055 memmove(this, next, (ulong)end - (ulong)next);
1060 * Adds, updates, or removes a configuration table.
1062 * This function is used for internal calls. For the API implementation of the
1063 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1065 * @guid GUID of the installed table
1066 * @table table to be installed
1067 * @return status code
1069 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table)
1073 /* Check for guid override */
1074 for (i = 0; i < systab.nr_tables; i++) {
1075 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
1077 efi_conf_table[i].table = table;
1079 efi_remove_configuration_table(i);
1085 return EFI_NOT_FOUND;
1087 /* No override, check for overflow */
1088 if (i >= ARRAY_SIZE(efi_conf_table))
1089 return EFI_OUT_OF_RESOURCES;
1091 /* Add a new entry */
1092 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1093 efi_conf_table[i].table = table;
1094 systab.nr_tables = i + 1;
1100 * Adds, updates, or removes a configuration table.
1102 * This function implements the InstallConfigurationTable service.
1103 * See the Unified Extensible Firmware Interface (UEFI) specification
1106 * @guid GUID of the installed table
1107 * @table table to be installed
1108 * @return status code
1110 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1113 EFI_ENTRY("%pUl, %p", guid, table);
1114 return EFI_EXIT(efi_install_configuration_table(guid, table));
1118 * Initialize a loaded_image_info + loaded_image_info object with correct
1119 * protocols, boot-device, etc.
1121 * @info loaded image info to be passed to the enty point of the
1123 * @obj internal object associated with the loaded image
1124 * @device_path device path of the loaded image
1125 * @file_path file path of the loaded image
1127 void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj,
1128 struct efi_device_path *device_path,
1129 struct efi_device_path *file_path)
1134 * When asking for the device path interface, return
1135 * bootefi_device_path
1137 obj->protocols[0].guid = &efi_guid_device_path;
1138 obj->protocols[0].protocol_interface = device_path;
1141 * When asking for the loaded_image interface, just
1142 * return handle which points to loaded_image_info
1144 obj->protocols[1].guid = &efi_guid_loaded_image;
1145 obj->protocols[1].protocol_interface = info;
1147 obj->protocols[2].guid = &efi_guid_console_control;
1148 obj->protocols[2].protocol_interface = (void *)&efi_console_control;
1150 obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol;
1151 obj->protocols[3].protocol_interface =
1152 (void *)&efi_device_path_to_text;
1154 info->file_path = file_path;
1155 info->device_handle = efi_dp_find_obj(device_path, NULL);
1157 list_add_tail(&obj->link, &efi_obj_list);
1161 * Load an image using a file path.
1163 * @file_path the path of the image to load
1164 * @buffer buffer containing the loaded image
1166 efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1169 struct efi_file_info *info = NULL;
1170 struct efi_file_handle *f;
1171 static efi_status_t ret;
1174 f = efi_file_from_path(file_path);
1176 return EFI_DEVICE_ERROR;
1179 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1181 if (ret == EFI_BUFFER_TOO_SMALL) {
1183 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1186 if (ret != EFI_SUCCESS)
1189 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1193 EFI_CALL(ret = f->read(f, &info->file_size, *buffer));
1197 EFI_CALL(f->close(f));
1199 if (ret != EFI_SUCCESS) {
1200 efi_free_pool(*buffer);
1208 * Load an EFI image into memory.
1210 * This function implements the LoadImage service.
1211 * See the Unified Extensible Firmware Interface (UEFI) specification
1214 * @boot_policy true for request originating from the boot manager
1215 * @parent_image the calles's image handle
1216 * @file_path the path of the image to load
1217 * @source_buffer memory location from which the image is installed
1218 * @source_size size of the memory area from which the image is
1220 * @image_handle handle for the newly installed image
1221 * @return status code
1223 static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1224 efi_handle_t parent_image,
1225 struct efi_device_path *file_path,
1226 void *source_buffer,
1227 unsigned long source_size,
1228 efi_handle_t *image_handle)
1230 struct efi_loaded_image *info;
1231 struct efi_object *obj;
1233 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
1234 file_path, source_buffer, source_size, image_handle);
1236 info = calloc(1, sizeof(*info));
1237 obj = calloc(1, sizeof(*obj));
1239 if (!source_buffer) {
1240 struct efi_device_path *dp, *fp;
1243 ret = efi_load_image_from_path(file_path, &source_buffer);
1244 if (ret != EFI_SUCCESS) {
1247 return EFI_EXIT(ret);
1251 * split file_path which contains both the device and
1254 efi_dp_split_file_path(file_path, &dp, &fp);
1256 efi_setup_loaded_image(info, obj, dp, fp);
1258 /* In this case, file_path is the "device" path, ie.
1259 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1261 efi_setup_loaded_image(info, obj, file_path, NULL);
1264 info->reserved = efi_load_pe(source_buffer, info);
1265 if (!info->reserved) {
1268 return EFI_EXIT(EFI_UNSUPPORTED);
1271 *image_handle = info;
1273 return EFI_EXIT(EFI_SUCCESS);
1277 * Call the entry point of an image.
1279 * This function implements the StartImage service.
1280 * See the Unified Extensible Firmware Interface (UEFI) specification
1283 * @image_handle handle of the image
1284 * @exit_data_size size of the buffer
1285 * @exit_data buffer to receive the exit data of the called image
1287 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1288 unsigned long *exit_data_size,
1291 ulong (*entry)(void *image_handle, struct efi_system_table *st);
1292 struct efi_loaded_image *info = image_handle;
1294 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1295 entry = info->reserved;
1297 efi_is_direct_boot = false;
1299 /* call the image! */
1300 if (setjmp(&info->exit_jmp)) {
1301 /* We returned from the child image */
1302 return EFI_EXIT(info->exit_status);
1305 __efi_nesting_dec();
1307 entry(image_handle, &systab);
1308 __efi_entry_check();
1309 __efi_nesting_inc();
1311 /* Should usually never get here */
1312 return EFI_EXIT(EFI_SUCCESS);
1316 * Leave an EFI application or driver.
1318 * This function implements the Exit service.
1319 * See the Unified Extensible Firmware Interface (UEFI) specification
1322 * @image_handle handle of the application or driver that is exiting
1323 * @exit_status status code
1324 * @exit_data_size size of the buffer in bytes
1325 * @exit_data buffer with data describing an error
1327 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
1328 efi_status_t exit_status, unsigned long exit_data_size,
1331 struct efi_loaded_image *loaded_image_info = (void*)image_handle;
1333 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1334 exit_data_size, exit_data);
1336 /* Make sure entry/exit counts for EFI world cross-overs match */
1340 * But longjmp out with the U-Boot gd, not the application's, as
1341 * the other end is a setjmp call inside EFI context.
1345 loaded_image_info->exit_status = exit_status;
1346 longjmp(&loaded_image_info->exit_jmp, 1);
1348 panic("EFI application exited");
1352 * Find the internal EFI object for a handle.
1354 * @handle handle to find
1355 * @return EFI object
1357 static struct efi_object *efi_search_obj(void *handle)
1359 struct list_head *lhandle;
1361 list_for_each(lhandle, &efi_obj_list) {
1362 struct efi_object *efiobj;
1363 efiobj = list_entry(lhandle, struct efi_object, link);
1364 if (efiobj->handle == handle)
1372 * Unload an EFI image.
1374 * This function implements the UnloadImage service.
1375 * See the Unified Extensible Firmware Interface (UEFI) specification
1378 * @image_handle handle of the image to be unloaded
1379 * @return status code
1381 static efi_status_t EFIAPI efi_unload_image(void *image_handle)
1383 struct efi_object *efiobj;
1385 EFI_ENTRY("%p", image_handle);
1386 efiobj = efi_search_obj(image_handle);
1388 list_del(&efiobj->link);
1390 return EFI_EXIT(EFI_SUCCESS);
1394 * Fix up caches for EFI payloads if necessary.
1396 static void efi_exit_caches(void)
1398 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1400 * Grub on 32bit ARM needs to have caches disabled before jumping into
1401 * a zImage, but does not know of all cache layers. Give it a hand.
1403 if (efi_is_direct_boot)
1404 cleanup_before_linux();
1409 * Stop boot services.
1411 * This function implements the ExitBootServices service.
1412 * See the Unified Extensible Firmware Interface (UEFI) specification
1415 * @image_handle handle of the loaded image
1416 * @map_key key of the memory map
1417 * @return status code
1419 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle,
1420 unsigned long map_key)
1424 EFI_ENTRY("%p, %ld", image_handle, map_key);
1426 /* Notify that ExitBootServices is invoked. */
1427 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) {
1428 if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES)
1430 efi_signal_event(&efi_events[i]);
1432 /* Make sure that notification functions are not called anymore */
1433 efi_tpl = TPL_HIGH_LEVEL;
1435 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
1436 /* save any EFI variables that have been written: */
1440 board_quiesce_devices();
1442 /* Fix up caches for EFI payloads if necessary */
1445 /* This stops all lingering devices */
1446 bootm_disable_interrupts();
1448 /* Give the payload some time to boot */
1451 return EFI_EXIT(EFI_SUCCESS);
1455 * Get next value of the counter.
1457 * This function implements the NextMonotonicCount service.
1458 * See the Unified Extensible Firmware Interface (UEFI) specification
1461 * @count returned value of the counter
1462 * @return status code
1464 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1466 static uint64_t mono = 0;
1467 EFI_ENTRY("%p", count);
1469 return EFI_EXIT(EFI_SUCCESS);
1475 * This function implements the Stall sercive.
1476 * See the Unified Extensible Firmware Interface (UEFI) specification
1479 * @microseconds period to sleep in microseconds
1480 * @return status code
1482 static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1484 EFI_ENTRY("%ld", microseconds);
1485 udelay(microseconds);
1486 return EFI_EXIT(EFI_SUCCESS);
1490 * Reset the watchdog timer.
1492 * This function implements the WatchdogTimer service.
1493 * See the Unified Extensible Firmware Interface (UEFI) specification
1496 * @timeout seconds before reset by watchdog
1497 * @watchdog_code code to be logged when resetting
1498 * @data_size size of buffer in bytes
1499 * @watchdog_data buffer with data describing the reset reason
1501 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1502 uint64_t watchdog_code,
1503 unsigned long data_size,
1504 uint16_t *watchdog_data)
1506 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code,
1507 data_size, watchdog_data);
1508 return efi_unsupported(__func__);
1512 * Connect a controller to a driver.
1514 * This function implements the ConnectController service.
1515 * See the Unified Extensible Firmware Interface (UEFI) specification
1518 * @controller_handle handle of the controller
1519 * @driver_image_handle handle of the driver
1520 * @remain_device_path device path of a child controller
1521 * @recursive true to connect all child controllers
1522 * @return status code
1524 static efi_status_t EFIAPI efi_connect_controller(
1525 efi_handle_t controller_handle,
1526 efi_handle_t *driver_image_handle,
1527 struct efi_device_path *remain_device_path,
1530 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
1531 remain_device_path, recursive);
1532 return EFI_EXIT(EFI_NOT_FOUND);
1536 * Disconnect a controller from a driver.
1538 * This function implements the DisconnectController service.
1539 * See the Unified Extensible Firmware Interface (UEFI) specification
1542 * @controller_handle handle of the controller
1543 * @driver_image_handle handle of the driver
1544 * @child_handle handle of the child to destroy
1545 * @return status code
1547 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle,
1548 void *driver_image_handle,
1551 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
1553 return EFI_EXIT(EFI_INVALID_PARAMETER);
1559 * This function implements the CloseProtocol service.
1560 * See the Unified Extensible Firmware Interface (UEFI) specification
1563 * @handle handle on which the protocol shall be closed
1564 * @protocol GUID of the protocol to close
1565 * @agent_handle handle of the driver
1566 * @controller_handle handle of the controller
1567 * @return status code
1569 static efi_status_t EFIAPI efi_close_protocol(void *handle,
1570 efi_guid_t *protocol,
1572 void *controller_handle)
1574 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
1576 return EFI_EXIT(EFI_NOT_FOUND);
1580 * Provide information about then open status of a protocol on a handle
1582 * This function implements the OpenProtocolInformation service.
1583 * See the Unified Extensible Firmware Interface (UEFI) specification
1586 * @handle handle for which the information shall be retrieved
1587 * @protocol GUID of the protocol
1588 * @entry_buffer buffer to receive the open protocol information
1589 * @entry_count number of entries available in the buffer
1590 * @return status code
1592 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle,
1593 efi_guid_t *protocol,
1594 struct efi_open_protocol_info_entry **entry_buffer,
1595 unsigned long *entry_count)
1597 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
1599 return EFI_EXIT(EFI_NOT_FOUND);
1603 * Get protocols installed on a handle.
1605 * This function implements the ProtocolsPerHandleService.
1606 * See the Unified Extensible Firmware Interface (UEFI) specification
1609 * @handle handle for which the information is retrieved
1610 * @protocol_buffer buffer with protocol GUIDs
1611 * @protocol_buffer_count number of entries in the buffer
1613 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle,
1614 efi_guid_t ***protocol_buffer,
1615 unsigned long *protocol_buffer_count)
1617 unsigned long buffer_size;
1618 struct efi_object *efiobj;
1620 struct list_head *lhandle;
1623 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
1624 protocol_buffer_count);
1626 if (!handle || !protocol_buffer || !protocol_buffer_count)
1627 return EFI_EXIT(EFI_INVALID_PARAMETER);
1629 *protocol_buffer = NULL;
1630 *protocol_buffer_count = 0;
1631 list_for_each(lhandle, &efi_obj_list) {
1632 efiobj = list_entry(lhandle, struct efi_object, link);
1634 if (efiobj->handle != handle)
1637 /* Count protocols */
1638 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1639 if (efiobj->protocols[i].guid)
1640 ++*protocol_buffer_count;
1643 if (*protocol_buffer_count) {
1644 buffer_size = sizeof(efi_guid_t *) *
1645 *protocol_buffer_count;
1646 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES,
1648 (void **)protocol_buffer);
1649 if (r != EFI_SUCCESS)
1652 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) {
1653 if (efiobj->protocols[i].guid) {
1654 (*protocol_buffer)[j] = (void *)
1655 efiobj->protocols[i].guid;
1663 return EFI_EXIT(EFI_SUCCESS);
1667 * Locate handles implementing a protocol.
1669 * This function implements the LocateHandleBuffer service.
1670 * See the Unified Extensible Firmware Interface (UEFI) specification
1673 * @search_type selection criterion
1674 * @protocol GUID of the protocol
1675 * @search_key registration key
1676 * @no_handles number of returned handles
1677 * @buffer buffer with the returned handles
1678 * @return status code
1680 static efi_status_t EFIAPI efi_locate_handle_buffer(
1681 enum efi_locate_search_type search_type,
1682 efi_guid_t *protocol, void *search_key,
1683 unsigned long *no_handles, efi_handle_t **buffer)
1686 unsigned long buffer_size = 0;
1688 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
1689 no_handles, buffer);
1691 if (!no_handles || !buffer) {
1692 r = EFI_INVALID_PARAMETER;
1697 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1699 if (r != EFI_BUFFER_TOO_SMALL)
1701 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
1703 if (r != EFI_SUCCESS)
1705 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
1707 if (r == EFI_SUCCESS)
1708 *no_handles = buffer_size / sizeof(void *);
1714 * Find an interface implementing a protocol.
1716 * This function implements the LocateProtocol service.
1717 * See the Unified Extensible Firmware Interface (UEFI) specification
1720 * @protocol GUID of the protocol
1721 * @registration registration key passed to the notification function
1722 * @protocol_interface interface implementing the protocol
1724 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol,
1726 void **protocol_interface)
1728 struct list_head *lhandle;
1731 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
1733 if (!protocol || !protocol_interface)
1734 return EFI_EXIT(EFI_INVALID_PARAMETER);
1736 EFI_PRINT_GUID("protocol", protocol);
1738 list_for_each(lhandle, &efi_obj_list) {
1739 struct efi_object *efiobj;
1741 efiobj = list_entry(lhandle, struct efi_object, link);
1742 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1743 struct efi_handler *handler = &efiobj->protocols[i];
1747 if (!guidcmp(handler->guid, protocol)) {
1748 *protocol_interface =
1749 handler->protocol_interface;
1750 return EFI_EXIT(EFI_SUCCESS);
1754 *protocol_interface = NULL;
1756 return EFI_EXIT(EFI_NOT_FOUND);
1760 * Install multiple protocol interfaces.
1762 * This function implements the MultipleProtocolInterfaces service.
1763 * See the Unified Extensible Firmware Interface (UEFI) specification
1766 * @handle handle on which the protocol interfaces shall be installed
1767 * @... NULL terminated argument list with pairs of protocol GUIDS and
1769 * @return status code
1771 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
1774 EFI_ENTRY("%p", handle);
1777 efi_guid_t *protocol;
1778 void *protocol_interface;
1779 efi_status_t r = EFI_SUCCESS;
1783 return EFI_EXIT(EFI_INVALID_PARAMETER);
1785 va_start(argptr, handle);
1787 protocol = va_arg(argptr, efi_guid_t*);
1790 protocol_interface = va_arg(argptr, void*);
1791 r = efi_install_protocol_interface(handle, protocol,
1792 EFI_NATIVE_INTERFACE,
1793 protocol_interface);
1794 if (r != EFI_SUCCESS)
1799 if (r == EFI_SUCCESS)
1802 /* If an error occured undo all changes. */
1803 va_start(argptr, handle);
1805 protocol = va_arg(argptr, efi_guid_t*);
1806 protocol_interface = va_arg(argptr, void*);
1807 efi_uninstall_protocol_interface(handle, protocol,
1808 protocol_interface);
1816 * Uninstall multiple protocol interfaces.
1818 * This function implements the UninstallMultipleProtocolInterfaces service.
1819 * See the Unified Extensible Firmware Interface (UEFI) specification
1822 * @handle handle from which the protocol interfaces shall be removed
1823 * @... NULL terminated argument list with pairs of protocol GUIDS and
1825 * @return status code
1827 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
1830 EFI_ENTRY("%p", handle);
1831 return EFI_EXIT(EFI_INVALID_PARAMETER);
1835 * Calculate cyclic redundancy code.
1837 * This function implements the CalculateCrc32 service.
1838 * See the Unified Extensible Firmware Interface (UEFI) specification
1841 * @data buffer with data
1842 * @data_size size of buffer in bytes
1843 * @crc32_p cyclic redundancy code
1844 * @return status code
1846 static efi_status_t EFIAPI efi_calculate_crc32(void *data,
1847 unsigned long data_size,
1850 EFI_ENTRY("%p, %ld", data, data_size);
1851 *crc32_p = crc32(0, data, data_size);
1852 return EFI_EXIT(EFI_SUCCESS);
1858 * This function implements the CopyMem service.
1859 * See the Unified Extensible Firmware Interface (UEFI) specification
1862 * @destination destination of the copy operation
1863 * @source source of the copy operation
1864 * @length number of bytes to copy
1866 static void EFIAPI efi_copy_mem(void *destination, void *source,
1867 unsigned long length)
1869 EFI_ENTRY("%p, %p, %ld", destination, source, length);
1870 memcpy(destination, source, length);
1874 * Fill memory with a byte value.
1876 * This function implements the SetMem service.
1877 * See the Unified Extensible Firmware Interface (UEFI) specification
1880 * @buffer buffer to fill
1881 * @size size of buffer in bytes
1882 * @value byte to copy to the buffer
1884 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value)
1886 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value);
1887 memset(buffer, value, size);
1891 * Open protocol interface on a handle.
1893 * This function implements the OpenProtocol interface.
1894 * See the Unified Extensible Firmware Interface (UEFI) specification
1897 * @handle handle on which the protocol shall be opened
1898 * @protocol GUID of the protocol
1899 * @protocol_interface interface implementing the protocol
1900 * @agent_handle handle of the driver
1901 * @controller_handle handle of the controller
1902 * @attributes attributes indicating how to open the protocol
1903 * @return status code
1905 static efi_status_t EFIAPI efi_open_protocol(
1906 void *handle, efi_guid_t *protocol,
1907 void **protocol_interface, void *agent_handle,
1908 void *controller_handle, uint32_t attributes)
1910 struct list_head *lhandle;
1912 efi_status_t r = EFI_INVALID_PARAMETER;
1914 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
1915 protocol_interface, agent_handle, controller_handle,
1918 if (!handle || !protocol ||
1919 (!protocol_interface && attributes !=
1920 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
1924 EFI_PRINT_GUID("protocol", protocol);
1926 switch (attributes) {
1927 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
1928 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
1929 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
1931 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
1932 if (controller_handle == handle)
1934 case EFI_OPEN_PROTOCOL_BY_DRIVER:
1935 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
1936 if (controller_handle == NULL)
1938 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
1939 if (agent_handle == NULL)
1946 list_for_each(lhandle, &efi_obj_list) {
1947 struct efi_object *efiobj;
1948 efiobj = list_entry(lhandle, struct efi_object, link);
1950 if (efiobj->handle != handle)
1953 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
1954 struct efi_handler *handler = &efiobj->protocols[i];
1955 const efi_guid_t *hprotocol = handler->guid;
1958 if (!guidcmp(hprotocol, protocol)) {
1960 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) {
1961 *protocol_interface =
1962 handler->protocol_interface;
1972 r = EFI_UNSUPPORTED;
1978 * Get interface of a protocol on a handle.
1980 * This function implements the HandleProtocol service.
1981 * See the Unified Extensible Firmware Interface (UEFI) specification
1984 * @handle handle on which the protocol shall be opened
1985 * @protocol GUID of the protocol
1986 * @protocol_interface interface implementing the protocol
1987 * @return status code
1989 static efi_status_t EFIAPI efi_handle_protocol(void *handle,
1990 efi_guid_t *protocol,
1991 void **protocol_interface)
1993 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
1994 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
1997 static const struct efi_boot_services efi_boot_services = {
1999 .headersize = sizeof(struct efi_table_hdr),
2001 .raise_tpl = efi_raise_tpl,
2002 .restore_tpl = efi_restore_tpl,
2003 .allocate_pages = efi_allocate_pages_ext,
2004 .free_pages = efi_free_pages_ext,
2005 .get_memory_map = efi_get_memory_map_ext,
2006 .allocate_pool = efi_allocate_pool_ext,
2007 .free_pool = efi_free_pool_ext,
2008 .create_event = efi_create_event_ext,
2009 .set_timer = efi_set_timer_ext,
2010 .wait_for_event = efi_wait_for_event,
2011 .signal_event = efi_signal_event_ext,
2012 .close_event = efi_close_event,
2013 .check_event = efi_check_event,
2014 .install_protocol_interface = efi_install_protocol_interface_ext,
2015 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
2016 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext,
2017 .handle_protocol = efi_handle_protocol,
2019 .register_protocol_notify = efi_register_protocol_notify,
2020 .locate_handle = efi_locate_handle_ext,
2021 .locate_device_path = efi_locate_device_path,
2022 .install_configuration_table = efi_install_configuration_table_ext,
2023 .load_image = efi_load_image,
2024 .start_image = efi_start_image,
2026 .unload_image = efi_unload_image,
2027 .exit_boot_services = efi_exit_boot_services,
2028 .get_next_monotonic_count = efi_get_next_monotonic_count,
2030 .set_watchdog_timer = efi_set_watchdog_timer,
2031 .connect_controller = efi_connect_controller,
2032 .disconnect_controller = efi_disconnect_controller,
2033 .open_protocol = efi_open_protocol,
2034 .close_protocol = efi_close_protocol,
2035 .open_protocol_information = efi_open_protocol_information,
2036 .protocols_per_handle = efi_protocols_per_handle,
2037 .locate_handle_buffer = efi_locate_handle_buffer,
2038 .locate_protocol = efi_locate_protocol,
2039 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces,
2040 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces,
2041 .calculate_crc32 = efi_calculate_crc32,
2042 .copy_mem = efi_copy_mem,
2043 .set_mem = efi_set_mem,
2047 static uint16_t __efi_runtime_data firmware_vendor[] =
2048 { 'D','a','s',' ','U','-','b','o','o','t',0 };
2050 struct efi_system_table __efi_runtime_data systab = {
2052 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
2053 .revision = 0x20005, /* 2.5 */
2054 .headersize = sizeof(struct efi_table_hdr),
2056 .fw_vendor = (long)firmware_vendor,
2057 .con_in = (void*)&efi_con_in,
2058 .con_out = (void*)&efi_con_out,
2059 .std_err = (void*)&efi_con_out,
2060 .runtime = (void*)&efi_runtime_services,
2061 .boottime = (void*)&efi_boot_services,
2063 .tables = (void*)efi_conf_table,