1 /* 2 * EFI application boot time services 3 * 4 * Copyright (c) 2016 Alexander Graf 5 * 6 * SPDX-License-Identifier: GPL-2.0+ 7 */ 8 9 #include <common.h> 10 #include <efi_loader.h> 11 #include <malloc.h> 12 #include <asm/global_data.h> 13 #include <linux/libfdt_env.h> 14 #include <u-boot/crc.h> 15 #include <bootm.h> 16 #include <inttypes.h> 17 #include <watchdog.h> 18 19 DECLARE_GLOBAL_DATA_PTR; 20 21 /* This list contains all the EFI objects our payload has access to */ 22 LIST_HEAD(efi_obj_list); 23 24 /* 25 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux) 26 * we need to do trickery with caches. Since we don't want to break the EFI 27 * aware boot path, only apply hacks when loading exiting directly (breaking 28 * direct Linux EFI booting along the way - oh well). 29 */ 30 static bool efi_is_direct_boot = true; 31 32 /* 33 * EFI can pass arbitrary additional "tables" containing vendor specific 34 * information to the payload. One such table is the FDT table which contains 35 * a pointer to a flattened device tree blob. 36 * 37 * In most cases we want to pass an FDT to the payload, so reserve one slot of 38 * config table space for it. The pointer gets populated by do_bootefi_exec(). 39 */ 40 static struct efi_configuration_table __efi_runtime_data efi_conf_table[2]; 41 42 #ifdef CONFIG_ARM 43 /* 44 * The "gd" pointer lives in a register on ARM and AArch64 that we declare 45 * fixed when compiling U-Boot. However, the payload does not know about that 46 * restriction so we need to manually swap its and our view of that register on 47 * EFI callback entry/exit. 48 */ 49 static volatile void *efi_gd, *app_gd; 50 #endif 51 52 static int entry_count; 53 static int nesting_level; 54 55 /* Called on every callback entry */ 56 int __efi_entry_check(void) 57 { 58 int ret = entry_count++ == 0; 59 #ifdef CONFIG_ARM 60 assert(efi_gd); 61 app_gd = gd; 62 gd = efi_gd; 63 #endif 64 return ret; 65 } 66 67 /* Called on every callback exit */ 68 int __efi_exit_check(void) 69 { 70 int ret = --entry_count == 0; 71 #ifdef CONFIG_ARM 72 gd = app_gd; 73 #endif 74 return ret; 75 } 76 77 /* Called from do_bootefi_exec() */ 78 void efi_save_gd(void) 79 { 80 #ifdef CONFIG_ARM 81 efi_gd = gd; 82 #endif 83 } 84 85 /* 86 * Special case handler for error/abort that just forces things back 87 * to u-boot world so we can dump out an abort msg, without any care 88 * about returning back to UEFI world. 89 */ 90 void efi_restore_gd(void) 91 { 92 #ifdef CONFIG_ARM 93 /* Only restore if we're already in EFI context */ 94 if (!efi_gd) 95 return; 96 gd = efi_gd; 97 #endif 98 } 99 100 /* 101 * Two spaces per indent level, maxing out at 10.. which ought to be 102 * enough for anyone ;-) 103 */ 104 static const char *indent_string(int level) 105 { 106 const char *indent = " "; 107 const int max = strlen(indent); 108 level = min(max, level * 2); 109 return &indent[max - level]; 110 } 111 112 const char *__efi_nesting_inc(void) 113 { 114 return indent_string(nesting_level++); 115 } 116 117 const char *__efi_nesting_dec(void) 118 { 119 return indent_string(--nesting_level); 120 } 121 122 /* Low 32 bit */ 123 #define EFI_LOW32(a) (a & 0xFFFFFFFFULL) 124 /* High 32 bit */ 125 #define EFI_HIGH32(a) (a >> 32) 126 127 /* 128 * 64bit division by 10 implemented as multiplication by 1 / 10 129 * 130 * Decimals of one tenth: 0x1 / 0xA = 0x0.19999... 131 */ 132 #define EFI_TENTH 0x199999999999999A 133 static u64 efi_div10(u64 a) 134 { 135 u64 prod; 136 u64 rem; 137 u64 ret; 138 139 ret = EFI_HIGH32(a) * EFI_HIGH32(EFI_TENTH); 140 prod = EFI_HIGH32(a) * EFI_LOW32(EFI_TENTH); 141 rem = EFI_LOW32(prod); 142 ret += EFI_HIGH32(prod); 143 prod = EFI_LOW32(a) * EFI_HIGH32(EFI_TENTH); 144 rem += EFI_LOW32(prod); 145 ret += EFI_HIGH32(prod); 146 prod = EFI_LOW32(a) * EFI_LOW32(EFI_TENTH); 147 rem += EFI_HIGH32(prod); 148 ret += EFI_HIGH32(rem); 149 /* Round to nearest integer */ 150 if (rem >= (1 << 31)) 151 ++ret; 152 return ret; 153 } 154 155 void efi_signal_event(struct efi_event *event) 156 { 157 if (event->signaled) 158 return; 159 event->signaled = 1; 160 if (event->type & EVT_NOTIFY_SIGNAL) { 161 EFI_CALL(event->notify_function(event, event->notify_context)); 162 } 163 } 164 165 static efi_status_t efi_unsupported(const char *funcname) 166 { 167 debug("EFI: App called into unimplemented function %s\n", funcname); 168 return EFI_EXIT(EFI_UNSUPPORTED); 169 } 170 171 static unsigned long EFIAPI efi_raise_tpl(UINTN new_tpl) 172 { 173 EFI_ENTRY("0x%zx", new_tpl); 174 return EFI_EXIT(0); 175 } 176 177 static void EFIAPI efi_restore_tpl(UINTN old_tpl) 178 { 179 EFI_ENTRY("0x%zx", old_tpl); 180 efi_unsupported(__func__); 181 } 182 183 static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type, 184 unsigned long pages, 185 uint64_t *memory) 186 { 187 efi_status_t r; 188 189 EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory); 190 r = efi_allocate_pages(type, memory_type, pages, memory); 191 return EFI_EXIT(r); 192 } 193 194 static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory, 195 unsigned long pages) 196 { 197 efi_status_t r; 198 199 EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages); 200 r = efi_free_pages(memory, pages); 201 return EFI_EXIT(r); 202 } 203 204 static efi_status_t EFIAPI efi_get_memory_map_ext( 205 unsigned long *memory_map_size, 206 struct efi_mem_desc *memory_map, 207 unsigned long *map_key, 208 unsigned long *descriptor_size, 209 uint32_t *descriptor_version) 210 { 211 efi_status_t r; 212 213 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map, 214 map_key, descriptor_size, descriptor_version); 215 r = efi_get_memory_map(memory_map_size, memory_map, map_key, 216 descriptor_size, descriptor_version); 217 return EFI_EXIT(r); 218 } 219 220 static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type, 221 unsigned long size, 222 void **buffer) 223 { 224 efi_status_t r; 225 226 EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer); 227 r = efi_allocate_pool(pool_type, size, buffer); 228 return EFI_EXIT(r); 229 } 230 231 static efi_status_t EFIAPI efi_free_pool_ext(void *buffer) 232 { 233 efi_status_t r; 234 235 EFI_ENTRY("%p", buffer); 236 r = efi_free_pool(buffer); 237 return EFI_EXIT(r); 238 } 239 240 /* 241 * Our event capabilities are very limited. Only a small limited 242 * number of events is allowed to coexist. 243 */ 244 static struct efi_event efi_events[16]; 245 246 efi_status_t efi_create_event(uint32_t type, UINTN notify_tpl, 247 void (EFIAPI *notify_function) ( 248 struct efi_event *event, 249 void *context), 250 void *notify_context, struct efi_event **event) 251 { 252 int i; 253 254 if (event == NULL) 255 return EFI_INVALID_PARAMETER; 256 257 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT)) 258 return EFI_INVALID_PARAMETER; 259 260 if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) && 261 notify_function == NULL) 262 return EFI_INVALID_PARAMETER; 263 264 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { 265 if (efi_events[i].type) 266 continue; 267 efi_events[i].type = type; 268 efi_events[i].notify_tpl = notify_tpl; 269 efi_events[i].notify_function = notify_function; 270 efi_events[i].notify_context = notify_context; 271 /* Disable timers on bootup */ 272 efi_events[i].trigger_next = -1ULL; 273 efi_events[i].signaled = 0; 274 *event = &efi_events[i]; 275 return EFI_SUCCESS; 276 } 277 return EFI_OUT_OF_RESOURCES; 278 } 279 280 static efi_status_t EFIAPI efi_create_event_ext( 281 uint32_t type, UINTN notify_tpl, 282 void (EFIAPI *notify_function) ( 283 struct efi_event *event, 284 void *context), 285 void *notify_context, struct efi_event **event) 286 { 287 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function, 288 notify_context); 289 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function, 290 notify_context, event)); 291 } 292 293 294 /* 295 * Our timers have to work without interrupts, so we check whenever keyboard 296 * input or disk accesses happen if enough time elapsed for it to fire. 297 */ 298 void efi_timer_check(void) 299 { 300 int i; 301 u64 now = timer_get_us(); 302 303 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { 304 if (!efi_events[i].type || 305 !(efi_events[i].type & EVT_TIMER) || 306 efi_events[i].trigger_type == EFI_TIMER_STOP || 307 now < efi_events[i].trigger_next) 308 continue; 309 if (efi_events[i].trigger_type == EFI_TIMER_PERIODIC) { 310 efi_events[i].trigger_next += 311 efi_events[i].trigger_time; 312 efi_events[i].signaled = 0; 313 } 314 efi_signal_event(&efi_events[i]); 315 } 316 WATCHDOG_RESET(); 317 } 318 319 efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type, 320 uint64_t trigger_time) 321 { 322 int i; 323 324 /* 325 * The parameter defines a multiple of 100ns. 326 * We use multiples of 1000ns. So divide by 10. 327 */ 328 trigger_time = efi_div10(trigger_time); 329 330 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { 331 if (event != &efi_events[i]) 332 continue; 333 334 if (!(event->type & EVT_TIMER)) 335 break; 336 switch (type) { 337 case EFI_TIMER_STOP: 338 event->trigger_next = -1ULL; 339 break; 340 case EFI_TIMER_PERIODIC: 341 case EFI_TIMER_RELATIVE: 342 event->trigger_next = 343 timer_get_us() + trigger_time; 344 break; 345 default: 346 return EFI_INVALID_PARAMETER; 347 } 348 event->trigger_type = type; 349 event->trigger_time = trigger_time; 350 return EFI_SUCCESS; 351 } 352 return EFI_INVALID_PARAMETER; 353 } 354 355 static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event, 356 enum efi_timer_delay type, 357 uint64_t trigger_time) 358 { 359 EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time); 360 return EFI_EXIT(efi_set_timer(event, type, trigger_time)); 361 } 362 363 static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events, 364 struct efi_event **event, 365 unsigned long *index) 366 { 367 int i, j; 368 369 EFI_ENTRY("%ld, %p, %p", num_events, event, index); 370 371 /* Check parameters */ 372 if (!num_events || !event) 373 return EFI_EXIT(EFI_INVALID_PARAMETER); 374 for (i = 0; i < num_events; ++i) { 375 for (j = 0; j < ARRAY_SIZE(efi_events); ++j) { 376 if (event[i] == &efi_events[j]) 377 goto known_event; 378 } 379 return EFI_EXIT(EFI_INVALID_PARAMETER); 380 known_event: 381 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL) 382 return EFI_EXIT(EFI_INVALID_PARAMETER); 383 } 384 385 /* Wait for signal */ 386 for (;;) { 387 for (i = 0; i < num_events; ++i) { 388 if (event[i]->signaled) 389 goto out; 390 } 391 /* Allow events to occur. */ 392 efi_timer_check(); 393 } 394 395 out: 396 /* 397 * Reset the signal which is passed to the caller to allow periodic 398 * events to occur. 399 */ 400 event[i]->signaled = 0; 401 if (index) 402 *index = i; 403 404 return EFI_EXIT(EFI_SUCCESS); 405 } 406 407 static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event) 408 { 409 int i; 410 411 EFI_ENTRY("%p", event); 412 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { 413 if (event != &efi_events[i]) 414 continue; 415 efi_signal_event(event); 416 break; 417 } 418 return EFI_EXIT(EFI_SUCCESS); 419 } 420 421 static efi_status_t EFIAPI efi_close_event(struct efi_event *event) 422 { 423 int i; 424 425 EFI_ENTRY("%p", event); 426 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { 427 if (event == &efi_events[i]) { 428 event->type = 0; 429 event->trigger_next = -1ULL; 430 event->signaled = 0; 431 return EFI_EXIT(EFI_SUCCESS); 432 } 433 } 434 return EFI_EXIT(EFI_INVALID_PARAMETER); 435 } 436 437 static efi_status_t EFIAPI efi_check_event(struct efi_event *event) 438 { 439 int i; 440 441 EFI_ENTRY("%p", event); 442 efi_timer_check(); 443 for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { 444 if (event != &efi_events[i]) 445 continue; 446 if (!event->type || event->type & EVT_NOTIFY_SIGNAL) 447 break; 448 if (event->signaled) 449 return EFI_EXIT(EFI_SUCCESS); 450 return EFI_EXIT(EFI_NOT_READY); 451 } 452 return EFI_EXIT(EFI_INVALID_PARAMETER); 453 } 454 455 static efi_status_t EFIAPI efi_install_protocol_interface(void **handle, 456 efi_guid_t *protocol, int protocol_interface_type, 457 void *protocol_interface) 458 { 459 struct list_head *lhandle; 460 int i; 461 efi_status_t r; 462 463 if (!handle || !protocol || 464 protocol_interface_type != EFI_NATIVE_INTERFACE) { 465 r = EFI_INVALID_PARAMETER; 466 goto out; 467 } 468 469 /* Create new handle if requested. */ 470 if (!*handle) { 471 r = EFI_OUT_OF_RESOURCES; 472 goto out; 473 } 474 /* Find object. */ 475 list_for_each(lhandle, &efi_obj_list) { 476 struct efi_object *efiobj; 477 efiobj = list_entry(lhandle, struct efi_object, link); 478 479 if (efiobj->handle != *handle) 480 continue; 481 /* Check if protocol is already installed on the handle. */ 482 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { 483 struct efi_handler *handler = &efiobj->protocols[i]; 484 485 if (!handler->guid) 486 continue; 487 if (!guidcmp(handler->guid, protocol)) { 488 r = EFI_INVALID_PARAMETER; 489 goto out; 490 } 491 } 492 /* Install protocol in first empty slot. */ 493 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { 494 struct efi_handler *handler = &efiobj->protocols[i]; 495 496 if (handler->guid) 497 continue; 498 499 handler->guid = protocol; 500 handler->protocol_interface = protocol_interface; 501 r = EFI_SUCCESS; 502 goto out; 503 } 504 r = EFI_OUT_OF_RESOURCES; 505 goto out; 506 } 507 r = EFI_INVALID_PARAMETER; 508 out: 509 return r; 510 } 511 512 static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle, 513 efi_guid_t *protocol, int protocol_interface_type, 514 void *protocol_interface) 515 { 516 EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type, 517 protocol_interface); 518 519 return EFI_EXIT(efi_install_protocol_interface(handle, protocol, 520 protocol_interface_type, 521 protocol_interface)); 522 } 523 524 static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle, 525 efi_guid_t *protocol, void *old_interface, 526 void *new_interface) 527 { 528 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface, 529 new_interface); 530 return EFI_EXIT(EFI_ACCESS_DENIED); 531 } 532 533 static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle, 534 efi_guid_t *protocol, void *protocol_interface) 535 { 536 struct list_head *lhandle; 537 int i; 538 efi_status_t r = EFI_NOT_FOUND; 539 540 if (!handle || !protocol) { 541 r = EFI_INVALID_PARAMETER; 542 goto out; 543 } 544 545 list_for_each(lhandle, &efi_obj_list) { 546 struct efi_object *efiobj; 547 efiobj = list_entry(lhandle, struct efi_object, link); 548 549 if (efiobj->handle != handle) 550 continue; 551 552 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { 553 struct efi_handler *handler = &efiobj->protocols[i]; 554 const efi_guid_t *hprotocol = handler->guid; 555 556 if (!hprotocol) 557 continue; 558 if (!guidcmp(hprotocol, protocol)) { 559 if (handler->protocol_interface) { 560 r = EFI_ACCESS_DENIED; 561 } else { 562 handler->guid = 0; 563 r = EFI_SUCCESS; 564 } 565 goto out; 566 } 567 } 568 } 569 570 out: 571 return r; 572 } 573 574 static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle, 575 efi_guid_t *protocol, void *protocol_interface) 576 { 577 EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface); 578 579 return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol, 580 protocol_interface)); 581 } 582 583 static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol, 584 struct efi_event *event, 585 void **registration) 586 { 587 EFI_ENTRY("%p, %p, %p", protocol, event, registration); 588 return EFI_EXIT(EFI_OUT_OF_RESOURCES); 589 } 590 591 static int efi_search(enum efi_locate_search_type search_type, 592 efi_guid_t *protocol, void *search_key, 593 struct efi_object *efiobj) 594 { 595 int i; 596 597 switch (search_type) { 598 case all_handles: 599 return 0; 600 case by_register_notify: 601 return -1; 602 case by_protocol: 603 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { 604 const efi_guid_t *guid = efiobj->protocols[i].guid; 605 if (guid && !guidcmp(guid, protocol)) 606 return 0; 607 } 608 return -1; 609 } 610 611 return -1; 612 } 613 614 static efi_status_t efi_locate_handle( 615 enum efi_locate_search_type search_type, 616 efi_guid_t *protocol, void *search_key, 617 unsigned long *buffer_size, efi_handle_t *buffer) 618 { 619 struct list_head *lhandle; 620 unsigned long size = 0; 621 622 /* Count how much space we need */ 623 list_for_each(lhandle, &efi_obj_list) { 624 struct efi_object *efiobj; 625 efiobj = list_entry(lhandle, struct efi_object, link); 626 if (!efi_search(search_type, protocol, search_key, efiobj)) { 627 size += sizeof(void*); 628 } 629 } 630 631 if (*buffer_size < size) { 632 *buffer_size = size; 633 return EFI_BUFFER_TOO_SMALL; 634 } 635 636 *buffer_size = size; 637 if (size == 0) 638 return EFI_NOT_FOUND; 639 640 /* Then fill the array */ 641 list_for_each(lhandle, &efi_obj_list) { 642 struct efi_object *efiobj; 643 efiobj = list_entry(lhandle, struct efi_object, link); 644 if (!efi_search(search_type, protocol, search_key, efiobj)) { 645 *(buffer++) = efiobj->handle; 646 } 647 } 648 649 return EFI_SUCCESS; 650 } 651 652 static efi_status_t EFIAPI efi_locate_handle_ext( 653 enum efi_locate_search_type search_type, 654 efi_guid_t *protocol, void *search_key, 655 unsigned long *buffer_size, efi_handle_t *buffer) 656 { 657 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key, 658 buffer_size, buffer); 659 660 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key, 661 buffer_size, buffer)); 662 } 663 664 static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol, 665 struct efi_device_path **device_path, 666 efi_handle_t *device) 667 { 668 struct efi_object *efiobj; 669 670 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device); 671 672 efiobj = efi_dp_find_obj(*device_path, device_path); 673 if (!efiobj) 674 return EFI_EXIT(EFI_NOT_FOUND); 675 676 *device = efiobj->handle; 677 678 return EFI_EXIT(EFI_SUCCESS); 679 } 680 681 /* Collapses configuration table entries, removing index i */ 682 static void efi_remove_configuration_table(int i) 683 { 684 struct efi_configuration_table *this = &efi_conf_table[i]; 685 struct efi_configuration_table *next = &efi_conf_table[i+1]; 686 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables]; 687 688 memmove(this, next, (ulong)end - (ulong)next); 689 systab.nr_tables--; 690 } 691 692 efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table) 693 { 694 int i; 695 696 /* Check for guid override */ 697 for (i = 0; i < systab.nr_tables; i++) { 698 if (!guidcmp(guid, &efi_conf_table[i].guid)) { 699 if (table) 700 efi_conf_table[i].table = table; 701 else 702 efi_remove_configuration_table(i); 703 return EFI_SUCCESS; 704 } 705 } 706 707 if (!table) 708 return EFI_NOT_FOUND; 709 710 /* No override, check for overflow */ 711 if (i >= ARRAY_SIZE(efi_conf_table)) 712 return EFI_OUT_OF_RESOURCES; 713 714 /* Add a new entry */ 715 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid)); 716 efi_conf_table[i].table = table; 717 systab.nr_tables = i + 1; 718 719 return EFI_SUCCESS; 720 } 721 722 static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid, 723 void *table) 724 { 725 EFI_ENTRY("%p, %p", guid, table); 726 return EFI_EXIT(efi_install_configuration_table(guid, table)); 727 } 728 729 /* Initialize a loaded_image_info + loaded_image_info object with correct 730 * protocols, boot-device, etc. 731 */ 732 void efi_setup_loaded_image(struct efi_loaded_image *info, struct efi_object *obj, 733 struct efi_device_path *device_path, 734 struct efi_device_path *file_path) 735 { 736 obj->handle = info; 737 738 /* 739 * When asking for the device path interface, return 740 * bootefi_device_path 741 */ 742 obj->protocols[0].guid = &efi_guid_device_path; 743 obj->protocols[0].protocol_interface = device_path; 744 745 /* 746 * When asking for the loaded_image interface, just 747 * return handle which points to loaded_image_info 748 */ 749 obj->protocols[1].guid = &efi_guid_loaded_image; 750 obj->protocols[1].protocol_interface = info; 751 752 obj->protocols[2].guid = &efi_guid_console_control; 753 obj->protocols[2].protocol_interface = (void *)&efi_console_control; 754 755 obj->protocols[3].guid = &efi_guid_device_path_to_text_protocol; 756 obj->protocols[3].protocol_interface = 757 (void *)&efi_device_path_to_text; 758 759 info->file_path = file_path; 760 info->device_handle = efi_dp_find_obj(device_path, NULL); 761 762 list_add_tail(&obj->link, &efi_obj_list); 763 } 764 765 static efi_status_t EFIAPI efi_load_image(bool boot_policy, 766 efi_handle_t parent_image, 767 struct efi_device_path *file_path, 768 void *source_buffer, 769 unsigned long source_size, 770 efi_handle_t *image_handle) 771 { 772 static struct efi_object loaded_image_info_obj = { 773 .protocols = { 774 { 775 .guid = &efi_guid_loaded_image, 776 }, 777 }, 778 }; 779 struct efi_loaded_image *info; 780 struct efi_object *obj; 781 782 EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image, 783 file_path, source_buffer, source_size, image_handle); 784 info = malloc(sizeof(*info)); 785 loaded_image_info_obj.protocols[0].protocol_interface = info; 786 obj = malloc(sizeof(loaded_image_info_obj)); 787 memset(info, 0, sizeof(*info)); 788 memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj)); 789 obj->handle = info; 790 info->file_path = file_path; 791 info->reserved = efi_load_pe(source_buffer, info); 792 if (!info->reserved) { 793 free(info); 794 free(obj); 795 return EFI_EXIT(EFI_UNSUPPORTED); 796 } 797 798 *image_handle = info; 799 list_add_tail(&obj->link, &efi_obj_list); 800 801 return EFI_EXIT(EFI_SUCCESS); 802 } 803 804 static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle, 805 unsigned long *exit_data_size, 806 s16 **exit_data) 807 { 808 ulong (*entry)(void *image_handle, struct efi_system_table *st); 809 struct efi_loaded_image *info = image_handle; 810 811 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data); 812 entry = info->reserved; 813 814 efi_is_direct_boot = false; 815 816 /* call the image! */ 817 if (setjmp(&info->exit_jmp)) { 818 /* We returned from the child image */ 819 return EFI_EXIT(info->exit_status); 820 } 821 822 __efi_nesting_dec(); 823 __efi_exit_check(); 824 entry(image_handle, &systab); 825 __efi_entry_check(); 826 __efi_nesting_inc(); 827 828 /* Should usually never get here */ 829 return EFI_EXIT(EFI_SUCCESS); 830 } 831 832 static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle, 833 efi_status_t exit_status, unsigned long exit_data_size, 834 int16_t *exit_data) 835 { 836 struct efi_loaded_image *loaded_image_info = (void*)image_handle; 837 838 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status, 839 exit_data_size, exit_data); 840 841 loaded_image_info->exit_status = exit_status; 842 longjmp(&loaded_image_info->exit_jmp, 1); 843 844 panic("EFI application exited"); 845 } 846 847 static struct efi_object *efi_search_obj(void *handle) 848 { 849 struct list_head *lhandle; 850 851 list_for_each(lhandle, &efi_obj_list) { 852 struct efi_object *efiobj; 853 efiobj = list_entry(lhandle, struct efi_object, link); 854 if (efiobj->handle == handle) 855 return efiobj; 856 } 857 858 return NULL; 859 } 860 861 static efi_status_t EFIAPI efi_unload_image(void *image_handle) 862 { 863 struct efi_object *efiobj; 864 865 EFI_ENTRY("%p", image_handle); 866 efiobj = efi_search_obj(image_handle); 867 if (efiobj) 868 list_del(&efiobj->link); 869 870 return EFI_EXIT(EFI_SUCCESS); 871 } 872 873 static void efi_exit_caches(void) 874 { 875 #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64) 876 /* 877 * Grub on 32bit ARM needs to have caches disabled before jumping into 878 * a zImage, but does not know of all cache layers. Give it a hand. 879 */ 880 if (efi_is_direct_boot) 881 cleanup_before_linux(); 882 #endif 883 } 884 885 static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle, 886 unsigned long map_key) 887 { 888 EFI_ENTRY("%p, %ld", image_handle, map_key); 889 890 board_quiesce_devices(); 891 892 /* Fix up caches for EFI payloads if necessary */ 893 efi_exit_caches(); 894 895 /* This stops all lingering devices */ 896 bootm_disable_interrupts(); 897 898 /* Give the payload some time to boot */ 899 WATCHDOG_RESET(); 900 901 return EFI_EXIT(EFI_SUCCESS); 902 } 903 904 static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count) 905 { 906 static uint64_t mono = 0; 907 EFI_ENTRY("%p", count); 908 *count = mono++; 909 return EFI_EXIT(EFI_SUCCESS); 910 } 911 912 static efi_status_t EFIAPI efi_stall(unsigned long microseconds) 913 { 914 EFI_ENTRY("%ld", microseconds); 915 udelay(microseconds); 916 return EFI_EXIT(EFI_SUCCESS); 917 } 918 919 static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout, 920 uint64_t watchdog_code, 921 unsigned long data_size, 922 uint16_t *watchdog_data) 923 { 924 EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code, 925 data_size, watchdog_data); 926 return efi_unsupported(__func__); 927 } 928 929 static efi_status_t EFIAPI efi_connect_controller( 930 efi_handle_t controller_handle, 931 efi_handle_t *driver_image_handle, 932 struct efi_device_path *remain_device_path, 933 bool recursive) 934 { 935 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle, 936 remain_device_path, recursive); 937 return EFI_EXIT(EFI_NOT_FOUND); 938 } 939 940 static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle, 941 void *driver_image_handle, 942 void *child_handle) 943 { 944 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle, 945 child_handle); 946 return EFI_EXIT(EFI_INVALID_PARAMETER); 947 } 948 949 static efi_status_t EFIAPI efi_close_protocol(void *handle, 950 efi_guid_t *protocol, 951 void *agent_handle, 952 void *controller_handle) 953 { 954 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle, 955 controller_handle); 956 return EFI_EXIT(EFI_NOT_FOUND); 957 } 958 959 static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle, 960 efi_guid_t *protocol, 961 struct efi_open_protocol_info_entry **entry_buffer, 962 unsigned long *entry_count) 963 { 964 EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer, 965 entry_count); 966 return EFI_EXIT(EFI_NOT_FOUND); 967 } 968 969 static efi_status_t EFIAPI efi_protocols_per_handle(void *handle, 970 efi_guid_t ***protocol_buffer, 971 unsigned long *protocol_buffer_count) 972 { 973 unsigned long buffer_size; 974 struct efi_object *efiobj; 975 unsigned long i, j; 976 struct list_head *lhandle; 977 efi_status_t r; 978 979 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer, 980 protocol_buffer_count); 981 982 if (!handle || !protocol_buffer || !protocol_buffer_count) 983 return EFI_EXIT(EFI_INVALID_PARAMETER); 984 985 *protocol_buffer = NULL; 986 *protocol_buffer_count = 0; 987 list_for_each(lhandle, &efi_obj_list) { 988 efiobj = list_entry(lhandle, struct efi_object, link); 989 990 if (efiobj->handle != handle) 991 continue; 992 993 /* Count protocols */ 994 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { 995 if (efiobj->protocols[i].guid) 996 ++*protocol_buffer_count; 997 } 998 /* Copy guids */ 999 if (*protocol_buffer_count) { 1000 buffer_size = sizeof(efi_guid_t *) * 1001 *protocol_buffer_count; 1002 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, 1003 buffer_size, 1004 (void **)protocol_buffer); 1005 if (r != EFI_SUCCESS) 1006 return EFI_EXIT(r); 1007 j = 0; 1008 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); ++i) { 1009 if (efiobj->protocols[i].guid) { 1010 (*protocol_buffer)[j] = (void *) 1011 efiobj->protocols[i].guid; 1012 ++j; 1013 } 1014 } 1015 } 1016 break; 1017 } 1018 1019 return EFI_EXIT(EFI_SUCCESS); 1020 } 1021 1022 static efi_status_t EFIAPI efi_locate_handle_buffer( 1023 enum efi_locate_search_type search_type, 1024 efi_guid_t *protocol, void *search_key, 1025 unsigned long *no_handles, efi_handle_t **buffer) 1026 { 1027 efi_status_t r; 1028 unsigned long buffer_size = 0; 1029 1030 EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key, 1031 no_handles, buffer); 1032 1033 if (!no_handles || !buffer) { 1034 r = EFI_INVALID_PARAMETER; 1035 goto out; 1036 } 1037 *no_handles = 0; 1038 *buffer = NULL; 1039 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, 1040 *buffer); 1041 if (r != EFI_BUFFER_TOO_SMALL) 1042 goto out; 1043 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size, 1044 (void **)buffer); 1045 if (r != EFI_SUCCESS) 1046 goto out; 1047 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, 1048 *buffer); 1049 if (r == EFI_SUCCESS) 1050 *no_handles = buffer_size / sizeof(void *); 1051 out: 1052 return EFI_EXIT(r); 1053 } 1054 1055 static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol, 1056 void *registration, 1057 void **protocol_interface) 1058 { 1059 struct list_head *lhandle; 1060 int i; 1061 1062 EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface); 1063 1064 if (!protocol || !protocol_interface) 1065 return EFI_EXIT(EFI_INVALID_PARAMETER); 1066 1067 list_for_each(lhandle, &efi_obj_list) { 1068 struct efi_object *efiobj; 1069 1070 efiobj = list_entry(lhandle, struct efi_object, link); 1071 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { 1072 struct efi_handler *handler = &efiobj->protocols[i]; 1073 1074 if (!handler->guid) 1075 continue; 1076 if (!guidcmp(handler->guid, protocol)) { 1077 *protocol_interface = 1078 handler->protocol_interface; 1079 return EFI_EXIT(EFI_SUCCESS); 1080 } 1081 } 1082 } 1083 *protocol_interface = NULL; 1084 1085 return EFI_EXIT(EFI_NOT_FOUND); 1086 } 1087 1088 static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces( 1089 void **handle, ...) 1090 { 1091 EFI_ENTRY("%p", handle); 1092 1093 va_list argptr; 1094 efi_guid_t *protocol; 1095 void *protocol_interface; 1096 efi_status_t r = EFI_SUCCESS; 1097 int i = 0; 1098 1099 if (!handle) 1100 return EFI_EXIT(EFI_INVALID_PARAMETER); 1101 1102 va_start(argptr, handle); 1103 for (;;) { 1104 protocol = va_arg(argptr, efi_guid_t*); 1105 if (!protocol) 1106 break; 1107 protocol_interface = va_arg(argptr, void*); 1108 r = efi_install_protocol_interface(handle, protocol, 1109 EFI_NATIVE_INTERFACE, 1110 protocol_interface); 1111 if (r != EFI_SUCCESS) 1112 break; 1113 i++; 1114 } 1115 va_end(argptr); 1116 if (r == EFI_SUCCESS) 1117 return EFI_EXIT(r); 1118 1119 /* If an error occured undo all changes. */ 1120 va_start(argptr, handle); 1121 for (; i; --i) { 1122 protocol = va_arg(argptr, efi_guid_t*); 1123 protocol_interface = va_arg(argptr, void*); 1124 efi_uninstall_protocol_interface(handle, protocol, 1125 protocol_interface); 1126 } 1127 va_end(argptr); 1128 1129 return EFI_EXIT(r); 1130 } 1131 1132 static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces( 1133 void *handle, ...) 1134 { 1135 EFI_ENTRY("%p", handle); 1136 return EFI_EXIT(EFI_INVALID_PARAMETER); 1137 } 1138 1139 static efi_status_t EFIAPI efi_calculate_crc32(void *data, 1140 unsigned long data_size, 1141 uint32_t *crc32_p) 1142 { 1143 EFI_ENTRY("%p, %ld", data, data_size); 1144 *crc32_p = crc32(0, data, data_size); 1145 return EFI_EXIT(EFI_SUCCESS); 1146 } 1147 1148 static void EFIAPI efi_copy_mem(void *destination, void *source, 1149 unsigned long length) 1150 { 1151 EFI_ENTRY("%p, %p, %ld", destination, source, length); 1152 memcpy(destination, source, length); 1153 } 1154 1155 static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value) 1156 { 1157 EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value); 1158 memset(buffer, value, size); 1159 } 1160 1161 static efi_status_t EFIAPI efi_open_protocol( 1162 void *handle, efi_guid_t *protocol, 1163 void **protocol_interface, void *agent_handle, 1164 void *controller_handle, uint32_t attributes) 1165 { 1166 struct list_head *lhandle; 1167 int i; 1168 efi_status_t r = EFI_INVALID_PARAMETER; 1169 1170 EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol, 1171 protocol_interface, agent_handle, controller_handle, 1172 attributes); 1173 1174 if (!handle || !protocol || 1175 (!protocol_interface && attributes != 1176 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) { 1177 goto out; 1178 } 1179 1180 switch (attributes) { 1181 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL: 1182 case EFI_OPEN_PROTOCOL_GET_PROTOCOL: 1183 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL: 1184 break; 1185 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER: 1186 if (controller_handle == handle) 1187 goto out; 1188 case EFI_OPEN_PROTOCOL_BY_DRIVER: 1189 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE: 1190 if (controller_handle == NULL) 1191 goto out; 1192 case EFI_OPEN_PROTOCOL_EXCLUSIVE: 1193 if (agent_handle == NULL) 1194 goto out; 1195 break; 1196 default: 1197 goto out; 1198 } 1199 1200 list_for_each(lhandle, &efi_obj_list) { 1201 struct efi_object *efiobj; 1202 efiobj = list_entry(lhandle, struct efi_object, link); 1203 1204 if (efiobj->handle != handle) 1205 continue; 1206 1207 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { 1208 struct efi_handler *handler = &efiobj->protocols[i]; 1209 const efi_guid_t *hprotocol = handler->guid; 1210 if (!hprotocol) 1211 continue; 1212 if (!guidcmp(hprotocol, protocol)) { 1213 if (attributes != 1214 EFI_OPEN_PROTOCOL_TEST_PROTOCOL) { 1215 *protocol_interface = 1216 handler->protocol_interface; 1217 } 1218 r = EFI_SUCCESS; 1219 goto out; 1220 } 1221 } 1222 goto unsupported; 1223 } 1224 1225 unsupported: 1226 r = EFI_UNSUPPORTED; 1227 out: 1228 return EFI_EXIT(r); 1229 } 1230 1231 static efi_status_t EFIAPI efi_handle_protocol(void *handle, 1232 efi_guid_t *protocol, 1233 void **protocol_interface) 1234 { 1235 return efi_open_protocol(handle, protocol, protocol_interface, NULL, 1236 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL); 1237 } 1238 1239 static const struct efi_boot_services efi_boot_services = { 1240 .hdr = { 1241 .headersize = sizeof(struct efi_table_hdr), 1242 }, 1243 .raise_tpl = efi_raise_tpl, 1244 .restore_tpl = efi_restore_tpl, 1245 .allocate_pages = efi_allocate_pages_ext, 1246 .free_pages = efi_free_pages_ext, 1247 .get_memory_map = efi_get_memory_map_ext, 1248 .allocate_pool = efi_allocate_pool_ext, 1249 .free_pool = efi_free_pool_ext, 1250 .create_event = efi_create_event_ext, 1251 .set_timer = efi_set_timer_ext, 1252 .wait_for_event = efi_wait_for_event, 1253 .signal_event = efi_signal_event_ext, 1254 .close_event = efi_close_event, 1255 .check_event = efi_check_event, 1256 .install_protocol_interface = efi_install_protocol_interface_ext, 1257 .reinstall_protocol_interface = efi_reinstall_protocol_interface, 1258 .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext, 1259 .handle_protocol = efi_handle_protocol, 1260 .reserved = NULL, 1261 .register_protocol_notify = efi_register_protocol_notify, 1262 .locate_handle = efi_locate_handle_ext, 1263 .locate_device_path = efi_locate_device_path, 1264 .install_configuration_table = efi_install_configuration_table_ext, 1265 .load_image = efi_load_image, 1266 .start_image = efi_start_image, 1267 .exit = efi_exit, 1268 .unload_image = efi_unload_image, 1269 .exit_boot_services = efi_exit_boot_services, 1270 .get_next_monotonic_count = efi_get_next_monotonic_count, 1271 .stall = efi_stall, 1272 .set_watchdog_timer = efi_set_watchdog_timer, 1273 .connect_controller = efi_connect_controller, 1274 .disconnect_controller = efi_disconnect_controller, 1275 .open_protocol = efi_open_protocol, 1276 .close_protocol = efi_close_protocol, 1277 .open_protocol_information = efi_open_protocol_information, 1278 .protocols_per_handle = efi_protocols_per_handle, 1279 .locate_handle_buffer = efi_locate_handle_buffer, 1280 .locate_protocol = efi_locate_protocol, 1281 .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces, 1282 .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces, 1283 .calculate_crc32 = efi_calculate_crc32, 1284 .copy_mem = efi_copy_mem, 1285 .set_mem = efi_set_mem, 1286 }; 1287 1288 1289 static uint16_t __efi_runtime_data firmware_vendor[] = 1290 { 'D','a','s',' ','U','-','b','o','o','t',0 }; 1291 1292 struct efi_system_table __efi_runtime_data systab = { 1293 .hdr = { 1294 .signature = EFI_SYSTEM_TABLE_SIGNATURE, 1295 .revision = 0x20005, /* 2.5 */ 1296 .headersize = sizeof(struct efi_table_hdr), 1297 }, 1298 .fw_vendor = (long)firmware_vendor, 1299 .con_in = (void*)&efi_con_in, 1300 .con_out = (void*)&efi_con_out, 1301 .std_err = (void*)&efi_con_out, 1302 .runtime = (void*)&efi_runtime_services, 1303 .boottime = (void*)&efi_boot_services, 1304 .nr_tables = 0, 1305 .tables = (void*)efi_conf_table, 1306 }; 1307