]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/Pei/Image/Image.c
MdeModulePkg RamDiskDxe: Install Block I/O 2 Protocol on RAM disk devices
[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
3d44658c 4Copyright (c) 2006 - 2015, 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
142 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
143 memory range is avaliable, the function will mark the correponding bits to 1 which indicates the memory range is used.\r
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
147 @param ImageBase The base addres the image will be loaded at.\r
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
210 Get the fixed loadding address from image header assigned by build tool. This function only be called\r
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
218 @retval EFI_NOT_FOUND The image has no assigned fixed loadding 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
231 EFI_PHYSICAL_ADDRESS FixLoaddingAddress;\r
232 UINT16 Index;\r
233 UINTN Size;\r
234 UINT16 NumberOfSections;\r
235 UINT64 ValueInSectionHeader;\r
236 \r
237\r
238 FixLoaddingAddress = 0;\r
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
247 // for TE image, the fix loadding address is saved in first section header that doesn't point\r
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
289 // else, these 2 fileds should be set to Zero\r
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
299 // hold the absolute address of image base runing in memory\r
300 //\r
301 FixLoaddingAddress = ValueInSectionHeader;\r
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
307 FixLoaddingAddress = (EFI_PHYSICAL_ADDRESS)(Private->LoadModuleAtFixAddressTopAddress + (INT64)ValueInSectionHeader);\r
308 }\r
309 //\r
310 // Check if the memory range is avaliable.\r
311 //\r
312 Status = CheckAndMarkFixLoadingMemoryUsageBitMap (Private, FixLoaddingAddress, (UINT32) ImageContext->ImageSize);\r
313 if (!EFI_ERROR(Status)) {\r
314 //\r
315 // The assigned address is valid. Return the specified loadding address\r
316 //\r
317 ImageContext->ImageAddress = FixLoaddingAddress;\r
318 }\r
319 }\r
320 break;\r
321 }\r
322 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
323 }\r
852081fc 324 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address 0x%11p. Status= %r \n", (VOID *)(UINTN)FixLoaddingAddress, 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
LG
360 BOOLEAN IsS3Boot;\r
361 BOOLEAN IsRegisterForShadow;\r
43ada17c 362\r
363 Private = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
b0d803fe 364\r
5d7f3126
LG
365 ReturnStatus = EFI_SUCCESS;\r
366 IsXipImage = FALSE;\r
b0d803fe 367 ZeroMem (&ImageContext, sizeof (ImageContext));\r
368 ImageContext.Handle = Pe32Data;\r
369 Status = GetImageReadFunction (&ImageContext);\r
370\r
371 ASSERT_EFI_ERROR (Status);\r
372\r
3d7b0992 373 Status = PeCoffLoaderGetImageInfo (&ImageContext);\r
b0d803fe 374 if (EFI_ERROR (Status)) {\r
375 return Status;\r
376 }\r
5d7f3126 377 \r
3d44658c
LG
378 //\r
379 // Initilize local IsS3Boot and IsRegisterForShadow variable\r
380 //\r
381 IsS3Boot = FALSE;\r
382 if (Private->HobList.HandoffInformationTable->BootMode == BOOT_ON_S3_RESUME) {\r
383 IsS3Boot = TRUE;\r
384 }\r
385 IsRegisterForShadow = FALSE;\r
386 if ((Private->CurrentFileHandle == FileHandle) \r
387 && (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW)) {\r
388 IsRegisterForShadow = TRUE;\r
389 }\r
390\r
5d7f3126
LG
391 //\r
392 // XIP image that ImageAddress is same to Image handle.\r
393 //\r
394 if (ImageContext.ImageAddress == (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data) {\r
395 IsXipImage = TRUE;\r
396 }\r
397\r
b0d803fe 398 //\r
9626a87e
LG
399 // When Image has no reloc section, it can't be relocated into memory.\r
400 //\r
3d44658c
LG
401 if (ImageContext.RelocationsStripped && (Private->PeiMemoryInstalled) && (\r
402 (!IsS3Boot && (PcdGetBool (PcdShadowPeimOnBoot) || IsRegisterForShadow)) || (IsS3Boot && PcdGetBool (PcdShadowPeimOnS3Boot)))) {\r
54ea99a7 403 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 404 }\r
414bdfb6 405\r
341a658f
LG
406 //\r
407 // Set default base address to current image address.\r
408 //\r
409 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
54ea99a7 410\r
9626a87e 411 //\r
3d44658c
LG
412 // Allocate Memory for the image when memory is ready, and image is relocatable.\r
413 // On normal boot, PcdShadowPeimOnBoot decides whether load PEIM or PeiCore into memory.\r
414 // On S3 boot, PcdShadowPeimOnS3Boot decides whether load PEIM or PeiCore into memory.\r
b0d803fe 415 //\r
3d44658c
LG
416 if ((!ImageContext.RelocationsStripped) && (Private->PeiMemoryInstalled) && (\r
417 (!IsS3Boot && (PcdGetBool (PcdShadowPeimOnBoot) || IsRegisterForShadow)) || (IsS3Boot && PcdGetBool (PcdShadowPeimOnS3Boot)))) {\r
935efc21
ED
418 //\r
419 // Allocate more buffer to avoid buffer overflow.\r
420 //\r
421 if (ImageContext.IsTeImage) {\r
422 AlignImageSize = ImageContext.ImageSize + ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER);\r
423 } else {\r
424 AlignImageSize = ImageContext.ImageSize;\r
425 }\r
426\r
427 if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r
428 AlignImageSize += ImageContext.SectionAlignment;\r
429 }\r
430\r
5d7f3126 431 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {\r
54ea99a7 432 Status = GetPeCoffImageFixLoadingAssignedAddress(&ImageContext, Private);\r
433 if (EFI_ERROR (Status)){\r
434 DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));\r
435 //\r
436 // The PEIM is not assiged valid address, try to allocate page to load it.\r
437 //\r
935efc21 438 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize));\r
54ea99a7 439 }\r
440 } else {\r
935efc21 441 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) AlignImageSize));\r
54ea99a7 442 }\r
5d7f3126
LG
443 if (ImageContext.ImageAddress != 0) {\r
444 //\r
445 // Adjust the Image Address to make sure it is section alignment.\r
446 //\r
447 if (ImageContext.SectionAlignment > EFI_PAGE_SIZE) {\r
448 ImageContext.ImageAddress =\r
449 (ImageContext.ImageAddress + ImageContext.SectionAlignment - 1) &\r
450 ~((UINTN)ImageContext.SectionAlignment - 1);\r
451 }\r
452 //\r
453 // Fix alignment requirement when Load IPF TeImage into memory.\r
454 // Skip the reserved space for the stripped PeHeader when load TeImage into memory.\r
455 //\r
456 if (ImageContext.IsTeImage) {\r
457 ImageContext.ImageAddress = ImageContext.ImageAddress +\r
458 ((EFI_TE_IMAGE_HEADER *) Pe32Data)->StrippedSize -\r
459 sizeof (EFI_TE_IMAGE_HEADER);\r
460 }\r
461 } else {\r
462 //\r
463 // No enough memory resource.\r
464 //\r
465 if (IsXipImage) {\r
466 //\r
467 // XIP image can still be invoked.\r
468 //\r
469 ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data;\r
470 ReturnStatus = EFI_WARN_BUFFER_TOO_SMALL;\r
471 } else {\r
472 //\r
473 // Non XIP image can't be loaded because no enough memory is allocated.\r
474 //\r
475 ASSERT (FALSE);\r
476 return EFI_OUT_OF_RESOURCES;\r
477 }\r
43ada17c 478 }\r
4e844595 479 }\r
b0d803fe 480\r
481 //\r
482 // Load the image to our new buffer\r
483 //\r
3d7b0992 484 Status = PeCoffLoaderLoadImage (&ImageContext);\r
b0d803fe 485 if (EFI_ERROR (Status)) {\r
486 return Status;\r
487 }\r
488 //\r
489 // Relocate the image in our new buffer\r
490 //\r
3d7b0992 491 Status = PeCoffLoaderRelocateImage (&ImageContext);\r
b0d803fe 492 if (EFI_ERROR (Status)) {\r
493 return Status;\r
494 }\r
495\r
496 //\r
497 // Flush the instruction cache so the image data is written before we execute it\r
498 //\r
5d7f3126 499 if (ImageContext.ImageAddress != (EFI_PHYSICAL_ADDRESS)(UINTN) Pe32Data) {\r
43ada17c 500 InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
501 }\r
b0d803fe 502\r
503 *ImageAddress = ImageContext.ImageAddress;\r
504 *ImageSize = ImageContext.ImageSize;\r
505 *EntryPoint = ImageContext.EntryPoint;\r
506\r
5d7f3126 507 return ReturnStatus;\r
b0d803fe 508}\r
509\r
b1f6a7c6 510/**\r
54ea99a7 511 Loads a PEIM into memory for subsequent execution. If there are compressed\r
512 images or images that need to be relocated into memory for performance reasons,\r
ed299e3c 513 this service performs that transformation.\r
b1f6a7c6 514\r
ed299e3c 515 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
b1f6a7c6 516 @param FileHandle Pointer to the FFS file header of the image.\r
517 @param ImageAddressArg Pointer to PE/TE image.\r
518 @param ImageSizeArg Size of PE/TE image.\r
519 @param EntryPoint Pointer to entry point of specified image file for output.\r
520 @param AuthenticationState - Pointer to attestation authentication state of image.\r
521\r
ed299e3c
LG
522 @retval EFI_SUCCESS Image is successfully loaded.\r
523 @retval EFI_NOT_FOUND Fail to locate necessary PPI.\r
524 @retval EFI_UNSUPPORTED Image Machine Type is not supported.\r
5d7f3126
LG
525 @retval EFI_WARN_BUFFER_TOO_SMALL \r
526 There is not enough heap to allocate the requested size.\r
527 This will not prevent the XIP image from being invoked.\r
b1f6a7c6 528\r
529**/\r
b0d803fe 530EFI_STATUS\r
531PeiLoadImageLoadImage (\r
284c8400 532 IN CONST EFI_PEI_SERVICES **PeiServices,\r
b0d803fe 533 IN EFI_PEI_FILE_HANDLE FileHandle,\r
534 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
535 OUT UINT64 *ImageSizeArg, OPTIONAL\r
536 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
537 OUT UINT32 *AuthenticationState\r
538 )\r
192f6d4c 539{\r
540 EFI_STATUS Status;\r
541 VOID *Pe32Data;\r
192f6d4c 542 EFI_PHYSICAL_ADDRESS ImageAddress;\r
543 UINT64 ImageSize;\r
544 EFI_PHYSICAL_ADDRESS ImageEntryPoint;\r
192f6d4c 545 UINT16 Machine;\r
3076397e 546 EFI_SECTION_TYPE SearchType1;\r
547 EFI_SECTION_TYPE SearchType2;\r
192f6d4c 548\r
3d7b0992
LG
549 *EntryPoint = 0;\r
550 ImageSize = 0;\r
b0d803fe 551 *AuthenticationState = 0;\r
192f6d4c 552\r
3076397e 553 if (FeaturePcdGet (PcdPeiCoreImageLoaderSearchTeSectionFirst)) {\r
554 SearchType1 = EFI_SECTION_TE;\r
555 SearchType2 = EFI_SECTION_PE32;\r
556 } else {\r
557 SearchType1 = EFI_SECTION_PE32;\r
558 SearchType2 = EFI_SECTION_TE;\r
559 }\r
1b620adb 560\r
192f6d4c 561 //\r
54ea99a7 562 // Try to find a first exe section (if PcdPeiCoreImageLoaderSearchTeSectionFirst\r
3076397e 563 // is true, TE will be searched first).\r
192f6d4c 564 //\r
c7935105 565 Status = PeiServicesFfsFindSectionData3 (\r
3076397e 566 SearchType1,\r
c7935105 567 0,\r
b0d803fe 568 FileHandle,\r
c7935105
SZ
569 &Pe32Data,\r
570 AuthenticationState\r
192f6d4c 571 );\r
572 //\r
3076397e 573 // If we didn't find a first exe section, try to find the second exe section.\r
192f6d4c 574 //\r
575 if (EFI_ERROR (Status)) {\r
c7935105 576 Status = PeiServicesFfsFindSectionData3 (\r
3076397e 577 SearchType2,\r
c7935105 578 0,\r
b0d803fe 579 FileHandle,\r
c7935105
SZ
580 &Pe32Data,\r
581 AuthenticationState\r
192f6d4c 582 );\r
b0d803fe 583 if (EFI_ERROR (Status)) {\r
192f6d4c 584 //\r
c7935105 585 // PEI core only carry the loader function for TE and PE32 executables\r
b0d803fe 586 // If this two section does not exist, just return.\r
192f6d4c 587 //\r
b0d803fe 588 return Status;\r
589 }\r
590 }\r
54ea99a7 591\r
43ada17c 592 //\r
593 // If memory is installed, perform the shadow operations\r
594 //\r
595 Status = LoadAndRelocatePeCoffImage (\r
3d44658c 596 FileHandle,\r
43ada17c 597 Pe32Data,\r
598 &ImageAddress,\r
599 &ImageSize,\r
600 &ImageEntryPoint\r
601 );\r
192f6d4c 602\r
43ada17c 603 ASSERT_EFI_ERROR (Status);\r
3d7b0992 604\r
43ada17c 605\r
606 if (EFI_ERROR (Status)) {\r
607 return Status;\r
192f6d4c 608 }\r
43ada17c 609\r
610 //\r
611 // Got the entry point from the loaded Pe32Data\r
612 //\r
613 Pe32Data = (VOID *) ((UINTN) ImageAddress);\r
614 *EntryPoint = ImageEntryPoint;\r
54ea99a7 615\r
3d7b0992 616 Machine = PeCoffLoaderGetMachineType (Pe32Data);\r
54ea99a7 617\r
192f6d4c 618 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (Machine)) {\r
8c519a56 619 if (!EFI_IMAGE_MACHINE_CROSS_TYPE_SUPPORTED (Machine)) {\r
620 return EFI_UNSUPPORTED;\r
621 }\r
192f6d4c 622 }\r
623\r
b0d803fe 624 if (ImageAddressArg != NULL) {\r
625 *ImageAddressArg = ImageAddress;\r
626 }\r
627\r
628 if (ImageSizeArg != NULL) {\r
629 *ImageSizeArg = ImageSize;\r
630 }\r
54ea99a7 631\r
192f6d4c 632 DEBUG_CODE_BEGIN ();\r
3d7b0992 633 CHAR8 *AsciiString;\r
77a750a5 634 CHAR8 EfiFileName[512];\r
3d7b0992 635 INT32 Index;\r
77a750a5 636 INT32 StartIndex;\r
e98cd821
LG
637\r
638 //\r
639 // Print debug message: Loading PEIM at 0x12345678 EntryPoint=0x12345688 Driver.efi\r
640 //\r
7cf02714 641 if (Machine != EFI_IMAGE_MACHINE_IA64) {\r
91136124 642 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "Loading PEIM at 0x%11p EntryPoint=0x%11p ", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)*EntryPoint));\r
e98cd821
LG
643 } else {\r
644 //\r
645 // For IPF Image, the real entry point should be print.\r
646 //\r
91136124 647 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 648 }\r
54ea99a7 649\r
e98cd821
LG
650 //\r
651 // Print Module Name by PeImage PDB file name.\r
652 //\r
3d7b0992 653 AsciiString = PeCoffLoaderGetPdbPointer (Pe32Data);\r
54ea99a7 654\r
3d7b0992 655 if (AsciiString != NULL) {\r
77a750a5
LG
656 StartIndex = 0;\r
657 for (Index = 0; AsciiString[Index] != 0; Index++) {\r
1e21413c 658 if (AsciiString[Index] == '\\' || AsciiString[Index] == '/') {\r
77a750a5 659 StartIndex = Index + 1;\r
3d7b0992 660 }\r
192f6d4c 661 }\r
192f6d4c 662\r
77a750a5
LG
663 //\r
664 // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r
665 // The PDB file name is limited in the range of 0~511.\r
666 // If the length is bigger than 511, trim the redudant characters to avoid overflow in array boundary.\r
667 //\r
668 for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
669 EfiFileName[Index] = AsciiString[Index + StartIndex];\r
670 if (EfiFileName[Index] == 0) {\r
671 EfiFileName[Index] = '.';\r
672 }\r
673 if (EfiFileName[Index] == '.') {\r
674 EfiFileName[Index + 1] = 'e';\r
675 EfiFileName[Index + 2] = 'f';\r
676 EfiFileName[Index + 3] = 'i';\r
677 EfiFileName[Index + 4] = 0;\r
678 break;\r
192f6d4c 679 }\r
680 }\r
77a750a5
LG
681\r
682 if (Index == sizeof (EfiFileName) - 4) {\r
683 EfiFileName[Index] = 0;\r
684 }\r
685\r
686 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "%a", EfiFileName));\r
192f6d4c 687 }\r
3d7b0992 688\r
192f6d4c 689 DEBUG_CODE_END ();\r
690\r
691 DEBUG ((EFI_D_INFO | EFI_D_LOAD, "\n"));\r
692\r
693 return EFI_SUCCESS;\r
b0d803fe 694\r
695}\r
696\r
697\r
b1f6a7c6 698/**\r
699 The wrapper function of PeiLoadImageLoadImage().\r
700\r
701 @param This - Pointer to EFI_PEI_LOAD_FILE_PPI.\r
702 @param FileHandle - Pointer to the FFS file header of the image.\r
703 @param ImageAddressArg - Pointer to PE/TE image.\r
704 @param ImageSizeArg - Size of PE/TE image.\r
705 @param EntryPoint - Pointer to entry point of specified image file for output.\r
706 @param AuthenticationState - Pointer to attestation authentication state of image.\r
707\r
708 @return Status of PeiLoadImageLoadImage().\r
709\r
710**/\r
b0d803fe 711EFI_STATUS\r
712EFIAPI\r
713PeiLoadImageLoadImageWrapper (\r
714 IN CONST EFI_PEI_LOAD_FILE_PPI *This,\r
715 IN EFI_PEI_FILE_HANDLE FileHandle,\r
716 OUT EFI_PHYSICAL_ADDRESS *ImageAddressArg, OPTIONAL\r
717 OUT UINT64 *ImageSizeArg, OPTIONAL\r
718 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
719 OUT UINT32 *AuthenticationState\r
720 )\r
b0d803fe 721{\r
722 return PeiLoadImageLoadImage (\r
723 GetPeiServicesTablePointer (),\r
724 FileHandle,\r
725 ImageAddressArg,\r
726 ImageSizeArg,\r
727 EntryPoint,\r
728 AuthenticationState\r
729 );\r
192f6d4c 730}\r
b0d803fe 731\r
341a658f
LG
732/**\r
733 Check whether the input image has the relocation.\r
734\r
735 @param Pe32Data Pointer to the PE/COFF or TE image.\r
736\r
737 @retval TRUE Relocation is stripped.\r
738 @retval FALSE Relocation is not stripped.\r
739\r
740**/\r
741BOOLEAN\r
742RelocationIsStrip (\r
743 IN VOID *Pe32Data\r
744 )\r
745{\r
746 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
747 EFI_IMAGE_DOS_HEADER *DosHdr;\r
748\r
749 ASSERT (Pe32Data != NULL);\r
750\r
751 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
752 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
753 //\r
754 // DOS image header is present, so read the PE header after the DOS image header.\r
755 //\r
756 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN) Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r
757 } else {\r
758 //\r
759 // DOS image header is not present, so PE header is at the image base.\r
760 //\r
761 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r
762 }\r
763\r
764 //\r
765 // Three cases with regards to relocations:\r
766 // - Image has base relocs, RELOCS_STRIPPED==0 => image is relocatable\r
767 // - Image has no base relocs, RELOCS_STRIPPED==1 => Image is not relocatable\r
768 // - Image has no base relocs, RELOCS_STRIPPED==0 => Image is relocatable but\r
769 // has no base relocs to apply\r
770 // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.\r
771 //\r
772 // Look at the file header to determine if relocations have been stripped, and\r
773 // save this info in the image context for later use.\r
774 //\r
775 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
776 if ((Hdr.Te->DataDirectory[0].Size == 0) && (Hdr.Te->DataDirectory[0].VirtualAddress == 0)) {\r
777 return TRUE;\r
778 } else {\r
779 return FALSE;\r
780 }\r
781 } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
782 if ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0) {\r
783 return TRUE;\r
784 } else {\r
785 return FALSE;\r
786 }\r
787 }\r
788\r
789 return FALSE;\r
790}\r
791\r
b1f6a7c6 792/**\r
ed299e3c 793 Routine to load image file for subsequent execution by LoadFile Ppi.\r
54ea99a7 794 If any LoadFile Ppi is not found, the build-in support function for the PE32+/TE\r
ed299e3c 795 XIP image format is used.\r
b1f6a7c6 796\r
ed299e3c
LG
797 @param PeiServices - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
798 @param FileHandle - Pointer to the FFS file header of the image.\r
341a658f 799 @param PeimState - The dispatch state of the input PEIM handle.\r
ed299e3c
LG
800 @param EntryPoint - Pointer to entry point of specified image file for output.\r
801 @param AuthenticationState - Pointer to attestation authentication state of image.\r
b1f6a7c6 802\r
ed299e3c
LG
803 @retval EFI_SUCCESS - Image is successfully loaded.\r
804 @retval EFI_NOT_FOUND - Fail to locate necessary PPI\r
805 @retval Others - Fail to load file.\r
b1f6a7c6 806\r
807**/\r
b0d803fe 808EFI_STATUS\r
809PeiLoadImage (\r
6c7a807a 810 IN CONST EFI_PEI_SERVICES **PeiServices,\r
b0d803fe 811 IN EFI_PEI_FILE_HANDLE FileHandle,\r
341a658f 812 IN UINT8 PeimState,\r
b0d803fe 813 OUT EFI_PHYSICAL_ADDRESS *EntryPoint,\r
814 OUT UINT32 *AuthenticationState\r
815 )\r
b0d803fe 816{\r
817 EFI_STATUS PpiStatus;\r
818 EFI_STATUS Status;\r
819 UINTN Index;\r
820 EFI_PEI_LOAD_FILE_PPI *LoadFile;\r
821 EFI_PHYSICAL_ADDRESS ImageAddress;\r
822 UINT64 ImageSize;\r
341a658f 823 BOOLEAN IsStrip;\r
b0d803fe 824\r
341a658f 825 IsStrip = FALSE;\r
b0d803fe 826 //\r
827 // If any instances of PEI_LOAD_FILE_PPI are installed, they are called.\r
828 // one at a time, until one reports EFI_SUCCESS.\r
829 //\r
830 Index = 0;\r
831 do {\r
832 PpiStatus = PeiServicesLocatePpi (\r
833 &gEfiPeiLoadFilePpiGuid,\r
834 Index,\r
835 NULL,\r
836 (VOID **)&LoadFile\r
837 );\r
838 if (!EFI_ERROR (PpiStatus)) {\r
839 Status = LoadFile->LoadFile (\r
54ea99a7 840 LoadFile,\r
841 FileHandle,\r
842 &ImageAddress,\r
b0d803fe 843 &ImageSize,\r
844 EntryPoint,\r
845 AuthenticationState\r
846 );\r
5d7f3126 847 if (!EFI_ERROR (Status) || Status == EFI_WARN_BUFFER_TOO_SMALL) {\r
341a658f
LG
848 //\r
849 // The shadowed PEIM must be relocatable.\r
850 //\r
851 if (PeimState == PEIM_STATE_REGISITER_FOR_SHADOW) {\r
852 IsStrip = RelocationIsStrip ((VOID *) (UINTN) ImageAddress);\r
853 ASSERT (!IsStrip);\r
854 if (IsStrip) {\r
855 return EFI_UNSUPPORTED;\r
856 }\r
857 }\r
858\r
db0b7ad5
LG
859 //\r
860 // The image to be started must have the machine type supported by PeiCore.\r
861 //\r
862 ASSERT (EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress)));\r
919df8e6 863 if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED (PeCoffLoaderGetMachineType ((VOID *) (UINTN) ImageAddress))) {\r
919df8e6
LG
864 return EFI_UNSUPPORTED;\r
865 }\r
5d7f3126 866 return EFI_SUCCESS;\r
b0d803fe 867 }\r
868 }\r
869 Index++;\r
870 } while (!EFI_ERROR (PpiStatus));\r
871\r
8c519a56 872 return PpiStatus;\r
b0d803fe 873}\r
874\r
875\r
b1f6a7c6 876/**\r
b0d803fe 877\r
ed299e3c
LG
878 Install Pei Load File PPI.\r
879\r
880\r
881 @param PrivateData - Pointer to PEI_CORE_INSTANCE.\r
882 @param OldCoreData - Pointer to PEI_CORE_INSTANCE.\r
b0d803fe 883\r
b1f6a7c6 884**/\r
885VOID\r
886InitializeImageServices (\r
887 IN PEI_CORE_INSTANCE *PrivateData,\r
888 IN PEI_CORE_INSTANCE *OldCoreData\r
889 )\r
b0d803fe 890{\r
b0d803fe 891 if (OldCoreData == NULL) {\r
892 //\r
893 // The first time we are XIP (running from FLASH). We need to remember the\r
894 // FLASH address so we can reinstall the memory version that runs faster\r
895 //\r
896 PrivateData->XipLoadFile = &gPpiLoadFilePpiList;\r
897 PeiServicesInstallPpi (PrivateData->XipLoadFile);\r
898 } else {\r
899 //\r
54ea99a7 900 // 2nd time we are running from memory so replace the XIP version with the\r
901 // new memory version.\r
b0d803fe 902 //\r
54ea99a7 903 PeiServicesReInstallPpi (PrivateData->XipLoadFile, &gPpiLoadFilePpiList);\r
b0d803fe 904 }\r
905}\r
906\r
907\r
908\r
341a658f 909\r