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_TEXT EFIAPI efi_unimplemented(void);
30 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void);
31 static efi_status_t EFI_RUNTIME_TEXT 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
48 #error Need to add relocation awareness
63 * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
64 * payload are running concurrently at the same time. In this mode, we can
65 * handle a good number of runtime callbacks
68 static void EFIAPI efi_reset_system_boottime(
69 enum efi_reset_type reset_type,
70 efi_status_t reset_status,
71 unsigned long data_size, void *reset_data)
73 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
79 do_reset(NULL, 0, 0, NULL);
81 case EFI_RESET_SHUTDOWN:
82 /* We don't have anything to map this to */
89 static efi_status_t EFIAPI efi_get_time_boottime(
90 struct efi_time *time,
91 struct efi_time_cap *capabilities)
93 #if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
98 EFI_ENTRY("%p %p", time, capabilities);
100 r = uclass_get_device(UCLASS_RTC, 0, &dev);
102 return EFI_EXIT(EFI_DEVICE_ERROR);
104 r = dm_rtc_get(dev, &tm);
106 return EFI_EXIT(EFI_DEVICE_ERROR);
108 memset(time, 0, sizeof(*time));
109 time->year = tm.tm_year;
110 time->month = tm.tm_mon;
111 time->day = tm.tm_mday;
112 time->hour = tm.tm_hour;
113 time->minute = tm.tm_min;
114 time->daylight = tm.tm_isdst;
116 return EFI_EXIT(EFI_SUCCESS);
118 return EFI_DEVICE_ERROR;
122 /* Boards may override the helpers below to implement RTS functionality */
124 void __weak EFI_RUNTIME_TEXT EFIAPI efi_reset_system(
125 enum efi_reset_type reset_type,
126 efi_status_t reset_status,
127 unsigned long data_size, void *reset_data)
129 /* Nothing we can do */
133 void __weak efi_reset_system_init(void)
137 efi_status_t __weak EFI_RUNTIME_TEXT EFIAPI efi_get_time(
138 struct efi_time *time,
139 struct efi_time_cap *capabilities)
141 /* Nothing we can do */
142 return EFI_DEVICE_ERROR;
145 void __weak efi_get_time_init(void)
149 struct efi_runtime_detach_list_struct {
154 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
156 /* do_reset is gone */
157 .ptr = &efi_runtime_services.reset_system,
158 .patchto = efi_reset_system,
160 /* invalidate_*cache_all are gone */
161 .ptr = &efi_runtime_services.set_virtual_address_map,
162 .patchto = &efi_invalid_parameter,
164 /* RTC accessors are gone */
165 .ptr = &efi_runtime_services.get_time,
166 .patchto = &efi_get_time,
168 /* Clean up system table */
169 .ptr = &systab.con_in,
172 /* Clean up system table */
173 .ptr = &systab.con_out,
176 /* Clean up system table */
177 .ptr = &systab.std_err,
180 /* Clean up system table */
181 .ptr = &systab.boottime,
186 static bool efi_runtime_tobedetached(void *p)
190 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
191 if (efi_runtime_detach_list[i].ptr == p)
197 static void efi_runtime_detach(ulong offset)
200 ulong patchoff = offset - (ulong)gd->relocaddr;
202 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
203 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
204 ulong *p = efi_runtime_detach_list[i].ptr;
205 ulong newaddr = patchto ? (patchto + patchoff) : 0;
207 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
212 /* Relocate EFI runtime to uboot_reloc_base = offset */
213 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
216 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
218 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
219 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
222 debug("%s: Relocating to offset=%lx\n", __func__, offset);
223 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
224 ulong base = CONFIG_SYS_TEXT_BASE;
228 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
230 if ((rel->info & R_MASK) != R_RELATIVE) {
235 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
237 newaddr = *p - lastoff + offset;
240 /* Check if the relocation is inside bounds */
241 if (map && ((newaddr < map->virtual_start) ||
242 newaddr > (map->virtual_start + (map->num_pages << 12)))) {
243 if (!efi_runtime_tobedetached(p))
244 printf("U-Boot EFI: Relocation at %p is out of "
245 "range (%lx)\n", p, newaddr);
249 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
251 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
252 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
259 invalidate_icache_all();
262 static efi_status_t EFIAPI efi_set_virtual_address_map(
263 unsigned long memory_map_size,
264 unsigned long descriptor_size,
265 uint32_t descriptor_version,
266 struct efi_mem_desc *virtmap)
268 ulong runtime_start = (ulong)&__efi_runtime_start & ~0xfffULL;
269 int n = memory_map_size / descriptor_size;
272 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
273 descriptor_version, virtmap);
275 /* Rebind mmio pointers */
276 for (i = 0; i < n; i++) {
277 struct efi_mem_desc *map = (void*)virtmap +
278 (descriptor_size * i);
279 struct list_head *lhandle;
280 efi_physical_addr_t map_start = map->physical_start;
281 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
282 efi_physical_addr_t map_end = map_start + map_len;
284 /* Adjust all mmio pointers in this region */
285 list_for_each(lhandle, &efi_runtime_mmio) {
286 struct efi_runtime_mmio_list *lmmio;
288 lmmio = list_entry(lhandle,
289 struct efi_runtime_mmio_list,
291 if ((map_start <= lmmio->paddr) &&
292 (map_end >= lmmio->paddr)) {
293 u64 off = map->virtual_start - map_start;
294 uintptr_t new_addr = lmmio->paddr + off;
295 *lmmio->ptr = (void *)new_addr;
300 /* Move the actual runtime code over */
301 for (i = 0; i < n; i++) {
302 struct efi_mem_desc *map;
304 map = (void*)virtmap + (descriptor_size * i);
305 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
306 ulong new_offset = map->virtual_start -
307 (runtime_start - gd->relocaddr);
309 efi_runtime_relocate(new_offset, map);
310 /* Once we're virtual, we can no longer handle
312 efi_runtime_detach(new_offset);
313 return EFI_EXIT(EFI_SUCCESS);
317 return EFI_EXIT(EFI_INVALID_PARAMETER);
320 void efi_add_runtime_mmio(void *mmio_ptr, u64 len)
322 struct efi_runtime_mmio_list *newmmio;
324 u64 pages = (len + EFI_PAGE_SIZE - 1) >> EFI_PAGE_SHIFT;
325 efi_add_memory_map(*(uintptr_t *)mmio_ptr, pages, EFI_MMAP_IO, false);
327 newmmio = calloc(1, sizeof(*newmmio));
328 newmmio->ptr = mmio_ptr;
329 newmmio->paddr = *(uintptr_t *)mmio_ptr;
331 list_add_tail(&newmmio->link, &efi_runtime_mmio);
335 * In the second stage, U-Boot has disappeared. To isolate our runtime code
336 * that at this point still exists from the rest, we put it into a special
341 * This means that we can not rely on any code outside of this file in any
342 * function or variable below this line.
344 * Please keep everything fully self-contained and annotated with
345 * EFI_RUNTIME_TEXT and EFI_RUNTIME_DATA markers.
349 * Relocate the EFI runtime stub to a different place. We need to call this
350 * the first time we expose the runtime interface to a user and on set virtual
354 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_unimplemented(void)
356 return EFI_UNSUPPORTED;
359 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void)
361 return EFI_DEVICE_ERROR;
364 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_invalid_parameter(void)
366 return EFI_INVALID_PARAMETER;
369 struct efi_runtime_services EFI_RUNTIME_DATA efi_runtime_services = {
371 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
372 .revision = EFI_RUNTIME_SERVICES_REVISION,
373 .headersize = sizeof(struct efi_table_hdr),
375 .get_time = &efi_get_time_boottime,
376 .set_time = (void *)&efi_device_error,
377 .get_wakeup_time = (void *)&efi_unimplemented,
378 .set_wakeup_time = (void *)&efi_unimplemented,
379 .set_virtual_address_map = &efi_set_virtual_address_map,
380 .convert_pointer = (void *)&efi_invalid_parameter,
381 .get_variable = (void *)&efi_device_error,
382 .get_next_variable = (void *)&efi_device_error,
383 .set_variable = (void *)&efi_device_error,
384 .get_next_high_mono_count = (void *)&efi_device_error,
385 .reset_system = &efi_reset_system_boottime,