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