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