]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Pei/FwVol/FwVol.c
MdeModulePkg PeiCore: Check error status when processing boot FV
[mirror_edk2.git] / MdeModulePkg / Core / Pei / FwVol / FwVol.c
index 1af4a760362c0518b8fdb76bca7cabf02636642e..4150b338b211847a17cf2d2968a11b11e1e46976 100644 (file)
@@ -1,7 +1,9 @@
 /** @file\r
-\r
-Copyright (c) 2006 - 2007, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
+  Pei Core Firmware File System service routines.\r
+  \r
+Copyright (c) 2015 HP Development Company, L.P.\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
+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
 http://opensource.org/licenses/bsd-license.php                                            \r
@@ -9,50 +11,139 @@ http://opensource.org/licenses/bsd-license.php
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
 \r
-Module Name:\r
+**/\r
+\r
+#include "FwVol.h"\r
+\r
+EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnFvInfoList[] = {\r
+  {\r
+    EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,\r
+    &gEfiPeiFirmwareVolumeInfoPpiGuid,\r
+    FirmwareVolmeInfoPpiNotifyCallback \r
+  },\r
+  {\r
+    (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
+    &gEfiPeiFirmwareVolumeInfo2PpiGuid,\r
+    FirmwareVolmeInfoPpiNotifyCallback \r
+  }\r
+};\r
 \r
-  FwVol.c\r
+PEI_FW_VOL_INSTANCE mPeiFfs2FwVol = {\r
+  PEI_FW_VOL_SIGNATURE,\r
+  FALSE,\r
+  {\r
+    PeiFfsFvPpiProcessVolume,\r
+    PeiFfsFvPpiFindFileByType,\r
+    PeiFfsFvPpiFindFileByName,\r
+    PeiFfsFvPpiGetFileInfo,\r
+    PeiFfsFvPpiGetVolumeInfo,\r
+    PeiFfsFvPpiFindSectionByType,\r
+    PeiFfsFvPpiGetFileInfo2,\r
+    PeiFfsFvPpiFindSectionByType2,\r
+    EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE,\r
+    EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION\r
+  }\r
+};\r
 \r
-Abstract:\r
+PEI_FW_VOL_INSTANCE mPeiFfs3FwVol = {\r
+  PEI_FW_VOL_SIGNATURE,\r
+  TRUE,\r
+  {\r
+    PeiFfsFvPpiProcessVolume,\r
+    PeiFfsFvPpiFindFileByType,\r
+    PeiFfsFvPpiFindFileByName,\r
+    PeiFfsFvPpiGetFileInfo,\r
+    PeiFfsFvPpiGetVolumeInfo,\r
+    PeiFfsFvPpiFindSectionByType,\r
+    PeiFfsFvPpiGetFileInfo2,\r
+    PeiFfsFvPpiFindSectionByType2,\r
+    EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE,\r
+    EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION\r
+  }\r
+};\r
+            \r
+EFI_PEI_PPI_DESCRIPTOR  mPeiFfs2FvPpiList = {\r
+  (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
+  &gEfiFirmwareFileSystem2Guid,\r
+  &mPeiFfs2FwVol.Fv\r
+};\r
 \r
-  Pei Core Firmware File System service routines.\r
+EFI_PEI_PPI_DESCRIPTOR  mPeiFfs3FvPpiList = {\r
+  (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
+  &gEfiFirmwareFileSystem3Guid,\r
+  &mPeiFfs3FwVol.Fv\r
+};\r
 \r
+/**\r
+Required Alignment   Alignment Value in FFS   FFS_ATTRIB_DATA_ALIGNMENT2   Alignment Value in\r
+(bytes)              Attributes Field         in FFS Attributes Field      Firmware Volume Interfaces\r
+1                               0                          0                            0\r
+16                              1                          0                            4\r
+128                             2                          0                            7\r
+512                             3                          0                            9\r
+1 KB                            4                          0                            10\r
+4 KB                            5                          0                            12\r
+32 KB                           6                          0                            15\r
+64 KB                           7                          0                            16\r
+128 KB                          0                          1                            17\r
+256 KB                          1                          1                            18\r
+512 KB                          2                          1                            19\r
+1 MB                            3                          1                            20\r
+2 MB                            4                          1                            21\r
+4 MB                            5                          1                            22\r
+8 MB                            6                          1                            23\r
+16 MB                           7                          1                            24\r
 **/\r
+UINT8 mFvAttributes[] = {0, 4, 7, 9, 10, 12, 15, 16};\r
+UINT8 mFvAttributes2[] = {17, 18, 19, 20, 21, 22, 23, 24};\r
 \r
-#include <PeiMain.h>\r
-\r
-STATIC EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnFvInfoList = {\r
-  (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
-  &gEfiPeiFirmwareVolumeInfoPpiGuid,\r
-  FirmwareVolmeInfoPpiNotifyCallback \r
-};\r
+/**\r
+  Convert the FFS File Attributes to FV File Attributes\r
 \r
+  @param  FfsAttributes              The attributes of UINT8 type.\r
 \r
-#define GET_OCCUPIED_SIZE(ActualSize, Alignment) \\r
-  (ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) & ((Alignment) - 1))\r
+  @return The attributes of EFI_FV_FILE_ATTRIBUTES\r
 \r
-STATIC\r
-EFI_FFS_FILE_STATE\r
-GetFileState(\r
-  IN UINT8                ErasePolarity,\r
-  IN EFI_FFS_FILE_HEADER  *FfsHeader\r
+**/\r
+EFI_FV_FILE_ATTRIBUTES\r
+FfsAttributes2FvFileAttributes (\r
+  IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes\r
   )\r
-/*++\r
+{\r
+  UINT8                     DataAlignment;\r
+  EFI_FV_FILE_ATTRIBUTES    FileAttribute;\r
 \r
-Routine Description:\r
+  DataAlignment = (UINT8) ((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT) >> 3);\r
+  ASSERT (DataAlignment < 8);\r
+\r
+  if ((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT_2) != 0) {\r
+    FileAttribute = (EFI_FV_FILE_ATTRIBUTES) mFvAttributes2[DataAlignment];\r
+  } else {\r
+    FileAttribute = (EFI_FV_FILE_ATTRIBUTES) mFvAttributes[DataAlignment];\r
+  }\r
 \r
-  Returns the highest bit set of the State field\r
+  if ((FfsAttributes & FFS_ATTRIB_FIXED) == FFS_ATTRIB_FIXED) {\r
+    FileAttribute |= EFI_FV_FILE_ATTRIB_FIXED;\r
+  }\r
 \r
-Arguments:\r
+  return FileAttribute;\r
+}\r
 \r
-  ErasePolarity   - Erase Polarity  as defined by EFI_FVB2_ERASE_POLARITY\r
-                    in the Attributes field.\r
-  FfsHeader       - Pointer to FFS File Header.\r
+/**\r
+  Returns the file state set by the highest zero bit in the State field\r
 \r
-Returns:\r
-  Returns the highest bit in the State field\r
+  @param ErasePolarity   Erase Polarity  as defined by EFI_FVB2_ERASE_POLARITY\r
+                         in the Attributes field.\r
+  @param FfsHeader       Pointer to FFS File Header.\r
 \r
---*/\r
+  @retval EFI_FFS_FILE_STATE File state is set by the highest none zero bit \r
+                             in the header State field.\r
+**/\r
+EFI_FFS_FILE_STATE\r
+GetFileState(\r
+  IN UINT8                ErasePolarity,\r
+  IN EFI_FFS_FILE_HEADER  *FfsHeader\r
+  )\r
 {\r
   EFI_FFS_FILE_STATE  FileState;\r
   EFI_FFS_FILE_STATE  HighestBit;\r
@@ -62,7 +153,10 @@ Returns:
   if (ErasePolarity != 0) {\r
     FileState = (EFI_FFS_FILE_STATE)~FileState;\r
   }\r
-\r
+  \r
+  //\r
+  // Get file state set by its highest none zero bit.\r
+  //\r
   HighestBit = 0x80;\r
   while (HighestBit != 0 && (HighestBit & FileState) == 0) {\r
     HighestBit >>= 1;\r
@@ -71,134 +165,140 @@ Returns:
   return HighestBit;\r
 } \r
 \r
-STATIC\r
+/**\r
+  Calculates the checksum of the header of a file.\r
+\r
+  @param FileHeader      Pointer to FFS File Header.\r
+\r
+  @return Checksum of the header.\r
+          Zero means the header is good.\r
+          Non-zero means the header is bad.\r
+**/\r
 UINT8\r
 CalculateHeaderChecksum (\r
   IN EFI_FFS_FILE_HEADER  *FileHeader\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Calculates the checksum of the header of a file.\r
-\r
-Arguments:\r
-\r
-  FileHeader       - Pointer to FFS File Header.\r
-\r
-Returns:\r
-  Checksum of the header.\r
-  \r
-  The header is zero byte checksum.\r
-    - Zero means the header is good.\r
-    - Non-zero means the header is bad.\r
-    \r
-  \r
-Bugbug: For PEI performance reason, we comments this code at this time.\r
---*/\r
 {\r
-  UINT8   *ptr;\r
-  UINTN   Index;\r
-  UINT8   Sum;\r
-  \r
-  Sum = 0;\r
-  ptr = (UINT8 *)FileHeader;\r
+  EFI_FFS_FILE_HEADER2 TestFileHeader;\r
 \r
-  for (Index = 0; Index < sizeof(EFI_FFS_FILE_HEADER) - 3; Index += 4) {\r
-    Sum = (UINT8)(Sum + ptr[Index]);\r
-    Sum = (UINT8)(Sum + ptr[Index+1]);\r
-    Sum = (UINT8)(Sum + ptr[Index+2]);\r
-    Sum = (UINT8)(Sum + ptr[Index+3]);\r
-  }\r
+  if (IS_FFS_FILE2 (FileHeader)) {\r
+    CopyMem (&TestFileHeader, FileHeader, sizeof (EFI_FFS_FILE_HEADER2));\r
+    //\r
+    // Ingore State and File field in FFS header.\r
+    //\r
+    TestFileHeader.State = 0;\r
+    TestFileHeader.IntegrityCheck.Checksum.File = 0;\r
 \r
-  for (; Index < sizeof(EFI_FFS_FILE_HEADER); Index++) {\r
-    Sum = (UINT8)(Sum + ptr[Index]);\r
-  }\r
-  \r
-  //\r
-  // State field (since this indicates the different state of file). \r
-  //\r
-  Sum = (UINT8)(Sum - FileHeader->State);\r
-  //\r
-  // Checksum field of the file is not part of the header checksum.\r
-  //\r
-  Sum = (UINT8)(Sum - FileHeader->IntegrityCheck.Checksum.File);\r
+    return CalculateSum8 ((CONST UINT8 *) &TestFileHeader, sizeof (EFI_FFS_FILE_HEADER2));\r
+  } else {\r
+    CopyMem (&TestFileHeader, FileHeader, sizeof (EFI_FFS_FILE_HEADER));\r
+    //\r
+    // Ingore State and File field in FFS header.\r
+    //\r
+    TestFileHeader.State = 0;\r
+    TestFileHeader.IntegrityCheck.Checksum.File = 0;\r
 \r
-  return Sum;\r
+    return CalculateSum8 ((CONST UINT8 *) &TestFileHeader, sizeof (EFI_FFS_FILE_HEADER));\r
+  }\r
 }\r
 \r
-STATIC\r
-BOOLEAN\r
-EFIAPI\r
-PeiFileHandleToVolume (\r
-  IN   EFI_PEI_FILE_HANDLE     FileHandle,\r
-  OUT  EFI_PEI_FV_HANDLE       *VolumeHandle\r
+/**\r
+  Find FV handler according to FileHandle in that FV.\r
+\r
+  @param FileHandle      Handle of file image\r
+  \r
+  @return Pointer to instance of PEI_CORE_FV_HANDLE.\r
+**/\r
+PEI_CORE_FV_HANDLE*\r
+FileHandleToVolume (\r
+  IN   EFI_PEI_FILE_HANDLE          FileHandle\r
   )\r
 {\r
   UINTN                       Index;\r
   PEI_CORE_INSTANCE           *PrivateData;\r
   EFI_FIRMWARE_VOLUME_HEADER  *FwVolHeader;\r
+  UINTN                       BestIndex;\r
 \r
   PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (GetPeiServicesTablePointer ());\r
+  BestIndex   = PrivateData->FvCount;\r
+  \r
+  //\r
+  // Find the best matched FV image that includes this FileHandle.\r
+  // FV may include the child FV, and they are in the same continuous space. \r
+  // If FileHandle is from the child FV, the updated logic can find its matched FV.\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
+      if (BestIndex == PrivateData->FvCount) {\r
+        BestIndex = Index;\r
+      } else {\r
+        if ((UINT64) (UINTN) PrivateData->Fv[BestIndex].FvHeader < (UINT64) (UINTN) FwVolHeader) {\r
+          BestIndex = Index;\r
+        }\r
+      }\r
     }\r
   }\r
-  return FALSE;\r
+\r
+  if (BestIndex < PrivateData->FvCount) {\r
+    return &PrivateData->Fv[BestIndex];\r
+  }\r
+\r
+  return NULL;\r
 }\r
 \r
+/**\r
+  Given the input file pointer, search for the first 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
+  If SearchType is EFI_FV_FILETYPE_ALL, the first FFS file will return without check its file type.\r
+  If SearchType is PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE, \r
+  the first PEIM, or COMBINED PEIM or FV file type FFS file will return.  \r
+\r
+  @param FvHandle        Pointer to the FV header of the volume to search\r
+  @param FileName        File name\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 FileHandle      This parameter must point to a valid FFS volume.\r
+  @param AprioriFile     Pointer to AprioriFile image in this FV if has\r
+\r
+  @return EFI_NOT_FOUND  No files matching the search criteria were found\r
+  @retval EFI_SUCCESS    Success to search given file\r
 \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
   IN OUT    EFI_PEI_FILE_HANDLE      *FileHandle,\r
-  IN OUT    EFI_PEI_FV_HANDLE        *AprioriFile  OPTIONAL\r
-  )\r
-/*++\r
-\r
-Routine Description:\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
-Arguments:\r
-    PeiServices - Pointer to the PEI Core Services Table.\r
-    SearchType - Filter to find only files of this type.\r
-      Type EFI_FV_FILETYPE_ALL causes no filtering to be done.\r
-    FwVolHeader - Pointer to the FV header of the volume to search.\r
-      This parameter must point to a valid FFS volume.\r
-    FileHeader  - Pointer to the current file from which to begin searching.\r
-      This pointer will be updated upon return to reflect the file found.\r
-    Flag        - Indicator for if this is for PEI Dispath search \r
-    \r
-Returns:\r
-    EFI_NOT_FOUND - No files matching the search criteria were found\r
-    EFI_SUCCESS\r
-\r
---*/\r
+  IN OUT    EFI_PEI_FILE_HANDLE      *AprioriFile  OPTIONAL\r
+  )\r
 {\r
-  EFI_FIRMWARE_VOLUME_HEADER           *FwVolHeader;\r
+  EFI_FIRMWARE_VOLUME_HEADER            *FwVolHeader;\r
+  EFI_FIRMWARE_VOLUME_EXT_HEADER        *FwVolExtHeader;\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
+  UINT8                                 DataCheckSum;\r
+  BOOLEAN                               IsFfs3Fv;\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
+  IsFfs3Fv = CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem3Guid);\r
+\r
   FvLength = FwVolHeader->FvLength;\r
-  if (FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {\r
+  if ((FwVolHeader->Attributes & EFI_FVB2_ERASE_POLARITY) != 0) {\r
     ErasePolarity = 1;\r
   } else {\r
     ErasePolarity = 0;\r
@@ -210,17 +310,29 @@ Returns:
   // start from the FileHeader.\r
   //\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
+      // Searching for files starts on an 8 byte aligned boundary after the end of the Extended Header if it exists.\r
+      //\r
+      FwVolExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *) ((UINT8 *) FwVolHeader + FwVolHeader->ExtHeaderOffset);\r
+      FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FwVolExtHeader + FwVolExtHeader->ExtHeaderSize);\r
+      FfsFileHeader = (EFI_FFS_FILE_HEADER *) ALIGN_POINTER (FfsFileHeader, 8);\r
+    } else {\r
+      FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *) FwVolHeader + FwVolHeader->HeaderLength);\r
     }\r
   } else {\r
+    if (IS_FFS_FILE2 (*FileHeader)) {\r
+      if (!IsFfs3Fv) {\r
+        DEBUG ((EFI_D_ERROR, "It is a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &(*FileHeader)->Name));\r
+      }\r
+      FileLength = FFS_FILE2_SIZE (*FileHeader);\r
+      ASSERT (FileLength > 0x00FFFFFF);\r
+    } else {\r
+      FileLength = FFS_FILE_SIZE (*FileHeader);\r
+    }\r
     //\r
-    // Length is 24 bits wide so mask upper 8 bits\r
     // FileLength is adjusted to FileOccupiedSize as it is 8 byte aligned.\r
     //\r
-    FileLength = *(UINT32 *)(*FileHeader)->Size & 0x00FFFFFF;\r
     FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);\r
     FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)*FileHeader + FileOccupiedSize);\r
   }\r
@@ -235,9 +347,18 @@ Returns:
     FileState = GetFileState (ErasePolarity, FfsFileHeader);\r
     switch (FileState) {\r
 \r
+    case EFI_FILE_HEADER_CONSTRUCTION:\r
     case EFI_FILE_HEADER_INVALID:\r
-      FileOffset += sizeof(EFI_FFS_FILE_HEADER);\r
-      FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + sizeof(EFI_FFS_FILE_HEADER));\r
+      if (IS_FFS_FILE2 (FfsFileHeader)) {\r
+        if (!IsFfs3Fv) {\r
+          DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));\r
+        }\r
+        FileOffset    += sizeof (EFI_FFS_FILE_HEADER2);\r
+        FfsFileHeader =  (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2));\r
+      } else {\r
+        FileOffset    += sizeof (EFI_FFS_FILE_HEADER);\r
+        FfsFileHeader =  (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));\r
+      }\r
       break;\r
         \r
     case EFI_FILE_DATA_VALID:\r
@@ -248,8 +369,34 @@ Returns:
         return EFI_NOT_FOUND;\r
       }\r
 \r
-      FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;\r
-      FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8);\r
+      if (IS_FFS_FILE2 (FfsFileHeader)) {\r
+        FileLength = FFS_FILE2_SIZE (FfsFileHeader);\r
+        ASSERT (FileLength > 0x00FFFFFF);\r
+        FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);\r
+        if (!IsFfs3Fv) {\r
+          DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));\r
+          FileOffset += FileOccupiedSize;\r
+          FfsFileHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FfsFileHeader + FileOccupiedSize);\r
+          break;\r
+        }\r
+      } else {\r
+        FileLength = FFS_FILE_SIZE (FfsFileHeader);\r
+        FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);\r
+      }\r
+\r
+      DataCheckSum = FFS_FIXED_CHECKSUM;\r
+      if ((FfsFileHeader->Attributes & FFS_ATTRIB_CHECKSUM) == FFS_ATTRIB_CHECKSUM) {\r
+        if (IS_FFS_FILE2 (FfsFileHeader)) {\r
+          DataCheckSum = CalculateCheckSum8 ((CONST UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2), FileLength - sizeof(EFI_FFS_FILE_HEADER2));\r
+        } else {\r
+          DataCheckSum = CalculateCheckSum8 ((CONST UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER), FileLength - sizeof(EFI_FFS_FILE_HEADER));\r
+        }\r
+      }\r
+      if (FfsFileHeader->IntegrityCheck.Checksum.File != DataCheckSum) {\r
+        ASSERT (FALSE);\r
+        *FileHeader = NULL;\r
+        return EFI_NOT_FOUND;\r
+      }\r
 \r
       if (FileName != NULL) {\r
         if (CompareGuid (&FfsFileHeader->Name, (EFI_GUID*)FileName)) {\r
@@ -276,15 +423,23 @@ Returns:
         return EFI_SUCCESS;\r
       }\r
 \r
-      FileOffset += FileOccupiedSize; \r
-      FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);\r
+      FileOffset    += FileOccupiedSize; \r
+      FfsFileHeader =  (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);\r
       break;\r
     \r
     case EFI_FILE_DELETED:\r
-      FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;\r
-      FileOccupiedSize = GET_OCCUPIED_SIZE(FileLength, 8);\r
-      FileOffset += FileOccupiedSize;\r
-      FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);\r
+      if (IS_FFS_FILE2 (FfsFileHeader)) {\r
+        if (!IsFfs3Fv) {\r
+          DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));\r
+        }\r
+        FileLength = FFS_FILE2_SIZE (FfsFileHeader);\r
+        ASSERT (FileLength > 0x00FFFFFF);\r
+      } else {\r
+        FileLength = FFS_FILE_SIZE (FfsFileHeader);\r
+      }\r
+      FileOccupiedSize =  GET_OCCUPIED_SIZE(FileLength, 8);\r
+      FileOffset       += FileOccupiedSize;\r
+      FfsFileHeader    =  (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);\r
       break;\r
 \r
     default:\r
@@ -297,49 +452,96 @@ Returns:
   return EFI_NOT_FOUND;  \r
 }\r
 \r
+/**\r
+  Initialize PeiCore Fv List.\r
+\r
+  @param PrivateData     - Pointer to PEI_CORE_INSTANCE.\r
+  @param SecCoreData     - Pointer to EFI_SEC_PEI_HAND_OFF.\r
+**/\r
 VOID \r
 PeiInitializeFv (\r
   IN  PEI_CORE_INSTANCE           *PrivateData,\r
   IN CONST EFI_SEC_PEI_HAND_OFF   *SecCoreData\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Initialize PeiCore Fv List.\r
-\r
-Arguments:\r
-  PrivateData     - Pointer to PEI_CORE_INSTANCE.\r
-  SecCoreData     - Pointer to EFI_SEC_PEI_HAND_OFF.\r
-\r
-Returns:\r
-  NONE  \r
-  \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
+  PeiServicesInstallPpi (&mPeiFfs2FvPpiList);\r
 \r
-  PrivateData->AllFvCount = 1;\r
-  PrivateData->AllFv[0] = (EFI_PEI_FV_HANDLE)PrivateData->Fv[0].FvHeader;\r
+  //\r
+  // Install FV_PPI for FFS3 file system.\r
+  //\r
+  PeiServicesInstallPpi (&mPeiFfs3FvPpiList);\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
+  Status = FvPpi->ProcessVolume (\r
+                    FvPpi,\r
+                    SecCoreData->BootFirmwareVolumeBase,\r
+                    (UINTN)BfvHeader->FvLength,\r
+                    &FvHandle\r
+                    );\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
-  // Post a call-back for the FvInfoPPI services to expose\r
+  // Update internal PEI_CORE_FV array.\r
+  //\r
+  PrivateData->Fv[PrivateData->FvCount].FvHeader = BfvHeader;\r
+  PrivateData->Fv[PrivateData->FvCount].FvPpi    = FvPpi;\r
+  PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;\r
+  PrivateData->Fv[PrivateData->FvCount].AuthenticationStatus = 0;\r
+  DEBUG ((\r
+    EFI_D_INFO, \r
+    "The %dth FV start address is 0x%11p, size is 0x%08x, handle is 0x%p\n", \r
+    (UINT32) PrivateData->FvCount, \r
+    (VOID *) BfvHeader, \r
+    (UINT32) BfvHeader->FvLength,\r
+    FvHandle\r
+    ));    \r
+  PrivateData->FvCount ++;\r
+                            \r
+  //\r
+  // Post a call-back for the FvInfoPPI and FvInfo2PPI services to expose\r
   // additional Fvs to PeiCore.\r
   //\r
-  Status = PeiServicesNotifyPpi (&mNotifyOnFvInfoList);\r
+  Status = PeiServicesNotifyPpi (mNotifyOnFvInfoList);\r
   ASSERT_EFI_ERROR (Status);\r
 \r
 }\r
+  \r
+/**\r
+  Process Firmware Volum Information once FvInfoPPI or FvInfo2PPI install.\r
+  The FV Info will be registered into PeiCore private data structure.\r
+  And search the inside FV image, if found, the new FV INFO(2) PPI will be installed.\r
+\r
+  @param PeiServices       An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
+  @param NotifyDescriptor  Address of the notification descriptor data structure.\r
+  @param Ppi               Address of the PPI that was installed.\r
+\r
+  @retval EFI_SUCCESS    The FV Info is registered into PeiCore private data structure.\r
+  @return if not EFI_SUCESS, fail to verify FV.\r
 \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 FirmwareVolmeInfoPpiNotifyCallback (\r
@@ -347,74 +549,125 @@ FirmwareVolmeInfoPpiNotifyCallback (
   IN EFI_PEI_NOTIFY_DESCRIPTOR     *NotifyDescriptor,\r
   IN VOID                          *Ppi\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Process Firmware Volum Information once FvInfoPPI install.\r
-\r
-Arguments:\r
-\r
-  PeiServices - General purpose services available to every PEIM.\r
-    \r
-Returns:\r
-\r
-  Status -  EFI_SUCCESS if the interface could be successfully\r
-            installed\r
-\r
---*/\r
 {\r
-  UINT8                                 FvCount;\r
-  EFI_PEI_FIRMWARE_VOLUME_INFO_PPI      *Fv;\r
+  EFI_PEI_FIRMWARE_VOLUME_INFO2_PPI     FvInfo2Ppi;\r
+  EFI_PEI_FIRMWARE_VOLUME_PPI           *FvPpi;\r
   PEI_CORE_INSTANCE                     *PrivateData;\r
+  EFI_STATUS                            Status;\r
+  EFI_PEI_FV_HANDLE                     FvHandle;\r
+  UINTN                                 FvIndex;\r
   EFI_PEI_FILE_HANDLE                   FileHandle;\r
   VOID                                  *DepexData;\r
-  UINT32                                AuthenticationStatus;\r
-  EFI_STATUS                            Status;\r
-  \r
-  FileHandle   = NULL;\r
-  DepexData    = NULL;\r
+  BOOLEAN                               IsFvInfo2;\r
+  UINTN                                 CurFvCount;\r
+\r
   Status       = EFI_SUCCESS;\r
   PrivateData  = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
 \r
-  if (PrivateData->FvCount >= FixedPcdGet32 (PcdPeiCoreMaxFvSupported)) {\r
-    ASSERT (FALSE);\r
+  if (CompareGuid (NotifyDescriptor->Guid, &gEfiPeiFirmwareVolumeInfo2PpiGuid)) {\r
+    //\r
+    // It is FvInfo2PPI.\r
+    //\r
+    CopyMem (&FvInfo2Ppi, Ppi, sizeof (EFI_PEI_FIRMWARE_VOLUME_INFO2_PPI));\r
+    IsFvInfo2 = TRUE;\r
+  } else {\r
+    //\r
+    // It is FvInfoPPI.\r
+    //\r
+    CopyMem (&FvInfo2Ppi, Ppi, sizeof (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI));\r
+    FvInfo2Ppi.AuthenticationStatus = 0;\r
+    IsFvInfo2 = FALSE;\r
+  }\r
+\r
+  if (CompareGuid (&FvInfo2Ppi.FvFormat, &gEfiFirmwareFileSystem2Guid)) {\r
+    //\r
+    // gEfiFirmwareFileSystem2Guid is specified for FvFormat, then here to check the\r
+    // FileSystemGuid pointed by FvInfo against gEfiFirmwareFileSystem2Guid to make sure\r
+    // FvInfo has the firmware file system 2 format.\r
+    //\r
+    // If the ASSERT really appears, FvFormat needs to be specified correctly, for example,\r
+    // gEfiFirmwareFileSystem3Guid can be used for firmware file system 3 format, or\r
+    // ((EFI_FIRMWARE_VOLUME_HEADER *) FvInfo)->FileSystemGuid can be just used for both\r
+    // firmware file system 2 and 3 format.\r
+    //\r
+    ASSERT (CompareGuid (&(((EFI_FIRMWARE_VOLUME_HEADER *) FvInfo2Ppi.FvInfo)->FileSystemGuid), &gEfiFirmwareFileSystem2Guid));\r
   }\r
 \r
-  Fv = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *)Ppi;\r
+  //\r
+  // Locate the corresponding FV_PPI according to founded FV's format guid\r
+  //\r
+  Status = PeiServicesLocatePpi (\r
+             &FvInfo2Ppi.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, FvInfo2Ppi.FvInfo, FvInfo2Ppi.FvInfoSize, &FvHandle);\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "Fail to process new found FV, FV may be corrupted!\n"));\r
+      return Status;\r
+    }\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
+    //\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
+        if (IsFvInfo2 && (FvInfo2Ppi.AuthenticationStatus != PrivateData->Fv[FvIndex].AuthenticationStatus)) {\r
+          PrivateData->Fv[FvIndex].AuthenticationStatus = FvInfo2Ppi.AuthenticationStatus;\r
+          DEBUG ((EFI_D_INFO, "Update AuthenticationStatus of the %dth FV to 0x%x!\n", FvIndex, FvInfo2Ppi.AuthenticationStatus));          \r
+        }\r
+        DEBUG ((EFI_D_INFO, "The Fv %p has already been processed!\n", FvInfo2Ppi.FvInfo));\r
         return EFI_SUCCESS;\r
       }\r
     }\r
-    PrivateData->Fv[PrivateData->FvCount++].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*)Fv->FvInfo;\r
+\r
+    if (PrivateData->FvCount >= PcdGet32 (PcdPeiCoreMaxFvSupported)) {\r
+      DEBUG ((EFI_D_ERROR, "The number of Fv Images (%d) exceed the max supported FVs (%d) in Pei", PrivateData->FvCount + 1, PcdGet32 (PcdPeiCoreMaxFvSupported)));\r
+      DEBUG ((EFI_D_ERROR, "PcdPeiCoreMaxFvSupported value need be reconfigurated in DSC"));\r
+      ASSERT (FALSE);\r
+    }\r
 \r
     //\r
-    // Only add FileSystem2 Fv to the All list\r
+    // Update internal PEI_CORE_FV array.\r
     //\r
-    PrivateData->AllFv[PrivateData->AllFvCount++] = (EFI_PEI_FV_HANDLE)Fv->FvInfo;\r
-    \r
-    DEBUG ((EFI_D_INFO, "The %dth FvImage start address is 0x%10p and size is 0x%08x\n", PrivateData->AllFvCount, (VOID *) Fv->FvInfo, Fv->FvInfoSize));\r
+    PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) FvInfo2Ppi.FvInfo;\r
+    PrivateData->Fv[PrivateData->FvCount].FvPpi    = FvPpi;\r
+    PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;\r
+    PrivateData->Fv[PrivateData->FvCount].AuthenticationStatus = FvInfo2Ppi.AuthenticationStatus;\r
+    CurFvCount = PrivateData->FvCount;\r
+    DEBUG ((\r
+      EFI_D_INFO, \r
+      "The %dth FV start address is 0x%11p, size is 0x%08x, handle is 0x%p\n", \r
+      (UINT32) CurFvCount,\r
+      (VOID *) FvInfo2Ppi.FvInfo, \r
+      FvInfo2Ppi.FvInfoSize,\r
+      FvHandle\r
+      ));    \r
+    PrivateData->FvCount ++;\r
+\r
     //\r
-    // Preprocess all FV type files in this new FileSystem2 Fv image\r
+    // Scan and process the new discoveried FV for EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE \r
     //\r
+    FileHandle = NULL;\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
+      Status = FvPpi->FindFileByType (\r
+                        FvPpi,\r
+                        EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,\r
+                        FvHandle,\r
+                        &FileHandle\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
+        Status = FvPpi->FindSectionByType (\r
+                          FvPpi,\r
+                          EFI_SECTION_PEI_DEPEX,\r
+                          FileHandle,\r
+                          (VOID**)&DepexData\r
+                          );\r
         if (!EFI_ERROR (Status)) {\r
           if (!PeimDispatchReadiness (PeiServices, DepexData)) {\r
             //\r
@@ -423,43 +676,99 @@ Returns:
             continue;\r
           }\r
         }\r
-        //\r
-        // Process FvFile to install FvInfo ppi and build FvHob\r
-        // \r
-        ProcessFvFile (PeiServices, FileHandle, &AuthenticationStatus);\r
+        \r
+        DEBUG ((EFI_D_INFO, "Found firmware volume Image File %p in FV[%d] %p\n", FileHandle, CurFvCount, FvHandle));\r
+        ProcessFvFile (PrivateData, &PrivateData->Fv[CurFvCount], FileHandle);\r
       }\r
     } while (FileHandle != NULL);\r
+  } else {\r
+    DEBUG ((EFI_D_ERROR, "Fail to process FV %p because no corresponding EFI_FIRMWARE_VOLUME_PPI is found!\n", FvInfo2Ppi.FvInfo));\r
+    \r
+    AddUnknownFormatFvInfo (PrivateData, &FvInfo2Ppi);\r
   }\r
-\r
+  \r
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Verify the Guided Section GUID by checking if there is the Guided Section GUID HOB recorded the GUID itself.\r
+\r
+  @param GuidedSectionGuid          The Guided Section GUID.\r
+  @param GuidedSectionExtraction    A pointer to the pointer to the supported Guided Section Extraction Ppi\r
+                                    for the Guided Section.\r
+\r
+  @return TRUE      The GuidedSectionGuid could be identified, and the pointer to\r
+                    the Guided Section Extraction Ppi will be returned to *GuidedSectionExtraction.\r
+  @return FALSE     The GuidedSectionGuid could not be identified, or \r
+                    the Guided Section Extraction Ppi has not been installed yet.\r
+\r
+**/\r
+BOOLEAN\r
+VerifyGuidedSectionGuid (\r
+  IN  EFI_GUID                                  *GuidedSectionGuid,\r
+  OUT EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI     **GuidedSectionExtraction\r
+  )\r
+{\r
+  EFI_PEI_HOB_POINTERS  Hob;\r
+  EFI_GUID              *GuidRecorded;\r
+  VOID                  *Interface;\r
+  EFI_STATUS            Status;\r
+\r
+  //\r
+  // Check if there is the Guided Section GUID HOB recorded the GUID itself.\r
+  //\r
+  Hob.Raw = GetFirstGuidHob (GuidedSectionGuid);\r
+  if (Hob.Raw != NULL) {\r
+    GuidRecorded = (EFI_GUID *) GET_GUID_HOB_DATA (Hob);\r
+    if (CompareGuid (GuidRecorded, GuidedSectionGuid)) {\r
+      //\r
+      // Found the recorded GuidedSectionGuid.\r
+      //\r
+      Status = PeiServicesLocatePpi (GuidedSectionGuid, 0, NULL, (VOID **) &Interface);\r
+      if (!EFI_ERROR (Status) && Interface != NULL) {\r
+        //\r
+        // Found the supported Guided Section Extraction Ppi for the Guided Section.\r
+        //\r
+        *GuidedSectionExtraction = (EFI_PEI_GUIDED_SECTION_EXTRACTION_PPI *) Interface;\r
+        return TRUE;\r
+      }\r
+      return FALSE;\r
+    }\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
+/**\r
+  Go through the file to search SectionType section. \r
+  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 SectionInstance   Pointer to the filter to find the specific instance of section.\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
+  @param AuthenticationStatus Updated upon return to point to the authentication status for this section.\r
+  @param IsFfs3Fv          Indicates the FV format.\r
+\r
+  @return EFI_NOT_FOUND    The match section is not found.\r
+  @return EFI_SUCCESS      The match section is found.\r
+\r
+**/\r
 EFI_STATUS\r
-PeiFfsProcessSection (\r
+ProcessSection (\r
   IN CONST EFI_PEI_SERVICES     **PeiServices,\r
   IN EFI_SECTION_TYPE           SectionType,\r
+  IN OUT UINTN                  *SectionInstance,\r
   IN EFI_COMMON_SECTION_HEADER  *Section,\r
   IN UINTN                      SectionSize,\r
-  OUT VOID                      **OutputBuffer\r
+  OUT VOID                      **OutputBuffer,\r
+  OUT UINT32                    *AuthenticationStatus,\r
+  IN BOOLEAN                    IsFfs3Fv\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Go through the file to search SectionType section,\r
-  when meeting an encapsuled section. \r
-  \r
-Arguments:\r
-  PeiServices  - General purpose services available to every PEIM.\r
-  SearchType   - Filter to find only section of this type.\r
-  Section      - From where to search.\r
-  SectionSize  - The file size to search.\r
-  OutputBuffer - Pointer to the section to search.\r
-\r
-Returns:\r
-  EFI_STATUS\r
-  \r
---*/\r
 {\r
   EFI_STATUS                              Status;\r
   UINT32                                  SectionLength;\r
@@ -471,6 +780,11 @@ Returns:
   UINTN                                   Index;\r
   UINT32                                  Authentication;\r
   PEI_CORE_INSTANCE                       *PrivateData;\r
+  EFI_GUID                                *SectionDefinitionGuid;\r
+  BOOLEAN                                 SectionCached;\r
+  VOID                                    *TempOutputBuffer;\r
+  UINT32                                  TempAuthenticationStatus;\r
+  UINT16                                  GuidedSectionAttributes;\r
 \r
   PrivateData   = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
   *OutputBuffer = NULL;\r
@@ -480,87 +794,182 @@ Returns:
   PpiOutput     = NULL;\r
   PpiOutputSize = 0;\r
   while (ParsedLength < SectionSize) {\r
+\r
+    if (IS_SECTION2 (Section)) {\r
+      ASSERT (SECTION2_SIZE (Section) > 0x00FFFFFF);\r
+      if (!IsFfs3Fv) {\r
+        DEBUG ((EFI_D_ERROR, "Found a FFS3 formatted section in a non-FFS3 formatted FV.\n"));\r
+        SectionLength = SECTION2_SIZE (Section);\r
+        //\r
+        // SectionLength is adjusted it is 4 byte aligned.\r
+        // Go to the next section\r
+        //\r
+        SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);\r
+        ASSERT (SectionLength != 0);\r
+        ParsedLength += SectionLength;\r
+        Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) Section + SectionLength);\r
+        continue;\r
+      }\r
+    }\r
+\r
     if (Section->Type == SectionType) {\r
-      *OutputBuffer = (VOID *)(Section + 1);\r
-      return EFI_SUCCESS;\r
+      //\r
+      // The type matches, so check the instance count to see if it's the one we want.\r
+      //\r
+      (*SectionInstance)--;\r
+      if (*SectionInstance == 0) {\r
+        //\r
+        // Got it!\r
+        //\r
+        if (IS_SECTION2 (Section)) {\r
+          *OutputBuffer = (VOID *)((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER2));\r
+        } else {\r
+          *OutputBuffer = (VOID *)((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER));\r
+        }\r
+        return EFI_SUCCESS;\r
+      } else {\r
+        if (IS_SECTION2 (Section)) {\r
+          SectionLength = SECTION2_SIZE (Section);\r
+        } else {\r
+          SectionLength = SECTION_SIZE (Section);\r
+        }\r
+        //\r
+        // SectionLength is adjusted it is 4 byte aligned.\r
+        // Go to the next section\r
+        //\r
+        SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);\r
+        ASSERT (SectionLength != 0);\r
+        ParsedLength += SectionLength;\r
+        Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionLength);\r
+        continue;\r
+      }\r
     } else if ((Section->Type == EFI_SECTION_GUID_DEFINED) || (Section->Type == EFI_SECTION_COMPRESSION)) {\r
       //\r
       // Check the encapsulated section is extracted into the cache data.\r
       //\r
+      SectionCached = FALSE;\r
       for (Index = 0; Index < PrivateData->CacheSection.AllSectionCount; Index ++) {\r
         if (Section == PrivateData->CacheSection.Section[Index]) {\r
+          SectionCached = TRUE;\r
           PpiOutput     = PrivateData->CacheSection.SectionData[Index];\r
           PpiOutputSize = PrivateData->CacheSection.SectionSize[Index];\r
+          Authentication = PrivateData->CacheSection.AuthenticationStatus[Index];\r
           //\r
           // Search section directly from the cache data.\r
           //\r
-          return PeiFfsProcessSection (\r
-                  PeiServices,\r
-                  SectionType, \r
-                  PpiOutput, \r
-                  PpiOutputSize, \r
-                  OutputBuffer \r
-                  );\r
+          TempAuthenticationStatus = 0;\r
+          Status = ProcessSection (\r
+                     PeiServices,\r
+                     SectionType,\r
+                     SectionInstance,\r
+                     PpiOutput, \r
+                     PpiOutputSize, \r
+                     &TempOutputBuffer,\r
+                     &TempAuthenticationStatus,\r
+                     IsFfs3Fv\r
+                   );\r
+          if (!EFI_ERROR (Status)) {\r
+            *OutputBuffer = TempOutputBuffer;\r
+            *AuthenticationStatus = TempAuthenticationStatus | Authentication;\r
+            return EFI_SUCCESS;\r
+          }\r
         }\r
       }\r
-      \r
-      Status = EFI_NOT_FOUND;\r
-      if (Section->Type == EFI_SECTION_GUID_DEFINED) {\r
-        Status = PeiServicesLocatePpi (\r
-                   &((EFI_GUID_DEFINED_SECTION *)Section)->SectionDefinitionGuid, \r
-                   0, \r
-                   NULL, \r
-                   (VOID **) &GuidSectionPpi\r
-                   );\r
-        if (!EFI_ERROR (Status)) {\r
-          Status = GuidSectionPpi->ExtractSection (\r
-                                    GuidSectionPpi,\r
-                                    Section,\r
-                                    &PpiOutput,\r
-                                    &PpiOutputSize,\r
-                                    &Authentication\r
-                                    );\r
+\r
+      //\r
+      // If SectionCached is TRUE, the section data has been cached and scanned.\r
+      //\r
+      if (!SectionCached) {\r
+        Status = EFI_NOT_FOUND;\r
+        Authentication = 0;\r
+        if (Section->Type == EFI_SECTION_GUID_DEFINED) {\r
+          if (IS_SECTION2 (Section)) {\r
+            SectionDefinitionGuid   = &((EFI_GUID_DEFINED_SECTION2 *)Section)->SectionDefinitionGuid;\r
+            GuidedSectionAttributes = ((EFI_GUID_DEFINED_SECTION2 *)Section)->Attributes;\r
+          } else {\r
+            SectionDefinitionGuid   = &((EFI_GUID_DEFINED_SECTION *)Section)->SectionDefinitionGuid;\r
+            GuidedSectionAttributes = ((EFI_GUID_DEFINED_SECTION *)Section)->Attributes;\r
+          }\r
+          if (VerifyGuidedSectionGuid (SectionDefinitionGuid, &GuidSectionPpi)) {\r
+            Status = GuidSectionPpi->ExtractSection (\r
+                                       GuidSectionPpi,\r
+                                       Section,\r
+                                       &PpiOutput,\r
+                                       &PpiOutputSize,\r
+                                       &Authentication\r
+                                       );\r
+          } else if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) == 0) {\r
+            //\r
+            // Figure out the proper authentication status for GUIDED section without processing required\r
+            //\r
+            Status = EFI_SUCCESS;\r
+            if ((GuidedSectionAttributes & EFI_GUIDED_SECTION_AUTH_STATUS_VALID) == EFI_GUIDED_SECTION_AUTH_STATUS_VALID) {\r
+              Authentication |= EFI_AUTH_STATUS_IMAGE_SIGNED | EFI_AUTH_STATUS_NOT_TESTED;\r
+            }\r
+            if (IS_SECTION2 (Section)) {\r
+              PpiOutputSize = SECTION2_SIZE (Section) - ((EFI_GUID_DEFINED_SECTION2 *) Section)->DataOffset;\r
+              PpiOutput     = (UINT8 *) Section + ((EFI_GUID_DEFINED_SECTION2 *) Section)->DataOffset;\r
+            } else {\r
+              PpiOutputSize = SECTION_SIZE (Section) - ((EFI_GUID_DEFINED_SECTION *) Section)->DataOffset;\r
+              PpiOutput     = (UINT8 *) Section + ((EFI_GUID_DEFINED_SECTION *) Section)->DataOffset;\r
+            }\r
+          }\r
+        } else if (Section->Type == EFI_SECTION_COMPRESSION) {\r
+          Status = PeiServicesLocatePpi (&gEfiPeiDecompressPpiGuid, 0, NULL, (VOID **) &DecompressPpi);\r
+          if (!EFI_ERROR (Status)) {\r
+            Status = DecompressPpi->Decompress (\r
+                                      DecompressPpi,\r
+                                      (CONST EFI_COMPRESSION_SECTION*) Section,\r
+                                      &PpiOutput,\r
+                                      &PpiOutputSize\r
+                                      );\r
+          }\r
         }\r
-      } else if (Section->Type == EFI_SECTION_COMPRESSION) {\r
-        Status = PeiServicesLocatePpi (&gEfiPeiDecompressPpiGuid, 0, NULL, (VOID **) &DecompressPpi);\r
+\r
         if (!EFI_ERROR (Status)) {\r
-          Status = DecompressPpi->Decompress (\r
-                                    DecompressPpi,\r
-                                    (CONST EFI_COMPRESSION_SECTION*) Section,\r
-                                    &PpiOutput,\r
-                                    &PpiOutputSize\r
-                                    );\r
-        }\r
-      }\r
-      \r
-      if (!EFI_ERROR (Status)) {\r
-        //\r
-        // Update cache section data.\r
-        //\r
-        if (PrivateData->CacheSection.AllSectionCount < CACHE_SETION_MAX_NUMBER) {\r
-          PrivateData->CacheSection.AllSectionCount ++;\r
+          if ((Authentication & EFI_AUTH_STATUS_NOT_TESTED) == 0) {\r
+            //\r
+            // Update cache section data.\r
+            //\r
+            if (PrivateData->CacheSection.AllSectionCount < CACHE_SETION_MAX_NUMBER) {\r
+              PrivateData->CacheSection.AllSectionCount ++;\r
+            }\r
+            PrivateData->CacheSection.Section [PrivateData->CacheSection.SectionIndex]     = Section;\r
+            PrivateData->CacheSection.SectionData [PrivateData->CacheSection.SectionIndex] = PpiOutput;\r
+            PrivateData->CacheSection.SectionSize [PrivateData->CacheSection.SectionIndex] = PpiOutputSize;\r
+            PrivateData->CacheSection.AuthenticationStatus [PrivateData->CacheSection.SectionIndex] = Authentication;\r
+            PrivateData->CacheSection.SectionIndex = (PrivateData->CacheSection.SectionIndex + 1)%CACHE_SETION_MAX_NUMBER;\r
+          }\r
+\r
+          TempAuthenticationStatus = 0;\r
+          Status = ProcessSection (\r
+                     PeiServices,\r
+                     SectionType,\r
+                     SectionInstance,\r
+                     PpiOutput, \r
+                     PpiOutputSize, \r
+                     &TempOutputBuffer,\r
+                     &TempAuthenticationStatus,\r
+                     IsFfs3Fv\r
+                   );\r
+          if (!EFI_ERROR (Status)) {\r
+            *OutputBuffer = TempOutputBuffer;\r
+            *AuthenticationStatus = TempAuthenticationStatus | Authentication;\r
+            return EFI_SUCCESS;\r
+          }\r
         }\r
-        PrivateData->CacheSection.Section [PrivateData->CacheSection.SectionIndex]     = Section;\r
-        PrivateData->CacheSection.SectionData [PrivateData->CacheSection.SectionIndex] = PpiOutput;\r
-        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
       }\r
     }\r
 \r
+    if (IS_SECTION2 (Section)) {\r
+      SectionLength = SECTION2_SIZE (Section);\r
+    } else {\r
+      SectionLength = SECTION_SIZE (Section);\r
+    }\r
     //\r
-    // Size is 24 bits wide so mask upper 8 bits. \r
     // SectionLength is adjusted it is 4 byte aligned.\r
     // Go to the next section\r
     //\r
-    SectionLength = *(UINT32 *)Section->Size & 0x00FFFFFF;\r
     SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);\r
     ASSERT (SectionLength != 0);\r
     ParsedLength += SectionLength;\r
@@ -571,218 +980,790 @@ Returns:
 }\r
 \r
 \r
+/**\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
+  @param FileHandle      Pointer to the current file to search.\r
+  @param SectionData     A pointer to the discovered section, if successful.\r
+                         NULL if section not found\r
+\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
-\r
-Routine Description:\r
-    Given the input file pointer, search for the next matching section in the\r
-    FFS volume.\r
-\r
-Arguments:\r
-    PeiServices - Pointer to the PEI Core Services Table.\r
-    SearchType - Filter to find only sections of this type.\r
-    FfsFileHeader  - Pointer to the current file to search.\r
-    SectionData - Pointer to the Section matching SectionType in FfsFileHeader.\r
-                - NULL if section not found\r
-\r
-Returns:\r
-    EFI_NOT_FOUND - No files matching the search criteria were found\r
-    EFI_SUCCESS\r
-\r
---*/\r
 {\r
-  EFI_FFS_FILE_HEADER                     *FfsFileHeader;\r
-  UINT32                                  FileSize;\r
-  EFI_COMMON_SECTION_HEADER               *Section;\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
-  FfsFileHeader = (EFI_FFS_FILE_HEADER *)(FileHandle);\r
+/**\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           The value of the section type to find.\r
+  @param  SectionInstance       Section instance to find.\r
+  @param  FileHandle            Handle of the firmware file to search.\r
+  @param  SectionData           A pointer to the discovered section, if successful.\r
+  @param  AuthenticationStatus  A pointer to the authentication status for this section.\r
+\r
+  @retval EFI_SUCCESS      The section was found.\r
+  @retval EFI_NOT_FOUND    The section was not found.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PeiFfsFindSectionData3 (\r
+  IN CONST EFI_PEI_SERVICES    **PeiServices,\r
+  IN     EFI_SECTION_TYPE      SectionType,\r
+  IN     UINTN                 SectionInstance,\r
+  IN     EFI_PEI_FILE_HANDLE   FileHandle,\r
+  OUT VOID                     **SectionData,\r
+  OUT UINT32                   *AuthenticationStatus\r
+  )\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
+  if ((CoreFvHandle->FvPpi->Signature == EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE) &&\r
+      (CoreFvHandle->FvPpi->Revision == EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION)) {\r
+    return CoreFvHandle->FvPpi->FindSectionByType2 (CoreFvHandle->FvPpi, SectionType, SectionInstance, FileHandle, SectionData, AuthenticationStatus);\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
+  // The old FvPpi doesn't support to find section by section instance\r
+  // and return authentication status, so return EFI_UNSUPPORTED.\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
+  return EFI_UNSUPPORTED;\r
 }\r
 \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 FvHandle        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
 EFIAPI\r
 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
+{\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/FFS3 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
-Routine Description:\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
-Arguments:\r
-    PeiServices - Pointer to the PEI Core Services Table.\r
-    \r
-    SearchType - Filter to find only files of this type.\r
-      Type EFI_FV_FILETYPE_ALL causes no filtering to be done.\r
-      \r
-    FwVolHeader - Pointer to the FV header of the volume to search.\r
-      This parameter must point to a valid FFS volume.\r
-      \r
-    FileHeader  - Pointer to the current file from which to begin searching.\r
-      This pointer will be updated upon return to reflect the file found.\r
-  \r
-Returns:\r
-    EFI_NOT_FOUND - No files matching the search criteria were found\r
-    EFI_SUCCESS\r
+/**\r
+  Search the firmware volumes by index\r
 \r
---*/\r
-{\r
-  return PeiFindFileEx (VolumeHandle, NULL, SearchType, FileHandle, NULL);\r
-}\r
+  @param PeiServices     An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\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  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
-  IN CONST EFI_PEI_SERVICES           **PeiServices,\r
+PeiFfsFindNextVolume (\r
+  IN CONST EFI_PEI_SERVICES         **PeiServices,\r
   IN     UINTN                      Instance,\r
   IN OUT EFI_PEI_FV_HANDLE          *VolumeHandle\r
   )\r
-/*++\r
+{\r
+  PEI_CORE_INSTANCE  *Private;\r
+  PEI_CORE_FV_HANDLE *CoreFvHandle;\r
+  \r
+  if (VolumeHandle == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  \r
+  Private = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
+  \r
+  CoreFvHandle = FindNextCoreFvHandle (Private, Instance);\r
+  if (CoreFvHandle == NULL) {\r
+    *VolumeHandle = NULL;\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  \r
+  *VolumeHandle = CoreFvHandle->FvHandle;\r
+  \r
+  return EFI_SUCCESS;\r
+}\r
 \r
-Routine Description:\r
 \r
-  Return the firmware volumes.\r
+/**\r
+  Find a file within a volume by its name.\r
 \r
-  BugBug -- Move this to the location of this code to where the\r
-  other FV and FFS support code lives.\r
-  Also, update to use FindFV for instances #'s >= 1.\r
+  @param FileName        A pointer to the name of the file to find within the firmware volume.\r
+  @param VolumeHandle    The firmware volume to search\r
+  @param FileHandle      Upon exit, points to the found file's handle \r
+                         or NULL if it could not be found.\r
 \r
-Arguments:\r
+  @retval EFI_SUCCESS            File was found.\r
+  @retval EFI_NOT_FOUND          File was not found.\r
+  @retval EFI_INVALID_PARAMETER  VolumeHandle or FileHandle or FileName was NULL.\r
 \r
-  PeiServices - The PEI core services table.\r
-  Instance    - Instance of FV to find\r
-  FwVolHeader - Pointer to contain the data to return\r
+**/\r
+EFI_STATUS\r
+EFIAPI \r
+PeiFfsFindFileByName (\r
+  IN  CONST EFI_GUID        *FileName,\r
+  IN  EFI_PEI_FV_HANDLE     VolumeHandle,\r
+  OUT EFI_PEI_FILE_HANDLE   *FileHandle\r
+  )\r
+{\r
+  PEI_CORE_FV_HANDLE            *CoreFvHandle;\r
+  \r
+  if ((VolumeHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  \r
+  CoreFvHandle = FvHandleToCoreHandle (VolumeHandle);\r
+  if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  \r
+  return CoreFvHandle->FvPpi->FindFileByName (CoreFvHandle->FvPpi, FileName, &VolumeHandle, FileHandle);\r
+}\r
 \r
-Returns:\r
-  Pointer to the Firmware Volume instance requested\r
+/**\r
+  Returns information about a specific file.\r
 \r
-  EFI_INVALID_PARAMETER     - FwVolHeader is NULL\r
-  \r
-  EFI_SUCCESS               - Firmware volume instance successfully found.\r
+  @param FileHandle       Handle of the file.\r
+  @param FileInfo         Upon exit, points to the file's information.\r
 \r
---*/\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
+EFIAPI \r
+PeiFfsGetFileInfo (\r
+  IN EFI_PEI_FILE_HANDLE  FileHandle,\r
+  OUT EFI_FV_FILE_INFO    *FileInfo\r
+  )\r
 {\r
-  PEI_CORE_INSTANCE   *Private;\r
+  PEI_CORE_FV_HANDLE          *CoreFvHandle;\r
+  \r
+  if ((FileHandle == NULL) || (FileInfo == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
 \r
-  Private = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
-  if (VolumeHandle == NULL) {\r
-   return EFI_INVALID_PARAMETER;\r
-  } \r
+  //\r
+  // Retrieve the FirmwareVolume which the file resides in.\r
+  //\r
+  CoreFvHandle = FileHandleToVolume (FileHandle);\r
+  if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  return CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, FileHandle, FileInfo);\r
+}\r
 \r
-  if (Instance >= Private->AllFvCount) {\r
-   VolumeHandle = NULL;\r
-   return EFI_NOT_FOUND;\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
+  @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
+EFIAPI \r
+PeiFfsGetFileInfo2 (\r
+  IN EFI_PEI_FILE_HANDLE  FileHandle,\r
+  OUT EFI_FV_FILE_INFO2   *FileInfo\r
+  )\r
+{\r
+  PEI_CORE_FV_HANDLE          *CoreFvHandle;\r
+  \r
+  if ((FileHandle == NULL) || (FileInfo == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  *VolumeHandle = Private->AllFv[Instance];\r
-  return EFI_SUCCESS;\r
+  //\r
+  // Retrieve the FirmwareVolume which the file resides in.\r
+  //\r
+  CoreFvHandle = FileHandleToVolume (FileHandle);\r
+  if ((CoreFvHandle == NULL) || (CoreFvHandle->FvPpi == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((CoreFvHandle->FvPpi->Signature == EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE) &&\r
+      (CoreFvHandle->FvPpi->Revision == EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION)) {\r
+    return CoreFvHandle->FvPpi->GetFileInfo2 (CoreFvHandle->FvPpi, FileHandle, FileInfo);\r
+  }\r
+  //\r
+  // The old FvPpi doesn't support to return file info with authentication status,\r
+  // so return EFI_UNSUPPORTED.\r
+  //\r
+  return EFI_UNSUPPORTED;\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
+  @param VolumeHandle   Handle of the volume.\r
+  @param VolumeInfo     Upon exit, points to the volume's information.\r
 \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
+**/\r
 EFI_STATUS\r
 EFIAPI \r
-PeiFfsFindFileByName (\r
-  IN  CONST EFI_GUID        *FileName,\r
-  IN  EFI_PEI_FV_HANDLE     VolumeHandle,\r
-  OUT EFI_PEI_FILE_HANDLE   *FileHandle\r
+PeiFfsGetVolumeInfo (\r
+  IN EFI_PEI_FV_HANDLE  VolumeHandle,\r
+  OUT EFI_FV_INFO       *VolumeInfo\r
+  )\r
+{\r
+  PEI_CORE_FV_HANDLE                     *CoreHandle;\r
+  \r
+  if ((VolumeInfo == NULL) || (VolumeHandle == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  \r
+  CoreHandle = FvHandleToCoreHandle (VolumeHandle);\r
+  \r
+  if ((CoreHandle == NULL) || (CoreHandle->FvPpi == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  \r
+  return CoreHandle->FvPpi->GetVolumeInfo (CoreHandle->FvPpi, VolumeHandle, VolumeInfo);\r
+}\r
+\r
+/**\r
+  Find USED_SIZE FV_EXT_TYPE entry in FV extension header and get the FV used size.\r
+\r
+  @param[in]  FvHeader      Pointer to FV header.\r
+  @param[out] FvUsedSize    Pointer to FV used size returned,\r
+                            only valid if USED_SIZE FV_EXT_TYPE entry is found.\r
+  @param[out] EraseByte     Pointer to erase byte returned,\r
+                            only valid if USED_SIZE FV_EXT_TYPE entry is found.\r
+\r
+  @retval TRUE              USED_SIZE FV_EXT_TYPE entry is found,\r
+                            FV used size and erase byte are returned.\r
+  @retval FALSE             No USED_SIZE FV_EXT_TYPE entry found.\r
+\r
+**/\r
+BOOLEAN\r
+GetFvUsedSize (\r
+  IN EFI_FIRMWARE_VOLUME_HEADER     *FvHeader,\r
+  OUT UINT32                        *FvUsedSize,\r
+  OUT UINT8                         *EraseByte\r
   )\r
-/*++\r
+{\r
+  UINT16                                        ExtHeaderOffset;\r
+  EFI_FIRMWARE_VOLUME_EXT_HEADER                *ExtHeader;\r
+  EFI_FIRMWARE_VOLUME_EXT_ENTRY                 *ExtEntryList;\r
+  EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE  *ExtEntryUsedSize;\r
+\r
+  ExtHeaderOffset = ReadUnaligned16 (&FvHeader->ExtHeaderOffset);\r
+  if (ExtHeaderOffset != 0) {\r
+    ExtHeader    = (EFI_FIRMWARE_VOLUME_EXT_HEADER *) ((UINT8 *) FvHeader + ExtHeaderOffset);\r
+    ExtEntryList = (EFI_FIRMWARE_VOLUME_EXT_ENTRY *) (ExtHeader + 1);\r
+    while ((UINTN) ExtEntryList < ((UINTN) ExtHeader + ReadUnaligned32 (&ExtHeader->ExtHeaderSize))) {\r
+      if (ReadUnaligned16 (&ExtEntryList->ExtEntryType) == EFI_FV_EXT_TYPE_USED_SIZE_TYPE) {\r
+        //\r
+        // USED_SIZE FV_EXT_TYPE entry is found.\r
+        //\r
+        ExtEntryUsedSize = (EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE *) ExtEntryList;\r
+        *FvUsedSize = ReadUnaligned32 (&ExtEntryUsedSize->UsedSize);\r
+        if ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_ERASE_POLARITY) != 0) {\r
+          *EraseByte = 0xFF;\r
+        } else {\r
+          *EraseByte = 0;\r
+        }\r
+        DEBUG ((\r
+          DEBUG_INFO,\r
+          "FV at 0x%x has 0x%x used size, and erase byte is 0x%02x\n",\r
+          FvHeader,\r
+          *FvUsedSize,\r
+          *EraseByte\r
+          ));\r
+        return TRUE;\r
+      }\r
+      ExtEntryList = (EFI_FIRMWARE_VOLUME_EXT_ENTRY *)\r
+                     ((UINT8 *) ExtEntryList + ReadUnaligned16 (&ExtEntryList->ExtEntrySize));\r
+    }\r
+  }\r
 \r
-Routine Description:\r
+  //\r
+  // No USED_SIZE FV_EXT_TYPE entry found.\r
+  //\r
+  return FALSE;\r
+}\r
 \r
-  Given the input VolumeHandle, search for the next matching name file.\r
+/**\r
+  Get Fv image from the FV type file, then install FV INFO(2) ppi, Build FV hob.\r
 \r
-Arguments:\r
+  @param PrivateData          PeiCore's private data structure\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
-  FileName      - File name to search.\r
-  VolumeHandle  - The current FV to search.\r
-  FileHandle    - Pointer to the file matching name in VolumeHandle.\r
-                - NULL if file not found\r
-Returns:\r
-  EFI_STATUS\r
+  @retval EFI_NOT_FOUND         FV image can't be found.\r
+  @retval EFI_SUCCESS           Successfully to process it.\r
+  @retval EFI_OUT_OF_RESOURCES  Can not allocate page when aligning FV image\r
+  @retval EFI_SECURITY_VIOLATION Image is illegal\r
+  @retval Others                Can not find EFI_SECTION_FIRMWARE_VOLUME_IMAGE section\r
   \r
---*/  \r
+**/\r
+EFI_STATUS\r
+ProcessFvFile (\r
+  IN  PEI_CORE_INSTANCE           *PrivateData,\r
+  IN  PEI_CORE_FV_HANDLE          *ParentFvCoreHandle,\r
+  IN  EFI_PEI_FILE_HANDLE         ParentFvFileHandle\r
+  )\r
 {\r
-  EFI_STATUS  Status;\r
-  if ((VolumeHandle == NULL) || (FileName == NULL) || (FileHandle == NULL)) {\r
-    return EFI_INVALID_PARAMETER;\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
+  UINT64                        FvLength;\r
+  UINT32                        AuthenticationStatus;\r
+  UINT32                        FvUsedSize;\r
+  UINT8                         EraseByte;\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 *)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
+  AuthenticationStatus = 0;\r
+  if ((ParentFvPpi->Signature == EFI_PEI_FIRMWARE_VOLUME_PPI_SIGNATURE) &&\r
+      (ParentFvPpi->Revision == EFI_PEI_FIRMWARE_VOLUME_PPI_REVISION)) {\r
+    Status = ParentFvPpi->FindSectionByType2 (\r
+                            ParentFvPpi,\r
+                            EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
+                            0,\r
+                            ParentFvFileHandle,\r
+                            (VOID **)&FvHeader,\r
+                            &AuthenticationStatus\r
+                            );\r
+  } else {\r
+    Status = ParentFvPpi->FindSectionByType (\r
+                            ParentFvPpi,\r
+                            EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
+                            ParentFvFileHandle,\r
+                            (VOID **)&FvHeader\r
+                            );\r
   }\r
-  Status = PeiFindFileEx (VolumeHandle, FileName, 0, FileHandle, NULL);\r
-  if (Status == EFI_NOT_FOUND) {\r
-    *FileHandle = NULL;\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
   }\r
-  return Status;\r
+\r
+  Status = VerifyPeim (PrivateData, ParentFvHandle, ParentFvFileHandle, AuthenticationStatus);\r
+  if (Status == EFI_SECURITY_VIOLATION) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // If EFI_FVB2_WEAK_ALIGNMENT is set in the volume header then the first byte of the volume\r
+  // can be aligned on any power-of-two boundary. A weakly aligned volume can not be moved from\r
+  // its initial linked location and maintain its alignment.\r
+  //\r
+  if ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) {\r
+    //\r
+    // FvAlignment must be greater than or equal to 8 bytes of the minimum FFS alignment value.\r
+    //\r
+    FvAlignment = 1 << ((ReadUnaligned32 (&FvHeader->Attributes) & EFI_FVB2_ALIGNMENT) >> 16);\r
+    if (FvAlignment < 8) {\r
+      FvAlignment = 8;\r
+    }\r
+\r
+    DEBUG ((\r
+      DEBUG_INFO,\r
+      "%a() FV at 0x%x, FvAlignment required is 0x%x\n",\r
+      __FUNCTION__,\r
+      FvHeader,\r
+      FvAlignment\r
+      ));\r
+\r
+    //\r
+    // Check FvImage alignment.\r
+    //\r
+    if ((UINTN) FvHeader % FvAlignment != 0) {\r
+      FvLength    = ReadUnaligned64 (&FvHeader->FvLength);\r
+      NewFvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32) FvLength), FvAlignment);\r
+      if (NewFvBuffer == NULL) {\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+      if (GetFvUsedSize (FvHeader, &FvUsedSize, &EraseByte)) {\r
+        //\r
+        // Copy the used bytes and fill the rest with the erase value.\r
+        //\r
+        CopyMem (NewFvBuffer, FvHeader, (UINTN) FvUsedSize);\r
+        SetMem (\r
+          (UINT8 *) NewFvBuffer + FvUsedSize,\r
+          (UINTN) (FvLength - FvUsedSize),\r
+          EraseByte\r
+          );\r
+      } else {\r
+        CopyMem (NewFvBuffer, FvHeader, (UINTN) FvLength);\r
+      }\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 FvInfo(2) Ppi\r
+  // NOTE: FvInfo2 must be installed before FvInfo so that recursive processing of encapsulated\r
+  // FVs inherit the proper AuthenticationStatus.\r
+  //\r
+  PeiServicesInstallFvInfo2Ppi(\r
+    &FvHeader->FileSystemGuid,\r
+    (VOID**)FvHeader,\r
+    (UINT32)FvHeader->FvLength,\r
+    &ParentFvImageInfo.FvName,\r
+    &FileInfo.FileName,\r
+    AuthenticationStatus\r
+    );\r
+\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) 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) FvHeader,\r
+    FvHeader->FvLength,\r
+    &ParentFvImageInfo.FvName,\r
+    &FileInfo.FileName\r
+    );\r
+\r
+  //\r
+  // Build FV3 HOB with authentication status to be propagated to DXE.\r
+  //\r
+  BuildFv3Hob (\r
+    (EFI_PHYSICAL_ADDRESS) (UINTN) FvHeader,\r
+    FvHeader->FvLength,\r
+    AuthenticationStatus,\r
+    TRUE,\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
-PeiFfsGetFileInfo (\r
-  IN EFI_PEI_FILE_HANDLE  FileHandle,\r
-  OUT EFI_FV_FILE_INFO    *FileInfo\r
+EFIAPI\r
+PeiFfsFvPpiProcessVolume (\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
+{\r
+  EFI_STATUS          Status;\r
+  \r
+  ASSERT (FvHandle != NULL);\r
+  \r
+  if (Buffer == NULL) {\r
+    return EFI_VOLUME_CORRUPTED;\r
+  }\r
+  \r
+  //\r
+  // The build-in EFI_PEI_FIRMWARE_VOLUME_PPI for FFS2/FFS3 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
+  return EFI_SUCCESS;\r
+}  \r
+\r
+/**\r
+  Finds the next file of the specified type.\r
 \r
-Routine Description:\r
+  This service enables PEI modules to discover additional firmware files. \r
+  The FileHandle must be unique within the system.\r
 \r
-  Collect information of given file.\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
-Arguments:\r
-  FileHandle   - The handle to file.\r
-  FileInfo     - Pointer to the file information.\r
+  @retval EFI_SUCCESS   The file was found.\r
+  @retval EFI_NOT_FOUND The file was not found. FileHandle contains NULL.\r
 \r
-Returns:\r
-  EFI_STATUS\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PeiFfsFvPpiFindFileByType (\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
---*/    \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
+PeiFfsFvPpiFindFileByName (\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
+          break;\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
+PeiFfsFvPpiGetFileInfo (\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
-  EFI_PEI_FV_HANDLE           VolumeHandle;\r
+  PEI_CORE_FV_HANDLE          *CoreFvHandle;\r
+  PEI_FW_VOL_INSTANCE         *FwVolInstance;\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) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (((EFI_FIRMWARE_VOLUME_HEADER*)VolumeHandle)->Attributes & EFI_FVB2_ERASE_POLARITY) {\r
+  FwVolInstance = PEI_FW_VOL_INSTANCE_FROM_FV_THIS (This);\r
+\r
+  if ((CoreFvHandle->FvHeader->Attributes & EFI_FVB2_ERASE_POLARITY) != 0) {\r
     ErasePolarity = 1;\r
   } else {\r
     ErasePolarity = 0;\r
@@ -802,40 +1783,103 @@ Returns:
     }\r
 \r
   FileHeader = (EFI_FFS_FILE_HEADER *)FileHandle;\r
+  if (IS_FFS_FILE2 (FileHeader)) {\r
+    ASSERT (FFS_FILE2_SIZE (FileHeader) > 0x00FFFFFF);\r
+    if (!FwVolInstance->IsFfs3Fv) {\r
+      DEBUG ((EFI_D_ERROR, "It is a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FileHeader->Name));\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+    FileInfo->BufferSize = FFS_FILE2_SIZE (FileHeader) - sizeof (EFI_FFS_FILE_HEADER2);\r
+    FileInfo->Buffer = (UINT8 *) FileHeader + sizeof (EFI_FFS_FILE_HEADER2);\r
+  } else {\r
+    FileInfo->BufferSize = FFS_FILE_SIZE (FileHeader) - sizeof (EFI_FFS_FILE_HEADER);\r
+    FileInfo->Buffer = (UINT8 *) FileHeader + sizeof (EFI_FFS_FILE_HEADER);\r
+  }\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
+  FileInfo->FileAttributes = FfsAttributes2FvFileAttributes (FileHeader->Attributes);\r
+  if ((CoreFvHandle->FvHeader->Attributes & EFI_FVB2_MEMORY_MAPPED) == EFI_FVB2_MEMORY_MAPPED) {\r
+    FileInfo->FileAttributes |= EFI_FV_FILE_ATTRIB_MEMORY_MAPPED;\r
+  }\r
   return EFI_SUCCESS;\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, size and authentication status.\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
-PeiFfsGetVolumeInfo (\r
-  IN EFI_PEI_FV_HANDLE  VolumeHandle,\r
-  OUT EFI_FV_INFO       *VolumeInfo\r
+EFIAPI\r
+PeiFfsFvPpiGetFileInfo2 (\r
+  IN  CONST EFI_PEI_FIRMWARE_VOLUME_PPI   *This, \r
+  IN        EFI_PEI_FILE_HANDLE           FileHandle, \r
+  OUT       EFI_FV_FILE_INFO2             *FileInfo\r
   )\r
-/*++\r
+{\r
+  EFI_STATUS                  Status;\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
-Routine Description:\r
+  Status = PeiFfsFvPpiGetFileInfo (This, FileHandle, (EFI_FV_FILE_INFO *) FileInfo);\r
+  if (!EFI_ERROR (Status)) {\r
+    FileInfo->AuthenticationStatus = CoreFvHandle->AuthenticationStatus;\r
+  }\r
 \r
-  Collect information of given Fv Volume.\r
+  return Status;\r
+}\r
 \r
-Arguments:\r
-  VolumeHandle    - The handle to Fv Volume.\r
-  VolumeInfo      - The pointer to volume information.\r
-  \r
-Returns:\r
-  EFI_STATUS\r
+/**\r
+  This function returns information about the firmware volume.\r
   \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
+PeiFfsFvPpiGetVolumeInfo (\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
+  if ((VolumeInfo == NULL) || (FvHandle == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
   \r
@@ -844,22 +1888,552 @@ Returns:
   // 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
+  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
-  VolumeInfo->FvAttributes = FwVolHeader.Attributes;\r
-  VolumeInfo->FvStart = (VOID *) VolumeHandle;\r
-  VolumeInfo->FvSize = FwVolHeader.FvLength;\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 *)VolumeHandle) + FwVolHeader.ExtHeaderOffset);\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
+PeiFfsFvPpiFindSectionByType (\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
+  UINT32 AuthenticationStatus;\r
+  return PeiFfsFvPpiFindSectionByType2 (This, SearchType, 0, FileHandle, SectionData, &AuthenticationStatus);\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 instance and 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 SearchInstance         A filter to find the specific instance\r
+                                of sections.\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
+  @param AuthenticationStatus   Updated upon return to point to the\r
+                                authentication status for this section.\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
+PeiFfsFvPpiFindSectionByType2 (\r
+  IN  CONST EFI_PEI_FIRMWARE_VOLUME_PPI    *This,\r
+  IN        EFI_SECTION_TYPE               SearchType,\r
+  IN        UINTN                          SearchInstance,\r
+  IN        EFI_PEI_FILE_HANDLE            FileHandle,\r
+  OUT VOID                                 **SectionData,\r
+  OUT UINT32                               *AuthenticationStatus\r
+  )\r
+{\r
+  EFI_STATUS                              Status;\r
+  EFI_FFS_FILE_HEADER                     *FfsFileHeader;\r
+  UINT32                                  FileSize;\r
+  EFI_COMMON_SECTION_HEADER               *Section;\r
+  PEI_FW_VOL_INSTANCE                     *FwVolInstance;\r
+  PEI_CORE_FV_HANDLE                      *CoreFvHandle;\r
+  UINTN                                   Instance;\r
+  UINT32                                  ExtractedAuthenticationStatus;\r
+\r
+  if (SectionData == NULL) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  FwVolInstance = PEI_FW_VOL_INSTANCE_FROM_FV_THIS (This);\r
+\r
+  //\r
+  // Retrieve the FirmwareVolume which the file resides in.\r
+  //\r
+  CoreFvHandle = FileHandleToVolume (FileHandle);\r
+  if (CoreFvHandle == NULL) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  FfsFileHeader = (EFI_FFS_FILE_HEADER *)(FileHandle);\r
+\r
+  if (IS_FFS_FILE2 (FfsFileHeader)) {\r
+    ASSERT (FFS_FILE2_SIZE (FfsFileHeader) > 0x00FFFFFF);\r
+    if (!FwVolInstance->IsFfs3Fv) {\r
+      DEBUG ((EFI_D_ERROR, "It is a FFS3 formatted file: %g in a non-FFS3 formatted FV.\n", &FfsFileHeader->Name));\r
+      return EFI_NOT_FOUND;\r
+    }\r
+    Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER2));\r
+    FileSize = FFS_FILE2_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER2);\r
+  } else {\r
+    Section = (EFI_COMMON_SECTION_HEADER *) ((UINT8 *) FfsFileHeader + sizeof (EFI_FFS_FILE_HEADER));\r
+    FileSize = FFS_FILE_SIZE (FfsFileHeader) - sizeof (EFI_FFS_FILE_HEADER);\r
+  }\r
+\r
+  Instance = SearchInstance + 1;\r
+  ExtractedAuthenticationStatus = 0;\r
+  Status = ProcessSection (\r
+             GetPeiServicesTablePointer (),\r
+             SearchType,\r
+             &Instance,\r
+             Section, \r
+             FileSize, \r
+             SectionData,\r
+             &ExtractedAuthenticationStatus,\r
+             FwVolInstance->IsFfs3Fv\r
+             );\r
+  if (!EFI_ERROR (Status)) {\r
+    //\r
+    // Inherit the authentication status.\r
+    //\r
+    *AuthenticationStatus = ExtractedAuthenticationStatus | CoreFvHandle->AuthenticationStatus;\r
+  }\r
+  return Status;\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
+      //\r
+      // Search whether FvHob has been installed into PeiCore's FV database.\r
+      // If found, no need install new FvInfoPpi for it.\r
+      //\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
+      //\r
+      // Search whether FvHob has been cached into PeiCore's Unknown FV database.\r
+      // If found, no need install new FvInfoPpi for it.\r
+      //\r
+      if (!Match) {\r
+        for (Index = 0; Index < Private->UnknownFvInfoCount; Index ++) {\r
+          if ((UINTN)FvHob->BaseAddress == (UINTN)Private->UnknownFvInfo[Index].FvInfo) {\r
+            Match = TRUE;\r
+            break;\r
+          }\r
+        }\r
+      }\r
+\r
+      //\r
+      // If the Fv in FvHob has not been installed into PeiCore's FV database and has\r
+      // not been cached into PeiCore's Unknown FV database, install a new FvInfoPpi\r
+      // for it then PeiCore will dispatch it in callback of FvInfoPpi.\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
+      \r
+      FvHob = (EFI_HOB_FIRMWARE_VOLUME *)GetNextHob (EFI_HOB_TYPE_FV, (VOID *)((UINTN)FvHob + FvHob->Header.HobLength)); \r
+    }\r
+  }\r
+\r
+  ASSERT (Private->FvCount <= PcdGet32 (PcdPeiCoreMaxFvSupported));\r
+  if (Instance >= Private->FvCount) {\r
+    return NULL;\r
+  }\r
+\r
+  return &Private->Fv[Instance];\r
+}  \r
+\r
+/**\r
+  After PeiCore image is shadowed into permanent memory, all build-in FvPpi should\r
+  be re-installed with the instance in permanent memory and all cached FvPpi pointers in \r
+  PrivateData->Fv[] array should be fixed up to be pointed to the one in permanent\r
+  memory.\r
+  \r
+  @param PrivateData   Pointer to PEI_CORE_INSTANCE.\r
+**/  \r
+VOID\r
+PeiReinitializeFv (\r
+  IN  PEI_CORE_INSTANCE           *PrivateData\r
+  )\r
+{\r
+  VOID                    *OldFfsFvPpi;\r
+  EFI_PEI_PPI_DESCRIPTOR  *OldDescriptor;\r
+  UINTN                   Index;\r
+  EFI_STATUS              Status;\r
+\r
+  //\r
+  // Locate old build-in Ffs2 EFI_PEI_FIRMWARE_VOLUME_PPI which\r
+  // in flash.\r
+  //\r
+  Status = PeiServicesLocatePpi (\r
+            &gEfiFirmwareFileSystem2Guid,\r
+            0,\r
+            &OldDescriptor,\r
+            &OldFfsFvPpi\r
+            );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Re-install the EFI_PEI_FIRMWARE_VOLUME_PPI for build-in Ffs2\r
+  // which is shadowed from flash to permanent memory within PeiCore image.\r
+  //\r
+  Status = PeiServicesReInstallPpi (OldDescriptor, &mPeiFfs2FvPpiList);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Fixup all FvPpi pointers for the implementation in flash to permanent memory.\r
+  //\r
+  for (Index = 0; Index < PcdGet32 (PcdPeiCoreMaxFvSupported); Index ++) {\r
+    if (PrivateData->Fv[Index].FvPpi == OldFfsFvPpi) {\r
+      PrivateData->Fv[Index].FvPpi = &mPeiFfs2FwVol.Fv;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Locate old build-in Ffs3 EFI_PEI_FIRMWARE_VOLUME_PPI which\r
+  // in flash.\r
+  //\r
+  Status = PeiServicesLocatePpi (\r
+             &gEfiFirmwareFileSystem3Guid,\r
+             0,\r
+             &OldDescriptor,\r
+             &OldFfsFvPpi\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Re-install the EFI_PEI_FIRMWARE_VOLUME_PPI for build-in Ffs3\r
+  // which is shadowed from flash to permanent memory within PeiCore image.\r
+  //\r
+  Status = PeiServicesReInstallPpi (OldDescriptor, &mPeiFfs3FvPpiList);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Fixup all FvPpi pointers for the implementation in flash to permanent memory.\r
+  //\r
+  for (Index = 0; Index < PcdGet32 (PcdPeiCoreMaxFvSupported); Index ++) {\r
+    if (PrivateData->Fv[Index].FvPpi == OldFfsFvPpi) {\r
+      PrivateData->Fv[Index].FvPpi = &mPeiFfs3FwVol.Fv;\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Report the information for a new discoveried FV in unknown third-party format.\r
+  \r
+  If the EFI_PEI_FIRMWARE_VOLUME_PPI has not been installed for third-party FV format, but\r
+  the FV in this format has been discoveried, then this FV's information will be cached into\r
+  PEI_CORE_INSTANCE's UnknownFvInfo array.\r
+  Also a notification would be installed for unknown third-party FV format guid, if EFI_PEI_FIRMWARE_VOLUME_PPI\r
+  is installed later by platform's PEIM, the original unknown third-party FV will be processed by\r
+  using new installed EFI_PEI_FIRMWARE_VOLUME_PPI.\r
+  \r
+  @param PrivateData  Point to instance of PEI_CORE_INSTANCE\r
+  @param FvInfo2Ppi   Point to FvInfo2 PPI.\r
+  \r
+  @retval EFI_OUT_OF_RESOURCES  The FV info array in PEI_CORE_INSTANCE has no more spaces.\r
+  @retval EFI_SUCCESS           Success to add the information for unknown FV.\r
+**/\r
+EFI_STATUS\r
+AddUnknownFormatFvInfo (\r
+  IN PEI_CORE_INSTANCE                  *PrivateData,\r
+  IN EFI_PEI_FIRMWARE_VOLUME_INFO2_PPI  *FvInfo2Ppi\r
+  )\r
+{\r
+  PEI_CORE_UNKNOW_FORMAT_FV_INFO    *NewUnknownFv;\r
+  \r
+  if (PrivateData->UnknownFvInfoCount + 1 >= PcdGet32 (PcdPeiCoreMaxFvSupported)) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  \r
+  NewUnknownFv = &PrivateData->UnknownFvInfo[PrivateData->UnknownFvInfoCount];\r
+  PrivateData->UnknownFvInfoCount ++;\r
+  \r
+  CopyGuid (&NewUnknownFv->FvFormat, &FvInfo2Ppi->FvFormat);\r
+  NewUnknownFv->FvInfo     = FvInfo2Ppi->FvInfo;\r
+  NewUnknownFv->FvInfoSize = FvInfo2Ppi->FvInfoSize;\r
+  NewUnknownFv->AuthenticationStatus = FvInfo2Ppi->AuthenticationStatus;\r
+  NewUnknownFv->NotifyDescriptor.Flags  = (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);\r
+  NewUnknownFv->NotifyDescriptor.Guid   = &NewUnknownFv->FvFormat;\r
+  NewUnknownFv->NotifyDescriptor.Notify = ThirdPartyFvPpiNotifyCallback;\r
+  \r
+  PeiServicesNotifyPpi (&NewUnknownFv->NotifyDescriptor);\r
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Find the FV information according to third-party FV format guid.\r
+  \r
+  This routine also will remove the FV information found by given FV format guid from\r
+  PrivateData->UnknownFvInfo[].\r
+  \r
+  @param PrivateData      Point to instance of PEI_CORE_INSTANCE\r
+  @param Format           Point to given FV format guid\r
+  @param FvInfo           On return, the pointer of FV information buffer\r
+  @param FvInfoSize       On return, the size of FV information buffer.\r
+  @param AuthenticationStatus On return, the authentication status of FV information buffer.\r
+  \r
+  @retval EFI_NOT_FOUND  The FV is not found for new installed EFI_PEI_FIRMWARE_VOLUME_PPI\r
+  @retval EFI_SUCCESS    Success to find a FV which could be processed by new installed EFI_PEI_FIRMWARE_VOLUME_PPI.\r
+**/\r
+EFI_STATUS\r
+FindUnknownFormatFvInfo (\r
+  IN  PEI_CORE_INSTANCE *PrivateData,\r
+  IN  EFI_GUID          *Format,\r
+  OUT VOID              **FvInfo,\r
+  OUT UINT32            *FvInfoSize,\r
+  OUT UINT32            *AuthenticationStatus\r
+  )\r
+{\r
+  UINTN Index;\r
+  UINTN Index2;\r
+\r
+  Index = 0;\r
+  for (; Index < PrivateData->UnknownFvInfoCount; Index ++) {\r
+    if (CompareGuid (Format, &PrivateData->UnknownFvInfo[Index].FvFormat)) {\r
+      break;\r
+    }\r
+  }\r
+  \r
+  if (Index == PrivateData->UnknownFvInfoCount) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  \r
+  *FvInfo     = PrivateData->UnknownFvInfo[Index].FvInfo;\r
+  *FvInfoSize = PrivateData->UnknownFvInfo[Index].FvInfoSize;\r
+  *AuthenticationStatus = PrivateData->UnknownFvInfo[Index].AuthenticationStatus;\r
+  \r
+  //\r
+  // Remove an entry from UnknownFvInfo array.\r
+  //\r
+  Index2 = Index + 1;\r
+  for (;Index2 < PrivateData->UnknownFvInfoCount; Index2 ++, Index ++) {\r
+    CopyMem (&PrivateData->UnknownFvInfo[Index], &PrivateData->UnknownFvInfo[Index2], sizeof (PEI_CORE_UNKNOW_FORMAT_FV_INFO));\r
+  }\r
+  PrivateData->UnknownFvInfoCount --;\r
+  return EFI_SUCCESS;\r
+}  \r
+\r
+/**\r
+  Notification callback function for EFI_PEI_FIRMWARE_VOLUME_PPI.\r
+  \r
+  When a EFI_PEI_FIRMWARE_VOLUME_PPI is installed to support new FV format, this \r
+  routine is called to process all discoveried FVs in this format.\r
+  \r
+  @param PeiServices       An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation\r
+  @param NotifyDescriptor  Address of the notification descriptor data structure.\r
+  @param Ppi               Address of the PPI that was installed.\r
+  \r
+  @retval EFI_SUCCESS  The notification callback is processed correctly.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ThirdPartyFvPpiNotifyCallback (\r
+  IN EFI_PEI_SERVICES              **PeiServices,\r
+  IN EFI_PEI_NOTIFY_DESCRIPTOR     *NotifyDescriptor,\r
+  IN VOID                          *Ppi\r
+  )\r
+{\r
+  PEI_CORE_INSTANCE            *PrivateData;\r
+  EFI_PEI_FIRMWARE_VOLUME_PPI  *FvPpi;\r
+  VOID                         *FvInfo;\r
+  UINT32                       FvInfoSize;\r
+  UINT32                       AuthenticationStatus;\r
+  EFI_STATUS                   Status;\r
+  EFI_PEI_FV_HANDLE            FvHandle;\r
+  BOOLEAN                      IsProcessed;\r
+  UINTN                        FvIndex;\r
+  EFI_PEI_FILE_HANDLE          FileHandle;\r
+  VOID                         *DepexData;  \r
+  UINTN                        CurFvCount;\r
+\r
+  PrivateData  = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
+  FvPpi = (EFI_PEI_FIRMWARE_VOLUME_PPI*) Ppi;\r
+  \r
+  do {\r
+    Status = FindUnknownFormatFvInfo (PrivateData, NotifyDescriptor->Guid, &FvInfo, &FvInfoSize, &AuthenticationStatus);\r
+    if (EFI_ERROR (Status)) {\r
+      return EFI_SUCCESS;\r
+    }\r
+    \r
+    //\r
+    // Process new found FV and get FV handle.\r
+    //\r
+    Status = FvPpi->ProcessVolume (FvPpi, FvInfo, FvInfoSize, &FvHandle);\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "Fail to process the FV 0x%p, FV may be corrupted!\n", FvInfo));\r
+      continue;\r
+    }\r
+\r
+    //\r
+    // Check whether the FV has already been processed.\r
+    //\r
+    IsProcessed = FALSE;\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", FvInfo));\r
+        IsProcessed = TRUE;\r
+        break;\r
+      }\r
+    }\r
+  \r
+    if (IsProcessed) {\r
+      continue;\r
+    }\r
+    \r
+    if (PrivateData->FvCount >= PcdGet32 (PcdPeiCoreMaxFvSupported)) {\r
+      DEBUG ((EFI_D_ERROR, "The number of Fv Images (%d) exceed the max supported FVs (%d) in Pei", PrivateData->FvCount + 1, PcdGet32 (PcdPeiCoreMaxFvSupported)));\r
+      DEBUG ((EFI_D_ERROR, "PcdPeiCoreMaxFvSupported value need be reconfigurated in DSC"));\r
+      ASSERT (FALSE);\r
+    }\r
+        \r
+    //\r
+    // Update internal PEI_CORE_FV array.\r
+    //\r
+    PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) FvInfo;\r
+    PrivateData->Fv[PrivateData->FvCount].FvPpi    = FvPpi;\r
+    PrivateData->Fv[PrivateData->FvCount].FvHandle = FvHandle;\r
+    PrivateData->Fv[PrivateData->FvCount].AuthenticationStatus = AuthenticationStatus;\r
+    CurFvCount = PrivateData->FvCount;\r
+    DEBUG ((\r
+      EFI_D_INFO, \r
+      "The %dth FV start address is 0x%11p, size is 0x%08x, handle is 0x%p\n", \r
+      (UINT32) CurFvCount,\r
+      (VOID *) FvInfo, \r
+      FvInfoSize,\r
+      FvHandle\r
+      ));    \r
+    PrivateData->FvCount ++;\r
+\r
+    //\r
+    // Scan and process the new discoveried FV for EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE \r
+    //\r
+    FileHandle = NULL;\r
+    do {\r
+      Status = FvPpi->FindFileByType (\r
+                        FvPpi,\r
+                        EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,\r
+                        FvHandle,\r
+                        &FileHandle\r
+                       );\r
+      if (!EFI_ERROR (Status)) {\r
+        Status = FvPpi->FindSectionByType (\r
+                          FvPpi,\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\n", FileHandle, CurFvCount, FvHandle));\r
+        ProcessFvFile (PrivateData, &PrivateData->Fv[CurFvCount], FileHandle);\r
+      }\r
+    } while (FileHandle != NULL);\r
+  } while (TRUE);\r
+}\r