blob: ca66d33e588200684000480f8bf39a0f66110c34 [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/*
Heinrich Schuchardt11121542018-09-03 19:29:41 +020033 * TODO(sjg@chromium.org): These defines and structures should come from the ELF
34 * header for each architecture (or a generic header) rather than being repeated
35 * here.
Simon Glassfff89d42018-06-11 23:26:40 -060036 */
Alexander Graf0fe51922018-06-18 17:23:08 +020037#if defined(__aarch64__)
Alexander Graf46c15142018-06-18 17:23:11 +020038#define R_RELATIVE R_AARCH64_RELATIVE
Alexander Graf0bd425a2016-03-04 01:10:01 +010039#define R_MASK 0xffffffffULL
40#define IS_RELA 1
Alexander Graf0fe51922018-06-18 17:23:08 +020041#elif defined(__arm__)
Alexander Graf46c15142018-06-18 17:23:11 +020042#define R_RELATIVE R_ARM_RELATIVE
Alexander Graf0bd425a2016-03-04 01:10:01 +010043#define R_MASK 0xffULL
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -070044#elif defined(__i386__)
Simon Glasscdfe6962016-09-25 15:27:35 -060045#define R_RELATIVE R_386_RELATIVE
46#define R_MASK 0xffULL
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -070047#elif defined(__x86_64__)
48#define R_RELATIVE R_X86_64_RELATIVE
49#define R_MASK 0xffffffffULL
50#define IS_RELA 1
Alexander Graf0fe51922018-06-18 17:23:08 +020051#elif defined(__riscv)
Rick Chen9677a372018-05-28 19:06:37 +080052#define R_RELATIVE R_RISCV_RELATIVE
53#define R_MASK 0xffULL
54#define IS_RELA 1
55
56struct dyn_sym {
57 ulong foo1;
58 ulong addr;
59 u32 foo2;
60 u32 foo3;
61};
Alexander Graf0fe51922018-06-18 17:23:08 +020062#if (__riscv_xlen == 32)
Rick Chen9677a372018-05-28 19:06:37 +080063#define R_ABSOLUTE R_RISCV_32
64#define SYM_INDEX 8
Alexander Graf0fe51922018-06-18 17:23:08 +020065#elif (__riscv_xlen == 64)
Rick Chen9677a372018-05-28 19:06:37 +080066#define R_ABSOLUTE R_RISCV_64
67#define SYM_INDEX 32
Alexander Graf0fe51922018-06-18 17:23:08 +020068#else
69#error unknown riscv target
Rick Chen9677a372018-05-28 19:06:37 +080070#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +010071#else
72#error Need to add relocation awareness
73#endif
74
75struct elf_rel {
76 ulong *offset;
77 ulong info;
78};
79
80struct elf_rela {
81 ulong *offset;
82 ulong info;
83 long addend;
84};
85
86/*
Heinrich Schuchardt11121542018-09-03 19:29:41 +020087 * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI
Alexander Graf0bd425a2016-03-04 01:10:01 +010088 * payload are running concurrently at the same time. In this mode, we can
89 * handle a good number of runtime callbacks
90 */
91
Heinrich Schuchardt69c15762018-07-29 09:49:04 +020092/**
93 * efi_update_table_header_crc32() - Update crc32 in table header
94 *
95 * @table: EFI table
96 */
97void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
98{
99 table->crc32 = 0;
100 table->crc32 = crc32(0, (const unsigned char *)table,
101 table->headersize);
102}
103
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200104/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200105 * efi_reset_system_boottime() - reset system at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200106 *
107 * This function implements the ResetSystem() runtime service before
108 * SetVirtualAddressMap() is called.
109 *
110 * See the Unified Extensible Firmware Interface (UEFI) specification for
111 * details.
112 *
113 * @reset_type: type of reset to perform
114 * @reset_status: status code for the reset
115 * @data_size: size of reset_data
116 * @reset_data: information about the reset
117 */
Alexander Graf77256092016-08-16 21:08:45 +0200118static void EFIAPI efi_reset_system_boottime(
119 enum efi_reset_type reset_type,
120 efi_status_t reset_status,
121 unsigned long data_size, void *reset_data)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100122{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100123 struct efi_event *evt;
124
Alexander Graf0bd425a2016-03-04 01:10:01 +0100125 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
126 reset_data);
127
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100128 /* Notify reset */
129 list_for_each_entry(evt, &efi_events, link) {
130 if (evt->group &&
131 !guidcmp(evt->group,
132 &efi_guid_event_group_reset_system)) {
133 efi_signal_event(evt, false);
134 break;
135 }
136 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100137 switch (reset_type) {
138 case EFI_RESET_COLD:
139 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +0100140 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100141 do_reset(NULL, 0, 0, NULL);
142 break;
143 case EFI_RESET_SHUTDOWN:
Heinrich Schuchardt44489452018-10-16 07:44:53 +0200144#ifdef CONFIG_CMD_POWEROFF
145 do_poweroff(NULL, 0, 0, NULL);
146#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +0100147 break;
148 }
149
Alexander Graf77256092016-08-16 21:08:45 +0200150 while (1) { }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100151}
152
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200153/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200154 * efi_get_time_boottime() - get current time at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200155 *
156 * This function implements the GetTime runtime service before
157 * SetVirtualAddressMap() is called.
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200158 *
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200159 * See the Unified Extensible Firmware Interface (UEFI) specification
160 * for details.
161 *
162 * @time: pointer to structure to receive current time
163 * @capabilities: pointer to structure to receive RTC properties
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200164 * Returns: status code
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200165 */
Alexander Graf77256092016-08-16 21:08:45 +0200166static efi_status_t EFIAPI efi_get_time_boottime(
167 struct efi_time *time,
168 struct efi_time_cap *capabilities)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100169{
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200170#ifdef CONFIG_DM_RTC
171 efi_status_t ret = EFI_SUCCESS;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100172 int r;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200173 struct rtc_time tm;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100174 struct udevice *dev;
175
176 EFI_ENTRY("%p %p", time, capabilities);
177
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200178 if (!time) {
179 ret = EFI_INVALID_PARAMETER;
180 goto out;
181 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100182
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200183 r = uclass_get_device(UCLASS_RTC, 0, &dev);
184 if (!r)
185 r = dm_rtc_get(dev, &tm);
186 if (r) {
187 ret = EFI_DEVICE_ERROR;
188 goto out;
189 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100190
191 memset(time, 0, sizeof(*time));
192 time->year = tm.tm_year;
193 time->month = tm.tm_mon;
194 time->day = tm.tm_mday;
195 time->hour = tm.tm_hour;
196 time->minute = tm.tm_min;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200197 time->second = tm.tm_sec;
198 time->daylight = EFI_TIME_ADJUST_DAYLIGHT;
199 if (tm.tm_isdst > 0)
200 time->daylight |= EFI_TIME_IN_DAYLIGHT;
201 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100202
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200203 if (capabilities) {
204 /* Set reasonable dummy values */
205 capabilities->resolution = 1; /* 1 Hz */
206 capabilities->accuracy = 100000000; /* 100 ppm */
207 capabilities->sets_to_zero = false;
208 }
209out:
210 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100211#else
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200212 EFI_ENTRY("%p %p", time, capabilities);
213 return EFI_EXIT(EFI_DEVICE_ERROR);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100214#endif
215}
216
Alexander Graf77256092016-08-16 21:08:45 +0200217
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200218/**
219 * efi_reset_system() - reset system
220 *
221 * This function implements the ResetSystem() runtime service after
222 * SetVirtualAddressMap() is called. It only executes an endless loop.
223 * Boards may override the helpers below to implement reset functionality.
224 *
225 * See the Unified Extensible Firmware Interface (UEFI) specification for
226 * details.
227 *
228 * @reset_type: type of reset to perform
229 * @reset_status: status code for the reset
230 * @data_size: size of reset_data
231 * @reset_data: information about the reset
232 */
Alexander Graf393dd912016-10-14 13:45:30 +0200233void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200234 enum efi_reset_type reset_type,
235 efi_status_t reset_status,
236 unsigned long data_size, void *reset_data)
237{
238 /* Nothing we can do */
239 while (1) { }
240}
241
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200242/**
243 * efi_reset_system_init() - initialize the reset driver
244 *
245 * Boards may override this function to initialize the reset driver.
246 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100247efi_status_t __weak efi_reset_system_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200248{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100249 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200250}
251
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200252/**
253 * efi_get_time() - get current time
254 *
255 * This function implements the GetTime runtime service after
256 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
257 * anymore only an error code is returned.
258 *
259 * See the Unified Extensible Firmware Interface (UEFI) specification
260 * for details.
261 *
262 * @time: pointer to structure to receive current time
263 * @capabilities: pointer to structure to receive RTC properties
264 * Returns: status code
265 */
Alexander Graf393dd912016-10-14 13:45:30 +0200266efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200267 struct efi_time *time,
268 struct efi_time_cap *capabilities)
269{
270 /* Nothing we can do */
271 return EFI_DEVICE_ERROR;
272}
273
Alexander Graf0bd425a2016-03-04 01:10:01 +0100274struct efi_runtime_detach_list_struct {
275 void *ptr;
276 void *patchto;
277};
278
279static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
280 {
281 /* do_reset is gone */
282 .ptr = &efi_runtime_services.reset_system,
Alexander Graf77256092016-08-16 21:08:45 +0200283 .patchto = efi_reset_system,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100284 }, {
285 /* invalidate_*cache_all are gone */
286 .ptr = &efi_runtime_services.set_virtual_address_map,
287 .patchto = &efi_invalid_parameter,
288 }, {
289 /* RTC accessors are gone */
290 .ptr = &efi_runtime_services.get_time,
Alexander Graf77256092016-08-16 21:08:45 +0200291 .patchto = &efi_get_time,
Alexander Grafc59d3602016-05-18 00:54:47 +0200292 }, {
293 /* Clean up system table */
294 .ptr = &systab.con_in,
295 .patchto = NULL,
296 }, {
297 /* Clean up system table */
298 .ptr = &systab.con_out,
299 .patchto = NULL,
300 }, {
301 /* Clean up system table */
302 .ptr = &systab.std_err,
303 .patchto = NULL,
304 }, {
305 /* Clean up system table */
306 .ptr = &systab.boottime,
307 .patchto = NULL,
Rob Clark15f3d742017-09-13 18:05:37 -0400308 }, {
309 .ptr = &efi_runtime_services.get_variable,
310 .patchto = &efi_device_error,
311 }, {
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200312 .ptr = &efi_runtime_services.get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400313 .patchto = &efi_device_error,
314 }, {
315 .ptr = &efi_runtime_services.set_variable,
316 .patchto = &efi_device_error,
317 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100318};
319
320static bool efi_runtime_tobedetached(void *p)
321{
322 int i;
323
324 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
325 if (efi_runtime_detach_list[i].ptr == p)
326 return true;
327
328 return false;
329}
330
331static void efi_runtime_detach(ulong offset)
332{
333 int i;
334 ulong patchoff = offset - (ulong)gd->relocaddr;
335
336 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
337 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
338 ulong *p = efi_runtime_detach_list[i].ptr;
339 ulong newaddr = patchto ? (patchto + patchoff) : 0;
340
Alexander Graf62a67482016-06-02 11:38:27 +0200341 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100342 *p = newaddr;
343 }
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200344
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200345 /* Update CRC32 */
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200346 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100347}
348
349/* Relocate EFI runtime to uboot_reloc_base = offset */
350void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
351{
352#ifdef IS_RELA
353 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
354#else
355 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
356 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
357#endif
358
Alexander Graf62a67482016-06-02 11:38:27 +0200359 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100360 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
361 ulong base = CONFIG_SYS_TEXT_BASE;
362 ulong *p;
363 ulong newaddr;
364
365 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
366
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700367 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
368 rel->info, *p, rel->offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100369
Rick Chen9677a372018-05-28 19:06:37 +0800370 switch (rel->info & R_MASK) {
371 case R_RELATIVE:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100372#ifdef IS_RELA
373 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
374#else
375 newaddr = *p - lastoff + offset;
376#endif
Rick Chen9677a372018-05-28 19:06:37 +0800377 break;
378#ifdef R_ABSOLUTE
379 case R_ABSOLUTE: {
380 ulong symidx = rel->info >> SYM_INDEX;
381 extern struct dyn_sym __dyn_sym_start[];
382 newaddr = __dyn_sym_start[symidx].addr + offset;
Alexander Grafdf0f6c72018-11-04 22:25:22 +0100383#ifdef IS_RELA
384 newaddr -= CONFIG_SYS_TEXT_BASE;
385#endif
Rick Chen9677a372018-05-28 19:06:37 +0800386 break;
387 }
388#endif
389 default:
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700390 if (!efi_runtime_tobedetached(p))
391 printf("%s: Unknown relocation type %llx\n",
392 __func__, rel->info & R_MASK);
Rick Chen9677a372018-05-28 19:06:37 +0800393 continue;
394 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100395
396 /* Check if the relocation is inside bounds */
397 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200398 newaddr > (map->virtual_start +
399 (map->num_pages << EFI_PAGE_SHIFT)))) {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100400 if (!efi_runtime_tobedetached(p))
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700401 printf("%s: Relocation at %p is out of "
402 "range (%lx)\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100403 continue;
404 }
405
Alexander Graf62a67482016-06-02 11:38:27 +0200406 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100407 *p = newaddr;
Alexander Grafe5702322016-04-11 23:20:39 +0200408 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
409 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf0bd425a2016-03-04 01:10:01 +0100410 }
411
412#ifndef IS_RELA
413 lastoff = offset;
414#endif
415
416 invalidate_icache_all();
417}
418
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200419/**
420 * efi_set_virtual_address_map() - change from physical to virtual mapping
421 *
422 * This function implements the SetVirtualAddressMap() runtime service.
423 *
424 * See the Unified Extensible Firmware Interface (UEFI) specification for
425 * details.
426 *
427 * @memory_map_size: size of the virtual map
428 * @descriptor_size: size of an entry in the map
429 * @descriptor_version: version of the map entries
430 * @virtmap: virtual address mapping information
431 * Return: status code
432 */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100433static efi_status_t EFIAPI efi_set_virtual_address_map(
434 unsigned long memory_map_size,
435 unsigned long descriptor_size,
436 uint32_t descriptor_version,
437 struct efi_mem_desc *virtmap)
438{
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200439 ulong runtime_start = (ulong)&__efi_runtime_start &
440 ~(ulong)EFI_PAGE_MASK;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100441 int n = memory_map_size / descriptor_size;
442 int i;
443
444 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
445 descriptor_version, virtmap);
446
Alexander Graf77256092016-08-16 21:08:45 +0200447 /* Rebind mmio pointers */
448 for (i = 0; i < n; i++) {
449 struct efi_mem_desc *map = (void*)virtmap +
450 (descriptor_size * i);
451 struct list_head *lhandle;
452 efi_physical_addr_t map_start = map->physical_start;
453 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
454 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200455 u64 off = map->virtual_start - map_start;
Alexander Graf77256092016-08-16 21:08:45 +0200456
457 /* Adjust all mmio pointers in this region */
458 list_for_each(lhandle, &efi_runtime_mmio) {
459 struct efi_runtime_mmio_list *lmmio;
460
461 lmmio = list_entry(lhandle,
462 struct efi_runtime_mmio_list,
463 link);
464 if ((map_start <= lmmio->paddr) &&
465 (map_end >= lmmio->paddr)) {
Alexander Graf77256092016-08-16 21:08:45 +0200466 uintptr_t new_addr = lmmio->paddr + off;
467 *lmmio->ptr = (void *)new_addr;
468 }
469 }
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200470 if ((map_start <= (uintptr_t)systab.tables) &&
471 (map_end >= (uintptr_t)systab.tables)) {
472 char *ptr = (char *)systab.tables;
473
474 ptr += off;
475 systab.tables = (struct efi_configuration_table *)ptr;
476 }
Alexander Graf77256092016-08-16 21:08:45 +0200477 }
478
479 /* Move the actual runtime code over */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100480 for (i = 0; i < n; i++) {
481 struct efi_mem_desc *map;
482
483 map = (void*)virtmap + (descriptor_size * i);
484 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf77256092016-08-16 21:08:45 +0200485 ulong new_offset = map->virtual_start -
486 (runtime_start - gd->relocaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100487
488 efi_runtime_relocate(new_offset, map);
489 /* Once we're virtual, we can no longer handle
490 complex callbacks */
491 efi_runtime_detach(new_offset);
492 return EFI_EXIT(EFI_SUCCESS);
493 }
494 }
495
496 return EFI_EXIT(EFI_INVALID_PARAMETER);
497}
498
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200499/**
500 * efi_add_runtime_mmio() - add memory-mapped IO region
501 *
502 * This function adds a memory-mapped IO region to the memory map to make it
503 * available at runtime.
504 *
505 * @mmio_ptr: address of the memory-mapped IO region
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200506 * @len: size of the memory-mapped IO region
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200507 * Returns: status code
508 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100509efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf77256092016-08-16 21:08:45 +0200510{
511 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.de93fcc7f2017-08-11 21:19:37 +0200512 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf33a83a32018-03-15 15:08:16 +0100513 uint64_t addr = *(uintptr_t *)mmio_ptr;
514 uint64_t retaddr;
515
516 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
517 if (retaddr != addr)
518 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200519
520 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100521 if (!newmmio)
522 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200523 newmmio->ptr = mmio_ptr;
524 newmmio->paddr = *(uintptr_t *)mmio_ptr;
525 newmmio->len = len;
526 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100527
Alexander Graf33a83a32018-03-15 15:08:16 +0100528 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200529}
530
Alexander Graf0bd425a2016-03-04 01:10:01 +0100531/*
532 * In the second stage, U-Boot has disappeared. To isolate our runtime code
533 * that at this point still exists from the rest, we put it into a special
534 * section.
535 *
536 * !!WARNING!!
537 *
538 * This means that we can not rely on any code outside of this file in any
539 * function or variable below this line.
540 *
541 * Please keep everything fully self-contained and annotated with
Alexander Graf393dd912016-10-14 13:45:30 +0200542 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf0bd425a2016-03-04 01:10:01 +0100543 */
544
545/*
546 * Relocate the EFI runtime stub to a different place. We need to call this
547 * the first time we expose the runtime interface to a user and on set virtual
548 * address map calls.
549 */
550
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200551/**
552 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
553 *
554 * This function is used after SetVirtualAddressMap() is called as replacement
555 * for services that are not available anymore due to constraints of the U-Boot
556 * implementation.
557 *
558 * Return: EFI_UNSUPPORTED
559 */
Alexander Graf393dd912016-10-14 13:45:30 +0200560static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100561{
562 return EFI_UNSUPPORTED;
563}
564
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200565/**
566 * efi_device_error() - replacement function, returns EFI_DEVICE_ERROR
567 *
568 * This function is used after SetVirtualAddressMap() is called as replacement
569 * for services that are not available anymore due to constraints of the U-Boot
570 * implementation.
571 *
572 * Return: EFI_DEVICE_ERROR
573 */
Alexander Graf393dd912016-10-14 13:45:30 +0200574static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100575{
576 return EFI_DEVICE_ERROR;
577}
578
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200579/**
580 * efi_invalid_parameter() - replacement function, returns EFI_INVALID_PARAMETER
581 *
582 * This function is used after SetVirtualAddressMap() is called as replacement
583 * for services that are not available anymore due to constraints of the U-Boot
584 * implementation.
585 *
586 * Return: EFI_INVALID_PARAMETER
587 */
Alexander Graf393dd912016-10-14 13:45:30 +0200588static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100589{
590 return EFI_INVALID_PARAMETER;
591}
592
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200593/**
594 * efi_update_capsule() - process information from operating system
595 *
596 * This function implements the UpdateCapsule() runtime service.
597 *
598 * See the Unified Extensible Firmware Interface (UEFI) specification for
599 * details.
600 *
601 * @capsule_header_array: pointer to array of virtual pointers
602 * @capsule_count: number of pointers in capsule_header_array
603 * @scatter_gather_list: pointer to arry of physical pointers
604 * Returns: status code
605 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100606efi_status_t __efi_runtime EFIAPI efi_update_capsule(
607 struct efi_capsule_header **capsule_header_array,
608 efi_uintn_t capsule_count,
609 u64 scatter_gather_list)
610{
611 return EFI_UNSUPPORTED;
612}
613
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200614/**
615 * efi_query_capsule_caps() - check if capsule is supported
616 *
617 * This function implements the QueryCapsuleCapabilities() runtime service.
618 *
619 * See the Unified Extensible Firmware Interface (UEFI) specification for
620 * details.
621 *
622 * @capsule_header_array: pointer to array of virtual pointers
623 * @capsule_count: number of pointers in capsule_header_array
Heinrich Schuchardt1d1be362018-09-03 20:59:25 +0200624 * @maximum_capsule_size: maximum capsule size
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200625 * @reset_type: type of reset needed for capsule update
626 * Returns: status code
627 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100628efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
629 struct efi_capsule_header **capsule_header_array,
630 efi_uintn_t capsule_count,
631 u64 maximum_capsule_size,
632 u32 reset_type)
633{
634 return EFI_UNSUPPORTED;
635}
636
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200637/**
638 * efi_query_variable_info() - get information about EFI variables
639 *
640 * This function implements the QueryVariableInfo() runtime service.
641 *
642 * See the Unified Extensible Firmware Interface (UEFI) specification for
643 * details.
644 *
645 * @attributes: bitmask to select variables to be
646 * queried
647 * @maximum_variable_storage_size: maximum size of storage area for the
648 * selected variable types
649 * @remaining_variable_storage_size: remaining size of storage are for the
650 * selected variable types
651 * @maximum_variable_size: maximum size of a variable of the
652 * selected type
653 * Returns: status code
654 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100655efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
656 u32 attributes,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200657 u64 *maximum_variable_storage_size,
658 u64 *remaining_variable_storage_size,
659 u64 *maximum_variable_size)
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100660{
661 return EFI_UNSUPPORTED;
662}
663
Alexander Graf393dd912016-10-14 13:45:30 +0200664struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100665 .hdr = {
666 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +0200667 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +0200668 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf0bd425a2016-03-04 01:10:01 +0100669 },
Alexander Graf77256092016-08-16 21:08:45 +0200670 .get_time = &efi_get_time_boottime,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100671 .set_time = (void *)&efi_device_error,
672 .get_wakeup_time = (void *)&efi_unimplemented,
673 .set_wakeup_time = (void *)&efi_unimplemented,
674 .set_virtual_address_map = &efi_set_virtual_address_map,
675 .convert_pointer = (void *)&efi_invalid_parameter,
Rob Clark15f3d742017-09-13 18:05:37 -0400676 .get_variable = efi_get_variable,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200677 .get_next_variable_name = efi_get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400678 .set_variable = efi_set_variable,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100679 .get_next_high_mono_count = (void *)&efi_device_error,
Alexander Graf77256092016-08-16 21:08:45 +0200680 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100681 .update_capsule = efi_update_capsule,
682 .query_capsule_caps = efi_query_capsule_caps,
683 .query_variable_info = efi_query_variable_info,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100684};