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 #ifdef CONFIG_SYS_CACHELINE_SIZE
24 #define EFI_CACHELINE_SIZE CONFIG_SYS_CACHELINE_SIZE
26 /* Just use the greatest cache flush alignment requirement I'm aware of */
27 #define EFI_CACHELINE_SIZE 128
30 #if defined(CONFIG_ARM64)
31 #define R_RELATIVE 1027
32 #define R_MASK 0xffffffffULL
34 #elif defined(CONFIG_ARM)
36 #define R_MASK 0xffULL
38 #error Need to add relocation awareness
53 * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
54 * payload are running concurrently at the same time. In this mode, we can
55 * handle a good number of runtime callbacks
58 static void EFIAPI efi_reset_system(enum efi_reset_type reset_type,
59 efi_status_t reset_status,
60 unsigned long data_size, void *reset_data)
62 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
68 do_reset(NULL, 0, 0, NULL);
70 case EFI_RESET_SHUTDOWN:
71 /* We don't have anything to map this to */
75 EFI_EXIT(EFI_SUCCESS);
78 static efi_status_t EFIAPI efi_get_time(struct efi_time *time,
79 struct efi_time_cap *capabilities)
81 #if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
86 EFI_ENTRY("%p %p", time, capabilities);
88 r = uclass_get_device(UCLASS_RTC, 0, &dev);
90 return EFI_EXIT(EFI_DEVICE_ERROR);
92 r = dm_rtc_get(dev, &tm);
94 return EFI_EXIT(EFI_DEVICE_ERROR);
96 memset(time, 0, sizeof(*time));
97 time->year = tm.tm_year;
98 time->month = tm.tm_mon;
99 time->day = tm.tm_mday;
100 time->hour = tm.tm_hour;
101 time->minute = tm.tm_min;
102 time->daylight = tm.tm_isdst;
104 return EFI_EXIT(EFI_SUCCESS);
106 return EFI_DEVICE_ERROR;
110 struct efi_runtime_detach_list_struct {
115 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
117 /* do_reset is gone */
118 .ptr = &efi_runtime_services.reset_system,
121 /* invalidate_*cache_all are gone */
122 .ptr = &efi_runtime_services.set_virtual_address_map,
123 .patchto = &efi_invalid_parameter,
125 /* RTC accessors are gone */
126 .ptr = &efi_runtime_services.get_time,
127 .patchto = &efi_device_error,
129 /* Clean up system table */
130 .ptr = &systab.con_in,
133 /* Clean up system table */
134 .ptr = &systab.con_out,
137 /* Clean up system table */
138 .ptr = &systab.std_err,
141 /* Clean up system table */
142 .ptr = &systab.boottime,
147 static bool efi_runtime_tobedetached(void *p)
151 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
152 if (efi_runtime_detach_list[i].ptr == p)
158 static void efi_runtime_detach(ulong offset)
161 ulong patchoff = offset - (ulong)gd->relocaddr;
163 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
164 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
165 ulong *p = efi_runtime_detach_list[i].ptr;
166 ulong newaddr = patchto ? (patchto + patchoff) : 0;
168 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
173 /* Relocate EFI runtime to uboot_reloc_base = offset */
174 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
177 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
179 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
180 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
183 debug("%s: Relocating to offset=%lx\n", __func__, offset);
184 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
185 ulong base = CONFIG_SYS_TEXT_BASE;
189 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
191 if ((rel->info & R_MASK) != R_RELATIVE) {
196 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
198 newaddr = *p - lastoff + offset;
201 /* Check if the relocation is inside bounds */
202 if (map && ((newaddr < map->virtual_start) ||
203 newaddr > (map->virtual_start + (map->num_pages << 12)))) {
204 if (!efi_runtime_tobedetached(p))
205 printf("U-Boot EFI: Relocation at %p is out of "
206 "range (%lx)\n", p, newaddr);
210 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
212 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
213 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
220 invalidate_icache_all();
223 static efi_status_t EFIAPI efi_set_virtual_address_map(
224 unsigned long memory_map_size,
225 unsigned long descriptor_size,
226 uint32_t descriptor_version,
227 struct efi_mem_desc *virtmap)
229 ulong runtime_start = (ulong)&__efi_runtime_start & ~0xfffULL;
230 int n = memory_map_size / descriptor_size;
233 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
234 descriptor_version, virtmap);
236 for (i = 0; i < n; i++) {
237 struct efi_mem_desc *map;
239 map = (void*)virtmap + (descriptor_size * i);
240 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
241 ulong new_offset = map->virtual_start - (runtime_start - gd->relocaddr);
243 efi_runtime_relocate(new_offset, map);
244 /* Once we're virtual, we can no longer handle
246 efi_runtime_detach(new_offset);
247 return EFI_EXIT(EFI_SUCCESS);
251 return EFI_EXIT(EFI_INVALID_PARAMETER);
255 * In the second stage, U-Boot has disappeared. To isolate our runtime code
256 * that at this point still exists from the rest, we put it into a special
261 * This means that we can not rely on any code outside of this file in any
262 * function or variable below this line.
264 * Please keep everything fully self-contained and annotated with
265 * EFI_RUNTIME_TEXT and EFI_RUNTIME_DATA markers.
269 * Relocate the EFI runtime stub to a different place. We need to call this
270 * the first time we expose the runtime interface to a user and on set virtual
274 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_unimplemented(void)
276 return EFI_UNSUPPORTED;
279 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void)
281 return EFI_DEVICE_ERROR;
284 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_invalid_parameter(void)
286 return EFI_INVALID_PARAMETER;
289 struct efi_runtime_services EFI_RUNTIME_DATA efi_runtime_services = {
291 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
292 .revision = EFI_RUNTIME_SERVICES_REVISION,
293 .headersize = sizeof(struct efi_table_hdr),
295 .get_time = &efi_get_time,
296 .set_time = (void *)&efi_device_error,
297 .get_wakeup_time = (void *)&efi_unimplemented,
298 .set_wakeup_time = (void *)&efi_unimplemented,
299 .set_virtual_address_map = &efi_set_virtual_address_map,
300 .convert_pointer = (void *)&efi_invalid_parameter,
301 .get_variable = (void *)&efi_device_error,
302 .get_next_variable = (void *)&efi_device_error,
303 .set_variable = (void *)&efi_device_error,
304 .get_next_high_mono_count = (void *)&efi_device_error,
305 .reset_system = &efi_reset_system,