1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use crate::{
    base::{EFI_GUID, EFI_HANDLE, EFI_STATUS, EFI_EVENT, EFI_TABLE_HEADER, UINT32, UINT64, UINTN, CHAR16, BOOLEAN, VOID, NOT_DEFINED},
    EFI_SPECIFICATION_VERSION,
    device_path::EFI_DEVICE_PATH_PROTOCOL
};
// use base::{Event, Handle, Handles, MemoryType, Status};
// use guid;
// use table;

pub const EFI_BOOT_SERVICES_SIGNATURE: UINTN = 0x56524553544f4f42;
pub const EFI_BOOT_SERVICES_REVISION: UINTN = EFI_SPECIFICATION_VERSION;

#[repr(C)]
pub struct EFI_BOOT_SERVICES {
  pub Hdr: EFI_TABLE_HEADER,

  pub RaiseTPL: EFI_RAISE_TPL,
  pub RestoreTPL: EFI_RESTORE_TPL,

  pub AllocatePages: EFI_ALLOCATE_PAGES,
  pub FreePages: EFI_FREE_PAGES,
  pub GetMemoryMap: EFI_GET_MEMORY_MAP,
  pub AllocatePool: EFI_ALLOCATE_POOL,
  pub FreePool: EFI_FREE_POOL,

  pub CreateEvent: EFI_CREATE_EVENT,
  pub SetTimer: EFI_SET_TIMER,
  pub WaitForEvent: EFI_WAIT_FOR_EVENT,
  pub SignalEvent: EFI_SIGNAL_EVENT,
  pub CloseEvent: EFI_CLOSE_EVENT,
  pub CheckEvent: EFI_CHECK_EVENT,

  pub InstallProtocolInterface: EFI_INSTALL_PROTOCOL_INTERFACE,
  pub ReinstallProtocolInterface: EFI_REINSTALL_PROTOCOL_INTERFACE,
  pub UninstallProtocolInterface: EFI_UNINSTALL_PROTOCOL_INTERFACE,
  pub HandleProtocol: EFI_HANDLE_PROTOCOL,
  pub Reserve: *const VOID,
  pub RegisterProtocolNotify: EFI_REGISTER_PROTOCOL_NOTIFY,
  pub LocateHandle: EFI_LOCATE_HANDLE,
  pub LocateDevicePath: EFI_LOCATE_DEVICE_PATH,
  pub InstallConfigurationTable: EFI_INSTALL_CONFIGURATION_TABLE,

  pub LoadImage: EFI_IMAGE_LOAD,
  pub StartImage: EFI_IMAGE_START,
  pub Exit: EFI_EXIT,
  pub UnloadImage: EFI_IMAGE_UNLOAD,
  pub ExitBootServices: EFI_EXIT_BOOT_SERVICES,

  pub GetNextMonotonicCount: EFI_GET_NEXT_MONOTONIC_COUNT,
  pub Stall: EFI_STALL,
  pub SetWatchdogTimer: EFI_SET_WATCHDOG_TIMER,

  pub ConnectController: EFI_CONNECT_CONTROLLER,
  pub DisconnectController: EFI_DISCONNECT_CONTROLLER,

  pub OpenProtocol: EFI_OPEN_PROTOCOL,
  pub CloseProtocol: EFI_CLOSE_PROTOCOL,
  pub OpenProtocolInformation: EFI_OPEN_PROTOCOL_INFORMATION,

  pub ProtocolsPerHandle: EFI_PROTOCOLS_PER_HANDLE,
  pub LocateHandleBuffer: EFI_LOCATE_HANDLE_BUFFER,
  pub LocateProtocol: EFI_LOCATE_PROTOCOL,
  pub InstallMultipleProtocolInterfaces: EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES,
  pub UninstallMultipleProtocolInterfaces: EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES,

  pub CalculateCrc32: EFI_CALCULATE_CRC32,

  pub CopyMem: EFI_COPY_MEM,
  pub SetMem: EFI_SET_MEM,
  pub CreateEventEx: EFI_CREATE_EVENT_EX,
}

// The below are methods currently not defined
pub type EFI_RAISE_TPL = *const NOT_DEFINED;
pub type EFI_RESTORE_TPL = *const NOT_DEFINED;
pub type EFI_ALLOCATE_PAGES = *const NOT_DEFINED;
pub type EFI_FREE_PAGES = *const NOT_DEFINED;
pub type EFI_GET_MEMORY_MAP = *const NOT_DEFINED;

pub type EFI_REINSTALL_PROTOCOL_INTERFACE = *const NOT_DEFINED;
pub type EFI_HANDLE_PROTOCOL = *const NOT_DEFINED;
pub type EFI_REGISTER_PROTOCOL_NOTIFY = *const NOT_DEFINED;
pub type EFI_LOCATE_HANDLE = *const NOT_DEFINED;
pub type EFI_LOCATE_DEVICE_PATH = *const NOT_DEFINED;
pub type EFI_INSTALL_CONFIGURATION_TABLE = *const NOT_DEFINED;
pub type EFI_EXIT = *const NOT_DEFINED;
pub type EFI_IMAGE_UNLOAD = *const NOT_DEFINED;
pub type EFI_EXIT_BOOT_SERVICES = *const NOT_DEFINED;
pub type EFI_SET_WATCHDOG_TIMER = *const NOT_DEFINED;
pub type EFI_CONNECT_CONTROLLER = *const NOT_DEFINED;
pub type EFI_DISCONNECT_CONTROLLER = *const NOT_DEFINED;
pub type EFI_OPEN_PROTOCOL_INFORMATION = *const NOT_DEFINED;
pub type EFI_PROTOCOLS_PER_HANDLE = *const NOT_DEFINED;
pub type EFI_INSTALL_MULTIPLE_PROTOCOL_INTERFACES = *const NOT_DEFINED;
pub type EFI_UNINSTALL_MULTIPLE_PROTOCOL_INTERFACES = *const NOT_DEFINED;
pub type EFI_CALCULATE_CRC32 = *const NOT_DEFINED;
pub type EFI_COPY_MEM = *const NOT_DEFINED;
pub type EFI_SET_MEM = *const NOT_DEFINED;
pub type EFI_CREATE_EVENT_EX = *const NOT_DEFINED;

pub type EFI_ALLOCATE_POOL = extern "win64" fn(
    PoolType: EFI_MEMORY_TYPE,
    Size: UINTN,
    Buffer: *mut *const VOID
) -> EFI_STATUS;

pub type EFI_FREE_POOL = extern "win64" fn(
    Buffer: *const VOID
) -> EFI_STATUS;

pub type EFI_TPL = UINTN;

pub const TPL_APPLICATION: UINTN = 4;
pub const TPL_CALLBACK: UINTN = 8;
pub const TPL_NOTIFY: UINTN = 16;
pub const TPL_HIGH_LEVEL: UINTN = 31;

pub const EVT_TIMER: UINT32 = 0x80000000;
pub const EVT_RUNTIME: UINT32 = 0x40000000;
pub const EVT_NOTIFY_WAIT: UINT32 = 0x00000100;
pub const EVT_NOTIFY_SIGNAL: UINT32 = 0x00000200;
pub const EVT_SIGNAL_EXIT_BOOT_SERVICES: UINT32 = 0x00000201;
pub const EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE: UINT32 = 0x60000202;

pub type EFI_CREATE_EVENT = extern "win64" fn(
    Type: UINT32,
    NotifyTpl: EFI_TPL,
    NotifyFunction: Option<EFI_EVENT_NOTIFY>,
    NotifyContext: *const VOID,
    Event: *mut EFI_EVENT 
) -> EFI_STATUS;

pub type EFI_CLOSE_EVENT = extern "win64" fn(
    Event: EFI_EVENT 
) -> EFI_STATUS;

pub type EFI_SIGNAL_EVENT = extern "win64" fn(
    Event: EFI_EVENT
) -> EFI_STATUS;

pub type EFI_WAIT_FOR_EVENT = extern "win64" fn(
    NumberOfEvents: UINTN,
    Event: *const EFI_EVENT,
    Index: *mut UINTN
) -> EFI_STATUS;

pub type EFI_CHECK_EVENT = extern "win64" fn(
    Event: EFI_EVENT
) -> EFI_STATUS;

#[derive(Debug)]
#[repr(C)]
pub enum EFI_TIMER_DELAY {
    TimerCancel,
    TimerPeriodic,
    TimerRelative
}

pub type EFI_SET_TIMER = extern "win64" fn(
    Event: EFI_EVENT,
    Type: EFI_TIMER_DELAY,
    TriggerTime: UINT64
) -> EFI_STATUS;

#[derive(Debug)]
#[repr(C)]
pub enum EFI_INTERFACE_TYPE {
    EFI_NATIVE_INTERFACE = 0
}

pub type EFI_INSTALL_PROTOCOL_INTERFACE = extern "win64" fn(
    Handle: *mut EFI_HANDLE,
    Protocol: *const EFI_GUID,
    InterfaceType: EFI_INTERFACE_TYPE,
    Interface: *const VOID
) -> EFI_STATUS;

pub type EFI_UNINSTALL_PROTOCOL_INTERFACE  = extern "win64" fn(
    Handle: EFI_HANDLE,
    Protocol: *const EFI_GUID,
    Interface: *const VOID
) -> EFI_STATUS;

pub type EFI_EVENT_NOTIFY = extern "win64" fn(
    Event: EFI_EVENT,
    Context: *const VOID
) -> EFI_STATUS;

pub const EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL: UINT32 = 0x00000001;
pub const EFI_OPEN_PROTOCOL_GET_PROTOCOL: UINT32 = 0x00000002;
pub const EFI_OPEN_PROTOCOL_TEST_PROTOCOL: UINT32 = 0x00000004;
pub const EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER: UINT32 = 0x00000008;
pub const EFI_OPEN_PROTOCOL_BY_DRIVER: UINT32 = 0x00000010;
pub const EFI_OPEN_PROTOCOL_EXCLUSIVE: UINT32 = 0x00000020;

pub type EFI_OPEN_PROTOCOL =  extern "win64" fn(
    Handle: EFI_HANDLE,
    Protocol: *const EFI_GUID,
    Interface: *mut *const VOID,
    AgentHandle: EFI_HANDLE,
    ControllerHandle: EFI_HANDLE,
    Attributes: UINT32
) -> EFI_STATUS;

pub type EFI_CLOSE_PROTOCOL = extern "win64" fn(
  Handle: EFI_HANDLE,
  Protocol: *const EFI_GUID,
  AgentHandle: EFI_HANDLE,
  ControllerHandle: EFI_HANDLE
) -> EFI_STATUS;

#[derive(Debug)]
#[repr(C)]
pub enum EFI_LOCATE_SEARCH_TYPE {
  /// Retrieve all the handles in the handle database.
  AllHandles,
  /// Retrieve the next handle fron a RegisterProtocolNotify() event.
  ByRegisterNotify,
  /// Retrieve the set of handles from the handle database that support a 
  /// specified protocol.
  ByProtocol
}

pub type EFI_LOCATE_HANDLE_BUFFER = extern "win64" fn(
    SearchType: EFI_LOCATE_SEARCH_TYPE,
    Protocol: *const EFI_GUID,
    SearchKey: *const VOID,
    NoHandles: *mut UINTN,
    Buffer: *mut *const EFI_HANDLE
) -> EFI_STATUS;
 
pub type EFI_LOCATE_PROTOCOL = extern "win64" fn(
    Protocol: *const EFI_GUID,
    Registration: *const VOID,
    Interface: *mut *const VOID
) -> EFI_STATUS;

pub type EFI_IMAGE_LOAD = extern "win64" fn(
    BootPolicy: BOOLEAN,
    ParentImageHandle: EFI_HANDLE,
    DevicePath: *const EFI_DEVICE_PATH_PROTOCOL,
    SourceBuffer: *const VOID,
    SourceSize: UINTN,
    ImageHandle: *mut EFI_HANDLE
) -> EFI_STATUS;

pub type EFI_IMAGE_START = extern "win64" fn(
    ImageHandle: EFI_HANDLE,
    ExitDataSize: *mut UINTN,
    ExitData: *mut *const CHAR16
) -> EFI_STATUS;

pub type EFI_STALL = extern "win64" fn(
    Microseconds: UINTN
) -> EFI_STATUS;

pub type EFI_GET_NEXT_MONOTONIC_COUNT = extern "win64" fn(
    Count: *mut UINT64  
) -> EFI_STATUS;

#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub enum EFI_ALLOCATE_TYPE {
    AllocateAnyPages,
    AllocateMaxAddress,
    AllocateAddress,
    MaxAllocateType
}

#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub enum EFI_MEMORY_TYPE {
    EfiReservedMemoryType,
    EfiLoaderCode,
    EfiLoaderData,
    EfiBootServicesCode,
    EfiBootServicesData,
    EfiRuntimeServicesCode,
    EfiRuntimeServicesData,
    EfiConventionalMemory,
    EfiUnusableMemory,
    EfiACPIReclaimMemory,
    EfiACPIMemoryNVS,
    EfiMemoryMappedIO,
    EfiMemoryMappedIOPortSpace,
    EfiPalCode,
    EfiMaxMemoryType
} 

pub type EFI_PHYSICAL_ADDRESS = UINT64;