blob: 63d8a291466812c6c7fd81e805f92aeba9461fa1 [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);
Alexander Graf0bd425a2016-03-04 01:10:01 +010029
Simon Glassfff89d42018-06-11 23:26:40 -060030/*
Heinrich Schuchardt11121542018-09-03 19:29:41 +020031 * TODO(sjg@chromium.org): These defines and structures should come from the ELF
32 * header for each architecture (or a generic header) rather than being repeated
33 * here.
Simon Glassfff89d42018-06-11 23:26:40 -060034 */
Alexander Graf0fe51922018-06-18 17:23:08 +020035#if defined(__aarch64__)
Alexander Graf46c15142018-06-18 17:23:11 +020036#define R_RELATIVE R_AARCH64_RELATIVE
Alexander Graf0bd425a2016-03-04 01:10:01 +010037#define R_MASK 0xffffffffULL
38#define IS_RELA 1
Alexander Graf0fe51922018-06-18 17:23:08 +020039#elif defined(__arm__)
Alexander Graf46c15142018-06-18 17:23:11 +020040#define R_RELATIVE R_ARM_RELATIVE
Alexander Graf0bd425a2016-03-04 01:10:01 +010041#define R_MASK 0xffULL
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -070042#elif defined(__i386__)
Simon Glasscdfe6962016-09-25 15:27:35 -060043#define R_RELATIVE R_386_RELATIVE
44#define R_MASK 0xffULL
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -070045#elif defined(__x86_64__)
46#define R_RELATIVE R_X86_64_RELATIVE
47#define R_MASK 0xffffffffULL
48#define IS_RELA 1
Alexander Graf0fe51922018-06-18 17:23:08 +020049#elif defined(__riscv)
Rick Chen9677a372018-05-28 19:06:37 +080050#define R_RELATIVE R_RISCV_RELATIVE
51#define R_MASK 0xffULL
52#define IS_RELA 1
53
54struct dyn_sym {
55 ulong foo1;
56 ulong addr;
57 u32 foo2;
58 u32 foo3;
59};
Alexander Graf0fe51922018-06-18 17:23:08 +020060#if (__riscv_xlen == 32)
Rick Chen9677a372018-05-28 19:06:37 +080061#define R_ABSOLUTE R_RISCV_32
62#define SYM_INDEX 8
Alexander Graf0fe51922018-06-18 17:23:08 +020063#elif (__riscv_xlen == 64)
Rick Chen9677a372018-05-28 19:06:37 +080064#define R_ABSOLUTE R_RISCV_64
65#define SYM_INDEX 32
Alexander Graf0fe51922018-06-18 17:23:08 +020066#else
67#error unknown riscv target
Rick Chen9677a372018-05-28 19:06:37 +080068#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +010069#else
70#error Need to add relocation awareness
71#endif
72
73struct elf_rel {
74 ulong *offset;
75 ulong info;
76};
77
78struct elf_rela {
79 ulong *offset;
80 ulong info;
81 long addend;
82};
83
84/*
Heinrich Schuchardt11121542018-09-03 19:29:41 +020085 * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI
Alexander Graf0bd425a2016-03-04 01:10:01 +010086 * payload are running concurrently at the same time. In this mode, we can
87 * handle a good number of runtime callbacks
88 */
89
AKASHI Takahiro32401842019-06-05 13:21:38 +090090efi_status_t efi_init_runtime_supported(void)
91{
92 u16 efi_runtime_services_supported = 0;
93
94 /*
95 * This value must be synced with efi_runtime_detach_list
96 * as well as efi_runtime_services.
97 */
98#if CONFIG_IS_ENABLED(ARCH_BCM283X) || \
99 CONFIG_IS_ENABLED(FSL_LAYERSCAPE) || \
100 CONFIG_IS_ENABLED(SYSRESET_X86) || \
101 CONFIG_IS_ENABLED(PSCI_RESET)
102 efi_runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM;
103#endif
104 efi_runtime_services_supported |=
105 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP;
106 return EFI_CALL(efi_set_variable(L"RuntimeServicesSupported",
107 &efi_global_variable_guid,
108 EFI_VARIABLE_BOOTSERVICE_ACCESS |
109 EFI_VARIABLE_RUNTIME_ACCESS,
110 sizeof(efi_runtime_services_supported),
111 &efi_runtime_services_supported));
112}
113
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200114/**
115 * efi_update_table_header_crc32() - Update crc32 in table header
116 *
117 * @table: EFI table
118 */
119void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
120{
121 table->crc32 = 0;
122 table->crc32 = crc32(0, (const unsigned char *)table,
123 table->headersize);
124}
125
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200126/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200127 * efi_reset_system_boottime() - reset system at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200128 *
129 * This function implements the ResetSystem() runtime service before
130 * SetVirtualAddressMap() is called.
131 *
132 * See the Unified Extensible Firmware Interface (UEFI) specification for
133 * details.
134 *
135 * @reset_type: type of reset to perform
136 * @reset_status: status code for the reset
137 * @data_size: size of reset_data
138 * @reset_data: information about the reset
139 */
Alexander Graf77256092016-08-16 21:08:45 +0200140static void EFIAPI efi_reset_system_boottime(
141 enum efi_reset_type reset_type,
142 efi_status_t reset_status,
143 unsigned long data_size, void *reset_data)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100144{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100145 struct efi_event *evt;
146
Alexander Graf0bd425a2016-03-04 01:10:01 +0100147 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
148 reset_data);
149
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100150 /* Notify reset */
151 list_for_each_entry(evt, &efi_events, link) {
152 if (evt->group &&
153 !guidcmp(evt->group,
154 &efi_guid_event_group_reset_system)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200155 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100156 break;
157 }
158 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100159 switch (reset_type) {
160 case EFI_RESET_COLD:
161 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +0100162 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100163 do_reset(NULL, 0, 0, NULL);
164 break;
165 case EFI_RESET_SHUTDOWN:
Heinrich Schuchardt44489452018-10-16 07:44:53 +0200166#ifdef CONFIG_CMD_POWEROFF
167 do_poweroff(NULL, 0, 0, NULL);
168#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +0100169 break;
170 }
171
Alexander Graf77256092016-08-16 21:08:45 +0200172 while (1) { }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100173}
174
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200175/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200176 * efi_get_time_boottime() - get current time at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200177 *
178 * This function implements the GetTime runtime service before
179 * SetVirtualAddressMap() is called.
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200180 *
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200181 * See the Unified Extensible Firmware Interface (UEFI) specification
182 * for details.
183 *
184 * @time: pointer to structure to receive current time
185 * @capabilities: pointer to structure to receive RTC properties
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200186 * Returns: status code
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200187 */
Alexander Graf77256092016-08-16 21:08:45 +0200188static efi_status_t EFIAPI efi_get_time_boottime(
189 struct efi_time *time,
190 struct efi_time_cap *capabilities)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100191{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200192#ifdef CONFIG_EFI_GET_TIME
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200193 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200194 struct rtc_time tm;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100195 struct udevice *dev;
196
197 EFI_ENTRY("%p %p", time, capabilities);
198
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200199 if (!time) {
200 ret = EFI_INVALID_PARAMETER;
201 goto out;
202 }
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200203 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
204 dm_rtc_get(dev, &tm)) {
205 ret = EFI_UNSUPPORTED;
206 goto out;
207 }
208 if (dm_rtc_get(dev, &tm)) {
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200209 ret = EFI_DEVICE_ERROR;
210 goto out;
211 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100212
213 memset(time, 0, sizeof(*time));
214 time->year = tm.tm_year;
215 time->month = tm.tm_mon;
216 time->day = tm.tm_mday;
217 time->hour = tm.tm_hour;
218 time->minute = tm.tm_min;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200219 time->second = tm.tm_sec;
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200220 if (tm.tm_isdst)
221 time->daylight =
222 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200223 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100224
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200225 if (capabilities) {
226 /* Set reasonable dummy values */
227 capabilities->resolution = 1; /* 1 Hz */
228 capabilities->accuracy = 100000000; /* 100 ppm */
229 capabilities->sets_to_zero = false;
230 }
231out:
232 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100233#else
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200234 EFI_ENTRY("%p %p", time, capabilities);
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200235 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100236#endif
237}
238
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200239#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200240
241/**
242 * efi_validate_time() - checks if timestamp is valid
243 *
244 * @time: timestamp to validate
245 * Returns: 0 if timestamp is valid, 1 otherwise
246 */
247static int efi_validate_time(struct efi_time *time)
248{
249 return (!time ||
250 time->year < 1900 || time->year > 9999 ||
251 !time->month || time->month > 12 || !time->day ||
252 time->day > rtc_month_days(time->month - 1, time->year) ||
253 time->hour > 23 || time->minute > 59 || time->second > 59 ||
254 time->nanosecond > 999999999 ||
255 time->daylight &
256 ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
257 ((time->timezone < -1440 || time->timezone > 1440) &&
258 time->timezone != EFI_UNSPECIFIED_TIMEZONE));
259}
260
261#endif
262
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200263/**
264 * efi_set_time_boottime() - set current time
265 *
266 * This function implements the SetTime() runtime service before
267 * SetVirtualAddressMap() is called.
268 *
269 * See the Unified Extensible Firmware Interface (UEFI) specification
270 * for details.
271 *
272 * @time: pointer to structure to with current time
273 * Returns: status code
274 */
275static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
276{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200277#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200278 efi_status_t ret = EFI_SUCCESS;
279 struct rtc_time tm;
280 struct udevice *dev;
Alexander Graf77256092016-08-16 21:08:45 +0200281
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200282 EFI_ENTRY("%p", time);
283
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200284 if (efi_validate_time(time)) {
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200285 ret = EFI_INVALID_PARAMETER;
286 goto out;
287 }
288
289 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
290 ret = EFI_UNSUPPORTED;
291 goto out;
292 }
293
294 memset(&tm, 0, sizeof(tm));
295 tm.tm_year = time->year;
296 tm.tm_mon = time->month;
297 tm.tm_mday = time->day;
298 tm.tm_hour = time->hour;
299 tm.tm_min = time->minute;
300 tm.tm_sec = time->second;
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200301 tm.tm_isdst = time->daylight ==
302 (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200303 /* Calculate day of week */
304 rtc_calc_weekday(&tm);
305
306 if (dm_rtc_set(dev, &tm))
307 ret = EFI_DEVICE_ERROR;
308out:
309 return EFI_EXIT(ret);
310#else
311 EFI_ENTRY("%p", time);
312 return EFI_EXIT(EFI_UNSUPPORTED);
313#endif
314}
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200315/**
316 * efi_reset_system() - reset system
317 *
318 * This function implements the ResetSystem() runtime service after
319 * SetVirtualAddressMap() is called. It only executes an endless loop.
320 * Boards may override the helpers below to implement reset functionality.
321 *
322 * See the Unified Extensible Firmware Interface (UEFI) specification for
323 * details.
324 *
325 * @reset_type: type of reset to perform
326 * @reset_status: status code for the reset
327 * @data_size: size of reset_data
328 * @reset_data: information about the reset
329 */
Alexander Graf393dd912016-10-14 13:45:30 +0200330void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200331 enum efi_reset_type reset_type,
332 efi_status_t reset_status,
333 unsigned long data_size, void *reset_data)
334{
335 /* Nothing we can do */
336 while (1) { }
337}
338
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200339/**
340 * efi_reset_system_init() - initialize the reset driver
341 *
342 * Boards may override this function to initialize the reset driver.
343 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100344efi_status_t __weak efi_reset_system_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200345{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100346 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200347}
348
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200349/**
350 * efi_get_time() - get current time
351 *
352 * This function implements the GetTime 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 receive current time
360 * @capabilities: pointer to structure to receive RTC properties
361 * Returns: status code
362 */
Alexander Graf393dd912016-10-14 13:45:30 +0200363efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200364 struct efi_time *time,
365 struct efi_time_cap *capabilities)
366{
Heinrich Schuchardtb4ba5be2019-06-13 18:42:40 +0200367 return EFI_UNSUPPORTED;
Alexander Graf77256092016-08-16 21:08:45 +0200368}
369
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200370/**
371 * efi_set_time() - set current time
372 *
373 * This function implements the SetTime runtime service after
374 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
375 * anymore only an error code is returned.
376 *
377 * See the Unified Extensible Firmware Interface (UEFI) specification
378 * for details.
379 *
380 * @time: pointer to structure to with current time
381 * Returns: status code
382 */
383efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
384{
385 return EFI_UNSUPPORTED;
386}
387
Alexander Graf0bd425a2016-03-04 01:10:01 +0100388struct efi_runtime_detach_list_struct {
389 void *ptr;
390 void *patchto;
391};
392
393static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
394 {
395 /* do_reset is gone */
396 .ptr = &efi_runtime_services.reset_system,
Alexander Graf77256092016-08-16 21:08:45 +0200397 .patchto = efi_reset_system,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100398 }, {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100399 /* RTC accessors are gone */
400 .ptr = &efi_runtime_services.get_time,
Alexander Graf77256092016-08-16 21:08:45 +0200401 .patchto = &efi_get_time,
Alexander Grafc59d3602016-05-18 00:54:47 +0200402 }, {
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200403 .ptr = &efi_runtime_services.set_time,
404 .patchto = &efi_set_time,
Rob Clark15f3d742017-09-13 18:05:37 -0400405 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100406};
407
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000408/**
409 * efi_is_runtime_service_pointer() - check if pointer points to runtime table
410 *
411 * @p: pointer to check
412 * Return: true if the pointer points to a service function pointer in the
413 * runtime table
414 */
415static bool efi_is_runtime_service_pointer(void *p)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100416{
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000417 return p >= (void *)&efi_runtime_services.get_time &&
418 p <= (void *)&efi_runtime_services.query_variable_info;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100419}
420
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000421static __efi_runtime void efi_runtime_detach(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100422{
423 int i;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100424
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000425 /*
426 * Replace boottime functions by runtime functions
427 * TODO: move this step to ExitBootServices()
428 */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100429 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
430 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
431 ulong *p = efi_runtime_detach_list[i].ptr;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100432
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000433 debug("%s: Setting %p to %lx\n", __func__, p, patchto);
434 *p = patchto;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100435 }
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000436}
437
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200438/**
439 * efi_set_virtual_address_map_runtime() - change from physical to virtual
440 * mapping
441 *
442 * This function implements the SetVirtualAddressMap() runtime service after
443 * it is first called.
444 *
445 * See the Unified Extensible Firmware Interface (UEFI) specification for
446 * details.
447 *
448 * @memory_map_size: size of the virtual map
449 * @descriptor_size: size of an entry in the map
450 * @descriptor_version: version of the map entries
451 * @virtmap: virtual address mapping information
452 * Return: status code EFI_UNSUPPORTED
453 */
454static efi_status_t EFIAPI efi_set_virtual_address_map_runtime(
455 unsigned long memory_map_size,
456 unsigned long descriptor_size,
457 uint32_t descriptor_version,
458 struct efi_mem_desc *virtmap)
459{
460 return EFI_UNSUPPORTED;
461}
462
463/**
464 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
465 *
466 * This function implements the ConvertPointer() runtime service after
467 * the first call to SetVirtualAddressMap().
468 *
469 * See the Unified Extensible Firmware Interface (UEFI) specification for
470 * details.
471 *
472 * @debug_disposition: indicates if pointer may be converted to NULL
473 * @address: pointer to be converted
474 * Return: status code EFI_UNSUPPORTED
475 */
476static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime(
477 efi_uintn_t debug_disposition, void **address)
478{
479 return EFI_UNSUPPORTED;
480}
481
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000482static __efi_runtime void efi_relocate_runtime_table(ulong offset)
483{
484 ulong patchoff;
485 void **pos;
486
487 /* Relocate the runtime services pointers */
488 patchoff = offset - gd->relocaddr;
489 for (pos = (void **)&efi_runtime_services.get_time;
490 pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200491 if (*pos)
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000492 *pos += patchoff;
493 }
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200494
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200495 /*
496 * The entry for SetVirtualAddress() must point to a physical address.
497 * After the first execution the service must return EFI_UNSUPPORTED.
498 */
499 efi_runtime_services.set_virtual_address_map =
500 &efi_set_virtual_address_map_runtime;
501
502 /*
503 * The entry for ConvertPointer() must point to a physical address.
504 * The service is not usable after SetVirtualAddress().
505 */
506 efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime;
507
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200508 /* Update CRC32 */
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200509 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100510}
511
512/* Relocate EFI runtime to uboot_reloc_base = offset */
513void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
514{
515#ifdef IS_RELA
516 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
517#else
518 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
519 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
520#endif
521
Alexander Graf62a67482016-06-02 11:38:27 +0200522 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100523 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
524 ulong base = CONFIG_SYS_TEXT_BASE;
525 ulong *p;
526 ulong newaddr;
527
528 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
529
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000530 /* The runtime services are updated in efi_runtime_detach() */
531 if (map && efi_is_runtime_service_pointer(p))
532 continue;
533
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700534 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
535 rel->info, *p, rel->offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100536
Rick Chen9677a372018-05-28 19:06:37 +0800537 switch (rel->info & R_MASK) {
538 case R_RELATIVE:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100539#ifdef IS_RELA
540 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
541#else
542 newaddr = *p - lastoff + offset;
543#endif
Rick Chen9677a372018-05-28 19:06:37 +0800544 break;
545#ifdef R_ABSOLUTE
546 case R_ABSOLUTE: {
547 ulong symidx = rel->info >> SYM_INDEX;
548 extern struct dyn_sym __dyn_sym_start[];
549 newaddr = __dyn_sym_start[symidx].addr + offset;
Alexander Grafdf0f6c72018-11-04 22:25:22 +0100550#ifdef IS_RELA
551 newaddr -= CONFIG_SYS_TEXT_BASE;
552#endif
Rick Chen9677a372018-05-28 19:06:37 +0800553 break;
554 }
555#endif
556 default:
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000557 printf("%s: Unknown relocation type %llx\n",
558 __func__, rel->info & R_MASK);
Rick Chen9677a372018-05-28 19:06:37 +0800559 continue;
560 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100561
562 /* Check if the relocation is inside bounds */
563 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200564 newaddr > (map->virtual_start +
565 (map->num_pages << EFI_PAGE_SHIFT)))) {
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000566 printf("%s: Relocation at %p is out of range (%lx)\n",
567 __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100568 continue;
569 }
570
Alexander Graf62a67482016-06-02 11:38:27 +0200571 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100572 *p = newaddr;
Alexander Grafe5702322016-04-11 23:20:39 +0200573 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
574 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf0bd425a2016-03-04 01:10:01 +0100575 }
576
577#ifndef IS_RELA
578 lastoff = offset;
579#endif
580
581 invalidate_icache_all();
582}
583
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200584/**
585 * efi_set_virtual_address_map() - change from physical to virtual mapping
586 *
587 * This function implements the SetVirtualAddressMap() runtime service.
588 *
589 * See the Unified Extensible Firmware Interface (UEFI) specification for
590 * details.
591 *
592 * @memory_map_size: size of the virtual map
593 * @descriptor_size: size of an entry in the map
594 * @descriptor_version: version of the map entries
595 * @virtmap: virtual address mapping information
596 * Return: status code
597 */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100598static efi_status_t EFIAPI efi_set_virtual_address_map(
599 unsigned long memory_map_size,
600 unsigned long descriptor_size,
601 uint32_t descriptor_version,
602 struct efi_mem_desc *virtmap)
603{
Alexander Graf0bd425a2016-03-04 01:10:01 +0100604 int n = memory_map_size / descriptor_size;
605 int i;
Alexander Graf86e00212018-12-11 10:00:42 +0100606 int rt_code_sections = 0;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100607
608 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
609 descriptor_version, virtmap);
610
Alexander Graf86e00212018-12-11 10:00:42 +0100611 /*
612 * TODO:
613 * Further down we are cheating. While really we should implement
614 * SetVirtualAddressMap() events and ConvertPointer() to allow
615 * dynamically loaded drivers to expose runtime services, we don't
616 * today.
617 *
618 * So let's ensure we see exactly one single runtime section, as
619 * that is the built-in one. If we see more (or less), someone must
620 * have tried adding or removing to that which we don't support yet.
621 * In that case, let's better fail rather than expose broken runtime
622 * services.
623 */
624 for (i = 0; i < n; i++) {
625 struct efi_mem_desc *map = (void*)virtmap +
626 (descriptor_size * i);
627
628 if (map->type == EFI_RUNTIME_SERVICES_CODE)
629 rt_code_sections++;
630 }
631
632 if (rt_code_sections != 1) {
633 /*
634 * We expose exactly one single runtime code section, so
635 * something is definitely going wrong.
636 */
637 return EFI_EXIT(EFI_INVALID_PARAMETER);
638 }
639
Alexander Graf77256092016-08-16 21:08:45 +0200640 /* Rebind mmio pointers */
641 for (i = 0; i < n; i++) {
642 struct efi_mem_desc *map = (void*)virtmap +
643 (descriptor_size * i);
644 struct list_head *lhandle;
645 efi_physical_addr_t map_start = map->physical_start;
646 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
647 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200648 u64 off = map->virtual_start - map_start;
Alexander Graf77256092016-08-16 21:08:45 +0200649
650 /* Adjust all mmio pointers in this region */
651 list_for_each(lhandle, &efi_runtime_mmio) {
652 struct efi_runtime_mmio_list *lmmio;
653
654 lmmio = list_entry(lhandle,
655 struct efi_runtime_mmio_list,
656 link);
657 if ((map_start <= lmmio->paddr) &&
658 (map_end >= lmmio->paddr)) {
Alexander Graf77256092016-08-16 21:08:45 +0200659 uintptr_t new_addr = lmmio->paddr + off;
660 *lmmio->ptr = (void *)new_addr;
661 }
662 }
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200663 if ((map_start <= (uintptr_t)systab.tables) &&
664 (map_end >= (uintptr_t)systab.tables)) {
665 char *ptr = (char *)systab.tables;
666
667 ptr += off;
668 systab.tables = (struct efi_configuration_table *)ptr;
669 }
Alexander Graf77256092016-08-16 21:08:45 +0200670 }
671
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000672 /*
673 * Some runtime services are implemented in a way that we can only offer
674 * them at boottime. Replace those function pointers.
675 *
676 * TODO: move this call to ExitBootServices().
677 */
678 efi_runtime_detach();
679
680 /* Relocate the runtime. See TODO above */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100681 for (i = 0; i < n; i++) {
682 struct efi_mem_desc *map;
683
684 map = (void*)virtmap + (descriptor_size * i);
685 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf77256092016-08-16 21:08:45 +0200686 ulong new_offset = map->virtual_start -
Alexander Graf86e00212018-12-11 10:00:42 +0100687 map->physical_start + gd->relocaddr;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100688
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000689 efi_relocate_runtime_table(new_offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100690 efi_runtime_relocate(new_offset, map);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100691 return EFI_EXIT(EFI_SUCCESS);
692 }
693 }
694
695 return EFI_EXIT(EFI_INVALID_PARAMETER);
696}
697
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200698/**
699 * efi_add_runtime_mmio() - add memory-mapped IO region
700 *
701 * This function adds a memory-mapped IO region to the memory map to make it
702 * available at runtime.
703 *
Heinrich Schuchardt7e307642018-12-23 02:35:13 +0100704 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
705 * IO region
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200706 * @len: size of the memory-mapped IO region
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200707 * Returns: status code
708 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100709efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf77256092016-08-16 21:08:45 +0200710{
711 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.de93fcc7f2017-08-11 21:19:37 +0200712 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf33a83a32018-03-15 15:08:16 +0100713 uint64_t addr = *(uintptr_t *)mmio_ptr;
714 uint64_t retaddr;
715
716 retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
717 if (retaddr != addr)
718 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200719
720 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100721 if (!newmmio)
722 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200723 newmmio->ptr = mmio_ptr;
724 newmmio->paddr = *(uintptr_t *)mmio_ptr;
725 newmmio->len = len;
726 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100727
Alexander Graf33a83a32018-03-15 15:08:16 +0100728 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200729}
730
Alexander Graf0bd425a2016-03-04 01:10:01 +0100731/*
732 * In the second stage, U-Boot has disappeared. To isolate our runtime code
733 * that at this point still exists from the rest, we put it into a special
734 * section.
735 *
736 * !!WARNING!!
737 *
738 * This means that we can not rely on any code outside of this file in any
739 * function or variable below this line.
740 *
741 * Please keep everything fully self-contained and annotated with
Alexander Graf393dd912016-10-14 13:45:30 +0200742 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf0bd425a2016-03-04 01:10:01 +0100743 */
744
745/*
746 * Relocate the EFI runtime stub to a different place. We need to call this
747 * the first time we expose the runtime interface to a user and on set virtual
748 * address map calls.
749 */
750
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200751/**
752 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
753 *
754 * This function is used after SetVirtualAddressMap() is called as replacement
755 * for services that are not available anymore due to constraints of the U-Boot
756 * implementation.
757 *
758 * Return: EFI_UNSUPPORTED
759 */
Alexander Graf393dd912016-10-14 13:45:30 +0200760static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100761{
762 return EFI_UNSUPPORTED;
763}
764
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200765/**
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200766 * efi_update_capsule() - process information from operating system
767 *
768 * This function implements the UpdateCapsule() runtime service.
769 *
770 * See the Unified Extensible Firmware Interface (UEFI) specification for
771 * details.
772 *
773 * @capsule_header_array: pointer to array of virtual pointers
774 * @capsule_count: number of pointers in capsule_header_array
775 * @scatter_gather_list: pointer to arry of physical pointers
776 * Returns: status code
777 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100778efi_status_t __efi_runtime EFIAPI efi_update_capsule(
779 struct efi_capsule_header **capsule_header_array,
780 efi_uintn_t capsule_count,
781 u64 scatter_gather_list)
782{
783 return EFI_UNSUPPORTED;
784}
785
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200786/**
787 * efi_query_capsule_caps() - check if capsule is supported
788 *
789 * This function implements the QueryCapsuleCapabilities() runtime service.
790 *
791 * See the Unified Extensible Firmware Interface (UEFI) specification for
792 * details.
793 *
794 * @capsule_header_array: pointer to array of virtual pointers
795 * @capsule_count: number of pointers in capsule_header_array
Heinrich Schuchardt1d1be362018-09-03 20:59:25 +0200796 * @maximum_capsule_size: maximum capsule size
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200797 * @reset_type: type of reset needed for capsule update
798 * Returns: status code
799 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100800efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
801 struct efi_capsule_header **capsule_header_array,
802 efi_uintn_t capsule_count,
AKASHI Takahirod840b672018-11-14 16:18:53 +0900803 u64 *maximum_capsule_size,
804 u32 *reset_type)
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100805{
806 return EFI_UNSUPPORTED;
807}
808
Alexander Graf393dd912016-10-14 13:45:30 +0200809struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100810 .hdr = {
811 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +0200812 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +0200813 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf0bd425a2016-03-04 01:10:01 +0100814 },
Alexander Graf77256092016-08-16 21:08:45 +0200815 .get_time = &efi_get_time_boottime,
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200816 .set_time = &efi_set_time_boottime,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100817 .get_wakeup_time = (void *)&efi_unimplemented,
818 .set_wakeup_time = (void *)&efi_unimplemented,
819 .set_virtual_address_map = &efi_set_virtual_address_map,
Heinrich Schuchardt5337a6c2019-06-20 15:40:49 +0200820 .convert_pointer = (void *)&efi_unimplemented,
Rob Clark15f3d742017-09-13 18:05:37 -0400821 .get_variable = efi_get_variable,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200822 .get_next_variable_name = efi_get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400823 .set_variable = efi_set_variable,
Heinrich Schuchardt5337a6c2019-06-20 15:40:49 +0200824 .get_next_high_mono_count = (void *)&efi_unimplemented,
Alexander Graf77256092016-08-16 21:08:45 +0200825 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100826 .update_capsule = efi_update_capsule,
827 .query_capsule_caps = efi_query_capsule_caps,
828 .query_variable_info = efi_query_variable_info,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100829};