]> git.proxmox.com Git - mirror_edk2.git/blob - Nt32Pkg/Library/PeiNt32PeCoffLib/PeiNt32PeCoffLib.c
1. Replace PeCoffLoader library by PeCoff lib for PeiCore, DxeIpl and DxeMain.
[mirror_edk2.git] / Nt32Pkg / Library / PeiNt32PeCoffLib / PeiNt32PeCoffLib.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 PeiNt32PeCoffLib.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
30 EFI_PEI_PE_COFF_LOADER_PROTOCOL *mPeiEfiPeiPeCoffLoader;
31
32 /**
33 The constructor function caches the pointer of PeCofferLoader guid structure
34 into the guid data hob.
35
36 The constructor must be called after PeCofferLoader guid structure is installed.
37 It will ASSERT() if PeCofferLoader guid structure is not installed.
38
39 @param FfsHeader Pointer to FFS header the loaded driver.
40 @param PeiServices Pointer to the PEI services.
41
42 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
43
44 **/
45 EFI_STATUS
46 EFIAPI
47 PeiNt32PeCoffLibConstructor (
48 IN EFI_FFS_FILE_HEADER *FfsHeader,
49 IN EFI_PEI_SERVICES **PeiServices
50 )
51 {
52 EFI_STATUS Status;
53 EFI_HOB_GUID_TYPE *GuidHob;
54
55 Status = EFI_NOT_FOUND;
56
57 //
58 // Try to get guid data hob that contains PeCoffLoader guid structure.
59 //
60 GuidHob = GetFirstGuidHob (&gEfiPeiPeCoffLoaderGuid);
61
62 if (GuidHob == NULL) {
63 //
64 // GuidHob is not ready, try to locate PeCoffLoader guid structure.
65 //
66 Status = (*PeiServices)->LocatePpi (
67 PeiServices,
68 &gEfiPeiPeCoffLoaderGuid,
69 0,
70 NULL,
71 &mPeiEfiPeiPeCoffLoader
72 );
73
74 //
75 // PeCofferLoader guid structure must be installed before this library runs.
76 //
77 ASSERT_EFI_ERROR (Status);
78
79 //
80 // Build guid data hob of PeCofferLoader guid structure for DXE module use.
81 //
82 BuildGuidDataHob (
83 &gEfiPeiPeCoffLoaderGuid,
84 (VOID *) &mPeiEfiPeiPeCoffLoader,
85 sizeof (VOID *)
86 );
87 } else {
88 //
89 // Get PeCofferLoader guid structure directly from guid hob data.
90 //
91 mPeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)(*(UINTN *)(GET_GUID_HOB_DATA (GuidHob)));
92 }
93
94 return EFI_SUCCESS;
95 }
96
97 /**
98 Retrieves information about a PE/COFF image.
99
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
106 ImageContext.
107
108 @param ImageContext Pointer to the image context structure that describes the PE/COFF
109 image that needs to be examined by this function.
110
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.
114
115 **/
116 RETURN_STATUS
117 EFIAPI
118 PeCoffLoaderGetImageInfo (
119 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
120 )
121 {
122 return mPeiEfiPeiPeCoffLoader->GetImageInfo (mPeiEfiPeiPeCoffLoader, ImageContext);
123 }
124
125 /**
126 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().
127
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().
133
134 @param ImageContext Pointer to the image context structure that describes the PE/COFF
135 image that is being relocated.
136
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.
143
144 **/
145 RETURN_STATUS
146 EFIAPI
147 PeCoffLoaderRelocateImage (
148 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
149 )
150 {
151 return mPeiEfiPeiPeCoffLoader->RelocateImage (mPeiEfiPeiPeCoffLoader, ImageContext);
152 }
153
154 /**
155 Loads a PE/COFF image into memory.
156
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().
162
163 @param ImageContext Pointer to the image context structure that describes the PE/COFF
164 image that is being loaded.
165
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.
175
176 **/
177 RETURN_STATUS
178 EFIAPI
179 PeCoffLoaderLoadImage (
180 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
181 )
182 {
183 return mPeiEfiPeiPeCoffLoader->LoadImage (mPeiEfiPeiPeCoffLoader, ImageContext);
184 }
185
186 /**
187 ImageRead function that operates on a memory buffer whos base is passed into
188 FileHandle.
189
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
194
195 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into
196 the buffer.
197 **/
198 RETURN_STATUS
199 EFIAPI
200 PeCoffLoaderImageReadFromMemory (
201 IN VOID *FileHandle,
202 IN UINTN FileOffset,
203 IN OUT UINTN *ReadSize,
204 OUT VOID *Buffer
205 )
206 {
207 return RETURN_UNSUPPORTED;
208 }
209
210
211 /**
212 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI
213 runtime.
214
215 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply
216 the fixups with a virtual mapping.
217
218
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
223
224 **/
225 VOID
226 EFIAPI
227 PeCoffLoaderRelocateImageForRuntime (
228 IN PHYSICAL_ADDRESS ImageBase,
229 IN PHYSICAL_ADDRESS VirtImageBase,
230 IN UINTN ImageSize,
231 IN VOID *RelocationData
232 )
233 {
234 }
235
236 /**
237 Unloads a loaded PE/COFF image from memory and releases its taken resource.
238
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.
242
243 @param ImageContext Pointer to the image context structure that describes the PE/COFF
244 image to be unloaded.
245
246 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.
247 **/
248 RETURN_STATUS
249 EFIAPI
250 PeCoffLoaderUnloadImage (
251 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
252 )
253 {
254 return mPeiEfiPeiPeCoffLoader->UnloadImage (mPeiEfiPeiPeCoffLoader, ImageContext);
255 }