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