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