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.
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>
30 EFI_PEI_PE_COFF_LOADER_PROTOCOL
*mPeiEfiPeiPeCoffLoader
;
33 The constructor function caches the pointer of PeCofferLoader guid structure
34 into the guid data hob.
36 The constructor must be called after PeCofferLoader guid structure is installed.
37 It will ASSERT() if PeCofferLoader guid structure is not installed.
39 @param FfsHeader Pointer to FFS header the loaded driver.
40 @param PeiServices Pointer to the PEI services.
42 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
47 PeiNt32PeCoffLibConstructor (
48 IN EFI_FFS_FILE_HEADER
*FfsHeader
,
49 IN EFI_PEI_SERVICES
**PeiServices
53 EFI_HOB_GUID_TYPE
*GuidHob
;
55 Status
= EFI_NOT_FOUND
;
58 // Try to get guid data hob that contains PeCoffLoader guid structure.
60 GuidHob
= GetFirstGuidHob (&gEfiPeiPeCoffLoaderGuid
);
62 if (GuidHob
== NULL
) {
64 // GuidHob is not ready, try to locate PeCoffLoader guid structure.
66 Status
= (*PeiServices
)->LocatePpi (
68 &gEfiPeiPeCoffLoaderGuid
,
71 &mPeiEfiPeiPeCoffLoader
75 // PeCofferLoader guid structure must be installed before this library runs.
77 ASSERT_EFI_ERROR (Status
);
80 // Build guid data hob of PeCofferLoader guid structure for DXE module use.
83 &gEfiPeiPeCoffLoaderGuid
,
84 (VOID
*) &mPeiEfiPeiPeCoffLoader
,
89 // Get PeCofferLoader guid structure directly from guid hob data.
91 mPeiEfiPeiPeCoffLoader
= (EFI_PEI_PE_COFF_LOADER_PROTOCOL
*)(*(UINTN
*)(GET_GUID_HOB_DATA (GuidHob
)));
98 Retrieves information about a PE/COFF image.
100 Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView,
101 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva
102 fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.
103 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not
104 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while
105 computing the fields of ImageContext, then the error status is returned in the ImageError field of
108 @param ImageContext Pointer to the image context structure that describes the PE/COFF
109 image that needs to be examined by this function.
111 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.
112 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.
113 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.
118 PeCoffLoaderGetImageInfo (
119 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
122 return mPeiEfiPeiPeCoffLoader
->GetImageInfo (mPeiEfiPeiPeCoffLoader
, ImageContext
);
126 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
128 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of
129 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field
130 of ImageContext as the relocation base address. The caller must allocate the relocation
131 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.
132 If ImageContext is NULL, then ASSERT().
134 @param ImageContext Pointer to the image context structure that describes the PE/COFF
135 image that is being relocated.
137 @retval RETURN_SUCCESS The PE/COFF image was relocated.
138 Extended status information is in the ImageError field of ImageContext.
139 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.
140 Extended status information is in the ImageError field of ImageContext.
141 @retval RETURN_UNSUPPORTED A relocation record type is not supported.
142 Extended status information is in the ImageError field of ImageContext.
147 PeCoffLoaderRelocateImage (
148 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
151 return mPeiEfiPeiPeCoffLoader
->RelocateImage (mPeiEfiPeiPeCoffLoader
, ImageContext
);
155 Loads a PE/COFF image into memory.
157 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
158 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate
159 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.
160 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.
161 If ImageContext is NULL, then ASSERT().
163 @param ImageContext Pointer to the image context structure that describes the PE/COFF
164 image that is being loaded.
166 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
167 the ImageAddress and ImageSize fields of ImageContext.
168 Extended status information is in the ImageError field of ImageContext.
169 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
170 Extended status information is in the ImageError field of ImageContext.
171 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
172 Extended status information is in the ImageError field of ImageContext.
173 @retval RETURN_INVALID_PARAMETER The image address is invalid.
174 Extended status information is in the ImageError field of ImageContext.
179 PeCoffLoaderLoadImage (
180 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
183 return mPeiEfiPeiPeCoffLoader
->LoadImage (mPeiEfiPeiPeCoffLoader
, ImageContext
);
187 ImageRead function that operates on a memory buffer whos base is passed into
190 @param FileHandle Ponter to baes of the input stream
191 @param FileOffset Offset to the start of the buffer
192 @param ReadSize Number of bytes to copy into the buffer
193 @param Buffer Location to place results of read
195 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
200 PeCoffLoaderImageReadFromMemory (
203 IN OUT UINTN
*ReadSize
,
207 return RETURN_UNSUPPORTED
;
212 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
215 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
216 the fixups with a virtual mapping.
219 @param ImageBase Base address of relocated image
220 @param VirtImageBase Virtual mapping for ImageBase
221 @param ImageSize Size of the image to relocate
222 @param RelocationData Location to place results of read
227 PeCoffLoaderRelocateImageForRuntime (
228 IN PHYSICAL_ADDRESS ImageBase
,
229 IN PHYSICAL_ADDRESS VirtImageBase
,
231 IN VOID
*RelocationData
237 Unloads a loaded PE/COFF image from memory and releases its taken resource.
239 For NT32 emulator, the PE/COFF image loaded by system needs to release.
240 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,
241 this function can simply return RETURN_SUCCESS.
243 @param ImageContext Pointer to the image context structure that describes the PE/COFF
244 image to be unloaded.
246 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
250 PeCoffLoaderUnloadImage (
251 IN PE_COFF_LOADER_IMAGE_CONTEXT
*ImageContext
254 return mPeiEfiPeiPeCoffLoader
->UnloadImage (mPeiEfiPeiPeCoffLoader
, ImageContext
);