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