blob: 8b56ab0207cebaa50b5eafea4cc66e904f0d455f [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
84/*
Heinrich Schuchardt250b3252018-09-03 19:29:41 +020085 * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI
Alexander Graf50149ea2016-03-04 01:10:01 +010086 * payload are running concurrently at the same time. In this mode, we can
87 * handle a good number of runtime callbacks
88 */
89
AKASHI Takahiroe771b4b2019-06-05 13:21:38 +090090efi_status_t efi_init_runtime_supported(void)
91{
92 u16 efi_runtime_services_supported = 0;
93
94 /*
95 * This value must be synced with efi_runtime_detach_list
96 * as well as efi_runtime_services.
97 */
Heinrich Schuchardt953661a2019-07-05 18:12:16 +020098#ifdef CONFIG_EFI_HAVE_RUNTIME_RESET
AKASHI Takahiroe771b4b2019-06-05 13:21:38 +090099 efi_runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM;
100#endif
101 efi_runtime_services_supported |=
102 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP;
103 return EFI_CALL(efi_set_variable(L"RuntimeServicesSupported",
104 &efi_global_variable_guid,
105 EFI_VARIABLE_BOOTSERVICE_ACCESS |
106 EFI_VARIABLE_RUNTIME_ACCESS,
107 sizeof(efi_runtime_services_supported),
108 &efi_runtime_services_supported));
109}
110
Heinrich Schuchardta39f39c2018-07-29 09:49:04 +0200111/**
112 * efi_update_table_header_crc32() - Update crc32 in table header
113 *
114 * @table: EFI table
115 */
116void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
117{
118 table->crc32 = 0;
119 table->crc32 = crc32(0, (const unsigned char *)table,
120 table->headersize);
121}
122
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200123/**
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200124 * efi_reset_system_boottime() - reset system at boot time
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200125 *
126 * This function implements the ResetSystem() runtime service before
127 * SetVirtualAddressMap() is called.
128 *
129 * See the Unified Extensible Firmware Interface (UEFI) specification for
130 * details.
131 *
132 * @reset_type: type of reset to perform
133 * @reset_status: status code for the reset
134 * @data_size: size of reset_data
135 * @reset_data: information about the reset
136 */
Alexander Graf80a48002016-08-16 21:08:45 +0200137static void EFIAPI efi_reset_system_boottime(
138 enum efi_reset_type reset_type,
139 efi_status_t reset_status,
140 unsigned long data_size, void *reset_data)
Alexander Graf50149ea2016-03-04 01:10:01 +0100141{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100142 struct efi_event *evt;
143
Alexander Graf50149ea2016-03-04 01:10:01 +0100144 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
145 reset_data);
146
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100147 /* Notify reset */
148 list_for_each_entry(evt, &efi_events, link) {
149 if (evt->group &&
150 !guidcmp(evt->group,
151 &efi_guid_event_group_reset_system)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200152 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100153 break;
154 }
155 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100156 switch (reset_type) {
157 case EFI_RESET_COLD:
158 case EFI_RESET_WARM:
Heinrich Schuchardt482fc902018-02-06 22:00:22 +0100159 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf50149ea2016-03-04 01:10:01 +0100160 do_reset(NULL, 0, 0, NULL);
161 break;
162 case EFI_RESET_SHUTDOWN:
Heinrich Schuchardte706ed72018-10-16 07:44:53 +0200163#ifdef CONFIG_CMD_POWEROFF
164 do_poweroff(NULL, 0, 0, NULL);
165#endif
Alexander Graf50149ea2016-03-04 01:10:01 +0100166 break;
167 }
168
Alexander Graf80a48002016-08-16 21:08:45 +0200169 while (1) { }
Alexander Graf50149ea2016-03-04 01:10:01 +0100170}
171
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200172/**
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200173 * efi_get_time_boottime() - get current time at boot time
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200174 *
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200175 * This function implements the GetTime runtime service before
176 * SetVirtualAddressMap() is called.
177 *
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200178 * See the Unified Extensible Firmware Interface (UEFI) specification
179 * for details.
180 *
181 * @time: pointer to structure to receive current time
182 * @capabilities: pointer to structure to receive RTC properties
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200183 * Returns: status code
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200184 */
Alexander Graf80a48002016-08-16 21:08:45 +0200185static efi_status_t EFIAPI efi_get_time_boottime(
186 struct efi_time *time,
187 struct efi_time_cap *capabilities)
Alexander Graf50149ea2016-03-04 01:10:01 +0100188{
Heinrich Schuchardt5ec48e32019-05-31 22:56:02 +0200189#ifdef CONFIG_EFI_GET_TIME
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200190 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200191 struct rtc_time tm;
Alexander Graf50149ea2016-03-04 01:10:01 +0100192 struct udevice *dev;
193
194 EFI_ENTRY("%p %p", time, capabilities);
195
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200196 if (!time) {
197 ret = EFI_INVALID_PARAMETER;
198 goto out;
199 }
Heinrich Schuchardtbb2b13d2019-05-19 21:41:28 +0200200 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
201 dm_rtc_get(dev, &tm)) {
202 ret = EFI_UNSUPPORTED;
203 goto out;
204 }
205 if (dm_rtc_get(dev, &tm)) {
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200206 ret = EFI_DEVICE_ERROR;
207 goto out;
208 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100209
210 memset(time, 0, sizeof(*time));
211 time->year = tm.tm_year;
212 time->month = tm.tm_mon;
213 time->day = tm.tm_mday;
214 time->hour = tm.tm_hour;
215 time->minute = tm.tm_min;
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200216 time->second = tm.tm_sec;
Heinrich Schuchardt38b9a792019-05-31 22:08:45 +0200217 if (tm.tm_isdst)
218 time->daylight =
219 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200220 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
Alexander Graf50149ea2016-03-04 01:10:01 +0100221
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200222 if (capabilities) {
223 /* Set reasonable dummy values */
224 capabilities->resolution = 1; /* 1 Hz */
225 capabilities->accuracy = 100000000; /* 100 ppm */
226 capabilities->sets_to_zero = false;
227 }
228out:
229 return EFI_EXIT(ret);
Alexander Graf50149ea2016-03-04 01:10:01 +0100230#else
Heinrich Schuchardt49de2452018-07-07 23:39:14 +0200231 EFI_ENTRY("%p %p", time, capabilities);
Heinrich Schuchardtbb2b13d2019-05-19 21:41:28 +0200232 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf50149ea2016-03-04 01:10:01 +0100233#endif
234}
235
Heinrich Schuchardt5ec48e32019-05-31 22:56:02 +0200236#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardte6bcc352019-05-31 07:35:19 +0200237
238/**
239 * efi_validate_time() - checks if timestamp is valid
240 *
241 * @time: timestamp to validate
242 * Returns: 0 if timestamp is valid, 1 otherwise
243 */
244static int efi_validate_time(struct efi_time *time)
245{
246 return (!time ||
247 time->year < 1900 || time->year > 9999 ||
248 !time->month || time->month > 12 || !time->day ||
249 time->day > rtc_month_days(time->month - 1, time->year) ||
250 time->hour > 23 || time->minute > 59 || time->second > 59 ||
251 time->nanosecond > 999999999 ||
252 time->daylight &
253 ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
254 ((time->timezone < -1440 || time->timezone > 1440) &&
255 time->timezone != EFI_UNSPECIFIED_TIMEZONE));
256}
257
258#endif
259
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200260/**
261 * efi_set_time_boottime() - set current time
262 *
263 * This function implements the SetTime() runtime service before
264 * SetVirtualAddressMap() is called.
265 *
266 * See the Unified Extensible Firmware Interface (UEFI) specification
267 * for details.
268 *
269 * @time: pointer to structure to with current time
270 * Returns: status code
271 */
272static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
273{
Heinrich Schuchardt5ec48e32019-05-31 22:56:02 +0200274#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200275 efi_status_t ret = EFI_SUCCESS;
276 struct rtc_time tm;
277 struct udevice *dev;
Alexander Graf80a48002016-08-16 21:08:45 +0200278
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200279 EFI_ENTRY("%p", time);
280
Heinrich Schuchardte6bcc352019-05-31 07:35:19 +0200281 if (efi_validate_time(time)) {
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200282 ret = EFI_INVALID_PARAMETER;
283 goto out;
284 }
285
286 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
287 ret = EFI_UNSUPPORTED;
288 goto out;
289 }
290
291 memset(&tm, 0, sizeof(tm));
292 tm.tm_year = time->year;
293 tm.tm_mon = time->month;
294 tm.tm_mday = time->day;
295 tm.tm_hour = time->hour;
296 tm.tm_min = time->minute;
297 tm.tm_sec = time->second;
Heinrich Schuchardt38b9a792019-05-31 22:08:45 +0200298 tm.tm_isdst = time->daylight ==
299 (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT);
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200300 /* Calculate day of week */
301 rtc_calc_weekday(&tm);
302
303 if (dm_rtc_set(dev, &tm))
304 ret = EFI_DEVICE_ERROR;
305out:
306 return EFI_EXIT(ret);
307#else
308 EFI_ENTRY("%p", time);
309 return EFI_EXIT(EFI_UNSUPPORTED);
310#endif
311}
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200312/**
313 * efi_reset_system() - reset system
314 *
315 * This function implements the ResetSystem() runtime service after
316 * SetVirtualAddressMap() is called. It only executes an endless loop.
317 * Boards may override the helpers below to implement reset functionality.
318 *
319 * See the Unified Extensible Firmware Interface (UEFI) specification for
320 * details.
321 *
322 * @reset_type: type of reset to perform
323 * @reset_status: status code for the reset
324 * @data_size: size of reset_data
325 * @reset_data: information about the reset
326 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200327void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf80a48002016-08-16 21:08:45 +0200328 enum efi_reset_type reset_type,
329 efi_status_t reset_status,
330 unsigned long data_size, void *reset_data)
331{
332 /* Nothing we can do */
333 while (1) { }
334}
335
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200336/**
337 * efi_reset_system_init() - initialize the reset driver
338 *
339 * Boards may override this function to initialize the reset driver.
340 */
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100341efi_status_t __weak efi_reset_system_init(void)
Alexander Graf80a48002016-08-16 21:08:45 +0200342{
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100343 return EFI_SUCCESS;
Alexander Graf80a48002016-08-16 21:08:45 +0200344}
345
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200346/**
347 * efi_get_time() - get current time
348 *
349 * This function implements the GetTime runtime service after
350 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
351 * anymore only an error code is returned.
352 *
353 * See the Unified Extensible Firmware Interface (UEFI) specification
354 * for details.
355 *
356 * @time: pointer to structure to receive current time
357 * @capabilities: pointer to structure to receive RTC properties
358 * Returns: status code
359 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200360efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf80a48002016-08-16 21:08:45 +0200361 struct efi_time *time,
362 struct efi_time_cap *capabilities)
363{
Heinrich Schuchardtc5b63be2019-06-13 18:42:40 +0200364 return EFI_UNSUPPORTED;
Alexander Graf80a48002016-08-16 21:08:45 +0200365}
366
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200367/**
368 * efi_set_time() - set current time
369 *
370 * This function implements the SetTime runtime service after
371 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
372 * anymore only an error code is returned.
373 *
374 * See the Unified Extensible Firmware Interface (UEFI) specification
375 * for details.
376 *
377 * @time: pointer to structure to with current time
378 * Returns: status code
379 */
380efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
381{
382 return EFI_UNSUPPORTED;
383}
384
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000385/**
386 * efi_is_runtime_service_pointer() - check if pointer points to runtime table
387 *
388 * @p: pointer to check
389 * Return: true if the pointer points to a service function pointer in the
390 * runtime table
391 */
392static bool efi_is_runtime_service_pointer(void *p)
Alexander Graf50149ea2016-03-04 01:10:01 +0100393{
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200394 return (p >= (void *)&efi_runtime_services.get_time &&
395 p <= (void *)&efi_runtime_services.query_variable_info) ||
396 p == (void *)&efi_events.prev ||
397 p == (void *)&efi_events.next;
Alexander Graf50149ea2016-03-04 01:10:01 +0100398}
399
Heinrich Schuchardtb23ffcb2019-07-05 18:12:21 +0200400/**
401 * efi_runtime_detach() - detach unimplemented runtime functions
402 */
Heinrich Schuchardt7f951042019-07-05 17:42:16 +0200403void efi_runtime_detach(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100404{
Heinrich Schuchardtb23ffcb2019-07-05 18:12:21 +0200405 efi_runtime_services.reset_system = efi_reset_system;
406 efi_runtime_services.get_time = efi_get_time;
407 efi_runtime_services.set_time = efi_set_time;
Alexander Graf50149ea2016-03-04 01:10:01 +0100408
Heinrich Schuchardtb23ffcb2019-07-05 18:12:21 +0200409 /* Update CRC32 */
410 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000411}
412
Heinrich Schuchardtee8ebaa2019-06-29 03:32:52 +0200413/**
414 * efi_set_virtual_address_map_runtime() - change from physical to virtual
415 * mapping
416 *
417 * This function implements the SetVirtualAddressMap() runtime service after
418 * it is first called.
419 *
420 * See the Unified Extensible Firmware Interface (UEFI) specification for
421 * details.
422 *
423 * @memory_map_size: size of the virtual map
424 * @descriptor_size: size of an entry in the map
425 * @descriptor_version: version of the map entries
426 * @virtmap: virtual address mapping information
427 * Return: status code EFI_UNSUPPORTED
428 */
Heinrich Schuchardt96185602019-07-12 22:41:43 +0200429static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map_runtime(
Heinrich Schuchardtee8ebaa2019-06-29 03:32:52 +0200430 unsigned long memory_map_size,
431 unsigned long descriptor_size,
432 uint32_t descriptor_version,
433 struct efi_mem_desc *virtmap)
434{
435 return EFI_UNSUPPORTED;
436}
437
438/**
439 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
440 *
441 * This function implements the ConvertPointer() runtime service after
442 * the first call to SetVirtualAddressMap().
443 *
444 * See the Unified Extensible Firmware Interface (UEFI) specification for
445 * details.
446 *
447 * @debug_disposition: indicates if pointer may be converted to NULL
448 * @address: pointer to be converted
449 * Return: status code EFI_UNSUPPORTED
450 */
451static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime(
452 efi_uintn_t debug_disposition, void **address)
453{
454 return EFI_UNSUPPORTED;
455}
456
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000457static __efi_runtime void efi_relocate_runtime_table(ulong offset)
458{
459 ulong patchoff;
460 void **pos;
461
462 /* Relocate the runtime services pointers */
463 patchoff = offset - gd->relocaddr;
464 for (pos = (void **)&efi_runtime_services.get_time;
465 pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
Heinrich Schuchardtee8ebaa2019-06-29 03:32:52 +0200466 if (*pos)
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000467 *pos += patchoff;
Alexander Graf50149ea2016-03-04 01:10:01 +0100468 }
Heinrich Schuchardta39f39c2018-07-29 09:49:04 +0200469
Heinrich Schuchardtee8ebaa2019-06-29 03:32:52 +0200470 /*
471 * The entry for SetVirtualAddress() must point to a physical address.
472 * After the first execution the service must return EFI_UNSUPPORTED.
473 */
474 efi_runtime_services.set_virtual_address_map =
475 &efi_set_virtual_address_map_runtime;
476
477 /*
478 * The entry for ConvertPointer() must point to a physical address.
479 * The service is not usable after SetVirtualAddress().
480 */
481 efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime;
482
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200483 /* Update CRC32 */
Heinrich Schuchardta39f39c2018-07-29 09:49:04 +0200484 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100485}
486
487/* Relocate EFI runtime to uboot_reloc_base = offset */
488void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
489{
490#ifdef IS_RELA
491 struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
492#else
493 struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
494 static ulong lastoff = CONFIG_SYS_TEXT_BASE;
495#endif
496
Alexander Grafedcef3b2016-06-02 11:38:27 +0200497 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Alexander Graf50149ea2016-03-04 01:10:01 +0100498 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
499 ulong base = CONFIG_SYS_TEXT_BASE;
500 ulong *p;
501 ulong newaddr;
502
503 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
504
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000505 /* The runtime services are updated in efi_runtime_detach() */
506 if (map && efi_is_runtime_service_pointer(p))
507 continue;
508
Heinrich Schuchardt3ce78292018-10-13 20:52:08 -0700509 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
510 rel->info, *p, rel->offset);
Alexander Graf50149ea2016-03-04 01:10:01 +0100511
Rick Chen6836adb2018-05-28 19:06:37 +0800512 switch (rel->info & R_MASK) {
513 case R_RELATIVE:
Alexander Graf50149ea2016-03-04 01:10:01 +0100514#ifdef IS_RELA
515 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
516#else
517 newaddr = *p - lastoff + offset;
518#endif
Rick Chen6836adb2018-05-28 19:06:37 +0800519 break;
520#ifdef R_ABSOLUTE
521 case R_ABSOLUTE: {
522 ulong symidx = rel->info >> SYM_INDEX;
523 extern struct dyn_sym __dyn_sym_start[];
524 newaddr = __dyn_sym_start[symidx].addr + offset;
Alexander Grafafdc4fc2018-11-04 22:25:22 +0100525#ifdef IS_RELA
526 newaddr -= CONFIG_SYS_TEXT_BASE;
527#endif
Rick Chen6836adb2018-05-28 19:06:37 +0800528 break;
529 }
530#endif
531 default:
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000532 printf("%s: Unknown relocation type %llx\n",
533 __func__, rel->info & R_MASK);
Rick Chen6836adb2018-05-28 19:06:37 +0800534 continue;
535 }
Alexander Graf50149ea2016-03-04 01:10:01 +0100536
537 /* Check if the relocation is inside bounds */
538 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt591cf2e2017-09-18 22:11:34 +0200539 newaddr > (map->virtual_start +
540 (map->num_pages << EFI_PAGE_SHIFT)))) {
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000541 printf("%s: Relocation at %p is out of range (%lx)\n",
542 __func__, p, newaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100543 continue;
544 }
545
Alexander Grafedcef3b2016-06-02 11:38:27 +0200546 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf50149ea2016-03-04 01:10:01 +0100547 *p = newaddr;
Alexander Graf36c37a82016-04-11 23:20:39 +0200548 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
549 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf50149ea2016-03-04 01:10:01 +0100550 }
551
552#ifndef IS_RELA
553 lastoff = offset;
554#endif
555
556 invalidate_icache_all();
557}
558
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200559/**
560 * efi_set_virtual_address_map() - change from physical to virtual mapping
561 *
562 * This function implements the SetVirtualAddressMap() runtime service.
563 *
564 * See the Unified Extensible Firmware Interface (UEFI) specification for
565 * details.
566 *
567 * @memory_map_size: size of the virtual map
568 * @descriptor_size: size of an entry in the map
569 * @descriptor_version: version of the map entries
570 * @virtmap: virtual address mapping information
571 * Return: status code
572 */
Alexander Graf50149ea2016-03-04 01:10:01 +0100573static efi_status_t EFIAPI efi_set_virtual_address_map(
574 unsigned long memory_map_size,
575 unsigned long descriptor_size,
576 uint32_t descriptor_version,
577 struct efi_mem_desc *virtmap)
578{
Alexander Graf50149ea2016-03-04 01:10:01 +0100579 int n = memory_map_size / descriptor_size;
580 int i;
Alexander Graf5c38e052018-12-11 10:00:42 +0100581 int rt_code_sections = 0;
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200582 struct efi_event *event;
Alexander Graf50149ea2016-03-04 01:10:01 +0100583
584 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
585 descriptor_version, virtmap);
586
Alexander Graf5c38e052018-12-11 10:00:42 +0100587 /*
588 * TODO:
589 * Further down we are cheating. While really we should implement
590 * SetVirtualAddressMap() events and ConvertPointer() to allow
591 * dynamically loaded drivers to expose runtime services, we don't
592 * today.
593 *
594 * So let's ensure we see exactly one single runtime section, as
595 * that is the built-in one. If we see more (or less), someone must
596 * have tried adding or removing to that which we don't support yet.
597 * In that case, let's better fail rather than expose broken runtime
598 * services.
599 */
600 for (i = 0; i < n; i++) {
601 struct efi_mem_desc *map = (void*)virtmap +
602 (descriptor_size * i);
603
604 if (map->type == EFI_RUNTIME_SERVICES_CODE)
605 rt_code_sections++;
606 }
607
608 if (rt_code_sections != 1) {
609 /*
610 * We expose exactly one single runtime code section, so
611 * something is definitely going wrong.
612 */
613 return EFI_EXIT(EFI_INVALID_PARAMETER);
614 }
615
Heinrich Schuchardt14b40482019-07-11 20:15:09 +0200616 /* Notify EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
617 list_for_each_entry(event, &efi_events, link) {
618 if (event->notify_function)
619 EFI_CALL_VOID(event->notify_function(
620 event, event->notify_context));
621 }
622
Alexander Graf80a48002016-08-16 21:08:45 +0200623 /* Rebind mmio pointers */
624 for (i = 0; i < n; i++) {
625 struct efi_mem_desc *map = (void*)virtmap +
626 (descriptor_size * i);
627 struct list_head *lhandle;
628 efi_physical_addr_t map_start = map->physical_start;
629 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
630 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt07240da2018-08-04 23:16:06 +0200631 u64 off = map->virtual_start - map_start;
Alexander Graf80a48002016-08-16 21:08:45 +0200632
633 /* Adjust all mmio pointers in this region */
634 list_for_each(lhandle, &efi_runtime_mmio) {
635 struct efi_runtime_mmio_list *lmmio;
636
637 lmmio = list_entry(lhandle,
638 struct efi_runtime_mmio_list,
639 link);
640 if ((map_start <= lmmio->paddr) &&
641 (map_end >= lmmio->paddr)) {
Alexander Graf80a48002016-08-16 21:08:45 +0200642 uintptr_t new_addr = lmmio->paddr + off;
643 *lmmio->ptr = (void *)new_addr;
644 }
645 }
Heinrich Schuchardt07240da2018-08-04 23:16:06 +0200646 if ((map_start <= (uintptr_t)systab.tables) &&
647 (map_end >= (uintptr_t)systab.tables)) {
648 char *ptr = (char *)systab.tables;
649
650 ptr += off;
651 systab.tables = (struct efi_configuration_table *)ptr;
652 }
Alexander Graf80a48002016-08-16 21:08:45 +0200653 }
654
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000655 /*
656 * Some runtime services are implemented in a way that we can only offer
657 * them at boottime. Replace those function pointers.
658 *
659 * TODO: move this call to ExitBootServices().
660 */
661 efi_runtime_detach();
662
663 /* Relocate the runtime. See TODO above */
Alexander Graf50149ea2016-03-04 01:10:01 +0100664 for (i = 0; i < n; i++) {
665 struct efi_mem_desc *map;
666
667 map = (void*)virtmap + (descriptor_size * i);
668 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf80a48002016-08-16 21:08:45 +0200669 ulong new_offset = map->virtual_start -
Alexander Graf5c38e052018-12-11 10:00:42 +0100670 map->physical_start + gd->relocaddr;
Alexander Graf50149ea2016-03-04 01:10:01 +0100671
Heinrich Schuchardt24a238f2019-06-20 22:00:02 +0000672 efi_relocate_runtime_table(new_offset);
Alexander Graf50149ea2016-03-04 01:10:01 +0100673 efi_runtime_relocate(new_offset, map);
Alexander Graf50149ea2016-03-04 01:10:01 +0100674 return EFI_EXIT(EFI_SUCCESS);
675 }
676 }
677
678 return EFI_EXIT(EFI_INVALID_PARAMETER);
679}
680
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200681/**
682 * efi_add_runtime_mmio() - add memory-mapped IO region
683 *
684 * This function adds a memory-mapped IO region to the memory map to make it
685 * available at runtime.
686 *
Heinrich Schuchardtfb34f292018-12-23 02:35:13 +0100687 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
688 * IO region
Heinrich Schuchardt250b3252018-09-03 19:29:41 +0200689 * @len: size of the memory-mapped IO region
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200690 * Returns: status code
691 */
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100692efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf80a48002016-08-16 21:08:45 +0200693{
694 struct efi_runtime_mmio_list *newmmio;
xypron.glpk@gmx.deaee4f062017-08-11 21:19:37 +0200695 u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
Alexander Graf813468c2018-03-15 15:08:16 +0100696 uint64_t addr = *(uintptr_t *)mmio_ptr;
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100697 efi_status_t ret;
Alexander Graf813468c2018-03-15 15:08:16 +0100698
Bryan O'Donoghueb225c922019-07-15 12:00:39 +0100699 ret = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
700 if (ret != EFI_SUCCESS)
Alexander Graf813468c2018-03-15 15:08:16 +0100701 return EFI_OUT_OF_RESOURCES;
Alexander Graf80a48002016-08-16 21:08:45 +0200702
703 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100704 if (!newmmio)
705 return EFI_OUT_OF_RESOURCES;
Alexander Graf80a48002016-08-16 21:08:45 +0200706 newmmio->ptr = mmio_ptr;
707 newmmio->paddr = *(uintptr_t *)mmio_ptr;
708 newmmio->len = len;
709 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt22c793e2018-03-03 15:28:59 +0100710
Alexander Graf813468c2018-03-15 15:08:16 +0100711 return EFI_SUCCESS;
Alexander Graf80a48002016-08-16 21:08:45 +0200712}
713
Alexander Graf50149ea2016-03-04 01:10:01 +0100714/*
715 * In the second stage, U-Boot has disappeared. To isolate our runtime code
716 * that at this point still exists from the rest, we put it into a special
717 * section.
718 *
719 * !!WARNING!!
720 *
721 * This means that we can not rely on any code outside of this file in any
722 * function or variable below this line.
723 *
724 * Please keep everything fully self-contained and annotated with
Alexander Graf3c63db92016-10-14 13:45:30 +0200725 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf50149ea2016-03-04 01:10:01 +0100726 */
727
728/*
729 * Relocate the EFI runtime stub to a different place. We need to call this
730 * the first time we expose the runtime interface to a user and on set virtual
731 * address map calls.
732 */
733
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200734/**
735 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
736 *
737 * This function is used after SetVirtualAddressMap() is called as replacement
738 * for services that are not available anymore due to constraints of the U-Boot
739 * implementation.
740 *
741 * Return: EFI_UNSUPPORTED
742 */
Alexander Graf3c63db92016-10-14 13:45:30 +0200743static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf50149ea2016-03-04 01:10:01 +0100744{
745 return EFI_UNSUPPORTED;
746}
747
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200748/**
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200749 * efi_update_capsule() - process information from operating system
750 *
751 * This function implements the UpdateCapsule() runtime service.
752 *
753 * See the Unified Extensible Firmware Interface (UEFI) specification for
754 * details.
755 *
756 * @capsule_header_array: pointer to array of virtual pointers
757 * @capsule_count: number of pointers in capsule_header_array
758 * @scatter_gather_list: pointer to arry of physical pointers
759 * Returns: status code
760 */
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100761efi_status_t __efi_runtime EFIAPI efi_update_capsule(
762 struct efi_capsule_header **capsule_header_array,
763 efi_uintn_t capsule_count,
764 u64 scatter_gather_list)
765{
766 return EFI_UNSUPPORTED;
767}
768
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200769/**
770 * efi_query_capsule_caps() - check if capsule is supported
771 *
772 * This function implements the QueryCapsuleCapabilities() runtime service.
773 *
774 * See the Unified Extensible Firmware Interface (UEFI) specification for
775 * details.
776 *
777 * @capsule_header_array: pointer to array of virtual pointers
778 * @capsule_count: number of pointers in capsule_header_array
Heinrich Schuchardtcc0bfc02018-09-03 20:59:25 +0200779 * @maximum_capsule_size: maximum capsule size
Heinrich Schuchardtbcfb0e22018-07-29 15:10:11 +0200780 * @reset_type: type of reset needed for capsule update
781 * Returns: status code
782 */
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100783efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
784 struct efi_capsule_header **capsule_header_array,
785 efi_uintn_t capsule_count,
AKASHI Takahiro19dd9072018-11-14 16:18:53 +0900786 u64 *maximum_capsule_size,
787 u32 *reset_type)
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100788{
789 return EFI_UNSUPPORTED;
790}
791
Alexander Graf3c63db92016-10-14 13:45:30 +0200792struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf50149ea2016-03-04 01:10:01 +0100793 .hdr = {
794 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +0200795 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +0200796 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf50149ea2016-03-04 01:10:01 +0100797 },
Alexander Graf80a48002016-08-16 21:08:45 +0200798 .get_time = &efi_get_time_boottime,
Heinrich Schuchardt433bfe72019-05-19 20:07:39 +0200799 .set_time = &efi_set_time_boottime,
Alexander Graf50149ea2016-03-04 01:10:01 +0100800 .get_wakeup_time = (void *)&efi_unimplemented,
801 .set_wakeup_time = (void *)&efi_unimplemented,
802 .set_virtual_address_map = &efi_set_virtual_address_map,
Heinrich Schuchardtb94461c2019-06-20 15:40:49 +0200803 .convert_pointer = (void *)&efi_unimplemented,
Rob Clarkad644e72017-09-13 18:05:37 -0400804 .get_variable = efi_get_variable,
Heinrich Schuchardt45c66f92018-05-17 07:57:05 +0200805 .get_next_variable_name = efi_get_next_variable_name,
Rob Clarkad644e72017-09-13 18:05:37 -0400806 .set_variable = efi_set_variable,
Heinrich Schuchardtb94461c2019-06-20 15:40:49 +0200807 .get_next_high_mono_count = (void *)&efi_unimplemented,
Alexander Graf80a48002016-08-16 21:08:45 +0200808 .reset_system = &efi_reset_system_boottime,
Heinrich Schuchardt0c230742018-02-09 20:41:21 +0100809 .update_capsule = efi_update_capsule,
810 .query_capsule_caps = efi_query_capsule_caps,
811 .query_variable_info = efi_query_variable_info,
Alexander Graf50149ea2016-03-04 01:10:01 +0100812};