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,
99 static void EFIAPI efi_reset_system_boottime(
100 enum efi_reset_type reset_type,
101 efi_status_t reset_status,
102 unsigned long data_size, void *reset_data)
104 struct efi_event *evt;
106 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
110 list_for_each_entry(evt, &efi_events, link) {
113 &efi_guid_event_group_reset_system)) {
114 efi_signal_event(evt, false);
118 switch (reset_type) {
121 case EFI_RESET_PLATFORM_SPECIFIC:
122 do_reset(NULL, 0, 0, NULL);
124 case EFI_RESET_SHUTDOWN:
125 /* We don't have anything to map this to */
133 * efi_get_time_boottime - get current time
135 * This function implements the GetTime runtime service.
136 * See the Unified Extensible Firmware Interface (UEFI) specification
139 * @time: pointer to structure to receive current time
140 * @capabilities: pointer to structure to receive RTC properties
141 * Return Value: status code
143 static efi_status_t EFIAPI efi_get_time_boottime(
144 struct efi_time *time,
145 struct efi_time_cap *capabilities)
148 efi_status_t ret = EFI_SUCCESS;
153 EFI_ENTRY("%p %p", time, capabilities);
156 ret = EFI_INVALID_PARAMETER;
160 r = uclass_get_device(UCLASS_RTC, 0, &dev);
162 r = dm_rtc_get(dev, &tm);
164 ret = EFI_DEVICE_ERROR;
168 memset(time, 0, sizeof(*time));
169 time->year = tm.tm_year;
170 time->month = tm.tm_mon;
171 time->day = tm.tm_mday;
172 time->hour = tm.tm_hour;
173 time->minute = tm.tm_min;
174 time->second = tm.tm_sec;
175 time->daylight = EFI_TIME_ADJUST_DAYLIGHT;
177 time->daylight |= EFI_TIME_IN_DAYLIGHT;
178 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
181 /* Set reasonable dummy values */
182 capabilities->resolution = 1; /* 1 Hz */
183 capabilities->accuracy = 100000000; /* 100 ppm */
184 capabilities->sets_to_zero = false;
187 return EFI_EXIT(ret);
189 EFI_ENTRY("%p %p", time, capabilities);
190 return EFI_EXIT(EFI_DEVICE_ERROR);
194 /* Boards may override the helpers below to implement RTS functionality */
196 void __weak __efi_runtime EFIAPI efi_reset_system(
197 enum efi_reset_type reset_type,
198 efi_status_t reset_status,
199 unsigned long data_size, void *reset_data)
201 /* Nothing we can do */
205 efi_status_t __weak efi_reset_system_init(void)
210 efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
211 struct efi_time *time,
212 struct efi_time_cap *capabilities)
214 /* Nothing we can do */
215 return EFI_DEVICE_ERROR;
218 struct efi_runtime_detach_list_struct {
223 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
225 /* do_reset is gone */
226 .ptr = &efi_runtime_services.reset_system,
227 .patchto = efi_reset_system,
229 /* invalidate_*cache_all are gone */
230 .ptr = &efi_runtime_services.set_virtual_address_map,
231 .patchto = &efi_invalid_parameter,
233 /* RTC accessors are gone */
234 .ptr = &efi_runtime_services.get_time,
235 .patchto = &efi_get_time,
237 /* Clean up system table */
238 .ptr = &systab.con_in,
241 /* Clean up system table */
242 .ptr = &systab.con_out,
245 /* Clean up system table */
246 .ptr = &systab.std_err,
249 /* Clean up system table */
250 .ptr = &systab.boottime,
253 .ptr = &efi_runtime_services.get_variable,
254 .patchto = &efi_device_error,
256 .ptr = &efi_runtime_services.get_next_variable_name,
257 .patchto = &efi_device_error,
259 .ptr = &efi_runtime_services.set_variable,
260 .patchto = &efi_device_error,
264 static bool efi_runtime_tobedetached(void *p)
268 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
269 if (efi_runtime_detach_list[i].ptr == p)
275 static void efi_runtime_detach(ulong offset)
278 ulong patchoff = offset - (ulong)gd->relocaddr;
280 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
281 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
282 ulong *p = efi_runtime_detach_list[i].ptr;
283 ulong newaddr = patchto ? (patchto + patchoff) : 0;
285 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
290 efi_update_table_header_crc32(&efi_runtime_services.hdr);
293 /* Relocate EFI runtime to uboot_reloc_base = offset */
294 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
297 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
299 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
300 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
303 debug("%s: Relocating to offset=%lx\n", __func__, offset);
304 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
305 ulong base = CONFIG_SYS_TEXT_BASE;
309 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
311 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, rel->info, *p, rel->offset);
313 switch (rel->info & R_MASK) {
316 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
318 newaddr = *p - lastoff + offset;
323 ulong symidx = rel->info >> SYM_INDEX;
324 extern struct dyn_sym __dyn_sym_start[];
325 newaddr = __dyn_sym_start[symidx].addr + offset;
333 /* Check if the relocation is inside bounds */
334 if (map && ((newaddr < map->virtual_start) ||
335 newaddr > (map->virtual_start +
336 (map->num_pages << EFI_PAGE_SHIFT)))) {
337 if (!efi_runtime_tobedetached(p))
338 printf("U-Boot EFI: Relocation at %p is out of "
339 "range (%lx)\n", p, newaddr);
343 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
345 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
346 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
353 invalidate_icache_all();
356 static efi_status_t EFIAPI efi_set_virtual_address_map(
357 unsigned long memory_map_size,
358 unsigned long descriptor_size,
359 uint32_t descriptor_version,
360 struct efi_mem_desc *virtmap)
362 ulong runtime_start = (ulong)&__efi_runtime_start &
363 ~(ulong)EFI_PAGE_MASK;
364 int n = memory_map_size / descriptor_size;
367 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
368 descriptor_version, virtmap);
370 /* Rebind mmio pointers */
371 for (i = 0; i < n; i++) {
372 struct efi_mem_desc *map = (void*)virtmap +
373 (descriptor_size * i);
374 struct list_head *lhandle;
375 efi_physical_addr_t map_start = map->physical_start;
376 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
377 efi_physical_addr_t map_end = map_start + map_len;
378 u64 off = map->virtual_start - map_start;
380 /* Adjust all mmio pointers in this region */
381 list_for_each(lhandle, &efi_runtime_mmio) {
382 struct efi_runtime_mmio_list *lmmio;
384 lmmio = list_entry(lhandle,
385 struct efi_runtime_mmio_list,
387 if ((map_start <= lmmio->paddr) &&
388 (map_end >= lmmio->paddr)) {
389 uintptr_t new_addr = lmmio->paddr + off;
390 *lmmio->ptr = (void *)new_addr;
393 if ((map_start <= (uintptr_t)systab.tables) &&
394 (map_end >= (uintptr_t)systab.tables)) {
395 char *ptr = (char *)systab.tables;
398 systab.tables = (struct efi_configuration_table *)ptr;
402 /* Move the actual runtime code over */
403 for (i = 0; i < n; i++) {
404 struct efi_mem_desc *map;
406 map = (void*)virtmap + (descriptor_size * i);
407 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
408 ulong new_offset = map->virtual_start -
409 (runtime_start - gd->relocaddr);
411 efi_runtime_relocate(new_offset, map);
412 /* Once we're virtual, we can no longer handle
414 efi_runtime_detach(new_offset);
415 return EFI_EXIT(EFI_SUCCESS);
419 return EFI_EXIT(EFI_INVALID_PARAMETER);
422 efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
424 struct efi_runtime_mmio_list *newmmio;
425 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
426 uint64_t addr = *(uintptr_t *)mmio_ptr;
429 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
431 return EFI_OUT_OF_RESOURCES;
433 newmmio = calloc(1, sizeof(*newmmio));
435 return EFI_OUT_OF_RESOURCES;
436 newmmio->ptr = mmio_ptr;
437 newmmio->paddr = *(uintptr_t *)mmio_ptr;
439 list_add_tail(&newmmio->link, &efi_runtime_mmio);
445 * In the second stage, U-Boot has disappeared. To isolate our runtime code
446 * that at this point still exists from the rest, we put it into a special
451 * This means that we can not rely on any code outside of this file in any
452 * function or variable below this line.
454 * Please keep everything fully self-contained and annotated with
455 * __efi_runtime and __efi_runtime_data markers.
459 * Relocate the EFI runtime stub to a different place. We need to call this
460 * the first time we expose the runtime interface to a user and on set virtual
464 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
466 return EFI_UNSUPPORTED;
469 static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
471 return EFI_DEVICE_ERROR;
474 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
476 return EFI_INVALID_PARAMETER;
479 efi_status_t __efi_runtime EFIAPI efi_update_capsule(
480 struct efi_capsule_header **capsule_header_array,
481 efi_uintn_t capsule_count,
482 u64 scatter_gather_list)
484 return EFI_UNSUPPORTED;
487 efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
488 struct efi_capsule_header **capsule_header_array,
489 efi_uintn_t capsule_count,
490 u64 maximum_capsule_size,
493 return EFI_UNSUPPORTED;
496 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
498 u64 *maximum_variable_storage_size,
499 u64 *remaining_variable_storage_size,
500 u64 *maximum_variable_size)
502 return EFI_UNSUPPORTED;
505 struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
507 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
508 .revision = EFI_SPECIFICATION_VERSION,
509 .headersize = sizeof(struct efi_runtime_services),
511 .get_time = &efi_get_time_boottime,
512 .set_time = (void *)&efi_device_error,
513 .get_wakeup_time = (void *)&efi_unimplemented,
514 .set_wakeup_time = (void *)&efi_unimplemented,
515 .set_virtual_address_map = &efi_set_virtual_address_map,
516 .convert_pointer = (void *)&efi_invalid_parameter,
517 .get_variable = efi_get_variable,
518 .get_next_variable_name = efi_get_next_variable_name,
519 .set_variable = efi_set_variable,
520 .get_next_high_mono_count = (void *)&efi_device_error,
521 .reset_system = &efi_reset_system_boottime,
522 .update_capsule = efi_update_capsule,
523 .query_capsule_caps = efi_query_capsule_caps,
524 .query_variable_info = efi_query_variable_info,