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