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 */
120 static efi_status_t EFIAPI efi_get_time_boottime(
121 struct efi_time *time,
122 struct efi_time_cap *capabilities)
124 #if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
129 EFI_ENTRY("%p %p", time, capabilities);
131 r = uclass_get_device(UCLASS_RTC, 0, &dev);
133 return EFI_EXIT(EFI_DEVICE_ERROR);
135 r = dm_rtc_get(dev, &tm);
137 return EFI_EXIT(EFI_DEVICE_ERROR);
139 memset(time, 0, sizeof(*time));
140 time->year = tm.tm_year;
141 time->month = tm.tm_mon;
142 time->day = tm.tm_mday;
143 time->hour = tm.tm_hour;
144 time->minute = tm.tm_min;
145 time->daylight = tm.tm_isdst;
147 return EFI_EXIT(EFI_SUCCESS);
149 return EFI_DEVICE_ERROR;
153 /* Boards may override the helpers below to implement RTS functionality */
155 void __weak __efi_runtime EFIAPI efi_reset_system(
156 enum efi_reset_type reset_type,
157 efi_status_t reset_status,
158 unsigned long data_size, void *reset_data)
160 /* Nothing we can do */
164 efi_status_t __weak efi_reset_system_init(void)
169 efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
170 struct efi_time *time,
171 struct efi_time_cap *capabilities)
173 /* Nothing we can do */
174 return EFI_DEVICE_ERROR;
177 efi_status_t __weak efi_get_time_init(void)
182 struct efi_runtime_detach_list_struct {
187 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
189 /* do_reset is gone */
190 .ptr = &efi_runtime_services.reset_system,
191 .patchto = efi_reset_system,
193 /* invalidate_*cache_all are gone */
194 .ptr = &efi_runtime_services.set_virtual_address_map,
195 .patchto = &efi_invalid_parameter,
197 /* RTC accessors are gone */
198 .ptr = &efi_runtime_services.get_time,
199 .patchto = &efi_get_time,
201 /* Clean up system table */
202 .ptr = &systab.con_in,
205 /* Clean up system table */
206 .ptr = &systab.con_out,
209 /* Clean up system table */
210 .ptr = &systab.std_err,
213 /* Clean up system table */
214 .ptr = &systab.boottime,
217 .ptr = &efi_runtime_services.get_variable,
218 .patchto = &efi_device_error,
220 .ptr = &efi_runtime_services.get_next_variable_name,
221 .patchto = &efi_device_error,
223 .ptr = &efi_runtime_services.set_variable,
224 .patchto = &efi_device_error,
228 static bool efi_runtime_tobedetached(void *p)
232 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
233 if (efi_runtime_detach_list[i].ptr == p)
239 static void efi_runtime_detach(ulong offset)
242 ulong patchoff = offset - (ulong)gd->relocaddr;
244 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
245 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
246 ulong *p = efi_runtime_detach_list[i].ptr;
247 ulong newaddr = patchto ? (patchto + patchoff) : 0;
249 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
254 /* Relocate EFI runtime to uboot_reloc_base = offset */
255 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
258 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
260 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
261 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
264 debug("%s: Relocating to offset=%lx\n", __func__, offset);
265 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
266 ulong base = CONFIG_SYS_TEXT_BASE;
270 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
272 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, rel->info, *p, rel->offset);
274 switch (rel->info & R_MASK) {
277 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
279 newaddr = *p - lastoff + offset;
284 ulong symidx = rel->info >> SYM_INDEX;
285 extern struct dyn_sym __dyn_sym_start[];
286 newaddr = __dyn_sym_start[symidx].addr + offset;
294 /* Check if the relocation is inside bounds */
295 if (map && ((newaddr < map->virtual_start) ||
296 newaddr > (map->virtual_start +
297 (map->num_pages << EFI_PAGE_SHIFT)))) {
298 if (!efi_runtime_tobedetached(p))
299 printf("U-Boot EFI: Relocation at %p is out of "
300 "range (%lx)\n", p, newaddr);
304 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
306 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
307 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
314 invalidate_icache_all();
317 static efi_status_t EFIAPI efi_set_virtual_address_map(
318 unsigned long memory_map_size,
319 unsigned long descriptor_size,
320 uint32_t descriptor_version,
321 struct efi_mem_desc *virtmap)
323 ulong runtime_start = (ulong)&__efi_runtime_start &
324 ~(ulong)EFI_PAGE_MASK;
325 int n = memory_map_size / descriptor_size;
328 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
329 descriptor_version, virtmap);
331 /* Rebind mmio pointers */
332 for (i = 0; i < n; i++) {
333 struct efi_mem_desc *map = (void*)virtmap +
334 (descriptor_size * i);
335 struct list_head *lhandle;
336 efi_physical_addr_t map_start = map->physical_start;
337 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
338 efi_physical_addr_t map_end = map_start + map_len;
340 /* Adjust all mmio pointers in this region */
341 list_for_each(lhandle, &efi_runtime_mmio) {
342 struct efi_runtime_mmio_list *lmmio;
344 lmmio = list_entry(lhandle,
345 struct efi_runtime_mmio_list,
347 if ((map_start <= lmmio->paddr) &&
348 (map_end >= lmmio->paddr)) {
349 u64 off = map->virtual_start - map_start;
350 uintptr_t new_addr = lmmio->paddr + off;
351 *lmmio->ptr = (void *)new_addr;
356 /* Move the actual runtime code over */
357 for (i = 0; i < n; i++) {
358 struct efi_mem_desc *map;
360 map = (void*)virtmap + (descriptor_size * i);
361 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
362 ulong new_offset = map->virtual_start -
363 (runtime_start - gd->relocaddr);
365 efi_runtime_relocate(new_offset, map);
366 /* Once we're virtual, we can no longer handle
368 efi_runtime_detach(new_offset);
369 return EFI_EXIT(EFI_SUCCESS);
373 return EFI_EXIT(EFI_INVALID_PARAMETER);
376 efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
378 struct efi_runtime_mmio_list *newmmio;
379 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
380 uint64_t addr = *(uintptr_t *)mmio_ptr;
383 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
385 return EFI_OUT_OF_RESOURCES;
387 newmmio = calloc(1, sizeof(*newmmio));
389 return EFI_OUT_OF_RESOURCES;
390 newmmio->ptr = mmio_ptr;
391 newmmio->paddr = *(uintptr_t *)mmio_ptr;
393 list_add_tail(&newmmio->link, &efi_runtime_mmio);
399 * In the second stage, U-Boot has disappeared. To isolate our runtime code
400 * that at this point still exists from the rest, we put it into a special
405 * This means that we can not rely on any code outside of this file in any
406 * function or variable below this line.
408 * Please keep everything fully self-contained and annotated with
409 * __efi_runtime and __efi_runtime_data markers.
413 * Relocate the EFI runtime stub to a different place. We need to call this
414 * the first time we expose the runtime interface to a user and on set virtual
418 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
420 return EFI_UNSUPPORTED;
423 static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
425 return EFI_DEVICE_ERROR;
428 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
430 return EFI_INVALID_PARAMETER;
433 efi_status_t __efi_runtime EFIAPI efi_update_capsule(
434 struct efi_capsule_header **capsule_header_array,
435 efi_uintn_t capsule_count,
436 u64 scatter_gather_list)
438 return EFI_UNSUPPORTED;
441 efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
442 struct efi_capsule_header **capsule_header_array,
443 efi_uintn_t capsule_count,
444 u64 maximum_capsule_size,
447 return EFI_UNSUPPORTED;
450 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
452 u64 *maximum_variable_storage_size,
453 u64 *remaining_variable_storage_size,
454 u64 *maximum_variable_size)
456 return EFI_UNSUPPORTED;
459 struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
461 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
462 .revision = EFI_SPECIFICATION_VERSION,
463 .headersize = sizeof(struct efi_runtime_services),
465 .get_time = &efi_get_time_boottime,
466 .set_time = (void *)&efi_device_error,
467 .get_wakeup_time = (void *)&efi_unimplemented,
468 .set_wakeup_time = (void *)&efi_unimplemented,
469 .set_virtual_address_map = &efi_set_virtual_address_map,
470 .convert_pointer = (void *)&efi_invalid_parameter,
471 .get_variable = efi_get_variable,
472 .get_next_variable_name = efi_get_next_variable_name,
473 .set_variable = efi_set_variable,
474 .get_next_high_mono_count = (void *)&efi_device_error,
475 .reset_system = &efi_reset_system_boottime,
476 .update_capsule = efi_update_capsule,
477 .query_capsule_caps = efi_query_capsule_caps,
478 .query_variable_info = efi_query_variable_info,