]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
MdeModulePkg PiSmmIpl: Handle CommSize OPTIONAL case
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmIpl.c
index 9b269779ea256b3002b82b7eae3f0f3b09cd1672..31d2c9e45e1f56e7483892ab5e246a3f3e9b03cb 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   SMM IPL that produces SMM related runtime protocols and load the SMM Core into SMRAM\r
 \r
-  Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2017, 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
@@ -38,6 +38,7 @@
 #include <Library/UefiLib.h>\r
 #include <Library/UefiRuntimeLib.h>\r
 #include <Library/PcdLib.h>\r
+#include <Library/ReportStatusCodeLib.h>\r
 \r
 #include "PiSmmCorePrivateData.h"\r
 \r
@@ -162,6 +163,20 @@ SmmIplGuidedEventNotify (
   IN VOID       *Context\r
   );\r
 \r
+/**\r
+  Event notification that is fired when EndOfDxe Event Group is signaled.\r
+\r
+  @param  Event                 The Event that is being processed, not used.\r
+  @param  Context               Event Context, not used.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+SmmIplEndOfDxeEventNotify (\r
+  IN EFI_EVENT  Event,\r
+  IN VOID       *Context\r
+  );\r
+\r
 /**\r
   Notification function of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE.\r
 \r
@@ -243,15 +258,19 @@ EFI_SMM_CONTROL2_PROTOCOL  *mSmmControl2;
 EFI_SMM_ACCESS2_PROTOCOL   *mSmmAccess;\r
 EFI_SMRAM_DESCRIPTOR       *mCurrentSmramRange;\r
 BOOLEAN                    mSmmLocked = FALSE;\r
+BOOLEAN                    mEndOfDxe  = FALSE;\r
 EFI_PHYSICAL_ADDRESS       mSmramCacheBase;\r
 UINT64                     mSmramCacheSize;\r
 \r
+EFI_SMM_COMMUNICATE_HEADER mCommunicateHeader;\r
+EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE    *mLMFAConfigurationTable = NULL;\r
+\r
 //\r
 // Table of Protocol notification and GUIDed Event notifications that the SMM IPL requires\r
 //\r
 SMM_IPL_EVENT_NOTIFICATION  mSmmIplEvents[] = {\r
   //\r
-  // Declare protocol notification on the SMM Configuration protocol.  When this notification is etablished, \r
+  // Declare protocol notification on the SMM Configuration protocol.  When this notification is established,\r
   // the associated event is immediately signalled, so the notification function will be executed and the \r
   // SMM Configuration Protocol will be found if it is already in the handle database.\r
   //\r
@@ -263,12 +282,16 @@ SMM_IPL_EVENT_NOTIFICATION  mSmmIplEvents[] = {
   //\r
   { TRUE,  TRUE,  &gEfiDxeSmmReadyToLockProtocolGuid, SmmIplReadyToLockEventNotify,      &gEfiDxeSmmReadyToLockProtocolGuid, TPL_CALLBACK, NULL },\r
   //\r
-  // Declare event notification on EndOfDxe event.  When this notification is etablished, \r
+  // Declare event notification on EndOfDxe event.  When this notification is established,\r
   // the associated event is immediately signalled, so the notification function will be executed and the \r
   // SMM End Of Dxe Protocol will be found if it is already in the handle database.\r
   //\r
   { FALSE, TRUE,  &gEfiEndOfDxeEventGroupGuid,        SmmIplGuidedEventNotify,           &gEfiEndOfDxeEventGroupGuid,        TPL_CALLBACK, NULL },\r
   //\r
+  // Declare event notification on EndOfDxe event.  This is used to set EndOfDxe event signaled flag.\r
+  //\r
+  { FALSE, TRUE,  &gEfiEndOfDxeEventGroupGuid,        SmmIplEndOfDxeEventNotify,         &gEfiEndOfDxeEventGroupGuid,        TPL_CALLBACK, NULL },\r
+  //\r
   // Declare event notification on the DXE Dispatch Event Group.  This event is signaled by the DXE Core\r
   // each time the DXE Core dispatcher has completed its work.  When this event is signalled, the SMM Core\r
   // if notified, so the SMM Core can dispatch SMM drivers.\r
@@ -417,37 +440,55 @@ SmmBase2GetSmstLocation (
   after SetVirtualAddressMap().\r
 \r
   @param[in] This                The EFI_SMM_COMMUNICATION_PROTOCOL instance.\r
-  @param[in, out] CommBuffer          A pointer to the buffer to convey into SMRAM.\r
-  @param[in, out] CommSize            The size of the data buffer being passed in.On exit, the size of data\r
+  @param[in, out] CommBuffer     A pointer to the buffer to convey into SMRAM.\r
+  @param[in, out] CommSize       The size of the data buffer being passed in. On exit, the size of data\r
                                  being returned. Zero if the handler does not wish to reply with any data.\r
+                                 This parameter is optional and may be NULL.\r
 \r
   @retval EFI_SUCCESS            The message was successfully posted.\r
   @retval EFI_INVALID_PARAMETER  The CommBuffer was NULL.\r
+  @retval EFI_BAD_BUFFER_SIZE    The buffer is too large for the MM implementation.\r
+                                 If this error is returned, the MessageLength field\r
+                                 in the CommBuffer header or the integer pointed by\r
+                                 CommSize, are updated to reflect the maximum payload\r
+                                 size the implementation can accommodate.\r
+  @retval EFI_ACCESS_DENIED      The CommunicateBuffer parameter or CommSize parameter,\r
+                                 if not omitted, are in address range that cannot be\r
+                                 accessed by the MM environment.\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 SmmCommunicationCommunicate (\r
   IN CONST EFI_SMM_COMMUNICATION_PROTOCOL  *This,\r
   IN OUT VOID                              *CommBuffer,\r
-  IN OUT UINTN                             *CommSize\r
+  IN OUT UINTN                             *CommSize OPTIONAL\r
   )\r
 {\r
   EFI_STATUS                  Status;\r
   EFI_SMM_COMMUNICATE_HEADER  *CommunicateHeader;\r
   BOOLEAN                     OldInSmm;\r
+  UINTN                       TempCommSize;\r
 \r
   //\r
   // Check parameters\r
   //\r
-  if ((CommBuffer == NULL) || (CommSize == NULL)) {\r
+  if (CommBuffer == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  //\r
-  // CommSize must hold HeaderGuid and MessageLength\r
-  //\r
-  if (*CommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {\r
-    return EFI_INVALID_PARAMETER;\r
+  CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer;\r
+\r
+  if (CommSize == NULL) {\r
+    TempCommSize = OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data) + CommunicateHeader->MessageLength;\r
+  } else {\r
+    TempCommSize = *CommSize;\r
+    //\r
+    // CommSize must hold HeaderGuid and MessageLength\r
+    //\r
+    if (TempCommSize < OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
   }\r
 \r
   //\r
@@ -458,7 +499,7 @@ SmmCommunicationCommunicate (
     // Put arguments for Software SMI in gSmmCorePrivate\r
     //\r
     gSmmCorePrivate->CommunicationBuffer = CommBuffer;\r
-    gSmmCorePrivate->BufferSize          = *CommSize;\r
+    gSmmCorePrivate->BufferSize          = TempCommSize;\r
 \r
     //\r
     // Generate Software SMI\r
@@ -471,15 +512,17 @@ SmmCommunicationCommunicate (
     //\r
     // Return status from software SMI \r
     //\r
-    *CommSize = gSmmCorePrivate->BufferSize;\r
+    if (CommSize != NULL) {\r
+      *CommSize = gSmmCorePrivate->BufferSize;\r
+    }\r
     return gSmmCorePrivate->ReturnStatus;\r
   }\r
 \r
   //\r
   // If we are in SMM, then the execution mode must be physical, which means that\r
   // OS established virtual addresses can not be used.  If SetVirtualAddressMap()\r
-  // has been called, then a direct invocation of the Software SMI is not \r
-  // not allowed so return EFI_INVALID_PARAMETER.\r
+  // has been called, then a direct invocation of the Software SMI is not allowed,\r
+  // so return EFI_INVALID_PARAMETER.\r
   //\r
   if (EfiGoneVirtual()) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -499,22 +542,19 @@ SmmCommunicationCommunicate (
   gSmmCorePrivate->InSmm = TRUE;\r
 \r
   //\r
-  // Already in SMM and before SetVirtualAddressMap(), so call SmiManage() directly.\r
+  // Before SetVirtualAddressMap(), we are in SMM or SMRAM is open and unlocked, call SmiManage() directly.\r
   //\r
-  CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)CommBuffer;\r
-  *CommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
+  TempCommSize -= OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
   Status = gSmmCorePrivate->Smst->SmiManage (\r
                                     &CommunicateHeader->HeaderGuid, \r
                                     NULL, \r
                                     CommunicateHeader->Data, \r
-                                    CommSize\r
+                                    &TempCommSize\r
                                     );\r
-\r
-  //\r
-  // Update CommunicationBuffer, BufferSize and ReturnStatus\r
-  // Communicate service finished, reset the pointer to CommBuffer to NULL\r
-  //\r
-  *CommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
+  TempCommSize += OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data);\r
+  if (CommSize != NULL) {\r
+    *CommSize = TempCommSize;\r
+  }\r
 \r
   //\r
   // Restore original InSmm state\r
@@ -538,21 +578,37 @@ SmmIplGuidedEventNotify (
   IN VOID       *Context\r
   )\r
 {\r
-  EFI_SMM_COMMUNICATE_HEADER  CommunicateHeader;\r
   UINTN                       Size;\r
 \r
   //\r
   // Use Guid to initialize EFI_SMM_COMMUNICATE_HEADER structure \r
   //\r
-  CopyGuid (&CommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
-  CommunicateHeader.MessageLength = 1;\r
-  CommunicateHeader.Data[0] = 0;\r
+  CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
+  mCommunicateHeader.MessageLength = 1;\r
+  mCommunicateHeader.Data[0] = 0;\r
 \r
   //\r
   // Generate the Software SMI and return the result\r
   //\r
-  Size = sizeof (CommunicateHeader);\r
-  SmmCommunicationCommunicate (&mSmmCommunication, &CommunicateHeader, &Size);\r
+  Size = sizeof (mCommunicateHeader);\r
+  SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);\r
+}\r
+\r
+/**\r
+  Event notification that is fired when EndOfDxe Event Group is signaled.\r
+\r
+  @param  Event                 The Event that is being processed, not used.\r
+  @param  Context               Event Context, not used.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+SmmIplEndOfDxeEventNotify (\r
+  IN EFI_EVENT  Event,\r
+  IN VOID       *Context\r
+  )\r
+{\r
+  mEndOfDxe = TRUE;\r
 }\r
 \r
 /**\r
@@ -569,7 +625,6 @@ SmmIplDxeDispatchEventNotify (
   IN VOID       *Context\r
   )\r
 {\r
-  EFI_SMM_COMMUNICATE_HEADER  CommunicateHeader;\r
   UINTN                       Size;\r
   EFI_STATUS                  Status;\r
 \r
@@ -582,20 +637,20 @@ SmmIplDxeDispatchEventNotify (
     // Clear the buffer passed into the Software SMI.  This buffer will return\r
     // the status of the SMM Core Dispatcher.\r
     //\r
-    CopyGuid (&CommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
-    CommunicateHeader.MessageLength = 1;\r
-    CommunicateHeader.Data[0] = 0;\r
+    CopyGuid (&mCommunicateHeader.HeaderGuid, (EFI_GUID *)Context);\r
+    mCommunicateHeader.MessageLength = 1;\r
+    mCommunicateHeader.Data[0] = 0;\r
 \r
     //\r
     // Generate the Software SMI and return the result\r
     //\r
-    Size = sizeof (CommunicateHeader);\r
-    SmmCommunicationCommunicate (&mSmmCommunication, &CommunicateHeader, &Size);\r
+    Size = sizeof (mCommunicateHeader);\r
+    SmmCommunicationCommunicate (&mSmmCommunication, &mCommunicateHeader, &Size);\r
     \r
     //\r
     // Return if there is no request to restart the SMM Core Dispatcher\r
     //\r
-    if (CommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {\r
+    if (mCommunicateHeader.Data[0] != COMM_BUFFER_SMM_DISPATCH_RESTART) {\r
       return;\r
     }\r
       \r
@@ -711,6 +766,15 @@ SmmIplReadyToLockEventNotify (
     DEBUG ((DEBUG_WARN, "SMM IPL!  DXE SMM Ready To Lock Protocol not installed before Ready To Boot signal\n"));\r
   }\r
 \r
+  if (!mEndOfDxe) {\r
+    DEBUG ((DEBUG_ERROR, "EndOfDxe Event must be signaled before DxeSmmReadyToLock Protocol installation!\n"));\r
+    REPORT_STATUS_CODE (\r
+      EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,\r
+      (EFI_SOFTWARE_SMM_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)\r
+      );\r
+    ASSERT (FALSE);\r
+  }\r
+\r
   //\r
   // Lock the SMRAM (Note: Locking SMRAM may not be supported on all platforms)\r
   //\r
@@ -764,13 +828,13 @@ SmmIplSetVirtualAddressNotify (
 }\r
 \r
 /**\r
-  Get the fixed loadding address from image header assigned by build tool. This function only be called\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
   @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
-  @retval EFI_NOT_FOUND             The image has no assigned fixed loadding address.\r
+  @retval EFI_NOT_FOUND             The image has no assigned fixed loading address.\r
 **/\r
 EFI_STATUS\r
 GetPeCoffImageFixLoadingAssignedAddress(\r
@@ -781,7 +845,7 @@ GetPeCoffImageFixLoadingAssignedAddress(
    EFI_STATUS                         Status;\r
    EFI_IMAGE_SECTION_HEADER           SectionHeader;\r
    EFI_IMAGE_OPTIONAL_HEADER_UNION    *ImgHdr;\r
-   EFI_PHYSICAL_ADDRESS               FixLoaddingAddress;\r
+   EFI_PHYSICAL_ADDRESS               FixLoadingAddress;\r
    UINT16                             Index;\r
    UINTN                              Size;\r
    UINT16                             NumberOfSections;\r
@@ -793,19 +857,17 @@ GetPeCoffImageFixLoadingAssignedAddress(
    //\r
    SmmCodeSize = EFI_PAGES_TO_SIZE (PcdGet32(PcdLoadFixAddressSmmCodePageNumber));\r
  \r
-   FixLoaddingAddress = 0;\r
+   FixLoadingAddress = 0;\r
    Status = EFI_NOT_FOUND;\r
-   SmramBase = mCurrentSmramRange->CpuStart;\r
+   SmramBase = mLMFAConfigurationTable->SmramBase;\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
+   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
@@ -833,21 +895,21 @@ GetPeCoffImageFixLoadingAssignedAddress(
        // Build tool saves the offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields in the\r
        // first section header that doesn't point to code section in image header. And there is an assumption that when the\r
        // feature is enabled, if a module is assigned a loading address by tools, PointerToRelocations & PointerToLineNumbers\r
-       // fields should NOT be Zero, or else, these 2 fileds should be set to Zero\r
+       // fields should NOT be Zero, or else, these 2 fields should be set to Zero\r
        //\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 uild tool saves the\r
+         // Found first section header that doesn't point to code section in which build tool saves the\r
          // offset to SMRAM base as image base in PointerToRelocations & PointerToLineNumbers fields\r
          //\r
-         FixLoaddingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);\r
+         FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(SmramBase + (INT64)ValueInSectionHeader);\r
 \r
-         if (SmramBase + SmmCodeSize > FixLoaddingAddress && SmramBase <=  FixLoaddingAddress) {\r
+         if (SmramBase + SmmCodeSize > FixLoadingAddress && SmramBase <=  FixLoadingAddress) {\r
            //\r
-           // The assigned address is valid. Return the specified loadding address\r
+           // The assigned address is valid. Return the specified loading address\r
            //\r
-           ImageContext->ImageAddress = FixLoaddingAddress;\r
+           ImageContext->ImageAddress = FixLoadingAddress;\r
            Status = EFI_SUCCESS;\r
          }\r
        }\r
@@ -855,7 +917,7 @@ GetPeCoffImageFixLoadingAssignedAddress(
      }\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", FixLoaddingAddress, Status));\r
+   DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r \n", FixLoadingAddress, Status));\r
    return Status;\r
 }\r
 /**\r
@@ -927,6 +989,10 @@ ExecuteSmmCoreFromSmram (
       // Since the memory range to load SMM CORE will be cut out in SMM core, so no need to allocate and free this range\r
       //\r
       PageCount = 0;\r
+      //\r
+      // Reserved Smram Region for SmmCore is not used, and remove it from SmramRangeCount.\r
+      //\r
+      gSmmCorePrivate->SmramRangeCount --;\r
     } else {\r
       DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED ERROR: Loading module at fixed address at address failed\n"));\r
       //\r
@@ -972,7 +1038,7 @@ ExecuteSmmCoreFromSmram (
   }\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
   // Print debug message showing SMM Core load address.\r
@@ -1253,6 +1319,7 @@ GetFullSmramRanges (
   UINTN                             Index2;\r
   EFI_SMRAM_DESCRIPTOR              *FullSmramRanges;\r
   UINTN                             TempSmramRangeCount;\r
+  UINTN                             AdditionSmramRangeCount;\r
   EFI_SMRAM_DESCRIPTOR              *TempSmramRanges;\r
   UINTN                             SmramRangeCount;\r
   EFI_SMRAM_DESCRIPTOR              *SmramRanges;\r
@@ -1286,14 +1353,24 @@ GetFullSmramRanges (
     }\r
   }\r
 \r
+  //\r
+  // Reserve one entry for SMM Core in the full SMRAM ranges.\r
+  //\r
+  AdditionSmramRangeCount = 1;\r
+  if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0) {\r
+    //\r
+    // Reserve two entries for all SMM drivers and SMM Core in the full SMRAM ranges.\r
+    //\r
+    AdditionSmramRangeCount = 2;\r
+  }\r
+\r
   if (SmramReservedCount == 0) {\r
     //\r
     // No reserved SMRAM entry from SMM Configuration Protocol.\r
-    // Reserve one entry for SMM Core in the full SMRAM ranges.\r
     //\r
-    *FullSmramRangeCount = SmramRangeCount + 1;\r
+    *FullSmramRangeCount = SmramRangeCount + AdditionSmramRangeCount;\r
     Size = (*FullSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR);\r
-    FullSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocatePool (Size);\r
+    FullSmramRanges = (EFI_SMRAM_DESCRIPTOR *) AllocateZeroPool (Size);\r
     ASSERT (FullSmramRanges != NULL);\r
 \r
     Status = mSmmAccess->GetCapabilities (mSmmAccess, &Size, FullSmramRanges);\r
@@ -1403,10 +1480,9 @@ GetFullSmramRanges (
   ASSERT (TempSmramRangeCount <= MaxCount);\r
 \r
   //\r
-  // Sort the entries,\r
-  // and reserve one entry for SMM Core in the full SMRAM ranges.\r
+  // Sort the entries\r
   //\r
-  FullSmramRanges = AllocatePool ((TempSmramRangeCount + 1) * sizeof (EFI_SMRAM_DESCRIPTOR));\r
+  FullSmramRanges = AllocateZeroPool ((TempSmramRangeCount + AdditionSmramRangeCount) * sizeof (EFI_SMRAM_DESCRIPTOR));\r
   ASSERT (FullSmramRanges != NULL);\r
   *FullSmramRangeCount = 0;\r
   do {\r
@@ -1426,7 +1502,7 @@ GetFullSmramRanges (
     TempSmramRanges[Index].PhysicalSize = 0;\r
   } while (*FullSmramRangeCount < TempSmramRangeCount);\r
   ASSERT (*FullSmramRangeCount == TempSmramRangeCount);\r
-  *FullSmramRangeCount += 1;\r
+  *FullSmramRangeCount += AdditionSmramRangeCount;\r
 \r
   FreePool (SmramRanges);\r
   FreePool (SmramReservedRanges);\r
@@ -1461,9 +1537,9 @@ SmmIplEntry (
   UINT64                          MaxSize;\r
   VOID                            *Registration;\r
   UINT64                          SmmCodeSize;\r
-  EFI_LOAD_FIXED_ADDRESS_CONFIGURATION_TABLE    *LMFAConfigurationTable;\r
   EFI_CPU_ARCH_PROTOCOL           *CpuArch;\r
   EFI_STATUS                      SetAttrStatus;\r
+  EFI_SMRAM_DESCRIPTOR            *SmramRangeSmmDriver;\r
 \r
   //\r
   // Fill in the image handle of the SMM IPL so the SMM Core can use this as the \r
@@ -1510,7 +1586,7 @@ SmmIplEntry (
     }\r
 \r
     if (gSmmCorePrivate->SmramRanges[Index].CpuStart >= BASE_1MB) {\r
-      if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize) <= BASE_4GB) {\r
+      if ((gSmmCorePrivate->SmramRanges[Index].CpuStart + gSmmCorePrivate->SmramRanges[Index].PhysicalSize - 1) <= MAX_ADDRESS) {\r
         if (gSmmCorePrivate->SmramRanges[Index].PhysicalSize >= MaxSize) {\r
           MaxSize = gSmmCorePrivate->SmramRanges[Index].PhysicalSize;\r
           mCurrentSmramRange = &gSmmCorePrivate->SmramRanges[Index];\r
@@ -1564,15 +1640,28 @@ SmmIplEntry (
       //\r
       Status = EfiGetSystemConfigurationTable (\r
                 &gLoadFixedAddressConfigurationTableGuid,\r
-               (VOID **) &LMFAConfigurationTable\r
+               (VOID **) &mLMFAConfigurationTable\r
                );\r
-      if (!EFI_ERROR (Status) && LMFAConfigurationTable != NULL) {\r
-        LMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;\r
+      if (!EFI_ERROR (Status) && mLMFAConfigurationTable != NULL) {\r
+        mLMFAConfigurationTable->SmramBase = mCurrentSmramRange->CpuStart;\r
         //\r
         // Print the SMRAM base\r
         //\r
-        DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable->SmramBase));\r
+        DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", mLMFAConfigurationTable->SmramBase));\r
       }\r
+\r
+      //\r
+      // Fill the Smram range for all SMM code\r
+      //\r
+      SmramRangeSmmDriver = &gSmmCorePrivate->SmramRanges[gSmmCorePrivate->SmramRangeCount - 2];\r
+      SmramRangeSmmDriver->CpuStart      = mCurrentSmramRange->CpuStart;\r
+      SmramRangeSmmDriver->PhysicalStart = mCurrentSmramRange->PhysicalStart;\r
+      SmramRangeSmmDriver->RegionState   = mCurrentSmramRange->RegionState | EFI_ALLOCATED;\r
+      SmramRangeSmmDriver->PhysicalSize  = SmmCodeSize;\r
+\r
+      mCurrentSmramRange->PhysicalSize  -= SmmCodeSize;\r
+      mCurrentSmramRange->CpuStart       = mCurrentSmramRange->CpuStart + SmmCodeSize;\r
+      mCurrentSmramRange->PhysicalStart  = mCurrentSmramRange->PhysicalStart + SmmCodeSize;\r
     }\r
     //\r
     // Load SMM Core into SMRAM and execute it from SMRAM\r