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