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