blob: 35eb6a7776658cb21a3e3d283c30edeb862bc269 [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
Heinrich Schuchardt955a3212025-01-16 20:26:59 +01008#define LOG_CATEGORY LOGC_EFI
9
Alexander Graf0bd425a2016-03-04 01:10:01 +010010#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070011#include <cpu_func.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010012#include <dm.h>
Alexander Graf46c15142018-06-18 17:23:11 +020013#include <elf.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010014#include <efi_loader.h>
Ilias Apalodimas465cb7e2024-04-18 15:54:51 +030015#include <efi_variable.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <malloc.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010018#include <rtc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060019#include <asm/global_data.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070020#include <u-boot/crc.h>
Ilias Apalodimas9b378942024-03-15 08:43:47 +020021#include <asm/sections.h>
Alexander Graf0bd425a2016-03-04 01:10:01 +010022
23/* For manual relocation support */
24DECLARE_GLOBAL_DATA_PTR;
25
Heinrich Schuchardt956eff32020-02-19 20:48:49 +010026/* GUID of the runtime properties table */
27static const efi_guid_t efi_rt_properties_table_guid =
28 EFI_RT_PROPERTIES_TABLE_GUID;
29
Alexander Graf77256092016-08-16 21:08:45 +020030struct efi_runtime_mmio_list {
31 struct list_head link;
32 void **ptr;
33 u64 paddr;
34 u64 len;
35};
36
37/* This list contains all runtime available mmio regions */
Bin Mengba7ad022023-04-05 20:15:19 +080038static LIST_HEAD(efi_runtime_mmio);
Alexander Graf77256092016-08-16 21:08:45 +020039
Alexander Graf393dd912016-10-14 13:45:30 +020040static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void);
Alexander Graf0bd425a2016-03-04 01:10:01 +010041
Simon Glassfff89d42018-06-11 23:26:40 -060042/*
Heinrich Schuchardt11121542018-09-03 19:29:41 +020043 * TODO(sjg@chromium.org): These defines and structures should come from the ELF
44 * header for each architecture (or a generic header) rather than being repeated
45 * here.
Simon Glassfff89d42018-06-11 23:26:40 -060046 */
Alexander Graf0fe51922018-06-18 17:23:08 +020047#if defined(__aarch64__)
Alexander Graf46c15142018-06-18 17:23:11 +020048#define R_RELATIVE R_AARCH64_RELATIVE
Alexander Graf0bd425a2016-03-04 01:10:01 +010049#define R_MASK 0xffffffffULL
50#define IS_RELA 1
Alexander Graf0fe51922018-06-18 17:23:08 +020051#elif defined(__arm__)
Alexander Graf46c15142018-06-18 17:23:11 +020052#define R_RELATIVE R_ARM_RELATIVE
Alexander Graf0bd425a2016-03-04 01:10:01 +010053#define R_MASK 0xffULL
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -070054#elif defined(__i386__)
Simon Glasscdfe6962016-09-25 15:27:35 -060055#define R_RELATIVE R_386_RELATIVE
56#define R_MASK 0xffULL
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -070057#elif defined(__x86_64__)
58#define R_RELATIVE R_X86_64_RELATIVE
59#define R_MASK 0xffffffffULL
60#define IS_RELA 1
Alexander Graf0fe51922018-06-18 17:23:08 +020061#elif defined(__riscv)
Rick Chen9677a372018-05-28 19:06:37 +080062#define R_RELATIVE R_RISCV_RELATIVE
63#define R_MASK 0xffULL
64#define IS_RELA 1
65
66struct dyn_sym {
67 ulong foo1;
68 ulong addr;
69 u32 foo2;
70 u32 foo3;
71};
Alexander Graf0fe51922018-06-18 17:23:08 +020072#if (__riscv_xlen == 32)
Rick Chen9677a372018-05-28 19:06:37 +080073#define R_ABSOLUTE R_RISCV_32
74#define SYM_INDEX 8
Alexander Graf0fe51922018-06-18 17:23:08 +020075#elif (__riscv_xlen == 64)
Rick Chen9677a372018-05-28 19:06:37 +080076#define R_ABSOLUTE R_RISCV_64
77#define SYM_INDEX 32
Alexander Graf0fe51922018-06-18 17:23:08 +020078#else
79#error unknown riscv target
Rick Chen9677a372018-05-28 19:06:37 +080080#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +010081#else
82#error Need to add relocation awareness
83#endif
84
85struct elf_rel {
86 ulong *offset;
87 ulong info;
88};
89
90struct elf_rela {
91 ulong *offset;
92 ulong info;
93 long addend;
94};
95
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +020096static __efi_runtime_data struct efi_mem_desc *efi_virtmap;
97static __efi_runtime_data efi_uintn_t efi_descriptor_count;
98static __efi_runtime_data efi_uintn_t efi_descriptor_size;
99
Alexander Graf0bd425a2016-03-04 01:10:01 +0100100/*
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200101 * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI
Alexander Graf0bd425a2016-03-04 01:10:01 +0100102 * payload are running concurrently at the same time. In this mode, we can
103 * handle a good number of runtime callbacks
104 */
105
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100106/**
107 * efi_init_runtime_supported() - create runtime properties table
108 *
109 * Create a configuration table specifying which services are available at
110 * runtime.
111 *
112 * Return: status code
113 */
AKASHI Takahiro32401842019-06-05 13:21:38 +0900114efi_status_t efi_init_runtime_supported(void)
115{
Ilias Apalodimas465cb7e2024-04-18 15:54:51 +0300116 const efi_guid_t efi_guid_efi_rt_var_file = U_BOOT_EFI_RT_VAR_FILE_GUID;
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100117 efi_status_t ret;
118 struct efi_rt_properties_table *rt_table;
119
120 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
121 sizeof(struct efi_rt_properties_table),
122 (void **)&rt_table);
123 if (ret != EFI_SUCCESS)
124 return ret;
125
126 rt_table->version = EFI_RT_PROPERTIES_TABLE_VERSION;
127 rt_table->length = sizeof(struct efi_rt_properties_table);
128 rt_table->runtime_services_supported =
Heinrich Schuchardt5be59712020-03-24 19:54:53 +0000129 EFI_RT_SUPPORTED_GET_VARIABLE |
130 EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME |
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200131 EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP |
132 EFI_RT_SUPPORTED_CONVERT_POINTER;
AKASHI Takahiro32401842019-06-05 13:21:38 +0900133
Ilias Apalodimasfc071532024-04-25 08:18:19 +0300134 if (IS_ENABLED(CONFIG_EFI_VARIABLE_FILE_STORE))
135 rt_table->runtime_services_supported |=
136 EFI_RT_SUPPORTED_QUERY_VARIABLE_INFO;
137
Ilias Apalodimas465cb7e2024-04-18 15:54:51 +0300138 if (IS_ENABLED(CONFIG_EFI_RT_VOLATILE_STORE)) {
Ilias Apalodimasde708562024-04-18 15:54:52 +0300139 u8 s = 0;
140
Ilias Apalodimas465cb7e2024-04-18 15:54:51 +0300141 ret = efi_set_variable_int(u"RTStorageVolatile",
142 &efi_guid_efi_rt_var_file,
143 EFI_VARIABLE_BOOTSERVICE_ACCESS |
144 EFI_VARIABLE_RUNTIME_ACCESS |
145 EFI_VARIABLE_READ_ONLY,
146 sizeof(EFI_VAR_FILE_NAME),
147 EFI_VAR_FILE_NAME, false);
148 if (ret != EFI_SUCCESS) {
149 log_err("Failed to set RTStorageVolatile\n");
150 return ret;
151 }
Ilias Apalodimasde708562024-04-18 15:54:52 +0300152 /*
153 * This variable needs to be visible so users can read it,
154 * but the real contents are going to be filled during
155 * GetVariable
156 */
157 ret = efi_set_variable_int(u"VarToFile",
158 &efi_guid_efi_rt_var_file,
159 EFI_VARIABLE_BOOTSERVICE_ACCESS |
160 EFI_VARIABLE_RUNTIME_ACCESS |
161 EFI_VARIABLE_READ_ONLY,
162 sizeof(s),
163 &s, false);
164 if (ret != EFI_SUCCESS) {
165 log_err("Failed to set VarToFile\n");
166 efi_set_variable_int(u"RTStorageVolatile",
167 &efi_guid_efi_rt_var_file,
168 EFI_VARIABLE_BOOTSERVICE_ACCESS |
169 EFI_VARIABLE_RUNTIME_ACCESS |
170 EFI_VARIABLE_READ_ONLY,
171 0, NULL, false);
172
173 return ret;
174 }
Ilias Apalodimas465cb7e2024-04-18 15:54:51 +0300175 rt_table->runtime_services_supported |= EFI_RT_SUPPORTED_SET_VARIABLE;
176 }
Ilias Apalodimas86ba8692024-04-18 15:54:50 +0300177
AKASHI Takahiro32401842019-06-05 13:21:38 +0900178 /*
179 * This value must be synced with efi_runtime_detach_list
180 * as well as efi_runtime_services.
181 */
Heinrich Schuchardt05874fb2019-07-05 18:12:16 +0200182#ifdef CONFIG_EFI_HAVE_RUNTIME_RESET
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100183 rt_table->runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM;
AKASHI Takahiro32401842019-06-05 13:21:38 +0900184#endif
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200185
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100186 ret = efi_install_configuration_table(&efi_rt_properties_table_guid,
187 rt_table);
188 return ret;
AKASHI Takahiro32401842019-06-05 13:21:38 +0900189}
190
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200191/**
Heinrich Schuchardt8cd99752020-06-28 16:30:29 +0200192 * efi_memcpy_runtime() - copy memory area
193 *
194 * At runtime memcpy() is not available.
195 *
Heinrich Schuchardt447753e2020-07-22 07:56:14 +0200196 * Overlapping memory areas can be copied safely if src >= dest.
197 *
Heinrich Schuchardt8cd99752020-06-28 16:30:29 +0200198 * @dest: destination buffer
199 * @src: source buffer
200 * @n: number of bytes to copy
201 * Return: pointer to destination buffer
202 */
203void __efi_runtime efi_memcpy_runtime(void *dest, const void *src, size_t n)
204{
205 u8 *d = dest;
206 const u8 *s = src;
207
208 for (; n; --n)
209 *d++ = *s++;
210}
211
212/**
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200213 * efi_update_table_header_crc32() - Update crc32 in table header
214 *
215 * @table: EFI table
216 */
217void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
218{
219 table->crc32 = 0;
220 table->crc32 = crc32(0, (const unsigned char *)table,
221 table->headersize);
222}
223
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200224/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200225 * efi_reset_system_boottime() - reset system at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200226 *
227 * This function implements the ResetSystem() runtime service before
228 * SetVirtualAddressMap() is called.
229 *
230 * See the Unified Extensible Firmware Interface (UEFI) specification for
231 * details.
232 *
233 * @reset_type: type of reset to perform
234 * @reset_status: status code for the reset
235 * @data_size: size of reset_data
236 * @reset_data: information about the reset
237 */
Alexander Graf77256092016-08-16 21:08:45 +0200238static void EFIAPI efi_reset_system_boottime(
239 enum efi_reset_type reset_type,
240 efi_status_t reset_status,
241 unsigned long data_size, void *reset_data)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100242{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100243 struct efi_event *evt;
244
Alexander Graf0bd425a2016-03-04 01:10:01 +0100245 EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
246 reset_data);
247
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100248 /* Notify reset */
249 list_for_each_entry(evt, &efi_events, link) {
250 if (evt->group &&
251 !guidcmp(evt->group,
252 &efi_guid_event_group_reset_system)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200253 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100254 break;
255 }
256 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100257 switch (reset_type) {
258 case EFI_RESET_COLD:
259 case EFI_RESET_WARM:
Heinrich Schuchardt450d4c82018-02-06 22:00:22 +0100260 case EFI_RESET_PLATFORM_SPECIFIC:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100261 do_reset(NULL, 0, 0, NULL);
262 break;
263 case EFI_RESET_SHUTDOWN:
Heinrich Schuchardt44489452018-10-16 07:44:53 +0200264#ifdef CONFIG_CMD_POWEROFF
265 do_poweroff(NULL, 0, 0, NULL);
266#endif
Alexander Graf0bd425a2016-03-04 01:10:01 +0100267 break;
268 }
269
Alexander Graf77256092016-08-16 21:08:45 +0200270 while (1) { }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100271}
272
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200273/**
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200274 * efi_get_time_boottime() - get current time at boot time
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200275 *
276 * This function implements the GetTime runtime service before
277 * SetVirtualAddressMap() is called.
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200278 *
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200279 * See the Unified Extensible Firmware Interface (UEFI) specification
280 * for details.
281 *
282 * @time: pointer to structure to receive current time
283 * @capabilities: pointer to structure to receive RTC properties
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200284 * Returns: status code
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200285 */
Alexander Graf77256092016-08-16 21:08:45 +0200286static efi_status_t EFIAPI efi_get_time_boottime(
287 struct efi_time *time,
288 struct efi_time_cap *capabilities)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100289{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200290#ifdef CONFIG_EFI_GET_TIME
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200291 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200292 struct rtc_time tm;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100293 struct udevice *dev;
294
295 EFI_ENTRY("%p %p", time, capabilities);
296
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200297 if (!time) {
298 ret = EFI_INVALID_PARAMETER;
299 goto out;
300 }
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200301 if (uclass_get_device(UCLASS_RTC, 0, &dev) ||
302 dm_rtc_get(dev, &tm)) {
303 ret = EFI_UNSUPPORTED;
304 goto out;
305 }
306 if (dm_rtc_get(dev, &tm)) {
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200307 ret = EFI_DEVICE_ERROR;
308 goto out;
309 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100310
311 memset(time, 0, sizeof(*time));
312 time->year = tm.tm_year;
313 time->month = tm.tm_mon;
314 time->day = tm.tm_mday;
315 time->hour = tm.tm_hour;
316 time->minute = tm.tm_min;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200317 time->second = tm.tm_sec;
Heinrich Schuchardt6846c572020-10-23 05:30:29 +0200318 if (tm.tm_isdst > 0)
Heinrich Schuchardtba3e8282019-05-31 22:08:45 +0200319 time->daylight =
320 EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT;
Heinrich Schuchardt6846c572020-10-23 05:30:29 +0200321 else if (!tm.tm_isdst)
322 time->daylight = EFI_TIME_ADJUST_DAYLIGHT;
323 else
324 time->daylight = 0;
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200325 time->timezone = EFI_UNSPECIFIED_TIMEZONE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100326
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200327 if (capabilities) {
328 /* Set reasonable dummy values */
329 capabilities->resolution = 1; /* 1 Hz */
330 capabilities->accuracy = 100000000; /* 100 ppm */
331 capabilities->sets_to_zero = false;
332 }
333out:
334 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100335#else
Heinrich Schuchardt37035b12018-07-07 23:39:14 +0200336 EFI_ENTRY("%p %p", time, capabilities);
Heinrich Schuchardtb4bd3662019-05-19 21:41:28 +0200337 return EFI_EXIT(EFI_UNSUPPORTED);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100338#endif
339}
340
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200341#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200342
343/**
344 * efi_validate_time() - checks if timestamp is valid
345 *
346 * @time: timestamp to validate
347 * Returns: 0 if timestamp is valid, 1 otherwise
348 */
349static int efi_validate_time(struct efi_time *time)
350{
351 return (!time ||
352 time->year < 1900 || time->year > 9999 ||
353 !time->month || time->month > 12 || !time->day ||
354 time->day > rtc_month_days(time->month - 1, time->year) ||
355 time->hour > 23 || time->minute > 59 || time->second > 59 ||
356 time->nanosecond > 999999999 ||
357 time->daylight &
358 ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) ||
359 ((time->timezone < -1440 || time->timezone > 1440) &&
360 time->timezone != EFI_UNSPECIFIED_TIMEZONE));
361}
362
363#endif
364
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200365/**
366 * efi_set_time_boottime() - set current time
367 *
368 * This function implements the SetTime() runtime service before
369 * SetVirtualAddressMap() is called.
370 *
371 * See the Unified Extensible Firmware Interface (UEFI) specification
372 * for details.
373 *
374 * @time: pointer to structure to with current time
375 * Returns: status code
376 */
377static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time)
378{
Heinrich Schuchardtf2856ad2019-05-31 22:56:02 +0200379#ifdef CONFIG_EFI_SET_TIME
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200380 efi_status_t ret = EFI_SUCCESS;
381 struct rtc_time tm;
382 struct udevice *dev;
Alexander Graf77256092016-08-16 21:08:45 +0200383
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200384 EFI_ENTRY("%p", time);
385
Heinrich Schuchardt33e5a0e2019-05-31 07:35:19 +0200386 if (efi_validate_time(time)) {
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200387 ret = EFI_INVALID_PARAMETER;
388 goto out;
389 }
390
391 if (uclass_get_device(UCLASS_RTC, 0, &dev)) {
392 ret = EFI_UNSUPPORTED;
393 goto out;
394 }
395
396 memset(&tm, 0, sizeof(tm));
397 tm.tm_year = time->year;
398 tm.tm_mon = time->month;
399 tm.tm_mday = time->day;
400 tm.tm_hour = time->hour;
401 tm.tm_min = time->minute;
402 tm.tm_sec = time->second;
Heinrich Schuchardt6846c572020-10-23 05:30:29 +0200403 switch (time->daylight) {
404 case EFI_TIME_ADJUST_DAYLIGHT:
405 tm.tm_isdst = 0;
406 break;
407 case EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT:
408 tm.tm_isdst = 1;
409 break;
410 default:
411 tm.tm_isdst = -1;
412 break;
413 }
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200414 /* Calculate day of week */
415 rtc_calc_weekday(&tm);
416
417 if (dm_rtc_set(dev, &tm))
418 ret = EFI_DEVICE_ERROR;
419out:
420 return EFI_EXIT(ret);
421#else
422 EFI_ENTRY("%p", time);
423 return EFI_EXIT(EFI_UNSUPPORTED);
424#endif
425}
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200426/**
427 * efi_reset_system() - reset system
428 *
429 * This function implements the ResetSystem() runtime service after
Heinrich Schuchardt7b66fcc2020-08-22 08:29:53 +0200430 * SetVirtualAddressMap() is called. As this placeholder cannot reset the
431 * system it simply return to the caller.
432 *
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200433 * Boards may override the helpers below to implement reset functionality.
434 *
435 * See the Unified Extensible Firmware Interface (UEFI) specification for
436 * details.
437 *
438 * @reset_type: type of reset to perform
439 * @reset_status: status code for the reset
440 * @data_size: size of reset_data
441 * @reset_data: information about the reset
442 */
Alexander Graf393dd912016-10-14 13:45:30 +0200443void __weak __efi_runtime EFIAPI efi_reset_system(
Alexander Graf77256092016-08-16 21:08:45 +0200444 enum efi_reset_type reset_type,
445 efi_status_t reset_status,
446 unsigned long data_size, void *reset_data)
447{
Heinrich Schuchardt7b66fcc2020-08-22 08:29:53 +0200448 return;
Alexander Graf77256092016-08-16 21:08:45 +0200449}
450
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200451/**
452 * efi_reset_system_init() - initialize the reset driver
453 *
454 * Boards may override this function to initialize the reset driver.
455 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100456efi_status_t __weak efi_reset_system_init(void)
Alexander Graf77256092016-08-16 21:08:45 +0200457{
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100458 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200459}
460
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200461/**
462 * efi_get_time() - get current time
463 *
464 * This function implements the GetTime runtime service after
465 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
466 * anymore only an error code is returned.
467 *
468 * See the Unified Extensible Firmware Interface (UEFI) specification
469 * for details.
470 *
471 * @time: pointer to structure to receive current time
472 * @capabilities: pointer to structure to receive RTC properties
473 * Returns: status code
474 */
Alexander Graf393dd912016-10-14 13:45:30 +0200475efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
Alexander Graf77256092016-08-16 21:08:45 +0200476 struct efi_time *time,
477 struct efi_time_cap *capabilities)
478{
Heinrich Schuchardtb4ba5be2019-06-13 18:42:40 +0200479 return EFI_UNSUPPORTED;
Alexander Graf77256092016-08-16 21:08:45 +0200480}
481
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200482/**
483 * efi_set_time() - set current time
484 *
485 * This function implements the SetTime runtime service after
486 * SetVirtualAddressMap() is called. As the U-Boot driver are not available
487 * anymore only an error code is returned.
488 *
489 * See the Unified Extensible Firmware Interface (UEFI) specification
490 * for details.
491 *
492 * @time: pointer to structure to with current time
493 * Returns: status code
494 */
495efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time)
496{
497 return EFI_UNSUPPORTED;
498}
499
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000500/**
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900501 * efi_update_capsule_unsupported() - process information from operating system
502 *
503 * This function implements the UpdateCapsule() runtime service.
504 *
505 * See the Unified Extensible Firmware Interface (UEFI) specification for
506 * details.
507 *
508 * @capsule_header_array: pointer to array of virtual pointers
509 * @capsule_count: number of pointers in capsule_header_array
510 * @scatter_gather_list: pointer to array of physical pointers
511 * Returns: status code
512 */
Heinrich Schuchardt6ab94032023-02-10 08:56:06 +0100513static efi_status_t __efi_runtime EFIAPI efi_update_capsule_unsupported(
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900514 struct efi_capsule_header **capsule_header_array,
515 efi_uintn_t capsule_count,
516 u64 scatter_gather_list)
517{
518 return EFI_UNSUPPORTED;
519}
520
521/**
522 * efi_query_capsule_caps_unsupported() - check if capsule is supported
523 *
524 * This function implements the QueryCapsuleCapabilities() runtime service.
525 *
526 * See the Unified Extensible Firmware Interface (UEFI) specification for
527 * details.
528 *
529 * @capsule_header_array: pointer to array of virtual pointers
530 * @capsule_count: number of pointers in capsule_header_array
531 * @maximum_capsule_size: maximum capsule size
532 * @reset_type: type of reset needed for capsule update
533 * Returns: status code
534 */
Heinrich Schuchardt6ab94032023-02-10 08:56:06 +0100535static efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps_unsupported(
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900536 struct efi_capsule_header **capsule_header_array,
537 efi_uintn_t capsule_count,
538 u64 *maximum_capsule_size,
539 u32 *reset_type)
540{
541 return EFI_UNSUPPORTED;
542}
543
544/**
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000545 * efi_is_runtime_service_pointer() - check if pointer points to runtime table
546 *
547 * @p: pointer to check
548 * Return: true if the pointer points to a service function pointer in the
549 * runtime table
550 */
551static bool efi_is_runtime_service_pointer(void *p)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100552{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200553 return (p >= (void *)&efi_runtime_services.get_time &&
554 p <= (void *)&efi_runtime_services.query_variable_info) ||
555 p == (void *)&efi_events.prev ||
556 p == (void *)&efi_events.next;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100557}
558
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200559/**
560 * efi_runtime_detach() - detach unimplemented runtime functions
561 */
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +0200562void efi_runtime_detach(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100563{
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200564 efi_runtime_services.reset_system = efi_reset_system;
565 efi_runtime_services.get_time = efi_get_time;
566 efi_runtime_services.set_time = efi_set_time;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900567 if (IS_ENABLED(CONFIG_EFI_RUNTIME_UPDATE_CAPSULE)) {
568 /* won't support at runtime */
569 efi_runtime_services.update_capsule =
570 efi_update_capsule_unsupported;
571 efi_runtime_services.query_capsule_caps =
572 efi_query_capsule_caps_unsupported;
573 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100574
Heinrich Schuchardtc3f2a9f2019-07-05 18:12:21 +0200575 /* Update CRC32 */
576 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000577}
578
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200579/**
580 * efi_set_virtual_address_map_runtime() - change from physical to virtual
581 * mapping
582 *
583 * This function implements the SetVirtualAddressMap() runtime service after
584 * it is first called.
585 *
586 * See the Unified Extensible Firmware Interface (UEFI) specification for
587 * details.
588 *
589 * @memory_map_size: size of the virtual map
590 * @descriptor_size: size of an entry in the map
591 * @descriptor_version: version of the map entries
592 * @virtmap: virtual address mapping information
593 * Return: status code EFI_UNSUPPORTED
594 */
Heinrich Schuchardt2b6bd382019-07-12 22:41:43 +0200595static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map_runtime(
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200596 efi_uintn_t memory_map_size,
597 efi_uintn_t descriptor_size,
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200598 uint32_t descriptor_version,
599 struct efi_mem_desc *virtmap)
600{
601 return EFI_UNSUPPORTED;
602}
603
604/**
605 * efi_convert_pointer_runtime() - convert from physical to virtual pointer
606 *
607 * This function implements the ConvertPointer() runtime service after
608 * the first call to SetVirtualAddressMap().
609 *
610 * See the Unified Extensible Firmware Interface (UEFI) specification for
611 * details.
612 *
613 * @debug_disposition: indicates if pointer may be converted to NULL
614 * @address: pointer to be converted
615 * Return: status code EFI_UNSUPPORTED
616 */
617static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime(
618 efi_uintn_t debug_disposition, void **address)
619{
620 return EFI_UNSUPPORTED;
621}
622
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200623/**
Heinrich Schuchardt582f8572020-03-22 08:28:15 +0100624 * efi_convert_pointer() - convert from physical to virtual pointer
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200625 *
626 * This function implements the ConvertPointer() runtime service until
627 * the first call to SetVirtualAddressMap().
628 *
629 * See the Unified Extensible Firmware Interface (UEFI) specification for
630 * details.
631 *
632 * @debug_disposition: indicates if pointer may be converted to NULL
633 * @address: pointer to be converted
Heinrich Schuchardt582f8572020-03-22 08:28:15 +0100634 * Return: status code
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200635 */
Heinrich Schuchardt699b7c62020-03-24 18:05:22 +0100636__efi_runtime efi_status_t EFIAPI
637efi_convert_pointer(efi_uintn_t debug_disposition, void **address)
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200638{
Heinrich Schuchardtb0818b72020-07-07 03:10:12 +0200639 efi_physical_addr_t addr;
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200640 efi_uintn_t i;
641 efi_status_t ret = EFI_NOT_FOUND;
642
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200643 if (!efi_virtmap) {
644 ret = EFI_UNSUPPORTED;
645 goto out;
646 }
647
648 if (!address) {
649 ret = EFI_INVALID_PARAMETER;
650 goto out;
651 }
Heinrich Schuchardtdabe54a2020-03-24 17:52:40 +0100652 if (!*address) {
653 if (debug_disposition & EFI_OPTIONAL_PTR)
654 return EFI_SUCCESS;
655 else
656 return EFI_INVALID_PARAMETER;
657 }
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200658
Heinrich Schuchardtb0818b72020-07-07 03:10:12 +0200659 addr = (uintptr_t)*address;
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200660 for (i = 0; i < efi_descriptor_count; i++) {
661 struct efi_mem_desc *map = (void *)efi_virtmap +
662 (efi_descriptor_size * i);
663
664 if (addr >= map->physical_start &&
665 (addr < map->physical_start
666 + (map->num_pages << EFI_PAGE_SHIFT))) {
667 *address = (void *)(uintptr_t)
668 (addr + map->virtual_start -
669 map->physical_start);
670
671 ret = EFI_SUCCESS;
672 break;
673 }
674 }
675
676out:
Heinrich Schuchardt699b7c62020-03-24 18:05:22 +0100677 return ret;
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200678}
679
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000680static __efi_runtime void efi_relocate_runtime_table(ulong offset)
681{
682 ulong patchoff;
683 void **pos;
684
685 /* Relocate the runtime services pointers */
686 patchoff = offset - gd->relocaddr;
687 for (pos = (void **)&efi_runtime_services.get_time;
688 pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) {
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200689 if (*pos)
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000690 *pos += patchoff;
691 }
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200692
Heinrich Schuchardt25728512019-06-29 03:32:52 +0200693 /*
694 * The entry for SetVirtualAddress() must point to a physical address.
695 * After the first execution the service must return EFI_UNSUPPORTED.
696 */
697 efi_runtime_services.set_virtual_address_map =
698 &efi_set_virtual_address_map_runtime;
699
700 /*
701 * The entry for ConvertPointer() must point to a physical address.
702 * The service is not usable after SetVirtualAddress().
703 */
704 efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime;
705
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200706 /*
707 * TODO: Update UEFI variable RuntimeServicesSupported removing flags
708 * EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP and
709 * EFI_RT_SUPPORTED_CONVERT_POINTER as required by the UEFI spec 2.8.
710 */
711
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200712 /* Update CRC32 */
Heinrich Schuchardt69c15762018-07-29 09:49:04 +0200713 efi_update_table_header_crc32(&efi_runtime_services.hdr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100714}
715
716/* Relocate EFI runtime to uboot_reloc_base = offset */
717void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
718{
719#ifdef IS_RELA
Ilias Apalodimas9bad9cd2024-04-04 09:37:37 +0300720 struct elf_rela *rel = (void *)__efi_runtime_rel_start;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100721#else
Ilias Apalodimas9bad9cd2024-04-04 09:37:37 +0300722 struct elf_rel *rel = (void *)__efi_runtime_rel_start;
Simon Glass72cc5382022-10-20 18:22:39 -0600723 static ulong lastoff = CONFIG_TEXT_BASE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100724#endif
725
Alexander Graf62a67482016-06-02 11:38:27 +0200726 debug("%s: Relocating to offset=%lx\n", __func__, offset);
Ilias Apalodimas9bad9cd2024-04-04 09:37:37 +0300727 for (; (uintptr_t)rel < (uintptr_t)__efi_runtime_rel_stop; rel++) {
Simon Glass72cc5382022-10-20 18:22:39 -0600728 ulong base = CONFIG_TEXT_BASE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100729 ulong *p;
730 ulong newaddr;
731
732 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
733
Heinrich Schuchardt33691582019-08-14 06:49:09 +0200734 /*
735 * The runtime services table is updated in
736 * efi_relocate_runtime_table()
737 */
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000738 if (map && efi_is_runtime_service_pointer(p))
739 continue;
740
Heinrich Schuchardt56e82be2018-10-13 20:52:08 -0700741 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__,
742 rel->info, *p, rel->offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100743
Rick Chen9677a372018-05-28 19:06:37 +0800744 switch (rel->info & R_MASK) {
745 case R_RELATIVE:
Alexander Graf0bd425a2016-03-04 01:10:01 +0100746#ifdef IS_RELA
Simon Glass72cc5382022-10-20 18:22:39 -0600747 newaddr = rel->addend + offset - CONFIG_TEXT_BASE;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100748#else
749 newaddr = *p - lastoff + offset;
750#endif
Rick Chen9677a372018-05-28 19:06:37 +0800751 break;
752#ifdef R_ABSOLUTE
753 case R_ABSOLUTE: {
754 ulong symidx = rel->info >> SYM_INDEX;
755 extern struct dyn_sym __dyn_sym_start[];
756 newaddr = __dyn_sym_start[symidx].addr + offset;
Alexander Grafdf0f6c72018-11-04 22:25:22 +0100757#ifdef IS_RELA
Simon Glass72cc5382022-10-20 18:22:39 -0600758 newaddr -= CONFIG_TEXT_BASE;
Alexander Grafdf0f6c72018-11-04 22:25:22 +0100759#endif
Rick Chen9677a372018-05-28 19:06:37 +0800760 break;
761 }
762#endif
763 default:
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000764 printf("%s: Unknown relocation type %llx\n",
765 __func__, rel->info & R_MASK);
Rick Chen9677a372018-05-28 19:06:37 +0800766 continue;
767 }
Alexander Graf0bd425a2016-03-04 01:10:01 +0100768
769 /* Check if the relocation is inside bounds */
770 if (map && ((newaddr < map->virtual_start) ||
Heinrich Schuchardt9ebc47f2017-09-18 22:11:34 +0200771 newaddr > (map->virtual_start +
772 (map->num_pages << EFI_PAGE_SHIFT)))) {
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000773 printf("%s: Relocation at %p is out of range (%lx)\n",
774 __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100775 continue;
776 }
777
Alexander Graf62a67482016-06-02 11:38:27 +0200778 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100779 *p = newaddr;
Alexander Grafe5702322016-04-11 23:20:39 +0200780 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
781 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
Alexander Graf0bd425a2016-03-04 01:10:01 +0100782 }
783
784#ifndef IS_RELA
785 lastoff = offset;
786#endif
787
Heinrich Schuchardt3a67d132024-06-16 19:31:05 +0200788 /*
789 * If on x86 a write affects a prefetched instruction,
790 * the prefetch queue is invalidated.
791 */
792 if (!CONFIG_IS_ENABLED(X86))
793 invalidate_icache_all();
Alexander Graf0bd425a2016-03-04 01:10:01 +0100794}
795
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200796/**
797 * efi_set_virtual_address_map() - change from physical to virtual mapping
798 *
799 * This function implements the SetVirtualAddressMap() runtime service.
800 *
801 * See the Unified Extensible Firmware Interface (UEFI) specification for
802 * details.
803 *
804 * @memory_map_size: size of the virtual map
805 * @descriptor_size: size of an entry in the map
806 * @descriptor_version: version of the map entries
807 * @virtmap: virtual address mapping information
808 * Return: status code
809 */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100810static efi_status_t EFIAPI efi_set_virtual_address_map(
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200811 efi_uintn_t memory_map_size,
812 efi_uintn_t descriptor_size,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100813 uint32_t descriptor_version,
814 struct efi_mem_desc *virtmap)
815{
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200816 efi_uintn_t n = memory_map_size / descriptor_size;
817 efi_uintn_t i;
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200818 efi_status_t ret = EFI_INVALID_PARAMETER;
Alexander Graf86e00212018-12-11 10:00:42 +0100819 int rt_code_sections = 0;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200820 struct efi_event *event;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100821
Heinrich Schuchardtf8599dc2019-07-27 20:28:47 +0200822 EFI_ENTRY("%zx %zx %x %p", memory_map_size, descriptor_size,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100823 descriptor_version, virtmap);
824
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200825 if (descriptor_version != EFI_MEMORY_DESCRIPTOR_VERSION ||
826 descriptor_size < sizeof(struct efi_mem_desc))
827 goto out;
828
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200829 efi_virtmap = virtmap;
830 efi_descriptor_size = descriptor_size;
831 efi_descriptor_count = n;
832
Alexander Graf86e00212018-12-11 10:00:42 +0100833 /*
834 * TODO:
835 * Further down we are cheating. While really we should implement
836 * SetVirtualAddressMap() events and ConvertPointer() to allow
837 * dynamically loaded drivers to expose runtime services, we don't
838 * today.
839 *
840 * So let's ensure we see exactly one single runtime section, as
841 * that is the built-in one. If we see more (or less), someone must
842 * have tried adding or removing to that which we don't support yet.
843 * In that case, let's better fail rather than expose broken runtime
844 * services.
845 */
846 for (i = 0; i < n; i++) {
847 struct efi_mem_desc *map = (void*)virtmap +
848 (descriptor_size * i);
849
850 if (map->type == EFI_RUNTIME_SERVICES_CODE)
851 rt_code_sections++;
852 }
853
854 if (rt_code_sections != 1) {
855 /*
856 * We expose exactly one single runtime code section, so
857 * something is definitely going wrong.
858 */
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200859 goto out;
Alexander Graf86e00212018-12-11 10:00:42 +0100860 }
861
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200862 /* Notify EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
863 list_for_each_entry(event, &efi_events, link) {
864 if (event->notify_function)
865 EFI_CALL_VOID(event->notify_function(
866 event, event->notify_context));
867 }
868
Alexander Graf77256092016-08-16 21:08:45 +0200869 /* Rebind mmio pointers */
870 for (i = 0; i < n; i++) {
871 struct efi_mem_desc *map = (void*)virtmap +
872 (descriptor_size * i);
873 struct list_head *lhandle;
874 efi_physical_addr_t map_start = map->physical_start;
875 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
876 efi_physical_addr_t map_end = map_start + map_len;
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200877 u64 off = map->virtual_start - map_start;
Alexander Graf77256092016-08-16 21:08:45 +0200878
879 /* Adjust all mmio pointers in this region */
880 list_for_each(lhandle, &efi_runtime_mmio) {
881 struct efi_runtime_mmio_list *lmmio;
882
883 lmmio = list_entry(lhandle,
884 struct efi_runtime_mmio_list,
885 link);
886 if ((map_start <= lmmio->paddr) &&
887 (map_end >= lmmio->paddr)) {
Alexander Graf77256092016-08-16 21:08:45 +0200888 uintptr_t new_addr = lmmio->paddr + off;
889 *lmmio->ptr = (void *)new_addr;
890 }
891 }
Heinrich Schuchardt39054562018-08-04 23:16:06 +0200892 if ((map_start <= (uintptr_t)systab.tables) &&
893 (map_end >= (uintptr_t)systab.tables)) {
894 char *ptr = (char *)systab.tables;
895
896 ptr += off;
897 systab.tables = (struct efi_configuration_table *)ptr;
898 }
Alexander Graf77256092016-08-16 21:08:45 +0200899 }
900
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000901 /* Relocate the runtime. See TODO above */
Alexander Graf0bd425a2016-03-04 01:10:01 +0100902 for (i = 0; i < n; i++) {
903 struct efi_mem_desc *map;
904
905 map = (void*)virtmap + (descriptor_size * i);
906 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
Alexander Graf77256092016-08-16 21:08:45 +0200907 ulong new_offset = map->virtual_start -
Alexander Graf86e00212018-12-11 10:00:42 +0100908 map->physical_start + gd->relocaddr;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100909
Heinrich Schuchardt19cd80b2019-06-20 22:00:02 +0000910 efi_relocate_runtime_table(new_offset);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100911 efi_runtime_relocate(new_offset, map);
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200912 ret = EFI_SUCCESS;
913 goto out;
Alexander Graf0bd425a2016-03-04 01:10:01 +0100914 }
915 }
916
Heinrich Schuchardtc70a1612019-08-14 05:19:37 +0200917out:
918 return EFI_EXIT(ret);
Alexander Graf0bd425a2016-03-04 01:10:01 +0100919}
920
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200921/**
922 * efi_add_runtime_mmio() - add memory-mapped IO region
923 *
924 * This function adds a memory-mapped IO region to the memory map to make it
925 * available at runtime.
926 *
Heinrich Schuchardt7e307642018-12-23 02:35:13 +0100927 * @mmio_ptr: pointer to a pointer to the start of the memory-mapped
928 * IO region
Heinrich Schuchardt11121542018-09-03 19:29:41 +0200929 * @len: size of the memory-mapped IO region
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200930 * Returns: status code
931 */
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100932efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
Alexander Graf77256092016-08-16 21:08:45 +0200933{
934 struct efi_runtime_mmio_list *newmmio;
Alexander Graf33a83a32018-03-15 15:08:16 +0100935 uint64_t addr = *(uintptr_t *)mmio_ptr;
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100936 efi_status_t ret;
Alexander Graf33a83a32018-03-15 15:08:16 +0100937
Michael Walle282d3862020-05-17 12:29:19 +0200938 ret = efi_add_memory_map(addr, len, EFI_MMAP_IO);
Bryan O'Donoghue89a22b42019-07-15 12:00:39 +0100939 if (ret != EFI_SUCCESS)
Alexander Graf33a83a32018-03-15 15:08:16 +0100940 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200941
942 newmmio = calloc(1, sizeof(*newmmio));
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100943 if (!newmmio)
944 return EFI_OUT_OF_RESOURCES;
Alexander Graf77256092016-08-16 21:08:45 +0200945 newmmio->ptr = mmio_ptr;
946 newmmio->paddr = *(uintptr_t *)mmio_ptr;
947 newmmio->len = len;
948 list_add_tail(&newmmio->link, &efi_runtime_mmio);
Heinrich Schuchardt099b3b72018-03-03 15:28:59 +0100949
Alexander Graf33a83a32018-03-15 15:08:16 +0100950 return EFI_SUCCESS;
Alexander Graf77256092016-08-16 21:08:45 +0200951}
952
Alexander Graf0bd425a2016-03-04 01:10:01 +0100953/*
954 * In the second stage, U-Boot has disappeared. To isolate our runtime code
955 * that at this point still exists from the rest, we put it into a special
956 * section.
957 *
958 * !!WARNING!!
959 *
960 * This means that we can not rely on any code outside of this file in any
961 * function or variable below this line.
962 *
963 * Please keep everything fully self-contained and annotated with
Alexander Graf393dd912016-10-14 13:45:30 +0200964 * __efi_runtime and __efi_runtime_data markers.
Alexander Graf0bd425a2016-03-04 01:10:01 +0100965 */
966
967/*
968 * Relocate the EFI runtime stub to a different place. We need to call this
969 * the first time we expose the runtime interface to a user and on set virtual
970 * address map calls.
971 */
972
Heinrich Schuchardt711853b2018-07-29 15:10:11 +0200973/**
974 * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED
975 *
976 * This function is used after SetVirtualAddressMap() is called as replacement
977 * for services that are not available anymore due to constraints of the U-Boot
978 * implementation.
979 *
980 * Return: EFI_UNSUPPORTED
981 */
Alexander Graf393dd912016-10-14 13:45:30 +0200982static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
Alexander Graf0bd425a2016-03-04 01:10:01 +0100983{
984 return EFI_UNSUPPORTED;
985}
986
Alexander Graf393dd912016-10-14 13:45:30 +0200987struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
Alexander Graf0bd425a2016-03-04 01:10:01 +0100988 .hdr = {
989 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +0200990 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +0200991 .headersize = sizeof(struct efi_runtime_services),
Alexander Graf0bd425a2016-03-04 01:10:01 +0100992 },
Alexander Graf77256092016-08-16 21:08:45 +0200993 .get_time = &efi_get_time_boottime,
Heinrich Schuchardt411b2d52019-05-19 20:07:39 +0200994 .set_time = &efi_set_time_boottime,
Alexander Graf0bd425a2016-03-04 01:10:01 +0100995 .get_wakeup_time = (void *)&efi_unimplemented,
996 .set_wakeup_time = (void *)&efi_unimplemented,
997 .set_virtual_address_map = &efi_set_virtual_address_map,
Heinrich Schuchardtc680dc42019-07-27 20:35:24 +0200998 .convert_pointer = efi_convert_pointer,
Rob Clark15f3d742017-09-13 18:05:37 -0400999 .get_variable = efi_get_variable,
Heinrich Schuchardtd6a6baa2018-05-17 07:57:05 +02001000 .get_next_variable_name = efi_get_next_variable_name,
Rob Clark15f3d742017-09-13 18:05:37 -04001001 .set_variable = efi_set_variable,
Heinrich Schuchardt5337a6c2019-06-20 15:40:49 +02001002 .get_next_high_mono_count = (void *)&efi_unimplemented,
Alexander Graf77256092016-08-16 21:08:45 +02001003 .reset_system = &efi_reset_system_boottime,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +09001004#ifdef CONFIG_EFI_RUNTIME_UPDATE_CAPSULE
Heinrich Schuchardt897c0722018-02-09 20:41:21 +01001005 .update_capsule = efi_update_capsule,
1006 .query_capsule_caps = efi_query_capsule_caps,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +09001007#else
1008 .update_capsule = efi_update_capsule_unsupported,
1009 .query_capsule_caps = efi_query_capsule_caps_unsupported,
1010#endif
Heinrich Schuchardt897c0722018-02-09 20:41:21 +01001011 .query_variable_info = efi_query_variable_info,
Alexander Graf0bd425a2016-03-04 01:10:01 +01001012};