1 /* SPDX-License-Identifier: GPL-2.0 */ 2 3 #ifndef _DRIVERS_FIRMWARE_EFI_EFISTUB_H 4 #define _DRIVERS_FIRMWARE_EFI_EFISTUB_H 5 6 /* error code which can't be mistaken for valid address */ 7 #define EFI_ERROR (~0UL) 8 9 /* 10 * __init annotations should not be used in the EFI stub, since the code is 11 * either included in the decompressor (x86, ARM) where they have no effect, 12 * or the whole stub is __init annotated at the section level (arm64), by 13 * renaming the sections, in which case the __init annotation will be 14 * redundant, and will result in section names like .init.init.text, and our 15 * linker script does not expect that. 16 */ 17 #undef __init 18 19 /* 20 * Allow the platform to override the allocation granularity: this allows 21 * systems that have the capability to run with a larger page size to deal 22 * with the allocations for initrd and fdt more efficiently. 23 */ 24 #ifndef EFI_ALLOC_ALIGN 25 #define EFI_ALLOC_ALIGN EFI_PAGE_SIZE 26 #endif 27 28 #if defined(CONFIG_ARM) || defined(CONFIG_X86) 29 #define __efistub_global __section(.data) 30 #else 31 #define __efistub_global 32 #endif 33 34 extern bool __pure nochunk(void); 35 extern bool __pure nokaslr(void); 36 extern bool __pure noinitrd(void); 37 extern bool __pure is_quiet(void); 38 extern bool __pure novamap(void); 39 40 extern __pure efi_system_table_t *efi_system_table(void); 41 42 #define pr_efi(msg) do { \ 43 if (!is_quiet()) efi_printk("EFI stub: "msg); \ 44 } while (0) 45 46 #define pr_efi_err(msg) efi_printk("EFI stub: ERROR: "msg) 47 48 /* Helper macros for the usual case of using simple C variables: */ 49 #ifndef fdt_setprop_inplace_var 50 #define fdt_setprop_inplace_var(fdt, node_offset, name, var) \ 51 fdt_setprop_inplace((fdt), (node_offset), (name), &(var), sizeof(var)) 52 #endif 53 54 #ifndef fdt_setprop_var 55 #define fdt_setprop_var(fdt, node_offset, name, var) \ 56 fdt_setprop((fdt), (node_offset), (name), &(var), sizeof(var)) 57 #endif 58 59 #define get_efi_var(name, vendor, ...) \ 60 efi_rt_call(get_variable, (efi_char16_t *)(name), \ 61 (efi_guid_t *)(vendor), __VA_ARGS__) 62 63 #define set_efi_var(name, vendor, ...) \ 64 efi_rt_call(set_variable, (efi_char16_t *)(name), \ 65 (efi_guid_t *)(vendor), __VA_ARGS__) 66 67 #define efi_get_handle_at(array, idx) \ 68 (efi_is_native() ? (array)[idx] \ 69 : (efi_handle_t)(unsigned long)((u32 *)(array))[idx]) 70 71 #define efi_get_handle_num(size) \ 72 ((size) / (efi_is_native() ? sizeof(efi_handle_t) : sizeof(u32))) 73 74 #define for_each_efi_handle(handle, array, size, i) \ 75 for (i = 0; \ 76 i < efi_get_handle_num(size) && \ 77 ((handle = efi_get_handle_at((array), i)) || true); \ 78 i++) 79 80 /* 81 * Allocation types for calls to boottime->allocate_pages. 82 */ 83 #define EFI_ALLOCATE_ANY_PAGES 0 84 #define EFI_ALLOCATE_MAX_ADDRESS 1 85 #define EFI_ALLOCATE_ADDRESS 2 86 #define EFI_MAX_ALLOCATE_TYPE 3 87 88 /* 89 * The type of search to perform when calling boottime->locate_handle 90 */ 91 #define EFI_LOCATE_ALL_HANDLES 0 92 #define EFI_LOCATE_BY_REGISTER_NOTIFY 1 93 #define EFI_LOCATE_BY_PROTOCOL 2 94 95 struct efi_boot_memmap { 96 efi_memory_desc_t **map; 97 unsigned long *map_size; 98 unsigned long *desc_size; 99 u32 *desc_ver; 100 unsigned long *key_ptr; 101 unsigned long *buff_size; 102 }; 103 104 typedef struct efi_generic_dev_path efi_device_path_protocol_t; 105 106 /* 107 * EFI Boot Services table 108 */ 109 union efi_boot_services { 110 struct { 111 efi_table_hdr_t hdr; 112 void *raise_tpl; 113 void *restore_tpl; 114 efi_status_t (__efiapi *allocate_pages)(int, int, unsigned long, 115 efi_physical_addr_t *); 116 efi_status_t (__efiapi *free_pages)(efi_physical_addr_t, 117 unsigned long); 118 efi_status_t (__efiapi *get_memory_map)(unsigned long *, void *, 119 unsigned long *, 120 unsigned long *, u32 *); 121 efi_status_t (__efiapi *allocate_pool)(int, unsigned long, 122 void **); 123 efi_status_t (__efiapi *free_pool)(void *); 124 void *create_event; 125 void *set_timer; 126 void *wait_for_event; 127 void *signal_event; 128 void *close_event; 129 void *check_event; 130 void *install_protocol_interface; 131 void *reinstall_protocol_interface; 132 void *uninstall_protocol_interface; 133 efi_status_t (__efiapi *handle_protocol)(efi_handle_t, 134 efi_guid_t *, void **); 135 void *__reserved; 136 void *register_protocol_notify; 137 efi_status_t (__efiapi *locate_handle)(int, efi_guid_t *, 138 void *, unsigned long *, 139 efi_handle_t *); 140 efi_status_t (__efiapi *locate_device_path)(efi_guid_t *, 141 efi_device_path_protocol_t **, 142 efi_handle_t *); 143 efi_status_t (__efiapi *install_configuration_table)(efi_guid_t *, 144 void *); 145 void *load_image; 146 void *start_image; 147 efi_status_t __noreturn (__efiapi *exit)(efi_handle_t, 148 efi_status_t, 149 unsigned long, 150 efi_char16_t *); 151 void *unload_image; 152 efi_status_t (__efiapi *exit_boot_services)(efi_handle_t, 153 unsigned long); 154 void *get_next_monotonic_count; 155 void *stall; 156 void *set_watchdog_timer; 157 void *connect_controller; 158 efi_status_t (__efiapi *disconnect_controller)(efi_handle_t, 159 efi_handle_t, 160 efi_handle_t); 161 void *open_protocol; 162 void *close_protocol; 163 void *open_protocol_information; 164 void *protocols_per_handle; 165 void *locate_handle_buffer; 166 efi_status_t (__efiapi *locate_protocol)(efi_guid_t *, void *, 167 void **); 168 void *install_multiple_protocol_interfaces; 169 void *uninstall_multiple_protocol_interfaces; 170 void *calculate_crc32; 171 void *copy_mem; 172 void *set_mem; 173 void *create_event_ex; 174 }; 175 struct { 176 efi_table_hdr_t hdr; 177 u32 raise_tpl; 178 u32 restore_tpl; 179 u32 allocate_pages; 180 u32 free_pages; 181 u32 get_memory_map; 182 u32 allocate_pool; 183 u32 free_pool; 184 u32 create_event; 185 u32 set_timer; 186 u32 wait_for_event; 187 u32 signal_event; 188 u32 close_event; 189 u32 check_event; 190 u32 install_protocol_interface; 191 u32 reinstall_protocol_interface; 192 u32 uninstall_protocol_interface; 193 u32 handle_protocol; 194 u32 __reserved; 195 u32 register_protocol_notify; 196 u32 locate_handle; 197 u32 locate_device_path; 198 u32 install_configuration_table; 199 u32 load_image; 200 u32 start_image; 201 u32 exit; 202 u32 unload_image; 203 u32 exit_boot_services; 204 u32 get_next_monotonic_count; 205 u32 stall; 206 u32 set_watchdog_timer; 207 u32 connect_controller; 208 u32 disconnect_controller; 209 u32 open_protocol; 210 u32 close_protocol; 211 u32 open_protocol_information; 212 u32 protocols_per_handle; 213 u32 locate_handle_buffer; 214 u32 locate_protocol; 215 u32 install_multiple_protocol_interfaces; 216 u32 uninstall_multiple_protocol_interfaces; 217 u32 calculate_crc32; 218 u32 copy_mem; 219 u32 set_mem; 220 u32 create_event_ex; 221 } mixed_mode; 222 }; 223 224 typedef union efi_uga_draw_protocol efi_uga_draw_protocol_t; 225 226 union efi_uga_draw_protocol { 227 struct { 228 efi_status_t (__efiapi *get_mode)(efi_uga_draw_protocol_t *, 229 u32*, u32*, u32*, u32*); 230 void *set_mode; 231 void *blt; 232 }; 233 struct { 234 u32 get_mode; 235 u32 set_mode; 236 u32 blt; 237 } mixed_mode; 238 }; 239 240 union efi_simple_text_output_protocol { 241 struct { 242 void *reset; 243 efi_status_t (__efiapi *output_string)(efi_simple_text_output_protocol_t *, 244 efi_char16_t *); 245 void *test_string; 246 }; 247 struct { 248 u32 reset; 249 u32 output_string; 250 u32 test_string; 251 } mixed_mode; 252 }; 253 254 #define PIXEL_RGB_RESERVED_8BIT_PER_COLOR 0 255 #define PIXEL_BGR_RESERVED_8BIT_PER_COLOR 1 256 #define PIXEL_BIT_MASK 2 257 #define PIXEL_BLT_ONLY 3 258 #define PIXEL_FORMAT_MAX 4 259 260 typedef struct { 261 u32 red_mask; 262 u32 green_mask; 263 u32 blue_mask; 264 u32 reserved_mask; 265 } efi_pixel_bitmask_t; 266 267 typedef struct { 268 u32 version; 269 u32 horizontal_resolution; 270 u32 vertical_resolution; 271 int pixel_format; 272 efi_pixel_bitmask_t pixel_information; 273 u32 pixels_per_scan_line; 274 } efi_graphics_output_mode_info_t; 275 276 typedef union efi_graphics_output_protocol_mode efi_graphics_output_protocol_mode_t; 277 278 union efi_graphics_output_protocol_mode { 279 struct { 280 u32 max_mode; 281 u32 mode; 282 efi_graphics_output_mode_info_t *info; 283 unsigned long size_of_info; 284 efi_physical_addr_t frame_buffer_base; 285 unsigned long frame_buffer_size; 286 }; 287 struct { 288 u32 max_mode; 289 u32 mode; 290 u32 info; 291 u32 size_of_info; 292 u64 frame_buffer_base; 293 u32 frame_buffer_size; 294 } mixed_mode; 295 }; 296 297 typedef union efi_graphics_output_protocol efi_graphics_output_protocol_t; 298 299 union efi_graphics_output_protocol { 300 struct { 301 void *query_mode; 302 void *set_mode; 303 void *blt; 304 efi_graphics_output_protocol_mode_t *mode; 305 }; 306 struct { 307 u32 query_mode; 308 u32 set_mode; 309 u32 blt; 310 u32 mode; 311 } mixed_mode; 312 }; 313 314 typedef union { 315 struct { 316 u32 revision; 317 efi_handle_t parent_handle; 318 efi_system_table_t *system_table; 319 efi_handle_t device_handle; 320 void *file_path; 321 void *reserved; 322 u32 load_options_size; 323 void *load_options; 324 void *image_base; 325 __aligned_u64 image_size; 326 unsigned int image_code_type; 327 unsigned int image_data_type; 328 efi_status_t (__efiapi *unload)(efi_handle_t image_handle); 329 }; 330 struct { 331 u32 revision; 332 u32 parent_handle; 333 u32 system_table; 334 u32 device_handle; 335 u32 file_path; 336 u32 reserved; 337 u32 load_options_size; 338 u32 load_options; 339 u32 image_base; 340 __aligned_u64 image_size; 341 u32 image_code_type; 342 u32 image_data_type; 343 u32 unload; 344 } mixed_mode; 345 } efi_loaded_image_t; 346 347 typedef struct { 348 u64 size; 349 u64 file_size; 350 u64 phys_size; 351 efi_time_t create_time; 352 efi_time_t last_access_time; 353 efi_time_t modification_time; 354 __aligned_u64 attribute; 355 efi_char16_t filename[]; 356 } efi_file_info_t; 357 358 typedef struct efi_file_protocol efi_file_protocol_t; 359 360 struct efi_file_protocol { 361 u64 revision; 362 efi_status_t (__efiapi *open) (efi_file_protocol_t *, 363 efi_file_protocol_t **, 364 efi_char16_t *, u64, u64); 365 efi_status_t (__efiapi *close) (efi_file_protocol_t *); 366 efi_status_t (__efiapi *delete) (efi_file_protocol_t *); 367 efi_status_t (__efiapi *read) (efi_file_protocol_t *, 368 unsigned long *, void *); 369 efi_status_t (__efiapi *write) (efi_file_protocol_t *, 370 unsigned long, void *); 371 efi_status_t (__efiapi *get_position)(efi_file_protocol_t *, u64 *); 372 efi_status_t (__efiapi *set_position)(efi_file_protocol_t *, u64); 373 efi_status_t (__efiapi *get_info) (efi_file_protocol_t *, 374 efi_guid_t *, unsigned long *, 375 void *); 376 efi_status_t (__efiapi *set_info) (efi_file_protocol_t *, 377 efi_guid_t *, unsigned long, 378 void *); 379 efi_status_t (__efiapi *flush) (efi_file_protocol_t *); 380 }; 381 382 typedef struct efi_simple_file_system_protocol efi_simple_file_system_protocol_t; 383 384 struct efi_simple_file_system_protocol { 385 u64 revision; 386 int (__efiapi *open_volume)(efi_simple_file_system_protocol_t *, 387 efi_file_protocol_t **); 388 }; 389 390 #define EFI_FILE_MODE_READ 0x0000000000000001 391 #define EFI_FILE_MODE_WRITE 0x0000000000000002 392 #define EFI_FILE_MODE_CREATE 0x8000000000000000 393 394 typedef enum { 395 EfiPciIoWidthUint8, 396 EfiPciIoWidthUint16, 397 EfiPciIoWidthUint32, 398 EfiPciIoWidthUint64, 399 EfiPciIoWidthFifoUint8, 400 EfiPciIoWidthFifoUint16, 401 EfiPciIoWidthFifoUint32, 402 EfiPciIoWidthFifoUint64, 403 EfiPciIoWidthFillUint8, 404 EfiPciIoWidthFillUint16, 405 EfiPciIoWidthFillUint32, 406 EfiPciIoWidthFillUint64, 407 EfiPciIoWidthMaximum 408 } EFI_PCI_IO_PROTOCOL_WIDTH; 409 410 typedef enum { 411 EfiPciIoAttributeOperationGet, 412 EfiPciIoAttributeOperationSet, 413 EfiPciIoAttributeOperationEnable, 414 EfiPciIoAttributeOperationDisable, 415 EfiPciIoAttributeOperationSupported, 416 EfiPciIoAttributeOperationMaximum 417 } EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION; 418 419 typedef struct { 420 u32 read; 421 u32 write; 422 } efi_pci_io_protocol_access_32_t; 423 424 typedef union efi_pci_io_protocol efi_pci_io_protocol_t; 425 426 typedef 427 efi_status_t (__efiapi *efi_pci_io_protocol_cfg_t)(efi_pci_io_protocol_t *, 428 EFI_PCI_IO_PROTOCOL_WIDTH, 429 u32 offset, 430 unsigned long count, 431 void *buffer); 432 433 typedef struct { 434 void *read; 435 void *write; 436 } efi_pci_io_protocol_access_t; 437 438 typedef struct { 439 efi_pci_io_protocol_cfg_t read; 440 efi_pci_io_protocol_cfg_t write; 441 } efi_pci_io_protocol_config_access_t; 442 443 union efi_pci_io_protocol { 444 struct { 445 void *poll_mem; 446 void *poll_io; 447 efi_pci_io_protocol_access_t mem; 448 efi_pci_io_protocol_access_t io; 449 efi_pci_io_protocol_config_access_t pci; 450 void *copy_mem; 451 void *map; 452 void *unmap; 453 void *allocate_buffer; 454 void *free_buffer; 455 void *flush; 456 efi_status_t (__efiapi *get_location)(efi_pci_io_protocol_t *, 457 unsigned long *segment_nr, 458 unsigned long *bus_nr, 459 unsigned long *device_nr, 460 unsigned long *func_nr); 461 void *attributes; 462 void *get_bar_attributes; 463 void *set_bar_attributes; 464 uint64_t romsize; 465 void *romimage; 466 }; 467 struct { 468 u32 poll_mem; 469 u32 poll_io; 470 efi_pci_io_protocol_access_32_t mem; 471 efi_pci_io_protocol_access_32_t io; 472 efi_pci_io_protocol_access_32_t pci; 473 u32 copy_mem; 474 u32 map; 475 u32 unmap; 476 u32 allocate_buffer; 477 u32 free_buffer; 478 u32 flush; 479 u32 get_location; 480 u32 attributes; 481 u32 get_bar_attributes; 482 u32 set_bar_attributes; 483 u64 romsize; 484 u32 romimage; 485 } mixed_mode; 486 }; 487 488 #define EFI_PCI_IO_ATTRIBUTE_ISA_MOTHERBOARD_IO 0x0001 489 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO 0x0002 490 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO 0x0004 491 #define EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY 0x0008 492 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO 0x0010 493 #define EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO 0x0020 494 #define EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO 0x0040 495 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE 0x0080 496 #define EFI_PCI_IO_ATTRIBUTE_IO 0x0100 497 #define EFI_PCI_IO_ATTRIBUTE_MEMORY 0x0200 498 #define EFI_PCI_IO_ATTRIBUTE_BUS_MASTER 0x0400 499 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED 0x0800 500 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_DISABLE 0x1000 501 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE 0x2000 502 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM 0x4000 503 #define EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE 0x8000 504 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO_16 0x10000 505 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16 0x20000 506 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO_16 0x40000 507 508 struct efi_dev_path; 509 510 typedef union apple_properties_protocol apple_properties_protocol_t; 511 512 union apple_properties_protocol { 513 struct { 514 unsigned long version; 515 efi_status_t (__efiapi *get)(apple_properties_protocol_t *, 516 struct efi_dev_path *, 517 efi_char16_t *, void *, u32 *); 518 efi_status_t (__efiapi *set)(apple_properties_protocol_t *, 519 struct efi_dev_path *, 520 efi_char16_t *, void *, u32); 521 efi_status_t (__efiapi *del)(apple_properties_protocol_t *, 522 struct efi_dev_path *, 523 efi_char16_t *); 524 efi_status_t (__efiapi *get_all)(apple_properties_protocol_t *, 525 void *buffer, u32 *); 526 }; 527 struct { 528 u32 version; 529 u32 get; 530 u32 set; 531 u32 del; 532 u32 get_all; 533 } mixed_mode; 534 }; 535 536 typedef u32 efi_tcg2_event_log_format; 537 538 typedef union efi_tcg2_protocol efi_tcg2_protocol_t; 539 540 union efi_tcg2_protocol { 541 struct { 542 void *get_capability; 543 efi_status_t (__efiapi *get_event_log)(efi_handle_t, 544 efi_tcg2_event_log_format, 545 efi_physical_addr_t *, 546 efi_physical_addr_t *, 547 efi_bool_t *); 548 void *hash_log_extend_event; 549 void *submit_command; 550 void *get_active_pcr_banks; 551 void *set_active_pcr_banks; 552 void *get_result_of_set_active_pcr_banks; 553 }; 554 struct { 555 u32 get_capability; 556 u32 get_event_log; 557 u32 hash_log_extend_event; 558 u32 submit_command; 559 u32 get_active_pcr_banks; 560 u32 set_active_pcr_banks; 561 u32 get_result_of_set_active_pcr_banks; 562 } mixed_mode; 563 }; 564 565 typedef union efi_load_file_protocol efi_load_file_protocol_t; 566 typedef union efi_load_file_protocol efi_load_file2_protocol_t; 567 568 union efi_load_file_protocol { 569 struct { 570 efi_status_t (__efiapi *load_file)(efi_load_file_protocol_t *, 571 efi_device_path_protocol_t *, 572 bool, unsigned long *, void *); 573 }; 574 struct { 575 u32 load_file; 576 } mixed_mode; 577 }; 578 579 void efi_pci_disable_bridge_busmaster(void); 580 581 typedef efi_status_t (*efi_exit_boot_map_processing)( 582 struct efi_boot_memmap *map, 583 void *priv); 584 585 efi_status_t efi_exit_boot_services(void *handle, 586 struct efi_boot_memmap *map, 587 void *priv, 588 efi_exit_boot_map_processing priv_func); 589 590 void efi_char16_printk(efi_char16_t *); 591 592 efi_status_t allocate_new_fdt_and_exit_boot(void *handle, 593 unsigned long *new_fdt_addr, 594 unsigned long max_addr, 595 u64 initrd_addr, u64 initrd_size, 596 char *cmdline_ptr, 597 unsigned long fdt_addr, 598 unsigned long fdt_size); 599 600 void *get_fdt(unsigned long *fdt_size); 601 602 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size, 603 unsigned long desc_size, efi_memory_desc_t *runtime_map, 604 int *count); 605 606 efi_status_t efi_get_random_bytes(unsigned long size, u8 *out); 607 608 efi_status_t efi_random_alloc(unsigned long size, unsigned long align, 609 unsigned long *addr, unsigned long random_seed); 610 611 efi_status_t check_platform_features(void); 612 613 void *get_efi_config_table(efi_guid_t guid); 614 615 void efi_printk(char *str); 616 617 void efi_free(unsigned long size, unsigned long addr); 618 619 char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len, 620 unsigned long max_addr); 621 622 efi_status_t efi_get_memory_map(struct efi_boot_memmap *map); 623 624 efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align, 625 unsigned long *addr, unsigned long min); 626 627 static inline 628 efi_status_t efi_low_alloc(unsigned long size, unsigned long align, 629 unsigned long *addr) 630 { 631 /* 632 * Don't allocate at 0x0. It will confuse code that 633 * checks pointers against NULL. Skip the first 8 634 * bytes so we start at a nice even number. 635 */ 636 return efi_low_alloc_above(size, align, addr, 0x8); 637 } 638 639 efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr, 640 unsigned long max); 641 642 efi_status_t efi_relocate_kernel(unsigned long *image_addr, 643 unsigned long image_size, 644 unsigned long alloc_size, 645 unsigned long preferred_addr, 646 unsigned long alignment, 647 unsigned long min_addr); 648 649 efi_status_t efi_parse_options(char const *cmdline); 650 651 efi_status_t efi_setup_gop(struct screen_info *si, efi_guid_t *proto, 652 unsigned long size); 653 654 efi_status_t efi_load_dtb(efi_loaded_image_t *image, 655 unsigned long *load_addr, 656 unsigned long *load_size); 657 658 efi_status_t efi_load_initrd(efi_loaded_image_t *image, 659 unsigned long *load_addr, 660 unsigned long *load_size, 661 unsigned long soft_limit, 662 unsigned long hard_limit); 663 664 efi_status_t efi_load_initrd_dev_path(unsigned long *load_addr, 665 unsigned long *load_size, 666 unsigned long max); 667 668 #endif 669