1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application runtime services
5 * Copyright (c) 2016 Alexander Graf
12 #include <efi_loader.h>
15 /* For manual relocation support */
16 DECLARE_GLOBAL_DATA_PTR;
18 struct efi_runtime_mmio_list {
19 struct list_head link;
25 /* This list contains all runtime available mmio regions */
26 LIST_HEAD(efi_runtime_mmio);
28 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void);
31 * TODO(sjg@chromium.org): These defines and structures should come from the ELF
32 * header for each architecture (or a generic header) rather than being repeated
35 #if defined(__aarch64__)
36 #define R_RELATIVE R_AARCH64_RELATIVE
37 #define R_MASK 0xffffffffULL
39 #elif defined(__arm__)
40 #define R_RELATIVE R_ARM_RELATIVE
41 #define R_MASK 0xffULL
42 #elif defined(__i386__)
43 #define R_RELATIVE R_386_RELATIVE
44 #define R_MASK 0xffULL
45 #elif defined(__x86_64__)
46 #define R_RELATIVE R_X86_64_RELATIVE
47 #define R_MASK 0xffffffffULL
49 #elif defined(__riscv)
50 #define R_RELATIVE R_RISCV_RELATIVE
51 #define R_MASK 0xffULL
60 #if (__riscv_xlen == 32)
61 #define R_ABSOLUTE R_RISCV_32
63 #elif (__riscv_xlen == 64)
64 #define R_ABSOLUTE R_RISCV_64
67 #error unknown riscv target
70 #error Need to add relocation awareness
85 * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI
86 * payload are running concurrently at the same time. In this mode, we can
87 * handle a good number of runtime callbacks
90 efi_status_t efi_init_runtime_supported(void)
92 u16 efi_runtime_services_supported = 0;
95 * This value must be synced with efi_runtime_detach_list
96 * as well as efi_runtime_services.
98 #if CONFIG_IS_ENABLED(ARCH_BCM283X) || \
99 CONFIG_IS_ENABLED(FSL_LAYERSCAPE) || \
100 CONFIG_IS_ENABLED(SYSRESET_X86) || \
101 CONFIG_IS_ENABLED(PSCI_RESET)
102 efi_runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM;
104 efi_runtime_services_supported |=
105 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP;
106 return EFI_CALL(efi_set_variable(L"RuntimeServicesSupported",
107 &efi_global_variable_guid,
108 EFI_VARIABLE_BOOTSERVICE_ACCESS |
109 EFI_VARIABLE_RUNTIME_ACCESS,
110 sizeof(efi_runtime_services_supported),
111 &efi_runtime_services_supported));
115 * efi_update_table_header_crc32() - Update crc32 in table header
119 void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
122 table->crc32 = crc32(0, (const unsigned char *)table,
127 * efi_reset_system_boottime() - reset system at boot time
129 * This function implements the ResetSystem() runtime service before
130 * SetVirtualAddressMap() is called.
132 * See the Unified Extensible Firmware Interface (UEFI) specification for
135 * @reset_type: type of reset to perform
136 * @reset_status: status code for the reset
137 * @data_size: size of reset_data
138 * @reset_data: information about the reset
140 static void EFIAPI efi_reset_system_boottime(
141 enum efi_reset_type reset_type,
142 efi_status_t reset_status,
143 unsigned long data_size, void *reset_data)
145 struct efi_event *evt;
147 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
151 list_for_each_entry(evt, &efi_events, link) {
154 &efi_guid_event_group_reset_system)) {
155 efi_signal_event(evt);
159 switch (reset_type) {
162 case EFI_RESET_PLATFORM_SPECIFIC:
163 do_reset(NULL, 0, 0, NULL);
165 case EFI_RESET_SHUTDOWN:
166 #ifdef CONFIG_CMD_POWEROFF
167 do_poweroff(NULL, 0, 0, NULL);
176 * efi_get_time_boottime() - get current time at boot time
178 * This function implements the GetTime runtime service before
179 * SetVirtualAddressMap() is called.
181 * See the Unified Extensible Firmware Interface (UEFI) specification
184 * @time: pointer to structure to receive current time
185 * @capabilities: pointer to structure to receive RTC properties
186 * Returns: status code
188 static efi_status_t EFIAPI efi_get_time_boottime(
189 struct efi_time *time,
190 struct efi_time_cap *capabilities)
192 #ifdef CONFIG_EFI_GET_TIME
193 efi_status_t ret = EFI_SUCCESS;
197 EFI_ENTRY("%p %p", time, capabilities);
200 ret = EFI_INVALID_PARAMETER;
203 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
204 dm_rtc_get(dev, &tm)) {
205 ret = EFI_UNSUPPORTED;
208 if (dm_rtc_get(dev, &tm)) {
209 ret = EFI_DEVICE_ERROR;
213 memset(time, 0, sizeof(*time));
214 time->year = tm.tm_year;
215 time->month = tm.tm_mon;
216 time->day = tm.tm_mday;
217 time->hour = tm.tm_hour;
218 time->minute = tm.tm_min;
219 time->second = tm.tm_sec;
222 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
223 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
226 /* Set reasonable dummy values */
227 capabilities->resolution = 1; /* 1 Hz */
228 capabilities->accuracy = 100000000; /* 100 ppm */
229 capabilities->sets_to_zero = false;
232 return EFI_EXIT(ret);
234 EFI_ENTRY("%p %p", time, capabilities);
235 return EFI_EXIT(EFI_UNSUPPORTED);
239 #ifdef CONFIG_EFI_SET_TIME
242 * efi_validate_time() - checks if timestamp is valid
244 * @time: timestamp to validate
245 * Returns: 0 if timestamp is valid, 1 otherwise
247 static int efi_validate_time(struct efi_time *time)
250 time->year < 1900 || time->year > 9999 ||
251 !time->month || time->month > 12 || !time->day ||
252 time->day > rtc_month_days(time->month - 1, time->year) ||
253 time->hour > 23 || time->minute > 59 || time->second > 59 ||
254 time->nanosecond > 999999999 ||
256 ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
257 ((time->timezone < -1440 || time->timezone > 1440) &&
258 time->timezone != EFI_UNSPECIFIED_TIMEZONE));
264 * efi_set_time_boottime() - set current time
266 * This function implements the SetTime() runtime service before
267 * SetVirtualAddressMap() is called.
269 * See the Unified Extensible Firmware Interface (UEFI) specification
272 * @time: pointer to structure to with current time
273 * Returns: status code
275 static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
277 #ifdef CONFIG_EFI_SET_TIME
278 efi_status_t ret = EFI_SUCCESS;
282 EFI_ENTRY("%p", time);
284 if (efi_validate_time(time)) {
285 ret = EFI_INVALID_PARAMETER;
289 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
290 ret = EFI_UNSUPPORTED;
294 memset(&tm, 0, sizeof(tm));
295 tm.tm_year = time->year;
296 tm.tm_mon = time->month;
297 tm.tm_mday = time->day;
298 tm.tm_hour = time->hour;
299 tm.tm_min = time->minute;
300 tm.tm_sec = time->second;
301 tm.tm_isdst = time->daylight ==
302 (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
303 /* Calculate day of week */
304 rtc_calc_weekday(&tm);
306 if (dm_rtc_set(dev, &tm))
307 ret = EFI_DEVICE_ERROR;
309 return EFI_EXIT(ret);
311 EFI_ENTRY("%p", time);
312 return EFI_EXIT(EFI_UNSUPPORTED);
316 * efi_reset_system() - reset system
318 * This function implements the ResetSystem() runtime service after
319 * SetVirtualAddressMap() is called. It only executes an endless loop.
320 * Boards may override the helpers below to implement reset functionality.
322 * See the Unified Extensible Firmware Interface (UEFI) specification for
325 * @reset_type: type of reset to perform
326 * @reset_status: status code for the reset
327 * @data_size: size of reset_data
328 * @reset_data: information about the reset
330 void __weak __efi_runtime EFIAPI efi_reset_system(
331 enum efi_reset_type reset_type,
332 efi_status_t reset_status,
333 unsigned long data_size, void *reset_data)
335 /* Nothing we can do */
340 * efi_reset_system_init() - initialize the reset driver
342 * Boards may override this function to initialize the reset driver.
344 efi_status_t __weak efi_reset_system_init(void)
350 * efi_get_time() - get current time
352 * This function implements the GetTime runtime service after
353 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
354 * anymore only an error code is returned.
356 * See the Unified Extensible Firmware Interface (UEFI) specification
359 * @time: pointer to structure to receive current time
360 * @capabilities: pointer to structure to receive RTC properties
361 * Returns: status code
363 efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
364 struct efi_time *time,
365 struct efi_time_cap *capabilities)
367 return EFI_UNSUPPORTED;
371 * efi_set_time() - set current time
373 * This function implements the SetTime runtime service after
374 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
375 * anymore only an error code is returned.
377 * See the Unified Extensible Firmware Interface (UEFI) specification
380 * @time: pointer to structure to with current time
381 * Returns: status code
383 efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
385 return EFI_UNSUPPORTED;
388 struct efi_runtime_detach_list_struct {
393 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
395 /* do_reset is gone */
396 .ptr = &efi_runtime_services.reset_system,
397 .patchto = efi_reset_system,
399 /* RTC accessors are gone */
400 .ptr = &efi_runtime_services.get_time,
401 .patchto = &efi_get_time,
403 .ptr = &efi_runtime_services.set_time,
404 .patchto = &efi_set_time,
409 * efi_is_runtime_service_pointer() - check if pointer points to runtime table
411 * @p: pointer to check
412 * Return: true if the pointer points to a service function pointer in the
415 static bool efi_is_runtime_service_pointer(void *p)
417 return p >= (void *)&efi_runtime_services.get_time &&
418 p <= (void *)&efi_runtime_services.query_variable_info;
421 static __efi_runtime void efi_runtime_detach(void)
426 * Replace boottime functions by runtime functions
427 * TODO: move this step to ExitBootServices()
429 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
430 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
431 ulong *p = efi_runtime_detach_list[i].ptr;
433 debug("%s: Setting %p to %lx\n", __func__, p, patchto);
439 * efi_set_virtual_address_map_runtime() - change from physical to virtual
442 * This function implements the SetVirtualAddressMap() runtime service after
443 * it is first called.
445 * See the Unified Extensible Firmware Interface (UEFI) specification for
448 * @memory_map_size: size of the virtual map
449 * @descriptor_size: size of an entry in the map
450 * @descriptor_version: version of the map entries
451 * @virtmap: virtual address mapping information
452 * Return: status code EFI_UNSUPPORTED
454 static efi_status_t EFIAPI efi_set_virtual_address_map_runtime(
455 unsigned long memory_map_size,
456 unsigned long descriptor_size,
457 uint32_t descriptor_version,
458 struct efi_mem_desc *virtmap)
460 return EFI_UNSUPPORTED;
464 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
466 * This function implements the ConvertPointer() runtime service after
467 * the first call to SetVirtualAddressMap().
469 * See the Unified Extensible Firmware Interface (UEFI) specification for
472 * @debug_disposition: indicates if pointer may be converted to NULL
473 * @address: pointer to be converted
474 * Return: status code EFI_UNSUPPORTED
476 static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime(
477 efi_uintn_t debug_disposition, void **address)
479 return EFI_UNSUPPORTED;
482 static __efi_runtime void efi_relocate_runtime_table(ulong offset)
487 /* Relocate the runtime services pointers */
488 patchoff = offset - gd->relocaddr;
489 for (pos = (void **)&efi_runtime_services.get_time;
490 pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
496 * The entry for SetVirtualAddress() must point to a physical address.
497 * After the first execution the service must return EFI_UNSUPPORTED.
499 efi_runtime_services.set_virtual_address_map =
500 &efi_set_virtual_address_map_runtime;
503 * The entry for ConvertPointer() must point to a physical address.
504 * The service is not usable after SetVirtualAddress().
506 efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime;
509 efi_update_table_header_crc32(&efi_runtime_services.hdr);
512 /* Relocate EFI runtime to uboot_reloc_base = offset */
513 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
516 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
518 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
519 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
522 debug("%s: Relocating to offset=%lx\n", __func__, offset);
523 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
524 ulong base = CONFIG_SYS_TEXT_BASE;
528 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
530 /* The runtime services are updated in efi_runtime_detach() */
531 if (map && efi_is_runtime_service_pointer(p))
534 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
535 rel->info, *p, rel->offset);
537 switch (rel->info & R_MASK) {
540 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
542 newaddr = *p - lastoff + offset;
547 ulong symidx = rel->info >> SYM_INDEX;
548 extern struct dyn_sym __dyn_sym_start[];
549 newaddr = __dyn_sym_start[symidx].addr + offset;
551 newaddr -= CONFIG_SYS_TEXT_BASE;
557 printf("%s: Unknown relocation type %llx\n",
558 __func__, rel->info & R_MASK);
562 /* Check if the relocation is inside bounds */
563 if (map && ((newaddr < map->virtual_start) ||
564 newaddr > (map->virtual_start +
565 (map->num_pages << EFI_PAGE_SHIFT)))) {
566 printf("%s: Relocation at %p is out of range (%lx)\n",
567 __func__, p, newaddr);
571 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
573 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
574 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
581 invalidate_icache_all();
585 * efi_set_virtual_address_map() - change from physical to virtual mapping
587 * This function implements the SetVirtualAddressMap() runtime service.
589 * See the Unified Extensible Firmware Interface (UEFI) specification for
592 * @memory_map_size: size of the virtual map
593 * @descriptor_size: size of an entry in the map
594 * @descriptor_version: version of the map entries
595 * @virtmap: virtual address mapping information
596 * Return: status code
598 static efi_status_t EFIAPI efi_set_virtual_address_map(
599 unsigned long memory_map_size,
600 unsigned long descriptor_size,
601 uint32_t descriptor_version,
602 struct efi_mem_desc *virtmap)
604 int n = memory_map_size / descriptor_size;
606 int rt_code_sections = 0;
608 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
609 descriptor_version, virtmap);
613 * Further down we are cheating. While really we should implement
614 * SetVirtualAddressMap() events and ConvertPointer() to allow
615 * dynamically loaded drivers to expose runtime services, we don't
618 * So let's ensure we see exactly one single runtime section, as
619 * that is the built-in one. If we see more (or less), someone must
620 * have tried adding or removing to that which we don't support yet.
621 * In that case, let's better fail rather than expose broken runtime
624 for (i = 0; i < n; i++) {
625 struct efi_mem_desc *map = (void*)virtmap +
626 (descriptor_size * i);
628 if (map->type == EFI_RUNTIME_SERVICES_CODE)
632 if (rt_code_sections != 1) {
634 * We expose exactly one single runtime code section, so
635 * something is definitely going wrong.
637 return EFI_EXIT(EFI_INVALID_PARAMETER);
640 /* Rebind mmio pointers */
641 for (i = 0; i < n; i++) {
642 struct efi_mem_desc *map = (void*)virtmap +
643 (descriptor_size * i);
644 struct list_head *lhandle;
645 efi_physical_addr_t map_start = map->physical_start;
646 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
647 efi_physical_addr_t map_end = map_start + map_len;
648 u64 off = map->virtual_start - map_start;
650 /* Adjust all mmio pointers in this region */
651 list_for_each(lhandle, &efi_runtime_mmio) {
652 struct efi_runtime_mmio_list *lmmio;
654 lmmio = list_entry(lhandle,
655 struct efi_runtime_mmio_list,
657 if ((map_start <= lmmio->paddr) &&
658 (map_end >= lmmio->paddr)) {
659 uintptr_t new_addr = lmmio->paddr + off;
660 *lmmio->ptr = (void *)new_addr;
663 if ((map_start <= (uintptr_t)systab.tables) &&
664 (map_end >= (uintptr_t)systab.tables)) {
665 char *ptr = (char *)systab.tables;
668 systab.tables = (struct efi_configuration_table *)ptr;
673 * Some runtime services are implemented in a way that we can only offer
674 * them at boottime. Replace those function pointers.
676 * TODO: move this call to ExitBootServices().
678 efi_runtime_detach();
680 /* Relocate the runtime. See TODO above */
681 for (i = 0; i < n; i++) {
682 struct efi_mem_desc *map;
684 map = (void*)virtmap + (descriptor_size * i);
685 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
686 ulong new_offset = map->virtual_start -
687 map->physical_start + gd->relocaddr;
689 efi_relocate_runtime_table(new_offset);
690 efi_runtime_relocate(new_offset, map);
691 return EFI_EXIT(EFI_SUCCESS);
695 return EFI_EXIT(EFI_INVALID_PARAMETER);
699 * efi_add_runtime_mmio() - add memory-mapped IO region
701 * This function adds a memory-mapped IO region to the memory map to make it
702 * available at runtime.
704 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
706 * @len: size of the memory-mapped IO region
707 * Returns: status code
709 efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
711 struct efi_runtime_mmio_list *newmmio;
712 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
713 uint64_t addr = *(uintptr_t *)mmio_ptr;
716 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
718 return EFI_OUT_OF_RESOURCES;
720 newmmio = calloc(1, sizeof(*newmmio));
722 return EFI_OUT_OF_RESOURCES;
723 newmmio->ptr = mmio_ptr;
724 newmmio->paddr = *(uintptr_t *)mmio_ptr;
726 list_add_tail(&newmmio->link, &efi_runtime_mmio);
732 * In the second stage, U-Boot has disappeared. To isolate our runtime code
733 * that at this point still exists from the rest, we put it into a special
738 * This means that we can not rely on any code outside of this file in any
739 * function or variable below this line.
741 * Please keep everything fully self-contained and annotated with
742 * __efi_runtime and __efi_runtime_data markers.
746 * Relocate the EFI runtime stub to a different place. We need to call this
747 * the first time we expose the runtime interface to a user and on set virtual
752 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
754 * This function is used after SetVirtualAddressMap() is called as replacement
755 * for services that are not available anymore due to constraints of the U-Boot
758 * Return: EFI_UNSUPPORTED
760 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
762 return EFI_UNSUPPORTED;
766 * efi_update_capsule() - process information from operating system
768 * This function implements the UpdateCapsule() runtime service.
770 * See the Unified Extensible Firmware Interface (UEFI) specification for
773 * @capsule_header_array: pointer to array of virtual pointers
774 * @capsule_count: number of pointers in capsule_header_array
775 * @scatter_gather_list: pointer to arry of physical pointers
776 * Returns: status code
778 efi_status_t __efi_runtime EFIAPI efi_update_capsule(
779 struct efi_capsule_header **capsule_header_array,
780 efi_uintn_t capsule_count,
781 u64 scatter_gather_list)
783 return EFI_UNSUPPORTED;
787 * efi_query_capsule_caps() - check if capsule is supported
789 * This function implements the QueryCapsuleCapabilities() runtime service.
791 * See the Unified Extensible Firmware Interface (UEFI) specification for
794 * @capsule_header_array: pointer to array of virtual pointers
795 * @capsule_count: number of pointers in capsule_header_array
796 * @maximum_capsule_size: maximum capsule size
797 * @reset_type: type of reset needed for capsule update
798 * Returns: status code
800 efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
801 struct efi_capsule_header **capsule_header_array,
802 efi_uintn_t capsule_count,
803 u64 *maximum_capsule_size,
806 return EFI_UNSUPPORTED;
809 struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
811 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
812 .revision = EFI_SPECIFICATION_VERSION,
813 .headersize = sizeof(struct efi_runtime_services),
815 .get_time = &efi_get_time_boottime,
816 .set_time = &efi_set_time_boottime,
817 .get_wakeup_time = (void *)&efi_unimplemented,
818 .set_wakeup_time = (void *)&efi_unimplemented,
819 .set_virtual_address_map = &efi_set_virtual_address_map,
820 .convert_pointer = (void *)&efi_unimplemented,
821 .get_variable = efi_get_variable,
822 .get_next_variable_name = efi_get_next_variable_name,
823 .set_variable = efi_set_variable,
824 .get_next_high_mono_count = (void *)&efi_unimplemented,
825 .reset_system = &efi_reset_system_boottime,
826 .update_capsule = efi_update_capsule,
827 .query_capsule_caps = efi_query_capsule_caps,
828 .query_variable_info = efi_query_variable_info,