]>
Commit | Line | Data |
---|---|---|
fb3df220 | 1 | /** @file\r |
2 | Memory Only PE COFF loader. \r | |
3 | \r | |
4 | Copyright (c) 2006 - 2007, Intel Corporation \r | |
5 | All rights reserved. This program and the accompanying materials \r | |
6 | are licensed and made available under the terms and conditions of the BSD License \r | |
7 | which accompanies this distribution. The full text of the license may be found at \r | |
8 | http://opensource.org/licenses/bsd-license.php \r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
12 | \r | |
fb3df220 | 13 | **/\r |
14 | \r | |
15 | #ifndef __BASE_PE_COFF_LIB_H__\r | |
16 | #define __BASE_PE_COFF_LIB_H__\r | |
17 | \r | |
d8dcb666 | 18 | #include <IndustryStandard/PeImage.h>\r |
fb3df220 | 19 | //\r |
20 | // Return status codes from the PE/COFF Loader services\r | |
fb3df220 | 21 | //\r |
22 | #define IMAGE_ERROR_SUCCESS 0\r | |
23 | #define IMAGE_ERROR_IMAGE_READ 1 \r | |
24 | #define IMAGE_ERROR_INVALID_PE_HEADER_SIGNATURE 2\r | |
25 | #define IMAGE_ERROR_INVALID_MACHINE_TYPE 3\r | |
26 | #define IMAGE_ERROR_INVALID_SUBSYSTEM 4\r | |
27 | #define IMAGE_ERROR_INVALID_IMAGE_ADDRESS 5\r | |
28 | #define IMAGE_ERROR_INVALID_IMAGE_SIZE 6\r | |
29 | #define IMAGE_ERROR_INVALID_SECTION_ALIGNMENT 7\r | |
30 | #define IMAGE_ERROR_SECTION_NOT_LOADED 8\r | |
31 | #define IMAGE_ERROR_FAILED_RELOCATION 9\r | |
32 | #define IMAGE_ERROR_FAILED_ICACHE_FLUSH 10\r | |
33 | \r | |
146332ae | 34 | //\r |
35 | // PE/COFF Loader Read Function passed in by caller\r | |
36 | //\r | |
37 | typedef\r | |
38 | RETURN_STATUS\r | |
39 | (EFIAPI *PE_COFF_LOADER_READ_FILE) (\r | |
40 | IN VOID *FileHandle,\r | |
41 | IN UINTN FileOffset,\r | |
42 | IN OUT UINTN *ReadSize,\r | |
43 | OUT VOID *Buffer\r | |
44 | );\r | |
45 | \r | |
f6d7003d | 46 | ///\r |
47 | /// Context structure used while PE/COFF image is being loaded and relocated\r | |
48 | ///\r | |
146332ae | 49 | typedef struct {\r |
f6d7003d | 50 | ///\r |
51 | /// Is set by PeCoffLoaderGetImageInfo() to the ImageBase in the PE/COFF header\r | |
52 | ///\r | |
146332ae | 53 | PHYSICAL_ADDRESS ImageAddress;\r |
f6d7003d | 54 | ///\r |
55 | /// Is set by PeCoffLoaderGetImageInfo() to the SizeOfImage in the PE/COFF header.\r | |
56 | /// Image size includes the size of Debug Entry if it is present.\r | |
57 | ///\r | |
146332ae | 58 | UINT64 ImageSize;\r |
f6d7003d | 59 | ///\r |
60 | /// Is set to zero by PeCoffLoaderGetImageInfo(). If DestinationAddress is non zero,\r | |
61 | /// PeCoffLoaderRelocateImage() will relocate the image using this base address.\r | |
62 | /// If the DestinationAddress is zero, the ImageAddress will be used as the base\r | |
63 | /// address of relocation.\r | |
64 | ///\r | |
146332ae | 65 | PHYSICAL_ADDRESS DestinationAddress;\r |
f6d7003d | 66 | ///\r |
67 | /// PeCoffLoaderLoadImage() sets EntryPoint to to the entry point of the PE/COFF image.\r | |
68 | ///\r | |
146332ae | 69 | PHYSICAL_ADDRESS EntryPoint;\r |
f6d7003d | 70 | ///\r |
71 | /// Passed in by the caller to PeCoffLoaderGetImageInfo() and PeCoffLoaderLoadImage()\r | |
72 | /// to abstract accessing the image from the library.\r | |
73 | ///\r | |
146332ae | 74 | PE_COFF_LOADER_READ_FILE ImageRead;\r |
f6d7003d | 75 | ///\r |
76 | /// Used as the FileHandle passed into the ImageRead function when it's called.\r | |
77 | ///\r | |
146332ae | 78 | VOID *Handle;\r |
f6d7003d | 79 | ///\r |
80 | /// Caller allocated buffer of size FixupDataSize that can be optionally allocated\r | |
81 | /// prior to calling PeCoffLoaderRelocateImage(). \r | |
82 | /// This buffer is filled with the information used to fix up the image. \r | |
83 | /// The fixups have been applied to the image and this entry is just for information.\r | |
84 | ///\r | |
146332ae | 85 | VOID *FixupData;\r |
f6d7003d | 86 | ///\r |
87 | /// Is set by PeCoffLoaderGetImageInfo() to the Section Alignment in the PE/COFF header\r | |
88 | ///\r | |
146332ae | 89 | UINT32 SectionAlignment;\r |
f6d7003d | 90 | ///\r |
91 | /// Set by PeCoffLoaderGetImageInfo() to offset to the PE/COFF header.\r | |
92 | /// If the PE/COFF image does not start with a DOS header, this value is zero; \r | |
93 | /// otherwise, it's the offset to the PE/COFF header.\r | |
94 | ///\r | |
146332ae | 95 | UINT32 PeCoffHeaderOffset;\r |
f6d7003d | 96 | ///\r |
97 | /// Set by PeCoffLoaderGetImageInfo() to the Relative Virtual Address of the debug directory\r | |
98 | /// if it exists in the image\r | |
99 | ///\r | |
146332ae | 100 | UINT32 DebugDirectoryEntryRva;\r |
f6d7003d | 101 | ///\r |
102 | /// Set by PeCoffLoaderLoadImage() to CodeView area of the PE/COFF Debug directory.\r | |
103 | ///\r | |
146332ae | 104 | VOID *CodeView;\r |
f6d7003d | 105 | ///\r |
106 | /// Set by PeCoffLoaderLoadImage() to point to the PDB entry contained in the CodeView area.\r | |
107 | /// The PdbPointer points to the filename of the PDB file used for source-level debug of \r | |
108 | /// the image by a debugger.\r | |
109 | ///\r | |
146332ae | 110 | CHAR8 *PdbPointer;\r |
f6d7003d | 111 | ///\r |
112 | /// Is set by PeCoffLoaderGetImageInfo() to the Section Alignment in the PE/COFF header.\r | |
113 | ///\r | |
146332ae | 114 | UINTN SizeOfHeaders;\r |
f6d7003d | 115 | ///\r |
116 | /// Not used by this library class. Other library classes that layer on top of this library\r | |
117 | /// class fill in this value as part of their GetImageInfo call. \r | |
118 | /// This allows the caller of the library to know what type of memory needs to be allocated\r | |
119 | /// to load and relocate the image.\r | |
120 | ///\r | |
146332ae | 121 | UINT32 ImageCodeMemoryType;\r |
f6d7003d | 122 | ///\r |
123 | /// Not used by this library class. Other library classes that layer on top of this library \r | |
124 | /// class fill in this value as part of their GetImageInfo call.\r | |
125 | /// This allows the caller of the library to know what type of memory needs to be allocated\r | |
126 | /// to load and relocate the image\r | |
127 | ///\r | |
146332ae | 128 | UINT32 ImageDataMemoryType;\r |
f6d7003d | 129 | ///\r |
130 | /// Set by any of the library functions if they encounter an error. \r | |
131 | ///\r | |
146332ae | 132 | UINT32 ImageError;\r |
f6d7003d | 133 | ///\r |
134 | /// Set by PeCoffLoaderLoadImage() to indicate the size of FixupData that the caller must\r | |
135 | /// allocate before calling PeCoffLoaderRelocateImage()\r | |
136 | ///\r | |
146332ae | 137 | UINTN FixupDataSize;\r |
f6d7003d | 138 | ///\r |
139 | /// Set by PeCoffLoaderGetImageInfo() to the machine type stored in the PE/COFF header\r | |
140 | ///\r | |
146332ae | 141 | UINT16 Machine;\r |
f6d7003d | 142 | ///\r |
143 | /// Set by PeCoffLoaderGetImageInfo() to the subsystem type stored in the PE/COFF header.\r | |
144 | ///\r | |
146332ae | 145 | UINT16 ImageType;\r |
f6d7003d | 146 | ///\r |
147 | /// Set by PeCoffLoaderGetImageInfo() to TRUE if the PE/COFF image does not contain\r | |
148 | /// relocation information.\r | |
149 | ///\r | |
146332ae | 150 | BOOLEAN RelocationsStripped;\r |
f6d7003d | 151 | ///\r |
152 | /// Set by PeCoffLoaderGetImageInfo() to TRUE if the image is a TE image. \r | |
153 | /// For a definition of the TE Image format, see the Platform Initialization Pre-EFI\r | |
154 | /// Initialization Core Interface Specification.\r | |
155 | ///\r | |
146332ae | 156 | BOOLEAN IsTeImage;\r |
157 | } PE_COFF_LOADER_IMAGE_CONTEXT;\r | |
fb3df220 | 158 | \r |
159 | /**\r | |
160 | Retrieves information about a PE/COFF image.\r | |
161 | \r | |
162 | Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView,\r | |
163 | PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva\r | |
164 | fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.\r | |
165 | If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not\r | |
166 | a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while\r | |
167 | computing the fields of ImageContext, then the error status is returned in the ImageError field of\r | |
168 | ImageContext. \r | |
169 | \r | |
170 | @param ImageContext Pointer to the image context structure that describes the PE/COFF\r | |
171 | image that needs to be examined by this function.\r | |
172 | \r | |
173 | @retval RETURN_SUCCESS The information on the PE/COFF image was collected.\r | |
174 | @retval RETURN_INVALID_PARAMETER ImageContext is NULL.\r | |
175 | @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.\r | |
176 | \r | |
177 | **/\r | |
178 | RETURN_STATUS\r | |
179 | EFIAPI\r | |
180 | PeCoffLoaderGetImageInfo (\r | |
181 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
ed66e1bc | 182 | );\r |
fb3df220 | 183 | \r |
184 | /**\r | |
185 | Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().\r | |
186 | \r | |
187 | If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of\r | |
188 | ImageContext as the relocation base address. Otherwise, use the DestinationAddress field\r | |
189 | of ImageContext as the relocation base address. The caller must allocate the relocation\r | |
190 | fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function. \r | |
191 | If ImageContext is NULL, then ASSERT().\r | |
192 | \r | |
193 | @param ImageContext Pointer to the image context structure that describes the PE/COFF\r | |
194 | image that is being relocated.\r | |
195 | \r | |
196 | @retval RETURN_SUCCESS The PE/COFF image was relocated.\r | |
197 | Extended status information is in the ImageError field of ImageContext.\r | |
198 | @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.\r | |
199 | Extended status information is in the ImageError field of ImageContext.\r | |
200 | @retval RETURN_UNSUPPORTED A relocation record type is not supported.\r | |
201 | Extended status information is in the ImageError field of ImageContext.\r | |
202 | \r | |
203 | **/\r | |
204 | RETURN_STATUS\r | |
205 | EFIAPI\r | |
206 | PeCoffLoaderRelocateImage (\r | |
207 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
ed66e1bc | 208 | );\r |
fb3df220 | 209 | \r |
210 | /**\r | |
211 | Loads a PE/COFF image into memory.\r | |
212 | \r | |
213 | Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer\r | |
214 | specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate\r | |
215 | the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.\r | |
216 | The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.\r | |
217 | If ImageContext is NULL, then ASSERT().\r | |
218 | \r | |
219 | @param ImageContext Pointer to the image context structure that describes the PE/COFF\r | |
220 | image that is being loaded.\r | |
221 | \r | |
222 | @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by\r | |
223 | the ImageAddress and ImageSize fields of ImageContext.\r | |
224 | Extended status information is in the ImageError field of ImageContext.\r | |
225 | @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.\r | |
226 | Extended status information is in the ImageError field of ImageContext.\r | |
227 | @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.\r | |
228 | Extended status information is in the ImageError field of ImageContext.\r | |
229 | @retval RETURN_INVALID_PARAMETER The image address is invalid.\r | |
230 | Extended status information is in the ImageError field of ImageContext.\r | |
231 | \r | |
232 | **/\r | |
233 | RETURN_STATUS\r | |
234 | EFIAPI\r | |
235 | PeCoffLoaderLoadImage (\r | |
236 | IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
ed66e1bc | 237 | );\r |
fb3df220 | 238 | \r |
239 | \r | |
240 | /**\r | |
241 | ImageRead function that operates on a memory buffer whos base is passed into\r | |
242 | FileHandle. \r | |
243 | \r | |
244 | @param FileHandle Ponter to baes of the input stream\r | |
245 | @param FileOffset Offset to the start of the buffer\r | |
246 | @param ReadSize Number of bytes to copy into the buffer\r | |
247 | @param Buffer Location to place results of read\r | |
248 | \r | |
249 | @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into \r | |
250 | the buffer.\r | |
251 | **/\r | |
252 | RETURN_STATUS\r | |
253 | EFIAPI\r | |
254 | PeCoffLoaderImageReadFromMemory (\r | |
255 | IN VOID *FileHandle,\r | |
256 | IN UINTN FileOffset,\r | |
257 | IN OUT UINTN *ReadSize,\r | |
258 | OUT VOID *Buffer\r | |
ed66e1bc | 259 | );\r |
fb3df220 | 260 | \r |
261 | \r | |
262 | /**\r | |
263 | Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI\r | |
264 | runtime. \r | |
265 | \r | |
266 | PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply\r | |
267 | the fixups with a virtual mapping.\r | |
268 | \r | |
269 | \r | |
270 | @param ImageBase Base address of relocated image\r | |
271 | @param VirtImageBase Virtual mapping for ImageBase\r | |
272 | @param ImageSize Size of the image to relocate\r | |
273 | @param RelocationData Location to place results of read\r | |
274 | \r | |
275 | **/\r | |
276 | VOID\r | |
277 | EFIAPI\r | |
278 | PeCoffLoaderRelocateImageForRuntime (\r | |
279 | IN PHYSICAL_ADDRESS ImageBase,\r | |
280 | IN PHYSICAL_ADDRESS VirtImageBase,\r | |
281 | IN UINTN ImageSize,\r | |
282 | IN VOID *RelocationData\r | |
ed66e1bc | 283 | );\r |
fb3df220 | 284 | \r |
3d7b0992 LG |
285 | /**\r |
286 | Unloads a loaded PE/COFF image from memory and releases its taken resource.\r | |
287 | \r | |
288 | For NT32 emulator, the PE/COFF image loaded by system needs to release.\r | |
289 | For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded, \r | |
290 | this function can simply return RETURN_SUCCESS.\r | |
291 | \r | |
292 | @param ImageContext Pointer to the image context structure that describes the PE/COFF\r | |
293 | image to be unloaded.\r | |
fb3df220 | 294 | \r |
3d7b0992 LG |
295 | @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.\r |
296 | **/\r | |
297 | RETURN_STATUS\r | |
298 | EFIAPI\r | |
299 | PeCoffLoaderUnloadImage (\r | |
300 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
ed66e1bc | 301 | );\r |
fb3df220 | 302 | #endif\r |