blob: 4b3c843b2ced089fc355c88b228451587a072511 [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>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <malloc.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010015#include <rtc.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070016#include <u-boot/crc.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010017
18/* For manual relocation support */
19DECLARE_GLOBAL_DATA_PTR;
20
Alexander Graf77256092016-08-16 21:08:45 +020021struct efi_runtime_mmio_list {
22 struct list_head link;
23 void **ptr;
24 u64 paddr;
25 u64 len;
26};
27
28/* This list contains all runtime available mmio regions */
29LIST_HEAD(efi_runtime_mmio);
30
Alexander Graf393dd912016-10-14 13:45:30 +020031static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void);
Alexander Graf0bd425a2016-03-04 01:10:01 +010032
Simon Glassfff89d42018-06-11 23:26:40 -060033/*
Heinrich Schuchardt11121542018-09-03 19:29:41 +020034 * TODO(sjg@chromium.org): These defines and structures should come from the ELF
35 * header for each architecture (or a generic header) rather than being repeated
36 * here.
Simon Glassfff89d42018-06-11 23:26:40 -060037 */
Alexander Graf0fe51922018-06-18 17:23:08 +020038#if defined(__aarch64__)
Alexander Graf46c15142018-06-18 17:23:11 +020039#define R_RELATIVE R_AARCH64_RELATIVE
Alexander Graf0bd425a2016-03-04 01:10:01 +010040#define R_MASK 0xffffffffULL
41#define IS_RELA 1
Alexander Graf0fe51922018-06-18 17:23:08 +020042#elif defined(__arm__)
Alexander Graf46c15142018-06-18 17:23:11 +020043#define R_RELATIVE R_ARM_RELATIVE
Alexander Graf0bd425a2016-03-04 01:10:01 +010044#define R_MASK 0xffULL
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -070045#elif defined(__i386__)
Simon Glasscdfe6962016-09-25 15:27:35 -060046#define R_RELATIVE R_386_RELATIVE
47#define R_MASK 0xffULL
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -070048#elif defined(__x86_64__)
49#define R_RELATIVE R_X86_64_RELATIVE
50#define R_MASK 0xffffffffULL
51#define IS_RELA 1
Alexander Graf0fe51922018-06-18 17:23:08 +020052#elif defined(__riscv)
Rick Chen9677a372018-05-28 19:06:37 +080053#define R_RELATIVE R_RISCV_RELATIVE
54#define R_MASK 0xffULL
55#define IS_RELA 1
56
57struct dyn_sym {
58 ulong foo1;
59 ulong addr;
60 u32 foo2;
61 u32 foo3;
62};
Alexander Graf0fe51922018-06-18 17:23:08 +020063#if (__riscv_xlen == 32)
Rick Chen9677a372018-05-28 19:06:37 +080064#define R_ABSOLUTE R_RISCV_32
65#define SYM_INDEX 8
Alexander Graf0fe51922018-06-18 17:23:08 +020066#elif (__riscv_xlen == 64)
Rick Chen9677a372018-05-28 19:06:37 +080067#define R_ABSOLUTE R_RISCV_64
68#define SYM_INDEX 32
Alexander Graf0fe51922018-06-18 17:23:08 +020069#else
70#error unknown riscv target
Rick Chen9677a372018-05-28 19:06:37 +080071#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +010072#else
73#error Need to add relocation awareness
74#endif
75
76struct elf_rel {
77 ulong *offset;
78 ulong info;
79};
80
81struct elf_rela {
82 ulong *offset;
83 ulong info;
84 long addend;
85};
86
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +020087static __efi_runtime_data struct efi_mem_desc *efi_virtmap;
88static __efi_runtime_data efi_uintn_t efi_descriptor_count;
89static __efi_runtime_data efi_uintn_t efi_descriptor_size;
90
Alexander Graf0bd425a2016-03-04 01:10:01 +010091/*
Heinrich Schuchardt11121542018-09-03 19:29:41 +020092 * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI
Alexander Graf0bd425a2016-03-04 01:10:01 +010093 * payload are running concurrently at the same time. In this mode, we can
94 * handle a good number of runtime callbacks
95 */
96
AKASHI Takahiro32401842019-06-05 13:21:38 +090097efi_status_t efi_init_runtime_supported(void)
98{
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +020099 u16 efi_runtime_services_supported =
100 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP |
101 EFI_RT_SUPPORTED_CONVERT_POINTER;
AKASHI Takahiro32401842019-06-05 13:21:38 +0900102
103 /*
104 * This value must be synced with efi_runtime_detach_list
105 * as well as efi_runtime_services.
106 */
Heinrich Schuchardt05874fb2019-07-05 18:12:16 +0200107#ifdef CONFIG_EFI_HAVE_RUNTIME_RESET
AKASHI Takahiro32401842019-06-05 13:21:38 +0900108 efi_runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM;
109#endif
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200110
AKASHI Takahiro32401842019-06-05 13:21:38 +0900111 return EFI_CALL(efi_set_variable(L"RuntimeServicesSupported",
112 &efi_global_variable_guid,
113 EFI_VARIABLE_BOOTSERVICE_ACCESS |
114 EFI_VARIABLE_RUNTIME_ACCESS,
115 sizeof(efi_runtime_services_supported),
116 &efi_runtime_services_supported));
117}
118
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200119/**
120 * efi_update_table_header_crc32() - Update crc32 in table header
121 *
122 * @table: EFI table
123 */
124void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
125{
126 table->crc32 = 0;
127 table->crc32 = crc32(0, (const unsigned char *)table,
128 table->headersize);
129}
130
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200131/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200132 * efi_reset_system_boottime() - reset system at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200133 *
134 * This function implements the ResetSystem() runtime service before
135 * SetVirtualAddressMap() is called.
136 *
137 * See the Unified Extensible Firmware Interface (UEFI) specification for
138 * details.
139 *
140 * @reset_type: type of reset to perform
141 * @reset_status: status code for the reset
142 * @data_size: size of reset_data
143 * @reset_data: information about the reset
144 */
Alexander Graf77256092016-08-16 21:08:45 +0200145static void EFIAPI efi_reset_system_boottime(
146 enum efi_reset_type reset_type,
147 efi_status_t reset_status,
148 unsigned long data_size, void *reset_data)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100149{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100150 struct efi_event *evt;
151
Alexander Graf0bd425a2016-03-04 01:10:01 +0100152 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
153 reset_data);
154
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100155 /* Notify reset */
156 list_for_each_entry(evt, &efi_events, link) {
157 if (evt->group &&
158 !guidcmp(evt->group,
159 &efi_guid_event_group_reset_system)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200160 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100161 break;
162 }
163 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100164 switch (reset_type) {
165 case EFI_RESET_COLD:
166 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +0100167 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100168 do_reset(NULL, 0, 0, NULL);
169 break;
170 case EFI_RESET_SHUTDOWN:
Heinrich Schuchardt44489452018-10-16 07:44:53 +0200171#ifdef CONFIG_CMD_POWEROFF
172 do_poweroff(NULL, 0, 0, NULL);
173#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +0100174 break;
175 }
176
Alexander Graf77256092016-08-16 21:08:45 +0200177 while (1) { }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100178}
179
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200180/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200181 * efi_get_time_boottime() - get current time at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200182 *
183 * This function implements the GetTime runtime service before
184 * SetVirtualAddressMap() is called.
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200185 *
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200186 * See the Unified Extensible Firmware Interface (UEFI) specification
187 * for details.
188 *
189 * @time: pointer to structure to receive current time
190 * @capabilities: pointer to structure to receive RTC properties
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200191 * Returns: status code
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200192 */
Alexander Graf77256092016-08-16 21:08:45 +0200193static efi_status_t EFIAPI efi_get_time_boottime(
194 struct efi_time *time,
195 struct efi_time_cap *capabilities)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100196{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200197#ifdef CONFIG_EFI_GET_TIME
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200198 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200199 struct rtc_time tm;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100200 struct udevice *dev;
201
202 EFI_ENTRY("%p %p", time, capabilities);
203
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200204 if (!time) {
205 ret = EFI_INVALID_PARAMETER;
206 goto out;
207 }
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200208 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
209 dm_rtc_get(dev, &tm)) {
210 ret = EFI_UNSUPPORTED;
211 goto out;
212 }
213 if (dm_rtc_get(dev, &tm)) {
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200214 ret = EFI_DEVICE_ERROR;
215 goto out;
216 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100217
218 memset(time, 0, sizeof(*time));
219 time->year = tm.tm_year;
220 time->month = tm.tm_mon;
221 time->day = tm.tm_mday;
222 time->hour = tm.tm_hour;
223 time->minute = tm.tm_min;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200224 time->second = tm.tm_sec;
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200225 if (tm.tm_isdst)
226 time->daylight =
227 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200228 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100229
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200230 if (capabilities) {
231 /* Set reasonable dummy values */
232 capabilities->resolution = 1; /* 1 Hz */
233 capabilities->accuracy = 100000000; /* 100 ppm */
234 capabilities->sets_to_zero = false;
235 }
236out:
237 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100238#else
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200239 EFI_ENTRY("%p %p", time, capabilities);
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200240 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100241#endif
242}
243
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200244#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200245
246/**
247 * efi_validate_time() - checks if timestamp is valid
248 *
249 * @time: timestamp to validate
250 * Returns: 0 if timestamp is valid, 1 otherwise
251 */
252static int efi_validate_time(struct efi_time *time)
253{
254 return (!time ||
255 time->year < 1900 || time->year > 9999 ||
256 !time->month || time->month > 12 || !time->day ||
257 time->day > rtc_month_days(time->month - 1, time->year) ||
258 time->hour > 23 || time->minute > 59 || time->second > 59 ||
259 time->nanosecond > 999999999 ||
260 time->daylight &
261 ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
262 ((time->timezone < -1440 || time->timezone > 1440) &&
263 time->timezone != EFI_UNSPECIFIED_TIMEZONE));
264}
265
266#endif
267
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200268/**
269 * efi_set_time_boottime() - set current time
270 *
271 * This function implements the SetTime() runtime service before
272 * SetVirtualAddressMap() is called.
273 *
274 * See the Unified Extensible Firmware Interface (UEFI) specification
275 * for details.
276 *
277 * @time: pointer to structure to with current time
278 * Returns: status code
279 */
280static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
281{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200282#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200283 efi_status_t ret = EFI_SUCCESS;
284 struct rtc_time tm;
285 struct udevice *dev;
Alexander Graf77256092016-08-16 21:08:45 +0200286
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200287 EFI_ENTRY("%p", time);
288
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200289 if (efi_validate_time(time)) {
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200290 ret = EFI_INVALID_PARAMETER;
291 goto out;
292 }
293
294 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
295 ret = EFI_UNSUPPORTED;
296 goto out;
297 }
298
299 memset(&tm, 0, sizeof(tm));
300 tm.tm_year = time->year;
301 tm.tm_mon = time->month;
302 tm.tm_mday = time->day;
303 tm.tm_hour = time->hour;
304 tm.tm_min = time->minute;
305 tm.tm_sec = time->second;
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200306 tm.tm_isdst = time->daylight ==
307 (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200308 /* Calculate day of week */
309 rtc_calc_weekday(&tm);
310
311 if (dm_rtc_set(dev, &tm))
312 ret = EFI_DEVICE_ERROR;
313out:
314 return EFI_EXIT(ret);
315#else
316 EFI_ENTRY("%p", time);
317 return EFI_EXIT(EFI_UNSUPPORTED);
318#endif
319}
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200320/**
321 * efi_reset_system() - reset system
322 *
323 * This function implements the ResetSystem() runtime service after
324 * SetVirtualAddressMap() is called. It only executes an endless loop.
325 * Boards may override the helpers below to implement reset functionality.
326 *
327 * See the Unified Extensible Firmware Interface (UEFI) specification for
328 * details.
329 *
330 * @reset_type: type of reset to perform
331 * @reset_status: status code for the reset
332 * @data_size: size of reset_data
333 * @reset_data: information about the reset
334 */
Alexander Graf393dd912016-10-14 13:45:30 +0200335void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200336 enum efi_reset_type reset_type,
337 efi_status_t reset_status,
338 unsigned long data_size, void *reset_data)
339{
340 /* Nothing we can do */
341 while (1) { }
342}
343
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200344/**
345 * efi_reset_system_init() - initialize the reset driver
346 *
347 * Boards may override this function to initialize the reset driver.
348 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100349efi_status_t __weak efi_reset_system_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200350{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100351 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200352}
353
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200354/**
355 * efi_get_time() - get current time
356 *
357 * This function implements the GetTime runtime service after
358 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
359 * anymore only an error code is returned.
360 *
361 * See the Unified Extensible Firmware Interface (UEFI) specification
362 * for details.
363 *
364 * @time: pointer to structure to receive current time
365 * @capabilities: pointer to structure to receive RTC properties
366 * Returns: status code
367 */
Alexander Graf393dd912016-10-14 13:45:30 +0200368efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200369 struct efi_time *time,
370 struct efi_time_cap *capabilities)
371{
Heinrich Schuchardtb4ba5be2019-06-13 18:42:40 +0200372 return EFI_UNSUPPORTED;
Alexander Graf77256092016-08-16 21:08:45 +0200373}
374
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200375/**
376 * efi_set_time() - set current time
377 *
378 * This function implements the SetTime runtime service after
379 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
380 * anymore only an error code is returned.
381 *
382 * See the Unified Extensible Firmware Interface (UEFI) specification
383 * for details.
384 *
385 * @time: pointer to structure to with current time
386 * Returns: status code
387 */
388efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
389{
390 return EFI_UNSUPPORTED;
391}
392
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000393/**
394 * efi_is_runtime_service_pointer() - check if pointer points to runtime table
395 *
396 * @p: pointer to check
397 * Return: true if the pointer points to a service function pointer in the
398 * runtime table
399 */
400static bool efi_is_runtime_service_pointer(void *p)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100401{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200402 return (p >= (void *)&efi_runtime_services.get_time &&
403 p <= (void *)&efi_runtime_services.query_variable_info) ||
404 p == (void *)&efi_events.prev ||
405 p == (void *)&efi_events.next;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100406}
407
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200408/**
409 * efi_runtime_detach() - detach unimplemented runtime functions
410 */
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +0200411void efi_runtime_detach(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100412{
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200413 efi_runtime_services.reset_system = efi_reset_system;
414 efi_runtime_services.get_time = efi_get_time;
415 efi_runtime_services.set_time = efi_set_time;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100416
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200417 /* Update CRC32 */
418 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000419}
420
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200421/**
422 * efi_set_virtual_address_map_runtime() - change from physical to virtual
423 * mapping
424 *
425 * This function implements the SetVirtualAddressMap() runtime service after
426 * it is first called.
427 *
428 * See the Unified Extensible Firmware Interface (UEFI) specification for
429 * details.
430 *
431 * @memory_map_size: size of the virtual map
432 * @descriptor_size: size of an entry in the map
433 * @descriptor_version: version of the map entries
434 * @virtmap: virtual address mapping information
435 * Return: status code EFI_UNSUPPORTED
436 */
Heinrich Schuchardt2b6bd382019-07-12 22:41:43 +0200437static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map_runtime(
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200438 efi_uintn_t memory_map_size,
439 efi_uintn_t descriptor_size,
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200440 uint32_t descriptor_version,
441 struct efi_mem_desc *virtmap)
442{
443 return EFI_UNSUPPORTED;
444}
445
446/**
447 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
448 *
449 * This function implements the ConvertPointer() runtime service after
450 * the first call to SetVirtualAddressMap().
451 *
452 * See the Unified Extensible Firmware Interface (UEFI) specification for
453 * details.
454 *
455 * @debug_disposition: indicates if pointer may be converted to NULL
456 * @address: pointer to be converted
457 * Return: status code EFI_UNSUPPORTED
458 */
459static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime(
460 efi_uintn_t debug_disposition, void **address)
461{
462 return EFI_UNSUPPORTED;
463}
464
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200465/**
466 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
467 *
468 * This function implements the ConvertPointer() runtime service until
469 * the first call to SetVirtualAddressMap().
470 *
471 * See the Unified Extensible Firmware Interface (UEFI) specification for
472 * details.
473 *
474 * @debug_disposition: indicates if pointer may be converted to NULL
475 * @address: pointer to be converted
476 * Return: status code EFI_UNSUPPORTED
477 */
478static __efi_runtime efi_status_t EFIAPI efi_convert_pointer(
479 efi_uintn_t debug_disposition, void **address)
480{
481 efi_physical_addr_t addr = (uintptr_t)*address;
482 efi_uintn_t i;
483 efi_status_t ret = EFI_NOT_FOUND;
484
485 EFI_ENTRY("%zu %p", debug_disposition, address);
486
487 if (!efi_virtmap) {
488 ret = EFI_UNSUPPORTED;
489 goto out;
490 }
491
492 if (!address) {
493 ret = EFI_INVALID_PARAMETER;
494 goto out;
495 }
496
497 for (i = 0; i < efi_descriptor_count; i++) {
498 struct efi_mem_desc *map = (void *)efi_virtmap +
499 (efi_descriptor_size * i);
500
501 if (addr >= map->physical_start &&
502 (addr < map->physical_start
503 + (map->num_pages << EFI_PAGE_SHIFT))) {
504 *address = (void *)(uintptr_t)
505 (addr + map->virtual_start -
506 map->physical_start);
507
508 ret = EFI_SUCCESS;
509 break;
510 }
511 }
512
513out:
514 return EFI_EXIT(ret);
515}
516
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000517static __efi_runtime void efi_relocate_runtime_table(ulong offset)
518{
519 ulong patchoff;
520 void **pos;
521
522 /* Relocate the runtime services pointers */
523 patchoff = offset - gd->relocaddr;
524 for (pos = (void **)&efi_runtime_services.get_time;
525 pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200526 if (*pos)
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000527 *pos += patchoff;
528 }
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200529
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200530 /*
531 * The entry for SetVirtualAddress() must point to a physical address.
532 * After the first execution the service must return EFI_UNSUPPORTED.
533 */
534 efi_runtime_services.set_virtual_address_map =
535 &efi_set_virtual_address_map_runtime;
536
537 /*
538 * The entry for ConvertPointer() must point to a physical address.
539 * The service is not usable after SetVirtualAddress().
540 */
541 efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime;
542
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200543 /*
544 * TODO: Update UEFI variable RuntimeServicesSupported removing flags
545 * EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP and
546 * EFI_RT_SUPPORTED_CONVERT_POINTER as required by the UEFI spec 2.8.
547 */
548
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200549 /* Update CRC32 */
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200550 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100551}
552
553/* Relocate EFI runtime to uboot_reloc_base = offset */
554void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
555{
556#ifdef IS_RELA
557 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
558#else
559 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
560 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
561#endif
562
Alexander Graf62a67482016-06-02 11:38:27 +0200563 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100564 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
565 ulong base = CONFIG_SYS_TEXT_BASE;
566 ulong *p;
567 ulong newaddr;
568
569 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
570
Heinrich Schuchardt33691582019-08-14 06:49:09 +0200571 /*
572 * The runtime services table is updated in
573 * efi_relocate_runtime_table()
574 */
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000575 if (map && efi_is_runtime_service_pointer(p))
576 continue;
577
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700578 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
579 rel->info, *p, rel->offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100580
Rick Chen9677a372018-05-28 19:06:37 +0800581 switch (rel->info & R_MASK) {
582 case R_RELATIVE:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100583#ifdef IS_RELA
584 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
585#else
586 newaddr = *p - lastoff + offset;
587#endif
Rick Chen9677a372018-05-28 19:06:37 +0800588 break;
589#ifdef R_ABSOLUTE
590 case R_ABSOLUTE: {
591 ulong symidx = rel->info >> SYM_INDEX;
592 extern struct dyn_sym __dyn_sym_start[];
593 newaddr = __dyn_sym_start[symidx].addr + offset;
Alexander Grafdf0f6c72018-11-04 22:25:22 +0100594#ifdef IS_RELA
595 newaddr -= CONFIG_SYS_TEXT_BASE;
596#endif
Rick Chen9677a372018-05-28 19:06:37 +0800597 break;
598 }
599#endif
600 default:
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000601 printf("%s: Unknown relocation type %llx\n",
602 __func__, rel->info & R_MASK);
Rick Chen9677a372018-05-28 19:06:37 +0800603 continue;
604 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100605
606 /* Check if the relocation is inside bounds */
607 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200608 newaddr > (map->virtual_start +
609 (map->num_pages << EFI_PAGE_SHIFT)))) {
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000610 printf("%s: Relocation at %p is out of range (%lx)\n",
611 __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100612 continue;
613 }
614
Alexander Graf62a67482016-06-02 11:38:27 +0200615 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100616 *p = newaddr;
Alexander Grafe5702322016-04-11 23:20:39 +0200617 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
618 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf0bd425a2016-03-04 01:10:01 +0100619 }
620
621#ifndef IS_RELA
622 lastoff = offset;
623#endif
624
625 invalidate_icache_all();
626}
627
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200628/**
629 * efi_set_virtual_address_map() - change from physical to virtual mapping
630 *
631 * This function implements the SetVirtualAddressMap() runtime service.
632 *
633 * See the Unified Extensible Firmware Interface (UEFI) specification for
634 * details.
635 *
636 * @memory_map_size: size of the virtual map
637 * @descriptor_size: size of an entry in the map
638 * @descriptor_version: version of the map entries
639 * @virtmap: virtual address mapping information
640 * Return: status code
641 */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100642static efi_status_t EFIAPI efi_set_virtual_address_map(
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200643 efi_uintn_t memory_map_size,
644 efi_uintn_t descriptor_size,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100645 uint32_t descriptor_version,
646 struct efi_mem_desc *virtmap)
647{
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200648 efi_uintn_t n = memory_map_size / descriptor_size;
649 efi_uintn_t i;
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200650 efi_status_t ret = EFI_INVALID_PARAMETER;
Alexander Graf86e00212018-12-11 10:00:42 +0100651 int rt_code_sections = 0;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200652 struct efi_event *event;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100653
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200654 EFI_ENTRY("%zx %zx %x %p", memory_map_size, descriptor_size,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100655 descriptor_version, virtmap);
656
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200657 if (descriptor_version != EFI_MEMORY_DESCRIPTOR_VERSION ||
658 descriptor_size < sizeof(struct efi_mem_desc))
659 goto out;
660
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200661 efi_virtmap = virtmap;
662 efi_descriptor_size = descriptor_size;
663 efi_descriptor_count = n;
664
Alexander Graf86e00212018-12-11 10:00:42 +0100665 /*
666 * TODO:
667 * Further down we are cheating. While really we should implement
668 * SetVirtualAddressMap() events and ConvertPointer() to allow
669 * dynamically loaded drivers to expose runtime services, we don't
670 * today.
671 *
672 * So let's ensure we see exactly one single runtime section, as
673 * that is the built-in one. If we see more (or less), someone must
674 * have tried adding or removing to that which we don't support yet.
675 * In that case, let's better fail rather than expose broken runtime
676 * services.
677 */
678 for (i = 0; i < n; i++) {
679 struct efi_mem_desc *map = (void*)virtmap +
680 (descriptor_size * i);
681
682 if (map->type == EFI_RUNTIME_SERVICES_CODE)
683 rt_code_sections++;
684 }
685
686 if (rt_code_sections != 1) {
687 /*
688 * We expose exactly one single runtime code section, so
689 * something is definitely going wrong.
690 */
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200691 goto out;
Alexander Graf86e00212018-12-11 10:00:42 +0100692 }
693
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200694 /* Notify EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
695 list_for_each_entry(event, &efi_events, link) {
696 if (event->notify_function)
697 EFI_CALL_VOID(event->notify_function(
698 event, event->notify_context));
699 }
700
Alexander Graf77256092016-08-16 21:08:45 +0200701 /* Rebind mmio pointers */
702 for (i = 0; i < n; i++) {
703 struct efi_mem_desc *map = (void*)virtmap +
704 (descriptor_size * i);
705 struct list_head *lhandle;
706 efi_physical_addr_t map_start = map->physical_start;
707 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
708 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200709 u64 off = map->virtual_start - map_start;
Alexander Graf77256092016-08-16 21:08:45 +0200710
711 /* Adjust all mmio pointers in this region */
712 list_for_each(lhandle, &efi_runtime_mmio) {
713 struct efi_runtime_mmio_list *lmmio;
714
715 lmmio = list_entry(lhandle,
716 struct efi_runtime_mmio_list,
717 link);
718 if ((map_start <= lmmio->paddr) &&
719 (map_end >= lmmio->paddr)) {
Alexander Graf77256092016-08-16 21:08:45 +0200720 uintptr_t new_addr = lmmio->paddr + off;
721 *lmmio->ptr = (void *)new_addr;
722 }
723 }
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200724 if ((map_start <= (uintptr_t)systab.tables) &&
725 (map_end >= (uintptr_t)systab.tables)) {
726 char *ptr = (char *)systab.tables;
727
728 ptr += off;
729 systab.tables = (struct efi_configuration_table *)ptr;
730 }
Alexander Graf77256092016-08-16 21:08:45 +0200731 }
732
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000733 /* Relocate the runtime. See TODO above */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100734 for (i = 0; i < n; i++) {
735 struct efi_mem_desc *map;
736
737 map = (void*)virtmap + (descriptor_size * i);
738 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf77256092016-08-16 21:08:45 +0200739 ulong new_offset = map->virtual_start -
Alexander Graf86e00212018-12-11 10:00:42 +0100740 map->physical_start + gd->relocaddr;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100741
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000742 efi_relocate_runtime_table(new_offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100743 efi_runtime_relocate(new_offset, map);
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200744 ret = EFI_SUCCESS;
745 goto out;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100746 }
747 }
748
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200749out:
750 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100751}
752
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200753/**
754 * efi_add_runtime_mmio() - add memory-mapped IO region
755 *
756 * This function adds a memory-mapped IO region to the memory map to make it
757 * available at runtime.
758 *
Heinrich Schuchardt7e307642018-12-23 02:35:13 +0100759 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
760 * IO region
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200761 * @len: size of the memory-mapped IO region
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200762 * Returns: status code
763 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100764efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf77256092016-08-16 21:08:45 +0200765{
766 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.de93fcc7f2017-08-11 21:19:37 +0200767 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf33a83a32018-03-15 15:08:16 +0100768 uint64_t addr = *(uintptr_t *)mmio_ptr;
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100769 efi_status_t ret;
Alexander Graf33a83a32018-03-15 15:08:16 +0100770
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100771 ret = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
772 if (ret != EFI_SUCCESS)
Alexander Graf33a83a32018-03-15 15:08:16 +0100773 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200774
775 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100776 if (!newmmio)
777 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200778 newmmio->ptr = mmio_ptr;
779 newmmio->paddr = *(uintptr_t *)mmio_ptr;
780 newmmio->len = len;
781 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100782
Alexander Graf33a83a32018-03-15 15:08:16 +0100783 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200784}
785
Alexander Graf0bd425a2016-03-04 01:10:01 +0100786/*
787 * In the second stage, U-Boot has disappeared. To isolate our runtime code
788 * that at this point still exists from the rest, we put it into a special
789 * section.
790 *
791 * !!WARNING!!
792 *
793 * This means that we can not rely on any code outside of this file in any
794 * function or variable below this line.
795 *
796 * Please keep everything fully self-contained and annotated with
Alexander Graf393dd912016-10-14 13:45:30 +0200797 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf0bd425a2016-03-04 01:10:01 +0100798 */
799
800/*
801 * Relocate the EFI runtime stub to a different place. We need to call this
802 * the first time we expose the runtime interface to a user and on set virtual
803 * address map calls.
804 */
805
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200806/**
807 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
808 *
809 * This function is used after SetVirtualAddressMap() is called as replacement
810 * for services that are not available anymore due to constraints of the U-Boot
811 * implementation.
812 *
813 * Return: EFI_UNSUPPORTED
814 */
Alexander Graf393dd912016-10-14 13:45:30 +0200815static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100816{
817 return EFI_UNSUPPORTED;
818}
819
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200820/**
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200821 * efi_update_capsule() - process information from operating system
822 *
823 * This function implements the UpdateCapsule() runtime service.
824 *
825 * See the Unified Extensible Firmware Interface (UEFI) specification for
826 * details.
827 *
828 * @capsule_header_array: pointer to array of virtual pointers
829 * @capsule_count: number of pointers in capsule_header_array
830 * @scatter_gather_list: pointer to arry of physical pointers
831 * Returns: status code
832 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100833efi_status_t __efi_runtime EFIAPI efi_update_capsule(
834 struct efi_capsule_header **capsule_header_array,
835 efi_uintn_t capsule_count,
836 u64 scatter_gather_list)
837{
838 return EFI_UNSUPPORTED;
839}
840
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200841/**
842 * efi_query_capsule_caps() - check if capsule is supported
843 *
844 * This function implements the QueryCapsuleCapabilities() runtime service.
845 *
846 * See the Unified Extensible Firmware Interface (UEFI) specification for
847 * details.
848 *
849 * @capsule_header_array: pointer to array of virtual pointers
850 * @capsule_count: number of pointers in capsule_header_array
Heinrich Schuchardt1d1be362018-09-03 20:59:25 +0200851 * @maximum_capsule_size: maximum capsule size
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200852 * @reset_type: type of reset needed for capsule update
853 * Returns: status code
854 */
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100855efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
856 struct efi_capsule_header **capsule_header_array,
857 efi_uintn_t capsule_count,
AKASHI Takahirod840b672018-11-14 16:18:53 +0900858 u64 *maximum_capsule_size,
859 u32 *reset_type)
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100860{
861 return EFI_UNSUPPORTED;
862}
863
Alexander Graf393dd912016-10-14 13:45:30 +0200864struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100865 .hdr = {
866 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +0200867 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +0200868 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf0bd425a2016-03-04 01:10:01 +0100869 },
Alexander Graf77256092016-08-16 21:08:45 +0200870 .get_time = &efi_get_time_boottime,
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200871 .set_time = &efi_set_time_boottime,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100872 .get_wakeup_time = (void *)&efi_unimplemented,
873 .set_wakeup_time = (void *)&efi_unimplemented,
874 .set_virtual_address_map = &efi_set_virtual_address_map,
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200875 .convert_pointer = efi_convert_pointer,
Rob Clark15f3d742017-09-13 18:05:37 -0400876 .get_variable = efi_get_variable,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200877 .get_next_variable_name = efi_get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400878 .set_variable = efi_set_variable,
Heinrich Schuchardt5337a6c2019-06-20 15:40:49 +0200879 .get_next_high_mono_count = (void *)&efi_unimplemented,
Alexander Graf77256092016-08-16 21:08:45 +0200880 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100881 .update_capsule = efi_update_capsule,
882 .query_capsule_caps = efi_query_capsule_caps,
883 .query_variable_info = efi_query_variable_info,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100884};