blob: 73831c527e00929d46348a9c44524604a4439339 [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 Apalodimas465cb7e2024-04-18 15:54:51 +0300132 if (IS_ENABLED(CONFIG_EFI_RT_VOLATILE_STORE)) {
Ilias Apalodimasde708562024-04-18 15:54:52 +0300133 u8 s = 0;
134
Ilias Apalodimas465cb7e2024-04-18 15:54:51 +0300135 ret = efi_set_variable_int(u"RTStorageVolatile",
136 &efi_guid_efi_rt_var_file,
137 EFI_VARIABLE_BOOTSERVICE_ACCESS |
138 EFI_VARIABLE_RUNTIME_ACCESS |
139 EFI_VARIABLE_READ_ONLY,
140 sizeof(EFI_VAR_FILE_NAME),
141 EFI_VAR_FILE_NAME, false);
142 if (ret != EFI_SUCCESS) {
143 log_err("Failed to set RTStorageVolatile\n");
144 return ret;
145 }
Ilias Apalodimasde708562024-04-18 15:54:52 +0300146 /*
147 * This variable needs to be visible so users can read it,
148 * but the real contents are going to be filled during
149 * GetVariable
150 */
151 ret = efi_set_variable_int(u"VarToFile",
152 &efi_guid_efi_rt_var_file,
153 EFI_VARIABLE_BOOTSERVICE_ACCESS |
154 EFI_VARIABLE_RUNTIME_ACCESS |
155 EFI_VARIABLE_READ_ONLY,
156 sizeof(s),
157 &s, false);
158 if (ret != EFI_SUCCESS) {
159 log_err("Failed to set VarToFile\n");
160 efi_set_variable_int(u"RTStorageVolatile",
161 &efi_guid_efi_rt_var_file,
162 EFI_VARIABLE_BOOTSERVICE_ACCESS |
163 EFI_VARIABLE_RUNTIME_ACCESS |
164 EFI_VARIABLE_READ_ONLY,
165 0, NULL, false);
166
167 return ret;
168 }
Ilias Apalodimas465cb7e2024-04-18 15:54:51 +0300169 rt_table->runtime_services_supported |= EFI_RT_SUPPORTED_SET_VARIABLE;
170 }
Ilias Apalodimas86ba8692024-04-18 15:54:50 +0300171
AKASHI Takahiro32401842019-06-05 13:21:38 +0900172 /*
173 * This value must be synced with efi_runtime_detach_list
174 * as well as efi_runtime_services.
175 */
Heinrich Schuchardt05874fb2019-07-05 18:12:16 +0200176#ifdef CONFIG_EFI_HAVE_RUNTIME_RESET
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100177 rt_table->runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM;
AKASHI Takahiro32401842019-06-05 13:21:38 +0900178#endif
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200179
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100180 ret = efi_install_configuration_table(&efi_rt_properties_table_guid,
181 rt_table);
182 return ret;
AKASHI Takahiro32401842019-06-05 13:21:38 +0900183}
184
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200185/**
Heinrich Schuchardt8cd99752020-06-28 16:30:29 +0200186 * efi_memcpy_runtime() - copy memory area
187 *
188 * At runtime memcpy() is not available.
189 *
Heinrich Schuchardt447753e2020-07-22 07:56:14 +0200190 * Overlapping memory areas can be copied safely if src >= dest.
191 *
Heinrich Schuchardt8cd99752020-06-28 16:30:29 +0200192 * @dest: destination buffer
193 * @src: source buffer
194 * @n: number of bytes to copy
195 * Return: pointer to destination buffer
196 */
197void __efi_runtime efi_memcpy_runtime(void *dest, const void *src, size_t n)
198{
199 u8 *d = dest;
200 const u8 *s = src;
201
202 for (; n; --n)
203 *d++ = *s++;
204}
205
206/**
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200207 * efi_update_table_header_crc32() - Update crc32 in table header
208 *
209 * @table: EFI table
210 */
211void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
212{
213 table->crc32 = 0;
214 table->crc32 = crc32(0, (const unsigned char *)table,
215 table->headersize);
216}
217
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200218/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200219 * efi_reset_system_boottime() - reset system at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200220 *
221 * This function implements the ResetSystem() runtime service before
222 * SetVirtualAddressMap() is called.
223 *
224 * See the Unified Extensible Firmware Interface (UEFI) specification for
225 * details.
226 *
227 * @reset_type: type of reset to perform
228 * @reset_status: status code for the reset
229 * @data_size: size of reset_data
230 * @reset_data: information about the reset
231 */
Alexander Graf77256092016-08-16 21:08:45 +0200232static void EFIAPI efi_reset_system_boottime(
233 enum efi_reset_type reset_type,
234 efi_status_t reset_status,
235 unsigned long data_size, void *reset_data)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100236{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100237 struct efi_event *evt;
238
Alexander Graf0bd425a2016-03-04 01:10:01 +0100239 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
240 reset_data);
241
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100242 /* Notify reset */
243 list_for_each_entry(evt, &efi_events, link) {
244 if (evt->group &&
245 !guidcmp(evt->group,
246 &efi_guid_event_group_reset_system)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200247 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100248 break;
249 }
250 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100251 switch (reset_type) {
252 case EFI_RESET_COLD:
253 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +0100254 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100255 do_reset(NULL, 0, 0, NULL);
256 break;
257 case EFI_RESET_SHUTDOWN:
Heinrich Schuchardt44489452018-10-16 07:44:53 +0200258#ifdef CONFIG_CMD_POWEROFF
259 do_poweroff(NULL, 0, 0, NULL);
260#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +0100261 break;
262 }
263
Alexander Graf77256092016-08-16 21:08:45 +0200264 while (1) { }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100265}
266
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200267/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200268 * efi_get_time_boottime() - get current time at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200269 *
270 * This function implements the GetTime runtime service before
271 * SetVirtualAddressMap() is called.
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200272 *
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200273 * See the Unified Extensible Firmware Interface (UEFI) specification
274 * for details.
275 *
276 * @time: pointer to structure to receive current time
277 * @capabilities: pointer to structure to receive RTC properties
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200278 * Returns: status code
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200279 */
Alexander Graf77256092016-08-16 21:08:45 +0200280static efi_status_t EFIAPI efi_get_time_boottime(
281 struct efi_time *time,
282 struct efi_time_cap *capabilities)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100283{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200284#ifdef CONFIG_EFI_GET_TIME
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200285 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200286 struct rtc_time tm;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100287 struct udevice *dev;
288
289 EFI_ENTRY("%p %p", time, capabilities);
290
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200291 if (!time) {
292 ret = EFI_INVALID_PARAMETER;
293 goto out;
294 }
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200295 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
296 dm_rtc_get(dev, &tm)) {
297 ret = EFI_UNSUPPORTED;
298 goto out;
299 }
300 if (dm_rtc_get(dev, &tm)) {
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200301 ret = EFI_DEVICE_ERROR;
302 goto out;
303 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100304
305 memset(time, 0, sizeof(*time));
306 time->year = tm.tm_year;
307 time->month = tm.tm_mon;
308 time->day = tm.tm_mday;
309 time->hour = tm.tm_hour;
310 time->minute = tm.tm_min;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200311 time->second = tm.tm_sec;
Heinrich Schuchardt6846c572020-10-23 05:30:29 +0200312 if (tm.tm_isdst > 0)
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200313 time->daylight =
314 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
Heinrich Schuchardt6846c572020-10-23 05:30:29 +0200315 else if (!tm.tm_isdst)
316 time->daylight = EFI_TIME_ADJUST_DAYLIGHT;
317 else
318 time->daylight = 0;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200319 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100320
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200321 if (capabilities) {
322 /* Set reasonable dummy values */
323 capabilities->resolution = 1; /* 1 Hz */
324 capabilities->accuracy = 100000000; /* 100 ppm */
325 capabilities->sets_to_zero = false;
326 }
327out:
328 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100329#else
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200330 EFI_ENTRY("%p %p", time, capabilities);
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200331 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100332#endif
333}
334
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200335#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200336
337/**
338 * efi_validate_time() - checks if timestamp is valid
339 *
340 * @time: timestamp to validate
341 * Returns: 0 if timestamp is valid, 1 otherwise
342 */
343static int efi_validate_time(struct efi_time *time)
344{
345 return (!time ||
346 time->year < 1900 || time->year > 9999 ||
347 !time->month || time->month > 12 || !time->day ||
348 time->day > rtc_month_days(time->month - 1, time->year) ||
349 time->hour > 23 || time->minute > 59 || time->second > 59 ||
350 time->nanosecond > 999999999 ||
351 time->daylight &
352 ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
353 ((time->timezone < -1440 || time->timezone > 1440) &&
354 time->timezone != EFI_UNSPECIFIED_TIMEZONE));
355}
356
357#endif
358
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200359/**
360 * efi_set_time_boottime() - set current time
361 *
362 * This function implements the SetTime() runtime service before
363 * SetVirtualAddressMap() is called.
364 *
365 * See the Unified Extensible Firmware Interface (UEFI) specification
366 * for details.
367 *
368 * @time: pointer to structure to with current time
369 * Returns: status code
370 */
371static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
372{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200373#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200374 efi_status_t ret = EFI_SUCCESS;
375 struct rtc_time tm;
376 struct udevice *dev;
Alexander Graf77256092016-08-16 21:08:45 +0200377
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200378 EFI_ENTRY("%p", time);
379
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200380 if (efi_validate_time(time)) {
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200381 ret = EFI_INVALID_PARAMETER;
382 goto out;
383 }
384
385 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
386 ret = EFI_UNSUPPORTED;
387 goto out;
388 }
389
390 memset(&tm, 0, sizeof(tm));
391 tm.tm_year = time->year;
392 tm.tm_mon = time->month;
393 tm.tm_mday = time->day;
394 tm.tm_hour = time->hour;
395 tm.tm_min = time->minute;
396 tm.tm_sec = time->second;
Heinrich Schuchardt6846c572020-10-23 05:30:29 +0200397 switch (time->daylight) {
398 case EFI_TIME_ADJUST_DAYLIGHT:
399 tm.tm_isdst = 0;
400 break;
401 case EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT:
402 tm.tm_isdst = 1;
403 break;
404 default:
405 tm.tm_isdst = -1;
406 break;
407 }
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200408 /* Calculate day of week */
409 rtc_calc_weekday(&tm);
410
411 if (dm_rtc_set(dev, &tm))
412 ret = EFI_DEVICE_ERROR;
413out:
414 return EFI_EXIT(ret);
415#else
416 EFI_ENTRY("%p", time);
417 return EFI_EXIT(EFI_UNSUPPORTED);
418#endif
419}
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200420/**
421 * efi_reset_system() - reset system
422 *
423 * This function implements the ResetSystem() runtime service after
Heinrich Schuchardt7b66fcc2020-08-22 08:29:53 +0200424 * SetVirtualAddressMap() is called. As this placeholder cannot reset the
425 * system it simply return to the caller.
426 *
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200427 * Boards may override the helpers below to implement reset functionality.
428 *
429 * See the Unified Extensible Firmware Interface (UEFI) specification for
430 * details.
431 *
432 * @reset_type: type of reset to perform
433 * @reset_status: status code for the reset
434 * @data_size: size of reset_data
435 * @reset_data: information about the reset
436 */
Alexander Graf393dd912016-10-14 13:45:30 +0200437void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200438 enum efi_reset_type reset_type,
439 efi_status_t reset_status,
440 unsigned long data_size, void *reset_data)
441{
Heinrich Schuchardt7b66fcc2020-08-22 08:29:53 +0200442 return;
Alexander Graf77256092016-08-16 21:08:45 +0200443}
444
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200445/**
446 * efi_reset_system_init() - initialize the reset driver
447 *
448 * Boards may override this function to initialize the reset driver.
449 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100450efi_status_t __weak efi_reset_system_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200451{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100452 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200453}
454
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200455/**
456 * efi_get_time() - get current time
457 *
458 * This function implements the GetTime runtime service after
459 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
460 * anymore only an error code is returned.
461 *
462 * See the Unified Extensible Firmware Interface (UEFI) specification
463 * for details.
464 *
465 * @time: pointer to structure to receive current time
466 * @capabilities: pointer to structure to receive RTC properties
467 * Returns: status code
468 */
Alexander Graf393dd912016-10-14 13:45:30 +0200469efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200470 struct efi_time *time,
471 struct efi_time_cap *capabilities)
472{
Heinrich Schuchardtb4ba5be2019-06-13 18:42:40 +0200473 return EFI_UNSUPPORTED;
Alexander Graf77256092016-08-16 21:08:45 +0200474}
475
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200476/**
477 * efi_set_time() - set current time
478 *
479 * This function implements the SetTime runtime service after
480 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
481 * anymore only an error code is returned.
482 *
483 * See the Unified Extensible Firmware Interface (UEFI) specification
484 * for details.
485 *
486 * @time: pointer to structure to with current time
487 * Returns: status code
488 */
489efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
490{
491 return EFI_UNSUPPORTED;
492}
493
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000494/**
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900495 * efi_update_capsule_unsupported() - process information from operating system
496 *
497 * This function implements the UpdateCapsule() runtime service.
498 *
499 * See the Unified Extensible Firmware Interface (UEFI) specification for
500 * details.
501 *
502 * @capsule_header_array: pointer to array of virtual pointers
503 * @capsule_count: number of pointers in capsule_header_array
504 * @scatter_gather_list: pointer to array of physical pointers
505 * Returns: status code
506 */
Heinrich Schuchardt6ab94032023-02-10 08:56:06 +0100507static efi_status_t __efi_runtime EFIAPI efi_update_capsule_unsupported(
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900508 struct efi_capsule_header **capsule_header_array,
509 efi_uintn_t capsule_count,
510 u64 scatter_gather_list)
511{
512 return EFI_UNSUPPORTED;
513}
514
515/**
516 * efi_query_capsule_caps_unsupported() - check if capsule is supported
517 *
518 * This function implements the QueryCapsuleCapabilities() runtime service.
519 *
520 * See the Unified Extensible Firmware Interface (UEFI) specification for
521 * details.
522 *
523 * @capsule_header_array: pointer to array of virtual pointers
524 * @capsule_count: number of pointers in capsule_header_array
525 * @maximum_capsule_size: maximum capsule size
526 * @reset_type: type of reset needed for capsule update
527 * Returns: status code
528 */
Heinrich Schuchardt6ab94032023-02-10 08:56:06 +0100529static efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps_unsupported(
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900530 struct efi_capsule_header **capsule_header_array,
531 efi_uintn_t capsule_count,
532 u64 *maximum_capsule_size,
533 u32 *reset_type)
534{
535 return EFI_UNSUPPORTED;
536}
537
538/**
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000539 * efi_is_runtime_service_pointer() - check if pointer points to runtime table
540 *
541 * @p: pointer to check
542 * Return: true if the pointer points to a service function pointer in the
543 * runtime table
544 */
545static bool efi_is_runtime_service_pointer(void *p)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100546{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200547 return (p >= (void *)&efi_runtime_services.get_time &&
548 p <= (void *)&efi_runtime_services.query_variable_info) ||
549 p == (void *)&efi_events.prev ||
550 p == (void *)&efi_events.next;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100551}
552
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200553/**
554 * efi_runtime_detach() - detach unimplemented runtime functions
555 */
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +0200556void efi_runtime_detach(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100557{
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200558 efi_runtime_services.reset_system = efi_reset_system;
559 efi_runtime_services.get_time = efi_get_time;
560 efi_runtime_services.set_time = efi_set_time;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900561 if (IS_ENABLED(CONFIG_EFI_RUNTIME_UPDATE_CAPSULE)) {
562 /* won't support at runtime */
563 efi_runtime_services.update_capsule =
564 efi_update_capsule_unsupported;
565 efi_runtime_services.query_capsule_caps =
566 efi_query_capsule_caps_unsupported;
567 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100568
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200569 /* Update CRC32 */
570 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000571}
572
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200573/**
574 * efi_set_virtual_address_map_runtime() - change from physical to virtual
575 * mapping
576 *
577 * This function implements the SetVirtualAddressMap() runtime service after
578 * it is first called.
579 *
580 * See the Unified Extensible Firmware Interface (UEFI) specification for
581 * details.
582 *
583 * @memory_map_size: size of the virtual map
584 * @descriptor_size: size of an entry in the map
585 * @descriptor_version: version of the map entries
586 * @virtmap: virtual address mapping information
587 * Return: status code EFI_UNSUPPORTED
588 */
Heinrich Schuchardt2b6bd382019-07-12 22:41:43 +0200589static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map_runtime(
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200590 efi_uintn_t memory_map_size,
591 efi_uintn_t descriptor_size,
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200592 uint32_t descriptor_version,
593 struct efi_mem_desc *virtmap)
594{
595 return EFI_UNSUPPORTED;
596}
597
598/**
599 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
600 *
601 * This function implements the ConvertPointer() runtime service after
602 * the first call to SetVirtualAddressMap().
603 *
604 * See the Unified Extensible Firmware Interface (UEFI) specification for
605 * details.
606 *
607 * @debug_disposition: indicates if pointer may be converted to NULL
608 * @address: pointer to be converted
609 * Return: status code EFI_UNSUPPORTED
610 */
611static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime(
612 efi_uintn_t debug_disposition, void **address)
613{
614 return EFI_UNSUPPORTED;
615}
616
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200617/**
Heinrich Schuchardt582f8572020-03-22 08:28:15 +0100618 * efi_convert_pointer() - convert from physical to virtual pointer
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200619 *
620 * This function implements the ConvertPointer() runtime service until
621 * the first call to SetVirtualAddressMap().
622 *
623 * See the Unified Extensible Firmware Interface (UEFI) specification for
624 * details.
625 *
626 * @debug_disposition: indicates if pointer may be converted to NULL
627 * @address: pointer to be converted
Heinrich Schuchardt582f8572020-03-22 08:28:15 +0100628 * Return: status code
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200629 */
Heinrich Schuchardt699b7c62020-03-24 18:05:22 +0100630__efi_runtime efi_status_t EFIAPI
631efi_convert_pointer(efi_uintn_t debug_disposition, void **address)
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200632{
Heinrich Schuchardtb0818b72020-07-07 03:10:12 +0200633 efi_physical_addr_t addr;
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200634 efi_uintn_t i;
635 efi_status_t ret = EFI_NOT_FOUND;
636
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200637 if (!efi_virtmap) {
638 ret = EFI_UNSUPPORTED;
639 goto out;
640 }
641
642 if (!address) {
643 ret = EFI_INVALID_PARAMETER;
644 goto out;
645 }
Heinrich Schuchardtdabe54a2020-03-24 17:52:40 +0100646 if (!*address) {
647 if (debug_disposition & EFI_OPTIONAL_PTR)
648 return EFI_SUCCESS;
649 else
650 return EFI_INVALID_PARAMETER;
651 }
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200652
Heinrich Schuchardtb0818b72020-07-07 03:10:12 +0200653 addr = (uintptr_t)*address;
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200654 for (i = 0; i < efi_descriptor_count; i++) {
655 struct efi_mem_desc *map = (void *)efi_virtmap +
656 (efi_descriptor_size * i);
657
658 if (addr >= map->physical_start &&
659 (addr < map->physical_start
660 + (map->num_pages << EFI_PAGE_SHIFT))) {
661 *address = (void *)(uintptr_t)
662 (addr + map->virtual_start -
663 map->physical_start);
664
665 ret = EFI_SUCCESS;
666 break;
667 }
668 }
669
670out:
Heinrich Schuchardt699b7c62020-03-24 18:05:22 +0100671 return ret;
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200672}
673
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000674static __efi_runtime void efi_relocate_runtime_table(ulong offset)
675{
676 ulong patchoff;
677 void **pos;
678
679 /* Relocate the runtime services pointers */
680 patchoff = offset - gd->relocaddr;
681 for (pos = (void **)&efi_runtime_services.get_time;
682 pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200683 if (*pos)
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000684 *pos += patchoff;
685 }
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200686
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200687 /*
688 * The entry for SetVirtualAddress() must point to a physical address.
689 * After the first execution the service must return EFI_UNSUPPORTED.
690 */
691 efi_runtime_services.set_virtual_address_map =
692 &efi_set_virtual_address_map_runtime;
693
694 /*
695 * The entry for ConvertPointer() must point to a physical address.
696 * The service is not usable after SetVirtualAddress().
697 */
698 efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime;
699
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200700 /*
701 * TODO: Update UEFI variable RuntimeServicesSupported removing flags
702 * EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP and
703 * EFI_RT_SUPPORTED_CONVERT_POINTER as required by the UEFI spec 2.8.
704 */
705
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200706 /* Update CRC32 */
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200707 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100708}
709
710/* Relocate EFI runtime to uboot_reloc_base = offset */
711void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
712{
713#ifdef IS_RELA
Ilias Apalodimas9bad9cd2024-04-04 09:37:37 +0300714 struct elf_rela *rel = (void *)__efi_runtime_rel_start;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100715#else
Ilias Apalodimas9bad9cd2024-04-04 09:37:37 +0300716 struct elf_rel *rel = (void *)__efi_runtime_rel_start;
Simon Glass72cc5382022-10-20 18:22:39 -0600717 static ulong lastoff = CONFIG_TEXT_BASE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100718#endif
719
Alexander Graf62a67482016-06-02 11:38:27 +0200720 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Ilias Apalodimas9bad9cd2024-04-04 09:37:37 +0300721 for (; (uintptr_t)rel < (uintptr_t)__efi_runtime_rel_stop; rel++) {
Simon Glass72cc5382022-10-20 18:22:39 -0600722 ulong base = CONFIG_TEXT_BASE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100723 ulong *p;
724 ulong newaddr;
725
726 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
727
Heinrich Schuchardt33691582019-08-14 06:49:09 +0200728 /*
729 * The runtime services table is updated in
730 * efi_relocate_runtime_table()
731 */
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000732 if (map && efi_is_runtime_service_pointer(p))
733 continue;
734
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700735 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
736 rel->info, *p, rel->offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100737
Rick Chen9677a372018-05-28 19:06:37 +0800738 switch (rel->info & R_MASK) {
739 case R_RELATIVE:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100740#ifdef IS_RELA
Simon Glass72cc5382022-10-20 18:22:39 -0600741 newaddr = rel->addend + offset - CONFIG_TEXT_BASE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100742#else
743 newaddr = *p - lastoff + offset;
744#endif
Rick Chen9677a372018-05-28 19:06:37 +0800745 break;
746#ifdef R_ABSOLUTE
747 case R_ABSOLUTE: {
748 ulong symidx = rel->info >> SYM_INDEX;
749 extern struct dyn_sym __dyn_sym_start[];
750 newaddr = __dyn_sym_start[symidx].addr + offset;
Alexander Grafdf0f6c72018-11-04 22:25:22 +0100751#ifdef IS_RELA
Simon Glass72cc5382022-10-20 18:22:39 -0600752 newaddr -= CONFIG_TEXT_BASE;
Alexander Grafdf0f6c72018-11-04 22:25:22 +0100753#endif
Rick Chen9677a372018-05-28 19:06:37 +0800754 break;
755 }
756#endif
757 default:
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000758 printf("%s: Unknown relocation type %llx\n",
759 __func__, rel->info & R_MASK);
Rick Chen9677a372018-05-28 19:06:37 +0800760 continue;
761 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100762
763 /* Check if the relocation is inside bounds */
764 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200765 newaddr > (map->virtual_start +
766 (map->num_pages << EFI_PAGE_SHIFT)))) {
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000767 printf("%s: Relocation at %p is out of range (%lx)\n",
768 __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100769 continue;
770 }
771
Alexander Graf62a67482016-06-02 11:38:27 +0200772 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100773 *p = newaddr;
Alexander Grafe5702322016-04-11 23:20:39 +0200774 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
775 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf0bd425a2016-03-04 01:10:01 +0100776 }
777
778#ifndef IS_RELA
779 lastoff = offset;
780#endif
781
782 invalidate_icache_all();
783}
784
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200785/**
786 * efi_set_virtual_address_map() - change from physical to virtual mapping
787 *
788 * This function implements the SetVirtualAddressMap() runtime service.
789 *
790 * See the Unified Extensible Firmware Interface (UEFI) specification for
791 * details.
792 *
793 * @memory_map_size: size of the virtual map
794 * @descriptor_size: size of an entry in the map
795 * @descriptor_version: version of the map entries
796 * @virtmap: virtual address mapping information
797 * Return: status code
798 */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100799static efi_status_t EFIAPI efi_set_virtual_address_map(
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200800 efi_uintn_t memory_map_size,
801 efi_uintn_t descriptor_size,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100802 uint32_t descriptor_version,
803 struct efi_mem_desc *virtmap)
804{
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200805 efi_uintn_t n = memory_map_size / descriptor_size;
806 efi_uintn_t i;
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200807 efi_status_t ret = EFI_INVALID_PARAMETER;
Alexander Graf86e00212018-12-11 10:00:42 +0100808 int rt_code_sections = 0;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200809 struct efi_event *event;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100810
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200811 EFI_ENTRY("%zx %zx %x %p", memory_map_size, descriptor_size,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100812 descriptor_version, virtmap);
813
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200814 if (descriptor_version != EFI_MEMORY_DESCRIPTOR_VERSION ||
815 descriptor_size < sizeof(struct efi_mem_desc))
816 goto out;
817
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200818 efi_virtmap = virtmap;
819 efi_descriptor_size = descriptor_size;
820 efi_descriptor_count = n;
821
Alexander Graf86e00212018-12-11 10:00:42 +0100822 /*
823 * TODO:
824 * Further down we are cheating. While really we should implement
825 * SetVirtualAddressMap() events and ConvertPointer() to allow
826 * dynamically loaded drivers to expose runtime services, we don't
827 * today.
828 *
829 * So let's ensure we see exactly one single runtime section, as
830 * that is the built-in one. If we see more (or less), someone must
831 * have tried adding or removing to that which we don't support yet.
832 * In that case, let's better fail rather than expose broken runtime
833 * services.
834 */
835 for (i = 0; i < n; i++) {
836 struct efi_mem_desc *map = (void*)virtmap +
837 (descriptor_size * i);
838
839 if (map->type == EFI_RUNTIME_SERVICES_CODE)
840 rt_code_sections++;
841 }
842
843 if (rt_code_sections != 1) {
844 /*
845 * We expose exactly one single runtime code section, so
846 * something is definitely going wrong.
847 */
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200848 goto out;
Alexander Graf86e00212018-12-11 10:00:42 +0100849 }
850
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200851 /* Notify EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
852 list_for_each_entry(event, &efi_events, link) {
853 if (event->notify_function)
854 EFI_CALL_VOID(event->notify_function(
855 event, event->notify_context));
856 }
857
Alexander Graf77256092016-08-16 21:08:45 +0200858 /* Rebind mmio pointers */
859 for (i = 0; i < n; i++) {
860 struct efi_mem_desc *map = (void*)virtmap +
861 (descriptor_size * i);
862 struct list_head *lhandle;
863 efi_physical_addr_t map_start = map->physical_start;
864 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
865 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200866 u64 off = map->virtual_start - map_start;
Alexander Graf77256092016-08-16 21:08:45 +0200867
868 /* Adjust all mmio pointers in this region */
869 list_for_each(lhandle, &efi_runtime_mmio) {
870 struct efi_runtime_mmio_list *lmmio;
871
872 lmmio = list_entry(lhandle,
873 struct efi_runtime_mmio_list,
874 link);
875 if ((map_start <= lmmio->paddr) &&
876 (map_end >= lmmio->paddr)) {
Alexander Graf77256092016-08-16 21:08:45 +0200877 uintptr_t new_addr = lmmio->paddr + off;
878 *lmmio->ptr = (void *)new_addr;
879 }
880 }
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200881 if ((map_start <= (uintptr_t)systab.tables) &&
882 (map_end >= (uintptr_t)systab.tables)) {
883 char *ptr = (char *)systab.tables;
884
885 ptr += off;
886 systab.tables = (struct efi_configuration_table *)ptr;
887 }
Alexander Graf77256092016-08-16 21:08:45 +0200888 }
889
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000890 /* Relocate the runtime. See TODO above */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100891 for (i = 0; i < n; i++) {
892 struct efi_mem_desc *map;
893
894 map = (void*)virtmap + (descriptor_size * i);
895 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf77256092016-08-16 21:08:45 +0200896 ulong new_offset = map->virtual_start -
Alexander Graf86e00212018-12-11 10:00:42 +0100897 map->physical_start + gd->relocaddr;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100898
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000899 efi_relocate_runtime_table(new_offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100900 efi_runtime_relocate(new_offset, map);
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200901 ret = EFI_SUCCESS;
902 goto out;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100903 }
904 }
905
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200906out:
907 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100908}
909
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200910/**
911 * efi_add_runtime_mmio() - add memory-mapped IO region
912 *
913 * This function adds a memory-mapped IO region to the memory map to make it
914 * available at runtime.
915 *
Heinrich Schuchardt7e307642018-12-23 02:35:13 +0100916 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
917 * IO region
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200918 * @len: size of the memory-mapped IO region
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200919 * Returns: status code
920 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100921efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf77256092016-08-16 21:08:45 +0200922{
923 struct efi_runtime_mmio_list *newmmio;
Alexander Graf33a83a32018-03-15 15:08:16 +0100924 uint64_t addr = *(uintptr_t *)mmio_ptr;
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100925 efi_status_t ret;
Alexander Graf33a83a32018-03-15 15:08:16 +0100926
Michael Walle282d3862020-05-17 12:29:19 +0200927 ret = efi_add_memory_map(addr, len, EFI_MMAP_IO);
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100928 if (ret != EFI_SUCCESS)
Alexander Graf33a83a32018-03-15 15:08:16 +0100929 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200930
931 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100932 if (!newmmio)
933 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200934 newmmio->ptr = mmio_ptr;
935 newmmio->paddr = *(uintptr_t *)mmio_ptr;
936 newmmio->len = len;
937 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100938
Alexander Graf33a83a32018-03-15 15:08:16 +0100939 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200940}
941
Alexander Graf0bd425a2016-03-04 01:10:01 +0100942/*
943 * In the second stage, U-Boot has disappeared. To isolate our runtime code
944 * that at this point still exists from the rest, we put it into a special
945 * section.
946 *
947 * !!WARNING!!
948 *
949 * This means that we can not rely on any code outside of this file in any
950 * function or variable below this line.
951 *
952 * Please keep everything fully self-contained and annotated with
Alexander Graf393dd912016-10-14 13:45:30 +0200953 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf0bd425a2016-03-04 01:10:01 +0100954 */
955
956/*
957 * Relocate the EFI runtime stub to a different place. We need to call this
958 * the first time we expose the runtime interface to a user and on set virtual
959 * address map calls.
960 */
961
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200962/**
963 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
964 *
965 * This function is used after SetVirtualAddressMap() is called as replacement
966 * for services that are not available anymore due to constraints of the U-Boot
967 * implementation.
968 *
969 * Return: EFI_UNSUPPORTED
970 */
Alexander Graf393dd912016-10-14 13:45:30 +0200971static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100972{
973 return EFI_UNSUPPORTED;
974}
975
Alexander Graf393dd912016-10-14 13:45:30 +0200976struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100977 .hdr = {
978 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +0200979 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +0200980 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf0bd425a2016-03-04 01:10:01 +0100981 },
Alexander Graf77256092016-08-16 21:08:45 +0200982 .get_time = &efi_get_time_boottime,
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200983 .set_time = &efi_set_time_boottime,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100984 .get_wakeup_time = (void *)&efi_unimplemented,
985 .set_wakeup_time = (void *)&efi_unimplemented,
986 .set_virtual_address_map = &efi_set_virtual_address_map,
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200987 .convert_pointer = efi_convert_pointer,
Rob Clark15f3d742017-09-13 18:05:37 -0400988 .get_variable = efi_get_variable,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +0200989 .get_next_variable_name = efi_get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -0400990 .set_variable = efi_set_variable,
Heinrich Schuchardt5337a6c2019-06-20 15:40:49 +0200991 .get_next_high_mono_count = (void *)&efi_unimplemented,
Alexander Graf77256092016-08-16 21:08:45 +0200992 .reset_system = &efi_reset_system_boottime,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900993#ifdef CONFIG_EFI_RUNTIME_UPDATE_CAPSULE
Heinrich Schuchardt897c0722018-02-09 20:41:21 +0100994 .update_capsule = efi_update_capsule,
995 .query_capsule_caps = efi_query_capsule_caps,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900996#else
997 .update_capsule = efi_update_capsule_unsupported,
998 .query_capsule_caps = efi_query_capsule_caps_unsupported,
999#endif
Heinrich Schuchardt897c0722018-02-09 20:41:21 +01001000 .query_variable_info = efi_query_variable_info,
Alexander Graf0bd425a2016-03-04 01:10:01 +01001001};