summaryrefslogtreecommitdiffstats
path: root/include/grub/efi/pe32.h
blob: 0ed8781f037634ab4f89e5d19e9aecd948965b21 (plain)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2006,2007,2008,2009  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef GRUB_EFI_PE32_HEADER
#define GRUB_EFI_PE32_HEADER	1

#include <grub/types.h>
#include <grub/efi/memory.h>

/* The MSDOS compatibility stub. This was copied from the output of
   objcopy, and it is not necessary to care about what this means.  */
#define GRUB_PE32_MSDOS_STUB \
  { \
    0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, \
    0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, \
    0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
    0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, \
    0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, \
    0x21, 0xb8, 0x01, 0x4c, 0xcd, 0x21, 0x54, 0x68, \
    0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72, \
    0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f, \
    0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6e, \
    0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20, \
    0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x0d, 0x0d, 0x0a, \
    0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00  \
  }

#define GRUB_PE32_MSDOS_STUB_SIZE	0x80

#define GRUB_PE32_MAGIC			0x5a4d

/* According to the spec, the minimal alignment is 512 bytes...
   But some examples (such as EFI drivers in the Intel
   Sample Implementation) use 32 bytes (0x20) instead, and it seems
   to be working.

   However, there is firmware showing up in the field now with
   page alignment constraints to guarantee that page protection
   bits take effect. Because currently existing GRUB code can not
   properly distinguish between in-memory and in-file layout, let's
   bump all alignment to GRUB_EFI_PAGE_SIZE. */
#define GRUB_PE32_SECTION_ALIGNMENT	GRUB_EFI_PAGE_SIZE
#define GRUB_PE32_FILE_ALIGNMENT	GRUB_PE32_SECTION_ALIGNMENT

struct grub_pe32_coff_header
{
  grub_uint16_t machine;
  grub_uint16_t num_sections;
  grub_uint32_t time;
  grub_uint32_t symtab_offset;
  grub_uint32_t num_symbols;
  grub_uint16_t optional_header_size;
  grub_uint16_t characteristics;
};

#define GRUB_PE32_MACHINE_I386			0x14c
#define GRUB_PE32_MACHINE_IA64			0x200
#define GRUB_PE32_MACHINE_X86_64		0x8664
#define GRUB_PE32_MACHINE_ARMTHUMB_MIXED	0x01c2
#define GRUB_PE32_MACHINE_ARM64			0xAA64
#define GRUB_PE32_MACHINE_RISCV32		0x5032
#define GRUB_PE32_MACHINE_RISCV64		0x5064

#define GRUB_PE32_RELOCS_STRIPPED		0x0001
#define GRUB_PE32_EXECUTABLE_IMAGE		0x0002
#define GRUB_PE32_LINE_NUMS_STRIPPED		0x0004
#define GRUB_PE32_LOCAL_SYMS_STRIPPED		0x0008
#define GRUB_PE32_AGGRESSIVE_WS_TRIM		0x0010
#define GRUB_PE32_LARGE_ADDRESS_AWARE		0x0020
#define GRUB_PE32_16BIT_MACHINE			0x0040
#define GRUB_PE32_BYTES_REVERSED_LO		0x0080
#define GRUB_PE32_32BIT_MACHINE			0x0100
#define GRUB_PE32_DEBUG_STRIPPED		0x0200
#define GRUB_PE32_REMOVABLE_RUN_FROM_SWAP	0x0400
#define GRUB_PE32_SYSTEM			0x1000
#define GRUB_PE32_DLL				0x2000
#define GRUB_PE32_UP_SYSTEM_ONLY		0x4000
#define GRUB_PE32_BYTES_REVERSED_HI		0x8000

struct grub_pe32_data_directory
{
  grub_uint32_t rva;
  grub_uint32_t size;
};

struct grub_pe32_optional_header
{
  grub_uint16_t magic;
  grub_uint8_t major_linker_version;
  grub_uint8_t minor_linker_version;
  grub_uint32_t code_size;
  grub_uint32_t data_size;
  grub_uint32_t bss_size;
  grub_uint32_t entry_addr;
  grub_uint32_t code_base;

  grub_uint32_t data_base;
  grub_uint32_t image_base;

  grub_uint32_t section_alignment;
  grub_uint32_t file_alignment;
  grub_uint16_t major_os_version;
  grub_uint16_t minor_os_version;
  grub_uint16_t major_image_version;
  grub_uint16_t minor_image_version;
  grub_uint16_t major_subsystem_version;
  grub_uint16_t minor_subsystem_version;
  grub_uint32_t reserved;
  grub_uint32_t image_size;
  grub_uint32_t header_size;
  grub_uint32_t checksum;
  grub_uint16_t subsystem;
  grub_uint16_t dll_characteristics;

  grub_uint32_t stack_reserve_size;
  grub_uint32_t stack_commit_size;
  grub_uint32_t heap_reserve_size;
  grub_uint32_t heap_commit_size;

  grub_uint32_t loader_flags;
  grub_uint32_t num_data_directories;

  /* Data directories.  */
  struct grub_pe32_data_directory export_table;
  struct grub_pe32_data_directory import_table;
  struct grub_pe32_data_directory resource_table;
  struct grub_pe32_data_directory exception_table;
  struct grub_pe32_data_directory certificate_table;
  struct grub_pe32_data_directory base_relocation_table;
  struct grub_pe32_data_directory debug;
  struct grub_pe32_data_directory architecture;
  struct grub_pe32_data_directory global_ptr;
  struct grub_pe32_data_directory tls_table;
  struct grub_pe32_data_directory load_config_table;
  struct grub_pe32_data_directory bound_import;
  struct grub_pe32_data_directory iat;
  struct grub_pe32_data_directory delay_import_descriptor;
  struct grub_pe32_data_directory com_runtime_header;
  struct grub_pe32_data_directory reserved_entry;
};

struct grub_pe64_optional_header
{
  grub_uint16_t magic;
  grub_uint8_t major_linker_version;
  grub_uint8_t minor_linker_version;
  grub_uint32_t code_size;
  grub_uint32_t data_size;
  grub_uint32_t bss_size;
  grub_uint32_t entry_addr;
  grub_uint32_t code_base;

  grub_uint64_t image_base;

  grub_uint32_t section_alignment;
  grub_uint32_t file_alignment;
  grub_uint16_t major_os_version;
  grub_uint16_t minor_os_version;
  grub_uint16_t major_image_version;
  grub_uint16_t minor_image_version;
  grub_uint16_t major_subsystem_version;
  grub_uint16_t minor_subsystem_version;
  grub_uint32_t reserved;
  grub_uint32_t image_size;
  grub_uint32_t header_size;
  grub_uint32_t checksum;
  grub_uint16_t subsystem;
  grub_uint16_t dll_characteristics;

  grub_uint64_t stack_reserve_size;
  grub_uint64_t stack_commit_size;
  grub_uint64_t heap_reserve_size;
  grub_uint64_t heap_commit_size;

  grub_uint32_t loader_flags;
  grub_uint32_t num_data_directories;

  /* Data directories.  */
  struct grub_pe32_data_directory export_table;
  struct grub_pe32_data_directory import_table;
  struct grub_pe32_data_directory resource_table;
  struct grub_pe32_data_directory exception_table;
  struct grub_pe32_data_directory certificate_table;
  struct grub_pe32_data_directory base_relocation_table;
  struct grub_pe32_data_directory debug;
  struct grub_pe32_data_directory architecture;
  struct grub_pe32_data_directory global_ptr;
  struct grub_pe32_data_directory tls_table;
  struct grub_pe32_data_directory load_config_table;
  struct grub_pe32_data_directory bound_import;
  struct grub_pe32_data_directory iat;
  struct grub_pe32_data_directory delay_import_descriptor;
  struct grub_pe32_data_directory com_runtime_header;
  struct grub_pe32_data_directory reserved_entry;
};

#define GRUB_PE32_PE32_MAGIC	0x10b
#define GRUB_PE32_PE64_MAGIC	0x20b

#define GRUB_PE32_SUBSYSTEM_EFI_APPLICATION	10

#define GRUB_PE32_NUM_DATA_DIRECTORIES	16

struct grub_pe32_section_table
{
  char name[8];
  grub_uint32_t virtual_size;
  grub_uint32_t virtual_address;
  grub_uint32_t raw_data_size;
  grub_uint32_t raw_data_offset;
  grub_uint32_t relocations_offset;
  grub_uint32_t line_numbers_offset;
  grub_uint16_t num_relocations;
  grub_uint16_t num_line_numbers;
  grub_uint32_t characteristics;
};

#define GRUB_PE32_SCN_CNT_CODE			0x00000020
#define GRUB_PE32_SCN_CNT_INITIALIZED_DATA	0x00000040
#define GRUB_PE32_SCN_MEM_DISCARDABLE		0x02000000
#define GRUB_PE32_SCN_MEM_EXECUTE		0x20000000
#define GRUB_PE32_SCN_MEM_READ			0x40000000
#define GRUB_PE32_SCN_MEM_WRITE			0x80000000

#define GRUB_PE32_SCN_ALIGN_1BYTES		0x00100000
#define GRUB_PE32_SCN_ALIGN_2BYTES		0x00200000
#define GRUB_PE32_SCN_ALIGN_4BYTES		0x00300000
#define GRUB_PE32_SCN_ALIGN_8BYTES		0x00400000
#define GRUB_PE32_SCN_ALIGN_16BYTES		0x00500000
#define GRUB_PE32_SCN_ALIGN_32BYTES		0x00600000
#define GRUB_PE32_SCN_ALIGN_64BYTES		0x00700000

#define GRUB_PE32_SCN_ALIGN_SHIFT		20
#define GRUB_PE32_SCN_ALIGN_MASK		7

#define GRUB_PE32_SIGNATURE_SIZE 4

struct grub_pe32_header
{
  /* This should be filled in with GRUB_PE32_MSDOS_STUB.  */
  grub_uint8_t msdos_stub[GRUB_PE32_MSDOS_STUB_SIZE];

  /* This is always PE\0\0.  */
  char signature[GRUB_PE32_SIGNATURE_SIZE];

  /* The COFF file header.  */
  struct grub_pe32_coff_header coff_header;

#if GRUB_TARGET_SIZEOF_VOID_P == 8
  /* The Optional header.  */
  struct grub_pe64_optional_header optional_header;
#else
  /* The Optional header.  */
  struct grub_pe32_optional_header optional_header;
#endif
};

struct grub_pe32_fixup_block
{
  grub_uint32_t page_rva;
  grub_uint32_t block_size;
  grub_uint16_t entries[0];
};

#define GRUB_PE32_FIXUP_ENTRY(type, offset)	(((type) << 12) | (offset))

#define GRUB_PE32_REL_BASED_ABSOLUTE	0
#define GRUB_PE32_REL_BASED_HIGH	1
#define GRUB_PE32_REL_BASED_LOW		2
#define GRUB_PE32_REL_BASED_HIGHLOW	3
#define GRUB_PE32_REL_BASED_HIGHADJ	4
#define GRUB_PE32_REL_BASED_MIPS_JMPADDR 5
#define GRUB_PE32_REL_BASED_ARM_MOV32A  5
#define GRUB_PE32_REL_BASED_RISCV_HI20	5
#define GRUB_PE32_REL_BASED_SECTION	6
#define GRUB_PE32_REL_BASED_REL		7
#define GRUB_PE32_REL_BASED_ARM_MOV32T  7
#define GRUB_PE32_REL_BASED_RISCV_LOW12I 7
#define GRUB_PE32_REL_BASED_RISCV_LOW12S 8
#define GRUB_PE32_REL_BASED_IA64_IMM64	9
#define GRUB_PE32_REL_BASED_DIR64	10
#define GRUB_PE32_REL_BASED_HIGH3ADJ	11

struct grub_pe32_symbol
{
  union
  {
    char short_name[8];
    grub_uint32_t long_name[2];
  };

  grub_uint32_t value;
  grub_uint16_t section;
  grub_uint16_t type;
  grub_uint8_t storage_class;
  grub_uint8_t num_aux;
} GRUB_PACKED;

#define GRUB_PE32_SYM_CLASS_EXTERNAL	2
#define GRUB_PE32_SYM_CLASS_STATIC	3
#define GRUB_PE32_SYM_CLASS_FILE	0x67

#define GRUB_PE32_DT_FUNCTION		0x20

struct grub_pe32_reloc
{
  grub_uint32_t offset;
  grub_uint32_t symtab_index;
  grub_uint16_t type;
} GRUB_PACKED;

#define GRUB_PE32_REL_I386_DIR32	0x6
#define GRUB_PE32_REL_I386_REL32	0x14

#endif /* ! GRUB_EFI_PE32_HEADER */