]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Pei/Dispatcher/Dispatcher.c
IntelSiliconPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Dispatcher / Dispatcher.c
index 38299c5d98c653042af5cb27a42739e394e8886c..68670f43e0b3f97a0e6e7c2ae1152e1e3f882a16 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   EFI PEI Core dispatch services\r
-  \r
-Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
+\r
+Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
@@ -15,11 +15,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include "PeiMain.h"\r
 \r
-///\r
-/// temporary memory is filled with this initial value during SEC phase\r
-///\r
-#define INIT_CAR_VALUE 0x5AA55AA5\r
-\r
 /**\r
 \r
   Discover all Peims and optional Apriori file in one FV. There is at most one\r
@@ -46,48 +41,91 @@ 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
+\r
   FvPpi = CoreFileHandle->FvPpi;\r
-  \r
+\r
   //\r
   // 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
@@ -101,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
@@ -118,84 +156,68 @@ 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
 // This is the minimum memory required by DxeCore initialization. When LMFA feature enabled,\r
-// This part of memory still need reserved on the very top of memory so that the DXE Core could  \r
+// This part of memory still need reserved on the very top of memory so that the DXE Core could\r
 // use these memory for data initialization. This macro should be sync with the same marco\r
 // defined in DXE Core.\r
 //\r
 #define MINIMUM_INITIAL_MEMORY_SIZE 0x10000\r
 /**\r
-  This function is to test if the memory range described in resource HOB is available or not. \r
-  \r
-  This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. Some platform may allocate the \r
+  This function is to test if the memory range described in resource HOB is available or not.\r
+\r
+  This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. Some platform may allocate the\r
   memory before PeiLoadFixAddressHook in invoked. so this function is to test if the memory range described by the input resource HOB is\r
   available or not.\r
 \r
@@ -208,41 +230,41 @@ PeiLoadFixAddressIsMemoryRangeAvailable (
   IN EFI_HOB_RESOURCE_DESCRIPTOR        *ResourceHob\r
   )\r
 {\r
-       EFI_HOB_MEMORY_ALLOCATION          *MemoryHob;\r
-       BOOLEAN                             IsAvailable;\r
-       EFI_PEI_HOB_POINTERS                Hob;\r
-       \r
+  EFI_HOB_MEMORY_ALLOCATION          *MemoryHob;\r
+  BOOLEAN                             IsAvailable;\r
+  EFI_PEI_HOB_POINTERS                Hob;\r
+\r
   IsAvailable = TRUE;\r
-       if (PrivateData == NULL || ResourceHob == NULL) {\r
-         return FALSE;\r
-       }\r
-       //\r
+  if (PrivateData == NULL || ResourceHob == NULL) {\r
+    return FALSE;\r
+  }\r
+  //\r
   // test if the memory range describe in the HOB is already allocated.\r
   //\r
   for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
-    //                                                              \r
-    // See if this is a memory allocation HOB                     \r
     //\r
-    if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) { \r
+    // See if this is a memory allocation HOB\r
+    //\r
+    if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {\r
       MemoryHob = Hob.MemoryAllocation;\r
-      if(MemoryHob->AllocDescriptor.MemoryBaseAddress == ResourceHob->PhysicalStart && \r
+      if(MemoryHob->AllocDescriptor.MemoryBaseAddress == ResourceHob->PhysicalStart &&\r
          MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength == ResourceHob->PhysicalStart + ResourceHob->ResourceLength) {\r
          IsAvailable = FALSE;\r
-         break;  \r
+         break;\r
        }\r
      }\r
   }\r
-  \r
+\r
   return IsAvailable;\r
-       \r
+\r
 }\r
 /**\r
   Hook function for Loading Module at Fixed Address feature\r
-  \r
+\r
   This function should only be invoked when Loading Module at Fixed Address(LMFA) feature is enabled. When feature is\r
-  configured as Load Modules at Fix Absolute Address, this function is to validate the top address assigned by user. When \r
-  feature is configured as Load Modules at Fixed Offset, the functino is to find the top address which is TOLM-TSEG in general.  \r
-  And also the function will re-install PEI memory. \r
+  configured as Load Modules at Fix Absolute Address, this function is to validate the top address assigned by user. When\r
+  feature is configured as Load Modules at Fixed Offset, the functino is to find the top address which is TOLM-TSEG in general.\r
+  And also the function will re-install PEI memory.\r
 \r
   @param PrivateData         Pointer to the private data passed in from caller\r
 \r
@@ -256,7 +278,7 @@ PeiLoadFixAddressHook(
   UINT64                             PeiMemorySize;\r
   UINT64                             TotalReservedMemorySize;\r
   UINT64                             MemoryRangeEnd;\r
-  EFI_PHYSICAL_ADDRESS               HighAddress; \r
+  EFI_PHYSICAL_ADDRESS               HighAddress;\r
   EFI_HOB_RESOURCE_DESCRIPTOR        *ResourceHob;\r
   EFI_HOB_RESOURCE_DESCRIPTOR        *NextResourceHob;\r
   EFI_HOB_RESOURCE_DESCRIPTOR        *CurrentResourceHob;\r
@@ -278,89 +300,89 @@ PeiLoadFixAddressHook(
   //\r
   // The top reserved memory include 3 parts: the topest range is for DXE core initialization with the size  MINIMUM_INITIAL_MEMORY_SIZE\r
   // then RuntimeCodePage range and Boot time code range.\r
-  //  \r
+  //\r
   TotalReservedMemorySize = MINIMUM_INITIAL_MEMORY_SIZE + EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber));\r
-  TotalReservedMemorySize+= EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber)) ;  \r
+  TotalReservedMemorySize+= EFI_PAGES_TO_SIZE(PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber)) ;\r
   //\r
   // PEI memory range lies below the top reserved memory\r
-  // \r
+  //\r
   TotalReservedMemorySize += PeiMemorySize;\r
-  \r
+\r
   DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressRuntimeCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressRuntimeCodePageNumber)));\r
   DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressBootTimeCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressBootTimeCodePageNumber)));\r
-  DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressPeiCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressPeiCodePageNumber)));   \r
+  DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: PcdLoadFixAddressPeiCodePageNumber= 0x%x.\n", PcdGet32(PcdLoadFixAddressPeiCodePageNumber)));\r
   DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Total Reserved Memory Size = 0x%lx.\n", TotalReservedMemorySize));\r
   //\r
-  // Loop through the system memory typed hob to merge the adjacent memory range \r
+  // Loop through the system memory typed hob to merge the adjacent memory range\r
   //\r
   for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
-    //                                                              \r
-    // See if this is a resource descriptor HOB                     \r
+    //\r
+    // See if this is a resource descriptor HOB\r
     //\r
     if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
-      \r
-      ResourceHob = Hob.ResourceDescriptor;  \r
+\r
+      ResourceHob = Hob.ResourceDescriptor;\r
       //\r
       // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored.\r
       //\r
       if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY ||\r
           ResourceHob->PhysicalStart + ResourceHob->ResourceLength > MAX_ADDRESS)   {\r
         continue;\r
-      }   \r
-      \r
-      for (NextHob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(NextHob); NextHob.Raw = GET_NEXT_HOB(NextHob)) {       \r
+      }\r
+\r
+      for (NextHob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(NextHob); NextHob.Raw = GET_NEXT_HOB(NextHob)) {\r
         if (NextHob.Raw == Hob.Raw){\r
           continue;\r
-        }  \r
+        }\r
         //\r
         // See if this is a resource descriptor HOB\r
         //\r
         if (GET_HOB_TYPE (NextHob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
-      \r
+\r
           NextResourceHob = NextHob.ResourceDescriptor;\r
           //\r
           // test if range described in this NextResourceHob is system memory and have the same attribute.\r
           // Note: Here is a assumption that system memory should always be healthy even without test.\r
-          //    \r
+          //\r
           if (NextResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
              (((NextResourceHob->ResourceAttribute^ResourceHob->ResourceAttribute)&(~EFI_RESOURCE_ATTRIBUTE_TESTED)) == 0)){\r
-              \r
+\r
               //\r
               // See if the memory range described in ResourceHob and NextResourceHob is adjacent\r
               //\r
-              if ((ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart && \r
-                    ResourceHob->PhysicalStart + ResourceHob->ResourceLength >= NextResourceHob->PhysicalStart)|| \r
+              if ((ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart &&\r
+                    ResourceHob->PhysicalStart + ResourceHob->ResourceLength >= NextResourceHob->PhysicalStart)||\r
                   (ResourceHob->PhysicalStart >= NextResourceHob->PhysicalStart&&\r
                      ResourceHob->PhysicalStart <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) {\r
-             \r
+\r
                 MemoryRangeEnd = ((ResourceHob->PhysicalStart + ResourceHob->ResourceLength)>(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength)) ?\r
                                      (ResourceHob->PhysicalStart + ResourceHob->ResourceLength):(NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength);\r
-          \r
-                ResourceHob->PhysicalStart = (ResourceHob->PhysicalStart < NextResourceHob->PhysicalStart) ? \r
+\r
+                ResourceHob->PhysicalStart = (ResourceHob->PhysicalStart < NextResourceHob->PhysicalStart) ?\r
                                                     ResourceHob->PhysicalStart : NextResourceHob->PhysicalStart;\r
-                \r
-               \r
+\r
+\r
                 ResourceHob->ResourceLength = (MemoryRangeEnd - ResourceHob->PhysicalStart);\r
-                \r
+\r
                 ResourceHob->ResourceAttribute = ResourceHob->ResourceAttribute & (~EFI_RESOURCE_ATTRIBUTE_TESTED);\r
                 //\r
                 // Delete the NextResourceHob by marking it as unused.\r
                 //\r
                 GET_HOB_TYPE (NextHob) = EFI_HOB_TYPE_UNUSED;\r
-                \r
+\r
               }\r
            }\r
-        } \r
+        }\r
       }\r
-    } \r
+    }\r
   }\r
   //\r
   // Some platform is already allocated pages before the HOB re-org. Here to build dedicated resource HOB to describe\r
   //  the allocated memory range\r
   //\r
   for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
-    //                                                              \r
-    // See if this is a memory allocation HOB                     \r
+    //\r
+    // See if this is a memory allocation HOB\r
     //\r
     if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_MEMORY_ALLOCATION) {\r
       MemoryHob = Hob.MemoryAllocation;\r
@@ -369,7 +391,7 @@ PeiLoadFixAddressHook(
         // See if this is a resource descriptor HOB\r
         //\r
         if (GET_HOB_TYPE (NextHob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
-               NextResourceHob = NextHob.ResourceDescriptor;\r
+          NextResourceHob = NextHob.ResourceDescriptor;\r
           //\r
           // If range described in this hob is not system memory or heigher than MAX_ADDRESS, ignored.\r
           //\r
@@ -378,26 +400,26 @@ PeiLoadFixAddressHook(
           }\r
           //\r
           // If the range describe in memory allocation HOB  belongs to the memroy range described by the resource hob\r
-          //          \r
-          if (MemoryHob->AllocDescriptor.MemoryBaseAddress >= NextResourceHob->PhysicalStart && \r
+          //\r
+          if (MemoryHob->AllocDescriptor.MemoryBaseAddress >= NextResourceHob->PhysicalStart &&\r
               MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength <= NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength) {\r
              //\r
              // Build seperate resource hob for this allocated range\r
-             //                     \r
+             //\r
              if (MemoryHob->AllocDescriptor.MemoryBaseAddress > NextResourceHob->PhysicalStart) {\r
                BuildResourceDescriptorHob (\r
-                 EFI_RESOURCE_SYSTEM_MEMORY,                       \r
+                 EFI_RESOURCE_SYSTEM_MEMORY,\r
                  NextResourceHob->ResourceAttribute,\r
-                 NextResourceHob->PhysicalStart,                             \r
-                 (MemoryHob->AllocDescriptor.MemoryBaseAddress - NextResourceHob->PhysicalStart)      \r
+                 NextResourceHob->PhysicalStart,\r
+                 (MemoryHob->AllocDescriptor.MemoryBaseAddress - NextResourceHob->PhysicalStart)\r
                );\r
              }\r
              if (MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength < NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength) {\r
                BuildResourceDescriptorHob (\r
-                 EFI_RESOURCE_SYSTEM_MEMORY,                       \r
+                 EFI_RESOURCE_SYSTEM_MEMORY,\r
                  NextResourceHob->ResourceAttribute,\r
-                 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength,                            \r
-                 (NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength -(MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength))    \r
+                 MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength,\r
+                 (NextResourceHob->PhysicalStart + NextResourceHob->ResourceLength -(MemoryHob->AllocDescriptor.MemoryBaseAddress + MemoryHob->AllocDescriptor.MemoryLength))\r
                );\r
              }\r
              NextResourceHob->PhysicalStart = MemoryHob->AllocDescriptor.MemoryBaseAddress;\r
@@ -411,7 +433,7 @@ PeiLoadFixAddressHook(
 \r
   //\r
   // Try to find and validate the TOP address.\r
-  //  \r
+  //\r
   if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0 ) {\r
     //\r
     // The LMFA feature is enabled as load module at fixed absolute address.\r
@@ -422,11 +444,11 @@ PeiLoadFixAddressHook(
     // validate the Address. Loop the resource descriptor HOB to make sure the address is in valid memory range\r
     //\r
     if ((TopLoadingAddress & EFI_PAGE_MASK) != 0) {\r
-      DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid since top address should be page align. \n", TopLoadingAddress)); \r
-      ASSERT (FALSE);    \r
+      DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid since top address should be page align. \n", TopLoadingAddress));\r
+      ASSERT (FALSE);\r
     }\r
     //\r
-    // Search for a memory region that is below MAX_ADDRESS and in which TopLoadingAddress lies \r
+    // Search for a memory region that is below MAX_ADDRESS and in which TopLoadingAddress lies\r
     //\r
     for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
       //\r
@@ -437,61 +459,61 @@ PeiLoadFixAddressHook(
         ResourceHob = Hob.ResourceDescriptor;\r
         //\r
         // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
-        //    \r
+        //\r
         if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
             ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) {\r
             //\r
             // See if Top address specified by user is valid.\r
             //\r
-            if (ResourceHob->PhysicalStart + TotalReservedMemorySize < TopLoadingAddress && \r
-                (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - MINIMUM_INITIAL_MEMORY_SIZE) >= TopLoadingAddress && \r
+            if (ResourceHob->PhysicalStart + TotalReservedMemorySize < TopLoadingAddress &&\r
+                (ResourceHob->PhysicalStart + ResourceHob->ResourceLength - MINIMUM_INITIAL_MEMORY_SIZE) >= TopLoadingAddress &&\r
                 PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
-              CurrentResourceHob = ResourceHob; \r
+              CurrentResourceHob = ResourceHob;\r
               CurrentHob = Hob;\r
               break;\r
            }\r
         }\r
-      }  \r
-    }  \r
+      }\r
+    }\r
     if (CurrentResourceHob != NULL) {\r
       DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO:Top Address 0x%lx is valid \n",  TopLoadingAddress));\r
-      TopLoadingAddress += MINIMUM_INITIAL_MEMORY_SIZE; \r
+      TopLoadingAddress += MINIMUM_INITIAL_MEMORY_SIZE;\r
     } else {\r
-      DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid \n",  TopLoadingAddress)); \r
-      DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The recommended Top Address for the platform is: \n")); \r
+      DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:Top Address 0x%lx is invalid \n",  TopLoadingAddress));\r
+      DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The recommended Top Address for the platform is: \n"));\r
       //\r
       // Print the recomended Top address range.\r
-      // \r
+      //\r
       for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
         //\r
         // See if this is a resource descriptor HOB\r
         //\r
         if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
-        \r
+\r
           ResourceHob = Hob.ResourceDescriptor;\r
           //\r
           // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
-          //    \r
+          //\r
           if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
               ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS) {\r
               //\r
               // See if Top address specified by user is valid.\r
               //\r
               if (ResourceHob->ResourceLength > TotalReservedMemorySize && PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
-                 DEBUG ((EFI_D_INFO, "(0x%lx, 0x%lx)\n",  \r
-                          (ResourceHob->PhysicalStart + TotalReservedMemorySize -MINIMUM_INITIAL_MEMORY_SIZE), \r
-                          (ResourceHob->PhysicalStart + ResourceHob->ResourceLength -MINIMUM_INITIAL_MEMORY_SIZE) \r
-                        )); \r
+                 DEBUG ((EFI_D_INFO, "(0x%lx, 0x%lx)\n",\r
+                          (ResourceHob->PhysicalStart + TotalReservedMemorySize -MINIMUM_INITIAL_MEMORY_SIZE),\r
+                          (ResourceHob->PhysicalStart + ResourceHob->ResourceLength -MINIMUM_INITIAL_MEMORY_SIZE)\r
+                        ));\r
               }\r
           }\r
         }\r
-      }  \r
+      }\r
       //\r
-      // Assert here \r
+      // Assert here\r
       //\r
-      ASSERT (FALSE);   \r
-      return;   \r
-    }     \r
+      ASSERT (FALSE);\r
+      return;\r
+    }\r
   } else {\r
     //\r
     // The LMFA feature is enabled as load module at fixed offset relative to TOLM\r
@@ -502,15 +524,15 @@ PeiLoadFixAddressHook(
     //\r
     for (Hob.Raw = PrivateData->HobList.Raw; !END_OF_HOB_LIST(Hob); Hob.Raw = GET_NEXT_HOB(Hob)) {\r
       //\r
-      // See if this is a resource descriptor HOB \r
+      // See if this is a resource descriptor HOB\r
       //\r
       if (GET_HOB_TYPE (Hob) == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
-        \r
-        ResourceHob = Hob.ResourceDescriptor;                                                                                                                                                                                                                               \r
+\r
+        ResourceHob = Hob.ResourceDescriptor;\r
         //\r
         // See if this resource descrior HOB describes tested system memory below MAX_ADDRESS\r
         //\r
-        if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY && \r
+        if (ResourceHob->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY &&\r
             ResourceHob->PhysicalStart + ResourceHob->ResourceLength <= MAX_ADDRESS &&\r
             ResourceHob->ResourceLength > TotalReservedMemorySize && PeiLoadFixAddressIsMemoryRangeAvailable(PrivateData, ResourceHob)) {\r
           //\r
@@ -522,26 +544,26 @@ PeiLoadFixAddressHook(
              HighAddress = CurrentResourceHob->PhysicalStart;\r
           }\r
         }\r
-      }  \r
+      }\r
     }\r
     if (CurrentResourceHob == NULL) {\r
-      DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The System Memory is too small\n")); \r
+      DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR:The System Memory is too small\n"));\r
       //\r
-      // Assert here \r
+      // Assert here\r
       //\r
       ASSERT (FALSE);\r
-      return;  \r
+      return;\r
     } else {\r
-      TopLoadingAddress = CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength ; \r
-    }         \r
+      TopLoadingAddress = CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength ;\r
+    }\r
   }\r
-  \r
+\r
   if (CurrentResourceHob != NULL) {\r
     //\r
     // rebuild resource HOB for PEI memmory and reserved memory\r
     //\r
     BuildResourceDescriptorHob (\r
-      EFI_RESOURCE_SYSTEM_MEMORY,                       \r
+      EFI_RESOURCE_SYSTEM_MEMORY,\r
       (\r
       EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
       EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
@@ -551,15 +573,15 @@ PeiLoadFixAddressHook(
       EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
       EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
       ),\r
-      (TopLoadingAddress - TotalReservedMemorySize),                             \r
-      TotalReservedMemorySize     \r
+      (TopLoadingAddress - TotalReservedMemorySize),\r
+      TotalReservedMemorySize\r
     );\r
     //\r
     // rebuild resource for the remain memory if necessary\r
     //\r
     if (CurrentResourceHob->PhysicalStart < TopLoadingAddress - TotalReservedMemorySize) {\r
       BuildResourceDescriptorHob (\r
-        EFI_RESOURCE_SYSTEM_MEMORY,                       \r
+        EFI_RESOURCE_SYSTEM_MEMORY,\r
         (\r
          EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
          EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
@@ -568,13 +590,13 @@ PeiLoadFixAddressHook(
          EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
          EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
          ),\r
-         CurrentResourceHob->PhysicalStart,                             \r
-         (TopLoadingAddress - TotalReservedMemorySize - CurrentResourceHob->PhysicalStart)      \r
+         CurrentResourceHob->PhysicalStart,\r
+         (TopLoadingAddress - TotalReservedMemorySize - CurrentResourceHob->PhysicalStart)\r
        );\r
     }\r
     if (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength  > TopLoadingAddress ) {\r
       BuildResourceDescriptorHob (\r
-        EFI_RESOURCE_SYSTEM_MEMORY,                     \r
+        EFI_RESOURCE_SYSTEM_MEMORY,\r
         (\r
          EFI_RESOURCE_ATTRIBUTE_PRESENT |\r
          EFI_RESOURCE_ATTRIBUTE_INITIALIZED |\r
@@ -583,21 +605,21 @@ PeiLoadFixAddressHook(
          EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |\r
          EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE\r
          ),\r
-         TopLoadingAddress,                            \r
-         (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength  - TopLoadingAddress)     \r
+         TopLoadingAddress,\r
+         (CurrentResourceHob->PhysicalStart + CurrentResourceHob->ResourceLength  - TopLoadingAddress)\r
        );\r
     }\r
     //\r
     // Delete CurrentHob by marking it as unused since the the memory range described by is rebuilt.\r
     //\r
-    GET_HOB_TYPE (CurrentHob) = EFI_HOB_TYPE_UNUSED;         \r
+    GET_HOB_TYPE (CurrentHob) = EFI_HOB_TYPE_UNUSED;\r
   }\r
 \r
   //\r
   // Cache the top address for Loading Module at Fixed Address feature\r
   //\r
   PrivateData->LoadModuleAtFixAddressTopAddress = TopLoadingAddress - MINIMUM_INITIAL_MEMORY_SIZE;\r
-  DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Top address = 0x%lx\n",  PrivateData->LoadModuleAtFixAddressTopAddress)); \r
+  DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: Top address = 0x%lx\n",  PrivateData->LoadModuleAtFixAddressTopAddress));\r
   //\r
   // reinstall the PEI memory relative to TopLoadingAddress\r
   //\r
@@ -680,8 +702,9 @@ PeiCheckAndSwitchStack (
 \r
       for (StackPointer = (UINT32*)SecCoreData->StackBase;\r
            (StackPointer < (UINT32*)((UINTN)SecCoreData->StackBase + SecCoreData->StackSize)) \\r
-           && (*StackPointer == INIT_CAR_VALUE);\r
-           StackPointer ++);\r
+           && (*StackPointer == PcdGet32 (PcdInitValueInTempStack));\r
+           StackPointer ++) {\r
+      }\r
 \r
       DEBUG ((DEBUG_INFO, "Temp Stack : BaseAddress=0x%p Length=0x%X\n", SecCoreData->StackBase, (UINT32)SecCoreData->StackSize));\r
       DEBUG ((DEBUG_INFO, "Temp Heap  : BaseAddress=0x%p Length=0x%X\n", SecCoreData->PeiTemporaryRamBase, (UINT32)SecCoreData->PeiTemporaryRamSize));\r
@@ -733,7 +756,7 @@ PeiCheckAndSwitchStack (
     ASSERT (NewStackSize >= SecCoreData->StackSize);\r
 \r
     //\r
-    // Calculate stack offset and heap offset between temporary memory and new permement \r
+    // Calculate stack offset and heap offset between temporary memory and new permement\r
     // memory seperately.\r
     //\r
     TopOfOldStack = (UINTN)SecCoreData->StackBase + SecCoreData->StackSize;\r
@@ -800,9 +823,9 @@ PeiCheckAndSwitchStack (
       }\r
 \r
       //\r
-      // Temporary Ram Support PPI is provided by platform, it will copy \r
+      // Temporary Ram Support PPI is provided by platform, it will copy\r
       // temporary memory to permanent memory and do stack switching.\r
-      // After invoking Temporary Ram Support PPI, the following code's \r
+      // After invoking Temporary Ram Support PPI, the following code's\r
       // stack is in permanent memory.\r
       //\r
       TemporaryRamSupportPpi->TemporaryRamMigration (\r
@@ -965,7 +988,7 @@ PeiDispatcher (
   EFI_PEI_FILE_HANDLE                 SaveCurrentFileHandle;\r
   EFI_FV_FILE_INFO                    FvFileInfo;\r
   PEI_CORE_FV_HANDLE                  *CoreFvHandle;\r
-  \r
+\r
   PeiServices = (CONST EFI_PEI_SERVICES **) &Private->Ps;\r
   PeimEntryPoint = NULL;\r
   PeimFileHandle = NULL;\r
@@ -974,15 +997,15 @@ PeiDispatcher (
   if ((Private->PeiMemoryInstalled) && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME || PcdGetBool (PcdShadowPeimOnS3Boot))) {\r
     //\r
     // Once real memory is available, shadow the RegisterForShadow modules. And meanwhile\r
-    // update the modules' status from PEIM_STATE_REGISITER_FOR_SHADOW to PEIM_STATE_DONE.\r
+    // update the modules' status from PEIM_STATE_REGISTER_FOR_SHADOW to PEIM_STATE_DONE.\r
     //\r
     SaveCurrentPeimCount  = Private->CurrentPeimCount;\r
     SaveCurrentFvCount    = Private->CurrentPeimFvCount;\r
     SaveCurrentFileHandle =  Private->CurrentFileHandle;\r
 \r
-    for (Index1 = 0; Index1 <= SaveCurrentFvCount; Index1++) {\r
-      for (Index2 = 0; (Index2 < PcdGet32 (PcdPeiCoreMaxPeimPerFv)) && (Private->Fv[Index1].FvFileHandles[Index2] != NULL); Index2++) {\r
-        if (Private->Fv[Index1].PeimState[Index2] == PEIM_STATE_REGISITER_FOR_SHADOW) {\r
+    for (Index1 = 0; Index1 < Private->FvCount; Index1++) {\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
           Private->CurrentPeimFvCount  = Index1;\r
@@ -990,13 +1013,13 @@ PeiDispatcher (
           Status = PeiLoadImage (\r
                     (CONST EFI_PEI_SERVICES **) &Private->Ps,\r
                     PeimFileHandle,\r
-                    PEIM_STATE_REGISITER_FOR_SHADOW,\r
+                    PEIM_STATE_REGISTER_FOR_SHADOW,\r
                     &EntryPoint,\r
                     &AuthenticationState\r
                     );\r
           if (Status == EFI_SUCCESS) {\r
             //\r
-            // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
+            // PEIM_STATE_REGISTER_FOR_SHADOW move to PEIM_STATE_DONE\r
             //\r
             Private->Fv[Index1].PeimState[Index2]++;\r
             //\r
@@ -1004,16 +1027,16 @@ PeiDispatcher (
             //\r
             PeimEntryPoint = (EFI_PEIM_ENTRY_POINT2)(UINTN)EntryPoint;\r
 \r
-            PERF_START (PeimFileHandle, "PEIM", NULL, 0);\r
+            PERF_START_IMAGE_BEGIN (PeimFileHandle);\r
             PeimEntryPoint(PeimFileHandle, (const EFI_PEI_SERVICES **) &Private->Ps);\r
-            PERF_END (PeimFileHandle, "PEIM", NULL, 0);\r
+            PERF_START_IMAGE_END (PeimFileHandle);\r
           }\r
 \r
           //\r
           // Process the Notify list and dispatch any notifies for\r
           // newly installed PPIs.\r
           //\r
-          ProcessNotifyList (Private);\r
+          ProcessDispatchNotifyList (Private);\r
         }\r
       }\r
     }\r
@@ -1032,7 +1055,7 @@ PeiDispatcher (
   //\r
   do {\r
     //\r
-    // In case that reenter PeiCore happens, the last pass record is still available.   \r
+    // In case that reenter PeiCore happens, the last pass record is still available.\r
     //\r
     if (!Private->PeimDispatcherReenter) {\r
       Private->PeimNeedingDispatch      = FALSE;\r
@@ -1040,18 +1063,18 @@ PeiDispatcher (
     } else {\r
       Private->PeimDispatcherReenter    = FALSE;\r
     }\r
-    \r
+\r
     for (FvCount = Private->CurrentPeimFvCount; FvCount < Private->FvCount; FvCount++) {\r
       CoreFvHandle = FindNextCoreFvHandle (Private, FvCount);\r
       ASSERT (CoreFvHandle != NULL);\r
-      \r
+\r
       //\r
       // If the FV has corresponding EFI_PEI_FIRMWARE_VOLUME_PPI instance, then dispatch it.\r
       //\r
       if (CoreFvHandle->FvPpi == NULL) {\r
         continue;\r
       }\r
-      \r
+\r
       Private->CurrentPeimFvCount = FvCount;\r
 \r
       if (Private->CurrentPeimCount == 0) {\r
@@ -1067,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
@@ -1113,7 +1136,7 @@ PeiDispatcher (
                 // The PEIM has its dependencies satisfied, and its entry point\r
                 // has been found, so invoke it.\r
                 //\r
-                PERF_START (PeimFileHandle, "PEIM", NULL, 0);\r
+                PERF_START_IMAGE_BEGIN (PeimFileHandle);\r
 \r
                 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
                   EFI_PROGRESS_CODE,\r
@@ -1149,7 +1172,7 @@ PeiDispatcher (
                   (VOID *)(&PeimFileHandle),\r
                   sizeof (PeimFileHandle)\r
                   );\r
-                PERF_END (PeimFileHandle, "PEIM", NULL, 0);\r
+                PERF_START_IMAGE_END (PeimFileHandle);\r
 \r
               }\r
             }\r
@@ -1160,16 +1183,16 @@ PeiDispatcher (
             // Process the Notify list and dispatch any notifies for\r
             // newly installed PPIs.\r
             //\r
-            ProcessNotifyList (Private);\r
+            ProcessDispatchNotifyList (Private);\r
 \r
             //\r
-            // Recheck SwitchStackSignal after ProcessNotifyList()\r
+            // Recheck SwitchStackSignal after ProcessDispatchNotifyList()\r
             // in case PeiInstallPeiMemory() is done in a callback with\r
             // EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH.\r
             //\r
             PeiCheckAndSwitchStack (SecCoreData, Private);\r
 \r
-            if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISITER_FOR_SHADOW) &&   \\r
+            if ((Private->PeiMemoryInstalled) && (Private->Fv[FvCount].PeimState[PeimCount] == PEIM_STATE_REGISTER_FOR_SHADOW) &&   \\r
                 (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME || PcdGetBool (PcdShadowPeimOnS3Boot))) {\r
               //\r
               // If memory is available we shadow images by default for performance reasons.\r
@@ -1183,7 +1206,7 @@ PeiDispatcher (
                 Status = PeiLoadImage (\r
                            PeiServices,\r
                            PeimFileHandle,\r
-                           PEIM_STATE_REGISITER_FOR_SHADOW,\r
+                           PEIM_STATE_REGISTER_FOR_SHADOW,\r
                            &EntryPoint,\r
                            &AuthenticationState\r
                            );\r
@@ -1196,7 +1219,7 @@ PeiDispatcher (
               //PERF_END (PeiServices, L"PEIM", PeimFileHandle, 0);\r
 \r
               //\r
-              // PEIM_STATE_REGISITER_FOR_SHADOW move to PEIM_STATE_DONE\r
+              // PEIM_STATE_REGISTER_FOR_SHADOW move to PEIM_STATE_DONE\r
               //\r
               Private->Fv[FvCount].PeimState[PeimCount]++;\r
 \r
@@ -1204,28 +1227,24 @@ PeiDispatcher (
               // Process the Notify list and dispatch any notifies for\r
               // newly installed PPIs.\r
               //\r
-              ProcessNotifyList (Private);\r
+              ProcessDispatchNotifyList (Private);\r
             }\r
           }\r
         }\r
       }\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
@@ -1301,10 +1320,10 @@ DepexSatisfied (
   } else {\r
     DEBUG ((DEBUG_DISPATCH, "Evaluate PEI DEPEX for FFS(%g)\n", &FileInfo.FileName));\r
   }\r
-  \r
+\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
@@ -1360,14 +1379,14 @@ PeiRegisterForShadow (
     return EFI_NOT_FOUND;\r
   }\r
 \r
-  if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISITER_FOR_SHADOW) {\r
+  if (Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] >= PEIM_STATE_REGISTER_FOR_SHADOW) {\r
     //\r
     // If the PEIM has already entered the PEIM_STATE_REGISTER_FOR_SHADOW or PEIM_STATE_DONE then it's already been started\r
     //\r
     return EFI_ALREADY_STARTED;\r
   }\r
 \r
-  Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISITER_FOR_SHADOW;\r
+  Private->Fv[Private->CurrentPeimFvCount].PeimState[Private->CurrentPeimCount] = PEIM_STATE_REGISTER_FOR_SHADOW;\r
 \r
   return EFI_SUCCESS;\r
 }\r