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 structs should come from the elf
34 * header for each arch (or a generic header) rather than being repeated here.
36 #if defined(__aarch64__)
37 #define R_RELATIVE R_AARCH64_RELATIVE
38 #define R_MASK 0xffffffffULL
40 #elif defined(__arm__)
41 #define R_RELATIVE R_ARM_RELATIVE
42 #define R_MASK 0xffULL
43 #elif defined(__x86_64__) || defined(__i386__)
44 #define R_RELATIVE R_386_RELATIVE
45 #define R_MASK 0xffULL
46 #elif defined(__riscv)
47 #define R_RELATIVE R_RISCV_RELATIVE
48 #define R_MASK 0xffULL
57 #if (__riscv_xlen == 32)
58 #define R_ABSOLUTE R_RISCV_32
60 #elif (__riscv_xlen == 64)
61 #define R_ABSOLUTE R_RISCV_64
64 #error unknown riscv target
67 #error Need to add relocation awareness
82 * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
83 * payload are running concurrently at the same time. In this mode, we can
84 * handle a good number of runtime callbacks
88 * efi_update_table_header_crc32() - Update crc32 in table header
92 void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
95 table->crc32 = crc32(0, (const unsigned char *)table,
100 * efi_reset_system_boottime() - reset system at boottime
102 * This function implements the ResetSystem() runtime service before
103 * SetVirtualAddressMap() is called.
105 * See the Unified Extensible Firmware Interface (UEFI) specification for
108 * @reset_type: type of reset to perform
109 * @reset_status: status code for the reset
110 * @data_size: size of reset_data
111 * @reset_data: information about the reset
113 static void EFIAPI efi_reset_system_boottime(
114 enum efi_reset_type reset_type,
115 efi_status_t reset_status,
116 unsigned long data_size, void *reset_data)
118 struct efi_event *evt;
120 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
124 list_for_each_entry(evt, &efi_events, link) {
127 &efi_guid_event_group_reset_system)) {
128 efi_signal_event(evt, false);
132 switch (reset_type) {
135 case EFI_RESET_PLATFORM_SPECIFIC:
136 do_reset(NULL, 0, 0, NULL);
138 case EFI_RESET_SHUTDOWN:
139 /* We don't have anything to map this to */
147 * efi_get_time_boottime() - get current time at boottime
149 * This function implements the GetTime runtime service before
150 * SetVirtualAddressMap() is called.
152 * See the Unified Extensible Firmware Interface (UEFI) specification
155 * @time: pointer to structure to receive current time
156 * @capabilities: pointer to structure to receive RTC properties
157 * Returns: status code
159 static efi_status_t EFIAPI efi_get_time_boottime(
160 struct efi_time *time,
161 struct efi_time_cap *capabilities)
164 efi_status_t ret = EFI_SUCCESS;
169 EFI_ENTRY("%p %p", time, capabilities);
172 ret = EFI_INVALID_PARAMETER;
176 r = uclass_get_device(UCLASS_RTC, 0, &dev);
178 r = dm_rtc_get(dev, &tm);
180 ret = EFI_DEVICE_ERROR;
184 memset(time, 0, sizeof(*time));
185 time->year = tm.tm_year;
186 time->month = tm.tm_mon;
187 time->day = tm.tm_mday;
188 time->hour = tm.tm_hour;
189 time->minute = tm.tm_min;
190 time->second = tm.tm_sec;
191 time->daylight = EFI_TIME_ADJUST_DAYLIGHT;
193 time->daylight |= EFI_TIME_IN_DAYLIGHT;
194 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
197 /* Set reasonable dummy values */
198 capabilities->resolution = 1; /* 1 Hz */
199 capabilities->accuracy = 100000000; /* 100 ppm */
200 capabilities->sets_to_zero = false;
203 return EFI_EXIT(ret);
205 EFI_ENTRY("%p %p", time, capabilities);
206 return EFI_EXIT(EFI_DEVICE_ERROR);
212 * efi_reset_system() - reset system
214 * This function implements the ResetSystem() runtime service after
215 * SetVirtualAddressMap() is called. It only executes an endless loop.
216 * Boards may override the helpers below to implement reset functionality.
218 * See the Unified Extensible Firmware Interface (UEFI) specification for
221 * @reset_type: type of reset to perform
222 * @reset_status: status code for the reset
223 * @data_size: size of reset_data
224 * @reset_data: information about the reset
226 void __weak __efi_runtime EFIAPI efi_reset_system(
227 enum efi_reset_type reset_type,
228 efi_status_t reset_status,
229 unsigned long data_size, void *reset_data)
231 /* Nothing we can do */
236 * efi_reset_system_init() - initialize the reset driver
238 * Boards may override this function to initialize the reset driver.
240 efi_status_t __weak efi_reset_system_init(void)
246 * efi_get_time() - get current time
248 * This function implements the GetTime runtime service after
249 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
250 * anymore only an error code is returned.
252 * See the Unified Extensible Firmware Interface (UEFI) specification
255 * @time: pointer to structure to receive current time
256 * @capabilities: pointer to structure to receive RTC properties
257 * Returns: status code
259 efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
260 struct efi_time *time,
261 struct efi_time_cap *capabilities)
263 /* Nothing we can do */
264 return EFI_DEVICE_ERROR;
267 struct efi_runtime_detach_list_struct {
272 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
274 /* do_reset is gone */
275 .ptr = &efi_runtime_services.reset_system,
276 .patchto = efi_reset_system,
278 /* invalidate_*cache_all are gone */
279 .ptr = &efi_runtime_services.set_virtual_address_map,
280 .patchto = &efi_invalid_parameter,
282 /* RTC accessors are gone */
283 .ptr = &efi_runtime_services.get_time,
284 .patchto = &efi_get_time,
286 /* Clean up system table */
287 .ptr = &systab.con_in,
290 /* Clean up system table */
291 .ptr = &systab.con_out,
294 /* Clean up system table */
295 .ptr = &systab.std_err,
298 /* Clean up system table */
299 .ptr = &systab.boottime,
302 .ptr = &efi_runtime_services.get_variable,
303 .patchto = &efi_device_error,
305 .ptr = &efi_runtime_services.get_next_variable_name,
306 .patchto = &efi_device_error,
308 .ptr = &efi_runtime_services.set_variable,
309 .patchto = &efi_device_error,
313 static bool efi_runtime_tobedetached(void *p)
317 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
318 if (efi_runtime_detach_list[i].ptr == p)
324 static void efi_runtime_detach(ulong offset)
327 ulong patchoff = offset - (ulong)gd->relocaddr;
329 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
330 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
331 ulong *p = efi_runtime_detach_list[i].ptr;
332 ulong newaddr = patchto ? (patchto + patchoff) : 0;
334 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
339 efi_update_table_header_crc32(&efi_runtime_services.hdr);
342 /* Relocate EFI runtime to uboot_reloc_base = offset */
343 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
346 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
348 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
349 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
352 debug("%s: Relocating to offset=%lx\n", __func__, offset);
353 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
354 ulong base = CONFIG_SYS_TEXT_BASE;
358 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
360 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, rel->info, *p, rel->offset);
362 switch (rel->info & R_MASK) {
365 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
367 newaddr = *p - lastoff + offset;
372 ulong symidx = rel->info >> SYM_INDEX;
373 extern struct dyn_sym __dyn_sym_start[];
374 newaddr = __dyn_sym_start[symidx].addr + offset;
382 /* Check if the relocation is inside bounds */
383 if (map && ((newaddr < map->virtual_start) ||
384 newaddr > (map->virtual_start +
385 (map->num_pages << EFI_PAGE_SHIFT)))) {
386 if (!efi_runtime_tobedetached(p))
387 printf("U-Boot EFI: Relocation at %p is out of "
388 "range (%lx)\n", p, newaddr);
392 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
394 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
395 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
402 invalidate_icache_all();
406 * efi_set_virtual_address_map() - change from physical to virtual mapping
408 * This function implements the SetVirtualAddressMap() runtime service.
410 * See the Unified Extensible Firmware Interface (UEFI) specification for
413 * @memory_map_size: size of the virtual map
414 * @descriptor_size: size of an entry in the map
415 * @descriptor_version: version of the map entries
416 * @virtmap: virtual address mapping information
417 * Return: status code
419 static efi_status_t EFIAPI efi_set_virtual_address_map(
420 unsigned long memory_map_size,
421 unsigned long descriptor_size,
422 uint32_t descriptor_version,
423 struct efi_mem_desc *virtmap)
425 ulong runtime_start = (ulong)&__efi_runtime_start &
426 ~(ulong)EFI_PAGE_MASK;
427 int n = memory_map_size / descriptor_size;
430 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
431 descriptor_version, virtmap);
433 /* Rebind mmio pointers */
434 for (i = 0; i < n; i++) {
435 struct efi_mem_desc *map = (void*)virtmap +
436 (descriptor_size * i);
437 struct list_head *lhandle;
438 efi_physical_addr_t map_start = map->physical_start;
439 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
440 efi_physical_addr_t map_end = map_start + map_len;
441 u64 off = map->virtual_start - map_start;
443 /* Adjust all mmio pointers in this region */
444 list_for_each(lhandle, &efi_runtime_mmio) {
445 struct efi_runtime_mmio_list *lmmio;
447 lmmio = list_entry(lhandle,
448 struct efi_runtime_mmio_list,
450 if ((map_start <= lmmio->paddr) &&
451 (map_end >= lmmio->paddr)) {
452 uintptr_t new_addr = lmmio->paddr + off;
453 *lmmio->ptr = (void *)new_addr;
456 if ((map_start <= (uintptr_t)systab.tables) &&
457 (map_end >= (uintptr_t)systab.tables)) {
458 char *ptr = (char *)systab.tables;
461 systab.tables = (struct efi_configuration_table *)ptr;
465 /* Move the actual runtime code over */
466 for (i = 0; i < n; i++) {
467 struct efi_mem_desc *map;
469 map = (void*)virtmap + (descriptor_size * i);
470 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
471 ulong new_offset = map->virtual_start -
472 (runtime_start - gd->relocaddr);
474 efi_runtime_relocate(new_offset, map);
475 /* Once we're virtual, we can no longer handle
477 efi_runtime_detach(new_offset);
478 return EFI_EXIT(EFI_SUCCESS);
482 return EFI_EXIT(EFI_INVALID_PARAMETER);
486 * efi_add_runtime_mmio() - add memory-mapped IO region
488 * This function adds a memory-mapped IO region to the memory map to make it
489 * available at runtime.
491 * @mmio_ptr: address of the memory-mapped IO region
492 * @len: size of thememory-mapped IO region
493 * Returns: status code
495 efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
497 struct efi_runtime_mmio_list *newmmio;
498 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
499 uint64_t addr = *(uintptr_t *)mmio_ptr;
502 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
504 return EFI_OUT_OF_RESOURCES;
506 newmmio = calloc(1, sizeof(*newmmio));
508 return EFI_OUT_OF_RESOURCES;
509 newmmio->ptr = mmio_ptr;
510 newmmio->paddr = *(uintptr_t *)mmio_ptr;
512 list_add_tail(&newmmio->link, &efi_runtime_mmio);
518 * In the second stage, U-Boot has disappeared. To isolate our runtime code
519 * that at this point still exists from the rest, we put it into a special
524 * This means that we can not rely on any code outside of this file in any
525 * function or variable below this line.
527 * Please keep everything fully self-contained and annotated with
528 * __efi_runtime and __efi_runtime_data markers.
532 * Relocate the EFI runtime stub to a different place. We need to call this
533 * the first time we expose the runtime interface to a user and on set virtual
538 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
540 * This function is used after SetVirtualAddressMap() is called as replacement
541 * for services that are not available anymore due to constraints of the U-Boot
544 * Return: EFI_UNSUPPORTED
546 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
548 return EFI_UNSUPPORTED;
552 * efi_device_error() - replacement function, returns EFI_DEVICE_ERROR
554 * This function is used after SetVirtualAddressMap() is called as replacement
555 * for services that are not available anymore due to constraints of the U-Boot
558 * Return: EFI_DEVICE_ERROR
560 static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
562 return EFI_DEVICE_ERROR;
566 * efi_invalid_parameter() - replacement function, returns EFI_INVALID_PARAMETER
568 * This function is used after SetVirtualAddressMap() is called as replacement
569 * for services that are not available anymore due to constraints of the U-Boot
572 * Return: EFI_INVALID_PARAMETER
574 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
576 return EFI_INVALID_PARAMETER;
580 * efi_update_capsule() - process information from operating system
582 * This function implements the UpdateCapsule() runtime service.
584 * See the Unified Extensible Firmware Interface (UEFI) specification for
587 * @capsule_header_array: pointer to array of virtual pointers
588 * @capsule_count: number of pointers in capsule_header_array
589 * @scatter_gather_list: pointer to arry of physical pointers
590 * Returns: status code
592 efi_status_t __efi_runtime EFIAPI efi_update_capsule(
593 struct efi_capsule_header **capsule_header_array,
594 efi_uintn_t capsule_count,
595 u64 scatter_gather_list)
597 return EFI_UNSUPPORTED;
601 * efi_query_capsule_caps() - check if capsule is supported
603 * This function implements the QueryCapsuleCapabilities() runtime service.
605 * See the Unified Extensible Firmware Interface (UEFI) specification for
608 * @capsule_header_array: pointer to array of virtual pointers
609 * @capsule_count: number of pointers in capsule_header_array
610 * @capsule_size: maximum capsule size
611 * @reset_type: type of reset needed for capsule update
612 * Returns: status code
614 efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
615 struct efi_capsule_header **capsule_header_array,
616 efi_uintn_t capsule_count,
617 u64 maximum_capsule_size,
620 return EFI_UNSUPPORTED;
624 * efi_query_variable_info() - get information about EFI variables
626 * This function implements the QueryVariableInfo() runtime service.
628 * See the Unified Extensible Firmware Interface (UEFI) specification for
631 * @attributes: bitmask to select variables to be
633 * @maximum_variable_storage_size: maximum size of storage area for the
634 * selected variable types
635 * @remaining_variable_storage_size: remaining size of storage are for the
636 * selected variable types
637 * @maximum_variable_size: maximum size of a variable of the
639 * Returns: status code
641 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
643 u64 *maximum_variable_storage_size,
644 u64 *remaining_variable_storage_size,
645 u64 *maximum_variable_size)
647 return EFI_UNSUPPORTED;
650 struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
652 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
653 .revision = EFI_SPECIFICATION_VERSION,
654 .headersize = sizeof(struct efi_runtime_services),
656 .get_time = &efi_get_time_boottime,
657 .set_time = (void *)&efi_device_error,
658 .get_wakeup_time = (void *)&efi_unimplemented,
659 .set_wakeup_time = (void *)&efi_unimplemented,
660 .set_virtual_address_map = &efi_set_virtual_address_map,
661 .convert_pointer = (void *)&efi_invalid_parameter,
662 .get_variable = efi_get_variable,
663 .get_next_variable_name = efi_get_next_variable_name,
664 .set_variable = efi_set_variable,
665 .get_next_high_mono_count = (void *)&efi_device_error,
666 .reset_system = &efi_reset_system_boottime,
667 .update_capsule = efi_update_capsule,
668 .query_capsule_caps = efi_query_capsule_caps,
669 .query_variable_info = efi_query_variable_info,