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 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_unimplemented(void);
20 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void);
21 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_invalid_parameter(void);
23 #if defined(CONFIG_ARM64)
24 #define R_RELATIVE 1027
25 #define R_MASK 0xffffffffULL
27 #elif defined(CONFIG_ARM)
29 #define R_MASK 0xffULL
31 #error Need to add relocation awareness
46 * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
47 * payload are running concurrently at the same time. In this mode, we can
48 * handle a good number of runtime callbacks
51 static void EFIAPI efi_reset_system(enum efi_reset_type reset_type,
52 efi_status_t reset_status,
53 unsigned long data_size, void *reset_data)
55 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
61 do_reset(NULL, 0, 0, NULL);
63 case EFI_RESET_SHUTDOWN:
64 /* We don't have anything to map this to */
68 EFI_EXIT(EFI_SUCCESS);
71 static efi_status_t EFIAPI efi_get_time(struct efi_time *time,
72 struct efi_time_cap *capabilities)
74 #if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
79 EFI_ENTRY("%p %p", time, capabilities);
81 r = uclass_get_device(UCLASS_RTC, 0, &dev);
83 return EFI_EXIT(EFI_DEVICE_ERROR);
85 r = dm_rtc_get(dev, &tm);
87 return EFI_EXIT(EFI_DEVICE_ERROR);
89 memset(time, 0, sizeof(*time));
90 time->year = tm.tm_year;
91 time->month = tm.tm_mon;
92 time->day = tm.tm_mday;
93 time->hour = tm.tm_hour;
94 time->minute = tm.tm_min;
95 time->daylight = tm.tm_isdst;
97 return EFI_EXIT(EFI_SUCCESS);
99 return EFI_DEVICE_ERROR;
103 struct efi_runtime_detach_list_struct {
108 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
110 /* do_reset is gone */
111 .ptr = &efi_runtime_services.reset_system,
114 /* invalidate_*cache_all are gone */
115 .ptr = &efi_runtime_services.set_virtual_address_map,
116 .patchto = &efi_invalid_parameter,
118 /* RTC accessors are gone */
119 .ptr = &efi_runtime_services.get_time,
120 .patchto = &efi_device_error,
124 static bool efi_runtime_tobedetached(void *p)
128 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
129 if (efi_runtime_detach_list[i].ptr == p)
135 static void efi_runtime_detach(ulong offset)
138 ulong patchoff = offset - (ulong)gd->relocaddr;
140 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
141 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
142 ulong *p = efi_runtime_detach_list[i].ptr;
143 ulong newaddr = patchto ? (patchto + patchoff) : 0;
146 printf("%s: Setting %p to %lx\n", __func__, p, newaddr);
152 /* Relocate EFI runtime to uboot_reloc_base = offset */
153 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
156 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
158 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
159 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
163 printf("%s: Relocating to offset=%lx\n", __func__, offset);
166 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
167 ulong base = CONFIG_SYS_TEXT_BASE;
171 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
173 if ((rel->info & R_MASK) != R_RELATIVE) {
178 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
180 newaddr = *p - lastoff + offset;
183 /* Check if the relocation is inside bounds */
184 if (map && ((newaddr < map->virtual_start) ||
185 newaddr > (map->virtual_start + (map->num_pages << 12)))) {
186 if (!efi_runtime_tobedetached(p))
187 printf("U-Boot EFI: Relocation at %p is out of "
188 "range (%lx)\n", p, newaddr);
193 printf("%s: Setting %p to %lx\n", __func__, p, newaddr);
197 flush_dcache_range((ulong)p, (ulong)&p[1]);
204 invalidate_icache_all();
207 static efi_status_t EFIAPI efi_set_virtual_address_map(
208 unsigned long memory_map_size,
209 unsigned long descriptor_size,
210 uint32_t descriptor_version,
211 struct efi_mem_desc *virtmap)
213 ulong runtime_start = (ulong)&__efi_runtime_start & ~0xfffULL;
214 int n = memory_map_size / descriptor_size;
217 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
218 descriptor_version, virtmap);
220 for (i = 0; i < n; i++) {
221 struct efi_mem_desc *map;
223 map = (void*)virtmap + (descriptor_size * i);
224 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
225 ulong new_offset = map->virtual_start - (runtime_start - gd->relocaddr);
227 efi_runtime_relocate(new_offset, map);
228 /* Once we're virtual, we can no longer handle
230 efi_runtime_detach(new_offset);
231 return EFI_EXIT(EFI_SUCCESS);
235 return EFI_EXIT(EFI_INVALID_PARAMETER);
239 * In the second stage, U-Boot has disappeared. To isolate our runtime code
240 * that at this point still exists from the rest, we put it into a special
245 * This means that we can not rely on any code outside of this file in any
246 * function or variable below this line.
248 * Please keep everything fully self-contained and annotated with
249 * EFI_RUNTIME_TEXT and EFI_RUNTIME_DATA markers.
253 * Relocate the EFI runtime stub to a different place. We need to call this
254 * the first time we expose the runtime interface to a user and on set virtual
258 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_unimplemented(void)
260 return EFI_UNSUPPORTED;
263 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void)
265 return EFI_DEVICE_ERROR;
268 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_invalid_parameter(void)
270 return EFI_INVALID_PARAMETER;
273 struct efi_runtime_services EFI_RUNTIME_DATA efi_runtime_services = {
275 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
276 .revision = EFI_RUNTIME_SERVICES_REVISION,
277 .headersize = sizeof(struct efi_table_hdr),
279 .get_time = &efi_get_time,
280 .set_time = (void *)&efi_device_error,
281 .get_wakeup_time = (void *)&efi_unimplemented,
282 .set_wakeup_time = (void *)&efi_unimplemented,
283 .set_virtual_address_map = &efi_set_virtual_address_map,
284 .convert_pointer = (void *)&efi_invalid_parameter,
285 .get_variable = (void *)&efi_device_error,
286 .get_next_variable = (void *)&efi_device_error,
287 .set_variable = (void *)&efi_device_error,
288 .get_next_high_mono_count = (void *)&efi_device_error,
289 .reset_system = &efi_reset_system,