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