blob: 27136cbedd79507c34505023de3bb46969768a47 [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
Heinrich Schuchardt69c15762018-07-29 09:49:04 +020087/**
88 * efi_update_table_header_crc32() - Update crc32 in table header
89 *
90 * @table: EFI table
91 */
92void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
93{
94 table->crc32 = 0;
95 table->crc32 = crc32(0, (const unsigned char *)table,
96 table->headersize);
97}
98
Heinrich Schuchardt711853b2018-07-29 15:10:11 +020099/**
100 * efi_reset_system_boottime() - reset system at boottime
101 *
102 * This function implements the ResetSystem() runtime service before
103 * SetVirtualAddressMap() is called.
104 *
105 * See the Unified Extensible Firmware Interface (UEFI) specification for
106 * details.
107 *
108 * @reset_type: type of reset to perform
109 * @reset_status: status code for the reset
110 * @data_size: size of reset_data
111 * @reset_data: information about the reset
112 */
Alexander Graf77256092016-08-16 21:08:45 +0200113static void EFIAPI efi_reset_system_boottime(
114 enum efi_reset_type reset_type,
115 efi_status_t reset_status,
116 unsigned long data_size, void *reset_data)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100117{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100118 struct efi_event *evt;
119
Alexander Graf0bd425a2016-03-04 01:10:01 +0100120 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
121 reset_data);
122
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100123 /* Notify reset */
124 list_for_each_entry(evt, &efi_events, link) {
125 if (evt->group &&
126 !guidcmp(evt->group,
127 &efi_guid_event_group_reset_system)) {
128 efi_signal_event(evt, false);
129 break;
130 }
131 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100132 switch (reset_type) {
133 case EFI_RESET_COLD:
134 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +0100135 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100136 do_reset(NULL, 0, 0, NULL);
137 break;
138 case EFI_RESET_SHUTDOWN:
139 /* We don't have anything to map this to */
140 break;
141 }
142
Alexander Graf77256092016-08-16 21:08:45 +0200143 while (1) { }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100144}
145
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200146/**
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200147 * efi_get_time_boottime() - get current time at boottime
148 *
149 * This function implements the GetTime runtime service before
150 * SetVirtualAddressMap() is called.
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200151 *
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200152 * See the Unified Extensible Firmware Interface (UEFI) specification
153 * for details.
154 *
155 * @time: pointer to structure to receive current time
156 * @capabilities: pointer to structure to receive RTC properties
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200157 * Returns: status code
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200158 */
Alexander Graf77256092016-08-16 21:08:45 +0200159static efi_status_t EFIAPI efi_get_time_boottime(
160 struct efi_time *time,
161 struct efi_time_cap *capabilities)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100162{
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200163#ifdef CONFIG_DM_RTC
164 efi_status_t ret = EFI_SUCCESS;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100165 int r;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200166 struct rtc_time tm;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100167 struct udevice *dev;
168
169 EFI_ENTRY("%p %p", time, capabilities);
170
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200171 if (!time) {
172 ret = EFI_INVALID_PARAMETER;
173 goto out;
174 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100175
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200176 r = uclass_get_device(UCLASS_RTC, 0, &dev);
177 if (!r)
178 r = dm_rtc_get(dev, &tm);
179 if (r) {
180 ret = EFI_DEVICE_ERROR;
181 goto out;
182 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100183
184 memset(time, 0, sizeof(*time));
185 time->year = tm.tm_year;
186 time->month = tm.tm_mon;
187 time->day = tm.tm_mday;
188 time->hour = tm.tm_hour;
189 time->minute = tm.tm_min;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200190 time->second = tm.tm_sec;
191 time->daylight = EFI_TIME_ADJUST_DAYLIGHT;
192 if (tm.tm_isdst > 0)
193 time->daylight |= EFI_TIME_IN_DAYLIGHT;
194 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100195
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200196 if (capabilities) {
197 /* Set reasonable dummy values */
198 capabilities->resolution = 1; /* 1 Hz */
199 capabilities->accuracy = 100000000; /* 100 ppm */
200 capabilities->sets_to_zero = false;
201 }
202out:
203 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100204#else
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200205 EFI_ENTRY("%p %p", time, capabilities);
206 return EFI_EXIT(EFI_DEVICE_ERROR);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100207#endif
208}
209
Alexander Graf77256092016-08-16 21:08:45 +0200210
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200211/**
212 * efi_reset_system() - reset system
213 *
214 * This function implements the ResetSystem() runtime service after
215 * SetVirtualAddressMap() is called. It only executes an endless loop.
216 * Boards may override the helpers below to implement reset functionality.
217 *
218 * See the Unified Extensible Firmware Interface (UEFI) specification for
219 * details.
220 *
221 * @reset_type: type of reset to perform
222 * @reset_status: status code for the reset
223 * @data_size: size of reset_data
224 * @reset_data: information about the reset
225 */
Alexander Graf393dd912016-10-14 13:45:30 +0200226void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200227 enum efi_reset_type reset_type,
228 efi_status_t reset_status,
229 unsigned long data_size, void *reset_data)
230{
231 /* Nothing we can do */
232 while (1) { }
233}
234
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200235/**
236 * efi_reset_system_init() - initialize the reset driver
237 *
238 * Boards may override this function to initialize the reset driver.
239 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100240efi_status_t __weak efi_reset_system_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200241{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100242 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200243}
244
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200245/**
246 * efi_get_time() - get current time
247 *
248 * This function implements the GetTime runtime service after
249 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
250 * anymore only an error code is returned.
251 *
252 * See the Unified Extensible Firmware Interface (UEFI) specification
253 * for details.
254 *
255 * @time: pointer to structure to receive current time
256 * @capabilities: pointer to structure to receive RTC properties
257 * Returns: status code
258 */
Alexander Graf393dd912016-10-14 13:45:30 +0200259efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200260 struct efi_time *time,
261 struct efi_time_cap *capabilities)
262{
263 /* Nothing we can do */
264 return EFI_DEVICE_ERROR;
265}
266
Alexander Graf0bd425a2016-03-04 01:10:01 +0100267struct efi_runtime_detach_list_struct {
268 void *ptr;
269 void *patchto;
270};
271
272static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
273 {
274 /* do_reset is gone */
275 .ptr = &efi_runtime_services.reset_system,
Alexander Graf77256092016-08-16 21:08:45 +0200276 .patchto = efi_reset_system,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100277 }, {
278 /* invalidate_*cache_all are gone */
279 .ptr = &efi_runtime_services.set_virtual_address_map,
280 .patchto = &efi_invalid_parameter,
281 }, {
282 /* RTC accessors are gone */
283 .ptr = &efi_runtime_services.get_time,
Alexander Graf77256092016-08-16 21:08:45 +0200284 .patchto = &efi_get_time,
Alexander Grafc59d3602016-05-18 00:54:47 +0200285 }, {
286 /* Clean up system table */
287 .ptr = &systab.con_in,
288 .patchto = NULL,
289 }, {
290 /* Clean up system table */
291 .ptr = &systab.con_out,
292 .patchto = NULL,
293 }, {
294 /* Clean up system table */
295 .ptr = &systab.std_err,
296 .patchto = NULL,
297 }, {
298 /* Clean up system table */
299 .ptr = &systab.boottime,
300 .patchto = NULL,
Rob Clark15f3d742017-09-13 18:05:37 -0400301 }, {
302 .ptr = &efi_runtime_services.get_variable,
303 .patchto = &efi_device_error,
304 }, {
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200305 .ptr = &efi_runtime_services.get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400306 .patchto = &efi_device_error,
307 }, {
308 .ptr = &efi_runtime_services.set_variable,
309 .patchto = &efi_device_error,
310 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100311};
312
313static bool efi_runtime_tobedetached(void *p)
314{
315 int i;
316
317 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
318 if (efi_runtime_detach_list[i].ptr == p)
319 return true;
320
321 return false;
322}
323
324static void efi_runtime_detach(ulong offset)
325{
326 int i;
327 ulong patchoff = offset - (ulong)gd->relocaddr;
328
329 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
330 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
331 ulong *p = efi_runtime_detach_list[i].ptr;
332 ulong newaddr = patchto ? (patchto + patchoff) : 0;
333
Alexander Graf62a67482016-06-02 11:38:27 +0200334 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100335 *p = newaddr;
336 }
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200337
338 /* Update crc32 */
339 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100340}
341
342/* Relocate EFI runtime to uboot_reloc_base = offset */
343void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
344{
345#ifdef IS_RELA
346 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
347#else
348 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
349 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
350#endif
351
Alexander Graf62a67482016-06-02 11:38:27 +0200352 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100353 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
354 ulong base = CONFIG_SYS_TEXT_BASE;
355 ulong *p;
356 ulong newaddr;
357
358 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
359
Rick Chen9677a372018-05-28 19:06:37 +0800360 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, rel->info, *p, rel->offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100361
Rick Chen9677a372018-05-28 19:06:37 +0800362 switch (rel->info & R_MASK) {
363 case R_RELATIVE:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100364#ifdef IS_RELA
365 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
366#else
367 newaddr = *p - lastoff + offset;
368#endif
Rick Chen9677a372018-05-28 19:06:37 +0800369 break;
370#ifdef R_ABSOLUTE
371 case R_ABSOLUTE: {
372 ulong symidx = rel->info >> SYM_INDEX;
373 extern struct dyn_sym __dyn_sym_start[];
374 newaddr = __dyn_sym_start[symidx].addr + offset;
375 break;
376 }
377#endif
378 default:
379 continue;
380 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100381
382 /* Check if the relocation is inside bounds */
383 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200384 newaddr > (map->virtual_start +
385 (map->num_pages << EFI_PAGE_SHIFT)))) {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100386 if (!efi_runtime_tobedetached(p))
387 printf("U-Boot EFI: Relocation at %p is out of "
388 "range (%lx)\n", p, newaddr);
389 continue;
390 }
391
Alexander Graf62a67482016-06-02 11:38:27 +0200392 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100393 *p = newaddr;
Alexander Grafe5702322016-04-11 23:20:39 +0200394 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
395 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf0bd425a2016-03-04 01:10:01 +0100396 }
397
398#ifndef IS_RELA
399 lastoff = offset;
400#endif
401
402 invalidate_icache_all();
403}
404
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200405/**
406 * efi_set_virtual_address_map() - change from physical to virtual mapping
407 *
408 * This function implements the SetVirtualAddressMap() runtime service.
409 *
410 * See the Unified Extensible Firmware Interface (UEFI) specification for
411 * details.
412 *
413 * @memory_map_size: size of the virtual map
414 * @descriptor_size: size of an entry in the map
415 * @descriptor_version: version of the map entries
416 * @virtmap: virtual address mapping information
417 * Return: status code
418 */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100419static efi_status_t EFIAPI efi_set_virtual_address_map(
420 unsigned long memory_map_size,
421 unsigned long descriptor_size,
422 uint32_t descriptor_version,
423 struct efi_mem_desc *virtmap)
424{
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200425 ulong runtime_start = (ulong)&__efi_runtime_start &
426 ~(ulong)EFI_PAGE_MASK;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100427 int n = memory_map_size / descriptor_size;
428 int i;
429
430 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
431 descriptor_version, virtmap);
432
Alexander Graf77256092016-08-16 21:08:45 +0200433 /* Rebind mmio pointers */
434 for (i = 0; i < n; i++) {
435 struct efi_mem_desc *map = (void*)virtmap +
436 (descriptor_size * i);
437 struct list_head *lhandle;
438 efi_physical_addr_t map_start = map->physical_start;
439 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
440 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200441 u64 off = map->virtual_start - map_start;
Alexander Graf77256092016-08-16 21:08:45 +0200442
443 /* Adjust all mmio pointers in this region */
444 list_for_each(lhandle, &efi_runtime_mmio) {
445 struct efi_runtime_mmio_list *lmmio;
446
447 lmmio = list_entry(lhandle,
448 struct efi_runtime_mmio_list,
449 link);
450 if ((map_start <= lmmio->paddr) &&
451 (map_end >= lmmio->paddr)) {
Alexander Graf77256092016-08-16 21:08:45 +0200452 uintptr_t new_addr = lmmio->paddr + off;
453 *lmmio->ptr = (void *)new_addr;
454 }
455 }
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200456 if ((map_start <= (uintptr_t)systab.tables) &&
457 (map_end >= (uintptr_t)systab.tables)) {
458 char *ptr = (char *)systab.tables;
459
460 ptr += off;
461 systab.tables = (struct efi_configuration_table *)ptr;
462 }
Alexander Graf77256092016-08-16 21:08:45 +0200463 }
464
465 /* Move the actual runtime code over */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100466 for (i = 0; i < n; i++) {
467 struct efi_mem_desc *map;
468
469 map = (void*)virtmap + (descriptor_size * i);
470 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf77256092016-08-16 21:08:45 +0200471 ulong new_offset = map->virtual_start -
472 (runtime_start - gd->relocaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100473
474 efi_runtime_relocate(new_offset, map);
475 /* Once we're virtual, we can no longer handle
476 complex callbacks */
477 efi_runtime_detach(new_offset);
478 return EFI_EXIT(EFI_SUCCESS);
479 }
480 }
481
482 return EFI_EXIT(EFI_INVALID_PARAMETER);
483}
484
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200485/**
486 * efi_add_runtime_mmio() - add memory-mapped IO region
487 *
488 * This function adds a memory-mapped IO region to the memory map to make it
489 * available at runtime.
490 *
491 * @mmio_ptr: address of the memory-mapped IO region
492 * @len: size of thememory-mapped IO region
493 * Returns: status code
494 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100495efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf77256092016-08-16 21:08:45 +0200496{
497 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.de93fcc7f2017-08-11 21:19:37 +0200498 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf33a83a32018-03-15 15:08:16 +0100499 uint64_t addr = *(uintptr_t *)mmio_ptr;
500 uint64_t retaddr;
501
502 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
503 if (retaddr != addr)
504 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200505
506 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100507 if (!newmmio)
508 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200509 newmmio->ptr = mmio_ptr;
510 newmmio->paddr = *(uintptr_t *)mmio_ptr;
511 newmmio->len = len;
512 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100513
Alexander Graf33a83a32018-03-15 15:08:16 +0100514 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200515}
516
Alexander Graf0bd425a2016-03-04 01:10:01 +0100517/*
518 * In the second stage, U-Boot has disappeared. To isolate our runtime code
519 * that at this point still exists from the rest, we put it into a special
520 * section.
521 *
522 * !!WARNING!!
523 *
524 * This means that we can not rely on any code outside of this file in any
525 * function or variable below this line.
526 *
527 * Please keep everything fully self-contained and annotated with
Alexander Graf393dd912016-10-14 13:45:30 +0200528 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf0bd425a2016-03-04 01:10:01 +0100529 */
530
531/*
532 * Relocate the EFI runtime stub to a different place. We need to call this
533 * the first time we expose the runtime interface to a user and on set virtual
534 * address map calls.
535 */
536
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200537/**
538 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
539 *
540 * This function is used after SetVirtualAddressMap() is called as replacement
541 * for services that are not available anymore due to constraints of the U-Boot
542 * implementation.
543 *
544 * Return: EFI_UNSUPPORTED
545 */
Alexander Graf393dd912016-10-14 13:45:30 +0200546static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100547{
548 return EFI_UNSUPPORTED;
549}
550
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200551/**
552 * efi_device_error() - replacement function, returns EFI_DEVICE_ERROR
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_DEVICE_ERROR
559 */
Alexander Graf393dd912016-10-14 13:45:30 +0200560static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100561{
562 return EFI_DEVICE_ERROR;
563}
564
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200565/**
566 * efi_invalid_parameter() - replacement function, returns EFI_INVALID_PARAMETER
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_INVALID_PARAMETER
573 */
Alexander Graf393dd912016-10-14 13:45:30 +0200574static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100575{
576 return EFI_INVALID_PARAMETER;
577}
578
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200579/**
580 * efi_update_capsule() - process information from operating system
581 *
582 * This function implements the UpdateCapsule() runtime service.
583 *
584 * See the Unified Extensible Firmware Interface (UEFI) specification for
585 * details.
586 *
587 * @capsule_header_array: pointer to array of virtual pointers
588 * @capsule_count: number of pointers in capsule_header_array
589 * @scatter_gather_list: pointer to arry of physical pointers
590 * Returns: status code
591 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100592efi_status_t __efi_runtime EFIAPI efi_update_capsule(
593 struct efi_capsule_header **capsule_header_array,
594 efi_uintn_t capsule_count,
595 u64 scatter_gather_list)
596{
597 return EFI_UNSUPPORTED;
598}
599
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200600/**
601 * efi_query_capsule_caps() - check if capsule is supported
602 *
603 * This function implements the QueryCapsuleCapabilities() runtime service.
604 *
605 * See the Unified Extensible Firmware Interface (UEFI) specification for
606 * details.
607 *
608 * @capsule_header_array: pointer to array of virtual pointers
609 * @capsule_count: number of pointers in capsule_header_array
610 * @capsule_size: maximum capsule size
611 * @reset_type: type of reset needed for capsule update
612 * Returns: status code
613 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100614efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
615 struct efi_capsule_header **capsule_header_array,
616 efi_uintn_t capsule_count,
617 u64 maximum_capsule_size,
618 u32 reset_type)
619{
620 return EFI_UNSUPPORTED;
621}
622
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200623/**
624 * efi_query_variable_info() - get information about EFI variables
625 *
626 * This function implements the QueryVariableInfo() runtime service.
627 *
628 * See the Unified Extensible Firmware Interface (UEFI) specification for
629 * details.
630 *
631 * @attributes: bitmask to select variables to be
632 * queried
633 * @maximum_variable_storage_size: maximum size of storage area for the
634 * selected variable types
635 * @remaining_variable_storage_size: remaining size of storage are for the
636 * selected variable types
637 * @maximum_variable_size: maximum size of a variable of the
638 * selected type
639 * Returns: status code
640 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100641efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
642 u32 attributes,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200643 u64 *maximum_variable_storage_size,
644 u64 *remaining_variable_storage_size,
645 u64 *maximum_variable_size)
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100646{
647 return EFI_UNSUPPORTED;
648}
649
Alexander Graf393dd912016-10-14 13:45:30 +0200650struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100651 .hdr = {
652 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +0200653 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +0200654 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf0bd425a2016-03-04 01:10:01 +0100655 },
Alexander Graf77256092016-08-16 21:08:45 +0200656 .get_time = &efi_get_time_boottime,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100657 .set_time = (void *)&efi_device_error,
658 .get_wakeup_time = (void *)&efi_unimplemented,
659 .set_wakeup_time = (void *)&efi_unimplemented,
660 .set_virtual_address_map = &efi_set_virtual_address_map,
661 .convert_pointer = (void *)&efi_invalid_parameter,
Rob Clark15f3d742017-09-13 18:05:37 -0400662 .get_variable = efi_get_variable,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200663 .get_next_variable_name = efi_get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400664 .set_variable = efi_set_variable,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100665 .get_next_high_mono_count = (void *)&efi_device_error,
Alexander Graf77256092016-08-16 21:08:45 +0200666 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100667 .update_capsule = efi_update_capsule,
668 .query_capsule_caps = efi_query_capsule_caps,
669 .query_variable_info = efi_query_variable_info,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100670};