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