]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Image/Image.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Image / Image.c
CommitLineData
615c6dd0 1/** @file\r
b1f6a7c6 2 Pei Core Load Image Support\r
54ea99a7 3\r
63c677e2 4Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
9d510e61 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
192f6d4c 6\r
615c6dd0 7**/\r
192f6d4c 8\r
0d516397 9#include "PeiMain.h"\r
192f6d4c 10\r
b0d803fe 11\r
fe1e36e5 12EFI_PEI_LOAD_FILE_PPI mPeiLoadImagePpi = {\r
b0d803fe 13 PeiLoadImageLoadImageWrapper\r
14};\r
15\r
16\r
fe1e36e5 17EFI_PEI_PPI_DESCRIPTOR gPpiLoadFilePpiList = {\r
b0d803fe 18 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
19 &gEfiPeiLoadFilePpiGuid,\r
20 &mPeiLoadImagePpi\r
21};\r
22\r
b1f6a7c6 23/**\r
24\r
25973fc3 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
b1f6a7c6 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
b0d803fe 36EFI_STATUS\r
37EFIAPI\r
38PeiImageRead (\r
39 IN VOID *FileHandle,\r
40 IN UINTN FileOffset,\r
ed299e3c 41 IN UINTN *ReadSize,\r
b0d803fe 42 OUT VOID *Buffer\r
43 )\r
b0d803fe 44{\r
a9bfd802
LG
45 CHAR8 *Destination8;\r
46 CHAR8 *Source8;\r
d1102dba 47\r
25973fc3 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
54ea99a7 57/**\r
2048c585
GM
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
d1102dba
LG
60 The function is only invoked when load modules at fixed address feature is enabled.\r
61\r
54ea99a7 62 @param Private Pointer to the private data passed in from caller\r
2048c585 63 @param ImageBase The base address the image will be loaded at.\r
54ea99a7 64 @param ImageSize The size of the image\r
d1102dba 65\r
54ea99a7 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
d1102dba 83\r
54ea99a7 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
d1102dba 92\r
54ea99a7 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
d1102dba
LG
97 (PeiCodeBase > ImageBase)) {\r
98 return EFI_NOT_FOUND;\r
54ea99a7 99 }\r
d1102dba 100\r
54ea99a7 101 //\r
102 // Test if the memory is avalaible or not.\r
103 //\r
d1102dba 104 MemoryUsageBitMap = Private->PeiCodeMemoryRangeUsageBitMap;\r
54ea99a7 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
d1102dba 112 return EFI_NOT_FOUND;\r
54ea99a7 113 }\r
114 }\r
d1102dba 115\r
54ea99a7 116 //\r
117 // Being here means the memory range is available. So mark the bits for the memory range\r
d1102dba 118 //\r
54ea99a7 119 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
120 MemoryUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));\r
121 }\r
d1102dba 122 return EFI_SUCCESS;\r
54ea99a7 123}\r
124/**\r
125\r
2048c585 126 Get the fixed loading address from image header assigned by build tool. This function only be called\r
54ea99a7 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
2048c585 134 @retval EFI_NOT_FOUND The image has no assigned fixed loading address.\r
b0d803fe 135\r
54ea99a7 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
2048c585 147 EFI_PHYSICAL_ADDRESS FixLoadingAddress;\r
54ea99a7 148 UINT16 Index;\r
149 UINTN Size;\r
150 UINT16 NumberOfSections;\r
151 UINT64 ValueInSectionHeader;\r
d1102dba 152\r
54ea99a7 153\r
2048c585 154 FixLoadingAddress = 0;\r
54ea99a7 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
2048c585 163 // for TE image, the fix loading address is saved in first section header that doesn't point\r
54ea99a7 164 // to code section.\r
165 //\r
166 SectionHeaderOffset = sizeof (EFI_TE_IMAGE_HEADER);\r
167 NumberOfSections = ImgHdr->Te.NumberOfSections;\r
168 } else {\r
16f69227
HW
169 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +\r
170 sizeof (UINT32) +\r
171 sizeof (EFI_IMAGE_FILE_HEADER) +\r
172 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;\r
54ea99a7 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
2048c585 203 // else, these 2 fields should be set to Zero\r
54ea99a7 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
852081fc 210 if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0) {\r
54ea99a7 211 //\r
212 // When LMFA feature is configured as Load Module at Fixed Absolute Address mode, PointerToRelocations & PointerToLineNumbers field\r
2048c585 213 // hold the absolute address of image base running in memory\r
54ea99a7 214 //\r
2048c585 215 FixLoadingAddress = ValueInSectionHeader;\r
54ea99a7 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
2048c585 221 FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(Private->LoadModuleAtFixAddressTopAddress + (INT64)ValueInSectionHeader);\r
54ea99a7 222 }\r
223 //\r
2048c585 224 // Check if the memory range is available.\r
54ea99a7 225 //\r
2048c585 226 Status = CheckAndMarkFixLoadingMemoryUsageBitMap (Private, FixLoadingAddress, (UINT32) ImageContext->ImageSize);\r
54ea99a7 227 if (!EFI_ERROR(Status)) {\r
228 //\r
2048c585 229 // The assigned address is valid. Return the specified loading address\r
54ea99a7 230 //\r
2048c585 231 ImageContext->ImageAddress = FixLoadingAddress;\r
54ea99a7 232 }\r
233 }\r
234 break;\r
235 }\r
236 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
237 }\r
2048c585 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
54ea99a7 239 return Status;\r
240}\r
b1f6a7c6 241/**\r
242\r
243 Loads and relocates a PE/COFF image into memory.\r
341a658f 244 If the image is not relocatable, it will not be loaded into memory and be loaded as XIP image.\r
b1f6a7c6 245\r
3d44658c 246 @param FileHandle - Pointer to the FFS file header of the image.\r
b1f6a7c6 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
d1102dba 254 @retval EFI_WARN_BUFFER_TOO_SMALL\r
5d7f3126
LG
255 There is not enough heap to allocate the requested size.\r
256 This will not prevent the XIP image from being invoked.\r
b1f6a7c6 257\r
258**/\r
b0d803fe 259EFI_STATUS\r
260LoadAndRelocatePeCoffImage (\r
3d44658c 261 IN EFI_PEI_FILE_HANDLE FileHandle,\r
b0d803fe 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
b0d803fe 267{\r
268 EFI_STATUS Status;\r
269 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
43ada17c 270 PEI_CORE_INSTANCE *Private;\r
935efc21 271 UINT64 AlignImageSize;\r
5d7f3126
LG
272 BOOLEAN IsXipImage;\r
273 EFI_STATUS ReturnStatus;\r
3d44658c 274 BOOLEAN IsS3Boot;\r
609730ef 275 BOOLEAN IsPeiModule;\r
3d44658c 276 BOOLEAN IsRegisterForShadow;\r
609730ef 277 EFI_FV_FILE_INFO FileInfo;\r
43ada17c 278\r
279 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
b0d803fe 280\r
5d7f3126
LG
281 ReturnStatus = EFI_SUCCESS;\r
282 IsXipImage = FALSE;\r
b0d803fe 283 ZeroMem (&ImageContext, sizeof (ImageContext));\r
284 ImageContext.Handle = Pe32Data;\r
40a7b235 285 ImageContext.ImageRead = PeiImageRead;\r
b0d803fe 286\r
3d7b0992 287 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
b0d803fe 288 if (EFI_ERROR (Status)) {\r
289 return Status;\r
290 }\r
d1102dba 291\r
3d44658c
LG
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
d1102dba 300 if ((Private->CurrentFileHandle == FileHandle)\r
c2c4199b 301 && (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] == PEIM_STATE_REGISTER_FOR_SHADOW)) {\r
3d44658c
LG
302 IsRegisterForShadow = TRUE;\r
303 }\r
304\r
5d7f3126
LG
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
609730ef
LG
312 //\r
313 // Get file type first\r
314 //\r
315 Status = PeiServicesFfsGetFileInfo (FileHandle, &FileInfo);\r
316 ASSERT_EFI_ERROR (Status);\r
d1102dba 317\r
609730ef
LG
318 //\r
319 // Check whether the file type is PEI module.\r
320 //\r
321 IsPeiModule = FALSE;\r
d1102dba
LG
322 if (FileInfo.FileType == EFI_FV_FILETYPE_PEI_CORE ||\r
323 FileInfo.FileType == EFI_FV_FILETYPE_PEIM ||\r
609730ef
LG
324 FileInfo.FileType == EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER) {\r
325 IsPeiModule = TRUE;\r
326 }\r
327\r
b0d803fe 328 //\r
9626a87e
LG
329 // When Image has no reloc section, it can't be relocated into memory.\r
330 //\r
609730ef 331 if (ImageContext.RelocationsStripped && (Private->PeiMemoryInstalled) && ((!IsPeiModule) ||\r
3d44658c 332 (!IsS3Boot && (PcdGetBool (PcdShadowPeimOnBoot) || IsRegisterForShadow)) || (IsS3Boot && PcdGetBool (PcdShadowPeimOnS3Boot)))) {\r
54ea99a7 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
9626a87e 334 }\r
414bdfb6 335\r
341a658f
LG
336 //\r
337 // Set default base address to current image address.\r
338 //\r
339 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
54ea99a7 340\r
9626a87e 341 //\r
3d44658c
LG
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
b0d803fe 345 //\r
609730ef 346 if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && ((!IsPeiModule) ||\r
3d44658c 347 (!IsS3Boot && (PcdGetBool (PcdShadowPeimOnBoot) || IsRegisterForShadow)) || (IsS3Boot && PcdGetBool (PcdShadowPeimOnS3Boot)))) {\r
935efc21
ED
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
5d7f3126 361 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
54ea99a7 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
a0ffd7a9
AB
368 Status = PeiServicesAllocatePages (EfiBootServicesCode,\r
369 EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize),\r
370 &ImageContext.ImageAddress);\r
54ea99a7 371 }\r
372 } else {\r
a0ffd7a9
AB
373 Status = PeiServicesAllocatePages (EfiBootServicesCode,\r
374 EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize),\r
375 &ImageContext.ImageAddress);\r
54ea99a7 376 }\r
a0ffd7a9 377 if (!EFI_ERROR (Status)) {\r
5d7f3126
LG
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
43ada17c 412 }\r
4e844595 413 }\r
b0d803fe 414\r
415 //\r
416 // Load the image to our new buffer\r
417 //\r
3d7b0992 418 Status = PeCoffLoaderLoadImage (&ImageContext);\r
b0d803fe 419 if (EFI_ERROR (Status)) {\r
63c677e2
LG
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
b0d803fe 423 return Status;\r
424 }\r
425 //\r
426 // Relocate the image in our new buffer\r
427 //\r
3d7b0992 428 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
b0d803fe 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
5d7f3126 436 if (ImageContext.ImageAddress != (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data) {\r
43ada17c 437 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
438 }\r
b0d803fe 439\r
440 *ImageAddress = ImageContext.ImageAddress;\r
441 *ImageSize = ImageContext.ImageSize;\r
442 *EntryPoint = ImageContext.EntryPoint;\r
443\r
5d7f3126 444 return ReturnStatus;\r
b0d803fe 445}\r
446\r
b1f6a7c6 447/**\r
54ea99a7 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
ed299e3c 450 this service performs that transformation.\r
b1f6a7c6 451\r
ed299e3c 452 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
b1f6a7c6 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
ed299e3c
LG
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
d1102dba 462 @retval EFI_WARN_BUFFER_TOO_SMALL\r
5d7f3126
LG
463 There is not enough heap to allocate the requested size.\r
464 This will not prevent the XIP image from being invoked.\r
b1f6a7c6 465\r
466**/\r
b0d803fe 467EFI_STATUS\r
468PeiLoadImageLoadImage (\r
284c8400 469 IN CONST EFI_PEI_SERVICES **PeiServices,\r
b0d803fe 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
192f6d4c 476{\r
477 EFI_STATUS Status;\r
478 VOID *Pe32Data;\r
192f6d4c 479 EFI_PHYSICAL_ADDRESS ImageAddress;\r
480 UINT64 ImageSize;\r
481 EFI_PHYSICAL_ADDRESS ImageEntryPoint;\r
192f6d4c 482 UINT16 Machine;\r
3076397e 483 EFI_SECTION_TYPE SearchType1;\r
484 EFI_SECTION_TYPE SearchType2;\r
192f6d4c 485\r
3d7b0992
LG
486 *EntryPoint = 0;\r
487 ImageSize = 0;\r
b0d803fe 488 *AuthenticationState = 0;\r
192f6d4c 489\r
3076397e 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
1b620adb 497\r
192f6d4c 498 //\r
54ea99a7 499 // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst\r
3076397e 500 // is true, TE will be searched first).\r
192f6d4c 501 //\r
c7935105 502 Status = PeiServicesFfsFindSectionData3 (\r
3076397e 503 SearchType1,\r
c7935105 504 0,\r
b0d803fe 505 FileHandle,\r
c7935105
SZ
506 &Pe32Data,\r
507 AuthenticationState\r
192f6d4c 508 );\r
509 //\r
3076397e 510 // If we didn't find a first exe section, try to find the second exe section.\r
192f6d4c 511 //\r
512 if (EFI_ERROR (Status)) {\r
c7935105 513 Status = PeiServicesFfsFindSectionData3 (\r
3076397e 514 SearchType2,\r
c7935105 515 0,\r
b0d803fe 516 FileHandle,\r
c7935105
SZ
517 &Pe32Data,\r
518 AuthenticationState\r
192f6d4c 519 );\r
b0d803fe 520 if (EFI_ERROR (Status)) {\r
192f6d4c 521 //\r
c7935105 522 // PEI core only carry the loader function for TE and PE32 executables\r
b0d803fe 523 // If this two section does not exist, just return.\r
192f6d4c 524 //\r
b0d803fe 525 return Status;\r
526 }\r
527 }\r
54ea99a7 528\r
63c677e2
LG
529 DEBUG ((DEBUG_INFO, "Loading PEIM %g\n", FileHandle));\r
530\r
43ada17c 531 //\r
532 // If memory is installed, perform the shadow operations\r
533 //\r
534 Status = LoadAndRelocatePeCoffImage (\r
3d44658c 535 FileHandle,\r
43ada17c 536 Pe32Data,\r
537 &ImageAddress,\r
538 &ImageSize,\r
539 &ImageEntryPoint\r
540 );\r
192f6d4c 541\r
43ada17c 542 ASSERT_EFI_ERROR (Status);\r
3d7b0992 543\r
43ada17c 544\r
545 if (EFI_ERROR (Status)) {\r
546 return Status;\r
192f6d4c 547 }\r
43ada17c 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
54ea99a7 554\r
3d7b0992 555 Machine = PeCoffLoaderGetMachineType (Pe32Data);\r
54ea99a7 556\r
192f6d4c 557 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {\r
8c519a56 558 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine)) {\r
559 return EFI_UNSUPPORTED;\r
560 }\r
192f6d4c 561 }\r
562\r
b0d803fe 563 if (ImageAddressArg != NULL) {\r
564 *ImageAddressArg = ImageAddress;\r
565 }\r
566\r
567 if (ImageSizeArg != NULL) {\r
568 *ImageSizeArg = ImageSize;\r
569 }\r
54ea99a7 570\r
192f6d4c 571 DEBUG_CODE_BEGIN ();\r
3d7b0992 572 CHAR8 *AsciiString;\r
77a750a5 573 CHAR8 EfiFileName[512];\r
3d7b0992 574 INT32 Index;\r
77a750a5 575 INT32 StartIndex;\r
e98cd821
LG
576\r
577 //\r
578 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
579 //\r
7cf02714 580 if (Machine != EFI_IMAGE_MACHINE_IA64) {\r
91136124 581 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));\r
e98cd821
LG
582 } else {\r
583 //\r
584 // For IPF Image, the real entry point should be print.\r
585 //\r
91136124 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
e98cd821 587 }\r
54ea99a7 588\r
e98cd821
LG
589 //\r
590 // Print Module Name by PeImage PDB file name.\r
591 //\r
3d7b0992 592 AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r
54ea99a7 593\r
3d7b0992 594 if (AsciiString != NULL) {\r
77a750a5
LG
595 StartIndex = 0;\r
596 for (Index = 0; AsciiString[Index] != 0; Index++) {\r
1e21413c 597 if (AsciiString[Index] == '\\' || AsciiString[Index] == '/') {\r
77a750a5 598 StartIndex = Index + 1;\r
3d7b0992 599 }\r
192f6d4c 600 }\r
192f6d4c 601\r
77a750a5
LG
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
192f6d4c 618 }\r
619 }\r
77a750a5
LG
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
192f6d4c 626 }\r
3d7b0992 627\r
192f6d4c 628 DEBUG_CODE_END ();\r
629\r
630 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r
631\r
632 return EFI_SUCCESS;\r
b0d803fe 633\r
634}\r
635\r
636\r
b1f6a7c6 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
b0d803fe 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
b0d803fe 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
192f6d4c 669}\r
b0d803fe 670\r
341a658f
LG
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
b1f6a7c6 731/**\r
ed299e3c 732 Routine to load image file for subsequent execution by LoadFile Ppi.\r
54ea99a7 733 If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE\r
ed299e3c 734 XIP image format is used.\r
b1f6a7c6 735\r
ed299e3c
LG
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
341a658f 738 @param PeimState - The dispatch state of the input PEIM handle.\r
ed299e3c
LG
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
b1f6a7c6 741\r
ed299e3c
LG
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
b1f6a7c6 745\r
746**/\r
b0d803fe 747EFI_STATUS\r
748PeiLoadImage (\r
6c7a807a 749 IN CONST EFI_PEI_SERVICES **PeiServices,\r
b0d803fe 750 IN EFI_PEI_FILE_HANDLE FileHandle,\r
341a658f 751 IN UINT8 PeimState,\r
b0d803fe 752 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
753 OUT UINT32 *AuthenticationState\r
754 )\r
b0d803fe 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
341a658f 762 BOOLEAN IsStrip;\r
b0d803fe 763\r
341a658f 764 IsStrip = FALSE;\r
b0d803fe 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
54ea99a7 779 LoadFile,\r
780 FileHandle,\r
781 &ImageAddress,\r
b0d803fe 782 &ImageSize,\r
783 EntryPoint,\r
784 AuthenticationState\r
785 );\r
5d7f3126 786 if (!EFI_ERROR (Status) || Status == EFI_WARN_BUFFER_TOO_SMALL) {\r
341a658f
LG
787 //\r
788 // The shadowed PEIM must be relocatable.\r
789 //\r
c2c4199b 790 if (PeimState == PEIM_STATE_REGISTER_FOR_SHADOW) {\r
341a658f
LG
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
db0b7ad5
LG
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
919df8e6 802 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress))) {\r
919df8e6
LG
803 return EFI_UNSUPPORTED;\r
804 }\r
5d7f3126 805 return EFI_SUCCESS;\r
b0d803fe 806 }\r
807 }\r
808 Index++;\r
809 } while (!EFI_ERROR (PpiStatus));\r
810\r
8c519a56 811 return PpiStatus;\r
b0d803fe 812}\r
813\r
814\r
b1f6a7c6 815/**\r
b0d803fe 816\r
ed299e3c
LG
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
b0d803fe 822\r
b1f6a7c6 823**/\r
824VOID\r
825InitializeImageServices (\r
826 IN PEI_CORE_INSTANCE *PrivateData,\r
827 IN PEI_CORE_INSTANCE *OldCoreData\r
828 )\r
b0d803fe 829{\r
b0d803fe 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
54ea99a7 839 // 2nd time we are running from memory so replace the XIP version with the\r
840 // new memory version.\r
b0d803fe 841 //\r
54ea99a7 842 PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList);\r
b0d803fe 843 }\r
844}\r
845\r
846\r
847\r
341a658f 848\r