λ

furix

cute os

settings

include/efi.h

10670 bytes raw

#ifndef FURIX_EFI_H
#define FURIX_EFI_H

#include <types.h>

#if defined(_MSC_VER)
#define EFIAPI __cdecl
#elif defined(__GNUC__) || defined(__clang__)
#define EFIAPI __attribute__((ms_abi))
#else
#define EFIAPI
#endif

typedef uint64_t EFI_STATUS;
typedef uint64_t UINTN;
typedef uint16_t CHAR16;
typedef uint8_t BOOLEAN;
typedef void *EFI_HANDLE;
typedef void *EFI_EVENT;

#define EFI_SUCCESS ((EFI_STATUS)0)
#define EFI_ERROR_MASK ((EFI_STATUS)1 << 63)
#define EFIERR(status) (((status) & EFI_ERROR_MASK) != 0)

/* the status codes the kernel actually inspects by value. */
#define EFI_BUFFER_TOO_SMALL ((EFI_STATUS)(EFI_ERROR_MASK | 5))

typedef struct {
  uint32_t Data1;
  uint16_t Data2;
  uint16_t Data3;
  uint8_t Data4[8];
} EFI_GUID;

typedef struct {
  EFI_GUID VendorGuid;
  void *VendorTable;
} EFI_CONFIGURATION_TABLE;

typedef struct {
  uint64_t Signature;
  uint32_t Revision;
  uint32_t HeaderSize;
  uint32_t CRC32;
  uint32_t Reserved;
} EFI_TABLE_HEADER;

typedef uint64_t EFI_PHYSICAL_ADDRESS;
typedef uint64_t EFI_VIRTUAL_ADDRESS;
typedef UINTN EFI_TPL;

typedef enum {
  AllocateAnyPages,
  AllocateMaxAddress,
  AllocateAddress,
  MaxAllocateType
} EFI_ALLOCATE_TYPE;

typedef enum {
  EfiReservedMemoryType,
  EfiLoaderCode,
  EfiLoaderData,
  EfiBootServicesCode,
  EfiBootServicesData,
  EfiRuntimeServicesCode,
  EfiRuntimeServicesData,
  EfiConventionalMemory,
  EfiUnusableMemory,
  EfiACPIReclaimMemory,
  EfiACPIMemoryNVM,
  EfiMemoryMappedIO,
  EfiMemoryMappedIOPortSpace,
  EfiPalCode,
  EfiPersistentMemory,
  EfiMaxMemoryType
} EFI_MEMORY_TYPE;

/*
 * NOT packed, and that is deliberate. the UEFI layout puts PhysicalStart at
 * offset 8, which needs 4 bytes of padding after the 4-byte Type, so the
 * struct is 40 bytes, and marking it packed would move every following field
 * down by 4 and silently corrupt every read.
 *
 * note that sizeof() here is 40 while EDK2 reports a DescriptorSize of 48: the
 * firmware struct carries an extra union for versioned descriptors. always
 * stride the memory map by the reported DescriptorSize, never by sizeof.
 */
typedef struct {
  uint32_t Type;
  EFI_PHYSICAL_ADDRESS PhysicalStart;
  EFI_VIRTUAL_ADDRESS VirtualStart;
  uint64_t NumberOfPages;
  uint64_t Attribute;
} EFI_MEMORY_DESCRIPTOR;

typedef enum {
  AllHandles,
  ByRegisterNotify,
  ByProtocol
} EFI_LOCATE_SEARCH_TYPE;

typedef enum {
  TimerCancel,
  TimerPeriodic,
  TimerRelative
} EFI_TIMER_DELAY;

typedef struct EFI_DEVICE_PATH_PROTOCOL EFI_DEVICE_PATH_PROTOCOL;

// member order and offsets are part of the UEFI ABI; do not reorder.
// only the entries needed so far are typed precisely, but every member that
// precedes them must still be present so their offsets stay correct.
typedef struct EFI_BOOT_SERVICES EFI_BOOT_SERVICES;
struct EFI_BOOT_SERVICES {
  EFI_TABLE_HEADER Hdr;

  EFI_STATUS (EFIAPI *RaiseTPL)(EFI_TPL NewTpl);
  void (EFIAPI *RestoreTPL)(EFI_TPL OldTpl);

  EFI_STATUS (EFIAPI *AllocatePages)(EFI_ALLOCATE_TYPE Type, EFI_MEMORY_TYPE MemoryType, UINTN Pages, EFI_PHYSICAL_ADDRESS *Memory);
  EFI_STATUS (EFIAPI *FreePages)(EFI_PHYSICAL_ADDRESS Memory, UINTN Pages);
  EFI_STATUS (EFIAPI *GetMemoryMap)(UINTN *MemoryMapSize, EFI_MEMORY_DESCRIPTOR *MemoryMap, UINTN *MapKey, UINTN *DescriptorSize, uint32_t *DescriptorVersion);
  EFI_STATUS (EFIAPI *AllocatePool)(EFI_MEMORY_TYPE PoolType, UINTN Size, void **Buffer);
  EFI_STATUS (EFIAPI *FreePool)(void *Buffer);

  EFI_STATUS (EFIAPI *CreateEvent)(uint32_t Type, EFI_TPL NotifyTpl, void (EFIAPI *NotifyFunction)(EFI_EVENT Event, void *Context), void *NotifyContext, EFI_EVENT *Event);
  EFI_STATUS (EFIAPI *SetTimer)(EFI_EVENT Event, EFI_TIMER_DELAY Type, uint64_t TriggerTime);
  EFI_STATUS (EFIAPI *WaitForEvent)(UINTN NumberOfEvents, EFI_EVENT *Event, UINTN *Index);
  EFI_STATUS (EFIAPI *SignalEvent)(EFI_EVENT Event);
  EFI_STATUS (EFIAPI *CloseEvent)(EFI_EVENT Event);
  EFI_STATUS (EFIAPI *CheckEvent)(EFI_EVENT Event);

  EFI_STATUS (EFIAPI *InstallProtocolInterface)(EFI_HANDLE *Handle, EFI_GUID *Protocol, uint32_t InterfaceType, void *Interface);
  EFI_STATUS (EFIAPI *ReinstallProtocolInterface)(EFI_HANDLE Handle, EFI_GUID *Protocol, void *OldInterface, void *NewInterface);
  EFI_STATUS (EFIAPI *UninstallProtocolInterface)(EFI_HANDLE Handle, EFI_GUID *Protocol, void *Interface);
  EFI_STATUS (EFIAPI *HandleProtocol)(EFI_HANDLE Handle, EFI_GUID *Protocol, void **Interface);
  void *Reserved;
  EFI_STATUS (EFIAPI *RegisterProtocolNotify)(EFI_GUID *Protocol, void (EFIAPI *NotifyFunction)(EFI_EVENT Event, void *Context), EFI_EVENT *Registration);
  EFI_STATUS (EFIAPI *LocateHandle)(EFI_LOCATE_SEARCH_TYPE SearchType, EFI_GUID *Protocol, void *SearchKey, UINTN *BufferSize, EFI_HANDLE *Buffer);
  EFI_STATUS (EFIAPI *LocateDevicePath)(EFI_GUID *Protocol, EFI_DEVICE_PATH_PROTOCOL **DevicePath, EFI_HANDLE *Device);
  EFI_STATUS (EFIAPI *InstallConfigurationTable)(EFI_GUID *Guid, void *Table);

  EFI_STATUS (EFIAPI *LoadImage)(BOOLEAN BootPolicy, EFI_HANDLE ParentImageHandle, EFI_DEVICE_PATH_PROTOCOL *DevicePath, void *SourceBuffer, UINTN SourceSize, EFI_HANDLE *ImageHandle);
  EFI_STATUS (EFIAPI *StartImage)(EFI_HANDLE ImageHandle, UINTN *ExitDataSize, CHAR16 **ExitData);
  EFI_STATUS (EFIAPI *Exit)(EFI_HANDLE ImageHandle, EFI_STATUS ExitStatus, UINTN ExitDataSize, CHAR16 *ExitData);
  EFI_STATUS (EFIAPI *UnloadImage)(EFI_HANDLE ImageHandle);
  EFI_STATUS (EFIAPI *ExitBootServices)(EFI_HANDLE ImageHandle, UINTN MapKey);
  EFI_STATUS (EFIAPI *GetNextMonotonicCount)(uint64_t *Count);
  EFI_STATUS (EFIAPI *Stall)(UINTN Microseconds);
  EFI_STATUS (EFIAPI *SetWatchdogTimer)(UINTN Timeout, uint64_t WatchdogCode, UINTN DataSize, CHAR16 *WatchdogData);

  EFI_STATUS (EFIAPI *ConnectController)(EFI_HANDLE ControllerHandle, EFI_HANDLE *DriverImageHandle, EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath, BOOLEAN Recursive);
  EFI_STATUS (EFIAPI *DisconnectController)(EFI_HANDLE ControllerHandle, EFI_HANDLE DriverImageHandle, EFI_HANDLE ChildHandle);
  EFI_STATUS (EFIAPI *OpenProtocol)(EFI_HANDLE Handle, EFI_GUID *Protocol, void **Interface, EFI_HANDLE AgentHandle, EFI_HANDLE ControllerHandle, uint32_t Attributes);
  EFI_STATUS (EFIAPI *CloseProtocol)(EFI_HANDLE Handle, EFI_GUID *Protocol, EFI_HANDLE AgentHandle, EFI_HANDLE ControllerHandle);
  EFI_STATUS (EFIAPI *OpenProtocolInformation)(EFI_HANDLE Handle, EFI_GUID *Protocol, void **EntryBuffer, UINTN *EntryCount);
  EFI_STATUS (EFIAPI *ProtocolsPerHandle)(EFI_HANDLE Handle, EFI_GUID ***ProtocolBuffer, UINTN *ProtocolBufferCount);
  EFI_STATUS (EFIAPI *LocateHandleBuffer)(EFI_LOCATE_SEARCH_TYPE SearchType, EFI_GUID *Protocol, void *SearchKey, UINTN *NoHandles, EFI_HANDLE **Buffer);
  EFI_STATUS (EFIAPI *LocateProtocol)(EFI_GUID *Protocol, void *Registration, void **Interface);
  EFI_STATUS (EFIAPI *InstallMultipleProtocolInterfaces)(EFI_HANDLE *Handle, ...);
  EFI_STATUS (EFIAPI *UninstallMultipleProtocolInterfaces)(EFI_HANDLE Handle, ...);

  EFI_STATUS (EFIAPI *CalculateCrc32)(void *Data, UINTN DataSize, uint32_t *Crc32);
  void (EFIAPI *CopyMem)(void *Destination, void *Source, UINTN Length);
  void (EFIAPI *SetMem)(void *Buffer, UINTN Size, uint8_t Value);
  EFI_STATUS (EFIAPI *CreateEventEx)(uint32_t Type, EFI_TPL NotifyTpl, void (EFIAPI *NotifyFunction)(EFI_EVENT Event, void *Context), const void *NotifyContext, const EFI_GUID *EventGroup, EFI_EVENT *Event);
};

struct EFI_RUNTIME_SERVICES;

typedef struct {
  uint16_t ScanCode;
  CHAR16 UnicodeChar;
} EFI_INPUT_KEY;

typedef struct EFI_SIMPLE_TEXT_INPUT_PROTOCOL EFI_SIMPLE_TEXT_INPUT_PROTOCOL;
struct EFI_SIMPLE_TEXT_INPUT_PROTOCOL {
  EFI_STATUS (EFIAPI *Reset)(EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This, BOOLEAN ExtendedVerification);
  EFI_STATUS (EFIAPI *ReadKeyStroke)(EFI_SIMPLE_TEXT_INPUT_PROTOCOL *This, EFI_INPUT_KEY *Key);
  EFI_EVENT WaitForKey;
};

typedef struct {
  int32_t CursorColumn;
  int32_t CursorRow;
  BOOLEAN CursorVisible;
} EFI_SIMPLE_TEXT_OUTPUT_MODE;

typedef struct EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL;
struct EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL {
  EFI_STATUS (EFIAPI *Reset)(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, BOOLEAN ExtendedVerification);
  EFI_STATUS (EFIAPI *OutputString)(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, CHAR16 *String);
  EFI_STATUS (EFIAPI *TestString)(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, CHAR16 *String);
  EFI_STATUS (EFIAPI *QueryMode)(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, uint64_t ModeNumber, uint64_t *Columns, uint64_t *Rows);
  EFI_STATUS (EFIAPI *SetMode)(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, uint64_t ModeNumber);
  EFI_STATUS (EFIAPI *SetAttribute)(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, uint64_t Attribute);
  EFI_STATUS (EFIAPI *ClearScreen)(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This);
  EFI_STATUS (EFIAPI *SetCursorPosition)(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, uint64_t Column, uint64_t Row);
  EFI_STATUS (EFIAPI *EnableCursor)(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *This, BOOLEAN Visible);
  EFI_SIMPLE_TEXT_OUTPUT_MODE *Mode;
};

typedef struct EFI_SYSTEM_TABLE EFI_SYSTEM_TABLE;
struct EFI_SYSTEM_TABLE {
  EFI_TABLE_HEADER Hdr;
  CHAR16 *FirmwareVendor;
  uint32_t FirmwareRevision;
  EFI_HANDLE ConsoleInHandle;
  EFI_SIMPLE_TEXT_INPUT_PROTOCOL *ConIn;
  EFI_HANDLE ConsoleOutHandle;
  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ConOut;
  EFI_HANDLE StandardErrorHandle;
  EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *StdErr;
  struct EFI_RUNTIME_SERVICES *RuntimeServices;
  struct EFI_BOOT_SERVICES *BootServices;
  UINTN NumberOfTableEntries;
  EFI_CONFIGURATION_TABLE *ConfigurationTable;
};

typedef char efi_assert_sizeof_guid[(sizeof(EFI_GUID) == 16) ? 1 : -1];
typedef char efi_assert_sizeof_header[(sizeof(EFI_TABLE_HEADER) == 24) ? 1 : -1];
typedef char efi_assert_sizeof_system_table[(sizeof(EFI_SYSTEM_TABLE) == 120) ? 1 : -1];

typedef char efi_assert_sizeof_boot_services[(sizeof(struct EFI_BOOT_SERVICES) == 376) ? 1 : -1];
typedef char efi_assert_offset_get_memory_map[(offsetof(struct EFI_BOOT_SERVICES, GetMemoryMap) == 56) ? 1 : -1];

/* EFI_MEMORY_DESCRIPTOR layout is part of the UEFI ABI; see the struct comment. */
typedef char efi_assert_sizeof_memory_descriptor[(sizeof(EFI_MEMORY_DESCRIPTOR) == 40) ? 1 : -1];
typedef char efi_assert_offset_memory_start[(offsetof(EFI_MEMORY_DESCRIPTOR, PhysicalStart) == 8) ? 1 : -1];
typedef char efi_assert_offset_memory_pages[(offsetof(EFI_MEMORY_DESCRIPTOR, NumberOfPages) == 24) ? 1 : -1];
typedef char efi_assert_offset_memory_attr[(offsetof(EFI_MEMORY_DESCRIPTOR, Attribute) == 32) ? 1 : -1];
typedef char efi_assert_offset_exit_boot_services[(offsetof(struct EFI_BOOT_SERVICES, ExitBootServices) == 232) ? 1 : -1];


#endif /* FURIX_EFI_H */