]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Pei/Image/Image.c
PeiCore: Remove assertion when failing to load PE image
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Image / Image.c
... / ...
CommitLineData
1/** @file\r
2 Pei Core Load Image Support\r
3\r
4Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>\r
5SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7**/\r
8\r
9#include "PeiMain.h"\r
10\r
11\r
12EFI_PEI_LOAD_FILE_PPI mPeiLoadImagePpi = {\r
13 PeiLoadImageLoadImageWrapper\r
14};\r
15\r
16\r
17EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList = {\r
18 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
19 &gEfiPeiLoadFilePpiGuid,\r
20 &mPeiLoadImagePpi\r
21};\r
22\r
23/**\r
24\r
25 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file.\r
26 The function is used for XIP code to have optimized memory copy.\r
27\r
28 @param FileHandle - The handle to the PE/COFF file\r
29 @param FileOffset - The offset, in bytes, into the file to read\r
30 @param ReadSize - The number of bytes to read from the file starting at FileOffset\r
31 @param Buffer - A pointer to the buffer to read the data into.\r
32\r
33 @return EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset\r
34\r
35**/\r
36EFI_STATUS\r
37EFIAPI\r
38PeiImageRead (\r
39 IN VOID *FileHandle,\r
40 IN UINTN FileOffset,\r
41 IN UINTN *ReadSize,\r
42 OUT VOID *Buffer\r
43 )\r
44{\r
45 CHAR8 *Destination8;\r
46 CHAR8 *Source8;\r
47\r
48 Destination8 = Buffer;\r
49 Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
50 if (Destination8 != Source8) {\r
51 CopyMem (Destination8, Source8, *ReadSize);\r
52 }\r
53\r
54 return EFI_SUCCESS;\r
55}\r
56\r
57/**\r
58 To check memory usage bit map array to figure out if the memory range the image will be loaded in is available or not. If\r
59 memory range is available, the function will mark the corresponding bits to 1 which indicates the memory range is used.\r
60 The function is only invoked when load modules at fixed address feature is enabled.\r
61\r
62 @param Private Pointer to the private data passed in from caller\r
63 @param ImageBase The base address the image will be loaded at.\r
64 @param ImageSize The size of the image\r
65\r
66 @retval EFI_SUCCESS The memory range the image will be loaded in is available\r
67 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available\r
68**/\r
69EFI_STATUS\r
70CheckAndMarkFixLoadingMemoryUsageBitMap (\r
71 IN PEI_CORE_INSTANCE *Private,\r
72 IN EFI_PHYSICAL_ADDRESS ImageBase,\r
73 IN UINT32 ImageSize\r
74 )\r
75{\r
76 UINT32 DxeCodePageNumber;\r
77 UINT64 ReservedCodeSize;\r
78 EFI_PHYSICAL_ADDRESS PeiCodeBase;\r
79 UINT32 BaseOffsetPageNumber;\r
80 UINT32 TopOffsetPageNumber;\r
81 UINT32 Index;\r
82 UINT64 *MemoryUsageBitMap;\r
83\r
84\r
85 //\r
86 // The reserved code range includes RuntimeCodePage range, Boot time code range and PEI code range.\r
87 //\r
88 DxeCodePageNumber = PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r
89 DxeCodePageNumber += PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r
90 ReservedCodeSize = EFI_PAGES_TO_SIZE(DxeCodePageNumber + PcdGet32(PcdLoadFixAddressPeiCodePageNumber));\r
91 PeiCodeBase = Private->LoadModuleAtFixAddressTopAddress - ReservedCodeSize;\r
92\r
93 //\r
94 // Test the memory range for loading the image in the PEI code range.\r
95 //\r
96 if ((Private->LoadModuleAtFixAddressTopAddress - EFI_PAGES_TO_SIZE(DxeCodePageNumber)) < (ImageBase + ImageSize) ||\r
97 (PeiCodeBase > ImageBase)) {\r
98 return EFI_NOT_FOUND;\r
99 }\r
100\r
101 //\r
102 // Test if the memory is available or not.\r
103 //\r
104 MemoryUsageBitMap = Private->PeiCodeMemoryRangeUsageBitMap;\r
105 BaseOffsetPageNumber = EFI_SIZE_TO_PAGES((UINT32)(ImageBase - PeiCodeBase));\r
106 TopOffsetPageNumber = EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - PeiCodeBase));\r
107 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
108 if ((MemoryUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {\r
109 //\r
110 // This page is already used.\r
111 //\r
112 return EFI_NOT_FOUND;\r
113 }\r
114 }\r
115\r
116 //\r
117 // Being here means the memory range is available. So mark the bits for the memory range\r
118 //\r
119 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
120 MemoryUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));\r
121 }\r
122 return EFI_SUCCESS;\r
123}\r
124/**\r
125\r
126 Get the fixed loading address from image header assigned by build tool. This function only be called\r
127 when Loading module at Fixed address feature enabled.\r
128\r
129 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
130 image that needs to be examined by this function.\r
131 @param Private Pointer to the private data passed in from caller\r
132\r
133 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .\r
134 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.\r
135\r
136**/\r
137EFI_STATUS\r
138GetPeCoffImageFixLoadingAssignedAddress(\r
139 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r
140 IN PEI_CORE_INSTANCE *Private\r
141 )\r
142{\r
143 UINTN SectionHeaderOffset;\r
144 EFI_STATUS Status;\r
145 EFI_IMAGE_SECTION_HEADER SectionHeader;\r
146 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r
147 EFI_PHYSICAL_ADDRESS FixLoadingAddress;\r
148 UINT16 Index;\r
149 UINTN Size;\r
150 UINT16 NumberOfSections;\r
151 UINT64 ValueInSectionHeader;\r
152\r
153\r
154 FixLoadingAddress = 0;\r
155 Status = EFI_NOT_FOUND;\r
156\r
157 //\r
158 // Get PeHeader pointer\r
159 //\r
160 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r
161 if (ImageContext->IsTeImage) {\r
162 //\r
163 // for TE image, the fix loading address is saved in first section header that doesn't point\r
164 // to code section.\r
165 //\r
166 SectionHeaderOffset = sizeof (EFI_TE_IMAGE_HEADER);\r
167 NumberOfSections = ImgHdr->Te.NumberOfSections;\r
168 } else {\r
169 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +\r
170 sizeof (UINT32) +\r
171 sizeof (EFI_IMAGE_FILE_HEADER) +\r
172 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;\r
173 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
174 }\r
175 //\r
176 // Get base address from the first section header that doesn't point to code section.\r
177 //\r
178 for (Index = 0; Index < NumberOfSections; Index++) {\r
179 //\r
180 // Read section header from file\r
181 //\r
182 Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
183 Status = ImageContext->ImageRead (\r
184 ImageContext->Handle,\r
185 SectionHeaderOffset,\r
186 &Size,\r
187 &SectionHeader\r
188 );\r
189 if (EFI_ERROR (Status)) {\r
190 return Status;\r
191 }\r
192\r
193 Status = EFI_NOT_FOUND;\r
194\r
195 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r
196 //\r
197 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header\r
198 // that doesn't point to code section in image header, as well as ImageBase field of image header. A notable thing is\r
199 // that for PEIM, the value in ImageBase field may not be equal to the value in PointerToRelocations & PointerToLineNumbers because\r
200 // for XIP PEIM, ImageBase field holds the image base address running on the Flash. And PointerToRelocations & PointerToLineNumbers\r
201 // hold the image base address when it is shadow to the memory. And there is an assumption that when the feature is enabled, if a\r
202 // module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers fields should NOT be Zero, or\r
203 // else, these 2 fields should be set to Zero\r
204 //\r
205 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r
206 if (ValueInSectionHeader != 0) {\r
207 //\r
208 // Found first section header that doesn't point to code section.\r
209 //\r
210 if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0) {\r
211 //\r
212 // When LMFA feature is configured as Load Module at Fixed Absolute Address mode, PointerToRelocations & PointerToLineNumbers field\r
213 // hold the absolute address of image base running in memory\r
214 //\r
215 FixLoadingAddress = ValueInSectionHeader;\r
216 } else {\r
217 //\r
218 // When LMFA feature is configured as Load Module at Fixed offset mode, PointerToRelocations & PointerToLineNumbers field\r
219 // hold the offset relative to a platform-specific top address.\r
220 //\r
221 FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(Private->LoadModuleAtFixAddressTopAddress + (INT64)ValueInSectionHeader);\r
222 }\r
223 //\r
224 // Check if the memory range is available.\r
225 //\r
226 Status = CheckAndMarkFixLoadingMemoryUsageBitMap (Private, FixLoadingAddress, (UINT32) ImageContext->ImageSize);\r
227 if (!EFI_ERROR(Status)) {\r
228 //\r
229 // The assigned address is valid. Return the specified loading address\r
230 //\r
231 ImageContext->ImageAddress = FixLoadingAddress;\r
232 }\r
233 }\r
234 break;\r
235 }\r
236 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
237 }\r
238 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address 0x%11p. Status= %r \n", (VOID *)(UINTN)FixLoadingAddress, Status));\r
239 return Status;\r
240}\r
241/**\r
242\r
243 Loads and relocates a PE/COFF image into memory.\r
244 If the image is not relocatable, it will not be loaded into memory and be loaded as XIP image.\r
245\r
246 @param FileHandle - Pointer to the FFS file header of the image.\r
247 @param Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r
248 @param ImageAddress - The base address of the relocated PE/COFF image\r
249 @param ImageSize - The size of the relocated PE/COFF image\r
250 @param EntryPoint - The entry point of the relocated PE/COFF image\r
251\r
252 @retval EFI_SUCCESS The file was loaded and relocated\r
253 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file\r
254 @retval EFI_WARN_BUFFER_TOO_SMALL\r
255 There is not enough heap to allocate the requested size.\r
256 This will not prevent the XIP image from being invoked.\r
257\r
258**/\r
259EFI_STATUS\r
260LoadAndRelocatePeCoffImage (\r
261 IN EFI_PEI_FILE_HANDLE FileHandle,\r
262 IN VOID *Pe32Data,\r
263 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r
264 OUT UINT64 *ImageSize,\r
265 OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r
266 )\r
267{\r
268 EFI_STATUS Status;\r
269 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
270 PEI_CORE_INSTANCE *Private;\r
271 UINT64 AlignImageSize;\r
272 BOOLEAN IsXipImage;\r
273 EFI_STATUS ReturnStatus;\r
274 BOOLEAN IsS3Boot;\r
275 BOOLEAN IsPeiModule;\r
276 BOOLEAN IsRegisterForShadow;\r
277 EFI_FV_FILE_INFO FileInfo;\r
278\r
279 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
280\r
281 ReturnStatus = EFI_SUCCESS;\r
282 IsXipImage = FALSE;\r
283 ZeroMem (&ImageContext, sizeof (ImageContext));\r
284 ImageContext.Handle = Pe32Data;\r
285 ImageContext.ImageRead = PeiImageRead;\r
286\r
287 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
288 if (EFI_ERROR (Status)) {\r
289 return Status;\r
290 }\r
291\r
292 //\r
293 // Initialize local IsS3Boot and IsRegisterForShadow variable\r
294 //\r
295 IsS3Boot = FALSE;\r
296 if (Private->HobList.HandoffInformationTable->BootMode == BOOT_ON_S3_RESUME) {\r
297 IsS3Boot = TRUE;\r
298 }\r
299 IsRegisterForShadow = FALSE;\r
300 if ((Private->CurrentFileHandle == FileHandle)\r
301 && (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] == PEIM_STATE_REGISTER_FOR_SHADOW)) {\r
302 IsRegisterForShadow = TRUE;\r
303 }\r
304\r
305 //\r
306 // XIP image that ImageAddress is same to Image handle.\r
307 //\r
308 if (ImageContext.ImageAddress == (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data) {\r
309 IsXipImage = TRUE;\r
310 }\r
311\r
312 //\r
313 // Get file type first\r
314 //\r
315 Status = PeiServicesFfsGetFileInfo (FileHandle, &FileInfo);\r
316 ASSERT_EFI_ERROR (Status);\r
317\r
318 //\r
319 // Check whether the file type is PEI module.\r
320 //\r
321 IsPeiModule = FALSE;\r
322 if (FileInfo.FileType == EFI_FV_FILETYPE_PEI_CORE ||\r
323 FileInfo.FileType == EFI_FV_FILETYPE_PEIM ||\r
324 FileInfo.FileType == EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER) {\r
325 IsPeiModule = TRUE;\r
326 }\r
327\r
328 //\r
329 // When Image has no reloc section, it can't be relocated into memory.\r
330 //\r
331 if (ImageContext.RelocationsStripped && (Private->PeiMemoryInstalled) &&\r
332 ((!IsPeiModule) || PcdGetBool (PcdMigrateTemporaryRamFirmwareVolumes) ||\r
333 (!IsS3Boot && (PcdGetBool (PcdShadowPeimOnBoot) || IsRegisterForShadow)) ||\r
334 (IsS3Boot && PcdGetBool (PcdShadowPeimOnS3Boot)))\r
335 ) {\r
336 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "The image at 0x%08x without reloc section can't be loaded into memory\n", (UINTN) Pe32Data));\r
337 }\r
338\r
339 //\r
340 // Set default base address to current image address.\r
341 //\r
342 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
343\r
344 //\r
345 // Allocate Memory for the image when memory is ready, and image is relocatable.\r
346 // On normal boot, PcdShadowPeimOnBoot decides whether load PEIM or PeiCore into memory.\r
347 // On S3 boot, PcdShadowPeimOnS3Boot decides whether load PEIM or PeiCore into memory.\r
348 //\r
349 if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) &&\r
350 ((!IsPeiModule) || PcdGetBool (PcdMigrateTemporaryRamFirmwareVolumes) ||\r
351 (!IsS3Boot && (PcdGetBool (PcdShadowPeimOnBoot) || IsRegisterForShadow)) ||\r
352 (IsS3Boot && PcdGetBool (PcdShadowPeimOnS3Boot)))\r
353 ) {\r
354 //\r
355 // Allocate more buffer to avoid buffer overflow.\r
356 //\r
357 if (ImageContext.IsTeImage) {\r
358 AlignImageSize = ImageContext.ImageSize + ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER);\r
359 } else {\r
360 AlignImageSize = ImageContext.ImageSize;\r
361 }\r
362\r
363 if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r
364 AlignImageSize += ImageContext.SectionAlignment;\r
365 }\r
366\r
367 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
368 Status = GetPeCoffImageFixLoadingAssignedAddress(&ImageContext, Private);\r
369 if (EFI_ERROR (Status)){\r
370 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));\r
371 //\r
372 // The PEIM is not assigned valid address, try to allocate page to load it.\r
373 //\r
374 Status = PeiServicesAllocatePages (EfiBootServicesCode,\r
375 EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize),\r
376 &ImageContext.ImageAddress);\r
377 }\r
378 } else {\r
379 Status = PeiServicesAllocatePages (EfiBootServicesCode,\r
380 EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize),\r
381 &ImageContext.ImageAddress);\r
382 }\r
383 if (!EFI_ERROR (Status)) {\r
384 //\r
385 // Adjust the Image Address to make sure it is section alignment.\r
386 //\r
387 if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r
388 ImageContext.ImageAddress =\r
389 (ImageContext.ImageAddress + ImageContext.SectionAlignment - 1) &\r
390 ~((UINTN)ImageContext.SectionAlignment - 1);\r
391 }\r
392 //\r
393 // Fix alignment requirement when Load IPF TeImage into memory.\r
394 // Skip the reserved space for the stripped PeHeader when load TeImage into memory.\r
395 //\r
396 if (ImageContext.IsTeImage) {\r
397 ImageContext.ImageAddress = ImageContext.ImageAddress +\r
398 ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -\r
399 sizeof (EFI_TE_IMAGE_HEADER);\r
400 }\r
401 } else {\r
402 //\r
403 // No enough memory resource.\r
404 //\r
405 if (IsXipImage) {\r
406 //\r
407 // XIP image can still be invoked.\r
408 //\r
409 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
410 ReturnStatus = EFI_WARN_BUFFER_TOO_SMALL;\r
411 } else {\r
412 //\r
413 // Non XIP image can't be loaded because no enough memory is allocated.\r
414 //\r
415 ASSERT (FALSE);\r
416 return EFI_OUT_OF_RESOURCES;\r
417 }\r
418 }\r
419 }\r
420\r
421 //\r
422 // Load the image to our new buffer\r
423 //\r
424 Status = PeCoffLoaderLoadImage (&ImageContext);\r
425 if (EFI_ERROR (Status)) {\r
426 if (ImageContext.ImageError == IMAGE_ERROR_INVALID_SECTION_ALIGNMENT) {\r
427 DEBUG ((DEBUG_ERROR, "PEIM Image Address 0x%11p doesn't meet with section alignment 0x%x.\n", (VOID*)(UINTN)ImageContext.ImageAddress, ImageContext.SectionAlignment));\r
428 }\r
429 return Status;\r
430 }\r
431 //\r
432 // Relocate the image in our new buffer\r
433 //\r
434 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
435 if (EFI_ERROR (Status)) {\r
436 return Status;\r
437 }\r
438\r
439 //\r
440 // Flush the instruction cache so the image data is written before we execute it\r
441 //\r
442 if (ImageContext.ImageAddress != (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data) {\r
443 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
444 }\r
445\r
446 *ImageAddress = ImageContext.ImageAddress;\r
447 *ImageSize = ImageContext.ImageSize;\r
448 *EntryPoint = ImageContext.EntryPoint;\r
449\r
450 return ReturnStatus;\r
451}\r
452\r
453/**\r
454 Loads and relocates a PE/COFF image in place.\r
455\r
456 @param Pe32Data The base address of the PE/COFF file that is to be loaded and relocated\r
457 @param ImageAddress The base address of the relocated PE/COFF image\r
458\r
459 @retval EFI_SUCCESS The file was loaded and relocated.\r
460 @retval Others The file not be loaded and error occurred.\r
461\r
462**/\r
463EFI_STATUS\r
464LoadAndRelocatePeCoffImageInPlace (\r
465 IN VOID *Pe32Data,\r
466 IN VOID *ImageAddress\r
467 )\r
468{\r
469 EFI_STATUS Status;\r
470 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
471\r
472 ZeroMem (&ImageContext, sizeof (ImageContext));\r
473 ImageContext.Handle = Pe32Data;\r
474 ImageContext.ImageRead = PeiImageRead;\r
475\r
476 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
477 if (EFI_ERROR (Status)) {\r
478 ASSERT_EFI_ERROR (Status);\r
479 return Status;\r
480 }\r
481\r
482 ImageContext.ImageAddress = (PHYSICAL_ADDRESS)(UINTN) ImageAddress;\r
483\r
484 //\r
485 // Load the image in place\r
486 //\r
487 Status = PeCoffLoaderLoadImage (&ImageContext);\r
488 if (EFI_ERROR (Status)) {\r
489 ASSERT_EFI_ERROR (Status);\r
490 return Status;\r
491 }\r
492\r
493 //\r
494 // Relocate the image in place\r
495 //\r
496 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
497 if (EFI_ERROR (Status)) {\r
498 ASSERT_EFI_ERROR (Status);\r
499 return Status;\r
500 }\r
501\r
502 //\r
503 // Flush the instruction cache so the image data is written before we execute it\r
504 //\r
505 if (ImageContext.ImageAddress != (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data) {\r
506 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
507 }\r
508\r
509 return Status;\r
510}\r
511\r
512/**\r
513 Find the PE32 Data for an FFS file.\r
514\r
515 @param FileHandle Pointer to the FFS file header of the image.\r
516 @param Pe32Data Pointer to a (VOID *) PE32 Data pointer.\r
517\r
518 @retval EFI_SUCCESS Image is successfully loaded.\r
519 @retval EFI_NOT_FOUND Fail to locate PE32 Data.\r
520\r
521**/\r
522EFI_STATUS\r
523PeiGetPe32Data (\r
524 IN EFI_PEI_FILE_HANDLE FileHandle,\r
525 OUT VOID **Pe32Data\r
526 )\r
527{\r
528 EFI_STATUS Status;\r
529 EFI_SECTION_TYPE SearchType1;\r
530 EFI_SECTION_TYPE SearchType2;\r
531 UINT32 AuthenticationState;\r
532\r
533 *Pe32Data = NULL;\r
534\r
535 if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst)) {\r
536 SearchType1 = EFI_SECTION_TE;\r
537 SearchType2 = EFI_SECTION_PE32;\r
538 } else {\r
539 SearchType1 = EFI_SECTION_PE32;\r
540 SearchType2 = EFI_SECTION_TE;\r
541 }\r
542\r
543 //\r
544 // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst\r
545 // is true, TE will be searched first).\r
546 //\r
547 Status = PeiServicesFfsFindSectionData3 (\r
548 SearchType1,\r
549 0,\r
550 FileHandle,\r
551 Pe32Data,\r
552 &AuthenticationState\r
553 );\r
554 //\r
555 // If we didn't find a first exe section, try to find the second exe section.\r
556 //\r
557 if (EFI_ERROR (Status)) {\r
558 Status = PeiServicesFfsFindSectionData3 (\r
559 SearchType2,\r
560 0,\r
561 FileHandle,\r
562 Pe32Data,\r
563 &AuthenticationState\r
564 );\r
565 }\r
566 return Status;\r
567}\r
568\r
569/**\r
570 Loads a PEIM into memory for subsequent execution. If there are compressed\r
571 images or images that need to be relocated into memory for performance reasons,\r
572 this service performs that transformation.\r
573\r
574 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
575 @param FileHandle Pointer to the FFS file header of the image.\r
576 @param ImageAddressArg Pointer to PE/TE image.\r
577 @param ImageSizeArg Size of PE/TE image.\r
578 @param EntryPoint Pointer to entry point of specified image file for output.\r
579 @param AuthenticationState - Pointer to attestation authentication state of image.\r
580\r
581 @retval EFI_SUCCESS Image is successfully loaded.\r
582 @retval EFI_NOT_FOUND Fail to locate necessary PPI.\r
583 @retval EFI_UNSUPPORTED Image Machine Type is not supported.\r
584 @retval EFI_WARN_BUFFER_TOO_SMALL\r
585 There is not enough heap to allocate the requested size.\r
586 This will not prevent the XIP image from being invoked.\r
587\r
588**/\r
589EFI_STATUS\r
590PeiLoadImageLoadImage (\r
591 IN CONST EFI_PEI_SERVICES **PeiServices,\r
592 IN EFI_PEI_FILE_HANDLE FileHandle,\r
593 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
594 OUT UINT64 *ImageSizeArg, OPTIONAL\r
595 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
596 OUT UINT32 *AuthenticationState\r
597 )\r
598{\r
599 EFI_STATUS Status;\r
600 VOID *Pe32Data;\r
601 EFI_PHYSICAL_ADDRESS ImageAddress;\r
602 UINT64 ImageSize;\r
603 EFI_PHYSICAL_ADDRESS ImageEntryPoint;\r
604 UINT16 Machine;\r
605 EFI_SECTION_TYPE SearchType1;\r
606 EFI_SECTION_TYPE SearchType2;\r
607\r
608 *EntryPoint = 0;\r
609 ImageSize = 0;\r
610 *AuthenticationState = 0;\r
611\r
612 if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst)) {\r
613 SearchType1 = EFI_SECTION_TE;\r
614 SearchType2 = EFI_SECTION_PE32;\r
615 } else {\r
616 SearchType1 = EFI_SECTION_PE32;\r
617 SearchType2 = EFI_SECTION_TE;\r
618 }\r
619\r
620 //\r
621 // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst\r
622 // is true, TE will be searched first).\r
623 //\r
624 Status = PeiServicesFfsFindSectionData3 (\r
625 SearchType1,\r
626 0,\r
627 FileHandle,\r
628 &Pe32Data,\r
629 AuthenticationState\r
630 );\r
631 //\r
632 // If we didn't find a first exe section, try to find the second exe section.\r
633 //\r
634 if (EFI_ERROR (Status)) {\r
635 Status = PeiServicesFfsFindSectionData3 (\r
636 SearchType2,\r
637 0,\r
638 FileHandle,\r
639 &Pe32Data,\r
640 AuthenticationState\r
641 );\r
642 if (EFI_ERROR (Status)) {\r
643 //\r
644 // PEI core only carry the loader function for TE and PE32 executables\r
645 // If this two section does not exist, just return.\r
646 //\r
647 return Status;\r
648 }\r
649 }\r
650\r
651 DEBUG ((DEBUG_INFO, "Loading PEIM %g\n", FileHandle));\r
652\r
653 //\r
654 // If memory is installed, perform the shadow operations\r
655 //\r
656 Status = LoadAndRelocatePeCoffImage (\r
657 FileHandle,\r
658 Pe32Data,\r
659 &ImageAddress,\r
660 &ImageSize,\r
661 &ImageEntryPoint\r
662 );\r
663\r
664 if (EFI_ERROR (Status)) {\r
665 return Status;\r
666 }\r
667\r
668 //\r
669 // Got the entry point from the loaded Pe32Data\r
670 //\r
671 Pe32Data = (VOID *) ((UINTN) ImageAddress);\r
672 *EntryPoint = ImageEntryPoint;\r
673\r
674 Machine = PeCoffLoaderGetMachineType (Pe32Data);\r
675\r
676 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {\r
677 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine)) {\r
678 return EFI_UNSUPPORTED;\r
679 }\r
680 }\r
681\r
682 if (ImageAddressArg != NULL) {\r
683 *ImageAddressArg = ImageAddress;\r
684 }\r
685\r
686 if (ImageSizeArg != NULL) {\r
687 *ImageSizeArg = ImageSize;\r
688 }\r
689\r
690 DEBUG_CODE_BEGIN ();\r
691 CHAR8 *AsciiString;\r
692 CHAR8 EfiFileName[512];\r
693 INT32 Index;\r
694 INT32 StartIndex;\r
695\r
696 //\r
697 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
698 //\r
699 if (Machine != EFI_IMAGE_MACHINE_IA64) {\r
700 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));\r
701 } else {\r
702 //\r
703 // For IPF Image, the real entry point should be print.\r
704 //\r
705 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)(*(UINT64 *)(UINTN)*EntryPoint)));\r
706 }\r
707\r
708 //\r
709 // Print Module Name by PeImage PDB file name.\r
710 //\r
711 AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r
712\r
713 if (AsciiString != NULL) {\r
714 StartIndex = 0;\r
715 for (Index = 0; AsciiString[Index] != 0; Index++) {\r
716 if (AsciiString[Index] == '\\' || AsciiString[Index] == '/') {\r
717 StartIndex = Index + 1;\r
718 }\r
719 }\r
720\r
721 //\r
722 // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r
723 // The PDB file name is limited in the range of 0~511.\r
724 // If the length is bigger than 511, trim the redundant characters to avoid overflow in array boundary.\r
725 //\r
726 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
727 EfiFileName[Index] = AsciiString[Index + StartIndex];\r
728 if (EfiFileName[Index] == 0) {\r
729 EfiFileName[Index] = '.';\r
730 }\r
731 if (EfiFileName[Index] == '.') {\r
732 EfiFileName[Index + 1] = 'e';\r
733 EfiFileName[Index + 2] = 'f';\r
734 EfiFileName[Index + 3] = 'i';\r
735 EfiFileName[Index + 4] = 0;\r
736 break;\r
737 }\r
738 }\r
739\r
740 if (Index == sizeof (EfiFileName) - 4) {\r
741 EfiFileName[Index] = 0;\r
742 }\r
743\r
744 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a", EfiFileName));\r
745 }\r
746\r
747 DEBUG_CODE_END ();\r
748\r
749 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r
750\r
751 return EFI_SUCCESS;\r
752\r
753}\r
754\r
755\r
756/**\r
757 The wrapper function of PeiLoadImageLoadImage().\r
758\r
759 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.\r
760 @param FileHandle - Pointer to the FFS file header of the image.\r
761 @param ImageAddressArg - Pointer to PE/TE image.\r
762 @param ImageSizeArg - Size of PE/TE image.\r
763 @param EntryPoint - Pointer to entry point of specified image file for output.\r
764 @param AuthenticationState - Pointer to attestation authentication state of image.\r
765\r
766 @return Status of PeiLoadImageLoadImage().\r
767\r
768**/\r
769EFI_STATUS\r
770EFIAPI\r
771PeiLoadImageLoadImageWrapper (\r
772 IN CONST EFI_PEI_LOAD_FILE_PPI *This,\r
773 IN EFI_PEI_FILE_HANDLE FileHandle,\r
774 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
775 OUT UINT64 *ImageSizeArg, OPTIONAL\r
776 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
777 OUT UINT32 *AuthenticationState\r
778 )\r
779{\r
780 return PeiLoadImageLoadImage (\r
781 GetPeiServicesTablePointer (),\r
782 FileHandle,\r
783 ImageAddressArg,\r
784 ImageSizeArg,\r
785 EntryPoint,\r
786 AuthenticationState\r
787 );\r
788}\r
789\r
790/**\r
791 Check whether the input image has the relocation.\r
792\r
793 @param Pe32Data Pointer to the PE/COFF or TE image.\r
794\r
795 @retval TRUE Relocation is stripped.\r
796 @retval FALSE Relocation is not stripped.\r
797\r
798**/\r
799BOOLEAN\r
800RelocationIsStrip (\r
801 IN VOID *Pe32Data\r
802 )\r
803{\r
804 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
805 EFI_IMAGE_DOS_HEADER *DosHdr;\r
806\r
807 ASSERT (Pe32Data != NULL);\r
808\r
809 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
810 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
811 //\r
812 // DOS image header is present, so read the PE header after the DOS image header.\r
813 //\r
814 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r
815 } else {\r
816 //\r
817 // DOS image header is not present, so PE header is at the image base.\r
818 //\r
819 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
820 }\r
821\r
822 //\r
823 // Three cases with regards to relocations:\r
824 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable\r
825 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable\r
826 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but\r
827 // has no base relocs to apply\r
828 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.\r
829 //\r
830 // Look at the file header to determine if relocations have been stripped, and\r
831 // save this info in the image context for later use.\r
832 //\r
833 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
834 if ((Hdr.Te->DataDirectory[0].Size == 0) && (Hdr.Te->DataDirectory[0].VirtualAddress == 0)) {\r
835 return TRUE;\r
836 } else {\r
837 return FALSE;\r
838 }\r
839 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
840 if ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0) {\r
841 return TRUE;\r
842 } else {\r
843 return FALSE;\r
844 }\r
845 }\r
846\r
847 return FALSE;\r
848}\r
849\r
850/**\r
851 Routine to load image file for subsequent execution by LoadFile Ppi.\r
852 If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE\r
853 XIP image format is used.\r
854\r
855 @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
856 @param FileHandle - Pointer to the FFS file header of the image.\r
857 @param PeimState - The dispatch state of the input PEIM handle.\r
858 @param EntryPoint - Pointer to entry point of specified image file for output.\r
859 @param AuthenticationState - Pointer to attestation authentication state of image.\r
860\r
861 @retval EFI_SUCCESS - Image is successfully loaded.\r
862 @retval EFI_NOT_FOUND - Fail to locate necessary PPI\r
863 @retval Others - Fail to load file.\r
864\r
865**/\r
866EFI_STATUS\r
867PeiLoadImage (\r
868 IN CONST EFI_PEI_SERVICES **PeiServices,\r
869 IN EFI_PEI_FILE_HANDLE FileHandle,\r
870 IN UINT8 PeimState,\r
871 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
872 OUT UINT32 *AuthenticationState\r
873 )\r
874{\r
875 EFI_STATUS PpiStatus;\r
876 EFI_STATUS Status;\r
877 UINTN Index;\r
878 EFI_PEI_LOAD_FILE_PPI *LoadFile;\r
879 EFI_PHYSICAL_ADDRESS ImageAddress;\r
880 UINT64 ImageSize;\r
881 BOOLEAN IsStrip;\r
882\r
883 IsStrip = FALSE;\r
884 //\r
885 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.\r
886 // one at a time, until one reports EFI_SUCCESS.\r
887 //\r
888 Index = 0;\r
889 do {\r
890 PpiStatus = PeiServicesLocatePpi (\r
891 &gEfiPeiLoadFilePpiGuid,\r
892 Index,\r
893 NULL,\r
894 (VOID **)&LoadFile\r
895 );\r
896 if (!EFI_ERROR (PpiStatus)) {\r
897 Status = LoadFile->LoadFile (\r
898 LoadFile,\r
899 FileHandle,\r
900 &ImageAddress,\r
901 &ImageSize,\r
902 EntryPoint,\r
903 AuthenticationState\r
904 );\r
905 if (!EFI_ERROR (Status) || Status == EFI_WARN_BUFFER_TOO_SMALL) {\r
906 //\r
907 // The shadowed PEIM must be relocatable.\r
908 //\r
909 if (PeimState == PEIM_STATE_REGISTER_FOR_SHADOW) {\r
910 IsStrip = RelocationIsStrip ((VOID *) (UINTN) ImageAddress);\r
911 ASSERT (!IsStrip);\r
912 if (IsStrip) {\r
913 return EFI_UNSUPPORTED;\r
914 }\r
915 }\r
916\r
917 //\r
918 // The image to be started must have the machine type supported by PeiCore.\r
919 //\r
920 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress)));\r
921 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress))) {\r
922 return EFI_UNSUPPORTED;\r
923 }\r
924 return EFI_SUCCESS;\r
925 }\r
926 }\r
927 Index++;\r
928 } while (!EFI_ERROR (PpiStatus));\r
929\r
930 return PpiStatus;\r
931}\r
932\r
933\r
934/**\r
935\r
936 Install Pei Load File PPI.\r
937\r
938\r
939 @param PrivateData - Pointer to PEI_CORE_INSTANCE.\r
940 @param OldCoreData - Pointer to PEI_CORE_INSTANCE.\r
941\r
942**/\r
943VOID\r
944InitializeImageServices (\r
945 IN PEI_CORE_INSTANCE *PrivateData,\r
946 IN PEI_CORE_INSTANCE *OldCoreData\r
947 )\r
948{\r
949 if (OldCoreData == NULL) {\r
950 //\r
951 // The first time we are XIP (running from FLASH). We need to remember the\r
952 // FLASH address so we can reinstall the memory version that runs faster\r
953 //\r
954 PrivateData->XipLoadFile = &gPpiLoadFilePpiList;\r
955 PeiServicesInstallPpi (PrivateData->XipLoadFile);\r
956 } else {\r
957 //\r
958 // 2nd time we are running from memory so replace the XIP version with the\r
959 // new memory version.\r
960 //\r
961 PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList);\r
962 }\r
963}\r
964\r
965\r
966\r
967\r