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);
29 static efi_status_t __efi_runtime EFIAPI efi_device_error(void);
30 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void);
33 * TODO(sjg@chromium.org): These defines and structures should come from the ELF
34 * header for each architecture (or a generic header) rather than being repeated
37 #if defined(__aarch64__)
38 #define R_RELATIVE R_AARCH64_RELATIVE
39 #define R_MASK 0xffffffffULL
41 #elif defined(__arm__)
42 #define R_RELATIVE R_ARM_RELATIVE
43 #define R_MASK 0xffULL
44 #elif defined(__i386__)
45 #define R_RELATIVE R_386_RELATIVE
46 #define R_MASK 0xffULL
47 #elif defined(__x86_64__)
48 #define R_RELATIVE R_X86_64_RELATIVE
49 #define R_MASK 0xffffffffULL
51 #elif defined(__riscv)
52 #define R_RELATIVE R_RISCV_RELATIVE
53 #define R_MASK 0xffULL
62 #if (__riscv_xlen == 32)
63 #define R_ABSOLUTE R_RISCV_32
65 #elif (__riscv_xlen == 64)
66 #define R_ABSOLUTE R_RISCV_64
69 #error unknown riscv target
72 #error Need to add relocation awareness
87 * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI
88 * payload are running concurrently at the same time. In this mode, we can
89 * handle a good number of runtime callbacks
92 efi_status_t efi_init_runtime_supported(void)
94 u16 efi_runtime_services_supported = 0;
97 * This value must be synced with efi_runtime_detach_list
98 * as well as efi_runtime_services.
100 #if CONFIG_IS_ENABLED(ARCH_BCM283X) || \
101 CONFIG_IS_ENABLED(FSL_LAYERSCAPE) || \
102 CONFIG_IS_ENABLED(SYSRESET_X86) || \
103 CONFIG_IS_ENABLED(PSCI_RESET)
104 efi_runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM;
106 efi_runtime_services_supported |=
107 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP;
108 return EFI_CALL(efi_set_variable(L"RuntimeServicesSupported",
109 &efi_global_variable_guid,
110 EFI_VARIABLE_BOOTSERVICE_ACCESS |
111 EFI_VARIABLE_RUNTIME_ACCESS,
112 sizeof(efi_runtime_services_supported),
113 &efi_runtime_services_supported));
117 * efi_update_table_header_crc32() - Update crc32 in table header
121 void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
124 table->crc32 = crc32(0, (const unsigned char *)table,
129 * efi_reset_system_boottime() - reset system at boot time
131 * This function implements the ResetSystem() runtime service before
132 * SetVirtualAddressMap() is called.
134 * See the Unified Extensible Firmware Interface (UEFI) specification for
137 * @reset_type: type of reset to perform
138 * @reset_status: status code for the reset
139 * @data_size: size of reset_data
140 * @reset_data: information about the reset
142 static void EFIAPI efi_reset_system_boottime(
143 enum efi_reset_type reset_type,
144 efi_status_t reset_status,
145 unsigned long data_size, void *reset_data)
147 struct efi_event *evt;
149 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
153 list_for_each_entry(evt, &efi_events, link) {
156 &efi_guid_event_group_reset_system)) {
157 efi_signal_event(evt);
161 switch (reset_type) {
164 case EFI_RESET_PLATFORM_SPECIFIC:
165 do_reset(NULL, 0, 0, NULL);
167 case EFI_RESET_SHUTDOWN:
168 #ifdef CONFIG_CMD_POWEROFF
169 do_poweroff(NULL, 0, 0, NULL);
178 * efi_get_time_boottime() - get current time at boot time
180 * This function implements the GetTime runtime service before
181 * SetVirtualAddressMap() is called.
183 * See the Unified Extensible Firmware Interface (UEFI) specification
186 * @time: pointer to structure to receive current time
187 * @capabilities: pointer to structure to receive RTC properties
188 * Returns: status code
190 static efi_status_t EFIAPI efi_get_time_boottime(
191 struct efi_time *time,
192 struct efi_time_cap *capabilities)
194 #ifdef CONFIG_EFI_GET_TIME
195 efi_status_t ret = EFI_SUCCESS;
199 EFI_ENTRY("%p %p", time, capabilities);
202 ret = EFI_INVALID_PARAMETER;
205 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
206 dm_rtc_get(dev, &tm)) {
207 ret = EFI_UNSUPPORTED;
210 if (dm_rtc_get(dev, &tm)) {
211 ret = EFI_DEVICE_ERROR;
215 memset(time, 0, sizeof(*time));
216 time->year = tm.tm_year;
217 time->month = tm.tm_mon;
218 time->day = tm.tm_mday;
219 time->hour = tm.tm_hour;
220 time->minute = tm.tm_min;
221 time->second = tm.tm_sec;
224 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
225 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
228 /* Set reasonable dummy values */
229 capabilities->resolution = 1; /* 1 Hz */
230 capabilities->accuracy = 100000000; /* 100 ppm */
231 capabilities->sets_to_zero = false;
234 return EFI_EXIT(ret);
236 EFI_ENTRY("%p %p", time, capabilities);
237 return EFI_EXIT(EFI_UNSUPPORTED);
241 #ifdef CONFIG_EFI_SET_TIME
244 * efi_validate_time() - checks if timestamp is valid
246 * @time: timestamp to validate
247 * Returns: 0 if timestamp is valid, 1 otherwise
249 static int efi_validate_time(struct efi_time *time)
252 time->year < 1900 || time->year > 9999 ||
253 !time->month || time->month > 12 || !time->day ||
254 time->day > rtc_month_days(time->month - 1, time->year) ||
255 time->hour > 23 || time->minute > 59 || time->second > 59 ||
256 time->nanosecond > 999999999 ||
258 ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
259 ((time->timezone < -1440 || time->timezone > 1440) &&
260 time->timezone != EFI_UNSPECIFIED_TIMEZONE));
266 * efi_set_time_boottime() - set current time
268 * This function implements the SetTime() runtime service before
269 * SetVirtualAddressMap() is called.
271 * See the Unified Extensible Firmware Interface (UEFI) specification
274 * @time: pointer to structure to with current time
275 * Returns: status code
277 static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
279 #ifdef CONFIG_EFI_SET_TIME
280 efi_status_t ret = EFI_SUCCESS;
284 EFI_ENTRY("%p", time);
286 if (efi_validate_time(time)) {
287 ret = EFI_INVALID_PARAMETER;
291 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
292 ret = EFI_UNSUPPORTED;
296 memset(&tm, 0, sizeof(tm));
297 tm.tm_year = time->year;
298 tm.tm_mon = time->month;
299 tm.tm_mday = time->day;
300 tm.tm_hour = time->hour;
301 tm.tm_min = time->minute;
302 tm.tm_sec = time->second;
303 tm.tm_isdst = time->daylight ==
304 (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
305 /* Calculate day of week */
306 rtc_calc_weekday(&tm);
308 if (dm_rtc_set(dev, &tm))
309 ret = EFI_DEVICE_ERROR;
311 return EFI_EXIT(ret);
313 EFI_ENTRY("%p", time);
314 return EFI_EXIT(EFI_UNSUPPORTED);
318 * efi_reset_system() - reset system
320 * This function implements the ResetSystem() runtime service after
321 * SetVirtualAddressMap() is called. It only executes an endless loop.
322 * Boards may override the helpers below to implement reset functionality.
324 * See the Unified Extensible Firmware Interface (UEFI) specification for
327 * @reset_type: type of reset to perform
328 * @reset_status: status code for the reset
329 * @data_size: size of reset_data
330 * @reset_data: information about the reset
332 void __weak __efi_runtime EFIAPI efi_reset_system(
333 enum efi_reset_type reset_type,
334 efi_status_t reset_status,
335 unsigned long data_size, void *reset_data)
337 /* Nothing we can do */
342 * efi_reset_system_init() - initialize the reset driver
344 * Boards may override this function to initialize the reset driver.
346 efi_status_t __weak efi_reset_system_init(void)
352 * efi_get_time() - get current time
354 * This function implements the GetTime runtime service after
355 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
356 * anymore only an error code is returned.
358 * See the Unified Extensible Firmware Interface (UEFI) specification
361 * @time: pointer to structure to receive current time
362 * @capabilities: pointer to structure to receive RTC properties
363 * Returns: status code
365 efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
366 struct efi_time *time,
367 struct efi_time_cap *capabilities)
369 return EFI_UNSUPPORTED;
373 * efi_set_time() - set current time
375 * This function implements the SetTime runtime service after
376 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
377 * anymore only an error code is returned.
379 * See the Unified Extensible Firmware Interface (UEFI) specification
382 * @time: pointer to structure to with current time
383 * Returns: status code
385 efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
387 return EFI_UNSUPPORTED;
390 struct efi_runtime_detach_list_struct {
395 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
397 /* do_reset is gone */
398 .ptr = &efi_runtime_services.reset_system,
399 .patchto = efi_reset_system,
401 /* invalidate_*cache_all are gone */
402 .ptr = &efi_runtime_services.set_virtual_address_map,
403 .patchto = &efi_unimplemented,
405 /* RTC accessors are gone */
406 .ptr = &efi_runtime_services.get_time,
407 .patchto = &efi_get_time,
409 .ptr = &efi_runtime_services.set_time,
410 .patchto = &efi_set_time,
412 /* Clean up system table */
413 .ptr = &systab.con_in,
416 /* Clean up system table */
417 .ptr = &systab.con_out,
420 /* Clean up system table */
421 .ptr = &systab.std_err,
424 /* Clean up system table */
425 .ptr = &systab.boottime,
428 .ptr = &efi_runtime_services.get_variable,
429 .patchto = &efi_device_error,
431 .ptr = &efi_runtime_services.get_next_variable_name,
432 .patchto = &efi_device_error,
434 .ptr = &efi_runtime_services.set_variable,
435 .patchto = &efi_device_error,
439 static bool efi_runtime_tobedetached(void *p)
443 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
444 if (efi_runtime_detach_list[i].ptr == p)
450 static void efi_runtime_detach(ulong offset)
453 ulong patchoff = offset - (ulong)gd->relocaddr;
455 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
456 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
457 ulong *p = efi_runtime_detach_list[i].ptr;
458 ulong newaddr = patchto ? (patchto + patchoff) : 0;
460 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
465 efi_update_table_header_crc32(&efi_runtime_services.hdr);
468 /* Relocate EFI runtime to uboot_reloc_base = offset */
469 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
472 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
474 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
475 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
478 debug("%s: Relocating to offset=%lx\n", __func__, offset);
479 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
480 ulong base = CONFIG_SYS_TEXT_BASE;
484 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
486 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
487 rel->info, *p, rel->offset);
489 switch (rel->info & R_MASK) {
492 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
494 newaddr = *p - lastoff + offset;
499 ulong symidx = rel->info >> SYM_INDEX;
500 extern struct dyn_sym __dyn_sym_start[];
501 newaddr = __dyn_sym_start[symidx].addr + offset;
503 newaddr -= CONFIG_SYS_TEXT_BASE;
509 if (!efi_runtime_tobedetached(p))
510 printf("%s: Unknown relocation type %llx\n",
511 __func__, rel->info & R_MASK);
515 /* Check if the relocation is inside bounds */
516 if (map && ((newaddr < map->virtual_start) ||
517 newaddr > (map->virtual_start +
518 (map->num_pages << EFI_PAGE_SHIFT)))) {
519 if (!efi_runtime_tobedetached(p))
520 printf("%s: Relocation at %p is out of "
521 "range (%lx)\n", __func__, p, newaddr);
525 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
527 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
528 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
535 invalidate_icache_all();
539 * efi_set_virtual_address_map() - change from physical to virtual mapping
541 * This function implements the SetVirtualAddressMap() runtime service.
543 * See the Unified Extensible Firmware Interface (UEFI) specification for
546 * @memory_map_size: size of the virtual map
547 * @descriptor_size: size of an entry in the map
548 * @descriptor_version: version of the map entries
549 * @virtmap: virtual address mapping information
550 * Return: status code
552 static efi_status_t EFIAPI efi_set_virtual_address_map(
553 unsigned long memory_map_size,
554 unsigned long descriptor_size,
555 uint32_t descriptor_version,
556 struct efi_mem_desc *virtmap)
558 int n = memory_map_size / descriptor_size;
560 int rt_code_sections = 0;
562 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
563 descriptor_version, virtmap);
567 * Further down we are cheating. While really we should implement
568 * SetVirtualAddressMap() events and ConvertPointer() to allow
569 * dynamically loaded drivers to expose runtime services, we don't
572 * So let's ensure we see exactly one single runtime section, as
573 * that is the built-in one. If we see more (or less), someone must
574 * have tried adding or removing to that which we don't support yet.
575 * In that case, let's better fail rather than expose broken runtime
578 for (i = 0; i < n; i++) {
579 struct efi_mem_desc *map = (void*)virtmap +
580 (descriptor_size * i);
582 if (map->type == EFI_RUNTIME_SERVICES_CODE)
586 if (rt_code_sections != 1) {
588 * We expose exactly one single runtime code section, so
589 * something is definitely going wrong.
591 return EFI_EXIT(EFI_INVALID_PARAMETER);
594 /* Rebind mmio pointers */
595 for (i = 0; i < n; i++) {
596 struct efi_mem_desc *map = (void*)virtmap +
597 (descriptor_size * i);
598 struct list_head *lhandle;
599 efi_physical_addr_t map_start = map->physical_start;
600 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
601 efi_physical_addr_t map_end = map_start + map_len;
602 u64 off = map->virtual_start - map_start;
604 /* Adjust all mmio pointers in this region */
605 list_for_each(lhandle, &efi_runtime_mmio) {
606 struct efi_runtime_mmio_list *lmmio;
608 lmmio = list_entry(lhandle,
609 struct efi_runtime_mmio_list,
611 if ((map_start <= lmmio->paddr) &&
612 (map_end >= lmmio->paddr)) {
613 uintptr_t new_addr = lmmio->paddr + off;
614 *lmmio->ptr = (void *)new_addr;
617 if ((map_start <= (uintptr_t)systab.tables) &&
618 (map_end >= (uintptr_t)systab.tables)) {
619 char *ptr = (char *)systab.tables;
622 systab.tables = (struct efi_configuration_table *)ptr;
626 /* Move the actual runtime code over */
627 for (i = 0; i < n; i++) {
628 struct efi_mem_desc *map;
630 map = (void*)virtmap + (descriptor_size * i);
631 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
632 ulong new_offset = map->virtual_start -
633 map->physical_start + gd->relocaddr;
635 efi_runtime_relocate(new_offset, map);
636 /* Once we're virtual, we can no longer handle
638 efi_runtime_detach(new_offset);
639 return EFI_EXIT(EFI_SUCCESS);
643 return EFI_EXIT(EFI_INVALID_PARAMETER);
647 * efi_add_runtime_mmio() - add memory-mapped IO region
649 * This function adds a memory-mapped IO region to the memory map to make it
650 * available at runtime.
652 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
654 * @len: size of the memory-mapped IO region
655 * Returns: status code
657 efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
659 struct efi_runtime_mmio_list *newmmio;
660 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
661 uint64_t addr = *(uintptr_t *)mmio_ptr;
664 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
666 return EFI_OUT_OF_RESOURCES;
668 newmmio = calloc(1, sizeof(*newmmio));
670 return EFI_OUT_OF_RESOURCES;
671 newmmio->ptr = mmio_ptr;
672 newmmio->paddr = *(uintptr_t *)mmio_ptr;
674 list_add_tail(&newmmio->link, &efi_runtime_mmio);
680 * In the second stage, U-Boot has disappeared. To isolate our runtime code
681 * that at this point still exists from the rest, we put it into a special
686 * This means that we can not rely on any code outside of this file in any
687 * function or variable below this line.
689 * Please keep everything fully self-contained and annotated with
690 * __efi_runtime and __efi_runtime_data markers.
694 * Relocate the EFI runtime stub to a different place. We need to call this
695 * the first time we expose the runtime interface to a user and on set virtual
700 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
702 * This function is used after SetVirtualAddressMap() is called as replacement
703 * for services that are not available anymore due to constraints of the U-Boot
706 * Return: EFI_UNSUPPORTED
708 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
710 return EFI_UNSUPPORTED;
714 * efi_device_error() - replacement function, returns EFI_DEVICE_ERROR
716 * This function is used after SetVirtualAddressMap() is called as replacement
717 * for services that are not available anymore due to constraints of the U-Boot
720 * Return: EFI_DEVICE_ERROR
722 static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
724 return EFI_DEVICE_ERROR;
728 * efi_invalid_parameter() - replacement function, returns EFI_INVALID_PARAMETER
730 * This function is used after SetVirtualAddressMap() is called as replacement
731 * for services that are not available anymore due to constraints of the U-Boot
734 * Return: EFI_INVALID_PARAMETER
736 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
738 return EFI_INVALID_PARAMETER;
742 * efi_update_capsule() - process information from operating system
744 * This function implements the UpdateCapsule() runtime service.
746 * See the Unified Extensible Firmware Interface (UEFI) specification for
749 * @capsule_header_array: pointer to array of virtual pointers
750 * @capsule_count: number of pointers in capsule_header_array
751 * @scatter_gather_list: pointer to arry of physical pointers
752 * Returns: status code
754 efi_status_t __efi_runtime EFIAPI efi_update_capsule(
755 struct efi_capsule_header **capsule_header_array,
756 efi_uintn_t capsule_count,
757 u64 scatter_gather_list)
759 return EFI_UNSUPPORTED;
763 * efi_query_capsule_caps() - check if capsule is supported
765 * This function implements the QueryCapsuleCapabilities() runtime service.
767 * See the Unified Extensible Firmware Interface (UEFI) specification for
770 * @capsule_header_array: pointer to array of virtual pointers
771 * @capsule_count: number of pointers in capsule_header_array
772 * @maximum_capsule_size: maximum capsule size
773 * @reset_type: type of reset needed for capsule update
774 * Returns: status code
776 efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
777 struct efi_capsule_header **capsule_header_array,
778 efi_uintn_t capsule_count,
779 u64 *maximum_capsule_size,
782 return EFI_UNSUPPORTED;
786 * efi_query_variable_info() - get information about EFI variables
788 * This function implements the QueryVariableInfo() runtime service.
790 * See the Unified Extensible Firmware Interface (UEFI) specification for
793 * @attributes: bitmask to select variables to be
795 * @maximum_variable_storage_size: maximum size of storage area for the
796 * selected variable types
797 * @remaining_variable_storage_size: remaining size of storage are for the
798 * selected variable types
799 * @maximum_variable_size: maximum size of a variable of the
801 * Returns: status code
803 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
805 u64 *maximum_variable_storage_size,
806 u64 *remaining_variable_storage_size,
807 u64 *maximum_variable_size)
809 return EFI_UNSUPPORTED;
812 struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
814 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
815 .revision = EFI_SPECIFICATION_VERSION,
816 .headersize = sizeof(struct efi_runtime_services),
818 .get_time = &efi_get_time_boottime,
819 .set_time = &efi_set_time_boottime,
820 .get_wakeup_time = (void *)&efi_unimplemented,
821 .set_wakeup_time = (void *)&efi_unimplemented,
822 .set_virtual_address_map = &efi_set_virtual_address_map,
823 .convert_pointer = (void *)&efi_invalid_parameter,
824 .get_variable = efi_get_variable,
825 .get_next_variable_name = efi_get_next_variable_name,
826 .set_variable = efi_set_variable,
827 .get_next_high_mono_count = (void *)&efi_device_error,
828 .reset_system = &efi_reset_system_boottime,
829 .update_capsule = efi_update_capsule,
830 .query_capsule_caps = efi_query_capsule_caps,
831 .query_variable_info = efi_query_variable_info,