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 /* We don't have anything to map this to */
152 * efi_get_time_boottime() - get current time at boot time
154 * This function implements the GetTime runtime service before
155 * SetVirtualAddressMap() is called.
157 * See the Unified Extensible Firmware Interface (UEFI) specification
160 * @time: pointer to structure to receive current time
161 * @capabilities: pointer to structure to receive RTC properties
162 * Returns: status code
164 static efi_status_t EFIAPI efi_get_time_boottime(
165 struct efi_time *time,
166 struct efi_time_cap *capabilities)
169 efi_status_t ret = EFI_SUCCESS;
174 EFI_ENTRY("%p %p", time, capabilities);
177 ret = EFI_INVALID_PARAMETER;
181 r = uclass_get_device(UCLASS_RTC, 0, &dev);
183 r = dm_rtc_get(dev, &tm);
185 ret = EFI_DEVICE_ERROR;
189 memset(time, 0, sizeof(*time));
190 time->year = tm.tm_year;
191 time->month = tm.tm_mon;
192 time->day = tm.tm_mday;
193 time->hour = tm.tm_hour;
194 time->minute = tm.tm_min;
195 time->second = tm.tm_sec;
196 time->daylight = EFI_TIME_ADJUST_DAYLIGHT;
198 time->daylight |= EFI_TIME_IN_DAYLIGHT;
199 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
202 /* Set reasonable dummy values */
203 capabilities->resolution = 1; /* 1 Hz */
204 capabilities->accuracy = 100000000; /* 100 ppm */
205 capabilities->sets_to_zero = false;
208 return EFI_EXIT(ret);
210 EFI_ENTRY("%p %p", time, capabilities);
211 return EFI_EXIT(EFI_DEVICE_ERROR);
217 * efi_reset_system() - reset system
219 * This function implements the ResetSystem() runtime service after
220 * SetVirtualAddressMap() is called. It only executes an endless loop.
221 * Boards may override the helpers below to implement reset functionality.
223 * See the Unified Extensible Firmware Interface (UEFI) specification for
226 * @reset_type: type of reset to perform
227 * @reset_status: status code for the reset
228 * @data_size: size of reset_data
229 * @reset_data: information about the reset
231 void __weak __efi_runtime EFIAPI efi_reset_system(
232 enum efi_reset_type reset_type,
233 efi_status_t reset_status,
234 unsigned long data_size, void *reset_data)
236 /* Nothing we can do */
241 * efi_reset_system_init() - initialize the reset driver
243 * Boards may override this function to initialize the reset driver.
245 efi_status_t __weak efi_reset_system_init(void)
251 * efi_get_time() - get current time
253 * This function implements the GetTime runtime service after
254 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
255 * anymore only an error code is returned.
257 * See the Unified Extensible Firmware Interface (UEFI) specification
260 * @time: pointer to structure to receive current time
261 * @capabilities: pointer to structure to receive RTC properties
262 * Returns: status code
264 efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
265 struct efi_time *time,
266 struct efi_time_cap *capabilities)
268 /* Nothing we can do */
269 return EFI_DEVICE_ERROR;
272 struct efi_runtime_detach_list_struct {
277 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
279 /* do_reset is gone */
280 .ptr = &efi_runtime_services.reset_system,
281 .patchto = efi_reset_system,
283 /* invalidate_*cache_all are gone */
284 .ptr = &efi_runtime_services.set_virtual_address_map,
285 .patchto = &efi_invalid_parameter,
287 /* RTC accessors are gone */
288 .ptr = &efi_runtime_services.get_time,
289 .patchto = &efi_get_time,
291 /* Clean up system table */
292 .ptr = &systab.con_in,
295 /* Clean up system table */
296 .ptr = &systab.con_out,
299 /* Clean up system table */
300 .ptr = &systab.std_err,
303 /* Clean up system table */
304 .ptr = &systab.boottime,
307 .ptr = &efi_runtime_services.get_variable,
308 .patchto = &efi_device_error,
310 .ptr = &efi_runtime_services.get_next_variable_name,
311 .patchto = &efi_device_error,
313 .ptr = &efi_runtime_services.set_variable,
314 .patchto = &efi_device_error,
318 static bool efi_runtime_tobedetached(void *p)
322 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
323 if (efi_runtime_detach_list[i].ptr == p)
329 static void efi_runtime_detach(ulong offset)
332 ulong patchoff = offset - (ulong)gd->relocaddr;
334 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
335 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
336 ulong *p = efi_runtime_detach_list[i].ptr;
337 ulong newaddr = patchto ? (patchto + patchoff) : 0;
339 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
344 efi_update_table_header_crc32(&efi_runtime_services.hdr);
347 /* Relocate EFI runtime to uboot_reloc_base = offset */
348 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
351 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
353 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
354 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
357 debug("%s: Relocating to offset=%lx\n", __func__, offset);
358 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
359 ulong base = CONFIG_SYS_TEXT_BASE;
363 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
365 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
366 rel->info, *p, rel->offset);
368 switch (rel->info & R_MASK) {
371 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
373 newaddr = *p - lastoff + offset;
378 ulong symidx = rel->info >> SYM_INDEX;
379 extern struct dyn_sym __dyn_sym_start[];
380 newaddr = __dyn_sym_start[symidx].addr + offset;
385 if (!efi_runtime_tobedetached(p))
386 printf("%s: Unknown relocation type %llx\n",
387 __func__, rel->info & R_MASK);
391 /* Check if the relocation is inside bounds */
392 if (map && ((newaddr < map->virtual_start) ||
393 newaddr > (map->virtual_start +
394 (map->num_pages << EFI_PAGE_SHIFT)))) {
395 if (!efi_runtime_tobedetached(p))
396 printf("%s: Relocation at %p is out of "
397 "range (%lx)\n", __func__, p, newaddr);
401 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
403 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
404 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
411 invalidate_icache_all();
415 * efi_set_virtual_address_map() - change from physical to virtual mapping
417 * This function implements the SetVirtualAddressMap() runtime service.
419 * See the Unified Extensible Firmware Interface (UEFI) specification for
422 * @memory_map_size: size of the virtual map
423 * @descriptor_size: size of an entry in the map
424 * @descriptor_version: version of the map entries
425 * @virtmap: virtual address mapping information
426 * Return: status code
428 static efi_status_t EFIAPI efi_set_virtual_address_map(
429 unsigned long memory_map_size,
430 unsigned long descriptor_size,
431 uint32_t descriptor_version,
432 struct efi_mem_desc *virtmap)
434 ulong runtime_start = (ulong)&__efi_runtime_start &
435 ~(ulong)EFI_PAGE_MASK;
436 int n = memory_map_size / descriptor_size;
439 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
440 descriptor_version, virtmap);
442 /* Rebind mmio pointers */
443 for (i = 0; i < n; i++) {
444 struct efi_mem_desc *map = (void*)virtmap +
445 (descriptor_size * i);
446 struct list_head *lhandle;
447 efi_physical_addr_t map_start = map->physical_start;
448 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
449 efi_physical_addr_t map_end = map_start + map_len;
450 u64 off = map->virtual_start - map_start;
452 /* Adjust all mmio pointers in this region */
453 list_for_each(lhandle, &efi_runtime_mmio) {
454 struct efi_runtime_mmio_list *lmmio;
456 lmmio = list_entry(lhandle,
457 struct efi_runtime_mmio_list,
459 if ((map_start <= lmmio->paddr) &&
460 (map_end >= lmmio->paddr)) {
461 uintptr_t new_addr = lmmio->paddr + off;
462 *lmmio->ptr = (void *)new_addr;
465 if ((map_start <= (uintptr_t)systab.tables) &&
466 (map_end >= (uintptr_t)systab.tables)) {
467 char *ptr = (char *)systab.tables;
470 systab.tables = (struct efi_configuration_table *)ptr;
474 /* Move the actual runtime code over */
475 for (i = 0; i < n; i++) {
476 struct efi_mem_desc *map;
478 map = (void*)virtmap + (descriptor_size * i);
479 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
480 ulong new_offset = map->virtual_start -
481 (runtime_start - gd->relocaddr);
483 efi_runtime_relocate(new_offset, map);
484 /* Once we're virtual, we can no longer handle
486 efi_runtime_detach(new_offset);
487 return EFI_EXIT(EFI_SUCCESS);
491 return EFI_EXIT(EFI_INVALID_PARAMETER);
495 * efi_add_runtime_mmio() - add memory-mapped IO region
497 * This function adds a memory-mapped IO region to the memory map to make it
498 * available at runtime.
500 * @mmio_ptr: address of the memory-mapped IO region
501 * @len: size of the memory-mapped IO region
502 * Returns: status code
504 efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
506 struct efi_runtime_mmio_list *newmmio;
507 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
508 uint64_t addr = *(uintptr_t *)mmio_ptr;
511 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
513 return EFI_OUT_OF_RESOURCES;
515 newmmio = calloc(1, sizeof(*newmmio));
517 return EFI_OUT_OF_RESOURCES;
518 newmmio->ptr = mmio_ptr;
519 newmmio->paddr = *(uintptr_t *)mmio_ptr;
521 list_add_tail(&newmmio->link, &efi_runtime_mmio);
527 * In the second stage, U-Boot has disappeared. To isolate our runtime code
528 * that at this point still exists from the rest, we put it into a special
533 * This means that we can not rely on any code outside of this file in any
534 * function or variable below this line.
536 * Please keep everything fully self-contained and annotated with
537 * __efi_runtime and __efi_runtime_data markers.
541 * Relocate the EFI runtime stub to a different place. We need to call this
542 * the first time we expose the runtime interface to a user and on set virtual
547 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
549 * This function is used after SetVirtualAddressMap() is called as replacement
550 * for services that are not available anymore due to constraints of the U-Boot
553 * Return: EFI_UNSUPPORTED
555 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
557 return EFI_UNSUPPORTED;
561 * efi_device_error() - replacement function, returns EFI_DEVICE_ERROR
563 * This function is used after SetVirtualAddressMap() is called as replacement
564 * for services that are not available anymore due to constraints of the U-Boot
567 * Return: EFI_DEVICE_ERROR
569 static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
571 return EFI_DEVICE_ERROR;
575 * efi_invalid_parameter() - replacement function, returns EFI_INVALID_PARAMETER
577 * This function is used after SetVirtualAddressMap() is called as replacement
578 * for services that are not available anymore due to constraints of the U-Boot
581 * Return: EFI_INVALID_PARAMETER
583 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
585 return EFI_INVALID_PARAMETER;
589 * efi_update_capsule() - process information from operating system
591 * This function implements the UpdateCapsule() runtime service.
593 * See the Unified Extensible Firmware Interface (UEFI) specification for
596 * @capsule_header_array: pointer to array of virtual pointers
597 * @capsule_count: number of pointers in capsule_header_array
598 * @scatter_gather_list: pointer to arry of physical pointers
599 * Returns: status code
601 efi_status_t __efi_runtime EFIAPI efi_update_capsule(
602 struct efi_capsule_header **capsule_header_array,
603 efi_uintn_t capsule_count,
604 u64 scatter_gather_list)
606 return EFI_UNSUPPORTED;
610 * efi_query_capsule_caps() - check if capsule is supported
612 * This function implements the QueryCapsuleCapabilities() runtime service.
614 * See the Unified Extensible Firmware Interface (UEFI) specification for
617 * @capsule_header_array: pointer to array of virtual pointers
618 * @capsule_count: number of pointers in capsule_header_array
619 * @maximum_capsule_size: maximum capsule size
620 * @reset_type: type of reset needed for capsule update
621 * Returns: status code
623 efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
624 struct efi_capsule_header **capsule_header_array,
625 efi_uintn_t capsule_count,
626 u64 maximum_capsule_size,
629 return EFI_UNSUPPORTED;
633 * efi_query_variable_info() - get information about EFI variables
635 * This function implements the QueryVariableInfo() runtime service.
637 * See the Unified Extensible Firmware Interface (UEFI) specification for
640 * @attributes: bitmask to select variables to be
642 * @maximum_variable_storage_size: maximum size of storage area for the
643 * selected variable types
644 * @remaining_variable_storage_size: remaining size of storage are for the
645 * selected variable types
646 * @maximum_variable_size: maximum size of a variable of the
648 * Returns: status code
650 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
652 u64 *maximum_variable_storage_size,
653 u64 *remaining_variable_storage_size,
654 u64 *maximum_variable_size)
656 return EFI_UNSUPPORTED;
659 struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
661 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
662 .revision = EFI_SPECIFICATION_VERSION,
663 .headersize = sizeof(struct efi_runtime_services),
665 .get_time = &efi_get_time_boottime,
666 .set_time = (void *)&efi_device_error,
667 .get_wakeup_time = (void *)&efi_unimplemented,
668 .set_wakeup_time = (void *)&efi_unimplemented,
669 .set_virtual_address_map = &efi_set_virtual_address_map,
670 .convert_pointer = (void *)&efi_invalid_parameter,
671 .get_variable = efi_get_variable,
672 .get_next_variable_name = efi_get_next_variable_name,
673 .set_variable = efi_set_variable,
674 .get_next_high_mono_count = (void *)&efi_device_error,
675 .reset_system = &efi_reset_system_boottime,
676 .update_capsule = efi_update_capsule,
677 .query_capsule_caps = efi_query_capsule_caps,
678 .query_variable_info = efi_query_variable_info,