]> 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 a347129be420853fccc41288fe2241e742315f40..4150b338b211847a17cf2d2968a11b11e1e46976 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   Pei Core Firmware File System service routines.\r
   \r
-Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\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
@@ -74,18 +75,27 @@ EFI_PEI_PPI_DESCRIPTOR  mPeiFfs3FvPpiList = {
 };\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
+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
 /**\r
   Convert the FFS File Attributes to FV File Attributes\r
@@ -106,7 +116,11 @@ FfsAttributes2FvFileAttributes (
   DataAlignment = (UINT8) ((FfsAttributes & FFS_ATTRIB_DATA_ALIGNMENT) >> 3);\r
   ASSERT (DataAlignment < 8);\r
 \r
-  FileAttribute = (EFI_FV_FILE_ATTRIBUTES) mFvAttributes[DataAlignment];\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
   if ((FfsAttributes & FFS_ATTRIB_FIXED) == FFS_ATTRIB_FIXED) {\r
     FileAttribute |= EFI_FV_FILE_ATTRIB_FIXED;\r
@@ -203,16 +217,34 @@ FileHandleToVolume (
   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
-      return &PrivateData->Fv[Index];\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
+\r
+  if (BestIndex < PrivateData->FvCount) {\r
+    return &PrivateData->Fv[BestIndex];\r
+  }\r
+\r
   return NULL;\r
 }\r
 \r
@@ -463,12 +495,13 @@ PeiInitializeFv (
   //\r
   // Get handle of BFV\r
   //\r
-  FvPpi->ProcessVolume (\r
-           FvPpi, \r
-           SecCoreData->BootFirmwareVolumeBase,\r
-           (UINTN)BfvHeader->FvLength,\r
-           &FvHandle\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
   // Update internal PEI_CORE_FV array.\r
@@ -482,7 +515,7 @@ PeiInitializeFv (
     "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
+    (UINT32) BfvHeader->FvLength,\r
     FvHandle\r
     ));    \r
   PrivateData->FvCount ++;\r
@@ -526,16 +559,11 @@ FirmwareVolmeInfoPpiNotifyCallback (
   EFI_PEI_FILE_HANDLE                   FileHandle;\r
   VOID                                  *DepexData;\r
   BOOLEAN                               IsFvInfo2;\r
-  \r
+  UINTN                                 CurFvCount;\r
+\r
   Status       = EFI_SUCCESS;\r
   PrivateData  = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\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
   if (CompareGuid (NotifyDescriptor->Guid, &gEfiPeiFirmwareVolumeInfo2PpiGuid)) {\r
     //\r
     // It is FvInfo2PPI.\r
@@ -551,6 +579,20 @@ FirmwareVolmeInfoPpiNotifyCallback (
     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
   //\r
   // Locate the corresponding FV_PPI according to founded FV's format guid\r
   //\r
@@ -584,6 +626,12 @@ FirmwareVolmeInfoPpiNotifyCallback (
       }\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
@@ -591,10 +639,11 @@ FirmwareVolmeInfoPpiNotifyCallback (
     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) PrivateData->FvCount, \r
+      (UINT32) CurFvCount,\r
       (VOID *) FvInfo2Ppi.FvInfo, \r
       FvInfo2Ppi.FvInfoSize,\r
       FvHandle\r
@@ -628,8 +677,8 @@ FirmwareVolmeInfoPpiNotifyCallback (
           }\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
+        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
@@ -735,6 +784,7 @@ ProcessSection (
   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
@@ -834,9 +884,11 @@ ProcessSection (
         Authentication = 0;\r
         if (Section->Type == EFI_SECTION_GUID_DEFINED) {\r
           if (IS_SECTION2 (Section)) {\r
-            SectionDefinitionGuid = &((EFI_GUID_DEFINED_SECTION2 *)Section)->SectionDefinitionGuid;\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
+            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
@@ -846,6 +898,21 @@ ProcessSection (
                                        &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
@@ -860,17 +927,19 @@ ProcessSection (
         }\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
-          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
@@ -1226,6 +1295,68 @@ PeiFfsGetVolumeInfo (
   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
+  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
+  //\r
+  // No USED_SIZE FV_EXT_TYPE entry found.\r
+  //\r
+  return FALSE;\r
+}\r
+\r
 /**\r
   Get Fv image from the FV type file, then install FV INFO(2) ppi, Build FV hob.\r
 \r
@@ -1258,7 +1389,9 @@ ProcessFvFile (
   EFI_FV_FILE_INFO              FileInfo;\r
   UINT64                        FvLength;\r
   UINT32                        AuthenticationStatus;\r
-  \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
@@ -1323,8 +1456,16 @@ ProcessFvFile (
       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\r
+    // Check FvImage alignment.\r
     //\r
     if ((UINTN) FvHeader % FvAlignment != 0) {\r
       FvLength    = ReadUnaligned64 (&FvHeader->FvLength);\r
@@ -1332,7 +1473,19 @@ ProcessFvFile (
       if (NewFvBuffer == NULL) {\r
         return EFI_OUT_OF_RESOURCES;\r
       }\r
-      CopyMem (NewFvBuffer, FvHeader, (UINTN) FvLength);\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
@@ -1345,22 +1498,24 @@ ProcessFvFile (
   \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
-  PeiServicesInstallFvInfoPpi (\r
+  PeiServicesInstallFvInfo2Ppi(\r
     &FvHeader->FileSystemGuid,\r
-    (VOID**) FvHeader,\r
-    (UINT32) FvHeader->FvLength,\r
+    (VOID**)FvHeader,\r
+    (UINT32)FvHeader->FvLength,\r
     &ParentFvImageInfo.FvName,\r
-    &FileInfo.FileName\r
+    &FileInfo.FileName,\r
+    AuthenticationStatus\r
     );\r
 \r
-  PeiServicesInstallFvInfo2Ppi (\r
+  PeiServicesInstallFvInfoPpi (\r
     &FvHeader->FileSystemGuid,\r
     (VOID**) FvHeader,\r
     (UINT32) FvHeader->FvLength,\r
     &ParentFvImageInfo.FvName,\r
-    &FileInfo.FileName,\r
-    AuthenticationStatus\r
+    &FileInfo.FileName\r
     );\r
 \r
   //\r
@@ -1382,6 +1537,18 @@ ProcessFvFile (
     &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
@@ -1978,7 +2145,7 @@ FindNextCoreFvHandle (
 /**\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
+  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
@@ -2177,7 +2344,8 @@ ThirdPartyFvPpiNotifyCallback (
   UINTN                        FvIndex;\r
   EFI_PEI_FILE_HANDLE          FileHandle;\r
   VOID                         *DepexData;  \r
-  \r
+  UINTN                        CurFvCount;\r
+\r
   PrivateData  = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
   FvPpi = (EFI_PEI_FIRMWARE_VOLUME_PPI*) Ppi;\r
   \r
@@ -2225,10 +2393,11 @@ ThirdPartyFvPpiNotifyCallback (
     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) PrivateData->FvCount, \r
+      (UINT32) CurFvCount,\r
       (VOID *) FvInfo, \r
       FvInfoSize,\r
       FvHandle\r
@@ -2262,8 +2431,8 @@ ThirdPartyFvPpiNotifyCallback (
           }\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
+        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