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 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 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, 128 }, 129 }; 130 131 static bool efi_runtime_tobedetached(void *p) 132 { 133 int i; 134 135 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) 136 if (efi_runtime_detach_list[i].ptr == p) 137 return true; 138 139 return false; 140 } 141 142 static void efi_runtime_detach(ulong offset) 143 { 144 int i; 145 ulong patchoff = offset - (ulong)gd->relocaddr; 146 147 for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) { 148 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto; 149 ulong *p = efi_runtime_detach_list[i].ptr; 150 ulong newaddr = patchto ? (patchto + patchoff) : 0; 151 152 #ifdef DEBUG_EFI 153 printf("%s: Setting %p to %lx\n", __func__, p, newaddr); 154 #endif 155 *p = newaddr; 156 } 157 } 158 159 /* Relocate EFI runtime to uboot_reloc_base = offset */ 160 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map) 161 { 162 #ifdef IS_RELA 163 struct elf_rela *rel = (void*)&__efi_runtime_rel_start; 164 #else 165 struct elf_rel *rel = (void*)&__efi_runtime_rel_start; 166 static ulong lastoff = CONFIG_SYS_TEXT_BASE; 167 #endif 168 169 #ifdef DEBUG_EFI 170 printf("%s: Relocating to offset=%lx\n", __func__, offset); 171 #endif 172 173 for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) { 174 ulong base = CONFIG_SYS_TEXT_BASE; 175 ulong *p; 176 ulong newaddr; 177 178 p = (void*)((ulong)rel->offset - base) + gd->relocaddr; 179 180 if ((rel->info & R_MASK) != R_RELATIVE) { 181 continue; 182 } 183 184 #ifdef IS_RELA 185 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE; 186 #else 187 newaddr = *p - lastoff + offset; 188 #endif 189 190 /* Check if the relocation is inside bounds */ 191 if (map && ((newaddr < map->virtual_start) || 192 newaddr > (map->virtual_start + (map->num_pages << 12)))) { 193 if (!efi_runtime_tobedetached(p)) 194 printf("U-Boot EFI: Relocation at %p is out of " 195 "range (%lx)\n", p, newaddr); 196 continue; 197 } 198 199 #ifdef DEBUG_EFI 200 printf("%s: Setting %p to %lx\n", __func__, p, newaddr); 201 #endif 202 203 *p = newaddr; 204 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1), 205 ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE)); 206 } 207 208 #ifndef IS_RELA 209 lastoff = offset; 210 #endif 211 212 invalidate_icache_all(); 213 } 214 215 static efi_status_t EFIAPI efi_set_virtual_address_map( 216 unsigned long memory_map_size, 217 unsigned long descriptor_size, 218 uint32_t descriptor_version, 219 struct efi_mem_desc *virtmap) 220 { 221 ulong runtime_start = (ulong)&__efi_runtime_start & ~0xfffULL; 222 int n = memory_map_size / descriptor_size; 223 int i; 224 225 EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size, 226 descriptor_version, virtmap); 227 228 for (i = 0; i < n; i++) { 229 struct efi_mem_desc *map; 230 231 map = (void*)virtmap + (descriptor_size * i); 232 if (map->type == EFI_RUNTIME_SERVICES_CODE) { 233 ulong new_offset = map->virtual_start - (runtime_start - gd->relocaddr); 234 235 efi_runtime_relocate(new_offset, map); 236 /* Once we're virtual, we can no longer handle 237 complex callbacks */ 238 efi_runtime_detach(new_offset); 239 return EFI_EXIT(EFI_SUCCESS); 240 } 241 } 242 243 return EFI_EXIT(EFI_INVALID_PARAMETER); 244 } 245 246 /* 247 * In the second stage, U-Boot has disappeared. To isolate our runtime code 248 * that at this point still exists from the rest, we put it into a special 249 * section. 250 * 251 * !!WARNING!! 252 * 253 * This means that we can not rely on any code outside of this file in any 254 * function or variable below this line. 255 * 256 * Please keep everything fully self-contained and annotated with 257 * EFI_RUNTIME_TEXT and EFI_RUNTIME_DATA markers. 258 */ 259 260 /* 261 * Relocate the EFI runtime stub to a different place. We need to call this 262 * the first time we expose the runtime interface to a user and on set virtual 263 * address map calls. 264 */ 265 266 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_unimplemented(void) 267 { 268 return EFI_UNSUPPORTED; 269 } 270 271 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void) 272 { 273 return EFI_DEVICE_ERROR; 274 } 275 276 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_invalid_parameter(void) 277 { 278 return EFI_INVALID_PARAMETER; 279 } 280 281 struct efi_runtime_services EFI_RUNTIME_DATA efi_runtime_services = { 282 .hdr = { 283 .signature = EFI_RUNTIME_SERVICES_SIGNATURE, 284 .revision = EFI_RUNTIME_SERVICES_REVISION, 285 .headersize = sizeof(struct efi_table_hdr), 286 }, 287 .get_time = &efi_get_time, 288 .set_time = (void *)&efi_device_error, 289 .get_wakeup_time = (void *)&efi_unimplemented, 290 .set_wakeup_time = (void *)&efi_unimplemented, 291 .set_virtual_address_map = &efi_set_virtual_address_map, 292 .convert_pointer = (void *)&efi_invalid_parameter, 293 .get_variable = (void *)&efi_device_error, 294 .get_next_variable = (void *)&efi_device_error, 295 .set_variable = (void *)&efi_device_error, 296 .get_next_high_mono_count = (void *)&efi_device_error, 297 .reset_system = &efi_reset_system, 298 }; 299