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> |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 14 | |
| 15 | /* For manual relocation support */ |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 18 | struct efi_runtime_mmio_list { |
| 19 | struct list_head link; |
| 20 | void **ptr; |
| 21 | u64 paddr; |
| 22 | u64 len; |
| 23 | }; |
| 24 | |
| 25 | /* This list contains all runtime available mmio regions */ |
| 26 | LIST_HEAD(efi_runtime_mmio); |
| 27 | |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 28 | static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void); |
| 29 | static efi_status_t __efi_runtime EFIAPI efi_device_error(void); |
| 30 | static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 31 | |
Simon Glass | fff89d4 | 2018-06-11 23:26:40 -0600 | [diff] [blame] | 32 | /* |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 33 | * TODO(sjg@chromium.org): These defines and structures should come from the ELF |
| 34 | * header for each architecture (or a generic header) rather than being repeated |
| 35 | * here. |
Simon Glass | fff89d4 | 2018-06-11 23:26:40 -0600 | [diff] [blame] | 36 | */ |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 37 | #if defined(__aarch64__) |
Alexander Graf | 46c1514 | 2018-06-18 17:23:11 +0200 | [diff] [blame] | 38 | #define R_RELATIVE R_AARCH64_RELATIVE |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 39 | #define R_MASK 0xffffffffULL |
| 40 | #define IS_RELA 1 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 41 | #elif defined(__arm__) |
Alexander Graf | 46c1514 | 2018-06-18 17:23:11 +0200 | [diff] [blame] | 42 | #define R_RELATIVE R_ARM_RELATIVE |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 43 | #define R_MASK 0xffULL |
Heinrich Schuchardt | 56e82be | 2018-10-13 20:52:08 -0700 | [diff] [blame] | 44 | #elif defined(__i386__) |
Simon Glass | cdfe696 | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 45 | #define R_RELATIVE R_386_RELATIVE |
| 46 | #define R_MASK 0xffULL |
Heinrich Schuchardt | 56e82be | 2018-10-13 20:52:08 -0700 | [diff] [blame] | 47 | #elif defined(__x86_64__) |
| 48 | #define R_RELATIVE R_X86_64_RELATIVE |
| 49 | #define R_MASK 0xffffffffULL |
| 50 | #define IS_RELA 1 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 51 | #elif defined(__riscv) |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 52 | #define R_RELATIVE R_RISCV_RELATIVE |
| 53 | #define R_MASK 0xffULL |
| 54 | #define IS_RELA 1 |
| 55 | |
| 56 | struct dyn_sym { |
| 57 | ulong foo1; |
| 58 | ulong addr; |
| 59 | u32 foo2; |
| 60 | u32 foo3; |
| 61 | }; |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 62 | #if (__riscv_xlen == 32) |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 63 | #define R_ABSOLUTE R_RISCV_32 |
| 64 | #define SYM_INDEX 8 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 65 | #elif (__riscv_xlen == 64) |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 66 | #define R_ABSOLUTE R_RISCV_64 |
| 67 | #define SYM_INDEX 32 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 68 | #else |
| 69 | #error unknown riscv target |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 70 | #endif |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 71 | #else |
| 72 | #error Need to add relocation awareness |
| 73 | #endif |
| 74 | |
| 75 | struct elf_rel { |
| 76 | ulong *offset; |
| 77 | ulong info; |
| 78 | }; |
| 79 | |
| 80 | struct elf_rela { |
| 81 | ulong *offset; |
| 82 | ulong info; |
| 83 | long addend; |
| 84 | }; |
| 85 | |
| 86 | /* |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 87 | * 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] | 88 | * payload are running concurrently at the same time. In this mode, we can |
| 89 | * handle a good number of runtime callbacks |
| 90 | */ |
| 91 | |
Heinrich Schuchardt | 69c1576 | 2018-07-29 09:49:04 +0200 | [diff] [blame] | 92 | /** |
| 93 | * efi_update_table_header_crc32() - Update crc32 in table header |
| 94 | * |
| 95 | * @table: EFI table |
| 96 | */ |
| 97 | void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table) |
| 98 | { |
| 99 | table->crc32 = 0; |
| 100 | table->crc32 = crc32(0, (const unsigned char *)table, |
| 101 | table->headersize); |
| 102 | } |
| 103 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 104 | /** |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 105 | * efi_reset_system_boottime() - reset system at boot time |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 106 | * |
| 107 | * This function implements the ResetSystem() runtime service before |
| 108 | * SetVirtualAddressMap() is called. |
| 109 | * |
| 110 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 111 | * details. |
| 112 | * |
| 113 | * @reset_type: type of reset to perform |
| 114 | * @reset_status: status code for the reset |
| 115 | * @data_size: size of reset_data |
| 116 | * @reset_data: information about the reset |
| 117 | */ |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 118 | static void EFIAPI efi_reset_system_boottime( |
| 119 | enum efi_reset_type reset_type, |
| 120 | efi_status_t reset_status, |
| 121 | unsigned long data_size, void *reset_data) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 122 | { |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 123 | struct efi_event *evt; |
| 124 | |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 125 | EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size, |
| 126 | reset_data); |
| 127 | |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 128 | /* Notify reset */ |
| 129 | list_for_each_entry(evt, &efi_events, link) { |
| 130 | if (evt->group && |
| 131 | !guidcmp(evt->group, |
| 132 | &efi_guid_event_group_reset_system)) { |
| 133 | efi_signal_event(evt, false); |
| 134 | break; |
| 135 | } |
| 136 | } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 137 | switch (reset_type) { |
| 138 | case EFI_RESET_COLD: |
| 139 | case EFI_RESET_WARM: |
Heinrich Schuchardt | 450d4c8 | 2018-02-06 22:00:22 +0100 | [diff] [blame] | 140 | case EFI_RESET_PLATFORM_SPECIFIC: |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 141 | do_reset(NULL, 0, 0, NULL); |
| 142 | break; |
| 143 | case EFI_RESET_SHUTDOWN: |
| 144 | /* We don't have anything to map this to */ |
| 145 | break; |
| 146 | } |
| 147 | |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 148 | while (1) { } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 151 | /** |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 152 | * efi_get_time_boottime() - get current time at boot time |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 153 | * |
| 154 | * This function implements the GetTime runtime service before |
| 155 | * SetVirtualAddressMap() is called. |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 156 | * |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 157 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 158 | * for details. |
| 159 | * |
| 160 | * @time: pointer to structure to receive current time |
| 161 | * @capabilities: pointer to structure to receive RTC properties |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 162 | * Returns: status code |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 163 | */ |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 164 | static efi_status_t EFIAPI efi_get_time_boottime( |
| 165 | struct efi_time *time, |
| 166 | struct efi_time_cap *capabilities) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 167 | { |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 168 | #ifdef CONFIG_DM_RTC |
| 169 | efi_status_t ret = EFI_SUCCESS; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 170 | int r; |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 171 | struct rtc_time tm; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 172 | struct udevice *dev; |
| 173 | |
| 174 | EFI_ENTRY("%p %p", time, capabilities); |
| 175 | |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 176 | if (!time) { |
| 177 | ret = EFI_INVALID_PARAMETER; |
| 178 | goto out; |
| 179 | } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 180 | |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 181 | r = uclass_get_device(UCLASS_RTC, 0, &dev); |
| 182 | if (!r) |
| 183 | r = dm_rtc_get(dev, &tm); |
| 184 | if (r) { |
| 185 | ret = EFI_DEVICE_ERROR; |
| 186 | goto out; |
| 187 | } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 188 | |
| 189 | memset(time, 0, sizeof(*time)); |
| 190 | time->year = tm.tm_year; |
| 191 | time->month = tm.tm_mon; |
| 192 | time->day = tm.tm_mday; |
| 193 | time->hour = tm.tm_hour; |
| 194 | time->minute = tm.tm_min; |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 195 | time->second = tm.tm_sec; |
| 196 | time->daylight = EFI_TIME_ADJUST_DAYLIGHT; |
| 197 | if (tm.tm_isdst > 0) |
| 198 | time->daylight |= EFI_TIME_IN_DAYLIGHT; |
| 199 | time->timezone = EFI_UNSPECIFIED_TIMEZONE; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 200 | |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 201 | if (capabilities) { |
| 202 | /* Set reasonable dummy values */ |
| 203 | capabilities->resolution = 1; /* 1 Hz */ |
| 204 | capabilities->accuracy = 100000000; /* 100 ppm */ |
| 205 | capabilities->sets_to_zero = false; |
| 206 | } |
| 207 | out: |
| 208 | return EFI_EXIT(ret); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 209 | #else |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 210 | EFI_ENTRY("%p %p", time, capabilities); |
| 211 | return EFI_EXIT(EFI_DEVICE_ERROR); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 212 | #endif |
| 213 | } |
| 214 | |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 215 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 216 | /** |
| 217 | * efi_reset_system() - reset system |
| 218 | * |
| 219 | * This function implements the ResetSystem() runtime service after |
| 220 | * SetVirtualAddressMap() is called. It only executes an endless loop. |
| 221 | * Boards may override the helpers below to implement reset functionality. |
| 222 | * |
| 223 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 224 | * details. |
| 225 | * |
| 226 | * @reset_type: type of reset to perform |
| 227 | * @reset_status: status code for the reset |
| 228 | * @data_size: size of reset_data |
| 229 | * @reset_data: information about the reset |
| 230 | */ |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 231 | void __weak __efi_runtime EFIAPI efi_reset_system( |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 232 | enum efi_reset_type reset_type, |
| 233 | efi_status_t reset_status, |
| 234 | unsigned long data_size, void *reset_data) |
| 235 | { |
| 236 | /* Nothing we can do */ |
| 237 | while (1) { } |
| 238 | } |
| 239 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 240 | /** |
| 241 | * efi_reset_system_init() - initialize the reset driver |
| 242 | * |
| 243 | * Boards may override this function to initialize the reset driver. |
| 244 | */ |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 245 | efi_status_t __weak efi_reset_system_init(void) |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 246 | { |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 247 | return EFI_SUCCESS; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 250 | /** |
| 251 | * efi_get_time() - get current time |
| 252 | * |
| 253 | * This function implements the GetTime runtime service after |
| 254 | * SetVirtualAddressMap() is called. As the U-Boot driver are not available |
| 255 | * anymore only an error code is returned. |
| 256 | * |
| 257 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 258 | * for details. |
| 259 | * |
| 260 | * @time: pointer to structure to receive current time |
| 261 | * @capabilities: pointer to structure to receive RTC properties |
| 262 | * Returns: status code |
| 263 | */ |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 264 | efi_status_t __weak __efi_runtime EFIAPI efi_get_time( |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 265 | struct efi_time *time, |
| 266 | struct efi_time_cap *capabilities) |
| 267 | { |
| 268 | /* Nothing we can do */ |
| 269 | return EFI_DEVICE_ERROR; |
| 270 | } |
| 271 | |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 272 | struct efi_runtime_detach_list_struct { |
| 273 | void *ptr; |
| 274 | void *patchto; |
| 275 | }; |
| 276 | |
| 277 | static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = { |
| 278 | { |
| 279 | /* do_reset is gone */ |
| 280 | .ptr = &efi_runtime_services.reset_system, |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 281 | .patchto = efi_reset_system, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 282 | }, { |
| 283 | /* invalidate_*cache_all are gone */ |
| 284 | .ptr = &efi_runtime_services.set_virtual_address_map, |
| 285 | .patchto = &efi_invalid_parameter, |
| 286 | }, { |
| 287 | /* RTC accessors are gone */ |
| 288 | .ptr = &efi_runtime_services.get_time, |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 289 | .patchto = &efi_get_time, |
Alexander Graf | c59d360 | 2016-05-18 00:54:47 +0200 | [diff] [blame] | 290 | }, { |
| 291 | /* Clean up system table */ |
| 292 | .ptr = &systab.con_in, |
| 293 | .patchto = NULL, |
| 294 | }, { |
| 295 | /* Clean up system table */ |
| 296 | .ptr = &systab.con_out, |
| 297 | .patchto = NULL, |
| 298 | }, { |
| 299 | /* Clean up system table */ |
| 300 | .ptr = &systab.std_err, |
| 301 | .patchto = NULL, |
| 302 | }, { |
| 303 | /* Clean up system table */ |
| 304 | .ptr = &systab.boottime, |
| 305 | .patchto = NULL, |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 306 | }, { |
| 307 | .ptr = &efi_runtime_services.get_variable, |
| 308 | .patchto = &efi_device_error, |
| 309 | }, { |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 310 | .ptr = &efi_runtime_services.get_next_variable_name, |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 311 | .patchto = &efi_device_error, |
| 312 | }, { |
| 313 | .ptr = &efi_runtime_services.set_variable, |
| 314 | .patchto = &efi_device_error, |
| 315 | } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 316 | }; |
| 317 | |
| 318 | static bool efi_runtime_tobedetached(void *p) |
| 319 | { |
| 320 | int i; |
| 321 | |
| 322 | for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) |
| 323 | if (efi_runtime_detach_list[i].ptr == p) |
| 324 | return true; |
| 325 | |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | static void efi_runtime_detach(ulong offset) |
| 330 | { |
| 331 | int i; |
| 332 | ulong patchoff = offset - (ulong)gd->relocaddr; |
| 333 | |
| 334 | for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) { |
| 335 | ulong patchto = (ulong)efi_runtime_detach_list[i].patchto; |
| 336 | ulong *p = efi_runtime_detach_list[i].ptr; |
| 337 | ulong newaddr = patchto ? (patchto + patchoff) : 0; |
| 338 | |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 339 | debug("%s: Setting %p to %lx\n", __func__, p, newaddr); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 340 | *p = newaddr; |
| 341 | } |
Heinrich Schuchardt | 69c1576 | 2018-07-29 09:49:04 +0200 | [diff] [blame] | 342 | |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 343 | /* Update CRC32 */ |
Heinrich Schuchardt | 69c1576 | 2018-07-29 09:49:04 +0200 | [diff] [blame] | 344 | efi_update_table_header_crc32(&efi_runtime_services.hdr); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* Relocate EFI runtime to uboot_reloc_base = offset */ |
| 348 | void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map) |
| 349 | { |
| 350 | #ifdef IS_RELA |
| 351 | struct elf_rela *rel = (void*)&__efi_runtime_rel_start; |
| 352 | #else |
| 353 | struct elf_rel *rel = (void*)&__efi_runtime_rel_start; |
| 354 | static ulong lastoff = CONFIG_SYS_TEXT_BASE; |
| 355 | #endif |
| 356 | |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 357 | debug("%s: Relocating to offset=%lx\n", __func__, offset); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 358 | for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) { |
| 359 | ulong base = CONFIG_SYS_TEXT_BASE; |
| 360 | ulong *p; |
| 361 | ulong newaddr; |
| 362 | |
| 363 | p = (void*)((ulong)rel->offset - base) + gd->relocaddr; |
| 364 | |
Heinrich Schuchardt | 56e82be | 2018-10-13 20:52:08 -0700 | [diff] [blame] | 365 | debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, |
| 366 | rel->info, *p, rel->offset); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 367 | |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 368 | switch (rel->info & R_MASK) { |
| 369 | case R_RELATIVE: |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 370 | #ifdef IS_RELA |
| 371 | newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE; |
| 372 | #else |
| 373 | newaddr = *p - lastoff + offset; |
| 374 | #endif |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 375 | break; |
| 376 | #ifdef R_ABSOLUTE |
| 377 | case R_ABSOLUTE: { |
| 378 | ulong symidx = rel->info >> SYM_INDEX; |
| 379 | extern struct dyn_sym __dyn_sym_start[]; |
| 380 | newaddr = __dyn_sym_start[symidx].addr + offset; |
| 381 | break; |
| 382 | } |
| 383 | #endif |
| 384 | default: |
Heinrich Schuchardt | 56e82be | 2018-10-13 20:52:08 -0700 | [diff] [blame] | 385 | if (!efi_runtime_tobedetached(p)) |
| 386 | printf("%s: Unknown relocation type %llx\n", |
| 387 | __func__, rel->info & R_MASK); |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 388 | continue; |
| 389 | } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 390 | |
| 391 | /* Check if the relocation is inside bounds */ |
| 392 | if (map && ((newaddr < map->virtual_start) || |
Heinrich Schuchardt | 9ebc47f | 2017-09-18 22:11:34 +0200 | [diff] [blame] | 393 | newaddr > (map->virtual_start + |
| 394 | (map->num_pages << EFI_PAGE_SHIFT)))) { |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 395 | if (!efi_runtime_tobedetached(p)) |
Heinrich Schuchardt | 56e82be | 2018-10-13 20:52:08 -0700 | [diff] [blame] | 396 | printf("%s: Relocation at %p is out of " |
| 397 | "range (%lx)\n", __func__, p, newaddr); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 398 | continue; |
| 399 | } |
| 400 | |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 401 | debug("%s: Setting %p to %lx\n", __func__, p, newaddr); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 402 | *p = newaddr; |
Alexander Graf | e570232 | 2016-04-11 23:20:39 +0200 | [diff] [blame] | 403 | flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1), |
| 404 | ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE)); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | #ifndef IS_RELA |
| 408 | lastoff = offset; |
| 409 | #endif |
| 410 | |
| 411 | invalidate_icache_all(); |
| 412 | } |
| 413 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 414 | /** |
| 415 | * efi_set_virtual_address_map() - change from physical to virtual mapping |
| 416 | * |
| 417 | * This function implements the SetVirtualAddressMap() runtime service. |
| 418 | * |
| 419 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 420 | * details. |
| 421 | * |
| 422 | * @memory_map_size: size of the virtual map |
| 423 | * @descriptor_size: size of an entry in the map |
| 424 | * @descriptor_version: version of the map entries |
| 425 | * @virtmap: virtual address mapping information |
| 426 | * Return: status code |
| 427 | */ |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 428 | static efi_status_t EFIAPI efi_set_virtual_address_map( |
| 429 | unsigned long memory_map_size, |
| 430 | unsigned long descriptor_size, |
| 431 | uint32_t descriptor_version, |
| 432 | struct efi_mem_desc *virtmap) |
| 433 | { |
Heinrich Schuchardt | 9ebc47f | 2017-09-18 22:11:34 +0200 | [diff] [blame] | 434 | ulong runtime_start = (ulong)&__efi_runtime_start & |
| 435 | ~(ulong)EFI_PAGE_MASK; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 436 | int n = memory_map_size / descriptor_size; |
| 437 | int i; |
| 438 | |
| 439 | EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size, |
| 440 | descriptor_version, virtmap); |
| 441 | |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 442 | /* Rebind mmio pointers */ |
| 443 | for (i = 0; i < n; i++) { |
| 444 | struct efi_mem_desc *map = (void*)virtmap + |
| 445 | (descriptor_size * i); |
| 446 | struct list_head *lhandle; |
| 447 | efi_physical_addr_t map_start = map->physical_start; |
| 448 | efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT; |
| 449 | efi_physical_addr_t map_end = map_start + map_len; |
Heinrich Schuchardt | 3905456 | 2018-08-04 23:16:06 +0200 | [diff] [blame] | 450 | u64 off = map->virtual_start - map_start; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 451 | |
| 452 | /* Adjust all mmio pointers in this region */ |
| 453 | list_for_each(lhandle, &efi_runtime_mmio) { |
| 454 | struct efi_runtime_mmio_list *lmmio; |
| 455 | |
| 456 | lmmio = list_entry(lhandle, |
| 457 | struct efi_runtime_mmio_list, |
| 458 | link); |
| 459 | if ((map_start <= lmmio->paddr) && |
| 460 | (map_end >= lmmio->paddr)) { |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 461 | uintptr_t new_addr = lmmio->paddr + off; |
| 462 | *lmmio->ptr = (void *)new_addr; |
| 463 | } |
| 464 | } |
Heinrich Schuchardt | 3905456 | 2018-08-04 23:16:06 +0200 | [diff] [blame] | 465 | if ((map_start <= (uintptr_t)systab.tables) && |
| 466 | (map_end >= (uintptr_t)systab.tables)) { |
| 467 | char *ptr = (char *)systab.tables; |
| 468 | |
| 469 | ptr += off; |
| 470 | systab.tables = (struct efi_configuration_table *)ptr; |
| 471 | } |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /* Move the actual runtime code over */ |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 475 | for (i = 0; i < n; i++) { |
| 476 | struct efi_mem_desc *map; |
| 477 | |
| 478 | map = (void*)virtmap + (descriptor_size * i); |
| 479 | if (map->type == EFI_RUNTIME_SERVICES_CODE) { |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 480 | ulong new_offset = map->virtual_start - |
| 481 | (runtime_start - gd->relocaddr); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 482 | |
| 483 | efi_runtime_relocate(new_offset, map); |
| 484 | /* Once we're virtual, we can no longer handle |
| 485 | complex callbacks */ |
| 486 | efi_runtime_detach(new_offset); |
| 487 | return EFI_EXIT(EFI_SUCCESS); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 492 | } |
| 493 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 494 | /** |
| 495 | * efi_add_runtime_mmio() - add memory-mapped IO region |
| 496 | * |
| 497 | * This function adds a memory-mapped IO region to the memory map to make it |
| 498 | * available at runtime. |
| 499 | * |
| 500 | * @mmio_ptr: address of the memory-mapped IO region |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 501 | * @len: size of the memory-mapped IO region |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 502 | * Returns: status code |
| 503 | */ |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 504 | efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len) |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 505 | { |
| 506 | struct efi_runtime_mmio_list *newmmio; |
xypron.glpk@gmx.de | 93fcc7f | 2017-08-11 21:19:37 +0200 | [diff] [blame] | 507 | u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; |
Alexander Graf | 33a83a3 | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 508 | uint64_t addr = *(uintptr_t *)mmio_ptr; |
| 509 | uint64_t retaddr; |
| 510 | |
| 511 | retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false); |
| 512 | if (retaddr != addr) |
| 513 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 514 | |
| 515 | newmmio = calloc(1, sizeof(*newmmio)); |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 516 | if (!newmmio) |
| 517 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 518 | newmmio->ptr = mmio_ptr; |
| 519 | newmmio->paddr = *(uintptr_t *)mmio_ptr; |
| 520 | newmmio->len = len; |
| 521 | list_add_tail(&newmmio->link, &efi_runtime_mmio); |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 522 | |
Alexander Graf | 33a83a3 | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 523 | return EFI_SUCCESS; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 526 | /* |
| 527 | * In the second stage, U-Boot has disappeared. To isolate our runtime code |
| 528 | * that at this point still exists from the rest, we put it into a special |
| 529 | * section. |
| 530 | * |
| 531 | * !!WARNING!! |
| 532 | * |
| 533 | * This means that we can not rely on any code outside of this file in any |
| 534 | * function or variable below this line. |
| 535 | * |
| 536 | * Please keep everything fully self-contained and annotated with |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 537 | * __efi_runtime and __efi_runtime_data markers. |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 538 | */ |
| 539 | |
| 540 | /* |
| 541 | * Relocate the EFI runtime stub to a different place. We need to call this |
| 542 | * the first time we expose the runtime interface to a user and on set virtual |
| 543 | * address map calls. |
| 544 | */ |
| 545 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 546 | /** |
| 547 | * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED |
| 548 | * |
| 549 | * This function is used after SetVirtualAddressMap() is called as replacement |
| 550 | * for services that are not available anymore due to constraints of the U-Boot |
| 551 | * implementation. |
| 552 | * |
| 553 | * Return: EFI_UNSUPPORTED |
| 554 | */ |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 555 | static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 556 | { |
| 557 | return EFI_UNSUPPORTED; |
| 558 | } |
| 559 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 560 | /** |
| 561 | * efi_device_error() - replacement function, returns EFI_DEVICE_ERROR |
| 562 | * |
| 563 | * This function is used after SetVirtualAddressMap() is called as replacement |
| 564 | * for services that are not available anymore due to constraints of the U-Boot |
| 565 | * implementation. |
| 566 | * |
| 567 | * Return: EFI_DEVICE_ERROR |
| 568 | */ |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 569 | static efi_status_t __efi_runtime EFIAPI efi_device_error(void) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 570 | { |
| 571 | return EFI_DEVICE_ERROR; |
| 572 | } |
| 573 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 574 | /** |
| 575 | * efi_invalid_parameter() - replacement function, returns EFI_INVALID_PARAMETER |
| 576 | * |
| 577 | * This function is used after SetVirtualAddressMap() is called as replacement |
| 578 | * for services that are not available anymore due to constraints of the U-Boot |
| 579 | * implementation. |
| 580 | * |
| 581 | * Return: EFI_INVALID_PARAMETER |
| 582 | */ |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 583 | static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 584 | { |
| 585 | return EFI_INVALID_PARAMETER; |
| 586 | } |
| 587 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 588 | /** |
| 589 | * efi_update_capsule() - process information from operating system |
| 590 | * |
| 591 | * This function implements the UpdateCapsule() runtime service. |
| 592 | * |
| 593 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 594 | * details. |
| 595 | * |
| 596 | * @capsule_header_array: pointer to array of virtual pointers |
| 597 | * @capsule_count: number of pointers in capsule_header_array |
| 598 | * @scatter_gather_list: pointer to arry of physical pointers |
| 599 | * Returns: status code |
| 600 | */ |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 601 | efi_status_t __efi_runtime EFIAPI efi_update_capsule( |
| 602 | struct efi_capsule_header **capsule_header_array, |
| 603 | efi_uintn_t capsule_count, |
| 604 | u64 scatter_gather_list) |
| 605 | { |
| 606 | return EFI_UNSUPPORTED; |
| 607 | } |
| 608 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 609 | /** |
| 610 | * efi_query_capsule_caps() - check if capsule is supported |
| 611 | * |
| 612 | * This function implements the QueryCapsuleCapabilities() runtime service. |
| 613 | * |
| 614 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 615 | * details. |
| 616 | * |
| 617 | * @capsule_header_array: pointer to array of virtual pointers |
| 618 | * @capsule_count: number of pointers in capsule_header_array |
Heinrich Schuchardt | 1d1be36 | 2018-09-03 20:59:25 +0200 | [diff] [blame] | 619 | * @maximum_capsule_size: maximum capsule size |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 620 | * @reset_type: type of reset needed for capsule update |
| 621 | * Returns: status code |
| 622 | */ |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 623 | efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps( |
| 624 | struct efi_capsule_header **capsule_header_array, |
| 625 | efi_uintn_t capsule_count, |
| 626 | u64 maximum_capsule_size, |
| 627 | u32 reset_type) |
| 628 | { |
| 629 | return EFI_UNSUPPORTED; |
| 630 | } |
| 631 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 632 | /** |
| 633 | * efi_query_variable_info() - get information about EFI variables |
| 634 | * |
| 635 | * This function implements the QueryVariableInfo() runtime service. |
| 636 | * |
| 637 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 638 | * details. |
| 639 | * |
| 640 | * @attributes: bitmask to select variables to be |
| 641 | * queried |
| 642 | * @maximum_variable_storage_size: maximum size of storage area for the |
| 643 | * selected variable types |
| 644 | * @remaining_variable_storage_size: remaining size of storage are for the |
| 645 | * selected variable types |
| 646 | * @maximum_variable_size: maximum size of a variable of the |
| 647 | * selected type |
| 648 | * Returns: status code |
| 649 | */ |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 650 | efi_status_t __efi_runtime EFIAPI efi_query_variable_info( |
| 651 | u32 attributes, |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 652 | u64 *maximum_variable_storage_size, |
| 653 | u64 *remaining_variable_storage_size, |
| 654 | u64 *maximum_variable_size) |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 655 | { |
| 656 | return EFI_UNSUPPORTED; |
| 657 | } |
| 658 | |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 659 | struct efi_runtime_services __efi_runtime_data efi_runtime_services = { |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 660 | .hdr = { |
| 661 | .signature = EFI_RUNTIME_SERVICES_SIGNATURE, |
Heinrich Schuchardt | e75b3cb | 2018-06-28 12:45:27 +0200 | [diff] [blame] | 662 | .revision = EFI_SPECIFICATION_VERSION, |
Heinrich Schuchardt | 1020425 | 2018-06-28 12:45:29 +0200 | [diff] [blame] | 663 | .headersize = sizeof(struct efi_runtime_services), |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 664 | }, |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 665 | .get_time = &efi_get_time_boottime, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 666 | .set_time = (void *)&efi_device_error, |
| 667 | .get_wakeup_time = (void *)&efi_unimplemented, |
| 668 | .set_wakeup_time = (void *)&efi_unimplemented, |
| 669 | .set_virtual_address_map = &efi_set_virtual_address_map, |
| 670 | .convert_pointer = (void *)&efi_invalid_parameter, |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 671 | .get_variable = efi_get_variable, |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 672 | .get_next_variable_name = efi_get_next_variable_name, |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 673 | .set_variable = efi_set_variable, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 674 | .get_next_high_mono_count = (void *)&efi_device_error, |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 675 | .reset_system = &efi_reset_system_boottime, |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 676 | .update_capsule = efi_update_capsule, |
| 677 | .query_capsule_caps = efi_query_capsule_caps, |
| 678 | .query_variable_info = efi_query_variable_info, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 679 | }; |