]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Image/Image.c
MdeModulePkg/PeiCore: Enable T-RAM evacuation in PeiCore (CVE-2019-11098)
[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
d39d1260 4Copyright (c) 2006 - 2019, 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
d39d1260 102 // Test if the memory is available or not.\r
54ea99a7 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 292 //\r
d39d1260 293 // Initialize local IsS3Boot and IsRegisterForShadow variable\r
3d44658c
LG
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
9bedaec0
MK
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
54ea99a7 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
9626a87e 337 }\r
414bdfb6 338\r
341a658f
LG
339 //\r
340 // Set default base address to current image address.\r
341 //\r
342 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
54ea99a7 343\r
9626a87e 344 //\r
3d44658c
LG
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
b0d803fe 348 //\r
9bedaec0
MK
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
935efc21
ED
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
5d7f3126 367 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
54ea99a7 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
d39d1260 372 // The PEIM is not assigned valid address, try to allocate page to load it.\r
54ea99a7 373 //\r
a0ffd7a9
AB
374 Status = PeiServicesAllocatePages (EfiBootServicesCode,\r
375 EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize),\r
376 &ImageContext.ImageAddress);\r
54ea99a7 377 }\r
378 } else {\r
a0ffd7a9
AB
379 Status = PeiServicesAllocatePages (EfiBootServicesCode,\r
380 EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize),\r
381 &ImageContext.ImageAddress);\r
54ea99a7 382 }\r
a0ffd7a9 383 if (!EFI_ERROR (Status)) {\r
5d7f3126
LG
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
43ada17c 418 }\r
4e844595 419 }\r
b0d803fe 420\r
421 //\r
422 // Load the image to our new buffer\r
423 //\r
3d7b0992 424 Status = PeCoffLoaderLoadImage (&ImageContext);\r
b0d803fe 425 if (EFI_ERROR (Status)) {\r
63c677e2
LG
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
b0d803fe 429 return Status;\r
430 }\r
431 //\r
432 // Relocate the image in our new buffer\r
433 //\r
3d7b0992 434 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
b0d803fe 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
5d7f3126 442 if (ImageContext.ImageAddress != (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data) {\r
43ada17c 443 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
444 }\r
b0d803fe 445\r
446 *ImageAddress = ImageContext.ImageAddress;\r
447 *ImageSize = ImageContext.ImageSize;\r
448 *EntryPoint = ImageContext.EntryPoint;\r
449\r
5d7f3126 450 return ReturnStatus;\r
b0d803fe 451}\r
452\r
9bedaec0
MK
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
b1f6a7c6 569/**\r
54ea99a7 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
ed299e3c 572 this service performs that transformation.\r
b1f6a7c6 573\r
ed299e3c 574 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
b1f6a7c6 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
ed299e3c
LG
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
d1102dba 584 @retval EFI_WARN_BUFFER_TOO_SMALL\r
5d7f3126
LG
585 There is not enough heap to allocate the requested size.\r
586 This will not prevent the XIP image from being invoked.\r
b1f6a7c6 587\r
588**/\r
b0d803fe 589EFI_STATUS\r
590PeiLoadImageLoadImage (\r
284c8400 591 IN CONST EFI_PEI_SERVICES **PeiServices,\r
b0d803fe 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
192f6d4c 598{\r
599 EFI_STATUS Status;\r
600 VOID *Pe32Data;\r
192f6d4c 601 EFI_PHYSICAL_ADDRESS ImageAddress;\r
602 UINT64 ImageSize;\r
603 EFI_PHYSICAL_ADDRESS ImageEntryPoint;\r
192f6d4c 604 UINT16 Machine;\r
3076397e 605 EFI_SECTION_TYPE SearchType1;\r
606 EFI_SECTION_TYPE SearchType2;\r
192f6d4c 607\r
3d7b0992
LG
608 *EntryPoint = 0;\r
609 ImageSize = 0;\r
b0d803fe 610 *AuthenticationState = 0;\r
192f6d4c 611\r
3076397e 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
1b620adb 619\r
192f6d4c 620 //\r
54ea99a7 621 // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst\r
3076397e 622 // is true, TE will be searched first).\r
192f6d4c 623 //\r
c7935105 624 Status = PeiServicesFfsFindSectionData3 (\r
3076397e 625 SearchType1,\r
c7935105 626 0,\r
b0d803fe 627 FileHandle,\r
c7935105
SZ
628 &Pe32Data,\r
629 AuthenticationState\r
192f6d4c 630 );\r
631 //\r
3076397e 632 // If we didn't find a first exe section, try to find the second exe section.\r
192f6d4c 633 //\r
634 if (EFI_ERROR (Status)) {\r
c7935105 635 Status = PeiServicesFfsFindSectionData3 (\r
3076397e 636 SearchType2,\r
c7935105 637 0,\r
b0d803fe 638 FileHandle,\r
c7935105
SZ
639 &Pe32Data,\r
640 AuthenticationState\r
192f6d4c 641 );\r
b0d803fe 642 if (EFI_ERROR (Status)) {\r
192f6d4c 643 //\r
c7935105 644 // PEI core only carry the loader function for TE and PE32 executables\r
b0d803fe 645 // If this two section does not exist, just return.\r
192f6d4c 646 //\r
b0d803fe 647 return Status;\r
648 }\r
649 }\r
54ea99a7 650\r
63c677e2
LG
651 DEBUG ((DEBUG_INFO, "Loading PEIM %g\n", FileHandle));\r
652\r
43ada17c 653 //\r
654 // If memory is installed, perform the shadow operations\r
655 //\r
656 Status = LoadAndRelocatePeCoffImage (\r
3d44658c 657 FileHandle,\r
43ada17c 658 Pe32Data,\r
659 &ImageAddress,\r
660 &ImageSize,\r
661 &ImageEntryPoint\r
662 );\r
192f6d4c 663\r
43ada17c 664 ASSERT_EFI_ERROR (Status);\r
3d7b0992 665\r
43ada17c 666\r
667 if (EFI_ERROR (Status)) {\r
668 return Status;\r
192f6d4c 669 }\r
43ada17c 670\r
671 //\r
672 // Got the entry point from the loaded Pe32Data\r
673 //\r
674 Pe32Data = (VOID *) ((UINTN) ImageAddress);\r
675 *EntryPoint = ImageEntryPoint;\r
54ea99a7 676\r
3d7b0992 677 Machine = PeCoffLoaderGetMachineType (Pe32Data);\r
54ea99a7 678\r
192f6d4c 679 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {\r
8c519a56 680 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine)) {\r
681 return EFI_UNSUPPORTED;\r
682 }\r
192f6d4c 683 }\r
684\r
b0d803fe 685 if (ImageAddressArg != NULL) {\r
686 *ImageAddressArg = ImageAddress;\r
687 }\r
688\r
689 if (ImageSizeArg != NULL) {\r
690 *ImageSizeArg = ImageSize;\r
691 }\r
54ea99a7 692\r
192f6d4c 693 DEBUG_CODE_BEGIN ();\r
3d7b0992 694 CHAR8 *AsciiString;\r
77a750a5 695 CHAR8 EfiFileName[512];\r
3d7b0992 696 INT32 Index;\r
77a750a5 697 INT32 StartIndex;\r
e98cd821
LG
698\r
699 //\r
700 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
701 //\r
7cf02714 702 if (Machine != EFI_IMAGE_MACHINE_IA64) {\r
91136124 703 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));\r
e98cd821
LG
704 } else {\r
705 //\r
706 // For IPF Image, the real entry point should be print.\r
707 //\r
91136124 708 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 709 }\r
54ea99a7 710\r
e98cd821
LG
711 //\r
712 // Print Module Name by PeImage PDB file name.\r
713 //\r
3d7b0992 714 AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r
54ea99a7 715\r
3d7b0992 716 if (AsciiString != NULL) {\r
77a750a5
LG
717 StartIndex = 0;\r
718 for (Index = 0; AsciiString[Index] != 0; Index++) {\r
1e21413c 719 if (AsciiString[Index] == '\\' || AsciiString[Index] == '/') {\r
77a750a5 720 StartIndex = Index + 1;\r
3d7b0992 721 }\r
192f6d4c 722 }\r
192f6d4c 723\r
77a750a5
LG
724 //\r
725 // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r
726 // The PDB file name is limited in the range of 0~511.\r
d39d1260 727 // If the length is bigger than 511, trim the redundant characters to avoid overflow in array boundary.\r
77a750a5
LG
728 //\r
729 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
730 EfiFileName[Index] = AsciiString[Index + StartIndex];\r
731 if (EfiFileName[Index] == 0) {\r
732 EfiFileName[Index] = '.';\r
733 }\r
734 if (EfiFileName[Index] == '.') {\r
735 EfiFileName[Index + 1] = 'e';\r
736 EfiFileName[Index + 2] = 'f';\r
737 EfiFileName[Index + 3] = 'i';\r
738 EfiFileName[Index + 4] = 0;\r
739 break;\r
192f6d4c 740 }\r
741 }\r
77a750a5
LG
742\r
743 if (Index == sizeof (EfiFileName) - 4) {\r
744 EfiFileName[Index] = 0;\r
745 }\r
746\r
747 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a", EfiFileName));\r
192f6d4c 748 }\r
3d7b0992 749\r
192f6d4c 750 DEBUG_CODE_END ();\r
751\r
752 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r
753\r
754 return EFI_SUCCESS;\r
b0d803fe 755\r
756}\r
757\r
758\r
b1f6a7c6 759/**\r
760 The wrapper function of PeiLoadImageLoadImage().\r
761\r
762 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.\r
763 @param FileHandle - Pointer to the FFS file header of the image.\r
764 @param ImageAddressArg - Pointer to PE/TE image.\r
765 @param ImageSizeArg - Size of PE/TE image.\r
766 @param EntryPoint - Pointer to entry point of specified image file for output.\r
767 @param AuthenticationState - Pointer to attestation authentication state of image.\r
768\r
769 @return Status of PeiLoadImageLoadImage().\r
770\r
771**/\r
b0d803fe 772EFI_STATUS\r
773EFIAPI\r
774PeiLoadImageLoadImageWrapper (\r
775 IN CONST EFI_PEI_LOAD_FILE_PPI *This,\r
776 IN EFI_PEI_FILE_HANDLE FileHandle,\r
777 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
778 OUT UINT64 *ImageSizeArg, OPTIONAL\r
779 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
780 OUT UINT32 *AuthenticationState\r
781 )\r
b0d803fe 782{\r
783 return PeiLoadImageLoadImage (\r
784 GetPeiServicesTablePointer (),\r
785 FileHandle,\r
786 ImageAddressArg,\r
787 ImageSizeArg,\r
788 EntryPoint,\r
789 AuthenticationState\r
790 );\r
192f6d4c 791}\r
b0d803fe 792\r
341a658f
LG
793/**\r
794 Check whether the input image has the relocation.\r
795\r
796 @param Pe32Data Pointer to the PE/COFF or TE image.\r
797\r
798 @retval TRUE Relocation is stripped.\r
799 @retval FALSE Relocation is not stripped.\r
800\r
801**/\r
802BOOLEAN\r
803RelocationIsStrip (\r
804 IN VOID *Pe32Data\r
805 )\r
806{\r
807 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
808 EFI_IMAGE_DOS_HEADER *DosHdr;\r
809\r
810 ASSERT (Pe32Data != NULL);\r
811\r
812 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
813 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
814 //\r
815 // DOS image header is present, so read the PE header after the DOS image header.\r
816 //\r
817 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r
818 } else {\r
819 //\r
820 // DOS image header is not present, so PE header is at the image base.\r
821 //\r
822 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
823 }\r
824\r
825 //\r
826 // Three cases with regards to relocations:\r
827 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable\r
828 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable\r
829 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but\r
830 // has no base relocs to apply\r
831 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.\r
832 //\r
833 // Look at the file header to determine if relocations have been stripped, and\r
834 // save this info in the image context for later use.\r
835 //\r
836 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
837 if ((Hdr.Te->DataDirectory[0].Size == 0) && (Hdr.Te->DataDirectory[0].VirtualAddress == 0)) {\r
838 return TRUE;\r
839 } else {\r
840 return FALSE;\r
841 }\r
842 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
843 if ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0) {\r
844 return TRUE;\r
845 } else {\r
846 return FALSE;\r
847 }\r
848 }\r
849\r
850 return FALSE;\r
851}\r
852\r
b1f6a7c6 853/**\r
ed299e3c 854 Routine to load image file for subsequent execution by LoadFile Ppi.\r
54ea99a7 855 If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE\r
ed299e3c 856 XIP image format is used.\r
b1f6a7c6 857\r
ed299e3c
LG
858 @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
859 @param FileHandle - Pointer to the FFS file header of the image.\r
341a658f 860 @param PeimState - The dispatch state of the input PEIM handle.\r
ed299e3c
LG
861 @param EntryPoint - Pointer to entry point of specified image file for output.\r
862 @param AuthenticationState - Pointer to attestation authentication state of image.\r
b1f6a7c6 863\r
ed299e3c
LG
864 @retval EFI_SUCCESS - Image is successfully loaded.\r
865 @retval EFI_NOT_FOUND - Fail to locate necessary PPI\r
866 @retval Others - Fail to load file.\r
b1f6a7c6 867\r
868**/\r
b0d803fe 869EFI_STATUS\r
870PeiLoadImage (\r
6c7a807a 871 IN CONST EFI_PEI_SERVICES **PeiServices,\r
b0d803fe 872 IN EFI_PEI_FILE_HANDLE FileHandle,\r
341a658f 873 IN UINT8 PeimState,\r
b0d803fe 874 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
875 OUT UINT32 *AuthenticationState\r
876 )\r
b0d803fe 877{\r
878 EFI_STATUS PpiStatus;\r
879 EFI_STATUS Status;\r
880 UINTN Index;\r
881 EFI_PEI_LOAD_FILE_PPI *LoadFile;\r
882 EFI_PHYSICAL_ADDRESS ImageAddress;\r
883 UINT64 ImageSize;\r
341a658f 884 BOOLEAN IsStrip;\r
b0d803fe 885\r
341a658f 886 IsStrip = FALSE;\r
b0d803fe 887 //\r
888 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.\r
889 // one at a time, until one reports EFI_SUCCESS.\r
890 //\r
891 Index = 0;\r
892 do {\r
893 PpiStatus = PeiServicesLocatePpi (\r
894 &gEfiPeiLoadFilePpiGuid,\r
895 Index,\r
896 NULL,\r
897 (VOID **)&LoadFile\r
898 );\r
899 if (!EFI_ERROR (PpiStatus)) {\r
900 Status = LoadFile->LoadFile (\r
54ea99a7 901 LoadFile,\r
902 FileHandle,\r
903 &ImageAddress,\r
b0d803fe 904 &ImageSize,\r
905 EntryPoint,\r
906 AuthenticationState\r
907 );\r
5d7f3126 908 if (!EFI_ERROR (Status) || Status == EFI_WARN_BUFFER_TOO_SMALL) {\r
341a658f
LG
909 //\r
910 // The shadowed PEIM must be relocatable.\r
911 //\r
c2c4199b 912 if (PeimState == PEIM_STATE_REGISTER_FOR_SHADOW) {\r
341a658f
LG
913 IsStrip = RelocationIsStrip ((VOID *) (UINTN) ImageAddress);\r
914 ASSERT (!IsStrip);\r
915 if (IsStrip) {\r
916 return EFI_UNSUPPORTED;\r
917 }\r
918 }\r
919\r
db0b7ad5
LG
920 //\r
921 // The image to be started must have the machine type supported by PeiCore.\r
922 //\r
923 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress)));\r
919df8e6 924 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress))) {\r
919df8e6
LG
925 return EFI_UNSUPPORTED;\r
926 }\r
5d7f3126 927 return EFI_SUCCESS;\r
b0d803fe 928 }\r
929 }\r
930 Index++;\r
931 } while (!EFI_ERROR (PpiStatus));\r
932\r
8c519a56 933 return PpiStatus;\r
b0d803fe 934}\r
935\r
936\r
b1f6a7c6 937/**\r
b0d803fe 938\r
ed299e3c
LG
939 Install Pei Load File PPI.\r
940\r
941\r
942 @param PrivateData - Pointer to PEI_CORE_INSTANCE.\r
943 @param OldCoreData - Pointer to PEI_CORE_INSTANCE.\r
b0d803fe 944\r
b1f6a7c6 945**/\r
946VOID\r
947InitializeImageServices (\r
948 IN PEI_CORE_INSTANCE *PrivateData,\r
949 IN PEI_CORE_INSTANCE *OldCoreData\r
950 )\r
b0d803fe 951{\r
b0d803fe 952 if (OldCoreData == NULL) {\r
953 //\r
954 // The first time we are XIP (running from FLASH). We need to remember the\r
955 // FLASH address so we can reinstall the memory version that runs faster\r
956 //\r
957 PrivateData->XipLoadFile = &gPpiLoadFilePpiList;\r
958 PeiServicesInstallPpi (PrivateData->XipLoadFile);\r
959 } else {\r
960 //\r
54ea99a7 961 // 2nd time we are running from memory so replace the XIP version with the\r
962 // new memory version.\r
b0d803fe 963 //\r
54ea99a7 964 PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList);\r
b0d803fe 965 }\r
966}\r
967\r
968\r
969\r
341a658f 970\r