1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application runtime services
5 * Copyright (c) 2016 Alexander Graf
11 #include <efi_loader.h>
13 #include <asm/global_data.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);
32 #if defined(CONFIG_ARM64)
33 #define R_RELATIVE 1027
34 #define R_MASK 0xffffffffULL
36 #elif defined(CONFIG_ARM)
38 #define R_MASK 0xffULL
39 #elif defined(CONFIG_X86)
41 #define R_RELATIVE R_386_RELATIVE
42 #define R_MASK 0xffULL
43 #elif defined(CONFIG_RISCV)
45 #define R_RELATIVE R_RISCV_RELATIVE
46 #define R_MASK 0xffULL
55 #ifdef CONFIG_CPU_RISCV_32
56 #define R_ABSOLUTE R_RISCV_32
59 #define R_ABSOLUTE R_RISCV_64
63 #error Need to add relocation awareness
78 * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
79 * payload are running concurrently at the same time. In this mode, we can
80 * handle a good number of runtime callbacks
83 static void EFIAPI efi_reset_system_boottime(
84 enum efi_reset_type reset_type,
85 efi_status_t reset_status,
86 unsigned long data_size, void *reset_data)
88 struct efi_event *evt;
90 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
94 list_for_each_entry(evt, &efi_events, link) {
97 &efi_guid_event_group_reset_system)) {
98 efi_signal_event(evt, false);
102 switch (reset_type) {
105 case EFI_RESET_PLATFORM_SPECIFIC:
106 do_reset(NULL, 0, 0, NULL);
108 case EFI_RESET_SHUTDOWN:
109 /* We don't have anything to map this to */
116 static efi_status_t EFIAPI efi_get_time_boottime(
117 struct efi_time *time,
118 struct efi_time_cap *capabilities)
120 #if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
125 EFI_ENTRY("%p %p", time, capabilities);
127 r = uclass_get_device(UCLASS_RTC, 0, &dev);
129 return EFI_EXIT(EFI_DEVICE_ERROR);
131 r = dm_rtc_get(dev, &tm);
133 return EFI_EXIT(EFI_DEVICE_ERROR);
135 memset(time, 0, sizeof(*time));
136 time->year = tm.tm_year;
137 time->month = tm.tm_mon;
138 time->day = tm.tm_mday;
139 time->hour = tm.tm_hour;
140 time->minute = tm.tm_min;
141 time->daylight = tm.tm_isdst;
143 return EFI_EXIT(EFI_SUCCESS);
145 return EFI_DEVICE_ERROR;
149 /* Boards may override the helpers below to implement RTS functionality */
151 void __weak __efi_runtime EFIAPI efi_reset_system(
152 enum efi_reset_type reset_type,
153 efi_status_t reset_status,
154 unsigned long data_size, void *reset_data)
156 /* Nothing we can do */
160 efi_status_t __weak efi_reset_system_init(void)
165 efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
166 struct efi_time *time,
167 struct efi_time_cap *capabilities)
169 /* Nothing we can do */
170 return EFI_DEVICE_ERROR;
173 efi_status_t __weak efi_get_time_init(void)
178 struct efi_runtime_detach_list_struct {
183 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
185 /* do_reset is gone */
186 .ptr = &efi_runtime_services.reset_system,
187 .patchto = efi_reset_system,
189 /* invalidate_*cache_all are gone */
190 .ptr = &efi_runtime_services.set_virtual_address_map,
191 .patchto = &efi_invalid_parameter,
193 /* RTC accessors are gone */
194 .ptr = &efi_runtime_services.get_time,
195 .patchto = &efi_get_time,
197 /* Clean up system table */
198 .ptr = &systab.con_in,
201 /* Clean up system table */
202 .ptr = &systab.con_out,
205 /* Clean up system table */
206 .ptr = &systab.std_err,
209 /* Clean up system table */
210 .ptr = &systab.boottime,
213 .ptr = &efi_runtime_services.get_variable,
214 .patchto = &efi_device_error,
216 .ptr = &efi_runtime_services.get_next_variable,
217 .patchto = &efi_device_error,
219 .ptr = &efi_runtime_services.set_variable,
220 .patchto = &efi_device_error,
224 static bool efi_runtime_tobedetached(void *p)
228 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
229 if (efi_runtime_detach_list[i].ptr == p)
235 static void efi_runtime_detach(ulong offset)
238 ulong patchoff = offset - (ulong)gd->relocaddr;
240 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
241 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
242 ulong *p = efi_runtime_detach_list[i].ptr;
243 ulong newaddr = patchto ? (patchto + patchoff) : 0;
245 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
250 /* Relocate EFI runtime to uboot_reloc_base = offset */
251 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
254 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
256 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
257 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
260 debug("%s: Relocating to offset=%lx\n", __func__, offset);
261 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
262 ulong base = CONFIG_SYS_TEXT_BASE;
266 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
268 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, rel->info, *p, rel->offset);
270 switch (rel->info & R_MASK) {
273 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
275 newaddr = *p - lastoff + offset;
280 ulong symidx = rel->info >> SYM_INDEX;
281 extern struct dyn_sym __dyn_sym_start[];
282 newaddr = __dyn_sym_start[symidx].addr + offset;
290 /* Check if the relocation is inside bounds */
291 if (map && ((newaddr < map->virtual_start) ||
292 newaddr > (map->virtual_start +
293 (map->num_pages << EFI_PAGE_SHIFT)))) {
294 if (!efi_runtime_tobedetached(p))
295 printf("U-Boot EFI: Relocation at %p is out of "
296 "range (%lx)\n", p, newaddr);
300 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
302 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
303 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
310 invalidate_icache_all();
313 static efi_status_t EFIAPI efi_set_virtual_address_map(
314 unsigned long memory_map_size,
315 unsigned long descriptor_size,
316 uint32_t descriptor_version,
317 struct efi_mem_desc *virtmap)
319 ulong runtime_start = (ulong)&__efi_runtime_start &
320 ~(ulong)EFI_PAGE_MASK;
321 int n = memory_map_size / descriptor_size;
324 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
325 descriptor_version, virtmap);
327 /* Rebind mmio pointers */
328 for (i = 0; i < n; i++) {
329 struct efi_mem_desc *map = (void*)virtmap +
330 (descriptor_size * i);
331 struct list_head *lhandle;
332 efi_physical_addr_t map_start = map->physical_start;
333 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
334 efi_physical_addr_t map_end = map_start + map_len;
336 /* Adjust all mmio pointers in this region */
337 list_for_each(lhandle, &efi_runtime_mmio) {
338 struct efi_runtime_mmio_list *lmmio;
340 lmmio = list_entry(lhandle,
341 struct efi_runtime_mmio_list,
343 if ((map_start <= lmmio->paddr) &&
344 (map_end >= lmmio->paddr)) {
345 u64 off = map->virtual_start - map_start;
346 uintptr_t new_addr = lmmio->paddr + off;
347 *lmmio->ptr = (void *)new_addr;
352 /* Move the actual runtime code over */
353 for (i = 0; i < n; i++) {
354 struct efi_mem_desc *map;
356 map = (void*)virtmap + (descriptor_size * i);
357 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
358 ulong new_offset = map->virtual_start -
359 (runtime_start - gd->relocaddr);
361 efi_runtime_relocate(new_offset, map);
362 /* Once we're virtual, we can no longer handle
364 efi_runtime_detach(new_offset);
365 return EFI_EXIT(EFI_SUCCESS);
369 return EFI_EXIT(EFI_INVALID_PARAMETER);
372 efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
374 struct efi_runtime_mmio_list *newmmio;
375 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
376 uint64_t addr = *(uintptr_t *)mmio_ptr;
379 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
381 return EFI_OUT_OF_RESOURCES;
383 newmmio = calloc(1, sizeof(*newmmio));
385 return EFI_OUT_OF_RESOURCES;
386 newmmio->ptr = mmio_ptr;
387 newmmio->paddr = *(uintptr_t *)mmio_ptr;
389 list_add_tail(&newmmio->link, &efi_runtime_mmio);
395 * In the second stage, U-Boot has disappeared. To isolate our runtime code
396 * that at this point still exists from the rest, we put it into a special
401 * This means that we can not rely on any code outside of this file in any
402 * function or variable below this line.
404 * Please keep everything fully self-contained and annotated with
405 * __efi_runtime and __efi_runtime_data markers.
409 * Relocate the EFI runtime stub to a different place. We need to call this
410 * the first time we expose the runtime interface to a user and on set virtual
414 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
416 return EFI_UNSUPPORTED;
419 static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
421 return EFI_DEVICE_ERROR;
424 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
426 return EFI_INVALID_PARAMETER;
429 efi_status_t __efi_runtime EFIAPI efi_update_capsule(
430 struct efi_capsule_header **capsule_header_array,
431 efi_uintn_t capsule_count,
432 u64 scatter_gather_list)
434 return EFI_UNSUPPORTED;
437 efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
438 struct efi_capsule_header **capsule_header_array,
439 efi_uintn_t capsule_count,
440 u64 maximum_capsule_size,
443 return EFI_UNSUPPORTED;
446 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
448 u64 maximum_variable_storage_size,
449 u64 remaining_variable_storage_size,
450 u64 maximum_variable_size)
452 return EFI_UNSUPPORTED;
455 struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
457 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
458 .revision = EFI_RUNTIME_SERVICES_REVISION,
459 .headersize = sizeof(struct efi_table_hdr),
461 .get_time = &efi_get_time_boottime,
462 .set_time = (void *)&efi_device_error,
463 .get_wakeup_time = (void *)&efi_unimplemented,
464 .set_wakeup_time = (void *)&efi_unimplemented,
465 .set_virtual_address_map = &efi_set_virtual_address_map,
466 .convert_pointer = (void *)&efi_invalid_parameter,
467 .get_variable = efi_get_variable,
468 .get_next_variable = efi_get_next_variable,
469 .set_variable = efi_set_variable,
470 .get_next_high_mono_count = (void *)&efi_device_error,
471 .reset_system = &efi_reset_system_boottime,
472 .update_capsule = efi_update_capsule,
473 .query_capsule_caps = efi_query_capsule_caps,
474 .query_variable_info = efi_query_variable_info,