1// Copyright 2016 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#pragma once
6
7#include <stddef.h>
8#include <stdint.h>
9
10// EFI on x86 uses the Microsoft ABI which is not the default for gcc
11#if defined(__x86_64__) || defined(__x86__)
12#define EFIAPI __attribute__((ms_abi))
13#else
14#define EFIAPI
15#endif
16
17#if UINTPTR_MAX == 0xffffffff
18#define EFI_ERROR_MASK 0x80000000
19#elif UINTPTR_MAX == 0xffffffffffffffff
20#define EFI_ERROR_MASK 0x8000000000000000
21#endif
22
23#define EFI_ERR(x) (EFI_ERROR_MASK | x)
24#define EFI_ERROR(x) (((ssize_t)x) < 0)
25
26#define EFI_SUCCESS              0
27#define EFI_LOAD_ERROR           EFI_ERR(1)
28#define EFI_INVALID_PARAMETER    EFI_ERR(2)
29#define EFI_UNSUPPORTED          EFI_ERR(3)
30#define EFI_BAD_BUFFER_SIZE      EFI_ERR(4)
31#define EFI_BUFFER_TOO_SMALL     EFI_ERR(5)
32#define EFI_NOT_READY            EFI_ERR(6)
33#define EFI_DEVICE_ERROR         EFI_ERR(7)
34#define EFI_WRITE_PROTECTED      EFI_ERR(8)
35#define EFI_OUT_OF_RESOURCES     EFI_ERR(9)
36#define EFI_VOLUME_CORRUPTED     EFI_ERR(10)
37#define EFI_VOLUME_FULL          EFI_ERR(11)
38#define EFI_NO_MEDIA             EFI_ERR(12)
39#define EFI_MEDIA_CHANGED        EFI_ERR(13)
40#define EFI_NOT_FOUND            EFI_ERR(14)
41#define EFI_ACCESS_DENIED        EFI_ERR(15)
42#define EFI_NO_RESPONSE          EFI_ERR(16)
43#define EFI_NO_MAPPING           EFI_ERR(17)
44#define EFI_TIMEOUT              EFI_ERR(18)
45#define EFI_NOT_STARTED          EFI_ERR(19)
46#define EFI_ALREADY_STARTED      EFI_ERR(20)
47#define EFI_ABORTED              EFI_ERR(21)
48#define EFI_ICMP_ERROR           EFI_ERR(22)
49#define EFI_TFTP_ERROR           EFI_ERR(23)
50#define EFI_PROTOCOL_ERROR       EFI_ERR(24)
51#define EFI_INCOMPATIBLE_VERSION EFI_ERR(25)
52#define EFI_SECURITY_VIOLATION   EFI_ERR(26)
53#define EFI_CRC_ERROR            EFI_ERR(27)
54#define EFI_END_OF_MEDIA         EFI_ERR(28)
55#define EFI_END_OF_FILE          EFI_ERR(31)
56#define EFI_INVALID_LANGUAGE     EFI_ERR(32)
57#define EFI_COMPROMISED_DATA     EFI_ERR(33)
58#define EFI_IP_ADDRESS_CONFLICT  EFI_ERR(34)
59#define EFI_HTTP_ERROR           EFI_ERR(35)
60
61// TODO: figure out where to put these. They're just mentioned in passing in the
62// spec as some of many industry standard GUIDs but not part of the spec itself.
63#define ACPI_TABLE_GUID \
64    {0xeb9d2d30, 0x2d88, 0x11d3, {0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}
65#define ACPI_20_TABLE_GUID \
66    {0x8868e871, 0xe4f1, 0x11d3, {0xbc, 0x22, 0x00, 0x80, 0xc7, 0x3c, 0x88, 0x81}}
67#define SMBIOS_TABLE_GUID \
68    {0xeb9d2d31, 0x2d88, 0x11d3, {0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}
69#define SMBIOS3_TABLE_GUID \
70    {0xf2fd1544, 0x9794, 0x4a2c, {0x99, 0x2e, 0xe5, 0xbb, 0xcf, 0x20, 0xe3, 0x94}}
71#define DEVICE_TREE_GUID \
72    {0xb1b621d5, 0xf19c, 0x41a5, {0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0}}
73
74
75typedef struct {
76    uint64_t Signature;
77    uint32_t Revision;
78    uint32_t HeaderSize;
79    uint32_t CRC32;
80    uint32_t Reserved;
81} efi_table_header;
82
83typedef struct efi_guid {
84    uint32_t data1;
85    uint16_t data2;
86    uint16_t data3;
87    uint8_t data4[8];
88
89	bool equals(const efi_guid& other) const {
90		bool matches = data1 == other.data1 && data2 == other.data2
91			&& data3 == other.data3;
92		for (auto i = 0; matches && i < 8; i++)
93			matches = data4[i] == other.data4[i];
94		return matches;
95	}
96} efi_guid;
97
98typedef void* efi_handle;
99
100typedef size_t efi_status;
101
102typedef struct {
103    uint8_t addr[32];
104} efi_mac_addr;
105
106typedef struct {
107    uint8_t addr[4];
108} efi_ipv4_addr;
109
110typedef struct {
111    uint8_t addr[16];
112} efi_ipv6_addr;
113
114typedef union {
115    efi_ipv4_addr v4;
116    efi_ipv6_addr v6;
117} efi_ip_addr;
118
119// This really belongs in boot-services.h, but causes circular dependencies with
120// device-path.h.
121typedef enum {
122    EfiReservedMemoryType,
123    EfiLoaderCode,
124    EfiLoaderData,
125    EfiBootServicesCode,
126    EfiBootServicesData,
127    EfiRuntimeServicesCode,
128    EfiRuntimeServicesData,
129    EfiConventionalMemory,
130    EfiUnusableMemory,
131    EfiACPIReclaimMemory,
132    EfiACPIMemoryNVS,
133    EfiMemoryMappedIO,
134    EfiMemoryMappedIOPortSpace,
135    EfiPalCode,
136    EfiPersistentMemory,
137    EfiMaxMemoryType
138} efi_memory_type;
139
140typedef uint64_t efi_physical_addr;
141typedef uint64_t efi_virtual_addr;
142
143typedef void* efi_event;
144
145#define EVT_TIMER                         0x80000000
146#define EVT_RUNTIME                       0x40000000
147#define EVT_NOTIFY_WAIT                   0x00000100
148#define EVT_NOTIFY_SIGNAL                 0x00000200
149#define EVT_SIGNAL_EXIT_BOOT_SERVICES     0x00000201
150#define EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE 0x60000202
151
152#define EFI_EVENT_GROUP_EXIT_BOOT_SERVICES \
153    {0x27abf055, 0xb1b8, 0x4c26, {0x80, 0x48, 0x74, 0x8f, 0x37, 0xba, 0xa2, 0xdf}}
154#define EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE \
155    {0x13fa7698, 0xc831, 0x49c7, {0x87, 0xea, 0x8f, 0x43, 0xfc, 0xc2, 0x51, 0x96}}
156#define EFI_EVENT_GROUP_MEMORY_MAP_CHANGE \
157    {0x78bee926, 0x692f, 0x48fd, {0x9e, 0xdb, 0x01, 0x42, 0x2e, 0xf0, 0xd7, 0xab}}
158#define EFI_EVENT_GROUP_READY_TO_BOOT \
159    {0x7ce88fb3, 0x4bd7, 0x4679, {0x87, 0xa8, 0xa8, 0xd8, 0xde, 0xe5, 0x0d, 0x2b}}
160
161typedef void (*efi_event_notify) (efi_event event, void* ctx) EFIAPI;
162
163typedef enum {
164    TimerCancel,
165    TimerPeriodic,
166    TimerRelative
167} efi_timer_delay;
168
169#ifndef __cplusplus
170typedef unsigned short char16_t;
171#endif
172