Revision: 16793
          http://sourceforge.net/p/edk2/code/16793
Author:   vanjeff
Date:     2015-02-06 06:37:07 +0000 (Fri, 06 Feb 2015)
Log Message:
-----------
Use SmmMemLib to check communication buffer.

(Sync patch r16694 from main trunk.)

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: "Yao, Jiewen" <[email protected]>
Reviewed-by: "Gao, Liming" <[email protected]>
Reviewed-by: "Fan, Jeff" <[email protected]>

Revision Links:
--------------
    http://sourceforge.net/p/edk2/code/16694

Modified Paths:
--------------
    branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
    branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/PiSmmCore.h
    branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf
    branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/SmramProfileRecord.c
    
branches/UDK2014.SP1/MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/MemoryAllocationLib.c
    
branches/UDK2014.SP1/MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryAllocationLib.inf
    
branches/UDK2014.SP1/MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryAllocationServices.h
    
branches/UDK2014.SP1/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c
    
branches/UDK2014.SP1/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.inf
    
branches/UDK2014.SP1/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLibInternal.h
    branches/UDK2014.SP1/MdeModulePkg/MdeModulePkg.dsc
    
branches/UDK2014.SP1/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.c
    
branches/UDK2014.SP1/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.inf
    
branches/UDK2014.SP1/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
    
branches/UDK2014.SP1/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf
    branches/UDK2014.SP1/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c
    
branches/UDK2014.SP1/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
    
branches/UDK2014.SP1/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
    
branches/UDK2014.SP1/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf

Modified: branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c
===================================================================
--- branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c        
2015-02-06 06:36:27 UTC (rev 16792)
+++ branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/PiSmmCore.c        
2015-02-06 06:37:07 UTC (rev 16793)
@@ -85,11 +85,6 @@
 UINTN                           mFullSmramRangeCount;
 EFI_SMRAM_DESCRIPTOR            *mFullSmramRanges;
 
-//
-// Maximum support address used to check input CommunicationBuffer
-//
-UINTN  mMaximumSupportAddress = 0;
-
 /**
   Place holder function until all the SMM System Table Service are available.
 
@@ -280,76 +275,6 @@
 }
 
 /**
-  Caculate and save the maximum support address.
-
-**/
-VOID
-CaculateMaximumSupportAddress (
-  VOID
-  )
-{
-  VOID         *Hob;
-  UINT32       RegEax;
-  UINT8        PhysicalAddressBits;
-
-  //
-  // Get physical address bits supported.
-  //
-  Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
-  if (Hob != NULL) {
-    PhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;
-  } else {
-    AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
-    if (RegEax >= 0x80000008) {
-      AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
-      PhysicalAddressBits = (UINT8) RegEax;
-    } else {
-      PhysicalAddressBits = 36;
-    }
-  }
-  //
-  // IA-32e paging translates 48-bit linear addresses to 52-bit physical 
addresses.
-  //
-  ASSERT (PhysicalAddressBits <= 52);
-  if (PhysicalAddressBits > 48) {
-    PhysicalAddressBits = 48;
-  }
-  
-  //
-  // Save the maximum support address in one global variable  
-  //
-  mMaximumSupportAddress = (UINTN) (LShiftU64 (1, PhysicalAddressBits) - 1);
-  DEBUG ((EFI_D_INFO, "mMaximumSupportAddress = 0x%lx\n", 
mMaximumSupportAddress));
-}
-
-/**
-  Check if input buffer is in valid address scope or not.
-
-  @param[in]  Pointer      Pointer to the input buffer.
-  @param[in]  BufferSize   Input buffer size in bytes.
-
-  @retval TRUE    The input buffer is in valid address scope.  
-  @retval FALSE   The input buffer is not in valid address scope.  
-
-**/
-BOOLEAN
-IsValidPointer (
-  IN VOID     *Pointer,
-  IN UINTN    BufferSize
-  )
-{
-  if ((UINTN) Pointer > mMaximumSupportAddress) {
-    return FALSE;
-  }
-
-  if (BufferSize > (mMaximumSupportAddress - (UINTN) Pointer)) {
-    return FALSE;
-  }
-
-  return TRUE;
-}
-
-/**
   The main entry point to SMM Foundation.
 
   Note: This function is only used by SMRAM invocation.  It is never used by 
DXE invocation.
@@ -398,7 +323,7 @@
       //
       // Synchronous SMI for SMM Core or request from Communicate protocol
       //
-      if (!IsValidPointer (gSmmCorePrivate->CommunicationBuffer, 
gSmmCorePrivate->BufferSize)) {
+      if (!SmmIsBufferOutsideSmmValid 
((UINTN)gSmmCorePrivate->CommunicationBuffer, gSmmCorePrivate->BufferSize)) {
         //
         // If CommunicationBuffer is not in valid address scope, return 
EFI_INVALID_PARAMETER
         //
@@ -484,9 +409,10 @@
   gSmmCorePrivate->SmmEntryPoint = SmmEntryPoint;
   
   //
-  // Initialize memory service using free SMRAM
+  // No need to initialize memory service.
+  // It is done in constructor of PiSmmCoreMemoryAllocationLib(),
+  // so that the library linked with PiSmmCore can use AllocatePool() in 
constuctor.
   //
-  SmmInitializeMemoryServices (gSmmCorePrivate->SmramRangeCount, 
gSmmCorePrivate->SmramRanges);
 
   SmramProfileInit ();
 
@@ -512,10 +438,5 @@
 
   RegisterSmramProfileHandler ();
 
-  //
-  // Caculate and save maximum support address used in SmmEntryPoint().
-  //
-  CaculateMaximumSupportAddress ();
-
   return EFI_SUCCESS;
 }

Modified: branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/PiSmmCore.h
===================================================================
--- branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/PiSmmCore.h        
2015-02-06 06:36:27 UTC (rev 16792)
+++ branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/PiSmmCore.h        
2015-02-06 06:37:07 UTC (rev 16793)
@@ -51,6 +51,7 @@
 #include <Library/PerformanceLib.h>
 #include <Library/TimerLib.h>
 #include <Library/HobLib.h>
+#include <Library/SmmMemLib.h>
 
 #include "PiSmmCorePrivateData.h"
 

Modified: branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf
===================================================================
--- branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf      
2015-02-06 06:36:27 UTC (rev 16792)
+++ branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf      
2015-02-06 06:37:07 UTC (rev 16793)
@@ -1,7 +1,7 @@
 ## @file
 # This module provide an SMM CIS compliant implementation of SMM Core.
 #
-# Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
 #
 # This program and the accompanying materials
 # are licensed and made available under the terms and conditions of the BSD 
License
@@ -60,6 +60,7 @@
   PerformanceLib
   TimerLib
   HobLib
+  SmmMemLib
 
 [Protocols]
   gEfiDxeSmmReadyToLockProtocolGuid             ## UNDEFINED # 
SmiHandlerRegister

Modified: branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/SmramProfileRecord.c
===================================================================
--- branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/SmramProfileRecord.c       
2015-02-06 06:36:27 UTC (rev 16792)
+++ branches/UDK2014.SP1/MdeModulePkg/Core/PiSmmCore/SmramProfileRecord.c       
2015-02-06 06:37:07 UTC (rev 16793)
@@ -1,7 +1,7 @@
 /** @file
   Support routines for SMRAM profile.
 
-  Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD 
License
   which accompanies this distribution.  The full text of the license may be 
found at
@@ -1176,61 +1176,6 @@
 ////////////////////
 
 /**
-  This function check if the address is in SMRAM.
-
-  @param Buffer  the buffer address to be checked.
-  @param Length  the buffer length to be checked.
-
-  @retval TRUE  this address is in SMRAM.
-  @retval FALSE this address is NOT in SMRAM.
-
-**/
-BOOLEAN
-InternalIsAddressInSmram (
-  IN PHYSICAL_ADDRESS   Buffer,
-  IN UINT64             Length
-  )
-{
-  UINTN  Index;
-
-  for (Index = 0; Index < mFullSmramRangeCount; Index ++) {
-    if (((Buffer >= mFullSmramRanges[Index].CpuStart) && (Buffer < 
mFullSmramRanges[Index].CpuStart + mFullSmramRanges[Index].PhysicalSize)) ||
-        ((mFullSmramRanges[Index].CpuStart >= Buffer) && 
(mFullSmramRanges[Index].CpuStart < Buffer + Length))) {
-      return TRUE;
-    }
-  }
-
-  return FALSE;
-}
-
-/**
-  This function check if the address refered by Buffer and Length is valid.
-
-  @param Buffer  the buffer address to be checked.
-  @param Length  the buffer length to be checked.
-
-  @retval TRUE  this address is valid.
-  @retval FALSE this address is NOT valid.
-**/
-BOOLEAN
-InternalIsAddressValid (
-  IN UINTN                 Buffer,
-  IN UINTN                 Length
-  )
-{
-  if (Buffer > (MAX_ADDRESS - Length)) {
-    //
-    // Overflow happen
-    //
-    return FALSE;
-  }
-  if (InternalIsAddressInSmram ((PHYSICAL_ADDRESS) Buffer, (UINT64)Length)) {
-    return FALSE;
-  }
-  return TRUE;
-}
-
-/**
   Get SMRAM profile data size.
 
   @return SMRAM profile data size.
@@ -1485,7 +1430,7 @@
   //
   // Sanity check
   //
-  if (!InternalIsAddressValid ((UINTN) SmramProfileGetData.ProfileBuffer, 
(UINTN) ProfileSize)) {
+  if (!SmmIsBufferOutsideSmmValid ((UINTN) SmramProfileGetData.ProfileBuffer, 
(UINTN) ProfileSize)) {
     DEBUG ((EFI_D_ERROR, "SmramProfileHandlerGetData: SMM ProfileBuffer in 
SMRAM or overflow!\n"));
     SmramProfileParameterGetData->ProfileSize = ProfileSize;
     SmramProfileParameterGetData->Header.ReturnStatus = (UINT64) (INT64) 
(INTN) EFI_ACCESS_DENIED;
@@ -1610,7 +1555,7 @@
     return EFI_SUCCESS;
   }
 
-  if (mSmramReadyToLock && !InternalIsAddressValid ((UINTN)CommBuffer, 
TempCommBufferSize)) {
+  if (mSmramReadyToLock && !SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, 
TempCommBufferSize)) {
     DEBUG ((EFI_D_ERROR, "SmramProfileHandler: SMM communication buffer in 
SMRAM or overflow!\n"));
     return EFI_SUCCESS;
   }

Modified: 
branches/UDK2014.SP1/MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/MemoryAllocationLib.c
===================================================================
--- 
branches/UDK2014.SP1/MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/MemoryAllocationLib.c
        2015-02-06 06:36:27 UTC (rev 16792)
+++ 
branches/UDK2014.SP1/MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/MemoryAllocationLib.c
        2015-02-06 06:37:07 UTC (rev 16793)
@@ -1,7 +1,7 @@
 /** @file
   Support routines for memory allocation routines based on SMM Core internal 
functions.
 
-  Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials                          
   are licensed and made available under the terms and conditions of the BSD 
License         
   which accompanies this distribution.  The full text of the license may be 
found at        
@@ -936,3 +936,28 @@
   ASSERT_EFI_ERROR (Status);
 }
 
+/**
+  The constructor function calls SmmInitializeMemoryServices to initialize 
memory in SMRAM.
+
+  @param  ImageHandle   The firmware allocated handle for the EFI image.
+  @param  SystemTable   A pointer to the EFI System Table.
+
+  @retval EFI_SUCCESS   The constructor always returns EFI_SUCCESS.
+
+**/
+EFI_STATUS
+EFIAPI
+PiSmmCoreMemoryAllocationLibConstructor (
+  IN EFI_HANDLE        ImageHandle,
+  IN EFI_SYSTEM_TABLE  *SystemTable
+  )
+{
+  SMM_CORE_PRIVATE_DATA  *SmmCorePrivate;
+
+  SmmCorePrivate = (SMM_CORE_PRIVATE_DATA *)ImageHandle;
+  //
+  // Initialize memory service using free SMRAM
+  //
+  SmmInitializeMemoryServices (SmmCorePrivate->SmramRangeCount, 
SmmCorePrivate->SmramRanges);
+  return EFI_SUCCESS;
+}

Modified: 
branches/UDK2014.SP1/MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryAllocationLib.inf
===================================================================
--- 
branches/UDK2014.SP1/MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryAllocationLib.inf
     2015-02-06 06:36:27 UTC (rev 16792)
+++ 
branches/UDK2014.SP1/MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryAllocationLib.inf
     2015-02-06 06:37:07 UTC (rev 16793)
@@ -4,7 +4,7 @@
 # for memory allocation instead of using SMM System Table servces in an 
indirect way.
 # It is assumed that this library instance must be linked with SMM Cre in this 
package. 
 #
-# Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
 #
 #  This program and the accompanying materials
 #  are licensed and made available under the terms and conditions of the BSD 
License
@@ -24,7 +24,8 @@
   VERSION_STRING                 = 1.0
   PI_SPECIFICATION_VERSION       = 0x0001000A
   LIBRARY_CLASS                  = MemoryAllocationLib|SMM_CORE
-  
+  CONSTRUCTOR                    = PiSmmCoreMemoryAllocationLibConstructor
+
 #
 # The following information is for reference only and not required by the 
build tools.
 #

Modified: 
branches/UDK2014.SP1/MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryAllocationServices.h
===================================================================
--- 
branches/UDK2014.SP1/MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryAllocationServices.h
  2015-02-06 06:36:27 UTC (rev 16792)
+++ 
branches/UDK2014.SP1/MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryAllocationServices.h
  2015-02-06 06:37:07 UTC (rev 16793)
@@ -4,7 +4,7 @@
   This header file borrows the PiSmmCore Memory Allocation services as the 
primitive
   for memory allocation. 
 
-  Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials                          
   are licensed and made available under the terms and conditions of the BSD 
License         
   which accompanies this distribution.  The full text of the license may be 
found at        
@@ -18,7 +18,40 @@
 #ifndef _PI_SMM_CORE_MEMORY_ALLOCATION_SERVICES_H_
 #define _PI_SMM_CORE_MEMORY_ALLOCATION_SERVICES_H_
 
+typedef struct {
+  UINTN                           Signature;
+  ///
+  /// The ImageHandle passed into the entry point of the SMM IPL.  This 
ImageHandle
+  /// is used by the SMM Core to fill in the ParentImageHandle field of the 
Loaded
+  /// Image Protocol for each SMM Driver that is dispatched by the SMM Core.
+  ///
+  EFI_HANDLE                      SmmIplImageHandle;
+  ///
+  /// The number of SMRAM ranges passed from the SMM IPL to the SMM Core.  The 
SMM
+  /// Core uses these ranges of SMRAM to initialize the SMM Core memory 
manager.
+  ///
+  UINTN                           SmramRangeCount;
+  ///
+  /// A table of SMRAM ranges passed from the SMM IPL to the SMM Core.  The SMM
+  /// Core uses these ranges of SMRAM to initialize the SMM Core memory 
manager.
+  ///
+  EFI_SMRAM_DESCRIPTOR            *SmramRanges;
+} SMM_CORE_PRIVATE_DATA;
+
 /**
+  Called to initialize the memory service.
+
+  @param   SmramRangeCount       Number of SMRAM Regions
+  @param   SmramRanges           Pointer to SMRAM Descriptors
+
+**/
+VOID
+SmmInitializeMemoryServices (
+  IN UINTN                 SmramRangeCount,
+  IN EFI_SMRAM_DESCRIPTOR  *SmramRanges
+  );
+
+/**
   Allocates pages from the memory map.
 
   @param  Type                   The type of allocation to perform

Modified: 
branches/UDK2014.SP1/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c
===================================================================
--- 
branches/UDK2014.SP1/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c
     2015-02-06 06:36:27 UTC (rev 16792)
+++ 
branches/UDK2014.SP1/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c
     2015-02-06 06:37:07 UTC (rev 16793)
@@ -16,7 +16,7 @@
 
  SmmPerformanceHandlerEx(), SmmPerformanceHandler() will receive untrusted 
input and do basic validation.
 
-Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD 
License
 which accompanies this distribution.  The full text of the license may be 
found at
@@ -51,9 +51,6 @@
 
 SPIN_LOCK               mSmmPerfLock;
 
-EFI_SMRAM_DESCRIPTOR    *mSmramRanges;
-UINTN                   mSmramRangeCount;
-
 //
 // Interfaces for SMM Performance Protocol.
 //
@@ -451,60 +448,6 @@
 }
 
 /**
-  This function check if the address is in SMRAM.
-
-  @param Buffer  the buffer address to be checked.
-  @param Length  the buffer length to be checked.
-
-  @retval TRUE  this address is in SMRAM.
-  @retval FALSE this address is NOT in SMRAM.
-**/
-BOOLEAN
-IsAddressInSmram (
-  IN EFI_PHYSICAL_ADDRESS  Buffer,
-  IN UINT64                Length
-  )
-{
-  UINTN  Index;
-
-  for (Index = 0; Index < mSmramRangeCount; Index ++) {
-    if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < 
mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||
-        ((mSmramRanges[Index].CpuStart >= Buffer) && 
(mSmramRanges[Index].CpuStart < Buffer + Length))) {
-      return TRUE;
-    }
-  }
-
-  return FALSE;
-}
-
-/**
-  This function check if the address refered by Buffer and Length is valid.
-
-  @param Buffer  the buffer address to be checked.
-  @param Length  the buffer length to be checked.
-
-  @retval TRUE  this address is valid.
-  @retval FALSE this address is NOT valid.
-**/
-BOOLEAN
-IsAddressValid (
-  IN UINTN                 Buffer,
-  IN UINTN                 Length
-  )
-{
-  if (Buffer > (MAX_ADDRESS - Length)) {
-    //
-    // Overflow happen
-    //
-    return FALSE;
-  }
-  if (IsAddressInSmram ((EFI_PHYSICAL_ADDRESS)Buffer, (UINT64)Length)) {
-    return FALSE;
-  }
-  return TRUE;
-}
-
-/**
   Communication service SMI Handler entry.
 
   This SMI handler provides services for the performance wrapper driver.
@@ -560,7 +503,7 @@
     return EFI_SUCCESS;
   }
 
-  if (!IsAddressValid ((UINTN)CommBuffer, TempCommBufferSize)) {
+  if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
     DEBUG ((EFI_D_ERROR, "SmmPerformanceHandlerEx: SMM communcation data 
buffer in SMRAM or overflow!\n"));
     return EFI_SUCCESS;
   }
@@ -587,7 +530,7 @@
        // Sanity check
        //
        DataSize = NumberOfEntries * sizeof(GAUGE_DATA_ENTRY_EX);
-       if (!IsAddressValid ((UINTN)GaugeDataEx, DataSize)) {
+       if (!SmmIsBufferOutsideSmmValid ((UINTN)GaugeDataEx, DataSize)) {
          DEBUG ((EFI_D_ERROR, "SmmPerformanceHandlerEx: SMM Performance Data 
buffer in SMRAM or overflow!\n"));
          Status = EFI_ACCESS_DENIED;
          break;
@@ -669,7 +612,7 @@
     return EFI_SUCCESS;
   }
 
-  if (!IsAddressValid ((UINTN)CommBuffer, TempCommBufferSize)) {
+  if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
     DEBUG ((EFI_D_ERROR, "SmmPerformanceHandler: SMM communcation data buffer 
in SMRAM or overflow!\n"));
     return EFI_SUCCESS;
   }
@@ -696,7 +639,7 @@
        // Sanity check
        //
        DataSize = NumberOfEntries * sizeof(GAUGE_DATA_ENTRY);
-       if (!IsAddressValid ((UINTN)GaugeData, DataSize)) {
+       if (!SmmIsBufferOutsideSmmValid ((UINTN)GaugeData, DataSize)) {
          DEBUG ((EFI_D_ERROR, "SmmPerformanceHandler: SMM Performance Data 
buffer in SMRAM or overflow!\n"));
          Status = EFI_ACCESS_DENIED;
          break;
@@ -741,10 +684,7 @@
 {
   EFI_STATUS                Status;
   EFI_HANDLE                Handle;
-  EFI_SMM_ACCESS2_PROTOCOL  *SmmAccess;
-  UINTN                     Size;
 
-
   //
   // Initialize spin lock
   //
@@ -756,28 +696,6 @@
   ASSERT (mGaugeData != NULL);
   
   //
-  // Get SMRAM information
-  //
-  Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID 
**)&SmmAccess);
-  ASSERT_EFI_ERROR (Status);
-
-  Size = 0;
-  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);
-  ASSERT (Status == EFI_BUFFER_TOO_SMALL);
-
-  Status = gSmst->SmmAllocatePool (
-                    EfiRuntimeServicesData,
-                    Size,
-                    (VOID **)&mSmramRanges
-                    );
-  ASSERT_EFI_ERROR (Status);
-
-  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);
-  ASSERT_EFI_ERROR (Status);
-
-  mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
-
-  //
   // Install the protocol interfaces.
   //
   Status = gSmst->SmmInstallProtocolInterface (

Modified: 
branches/UDK2014.SP1/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.inf
===================================================================
--- 
branches/UDK2014.SP1/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.inf
   2015-02-06 06:36:27 UTC (rev 16792)
+++ 
branches/UDK2014.SP1/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.inf
   2015-02-06 06:37:07 UTC (rev 16793)
@@ -8,7 +8,7 @@
 #  This library is mainly used by SMM Core to start performance logging to 
ensure that
 #  SMM Performance and PerformanceEx Protocol are installed at the very 
beginning of SMM phase.
 #  
-#  Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
 #  This program and the accompanying materials
 #  are licensed and made available under the terms and conditions of the BSD 
License
 #  which accompanies this distribution.  The full text of the license may be 
found at
@@ -56,6 +56,7 @@
   DebugLib
   SynchronizationLib
   SmmServicesTableLib
+  SmmMemLib
 
 [Protocols]
   gEfiSmmBase2ProtocolGuid                  ## CONSUMES

Modified: 
branches/UDK2014.SP1/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLibInternal.h
===================================================================
--- 
branches/UDK2014.SP1/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLibInternal.h
     2015-02-06 06:36:27 UTC (rev 16792)
+++ 
branches/UDK2014.SP1/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLibInternal.h
     2015-02-06 06:37:07 UTC (rev 16793)
@@ -4,7 +4,7 @@
   This header file holds the prototypes of the SMM Performance and 
PerformanceEx Protocol published by this
   library instance at its constructor.
 
-Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
 This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD 
License
 which accompanies this distribution.  The full text of the license may be 
found at
@@ -32,6 +32,7 @@
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/MemoryAllocationLib.h>
 #include <Library/SynchronizationLib.h>
+#include <Library/SmmMemLib.h>
 
 #include <Protocol/SmmBase2.h>
 #include <Protocol/SmmAccess2.h>

Modified: branches/UDK2014.SP1/MdeModulePkg/MdeModulePkg.dsc
===================================================================
--- branches/UDK2014.SP1/MdeModulePkg/MdeModulePkg.dsc  2015-02-06 06:36:27 UTC 
(rev 16792)
+++ branches/UDK2014.SP1/MdeModulePkg/MdeModulePkg.dsc  2015-02-06 06:37:07 UTC 
(rev 16793)
@@ -1,7 +1,7 @@
 ## @file
 # EFI/PI Reference Module Package for All Architectures
 #
-# Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>
 #
 #    This program and the accompanying materials
 #    are licensed and made available under the terms and conditions of the BSD 
License
@@ -127,6 +127,7 @@
   
MemoryAllocationLib|MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryAllocationLib.inf
   
SmmServicesTableLib|MdeModulePkg/Library/PiSmmCoreSmmServicesTableLib/PiSmmCoreSmmServicesTableLib.inf
   
SmmCorePlatformHookLib|MdeModulePkg/Library/SmmCorePlatformHookLibNull/SmmCorePlatformHookLibNull.inf
  
+  SmmMemLib|MdePkg/Library/SmmMemLib/SmmMemLib.inf
 
 [LibraryClasses.common.DXE_SMM_DRIVER]
   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf
@@ -134,6 +135,7 @@
   
MemoryAllocationLib|MdePkg/Library/SmmMemoryAllocationLib/SmmMemoryAllocationLib.inf
   
SmmServicesTableLib|MdePkg/Library/SmmServicesTableLib/SmmServicesTableLib.inf
   LockBoxLib|MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.inf
+  SmmMemLib|MdePkg/Library/SmmMemLib/SmmMemLib.inf
 
 [LibraryClasses.common.UEFI_DRIVER]
   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf

Modified: 
branches/UDK2014.SP1/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.c
===================================================================
--- 
branches/UDK2014.SP1/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.c
   2015-02-06 06:36:27 UTC (rev 16792)
+++ 
branches/UDK2014.SP1/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.c
   2015-02-06 06:37:07 UTC (rev 16793)
@@ -11,7 +11,7 @@
 
   FpdtSmiHandler() will receive untrusted input and do basic validation.
 
-  Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD 
License
   which accompanies this distribution.  The full text of the license may be 
found at
@@ -39,6 +39,7 @@
 #include <Library/MemoryAllocationLib.h>
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/SynchronizationLib.h>
+#include <Library/SmmMemLib.h>
 
 #define EXTENSION_RECORD_SIZE     0x1000
 
@@ -49,8 +50,6 @@
 UINT32                        mBootRecordMaxSize = 0;
 UINT8                         *mBootRecordBuffer = NULL;
 
-EFI_SMRAM_DESCRIPTOR          *mSmramRanges;
-UINTN                         mSmramRangeCount;
 SPIN_LOCK                     mSmmFpdtLock;
 BOOLEAN                       mSmramIsOutOfResource = FALSE;
 
@@ -178,60 +177,6 @@
 }
 
 /**
-  This function check if the address is in SMRAM.
-
-  @param Buffer  the buffer address to be checked.
-  @param Length  the buffer length to be checked.
-
-  @retval TRUE  this address is in SMRAM.
-  @retval FALSE this address is NOT in SMRAM.
-**/
-BOOLEAN
-InternalIsAddressInSmram (
-  IN EFI_PHYSICAL_ADDRESS  Buffer,
-  IN UINT64                Length
-  )
-{
-  UINTN  Index;
-
-  for (Index = 0; Index < mSmramRangeCount; Index ++) {
-    if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < 
mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||
-        ((mSmramRanges[Index].CpuStart >= Buffer) && 
(mSmramRanges[Index].CpuStart < Buffer + Length))) {
-      return TRUE;
-    }
-  }
-
-  return FALSE;
-}
-
-/**
-  This function check if the address refered by Buffer and Length is valid.
-
-  @param Buffer  the buffer address to be checked.
-  @param Length  the buffer length to be checked.
-
-  @retval TRUE  this address is valid.
-  @retval FALSE this address is NOT valid.
-**/
-BOOLEAN
-InternalIsAddressValid (
-  IN UINTN                 Buffer,
-  IN UINTN                 Length
-  )
-{
-  if (Buffer > (MAX_ADDRESS - Length)) {
-    //
-    // Overflow happen
-    //
-    return FALSE;
-  }
-  if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)Buffer, (UINT64)Length)) 
{
-    return FALSE;
-  }
-  return TRUE;
-}
-
-/**
   Communication service SMI Handler entry.
 
   This SMI handler provides services for report SMM boot records. 
@@ -283,7 +228,7 @@
     return EFI_SUCCESS;
   }
   
-  if (!InternalIsAddressValid ((UINTN)CommBuffer, TempCommBufferSize)) {
+  if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
     DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM communication data buffer in 
SMRAM or overflow!\n"));
     return EFI_SUCCESS;
   }
@@ -309,7 +254,7 @@
        // Sanity check
        //
        SmmCommData->BootRecordSize = mBootRecordSize;
-       if (!InternalIsAddressValid ((UINTN)BootRecordData, mBootRecordSize)) {
+       if (!SmmIsBufferOutsideSmmValid ((UINTN)BootRecordData, 
mBootRecordSize)) {
          DEBUG ((EFI_D_ERROR, "FpdtSmiHandler: SMM Data buffer in SMRAM or 
overflow!\n"));
          Status = EFI_ACCESS_DENIED;
          break;
@@ -350,8 +295,6 @@
 {
   EFI_STATUS                Status;
   EFI_HANDLE                Handle;
-  EFI_SMM_ACCESS2_PROTOCOL  *SmmAccess;
-  UINTN                     Size;
 
   //
   // Initialize spin lock
@@ -375,28 +318,6 @@
   ASSERT_EFI_ERROR (Status);
 
   //
-  // Get SMRAM information
-  //
-  Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID 
**)&SmmAccess);
-  ASSERT_EFI_ERROR (Status);
-
-  Size = 0;
-  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);
-  ASSERT (Status == EFI_BUFFER_TOO_SMALL);
-
-  Status = gSmst->SmmAllocatePool (
-                    EfiRuntimeServicesData,
-                    Size,
-                    (VOID **)&mSmramRanges
-                    );
-  ASSERT_EFI_ERROR (Status);
-
-  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);
-  ASSERT_EFI_ERROR (Status);
-
-  mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
-
-  //
   // Register SMI handler.
   //
   Handle = NULL;

Modified: 
branches/UDK2014.SP1/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.inf
===================================================================
--- 
branches/UDK2014.SP1/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.inf
 2015-02-06 06:36:27 UTC (rev 16792)
+++ 
branches/UDK2014.SP1/MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceSmm.inf
 2015-02-06 06:37:07 UTC (rev 16793)
@@ -4,7 +4,7 @@
 #  This module registers report status code listener to collect performance 
data
 #  for SMM boot performance records and S3 Suspend Performance Record.
 #
-#  Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>
 #  This program and the accompanying materials
 #  are licensed and made available under the terms and conditions of the BSD 
License
 #  which accompanies this distribution.  The full text of the license may be 
found at
@@ -50,6 +50,7 @@
   MemoryAllocationLib
   UefiBootServicesTableLib
   SynchronizationLib
+  SmmMemLib
 
 [Protocols]
   gEfiSmmRscHandlerProtocolGuid                 ## CONSUMES

Modified: 
branches/UDK2014.SP1/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
===================================================================
--- 
branches/UDK2014.SP1/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
   2015-02-06 06:36:27 UTC (rev 16792)
+++ 
branches/UDK2014.SP1/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.c
   2015-02-06 06:37:07 UTC (rev 16793)
@@ -43,7 +43,7 @@
   Caution: This module requires additional review when modified.
   This driver need to make sure the CommBuffer is not in the SMRAM range. 
 
-Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
 This program and the accompanying materials                          
 are licensed and made available under the terms and conditions of the BSD 
License         
 which accompanies this distribution.  The full text of the license may be 
found at        
@@ -56,6 +56,7 @@
 
 #include <PiSmm.h>
 #include <Library/SmmServicesTableLib.h>
+#include <Library/SmmMemLib.h>
 #include <Protocol/SmmSwapAddressRange.h>
 #include "FaultTolerantWrite.h"
 #include "FaultTolerantWriteSmmCommon.h"
@@ -64,8 +65,6 @@
 
 EFI_EVENT                                 mFvbRegistration = NULL;
 EFI_FTW_DEVICE                            *mFtwDevice      = NULL;
-EFI_SMRAM_DESCRIPTOR                      *mSmramRanges;
-UINTN                                     mSmramRangeCount;
 
 ///
 /// The flag to indicate whether the platform has left the DXE phase of 
execution.
@@ -73,60 +72,6 @@
 BOOLEAN                                   mEndOfDxe = FALSE;
 
 /**
-  This function check if the address is in SMRAM.
-
-  @param Buffer  the buffer address to be checked.
-  @param Length  the buffer length to be checked.
-
-  @retval TRUE  this address is in SMRAM.
-  @retval FALSE this address is NOT in SMRAM.
-**/
-BOOLEAN
-InternalIsAddressInSmram (
-  IN EFI_PHYSICAL_ADDRESS  Buffer,
-  IN UINT64                Length
-  )
-{
-  UINTN  Index;
-
-  for (Index = 0; Index < mSmramRangeCount; Index ++) {
-    if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < 
mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||
-        ((mSmramRanges[Index].CpuStart >= Buffer) && 
(mSmramRanges[Index].CpuStart < Buffer + Length))) {
-      return TRUE;
-    }
-  }
-
-  return FALSE;
-}
-
-/**
-  This function check if the address refered by Buffer and Length is valid.
-
-  @param Buffer  the buffer address to be checked.
-  @param Length  the buffer length to be checked.
-
-  @retval TRUE  this address is valid.
-  @retval FALSE this address is NOT valid.
-**/
-BOOLEAN
-InternalIsAddressValid (
-  IN UINTN                 Buffer,
-  IN UINTN                 Length
-  )
-{
-  if (Buffer > (MAX_ADDRESS - Length)) {
-    //
-    // Overflow happen
-    //
-    return FALSE;
-  }
-  if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)Buffer, (UINT64)Length)) 
{
-    return FALSE;
-  }
-  return TRUE;
-}
-
-/**
   Retrive the SMM FVB protocol interface by HANDLE.
 
   @param[in]  FvBlockHandle     The handle of SMM FVB protocol that provides 
services for
@@ -391,7 +336,7 @@
   }
   CommBufferPayloadSize = TempCommBufferSize - SMM_FTW_COMMUNICATE_HEADER_SIZE;
 
-  if (!InternalIsAddressValid ((UINTN)CommBuffer, TempCommBufferSize)) {
+  if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
     DEBUG ((EFI_D_ERROR, "SmmFtwHandler: SMM communication buffer in SMRAM or 
overflow!\n"));
     return EFI_SUCCESS;
   }
@@ -670,8 +615,6 @@
   )
 {
   EFI_STATUS                              Status;
-  EFI_SMM_ACCESS2_PROTOCOL                *SmmAccess;
-  UINTN                                   Size;
   VOID                                    *SmmEndOfDxeRegistration;
 
   //
@@ -683,28 +626,6 @@
   }
 
   //
-  // Get SMRAM information
-  //
-  Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID 
**)&SmmAccess);
-  ASSERT_EFI_ERROR (Status);
-
-  Size = 0;
-  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);
-  ASSERT (Status == EFI_BUFFER_TOO_SMALL);
-
-  Status = gSmst->SmmAllocatePool (
-                    EfiRuntimeServicesData,
-                    Size,
-                    (VOID **)&mSmramRanges
-                    );
-  ASSERT_EFI_ERROR (Status);
-
-  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);
-  ASSERT_EFI_ERROR (Status);
-
-  mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
-
-  //
   // Register EFI_SMM_END_OF_DXE_PROTOCOL_GUID notify function.
   //
   Status = gSmst->SmmRegisterProtocolNotify (

Modified: 
branches/UDK2014.SP1/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf
===================================================================
--- 
branches/UDK2014.SP1/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf
 2015-02-06 06:36:27 UTC (rev 16792)
+++ 
branches/UDK2014.SP1/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf
 2015-02-06 06:37:07 UTC (rev 16793)
@@ -6,7 +6,7 @@
 #   depends on the full functionality SMM FVB protocol that support read, 
write/erase 
 #   flash access.
 #
-# Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
 #
 #  This program and the accompanying materials
 #  are licensed and made available under the terms and conditions of the BSD 
License
@@ -54,6 +54,7 @@
   UefiLib
   PcdLib
   ReportStatusCodeLib
+  SmmMemLib
 
 [Guids]
   #

Modified: 
branches/UDK2014.SP1/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c
===================================================================
--- branches/UDK2014.SP1/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c 
2015-02-06 06:36:27 UTC (rev 16792)
+++ branches/UDK2014.SP1/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c 
2015-02-06 06:37:07 UTC (rev 16793)
@@ -9,7 +9,7 @@
   SmmLockBoxHandler(), SmmLockBoxRestore(), SmmLockBoxUpdate(), 
SmmLockBoxSave()
   will receive untrusted input and do basic validation.
 
-Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
 
 This program and the accompanying materials
 are licensed and made available under the terms and conditions
@@ -30,7 +30,9 @@
 #include <Library/BaseLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/DebugLib.h>
+#include <Library/SmmMemLib.h>
 #include <Library/LockBoxLib.h>
+
 #include <Protocol/SmmReadyToLock.h>
 #include <Protocol/SmmCommunication.h>
 #include <Protocol/SmmAccess2.h>
@@ -39,64 +41,7 @@
 
 BOOLEAN              mLocked = FALSE;
 
-EFI_SMRAM_DESCRIPTOR *mSmramRanges;
-UINTN                mSmramRangeCount;
-
 /**
-  This function check if the address is in SMRAM.
-
-  @param Buffer  the buffer address to be checked.
-  @param Length  the buffer length to be checked.
-
-  @retval TRUE  this address is in SMRAM.
-  @retval FALSE this address is NOT in SMRAM.
-**/
-BOOLEAN
-IsAddressInSmram (
-  IN EFI_PHYSICAL_ADDRESS  Buffer,
-  IN UINT64                Length
-  )
-{
-  UINTN  Index;
-
-  for (Index = 0; Index < mSmramRangeCount; Index ++) {
-    if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < 
mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||
-        ((mSmramRanges[Index].CpuStart >= Buffer) && 
(mSmramRanges[Index].CpuStart < Buffer + Length))) {
-      return TRUE;
-    }
-  }
-
-  return FALSE;
-}
-
-/**
-  This function check if the address refered by Buffer and Length is valid.
-
-  @param Buffer  the buffer address to be checked.
-  @param Length  the buffer length to be checked.
-
-  @retval TRUE  this address is valid.
-  @retval FALSE this address is NOT valid.
-**/
-BOOLEAN
-IsAddressValid (
-  IN UINTN                 Buffer,
-  IN UINTN                 Length
-  )
-{
-  if (Buffer > (MAX_ADDRESS - Length)) {
-    //
-    // Overflow happen
-    //
-    return FALSE;
-  }
-  if (IsAddressInSmram ((EFI_PHYSICAL_ADDRESS)Buffer, (UINT64)Length)) {
-    return FALSE;
-  }
-  return TRUE;
-}
-
-/**
   Dispatch function for SMM lock box save.
 
   Caution: This function may receive untrusted input.
@@ -127,7 +72,7 @@
   //
   // Sanity check
   //
-  if (!IsAddressValid ((UINTN)TempLockBoxParameterSave.Buffer, 
(UINTN)TempLockBoxParameterSave.Length)) {
+  if (!SmmIsBufferOutsideSmmValid ((UINTN)TempLockBoxParameterSave.Buffer, 
(UINTN)TempLockBoxParameterSave.Length)) {
     DEBUG ((EFI_D_ERROR, "SmmLockBox Save address in SMRAM or buffer 
overflow!\n"));
     LockBoxParameterSave->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
     return ;
@@ -211,7 +156,7 @@
   //
   // Sanity check
   //
-  if (!IsAddressValid ((UINTN)TempLockBoxParameterUpdate.Buffer, 
(UINTN)TempLockBoxParameterUpdate.Length)) {
+  if (!SmmIsBufferOutsideSmmValid ((UINTN)TempLockBoxParameterUpdate.Buffer, 
(UINTN)TempLockBoxParameterUpdate.Length)) {
     DEBUG ((EFI_D_ERROR, "SmmLockBox Update address in SMRAM or buffer 
overflow!\n"));
     LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
     return ;
@@ -252,7 +197,7 @@
   //
   // Sanity check
   //
-  if (!IsAddressValid ((UINTN)TempLockBoxParameterRestore.Buffer, 
(UINTN)TempLockBoxParameterRestore.Length)) {
+  if (!SmmIsBufferOutsideSmmValid ((UINTN)TempLockBoxParameterRestore.Buffer, 
(UINTN)TempLockBoxParameterRestore.Length)) {
     DEBUG ((EFI_D_ERROR, "SmmLockBox Restore address in SMRAM or buffer 
overflow!\n"));
     LockBoxParameterRestore->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
     return ;
@@ -341,7 +286,7 @@
     DEBUG ((EFI_D_ERROR, "SmmLockBox Command Buffer Size invalid!\n"));
     return EFI_SUCCESS;
   }
-  if (!IsAddressValid ((UINTN)CommBuffer, TempCommBufferSize)) {
+  if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
     DEBUG ((EFI_D_ERROR, "SmmLockBox Command Buffer in SMRAM or overflow!\n"));
     return EFI_SUCCESS;
   }
@@ -444,32 +389,8 @@
   EFI_STATUS                    Status;
   EFI_HANDLE                    DispatchHandle;
   VOID                          *Registration;
-  EFI_SMM_ACCESS2_PROTOCOL      *SmmAccess;
-  UINTN                         Size;
 
   //
-  // Get SMRAM information
-  //
-  Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID 
**)&SmmAccess);
-  ASSERT_EFI_ERROR (Status);
-
-  Size = 0;
-  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);
-  ASSERT (Status == EFI_BUFFER_TOO_SMALL);
-
-  Status = gSmst->SmmAllocatePool (
-                    EfiRuntimeServicesData,
-                    Size,
-                    (VOID **)&mSmramRanges
-                    );
-  ASSERT_EFI_ERROR (Status);
-
-  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);
-  ASSERT_EFI_ERROR (Status);
-
-  mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
-
-  //
   // Register LockBox communication handler
   //
   Status = gSmst->SmiHandlerRegister (

Modified: 
branches/UDK2014.SP1/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
===================================================================
--- 
branches/UDK2014.SP1/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf   
    2015-02-06 06:36:27 UTC (rev 16792)
+++ 
branches/UDK2014.SP1/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf   
    2015-02-06 06:37:07 UTC (rev 16793)
@@ -6,7 +6,7 @@
 #  This external input must be validated carefully to avoid security issue like
 #  buffer overflow, integer overflow.
 #
-#  Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
 #
 #  This program and the accompanying materials
 #  are licensed and made available under the terms and conditions
@@ -50,6 +50,7 @@
   BaseMemoryLib
   DebugLib
   LockBoxLib
+  SmmMemLib
 
 [Guids]
   gEfiSmmLockBoxCommunicationGuid   ## PRODUCES ## GUID # SmiHandlerRegister

Modified: 
branches/UDK2014.SP1/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c
===================================================================
--- 
branches/UDK2014.SP1/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c   
    2015-02-06 06:36:27 UTC (rev 16792)
+++ 
branches/UDK2014.SP1/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c   
    2015-02-06 06:37:07 UTC (rev 16793)
@@ -32,14 +32,12 @@
 #include <Protocol/SmmEndOfDxe.h>
 
 #include <Library/SmmServicesTableLib.h>
+#include <Library/SmmMemLib.h>
 
 #include <Guid/VariableFormat.h>
 #include <Guid/SmmVariableCommon.h>
 #include "Variable.h"
 
-EFI_SMRAM_DESCRIPTOR                                 *mSmramRanges;
-UINTN                                                mSmramRangeCount;
-
 extern VARIABLE_INFO_ENTRY                           *gVariableInfo;
 EFI_HANDLE                                           mSmmVariableHandle      = 
NULL;
 EFI_HANDLE                                           mVariableHandle         = 
NULL;
@@ -116,60 +114,6 @@
 }
 
 /**
-  This function check if the address is in SMRAM.
-
-  @param Buffer  the buffer address to be checked.
-  @param Length  the buffer length to be checked.
-
-  @retval TRUE  this address is in SMRAM.
-  @retval FALSE this address is NOT in SMRAM.
-**/
-BOOLEAN
-InternalIsAddressInSmram (
-  IN EFI_PHYSICAL_ADDRESS  Buffer,
-  IN UINT64                Length
-  )
-{
-  UINTN  Index;
-
-  for (Index = 0; Index < mSmramRangeCount; Index ++) {
-    if (((Buffer >= mSmramRanges[Index].CpuStart) && (Buffer < 
mSmramRanges[Index].CpuStart + mSmramRanges[Index].PhysicalSize)) ||
-        ((mSmramRanges[Index].CpuStart >= Buffer) && 
(mSmramRanges[Index].CpuStart < Buffer + Length))) {
-      return TRUE;
-    }
-  }
-
-  return FALSE;
-}
-
-/**
-  This function check if the address refered by Buffer and Length is valid.
-
-  @param Buffer  the buffer address to be checked.
-  @param Length  the buffer length to be checked.
-
-  @retval TRUE  this address is valid.
-  @retval FALSE this address is NOT valid.
-**/
-BOOLEAN
-InternalIsAddressValid (
-  IN UINTN                 Buffer,
-  IN UINTN                 Length
-  )
-{
-  if (Buffer > (MAX_ADDRESS - Length)) {
-    //
-    // Overflow happen
-    //
-    return FALSE;
-  }
-  if (InternalIsAddressInSmram ((EFI_PHYSICAL_ADDRESS)Buffer, (UINT64)Length)) 
{
-    return FALSE;
-  }
-  return TRUE;
-}
-
-/**
   Initializes a basic mutual exclusion lock.
 
   This function initializes a basic mutual exclusion lock to the released 
state 
@@ -522,7 +466,7 @@
     return EFI_SUCCESS;
   }
 
-  if (!InternalIsAddressValid ((UINTN)CommBuffer, TempCommBufferSize)) {
+  if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
     DEBUG ((EFI_D_ERROR, "SmmVariableHandler: SMM communication buffer in 
SMRAM or overflow!\n"));
     return EFI_SUCCESS;
   }
@@ -709,7 +653,7 @@
       // It is covered by previous CommBuffer check 
       //
      
-      if (InternalIsAddressInSmram 
((EFI_PHYSICAL_ADDRESS)(UINTN)CommBufferSize, sizeof(UINTN))) {
+      if (!SmmIsBufferOutsideSmmValid 
((EFI_PHYSICAL_ADDRESS)(UINTN)CommBufferSize, sizeof(UINTN))) {
         DEBUG ((EFI_D_ERROR, "GetStatistics: SMM communication buffer in 
SMRAM!\n"));
         Status = EFI_ACCESS_DENIED;
         goto EXIT;
@@ -868,8 +812,6 @@
   EFI_STATUS                              Status;
   EFI_HANDLE                              VariableHandle;
   VOID                                    *SmmFtwRegistration;
-  EFI_SMM_ACCESS2_PROTOCOL                *SmmAccess;
-  UINTN                                   Size;
   VOID                                    *SmmEndOfDxeRegistration;
 
   //
@@ -890,28 +832,6 @@
                     );
   ASSERT_EFI_ERROR (Status);
 
-  //
-  // Get SMRAM information
-  //
-  Status = gBS->LocateProtocol (&gEfiSmmAccess2ProtocolGuid, NULL, (VOID 
**)&SmmAccess);
-  ASSERT_EFI_ERROR (Status);
-
-  Size = 0;
-  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, NULL);
-  ASSERT (Status == EFI_BUFFER_TOO_SMALL);
-
-  Status = gSmst->SmmAllocatePool (
-                    EfiRuntimeServicesData,
-                    Size,
-                    (VOID **)&mSmramRanges
-                    );
-  ASSERT_EFI_ERROR (Status);
-
-  Status = SmmAccess->GetCapabilities (SmmAccess, &Size, mSmramRanges);
-  ASSERT_EFI_ERROR (Status);
-
-  mSmramRangeCount = Size / sizeof (EFI_SMRAM_DESCRIPTOR);
-
   mVariableBufferPayloadSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 
(PcdMaxHardwareErrorVariableSize)) +
                                OFFSET_OF 
(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Name) - sizeof (VARIABLE_HEADER);
 

Modified: 
branches/UDK2014.SP1/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf
===================================================================
--- 
branches/UDK2014.SP1/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf 
    2015-02-06 06:36:27 UTC (rev 16792)
+++ 
branches/UDK2014.SP1/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf 
    2015-02-06 06:37:07 UTC (rev 16793)
@@ -62,6 +62,7 @@
   DxeServicesTableLib
   HobLib
   PcdLib
+  SmmMemLib
 
 [Protocols]
   gEfiSmmFirmwareVolumeBlockProtocolGuid        ## CONSUMES


------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
edk2-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/edk2-commits

Reply via email to