interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / boot / efi.h

273 lines · 12571 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* efi.h -- the slice of UEFI the AIENOS C boot stub uses: system table,
18 * boot services it calls (GetMemoryMap, ExitBootServices, SetWatchdogTimer,
19 * AllocatePages, FreePages, FreePool, HandleProtocol, LocateHandleBuffer),
20 * the Loaded Image, Simple File System and Block I/O protocols (model ingest,
21 * efi_model.c) and the Graphics Output Protocol (framebuffer, efi_gop.c). Field orders and offsets follow UEFI 2.10 (edk2
22 * MdePkg/Include/Uefi/UefiSpec.h, Protocol/BlockIo.h, SimpleFileSystem.h,
23 * LoadedImage.h); _Static_asserts below pin the offsets this stub relies on.
24 * Everything not used is a void pointer placeholder of the right size. */
25#ifndef AIENOS_CK_EFI_H
26#define AIENOS_CK_EFI_H
27#include <stdint.h>
28#include <stddef.h>
29
30typedef uint16_t CHAR16;
31typedef uint64_t EFI_STATUS;
32typedef void *EFI_HANDLE;
33
34#define EFI_SUCCESS 0ull
35#define EFI_ERR(n) (0x8000000000000000ull | (n))
36#define EFI_INVALID_PARAMETER EFI_ERR(2)
37#define EFI_BUFFER_TOO_SMALL EFI_ERR(5)
38#define EFI_DEVICE_ERROR EFI_ERR(7)
39#define EFI_OUT_OF_RESOURCES EFI_ERR(9)
40#define EFI_NOT_FOUND EFI_ERR(14)
41
42typedef struct {
43 uint32_t d1;
44 uint16_t d2, d3;
45 uint8_t d4[8];
46} EFI_GUID;
47
48typedef struct {
49 uint64_t Signature;
50 uint32_t Revision, HeaderSize, CRC32, Reserved;
51} EFI_TABLE_HEADER;
52
53typedef struct EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL {
54 EFI_STATUS (*Reset)(struct EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *, uint8_t);
55 EFI_STATUS (*OutputString)(struct EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *, CHAR16 *);
56} EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL;
57
58/* Memory types (UEFI 2.10 table 7.10). */
59enum {
60 EfiReservedMemoryType, EfiLoaderCode, EfiLoaderData, EfiBootServicesCode,
61 EfiBootServicesData, EfiRuntimeServicesCode, EfiRuntimeServicesData,
62 EfiConventionalMemory, EfiUnusableMemory, EfiACPIReclaimMemory,
63 EfiACPIMemoryNVS, EfiMemoryMappedIO, EfiMemoryMappedIOPortSpace,
64 EfiPalCode, EfiPersistentMemory, EfiUnacceptedMemoryType
65};
66#define EFI_MEMORY_WB 0x8ull
67
68/* EFI_ALLOCATE_TYPE and EFI_LOCATE_SEARCH_TYPE (UefiSpec.h). */
69typedef enum { AllocateAnyPages, AllocateMaxAddress, AllocateAddress, MaxAllocateType } EFI_ALLOCATE_TYPE;
70typedef enum { AllHandles, ByRegisterNotify, ByProtocol } EFI_LOCATE_SEARCH_TYPE;
71
72typedef struct {
73 uint32_t Type;
74 uint32_t Pad;
75 uint64_t PhysicalStart;
76 uint64_t VirtualStart;
77 uint64_t NumberOfPages;
78 uint64_t Attribute;
79} EFI_MEMORY_DESCRIPTOR;
80
81typedef struct {
82 EFI_TABLE_HEADER Hdr;
83 void *RaiseTPL, *RestoreTPL;
84 EFI_STATUS (*AllocatePages)(EFI_ALLOCATE_TYPE Type, uint32_t MemoryType, uint64_t Pages,
85 uint64_t *Memory);
86 EFI_STATUS (*FreePages)(uint64_t Memory, uint64_t Pages);
87 EFI_STATUS (*GetMemoryMap)(uint64_t *MapSize, EFI_MEMORY_DESCRIPTOR *Map,
88 uint64_t *MapKey, uint64_t *DescriptorSize,
89 uint32_t *DescriptorVersion);
90 void *AllocatePool;
91 EFI_STATUS (*FreePool)(void *Buffer);
92 void *CreateEvent, *SetTimer, *WaitForEvent, *SignalEvent, *CloseEvent, *CheckEvent;
93 void *InstallProtocolInterface, *ReinstallProtocolInterface;
94 void *UninstallProtocolInterface;
95 EFI_STATUS (*HandleProtocol)(EFI_HANDLE Handle, const EFI_GUID *Protocol, void **Interface);
96 void *Reserved;
97 void *RegisterProtocolNotify, *LocateHandle, *LocateDevicePath;
98 void *InstallConfigurationTable;
99 void *LoadImage, *StartImage, *Exit, *UnloadImage;
100 EFI_STATUS (*ExitBootServices)(EFI_HANDLE ImageHandle, uint64_t MapKey);
101 void *GetNextMonotonicCount;
102 EFI_STATUS (*Stall)(uint64_t Microseconds);
103 EFI_STATUS (*SetWatchdogTimer)(uint64_t Timeout, uint64_t WatchdogCode,
104 uint64_t DataSize, CHAR16 *WatchdogData);
105 void *ConnectController, *DisconnectController;
106 void *OpenProtocol, *CloseProtocol, *OpenProtocolInformation, *ProtocolsPerHandle;
107 EFI_STATUS (*LocateHandleBuffer)(EFI_LOCATE_SEARCH_TYPE SearchType, const EFI_GUID *Protocol,
108 void *SearchKey, uint64_t *NoHandles, EFI_HANDLE **Buffer);
109 void *LocateProtocol;
110} EFI_BOOT_SERVICES;
111
112typedef struct {
113 EFI_GUID VendorGuid;
114 void *VendorTable;
115} EFI_CONFIGURATION_TABLE;
116
117typedef struct {
118 EFI_TABLE_HEADER Hdr;
119 CHAR16 *FirmwareVendor;
120 uint32_t FirmwareRevision;
121 EFI_HANDLE ConsoleInHandle;
122 void *ConIn;
123 EFI_HANDLE ConsoleOutHandle;
124 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ConOut;
125 EFI_HANDLE StandardErrorHandle;
126 void *StdErr;
127 void *RuntimeServices;
128 EFI_BOOT_SERVICES *BootServices;
129 uint64_t NumberOfTableEntries;
130 EFI_CONFIGURATION_TABLE *ConfigurationTable;
131} EFI_SYSTEM_TABLE;
132
133/* Runtime services: only ResetSystem is typed (UEFI 2.x section 8.5.1). */
134typedef enum { EfiResetCold, EfiResetWarm, EfiResetShutdown, EfiResetPlatformSpecific } EFI_RESET_TYPE;
135typedef struct {
136 EFI_TABLE_HEADER Hdr;
137 void *GetTime, *SetTime, *GetWakeupTime, *SetWakeupTime;
138 void *SetVirtualAddressMap, *ConvertPointer;
139 void *GetVariable, *GetNextVariableName, *SetVariable;
140 void *GetNextHighMonotonicCount;
141 void (*ResetSystem)(EFI_RESET_TYPE ResetType, EFI_STATUS ResetStatus, uint64_t DataSize, void *ResetData);
142} EFI_RUNTIME_SERVICES;
143_Static_assert(offsetof(EFI_RUNTIME_SERVICES, ResetSystem) == 0x68, "ResetSystem");
144
145/* Offsets fixed by the specification (LP64). */
146_Static_assert(offsetof(EFI_BOOT_SERVICES, AllocatePages) == 0x28, "AllocatePages");
147_Static_assert(offsetof(EFI_BOOT_SERVICES, GetMemoryMap) == 0x38, "GetMemoryMap");
148_Static_assert(offsetof(EFI_BOOT_SERVICES, FreePool) == 0x48, "FreePool");
149_Static_assert(offsetof(EFI_BOOT_SERVICES, HandleProtocol) == 0x98, "HandleProtocol");
150_Static_assert(offsetof(EFI_BOOT_SERVICES, ExitBootServices) == 0xe8, "ExitBootServices");
151_Static_assert(offsetof(EFI_BOOT_SERVICES, SetWatchdogTimer) == 0x100, "SetWatchdogTimer");
152_Static_assert(offsetof(EFI_BOOT_SERVICES, LocateHandleBuffer) == 0x138, "LocateHandleBuffer");
153_Static_assert(offsetof(EFI_SYSTEM_TABLE, ConOut) == 0x40, "ConOut");
154_Static_assert(offsetof(EFI_SYSTEM_TABLE, BootServices) == 0x60, "BootServices");
155_Static_assert(offsetof(EFI_SYSTEM_TABLE, ConfigurationTable) == 0x70, "ConfigurationTable");
156_Static_assert(sizeof(EFI_MEMORY_DESCRIPTOR) == 40, "descriptor");
157
158/* EFI_ACPI_20_TABLE_GUID */
159#define EFI_ACPI_20_TABLE_GUID_INIT \
160 { 0x8868e871, 0xe4f1, 0x11d3, { 0xbc, 0x22, 0x00, 0x80, 0xc7, 0x3c, 0x88, 0x81 } }
161
162/* --- Loaded Image (UEFI 2.10 section 9.1) --- */
163#define EFI_LOADED_IMAGE_PROTOCOL_GUID_INIT \
164 { 0x5B1B31A1, 0x9562, 0x11d2, { 0x8E, 0x3F, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B } }
165typedef struct {
166 uint32_t Revision;
167 EFI_HANDLE ParentHandle;
168 EFI_SYSTEM_TABLE *SystemTable;
169 EFI_HANDLE DeviceHandle; /* the device handle that the EFI Image was loaded from */
170 void *FilePath;
171 void *Reserved;
172 uint32_t LoadOptionsSize;
173 void *LoadOptions;
174 void *ImageBase;
175 uint64_t ImageSize;
176 uint32_t ImageCodeType, ImageDataType;
177 void *Unload;
178} EFI_LOADED_IMAGE_PROTOCOL;
179_Static_assert(offsetof(EFI_LOADED_IMAGE_PROTOCOL, DeviceHandle) == 0x18, "DeviceHandle");
180
181/* --- Simple File System + File (UEFI 2.10 section 13.4, 13.5) --- */
182#define EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID_INIT \
183 { 0x964e5b22, 0x6459, 0x11d2, { 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } }
184#define EFI_FILE_MODE_READ 0x0000000000000001ull
185#define EFI_FILE_MODE_WRITE 0x0000000000000002ull
186#define EFI_FILE_MODE_CREATE 0x8000000000000000ull
187#define EFI_FILE_DIRECTORY 0x0000000000000010ull
188typedef struct EFI_FILE_PROTOCOL {
189 uint64_t Revision;
190 EFI_STATUS (*Open)(struct EFI_FILE_PROTOCOL *This, struct EFI_FILE_PROTOCOL **NewHandle,
191 CHAR16 *FileName, uint64_t OpenMode, uint64_t Attributes);
192 EFI_STATUS (*Close)(struct EFI_FILE_PROTOCOL *This);
193 EFI_STATUS (*Delete)(struct EFI_FILE_PROTOCOL *This);
194 EFI_STATUS (*Read)(struct EFI_FILE_PROTOCOL *This, uint64_t *BufferSize, void *Buffer);
195 EFI_STATUS (*Write)(struct EFI_FILE_PROTOCOL *This, uint64_t *BufferSize, const void *Buffer);
196 void *GetPosition, *SetPosition, *GetInfo, *SetInfo;
197 EFI_STATUS (*Flush)(struct EFI_FILE_PROTOCOL *This);
198 void *OpenEx, *ReadEx, *WriteEx, *FlushEx;
199} EFI_FILE_PROTOCOL;
200typedef struct EFI_SIMPLE_FILE_SYSTEM_PROTOCOL {
201 uint64_t Revision;
202 EFI_STATUS (*OpenVolume)(struct EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This, EFI_FILE_PROTOCOL **Root);
203} EFI_SIMPLE_FILE_SYSTEM_PROTOCOL;
204_Static_assert(offsetof(EFI_FILE_PROTOCOL, Read) == 0x20, "File.Read");
205_Static_assert(offsetof(EFI_FILE_PROTOCOL, Write) == 0x28, "File.Write");
206_Static_assert(offsetof(EFI_FILE_PROTOCOL, Flush) == 0x50, "File.Flush");
207_Static_assert(offsetof(EFI_BOOT_SERVICES, Stall) == 0xf8, "Stall");
208
209/* --- Block I/O (UEFI 2.10 section 13.9) --- */
210#define EFI_BLOCK_IO_PROTOCOL_GUID_INIT \
211 { 0x964e5b21, 0x6459, 0x11d2, { 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } }
212typedef struct {
213 uint32_t MediaId;
214 uint8_t RemovableMedia;
215 uint8_t MediaPresent;
216 uint8_t LogicalPartition; /* TRUE if LBA 0 is the first block of a partition */
217 uint8_t ReadOnly;
218 uint8_t WriteCaching;
219 uint32_t BlockSize; /* ReadBlocks BufferSize must be a multiple of this */
220 uint32_t IoAlign; /* alignment requirement for any buffer to read or write */
221 uint64_t LastBlock;
222 uint64_t LowestAlignedLba; /* Revision 2 */
223 uint32_t LogicalBlocksPerPhysicalBlock; /* Revision 2 */
224 uint32_t OptimalTransferLengthGranularity; /* Revision 3 */
225} EFI_BLOCK_IO_MEDIA;
226typedef struct EFI_BLOCK_IO_PROTOCOL {
227 uint64_t Revision;
228 EFI_BLOCK_IO_MEDIA *Media;
229 void *Reset;
230 EFI_STATUS (*ReadBlocks)(struct EFI_BLOCK_IO_PROTOCOL *This, uint32_t MediaId, uint64_t Lba,
231 uint64_t BufferSize, void *Buffer);
232 void *WriteBlocks, *FlushBlocks;
233} EFI_BLOCK_IO_PROTOCOL;
234_Static_assert(offsetof(EFI_BLOCK_IO_MEDIA, BlockSize) == 12, "Media.BlockSize");
235_Static_assert(offsetof(EFI_BLOCK_IO_MEDIA, LastBlock) == 24, "Media.LastBlock");
236_Static_assert(offsetof(EFI_BLOCK_IO_PROTOCOL, ReadBlocks) == 0x18, "BlockIo.ReadBlocks");
237
238/* --- Graphics Output Protocol (UEFI 2.10 section 12.9.2) --- */
239#define EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID_INIT \
240 { 0x9042a9de, 0x23dc, 0x4a38, { 0x96, 0xfb, 0x7a, 0xde, 0xd0, 0x80, 0x51, 0x6a } }
241/* EFI_GRAPHICS_PIXEL_FORMAT */
242enum { PixelRedGreenBlueReserved8BitPerColor, PixelBlueGreenRedReserved8BitPerColor, PixelBitMask,
243 PixelBltOnly, PixelFormatMax };
244typedef struct {
245 uint32_t RedMask, GreenMask, BlueMask, ReservedMask;
246} EFI_PIXEL_BITMASK;
247typedef struct {
248 uint32_t Version;
249 uint32_t HorizontalResolution;
250 uint32_t VerticalResolution;
251 uint32_t PixelFormat; /* EFI_GRAPHICS_PIXEL_FORMAT (a C enum, 4 bytes) */
252 EFI_PIXEL_BITMASK PixelInformation;
253 uint32_t PixelsPerScanLine;
254} EFI_GRAPHICS_OUTPUT_MODE_INFORMATION;
255typedef struct {
256 uint32_t MaxMode;
257 uint32_t Mode;
258 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
259 uint64_t SizeOfInfo;
260 uint64_t FrameBufferBase; /* EFI_PHYSICAL_ADDRESS */
261 uint64_t FrameBufferSize; /* UINTN */
262} EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE;
263typedef struct EFI_GRAPHICS_OUTPUT_PROTOCOL {
264 void *QueryMode, *SetMode, *Blt;
265 EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *Mode;
266} EFI_GRAPHICS_OUTPUT_PROTOCOL;
267_Static_assert(offsetof(EFI_GRAPHICS_OUTPUT_MODE_INFORMATION, PixelFormat) == 12, "Gop.Info.PixelFormat");
268_Static_assert(offsetof(EFI_GRAPHICS_OUTPUT_MODE_INFORMATION, PixelsPerScanLine) == 32, "Gop.Info.PixelsPerScanLine");
269_Static_assert(offsetof(EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE, FrameBufferBase) == 24, "Gop.Mode.FrameBufferBase");
270_Static_assert(offsetof(EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE, FrameBufferSize) == 32, "Gop.Mode.FrameBufferSize");
271_Static_assert(offsetof(EFI_GRAPHICS_OUTPUT_PROTOCOL, Mode) == 24, "Gop.Mode");
272
273#endif