1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI application runtime services
5 * Copyright (c) 2016 Alexander Graf
11 #include <efi_loader.h>
14 /* For manual relocation support */
15 DECLARE_GLOBAL_DATA_PTR;
17 struct efi_runtime_mmio_list {
18 struct list_head link;
24 /* This list contains all runtime available mmio regions */
25 LIST_HEAD(efi_runtime_mmio);
27 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void);
28 static efi_status_t __efi_runtime EFIAPI efi_device_error(void);
29 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void);
32 * TODO(sjg@chromium.org): These defines and structs should come from the elf
33 * header for each arch (or a generic header) rather than being repeated here.
35 #if defined(__aarch64__)
36 #define R_RELATIVE 1027
37 #define R_MASK 0xffffffffULL
39 #elif defined(__arm__)
41 #define R_MASK 0xffULL
42 #elif defined(__x86_64__) || defined(__i386__)
44 #define R_RELATIVE R_386_RELATIVE
45 #define R_MASK 0xffULL
46 #elif defined(__riscv)
48 #define R_RELATIVE R_RISCV_RELATIVE
49 #define R_MASK 0xffULL
58 #if (__riscv_xlen == 32)
59 #define R_ABSOLUTE R_RISCV_32
61 #elif (__riscv_xlen == 64)
62 #define R_ABSOLUTE R_RISCV_64
65 #error unknown riscv target
68 #error Need to add relocation awareness
83 * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
84 * payload are running concurrently at the same time. In this mode, we can
85 * handle a good number of runtime callbacks
88 static void EFIAPI efi_reset_system_boottime(
89 enum efi_reset_type reset_type,
90 efi_status_t reset_status,
91 unsigned long data_size, void *reset_data)
93 struct efi_event *evt;
95 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
99 list_for_each_entry(evt, &efi_events, link) {
102 &efi_guid_event_group_reset_system)) {
103 efi_signal_event(evt, false);
107 switch (reset_type) {
110 case EFI_RESET_PLATFORM_SPECIFIC:
111 do_reset(NULL, 0, 0, NULL);
113 case EFI_RESET_SHUTDOWN:
114 /* We don't have anything to map this to */
121 static efi_status_t EFIAPI efi_get_time_boottime(
122 struct efi_time *time,
123 struct efi_time_cap *capabilities)
125 #if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
130 EFI_ENTRY("%p %p", time, capabilities);
132 r = uclass_get_device(UCLASS_RTC, 0, &dev);
134 return EFI_EXIT(EFI_DEVICE_ERROR);
136 r = dm_rtc_get(dev, &tm);
138 return EFI_EXIT(EFI_DEVICE_ERROR);
140 memset(time, 0, sizeof(*time));
141 time->year = tm.tm_year;
142 time->month = tm.tm_mon;
143 time->day = tm.tm_mday;
144 time->hour = tm.tm_hour;
145 time->minute = tm.tm_min;
146 time->daylight = tm.tm_isdst;
148 return EFI_EXIT(EFI_SUCCESS);
150 return EFI_DEVICE_ERROR;
154 /* Boards may override the helpers below to implement RTS functionality */
156 void __weak __efi_runtime EFIAPI efi_reset_system(
157 enum efi_reset_type reset_type,
158 efi_status_t reset_status,
159 unsigned long data_size, void *reset_data)
161 /* Nothing we can do */
165 efi_status_t __weak efi_reset_system_init(void)
170 efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
171 struct efi_time *time,
172 struct efi_time_cap *capabilities)
174 /* Nothing we can do */
175 return EFI_DEVICE_ERROR;
178 efi_status_t __weak efi_get_time_init(void)
183 struct efi_runtime_detach_list_struct {
188 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
190 /* do_reset is gone */
191 .ptr = &efi_runtime_services.reset_system,
192 .patchto = efi_reset_system,
194 /* invalidate_*cache_all are gone */
195 .ptr = &efi_runtime_services.set_virtual_address_map,
196 .patchto = &efi_invalid_parameter,
198 /* RTC accessors are gone */
199 .ptr = &efi_runtime_services.get_time,
200 .patchto = &efi_get_time,
202 /* Clean up system table */
203 .ptr = &systab.con_in,
206 /* Clean up system table */
207 .ptr = &systab.con_out,
210 /* Clean up system table */
211 .ptr = &systab.std_err,
214 /* Clean up system table */
215 .ptr = &systab.boottime,
218 .ptr = &efi_runtime_services.get_variable,
219 .patchto = &efi_device_error,
221 .ptr = &efi_runtime_services.get_next_variable_name,
222 .patchto = &efi_device_error,
224 .ptr = &efi_runtime_services.set_variable,
225 .patchto = &efi_device_error,
229 static bool efi_runtime_tobedetached(void *p)
233 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
234 if (efi_runtime_detach_list[i].ptr == p)
240 static void efi_runtime_detach(ulong offset)
243 ulong patchoff = offset - (ulong)gd->relocaddr;
245 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
246 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
247 ulong *p = efi_runtime_detach_list[i].ptr;
248 ulong newaddr = patchto ? (patchto + patchoff) : 0;
250 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
255 /* Relocate EFI runtime to uboot_reloc_base = offset */
256 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
259 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
261 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
262 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
265 debug("%s: Relocating to offset=%lx\n", __func__, offset);
266 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
267 ulong base = CONFIG_SYS_TEXT_BASE;
271 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
273 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, rel->info, *p, rel->offset);
275 switch (rel->info & R_MASK) {
278 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
280 newaddr = *p - lastoff + offset;
285 ulong symidx = rel->info >> SYM_INDEX;
286 extern struct dyn_sym __dyn_sym_start[];
287 newaddr = __dyn_sym_start[symidx].addr + offset;
295 /* Check if the relocation is inside bounds */
296 if (map && ((newaddr < map->virtual_start) ||
297 newaddr > (map->virtual_start +
298 (map->num_pages << EFI_PAGE_SHIFT)))) {
299 if (!efi_runtime_tobedetached(p))
300 printf("U-Boot EFI: Relocation at %p is out of "
301 "range (%lx)\n", p, newaddr);
305 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
307 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
308 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
315 invalidate_icache_all();
318 static efi_status_t EFIAPI efi_set_virtual_address_map(
319 unsigned long memory_map_size,
320 unsigned long descriptor_size,
321 uint32_t descriptor_version,
322 struct efi_mem_desc *virtmap)
324 ulong runtime_start = (ulong)&__efi_runtime_start &
325 ~(ulong)EFI_PAGE_MASK;
326 int n = memory_map_size / descriptor_size;
329 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
330 descriptor_version, virtmap);
332 /* Rebind mmio pointers */
333 for (i = 0; i < n; i++) {
334 struct efi_mem_desc *map = (void*)virtmap +
335 (descriptor_size * i);
336 struct list_head *lhandle;
337 efi_physical_addr_t map_start = map->physical_start;
338 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
339 efi_physical_addr_t map_end = map_start + map_len;
341 /* Adjust all mmio pointers in this region */
342 list_for_each(lhandle, &efi_runtime_mmio) {
343 struct efi_runtime_mmio_list *lmmio;
345 lmmio = list_entry(lhandle,
346 struct efi_runtime_mmio_list,
348 if ((map_start <= lmmio->paddr) &&
349 (map_end >= lmmio->paddr)) {
350 u64 off = map->virtual_start - map_start;
351 uintptr_t new_addr = lmmio->paddr + off;
352 *lmmio->ptr = (void *)new_addr;
357 /* Move the actual runtime code over */
358 for (i = 0; i < n; i++) {
359 struct efi_mem_desc *map;
361 map = (void*)virtmap + (descriptor_size * i);
362 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
363 ulong new_offset = map->virtual_start -
364 (runtime_start - gd->relocaddr);
366 efi_runtime_relocate(new_offset, map);
367 /* Once we're virtual, we can no longer handle
369 efi_runtime_detach(new_offset);
370 return EFI_EXIT(EFI_SUCCESS);
374 return EFI_EXIT(EFI_INVALID_PARAMETER);
377 efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
379 struct efi_runtime_mmio_list *newmmio;
380 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
381 uint64_t addr = *(uintptr_t *)mmio_ptr;
384 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
386 return EFI_OUT_OF_RESOURCES;
388 newmmio = calloc(1, sizeof(*newmmio));
390 return EFI_OUT_OF_RESOURCES;
391 newmmio->ptr = mmio_ptr;
392 newmmio->paddr = *(uintptr_t *)mmio_ptr;
394 list_add_tail(&newmmio->link, &efi_runtime_mmio);
400 * In the second stage, U-Boot has disappeared. To isolate our runtime code
401 * that at this point still exists from the rest, we put it into a special
406 * This means that we can not rely on any code outside of this file in any
407 * function or variable below this line.
409 * Please keep everything fully self-contained and annotated with
410 * __efi_runtime and __efi_runtime_data markers.
414 * Relocate the EFI runtime stub to a different place. We need to call this
415 * the first time we expose the runtime interface to a user and on set virtual
419 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
421 return EFI_UNSUPPORTED;
424 static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
426 return EFI_DEVICE_ERROR;
429 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
431 return EFI_INVALID_PARAMETER;
434 efi_status_t __efi_runtime EFIAPI efi_update_capsule(
435 struct efi_capsule_header **capsule_header_array,
436 efi_uintn_t capsule_count,
437 u64 scatter_gather_list)
439 return EFI_UNSUPPORTED;
442 efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
443 struct efi_capsule_header **capsule_header_array,
444 efi_uintn_t capsule_count,
445 u64 maximum_capsule_size,
448 return EFI_UNSUPPORTED;
451 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
453 u64 *maximum_variable_storage_size,
454 u64 *remaining_variable_storage_size,
455 u64 *maximum_variable_size)
457 return EFI_UNSUPPORTED;
460 struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
462 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
463 .revision = EFI_RUNTIME_SERVICES_REVISION,
464 .headersize = sizeof(struct efi_table_hdr),
466 .get_time = &efi_get_time_boottime,
467 .set_time = (void *)&efi_device_error,
468 .get_wakeup_time = (void *)&efi_unimplemented,
469 .set_wakeup_time = (void *)&efi_unimplemented,
470 .set_virtual_address_map = &efi_set_virtual_address_map,
471 .convert_pointer = (void *)&efi_invalid_parameter,
472 .get_variable = efi_get_variable,
473 .get_next_variable_name = efi_get_next_variable_name,
474 .set_variable = efi_set_variable,
475 .get_next_high_mono_count = (void *)&efi_device_error,
476 .reset_system = &efi_reset_system_boottime,
477 .update_capsule = efi_update_capsule,
478 .query_capsule_caps = efi_query_capsule_caps,
479 .query_variable_info = efi_query_variable_info,