]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Image/Image.c
Enable the Load Module At fixed Address feature
[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
4Copyright (c) 2006 - 2010, Intel Corporation\r
5All rights reserved. This program and the accompanying materials\r
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
31 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file\r
32\r
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
53 UINTN Length;\r
54\r
55 Destination8 = Buffer;\r
56 Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);\r
43ada17c 57 if (Destination8 != Source8) {\r
58 Length = *ReadSize;\r
59 while ((Length--) > 0) {\r
60 *(Destination8++) = *(Source8++);\r
61 }\r
a9bfd802
LG
62 }\r
63\r
b0d803fe 64 return EFI_SUCCESS;\r
65}\r
66\r
b1f6a7c6 67/**\r
b0d803fe 68\r
ed299e3c 69 Support routine to get the Image read file function.\r
b0d803fe 70\r
b1f6a7c6 71 @param ImageContext - The context of the image being loaded\r
b0d803fe 72\r
b1f6a7c6 73 @retval EFI_SUCCESS - If Image function location is found\r
b0d803fe 74\r
b1f6a7c6 75**/\r
76EFI_STATUS\r
77GetImageReadFunction (\r
78 IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext\r
79 )\r
b0d803fe 80{\r
43ada17c 81 PEI_CORE_INSTANCE *Private;\r
b0d803fe 82 VOID* MemoryBuffer;\r
83\r
43ada17c 84 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
b0d803fe 85\r
c2a19e92 86 if (!Private->PeiMemoryInstalled || (Private->HobList.HandoffInformationTable->BootMode == BOOT_ON_S3_RESUME)) {\r
43ada17c 87 ImageContext->ImageRead = PeiImageRead;\r
88 } else {\r
89 MemoryBuffer = AllocatePages (0x400 / EFI_PAGE_SIZE + 1);\r
90 ASSERT (MemoryBuffer != NULL);\r
b0d803fe 91\r
43ada17c 92 CopyMem (MemoryBuffer, (CONST VOID *) (UINTN) PeiImageRead, 0x400);\r
93\r
94 ImageContext->ImageRead = (PE_COFF_LOADER_READ_FILE) (UINTN) MemoryBuffer;\r
95 }\r
b0d803fe 96\r
97 return EFI_SUCCESS;\r
98}\r
54ea99a7 99/**\r
100 To check memory usage bit map arry to figure out if the memory range the image will be loaded in is available or not. If \r
101 memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.\r
102 The function is only invoked when load modules at fixed address feature is enabled. \r
103 \r
104 @param Private Pointer to the private data passed in from caller\r
105 @param ImageBase The base addres the image will be loaded at.\r
106 @param ImageSize The size of the image\r
107 \r
108 @retval EFI_SUCCESS The memory range the image will be loaded in is available\r
109 @retval EFI_NOT_FOUND The memory range the image will be loaded in is not available\r
110**/\r
111EFI_STATUS\r
112CheckAndMarkFixLoadingMemoryUsageBitMap (\r
113 IN PEI_CORE_INSTANCE *Private,\r
114 IN EFI_PHYSICAL_ADDRESS ImageBase,\r
115 IN UINT32 ImageSize\r
116 )\r
117{\r
118 UINT32 DxeCodePageNumber;\r
119 UINT64 ReservedCodeSize;\r
120 EFI_PHYSICAL_ADDRESS PeiCodeBase;\r
121 UINT32 BaseOffsetPageNumber;\r
122 UINT32 TopOffsetPageNumber;\r
123 UINT32 Index;\r
124 UINT64 *MemoryUsageBitMap;\r
125 \r
126\r
127 //\r
128 // The reserved code range includes RuntimeCodePage range, Boot time code range and PEI code range.\r
129 //\r
130 DxeCodePageNumber = PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber);\r
131 DxeCodePageNumber += PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber);\r
132 ReservedCodeSize = EFI_PAGES_TO_SIZE(DxeCodePageNumber + PcdGet32(PcdLoadFixAddressPeiCodePageNumber));\r
133 PeiCodeBase = Private->LoadModuleAtFixAddressTopAddress - ReservedCodeSize;\r
134 \r
135 //\r
136 // Test the memory range for loading the image in the PEI code range.\r
137 //\r
138 if ((Private->LoadModuleAtFixAddressTopAddress - EFI_PAGES_TO_SIZE(DxeCodePageNumber)) < (ImageBase + ImageSize) ||\r
139 (PeiCodeBase > ImageBase)) { \r
140 return EFI_NOT_FOUND; \r
141 }\r
142 \r
143 //\r
144 // Test if the memory is avalaible or not.\r
145 //\r
146 MemoryUsageBitMap = Private->PeiCodeMemoryRangeUsageBitMap; \r
147 BaseOffsetPageNumber = EFI_SIZE_TO_PAGES((UINT32)(ImageBase - PeiCodeBase));\r
148 TopOffsetPageNumber = EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - PeiCodeBase));\r
149 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
150 if ((MemoryUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {\r
151 //\r
152 // This page is already used.\r
153 //\r
154 return EFI_NOT_FOUND; \r
155 }\r
156 }\r
157 \r
158 //\r
159 // Being here means the memory range is available. So mark the bits for the memory range\r
160 // \r
161 for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
162 MemoryUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));\r
163 }\r
164 return EFI_SUCCESS; \r
165}\r
166/**\r
167\r
168 Get the fixed loadding address from image header assigned by build tool. This function only be called\r
169 when Loading module at Fixed address feature enabled.\r
170\r
171 @param ImageContext Pointer to the image context structure that describes the PE/COFF\r
172 image that needs to be examined by this function.\r
173 @param Private Pointer to the private data passed in from caller\r
174\r
175 @retval EFI_SUCCESS An fixed loading address is assigned to this image by build tools .\r
176 @retval EFI_NOT_FOUND The image has no assigned fixed loadding address.\r
b0d803fe 177\r
54ea99a7 178**/\r
179EFI_STATUS\r
180GetPeCoffImageFixLoadingAssignedAddress(\r
181 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r
182 IN PEI_CORE_INSTANCE *Private\r
183 )\r
184{\r
185 UINTN SectionHeaderOffset;\r
186 EFI_STATUS Status;\r
187 EFI_IMAGE_SECTION_HEADER SectionHeader;\r
188 EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;\r
189 EFI_PHYSICAL_ADDRESS FixLoaddingAddress;\r
190 UINT16 Index;\r
191 UINTN Size;\r
192 UINT16 NumberOfSections;\r
193 UINT64 ValueInSectionHeader;\r
194 \r
195\r
196 FixLoaddingAddress = 0;\r
197 Status = EFI_NOT_FOUND;\r
198\r
199 //\r
200 // Get PeHeader pointer\r
201 //\r
202 ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r
203 if (ImageContext->IsTeImage) {\r
204 //\r
205 // for TE image, the fix loadding address is saved in first section header that doesn't point\r
206 // to code section.\r
207 //\r
208 SectionHeaderOffset = sizeof (EFI_TE_IMAGE_HEADER);\r
209 NumberOfSections = ImgHdr->Te.NumberOfSections;\r
210 } else {\r
211 SectionHeaderOffset = (UINTN)(\r
212 ImageContext->PeCoffHeaderOffset +\r
213 sizeof (UINT32) +\r
214 sizeof (EFI_IMAGE_FILE_HEADER) +\r
215 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader\r
216 );\r
217 NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
218 }\r
219 //\r
220 // Get base address from the first section header that doesn't point to code section.\r
221 //\r
222 for (Index = 0; Index < NumberOfSections; Index++) {\r
223 //\r
224 // Read section header from file\r
225 //\r
226 Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
227 Status = ImageContext->ImageRead (\r
228 ImageContext->Handle,\r
229 SectionHeaderOffset,\r
230 &Size,\r
231 &SectionHeader\r
232 );\r
233 if (EFI_ERROR (Status)) {\r
234 return Status;\r
235 }\r
236\r
237 Status = EFI_NOT_FOUND;\r
238\r
239 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_CNT_CODE) == 0) {\r
240 //\r
241 // Build tool will save the address in PointerToRelocations & PointerToLineNumbers fields in the first section header\r
242 // that doesn't point to code section in image header, as well as ImageBase field of image header. A notable thing is\r
243 // that for PEIM, the value in ImageBase field may not be equal to the value in PointerToRelocations & PointerToLineNumbers because\r
244 // for XIP PEIM, ImageBase field holds the image base address running on the Flash. And PointerToRelocations & PointerToLineNumbers\r
245 // 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
246 // module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers fields should NOT be Zero, or\r
247 // else, these 2 fileds should be set to Zero\r
248 //\r
249 ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r
250 if (ValueInSectionHeader != 0) {\r
251 //\r
252 // Found first section header that doesn't point to code section.\r
253 //\r
254 if ((INT64)FixedPcdGet64(PcdLoadModuleAtFixAddressEnable) > 0) {\r
255 //\r
256 // When LMFA feature is configured as Load Module at Fixed Absolute Address mode, PointerToRelocations & PointerToLineNumbers field\r
257 // hold the absolute address of image base runing in memory\r
258 //\r
259 FixLoaddingAddress = ValueInSectionHeader;\r
260 } else {\r
261 //\r
262 // When LMFA feature is configured as Load Module at Fixed offset mode, PointerToRelocations & PointerToLineNumbers field\r
263 // hold the offset relative to a platform-specific top address.\r
264 //\r
265 FixLoaddingAddress = (EFI_PHYSICAL_ADDRESS)(Private->LoadModuleAtFixAddressTopAddress + (INT64)ValueInSectionHeader);\r
266 }\r
267 //\r
268 // Check if the memory range is avaliable.\r
269 //\r
270 Status = CheckAndMarkFixLoadingMemoryUsageBitMap (Private, FixLoaddingAddress, (UINT32) ImageContext->ImageSize);\r
271 if (!EFI_ERROR(Status)) {\r
272 //\r
273 // The assigned address is valid. Return the specified loadding address\r
274 //\r
275 ImageContext->ImageAddress = FixLoaddingAddress;\r
276 }\r
277 }\r
278 break;\r
279 }\r
280 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
281 }\r
282 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %lx. Status= %r \n", FixLoaddingAddress, Status));\r
283 return Status;\r
284}\r
b1f6a7c6 285/**\r
286\r
287 Loads and relocates a PE/COFF image into memory.\r
341a658f 288 If the image is not relocatable, it will not be loaded into memory and be loaded as XIP image.\r
b1f6a7c6 289\r
290 @param Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated\r
291 @param ImageAddress - The base address of the relocated PE/COFF image\r
292 @param ImageSize - The size of the relocated PE/COFF image\r
293 @param EntryPoint - The entry point of the relocated PE/COFF image\r
294\r
295 @retval EFI_SUCCESS The file was loaded and relocated\r
296 @retval EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file\r
297\r
298**/\r
b0d803fe 299EFI_STATUS\r
300LoadAndRelocatePeCoffImage (\r
b0d803fe 301 IN VOID *Pe32Data,\r
302 OUT EFI_PHYSICAL_ADDRESS *ImageAddress,\r
303 OUT UINT64 *ImageSize,\r
304 OUT EFI_PHYSICAL_ADDRESS *EntryPoint\r
305 )\r
b0d803fe 306{\r
307 EFI_STATUS Status;\r
308 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
43ada17c 309 PEI_CORE_INSTANCE *Private;\r
310\r
311 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
b0d803fe 312\r
b0d803fe 313 ZeroMem (&ImageContext, sizeof (ImageContext));\r
314 ImageContext.Handle = Pe32Data;\r
315 Status = GetImageReadFunction (&ImageContext);\r
316\r
317 ASSERT_EFI_ERROR (Status);\r
318\r
3d7b0992 319 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
b0d803fe 320 if (EFI_ERROR (Status)) {\r
321 return Status;\r
322 }\r
323 //\r
9626a87e
LG
324 // When Image has no reloc section, it can't be relocated into memory.\r
325 //\r
414bdfb6 326 if (ImageContext.RelocationsStripped && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
54ea99a7 327 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 328 }\r
414bdfb6 329\r
341a658f
LG
330 //\r
331 // Set default base address to current image address.\r
332 //\r
333 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
54ea99a7 334\r
9626a87e 335 //\r
341a658f 336 // Allocate Memory for the image when memory is ready, boot mode is not S3, and image is relocatable.\r
b0d803fe 337 //\r
bfd4a3f1 338 if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
54ea99a7 339 if (FixedPcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
340 Status = GetPeCoffImageFixLoadingAssignedAddress(&ImageContext, Private);\r
341 if (EFI_ERROR (Status)){\r
342 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));\r
343 //\r
344 // The PEIM is not assiged valid address, try to allocate page to load it.\r
345 //\r
346 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
347 }\r
348 } else {\r
349 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize));\r
350 }\r
43ada17c 351 ASSERT (ImageContext.ImageAddress != 0);\r
341a658f
LG
352 if (ImageContext.ImageAddress == 0) {\r
353 return EFI_OUT_OF_RESOURCES;\r
354 }\r
54ea99a7 355\r
43ada17c 356 //\r
357 // Skip the reserved space for the stripped PeHeader when load TeImage into memory.\r
358 //\r
359 if (ImageContext.IsTeImage) {\r
54ea99a7 360 ImageContext.ImageAddress = ImageContext.ImageAddress +\r
43ada17c 361 ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -\r
362 sizeof (EFI_TE_IMAGE_HEADER);\r
363 }\r
4e844595 364 }\r
b0d803fe 365\r
366 //\r
367 // Load the image to our new buffer\r
368 //\r
3d7b0992 369 Status = PeCoffLoaderLoadImage (&ImageContext);\r
b0d803fe 370 if (EFI_ERROR (Status)) {\r
371 return Status;\r
372 }\r
373 //\r
374 // Relocate the image in our new buffer\r
375 //\r
3d7b0992 376 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
b0d803fe 377 if (EFI_ERROR (Status)) {\r
378 return Status;\r
379 }\r
380\r
381 //\r
382 // Flush the instruction cache so the image data is written before we execute it\r
383 //\r
341a658f 384 if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
43ada17c 385 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
386 }\r
b0d803fe 387\r
388 *ImageAddress = ImageContext.ImageAddress;\r
389 *ImageSize = ImageContext.ImageSize;\r
390 *EntryPoint = ImageContext.EntryPoint;\r
391\r
392 return EFI_SUCCESS;\r
393}\r
394\r
b1f6a7c6 395/**\r
54ea99a7 396 Loads a PEIM into memory for subsequent execution. If there are compressed\r
397 images or images that need to be relocated into memory for performance reasons,\r
ed299e3c 398 this service performs that transformation.\r
b1f6a7c6 399\r
ed299e3c 400 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
b1f6a7c6 401 @param FileHandle Pointer to the FFS file header of the image.\r
402 @param ImageAddressArg Pointer to PE/TE image.\r
403 @param ImageSizeArg Size of PE/TE image.\r
404 @param EntryPoint Pointer to entry point of specified image file for output.\r
405 @param AuthenticationState - Pointer to attestation authentication state of image.\r
406\r
ed299e3c
LG
407 @retval EFI_SUCCESS Image is successfully loaded.\r
408 @retval EFI_NOT_FOUND Fail to locate necessary PPI.\r
409 @retval EFI_UNSUPPORTED Image Machine Type is not supported.\r
b1f6a7c6 410\r
411**/\r
b0d803fe 412EFI_STATUS\r
413PeiLoadImageLoadImage (\r
284c8400 414 IN CONST EFI_PEI_SERVICES **PeiServices,\r
b0d803fe 415 IN EFI_PEI_FILE_HANDLE FileHandle,\r
416 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
417 OUT UINT64 *ImageSizeArg, OPTIONAL\r
418 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
419 OUT UINT32 *AuthenticationState\r
420 )\r
192f6d4c 421{\r
422 EFI_STATUS Status;\r
423 VOID *Pe32Data;\r
192f6d4c 424 EFI_PHYSICAL_ADDRESS ImageAddress;\r
425 UINT64 ImageSize;\r
426 EFI_PHYSICAL_ADDRESS ImageEntryPoint;\r
192f6d4c 427 UINT16 Machine;\r
3076397e 428 EFI_SECTION_TYPE SearchType1;\r
429 EFI_SECTION_TYPE SearchType2;\r
192f6d4c 430\r
3d7b0992
LG
431 *EntryPoint = 0;\r
432 ImageSize = 0;\r
b0d803fe 433 *AuthenticationState = 0;\r
192f6d4c 434\r
3076397e 435 if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst)) {\r
436 SearchType1 = EFI_SECTION_TE;\r
437 SearchType2 = EFI_SECTION_PE32;\r
438 } else {\r
439 SearchType1 = EFI_SECTION_PE32;\r
440 SearchType2 = EFI_SECTION_TE;\r
441 }\r
1b620adb 442\r
192f6d4c 443 //\r
54ea99a7 444 // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst\r
3076397e 445 // is true, TE will be searched first).\r
192f6d4c 446 //\r
447 Status = PeiServicesFfsFindSectionData (\r
3076397e 448 SearchType1,\r
b0d803fe 449 FileHandle,\r
192f6d4c 450 &Pe32Data\r
451 );\r
452 //\r
3076397e 453 // If we didn't find a first exe section, try to find the second exe section.\r
192f6d4c 454 //\r
455 if (EFI_ERROR (Status)) {\r
456 Status = PeiServicesFfsFindSectionData (\r
3076397e 457 SearchType2,\r
b0d803fe 458 FileHandle,\r
459 &Pe32Data\r
192f6d4c 460 );\r
b0d803fe 461 if (EFI_ERROR (Status)) {\r
192f6d4c 462 //\r
b0d803fe 463 // PEI core only carry the loader function fro TE and PE32 executables\r
464 // If this two section does not exist, just return.\r
192f6d4c 465 //\r
b0d803fe 466 return Status;\r
467 }\r
468 }\r
54ea99a7 469\r
43ada17c 470 //\r
471 // If memory is installed, perform the shadow operations\r
472 //\r
473 Status = LoadAndRelocatePeCoffImage (\r
474 Pe32Data,\r
475 &ImageAddress,\r
476 &ImageSize,\r
477 &ImageEntryPoint\r
478 );\r
192f6d4c 479\r
43ada17c 480 ASSERT_EFI_ERROR (Status);\r
3d7b0992 481\r
43ada17c 482\r
483 if (EFI_ERROR (Status)) {\r
484 return Status;\r
192f6d4c 485 }\r
43ada17c 486\r
487 //\r
488 // Got the entry point from the loaded Pe32Data\r
489 //\r
490 Pe32Data = (VOID *) ((UINTN) ImageAddress);\r
491 *EntryPoint = ImageEntryPoint;\r
54ea99a7 492\r
3d7b0992 493 Machine = PeCoffLoaderGetMachineType (Pe32Data);\r
54ea99a7 494\r
192f6d4c 495 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {\r
8c519a56 496 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine)) {\r
497 return EFI_UNSUPPORTED;\r
498 }\r
192f6d4c 499 }\r
500\r
b0d803fe 501 if (ImageAddressArg != NULL) {\r
502 *ImageAddressArg = ImageAddress;\r
503 }\r
504\r
505 if (ImageSizeArg != NULL) {\r
506 *ImageSizeArg = ImageSize;\r
507 }\r
54ea99a7 508\r
192f6d4c 509 DEBUG_CODE_BEGIN ();\r
3d7b0992
LG
510 CHAR8 *AsciiString;\r
511 CHAR8 AsciiBuffer[512];\r
512 INT32 Index;\r
513 INT32 Index1;\r
e98cd821
LG
514\r
515 //\r
516 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
517 //\r
7cf02714 518 if (Machine != EFI_IMAGE_MACHINE_IA64) {\r
91136124 519 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));\r
e98cd821
LG
520 } else {\r
521 //\r
522 // For IPF Image, the real entry point should be print.\r
523 //\r
91136124 524 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 525 }\r
54ea99a7 526\r
e98cd821
LG
527 //\r
528 // Print Module Name by PeImage PDB file name.\r
529 //\r
3d7b0992 530 AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r
54ea99a7 531\r
3d7b0992 532 if (AsciiString != NULL) {\r
19ea58a1 533 for (Index = (INT32) AsciiStrLen (AsciiString) - 1; Index >= 0; Index --) {\r
3d7b0992
LG
534 if (AsciiString[Index] == '\\') {\r
535 break;\r
536 }\r
192f6d4c 537 }\r
192f6d4c 538\r
3d7b0992
LG
539 if (Index != 0) {\r
540 for (Index1 = 0; AsciiString[Index + 1 + Index1] != '.'; Index1 ++) {\r
541 AsciiBuffer [Index1] = AsciiString[Index + 1 + Index1];\r
192f6d4c 542 }\r
3d7b0992
LG
543 AsciiBuffer [Index1] = '\0';\r
544 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a.efi", AsciiBuffer));\r
192f6d4c 545 }\r
546 }\r
3d7b0992 547\r
192f6d4c 548 DEBUG_CODE_END ();\r
549\r
550 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r
551\r
552 return EFI_SUCCESS;\r
b0d803fe 553\r
554}\r
555\r
556\r
b1f6a7c6 557/**\r
558 The wrapper function of PeiLoadImageLoadImage().\r
559\r
560 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.\r
561 @param FileHandle - Pointer to the FFS file header of the image.\r
562 @param ImageAddressArg - Pointer to PE/TE image.\r
563 @param ImageSizeArg - Size of PE/TE image.\r
564 @param EntryPoint - Pointer to entry point of specified image file for output.\r
565 @param AuthenticationState - Pointer to attestation authentication state of image.\r
566\r
567 @return Status of PeiLoadImageLoadImage().\r
568\r
569**/\r
b0d803fe 570EFI_STATUS\r
571EFIAPI\r
572PeiLoadImageLoadImageWrapper (\r
573 IN CONST EFI_PEI_LOAD_FILE_PPI *This,\r
574 IN EFI_PEI_FILE_HANDLE FileHandle,\r
575 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
576 OUT UINT64 *ImageSizeArg, OPTIONAL\r
577 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
578 OUT UINT32 *AuthenticationState\r
579 )\r
b0d803fe 580{\r
581 return PeiLoadImageLoadImage (\r
582 GetPeiServicesTablePointer (),\r
583 FileHandle,\r
584 ImageAddressArg,\r
585 ImageSizeArg,\r
586 EntryPoint,\r
587 AuthenticationState\r
588 );\r
192f6d4c 589}\r
b0d803fe 590\r
341a658f
LG
591/**\r
592 Check whether the input image has the relocation.\r
593\r
594 @param Pe32Data Pointer to the PE/COFF or TE image.\r
595\r
596 @retval TRUE Relocation is stripped.\r
597 @retval FALSE Relocation is not stripped.\r
598\r
599**/\r
600BOOLEAN\r
601RelocationIsStrip (\r
602 IN VOID *Pe32Data\r
603 )\r
604{\r
605 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
606 EFI_IMAGE_DOS_HEADER *DosHdr;\r
607\r
608 ASSERT (Pe32Data != NULL);\r
609\r
610 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
611 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
612 //\r
613 // DOS image header is present, so read the PE header after the DOS image header.\r
614 //\r
615 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r
616 } else {\r
617 //\r
618 // DOS image header is not present, so PE header is at the image base.\r
619 //\r
620 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
621 }\r
622\r
623 //\r
624 // Three cases with regards to relocations:\r
625 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable\r
626 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable\r
627 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but\r
628 // has no base relocs to apply\r
629 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.\r
630 //\r
631 // Look at the file header to determine if relocations have been stripped, and\r
632 // save this info in the image context for later use.\r
633 //\r
634 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
635 if ((Hdr.Te->DataDirectory[0].Size == 0) && (Hdr.Te->DataDirectory[0].VirtualAddress == 0)) {\r
636 return TRUE;\r
637 } else {\r
638 return FALSE;\r
639 }\r
640 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
641 if ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0) {\r
642 return TRUE;\r
643 } else {\r
644 return FALSE;\r
645 }\r
646 }\r
647\r
648 return FALSE;\r
649}\r
650\r
b1f6a7c6 651/**\r
ed299e3c 652 Routine to load image file for subsequent execution by LoadFile Ppi.\r
54ea99a7 653 If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE\r
ed299e3c 654 XIP image format is used.\r
b1f6a7c6 655\r
ed299e3c
LG
656 @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
657 @param FileHandle - Pointer to the FFS file header of the image.\r
341a658f 658 @param PeimState - The dispatch state of the input PEIM handle.\r
ed299e3c
LG
659 @param EntryPoint - Pointer to entry point of specified image file for output.\r
660 @param AuthenticationState - Pointer to attestation authentication state of image.\r
b1f6a7c6 661\r
ed299e3c
LG
662 @retval EFI_SUCCESS - Image is successfully loaded.\r
663 @retval EFI_NOT_FOUND - Fail to locate necessary PPI\r
664 @retval Others - Fail to load file.\r
b1f6a7c6 665\r
666**/\r
b0d803fe 667EFI_STATUS\r
668PeiLoadImage (\r
6c7a807a 669 IN CONST EFI_PEI_SERVICES **PeiServices,\r
b0d803fe 670 IN EFI_PEI_FILE_HANDLE FileHandle,\r
341a658f 671 IN UINT8 PeimState,\r
b0d803fe 672 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
673 OUT UINT32 *AuthenticationState\r
674 )\r
b0d803fe 675{\r
676 EFI_STATUS PpiStatus;\r
677 EFI_STATUS Status;\r
678 UINTN Index;\r
679 EFI_PEI_LOAD_FILE_PPI *LoadFile;\r
680 EFI_PHYSICAL_ADDRESS ImageAddress;\r
681 UINT64 ImageSize;\r
341a658f 682 BOOLEAN IsStrip;\r
b0d803fe 683\r
341a658f 684 IsStrip = FALSE;\r
b0d803fe 685 //\r
686 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.\r
687 // one at a time, until one reports EFI_SUCCESS.\r
688 //\r
689 Index = 0;\r
690 do {\r
691 PpiStatus = PeiServicesLocatePpi (\r
692 &gEfiPeiLoadFilePpiGuid,\r
693 Index,\r
694 NULL,\r
695 (VOID **)&LoadFile\r
696 );\r
697 if (!EFI_ERROR (PpiStatus)) {\r
698 Status = LoadFile->LoadFile (\r
54ea99a7 699 LoadFile,\r
700 FileHandle,\r
701 &ImageAddress,\r
b0d803fe 702 &ImageSize,\r
703 EntryPoint,\r
704 AuthenticationState\r
705 );\r
706 if (!EFI_ERROR (Status)) {\r
341a658f
LG
707 //\r
708 // The shadowed PEIM must be relocatable.\r
709 //\r
710 if (PeimState == PEIM_STATE_REGISITER_FOR_SHADOW) {\r
711 IsStrip = RelocationIsStrip ((VOID *) (UINTN) ImageAddress);\r
712 ASSERT (!IsStrip);\r
713 if (IsStrip) {\r
714 return EFI_UNSUPPORTED;\r
715 }\r
716 }\r
717\r
db0b7ad5
LG
718 //\r
719 // The image to be started must have the machine type supported by PeiCore.\r
720 //\r
721 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress)));\r
919df8e6 722 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress))) {\r
919df8e6
LG
723 return EFI_UNSUPPORTED;\r
724 }\r
b0d803fe 725 return Status;\r
726 }\r
727 }\r
728 Index++;\r
729 } while (!EFI_ERROR (PpiStatus));\r
730\r
8c519a56 731 return PpiStatus;\r
b0d803fe 732}\r
733\r
734\r
b1f6a7c6 735/**\r
b0d803fe 736\r
ed299e3c
LG
737 Install Pei Load File PPI.\r
738\r
739\r
740 @param PrivateData - Pointer to PEI_CORE_INSTANCE.\r
741 @param OldCoreData - Pointer to PEI_CORE_INSTANCE.\r
b0d803fe 742\r
b1f6a7c6 743**/\r
744VOID\r
745InitializeImageServices (\r
746 IN PEI_CORE_INSTANCE *PrivateData,\r
747 IN PEI_CORE_INSTANCE *OldCoreData\r
748 )\r
b0d803fe 749{\r
b0d803fe 750 if (OldCoreData == NULL) {\r
751 //\r
752 // The first time we are XIP (running from FLASH). We need to remember the\r
753 // FLASH address so we can reinstall the memory version that runs faster\r
754 //\r
755 PrivateData->XipLoadFile = &gPpiLoadFilePpiList;\r
756 PeiServicesInstallPpi (PrivateData->XipLoadFile);\r
757 } else {\r
758 //\r
54ea99a7 759 // 2nd time we are running from memory so replace the XIP version with the\r
760 // new memory version.\r
b0d803fe 761 //\r
54ea99a7 762 PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList);\r
b0d803fe 763 }\r
764}\r
765\r
766\r
767\r
341a658f 768\r