blob: 72555f07fc1c8b249fd8141fc4f1180cb8d6af9e [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>
Simon Glass48b6c6b2019-11-14 12:57:16 -070014#include <u-boot/crc.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010015
16/* For manual relocation support */
17DECLARE_GLOBAL_DATA_PTR;
18
Alexander Graf77256092016-08-16 21:08:45 +020019struct efi_runtime_mmio_list {
20 struct list_head link;
21 void **ptr;
22 u64 paddr;
23 u64 len;
24};
25
26/* This list contains all runtime available mmio regions */
27LIST_HEAD(efi_runtime_mmio);
28
Alexander Graf393dd912016-10-14 13:45:30 +020029static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void);
Alexander Graf0bd425a2016-03-04 01:10:01 +010030
Simon Glassfff89d42018-06-11 23:26:40 -060031/*
Heinrich Schuchardt11121542018-09-03 19:29:41 +020032 * TODO(sjg@chromium.org): These defines and structures should come from the ELF
33 * header for each architecture (or a generic header) rather than being repeated
34 * here.
Simon Glassfff89d42018-06-11 23:26:40 -060035 */
Alexander Graf0fe51922018-06-18 17:23:08 +020036#if defined(__aarch64__)
Alexander Graf46c15142018-06-18 17:23:11 +020037#define R_RELATIVE R_AARCH64_RELATIVE
Alexander Graf0bd425a2016-03-04 01:10:01 +010038#define R_MASK 0xffffffffULL
39#define IS_RELA 1
Alexander Graf0fe51922018-06-18 17:23:08 +020040#elif defined(__arm__)
Alexander Graf46c15142018-06-18 17:23:11 +020041#define R_RELATIVE R_ARM_RELATIVE
Alexander Graf0bd425a2016-03-04 01:10:01 +010042#define R_MASK 0xffULL
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -070043#elif defined(__i386__)
Simon Glasscdfe6962016-09-25 15:27:35 -060044#define R_RELATIVE R_386_RELATIVE
45#define R_MASK 0xffULL
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -070046#elif defined(__x86_64__)
47#define R_RELATIVE R_X86_64_RELATIVE
48#define R_MASK 0xffffffffULL
49#define IS_RELA 1
Alexander Graf0fe51922018-06-18 17:23:08 +020050#elif defined(__riscv)
Rick Chen9677a372018-05-28 19:06:37 +080051#define R_RELATIVE R_RISCV_RELATIVE
52#define R_MASK 0xffULL
53#define IS_RELA 1
54
55struct dyn_sym {
56 ulong foo1;
57 ulong addr;
58 u32 foo2;
59 u32 foo3;
60};
Alexander Graf0fe51922018-06-18 17:23:08 +020061#if (__riscv_xlen == 32)
Rick Chen9677a372018-05-28 19:06:37 +080062#define R_ABSOLUTE R_RISCV_32
63#define SYM_INDEX 8
Alexander Graf0fe51922018-06-18 17:23:08 +020064#elif (__riscv_xlen == 64)
Rick Chen9677a372018-05-28 19:06:37 +080065#define R_ABSOLUTE R_RISCV_64
66#define SYM_INDEX 32
Alexander Graf0fe51922018-06-18 17:23:08 +020067#else
68#error unknown riscv target
Rick Chen9677a372018-05-28 19:06:37 +080069#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +010070#else
71#error Need to add relocation awareness
72#endif
73
74struct elf_rel {
75 ulong *offset;
76 ulong info;
77};
78
79struct elf_rela {
80 ulong *offset;
81 ulong info;
82 long addend;
83};
84
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +020085static __efi_runtime_data struct efi_mem_desc *efi_virtmap;
86static __efi_runtime_data efi_uintn_t efi_descriptor_count;
87static __efi_runtime_data efi_uintn_t efi_descriptor_size;
88
Alexander Graf0bd425a2016-03-04 01:10:01 +010089/*
Heinrich Schuchardt11121542018-09-03 19:29:41 +020090 * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI
Alexander Graf0bd425a2016-03-04 01:10:01 +010091 * payload are running concurrently at the same time. In this mode, we can
92 * handle a good number of runtime callbacks
93 */
94
AKASHI Takahiro32401842019-06-05 13:21:38 +090095efi_status_t efi_init_runtime_supported(void)
96{
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +020097 u16 efi_runtime_services_supported =
98 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP |
99 EFI_RT_SUPPORTED_CONVERT_POINTER;
AKASHI Takahiro32401842019-06-05 13:21:38 +0900100
101 /*
102 * This value must be synced with efi_runtime_detach_list
103 * as well as efi_runtime_services.
104 */
Heinrich Schuchardt05874fb2019-07-05 18:12:16 +0200105#ifdef CONFIG_EFI_HAVE_RUNTIME_RESET
AKASHI Takahiro32401842019-06-05 13:21:38 +0900106 efi_runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM;
107#endif
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200108
AKASHI Takahiro32401842019-06-05 13:21:38 +0900109 return EFI_CALL(efi_set_variable(L"RuntimeServicesSupported",
110 &efi_global_variable_guid,
111 EFI_VARIABLE_BOOTSERVICE_ACCESS |
112 EFI_VARIABLE_RUNTIME_ACCESS,
113 sizeof(efi_runtime_services_supported),
114 &efi_runtime_services_supported));
115}
116
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200117/**
118 * efi_update_table_header_crc32() - Update crc32 in table header
119 *
120 * @table: EFI table
121 */
122void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
123{
124 table->crc32 = 0;
125 table->crc32 = crc32(0, (const unsigned char *)table,
126 table->headersize);
127}
128
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200129/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200130 * efi_reset_system_boottime() - reset system at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200131 *
132 * This function implements the ResetSystem() runtime service before
133 * SetVirtualAddressMap() is called.
134 *
135 * See the Unified Extensible Firmware Interface (UEFI) specification for
136 * details.
137 *
138 * @reset_type: type of reset to perform
139 * @reset_status: status code for the reset
140 * @data_size: size of reset_data
141 * @reset_data: information about the reset
142 */
Alexander Graf77256092016-08-16 21:08:45 +0200143static void EFIAPI efi_reset_system_boottime(
144 enum efi_reset_type reset_type,
145 efi_status_t reset_status,
146 unsigned long data_size, void *reset_data)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100147{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100148 struct efi_event *evt;
149
Alexander Graf0bd425a2016-03-04 01:10:01 +0100150 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
151 reset_data);
152
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100153 /* Notify reset */
154 list_for_each_entry(evt, &efi_events, link) {
155 if (evt->group &&
156 !guidcmp(evt->group,
157 &efi_guid_event_group_reset_system)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200158 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100159 break;
160 }
161 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100162 switch (reset_type) {
163 case EFI_RESET_COLD:
164 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +0100165 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100166 do_reset(NULL, 0, 0, NULL);
167 break;
168 case EFI_RESET_SHUTDOWN:
Heinrich Schuchardt44489452018-10-16 07:44:53 +0200169#ifdef CONFIG_CMD_POWEROFF
170 do_poweroff(NULL, 0, 0, NULL);
171#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +0100172 break;
173 }
174
Alexander Graf77256092016-08-16 21:08:45 +0200175 while (1) { }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100176}
177
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200178/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200179 * efi_get_time_boottime() - get current time at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200180 *
181 * This function implements the GetTime runtime service before
182 * SetVirtualAddressMap() is called.
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200183 *
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200184 * See the Unified Extensible Firmware Interface (UEFI) specification
185 * for details.
186 *
187 * @time: pointer to structure to receive current time
188 * @capabilities: pointer to structure to receive RTC properties
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200189 * Returns: status code
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200190 */
Alexander Graf77256092016-08-16 21:08:45 +0200191static efi_status_t EFIAPI efi_get_time_boottime(
192 struct efi_time *time,
193 struct efi_time_cap *capabilities)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100194{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200195#ifdef CONFIG_EFI_GET_TIME
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200196 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200197 struct rtc_time tm;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100198 struct udevice *dev;
199
200 EFI_ENTRY("%p %p", time, capabilities);
201
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200202 if (!time) {
203 ret = EFI_INVALID_PARAMETER;
204 goto out;
205 }
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200206 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
207 dm_rtc_get(dev, &tm)) {
208 ret = EFI_UNSUPPORTED;
209 goto out;
210 }
211 if (dm_rtc_get(dev, &tm)) {
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200212 ret = EFI_DEVICE_ERROR;
213 goto out;
214 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100215
216 memset(time, 0, sizeof(*time));
217 time->year = tm.tm_year;
218 time->month = tm.tm_mon;
219 time->day = tm.tm_mday;
220 time->hour = tm.tm_hour;
221 time->minute = tm.tm_min;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200222 time->second = tm.tm_sec;
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200223 if (tm.tm_isdst)
224 time->daylight =
225 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200226 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100227
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200228 if (capabilities) {
229 /* Set reasonable dummy values */
230 capabilities->resolution = 1; /* 1 Hz */
231 capabilities->accuracy = 100000000; /* 100 ppm */
232 capabilities->sets_to_zero = false;
233 }
234out:
235 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100236#else
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200237 EFI_ENTRY("%p %p", time, capabilities);
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200238 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100239#endif
240}
241
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200242#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200243
244/**
245 * efi_validate_time() - checks if timestamp is valid
246 *
247 * @time: timestamp to validate
248 * Returns: 0 if timestamp is valid, 1 otherwise
249 */
250static int efi_validate_time(struct efi_time *time)
251{
252 return (!time ||
253 time->year < 1900 || time->year > 9999 ||
254 !time->month || time->month > 12 || !time->day ||
255 time->day > rtc_month_days(time->month - 1, time->year) ||
256 time->hour > 23 || time->minute > 59 || time->second > 59 ||
257 time->nanosecond > 999999999 ||
258 time->daylight &
259 ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
260 ((time->timezone < -1440 || time->timezone > 1440) &&
261 time->timezone != EFI_UNSPECIFIED_TIMEZONE));
262}
263
264#endif
265
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200266/**
267 * efi_set_time_boottime() - set current time
268 *
269 * This function implements the SetTime() runtime service before
270 * SetVirtualAddressMap() is called.
271 *
272 * See the Unified Extensible Firmware Interface (UEFI) specification
273 * for details.
274 *
275 * @time: pointer to structure to with current time
276 * Returns: status code
277 */
278static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
279{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200280#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200281 efi_status_t ret = EFI_SUCCESS;
282 struct rtc_time tm;
283 struct udevice *dev;
Alexander Graf77256092016-08-16 21:08:45 +0200284
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200285 EFI_ENTRY("%p", time);
286
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200287 if (efi_validate_time(time)) {
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200288 ret = EFI_INVALID_PARAMETER;
289 goto out;
290 }
291
292 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
293 ret = EFI_UNSUPPORTED;
294 goto out;
295 }
296
297 memset(&tm, 0, sizeof(tm));
298 tm.tm_year = time->year;
299 tm.tm_mon = time->month;
300 tm.tm_mday = time->day;
301 tm.tm_hour = time->hour;
302 tm.tm_min = time->minute;
303 tm.tm_sec = time->second;
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200304 tm.tm_isdst = time->daylight ==
305 (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200306 /* Calculate day of week */
307 rtc_calc_weekday(&tm);
308
309 if (dm_rtc_set(dev, &tm))
310 ret = EFI_DEVICE_ERROR;
311out:
312 return EFI_EXIT(ret);
313#else
314 EFI_ENTRY("%p", time);
315 return EFI_EXIT(EFI_UNSUPPORTED);
316#endif
317}
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200318/**
319 * efi_reset_system() - reset system
320 *
321 * This function implements the ResetSystem() runtime service after
322 * SetVirtualAddressMap() is called. It only executes an endless loop.
323 * Boards may override the helpers below to implement reset functionality.
324 *
325 * See the Unified Extensible Firmware Interface (UEFI) specification for
326 * details.
327 *
328 * @reset_type: type of reset to perform
329 * @reset_status: status code for the reset
330 * @data_size: size of reset_data
331 * @reset_data: information about the reset
332 */
Alexander Graf393dd912016-10-14 13:45:30 +0200333void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200334 enum efi_reset_type reset_type,
335 efi_status_t reset_status,
336 unsigned long data_size, void *reset_data)
337{
338 /* Nothing we can do */
339 while (1) { }
340}
341
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200342/**
343 * efi_reset_system_init() - initialize the reset driver
344 *
345 * Boards may override this function to initialize the reset driver.
346 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100347efi_status_t __weak efi_reset_system_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200348{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100349 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200350}
351
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200352/**
353 * efi_get_time() - get current time
354 *
355 * This function implements the GetTime runtime service after
356 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
357 * anymore only an error code is returned.
358 *
359 * See the Unified Extensible Firmware Interface (UEFI) specification
360 * for details.
361 *
362 * @time: pointer to structure to receive current time
363 * @capabilities: pointer to structure to receive RTC properties
364 * Returns: status code
365 */
Alexander Graf393dd912016-10-14 13:45:30 +0200366efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200367 struct efi_time *time,
368 struct efi_time_cap *capabilities)
369{
Heinrich Schuchardtb4ba5be2019-06-13 18:42:40 +0200370 return EFI_UNSUPPORTED;
Alexander Graf77256092016-08-16 21:08:45 +0200371}
372
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200373/**
374 * efi_set_time() - set current time
375 *
376 * This function implements the SetTime runtime service after
377 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
378 * anymore only an error code is returned.
379 *
380 * See the Unified Extensible Firmware Interface (UEFI) specification
381 * for details.
382 *
383 * @time: pointer to structure to with current time
384 * Returns: status code
385 */
386efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
387{
388 return EFI_UNSUPPORTED;
389}
390
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000391/**
392 * efi_is_runtime_service_pointer() - check if pointer points to runtime table
393 *
394 * @p: pointer to check
395 * Return: true if the pointer points to a service function pointer in the
396 * runtime table
397 */
398static bool efi_is_runtime_service_pointer(void *p)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100399{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200400 return (p >= (void *)&efi_runtime_services.get_time &&
401 p <= (void *)&efi_runtime_services.query_variable_info) ||
402 p == (void *)&efi_events.prev ||
403 p == (void *)&efi_events.next;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100404}
405
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200406/**
407 * efi_runtime_detach() - detach unimplemented runtime functions
408 */
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +0200409void efi_runtime_detach(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100410{
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200411 efi_runtime_services.reset_system = efi_reset_system;
412 efi_runtime_services.get_time = efi_get_time;
413 efi_runtime_services.set_time = efi_set_time;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100414
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200415 /* Update CRC32 */
416 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000417}
418
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200419/**
420 * efi_set_virtual_address_map_runtime() - change from physical to virtual
421 * mapping
422 *
423 * This function implements the SetVirtualAddressMap() runtime service after
424 * it is first called.
425 *
426 * See the Unified Extensible Firmware Interface (UEFI) specification for
427 * details.
428 *
429 * @memory_map_size: size of the virtual map
430 * @descriptor_size: size of an entry in the map
431 * @descriptor_version: version of the map entries
432 * @virtmap: virtual address mapping information
433 * Return: status code EFI_UNSUPPORTED
434 */
Heinrich Schuchardt2b6bd382019-07-12 22:41:43 +0200435static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map_runtime(
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200436 efi_uintn_t memory_map_size,
437 efi_uintn_t descriptor_size,
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200438 uint32_t descriptor_version,
439 struct efi_mem_desc *virtmap)
440{
441 return EFI_UNSUPPORTED;
442}
443
444/**
445 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
446 *
447 * This function implements the ConvertPointer() runtime service after
448 * the first call to SetVirtualAddressMap().
449 *
450 * See the Unified Extensible Firmware Interface (UEFI) specification for
451 * details.
452 *
453 * @debug_disposition: indicates if pointer may be converted to NULL
454 * @address: pointer to be converted
455 * Return: status code EFI_UNSUPPORTED
456 */
457static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime(
458 efi_uintn_t debug_disposition, void **address)
459{
460 return EFI_UNSUPPORTED;
461}
462
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200463/**
464 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
465 *
466 * This function implements the ConvertPointer() runtime service until
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(
477 efi_uintn_t debug_disposition, void **address)
478{
479 efi_physical_addr_t addr = (uintptr_t)*address;
480 efi_uintn_t i;
481 efi_status_t ret = EFI_NOT_FOUND;
482
483 EFI_ENTRY("%zu %p", debug_disposition, address);
484
485 if (!efi_virtmap) {
486 ret = EFI_UNSUPPORTED;
487 goto out;
488 }
489
490 if (!address) {
491 ret = EFI_INVALID_PARAMETER;
492 goto out;
493 }
494
495 for (i = 0; i < efi_descriptor_count; i++) {
496 struct efi_mem_desc *map = (void *)efi_virtmap +
497 (efi_descriptor_size * i);
498
499 if (addr >= map->physical_start &&
500 (addr < map->physical_start
501 + (map->num_pages << EFI_PAGE_SHIFT))) {
502 *address = (void *)(uintptr_t)
503 (addr + map->virtual_start -
504 map->physical_start);
505
506 ret = EFI_SUCCESS;
507 break;
508 }
509 }
510
511out:
512 return EFI_EXIT(ret);
513}
514
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000515static __efi_runtime void efi_relocate_runtime_table(ulong offset)
516{
517 ulong patchoff;
518 void **pos;
519
520 /* Relocate the runtime services pointers */
521 patchoff = offset - gd->relocaddr;
522 for (pos = (void **)&efi_runtime_services.get_time;
523 pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200524 if (*pos)
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000525 *pos += patchoff;
526 }
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200527
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200528 /*
529 * The entry for SetVirtualAddress() must point to a physical address.
530 * After the first execution the service must return EFI_UNSUPPORTED.
531 */
532 efi_runtime_services.set_virtual_address_map =
533 &efi_set_virtual_address_map_runtime;
534
535 /*
536 * The entry for ConvertPointer() must point to a physical address.
537 * The service is not usable after SetVirtualAddress().
538 */
539 efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime;
540
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200541 /*
542 * TODO: Update UEFI variable RuntimeServicesSupported removing flags
543 * EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP and
544 * EFI_RT_SUPPORTED_CONVERT_POINTER as required by the UEFI spec 2.8.
545 */
546
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200547 /* Update CRC32 */
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200548 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100549}
550
551/* Relocate EFI runtime to uboot_reloc_base = offset */
552void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
553{
554#ifdef IS_RELA
555 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
556#else
557 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
558 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
559#endif
560
Alexander Graf62a67482016-06-02 11:38:27 +0200561 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100562 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
563 ulong base = CONFIG_SYS_TEXT_BASE;
564 ulong *p;
565 ulong newaddr;
566
567 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
568
Heinrich Schuchardt33691582019-08-14 06:49:09 +0200569 /*
570 * The runtime services table is updated in
571 * efi_relocate_runtime_table()
572 */
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000573 if (map && efi_is_runtime_service_pointer(p))
574 continue;
575
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700576 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
577 rel->info, *p, rel->offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100578
Rick Chen9677a372018-05-28 19:06:37 +0800579 switch (rel->info & R_MASK) {
580 case R_RELATIVE:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100581#ifdef IS_RELA
582 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
583#else
584 newaddr = *p - lastoff + offset;
585#endif
Rick Chen9677a372018-05-28 19:06:37 +0800586 break;
587#ifdef R_ABSOLUTE
588 case R_ABSOLUTE: {
589 ulong symidx = rel->info >> SYM_INDEX;
590 extern struct dyn_sym __dyn_sym_start[];
591 newaddr = __dyn_sym_start[symidx].addr + offset;
Alexander Grafdf0f6c72018-11-04 22:25:22 +0100592#ifdef IS_RELA
593 newaddr -= CONFIG_SYS_TEXT_BASE;
594#endif
Rick Chen9677a372018-05-28 19:06:37 +0800595 break;
596 }
597#endif
598 default:
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000599 printf("%s: Unknown relocation type %llx\n",
600 __func__, rel->info & R_MASK);
Rick Chen9677a372018-05-28 19:06:37 +0800601 continue;
602 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100603
604 /* Check if the relocation is inside bounds */
605 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200606 newaddr > (map->virtual_start +
607 (map->num_pages << EFI_PAGE_SHIFT)))) {
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000608 printf("%s: Relocation at %p is out of range (%lx)\n",
609 __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100610 continue;
611 }
612
Alexander Graf62a67482016-06-02 11:38:27 +0200613 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100614 *p = newaddr;
Alexander Grafe5702322016-04-11 23:20:39 +0200615 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
616 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf0bd425a2016-03-04 01:10:01 +0100617 }
618
619#ifndef IS_RELA
620 lastoff = offset;
621#endif
622
623 invalidate_icache_all();
624}
625
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200626/**
627 * efi_set_virtual_address_map() - change from physical to virtual mapping
628 *
629 * This function implements the SetVirtualAddressMap() runtime service.
630 *
631 * See the Unified Extensible Firmware Interface (UEFI) specification for
632 * details.
633 *
634 * @memory_map_size: size of the virtual map
635 * @descriptor_size: size of an entry in the map
636 * @descriptor_version: version of the map entries
637 * @virtmap: virtual address mapping information
638 * Return: status code
639 */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100640static efi_status_t EFIAPI efi_set_virtual_address_map(
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200641 efi_uintn_t memory_map_size,
642 efi_uintn_t descriptor_size,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100643 uint32_t descriptor_version,
644 struct efi_mem_desc *virtmap)
645{
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200646 efi_uintn_t n = memory_map_size / descriptor_size;
647 efi_uintn_t i;
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200648 efi_status_t ret = EFI_INVALID_PARAMETER;
Alexander Graf86e00212018-12-11 10:00:42 +0100649 int rt_code_sections = 0;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200650 struct efi_event *event;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100651
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200652 EFI_ENTRY("%zx %zx %x %p", memory_map_size, descriptor_size,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100653 descriptor_version, virtmap);
654
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200655 if (descriptor_version != EFI_MEMORY_DESCRIPTOR_VERSION ||
656 descriptor_size < sizeof(struct efi_mem_desc))
657 goto out;
658
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200659 efi_virtmap = virtmap;
660 efi_descriptor_size = descriptor_size;
661 efi_descriptor_count = n;
662
Alexander Graf86e00212018-12-11 10:00:42 +0100663 /*
664 * TODO:
665 * Further down we are cheating. While really we should implement
666 * SetVirtualAddressMap() events and ConvertPointer() to allow
667 * dynamically loaded drivers to expose runtime services, we don't
668 * today.
669 *
670 * So let's ensure we see exactly one single runtime section, as
671 * that is the built-in one. If we see more (or less), someone must
672 * have tried adding or removing to that which we don't support yet.
673 * In that case, let's better fail rather than expose broken runtime
674 * services.
675 */
676 for (i = 0; i < n; i++) {
677 struct efi_mem_desc *map = (void*)virtmap +
678 (descriptor_size * i);
679
680 if (map->type == EFI_RUNTIME_SERVICES_CODE)
681 rt_code_sections++;
682 }
683
684 if (rt_code_sections != 1) {
685 /*
686 * We expose exactly one single runtime code section, so
687 * something is definitely going wrong.
688 */
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200689 goto out;
Alexander Graf86e00212018-12-11 10:00:42 +0100690 }
691
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200692 /* Notify EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
693 list_for_each_entry(event, &efi_events, link) {
694 if (event->notify_function)
695 EFI_CALL_VOID(event->notify_function(
696 event, event->notify_context));
697 }
698
Alexander Graf77256092016-08-16 21:08:45 +0200699 /* Rebind mmio pointers */
700 for (i = 0; i < n; i++) {
701 struct efi_mem_desc *map = (void*)virtmap +
702 (descriptor_size * i);
703 struct list_head *lhandle;
704 efi_physical_addr_t map_start = map->physical_start;
705 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
706 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200707 u64 off = map->virtual_start - map_start;
Alexander Graf77256092016-08-16 21:08:45 +0200708
709 /* Adjust all mmio pointers in this region */
710 list_for_each(lhandle, &efi_runtime_mmio) {
711 struct efi_runtime_mmio_list *lmmio;
712
713 lmmio = list_entry(lhandle,
714 struct efi_runtime_mmio_list,
715 link);
716 if ((map_start <= lmmio->paddr) &&
717 (map_end >= lmmio->paddr)) {
Alexander Graf77256092016-08-16 21:08:45 +0200718 uintptr_t new_addr = lmmio->paddr + off;
719 *lmmio->ptr = (void *)new_addr;
720 }
721 }
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200722 if ((map_start <= (uintptr_t)systab.tables) &&
723 (map_end >= (uintptr_t)systab.tables)) {
724 char *ptr = (char *)systab.tables;
725
726 ptr += off;
727 systab.tables = (struct efi_configuration_table *)ptr;
728 }
Alexander Graf77256092016-08-16 21:08:45 +0200729 }
730
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000731 /* Relocate the runtime. See TODO above */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100732 for (i = 0; i < n; i++) {
733 struct efi_mem_desc *map;
734
735 map = (void*)virtmap + (descriptor_size * i);
736 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf77256092016-08-16 21:08:45 +0200737 ulong new_offset = map->virtual_start -
Alexander Graf86e00212018-12-11 10:00:42 +0100738 map->physical_start + gd->relocaddr;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100739
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000740 efi_relocate_runtime_table(new_offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100741 efi_runtime_relocate(new_offset, map);
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200742 ret = EFI_SUCCESS;
743 goto out;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100744 }
745 }
746
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200747out:
748 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100749}
750
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200751/**
752 * efi_add_runtime_mmio() - add memory-mapped IO region
753 *
754 * This function adds a memory-mapped IO region to the memory map to make it
755 * available at runtime.
756 *
Heinrich Schuchardt7e307642018-12-23 02:35:13 +0100757 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
758 * IO region
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200759 * @len: size of the memory-mapped IO region
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200760 * Returns: status code
761 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100762efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf77256092016-08-16 21:08:45 +0200763{
764 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.de93fcc7f2017-08-11 21:19:37 +0200765 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf33a83a32018-03-15 15:08:16 +0100766 uint64_t addr = *(uintptr_t *)mmio_ptr;
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100767 efi_status_t ret;
Alexander Graf33a83a32018-03-15 15:08:16 +0100768
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100769 ret = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
770 if (ret != EFI_SUCCESS)
Alexander Graf33a83a32018-03-15 15:08:16 +0100771 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200772
773 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100774 if (!newmmio)
775 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200776 newmmio->ptr = mmio_ptr;
777 newmmio->paddr = *(uintptr_t *)mmio_ptr;
778 newmmio->len = len;
779 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100780
Alexander Graf33a83a32018-03-15 15:08:16 +0100781 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200782}
783
Alexander Graf0bd425a2016-03-04 01:10:01 +0100784/*
785 * In the second stage, U-Boot has disappeared. To isolate our runtime code
786 * that at this point still exists from the rest, we put it into a special
787 * section.
788 *
789 * !!WARNING!!
790 *
791 * This means that we can not rely on any code outside of this file in any
792 * function or variable below this line.
793 *
794 * Please keep everything fully self-contained and annotated with
Alexander Graf393dd912016-10-14 13:45:30 +0200795 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf0bd425a2016-03-04 01:10:01 +0100796 */
797
798/*
799 * Relocate the EFI runtime stub to a different place. We need to call this
800 * the first time we expose the runtime interface to a user and on set virtual
801 * address map calls.
802 */
803
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200804/**
805 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
806 *
807 * This function is used after SetVirtualAddressMap() is called as replacement
808 * for services that are not available anymore due to constraints of the U-Boot
809 * implementation.
810 *
811 * Return: EFI_UNSUPPORTED
812 */
Alexander Graf393dd912016-10-14 13:45:30 +0200813static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100814{
815 return EFI_UNSUPPORTED;
816}
817
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200818/**
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200819 * efi_update_capsule() - process information from operating system
820 *
821 * This function implements the UpdateCapsule() runtime service.
822 *
823 * See the Unified Extensible Firmware Interface (UEFI) specification for
824 * details.
825 *
826 * @capsule_header_array: pointer to array of virtual pointers
827 * @capsule_count: number of pointers in capsule_header_array
828 * @scatter_gather_list: pointer to arry of physical pointers
829 * Returns: status code
830 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100831efi_status_t __efi_runtime EFIAPI efi_update_capsule(
832 struct efi_capsule_header **capsule_header_array,
833 efi_uintn_t capsule_count,
834 u64 scatter_gather_list)
835{
836 return EFI_UNSUPPORTED;
837}
838
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200839/**
840 * efi_query_capsule_caps() - check if capsule is supported
841 *
842 * This function implements the QueryCapsuleCapabilities() runtime service.
843 *
844 * See the Unified Extensible Firmware Interface (UEFI) specification for
845 * details.
846 *
847 * @capsule_header_array: pointer to array of virtual pointers
848 * @capsule_count: number of pointers in capsule_header_array
Heinrich Schuchardt1d1be362018-09-03 20:59:25 +0200849 * @maximum_capsule_size: maximum capsule size
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200850 * @reset_type: type of reset needed for capsule update
851 * Returns: status code
852 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100853efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
854 struct efi_capsule_header **capsule_header_array,
855 efi_uintn_t capsule_count,
AKASHI Takahirod840b672018-11-14 16:18:53 +0900856 u64 *maximum_capsule_size,
857 u32 *reset_type)
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100858{
859 return EFI_UNSUPPORTED;
860}
861
Alexander Graf393dd912016-10-14 13:45:30 +0200862struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100863 .hdr = {
864 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +0200865 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +0200866 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf0bd425a2016-03-04 01:10:01 +0100867 },
Alexander Graf77256092016-08-16 21:08:45 +0200868 .get_time = &efi_get_time_boottime,
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200869 .set_time = &efi_set_time_boottime,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100870 .get_wakeup_time = (void *)&efi_unimplemented,
871 .set_wakeup_time = (void *)&efi_unimplemented,
872 .set_virtual_address_map = &efi_set_virtual_address_map,
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200873 .convert_pointer = efi_convert_pointer,
Rob Clark15f3d742017-09-13 18:05:37 -0400874 .get_variable = efi_get_variable,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200875 .get_next_variable_name = efi_get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400876 .set_variable = efi_set_variable,
Heinrich Schuchardt5337a6c2019-06-20 15:40:49 +0200877 .get_next_high_mono_count = (void *)&efi_unimplemented,
Alexander Graf77256092016-08-16 21:08:45 +0200878 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100879 .update_capsule = efi_update_capsule,
880 .query_capsule_caps = efi_query_capsule_caps,
881 .query_variable_info = efi_query_variable_info,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100882};