]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Pei/FwVol/FwVol.c
MdeModulePkg/SecurityPkg: Update TreePei to handle FvInfo2 and update FwVol of PeiCor...
[mirror_edk2.git] / MdeModulePkg / Core / Pei / FwVol / FwVol.c
index 7b74edb69ac92778e1c942d7b91d8f444df4cdf1..fe876ed393a9357bca3d689ab4be8401637ff3b1 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   Pei Core Firmware File System service routines.\r
   \r
-Copyright (c) 2006 - 2009, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
+Copyright (c) 2006 - 2014, 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
@@ -14,27 +14,107 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "FwVol.h"\r
 \r
-EFI_PEI_NOTIFY_DESCRIPTOR mNotifyOnFvInfoList = {\r
-  (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
-  &gEfiPeiFirmwareVolumeInfoPpiGuid,\r
-  FirmwareVolmeInfoPpiNotifyCallback \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
+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
-EFI_PEI_FIRMWARE_VOLUME_PPI mPeiFfs2FvPpi = {\r
-  PeiFfs2FvPpiProcessVolume,\r
-  PeiFfs2FvPpiFindFileByType,\r
-  PeiFfs2FvPpiFindFileByName,\r
-  PeiFfs2FvPpiGetFileInfo,\r
-  PeiFfs2FvPpiGetVolumeInfo,\r
-  PeiFfs2FvPpiFindSectionByType\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
-  &mPeiFfs2FvPpi\r
+  &mPeiFfs2FwVol.Fv\r
+};\r
+\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
+/**\r
+Required Alignment             Alignment Value in FFS         Alignment Value in\r
+(bytes)                        Attributes Field               Firmware Volume Interfaces\r
+1                                    0                                     0\r
+16                                   1                                     4\r
+128                                  2                                     7\r
+512                                  3                                     9\r
+1 KB                                 4                                     10\r
+4 KB                                 5                                     12\r
+32 KB                                6                                     15\r
+64 KB                                7                                     16\r
+**/\r
+UINT8 mFvAttributes[] = {0, 4, 7, 9, 10, 12, 15, 16};\r
+\r
+/**\r
+  Convert the FFS File Attributes to FV File Attributes\r
+\r
+  @param  FfsAttributes              The attributes of UINT8 type.\r
+\r
+  @return The attributes of EFI_FV_FILE_ATTRIBUTES\r
+\r
+**/\r
+EFI_FV_FILE_ATTRIBUTES\r
+FfsAttributes2FvFileAttributes (\r
+  IN EFI_FFS_FILE_ATTRIBUTES FfsAttributes\r
+  )\r
+{\r
+  UINT8                     DataAlignment;\r
+  EFI_FV_FILE_ATTRIBUTES    FileAttribute;\r
+\r
+  DataAlignment = (UINT8) ((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT) >> 3);\r
+  ASSERT (DataAlignment < 8);\r
+\r
+  FileAttribute = (EFI_FV_FILE_ATTRIBUTES) mFvAttributes[DataAlignment];\r
+\r
+  if ((FfsAttributes & FFS_ATTRIB_FIXED) == FFS_ATTRIB_FIXED) {\r
+    FileAttribute |= EFI_FV_FILE_ATTRIB_FIXED;\r
+  }\r
+\r
+  return FileAttribute;\r
+}\r
+\r
 /**\r
   Returns the file state set by the highest zero bit in the State field\r
 \r
@@ -85,16 +165,27 @@ CalculateHeaderChecksum (
   IN EFI_FFS_FILE_HEADER  *FileHeader\r
   )\r
 {\r
-  EFI_FFS_FILE_HEADER TestFileHeader;\r
-  \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
+  EFI_FFS_FILE_HEADER2 TestFileHeader;\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
-  return CalculateSum8 ((CONST UINT8 *) &TestFileHeader, sizeof (EFI_FFS_FILE_HEADER));\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 CalculateSum8 ((CONST UINT8 *) &TestFileHeader, sizeof (EFI_FFS_FILE_HEADER));\r
+  }\r
 }\r
 \r
 /**\r
@@ -150,10 +241,11 @@ FindFileEx (
   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
+  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
   UINT32                                FileLength;\r
@@ -162,6 +254,8 @@ FindFileEx (
   UINT64                                FvLength;\r
   UINT8                                 ErasePolarity;\r
   UINT8                                 FileState;\r
+  UINT8                                 DataCheckSum;\r
+  BOOLEAN                               IsFfs3Fv;\r
   \r
   //\r
   // Convert the handle of FV to FV header for memory-mapped firmware volume\r
@@ -169,6 +263,8 @@ FindFileEx (
   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) != 0) {\r
     ErasePolarity = 1;\r
@@ -182,13 +278,29 @@ FindFileEx (
   // 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
+      //\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
@@ -203,9 +315,18 @@ FindFileEx (
     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
@@ -216,8 +337,34 @@ FindFileEx (
         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
@@ -249,7 +396,15 @@ FindFileEx (
       break;\r
     \r
     case EFI_FILE_DELETED:\r
-      FileLength       =  *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;\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
@@ -286,7 +441,12 @@ PeiInitializeFv (
   // Install FV_PPI for FFS2 file system.\r
   //\r
   PeiServicesInstallPpi (&mPeiFfs2FvPpiList);\r
-  \r
+\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
@@ -309,20 +469,37 @@ PeiInitializeFv (
            (UINTN)BfvHeader->FvLength,\r
            &FvHandle\r
            );\r
-                        \r
+\r
+  //\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
+    BfvHeader->FvLength,\r
+    FvHandle\r
+    ));    \r
+  PrivateData->FvCount ++;\r
+                            \r
   //\r
-  // Post a call-back for the FvInfoPPI services to expose\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 install.\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 PPI will be installed.\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
@@ -340,12 +517,15 @@ FirmwareVolmeInfoPpiNotifyCallback (
   IN VOID                          *Ppi\r
   )\r
 {\r
-  EFI_PEI_FIRMWARE_VOLUME_INFO_PPI      *FvInfoPpi;\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
+  BOOLEAN                               IsFvInfo2;\r
   \r
   Status       = EFI_SUCCESS;\r
   PrivateData  = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
@@ -356,14 +536,27 @@ FirmwareVolmeInfoPpiNotifyCallback (
     ASSERT (FALSE);\r
   }\r
 \r
-  FvInfoPpi = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *)Ppi;\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
   //\r
   // Locate the corresponding FV_PPI according to founded FV's format guid\r
   //\r
   Status = PeiServicesLocatePpi (\r
-             &FvInfoPpi->FvFormat, \r
-             0, \r
+             &FvInfo2Ppi.FvFormat,\r
+             0,\r
              NULL,\r
              (VOID**)&FvPpi\r
              );\r
@@ -371,40 +564,132 @@ FirmwareVolmeInfoPpiNotifyCallback (
     //\r
     // Process new found FV and get FV handle.\r
     //\r
-    Status = FvPpi->ProcessVolume (FvPpi, FvInfoPpi->FvInfo, FvInfoPpi->FvInfoSize, &FvHandle);\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!"));\r
+      DEBUG ((EFI_D_ERROR, "Fail to process new found FV, FV may be corrupted!\n"));\r
       return Status;\r
     }\r
-    DEBUG ((EFI_D_INFO, "Found and process new FV %p, all fv's count is %d\n", FvHandle, PrivateData->FvCount));\r
-  } else {\r
-    DEBUG ((EFI_D_ERROR, "Fail to process FV %p because no corresponding EFI_FIRMWARE_VOLUME_PPI is found!\n", FvInfoPpi->FvInfo));\r
-    \r
-    //\r
-    // If can not find EFI_FIRMWARE_VOLUME_PPI to process firmware to get FvHandle, \r
-    // use the address of FV buffer as its handle.\r
-    //\r
-    FvHandle = FvInfoPpi->FvInfo;\r
-    \r
+\r
     //\r
     // Check whether the FV has already been processed.\r
     //\r
     for (FvIndex = 0; FvIndex < PrivateData->FvCount; FvIndex ++) {\r
       if (PrivateData->Fv[FvIndex].FvHandle == FvHandle) {\r
-        DEBUG ((EFI_D_INFO, "The Fv %p has already been processed!\n", 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
-    \r
-    PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) FvInfoPpi->FvInfo;\r
-    PrivateData->Fv[PrivateData->FvCount].FvPpi    = NULL;\r
+    }\r
+\r
+    //\r
+    // Update internal PEI_CORE_FV array.\r
+    //\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
+    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 *) FvInfo2Ppi.FvInfo, \r
+      FvInfo2Ppi.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, PrivateData->FvCount - 1, FvHandle));\r
+        ProcessFvFile (PrivateData, &PrivateData->Fv[PrivateData->FvCount - 1], 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
   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
@@ -412,10 +697,13 @@ FirmwareVolmeInfoPpiNotifyCallback (
   \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
@@ -425,9 +713,12 @@ EFI_STATUS
 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
   EFI_STATUS                              Status;\r
@@ -440,6 +731,10 @@ ProcessSection (
   UINTN                                   Index;\r
   UINT32                                  Authentication;\r
   PEI_CORE_INSTANCE                       *PrivateData;\r
+  EFI_GUID                                *SectionDefinitionGuid;\r
+  BOOLEAN                                 SectionCached;\r
+  VOID                                    *TempOutputBuffer;\r
+  UINT32                                  TempAuthenticationStatus;\r
 \r
   PrivateData   = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
   *OutputBuffer = NULL;\r
@@ -449,87 +744,163 @@ ProcessSection (
   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 ProcessSection (\r
-                   PeiServices,\r
-                   SectionType, \r
-                   PpiOutput, \r
-                   PpiOutputSize, \r
-                   OutputBuffer \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
+          } else {\r
+            SectionDefinitionGuid = &((EFI_GUID_DEFINED_SECTION *)Section)->SectionDefinitionGuid;\r
+          }\r
+          if (VerifyGuidedSectionGuid (SectionDefinitionGuid, &GuidSectionPpi)) {\r
+            Status = GuidSectionPpi->ExtractSection (\r
+                                       GuidSectionPpi,\r
+                                       Section,\r
+                                       &PpiOutput,\r
+                                       &PpiOutputSize,\r
+                                       &Authentication\r
+                                       );\r
+          }\r
+        } else if (Section->Type == EFI_SECTION_COMPRESSION) {\r
+          Status = PeiServicesLocatePpi (&gEfiPeiDecompressPpiGuid, 0, NULL, (VOID **) &DecompressPpi);\r
+          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
+          //\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
+          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 ProcessSection (\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
@@ -572,13 +943,56 @@ PeiFfsFindSectionData (
   return CoreFvHandle->FvPpi->FindSectionByType (CoreFvHandle->FvPpi, SectionType, FileHandle, SectionData);\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           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
+  // The old FvPpi doesn't support to find section by section instance\r
+  // and return authentication status, so return EFI_UNSUPPORTED.\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 VolumeHandle    Handle of firmware volume in which to search.\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
@@ -603,7 +1017,7 @@ PeiFfsFindNextFile (
   \r
   //\r
   // To make backward compatiblity, if can not find corresponding the handle of FV\r
-  // then treat FV as build-in FFS2 format and memory mapped FV that FV handle is pointed\r
+  // 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
@@ -642,6 +1056,10 @@ PeiFfsFindNextVolume (
   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
@@ -695,7 +1113,7 @@ PeiFfsFindFileByName (
   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
+  @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
@@ -726,6 +1144,48 @@ PeiFfsGetFileInfo (
   return CoreFvHandle->FvPpi->GetFileInfo (CoreFvHandle->FvPpi, FileHandle, FileInfo);\r
 }\r
 \r
+/**\r
+  Returns information about a specific file.\r
+\r
+  @param FileHandle       Handle of the file.\r
+  @param FileInfo         Upon exit, points to the file's information.\r
+\r
+  @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
+  //\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
@@ -753,7 +1213,7 @@ PeiFfsGetVolumeInfo (
 {\r
   PEI_CORE_FV_HANDLE                     *CoreHandle;\r
   \r
-  if (VolumeInfo == NULL) {\r
+  if ((VolumeInfo == NULL) || (VolumeHandle == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
   \r
@@ -767,19 +1227,22 @@ PeiFfsGetVolumeInfo (
 }\r
 \r
 /**\r
-  Get Fv image from the FV type file, then install FV INFO ppi, Build FV hob.\r
+  Get Fv image from the FV type file, then install FV INFO(2) ppi, Build FV hob.\r
 \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
   @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
 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
@@ -793,6 +1256,8 @@ ProcessFvFile (
   EFI_PEI_FV_HANDLE             ParentFvHandle;\r
   EFI_FIRMWARE_VOLUME_HEADER    *FvHeader;\r
   EFI_FV_FILE_INFO              FileInfo;\r
+  UINT64                        FvLength;\r
+  UINT32                        AuthenticationStatus;\r
   \r
   //\r
   // Check if this EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE file has already\r
@@ -816,37 +1281,62 @@ ProcessFvFile (
   //\r
   // Find FvImage in FvFile\r
   //\r
-  Status = ParentFvPpi->FindSectionByType (\r
-                          ParentFvPpi,\r
-                          EFI_SECTION_FIRMWARE_VOLUME_IMAGE,\r
-                          ParentFvFileHandle,\r
-                          (VOID **)&FvHeader\r
-                          );\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
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
-  //\r
-  // FvAlignment must be more than 8 bytes required by FvHeader structure.\r
-  //\r
-  FvAlignment = 1 << ((FvHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16);\r
-  if (FvAlignment < 8) {\r
-    FvAlignment = 8;\r
+  Status = VerifyPeim (PrivateData, ParentFvHandle, ParentFvFileHandle, AuthenticationStatus);\r
+  if (Status == EFI_SECURITY_VIOLATION) {\r
+    return Status;\r
   }\r
-  \r
+\r
   //\r
-  // Check FvImage\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 ((UINTN) FvHeader % FvAlignment != 0) {\r
-    NewFvBuffer = AllocateAlignedPages (EFI_SIZE_TO_PAGES ((UINT32) FvHeader->FvLength), FvAlignment);\r
-    if (NewFvBuffer == NULL) {\r
-      return EFI_OUT_OF_RESOURCES;\r
+  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
+    //\r
+    // Check FvImage\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
+      CopyMem (NewFvBuffer, FvHeader, (UINTN) FvLength);\r
+      FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) NewFvBuffer;\r
     }\r
-    CopyMem (NewFvBuffer, FvHeader, (UINTN) FvHeader->FvLength);\r
-    FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) NewFvBuffer;\r
   }\r
-  \r
+\r
   Status = ParentFvPpi->GetVolumeInfo (ParentFvPpi, ParentFvHandle, &ParentFvImageInfo);\r
   ASSERT_EFI_ERROR (Status);\r
   \r
@@ -854,7 +1344,7 @@ ProcessFvFile (
   ASSERT_EFI_ERROR (Status);\r
   \r
   //\r
-  // Install FvPpi and Build FvHob\r
+  // Install FvInfo(2) Ppi\r
   //\r
   PeiServicesInstallFvInfoPpi (\r
     &FvHeader->FileSystemGuid,\r
@@ -864,6 +1354,15 @@ ProcessFvFile (
     &FileInfo.FileName\r
     );\r
 \r
+  PeiServicesInstallFvInfo2Ppi (\r
+    &FvHeader->FileSystemGuid,\r
+    (VOID**) FvHeader,\r
+    (UINT32) FvHeader->FvLength,\r
+    &ParentFvImageInfo.FvName,\r
+    &FileInfo.FileName,\r
+    AuthenticationStatus\r
+    );\r
+\r
   //\r
   // Inform the extracted FvImage to Fv HOB consumer phase, i.e. DXE phase\r
   //\r
@@ -912,7 +1411,7 @@ ProcessFvFile (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-PeiFfs2FvPpiProcessVolume (\r
+PeiFfsFvPpiProcessVolume (\r
   IN  CONST  EFI_PEI_FIRMWARE_VOLUME_PPI *This,\r
   IN  VOID                               *Buffer,\r
   IN  UINTN                              BufferSize,\r
@@ -920,17 +1419,15 @@ PeiFfs2FvPpiProcessVolume (
   )\r
 {\r
   EFI_STATUS          Status;\r
-  PEI_CORE_INSTANCE   *PrivateData;\r
-  EFI_PEI_FILE_HANDLE FileHandle;\r
-  VOID                *DepexData;\r
-  EFI_PEI_SERVICES    **PeiServices;\r
-  UINTN               FvIndex;\r
   \r
-  PeiServices = (EFI_PEI_SERVICES**) GetPeiServicesTablePointer ();\r
-  PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
+  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 support memory-mapped\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
@@ -944,61 +1441,6 @@ PeiFfs2FvPpiProcessVolume (
     return EFI_VOLUME_CORRUPTED;\r
   }\r
 \r
-  //\r
-  // Check whether the FV has already been processed.\r
-  //\r
-  for (FvIndex = 0; FvIndex < PrivateData->FvCount; FvIndex ++) {\r
-    if (PrivateData->Fv[FvIndex].FvHandle == *FvHandle) {\r
-      DEBUG ((EFI_D_INFO, "The Fv %p has already been processed!", Buffer));\r
-      return EFI_SUCCESS;\r
-    }\r
-  }\r
-  \r
-  //\r
-  // Update internal PEI_CORE_FV array.\r
-  //\r
-  PrivateData->Fv[PrivateData->FvCount].FvHeader = (EFI_FIRMWARE_VOLUME_HEADER*) Buffer;\r
-  PrivateData->Fv[PrivateData->FvCount].FvPpi    = (EFI_PEI_FIRMWARE_VOLUME_PPI*) This;\r
-  PrivateData->Fv[PrivateData->FvCount].FvHandle = *FvHandle;\r
-  \r
-  DEBUG ((EFI_D_INFO, \r
-          "The %dth FV start address is 0x%11p and size is 0x%08x\n", \r
-          (UINT32) PrivateData->FvCount, \r
-          (VOID *) Buffer, \r
-          BufferSize\r
-          ));\r
-  PrivateData->FvCount ++;\r
-  \r
-  FileHandle = NULL;\r
-  \r
-  do {\r
-    Status = This->FindFileByType (\r
-                     This,\r
-                     EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE,\r
-                     *FvHandle,\r
-                     &FileHandle\r
-                     );\r
-    if (!EFI_ERROR (Status)) {\r
-      Status = This->FindSectionByType (\r
-                       This,\r
-                       EFI_SECTION_PEI_DEPEX,\r
-                       FileHandle,\r
-                       (VOID**)&DepexData\r
-                       );\r
-      if (!EFI_ERROR (Status)) {\r
-        if (!PeimDispatchReadiness (PeiServices, DepexData)) {\r
-          //\r
-          // Dependency is not satisfied.\r
-          //\r
-          continue;\r
-        }\r
-      }\r
-      \r
-      DEBUG ((EFI_D_INFO, "Found firmware volume Image File %p in FV[%d] %p", FileHandle, PrivateData->FvCount - 1, *FvHandle));\r
-      ProcessFvFile (&PrivateData->Fv[PrivateData->FvCount - 1], FileHandle);\r
-    }\r
-  } while (FileHandle != NULL);\r
-\r
   return EFI_SUCCESS;\r
 }  \r
 \r
@@ -1026,7 +1468,7 @@ PeiFfs2FvPpiProcessVolume (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-PeiFfs2FvPpiFindFileByType (\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
@@ -1063,7 +1505,7 @@ PeiFfs2FvPpiFindFileByType (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-PeiFfs2FvPpiFindFileByName (\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
@@ -1098,6 +1540,7 @@ PeiFfs2FvPpiFindFileByName (
         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
@@ -1127,7 +1570,7 @@ PeiFfs2FvPpiFindFileByName (
 **/ \r
 EFI_STATUS\r
 EFIAPI\r
-PeiFfs2FvPpiGetFileInfo (\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
@@ -1137,7 +1580,8 @@ PeiFfs2FvPpiGetFileInfo (
   UINT8                       ErasePolarity;\r
   EFI_FFS_FILE_HEADER         *FileHeader;\r
   PEI_CORE_FV_HANDLE          *CoreFvHandle;\r
-  \r
+  PEI_FW_VOL_INSTANCE         *FwVolInstance;\r
+\r
   if ((FileHandle == NULL) || (FileInfo == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
@@ -1150,7 +1594,9 @@ PeiFfs2FvPpiGetFileInfo (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (CoreFvHandle->FvHeader->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
@@ -1170,14 +1616,77 @@ PeiFfs2FvPpiGetFileInfo (
     }\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
-  return EFI_SUCCESS;  \r
-}  \r
-  \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
+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
+  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
+  Status = PeiFfsFvPpiGetFileInfo (This, FileHandle, (EFI_FV_FILE_INFO *) FileInfo);\r
+  if (!EFI_ERROR (Status)) {\r
+    FileInfo->AuthenticationStatus = CoreFvHandle->AuthenticationStatus;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
 /**\r
   This function returns information about the firmware volume.\r
   \r
@@ -1194,7 +1703,7 @@ PeiFfs2FvPpiGetFileInfo (
 **/   \r
 EFI_STATUS\r
 EFIAPI\r
-PeiFfs2FvPpiGetVolumeInfo (\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
@@ -1203,7 +1712,7 @@ PeiFfs2FvPpiGetVolumeInfo (
   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
@@ -1247,7 +1756,7 @@ PeiFfs2FvPpiGetVolumeInfo (
                           type.\r
   @param FileHandle       Handle of firmware file in which to\r
                           search.\r
-  @param SectionData      Updated upon  return to point to the\r
+  @param SectionData      Updated upon return to point to the\r
                           section found.\r
   \r
   @retval EFI_SUCCESS     Section was found.\r
@@ -1256,36 +1765,109 @@ PeiFfs2FvPpiGetVolumeInfo (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-PeiFfs2FvPpiFindSectionByType (\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
-  \r
-  FfsFileHeader = (EFI_FFS_FILE_HEADER *)(FileHandle);\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
-  // 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
+  // Retrieve the FirmwareVolume which the file resides in.\r
   //\r
-  Section = (EFI_COMMON_SECTION_HEADER *)(FfsFileHeader + 1);\r
-  FileSize = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;\r
-  FileSize -= sizeof (EFI_FFS_FILE_HEADER);\r
+  CoreFvHandle = FileHandleToVolume (FileHandle);\r
+  if (CoreFvHandle == NULL) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
 \r
-  return ProcessSection (\r
-           GetPeiServicesTablePointer (),\r
-           SearchType, \r
-           Section, \r
-           FileSize, \r
-           SectionData\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
@@ -1342,14 +1924,34 @@ FindNextCoreFvHandle (
     //\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 Not Found, Install FvInfo Ppi for it.\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
@@ -1360,13 +1962,309 @@ FindNextCoreFvHandle (
           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 <= FixedPcdGet32 (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 permenant\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 < FixedPcdGet32 (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 < FixedPcdGet32 (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 >= FixedPcdGet32 (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
+  \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 >= FixedPcdGet32 (PcdPeiCoreMaxFvSupported)) {\r
+      DEBUG ((EFI_D_ERROR, "The number of Fv Images (%d) exceed the max supported FVs (%d) in Pei", PrivateData->FvCount + 1, FixedPcdGet32 (PcdPeiCoreMaxFvSupported)));\r
+      DEBUG ((EFI_D_ERROR, "PcdPeiCoreMaxFvSupported value need be reconfigurated in DSC"));\r
+      ASSERT (FALSE);\r
+    }\r
+        \r
+    //\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
+    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 *) 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, PrivateData->FvCount - 1, FvHandle));\r
+        ProcessFvFile (PrivateData, &PrivateData->Fv[PrivateData->FvCount - 1], FileHandle);\r
+      }\r
+    } while (FileHandle != NULL);\r
+  } while (TRUE);\r
+}\r