X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FCore%2FDxeIplPeim%2FDxeLoad.c;h=c6e5b8330927ed093cac6222b1591160cbf0b989;hp=b749fb18c15aa66e8fee360b596ea404a42502f1;hb=9d510e61fceee7b92955ef9a3c20343752d8ce3f;hpb=3cfb790c9499ef62eb8908255f5d7648f2de8d28 diff --git a/MdeModulePkg/Core/DxeIplPeim/DxeLoad.c b/MdeModulePkg/Core/DxeIplPeim/DxeLoad.c index b749fb18c1..c6e5b83309 100644 --- a/MdeModulePkg/Core/DxeIplPeim/DxeLoad.c +++ b/MdeModulePkg/Core/DxeIplPeim/DxeLoad.c @@ -1,306 +1,427 @@ -/*++ - -Copyright (c) 2006, Intel Corporation -All rights reserved. This program and the accompanying materials -are licensed and made available under the terms and conditions of the BSD License -which accompanies this distribution. The full text of the license may be found at -http://opensource.org/licenses/bsd-license.php - -THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. - -Module Name: - - DxeLoad.c - -Abstract: - +/** @file Last PEIM. Responsibility of this module is to load the DXE Core from a Firmware Volume. ---*/ +Copyright (c) 2016 HP Development Company, L.P. +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent -#include "DxeIpl.h" +**/ -// porting note remove later -#include "DecompressLibrary.h" -#include "FrameworkPei.h" -// end of remove later +#include "DxeIpl.h" -BOOLEAN gInMemory = FALSE; // -// Module Globals used in the DXE to PEI handoff +// Module Globals used in the DXE to PEI hand off // These must be module globals, so the stack can be switched // -static EFI_DXE_IPL_PPI mDxeIplPpi = { +CONST EFI_DXE_IPL_PPI mDxeIplPpi = { DxeLoadCore }; -static EFI_PEI_FV_FILE_LOADER_PPI mLoadFilePpi = { - DxeIplLoadFile +CONST EFI_PEI_PPI_DESCRIPTOR mDxeIplPpiList = { + EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST, + &gEfiDxeIplPpiGuid, + (VOID *) &mDxeIplPpi +}; + +CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI mCustomGuidedSectionExtractionPpi = { + CustomGuidedSectionExtract }; -static EFI_PEI_PPI_DESCRIPTOR mPpiList[] = { - { - EFI_PEI_PPI_DESCRIPTOR_PPI, - &gEfiPeiFvFileLoaderPpiGuid, - &mLoadFilePpi - }, - { +CONST EFI_PEI_DECOMPRESS_PPI mDecompressPpi = { + Decompress +}; + +CONST EFI_PEI_PPI_DESCRIPTOR mDecompressPpiList = { (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST), - &gEfiDxeIplPpiGuid, - &mDxeIplPpi - } + &gEfiPeiDecompressPpiGuid, + (VOID *) &mDecompressPpi }; -static EFI_PEI_PPI_DESCRIPTOR mPpiSignal = { +CONST EFI_PEI_PPI_DESCRIPTOR gEndOfPeiSignalPpi = { (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST), &gEfiEndOfPeiSignalPpiGuid, NULL }; -GLOBAL_REMOVE_IF_UNREFERENCED DECOMPRESS_LIBRARY gEfiDecompress = { - UefiDecompressGetInfo, - UefiDecompress +CONST EFI_PEI_NOTIFY_DESCRIPTOR mMemoryDiscoveredNotifyList = { + (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST), + &gEfiPeiMemoryDiscoveredPpiGuid, + InstallIplPermanentMemoryPpis }; -GLOBAL_REMOVE_IF_UNREFERENCED DECOMPRESS_LIBRARY gCustomDecompress = { - CustomDecompressGetInfo, - CustomDecompress -}; +/** + Entry point of DXE IPL PEIM. + + This function installs DXE IPL PPI. It also reloads + itself to memory on non-S3 resume boot path. + @param FileHandle Handle of the file being invoked. + @param PeiServices Describes the list of possible PEI Services. + + @retval EFI_SUCESS The entry point of DXE IPL PEIM executes successfully. + @retval Others Some error occurs during the execution of this function. + +**/ EFI_STATUS EFIAPI PeimInitializeDxeIpl ( - IN EFI_FFS_FILE_HEADER *FfsHeader, - IN EFI_PEI_SERVICES **PeiServices + IN EFI_PEI_FILE_HANDLE FileHandle, + IN CONST EFI_PEI_SERVICES **PeiServices ) -/*++ - -Routine Description: - - Initializes the Dxe Ipl PPI - -Arguments: - - FfsHeader - Pointer to FFS file header - PeiServices - General purpose services available to every PEIM. - -Returns: - - EFI_SUCCESS - ---*/ { EFI_STATUS Status; - EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader; EFI_BOOT_MODE BootMode; + VOID *Dummy; - Status = PeiServicesGetBootMode (&BootMode); - ASSERT_EFI_ERROR (Status); + BootMode = GetBootModeHob (); + + if (BootMode != BOOT_ON_S3_RESUME) { + Status = PeiServicesRegisterForShadow (FileHandle); + if (Status == EFI_SUCCESS) { + // + // EFI_SUCESS means it is the first time to call register for shadow. + // + return Status; + } - if (!gInMemory && (BootMode != BOOT_ON_S3_RESUME)) { // - // The DxeIpl has not yet been shadowed + // Ensure that DXE IPL is shadowed to permanent memory. // - PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol (); + ASSERT (Status == EFI_ALREADY_STARTED); // - // Shadow DxeIpl and then re-run its entry point + // DXE core load requires permanent memory. // - Status = ShadowDxeIpl (FfsHeader, PeiEfiPeiPeCoffLoader); + Status = PeiServicesLocatePpi ( + &gEfiPeiMemoryDiscoveredPpiGuid, + 0, + NULL, + (VOID **) &Dummy + ); + ASSERT_EFI_ERROR (Status); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Now the permanent memory exists, install the PPIs for decompression + // and section extraction. + // + Status = InstallIplPermanentMemoryPpis (NULL, NULL, NULL); + ASSERT_EFI_ERROR (Status); } else { // - // Install FvFileLoader and DxeIpl PPIs. + // Install memory discovered PPI notification to install PPIs for + // decompression and section extraction. // - Status = PeiServicesInstallPpi (mPpiList); - ASSERT_EFI_ERROR(Status); + Status = PeiServicesNotifyPpi (&mMemoryDiscoveredNotifyList); + ASSERT_EFI_ERROR (Status); } - + + // + // Install DxeIpl PPI. + // + Status = PeiServicesInstallPpi (&mDxeIplPpiList); + ASSERT_EFI_ERROR(Status); + return Status; } +/** + This function installs the PPIs that require permanent memory. + + @param PeiServices Indirect reference to the PEI Services Table. + @param NotifyDescriptor Address of the notification descriptor data structure. + @param Ppi Address of the PPI that was installed. + + @return EFI_SUCCESS The PPIs were installed successfully. + @return Others Some error occurs during the execution of this function. + +**/ EFI_STATUS EFIAPI -DxeLoadCore ( - IN EFI_DXE_IPL_PPI *This, - IN EFI_PEI_SERVICES **PeiServices, - IN EFI_PEI_HOB_POINTERS HobList +InstallIplPermanentMemoryPpis ( + IN EFI_PEI_SERVICES **PeiServices, + IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor, + IN VOID *Ppi ) -/*++ +{ + EFI_STATUS Status; + EFI_GUID *ExtractHandlerGuidTable; + UINTN ExtractHandlerNumber; + EFI_PEI_PPI_DESCRIPTOR *GuidPpi; + + // + // Get custom extract guided section method guid list + // + ExtractHandlerNumber = ExtractGuidedSectionGetGuidList (&ExtractHandlerGuidTable); + + // + // Install custom guided section extraction PPI + // + if (ExtractHandlerNumber > 0) { + GuidPpi = (EFI_PEI_PPI_DESCRIPTOR *) AllocatePool (ExtractHandlerNumber * sizeof (EFI_PEI_PPI_DESCRIPTOR)); + ASSERT (GuidPpi != NULL); + while (ExtractHandlerNumber-- > 0) { + GuidPpi->Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST; + GuidPpi->Ppi = (VOID *) &mCustomGuidedSectionExtractionPpi; + GuidPpi->Guid = &ExtractHandlerGuidTable[ExtractHandlerNumber]; + Status = PeiServicesInstallPpi (GuidPpi++); + ASSERT_EFI_ERROR(Status); + } + } -Routine Description: + // + // Install Decompress PPI. + // + Status = PeiServicesInstallPpi (&mDecompressPpiList); + ASSERT_EFI_ERROR(Status); - Main entry point to last PEIM + return Status; +} -Arguments: - This - Entry point for DXE IPL PPI - PeiServices - General purpose services available to every PEIM. - HobList - Address to the Pei HOB list +/** + Validate variable data for the MemoryTypeInformation. -Returns: + @param MemoryData Variable data. + @param MemoryDataSize Variable data length. - EFI_SUCCESS - DEX core was successfully loaded. - EFI_OUT_OF_RESOURCES - There are not enough resources to load DXE core. + @return TRUE The variable data is valid. + @return FALSE The variable data is invalid. ---*/ +**/ +BOOLEAN +ValidateMemoryTypeInfoVariable ( + IN EFI_MEMORY_TYPE_INFORMATION *MemoryData, + IN UINTN MemoryDataSize + ) +{ + UINTN Count; + UINTN Index; + + // Check the input parameter. + if (MemoryData == NULL) { + return FALSE; + } + + // Get Count + Count = MemoryDataSize / sizeof (*MemoryData); + + // Check Size + if (Count * sizeof(*MemoryData) != MemoryDataSize) { + return FALSE; + } + + // Check last entry type filed. + if (MemoryData[Count - 1].Type != EfiMaxMemoryType) { + return FALSE; + } + + // Check the type filed. + for (Index = 0; Index < Count - 1; Index++) { + if (MemoryData[Index].Type >= EfiMaxMemoryType) { + return FALSE; + } + } + + return TRUE; +} + +/** + Main entry point to last PEIM. + + This function finds DXE Core in the firmware volume and transfer the control to + DXE core. + + @param This Entry point for DXE IPL PPI. + @param PeiServices General purpose services available to every PEIM. + @param HobList Address to the Pei HOB list. + + @return EFI_SUCCESS DXE core was successfully loaded. + @return EFI_OUT_OF_RESOURCES There are not enough resources to load DXE core. + +**/ +EFI_STATUS +EFIAPI +DxeLoadCore ( + IN CONST EFI_DXE_IPL_PPI *This, + IN EFI_PEI_SERVICES **PeiServices, + IN EFI_PEI_HOB_POINTERS HobList + ) { EFI_STATUS Status; - EFI_GUID DxeCoreFileName; - EFI_GUID FirmwareFileName; - VOID *Pe32Data; - VOID *FvImageData; + EFI_FV_FILE_INFO DxeCoreFileInfo; EFI_PHYSICAL_ADDRESS DxeCoreAddress; UINT64 DxeCoreSize; EFI_PHYSICAL_ADDRESS DxeCoreEntryPoint; - EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader; EFI_BOOT_MODE BootMode; + EFI_PEI_FILE_HANDLE FileHandle; + EFI_PEI_READ_ONLY_VARIABLE2_PPI *Variable; + EFI_PEI_LOAD_FILE_PPI *LoadFile; + UINTN Instance; + UINT32 AuthenticationState; + UINTN DataSize; + EFI_PEI_S3_RESUME2_PPI *S3Resume; EFI_PEI_RECOVERY_MODULE_PPI *PeiRecovery; - EFI_PEI_S3_RESUME_PPI *S3Resume; - -// PERF_START (PeiServices, L"DxeIpl", NULL, 0); + EFI_MEMORY_TYPE_INFORMATION MemoryData[EfiMaxMemoryType + 1]; // // if in S3 Resume, restore configure // - Status = PeiServicesGetBootMode (&BootMode); - ASSERT_EFI_ERROR(Status); + BootMode = GetBootModeHob (); if (BootMode == BOOT_ON_S3_RESUME) { Status = PeiServicesLocatePpi ( - &gEfiPeiS3ResumePpiGuid, + &gEfiPeiS3Resume2PpiGuid, 0, NULL, - (VOID **)&S3Resume + (VOID **) &S3Resume ); + if (EFI_ERROR (Status)) { + // + // Report Status code that S3Resume PPI can not be found + // + REPORT_STATUS_CODE ( + EFI_ERROR_CODE | EFI_ERROR_MAJOR, + (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_S3_RESUME_PPI_NOT_FOUND) + ); + } ASSERT_EFI_ERROR (Status); - Status = S3Resume->S3RestoreConfig (PeiServices); + Status = S3Resume->S3RestoreConfig2 (S3Resume); ASSERT_EFI_ERROR (Status); } else if (BootMode == BOOT_IN_RECOVERY_MODE) { - + REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_RECOVERY_BEGIN)); Status = PeiServicesLocatePpi ( &gEfiPeiRecoveryModulePpiGuid, 0, NULL, - (VOID **)&PeiRecovery + (VOID **) &PeiRecovery ); - ASSERT_EFI_ERROR (Status); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Locate Recovery PPI Failed.(Status = %r)\n", Status)); + // + // Report Status code the failure of locating Recovery PPI + // + REPORT_STATUS_CODE ( + EFI_ERROR_CODE | EFI_ERROR_MAJOR, + (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_RECOVERY_PPI_NOT_FOUND) + ); + CpuDeadLoop (); + } + + REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_CAPSULE_LOAD)); Status = PeiRecovery->LoadRecoveryCapsule (PeiServices, PeiRecovery); if (EFI_ERROR (Status)) { - DEBUG ((EFI_D_ERROR, "Load Recovery Capsule Failed.(Status = %r)\n", Status)); + DEBUG ((DEBUG_ERROR, "Load Recovery Capsule Failed.(Status = %r)\n", Status)); + // + // Report Status code that recovery image can not be found + // + REPORT_STATUS_CODE ( + EFI_ERROR_CODE | EFI_ERROR_MAJOR, + (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_NO_RECOVERY_CAPSULE) + ); CpuDeadLoop (); } + REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_CAPSULE_START)); + // + // Now should have a HOB with the DXE core + // + } + if (GetFirstGuidHob ((CONST EFI_GUID *)&gEfiMemoryTypeInformationGuid) == NULL) { // - // Now should have a HOB with the DXE core w/ the old HOB destroyed + // Don't build GuidHob if GuidHob has been installed. // + Status = PeiServicesLocatePpi ( + &gEfiPeiReadOnlyVariable2PpiGuid, + 0, + NULL, + (VOID **)&Variable + ); + if (!EFI_ERROR (Status)) { + DataSize = sizeof (MemoryData); + Status = Variable->GetVariable ( + Variable, + EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME, + &gEfiMemoryTypeInformationGuid, + NULL, + &DataSize, + &MemoryData + ); + if (!EFI_ERROR (Status) && ValidateMemoryTypeInfoVariable(MemoryData, DataSize)) { + // + // Build the GUID'd HOB for DXE + // + BuildGuidDataHob ( + &gEfiMemoryTypeInformationGuid, + MemoryData, + DataSize + ); + } + } } // - // Install the PEI Protocols that are shared between PEI and DXE + // Look in all the FVs present in PEI and find the DXE Core FileHandle // - PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol (); - ASSERT (PeiEfiPeiPeCoffLoader != NULL); - + FileHandle = DxeIplFindDxeCore (); // - // Find the EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE type compressed Firmware Volume file - // The file found will be processed by PeiProcessFile: It will first be decompressed to - // a normal FV, then a corresponding FV type hob will be built. + // Load the DXE Core from a Firmware Volume. // - Status = PeiFindFile ( - EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, - EFI_SECTION_FIRMWARE_VOLUME_IMAGE, - &FirmwareFileName, - &FvImageData - ); + Instance = 0; + do { + Status = PeiServicesLocatePpi (&gEfiPeiLoadFilePpiGuid, Instance++, NULL, (VOID **) &LoadFile); + // + // These must exist an instance of EFI_PEI_LOAD_FILE_PPI to support to load DxeCore file handle successfully. + // + ASSERT_EFI_ERROR (Status); - // - // Find the DXE Core in a Firmware Volume - // - Status = PeiFindFile ( - EFI_FV_FILETYPE_DXE_CORE, - EFI_SECTION_PE32, - &DxeCoreFileName, - &Pe32Data - ); - ASSERT_EFI_ERROR (Status); + Status = LoadFile->LoadFile ( + LoadFile, + FileHandle, + &DxeCoreAddress, + &DxeCoreSize, + &DxeCoreEntryPoint, + &AuthenticationState + ); + } while (EFI_ERROR (Status)); // - // Load the DXE Core from a Firmware Volume + // Get the DxeCore File Info from the FileHandle for the DxeCore GUID file name. // - Status = PeiLoadFile ( - PeiEfiPeiPeCoffLoader, - Pe32Data, - &DxeCoreAddress, - &DxeCoreSize, - &DxeCoreEntryPoint - ); + Status = PeiServicesFfsGetFileInfo (FileHandle, &DxeCoreFileInfo); ASSERT_EFI_ERROR (Status); // // Add HOB for the DXE Core // BuildModuleHob ( - &DxeCoreFileName, + &DxeCoreFileInfo.FileName, DxeCoreAddress, - DxeCoreSize, + ALIGN_VALUE (DxeCoreSize, EFI_PAGE_SIZE), DxeCoreEntryPoint ); // // Report Status Code EFI_SW_PEI_PC_HANDOFF_TO_NEXT // - REPORT_STATUS_CODE ( - EFI_PROGRESS_CODE, - EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT - ); + REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_CORE | EFI_SW_PEI_CORE_PC_HANDOFF_TO_NEXT)); - if (FeaturePcdGet (PcdDxeIplBuildShareCodeHobs)) { - if (FeaturePcdGet (PcdDxeIplSupportEfiDecompress)) { - // - // Add HOB for the EFI Decompress Protocol - // - BuildGuidDataHob ( - &gEfiDecompressProtocolGuid, - (VOID *)&gEfiDecompress, - sizeof (gEfiDecompress) - ); - } - if (FeaturePcdGet (PcdDxeIplSupportCustomDecompress)) { - // - // Add HOB for the user customized Decompress Protocol - // - BuildGuidDataHob ( - &gEfiCustomizedDecompressProtocolGuid, - (VOID *)&gCustomDecompress, - sizeof (gCustomDecompress) - ); - } - - // - // Add HOB for the PE/COFF Loader Protocol - // - BuildGuidDataHob ( - &gEfiPeiPeCoffLoaderGuid, - (VOID *)&PeiEfiPeiPeCoffLoader, - sizeof (VOID *) - ); - } + DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Loading DXE CORE at 0x%11p EntryPoint=0x%11p\n", (VOID *)(UINTN)DxeCoreAddress, FUNCTION_ENTRY_POINT (DxeCoreEntryPoint))); // // Transfer control to the DXE Core - // The handoff state is simply a pointer to the HOB list + // The hand off state is simply a pointer to the HOB list // - - DEBUG ((EFI_D_INFO, "DXE Core Entry Point 0x%08x\n", (UINTN) DxeCoreEntryPoint)); - HandOffToDxeCore (DxeCoreEntryPoint, HobList, &mPpiSignal); + HandOffToDxeCore (DxeCoreEntryPoint, HobList); // // If we get here, then the DXE Core returned. This is an error - // Dxe Core should not return. + // DxeCore should not return. // ASSERT (FALSE); CpuDeadLoop (); @@ -308,668 +429,379 @@ Returns: return EFI_OUT_OF_RESOURCES; } -EFI_STATUS -PeiFindFile ( - IN UINT8 Type, - IN UINT16 SectionType, - OUT EFI_GUID *FileName, - OUT VOID **Pe32Data - ) -/*++ - -Routine Description: - - Finds a PE/COFF of a specific Type and SectionType in the Firmware Volumes - described in the HOB list. Able to search in a compression set in a FFS file. - But only one level of compression is supported, that is, not able to search - in a compression set that is within another compression set. - -Arguments: - - Type - The Type of file to retrieve - - SectionType - The type of section to retrieve from a file - FileName - The name of the file found in the Firmware Volume +/** + Searches DxeCore in all firmware Volumes and loads the first + instance that contains DxeCore. - Pe32Data - Pointer to the beginning of the PE/COFF file found in the Firmware Volume + @return FileHandle of DxeCore to load DxeCore. -Returns: - - EFI_SUCCESS - The file was found, and the name is returned in FileName, and a pointer to - the PE/COFF image is returned in Pe32Data - - EFI_NOT_FOUND - The file was not found in the Firmware Volumes present in the HOB List - ---*/ +**/ +EFI_PEI_FILE_HANDLE +DxeIplFindDxeCore ( + VOID + ) { - EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader; - EFI_FFS_FILE_HEADER *FfsFileHeader; - EFI_STATUS Status; - EFI_PEI_HOB_POINTERS Hob; - + EFI_STATUS Status; + UINTN Instance; + EFI_PEI_FV_HANDLE VolumeHandle; + EFI_PEI_FILE_HANDLE FileHandle; - FwVolHeader = NULL; - FfsFileHeader = NULL; - Status = EFI_SUCCESS; - - // - // For each Firmware Volume, look for a specified type - // of file and break out until no one is found - // - Hob.Raw = GetHobList (); - while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_FV, Hob.Raw)) != NULL) { - FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (Hob.FirmwareVolume->BaseAddress); + Instance = 0; + while (TRUE) { + // + // Traverse all firmware volume instances // - // Make sure the FV HOB does not get corrupted. + Status = PeiServicesFfsFindNextVolume (Instance, &VolumeHandle); // - ASSERT (FwVolHeader->Signature == EFI_FVH_SIGNATURE); + // If some error occurs here, then we cannot find any firmware + // volume that may contain DxeCore. + // + if (EFI_ERROR (Status)) { + REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_CORE_EC_DXE_CORRUPT)); + } + ASSERT_EFI_ERROR (Status); - Status = PeiServicesFfsFindNextFile ( - Type, - FwVolHeader, - &FfsFileHeader - ); + // + // Find the DxeCore file type from the beginning in this firmware volume. + // + FileHandle = NULL; + Status = PeiServicesFfsFindNextFile (EFI_FV_FILETYPE_DXE_CORE, VolumeHandle, &FileHandle); if (!EFI_ERROR (Status)) { - Status = PeiProcessFile ( - SectionType, - FfsFileHeader, - Pe32Data, - &Hob - ); - CopyMem (FileName, &FfsFileHeader->Name, sizeof (EFI_GUID)); // - // Find all Fv type ffs to get all FvImage and add them into FvHob + // Find DxeCore FileHandle in this volume, then we skip other firmware volume and + // return the FileHandle. // - if (!EFI_ERROR (Status) && (Type != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE)) { - return EFI_SUCCESS; - } + return FileHandle; } - Hob.Raw = GET_NEXT_HOB (Hob); + // + // We cannot find DxeCore in this firmware volume, then search the next volume. + // + Instance++; } - return EFI_NOT_FOUND; } -EFI_STATUS -PeiLoadFile ( - IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader, - IN VOID *Pe32Data, - OUT EFI_PHYSICAL_ADDRESS *ImageAddress, - OUT UINT64 *ImageSize, - OUT EFI_PHYSICAL_ADDRESS *EntryPoint - ) -/*++ - -Routine Description: - - Loads and relocates a PE/COFF image into memory. -Arguments: - PeiEfiPeiPeCoffLoader - Pointer to a PE COFF loader protocol - - Pe32Data - The base address of the PE/COFF file that is to be loaded and relocated - - ImageAddress - The base address of the relocated PE/COFF image - - ImageSize - The size of the relocated PE/COFF image - - EntryPoint - The entry point of the relocated PE/COFF image - -Returns: - - EFI_SUCCESS - The file was loaded and relocated - - EFI_OUT_OF_RESOURCES - There was not enough memory to load and relocate the PE/COFF file - ---*/ +/** + The ExtractSection() function processes the input section and + returns a pointer to the section contents. If the section being + extracted does not require processing (if the section + GuidedSectionHeader.Attributes has the + EFI_GUIDED_SECTION_PROCESSING_REQUIRED field cleared), then + OutputBuffer is just updated to point to the start of the + section's contents. Otherwise, *Buffer must be allocated + from PEI permanent memory. + + @param This Indicates the + EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI instance. + Buffer containing the input GUIDed section to be + processed. OutputBuffer OutputBuffer is + allocated from PEI permanent memory and contains + the new section stream. + @param InputSection A pointer to the input buffer, which contains + the input section to be processed. + @param OutputBuffer A pointer to a caller-allocated buffer, whose + size is specified by the contents of OutputSize. + @param OutputSize A pointer to a caller-allocated + UINTN in which the size of *OutputBuffer + allocation is stored. If the function + returns anything other than EFI_SUCCESS, + the value of OutputSize is undefined. + @param AuthenticationStatus A pointer to a caller-allocated + UINT32 that indicates the + authentication status of the + output buffer. If the input + section's GuidedSectionHeader. + Attributes field has the + EFI_GUIDED_SECTION_AUTH_STATUS_VALID + bit as clear, + AuthenticationStatus must return + zero. These bits reflect the + status of the extraction + operation. If the function + returns anything other than + EFI_SUCCESS, the value of + AuthenticationStatus is + undefined. + + @retval EFI_SUCCESS The InputSection was + successfully processed and the + section contents were returned. + + @retval EFI_OUT_OF_RESOURCES The system has insufficient + resources to process the request. + + @retval EFI_INVALID_PARAMETER The GUID in InputSection does + not match this instance of the + GUIDed Section Extraction PPI. + +**/ +EFI_STATUS +EFIAPI +CustomGuidedSectionExtract ( + IN CONST EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *This, + IN CONST VOID *InputSection, + OUT VOID **OutputBuffer, + OUT UINTN *OutputSize, + OUT UINT32 *AuthenticationStatus +) { - EFI_STATUS Status; - PE_COFF_LOADER_IMAGE_CONTEXT ImageContext; + EFI_STATUS Status; + UINT8 *ScratchBuffer; + UINT32 ScratchBufferSize; + UINT32 OutputBufferSize; + UINT16 SectionAttribute; - ZeroMem (&ImageContext, sizeof (ImageContext)); - ImageContext.Handle = Pe32Data; - Status = GetImageReadFunction (&ImageContext); - - ASSERT_EFI_ERROR (Status); - - Status = PeiEfiPeiPeCoffLoader->GetImageInfo (PeiEfiPeiPeCoffLoader, &ImageContext); - if (EFI_ERROR (Status)) { - return Status; - } // - // Allocate Memory for the image + // Init local variable // - ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) AllocatePages (EFI_SIZE_TO_PAGES ((UINT32) ImageContext.ImageSize)); - ASSERT (ImageContext.ImageAddress != 0); + ScratchBuffer = NULL; // - // Load the image to our new buffer - // - Status = PeiEfiPeiPeCoffLoader->LoadImage (PeiEfiPeiPeCoffLoader, &ImageContext); - if (EFI_ERROR (Status)) { - return Status; - } - // - // Relocate the image in our new buffer + // Call GetInfo to get the size and attribute of input guided section data. // - Status = PeiEfiPeiPeCoffLoader->RelocateImage (PeiEfiPeiPeCoffLoader, &ImageContext); + Status = ExtractGuidedSectionGetInfo ( + InputSection, + &OutputBufferSize, + &ScratchBufferSize, + &SectionAttribute + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "GetInfo from guided section Failed - %r\n", Status)); return Status; } - // - // Flush the instruction cache so the image data is written before we execute it - // - InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize); - - *ImageAddress = ImageContext.ImageAddress; - *ImageSize = ImageContext.ImageSize; - *EntryPoint = ImageContext.EntryPoint; - - return EFI_SUCCESS; -} - -EFI_STATUS -ShadowDxeIpl ( - IN EFI_FFS_FILE_HEADER *DxeIplFileHeader, - IN EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader - ) -/*++ - -Routine Description: - - Shadow the DXE IPL to a different memory location. This occurs after permanent - memory has been discovered. - -Arguments: - - DxeIplFileHeader - Pointer to the FFS file header of the DXE IPL driver - - PeiEfiPeiPeCoffLoader - Pointer to a PE COFF loader protocol - -Returns: - - EFI_SUCCESS - DXE IPL was successfully shadowed to a different memory location. - - EFI_ ERROR - The shadow was unsuccessful. - - ---*/ -{ - UINTN SectionLength; - UINTN OccupiedSectionLength; - EFI_PHYSICAL_ADDRESS DxeIplAddress; - UINT64 DxeIplSize; - EFI_PHYSICAL_ADDRESS DxeIplEntryPoint; - EFI_STATUS Status; - EFI_COMMON_SECTION_HEADER *Section; - - Section = (EFI_COMMON_SECTION_HEADER *) (DxeIplFileHeader + 1); - - while ((Section->Type != EFI_SECTION_PE32) && (Section->Type != EFI_SECTION_TE)) { - SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff; - OccupiedSectionLength = GET_OCCUPIED_SIZE (SectionLength, 4); - Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + OccupiedSectionLength); + if (ScratchBufferSize != 0) { + // + // Allocate scratch buffer + // + ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize)); + if (ScratchBuffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } } - // - // Relocate DxeIpl into memory by using loadfile service - // - Status = PeiLoadFile ( - PeiEfiPeiPeCoffLoader, - (VOID *) (Section + 1), - &DxeIplAddress, - &DxeIplSize, - &DxeIplEntryPoint - ); - if (Status == EFI_SUCCESS) { + if (((SectionAttribute & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) != 0) && OutputBufferSize > 0) { // - // Set gInMemory global variable to TRUE to indicate the dxeipl is shadowed. + // Allocate output buffer // - *(BOOLEAN *) ((UINTN) &gInMemory + (UINTN) DxeIplEntryPoint - (UINTN) _ModuleEntryPoint) = TRUE; - Status = ((EFI_PEIM_ENTRY_POINT) (UINTN) DxeIplEntryPoint) ((EFI_PEI_FILE_HANDLE *) DxeIplFileHeader, GetPeiServicesTablePointer()); + *OutputBuffer = AllocatePages (EFI_SIZE_TO_PAGES (OutputBufferSize)); + if (*OutputBuffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + DEBUG ((DEBUG_INFO, "Customized Guided section Memory Size required is 0x%x and address is 0x%p\n", OutputBufferSize, *OutputBuffer)); } - return Status; -} - -EFI_STATUS -EFIAPI -DxeIplLoadFile ( - IN EFI_PEI_FV_FILE_LOADER_PPI *This, - IN EFI_FFS_FILE_HEADER *FfsHeader, - OUT EFI_PHYSICAL_ADDRESS *ImageAddress, - OUT UINT64 *ImageSize, - OUT EFI_PHYSICAL_ADDRESS *EntryPoint - ) -/*++ - -Routine Description: - - Given a pointer to an FFS file containing a PE32 image, get the - information on the PE32 image, and then "load" it so that it - can be executed. - -Arguments: - - This - pointer to our file loader protocol - - FfsHeader - pointer to the FFS file header of the FFS file that - contains the PE32 image we want to load - - ImageAddress - returned address where the PE32 image is loaded - - ImageSize - returned size of the loaded PE32 image - - EntryPoint - entry point to the loaded PE32 image - -Returns: - - EFI_SUCCESS - The FFS file was successfully loaded. - - EFI_ERROR - Unable to load the FFS file. - ---*/ -{ - EFI_PEI_PE_COFF_LOADER_PROTOCOL *PeiEfiPeiPeCoffLoader; - EFI_STATUS Status; - VOID *Pe32Data; - - Pe32Data = NULL; - PeiEfiPeiPeCoffLoader = (EFI_PEI_PE_COFF_LOADER_PROTOCOL *)GetPeCoffLoaderProtocol (); - - // - // Preprocess the FFS file to get a pointer to the PE32 information - // in the enclosed PE32 image. - // - Status = PeiProcessFile ( - EFI_SECTION_PE32, - FfsHeader, - &Pe32Data, - NULL - ); - + Status = ExtractGuidedSectionDecode ( + InputSection, + OutputBuffer, + ScratchBuffer, + AuthenticationStatus + ); if (EFI_ERROR (Status)) { + // + // Decode failed + // + DEBUG ((DEBUG_ERROR, "Extract guided section Failed - %r\n", Status)); return Status; } - // - // Load the PE image from the FFS file - // - Status = PeiLoadFile ( - PeiEfiPeiPeCoffLoader, - Pe32Data, - ImageAddress, - ImageSize, - EntryPoint - ); - return Status; -} - -EFI_STATUS -PeiProcessFile ( - IN UINT16 SectionType, - IN EFI_FFS_FILE_HEADER *FfsFileHeader, - OUT VOID **Pe32Data, - IN EFI_PEI_HOB_POINTERS *OrigHob - ) -/*++ + *OutputSize = (UINTN) OutputBufferSize; -Routine Description: + return EFI_SUCCESS; +} -Arguments: - SectionType - The type of section in the FFS file to process. - FfsFileHeader - Pointer to the FFS file to process, looking for the - specified SectionType +/** + Decompresses a section to the output buffer. - Pe32Data - returned pointer to the start of the PE32 image found - in the FFS file. + This function looks up the compression type field in the input section and + applies the appropriate compression algorithm to compress the section to a + callee allocated buffer. -Returns: + @param This Points to this instance of the + EFI_PEI_DECOMPRESS_PEI PPI. + @param CompressionSection Points to the compressed section. + @param OutputBuffer Holds the returned pointer to the decompressed + sections. + @param OutputSize Holds the returned size of the decompress + section streams. - EFI_SUCCESS - found the PE32 section in the FFS file + @retval EFI_SUCCESS The section was decompressed successfully. + OutputBuffer contains the resulting data and + OutputSize contains the resulting size. ---*/ +**/ +EFI_STATUS +EFIAPI +Decompress ( + IN CONST EFI_PEI_DECOMPRESS_PPI *This, + IN CONST EFI_COMPRESSION_SECTION *CompressionSection, + OUT VOID **OutputBuffer, + OUT UINTN *OutputSize + ) { EFI_STATUS Status; - VOID *SectionData; - DECOMPRESS_LIBRARY *DecompressLibrary; UINT8 *DstBuffer; UINT8 *ScratchBuffer; UINT32 DstBufferSize; UINT32 ScratchBufferSize; - EFI_COMMON_SECTION_HEADER *CmpSection; - UINTN CmpSectionLength; - UINTN OccupiedCmpSectionLength; - VOID *CmpFileData; - UINTN CmpFileSize; - EFI_COMMON_SECTION_HEADER *Section; - UINTN SectionLength; - UINTN OccupiedSectionLength; - UINT64 FileSize; - UINT32 AuthenticationStatus; - EFI_PEI_SECTION_EXTRACTION_PPI *SectionExtract; - UINT32 BufferSize; - UINT8 *Buffer; - EFI_PEI_SECURITY_PPI *Security; - BOOLEAN StartCrisisRecovery; - EFI_GUID TempGuid; - EFI_FIRMWARE_VOLUME_HEADER *FvHeader; - EFI_COMPRESSION_SECTION *CompressionSection; + VOID *CompressionSource; + UINT32 CompressionSourceSize; + UINT32 UncompressedLength; + UINT8 CompressionType; + + if (CompressionSection->CommonHeader.Type != EFI_SECTION_COMPRESSION) { + ASSERT (FALSE); + return EFI_INVALID_PARAMETER; + } - // - // Initialize local variables. - // - DecompressLibrary = NULL; - DstBuffer = NULL; - DstBufferSize = 0; - - Status = PeiServicesFfsFindSectionData ( - EFI_SECTION_COMPRESSION, - FfsFileHeader, - &SectionData - ); + if (IS_SECTION2 (CompressionSection)) { + CompressionSource = (VOID *) ((UINT8 *) CompressionSection + sizeof (EFI_COMPRESSION_SECTION2)); + CompressionSourceSize = (UINT32) (SECTION2_SIZE (CompressionSection) - sizeof (EFI_COMPRESSION_SECTION2)); + UncompressedLength = ((EFI_COMPRESSION_SECTION2 *) CompressionSection)->UncompressedLength; + CompressionType = ((EFI_COMPRESSION_SECTION2 *) CompressionSection)->CompressionType; + } else { + CompressionSource = (VOID *) ((UINT8 *) CompressionSection + sizeof (EFI_COMPRESSION_SECTION)); + CompressionSourceSize = (UINT32) (SECTION_SIZE (CompressionSection) - sizeof (EFI_COMPRESSION_SECTION)); + UncompressedLength = CompressionSection->UncompressedLength; + CompressionType = CompressionSection->CompressionType; + } // - // First process the compression section + // This is a compression set, expand it // - if (!EFI_ERROR (Status)) { - // - // Yes, there is a compression section, so extract the contents - // Decompress the image here - // - Section = (EFI_COMMON_SECTION_HEADER *) (UINTN) (VOID *) ((UINT8 *) (FfsFileHeader) + (UINTN) sizeof (EFI_FFS_FILE_HEADER)); - - do { - SectionLength = *(UINT32 *) (Section->Size) & 0x00ffffff; - OccupiedSectionLength = GET_OCCUPIED_SIZE (SectionLength, 4); - + switch (CompressionType) { + case EFI_STANDARD_COMPRESSION: + if (FeaturePcdGet(PcdDxeIplSupportUefiDecompress)) { // - // Was the DXE Core file encapsulated in a GUID'd section? + // Load EFI standard compression. + // For compressed data, decompress them to destination buffer. // - if (Section->Type == EFI_SECTION_GUID_DEFINED) { - - // - // This following code constitutes the addition of the security model - // to the DXE IPL. - // - // - // Set a default authenticatino state - // - AuthenticationStatus = 0; - - Status = PeiServicesLocatePpi ( - &gEfiPeiSectionExtractionPpiGuid, - 0, - NULL, - (VOID **)&SectionExtract - ); - - if (EFI_ERROR (Status)) { - return Status; - } - // - // Verify Authentication State - // - CopyMem (&TempGuid, Section + 1, sizeof (EFI_GUID)); - - Status = SectionExtract->PeiGetSection ( - GetPeiServicesTablePointer(), - SectionExtract, - (EFI_SECTION_TYPE *) &SectionType, - &TempGuid, - 0, - (VOID **) &Buffer, - &BufferSize, - &AuthenticationStatus - ); - - if (EFI_ERROR (Status)) { - return Status; - } - // - // If not ask the Security PPI, if exists, for disposition - // - // - Status = PeiServicesLocatePpi ( - &gEfiPeiSecurityPpiGuid, - 0, - NULL, - (VOID **)&Security - ); - if (EFI_ERROR (Status)) { - return Status; - } - - Status = Security->AuthenticationState ( - GetPeiServicesTablePointer(), - (struct _EFI_PEI_SECURITY_PPI *) Security, - AuthenticationStatus, - FfsFileHeader, - &StartCrisisRecovery - ); - - if (EFI_ERROR (Status)) { - return Status; - } + Status = UefiDecompressGetInfo ( + CompressionSource, + CompressionSourceSize, + &DstBufferSize, + &ScratchBufferSize + ); + if (EFI_ERROR (Status)) { // - // If there is a security violation, report to caller and have - // the upper-level logic possible engender a crisis recovery + // GetInfo failed // - if (StartCrisisRecovery) { - return EFI_SECURITY_VIOLATION; - } + DEBUG ((DEBUG_ERROR, "Decompress GetInfo Failed - %r\n", Status)); + return EFI_NOT_FOUND; } - - if (Section->Type == EFI_SECTION_PE32) { - // - // This is what we want - // - *Pe32Data = (VOID *) (Section + 1); - return EFI_SUCCESS; - } else if (Section->Type == EFI_SECTION_COMPRESSION) { - // - // This is a compression set, expand it - // - CompressionSection = (EFI_COMPRESSION_SECTION *) Section; - - switch (CompressionSection->CompressionType) { - case EFI_STANDARD_COMPRESSION: - // - // Load EFI standard compression. - // - if (FeaturePcdGet (PcdDxeIplSupportTianoDecompress)) { - DecompressLibrary = &gEfiDecompress; - } else { - ASSERT (FALSE); - return EFI_NOT_FOUND; - } - break; - - // porting note the original branch for customized compress is removed, it should be change to use GUID compress - - case EFI_NOT_COMPRESSED: - // - // Allocate destination buffer - // - DstBufferSize = CompressionSection->UncompressedLength; - DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize)); - if (DstBuffer == NULL) { - return EFI_OUT_OF_RESOURCES; - } - // - // stream is not actually compressed, just encapsulated. So just copy it. - // - CopyMem (DstBuffer, CompressionSection + 1, DstBufferSize); - break; - - default: - // - // Don't support other unknown compression type. - // - ASSERT_EFI_ERROR (Status); - return EFI_NOT_FOUND; - } - - if (CompressionSection->CompressionType != EFI_NOT_COMPRESSED) { - // - // For compressed data, decompress them to dstbuffer. - // - Status = DecompressLibrary->GetInfo ( - (UINT8 *) ((EFI_COMPRESSION_SECTION *) Section + 1), - (UINT32) SectionLength - sizeof (EFI_COMPRESSION_SECTION), - &DstBufferSize, - &ScratchBufferSize - ); - if (EFI_ERROR (Status)) { - // - // GetInfo failed - // - DEBUG ((EFI_D_ERROR, "Decompress GetInfo Failed - %r\n", Status)); - return EFI_NOT_FOUND; - } - - // - // Allocate scratch buffer - // - ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize)); - if (ScratchBuffer == NULL) { - return EFI_OUT_OF_RESOURCES; - } - - // - // Allocate destination buffer - // - DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize)); - if (DstBuffer == NULL) { - return EFI_OUT_OF_RESOURCES; - } - - // - // Call decompress function - // - Status = DecompressLibrary->Decompress ( - (CHAR8 *) ((EFI_COMPRESSION_SECTION *) Section + 1), - DstBuffer, - ScratchBuffer - ); - if (EFI_ERROR (Status)) { - // - // Decompress failed - // - DEBUG ((EFI_D_ERROR, "Decompress Failed - %r\n", Status)); - return EFI_NOT_FOUND; - } - } - + // + // Allocate scratch buffer + // + ScratchBuffer = AllocatePages (EFI_SIZE_TO_PAGES (ScratchBufferSize)); + if (ScratchBuffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + // + // Allocate destination buffer + // + DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize)); + if (DstBuffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } + // + // Call decompress function + // + Status = UefiDecompress ( + CompressionSource, + DstBuffer, + ScratchBuffer + ); + if (EFI_ERROR (Status)) { // - // Decompress successfully. - // Loop the decompressed data searching for expected section. + // Decompress failed // - CmpSection = (EFI_COMMON_SECTION_HEADER *) DstBuffer; - CmpFileData = (VOID *) DstBuffer; - CmpFileSize = DstBufferSize; - do { - CmpSectionLength = *(UINT32 *) (CmpSection->Size) & 0x00ffffff; - if (CmpSection->Type == SectionType) { - // - // This is what we want - // - if (SectionType == EFI_SECTION_PE32) { - *Pe32Data = (VOID *) (CmpSection + 1); - return EFI_SUCCESS; - } else if (SectionType == EFI_SECTION_FIRMWARE_VOLUME_IMAGE) { - // - // Firmware Volume Image in this Section - // Skip the section header to get FvHeader - // - FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) (CmpSection + 1); - - if (FvHeader->Signature == EFI_FVH_SIGNATURE) { - // - // Because FvLength in FvHeader is UINT64 type, - // so FvHeader must meed at least 8 bytes alignment. - // If current FvImage base address doesn't meet its alignment, - // we need to reload this FvImage to another correct memory address. - // - if (((UINTN) FvHeader % sizeof (UINT64)) != 0) { - DstBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINTN) CmpSectionLength - sizeof (EFI_COMMON_SECTION_HEADER)), sizeof (UINT64)); - if (DstBuffer == NULL) { - return EFI_OUT_OF_RESOURCES; - } - CopyMem (DstBuffer, FvHeader, (UINTN) CmpSectionLength - sizeof (EFI_COMMON_SECTION_HEADER)); - FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) DstBuffer; - } - - // - // Build new FvHob for new decompressed Fv image. - // - BuildFvHob ((EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader, FvHeader->FvLength); - - // - // Set the original FvHob to unused. - // - if (OrigHob != NULL) { - OrigHob->Header->HobType = EFI_HOB_TYPE_UNUSED; - } - - // - // return found FvImage data. - // - *Pe32Data = (VOID *) FvHeader; - return EFI_SUCCESS; - } - } - } - OccupiedCmpSectionLength = GET_OCCUPIED_SIZE (CmpSectionLength, 4); - CmpSection = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) CmpSection + OccupiedCmpSectionLength); - } while (CmpSection->Type != 0 && (UINTN) ((UINT8 *) CmpSection - (UINT8 *) CmpFileData) < CmpFileSize); + DEBUG ((DEBUG_ERROR, "Decompress Failed - %r\n", Status)); + return EFI_NOT_FOUND; } + break; + } else { // - // End of the decompression activity + // PcdDxeIplSupportUefiDecompress is FALSE + // Don't support UEFI decompression algorithm. // + ASSERT (FALSE); + return EFI_NOT_FOUND; + } - Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + OccupiedSectionLength); - FileSize = FfsFileHeader->Size[0] & 0xFF; - FileSize += (FfsFileHeader->Size[1] << 8) & 0xFF00; - FileSize += (FfsFileHeader->Size[2] << 16) & 0xFF0000; - FileSize &= 0x00FFFFFF; - } while (Section->Type != 0 && (UINTN) ((UINT8 *) Section - (UINT8 *) FfsFileHeader) < FileSize); - + case EFI_NOT_COMPRESSED: // - // search all sections (compression and non compression) in this FFS, don't - // find expected section. + // Allocate destination buffer // - return EFI_NOT_FOUND; - } else { + DstBufferSize = UncompressedLength; + DstBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize)); + if (DstBuffer == NULL) { + return EFI_OUT_OF_RESOURCES; + } // - // For those FFS that doesn't contain compression section, directly search - // PE or TE section in this FFS. + // stream is not actually compressed, just encapsulated. So just copy it. // + CopyMem (DstBuffer, CompressionSource, DstBufferSize); + break; - Status = PeiServicesFfsFindSectionData ( - EFI_SECTION_PE32, - FfsFileHeader, - &SectionData - ); - - if (EFI_ERROR (Status)) { - Status = PeiServicesFfsFindSectionData ( - EFI_SECTION_TE, - FfsFileHeader, - &SectionData - ); - if (EFI_ERROR (Status)) { - return Status; - } - } + default: + // + // Don't support other unknown compression type. + // + ASSERT (FALSE); + return EFI_NOT_FOUND; } - *Pe32Data = SectionData; + *OutputSize = DstBufferSize; + *OutputBuffer = DstBuffer; return EFI_SUCCESS; } + +/** + Updates the Stack HOB passed to DXE phase. + + This function traverses the whole HOB list and update the stack HOB to + reflect the real stack that is used by DXE core. + + @param BaseAddress The lower address of stack used by DxeCore. + @param Length The length of stack used by DxeCore. + +**/ +VOID +UpdateStackHob ( + IN EFI_PHYSICAL_ADDRESS BaseAddress, + IN UINT64 Length + ) +{ + EFI_PEI_HOB_POINTERS Hob; + + Hob.Raw = GetHobList (); + while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw)) != NULL) { + if (CompareGuid (&gEfiHobMemoryAllocStackGuid, &(Hob.MemoryAllocationStack->AllocDescriptor.Name))) { + // + // Build a new memory allocation HOB with old stack info with EfiBootServicesData type. Need to + // avoid this region be reclaimed by DXE core as the IDT built in SEC might be on stack, and some + // PEIMs may also keep key information on stack + // + BuildMemoryAllocationHob ( + Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress, + Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength, + EfiBootServicesData + ); + // + // Update the BSP Stack Hob to reflect the new stack info. + // + Hob.MemoryAllocationStack->AllocDescriptor.MemoryBaseAddress = BaseAddress; + Hob.MemoryAllocationStack->AllocDescriptor.MemoryLength = Length; + break; + } + Hob.Raw = GET_NEXT_HOB (Hob); + } +} +