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