blob: 40fdc0ea9286945a292d38080dd97a80671bf59d [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
AKASHI Takahiro32401842019-06-05 13:21:38 +090092efi_status_t efi_init_runtime_supported(void)
93{
94 u16 efi_runtime_services_supported = 0;
95
96 /*
97 * This value must be synced with efi_runtime_detach_list
98 * as well as efi_runtime_services.
99 */
100#if CONFIG_IS_ENABLED(ARCH_BCM283X) || \
101 CONFIG_IS_ENABLED(FSL_LAYERSCAPE) || \
102 CONFIG_IS_ENABLED(SYSRESET_X86) || \
103 CONFIG_IS_ENABLED(PSCI_RESET)
104 efi_runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM;
105#endif
106 efi_runtime_services_supported |=
107 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP;
108 return EFI_CALL(efi_set_variable(L"RuntimeServicesSupported",
109 &efi_global_variable_guid,
110 EFI_VARIABLE_BOOTSERVICE_ACCESS |
111 EFI_VARIABLE_RUNTIME_ACCESS,
112 sizeof(efi_runtime_services_supported),
113 &efi_runtime_services_supported));
114}
115
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200116/**
117 * efi_update_table_header_crc32() - Update crc32 in table header
118 *
119 * @table: EFI table
120 */
121void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
122{
123 table->crc32 = 0;
124 table->crc32 = crc32(0, (const unsigned char *)table,
125 table->headersize);
126}
127
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200128/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200129 * efi_reset_system_boottime() - reset system at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200130 *
131 * This function implements the ResetSystem() runtime service before
132 * SetVirtualAddressMap() is called.
133 *
134 * See the Unified Extensible Firmware Interface (UEFI) specification for
135 * details.
136 *
137 * @reset_type: type of reset to perform
138 * @reset_status: status code for the reset
139 * @data_size: size of reset_data
140 * @reset_data: information about the reset
141 */
Alexander Graf77256092016-08-16 21:08:45 +0200142static void EFIAPI efi_reset_system_boottime(
143 enum efi_reset_type reset_type,
144 efi_status_t reset_status,
145 unsigned long data_size, void *reset_data)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100146{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100147 struct efi_event *evt;
148
Alexander Graf0bd425a2016-03-04 01:10:01 +0100149 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
150 reset_data);
151
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100152 /* Notify reset */
153 list_for_each_entry(evt, &efi_events, link) {
154 if (evt->group &&
155 !guidcmp(evt->group,
156 &efi_guid_event_group_reset_system)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200157 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100158 break;
159 }
160 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100161 switch (reset_type) {
162 case EFI_RESET_COLD:
163 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +0100164 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100165 do_reset(NULL, 0, 0, NULL);
166 break;
167 case EFI_RESET_SHUTDOWN:
Heinrich Schuchardt44489452018-10-16 07:44:53 +0200168#ifdef CONFIG_CMD_POWEROFF
169 do_poweroff(NULL, 0, 0, NULL);
170#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +0100171 break;
172 }
173
Alexander Graf77256092016-08-16 21:08:45 +0200174 while (1) { }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100175}
176
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200177/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200178 * efi_get_time_boottime() - get current time at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200179 *
180 * This function implements the GetTime runtime service before
181 * SetVirtualAddressMap() is called.
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200182 *
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200183 * See the Unified Extensible Firmware Interface (UEFI) specification
184 * for details.
185 *
186 * @time: pointer to structure to receive current time
187 * @capabilities: pointer to structure to receive RTC properties
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200188 * Returns: status code
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200189 */
Alexander Graf77256092016-08-16 21:08:45 +0200190static efi_status_t EFIAPI efi_get_time_boottime(
191 struct efi_time *time,
192 struct efi_time_cap *capabilities)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100193{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200194#ifdef CONFIG_EFI_GET_TIME
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200195 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200196 struct rtc_time tm;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100197 struct udevice *dev;
198
199 EFI_ENTRY("%p %p", time, capabilities);
200
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200201 if (!time) {
202 ret = EFI_INVALID_PARAMETER;
203 goto out;
204 }
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200205 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
206 dm_rtc_get(dev, &tm)) {
207 ret = EFI_UNSUPPORTED;
208 goto out;
209 }
210 if (dm_rtc_get(dev, &tm)) {
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200211 ret = EFI_DEVICE_ERROR;
212 goto out;
213 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100214
215 memset(time, 0, sizeof(*time));
216 time->year = tm.tm_year;
217 time->month = tm.tm_mon;
218 time->day = tm.tm_mday;
219 time->hour = tm.tm_hour;
220 time->minute = tm.tm_min;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200221 time->second = tm.tm_sec;
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200222 if (tm.tm_isdst)
223 time->daylight =
224 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200225 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100226
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200227 if (capabilities) {
228 /* Set reasonable dummy values */
229 capabilities->resolution = 1; /* 1 Hz */
230 capabilities->accuracy = 100000000; /* 100 ppm */
231 capabilities->sets_to_zero = false;
232 }
233out:
234 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100235#else
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200236 EFI_ENTRY("%p %p", time, capabilities);
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200237 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100238#endif
239}
240
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200241#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200242
243/**
244 * efi_validate_time() - checks if timestamp is valid
245 *
246 * @time: timestamp to validate
247 * Returns: 0 if timestamp is valid, 1 otherwise
248 */
249static int efi_validate_time(struct efi_time *time)
250{
251 return (!time ||
252 time->year < 1900 || time->year > 9999 ||
253 !time->month || time->month > 12 || !time->day ||
254 time->day > rtc_month_days(time->month - 1, time->year) ||
255 time->hour > 23 || time->minute > 59 || time->second > 59 ||
256 time->nanosecond > 999999999 ||
257 time->daylight &
258 ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
259 ((time->timezone < -1440 || time->timezone > 1440) &&
260 time->timezone != EFI_UNSPECIFIED_TIMEZONE));
261}
262
263#endif
264
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200265/**
266 * efi_set_time_boottime() - set current time
267 *
268 * This function implements the SetTime() runtime service before
269 * SetVirtualAddressMap() is called.
270 *
271 * See the Unified Extensible Firmware Interface (UEFI) specification
272 * for details.
273 *
274 * @time: pointer to structure to with current time
275 * Returns: status code
276 */
277static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
278{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200279#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200280 efi_status_t ret = EFI_SUCCESS;
281 struct rtc_time tm;
282 struct udevice *dev;
Alexander Graf77256092016-08-16 21:08:45 +0200283
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200284 EFI_ENTRY("%p", time);
285
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200286 if (efi_validate_time(time)) {
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200287 ret = EFI_INVALID_PARAMETER;
288 goto out;
289 }
290
291 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
292 ret = EFI_UNSUPPORTED;
293 goto out;
294 }
295
296 memset(&tm, 0, sizeof(tm));
297 tm.tm_year = time->year;
298 tm.tm_mon = time->month;
299 tm.tm_mday = time->day;
300 tm.tm_hour = time->hour;
301 tm.tm_min = time->minute;
302 tm.tm_sec = time->second;
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200303 tm.tm_isdst = time->daylight ==
304 (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200305 /* Calculate day of week */
306 rtc_calc_weekday(&tm);
307
308 if (dm_rtc_set(dev, &tm))
309 ret = EFI_DEVICE_ERROR;
310out:
311 return EFI_EXIT(ret);
312#else
313 EFI_ENTRY("%p", time);
314 return EFI_EXIT(EFI_UNSUPPORTED);
315#endif
316}
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200317/**
318 * efi_reset_system() - reset system
319 *
320 * This function implements the ResetSystem() runtime service after
321 * SetVirtualAddressMap() is called. It only executes an endless loop.
322 * Boards may override the helpers below to implement reset functionality.
323 *
324 * See the Unified Extensible Firmware Interface (UEFI) specification for
325 * details.
326 *
327 * @reset_type: type of reset to perform
328 * @reset_status: status code for the reset
329 * @data_size: size of reset_data
330 * @reset_data: information about the reset
331 */
Alexander Graf393dd912016-10-14 13:45:30 +0200332void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200333 enum efi_reset_type reset_type,
334 efi_status_t reset_status,
335 unsigned long data_size, void *reset_data)
336{
337 /* Nothing we can do */
338 while (1) { }
339}
340
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200341/**
342 * efi_reset_system_init() - initialize the reset driver
343 *
344 * Boards may override this function to initialize the reset driver.
345 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100346efi_status_t __weak efi_reset_system_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200347{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100348 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200349}
350
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200351/**
352 * efi_get_time() - get current time
353 *
354 * This function implements the GetTime runtime service after
355 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
356 * anymore only an error code is returned.
357 *
358 * See the Unified Extensible Firmware Interface (UEFI) specification
359 * for details.
360 *
361 * @time: pointer to structure to receive current time
362 * @capabilities: pointer to structure to receive RTC properties
363 * Returns: status code
364 */
Alexander Graf393dd912016-10-14 13:45:30 +0200365efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200366 struct efi_time *time,
367 struct efi_time_cap *capabilities)
368{
Heinrich Schuchardtb4ba5be2019-06-13 18:42:40 +0200369 return EFI_UNSUPPORTED;
Alexander Graf77256092016-08-16 21:08:45 +0200370}
371
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200372/**
373 * efi_set_time() - set current time
374 *
375 * This function implements the SetTime runtime service after
376 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
377 * anymore only an error code is returned.
378 *
379 * See the Unified Extensible Firmware Interface (UEFI) specification
380 * for details.
381 *
382 * @time: pointer to structure to with current time
383 * Returns: status code
384 */
385efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
386{
387 return EFI_UNSUPPORTED;
388}
389
Alexander Graf0bd425a2016-03-04 01:10:01 +0100390struct efi_runtime_detach_list_struct {
391 void *ptr;
392 void *patchto;
393};
394
395static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
396 {
397 /* do_reset is gone */
398 .ptr = &efi_runtime_services.reset_system,
Alexander Graf77256092016-08-16 21:08:45 +0200399 .patchto = efi_reset_system,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100400 }, {
401 /* invalidate_*cache_all are gone */
402 .ptr = &efi_runtime_services.set_virtual_address_map,
AKASHI Takahirod0ae0922018-11-14 16:18:07 +0900403 .patchto = &efi_unimplemented,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100404 }, {
405 /* RTC accessors are gone */
406 .ptr = &efi_runtime_services.get_time,
Alexander Graf77256092016-08-16 21:08:45 +0200407 .patchto = &efi_get_time,
Alexander Grafc59d3602016-05-18 00:54:47 +0200408 }, {
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200409 .ptr = &efi_runtime_services.set_time,
410 .patchto = &efi_set_time,
411 }, {
Alexander Grafc59d3602016-05-18 00:54:47 +0200412 /* Clean up system table */
413 .ptr = &systab.con_in,
414 .patchto = NULL,
415 }, {
416 /* Clean up system table */
417 .ptr = &systab.con_out,
418 .patchto = NULL,
419 }, {
420 /* Clean up system table */
421 .ptr = &systab.std_err,
422 .patchto = NULL,
423 }, {
424 /* Clean up system table */
425 .ptr = &systab.boottime,
426 .patchto = NULL,
Rob Clark15f3d742017-09-13 18:05:37 -0400427 }, {
428 .ptr = &efi_runtime_services.get_variable,
429 .patchto = &efi_device_error,
430 }, {
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200431 .ptr = &efi_runtime_services.get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400432 .patchto = &efi_device_error,
433 }, {
434 .ptr = &efi_runtime_services.set_variable,
435 .patchto = &efi_device_error,
436 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100437};
438
439static bool efi_runtime_tobedetached(void *p)
440{
441 int i;
442
443 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
444 if (efi_runtime_detach_list[i].ptr == p)
445 return true;
446
447 return false;
448}
449
450static void efi_runtime_detach(ulong offset)
451{
452 int i;
453 ulong patchoff = offset - (ulong)gd->relocaddr;
454
455 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
456 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
457 ulong *p = efi_runtime_detach_list[i].ptr;
458 ulong newaddr = patchto ? (patchto + patchoff) : 0;
459
Alexander Graf62a67482016-06-02 11:38:27 +0200460 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100461 *p = newaddr;
462 }
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200463
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200464 /* Update CRC32 */
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200465 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100466}
467
468/* Relocate EFI runtime to uboot_reloc_base = offset */
469void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
470{
471#ifdef IS_RELA
472 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
473#else
474 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
475 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
476#endif
477
Alexander Graf62a67482016-06-02 11:38:27 +0200478 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100479 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
480 ulong base = CONFIG_SYS_TEXT_BASE;
481 ulong *p;
482 ulong newaddr;
483
484 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
485
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700486 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
487 rel->info, *p, rel->offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100488
Rick Chen9677a372018-05-28 19:06:37 +0800489 switch (rel->info & R_MASK) {
490 case R_RELATIVE:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100491#ifdef IS_RELA
492 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
493#else
494 newaddr = *p - lastoff + offset;
495#endif
Rick Chen9677a372018-05-28 19:06:37 +0800496 break;
497#ifdef R_ABSOLUTE
498 case R_ABSOLUTE: {
499 ulong symidx = rel->info >> SYM_INDEX;
500 extern struct dyn_sym __dyn_sym_start[];
501 newaddr = __dyn_sym_start[symidx].addr + offset;
Alexander Grafdf0f6c72018-11-04 22:25:22 +0100502#ifdef IS_RELA
503 newaddr -= CONFIG_SYS_TEXT_BASE;
504#endif
Rick Chen9677a372018-05-28 19:06:37 +0800505 break;
506 }
507#endif
508 default:
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700509 if (!efi_runtime_tobedetached(p))
510 printf("%s: Unknown relocation type %llx\n",
511 __func__, rel->info & R_MASK);
Rick Chen9677a372018-05-28 19:06:37 +0800512 continue;
513 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100514
515 /* Check if the relocation is inside bounds */
516 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200517 newaddr > (map->virtual_start +
518 (map->num_pages << EFI_PAGE_SHIFT)))) {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100519 if (!efi_runtime_tobedetached(p))
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700520 printf("%s: Relocation at %p is out of "
521 "range (%lx)\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100522 continue;
523 }
524
Alexander Graf62a67482016-06-02 11:38:27 +0200525 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100526 *p = newaddr;
Alexander Grafe5702322016-04-11 23:20:39 +0200527 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
528 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf0bd425a2016-03-04 01:10:01 +0100529 }
530
531#ifndef IS_RELA
532 lastoff = offset;
533#endif
534
535 invalidate_icache_all();
536}
537
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200538/**
539 * efi_set_virtual_address_map() - change from physical to virtual mapping
540 *
541 * This function implements the SetVirtualAddressMap() runtime service.
542 *
543 * See the Unified Extensible Firmware Interface (UEFI) specification for
544 * details.
545 *
546 * @memory_map_size: size of the virtual map
547 * @descriptor_size: size of an entry in the map
548 * @descriptor_version: version of the map entries
549 * @virtmap: virtual address mapping information
550 * Return: status code
551 */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100552static efi_status_t EFIAPI efi_set_virtual_address_map(
553 unsigned long memory_map_size,
554 unsigned long descriptor_size,
555 uint32_t descriptor_version,
556 struct efi_mem_desc *virtmap)
557{
Alexander Graf0bd425a2016-03-04 01:10:01 +0100558 int n = memory_map_size / descriptor_size;
559 int i;
Alexander Graf86e00212018-12-11 10:00:42 +0100560 int rt_code_sections = 0;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100561
562 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
563 descriptor_version, virtmap);
564
Alexander Graf86e00212018-12-11 10:00:42 +0100565 /*
566 * TODO:
567 * Further down we are cheating. While really we should implement
568 * SetVirtualAddressMap() events and ConvertPointer() to allow
569 * dynamically loaded drivers to expose runtime services, we don't
570 * today.
571 *
572 * So let's ensure we see exactly one single runtime section, as
573 * that is the built-in one. If we see more (or less), someone must
574 * have tried adding or removing to that which we don't support yet.
575 * In that case, let's better fail rather than expose broken runtime
576 * services.
577 */
578 for (i = 0; i < n; i++) {
579 struct efi_mem_desc *map = (void*)virtmap +
580 (descriptor_size * i);
581
582 if (map->type == EFI_RUNTIME_SERVICES_CODE)
583 rt_code_sections++;
584 }
585
586 if (rt_code_sections != 1) {
587 /*
588 * We expose exactly one single runtime code section, so
589 * something is definitely going wrong.
590 */
591 return EFI_EXIT(EFI_INVALID_PARAMETER);
592 }
593
Alexander Graf77256092016-08-16 21:08:45 +0200594 /* Rebind mmio pointers */
595 for (i = 0; i < n; i++) {
596 struct efi_mem_desc *map = (void*)virtmap +
597 (descriptor_size * i);
598 struct list_head *lhandle;
599 efi_physical_addr_t map_start = map->physical_start;
600 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
601 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200602 u64 off = map->virtual_start - map_start;
Alexander Graf77256092016-08-16 21:08:45 +0200603
604 /* Adjust all mmio pointers in this region */
605 list_for_each(lhandle, &efi_runtime_mmio) {
606 struct efi_runtime_mmio_list *lmmio;
607
608 lmmio = list_entry(lhandle,
609 struct efi_runtime_mmio_list,
610 link);
611 if ((map_start <= lmmio->paddr) &&
612 (map_end >= lmmio->paddr)) {
Alexander Graf77256092016-08-16 21:08:45 +0200613 uintptr_t new_addr = lmmio->paddr + off;
614 *lmmio->ptr = (void *)new_addr;
615 }
616 }
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200617 if ((map_start <= (uintptr_t)systab.tables) &&
618 (map_end >= (uintptr_t)systab.tables)) {
619 char *ptr = (char *)systab.tables;
620
621 ptr += off;
622 systab.tables = (struct efi_configuration_table *)ptr;
623 }
Alexander Graf77256092016-08-16 21:08:45 +0200624 }
625
626 /* Move the actual runtime code over */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100627 for (i = 0; i < n; i++) {
628 struct efi_mem_desc *map;
629
630 map = (void*)virtmap + (descriptor_size * i);
631 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf77256092016-08-16 21:08:45 +0200632 ulong new_offset = map->virtual_start -
Alexander Graf86e00212018-12-11 10:00:42 +0100633 map->physical_start + gd->relocaddr;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100634
635 efi_runtime_relocate(new_offset, map);
636 /* Once we're virtual, we can no longer handle
637 complex callbacks */
638 efi_runtime_detach(new_offset);
639 return EFI_EXIT(EFI_SUCCESS);
640 }
641 }
642
643 return EFI_EXIT(EFI_INVALID_PARAMETER);
644}
645
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200646/**
647 * efi_add_runtime_mmio() - add memory-mapped IO region
648 *
649 * This function adds a memory-mapped IO region to the memory map to make it
650 * available at runtime.
651 *
Heinrich Schuchardt7e307642018-12-23 02:35:13 +0100652 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
653 * IO region
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200654 * @len: size of the memory-mapped IO region
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200655 * Returns: status code
656 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100657efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf77256092016-08-16 21:08:45 +0200658{
659 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.de93fcc7f2017-08-11 21:19:37 +0200660 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf33a83a32018-03-15 15:08:16 +0100661 uint64_t addr = *(uintptr_t *)mmio_ptr;
662 uint64_t retaddr;
663
664 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
665 if (retaddr != addr)
666 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200667
668 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100669 if (!newmmio)
670 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200671 newmmio->ptr = mmio_ptr;
672 newmmio->paddr = *(uintptr_t *)mmio_ptr;
673 newmmio->len = len;
674 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100675
Alexander Graf33a83a32018-03-15 15:08:16 +0100676 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200677}
678
Alexander Graf0bd425a2016-03-04 01:10:01 +0100679/*
680 * In the second stage, U-Boot has disappeared. To isolate our runtime code
681 * that at this point still exists from the rest, we put it into a special
682 * section.
683 *
684 * !!WARNING!!
685 *
686 * This means that we can not rely on any code outside of this file in any
687 * function or variable below this line.
688 *
689 * Please keep everything fully self-contained and annotated with
Alexander Graf393dd912016-10-14 13:45:30 +0200690 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf0bd425a2016-03-04 01:10:01 +0100691 */
692
693/*
694 * Relocate the EFI runtime stub to a different place. We need to call this
695 * the first time we expose the runtime interface to a user and on set virtual
696 * address map calls.
697 */
698
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200699/**
700 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
701 *
702 * This function is used after SetVirtualAddressMap() is called as replacement
703 * for services that are not available anymore due to constraints of the U-Boot
704 * implementation.
705 *
706 * Return: EFI_UNSUPPORTED
707 */
Alexander Graf393dd912016-10-14 13:45:30 +0200708static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100709{
710 return EFI_UNSUPPORTED;
711}
712
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200713/**
714 * efi_device_error() - replacement function, returns EFI_DEVICE_ERROR
715 *
716 * This function is used after SetVirtualAddressMap() is called as replacement
717 * for services that are not available anymore due to constraints of the U-Boot
718 * implementation.
719 *
720 * Return: EFI_DEVICE_ERROR
721 */
Alexander Graf393dd912016-10-14 13:45:30 +0200722static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100723{
724 return EFI_DEVICE_ERROR;
725}
726
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200727/**
728 * efi_invalid_parameter() - replacement function, returns EFI_INVALID_PARAMETER
729 *
730 * This function is used after SetVirtualAddressMap() is called as replacement
731 * for services that are not available anymore due to constraints of the U-Boot
732 * implementation.
733 *
734 * Return: EFI_INVALID_PARAMETER
735 */
Alexander Graf393dd912016-10-14 13:45:30 +0200736static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100737{
738 return EFI_INVALID_PARAMETER;
739}
740
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200741/**
742 * efi_update_capsule() - process information from operating system
743 *
744 * This function implements the UpdateCapsule() runtime service.
745 *
746 * See the Unified Extensible Firmware Interface (UEFI) specification for
747 * details.
748 *
749 * @capsule_header_array: pointer to array of virtual pointers
750 * @capsule_count: number of pointers in capsule_header_array
751 * @scatter_gather_list: pointer to arry of physical pointers
752 * Returns: status code
753 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100754efi_status_t __efi_runtime EFIAPI efi_update_capsule(
755 struct efi_capsule_header **capsule_header_array,
756 efi_uintn_t capsule_count,
757 u64 scatter_gather_list)
758{
759 return EFI_UNSUPPORTED;
760}
761
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200762/**
763 * efi_query_capsule_caps() - check if capsule is supported
764 *
765 * This function implements the QueryCapsuleCapabilities() runtime service.
766 *
767 * See the Unified Extensible Firmware Interface (UEFI) specification for
768 * details.
769 *
770 * @capsule_header_array: pointer to array of virtual pointers
771 * @capsule_count: number of pointers in capsule_header_array
Heinrich Schuchardt1d1be362018-09-03 20:59:25 +0200772 * @maximum_capsule_size: maximum capsule size
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200773 * @reset_type: type of reset needed for capsule update
774 * Returns: status code
775 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100776efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
777 struct efi_capsule_header **capsule_header_array,
778 efi_uintn_t capsule_count,
AKASHI Takahirod840b672018-11-14 16:18:53 +0900779 u64 *maximum_capsule_size,
780 u32 *reset_type)
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100781{
782 return EFI_UNSUPPORTED;
783}
784
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200785/**
786 * efi_query_variable_info() - get information about EFI variables
787 *
788 * This function implements the QueryVariableInfo() runtime service.
789 *
790 * See the Unified Extensible Firmware Interface (UEFI) specification for
791 * details.
792 *
793 * @attributes: bitmask to select variables to be
794 * queried
795 * @maximum_variable_storage_size: maximum size of storage area for the
796 * selected variable types
797 * @remaining_variable_storage_size: remaining size of storage are for the
798 * selected variable types
799 * @maximum_variable_size: maximum size of a variable of the
800 * selected type
801 * Returns: status code
802 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100803efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
804 u32 attributes,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200805 u64 *maximum_variable_storage_size,
806 u64 *remaining_variable_storage_size,
807 u64 *maximum_variable_size)
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100808{
809 return EFI_UNSUPPORTED;
810}
811
Alexander Graf393dd912016-10-14 13:45:30 +0200812struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100813 .hdr = {
814 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +0200815 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +0200816 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf0bd425a2016-03-04 01:10:01 +0100817 },
Alexander Graf77256092016-08-16 21:08:45 +0200818 .get_time = &efi_get_time_boottime,
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200819 .set_time = &efi_set_time_boottime,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100820 .get_wakeup_time = (void *)&efi_unimplemented,
821 .set_wakeup_time = (void *)&efi_unimplemented,
822 .set_virtual_address_map = &efi_set_virtual_address_map,
823 .convert_pointer = (void *)&efi_invalid_parameter,
Rob Clark15f3d742017-09-13 18:05:37 -0400824 .get_variable = efi_get_variable,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200825 .get_next_variable_name = efi_get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400826 .set_variable = efi_set_variable,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100827 .get_next_high_mono_count = (void *)&efi_device_error,
Alexander Graf77256092016-08-16 21:08:45 +0200828 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100829 .update_capsule = efi_update_capsule,
830 .query_capsule_caps = efi_query_capsule_caps,
831 .query_variable_info = efi_query_variable_info,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100832};