2 * EFI application runtime services
4 * Copyright (c) 2016 Alexander Graf
6 * SPDX-License-Identifier: GPL-2.0+
12 #include <efi_loader.h>
14 #include <asm/global_data.h>
16 /* For manual relocation support */
17 DECLARE_GLOBAL_DATA_PTR;
19 struct efi_runtime_mmio_list {
20 struct list_head link;
26 /* This list contains all runtime available mmio regions */
27 LIST_HEAD(efi_runtime_mmio);
29 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void);
30 static efi_status_t __efi_runtime EFIAPI efi_device_error(void);
31 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void);
33 #ifdef CONFIG_SYS_CACHELINE_SIZE
34 #define EFI_CACHELINE_SIZE CONFIG_SYS_CACHELINE_SIZE
36 /* Just use the greatest cache flush alignment requirement I'm aware of */
37 #define EFI_CACHELINE_SIZE 128
40 #if defined(CONFIG_ARM64)
41 #define R_RELATIVE 1027
42 #define R_MASK 0xffffffffULL
44 #elif defined(CONFIG_ARM)
46 #define R_MASK 0xffULL
47 #elif defined(CONFIG_X86)
49 #define R_RELATIVE R_386_RELATIVE
50 #define R_MASK 0xffULL
52 #error Need to add relocation awareness
67 * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
68 * payload are running concurrently at the same time. In this mode, we can
69 * handle a good number of runtime callbacks
72 static void EFIAPI efi_reset_system_boottime(
73 enum efi_reset_type reset_type,
74 efi_status_t reset_status,
75 unsigned long data_size, void *reset_data)
77 struct efi_event *evt;
79 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
83 list_for_each_entry(evt, &efi_events, link) {
86 &efi_guid_event_group_reset_system)) {
87 efi_signal_event(evt, false);
94 case EFI_RESET_PLATFORM_SPECIFIC:
95 do_reset(NULL, 0, 0, NULL);
97 case EFI_RESET_SHUTDOWN:
98 /* We don't have anything to map this to */
105 static efi_status_t EFIAPI efi_get_time_boottime(
106 struct efi_time *time,
107 struct efi_time_cap *capabilities)
109 #if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
114 EFI_ENTRY("%p %p", time, capabilities);
116 r = uclass_get_device(UCLASS_RTC, 0, &dev);
118 return EFI_EXIT(EFI_DEVICE_ERROR);
120 r = dm_rtc_get(dev, &tm);
122 return EFI_EXIT(EFI_DEVICE_ERROR);
124 memset(time, 0, sizeof(*time));
125 time->year = tm.tm_year;
126 time->month = tm.tm_mon;
127 time->day = tm.tm_mday;
128 time->hour = tm.tm_hour;
129 time->minute = tm.tm_min;
130 time->daylight = tm.tm_isdst;
132 return EFI_EXIT(EFI_SUCCESS);
134 return EFI_DEVICE_ERROR;
138 /* Boards may override the helpers below to implement RTS functionality */
140 void __weak __efi_runtime EFIAPI efi_reset_system(
141 enum efi_reset_type reset_type,
142 efi_status_t reset_status,
143 unsigned long data_size, void *reset_data)
145 /* Nothing we can do */
149 efi_status_t __weak efi_reset_system_init(void)
154 efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
155 struct efi_time *time,
156 struct efi_time_cap *capabilities)
158 /* Nothing we can do */
159 return EFI_DEVICE_ERROR;
162 efi_status_t __weak efi_get_time_init(void)
167 struct efi_runtime_detach_list_struct {
172 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
174 /* do_reset is gone */
175 .ptr = &efi_runtime_services.reset_system,
176 .patchto = efi_reset_system,
178 /* invalidate_*cache_all are gone */
179 .ptr = &efi_runtime_services.set_virtual_address_map,
180 .patchto = &efi_invalid_parameter,
182 /* RTC accessors are gone */
183 .ptr = &efi_runtime_services.get_time,
184 .patchto = &efi_get_time,
186 /* Clean up system table */
187 .ptr = &systab.con_in,
190 /* Clean up system table */
191 .ptr = &systab.con_out,
194 /* Clean up system table */
195 .ptr = &systab.std_err,
198 /* Clean up system table */
199 .ptr = &systab.boottime,
202 .ptr = &efi_runtime_services.get_variable,
203 .patchto = &efi_device_error,
205 .ptr = &efi_runtime_services.get_next_variable,
206 .patchto = &efi_device_error,
208 .ptr = &efi_runtime_services.set_variable,
209 .patchto = &efi_device_error,
213 static bool efi_runtime_tobedetached(void *p)
217 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
218 if (efi_runtime_detach_list[i].ptr == p)
224 static void efi_runtime_detach(ulong offset)
227 ulong patchoff = offset - (ulong)gd->relocaddr;
229 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
230 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
231 ulong *p = efi_runtime_detach_list[i].ptr;
232 ulong newaddr = patchto ? (patchto + patchoff) : 0;
234 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
239 /* Relocate EFI runtime to uboot_reloc_base = offset */
240 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
243 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
245 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
246 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
249 debug("%s: Relocating to offset=%lx\n", __func__, offset);
250 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
251 ulong base = CONFIG_SYS_TEXT_BASE;
255 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
257 if ((rel->info & R_MASK) != R_RELATIVE) {
262 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
264 newaddr = *p - lastoff + offset;
267 /* Check if the relocation is inside bounds */
268 if (map && ((newaddr < map->virtual_start) ||
269 newaddr > (map->virtual_start +
270 (map->num_pages << EFI_PAGE_SHIFT)))) {
271 if (!efi_runtime_tobedetached(p))
272 printf("U-Boot EFI: Relocation at %p is out of "
273 "range (%lx)\n", p, newaddr);
277 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
279 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
280 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
287 invalidate_icache_all();
290 static efi_status_t EFIAPI efi_set_virtual_address_map(
291 unsigned long memory_map_size,
292 unsigned long descriptor_size,
293 uint32_t descriptor_version,
294 struct efi_mem_desc *virtmap)
296 ulong runtime_start = (ulong)&__efi_runtime_start &
297 ~(ulong)EFI_PAGE_MASK;
298 int n = memory_map_size / descriptor_size;
301 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
302 descriptor_version, virtmap);
304 /* Rebind mmio pointers */
305 for (i = 0; i < n; i++) {
306 struct efi_mem_desc *map = (void*)virtmap +
307 (descriptor_size * i);
308 struct list_head *lhandle;
309 efi_physical_addr_t map_start = map->physical_start;
310 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
311 efi_physical_addr_t map_end = map_start + map_len;
313 /* Adjust all mmio pointers in this region */
314 list_for_each(lhandle, &efi_runtime_mmio) {
315 struct efi_runtime_mmio_list *lmmio;
317 lmmio = list_entry(lhandle,
318 struct efi_runtime_mmio_list,
320 if ((map_start <= lmmio->paddr) &&
321 (map_end >= lmmio->paddr)) {
322 u64 off = map->virtual_start - map_start;
323 uintptr_t new_addr = lmmio->paddr + off;
324 *lmmio->ptr = (void *)new_addr;
329 /* Move the actual runtime code over */
330 for (i = 0; i < n; i++) {
331 struct efi_mem_desc *map;
333 map = (void*)virtmap + (descriptor_size * i);
334 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
335 ulong new_offset = map->virtual_start -
336 (runtime_start - gd->relocaddr);
338 efi_runtime_relocate(new_offset, map);
339 /* Once we're virtual, we can no longer handle
341 efi_runtime_detach(new_offset);
342 return EFI_EXIT(EFI_SUCCESS);
346 return EFI_EXIT(EFI_INVALID_PARAMETER);
349 efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
351 struct efi_runtime_mmio_list *newmmio;
352 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
353 uint64_t addr = *(uintptr_t *)mmio_ptr;
356 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
358 return EFI_OUT_OF_RESOURCES;
360 newmmio = calloc(1, sizeof(*newmmio));
362 return EFI_OUT_OF_RESOURCES;
363 newmmio->ptr = mmio_ptr;
364 newmmio->paddr = *(uintptr_t *)mmio_ptr;
366 list_add_tail(&newmmio->link, &efi_runtime_mmio);
372 * In the second stage, U-Boot has disappeared. To isolate our runtime code
373 * that at this point still exists from the rest, we put it into a special
378 * This means that we can not rely on any code outside of this file in any
379 * function or variable below this line.
381 * Please keep everything fully self-contained and annotated with
382 * __efi_runtime and __efi_runtime_data markers.
386 * Relocate the EFI runtime stub to a different place. We need to call this
387 * the first time we expose the runtime interface to a user and on set virtual
391 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
393 return EFI_UNSUPPORTED;
396 static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
398 return EFI_DEVICE_ERROR;
401 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
403 return EFI_INVALID_PARAMETER;
406 efi_status_t __efi_runtime EFIAPI efi_update_capsule(
407 struct efi_capsule_header **capsule_header_array,
408 efi_uintn_t capsule_count,
409 u64 scatter_gather_list)
411 return EFI_UNSUPPORTED;
414 efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
415 struct efi_capsule_header **capsule_header_array,
416 efi_uintn_t capsule_count,
417 u64 maximum_capsule_size,
420 return EFI_UNSUPPORTED;
423 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
425 u64 maximum_variable_storage_size,
426 u64 remaining_variable_storage_size,
427 u64 maximum_variable_size)
429 return EFI_UNSUPPORTED;
432 struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
434 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
435 .revision = EFI_RUNTIME_SERVICES_REVISION,
436 .headersize = sizeof(struct efi_table_hdr),
438 .get_time = &efi_get_time_boottime,
439 .set_time = (void *)&efi_device_error,
440 .get_wakeup_time = (void *)&efi_unimplemented,
441 .set_wakeup_time = (void *)&efi_unimplemented,
442 .set_virtual_address_map = &efi_set_virtual_address_map,
443 .convert_pointer = (void *)&efi_invalid_parameter,
444 .get_variable = efi_get_variable,
445 .get_next_variable = efi_get_next_variable,
446 .set_variable = efi_set_variable,
447 .get_next_high_mono_count = (void *)&efi_device_error,
448 .reset_system = &efi_reset_system_boottime,
449 .update_capsule = efi_update_capsule,
450 .query_capsule_caps = efi_query_capsule_caps,
451 .query_variable_info = efi_query_variable_info,