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