]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Pei/FwVol/FwVol.c
Following 3 updates are for spec update published in PI 1.2.
[mirror_edk2.git] / MdeModulePkg / Core / Pei / FwVol / FwVol.c
index 8ab0ffcae9046a0f045066cc45cfec9b90ef6111..7b74edb69ac92778e1c942d7b91d8f444df4cdf1 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Pei Core Firmware File System service routines.\r
   \r
-Copyright (c) 2006 - 2008, Intel Corporation                                                         \r
+Copyright (c) 2006 - 2009, Intel Corporation                                                         \r
 All rights reserved. This program and the accompanying materials                          \r
 are licensed and made available under the terms and conditions of the BSD License         \r
 which accompanies this distribution.  The full text of the license may be found at        \r
@@ -12,7 +12,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include <PeiMain.h>\r
+#include "FwVol.h"\r
 \r
 EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnFvInfoList = {\r
   (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
@@ -20,10 +20,21 @@ EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnFvInfoList = {
   FirmwareVolmeInfoPpiNotifyCallback \r
 };\r
 \r
-\r
-#define GET_OCCUPIED_SIZE(ActualSize, Alignment) \\r
-  ((ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1)))\r
-\r
+EFI_PEI_FIRMWARE_VOLUME_PPI mPeiFfs2FvPpi = {\r
+  PeiFfs2FvPpiProcessVolume,\r
+  PeiFfs2FvPpiFindFileByType,\r
+  PeiFfs2FvPpiFindFileByName,\r
+  PeiFfs2FvPpiGetFileInfo,\r
+  PeiFfs2FvPpiGetVolumeInfo,\r
+  PeiFfs2FvPpiFindSectionByType\r
+};\r
+            \r
+EFI_PEI_PPI_DESCRIPTOR  mPeiFfs2FvPpiList = {\r
+  (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
+  &gEfiFirmwareFileSystem2Guid,\r
+  &mPeiFfs2FvPpi\r
+};\r
\r
 /**\r
   Returns the file state set by the highest zero bit in the State field\r
 \r
@@ -87,20 +98,15 @@ CalculateHeaderChecksum (
 }\r
 \r
 /**\r
-  Find FV handler according some FileHandle in that FV.\r
+  Find FV handler according to FileHandle in that FV.\r
 \r
   @param FileHandle      Handle of file image\r
-  @param VolumeHandle    Handle of the found FV, if not found, NULL will be set.\r
-\r
-  @retval TRUE           The corresponding FV handler is found.\r
-  @retval FALSE          The corresponding FV handler is not found.\r
-\r
+  \r
+  @return Pointer to instance of PEI_CORE_FV_HANDLE.\r
 **/\r
-BOOLEAN\r
-EFIAPI\r
-PeiFileHandleToVolume (\r
-  IN   EFI_PEI_FILE_HANDLE     FileHandle,\r
-  OUT  EFI_PEI_FV_HANDLE       *VolumeHandle\r
+PEI_CORE_FV_HANDLE*\r
+FileHandleToVolume (\r
+  IN   EFI_PEI_FILE_HANDLE          FileHandle\r
   )\r
 {\r
   UINTN                       Index;\r
@@ -108,16 +114,15 @@ PeiFileHandleToVolume (
   EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader;\r
 \r
   PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
+  \r
   for (Index = 0; Index < PrivateData->FvCount; Index++) {\r
     FwVolHeader = PrivateData->Fv[Index].FvHeader;\r
     if (((UINT64) (UINTN) FileHandle > (UINT64) (UINTN) FwVolHeader ) &&   \\r
         ((UINT64) (UINTN) FileHandle <= ((UINT64) (UINTN) FwVolHeader + FwVolHeader->FvLength - 1))) {\r
-      *VolumeHandle = (EFI_PEI_FV_HANDLE)FwVolHeader;\r
-      return TRUE;\r
+      return &PrivateData->Fv[Index];\r
     }\r
   }\r
-  *VolumeHandle = NULL;\r
-  return FALSE;\r
+  return NULL;\r
 }\r
 \r
 /**\r
@@ -140,7 +145,7 @@ PeiFileHandleToVolume (
 \r
 **/\r
 EFI_STATUS\r
-PeiFindFileEx (\r
+FindFileEx (\r
   IN  CONST EFI_PEI_FV_HANDLE        FvHandle,\r
   IN  CONST EFI_GUID                 *FileName,   OPTIONAL\r
   IN        EFI_FV_FILETYPE          SearchType,\r
@@ -151,15 +156,17 @@ PeiFindFileEx (
   EFI_FIRMWARE_VOLUME_HEADER           *FwVolHeader;\r
   EFI_FFS_FILE_HEADER                   **FileHeader;\r
   EFI_FFS_FILE_HEADER                   *FfsFileHeader;\r
-  EFI_FIRMWARE_VOLUME_EXT_HEADER        *FwVolExHeaderInfo;\r
   UINT32                                FileLength;\r
   UINT32                                FileOccupiedSize;\r
   UINT32                                FileOffset;\r
   UINT64                                FvLength;\r
   UINT8                                 ErasePolarity;\r
   UINT8                                 FileState;\r
-\r
-  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)FvHandle;\r
+  \r
+  //\r
+  // Convert the handle of FV to FV header for memory-mapped firmware volume\r
+  //\r
+  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) FvHandle;\r
   FileHeader  = (EFI_FFS_FILE_HEADER **)FileHandle;\r
 \r
   FvLength = FwVolHeader->FvLength;\r
@@ -176,10 +183,6 @@ PeiFindFileEx (
   //\r
   if ((*FileHeader == NULL) || (FileName != NULL)) {\r
     FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FwVolHeader + FwVolHeader->HeaderLength);\r
-    if (FwVolHeader->ExtHeaderOffset != 0) {\r
-      FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER *)(((UINT8 *)FwVolHeader) + FwVolHeader->ExtHeaderOffset);\r
-      FfsFileHeader = (EFI_FFS_FILE_HEADER *)(((UINT8 *)FwVolExHeaderInfo) + FwVolExHeaderInfo->ExtHeaderSize);\r
-    }\r
   } else {\r
     //\r
     // Length is 24 bits wide so mask upper 8 bits\r
@@ -274,20 +277,39 @@ PeiInitializeFv (
   IN CONST EFI_SEC_PEI_HAND_OFF   *SecCoreData\r
   )\r
 {\r
-  EFI_STATUS  Status;\r
+  EFI_STATUS                    Status;\r
+  EFI_PEI_FIRMWARE_VOLUME_PPI   *FvPpi;\r
+  EFI_PEI_FV_HANDLE             FvHandle;\r
+  EFI_FIRMWARE_VOLUME_HEADER    *BfvHeader;\r
+  \r
   //\r
-  // The BFV must be the first entry. The Core FV support is stateless \r
-  // The AllFV list has a single entry per FV in PEI. \r
-  // The Fv list only includes FV that PEIMs will be dispatched from and\r
-  // its File System Format is PI 1.0 definition.\r
+  // Install FV_PPI for FFS2 file system.\r
   //\r
-  PrivateData->FvCount = 1;\r
-  PrivateData->Fv[0].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)SecCoreData->BootFirmwareVolumeBase;\r
-\r
-  PrivateData->AllFvCount = 1;\r
-  PrivateData->AllFv[0] = (EFI_PEI_FV_HANDLE)PrivateData->Fv[0].FvHeader;\r
-\r
-\r
+  PeiServicesInstallPpi (&mPeiFfs2FvPpiList);\r
+  \r
+  BfvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)SecCoreData->BootFirmwareVolumeBase;\r
+  \r
+  //\r
+  // The FV_PPI in BFV's format should be installed.\r
+  //\r
+  Status = PeiServicesLocatePpi (\r
+             &BfvHeader->FileSystemGuid,\r
+             0,\r
+             NULL,\r
+             (VOID**)&FvPpi\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+    \r
+  //\r
+  // Get handle of BFV\r
+  //\r
+  FvPpi->ProcessVolume (\r
+           FvPpi, \r
+           SecCoreData->BootFirmwareVolumeBase,\r
+           (UINTN)BfvHeader->FvLength,\r
+           &FvHandle\r
+           );\r
+                        \r
   //\r
   // Post a call-back for the FvInfoPPI services to expose\r
   // additional Fvs to PeiCore.\r
@@ -296,7 +318,7 @@ PeiInitializeFv (
   ASSERT_EFI_ERROR (Status);\r
 \r
 }\r
-\r
+  \r
 /**\r
   Process Firmware Volum Information once FvInfoPPI install.\r
   The FV Info will be registered into PeiCore private data structure.\r
@@ -318,80 +340,68 @@ FirmwareVolmeInfoPpiNotifyCallback (
   IN VOID                          *Ppi\r
   )\r
 {\r
-  UINT8                                 FvCount;\r
-  EFI_PEI_FIRMWARE_VOLUME_INFO_PPI      *Fv;\r
+  EFI_PEI_FIRMWARE_VOLUME_INFO_PPI      *FvInfoPpi;\r
+  EFI_PEI_FIRMWARE_VOLUME_PPI           *FvPpi;\r
   PEI_CORE_INSTANCE                     *PrivateData;\r
-  EFI_PEI_FILE_HANDLE                   FileHandle;\r
-  VOID                                  *DepexData;\r
-  UINT32                                AuthenticationStatus;\r
   EFI_STATUS                            Status;\r
+  EFI_PEI_FV_HANDLE                     FvHandle;\r
+  UINTN                                 FvIndex;\r
   \r
-  FileHandle   = NULL;\r
-  DepexData    = NULL;\r
   Status       = EFI_SUCCESS;\r
   PrivateData  = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
 \r
   if (PrivateData->FvCount >= FixedPcdGet32 (PcdPeiCoreMaxFvSupported)) {\r
+    DEBUG ((EFI_D_ERROR, "The number of Fv Images (%d) exceed the max supported FVs (%d) in Pei", PrivateData->FvCount + 1, FixedPcdGet32 (PcdPeiCoreMaxFvSupported)));\r
+    DEBUG ((EFI_D_ERROR, "PcdPeiCoreMaxFvSupported value need be reconfigurated in DSC"));\r
     ASSERT (FALSE);\r
   }\r
 \r
-  Fv = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *)Ppi;\r
+  FvInfoPpi = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *)Ppi;\r
 \r
-  if (CompareGuid (&Fv->FvFormat, &gEfiFirmwareFileSystem2Guid)) {\r
-    for (FvCount = 0; FvCount < PrivateData->FvCount; FvCount ++) {\r
-      if ((UINTN)PrivateData->Fv[FvCount].FvHeader == (UINTN)Fv->FvInfo) {\r
-        return EFI_SUCCESS;\r
-      }\r
-    }\r
-    \r
-    Status = VerifyFv ((EFI_FIRMWARE_VOLUME_HEADER*)Fv->FvInfo);\r
-    if (EFI_ERROR(Status)) {\r
-      DEBUG ((EFI_D_ERROR, "Fail to verify FV which address is 0x%11p", (VOID *) Fv->FvInfo));\r
+  //\r
+  // Locate the corresponding FV_PPI according to founded FV's format guid\r
+  //\r
+  Status = PeiServicesLocatePpi (\r
+             &FvInfoPpi->FvFormat, \r
+             0, \r
+             NULL,\r
+             (VOID**)&FvPpi\r
+             );\r
+  if (!EFI_ERROR (Status)) {\r
+    //\r
+    // Process new found FV and get FV handle.\r
+    //\r
+    Status = FvPpi->ProcessVolume (FvPpi, FvInfoPpi->FvInfo, FvInfoPpi->FvInfoSize, &FvHandle);\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "Fail to process new found FV, FV may be corrupted!"));\r
       return Status;\r
     }\r
+    DEBUG ((EFI_D_INFO, "Found and process new FV %p, all fv's count is %d\n", FvHandle, PrivateData->FvCount));\r
+  } else {\r
+    DEBUG ((EFI_D_ERROR, "Fail to process FV %p because no corresponding EFI_FIRMWARE_VOLUME_PPI is found!\n", FvInfoPpi->FvInfo));\r
     \r
-    PrivateData->Fv[PrivateData->FvCount++].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*)Fv->FvInfo;\r
-\r
     //\r
-    // Only add FileSystem2 Fv to the All list\r
+    // If can not find EFI_FIRMWARE_VOLUME_PPI to process firmware to get FvHandle, \r
+    // use the address of FV buffer as its handle.\r
     //\r
-    PrivateData->AllFv[PrivateData->AllFvCount++] = (EFI_PEI_FV_HANDLE)Fv->FvInfo;\r
+    FvHandle = FvInfoPpi->FvInfo;\r
     \r
-    DEBUG ((EFI_D_INFO, "The %dth FvImage start address is 0x%11p and size is 0x%08x\n", PrivateData->AllFvCount, (VOID *) Fv->FvInfo, Fv->FvInfoSize));\r
     //\r
-    // Preprocess all FV type files in this new FileSystem2 Fv image\r
+    // Check whether the FV has already been processed.\r
     //\r
-    do {\r
-      Status = PeiFindFileEx (\r
-                 (EFI_PEI_FV_HANDLE)Fv->FvInfo, \r
-                 NULL, \r
-                 EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, \r
-                 &FileHandle, \r
-                 NULL\r
-                 );\r
-      if (!EFI_ERROR (Status)) {\r
-        Status = PeiFfsFindSectionData (\r
-                    (CONST EFI_PEI_SERVICES **) PeiServices,\r
-                    EFI_SECTION_PEI_DEPEX,\r
-                    FileHandle, \r
-                    (VOID **)&DepexData\r
-                    );\r
-        if (!EFI_ERROR (Status)) {\r
-          if (!PeimDispatchReadiness (PeiServices, DepexData)) {\r
-            //\r
-            // Dependency is not satisfied.\r
-            //\r
-            continue;\r
-          }\r
-        }\r
-        //\r
-        // Process FvFile to install FvInfo ppi and build FvHob\r
-        // \r
-        ProcessFvFile ((CONST EFI_PEI_SERVICES **) PeiServices, FileHandle, &AuthenticationStatus);\r
+    for (FvIndex = 0; FvIndex < PrivateData->FvCount; FvIndex ++) {\r
+      if (PrivateData->Fv[FvIndex].FvHandle == FvHandle) {\r
+        DEBUG ((EFI_D_INFO, "The Fv %p has already been processed!\n", FvHandle));\r
+        return EFI_SUCCESS;\r
       }\r
-    } while (FileHandle != NULL);\r
+    }    \r
+    \r
+    PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) FvInfoPpi->FvInfo;\r
+    PrivateData->Fv[PrivateData->FvCount].FvPpi    = NULL;\r
+    PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;\r
+    PrivateData->FvCount ++;\r
   }\r
-\r
+  \r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -400,11 +410,11 @@ FirmwareVolmeInfoPpiNotifyCallback (
   Search within encapsulation sections (compression and GUIDed) recursively, \r
   until the match section is found.\r
   \r
-  @param PeiServices     - An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
-  @param SectionType     - Filter to find only section of this type.\r
-  @param Section         - From where to search.\r
-  @param SectionSize     - The file size to search.\r
-  @param OutputBuffer    - A pointer to the discovered section, if successful.\r
+  @param PeiServices       An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
+  @param SectionType       Filter to find only section of this type.\r
+  @param Section           From where to search.\r
+  @param SectionSize       The file size to search.\r
+  @param OutputBuffer      A pointer to the discovered section, if successful.\r
                            NULL if section not found\r
 \r
   @return EFI_NOT_FOUND    The match section is not found.\r
@@ -412,7 +422,7 @@ FirmwareVolmeInfoPpiNotifyCallback (
 \r
 **/\r
 EFI_STATUS\r
-PeiFfsProcessSection (\r
+ProcessSection (\r
   IN CONST EFI_PEI_SERVICES     **PeiServices,\r
   IN EFI_SECTION_TYPE           SectionType,\r
   IN EFI_COMMON_SECTION_HEADER  *Section,\r
@@ -453,13 +463,13 @@ PeiFfsProcessSection (
           //\r
           // Search section directly from the cache data.\r
           //\r
-          return PeiFfsProcessSection (\r
-                  PeiServices,\r
-                  SectionType, \r
-                  PpiOutput, \r
-                  PpiOutputSize, \r
-                  OutputBuffer \r
-                  );\r
+          return ProcessSection (\r
+                   PeiServices,\r
+                   SectionType, \r
+                   PpiOutput, \r
+                   PpiOutputSize, \r
+                   OutputBuffer \r
+                   );\r
         }\r
       }\r
       \r
@@ -473,12 +483,12 @@ PeiFfsProcessSection (
                    );\r
         if (!EFI_ERROR (Status)) {\r
           Status = GuidSectionPpi->ExtractSection (\r
-                                    GuidSectionPpi,\r
-                                    Section,\r
-                                    &PpiOutput,\r
-                                    &PpiOutputSize,\r
-                                    &Authentication\r
-                                    );\r
+                                     GuidSectionPpi,\r
+                                     Section,\r
+                                     &PpiOutput,\r
+                                     &PpiOutputSize,\r
+                                     &Authentication\r
+                                     );\r
         }\r
       } else if (Section->Type == EFI_SECTION_COMPRESSION) {\r
         Status = PeiServicesLocatePpi (&gEfiPeiDecompressPpiGuid, 0, NULL, (VOID **) &DecompressPpi);\r
@@ -504,13 +514,13 @@ PeiFfsProcessSection (
         PrivateData->CacheSection.SectionSize [PrivateData->CacheSection.SectionIndex] = PpiOutputSize;\r
         PrivateData->CacheSection.SectionIndex = (PrivateData->CacheSection.SectionIndex + 1)%CACHE_SETION_MAX_NUMBER;\r
         \r
-        return PeiFfsProcessSection (\r
-                PeiServices,\r
-                SectionType, \r
-                PpiOutput, \r
-                PpiOutputSize, \r
-                OutputBuffer \r
-                );\r
+        return ProcessSection (\r
+                 PeiServices,\r
+                 SectionType, \r
+                 PpiOutput, \r
+                 PpiOutputSize, \r
+                 OutputBuffer \r
+                 );\r
       }\r
     }\r
 \r
@@ -531,8 +541,7 @@ PeiFfsProcessSection (
 \r
 \r
 /**\r
-  Given the input file pointer, search for the first matching section in the\r
-  FFS volume.\r
+  Searches for the next matching section within the specified file.\r
 \r
   @param PeiServices     An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
   @param SectionType     Filter to find only sections of this type.\r
@@ -540,57 +549,43 @@ PeiFfsProcessSection (
   @param SectionData     A pointer to the discovered section, if successful.\r
                          NULL if section not found\r
 \r
-  @retval EFI_NOT_FOUND  No files matching the search criteria were found\r
-  @retval EFI_SUCCESS    Success to find section data in given file\r
+  @retval EFI_NOT_FOUND  The section was not found.\r
+  @retval EFI_SUCCESS    The section was found.\r
 \r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiFfsFindSectionData (\r
-  IN CONST EFI_PEI_SERVICES      **PeiServices,\r
+  IN CONST EFI_PEI_SERVICES    **PeiServices,\r
   IN     EFI_SECTION_TYPE      SectionType,\r
   IN     EFI_PEI_FILE_HANDLE   FileHandle,\r
-  IN OUT VOID                  **SectionData\r
+  OUT VOID                     **SectionData\r
   )\r
 {\r
-  EFI_FFS_FILE_HEADER                     *FfsFileHeader;\r
-  UINT32                                  FileSize;\r
-  EFI_COMMON_SECTION_HEADER               *Section;\r
-\r
-  FfsFileHeader = (EFI_FFS_FILE_HEADER *)(FileHandle);\r
-\r
-  //\r
-  // Size is 24 bits wide so mask upper 8 bits. \r
-  // Does not include FfsFileHeader header size\r
-  // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.\r
-  //\r
-  Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);\r
-  FileSize = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;\r
-  FileSize -= sizeof (EFI_FFS_FILE_HEADER);\r
-\r
-  return PeiFfsProcessSection (\r
-          PeiServices,\r
-          SectionType, \r
-          Section, \r
-          FileSize, \r
-          SectionData\r
-          );\r
+  PEI_CORE_FV_HANDLE           *CoreFvHandle;\r
+  \r
+  CoreFvHandle = FileHandleToVolume (FileHandle);\r
+  if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  \r
+  return CoreFvHandle->FvPpi->FindSectionByType (CoreFvHandle->FvPpi, SectionType, FileHandle, SectionData);\r
 }\r
 \r
 /**\r
-  Given the input file pointer, search for the next matching file in the\r
-  FFS volume as defined by SearchType. The search starts from FileHeader inside\r
-  the Firmware Volume defined by FwVolHeader.\r
-\r
+  Searches for the next matching file in the firmware volume.\r
 \r
   @param PeiServices     An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
   @param SearchType      Filter to find only files of this type.\r
                          Type EFI_FV_FILETYPE_ALL causes no filtering to be done.\r
-  @param VolumeHandle    Pointer to the FV header of the volume to search.\r
-  @param FileHandle      Pointer to the current file from which to begin searching.\r
-                         This pointer will be updated upon return to reflect the file found.\r
-  @retval EFI_NOT_FOUND  No files matching the search criteria were found\r
-  @retval EFI_SUCCESS    Success to find next file in given volume\r
+  @param VolumeHandle    Handle of firmware volume in which to search.\r
+  @param FileHandle      On entry, points to the current handle from which to begin searching or NULL to start\r
+                         at the beginning of the firmware volume. On exit, points the file handle of the next file\r
+                         in the volume or NULL if there are no more files.\r
+\r
+  @retval EFI_NOT_FOUND  The file was not found.\r
+  @retval EFI_NOT_FOUND  The header checksum was not zero.\r
+  @retval EFI_SUCCESS    The file was found.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -598,11 +593,28 @@ EFIAPI
 PeiFfsFindNextFile (\r
   IN CONST EFI_PEI_SERVICES      **PeiServices,\r
   IN UINT8                       SearchType,\r
-  IN EFI_PEI_FV_HANDLE           VolumeHandle,\r
+  IN EFI_PEI_FV_HANDLE           FvHandle,\r
   IN OUT EFI_PEI_FILE_HANDLE     *FileHandle\r
   )\r
 {\r
-  return PeiFindFileEx (VolumeHandle, NULL, SearchType, FileHandle, NULL);\r
+  PEI_CORE_FV_HANDLE      *CoreFvHandle;\r
+  \r
+  CoreFvHandle = FvHandleToCoreHandle (FvHandle);\r
+  \r
+  //\r
+  // To make backward compatiblity, if can not find corresponding the handle of FV\r
+  // then treat FV as build-in FFS2 format and memory mapped FV that FV handle is pointed\r
+  // to the address of first byte of FV.\r
+  //\r
+  if ((CoreFvHandle == NULL) && FeaturePcdGet (PcdFrameworkCompatibilitySupport)) {\r
+    return FindFileEx (FvHandle, NULL, SearchType, FileHandle, NULL);\r
+  } \r
+  \r
+  if ((CoreFvHandle == NULL) || CoreFvHandle->FvPpi == NULL) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  \r
+  return CoreFvHandle->FvPpi->FindFileByType (CoreFvHandle->FvPpi, SearchType, FvHandle, FileHandle);\r
 }\r
 \r
 \r
@@ -610,68 +622,36 @@ PeiFfsFindNextFile (
   Search the firmware volumes by index\r
 \r
   @param PeiServices     An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
-  @param Instance        Instance of FV to find\r
-  @param VolumeHandle    Pointer to found Volume.\r
+  @param Instance        This instance of the firmware volume to find. The value 0 is the Boot Firmware\r
+                         Volume (BFV).\r
+  @param VolumeHandle    On exit, points to the next volume handle or NULL if it does not exist.\r
 \r
-  @retval EFI_INVALID_PARAMETER  FwVolHeader is NULL\r
-  @retval EFI_SUCCESS            Firmware volume instance successfully found.\r
+  @retval EFI_INVALID_PARAMETER  VolumeHandle is NULL\r
+  @retval EFI_NOT_FOUND          The volume was not found.\r
+  @retval EFI_SUCCESS            The volume was found.\r
 \r
 **/\r
 EFI_STATUS \r
 EFIAPI\r
-PeiFvFindNextVolume (\r
+PeiFfsFindNextVolume (\r
   IN CONST EFI_PEI_SERVICES         **PeiServices,\r
   IN     UINTN                      Instance,\r
   IN OUT EFI_PEI_FV_HANDLE          *VolumeHandle\r
   )\r
 {\r
-  PEI_CORE_INSTANCE        *Private;\r
-  UINTN                    Index;\r
-  BOOLEAN                  Match;\r
-  EFI_HOB_FIRMWARE_VOLUME  *FvHob;\r
-\r
+  PEI_CORE_INSTANCE  *Private;\r
+  PEI_CORE_FV_HANDLE *CoreFvHandle;\r
+  \r
   Private = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
-  if (VolumeHandle == NULL) {\r
-   return EFI_INVALID_PARAMETER;\r
-  }\r
   \r
-  //\r
-  // Handle Framework FvHob and Install FvInfo Ppi for it.\r
-  //\r
-  if (FeaturePcdGet (PcdFrameworkFvHobCompatibilitySupport)) {\r
-    //\r
-    // Loop to search the wanted FirmwareVolume which supports FFS\r
-    //\r
-    FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetFirstHob (EFI_HOB_TYPE_FV);\r
-    while (FvHob != NULL) {\r
-      for (Index = 0, Match = FALSE; Index < Private->AllFvCount; Index++) {\r
-        if ((EFI_PEI_FV_HANDLE)(UINTN)FvHob->BaseAddress == Private->AllFv[Index]) {\r
-          Match = TRUE;\r
-          break;\r
-        }\r
-      }\r
-      //\r
-      // If Not Found, Install FvInfo Ppi for it.\r
-      //\r
-      if (!Match) {\r
-        PiLibInstallFvInfoPpi (\r
-          NULL,\r
-          (VOID *)(UINTN)FvHob->BaseAddress,\r
-          (UINT32)FvHob->Length,\r
-          NULL,\r
-          NULL\r
-          );\r
-      }\r
-      FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetNextHob (EFI_HOB_TYPE_FV, (VOID *)((UINTN)FvHob + FvHob->Header.HobLength)); \r
-    }\r
+  CoreFvHandle = FindNextCoreFvHandle (Private, Instance);\r
+  if (CoreFvHandle == NULL) {\r
+    *VolumeHandle = NULL;\r
+    return EFI_NOT_FOUND;\r
   }\r
-\r
-  if (Instance >= Private->AllFvCount) {\r
-   VolumeHandle = NULL;\r
-   return EFI_NOT_FOUND;\r
-  }\r
-\r
-  *VolumeHandle = Private->AllFv[Instance];\r
+  \r
+  *VolumeHandle = CoreFvHandle->FvHandle;\r
+  \r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -697,27 +677,29 @@ PeiFfsFindFileByName (
   OUT EFI_PEI_FILE_HANDLE   *FileHandle\r
   )\r
 {\r
-  EFI_STATUS  Status;\r
+  PEI_CORE_FV_HANDLE            *CoreFvHandle;\r
+  \r
   if ((VolumeHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  Status = PeiFindFileEx (VolumeHandle, FileName, 0, FileHandle, NULL);\r
-  if (Status == EFI_NOT_FOUND) {\r
-    *FileHandle = NULL;\r
+  \r
+  CoreFvHandle = FvHandleToCoreHandle (VolumeHandle);\r
+  if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {\r
+    return EFI_NOT_FOUND;\r
   }\r
-  return Status;\r
+  \r
+  return CoreFvHandle->FvPpi->FindFileByName (CoreFvHandle->FvPpi, FileName, &VolumeHandle, FileHandle);\r
 }\r
 \r
 /**\r
-\r
   Returns information about a specific file.\r
 \r
+  @param FileHandle       Handle of the file.\r
+  @param FileInfo         Upon exit, points to the file’s information.\r
 \r
-  @param FileHandle      - The handle to file.\r
-  @param FileInfo        - Pointer to the file information.\r
-\r
-  @retval EFI_INVALID_PARAMETER Invalid FileHandle or FileInfo.\r
-  @retval EFI_SUCCESS           Success to collect file info.\r
+  @retval EFI_INVALID_PARAMETER If FileInfo is NULL.\r
+  @retval EFI_INVALID_PARAMETER If FileHandle does not represent a valid file.\r
+  @retval EFI_SUCCESS           File information returned.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -727,61 +709,40 @@ PeiFfsGetFileInfo (
   OUT EFI_FV_FILE_INFO    *FileInfo\r
   )\r
 {\r
-  UINT8                       FileState;\r
-  UINT8                       ErasePolarity;\r
-  EFI_FFS_FILE_HEADER         *FileHeader;\r
-  EFI_PEI_FV_HANDLE           VolumeHandle;\r
-\r
+  PEI_CORE_FV_HANDLE          *CoreFvHandle;\r
+  \r
   if ((FileHandle == NULL) || (FileInfo == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  VolumeHandle = 0;\r
   //\r
   // Retrieve the FirmwareVolume which the file resides in.\r
   //\r
-  if (!PeiFileHandleToVolume(FileHandle, &VolumeHandle)) {\r
+  CoreFvHandle = FileHandleToVolume (FileHandle);\r
+  if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (((EFI_FIRMWARE_VOLUME_HEADER*)VolumeHandle)->Attributes & EFI_FVB2_ERASE_POLARITY) {\r
-    ErasePolarity = 1;\r
-  } else {\r
-    ErasePolarity = 0;\r
-  }\r
-\r
-  //\r
-  // Get FileState which is the highest bit of the State \r
-  //\r
-  FileState = GetFileState (ErasePolarity, (EFI_FFS_FILE_HEADER*)FileHandle);\r
-\r
-  switch (FileState) {\r
-    case EFI_FILE_DATA_VALID:\r
-    case EFI_FILE_MARKED_FOR_UPDATE:\r
-      break;  \r
-    default:\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
-\r
-  FileHeader = (EFI_FFS_FILE_HEADER *)FileHandle;\r
-  CopyMem (&FileInfo->FileName, &FileHeader->Name, sizeof(EFI_GUID));\r
-  FileInfo->FileType = FileHeader->Type;\r
-  FileInfo->FileAttributes = FileHeader->Attributes;\r
-  FileInfo->BufferSize = ((*(UINT32 *)FileHeader->Size) & 0x00FFFFFF) -  sizeof (EFI_FFS_FILE_HEADER);\r
-  FileInfo->Buffer = (FileHeader + 1);\r
-  return EFI_SUCCESS;\r
+  return CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, FileHandle, FileInfo);\r
 }\r
 \r
 \r
 /**\r
+  Returns information about the specified volume.\r
+\r
+  This function returns information about a specific firmware\r
+  volume, including its name, type, attributes, starting address\r
+  and size.\r
 \r
-  Collect information of given Fv Volume.\r
+  @param VolumeHandle   Handle of the volume.\r
+  @param VolumeInfo     Upon exit, points to the volume's information.\r
 \r
-  @param VolumeHandle    - The handle to Fv Volume.\r
-  @param VolumeInfo      - The pointer to volume information.\r
+  @retval EFI_SUCCESS             Volume information returned.\r
+  @retval EFI_INVALID_PARAMETER   If VolumeHandle does not represent a valid volume.\r
+  @retval EFI_INVALID_PARAMETER   If VolumeHandle is NULL.\r
+  @retval EFI_SUCCESS             Information successfully returned.\r
+  @retval EFI_INVALID_PARAMETER   The volume designated by the VolumeHandle is not available.\r
 \r
-  @retval EFI_INVALID_PARAMETER VolumeInfo is NULL\r
-  @retval EFI_SUCCESS           Success to collect fv info.\r
 **/\r
 EFI_STATUS\r
 EFIAPI \r
@@ -790,44 +751,26 @@ PeiFfsGetVolumeInfo (
   OUT EFI_FV_INFO       *VolumeInfo\r
   )\r
 {\r
-  EFI_FIRMWARE_VOLUME_HEADER             FwVolHeader;\r
-  EFI_FIRMWARE_VOLUME_EXT_HEADER         *FwVolExHeaderInfo;\r
-\r
+  PEI_CORE_FV_HANDLE                     *CoreHandle;\r
+  \r
   if (VolumeInfo == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
   \r
-  //\r
-  // VolumeHandle may not align at 8 byte, \r
-  // but FvLength is UINT64 type, which requires FvHeader align at least 8 byte. \r
-  // So, Copy FvHeader into the local FvHeader structure.\r
-  //\r
-  CopyMem (&FwVolHeader, VolumeHandle, sizeof (EFI_FIRMWARE_VOLUME_HEADER));\r
-  //\r
-  // Check Fv Image Signature\r
-  //\r
-  if (FwVolHeader.Signature != EFI_FVH_SIGNATURE) {\r
+  CoreHandle = FvHandleToCoreHandle (VolumeHandle);\r
+  \r
+  if ((CoreHandle == NULL) || (CoreHandle->FvPpi == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  VolumeInfo->FvAttributes = FwVolHeader.Attributes;\r
-  VolumeInfo->FvStart = (VOID *) VolumeHandle;\r
-  VolumeInfo->FvSize = FwVolHeader.FvLength;\r
-  CopyMem (&VolumeInfo->FvFormat, &FwVolHeader.FileSystemGuid, sizeof(EFI_GUID));\r
-\r
-  if (FwVolHeader.ExtHeaderOffset != 0) {\r
-    FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER*)(((UINT8 *)VolumeHandle) + FwVolHeader.ExtHeaderOffset);\r
-    CopyMem (&VolumeInfo->FvName, &FwVolExHeaderInfo->FvName, sizeof(EFI_GUID));\r
-  }\r
-  return EFI_SUCCESS;\r
+  \r
+  return CoreHandle->FvPpi->GetVolumeInfo (CoreHandle->FvPpi, VolumeHandle, VolumeInfo);\r
 }\r
 \r
 /**\r
   Get Fv image from the FV type file, then install FV INFO ppi, Build FV hob.\r
 \r
-  @param PeiServices          An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
-  @param FvFileHandle         File handle of a Fv type file.\r
-  @param AuthenticationState  Pointer to attestation authentication state of image.\r
-\r
+  @param ParentFvCoreHandle   Pointer of EFI_CORE_FV_HANDLE to parent Fv image that contain this Fv image.\r
+  @param ParentFvFileHandle   File handle of a Fv type file that contain this Fv image.\r
 \r
   @retval EFI_NOT_FOUND         FV image can't be found.\r
   @retval EFI_SUCCESS           Successfully to process it.\r
@@ -837,60 +780,57 @@ PeiFfsGetVolumeInfo (
 **/\r
 EFI_STATUS\r
 ProcessFvFile (\r
-  IN  CONST EFI_PEI_SERVICES      **PeiServices,\r
-  IN  EFI_PEI_FILE_HANDLE         FvFileHandle,\r
-  OUT UINT32                      *AuthenticationState\r
+  IN  PEI_CORE_FV_HANDLE          *ParentFvCoreHandle,\r
+  IN  EFI_PEI_FILE_HANDLE         ParentFvFileHandle\r
   )\r
 {\r
-  EFI_STATUS            Status;\r
-  EFI_PEI_FV_HANDLE     FvImageHandle;\r
-  EFI_FV_INFO           FvImageInfo;\r
-  UINT32                FvAlignment;\r
-  VOID                  *FvBuffer;\r
-  EFI_PEI_HOB_POINTERS  HobPtr;\r
-\r
-  FvBuffer             = NULL;\r
-  *AuthenticationState = 0;\r
-\r
+  EFI_STATUS                    Status;\r
+  EFI_FV_INFO                   ParentFvImageInfo;\r
+  UINT32                        FvAlignment;\r
+  VOID                          *NewFvBuffer;\r
+  EFI_PEI_HOB_POINTERS          HobPtr;\r
+  EFI_PEI_FIRMWARE_VOLUME_PPI   *ParentFvPpi;\r
+  EFI_PEI_FV_HANDLE             ParentFvHandle;\r
+  EFI_FIRMWARE_VOLUME_HEADER    *FvHeader;\r
+  EFI_FV_FILE_INFO              FileInfo;\r
+  \r
   //\r
   // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already\r
   // been extracted.\r
   //\r
   HobPtr.Raw = GetHobList ();\r
   while ((HobPtr.Raw = GetNextHob (EFI_HOB_TYPE_FV2, HobPtr.Raw)) != NULL) {\r
-    if (CompareGuid (&(((EFI_FFS_FILE_HEADER *)FvFileHandle)->Name), &HobPtr.FirmwareVolume2->FileName)) {\r
+    if (CompareGuid (&(((EFI_FFS_FILE_HEADER *)ParentFvFileHandle)->Name), &HobPtr.FirmwareVolume2->FileName)) {\r
       //\r
       // this FILE has been dispatched, it will not be dispatched again.\r
       //\r
+      DEBUG ((EFI_D_INFO, "FV file %p has been dispatched!\r\n", ParentFvFileHandle));\r
       return EFI_SUCCESS;\r
     }\r
     HobPtr.Raw = GET_NEXT_HOB (HobPtr);\r
   }\r
 \r
+  ParentFvHandle = ParentFvCoreHandle->FvHandle;\r
+  ParentFvPpi    = ParentFvCoreHandle->FvPpi;\r
+  \r
   //\r
   // Find FvImage in FvFile\r
   //\r
-  Status = PeiFfsFindSectionData (\r
-             PeiServices,\r
-             EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
-             FvFileHandle,\r
-             (VOID **)&FvImageHandle\r
-             );\r
-\r
+  Status = ParentFvPpi->FindSectionByType (\r
+                          ParentFvPpi,\r
+                          EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
+                          ParentFvFileHandle,\r
+                          (VOID **)&FvHeader\r
+                          );\r
+             \r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  \r
-  //\r
-  // Collect FvImage Info.\r
-  //\r
-  Status = PeiFfsGetVolumeInfo (FvImageHandle, &FvImageInfo);\r
-  ASSERT_EFI_ERROR (Status);\r
-  \r
+\r
   //\r
   // FvAlignment must be more than 8 bytes required by FvHeader structure.\r
   //\r
-  FvAlignment = 1 << ((FvImageInfo.FvAttributes & EFI_FVB2_ALIGNMENT) >> 16);\r
+  FvAlignment = 1 << ((FvHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16);\r
   if (FvAlignment < 8) {\r
     FvAlignment = 8;\r
   }\r
@@ -898,49 +838,535 @@ ProcessFvFile (
   //\r
   // Check FvImage\r
   //\r
-  if ((UINTN) FvImageInfo.FvStart % FvAlignment != 0) {\r
-    FvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32) FvImageInfo.FvSize), FvAlignment);\r
-    if (FvBuffer == NULL) {\r
+  if ((UINTN) FvHeader % FvAlignment != 0) {\r
+    NewFvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32) FvHeader->FvLength), FvAlignment);\r
+    if (NewFvBuffer == NULL) {\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
-    CopyMem (FvBuffer, FvImageInfo.FvStart, (UINTN) FvImageInfo.FvSize);\r
-    //\r
-    // Update FvImageInfo after reload FvImage to new aligned memory\r
-    //\r
-    PeiFfsGetVolumeInfo ((EFI_PEI_FV_HANDLE) FvBuffer, &FvImageInfo);\r
+    CopyMem (NewFvBuffer, FvHeader, (UINTN) FvHeader->FvLength);\r
+    FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) NewFvBuffer;\r
   }\r
-\r
+  \r
+  Status = ParentFvPpi->GetVolumeInfo (ParentFvPpi, ParentFvHandle, &ParentFvImageInfo);\r
+  ASSERT_EFI_ERROR (Status);\r
+  \r
+  Status = ParentFvPpi->GetFileInfo (ParentFvPpi, ParentFvFileHandle, &FileInfo);\r
+  ASSERT_EFI_ERROR (Status);\r
+  \r
   //\r
   // Install FvPpi and Build FvHob\r
   //\r
-  PiLibInstallFvInfoPpi (\r
-    NULL,\r
-    FvImageInfo.FvStart,\r
-    (UINT32) FvImageInfo.FvSize,\r
-    &(FvImageInfo.FvName),\r
-    &(((EFI_FFS_FILE_HEADER*)FvFileHandle)->Name)\r
+  PeiServicesInstallFvInfoPpi (\r
+    &FvHeader->FileSystemGuid,\r
+    (VOID**) FvHeader,\r
+    (UINT32) FvHeader->FvLength,\r
+    &ParentFvImageInfo.FvName,\r
+    &FileInfo.FileName\r
     );\r
 \r
   //\r
   // Inform the extracted FvImage to Fv HOB consumer phase, i.e. DXE phase\r
   //\r
   BuildFvHob (\r
-    (EFI_PHYSICAL_ADDRESS) (UINTN) FvImageInfo.FvStart,\r
-    FvImageInfo.FvSize\r
-  );\r
+    (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,\r
+    FvHeader->FvLength\r
+    );\r
 \r
   //\r
   // Makes the encapsulated volume show up in DXE phase to skip processing of\r
   // encapsulated file again.\r
   //\r
   BuildFv2Hob (\r
-    (EFI_PHYSICAL_ADDRESS) (UINTN) FvImageInfo.FvStart,\r
-    FvImageInfo.FvSize,\r
-    &FvImageInfo.FvName,\r
-    &(((EFI_FFS_FILE_HEADER *)FvFileHandle)->Name)\r
+    (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,\r
+    FvHeader->FvLength,\r
+    &ParentFvImageInfo.FvName,\r
+    &FileInfo.FileName\r
     );\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Process a firmware volume and create a volume handle.\r
+\r
+  Create a volume handle from the information in the buffer. For\r
+  memory-mapped firmware volumes, Buffer and BufferSize refer to\r
+  the start of the firmware volume and the firmware volume size.\r
+  For non memory-mapped firmware volumes, this points to a\r
+  buffer which contains the necessary information for creating\r
+  the firmware volume handle. Normally, these values are derived\r
+  from the EFI_FIRMWARE_VOLUME_INFO_PPI.\r
+  \r
+  \r
+  @param This                   Points to this instance of the\r
+                                EFI_PEI_FIRMWARE_VOLUME_PPI.\r
+  @param Buffer                 Points to the start of the buffer.\r
+  @param BufferSize             Size of the buffer.\r
+  @param FvHandle               Points to the returned firmware volume\r
+                                handle. The firmware volume handle must\r
+                                be unique within the system. \r
+\r
+  @retval EFI_SUCCESS           Firmware volume handle created.\r
+  @retval EFI_VOLUME_CORRUPTED  Volume was corrupt.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PeiFfs2FvPpiProcessVolume (\r
+  IN  CONST  EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
+  IN  VOID                               *Buffer,\r
+  IN  UINTN                              BufferSize,\r
+  OUT EFI_PEI_FV_HANDLE                  *FvHandle\r
+  )\r
+{\r
+  EFI_STATUS          Status;\r
+  PEI_CORE_INSTANCE   *PrivateData;\r
+  EFI_PEI_FILE_HANDLE FileHandle;\r
+  VOID                *DepexData;\r
+  EFI_PEI_SERVICES    **PeiServices;\r
+  UINTN               FvIndex;\r
+  \r
+  PeiServices = (EFI_PEI_SERVICES**) GetPeiServicesTablePointer ();\r
+  PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
+  \r
+  //\r
+  // The build-in EFI_PEI_FIRMWARE_VOLUME_PPI for FFS2 support memory-mapped\r
+  // FV image and the handle is pointed to Fv image's buffer.\r
+  //\r
+  *FvHandle = (EFI_PEI_FV_HANDLE) Buffer;\r
+  \r
+  //\r
+  // Do verify for given FV buffer.\r
+  //\r
+  Status = VerifyFv ((EFI_FIRMWARE_VOLUME_HEADER*) Buffer);\r
+  if (EFI_ERROR(Status)) {\r
+    DEBUG ((EFI_D_ERROR, "Fail to verify FV which address is 0x%11p", Buffer));\r
+    return EFI_VOLUME_CORRUPTED;\r
+  }\r
+\r
+  //\r
+  // Check whether the FV has already been processed.\r
+  //\r
+  for (FvIndex = 0; FvIndex < PrivateData->FvCount; FvIndex ++) {\r
+    if (PrivateData->Fv[FvIndex].FvHandle == *FvHandle) {\r
+      DEBUG ((EFI_D_INFO, "The Fv %p has already been processed!", Buffer));\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+  \r
+  //\r
+  // Update internal PEI_CORE_FV array.\r
+  //\r
+  PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) Buffer;\r
+  PrivateData->Fv[PrivateData->FvCount].FvPpi    = (EFI_PEI_FIRMWARE_VOLUME_PPI*) This;\r
+  PrivateData->Fv[PrivateData->FvCount].FvHandle = *FvHandle;\r
+  \r
+  DEBUG ((EFI_D_INFO, \r
+          "The %dth FV start address is 0x%11p and size is 0x%08x\n", \r
+          (UINT32) PrivateData->FvCount, \r
+          (VOID *) Buffer, \r
+          BufferSize\r
+          ));\r
+  PrivateData->FvCount ++;\r
+  \r
+  FileHandle = NULL;\r
+  \r
+  do {\r
+    Status = This->FindFileByType (\r
+                     This,\r
+                     EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,\r
+                     *FvHandle,\r
+                     &FileHandle\r
+                     );\r
+    if (!EFI_ERROR (Status)) {\r
+      Status = This->FindSectionByType (\r
+                       This,\r
+                       EFI_SECTION_PEI_DEPEX,\r
+                       FileHandle,\r
+                       (VOID**)&DepexData\r
+                       );\r
+      if (!EFI_ERROR (Status)) {\r
+        if (!PeimDispatchReadiness (PeiServices, DepexData)) {\r
+          //\r
+          // Dependency is not satisfied.\r
+          //\r
+          continue;\r
+        }\r
+      }\r
+      \r
+      DEBUG ((EFI_D_INFO, "Found firmware volume Image File %p in FV[%d] %p", FileHandle, PrivateData->FvCount - 1, *FvHandle));\r
+      ProcessFvFile (&PrivateData->Fv[PrivateData->FvCount - 1], FileHandle);\r
+    }\r
+  } while (FileHandle != NULL);\r
+\r
+  return EFI_SUCCESS;\r
+}  \r
+\r
+/**\r
+  Finds the next file of the specified type.\r
+\r
+  This service enables PEI modules to discover additional firmware files. \r
+  The FileHandle must be unique within the system.\r
+\r
+  @param This           Points to this instance of the\r
+                        EFI_PEI_FIRMWARE_VOLUME_PPI.\r
+  @param SearchType     A filter to find only files of this type. Type\r
+                        EFI_FV_FILETYPE_ALL causes no filtering to be\r
+                        done.\r
+  @param FvHandle       Handle of firmware volume in which to\r
+                        search.\r
+  @param FileHandle     Points to the current handle from which to\r
+                        begin searching or NULL to start at the\r
+                        beginning of the firmware volume. Updated\r
+                        upon return to reflect the file found.\r
+\r
+  @retval EFI_SUCCESS   The file was found.\r
+  @retval EFI_NOT_FOUND The file was not found. FileHandle contains NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PeiFfs2FvPpiFindFileByType (\r
+  IN CONST  EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
+  IN        EFI_FV_FILETYPE             SearchType,\r
+  IN        EFI_PEI_FV_HANDLE           FvHandle,\r
+  IN OUT    EFI_PEI_FILE_HANDLE         *FileHandle\r
+  )\r
+{ \r
+  return FindFileEx (FvHandle, NULL, SearchType, FileHandle, NULL);\r
+}\r
+\r
+/**\r
+  Find a file within a volume by its name. \r
+  \r
+  This service searches for files with a specific name, within\r
+  either the specified firmware volume or all firmware volumes.\r
+\r
+  @param This                   Points to this instance of the\r
+                                EFI_PEI_FIRMWARE_VOLUME_PPI.\r
+  @param FileName               A pointer to the name of the file to find\r
+                                within the firmware volume.\r
+  @param FvHandle               Upon entry, the pointer to the firmware\r
+                                volume to search or NULL if all firmware\r
+                                volumes should be searched. Upon exit, the\r
+                                actual firmware volume in which the file was\r
+                                found.\r
+  @param FileHandle             Upon exit, points to the found file's\r
+                                handle or NULL if it could not be found.\r
+\r
+  @retval EFI_SUCCESS           File was found.\r
+  @retval EFI_NOT_FOUND         File was not found.\r
+  @retval EFI_INVALID_PARAMETER FvHandle or FileHandle or\r
+                                FileName was NULL.\r
+\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PeiFfs2FvPpiFindFileByName (\r
+  IN  CONST  EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
+  IN  CONST  EFI_GUID                    *FileName,\r
+  IN  EFI_PEI_FV_HANDLE                  *FvHandle,\r
+  OUT EFI_PEI_FILE_HANDLE                *FileHandle  \r
+  )\r
+{\r
+  EFI_STATUS        Status;\r
+  PEI_CORE_INSTANCE *PrivateData;\r
+  UINTN             Index;\r
+  \r
+  if ((FvHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  \r
+  if (*FvHandle != NULL) {\r
+    Status = FindFileEx (*FvHandle, FileName, 0, FileHandle, NULL);\r
+    if (Status == EFI_NOT_FOUND) {\r
+      *FileHandle = NULL;\r
+    }\r
+  } else {   \r
+    //\r
+    // If *FvHandle = NULL, so search all FV for given filename\r
+    //\r
+    Status = EFI_NOT_FOUND;\r
+    \r
+    PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer());\r
+    for (Index = 0; Index < PrivateData->FvCount; Index ++) {\r
+      //\r
+      // Only search the FV which is associated with a EFI_PEI_FIRMWARE_VOLUME_PPI instance.\r
+      //\r
+      if (PrivateData->Fv[Index].FvPpi != NULL) {\r
+        Status = FindFileEx (PrivateData->Fv[Index].FvHandle, FileName, 0, FileHandle, NULL);\r
+        if (!EFI_ERROR (Status)) {\r
+          *FvHandle = PrivateData->Fv[Index].FvHandle;\r
+        }\r
+      }\r
+    }\r
+  }\r
+  \r
+  return Status;  \r
+}  \r
+\r
+/**\r
+  Returns information about a specific file.\r
+\r
+  This function returns information about a specific\r
+  file, including its file name, type, attributes, starting\r
+  address and size. \r
+   \r
+  @param This                     Points to this instance of the\r
+                                  EFI_PEI_FIRMWARE_VOLUME_PPI.\r
+  @param FileHandle               Handle of the file.\r
+  @param FileInfo                 Upon exit, points to the file's\r
+                                  information.\r
+\r
+  @retval EFI_SUCCESS             File information returned.\r
+  @retval EFI_INVALID_PARAMETER   If FileHandle does not\r
+                                  represent a valid file.\r
+  @retval EFI_INVALID_PARAMETER   If FileInfo is NULL.\r
+  \r
+**/ \r
+EFI_STATUS\r
+EFIAPI\r
+PeiFfs2FvPpiGetFileInfo (\r
+  IN  CONST EFI_PEI_FIRMWARE_VOLUME_PPI   *This, \r
+  IN        EFI_PEI_FILE_HANDLE           FileHandle, \r
+  OUT       EFI_FV_FILE_INFO              *FileInfo\r
+  )\r
+{\r
+  UINT8                       FileState;\r
+  UINT8                       ErasePolarity;\r
+  EFI_FFS_FILE_HEADER         *FileHeader;\r
+  PEI_CORE_FV_HANDLE          *CoreFvHandle;\r
+  \r
+  if ((FileHandle == NULL) || (FileInfo == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Retrieve the FirmwareVolume which the file resides in.\r
+  //\r
+  CoreFvHandle = FileHandleToVolume (FileHandle);\r
+  if (CoreFvHandle == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (CoreFvHandle->FvHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {\r
+    ErasePolarity = 1;\r
+  } else {\r
+    ErasePolarity = 0;\r
+  }\r
+\r
+  //\r
+  // Get FileState which is the highest bit of the State \r
+  //\r
+  FileState = GetFileState (ErasePolarity, (EFI_FFS_FILE_HEADER*)FileHandle);\r
+\r
+  switch (FileState) {\r
+    case EFI_FILE_DATA_VALID:\r
+    case EFI_FILE_MARKED_FOR_UPDATE:\r
+      break;  \r
+    default:\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+\r
+  FileHeader = (EFI_FFS_FILE_HEADER *)FileHandle;\r
+  CopyMem (&FileInfo->FileName, &FileHeader->Name, sizeof(EFI_GUID));\r
+  FileInfo->FileType = FileHeader->Type;\r
+  FileInfo->FileAttributes = FileHeader->Attributes;\r
+  FileInfo->BufferSize = ((*(UINT32 *)FileHeader->Size) & 0x00FFFFFF) -  sizeof (EFI_FFS_FILE_HEADER);\r
+  FileInfo->Buffer = (FileHeader + 1);\r
+  return EFI_SUCCESS;  \r
+}  \r
+  \r
+/**\r
+  This function returns information about the firmware volume.\r
+  \r
+  @param This                     Points to this instance of the\r
+                                  EFI_PEI_FIRMWARE_VOLUME_PPI.\r
+  @param FvHandle                 Handle to the firmware handle.\r
+  @param VolumeInfo               Points to the returned firmware volume\r
+                                  information.\r
+\r
+  @retval EFI_SUCCESS             Information returned successfully.\r
+  @retval EFI_INVALID_PARAMETER   FvHandle does not indicate a valid\r
+                                  firmware volume or VolumeInfo is NULL.\r
+\r
+**/   \r
+EFI_STATUS\r
+EFIAPI\r
+PeiFfs2FvPpiGetVolumeInfo (\r
+  IN  CONST  EFI_PEI_FIRMWARE_VOLUME_PPI   *This, \r
+  IN  EFI_PEI_FV_HANDLE                    FvHandle, \r
+  OUT EFI_FV_INFO                          *VolumeInfo\r
+  )\r
+{\r
+  EFI_FIRMWARE_VOLUME_HEADER             FwVolHeader;\r
+  EFI_FIRMWARE_VOLUME_EXT_HEADER         *FwVolExHeaderInfo;\r
+\r
+  if (VolumeInfo == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  \r
+  //\r
+  // VolumeHandle may not align at 8 byte, \r
+  // but FvLength is UINT64 type, which requires FvHeader align at least 8 byte. \r
+  // So, Copy FvHeader into the local FvHeader structure.\r
+  //\r
+  CopyMem (&FwVolHeader, FvHandle, sizeof (EFI_FIRMWARE_VOLUME_HEADER));\r
+\r
+  //\r
+  // Check Fv Image Signature\r
+  //\r
+  if (FwVolHeader.Signature != EFI_FVH_SIGNATURE) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  ZeroMem (VolumeInfo, sizeof (EFI_FV_INFO));\r
+  VolumeInfo->FvAttributes  = FwVolHeader.Attributes;\r
+  VolumeInfo->FvStart       = (VOID *) FvHandle;\r
+  VolumeInfo->FvSize        = FwVolHeader.FvLength;\r
+  CopyMem (&VolumeInfo->FvFormat, &FwVolHeader.FileSystemGuid, sizeof(EFI_GUID));\r
+\r
+  if (FwVolHeader.ExtHeaderOffset != 0) {\r
+    FwVolExHeaderInfo = (EFI_FIRMWARE_VOLUME_EXT_HEADER*)(((UINT8 *)FvHandle) + FwVolHeader.ExtHeaderOffset);\r
+    CopyMem (&VolumeInfo->FvName, &FwVolExHeaderInfo->FvName, sizeof(EFI_GUID));\r
+  }\r
+  \r
+  return EFI_SUCCESS;  \r
+}    \r
+\r
+/**\r
+  Find the next matching section in the firmware file.\r
+  \r
+  This service enables PEI modules to discover sections\r
+  of a given type within a valid file.\r
+  \r
+  @param This             Points to this instance of the\r
+                          EFI_PEI_FIRMWARE_VOLUME_PPI.\r
+  @param SearchType       A filter to find only sections of this\r
+                          type.\r
+  @param FileHandle       Handle of firmware file in which to\r
+                          search.\r
+  @param SectionData      Updated upon  return to point to the\r
+                          section found.\r
+  \r
+  @retval EFI_SUCCESS     Section was found.\r
+  @retval EFI_NOT_FOUND   Section of the specified type was not\r
+                          found. SectionData contains NULL.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PeiFfs2FvPpiFindSectionByType (\r
+  IN  CONST EFI_PEI_FIRMWARE_VOLUME_PPI    *This,\r
+  IN        EFI_SECTION_TYPE               SearchType,\r
+  IN        EFI_PEI_FILE_HANDLE            FileHandle,\r
+  OUT VOID                                 **SectionData\r
+  )\r
+{\r
+  EFI_FFS_FILE_HEADER                     *FfsFileHeader;\r
+  UINT32                                  FileSize;\r
+  EFI_COMMON_SECTION_HEADER               *Section;\r
+  \r
+  FfsFileHeader = (EFI_FFS_FILE_HEADER *)(FileHandle);\r
+\r
+  //\r
+  // Size is 24 bits wide so mask upper 8 bits. \r
+  // Does not include FfsFileHeader header size\r
+  // FileSize is adjusted to FileOccupiedSize as it is 8 byte aligned.\r
+  //\r
+  Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);\r
+  FileSize = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;\r
+  FileSize -= sizeof (EFI_FFS_FILE_HEADER);\r
+\r
+  return ProcessSection (\r
+           GetPeiServicesTablePointer (),\r
+           SearchType, \r
+           Section, \r
+           FileSize, \r
+           SectionData\r
+           );  \r
+}  \r
+\r
+/**\r
+  Convert the handle of FV to pointer of corresponding PEI_CORE_FV_HANDLE.\r
+  \r
+  @param FvHandle   The handle of a FV.\r
+  \r
+  @retval NULL if can not find.\r
+  @return Pointer of corresponding PEI_CORE_FV_HANDLE. \r
+**/\r
+PEI_CORE_FV_HANDLE *\r
+FvHandleToCoreHandle (\r
+  IN EFI_PEI_FV_HANDLE  FvHandle\r
+  )\r
+{\r
+  UINTN             Index;\r
+  PEI_CORE_INSTANCE *PrivateData;\r
+  \r
+  PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer());  \r
+  for (Index = 0; Index < PrivateData->FvCount; Index ++) {\r
+    if (FvHandle == PrivateData->Fv[Index].FvHandle) {\r
+      return &PrivateData->Fv[Index];\r
+    }\r
+  }\r
+  \r
+  return NULL;\r
+}  \r
+\r
+/**\r
+  Get instance of PEI_CORE_FV_HANDLE for next volume according to given index.\r
+  \r
+  This routine also will install FvInfo ppi for FV hob in PI ways.\r
+  \r
+  @param Private    Pointer of PEI_CORE_INSTANCE\r
+  @param Instance   The index of FV want to be searched.\r
+  \r
+  @return Instance of PEI_CORE_FV_HANDLE.\r
+**/\r
+PEI_CORE_FV_HANDLE *\r
+FindNextCoreFvHandle (\r
+  IN PEI_CORE_INSTANCE  *Private,\r
+  IN UINTN              Instance\r
+  )\r
+{\r
+  UINTN                    Index;\r
+  BOOLEAN                  Match;\r
+  EFI_HOB_FIRMWARE_VOLUME  *FvHob;\r
+  \r
+  //\r
+  // Handle Framework FvHob and Install FvInfo Ppi for it.\r
+  //\r
+  if (FeaturePcdGet (PcdFrameworkCompatibilitySupport)) {\r
+    //\r
+    // Loop to search the wanted FirmwareVolume which supports FFS\r
+    //\r
+    FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetFirstHob (EFI_HOB_TYPE_FV);\r
+    while (FvHob != NULL) {\r
+      for (Index = 0, Match = FALSE; Index < Private->FvCount; Index++) {\r
+        if ((EFI_PEI_FV_HANDLE)(UINTN)FvHob->BaseAddress == Private->Fv[Index].FvHeader) {\r
+          Match = TRUE;\r
+          break;\r
+        }\r
+      }\r
+      //\r
+      // If Not Found, Install FvInfo Ppi for it.\r
+      //\r
+      if (!Match) {\r
+        PeiServicesInstallFvInfoPpi (\r
+          &(((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)FvHob->BaseAddress)->FileSystemGuid),\r
+          (VOID *)(UINTN)FvHob->BaseAddress,\r
+          (UINT32)FvHob->Length,\r
+          NULL,\r
+          NULL\r
+          );\r
+      }\r
+      FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetNextHob (EFI_HOB_TYPE_FV, (VOID *)((UINTN)FvHob + FvHob->Header.HobLength)); \r
+    }\r
+  }\r
+\r
+  if (Instance >= Private->FvCount) {\r
+    return NULL;\r
+  }\r
 \r
+  return &Private->Fv[Instance];\r
+}  \r