blob: c59d161c8fe5eb6e981e954f46cb2ce118cf84b9 [file] [log] [blame]
Alexander Graf0bd425a2016-03-04 01:10:01 +01001/*
2 * EFI application runtime services
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <command.h>
11#include <dm.h>
12#include <efi_loader.h>
13#include <rtc.h>
14#include <asm/global_data.h>
15
16/* For manual relocation support */
17DECLARE_GLOBAL_DATA_PTR;
18
Alexander Graf77256092016-08-16 21:08:45 +020019struct efi_runtime_mmio_list {
20 struct list_head link;
21 void **ptr;
22 u64 paddr;
23 u64 len;
24};
25
26/* This list contains all runtime available mmio regions */
27LIST_HEAD(efi_runtime_mmio);
28
Alexander Graf393dd912016-10-14 13:45:30 +020029static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void);
30static efi_status_t __efi_runtime EFIAPI efi_device_error(void);
31static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void);
Alexander Graf0bd425a2016-03-04 01:10:01 +010032
Alexander Grafe5702322016-04-11 23:20:39 +020033#ifdef CONFIG_SYS_CACHELINE_SIZE
34#define EFI_CACHELINE_SIZE CONFIG_SYS_CACHELINE_SIZE
35#else
36/* Just use the greatest cache flush alignment requirement I'm aware of */
37#define EFI_CACHELINE_SIZE 128
38#endif
39
Alexander Graf0bd425a2016-03-04 01:10:01 +010040#if defined(CONFIG_ARM64)
41#define R_RELATIVE 1027
42#define R_MASK 0xffffffffULL
43#define IS_RELA 1
44#elif defined(CONFIG_ARM)
45#define R_RELATIVE 23
46#define R_MASK 0xffULL
Simon Glasscdfe6962016-09-25 15:27:35 -060047#elif defined(CONFIG_X86)
48#include <asm/elf.h>
49#define R_RELATIVE R_386_RELATIVE
50#define R_MASK 0xffULL
Alexander Graf0bd425a2016-03-04 01:10:01 +010051#else
52#error Need to add relocation awareness
53#endif
54
55struct elf_rel {
56 ulong *offset;
57 ulong info;
58};
59
60struct elf_rela {
61 ulong *offset;
62 ulong info;
63 long addend;
64};
65
66/*
67 * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
68 * payload are running concurrently at the same time. In this mode, we can
69 * handle a good number of runtime callbacks
70 */
71
Alexander Graf77256092016-08-16 21:08:45 +020072static void EFIAPI efi_reset_system_boottime(
73 enum efi_reset_type reset_type,
74 efi_status_t reset_status,
75 unsigned long data_size, void *reset_data)
Alexander Graf0bd425a2016-03-04 01:10:01 +010076{
77 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
78 reset_data);
79
80 switch (reset_type) {
81 case EFI_RESET_COLD:
82 case EFI_RESET_WARM:
83 do_reset(NULL, 0, 0, NULL);
84 break;
85 case EFI_RESET_SHUTDOWN:
86 /* We don't have anything to map this to */
87 break;
88 }
89
Alexander Graf77256092016-08-16 21:08:45 +020090 while (1) { }
Alexander Graf0bd425a2016-03-04 01:10:01 +010091}
92
Alexander Graf77256092016-08-16 21:08:45 +020093static efi_status_t EFIAPI efi_get_time_boottime(
94 struct efi_time *time,
95 struct efi_time_cap *capabilities)
Alexander Graf0bd425a2016-03-04 01:10:01 +010096{
97#if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
98 struct rtc_time tm;
99 int r;
100 struct udevice *dev;
101
102 EFI_ENTRY("%p %p", time, capabilities);
103
104 r = uclass_get_device(UCLASS_RTC, 0, &dev);
105 if (r)
106 return EFI_EXIT(EFI_DEVICE_ERROR);
107
108 r = dm_rtc_get(dev, &tm);
109 if (r)
110 return EFI_EXIT(EFI_DEVICE_ERROR);
111
112 memset(time, 0, sizeof(*time));
113 time->year = tm.tm_year;
114 time->month = tm.tm_mon;
115 time->day = tm.tm_mday;
116 time->hour = tm.tm_hour;
117 time->minute = tm.tm_min;
118 time->daylight = tm.tm_isdst;
119
120 return EFI_EXIT(EFI_SUCCESS);
121#else
122 return EFI_DEVICE_ERROR;
123#endif
124}
125
Alexander Graf77256092016-08-16 21:08:45 +0200126/* Boards may override the helpers below to implement RTS functionality */
127
Alexander Graf393dd912016-10-14 13:45:30 +0200128void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200129 enum efi_reset_type reset_type,
130 efi_status_t reset_status,
131 unsigned long data_size, void *reset_data)
132{
133 /* Nothing we can do */
134 while (1) { }
135}
136
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100137efi_status_t __weak efi_reset_system_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200138{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100139 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200140}
141
Alexander Graf393dd912016-10-14 13:45:30 +0200142efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200143 struct efi_time *time,
144 struct efi_time_cap *capabilities)
145{
146 /* Nothing we can do */
147 return EFI_DEVICE_ERROR;
148}
149
Heinrich Schuchardt096db012018-03-03 15:29:00 +0100150efi_status_t __weak efi_get_time_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200151{
Heinrich Schuchardt096db012018-03-03 15:29:00 +0100152 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200153}
154
Alexander Graf0bd425a2016-03-04 01:10:01 +0100155struct efi_runtime_detach_list_struct {
156 void *ptr;
157 void *patchto;
158};
159
160static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
161 {
162 /* do_reset is gone */
163 .ptr = &efi_runtime_services.reset_system,
Alexander Graf77256092016-08-16 21:08:45 +0200164 .patchto = efi_reset_system,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100165 }, {
166 /* invalidate_*cache_all are gone */
167 .ptr = &efi_runtime_services.set_virtual_address_map,
168 .patchto = &efi_invalid_parameter,
169 }, {
170 /* RTC accessors are gone */
171 .ptr = &efi_runtime_services.get_time,
Alexander Graf77256092016-08-16 21:08:45 +0200172 .patchto = &efi_get_time,
Alexander Grafc59d3602016-05-18 00:54:47 +0200173 }, {
174 /* Clean up system table */
175 .ptr = &systab.con_in,
176 .patchto = NULL,
177 }, {
178 /* Clean up system table */
179 .ptr = &systab.con_out,
180 .patchto = NULL,
181 }, {
182 /* Clean up system table */
183 .ptr = &systab.std_err,
184 .patchto = NULL,
185 }, {
186 /* Clean up system table */
187 .ptr = &systab.boottime,
188 .patchto = NULL,
Rob Clark15f3d742017-09-13 18:05:37 -0400189 }, {
190 .ptr = &efi_runtime_services.get_variable,
191 .patchto = &efi_device_error,
192 }, {
193 .ptr = &efi_runtime_services.get_next_variable,
194 .patchto = &efi_device_error,
195 }, {
196 .ptr = &efi_runtime_services.set_variable,
197 .patchto = &efi_device_error,
198 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100199};
200
201static bool efi_runtime_tobedetached(void *p)
202{
203 int i;
204
205 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
206 if (efi_runtime_detach_list[i].ptr == p)
207 return true;
208
209 return false;
210}
211
212static void efi_runtime_detach(ulong offset)
213{
214 int i;
215 ulong patchoff = offset - (ulong)gd->relocaddr;
216
217 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
218 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
219 ulong *p = efi_runtime_detach_list[i].ptr;
220 ulong newaddr = patchto ? (patchto + patchoff) : 0;
221
Alexander Graf62a67482016-06-02 11:38:27 +0200222 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100223 *p = newaddr;
224 }
225}
226
227/* Relocate EFI runtime to uboot_reloc_base = offset */
228void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
229{
230#ifdef IS_RELA
231 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
232#else
233 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
234 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
235#endif
236
Alexander Graf62a67482016-06-02 11:38:27 +0200237 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100238 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
239 ulong base = CONFIG_SYS_TEXT_BASE;
240 ulong *p;
241 ulong newaddr;
242
243 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
244
245 if ((rel->info & R_MASK) != R_RELATIVE) {
246 continue;
247 }
248
249#ifdef IS_RELA
250 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
251#else
252 newaddr = *p - lastoff + offset;
253#endif
254
255 /* Check if the relocation is inside bounds */
256 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200257 newaddr > (map->virtual_start +
258 (map->num_pages << EFI_PAGE_SHIFT)))) {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100259 if (!efi_runtime_tobedetached(p))
260 printf("U-Boot EFI: Relocation at %p is out of "
261 "range (%lx)\n", p, newaddr);
262 continue;
263 }
264
Alexander Graf62a67482016-06-02 11:38:27 +0200265 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100266 *p = newaddr;
Alexander Grafe5702322016-04-11 23:20:39 +0200267 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
268 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf0bd425a2016-03-04 01:10:01 +0100269 }
270
271#ifndef IS_RELA
272 lastoff = offset;
273#endif
274
275 invalidate_icache_all();
276}
277
278static efi_status_t EFIAPI efi_set_virtual_address_map(
279 unsigned long memory_map_size,
280 unsigned long descriptor_size,
281 uint32_t descriptor_version,
282 struct efi_mem_desc *virtmap)
283{
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200284 ulong runtime_start = (ulong)&__efi_runtime_start &
285 ~(ulong)EFI_PAGE_MASK;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100286 int n = memory_map_size / descriptor_size;
287 int i;
288
289 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
290 descriptor_version, virtmap);
291
Alexander Graf77256092016-08-16 21:08:45 +0200292 /* Rebind mmio pointers */
293 for (i = 0; i < n; i++) {
294 struct efi_mem_desc *map = (void*)virtmap +
295 (descriptor_size * i);
296 struct list_head *lhandle;
297 efi_physical_addr_t map_start = map->physical_start;
298 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
299 efi_physical_addr_t map_end = map_start + map_len;
300
301 /* Adjust all mmio pointers in this region */
302 list_for_each(lhandle, &efi_runtime_mmio) {
303 struct efi_runtime_mmio_list *lmmio;
304
305 lmmio = list_entry(lhandle,
306 struct efi_runtime_mmio_list,
307 link);
308 if ((map_start <= lmmio->paddr) &&
309 (map_end >= lmmio->paddr)) {
310 u64 off = map->virtual_start - map_start;
311 uintptr_t new_addr = lmmio->paddr + off;
312 *lmmio->ptr = (void *)new_addr;
313 }
314 }
315 }
316
317 /* Move the actual runtime code over */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100318 for (i = 0; i < n; i++) {
319 struct efi_mem_desc *map;
320
321 map = (void*)virtmap + (descriptor_size * i);
322 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf77256092016-08-16 21:08:45 +0200323 ulong new_offset = map->virtual_start -
324 (runtime_start - gd->relocaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100325
326 efi_runtime_relocate(new_offset, map);
327 /* Once we're virtual, we can no longer handle
328 complex callbacks */
329 efi_runtime_detach(new_offset);
330 return EFI_EXIT(EFI_SUCCESS);
331 }
332 }
333
334 return EFI_EXIT(EFI_INVALID_PARAMETER);
335}
336
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100337efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf77256092016-08-16 21:08:45 +0200338{
339 struct efi_runtime_mmio_list *newmmio;
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100340 efi_status_t ret;
Alexander Graf77256092016-08-16 21:08:45 +0200341
xypron.glpk@gmx.de93fcc7f2017-08-11 21:19:37 +0200342 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100343 ret = efi_add_memory_map(*(uintptr_t *)mmio_ptr, pages, EFI_MMAP_IO,
344 false);
345 if (ret != EFI_SUCCESS)
346 return ret;
Alexander Graf77256092016-08-16 21:08:45 +0200347
348 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100349 if (!newmmio)
350 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200351 newmmio->ptr = mmio_ptr;
352 newmmio->paddr = *(uintptr_t *)mmio_ptr;
353 newmmio->len = len;
354 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100355
356 return ret;
Alexander Graf77256092016-08-16 21:08:45 +0200357}
358
Alexander Graf0bd425a2016-03-04 01:10:01 +0100359/*
360 * In the second stage, U-Boot has disappeared. To isolate our runtime code
361 * that at this point still exists from the rest, we put it into a special
362 * section.
363 *
364 * !!WARNING!!
365 *
366 * This means that we can not rely on any code outside of this file in any
367 * function or variable below this line.
368 *
369 * Please keep everything fully self-contained and annotated with
Alexander Graf393dd912016-10-14 13:45:30 +0200370 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf0bd425a2016-03-04 01:10:01 +0100371 */
372
373/*
374 * Relocate the EFI runtime stub to a different place. We need to call this
375 * the first time we expose the runtime interface to a user and on set virtual
376 * address map calls.
377 */
378
Alexander Graf393dd912016-10-14 13:45:30 +0200379static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100380{
381 return EFI_UNSUPPORTED;
382}
383
Alexander Graf393dd912016-10-14 13:45:30 +0200384static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100385{
386 return EFI_DEVICE_ERROR;
387}
388
Alexander Graf393dd912016-10-14 13:45:30 +0200389static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100390{
391 return EFI_INVALID_PARAMETER;
392}
393
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100394efi_status_t __efi_runtime EFIAPI efi_update_capsule(
395 struct efi_capsule_header **capsule_header_array,
396 efi_uintn_t capsule_count,
397 u64 scatter_gather_list)
398{
399 return EFI_UNSUPPORTED;
400}
401
402efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
403 struct efi_capsule_header **capsule_header_array,
404 efi_uintn_t capsule_count,
405 u64 maximum_capsule_size,
406 u32 reset_type)
407{
408 return EFI_UNSUPPORTED;
409}
410
411efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
412 u32 attributes,
413 u64 maximum_variable_storage_size,
414 u64 remaining_variable_storage_size,
415 u64 maximum_variable_size)
416{
417 return EFI_UNSUPPORTED;
418}
419
Alexander Graf393dd912016-10-14 13:45:30 +0200420struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100421 .hdr = {
422 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
423 .revision = EFI_RUNTIME_SERVICES_REVISION,
424 .headersize = sizeof(struct efi_table_hdr),
425 },
Alexander Graf77256092016-08-16 21:08:45 +0200426 .get_time = &efi_get_time_boottime,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100427 .set_time = (void *)&efi_device_error,
428 .get_wakeup_time = (void *)&efi_unimplemented,
429 .set_wakeup_time = (void *)&efi_unimplemented,
430 .set_virtual_address_map = &efi_set_virtual_address_map,
431 .convert_pointer = (void *)&efi_invalid_parameter,
Rob Clark15f3d742017-09-13 18:05:37 -0400432 .get_variable = efi_get_variable,
433 .get_next_variable = efi_get_next_variable,
434 .set_variable = efi_set_variable,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100435 .get_next_high_mono_count = (void *)&efi_device_error,
Alexander Graf77256092016-08-16 21:08:45 +0200436 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100437 .update_capsule = efi_update_capsule,
438 .query_capsule_caps = efi_query_capsule_caps,
439 .query_variable_info = efi_query_variable_info,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100440};