]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/include/grub/efi/pe32.h
Only for VirtualBox.
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / include / grub / efi / pe32.h
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
4 *
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef GRUB_EFI_PE32_HEADER
20 #define GRUB_EFI_PE32_HEADER 1
21
22 #include <grub/types.h>
23 #include <grub/efi/memory.h>
24
25 /* The MSDOS compatibility stub. This was copied from the output of
26 objcopy, and it is not necessary to care about what this means. */
27 #define GRUB_PE32_MSDOS_STUB \
28 { \
29 0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, \
30 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, \
31 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
32 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
33 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
36 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, \
37 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, \
38 0x21, 0xb8, 0x01, 0x4c, 0xcd, 0x21, 0x54, 0x68, \
39 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72, \
40 0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f, \
41 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6e, \
42 0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20, \
43 0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x0d, 0x0d, 0x0a, \
44 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 \
45 }
46
47 #define GRUB_PE32_MSDOS_STUB_SIZE 0x80
48
49 #define GRUB_PE32_MAGIC 0x5a4d
50
51 /* According to the spec, the minimal alignment is 512 bytes...
52 But some examples (such as EFI drivers in the Intel
53 Sample Implementation) use 32 bytes (0x20) instead, and it seems
54 to be working.
55
56 However, there is firmware showing up in the field now with
57 page alignment constraints to guarantee that page protection
58 bits take effect. Because currently existing GRUB code can not
59 properly distinguish between in-memory and in-file layout, let's
60 bump all alignment to GRUB_EFI_PAGE_SIZE. */
61 #define GRUB_PE32_SECTION_ALIGNMENT GRUB_EFI_PAGE_SIZE
62 #define GRUB_PE32_FILE_ALIGNMENT GRUB_PE32_SECTION_ALIGNMENT
63
64 struct grub_pe32_coff_header
65 {
66 grub_uint16_t machine;
67 grub_uint16_t num_sections;
68 grub_uint32_t time;
69 grub_uint32_t symtab_offset;
70 grub_uint32_t num_symbols;
71 grub_uint16_t optional_header_size;
72 grub_uint16_t characteristics;
73 };
74
75 #define GRUB_PE32_MACHINE_I386 0x14c
76 #define GRUB_PE32_MACHINE_MIPS 0x166
77 #define GRUB_PE32_MACHINE_IA64 0x200
78 #define GRUB_PE32_MACHINE_X86_64 0x8664
79 #define GRUB_PE32_MACHINE_ARMTHUMB_MIXED 0x01c2
80 #define GRUB_PE32_MACHINE_ARM64 0xAA64
81 #define GRUB_PE32_MACHINE_RISCV32 0x5032
82 #define GRUB_PE32_MACHINE_RISCV64 0x5064
83
84 #define GRUB_PE32_RELOCS_STRIPPED 0x0001
85 #define GRUB_PE32_EXECUTABLE_IMAGE 0x0002
86 #define GRUB_PE32_LINE_NUMS_STRIPPED 0x0004
87 #define GRUB_PE32_LOCAL_SYMS_STRIPPED 0x0008
88 #define GRUB_PE32_AGGRESSIVE_WS_TRIM 0x0010
89 #define GRUB_PE32_LARGE_ADDRESS_AWARE 0x0020
90 #define GRUB_PE32_16BIT_MACHINE 0x0040
91 #define GRUB_PE32_BYTES_REVERSED_LO 0x0080
92 #define GRUB_PE32_32BIT_MACHINE 0x0100
93 #define GRUB_PE32_DEBUG_STRIPPED 0x0200
94 #define GRUB_PE32_REMOVABLE_RUN_FROM_SWAP 0x0400
95 #define GRUB_PE32_SYSTEM 0x1000
96 #define GRUB_PE32_DLL 0x2000
97 #define GRUB_PE32_UP_SYSTEM_ONLY 0x4000
98 #define GRUB_PE32_BYTES_REVERSED_HI 0x8000
99
100 struct grub_pe32_data_directory
101 {
102 grub_uint32_t rva;
103 grub_uint32_t size;
104 };
105
106 struct grub_pe32_optional_header
107 {
108 grub_uint16_t magic;
109 grub_uint8_t major_linker_version;
110 grub_uint8_t minor_linker_version;
111 grub_uint32_t code_size;
112 grub_uint32_t data_size;
113 grub_uint32_t bss_size;
114 grub_uint32_t entry_addr;
115 grub_uint32_t code_base;
116
117 grub_uint32_t data_base;
118 grub_uint32_t image_base;
119
120 grub_uint32_t section_alignment;
121 grub_uint32_t file_alignment;
122 grub_uint16_t major_os_version;
123 grub_uint16_t minor_os_version;
124 grub_uint16_t major_image_version;
125 grub_uint16_t minor_image_version;
126 grub_uint16_t major_subsystem_version;
127 grub_uint16_t minor_subsystem_version;
128 grub_uint32_t reserved;
129 grub_uint32_t image_size;
130 grub_uint32_t header_size;
131 grub_uint32_t checksum;
132 grub_uint16_t subsystem;
133 grub_uint16_t dll_characteristics;
134
135 grub_uint32_t stack_reserve_size;
136 grub_uint32_t stack_commit_size;
137 grub_uint32_t heap_reserve_size;
138 grub_uint32_t heap_commit_size;
139
140 grub_uint32_t loader_flags;
141 grub_uint32_t num_data_directories;
142
143 /* Data directories. */
144 struct grub_pe32_data_directory export_table;
145 struct grub_pe32_data_directory import_table;
146 struct grub_pe32_data_directory resource_table;
147 struct grub_pe32_data_directory exception_table;
148 struct grub_pe32_data_directory certificate_table;
149 struct grub_pe32_data_directory base_relocation_table;
150 struct grub_pe32_data_directory debug;
151 struct grub_pe32_data_directory architecture;
152 struct grub_pe32_data_directory global_ptr;
153 struct grub_pe32_data_directory tls_table;
154 struct grub_pe32_data_directory load_config_table;
155 struct grub_pe32_data_directory bound_import;
156 struct grub_pe32_data_directory iat;
157 struct grub_pe32_data_directory delay_import_descriptor;
158 struct grub_pe32_data_directory com_runtime_header;
159 struct grub_pe32_data_directory reserved_entry;
160 };
161
162 struct grub_pe64_optional_header
163 {
164 grub_uint16_t magic;
165 grub_uint8_t major_linker_version;
166 grub_uint8_t minor_linker_version;
167 grub_uint32_t code_size;
168 grub_uint32_t data_size;
169 grub_uint32_t bss_size;
170 grub_uint32_t entry_addr;
171 grub_uint32_t code_base;
172
173 grub_uint64_t image_base;
174
175 grub_uint32_t section_alignment;
176 grub_uint32_t file_alignment;
177 grub_uint16_t major_os_version;
178 grub_uint16_t minor_os_version;
179 grub_uint16_t major_image_version;
180 grub_uint16_t minor_image_version;
181 grub_uint16_t major_subsystem_version;
182 grub_uint16_t minor_subsystem_version;
183 grub_uint32_t reserved;
184 grub_uint32_t image_size;
185 grub_uint32_t header_size;
186 grub_uint32_t checksum;
187 grub_uint16_t subsystem;
188 grub_uint16_t dll_characteristics;
189
190 grub_uint64_t stack_reserve_size;
191 grub_uint64_t stack_commit_size;
192 grub_uint64_t heap_reserve_size;
193 grub_uint64_t heap_commit_size;
194
195 grub_uint32_t loader_flags;
196 grub_uint32_t num_data_directories;
197
198 /* Data directories. */
199 struct grub_pe32_data_directory export_table;
200 struct grub_pe32_data_directory import_table;
201 struct grub_pe32_data_directory resource_table;
202 struct grub_pe32_data_directory exception_table;
203 struct grub_pe32_data_directory certificate_table;
204 struct grub_pe32_data_directory base_relocation_table;
205 struct grub_pe32_data_directory debug;
206 struct grub_pe32_data_directory architecture;
207 struct grub_pe32_data_directory global_ptr;
208 struct grub_pe32_data_directory tls_table;
209 struct grub_pe32_data_directory load_config_table;
210 struct grub_pe32_data_directory bound_import;
211 struct grub_pe32_data_directory iat;
212 struct grub_pe32_data_directory delay_import_descriptor;
213 struct grub_pe32_data_directory com_runtime_header;
214 struct grub_pe32_data_directory reserved_entry;
215 };
216
217 #define GRUB_PE32_PE32_MAGIC 0x10b
218 #define GRUB_PE32_PE64_MAGIC 0x20b
219
220 #define GRUB_PE32_SUBSYSTEM_EFI_APPLICATION 10
221
222 #define GRUB_PE32_NUM_DATA_DIRECTORIES 16
223
224 struct grub_pe32_section_table
225 {
226 char name[8];
227 grub_uint32_t virtual_size;
228 grub_uint32_t virtual_address;
229 grub_uint32_t raw_data_size;
230 grub_uint32_t raw_data_offset;
231 grub_uint32_t relocations_offset;
232 grub_uint32_t line_numbers_offset;
233 grub_uint16_t num_relocations;
234 grub_uint16_t num_line_numbers;
235 grub_uint32_t characteristics;
236 };
237
238 #define GRUB_PE32_SCN_CNT_CODE 0x00000020
239 #define GRUB_PE32_SCN_CNT_INITIALIZED_DATA 0x00000040
240 #define GRUB_PE32_SCN_MEM_DISCARDABLE 0x02000000
241 #define GRUB_PE32_SCN_MEM_EXECUTE 0x20000000
242 #define GRUB_PE32_SCN_MEM_READ 0x40000000
243 #define GRUB_PE32_SCN_MEM_WRITE 0x80000000
244
245 #define GRUB_PE32_SCN_ALIGN_1BYTES 0x00100000
246 #define GRUB_PE32_SCN_ALIGN_2BYTES 0x00200000
247 #define GRUB_PE32_SCN_ALIGN_4BYTES 0x00300000
248 #define GRUB_PE32_SCN_ALIGN_8BYTES 0x00400000
249 #define GRUB_PE32_SCN_ALIGN_16BYTES 0x00500000
250 #define GRUB_PE32_SCN_ALIGN_32BYTES 0x00600000
251 #define GRUB_PE32_SCN_ALIGN_64BYTES 0x00700000
252
253 #define GRUB_PE32_SCN_ALIGN_SHIFT 20
254 #define GRUB_PE32_SCN_ALIGN_MASK 7
255
256 #define GRUB_PE32_SIGNATURE_SIZE 4
257
258 struct grub_pe32_header
259 {
260 /* This should be filled in with GRUB_PE32_MSDOS_STUB. */
261 grub_uint8_t msdos_stub[GRUB_PE32_MSDOS_STUB_SIZE];
262
263 /* This is always PE\0\0. */
264 char signature[GRUB_PE32_SIGNATURE_SIZE];
265
266 /* The COFF file header. */
267 struct grub_pe32_coff_header coff_header;
268
269 #if GRUB_TARGET_SIZEOF_VOID_P == 8
270 /* The Optional header. */
271 struct grub_pe64_optional_header optional_header;
272 #else
273 /* The Optional header. */
274 struct grub_pe32_optional_header optional_header;
275 #endif
276 };
277
278 struct grub_pe32_fixup_block
279 {
280 grub_uint32_t page_rva;
281 grub_uint32_t block_size;
282 grub_uint16_t entries[0];
283 };
284
285 #define GRUB_PE32_FIXUP_ENTRY(type, offset) (((type) << 12) | (offset))
286
287 #define GRUB_PE32_REL_BASED_ABSOLUTE 0
288 #define GRUB_PE32_REL_BASED_HIGH 1
289 #define GRUB_PE32_REL_BASED_LOW 2
290 #define GRUB_PE32_REL_BASED_HIGHLOW 3
291 #define GRUB_PE32_REL_BASED_HIGHADJ 4
292 #define GRUB_PE32_REL_BASED_MIPS_JMPADDR 5
293 #define GRUB_PE32_REL_BASED_MIPS_LOW 6
294 #define GRUB_PE32_REL_BASED_MIPS_HIGH 4
295 #define GRUB_PE32_REL_BASED_MIPS_HIGHER 7
296 #define GRUB_PE32_REL_BASED_MIPS_HIGHEST 8
297 #define GRUB_PE32_REL_BASED_ARM_MOV32A 5
298 #define GRUB_PE32_REL_BASED_RISCV_HI20 5
299 #define GRUB_PE32_REL_BASED_SECTION 6
300 #define GRUB_PE32_REL_BASED_REL 7
301 #define GRUB_PE32_REL_BASED_ARM_MOV32T 7
302 #define GRUB_PE32_REL_BASED_RISCV_LOW12I 7
303 #define GRUB_PE32_REL_BASED_RISCV_LOW12S 8
304 #define GRUB_PE32_REL_BASED_IA64_IMM64 9
305 #define GRUB_PE32_REL_BASED_DIR64 10
306 #define GRUB_PE32_REL_BASED_HIGH3ADJ 11
307
308 struct grub_pe32_symbol
309 {
310 union
311 {
312 char short_name[8];
313 grub_uint32_t long_name[2];
314 };
315
316 grub_uint32_t value;
317 grub_uint16_t section;
318 grub_uint16_t type;
319 grub_uint8_t storage_class;
320 grub_uint8_t num_aux;
321 } GRUB_PACKED;
322
323 #define GRUB_PE32_SYM_CLASS_EXTERNAL 2
324 #define GRUB_PE32_SYM_CLASS_STATIC 3
325 #define GRUB_PE32_SYM_CLASS_FILE 0x67
326
327 #define GRUB_PE32_DT_FUNCTION 0x20
328
329 struct grub_pe32_reloc
330 {
331 grub_uint32_t offset;
332 grub_uint32_t symtab_index;
333 grub_uint16_t type;
334 } GRUB_PACKED;
335
336 #define GRUB_PE32_REL_I386_DIR32 0x6
337 #define GRUB_PE32_REL_I386_REL32 0x14
338
339 #endif /* ! GRUB_EFI_PE32_HEADER */