1 /* 2 * Copyright (c) 2020 Intel Corporation 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifndef ZEPHYR_ARCH_X86_EFI_H_ 8 #define ZEPHYR_ARCH_X86_EFI_H_ 9 10 #ifndef _ASMLANGUAGE 11 12 #include <stdbool.h> 13 #include <zephyr/autoconf.h> 14 15 #define __abi __attribute__((ms_abi)) 16 17 /* 18 * This is a quick installment of EFI structures and functions. 19 * Only a very minimal subset will be used and documented, thus the 20 * lack of documentation at the moment. 21 * See the UEFI 2.8b specifications for more information 22 * at https://www.uefi.org/specifications 23 */ 24 25 /* Note: all specified attributes/parameters of type char16_t have been 26 * translated to uint16_t as, for now, we don't have char16_t and we don't 27 * care being pedantic, plus we do not use it yet. 28 * This will need to be changed if required. 29 */ 30 31 typedef uintptr_t efi_status_t; 32 33 #define EFI_STATUS(_status) (_status | BIT((BITS_PER_LONG-1))) 34 35 #define EFI_SUCCESS 0 36 #define EFI_LOAD_ERROR EFI_STATUS(1) 37 #define EFI_INVALID_PARAMETER EFI_STATUS(2) 38 #define EFI_UNSUPPORTED EFI_STATUS(3) 39 #define EFI_BAD_BUFFER_SIZE EFI_STATUS(4) 40 #define EFI_BUFFER_TOO_SMALL EFI_STATUS(5) 41 #define EFI_NOT_READY EFI_STATUS(6) 42 #define EFI_DEVICE_ERROR EFI_STATUS(7) 43 #define EFI_WRITE_PROTECTED EFI_STATUS(8) 44 #define EFI_OUT_OF_RESOURCES EFI_STATUS(9) 45 #define EFI_VOLUME_CORRUPTED EFI_STATUS(10) 46 #define EFI_VOLUME_FULL EFI_STATUS(11) 47 #define EFI_NO_MEDIA EFI_STATUS(12) 48 #define EFI_MEDIA_CHANGED EFI_STATUS(13) 49 #define EFI_NOT_FOUND EFI_STATUS(14) 50 #define EFI_ACCESS_DENIED EFI_STATUS(15) 51 #define EFI_NO_RESPONSE EFI_STATUS(16) 52 #define EFI_NO_MAPPING EFI_STATUS(17) 53 #define EFI_TIMEOUT EFI_STATUS(18) 54 #define EFI_NOT_STARTED EFI_STATUS(19) 55 #define EFI_ALREADY_STARTED EFI_STATUS(20) 56 #define EFI_ABORTED EFI_STATUS(21) 57 #define EFI_ICMP_ERROR EFI_STATUS(22) 58 #define EFI_TFTP_ERROR EFI_STATUS(23) 59 #define EFI_PROTOCOL_ERROR EFI_STATUS(24) 60 #define EFI_INCOMPATIBLE_VERSION EFI_STATUS(25) 61 #define EFI_SECURITY_VIOLATION EFI_STATUS(26) 62 #define EFI_CRC_ERROR EFI_STATUS(27) 63 #define EFI_END_OF_MEDIA EFI_STATUS(28) 64 #define EFI_END_OF_FILE EFI_STATUS(31) 65 #define EFI_INVALID_LANGUAGE EFI_STATUS(32) 66 #define EFI_COMPROMISED_DATA EFI_STATUS(33) 67 #define EFI_IP_ADDRESS_CONFLICT EFI_STATUS(34) 68 #define EFI_HTTP_ERROR EFI_STATUS(35) 69 70 typedef struct { 71 union { 72 struct { 73 uint32_t Data1; 74 uint16_t Data2; 75 uint16_t Data3; 76 uint8_t Data4[8]; 77 }; 78 79 /* Easier for comparison */ 80 struct { 81 uint64_t Part1; 82 uint64_t Part2; 83 }; 84 }; 85 } efi_guid_t; 86 87 struct efi_input_key { 88 uint16_t ScanCode; 89 uint16_t UnicodeChar; 90 }; 91 92 struct efi_table_header { 93 uint64_t Signature; 94 uint32_t Revision; 95 uint32_t HeaderSize; 96 uint32_t CRC32; 97 uint32_t Reserved; 98 }; 99 100 struct efi_simple_text_input; 101 102 typedef efi_status_t __abi (*efi_input_reset_t)( 103 struct efi_simple_text_input *This, 104 bool ExtendedVerification); 105 typedef efi_status_t __abi (*efi_input_read_key_t)( 106 struct efi_simple_text_input *This, 107 struct efi_input_key *Key); 108 109 struct efi_simple_text_input { 110 efi_input_reset_t Reset; 111 efi_input_read_key_t ReadKeyStroke; 112 void *WaitForKey; 113 }; 114 115 struct efi_simple_text_output_mode { 116 int32_t MaxMode; 117 int32_t Mode; 118 int32_t Attribute; 119 int32_t CursorColumn; 120 int32_t CursorRow; 121 bool CursorVisible; 122 }; 123 124 struct efi_simple_text_output; 125 126 typedef efi_status_t __abi (*efi_text_reset_t)( 127 struct efi_simple_text_output *This, 128 bool ExtendedVerification); 129 130 typedef efi_status_t __abi (*efi_text_string_t)( 131 struct efi_simple_text_output *This, 132 uint16_t *String); 133 134 typedef efi_status_t __abi (*efi_text_test_string_t)( 135 struct efi_simple_text_output *This, 136 uint16_t *String); 137 138 typedef efi_status_t __abi (*efi_text_query_mode_t)( 139 struct efi_simple_text_output *This, 140 uintptr_t ModeNumber, 141 uintptr_t *Columns, 142 uintptr_t *Rows); 143 144 typedef efi_status_t __abi (*efi_text_set_mode_t)( 145 struct efi_simple_text_output *This, 146 uintptr_t ModeNumber); 147 148 typedef efi_status_t __abi (*efi_text_set_attribute_t)( 149 struct efi_simple_text_output *This, 150 uintptr_t Attribute); 151 152 typedef efi_status_t __abi (*efi_text_clear_screen_t)( 153 struct efi_simple_text_output *This); 154 155 typedef efi_status_t __abi (*efi_text_cursor_position_t)( 156 struct efi_simple_text_output *This, 157 uintptr_t Column, 158 uintptr_t Row); 159 160 typedef efi_status_t __abi (*efi_text_enable_cursor_t)( 161 struct efi_simple_text_output *This, 162 bool Visible); 163 164 struct efi_simple_text_output { 165 efi_text_reset_t Reset; 166 efi_text_string_t OutputString; 167 efi_text_test_string_t TestString; 168 efi_text_query_mode_t QueryMode; 169 efi_text_set_mode_t SetMode; 170 efi_text_set_attribute_t SetAttribute; 171 efi_text_clear_screen_t ClearScreen; 172 efi_text_cursor_position_t SetCursorPosition; 173 efi_text_enable_cursor_t EnableCursor; 174 struct efi_simple_text_output_mode *Mode; 175 }; 176 177 struct efi_time { 178 uint16_t Year; 179 uint8_t Month; 180 uint8_t Day; 181 uint8_t Hour; 182 uint8_t Minute; 183 uint8_t Second; 184 uint8_t Pad1; 185 uint32_t NanoSecond; 186 int16_t TimeZone; 187 uint8_t DayLight; 188 uint8_t Pad2; 189 }; 190 191 struct efi_time_capabilities { 192 uint32_t Resolution; 193 uint32_t Accuracy; 194 bool SetsToZero; 195 }; 196 197 struct efi_memory_descriptor { 198 uint32_t Type; 199 uint64_t PhysicalStart; 200 uint64_t VirtualStart; 201 uint64_t NumberOfPages; 202 uint64_t Attribute; 203 }; 204 205 typedef efi_status_t __abi (*efi_get_time_t)( 206 struct efi_time *Time, 207 struct efi_time_capabilities *Capabilities); 208 209 typedef efi_status_t __abi (*efi_set_time_t)(struct efi_time *Time); 210 211 typedef efi_status_t __abi (*efi_get_wakeup_time_t)(bool *Enabled, 212 bool *Pending, 213 struct efi_time *Time); 214 215 typedef efi_status_t __abi (*efi_set_wakeup_time_t)(bool Enabled, 216 struct efi_time *Time); 217 218 typedef efi_status_t __abi (*efi_set_virtual_address_map_t)( 219 uintptr_t MemoryMapSize, 220 uintptr_t DescriptorSize, 221 uint32_t DescriptorVersion, 222 struct efi_memory_descriptor *VirtualMap); 223 224 typedef efi_status_t __abi (*efi_convert_pointer_t)(uintptr_t DebugDisposition, 225 void **Address); 226 227 typedef efi_status_t __abi (*efi_get_variable_t)(uint16_t *VariableName, 228 efi_guid_t *VendorGuid, 229 uint32_t *Attributes, 230 uintptr_t *DataSize, 231 void *Data); 232 233 typedef efi_status_t __abi (*efi_get_next_variable_name_t)( 234 uintptr_t *VariableNameSize, 235 uint16_t *VariableName, 236 efi_guid_t *VendorGuid); 237 238 typedef efi_status_t __abi (*efi_set_variable_t)(uint16_t *VariableName, 239 efi_guid_t *VendorGuid, 240 uint32_t *Attributes, 241 uintptr_t *DataSize, 242 void *Data); 243 244 typedef efi_status_t __abi (*efi_get_next_high_monotonic_count_t)( 245 uint32_t *HighCount); 246 247 enum efi_reset_type { 248 EfiResetCold, 249 EfiResetWarm, 250 EfiResetShutdown, 251 EfiResetPlatformSpecific 252 }; 253 254 typedef efi_status_t __abi (*efi_reset_system_t)( 255 enum efi_reset_type ResetType, 256 uintptr_t ResetStatus, 257 uintptr_t DataSize, 258 void *ResetData); 259 260 struct efi_capsule_header { 261 efi_guid_t CapsuleGuid; 262 uint32_t HeaderSize; 263 uint32_t Flags; 264 uint32_t CapsuleImageSize; 265 }; 266 267 typedef efi_status_t __abi (*efi_update_capsule_t)( 268 struct efi_capsule_header **CapsuleHeaderArray, 269 uintptr_t CapsuleCount, 270 uint64_t ScatterGatherList); 271 272 typedef efi_status_t __abi (*efi_query_capsule_capabilities_t)( 273 struct efi_capsule_header **CapsuleHeaderArray, 274 uintptr_t CapsuleCount, 275 uint64_t *MaximumCapsuleSize, 276 enum efi_reset_type ResetType); 277 278 typedef efi_status_t __abi (*efi_query_variable_info_t)( 279 uint32_t Attributes, 280 uint64_t *MaximumVariableStorageSize, 281 uint64_t *RemainingVariableStorageSize, 282 uint64_t *MaximumVariableSize); 283 284 struct efi_runtime_services { 285 struct efi_table_header Hdr; 286 efi_get_time_t GetTime; 287 efi_set_time_t SetTime; 288 efi_get_wakeup_time_t GetWakeupTime; 289 efi_set_wakeup_time_t SetWakeupTime; 290 efi_set_virtual_address_map_t SetVirtualAddressMap; 291 efi_convert_pointer_t ConvertPointer; 292 efi_get_variable_t GetVariable; 293 efi_get_next_variable_name_t GetNextVariableName; 294 efi_set_variable_t SetVariable; 295 efi_get_next_high_monotonic_count_t GetNextHighMonotonicCount; 296 efi_reset_system_t ResetSystem; 297 efi_update_capsule_t UpdateCapsule; 298 efi_query_capsule_capabilities_t QueryCapsuleCapabilities; 299 efi_query_variable_info_t QueryVariableInfo; 300 }; 301 302 typedef uintptr_t __abi (*efi_raise_tpl_t)(uintptr_t NewTpl); 303 304 typedef void __abi (*efi_restore_tpl_t)(uintptr_t OldTpl); 305 306 enum efi_allocate_type { 307 AllocateAnyPages, 308 AllocateMaxAddress, 309 AllocateAddress, 310 MaxAllocateType 311 }; 312 313 enum efi_memory_type { 314 EfiReservedMemoryType, 315 EfiLoaderCode, 316 EfiLoaderData, 317 EfiBootServicesCode, 318 EfiBootServicesData, 319 EfiRuntimeServicesCode, 320 EfiRuntimeServicesData, 321 EfiConventionalMemory, 322 EfiUnusableMemory, 323 EfiACPIReclaimMemory, 324 EfiACPIMemoryNVS, 325 EfiMemoryMappedIO, 326 EfiMemoryMappedIOPortSpace, 327 EfiPalCode, 328 EfiPersistentMemory, 329 EfiMaxMemoryType 330 }; 331 332 typedef efi_status_t __abi (*efi_allocate_pages_t)( 333 enum efi_allocate_type Type, 334 enum efi_memory_type MemoryType, 335 uintptr_t Pages, 336 uint64_t *Memory); 337 338 typedef efi_status_t __abi (*efi_free_pages_t)(uint64_t Memory, 339 uintptr_t Pages); 340 341 typedef efi_status_t __abi (*efi_get_memory_map_t)( 342 uintptr_t *MemoryMapSize, 343 struct efi_memory_descriptor *MemoryMap, 344 uintptr_t *MapKey, 345 uintptr_t *DescriptorSize, 346 uint32_t *DescriptorVersion); 347 348 typedef efi_status_t __abi (*efi_allocate_pool_t)( 349 enum efi_memory_type PoolType, 350 uintptr_t Size, 351 void **Buffer); 352 353 typedef efi_status_t __abi (*efi_free_pool_t)(void *Buffer); 354 355 typedef void __abi (*efi_notify_function_t)(void *Event, 356 void *context); 357 358 typedef efi_status_t __abi (*efi_create_event_t)( 359 uint32_t Type, 360 uintptr_t NotifyTpl, 361 efi_notify_function_t NotifyFunction, 362 void *NotifyContext, 363 void **Event); 364 365 enum efi_timer_delay { 366 TimerCancel, 367 TimerPeriodic, 368 TimerRelative 369 }; 370 371 typedef efi_status_t __abi (*efi_set_timer_t)(void *Event, 372 enum efi_timer_delay Type, 373 uint64_t TriggerTime); 374 375 typedef efi_status_t __abi (*efi_wait_for_event_t)(uintptr_t NumberOfEvents, 376 void **Event, 377 uintptr_t *Index); 378 379 typedef efi_status_t __abi (*efi_signal_event_t)(void *Event); 380 381 typedef efi_status_t __abi (*efi_close_event_t)(void *Event); 382 383 typedef efi_status_t __abi (*efi_check_event_t)(void *Event); 384 385 enum efi_interface_type { 386 EFI_NATIVE_INTERFACE 387 }; 388 389 typedef efi_status_t __abi (*efi_install_protocol_interface_t)( 390 void **Handle, 391 efi_guid_t *Protocol, 392 enum efi_interface_type InterfaceType, 393 void *Interface); 394 395 typedef efi_status_t __abi (*efi_reinstall_protocol_interface_t)( 396 void **Handle, 397 efi_guid_t *Protocol, 398 void *OldInterface, 399 void *NewInterface); 400 401 typedef efi_status_t __abi (*efi_uninstall_protocol_interface_t)( 402 void **Handle, 403 efi_guid_t *Protocol, 404 void *Interface); 405 406 typedef efi_status_t __abi (*efi_handle_protocol_t)( 407 void **Handle, 408 efi_guid_t *Protocol, 409 void **Interface); 410 411 typedef efi_status_t __abi (*efi_register_protocol_notify_t)( 412 efi_guid_t *Protocol, 413 void *Event, 414 void **Registration); 415 416 enum efi_locate_search_type { 417 AllHandles, 418 ByRegisterNotify, 419 ByProtocol 420 }; 421 422 typedef efi_status_t __abi (*efi_locate_handle_t)( 423 enum efi_locate_search_type SearchType, 424 efi_guid_t *Protocol, 425 void *SearchKey, 426 uintptr_t *BufferSize, 427 void **Buffer); 428 429 struct efi_device_path_protocol { 430 uint8_t Type; 431 uint8_t SubType; 432 uint8_t Length[2]; 433 }; 434 435 typedef efi_status_t __abi (*efi_locate_device_path_t)( 436 efi_guid_t *Protocol, 437 struct efi_device_path_protocol **DevicePath, 438 void **Handle); 439 440 typedef efi_status_t __abi (*efi_install_configuration_table_t)( 441 efi_guid_t *Guid, 442 void *Table); 443 444 typedef efi_status_t __abi (*efi_load_image_t)( 445 bool BootPolicy, 446 void *ParentImageHandle, 447 struct efi_device_path_protocol *DevicePath, 448 void *SourceBuffer, 449 uintptr_t SourceSize, 450 void **ImageHandle); 451 452 typedef efi_status_t __abi (*efi_start_image_t)(void *ImageHandle, 453 uintptr_t *ExitDataSize, 454 uint16_t **ExitData); 455 456 typedef efi_status_t __abi (*efi_exit_t)(void *ImageHandle, 457 uintptr_t ExitStatus, 458 uintptr_t ExitDataSize, 459 uint16_t *ExitData); 460 461 typedef efi_status_t __abi (*efi_unload_image_t)(void *ImageHandle); 462 463 typedef efi_status_t __abi (*efi_exit_boot_services_t)(void *ImageHandle, 464 uintptr_t MapKey); 465 466 typedef efi_status_t __abi (*efi_get_next_monotonic_count_t)(uint64_t *Count); 467 468 typedef efi_status_t __abi (*efi_stall_t)(uintptr_t Microseconds); 469 470 typedef efi_status_t __abi (*efi_set_watchdog_timer_t)(uintptr_t Timeout, 471 uint64_t WatchdogCode, 472 uintptr_t DataSize, 473 uint16_t *WatchdogData); 474 475 typedef efi_status_t __abi (*efi_connect_controller_t)( 476 void *ControllerHandle, 477 void **DriverImageHandle, 478 struct efi_device_path_protocol *RemainingDevicePath, 479 bool Recursive); 480 481 typedef efi_status_t __abi (*efi_disconnect_controller_t)( 482 void *ControllerHandle, 483 void *DriverImageHandle, 484 void *ChildHandle); 485 486 typedef efi_status_t __abi (*efi_open_protocol_t)(void *Handle, 487 efi_guid_t *Protocol, 488 void **Interface, 489 void *AgentHandle, 490 void *ControllerHandle, 491 uint32_t Attributes); 492 493 typedef efi_status_t __abi (*efi_close_protocol_t)(void *Handle, 494 efi_guid_t *Protocol, 495 void *AgentHandle, 496 void *ControllerHandle); 497 498 struct efi_open_protocol_information_entry { 499 void *AgentHandle; 500 void *ControllerHandle; 501 uint32_t Attributes; 502 uint32_t OpenCount; 503 }; 504 505 typedef efi_status_t __abi (*efi_open_protocol_information_t)( 506 void *Handle, 507 efi_guid_t *Protocol, 508 struct efi_open_protocol_information_entry **EntryBuffer, 509 uintptr_t *EntryCount); 510 511 typedef efi_status_t __abi (*efi_protocols_per_handle_t)( 512 void *Handle, 513 efi_guid_t ***ProtocolBuffer, 514 uintptr_t *ProtocolBufferCount); 515 516 typedef efi_status_t __abi (*efi_locate_handle_buffer_t)( 517 enum efi_locate_search_type SearchType, 518 efi_guid_t *Protocol, 519 void *SearchKey, 520 uintptr_t *NoHandles, 521 void ***Buffer); 522 523 typedef efi_status_t __abi (*efi_locate_protocol_t)(efi_guid_t *Protocol, 524 void *Registration, 525 void **Interface); 526 527 typedef efi_status_t __abi (*efi_multiple_protocol_interface_t)( 528 void *Handle, ...); 529 530 typedef efi_status_t __abi (*efi_calculate_crc32_t)(void *Data, 531 uintptr_t DataSize, 532 uint32_t CRC32); 533 534 typedef efi_status_t __abi (*efi_copy_mem_t)(void *Destination, 535 void *Source, 536 uintptr_t Size); 537 538 typedef efi_status_t __abi (*efi_set_mem_t)(void *Buffer, 539 uintptr_t Size, 540 uint8_t Value); 541 542 typedef efi_status_t __abi (*efi_create_event_ex_t)( 543 uint32_t Type, 544 uintptr_t NotifyTpl, 545 efi_notify_function_t NotifyFunction, 546 const void *NotifyContext, 547 const efi_guid_t *EventGroup, 548 void **Event); 549 550 struct efi_boot_services { 551 struct efi_table_header Hdr; 552 efi_raise_tpl_t RaiseTPL; 553 efi_restore_tpl_t RestoreTPL; 554 efi_allocate_pages_t AllocatePages; 555 efi_free_pages_t FreePages; 556 efi_get_memory_map_t GetMemoryMap; 557 efi_allocate_pool_t AllocatePool; 558 efi_free_pool_t FreePool; 559 efi_create_event_t CreateEvent; 560 efi_set_timer_t SetTimer; 561 efi_wait_for_event_t WaitForEvent; 562 efi_signal_event_t SignalEvent; 563 efi_close_event_t CloseEvent; 564 efi_check_event_t CheckEvent; 565 efi_install_protocol_interface_t InstallProtocolInterface; 566 efi_reinstall_protocol_interface_t ReinstallProtocolInterface; 567 efi_uninstall_protocol_interface_t UninstallProtocolInterface; 568 efi_handle_protocol_t HandleProtocol; 569 efi_register_protocol_notify_t RegisterProtocolNotify; 570 efi_locate_handle_t LocateHandle; 571 efi_locate_device_path_t LocateDevicePath; 572 efi_install_configuration_table_t InstallConfigurationTable; 573 efi_load_image_t LoadImage; 574 efi_start_image_t StartImage; 575 efi_exit_t Exit; 576 efi_unload_image_t UnloadImage; 577 efi_exit_boot_services_t ExitBootServices; 578 efi_get_next_monotonic_count_t GetNextMonotonicCount; 579 efi_stall_t Stall; 580 efi_set_watchdog_timer_t SetWatchdogTimer; 581 efi_connect_controller_t ConnectController; 582 efi_disconnect_controller_t DisconnectController; 583 efi_open_protocol_t OpenProtocol; 584 efi_close_protocol_t CloseProtocol; 585 efi_open_protocol_information_t OpenProtocolInformation; 586 efi_protocols_per_handle_t ProtocolsPerHandle; 587 efi_locate_handle_buffer_t LocateHandleBuffer; 588 efi_locate_protocol_t LocateProtocol; 589 efi_multiple_protocol_interface_t InstallMultipleProtocolInterfaces; 590 efi_multiple_protocol_interface_t UninstallMultipleProtocolInterfaces; 591 efi_calculate_crc32_t CalculateCrc32; 592 efi_copy_mem_t CopyMem; 593 efi_set_mem_t SetMem; 594 efi_create_event_ex_t CreateEventEx; 595 }; 596 597 struct efi_configuration_table { 598 /** Vendor EFI GUID Identifier */ 599 efi_guid_t VendorGuid; 600 /** Vendor table pointer */ 601 void *VendorTable; 602 }; 603 604 struct efi_system_table { 605 struct efi_table_header Hdr; 606 uint16_t *FirmwareVendor; 607 uint32_t FirmwareRevision; 608 void *ConsoleInHandle; 609 struct efi_simple_text_input *ConIn; 610 void *ConsoleOutHandle; 611 struct efi_simple_text_output *ConOut; 612 void *StandardErrorHandle; 613 struct efi_simple_text_output *StdErr; 614 struct efi_runtime_services *RuntimeServices; 615 struct efi_boot_services *BootServices; 616 /** The amount of entries to expect in the next attribute */ 617 uint64_t NumberOfTableEntries; 618 /** A pointer to the configuration table(s) */ 619 struct efi_configuration_table *ConfigurationTable; 620 }; 621 622 #ifdef CONFIG_DYNAMIC_BOOTARGS 623 struct efi_loaded_image_protocol { 624 uint32_t Revision; 625 void *ParentHandle; 626 struct efi_system_table *SystemTable; 627 void *DeviceHandle; 628 void *FilePath; 629 void *Reserved; 630 uint32_t LoadOptionsSize; 631 void *LoadOptions; 632 void *ImageBase; 633 uint64_t ImageSize; 634 enum efi_memory_type ImageCodeType; 635 enum efi_memory_type ImageDataType; 636 efi_unload_image_t Unload; 637 }; 638 #endif /* CONFIG_DYNAMIC_BOOTARGS */ 639 640 #endif /* _ASMLANGUAGE */ 641 642 #endif /* ZEPHYR_INCLUDE_ARCH_X86_EFI_H_ */ 643