]> git.proxmox.com Git - mirror_edk2.git/blame - BeagleBoardPkg/Library/DxeHobPeCoffLib/DxeHobPeCoff.c
BeagleBoardPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BeagleBoardPkg / Library / DxeHobPeCoffLib / DxeHobPeCoff.c
CommitLineData
607a0df6 1/** @file\r
3402aac7 2 PE/COFF Loader Library implementation that wraps a protocol passed up from\r
607a0df6 3 SEC/PEI via a HOB. This is done to save space.\r
4\r
60274cca
HT
5 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
6 Portions copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
a1594be9 7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
607a0df6 8\r
9**/\r
10\r
11#include <PiDxe.h>\r
12#include <Library/DebugLib.h>\r
13#include <Library/HobLib.h>\r
14\r
15#include <Protocol/PeCoffLoader.h>\r
16\r
17\r
ca3ad58b 18PE_COFF_LOADER_PROTOCOL *gPeCoffLoader = NULL;\r
19\r
20\r
607a0df6 21/**\r
22 Retrieves information about a PE/COFF image.\r
23\r
3402aac7
RC
24 Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize,\r
25 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and\r
26 DebugDirectoryEntryRva fields of the ImageContext structure.\r
27 If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.\r
28 If the PE/COFF image accessed through the ImageRead service in the ImageContext\r
29 structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED.\r
30 If any errors occur while computing the fields of ImageContext,\r
31 then the error status is returned in the ImageError field of ImageContext.\r
607a0df6 32 If the image is a TE image, then SectionAlignment is set to 0.\r
3402aac7 33 The ImageRead and Handle fields of ImageContext structure must be valid prior\r
607a0df6 34 to invoking this service.\r
35\r
36 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
37 image that needs to be examined by this function.\r
38\r
39 @retval RETURN_SUCCESS The information on the PE/COFF image was collected.\r
40 @retval RETURN_INVALID_PARAMETER ImageContext is NULL.\r
41 @retval RETURN_UNSUPPORTED The PE/COFF image is not supported.\r
42\r
43**/\r
44RETURN_STATUS\r
45EFIAPI\r
46PeCoffLoaderGetImageInfo (\r
47 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
48 )\r
49{\r
50 return gPeCoffLoader->GetImageInfo (ImageContext);\r
51}\r
52\r
53\r
54/**\r
55 Applies relocation fixups to a PE/COFF image that was loaded with PeCoffLoaderLoadImage().\r
56\r
57 If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of\r
58 ImageContext as the relocation base address. Otherwise, use the DestinationAddress field\r
59 of ImageContext as the relocation base address. The caller must allocate the relocation\r
60 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.\r
3402aac7
RC
61\r
62 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress,\r
63 ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders,\r
64 DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of\r
607a0df6 65 the ImageContext structure must be valid prior to invoking this service.\r
3402aac7 66\r
607a0df6 67 If ImageContext is NULL, then ASSERT().\r
68\r
69 Note that if the platform does not maintain coherency between the instruction cache(s) and the data\r
70 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations\r
71 prior to transferring control to a PE/COFF image that is loaded using this library.\r
72\r
73 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
74 image that is being relocated.\r
75\r
76 @retval RETURN_SUCCESS The PE/COFF image was relocated.\r
77 Extended status information is in the ImageError field of ImageContext.\r
78 @retval RETURN_LOAD_ERROR The image in not a valid PE/COFF image.\r
79 Extended status information is in the ImageError field of ImageContext.\r
80 @retval RETURN_UNSUPPORTED A relocation record type is not supported.\r
81 Extended status information is in the ImageError field of ImageContext.\r
82\r
83**/\r
84RETURN_STATUS\r
85EFIAPI\r
86PeCoffLoaderRelocateImage (\r
87 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
88 )\r
89{\r
90 return gPeCoffLoader->RelocateImage (ImageContext);\r
91}\r
92\r
93/**\r
94 Loads a PE/COFF image into memory.\r
95\r
96 Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer\r
97 specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate\r
98 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.\r
99 The EntryPoint, FixupDataSize, CodeView, PdbPointer and HiiResourceData fields of ImageContext are computed.\r
3402aac7
RC
100 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize,\r
101 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva\r
607a0df6 102 fields of the ImageContext structure must be valid prior to invoking this service.\r
3402aac7 103\r
607a0df6 104 If ImageContext is NULL, then ASSERT().\r
105\r
106 Note that if the platform does not maintain coherency between the instruction cache(s) and the data\r
107 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations\r
108 prior to transferring control to a PE/COFF image that is loaded using this library.\r
109\r
110 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
111 image that is being loaded.\r
112\r
113 @retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by\r
114 the ImageAddress and ImageSize fields of ImageContext.\r
115 Extended status information is in the ImageError field of ImageContext.\r
116 @retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.\r
117 Extended status information is in the ImageError field of ImageContext.\r
118 @retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.\r
119 Extended status information is in the ImageError field of ImageContext.\r
120 @retval RETURN_INVALID_PARAMETER The image address is invalid.\r
121 Extended status information is in the ImageError field of ImageContext.\r
122\r
123**/\r
124RETURN_STATUS\r
125EFIAPI\r
126PeCoffLoaderLoadImage (\r
127 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
128 )\r
129{\r
130 return gPeCoffLoader->LoadImage (ImageContext);\r
131}\r
132\r
133\r
134\r
135/**\r
136 Reads contents of a PE/COFF image from a buffer in system memory.\r
3402aac7
RC
137\r
138 This is the default implementation of a PE_COFF_LOADER_READ_FILE function\r
139 that assumes FileHandle pointer to the beginning of a PE/COFF image.\r
140 This function reads contents of the PE/COFF image that starts at the system memory\r
141 address specified by FileHandle. The read operation copies ReadSize bytes from the\r
142 PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer.\r
607a0df6 143 The size of the buffer actually read is returned in ReadSize.\r
3402aac7 144\r
607a0df6 145 If FileHandle is NULL, then ASSERT().\r
146 If ReadSize is NULL, then ASSERT().\r
147 If Buffer is NULL, then ASSERT().\r
148\r
149 @param FileHandle Pointer to base of the input stream\r
150 @param FileOffset Offset into the PE/COFF image to begin the read operation.\r
3402aac7 151 @param ReadSize On input, the size in bytes of the requested read operation.\r
607a0df6 152 On output, the number of bytes actually read.\r
153 @param Buffer Output buffer that contains the data read from the PE/COFF image.\r
154\r
3402aac7 155 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into\r
607a0df6 156 the buffer.\r
157**/\r
158RETURN_STATUS\r
159EFIAPI\r
160PeCoffLoaderImageReadFromMemory (\r
161 IN VOID *FileHandle,\r
162 IN UINTN FileOffset,\r
163 IN OUT UINTN *ReadSize,\r
164 OUT VOID *Buffer\r
165 )\r
166{\r
ca3ad58b 167 return gPeCoffLoader->ReadFromMemory (\r
168 FileHandle,\r
607a0df6 169 FileOffset,\r
ca3ad58b 170 ReadSize,\r
171 Buffer\r
607a0df6 172 );\r
3402aac7 173\r
607a0df6 174}\r
175\r
176\r
177\r
178/**\r
179 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI\r
3402aac7
RC
180 runtime.\r
181\r
182 This function reapplies relocation fixups to the PE/COFF image specified by ImageBase\r
183 and ImageSize so the image will execute correctly when the PE/COFF image is mapped\r
184 to the address specified by VirtualImageBase. RelocationData must be identical\r
185 to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure\r
607a0df6 186 after this PE/COFF image was relocated with PeCoffLoaderRelocateImage().\r
187\r
188 Note that if the platform does not maintain coherency between the instruction cache(s) and the data\r
189 cache(s) in hardware, then the caller is responsible for performing cache maintenance operations\r
190 prior to transferring control to a PE/COFF image that is loaded using this library.\r
191\r
3402aac7 192 @param ImageBase Base address of a PE/COFF image that has been loaded\r
607a0df6 193 and relocated into system memory.\r
194 @param VirtImageBase The request virtual address that the PE/COFF image is to\r
195 be fixed up for.\r
196 @param ImageSize The size, in bytes, of the PE/COFF image.\r
3402aac7 197 @param RelocationData A pointer to the relocation data that was collected when the PE/COFF\r
607a0df6 198 image was relocated using PeCoffLoaderRelocateImage().\r
3402aac7 199\r
607a0df6 200**/\r
201VOID\r
202EFIAPI\r
203PeCoffLoaderRelocateImageForRuntime (\r
204 IN PHYSICAL_ADDRESS ImageBase,\r
205 IN PHYSICAL_ADDRESS VirtImageBase,\r
206 IN UINTN ImageSize,\r
207 IN VOID *RelocationData\r
208 )\r
209{\r
210 return gPeCoffLoader->RelocateImageForRuntime (\r
211 ImageBase,\r
212 VirtImageBase,\r
213 ImageSize,\r
ca3ad58b 214 RelocationData\r
607a0df6 215 );\r
216}\r
217\r
218\r
219/**\r
220 Unloads a loaded PE/COFF image from memory and releases its taken resource.\r
3402aac7
RC
221 Releases any environment specific resources that were allocated when the image\r
222 specified by ImageContext was loaded using PeCoffLoaderLoadImage().\r
223\r
607a0df6 224 For NT32 emulator, the PE/COFF image loaded by system needs to release.\r
3402aac7 225 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded,\r
607a0df6 226 this function can simply return RETURN_SUCCESS.\r
3402aac7 227\r
607a0df6 228 If ImageContext is NULL, then ASSERT().\r
3402aac7 229\r
607a0df6 230 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
231 image to be unloaded.\r
232\r
233 @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully.\r
234**/\r
235RETURN_STATUS\r
236EFIAPI\r
237PeCoffLoaderUnloadImage (\r
238 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
239 )\r
240{\r
241 return gPeCoffLoader->UnloadImage (ImageContext);\r
242}\r
243\r
ca3ad58b 244typedef struct {\r
245 EFI_HOB_GUID_TYPE Hob;\r
246 VOID *Interface;\r
247} PROTOCOL_HOB;\r
607a0df6 248\r
249\r
250/**\r
ca3ad58b 251 The constructor function caches the pointer of DXE Services Table.\r
252\r
253 The constructor function caches the pointer of DXE Services Table.\r
254 It will ASSERT() if that operation fails.\r
255 It will ASSERT() if the pointer of DXE Services Table is NULL.\r
256 It will always return EFI_SUCCESS.\r
257\r
258 @param ImageHandle The firmware allocated handle for the EFI image.\r
259 @param SystemTable A pointer to the EFI System Table.\r
260\r
261 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
607a0df6 262\r
607a0df6 263**/\r
264EFI_STATUS\r
265EFIAPI\r
266DxeHobPeCoffLibConstructor (\r
ca3ad58b 267 IN EFI_HANDLE ImageHandle,\r
268 IN EFI_SYSTEM_TABLE *SystemTable\r
607a0df6 269 )\r
270{\r
271 PROTOCOL_HOB *Hob;\r
ca3ad58b 272\r
607a0df6 273 Hob = GetFirstGuidHob (&gPeCoffLoaderProtocolGuid);\r
274 if (Hob == NULL) {\r
275 return EFI_NOT_FOUND;\r
276 }\r
3402aac7 277\r
607a0df6 278 gPeCoffLoader = Hob->Interface;\r
ca3ad58b 279 return EFI_SUCCESS;\r
607a0df6 280}\r
281\r
282\r