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