]> git.ipfire.org Git - thirdparty/u-boot.git/blame - lib/efi_loader/efi_runtime.c
efi_loader: remove unused efi_get_time_init()
[thirdparty/u-boot.git] / lib / efi_loader / efi_runtime.c
CommitLineData
f739fcd8 1// SPDX-License-Identifier: GPL-2.0+
50149ea3
AG
2/*
3 * EFI application runtime services
4 *
5 * Copyright (c) 2016 Alexander Graf
50149ea3
AG
6 */
7
8#include <common.h>
9#include <command.h>
10#include <dm.h>
b34662d0 11#include <elf.h>
50149ea3
AG
12#include <efi_loader.h>
13#include <rtc.h>
50149ea3
AG
14
15/* For manual relocation support */
16DECLARE_GLOBAL_DATA_PTR;
17
80a4800e
AG
18struct 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
3c63db9c
AG
28static 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);
50149ea3 31
2d2b5b2d
SG
32/*
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 */
bc028919 36#if defined(__aarch64__)
b34662d0 37#define R_RELATIVE R_AARCH64_RELATIVE
50149ea3
AG
38#define R_MASK 0xffffffffULL
39#define IS_RELA 1
bc028919 40#elif defined(__arm__)
b34662d0 41#define R_RELATIVE R_ARM_RELATIVE
50149ea3 42#define R_MASK 0xffULL
bc028919 43#elif defined(__x86_64__) || defined(__i386__)
65e4c0b1
SG
44#define R_RELATIVE R_386_RELATIVE
45#define R_MASK 0xffULL
bc028919 46#elif defined(__riscv)
6836adbe
RC
47#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};
bc028919 57#if (__riscv_xlen == 32)
6836adbe
RC
58#define R_ABSOLUTE R_RISCV_32
59#define SYM_INDEX 8
bc028919 60#elif (__riscv_xlen == 64)
6836adbe
RC
61#define R_ABSOLUTE R_RISCV_64
62#define SYM_INDEX 32
bc028919
AG
63#else
64#error unknown riscv target
6836adbe 65#endif
50149ea3
AG
66#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
80a4800e
AG
87static 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)
50149ea3 91{
b095f3c8
HS
92 struct efi_event *evt;
93
50149ea3
AG
94 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
95 reset_data);
96
b095f3c8
HS
97 /* 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 }
50149ea3
AG
106 switch (reset_type) {
107 case EFI_RESET_COLD:
108 case EFI_RESET_WARM:
482fc90c 109 case EFI_RESET_PLATFORM_SPECIFIC:
50149ea3
AG
110 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
80a4800e 117 while (1) { }
50149ea3
AG
118}
119
80a4800e
AG
120static efi_status_t EFIAPI efi_get_time_boottime(
121 struct efi_time *time,
122 struct efi_time_cap *capabilities)
50149ea3
AG
123{
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
80a4800e
AG
153/* Boards may override the helpers below to implement RTS functionality */
154
3c63db9c 155void __weak __efi_runtime EFIAPI efi_reset_system(
80a4800e
AG
156 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
22c793e6 164efi_status_t __weak efi_reset_system_init(void)
80a4800e 165{
22c793e6 166 return EFI_SUCCESS;
80a4800e
AG
167}
168
3c63db9c 169efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
80a4800e
AG
170 struct efi_time *time,
171 struct efi_time_cap *capabilities)
172{
173 /* Nothing we can do */
174 return EFI_DEVICE_ERROR;
175}
176
50149ea3
AG
177struct efi_runtime_detach_list_struct {
178 void *ptr;
179 void *patchto;
180};
181
182static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
183 {
184 /* do_reset is gone */
185 .ptr = &efi_runtime_services.reset_system,
80a4800e 186 .patchto = efi_reset_system,
50149ea3
AG
187 }, {
188 /* invalidate_*cache_all are gone */
189 .ptr = &efi_runtime_services.set_virtual_address_map,
190 .patchto = &efi_invalid_parameter,
191 }, {
192 /* RTC accessors are gone */
193 .ptr = &efi_runtime_services.get_time,
80a4800e 194 .patchto = &efi_get_time,
ae874405
AG
195 }, {
196 /* Clean up system table */
197 .ptr = &systab.con_in,
198 .patchto = NULL,
199 }, {
200 /* Clean up system table */
201 .ptr = &systab.con_out,
202 .patchto = NULL,
203 }, {
204 /* Clean up system table */
205 .ptr = &systab.std_err,
206 .patchto = NULL,
207 }, {
208 /* Clean up system table */
209 .ptr = &systab.boottime,
210 .patchto = NULL,
ad644e7c
RC
211 }, {
212 .ptr = &efi_runtime_services.get_variable,
213 .patchto = &efi_device_error,
214 }, {
45c66f9c 215 .ptr = &efi_runtime_services.get_next_variable_name,
ad644e7c
RC
216 .patchto = &efi_device_error,
217 }, {
218 .ptr = &efi_runtime_services.set_variable,
219 .patchto = &efi_device_error,
220 }
50149ea3
AG
221};
222
223static bool efi_runtime_tobedetached(void *p)
224{
225 int i;
226
227 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
228 if (efi_runtime_detach_list[i].ptr == p)
229 return true;
230
231 return false;
232}
233
234static void efi_runtime_detach(ulong offset)
235{
236 int i;
237 ulong patchoff = offset - (ulong)gd->relocaddr;
238
239 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
240 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
241 ulong *p = efi_runtime_detach_list[i].ptr;
242 ulong newaddr = patchto ? (patchto + patchoff) : 0;
243
edcef3ba 244 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
50149ea3
AG
245 *p = newaddr;
246 }
247}
248
249/* Relocate EFI runtime to uboot_reloc_base = offset */
250void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
251{
252#ifdef IS_RELA
253 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
254#else
255 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
256 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
257#endif
258
edcef3ba 259 debug("%s: Relocating to offset=%lx\n", __func__, offset);
50149ea3
AG
260 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
261 ulong base = CONFIG_SYS_TEXT_BASE;
262 ulong *p;
263 ulong newaddr;
264
265 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
266
6836adbe 267 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, rel->info, *p, rel->offset);
50149ea3 268
6836adbe
RC
269 switch (rel->info & R_MASK) {
270 case R_RELATIVE:
50149ea3
AG
271#ifdef IS_RELA
272 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
273#else
274 newaddr = *p - lastoff + offset;
275#endif
6836adbe
RC
276 break;
277#ifdef R_ABSOLUTE
278 case R_ABSOLUTE: {
279 ulong symidx = rel->info >> SYM_INDEX;
280 extern struct dyn_sym __dyn_sym_start[];
281 newaddr = __dyn_sym_start[symidx].addr + offset;
282 break;
283 }
284#endif
285 default:
286 continue;
287 }
50149ea3
AG
288
289 /* Check if the relocation is inside bounds */
290 if (map && ((newaddr < map->virtual_start) ||
591cf2e1
HS
291 newaddr > (map->virtual_start +
292 (map->num_pages << EFI_PAGE_SHIFT)))) {
50149ea3
AG
293 if (!efi_runtime_tobedetached(p))
294 printf("U-Boot EFI: Relocation at %p is out of "
295 "range (%lx)\n", p, newaddr);
296 continue;
297 }
298
edcef3ba 299 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
50149ea3 300 *p = newaddr;
36c37a84
AG
301 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
302 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
50149ea3
AG
303 }
304
305#ifndef IS_RELA
306 lastoff = offset;
307#endif
308
309 invalidate_icache_all();
310}
311
312static efi_status_t EFIAPI efi_set_virtual_address_map(
313 unsigned long memory_map_size,
314 unsigned long descriptor_size,
315 uint32_t descriptor_version,
316 struct efi_mem_desc *virtmap)
317{
591cf2e1
HS
318 ulong runtime_start = (ulong)&__efi_runtime_start &
319 ~(ulong)EFI_PAGE_MASK;
50149ea3
AG
320 int n = memory_map_size / descriptor_size;
321 int i;
322
323 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
324 descriptor_version, virtmap);
325
80a4800e
AG
326 /* Rebind mmio pointers */
327 for (i = 0; i < n; i++) {
328 struct efi_mem_desc *map = (void*)virtmap +
329 (descriptor_size * i);
330 struct list_head *lhandle;
331 efi_physical_addr_t map_start = map->physical_start;
332 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
333 efi_physical_addr_t map_end = map_start + map_len;
334
335 /* Adjust all mmio pointers in this region */
336 list_for_each(lhandle, &efi_runtime_mmio) {
337 struct efi_runtime_mmio_list *lmmio;
338
339 lmmio = list_entry(lhandle,
340 struct efi_runtime_mmio_list,
341 link);
342 if ((map_start <= lmmio->paddr) &&
343 (map_end >= lmmio->paddr)) {
344 u64 off = map->virtual_start - map_start;
345 uintptr_t new_addr = lmmio->paddr + off;
346 *lmmio->ptr = (void *)new_addr;
347 }
348 }
349 }
350
351 /* Move the actual runtime code over */
50149ea3
AG
352 for (i = 0; i < n; i++) {
353 struct efi_mem_desc *map;
354
355 map = (void*)virtmap + (descriptor_size * i);
356 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
80a4800e
AG
357 ulong new_offset = map->virtual_start -
358 (runtime_start - gd->relocaddr);
50149ea3
AG
359
360 efi_runtime_relocate(new_offset, map);
361 /* Once we're virtual, we can no longer handle
362 complex callbacks */
363 efi_runtime_detach(new_offset);
364 return EFI_EXIT(EFI_SUCCESS);
365 }
366 }
367
368 return EFI_EXIT(EFI_INVALID_PARAMETER);
369}
370
22c793e6 371efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
80a4800e
AG
372{
373 struct efi_runtime_mmio_list *newmmio;
aee4f06d 374 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
813468cd
AG
375 uint64_t addr = *(uintptr_t *)mmio_ptr;
376 uint64_t retaddr;
377
378 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
379 if (retaddr != addr)
380 return EFI_OUT_OF_RESOURCES;
80a4800e
AG
381
382 newmmio = calloc(1, sizeof(*newmmio));
22c793e6
HS
383 if (!newmmio)
384 return EFI_OUT_OF_RESOURCES;
80a4800e
AG
385 newmmio->ptr = mmio_ptr;
386 newmmio->paddr = *(uintptr_t *)mmio_ptr;
387 newmmio->len = len;
388 list_add_tail(&newmmio->link, &efi_runtime_mmio);
22c793e6 389
813468cd 390 return EFI_SUCCESS;
80a4800e
AG
391}
392
50149ea3
AG
393/*
394 * In the second stage, U-Boot has disappeared. To isolate our runtime code
395 * that at this point still exists from the rest, we put it into a special
396 * section.
397 *
398 * !!WARNING!!
399 *
400 * This means that we can not rely on any code outside of this file in any
401 * function or variable below this line.
402 *
403 * Please keep everything fully self-contained and annotated with
3c63db9c 404 * __efi_runtime and __efi_runtime_data markers.
50149ea3
AG
405 */
406
407/*
408 * Relocate the EFI runtime stub to a different place. We need to call this
409 * the first time we expose the runtime interface to a user and on set virtual
410 * address map calls.
411 */
412
3c63db9c 413static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
50149ea3
AG
414{
415 return EFI_UNSUPPORTED;
416}
417
3c63db9c 418static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
50149ea3
AG
419{
420 return EFI_DEVICE_ERROR;
421}
422
3c63db9c 423static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
50149ea3
AG
424{
425 return EFI_INVALID_PARAMETER;
426}
427
0c230743
HS
428efi_status_t __efi_runtime EFIAPI efi_update_capsule(
429 struct efi_capsule_header **capsule_header_array,
430 efi_uintn_t capsule_count,
431 u64 scatter_gather_list)
432{
433 return EFI_UNSUPPORTED;
434}
435
436efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
437 struct efi_capsule_header **capsule_header_array,
438 efi_uintn_t capsule_count,
439 u64 maximum_capsule_size,
440 u32 reset_type)
441{
442 return EFI_UNSUPPORTED;
443}
444
445efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
446 u32 attributes,
45c66f9c
HS
447 u64 *maximum_variable_storage_size,
448 u64 *remaining_variable_storage_size,
449 u64 *maximum_variable_size)
0c230743
HS
450{
451 return EFI_UNSUPPORTED;
452}
453
3c63db9c 454struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
50149ea3
AG
455 .hdr = {
456 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
112f2430 457 .revision = EFI_SPECIFICATION_VERSION,
71c846ab 458 .headersize = sizeof(struct efi_runtime_services),
50149ea3 459 },
80a4800e 460 .get_time = &efi_get_time_boottime,
50149ea3
AG
461 .set_time = (void *)&efi_device_error,
462 .get_wakeup_time = (void *)&efi_unimplemented,
463 .set_wakeup_time = (void *)&efi_unimplemented,
464 .set_virtual_address_map = &efi_set_virtual_address_map,
465 .convert_pointer = (void *)&efi_invalid_parameter,
ad644e7c 466 .get_variable = efi_get_variable,
45c66f9c 467 .get_next_variable_name = efi_get_next_variable_name,
ad644e7c 468 .set_variable = efi_set_variable,
50149ea3 469 .get_next_high_mono_count = (void *)&efi_device_error,
80a4800e 470 .reset_system = &efi_reset_system_boottime,
0c230743
HS
471 .update_capsule = efi_update_capsule,
472 .query_capsule_caps = efi_query_capsule_caps,
473 .query_variable_info = efi_query_variable_info,
50149ea3 474};