Tom Rini | f739fcd | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application runtime services |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 10 | #include <cpu_func.h> |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 11 | #include <dm.h> |
Alexander Graf | b34662d | 2018-06-18 17:23:11 +0200 | [diff] [blame] | 12 | #include <elf.h> |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 13 | #include <efi_loader.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 16 | #include <rtc.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Simon Glass | 3db7110 | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 18 | #include <u-boot/crc.h> |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 19 | |
| 20 | /* For manual relocation support */ |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Heinrich Schuchardt | 76be687 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 23 | /* GUID of the runtime properties table */ |
| 24 | static const efi_guid_t efi_rt_properties_table_guid = |
| 25 | EFI_RT_PROPERTIES_TABLE_GUID; |
| 26 | |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 27 | struct efi_runtime_mmio_list { |
| 28 | struct list_head link; |
| 29 | void **ptr; |
| 30 | u64 paddr; |
| 31 | u64 len; |
| 32 | }; |
| 33 | |
| 34 | /* This list contains all runtime available mmio regions */ |
Bin Meng | 6fc4fc3 | 2023-04-05 20:15:19 +0800 | [diff] [blame] | 35 | static LIST_HEAD(efi_runtime_mmio); |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 36 | |
Alexander Graf | 3c63db9 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 37 | static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void); |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 38 | |
Simon Glass | 2d2b5b2 | 2018-06-11 23:26:40 -0600 | [diff] [blame] | 39 | /* |
Heinrich Schuchardt | 250b325 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 40 | * TODO(sjg@chromium.org): These defines and structures should come from the ELF |
| 41 | * header for each architecture (or a generic header) rather than being repeated |
| 42 | * here. |
Simon Glass | 2d2b5b2 | 2018-06-11 23:26:40 -0600 | [diff] [blame] | 43 | */ |
Alexander Graf | bc02891 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 44 | #if defined(__aarch64__) |
Alexander Graf | b34662d | 2018-06-18 17:23:11 +0200 | [diff] [blame] | 45 | #define R_RELATIVE R_AARCH64_RELATIVE |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 46 | #define R_MASK 0xffffffffULL |
| 47 | #define IS_RELA 1 |
Alexander Graf | bc02891 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 48 | #elif defined(__arm__) |
Alexander Graf | b34662d | 2018-06-18 17:23:11 +0200 | [diff] [blame] | 49 | #define R_RELATIVE R_ARM_RELATIVE |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 50 | #define R_MASK 0xffULL |
Heinrich Schuchardt | 3ce7829 | 2018-10-13 20:52:08 -0700 | [diff] [blame] | 51 | #elif defined(__i386__) |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 52 | #define R_RELATIVE R_386_RELATIVE |
| 53 | #define R_MASK 0xffULL |
Heinrich Schuchardt | 3ce7829 | 2018-10-13 20:52:08 -0700 | [diff] [blame] | 54 | #elif defined(__x86_64__) |
| 55 | #define R_RELATIVE R_X86_64_RELATIVE |
| 56 | #define R_MASK 0xffffffffULL |
| 57 | #define IS_RELA 1 |
Alexander Graf | bc02891 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 58 | #elif defined(__riscv) |
Rick Chen | 6836adb | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 59 | #define R_RELATIVE R_RISCV_RELATIVE |
| 60 | #define R_MASK 0xffULL |
| 61 | #define IS_RELA 1 |
| 62 | |
| 63 | struct dyn_sym { |
| 64 | ulong foo1; |
| 65 | ulong addr; |
| 66 | u32 foo2; |
| 67 | u32 foo3; |
| 68 | }; |
Alexander Graf | bc02891 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 69 | #if (__riscv_xlen == 32) |
Rick Chen | 6836adb | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 70 | #define R_ABSOLUTE R_RISCV_32 |
| 71 | #define SYM_INDEX 8 |
Alexander Graf | bc02891 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 72 | #elif (__riscv_xlen == 64) |
Rick Chen | 6836adb | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 73 | #define R_ABSOLUTE R_RISCV_64 |
| 74 | #define SYM_INDEX 32 |
Alexander Graf | bc02891 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 75 | #else |
| 76 | #error unknown riscv target |
Rick Chen | 6836adb | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 77 | #endif |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 78 | #else |
| 79 | #error Need to add relocation awareness |
| 80 | #endif |
| 81 | |
| 82 | struct elf_rel { |
| 83 | ulong *offset; |
| 84 | ulong info; |
| 85 | }; |
| 86 | |
| 87 | struct elf_rela { |
| 88 | ulong *offset; |
| 89 | ulong info; |
| 90 | long addend; |
| 91 | }; |
| 92 | |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 93 | static __efi_runtime_data struct efi_mem_desc *efi_virtmap; |
| 94 | static __efi_runtime_data efi_uintn_t efi_descriptor_count; |
| 95 | static __efi_runtime_data efi_uintn_t efi_descriptor_size; |
| 96 | |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 97 | /* |
Heinrich Schuchardt | 250b325 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 98 | * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 99 | * payload are running concurrently at the same time. In this mode, we can |
| 100 | * handle a good number of runtime callbacks |
| 101 | */ |
| 102 | |
Heinrich Schuchardt | 76be687 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 103 | /** |
| 104 | * efi_init_runtime_supported() - create runtime properties table |
| 105 | * |
| 106 | * Create a configuration table specifying which services are available at |
| 107 | * runtime. |
| 108 | * |
| 109 | * Return: status code |
| 110 | */ |
AKASHI Takahiro | e771b4b | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 111 | efi_status_t efi_init_runtime_supported(void) |
| 112 | { |
Heinrich Schuchardt | 76be687 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 113 | efi_status_t ret; |
| 114 | struct efi_rt_properties_table *rt_table; |
| 115 | |
| 116 | ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA, |
| 117 | sizeof(struct efi_rt_properties_table), |
| 118 | (void **)&rt_table); |
| 119 | if (ret != EFI_SUCCESS) |
| 120 | return ret; |
| 121 | |
| 122 | rt_table->version = EFI_RT_PROPERTIES_TABLE_VERSION; |
| 123 | rt_table->length = sizeof(struct efi_rt_properties_table); |
| 124 | rt_table->runtime_services_supported = |
Heinrich Schuchardt | b02a707 | 2020-03-24 19:54:53 +0000 | [diff] [blame] | 125 | EFI_RT_SUPPORTED_GET_VARIABLE | |
| 126 | EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME | |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 127 | EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP | |
| 128 | EFI_RT_SUPPORTED_CONVERT_POINTER; |
AKASHI Takahiro | e771b4b | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * This value must be synced with efi_runtime_detach_list |
| 132 | * as well as efi_runtime_services. |
| 133 | */ |
Heinrich Schuchardt | 953661a | 2019-07-05 18:12:16 +0200 | [diff] [blame] | 134 | #ifdef CONFIG_EFI_HAVE_RUNTIME_RESET |
Heinrich Schuchardt | 76be687 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 135 | rt_table->runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM; |
AKASHI Takahiro | e771b4b | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 136 | #endif |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 137 | |
Heinrich Schuchardt | 76be687 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 138 | ret = efi_install_configuration_table(&efi_rt_properties_table_guid, |
| 139 | rt_table); |
| 140 | return ret; |
AKASHI Takahiro | e771b4b | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 141 | } |
| 142 | |
Heinrich Schuchardt | a39f39c | 2018-07-29 09:49:04 +0200 | [diff] [blame] | 143 | /** |
Heinrich Schuchardt | b0dd8cb | 2020-06-28 16:30:29 +0200 | [diff] [blame] | 144 | * efi_memcpy_runtime() - copy memory area |
| 145 | * |
| 146 | * At runtime memcpy() is not available. |
| 147 | * |
Heinrich Schuchardt | ebbad02 | 2020-07-22 07:56:14 +0200 | [diff] [blame] | 148 | * Overlapping memory areas can be copied safely if src >= dest. |
| 149 | * |
Heinrich Schuchardt | b0dd8cb | 2020-06-28 16:30:29 +0200 | [diff] [blame] | 150 | * @dest: destination buffer |
| 151 | * @src: source buffer |
| 152 | * @n: number of bytes to copy |
| 153 | * Return: pointer to destination buffer |
| 154 | */ |
| 155 | void __efi_runtime efi_memcpy_runtime(void *dest, const void *src, size_t n) |
| 156 | { |
| 157 | u8 *d = dest; |
| 158 | const u8 *s = src; |
| 159 | |
| 160 | for (; n; --n) |
| 161 | *d++ = *s++; |
| 162 | } |
| 163 | |
| 164 | /** |
Heinrich Schuchardt | a39f39c | 2018-07-29 09:49:04 +0200 | [diff] [blame] | 165 | * efi_update_table_header_crc32() - Update crc32 in table header |
| 166 | * |
| 167 | * @table: EFI table |
| 168 | */ |
| 169 | void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table) |
| 170 | { |
| 171 | table->crc32 = 0; |
| 172 | table->crc32 = crc32(0, (const unsigned char *)table, |
| 173 | table->headersize); |
| 174 | } |
| 175 | |
Heinrich Schuchardt | bcfb0e2 | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 176 | /** |
Heinrich Schuchardt | 250b325 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 177 | * efi_reset_system_boottime() - reset system at boot time |
Heinrich Schuchardt | bcfb0e2 | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 178 | * |
| 179 | * This function implements the ResetSystem() runtime service before |
| 180 | * SetVirtualAddressMap() is called. |
| 181 | * |
| 182 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 183 | * details. |
| 184 | * |
| 185 | * @reset_type: type of reset to perform |
| 186 | * @reset_status: status code for the reset |
| 187 | * @data_size: size of reset_data |
| 188 | * @reset_data: information about the reset |
| 189 | */ |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 190 | static void EFIAPI efi_reset_system_boottime( |
| 191 | enum efi_reset_type reset_type, |
| 192 | efi_status_t reset_status, |
| 193 | unsigned long data_size, void *reset_data) |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 194 | { |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 195 | struct efi_event *evt; |
| 196 | |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 197 | EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size, |
| 198 | reset_data); |
| 199 | |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 200 | /* Notify reset */ |
| 201 | list_for_each_entry(evt, &efi_events, link) { |
| 202 | if (evt->group && |
| 203 | !guidcmp(evt->group, |
| 204 | &efi_guid_event_group_reset_system)) { |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 205 | efi_signal_event(evt); |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 206 | break; |
| 207 | } |
| 208 | } |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 209 | switch (reset_type) { |
| 210 | case EFI_RESET_COLD: |
| 211 | case EFI_RESET_WARM: |
Heinrich Schuchardt | 482fc90 | 2018-02-06 22:00:22 +0100 | [diff] [blame] | 212 | case EFI_RESET_PLATFORM_SPECIFIC: |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 213 | do_reset(NULL, 0, 0, NULL); |
| 214 | break; |
| 215 | case EFI_RESET_SHUTDOWN: |
Heinrich Schuchardt | e706ed7 | 2018-10-16 07:44:53 +0200 | [diff] [blame] | 216 | #ifdef CONFIG_CMD_POWEROFF |
| 217 | do_poweroff(NULL, 0, 0, NULL); |
| 218 | #endif |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 219 | break; |
| 220 | } |
| 221 | |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 222 | while (1) { } |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Heinrich Schuchardt | 49de245 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 225 | /** |
Heinrich Schuchardt | 250b325 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 226 | * efi_get_time_boottime() - get current time at boot time |
Heinrich Schuchardt | 49de245 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 227 | * |
Heinrich Schuchardt | bcfb0e2 | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 228 | * This function implements the GetTime runtime service before |
| 229 | * SetVirtualAddressMap() is called. |
| 230 | * |
Heinrich Schuchardt | 49de245 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 231 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 232 | * for details. |
| 233 | * |
| 234 | * @time: pointer to structure to receive current time |
| 235 | * @capabilities: pointer to structure to receive RTC properties |
Heinrich Schuchardt | bcfb0e2 | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 236 | * Returns: status code |
Heinrich Schuchardt | 49de245 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 237 | */ |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 238 | static efi_status_t EFIAPI efi_get_time_boottime( |
| 239 | struct efi_time *time, |
| 240 | struct efi_time_cap *capabilities) |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 241 | { |
Heinrich Schuchardt | 5ec48e3 | 2019-05-31 22:56:02 +0200 | [diff] [blame] | 242 | #ifdef CONFIG_EFI_GET_TIME |
Heinrich Schuchardt | 49de245 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 243 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 49de245 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 244 | struct rtc_time tm; |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 245 | struct udevice *dev; |
| 246 | |
| 247 | EFI_ENTRY("%p %p", time, capabilities); |
| 248 | |
Heinrich Schuchardt | 49de245 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 249 | if (!time) { |
| 250 | ret = EFI_INVALID_PARAMETER; |
| 251 | goto out; |
| 252 | } |
Heinrich Schuchardt | bb2b13d | 2019-05-19 21:41:28 +0200 | [diff] [blame] | 253 | if (uclass_get_device(UCLASS_RTC, 0, &dev) || |
| 254 | dm_rtc_get(dev, &tm)) { |
| 255 | ret = EFI_UNSUPPORTED; |
| 256 | goto out; |
| 257 | } |
| 258 | if (dm_rtc_get(dev, &tm)) { |
Heinrich Schuchardt | 49de245 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 259 | ret = EFI_DEVICE_ERROR; |
| 260 | goto out; |
| 261 | } |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 262 | |
| 263 | memset(time, 0, sizeof(*time)); |
| 264 | time->year = tm.tm_year; |
| 265 | time->month = tm.tm_mon; |
| 266 | time->day = tm.tm_mday; |
| 267 | time->hour = tm.tm_hour; |
| 268 | time->minute = tm.tm_min; |
Heinrich Schuchardt | 49de245 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 269 | time->second = tm.tm_sec; |
Heinrich Schuchardt | 0eae552 | 2020-10-23 05:30:29 +0200 | [diff] [blame] | 270 | if (tm.tm_isdst > 0) |
Heinrich Schuchardt | 38b9a79 | 2019-05-31 22:08:45 +0200 | [diff] [blame] | 271 | time->daylight = |
| 272 | EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT; |
Heinrich Schuchardt | 0eae552 | 2020-10-23 05:30:29 +0200 | [diff] [blame] | 273 | else if (!tm.tm_isdst) |
| 274 | time->daylight = EFI_TIME_ADJUST_DAYLIGHT; |
| 275 | else |
| 276 | time->daylight = 0; |
Heinrich Schuchardt | 49de245 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 277 | time->timezone = EFI_UNSPECIFIED_TIMEZONE; |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 278 | |
Heinrich Schuchardt | 49de245 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 279 | if (capabilities) { |
| 280 | /* Set reasonable dummy values */ |
| 281 | capabilities->resolution = 1; /* 1 Hz */ |
| 282 | capabilities->accuracy = 100000000; /* 100 ppm */ |
| 283 | capabilities->sets_to_zero = false; |
| 284 | } |
| 285 | out: |
| 286 | return EFI_EXIT(ret); |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 287 | #else |
Heinrich Schuchardt | 49de245 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 288 | EFI_ENTRY("%p %p", time, capabilities); |
Heinrich Schuchardt | bb2b13d | 2019-05-19 21:41:28 +0200 | [diff] [blame] | 289 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 290 | #endif |
| 291 | } |
| 292 | |
Heinrich Schuchardt | 5ec48e3 | 2019-05-31 22:56:02 +0200 | [diff] [blame] | 293 | #ifdef CONFIG_EFI_SET_TIME |
Heinrich Schuchardt | e6bcc35 | 2019-05-31 07:35:19 +0200 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * efi_validate_time() - checks if timestamp is valid |
| 297 | * |
| 298 | * @time: timestamp to validate |
| 299 | * Returns: 0 if timestamp is valid, 1 otherwise |
| 300 | */ |
| 301 | static int efi_validate_time(struct efi_time *time) |
| 302 | { |
| 303 | return (!time || |
| 304 | time->year < 1900 || time->year > 9999 || |
| 305 | !time->month || time->month > 12 || !time->day || |
| 306 | time->day > rtc_month_days(time->month - 1, time->year) || |
| 307 | time->hour > 23 || time->minute > 59 || time->second > 59 || |
| 308 | time->nanosecond > 999999999 || |
| 309 | time->daylight & |
| 310 | ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) || |
| 311 | ((time->timezone < -1440 || time->timezone > 1440) && |
| 312 | time->timezone != EFI_UNSPECIFIED_TIMEZONE)); |
| 313 | } |
| 314 | |
| 315 | #endif |
| 316 | |
Heinrich Schuchardt | 433bfe7 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 317 | /** |
| 318 | * efi_set_time_boottime() - set current time |
| 319 | * |
| 320 | * This function implements the SetTime() runtime service before |
| 321 | * SetVirtualAddressMap() is called. |
| 322 | * |
| 323 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 324 | * for details. |
| 325 | * |
| 326 | * @time: pointer to structure to with current time |
| 327 | * Returns: status code |
| 328 | */ |
| 329 | static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time) |
| 330 | { |
Heinrich Schuchardt | 5ec48e3 | 2019-05-31 22:56:02 +0200 | [diff] [blame] | 331 | #ifdef CONFIG_EFI_SET_TIME |
Heinrich Schuchardt | 433bfe7 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 332 | efi_status_t ret = EFI_SUCCESS; |
| 333 | struct rtc_time tm; |
| 334 | struct udevice *dev; |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 335 | |
Heinrich Schuchardt | 433bfe7 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 336 | EFI_ENTRY("%p", time); |
| 337 | |
Heinrich Schuchardt | e6bcc35 | 2019-05-31 07:35:19 +0200 | [diff] [blame] | 338 | if (efi_validate_time(time)) { |
Heinrich Schuchardt | 433bfe7 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 339 | ret = EFI_INVALID_PARAMETER; |
| 340 | goto out; |
| 341 | } |
| 342 | |
| 343 | if (uclass_get_device(UCLASS_RTC, 0, &dev)) { |
| 344 | ret = EFI_UNSUPPORTED; |
| 345 | goto out; |
| 346 | } |
| 347 | |
| 348 | memset(&tm, 0, sizeof(tm)); |
| 349 | tm.tm_year = time->year; |
| 350 | tm.tm_mon = time->month; |
| 351 | tm.tm_mday = time->day; |
| 352 | tm.tm_hour = time->hour; |
| 353 | tm.tm_min = time->minute; |
| 354 | tm.tm_sec = time->second; |
Heinrich Schuchardt | 0eae552 | 2020-10-23 05:30:29 +0200 | [diff] [blame] | 355 | switch (time->daylight) { |
| 356 | case EFI_TIME_ADJUST_DAYLIGHT: |
| 357 | tm.tm_isdst = 0; |
| 358 | break; |
| 359 | case EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT: |
| 360 | tm.tm_isdst = 1; |
| 361 | break; |
| 362 | default: |
| 363 | tm.tm_isdst = -1; |
| 364 | break; |
| 365 | } |
Heinrich Schuchardt | 433bfe7 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 366 | /* Calculate day of week */ |
| 367 | rtc_calc_weekday(&tm); |
| 368 | |
| 369 | if (dm_rtc_set(dev, &tm)) |
| 370 | ret = EFI_DEVICE_ERROR; |
| 371 | out: |
| 372 | return EFI_EXIT(ret); |
| 373 | #else |
| 374 | EFI_ENTRY("%p", time); |
| 375 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 376 | #endif |
| 377 | } |
Heinrich Schuchardt | bcfb0e2 | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 378 | /** |
| 379 | * efi_reset_system() - reset system |
| 380 | * |
| 381 | * This function implements the ResetSystem() runtime service after |
Heinrich Schuchardt | f03a879 | 2020-08-22 08:29:53 +0200 | [diff] [blame] | 382 | * SetVirtualAddressMap() is called. As this placeholder cannot reset the |
| 383 | * system it simply return to the caller. |
| 384 | * |
Heinrich Schuchardt | bcfb0e2 | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 385 | * Boards may override the helpers below to implement reset functionality. |
| 386 | * |
| 387 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 388 | * details. |
| 389 | * |
| 390 | * @reset_type: type of reset to perform |
| 391 | * @reset_status: status code for the reset |
| 392 | * @data_size: size of reset_data |
| 393 | * @reset_data: information about the reset |
| 394 | */ |
Alexander Graf | 3c63db9 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 395 | void __weak __efi_runtime EFIAPI efi_reset_system( |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 396 | enum efi_reset_type reset_type, |
| 397 | efi_status_t reset_status, |
| 398 | unsigned long data_size, void *reset_data) |
| 399 | { |
Heinrich Schuchardt | f03a879 | 2020-08-22 08:29:53 +0200 | [diff] [blame] | 400 | return; |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Heinrich Schuchardt | bcfb0e2 | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 403 | /** |
| 404 | * efi_reset_system_init() - initialize the reset driver |
| 405 | * |
| 406 | * Boards may override this function to initialize the reset driver. |
| 407 | */ |
Heinrich Schuchardt | 22c793e | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 408 | efi_status_t __weak efi_reset_system_init(void) |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 409 | { |
Heinrich Schuchardt | 22c793e | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 410 | return EFI_SUCCESS; |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 411 | } |
| 412 | |
Heinrich Schuchardt | bcfb0e2 | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 413 | /** |
| 414 | * efi_get_time() - get current time |
| 415 | * |
| 416 | * This function implements the GetTime runtime service after |
| 417 | * SetVirtualAddressMap() is called. As the U-Boot driver are not available |
| 418 | * anymore only an error code is returned. |
| 419 | * |
| 420 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 421 | * for details. |
| 422 | * |
| 423 | * @time: pointer to structure to receive current time |
| 424 | * @capabilities: pointer to structure to receive RTC properties |
| 425 | * Returns: status code |
| 426 | */ |
Alexander Graf | 3c63db9 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 427 | efi_status_t __weak __efi_runtime EFIAPI efi_get_time( |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 428 | struct efi_time *time, |
| 429 | struct efi_time_cap *capabilities) |
| 430 | { |
Heinrich Schuchardt | c5b63be | 2019-06-13 18:42:40 +0200 | [diff] [blame] | 431 | return EFI_UNSUPPORTED; |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Heinrich Schuchardt | 433bfe7 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 434 | /** |
| 435 | * efi_set_time() - set current time |
| 436 | * |
| 437 | * This function implements the SetTime runtime service after |
| 438 | * SetVirtualAddressMap() is called. As the U-Boot driver are not available |
| 439 | * anymore only an error code is returned. |
| 440 | * |
| 441 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 442 | * for details. |
| 443 | * |
| 444 | * @time: pointer to structure to with current time |
| 445 | * Returns: status code |
| 446 | */ |
| 447 | efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time) |
| 448 | { |
| 449 | return EFI_UNSUPPORTED; |
| 450 | } |
| 451 | |
Heinrich Schuchardt | 24a238f | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 452 | /** |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 453 | * efi_update_capsule_unsupported() - process information from operating system |
| 454 | * |
| 455 | * This function implements the UpdateCapsule() runtime service. |
| 456 | * |
| 457 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 458 | * details. |
| 459 | * |
| 460 | * @capsule_header_array: pointer to array of virtual pointers |
| 461 | * @capsule_count: number of pointers in capsule_header_array |
| 462 | * @scatter_gather_list: pointer to array of physical pointers |
| 463 | * Returns: status code |
| 464 | */ |
Heinrich Schuchardt | 6c2377f | 2023-02-10 08:56:06 +0100 | [diff] [blame] | 465 | static efi_status_t __efi_runtime EFIAPI efi_update_capsule_unsupported( |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 466 | struct efi_capsule_header **capsule_header_array, |
| 467 | efi_uintn_t capsule_count, |
| 468 | u64 scatter_gather_list) |
| 469 | { |
| 470 | return EFI_UNSUPPORTED; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * efi_query_capsule_caps_unsupported() - check if capsule is supported |
| 475 | * |
| 476 | * This function implements the QueryCapsuleCapabilities() runtime service. |
| 477 | * |
| 478 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 479 | * details. |
| 480 | * |
| 481 | * @capsule_header_array: pointer to array of virtual pointers |
| 482 | * @capsule_count: number of pointers in capsule_header_array |
| 483 | * @maximum_capsule_size: maximum capsule size |
| 484 | * @reset_type: type of reset needed for capsule update |
| 485 | * Returns: status code |
| 486 | */ |
Heinrich Schuchardt | 6c2377f | 2023-02-10 08:56:06 +0100 | [diff] [blame] | 487 | static efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps_unsupported( |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 488 | struct efi_capsule_header **capsule_header_array, |
| 489 | efi_uintn_t capsule_count, |
| 490 | u64 *maximum_capsule_size, |
| 491 | u32 *reset_type) |
| 492 | { |
| 493 | return EFI_UNSUPPORTED; |
| 494 | } |
| 495 | |
| 496 | /** |
Heinrich Schuchardt | 24a238f | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 497 | * efi_is_runtime_service_pointer() - check if pointer points to runtime table |
| 498 | * |
| 499 | * @p: pointer to check |
| 500 | * Return: true if the pointer points to a service function pointer in the |
| 501 | * runtime table |
| 502 | */ |
| 503 | static bool efi_is_runtime_service_pointer(void *p) |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 504 | { |
Heinrich Schuchardt | 14b4048 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 505 | return (p >= (void *)&efi_runtime_services.get_time && |
| 506 | p <= (void *)&efi_runtime_services.query_variable_info) || |
| 507 | p == (void *)&efi_events.prev || |
| 508 | p == (void *)&efi_events.next; |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 509 | } |
| 510 | |
Heinrich Schuchardt | b23ffcb | 2019-07-05 18:12:21 +0200 | [diff] [blame] | 511 | /** |
| 512 | * efi_runtime_detach() - detach unimplemented runtime functions |
| 513 | */ |
Heinrich Schuchardt | 7f95104 | 2019-07-05 17:42:16 +0200 | [diff] [blame] | 514 | void efi_runtime_detach(void) |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 515 | { |
Heinrich Schuchardt | b23ffcb | 2019-07-05 18:12:21 +0200 | [diff] [blame] | 516 | efi_runtime_services.reset_system = efi_reset_system; |
| 517 | efi_runtime_services.get_time = efi_get_time; |
| 518 | efi_runtime_services.set_time = efi_set_time; |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 519 | if (IS_ENABLED(CONFIG_EFI_RUNTIME_UPDATE_CAPSULE)) { |
| 520 | /* won't support at runtime */ |
| 521 | efi_runtime_services.update_capsule = |
| 522 | efi_update_capsule_unsupported; |
| 523 | efi_runtime_services.query_capsule_caps = |
| 524 | efi_query_capsule_caps_unsupported; |
| 525 | } |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 526 | |
Heinrich Schuchardt | b23ffcb | 2019-07-05 18:12:21 +0200 | [diff] [blame] | 527 | /* Update CRC32 */ |
| 528 | efi_update_table_header_crc32(&efi_runtime_services.hdr); |
Heinrich Schuchardt | 24a238f | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Heinrich Schuchardt | ee8ebaa | 2019-06-29 03:32:52 +0200 | [diff] [blame] | 531 | /** |
| 532 | * efi_set_virtual_address_map_runtime() - change from physical to virtual |
| 533 | * mapping |
| 534 | * |
| 535 | * This function implements the SetVirtualAddressMap() runtime service after |
| 536 | * it is first called. |
| 537 | * |
| 538 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 539 | * details. |
| 540 | * |
| 541 | * @memory_map_size: size of the virtual map |
| 542 | * @descriptor_size: size of an entry in the map |
| 543 | * @descriptor_version: version of the map entries |
| 544 | * @virtmap: virtual address mapping information |
| 545 | * Return: status code EFI_UNSUPPORTED |
| 546 | */ |
Heinrich Schuchardt | 9618560 | 2019-07-12 22:41:43 +0200 | [diff] [blame] | 547 | static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map_runtime( |
Heinrich Schuchardt | 24e6722 | 2019-07-27 20:28:47 +0200 | [diff] [blame] | 548 | efi_uintn_t memory_map_size, |
| 549 | efi_uintn_t descriptor_size, |
Heinrich Schuchardt | ee8ebaa | 2019-06-29 03:32:52 +0200 | [diff] [blame] | 550 | uint32_t descriptor_version, |
| 551 | struct efi_mem_desc *virtmap) |
| 552 | { |
| 553 | return EFI_UNSUPPORTED; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * efi_convert_pointer_runtime() - convert from physical to virtual pointer |
| 558 | * |
| 559 | * This function implements the ConvertPointer() runtime service after |
| 560 | * the first call to SetVirtualAddressMap(). |
| 561 | * |
| 562 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 563 | * details. |
| 564 | * |
| 565 | * @debug_disposition: indicates if pointer may be converted to NULL |
| 566 | * @address: pointer to be converted |
| 567 | * Return: status code EFI_UNSUPPORTED |
| 568 | */ |
| 569 | static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime( |
| 570 | efi_uintn_t debug_disposition, void **address) |
| 571 | { |
| 572 | return EFI_UNSUPPORTED; |
| 573 | } |
| 574 | |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 575 | /** |
Heinrich Schuchardt | 7aeceff | 2020-03-22 08:28:15 +0100 | [diff] [blame] | 576 | * efi_convert_pointer() - convert from physical to virtual pointer |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 577 | * |
| 578 | * This function implements the ConvertPointer() runtime service until |
| 579 | * the first call to SetVirtualAddressMap(). |
| 580 | * |
| 581 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 582 | * details. |
| 583 | * |
| 584 | * @debug_disposition: indicates if pointer may be converted to NULL |
| 585 | * @address: pointer to be converted |
Heinrich Schuchardt | 7aeceff | 2020-03-22 08:28:15 +0100 | [diff] [blame] | 586 | * Return: status code |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 587 | */ |
Heinrich Schuchardt | a44d2a2 | 2020-03-24 18:05:22 +0100 | [diff] [blame] | 588 | __efi_runtime efi_status_t EFIAPI |
| 589 | efi_convert_pointer(efi_uintn_t debug_disposition, void **address) |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 590 | { |
Heinrich Schuchardt | a27c78f | 2020-07-07 03:10:12 +0200 | [diff] [blame] | 591 | efi_physical_addr_t addr; |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 592 | efi_uintn_t i; |
| 593 | efi_status_t ret = EFI_NOT_FOUND; |
| 594 | |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 595 | if (!efi_virtmap) { |
| 596 | ret = EFI_UNSUPPORTED; |
| 597 | goto out; |
| 598 | } |
| 599 | |
| 600 | if (!address) { |
| 601 | ret = EFI_INVALID_PARAMETER; |
| 602 | goto out; |
| 603 | } |
Heinrich Schuchardt | 724d281 | 2020-03-24 17:52:40 +0100 | [diff] [blame] | 604 | if (!*address) { |
| 605 | if (debug_disposition & EFI_OPTIONAL_PTR) |
| 606 | return EFI_SUCCESS; |
| 607 | else |
| 608 | return EFI_INVALID_PARAMETER; |
| 609 | } |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 610 | |
Heinrich Schuchardt | a27c78f | 2020-07-07 03:10:12 +0200 | [diff] [blame] | 611 | addr = (uintptr_t)*address; |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 612 | for (i = 0; i < efi_descriptor_count; i++) { |
| 613 | struct efi_mem_desc *map = (void *)efi_virtmap + |
| 614 | (efi_descriptor_size * i); |
| 615 | |
| 616 | if (addr >= map->physical_start && |
| 617 | (addr < map->physical_start |
| 618 | + (map->num_pages << EFI_PAGE_SHIFT))) { |
| 619 | *address = (void *)(uintptr_t) |
| 620 | (addr + map->virtual_start - |
| 621 | map->physical_start); |
| 622 | |
| 623 | ret = EFI_SUCCESS; |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | out: |
Heinrich Schuchardt | a44d2a2 | 2020-03-24 18:05:22 +0100 | [diff] [blame] | 629 | return ret; |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Heinrich Schuchardt | 24a238f | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 632 | static __efi_runtime void efi_relocate_runtime_table(ulong offset) |
| 633 | { |
| 634 | ulong patchoff; |
| 635 | void **pos; |
| 636 | |
| 637 | /* Relocate the runtime services pointers */ |
| 638 | patchoff = offset - gd->relocaddr; |
| 639 | for (pos = (void **)&efi_runtime_services.get_time; |
| 640 | pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) { |
Heinrich Schuchardt | ee8ebaa | 2019-06-29 03:32:52 +0200 | [diff] [blame] | 641 | if (*pos) |
Heinrich Schuchardt | 24a238f | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 642 | *pos += patchoff; |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 643 | } |
Heinrich Schuchardt | a39f39c | 2018-07-29 09:49:04 +0200 | [diff] [blame] | 644 | |
Heinrich Schuchardt | ee8ebaa | 2019-06-29 03:32:52 +0200 | [diff] [blame] | 645 | /* |
| 646 | * The entry for SetVirtualAddress() must point to a physical address. |
| 647 | * After the first execution the service must return EFI_UNSUPPORTED. |
| 648 | */ |
| 649 | efi_runtime_services.set_virtual_address_map = |
| 650 | &efi_set_virtual_address_map_runtime; |
| 651 | |
| 652 | /* |
| 653 | * The entry for ConvertPointer() must point to a physical address. |
| 654 | * The service is not usable after SetVirtualAddress(). |
| 655 | */ |
| 656 | efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime; |
| 657 | |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 658 | /* |
| 659 | * TODO: Update UEFI variable RuntimeServicesSupported removing flags |
| 660 | * EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP and |
| 661 | * EFI_RT_SUPPORTED_CONVERT_POINTER as required by the UEFI spec 2.8. |
| 662 | */ |
| 663 | |
Heinrich Schuchardt | 250b325 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 664 | /* Update CRC32 */ |
Heinrich Schuchardt | a39f39c | 2018-07-29 09:49:04 +0200 | [diff] [blame] | 665 | efi_update_table_header_crc32(&efi_runtime_services.hdr); |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /* Relocate EFI runtime to uboot_reloc_base = offset */ |
| 669 | void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map) |
| 670 | { |
| 671 | #ifdef IS_RELA |
| 672 | struct elf_rela *rel = (void*)&__efi_runtime_rel_start; |
| 673 | #else |
| 674 | struct elf_rel *rel = (void*)&__efi_runtime_rel_start; |
Simon Glass | 9846390 | 2022-10-20 18:22:39 -0600 | [diff] [blame] | 675 | static ulong lastoff = CONFIG_TEXT_BASE; |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 676 | #endif |
| 677 | |
Alexander Graf | edcef3b | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 678 | debug("%s: Relocating to offset=%lx\n", __func__, offset); |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 679 | for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) { |
Simon Glass | 9846390 | 2022-10-20 18:22:39 -0600 | [diff] [blame] | 680 | ulong base = CONFIG_TEXT_BASE; |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 681 | ulong *p; |
| 682 | ulong newaddr; |
| 683 | |
| 684 | p = (void*)((ulong)rel->offset - base) + gd->relocaddr; |
| 685 | |
Heinrich Schuchardt | 9f8932d | 2019-08-14 06:49:09 +0200 | [diff] [blame] | 686 | /* |
| 687 | * The runtime services table is updated in |
| 688 | * efi_relocate_runtime_table() |
| 689 | */ |
Heinrich Schuchardt | 24a238f | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 690 | if (map && efi_is_runtime_service_pointer(p)) |
| 691 | continue; |
| 692 | |
Heinrich Schuchardt | 3ce7829 | 2018-10-13 20:52:08 -0700 | [diff] [blame] | 693 | debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, |
| 694 | rel->info, *p, rel->offset); |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 695 | |
Rick Chen | 6836adb | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 696 | switch (rel->info & R_MASK) { |
| 697 | case R_RELATIVE: |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 698 | #ifdef IS_RELA |
Simon Glass | 9846390 | 2022-10-20 18:22:39 -0600 | [diff] [blame] | 699 | newaddr = rel->addend + offset - CONFIG_TEXT_BASE; |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 700 | #else |
| 701 | newaddr = *p - lastoff + offset; |
| 702 | #endif |
Rick Chen | 6836adb | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 703 | break; |
| 704 | #ifdef R_ABSOLUTE |
| 705 | case R_ABSOLUTE: { |
| 706 | ulong symidx = rel->info >> SYM_INDEX; |
| 707 | extern struct dyn_sym __dyn_sym_start[]; |
| 708 | newaddr = __dyn_sym_start[symidx].addr + offset; |
Alexander Graf | afdc4fc | 2018-11-04 22:25:22 +0100 | [diff] [blame] | 709 | #ifdef IS_RELA |
Simon Glass | 9846390 | 2022-10-20 18:22:39 -0600 | [diff] [blame] | 710 | newaddr -= CONFIG_TEXT_BASE; |
Alexander Graf | afdc4fc | 2018-11-04 22:25:22 +0100 | [diff] [blame] | 711 | #endif |
Rick Chen | 6836adb | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 712 | break; |
| 713 | } |
| 714 | #endif |
| 715 | default: |
Heinrich Schuchardt | 24a238f | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 716 | printf("%s: Unknown relocation type %llx\n", |
| 717 | __func__, rel->info & R_MASK); |
Rick Chen | 6836adb | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 718 | continue; |
| 719 | } |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 720 | |
| 721 | /* Check if the relocation is inside bounds */ |
| 722 | if (map && ((newaddr < map->virtual_start) || |
Heinrich Schuchardt | 591cf2e | 2017-09-18 22:11:34 +0200 | [diff] [blame] | 723 | newaddr > (map->virtual_start + |
| 724 | (map->num_pages << EFI_PAGE_SHIFT)))) { |
Heinrich Schuchardt | 24a238f | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 725 | printf("%s: Relocation at %p is out of range (%lx)\n", |
| 726 | __func__, p, newaddr); |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 727 | continue; |
| 728 | } |
| 729 | |
Alexander Graf | edcef3b | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 730 | debug("%s: Setting %p to %lx\n", __func__, p, newaddr); |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 731 | *p = newaddr; |
Alexander Graf | 36c37a8 | 2016-04-11 23:20:39 +0200 | [diff] [blame] | 732 | flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1), |
| 733 | ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE)); |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | #ifndef IS_RELA |
| 737 | lastoff = offset; |
| 738 | #endif |
| 739 | |
| 740 | invalidate_icache_all(); |
| 741 | } |
| 742 | |
Heinrich Schuchardt | bcfb0e2 | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 743 | /** |
| 744 | * efi_set_virtual_address_map() - change from physical to virtual mapping |
| 745 | * |
| 746 | * This function implements the SetVirtualAddressMap() runtime service. |
| 747 | * |
| 748 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 749 | * details. |
| 750 | * |
| 751 | * @memory_map_size: size of the virtual map |
| 752 | * @descriptor_size: size of an entry in the map |
| 753 | * @descriptor_version: version of the map entries |
| 754 | * @virtmap: virtual address mapping information |
| 755 | * Return: status code |
| 756 | */ |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 757 | static efi_status_t EFIAPI efi_set_virtual_address_map( |
Heinrich Schuchardt | 24e6722 | 2019-07-27 20:28:47 +0200 | [diff] [blame] | 758 | efi_uintn_t memory_map_size, |
| 759 | efi_uintn_t descriptor_size, |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 760 | uint32_t descriptor_version, |
| 761 | struct efi_mem_desc *virtmap) |
| 762 | { |
Heinrich Schuchardt | 24e6722 | 2019-07-27 20:28:47 +0200 | [diff] [blame] | 763 | efi_uintn_t n = memory_map_size / descriptor_size; |
| 764 | efi_uintn_t i; |
Heinrich Schuchardt | 53e1d8f | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 765 | efi_status_t ret = EFI_INVALID_PARAMETER; |
Alexander Graf | 5c38e05 | 2018-12-11 10:00:42 +0100 | [diff] [blame] | 766 | int rt_code_sections = 0; |
Heinrich Schuchardt | 14b4048 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 767 | struct efi_event *event; |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 768 | |
Heinrich Schuchardt | 24e6722 | 2019-07-27 20:28:47 +0200 | [diff] [blame] | 769 | EFI_ENTRY("%zx %zx %x %p", memory_map_size, descriptor_size, |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 770 | descriptor_version, virtmap); |
| 771 | |
Heinrich Schuchardt | 53e1d8f | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 772 | if (descriptor_version != EFI_MEMORY_DESCRIPTOR_VERSION || |
| 773 | descriptor_size < sizeof(struct efi_mem_desc)) |
| 774 | goto out; |
| 775 | |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 776 | efi_virtmap = virtmap; |
| 777 | efi_descriptor_size = descriptor_size; |
| 778 | efi_descriptor_count = n; |
| 779 | |
Alexander Graf | 5c38e05 | 2018-12-11 10:00:42 +0100 | [diff] [blame] | 780 | /* |
| 781 | * TODO: |
| 782 | * Further down we are cheating. While really we should implement |
| 783 | * SetVirtualAddressMap() events and ConvertPointer() to allow |
| 784 | * dynamically loaded drivers to expose runtime services, we don't |
| 785 | * today. |
| 786 | * |
| 787 | * So let's ensure we see exactly one single runtime section, as |
| 788 | * that is the built-in one. If we see more (or less), someone must |
| 789 | * have tried adding or removing to that which we don't support yet. |
| 790 | * In that case, let's better fail rather than expose broken runtime |
| 791 | * services. |
| 792 | */ |
| 793 | for (i = 0; i < n; i++) { |
| 794 | struct efi_mem_desc *map = (void*)virtmap + |
| 795 | (descriptor_size * i); |
| 796 | |
| 797 | if (map->type == EFI_RUNTIME_SERVICES_CODE) |
| 798 | rt_code_sections++; |
| 799 | } |
| 800 | |
| 801 | if (rt_code_sections != 1) { |
| 802 | /* |
| 803 | * We expose exactly one single runtime code section, so |
| 804 | * something is definitely going wrong. |
| 805 | */ |
Heinrich Schuchardt | 53e1d8f | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 806 | goto out; |
Alexander Graf | 5c38e05 | 2018-12-11 10:00:42 +0100 | [diff] [blame] | 807 | } |
| 808 | |
Heinrich Schuchardt | 14b4048 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 809 | /* Notify EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */ |
| 810 | list_for_each_entry(event, &efi_events, link) { |
| 811 | if (event->notify_function) |
| 812 | EFI_CALL_VOID(event->notify_function( |
| 813 | event, event->notify_context)); |
| 814 | } |
| 815 | |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 816 | /* Rebind mmio pointers */ |
| 817 | for (i = 0; i < n; i++) { |
| 818 | struct efi_mem_desc *map = (void*)virtmap + |
| 819 | (descriptor_size * i); |
| 820 | struct list_head *lhandle; |
| 821 | efi_physical_addr_t map_start = map->physical_start; |
| 822 | efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT; |
| 823 | efi_physical_addr_t map_end = map_start + map_len; |
Heinrich Schuchardt | 07240da | 2018-08-04 23:16:06 +0200 | [diff] [blame] | 824 | u64 off = map->virtual_start - map_start; |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 825 | |
| 826 | /* Adjust all mmio pointers in this region */ |
| 827 | list_for_each(lhandle, &efi_runtime_mmio) { |
| 828 | struct efi_runtime_mmio_list *lmmio; |
| 829 | |
| 830 | lmmio = list_entry(lhandle, |
| 831 | struct efi_runtime_mmio_list, |
| 832 | link); |
| 833 | if ((map_start <= lmmio->paddr) && |
| 834 | (map_end >= lmmio->paddr)) { |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 835 | uintptr_t new_addr = lmmio->paddr + off; |
| 836 | *lmmio->ptr = (void *)new_addr; |
| 837 | } |
| 838 | } |
Heinrich Schuchardt | 07240da | 2018-08-04 23:16:06 +0200 | [diff] [blame] | 839 | if ((map_start <= (uintptr_t)systab.tables) && |
| 840 | (map_end >= (uintptr_t)systab.tables)) { |
| 841 | char *ptr = (char *)systab.tables; |
| 842 | |
| 843 | ptr += off; |
| 844 | systab.tables = (struct efi_configuration_table *)ptr; |
| 845 | } |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 846 | } |
| 847 | |
Heinrich Schuchardt | 24a238f | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 848 | /* Relocate the runtime. See TODO above */ |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 849 | for (i = 0; i < n; i++) { |
| 850 | struct efi_mem_desc *map; |
| 851 | |
| 852 | map = (void*)virtmap + (descriptor_size * i); |
| 853 | if (map->type == EFI_RUNTIME_SERVICES_CODE) { |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 854 | ulong new_offset = map->virtual_start - |
Alexander Graf | 5c38e05 | 2018-12-11 10:00:42 +0100 | [diff] [blame] | 855 | map->physical_start + gd->relocaddr; |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 856 | |
Heinrich Schuchardt | 24a238f | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 857 | efi_relocate_runtime_table(new_offset); |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 858 | efi_runtime_relocate(new_offset, map); |
Heinrich Schuchardt | 53e1d8f | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 859 | ret = EFI_SUCCESS; |
| 860 | goto out; |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
Heinrich Schuchardt | 53e1d8f | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 864 | out: |
| 865 | return EFI_EXIT(ret); |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 866 | } |
| 867 | |
Heinrich Schuchardt | bcfb0e2 | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 868 | /** |
| 869 | * efi_add_runtime_mmio() - add memory-mapped IO region |
| 870 | * |
| 871 | * This function adds a memory-mapped IO region to the memory map to make it |
| 872 | * available at runtime. |
| 873 | * |
Heinrich Schuchardt | fb34f29 | 2018-12-23 02:35:13 +0100 | [diff] [blame] | 874 | * @mmio_ptr: pointer to a pointer to the start of the memory-mapped |
| 875 | * IO region |
Heinrich Schuchardt | 250b325 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 876 | * @len: size of the memory-mapped IO region |
Heinrich Schuchardt | bcfb0e2 | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 877 | * Returns: status code |
| 878 | */ |
Heinrich Schuchardt | 22c793e | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 879 | efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len) |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 880 | { |
| 881 | struct efi_runtime_mmio_list *newmmio; |
Alexander Graf | 813468c | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 882 | uint64_t addr = *(uintptr_t *)mmio_ptr; |
Bryan O'Donoghue | b225c92 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 883 | efi_status_t ret; |
Alexander Graf | 813468c | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 884 | |
Michael Walle | 714497e | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 885 | ret = efi_add_memory_map(addr, len, EFI_MMAP_IO); |
Bryan O'Donoghue | b225c92 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 886 | if (ret != EFI_SUCCESS) |
Alexander Graf | 813468c | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 887 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 888 | |
| 889 | newmmio = calloc(1, sizeof(*newmmio)); |
Heinrich Schuchardt | 22c793e | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 890 | if (!newmmio) |
| 891 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 892 | newmmio->ptr = mmio_ptr; |
| 893 | newmmio->paddr = *(uintptr_t *)mmio_ptr; |
| 894 | newmmio->len = len; |
| 895 | list_add_tail(&newmmio->link, &efi_runtime_mmio); |
Heinrich Schuchardt | 22c793e | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 896 | |
Alexander Graf | 813468c | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 897 | return EFI_SUCCESS; |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 898 | } |
| 899 | |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 900 | /* |
| 901 | * In the second stage, U-Boot has disappeared. To isolate our runtime code |
| 902 | * that at this point still exists from the rest, we put it into a special |
| 903 | * section. |
| 904 | * |
| 905 | * !!WARNING!! |
| 906 | * |
| 907 | * This means that we can not rely on any code outside of this file in any |
| 908 | * function or variable below this line. |
| 909 | * |
| 910 | * Please keep everything fully self-contained and annotated with |
Alexander Graf | 3c63db9 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 911 | * __efi_runtime and __efi_runtime_data markers. |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 912 | */ |
| 913 | |
| 914 | /* |
| 915 | * Relocate the EFI runtime stub to a different place. We need to call this |
| 916 | * the first time we expose the runtime interface to a user and on set virtual |
| 917 | * address map calls. |
| 918 | */ |
| 919 | |
Heinrich Schuchardt | bcfb0e2 | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 920 | /** |
| 921 | * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED |
| 922 | * |
| 923 | * This function is used after SetVirtualAddressMap() is called as replacement |
| 924 | * for services that are not available anymore due to constraints of the U-Boot |
| 925 | * implementation. |
| 926 | * |
| 927 | * Return: EFI_UNSUPPORTED |
| 928 | */ |
Alexander Graf | 3c63db9 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 929 | static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void) |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 930 | { |
| 931 | return EFI_UNSUPPORTED; |
| 932 | } |
| 933 | |
Alexander Graf | 3c63db9 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 934 | struct efi_runtime_services __efi_runtime_data efi_runtime_services = { |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 935 | .hdr = { |
| 936 | .signature = EFI_RUNTIME_SERVICES_SIGNATURE, |
Heinrich Schuchardt | 112f243 | 2018-06-28 12:45:27 +0200 | [diff] [blame] | 937 | .revision = EFI_SPECIFICATION_VERSION, |
Heinrich Schuchardt | 71c846a | 2018-06-28 12:45:29 +0200 | [diff] [blame] | 938 | .headersize = sizeof(struct efi_runtime_services), |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 939 | }, |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 940 | .get_time = &efi_get_time_boottime, |
Heinrich Schuchardt | 433bfe7 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 941 | .set_time = &efi_set_time_boottime, |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 942 | .get_wakeup_time = (void *)&efi_unimplemented, |
| 943 | .set_wakeup_time = (void *)&efi_unimplemented, |
| 944 | .set_virtual_address_map = &efi_set_virtual_address_map, |
Heinrich Schuchardt | 7be56e8 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 945 | .convert_pointer = efi_convert_pointer, |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 946 | .get_variable = efi_get_variable, |
Heinrich Schuchardt | 45c66f9 | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 947 | .get_next_variable_name = efi_get_next_variable_name, |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 948 | .set_variable = efi_set_variable, |
Heinrich Schuchardt | b94461c | 2019-06-20 15:40:49 +0200 | [diff] [blame] | 949 | .get_next_high_mono_count = (void *)&efi_unimplemented, |
Alexander Graf | 80a4800 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 950 | .reset_system = &efi_reset_system_boottime, |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 951 | #ifdef CONFIG_EFI_RUNTIME_UPDATE_CAPSULE |
Heinrich Schuchardt | 0c23074 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 952 | .update_capsule = efi_update_capsule, |
| 953 | .query_capsule_caps = efi_query_capsule_caps, |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 954 | #else |
| 955 | .update_capsule = efi_update_capsule_unsupported, |
| 956 | .query_capsule_caps = efi_query_capsule_caps_unsupported, |
| 957 | #endif |
Heinrich Schuchardt | 0c23074 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 958 | .query_variable_info = efi_query_variable_info, |
Alexander Graf | 50149ea | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 959 | }; |