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
87 static void EFIAPI efi_reset_system_boottime(
88 enum efi_reset_type reset_type,
89 efi_status_t reset_status,
90 unsigned long data_size, void *reset_data)
92 struct efi_event *evt;
94 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
98 list_for_each_entry(evt, &efi_events, link) {
101 &efi_guid_event_group_reset_system)) {
102 efi_signal_event(evt, false);
106 switch (reset_type) {
109 case EFI_RESET_PLATFORM_SPECIFIC:
110 do_reset(NULL, 0, 0, NULL);
112 case EFI_RESET_SHUTDOWN:
113 /* We don't have anything to map this to */
121 * efi_get_time_boottime - get current time
123 * This function implements the GetTime runtime service.
124 * See the Unified Extensible Firmware Interface (UEFI) specification
127 * @time: pointer to structure to receive current time
128 * @capabilities: pointer to structure to receive RTC properties
129 * Return Value: status code
131 static efi_status_t EFIAPI efi_get_time_boottime(
132 struct efi_time *time,
133 struct efi_time_cap *capabilities)
136 efi_status_t ret = EFI_SUCCESS;
141 EFI_ENTRY("%p %p", time, capabilities);
144 ret = EFI_INVALID_PARAMETER;
148 r = uclass_get_device(UCLASS_RTC, 0, &dev);
150 r = dm_rtc_get(dev, &tm);
152 ret = EFI_DEVICE_ERROR;
156 memset(time, 0, sizeof(*time));
157 time->year = tm.tm_year;
158 time->month = tm.tm_mon;
159 time->day = tm.tm_mday;
160 time->hour = tm.tm_hour;
161 time->minute = tm.tm_min;
162 time->second = tm.tm_sec;
163 time->daylight = EFI_TIME_ADJUST_DAYLIGHT;
165 time->daylight |= EFI_TIME_IN_DAYLIGHT;
166 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
169 /* Set reasonable dummy values */
170 capabilities->resolution = 1; /* 1 Hz */
171 capabilities->accuracy = 100000000; /* 100 ppm */
172 capabilities->sets_to_zero = false;
175 return EFI_EXIT(ret);
177 EFI_ENTRY("%p %p", time, capabilities);
178 return EFI_EXIT(EFI_DEVICE_ERROR);
182 /* Boards may override the helpers below to implement RTS functionality */
184 void __weak __efi_runtime EFIAPI efi_reset_system(
185 enum efi_reset_type reset_type,
186 efi_status_t reset_status,
187 unsigned long data_size, void *reset_data)
189 /* Nothing we can do */
193 efi_status_t __weak efi_reset_system_init(void)
198 efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
199 struct efi_time *time,
200 struct efi_time_cap *capabilities)
202 /* Nothing we can do */
203 return EFI_DEVICE_ERROR;
206 struct efi_runtime_detach_list_struct {
211 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
213 /* do_reset is gone */
214 .ptr = &efi_runtime_services.reset_system,
215 .patchto = efi_reset_system,
217 /* invalidate_*cache_all are gone */
218 .ptr = &efi_runtime_services.set_virtual_address_map,
219 .patchto = &efi_invalid_parameter,
221 /* RTC accessors are gone */
222 .ptr = &efi_runtime_services.get_time,
223 .patchto = &efi_get_time,
225 /* Clean up system table */
226 .ptr = &systab.con_in,
229 /* Clean up system table */
230 .ptr = &systab.con_out,
233 /* Clean up system table */
234 .ptr = &systab.std_err,
237 /* Clean up system table */
238 .ptr = &systab.boottime,
241 .ptr = &efi_runtime_services.get_variable,
242 .patchto = &efi_device_error,
244 .ptr = &efi_runtime_services.get_next_variable_name,
245 .patchto = &efi_device_error,
247 .ptr = &efi_runtime_services.set_variable,
248 .patchto = &efi_device_error,
252 static bool efi_runtime_tobedetached(void *p)
256 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
257 if (efi_runtime_detach_list[i].ptr == p)
263 static void efi_runtime_detach(ulong offset)
266 ulong patchoff = offset - (ulong)gd->relocaddr;
268 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
269 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
270 ulong *p = efi_runtime_detach_list[i].ptr;
271 ulong newaddr = patchto ? (patchto + patchoff) : 0;
273 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
278 /* Relocate EFI runtime to uboot_reloc_base = offset */
279 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
282 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
284 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
285 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
288 debug("%s: Relocating to offset=%lx\n", __func__, offset);
289 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
290 ulong base = CONFIG_SYS_TEXT_BASE;
294 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
296 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, rel->info, *p, rel->offset);
298 switch (rel->info & R_MASK) {
301 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
303 newaddr = *p - lastoff + offset;
308 ulong symidx = rel->info >> SYM_INDEX;
309 extern struct dyn_sym __dyn_sym_start[];
310 newaddr = __dyn_sym_start[symidx].addr + offset;
318 /* Check if the relocation is inside bounds */
319 if (map && ((newaddr < map->virtual_start) ||
320 newaddr > (map->virtual_start +
321 (map->num_pages << EFI_PAGE_SHIFT)))) {
322 if (!efi_runtime_tobedetached(p))
323 printf("U-Boot EFI: Relocation at %p is out of "
324 "range (%lx)\n", p, newaddr);
328 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
330 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
331 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
338 invalidate_icache_all();
341 static efi_status_t EFIAPI efi_set_virtual_address_map(
342 unsigned long memory_map_size,
343 unsigned long descriptor_size,
344 uint32_t descriptor_version,
345 struct efi_mem_desc *virtmap)
347 ulong runtime_start = (ulong)&__efi_runtime_start &
348 ~(ulong)EFI_PAGE_MASK;
349 int n = memory_map_size / descriptor_size;
352 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
353 descriptor_version, virtmap);
355 /* Rebind mmio pointers */
356 for (i = 0; i < n; i++) {
357 struct efi_mem_desc *map = (void*)virtmap +
358 (descriptor_size * i);
359 struct list_head *lhandle;
360 efi_physical_addr_t map_start = map->physical_start;
361 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
362 efi_physical_addr_t map_end = map_start + map_len;
364 /* Adjust all mmio pointers in this region */
365 list_for_each(lhandle, &efi_runtime_mmio) {
366 struct efi_runtime_mmio_list *lmmio;
368 lmmio = list_entry(lhandle,
369 struct efi_runtime_mmio_list,
371 if ((map_start <= lmmio->paddr) &&
372 (map_end >= lmmio->paddr)) {
373 u64 off = map->virtual_start - map_start;
374 uintptr_t new_addr = lmmio->paddr + off;
375 *lmmio->ptr = (void *)new_addr;
380 /* Move the actual runtime code over */
381 for (i = 0; i < n; i++) {
382 struct efi_mem_desc *map;
384 map = (void*)virtmap + (descriptor_size * i);
385 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
386 ulong new_offset = map->virtual_start -
387 (runtime_start - gd->relocaddr);
389 efi_runtime_relocate(new_offset, map);
390 /* Once we're virtual, we can no longer handle
392 efi_runtime_detach(new_offset);
393 return EFI_EXIT(EFI_SUCCESS);
397 return EFI_EXIT(EFI_INVALID_PARAMETER);
400 efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
402 struct efi_runtime_mmio_list *newmmio;
403 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
404 uint64_t addr = *(uintptr_t *)mmio_ptr;
407 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
409 return EFI_OUT_OF_RESOURCES;
411 newmmio = calloc(1, sizeof(*newmmio));
413 return EFI_OUT_OF_RESOURCES;
414 newmmio->ptr = mmio_ptr;
415 newmmio->paddr = *(uintptr_t *)mmio_ptr;
417 list_add_tail(&newmmio->link, &efi_runtime_mmio);
423 * In the second stage, U-Boot has disappeared. To isolate our runtime code
424 * that at this point still exists from the rest, we put it into a special
429 * This means that we can not rely on any code outside of this file in any
430 * function or variable below this line.
432 * Please keep everything fully self-contained and annotated with
433 * __efi_runtime and __efi_runtime_data markers.
437 * Relocate the EFI runtime stub to a different place. We need to call this
438 * the first time we expose the runtime interface to a user and on set virtual
442 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
444 return EFI_UNSUPPORTED;
447 static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
449 return EFI_DEVICE_ERROR;
452 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
454 return EFI_INVALID_PARAMETER;
457 efi_status_t __efi_runtime EFIAPI efi_update_capsule(
458 struct efi_capsule_header **capsule_header_array,
459 efi_uintn_t capsule_count,
460 u64 scatter_gather_list)
462 return EFI_UNSUPPORTED;
465 efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
466 struct efi_capsule_header **capsule_header_array,
467 efi_uintn_t capsule_count,
468 u64 maximum_capsule_size,
471 return EFI_UNSUPPORTED;
474 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
476 u64 *maximum_variable_storage_size,
477 u64 *remaining_variable_storage_size,
478 u64 *maximum_variable_size)
480 return EFI_UNSUPPORTED;
483 struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
485 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
486 .revision = EFI_SPECIFICATION_VERSION,
487 .headersize = sizeof(struct efi_runtime_services),
489 .get_time = &efi_get_time_boottime,
490 .set_time = (void *)&efi_device_error,
491 .get_wakeup_time = (void *)&efi_unimplemented,
492 .set_wakeup_time = (void *)&efi_unimplemented,
493 .set_virtual_address_map = &efi_set_virtual_address_map,
494 .convert_pointer = (void *)&efi_invalid_parameter,
495 .get_variable = efi_get_variable,
496 .get_next_variable_name = efi_get_next_variable_name,
497 .set_variable = efi_set_variable,
498 .get_next_high_mono_count = (void *)&efi_device_error,
499 .reset_system = &efi_reset_system_boottime,
500 .update_capsule = efi_update_capsule,
501 .query_capsule_caps = efi_query_capsule_caps,
502 .query_variable_info = efi_query_variable_info,