blob: c56230cad32f57af722ac32d4328720a2a17fc72 [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
AKASHI Takahiroe771b4b2019-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 Schuchardta39f39c2018-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 Schuchardtbcfb0e22018-07-29 15:10:11 +0200128/**
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200129 * efi_reset_system_boottime() - reset system at boot time
Heinrich Schuchardtbcfb0e22018-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 Graf80a48002016-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 Graf50149ea2016-03-04 01:10:01 +0100146{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100147 struct efi_event *evt;
148
Alexander Graf50149ea2016-03-04 01:10:01 +0100149 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
150 reset_data);
151
Heinrich Schuchardtb095f3c2018-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 Schuchardt7eaa9002019-06-07 06:47:01 +0200157 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100158 break;
159 }
160 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100161 switch (reset_type) {
162 case EFI_RESET_COLD:
163 case EFI_RESET_WARM:
Heinrich Schuchardt482fc902018-02-06 22:00:22 +0100164 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf50149ea2016-03-04 01:10:01 +0100165 do_reset(NULL, 0, 0, NULL);
166 break;
167 case EFI_RESET_SHUTDOWN:
Heinrich Schuchardte706ed72018-10-16 07:44:53 +0200168#ifdef CONFIG_CMD_POWEROFF
169 do_poweroff(NULL, 0, 0, NULL);
170#endif
Alexander Graf50149ea2016-03-04 01:10:01 +0100171 break;
172 }
173
Alexander Graf80a48002016-08-16 21:08:45 +0200174 while (1) { }
Alexander Graf50149ea2016-03-04 01:10:01 +0100175}
176
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200177/**
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200178 * efi_get_time_boottime() - get current time at boot time
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200179 *
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200180 * This function implements the GetTime runtime service before
181 * SetVirtualAddressMap() is called.
182 *
Heinrich Schuchardt49de2452018-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 Schuchardtbcfb0e22018-07-29 15:10:11 +0200188 * Returns: status code
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200189 */
Alexander Graf80a48002016-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 Graf50149ea2016-03-04 01:10:01 +0100193{
Heinrich Schuchardt5ec48e32019-05-31 22:56:02 +0200194#ifdef CONFIG_EFI_GET_TIME
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200195 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200196 struct rtc_time tm;
Alexander Graf50149ea2016-03-04 01:10:01 +0100197 struct udevice *dev;
198
199 EFI_ENTRY("%p %p", time, capabilities);
200
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200201 if (!time) {
202 ret = EFI_INVALID_PARAMETER;
203 goto out;
204 }
Heinrich Schuchardtbb2b13d2019-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 Schuchardt49de2452018-07-07 23:39:14 +0200211 ret = EFI_DEVICE_ERROR;
212 goto out;
213 }
Alexander Graf50149ea2016-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 Schuchardt49de2452018-07-07 23:39:14 +0200221 time->second = tm.tm_sec;
Heinrich Schuchardt38b9a792019-05-31 22:08:45 +0200222 if (tm.tm_isdst)
223 time->daylight =
224 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200225 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
Alexander Graf50149ea2016-03-04 01:10:01 +0100226
Heinrich Schuchardt49de2452018-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 Graf50149ea2016-03-04 01:10:01 +0100235#else
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200236 EFI_ENTRY("%p %p", time, capabilities);
Heinrich Schuchardtbb2b13d2019-05-19 21:41:28 +0200237 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf50149ea2016-03-04 01:10:01 +0100238#endif
239}
240
Heinrich Schuchardt5ec48e32019-05-31 22:56:02 +0200241#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardte6bcc352019-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 Schuchardt433bfe72019-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 Schuchardt5ec48e32019-05-31 22:56:02 +0200279#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200280 efi_status_t ret = EFI_SUCCESS;
281 struct rtc_time tm;
282 struct udevice *dev;
Alexander Graf80a48002016-08-16 21:08:45 +0200283
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200284 EFI_ENTRY("%p", time);
285
Heinrich Schuchardte6bcc352019-05-31 07:35:19 +0200286 if (efi_validate_time(time)) {
Heinrich Schuchardt433bfe72019-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 Schuchardt38b9a792019-05-31 22:08:45 +0200303 tm.tm_isdst = time->daylight ==
304 (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
Heinrich Schuchardt433bfe72019-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 Schuchardtbcfb0e22018-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 Graf3c63db92016-10-14 13:45:30 +0200332void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf80a48002016-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 Schuchardtbcfb0e22018-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 Schuchardt22c793e2018-03-03 15:28:59 +0100346efi_status_t __weak efi_reset_system_init(void)
Alexander Graf80a48002016-08-16 21:08:45 +0200347{
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100348 return EFI_SUCCESS;
Alexander Graf80a48002016-08-16 21:08:45 +0200349}
350
Heinrich Schuchardtbcfb0e22018-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 Graf3c63db92016-10-14 13:45:30 +0200365efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf80a48002016-08-16 21:08:45 +0200366 struct efi_time *time,
367 struct efi_time_cap *capabilities)
368{
Heinrich Schuchardtc5b63be2019-06-13 18:42:40 +0200369 return EFI_UNSUPPORTED;
Alexander Graf80a48002016-08-16 21:08:45 +0200370}
371
Heinrich Schuchardt433bfe72019-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 Graf50149ea2016-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 Graf80a48002016-08-16 21:08:45 +0200399 .patchto = efi_reset_system,
Alexander Graf50149ea2016-03-04 01:10:01 +0100400 }, {
401 /* invalidate_*cache_all are gone */
402 .ptr = &efi_runtime_services.set_virtual_address_map,
AKASHI Takahiro83582412018-11-14 16:18:07 +0900403 .patchto = &efi_unimplemented,
Alexander Graf50149ea2016-03-04 01:10:01 +0100404 }, {
405 /* RTC accessors are gone */
406 .ptr = &efi_runtime_services.get_time,
Alexander Graf80a48002016-08-16 21:08:45 +0200407 .patchto = &efi_get_time,
Alexander Grafae874402016-05-18 00:54:47 +0200408 }, {
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200409 .ptr = &efi_runtime_services.set_time,
410 .patchto = &efi_set_time,
411 }, {
Rob Clarkad644e72017-09-13 18:05:37 -0400412 .ptr = &efi_runtime_services.get_variable,
413 .patchto = &efi_device_error,
414 }, {
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200415 .ptr = &efi_runtime_services.get_next_variable_name,
Rob Clarkad644e72017-09-13 18:05:37 -0400416 .patchto = &efi_device_error,
417 }, {
418 .ptr = &efi_runtime_services.set_variable,
419 .patchto = &efi_device_error,
420 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100421};
422
423static bool efi_runtime_tobedetached(void *p)
424{
425 int i;
426
427 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
428 if (efi_runtime_detach_list[i].ptr == p)
429 return true;
430
431 return false;
432}
433
434static void efi_runtime_detach(ulong offset)
435{
436 int i;
437 ulong patchoff = offset - (ulong)gd->relocaddr;
438
439 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
440 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
441 ulong *p = efi_runtime_detach_list[i].ptr;
442 ulong newaddr = patchto ? (patchto + patchoff) : 0;
443
Alexander Grafedcef3b2016-06-02 11:38:27 +0200444 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100445 *p = newaddr;
446 }
Heinrich Schuchardta39f39c2018-07-29 09:49:04 +0200447
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200448 /* Update CRC32 */
Heinrich Schuchardta39f39c2018-07-29 09:49:04 +0200449 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100450}
451
452/* Relocate EFI runtime to uboot_reloc_base = offset */
453void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
454{
455#ifdef IS_RELA
456 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
457#else
458 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
459 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
460#endif
461
Alexander Grafedcef3b2016-06-02 11:38:27 +0200462 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf50149ea2016-03-04 01:10:01 +0100463 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
464 ulong base = CONFIG_SYS_TEXT_BASE;
465 ulong *p;
466 ulong newaddr;
467
468 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
469
Heinrich Schuchardt3ce78292018-10-13 20:52:08 -0700470 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
471 rel->info, *p, rel->offset);
Alexander Graf50149ea2016-03-04 01:10:01 +0100472
Rick Chen6836adb2018-05-28 19:06:37 +0800473 switch (rel->info & R_MASK) {
474 case R_RELATIVE:
Alexander Graf50149ea2016-03-04 01:10:01 +0100475#ifdef IS_RELA
476 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
477#else
478 newaddr = *p - lastoff + offset;
479#endif
Rick Chen6836adb2018-05-28 19:06:37 +0800480 break;
481#ifdef R_ABSOLUTE
482 case R_ABSOLUTE: {
483 ulong symidx = rel->info >> SYM_INDEX;
484 extern struct dyn_sym __dyn_sym_start[];
485 newaddr = __dyn_sym_start[symidx].addr + offset;
Alexander Grafafdc4fc2018-11-04 22:25:22 +0100486#ifdef IS_RELA
487 newaddr -= CONFIG_SYS_TEXT_BASE;
488#endif
Rick Chen6836adb2018-05-28 19:06:37 +0800489 break;
490 }
491#endif
492 default:
Heinrich Schuchardt3ce78292018-10-13 20:52:08 -0700493 if (!efi_runtime_tobedetached(p))
494 printf("%s: Unknown relocation type %llx\n",
495 __func__, rel->info & R_MASK);
Rick Chen6836adb2018-05-28 19:06:37 +0800496 continue;
497 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100498
499 /* Check if the relocation is inside bounds */
500 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt591cf2e2017-09-18 22:11:34 +0200501 newaddr > (map->virtual_start +
502 (map->num_pages << EFI_PAGE_SHIFT)))) {
Alexander Graf50149ea2016-03-04 01:10:01 +0100503 if (!efi_runtime_tobedetached(p))
Heinrich Schuchardt3ce78292018-10-13 20:52:08 -0700504 printf("%s: Relocation at %p is out of "
505 "range (%lx)\n", __func__, p, newaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100506 continue;
507 }
508
Alexander Grafedcef3b2016-06-02 11:38:27 +0200509 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100510 *p = newaddr;
Alexander Graf36c37a82016-04-11 23:20:39 +0200511 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
512 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf50149ea2016-03-04 01:10:01 +0100513 }
514
515#ifndef IS_RELA
516 lastoff = offset;
517#endif
518
519 invalidate_icache_all();
520}
521
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200522/**
523 * efi_set_virtual_address_map() - change from physical to virtual mapping
524 *
525 * This function implements the SetVirtualAddressMap() runtime service.
526 *
527 * See the Unified Extensible Firmware Interface (UEFI) specification for
528 * details.
529 *
530 * @memory_map_size: size of the virtual map
531 * @descriptor_size: size of an entry in the map
532 * @descriptor_version: version of the map entries
533 * @virtmap: virtual address mapping information
534 * Return: status code
535 */
Alexander Graf50149ea2016-03-04 01:10:01 +0100536static efi_status_t EFIAPI efi_set_virtual_address_map(
537 unsigned long memory_map_size,
538 unsigned long descriptor_size,
539 uint32_t descriptor_version,
540 struct efi_mem_desc *virtmap)
541{
Alexander Graf50149ea2016-03-04 01:10:01 +0100542 int n = memory_map_size / descriptor_size;
543 int i;
Alexander Graf5c38e052018-12-11 10:00:42 +0100544 int rt_code_sections = 0;
Alexander Graf50149ea2016-03-04 01:10:01 +0100545
546 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
547 descriptor_version, virtmap);
548
Alexander Graf5c38e052018-12-11 10:00:42 +0100549 /*
550 * TODO:
551 * Further down we are cheating. While really we should implement
552 * SetVirtualAddressMap() events and ConvertPointer() to allow
553 * dynamically loaded drivers to expose runtime services, we don't
554 * today.
555 *
556 * So let's ensure we see exactly one single runtime section, as
557 * that is the built-in one. If we see more (or less), someone must
558 * have tried adding or removing to that which we don't support yet.
559 * In that case, let's better fail rather than expose broken runtime
560 * services.
561 */
562 for (i = 0; i < n; i++) {
563 struct efi_mem_desc *map = (void*)virtmap +
564 (descriptor_size * i);
565
566 if (map->type == EFI_RUNTIME_SERVICES_CODE)
567 rt_code_sections++;
568 }
569
570 if (rt_code_sections != 1) {
571 /*
572 * We expose exactly one single runtime code section, so
573 * something is definitely going wrong.
574 */
575 return EFI_EXIT(EFI_INVALID_PARAMETER);
576 }
577
Alexander Graf80a48002016-08-16 21:08:45 +0200578 /* Rebind mmio pointers */
579 for (i = 0; i < n; i++) {
580 struct efi_mem_desc *map = (void*)virtmap +
581 (descriptor_size * i);
582 struct list_head *lhandle;
583 efi_physical_addr_t map_start = map->physical_start;
584 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
585 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt07240da2018-08-04 23:16:06 +0200586 u64 off = map->virtual_start - map_start;
Alexander Graf80a48002016-08-16 21:08:45 +0200587
588 /* Adjust all mmio pointers in this region */
589 list_for_each(lhandle, &efi_runtime_mmio) {
590 struct efi_runtime_mmio_list *lmmio;
591
592 lmmio = list_entry(lhandle,
593 struct efi_runtime_mmio_list,
594 link);
595 if ((map_start <= lmmio->paddr) &&
596 (map_end >= lmmio->paddr)) {
Alexander Graf80a48002016-08-16 21:08:45 +0200597 uintptr_t new_addr = lmmio->paddr + off;
598 *lmmio->ptr = (void *)new_addr;
599 }
600 }
Heinrich Schuchardt07240da2018-08-04 23:16:06 +0200601 if ((map_start <= (uintptr_t)systab.tables) &&
602 (map_end >= (uintptr_t)systab.tables)) {
603 char *ptr = (char *)systab.tables;
604
605 ptr += off;
606 systab.tables = (struct efi_configuration_table *)ptr;
607 }
Alexander Graf80a48002016-08-16 21:08:45 +0200608 }
609
610 /* Move the actual runtime code over */
Alexander Graf50149ea2016-03-04 01:10:01 +0100611 for (i = 0; i < n; i++) {
612 struct efi_mem_desc *map;
613
614 map = (void*)virtmap + (descriptor_size * i);
615 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf80a48002016-08-16 21:08:45 +0200616 ulong new_offset = map->virtual_start -
Alexander Graf5c38e052018-12-11 10:00:42 +0100617 map->physical_start + gd->relocaddr;
Alexander Graf50149ea2016-03-04 01:10:01 +0100618
619 efi_runtime_relocate(new_offset, map);
620 /* Once we're virtual, we can no longer handle
621 complex callbacks */
622 efi_runtime_detach(new_offset);
623 return EFI_EXIT(EFI_SUCCESS);
624 }
625 }
626
627 return EFI_EXIT(EFI_INVALID_PARAMETER);
628}
629
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200630/**
631 * efi_add_runtime_mmio() - add memory-mapped IO region
632 *
633 * This function adds a memory-mapped IO region to the memory map to make it
634 * available at runtime.
635 *
Heinrich Schuchardtfb34f292018-12-23 02:35:13 +0100636 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
637 * IO region
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200638 * @len: size of the memory-mapped IO region
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200639 * Returns: status code
640 */
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100641efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf80a48002016-08-16 21:08:45 +0200642{
643 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.deaee4f062017-08-11 21:19:37 +0200644 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf813468c2018-03-15 15:08:16 +0100645 uint64_t addr = *(uintptr_t *)mmio_ptr;
646 uint64_t retaddr;
647
648 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
649 if (retaddr != addr)
650 return EFI_OUT_OF_RESOURCES;
Alexander Graf80a48002016-08-16 21:08:45 +0200651
652 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100653 if (!newmmio)
654 return EFI_OUT_OF_RESOURCES;
Alexander Graf80a48002016-08-16 21:08:45 +0200655 newmmio->ptr = mmio_ptr;
656 newmmio->paddr = *(uintptr_t *)mmio_ptr;
657 newmmio->len = len;
658 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100659
Alexander Graf813468c2018-03-15 15:08:16 +0100660 return EFI_SUCCESS;
Alexander Graf80a48002016-08-16 21:08:45 +0200661}
662
Alexander Graf50149ea2016-03-04 01:10:01 +0100663/*
664 * In the second stage, U-Boot has disappeared. To isolate our runtime code
665 * that at this point still exists from the rest, we put it into a special
666 * section.
667 *
668 * !!WARNING!!
669 *
670 * This means that we can not rely on any code outside of this file in any
671 * function or variable below this line.
672 *
673 * Please keep everything fully self-contained and annotated with
Alexander Graf3c63db92016-10-14 13:45:30 +0200674 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf50149ea2016-03-04 01:10:01 +0100675 */
676
677/*
678 * Relocate the EFI runtime stub to a different place. We need to call this
679 * the first time we expose the runtime interface to a user and on set virtual
680 * address map calls.
681 */
682
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200683/**
684 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
685 *
686 * This function is used after SetVirtualAddressMap() is called as replacement
687 * for services that are not available anymore due to constraints of the U-Boot
688 * implementation.
689 *
690 * Return: EFI_UNSUPPORTED
691 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200692static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100693{
694 return EFI_UNSUPPORTED;
695}
696
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200697/**
698 * efi_device_error() - replacement function, returns EFI_DEVICE_ERROR
699 *
700 * This function is used after SetVirtualAddressMap() is called as replacement
701 * for services that are not available anymore due to constraints of the U-Boot
702 * implementation.
703 *
704 * Return: EFI_DEVICE_ERROR
705 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200706static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100707{
708 return EFI_DEVICE_ERROR;
709}
710
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200711/**
712 * efi_invalid_parameter() - replacement function, returns EFI_INVALID_PARAMETER
713 *
714 * This function is used after SetVirtualAddressMap() is called as replacement
715 * for services that are not available anymore due to constraints of the U-Boot
716 * implementation.
717 *
718 * Return: EFI_INVALID_PARAMETER
719 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200720static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100721{
722 return EFI_INVALID_PARAMETER;
723}
724
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200725/**
726 * efi_update_capsule() - process information from operating system
727 *
728 * This function implements the UpdateCapsule() runtime service.
729 *
730 * See the Unified Extensible Firmware Interface (UEFI) specification for
731 * details.
732 *
733 * @capsule_header_array: pointer to array of virtual pointers
734 * @capsule_count: number of pointers in capsule_header_array
735 * @scatter_gather_list: pointer to arry of physical pointers
736 * Returns: status code
737 */
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100738efi_status_t __efi_runtime EFIAPI efi_update_capsule(
739 struct efi_capsule_header **capsule_header_array,
740 efi_uintn_t capsule_count,
741 u64 scatter_gather_list)
742{
743 return EFI_UNSUPPORTED;
744}
745
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200746/**
747 * efi_query_capsule_caps() - check if capsule is supported
748 *
749 * This function implements the QueryCapsuleCapabilities() runtime service.
750 *
751 * See the Unified Extensible Firmware Interface (UEFI) specification for
752 * details.
753 *
754 * @capsule_header_array: pointer to array of virtual pointers
755 * @capsule_count: number of pointers in capsule_header_array
Heinrich Schuchardtcc0bfc02018-09-03 20:59:25 +0200756 * @maximum_capsule_size: maximum capsule size
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200757 * @reset_type: type of reset needed for capsule update
758 * Returns: status code
759 */
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100760efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
761 struct efi_capsule_header **capsule_header_array,
762 efi_uintn_t capsule_count,
AKASHI Takahiro19dd9072018-11-14 16:18:53 +0900763 u64 *maximum_capsule_size,
764 u32 *reset_type)
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100765{
766 return EFI_UNSUPPORTED;
767}
768
Alexander Graf3c63db92016-10-14 13:45:30 +0200769struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf50149ea2016-03-04 01:10:01 +0100770 .hdr = {
771 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +0200772 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +0200773 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf50149ea2016-03-04 01:10:01 +0100774 },
Alexander Graf80a48002016-08-16 21:08:45 +0200775 .get_time = &efi_get_time_boottime,
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200776 .set_time = &efi_set_time_boottime,
Alexander Graf50149ea2016-03-04 01:10:01 +0100777 .get_wakeup_time = (void *)&efi_unimplemented,
778 .set_wakeup_time = (void *)&efi_unimplemented,
779 .set_virtual_address_map = &efi_set_virtual_address_map,
780 .convert_pointer = (void *)&efi_invalid_parameter,
Rob Clarkad644e72017-09-13 18:05:37 -0400781 .get_variable = efi_get_variable,
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200782 .get_next_variable_name = efi_get_next_variable_name,
Rob Clarkad644e72017-09-13 18:05:37 -0400783 .set_variable = efi_set_variable,
Alexander Graf50149ea2016-03-04 01:10:01 +0100784 .get_next_high_mono_count = (void *)&efi_device_error,
Alexander Graf80a48002016-08-16 21:08:45 +0200785 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100786 .update_capsule = efi_update_capsule,
787 .query_capsule_caps = efi_query_capsule_caps,
788 .query_variable_info = efi_query_variable_info,
Alexander Graf50149ea2016-03-04 01:10:01 +0100789};