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