]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg PeiCore: Remove the using of PcdPeiCoreMaxPeimPerFv
authorStar Zeng <star.zeng@intel.com>
Tue, 6 Nov 2018 13:29:11 +0000 (21:29 +0800)
committerStar Zeng <star.zeng@intel.com>
Wed, 19 Dec 2018 04:33:27 +0000 (12:33 +0800)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1405

Background as below.

Problem:
As static configuration from the PCDs, the binary PeiCore (for example
in FSP binary with dispatch mode) could not predict how many FVs,
Files or PPIs for different platforms.

Burden:
Platform developers need configure the PCDs accordingly for different
platforms.

To solve the problem and remove the burden, we can update code to
remove the using of PcdPeiCoreMaxFvSupported, PcdPeiCoreMaxPeimPerFv
and PcdPeiCoreMaxPpiSupported by extending buffer dynamically for FV,
File and PPI management.

This patch removes the using of PcdPeiCoreMaxPeimPerFv in PeiCore.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao Wu <hao.a.wu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Nate DeSimone <nathaniel.l.desimone@intel.com>
Cc: Chasel Chiu <chasel.chiu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Jian J Wang <jian.j.wang@intel.com>
Reviewed-by: Chasel Chiu <chasel.chiu@intel.com>
MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
MdeModulePkg/Core/Pei/PeiMain.h
MdeModulePkg/Core/Pei/PeiMain.inf
MdeModulePkg/Core/Pei/PeiMain/PeiMain.c

index f6bb35a5fe8d4c3726698021cd801501d4be3705..9692e2f6bf51f7efa6ae3d2a689e0626e8fc43e5 100644 (file)
@@ -41,7 +41,7 @@ DiscoverPeimsAndOrderWithApriori (
   UINTN                               PeimCount;\r
   EFI_GUID                            *Guid;\r
   EFI_PEI_FILE_HANDLE                 *TempFileHandles;\r
-  EFI_GUID                            *FileGuid;\r
+  EFI_GUID                            *TempFileGuid;\r
   EFI_PEI_FIRMWARE_VOLUME_PPI         *FvPpi;\r
   EFI_FV_FILE_INFO                    FileInfo;\r
 \r
@@ -51,38 +51,81 @@ DiscoverPeimsAndOrderWithApriori (
   // Walk the FV and find all the PEIMs and the Apriori file.\r
   //\r
   AprioriFileHandle = NULL;\r
-  Private->CurrentFvFileHandles[0] = NULL;\r
+  Private->CurrentFvFileHandles = NULL;\r
   Guid = NULL;\r
-  FileHandle = NULL;\r
-  TempFileHandles = Private->FileHandles;\r
-  FileGuid        = Private->FileGuid;\r
 \r
   //\r
-  // If the current Fv has been scanned, directly get its cachable record.\r
+  // If the current Fv has been scanned, directly get its cached records.\r
   //\r
-  if (Private->Fv[Private->CurrentPeimFvCount].ScanFv) {\r
-    CopyMem (Private->CurrentFvFileHandles, Private->Fv[Private->CurrentPeimFvCount].FvFileHandles, sizeof (EFI_PEI_FILE_HANDLE) * PcdGet32 (PcdPeiCoreMaxPeimPerFv));\r
+  if (CoreFileHandle->ScanFv) {\r
+    Private->CurrentFvFileHandles = CoreFileHandle->FvFileHandles;\r
     return;\r
   }\r
 \r
+  TempFileHandles = Private->TempFileHandles;\r
+  TempFileGuid    = Private->TempFileGuid;\r
+\r
   //\r
-  // Go ahead to scan this Fv, and cache FileHandles within it.\r
+  // Go ahead to scan this Fv, get PeimCount and cache FileHandles within it to TempFileHandles.\r
   //\r
-  Status = EFI_NOT_FOUND;\r
-  for (PeimCount = 0; PeimCount <= PcdGet32 (PcdPeiCoreMaxPeimPerFv); PeimCount++) {\r
+  PeimCount = 0;\r
+  FileHandle = NULL;\r
+  do {\r
     Status = FvPpi->FindFileByType (FvPpi, PEI_CORE_INTERNAL_FFS_FILE_DISPATCH_TYPE, CoreFileHandle->FvHandle, &FileHandle);\r
-    if (Status != EFI_SUCCESS || PeimCount == PcdGet32 (PcdPeiCoreMaxPeimPerFv)) {\r
-      break;\r
+    if (!EFI_ERROR (Status)) {\r
+      if (PeimCount >= Private->TempPeimCount) {\r
+        //\r
+        // Run out of room, grow the buffer.\r
+        //\r
+        TempFileHandles = AllocatePool (\r
+                            sizeof (EFI_PEI_FILE_HANDLE) * (Private->TempPeimCount + TEMP_FILE_GROWTH_STEP));\r
+        ASSERT (TempFileHandles != NULL);\r
+        CopyMem (\r
+          TempFileHandles,\r
+          Private->TempFileHandles,\r
+          sizeof (EFI_PEI_FILE_HANDLE) * Private->TempPeimCount\r
+          );\r
+        Private->TempFileHandles = TempFileHandles;\r
+        TempFileGuid = AllocatePool (\r
+                         sizeof (EFI_GUID) * (Private->TempPeimCount + TEMP_FILE_GROWTH_STEP));\r
+        ASSERT (TempFileGuid != NULL);\r
+        CopyMem (\r
+          TempFileGuid,\r
+          Private->TempFileGuid,\r
+          sizeof (EFI_GUID) * Private->TempPeimCount\r
+          );\r
+        Private->TempFileGuid = TempFileGuid;\r
+        Private->TempPeimCount = Private->TempPeimCount + TEMP_FILE_GROWTH_STEP;\r
+      }\r
+\r
+      TempFileHandles[PeimCount++] = FileHandle;\r
     }\r
+  } while (!EFI_ERROR (Status));\r
+\r
+  DEBUG ((\r
+    DEBUG_INFO,\r
+    "%a(): Found 0x%x PEI FFS files in the %dth FV\n",\r
+    __FUNCTION__,\r
+    PeimCount,\r
+    Private->CurrentPeimFvCount\r
+    ));\r
 \r
-    Private->CurrentFvFileHandles[PeimCount] = FileHandle;\r
+  if (PeimCount == 0) {\r
+    //\r
+    // No PEIM FFS file is found, set ScanFv flag and return.\r
+    //\r
+    CoreFileHandle->ScanFv = TRUE;\r
+    return;\r
   }\r
 \r
   //\r
-  // Check whether the count of files exceeds the max support files in a FV image\r
-  // If more files are required in a FV image, PcdPeiCoreMaxPeimPerFv can be set to a larger value in DSC file.\r
+  // Record PeimCount, allocate buffer for PeimState and FvFileHandles.\r
   //\r
-  ASSERT ((Status != EFI_SUCCESS) || (PeimCount < PcdGet32 (PcdPeiCoreMaxPeimPerFv)));\r
+  CoreFileHandle->PeimCount = PeimCount;\r
+  CoreFileHandle->PeimState = AllocateZeroPool (sizeof (UINT8) * PeimCount);\r
+  ASSERT (CoreFileHandle->PeimState != NULL);\r
+  CoreFileHandle->FvFileHandles = AllocateZeroPool (sizeof (EFI_PEI_FILE_HANDLE) * PeimCount);\r
+  ASSERT (CoreFileHandle->FvFileHandles != NULL);\r
 \r
   //\r
   // Get Apriori File handle\r
@@ -96,7 +139,7 @@ DiscoverPeimsAndOrderWithApriori (
     Status = FvPpi->FindSectionByType (FvPpi, EFI_SECTION_RAW, AprioriFileHandle, (VOID **) &Apriori);\r
     if (!EFI_ERROR (Status)) {\r
       //\r
-      // Calculate the number of PEIMs in the A Priori list\r
+      // Calculate the number of PEIMs in the Apriori file\r
       //\r
       Status = FvPpi->GetFileInfo (FvPpi, AprioriFileHandle, &FileInfo);\r
       ASSERT_EFI_ERROR (Status);\r
@@ -113,71 +156,55 @@ DiscoverPeimsAndOrderWithApriori (
         // Make an array of file name guids that matches the FileHandle array so we can convert\r
         // quickly from file name to file handle\r
         //\r
-        Status = FvPpi->GetFileInfo (FvPpi, Private->CurrentFvFileHandles[Index], &FileInfo);\r
-        CopyMem (&FileGuid[Index], &FileInfo.FileName, sizeof(EFI_GUID));\r
+        Status = FvPpi->GetFileInfo (FvPpi, TempFileHandles[Index], &FileInfo);\r
+        ASSERT_EFI_ERROR (Status);\r
+        CopyMem (&TempFileGuid[Index], &FileInfo.FileName, sizeof(EFI_GUID));\r
       }\r
 \r
       //\r
-      // Walk through FileGuid array to find out who is invalid PEIM guid in Apriori file.\r
-      // Add available PEIMs in Apriori file into TempFileHandles array at first.\r
+      // Walk through TempFileGuid array to find out who is invalid PEIM guid in Apriori file.\r
+      // Add available PEIMs in Apriori file into FvFileHandles array.\r
       //\r
-      Index2 = 0;\r
-      for (Index = 0; Index2 < Private->AprioriCount; Index++) {\r
-        while (Index2 < Private->AprioriCount) {\r
-          Guid = ScanGuid (FileGuid, PeimCount * sizeof (EFI_GUID), &Apriori[Index2++]);\r
-          if (Guid != NULL) {\r
-            break;\r
-          }\r
-        }\r
-        if (Guid == NULL) {\r
-          break;\r
-        }\r
-        PeimIndex = ((UINTN)Guid - (UINTN)&FileGuid[0])/sizeof (EFI_GUID);\r
-        TempFileHandles[Index] = Private->CurrentFvFileHandles[PeimIndex];\r
+      Index = 0;\r
+      for (Index2 = 0; Index2 < Private->AprioriCount; Index2++) {\r
+        Guid = ScanGuid (TempFileGuid, PeimCount * sizeof (EFI_GUID), &Apriori[Index2]);\r
+        if (Guid != NULL) {\r
+          PeimIndex = ((UINTN)Guid - (UINTN)&TempFileGuid[0])/sizeof (EFI_GUID);\r
+          CoreFileHandle->FvFileHandles[Index++] = TempFileHandles[PeimIndex];\r
 \r
-        //\r
-        // Since we have copied the file handle we can remove it from this list.\r
-        //\r
-        Private->CurrentFvFileHandles[PeimIndex] = NULL;\r
+          //\r
+          // Since we have copied the file handle we can remove it from this list.\r
+          //\r
+          TempFileHandles[PeimIndex] = NULL;\r
+        }\r
       }\r
 \r
       //\r
-      // Update valid Aprioricount\r
+      // Update valid AprioriCount\r
       //\r
       Private->AprioriCount = Index;\r
 \r
       //\r
       // Add in any PEIMs not in the Apriori file\r
       //\r
-      for (;Index < PeimCount; Index++) {\r
-        for (Index2 = 0; Index2 < PeimCount; Index2++) {\r
-          if (Private->CurrentFvFileHandles[Index2] != NULL) {\r
-            TempFileHandles[Index] = Private->CurrentFvFileHandles[Index2];\r
-            Private->CurrentFvFileHandles[Index2] = NULL;\r
-            break;\r
-          }\r
+      for (Index2 = 0; Index2 < PeimCount; Index2++) {\r
+        if (TempFileHandles[Index2] != NULL) {\r
+          CoreFileHandle->FvFileHandles[Index++] = TempFileHandles[Index2];\r
+          TempFileHandles[Index2] = NULL;\r
         }\r
       }\r
-      //\r
-      //Index the end of array contains re-range Pei moudle.\r
-      //\r
-      TempFileHandles[Index] = NULL;\r
-\r
-      //\r
-      // Private->CurrentFvFileHandles is currently in PEIM in the FV order.\r
-      // We need to update it to start with files in the A Priori list and\r
-      // then the remaining files in PEIM order.\r
-      //\r
-      CopyMem (Private->CurrentFvFileHandles, TempFileHandles, sizeof (EFI_PEI_FILE_HANDLE) * PcdGet32 (PcdPeiCoreMaxPeimPerFv));\r
+      ASSERT (Index == PeimCount);\r
     }\r
+  } else {\r
+    CopyMem (CoreFileHandle->FvFileHandles, TempFileHandles, sizeof (EFI_PEI_FILE_HANDLE) * PeimCount);\r
   }\r
+\r
   //\r
-  // Cache the current Fv File Handle. So that we don't have to scan the Fv again.\r
-  // Instead, we can retrieve the file handles within this Fv from cachable data.\r
+  // The current Fv File Handles have been cached. So that we don't have to scan the Fv again.\r
+  // Instead, we can retrieve the file handles within this Fv from cached records.\r
   //\r
-  Private->Fv[Private->CurrentPeimFvCount].ScanFv = TRUE;\r
-  CopyMem (Private->Fv[Private->CurrentPeimFvCount].FvFileHandles, Private->CurrentFvFileHandles, sizeof (EFI_PEI_FILE_HANDLE) * PcdGet32 (PcdPeiCoreMaxPeimPerFv));\r
-\r
+  CoreFileHandle->ScanFv = TRUE;\r
+  Private->CurrentFvFileHandles = CoreFileHandle->FvFileHandles;\r
 }\r
 \r
 //\r
@@ -977,7 +1004,7 @@ PeiDispatcher (
     SaveCurrentFileHandle =  Private->CurrentFileHandle;\r
 \r
     for (Index1 = 0; Index1 < Private->FvCount; Index1++) {\r
-      for (Index2 = 0; (Index2 < PcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->Fv[Index1].FvFileHandles[Index2] != NULL); Index2++) {\r
+      for (Index2 = 0; Index2 < Private->Fv[Index1].PeimCount; Index2++) {\r
         if (Private->Fv[Index1].PeimState[Index2] == PEIM_STATE_REGISTER_FOR_SHADOW) {\r
           PeimFileHandle = Private->Fv[Index1].FvFileHandles[Index2];\r
           Private->CurrentFileHandle   = PeimFileHandle;\r
@@ -1063,7 +1090,7 @@ PeiDispatcher (
       // Start to dispatch all modules within the current Fv.\r
       //\r
       for (PeimCount = Private->CurrentPeimCount;\r
-           (PeimCount < PcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->CurrentFvFileHandles[PeimCount] != NULL);\r
+           PeimCount < Private->Fv[FvCount].PeimCount;\r
            PeimCount++) {\r
         Private->CurrentPeimCount  = PeimCount;\r
         PeimFileHandle = Private->CurrentFileHandle = Private->CurrentFvFileHandles[PeimCount];\r
@@ -1207,21 +1234,17 @@ PeiDispatcher (
       }\r
 \r
       //\r
-      // We set to NULL here to optimize the 2nd entry to this routine after\r
-      //  memory is found. This reprevents rescanning of the FV. We set to\r
-      //  NULL here so we start at the begining of the next FV\r
+      // Before walking through the next FV, we should set them to NULL/0 to\r
+      // start at the begining of the next FV.\r
       //\r
       Private->CurrentFileHandle = NULL;\r
       Private->CurrentPeimCount = 0;\r
-      //\r
-      // Before walking through the next FV,Private->CurrentFvFileHandles[]should set to NULL\r
-      //\r
-      SetMem (Private->CurrentFvFileHandles, sizeof (EFI_PEI_FILE_HANDLE) * PcdGet32 (PcdPeiCoreMaxPeimPerFv), 0);\r
+      Private->CurrentFvFileHandles = NULL;\r
     }\r
 \r
     //\r
-    // Before making another pass, we should set Private->CurrentPeimFvCount =0 to go\r
-    // through all the FV.\r
+    // Before making another pass, we should set it to 0 to\r
+    // go through all the FVs.\r
     //\r
     Private->CurrentPeimFvCount = 0;\r
 \r
@@ -1300,7 +1323,7 @@ DepexSatisfied (
 \r
   if (PeimCount < Private->AprioriCount) {\r
     //\r
-    // If its in the A priori file then we set Depex to TRUE\r
+    // If it's in the Apriori file then we set Depex to TRUE\r
     //\r
     DEBUG ((DEBUG_DISPATCH, "  RESULT = TRUE (Apriori)\n"));\r
     return TRUE;\r
index 6469436b8f79319d7df802541b394029fd930201..195bdc3425b6255a83c9aca7c4cc9e6d4af6be33 100644 (file)
@@ -111,12 +111,13 @@ typedef struct {
   EFI_FIRMWARE_VOLUME_HEADER          *FvHeader;\r
   EFI_PEI_FIRMWARE_VOLUME_PPI         *FvPpi;\r
   EFI_PEI_FV_HANDLE                   FvHandle;\r
+  UINTN                               PeimCount;\r
   //\r
-  // Ponter to the buffer with the PcdPeiCoreMaxPeimPerFv number of Entries.\r
+  // Ponter to the buffer with the PeimCount number of Entries.\r
   //\r
   UINT8                               *PeimState;\r
   //\r
-  // Ponter to the buffer with the PcdPeiCoreMaxPeimPerFv number of Entries.\r
+  // Ponter to the buffer with the PeimCount number of Entries.\r
   //\r
   EFI_PEI_FILE_HANDLE                 *FvFileHandles;\r
   BOOLEAN                             ScanFv;\r
@@ -176,6 +177,11 @@ EFI_STATUS
   IN PEI_CORE_INSTANCE              *OldCoreData\r
   );\r
 \r
+//\r
+// Number of files to grow by each time we run out of room\r
+//\r
+#define TEMP_FILE_GROWTH_STEP 32\r
+\r
 #define PEI_CORE_HANDLE_SIGNATURE  SIGNATURE_32('P','e','i','C')\r
 \r
 ///\r
@@ -209,7 +215,7 @@ struct _PEI_CORE_INSTANCE {
   UINTN                              UnknownFvInfoCount;\r
 \r
   ///\r
-  /// Pointer to the buffer with the PcdPeiCoreMaxPeimPerFv number of entries.\r
+  /// Pointer to the buffer FvFileHandlers in PEI_CORE_FV_HANDLE specified by CurrentPeimFvCount.\r
   ///\r
   EFI_PEI_FILE_HANDLE                *CurrentFvFileHandles;\r
   UINTN                              AprioriCount;\r
@@ -256,14 +262,16 @@ struct _PEI_CORE_INSTANCE {
   //\r
   PE_COFF_LOADER_READ_FILE          ShadowedImageRead;\r
 \r
+  UINTN                             TempPeimCount;\r
+\r
   //\r
-  // Pointer to the temp buffer with the PcdPeiCoreMaxPeimPerFv + 1 number of entries.\r
+  // Pointer to the temp buffer with the TempPeimCount number of entries.\r
   //\r
-  EFI_PEI_FILE_HANDLE               *FileHandles;\r
+  EFI_PEI_FILE_HANDLE               *TempFileHandles;\r
   //\r
-  // Pointer to the temp buffer with the PcdPeiCoreMaxPeimPerFv number of entries.\r
+  // Pointer to the temp buffer with the TempPeimCount number of entries.\r
   //\r
-  EFI_GUID                          *FileGuid;\r
+  EFI_GUID                          *TempFileGuid;\r
 \r
   //\r
   // Temp Memory Range is not covered by PeiTempMem and Stack.\r
index 4e1581a926d9f77db014fcd0e846cc61640258ed..d106c3606e9784deef8a34f74c0e37c2abe0fa44 100644 (file)
 \r
 [Pcd]\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdPeiCoreMaxFvSupported                   ## CONSUMES\r
-  gEfiMdeModulePkgTokenSpaceGuid.PcdPeiCoreMaxPeimPerFv                     ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdPeiCoreMaxPpiSupported                  ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdPeiCoreMaxPeiStackSize                  ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdPeiCoreImageLoaderSearchTeSectionFirst  ## CONSUMES\r
index e3a301dfe0f22d0d227d8dd9157580bde6247eba..52adefeb44b4cfc969c1138887a63eb061aff2ed 100644 (file)
@@ -185,27 +185,39 @@ PeiCore (
       if (OldCoreData->HeapOffsetPositive) {\r
         OldCoreData->HobList.Raw = (VOID *)(OldCoreData->HobList.Raw + OldCoreData->HeapOffset);\r
         OldCoreData->UnknownFvInfo        = (PEI_CORE_UNKNOW_FORMAT_FV_INFO *) ((UINT8 *) OldCoreData->UnknownFvInfo + OldCoreData->HeapOffset);\r
-        OldCoreData->CurrentFvFileHandles = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->CurrentFvFileHandles + OldCoreData->HeapOffset);\r
+        if (OldCoreData->CurrentFvFileHandles != NULL) {\r
+          OldCoreData->CurrentFvFileHandles = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->CurrentFvFileHandles + OldCoreData->HeapOffset);\r
+        }\r
         OldCoreData->PpiData.PpiListPtrs  = (PEI_PPI_LIST_POINTERS *) ((UINT8 *) OldCoreData->PpiData.PpiListPtrs + OldCoreData->HeapOffset);\r
         OldCoreData->Fv                   = (PEI_CORE_FV_HANDLE *) ((UINT8 *) OldCoreData->Fv + OldCoreData->HeapOffset);\r
         for (Index = 0; Index < PcdGet32 (PcdPeiCoreMaxFvSupported); Index ++) {\r
-          OldCoreData->Fv[Index].PeimState     = (UINT8 *) OldCoreData->Fv[Index].PeimState + OldCoreData->HeapOffset;\r
-          OldCoreData->Fv[Index].FvFileHandles = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->Fv[Index].FvFileHandles + OldCoreData->HeapOffset);\r
+          if (OldCoreData->Fv[Index].PeimState != NULL) {\r
+            OldCoreData->Fv[Index].PeimState     = (UINT8 *) OldCoreData->Fv[Index].PeimState + OldCoreData->HeapOffset;\r
+          }\r
+          if (OldCoreData->Fv[Index].FvFileHandles != NULL) {\r
+            OldCoreData->Fv[Index].FvFileHandles = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->Fv[Index].FvFileHandles + OldCoreData->HeapOffset);\r
+          }\r
         }\r
-        OldCoreData->FileGuid             = (EFI_GUID *) ((UINT8 *) OldCoreData->FileGuid + OldCoreData->HeapOffset);\r
-        OldCoreData->FileHandles          = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->FileHandles + OldCoreData->HeapOffset);\r
+        OldCoreData->TempFileGuid         = (EFI_GUID *) ((UINT8 *) OldCoreData->TempFileGuid + OldCoreData->HeapOffset);\r
+        OldCoreData->TempFileHandles      = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->TempFileHandles + OldCoreData->HeapOffset);\r
       } else {\r
         OldCoreData->HobList.Raw = (VOID *)(OldCoreData->HobList.Raw - OldCoreData->HeapOffset);\r
         OldCoreData->UnknownFvInfo        = (PEI_CORE_UNKNOW_FORMAT_FV_INFO *) ((UINT8 *) OldCoreData->UnknownFvInfo - OldCoreData->HeapOffset);\r
-        OldCoreData->CurrentFvFileHandles = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->CurrentFvFileHandles - OldCoreData->HeapOffset);\r
+        if (OldCoreData->CurrentFvFileHandles != NULL) {\r
+          OldCoreData->CurrentFvFileHandles = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->CurrentFvFileHandles - OldCoreData->HeapOffset);\r
+        }\r
         OldCoreData->PpiData.PpiListPtrs  = (PEI_PPI_LIST_POINTERS *) ((UINT8 *) OldCoreData->PpiData.PpiListPtrs - OldCoreData->HeapOffset);\r
         OldCoreData->Fv                   = (PEI_CORE_FV_HANDLE *) ((UINT8 *) OldCoreData->Fv - OldCoreData->HeapOffset);\r
         for (Index = 0; Index < PcdGet32 (PcdPeiCoreMaxFvSupported); Index ++) {\r
-          OldCoreData->Fv[Index].PeimState     = (UINT8 *) OldCoreData->Fv[Index].PeimState - OldCoreData->HeapOffset;\r
-          OldCoreData->Fv[Index].FvFileHandles = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->Fv[Index].FvFileHandles - OldCoreData->HeapOffset);\r
+          if (OldCoreData->Fv[Index].PeimState != NULL) {\r
+            OldCoreData->Fv[Index].PeimState     = (UINT8 *) OldCoreData->Fv[Index].PeimState - OldCoreData->HeapOffset;\r
+          }\r
+          if (OldCoreData->Fv[Index].FvFileHandles != NULL) {\r
+            OldCoreData->Fv[Index].FvFileHandles = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->Fv[Index].FvFileHandles - OldCoreData->HeapOffset);\r
+          }\r
         }\r
-        OldCoreData->FileGuid             = (EFI_GUID *) ((UINT8 *) OldCoreData->FileGuid - OldCoreData->HeapOffset);\r
-        OldCoreData->FileHandles          = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->FileHandles - OldCoreData->HeapOffset);\r
+        OldCoreData->TempFileGuid         = (EFI_GUID *) ((UINT8 *) OldCoreData->TempFileGuid - OldCoreData->HeapOffset);\r
+        OldCoreData->TempFileHandles      = (EFI_PEI_FILE_HANDLE *) ((UINT8 *) OldCoreData->TempFileHandles - OldCoreData->HeapOffset);\r
       }\r
 \r
       //\r
@@ -320,7 +332,7 @@ PeiCore (
   //\r
   // Initialize PEI Core Services\r
   //\r
-  InitializeMemoryServices   (&PrivateData,    SecCoreData, OldCoreData);\r
+  InitializeMemoryServices   (&PrivateData, SecCoreData, OldCoreData);\r
   if (OldCoreData == NULL) {\r
     //\r
     // Initialize PEI Core Private Data Buffer\r
@@ -329,22 +341,8 @@ PeiCore (
     ASSERT (PrivateData.PpiData.PpiListPtrs != NULL);\r
     PrivateData.Fv                   = AllocateZeroPool (sizeof (PEI_CORE_FV_HANDLE) * PcdGet32 (PcdPeiCoreMaxFvSupported));\r
     ASSERT (PrivateData.Fv != NULL);\r
-    PrivateData.Fv[0].PeimState      = AllocateZeroPool (sizeof (UINT8) * PcdGet32 (PcdPeiCoreMaxPeimPerFv) * PcdGet32 (PcdPeiCoreMaxFvSupported));\r
-    ASSERT (PrivateData.Fv[0].PeimState != NULL);\r
-    PrivateData.Fv[0].FvFileHandles  = AllocateZeroPool (sizeof (EFI_PEI_FILE_HANDLE) * PcdGet32 (PcdPeiCoreMaxPeimPerFv) * PcdGet32 (PcdPeiCoreMaxFvSupported));\r
-    ASSERT (PrivateData.Fv[0].FvFileHandles != NULL);\r
-    for (Index = 1; Index < PcdGet32 (PcdPeiCoreMaxFvSupported); Index ++) {\r
-      PrivateData.Fv[Index].PeimState     = PrivateData.Fv[Index - 1].PeimState + PcdGet32 (PcdPeiCoreMaxPeimPerFv);\r
-      PrivateData.Fv[Index].FvFileHandles = PrivateData.Fv[Index - 1].FvFileHandles + PcdGet32 (PcdPeiCoreMaxPeimPerFv);\r
-    }\r
     PrivateData.UnknownFvInfo        = AllocateZeroPool (sizeof (PEI_CORE_UNKNOW_FORMAT_FV_INFO) * PcdGet32 (PcdPeiCoreMaxFvSupported));\r
     ASSERT (PrivateData.UnknownFvInfo != NULL);\r
-    PrivateData.CurrentFvFileHandles = AllocateZeroPool (sizeof (EFI_PEI_FILE_HANDLE) * PcdGet32 (PcdPeiCoreMaxPeimPerFv));\r
-    ASSERT (PrivateData.CurrentFvFileHandles != NULL);\r
-    PrivateData.FileGuid             = AllocatePool (sizeof (EFI_GUID) * PcdGet32 (PcdPeiCoreMaxPeimPerFv));\r
-    ASSERT (PrivateData.FileGuid != NULL);\r
-    PrivateData.FileHandles          = AllocatePool (sizeof (EFI_PEI_FILE_HANDLE) * (PcdGet32 (PcdPeiCoreMaxPeimPerFv) + 1));\r
-    ASSERT (PrivateData.FileHandles != NULL);\r
   }\r
   InitializePpiServices      (&PrivateData,    OldCoreData);\r
 \r