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