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