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
93 * efi_update_table_header_crc32() - Update crc32 in table header
97 void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
100 table->crc32 = crc32(0, (const unsigned char *)table,
105 * efi_reset_system_boottime() - reset system at boot time
107 * This function implements the ResetSystem() runtime service before
108 * SetVirtualAddressMap() is called.
110 * See the Unified Extensible Firmware Interface (UEFI) specification for
113 * @reset_type: type of reset to perform
114 * @reset_status: status code for the reset
115 * @data_size: size of reset_data
116 * @reset_data: information about the reset
118 static void EFIAPI efi_reset_system_boottime(
119 enum efi_reset_type reset_type,
120 efi_status_t reset_status,
121 unsigned long data_size, void *reset_data)
123 struct efi_event *evt;
125 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
129 list_for_each_entry(evt, &efi_events, link) {
132 &efi_guid_event_group_reset_system)) {
133 efi_signal_event(evt, false);
137 switch (reset_type) {
140 case EFI_RESET_PLATFORM_SPECIFIC:
141 do_reset(NULL, 0, 0, NULL);
143 case EFI_RESET_SHUTDOWN:
144 #ifdef CONFIG_CMD_POWEROFF
145 do_poweroff(NULL, 0, 0, NULL);
154 * efi_get_time_boottime() - get current time at boot time
156 * This function implements the GetTime runtime service before
157 * SetVirtualAddressMap() is called.
159 * See the Unified Extensible Firmware Interface (UEFI) specification
162 * @time: pointer to structure to receive current time
163 * @capabilities: pointer to structure to receive RTC properties
164 * Returns: status code
166 static efi_status_t EFIAPI efi_get_time_boottime(
167 struct efi_time *time,
168 struct efi_time_cap *capabilities)
171 efi_status_t ret = EFI_SUCCESS;
175 EFI_ENTRY("%p %p", time, capabilities);
178 ret = EFI_INVALID_PARAMETER;
181 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
182 dm_rtc_get(dev, &tm)) {
183 ret = EFI_UNSUPPORTED;
186 if (dm_rtc_get(dev, &tm)) {
187 ret = EFI_DEVICE_ERROR;
191 memset(time, 0, sizeof(*time));
192 time->year = tm.tm_year;
193 time->month = tm.tm_mon;
194 time->day = tm.tm_mday;
195 time->hour = tm.tm_hour;
196 time->minute = tm.tm_min;
197 time->second = tm.tm_sec;
198 time->daylight = EFI_TIME_ADJUST_DAYLIGHT;
200 time->daylight |= EFI_TIME_IN_DAYLIGHT;
201 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
204 /* Set reasonable dummy values */
205 capabilities->resolution = 1; /* 1 Hz */
206 capabilities->accuracy = 100000000; /* 100 ppm */
207 capabilities->sets_to_zero = false;
210 return EFI_EXIT(ret);
212 EFI_ENTRY("%p %p", time, capabilities);
213 return EFI_EXIT(EFI_UNSUPPORTED);
218 * efi_set_time_boottime() - set current time
220 * This function implements the SetTime() runtime service before
221 * SetVirtualAddressMap() is called.
223 * See the Unified Extensible Firmware Interface (UEFI) specification
226 * @time: pointer to structure to with current time
227 * Returns: status code
229 static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
232 efi_status_t ret = EFI_SUCCESS;
236 EFI_ENTRY("%p", time);
239 ret = EFI_INVALID_PARAMETER;
243 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
244 ret = EFI_UNSUPPORTED;
248 memset(&tm, 0, sizeof(tm));
249 tm.tm_year = time->year;
250 tm.tm_mon = time->month;
251 tm.tm_mday = time->day;
252 tm.tm_hour = time->hour;
253 tm.tm_min = time->minute;
254 tm.tm_sec = time->second;
255 tm.tm_isdst = time->daylight == EFI_TIME_IN_DAYLIGHT;
256 /* Calculate day of week */
257 rtc_calc_weekday(&tm);
259 if (dm_rtc_set(dev, &tm))
260 ret = EFI_DEVICE_ERROR;
262 return EFI_EXIT(ret);
264 EFI_ENTRY("%p", time);
265 return EFI_EXIT(EFI_UNSUPPORTED);
269 * efi_reset_system() - reset system
271 * This function implements the ResetSystem() runtime service after
272 * SetVirtualAddressMap() is called. It only executes an endless loop.
273 * Boards may override the helpers below to implement reset functionality.
275 * See the Unified Extensible Firmware Interface (UEFI) specification for
278 * @reset_type: type of reset to perform
279 * @reset_status: status code for the reset
280 * @data_size: size of reset_data
281 * @reset_data: information about the reset
283 void __weak __efi_runtime EFIAPI efi_reset_system(
284 enum efi_reset_type reset_type,
285 efi_status_t reset_status,
286 unsigned long data_size, void *reset_data)
288 /* Nothing we can do */
293 * efi_reset_system_init() - initialize the reset driver
295 * Boards may override this function to initialize the reset driver.
297 efi_status_t __weak efi_reset_system_init(void)
303 * efi_get_time() - get current time
305 * This function implements the GetTime runtime service after
306 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
307 * anymore only an error code is returned.
309 * See the Unified Extensible Firmware Interface (UEFI) specification
312 * @time: pointer to structure to receive current time
313 * @capabilities: pointer to structure to receive RTC properties
314 * Returns: status code
316 efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
317 struct efi_time *time,
318 struct efi_time_cap *capabilities)
320 /* Nothing we can do */
321 return EFI_DEVICE_ERROR;
325 * efi_set_time() - set current time
327 * This function implements the SetTime runtime service after
328 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
329 * anymore only an error code is returned.
331 * See the Unified Extensible Firmware Interface (UEFI) specification
334 * @time: pointer to structure to with current time
335 * Returns: status code
337 efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
339 return EFI_UNSUPPORTED;
342 struct efi_runtime_detach_list_struct {
347 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
349 /* do_reset is gone */
350 .ptr = &efi_runtime_services.reset_system,
351 .patchto = efi_reset_system,
353 /* invalidate_*cache_all are gone */
354 .ptr = &efi_runtime_services.set_virtual_address_map,
355 .patchto = &efi_unimplemented,
357 /* RTC accessors are gone */
358 .ptr = &efi_runtime_services.get_time,
359 .patchto = &efi_get_time,
361 .ptr = &efi_runtime_services.set_time,
362 .patchto = &efi_set_time,
364 /* Clean up system table */
365 .ptr = &systab.con_in,
368 /* Clean up system table */
369 .ptr = &systab.con_out,
372 /* Clean up system table */
373 .ptr = &systab.std_err,
376 /* Clean up system table */
377 .ptr = &systab.boottime,
380 .ptr = &efi_runtime_services.get_variable,
381 .patchto = &efi_device_error,
383 .ptr = &efi_runtime_services.get_next_variable_name,
384 .patchto = &efi_device_error,
386 .ptr = &efi_runtime_services.set_variable,
387 .patchto = &efi_device_error,
391 static bool efi_runtime_tobedetached(void *p)
395 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
396 if (efi_runtime_detach_list[i].ptr == p)
402 static void efi_runtime_detach(ulong offset)
405 ulong patchoff = offset - (ulong)gd->relocaddr;
407 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
408 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
409 ulong *p = efi_runtime_detach_list[i].ptr;
410 ulong newaddr = patchto ? (patchto + patchoff) : 0;
412 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
417 efi_update_table_header_crc32(&efi_runtime_services.hdr);
420 /* Relocate EFI runtime to uboot_reloc_base = offset */
421 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
424 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
426 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
427 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
430 debug("%s: Relocating to offset=%lx\n", __func__, offset);
431 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
432 ulong base = CONFIG_SYS_TEXT_BASE;
436 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
438 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
439 rel->info, *p, rel->offset);
441 switch (rel->info & R_MASK) {
444 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
446 newaddr = *p - lastoff + offset;
451 ulong symidx = rel->info >> SYM_INDEX;
452 extern struct dyn_sym __dyn_sym_start[];
453 newaddr = __dyn_sym_start[symidx].addr + offset;
455 newaddr -= CONFIG_SYS_TEXT_BASE;
461 if (!efi_runtime_tobedetached(p))
462 printf("%s: Unknown relocation type %llx\n",
463 __func__, rel->info & R_MASK);
467 /* Check if the relocation is inside bounds */
468 if (map && ((newaddr < map->virtual_start) ||
469 newaddr > (map->virtual_start +
470 (map->num_pages << EFI_PAGE_SHIFT)))) {
471 if (!efi_runtime_tobedetached(p))
472 printf("%s: Relocation at %p is out of "
473 "range (%lx)\n", __func__, p, newaddr);
477 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
479 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
480 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
487 invalidate_icache_all();
491 * efi_set_virtual_address_map() - change from physical to virtual mapping
493 * This function implements the SetVirtualAddressMap() runtime service.
495 * See the Unified Extensible Firmware Interface (UEFI) specification for
498 * @memory_map_size: size of the virtual map
499 * @descriptor_size: size of an entry in the map
500 * @descriptor_version: version of the map entries
501 * @virtmap: virtual address mapping information
502 * Return: status code
504 static efi_status_t EFIAPI efi_set_virtual_address_map(
505 unsigned long memory_map_size,
506 unsigned long descriptor_size,
507 uint32_t descriptor_version,
508 struct efi_mem_desc *virtmap)
510 int n = memory_map_size / descriptor_size;
512 int rt_code_sections = 0;
514 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
515 descriptor_version, virtmap);
519 * Further down we are cheating. While really we should implement
520 * SetVirtualAddressMap() events and ConvertPointer() to allow
521 * dynamically loaded drivers to expose runtime services, we don't
524 * So let's ensure we see exactly one single runtime section, as
525 * that is the built-in one. If we see more (or less), someone must
526 * have tried adding or removing to that which we don't support yet.
527 * In that case, let's better fail rather than expose broken runtime
530 for (i = 0; i < n; i++) {
531 struct efi_mem_desc *map = (void*)virtmap +
532 (descriptor_size * i);
534 if (map->type == EFI_RUNTIME_SERVICES_CODE)
538 if (rt_code_sections != 1) {
540 * We expose exactly one single runtime code section, so
541 * something is definitely going wrong.
543 return EFI_EXIT(EFI_INVALID_PARAMETER);
546 /* Rebind mmio pointers */
547 for (i = 0; i < n; i++) {
548 struct efi_mem_desc *map = (void*)virtmap +
549 (descriptor_size * i);
550 struct list_head *lhandle;
551 efi_physical_addr_t map_start = map->physical_start;
552 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
553 efi_physical_addr_t map_end = map_start + map_len;
554 u64 off = map->virtual_start - map_start;
556 /* Adjust all mmio pointers in this region */
557 list_for_each(lhandle, &efi_runtime_mmio) {
558 struct efi_runtime_mmio_list *lmmio;
560 lmmio = list_entry(lhandle,
561 struct efi_runtime_mmio_list,
563 if ((map_start <= lmmio->paddr) &&
564 (map_end >= lmmio->paddr)) {
565 uintptr_t new_addr = lmmio->paddr + off;
566 *lmmio->ptr = (void *)new_addr;
569 if ((map_start <= (uintptr_t)systab.tables) &&
570 (map_end >= (uintptr_t)systab.tables)) {
571 char *ptr = (char *)systab.tables;
574 systab.tables = (struct efi_configuration_table *)ptr;
578 /* Move the actual runtime code over */
579 for (i = 0; i < n; i++) {
580 struct efi_mem_desc *map;
582 map = (void*)virtmap + (descriptor_size * i);
583 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
584 ulong new_offset = map->virtual_start -
585 map->physical_start + gd->relocaddr;
587 efi_runtime_relocate(new_offset, map);
588 /* Once we're virtual, we can no longer handle
590 efi_runtime_detach(new_offset);
591 return EFI_EXIT(EFI_SUCCESS);
595 return EFI_EXIT(EFI_INVALID_PARAMETER);
599 * efi_add_runtime_mmio() - add memory-mapped IO region
601 * This function adds a memory-mapped IO region to the memory map to make it
602 * available at runtime.
604 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
606 * @len: size of the memory-mapped IO region
607 * Returns: status code
609 efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
611 struct efi_runtime_mmio_list *newmmio;
612 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
613 uint64_t addr = *(uintptr_t *)mmio_ptr;
616 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
618 return EFI_OUT_OF_RESOURCES;
620 newmmio = calloc(1, sizeof(*newmmio));
622 return EFI_OUT_OF_RESOURCES;
623 newmmio->ptr = mmio_ptr;
624 newmmio->paddr = *(uintptr_t *)mmio_ptr;
626 list_add_tail(&newmmio->link, &efi_runtime_mmio);
632 * In the second stage, U-Boot has disappeared. To isolate our runtime code
633 * that at this point still exists from the rest, we put it into a special
638 * This means that we can not rely on any code outside of this file in any
639 * function or variable below this line.
641 * Please keep everything fully self-contained and annotated with
642 * __efi_runtime and __efi_runtime_data markers.
646 * Relocate the EFI runtime stub to a different place. We need to call this
647 * the first time we expose the runtime interface to a user and on set virtual
652 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
654 * This function is used after SetVirtualAddressMap() is called as replacement
655 * for services that are not available anymore due to constraints of the U-Boot
658 * Return: EFI_UNSUPPORTED
660 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
662 return EFI_UNSUPPORTED;
666 * efi_device_error() - replacement function, returns EFI_DEVICE_ERROR
668 * This function is used after SetVirtualAddressMap() is called as replacement
669 * for services that are not available anymore due to constraints of the U-Boot
672 * Return: EFI_DEVICE_ERROR
674 static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
676 return EFI_DEVICE_ERROR;
680 * efi_invalid_parameter() - replacement function, returns EFI_INVALID_PARAMETER
682 * This function is used after SetVirtualAddressMap() is called as replacement
683 * for services that are not available anymore due to constraints of the U-Boot
686 * Return: EFI_INVALID_PARAMETER
688 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
690 return EFI_INVALID_PARAMETER;
694 * efi_update_capsule() - process information from operating system
696 * This function implements the UpdateCapsule() runtime service.
698 * See the Unified Extensible Firmware Interface (UEFI) specification for
701 * @capsule_header_array: pointer to array of virtual pointers
702 * @capsule_count: number of pointers in capsule_header_array
703 * @scatter_gather_list: pointer to arry of physical pointers
704 * Returns: status code
706 efi_status_t __efi_runtime EFIAPI efi_update_capsule(
707 struct efi_capsule_header **capsule_header_array,
708 efi_uintn_t capsule_count,
709 u64 scatter_gather_list)
711 return EFI_UNSUPPORTED;
715 * efi_query_capsule_caps() - check if capsule is supported
717 * This function implements the QueryCapsuleCapabilities() runtime service.
719 * See the Unified Extensible Firmware Interface (UEFI) specification for
722 * @capsule_header_array: pointer to array of virtual pointers
723 * @capsule_count: number of pointers in capsule_header_array
724 * @maximum_capsule_size: maximum capsule size
725 * @reset_type: type of reset needed for capsule update
726 * Returns: status code
728 efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
729 struct efi_capsule_header **capsule_header_array,
730 efi_uintn_t capsule_count,
731 u64 *maximum_capsule_size,
734 return EFI_UNSUPPORTED;
738 * efi_query_variable_info() - get information about EFI variables
740 * This function implements the QueryVariableInfo() runtime service.
742 * See the Unified Extensible Firmware Interface (UEFI) specification for
745 * @attributes: bitmask to select variables to be
747 * @maximum_variable_storage_size: maximum size of storage area for the
748 * selected variable types
749 * @remaining_variable_storage_size: remaining size of storage are for the
750 * selected variable types
751 * @maximum_variable_size: maximum size of a variable of the
753 * Returns: status code
755 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
757 u64 *maximum_variable_storage_size,
758 u64 *remaining_variable_storage_size,
759 u64 *maximum_variable_size)
761 return EFI_UNSUPPORTED;
764 struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
766 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
767 .revision = EFI_SPECIFICATION_VERSION,
768 .headersize = sizeof(struct efi_runtime_services),
770 .get_time = &efi_get_time_boottime,
771 .set_time = &efi_set_time_boottime,
772 .get_wakeup_time = (void *)&efi_unimplemented,
773 .set_wakeup_time = (void *)&efi_unimplemented,
774 .set_virtual_address_map = &efi_set_virtual_address_map,
775 .convert_pointer = (void *)&efi_invalid_parameter,
776 .get_variable = efi_get_variable,
777 .get_next_variable_name = efi_get_next_variable_name,
778 .set_variable = efi_set_variable,
779 .get_next_high_mono_count = (void *)&efi_device_error,
780 .reset_system = &efi_reset_system_boottime,
781 .update_capsule = efi_update_capsule,
782 .query_capsule_caps = efi_query_capsule_caps,
783 .query_variable_info = efi_query_variable_info,