Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 1 | /* |
| 2 | * EFI application runtime services |
| 3 | * |
| 4 | * Copyright (c) 2016 Alexander Graf |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
| 11 | #include <dm.h> |
| 12 | #include <efi_loader.h> |
| 13 | #include <rtc.h> |
| 14 | #include <asm/global_data.h> |
| 15 | |
| 16 | /* For manual relocation support */ |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_unimplemented(void); |
| 20 | static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void); |
| 21 | static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_invalid_parameter(void); |
| 22 | |
Alexander Graf | e570232 | 2016-04-11 23:20:39 +0200 | [diff] [blame] | 23 | #ifdef CONFIG_SYS_CACHELINE_SIZE |
| 24 | #define EFI_CACHELINE_SIZE CONFIG_SYS_CACHELINE_SIZE |
| 25 | #else |
| 26 | /* Just use the greatest cache flush alignment requirement I'm aware of */ |
| 27 | #define EFI_CACHELINE_SIZE 128 |
| 28 | #endif |
| 29 | |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 30 | #if defined(CONFIG_ARM64) |
| 31 | #define R_RELATIVE 1027 |
| 32 | #define R_MASK 0xffffffffULL |
| 33 | #define IS_RELA 1 |
| 34 | #elif defined(CONFIG_ARM) |
| 35 | #define R_RELATIVE 23 |
| 36 | #define R_MASK 0xffULL |
| 37 | #else |
| 38 | #error Need to add relocation awareness |
| 39 | #endif |
| 40 | |
| 41 | struct elf_rel { |
| 42 | ulong *offset; |
| 43 | ulong info; |
| 44 | }; |
| 45 | |
| 46 | struct elf_rela { |
| 47 | ulong *offset; |
| 48 | ulong info; |
| 49 | long addend; |
| 50 | }; |
| 51 | |
| 52 | /* |
| 53 | * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI |
| 54 | * payload are running concurrently at the same time. In this mode, we can |
| 55 | * handle a good number of runtime callbacks |
| 56 | */ |
| 57 | |
| 58 | static void EFIAPI efi_reset_system(enum efi_reset_type reset_type, |
| 59 | efi_status_t reset_status, |
| 60 | unsigned long data_size, void *reset_data) |
| 61 | { |
| 62 | EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size, |
| 63 | reset_data); |
| 64 | |
| 65 | switch (reset_type) { |
| 66 | case EFI_RESET_COLD: |
| 67 | case EFI_RESET_WARM: |
| 68 | do_reset(NULL, 0, 0, NULL); |
| 69 | break; |
| 70 | case EFI_RESET_SHUTDOWN: |
| 71 | /* We don't have anything to map this to */ |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | EFI_EXIT(EFI_SUCCESS); |
| 76 | } |
| 77 | |
| 78 | static efi_status_t EFIAPI efi_get_time(struct efi_time *time, |
| 79 | struct efi_time_cap *capabilities) |
| 80 | { |
| 81 | #if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC) |
| 82 | struct rtc_time tm; |
| 83 | int r; |
| 84 | struct udevice *dev; |
| 85 | |
| 86 | EFI_ENTRY("%p %p", time, capabilities); |
| 87 | |
| 88 | r = uclass_get_device(UCLASS_RTC, 0, &dev); |
| 89 | if (r) |
| 90 | return EFI_EXIT(EFI_DEVICE_ERROR); |
| 91 | |
| 92 | r = dm_rtc_get(dev, &tm); |
| 93 | if (r) |
| 94 | return EFI_EXIT(EFI_DEVICE_ERROR); |
| 95 | |
| 96 | memset(time, 0, sizeof(*time)); |
| 97 | time->year = tm.tm_year; |
| 98 | time->month = tm.tm_mon; |
| 99 | time->day = tm.tm_mday; |
| 100 | time->hour = tm.tm_hour; |
| 101 | time->minute = tm.tm_min; |
| 102 | time->daylight = tm.tm_isdst; |
| 103 | |
| 104 | return EFI_EXIT(EFI_SUCCESS); |
| 105 | #else |
| 106 | return EFI_DEVICE_ERROR; |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | struct efi_runtime_detach_list_struct { |
| 111 | void *ptr; |
| 112 | void *patchto; |
| 113 | }; |
| 114 | |
| 115 | static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = { |
| 116 | { |
| 117 | /* do_reset is gone */ |
| 118 | .ptr = &efi_runtime_services.reset_system, |
| 119 | .patchto = NULL, |
| 120 | }, { |
| 121 | /* invalidate_*cache_all are gone */ |
| 122 | .ptr = &efi_runtime_services.set_virtual_address_map, |
| 123 | .patchto = &efi_invalid_parameter, |
| 124 | }, { |
| 125 | /* RTC accessors are gone */ |
| 126 | .ptr = &efi_runtime_services.get_time, |
| 127 | .patchto = &efi_device_error, |
Alexander Graf | c59d360 | 2016-05-18 00:54:47 +0200 | [diff] [blame^] | 128 | }, { |
| 129 | /* Clean up system table */ |
| 130 | .ptr = &systab.con_in, |
| 131 | .patchto = NULL, |
| 132 | }, { |
| 133 | /* Clean up system table */ |
| 134 | .ptr = &systab.con_out, |
| 135 | .patchto = NULL, |
| 136 | }, { |
| 137 | /* Clean up system table */ |
| 138 | .ptr = &systab.std_err, |
| 139 | .patchto = NULL, |
| 140 | }, { |
| 141 | /* Clean up system table */ |
| 142 | .ptr = &systab.boottime, |
| 143 | .patchto = NULL, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 144 | }, |
| 145 | }; |
| 146 | |
| 147 | static bool efi_runtime_tobedetached(void *p) |
| 148 | { |
| 149 | int i; |
| 150 | |
| 151 | for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) |
| 152 | if (efi_runtime_detach_list[i].ptr == p) |
| 153 | return true; |
| 154 | |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | static void efi_runtime_detach(ulong offset) |
| 159 | { |
| 160 | int i; |
| 161 | ulong patchoff = offset - (ulong)gd->relocaddr; |
| 162 | |
| 163 | for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) { |
| 164 | ulong patchto = (ulong)efi_runtime_detach_list[i].patchto; |
| 165 | ulong *p = efi_runtime_detach_list[i].ptr; |
| 166 | ulong newaddr = patchto ? (patchto + patchoff) : 0; |
| 167 | |
| 168 | #ifdef DEBUG_EFI |
| 169 | printf("%s: Setting %p to %lx\n", __func__, p, newaddr); |
| 170 | #endif |
| 171 | *p = newaddr; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /* Relocate EFI runtime to uboot_reloc_base = offset */ |
| 176 | void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map) |
| 177 | { |
| 178 | #ifdef IS_RELA |
| 179 | struct elf_rela *rel = (void*)&__efi_runtime_rel_start; |
| 180 | #else |
| 181 | struct elf_rel *rel = (void*)&__efi_runtime_rel_start; |
| 182 | static ulong lastoff = CONFIG_SYS_TEXT_BASE; |
| 183 | #endif |
| 184 | |
| 185 | #ifdef DEBUG_EFI |
| 186 | printf("%s: Relocating to offset=%lx\n", __func__, offset); |
| 187 | #endif |
| 188 | |
| 189 | for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) { |
| 190 | ulong base = CONFIG_SYS_TEXT_BASE; |
| 191 | ulong *p; |
| 192 | ulong newaddr; |
| 193 | |
| 194 | p = (void*)((ulong)rel->offset - base) + gd->relocaddr; |
| 195 | |
| 196 | if ((rel->info & R_MASK) != R_RELATIVE) { |
| 197 | continue; |
| 198 | } |
| 199 | |
| 200 | #ifdef IS_RELA |
| 201 | newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE; |
| 202 | #else |
| 203 | newaddr = *p - lastoff + offset; |
| 204 | #endif |
| 205 | |
| 206 | /* Check if the relocation is inside bounds */ |
| 207 | if (map && ((newaddr < map->virtual_start) || |
| 208 | newaddr > (map->virtual_start + (map->num_pages << 12)))) { |
| 209 | if (!efi_runtime_tobedetached(p)) |
| 210 | printf("U-Boot EFI: Relocation at %p is out of " |
| 211 | "range (%lx)\n", p, newaddr); |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | #ifdef DEBUG_EFI |
| 216 | printf("%s: Setting %p to %lx\n", __func__, p, newaddr); |
| 217 | #endif |
| 218 | |
| 219 | *p = newaddr; |
Alexander Graf | e570232 | 2016-04-11 23:20:39 +0200 | [diff] [blame] | 220 | flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1), |
| 221 | ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE)); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | #ifndef IS_RELA |
| 225 | lastoff = offset; |
| 226 | #endif |
| 227 | |
| 228 | invalidate_icache_all(); |
| 229 | } |
| 230 | |
| 231 | static efi_status_t EFIAPI efi_set_virtual_address_map( |
| 232 | unsigned long memory_map_size, |
| 233 | unsigned long descriptor_size, |
| 234 | uint32_t descriptor_version, |
| 235 | struct efi_mem_desc *virtmap) |
| 236 | { |
| 237 | ulong runtime_start = (ulong)&__efi_runtime_start & ~0xfffULL; |
| 238 | int n = memory_map_size / descriptor_size; |
| 239 | int i; |
| 240 | |
| 241 | EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size, |
| 242 | descriptor_version, virtmap); |
| 243 | |
| 244 | for (i = 0; i < n; i++) { |
| 245 | struct efi_mem_desc *map; |
| 246 | |
| 247 | map = (void*)virtmap + (descriptor_size * i); |
| 248 | if (map->type == EFI_RUNTIME_SERVICES_CODE) { |
| 249 | ulong new_offset = map->virtual_start - (runtime_start - gd->relocaddr); |
| 250 | |
| 251 | efi_runtime_relocate(new_offset, map); |
| 252 | /* Once we're virtual, we can no longer handle |
| 253 | complex callbacks */ |
| 254 | efi_runtime_detach(new_offset); |
| 255 | return EFI_EXIT(EFI_SUCCESS); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * In the second stage, U-Boot has disappeared. To isolate our runtime code |
| 264 | * that at this point still exists from the rest, we put it into a special |
| 265 | * section. |
| 266 | * |
| 267 | * !!WARNING!! |
| 268 | * |
| 269 | * This means that we can not rely on any code outside of this file in any |
| 270 | * function or variable below this line. |
| 271 | * |
| 272 | * Please keep everything fully self-contained and annotated with |
| 273 | * EFI_RUNTIME_TEXT and EFI_RUNTIME_DATA markers. |
| 274 | */ |
| 275 | |
| 276 | /* |
| 277 | * Relocate the EFI runtime stub to a different place. We need to call this |
| 278 | * the first time we expose the runtime interface to a user and on set virtual |
| 279 | * address map calls. |
| 280 | */ |
| 281 | |
| 282 | static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_unimplemented(void) |
| 283 | { |
| 284 | return EFI_UNSUPPORTED; |
| 285 | } |
| 286 | |
| 287 | static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void) |
| 288 | { |
| 289 | return EFI_DEVICE_ERROR; |
| 290 | } |
| 291 | |
| 292 | static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_invalid_parameter(void) |
| 293 | { |
| 294 | return EFI_INVALID_PARAMETER; |
| 295 | } |
| 296 | |
| 297 | struct efi_runtime_services EFI_RUNTIME_DATA efi_runtime_services = { |
| 298 | .hdr = { |
| 299 | .signature = EFI_RUNTIME_SERVICES_SIGNATURE, |
| 300 | .revision = EFI_RUNTIME_SERVICES_REVISION, |
| 301 | .headersize = sizeof(struct efi_table_hdr), |
| 302 | }, |
| 303 | .get_time = &efi_get_time, |
| 304 | .set_time = (void *)&efi_device_error, |
| 305 | .get_wakeup_time = (void *)&efi_unimplemented, |
| 306 | .set_wakeup_time = (void *)&efi_unimplemented, |
| 307 | .set_virtual_address_map = &efi_set_virtual_address_map, |
| 308 | .convert_pointer = (void *)&efi_invalid_parameter, |
| 309 | .get_variable = (void *)&efi_device_error, |
| 310 | .get_next_variable = (void *)&efi_device_error, |
| 311 | .set_variable = (void *)&efi_device_error, |
| 312 | .get_next_high_mono_count = (void *)&efi_device_error, |
| 313 | .reset_system = &efi_reset_system, |
| 314 | }; |