blob: 241090f6b4842639b2911032b1e0a731df27de97 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf50149ea2016-03-04 01:10:01 +01002/*
3 * EFI application runtime services
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Graf50149ea2016-03-04 01:10:01 +01006 */
7
8#include <common.h>
9#include <command.h>
10#include <dm.h>
11#include <efi_loader.h>
12#include <rtc.h>
13#include <asm/global_data.h>
14
15/* For manual relocation support */
16DECLARE_GLOBAL_DATA_PTR;
17
Alexander Graf80a48002016-08-16 21:08:45 +020018struct efi_runtime_mmio_list {
19 struct list_head link;
20 void **ptr;
21 u64 paddr;
22 u64 len;
23};
24
25/* This list contains all runtime available mmio regions */
26LIST_HEAD(efi_runtime_mmio);
27
Alexander Graf3c63db92016-10-14 13:45:30 +020028static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void);
29static efi_status_t __efi_runtime EFIAPI efi_device_error(void);
30static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void);
Alexander Graf50149ea2016-03-04 01:10:01 +010031
32#if defined(CONFIG_ARM64)
33#define R_RELATIVE 1027
34#define R_MASK 0xffffffffULL
35#define IS_RELA 1
36#elif defined(CONFIG_ARM)
37#define R_RELATIVE 23
38#define R_MASK 0xffULL
Simon Glass65e4c0b2016-09-25 15:27:35 -060039#elif defined(CONFIG_X86)
40#include <asm/elf.h>
41#define R_RELATIVE R_386_RELATIVE
42#define R_MASK 0xffULL
Rick Chen6836adb2018-05-28 19:06:37 +080043#elif defined(CONFIG_RISCV)
44#include <elf.h>
45#define R_RELATIVE R_RISCV_RELATIVE
46#define R_MASK 0xffULL
47#define IS_RELA 1
48
49struct dyn_sym {
50 ulong foo1;
51 ulong addr;
52 u32 foo2;
53 u32 foo3;
54};
55#ifdef CONFIG_CPU_RISCV_32
56#define R_ABSOLUTE R_RISCV_32
57#define SYM_INDEX 8
58#else
59#define R_ABSOLUTE R_RISCV_64
60#define SYM_INDEX 32
61#endif
Alexander Graf50149ea2016-03-04 01:10:01 +010062#else
63#error Need to add relocation awareness
64#endif
65
66struct elf_rel {
67 ulong *offset;
68 ulong info;
69};
70
71struct elf_rela {
72 ulong *offset;
73 ulong info;
74 long addend;
75};
76
77/*
78 * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
79 * payload are running concurrently at the same time. In this mode, we can
80 * handle a good number of runtime callbacks
81 */
82
Alexander Graf80a48002016-08-16 21:08:45 +020083static void EFIAPI efi_reset_system_boottime(
84 enum efi_reset_type reset_type,
85 efi_status_t reset_status,
86 unsigned long data_size, void *reset_data)
Alexander Graf50149ea2016-03-04 01:10:01 +010087{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +010088 struct efi_event *evt;
89
Alexander Graf50149ea2016-03-04 01:10:01 +010090 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
91 reset_data);
92
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +010093 /* Notify reset */
94 list_for_each_entry(evt, &efi_events, link) {
95 if (evt->group &&
96 !guidcmp(evt->group,
97 &efi_guid_event_group_reset_system)) {
98 efi_signal_event(evt, false);
99 break;
100 }
101 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100102 switch (reset_type) {
103 case EFI_RESET_COLD:
104 case EFI_RESET_WARM:
Heinrich Schuchardt482fc902018-02-06 22:00:22 +0100105 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf50149ea2016-03-04 01:10:01 +0100106 do_reset(NULL, 0, 0, NULL);
107 break;
108 case EFI_RESET_SHUTDOWN:
109 /* We don't have anything to map this to */
110 break;
111 }
112
Alexander Graf80a48002016-08-16 21:08:45 +0200113 while (1) { }
Alexander Graf50149ea2016-03-04 01:10:01 +0100114}
115
Alexander Graf80a48002016-08-16 21:08:45 +0200116static efi_status_t EFIAPI efi_get_time_boottime(
117 struct efi_time *time,
118 struct efi_time_cap *capabilities)
Alexander Graf50149ea2016-03-04 01:10:01 +0100119{
120#if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
121 struct rtc_time tm;
122 int r;
123 struct udevice *dev;
124
125 EFI_ENTRY("%p %p", time, capabilities);
126
127 r = uclass_get_device(UCLASS_RTC, 0, &dev);
128 if (r)
129 return EFI_EXIT(EFI_DEVICE_ERROR);
130
131 r = dm_rtc_get(dev, &tm);
132 if (r)
133 return EFI_EXIT(EFI_DEVICE_ERROR);
134
135 memset(time, 0, sizeof(*time));
136 time->year = tm.tm_year;
137 time->month = tm.tm_mon;
138 time->day = tm.tm_mday;
139 time->hour = tm.tm_hour;
140 time->minute = tm.tm_min;
141 time->daylight = tm.tm_isdst;
142
143 return EFI_EXIT(EFI_SUCCESS);
144#else
145 return EFI_DEVICE_ERROR;
146#endif
147}
148
Alexander Graf80a48002016-08-16 21:08:45 +0200149/* Boards may override the helpers below to implement RTS functionality */
150
Alexander Graf3c63db92016-10-14 13:45:30 +0200151void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf80a48002016-08-16 21:08:45 +0200152 enum efi_reset_type reset_type,
153 efi_status_t reset_status,
154 unsigned long data_size, void *reset_data)
155{
156 /* Nothing we can do */
157 while (1) { }
158}
159
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100160efi_status_t __weak efi_reset_system_init(void)
Alexander Graf80a48002016-08-16 21:08:45 +0200161{
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100162 return EFI_SUCCESS;
Alexander Graf80a48002016-08-16 21:08:45 +0200163}
164
Alexander Graf3c63db92016-10-14 13:45:30 +0200165efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf80a48002016-08-16 21:08:45 +0200166 struct efi_time *time,
167 struct efi_time_cap *capabilities)
168{
169 /* Nothing we can do */
170 return EFI_DEVICE_ERROR;
171}
172
Heinrich Schuchardt14ad49d2018-03-03 15:29:00 +0100173efi_status_t __weak efi_get_time_init(void)
Alexander Graf80a48002016-08-16 21:08:45 +0200174{
Heinrich Schuchardt14ad49d2018-03-03 15:29:00 +0100175 return EFI_SUCCESS;
Alexander Graf80a48002016-08-16 21:08:45 +0200176}
177
Alexander Graf50149ea2016-03-04 01:10:01 +0100178struct efi_runtime_detach_list_struct {
179 void *ptr;
180 void *patchto;
181};
182
183static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
184 {
185 /* do_reset is gone */
186 .ptr = &efi_runtime_services.reset_system,
Alexander Graf80a48002016-08-16 21:08:45 +0200187 .patchto = efi_reset_system,
Alexander Graf50149ea2016-03-04 01:10:01 +0100188 }, {
189 /* invalidate_*cache_all are gone */
190 .ptr = &efi_runtime_services.set_virtual_address_map,
191 .patchto = &efi_invalid_parameter,
192 }, {
193 /* RTC accessors are gone */
194 .ptr = &efi_runtime_services.get_time,
Alexander Graf80a48002016-08-16 21:08:45 +0200195 .patchto = &efi_get_time,
Alexander Grafae874402016-05-18 00:54:47 +0200196 }, {
197 /* Clean up system table */
198 .ptr = &systab.con_in,
199 .patchto = NULL,
200 }, {
201 /* Clean up system table */
202 .ptr = &systab.con_out,
203 .patchto = NULL,
204 }, {
205 /* Clean up system table */
206 .ptr = &systab.std_err,
207 .patchto = NULL,
208 }, {
209 /* Clean up system table */
210 .ptr = &systab.boottime,
211 .patchto = NULL,
Rob Clarkad644e72017-09-13 18:05:37 -0400212 }, {
213 .ptr = &efi_runtime_services.get_variable,
214 .patchto = &efi_device_error,
215 }, {
216 .ptr = &efi_runtime_services.get_next_variable,
217 .patchto = &efi_device_error,
218 }, {
219 .ptr = &efi_runtime_services.set_variable,
220 .patchto = &efi_device_error,
221 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100222};
223
224static bool efi_runtime_tobedetached(void *p)
225{
226 int i;
227
228 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
229 if (efi_runtime_detach_list[i].ptr == p)
230 return true;
231
232 return false;
233}
234
235static void efi_runtime_detach(ulong offset)
236{
237 int i;
238 ulong patchoff = offset - (ulong)gd->relocaddr;
239
240 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
241 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
242 ulong *p = efi_runtime_detach_list[i].ptr;
243 ulong newaddr = patchto ? (patchto + patchoff) : 0;
244
Alexander Grafedcef3b2016-06-02 11:38:27 +0200245 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100246 *p = newaddr;
247 }
248}
249
250/* Relocate EFI runtime to uboot_reloc_base = offset */
251void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
252{
253#ifdef IS_RELA
254 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
255#else
256 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
257 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
258#endif
259
Alexander Grafedcef3b2016-06-02 11:38:27 +0200260 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf50149ea2016-03-04 01:10:01 +0100261 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
262 ulong base = CONFIG_SYS_TEXT_BASE;
263 ulong *p;
264 ulong newaddr;
265
266 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
267
Rick Chen6836adb2018-05-28 19:06:37 +0800268 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, rel->info, *p, rel->offset);
Alexander Graf50149ea2016-03-04 01:10:01 +0100269
Rick Chen6836adb2018-05-28 19:06:37 +0800270 switch (rel->info & R_MASK) {
271 case R_RELATIVE:
Alexander Graf50149ea2016-03-04 01:10:01 +0100272#ifdef IS_RELA
273 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
274#else
275 newaddr = *p - lastoff + offset;
276#endif
Rick Chen6836adb2018-05-28 19:06:37 +0800277 break;
278#ifdef R_ABSOLUTE
279 case R_ABSOLUTE: {
280 ulong symidx = rel->info >> SYM_INDEX;
281 extern struct dyn_sym __dyn_sym_start[];
282 newaddr = __dyn_sym_start[symidx].addr + offset;
283 break;
284 }
285#endif
286 default:
287 continue;
288 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100289
290 /* Check if the relocation is inside bounds */
291 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt591cf2e2017-09-18 22:11:34 +0200292 newaddr > (map->virtual_start +
293 (map->num_pages << EFI_PAGE_SHIFT)))) {
Alexander Graf50149ea2016-03-04 01:10:01 +0100294 if (!efi_runtime_tobedetached(p))
295 printf("U-Boot EFI: Relocation at %p is out of "
296 "range (%lx)\n", p, newaddr);
297 continue;
298 }
299
Alexander Grafedcef3b2016-06-02 11:38:27 +0200300 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100301 *p = newaddr;
Alexander Graf36c37a82016-04-11 23:20:39 +0200302 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
303 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf50149ea2016-03-04 01:10:01 +0100304 }
305
306#ifndef IS_RELA
307 lastoff = offset;
308#endif
309
310 invalidate_icache_all();
311}
312
313static efi_status_t EFIAPI efi_set_virtual_address_map(
314 unsigned long memory_map_size,
315 unsigned long descriptor_size,
316 uint32_t descriptor_version,
317 struct efi_mem_desc *virtmap)
318{
Heinrich Schuchardt591cf2e2017-09-18 22:11:34 +0200319 ulong runtime_start = (ulong)&__efi_runtime_start &
320 ~(ulong)EFI_PAGE_MASK;
Alexander Graf50149ea2016-03-04 01:10:01 +0100321 int n = memory_map_size / descriptor_size;
322 int i;
323
324 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
325 descriptor_version, virtmap);
326
Alexander Graf80a48002016-08-16 21:08:45 +0200327 /* Rebind mmio pointers */
328 for (i = 0; i < n; i++) {
329 struct efi_mem_desc *map = (void*)virtmap +
330 (descriptor_size * i);
331 struct list_head *lhandle;
332 efi_physical_addr_t map_start = map->physical_start;
333 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
334 efi_physical_addr_t map_end = map_start + map_len;
335
336 /* Adjust all mmio pointers in this region */
337 list_for_each(lhandle, &efi_runtime_mmio) {
338 struct efi_runtime_mmio_list *lmmio;
339
340 lmmio = list_entry(lhandle,
341 struct efi_runtime_mmio_list,
342 link);
343 if ((map_start <= lmmio->paddr) &&
344 (map_end >= lmmio->paddr)) {
345 u64 off = map->virtual_start - map_start;
346 uintptr_t new_addr = lmmio->paddr + off;
347 *lmmio->ptr = (void *)new_addr;
348 }
349 }
350 }
351
352 /* Move the actual runtime code over */
Alexander Graf50149ea2016-03-04 01:10:01 +0100353 for (i = 0; i < n; i++) {
354 struct efi_mem_desc *map;
355
356 map = (void*)virtmap + (descriptor_size * i);
357 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf80a48002016-08-16 21:08:45 +0200358 ulong new_offset = map->virtual_start -
359 (runtime_start - gd->relocaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100360
361 efi_runtime_relocate(new_offset, map);
362 /* Once we're virtual, we can no longer handle
363 complex callbacks */
364 efi_runtime_detach(new_offset);
365 return EFI_EXIT(EFI_SUCCESS);
366 }
367 }
368
369 return EFI_EXIT(EFI_INVALID_PARAMETER);
370}
371
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100372efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf80a48002016-08-16 21:08:45 +0200373{
374 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.deaee4f062017-08-11 21:19:37 +0200375 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf813468c2018-03-15 15:08:16 +0100376 uint64_t addr = *(uintptr_t *)mmio_ptr;
377 uint64_t retaddr;
378
379 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
380 if (retaddr != addr)
381 return EFI_OUT_OF_RESOURCES;
Alexander Graf80a48002016-08-16 21:08:45 +0200382
383 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100384 if (!newmmio)
385 return EFI_OUT_OF_RESOURCES;
Alexander Graf80a48002016-08-16 21:08:45 +0200386 newmmio->ptr = mmio_ptr;
387 newmmio->paddr = *(uintptr_t *)mmio_ptr;
388 newmmio->len = len;
389 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100390
Alexander Graf813468c2018-03-15 15:08:16 +0100391 return EFI_SUCCESS;
Alexander Graf80a48002016-08-16 21:08:45 +0200392}
393
Alexander Graf50149ea2016-03-04 01:10:01 +0100394/*
395 * In the second stage, U-Boot has disappeared. To isolate our runtime code
396 * that at this point still exists from the rest, we put it into a special
397 * section.
398 *
399 * !!WARNING!!
400 *
401 * This means that we can not rely on any code outside of this file in any
402 * function or variable below this line.
403 *
404 * Please keep everything fully self-contained and annotated with
Alexander Graf3c63db92016-10-14 13:45:30 +0200405 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf50149ea2016-03-04 01:10:01 +0100406 */
407
408/*
409 * Relocate the EFI runtime stub to a different place. We need to call this
410 * the first time we expose the runtime interface to a user and on set virtual
411 * address map calls.
412 */
413
Alexander Graf3c63db92016-10-14 13:45:30 +0200414static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100415{
416 return EFI_UNSUPPORTED;
417}
418
Alexander Graf3c63db92016-10-14 13:45:30 +0200419static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100420{
421 return EFI_DEVICE_ERROR;
422}
423
Alexander Graf3c63db92016-10-14 13:45:30 +0200424static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100425{
426 return EFI_INVALID_PARAMETER;
427}
428
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100429efi_status_t __efi_runtime EFIAPI efi_update_capsule(
430 struct efi_capsule_header **capsule_header_array,
431 efi_uintn_t capsule_count,
432 u64 scatter_gather_list)
433{
434 return EFI_UNSUPPORTED;
435}
436
437efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
438 struct efi_capsule_header **capsule_header_array,
439 efi_uintn_t capsule_count,
440 u64 maximum_capsule_size,
441 u32 reset_type)
442{
443 return EFI_UNSUPPORTED;
444}
445
446efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
447 u32 attributes,
448 u64 maximum_variable_storage_size,
449 u64 remaining_variable_storage_size,
450 u64 maximum_variable_size)
451{
452 return EFI_UNSUPPORTED;
453}
454
Alexander Graf3c63db92016-10-14 13:45:30 +0200455struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf50149ea2016-03-04 01:10:01 +0100456 .hdr = {
457 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
458 .revision = EFI_RUNTIME_SERVICES_REVISION,
459 .headersize = sizeof(struct efi_table_hdr),
460 },
Alexander Graf80a48002016-08-16 21:08:45 +0200461 .get_time = &efi_get_time_boottime,
Alexander Graf50149ea2016-03-04 01:10:01 +0100462 .set_time = (void *)&efi_device_error,
463 .get_wakeup_time = (void *)&efi_unimplemented,
464 .set_wakeup_time = (void *)&efi_unimplemented,
465 .set_virtual_address_map = &efi_set_virtual_address_map,
466 .convert_pointer = (void *)&efi_invalid_parameter,
Rob Clarkad644e72017-09-13 18:05:37 -0400467 .get_variable = efi_get_variable,
468 .get_next_variable = efi_get_next_variable,
469 .set_variable = efi_set_variable,
Alexander Graf50149ea2016-03-04 01:10:01 +0100470 .get_next_high_mono_count = (void *)&efi_device_error,
Alexander Graf80a48002016-08-16 21:08:45 +0200471 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100472 .update_capsule = efi_update_capsule,
473 .query_capsule_caps = efi_query_capsule_caps,
474 .query_variable_info = efi_query_variable_info,
Alexander Graf50149ea2016-03-04 01:10:01 +0100475};