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