]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/SmmMemLib/SmmMemLib.c
MdePkg-SmmMemLib: Enhance SmmIsBufferOutsideSmmValid() check for fixed comm buffer.
[mirror_edk2.git] / MdePkg / Library / SmmMemLib / SmmMemLib.c
index 420c5a755f90b7149c181daaa14ab711b6ace117..b4e3156cb42a747feccc656a1119c116a8066b80 100644 (file)
@@ -6,7 +6,7 @@
   all SMRAM range via SMM_ACCESS2_PROTOCOL, including the range for firmware (like SMM Core\r
   and SMM driver) and/or specific dedicated hardware.\r
 \r
-  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
 #include <Library/SmmServicesTableLib.h>\r
 #include <Library/HobLib.h>\r
 #include <Protocol/SmmAccess2.h>\r
+#include <Protocol/SmmReadyToLock.h>\r
+#include <Protocol/SmmEndOfDxe.h>\r
+\r
+#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \\r
+  ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size)))\r
 \r
 EFI_SMRAM_DESCRIPTOR *mSmmMemLibInternalSmramRanges;\r
 UINTN                mSmmMemLibInternalSmramCount;\r
@@ -37,6 +42,15 @@ UINTN                mSmmMemLibInternalSmramCount;
 //\r
 EFI_PHYSICAL_ADDRESS  mSmmMemLibInternalMaximumSupportAddress = 0;\r
 \r
+UINTN                 mMemoryMapEntryCount;\r
+EFI_MEMORY_DESCRIPTOR *mMemoryMap;\r
+UINTN                 mDescriptorSize;\r
+\r
+VOID                  *mRegistrationEndOfDxe;\r
+VOID                  *mRegistrationReadyToLock;\r
+\r
+BOOLEAN               mSmmReadyToLock = FALSE;\r
+\r
 /**\r
   Calculate and save the maximum support address.\r
 \r
@@ -137,6 +151,34 @@ SmmIsBufferOutsideSmmValid (
     }\r
   }\r
 \r
+  //\r
+  // Check override for Valid Communication Region\r
+  //\r
+  if (mSmmReadyToLock) {\r
+    EFI_MEMORY_DESCRIPTOR          *MemoryMap;\r
+    BOOLEAN                        InValidCommunicationRegion;\r
+    \r
+    InValidCommunicationRegion = FALSE;\r
+    MemoryMap = mMemoryMap;\r
+    for (Index = 0; Index < mMemoryMapEntryCount; Index++) {\r
+      if ((Buffer >= MemoryMap->PhysicalStart) &&\r
+          (Buffer + Length <= MemoryMap->PhysicalStart + LShiftU64 (MemoryMap->NumberOfPages, EFI_PAGE_SHIFT))) {\r
+        InValidCommunicationRegion = TRUE;\r
+      }\r
+      MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, mDescriptorSize);\r
+    }\r
+\r
+    if (!InValidCommunicationRegion) {\r
+      DEBUG ((\r
+        EFI_D_ERROR,\r
+        "SmmIsBufferOutsideSmmValid: Not in ValidCommunicationRegion: Buffer (0x%lx) - Length (0x%lx), ",\r
+        Buffer,\r
+        Length\r
+        ));\r
+      ASSERT (FALSE);\r
+      return FALSE;\r
+    }\r
+  }\r
   return TRUE;\r
 }\r
 \r
@@ -276,6 +318,128 @@ SmmSetMem (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Notification for SMM EndOfDxe protocol.\r
+\r
+  @param[in] Protocol   Points to the protocol's unique identifier.\r
+  @param[in] Interface  Points to the interface instance.\r
+  @param[in] Handle     The handle on which the interface was installed.\r
+\r
+  @retval EFI_SUCCESS   Notification runs successfully.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SmmLibInternalEndOfDxeNotify (\r
+  IN CONST EFI_GUID  *Protocol,\r
+  IN VOID            *Interface,\r
+  IN EFI_HANDLE      Handle\r
+  )\r
+{\r
+  EFI_STATUS            Status;\r
+  UINTN                 MapKey;\r
+  UINTN                 MemoryMapSize;\r
+  EFI_MEMORY_DESCRIPTOR *MemoryMap;\r
+  EFI_MEMORY_DESCRIPTOR *MemoryMapStart;\r
+  EFI_MEMORY_DESCRIPTOR *SmmMemoryMapStart;\r
+  UINTN                 MemoryMapEntryCount;\r
+  UINTN                 DescriptorSize;\r
+  UINT32                DescriptorVersion;\r
+  UINTN                 Index;\r
+\r
+  MemoryMapSize = 0;\r
+  MemoryMap = NULL;\r
+  Status = gBS->GetMemoryMap (\r
+             &MemoryMapSize,\r
+             MemoryMap,\r
+             &MapKey,\r
+             &DescriptorSize,\r
+             &DescriptorVersion\r
+             );\r
+  ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
+\r
+  do {\r
+    Status = gBS->AllocatePool (EfiBootServicesData, MemoryMapSize, (VOID **)&MemoryMap);\r
+    ASSERT (MemoryMap != NULL);\r
+  \r
+    Status = gBS->GetMemoryMap (\r
+               &MemoryMapSize,\r
+               MemoryMap,\r
+               &MapKey,\r
+               &DescriptorSize,\r
+               &DescriptorVersion\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      gBS->FreePool (MemoryMap);\r
+    }\r
+  } while (Status == EFI_BUFFER_TOO_SMALL);\r
+\r
+  //\r
+  // Get Count\r
+  //\r
+  mDescriptorSize = DescriptorSize;\r
+  MemoryMapEntryCount = MemoryMapSize/DescriptorSize;\r
+  MemoryMapStart = MemoryMap;\r
+  mMemoryMapEntryCount = 0;\r
+  for (Index = 0; Index < MemoryMapEntryCount; Index++) {\r
+    switch (MemoryMap->Type) {\r
+    case EfiReservedMemoryType:\r
+    case EfiRuntimeServicesCode:\r
+    case EfiRuntimeServicesData:\r
+    case EfiACPIMemoryNVS:\r
+      mMemoryMapEntryCount++;\r
+      break;\r
+    }\r
+    MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, DescriptorSize);\r
+  }\r
+  MemoryMap = MemoryMapStart;\r
+  \r
+  //\r
+  // Get Data\r
+  //\r
+  mMemoryMap = AllocatePool (mMemoryMapEntryCount*DescriptorSize);\r
+  ASSERT (mMemoryMap != NULL);\r
+  SmmMemoryMapStart = mMemoryMap;\r
+  for (Index = 0; Index < MemoryMapEntryCount; Index++) {\r
+    switch (MemoryMap->Type) {\r
+    case EfiReservedMemoryType:\r
+    case EfiRuntimeServicesCode:\r
+    case EfiRuntimeServicesData:\r
+    case EfiACPIMemoryNVS:\r
+      CopyMem (mMemoryMap, MemoryMap, DescriptorSize);\r
+      mMemoryMap = NEXT_MEMORY_DESCRIPTOR(mMemoryMap, DescriptorSize);\r
+      break;\r
+    }\r
+    MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, DescriptorSize);\r
+  }\r
+  mMemoryMap = SmmMemoryMapStart;\r
+  MemoryMap = MemoryMapStart;\r
+  \r
+  gBS->FreePool (MemoryMap);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Notification for SMM ReadyToLock protocol.\r
+\r
+  @param[in] Protocol   Points to the protocol's unique identifier.\r
+  @param[in] Interface  Points to the interface instance.\r
+  @param[in] Handle     The handle on which the interface was installed.\r
+\r
+  @retval EFI_SUCCESS   Notification runs successfully.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SmmLibInternalReadyToLockNotify (\r
+  IN CONST EFI_GUID  *Protocol,\r
+  IN VOID            *Interface,\r
+  IN EFI_HANDLE      Handle\r
+  )\r
+{\r
+  mSmmReadyToLock = TRUE;\r
+  return EFI_SUCCESS;\r
+}\r
 /**\r
   The constructor function initializes the Smm Mem library\r
 \r
@@ -319,6 +483,18 @@ SmmMemLibConstructor (
   //\r
   SmmMemLibInternalCalculateMaximumSupportAddress ();\r
 \r
+  //\r
+  // Register EndOfDxe to get UEFI memory map\r
+  //\r
+  Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, SmmLibInternalEndOfDxeNotify, &mRegistrationEndOfDxe);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Register ready to lock so that we can know when to check valid SMRAM region\r
+  //\r
+  Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, SmmLibInternalReadyToLockNotify, &mRegistrationReadyToLock);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -339,5 +515,7 @@ SmmMemLibDestructor (
 {\r
   FreePool (mSmmMemLibInternalSmramRanges);\r
 \r
+  gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, NULL, &mRegistrationEndOfDxe);\r
+  gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, NULL, &mRegistrationReadyToLock);\r
   return EFI_SUCCESS;\r
 }\r