]> git.proxmox.com Git - mirror_edk2.git/blame - UnixPkg/Library/PeiUnixPeCoffLib/PeiUnixPeCoffLib.c
Apply PeiServicesLib
[mirror_edk2.git] / UnixPkg / Library / PeiUnixPeCoffLib / PeiUnixPeCoffLib.c
CommitLineData
804405e7 1/**@file\r
2\r
3Copyright (c) 2006 - 2008, Intel Corporation\r
4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 PeiUnixPeCoffLib.c\r
15\r
16Abstract:\r
17\r
18 Wrap the Unix PE/COFF loader with the PE COFF LOADER guid structure\r
19 to produce PeCoff library class.\r
20\r
21\r
22**/\r
23\r
24#include <PiPei.h>\r
25#include <Guid/PeiPeCoffLoader.h>\r
26#include <Library/DebugLib.h>\r
27#include <Library/PeCoffLib.h>\r
28#include <Library/HobLib.h>\r
6c365805 29#include <Library/PeiServicesLib.h>\r
804405e7 30\r
31EFI_PEI_PE_COFF_LOADER_PROTOCOL *mPeiEfiPeiPeCoffLoader;\r
32\r
33/**\r
34 The constructor function caches the pointer of PeCofferLoader guid structure\r
35 into the guid data hob.\r
36\r
37 The constructor must be called after PeCofferLoader guid structure is installed.\r
38 It will ASSERT() if PeCofferLoader guid structure is not installed.\r
39\r
40 @param FfsHeader Pointer to FFS header the loaded driver.\r
41 @param PeiServices Pointer to the PEI services.\r
42\r
43 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
44\r
45**/\r
46EFI_STATUS\r
47EFIAPI\r
48PeiUnixPeCoffLibConstructor (\r
7a944d06 49 IN EFI_PEI_FILE_HANDLE FileHandle,\r
50 IN CONST EFI_PEI_SERVICES **PeiServices\r
804405e7 51 )\r
52{\r
53 EFI_STATUS Status;\r
54 EFI_HOB_GUID_TYPE *GuidHob;\r
55\r
56 Status = EFI_NOT_FOUND;\r
57 \r
58 //\r
59 // Try to get guid data hob that contains PeCoffLoader guid structure.\r
60 //\r
61 GuidHob = GetFirstGuidHob (&gEfiPeiPeCoffLoaderGuid);\r
62\r
63 if (GuidHob == NULL) {\r
64 //\r
65 // GuidHob is not ready, try to locate PeCoffLoader guid structure.\r
66 //\r
6c365805 67 Status = PeiServicesLocatePpi (\r
68 &gEfiPeiPeCoffLoaderGuid,\r
69 0,\r
70 NULL,\r
71 &mPeiEfiPeiPeCoffLoader\r
72 );\r
73 //\r
804405e7 74 // PeCofferLoader guid structure must be installed before this library runs.\r
75 //\r
76 ASSERT_EFI_ERROR (Status);\r
77 \r
78 //\r
79 // Build guid data hob of PeCofferLoader guid structure for DXE module use. \r
80 //\r
81 BuildGuidDataHob (\r
82 &gEfiPeiPeCoffLoaderGuid,\r
83 (VOID *) &mPeiEfiPeiPeCoffLoader,\r
84 sizeof (VOID *)\r
85 );\r
86 } else {\r
87 //\r
88 // Get PeCofferLoader guid structure directly from guid hob data.\r
89 //\r
90 mPeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)(*(UINTN *)(GET_GUID_HOB_DATA (GuidHob)));\r
91 }\r
92\r
93 return EFI_SUCCESS;\r
94}\r
95\r
96/**\r
97 Retrieves information about a PE/COFF image.\r
98\r
99 Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView,\r
100 PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva\r
101 fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.\r
102 If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not\r
103 a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while\r
104 computing the fields of ImageContext, then the error status is returned in the ImageError field of\r
105 ImageContext. \r
106\r
107 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
108 image that needs to be examined by this function.\r
109\r
110 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.\r
111 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.\r
112 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.\r
113\r
114**/\r
115RETURN_STATUS\r
116EFIAPI\r
117PeCoffLoaderGetImageInfo (\r
118 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
119 )\r
120{\r
121 return mPeiEfiPeiPeCoffLoader->GetImageInfo (mPeiEfiPeiPeCoffLoader, ImageContext);\r
122}\r
123\r
124/**\r
125 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().\r
126\r
127 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of\r
128 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field\r
129 of ImageContext as the relocation base address. The caller must allocate the relocation\r
130 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function. \r
131 If ImageContext is NULL, then ASSERT().\r
132\r
133 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
134 image that is being relocated.\r
135\r
136 @retval RETURN_SUCCESS The PE/COFF image was relocated.\r
137 Extended status information is in the ImageError field of ImageContext.\r
138 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.\r
139 Extended status information is in the ImageError field of ImageContext.\r
140 @retval RETURN_UNSUPPORTED A relocation record type is not supported.\r
141 Extended status information is in the ImageError field of ImageContext.\r
142\r
143**/\r
144RETURN_STATUS\r
145EFIAPI\r
146PeCoffLoaderRelocateImage (\r
147 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
148 )\r
149{\r
150 return mPeiEfiPeiPeCoffLoader->RelocateImage (mPeiEfiPeiPeCoffLoader, ImageContext);\r
151}\r
152\r
153/**\r
154 Loads a PE/COFF image into memory.\r
155\r
156 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer\r
157 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate\r
158 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.\r
159 The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed.\r
160 If ImageContext is NULL, then ASSERT().\r
161\r
162 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
163 image that is being loaded.\r
164\r
165 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by\r
166 the ImageAddress and ImageSize fields of ImageContext.\r
167 Extended status information is in the ImageError field of ImageContext.\r
168 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.\r
169 Extended status information is in the ImageError field of ImageContext.\r
170 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.\r
171 Extended status information is in the ImageError field of ImageContext.\r
172 @retval RETURN_INVALID_PARAMETER The image address is invalid.\r
173 Extended status information is in the ImageError field of ImageContext.\r
174\r
175**/\r
176RETURN_STATUS\r
177EFIAPI\r
178PeCoffLoaderLoadImage (\r
179 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
180 )\r
181{\r
182 return mPeiEfiPeiPeCoffLoader->LoadImage (mPeiEfiPeiPeCoffLoader, ImageContext);\r
183}\r
184\r
185/**\r
186 ImageRead function that operates on a memory buffer whos base is passed into\r
187 FileHandle. \r
188\r
189 @param FileHandle Ponter to baes of the input stream\r
190 @param FileOffset Offset to the start of the buffer\r
191 @param ReadSize Number of bytes to copy into the buffer\r
192 @param Buffer Location to place results of read\r
193\r
194 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into \r
195 the buffer.\r
196**/\r
197RETURN_STATUS\r
198EFIAPI\r
199PeCoffLoaderImageReadFromMemory (\r
200 IN VOID *FileHandle,\r
201 IN UINTN FileOffset,\r
202 IN OUT UINTN *ReadSize,\r
203 OUT VOID *Buffer\r
204 )\r
205{\r
206 return RETURN_UNSUPPORTED;\r
207}\r
208\r
209\r
210/**\r
211 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI\r
212 runtime. \r
213 \r
214 PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply\r
215 the fixups with a virtual mapping.\r
216\r
217\r
218 @param ImageBase Base address of relocated image\r
219 @param VirtImageBase Virtual mapping for ImageBase\r
220 @param ImageSize Size of the image to relocate\r
221 @param RelocationData Location to place results of read\r
222 \r
223**/\r
224VOID\r
225EFIAPI\r
226PeCoffLoaderRelocateImageForRuntime (\r
227 IN PHYSICAL_ADDRESS ImageBase,\r
228 IN PHYSICAL_ADDRESS VirtImageBase,\r
229 IN UINTN ImageSize,\r
230 IN VOID *RelocationData\r
231 )\r
232{\r
233}\r
234\r
235/**\r
236 Unloads a loaded PE/COFF image from memory and releases its taken resource.\r
237 \r
238 For Unix emulator, the PE/COFF image loaded by system needs to release.\r
239 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded, \r
240 this function can simply return RETURN_SUCCESS.\r
241\r
242 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
243 image to be unloaded.\r
244\r
245 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.\r
246**/\r
247RETURN_STATUS\r
248EFIAPI\r
249PeCoffLoaderUnloadImage (\r
250 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
251 )\r
252{\r
253 return mPeiEfiPeiPeCoffLoader->UnloadImage (mPeiEfiPeiPeCoffLoader, ImageContext);\r
254}\r