]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c
MdeModulePkg: Refine casting expression result to bigger size
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / PiSmmIpl.c
index 18bec842c4c5c6954eac4d4cb7e0872662078f50..feb846ee9e0ef49dc92dd22b4d55bbe6d9f5550d 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 - 2016, 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
@@ -521,7 +521,7 @@ 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
@@ -846,12 +846,10 @@ GetPeCoffImageFixLoadingAssignedAddress(
    // 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
@@ -973,6 +971,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
@@ -1018,7 +1020,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
@@ -1299,6 +1301,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
@@ -1332,12 +1335,22 @@ 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 *) AllocateZeroPool (Size);\r
     ASSERT (FullSmramRanges != NULL);\r
@@ -1449,10 +1462,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 = AllocateZeroPool ((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
@@ -1472,7 +1484,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
@@ -1510,6 +1522,7 @@ SmmIplEntry (
   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
@@ -1619,6 +1632,19 @@ SmmIplEntry (
         //\r
         DEBUG ((EFI_D_INFO, "LOADING MODULE FIXED INFO: TSEG BASE is %x. \n", LMFAConfigurationTable->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