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