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