• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef EFI_WRAPPER_H
2 #define EFI_WRAPPER_H
3 
4 #define MSDOS_SIGNATURE	0x5a4d
5 #define PE_SIGNATURE	0x4550
6 #define PE32_FORMAT	0x10b
7 #define PE32P_FORMAT	0x20b	/* PE32+ */
8 
9 #define IMAGE_FILE_MACHINE_I386			0x14c
10 #define IMAGE_FILE_MACHINE_X86_64		0x8664
11 #define IMAGE_FILE_EXECUTABLE_IMAGE		0x0002
12 #define IMAGE_FILE_LINE_NUMBERS_STRIPPED	0x0004
13 #define IMAGE_FILE_32BIT_MACHINE		0x0100
14 #define IMAGE_FILE_DEBUG_STRIPPED		0x0200
15 
16 #define IMAGE_SUBSYSTEM_EFI_APPLICATION		0x0a
17 
18 #define IMAGE_SCN_CNT_CODE		0x00000020
19 #define IMAGE_SCN_CNT_INITIALIZED_DATA	0x00000040
20 #define IMAGE_SCN_ALIGN_1BYTES		0x00100000
21 #define IMAGE_SCN_ALIGN_16BYTES		0x00500000
22 #define IMAGE_SCN_MEM_DISCARDABLE	0x02000000
23 #define IMAGE_SCN_MEM_EXECUTE		0x20000000
24 #define IMAGE_SCN_MEM_READ		0x40000000
25 
26 #define __packed	__attribute__((packed))
27 #define OFFSETOF(t,m)	((size_t)&((t *)0)->m)
28 
29 struct header {
30 	__uint16_t msdos_signature;
31 	__uint8_t _pad1[0x16];
32 	__uint16_t relocs_ptr;
33 	__uint8_t __pad2[0x3c - 0x1a];
34 	__uint32_t pe_hdr;
35 	__uint16_t pe_signature;
36 	__uint16_t _pad2;
37 } __packed;
38 
39 /* FIXME: when setting up coff_hdr, set up optional_hdr_sz
40  * based on PE32 or PE32+ format
41  */
42 /*
43  * COFF header
44  */
45 struct coff_hdr {
46 	__uint16_t arch;
47 	__uint16_t nr_sections;
48 	__uint32_t timedatestamp;
49 	__uint32_t symtab;
50 	__uint32_t nr_syms;
51 	__uint16_t optional_hdr_sz;
52 	__uint16_t characteristics;
53 } __packed;
54 
55 struct optional_hdr {
56 	__uint16_t format;
57 	__uint8_t major_linker_version;
58 	__uint8_t minor_linker_version;
59 	__uint32_t code_sz;
60 	__uint32_t initialized_data_sz;
61 	__uint32_t uninitialized_data_sz;
62 	__uint32_t entry_point;
63 	__uint32_t base_code;
64 	__uint32_t data;
65 } __packed;
66 
67 /* For PE32+, the optional_header does NOT have
68  * data after base_code
69  */
70 struct optional_hdr_pe32p {
71 	__uint16_t format;
72 	__uint8_t major_linker_version;
73 	__uint8_t minor_linker_version;
74 	__uint32_t code_sz;
75 	__uint32_t initialized_data_sz;
76 	__uint32_t uninitialized_data_sz;
77 	__uint32_t entry_point;
78 	__uint32_t base_code;
79 } __packed;
80 /*
81  * Extra header fields
82  */
83 struct extra_hdr {
84 	__uint32_t image_base;
85 	__uint32_t section_align;
86 	__uint32_t file_align;
87 	__uint16_t major_os_version;
88 	__uint16_t minor_os_version;
89 	__uint16_t major_image_version;
90 	__uint16_t minor_image_version;
91 	__uint16_t major_subsystem_version;
92 	__uint16_t minor_subsystem_version;
93 	__uint32_t win32_version;
94 	__uint32_t image_sz;
95 	__uint32_t headers_sz;
96 	__uint32_t checksum;
97 	__uint16_t subsystem;
98 	__uint16_t dll_characteristics;
99 	__uint32_t stack_reserve_sz;
100 	__uint32_t stack_commit_sz;
101 	__uint32_t heap_reserve_sz;
102 	__uint32_t heap_commit_sz;
103 	__uint32_t loader_flags;
104 	__uint32_t rva_and_sizes_nr;
105 	struct {
106 		__uint64_t export_table;
107 		__uint64_t import_table;
108 		__uint64_t resource_table;
109 		__uint64_t exception_table;
110 		__uint64_t certification_table;
111 		__uint64_t base_relocation_table;
112 	} data_directory;
113 } __packed;
114 
115 /* Extra header for PE32+ format
116  * FIXME: There are additional fields in Microsoft PE COFF v8
117  */
118 
119 struct extra_hdr_pe32p {
120 	__uint64_t image_base;
121 	__uint32_t section_align;
122 	__uint32_t file_align;
123 	__uint16_t major_os_version;
124 	__uint16_t minor_os_version;
125 	__uint16_t major_image_version;
126 	__uint16_t minor_image_version;
127 	__uint16_t major_subsystem_version;
128 	__uint16_t minor_subsystem_version;
129 	__uint32_t win32_version;
130 	__uint32_t image_sz;
131 	__uint32_t headers_sz;
132 	__uint32_t checksum;
133 	__uint16_t subsystem;
134 	__uint16_t dll_characteristics;
135 	__uint64_t stack_reserve_sz;
136 	__uint64_t stack_commit_sz;
137 	__uint64_t heap_reserve_sz;
138 	__uint64_t heap_commit_sz;
139 	__uint32_t loader_flags;
140 	__uint32_t rva_and_sizes_nr;
141 	struct {
142 		__uint64_t export_table;
143 		__uint64_t import_table;
144 		__uint64_t resource_table;
145 		__uint64_t exception_table;
146 		__uint64_t certification_table;
147 		__uint64_t base_relocation_table;
148 	} data_directory;
149 } __packed;
150 
151 struct section {
152 	__uint8_t name[8];
153 	__uint32_t virtual_sz;
154 	__uint32_t virtual_address;
155 	__uint32_t raw_data_sz;
156 	__uint32_t raw_data;
157 	__uint32_t relocs;
158 	__uint32_t line_numbers;
159 	__uint16_t relocs_nr;
160 	__uint16_t line_numbers_nr;
161 	__uint32_t characteristics;
162 } __packed;
163 
164 struct coff_reloc {
165 	__uint32_t virtual_address;
166 	__uint32_t symtab_index;
167 	__uint16_t type;
168 };
169 
170 #endif /* EFI_WRAPPER_H */
171