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 #ifdef CONFIG_EFI_HAVE_RUNTIME_RESET
99 efi_runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM;
101 efi_runtime_services_supported |=
102 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP;
103 return EFI_CALL(efi_set_variable(L"RuntimeServicesSupported",
104 &efi_global_variable_guid,
105 EFI_VARIABLE_BOOTSERVICE_ACCESS |
106 EFI_VARIABLE_RUNTIME_ACCESS,
107 sizeof(efi_runtime_services_supported),
108 &efi_runtime_services_supported));
112 * efi_update_table_header_crc32() - Update crc32 in table header
116 void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
119 table->crc32 = crc32(0, (const unsigned char *)table,
124 * efi_reset_system_boottime() - reset system at boot time
126 * This function implements the ResetSystem() runtime service before
127 * SetVirtualAddressMap() is called.
129 * See the Unified Extensible Firmware Interface (UEFI) specification for
132 * @reset_type: type of reset to perform
133 * @reset_status: status code for the reset
134 * @data_size: size of reset_data
135 * @reset_data: information about the reset
137 static void EFIAPI efi_reset_system_boottime(
138 enum efi_reset_type reset_type,
139 efi_status_t reset_status,
140 unsigned long data_size, void *reset_data)
142 struct efi_event *evt;
144 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
148 list_for_each_entry(evt, &efi_events, link) {
151 &efi_guid_event_group_reset_system)) {
152 efi_signal_event(evt);
156 switch (reset_type) {
159 case EFI_RESET_PLATFORM_SPECIFIC:
160 do_reset(NULL, 0, 0, NULL);
162 case EFI_RESET_SHUTDOWN:
163 #ifdef CONFIG_CMD_POWEROFF
164 do_poweroff(NULL, 0, 0, NULL);
173 * efi_get_time_boottime() - get current time at boot time
175 * This function implements the GetTime runtime service before
176 * SetVirtualAddressMap() is called.
178 * See the Unified Extensible Firmware Interface (UEFI) specification
181 * @time: pointer to structure to receive current time
182 * @capabilities: pointer to structure to receive RTC properties
183 * Returns: status code
185 static efi_status_t EFIAPI efi_get_time_boottime(
186 struct efi_time *time,
187 struct efi_time_cap *capabilities)
189 #ifdef CONFIG_EFI_GET_TIME
190 efi_status_t ret = EFI_SUCCESS;
194 EFI_ENTRY("%p %p", time, capabilities);
197 ret = EFI_INVALID_PARAMETER;
200 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
201 dm_rtc_get(dev, &tm)) {
202 ret = EFI_UNSUPPORTED;
205 if (dm_rtc_get(dev, &tm)) {
206 ret = EFI_DEVICE_ERROR;
210 memset(time, 0, sizeof(*time));
211 time->year = tm.tm_year;
212 time->month = tm.tm_mon;
213 time->day = tm.tm_mday;
214 time->hour = tm.tm_hour;
215 time->minute = tm.tm_min;
216 time->second = tm.tm_sec;
219 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
220 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
223 /* Set reasonable dummy values */
224 capabilities->resolution = 1; /* 1 Hz */
225 capabilities->accuracy = 100000000; /* 100 ppm */
226 capabilities->sets_to_zero = false;
229 return EFI_EXIT(ret);
231 EFI_ENTRY("%p %p", time, capabilities);
232 return EFI_EXIT(EFI_UNSUPPORTED);
236 #ifdef CONFIG_EFI_SET_TIME
239 * efi_validate_time() - checks if timestamp is valid
241 * @time: timestamp to validate
242 * Returns: 0 if timestamp is valid, 1 otherwise
244 static int efi_validate_time(struct efi_time *time)
247 time->year < 1900 || time->year > 9999 ||
248 !time->month || time->month > 12 || !time->day ||
249 time->day > rtc_month_days(time->month - 1, time->year) ||
250 time->hour > 23 || time->minute > 59 || time->second > 59 ||
251 time->nanosecond > 999999999 ||
253 ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
254 ((time->timezone < -1440 || time->timezone > 1440) &&
255 time->timezone != EFI_UNSPECIFIED_TIMEZONE));
261 * efi_set_time_boottime() - set current time
263 * This function implements the SetTime() runtime service before
264 * SetVirtualAddressMap() is called.
266 * See the Unified Extensible Firmware Interface (UEFI) specification
269 * @time: pointer to structure to with current time
270 * Returns: status code
272 static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
274 #ifdef CONFIG_EFI_SET_TIME
275 efi_status_t ret = EFI_SUCCESS;
279 EFI_ENTRY("%p", time);
281 if (efi_validate_time(time)) {
282 ret = EFI_INVALID_PARAMETER;
286 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
287 ret = EFI_UNSUPPORTED;
291 memset(&tm, 0, sizeof(tm));
292 tm.tm_year = time->year;
293 tm.tm_mon = time->month;
294 tm.tm_mday = time->day;
295 tm.tm_hour = time->hour;
296 tm.tm_min = time->minute;
297 tm.tm_sec = time->second;
298 tm.tm_isdst = time->daylight ==
299 (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
300 /* Calculate day of week */
301 rtc_calc_weekday(&tm);
303 if (dm_rtc_set(dev, &tm))
304 ret = EFI_DEVICE_ERROR;
306 return EFI_EXIT(ret);
308 EFI_ENTRY("%p", time);
309 return EFI_EXIT(EFI_UNSUPPORTED);
313 * efi_reset_system() - reset system
315 * This function implements the ResetSystem() runtime service after
316 * SetVirtualAddressMap() is called. It only executes an endless loop.
317 * Boards may override the helpers below to implement reset functionality.
319 * See the Unified Extensible Firmware Interface (UEFI) specification for
322 * @reset_type: type of reset to perform
323 * @reset_status: status code for the reset
324 * @data_size: size of reset_data
325 * @reset_data: information about the reset
327 void __weak __efi_runtime EFIAPI efi_reset_system(
328 enum efi_reset_type reset_type,
329 efi_status_t reset_status,
330 unsigned long data_size, void *reset_data)
332 /* Nothing we can do */
337 * efi_reset_system_init() - initialize the reset driver
339 * Boards may override this function to initialize the reset driver.
341 efi_status_t __weak efi_reset_system_init(void)
347 * efi_get_time() - get current time
349 * This function implements the GetTime runtime service after
350 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
351 * anymore only an error code is returned.
353 * See the Unified Extensible Firmware Interface (UEFI) specification
356 * @time: pointer to structure to receive current time
357 * @capabilities: pointer to structure to receive RTC properties
358 * Returns: status code
360 efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
361 struct efi_time *time,
362 struct efi_time_cap *capabilities)
364 return EFI_UNSUPPORTED;
368 * efi_set_time() - set current time
370 * This function implements the SetTime runtime service after
371 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
372 * anymore only an error code is returned.
374 * See the Unified Extensible Firmware Interface (UEFI) specification
377 * @time: pointer to structure to with current time
378 * Returns: status code
380 efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
382 return EFI_UNSUPPORTED;
386 * efi_is_runtime_service_pointer() - check if pointer points to runtime table
388 * @p: pointer to check
389 * Return: true if the pointer points to a service function pointer in the
392 static bool efi_is_runtime_service_pointer(void *p)
394 return (p >= (void *)&efi_runtime_services.get_time &&
395 p <= (void *)&efi_runtime_services.query_variable_info) ||
396 p == (void *)&efi_events.prev ||
397 p == (void *)&efi_events.next;
401 * efi_runtime_detach() - detach unimplemented runtime functions
403 void efi_runtime_detach(void)
405 efi_runtime_services.reset_system = efi_reset_system;
406 efi_runtime_services.get_time = efi_get_time;
407 efi_runtime_services.set_time = efi_set_time;
410 efi_update_table_header_crc32(&efi_runtime_services.hdr);
414 * efi_set_virtual_address_map_runtime() - change from physical to virtual
417 * This function implements the SetVirtualAddressMap() runtime service after
418 * it is first called.
420 * See the Unified Extensible Firmware Interface (UEFI) specification for
423 * @memory_map_size: size of the virtual map
424 * @descriptor_size: size of an entry in the map
425 * @descriptor_version: version of the map entries
426 * @virtmap: virtual address mapping information
427 * Return: status code EFI_UNSUPPORTED
429 static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map_runtime(
430 unsigned long memory_map_size,
431 unsigned long descriptor_size,
432 uint32_t descriptor_version,
433 struct efi_mem_desc *virtmap)
435 return EFI_UNSUPPORTED;
439 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
441 * This function implements the ConvertPointer() runtime service after
442 * the first call to SetVirtualAddressMap().
444 * See the Unified Extensible Firmware Interface (UEFI) specification for
447 * @debug_disposition: indicates if pointer may be converted to NULL
448 * @address: pointer to be converted
449 * Return: status code EFI_UNSUPPORTED
451 static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime(
452 efi_uintn_t debug_disposition, void **address)
454 return EFI_UNSUPPORTED;
457 static __efi_runtime void efi_relocate_runtime_table(ulong offset)
462 /* Relocate the runtime services pointers */
463 patchoff = offset - gd->relocaddr;
464 for (pos = (void **)&efi_runtime_services.get_time;
465 pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
471 * The entry for SetVirtualAddress() must point to a physical address.
472 * After the first execution the service must return EFI_UNSUPPORTED.
474 efi_runtime_services.set_virtual_address_map =
475 &efi_set_virtual_address_map_runtime;
478 * The entry for ConvertPointer() must point to a physical address.
479 * The service is not usable after SetVirtualAddress().
481 efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime;
484 efi_update_table_header_crc32(&efi_runtime_services.hdr);
487 /* Relocate EFI runtime to uboot_reloc_base = offset */
488 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
491 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
493 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
494 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
497 debug("%s: Relocating to offset=%lx\n", __func__, offset);
498 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
499 ulong base = CONFIG_SYS_TEXT_BASE;
503 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
505 /* The runtime services are updated in efi_runtime_detach() */
506 if (map && efi_is_runtime_service_pointer(p))
509 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
510 rel->info, *p, rel->offset);
512 switch (rel->info & R_MASK) {
515 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
517 newaddr = *p - lastoff + offset;
522 ulong symidx = rel->info >> SYM_INDEX;
523 extern struct dyn_sym __dyn_sym_start[];
524 newaddr = __dyn_sym_start[symidx].addr + offset;
526 newaddr -= CONFIG_SYS_TEXT_BASE;
532 printf("%s: Unknown relocation type %llx\n",
533 __func__, rel->info & R_MASK);
537 /* Check if the relocation is inside bounds */
538 if (map && ((newaddr < map->virtual_start) ||
539 newaddr > (map->virtual_start +
540 (map->num_pages << EFI_PAGE_SHIFT)))) {
541 printf("%s: Relocation at %p is out of range (%lx)\n",
542 __func__, p, newaddr);
546 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
548 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
549 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
556 invalidate_icache_all();
560 * efi_set_virtual_address_map() - change from physical to virtual mapping
562 * This function implements the SetVirtualAddressMap() runtime service.
564 * See the Unified Extensible Firmware Interface (UEFI) specification for
567 * @memory_map_size: size of the virtual map
568 * @descriptor_size: size of an entry in the map
569 * @descriptor_version: version of the map entries
570 * @virtmap: virtual address mapping information
571 * Return: status code
573 static efi_status_t EFIAPI efi_set_virtual_address_map(
574 unsigned long memory_map_size,
575 unsigned long descriptor_size,
576 uint32_t descriptor_version,
577 struct efi_mem_desc *virtmap)
579 int n = memory_map_size / descriptor_size;
581 int rt_code_sections = 0;
582 struct efi_event *event;
584 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
585 descriptor_version, virtmap);
589 * Further down we are cheating. While really we should implement
590 * SetVirtualAddressMap() events and ConvertPointer() to allow
591 * dynamically loaded drivers to expose runtime services, we don't
594 * So let's ensure we see exactly one single runtime section, as
595 * that is the built-in one. If we see more (or less), someone must
596 * have tried adding or removing to that which we don't support yet.
597 * In that case, let's better fail rather than expose broken runtime
600 for (i = 0; i < n; i++) {
601 struct efi_mem_desc *map = (void*)virtmap +
602 (descriptor_size * i);
604 if (map->type == EFI_RUNTIME_SERVICES_CODE)
608 if (rt_code_sections != 1) {
610 * We expose exactly one single runtime code section, so
611 * something is definitely going wrong.
613 return EFI_EXIT(EFI_INVALID_PARAMETER);
616 /* Notify EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
617 list_for_each_entry(event, &efi_events, link) {
618 if (event->notify_function)
619 EFI_CALL_VOID(event->notify_function(
620 event, event->notify_context));
623 /* Rebind mmio pointers */
624 for (i = 0; i < n; i++) {
625 struct efi_mem_desc *map = (void*)virtmap +
626 (descriptor_size * i);
627 struct list_head *lhandle;
628 efi_physical_addr_t map_start = map->physical_start;
629 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
630 efi_physical_addr_t map_end = map_start + map_len;
631 u64 off = map->virtual_start - map_start;
633 /* Adjust all mmio pointers in this region */
634 list_for_each(lhandle, &efi_runtime_mmio) {
635 struct efi_runtime_mmio_list *lmmio;
637 lmmio = list_entry(lhandle,
638 struct efi_runtime_mmio_list,
640 if ((map_start <= lmmio->paddr) &&
641 (map_end >= lmmio->paddr)) {
642 uintptr_t new_addr = lmmio->paddr + off;
643 *lmmio->ptr = (void *)new_addr;
646 if ((map_start <= (uintptr_t)systab.tables) &&
647 (map_end >= (uintptr_t)systab.tables)) {
648 char *ptr = (char *)systab.tables;
651 systab.tables = (struct efi_configuration_table *)ptr;
656 * Some runtime services are implemented in a way that we can only offer
657 * them at boottime. Replace those function pointers.
659 * TODO: move this call to ExitBootServices().
661 efi_runtime_detach();
663 /* Relocate the runtime. See TODO above */
664 for (i = 0; i < n; i++) {
665 struct efi_mem_desc *map;
667 map = (void*)virtmap + (descriptor_size * i);
668 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
669 ulong new_offset = map->virtual_start -
670 map->physical_start + gd->relocaddr;
672 efi_relocate_runtime_table(new_offset);
673 efi_runtime_relocate(new_offset, map);
674 return EFI_EXIT(EFI_SUCCESS);
678 return EFI_EXIT(EFI_INVALID_PARAMETER);
682 * efi_add_runtime_mmio() - add memory-mapped IO region
684 * This function adds a memory-mapped IO region to the memory map to make it
685 * available at runtime.
687 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
689 * @len: size of the memory-mapped IO region
690 * Returns: status code
692 efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
694 struct efi_runtime_mmio_list *newmmio;
695 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
696 uint64_t addr = *(uintptr_t *)mmio_ptr;
699 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
701 return EFI_OUT_OF_RESOURCES;
703 newmmio = calloc(1, sizeof(*newmmio));
705 return EFI_OUT_OF_RESOURCES;
706 newmmio->ptr = mmio_ptr;
707 newmmio->paddr = *(uintptr_t *)mmio_ptr;
709 list_add_tail(&newmmio->link, &efi_runtime_mmio);
715 * In the second stage, U-Boot has disappeared. To isolate our runtime code
716 * that at this point still exists from the rest, we put it into a special
721 * This means that we can not rely on any code outside of this file in any
722 * function or variable below this line.
724 * Please keep everything fully self-contained and annotated with
725 * __efi_runtime and __efi_runtime_data markers.
729 * Relocate the EFI runtime stub to a different place. We need to call this
730 * the first time we expose the runtime interface to a user and on set virtual
735 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
737 * This function is used after SetVirtualAddressMap() is called as replacement
738 * for services that are not available anymore due to constraints of the U-Boot
741 * Return: EFI_UNSUPPORTED
743 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
745 return EFI_UNSUPPORTED;
749 * efi_update_capsule() - process information from operating system
751 * This function implements the UpdateCapsule() runtime service.
753 * See the Unified Extensible Firmware Interface (UEFI) specification for
756 * @capsule_header_array: pointer to array of virtual pointers
757 * @capsule_count: number of pointers in capsule_header_array
758 * @scatter_gather_list: pointer to arry of physical pointers
759 * Returns: status code
761 efi_status_t __efi_runtime EFIAPI efi_update_capsule(
762 struct efi_capsule_header **capsule_header_array,
763 efi_uintn_t capsule_count,
764 u64 scatter_gather_list)
766 return EFI_UNSUPPORTED;
770 * efi_query_capsule_caps() - check if capsule is supported
772 * This function implements the QueryCapsuleCapabilities() runtime service.
774 * See the Unified Extensible Firmware Interface (UEFI) specification for
777 * @capsule_header_array: pointer to array of virtual pointers
778 * @capsule_count: number of pointers in capsule_header_array
779 * @maximum_capsule_size: maximum capsule size
780 * @reset_type: type of reset needed for capsule update
781 * Returns: status code
783 efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
784 struct efi_capsule_header **capsule_header_array,
785 efi_uintn_t capsule_count,
786 u64 *maximum_capsule_size,
789 return EFI_UNSUPPORTED;
792 struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
794 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
795 .revision = EFI_SPECIFICATION_VERSION,
796 .headersize = sizeof(struct efi_runtime_services),
798 .get_time = &efi_get_time_boottime,
799 .set_time = &efi_set_time_boottime,
800 .get_wakeup_time = (void *)&efi_unimplemented,
801 .set_wakeup_time = (void *)&efi_unimplemented,
802 .set_virtual_address_map = &efi_set_virtual_address_map,
803 .convert_pointer = (void *)&efi_unimplemented,
804 .get_variable = efi_get_variable,
805 .get_next_variable_name = efi_get_next_variable_name,
806 .set_variable = efi_set_variable,
807 .get_next_high_mono_count = (void *)&efi_unimplemented,
808 .reset_system = &efi_reset_system_boottime,
809 .update_capsule = efi_update_capsule,
810 .query_capsule_caps = efi_query_capsule_caps,
811 .query_variable_info = efi_query_variable_info,