blob: bc44e437458eb3794821969342d8834a045185f4 [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>
Alexander Graf50149ea2016-03-04 01:10:01 +010013
14/* For manual relocation support */
15DECLARE_GLOBAL_DATA_PTR;
16
Alexander Graf80a48002016-08-16 21:08:45 +020017struct efi_runtime_mmio_list {
18 struct list_head link;
19 void **ptr;
20 u64 paddr;
21 u64 len;
22};
23
24/* This list contains all runtime available mmio regions */
25LIST_HEAD(efi_runtime_mmio);
26
Alexander Graf3c63db92016-10-14 13:45:30 +020027static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void);
28static efi_status_t __efi_runtime EFIAPI efi_device_error(void);
29static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void);
Alexander Graf50149ea2016-03-04 01:10:01 +010030
Simon Glass2d2b5b22018-06-11 23:26:40 -060031/*
32 * TODO(sjg@chromium.org): These defines and structs should come from the elf
33 * header for each arch (or a generic header) rather than being repeated here.
34 */
Alexander Grafbc028912018-06-18 17:23:08 +020035#if defined(__aarch64__)
Alexander Graf50149ea2016-03-04 01:10:01 +010036#define R_RELATIVE 1027
37#define R_MASK 0xffffffffULL
38#define IS_RELA 1
Alexander Grafbc028912018-06-18 17:23:08 +020039#elif defined(__arm__)
Alexander Graf50149ea2016-03-04 01:10:01 +010040#define R_RELATIVE 23
41#define R_MASK 0xffULL
Alexander Grafbc028912018-06-18 17:23:08 +020042#elif defined(__x86_64__) || defined(__i386__)
Simon Glass65e4c0b2016-09-25 15:27:35 -060043#include <asm/elf.h>
44#define R_RELATIVE R_386_RELATIVE
45#define R_MASK 0xffULL
Alexander Grafbc028912018-06-18 17:23:08 +020046#elif defined(__riscv)
Rick Chen6836adb2018-05-28 19:06:37 +080047#include <elf.h>
48#define R_RELATIVE R_RISCV_RELATIVE
49#define R_MASK 0xffULL
50#define IS_RELA 1
51
52struct dyn_sym {
53 ulong foo1;
54 ulong addr;
55 u32 foo2;
56 u32 foo3;
57};
Alexander Grafbc028912018-06-18 17:23:08 +020058#if (__riscv_xlen == 32)
Rick Chen6836adb2018-05-28 19:06:37 +080059#define R_ABSOLUTE R_RISCV_32
60#define SYM_INDEX 8
Alexander Grafbc028912018-06-18 17:23:08 +020061#elif (__riscv_xlen == 64)
Rick Chen6836adb2018-05-28 19:06:37 +080062#define R_ABSOLUTE R_RISCV_64
63#define SYM_INDEX 32
Alexander Grafbc028912018-06-18 17:23:08 +020064#else
65#error unknown riscv target
Rick Chen6836adb2018-05-28 19:06:37 +080066#endif
Alexander Graf50149ea2016-03-04 01:10:01 +010067#else
68#error Need to add relocation awareness
69#endif
70
71struct elf_rel {
72 ulong *offset;
73 ulong info;
74};
75
76struct elf_rela {
77 ulong *offset;
78 ulong info;
79 long addend;
80};
81
82/*
83 * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
84 * payload are running concurrently at the same time. In this mode, we can
85 * handle a good number of runtime callbacks
86 */
87
Alexander Graf80a48002016-08-16 21:08:45 +020088static void EFIAPI efi_reset_system_boottime(
89 enum efi_reset_type reset_type,
90 efi_status_t reset_status,
91 unsigned long data_size, void *reset_data)
Alexander Graf50149ea2016-03-04 01:10:01 +010092{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +010093 struct efi_event *evt;
94
Alexander Graf50149ea2016-03-04 01:10:01 +010095 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
96 reset_data);
97
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +010098 /* Notify reset */
99 list_for_each_entry(evt, &efi_events, link) {
100 if (evt->group &&
101 !guidcmp(evt->group,
102 &efi_guid_event_group_reset_system)) {
103 efi_signal_event(evt, false);
104 break;
105 }
106 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100107 switch (reset_type) {
108 case EFI_RESET_COLD:
109 case EFI_RESET_WARM:
Heinrich Schuchardt482fc902018-02-06 22:00:22 +0100110 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf50149ea2016-03-04 01:10:01 +0100111 do_reset(NULL, 0, 0, NULL);
112 break;
113 case EFI_RESET_SHUTDOWN:
114 /* We don't have anything to map this to */
115 break;
116 }
117
Alexander Graf80a48002016-08-16 21:08:45 +0200118 while (1) { }
Alexander Graf50149ea2016-03-04 01:10:01 +0100119}
120
Alexander Graf80a48002016-08-16 21:08:45 +0200121static efi_status_t EFIAPI efi_get_time_boottime(
122 struct efi_time *time,
123 struct efi_time_cap *capabilities)
Alexander Graf50149ea2016-03-04 01:10:01 +0100124{
125#if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
126 struct rtc_time tm;
127 int r;
128 struct udevice *dev;
129
130 EFI_ENTRY("%p %p", time, capabilities);
131
132 r = uclass_get_device(UCLASS_RTC, 0, &dev);
133 if (r)
134 return EFI_EXIT(EFI_DEVICE_ERROR);
135
136 r = dm_rtc_get(dev, &tm);
137 if (r)
138 return EFI_EXIT(EFI_DEVICE_ERROR);
139
140 memset(time, 0, sizeof(*time));
141 time->year = tm.tm_year;
142 time->month = tm.tm_mon;
143 time->day = tm.tm_mday;
144 time->hour = tm.tm_hour;
145 time->minute = tm.tm_min;
146 time->daylight = tm.tm_isdst;
147
148 return EFI_EXIT(EFI_SUCCESS);
149#else
150 return EFI_DEVICE_ERROR;
151#endif
152}
153
Alexander Graf80a48002016-08-16 21:08:45 +0200154/* Boards may override the helpers below to implement RTS functionality */
155
Alexander Graf3c63db92016-10-14 13:45:30 +0200156void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf80a48002016-08-16 21:08:45 +0200157 enum efi_reset_type reset_type,
158 efi_status_t reset_status,
159 unsigned long data_size, void *reset_data)
160{
161 /* Nothing we can do */
162 while (1) { }
163}
164
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100165efi_status_t __weak efi_reset_system_init(void)
Alexander Graf80a48002016-08-16 21:08:45 +0200166{
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100167 return EFI_SUCCESS;
Alexander Graf80a48002016-08-16 21:08:45 +0200168}
169
Alexander Graf3c63db92016-10-14 13:45:30 +0200170efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf80a48002016-08-16 21:08:45 +0200171 struct efi_time *time,
172 struct efi_time_cap *capabilities)
173{
174 /* Nothing we can do */
175 return EFI_DEVICE_ERROR;
176}
177
Heinrich Schuchardt14ad49d2018-03-03 15:29:00 +0100178efi_status_t __weak efi_get_time_init(void)
Alexander Graf80a48002016-08-16 21:08:45 +0200179{
Heinrich Schuchardt14ad49d2018-03-03 15:29:00 +0100180 return EFI_SUCCESS;
Alexander Graf80a48002016-08-16 21:08:45 +0200181}
182
Alexander Graf50149ea2016-03-04 01:10:01 +0100183struct efi_runtime_detach_list_struct {
184 void *ptr;
185 void *patchto;
186};
187
188static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
189 {
190 /* do_reset is gone */
191 .ptr = &efi_runtime_services.reset_system,
Alexander Graf80a48002016-08-16 21:08:45 +0200192 .patchto = efi_reset_system,
Alexander Graf50149ea2016-03-04 01:10:01 +0100193 }, {
194 /* invalidate_*cache_all are gone */
195 .ptr = &efi_runtime_services.set_virtual_address_map,
196 .patchto = &efi_invalid_parameter,
197 }, {
198 /* RTC accessors are gone */
199 .ptr = &efi_runtime_services.get_time,
Alexander Graf80a48002016-08-16 21:08:45 +0200200 .patchto = &efi_get_time,
Alexander Grafae874402016-05-18 00:54:47 +0200201 }, {
202 /* Clean up system table */
203 .ptr = &systab.con_in,
204 .patchto = NULL,
205 }, {
206 /* Clean up system table */
207 .ptr = &systab.con_out,
208 .patchto = NULL,
209 }, {
210 /* Clean up system table */
211 .ptr = &systab.std_err,
212 .patchto = NULL,
213 }, {
214 /* Clean up system table */
215 .ptr = &systab.boottime,
216 .patchto = NULL,
Rob Clarkad644e72017-09-13 18:05:37 -0400217 }, {
218 .ptr = &efi_runtime_services.get_variable,
219 .patchto = &efi_device_error,
220 }, {
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200221 .ptr = &efi_runtime_services.get_next_variable_name,
Rob Clarkad644e72017-09-13 18:05:37 -0400222 .patchto = &efi_device_error,
223 }, {
224 .ptr = &efi_runtime_services.set_variable,
225 .patchto = &efi_device_error,
226 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100227};
228
229static bool efi_runtime_tobedetached(void *p)
230{
231 int i;
232
233 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
234 if (efi_runtime_detach_list[i].ptr == p)
235 return true;
236
237 return false;
238}
239
240static void efi_runtime_detach(ulong offset)
241{
242 int i;
243 ulong patchoff = offset - (ulong)gd->relocaddr;
244
245 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
246 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
247 ulong *p = efi_runtime_detach_list[i].ptr;
248 ulong newaddr = patchto ? (patchto + patchoff) : 0;
249
Alexander Grafedcef3b2016-06-02 11:38:27 +0200250 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100251 *p = newaddr;
252 }
253}
254
255/* Relocate EFI runtime to uboot_reloc_base = offset */
256void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
257{
258#ifdef IS_RELA
259 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
260#else
261 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
262 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
263#endif
264
Alexander Grafedcef3b2016-06-02 11:38:27 +0200265 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf50149ea2016-03-04 01:10:01 +0100266 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
267 ulong base = CONFIG_SYS_TEXT_BASE;
268 ulong *p;
269 ulong newaddr;
270
271 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
272
Rick Chen6836adb2018-05-28 19:06:37 +0800273 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, rel->info, *p, rel->offset);
Alexander Graf50149ea2016-03-04 01:10:01 +0100274
Rick Chen6836adb2018-05-28 19:06:37 +0800275 switch (rel->info & R_MASK) {
276 case R_RELATIVE:
Alexander Graf50149ea2016-03-04 01:10:01 +0100277#ifdef IS_RELA
278 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
279#else
280 newaddr = *p - lastoff + offset;
281#endif
Rick Chen6836adb2018-05-28 19:06:37 +0800282 break;
283#ifdef R_ABSOLUTE
284 case R_ABSOLUTE: {
285 ulong symidx = rel->info >> SYM_INDEX;
286 extern struct dyn_sym __dyn_sym_start[];
287 newaddr = __dyn_sym_start[symidx].addr + offset;
288 break;
289 }
290#endif
291 default:
292 continue;
293 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100294
295 /* Check if the relocation is inside bounds */
296 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt591cf2e2017-09-18 22:11:34 +0200297 newaddr > (map->virtual_start +
298 (map->num_pages << EFI_PAGE_SHIFT)))) {
Alexander Graf50149ea2016-03-04 01:10:01 +0100299 if (!efi_runtime_tobedetached(p))
300 printf("U-Boot EFI: Relocation at %p is out of "
301 "range (%lx)\n", p, newaddr);
302 continue;
303 }
304
Alexander Grafedcef3b2016-06-02 11:38:27 +0200305 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100306 *p = newaddr;
Alexander Graf36c37a82016-04-11 23:20:39 +0200307 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
308 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf50149ea2016-03-04 01:10:01 +0100309 }
310
311#ifndef IS_RELA
312 lastoff = offset;
313#endif
314
315 invalidate_icache_all();
316}
317
318static efi_status_t EFIAPI efi_set_virtual_address_map(
319 unsigned long memory_map_size,
320 unsigned long descriptor_size,
321 uint32_t descriptor_version,
322 struct efi_mem_desc *virtmap)
323{
Heinrich Schuchardt591cf2e2017-09-18 22:11:34 +0200324 ulong runtime_start = (ulong)&__efi_runtime_start &
325 ~(ulong)EFI_PAGE_MASK;
Alexander Graf50149ea2016-03-04 01:10:01 +0100326 int n = memory_map_size / descriptor_size;
327 int i;
328
329 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
330 descriptor_version, virtmap);
331
Alexander Graf80a48002016-08-16 21:08:45 +0200332 /* Rebind mmio pointers */
333 for (i = 0; i < n; i++) {
334 struct efi_mem_desc *map = (void*)virtmap +
335 (descriptor_size * i);
336 struct list_head *lhandle;
337 efi_physical_addr_t map_start = map->physical_start;
338 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
339 efi_physical_addr_t map_end = map_start + map_len;
340
341 /* Adjust all mmio pointers in this region */
342 list_for_each(lhandle, &efi_runtime_mmio) {
343 struct efi_runtime_mmio_list *lmmio;
344
345 lmmio = list_entry(lhandle,
346 struct efi_runtime_mmio_list,
347 link);
348 if ((map_start <= lmmio->paddr) &&
349 (map_end >= lmmio->paddr)) {
350 u64 off = map->virtual_start - map_start;
351 uintptr_t new_addr = lmmio->paddr + off;
352 *lmmio->ptr = (void *)new_addr;
353 }
354 }
355 }
356
357 /* Move the actual runtime code over */
Alexander Graf50149ea2016-03-04 01:10:01 +0100358 for (i = 0; i < n; i++) {
359 struct efi_mem_desc *map;
360
361 map = (void*)virtmap + (descriptor_size * i);
362 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf80a48002016-08-16 21:08:45 +0200363 ulong new_offset = map->virtual_start -
364 (runtime_start - gd->relocaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100365
366 efi_runtime_relocate(new_offset, map);
367 /* Once we're virtual, we can no longer handle
368 complex callbacks */
369 efi_runtime_detach(new_offset);
370 return EFI_EXIT(EFI_SUCCESS);
371 }
372 }
373
374 return EFI_EXIT(EFI_INVALID_PARAMETER);
375}
376
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100377efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf80a48002016-08-16 21:08:45 +0200378{
379 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.deaee4f062017-08-11 21:19:37 +0200380 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf813468c2018-03-15 15:08:16 +0100381 uint64_t addr = *(uintptr_t *)mmio_ptr;
382 uint64_t retaddr;
383
384 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
385 if (retaddr != addr)
386 return EFI_OUT_OF_RESOURCES;
Alexander Graf80a48002016-08-16 21:08:45 +0200387
388 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100389 if (!newmmio)
390 return EFI_OUT_OF_RESOURCES;
Alexander Graf80a48002016-08-16 21:08:45 +0200391 newmmio->ptr = mmio_ptr;
392 newmmio->paddr = *(uintptr_t *)mmio_ptr;
393 newmmio->len = len;
394 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100395
Alexander Graf813468c2018-03-15 15:08:16 +0100396 return EFI_SUCCESS;
Alexander Graf80a48002016-08-16 21:08:45 +0200397}
398
Alexander Graf50149ea2016-03-04 01:10:01 +0100399/*
400 * In the second stage, U-Boot has disappeared. To isolate our runtime code
401 * that at this point still exists from the rest, we put it into a special
402 * section.
403 *
404 * !!WARNING!!
405 *
406 * This means that we can not rely on any code outside of this file in any
407 * function or variable below this line.
408 *
409 * Please keep everything fully self-contained and annotated with
Alexander Graf3c63db92016-10-14 13:45:30 +0200410 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf50149ea2016-03-04 01:10:01 +0100411 */
412
413/*
414 * Relocate the EFI runtime stub to a different place. We need to call this
415 * the first time we expose the runtime interface to a user and on set virtual
416 * address map calls.
417 */
418
Alexander Graf3c63db92016-10-14 13:45:30 +0200419static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100420{
421 return EFI_UNSUPPORTED;
422}
423
Alexander Graf3c63db92016-10-14 13:45:30 +0200424static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100425{
426 return EFI_DEVICE_ERROR;
427}
428
Alexander Graf3c63db92016-10-14 13:45:30 +0200429static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100430{
431 return EFI_INVALID_PARAMETER;
432}
433
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100434efi_status_t __efi_runtime EFIAPI efi_update_capsule(
435 struct efi_capsule_header **capsule_header_array,
436 efi_uintn_t capsule_count,
437 u64 scatter_gather_list)
438{
439 return EFI_UNSUPPORTED;
440}
441
442efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
443 struct efi_capsule_header **capsule_header_array,
444 efi_uintn_t capsule_count,
445 u64 maximum_capsule_size,
446 u32 reset_type)
447{
448 return EFI_UNSUPPORTED;
449}
450
451efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
452 u32 attributes,
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200453 u64 *maximum_variable_storage_size,
454 u64 *remaining_variable_storage_size,
455 u64 *maximum_variable_size)
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100456{
457 return EFI_UNSUPPORTED;
458}
459
Alexander Graf3c63db92016-10-14 13:45:30 +0200460struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf50149ea2016-03-04 01:10:01 +0100461 .hdr = {
462 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
463 .revision = EFI_RUNTIME_SERVICES_REVISION,
464 .headersize = sizeof(struct efi_table_hdr),
465 },
Alexander Graf80a48002016-08-16 21:08:45 +0200466 .get_time = &efi_get_time_boottime,
Alexander Graf50149ea2016-03-04 01:10:01 +0100467 .set_time = (void *)&efi_device_error,
468 .get_wakeup_time = (void *)&efi_unimplemented,
469 .set_wakeup_time = (void *)&efi_unimplemented,
470 .set_virtual_address_map = &efi_set_virtual_address_map,
471 .convert_pointer = (void *)&efi_invalid_parameter,
Rob Clarkad644e72017-09-13 18:05:37 -0400472 .get_variable = efi_get_variable,
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200473 .get_next_variable_name = efi_get_next_variable_name,
Rob Clarkad644e72017-09-13 18:05:37 -0400474 .set_variable = efi_set_variable,
Alexander Graf50149ea2016-03-04 01:10:01 +0100475 .get_next_high_mono_count = (void *)&efi_device_error,
Alexander Graf80a48002016-08-16 21:08:45 +0200476 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100477 .update_capsule = efi_update_capsule,
478 .query_capsule_caps = efi_query_capsule_caps,
479 .query_variable_info = efi_query_variable_info,
Alexander Graf50149ea2016-03-04 01:10:01 +0100480};