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 | /* |
| 33 | * TODO(sjg@chromium.org): These defines and structs should come from the elf |
| 34 | * header for each arch (or a generic header) rather than being repeated here. |
| 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 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 43 | #elif defined(__x86_64__) || 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 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 46 | #elif defined(__riscv) |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 47 | #define R_RELATIVE R_RISCV_RELATIVE |
| 48 | #define R_MASK 0xffULL |
| 49 | #define IS_RELA 1 |
| 50 | |
| 51 | struct dyn_sym { |
| 52 | ulong foo1; |
| 53 | ulong addr; |
| 54 | u32 foo2; |
| 55 | u32 foo3; |
| 56 | }; |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 57 | #if (__riscv_xlen == 32) |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 58 | #define R_ABSOLUTE R_RISCV_32 |
| 59 | #define SYM_INDEX 8 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 60 | #elif (__riscv_xlen == 64) |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 61 | #define R_ABSOLUTE R_RISCV_64 |
| 62 | #define SYM_INDEX 32 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 63 | #else |
| 64 | #error unknown riscv target |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 65 | #endif |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 66 | #else |
| 67 | #error Need to add relocation awareness |
| 68 | #endif |
| 69 | |
| 70 | struct elf_rel { |
| 71 | ulong *offset; |
| 72 | ulong info; |
| 73 | }; |
| 74 | |
| 75 | struct elf_rela { |
| 76 | ulong *offset; |
| 77 | ulong info; |
| 78 | long addend; |
| 79 | }; |
| 80 | |
| 81 | /* |
| 82 | * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI |
| 83 | * payload are running concurrently at the same time. In this mode, we can |
| 84 | * handle a good number of runtime callbacks |
| 85 | */ |
| 86 | |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 87 | static void EFIAPI efi_reset_system_boottime( |
| 88 | enum efi_reset_type reset_type, |
| 89 | efi_status_t reset_status, |
| 90 | unsigned long data_size, void *reset_data) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 91 | { |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 92 | struct efi_event *evt; |
| 93 | |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 94 | EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size, |
| 95 | reset_data); |
| 96 | |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 97 | /* Notify reset */ |
| 98 | list_for_each_entry(evt, &efi_events, link) { |
| 99 | if (evt->group && |
| 100 | !guidcmp(evt->group, |
| 101 | &efi_guid_event_group_reset_system)) { |
| 102 | efi_signal_event(evt, false); |
| 103 | break; |
| 104 | } |
| 105 | } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 106 | switch (reset_type) { |
| 107 | case EFI_RESET_COLD: |
| 108 | case EFI_RESET_WARM: |
Heinrich Schuchardt | 450d4c8 | 2018-02-06 22:00:22 +0100 | [diff] [blame] | 109 | case EFI_RESET_PLATFORM_SPECIFIC: |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 110 | do_reset(NULL, 0, 0, NULL); |
| 111 | break; |
| 112 | case EFI_RESET_SHUTDOWN: |
| 113 | /* We don't have anything to map this to */ |
| 114 | break; |
| 115 | } |
| 116 | |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 117 | while (1) { } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 120 | /** |
| 121 | * efi_get_time_boottime - get current time |
| 122 | * |
| 123 | * This function implements the GetTime runtime service. |
| 124 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 125 | * for details. |
| 126 | * |
| 127 | * @time: pointer to structure to receive current time |
| 128 | * @capabilities: pointer to structure to receive RTC properties |
| 129 | * Return Value: status code |
| 130 | */ |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 131 | static efi_status_t EFIAPI efi_get_time_boottime( |
| 132 | struct efi_time *time, |
| 133 | struct efi_time_cap *capabilities) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 134 | { |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 135 | #ifdef CONFIG_DM_RTC |
| 136 | efi_status_t ret = EFI_SUCCESS; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 137 | int r; |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 138 | struct rtc_time tm; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 139 | struct udevice *dev; |
| 140 | |
| 141 | EFI_ENTRY("%p %p", time, capabilities); |
| 142 | |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 143 | if (!time) { |
| 144 | ret = EFI_INVALID_PARAMETER; |
| 145 | goto out; |
| 146 | } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 147 | |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 148 | r = uclass_get_device(UCLASS_RTC, 0, &dev); |
| 149 | if (!r) |
| 150 | r = dm_rtc_get(dev, &tm); |
| 151 | if (r) { |
| 152 | ret = EFI_DEVICE_ERROR; |
| 153 | goto out; |
| 154 | } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 155 | |
| 156 | memset(time, 0, sizeof(*time)); |
| 157 | time->year = tm.tm_year; |
| 158 | time->month = tm.tm_mon; |
| 159 | time->day = tm.tm_mday; |
| 160 | time->hour = tm.tm_hour; |
| 161 | time->minute = tm.tm_min; |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 162 | time->second = tm.tm_sec; |
| 163 | time->daylight = EFI_TIME_ADJUST_DAYLIGHT; |
| 164 | if (tm.tm_isdst > 0) |
| 165 | time->daylight |= EFI_TIME_IN_DAYLIGHT; |
| 166 | time->timezone = EFI_UNSPECIFIED_TIMEZONE; |
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 | if (capabilities) { |
| 169 | /* Set reasonable dummy values */ |
| 170 | capabilities->resolution = 1; /* 1 Hz */ |
| 171 | capabilities->accuracy = 100000000; /* 100 ppm */ |
| 172 | capabilities->sets_to_zero = false; |
| 173 | } |
| 174 | out: |
| 175 | return EFI_EXIT(ret); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 176 | #else |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 177 | EFI_ENTRY("%p %p", time, capabilities); |
| 178 | return EFI_EXIT(EFI_DEVICE_ERROR); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 179 | #endif |
| 180 | } |
| 181 | |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 182 | /* Boards may override the helpers below to implement RTS functionality */ |
| 183 | |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 184 | void __weak __efi_runtime EFIAPI efi_reset_system( |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 185 | enum efi_reset_type reset_type, |
| 186 | efi_status_t reset_status, |
| 187 | unsigned long data_size, void *reset_data) |
| 188 | { |
| 189 | /* Nothing we can do */ |
| 190 | while (1) { } |
| 191 | } |
| 192 | |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 193 | efi_status_t __weak efi_reset_system_init(void) |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 194 | { |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 195 | return EFI_SUCCESS; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 198 | efi_status_t __weak __efi_runtime EFIAPI efi_get_time( |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 199 | struct efi_time *time, |
| 200 | struct efi_time_cap *capabilities) |
| 201 | { |
| 202 | /* Nothing we can do */ |
| 203 | return EFI_DEVICE_ERROR; |
| 204 | } |
| 205 | |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 206 | struct efi_runtime_detach_list_struct { |
| 207 | void *ptr; |
| 208 | void *patchto; |
| 209 | }; |
| 210 | |
| 211 | static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = { |
| 212 | { |
| 213 | /* do_reset is gone */ |
| 214 | .ptr = &efi_runtime_services.reset_system, |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 215 | .patchto = efi_reset_system, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 216 | }, { |
| 217 | /* invalidate_*cache_all are gone */ |
| 218 | .ptr = &efi_runtime_services.set_virtual_address_map, |
| 219 | .patchto = &efi_invalid_parameter, |
| 220 | }, { |
| 221 | /* RTC accessors are gone */ |
| 222 | .ptr = &efi_runtime_services.get_time, |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 223 | .patchto = &efi_get_time, |
Alexander Graf | c59d360 | 2016-05-18 00:54:47 +0200 | [diff] [blame] | 224 | }, { |
| 225 | /* Clean up system table */ |
| 226 | .ptr = &systab.con_in, |
| 227 | .patchto = NULL, |
| 228 | }, { |
| 229 | /* Clean up system table */ |
| 230 | .ptr = &systab.con_out, |
| 231 | .patchto = NULL, |
| 232 | }, { |
| 233 | /* Clean up system table */ |
| 234 | .ptr = &systab.std_err, |
| 235 | .patchto = NULL, |
| 236 | }, { |
| 237 | /* Clean up system table */ |
| 238 | .ptr = &systab.boottime, |
| 239 | .patchto = NULL, |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 240 | }, { |
| 241 | .ptr = &efi_runtime_services.get_variable, |
| 242 | .patchto = &efi_device_error, |
| 243 | }, { |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 244 | .ptr = &efi_runtime_services.get_next_variable_name, |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 245 | .patchto = &efi_device_error, |
| 246 | }, { |
| 247 | .ptr = &efi_runtime_services.set_variable, |
| 248 | .patchto = &efi_device_error, |
| 249 | } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | static bool efi_runtime_tobedetached(void *p) |
| 253 | { |
| 254 | int i; |
| 255 | |
| 256 | for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) |
| 257 | if (efi_runtime_detach_list[i].ptr == p) |
| 258 | return true; |
| 259 | |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | static void efi_runtime_detach(ulong offset) |
| 264 | { |
| 265 | int i; |
| 266 | ulong patchoff = offset - (ulong)gd->relocaddr; |
| 267 | |
| 268 | for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) { |
| 269 | ulong patchto = (ulong)efi_runtime_detach_list[i].patchto; |
| 270 | ulong *p = efi_runtime_detach_list[i].ptr; |
| 271 | ulong newaddr = patchto ? (patchto + patchoff) : 0; |
| 272 | |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 273 | debug("%s: Setting %p to %lx\n", __func__, p, newaddr); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 274 | *p = newaddr; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* Relocate EFI runtime to uboot_reloc_base = offset */ |
| 279 | void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map) |
| 280 | { |
| 281 | #ifdef IS_RELA |
| 282 | struct elf_rela *rel = (void*)&__efi_runtime_rel_start; |
| 283 | #else |
| 284 | struct elf_rel *rel = (void*)&__efi_runtime_rel_start; |
| 285 | static ulong lastoff = CONFIG_SYS_TEXT_BASE; |
| 286 | #endif |
| 287 | |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 288 | debug("%s: Relocating to offset=%lx\n", __func__, offset); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 289 | for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) { |
| 290 | ulong base = CONFIG_SYS_TEXT_BASE; |
| 291 | ulong *p; |
| 292 | ulong newaddr; |
| 293 | |
| 294 | p = (void*)((ulong)rel->offset - base) + gd->relocaddr; |
| 295 | |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 296 | 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] | 297 | |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 298 | switch (rel->info & R_MASK) { |
| 299 | case R_RELATIVE: |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 300 | #ifdef IS_RELA |
| 301 | newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE; |
| 302 | #else |
| 303 | newaddr = *p - lastoff + offset; |
| 304 | #endif |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 305 | break; |
| 306 | #ifdef R_ABSOLUTE |
| 307 | case R_ABSOLUTE: { |
| 308 | ulong symidx = rel->info >> SYM_INDEX; |
| 309 | extern struct dyn_sym __dyn_sym_start[]; |
| 310 | newaddr = __dyn_sym_start[symidx].addr + offset; |
| 311 | break; |
| 312 | } |
| 313 | #endif |
| 314 | default: |
| 315 | continue; |
| 316 | } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 317 | |
| 318 | /* Check if the relocation is inside bounds */ |
| 319 | if (map && ((newaddr < map->virtual_start) || |
Heinrich Schuchardt | 9ebc47f | 2017-09-18 22:11:34 +0200 | [diff] [blame] | 320 | newaddr > (map->virtual_start + |
| 321 | (map->num_pages << EFI_PAGE_SHIFT)))) { |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 322 | if (!efi_runtime_tobedetached(p)) |
| 323 | printf("U-Boot EFI: Relocation at %p is out of " |
| 324 | "range (%lx)\n", p, newaddr); |
| 325 | continue; |
| 326 | } |
| 327 | |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 328 | debug("%s: Setting %p to %lx\n", __func__, p, newaddr); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 329 | *p = newaddr; |
Alexander Graf | e570232 | 2016-04-11 23:20:39 +0200 | [diff] [blame] | 330 | flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1), |
| 331 | ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE)); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | #ifndef IS_RELA |
| 335 | lastoff = offset; |
| 336 | #endif |
| 337 | |
| 338 | invalidate_icache_all(); |
| 339 | } |
| 340 | |
| 341 | static efi_status_t EFIAPI efi_set_virtual_address_map( |
| 342 | unsigned long memory_map_size, |
| 343 | unsigned long descriptor_size, |
| 344 | uint32_t descriptor_version, |
| 345 | struct efi_mem_desc *virtmap) |
| 346 | { |
Heinrich Schuchardt | 9ebc47f | 2017-09-18 22:11:34 +0200 | [diff] [blame] | 347 | ulong runtime_start = (ulong)&__efi_runtime_start & |
| 348 | ~(ulong)EFI_PAGE_MASK; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 349 | int n = memory_map_size / descriptor_size; |
| 350 | int i; |
| 351 | |
| 352 | EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size, |
| 353 | descriptor_version, virtmap); |
| 354 | |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 355 | /* Rebind mmio pointers */ |
| 356 | for (i = 0; i < n; i++) { |
| 357 | struct efi_mem_desc *map = (void*)virtmap + |
| 358 | (descriptor_size * i); |
| 359 | struct list_head *lhandle; |
| 360 | efi_physical_addr_t map_start = map->physical_start; |
| 361 | efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT; |
| 362 | efi_physical_addr_t map_end = map_start + map_len; |
Heinrich Schuchardt | 3905456 | 2018-08-04 23:16:06 +0200 | [diff] [blame^] | 363 | u64 off = map->virtual_start - map_start; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 364 | |
| 365 | /* Adjust all mmio pointers in this region */ |
| 366 | list_for_each(lhandle, &efi_runtime_mmio) { |
| 367 | struct efi_runtime_mmio_list *lmmio; |
| 368 | |
| 369 | lmmio = list_entry(lhandle, |
| 370 | struct efi_runtime_mmio_list, |
| 371 | link); |
| 372 | if ((map_start <= lmmio->paddr) && |
| 373 | (map_end >= lmmio->paddr)) { |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 374 | uintptr_t new_addr = lmmio->paddr + off; |
| 375 | *lmmio->ptr = (void *)new_addr; |
| 376 | } |
| 377 | } |
Heinrich Schuchardt | 3905456 | 2018-08-04 23:16:06 +0200 | [diff] [blame^] | 378 | if ((map_start <= (uintptr_t)systab.tables) && |
| 379 | (map_end >= (uintptr_t)systab.tables)) { |
| 380 | char *ptr = (char *)systab.tables; |
| 381 | |
| 382 | ptr += off; |
| 383 | systab.tables = (struct efi_configuration_table *)ptr; |
| 384 | } |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* Move the actual runtime code over */ |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 388 | for (i = 0; i < n; i++) { |
| 389 | struct efi_mem_desc *map; |
| 390 | |
| 391 | map = (void*)virtmap + (descriptor_size * i); |
| 392 | if (map->type == EFI_RUNTIME_SERVICES_CODE) { |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 393 | ulong new_offset = map->virtual_start - |
| 394 | (runtime_start - gd->relocaddr); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 395 | |
| 396 | efi_runtime_relocate(new_offset, map); |
| 397 | /* Once we're virtual, we can no longer handle |
| 398 | complex callbacks */ |
| 399 | efi_runtime_detach(new_offset); |
| 400 | return EFI_EXIT(EFI_SUCCESS); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 405 | } |
| 406 | |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 407 | efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len) |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 408 | { |
| 409 | struct efi_runtime_mmio_list *newmmio; |
xypron.glpk@gmx.de | 93fcc7f | 2017-08-11 21:19:37 +0200 | [diff] [blame] | 410 | u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT; |
Alexander Graf | 33a83a3 | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 411 | uint64_t addr = *(uintptr_t *)mmio_ptr; |
| 412 | uint64_t retaddr; |
| 413 | |
| 414 | retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false); |
| 415 | if (retaddr != addr) |
| 416 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 417 | |
| 418 | newmmio = calloc(1, sizeof(*newmmio)); |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 419 | if (!newmmio) |
| 420 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 421 | newmmio->ptr = mmio_ptr; |
| 422 | newmmio->paddr = *(uintptr_t *)mmio_ptr; |
| 423 | newmmio->len = len; |
| 424 | list_add_tail(&newmmio->link, &efi_runtime_mmio); |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 425 | |
Alexander Graf | 33a83a3 | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 426 | return EFI_SUCCESS; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 427 | } |
| 428 | |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 429 | /* |
| 430 | * In the second stage, U-Boot has disappeared. To isolate our runtime code |
| 431 | * that at this point still exists from the rest, we put it into a special |
| 432 | * section. |
| 433 | * |
| 434 | * !!WARNING!! |
| 435 | * |
| 436 | * This means that we can not rely on any code outside of this file in any |
| 437 | * function or variable below this line. |
| 438 | * |
| 439 | * Please keep everything fully self-contained and annotated with |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 440 | * __efi_runtime and __efi_runtime_data markers. |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 441 | */ |
| 442 | |
| 443 | /* |
| 444 | * Relocate the EFI runtime stub to a different place. We need to call this |
| 445 | * the first time we expose the runtime interface to a user and on set virtual |
| 446 | * address map calls. |
| 447 | */ |
| 448 | |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 449 | static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 450 | { |
| 451 | return EFI_UNSUPPORTED; |
| 452 | } |
| 453 | |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 454 | static efi_status_t __efi_runtime EFIAPI efi_device_error(void) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 455 | { |
| 456 | return EFI_DEVICE_ERROR; |
| 457 | } |
| 458 | |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 459 | static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 460 | { |
| 461 | return EFI_INVALID_PARAMETER; |
| 462 | } |
| 463 | |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 464 | efi_status_t __efi_runtime EFIAPI efi_update_capsule( |
| 465 | struct efi_capsule_header **capsule_header_array, |
| 466 | efi_uintn_t capsule_count, |
| 467 | u64 scatter_gather_list) |
| 468 | { |
| 469 | return EFI_UNSUPPORTED; |
| 470 | } |
| 471 | |
| 472 | efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps( |
| 473 | struct efi_capsule_header **capsule_header_array, |
| 474 | efi_uintn_t capsule_count, |
| 475 | u64 maximum_capsule_size, |
| 476 | u32 reset_type) |
| 477 | { |
| 478 | return EFI_UNSUPPORTED; |
| 479 | } |
| 480 | |
| 481 | efi_status_t __efi_runtime EFIAPI efi_query_variable_info( |
| 482 | u32 attributes, |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 483 | u64 *maximum_variable_storage_size, |
| 484 | u64 *remaining_variable_storage_size, |
| 485 | u64 *maximum_variable_size) |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 486 | { |
| 487 | return EFI_UNSUPPORTED; |
| 488 | } |
| 489 | |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 490 | struct efi_runtime_services __efi_runtime_data efi_runtime_services = { |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 491 | .hdr = { |
| 492 | .signature = EFI_RUNTIME_SERVICES_SIGNATURE, |
Heinrich Schuchardt | e75b3cb | 2018-06-28 12:45:27 +0200 | [diff] [blame] | 493 | .revision = EFI_SPECIFICATION_VERSION, |
Heinrich Schuchardt | 1020425 | 2018-06-28 12:45:29 +0200 | [diff] [blame] | 494 | .headersize = sizeof(struct efi_runtime_services), |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 495 | }, |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 496 | .get_time = &efi_get_time_boottime, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 497 | .set_time = (void *)&efi_device_error, |
| 498 | .get_wakeup_time = (void *)&efi_unimplemented, |
| 499 | .set_wakeup_time = (void *)&efi_unimplemented, |
| 500 | .set_virtual_address_map = &efi_set_virtual_address_map, |
| 501 | .convert_pointer = (void *)&efi_invalid_parameter, |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 502 | .get_variable = efi_get_variable, |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 503 | .get_next_variable_name = efi_get_next_variable_name, |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 504 | .set_variable = efi_set_variable, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 505 | .get_next_high_mono_count = (void *)&efi_device_error, |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 506 | .reset_system = &efi_reset_system_boottime, |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 507 | .update_capsule = efi_update_capsule, |
| 508 | .query_capsule_caps = efi_query_capsule_caps, |
| 509 | .query_variable_info = efi_query_variable_info, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 510 | }; |