Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application runtime services |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 10 | #include <cpu_func.h> |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 11 | #include <dm.h> |
Alexander Graf | 46c1514 | 2018-06-18 17:23:11 +0200 | [diff] [blame] | 12 | #include <elf.h> |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 13 | #include <efi_loader.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 16 | #include <rtc.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 17 | #include <u-boot/crc.h> |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 18 | |
| 19 | /* For manual relocation support */ |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Heinrich Schuchardt | 956eff3 | 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 | 7725609 | 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 | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 36 | static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 37 | |
Simon Glass | fff89d4 | 2018-06-11 23:26:40 -0600 | [diff] [blame] | 38 | /* |
Heinrich Schuchardt | 1112154 | 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 | fff89d4 | 2018-06-11 23:26:40 -0600 | [diff] [blame] | 42 | */ |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 43 | #if defined(__aarch64__) |
Alexander Graf | 46c1514 | 2018-06-18 17:23:11 +0200 | [diff] [blame] | 44 | #define R_RELATIVE R_AARCH64_RELATIVE |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 45 | #define R_MASK 0xffffffffULL |
| 46 | #define IS_RELA 1 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 47 | #elif defined(__arm__) |
Alexander Graf | 46c1514 | 2018-06-18 17:23:11 +0200 | [diff] [blame] | 48 | #define R_RELATIVE R_ARM_RELATIVE |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 49 | #define R_MASK 0xffULL |
Heinrich Schuchardt | 56e82be | 2018-10-13 20:52:08 -0700 | [diff] [blame] | 50 | #elif defined(__i386__) |
Simon Glass | cdfe696 | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 51 | #define R_RELATIVE R_386_RELATIVE |
| 52 | #define R_MASK 0xffULL |
Heinrich Schuchardt | 56e82be | 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 | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 57 | #elif defined(__riscv) |
Rick Chen | 9677a37 | 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 | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 68 | #if (__riscv_xlen == 32) |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 69 | #define R_ABSOLUTE R_RISCV_32 |
| 70 | #define SYM_INDEX 8 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 71 | #elif (__riscv_xlen == 64) |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 72 | #define R_ABSOLUTE R_RISCV_64 |
| 73 | #define SYM_INDEX 32 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 74 | #else |
| 75 | #error unknown riscv target |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 76 | #endif |
Alexander Graf | 0bd425a | 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 | c680dc4 | 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 | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 96 | /* |
Heinrich Schuchardt | 1112154 | 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 | 0bd425a | 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 | 956eff3 | 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 | 3240184 | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 110 | efi_status_t efi_init_runtime_supported(void) |
| 111 | { |
Heinrich Schuchardt | 956eff3 | 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 | 5be5971 | 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 | c680dc4 | 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 | 3240184 | 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 | 05874fb | 2019-07-05 18:12:16 +0200 | [diff] [blame] | 133 | #ifdef CONFIG_EFI_HAVE_RUNTIME_RESET |
Heinrich Schuchardt | 956eff3 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 134 | rt_table->runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM; |
AKASHI Takahiro | 3240184 | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 135 | #endif |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 136 | |
Heinrich Schuchardt | 956eff3 | 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 | 3240184 | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 140 | } |
| 141 | |
Heinrich Schuchardt | 69c1576 | 2018-07-29 09:49:04 +0200 | [diff] [blame] | 142 | /** |
Heinrich Schuchardt | 8cd9975 | 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 | 447753e | 2020-07-22 07:56:14 +0200 | [diff] [blame] | 147 | * Overlapping memory areas can be copied safely if src >= dest. |
| 148 | * |
Heinrich Schuchardt | 8cd9975 | 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 | 69c1576 | 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 | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 175 | /** |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 176 | * efi_reset_system_boottime() - reset system at boot time |
Heinrich Schuchardt | 711853b | 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 | 7725609 | 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 | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 193 | { |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 194 | struct efi_event *evt; |
| 195 | |
Alexander Graf | 0bd425a | 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 | bf7f169 | 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 | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 204 | efi_signal_event(evt); |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 205 | break; |
| 206 | } |
| 207 | } |
Alexander Graf | 0bd425a | 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 | 450d4c8 | 2018-02-06 22:00:22 +0100 | [diff] [blame] | 211 | case EFI_RESET_PLATFORM_SPECIFIC: |
Alexander Graf | 0bd425a | 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 | 4448945 | 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 | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 218 | break; |
| 219 | } |
| 220 | |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 221 | while (1) { } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 224 | /** |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 225 | * efi_get_time_boottime() - get current time at boot time |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 226 | * |
| 227 | * This function implements the GetTime runtime service before |
| 228 | * SetVirtualAddressMap() is called. |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 229 | * |
Heinrich Schuchardt | 37035b1 | 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 | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 235 | * Returns: status code |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 236 | */ |
Alexander Graf | 7725609 | 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 | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 240 | { |
Heinrich Schuchardt | f2856ad | 2019-05-31 22:56:02 +0200 | [diff] [blame] | 241 | #ifdef CONFIG_EFI_GET_TIME |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 242 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 243 | struct rtc_time tm; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 244 | struct udevice *dev; |
| 245 | |
| 246 | EFI_ENTRY("%p %p", time, capabilities); |
| 247 | |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 248 | if (!time) { |
| 249 | ret = EFI_INVALID_PARAMETER; |
| 250 | goto out; |
| 251 | } |
Heinrich Schuchardt | b4bd366 | 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 | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 258 | ret = EFI_DEVICE_ERROR; |
| 259 | goto out; |
| 260 | } |
Alexander Graf | 0bd425a | 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 | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 268 | time->second = tm.tm_sec; |
Heinrich Schuchardt | 6846c57 | 2020-10-23 05:30:29 +0200 | [diff] [blame] | 269 | if (tm.tm_isdst > 0) |
Heinrich Schuchardt | ba3e828 | 2019-05-31 22:08:45 +0200 | [diff] [blame] | 270 | time->daylight = |
| 271 | EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT; |
Heinrich Schuchardt | 6846c57 | 2020-10-23 05:30:29 +0200 | [diff] [blame] | 272 | else if (!tm.tm_isdst) |
| 273 | time->daylight = EFI_TIME_ADJUST_DAYLIGHT; |
| 274 | else |
| 275 | time->daylight = 0; |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 276 | time->timezone = EFI_UNSPECIFIED_TIMEZONE; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 277 | |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 278 | if (capabilities) { |
| 279 | /* Set reasonable dummy values */ |
| 280 | capabilities->resolution = 1; /* 1 Hz */ |
| 281 | capabilities->accuracy = 100000000; /* 100 ppm */ |
| 282 | capabilities->sets_to_zero = false; |
| 283 | } |
| 284 | out: |
| 285 | return EFI_EXIT(ret); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 286 | #else |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 287 | EFI_ENTRY("%p %p", time, capabilities); |
Heinrich Schuchardt | b4bd366 | 2019-05-19 21:41:28 +0200 | [diff] [blame] | 288 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 289 | #endif |
| 290 | } |
| 291 | |
Heinrich Schuchardt | f2856ad | 2019-05-31 22:56:02 +0200 | [diff] [blame] | 292 | #ifdef CONFIG_EFI_SET_TIME |
Heinrich Schuchardt | 33e5a0e | 2019-05-31 07:35:19 +0200 | [diff] [blame] | 293 | |
| 294 | /** |
| 295 | * efi_validate_time() - checks if timestamp is valid |
| 296 | * |
| 297 | * @time: timestamp to validate |
| 298 | * Returns: 0 if timestamp is valid, 1 otherwise |
| 299 | */ |
| 300 | static int efi_validate_time(struct efi_time *time) |
| 301 | { |
| 302 | return (!time || |
| 303 | time->year < 1900 || time->year > 9999 || |
| 304 | !time->month || time->month > 12 || !time->day || |
| 305 | time->day > rtc_month_days(time->month - 1, time->year) || |
| 306 | time->hour > 23 || time->minute > 59 || time->second > 59 || |
| 307 | time->nanosecond > 999999999 || |
| 308 | time->daylight & |
| 309 | ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) || |
| 310 | ((time->timezone < -1440 || time->timezone > 1440) && |
| 311 | time->timezone != EFI_UNSPECIFIED_TIMEZONE)); |
| 312 | } |
| 313 | |
| 314 | #endif |
| 315 | |
Heinrich Schuchardt | 411b2d5 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 316 | /** |
| 317 | * efi_set_time_boottime() - set current time |
| 318 | * |
| 319 | * This function implements the SetTime() runtime service before |
| 320 | * SetVirtualAddressMap() is called. |
| 321 | * |
| 322 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 323 | * for details. |
| 324 | * |
| 325 | * @time: pointer to structure to with current time |
| 326 | * Returns: status code |
| 327 | */ |
| 328 | static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time) |
| 329 | { |
Heinrich Schuchardt | f2856ad | 2019-05-31 22:56:02 +0200 | [diff] [blame] | 330 | #ifdef CONFIG_EFI_SET_TIME |
Heinrich Schuchardt | 411b2d5 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 331 | efi_status_t ret = EFI_SUCCESS; |
| 332 | struct rtc_time tm; |
| 333 | struct udevice *dev; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 334 | |
Heinrich Schuchardt | 411b2d5 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 335 | EFI_ENTRY("%p", time); |
| 336 | |
Heinrich Schuchardt | 33e5a0e | 2019-05-31 07:35:19 +0200 | [diff] [blame] | 337 | if (efi_validate_time(time)) { |
Heinrich Schuchardt | 411b2d5 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 338 | ret = EFI_INVALID_PARAMETER; |
| 339 | goto out; |
| 340 | } |
| 341 | |
| 342 | if (uclass_get_device(UCLASS_RTC, 0, &dev)) { |
| 343 | ret = EFI_UNSUPPORTED; |
| 344 | goto out; |
| 345 | } |
| 346 | |
| 347 | memset(&tm, 0, sizeof(tm)); |
| 348 | tm.tm_year = time->year; |
| 349 | tm.tm_mon = time->month; |
| 350 | tm.tm_mday = time->day; |
| 351 | tm.tm_hour = time->hour; |
| 352 | tm.tm_min = time->minute; |
| 353 | tm.tm_sec = time->second; |
Heinrich Schuchardt | 6846c57 | 2020-10-23 05:30:29 +0200 | [diff] [blame] | 354 | switch (time->daylight) { |
| 355 | case EFI_TIME_ADJUST_DAYLIGHT: |
| 356 | tm.tm_isdst = 0; |
| 357 | break; |
| 358 | case EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT: |
| 359 | tm.tm_isdst = 1; |
| 360 | break; |
| 361 | default: |
| 362 | tm.tm_isdst = -1; |
| 363 | break; |
| 364 | } |
Heinrich Schuchardt | 411b2d5 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 365 | /* Calculate day of week */ |
| 366 | rtc_calc_weekday(&tm); |
| 367 | |
| 368 | if (dm_rtc_set(dev, &tm)) |
| 369 | ret = EFI_DEVICE_ERROR; |
| 370 | out: |
| 371 | return EFI_EXIT(ret); |
| 372 | #else |
| 373 | EFI_ENTRY("%p", time); |
| 374 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 375 | #endif |
| 376 | } |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 377 | /** |
| 378 | * efi_reset_system() - reset system |
| 379 | * |
| 380 | * This function implements the ResetSystem() runtime service after |
Heinrich Schuchardt | 7b66fcc | 2020-08-22 08:29:53 +0200 | [diff] [blame] | 381 | * SetVirtualAddressMap() is called. As this placeholder cannot reset the |
| 382 | * system it simply return to the caller. |
| 383 | * |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 384 | * Boards may override the helpers below to implement reset functionality. |
| 385 | * |
| 386 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 387 | * details. |
| 388 | * |
| 389 | * @reset_type: type of reset to perform |
| 390 | * @reset_status: status code for the reset |
| 391 | * @data_size: size of reset_data |
| 392 | * @reset_data: information about the reset |
| 393 | */ |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 394 | void __weak __efi_runtime EFIAPI efi_reset_system( |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 395 | enum efi_reset_type reset_type, |
| 396 | efi_status_t reset_status, |
| 397 | unsigned long data_size, void *reset_data) |
| 398 | { |
Heinrich Schuchardt | 7b66fcc | 2020-08-22 08:29:53 +0200 | [diff] [blame] | 399 | return; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 402 | /** |
| 403 | * efi_reset_system_init() - initialize the reset driver |
| 404 | * |
| 405 | * Boards may override this function to initialize the reset driver. |
| 406 | */ |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 407 | efi_status_t __weak efi_reset_system_init(void) |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 408 | { |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 409 | return EFI_SUCCESS; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 412 | /** |
| 413 | * efi_get_time() - get current time |
| 414 | * |
| 415 | * This function implements the GetTime runtime service after |
| 416 | * SetVirtualAddressMap() is called. As the U-Boot driver are not available |
| 417 | * anymore only an error code is returned. |
| 418 | * |
| 419 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 420 | * for details. |
| 421 | * |
| 422 | * @time: pointer to structure to receive current time |
| 423 | * @capabilities: pointer to structure to receive RTC properties |
| 424 | * Returns: status code |
| 425 | */ |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 426 | efi_status_t __weak __efi_runtime EFIAPI efi_get_time( |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 427 | struct efi_time *time, |
| 428 | struct efi_time_cap *capabilities) |
| 429 | { |
Heinrich Schuchardt | b4ba5be | 2019-06-13 18:42:40 +0200 | [diff] [blame] | 430 | return EFI_UNSUPPORTED; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Heinrich Schuchardt | 411b2d5 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 433 | /** |
| 434 | * efi_set_time() - set current time |
| 435 | * |
| 436 | * This function implements the SetTime runtime service after |
| 437 | * SetVirtualAddressMap() is called. As the U-Boot driver are not available |
| 438 | * anymore only an error code is returned. |
| 439 | * |
| 440 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 441 | * for details. |
| 442 | * |
| 443 | * @time: pointer to structure to with current time |
| 444 | * Returns: status code |
| 445 | */ |
| 446 | efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time) |
| 447 | { |
| 448 | return EFI_UNSUPPORTED; |
| 449 | } |
| 450 | |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 451 | /** |
| 452 | * efi_is_runtime_service_pointer() - check if pointer points to runtime table |
| 453 | * |
| 454 | * @p: pointer to check |
| 455 | * Return: true if the pointer points to a service function pointer in the |
| 456 | * runtime table |
| 457 | */ |
| 458 | static bool efi_is_runtime_service_pointer(void *p) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 459 | { |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 460 | return (p >= (void *)&efi_runtime_services.get_time && |
| 461 | p <= (void *)&efi_runtime_services.query_variable_info) || |
| 462 | p == (void *)&efi_events.prev || |
| 463 | p == (void *)&efi_events.next; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Heinrich Schuchardt | c3f2a9f | 2019-07-05 18:12:21 +0200 | [diff] [blame] | 466 | /** |
| 467 | * efi_runtime_detach() - detach unimplemented runtime functions |
| 468 | */ |
Heinrich Schuchardt | 1943dbb | 2019-07-05 17:42:16 +0200 | [diff] [blame] | 469 | void efi_runtime_detach(void) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 470 | { |
Heinrich Schuchardt | c3f2a9f | 2019-07-05 18:12:21 +0200 | [diff] [blame] | 471 | efi_runtime_services.reset_system = efi_reset_system; |
| 472 | efi_runtime_services.get_time = efi_get_time; |
| 473 | efi_runtime_services.set_time = efi_set_time; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 474 | |
Heinrich Schuchardt | c3f2a9f | 2019-07-05 18:12:21 +0200 | [diff] [blame] | 475 | /* Update CRC32 */ |
| 476 | efi_update_table_header_crc32(&efi_runtime_services.hdr); |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Heinrich Schuchardt | 2572851 | 2019-06-29 03:32:52 +0200 | [diff] [blame] | 479 | /** |
| 480 | * efi_set_virtual_address_map_runtime() - change from physical to virtual |
| 481 | * mapping |
| 482 | * |
| 483 | * This function implements the SetVirtualAddressMap() runtime service after |
| 484 | * it is first called. |
| 485 | * |
| 486 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 487 | * details. |
| 488 | * |
| 489 | * @memory_map_size: size of the virtual map |
| 490 | * @descriptor_size: size of an entry in the map |
| 491 | * @descriptor_version: version of the map entries |
| 492 | * @virtmap: virtual address mapping information |
| 493 | * Return: status code EFI_UNSUPPORTED |
| 494 | */ |
Heinrich Schuchardt | 2b6bd38 | 2019-07-12 22:41:43 +0200 | [diff] [blame] | 495 | static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map_runtime( |
Heinrich Schuchardt | f8599dc | 2019-07-27 20:28:47 +0200 | [diff] [blame] | 496 | efi_uintn_t memory_map_size, |
| 497 | efi_uintn_t descriptor_size, |
Heinrich Schuchardt | 2572851 | 2019-06-29 03:32:52 +0200 | [diff] [blame] | 498 | uint32_t descriptor_version, |
| 499 | struct efi_mem_desc *virtmap) |
| 500 | { |
| 501 | return EFI_UNSUPPORTED; |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * efi_convert_pointer_runtime() - convert from physical to virtual pointer |
| 506 | * |
| 507 | * This function implements the ConvertPointer() runtime service after |
| 508 | * the first call to SetVirtualAddressMap(). |
| 509 | * |
| 510 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 511 | * details. |
| 512 | * |
| 513 | * @debug_disposition: indicates if pointer may be converted to NULL |
| 514 | * @address: pointer to be converted |
| 515 | * Return: status code EFI_UNSUPPORTED |
| 516 | */ |
| 517 | static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime( |
| 518 | efi_uintn_t debug_disposition, void **address) |
| 519 | { |
| 520 | return EFI_UNSUPPORTED; |
| 521 | } |
| 522 | |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 523 | /** |
Heinrich Schuchardt | 582f857 | 2020-03-22 08:28:15 +0100 | [diff] [blame] | 524 | * efi_convert_pointer() - convert from physical to virtual pointer |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 525 | * |
| 526 | * This function implements the ConvertPointer() runtime service until |
| 527 | * the first call to SetVirtualAddressMap(). |
| 528 | * |
| 529 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 530 | * details. |
| 531 | * |
| 532 | * @debug_disposition: indicates if pointer may be converted to NULL |
| 533 | * @address: pointer to be converted |
Heinrich Schuchardt | 582f857 | 2020-03-22 08:28:15 +0100 | [diff] [blame] | 534 | * Return: status code |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 535 | */ |
Heinrich Schuchardt | 699b7c6 | 2020-03-24 18:05:22 +0100 | [diff] [blame] | 536 | __efi_runtime efi_status_t EFIAPI |
| 537 | efi_convert_pointer(efi_uintn_t debug_disposition, void **address) |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 538 | { |
Heinrich Schuchardt | b0818b7 | 2020-07-07 03:10:12 +0200 | [diff] [blame] | 539 | efi_physical_addr_t addr; |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 540 | efi_uintn_t i; |
| 541 | efi_status_t ret = EFI_NOT_FOUND; |
| 542 | |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 543 | if (!efi_virtmap) { |
| 544 | ret = EFI_UNSUPPORTED; |
| 545 | goto out; |
| 546 | } |
| 547 | |
| 548 | if (!address) { |
| 549 | ret = EFI_INVALID_PARAMETER; |
| 550 | goto out; |
| 551 | } |
Heinrich Schuchardt | dabe54a | 2020-03-24 17:52:40 +0100 | [diff] [blame] | 552 | if (!*address) { |
| 553 | if (debug_disposition & EFI_OPTIONAL_PTR) |
| 554 | return EFI_SUCCESS; |
| 555 | else |
| 556 | return EFI_INVALID_PARAMETER; |
| 557 | } |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 558 | |
Heinrich Schuchardt | b0818b7 | 2020-07-07 03:10:12 +0200 | [diff] [blame] | 559 | addr = (uintptr_t)*address; |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 560 | for (i = 0; i < efi_descriptor_count; i++) { |
| 561 | struct efi_mem_desc *map = (void *)efi_virtmap + |
| 562 | (efi_descriptor_size * i); |
| 563 | |
| 564 | if (addr >= map->physical_start && |
| 565 | (addr < map->physical_start |
| 566 | + (map->num_pages << EFI_PAGE_SHIFT))) { |
| 567 | *address = (void *)(uintptr_t) |
| 568 | (addr + map->virtual_start - |
| 569 | map->physical_start); |
| 570 | |
| 571 | ret = EFI_SUCCESS; |
| 572 | break; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | out: |
Heinrich Schuchardt | 699b7c6 | 2020-03-24 18:05:22 +0100 | [diff] [blame] | 577 | return ret; |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 580 | static __efi_runtime void efi_relocate_runtime_table(ulong offset) |
| 581 | { |
| 582 | ulong patchoff; |
| 583 | void **pos; |
| 584 | |
| 585 | /* Relocate the runtime services pointers */ |
| 586 | patchoff = offset - gd->relocaddr; |
| 587 | for (pos = (void **)&efi_runtime_services.get_time; |
| 588 | pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) { |
Heinrich Schuchardt | 2572851 | 2019-06-29 03:32:52 +0200 | [diff] [blame] | 589 | if (*pos) |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 590 | *pos += patchoff; |
| 591 | } |
Heinrich Schuchardt | 69c1576 | 2018-07-29 09:49:04 +0200 | [diff] [blame] | 592 | |
Heinrich Schuchardt | 2572851 | 2019-06-29 03:32:52 +0200 | [diff] [blame] | 593 | /* |
| 594 | * The entry for SetVirtualAddress() must point to a physical address. |
| 595 | * After the first execution the service must return EFI_UNSUPPORTED. |
| 596 | */ |
| 597 | efi_runtime_services.set_virtual_address_map = |
| 598 | &efi_set_virtual_address_map_runtime; |
| 599 | |
| 600 | /* |
| 601 | * The entry for ConvertPointer() must point to a physical address. |
| 602 | * The service is not usable after SetVirtualAddress(). |
| 603 | */ |
| 604 | efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime; |
| 605 | |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 606 | /* |
| 607 | * TODO: Update UEFI variable RuntimeServicesSupported removing flags |
| 608 | * EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP and |
| 609 | * EFI_RT_SUPPORTED_CONVERT_POINTER as required by the UEFI spec 2.8. |
| 610 | */ |
| 611 | |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 612 | /* Update CRC32 */ |
Heinrich Schuchardt | 69c1576 | 2018-07-29 09:49:04 +0200 | [diff] [blame] | 613 | efi_update_table_header_crc32(&efi_runtime_services.hdr); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /* Relocate EFI runtime to uboot_reloc_base = offset */ |
| 617 | void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map) |
| 618 | { |
| 619 | #ifdef IS_RELA |
| 620 | struct elf_rela *rel = (void*)&__efi_runtime_rel_start; |
| 621 | #else |
| 622 | struct elf_rel *rel = (void*)&__efi_runtime_rel_start; |
| 623 | static ulong lastoff = CONFIG_SYS_TEXT_BASE; |
| 624 | #endif |
| 625 | |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 626 | debug("%s: Relocating to offset=%lx\n", __func__, offset); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 627 | for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) { |
| 628 | ulong base = CONFIG_SYS_TEXT_BASE; |
| 629 | ulong *p; |
| 630 | ulong newaddr; |
| 631 | |
| 632 | p = (void*)((ulong)rel->offset - base) + gd->relocaddr; |
| 633 | |
Heinrich Schuchardt | 3369158 | 2019-08-14 06:49:09 +0200 | [diff] [blame] | 634 | /* |
| 635 | * The runtime services table is updated in |
| 636 | * efi_relocate_runtime_table() |
| 637 | */ |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 638 | if (map && efi_is_runtime_service_pointer(p)) |
| 639 | continue; |
| 640 | |
Heinrich Schuchardt | 56e82be | 2018-10-13 20:52:08 -0700 | [diff] [blame] | 641 | debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, |
| 642 | rel->info, *p, rel->offset); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 643 | |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 644 | switch (rel->info & R_MASK) { |
| 645 | case R_RELATIVE: |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 646 | #ifdef IS_RELA |
| 647 | newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE; |
| 648 | #else |
| 649 | newaddr = *p - lastoff + offset; |
| 650 | #endif |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 651 | break; |
| 652 | #ifdef R_ABSOLUTE |
| 653 | case R_ABSOLUTE: { |
| 654 | ulong symidx = rel->info >> SYM_INDEX; |
| 655 | extern struct dyn_sym __dyn_sym_start[]; |
| 656 | newaddr = __dyn_sym_start[symidx].addr + offset; |
Alexander Graf | df0f6c7 | 2018-11-04 22:25:22 +0100 | [diff] [blame] | 657 | #ifdef IS_RELA |
| 658 | newaddr -= CONFIG_SYS_TEXT_BASE; |
| 659 | #endif |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 660 | break; |
| 661 | } |
| 662 | #endif |
| 663 | default: |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 664 | printf("%s: Unknown relocation type %llx\n", |
| 665 | __func__, rel->info & R_MASK); |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 666 | continue; |
| 667 | } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 668 | |
| 669 | /* Check if the relocation is inside bounds */ |
| 670 | if (map && ((newaddr < map->virtual_start) || |
Heinrich Schuchardt | 9ebc47f | 2017-09-18 22:11:34 +0200 | [diff] [blame] | 671 | newaddr > (map->virtual_start + |
| 672 | (map->num_pages << EFI_PAGE_SHIFT)))) { |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 673 | printf("%s: Relocation at %p is out of range (%lx)\n", |
| 674 | __func__, p, newaddr); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 675 | continue; |
| 676 | } |
| 677 | |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 678 | debug("%s: Setting %p to %lx\n", __func__, p, newaddr); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 679 | *p = newaddr; |
Alexander Graf | e570232 | 2016-04-11 23:20:39 +0200 | [diff] [blame] | 680 | flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1), |
| 681 | ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE)); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | #ifndef IS_RELA |
| 685 | lastoff = offset; |
| 686 | #endif |
| 687 | |
| 688 | invalidate_icache_all(); |
| 689 | } |
| 690 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 691 | /** |
| 692 | * efi_set_virtual_address_map() - change from physical to virtual mapping |
| 693 | * |
| 694 | * This function implements the SetVirtualAddressMap() runtime service. |
| 695 | * |
| 696 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 697 | * details. |
| 698 | * |
| 699 | * @memory_map_size: size of the virtual map |
| 700 | * @descriptor_size: size of an entry in the map |
| 701 | * @descriptor_version: version of the map entries |
| 702 | * @virtmap: virtual address mapping information |
| 703 | * Return: status code |
| 704 | */ |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 705 | static efi_status_t EFIAPI efi_set_virtual_address_map( |
Heinrich Schuchardt | f8599dc | 2019-07-27 20:28:47 +0200 | [diff] [blame] | 706 | efi_uintn_t memory_map_size, |
| 707 | efi_uintn_t descriptor_size, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 708 | uint32_t descriptor_version, |
| 709 | struct efi_mem_desc *virtmap) |
| 710 | { |
Heinrich Schuchardt | f8599dc | 2019-07-27 20:28:47 +0200 | [diff] [blame] | 711 | efi_uintn_t n = memory_map_size / descriptor_size; |
| 712 | efi_uintn_t i; |
Heinrich Schuchardt | c70a161 | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 713 | efi_status_t ret = EFI_INVALID_PARAMETER; |
Alexander Graf | 86e0021 | 2018-12-11 10:00:42 +0100 | [diff] [blame] | 714 | int rt_code_sections = 0; |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 715 | struct efi_event *event; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 716 | |
Heinrich Schuchardt | f8599dc | 2019-07-27 20:28:47 +0200 | [diff] [blame] | 717 | EFI_ENTRY("%zx %zx %x %p", memory_map_size, descriptor_size, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 718 | descriptor_version, virtmap); |
| 719 | |
Heinrich Schuchardt | c70a161 | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 720 | if (descriptor_version != EFI_MEMORY_DESCRIPTOR_VERSION || |
| 721 | descriptor_size < sizeof(struct efi_mem_desc)) |
| 722 | goto out; |
| 723 | |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 724 | efi_virtmap = virtmap; |
| 725 | efi_descriptor_size = descriptor_size; |
| 726 | efi_descriptor_count = n; |
| 727 | |
Alexander Graf | 86e0021 | 2018-12-11 10:00:42 +0100 | [diff] [blame] | 728 | /* |
| 729 | * TODO: |
| 730 | * Further down we are cheating. While really we should implement |
| 731 | * SetVirtualAddressMap() events and ConvertPointer() to allow |
| 732 | * dynamically loaded drivers to expose runtime services, we don't |
| 733 | * today. |
| 734 | * |
| 735 | * So let's ensure we see exactly one single runtime section, as |
| 736 | * that is the built-in one. If we see more (or less), someone must |
| 737 | * have tried adding or removing to that which we don't support yet. |
| 738 | * In that case, let's better fail rather than expose broken runtime |
| 739 | * services. |
| 740 | */ |
| 741 | for (i = 0; i < n; i++) { |
| 742 | struct efi_mem_desc *map = (void*)virtmap + |
| 743 | (descriptor_size * i); |
| 744 | |
| 745 | if (map->type == EFI_RUNTIME_SERVICES_CODE) |
| 746 | rt_code_sections++; |
| 747 | } |
| 748 | |
| 749 | if (rt_code_sections != 1) { |
| 750 | /* |
| 751 | * We expose exactly one single runtime code section, so |
| 752 | * something is definitely going wrong. |
| 753 | */ |
Heinrich Schuchardt | c70a161 | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 754 | goto out; |
Alexander Graf | 86e0021 | 2018-12-11 10:00:42 +0100 | [diff] [blame] | 755 | } |
| 756 | |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 757 | /* Notify EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */ |
| 758 | list_for_each_entry(event, &efi_events, link) { |
| 759 | if (event->notify_function) |
| 760 | EFI_CALL_VOID(event->notify_function( |
| 761 | event, event->notify_context)); |
| 762 | } |
| 763 | |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 764 | /* Rebind mmio pointers */ |
| 765 | for (i = 0; i < n; i++) { |
| 766 | struct efi_mem_desc *map = (void*)virtmap + |
| 767 | (descriptor_size * i); |
| 768 | struct list_head *lhandle; |
| 769 | efi_physical_addr_t map_start = map->physical_start; |
| 770 | efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT; |
| 771 | efi_physical_addr_t map_end = map_start + map_len; |
Heinrich Schuchardt | 3905456 | 2018-08-04 23:16:06 +0200 | [diff] [blame] | 772 | u64 off = map->virtual_start - map_start; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 773 | |
| 774 | /* Adjust all mmio pointers in this region */ |
| 775 | list_for_each(lhandle, &efi_runtime_mmio) { |
| 776 | struct efi_runtime_mmio_list *lmmio; |
| 777 | |
| 778 | lmmio = list_entry(lhandle, |
| 779 | struct efi_runtime_mmio_list, |
| 780 | link); |
| 781 | if ((map_start <= lmmio->paddr) && |
| 782 | (map_end >= lmmio->paddr)) { |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 783 | uintptr_t new_addr = lmmio->paddr + off; |
| 784 | *lmmio->ptr = (void *)new_addr; |
| 785 | } |
| 786 | } |
Heinrich Schuchardt | 3905456 | 2018-08-04 23:16:06 +0200 | [diff] [blame] | 787 | if ((map_start <= (uintptr_t)systab.tables) && |
| 788 | (map_end >= (uintptr_t)systab.tables)) { |
| 789 | char *ptr = (char *)systab.tables; |
| 790 | |
| 791 | ptr += off; |
| 792 | systab.tables = (struct efi_configuration_table *)ptr; |
| 793 | } |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 794 | } |
| 795 | |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 796 | /* Relocate the runtime. See TODO above */ |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 797 | for (i = 0; i < n; i++) { |
| 798 | struct efi_mem_desc *map; |
| 799 | |
| 800 | map = (void*)virtmap + (descriptor_size * i); |
| 801 | if (map->type == EFI_RUNTIME_SERVICES_CODE) { |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 802 | ulong new_offset = map->virtual_start - |
Alexander Graf | 86e0021 | 2018-12-11 10:00:42 +0100 | [diff] [blame] | 803 | map->physical_start + gd->relocaddr; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 804 | |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 805 | efi_relocate_runtime_table(new_offset); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 806 | efi_runtime_relocate(new_offset, map); |
Heinrich Schuchardt | c70a161 | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 807 | ret = EFI_SUCCESS; |
| 808 | goto out; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
Heinrich Schuchardt | c70a161 | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 812 | out: |
| 813 | return EFI_EXIT(ret); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 814 | } |
| 815 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 816 | /** |
| 817 | * efi_add_runtime_mmio() - add memory-mapped IO region |
| 818 | * |
| 819 | * This function adds a memory-mapped IO region to the memory map to make it |
| 820 | * available at runtime. |
| 821 | * |
Heinrich Schuchardt | 7e30764 | 2018-12-23 02:35:13 +0100 | [diff] [blame] | 822 | * @mmio_ptr: pointer to a pointer to the start of the memory-mapped |
| 823 | * IO region |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 824 | * @len: size of the memory-mapped IO region |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 825 | * Returns: status code |
| 826 | */ |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 827 | efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len) |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 828 | { |
| 829 | struct efi_runtime_mmio_list *newmmio; |
Alexander Graf | 33a83a3 | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 830 | uint64_t addr = *(uintptr_t *)mmio_ptr; |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 831 | efi_status_t ret; |
Alexander Graf | 33a83a3 | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 832 | |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 833 | ret = efi_add_memory_map(addr, len, EFI_MMAP_IO); |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 834 | if (ret != EFI_SUCCESS) |
Alexander Graf | 33a83a3 | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 835 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 836 | |
| 837 | newmmio = calloc(1, sizeof(*newmmio)); |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 838 | if (!newmmio) |
| 839 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 840 | newmmio->ptr = mmio_ptr; |
| 841 | newmmio->paddr = *(uintptr_t *)mmio_ptr; |
| 842 | newmmio->len = len; |
| 843 | list_add_tail(&newmmio->link, &efi_runtime_mmio); |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 844 | |
Alexander Graf | 33a83a3 | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 845 | return EFI_SUCCESS; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 846 | } |
| 847 | |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 848 | /* |
| 849 | * In the second stage, U-Boot has disappeared. To isolate our runtime code |
| 850 | * that at this point still exists from the rest, we put it into a special |
| 851 | * section. |
| 852 | * |
| 853 | * !!WARNING!! |
| 854 | * |
| 855 | * This means that we can not rely on any code outside of this file in any |
| 856 | * function or variable below this line. |
| 857 | * |
| 858 | * Please keep everything fully self-contained and annotated with |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 859 | * __efi_runtime and __efi_runtime_data markers. |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 860 | */ |
| 861 | |
| 862 | /* |
| 863 | * Relocate the EFI runtime stub to a different place. We need to call this |
| 864 | * the first time we expose the runtime interface to a user and on set virtual |
| 865 | * address map calls. |
| 866 | */ |
| 867 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 868 | /** |
| 869 | * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED |
| 870 | * |
| 871 | * This function is used after SetVirtualAddressMap() is called as replacement |
| 872 | * for services that are not available anymore due to constraints of the U-Boot |
| 873 | * implementation. |
| 874 | * |
| 875 | * Return: EFI_UNSUPPORTED |
| 876 | */ |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 877 | static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 878 | { |
| 879 | return EFI_UNSUPPORTED; |
| 880 | } |
| 881 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 882 | /** |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 883 | * efi_update_capsule() - process information from operating system |
| 884 | * |
| 885 | * This function implements the UpdateCapsule() runtime service. |
| 886 | * |
| 887 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 888 | * details. |
| 889 | * |
| 890 | * @capsule_header_array: pointer to array of virtual pointers |
| 891 | * @capsule_count: number of pointers in capsule_header_array |
| 892 | * @scatter_gather_list: pointer to arry of physical pointers |
| 893 | * Returns: status code |
| 894 | */ |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 895 | efi_status_t __efi_runtime EFIAPI efi_update_capsule( |
| 896 | struct efi_capsule_header **capsule_header_array, |
| 897 | efi_uintn_t capsule_count, |
| 898 | u64 scatter_gather_list) |
| 899 | { |
| 900 | return EFI_UNSUPPORTED; |
| 901 | } |
| 902 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 903 | /** |
| 904 | * efi_query_capsule_caps() - check if capsule is supported |
| 905 | * |
| 906 | * This function implements the QueryCapsuleCapabilities() runtime service. |
| 907 | * |
| 908 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 909 | * details. |
| 910 | * |
| 911 | * @capsule_header_array: pointer to array of virtual pointers |
| 912 | * @capsule_count: number of pointers in capsule_header_array |
Heinrich Schuchardt | 1d1be36 | 2018-09-03 20:59:25 +0200 | [diff] [blame] | 913 | * @maximum_capsule_size: maximum capsule size |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 914 | * @reset_type: type of reset needed for capsule update |
| 915 | * Returns: status code |
| 916 | */ |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 917 | efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps( |
| 918 | struct efi_capsule_header **capsule_header_array, |
| 919 | efi_uintn_t capsule_count, |
AKASHI Takahiro | d840b67 | 2018-11-14 16:18:53 +0900 | [diff] [blame] | 920 | u64 *maximum_capsule_size, |
| 921 | u32 *reset_type) |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 922 | { |
| 923 | return EFI_UNSUPPORTED; |
| 924 | } |
| 925 | |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 926 | struct efi_runtime_services __efi_runtime_data efi_runtime_services = { |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 927 | .hdr = { |
| 928 | .signature = EFI_RUNTIME_SERVICES_SIGNATURE, |
Heinrich Schuchardt | e75b3cb | 2018-06-28 12:45:27 +0200 | [diff] [blame] | 929 | .revision = EFI_SPECIFICATION_VERSION, |
Heinrich Schuchardt | 1020425 | 2018-06-28 12:45:29 +0200 | [diff] [blame] | 930 | .headersize = sizeof(struct efi_runtime_services), |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 931 | }, |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 932 | .get_time = &efi_get_time_boottime, |
Heinrich Schuchardt | 411b2d5 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 933 | .set_time = &efi_set_time_boottime, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 934 | .get_wakeup_time = (void *)&efi_unimplemented, |
| 935 | .set_wakeup_time = (void *)&efi_unimplemented, |
| 936 | .set_virtual_address_map = &efi_set_virtual_address_map, |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 937 | .convert_pointer = efi_convert_pointer, |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 938 | .get_variable = efi_get_variable, |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 939 | .get_next_variable_name = efi_get_next_variable_name, |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 940 | .set_variable = efi_set_variable, |
Heinrich Schuchardt | 5337a6c | 2019-06-20 15:40:49 +0200 | [diff] [blame] | 941 | .get_next_high_mono_count = (void *)&efi_unimplemented, |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 942 | .reset_system = &efi_reset_system_boottime, |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 943 | .update_capsule = efi_update_capsule, |
| 944 | .query_capsule_caps = efi_query_capsule_caps, |
| 945 | .query_variable_info = efi_query_variable_info, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 946 | }; |