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