blob: 8d1370b4dd1a4673d8a1471c4ea8108cfe879bc6 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf50149ea2016-03-04 01:10:01 +01002/*
3 * EFI application runtime services
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Graf50149ea2016-03-04 01:10:01 +01006 */
7
8#include <common.h>
9#include <command.h>
10#include <dm.h>
Alexander Grafb34662d2018-06-18 17:23:11 +020011#include <elf.h>
Alexander Graf50149ea2016-03-04 01:10:01 +010012#include <efi_loader.h>
13#include <rtc.h>
Alexander Graf50149ea2016-03-04 01:10:01 +010014
15/* For manual relocation support */
16DECLARE_GLOBAL_DATA_PTR;
17
Alexander Graf80a48002016-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 Graf3c63db92016-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 Graf50149ea2016-03-04 01:10:01 +010031
Simon Glass2d2b5b22018-06-11 23:26:40 -060032/*
Heinrich Schuchardt250b3252018-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 Glass2d2b5b22018-06-11 23:26:40 -060036 */
Alexander Grafbc028912018-06-18 17:23:08 +020037#if defined(__aarch64__)
Alexander Grafb34662d2018-06-18 17:23:11 +020038#define R_RELATIVE R_AARCH64_RELATIVE
Alexander Graf50149ea2016-03-04 01:10:01 +010039#define R_MASK 0xffffffffULL
40#define IS_RELA 1
Alexander Grafbc028912018-06-18 17:23:08 +020041#elif defined(__arm__)
Alexander Grafb34662d2018-06-18 17:23:11 +020042#define R_RELATIVE R_ARM_RELATIVE
Alexander Graf50149ea2016-03-04 01:10:01 +010043#define R_MASK 0xffULL
Heinrich Schuchardt3ce78292018-10-13 20:52:08 -070044#elif defined(__i386__)
Simon Glass65e4c0b2016-09-25 15:27:35 -060045#define R_RELATIVE R_386_RELATIVE
46#define R_MASK 0xffULL
Heinrich Schuchardt3ce78292018-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 Grafbc028912018-06-18 17:23:08 +020051#elif defined(__riscv)
Rick Chen6836adb2018-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 Grafbc028912018-06-18 17:23:08 +020062#if (__riscv_xlen == 32)
Rick Chen6836adb2018-05-28 19:06:37 +080063#define R_ABSOLUTE R_RISCV_32
64#define SYM_INDEX 8
Alexander Grafbc028912018-06-18 17:23:08 +020065#elif (__riscv_xlen == 64)
Rick Chen6836adb2018-05-28 19:06:37 +080066#define R_ABSOLUTE R_RISCV_64
67#define SYM_INDEX 32
Alexander Grafbc028912018-06-18 17:23:08 +020068#else
69#error unknown riscv target
Rick Chen6836adb2018-05-28 19:06:37 +080070#endif
Alexander Graf50149ea2016-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 Schuchardt250b3252018-09-03 19:29:41 +020087 * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI
Alexander Graf50149ea2016-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 Schuchardta39f39c2018-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 Schuchardtbcfb0e22018-07-29 15:10:11 +0200104/**
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200105 * efi_reset_system_boottime() - reset system at boot time
Heinrich Schuchardtbcfb0e22018-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 Graf80a48002016-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 Graf50149ea2016-03-04 01:10:01 +0100122{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100123 struct efi_event *evt;
124
Alexander Graf50149ea2016-03-04 01:10:01 +0100125 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
126 reset_data);
127
Heinrich Schuchardtb095f3c2018-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 Graf50149ea2016-03-04 01:10:01 +0100137 switch (reset_type) {
138 case EFI_RESET_COLD:
139 case EFI_RESET_WARM:
Heinrich Schuchardt482fc902018-02-06 22:00:22 +0100140 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf50149ea2016-03-04 01:10:01 +0100141 do_reset(NULL, 0, 0, NULL);
142 break;
143 case EFI_RESET_SHUTDOWN:
Heinrich Schuchardte706ed72018-10-16 07:44:53 +0200144#ifdef CONFIG_CMD_POWEROFF
145 do_poweroff(NULL, 0, 0, NULL);
146#endif
Alexander Graf50149ea2016-03-04 01:10:01 +0100147 break;
148 }
149
Alexander Graf80a48002016-08-16 21:08:45 +0200150 while (1) { }
Alexander Graf50149ea2016-03-04 01:10:01 +0100151}
152
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200153/**
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200154 * efi_get_time_boottime() - get current time at boot time
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200155 *
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200156 * This function implements the GetTime runtime service before
157 * SetVirtualAddressMap() is called.
158 *
Heinrich Schuchardt49de2452018-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 Schuchardtbcfb0e22018-07-29 15:10:11 +0200164 * Returns: status code
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200165 */
Alexander Graf80a48002016-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 Graf50149ea2016-03-04 01:10:01 +0100169{
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200170#ifdef CONFIG_DM_RTC
171 efi_status_t ret = EFI_SUCCESS;
Alexander Graf50149ea2016-03-04 01:10:01 +0100172 int r;
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200173 struct rtc_time tm;
Alexander Graf50149ea2016-03-04 01:10:01 +0100174 struct udevice *dev;
175
176 EFI_ENTRY("%p %p", time, capabilities);
177
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200178 if (!time) {
179 ret = EFI_INVALID_PARAMETER;
180 goto out;
181 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100182
Heinrich Schuchardt49de2452018-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 Graf50149ea2016-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 Schuchardt49de2452018-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 Graf50149ea2016-03-04 01:10:01 +0100202
Heinrich Schuchardt49de2452018-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 Graf50149ea2016-03-04 01:10:01 +0100211#else
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200212 EFI_ENTRY("%p %p", time, capabilities);
213 return EFI_EXIT(EFI_DEVICE_ERROR);
Alexander Graf50149ea2016-03-04 01:10:01 +0100214#endif
215}
216
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200217/**
218 * efi_set_time_boottime() - set current time
219 *
220 * This function implements the SetTime() runtime service before
221 * SetVirtualAddressMap() is called.
222 *
223 * See the Unified Extensible Firmware Interface (UEFI) specification
224 * for details.
225 *
226 * @time: pointer to structure to with current time
227 * Returns: status code
228 */
229static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
230{
231#ifdef CONFIG_DM_RTC
232 efi_status_t ret = EFI_SUCCESS;
233 struct rtc_time tm;
234 struct udevice *dev;
Alexander Graf80a48002016-08-16 21:08:45 +0200235
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200236 EFI_ENTRY("%p", time);
237
238 if (!time) {
239 ret = EFI_INVALID_PARAMETER;
240 goto out;
241 }
242
243 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
244 ret = EFI_UNSUPPORTED;
245 goto out;
246 }
247
248 memset(&tm, 0, sizeof(tm));
249 tm.tm_year = time->year;
250 tm.tm_mon = time->month;
251 tm.tm_mday = time->day;
252 tm.tm_hour = time->hour;
253 tm.tm_min = time->minute;
254 tm.tm_sec = time->second;
255 tm.tm_isdst = time->daylight == EFI_TIME_IN_DAYLIGHT;
256 /* Calculate day of week */
257 rtc_calc_weekday(&tm);
258
259 if (dm_rtc_set(dev, &tm))
260 ret = EFI_DEVICE_ERROR;
261out:
262 return EFI_EXIT(ret);
263#else
264 EFI_ENTRY("%p", time);
265 return EFI_EXIT(EFI_UNSUPPORTED);
266#endif
267}
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200268/**
269 * efi_reset_system() - reset system
270 *
271 * This function implements the ResetSystem() runtime service after
272 * SetVirtualAddressMap() is called. It only executes an endless loop.
273 * Boards may override the helpers below to implement reset functionality.
274 *
275 * See the Unified Extensible Firmware Interface (UEFI) specification for
276 * details.
277 *
278 * @reset_type: type of reset to perform
279 * @reset_status: status code for the reset
280 * @data_size: size of reset_data
281 * @reset_data: information about the reset
282 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200283void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf80a48002016-08-16 21:08:45 +0200284 enum efi_reset_type reset_type,
285 efi_status_t reset_status,
286 unsigned long data_size, void *reset_data)
287{
288 /* Nothing we can do */
289 while (1) { }
290}
291
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200292/**
293 * efi_reset_system_init() - initialize the reset driver
294 *
295 * Boards may override this function to initialize the reset driver.
296 */
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100297efi_status_t __weak efi_reset_system_init(void)
Alexander Graf80a48002016-08-16 21:08:45 +0200298{
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100299 return EFI_SUCCESS;
Alexander Graf80a48002016-08-16 21:08:45 +0200300}
301
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200302/**
303 * efi_get_time() - get current time
304 *
305 * This function implements the GetTime runtime service after
306 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
307 * anymore only an error code is returned.
308 *
309 * See the Unified Extensible Firmware Interface (UEFI) specification
310 * for details.
311 *
312 * @time: pointer to structure to receive current time
313 * @capabilities: pointer to structure to receive RTC properties
314 * Returns: status code
315 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200316efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf80a48002016-08-16 21:08:45 +0200317 struct efi_time *time,
318 struct efi_time_cap *capabilities)
319{
320 /* Nothing we can do */
321 return EFI_DEVICE_ERROR;
322}
323
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200324/**
325 * efi_set_time() - set current time
326 *
327 * This function implements the SetTime runtime service after
328 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
329 * anymore only an error code is returned.
330 *
331 * See the Unified Extensible Firmware Interface (UEFI) specification
332 * for details.
333 *
334 * @time: pointer to structure to with current time
335 * Returns: status code
336 */
337efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
338{
339 return EFI_UNSUPPORTED;
340}
341
Alexander Graf50149ea2016-03-04 01:10:01 +0100342struct efi_runtime_detach_list_struct {
343 void *ptr;
344 void *patchto;
345};
346
347static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
348 {
349 /* do_reset is gone */
350 .ptr = &efi_runtime_services.reset_system,
Alexander Graf80a48002016-08-16 21:08:45 +0200351 .patchto = efi_reset_system,
Alexander Graf50149ea2016-03-04 01:10:01 +0100352 }, {
353 /* invalidate_*cache_all are gone */
354 .ptr = &efi_runtime_services.set_virtual_address_map,
AKASHI Takahiro83582412018-11-14 16:18:07 +0900355 .patchto = &efi_unimplemented,
Alexander Graf50149ea2016-03-04 01:10:01 +0100356 }, {
357 /* RTC accessors are gone */
358 .ptr = &efi_runtime_services.get_time,
Alexander Graf80a48002016-08-16 21:08:45 +0200359 .patchto = &efi_get_time,
Alexander Grafae874402016-05-18 00:54:47 +0200360 }, {
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200361 .ptr = &efi_runtime_services.set_time,
362 .patchto = &efi_set_time,
363 }, {
Alexander Grafae874402016-05-18 00:54:47 +0200364 /* Clean up system table */
365 .ptr = &systab.con_in,
366 .patchto = NULL,
367 }, {
368 /* Clean up system table */
369 .ptr = &systab.con_out,
370 .patchto = NULL,
371 }, {
372 /* Clean up system table */
373 .ptr = &systab.std_err,
374 .patchto = NULL,
375 }, {
376 /* Clean up system table */
377 .ptr = &systab.boottime,
378 .patchto = NULL,
Rob Clarkad644e72017-09-13 18:05:37 -0400379 }, {
380 .ptr = &efi_runtime_services.get_variable,
381 .patchto = &efi_device_error,
382 }, {
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200383 .ptr = &efi_runtime_services.get_next_variable_name,
Rob Clarkad644e72017-09-13 18:05:37 -0400384 .patchto = &efi_device_error,
385 }, {
386 .ptr = &efi_runtime_services.set_variable,
387 .patchto = &efi_device_error,
388 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100389};
390
391static bool efi_runtime_tobedetached(void *p)
392{
393 int i;
394
395 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
396 if (efi_runtime_detach_list[i].ptr == p)
397 return true;
398
399 return false;
400}
401
402static void efi_runtime_detach(ulong offset)
403{
404 int i;
405 ulong patchoff = offset - (ulong)gd->relocaddr;
406
407 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
408 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
409 ulong *p = efi_runtime_detach_list[i].ptr;
410 ulong newaddr = patchto ? (patchto + patchoff) : 0;
411
Alexander Grafedcef3b2016-06-02 11:38:27 +0200412 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100413 *p = newaddr;
414 }
Heinrich Schuchardta39f39c2018-07-29 09:49:04 +0200415
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200416 /* Update CRC32 */
Heinrich Schuchardta39f39c2018-07-29 09:49:04 +0200417 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100418}
419
420/* Relocate EFI runtime to uboot_reloc_base = offset */
421void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
422{
423#ifdef IS_RELA
424 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
425#else
426 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
427 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
428#endif
429
Alexander Grafedcef3b2016-06-02 11:38:27 +0200430 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf50149ea2016-03-04 01:10:01 +0100431 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
432 ulong base = CONFIG_SYS_TEXT_BASE;
433 ulong *p;
434 ulong newaddr;
435
436 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
437
Heinrich Schuchardt3ce78292018-10-13 20:52:08 -0700438 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
439 rel->info, *p, rel->offset);
Alexander Graf50149ea2016-03-04 01:10:01 +0100440
Rick Chen6836adb2018-05-28 19:06:37 +0800441 switch (rel->info & R_MASK) {
442 case R_RELATIVE:
Alexander Graf50149ea2016-03-04 01:10:01 +0100443#ifdef IS_RELA
444 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
445#else
446 newaddr = *p - lastoff + offset;
447#endif
Rick Chen6836adb2018-05-28 19:06:37 +0800448 break;
449#ifdef R_ABSOLUTE
450 case R_ABSOLUTE: {
451 ulong symidx = rel->info >> SYM_INDEX;
452 extern struct dyn_sym __dyn_sym_start[];
453 newaddr = __dyn_sym_start[symidx].addr + offset;
Alexander Grafafdc4fc2018-11-04 22:25:22 +0100454#ifdef IS_RELA
455 newaddr -= CONFIG_SYS_TEXT_BASE;
456#endif
Rick Chen6836adb2018-05-28 19:06:37 +0800457 break;
458 }
459#endif
460 default:
Heinrich Schuchardt3ce78292018-10-13 20:52:08 -0700461 if (!efi_runtime_tobedetached(p))
462 printf("%s: Unknown relocation type %llx\n",
463 __func__, rel->info & R_MASK);
Rick Chen6836adb2018-05-28 19:06:37 +0800464 continue;
465 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100466
467 /* Check if the relocation is inside bounds */
468 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt591cf2e2017-09-18 22:11:34 +0200469 newaddr > (map->virtual_start +
470 (map->num_pages << EFI_PAGE_SHIFT)))) {
Alexander Graf50149ea2016-03-04 01:10:01 +0100471 if (!efi_runtime_tobedetached(p))
Heinrich Schuchardt3ce78292018-10-13 20:52:08 -0700472 printf("%s: Relocation at %p is out of "
473 "range (%lx)\n", __func__, p, newaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100474 continue;
475 }
476
Alexander Grafedcef3b2016-06-02 11:38:27 +0200477 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100478 *p = newaddr;
Alexander Graf36c37a82016-04-11 23:20:39 +0200479 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
480 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf50149ea2016-03-04 01:10:01 +0100481 }
482
483#ifndef IS_RELA
484 lastoff = offset;
485#endif
486
487 invalidate_icache_all();
488}
489
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200490/**
491 * efi_set_virtual_address_map() - change from physical to virtual mapping
492 *
493 * This function implements the SetVirtualAddressMap() runtime service.
494 *
495 * See the Unified Extensible Firmware Interface (UEFI) specification for
496 * details.
497 *
498 * @memory_map_size: size of the virtual map
499 * @descriptor_size: size of an entry in the map
500 * @descriptor_version: version of the map entries
501 * @virtmap: virtual address mapping information
502 * Return: status code
503 */
Alexander Graf50149ea2016-03-04 01:10:01 +0100504static efi_status_t EFIAPI efi_set_virtual_address_map(
505 unsigned long memory_map_size,
506 unsigned long descriptor_size,
507 uint32_t descriptor_version,
508 struct efi_mem_desc *virtmap)
509{
Alexander Graf50149ea2016-03-04 01:10:01 +0100510 int n = memory_map_size / descriptor_size;
511 int i;
Alexander Graf5c38e052018-12-11 10:00:42 +0100512 int rt_code_sections = 0;
Alexander Graf50149ea2016-03-04 01:10:01 +0100513
514 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
515 descriptor_version, virtmap);
516
Alexander Graf5c38e052018-12-11 10:00:42 +0100517 /*
518 * TODO:
519 * Further down we are cheating. While really we should implement
520 * SetVirtualAddressMap() events and ConvertPointer() to allow
521 * dynamically loaded drivers to expose runtime services, we don't
522 * today.
523 *
524 * So let's ensure we see exactly one single runtime section, as
525 * that is the built-in one. If we see more (or less), someone must
526 * have tried adding or removing to that which we don't support yet.
527 * In that case, let's better fail rather than expose broken runtime
528 * services.
529 */
530 for (i = 0; i < n; i++) {
531 struct efi_mem_desc *map = (void*)virtmap +
532 (descriptor_size * i);
533
534 if (map->type == EFI_RUNTIME_SERVICES_CODE)
535 rt_code_sections++;
536 }
537
538 if (rt_code_sections != 1) {
539 /*
540 * We expose exactly one single runtime code section, so
541 * something is definitely going wrong.
542 */
543 return EFI_EXIT(EFI_INVALID_PARAMETER);
544 }
545
Alexander Graf80a48002016-08-16 21:08:45 +0200546 /* Rebind mmio pointers */
547 for (i = 0; i < n; i++) {
548 struct efi_mem_desc *map = (void*)virtmap +
549 (descriptor_size * i);
550 struct list_head *lhandle;
551 efi_physical_addr_t map_start = map->physical_start;
552 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
553 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt07240da2018-08-04 23:16:06 +0200554 u64 off = map->virtual_start - map_start;
Alexander Graf80a48002016-08-16 21:08:45 +0200555
556 /* Adjust all mmio pointers in this region */
557 list_for_each(lhandle, &efi_runtime_mmio) {
558 struct efi_runtime_mmio_list *lmmio;
559
560 lmmio = list_entry(lhandle,
561 struct efi_runtime_mmio_list,
562 link);
563 if ((map_start <= lmmio->paddr) &&
564 (map_end >= lmmio->paddr)) {
Alexander Graf80a48002016-08-16 21:08:45 +0200565 uintptr_t new_addr = lmmio->paddr + off;
566 *lmmio->ptr = (void *)new_addr;
567 }
568 }
Heinrich Schuchardt07240da2018-08-04 23:16:06 +0200569 if ((map_start <= (uintptr_t)systab.tables) &&
570 (map_end >= (uintptr_t)systab.tables)) {
571 char *ptr = (char *)systab.tables;
572
573 ptr += off;
574 systab.tables = (struct efi_configuration_table *)ptr;
575 }
Alexander Graf80a48002016-08-16 21:08:45 +0200576 }
577
578 /* Move the actual runtime code over */
Alexander Graf50149ea2016-03-04 01:10:01 +0100579 for (i = 0; i < n; i++) {
580 struct efi_mem_desc *map;
581
582 map = (void*)virtmap + (descriptor_size * i);
583 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf80a48002016-08-16 21:08:45 +0200584 ulong new_offset = map->virtual_start -
Alexander Graf5c38e052018-12-11 10:00:42 +0100585 map->physical_start + gd->relocaddr;
Alexander Graf50149ea2016-03-04 01:10:01 +0100586
587 efi_runtime_relocate(new_offset, map);
588 /* Once we're virtual, we can no longer handle
589 complex callbacks */
590 efi_runtime_detach(new_offset);
591 return EFI_EXIT(EFI_SUCCESS);
592 }
593 }
594
595 return EFI_EXIT(EFI_INVALID_PARAMETER);
596}
597
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200598/**
599 * efi_add_runtime_mmio() - add memory-mapped IO region
600 *
601 * This function adds a memory-mapped IO region to the memory map to make it
602 * available at runtime.
603 *
Heinrich Schuchardtfb34f292018-12-23 02:35:13 +0100604 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
605 * IO region
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200606 * @len: size of the memory-mapped IO region
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200607 * Returns: status code
608 */
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100609efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf80a48002016-08-16 21:08:45 +0200610{
611 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.deaee4f062017-08-11 21:19:37 +0200612 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf813468c2018-03-15 15:08:16 +0100613 uint64_t addr = *(uintptr_t *)mmio_ptr;
614 uint64_t retaddr;
615
616 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
617 if (retaddr != addr)
618 return EFI_OUT_OF_RESOURCES;
Alexander Graf80a48002016-08-16 21:08:45 +0200619
620 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100621 if (!newmmio)
622 return EFI_OUT_OF_RESOURCES;
Alexander Graf80a48002016-08-16 21:08:45 +0200623 newmmio->ptr = mmio_ptr;
624 newmmio->paddr = *(uintptr_t *)mmio_ptr;
625 newmmio->len = len;
626 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100627
Alexander Graf813468c2018-03-15 15:08:16 +0100628 return EFI_SUCCESS;
Alexander Graf80a48002016-08-16 21:08:45 +0200629}
630
Alexander Graf50149ea2016-03-04 01:10:01 +0100631/*
632 * In the second stage, U-Boot has disappeared. To isolate our runtime code
633 * that at this point still exists from the rest, we put it into a special
634 * section.
635 *
636 * !!WARNING!!
637 *
638 * This means that we can not rely on any code outside of this file in any
639 * function or variable below this line.
640 *
641 * Please keep everything fully self-contained and annotated with
Alexander Graf3c63db92016-10-14 13:45:30 +0200642 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf50149ea2016-03-04 01:10:01 +0100643 */
644
645/*
646 * Relocate the EFI runtime stub to a different place. We need to call this
647 * the first time we expose the runtime interface to a user and on set virtual
648 * address map calls.
649 */
650
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200651/**
652 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
653 *
654 * This function is used after SetVirtualAddressMap() is called as replacement
655 * for services that are not available anymore due to constraints of the U-Boot
656 * implementation.
657 *
658 * Return: EFI_UNSUPPORTED
659 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200660static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100661{
662 return EFI_UNSUPPORTED;
663}
664
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200665/**
666 * efi_device_error() - replacement function, returns EFI_DEVICE_ERROR
667 *
668 * This function is used after SetVirtualAddressMap() is called as replacement
669 * for services that are not available anymore due to constraints of the U-Boot
670 * implementation.
671 *
672 * Return: EFI_DEVICE_ERROR
673 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200674static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100675{
676 return EFI_DEVICE_ERROR;
677}
678
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200679/**
680 * efi_invalid_parameter() - replacement function, returns EFI_INVALID_PARAMETER
681 *
682 * This function is used after SetVirtualAddressMap() is called as replacement
683 * for services that are not available anymore due to constraints of the U-Boot
684 * implementation.
685 *
686 * Return: EFI_INVALID_PARAMETER
687 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200688static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100689{
690 return EFI_INVALID_PARAMETER;
691}
692
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200693/**
694 * efi_update_capsule() - process information from operating system
695 *
696 * This function implements the UpdateCapsule() runtime service.
697 *
698 * See the Unified Extensible Firmware Interface (UEFI) specification for
699 * details.
700 *
701 * @capsule_header_array: pointer to array of virtual pointers
702 * @capsule_count: number of pointers in capsule_header_array
703 * @scatter_gather_list: pointer to arry of physical pointers
704 * Returns: status code
705 */
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100706efi_status_t __efi_runtime EFIAPI efi_update_capsule(
707 struct efi_capsule_header **capsule_header_array,
708 efi_uintn_t capsule_count,
709 u64 scatter_gather_list)
710{
711 return EFI_UNSUPPORTED;
712}
713
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200714/**
715 * efi_query_capsule_caps() - check if capsule is supported
716 *
717 * This function implements the QueryCapsuleCapabilities() runtime service.
718 *
719 * See the Unified Extensible Firmware Interface (UEFI) specification for
720 * details.
721 *
722 * @capsule_header_array: pointer to array of virtual pointers
723 * @capsule_count: number of pointers in capsule_header_array
Heinrich Schuchardtcc0bfc02018-09-03 20:59:25 +0200724 * @maximum_capsule_size: maximum capsule size
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200725 * @reset_type: type of reset needed for capsule update
726 * Returns: status code
727 */
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100728efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
729 struct efi_capsule_header **capsule_header_array,
730 efi_uintn_t capsule_count,
AKASHI Takahiro19dd9072018-11-14 16:18:53 +0900731 u64 *maximum_capsule_size,
732 u32 *reset_type)
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100733{
734 return EFI_UNSUPPORTED;
735}
736
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200737/**
738 * efi_query_variable_info() - get information about EFI variables
739 *
740 * This function implements the QueryVariableInfo() runtime service.
741 *
742 * See the Unified Extensible Firmware Interface (UEFI) specification for
743 * details.
744 *
745 * @attributes: bitmask to select variables to be
746 * queried
747 * @maximum_variable_storage_size: maximum size of storage area for the
748 * selected variable types
749 * @remaining_variable_storage_size: remaining size of storage are for the
750 * selected variable types
751 * @maximum_variable_size: maximum size of a variable of the
752 * selected type
753 * Returns: status code
754 */
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100755efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
756 u32 attributes,
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200757 u64 *maximum_variable_storage_size,
758 u64 *remaining_variable_storage_size,
759 u64 *maximum_variable_size)
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100760{
761 return EFI_UNSUPPORTED;
762}
763
Alexander Graf3c63db92016-10-14 13:45:30 +0200764struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf50149ea2016-03-04 01:10:01 +0100765 .hdr = {
766 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +0200767 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +0200768 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf50149ea2016-03-04 01:10:01 +0100769 },
Alexander Graf80a48002016-08-16 21:08:45 +0200770 .get_time = &efi_get_time_boottime,
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200771 .set_time = &efi_set_time_boottime,
Alexander Graf50149ea2016-03-04 01:10:01 +0100772 .get_wakeup_time = (void *)&efi_unimplemented,
773 .set_wakeup_time = (void *)&efi_unimplemented,
774 .set_virtual_address_map = &efi_set_virtual_address_map,
775 .convert_pointer = (void *)&efi_invalid_parameter,
Rob Clarkad644e72017-09-13 18:05:37 -0400776 .get_variable = efi_get_variable,
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200777 .get_next_variable_name = efi_get_next_variable_name,
Rob Clarkad644e72017-09-13 18:05:37 -0400778 .set_variable = efi_set_variable,
Alexander Graf50149ea2016-03-04 01:10:01 +0100779 .get_next_high_mono_count = (void *)&efi_device_error,
Alexander Graf80a48002016-08-16 21:08:45 +0200780 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100781 .update_capsule = efi_update_capsule,
782 .query_capsule_caps = efi_query_capsule_caps,
783 .query_variable_info = efi_query_variable_info,
Alexander Graf50149ea2016-03-04 01:10:01 +0100784};