blob: 98ab4de03daf131c60fd3624e0fab36fda4e138f [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;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200172 struct rtc_time tm;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100173 struct udevice *dev;
174
175 EFI_ENTRY("%p %p", time, capabilities);
176
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200177 if (!time) {
178 ret = EFI_INVALID_PARAMETER;
179 goto out;
180 }
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200181 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
182 dm_rtc_get(dev, &tm)) {
183 ret = EFI_UNSUPPORTED;
184 goto out;
185 }
186 if (dm_rtc_get(dev, &tm)) {
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200187 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;
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200198 if (tm.tm_isdst)
199 time->daylight =
200 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200201 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);
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200213 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100214#endif
215}
216
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200217#ifdef CONFIG_DM_RTC
218
219/**
220 * efi_validate_time() - checks if timestamp is valid
221 *
222 * @time: timestamp to validate
223 * Returns: 0 if timestamp is valid, 1 otherwise
224 */
225static int efi_validate_time(struct efi_time *time)
226{
227 return (!time ||
228 time->year < 1900 || time->year > 9999 ||
229 !time->month || time->month > 12 || !time->day ||
230 time->day > rtc_month_days(time->month - 1, time->year) ||
231 time->hour > 23 || time->minute > 59 || time->second > 59 ||
232 time->nanosecond > 999999999 ||
233 time->daylight &
234 ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
235 ((time->timezone < -1440 || time->timezone > 1440) &&
236 time->timezone != EFI_UNSPECIFIED_TIMEZONE));
237}
238
239#endif
240
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200241/**
242 * efi_set_time_boottime() - set current time
243 *
244 * This function implements the SetTime() runtime service before
245 * SetVirtualAddressMap() is called.
246 *
247 * See the Unified Extensible Firmware Interface (UEFI) specification
248 * for details.
249 *
250 * @time: pointer to structure to with current time
251 * Returns: status code
252 */
253static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
254{
255#ifdef CONFIG_DM_RTC
256 efi_status_t ret = EFI_SUCCESS;
257 struct rtc_time tm;
258 struct udevice *dev;
Alexander Graf77256092016-08-16 21:08:45 +0200259
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200260 EFI_ENTRY("%p", time);
261
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200262 if (efi_validate_time(time)) {
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200263 ret = EFI_INVALID_PARAMETER;
264 goto out;
265 }
266
267 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
268 ret = EFI_UNSUPPORTED;
269 goto out;
270 }
271
272 memset(&tm, 0, sizeof(tm));
273 tm.tm_year = time->year;
274 tm.tm_mon = time->month;
275 tm.tm_mday = time->day;
276 tm.tm_hour = time->hour;
277 tm.tm_min = time->minute;
278 tm.tm_sec = time->second;
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200279 tm.tm_isdst = time->daylight ==
280 (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200281 /* Calculate day of week */
282 rtc_calc_weekday(&tm);
283
284 if (dm_rtc_set(dev, &tm))
285 ret = EFI_DEVICE_ERROR;
286out:
287 return EFI_EXIT(ret);
288#else
289 EFI_ENTRY("%p", time);
290 return EFI_EXIT(EFI_UNSUPPORTED);
291#endif
292}
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200293/**
294 * efi_reset_system() - reset system
295 *
296 * This function implements the ResetSystem() runtime service after
297 * SetVirtualAddressMap() is called. It only executes an endless loop.
298 * Boards may override the helpers below to implement reset functionality.
299 *
300 * See the Unified Extensible Firmware Interface (UEFI) specification for
301 * details.
302 *
303 * @reset_type: type of reset to perform
304 * @reset_status: status code for the reset
305 * @data_size: size of reset_data
306 * @reset_data: information about the reset
307 */
Alexander Graf393dd912016-10-14 13:45:30 +0200308void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200309 enum efi_reset_type reset_type,
310 efi_status_t reset_status,
311 unsigned long data_size, void *reset_data)
312{
313 /* Nothing we can do */
314 while (1) { }
315}
316
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200317/**
318 * efi_reset_system_init() - initialize the reset driver
319 *
320 * Boards may override this function to initialize the reset driver.
321 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100322efi_status_t __weak efi_reset_system_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200323{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100324 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200325}
326
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200327/**
328 * efi_get_time() - get current time
329 *
330 * This function implements the GetTime runtime service after
331 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
332 * anymore only an error code is returned.
333 *
334 * See the Unified Extensible Firmware Interface (UEFI) specification
335 * for details.
336 *
337 * @time: pointer to structure to receive current time
338 * @capabilities: pointer to structure to receive RTC properties
339 * Returns: status code
340 */
Alexander Graf393dd912016-10-14 13:45:30 +0200341efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200342 struct efi_time *time,
343 struct efi_time_cap *capabilities)
344{
345 /* Nothing we can do */
346 return EFI_DEVICE_ERROR;
347}
348
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200349/**
350 * efi_set_time() - set current time
351 *
352 * This function implements the SetTime runtime service after
353 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
354 * anymore only an error code is returned.
355 *
356 * See the Unified Extensible Firmware Interface (UEFI) specification
357 * for details.
358 *
359 * @time: pointer to structure to with current time
360 * Returns: status code
361 */
362efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
363{
364 return EFI_UNSUPPORTED;
365}
366
Alexander Graf0bd425a2016-03-04 01:10:01 +0100367struct efi_runtime_detach_list_struct {
368 void *ptr;
369 void *patchto;
370};
371
372static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
373 {
374 /* do_reset is gone */
375 .ptr = &efi_runtime_services.reset_system,
Alexander Graf77256092016-08-16 21:08:45 +0200376 .patchto = efi_reset_system,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100377 }, {
378 /* invalidate_*cache_all are gone */
379 .ptr = &efi_runtime_services.set_virtual_address_map,
AKASHI Takahirod0ae0922018-11-14 16:18:07 +0900380 .patchto = &efi_unimplemented,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100381 }, {
382 /* RTC accessors are gone */
383 .ptr = &efi_runtime_services.get_time,
Alexander Graf77256092016-08-16 21:08:45 +0200384 .patchto = &efi_get_time,
Alexander Grafc59d3602016-05-18 00:54:47 +0200385 }, {
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200386 .ptr = &efi_runtime_services.set_time,
387 .patchto = &efi_set_time,
388 }, {
Alexander Grafc59d3602016-05-18 00:54:47 +0200389 /* Clean up system table */
390 .ptr = &systab.con_in,
391 .patchto = NULL,
392 }, {
393 /* Clean up system table */
394 .ptr = &systab.con_out,
395 .patchto = NULL,
396 }, {
397 /* Clean up system table */
398 .ptr = &systab.std_err,
399 .patchto = NULL,
400 }, {
401 /* Clean up system table */
402 .ptr = &systab.boottime,
403 .patchto = NULL,
Rob Clark15f3d742017-09-13 18:05:37 -0400404 }, {
405 .ptr = &efi_runtime_services.get_variable,
406 .patchto = &efi_device_error,
407 }, {
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200408 .ptr = &efi_runtime_services.get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400409 .patchto = &efi_device_error,
410 }, {
411 .ptr = &efi_runtime_services.set_variable,
412 .patchto = &efi_device_error,
413 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100414};
415
416static bool efi_runtime_tobedetached(void *p)
417{
418 int i;
419
420 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
421 if (efi_runtime_detach_list[i].ptr == p)
422 return true;
423
424 return false;
425}
426
427static void efi_runtime_detach(ulong offset)
428{
429 int i;
430 ulong patchoff = offset - (ulong)gd->relocaddr;
431
432 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
433 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
434 ulong *p = efi_runtime_detach_list[i].ptr;
435 ulong newaddr = patchto ? (patchto + patchoff) : 0;
436
Alexander Graf62a67482016-06-02 11:38:27 +0200437 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100438 *p = newaddr;
439 }
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200440
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200441 /* Update CRC32 */
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200442 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100443}
444
445/* Relocate EFI runtime to uboot_reloc_base = offset */
446void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
447{
448#ifdef IS_RELA
449 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
450#else
451 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
452 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
453#endif
454
Alexander Graf62a67482016-06-02 11:38:27 +0200455 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100456 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
457 ulong base = CONFIG_SYS_TEXT_BASE;
458 ulong *p;
459 ulong newaddr;
460
461 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
462
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700463 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
464 rel->info, *p, rel->offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100465
Rick Chen9677a372018-05-28 19:06:37 +0800466 switch (rel->info & R_MASK) {
467 case R_RELATIVE:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100468#ifdef IS_RELA
469 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
470#else
471 newaddr = *p - lastoff + offset;
472#endif
Rick Chen9677a372018-05-28 19:06:37 +0800473 break;
474#ifdef R_ABSOLUTE
475 case R_ABSOLUTE: {
476 ulong symidx = rel->info >> SYM_INDEX;
477 extern struct dyn_sym __dyn_sym_start[];
478 newaddr = __dyn_sym_start[symidx].addr + offset;
Alexander Grafdf0f6c72018-11-04 22:25:22 +0100479#ifdef IS_RELA
480 newaddr -= CONFIG_SYS_TEXT_BASE;
481#endif
Rick Chen9677a372018-05-28 19:06:37 +0800482 break;
483 }
484#endif
485 default:
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700486 if (!efi_runtime_tobedetached(p))
487 printf("%s: Unknown relocation type %llx\n",
488 __func__, rel->info & R_MASK);
Rick Chen9677a372018-05-28 19:06:37 +0800489 continue;
490 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100491
492 /* Check if the relocation is inside bounds */
493 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200494 newaddr > (map->virtual_start +
495 (map->num_pages << EFI_PAGE_SHIFT)))) {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100496 if (!efi_runtime_tobedetached(p))
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700497 printf("%s: Relocation at %p is out of "
498 "range (%lx)\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100499 continue;
500 }
501
Alexander Graf62a67482016-06-02 11:38:27 +0200502 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100503 *p = newaddr;
Alexander Grafe5702322016-04-11 23:20:39 +0200504 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
505 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf0bd425a2016-03-04 01:10:01 +0100506 }
507
508#ifndef IS_RELA
509 lastoff = offset;
510#endif
511
512 invalidate_icache_all();
513}
514
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200515/**
516 * efi_set_virtual_address_map() - change from physical to virtual mapping
517 *
518 * This function implements the SetVirtualAddressMap() runtime service.
519 *
520 * See the Unified Extensible Firmware Interface (UEFI) specification for
521 * details.
522 *
523 * @memory_map_size: size of the virtual map
524 * @descriptor_size: size of an entry in the map
525 * @descriptor_version: version of the map entries
526 * @virtmap: virtual address mapping information
527 * Return: status code
528 */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100529static efi_status_t EFIAPI efi_set_virtual_address_map(
530 unsigned long memory_map_size,
531 unsigned long descriptor_size,
532 uint32_t descriptor_version,
533 struct efi_mem_desc *virtmap)
534{
Alexander Graf0bd425a2016-03-04 01:10:01 +0100535 int n = memory_map_size / descriptor_size;
536 int i;
Alexander Graf86e00212018-12-11 10:00:42 +0100537 int rt_code_sections = 0;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100538
539 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
540 descriptor_version, virtmap);
541
Alexander Graf86e00212018-12-11 10:00:42 +0100542 /*
543 * TODO:
544 * Further down we are cheating. While really we should implement
545 * SetVirtualAddressMap() events and ConvertPointer() to allow
546 * dynamically loaded drivers to expose runtime services, we don't
547 * today.
548 *
549 * So let's ensure we see exactly one single runtime section, as
550 * that is the built-in one. If we see more (or less), someone must
551 * have tried adding or removing to that which we don't support yet.
552 * In that case, let's better fail rather than expose broken runtime
553 * services.
554 */
555 for (i = 0; i < n; i++) {
556 struct efi_mem_desc *map = (void*)virtmap +
557 (descriptor_size * i);
558
559 if (map->type == EFI_RUNTIME_SERVICES_CODE)
560 rt_code_sections++;
561 }
562
563 if (rt_code_sections != 1) {
564 /*
565 * We expose exactly one single runtime code section, so
566 * something is definitely going wrong.
567 */
568 return EFI_EXIT(EFI_INVALID_PARAMETER);
569 }
570
Alexander Graf77256092016-08-16 21:08:45 +0200571 /* Rebind mmio pointers */
572 for (i = 0; i < n; i++) {
573 struct efi_mem_desc *map = (void*)virtmap +
574 (descriptor_size * i);
575 struct list_head *lhandle;
576 efi_physical_addr_t map_start = map->physical_start;
577 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
578 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200579 u64 off = map->virtual_start - map_start;
Alexander Graf77256092016-08-16 21:08:45 +0200580
581 /* Adjust all mmio pointers in this region */
582 list_for_each(lhandle, &efi_runtime_mmio) {
583 struct efi_runtime_mmio_list *lmmio;
584
585 lmmio = list_entry(lhandle,
586 struct efi_runtime_mmio_list,
587 link);
588 if ((map_start <= lmmio->paddr) &&
589 (map_end >= lmmio->paddr)) {
Alexander Graf77256092016-08-16 21:08:45 +0200590 uintptr_t new_addr = lmmio->paddr + off;
591 *lmmio->ptr = (void *)new_addr;
592 }
593 }
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200594 if ((map_start <= (uintptr_t)systab.tables) &&
595 (map_end >= (uintptr_t)systab.tables)) {
596 char *ptr = (char *)systab.tables;
597
598 ptr += off;
599 systab.tables = (struct efi_configuration_table *)ptr;
600 }
Alexander Graf77256092016-08-16 21:08:45 +0200601 }
602
603 /* Move the actual runtime code over */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100604 for (i = 0; i < n; i++) {
605 struct efi_mem_desc *map;
606
607 map = (void*)virtmap + (descriptor_size * i);
608 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf77256092016-08-16 21:08:45 +0200609 ulong new_offset = map->virtual_start -
Alexander Graf86e00212018-12-11 10:00:42 +0100610 map->physical_start + gd->relocaddr;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100611
612 efi_runtime_relocate(new_offset, map);
613 /* Once we're virtual, we can no longer handle
614 complex callbacks */
615 efi_runtime_detach(new_offset);
616 return EFI_EXIT(EFI_SUCCESS);
617 }
618 }
619
620 return EFI_EXIT(EFI_INVALID_PARAMETER);
621}
622
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200623/**
624 * efi_add_runtime_mmio() - add memory-mapped IO region
625 *
626 * This function adds a memory-mapped IO region to the memory map to make it
627 * available at runtime.
628 *
Heinrich Schuchardt7e307642018-12-23 02:35:13 +0100629 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
630 * IO region
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200631 * @len: size of the memory-mapped IO region
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200632 * Returns: status code
633 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100634efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf77256092016-08-16 21:08:45 +0200635{
636 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.de93fcc7f2017-08-11 21:19:37 +0200637 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf33a83a32018-03-15 15:08:16 +0100638 uint64_t addr = *(uintptr_t *)mmio_ptr;
639 uint64_t retaddr;
640
641 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
642 if (retaddr != addr)
643 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200644
645 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100646 if (!newmmio)
647 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200648 newmmio->ptr = mmio_ptr;
649 newmmio->paddr = *(uintptr_t *)mmio_ptr;
650 newmmio->len = len;
651 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100652
Alexander Graf33a83a32018-03-15 15:08:16 +0100653 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200654}
655
Alexander Graf0bd425a2016-03-04 01:10:01 +0100656/*
657 * In the second stage, U-Boot has disappeared. To isolate our runtime code
658 * that at this point still exists from the rest, we put it into a special
659 * section.
660 *
661 * !!WARNING!!
662 *
663 * This means that we can not rely on any code outside of this file in any
664 * function or variable below this line.
665 *
666 * Please keep everything fully self-contained and annotated with
Alexander Graf393dd912016-10-14 13:45:30 +0200667 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf0bd425a2016-03-04 01:10:01 +0100668 */
669
670/*
671 * Relocate the EFI runtime stub to a different place. We need to call this
672 * the first time we expose the runtime interface to a user and on set virtual
673 * address map calls.
674 */
675
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200676/**
677 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
678 *
679 * This function is used after SetVirtualAddressMap() is called as replacement
680 * for services that are not available anymore due to constraints of the U-Boot
681 * implementation.
682 *
683 * Return: EFI_UNSUPPORTED
684 */
Alexander Graf393dd912016-10-14 13:45:30 +0200685static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100686{
687 return EFI_UNSUPPORTED;
688}
689
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200690/**
691 * efi_device_error() - replacement function, returns EFI_DEVICE_ERROR
692 *
693 * This function is used after SetVirtualAddressMap() is called as replacement
694 * for services that are not available anymore due to constraints of the U-Boot
695 * implementation.
696 *
697 * Return: EFI_DEVICE_ERROR
698 */
Alexander Graf393dd912016-10-14 13:45:30 +0200699static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100700{
701 return EFI_DEVICE_ERROR;
702}
703
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200704/**
705 * efi_invalid_parameter() - replacement function, returns EFI_INVALID_PARAMETER
706 *
707 * This function is used after SetVirtualAddressMap() is called as replacement
708 * for services that are not available anymore due to constraints of the U-Boot
709 * implementation.
710 *
711 * Return: EFI_INVALID_PARAMETER
712 */
Alexander Graf393dd912016-10-14 13:45:30 +0200713static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100714{
715 return EFI_INVALID_PARAMETER;
716}
717
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200718/**
719 * efi_update_capsule() - process information from operating system
720 *
721 * This function implements the UpdateCapsule() runtime service.
722 *
723 * See the Unified Extensible Firmware Interface (UEFI) specification for
724 * details.
725 *
726 * @capsule_header_array: pointer to array of virtual pointers
727 * @capsule_count: number of pointers in capsule_header_array
728 * @scatter_gather_list: pointer to arry of physical pointers
729 * Returns: status code
730 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100731efi_status_t __efi_runtime EFIAPI efi_update_capsule(
732 struct efi_capsule_header **capsule_header_array,
733 efi_uintn_t capsule_count,
734 u64 scatter_gather_list)
735{
736 return EFI_UNSUPPORTED;
737}
738
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200739/**
740 * efi_query_capsule_caps() - check if capsule is supported
741 *
742 * This function implements the QueryCapsuleCapabilities() runtime service.
743 *
744 * See the Unified Extensible Firmware Interface (UEFI) specification for
745 * details.
746 *
747 * @capsule_header_array: pointer to array of virtual pointers
748 * @capsule_count: number of pointers in capsule_header_array
Heinrich Schuchardt1d1be362018-09-03 20:59:25 +0200749 * @maximum_capsule_size: maximum capsule size
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200750 * @reset_type: type of reset needed for capsule update
751 * Returns: status code
752 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100753efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
754 struct efi_capsule_header **capsule_header_array,
755 efi_uintn_t capsule_count,
AKASHI Takahirod840b672018-11-14 16:18:53 +0900756 u64 *maximum_capsule_size,
757 u32 *reset_type)
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100758{
759 return EFI_UNSUPPORTED;
760}
761
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200762/**
763 * efi_query_variable_info() - get information about EFI variables
764 *
765 * This function implements the QueryVariableInfo() runtime service.
766 *
767 * See the Unified Extensible Firmware Interface (UEFI) specification for
768 * details.
769 *
770 * @attributes: bitmask to select variables to be
771 * queried
772 * @maximum_variable_storage_size: maximum size of storage area for the
773 * selected variable types
774 * @remaining_variable_storage_size: remaining size of storage are for the
775 * selected variable types
776 * @maximum_variable_size: maximum size of a variable of the
777 * selected type
778 * Returns: status code
779 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100780efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
781 u32 attributes,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200782 u64 *maximum_variable_storage_size,
783 u64 *remaining_variable_storage_size,
784 u64 *maximum_variable_size)
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100785{
786 return EFI_UNSUPPORTED;
787}
788
Alexander Graf393dd912016-10-14 13:45:30 +0200789struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100790 .hdr = {
791 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +0200792 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +0200793 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf0bd425a2016-03-04 01:10:01 +0100794 },
Alexander Graf77256092016-08-16 21:08:45 +0200795 .get_time = &efi_get_time_boottime,
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200796 .set_time = &efi_set_time_boottime,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100797 .get_wakeup_time = (void *)&efi_unimplemented,
798 .set_wakeup_time = (void *)&efi_unimplemented,
799 .set_virtual_address_map = &efi_set_virtual_address_map,
800 .convert_pointer = (void *)&efi_invalid_parameter,
Rob Clark15f3d742017-09-13 18:05:37 -0400801 .get_variable = efi_get_variable,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200802 .get_next_variable_name = efi_get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400803 .set_variable = efi_set_variable,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100804 .get_next_high_mono_count = (void *)&efi_device_error,
Alexander Graf77256092016-08-16 21:08:45 +0200805 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100806 .update_capsule = efi_update_capsule,
807 .query_capsule_caps = efi_query_capsule_caps,
808 .query_variable_info = efi_query_variable_info,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100809};