3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 PeiCoreNt32PeCoffLib.c
18 Wrap the Nt32 PE/COFF loader with the PE COFF LOADER guid structure
19 to produce PeCoff library class.
25 #include <Guid/PeiPeCoffLoader.h>
26 #include <Library/DebugLib.h>
27 #include <Library/PeCoffLib.h>
28 #include <Library/HobLib.h>
29 #include <Library/PeiServicesLib.h>
31 EFI_PEI_PE_COFF_LOADER_PROTOCOL
*mPeiEfiPeiPeCoffLoader
= NULL
;
34 The function caches the pointer of PeCofferLoader guid structure
35 into the guid data hob.
37 The funtion must be called after PeCofferLoader guid structure is installed.
38 It will ASSERT() if PeCofferLoader guid structure is not installed.
40 @retval EFI_SUCCESS PeCofferLoader guid structure is found.
45 GetPeCoffLoaderStucture (
49 EFI_HOB_GUID_TYPE
*GuidHob
;
51 Status
= EFI_NOT_FOUND
;
54 // Try to get guid data hob that contains PeCoffLoader guid structure.
56 GuidHob
= GetFirstGuidHob (&gEfiPeiPeCoffLoaderGuid
);
58 if (GuidHob
== NULL
) {
60 // GuidHob is not ready, try to locate PeCoffLoader guid structure.
62 Status
= PeiServicesLocatePpi (
63 &gEfiPeiPeCoffLoaderGuid
,
66 &mPeiEfiPeiPeCoffLoader
70 // PeCofferLoader guid structure must be installed before this library runs.
72 ASSERT_EFI_ERROR (Status
);
75 // Build guid data hob of PeCofferLoader guid structure for DXE module use.
78 &gEfiPeiPeCoffLoaderGuid
,
79 (VOID
*) &mPeiEfiPeiPeCoffLoader
,
84 // Get PeCofferLoader guid structure directly from guid hob data.
86 mPeiEfiPeiPeCoffLoader
= (EFI_PEI_PE_COFF_LOADER_PROTOCOL
*)(*(UINTN
*)(GET_GUID_HOB_DATA (GuidHob
)));
93 Retrieves information about a PE/COFF image.
95 Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView,
96 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
97 fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
98 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not
99 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while
100 computing the fields of ImageContext, then the error status is returned in the ImageError field of
103 @param ImageContext Pointer to the image context structure that describes the PE/COFF
104 image that needs to be examined by this function.
106 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
107 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
108 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
113 PeCoffLoaderGetImageInfo (
114 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
117 if (mPeiEfiPeiPeCoffLoader
== NULL
) {
118 GetPeCoffLoaderStucture ();
120 return mPeiEfiPeiPeCoffLoader
->GetImageInfo (mPeiEfiPeiPeCoffLoader
, ImageContext
);
124 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
126 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
127 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
128 of ImageContext as the relocation base address. The caller must allocate the relocation
129 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
130 If ImageContext is NULL, then ASSERT().
132 @param ImageContext Pointer to the image context structure that describes the PE/COFF
133 image that is being relocated.
135 @retval RETURN_SUCCESS The PE/COFF image was relocated.
136 Extended status information is in the ImageError field of ImageContext.
137 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
138 Extended status information is in the ImageError field of ImageContext.
139 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
140 Extended status information is in the ImageError field of ImageContext.
145 PeCoffLoaderRelocateImage (
146 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
149 if (mPeiEfiPeiPeCoffLoader
== NULL
) {
150 GetPeCoffLoaderStucture ();
152 return mPeiEfiPeiPeCoffLoader
->RelocateImage (mPeiEfiPeiPeCoffLoader
, ImageContext
);
156 Loads a PE/COFF image into memory.
158 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
159 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
160 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
161 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
162 If ImageContext is NULL, then ASSERT().
164 @param ImageContext Pointer to the image context structure that describes the PE/COFF
165 image that is being loaded.
167 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
168 the ImageAddress and ImageSize fields of ImageContext.
169 Extended status information is in the ImageError field of ImageContext.
170 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
171 Extended status information is in the ImageError field of ImageContext.
172 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
173 Extended status information is in the ImageError field of ImageContext.
174 @retval RETURN_INVALID_PARAMETER The image address is invalid.
175 Extended status information is in the ImageError field of ImageContext.
180 PeCoffLoaderLoadImage (
181 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
184 if (mPeiEfiPeiPeCoffLoader
== NULL
) {
185 GetPeCoffLoaderStucture ();
187 return mPeiEfiPeiPeCoffLoader
->LoadImage (mPeiEfiPeiPeCoffLoader
, ImageContext
);
191 ImageRead function that operates on a memory buffer whos base is passed into
194 @param FileHandle Ponter to baes of the input stream
195 @param FileOffset Offset to the start of the buffer
196 @param ReadSize Number of bytes to copy into the buffer
197 @param Buffer Location to place results of read
199 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
204 PeCoffLoaderImageReadFromMemory (
207 IN OUT UINTN
*ReadSize
,
211 return RETURN_UNSUPPORTED
;
216 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
219 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
220 the fixups with a virtual mapping.
223 @param ImageBase Base address of relocated image
224 @param VirtImageBase Virtual mapping for ImageBase
225 @param ImageSize Size of the image to relocate
226 @param RelocationData Location to place results of read
231 PeCoffLoaderRelocateImageForRuntime (
232 IN PHYSICAL_ADDRESS ImageBase
,
233 IN PHYSICAL_ADDRESS VirtImageBase
,
235 IN VOID
*RelocationData
241 Unloads a loaded PE/COFF image from memory and releases its taken resource.
243 For NT32 emulator, the PE/COFF image loaded by system needs to release.
244 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
245 this function can simply return RETURN_SUCCESS.
247 @param ImageContext Pointer to the image context structure that describes the PE/COFF
248 image to be unloaded.
250 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
254 PeCoffLoaderUnloadImage (
255 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
258 if (mPeiEfiPeiPeCoffLoader
== NULL
) {
259 GetPeCoffLoaderStucture ();
261 return mPeiEfiPeiPeCoffLoader
->UnloadImage (mPeiEfiPeiPeCoffLoader
, ImageContext
);