]> git.proxmox.com Git - mirror_edk2.git/blob - Nt32Pkg/Library/PeiCoreNt32PeCoffLib/PeiCoreNt32PeCoffLib.c
1. Replace PeCoffLoader library by PeCoff lib for PeiCore, DxeIpl and DxeMain.
[mirror_edk2.git] / Nt32Pkg / Library / PeiCoreNt32PeCoffLib / PeiCoreNt32PeCoffLib.c
1 /*++
2
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
8
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.
11
12 Module Name:
13
14 PeiCoreNt32PeCoffLib.c
15
16 Abstract:
17
18 Wrap the Nt32 PE/COFF loader with the PE COFF LOADER guid structure
19 to produce PeCoff library class.
20
21
22 --*/
23
24 #include <PiPei.h>
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>
30
31 EFI_PEI_PE_COFF_LOADER_PROTOCOL *mPeiEfiPeiPeCoffLoader = NULL;
32
33 /**
34 The function caches the pointer of PeCofferLoader guid structure
35 into the guid data hob.
36
37 The funtion must be called after PeCofferLoader guid structure is installed.
38 It will ASSERT() if PeCofferLoader guid structure is not installed.
39
40 @retval EFI_SUCCESS PeCofferLoader guid structure is found.
41
42 **/
43 EFI_STATUS
44 EFIAPI
45 GetPeCoffLoaderStucture (
46 )
47 {
48 EFI_STATUS Status;
49 EFI_HOB_GUID_TYPE *GuidHob;
50
51 Status = EFI_NOT_FOUND;
52
53 //
54 // Try to get guid data hob that contains PeCoffLoader guid structure.
55 //
56 GuidHob = GetFirstGuidHob (&gEfiPeiPeCoffLoaderGuid);
57
58 if (GuidHob == NULL) {
59 //
60 // GuidHob is not ready, try to locate PeCoffLoader guid structure.
61 //
62 Status = PeiServicesLocatePpi (
63 &gEfiPeiPeCoffLoaderGuid,
64 0,
65 NULL,
66 &mPeiEfiPeiPeCoffLoader
67 );
68
69 //
70 // PeCofferLoader guid structure must be installed before this library runs.
71 //
72 ASSERT_EFI_ERROR (Status);
73
74 //
75 // Build guid data hob of PeCofferLoader guid structure for DXE module use.
76 //
77 BuildGuidDataHob (
78 &gEfiPeiPeCoffLoaderGuid,
79 (VOID *) &mPeiEfiPeiPeCoffLoader,
80 sizeof (VOID *)
81 );
82 } else {
83 //
84 // Get PeCofferLoader guid structure directly from guid hob data.
85 //
86 mPeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)(*(UINTN *)(GET_GUID_HOB_DATA (GuidHob)));
87 }
88
89 return EFI_SUCCESS;
90 }
91
92 /**
93 Retrieves information about a PE/COFF image.
94
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
101 ImageContext.
102
103 @param ImageContext Pointer to the image context structure that describes the PE/COFF
104 image that needs to be examined by this function.
105
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.
109
110 **/
111 RETURN_STATUS
112 EFIAPI
113 PeCoffLoaderGetImageInfo (
114 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
115 )
116 {
117 if (mPeiEfiPeiPeCoffLoader == NULL) {
118 GetPeCoffLoaderStucture ();
119 }
120 return mPeiEfiPeiPeCoffLoader->GetImageInfo (mPeiEfiPeiPeCoffLoader, ImageContext);
121 }
122
123 /**
124 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
125
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().
131
132 @param ImageContext Pointer to the image context structure that describes the PE/COFF
133 image that is being relocated.
134
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.
141
142 **/
143 RETURN_STATUS
144 EFIAPI
145 PeCoffLoaderRelocateImage (
146 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
147 )
148 {
149 if (mPeiEfiPeiPeCoffLoader == NULL) {
150 GetPeCoffLoaderStucture ();
151 }
152 return mPeiEfiPeiPeCoffLoader->RelocateImage (mPeiEfiPeiPeCoffLoader, ImageContext);
153 }
154
155 /**
156 Loads a PE/COFF image into memory.
157
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().
163
164 @param ImageContext Pointer to the image context structure that describes the PE/COFF
165 image that is being loaded.
166
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.
176
177 **/
178 RETURN_STATUS
179 EFIAPI
180 PeCoffLoaderLoadImage (
181 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
182 )
183 {
184 if (mPeiEfiPeiPeCoffLoader == NULL) {
185 GetPeCoffLoaderStucture ();
186 }
187 return mPeiEfiPeiPeCoffLoader->LoadImage (mPeiEfiPeiPeCoffLoader, ImageContext);
188 }
189
190 /**
191 ImageRead function that operates on a memory buffer whos base is passed into
192 FileHandle.
193
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
198
199 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
200 the buffer.
201 **/
202 RETURN_STATUS
203 EFIAPI
204 PeCoffLoaderImageReadFromMemory (
205 IN VOID *FileHandle,
206 IN UINTN FileOffset,
207 IN OUT UINTN *ReadSize,
208 OUT VOID *Buffer
209 )
210 {
211 return RETURN_UNSUPPORTED;
212 }
213
214
215 /**
216 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
217 runtime.
218
219 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
220 the fixups with a virtual mapping.
221
222
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
227
228 **/
229 VOID
230 EFIAPI
231 PeCoffLoaderRelocateImageForRuntime (
232 IN PHYSICAL_ADDRESS ImageBase,
233 IN PHYSICAL_ADDRESS VirtImageBase,
234 IN UINTN ImageSize,
235 IN VOID *RelocationData
236 )
237 {
238 }
239
240 /**
241 Unloads a loaded PE/COFF image from memory and releases its taken resource.
242
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.
246
247 @param ImageContext Pointer to the image context structure that describes the PE/COFF
248 image to be unloaded.
249
250 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
251 **/
252 RETURN_STATUS
253 EFIAPI
254 PeCoffLoaderUnloadImage (
255 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
256 )
257 {
258 if (mPeiEfiPeiPeCoffLoader == NULL) {
259 GetPeCoffLoaderStucture ();
260 }
261 return mPeiEfiPeiPeCoffLoader->UnloadImage (mPeiEfiPeiPeCoffLoader, ImageContext);
262 }