]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Pei/Image/Image.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[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 - 2018, 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 avalaible 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 // Initilize 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) && ((!IsPeiModule) ||\r
332 (!IsS3Boot && (PcdGetBool (PcdShadowPeimOnBoot) || IsRegisterForShadow)) || (IsS3Boot && PcdGetBool (PcdShadowPeimOnS3Boot)))) {\r
333 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
334 }\r
335\r
336 //\r
337 // Set default base address to current image address.\r
338 //\r
339 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
340\r
341 //\r
342 // Allocate Memory for the image when memory is ready, and image is relocatable.\r
343 // On normal boot, PcdShadowPeimOnBoot decides whether load PEIM or PeiCore into memory.\r
344 // On S3 boot, PcdShadowPeimOnS3Boot decides whether load PEIM or PeiCore into memory.\r
345 //\r
346 if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && ((!IsPeiModule) ||\r
347 (!IsS3Boot && (PcdGetBool (PcdShadowPeimOnBoot) || IsRegisterForShadow)) || (IsS3Boot && PcdGetBool (PcdShadowPeimOnS3Boot)))) {\r
348 //\r
349 // Allocate more buffer to avoid buffer overflow.\r
350 //\r
351 if (ImageContext.IsTeImage) {\r
352 AlignImageSize = ImageContext.ImageSize + ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER);\r
353 } else {\r
354 AlignImageSize = ImageContext.ImageSize;\r
355 }\r
356\r
357 if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r
358 AlignImageSize += ImageContext.SectionAlignment;\r
359 }\r
360\r
361 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
362 Status = GetPeCoffImageFixLoadingAssignedAddress(&ImageContext, Private);\r
363 if (EFI_ERROR (Status)){\r
364 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));\r
365 //\r
366 // The PEIM is not assiged valid address, try to allocate page to load it.\r
367 //\r
368 Status = PeiServicesAllocatePages (EfiBootServicesCode,\r
369 EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize),\r
370 &ImageContext.ImageAddress);\r
371 }\r
372 } else {\r
373 Status = PeiServicesAllocatePages (EfiBootServicesCode,\r
374 EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize),\r
375 &ImageContext.ImageAddress);\r
376 }\r
377 if (!EFI_ERROR (Status)) {\r
378 //\r
379 // Adjust the Image Address to make sure it is section alignment.\r
380 //\r
381 if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r
382 ImageContext.ImageAddress =\r
383 (ImageContext.ImageAddress + ImageContext.SectionAlignment - 1) &\r
384 ~((UINTN)ImageContext.SectionAlignment - 1);\r
385 }\r
386 //\r
387 // Fix alignment requirement when Load IPF TeImage into memory.\r
388 // Skip the reserved space for the stripped PeHeader when load TeImage into memory.\r
389 //\r
390 if (ImageContext.IsTeImage) {\r
391 ImageContext.ImageAddress = ImageContext.ImageAddress +\r
392 ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -\r
393 sizeof (EFI_TE_IMAGE_HEADER);\r
394 }\r
395 } else {\r
396 //\r
397 // No enough memory resource.\r
398 //\r
399 if (IsXipImage) {\r
400 //\r
401 // XIP image can still be invoked.\r
402 //\r
403 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
404 ReturnStatus = EFI_WARN_BUFFER_TOO_SMALL;\r
405 } else {\r
406 //\r
407 // Non XIP image can't be loaded because no enough memory is allocated.\r
408 //\r
409 ASSERT (FALSE);\r
410 return EFI_OUT_OF_RESOURCES;\r
411 }\r
412 }\r
413 }\r
414\r
415 //\r
416 // Load the image to our new buffer\r
417 //\r
418 Status = PeCoffLoaderLoadImage (&ImageContext);\r
419 if (EFI_ERROR (Status)) {\r
420 if (ImageContext.ImageError == IMAGE_ERROR_INVALID_SECTION_ALIGNMENT) {\r
421 DEBUG ((DEBUG_ERROR, "PEIM Image Address 0x%11p doesn't meet with section alignment 0x%x.\n", (VOID*)(UINTN)ImageContext.ImageAddress, ImageContext.SectionAlignment));\r
422 }\r
423 return Status;\r
424 }\r
425 //\r
426 // Relocate the image in our new buffer\r
427 //\r
428 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
429 if (EFI_ERROR (Status)) {\r
430 return Status;\r
431 }\r
432\r
433 //\r
434 // Flush the instruction cache so the image data is written before we execute it\r
435 //\r
436 if (ImageContext.ImageAddress != (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data) {\r
437 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
438 }\r
439\r
440 *ImageAddress = ImageContext.ImageAddress;\r
441 *ImageSize = ImageContext.ImageSize;\r
442 *EntryPoint = ImageContext.EntryPoint;\r
443\r
444 return ReturnStatus;\r
445}\r
446\r
447/**\r
448 Loads a PEIM into memory for subsequent execution. If there are compressed\r
449 images or images that need to be relocated into memory for performance reasons,\r
450 this service performs that transformation.\r
451\r
452 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
453 @param FileHandle Pointer to the FFS file header of the image.\r
454 @param ImageAddressArg Pointer to PE/TE image.\r
455 @param ImageSizeArg Size of PE/TE image.\r
456 @param EntryPoint Pointer to entry point of specified image file for output.\r
457 @param AuthenticationState - Pointer to attestation authentication state of image.\r
458\r
459 @retval EFI_SUCCESS Image is successfully loaded.\r
460 @retval EFI_NOT_FOUND Fail to locate necessary PPI.\r
461 @retval EFI_UNSUPPORTED Image Machine Type is not supported.\r
462 @retval EFI_WARN_BUFFER_TOO_SMALL\r
463 There is not enough heap to allocate the requested size.\r
464 This will not prevent the XIP image from being invoked.\r
465\r
466**/\r
467EFI_STATUS\r
468PeiLoadImageLoadImage (\r
469 IN CONST EFI_PEI_SERVICES **PeiServices,\r
470 IN EFI_PEI_FILE_HANDLE FileHandle,\r
471 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
472 OUT UINT64 *ImageSizeArg, OPTIONAL\r
473 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
474 OUT UINT32 *AuthenticationState\r
475 )\r
476{\r
477 EFI_STATUS Status;\r
478 VOID *Pe32Data;\r
479 EFI_PHYSICAL_ADDRESS ImageAddress;\r
480 UINT64 ImageSize;\r
481 EFI_PHYSICAL_ADDRESS ImageEntryPoint;\r
482 UINT16 Machine;\r
483 EFI_SECTION_TYPE SearchType1;\r
484 EFI_SECTION_TYPE SearchType2;\r
485\r
486 *EntryPoint = 0;\r
487 ImageSize = 0;\r
488 *AuthenticationState = 0;\r
489\r
490 if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst)) {\r
491 SearchType1 = EFI_SECTION_TE;\r
492 SearchType2 = EFI_SECTION_PE32;\r
493 } else {\r
494 SearchType1 = EFI_SECTION_PE32;\r
495 SearchType2 = EFI_SECTION_TE;\r
496 }\r
497\r
498 //\r
499 // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst\r
500 // is true, TE will be searched first).\r
501 //\r
502 Status = PeiServicesFfsFindSectionData3 (\r
503 SearchType1,\r
504 0,\r
505 FileHandle,\r
506 &Pe32Data,\r
507 AuthenticationState\r
508 );\r
509 //\r
510 // If we didn't find a first exe section, try to find the second exe section.\r
511 //\r
512 if (EFI_ERROR (Status)) {\r
513 Status = PeiServicesFfsFindSectionData3 (\r
514 SearchType2,\r
515 0,\r
516 FileHandle,\r
517 &Pe32Data,\r
518 AuthenticationState\r
519 );\r
520 if (EFI_ERROR (Status)) {\r
521 //\r
522 // PEI core only carry the loader function for TE and PE32 executables\r
523 // If this two section does not exist, just return.\r
524 //\r
525 return Status;\r
526 }\r
527 }\r
528\r
529 DEBUG ((DEBUG_INFO, "Loading PEIM %g\n", FileHandle));\r
530\r
531 //\r
532 // If memory is installed, perform the shadow operations\r
533 //\r
534 Status = LoadAndRelocatePeCoffImage (\r
535 FileHandle,\r
536 Pe32Data,\r
537 &ImageAddress,\r
538 &ImageSize,\r
539 &ImageEntryPoint\r
540 );\r
541\r
542 ASSERT_EFI_ERROR (Status);\r
543\r
544\r
545 if (EFI_ERROR (Status)) {\r
546 return Status;\r
547 }\r
548\r
549 //\r
550 // Got the entry point from the loaded Pe32Data\r
551 //\r
552 Pe32Data = (VOID *) ((UINTN) ImageAddress);\r
553 *EntryPoint = ImageEntryPoint;\r
554\r
555 Machine = PeCoffLoaderGetMachineType (Pe32Data);\r
556\r
557 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {\r
558 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine)) {\r
559 return EFI_UNSUPPORTED;\r
560 }\r
561 }\r
562\r
563 if (ImageAddressArg != NULL) {\r
564 *ImageAddressArg = ImageAddress;\r
565 }\r
566\r
567 if (ImageSizeArg != NULL) {\r
568 *ImageSizeArg = ImageSize;\r
569 }\r
570\r
571 DEBUG_CODE_BEGIN ();\r
572 CHAR8 *AsciiString;\r
573 CHAR8 EfiFileName[512];\r
574 INT32 Index;\r
575 INT32 StartIndex;\r
576\r
577 //\r
578 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
579 //\r
580 if (Machine != EFI_IMAGE_MACHINE_IA64) {\r
581 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));\r
582 } else {\r
583 //\r
584 // For IPF Image, the real entry point should be print.\r
585 //\r
586 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)(*(UINT64 *)(UINTN)*EntryPoint)));\r
587 }\r
588\r
589 //\r
590 // Print Module Name by PeImage PDB file name.\r
591 //\r
592 AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r
593\r
594 if (AsciiString != NULL) {\r
595 StartIndex = 0;\r
596 for (Index = 0; AsciiString[Index] != 0; Index++) {\r
597 if (AsciiString[Index] == '\\' || AsciiString[Index] == '/') {\r
598 StartIndex = Index + 1;\r
599 }\r
600 }\r
601\r
602 //\r
603 // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r
604 // The PDB file name is limited in the range of 0~511.\r
605 // If the length is bigger than 511, trim the redudant characters to avoid overflow in array boundary.\r
606 //\r
607 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
608 EfiFileName[Index] = AsciiString[Index + StartIndex];\r
609 if (EfiFileName[Index] == 0) {\r
610 EfiFileName[Index] = '.';\r
611 }\r
612 if (EfiFileName[Index] == '.') {\r
613 EfiFileName[Index + 1] = 'e';\r
614 EfiFileName[Index + 2] = 'f';\r
615 EfiFileName[Index + 3] = 'i';\r
616 EfiFileName[Index + 4] = 0;\r
617 break;\r
618 }\r
619 }\r
620\r
621 if (Index == sizeof (EfiFileName) - 4) {\r
622 EfiFileName[Index] = 0;\r
623 }\r
624\r
625 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a", EfiFileName));\r
626 }\r
627\r
628 DEBUG_CODE_END ();\r
629\r
630 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r
631\r
632 return EFI_SUCCESS;\r
633\r
634}\r
635\r
636\r
637/**\r
638 The wrapper function of PeiLoadImageLoadImage().\r
639\r
640 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.\r
641 @param FileHandle - Pointer to the FFS file header of the image.\r
642 @param ImageAddressArg - Pointer to PE/TE image.\r
643 @param ImageSizeArg - Size of PE/TE image.\r
644 @param EntryPoint - Pointer to entry point of specified image file for output.\r
645 @param AuthenticationState - Pointer to attestation authentication state of image.\r
646\r
647 @return Status of PeiLoadImageLoadImage().\r
648\r
649**/\r
650EFI_STATUS\r
651EFIAPI\r
652PeiLoadImageLoadImageWrapper (\r
653 IN CONST EFI_PEI_LOAD_FILE_PPI *This,\r
654 IN EFI_PEI_FILE_HANDLE FileHandle,\r
655 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
656 OUT UINT64 *ImageSizeArg, OPTIONAL\r
657 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
658 OUT UINT32 *AuthenticationState\r
659 )\r
660{\r
661 return PeiLoadImageLoadImage (\r
662 GetPeiServicesTablePointer (),\r
663 FileHandle,\r
664 ImageAddressArg,\r
665 ImageSizeArg,\r
666 EntryPoint,\r
667 AuthenticationState\r
668 );\r
669}\r
670\r
671/**\r
672 Check whether the input image has the relocation.\r
673\r
674 @param Pe32Data Pointer to the PE/COFF or TE image.\r
675\r
676 @retval TRUE Relocation is stripped.\r
677 @retval FALSE Relocation is not stripped.\r
678\r
679**/\r
680BOOLEAN\r
681RelocationIsStrip (\r
682 IN VOID *Pe32Data\r
683 )\r
684{\r
685 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
686 EFI_IMAGE_DOS_HEADER *DosHdr;\r
687\r
688 ASSERT (Pe32Data != NULL);\r
689\r
690 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
691 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
692 //\r
693 // DOS image header is present, so read the PE header after the DOS image header.\r
694 //\r
695 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r
696 } else {\r
697 //\r
698 // DOS image header is not present, so PE header is at the image base.\r
699 //\r
700 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
701 }\r
702\r
703 //\r
704 // Three cases with regards to relocations:\r
705 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable\r
706 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable\r
707 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but\r
708 // has no base relocs to apply\r
709 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.\r
710 //\r
711 // Look at the file header to determine if relocations have been stripped, and\r
712 // save this info in the image context for later use.\r
713 //\r
714 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
715 if ((Hdr.Te->DataDirectory[0].Size == 0) && (Hdr.Te->DataDirectory[0].VirtualAddress == 0)) {\r
716 return TRUE;\r
717 } else {\r
718 return FALSE;\r
719 }\r
720 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
721 if ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0) {\r
722 return TRUE;\r
723 } else {\r
724 return FALSE;\r
725 }\r
726 }\r
727\r
728 return FALSE;\r
729}\r
730\r
731/**\r
732 Routine to load image file for subsequent execution by LoadFile Ppi.\r
733 If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE\r
734 XIP image format is used.\r
735\r
736 @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
737 @param FileHandle - Pointer to the FFS file header of the image.\r
738 @param PeimState - The dispatch state of the input PEIM handle.\r
739 @param EntryPoint - Pointer to entry point of specified image file for output.\r
740 @param AuthenticationState - Pointer to attestation authentication state of image.\r
741\r
742 @retval EFI_SUCCESS - Image is successfully loaded.\r
743 @retval EFI_NOT_FOUND - Fail to locate necessary PPI\r
744 @retval Others - Fail to load file.\r
745\r
746**/\r
747EFI_STATUS\r
748PeiLoadImage (\r
749 IN CONST EFI_PEI_SERVICES **PeiServices,\r
750 IN EFI_PEI_FILE_HANDLE FileHandle,\r
751 IN UINT8 PeimState,\r
752 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
753 OUT UINT32 *AuthenticationState\r
754 )\r
755{\r
756 EFI_STATUS PpiStatus;\r
757 EFI_STATUS Status;\r
758 UINTN Index;\r
759 EFI_PEI_LOAD_FILE_PPI *LoadFile;\r
760 EFI_PHYSICAL_ADDRESS ImageAddress;\r
761 UINT64 ImageSize;\r
762 BOOLEAN IsStrip;\r
763\r
764 IsStrip = FALSE;\r
765 //\r
766 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.\r
767 // one at a time, until one reports EFI_SUCCESS.\r
768 //\r
769 Index = 0;\r
770 do {\r
771 PpiStatus = PeiServicesLocatePpi (\r
772 &gEfiPeiLoadFilePpiGuid,\r
773 Index,\r
774 NULL,\r
775 (VOID **)&LoadFile\r
776 );\r
777 if (!EFI_ERROR (PpiStatus)) {\r
778 Status = LoadFile->LoadFile (\r
779 LoadFile,\r
780 FileHandle,\r
781 &ImageAddress,\r
782 &ImageSize,\r
783 EntryPoint,\r
784 AuthenticationState\r
785 );\r
786 if (!EFI_ERROR (Status) || Status == EFI_WARN_BUFFER_TOO_SMALL) {\r
787 //\r
788 // The shadowed PEIM must be relocatable.\r
789 //\r
790 if (PeimState == PEIM_STATE_REGISTER_FOR_SHADOW) {\r
791 IsStrip = RelocationIsStrip ((VOID *) (UINTN) ImageAddress);\r
792 ASSERT (!IsStrip);\r
793 if (IsStrip) {\r
794 return EFI_UNSUPPORTED;\r
795 }\r
796 }\r
797\r
798 //\r
799 // The image to be started must have the machine type supported by PeiCore.\r
800 //\r
801 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress)));\r
802 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress))) {\r
803 return EFI_UNSUPPORTED;\r
804 }\r
805 return EFI_SUCCESS;\r
806 }\r
807 }\r
808 Index++;\r
809 } while (!EFI_ERROR (PpiStatus));\r
810\r
811 return PpiStatus;\r
812}\r
813\r
814\r
815/**\r
816\r
817 Install Pei Load File PPI.\r
818\r
819\r
820 @param PrivateData - Pointer to PEI_CORE_INSTANCE.\r
821 @param OldCoreData - Pointer to PEI_CORE_INSTANCE.\r
822\r
823**/\r
824VOID\r
825InitializeImageServices (\r
826 IN PEI_CORE_INSTANCE *PrivateData,\r
827 IN PEI_CORE_INSTANCE *OldCoreData\r
828 )\r
829{\r
830 if (OldCoreData == NULL) {\r
831 //\r
832 // The first time we are XIP (running from FLASH). We need to remember the\r
833 // FLASH address so we can reinstall the memory version that runs faster\r
834 //\r
835 PrivateData->XipLoadFile = &gPpiLoadFilePpiList;\r
836 PeiServicesInstallPpi (PrivateData->XipLoadFile);\r
837 } else {\r
838 //\r
839 // 2nd time we are running from memory so replace the XIP version with the\r
840 // new memory version.\r
841 //\r
842 PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList);\r
843 }\r
844}\r
845\r
846\r
847\r
848\r