blob: 54e5a98bfc6cc2518351ebe595e3cf1a42e7c47d [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Graf50149ea2016-03-04 01:10:01 +01002/*
3 * EFI application runtime services
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Graf50149ea2016-03-04 01:10:01 +01006 */
7
8#include <common.h>
9#include <command.h>
10#include <dm.h>
Alexander Grafb34662d2018-06-18 17:23:11 +020011#include <elf.h>
Alexander Graf50149ea2016-03-04 01:10:01 +010012#include <efi_loader.h>
13#include <rtc.h>
Alexander Graf50149ea2016-03-04 01:10:01 +010014
15/* For manual relocation support */
16DECLARE_GLOBAL_DATA_PTR;
17
Alexander Graf80a48002016-08-16 21:08:45 +020018struct efi_runtime_mmio_list {
19 struct list_head link;
20 void **ptr;
21 u64 paddr;
22 u64 len;
23};
24
25/* This list contains all runtime available mmio regions */
26LIST_HEAD(efi_runtime_mmio);
27
Alexander Graf3c63db92016-10-14 13:45:30 +020028static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void);
Alexander Graf50149ea2016-03-04 01:10:01 +010029
Simon Glass2d2b5b22018-06-11 23:26:40 -060030/*
Heinrich Schuchardt250b3252018-09-03 19:29:41 +020031 * TODO(sjg@chromium.org): These defines and structures should come from the ELF
32 * header for each architecture (or a generic header) rather than being repeated
33 * here.
Simon Glass2d2b5b22018-06-11 23:26:40 -060034 */
Alexander Grafbc028912018-06-18 17:23:08 +020035#if defined(__aarch64__)
Alexander Grafb34662d2018-06-18 17:23:11 +020036#define R_RELATIVE R_AARCH64_RELATIVE
Alexander Graf50149ea2016-03-04 01:10:01 +010037#define R_MASK 0xffffffffULL
38#define IS_RELA 1
Alexander Grafbc028912018-06-18 17:23:08 +020039#elif defined(__arm__)
Alexander Grafb34662d2018-06-18 17:23:11 +020040#define R_RELATIVE R_ARM_RELATIVE
Alexander Graf50149ea2016-03-04 01:10:01 +010041#define R_MASK 0xffULL
Heinrich Schuchardt3ce78292018-10-13 20:52:08 -070042#elif defined(__i386__)
Simon Glass65e4c0b2016-09-25 15:27:35 -060043#define R_RELATIVE R_386_RELATIVE
44#define R_MASK 0xffULL
Heinrich Schuchardt3ce78292018-10-13 20:52:08 -070045#elif defined(__x86_64__)
46#define R_RELATIVE R_X86_64_RELATIVE
47#define R_MASK 0xffffffffULL
48#define IS_RELA 1
Alexander Grafbc028912018-06-18 17:23:08 +020049#elif defined(__riscv)
Rick Chen6836adb2018-05-28 19:06:37 +080050#define R_RELATIVE R_RISCV_RELATIVE
51#define R_MASK 0xffULL
52#define IS_RELA 1
53
54struct dyn_sym {
55 ulong foo1;
56 ulong addr;
57 u32 foo2;
58 u32 foo3;
59};
Alexander Grafbc028912018-06-18 17:23:08 +020060#if (__riscv_xlen == 32)
Rick Chen6836adb2018-05-28 19:06:37 +080061#define R_ABSOLUTE R_RISCV_32
62#define SYM_INDEX 8
Alexander Grafbc028912018-06-18 17:23:08 +020063#elif (__riscv_xlen == 64)
Rick Chen6836adb2018-05-28 19:06:37 +080064#define R_ABSOLUTE R_RISCV_64
65#define SYM_INDEX 32
Alexander Grafbc028912018-06-18 17:23:08 +020066#else
67#error unknown riscv target
Rick Chen6836adb2018-05-28 19:06:37 +080068#endif
Alexander Graf50149ea2016-03-04 01:10:01 +010069#else
70#error Need to add relocation awareness
71#endif
72
73struct elf_rel {
74 ulong *offset;
75 ulong info;
76};
77
78struct elf_rela {
79 ulong *offset;
80 ulong info;
81 long addend;
82};
83
Heinrich Schuchardt7be56e82019-07-27 20:35:24 +020084static __efi_runtime_data struct efi_mem_desc *efi_virtmap;
85static __efi_runtime_data efi_uintn_t efi_descriptor_count;
86static __efi_runtime_data efi_uintn_t efi_descriptor_size;
87
Alexander Graf50149ea2016-03-04 01:10:01 +010088/*
Heinrich Schuchardt250b3252018-09-03 19:29:41 +020089 * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI
Alexander Graf50149ea2016-03-04 01:10:01 +010090 * payload are running concurrently at the same time. In this mode, we can
91 * handle a good number of runtime callbacks
92 */
93
AKASHI Takahiroe771b4b2019-06-05 13:21:38 +090094efi_status_t efi_init_runtime_supported(void)
95{
Heinrich Schuchardt7be56e82019-07-27 20:35:24 +020096 u16 efi_runtime_services_supported =
97 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP |
98 EFI_RT_SUPPORTED_CONVERT_POINTER;
AKASHI Takahiroe771b4b2019-06-05 13:21:38 +090099
100 /*
101 * This value must be synced with efi_runtime_detach_list
102 * as well as efi_runtime_services.
103 */
Heinrich Schuchardt953661a2019-07-05 18:12:16 +0200104#ifdef CONFIG_EFI_HAVE_RUNTIME_RESET
AKASHI Takahiroe771b4b2019-06-05 13:21:38 +0900105 efi_runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM;
106#endif
Heinrich Schuchardt7be56e82019-07-27 20:35:24 +0200107
AKASHI Takahiroe771b4b2019-06-05 13:21:38 +0900108 return EFI_CALL(efi_set_variable(L"RuntimeServicesSupported",
109 &efi_global_variable_guid,
110 EFI_VARIABLE_BOOTSERVICE_ACCESS |
111 EFI_VARIABLE_RUNTIME_ACCESS,
112 sizeof(efi_runtime_services_supported),
113 &efi_runtime_services_supported));
114}
115
Heinrich Schuchardta39f39c2018-07-29 09:49:04 +0200116/**
117 * efi_update_table_header_crc32() - Update crc32 in table header
118 *
119 * @table: EFI table
120 */
121void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
122{
123 table->crc32 = 0;
124 table->crc32 = crc32(0, (const unsigned char *)table,
125 table->headersize);
126}
127
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200128/**
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200129 * efi_reset_system_boottime() - reset system at boot time
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200130 *
131 * This function implements the ResetSystem() runtime service before
132 * SetVirtualAddressMap() is called.
133 *
134 * See the Unified Extensible Firmware Interface (UEFI) specification for
135 * details.
136 *
137 * @reset_type: type of reset to perform
138 * @reset_status: status code for the reset
139 * @data_size: size of reset_data
140 * @reset_data: information about the reset
141 */
Alexander Graf80a48002016-08-16 21:08:45 +0200142static void EFIAPI efi_reset_system_boottime(
143 enum efi_reset_type reset_type,
144 efi_status_t reset_status,
145 unsigned long data_size, void *reset_data)
Alexander Graf50149ea2016-03-04 01:10:01 +0100146{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100147 struct efi_event *evt;
148
Alexander Graf50149ea2016-03-04 01:10:01 +0100149 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
150 reset_data);
151
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100152 /* Notify reset */
153 list_for_each_entry(evt, &efi_events, link) {
154 if (evt->group &&
155 !guidcmp(evt->group,
156 &efi_guid_event_group_reset_system)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200157 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100158 break;
159 }
160 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100161 switch (reset_type) {
162 case EFI_RESET_COLD:
163 case EFI_RESET_WARM:
Heinrich Schuchardt482fc902018-02-06 22:00:22 +0100164 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf50149ea2016-03-04 01:10:01 +0100165 do_reset(NULL, 0, 0, NULL);
166 break;
167 case EFI_RESET_SHUTDOWN:
Heinrich Schuchardte706ed72018-10-16 07:44:53 +0200168#ifdef CONFIG_CMD_POWEROFF
169 do_poweroff(NULL, 0, 0, NULL);
170#endif
Alexander Graf50149ea2016-03-04 01:10:01 +0100171 break;
172 }
173
Alexander Graf80a48002016-08-16 21:08:45 +0200174 while (1) { }
Alexander Graf50149ea2016-03-04 01:10:01 +0100175}
176
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200177/**
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200178 * efi_get_time_boottime() - get current time at boot time
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200179 *
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200180 * This function implements the GetTime runtime service before
181 * SetVirtualAddressMap() is called.
182 *
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200183 * See the Unified Extensible Firmware Interface (UEFI) specification
184 * for details.
185 *
186 * @time: pointer to structure to receive current time
187 * @capabilities: pointer to structure to receive RTC properties
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200188 * Returns: status code
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200189 */
Alexander Graf80a48002016-08-16 21:08:45 +0200190static efi_status_t EFIAPI efi_get_time_boottime(
191 struct efi_time *time,
192 struct efi_time_cap *capabilities)
Alexander Graf50149ea2016-03-04 01:10:01 +0100193{
Heinrich Schuchardt5ec48e32019-05-31 22:56:02 +0200194#ifdef CONFIG_EFI_GET_TIME
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200195 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200196 struct rtc_time tm;
Alexander Graf50149ea2016-03-04 01:10:01 +0100197 struct udevice *dev;
198
199 EFI_ENTRY("%p %p", time, capabilities);
200
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200201 if (!time) {
202 ret = EFI_INVALID_PARAMETER;
203 goto out;
204 }
Heinrich Schuchardtbb2b13d2019-05-19 21:41:28 +0200205 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
206 dm_rtc_get(dev, &tm)) {
207 ret = EFI_UNSUPPORTED;
208 goto out;
209 }
210 if (dm_rtc_get(dev, &tm)) {
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200211 ret = EFI_DEVICE_ERROR;
212 goto out;
213 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100214
215 memset(time, 0, sizeof(*time));
216 time->year = tm.tm_year;
217 time->month = tm.tm_mon;
218 time->day = tm.tm_mday;
219 time->hour = tm.tm_hour;
220 time->minute = tm.tm_min;
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200221 time->second = tm.tm_sec;
Heinrich Schuchardt38b9a792019-05-31 22:08:45 +0200222 if (tm.tm_isdst)
223 time->daylight =
224 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200225 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
Alexander Graf50149ea2016-03-04 01:10:01 +0100226
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200227 if (capabilities) {
228 /* Set reasonable dummy values */
229 capabilities->resolution = 1; /* 1 Hz */
230 capabilities->accuracy = 100000000; /* 100 ppm */
231 capabilities->sets_to_zero = false;
232 }
233out:
234 return EFI_EXIT(ret);
Alexander Graf50149ea2016-03-04 01:10:01 +0100235#else
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200236 EFI_ENTRY("%p %p", time, capabilities);
Heinrich Schuchardtbb2b13d2019-05-19 21:41:28 +0200237 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf50149ea2016-03-04 01:10:01 +0100238#endif
239}
240
Heinrich Schuchardt5ec48e32019-05-31 22:56:02 +0200241#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardte6bcc352019-05-31 07:35:19 +0200242
243/**
244 * efi_validate_time() - checks if timestamp is valid
245 *
246 * @time: timestamp to validate
247 * Returns: 0 if timestamp is valid, 1 otherwise
248 */
249static int efi_validate_time(struct efi_time *time)
250{
251 return (!time ||
252 time->year < 1900 || time->year > 9999 ||
253 !time->month || time->month > 12 || !time->day ||
254 time->day > rtc_month_days(time->month - 1, time->year) ||
255 time->hour > 23 || time->minute > 59 || time->second > 59 ||
256 time->nanosecond > 999999999 ||
257 time->daylight &
258 ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
259 ((time->timezone < -1440 || time->timezone > 1440) &&
260 time->timezone != EFI_UNSPECIFIED_TIMEZONE));
261}
262
263#endif
264
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200265/**
266 * efi_set_time_boottime() - set current time
267 *
268 * This function implements the SetTime() runtime service before
269 * SetVirtualAddressMap() is called.
270 *
271 * See the Unified Extensible Firmware Interface (UEFI) specification
272 * for details.
273 *
274 * @time: pointer to structure to with current time
275 * Returns: status code
276 */
277static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
278{
Heinrich Schuchardt5ec48e32019-05-31 22:56:02 +0200279#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200280 efi_status_t ret = EFI_SUCCESS;
281 struct rtc_time tm;
282 struct udevice *dev;
Alexander Graf80a48002016-08-16 21:08:45 +0200283
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200284 EFI_ENTRY("%p", time);
285
Heinrich Schuchardte6bcc352019-05-31 07:35:19 +0200286 if (efi_validate_time(time)) {
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200287 ret = EFI_INVALID_PARAMETER;
288 goto out;
289 }
290
291 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
292 ret = EFI_UNSUPPORTED;
293 goto out;
294 }
295
296 memset(&tm, 0, sizeof(tm));
297 tm.tm_year = time->year;
298 tm.tm_mon = time->month;
299 tm.tm_mday = time->day;
300 tm.tm_hour = time->hour;
301 tm.tm_min = time->minute;
302 tm.tm_sec = time->second;
Heinrich Schuchardt38b9a792019-05-31 22:08:45 +0200303 tm.tm_isdst = time->daylight ==
304 (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200305 /* Calculate day of week */
306 rtc_calc_weekday(&tm);
307
308 if (dm_rtc_set(dev, &tm))
309 ret = EFI_DEVICE_ERROR;
310out:
311 return EFI_EXIT(ret);
312#else
313 EFI_ENTRY("%p", time);
314 return EFI_EXIT(EFI_UNSUPPORTED);
315#endif
316}
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200317/**
318 * efi_reset_system() - reset system
319 *
320 * This function implements the ResetSystem() runtime service after
321 * SetVirtualAddressMap() is called. It only executes an endless loop.
322 * Boards may override the helpers below to implement reset functionality.
323 *
324 * See the Unified Extensible Firmware Interface (UEFI) specification for
325 * details.
326 *
327 * @reset_type: type of reset to perform
328 * @reset_status: status code for the reset
329 * @data_size: size of reset_data
330 * @reset_data: information about the reset
331 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200332void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf80a48002016-08-16 21:08:45 +0200333 enum efi_reset_type reset_type,
334 efi_status_t reset_status,
335 unsigned long data_size, void *reset_data)
336{
337 /* Nothing we can do */
338 while (1) { }
339}
340
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200341/**
342 * efi_reset_system_init() - initialize the reset driver
343 *
344 * Boards may override this function to initialize the reset driver.
345 */
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100346efi_status_t __weak efi_reset_system_init(void)
Alexander Graf80a48002016-08-16 21:08:45 +0200347{
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100348 return EFI_SUCCESS;
Alexander Graf80a48002016-08-16 21:08:45 +0200349}
350
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200351/**
352 * efi_get_time() - get current time
353 *
354 * This function implements the GetTime runtime service after
355 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
356 * anymore only an error code is returned.
357 *
358 * See the Unified Extensible Firmware Interface (UEFI) specification
359 * for details.
360 *
361 * @time: pointer to structure to receive current time
362 * @capabilities: pointer to structure to receive RTC properties
363 * Returns: status code
364 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200365efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf80a48002016-08-16 21:08:45 +0200366 struct efi_time *time,
367 struct efi_time_cap *capabilities)
368{
Heinrich Schuchardtc5b63be2019-06-13 18:42:40 +0200369 return EFI_UNSUPPORTED;
Alexander Graf80a48002016-08-16 21:08:45 +0200370}
371
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200372/**
373 * efi_set_time() - set current time
374 *
375 * This function implements the SetTime runtime service after
376 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
377 * anymore only an error code is returned.
378 *
379 * See the Unified Extensible Firmware Interface (UEFI) specification
380 * for details.
381 *
382 * @time: pointer to structure to with current time
383 * Returns: status code
384 */
385efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
386{
387 return EFI_UNSUPPORTED;
388}
389
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000390/**
391 * efi_is_runtime_service_pointer() - check if pointer points to runtime table
392 *
393 * @p: pointer to check
394 * Return: true if the pointer points to a service function pointer in the
395 * runtime table
396 */
397static bool efi_is_runtime_service_pointer(void *p)
Alexander Graf50149ea2016-03-04 01:10:01 +0100398{
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200399 return (p >= (void *)&efi_runtime_services.get_time &&
400 p <= (void *)&efi_runtime_services.query_variable_info) ||
401 p == (void *)&efi_events.prev ||
402 p == (void *)&efi_events.next;
Alexander Graf50149ea2016-03-04 01:10:01 +0100403}
404
Heinrich Schuchardtb23ffcb2019-07-05 18:12:21 +0200405/**
406 * efi_runtime_detach() - detach unimplemented runtime functions
407 */
Heinrich Schuchardt7f951042019-07-05 17:42:16 +0200408void efi_runtime_detach(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100409{
Heinrich Schuchardtb23ffcb2019-07-05 18:12:21 +0200410 efi_runtime_services.reset_system = efi_reset_system;
411 efi_runtime_services.get_time = efi_get_time;
412 efi_runtime_services.set_time = efi_set_time;
Alexander Graf50149ea2016-03-04 01:10:01 +0100413
Heinrich Schuchardtb23ffcb2019-07-05 18:12:21 +0200414 /* Update CRC32 */
415 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000416}
417
Heinrich Schuchardtee8ebaa2019-06-29 03:32:52 +0200418/**
419 * efi_set_virtual_address_map_runtime() - change from physical to virtual
420 * mapping
421 *
422 * This function implements the SetVirtualAddressMap() runtime service after
423 * it is first called.
424 *
425 * See the Unified Extensible Firmware Interface (UEFI) specification for
426 * details.
427 *
428 * @memory_map_size: size of the virtual map
429 * @descriptor_size: size of an entry in the map
430 * @descriptor_version: version of the map entries
431 * @virtmap: virtual address mapping information
432 * Return: status code EFI_UNSUPPORTED
433 */
Heinrich Schuchardt96185602019-07-12 22:41:43 +0200434static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map_runtime(
Heinrich Schuchardt24e67222019-07-27 20:28:47 +0200435 efi_uintn_t memory_map_size,
436 efi_uintn_t descriptor_size,
Heinrich Schuchardtee8ebaa2019-06-29 03:32:52 +0200437 uint32_t descriptor_version,
438 struct efi_mem_desc *virtmap)
439{
440 return EFI_UNSUPPORTED;
441}
442
443/**
444 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
445 *
446 * This function implements the ConvertPointer() runtime service after
447 * the first call to SetVirtualAddressMap().
448 *
449 * See the Unified Extensible Firmware Interface (UEFI) specification for
450 * details.
451 *
452 * @debug_disposition: indicates if pointer may be converted to NULL
453 * @address: pointer to be converted
454 * Return: status code EFI_UNSUPPORTED
455 */
456static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime(
457 efi_uintn_t debug_disposition, void **address)
458{
459 return EFI_UNSUPPORTED;
460}
461
Heinrich Schuchardt7be56e82019-07-27 20:35:24 +0200462/**
463 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
464 *
465 * This function implements the ConvertPointer() runtime service until
466 * the first call to SetVirtualAddressMap().
467 *
468 * See the Unified Extensible Firmware Interface (UEFI) specification for
469 * details.
470 *
471 * @debug_disposition: indicates if pointer may be converted to NULL
472 * @address: pointer to be converted
473 * Return: status code EFI_UNSUPPORTED
474 */
475static __efi_runtime efi_status_t EFIAPI efi_convert_pointer(
476 efi_uintn_t debug_disposition, void **address)
477{
478 efi_physical_addr_t addr = (uintptr_t)*address;
479 efi_uintn_t i;
480 efi_status_t ret = EFI_NOT_FOUND;
481
482 EFI_ENTRY("%zu %p", debug_disposition, address);
483
484 if (!efi_virtmap) {
485 ret = EFI_UNSUPPORTED;
486 goto out;
487 }
488
489 if (!address) {
490 ret = EFI_INVALID_PARAMETER;
491 goto out;
492 }
493
494 for (i = 0; i < efi_descriptor_count; i++) {
495 struct efi_mem_desc *map = (void *)efi_virtmap +
496 (efi_descriptor_size * i);
497
498 if (addr >= map->physical_start &&
499 (addr < map->physical_start
500 + (map->num_pages << EFI_PAGE_SHIFT))) {
501 *address = (void *)(uintptr_t)
502 (addr + map->virtual_start -
503 map->physical_start);
504
505 ret = EFI_SUCCESS;
506 break;
507 }
508 }
509
510out:
511 return EFI_EXIT(ret);
512}
513
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000514static __efi_runtime void efi_relocate_runtime_table(ulong offset)
515{
516 ulong patchoff;
517 void **pos;
518
519 /* Relocate the runtime services pointers */
520 patchoff = offset - gd->relocaddr;
521 for (pos = (void **)&efi_runtime_services.get_time;
522 pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
Heinrich Schuchardtee8ebaa2019-06-29 03:32:52 +0200523 if (*pos)
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000524 *pos += patchoff;
Alexander Graf50149ea2016-03-04 01:10:01 +0100525 }
Heinrich Schuchardta39f39c2018-07-29 09:49:04 +0200526
Heinrich Schuchardtee8ebaa2019-06-29 03:32:52 +0200527 /*
528 * The entry for SetVirtualAddress() must point to a physical address.
529 * After the first execution the service must return EFI_UNSUPPORTED.
530 */
531 efi_runtime_services.set_virtual_address_map =
532 &efi_set_virtual_address_map_runtime;
533
534 /*
535 * The entry for ConvertPointer() must point to a physical address.
536 * The service is not usable after SetVirtualAddress().
537 */
538 efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime;
539
Heinrich Schuchardt7be56e82019-07-27 20:35:24 +0200540 /*
541 * TODO: Update UEFI variable RuntimeServicesSupported removing flags
542 * EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP and
543 * EFI_RT_SUPPORTED_CONVERT_POINTER as required by the UEFI spec 2.8.
544 */
545
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200546 /* Update CRC32 */
Heinrich Schuchardta39f39c2018-07-29 09:49:04 +0200547 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100548}
549
550/* Relocate EFI runtime to uboot_reloc_base = offset */
551void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
552{
553#ifdef IS_RELA
554 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
555#else
556 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
557 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
558#endif
559
Alexander Grafedcef3b2016-06-02 11:38:27 +0200560 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf50149ea2016-03-04 01:10:01 +0100561 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
562 ulong base = CONFIG_SYS_TEXT_BASE;
563 ulong *p;
564 ulong newaddr;
565
566 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
567
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000568 /* The runtime services are updated in efi_runtime_detach() */
569 if (map && efi_is_runtime_service_pointer(p))
570 continue;
571
Heinrich Schuchardt3ce78292018-10-13 20:52:08 -0700572 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
573 rel->info, *p, rel->offset);
Alexander Graf50149ea2016-03-04 01:10:01 +0100574
Rick Chen6836adb2018-05-28 19:06:37 +0800575 switch (rel->info & R_MASK) {
576 case R_RELATIVE:
Alexander Graf50149ea2016-03-04 01:10:01 +0100577#ifdef IS_RELA
578 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
579#else
580 newaddr = *p - lastoff + offset;
581#endif
Rick Chen6836adb2018-05-28 19:06:37 +0800582 break;
583#ifdef R_ABSOLUTE
584 case R_ABSOLUTE: {
585 ulong symidx = rel->info >> SYM_INDEX;
586 extern struct dyn_sym __dyn_sym_start[];
587 newaddr = __dyn_sym_start[symidx].addr + offset;
Alexander Grafafdc4fc2018-11-04 22:25:22 +0100588#ifdef IS_RELA
589 newaddr -= CONFIG_SYS_TEXT_BASE;
590#endif
Rick Chen6836adb2018-05-28 19:06:37 +0800591 break;
592 }
593#endif
594 default:
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000595 printf("%s: Unknown relocation type %llx\n",
596 __func__, rel->info & R_MASK);
Rick Chen6836adb2018-05-28 19:06:37 +0800597 continue;
598 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100599
600 /* Check if the relocation is inside bounds */
601 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt591cf2e2017-09-18 22:11:34 +0200602 newaddr > (map->virtual_start +
603 (map->num_pages << EFI_PAGE_SHIFT)))) {
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000604 printf("%s: Relocation at %p is out of range (%lx)\n",
605 __func__, p, newaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100606 continue;
607 }
608
Alexander Grafedcef3b2016-06-02 11:38:27 +0200609 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100610 *p = newaddr;
Alexander Graf36c37a82016-04-11 23:20:39 +0200611 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
612 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf50149ea2016-03-04 01:10:01 +0100613 }
614
615#ifndef IS_RELA
616 lastoff = offset;
617#endif
618
619 invalidate_icache_all();
620}
621
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200622/**
623 * efi_set_virtual_address_map() - change from physical to virtual mapping
624 *
625 * This function implements the SetVirtualAddressMap() runtime service.
626 *
627 * See the Unified Extensible Firmware Interface (UEFI) specification for
628 * details.
629 *
630 * @memory_map_size: size of the virtual map
631 * @descriptor_size: size of an entry in the map
632 * @descriptor_version: version of the map entries
633 * @virtmap: virtual address mapping information
634 * Return: status code
635 */
Alexander Graf50149ea2016-03-04 01:10:01 +0100636static efi_status_t EFIAPI efi_set_virtual_address_map(
Heinrich Schuchardt24e67222019-07-27 20:28:47 +0200637 efi_uintn_t memory_map_size,
638 efi_uintn_t descriptor_size,
Alexander Graf50149ea2016-03-04 01:10:01 +0100639 uint32_t descriptor_version,
640 struct efi_mem_desc *virtmap)
641{
Heinrich Schuchardt24e67222019-07-27 20:28:47 +0200642 efi_uintn_t n = memory_map_size / descriptor_size;
643 efi_uintn_t i;
Heinrich Schuchardt53e1d8f2019-08-14 05:19:37 +0200644 efi_status_t ret = EFI_INVALID_PARAMETER;
Alexander Graf5c38e052018-12-11 10:00:42 +0100645 int rt_code_sections = 0;
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200646 struct efi_event *event;
Alexander Graf50149ea2016-03-04 01:10:01 +0100647
Heinrich Schuchardt24e67222019-07-27 20:28:47 +0200648 EFI_ENTRY("%zx %zx %x %p", memory_map_size, descriptor_size,
Alexander Graf50149ea2016-03-04 01:10:01 +0100649 descriptor_version, virtmap);
650
Heinrich Schuchardt53e1d8f2019-08-14 05:19:37 +0200651 if (descriptor_version != EFI_MEMORY_DESCRIPTOR_VERSION ||
652 descriptor_size < sizeof(struct efi_mem_desc))
653 goto out;
654
Heinrich Schuchardt7be56e82019-07-27 20:35:24 +0200655 efi_virtmap = virtmap;
656 efi_descriptor_size = descriptor_size;
657 efi_descriptor_count = n;
658
Alexander Graf5c38e052018-12-11 10:00:42 +0100659 /*
660 * TODO:
661 * Further down we are cheating. While really we should implement
662 * SetVirtualAddressMap() events and ConvertPointer() to allow
663 * dynamically loaded drivers to expose runtime services, we don't
664 * today.
665 *
666 * So let's ensure we see exactly one single runtime section, as
667 * that is the built-in one. If we see more (or less), someone must
668 * have tried adding or removing to that which we don't support yet.
669 * In that case, let's better fail rather than expose broken runtime
670 * services.
671 */
672 for (i = 0; i < n; i++) {
673 struct efi_mem_desc *map = (void*)virtmap +
674 (descriptor_size * i);
675
676 if (map->type == EFI_RUNTIME_SERVICES_CODE)
677 rt_code_sections++;
678 }
679
680 if (rt_code_sections != 1) {
681 /*
682 * We expose exactly one single runtime code section, so
683 * something is definitely going wrong.
684 */
Heinrich Schuchardt53e1d8f2019-08-14 05:19:37 +0200685 goto out;
Alexander Graf5c38e052018-12-11 10:00:42 +0100686 }
687
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200688 /* Notify EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
689 list_for_each_entry(event, &efi_events, link) {
690 if (event->notify_function)
691 EFI_CALL_VOID(event->notify_function(
692 event, event->notify_context));
693 }
694
Alexander Graf80a48002016-08-16 21:08:45 +0200695 /* Rebind mmio pointers */
696 for (i = 0; i < n; i++) {
697 struct efi_mem_desc *map = (void*)virtmap +
698 (descriptor_size * i);
699 struct list_head *lhandle;
700 efi_physical_addr_t map_start = map->physical_start;
701 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
702 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt07240da2018-08-04 23:16:06 +0200703 u64 off = map->virtual_start - map_start;
Alexander Graf80a48002016-08-16 21:08:45 +0200704
705 /* Adjust all mmio pointers in this region */
706 list_for_each(lhandle, &efi_runtime_mmio) {
707 struct efi_runtime_mmio_list *lmmio;
708
709 lmmio = list_entry(lhandle,
710 struct efi_runtime_mmio_list,
711 link);
712 if ((map_start <= lmmio->paddr) &&
713 (map_end >= lmmio->paddr)) {
Alexander Graf80a48002016-08-16 21:08:45 +0200714 uintptr_t new_addr = lmmio->paddr + off;
715 *lmmio->ptr = (void *)new_addr;
716 }
717 }
Heinrich Schuchardt07240da2018-08-04 23:16:06 +0200718 if ((map_start <= (uintptr_t)systab.tables) &&
719 (map_end >= (uintptr_t)systab.tables)) {
720 char *ptr = (char *)systab.tables;
721
722 ptr += off;
723 systab.tables = (struct efi_configuration_table *)ptr;
724 }
Alexander Graf80a48002016-08-16 21:08:45 +0200725 }
726
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000727 /*
728 * Some runtime services are implemented in a way that we can only offer
729 * them at boottime. Replace those function pointers.
730 *
731 * TODO: move this call to ExitBootServices().
732 */
733 efi_runtime_detach();
734
735 /* Relocate the runtime. See TODO above */
Alexander Graf50149ea2016-03-04 01:10:01 +0100736 for (i = 0; i < n; i++) {
737 struct efi_mem_desc *map;
738
739 map = (void*)virtmap + (descriptor_size * i);
740 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf80a48002016-08-16 21:08:45 +0200741 ulong new_offset = map->virtual_start -
Alexander Graf5c38e052018-12-11 10:00:42 +0100742 map->physical_start + gd->relocaddr;
Alexander Graf50149ea2016-03-04 01:10:01 +0100743
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000744 efi_relocate_runtime_table(new_offset);
Alexander Graf50149ea2016-03-04 01:10:01 +0100745 efi_runtime_relocate(new_offset, map);
Heinrich Schuchardt53e1d8f2019-08-14 05:19:37 +0200746 ret = EFI_SUCCESS;
747 goto out;
Alexander Graf50149ea2016-03-04 01:10:01 +0100748 }
749 }
750
Heinrich Schuchardt53e1d8f2019-08-14 05:19:37 +0200751out:
752 return EFI_EXIT(ret);
Alexander Graf50149ea2016-03-04 01:10:01 +0100753}
754
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200755/**
756 * efi_add_runtime_mmio() - add memory-mapped IO region
757 *
758 * This function adds a memory-mapped IO region to the memory map to make it
759 * available at runtime.
760 *
Heinrich Schuchardtfb34f292018-12-23 02:35:13 +0100761 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
762 * IO region
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200763 * @len: size of the memory-mapped IO region
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200764 * Returns: status code
765 */
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100766efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf80a48002016-08-16 21:08:45 +0200767{
768 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.deaee4f062017-08-11 21:19:37 +0200769 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf813468c2018-03-15 15:08:16 +0100770 uint64_t addr = *(uintptr_t *)mmio_ptr;
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100771 efi_status_t ret;
Alexander Graf813468c2018-03-15 15:08:16 +0100772
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100773 ret = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
774 if (ret != EFI_SUCCESS)
Alexander Graf813468c2018-03-15 15:08:16 +0100775 return EFI_OUT_OF_RESOURCES;
Alexander Graf80a48002016-08-16 21:08:45 +0200776
777 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100778 if (!newmmio)
779 return EFI_OUT_OF_RESOURCES;
Alexander Graf80a48002016-08-16 21:08:45 +0200780 newmmio->ptr = mmio_ptr;
781 newmmio->paddr = *(uintptr_t *)mmio_ptr;
782 newmmio->len = len;
783 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100784
Alexander Graf813468c2018-03-15 15:08:16 +0100785 return EFI_SUCCESS;
Alexander Graf80a48002016-08-16 21:08:45 +0200786}
787
Alexander Graf50149ea2016-03-04 01:10:01 +0100788/*
789 * In the second stage, U-Boot has disappeared. To isolate our runtime code
790 * that at this point still exists from the rest, we put it into a special
791 * section.
792 *
793 * !!WARNING!!
794 *
795 * This means that we can not rely on any code outside of this file in any
796 * function or variable below this line.
797 *
798 * Please keep everything fully self-contained and annotated with
Alexander Graf3c63db92016-10-14 13:45:30 +0200799 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf50149ea2016-03-04 01:10:01 +0100800 */
801
802/*
803 * Relocate the EFI runtime stub to a different place. We need to call this
804 * the first time we expose the runtime interface to a user and on set virtual
805 * address map calls.
806 */
807
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200808/**
809 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
810 *
811 * This function is used after SetVirtualAddressMap() is called as replacement
812 * for services that are not available anymore due to constraints of the U-Boot
813 * implementation.
814 *
815 * Return: EFI_UNSUPPORTED
816 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200817static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100818{
819 return EFI_UNSUPPORTED;
820}
821
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200822/**
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200823 * efi_update_capsule() - process information from operating system
824 *
825 * This function implements the UpdateCapsule() runtime service.
826 *
827 * See the Unified Extensible Firmware Interface (UEFI) specification for
828 * details.
829 *
830 * @capsule_header_array: pointer to array of virtual pointers
831 * @capsule_count: number of pointers in capsule_header_array
832 * @scatter_gather_list: pointer to arry of physical pointers
833 * Returns: status code
834 */
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100835efi_status_t __efi_runtime EFIAPI efi_update_capsule(
836 struct efi_capsule_header **capsule_header_array,
837 efi_uintn_t capsule_count,
838 u64 scatter_gather_list)
839{
840 return EFI_UNSUPPORTED;
841}
842
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200843/**
844 * efi_query_capsule_caps() - check if capsule is supported
845 *
846 * This function implements the QueryCapsuleCapabilities() runtime service.
847 *
848 * See the Unified Extensible Firmware Interface (UEFI) specification for
849 * details.
850 *
851 * @capsule_header_array: pointer to array of virtual pointers
852 * @capsule_count: number of pointers in capsule_header_array
Heinrich Schuchardtcc0bfc02018-09-03 20:59:25 +0200853 * @maximum_capsule_size: maximum capsule size
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200854 * @reset_type: type of reset needed for capsule update
855 * Returns: status code
856 */
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100857efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
858 struct efi_capsule_header **capsule_header_array,
859 efi_uintn_t capsule_count,
AKASHI Takahiro19dd9072018-11-14 16:18:53 +0900860 u64 *maximum_capsule_size,
861 u32 *reset_type)
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100862{
863 return EFI_UNSUPPORTED;
864}
865
Alexander Graf3c63db92016-10-14 13:45:30 +0200866struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf50149ea2016-03-04 01:10:01 +0100867 .hdr = {
868 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +0200869 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +0200870 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf50149ea2016-03-04 01:10:01 +0100871 },
Alexander Graf80a48002016-08-16 21:08:45 +0200872 .get_time = &efi_get_time_boottime,
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200873 .set_time = &efi_set_time_boottime,
Alexander Graf50149ea2016-03-04 01:10:01 +0100874 .get_wakeup_time = (void *)&efi_unimplemented,
875 .set_wakeup_time = (void *)&efi_unimplemented,
876 .set_virtual_address_map = &efi_set_virtual_address_map,
Heinrich Schuchardt7be56e82019-07-27 20:35:24 +0200877 .convert_pointer = efi_convert_pointer,
Rob Clarkad644e72017-09-13 18:05:37 -0400878 .get_variable = efi_get_variable,
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200879 .get_next_variable_name = efi_get_next_variable_name,
Rob Clarkad644e72017-09-13 18:05:37 -0400880 .set_variable = efi_set_variable,
Heinrich Schuchardtb94461c2019-06-20 15:40:49 +0200881 .get_next_high_mono_count = (void *)&efi_unimplemented,
Alexander Graf80a48002016-08-16 21:08:45 +0200882 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100883 .update_capsule = efi_update_capsule,
884 .query_capsule_caps = efi_query_capsule_caps,
885 .query_variable_info = efi_query_variable_info,
Alexander Graf50149ea2016-03-04 01:10:01 +0100886};