blob: 05369c47b01bcd975d0db4d3f362f3af65955722 [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
Alexander Graf0bd425a2016-03-04 01:10:01 +01008#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -07009#include <cpu_func.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010010#include <dm.h>
Alexander Graf46c15142018-06-18 17:23:11 +020011#include <elf.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010012#include <efi_loader.h>
Ilias Apalodimas465cb7e2024-04-18 15:54:51 +030013#include <efi_variable.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <malloc.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010016#include <rtc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070018#include <u-boot/crc.h>
Ilias Apalodimas9b378942024-03-15 08:43:47 +020019#include <asm/sections.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010020
21/* For manual relocation support */
22DECLARE_GLOBAL_DATA_PTR;
23
Heinrich Schuchardt956eff32020-02-19 20:48:49 +010024/* GUID of the runtime properties table */
25static const efi_guid_t efi_rt_properties_table_guid =
26 EFI_RT_PROPERTIES_TABLE_GUID;
27
Alexander Graf77256092016-08-16 21:08:45 +020028struct efi_runtime_mmio_list {
29 struct list_head link;
30 void **ptr;
31 u64 paddr;
32 u64 len;
33};
34
35/* This list contains all runtime available mmio regions */
Bin Mengba7ad022023-04-05 20:15:19 +080036static LIST_HEAD(efi_runtime_mmio);
Alexander Graf77256092016-08-16 21:08:45 +020037
Alexander Graf393dd912016-10-14 13:45:30 +020038static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void);
Alexander Graf0bd425a2016-03-04 01:10:01 +010039
Simon Glassfff89d42018-06-11 23:26:40 -060040/*
Heinrich Schuchardt11121542018-09-03 19:29:41 +020041 * TODO(sjg@chromium.org): These defines and structures should come from the ELF
42 * header for each architecture (or a generic header) rather than being repeated
43 * here.
Simon Glassfff89d42018-06-11 23:26:40 -060044 */
Alexander Graf0fe51922018-06-18 17:23:08 +020045#if defined(__aarch64__)
Alexander Graf46c15142018-06-18 17:23:11 +020046#define R_RELATIVE R_AARCH64_RELATIVE
Alexander Graf0bd425a2016-03-04 01:10:01 +010047#define R_MASK 0xffffffffULL
48#define IS_RELA 1
Alexander Graf0fe51922018-06-18 17:23:08 +020049#elif defined(__arm__)
Alexander Graf46c15142018-06-18 17:23:11 +020050#define R_RELATIVE R_ARM_RELATIVE
Alexander Graf0bd425a2016-03-04 01:10:01 +010051#define R_MASK 0xffULL
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -070052#elif defined(__i386__)
Simon Glasscdfe6962016-09-25 15:27:35 -060053#define R_RELATIVE R_386_RELATIVE
54#define R_MASK 0xffULL
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -070055#elif defined(__x86_64__)
56#define R_RELATIVE R_X86_64_RELATIVE
57#define R_MASK 0xffffffffULL
58#define IS_RELA 1
Alexander Graf0fe51922018-06-18 17:23:08 +020059#elif defined(__riscv)
Rick Chen9677a372018-05-28 19:06:37 +080060#define R_RELATIVE R_RISCV_RELATIVE
61#define R_MASK 0xffULL
62#define IS_RELA 1
63
64struct dyn_sym {
65 ulong foo1;
66 ulong addr;
67 u32 foo2;
68 u32 foo3;
69};
Alexander Graf0fe51922018-06-18 17:23:08 +020070#if (__riscv_xlen == 32)
Rick Chen9677a372018-05-28 19:06:37 +080071#define R_ABSOLUTE R_RISCV_32
72#define SYM_INDEX 8
Alexander Graf0fe51922018-06-18 17:23:08 +020073#elif (__riscv_xlen == 64)
Rick Chen9677a372018-05-28 19:06:37 +080074#define R_ABSOLUTE R_RISCV_64
75#define SYM_INDEX 32
Alexander Graf0fe51922018-06-18 17:23:08 +020076#else
77#error unknown riscv target
Rick Chen9677a372018-05-28 19:06:37 +080078#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +010079#else
80#error Need to add relocation awareness
81#endif
82
83struct elf_rel {
84 ulong *offset;
85 ulong info;
86};
87
88struct elf_rela {
89 ulong *offset;
90 ulong info;
91 long addend;
92};
93
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +020094static __efi_runtime_data struct efi_mem_desc *efi_virtmap;
95static __efi_runtime_data efi_uintn_t efi_descriptor_count;
96static __efi_runtime_data efi_uintn_t efi_descriptor_size;
97
Alexander Graf0bd425a2016-03-04 01:10:01 +010098/*
Heinrich Schuchardt11121542018-09-03 19:29:41 +020099 * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI
Alexander Graf0bd425a2016-03-04 01:10:01 +0100100 * payload are running concurrently at the same time. In this mode, we can
101 * handle a good number of runtime callbacks
102 */
103
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100104/**
105 * efi_init_runtime_supported() - create runtime properties table
106 *
107 * Create a configuration table specifying which services are available at
108 * runtime.
109 *
110 * Return: status code
111 */
AKASHI Takahiro32401842019-06-05 13:21:38 +0900112efi_status_t efi_init_runtime_supported(void)
113{
Ilias Apalodimas465cb7e2024-04-18 15:54:51 +0300114 const efi_guid_t efi_guid_efi_rt_var_file = U_BOOT_EFI_RT_VAR_FILE_GUID;
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100115 efi_status_t ret;
116 struct efi_rt_properties_table *rt_table;
117
118 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
119 sizeof(struct efi_rt_properties_table),
120 (void **)&rt_table);
121 if (ret != EFI_SUCCESS)
122 return ret;
123
124 rt_table->version = EFI_RT_PROPERTIES_TABLE_VERSION;
125 rt_table->length = sizeof(struct efi_rt_properties_table);
126 rt_table->runtime_services_supported =
Heinrich Schuchardt5be59712020-03-24 19:54:53 +0000127 EFI_RT_SUPPORTED_GET_VARIABLE |
128 EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME |
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200129 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP |
130 EFI_RT_SUPPORTED_CONVERT_POINTER;
AKASHI Takahiro32401842019-06-05 13:21:38 +0900131
Ilias Apalodimasfc071532024-04-25 08:18:19 +0300132 if (IS_ENABLED(CONFIG_EFI_VARIABLE_FILE_STORE))
133 rt_table->runtime_services_supported |=
134 EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO;
135
Ilias Apalodimas465cb7e2024-04-18 15:54:51 +0300136 if (IS_ENABLED(CONFIG_EFI_RT_VOLATILE_STORE)) {
Ilias Apalodimasde708562024-04-18 15:54:52 +0300137 u8 s = 0;
138
Ilias Apalodimas465cb7e2024-04-18 15:54:51 +0300139 ret = efi_set_variable_int(u"RTStorageVolatile",
140 &efi_guid_efi_rt_var_file,
141 EFI_VARIABLE_BOOTSERVICE_ACCESS |
142 EFI_VARIABLE_RUNTIME_ACCESS |
143 EFI_VARIABLE_READ_ONLY,
144 sizeof(EFI_VAR_FILE_NAME),
145 EFI_VAR_FILE_NAME, false);
146 if (ret != EFI_SUCCESS) {
147 log_err("Failed to set RTStorageVolatile\n");
148 return ret;
149 }
Ilias Apalodimasde708562024-04-18 15:54:52 +0300150 /*
151 * This variable needs to be visible so users can read it,
152 * but the real contents are going to be filled during
153 * GetVariable
154 */
155 ret = efi_set_variable_int(u"VarToFile",
156 &efi_guid_efi_rt_var_file,
157 EFI_VARIABLE_BOOTSERVICE_ACCESS |
158 EFI_VARIABLE_RUNTIME_ACCESS |
159 EFI_VARIABLE_READ_ONLY,
160 sizeof(s),
161 &s, false);
162 if (ret != EFI_SUCCESS) {
163 log_err("Failed to set VarToFile\n");
164 efi_set_variable_int(u"RTStorageVolatile",
165 &efi_guid_efi_rt_var_file,
166 EFI_VARIABLE_BOOTSERVICE_ACCESS |
167 EFI_VARIABLE_RUNTIME_ACCESS |
168 EFI_VARIABLE_READ_ONLY,
169 0, NULL, false);
170
171 return ret;
172 }
Ilias Apalodimas465cb7e2024-04-18 15:54:51 +0300173 rt_table->runtime_services_supported |= EFI_RT_SUPPORTED_SET_VARIABLE;
174 }
Ilias Apalodimas86ba8692024-04-18 15:54:50 +0300175
AKASHI Takahiro32401842019-06-05 13:21:38 +0900176 /*
177 * This value must be synced with efi_runtime_detach_list
178 * as well as efi_runtime_services.
179 */
Heinrich Schuchardt05874fb2019-07-05 18:12:16 +0200180#ifdef CONFIG_EFI_HAVE_RUNTIME_RESET
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100181 rt_table->runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM;
AKASHI Takahiro32401842019-06-05 13:21:38 +0900182#endif
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200183
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100184 ret = efi_install_configuration_table(&efi_rt_properties_table_guid,
185 rt_table);
186 return ret;
AKASHI Takahiro32401842019-06-05 13:21:38 +0900187}
188
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200189/**
Heinrich Schuchardt8cd99752020-06-28 16:30:29 +0200190 * efi_memcpy_runtime() - copy memory area
191 *
192 * At runtime memcpy() is not available.
193 *
Heinrich Schuchardt447753e2020-07-22 07:56:14 +0200194 * Overlapping memory areas can be copied safely if src >= dest.
195 *
Heinrich Schuchardt8cd99752020-06-28 16:30:29 +0200196 * @dest: destination buffer
197 * @src: source buffer
198 * @n: number of bytes to copy
199 * Return: pointer to destination buffer
200 */
201void __efi_runtime efi_memcpy_runtime(void *dest, const void *src, size_t n)
202{
203 u8 *d = dest;
204 const u8 *s = src;
205
206 for (; n; --n)
207 *d++ = *s++;
208}
209
210/**
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200211 * efi_update_table_header_crc32() - Update crc32 in table header
212 *
213 * @table: EFI table
214 */
215void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
216{
217 table->crc32 = 0;
218 table->crc32 = crc32(0, (const unsigned char *)table,
219 table->headersize);
220}
221
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200222/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200223 * efi_reset_system_boottime() - reset system at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200224 *
225 * This function implements the ResetSystem() runtime service before
226 * SetVirtualAddressMap() is called.
227 *
228 * See the Unified Extensible Firmware Interface (UEFI) specification for
229 * details.
230 *
231 * @reset_type: type of reset to perform
232 * @reset_status: status code for the reset
233 * @data_size: size of reset_data
234 * @reset_data: information about the reset
235 */
Alexander Graf77256092016-08-16 21:08:45 +0200236static void EFIAPI efi_reset_system_boottime(
237 enum efi_reset_type reset_type,
238 efi_status_t reset_status,
239 unsigned long data_size, void *reset_data)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100240{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100241 struct efi_event *evt;
242
Alexander Graf0bd425a2016-03-04 01:10:01 +0100243 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
244 reset_data);
245
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100246 /* Notify reset */
247 list_for_each_entry(evt, &efi_events, link) {
248 if (evt->group &&
249 !guidcmp(evt->group,
250 &efi_guid_event_group_reset_system)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200251 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100252 break;
253 }
254 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100255 switch (reset_type) {
256 case EFI_RESET_COLD:
257 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +0100258 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100259 do_reset(NULL, 0, 0, NULL);
260 break;
261 case EFI_RESET_SHUTDOWN:
Heinrich Schuchardt44489452018-10-16 07:44:53 +0200262#ifdef CONFIG_CMD_POWEROFF
263 do_poweroff(NULL, 0, 0, NULL);
264#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +0100265 break;
266 }
267
Alexander Graf77256092016-08-16 21:08:45 +0200268 while (1) { }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100269}
270
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200271/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200272 * efi_get_time_boottime() - get current time at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200273 *
274 * This function implements the GetTime runtime service before
275 * SetVirtualAddressMap() is called.
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200276 *
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200277 * See the Unified Extensible Firmware Interface (UEFI) specification
278 * for details.
279 *
280 * @time: pointer to structure to receive current time
281 * @capabilities: pointer to structure to receive RTC properties
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200282 * Returns: status code
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200283 */
Alexander Graf77256092016-08-16 21:08:45 +0200284static efi_status_t EFIAPI efi_get_time_boottime(
285 struct efi_time *time,
286 struct efi_time_cap *capabilities)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100287{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200288#ifdef CONFIG_EFI_GET_TIME
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200289 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200290 struct rtc_time tm;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100291 struct udevice *dev;
292
293 EFI_ENTRY("%p %p", time, capabilities);
294
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200295 if (!time) {
296 ret = EFI_INVALID_PARAMETER;
297 goto out;
298 }
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200299 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
300 dm_rtc_get(dev, &tm)) {
301 ret = EFI_UNSUPPORTED;
302 goto out;
303 }
304 if (dm_rtc_get(dev, &tm)) {
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200305 ret = EFI_DEVICE_ERROR;
306 goto out;
307 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100308
309 memset(time, 0, sizeof(*time));
310 time->year = tm.tm_year;
311 time->month = tm.tm_mon;
312 time->day = tm.tm_mday;
313 time->hour = tm.tm_hour;
314 time->minute = tm.tm_min;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200315 time->second = tm.tm_sec;
Heinrich Schuchardt6846c572020-10-23 05:30:29 +0200316 if (tm.tm_isdst > 0)
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200317 time->daylight =
318 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
Heinrich Schuchardt6846c572020-10-23 05:30:29 +0200319 else if (!tm.tm_isdst)
320 time->daylight = EFI_TIME_ADJUST_DAYLIGHT;
321 else
322 time->daylight = 0;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200323 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100324
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200325 if (capabilities) {
326 /* Set reasonable dummy values */
327 capabilities->resolution = 1; /* 1 Hz */
328 capabilities->accuracy = 100000000; /* 100 ppm */
329 capabilities->sets_to_zero = false;
330 }
331out:
332 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100333#else
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200334 EFI_ENTRY("%p %p", time, capabilities);
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200335 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100336#endif
337}
338
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200339#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200340
341/**
342 * efi_validate_time() - checks if timestamp is valid
343 *
344 * @time: timestamp to validate
345 * Returns: 0 if timestamp is valid, 1 otherwise
346 */
347static int efi_validate_time(struct efi_time *time)
348{
349 return (!time ||
350 time->year < 1900 || time->year > 9999 ||
351 !time->month || time->month > 12 || !time->day ||
352 time->day > rtc_month_days(time->month - 1, time->year) ||
353 time->hour > 23 || time->minute > 59 || time->second > 59 ||
354 time->nanosecond > 999999999 ||
355 time->daylight &
356 ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
357 ((time->timezone < -1440 || time->timezone > 1440) &&
358 time->timezone != EFI_UNSPECIFIED_TIMEZONE));
359}
360
361#endif
362
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200363/**
364 * efi_set_time_boottime() - set current time
365 *
366 * This function implements the SetTime() runtime service before
367 * SetVirtualAddressMap() is called.
368 *
369 * See the Unified Extensible Firmware Interface (UEFI) specification
370 * for details.
371 *
372 * @time: pointer to structure to with current time
373 * Returns: status code
374 */
375static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
376{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200377#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200378 efi_status_t ret = EFI_SUCCESS;
379 struct rtc_time tm;
380 struct udevice *dev;
Alexander Graf77256092016-08-16 21:08:45 +0200381
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200382 EFI_ENTRY("%p", time);
383
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200384 if (efi_validate_time(time)) {
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200385 ret = EFI_INVALID_PARAMETER;
386 goto out;
387 }
388
389 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
390 ret = EFI_UNSUPPORTED;
391 goto out;
392 }
393
394 memset(&tm, 0, sizeof(tm));
395 tm.tm_year = time->year;
396 tm.tm_mon = time->month;
397 tm.tm_mday = time->day;
398 tm.tm_hour = time->hour;
399 tm.tm_min = time->minute;
400 tm.tm_sec = time->second;
Heinrich Schuchardt6846c572020-10-23 05:30:29 +0200401 switch (time->daylight) {
402 case EFI_TIME_ADJUST_DAYLIGHT:
403 tm.tm_isdst = 0;
404 break;
405 case EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT:
406 tm.tm_isdst = 1;
407 break;
408 default:
409 tm.tm_isdst = -1;
410 break;
411 }
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200412 /* Calculate day of week */
413 rtc_calc_weekday(&tm);
414
415 if (dm_rtc_set(dev, &tm))
416 ret = EFI_DEVICE_ERROR;
417out:
418 return EFI_EXIT(ret);
419#else
420 EFI_ENTRY("%p", time);
421 return EFI_EXIT(EFI_UNSUPPORTED);
422#endif
423}
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200424/**
425 * efi_reset_system() - reset system
426 *
427 * This function implements the ResetSystem() runtime service after
Heinrich Schuchardt7b66fcc2020-08-22 08:29:53 +0200428 * SetVirtualAddressMap() is called. As this placeholder cannot reset the
429 * system it simply return to the caller.
430 *
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200431 * Boards may override the helpers below to implement reset functionality.
432 *
433 * See the Unified Extensible Firmware Interface (UEFI) specification for
434 * details.
435 *
436 * @reset_type: type of reset to perform
437 * @reset_status: status code for the reset
438 * @data_size: size of reset_data
439 * @reset_data: information about the reset
440 */
Alexander Graf393dd912016-10-14 13:45:30 +0200441void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200442 enum efi_reset_type reset_type,
443 efi_status_t reset_status,
444 unsigned long data_size, void *reset_data)
445{
Heinrich Schuchardt7b66fcc2020-08-22 08:29:53 +0200446 return;
Alexander Graf77256092016-08-16 21:08:45 +0200447}
448
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200449/**
450 * efi_reset_system_init() - initialize the reset driver
451 *
452 * Boards may override this function to initialize the reset driver.
453 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100454efi_status_t __weak efi_reset_system_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200455{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100456 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200457}
458
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200459/**
460 * efi_get_time() - get current time
461 *
462 * This function implements the GetTime runtime service after
463 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
464 * anymore only an error code is returned.
465 *
466 * See the Unified Extensible Firmware Interface (UEFI) specification
467 * for details.
468 *
469 * @time: pointer to structure to receive current time
470 * @capabilities: pointer to structure to receive RTC properties
471 * Returns: status code
472 */
Alexander Graf393dd912016-10-14 13:45:30 +0200473efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200474 struct efi_time *time,
475 struct efi_time_cap *capabilities)
476{
Heinrich Schuchardtb4ba5be2019-06-13 18:42:40 +0200477 return EFI_UNSUPPORTED;
Alexander Graf77256092016-08-16 21:08:45 +0200478}
479
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200480/**
481 * efi_set_time() - set current time
482 *
483 * This function implements the SetTime runtime service after
484 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
485 * anymore only an error code is returned.
486 *
487 * See the Unified Extensible Firmware Interface (UEFI) specification
488 * for details.
489 *
490 * @time: pointer to structure to with current time
491 * Returns: status code
492 */
493efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
494{
495 return EFI_UNSUPPORTED;
496}
497
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000498/**
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900499 * efi_update_capsule_unsupported() - process information from operating system
500 *
501 * This function implements the UpdateCapsule() runtime service.
502 *
503 * See the Unified Extensible Firmware Interface (UEFI) specification for
504 * details.
505 *
506 * @capsule_header_array: pointer to array of virtual pointers
507 * @capsule_count: number of pointers in capsule_header_array
508 * @scatter_gather_list: pointer to array of physical pointers
509 * Returns: status code
510 */
Heinrich Schuchardt6ab94032023-02-10 08:56:06 +0100511static efi_status_t __efi_runtime EFIAPI efi_update_capsule_unsupported(
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900512 struct efi_capsule_header **capsule_header_array,
513 efi_uintn_t capsule_count,
514 u64 scatter_gather_list)
515{
516 return EFI_UNSUPPORTED;
517}
518
519/**
520 * efi_query_capsule_caps_unsupported() - check if capsule is supported
521 *
522 * This function implements the QueryCapsuleCapabilities() runtime service.
523 *
524 * See the Unified Extensible Firmware Interface (UEFI) specification for
525 * details.
526 *
527 * @capsule_header_array: pointer to array of virtual pointers
528 * @capsule_count: number of pointers in capsule_header_array
529 * @maximum_capsule_size: maximum capsule size
530 * @reset_type: type of reset needed for capsule update
531 * Returns: status code
532 */
Heinrich Schuchardt6ab94032023-02-10 08:56:06 +0100533static efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps_unsupported(
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900534 struct efi_capsule_header **capsule_header_array,
535 efi_uintn_t capsule_count,
536 u64 *maximum_capsule_size,
537 u32 *reset_type)
538{
539 return EFI_UNSUPPORTED;
540}
541
542/**
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000543 * efi_is_runtime_service_pointer() - check if pointer points to runtime table
544 *
545 * @p: pointer to check
546 * Return: true if the pointer points to a service function pointer in the
547 * runtime table
548 */
549static bool efi_is_runtime_service_pointer(void *p)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100550{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200551 return (p >= (void *)&efi_runtime_services.get_time &&
552 p <= (void *)&efi_runtime_services.query_variable_info) ||
553 p == (void *)&efi_events.prev ||
554 p == (void *)&efi_events.next;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100555}
556
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200557/**
558 * efi_runtime_detach() - detach unimplemented runtime functions
559 */
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +0200560void efi_runtime_detach(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100561{
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200562 efi_runtime_services.reset_system = efi_reset_system;
563 efi_runtime_services.get_time = efi_get_time;
564 efi_runtime_services.set_time = efi_set_time;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900565 if (IS_ENABLED(CONFIG_EFI_RUNTIME_UPDATE_CAPSULE)) {
566 /* won't support at runtime */
567 efi_runtime_services.update_capsule =
568 efi_update_capsule_unsupported;
569 efi_runtime_services.query_capsule_caps =
570 efi_query_capsule_caps_unsupported;
571 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100572
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200573 /* Update CRC32 */
574 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000575}
576
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200577/**
578 * efi_set_virtual_address_map_runtime() - change from physical to virtual
579 * mapping
580 *
581 * This function implements the SetVirtualAddressMap() runtime service after
582 * it is first called.
583 *
584 * See the Unified Extensible Firmware Interface (UEFI) specification for
585 * details.
586 *
587 * @memory_map_size: size of the virtual map
588 * @descriptor_size: size of an entry in the map
589 * @descriptor_version: version of the map entries
590 * @virtmap: virtual address mapping information
591 * Return: status code EFI_UNSUPPORTED
592 */
Heinrich Schuchardt2b6bd382019-07-12 22:41:43 +0200593static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map_runtime(
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200594 efi_uintn_t memory_map_size,
595 efi_uintn_t descriptor_size,
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200596 uint32_t descriptor_version,
597 struct efi_mem_desc *virtmap)
598{
599 return EFI_UNSUPPORTED;
600}
601
602/**
603 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
604 *
605 * This function implements the ConvertPointer() runtime service after
606 * the first call to SetVirtualAddressMap().
607 *
608 * See the Unified Extensible Firmware Interface (UEFI) specification for
609 * details.
610 *
611 * @debug_disposition: indicates if pointer may be converted to NULL
612 * @address: pointer to be converted
613 * Return: status code EFI_UNSUPPORTED
614 */
615static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime(
616 efi_uintn_t debug_disposition, void **address)
617{
618 return EFI_UNSUPPORTED;
619}
620
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200621/**
Heinrich Schuchardt582f8572020-03-22 08:28:15 +0100622 * efi_convert_pointer() - convert from physical to virtual pointer
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200623 *
624 * This function implements the ConvertPointer() runtime service until
625 * the first call to SetVirtualAddressMap().
626 *
627 * See the Unified Extensible Firmware Interface (UEFI) specification for
628 * details.
629 *
630 * @debug_disposition: indicates if pointer may be converted to NULL
631 * @address: pointer to be converted
Heinrich Schuchardt582f8572020-03-22 08:28:15 +0100632 * Return: status code
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200633 */
Heinrich Schuchardt699b7c62020-03-24 18:05:22 +0100634__efi_runtime efi_status_t EFIAPI
635efi_convert_pointer(efi_uintn_t debug_disposition, void **address)
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200636{
Heinrich Schuchardtb0818b72020-07-07 03:10:12 +0200637 efi_physical_addr_t addr;
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200638 efi_uintn_t i;
639 efi_status_t ret = EFI_NOT_FOUND;
640
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200641 if (!efi_virtmap) {
642 ret = EFI_UNSUPPORTED;
643 goto out;
644 }
645
646 if (!address) {
647 ret = EFI_INVALID_PARAMETER;
648 goto out;
649 }
Heinrich Schuchardtdabe54a2020-03-24 17:52:40 +0100650 if (!*address) {
651 if (debug_disposition & EFI_OPTIONAL_PTR)
652 return EFI_SUCCESS;
653 else
654 return EFI_INVALID_PARAMETER;
655 }
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200656
Heinrich Schuchardtb0818b72020-07-07 03:10:12 +0200657 addr = (uintptr_t)*address;
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200658 for (i = 0; i < efi_descriptor_count; i++) {
659 struct efi_mem_desc *map = (void *)efi_virtmap +
660 (efi_descriptor_size * i);
661
662 if (addr >= map->physical_start &&
663 (addr < map->physical_start
664 + (map->num_pages << EFI_PAGE_SHIFT))) {
665 *address = (void *)(uintptr_t)
666 (addr + map->virtual_start -
667 map->physical_start);
668
669 ret = EFI_SUCCESS;
670 break;
671 }
672 }
673
674out:
Heinrich Schuchardt699b7c62020-03-24 18:05:22 +0100675 return ret;
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200676}
677
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000678static __efi_runtime void efi_relocate_runtime_table(ulong offset)
679{
680 ulong patchoff;
681 void **pos;
682
683 /* Relocate the runtime services pointers */
684 patchoff = offset - gd->relocaddr;
685 for (pos = (void **)&efi_runtime_services.get_time;
686 pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200687 if (*pos)
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000688 *pos += patchoff;
689 }
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200690
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200691 /*
692 * The entry for SetVirtualAddress() must point to a physical address.
693 * After the first execution the service must return EFI_UNSUPPORTED.
694 */
695 efi_runtime_services.set_virtual_address_map =
696 &efi_set_virtual_address_map_runtime;
697
698 /*
699 * The entry for ConvertPointer() must point to a physical address.
700 * The service is not usable after SetVirtualAddress().
701 */
702 efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime;
703
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200704 /*
705 * TODO: Update UEFI variable RuntimeServicesSupported removing flags
706 * EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP and
707 * EFI_RT_SUPPORTED_CONVERT_POINTER as required by the UEFI spec 2.8.
708 */
709
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200710 /* Update CRC32 */
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200711 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100712}
713
714/* Relocate EFI runtime to uboot_reloc_base = offset */
715void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
716{
717#ifdef IS_RELA
Ilias Apalodimas9bad9cd2024-04-04 09:37:37 +0300718 struct elf_rela *rel = (void *)__efi_runtime_rel_start;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100719#else
Ilias Apalodimas9bad9cd2024-04-04 09:37:37 +0300720 struct elf_rel *rel = (void *)__efi_runtime_rel_start;
Simon Glass72cc5382022-10-20 18:22:39 -0600721 static ulong lastoff = CONFIG_TEXT_BASE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100722#endif
723
Alexander Graf62a67482016-06-02 11:38:27 +0200724 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Ilias Apalodimas9bad9cd2024-04-04 09:37:37 +0300725 for (; (uintptr_t)rel < (uintptr_t)__efi_runtime_rel_stop; rel++) {
Simon Glass72cc5382022-10-20 18:22:39 -0600726 ulong base = CONFIG_TEXT_BASE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100727 ulong *p;
728 ulong newaddr;
729
730 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
731
Heinrich Schuchardt33691582019-08-14 06:49:09 +0200732 /*
733 * The runtime services table is updated in
734 * efi_relocate_runtime_table()
735 */
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000736 if (map && efi_is_runtime_service_pointer(p))
737 continue;
738
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700739 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
740 rel->info, *p, rel->offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100741
Rick Chen9677a372018-05-28 19:06:37 +0800742 switch (rel->info & R_MASK) {
743 case R_RELATIVE:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100744#ifdef IS_RELA
Simon Glass72cc5382022-10-20 18:22:39 -0600745 newaddr = rel->addend + offset - CONFIG_TEXT_BASE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100746#else
747 newaddr = *p - lastoff + offset;
748#endif
Rick Chen9677a372018-05-28 19:06:37 +0800749 break;
750#ifdef R_ABSOLUTE
751 case R_ABSOLUTE: {
752 ulong symidx = rel->info >> SYM_INDEX;
753 extern struct dyn_sym __dyn_sym_start[];
754 newaddr = __dyn_sym_start[symidx].addr + offset;
Alexander Grafdf0f6c72018-11-04 22:25:22 +0100755#ifdef IS_RELA
Simon Glass72cc5382022-10-20 18:22:39 -0600756 newaddr -= CONFIG_TEXT_BASE;
Alexander Grafdf0f6c72018-11-04 22:25:22 +0100757#endif
Rick Chen9677a372018-05-28 19:06:37 +0800758 break;
759 }
760#endif
761 default:
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000762 printf("%s: Unknown relocation type %llx\n",
763 __func__, rel->info & R_MASK);
Rick Chen9677a372018-05-28 19:06:37 +0800764 continue;
765 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100766
767 /* Check if the relocation is inside bounds */
768 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200769 newaddr > (map->virtual_start +
770 (map->num_pages << EFI_PAGE_SHIFT)))) {
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000771 printf("%s: Relocation at %p is out of range (%lx)\n",
772 __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100773 continue;
774 }
775
Alexander Graf62a67482016-06-02 11:38:27 +0200776 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100777 *p = newaddr;
Alexander Grafe5702322016-04-11 23:20:39 +0200778 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
779 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf0bd425a2016-03-04 01:10:01 +0100780 }
781
782#ifndef IS_RELA
783 lastoff = offset;
784#endif
785
Heinrich Schuchardt3a67d132024-06-16 19:31:05 +0200786 /*
787 * If on x86 a write affects a prefetched instruction,
788 * the prefetch queue is invalidated.
789 */
790 if (!CONFIG_IS_ENABLED(X86))
791 invalidate_icache_all();
Alexander Graf0bd425a2016-03-04 01:10:01 +0100792}
793
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200794/**
795 * efi_set_virtual_address_map() - change from physical to virtual mapping
796 *
797 * This function implements the SetVirtualAddressMap() runtime service.
798 *
799 * See the Unified Extensible Firmware Interface (UEFI) specification for
800 * details.
801 *
802 * @memory_map_size: size of the virtual map
803 * @descriptor_size: size of an entry in the map
804 * @descriptor_version: version of the map entries
805 * @virtmap: virtual address mapping information
806 * Return: status code
807 */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100808static efi_status_t EFIAPI efi_set_virtual_address_map(
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200809 efi_uintn_t memory_map_size,
810 efi_uintn_t descriptor_size,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100811 uint32_t descriptor_version,
812 struct efi_mem_desc *virtmap)
813{
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200814 efi_uintn_t n = memory_map_size / descriptor_size;
815 efi_uintn_t i;
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200816 efi_status_t ret = EFI_INVALID_PARAMETER;
Alexander Graf86e00212018-12-11 10:00:42 +0100817 int rt_code_sections = 0;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200818 struct efi_event *event;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100819
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200820 EFI_ENTRY("%zx %zx %x %p", memory_map_size, descriptor_size,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100821 descriptor_version, virtmap);
822
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200823 if (descriptor_version != EFI_MEMORY_DESCRIPTOR_VERSION ||
824 descriptor_size < sizeof(struct efi_mem_desc))
825 goto out;
826
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200827 efi_virtmap = virtmap;
828 efi_descriptor_size = descriptor_size;
829 efi_descriptor_count = n;
830
Alexander Graf86e00212018-12-11 10:00:42 +0100831 /*
832 * TODO:
833 * Further down we are cheating. While really we should implement
834 * SetVirtualAddressMap() events and ConvertPointer() to allow
835 * dynamically loaded drivers to expose runtime services, we don't
836 * today.
837 *
838 * So let's ensure we see exactly one single runtime section, as
839 * that is the built-in one. If we see more (or less), someone must
840 * have tried adding or removing to that which we don't support yet.
841 * In that case, let's better fail rather than expose broken runtime
842 * services.
843 */
844 for (i = 0; i < n; i++) {
845 struct efi_mem_desc *map = (void*)virtmap +
846 (descriptor_size * i);
847
848 if (map->type == EFI_RUNTIME_SERVICES_CODE)
849 rt_code_sections++;
850 }
851
852 if (rt_code_sections != 1) {
853 /*
854 * We expose exactly one single runtime code section, so
855 * something is definitely going wrong.
856 */
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200857 goto out;
Alexander Graf86e00212018-12-11 10:00:42 +0100858 }
859
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200860 /* Notify EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
861 list_for_each_entry(event, &efi_events, link) {
862 if (event->notify_function)
863 EFI_CALL_VOID(event->notify_function(
864 event, event->notify_context));
865 }
866
Alexander Graf77256092016-08-16 21:08:45 +0200867 /* Rebind mmio pointers */
868 for (i = 0; i < n; i++) {
869 struct efi_mem_desc *map = (void*)virtmap +
870 (descriptor_size * i);
871 struct list_head *lhandle;
872 efi_physical_addr_t map_start = map->physical_start;
873 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
874 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200875 u64 off = map->virtual_start - map_start;
Alexander Graf77256092016-08-16 21:08:45 +0200876
877 /* Adjust all mmio pointers in this region */
878 list_for_each(lhandle, &efi_runtime_mmio) {
879 struct efi_runtime_mmio_list *lmmio;
880
881 lmmio = list_entry(lhandle,
882 struct efi_runtime_mmio_list,
883 link);
884 if ((map_start <= lmmio->paddr) &&
885 (map_end >= lmmio->paddr)) {
Alexander Graf77256092016-08-16 21:08:45 +0200886 uintptr_t new_addr = lmmio->paddr + off;
887 *lmmio->ptr = (void *)new_addr;
888 }
889 }
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200890 if ((map_start <= (uintptr_t)systab.tables) &&
891 (map_end >= (uintptr_t)systab.tables)) {
892 char *ptr = (char *)systab.tables;
893
894 ptr += off;
895 systab.tables = (struct efi_configuration_table *)ptr;
896 }
Alexander Graf77256092016-08-16 21:08:45 +0200897 }
898
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000899 /* Relocate the runtime. See TODO above */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100900 for (i = 0; i < n; i++) {
901 struct efi_mem_desc *map;
902
903 map = (void*)virtmap + (descriptor_size * i);
904 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf77256092016-08-16 21:08:45 +0200905 ulong new_offset = map->virtual_start -
Alexander Graf86e00212018-12-11 10:00:42 +0100906 map->physical_start + gd->relocaddr;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100907
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000908 efi_relocate_runtime_table(new_offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100909 efi_runtime_relocate(new_offset, map);
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200910 ret = EFI_SUCCESS;
911 goto out;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100912 }
913 }
914
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200915out:
916 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100917}
918
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200919/**
920 * efi_add_runtime_mmio() - add memory-mapped IO region
921 *
922 * This function adds a memory-mapped IO region to the memory map to make it
923 * available at runtime.
924 *
Heinrich Schuchardt7e307642018-12-23 02:35:13 +0100925 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
926 * IO region
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200927 * @len: size of the memory-mapped IO region
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200928 * Returns: status code
929 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100930efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf77256092016-08-16 21:08:45 +0200931{
932 struct efi_runtime_mmio_list *newmmio;
Alexander Graf33a83a32018-03-15 15:08:16 +0100933 uint64_t addr = *(uintptr_t *)mmio_ptr;
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100934 efi_status_t ret;
Alexander Graf33a83a32018-03-15 15:08:16 +0100935
Michael Walle282d3862020-05-17 12:29:19 +0200936 ret = efi_add_memory_map(addr, len, EFI_MMAP_IO);
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100937 if (ret != EFI_SUCCESS)
Alexander Graf33a83a32018-03-15 15:08:16 +0100938 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200939
940 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100941 if (!newmmio)
942 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200943 newmmio->ptr = mmio_ptr;
944 newmmio->paddr = *(uintptr_t *)mmio_ptr;
945 newmmio->len = len;
946 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100947
Alexander Graf33a83a32018-03-15 15:08:16 +0100948 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200949}
950
Alexander Graf0bd425a2016-03-04 01:10:01 +0100951/*
952 * In the second stage, U-Boot has disappeared. To isolate our runtime code
953 * that at this point still exists from the rest, we put it into a special
954 * section.
955 *
956 * !!WARNING!!
957 *
958 * This means that we can not rely on any code outside of this file in any
959 * function or variable below this line.
960 *
961 * Please keep everything fully self-contained and annotated with
Alexander Graf393dd912016-10-14 13:45:30 +0200962 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf0bd425a2016-03-04 01:10:01 +0100963 */
964
965/*
966 * Relocate the EFI runtime stub to a different place. We need to call this
967 * the first time we expose the runtime interface to a user and on set virtual
968 * address map calls.
969 */
970
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200971/**
972 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
973 *
974 * This function is used after SetVirtualAddressMap() is called as replacement
975 * for services that are not available anymore due to constraints of the U-Boot
976 * implementation.
977 *
978 * Return: EFI_UNSUPPORTED
979 */
Alexander Graf393dd912016-10-14 13:45:30 +0200980static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100981{
982 return EFI_UNSUPPORTED;
983}
984
Alexander Graf393dd912016-10-14 13:45:30 +0200985struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100986 .hdr = {
987 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +0200988 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +0200989 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf0bd425a2016-03-04 01:10:01 +0100990 },
Alexander Graf77256092016-08-16 21:08:45 +0200991 .get_time = &efi_get_time_boottime,
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200992 .set_time = &efi_set_time_boottime,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100993 .get_wakeup_time = (void *)&efi_unimplemented,
994 .set_wakeup_time = (void *)&efi_unimplemented,
995 .set_virtual_address_map = &efi_set_virtual_address_map,
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200996 .convert_pointer = efi_convert_pointer,
Rob Clark15f3d742017-09-13 18:05:37 -0400997 .get_variable = efi_get_variable,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200998 .get_next_variable_name = efi_get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400999 .set_variable = efi_set_variable,
Heinrich Schuchardt5337a6c2019-06-20 15:40:49 +02001000 .get_next_high_mono_count = (void *)&efi_unimplemented,
Alexander Graf77256092016-08-16 21:08:45 +02001001 .reset_system = &efi_reset_system_boottime,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +09001002#ifdef CONFIG_EFI_RUNTIME_UPDATE_CAPSULE
Heinrich Schuchardt897c0722018-02-09 20:41:21 +01001003 .update_capsule = efi_update_capsule,
1004 .query_capsule_caps = efi_query_capsule_caps,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +09001005#else
1006 .update_capsule = efi_update_capsule_unsupported,
1007 .query_capsule_caps = efi_query_capsule_caps_unsupported,
1008#endif
Heinrich Schuchardt897c0722018-02-09 20:41:21 +01001009 .query_variable_info = efi_query_variable_info,
Alexander Graf0bd425a2016-03-04 01:10:01 +01001010};