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