]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Image/Image.c
MdeModulePkg: Change use of EFI_D_* to DEBUG_*
[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
ab2b389e 4Copyright (c) 2006 - 2021, 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
87000d77 238 DEBUG ((DEBUG_INFO|DEBUG_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
87000d77 336 DEBUG ((DEBUG_INFO|DEBUG_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
87000d77 370 DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));\r
54ea99a7 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 if (EFI_ERROR (Status)) {\r
665 return Status;\r
192f6d4c 666 }\r
43ada17c 667\r
668 //\r
669 // Got the entry point from the loaded Pe32Data\r
670 //\r
671 Pe32Data = (VOID *) ((UINTN) ImageAddress);\r
672 *EntryPoint = ImageEntryPoint;\r
54ea99a7 673\r
3d7b0992 674 Machine = PeCoffLoaderGetMachineType (Pe32Data);\r
54ea99a7 675\r
192f6d4c 676 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {\r
8c519a56 677 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine)) {\r
678 return EFI_UNSUPPORTED;\r
679 }\r
192f6d4c 680 }\r
681\r
b0d803fe 682 if (ImageAddressArg != NULL) {\r
683 *ImageAddressArg = ImageAddress;\r
684 }\r
685\r
686 if (ImageSizeArg != NULL) {\r
687 *ImageSizeArg = ImageSize;\r
688 }\r
54ea99a7 689\r
192f6d4c 690 DEBUG_CODE_BEGIN ();\r
3d7b0992 691 CHAR8 *AsciiString;\r
77a750a5 692 CHAR8 EfiFileName[512];\r
3d7b0992 693 INT32 Index;\r
77a750a5 694 INT32 StartIndex;\r
e98cd821
LG
695\r
696 //\r
697 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
698 //\r
7cf02714 699 if (Machine != EFI_IMAGE_MACHINE_IA64) {\r
87000d77 700 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));\r
e98cd821
LG
701 } else {\r
702 //\r
703 // For IPF Image, the real entry point should be print.\r
704 //\r
87000d77 705 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)(*(UINT64 *)(UINTN)*EntryPoint)));\r
e98cd821 706 }\r
54ea99a7 707\r
e98cd821
LG
708 //\r
709 // Print Module Name by PeImage PDB file name.\r
710 //\r
3d7b0992 711 AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r
54ea99a7 712\r
3d7b0992 713 if (AsciiString != NULL) {\r
77a750a5
LG
714 StartIndex = 0;\r
715 for (Index = 0; AsciiString[Index] != 0; Index++) {\r
1e21413c 716 if (AsciiString[Index] == '\\' || AsciiString[Index] == '/') {\r
77a750a5 717 StartIndex = Index + 1;\r
3d7b0992 718 }\r
192f6d4c 719 }\r
192f6d4c 720\r
77a750a5
LG
721 //\r
722 // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r
723 // The PDB file name is limited in the range of 0~511.\r
d39d1260 724 // If the length is bigger than 511, trim the redundant characters to avoid overflow in array boundary.\r
77a750a5
LG
725 //\r
726 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
727 EfiFileName[Index] = AsciiString[Index + StartIndex];\r
728 if (EfiFileName[Index] == 0) {\r
729 EfiFileName[Index] = '.';\r
730 }\r
731 if (EfiFileName[Index] == '.') {\r
732 EfiFileName[Index + 1] = 'e';\r
733 EfiFileName[Index + 2] = 'f';\r
734 EfiFileName[Index + 3] = 'i';\r
735 EfiFileName[Index + 4] = 0;\r
736 break;\r
192f6d4c 737 }\r
738 }\r
77a750a5
LG
739\r
740 if (Index == sizeof (EfiFileName) - 4) {\r
741 EfiFileName[Index] = 0;\r
742 }\r
743\r
87000d77 744 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName));\r
192f6d4c 745 }\r
3d7b0992 746\r
192f6d4c 747 DEBUG_CODE_END ();\r
748\r
87000d77 749 DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));\r
192f6d4c 750\r
751 return EFI_SUCCESS;\r
b0d803fe 752\r
753}\r
754\r
755\r
b1f6a7c6 756/**\r
757 The wrapper function of PeiLoadImageLoadImage().\r
758\r
759 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.\r
760 @param FileHandle - Pointer to the FFS file header of the image.\r
761 @param ImageAddressArg - Pointer to PE/TE image.\r
762 @param ImageSizeArg - Size of PE/TE image.\r
763 @param EntryPoint - Pointer to entry point of specified image file for output.\r
764 @param AuthenticationState - Pointer to attestation authentication state of image.\r
765\r
766 @return Status of PeiLoadImageLoadImage().\r
767\r
768**/\r
b0d803fe 769EFI_STATUS\r
770EFIAPI\r
771PeiLoadImageLoadImageWrapper (\r
772 IN CONST EFI_PEI_LOAD_FILE_PPI *This,\r
773 IN EFI_PEI_FILE_HANDLE FileHandle,\r
774 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
775 OUT UINT64 *ImageSizeArg, OPTIONAL\r
776 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
777 OUT UINT32 *AuthenticationState\r
778 )\r
b0d803fe 779{\r
780 return PeiLoadImageLoadImage (\r
781 GetPeiServicesTablePointer (),\r
782 FileHandle,\r
783 ImageAddressArg,\r
784 ImageSizeArg,\r
785 EntryPoint,\r
786 AuthenticationState\r
787 );\r
192f6d4c 788}\r
b0d803fe 789\r
341a658f
LG
790/**\r
791 Check whether the input image has the relocation.\r
792\r
793 @param Pe32Data Pointer to the PE/COFF or TE image.\r
794\r
795 @retval TRUE Relocation is stripped.\r
796 @retval FALSE Relocation is not stripped.\r
797\r
798**/\r
799BOOLEAN\r
800RelocationIsStrip (\r
801 IN VOID *Pe32Data\r
802 )\r
803{\r
804 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
805 EFI_IMAGE_DOS_HEADER *DosHdr;\r
806\r
807 ASSERT (Pe32Data != NULL);\r
808\r
809 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
810 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
811 //\r
812 // DOS image header is present, so read the PE header after the DOS image header.\r
813 //\r
814 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r
815 } else {\r
816 //\r
817 // DOS image header is not present, so PE header is at the image base.\r
818 //\r
819 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
820 }\r
821\r
822 //\r
823 // Three cases with regards to relocations:\r
824 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable\r
825 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable\r
826 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but\r
827 // has no base relocs to apply\r
828 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.\r
829 //\r
830 // Look at the file header to determine if relocations have been stripped, and\r
831 // save this info in the image context for later use.\r
832 //\r
833 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
834 if ((Hdr.Te->DataDirectory[0].Size == 0) && (Hdr.Te->DataDirectory[0].VirtualAddress == 0)) {\r
835 return TRUE;\r
836 } else {\r
837 return FALSE;\r
838 }\r
839 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
840 if ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0) {\r
841 return TRUE;\r
842 } else {\r
843 return FALSE;\r
844 }\r
845 }\r
846\r
847 return FALSE;\r
848}\r
849\r
b1f6a7c6 850/**\r
ed299e3c 851 Routine to load image file for subsequent execution by LoadFile Ppi.\r
54ea99a7 852 If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE\r
ed299e3c 853 XIP image format is used.\r
b1f6a7c6 854\r
ed299e3c
LG
855 @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
856 @param FileHandle - Pointer to the FFS file header of the image.\r
341a658f 857 @param PeimState - The dispatch state of the input PEIM handle.\r
ed299e3c
LG
858 @param EntryPoint - Pointer to entry point of specified image file for output.\r
859 @param AuthenticationState - Pointer to attestation authentication state of image.\r
b1f6a7c6 860\r
ed299e3c
LG
861 @retval EFI_SUCCESS - Image is successfully loaded.\r
862 @retval EFI_NOT_FOUND - Fail to locate necessary PPI\r
863 @retval Others - Fail to load file.\r
b1f6a7c6 864\r
865**/\r
b0d803fe 866EFI_STATUS\r
867PeiLoadImage (\r
6c7a807a 868 IN CONST EFI_PEI_SERVICES **PeiServices,\r
b0d803fe 869 IN EFI_PEI_FILE_HANDLE FileHandle,\r
341a658f 870 IN UINT8 PeimState,\r
b0d803fe 871 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
872 OUT UINT32 *AuthenticationState\r
873 )\r
b0d803fe 874{\r
875 EFI_STATUS PpiStatus;\r
876 EFI_STATUS Status;\r
877 UINTN Index;\r
878 EFI_PEI_LOAD_FILE_PPI *LoadFile;\r
879 EFI_PHYSICAL_ADDRESS ImageAddress;\r
880 UINT64 ImageSize;\r
341a658f 881 BOOLEAN IsStrip;\r
b0d803fe 882\r
341a658f 883 IsStrip = FALSE;\r
b0d803fe 884 //\r
885 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.\r
886 // one at a time, until one reports EFI_SUCCESS.\r
887 //\r
888 Index = 0;\r
889 do {\r
890 PpiStatus = PeiServicesLocatePpi (\r
891 &gEfiPeiLoadFilePpiGuid,\r
892 Index,\r
893 NULL,\r
894 (VOID **)&LoadFile\r
895 );\r
896 if (!EFI_ERROR (PpiStatus)) {\r
897 Status = LoadFile->LoadFile (\r
54ea99a7 898 LoadFile,\r
899 FileHandle,\r
900 &ImageAddress,\r
b0d803fe 901 &ImageSize,\r
902 EntryPoint,\r
903 AuthenticationState\r
904 );\r
5d7f3126 905 if (!EFI_ERROR (Status) || Status == EFI_WARN_BUFFER_TOO_SMALL) {\r
341a658f
LG
906 //\r
907 // The shadowed PEIM must be relocatable.\r
908 //\r
c2c4199b 909 if (PeimState == PEIM_STATE_REGISTER_FOR_SHADOW) {\r
341a658f
LG
910 IsStrip = RelocationIsStrip ((VOID *) (UINTN) ImageAddress);\r
911 ASSERT (!IsStrip);\r
912 if (IsStrip) {\r
913 return EFI_UNSUPPORTED;\r
914 }\r
915 }\r
916\r
db0b7ad5
LG
917 //\r
918 // The image to be started must have the machine type supported by PeiCore.\r
919 //\r
920 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress)));\r
919df8e6 921 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress))) {\r
919df8e6
LG
922 return EFI_UNSUPPORTED;\r
923 }\r
5d7f3126 924 return EFI_SUCCESS;\r
b0d803fe 925 }\r
926 }\r
927 Index++;\r
928 } while (!EFI_ERROR (PpiStatus));\r
929\r
8c519a56 930 return PpiStatus;\r
b0d803fe 931}\r
932\r
933\r
b1f6a7c6 934/**\r
b0d803fe 935\r
ed299e3c
LG
936 Install Pei Load File PPI.\r
937\r
938\r
939 @param PrivateData - Pointer to PEI_CORE_INSTANCE.\r
940 @param OldCoreData - Pointer to PEI_CORE_INSTANCE.\r
b0d803fe 941\r
b1f6a7c6 942**/\r
943VOID\r
944InitializeImageServices (\r
945 IN PEI_CORE_INSTANCE *PrivateData,\r
946 IN PEI_CORE_INSTANCE *OldCoreData\r
947 )\r
b0d803fe 948{\r
b0d803fe 949 if (OldCoreData == NULL) {\r
950 //\r
951 // The first time we are XIP (running from FLASH). We need to remember the\r
952 // FLASH address so we can reinstall the memory version that runs faster\r
953 //\r
954 PrivateData->XipLoadFile = &gPpiLoadFilePpiList;\r
955 PeiServicesInstallPpi (PrivateData->XipLoadFile);\r
956 } else {\r
957 //\r
54ea99a7 958 // 2nd time we are running from memory so replace the XIP version with the\r
959 // new memory version.\r
b0d803fe 960 //\r
54ea99a7 961 PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList);\r
b0d803fe 962 }\r
963}\r