1 /* SPDX-License-Identifier: GPL-2.0 */ 2 3 #ifndef _DRIVERS_FIRMWARE_EFI_EFISTUB_H 4 #define _DRIVERS_FIRMWARE_EFI_EFISTUB_H 5 6 #include <linux/compiler.h> 7 #include <linux/cleanup.h> 8 #include <linux/efi.h> 9 #include <linux/kernel.h> 10 #include <linux/kern_levels.h> 11 #include <linux/types.h> 12 #include <asm/efi.h> 13 14 /* 15 * __init annotations should not be used in the EFI stub, since the code is 16 * either included in the decompressor (x86, ARM) where they have no effect, 17 * or the whole stub is __init annotated at the section level (arm64), by 18 * renaming the sections, in which case the __init annotation will be 19 * redundant, and will result in section names like .init.init.text, and our 20 * linker script does not expect that. 21 */ 22 #undef __init 23 24 /* 25 * Allow the platform to override the allocation granularity: this allows 26 * systems that have the capability to run with a larger page size to deal 27 * with the allocations for initrd and fdt more efficiently. 28 */ 29 #ifndef EFI_ALLOC_ALIGN 30 #define EFI_ALLOC_ALIGN EFI_PAGE_SIZE 31 #endif 32 33 #ifndef EFI_ALLOC_LIMIT 34 #define EFI_ALLOC_LIMIT ULONG_MAX 35 #endif 36 37 struct edid_info; 38 struct screen_info; 39 40 extern bool efi_no5lvl; 41 extern bool efi_nochunk; 42 extern bool efi_nokaslr; 43 extern int efi_loglevel; 44 extern int efi_mem_encrypt; 45 extern bool efi_novamap; 46 extern const efi_system_table_t *efi_system_table; 47 48 typedef union efi_dxe_services_table efi_dxe_services_table_t; 49 extern const efi_dxe_services_table_t *efi_dxe_table; 50 51 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, 52 efi_system_table_t *sys_table_arg); 53 54 #ifndef ARCH_HAS_EFISTUB_WRAPPERS 55 56 #define efi_is_native() (true) 57 #define efi_table_attr(inst, attr) (inst)->attr 58 #define efi_fn_call(inst, func, ...) (inst)->func(__VA_ARGS__) 59 60 #endif 61 62 #define efi_call_proto(inst, func, ...) ({ \ 63 __typeof__(inst) __inst = (inst); \ 64 efi_fn_call(__inst, func, __inst, ##__VA_ARGS__); \ 65 }) 66 #define efi_bs_call(func, ...) \ 67 efi_fn_call(efi_table_attr(efi_system_table, boottime), func, ##__VA_ARGS__) 68 #define efi_rt_call(func, ...) \ 69 efi_fn_call(efi_table_attr(efi_system_table, runtime), func, ##__VA_ARGS__) 70 #define efi_dxe_call(func, ...) \ 71 efi_fn_call(efi_dxe_table, func, ##__VA_ARGS__) 72 73 #define efi_info(fmt, ...) \ 74 efi_printk(KERN_INFO fmt, ##__VA_ARGS__) 75 #define efi_warn(fmt, ...) \ 76 efi_printk(KERN_WARNING "WARNING: " fmt, ##__VA_ARGS__) 77 #define efi_err(fmt, ...) \ 78 efi_printk(KERN_ERR "ERROR: " fmt, ##__VA_ARGS__) 79 #define efi_debug(fmt, ...) \ 80 efi_printk(KERN_DEBUG "DEBUG: " fmt, ##__VA_ARGS__) 81 82 #define efi_printk_once(fmt, ...) \ 83 ({ \ 84 static bool __print_once; \ 85 bool __ret_print_once = !__print_once; \ 86 \ 87 if (!__print_once) { \ 88 __print_once = true; \ 89 efi_printk(fmt, ##__VA_ARGS__); \ 90 } \ 91 __ret_print_once; \ 92 }) 93 94 #define efi_info_once(fmt, ...) \ 95 efi_printk_once(KERN_INFO fmt, ##__VA_ARGS__) 96 #define efi_warn_once(fmt, ...) \ 97 efi_printk_once(KERN_WARNING "WARNING: " fmt, ##__VA_ARGS__) 98 #define efi_err_once(fmt, ...) \ 99 efi_printk_once(KERN_ERR "ERROR: " fmt, ##__VA_ARGS__) 100 #define efi_debug_once(fmt, ...) \ 101 efi_printk_once(KERN_DEBUG "DEBUG: " fmt, ##__VA_ARGS__) 102 103 /* Helper macros for the usual case of using simple C variables: */ 104 #ifndef fdt_setprop_inplace_var 105 #define fdt_setprop_inplace_var(fdt, node_offset, name, var) \ 106 fdt_setprop_inplace((fdt), (node_offset), (name), &(var), sizeof(var)) 107 #endif 108 109 #ifndef fdt_setprop_var 110 #define fdt_setprop_var(fdt, node_offset, name, var) \ 111 fdt_setprop((fdt), (node_offset), (name), &(var), sizeof(var)) 112 #endif 113 114 #define get_efi_var(name, vendor, ...) \ 115 efi_rt_call(get_variable, (efi_char16_t *)(name), \ 116 (efi_guid_t *)(vendor), __VA_ARGS__) 117 118 #define set_efi_var(name, vendor, ...) \ 119 efi_rt_call(set_variable, (efi_char16_t *)(name), \ 120 (efi_guid_t *)(vendor), __VA_ARGS__) 121 122 #define efi_get_handle_at(array, idx) \ 123 (efi_is_native() ? (array)[idx] \ 124 : (efi_handle_t)(unsigned long)((u32 *)(array))[idx]) 125 126 #define efi_get_handle_num(size) \ 127 ((size) / (efi_is_native() ? sizeof(efi_handle_t) : sizeof(u32))) 128 129 #define for_each_efi_handle(handle, array, num) \ 130 for (int __i = 0; __i < (num) && \ 131 ((handle = efi_get_handle_at((array), __i)) || true); \ 132 __i++) 133 134 static inline 135 void efi_set_u64_split(u64 data, u32 *lo, u32 *hi) 136 { 137 *lo = lower_32_bits(data); 138 *hi = upper_32_bits(data); 139 } 140 141 /* 142 * Allocation types for calls to boottime->allocate_pages. 143 */ 144 #define EFI_ALLOCATE_ANY_PAGES 0 145 #define EFI_ALLOCATE_MAX_ADDRESS 1 146 #define EFI_ALLOCATE_ADDRESS 2 147 #define EFI_MAX_ALLOCATE_TYPE 3 148 149 /* 150 * The type of search to perform when calling boottime->locate_handle 151 */ 152 #define EFI_LOCATE_ALL_HANDLES 0 153 #define EFI_LOCATE_BY_REGISTER_NOTIFY 1 154 #define EFI_LOCATE_BY_PROTOCOL 2 155 156 /* 157 * boottime->stall takes the time period in microseconds 158 */ 159 #define EFI_USEC_PER_SEC 1000000 160 161 /* 162 * boottime->set_timer takes the time in 100ns units 163 */ 164 #define EFI_100NSEC_PER_USEC ((u64)10) 165 166 /* 167 * An efi_boot_memmap is used by efi_get_memory_map() to return the 168 * EFI memory map in a dynamically allocated buffer. 169 * 170 * The buffer allocated for the EFI memory map includes extra room for 171 * a minimum of EFI_MMAP_NR_SLACK_SLOTS additional EFI memory descriptors. 172 * This facilitates the reuse of the EFI memory map buffer when a second 173 * call to ExitBootServices() is needed because of intervening changes to 174 * the EFI memory map. Other related structures, e.g. x86 e820ext, need 175 * to factor in this headroom requirement as well. 176 */ 177 #define EFI_MMAP_NR_SLACK_SLOTS 32 178 179 typedef struct efi_generic_dev_path efi_device_path_protocol_t; 180 181 union efi_device_path_to_text_protocol { 182 struct { 183 efi_char16_t *(__efiapi *convert_device_node_to_text)( 184 const efi_device_path_protocol_t *, 185 bool, bool); 186 efi_char16_t *(__efiapi *convert_device_path_to_text)( 187 const efi_device_path_protocol_t *, 188 bool, bool); 189 }; 190 struct { 191 u32 convert_device_node_to_text; 192 u32 convert_device_path_to_text; 193 } mixed_mode; 194 }; 195 196 typedef union efi_device_path_to_text_protocol efi_device_path_to_text_protocol_t; 197 198 union efi_device_path_from_text_protocol { 199 struct { 200 efi_device_path_protocol_t * 201 (__efiapi *convert_text_to_device_node)(const efi_char16_t *); 202 efi_device_path_protocol_t * 203 (__efiapi *convert_text_to_device_path)(const efi_char16_t *); 204 }; 205 struct { 206 u32 convert_text_to_device_node; 207 u32 convert_text_to_device_path; 208 } mixed_mode; 209 }; 210 211 typedef union efi_device_path_from_text_protocol efi_device_path_from_text_protocol_t; 212 213 typedef void *efi_event_t; 214 /* Note that notifications won't work in mixed mode */ 215 typedef void (__efiapi *efi_event_notify_t)(efi_event_t, void *); 216 217 #define EFI_EVT_TIMER 0x80000000U 218 #define EFI_EVT_RUNTIME 0x40000000U 219 #define EFI_EVT_NOTIFY_WAIT 0x00000100U 220 #define EFI_EVT_NOTIFY_SIGNAL 0x00000200U 221 222 /** 223 * efi_set_event_at() - add event to events array 224 * 225 * @events: array of UEFI events 226 * @ids: index where to put the event in the array 227 * @event: event to add to the aray 228 * 229 * boottime->wait_for_event() takes an array of events as input. 230 * Provide a helper to set it up correctly for mixed mode. 231 */ 232 static inline 233 void efi_set_event_at(efi_event_t *events, size_t idx, efi_event_t event) 234 { 235 if (efi_is_native()) 236 events[idx] = event; 237 else 238 ((u32 *)events)[idx] = (u32)(unsigned long)event; 239 } 240 241 #define EFI_TPL_APPLICATION 4 242 #define EFI_TPL_CALLBACK 8 243 #define EFI_TPL_NOTIFY 16 244 #define EFI_TPL_HIGH_LEVEL 31 245 246 typedef enum { 247 EfiTimerCancel, 248 EfiTimerPeriodic, 249 EfiTimerRelative 250 } EFI_TIMER_DELAY; 251 252 /* 253 * EFI Boot Services table 254 */ 255 union efi_boot_services { 256 struct { 257 efi_table_hdr_t hdr; 258 void *raise_tpl; 259 void *restore_tpl; 260 efi_status_t (__efiapi *allocate_pages)(int, int, unsigned long, 261 efi_physical_addr_t *); 262 efi_status_t (__efiapi *free_pages)(efi_physical_addr_t, 263 unsigned long); 264 efi_status_t (__efiapi *get_memory_map)(unsigned long *, void *, 265 unsigned long *, 266 unsigned long *, u32 *); 267 efi_status_t (__efiapi *allocate_pool)(int, unsigned long, 268 void **); 269 efi_status_t (__efiapi *free_pool)(void *); 270 efi_status_t (__efiapi *create_event)(u32, unsigned long, 271 efi_event_notify_t, void *, 272 efi_event_t *); 273 efi_status_t (__efiapi *set_timer)(efi_event_t, 274 EFI_TIMER_DELAY, u64); 275 efi_status_t (__efiapi *wait_for_event)(unsigned long, 276 efi_event_t *, 277 unsigned long *); 278 void *signal_event; 279 efi_status_t (__efiapi *close_event)(efi_event_t); 280 void *check_event; 281 void *install_protocol_interface; 282 void *reinstall_protocol_interface; 283 void *uninstall_protocol_interface; 284 efi_status_t (__efiapi *handle_protocol)(efi_handle_t, 285 efi_guid_t *, void **); 286 void *__reserved; 287 void *register_protocol_notify; 288 efi_status_t (__efiapi *locate_handle)(int, efi_guid_t *, 289 void *, unsigned long *, 290 efi_handle_t *); 291 efi_status_t (__efiapi *locate_device_path)(efi_guid_t *, 292 efi_device_path_protocol_t **, 293 efi_handle_t *); 294 efi_status_t (__efiapi *install_configuration_table)(efi_guid_t *, 295 void *); 296 efi_status_t (__efiapi *load_image)(bool, efi_handle_t, 297 efi_device_path_protocol_t *, 298 void *, unsigned long, 299 efi_handle_t *); 300 efi_status_t (__efiapi *start_image)(efi_handle_t, unsigned long *, 301 efi_char16_t **); 302 efi_status_t __noreturn (__efiapi *exit)(efi_handle_t, 303 efi_status_t, 304 unsigned long, 305 efi_char16_t *); 306 efi_status_t (__efiapi *unload_image)(efi_handle_t); 307 efi_status_t (__efiapi *exit_boot_services)(efi_handle_t, 308 unsigned long); 309 void *get_next_monotonic_count; 310 efi_status_t (__efiapi *stall)(unsigned long); 311 void *set_watchdog_timer; 312 void *connect_controller; 313 efi_status_t (__efiapi *disconnect_controller)(efi_handle_t, 314 efi_handle_t, 315 efi_handle_t); 316 void *open_protocol; 317 void *close_protocol; 318 void *open_protocol_information; 319 void *protocols_per_handle; 320 efi_status_t (__efiapi *locate_handle_buffer)(int, efi_guid_t *, 321 void *, unsigned long *, 322 efi_handle_t **); 323 efi_status_t (__efiapi *locate_protocol)(efi_guid_t *, void *, 324 void **); 325 efi_status_t (__efiapi *install_multiple_protocol_interfaces)(efi_handle_t *, ...); 326 efi_status_t (__efiapi *uninstall_multiple_protocol_interfaces)(efi_handle_t, ...); 327 void *calculate_crc32; 328 void (__efiapi *copy_mem)(void *, const void *, unsigned long); 329 void (__efiapi *set_mem)(void *, unsigned long, unsigned char); 330 void *create_event_ex; 331 }; 332 struct { 333 efi_table_hdr_t hdr; 334 u32 raise_tpl; 335 u32 restore_tpl; 336 u32 allocate_pages; 337 u32 free_pages; 338 u32 get_memory_map; 339 u32 allocate_pool; 340 u32 free_pool; 341 u32 create_event; 342 u32 set_timer; 343 u32 wait_for_event; 344 u32 signal_event; 345 u32 close_event; 346 u32 check_event; 347 u32 install_protocol_interface; 348 u32 reinstall_protocol_interface; 349 u32 uninstall_protocol_interface; 350 u32 handle_protocol; 351 u32 __reserved; 352 u32 register_protocol_notify; 353 u32 locate_handle; 354 u32 locate_device_path; 355 u32 install_configuration_table; 356 u32 load_image; 357 u32 start_image; 358 u32 exit; 359 u32 unload_image; 360 u32 exit_boot_services; 361 u32 get_next_monotonic_count; 362 u32 stall; 363 u32 set_watchdog_timer; 364 u32 connect_controller; 365 u32 disconnect_controller; 366 u32 open_protocol; 367 u32 close_protocol; 368 u32 open_protocol_information; 369 u32 protocols_per_handle; 370 u32 locate_handle_buffer; 371 u32 locate_protocol; 372 u32 install_multiple_protocol_interfaces; 373 u32 uninstall_multiple_protocol_interfaces; 374 u32 calculate_crc32; 375 u32 copy_mem; 376 u32 set_mem; 377 u32 create_event_ex; 378 } mixed_mode; 379 }; 380 381 typedef enum { 382 EfiGcdMemoryTypeNonExistent, 383 EfiGcdMemoryTypeReserved, 384 EfiGcdMemoryTypeSystemMemory, 385 EfiGcdMemoryTypeMemoryMappedIo, 386 EfiGcdMemoryTypePersistent, 387 EfiGcdMemoryTypeMoreReliable, 388 EfiGcdMemoryTypeMaximum 389 } efi_gcd_memory_type_t; 390 391 typedef struct { 392 efi_physical_addr_t base_address; 393 u64 length; 394 u64 capabilities; 395 u64 attributes; 396 efi_gcd_memory_type_t gcd_memory_type; 397 void *image_handle; 398 void *device_handle; 399 } efi_gcd_memory_space_desc_t; 400 401 /* 402 * EFI DXE Services table 403 */ 404 union efi_dxe_services_table { 405 struct { 406 efi_table_hdr_t hdr; 407 void *add_memory_space; 408 void *allocate_memory_space; 409 void *free_memory_space; 410 void *remove_memory_space; 411 efi_status_t (__efiapi *get_memory_space_descriptor)(efi_physical_addr_t, 412 efi_gcd_memory_space_desc_t *); 413 efi_status_t (__efiapi *set_memory_space_attributes)(efi_physical_addr_t, 414 u64, u64); 415 void *get_memory_space_map; 416 void *add_io_space; 417 void *allocate_io_space; 418 void *free_io_space; 419 void *remove_io_space; 420 void *get_io_space_descriptor; 421 void *get_io_space_map; 422 void *dispatch; 423 void *schedule; 424 void *trust; 425 void *process_firmware_volume; 426 void *set_memory_space_capabilities; 427 }; 428 struct { 429 efi_table_hdr_t hdr; 430 u32 add_memory_space; 431 u32 allocate_memory_space; 432 u32 free_memory_space; 433 u32 remove_memory_space; 434 u32 get_memory_space_descriptor; 435 u32 set_memory_space_attributes; 436 u32 get_memory_space_map; 437 u32 add_io_space; 438 u32 allocate_io_space; 439 u32 free_io_space; 440 u32 remove_io_space; 441 u32 get_io_space_descriptor; 442 u32 get_io_space_map; 443 u32 dispatch; 444 u32 schedule; 445 u32 trust; 446 u32 process_firmware_volume; 447 u32 set_memory_space_capabilities; 448 } mixed_mode; 449 }; 450 451 typedef union efi_memory_attribute_protocol efi_memory_attribute_protocol_t; 452 453 union efi_memory_attribute_protocol { 454 struct { 455 efi_status_t (__efiapi *get_memory_attributes)( 456 efi_memory_attribute_protocol_t *, efi_physical_addr_t, u64, u64 *); 457 458 efi_status_t (__efiapi *set_memory_attributes)( 459 efi_memory_attribute_protocol_t *, efi_physical_addr_t, u64, u64); 460 461 efi_status_t (__efiapi *clear_memory_attributes)( 462 efi_memory_attribute_protocol_t *, efi_physical_addr_t, u64, u64); 463 }; 464 struct { 465 u32 get_memory_attributes; 466 u32 set_memory_attributes; 467 u32 clear_memory_attributes; 468 } mixed_mode; 469 }; 470 471 typedef union efi_uga_draw_protocol efi_uga_draw_protocol_t; 472 473 union efi_uga_draw_protocol { 474 struct { 475 efi_status_t (__efiapi *get_mode)(efi_uga_draw_protocol_t *, 476 u32*, u32*, u32*, u32*); 477 void *set_mode; 478 void *blt; 479 }; 480 struct { 481 u32 get_mode; 482 u32 set_mode; 483 u32 blt; 484 } mixed_mode; 485 }; 486 487 typedef struct { 488 u16 scan_code; 489 efi_char16_t unicode_char; 490 } efi_input_key_t; 491 492 union efi_simple_text_input_protocol { 493 struct { 494 void *reset; 495 efi_status_t (__efiapi *read_keystroke)(efi_simple_text_input_protocol_t *, 496 efi_input_key_t *); 497 efi_event_t wait_for_key; 498 }; 499 struct { 500 u32 reset; 501 u32 read_keystroke; 502 u32 wait_for_key; 503 } mixed_mode; 504 }; 505 506 efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key); 507 508 union efi_simple_text_output_protocol { 509 struct { 510 void *reset; 511 efi_status_t (__efiapi *output_string)(efi_simple_text_output_protocol_t *, 512 efi_char16_t *); 513 void *test_string; 514 }; 515 struct { 516 u32 reset; 517 u32 output_string; 518 u32 test_string; 519 } mixed_mode; 520 }; 521 522 #define PIXEL_RGB_RESERVED_8BIT_PER_COLOR 0 523 #define PIXEL_BGR_RESERVED_8BIT_PER_COLOR 1 524 #define PIXEL_BIT_MASK 2 525 #define PIXEL_BLT_ONLY 3 526 #define PIXEL_FORMAT_MAX 4 527 528 typedef struct { 529 u32 red_mask; 530 u32 green_mask; 531 u32 blue_mask; 532 u32 reserved_mask; 533 } efi_pixel_bitmask_t; 534 535 typedef struct { 536 u32 version; 537 u32 horizontal_resolution; 538 u32 vertical_resolution; 539 int pixel_format; 540 efi_pixel_bitmask_t pixel_information; 541 u32 pixels_per_scan_line; 542 } efi_graphics_output_mode_info_t; 543 544 typedef union efi_graphics_output_protocol_mode efi_graphics_output_protocol_mode_t; 545 546 union efi_graphics_output_protocol_mode { 547 struct { 548 u32 max_mode; 549 u32 mode; 550 efi_graphics_output_mode_info_t *info; 551 unsigned long size_of_info; 552 efi_physical_addr_t frame_buffer_base; 553 unsigned long frame_buffer_size; 554 }; 555 struct { 556 u32 max_mode; 557 u32 mode; 558 u32 info; 559 u32 size_of_info; 560 u64 frame_buffer_base; 561 u32 frame_buffer_size; 562 } mixed_mode; 563 }; 564 565 typedef union efi_graphics_output_protocol efi_graphics_output_protocol_t; 566 567 union efi_graphics_output_protocol { 568 struct { 569 efi_status_t (__efiapi *query_mode)(efi_graphics_output_protocol_t *, 570 u32, unsigned long *, 571 efi_graphics_output_mode_info_t **); 572 efi_status_t (__efiapi *set_mode) (efi_graphics_output_protocol_t *, u32); 573 void *blt; 574 efi_graphics_output_protocol_mode_t *mode; 575 }; 576 struct { 577 u32 query_mode; 578 u32 set_mode; 579 u32 blt; 580 u32 mode; 581 } mixed_mode; 582 }; 583 584 typedef union efi_edid_discovered_protocol efi_edid_discovered_protocol_t; 585 586 union efi_edid_discovered_protocol { 587 struct { 588 u32 size_of_edid; 589 u8 *edid; 590 }; 591 struct { 592 u32 size_of_edid; 593 u32 edid; 594 } mixed_mode; 595 }; 596 597 typedef union efi_edid_active_protocol efi_edid_active_protocol_t; 598 599 union efi_edid_active_protocol { 600 struct { 601 u32 size_of_edid; 602 u8 *edid; 603 }; 604 struct { 605 u32 size_of_edid; 606 u32 edid; 607 } mixed_mode; 608 }; 609 610 typedef union { 611 struct { 612 u32 revision; 613 efi_handle_t parent_handle; 614 efi_system_table_t *system_table; 615 efi_handle_t device_handle; 616 void *file_path; 617 void *reserved; 618 u32 load_options_size; 619 void *load_options; 620 void *image_base; 621 __aligned_u64 image_size; 622 unsigned int image_code_type; 623 unsigned int image_data_type; 624 efi_status_t (__efiapi *unload)(efi_handle_t image_handle); 625 }; 626 struct { 627 u32 revision; 628 u32 parent_handle; 629 u32 system_table; 630 u32 device_handle; 631 u32 file_path; 632 u32 reserved; 633 u32 load_options_size; 634 u32 load_options; 635 u32 image_base; 636 __aligned_u64 image_size; 637 u32 image_code_type; 638 u32 image_data_type; 639 u32 unload; 640 } mixed_mode; 641 } efi_loaded_image_t; 642 643 typedef struct { 644 u64 size; 645 u64 file_size; 646 u64 phys_size; 647 efi_time_t create_time; 648 efi_time_t last_access_time; 649 efi_time_t modification_time; 650 __aligned_u64 attribute; 651 efi_char16_t filename[]; 652 } efi_file_info_t; 653 654 typedef union efi_file_protocol efi_file_protocol_t; 655 656 union efi_file_protocol { 657 struct { 658 u64 revision; 659 efi_status_t (__efiapi *open) (efi_file_protocol_t *, 660 efi_file_protocol_t **, 661 efi_char16_t *, u64, 662 u64); 663 efi_status_t (__efiapi *close) (efi_file_protocol_t *); 664 efi_status_t (__efiapi *delete) (efi_file_protocol_t *); 665 efi_status_t (__efiapi *read) (efi_file_protocol_t *, 666 unsigned long *, 667 void *); 668 efi_status_t (__efiapi *write) (efi_file_protocol_t *, 669 unsigned long, void *); 670 efi_status_t (__efiapi *get_position)(efi_file_protocol_t *, 671 u64 *); 672 efi_status_t (__efiapi *set_position)(efi_file_protocol_t *, 673 u64); 674 efi_status_t (__efiapi *get_info) (efi_file_protocol_t *, 675 efi_guid_t *, 676 unsigned long *, 677 void *); 678 efi_status_t (__efiapi *set_info) (efi_file_protocol_t *, 679 efi_guid_t *, 680 unsigned long, 681 void *); 682 efi_status_t (__efiapi *flush) (efi_file_protocol_t *); 683 }; 684 struct { 685 u64 revision; 686 u32 open; 687 u32 close; 688 u32 delete; 689 u32 read; 690 u32 write; 691 u32 get_position; 692 u32 set_position; 693 u32 get_info; 694 u32 set_info; 695 u32 flush; 696 } mixed_mode; 697 }; 698 699 typedef union efi_simple_file_system_protocol efi_simple_file_system_protocol_t; 700 701 union efi_simple_file_system_protocol { 702 struct { 703 u64 revision; 704 efi_status_t (__efiapi *open_volume)(efi_simple_file_system_protocol_t *, 705 efi_file_protocol_t **); 706 }; 707 struct { 708 u64 revision; 709 u32 open_volume; 710 } mixed_mode; 711 }; 712 713 #define EFI_FILE_MODE_READ 0x0000000000000001 714 #define EFI_FILE_MODE_WRITE 0x0000000000000002 715 #define EFI_FILE_MODE_CREATE 0x8000000000000000 716 717 typedef enum { 718 EfiPciIoWidthUint8, 719 EfiPciIoWidthUint16, 720 EfiPciIoWidthUint32, 721 EfiPciIoWidthUint64, 722 EfiPciIoWidthFifoUint8, 723 EfiPciIoWidthFifoUint16, 724 EfiPciIoWidthFifoUint32, 725 EfiPciIoWidthFifoUint64, 726 EfiPciIoWidthFillUint8, 727 EfiPciIoWidthFillUint16, 728 EfiPciIoWidthFillUint32, 729 EfiPciIoWidthFillUint64, 730 EfiPciIoWidthMaximum 731 } EFI_PCI_IO_PROTOCOL_WIDTH; 732 733 typedef enum { 734 EfiPciIoAttributeOperationGet, 735 EfiPciIoAttributeOperationSet, 736 EfiPciIoAttributeOperationEnable, 737 EfiPciIoAttributeOperationDisable, 738 EfiPciIoAttributeOperationSupported, 739 EfiPciIoAttributeOperationMaximum 740 } EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION; 741 742 typedef struct { 743 u32 read; 744 u32 write; 745 } efi_pci_io_protocol_access_32_t; 746 747 typedef union efi_pci_io_protocol efi_pci_io_protocol_t; 748 749 typedef 750 efi_status_t (__efiapi *efi_pci_io_protocol_cfg_t)(efi_pci_io_protocol_t *, 751 EFI_PCI_IO_PROTOCOL_WIDTH, 752 u32 offset, 753 unsigned long count, 754 void *buffer); 755 756 typedef struct { 757 void *read; 758 void *write; 759 } efi_pci_io_protocol_access_t; 760 761 typedef struct { 762 efi_pci_io_protocol_cfg_t read; 763 efi_pci_io_protocol_cfg_t write; 764 } efi_pci_io_protocol_config_access_t; 765 766 union efi_pci_io_protocol { 767 struct { 768 void *poll_mem; 769 void *poll_io; 770 efi_pci_io_protocol_access_t mem; 771 efi_pci_io_protocol_access_t io; 772 efi_pci_io_protocol_config_access_t pci; 773 void *copy_mem; 774 void *map; 775 void *unmap; 776 void *allocate_buffer; 777 void *free_buffer; 778 void *flush; 779 efi_status_t (__efiapi *get_location)(efi_pci_io_protocol_t *, 780 unsigned long *segment_nr, 781 unsigned long *bus_nr, 782 unsigned long *device_nr, 783 unsigned long *func_nr); 784 void *attributes; 785 void *get_bar_attributes; 786 void *set_bar_attributes; 787 uint64_t romsize; 788 void *romimage; 789 }; 790 struct { 791 u32 poll_mem; 792 u32 poll_io; 793 efi_pci_io_protocol_access_32_t mem; 794 efi_pci_io_protocol_access_32_t io; 795 efi_pci_io_protocol_access_32_t pci; 796 u32 copy_mem; 797 u32 map; 798 u32 unmap; 799 u32 allocate_buffer; 800 u32 free_buffer; 801 u32 flush; 802 u32 get_location; 803 u32 attributes; 804 u32 get_bar_attributes; 805 u32 set_bar_attributes; 806 u64 romsize; 807 u32 romimage; 808 } mixed_mode; 809 }; 810 811 #define EFI_PCI_IO_ATTRIBUTE_ISA_MOTHERBOARD_IO 0x0001 812 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO 0x0002 813 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO 0x0004 814 #define EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY 0x0008 815 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO 0x0010 816 #define EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO 0x0020 817 #define EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO 0x0040 818 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE 0x0080 819 #define EFI_PCI_IO_ATTRIBUTE_IO 0x0100 820 #define EFI_PCI_IO_ATTRIBUTE_MEMORY 0x0200 821 #define EFI_PCI_IO_ATTRIBUTE_BUS_MASTER 0x0400 822 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED 0x0800 823 #define EFI_PCI_IO_ATTRIBUTE_MEMORY_DISABLE 0x1000 824 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE 0x2000 825 #define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM 0x4000 826 #define EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE 0x8000 827 #define EFI_PCI_IO_ATTRIBUTE_ISA_IO_16 0x10000 828 #define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16 0x20000 829 #define EFI_PCI_IO_ATTRIBUTE_VGA_IO_16 0x40000 830 831 struct efi_dev_path; 832 833 typedef union apple_properties_protocol apple_properties_protocol_t; 834 835 union apple_properties_protocol { 836 struct { 837 unsigned long version; 838 efi_status_t (__efiapi *get)(apple_properties_protocol_t *, 839 struct efi_dev_path *, 840 efi_char16_t *, void *, u32 *); 841 efi_status_t (__efiapi *set)(apple_properties_protocol_t *, 842 struct efi_dev_path *, 843 efi_char16_t *, void *, u32); 844 efi_status_t (__efiapi *del)(apple_properties_protocol_t *, 845 struct efi_dev_path *, 846 efi_char16_t *); 847 efi_status_t (__efiapi *get_all)(apple_properties_protocol_t *, 848 void *buffer, u32 *); 849 }; 850 struct { 851 u32 version; 852 u32 get; 853 u32 set; 854 u32 del; 855 u32 get_all; 856 } mixed_mode; 857 }; 858 859 typedef u32 efi_tcg2_event_log_format; 860 861 #define INITRD_EVENT_TAG_ID 0x8F3B22ECU 862 #define LOAD_OPTIONS_EVENT_TAG_ID 0x8F3B22EDU 863 #define EV_EVENT_TAG 0x00000006U 864 #define EFI_TCG2_EVENT_HEADER_VERSION 0x1 865 866 struct efi_tcg2_event { 867 u32 event_size; 868 struct { 869 u32 header_size; 870 u16 header_version; 871 u32 pcr_index; 872 u32 event_type; 873 } __packed event_header; 874 /* u8[] event follows here */ 875 } __packed; 876 877 /* from TCG PC Client Platform Firmware Profile Specification */ 878 typedef struct tdTCG_PCClientTaggedEvent { 879 u32 tagged_event_id; 880 u32 tagged_event_data_size; 881 u8 tagged_event_data[]; 882 } TCG_PCClientTaggedEvent; 883 884 typedef struct efi_tcg2_event efi_tcg2_event_t; 885 typedef union efi_tcg2_protocol efi_tcg2_protocol_t; 886 887 union efi_tcg2_protocol { 888 struct { 889 void *get_capability; 890 efi_status_t (__efiapi *get_event_log)(efi_tcg2_protocol_t *, 891 efi_tcg2_event_log_format, 892 efi_physical_addr_t *, 893 efi_physical_addr_t *, 894 efi_bool_t *); 895 efi_status_t (__efiapi *hash_log_extend_event)(efi_tcg2_protocol_t *, 896 u64, 897 efi_physical_addr_t, 898 u64, 899 const efi_tcg2_event_t *); 900 void *submit_command; 901 void *get_active_pcr_banks; 902 void *set_active_pcr_banks; 903 void *get_result_of_set_active_pcr_banks; 904 }; 905 struct { 906 u32 get_capability; 907 u32 get_event_log; 908 u32 hash_log_extend_event; 909 u32 submit_command; 910 u32 get_active_pcr_banks; 911 u32 set_active_pcr_banks; 912 u32 get_result_of_set_active_pcr_banks; 913 } mixed_mode; 914 }; 915 916 typedef struct { 917 u8 major; 918 u8 minor; 919 } efi_cc_version_t; 920 921 typedef struct { 922 u8 type; 923 u8 sub_type; 924 } efi_cc_type_t; 925 926 /* EFI CC type/subtype defines */ 927 #define EFI_CC_TYPE_NONE 0 928 #define EFI_CC_TYPE_AMD_SEV 1 929 #define EFI_CC_TYPE_INTEL_TDX 2 930 931 typedef u32 efi_cc_mr_index_t; 932 933 struct efi_cc_event { 934 u32 event_size; 935 struct { 936 u32 header_size; 937 u16 header_version; 938 u32 mr_index; 939 u32 event_type; 940 } __packed event_header; 941 /* u8[] event follows here */ 942 } __packed; 943 944 typedef struct efi_cc_event efi_cc_event_t; 945 946 typedef u32 efi_cc_event_log_bitmap_t; 947 typedef u32 efi_cc_event_log_format_t; 948 typedef u32 efi_cc_event_algorithm_bitmap_t; 949 950 typedef struct { 951 u8 size; 952 efi_cc_version_t structure_version; 953 efi_cc_version_t protocol_version; 954 efi_cc_event_algorithm_bitmap_t hash_algorithm_bitmap; 955 efi_cc_event_log_bitmap_t supported_event_logs; 956 efi_cc_type_t cc_type; 957 } efi_cc_boot_service_cap_t; 958 959 #define EFI_CC_EVENT_HEADER_VERSION 1 960 961 #define EFI_CC_BOOT_HASH_ALG_SHA384 0x00000004 962 963 #define EFI_CC_EVENT_LOG_FORMAT_TCG_2 0x00000002 964 965 typedef union efi_cc_protocol efi_cc_protocol_t; 966 967 union efi_cc_protocol { 968 struct { 969 efi_status_t 970 (__efiapi *get_capability)(efi_cc_protocol_t *, 971 efi_cc_boot_service_cap_t *); 972 973 efi_status_t 974 (__efiapi *get_event_log)(efi_cc_protocol_t *, 975 efi_cc_event_log_format_t, 976 efi_physical_addr_t *, 977 efi_physical_addr_t *, 978 efi_bool_t *); 979 980 efi_status_t 981 (__efiapi *hash_log_extend_event)(efi_cc_protocol_t *, u64, 982 efi_physical_addr_t, u64, 983 const efi_cc_event_t *); 984 985 efi_status_t 986 (__efiapi *map_pcr_to_mr_index)(efi_cc_protocol_t *, u32, 987 efi_cc_mr_index_t *); 988 }; 989 struct { 990 u32 get_capability; 991 u32 get_event_log; 992 u32 hash_log_extend_event; 993 u32 map_pcr_to_mr_index; 994 } mixed_mode; 995 }; 996 997 struct riscv_efi_boot_protocol { 998 u64 revision; 999 1000 efi_status_t (__efiapi *get_boot_hartid)(struct riscv_efi_boot_protocol *, 1001 unsigned long *boot_hartid); 1002 }; 1003 1004 typedef union efi_load_file_protocol efi_load_file_protocol_t; 1005 typedef union efi_load_file_protocol efi_load_file2_protocol_t; 1006 1007 union efi_load_file_protocol { 1008 struct { 1009 efi_status_t (__efiapi *load_file)(efi_load_file_protocol_t *, 1010 efi_device_path_protocol_t *, 1011 bool, unsigned long *, void *); 1012 }; 1013 struct { 1014 u32 load_file; 1015 } mixed_mode; 1016 }; 1017 1018 typedef struct { 1019 u32 attributes; 1020 u16 file_path_list_length; 1021 u8 variable_data[]; 1022 // efi_char16_t description[]; 1023 // efi_device_path_protocol_t file_path_list[]; 1024 // u8 optional_data[]; 1025 } __packed efi_load_option_t; 1026 1027 #define EFI_LOAD_OPTION_ACTIVE 0x0001U 1028 #define EFI_LOAD_OPTION_FORCE_RECONNECT 0x0002U 1029 #define EFI_LOAD_OPTION_HIDDEN 0x0008U 1030 #define EFI_LOAD_OPTION_CATEGORY 0x1f00U 1031 #define EFI_LOAD_OPTION_CATEGORY_BOOT 0x0000U 1032 #define EFI_LOAD_OPTION_CATEGORY_APP 0x0100U 1033 1034 #define EFI_LOAD_OPTION_BOOT_MASK \ 1035 (EFI_LOAD_OPTION_ACTIVE|EFI_LOAD_OPTION_HIDDEN|EFI_LOAD_OPTION_CATEGORY) 1036 #define EFI_LOAD_OPTION_MASK (EFI_LOAD_OPTION_FORCE_RECONNECT|EFI_LOAD_OPTION_BOOT_MASK) 1037 1038 typedef struct { 1039 u32 attributes; 1040 u16 file_path_list_length; 1041 const efi_char16_t *description; 1042 const efi_device_path_protocol_t *file_path_list; 1043 u32 optional_data_size; 1044 const void *optional_data; 1045 } efi_load_option_unpacked_t; 1046 1047 void efi_pci_disable_bridge_busmaster(void); 1048 1049 typedef efi_status_t (*efi_exit_boot_map_processing)( 1050 struct efi_boot_memmap *map, 1051 void *priv); 1052 1053 efi_status_t efi_exit_boot_services(void *handle, void *priv, 1054 efi_exit_boot_map_processing priv_func); 1055 1056 efi_status_t efi_boot_kernel(void *handle, efi_loaded_image_t *image, 1057 unsigned long kernel_addr, char *cmdline_ptr); 1058 1059 void *get_fdt(unsigned long *fdt_size); 1060 1061 efi_status_t efi_alloc_virtmap(efi_memory_desc_t **virtmap, 1062 unsigned long *desc_size, u32 *desc_ver); 1063 void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size, 1064 unsigned long desc_size, efi_memory_desc_t *runtime_map, 1065 int *count); 1066 1067 efi_status_t efi_get_random_bytes(unsigned long size, u8 *out); 1068 1069 efi_status_t efi_random_alloc(unsigned long size, unsigned long align, 1070 unsigned long *addr, unsigned long random_seed, 1071 int memory_type, unsigned long alloc_min, 1072 unsigned long alloc_max); 1073 1074 efi_status_t efi_random_get_seed(void); 1075 1076 efi_status_t check_platform_features(void); 1077 1078 void *get_efi_config_table(efi_guid_t guid); 1079 1080 /* NOTE: These functions do not print a trailing newline after the string */ 1081 void efi_char16_puts(efi_char16_t *); 1082 void efi_puts(const char *str); 1083 1084 __printf(1, 2) int efi_printk(char const *fmt, ...); 1085 1086 void efi_free(unsigned long size, unsigned long addr); 1087 DEFINE_FREE(efi_pool, void *, if (_T) efi_bs_call(free_pool, _T)); 1088 1089 void efi_apply_loadoptions_quirk(const void **load_options, u32 *load_options_size); 1090 1091 char *efi_convert_cmdline(efi_loaded_image_t *image); 1092 1093 efi_status_t efi_get_memory_map(struct efi_boot_memmap **map, 1094 bool install_cfg_tbl); 1095 1096 efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr, 1097 unsigned long max); 1098 1099 efi_status_t efi_allocate_pages_aligned(unsigned long size, unsigned long *addr, 1100 unsigned long max, unsigned long align, 1101 int memory_type); 1102 1103 efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align, 1104 unsigned long *addr, unsigned long min); 1105 1106 efi_status_t efi_relocate_kernel(unsigned long *image_addr, 1107 unsigned long image_size, 1108 unsigned long alloc_size, 1109 unsigned long preferred_addr, 1110 unsigned long alignment, 1111 unsigned long min_addr); 1112 1113 efi_status_t efi_parse_options(char const *cmdline); 1114 1115 void efi_parse_option_graphics(char *option); 1116 1117 efi_status_t efi_setup_graphics(struct screen_info *si, struct edid_info *edid); 1118 1119 efi_status_t handle_cmdline_files(efi_loaded_image_t *image, 1120 const efi_char16_t *optstr, 1121 int optstr_size, 1122 unsigned long soft_limit, 1123 unsigned long hard_limit, 1124 unsigned long *load_addr, 1125 unsigned long *load_size); 1126 1127 1128 static inline efi_status_t efi_load_dtb(efi_loaded_image_t *image, 1129 unsigned long *load_addr, 1130 unsigned long *load_size) 1131 { 1132 return handle_cmdline_files(image, L"dtb=", sizeof(L"dtb=") - 2, 1133 ULONG_MAX, ULONG_MAX, load_addr, load_size); 1134 } 1135 1136 efi_status_t efi_load_initrd(efi_loaded_image_t *image, 1137 unsigned long soft_limit, 1138 unsigned long hard_limit, 1139 const struct linux_efi_initrd **out); 1140 /* 1141 * This function handles the architcture specific differences between arm and 1142 * arm64 regarding where the kernel image must be loaded and any memory that 1143 * must be reserved. On failure it is required to free all 1144 * all allocations it has made. 1145 */ 1146 efi_status_t handle_kernel_image(unsigned long *image_addr, 1147 unsigned long *image_size, 1148 unsigned long *reserve_addr, 1149 unsigned long *reserve_size, 1150 efi_loaded_image_t *image, 1151 efi_handle_t image_handle); 1152 1153 /* shared entrypoint between the normal stub and the zboot stub */ 1154 efi_status_t efi_stub_common(efi_handle_t handle, 1155 efi_loaded_image_t *image, 1156 unsigned long image_addr, 1157 char *cmdline_ptr); 1158 1159 efi_status_t efi_handle_cmdline(efi_loaded_image_t *image, char **cmdline_ptr); 1160 1161 asmlinkage void __noreturn efi_enter_kernel(unsigned long entrypoint, 1162 unsigned long fdt_addr, 1163 unsigned long fdt_size); 1164 1165 void efi_handle_post_ebs_state(void); 1166 1167 enum efi_secureboot_mode efi_get_secureboot(void); 1168 1169 #ifdef CONFIG_RESET_ATTACK_MITIGATION 1170 void efi_enable_reset_attack_mitigation(void); 1171 #else 1172 static inline void 1173 efi_enable_reset_attack_mitigation(void) { } 1174 #endif 1175 1176 void efi_retrieve_eventlog(void); 1177 1178 struct screen_info *alloc_screen_info(void); 1179 struct screen_info *__alloc_screen_info(void); 1180 void free_screen_info(struct screen_info *si); 1181 1182 void efi_cache_sync_image(unsigned long image_base, 1183 unsigned long alloc_size); 1184 1185 struct efi_smbios_record { 1186 u8 type; 1187 u8 length; 1188 u16 handle; 1189 }; 1190 1191 const struct efi_smbios_record *efi_get_smbios_record(u8 type); 1192 1193 struct efi_smbios_type1_record { 1194 struct efi_smbios_record header; 1195 1196 u8 manufacturer; 1197 u8 product_name; 1198 u8 version; 1199 u8 serial_number; 1200 efi_guid_t uuid; 1201 u8 wakeup_type; 1202 u8 sku_number; 1203 u8 family; 1204 }; 1205 1206 struct efi_smbios_type4_record { 1207 struct efi_smbios_record header; 1208 1209 u8 socket; 1210 u8 processor_type; 1211 u8 processor_family; 1212 u8 processor_manufacturer; 1213 u8 processor_id[8]; 1214 u8 processor_version; 1215 u8 voltage; 1216 u16 external_clock; 1217 u16 max_speed; 1218 u16 current_speed; 1219 u8 status; 1220 u8 processor_upgrade; 1221 u16 l1_cache_handle; 1222 u16 l2_cache_handle; 1223 u16 l3_cache_handle; 1224 u8 serial_number; 1225 u8 asset_tag; 1226 u8 part_number; 1227 u8 core_count; 1228 u8 enabled_core_count; 1229 u8 thread_count; 1230 u16 processor_characteristics; 1231 u16 processor_family2; 1232 u16 core_count2; 1233 u16 enabled_core_count2; 1234 u16 thread_count2; 1235 u16 thread_enabled; 1236 }; 1237 1238 #define efi_get_smbios_string(__record, __field) ({ \ 1239 __typeof__(__record) __rec = __record; \ 1240 __efi_get_smbios_string(&__rec->header, &__rec->__field); \ 1241 }) 1242 1243 const u8 *__efi_get_smbios_string(const struct efi_smbios_record *record, 1244 const u8 *offset); 1245 1246 void efi_remap_image(unsigned long image_base, unsigned alloc_size, 1247 unsigned long code_size); 1248 efi_status_t efi_kaslr_relocate_kernel(unsigned long *image_addr, 1249 unsigned long *reserve_addr, 1250 unsigned long *reserve_size, 1251 unsigned long kernel_size, 1252 unsigned long kernel_codesize, 1253 unsigned long kernel_memsize, 1254 u32 phys_seed); 1255 u32 efi_kaslr_get_phys_seed(efi_handle_t image_handle); 1256 1257 asmlinkage efi_status_t __efiapi 1258 efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab); 1259 1260 efi_status_t allocate_unaccepted_bitmap(__u32 nr_desc, 1261 struct efi_boot_memmap *map); 1262 void process_unaccepted_memory(u64 start, u64 end); 1263 void accept_memory(phys_addr_t start, unsigned long size); 1264 void arch_accept_memory(phys_addr_t start, phys_addr_t end); 1265 1266 efi_status_t efi_zboot_decompress_init(unsigned long *alloc_size); 1267 efi_status_t efi_zboot_decompress(u8 *out, unsigned long outlen); 1268 1269 #endif 1270