blob: df0485cdad3f6f09bb3a1fc1e12235d8f7920e06 [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>
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010011#include <dm.h>
Alexander Graf46c15142018-06-18 17:23:11 +020012#include <elf.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010013#include <efi_loader.h>
14#include <rtc.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070015#include <u-boot/crc.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010016
17/* For manual relocation support */
18DECLARE_GLOBAL_DATA_PTR;
19
Alexander Graf77256092016-08-16 21:08:45 +020020struct efi_runtime_mmio_list {
21 struct list_head link;
22 void **ptr;
23 u64 paddr;
24 u64 len;
25};
26
27/* This list contains all runtime available mmio regions */
28LIST_HEAD(efi_runtime_mmio);
29
Alexander Graf393dd912016-10-14 13:45:30 +020030static efi_status_t __efi_runtime EFIAPI efi_unimplemented(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
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +020086static __efi_runtime_data struct efi_mem_desc *efi_virtmap;
87static __efi_runtime_data efi_uintn_t efi_descriptor_count;
88static __efi_runtime_data efi_uintn_t efi_descriptor_size;
89
Alexander Graf0bd425a2016-03-04 01:10:01 +010090/*
Heinrich Schuchardt11121542018-09-03 19:29:41 +020091 * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI
Alexander Graf0bd425a2016-03-04 01:10:01 +010092 * payload are running concurrently at the same time. In this mode, we can
93 * handle a good number of runtime callbacks
94 */
95
AKASHI Takahiro32401842019-06-05 13:21:38 +090096efi_status_t efi_init_runtime_supported(void)
97{
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +020098 u16 efi_runtime_services_supported =
99 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP |
100 EFI_RT_SUPPORTED_CONVERT_POINTER;
AKASHI Takahiro32401842019-06-05 13:21:38 +0900101
102 /*
103 * This value must be synced with efi_runtime_detach_list
104 * as well as efi_runtime_services.
105 */
Heinrich Schuchardt05874fb2019-07-05 18:12:16 +0200106#ifdef CONFIG_EFI_HAVE_RUNTIME_RESET
AKASHI Takahiro32401842019-06-05 13:21:38 +0900107 efi_runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM;
108#endif
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200109
AKASHI Takahiro32401842019-06-05 13:21:38 +0900110 return EFI_CALL(efi_set_variable(L"RuntimeServicesSupported",
111 &efi_global_variable_guid,
112 EFI_VARIABLE_BOOTSERVICE_ACCESS |
113 EFI_VARIABLE_RUNTIME_ACCESS,
114 sizeof(efi_runtime_services_supported),
115 &efi_runtime_services_supported));
116}
117
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200118/**
119 * efi_update_table_header_crc32() - Update crc32 in table header
120 *
121 * @table: EFI table
122 */
123void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
124{
125 table->crc32 = 0;
126 table->crc32 = crc32(0, (const unsigned char *)table,
127 table->headersize);
128}
129
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200130/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200131 * efi_reset_system_boottime() - reset system at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200132 *
133 * This function implements the ResetSystem() runtime service before
134 * SetVirtualAddressMap() is called.
135 *
136 * See the Unified Extensible Firmware Interface (UEFI) specification for
137 * details.
138 *
139 * @reset_type: type of reset to perform
140 * @reset_status: status code for the reset
141 * @data_size: size of reset_data
142 * @reset_data: information about the reset
143 */
Alexander Graf77256092016-08-16 21:08:45 +0200144static void EFIAPI efi_reset_system_boottime(
145 enum efi_reset_type reset_type,
146 efi_status_t reset_status,
147 unsigned long data_size, void *reset_data)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100148{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100149 struct efi_event *evt;
150
Alexander Graf0bd425a2016-03-04 01:10:01 +0100151 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
152 reset_data);
153
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100154 /* Notify reset */
155 list_for_each_entry(evt, &efi_events, link) {
156 if (evt->group &&
157 !guidcmp(evt->group,
158 &efi_guid_event_group_reset_system)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200159 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100160 break;
161 }
162 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100163 switch (reset_type) {
164 case EFI_RESET_COLD:
165 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +0100166 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100167 do_reset(NULL, 0, 0, NULL);
168 break;
169 case EFI_RESET_SHUTDOWN:
Heinrich Schuchardt44489452018-10-16 07:44:53 +0200170#ifdef CONFIG_CMD_POWEROFF
171 do_poweroff(NULL, 0, 0, NULL);
172#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +0100173 break;
174 }
175
Alexander Graf77256092016-08-16 21:08:45 +0200176 while (1) { }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100177}
178
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200179/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200180 * efi_get_time_boottime() - get current time at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200181 *
182 * This function implements the GetTime runtime service before
183 * SetVirtualAddressMap() is called.
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200184 *
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200185 * See the Unified Extensible Firmware Interface (UEFI) specification
186 * for details.
187 *
188 * @time: pointer to structure to receive current time
189 * @capabilities: pointer to structure to receive RTC properties
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200190 * Returns: status code
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200191 */
Alexander Graf77256092016-08-16 21:08:45 +0200192static efi_status_t EFIAPI efi_get_time_boottime(
193 struct efi_time *time,
194 struct efi_time_cap *capabilities)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100195{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200196#ifdef CONFIG_EFI_GET_TIME
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200197 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200198 struct rtc_time tm;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100199 struct udevice *dev;
200
201 EFI_ENTRY("%p %p", time, capabilities);
202
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200203 if (!time) {
204 ret = EFI_INVALID_PARAMETER;
205 goto out;
206 }
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200207 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
208 dm_rtc_get(dev, &tm)) {
209 ret = EFI_UNSUPPORTED;
210 goto out;
211 }
212 if (dm_rtc_get(dev, &tm)) {
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200213 ret = EFI_DEVICE_ERROR;
214 goto out;
215 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100216
217 memset(time, 0, sizeof(*time));
218 time->year = tm.tm_year;
219 time->month = tm.tm_mon;
220 time->day = tm.tm_mday;
221 time->hour = tm.tm_hour;
222 time->minute = tm.tm_min;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200223 time->second = tm.tm_sec;
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200224 if (tm.tm_isdst)
225 time->daylight =
226 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200227 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100228
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200229 if (capabilities) {
230 /* Set reasonable dummy values */
231 capabilities->resolution = 1; /* 1 Hz */
232 capabilities->accuracy = 100000000; /* 100 ppm */
233 capabilities->sets_to_zero = false;
234 }
235out:
236 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100237#else
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200238 EFI_ENTRY("%p %p", time, capabilities);
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200239 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100240#endif
241}
242
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200243#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200244
245/**
246 * efi_validate_time() - checks if timestamp is valid
247 *
248 * @time: timestamp to validate
249 * Returns: 0 if timestamp is valid, 1 otherwise
250 */
251static int efi_validate_time(struct efi_time *time)
252{
253 return (!time ||
254 time->year < 1900 || time->year > 9999 ||
255 !time->month || time->month > 12 || !time->day ||
256 time->day > rtc_month_days(time->month - 1, time->year) ||
257 time->hour > 23 || time->minute > 59 || time->second > 59 ||
258 time->nanosecond > 999999999 ||
259 time->daylight &
260 ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
261 ((time->timezone < -1440 || time->timezone > 1440) &&
262 time->timezone != EFI_UNSPECIFIED_TIMEZONE));
263}
264
265#endif
266
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200267/**
268 * efi_set_time_boottime() - set current time
269 *
270 * This function implements the SetTime() runtime service before
271 * SetVirtualAddressMap() is called.
272 *
273 * See the Unified Extensible Firmware Interface (UEFI) specification
274 * for details.
275 *
276 * @time: pointer to structure to with current time
277 * Returns: status code
278 */
279static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
280{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200281#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200282 efi_status_t ret = EFI_SUCCESS;
283 struct rtc_time tm;
284 struct udevice *dev;
Alexander Graf77256092016-08-16 21:08:45 +0200285
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200286 EFI_ENTRY("%p", time);
287
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200288 if (efi_validate_time(time)) {
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200289 ret = EFI_INVALID_PARAMETER;
290 goto out;
291 }
292
293 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
294 ret = EFI_UNSUPPORTED;
295 goto out;
296 }
297
298 memset(&tm, 0, sizeof(tm));
299 tm.tm_year = time->year;
300 tm.tm_mon = time->month;
301 tm.tm_mday = time->day;
302 tm.tm_hour = time->hour;
303 tm.tm_min = time->minute;
304 tm.tm_sec = time->second;
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200305 tm.tm_isdst = time->daylight ==
306 (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200307 /* Calculate day of week */
308 rtc_calc_weekday(&tm);
309
310 if (dm_rtc_set(dev, &tm))
311 ret = EFI_DEVICE_ERROR;
312out:
313 return EFI_EXIT(ret);
314#else
315 EFI_ENTRY("%p", time);
316 return EFI_EXIT(EFI_UNSUPPORTED);
317#endif
318}
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200319/**
320 * efi_reset_system() - reset system
321 *
322 * This function implements the ResetSystem() runtime service after
323 * SetVirtualAddressMap() is called. It only executes an endless loop.
324 * Boards may override the helpers below to implement reset functionality.
325 *
326 * See the Unified Extensible Firmware Interface (UEFI) specification for
327 * details.
328 *
329 * @reset_type: type of reset to perform
330 * @reset_status: status code for the reset
331 * @data_size: size of reset_data
332 * @reset_data: information about the reset
333 */
Alexander Graf393dd912016-10-14 13:45:30 +0200334void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200335 enum efi_reset_type reset_type,
336 efi_status_t reset_status,
337 unsigned long data_size, void *reset_data)
338{
339 /* Nothing we can do */
340 while (1) { }
341}
342
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200343/**
344 * efi_reset_system_init() - initialize the reset driver
345 *
346 * Boards may override this function to initialize the reset driver.
347 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100348efi_status_t __weak efi_reset_system_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200349{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100350 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200351}
352
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200353/**
354 * efi_get_time() - get current time
355 *
356 * This function implements the GetTime runtime service after
357 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
358 * anymore only an error code is returned.
359 *
360 * See the Unified Extensible Firmware Interface (UEFI) specification
361 * for details.
362 *
363 * @time: pointer to structure to receive current time
364 * @capabilities: pointer to structure to receive RTC properties
365 * Returns: status code
366 */
Alexander Graf393dd912016-10-14 13:45:30 +0200367efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200368 struct efi_time *time,
369 struct efi_time_cap *capabilities)
370{
Heinrich Schuchardtb4ba5be2019-06-13 18:42:40 +0200371 return EFI_UNSUPPORTED;
Alexander Graf77256092016-08-16 21:08:45 +0200372}
373
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200374/**
375 * efi_set_time() - set current time
376 *
377 * This function implements the SetTime runtime service after
378 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
379 * anymore only an error code is returned.
380 *
381 * See the Unified Extensible Firmware Interface (UEFI) specification
382 * for details.
383 *
384 * @time: pointer to structure to with current time
385 * Returns: status code
386 */
387efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
388{
389 return EFI_UNSUPPORTED;
390}
391
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000392/**
393 * efi_is_runtime_service_pointer() - check if pointer points to runtime table
394 *
395 * @p: pointer to check
396 * Return: true if the pointer points to a service function pointer in the
397 * runtime table
398 */
399static bool efi_is_runtime_service_pointer(void *p)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100400{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200401 return (p >= (void *)&efi_runtime_services.get_time &&
402 p <= (void *)&efi_runtime_services.query_variable_info) ||
403 p == (void *)&efi_events.prev ||
404 p == (void *)&efi_events.next;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100405}
406
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200407/**
408 * efi_runtime_detach() - detach unimplemented runtime functions
409 */
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +0200410void efi_runtime_detach(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100411{
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200412 efi_runtime_services.reset_system = efi_reset_system;
413 efi_runtime_services.get_time = efi_get_time;
414 efi_runtime_services.set_time = efi_set_time;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100415
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200416 /* Update CRC32 */
417 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000418}
419
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200420/**
421 * efi_set_virtual_address_map_runtime() - change from physical to virtual
422 * mapping
423 *
424 * This function implements the SetVirtualAddressMap() runtime service after
425 * it is first called.
426 *
427 * See the Unified Extensible Firmware Interface (UEFI) specification for
428 * details.
429 *
430 * @memory_map_size: size of the virtual map
431 * @descriptor_size: size of an entry in the map
432 * @descriptor_version: version of the map entries
433 * @virtmap: virtual address mapping information
434 * Return: status code EFI_UNSUPPORTED
435 */
Heinrich Schuchardt2b6bd382019-07-12 22:41:43 +0200436static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map_runtime(
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200437 efi_uintn_t memory_map_size,
438 efi_uintn_t descriptor_size,
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200439 uint32_t descriptor_version,
440 struct efi_mem_desc *virtmap)
441{
442 return EFI_UNSUPPORTED;
443}
444
445/**
446 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
447 *
448 * This function implements the ConvertPointer() runtime service after
449 * the first call to SetVirtualAddressMap().
450 *
451 * See the Unified Extensible Firmware Interface (UEFI) specification for
452 * details.
453 *
454 * @debug_disposition: indicates if pointer may be converted to NULL
455 * @address: pointer to be converted
456 * Return: status code EFI_UNSUPPORTED
457 */
458static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime(
459 efi_uintn_t debug_disposition, void **address)
460{
461 return EFI_UNSUPPORTED;
462}
463
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200464/**
465 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
466 *
467 * This function implements the ConvertPointer() runtime service until
468 * the first call to SetVirtualAddressMap().
469 *
470 * See the Unified Extensible Firmware Interface (UEFI) specification for
471 * details.
472 *
473 * @debug_disposition: indicates if pointer may be converted to NULL
474 * @address: pointer to be converted
475 * Return: status code EFI_UNSUPPORTED
476 */
477static __efi_runtime efi_status_t EFIAPI efi_convert_pointer(
478 efi_uintn_t debug_disposition, void **address)
479{
480 efi_physical_addr_t addr = (uintptr_t)*address;
481 efi_uintn_t i;
482 efi_status_t ret = EFI_NOT_FOUND;
483
484 EFI_ENTRY("%zu %p", debug_disposition, address);
485
486 if (!efi_virtmap) {
487 ret = EFI_UNSUPPORTED;
488 goto out;
489 }
490
491 if (!address) {
492 ret = EFI_INVALID_PARAMETER;
493 goto out;
494 }
495
496 for (i = 0; i < efi_descriptor_count; i++) {
497 struct efi_mem_desc *map = (void *)efi_virtmap +
498 (efi_descriptor_size * i);
499
500 if (addr >= map->physical_start &&
501 (addr < map->physical_start
502 + (map->num_pages << EFI_PAGE_SHIFT))) {
503 *address = (void *)(uintptr_t)
504 (addr + map->virtual_start -
505 map->physical_start);
506
507 ret = EFI_SUCCESS;
508 break;
509 }
510 }
511
512out:
513 return EFI_EXIT(ret);
514}
515
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000516static __efi_runtime void efi_relocate_runtime_table(ulong offset)
517{
518 ulong patchoff;
519 void **pos;
520
521 /* Relocate the runtime services pointers */
522 patchoff = offset - gd->relocaddr;
523 for (pos = (void **)&efi_runtime_services.get_time;
524 pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200525 if (*pos)
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000526 *pos += patchoff;
527 }
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200528
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200529 /*
530 * The entry for SetVirtualAddress() must point to a physical address.
531 * After the first execution the service must return EFI_UNSUPPORTED.
532 */
533 efi_runtime_services.set_virtual_address_map =
534 &efi_set_virtual_address_map_runtime;
535
536 /*
537 * The entry for ConvertPointer() must point to a physical address.
538 * The service is not usable after SetVirtualAddress().
539 */
540 efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime;
541
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200542 /*
543 * TODO: Update UEFI variable RuntimeServicesSupported removing flags
544 * EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP and
545 * EFI_RT_SUPPORTED_CONVERT_POINTER as required by the UEFI spec 2.8.
546 */
547
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200548 /* Update CRC32 */
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200549 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100550}
551
552/* Relocate EFI runtime to uboot_reloc_base = offset */
553void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
554{
555#ifdef IS_RELA
556 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
557#else
558 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
559 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
560#endif
561
Alexander Graf62a67482016-06-02 11:38:27 +0200562 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100563 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
564 ulong base = CONFIG_SYS_TEXT_BASE;
565 ulong *p;
566 ulong newaddr;
567
568 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
569
Heinrich Schuchardt33691582019-08-14 06:49:09 +0200570 /*
571 * The runtime services table is updated in
572 * efi_relocate_runtime_table()
573 */
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000574 if (map && efi_is_runtime_service_pointer(p))
575 continue;
576
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700577 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
578 rel->info, *p, rel->offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100579
Rick Chen9677a372018-05-28 19:06:37 +0800580 switch (rel->info & R_MASK) {
581 case R_RELATIVE:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100582#ifdef IS_RELA
583 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
584#else
585 newaddr = *p - lastoff + offset;
586#endif
Rick Chen9677a372018-05-28 19:06:37 +0800587 break;
588#ifdef R_ABSOLUTE
589 case R_ABSOLUTE: {
590 ulong symidx = rel->info >> SYM_INDEX;
591 extern struct dyn_sym __dyn_sym_start[];
592 newaddr = __dyn_sym_start[symidx].addr + offset;
Alexander Grafdf0f6c72018-11-04 22:25:22 +0100593#ifdef IS_RELA
594 newaddr -= CONFIG_SYS_TEXT_BASE;
595#endif
Rick Chen9677a372018-05-28 19:06:37 +0800596 break;
597 }
598#endif
599 default:
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000600 printf("%s: Unknown relocation type %llx\n",
601 __func__, rel->info & R_MASK);
Rick Chen9677a372018-05-28 19:06:37 +0800602 continue;
603 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100604
605 /* Check if the relocation is inside bounds */
606 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200607 newaddr > (map->virtual_start +
608 (map->num_pages << EFI_PAGE_SHIFT)))) {
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000609 printf("%s: Relocation at %p is out of range (%lx)\n",
610 __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100611 continue;
612 }
613
Alexander Graf62a67482016-06-02 11:38:27 +0200614 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100615 *p = newaddr;
Alexander Grafe5702322016-04-11 23:20:39 +0200616 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
617 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf0bd425a2016-03-04 01:10:01 +0100618 }
619
620#ifndef IS_RELA
621 lastoff = offset;
622#endif
623
624 invalidate_icache_all();
625}
626
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200627/**
628 * efi_set_virtual_address_map() - change from physical to virtual mapping
629 *
630 * This function implements the SetVirtualAddressMap() runtime service.
631 *
632 * See the Unified Extensible Firmware Interface (UEFI) specification for
633 * details.
634 *
635 * @memory_map_size: size of the virtual map
636 * @descriptor_size: size of an entry in the map
637 * @descriptor_version: version of the map entries
638 * @virtmap: virtual address mapping information
639 * Return: status code
640 */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100641static efi_status_t EFIAPI efi_set_virtual_address_map(
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200642 efi_uintn_t memory_map_size,
643 efi_uintn_t descriptor_size,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100644 uint32_t descriptor_version,
645 struct efi_mem_desc *virtmap)
646{
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200647 efi_uintn_t n = memory_map_size / descriptor_size;
648 efi_uintn_t i;
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200649 efi_status_t ret = EFI_INVALID_PARAMETER;
Alexander Graf86e00212018-12-11 10:00:42 +0100650 int rt_code_sections = 0;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200651 struct efi_event *event;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100652
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200653 EFI_ENTRY("%zx %zx %x %p", memory_map_size, descriptor_size,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100654 descriptor_version, virtmap);
655
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200656 if (descriptor_version != EFI_MEMORY_DESCRIPTOR_VERSION ||
657 descriptor_size < sizeof(struct efi_mem_desc))
658 goto out;
659
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200660 efi_virtmap = virtmap;
661 efi_descriptor_size = descriptor_size;
662 efi_descriptor_count = n;
663
Alexander Graf86e00212018-12-11 10:00:42 +0100664 /*
665 * TODO:
666 * Further down we are cheating. While really we should implement
667 * SetVirtualAddressMap() events and ConvertPointer() to allow
668 * dynamically loaded drivers to expose runtime services, we don't
669 * today.
670 *
671 * So let's ensure we see exactly one single runtime section, as
672 * that is the built-in one. If we see more (or less), someone must
673 * have tried adding or removing to that which we don't support yet.
674 * In that case, let's better fail rather than expose broken runtime
675 * services.
676 */
677 for (i = 0; i < n; i++) {
678 struct efi_mem_desc *map = (void*)virtmap +
679 (descriptor_size * i);
680
681 if (map->type == EFI_RUNTIME_SERVICES_CODE)
682 rt_code_sections++;
683 }
684
685 if (rt_code_sections != 1) {
686 /*
687 * We expose exactly one single runtime code section, so
688 * something is definitely going wrong.
689 */
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200690 goto out;
Alexander Graf86e00212018-12-11 10:00:42 +0100691 }
692
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200693 /* Notify EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
694 list_for_each_entry(event, &efi_events, link) {
695 if (event->notify_function)
696 EFI_CALL_VOID(event->notify_function(
697 event, event->notify_context));
698 }
699
Alexander Graf77256092016-08-16 21:08:45 +0200700 /* Rebind mmio pointers */
701 for (i = 0; i < n; i++) {
702 struct efi_mem_desc *map = (void*)virtmap +
703 (descriptor_size * i);
704 struct list_head *lhandle;
705 efi_physical_addr_t map_start = map->physical_start;
706 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
707 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200708 u64 off = map->virtual_start - map_start;
Alexander Graf77256092016-08-16 21:08:45 +0200709
710 /* Adjust all mmio pointers in this region */
711 list_for_each(lhandle, &efi_runtime_mmio) {
712 struct efi_runtime_mmio_list *lmmio;
713
714 lmmio = list_entry(lhandle,
715 struct efi_runtime_mmio_list,
716 link);
717 if ((map_start <= lmmio->paddr) &&
718 (map_end >= lmmio->paddr)) {
Alexander Graf77256092016-08-16 21:08:45 +0200719 uintptr_t new_addr = lmmio->paddr + off;
720 *lmmio->ptr = (void *)new_addr;
721 }
722 }
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200723 if ((map_start <= (uintptr_t)systab.tables) &&
724 (map_end >= (uintptr_t)systab.tables)) {
725 char *ptr = (char *)systab.tables;
726
727 ptr += off;
728 systab.tables = (struct efi_configuration_table *)ptr;
729 }
Alexander Graf77256092016-08-16 21:08:45 +0200730 }
731
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000732 /* Relocate the runtime. See TODO above */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100733 for (i = 0; i < n; i++) {
734 struct efi_mem_desc *map;
735
736 map = (void*)virtmap + (descriptor_size * i);
737 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf77256092016-08-16 21:08:45 +0200738 ulong new_offset = map->virtual_start -
Alexander Graf86e00212018-12-11 10:00:42 +0100739 map->physical_start + gd->relocaddr;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100740
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000741 efi_relocate_runtime_table(new_offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100742 efi_runtime_relocate(new_offset, map);
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200743 ret = EFI_SUCCESS;
744 goto out;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100745 }
746 }
747
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200748out:
749 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100750}
751
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200752/**
753 * efi_add_runtime_mmio() - add memory-mapped IO region
754 *
755 * This function adds a memory-mapped IO region to the memory map to make it
756 * available at runtime.
757 *
Heinrich Schuchardt7e307642018-12-23 02:35:13 +0100758 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
759 * IO region
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200760 * @len: size of the memory-mapped IO region
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200761 * Returns: status code
762 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100763efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf77256092016-08-16 21:08:45 +0200764{
765 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.de93fcc7f2017-08-11 21:19:37 +0200766 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf33a83a32018-03-15 15:08:16 +0100767 uint64_t addr = *(uintptr_t *)mmio_ptr;
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100768 efi_status_t ret;
Alexander Graf33a83a32018-03-15 15:08:16 +0100769
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100770 ret = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
771 if (ret != EFI_SUCCESS)
Alexander Graf33a83a32018-03-15 15:08:16 +0100772 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200773
774 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100775 if (!newmmio)
776 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200777 newmmio->ptr = mmio_ptr;
778 newmmio->paddr = *(uintptr_t *)mmio_ptr;
779 newmmio->len = len;
780 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100781
Alexander Graf33a83a32018-03-15 15:08:16 +0100782 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200783}
784
Alexander Graf0bd425a2016-03-04 01:10:01 +0100785/*
786 * In the second stage, U-Boot has disappeared. To isolate our runtime code
787 * that at this point still exists from the rest, we put it into a special
788 * section.
789 *
790 * !!WARNING!!
791 *
792 * This means that we can not rely on any code outside of this file in any
793 * function or variable below this line.
794 *
795 * Please keep everything fully self-contained and annotated with
Alexander Graf393dd912016-10-14 13:45:30 +0200796 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf0bd425a2016-03-04 01:10:01 +0100797 */
798
799/*
800 * Relocate the EFI runtime stub to a different place. We need to call this
801 * the first time we expose the runtime interface to a user and on set virtual
802 * address map calls.
803 */
804
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200805/**
806 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
807 *
808 * This function is used after SetVirtualAddressMap() is called as replacement
809 * for services that are not available anymore due to constraints of the U-Boot
810 * implementation.
811 *
812 * Return: EFI_UNSUPPORTED
813 */
Alexander Graf393dd912016-10-14 13:45:30 +0200814static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100815{
816 return EFI_UNSUPPORTED;
817}
818
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200819/**
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200820 * efi_update_capsule() - process information from operating system
821 *
822 * This function implements the UpdateCapsule() runtime service.
823 *
824 * See the Unified Extensible Firmware Interface (UEFI) specification for
825 * details.
826 *
827 * @capsule_header_array: pointer to array of virtual pointers
828 * @capsule_count: number of pointers in capsule_header_array
829 * @scatter_gather_list: pointer to arry of physical pointers
830 * Returns: status code
831 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100832efi_status_t __efi_runtime EFIAPI efi_update_capsule(
833 struct efi_capsule_header **capsule_header_array,
834 efi_uintn_t capsule_count,
835 u64 scatter_gather_list)
836{
837 return EFI_UNSUPPORTED;
838}
839
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200840/**
841 * efi_query_capsule_caps() - check if capsule is supported
842 *
843 * This function implements the QueryCapsuleCapabilities() runtime service.
844 *
845 * See the Unified Extensible Firmware Interface (UEFI) specification for
846 * details.
847 *
848 * @capsule_header_array: pointer to array of virtual pointers
849 * @capsule_count: number of pointers in capsule_header_array
Heinrich Schuchardt1d1be362018-09-03 20:59:25 +0200850 * @maximum_capsule_size: maximum capsule size
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200851 * @reset_type: type of reset needed for capsule update
852 * Returns: status code
853 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100854efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
855 struct efi_capsule_header **capsule_header_array,
856 efi_uintn_t capsule_count,
AKASHI Takahirod840b672018-11-14 16:18:53 +0900857 u64 *maximum_capsule_size,
858 u32 *reset_type)
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100859{
860 return EFI_UNSUPPORTED;
861}
862
Alexander Graf393dd912016-10-14 13:45:30 +0200863struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100864 .hdr = {
865 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +0200866 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +0200867 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf0bd425a2016-03-04 01:10:01 +0100868 },
Alexander Graf77256092016-08-16 21:08:45 +0200869 .get_time = &efi_get_time_boottime,
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200870 .set_time = &efi_set_time_boottime,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100871 .get_wakeup_time = (void *)&efi_unimplemented,
872 .set_wakeup_time = (void *)&efi_unimplemented,
873 .set_virtual_address_map = &efi_set_virtual_address_map,
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200874 .convert_pointer = efi_convert_pointer,
Rob Clark15f3d742017-09-13 18:05:37 -0400875 .get_variable = efi_get_variable,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200876 .get_next_variable_name = efi_get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400877 .set_variable = efi_set_variable,
Heinrich Schuchardt5337a6c2019-06-20 15:40:49 +0200878 .get_next_high_mono_count = (void *)&efi_unimplemented,
Alexander Graf77256092016-08-16 21:08:45 +0200879 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100880 .update_capsule = efi_update_capsule,
881 .query_capsule_caps = efi_query_capsule_caps,
882 .query_variable_info = efi_query_variable_info,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100883};