blob: dd3ff8ad2360305f69dc1119b276e0fb9b7b2e35 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf0bd425a2016-03-04 01:10:01 +01002/*
3 * EFI application runtime services
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Graf0bd425a2016-03-04 01:10:01 +01006 */
7
8#include <common.h>
9#include <command.h>
10#include <dm.h>
Alexander Graf46c15142018-06-18 17:23:11 +020011#include <elf.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010012#include <efi_loader.h>
13#include <rtc.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010014
15/* For manual relocation support */
16DECLARE_GLOBAL_DATA_PTR;
17
Alexander Graf77256092016-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 Graf393dd912016-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 Graf0bd425a2016-03-04 01:10:01 +010031
Simon Glassfff89d42018-06-11 23:26:40 -060032/*
33 * TODO(sjg@chromium.org): These defines and structs should come from the elf
34 * header for each arch (or a generic header) rather than being repeated here.
35 */
Alexander Graf0fe51922018-06-18 17:23:08 +020036#if defined(__aarch64__)
Alexander Graf46c15142018-06-18 17:23:11 +020037#define R_RELATIVE R_AARCH64_RELATIVE
Alexander Graf0bd425a2016-03-04 01:10:01 +010038#define R_MASK 0xffffffffULL
39#define IS_RELA 1
Alexander Graf0fe51922018-06-18 17:23:08 +020040#elif defined(__arm__)
Alexander Graf46c15142018-06-18 17:23:11 +020041#define R_RELATIVE R_ARM_RELATIVE
Alexander Graf0bd425a2016-03-04 01:10:01 +010042#define R_MASK 0xffULL
Alexander Graf0fe51922018-06-18 17:23:08 +020043#elif defined(__x86_64__) || defined(__i386__)
Simon Glasscdfe6962016-09-25 15:27:35 -060044#define R_RELATIVE R_386_RELATIVE
45#define R_MASK 0xffULL
Alexander Graf0fe51922018-06-18 17:23:08 +020046#elif defined(__riscv)
Rick Chen9677a372018-05-28 19:06:37 +080047#define R_RELATIVE R_RISCV_RELATIVE
48#define R_MASK 0xffULL
49#define IS_RELA 1
50
51struct dyn_sym {
52 ulong foo1;
53 ulong addr;
54 u32 foo2;
55 u32 foo3;
56};
Alexander Graf0fe51922018-06-18 17:23:08 +020057#if (__riscv_xlen == 32)
Rick Chen9677a372018-05-28 19:06:37 +080058#define R_ABSOLUTE R_RISCV_32
59#define SYM_INDEX 8
Alexander Graf0fe51922018-06-18 17:23:08 +020060#elif (__riscv_xlen == 64)
Rick Chen9677a372018-05-28 19:06:37 +080061#define R_ABSOLUTE R_RISCV_64
62#define SYM_INDEX 32
Alexander Graf0fe51922018-06-18 17:23:08 +020063#else
64#error unknown riscv target
Rick Chen9677a372018-05-28 19:06:37 +080065#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +010066#else
67#error Need to add relocation awareness
68#endif
69
70struct elf_rel {
71 ulong *offset;
72 ulong info;
73};
74
75struct elf_rela {
76 ulong *offset;
77 ulong info;
78 long addend;
79};
80
81/*
82 * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
83 * payload are running concurrently at the same time. In this mode, we can
84 * handle a good number of runtime callbacks
85 */
86
Alexander Graf77256092016-08-16 21:08:45 +020087static void EFIAPI efi_reset_system_boottime(
88 enum efi_reset_type reset_type,
89 efi_status_t reset_status,
90 unsigned long data_size, void *reset_data)
Alexander Graf0bd425a2016-03-04 01:10:01 +010091{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +010092 struct efi_event *evt;
93
Alexander Graf0bd425a2016-03-04 01:10:01 +010094 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
95 reset_data);
96
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +010097 /* Notify reset */
98 list_for_each_entry(evt, &efi_events, link) {
99 if (evt->group &&
100 !guidcmp(evt->group,
101 &efi_guid_event_group_reset_system)) {
102 efi_signal_event(evt, false);
103 break;
104 }
105 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100106 switch (reset_type) {
107 case EFI_RESET_COLD:
108 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +0100109 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100110 do_reset(NULL, 0, 0, NULL);
111 break;
112 case EFI_RESET_SHUTDOWN:
113 /* We don't have anything to map this to */
114 break;
115 }
116
Alexander Graf77256092016-08-16 21:08:45 +0200117 while (1) { }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100118}
119
Alexander Graf77256092016-08-16 21:08:45 +0200120static efi_status_t EFIAPI efi_get_time_boottime(
121 struct efi_time *time,
122 struct efi_time_cap *capabilities)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100123{
124#if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
125 struct rtc_time tm;
126 int r;
127 struct udevice *dev;
128
129 EFI_ENTRY("%p %p", time, capabilities);
130
131 r = uclass_get_device(UCLASS_RTC, 0, &dev);
132 if (r)
133 return EFI_EXIT(EFI_DEVICE_ERROR);
134
135 r = dm_rtc_get(dev, &tm);
136 if (r)
137 return EFI_EXIT(EFI_DEVICE_ERROR);
138
139 memset(time, 0, sizeof(*time));
140 time->year = tm.tm_year;
141 time->month = tm.tm_mon;
142 time->day = tm.tm_mday;
143 time->hour = tm.tm_hour;
144 time->minute = tm.tm_min;
145 time->daylight = tm.tm_isdst;
146
147 return EFI_EXIT(EFI_SUCCESS);
148#else
149 return EFI_DEVICE_ERROR;
150#endif
151}
152
Alexander Graf77256092016-08-16 21:08:45 +0200153/* Boards may override the helpers below to implement RTS functionality */
154
Alexander Graf393dd912016-10-14 13:45:30 +0200155void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200156 enum efi_reset_type reset_type,
157 efi_status_t reset_status,
158 unsigned long data_size, void *reset_data)
159{
160 /* Nothing we can do */
161 while (1) { }
162}
163
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100164efi_status_t __weak efi_reset_system_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200165{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100166 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200167}
168
Alexander Graf393dd912016-10-14 13:45:30 +0200169efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200170 struct efi_time *time,
171 struct efi_time_cap *capabilities)
172{
173 /* Nothing we can do */
174 return EFI_DEVICE_ERROR;
175}
176
Heinrich Schuchardt096db012018-03-03 15:29:00 +0100177efi_status_t __weak efi_get_time_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200178{
Heinrich Schuchardt096db012018-03-03 15:29:00 +0100179 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200180}
181
Alexander Graf0bd425a2016-03-04 01:10:01 +0100182struct efi_runtime_detach_list_struct {
183 void *ptr;
184 void *patchto;
185};
186
187static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
188 {
189 /* do_reset is gone */
190 .ptr = &efi_runtime_services.reset_system,
Alexander Graf77256092016-08-16 21:08:45 +0200191 .patchto = efi_reset_system,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100192 }, {
193 /* invalidate_*cache_all are gone */
194 .ptr = &efi_runtime_services.set_virtual_address_map,
195 .patchto = &efi_invalid_parameter,
196 }, {
197 /* RTC accessors are gone */
198 .ptr = &efi_runtime_services.get_time,
Alexander Graf77256092016-08-16 21:08:45 +0200199 .patchto = &efi_get_time,
Alexander Grafc59d3602016-05-18 00:54:47 +0200200 }, {
201 /* Clean up system table */
202 .ptr = &systab.con_in,
203 .patchto = NULL,
204 }, {
205 /* Clean up system table */
206 .ptr = &systab.con_out,
207 .patchto = NULL,
208 }, {
209 /* Clean up system table */
210 .ptr = &systab.std_err,
211 .patchto = NULL,
212 }, {
213 /* Clean up system table */
214 .ptr = &systab.boottime,
215 .patchto = NULL,
Rob Clark15f3d742017-09-13 18:05:37 -0400216 }, {
217 .ptr = &efi_runtime_services.get_variable,
218 .patchto = &efi_device_error,
219 }, {
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200220 .ptr = &efi_runtime_services.get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400221 .patchto = &efi_device_error,
222 }, {
223 .ptr = &efi_runtime_services.set_variable,
224 .patchto = &efi_device_error,
225 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100226};
227
228static bool efi_runtime_tobedetached(void *p)
229{
230 int i;
231
232 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
233 if (efi_runtime_detach_list[i].ptr == p)
234 return true;
235
236 return false;
237}
238
239static void efi_runtime_detach(ulong offset)
240{
241 int i;
242 ulong patchoff = offset - (ulong)gd->relocaddr;
243
244 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
245 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
246 ulong *p = efi_runtime_detach_list[i].ptr;
247 ulong newaddr = patchto ? (patchto + patchoff) : 0;
248
Alexander Graf62a67482016-06-02 11:38:27 +0200249 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100250 *p = newaddr;
251 }
252}
253
254/* Relocate EFI runtime to uboot_reloc_base = offset */
255void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
256{
257#ifdef IS_RELA
258 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
259#else
260 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
261 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
262#endif
263
Alexander Graf62a67482016-06-02 11:38:27 +0200264 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100265 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
266 ulong base = CONFIG_SYS_TEXT_BASE;
267 ulong *p;
268 ulong newaddr;
269
270 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
271
Rick Chen9677a372018-05-28 19:06:37 +0800272 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, rel->info, *p, rel->offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100273
Rick Chen9677a372018-05-28 19:06:37 +0800274 switch (rel->info & R_MASK) {
275 case R_RELATIVE:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100276#ifdef IS_RELA
277 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
278#else
279 newaddr = *p - lastoff + offset;
280#endif
Rick Chen9677a372018-05-28 19:06:37 +0800281 break;
282#ifdef R_ABSOLUTE
283 case R_ABSOLUTE: {
284 ulong symidx = rel->info >> SYM_INDEX;
285 extern struct dyn_sym __dyn_sym_start[];
286 newaddr = __dyn_sym_start[symidx].addr + offset;
287 break;
288 }
289#endif
290 default:
291 continue;
292 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100293
294 /* Check if the relocation is inside bounds */
295 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200296 newaddr > (map->virtual_start +
297 (map->num_pages << EFI_PAGE_SHIFT)))) {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100298 if (!efi_runtime_tobedetached(p))
299 printf("U-Boot EFI: Relocation at %p is out of "
300 "range (%lx)\n", p, newaddr);
301 continue;
302 }
303
Alexander Graf62a67482016-06-02 11:38:27 +0200304 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100305 *p = newaddr;
Alexander Grafe5702322016-04-11 23:20:39 +0200306 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
307 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf0bd425a2016-03-04 01:10:01 +0100308 }
309
310#ifndef IS_RELA
311 lastoff = offset;
312#endif
313
314 invalidate_icache_all();
315}
316
317static efi_status_t EFIAPI efi_set_virtual_address_map(
318 unsigned long memory_map_size,
319 unsigned long descriptor_size,
320 uint32_t descriptor_version,
321 struct efi_mem_desc *virtmap)
322{
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200323 ulong runtime_start = (ulong)&__efi_runtime_start &
324 ~(ulong)EFI_PAGE_MASK;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100325 int n = memory_map_size / descriptor_size;
326 int i;
327
328 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
329 descriptor_version, virtmap);
330
Alexander Graf77256092016-08-16 21:08:45 +0200331 /* Rebind mmio pointers */
332 for (i = 0; i < n; i++) {
333 struct efi_mem_desc *map = (void*)virtmap +
334 (descriptor_size * i);
335 struct list_head *lhandle;
336 efi_physical_addr_t map_start = map->physical_start;
337 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
338 efi_physical_addr_t map_end = map_start + map_len;
339
340 /* Adjust all mmio pointers in this region */
341 list_for_each(lhandle, &efi_runtime_mmio) {
342 struct efi_runtime_mmio_list *lmmio;
343
344 lmmio = list_entry(lhandle,
345 struct efi_runtime_mmio_list,
346 link);
347 if ((map_start <= lmmio->paddr) &&
348 (map_end >= lmmio->paddr)) {
349 u64 off = map->virtual_start - map_start;
350 uintptr_t new_addr = lmmio->paddr + off;
351 *lmmio->ptr = (void *)new_addr;
352 }
353 }
354 }
355
356 /* Move the actual runtime code over */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100357 for (i = 0; i < n; i++) {
358 struct efi_mem_desc *map;
359
360 map = (void*)virtmap + (descriptor_size * i);
361 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf77256092016-08-16 21:08:45 +0200362 ulong new_offset = map->virtual_start -
363 (runtime_start - gd->relocaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100364
365 efi_runtime_relocate(new_offset, map);
366 /* Once we're virtual, we can no longer handle
367 complex callbacks */
368 efi_runtime_detach(new_offset);
369 return EFI_EXIT(EFI_SUCCESS);
370 }
371 }
372
373 return EFI_EXIT(EFI_INVALID_PARAMETER);
374}
375
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100376efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf77256092016-08-16 21:08:45 +0200377{
378 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.de93fcc7f2017-08-11 21:19:37 +0200379 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf33a83a32018-03-15 15:08:16 +0100380 uint64_t addr = *(uintptr_t *)mmio_ptr;
381 uint64_t retaddr;
382
383 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
384 if (retaddr != addr)
385 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200386
387 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100388 if (!newmmio)
389 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200390 newmmio->ptr = mmio_ptr;
391 newmmio->paddr = *(uintptr_t *)mmio_ptr;
392 newmmio->len = len;
393 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100394
Alexander Graf33a83a32018-03-15 15:08:16 +0100395 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200396}
397
Alexander Graf0bd425a2016-03-04 01:10:01 +0100398/*
399 * In the second stage, U-Boot has disappeared. To isolate our runtime code
400 * that at this point still exists from the rest, we put it into a special
401 * section.
402 *
403 * !!WARNING!!
404 *
405 * This means that we can not rely on any code outside of this file in any
406 * function or variable below this line.
407 *
408 * Please keep everything fully self-contained and annotated with
Alexander Graf393dd912016-10-14 13:45:30 +0200409 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf0bd425a2016-03-04 01:10:01 +0100410 */
411
412/*
413 * Relocate the EFI runtime stub to a different place. We need to call this
414 * the first time we expose the runtime interface to a user and on set virtual
415 * address map calls.
416 */
417
Alexander Graf393dd912016-10-14 13:45:30 +0200418static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100419{
420 return EFI_UNSUPPORTED;
421}
422
Alexander Graf393dd912016-10-14 13:45:30 +0200423static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100424{
425 return EFI_DEVICE_ERROR;
426}
427
Alexander Graf393dd912016-10-14 13:45:30 +0200428static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100429{
430 return EFI_INVALID_PARAMETER;
431}
432
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100433efi_status_t __efi_runtime EFIAPI efi_update_capsule(
434 struct efi_capsule_header **capsule_header_array,
435 efi_uintn_t capsule_count,
436 u64 scatter_gather_list)
437{
438 return EFI_UNSUPPORTED;
439}
440
441efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
442 struct efi_capsule_header **capsule_header_array,
443 efi_uintn_t capsule_count,
444 u64 maximum_capsule_size,
445 u32 reset_type)
446{
447 return EFI_UNSUPPORTED;
448}
449
450efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
451 u32 attributes,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200452 u64 *maximum_variable_storage_size,
453 u64 *remaining_variable_storage_size,
454 u64 *maximum_variable_size)
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100455{
456 return EFI_UNSUPPORTED;
457}
458
Alexander Graf393dd912016-10-14 13:45:30 +0200459struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100460 .hdr = {
461 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
462 .revision = EFI_RUNTIME_SERVICES_REVISION,
463 .headersize = sizeof(struct efi_table_hdr),
464 },
Alexander Graf77256092016-08-16 21:08:45 +0200465 .get_time = &efi_get_time_boottime,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100466 .set_time = (void *)&efi_device_error,
467 .get_wakeup_time = (void *)&efi_unimplemented,
468 .set_wakeup_time = (void *)&efi_unimplemented,
469 .set_virtual_address_map = &efi_set_virtual_address_map,
470 .convert_pointer = (void *)&efi_invalid_parameter,
Rob Clark15f3d742017-09-13 18:05:37 -0400471 .get_variable = efi_get_variable,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200472 .get_next_variable_name = efi_get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400473 .set_variable = efi_set_variable,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100474 .get_next_high_mono_count = (void *)&efi_device_error,
Alexander Graf77256092016-08-16 21:08:45 +0200475 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100476 .update_capsule = efi_update_capsule,
477 .query_capsule_caps = efi_query_capsule_caps,
478 .query_variable_info = efi_query_variable_info,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100479};