]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Image/Image.c
Fix some coding style issues in MdeModulePkg.
[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
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
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
c2a19e92 148 if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
43ada17c 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
c2a19e92 182 if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
43ada17c 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
3076397e 226 EFI_SECTION_TYPE SearchType1;\r
227 EFI_SECTION_TYPE SearchType2;\r
192f6d4c 228\r
3d7b0992
LG
229 *EntryPoint = 0;\r
230 ImageSize = 0;\r
b0d803fe 231 *AuthenticationState = 0;\r
192f6d4c 232\r
3076397e 233 if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst)) {\r
234 SearchType1 = EFI_SECTION_TE;\r
235 SearchType2 = EFI_SECTION_PE32;\r
236 } else {\r
237 SearchType1 = EFI_SECTION_PE32;\r
238 SearchType2 = EFI_SECTION_TE;\r
239 }\r
1b620adb 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
43ada17c 268 //\r
269 // If memory is installed, perform the shadow operations\r
270 //\r
271 Status = LoadAndRelocatePeCoffImage (\r
272 Pe32Data,\r
273 &ImageAddress,\r
274 &ImageSize,\r
275 &ImageEntryPoint\r
276 );\r
192f6d4c 277\r
43ada17c 278 ASSERT_EFI_ERROR (Status);\r
3d7b0992 279\r
43ada17c 280\r
281 if (EFI_ERROR (Status)) {\r
282 return Status;\r
192f6d4c 283 }\r
43ada17c 284\r
285 //\r
286 // Got the entry point from the loaded Pe32Data\r
287 //\r
288 Pe32Data = (VOID *) ((UINTN) ImageAddress);\r
289 *EntryPoint = ImageEntryPoint;\r
3d7b0992
LG
290 \r
291 Machine = PeCoffLoaderGetMachineType (Pe32Data);\r
192f6d4c 292 \r
293 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {\r
8c519a56 294 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine)) {\r
295 return EFI_UNSUPPORTED;\r
296 }\r
192f6d4c 297 }\r
298\r
b0d803fe 299 if (ImageAddressArg != NULL) {\r
300 *ImageAddressArg = ImageAddress;\r
301 }\r
302\r
303 if (ImageSizeArg != NULL) {\r
304 *ImageSizeArg = ImageSize;\r
305 }\r
306 \r
192f6d4c 307 DEBUG_CODE_BEGIN ();\r
3d7b0992
LG
308 CHAR8 *AsciiString;\r
309 CHAR8 AsciiBuffer[512];\r
310 INT32 Index;\r
311 INT32 Index1;\r
e98cd821
LG
312\r
313 //\r
314 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
315 //\r
7cf02714 316 if (Machine != EFI_IMAGE_MACHINE_IA64) {\r
91136124 317 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));\r
e98cd821
LG
318 } else {\r
319 //\r
320 // For IPF Image, the real entry point should be print.\r
321 //\r
91136124 322 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 323 }\r
192f6d4c 324 \r
e98cd821
LG
325 //\r
326 // Print Module Name by PeImage PDB file name.\r
327 //\r
3d7b0992
LG
328 AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r
329 \r
330 if (AsciiString != NULL) {\r
19ea58a1 331 for (Index = (INT32) AsciiStrLen (AsciiString) - 1; Index >= 0; Index --) {\r
3d7b0992
LG
332 if (AsciiString[Index] == '\\') {\r
333 break;\r
334 }\r
192f6d4c 335 }\r
192f6d4c 336\r
3d7b0992
LG
337 if (Index != 0) {\r
338 for (Index1 = 0; AsciiString[Index + 1 + Index1] != '.'; Index1 ++) {\r
339 AsciiBuffer [Index1] = AsciiString[Index + 1 + Index1];\r
192f6d4c 340 }\r
3d7b0992
LG
341 AsciiBuffer [Index1] = '\0';\r
342 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer));\r
192f6d4c 343 }\r
344 }\r
3d7b0992 345\r
192f6d4c 346 DEBUG_CODE_END ();\r
347\r
348 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r
349\r
350 return EFI_SUCCESS;\r
b0d803fe 351\r
352}\r
353\r
354\r
b1f6a7c6 355/**\r
356 The wrapper function of PeiLoadImageLoadImage().\r
357\r
358 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.\r
359 @param FileHandle - Pointer to the FFS file header of the image.\r
360 @param ImageAddressArg - Pointer to PE/TE image.\r
361 @param ImageSizeArg - Size of PE/TE image.\r
362 @param EntryPoint - Pointer to entry point of specified image file for output.\r
363 @param AuthenticationState - Pointer to attestation authentication state of image.\r
364\r
365 @return Status of PeiLoadImageLoadImage().\r
366\r
367**/\r
b0d803fe 368EFI_STATUS\r
369EFIAPI\r
370PeiLoadImageLoadImageWrapper (\r
371 IN CONST EFI_PEI_LOAD_FILE_PPI *This,\r
372 IN EFI_PEI_FILE_HANDLE FileHandle,\r
373 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
374 OUT UINT64 *ImageSizeArg, OPTIONAL\r
375 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
376 OUT UINT32 *AuthenticationState\r
377 )\r
b0d803fe 378{\r
379 return PeiLoadImageLoadImage (\r
380 GetPeiServicesTablePointer (),\r
381 FileHandle,\r
382 ImageAddressArg,\r
383 ImageSizeArg,\r
384 EntryPoint,\r
385 AuthenticationState\r
386 );\r
192f6d4c 387}\r
b0d803fe 388\r
b1f6a7c6 389/**\r
ed299e3c
LG
390 Routine to load image file for subsequent execution by LoadFile Ppi.\r
391 If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE \r
392 XIP image format is used.\r
b1f6a7c6 393\r
ed299e3c
LG
394 @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
395 @param FileHandle - Pointer to the FFS file header of the image.\r
396 @param EntryPoint - Pointer to entry point of specified image file for output.\r
397 @param AuthenticationState - Pointer to attestation authentication state of image.\r
b1f6a7c6 398\r
ed299e3c
LG
399 @retval EFI_SUCCESS - Image is successfully loaded.\r
400 @retval EFI_NOT_FOUND - Fail to locate necessary PPI\r
401 @retval Others - Fail to load file.\r
b1f6a7c6 402\r
403**/\r
b0d803fe 404EFI_STATUS\r
405PeiLoadImage (\r
6c7a807a 406 IN CONST EFI_PEI_SERVICES **PeiServices,\r
b0d803fe 407 IN EFI_PEI_FILE_HANDLE FileHandle,\r
408 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
409 OUT UINT32 *AuthenticationState\r
410 )\r
b0d803fe 411{\r
412 EFI_STATUS PpiStatus;\r
413 EFI_STATUS Status;\r
414 UINTN Index;\r
415 EFI_PEI_LOAD_FILE_PPI *LoadFile;\r
416 EFI_PHYSICAL_ADDRESS ImageAddress;\r
417 UINT64 ImageSize;\r
418\r
419 //\r
420 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.\r
421 // one at a time, until one reports EFI_SUCCESS.\r
422 //\r
423 Index = 0;\r
424 do {\r
425 PpiStatus = PeiServicesLocatePpi (\r
426 &gEfiPeiLoadFilePpiGuid,\r
427 Index,\r
428 NULL,\r
429 (VOID **)&LoadFile\r
430 );\r
431 if (!EFI_ERROR (PpiStatus)) {\r
432 Status = LoadFile->LoadFile (\r
433 LoadFile, \r
434 FileHandle, \r
435 &ImageAddress, \r
436 &ImageSize,\r
437 EntryPoint,\r
438 AuthenticationState\r
439 );\r
440 if (!EFI_ERROR (Status)) {\r
db0b7ad5
LG
441 //\r
442 // The image to be started must have the machine type supported by PeiCore.\r
443 //\r
444 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress)));\r
919df8e6 445 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress))) {\r
919df8e6
LG
446 return EFI_UNSUPPORTED;\r
447 }\r
b0d803fe 448 return Status;\r
449 }\r
450 }\r
451 Index++;\r
452 } while (!EFI_ERROR (PpiStatus));\r
453\r
8c519a56 454 return PpiStatus;\r
b0d803fe 455}\r
456\r
457\r
b1f6a7c6 458/**\r
b0d803fe 459\r
ed299e3c
LG
460 Install Pei Load File PPI.\r
461\r
462\r
463 @param PrivateData - Pointer to PEI_CORE_INSTANCE.\r
464 @param OldCoreData - Pointer to PEI_CORE_INSTANCE.\r
b0d803fe 465\r
b1f6a7c6 466**/\r
467VOID\r
468InitializeImageServices (\r
469 IN PEI_CORE_INSTANCE *PrivateData,\r
470 IN PEI_CORE_INSTANCE *OldCoreData\r
471 )\r
b0d803fe 472{\r
b0d803fe 473 if (OldCoreData == NULL) {\r
474 //\r
475 // The first time we are XIP (running from FLASH). We need to remember the\r
476 // FLASH address so we can reinstall the memory version that runs faster\r
477 //\r
478 PrivateData->XipLoadFile = &gPpiLoadFilePpiList;\r
479 PeiServicesInstallPpi (PrivateData->XipLoadFile);\r
480 } else {\r
481 //\r
482 // 2nd time we are running from memory so replace the XIP version with the \r
483 // new memory version. \r
484 //\r
485 PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList); \r
486 }\r
487}\r
488\r
489\r
490\r