]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/PiSmmCore/Dispatcher.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / Dispatcher.c
index 8b347784e7915bd5c2f56df4462c0faa40c7d5c3..f635565dd19e33335598296f4f7da6f74ec7186d 100644 (file)
@@ -17,7 +17,7 @@
             and After dependencies. This is done recursively as the call to add\r
             to the mScheduledQueue checks for Before and recursively adds\r
             all Befores. It then addes the item that was passed in and then\r
-            processess the After dependecies by recursively calling the routine.\r
+            processes the After dependencies by recursively calling the routine.\r
 \r
   Dispatcher Rules:\r
   The rules for the dispatcher are similar to the DXE dispatcher.\r
   The rules for DXE dispatcher are in chapter 10 of the DXE CIS. Figure 10-3\r
   is the state diagram for the DXE dispatcher\r
 \r
-  Depex - Dependency Expresion.\r
+  Depex - Dependency Expression.\r
 \r
   Copyright (c) 2014, Hewlett-Packard Development Company, L.P.\r
-  Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r
-  This program and the accompanying materials are licensed and made available \r
-  under the terms and conditions of the BSD License which accompanies this \r
-  distribution.  The full text of the license may be found at        \r
-  http://opensource.org/licenses/bsd-license.php                                            \r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -46,9 +40,9 @@
 //\r
 #define KNOWN_HANDLE_SIGNATURE  SIGNATURE_32('k','n','o','w')\r
 typedef struct {\r
-  UINTN           Signature;\r
-  LIST_ENTRY      Link;         // mFvHandleList\r
-  EFI_HANDLE      Handle;\r
+  UINTN         Signature;\r
+  LIST_ENTRY    Link;           // mFvHandleList\r
+  EFI_HANDLE    Handle;\r
 } KNOWN_HANDLE;\r
 \r
 //\r
@@ -68,7 +62,7 @@ typedef struct {
 **/\r
 VOID\r
 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
-  IN  EFI_SMM_DRIVER_ENTRY   *InsertedDriverEntry\r
+  IN  EFI_SMM_DRIVER_ENTRY  *InsertedDriverEntry\r
   );\r
 \r
 //\r
@@ -89,19 +83,19 @@ LIST_ENTRY  mScheduledQueue = INITIALIZE_LIST_HEAD_VARIABLE (mScheduledQueue);
 LIST_ENTRY  mFvHandleList = INITIALIZE_LIST_HEAD_VARIABLE (mFvHandleList);\r
 \r
 //\r
-// Flag for the SMM Dispacher.  TRUE if dispatcher is execuing.\r
+// Flag for the SMM Dispatcher.  TRUE if dispatcher is executing.\r
 //\r
 BOOLEAN  gDispatcherRunning = FALSE;\r
 \r
 //\r
-// Flag for the SMM Dispacher.  TRUE if there is one or more SMM drivers ready to be dispatched\r
+// Flag for the SMM Dispatcher.  TRUE if there is one or more SMM drivers ready to be dispatched\r
 //\r
 BOOLEAN  gRequestDispatch = FALSE;\r
 \r
 //\r
 // List of file types supported by dispatcher\r
 //\r
-EFI_FV_FILETYPE mSmmFileTypes[] = {\r
+EFI_FV_FILETYPE  mSmmFileTypes[] = {\r
   EFI_FV_FILETYPE_SMM,\r
   EFI_FV_FILETYPE_COMBINED_SMM_DXE,\r
   EFI_FV_FILETYPE_SMM_CORE,\r
@@ -112,8 +106,8 @@ EFI_FV_FILETYPE mSmmFileTypes[] = {
 };\r
 \r
 typedef struct {\r
-  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH  File;\r
-  EFI_DEVICE_PATH_PROTOCOL           End;\r
+  MEDIA_FW_VOL_FILEPATH_DEVICE_PATH    File;\r
+  EFI_DEVICE_PATH_PROTOCOL             End;\r
 } FV_FILEPATH_DEVICE_PATH;\r
 \r
 FV_FILEPATH_DEVICE_PATH  mFvDevicePath;\r
@@ -121,91 +115,97 @@ FV_FILEPATH_DEVICE_PATH  mFvDevicePath;
 //\r
 // DXE Architecture Protocols\r
 //\r
-EFI_SECURITY_ARCH_PROTOCOL  *mSecurity = NULL;\r
-EFI_SECURITY2_ARCH_PROTOCOL *mSecurity2 = NULL;\r
+EFI_SECURITY_ARCH_PROTOCOL   *mSecurity  = NULL;\r
+EFI_SECURITY2_ARCH_PROTOCOL  *mSecurity2 = NULL;\r
 \r
 //\r
 // The global variable is defined for Loading modules at fixed address feature to track the SMM code\r
 // memory range usage. It is a bit mapped array in which every bit indicates the corresponding\r
-// memory page available or not. \r
+// memory page available or not.\r
 //\r
-GLOBAL_REMOVE_IF_UNREFERENCED    UINT64                *mSmmCodeMemoryRangeUsageBitMap=NULL;\r
+GLOBAL_REMOVE_IF_UNREFERENCED    UINT64  *mSmmCodeMemoryRangeUsageBitMap = NULL;\r
 \r
 /**\r
   To check memory usage bit map array to figure out if the memory range in which the image will be loaded is available or not. If\r
   memory range is available, the function will mark the corresponding bits to 1 which indicates the memory range is used.\r
-  The function is only invoked when load modules at fixed address feature is enabled. \r
-  \r
+  The function is only invoked when load modules at fixed address feature is enabled.\r
+\r
   @param  ImageBase                The base address the image will be loaded at.\r
   @param  ImageSize                The size of the image\r
-  \r
+\r
   @retval EFI_SUCCESS              The memory range the image will be loaded in is available\r
   @retval EFI_NOT_FOUND            The memory range the image will be loaded in is not available\r
 **/\r
 EFI_STATUS\r
 CheckAndMarkFixLoadingMemoryUsageBitMap (\r
-  IN  EFI_PHYSICAL_ADDRESS          ImageBase,\r
-  IN  UINTN                         ImageSize\r
+  IN  EFI_PHYSICAL_ADDRESS  ImageBase,\r
+  IN  UINTN                 ImageSize\r
   )\r
 {\r
-   UINT32                             SmmCodePageNumber;\r
-   UINT64                             SmmCodeSize; \r
-   EFI_PHYSICAL_ADDRESS               SmmCodeBase;\r
-   UINTN                              BaseOffsetPageNumber;\r
-   UINTN                              TopOffsetPageNumber;\r
-   UINTN                              Index;\r
-   //\r
-   // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
-   //\r
-   SmmCodePageNumber = PcdGet32(PcdLoadFixAddressSmmCodePageNumber);\r
-   SmmCodeSize = EFI_PAGES_TO_SIZE (SmmCodePageNumber);\r
-   SmmCodeBase = gLoadModuleAtFixAddressSmramBase;\r
-   \r
-   //\r
-   // If the memory usage bit map is not initialized,  do it. Every bit in the array \r
-   // indicate the status of the corresponding memory page, available or not\r
-   // \r
-   if (mSmmCodeMemoryRangeUsageBitMap == NULL) {\r
-     mSmmCodeMemoryRangeUsageBitMap = AllocateZeroPool(((SmmCodePageNumber / 64) + 1)*sizeof(UINT64));\r
-   }\r
-   //\r
-   // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND\r
-   //\r
-   if (mSmmCodeMemoryRangeUsageBitMap == NULL) {\r
-     return EFI_NOT_FOUND;\r
-   }\r
-   //\r
-   // see if the memory range for loading the image is in the SMM code range.\r
-   //\r
-   if (SmmCodeBase + SmmCodeSize <  ImageBase + ImageSize || SmmCodeBase >  ImageBase) {\r
-     return EFI_NOT_FOUND;   \r
-   }   \r
-   //\r
-   // Test if the memory is avalaible or not.\r
-   // \r
-   BaseOffsetPageNumber = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase - SmmCodeBase));\r
-   TopOffsetPageNumber  = (UINTN)EFI_SIZE_TO_PAGES((UINT32)(ImageBase + ImageSize - SmmCodeBase));\r
-   for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
-     if ((mSmmCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64(1, (Index % 64))) != 0) {\r
-       //\r
-       // This page is already used.\r
-       //\r
-       return EFI_NOT_FOUND;  \r
-     }\r
-   }\r
-   \r
-   //\r
-   // Being here means the memory range is available.  So mark the bits for the memory range\r
-   // \r
-   for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index ++) {\r
-     mSmmCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64(1, (Index % 64));\r
-   }\r
-   return  EFI_SUCCESS;   \r
+  UINT32                SmmCodePageNumber;\r
+  UINT64                SmmCodeSize;\r
+  EFI_PHYSICAL_ADDRESS  SmmCodeBase;\r
+  UINTN                 BaseOffsetPageNumber;\r
+  UINTN                 TopOffsetPageNumber;\r
+  UINTN                 Index;\r
+\r
+  //\r
+  // Build tool will calculate the smm code size and then patch the PcdLoadFixAddressSmmCodePageNumber\r
+  //\r
+  SmmCodePageNumber = PcdGet32 (PcdLoadFixAddressSmmCodePageNumber);\r
+  SmmCodeSize       = EFI_PAGES_TO_SIZE (SmmCodePageNumber);\r
+  SmmCodeBase       = gLoadModuleAtFixAddressSmramBase;\r
+\r
+  //\r
+  // If the memory usage bit map is not initialized,  do it. Every bit in the array\r
+  // indicate the status of the corresponding memory page, available or not\r
+  //\r
+  if (mSmmCodeMemoryRangeUsageBitMap == NULL) {\r
+    mSmmCodeMemoryRangeUsageBitMap = AllocateZeroPool (((SmmCodePageNumber / 64) + 1)*sizeof (UINT64));\r
+  }\r
+\r
+  //\r
+  // If the Dxe code memory range is not allocated or the bit map array allocation failed, return EFI_NOT_FOUND\r
+  //\r
+  if (mSmmCodeMemoryRangeUsageBitMap == NULL) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  //\r
+  // see if the memory range for loading the image is in the SMM code range.\r
+  //\r
+  if ((SmmCodeBase + SmmCodeSize <  ImageBase + ImageSize) || (SmmCodeBase >  ImageBase)) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  //\r
+  // Test if the memory is available or not.\r
+  //\r
+  BaseOffsetPageNumber = EFI_SIZE_TO_PAGES ((UINT32)(ImageBase - SmmCodeBase));\r
+  TopOffsetPageNumber  = EFI_SIZE_TO_PAGES ((UINT32)(ImageBase + ImageSize - SmmCodeBase));\r
+  for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index++) {\r
+    if ((mSmmCodeMemoryRangeUsageBitMap[Index / 64] & LShiftU64 (1, (Index % 64))) != 0) {\r
+      //\r
+      // This page is already used.\r
+      //\r
+      return EFI_NOT_FOUND;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Being here means the memory range is available.  So mark the bits for the memory range\r
+  //\r
+  for (Index = BaseOffsetPageNumber; Index < TopOffsetPageNumber; Index++) {\r
+    mSmmCodeMemoryRangeUsageBitMap[Index / 64] |= LShiftU64 (1, (Index % 64));\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
 }\r
+\r
 /**\r
   Get the fixed loading address from image header assigned by build tool. This function only be called\r
   when Loading module at Fixed address feature enabled.\r
-  \r
+\r
   @param  ImageContext              Pointer to the image context structure that describes the PE/COFF\r
                                     image that needs to be examined by this function.\r
   @retval EFI_SUCCESS               An fixed loading address is assigned to this image by build tools .\r
@@ -213,33 +213,31 @@ CheckAndMarkFixLoadingMemoryUsageBitMap (
 \r
 **/\r
 EFI_STATUS\r
-GetPeCoffImageFixLoadingAssignedAddress(\r
+GetPeCoffImageFixLoadingAssignedAddress (\r
   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext\r
   )\r
 {\r
-  UINTN                              SectionHeaderOffset;\r
-  EFI_STATUS                         Status;\r
-  EFI_IMAGE_SECTION_HEADER           SectionHeader;\r
-  EFI_IMAGE_OPTIONAL_HEADER_UNION    *ImgHdr;\r
-  EFI_PHYSICAL_ADDRESS               FixLoadingAddress;\r
-  UINT16                             Index;\r
-  UINTN                              Size;\r
-  UINT16                             NumberOfSections;\r
-  UINT64                             ValueInSectionHeader;\r
+  UINTN                            SectionHeaderOffset;\r
+  EFI_STATUS                       Status;\r
+  EFI_IMAGE_SECTION_HEADER         SectionHeader;\r
+  EFI_IMAGE_OPTIONAL_HEADER_UNION  *ImgHdr;\r
+  EFI_PHYSICAL_ADDRESS             FixLoadingAddress;\r
+  UINT16                           Index;\r
+  UINTN                            Size;\r
+  UINT16                           NumberOfSections;\r
+  UINT64                           ValueInSectionHeader;\r
 \r
   FixLoadingAddress = 0;\r
-  Status = EFI_NOT_FOUND;\r
+  Status            = EFI_NOT_FOUND;\r
 \r
   //\r
   // Get PeHeader pointer\r
   //\r
-  ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8* )ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r
-  SectionHeaderOffset = (UINTN)(\r
-                                 ImageContext->PeCoffHeaderOffset +\r
-                                 sizeof (UINT32) +\r
-                                 sizeof (EFI_IMAGE_FILE_HEADER) +\r
-                                 ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader\r
-                                 );\r
+  ImgHdr              = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((CHAR8 *)ImageContext->Handle + ImageContext->PeCoffHeaderOffset);\r
+  SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +\r
+                        sizeof (UINT32) +\r
+                        sizeof (EFI_IMAGE_FILE_HEADER) +\r
+                        ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader;\r
   NumberOfSections = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
 \r
   //\r
@@ -249,13 +247,13 @@ GetPeCoffImageFixLoadingAssignedAddress(
     //\r
     // Read section header from file\r
     //\r
-    Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
+    Size   = sizeof (EFI_IMAGE_SECTION_HEADER);\r
     Status = ImageContext->ImageRead (\r
-                              ImageContext->Handle,\r
-                              SectionHeaderOffset,\r
-                              &Size,\r
-                              &SectionHeader\r
-                              );\r
+                             ImageContext->Handle,\r
+                             SectionHeaderOffset,\r
+                             &Size,\r
+                             &SectionHeader\r
+                             );\r
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
@@ -269,7 +267,7 @@ GetPeCoffImageFixLoadingAssignedAddress(
       // if a module with a loading address assigned by tools, the PointerToRelocations & PointerToLineNumbers fields\r
       // should not be Zero, or else, these 2 fields should be set to Zero\r
       //\r
-      ValueInSectionHeader = ReadUnaligned64((UINT64*)&SectionHeader.PointerToRelocations);\r
+      ValueInSectionHeader = ReadUnaligned64 ((UINT64 *)&SectionHeader.PointerToRelocations);\r
       if (ValueInSectionHeader != 0) {\r
         //\r
         // Found first section header that doesn't point to code section in which build tool saves the\r
@@ -280,20 +278,24 @@ GetPeCoffImageFixLoadingAssignedAddress(
         // Check if the memory range is available.\r
         //\r
         Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, (UINTN)(ImageContext->ImageSize + ImageContext->SectionAlignment));\r
-        if (!EFI_ERROR(Status)) {\r
+        if (!EFI_ERROR (Status)) {\r
           //\r
           // The assigned address is valid. Return the specified loading address\r
           //\r
           ImageContext->ImageAddress = FixLoadingAddress;\r
         }\r
       }\r
+\r
       break;\r
     }\r
+\r
     SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
   }\r
-  DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoadingAddress, Status));\r
+\r
+  DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoadingAddress, Status));\r
   return Status;\r
 }\r
+\r
 /**\r
   Loads an EFI image into SMRAM.\r
 \r
@@ -323,18 +325,14 @@ SmmLoadImage (
   EFI_DEVICE_PATH_PROTOCOL       *HandleFilePath;\r
   EFI_FIRMWARE_VOLUME2_PROTOCOL  *Fv;\r
   PE_COFF_LOADER_IMAGE_CONTEXT   ImageContext;\r
-  UINT64                         Tick;\r
 \r
-  Tick = 0;\r
-  PERF_CODE (\r
-    Tick = GetPerformanceCounter ();\r
-  );\r
-   \r
-  Buffer               = NULL;\r
-  Size                 = 0;\r
-  Fv                   = DriverEntry->Fv;\r
-  NameGuid             = &DriverEntry->FileName;\r
-  FilePath             = DriverEntry->FvFileDevicePath;\r
+  PERF_LOAD_IMAGE_BEGIN (DriverEntry->ImageHandle);\r
+\r
+  Buffer   = NULL;\r
+  Size     = 0;\r
+  Fv       = DriverEntry->Fv;\r
+  NameGuid = &DriverEntry->FileName;\r
+  FilePath = DriverEntry->FvFileDevicePath;\r
 \r
   OriginalFilePath     = FilePath;\r
   HandleFilePath       = FilePath;\r
@@ -347,7 +345,7 @@ SmmLoadImage (
   // Try to get the image device handle by checking the match protocol.\r
   //\r
   Status = gBS->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &HandleFilePath, &DeviceHandle);\r
-  if (EFI_ERROR(Status)) {\r
+  if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
@@ -355,11 +353,13 @@ SmmLoadImage (
   // If the Security2 and Security Architectural Protocol has not been located yet, then attempt to locate it\r
   //\r
   if (mSecurity2 == NULL) {\r
-    gBS->LocateProtocol (&gEfiSecurity2ArchProtocolGuid, NULL, (VOID**)&mSecurity2);\r
+    gBS->LocateProtocol (&gEfiSecurity2ArchProtocolGuid, NULL, (VOID **)&mSecurity2);\r
   }\r
+\r
   if (mSecurity == NULL) {\r
-    gBS->LocateProtocol (&gEfiSecurityArchProtocolGuid, NULL, (VOID**)&mSecurity);\r
+    gBS->LocateProtocol (&gEfiSecurityArchProtocolGuid, NULL, (VOID **)&mSecurity);\r
   }\r
+\r
   //\r
   // When Security2 is installed, Security Architectural Protocol must be published.\r
   //\r
@@ -369,10 +369,10 @@ SmmLoadImage (
   // Pull out just the file portion of the DevicePath for the LoadedImage FilePath\r
   //\r
   FilePath = OriginalFilePath;\r
-  Status = gBS->HandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);\r
+  Status   = gBS->HandleProtocol (DeviceHandle, &gEfiDevicePathProtocolGuid, (VOID **)&HandleFilePath);\r
   if (!EFI_ERROR (Status)) {\r
-    FilePathSize = GetDevicePathSize (HandleFilePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);\r
-    FilePath = (EFI_DEVICE_PATH_PROTOCOL *) (((UINT8 *)FilePath) + FilePathSize );\r
+    FilePathSize = GetDevicePathSize (HandleFilePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL);\r
+    FilePath     = (EFI_DEVICE_PATH_PROTOCOL *)(((UINT8 *)FilePath) + FilePathSize);\r
   }\r
 \r
   //\r
@@ -395,20 +395,21 @@ SmmLoadImage (
     Buffer = NULL;\r
     Size   = 0;\r
     Status = Fv->ReadSection (\r
-                  Fv,\r
-                  NameGuid,\r
-                  EFI_SECTION_TE,\r
-                  0,\r
-                  &Buffer,\r
-                  &Size,\r
-                  &AuthenticationStatus\r
-                  );\r
+                   Fv,\r
+                   NameGuid,\r
+                   EFI_SECTION_TE,\r
+                   0,\r
+                   &Buffer,\r
+                   &Size,\r
+                   &AuthenticationStatus\r
+                   );\r
   }\r
-  \r
+\r
   if (EFI_ERROR (Status)) {\r
     if (Buffer != NULL) {\r
       gBS->FreePool (Buffer);\r
     }\r
+\r
     return Status;\r
   }\r
 \r
@@ -417,18 +418,18 @@ SmmLoadImage (
   //\r
   if (mSecurity2 != NULL) {\r
     SecurityStatus = mSecurity2->FileAuthentication (\r
-                                  mSecurity2,\r
-                                  OriginalFilePath,\r
-                                  Buffer,\r
-                                  Size,\r
-                                  FALSE\r
-                                  );\r
+                                   mSecurity2,\r
+                                   OriginalFilePath,\r
+                                   Buffer,\r
+                                   Size,\r
+                                   FALSE\r
+                                   );\r
   }\r
 \r
   //\r
   // Verify the Authentication Status through the Security Architectural Protocol\r
   // Only on images that have been read using Firmware Volume protocol.\r
-  // All SMM images are from FV protocol. \r
+  // All SMM images are from FV protocol.\r
   //\r
   if (!EFI_ERROR (SecurityStatus) && (mSecurity != NULL)) {\r
     SecurityStatus = mSecurity->FileAuthenticationState (\r
@@ -438,15 +439,15 @@ SmmLoadImage (
                                   );\r
   }\r
 \r
-  if (EFI_ERROR (SecurityStatus) && SecurityStatus != EFI_SECURITY_VIOLATION) {\r
+  if (EFI_ERROR (SecurityStatus) && (SecurityStatus != EFI_SECURITY_VIOLATION)) {\r
     Status = SecurityStatus;\r
     return Status;\r
   }\r
-  \r
+\r
   //\r
   // Initialize ImageContext\r
   //\r
-  ImageContext.Handle = Buffer;\r
+  ImageContext.Handle    = Buffer;\r
   ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;\r
 \r
   //\r
@@ -457,70 +458,76 @@ SmmLoadImage (
     if (Buffer != NULL) {\r
       gBS->FreePool (Buffer);\r
     }\r
+\r
     return Status;\r
   }\r
+\r
   //\r
   // if Loading module at Fixed Address feature is enabled, then  cut out a memory range started from TESG BASE\r
   // to hold the Smm driver code\r
   //\r
-  if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
+  if (PcdGet64 (PcdLoadModuleAtFixAddressEnable) != 0) {\r
     //\r
     // Get the fixed loading address assigned by Build tool\r
     //\r
     Status = GetPeCoffImageFixLoadingAssignedAddress (&ImageContext);\r
     if (!EFI_ERROR (Status)) {\r
       //\r
-      // Since the memory range to load Smm core alreay been cut out, so no need to allocate and free this range\r
+      // Since the memory range to load Smm core already been cut out, so no need to allocate and free this range\r
       // following statements is to bypass SmmFreePages\r
       //\r
       PageCount = 0;\r
-      DstBuffer = (UINTN)gLoadModuleAtFixAddressSmramBase;   \r
+      DstBuffer = (UINTN)gLoadModuleAtFixAddressSmramBase;\r
     } else {\r
-       DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));\r
-       //\r
-       // allocate the memory to load the SMM driver\r
-       //\r
-       PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
-       DstBuffer = (UINTN)(-1);\r
-     \r
-       Status = SmmAllocatePages (\r
-                   AllocateMaxAddress,\r
-                   EfiRuntimeServicesCode,\r
-                   PageCount,\r
-                   &DstBuffer\r
-                   );\r
-       if (EFI_ERROR (Status)) {\r
-         if (Buffer != NULL) {\r
-           gBS->FreePool (Buffer);\r
-         } \r
-         return Status;\r
-       }     \r
+      DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n"));\r
+      //\r
+      // allocate the memory to load the SMM driver\r
+      //\r
+      PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
+      DstBuffer = (UINTN)(-1);\r
+\r
+      Status = SmmAllocatePages (\r
+                 AllocateMaxAddress,\r
+                 EfiRuntimeServicesCode,\r
+                 PageCount,\r
+                 &DstBuffer\r
+                 );\r
+      if (EFI_ERROR (Status)) {\r
+        if (Buffer != NULL) {\r
+          gBS->FreePool (Buffer);\r
+        }\r
+\r
+        return Status;\r
+      }\r
+\r
       ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;\r
     }\r
   } else {\r
-     PageCount = (UINTN)EFI_SIZE_TO_PAGES((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
-     DstBuffer = (UINTN)(-1);\r
-     \r
-     Status = SmmAllocatePages (\r
-                  AllocateMaxAddress,\r
-                  EfiRuntimeServicesCode,\r
-                  PageCount,\r
-                  &DstBuffer\r
-                  );\r
-     if (EFI_ERROR (Status)) {\r
-       if (Buffer != NULL) {\r
-         gBS->FreePool (Buffer);\r
-       }\r
-       return Status;\r
-     }\r
-     \r
-     ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;\r
+    PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);\r
+    DstBuffer = (UINTN)(-1);\r
+\r
+    Status = SmmAllocatePages (\r
+               AllocateMaxAddress,\r
+               EfiRuntimeServicesCode,\r
+               PageCount,\r
+               &DstBuffer\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      if (Buffer != NULL) {\r
+        gBS->FreePool (Buffer);\r
+      }\r
+\r
+      return Status;\r
+    }\r
+\r
+    ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)DstBuffer;\r
   }\r
+\r
   //\r
-  // Align buffer on section boundry\r
+  // Align buffer on section boundary\r
   //\r
   ImageContext.ImageAddress += ImageContext.SectionAlignment - 1;\r
-  ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)(ImageContext.SectionAlignment - 1));\r
+  ImageContext.ImageAddress &= ~((EFI_PHYSICAL_ADDRESS)ImageContext.SectionAlignment - 1);\r
 \r
   //\r
   // Load the image to our new buffer\r
@@ -530,6 +537,7 @@ SmmLoadImage (
     if (Buffer != NULL) {\r
       gBS->FreePool (Buffer);\r
     }\r
+\r
     SmmFreePages (DstBuffer, PageCount);\r
     return Status;\r
   }\r
@@ -542,6 +550,7 @@ SmmLoadImage (
     if (Buffer != NULL) {\r
       gBS->FreePool (Buffer);\r
     }\r
+\r
     SmmFreePages (DstBuffer, PageCount);\r
     return Status;\r
   }\r
@@ -549,14 +558,14 @@ SmmLoadImage (
   //\r
   // Flush the instruction cache so the image data are written before we execute it\r
   //\r
-  InvalidateInstructionCacheRange ((VOID *)(UINTN) ImageContext.ImageAddress, (UINTN) ImageContext.ImageSize);\r
+  InvalidateInstructionCacheRange ((VOID *)(UINTN)ImageContext.ImageAddress, (UINTN)ImageContext.ImageSize);\r
 \r
   //\r
   // Save Image EntryPoint in DriverEntry\r
   //\r
-  DriverEntry->ImageEntryPoint  = ImageContext.EntryPoint;\r
-  DriverEntry->ImageBuffer      = DstBuffer; \r
-  DriverEntry->NumberOfPage     = PageCount;\r
+  DriverEntry->ImageEntryPoint = ImageContext.EntryPoint;\r
+  DriverEntry->ImageBuffer     = DstBuffer;\r
+  DriverEntry->NumberOfPage    = PageCount;\r
 \r
   //\r
   // Allocate a Loaded Image Protocol in EfiBootServicesData\r
@@ -566,6 +575,7 @@ SmmLoadImage (
     if (Buffer != NULL) {\r
       gBS->FreePool (Buffer);\r
     }\r
+\r
     SmmFreePages (DstBuffer, PageCount);\r
     return Status;\r
   }\r
@@ -575,10 +585,15 @@ SmmLoadImage (
   // Fill in the remaining fields of the Loaded Image Protocol instance.\r
   // Note: ImageBase is an SMRAM address that can not be accessed outside of SMRAM if SMRAM window is closed.\r
   //\r
-  DriverEntry->LoadedImage->Revision      = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
-  DriverEntry->LoadedImage->ParentHandle  = gSmmCorePrivate->SmmIplImageHandle;\r
-  DriverEntry->LoadedImage->SystemTable   = gST;\r
-  DriverEntry->LoadedImage->DeviceHandle  = DeviceHandle;\r
+  DriverEntry->LoadedImage->Revision     = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
+  DriverEntry->LoadedImage->ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
+  DriverEntry->LoadedImage->SystemTable  = gST;\r
+  DriverEntry->LoadedImage->DeviceHandle = DeviceHandle;\r
+\r
+  DriverEntry->SmmLoadedImage.Revision     = EFI_LOADED_IMAGE_PROTOCOL_REVISION;\r
+  DriverEntry->SmmLoadedImage.ParentHandle = gSmmCorePrivate->SmmIplImageHandle;\r
+  DriverEntry->SmmLoadedImage.SystemTable  = gST;\r
+  DriverEntry->SmmLoadedImage.DeviceHandle = DeviceHandle;\r
 \r
   //\r
   // Make an EfiBootServicesData buffer copy of FilePath\r
@@ -588,28 +603,62 @@ SmmLoadImage (
     if (Buffer != NULL) {\r
       gBS->FreePool (Buffer);\r
     }\r
+\r
     SmmFreePages (DstBuffer, PageCount);\r
     return Status;\r
   }\r
+\r
   CopyMem (DriverEntry->LoadedImage->FilePath, FilePath, GetDevicePathSize (FilePath));\r
 \r
-  DriverEntry->LoadedImage->ImageBase     = (VOID *)(UINTN)DriverEntry->ImageBuffer;\r
+  DriverEntry->LoadedImage->ImageBase     = (VOID *)(UINTN)ImageContext.ImageAddress;\r
   DriverEntry->LoadedImage->ImageSize     = ImageContext.ImageSize;\r
   DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;\r
   DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;\r
 \r
+  //\r
+  // Make a buffer copy of FilePath\r
+  //\r
+  Status = SmmAllocatePool (EfiRuntimeServicesData, GetDevicePathSize (FilePath), (VOID **)&DriverEntry->SmmLoadedImage.FilePath);\r
+  if (EFI_ERROR (Status)) {\r
+    if (Buffer != NULL) {\r
+      gBS->FreePool (Buffer);\r
+    }\r
+\r
+    gBS->FreePool (DriverEntry->LoadedImage->FilePath);\r
+    SmmFreePages (DstBuffer, PageCount);\r
+    return Status;\r
+  }\r
+\r
+  CopyMem (DriverEntry->SmmLoadedImage.FilePath, FilePath, GetDevicePathSize (FilePath));\r
+\r
+  DriverEntry->SmmLoadedImage.ImageBase     = (VOID *)(UINTN)ImageContext.ImageAddress;\r
+  DriverEntry->SmmLoadedImage.ImageSize     = ImageContext.ImageSize;\r
+  DriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode;\r
+  DriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData;\r
+\r
   //\r
   // Create a new image handle in the UEFI handle database for the SMM Driver\r
   //\r
   DriverEntry->ImageHandle = NULL;\r
-  Status = gBS->InstallMultipleProtocolInterfaces (\r
-                  &DriverEntry->ImageHandle,\r
-                  &gEfiLoadedImageProtocolGuid, DriverEntry->LoadedImage,\r
-                  NULL\r
-                  );\r
+  Status                   = gBS->InstallMultipleProtocolInterfaces (\r
+                                    &DriverEntry->ImageHandle,\r
+                                    &gEfiLoadedImageProtocolGuid,\r
+                                    DriverEntry->LoadedImage,\r
+                                    NULL\r
+                                    );\r
+\r
+  //\r
+  // Create a new image handle in the SMM handle database for the SMM Driver\r
+  //\r
+  DriverEntry->SmmImageHandle = NULL;\r
+  Status                      = SmmInstallProtocolInterface (\r
+                                  &DriverEntry->SmmImageHandle,\r
+                                  &gEfiLoadedImageProtocolGuid,\r
+                                  EFI_NATIVE_INTERFACE,\r
+                                  &DriverEntry->SmmLoadedImage\r
+                                  );\r
 \r
-  PERF_START (DriverEntry->ImageHandle, "LoadImage:", NULL, Tick);\r
-  PERF_END (DriverEntry->ImageHandle, "LoadImage:", NULL, 0);\r
+  PERF_LOAD_IMAGE_END (DriverEntry->ImageHandle);\r
 \r
   //\r
   // Print the load address and the PDB file name if it is available\r
@@ -617,73 +666,78 @@ SmmLoadImage (
 \r
   DEBUG_CODE_BEGIN ();\r
 \r
-    UINTN Index;\r
-    UINTN StartIndex;\r
-    CHAR8 EfiFileName[256];\r
-\r
+  UINTN  Index;\r
+  UINTN  StartIndex;\r
+  CHAR8  EfiFileName[256];\r
 \r
-    DEBUG ((DEBUG_INFO | DEBUG_LOAD,\r
-           "Loading SMM driver at 0x%11p EntryPoint=0x%11p ",\r
-           (VOID *)(UINTN) ImageContext.ImageAddress,\r
-           FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)));\r
+  DEBUG ((\r
+    DEBUG_INFO | DEBUG_LOAD,\r
+    "Loading SMM driver at 0x%11p EntryPoint=0x%11p ",\r
+    (VOID *)(UINTN)ImageContext.ImageAddress,\r
+    FUNCTION_ENTRY_POINT (ImageContext.EntryPoint)\r
+    ));\r
 \r
+  //\r
+  // Print Module Name by Pdb file path.\r
+  // Windows and Unix style file path are all trimmed correctly.\r
+  //\r
+  if (ImageContext.PdbPointer != NULL) {\r
+    StartIndex = 0;\r
+    for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {\r
+      if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {\r
+        StartIndex = Index + 1;\r
+      }\r
+    }\r
 \r
     //\r
-    // Print Module Name by Pdb file path.\r
-    // Windows and Unix style file path are all trimmed correctly.\r
+    // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r
+    // The PDB file name is limited in the range of 0~255.\r
+    // If the length is bigger than 255, trim the redundant characters to avoid overflow in array boundary.\r
     //\r
-    if (ImageContext.PdbPointer != NULL) {\r
-      StartIndex = 0;\r
-      for (Index = 0; ImageContext.PdbPointer[Index] != 0; Index++) {\r
-        if ((ImageContext.PdbPointer[Index] == '\\') || (ImageContext.PdbPointer[Index] == '/')) {\r
-          StartIndex = Index + 1;\r
-        }\r
-      }\r
-      //\r
-      // Copy the PDB file name to our temporary string, and replace .pdb with .efi\r
-      // The PDB file name is limited in the range of 0~255.\r
-      // If the length is bigger than 255, trim the redudant characters to avoid overflow in array boundary.\r
-      //\r
-      for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
-        EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];\r
-        if (EfiFileName[Index] == 0) {\r
-          EfiFileName[Index] = '.';\r
-        }\r
-        if (EfiFileName[Index] == '.') {\r
-          EfiFileName[Index + 1] = 'e';\r
-          EfiFileName[Index + 2] = 'f';\r
-          EfiFileName[Index + 3] = 'i';\r
-          EfiFileName[Index + 4] = 0;\r
-          break;\r
-        }\r
+    for (Index = 0; Index < sizeof (EfiFileName) - 4; Index++) {\r
+      EfiFileName[Index] = ImageContext.PdbPointer[Index + StartIndex];\r
+      if (EfiFileName[Index] == 0) {\r
+        EfiFileName[Index] = '.';\r
       }\r
 \r
-      if (Index == sizeof (EfiFileName) - 4) {\r
-        EfiFileName[Index] = 0;\r
+      if (EfiFileName[Index] == '.') {\r
+        EfiFileName[Index + 1] = 'e';\r
+        EfiFileName[Index + 2] = 'f';\r
+        EfiFileName[Index + 3] = 'i';\r
+        EfiFileName[Index + 4] = 0;\r
+        break;\r
       }\r
-      DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName)); // &Image->ImageContext.PdbPointer[StartIndex]));\r
     }\r
-    DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));\r
+\r
+    if (Index == sizeof (EfiFileName) - 4) {\r
+      EfiFileName[Index] = 0;\r
+    }\r
+\r
+    DEBUG ((DEBUG_INFO | DEBUG_LOAD, "%a", EfiFileName));   // &Image->ImageContext.PdbPointer[StartIndex]));\r
+  }\r
+\r
+  DEBUG ((DEBUG_INFO | DEBUG_LOAD, "\n"));\r
 \r
   DEBUG_CODE_END ();\r
 \r
   //\r
   // Free buffer allocated by Fv->ReadSection.\r
   //\r
-  // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection \r
+  // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection\r
   // used the UEFI Boot Services AllocatePool() function\r
   //\r
-  Status = gBS->FreePool(Buffer);\r
+  Status = gBS->FreePool (Buffer);\r
   if (!EFI_ERROR (Status) && EFI_ERROR (SecurityStatus)) {\r
     Status = SecurityStatus;\r
   }\r
-  return Status;  \r
+\r
+  return Status;\r
 }\r
 \r
 /**\r
   Preprocess dependency expression and update DriverEntry to reflect the\r
   state of  Before and After dependencies. If DriverEntry->Before\r
-  or DriverEntry->After is set it will never be cleared. \r
+  or DriverEntry->After is set it will never be cleared.\r
 \r
   @param  DriverEntry           DriverEntry element to update .\r
 \r
@@ -697,7 +751,7 @@ SmmPreProcessDepex (
 {\r
   UINT8  *Iterator;\r
 \r
-  Iterator = DriverEntry->Depex;\r
+  Iterator               = DriverEntry->Depex;\r
   DriverEntry->Dependent = TRUE;\r
 \r
   if (*Iterator == EFI_DEP_BEFORE) {\r
@@ -719,7 +773,7 @@ SmmPreProcessDepex (
 \r
   @param  DriverEntry           Driver to work on.\r
 \r
-  @retval EFI_SUCCESS           Depex read and preprossesed\r
+  @retval EFI_SUCCESS           Depex read and preprocessed\r
   @retval EFI_PROTOCOL_ERROR    The section extraction protocol returned an error\r
                                 and  Depex reading needs to be retried.\r
   @retval Error                 DEPEX not found.\r
@@ -741,16 +795,16 @@ SmmGetDepexSectionAndPreProccess (
   // Grab Depex info, it will never be free'ed.\r
   // (Note: DriverEntry->Depex is in DXE memory)\r
   //\r
-  SectionType         = EFI_SECTION_SMM_DEPEX;\r
-  Status = Fv->ReadSection (\r
-                DriverEntry->Fv,\r
-                &DriverEntry->FileName,\r
-                SectionType,\r
-                0,\r
-                &DriverEntry->Depex,\r
-                (UINTN *)&DriverEntry->DepexSize,\r
-                &AuthenticationStatus\r
-                );\r
+  SectionType = EFI_SECTION_SMM_DEPEX;\r
+  Status      = Fv->ReadSection (\r
+                      DriverEntry->Fv,\r
+                      &DriverEntry->FileName,\r
+                      SectionType,\r
+                      0,\r
+                      &DriverEntry->Depex,\r
+                      (UINTN *)&DriverEntry->DepexSize,\r
+                      &AuthenticationStatus\r
+                      );\r
   if (EFI_ERROR (Status)) {\r
     if (Status == EFI_PROTOCOL_ERROR) {\r
       //\r
@@ -761,8 +815,8 @@ SmmGetDepexSectionAndPreProccess (
       //\r
       // If no Depex assume depend on all architectural protocols\r
       //\r
-      DriverEntry->Depex = NULL;\r
-      DriverEntry->Dependent = TRUE;\r
+      DriverEntry->Depex              = NULL;\r
+      DriverEntry->Dependent          = TRUE;\r
       DriverEntry->DepexProtocolError = FALSE;\r
     }\r
   } else {\r
@@ -782,7 +836,7 @@ SmmGetDepexSectionAndPreProccess (
   drivers to run. Drain the mScheduledQueue and load and start a PE\r
   image for each driver. Search the mDiscoveredList to see if any driver can\r
   be placed on the mScheduledQueue. If no drivers are placed on the\r
-  mScheduledQueue exit the function. \r
+  mScheduledQueue exit the function.\r
 \r
   @retval EFI_SUCCESS           All of the SMM Drivers that could be dispatched\r
                                 have been run and the SMM Entry Point has been\r
@@ -845,8 +899,8 @@ SmmDispatcher (
           // The SMM Driver could not be loaded, and do not attempt to load or start it again.\r
           // Take driver from Scheduled to Initialized.\r
           //\r
-          DriverEntry->Initialized  = TRUE;\r
-          DriverEntry->Scheduled = FALSE;\r
+          DriverEntry->Initialized = TRUE;\r
+          DriverEntry->Scheduled   = FALSE;\r
           RemoveEntryList (&DriverEntry->ScheduledLink);\r
 \r
           //\r
@@ -856,8 +910,8 @@ SmmDispatcher (
         }\r
       }\r
 \r
-      DriverEntry->Scheduled    = FALSE;\r
-      DriverEntry->Initialized  = TRUE;\r
+      DriverEntry->Scheduled   = FALSE;\r
+      DriverEntry->Initialized = TRUE;\r
       RemoveEntryList (&DriverEntry->ScheduledLink);\r
 \r
       REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
@@ -876,12 +930,18 @@ SmmDispatcher (
       // For each SMM driver, pass NULL as ImageHandle\r
       //\r
       RegisterSmramProfileImage (DriverEntry, TRUE);\r
-      PERF_START (DriverEntry->ImageHandle, "StartImage:", NULL, 0);\r
+      PERF_START_IMAGE_BEGIN (DriverEntry->ImageHandle);\r
       Status = ((EFI_IMAGE_ENTRY_POINT)(UINTN)DriverEntry->ImageEntryPoint)(DriverEntry->ImageHandle, gST);\r
-      PERF_END (DriverEntry->ImageHandle, "StartImage:", NULL, 0);\r
-      if (EFI_ERROR(Status)){\r
+      PERF_START_IMAGE_END (DriverEntry->ImageHandle);\r
+      if (EFI_ERROR (Status)) {\r
+        DEBUG ((\r
+          DEBUG_ERROR,\r
+          "Error: SMM image at %11p start failed: %r\n",\r
+          DriverEntry->SmmLoadedImage.ImageBase,\r
+          Status\r
+          ));\r
         UnregisterSmramProfileImage (DriverEntry, TRUE);\r
-        SmmFreePages(DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);\r
+        SmmFreePages (DriverEntry->ImageBuffer, DriverEntry->NumberOfPage);\r
         //\r
         // Uninstall LoadedImage\r
         //\r
@@ -894,8 +954,20 @@ SmmDispatcher (
           if (DriverEntry->LoadedImage->FilePath != NULL) {\r
             gBS->FreePool (DriverEntry->LoadedImage->FilePath);\r
           }\r
+\r
           gBS->FreePool (DriverEntry->LoadedImage);\r
         }\r
+\r
+        Status = SmmUninstallProtocolInterface (\r
+                   DriverEntry->SmmImageHandle,\r
+                   &gEfiLoadedImageProtocolGuid,\r
+                   &DriverEntry->SmmLoadedImage\r
+                   );\r
+        if (!EFI_ERROR (Status)) {\r
+          if (DriverEntry->SmmLoadedImage.FilePath != NULL) {\r
+            SmmFreePool (DriverEntry->SmmLoadedImage.FilePath);\r
+          }\r
+        }\r
       }\r
 \r
       REPORT_STATUS_CODE_WITH_EXTENDED_DATA (\r
@@ -907,14 +979,14 @@ SmmDispatcher (
 \r
       if (!PreviousSmmEntryPointRegistered && gSmmCorePrivate->SmmEntryPointRegistered) {\r
         //\r
-        // Return immediately if the SMM Entry Point was registered by the SMM \r
+        // Return immediately if the SMM Entry Point was registered by the SMM\r
         // Driver that was just dispatched.  The SMM IPL will reinvoke the SMM\r
-        // Core Dispatcher.  This is required so SMM Mode may be enabled as soon \r
-        // as all the dependent SMM Drivers for SMM Mode have been dispatched.  \r
-        // Once the SMM Entry Point has been registered, then SMM Mode will be \r
+        // Core Dispatcher.  This is required so SMM Mode may be enabled as soon\r
+        // as all the dependent SMM Drivers for SMM Mode have been dispatched.\r
+        // Once the SMM Entry Point has been registered, then SMM Mode will be\r
         // used.\r
         //\r
-        gRequestDispatch = TRUE;\r
+        gRequestDispatch   = TRUE;\r
         gDispatcherRunning = FALSE;\r
         return EFI_NOT_READY;\r
       }\r
@@ -927,7 +999,7 @@ SmmDispatcher (
     for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
       DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
 \r
-      if (DriverEntry->DepexProtocolError){\r
+      if (DriverEntry->DepexProtocolError) {\r
         //\r
         // If Section Extraction Protocol did not let the Depex be read before retry the read\r
         //\r
@@ -950,7 +1022,7 @@ SmmDispatcher (
   for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
     DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
 \r
-    if (!DriverEntry->Initialized){\r
+    if (!DriverEntry->Initialized) {\r
       //\r
       // We have SMM driver pending to dispatch\r
       //\r
@@ -977,18 +1049,18 @@ SmmDispatcher (
 **/\r
 VOID\r
 SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (\r
-  IN  EFI_SMM_DRIVER_ENTRY   *InsertedDriverEntry\r
+  IN  EFI_SMM_DRIVER_ENTRY  *InsertedDriverEntry\r
   )\r
 {\r
   LIST_ENTRY            *Link;\r
-  EFI_SMM_DRIVER_ENTRY *DriverEntry;\r
+  EFI_SMM_DRIVER_ENTRY  *DriverEntry;\r
 \r
   //\r
   // Process Before Dependency\r
   //\r
   for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
-    DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
-    if (DriverEntry->Before && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {\r
+    DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
+    if (DriverEntry->Before && DriverEntry->Dependent && (DriverEntry != InsertedDriverEntry)) {\r
       DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
       DEBUG ((DEBUG_DISPATCH, "  BEFORE FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
       if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
@@ -1011,13 +1083,12 @@ SmmInsertOnScheduledQueueWhileProcessingBeforeAndAfter (
   InsertedDriverEntry->Scheduled = TRUE;\r
   InsertTailList (&mScheduledQueue, &InsertedDriverEntry->ScheduledLink);\r
 \r
-\r
   //\r
   // Process After Dependency\r
   //\r
   for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
-    DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
-    if (DriverEntry->After && DriverEntry->Dependent && DriverEntry != InsertedDriverEntry) {\r
+    DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
+    if (DriverEntry->After && DriverEntry->Dependent && (DriverEntry != InsertedDriverEntry)) {\r
       DEBUG ((DEBUG_DISPATCH, "Evaluate SMM DEPEX for FFS(%g)\n", &DriverEntry->FileName));\r
       DEBUG ((DEBUG_DISPATCH, "  AFTER FFS(%g) = ", &DriverEntry->BeforeAfterGuid));\r
       if (CompareGuid (&InsertedDriverEntry->FileName, &DriverEntry->BeforeAfterGuid)) {\r
@@ -1052,24 +1123,25 @@ FvHasBeenProcessed (
   KNOWN_HANDLE  *KnownHandle;\r
 \r
   for (Link = mFvHandleList.ForwardLink; Link != &mFvHandleList; Link = Link->ForwardLink) {\r
-    KnownHandle = CR(Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);\r
+    KnownHandle = CR (Link, KNOWN_HANDLE, Link, KNOWN_HANDLE_SIGNATURE);\r
     if (KnownHandle->Handle == FvHandle) {\r
       return TRUE;\r
     }\r
   }\r
+\r
   return FALSE;\r
 }\r
 \r
 /**\r
-  Remember that Fv protocol on FvHandle has had it's drivers placed on the\r
-  mDiscoveredList. This fucntion adds entries on the mFvHandleList. Items are\r
+  Remember that Fv protocol on FvHandle has had its drivers placed on the\r
+  mDiscoveredList. This function adds entries on the mFvHandleList. Items are\r
   never removed/freed from the mFvHandleList.\r
 \r
   @param  FvHandle              The handle of a FV that has been processed\r
 \r
 **/\r
 VOID\r
-FvIsBeingProcesssed (\r
+FvIsBeingProcessed (\r
   IN EFI_HANDLE  FvHandle\r
   )\r
 {\r
@@ -1079,7 +1151,7 @@ FvIsBeingProcesssed (
   ASSERT (KnownHandle != NULL);\r
 \r
   KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;\r
-  KnownHandle->Handle = FvHandle;\r
+  KnownHandle->Handle    = FvHandle;\r
   InsertTailList (&mFvHandleList, &KnownHandle->Link);\r
 }\r
 \r
@@ -1098,14 +1170,14 @@ FvIsBeingProcesssed (
 **/\r
 EFI_DEVICE_PATH_PROTOCOL *\r
 SmmFvToDevicePath (\r
-  IN  EFI_FIRMWARE_VOLUME2_PROTOCOL   *Fv,\r
-  IN  EFI_HANDLE                      FvHandle,\r
-  IN  EFI_GUID                        *DriverName\r
+  IN  EFI_FIRMWARE_VOLUME2_PROTOCOL  *Fv,\r
+  IN  EFI_HANDLE                     FvHandle,\r
+  IN  EFI_GUID                       *DriverName\r
   )\r
 {\r
-  EFI_STATUS                          Status;\r
-  EFI_DEVICE_PATH_PROTOCOL            *FvDevicePath;\r
-  EFI_DEVICE_PATH_PROTOCOL            *FileNameDevicePath;\r
+  EFI_STATUS                Status;\r
+  EFI_DEVICE_PATH_PROTOCOL  *FvDevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL  *FileNameDevicePath;\r
 \r
   //\r
   // Remember the device path of the FV\r
@@ -1124,16 +1196,17 @@ SmmFvToDevicePath (
     // Note: FileNameDevicePath is in DXE memory\r
     //\r
     FileNameDevicePath = AppendDevicePath (\r
-                            FvDevicePath,\r
-                            (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath\r
-                            );\r
+                           FvDevicePath,\r
+                           (EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath\r
+                           );\r
   }\r
+\r
   return FileNameDevicePath;\r
 }\r
 \r
 /**\r
   Add an entry to the mDiscoveredList. Allocate memory to store the DriverEntry,\r
-  and initilize any state variables. Read the Depex from the FV and store it\r
+  and initialize any state variables. Read the Depex from the FV and store it\r
   in DriverEntry. Pre-process the Depex to set the Before and After state.\r
   The Discovered list is never free'ed and contains booleans that represent the\r
   other possible SMM driver states.\r
@@ -1167,7 +1240,7 @@ SmmAddToDriverList (
   DriverEntry = AllocateZeroPool (sizeof (EFI_SMM_DRIVER_ENTRY));\r
   ASSERT (DriverEntry != NULL);\r
 \r
-  DriverEntry->Signature        = EFI_SMM_DRIVER_ENTRY_SIGNATURE;\r
+  DriverEntry->Signature = EFI_SMM_DRIVER_ENTRY_SIGNATURE;\r
   CopyGuid (&DriverEntry->FileName, DriverName);\r
   DriverEntry->FvHandle         = FvHandle;\r
   DriverEntry->Fv               = Fv;\r
@@ -1210,41 +1283,41 @@ EFI_STATUS
 EFIAPI\r
 SmmDriverDispatchHandler (\r
   IN     EFI_HANDLE  DispatchHandle,\r
-  IN     CONST VOID  *Context,        OPTIONAL\r
-  IN OUT VOID        *CommBuffer,     OPTIONAL\r
+  IN     CONST VOID  *Context         OPTIONAL,\r
+  IN OUT VOID        *CommBuffer      OPTIONAL,\r
   IN OUT UINTN       *CommBufferSize  OPTIONAL\r
   )\r
 {\r
-  EFI_STATUS                    Status;\r
-  UINTN                         HandleCount;\r
-  EFI_HANDLE                    *HandleBuffer;\r
-  EFI_STATUS                    GetNextFileStatus;\r
-  EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
-  EFI_DEVICE_PATH_PROTOCOL      *FvDevicePath;\r
-  EFI_HANDLE                    FvHandle;\r
-  EFI_GUID                      NameGuid;\r
-  UINTN                         Key;\r
-  EFI_FV_FILETYPE               Type;\r
-  EFI_FV_FILE_ATTRIBUTES        Attributes;\r
-  UINTN                         Size;\r
-  EFI_SMM_DRIVER_ENTRY          *DriverEntry;\r
-  EFI_GUID                      *AprioriFile;\r
-  UINTN                         AprioriEntryCount;\r
-  UINTN                         HandleIndex;\r
-  UINTN                         SmmTypeIndex;\r
-  UINTN                         AprioriIndex;\r
-  LIST_ENTRY                    *Link;\r
-  UINT32                        AuthenticationStatus;\r
-  UINTN                         SizeOfBuffer;\r
+  EFI_STATUS                     Status;\r
+  UINTN                          HandleCount;\r
+  EFI_HANDLE                     *HandleBuffer;\r
+  EFI_STATUS                     GetNextFileStatus;\r
+  EFI_FIRMWARE_VOLUME2_PROTOCOL  *Fv;\r
+  EFI_DEVICE_PATH_PROTOCOL       *FvDevicePath;\r
+  EFI_HANDLE                     FvHandle;\r
+  EFI_GUID                       NameGuid;\r
+  UINTN                          Key;\r
+  EFI_FV_FILETYPE                Type;\r
+  EFI_FV_FILE_ATTRIBUTES         Attributes;\r
+  UINTN                          Size;\r
+  EFI_SMM_DRIVER_ENTRY           *DriverEntry;\r
+  EFI_GUID                       *AprioriFile;\r
+  UINTN                          AprioriEntryCount;\r
+  UINTN                          HandleIndex;\r
+  UINTN                          SmmTypeIndex;\r
+  UINTN                          AprioriIndex;\r
+  LIST_ENTRY                     *Link;\r
+  UINT32                         AuthenticationStatus;\r
+  UINTN                          SizeOfBuffer;\r
 \r
   HandleBuffer = NULL;\r
-  Status = gBS->LocateHandleBuffer (\r
-                  ByProtocol,\r
-                  &gEfiFirmwareVolume2ProtocolGuid,\r
-                  NULL,\r
-                  &HandleCount,\r
-                  &HandleBuffer\r
-                  );\r
+  Status       = gBS->LocateHandleBuffer (\r
+                        ByProtocol,\r
+                        &gEfiFirmwareVolume2ProtocolGuid,\r
+                        NULL,\r
+                        &HandleCount,\r
+                        &HandleBuffer\r
+                        );\r
   if (EFI_ERROR (Status)) {\r
     return EFI_NOT_FOUND;\r
   }\r
@@ -1262,7 +1335,7 @@ SmmDriverDispatchHandler (
     //\r
     // Since we are about to process this Fv mark it as processed.\r
     //\r
-    FvIsBeingProcesssed (FvHandle);\r
+    FvIsBeingProcessed (FvHandle);\r
 \r
     Status = gBS->HandleProtocol (FvHandle, &gEfiFirmwareVolume2ProtocolGuid, (VOID **)&Fv);\r
     if (EFI_ERROR (Status)) {\r
@@ -1292,7 +1365,7 @@ SmmDriverDispatchHandler (
       //\r
       Key = 0;\r
       do {\r
-        Type = mSmmFileTypes[SmmTypeIndex];\r
+        Type              = mSmmFileTypes[SmmTypeIndex];\r
         GetNextFileStatus = Fv->GetNextFile (\r
                                   Fv,\r
                                   &Key,\r
@@ -1327,6 +1400,28 @@ SmmDriverDispatchHandler (
 \r
               mSmmCoreLoadedImage->DeviceHandle = FvHandle;\r
             }\r
+\r
+            if (mSmmCoreDriverEntry->SmmLoadedImage.FilePath == NULL) {\r
+              //\r
+              // Maybe one special FV contains only one SMM_CORE module, so its device path must\r
+              // be initialized completely.\r
+              //\r
+              EfiInitializeFwVolDevicepathNode (&mFvDevicePath.File, &NameGuid);\r
+              SetDevicePathEndNode (&mFvDevicePath.End);\r
+\r
+              //\r
+              // Make a buffer copy FilePath\r
+              //\r
+              Status = SmmAllocatePool (\r
+                         EfiRuntimeServicesData,\r
+                         GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath),\r
+                         (VOID **)&mSmmCoreDriverEntry->SmmLoadedImage.FilePath\r
+                         );\r
+              ASSERT_EFI_ERROR (Status);\r
+              CopyMem (mSmmCoreDriverEntry->SmmLoadedImage.FilePath, &mFvDevicePath, GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *)&mFvDevicePath));\r
+\r
+              mSmmCoreDriverEntry->SmmLoadedImage.DeviceHandle = FvHandle;\r
+            }\r
           } else {\r
             SmmAddToDriverList (Fv, FvHandle, &NameGuid);\r
           }\r
@@ -1339,15 +1434,15 @@ SmmDriverDispatchHandler (
     // (Note: AprioriFile is in DXE memory)\r
     //\r
     AprioriFile = NULL;\r
-    Status = Fv->ReadSection (\r
-                  Fv,\r
-                  &gAprioriGuid,\r
-                  EFI_SECTION_RAW,\r
-                  0,\r
-                  (VOID **)&AprioriFile,\r
-                  &SizeOfBuffer,\r
-                  &AuthenticationStatus\r
-                  );\r
+    Status      = Fv->ReadSection (\r
+                        Fv,\r
+                        &gAprioriGuid,\r
+                        EFI_SECTION_RAW,\r
+                        0,\r
+                        (VOID **)&AprioriFile,\r
+                        &SizeOfBuffer,\r
+                        &AuthenticationStatus\r
+                        );\r
     if (!EFI_ERROR (Status)) {\r
       AprioriEntryCount = SizeOfBuffer / sizeof (EFI_GUID);\r
     } else {\r
@@ -1362,9 +1457,10 @@ SmmDriverDispatchHandler (
 \r
     for (AprioriIndex = 0; AprioriIndex < AprioriEntryCount; AprioriIndex++) {\r
       for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
-        DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
+        DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
         if (CompareGuid (&DriverEntry->FileName, &AprioriFile[AprioriIndex]) &&\r
-            (FvHandle == DriverEntry->FvHandle)) {\r
+            (FvHandle == DriverEntry->FvHandle))\r
+        {\r
           DriverEntry->Dependent = FALSE;\r
           DriverEntry->Scheduled = TRUE;\r
           InsertTailList (&mScheduledQueue, &DriverEntry->ScheduledLink);\r
@@ -1378,14 +1474,14 @@ SmmDriverDispatchHandler (
     //\r
     // Free data allocated by Fv->ReadSection ()\r
     //\r
-    // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection \r
+    // The UEFI Boot Services FreePool() function must be used because Fv->ReadSection\r
     // used the UEFI Boot Services AllocatePool() function\r
     //\r
     gBS->FreePool (AprioriFile);\r
   }\r
 \r
   //\r
-  // Execute the SMM Dispatcher on any newly discovered FVs and previously \r
+  // Execute the SMM Dispatcher on any newly discovered FVs and previously\r
   // discovered SMM drivers that have been discovered but not dispatched.\r
   //\r
   Status = SmmDispatcher ();\r
@@ -1393,11 +1489,11 @@ SmmDriverDispatchHandler (
   //\r
   // Check to see if CommBuffer and CommBufferSize are valid\r
   //\r
-  if (CommBuffer != NULL && CommBufferSize != NULL) {\r
+  if ((CommBuffer != NULL) && (CommBufferSize != NULL)) {\r
     if (*CommBufferSize > 0) {\r
       if (Status == EFI_NOT_READY) {\r
         //\r
-        // If a the SMM Core Entry Point was just registered, then set flag to \r
+        // If a the SMM Core Entry Point was just registered, then set flag to\r
         // request the SMM Dispatcher to be restarted.\r
         //\r
         *(UINT8 *)CommBuffer = COMM_BUFFER_SMM_DISPATCH_RESTART;\r
@@ -1420,7 +1516,7 @@ SmmDriverDispatchHandler (
 \r
 /**\r
   Traverse the discovered list for any drivers that were discovered but not loaded\r
-  because the dependency experessions evaluated to false.\r
+  because the dependency expressions evaluated to false.\r
 \r
 **/\r
 VOID\r
@@ -1428,11 +1524,11 @@ SmmDisplayDiscoveredNotDispatched (
   VOID\r
   )\r
 {\r
-  LIST_ENTRY                   *Link;\r
-  EFI_SMM_DRIVER_ENTRY         *DriverEntry;\r
+  LIST_ENTRY            *Link;\r
+  EFI_SMM_DRIVER_ENTRY  *DriverEntry;\r
 \r
-  for (Link = mDiscoveredList.ForwardLink;Link !=&mDiscoveredList; Link = Link->ForwardLink) {\r
-    DriverEntry = CR(Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
+  for (Link = mDiscoveredList.ForwardLink; Link != &mDiscoveredList; Link = Link->ForwardLink) {\r
+    DriverEntry = CR (Link, EFI_SMM_DRIVER_ENTRY, Link, EFI_SMM_DRIVER_ENTRY_SIGNATURE);\r
     if (DriverEntry->Dependent) {\r
       DEBUG ((DEBUG_LOAD, "SMM Driver %g was discovered but not loaded!!\n", &DriverEntry->FileName));\r
     }\r