blob: c5fbd91fa3867de2051cb8bc3b621931ee2e20c4 [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
Alexander Graf0fe51922018-06-18 17:23:08 +020044#elif defined(__x86_64__) || defined(__i386__)
Simon Glasscdfe6962016-09-25 15:27:35 -060045#define R_RELATIVE R_386_RELATIVE
46#define R_MASK 0xffULL
Alexander Graf0fe51922018-06-18 17:23:08 +020047#elif defined(__riscv)
Rick Chen9677a372018-05-28 19:06:37 +080048#define R_RELATIVE R_RISCV_RELATIVE
49#define R_MASK 0xffULL
50#define IS_RELA 1
51
52struct dyn_sym {
53 ulong foo1;
54 ulong addr;
55 u32 foo2;
56 u32 foo3;
57};
Alexander Graf0fe51922018-06-18 17:23:08 +020058#if (__riscv_xlen == 32)
Rick Chen9677a372018-05-28 19:06:37 +080059#define R_ABSOLUTE R_RISCV_32
60#define SYM_INDEX 8
Alexander Graf0fe51922018-06-18 17:23:08 +020061#elif (__riscv_xlen == 64)
Rick Chen9677a372018-05-28 19:06:37 +080062#define R_ABSOLUTE R_RISCV_64
63#define SYM_INDEX 32
Alexander Graf0fe51922018-06-18 17:23:08 +020064#else
65#error unknown riscv target
Rick Chen9677a372018-05-28 19:06:37 +080066#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +010067#else
68#error Need to add relocation awareness
69#endif
70
71struct elf_rel {
72 ulong *offset;
73 ulong info;
74};
75
76struct elf_rela {
77 ulong *offset;
78 ulong info;
79 long addend;
80};
81
82/*
Heinrich Schuchardt11121542018-09-03 19:29:41 +020083 * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI
Alexander Graf0bd425a2016-03-04 01:10:01 +010084 * payload are running concurrently at the same time. In this mode, we can
85 * handle a good number of runtime callbacks
86 */
87
Heinrich Schuchardt69c15762018-07-29 09:49:04 +020088/**
89 * efi_update_table_header_crc32() - Update crc32 in table header
90 *
91 * @table: EFI table
92 */
93void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
94{
95 table->crc32 = 0;
96 table->crc32 = crc32(0, (const unsigned char *)table,
97 table->headersize);
98}
99
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200100/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200101 * efi_reset_system_boottime() - reset system at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200102 *
103 * This function implements the ResetSystem() runtime service before
104 * SetVirtualAddressMap() is called.
105 *
106 * See the Unified Extensible Firmware Interface (UEFI) specification for
107 * details.
108 *
109 * @reset_type: type of reset to perform
110 * @reset_status: status code for the reset
111 * @data_size: size of reset_data
112 * @reset_data: information about the reset
113 */
Alexander Graf77256092016-08-16 21:08:45 +0200114static void EFIAPI efi_reset_system_boottime(
115 enum efi_reset_type reset_type,
116 efi_status_t reset_status,
117 unsigned long data_size, void *reset_data)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100118{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100119 struct efi_event *evt;
120
Alexander Graf0bd425a2016-03-04 01:10:01 +0100121 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
122 reset_data);
123
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100124 /* Notify reset */
125 list_for_each_entry(evt, &efi_events, link) {
126 if (evt->group &&
127 !guidcmp(evt->group,
128 &efi_guid_event_group_reset_system)) {
129 efi_signal_event(evt, false);
130 break;
131 }
132 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100133 switch (reset_type) {
134 case EFI_RESET_COLD:
135 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +0100136 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100137 do_reset(NULL, 0, 0, NULL);
138 break;
139 case EFI_RESET_SHUTDOWN:
140 /* We don't have anything to map this to */
141 break;
142 }
143
Alexander Graf77256092016-08-16 21:08:45 +0200144 while (1) { }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100145}
146
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200147/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200148 * efi_get_time_boottime() - get current time at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200149 *
150 * This function implements the GetTime runtime service before
151 * SetVirtualAddressMap() is called.
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200152 *
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200153 * See the Unified Extensible Firmware Interface (UEFI) specification
154 * for details.
155 *
156 * @time: pointer to structure to receive current time
157 * @capabilities: pointer to structure to receive RTC properties
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200158 * Returns: status code
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200159 */
Alexander Graf77256092016-08-16 21:08:45 +0200160static efi_status_t EFIAPI efi_get_time_boottime(
161 struct efi_time *time,
162 struct efi_time_cap *capabilities)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100163{
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200164#ifdef CONFIG_DM_RTC
165 efi_status_t ret = EFI_SUCCESS;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100166 int r;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200167 struct rtc_time tm;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100168 struct udevice *dev;
169
170 EFI_ENTRY("%p %p", time, capabilities);
171
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200172 if (!time) {
173 ret = EFI_INVALID_PARAMETER;
174 goto out;
175 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100176
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200177 r = uclass_get_device(UCLASS_RTC, 0, &dev);
178 if (!r)
179 r = dm_rtc_get(dev, &tm);
180 if (r) {
181 ret = EFI_DEVICE_ERROR;
182 goto out;
183 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100184
185 memset(time, 0, sizeof(*time));
186 time->year = tm.tm_year;
187 time->month = tm.tm_mon;
188 time->day = tm.tm_mday;
189 time->hour = tm.tm_hour;
190 time->minute = tm.tm_min;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200191 time->second = tm.tm_sec;
192 time->daylight = EFI_TIME_ADJUST_DAYLIGHT;
193 if (tm.tm_isdst > 0)
194 time->daylight |= EFI_TIME_IN_DAYLIGHT;
195 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100196
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200197 if (capabilities) {
198 /* Set reasonable dummy values */
199 capabilities->resolution = 1; /* 1 Hz */
200 capabilities->accuracy = 100000000; /* 100 ppm */
201 capabilities->sets_to_zero = false;
202 }
203out:
204 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100205#else
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200206 EFI_ENTRY("%p %p", time, capabilities);
207 return EFI_EXIT(EFI_DEVICE_ERROR);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100208#endif
209}
210
Alexander Graf77256092016-08-16 21:08:45 +0200211
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200212/**
213 * efi_reset_system() - reset system
214 *
215 * This function implements the ResetSystem() runtime service after
216 * SetVirtualAddressMap() is called. It only executes an endless loop.
217 * Boards may override the helpers below to implement reset functionality.
218 *
219 * See the Unified Extensible Firmware Interface (UEFI) specification for
220 * details.
221 *
222 * @reset_type: type of reset to perform
223 * @reset_status: status code for the reset
224 * @data_size: size of reset_data
225 * @reset_data: information about the reset
226 */
Alexander Graf393dd912016-10-14 13:45:30 +0200227void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200228 enum efi_reset_type reset_type,
229 efi_status_t reset_status,
230 unsigned long data_size, void *reset_data)
231{
232 /* Nothing we can do */
233 while (1) { }
234}
235
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200236/**
237 * efi_reset_system_init() - initialize the reset driver
238 *
239 * Boards may override this function to initialize the reset driver.
240 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100241efi_status_t __weak efi_reset_system_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200242{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100243 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200244}
245
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200246/**
247 * efi_get_time() - get current time
248 *
249 * This function implements the GetTime runtime service after
250 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
251 * anymore only an error code is returned.
252 *
253 * See the Unified Extensible Firmware Interface (UEFI) specification
254 * for details.
255 *
256 * @time: pointer to structure to receive current time
257 * @capabilities: pointer to structure to receive RTC properties
258 * Returns: status code
259 */
Alexander Graf393dd912016-10-14 13:45:30 +0200260efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200261 struct efi_time *time,
262 struct efi_time_cap *capabilities)
263{
264 /* Nothing we can do */
265 return EFI_DEVICE_ERROR;
266}
267
Alexander Graf0bd425a2016-03-04 01:10:01 +0100268struct efi_runtime_detach_list_struct {
269 void *ptr;
270 void *patchto;
271};
272
273static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
274 {
275 /* do_reset is gone */
276 .ptr = &efi_runtime_services.reset_system,
Alexander Graf77256092016-08-16 21:08:45 +0200277 .patchto = efi_reset_system,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100278 }, {
279 /* invalidate_*cache_all are gone */
280 .ptr = &efi_runtime_services.set_virtual_address_map,
281 .patchto = &efi_invalid_parameter,
282 }, {
283 /* RTC accessors are gone */
284 .ptr = &efi_runtime_services.get_time,
Alexander Graf77256092016-08-16 21:08:45 +0200285 .patchto = &efi_get_time,
Alexander Grafc59d3602016-05-18 00:54:47 +0200286 }, {
287 /* Clean up system table */
288 .ptr = &systab.con_in,
289 .patchto = NULL,
290 }, {
291 /* Clean up system table */
292 .ptr = &systab.con_out,
293 .patchto = NULL,
294 }, {
295 /* Clean up system table */
296 .ptr = &systab.std_err,
297 .patchto = NULL,
298 }, {
299 /* Clean up system table */
300 .ptr = &systab.boottime,
301 .patchto = NULL,
Rob Clark15f3d742017-09-13 18:05:37 -0400302 }, {
303 .ptr = &efi_runtime_services.get_variable,
304 .patchto = &efi_device_error,
305 }, {
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200306 .ptr = &efi_runtime_services.get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400307 .patchto = &efi_device_error,
308 }, {
309 .ptr = &efi_runtime_services.set_variable,
310 .patchto = &efi_device_error,
311 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100312};
313
314static bool efi_runtime_tobedetached(void *p)
315{
316 int i;
317
318 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
319 if (efi_runtime_detach_list[i].ptr == p)
320 return true;
321
322 return false;
323}
324
325static void efi_runtime_detach(ulong offset)
326{
327 int i;
328 ulong patchoff = offset - (ulong)gd->relocaddr;
329
330 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
331 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
332 ulong *p = efi_runtime_detach_list[i].ptr;
333 ulong newaddr = patchto ? (patchto + patchoff) : 0;
334
Alexander Graf62a67482016-06-02 11:38:27 +0200335 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100336 *p = newaddr;
337 }
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200338
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200339 /* Update CRC32 */
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200340 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100341}
342
343/* Relocate EFI runtime to uboot_reloc_base = offset */
344void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
345{
346#ifdef IS_RELA
347 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
348#else
349 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
350 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
351#endif
352
Alexander Graf62a67482016-06-02 11:38:27 +0200353 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100354 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
355 ulong base = CONFIG_SYS_TEXT_BASE;
356 ulong *p;
357 ulong newaddr;
358
359 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
360
Rick Chen9677a372018-05-28 19:06:37 +0800361 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, rel->info, *p, rel->offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100362
Rick Chen9677a372018-05-28 19:06:37 +0800363 switch (rel->info & R_MASK) {
364 case R_RELATIVE:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100365#ifdef IS_RELA
366 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
367#else
368 newaddr = *p - lastoff + offset;
369#endif
Rick Chen9677a372018-05-28 19:06:37 +0800370 break;
371#ifdef R_ABSOLUTE
372 case R_ABSOLUTE: {
373 ulong symidx = rel->info >> SYM_INDEX;
374 extern struct dyn_sym __dyn_sym_start[];
375 newaddr = __dyn_sym_start[symidx].addr + offset;
376 break;
377 }
378#endif
379 default:
380 continue;
381 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100382
383 /* Check if the relocation is inside bounds */
384 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200385 newaddr > (map->virtual_start +
386 (map->num_pages << EFI_PAGE_SHIFT)))) {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100387 if (!efi_runtime_tobedetached(p))
388 printf("U-Boot EFI: Relocation at %p is out of "
389 "range (%lx)\n", p, newaddr);
390 continue;
391 }
392
Alexander Graf62a67482016-06-02 11:38:27 +0200393 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100394 *p = newaddr;
Alexander Grafe5702322016-04-11 23:20:39 +0200395 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
396 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf0bd425a2016-03-04 01:10:01 +0100397 }
398
399#ifndef IS_RELA
400 lastoff = offset;
401#endif
402
403 invalidate_icache_all();
404}
405
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200406/**
407 * efi_set_virtual_address_map() - change from physical to virtual mapping
408 *
409 * This function implements the SetVirtualAddressMap() runtime service.
410 *
411 * See the Unified Extensible Firmware Interface (UEFI) specification for
412 * details.
413 *
414 * @memory_map_size: size of the virtual map
415 * @descriptor_size: size of an entry in the map
416 * @descriptor_version: version of the map entries
417 * @virtmap: virtual address mapping information
418 * Return: status code
419 */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100420static efi_status_t EFIAPI efi_set_virtual_address_map(
421 unsigned long memory_map_size,
422 unsigned long descriptor_size,
423 uint32_t descriptor_version,
424 struct efi_mem_desc *virtmap)
425{
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200426 ulong runtime_start = (ulong)&__efi_runtime_start &
427 ~(ulong)EFI_PAGE_MASK;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100428 int n = memory_map_size / descriptor_size;
429 int i;
430
431 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
432 descriptor_version, virtmap);
433
Alexander Graf77256092016-08-16 21:08:45 +0200434 /* Rebind mmio pointers */
435 for (i = 0; i < n; i++) {
436 struct efi_mem_desc *map = (void*)virtmap +
437 (descriptor_size * i);
438 struct list_head *lhandle;
439 efi_physical_addr_t map_start = map->physical_start;
440 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
441 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200442 u64 off = map->virtual_start - map_start;
Alexander Graf77256092016-08-16 21:08:45 +0200443
444 /* Adjust all mmio pointers in this region */
445 list_for_each(lhandle, &efi_runtime_mmio) {
446 struct efi_runtime_mmio_list *lmmio;
447
448 lmmio = list_entry(lhandle,
449 struct efi_runtime_mmio_list,
450 link);
451 if ((map_start <= lmmio->paddr) &&
452 (map_end >= lmmio->paddr)) {
Alexander Graf77256092016-08-16 21:08:45 +0200453 uintptr_t new_addr = lmmio->paddr + off;
454 *lmmio->ptr = (void *)new_addr;
455 }
456 }
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200457 if ((map_start <= (uintptr_t)systab.tables) &&
458 (map_end >= (uintptr_t)systab.tables)) {
459 char *ptr = (char *)systab.tables;
460
461 ptr += off;
462 systab.tables = (struct efi_configuration_table *)ptr;
463 }
Alexander Graf77256092016-08-16 21:08:45 +0200464 }
465
466 /* Move the actual runtime code over */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100467 for (i = 0; i < n; i++) {
468 struct efi_mem_desc *map;
469
470 map = (void*)virtmap + (descriptor_size * i);
471 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf77256092016-08-16 21:08:45 +0200472 ulong new_offset = map->virtual_start -
473 (runtime_start - gd->relocaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100474
475 efi_runtime_relocate(new_offset, map);
476 /* Once we're virtual, we can no longer handle
477 complex callbacks */
478 efi_runtime_detach(new_offset);
479 return EFI_EXIT(EFI_SUCCESS);
480 }
481 }
482
483 return EFI_EXIT(EFI_INVALID_PARAMETER);
484}
485
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200486/**
487 * efi_add_runtime_mmio() - add memory-mapped IO region
488 *
489 * This function adds a memory-mapped IO region to the memory map to make it
490 * available at runtime.
491 *
492 * @mmio_ptr: address of the memory-mapped IO region
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200493 * @len: size of the memory-mapped IO region
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200494 * Returns: status code
495 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100496efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf77256092016-08-16 21:08:45 +0200497{
498 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.de93fcc7f2017-08-11 21:19:37 +0200499 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf33a83a32018-03-15 15:08:16 +0100500 uint64_t addr = *(uintptr_t *)mmio_ptr;
501 uint64_t retaddr;
502
503 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
504 if (retaddr != addr)
505 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200506
507 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100508 if (!newmmio)
509 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200510 newmmio->ptr = mmio_ptr;
511 newmmio->paddr = *(uintptr_t *)mmio_ptr;
512 newmmio->len = len;
513 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100514
Alexander Graf33a83a32018-03-15 15:08:16 +0100515 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200516}
517
Alexander Graf0bd425a2016-03-04 01:10:01 +0100518/*
519 * In the second stage, U-Boot has disappeared. To isolate our runtime code
520 * that at this point still exists from the rest, we put it into a special
521 * section.
522 *
523 * !!WARNING!!
524 *
525 * This means that we can not rely on any code outside of this file in any
526 * function or variable below this line.
527 *
528 * Please keep everything fully self-contained and annotated with
Alexander Graf393dd912016-10-14 13:45:30 +0200529 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf0bd425a2016-03-04 01:10:01 +0100530 */
531
532/*
533 * Relocate the EFI runtime stub to a different place. We need to call this
534 * the first time we expose the runtime interface to a user and on set virtual
535 * address map calls.
536 */
537
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200538/**
539 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
540 *
541 * This function is used after SetVirtualAddressMap() is called as replacement
542 * for services that are not available anymore due to constraints of the U-Boot
543 * implementation.
544 *
545 * Return: EFI_UNSUPPORTED
546 */
Alexander Graf393dd912016-10-14 13:45:30 +0200547static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100548{
549 return EFI_UNSUPPORTED;
550}
551
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200552/**
553 * efi_device_error() - replacement function, returns EFI_DEVICE_ERROR
554 *
555 * This function is used after SetVirtualAddressMap() is called as replacement
556 * for services that are not available anymore due to constraints of the U-Boot
557 * implementation.
558 *
559 * Return: EFI_DEVICE_ERROR
560 */
Alexander Graf393dd912016-10-14 13:45:30 +0200561static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100562{
563 return EFI_DEVICE_ERROR;
564}
565
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200566/**
567 * efi_invalid_parameter() - replacement function, returns EFI_INVALID_PARAMETER
568 *
569 * This function is used after SetVirtualAddressMap() is called as replacement
570 * for services that are not available anymore due to constraints of the U-Boot
571 * implementation.
572 *
573 * Return: EFI_INVALID_PARAMETER
574 */
Alexander Graf393dd912016-10-14 13:45:30 +0200575static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100576{
577 return EFI_INVALID_PARAMETER;
578}
579
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200580/**
581 * efi_update_capsule() - process information from operating system
582 *
583 * This function implements the UpdateCapsule() runtime service.
584 *
585 * See the Unified Extensible Firmware Interface (UEFI) specification for
586 * details.
587 *
588 * @capsule_header_array: pointer to array of virtual pointers
589 * @capsule_count: number of pointers in capsule_header_array
590 * @scatter_gather_list: pointer to arry of physical pointers
591 * Returns: status code
592 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100593efi_status_t __efi_runtime EFIAPI efi_update_capsule(
594 struct efi_capsule_header **capsule_header_array,
595 efi_uintn_t capsule_count,
596 u64 scatter_gather_list)
597{
598 return EFI_UNSUPPORTED;
599}
600
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200601/**
602 * efi_query_capsule_caps() - check if capsule is supported
603 *
604 * This function implements the QueryCapsuleCapabilities() runtime service.
605 *
606 * See the Unified Extensible Firmware Interface (UEFI) specification for
607 * details.
608 *
609 * @capsule_header_array: pointer to array of virtual pointers
610 * @capsule_count: number of pointers in capsule_header_array
Heinrich Schuchardt1d1be362018-09-03 20:59:25 +0200611 * @maximum_capsule_size: maximum capsule size
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200612 * @reset_type: type of reset needed for capsule update
613 * Returns: status code
614 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100615efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
616 struct efi_capsule_header **capsule_header_array,
617 efi_uintn_t capsule_count,
618 u64 maximum_capsule_size,
619 u32 reset_type)
620{
621 return EFI_UNSUPPORTED;
622}
623
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200624/**
625 * efi_query_variable_info() - get information about EFI variables
626 *
627 * This function implements the QueryVariableInfo() runtime service.
628 *
629 * See the Unified Extensible Firmware Interface (UEFI) specification for
630 * details.
631 *
632 * @attributes: bitmask to select variables to be
633 * queried
634 * @maximum_variable_storage_size: maximum size of storage area for the
635 * selected variable types
636 * @remaining_variable_storage_size: remaining size of storage are for the
637 * selected variable types
638 * @maximum_variable_size: maximum size of a variable of the
639 * selected type
640 * Returns: status code
641 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100642efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
643 u32 attributes,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200644 u64 *maximum_variable_storage_size,
645 u64 *remaining_variable_storage_size,
646 u64 *maximum_variable_size)
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100647{
648 return EFI_UNSUPPORTED;
649}
650
Alexander Graf393dd912016-10-14 13:45:30 +0200651struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100652 .hdr = {
653 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +0200654 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +0200655 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf0bd425a2016-03-04 01:10:01 +0100656 },
Alexander Graf77256092016-08-16 21:08:45 +0200657 .get_time = &efi_get_time_boottime,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100658 .set_time = (void *)&efi_device_error,
659 .get_wakeup_time = (void *)&efi_unimplemented,
660 .set_wakeup_time = (void *)&efi_unimplemented,
661 .set_virtual_address_map = &efi_set_virtual_address_map,
662 .convert_pointer = (void *)&efi_invalid_parameter,
Rob Clark15f3d742017-09-13 18:05:37 -0400663 .get_variable = efi_get_variable,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200664 .get_next_variable_name = efi_get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400665 .set_variable = efi_set_variable,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100666 .get_next_high_mono_count = (void *)&efi_device_error,
Alexander Graf77256092016-08-16 21:08:45 +0200667 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100668 .update_capsule = efi_update_capsule,
669 .query_capsule_caps = efi_query_capsule_caps,
670 .query_variable_info = efi_query_variable_info,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100671};