]>
Commit | Line | Data |
---|---|---|
615c6dd0 | 1 | /** @file\r |
b1f6a7c6 | 2 | Pei Core Load Image Support\r |
3 | \r | |
341a658f | 4 | Copyright (c) 2006 - 2010, Intel Corporation \r |
192f6d4c | 5 | All rights reserved. This program and the accompanying materials \r |
6 | are licensed and made available under the terms and conditions of the BSD License \r | |
7 | which accompanies this distribution. The full text of the license may be found at \r | |
8 | http://opensource.org/licenses/bsd-license.php \r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
12 | \r | |
615c6dd0 | 13 | **/\r |
192f6d4c | 14 | \r |
0d516397 | 15 | #include "PeiMain.h"\r |
192f6d4c | 16 | \r |
b0d803fe | 17 | \r |
fe1e36e5 | 18 | EFI_PEI_LOAD_FILE_PPI mPeiLoadImagePpi = {\r |
b0d803fe | 19 | PeiLoadImageLoadImageWrapper\r |
20 | };\r | |
21 | \r | |
22 | \r | |
fe1e36e5 | 23 | EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList = {\r |
b0d803fe | 24 | (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r |
25 | &gEfiPeiLoadFilePpiGuid,\r | |
26 | &mPeiLoadImagePpi\r | |
27 | };\r | |
28 | \r | |
b1f6a7c6 | 29 | /**\r |
30 | \r | |
31 | Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r | |
32 | \r | |
33 | \r | |
34 | @param FileHandle - The handle to the PE/COFF file\r | |
35 | @param FileOffset - The offset, in bytes, into the file to read\r | |
36 | @param ReadSize - The number of bytes to read from the file starting at FileOffset\r | |
37 | @param Buffer - A pointer to the buffer to read the data into.\r | |
38 | \r | |
39 | @return EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r | |
40 | \r | |
41 | **/\r | |
b0d803fe | 42 | EFI_STATUS\r |
43 | EFIAPI\r | |
44 | PeiImageRead (\r | |
45 | IN VOID *FileHandle,\r | |
46 | IN UINTN FileOffset,\r | |
ed299e3c | 47 | IN UINTN *ReadSize,\r |
b0d803fe | 48 | OUT VOID *Buffer\r |
49 | )\r | |
b0d803fe | 50 | {\r |
a9bfd802 LG |
51 | CHAR8 *Destination8;\r |
52 | CHAR8 *Source8;\r | |
53 | UINTN Length;\r | |
54 | \r | |
55 | Destination8 = Buffer;\r | |
56 | Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r | |
43ada17c | 57 | if (Destination8 != Source8) {\r |
58 | Length = *ReadSize;\r | |
59 | while ((Length--) > 0) {\r | |
60 | *(Destination8++) = *(Source8++);\r | |
61 | }\r | |
a9bfd802 LG |
62 | }\r |
63 | \r | |
b0d803fe | 64 | return EFI_SUCCESS;\r |
65 | }\r | |
66 | \r | |
b1f6a7c6 | 67 | /**\r |
b0d803fe | 68 | \r |
ed299e3c | 69 | Support routine to get the Image read file function.\r |
b0d803fe | 70 | \r |
b1f6a7c6 | 71 | @param ImageContext - The context of the image being loaded\r |
b0d803fe | 72 | \r |
b1f6a7c6 | 73 | @retval EFI_SUCCESS - If Image function location is found\r |
b0d803fe | 74 | \r |
b1f6a7c6 | 75 | **/\r |
76 | EFI_STATUS\r | |
77 | GetImageReadFunction (\r | |
78 | IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r | |
79 | )\r | |
b0d803fe | 80 | {\r |
43ada17c | 81 | PEI_CORE_INSTANCE *Private;\r |
b0d803fe | 82 | VOID* MemoryBuffer;\r |
83 | \r | |
43ada17c | 84 | Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r |
b0d803fe | 85 | \r |
c2a19e92 | 86 | if (!Private->PeiMemoryInstalled || (Private->HobList.HandoffInformationTable->BootMode == BOOT_ON_S3_RESUME)) {\r |
43ada17c | 87 | ImageContext->ImageRead = PeiImageRead;\r |
88 | } else {\r | |
89 | MemoryBuffer = AllocatePages (0x400 / EFI_PAGE_SIZE + 1);\r | |
90 | ASSERT (MemoryBuffer != NULL);\r | |
b0d803fe | 91 | \r |
43ada17c | 92 | CopyMem (MemoryBuffer, (CONST VOID *) (UINTN) PeiImageRead, 0x400);\r |
93 | \r | |
94 | ImageContext->ImageRead = (PE_COFF_LOADER_READ_FILE) (UINTN) MemoryBuffer;\r | |
95 | }\r | |
b0d803fe | 96 | \r |
97 | return EFI_SUCCESS;\r | |
98 | }\r | |
99 | \r | |
b1f6a7c6 | 100 | /**\r |
101 | \r | |
102 | Loads and relocates a PE/COFF image into memory.\r | |
341a658f | 103 | If the image is not relocatable, it will not be loaded into memory and be loaded as XIP image.\r |
b1f6a7c6 | 104 | \r |
105 | @param Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r | |
106 | @param ImageAddress - The base address of the relocated PE/COFF image\r | |
107 | @param ImageSize - The size of the relocated PE/COFF image\r | |
108 | @param EntryPoint - The entry point of the relocated PE/COFF image\r | |
109 | \r | |
110 | @retval EFI_SUCCESS The file was loaded and relocated\r | |
111 | @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file\r | |
112 | \r | |
113 | **/\r | |
b0d803fe | 114 | EFI_STATUS\r |
115 | LoadAndRelocatePeCoffImage (\r | |
b0d803fe | 116 | IN VOID *Pe32Data,\r |
117 | OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r | |
118 | OUT UINT64 *ImageSize,\r | |
119 | OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r | |
120 | )\r | |
b0d803fe | 121 | {\r |
122 | EFI_STATUS Status;\r | |
123 | PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r | |
43ada17c | 124 | PEI_CORE_INSTANCE *Private;\r |
125 | \r | |
126 | Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r | |
b0d803fe | 127 | \r |
b0d803fe | 128 | ZeroMem (&ImageContext, sizeof (ImageContext));\r |
129 | ImageContext.Handle = Pe32Data;\r | |
130 | Status = GetImageReadFunction (&ImageContext);\r | |
131 | \r | |
132 | ASSERT_EFI_ERROR (Status);\r | |
133 | \r | |
3d7b0992 | 134 | Status = PeCoffLoaderGetImageInfo (&ImageContext);\r |
b0d803fe | 135 | if (EFI_ERROR (Status)) {\r |
136 | return Status;\r | |
137 | }\r | |
138 | //\r | |
9626a87e LG |
139 | // When Image has no reloc section, it can't be relocated into memory.\r |
140 | //\r | |
414bdfb6 | 141 | if (ImageContext.RelocationsStripped && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r |
341a658f | 142 | DEBUG ((EFI_D_INFO, "The image at 0x%08x without reloc section can't be loaded into memory\n", (UINTN) Pe32Data));\r |
9626a87e | 143 | }\r |
414bdfb6 | 144 | \r |
341a658f LG |
145 | //\r |
146 | // Set default base address to current image address.\r | |
147 | //\r | |
148 | ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r | |
149 | \r | |
9626a87e | 150 | //\r |
341a658f | 151 | // Allocate Memory for the image when memory is ready, boot mode is not S3, and image is relocatable.\r |
b0d803fe | 152 | //\r |
bfd4a3f1 | 153 | if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r |
43ada17c | 154 | ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r |
155 | ASSERT (ImageContext.ImageAddress != 0);\r | |
341a658f LG |
156 | if (ImageContext.ImageAddress == 0) {\r |
157 | return EFI_OUT_OF_RESOURCES;\r | |
158 | }\r | |
43ada17c | 159 | \r |
160 | //\r | |
161 | // Skip the reserved space for the stripped PeHeader when load TeImage into memory.\r | |
162 | //\r | |
163 | if (ImageContext.IsTeImage) {\r | |
164 | ImageContext.ImageAddress = ImageContext.ImageAddress + \r | |
165 | ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -\r | |
166 | sizeof (EFI_TE_IMAGE_HEADER);\r | |
167 | }\r | |
4e844595 | 168 | }\r |
b0d803fe | 169 | \r |
170 | //\r | |
171 | // Load the image to our new buffer\r | |
172 | //\r | |
3d7b0992 | 173 | Status = PeCoffLoaderLoadImage (&ImageContext);\r |
b0d803fe | 174 | if (EFI_ERROR (Status)) {\r |
175 | return Status;\r | |
176 | }\r | |
177 | //\r | |
178 | // Relocate the image in our new buffer\r | |
179 | //\r | |
3d7b0992 | 180 | Status = PeCoffLoaderRelocateImage (&ImageContext);\r |
b0d803fe | 181 | if (EFI_ERROR (Status)) {\r |
182 | return Status;\r | |
183 | }\r | |
184 | \r | |
185 | //\r | |
186 | // Flush the instruction cache so the image data is written before we execute it\r | |
187 | //\r | |
341a658f | 188 | if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r |
43ada17c | 189 | InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r |
190 | }\r | |
b0d803fe | 191 | \r |
192 | *ImageAddress = ImageContext.ImageAddress;\r | |
193 | *ImageSize = ImageContext.ImageSize;\r | |
194 | *EntryPoint = ImageContext.EntryPoint;\r | |
195 | \r | |
196 | return EFI_SUCCESS;\r | |
197 | }\r | |
198 | \r | |
b1f6a7c6 | 199 | /**\r |
ed299e3c LG |
200 | Loads a PEIM into memory for subsequent execution. If there are compressed \r |
201 | images or images that need to be relocated into memory for performance reasons, \r | |
202 | this service performs that transformation.\r | |
b1f6a7c6 | 203 | \r |
ed299e3c | 204 | @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r |
b1f6a7c6 | 205 | @param FileHandle Pointer to the FFS file header of the image.\r |
206 | @param ImageAddressArg Pointer to PE/TE image.\r | |
207 | @param ImageSizeArg Size of PE/TE image.\r | |
208 | @param EntryPoint Pointer to entry point of specified image file for output.\r | |
209 | @param AuthenticationState - Pointer to attestation authentication state of image.\r | |
210 | \r | |
ed299e3c LG |
211 | @retval EFI_SUCCESS Image is successfully loaded.\r |
212 | @retval EFI_NOT_FOUND Fail to locate necessary PPI.\r | |
213 | @retval EFI_UNSUPPORTED Image Machine Type is not supported.\r | |
b1f6a7c6 | 214 | \r |
215 | **/\r | |
b0d803fe | 216 | EFI_STATUS\r |
217 | PeiLoadImageLoadImage (\r | |
284c8400 | 218 | IN CONST EFI_PEI_SERVICES **PeiServices,\r |
b0d803fe | 219 | IN EFI_PEI_FILE_HANDLE FileHandle,\r |
220 | OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r | |
221 | OUT UINT64 *ImageSizeArg, OPTIONAL\r | |
222 | OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r | |
223 | OUT UINT32 *AuthenticationState\r | |
224 | )\r | |
192f6d4c | 225 | {\r |
226 | EFI_STATUS Status;\r | |
227 | VOID *Pe32Data;\r | |
192f6d4c | 228 | EFI_PHYSICAL_ADDRESS ImageAddress;\r |
229 | UINT64 ImageSize;\r | |
230 | EFI_PHYSICAL_ADDRESS ImageEntryPoint;\r | |
192f6d4c | 231 | UINT16 Machine;\r |
3076397e | 232 | EFI_SECTION_TYPE SearchType1;\r |
233 | EFI_SECTION_TYPE SearchType2;\r | |
192f6d4c | 234 | \r |
3d7b0992 LG |
235 | *EntryPoint = 0;\r |
236 | ImageSize = 0;\r | |
b0d803fe | 237 | *AuthenticationState = 0;\r |
192f6d4c | 238 | \r |
3076397e | 239 | if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst)) {\r |
240 | SearchType1 = EFI_SECTION_TE;\r | |
241 | SearchType2 = EFI_SECTION_PE32;\r | |
242 | } else {\r | |
243 | SearchType1 = EFI_SECTION_PE32;\r | |
244 | SearchType2 = EFI_SECTION_TE;\r | |
245 | }\r | |
1b620adb | 246 | \r |
192f6d4c | 247 | //\r |
3076397e | 248 | // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst \r |
249 | // is true, TE will be searched first).\r | |
192f6d4c | 250 | //\r |
251 | Status = PeiServicesFfsFindSectionData (\r | |
3076397e | 252 | SearchType1,\r |
b0d803fe | 253 | FileHandle,\r |
192f6d4c | 254 | &Pe32Data\r |
255 | );\r | |
256 | //\r | |
3076397e | 257 | // If we didn't find a first exe section, try to find the second exe section.\r |
192f6d4c | 258 | //\r |
259 | if (EFI_ERROR (Status)) {\r | |
260 | Status = PeiServicesFfsFindSectionData (\r | |
3076397e | 261 | SearchType2,\r |
b0d803fe | 262 | FileHandle,\r |
263 | &Pe32Data\r | |
192f6d4c | 264 | );\r |
b0d803fe | 265 | if (EFI_ERROR (Status)) {\r |
192f6d4c | 266 | //\r |
b0d803fe | 267 | // PEI core only carry the loader function fro TE and PE32 executables\r |
268 | // If this two section does not exist, just return.\r | |
192f6d4c | 269 | //\r |
b0d803fe | 270 | return Status;\r |
271 | }\r | |
272 | }\r | |
273 | \r | |
43ada17c | 274 | //\r |
275 | // If memory is installed, perform the shadow operations\r | |
276 | //\r | |
277 | Status = LoadAndRelocatePeCoffImage (\r | |
278 | Pe32Data,\r | |
279 | &ImageAddress,\r | |
280 | &ImageSize,\r | |
281 | &ImageEntryPoint\r | |
282 | );\r | |
192f6d4c | 283 | \r |
43ada17c | 284 | ASSERT_EFI_ERROR (Status);\r |
3d7b0992 | 285 | \r |
43ada17c | 286 | \r |
287 | if (EFI_ERROR (Status)) {\r | |
288 | return Status;\r | |
192f6d4c | 289 | }\r |
43ada17c | 290 | \r |
291 | //\r | |
292 | // Got the entry point from the loaded Pe32Data\r | |
293 | //\r | |
294 | Pe32Data = (VOID *) ((UINTN) ImageAddress);\r | |
295 | *EntryPoint = ImageEntryPoint;\r | |
3d7b0992 LG |
296 | \r |
297 | Machine = PeCoffLoaderGetMachineType (Pe32Data);\r | |
192f6d4c | 298 | \r |
299 | if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {\r | |
8c519a56 | 300 | if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine)) {\r |
301 | return EFI_UNSUPPORTED;\r | |
302 | }\r | |
192f6d4c | 303 | }\r |
304 | \r | |
b0d803fe | 305 | if (ImageAddressArg != NULL) {\r |
306 | *ImageAddressArg = ImageAddress;\r | |
307 | }\r | |
308 | \r | |
309 | if (ImageSizeArg != NULL) {\r | |
310 | *ImageSizeArg = ImageSize;\r | |
311 | }\r | |
312 | \r | |
192f6d4c | 313 | DEBUG_CODE_BEGIN ();\r |
3d7b0992 LG |
314 | CHAR8 *AsciiString;\r |
315 | CHAR8 AsciiBuffer[512];\r | |
316 | INT32 Index;\r | |
317 | INT32 Index1;\r | |
e98cd821 LG |
318 | \r |
319 | //\r | |
320 | // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r | |
321 | //\r | |
7cf02714 | 322 | if (Machine != EFI_IMAGE_MACHINE_IA64) {\r |
91136124 | 323 | DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));\r |
e98cd821 LG |
324 | } else {\r |
325 | //\r | |
326 | // For IPF Image, the real entry point should be print.\r | |
327 | //\r | |
91136124 | 328 | DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)(*(UINT64 *)(UINTN)*EntryPoint)));\r |
e98cd821 | 329 | }\r |
192f6d4c | 330 | \r |
e98cd821 LG |
331 | //\r |
332 | // Print Module Name by PeImage PDB file name.\r | |
333 | //\r | |
3d7b0992 LG |
334 | AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r |
335 | \r | |
336 | if (AsciiString != NULL) {\r | |
19ea58a1 | 337 | for (Index = (INT32) AsciiStrLen (AsciiString) - 1; Index >= 0; Index --) {\r |
3d7b0992 LG |
338 | if (AsciiString[Index] == '\\') {\r |
339 | break;\r | |
340 | }\r | |
192f6d4c | 341 | }\r |
192f6d4c | 342 | \r |
3d7b0992 LG |
343 | if (Index != 0) {\r |
344 | for (Index1 = 0; AsciiString[Index + 1 + Index1] != '.'; Index1 ++) {\r | |
345 | AsciiBuffer [Index1] = AsciiString[Index + 1 + Index1];\r | |
192f6d4c | 346 | }\r |
3d7b0992 LG |
347 | AsciiBuffer [Index1] = '\0';\r |
348 | DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer));\r | |
192f6d4c | 349 | }\r |
350 | }\r | |
3d7b0992 | 351 | \r |
192f6d4c | 352 | DEBUG_CODE_END ();\r |
353 | \r | |
354 | DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r | |
355 | \r | |
356 | return EFI_SUCCESS;\r | |
b0d803fe | 357 | \r |
358 | }\r | |
359 | \r | |
360 | \r | |
b1f6a7c6 | 361 | /**\r |
362 | The wrapper function of PeiLoadImageLoadImage().\r | |
363 | \r | |
364 | @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.\r | |
365 | @param FileHandle - Pointer to the FFS file header of the image.\r | |
366 | @param ImageAddressArg - Pointer to PE/TE image.\r | |
367 | @param ImageSizeArg - Size of PE/TE image.\r | |
368 | @param EntryPoint - Pointer to entry point of specified image file for output.\r | |
369 | @param AuthenticationState - Pointer to attestation authentication state of image.\r | |
370 | \r | |
371 | @return Status of PeiLoadImageLoadImage().\r | |
372 | \r | |
373 | **/\r | |
b0d803fe | 374 | EFI_STATUS\r |
375 | EFIAPI\r | |
376 | PeiLoadImageLoadImageWrapper (\r | |
377 | IN CONST EFI_PEI_LOAD_FILE_PPI *This,\r | |
378 | IN EFI_PEI_FILE_HANDLE FileHandle,\r | |
379 | OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r | |
380 | OUT UINT64 *ImageSizeArg, OPTIONAL\r | |
381 | OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r | |
382 | OUT UINT32 *AuthenticationState\r | |
383 | )\r | |
b0d803fe | 384 | {\r |
385 | return PeiLoadImageLoadImage (\r | |
386 | GetPeiServicesTablePointer (),\r | |
387 | FileHandle,\r | |
388 | ImageAddressArg,\r | |
389 | ImageSizeArg,\r | |
390 | EntryPoint,\r | |
391 | AuthenticationState\r | |
392 | );\r | |
192f6d4c | 393 | }\r |
b0d803fe | 394 | \r |
341a658f LG |
395 | /**\r |
396 | Check whether the input image has the relocation.\r | |
397 | \r | |
398 | @param Pe32Data Pointer to the PE/COFF or TE image.\r | |
399 | \r | |
400 | @retval TRUE Relocation is stripped.\r | |
401 | @retval FALSE Relocation is not stripped.\r | |
402 | \r | |
403 | **/\r | |
404 | BOOLEAN\r | |
405 | RelocationIsStrip (\r | |
406 | IN VOID *Pe32Data\r | |
407 | )\r | |
408 | {\r | |
409 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r | |
410 | EFI_IMAGE_DOS_HEADER *DosHdr;\r | |
411 | \r | |
412 | ASSERT (Pe32Data != NULL);\r | |
413 | \r | |
414 | DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r | |
415 | if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r | |
416 | //\r | |
417 | // DOS image header is present, so read the PE header after the DOS image header.\r | |
418 | //\r | |
419 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r | |
420 | } else {\r | |
421 | //\r | |
422 | // DOS image header is not present, so PE header is at the image base.\r | |
423 | //\r | |
424 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r | |
425 | }\r | |
426 | \r | |
427 | //\r | |
428 | // Three cases with regards to relocations:\r | |
429 | // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable\r | |
430 | // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable\r | |
431 | // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but\r | |
432 | // has no base relocs to apply\r | |
433 | // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.\r | |
434 | //\r | |
435 | // Look at the file header to determine if relocations have been stripped, and\r | |
436 | // save this info in the image context for later use.\r | |
437 | //\r | |
438 | if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r | |
439 | if ((Hdr.Te->DataDirectory[0].Size == 0) && (Hdr.Te->DataDirectory[0].VirtualAddress == 0)) {\r | |
440 | return TRUE;\r | |
441 | } else {\r | |
442 | return FALSE;\r | |
443 | }\r | |
444 | } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r | |
445 | if ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0) {\r | |
446 | return TRUE;\r | |
447 | } else {\r | |
448 | return FALSE;\r | |
449 | }\r | |
450 | }\r | |
451 | \r | |
452 | return FALSE;\r | |
453 | }\r | |
454 | \r | |
b1f6a7c6 | 455 | /**\r |
ed299e3c LG |
456 | Routine to load image file for subsequent execution by LoadFile Ppi.\r |
457 | If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE \r | |
458 | XIP image format is used.\r | |
b1f6a7c6 | 459 | \r |
ed299e3c LG |
460 | @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r |
461 | @param FileHandle - Pointer to the FFS file header of the image.\r | |
341a658f | 462 | @param PeimState - The dispatch state of the input PEIM handle.\r |
ed299e3c LG |
463 | @param EntryPoint - Pointer to entry point of specified image file for output.\r |
464 | @param AuthenticationState - Pointer to attestation authentication state of image.\r | |
b1f6a7c6 | 465 | \r |
ed299e3c LG |
466 | @retval EFI_SUCCESS - Image is successfully loaded.\r |
467 | @retval EFI_NOT_FOUND - Fail to locate necessary PPI\r | |
468 | @retval Others - Fail to load file.\r | |
b1f6a7c6 | 469 | \r |
470 | **/\r | |
b0d803fe | 471 | EFI_STATUS\r |
472 | PeiLoadImage (\r | |
6c7a807a | 473 | IN CONST EFI_PEI_SERVICES **PeiServices,\r |
b0d803fe | 474 | IN EFI_PEI_FILE_HANDLE FileHandle,\r |
341a658f | 475 | IN UINT8 PeimState,\r |
b0d803fe | 476 | OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r |
477 | OUT UINT32 *AuthenticationState\r | |
478 | )\r | |
b0d803fe | 479 | {\r |
480 | EFI_STATUS PpiStatus;\r | |
481 | EFI_STATUS Status;\r | |
482 | UINTN Index;\r | |
483 | EFI_PEI_LOAD_FILE_PPI *LoadFile;\r | |
484 | EFI_PHYSICAL_ADDRESS ImageAddress;\r | |
485 | UINT64 ImageSize;\r | |
341a658f | 486 | BOOLEAN IsStrip;\r |
b0d803fe | 487 | \r |
341a658f | 488 | IsStrip = FALSE;\r |
b0d803fe | 489 | //\r |
490 | // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.\r | |
491 | // one at a time, until one reports EFI_SUCCESS.\r | |
492 | //\r | |
493 | Index = 0;\r | |
494 | do {\r | |
495 | PpiStatus = PeiServicesLocatePpi (\r | |
496 | &gEfiPeiLoadFilePpiGuid,\r | |
497 | Index,\r | |
498 | NULL,\r | |
499 | (VOID **)&LoadFile\r | |
500 | );\r | |
501 | if (!EFI_ERROR (PpiStatus)) {\r | |
502 | Status = LoadFile->LoadFile (\r | |
503 | LoadFile, \r | |
504 | FileHandle, \r | |
505 | &ImageAddress, \r | |
506 | &ImageSize,\r | |
507 | EntryPoint,\r | |
508 | AuthenticationState\r | |
509 | );\r | |
510 | if (!EFI_ERROR (Status)) {\r | |
341a658f LG |
511 | //\r |
512 | // The shadowed PEIM must be relocatable.\r | |
513 | //\r | |
514 | if (PeimState == PEIM_STATE_REGISITER_FOR_SHADOW) {\r | |
515 | IsStrip = RelocationIsStrip ((VOID *) (UINTN) ImageAddress);\r | |
516 | ASSERT (!IsStrip);\r | |
517 | if (IsStrip) {\r | |
518 | return EFI_UNSUPPORTED;\r | |
519 | }\r | |
520 | }\r | |
521 | \r | |
db0b7ad5 LG |
522 | //\r |
523 | // The image to be started must have the machine type supported by PeiCore.\r | |
524 | //\r | |
525 | ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress)));\r | |
919df8e6 | 526 | if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress))) {\r |
919df8e6 LG |
527 | return EFI_UNSUPPORTED;\r |
528 | }\r | |
b0d803fe | 529 | return Status;\r |
530 | }\r | |
531 | }\r | |
532 | Index++;\r | |
533 | } while (!EFI_ERROR (PpiStatus));\r | |
534 | \r | |
8c519a56 | 535 | return PpiStatus;\r |
b0d803fe | 536 | }\r |
537 | \r | |
538 | \r | |
b1f6a7c6 | 539 | /**\r |
b0d803fe | 540 | \r |
ed299e3c LG |
541 | Install Pei Load File PPI.\r |
542 | \r | |
543 | \r | |
544 | @param PrivateData - Pointer to PEI_CORE_INSTANCE.\r | |
545 | @param OldCoreData - Pointer to PEI_CORE_INSTANCE.\r | |
b0d803fe | 546 | \r |
b1f6a7c6 | 547 | **/\r |
548 | VOID\r | |
549 | InitializeImageServices (\r | |
550 | IN PEI_CORE_INSTANCE *PrivateData,\r | |
551 | IN PEI_CORE_INSTANCE *OldCoreData\r | |
552 | )\r | |
b0d803fe | 553 | {\r |
b0d803fe | 554 | if (OldCoreData == NULL) {\r |
555 | //\r | |
556 | // The first time we are XIP (running from FLASH). We need to remember the\r | |
557 | // FLASH address so we can reinstall the memory version that runs faster\r | |
558 | //\r | |
559 | PrivateData->XipLoadFile = &gPpiLoadFilePpiList;\r | |
560 | PeiServicesInstallPpi (PrivateData->XipLoadFile);\r | |
561 | } else {\r | |
562 | //\r | |
563 | // 2nd time we are running from memory so replace the XIP version with the \r | |
564 | // new memory version. \r | |
565 | //\r | |
566 | PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList); \r | |
567 | }\r | |
568 | }\r | |
569 | \r | |
570 | \r | |
571 | \r | |
341a658f | 572 | \r |