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
44 #error Need to add relocation awareness
59 * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
60 * payload are running concurrently at the same time. In this mode, we can
61 * handle a good number of runtime callbacks
64 static void EFIAPI efi_reset_system_boottime(
65 enum efi_reset_type reset_type,
66 efi_status_t reset_status,
67 unsigned long data_size, void *reset_data)
69 struct efi_event *evt;
71 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
75 list_for_each_entry(evt, &efi_events, link) {
78 &efi_guid_event_group_reset_system)) {
79 efi_signal_event(evt, false);
86 case EFI_RESET_PLATFORM_SPECIFIC:
87 do_reset(NULL, 0, 0, NULL);
89 case EFI_RESET_SHUTDOWN:
90 /* We don't have anything to map this to */
97 static efi_status_t EFIAPI efi_get_time_boottime(
98 struct efi_time *time,
99 struct efi_time_cap *capabilities)
101 #if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
106 EFI_ENTRY("%p %p", time, capabilities);
108 r = uclass_get_device(UCLASS_RTC, 0, &dev);
110 return EFI_EXIT(EFI_DEVICE_ERROR);
112 r = dm_rtc_get(dev, &tm);
114 return EFI_EXIT(EFI_DEVICE_ERROR);
116 memset(time, 0, sizeof(*time));
117 time->year = tm.tm_year;
118 time->month = tm.tm_mon;
119 time->day = tm.tm_mday;
120 time->hour = tm.tm_hour;
121 time->minute = tm.tm_min;
122 time->daylight = tm.tm_isdst;
124 return EFI_EXIT(EFI_SUCCESS);
126 return EFI_DEVICE_ERROR;
130 /* Boards may override the helpers below to implement RTS functionality */
132 void __weak __efi_runtime EFIAPI efi_reset_system(
133 enum efi_reset_type reset_type,
134 efi_status_t reset_status,
135 unsigned long data_size, void *reset_data)
137 /* Nothing we can do */
141 efi_status_t __weak efi_reset_system_init(void)
146 efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
147 struct efi_time *time,
148 struct efi_time_cap *capabilities)
150 /* Nothing we can do */
151 return EFI_DEVICE_ERROR;
154 efi_status_t __weak efi_get_time_init(void)
159 struct efi_runtime_detach_list_struct {
164 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
166 /* do_reset is gone */
167 .ptr = &efi_runtime_services.reset_system,
168 .patchto = efi_reset_system,
170 /* invalidate_*cache_all are gone */
171 .ptr = &efi_runtime_services.set_virtual_address_map,
172 .patchto = &efi_invalid_parameter,
174 /* RTC accessors are gone */
175 .ptr = &efi_runtime_services.get_time,
176 .patchto = &efi_get_time,
178 /* Clean up system table */
179 .ptr = &systab.con_in,
182 /* Clean up system table */
183 .ptr = &systab.con_out,
186 /* Clean up system table */
187 .ptr = &systab.std_err,
190 /* Clean up system table */
191 .ptr = &systab.boottime,
194 .ptr = &efi_runtime_services.get_variable,
195 .patchto = &efi_device_error,
197 .ptr = &efi_runtime_services.get_next_variable,
198 .patchto = &efi_device_error,
200 .ptr = &efi_runtime_services.set_variable,
201 .patchto = &efi_device_error,
205 static bool efi_runtime_tobedetached(void *p)
209 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
210 if (efi_runtime_detach_list[i].ptr == p)
216 static void efi_runtime_detach(ulong offset)
219 ulong patchoff = offset - (ulong)gd->relocaddr;
221 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
222 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
223 ulong *p = efi_runtime_detach_list[i].ptr;
224 ulong newaddr = patchto ? (patchto + patchoff) : 0;
226 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
231 /* Relocate EFI runtime to uboot_reloc_base = offset */
232 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
235 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
237 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
238 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
241 debug("%s: Relocating to offset=%lx\n", __func__, offset);
242 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
243 ulong base = CONFIG_SYS_TEXT_BASE;
247 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
249 if ((rel->info & R_MASK) != R_RELATIVE) {
254 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
256 newaddr = *p - lastoff + offset;
259 /* Check if the relocation is inside bounds */
260 if (map && ((newaddr < map->virtual_start) ||
261 newaddr > (map->virtual_start +
262 (map->num_pages << EFI_PAGE_SHIFT)))) {
263 if (!efi_runtime_tobedetached(p))
264 printf("U-Boot EFI: Relocation at %p is out of "
265 "range (%lx)\n", p, newaddr);
269 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
271 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
272 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
279 invalidate_icache_all();
282 static efi_status_t EFIAPI efi_set_virtual_address_map(
283 unsigned long memory_map_size,
284 unsigned long descriptor_size,
285 uint32_t descriptor_version,
286 struct efi_mem_desc *virtmap)
288 ulong runtime_start = (ulong)&__efi_runtime_start &
289 ~(ulong)EFI_PAGE_MASK;
290 int n = memory_map_size / descriptor_size;
293 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
294 descriptor_version, virtmap);
296 /* Rebind mmio pointers */
297 for (i = 0; i < n; i++) {
298 struct efi_mem_desc *map = (void*)virtmap +
299 (descriptor_size * i);
300 struct list_head *lhandle;
301 efi_physical_addr_t map_start = map->physical_start;
302 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
303 efi_physical_addr_t map_end = map_start + map_len;
305 /* Adjust all mmio pointers in this region */
306 list_for_each(lhandle, &efi_runtime_mmio) {
307 struct efi_runtime_mmio_list *lmmio;
309 lmmio = list_entry(lhandle,
310 struct efi_runtime_mmio_list,
312 if ((map_start <= lmmio->paddr) &&
313 (map_end >= lmmio->paddr)) {
314 u64 off = map->virtual_start - map_start;
315 uintptr_t new_addr = lmmio->paddr + off;
316 *lmmio->ptr = (void *)new_addr;
321 /* Move the actual runtime code over */
322 for (i = 0; i < n; i++) {
323 struct efi_mem_desc *map;
325 map = (void*)virtmap + (descriptor_size * i);
326 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
327 ulong new_offset = map->virtual_start -
328 (runtime_start - gd->relocaddr);
330 efi_runtime_relocate(new_offset, map);
331 /* Once we're virtual, we can no longer handle
333 efi_runtime_detach(new_offset);
334 return EFI_EXIT(EFI_SUCCESS);
338 return EFI_EXIT(EFI_INVALID_PARAMETER);
341 efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
343 struct efi_runtime_mmio_list *newmmio;
344 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
345 uint64_t addr = *(uintptr_t *)mmio_ptr;
348 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
350 return EFI_OUT_OF_RESOURCES;
352 newmmio = calloc(1, sizeof(*newmmio));
354 return EFI_OUT_OF_RESOURCES;
355 newmmio->ptr = mmio_ptr;
356 newmmio->paddr = *(uintptr_t *)mmio_ptr;
358 list_add_tail(&newmmio->link, &efi_runtime_mmio);
364 * In the second stage, U-Boot has disappeared. To isolate our runtime code
365 * that at this point still exists from the rest, we put it into a special
370 * This means that we can not rely on any code outside of this file in any
371 * function or variable below this line.
373 * Please keep everything fully self-contained and annotated with
374 * __efi_runtime and __efi_runtime_data markers.
378 * Relocate the EFI runtime stub to a different place. We need to call this
379 * the first time we expose the runtime interface to a user and on set virtual
383 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
385 return EFI_UNSUPPORTED;
388 static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
390 return EFI_DEVICE_ERROR;
393 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
395 return EFI_INVALID_PARAMETER;
398 efi_status_t __efi_runtime EFIAPI efi_update_capsule(
399 struct efi_capsule_header **capsule_header_array,
400 efi_uintn_t capsule_count,
401 u64 scatter_gather_list)
403 return EFI_UNSUPPORTED;
406 efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
407 struct efi_capsule_header **capsule_header_array,
408 efi_uintn_t capsule_count,
409 u64 maximum_capsule_size,
412 return EFI_UNSUPPORTED;
415 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
417 u64 maximum_variable_storage_size,
418 u64 remaining_variable_storage_size,
419 u64 maximum_variable_size)
421 return EFI_UNSUPPORTED;
424 struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
426 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
427 .revision = EFI_RUNTIME_SERVICES_REVISION,
428 .headersize = sizeof(struct efi_table_hdr),
430 .get_time = &efi_get_time_boottime,
431 .set_time = (void *)&efi_device_error,
432 .get_wakeup_time = (void *)&efi_unimplemented,
433 .set_wakeup_time = (void *)&efi_unimplemented,
434 .set_virtual_address_map = &efi_set_virtual_address_map,
435 .convert_pointer = (void *)&efi_invalid_parameter,
436 .get_variable = efi_get_variable,
437 .get_next_variable = efi_get_next_variable,
438 .set_variable = efi_set_variable,
439 .get_next_high_mono_count = (void *)&efi_device_error,
440 .reset_system = &efi_reset_system_boottime,
441 .update_capsule = efi_update_capsule,
442 .query_capsule_caps = efi_query_capsule_caps,
443 .query_variable_info = efi_query_variable_info,