]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - wimboot/wimboot-2.7.3/src/peloader.h
1.1.07 release
[Ventoy.git] / wimboot / wimboot-2.7.3 / src / peloader.h
1 #ifndef _PELOADER_H
2 #define _PELOADER_H
3
4 /*
5 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 */
22
23 /**
24 * @file
25 *
26 * PE image loader
27 *
28 */
29
30 #include <stdint.h>
31 #include "wimboot.h"
32
33 /** DOS MZ header */
34 struct mz_header {
35 /** Magic number */
36 uint16_t magic;
37 /** Bytes on last page of file */
38 uint16_t cblp;
39 /** Pages in file */
40 uint16_t cp;
41 /** Relocations */
42 uint16_t crlc;
43 /** Size of header in paragraphs */
44 uint16_t cparhdr;
45 /** Minimum extra paragraphs needed */
46 uint16_t minalloc;
47 /** Maximum extra paragraphs needed */
48 uint16_t maxalloc;
49 /** Initial (relative) SS value */
50 uint16_t ss;
51 /** Initial SP value */
52 uint16_t sp;
53 /** Checksum */
54 uint16_t csum;
55 /** Initial IP value */
56 uint16_t ip;
57 /** Initial (relative) CS value */
58 uint16_t cs;
59 /** File address of relocation table */
60 uint16_t lfarlc;
61 /** Overlay number */
62 uint16_t ovno;
63 /** Reserved words */
64 uint16_t res[4];
65 /** OEM identifier (for oeminfo) */
66 uint16_t oemid;
67 /** OEM information; oemid specific */
68 uint16_t oeminfo;
69 /** Reserved words */
70 uint16_t res2[10];
71 /** File address of new exe header */
72 uint32_t lfanew;
73 } __attribute__ (( packed ));
74
75 /** MZ header magic */
76 #define MZ_HEADER_MAGIC 0x5a4d
77
78 /** COFF file header */
79 struct coff_header {
80 /** Magic number */
81 uint16_t magic;
82 /** Number of sections */
83 uint16_t num_sections;
84 /** Timestamp (seconds since the Epoch) */
85 uint32_t timestamp;
86 /** Offset to symbol table */
87 uint32_t symtab;
88 /** Number of symbol table entries */
89 uint32_t num_syms;
90 /** Length of optional header */
91 uint16_t opthdr_len;
92 /** Flags */
93 uint16_t flags;
94 } __attribute__ (( packed ));
95
96 /** COFF section */
97 struct coff_section {
98 /** Section name */
99 char name[8];
100 /** Physical address or virtual length */
101 union {
102 /** Physical address */
103 uint32_t physical;
104 /** Virtual length */
105 uint32_t virtual_len;
106 } misc;
107 /** Virtual address */
108 uint32_t virtual;
109 /** Length of raw data */
110 uint32_t raw_len;
111 /** Offset to raw data */
112 uint32_t raw;
113 /** Offset to relocations */
114 uint32_t relocations;
115 /** Offset to line numbers */
116 uint32_t line_numbers;
117 /** Number of relocations */
118 uint16_t num_relocations;
119 /** Number of line numbers */
120 uint16_t num_line_numbers;
121 /** Flags */
122 uint32_t flags;
123 } __attribute__ (( packed ));
124
125 /** PE file header */
126 struct pe_header {
127 /** Magic number */
128 uint32_t magic;
129 /** COFF header */
130 struct coff_header coff;
131 } __attribute__ (( packed ));
132
133 /** PE header magic */
134 #define PE_HEADER_MAGIC 0x00004550
135
136 /** PE optional header */
137 struct pe_optional_header {
138 /** Magic number */
139 uint16_t magic;
140 /** Major linker version */
141 uint8_t linker_major;
142 /** Minor linker version */
143 uint8_t linker_minor;
144 /** Length of code */
145 uint32_t text_len;
146 /** Length of initialised data */
147 uint32_t data_len;
148 /** Length of uninitialised data */
149 uint32_t bss_len;
150 /** Entry point */
151 uint32_t entry;
152 /** Base of code */
153 uint32_t text;
154 /** Base of data */
155 uint32_t data;
156 /** Image base address */
157 uint32_t base;
158 /** Section alignment */
159 uint32_t section_align;
160 /** File alignment */
161 uint32_t file_align;
162 /** Major operating system version */
163 uint16_t os_major;
164 /** Minor operating system version */
165 uint16_t os_minor;
166 /** Major image version */
167 uint16_t image_major;
168 /** Minor image version */
169 uint16_t image_minor;
170 /** Major subsystem version */
171 uint16_t subsystem_major;
172 /** Minor subsystem version */
173 uint16_t subsystem_minor;
174 /** Win32 version */
175 uint32_t win32_version;
176 /** Size of image */
177 uint32_t len;
178 /** Size of headers */
179 uint32_t header_len;
180 /* Plus extra fields that we don't care about */
181 } __attribute__ (( packed ));
182
183 /** A loaded PE image */
184 struct loaded_pe {
185 /** Base address */
186 void *base;
187 /** Length */
188 size_t len;
189 /** Entry point */
190 void ( * entry ) ( struct bootapp_descriptor *bootapp );
191 };
192
193 extern int load_pe ( const void *data, size_t len, struct loaded_pe *pe );
194
195 #endif /* _PELOADER_H */