]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Library/DxeHobPeCoffLib/DxeHobPeCoff.c
Rmove info files
[mirror_edk2.git] / EmbeddedPkg / Library / DxeHobPeCoffLib / DxeHobPeCoff.c
CommitLineData
607a0df6 1/** @file\r
2 PE/COFF Loader Library implementation that wraps a protocol passed up from \r
3 SEC/PEI via a HOB. This is done to save space.\r
4\r
5 Copyright (c) 2010, Intel Corporation<BR>\r
6 Portions copyright (c) 2008-2010 Apple Inc. All rights reserved.<BR>\r
7 All rights reserved. This program and the accompanying materials\r
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include <PiDxe.h>\r
18#include <Library/DebugLib.h>\r
19#include <Library/HobLib.h>\r
20\r
21#include <Protocol/PeCoffLoader.h>\r
22\r
23\r
24/**\r
25 Retrieves information about a PE/COFF image.\r
26\r
27 Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize, \r
28 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and \r
29 DebugDirectoryEntryRva fields of the ImageContext structure. \r
30 If ImageContext is NULL, then return RETURN_INVALID_PARAMETER. \r
31 If the PE/COFF image accessed through the ImageRead service in the ImageContext \r
32 structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED. \r
33 If any errors occur while computing the fields of ImageContext, \r
34 then the error status is returned in the ImageError field of ImageContext. \r
35 If the image is a TE image, then SectionAlignment is set to 0.\r
36 The ImageRead and Handle fields of ImageContext structure must be valid prior \r
37 to invoking this service.\r
38\r
39 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
40 image that needs to be examined by this function.\r
41\r
42 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.\r
43 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.\r
44 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.\r
45\r
46**/\r
47RETURN_STATUS\r
48EFIAPI\r
49PeCoffLoaderGetImageInfo (\r
50 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
51 )\r
52{\r
53 return gPeCoffLoader->GetImageInfo (ImageContext);\r
54}\r
55\r
56\r
57/**\r
58 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().\r
59\r
60 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of\r
61 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field\r
62 of ImageContext as the relocation base address. The caller must allocate the relocation\r
63 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.\r
64 \r
65 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, \r
66 ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, \r
67 DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of \r
68 the ImageContext structure must be valid prior to invoking this service.\r
69 \r
70 If ImageContext is NULL, then ASSERT().\r
71\r
72 Note that if the platform does not maintain coherency between the instruction cache(s) and the data\r
73 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations\r
74 prior to transferring control to a PE/COFF image that is loaded using this library.\r
75\r
76 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
77 image that is being relocated.\r
78\r
79 @retval RETURN_SUCCESS The PE/COFF image was relocated.\r
80 Extended status information is in the ImageError field of ImageContext.\r
81 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.\r
82 Extended status information is in the ImageError field of ImageContext.\r
83 @retval RETURN_UNSUPPORTED A relocation record type is not supported.\r
84 Extended status information is in the ImageError field of ImageContext.\r
85\r
86**/\r
87RETURN_STATUS\r
88EFIAPI\r
89PeCoffLoaderRelocateImage (\r
90 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
91 )\r
92{\r
93 return gPeCoffLoader->RelocateImage (ImageContext);\r
94}\r
95\r
96/**\r
97 Loads a PE/COFF image into memory.\r
98\r
99 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer\r
100 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate\r
101 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.\r
102 The EntryPoint, FixupDataSize, CodeView, PdbPointer and HiiResourceData fields of ImageContext are computed.\r
103 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize, \r
104 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva \r
105 fields of the ImageContext structure must be valid prior to invoking this service.\r
106 \r
107 If ImageContext is NULL, then ASSERT().\r
108\r
109 Note that if the platform does not maintain coherency between the instruction cache(s) and the data\r
110 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations\r
111 prior to transferring control to a PE/COFF image that is loaded using this library.\r
112\r
113 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
114 image that is being loaded.\r
115\r
116 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by\r
117 the ImageAddress and ImageSize fields of ImageContext.\r
118 Extended status information is in the ImageError field of ImageContext.\r
119 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.\r
120 Extended status information is in the ImageError field of ImageContext.\r
121 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.\r
122 Extended status information is in the ImageError field of ImageContext.\r
123 @retval RETURN_INVALID_PARAMETER The image address is invalid.\r
124 Extended status information is in the ImageError field of ImageContext.\r
125\r
126**/\r
127RETURN_STATUS\r
128EFIAPI\r
129PeCoffLoaderLoadImage (\r
130 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
131 )\r
132{\r
133 return gPeCoffLoader->LoadImage (ImageContext);\r
134}\r
135\r
136\r
137\r
138/**\r
139 Reads contents of a PE/COFF image from a buffer in system memory.\r
140 \r
141 This is the default implementation of a PE_COFF_LOADER_READ_FILE function \r
142 that assumes FileHandle pointer to the beginning of a PE/COFF image. \r
143 This function reads contents of the PE/COFF image that starts at the system memory \r
144 address specified by FileHandle. The read operation copies ReadSize bytes from the \r
145 PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer. \r
146 The size of the buffer actually read is returned in ReadSize.\r
147 \r
148 If FileHandle is NULL, then ASSERT().\r
149 If ReadSize is NULL, then ASSERT().\r
150 If Buffer is NULL, then ASSERT().\r
151\r
152 @param FileHandle Pointer to base of the input stream\r
153 @param FileOffset Offset into the PE/COFF image to begin the read operation.\r
154 @param ReadSize On input, the size in bytes of the requested read operation. \r
155 On output, the number of bytes actually read.\r
156 @param Buffer Output buffer that contains the data read from the PE/COFF image.\r
157\r
158 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into \r
159 the buffer.\r
160**/\r
161RETURN_STATUS\r
162EFIAPI\r
163PeCoffLoaderImageReadFromMemory (\r
164 IN VOID *FileHandle,\r
165 IN UINTN FileOffset,\r
166 IN OUT UINTN *ReadSize,\r
167 OUT VOID *Buffer\r
168 )\r
169{\r
170 return gPeCoffLoader->RelocateImageForRuntime (\r
171 *FileHandle,\r
172 FileOffset,\r
173 *ReadSize,\r
174 *Buffer\r
175 );\r
176 \r
177}\r
178\r
179\r
180\r
181/**\r
182 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI\r
183 runtime. \r
184 \r
185 This function reapplies relocation fixups to the PE/COFF image specified by ImageBase \r
186 and ImageSize so the image will execute correctly when the PE/COFF image is mapped \r
187 to the address specified by VirtualImageBase. RelocationData must be identical \r
188 to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure \r
189 after this PE/COFF image was relocated with PeCoffLoaderRelocateImage().\r
190\r
191 Note that if the platform does not maintain coherency between the instruction cache(s) and the data\r
192 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations\r
193 prior to transferring control to a PE/COFF image that is loaded using this library.\r
194\r
195 @param ImageBase Base address of a PE/COFF image that has been loaded \r
196 and relocated into system memory.\r
197 @param VirtImageBase The request virtual address that the PE/COFF image is to\r
198 be fixed up for.\r
199 @param ImageSize The size, in bytes, of the PE/COFF image.\r
200 @param RelocationData A pointer to the relocation data that was collected when the PE/COFF \r
201 image was relocated using PeCoffLoaderRelocateImage().\r
202 \r
203**/\r
204VOID\r
205EFIAPI\r
206PeCoffLoaderRelocateImageForRuntime (\r
207 IN PHYSICAL_ADDRESS ImageBase,\r
208 IN PHYSICAL_ADDRESS VirtImageBase,\r
209 IN UINTN ImageSize,\r
210 IN VOID *RelocationData\r
211 )\r
212{\r
213 return gPeCoffLoader->RelocateImageForRuntime (\r
214 ImageBase,\r
215 VirtImageBase,\r
216 ImageSize,\r
217 *RelocationData\r
218 );\r
219}\r
220\r
221\r
222/**\r
223 Unloads a loaded PE/COFF image from memory and releases its taken resource.\r
224 Releases any environment specific resources that were allocated when the image \r
225 specified by ImageContext was loaded using PeCoffLoaderLoadImage(). \r
226 \r
227 For NT32 emulator, the PE/COFF image loaded by system needs to release.\r
228 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded, \r
229 this function can simply return RETURN_SUCCESS.\r
230 \r
231 If ImageContext is NULL, then ASSERT().\r
232 \r
233 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
234 image to be unloaded.\r
235\r
236 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.\r
237**/\r
238RETURN_STATUS\r
239EFIAPI\r
240PeCoffLoaderUnloadImage (\r
241 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
242 )\r
243{\r
244 return gPeCoffLoader->UnloadImage (ImageContext);\r
245}\r
246\r
247\r
248\r
249/**\r
250 Register LzmaDecompress and LzmaDecompressGetInfo handlers with LzmaCustomerDecompressGuid.\r
251\r
252 @retval RETURN_SUCCESS Register successfully.\r
253 @retval RETURN_OUT_OF_RESOURCES No enough memory to store this handler.\r
254**/\r
255EFI_STATUS\r
256EFIAPI\r
257DxeHobPeCoffLibConstructor (\r
258 )\r
259{\r
260 PROTOCOL_HOB *Hob;\r
261 \r
262 Hob = GetFirstGuidHob (&gPeCoffLoaderProtocolGuid);\r
263 if (Hob == NULL) {\r
264 return EFI_NOT_FOUND;\r
265 }\r
266 \r
267 gPeCoffLoader = Hob->Interface;\r
268}\r
269\r
270\r