3 Responsibility of this module is to load the DXE Core from a Firmware Volume.
5 Copyright (c) 2006 - 2007 Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Ppi/GuidedSectionExtraction.h>
18 #include <FrameworkPei.h>
21 CustomGuidedSectionExtract (
22 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
*This
,
23 IN CONST VOID
*InputSection
,
24 OUT VOID
**OutputBuffer
,
25 OUT UINTN
*OutputSize
,
26 OUT UINT32
*AuthenticationStatus
33 IN CONST EFI_PEI_DECOMPRESS_PPI
*This
,
34 IN CONST EFI_COMPRESSION_SECTION
*InputSection
,
35 OUT VOID
**OutputBuffer
,
40 BOOLEAN gInMemory
= FALSE
;
43 // Module Globals used in the DXE to PEI handoff
44 // These must be module globals, so the stack can be switched
46 static EFI_DXE_IPL_PPI mDxeIplPpi
= {
50 STATIC EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI mCustomGuidedSectionExtractionPpi
= {
51 CustomGuidedSectionExtract
54 STATIC EFI_PEI_DECOMPRESS_PPI mDecompressPpi
= {
58 static EFI_PEI_PPI_DESCRIPTOR mPpiList
[] = {
60 EFI_PEI_PPI_DESCRIPTOR_PPI
,
65 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
66 &gEfiPeiDecompressPpiGuid
,
71 static EFI_PEI_PPI_DESCRIPTOR mPpiSignal
= {
72 (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
73 &gEfiEndOfPeiSignalPpiGuid
,
78 Initializes the Dxe Ipl PPI
80 @param FfsHandle The handle of FFS file.
81 @param PeiServices General purpose services available to
87 PeimInitializeDxeIpl (
88 IN EFI_PEI_FILE_HANDLE FfsHandle
,
89 IN EFI_PEI_SERVICES
**PeiServices
93 EFI_BOOT_MODE BootMode
;
94 EFI_GUID
*ExtractHandlerGuidTable
;
95 UINTN ExtractHandlerNumber
;
96 EFI_PEI_PPI_DESCRIPTOR
*GuidPpi
;
98 Status
= PeiServicesGetBootMode (&BootMode
);
99 ASSERT_EFI_ERROR (Status
);
101 if (BootMode
!= BOOT_ON_S3_RESUME
) {
102 Status
= PeiServicesRegisterForShadow (FfsHandle
);
103 if (Status
== EFI_SUCCESS
) {
105 // EFI_SUCESS means the first time call register for shadow
108 } else if (Status
== EFI_ALREADY_STARTED
) {
113 // Get custom extract guided section method guid list
115 ExtractHandlerNumber
= ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable
);
118 // Install custom extraction guid ppi
120 if (ExtractHandlerNumber
> 0) {
122 GuidPpi
= (EFI_PEI_PPI_DESCRIPTOR
*) AllocatePool (ExtractHandlerNumber
* sizeof (EFI_PEI_PPI_DESCRIPTOR
));
123 ASSERT (GuidPpi
!= NULL
);
124 while (ExtractHandlerNumber
-- > 0) {
125 GuidPpi
->Flags
= EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
;
126 GuidPpi
->Ppi
= &mCustomGuidedSectionExtractionPpi
;
127 GuidPpi
->Guid
= &(ExtractHandlerGuidTable
[ExtractHandlerNumber
]);
128 Status
= PeiServicesInstallPpi (GuidPpi
++);
129 ASSERT_EFI_ERROR(Status
);
138 // Install FvFileLoader and DxeIpl PPIs.
140 Status
= PeiServicesInstallPpi (mPpiList
);
141 ASSERT_EFI_ERROR(Status
);
147 Main entry point to last PEIM
149 @param This Entry point for DXE IPL PPI
150 @param PeiServices General purpose services available to every PEIM.
151 @param HobList Address to the Pei HOB list
153 @return EFI_SUCCESS DXE core was successfully loaded.
154 @return EFI_OUT_OF_RESOURCES There are not enough resources to load DXE core.
159 IN EFI_DXE_IPL_PPI
*This
,
160 IN EFI_PEI_SERVICES
**PeiServices
,
161 IN EFI_PEI_HOB_POINTERS HobList
165 EFI_GUID DxeCoreFileName
;
166 EFI_PHYSICAL_ADDRESS DxeCoreAddress
;
168 EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint
;
169 EFI_BOOT_MODE BootMode
;
170 EFI_PEI_FV_HANDLE VolumeHandle
;
171 EFI_PEI_FILE_HANDLE FileHandle
;
175 // if in S3 Resume, restore configure
177 Status
= PeiServicesGetBootMode (&BootMode
);
178 ASSERT_EFI_ERROR(Status
);
180 if (BootMode
== BOOT_ON_S3_RESUME
) {
181 Status
= AcpiS3ResumeOs();
182 ASSERT_EFI_ERROR (Status
);
183 } else if (BootMode
== BOOT_IN_RECOVERY_MODE
) {
184 Status
= PeiRecoverFirmware ();
185 if (EFI_ERROR (Status
)) {
186 DEBUG ((EFI_D_ERROR
, "Load Recovery Capsule Failed.(Status = %r)\n", Status
));
191 // Now should have a HOB with the DXE core w/ the old HOB destroyed
196 // If any FV contains an encapsulated FV extract that FV
198 DxeIplAddEncapsulatedFirmwareVolumes ();
201 // Look in all the FVs present in PEI and find the DXE Core
204 Status
= DxeIplFindFirmwareVolumeInstance (&Instance
, EFI_FV_FILETYPE_DXE_CORE
, &VolumeHandle
, &FileHandle
);
205 ASSERT_EFI_ERROR (Status
);
207 CopyMem(&DxeCoreFileName
, &(((EFI_FFS_FILE_HEADER
*)FileHandle
)->Name
), sizeof (EFI_GUID
));
210 // Load the DXE Core from a Firmware Volume, may use LoadFile ppi to do this for save code size.
212 Status
= PeiLoadFile (
219 ASSERT_EFI_ERROR (Status
);
222 // Add HOB for the DXE Core
227 EFI_SIZE_TO_PAGES ((UINT32
) DxeCoreSize
) * EFI_PAGE_SIZE
,
232 // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT
236 EFI_SOFTWARE_PEI_MODULE
| EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT
241 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION PtrPeImage
;
242 PtrPeImage
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*) ((UINTN
) DxeCoreAddress
+ ((EFI_IMAGE_DOS_HEADER
*) (UINTN
) DxeCoreAddress
)->e_lfanew
);
244 if (PtrPeImage
.Pe32
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_IA64
) {
245 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading DXE CORE at 0x%10p EntryPoint=0x%10p\n", (VOID
*)(UINTN
)DxeCoreAddress
, (VOID
*)(UINTN
)DxeCoreEntryPoint
));
248 // For IPF Image, the real entry point should be print.
250 DEBUG ((EFI_D_INFO
| EFI_D_LOAD
, "Loading DXE CORE at 0x%10p EntryPoint=0x%10p\n", (VOID
*)(UINTN
)DxeCoreAddress
, (VOID
*)(UINTN
)(*(UINT64
*)(UINTN
)DxeCoreEntryPoint
)));
255 // Transfer control to the DXE Core
256 // The handoff state is simply a pointer to the HOB list
258 HandOffToDxeCore (DxeCoreEntryPoint
, HobList
, &mPpiSignal
);
260 // If we get here, then the DXE Core returned. This is an error
261 // Dxe Core should not return.
266 return EFI_OUT_OF_RESOURCES
;
273 IN EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
,
274 OUT UINT32
*FvAlignment
278 // Because FvLength in FvHeader is UINT64 type,
279 // so FvHeader must meed at least 8 bytes alignment.
280 // Get the appropriate alignment requirement.
282 if ((FvHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) < EFI_FVB2_ALIGNMENT_8
) {
283 return EFI_UNSUPPORTED
;
286 *FvAlignment
= 1 << ((FvHeader
->Attributes
& EFI_FVB2_ALIGNMENT
) >> 16);
291 Search EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE image and expand
294 @return EFI_OUT_OF_RESOURCES There are no memory space to exstract FV
295 @return EFI_SUCESS Sucess to find the FV
298 DxeIplAddEncapsulatedFirmwareVolumes (
303 EFI_STATUS VolumeStatus
;
305 EFI_FV_INFO VolumeInfo
;
306 EFI_PEI_FV_HANDLE VolumeHandle
;
307 EFI_PEI_FILE_HANDLE FileHandle
;
308 UINT32 SectionLength
;
309 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
310 EFI_FIRMWARE_VOLUME_IMAGE_SECTION
*SectionHeader
;
314 Status
= EFI_NOT_FOUND
;
318 VolumeStatus
= DxeIplFindFirmwareVolumeInstance (
320 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
,
325 if (!EFI_ERROR (VolumeStatus
)) {
326 Status
= PeiServicesFfsFindSectionData (
327 EFI_SECTION_FIRMWARE_VOLUME_IMAGE
,
328 (EFI_FFS_FILE_HEADER
*)FileHandle
,
332 if (!EFI_ERROR (Status
)) {
333 if (FvHeader
->Signature
== EFI_FVH_SIGNATURE
) {
335 // Because FvLength in FvHeader is UINT64 type,
336 // so FvHeader must meed at least 8 bytes alignment.
337 // If current FvImage base address doesn't meet its alignment,
338 // we need to reload this FvImage to another correct memory address.
340 Status
= GetFvAlignment(FvHeader
, &FvAlignment
);
341 if (EFI_ERROR(Status
)) {
344 if (((UINTN
) FvHeader
% FvAlignment
) != 0) {
345 SectionHeader
= (EFI_FIRMWARE_VOLUME_IMAGE_SECTION
*)((UINTN
)FvHeader
- sizeof(EFI_FIRMWARE_VOLUME_IMAGE_SECTION
));
346 SectionLength
= *(UINT32
*)SectionHeader
->Size
& 0x00FFFFFF;
348 DstBuffer
= AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINTN
) SectionLength
- sizeof (EFI_COMMON_SECTION_HEADER
)), FvAlignment
);
349 if (DstBuffer
== NULL
) {
350 return EFI_OUT_OF_RESOURCES
;
352 CopyMem (DstBuffer
, FvHeader
, (UINTN
) SectionLength
- sizeof (EFI_COMMON_SECTION_HEADER
));
353 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*) DstBuffer
;
357 // This new Firmware Volume comes from a firmware file within a firmware volume.
358 // Record the original Firmware Volume Name.
360 PeiServicesFfsGetVolumeInfo (&VolumeHandle
, &VolumeInfo
);
362 PiLibInstallFvInfoPpi (
365 (UINT32
) FvHeader
->FvLength
,
366 &(VolumeInfo
.FvName
),
367 &(((EFI_FFS_FILE_HEADER
*)FileHandle
)->Name
)
371 // Inform HOB consumer phase, i.e. DXE core, the existance of this FV
374 (EFI_PHYSICAL_ADDRESS
) (UINTN
) FvHeader
,
378 ASSERT_EFI_ERROR (Status
);
381 // Makes the encapsulated volume show up in DXE phase to skip processing of
382 // encapsulated file again.
385 (EFI_PHYSICAL_ADDRESS
)(UINTN
)FvHeader
,
388 &(((EFI_FFS_FILE_HEADER
*)FileHandle
)->Name
)
394 } while (!EFI_ERROR (VolumeStatus
));
400 Find the First Volume that contains the first FileType.
402 @param Instance The Fv instance.
403 @param SeachType The type of file to search.
404 @param VolumeHandle Pointer to Fv which contains the file to search.
405 @param FileHandle Pointer to FFS file to search.
407 @return EFI_SUCESS Success to find the FFS in specificed FV
408 @return others Fail to find the FFS in specificed FV
411 DxeIplFindFirmwareVolumeInstance (
412 IN OUT UINTN
*Instance
,
413 IN EFI_FV_FILETYPE SeachType
,
414 OUT EFI_PEI_FV_HANDLE
*VolumeHandle
,
415 OUT EFI_PEI_FILE_HANDLE
*FileHandle
419 EFI_STATUS VolumeStatus
;
422 VolumeStatus
= PeiServicesFfsFindNextVolume (*Instance
, VolumeHandle
);
423 if (!EFI_ERROR (VolumeStatus
)) {
425 Status
= PeiServicesFfsFindNextFile (SeachType
, *VolumeHandle
, FileHandle
);
426 if (!EFI_ERROR (Status
)) {
431 } while (!EFI_ERROR (VolumeStatus
));
437 Loads and relocates a PE/COFF image into memory.
439 @param FileHandle The image file handle
440 @param ImageAddress The base address of the relocated PE/COFF image
441 @param ImageSize The size of the relocated PE/COFF image
442 @param EntryPoint The entry point of the relocated PE/COFF image
444 @return EFI_SUCCESS The file was loaded and relocated
445 @return EFI_OUT_OF_RESOURCES There was not enough memory to load and relocate the PE/COFF file
449 IN EFI_PEI_FILE_HANDLE FileHandle
,
450 OUT EFI_PHYSICAL_ADDRESS
*ImageAddress
,
451 OUT UINT64
*ImageSize
,
452 OUT EFI_PHYSICAL_ADDRESS
*EntryPoint
457 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
460 // First try to find the PE32 section in this ffs file.
462 Status
= PeiServicesFfsFindSectionData (
468 if (EFI_ERROR (Status
)) {
470 // NO image types we support so exit.
475 ZeroMem (&ImageContext
, sizeof (ImageContext
));
476 ImageContext
.Handle
= Pe32Data
;
477 Status
= GetImageReadFunction (&ImageContext
);
479 ASSERT_EFI_ERROR (Status
);
481 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
482 if (EFI_ERROR (Status
)) {
486 // Allocate Memory for the image
488 ImageContext
.ImageAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32
) ImageContext
.ImageSize
));
489 ASSERT (ImageContext
.ImageAddress
!= 0);
492 // Load the image to our new buffer
494 Status
= PeCoffLoaderLoadImage (&ImageContext
);
495 if (EFI_ERROR (Status
)) {
499 // Relocate the image in our new buffer
501 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
502 if (EFI_ERROR (Status
)) {
507 // Flush the instruction cache so the image data is written before we execute it
509 InvalidateInstructionCacheRange ((VOID
*)(UINTN
)ImageContext
.ImageAddress
, (UINTN
)ImageContext
.ImageSize
);
511 *ImageAddress
= ImageContext
.ImageAddress
;
512 *ImageSize
= ImageContext
.ImageSize
;
513 *EntryPoint
= ImageContext
.EntryPoint
;
519 The ExtractSection() function processes the input section and
520 returns a pointer to the section contents. If the section being
521 extracted does not require processing (if the section
522 GuidedSectionHeader.Attributes has the
523 EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then
524 OutputBuffer is just updated to point to the start of the
525 section's contents. Otherwise, *Buffer must be allocated
526 from PEI permanent memory.
528 @param This Indicates the
529 EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance.
530 Buffer containing the input GUIDed section to be
531 processed. OutputBuffer OutputBuffer is
532 allocated from PEI permanent memory and contains
533 the new section stream.
535 @param OutputSize A pointer to a caller-allocated
536 UINTN in which the size of *OutputBuffer
537 allocation is stored. If the function
538 returns anything other than EFI_SUCCESS,
539 the value of OutputSize is undefined.
541 @param AuthenticationStatus A pointer to a caller-allocated
542 UINT32 that indicates the
543 authentication status of the
544 output buffer. If the input
545 section's GuidedSectionHeader.
546 Attributes field has the
547 EFI_GUIDED_SECTION_AUTH_STATUS_VALID
549 AuthenticationStatus must return
550 zero. These bits reflect the
551 status of the extraction
552 operation. If the function
553 returns anything other than
554 EFI_SUCCESS, the value of
555 AuthenticationStatus is
558 @retval EFI_SUCCESS The InputSection was
559 successfully processed and the
560 section contents were returned.
562 @retval EFI_OUT_OF_RESOURCES The system has insufficient
563 resources to process the request.
565 @reteval EFI_INVALID_PARAMETER The GUID in InputSection does
566 not match this instance of the
567 GUIDed Section Extraction PPI.
570 CustomGuidedSectionExtract (
571 IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI
*This
,
572 IN CONST VOID
*InputSection
,
573 OUT VOID
**OutputBuffer
,
574 OUT UINTN
*OutputSize
,
575 OUT UINT32
*AuthenticationStatus
579 UINT8
*ScratchBuffer
;
580 UINT32 ScratchBufferSize
;
581 UINT32 OutputBufferSize
;
582 UINT16 SectionAttribute
;
585 // Init local variable
587 ScratchBuffer
= NULL
;
590 // Call GetInfo to get the size and attribute of input guided section data.
592 Status
= ExtractGuidedSectionGetInfo (
599 if (EFI_ERROR (Status
)) {
600 DEBUG ((EFI_D_ERROR
, "GetInfo from guided section Failed - %r\n", Status
));
604 if (ScratchBufferSize
!= 0) {
606 // Allocate scratch buffer
608 ScratchBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize
));
609 if (ScratchBuffer
== NULL
) {
610 return EFI_OUT_OF_RESOURCES
;
614 if ((SectionAttribute
& EFI_GUIDED_SECTION_PROCESSING_REQUIRED
) && OutputBufferSize
> 0) {
616 // Allocate output buffer
618 *OutputBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (OutputBufferSize
));
619 if (*OutputBuffer
== NULL
) {
620 return EFI_OUT_OF_RESOURCES
;
624 Status
= ExtractGuidedSectionDecode (
631 if (EFI_ERROR (Status
)) {
635 DEBUG ((EFI_D_ERROR
, "Extract guided section Failed - %r\n", Status
));
639 *OutputSize
= (UINTN
) OutputBufferSize
;
648 IN CONST EFI_PEI_DECOMPRESS_PPI
*This
,
649 IN CONST EFI_COMPRESSION_SECTION
*CompressionSection
,
650 OUT VOID
**OutputBuffer
,
651 OUT UINTN
*OutputSize
656 UINT8
*ScratchBuffer
;
658 UINT32 ScratchBufferSize
;
659 EFI_COMMON_SECTION_HEADER
*Section
;
662 if (CompressionSection
->CommonHeader
.Type
!= EFI_SECTION_COMPRESSION
) {
664 return EFI_INVALID_PARAMETER
;
667 Section
= (EFI_COMMON_SECTION_HEADER
*) CompressionSection
;
668 SectionLength
= *(UINT32
*) (Section
->Size
) & 0x00ffffff;
671 // This is a compression set, expand it
673 switch (CompressionSection
->CompressionType
) {
674 case EFI_STANDARD_COMPRESSION
:
676 // Load EFI standard compression.
677 // For compressed data, decompress them to dstbuffer.
679 Status
= UefiDecompressGetInfo (
680 (UINT8
*) ((EFI_COMPRESSION_SECTION
*) Section
+ 1),
681 (UINT32
) SectionLength
- sizeof (EFI_COMPRESSION_SECTION
),
682 (UINT32
*) &DstBufferSize
,
685 if (EFI_ERROR (Status
)) {
689 DEBUG ((EFI_D_ERROR
, "Decompress GetInfo Failed - %r\n", Status
));
690 return EFI_NOT_FOUND
;
693 // Allocate scratch buffer
695 ScratchBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize
));
696 if (ScratchBuffer
== NULL
) {
697 return EFI_OUT_OF_RESOURCES
;
700 // Allocate destination buffer
702 DstBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize
));
703 if (DstBuffer
== NULL
) {
704 return EFI_OUT_OF_RESOURCES
;
707 // Call decompress function
709 Status
= UefiDecompress (
710 (CHAR8
*) ((EFI_COMPRESSION_SECTION
*) Section
+ 1),
714 if (EFI_ERROR (Status
)) {
718 DEBUG ((EFI_D_ERROR
, "Decompress Failed - %r\n", Status
));
719 return EFI_NOT_FOUND
;
723 // porting note the original branch for customized compress is removed, it should be change to use GUID compress
725 case EFI_NOT_COMPRESSED
:
727 // Allocate destination buffer
729 DstBufferSize
= CompressionSection
->UncompressedLength
;
730 DstBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize
));
731 if (DstBuffer
== NULL
) {
732 return EFI_OUT_OF_RESOURCES
;
735 // stream is not actually compressed, just encapsulated. So just copy it.
737 CopyMem (DstBuffer
, CompressionSection
+ 1, DstBufferSize
);
742 // Don't support other unknown compression type.
745 return EFI_NOT_FOUND
;
748 *OutputSize
= DstBufferSize
;
749 *OutputBuffer
= DstBuffer
;
756 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
760 EFI_PEI_HOB_POINTERS Hob
;
762 Hob
.Raw
= GetHobList ();
763 while ((Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
)) != NULL
) {
764 if (CompareGuid (&gEfiHobMemoryAllocStackGuid
, &(Hob
.MemoryAllocationStack
->AllocDescriptor
.Name
))) {
766 // Build a new memory allocation HOB with old stack info with EfiConventionalMemory type
767 // to be reclaimed by DXE core.
769 BuildMemoryAllocationHob (
770 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
,
771 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryLength
,
772 EfiConventionalMemory
775 // Update the BSP Stack Hob to reflect the new stack info.
777 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
778 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryLength
= Length
;
781 Hob
.Raw
= GET_NEXT_HOB (Hob
);