summaryrefslogtreecommitdiffstats
path: root/uefi.h
blob: 6d46f16001987a3d10311f67380cd03ff30b789a (plain) (blame)
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
/* =============================================================================
 * uefi.h — minimal UEFI types and protocols for the RNG bootloader
 *
 * Standalone, freestanding, no external headers.
 * ============================================================================= */

#ifndef UEFI_H
#define UEFI_H

/* ---- basic types ---- */
typedef unsigned char       u8;
typedef unsigned short      u16;
typedef unsigned int        u32;
typedef unsigned long long  u64;
/* UEFI CHAR16 — wchar_t must be 16-bit (use -fshort-wchar).
 * GCC by default makes wchar_t = int (32-bit).  -fshort-wchar makes
 * it unsigned short (16-bit) to match UEFI's CHAR16.
 * We don't include <stddef.h> (freestanding), so define our own. */
typedef __CHAR16_TYPE__     char16;
typedef signed short       int16;
typedef long long           int64;
typedef void               *handle;

/* ---- EFI status codes ---- */
#define EFI_SUCCESS             0
#define EFI_LOAD_ERROR          ((u64)1)
#define EFI_INVALID_PARAMETER   ((u64)2)
#define EFI_UNSUPPORTED         ((u64)3)
#define EFI_NOT_FOUND           ((u64)14)
#define EFI_ALREADY_STARTED     ((u64)25)
#define EFI_OUT_OF_RESOURCES    ((u64)9)

/* ---- GUIDs (128-bit) ---- */
typedef struct { u32 a; u16 b; u16 c; u8 d[8]; } efi_guid;

/* ---- EFI time (unused, part of file info struct) ---- */
typedef struct {
    u16 year; u8 month; u8 day;
    u8 hour; u8 minute; u8 second; u8 pad1;
    u32 ns; int16 tz; u8 daylight;
} efi_time;

/* ---- table headers ---- */
typedef struct {
    u64 signature;
    u32 revision;
    u32 header_size;
    u32 crc32;
    u32 reserved;
} efi_table_header;

/* ---- system table ---- */
typedef struct efi_config_table efi_config_table;
typedef struct efi_simple_text_output efi_simple_text_output;
typedef struct efi_simple_text_input  efi_simple_text_input;
typedef struct efi_boot_services      efi_boot_services;
typedef struct efi_runtime_services   efi_runtime_services;

typedef struct {
    efi_table_header                 hdr;
    char16                          *firmware_vendor;
    u32                              firmware_revision;
    handle                           console_in_handle;
    efi_simple_text_input           *con_in;
    handle                           console_out_handle;
    efi_simple_text_output          *con_out;
    handle                           stderr_handle;
    efi_simple_text_output          *std_err;
    efi_runtime_services            *runtime_services;
    efi_boot_services               *boot_services;
    u64                              number_of_table_entries;
    efi_config_table                *configuration_table;
} efi_system_table;

/* ---- EFI input key ---- */
typedef struct {
    u16  scan_code;
    char16 unicode_char;
} efi_input_key;

/* ---- simple text output (console) ---- */
typedef u64 (*efi_text_reset)(efi_simple_text_output *, u8);
typedef u64 (*efi_text_output)(efi_simple_text_output *, const char16 *);

struct efi_simple_text_output {
    void                *unused;            // Index 0: Reset
    efi_text_output      output_string;      // Index 1: OutputString
    void                *unused2[8];         // Index 2-9: TestString..Mode
};

/* ---- simple text input (keyboard) ---- */
typedef u64 (*efi_input_reset)(efi_simple_text_input *, u8);
typedef u64 (*efi_input_read_key)(efi_simple_text_input *, efi_input_key *);

struct efi_simple_text_input {
    efi_input_reset      reset;              /* Index 0: Reset */
    efi_input_read_key   read_key_stroke;    /* Index 1: ReadKeyStroke */
    handle               wait_for_key;       /* Index 2: WaitForKey event */
};

/* ---- boot services ---- */
typedef u64 (*efi_exit_boot_services)(handle, u64);
typedef u64 (*efi_get_memory_map)(void*, void*, void*, void*, void*);
typedef u64 (*efi_locate_protocol)(const efi_guid *, void *, void **);
typedef u64 (*efi_load_image)(u8, handle, void *, void *, u64, handle *);
typedef u64 (*efi_start_image)(handle, void *, u64 *);
typedef u64 (*efi_handle_protocol)(handle, const efi_guid *, void **);
typedef u64 (*efi_open_volume)(void *, void **);
typedef u64 (*efi_open_file)(void *, void **, char16 *, u64, u64);
typedef u64 (*efi_read_file)(void *, u64 *, void *);
typedef u64 (*efi_allocate_pool)(u8, u64, void **);

/* Boot Services table matching UEFI spec v2.10 function order.
   Each entry is one function pointer (8 bytes on x64). */
struct efi_boot_services {
    efi_table_header        hdr;                         /* 0  */
    void                   *raise_tpl;                   /* 24 index  0 */
    void                   *restore_tpl;                 /* 32 index  1 */
    void                   *allocate_pages;              /* 40 index  2 */
    void                   *free_pages;                  /* 48 index  3 */
    efi_get_memory_map      get_memory_map;              /* 56 index  4 */
    efi_allocate_pool       allocate_pool;               /* 64 index  5 */
    void                   *free_pool;                   /* 72 index  6 */
    void                   *create_event;                /* 80 index  7 */
    void                   *set_timer;                   /* 88 index  8 */
    void                   *wait_for_event;              /* 96 index  9 */
    void                   *signal_event;                /*104 index 10 */
    void                   *close_event;                 /*112 index 11 */
    void                   *check_event;                 /*120 index 12 */
    void                   *install_protocol_interface;  /*128 index 13 */
    void                   *reinstall_protocol_interface;/*136 index 14 */
    void                   *uninstall_protocol_interface;/*144 index 15 */
    efi_handle_protocol     handle_protocol;             /*152 index 16 */
    void                   *reserved;                    /*160 index 17 */
    void                   *register_protocol_notify;    /*168 index 18 */
    void                   *locate_handle;               /*176 index 19 */
    void                   *locate_device_path;          /*184 index 20 */
    void                   *install_config_table;        /*192 index 21 */
    efi_load_image          load_image;                  /*200 index 22 */
    efi_start_image         start_image;                 /*208 index 23 */
    void                   *exit;                        /*216 index 24 */
    void                   *unload_image;                /*224 index 25 */
    efi_exit_boot_services  exit_boot_services;          /*232 index 26 */
    void                   *get_next_monotonic_count;    /*240 index 27 */
    void                   *stall;                       /*248 index 28 */
    void                   *set_watchdog_timer;          /*256 index 29 */
    void                   *connect_controller;          /*264 index 30 */
    void                   *disconnect_controller;       /*272 index 31 */
    void                   *open_protocol;               /*280 index 32 */
    void                   *close_protocol;              /*288 index 33 */
    void                   *open_protocol_information;   /*296 index 34 */
    void                   *protocols_per_handle;        /*304 index 35 */
    void                   *locate_handle_buffer;        /*312 index 36 */
    efi_locate_protocol     locate_protocol;             /*320 index 37 */
    void                   *install_multiple_protocols;  /*328 index 38 */
    void                   *uninstall_multiple_protocols;/*336 index 39 */
    void                   *calculate_crc32;             /*344 index 40 */
    void                   *copy_mem;                    /*352 index 41 */
    void                   *set_mem;                     /*360 index 42 */
    void                   *create_event_ex;             /*368 index 43 */
};

/* ---- GUID for Simple File System Protocol ---- */
#define EFI_SIMPLE_FILE_SYSTEM_GUID \
    {0x0964e5b22, 0x6459, 0x11d2, {0x8e,0x39,0x00,0xa0,0xc9,0x69,0x72,0x3b}}

/* ---- file protocol ---- */
typedef struct {
    u64                              revision;
    efi_open_volume                  open_volume;
} efi_simple_file_system;

typedef struct efi_file_protocol     efi_file_protocol;

typedef u64 (*efi_file_open)(efi_file_protocol *, efi_file_protocol **, char16 *, u64, u64);
typedef u64 (*efi_file_read)(efi_file_protocol *, u64 *, void *);
typedef u64 (*efi_file_close)(efi_file_protocol *);

#define EFI_FILE_MODE_READ  1

struct efi_file_protocol {
    u64              revision;
    efi_file_open    open;
    efi_file_close   close;
    void            *unused;
    efi_file_read    read;
    void            *unused2[5];
};

/* ---- file info (for getting file size) ---- */
typedef struct {
    u64      size;
    u64      file_size;
    u64      physical_size;
    efi_time create_time;
    efi_time last_access;
    efi_time modification_time;
    u64      attribute;
    char16   file_name[1];
} efi_file_info;

/* ---- loaded image protocol ---- */
typedef struct {
    u32      revision;
    handle   parent_handle;
    handle   system_table;
    handle   device_handle;
    void    *file_path;
    void    *reserved;
    u32      load_options_size;
    void    *load_options;
    void    *image_base;
    u64      image_size;
    u64      image_code_type;
    u64      image_data_type;
    void    *unused;
} efi_loaded_image;

/* ---- loaded image protocol GUID ---- */
#define EFI_LOADED_IMAGE_GUID \
    {0x5b1b31a1, 0x9562, 0x11d2, {0x8e,0x3f,0x00,0xa0,0xc9,0x69,0x72,0x3b}}

/* ---- device path protocol ---- */
typedef struct {
    u8  type;
    u8  sub_type;
    u16 length;
} efi_device_path;

#endif /* UEFI_H */