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