Re: [edk2] [PATCH] MdeModulePkg SmmLockBoxDxeLib: Get SmmCommRegion for COMM buffer

2017-07-25 Thread Yao, Jiewen
Reviewed-by: jiewen@intel.com

> -Original Message-
> From: Zeng, Star
> Sent: Friday, July 21, 2017 2:40 PM
> To: edk2-devel@lists.01.org
> Cc: Zeng, Star ; Yao, Jiewen ;
> Baraneedharan Anbazhagan 
> Subject: [PATCH] MdeModulePkg SmmLockBoxDxeLib: Get SmmCommRegion
> for COMM buffer
> 
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=593
> 
> Currently, SmmCommunciate fails in RestoreLockBox after
> SmmReadyToLock since COMM buffer is in stack instead of
> using SmmCommRegion by gEdkiiPiSmmCommunicationRegionTableGuid.
> 
> This patch is to get SmmCommRegion by
> gEdkiiPiSmmCommunicationRegionTableGuid for COMM buffer
> 
> Cc: Jiewen Yao 
> Cc: Baraneedharan Anbazhagan 
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Star Zeng 
> ---
>  .../Library/SmmLockBoxLib/SmmLockBoxDxeLib.c   | 199
> +++--
>  .../Library/SmmLockBoxLib/SmmLockBoxDxeLib.inf |   6 +-
>  2 files changed, 147 insertions(+), 58 deletions(-)
> 
> diff --git a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.c
> b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.c
> index 9659f014e937..b75f81e69e04 100644
> --- a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.c
> +++ b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.c
> @@ -1,6 +1,6 @@
>  /** @file
> 
> -Copyright (c) 2010, Intel Corporation. All rights reserved.
> +Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
> 
>  This program and the accompanying materials
>  are licensed and made available under the terms and conditions
> @@ -20,11 +20,108 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY
> KIND, EITHER EXPRESS OR IMPLIED.
>  #include 
>  #include 
>  #include 
> +#include 
>  #include 
>  #include 
> +#include 
> 
>  #include "SmmLockBoxLibPrivate.h"
> 
> +EFI_SMM_COMMUNICATION_PROTOCOL  *mLockBoxSmmCommProtocol =
> NULL;
> +UINT8   *mLockBoxSmmCommBuffer   = NULL;
> +
> +/**
> +  Get smm communication protocol for lockbox.
> +
> +  @return Pointer to smm communication protocol, NULL if not found.
> +
> +**/
> +EFI_SMM_COMMUNICATION_PROTOCOL *
> +LockBoxGetSmmCommProtocol (
> +  VOID
> +  )
> +{
> +  EFI_STATUSStatus;
> +
> +  //
> +  // If the protocol has been got previously, return it.
> +  //
> +  if (mLockBoxSmmCommProtocol != NULL) {
> +return mLockBoxSmmCommProtocol;
> +  }
> +
> +  Status = gBS->LocateProtocol (
> +  ,
> +  NULL,
> +  (VOID **)
> +  );
> +  if (EFI_ERROR (Status)) {
> +mLockBoxSmmCommProtocol = NULL;
> +  }
> +  return mLockBoxSmmCommProtocol;
> +}
> +
> +/**
> +  Get smm communication buffer for lockbox.
> +
> +  @return Pointer to smm communication buffer, NULL if not found.
> +
> +**/
> +UINT8 *
> +LockBoxGetSmmCommBuffer (
> +  VOID
> +  )
> +{
> +  EFI_STATUSStatus;
> +  UINTN MinimalSizeNeeded;
> +  EDKII_PI_SMM_COMMUNICATION_REGION_TABLE
> *PiSmmCommunicationRegionTable;
> +  UINT32Index;
> +  EFI_MEMORY_DESCRIPTOR *Entry;
> +  UINTN Size;
> +
> +  //
> +  // If the buffer has been got previously, return it.
> +  //
> +  if (mLockBoxSmmCommBuffer != NULL) {
> +return mLockBoxSmmCommBuffer;
> +  }
> +
> +  MinimalSizeNeeded = sizeof (EFI_GUID) +
> +  sizeof (UINTN) +
> +  MAX (sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_SAVE),
> +   MAX (sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES),
> +MAX (sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE),
> + MAX (sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE),
> +  sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE);
> +
> +  Status = EfiGetSystemConfigurationTable (
> + ,
> + (VOID **) 
> + );
> +  if (EFI_ERROR (Status)) {
> +mLockBoxSmmCommBuffer = NULL;
> +return mLockBoxSmmCommBuffer;
> +  }
> +  ASSERT (PiSmmCommunicationRegionTable != NULL);
> +  Entry = (EFI_MEMORY_DESCRIPTOR *) (PiSmmCommunicationRegionTable +
> 1);
> +  Size = 0;
> +  for (Index = 0; Index < PiSmmCommunicationRegionTable->NumberOfEntries;
> Index++) {
> +if (Entry->Type == EfiConventionalMemory) {
> +  Size = EFI_PAGES_TO_SIZE ((UINTN) Entry->NumberOfPages);
> +  if (Size >= MinimalSizeNeeded) {
> +break;
> +  }
> +}
> +Entry = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) Entry +
> PiSmmCommunicationRegionTable->DescriptorSize);
> +  }
> +  if (Index >= PiSmmCommunicationRegionTable->NumberOfEntries) {
> +mLockBoxSmmCommBuffer = NULL;
> +  } else {
> +mLockBoxSmmCommBuffer = (UINT8 *) (UINTN) 

[edk2] [PATCH] MdeModulePkg SmmLockBoxDxeLib: Get SmmCommRegion for COMM buffer

2017-07-21 Thread Star Zeng
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=593

Currently, SmmCommunciate fails in RestoreLockBox after
SmmReadyToLock since COMM buffer is in stack instead of
using SmmCommRegion by gEdkiiPiSmmCommunicationRegionTableGuid.

This patch is to get SmmCommRegion by
gEdkiiPiSmmCommunicationRegionTableGuid for COMM buffer

Cc: Jiewen Yao 
Cc: Baraneedharan Anbazhagan 
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Star Zeng 
---
 .../Library/SmmLockBoxLib/SmmLockBoxDxeLib.c   | 199 +++--
 .../Library/SmmLockBoxLib/SmmLockBoxDxeLib.inf |   6 +-
 2 files changed, 147 insertions(+), 58 deletions(-)

diff --git a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.c 
b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.c
index 9659f014e937..b75f81e69e04 100644
--- a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.c
+++ b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.c
@@ -1,6 +1,6 @@
 /** @file
 
-Copyright (c) 2010, Intel Corporation. All rights reserved.
+Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
 
 This program and the accompanying materials
 are licensed and made available under the terms and conditions
@@ -20,11 +20,108 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER 
EXPRESS OR IMPLIED.
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
+#include 
 
 #include "SmmLockBoxLibPrivate.h"
 
+EFI_SMM_COMMUNICATION_PROTOCOL  *mLockBoxSmmCommProtocol = NULL;
+UINT8   *mLockBoxSmmCommBuffer   = NULL;
+
+/**
+  Get smm communication protocol for lockbox.
+
+  @return Pointer to smm communication protocol, NULL if not found.
+
+**/
+EFI_SMM_COMMUNICATION_PROTOCOL *
+LockBoxGetSmmCommProtocol (
+  VOID
+  )
+{
+  EFI_STATUSStatus;
+
+  //
+  // If the protocol has been got previously, return it.
+  //
+  if (mLockBoxSmmCommProtocol != NULL) {
+return mLockBoxSmmCommProtocol;
+  }
+
+  Status = gBS->LocateProtocol (
+  ,
+  NULL,
+  (VOID **)
+  );
+  if (EFI_ERROR (Status)) {
+mLockBoxSmmCommProtocol = NULL;
+  }
+  return mLockBoxSmmCommProtocol;
+}
+
+/**
+  Get smm communication buffer for lockbox.
+
+  @return Pointer to smm communication buffer, NULL if not found.
+
+**/
+UINT8 *
+LockBoxGetSmmCommBuffer (
+  VOID
+  )
+{
+  EFI_STATUSStatus;
+  UINTN MinimalSizeNeeded;
+  EDKII_PI_SMM_COMMUNICATION_REGION_TABLE   *PiSmmCommunicationRegionTable;
+  UINT32Index;
+  EFI_MEMORY_DESCRIPTOR *Entry;
+  UINTN Size;
+
+  //
+  // If the buffer has been got previously, return it.
+  //
+  if (mLockBoxSmmCommBuffer != NULL) {
+return mLockBoxSmmCommBuffer;
+  }
+
+  MinimalSizeNeeded = sizeof (EFI_GUID) +
+  sizeof (UINTN) +
+  MAX (sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SAVE),
+   MAX (sizeof 
(EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES),
+MAX (sizeof 
(EFI_SMM_LOCK_BOX_PARAMETER_UPDATE),
+ MAX (sizeof 
(EFI_SMM_LOCK_BOX_PARAMETER_RESTORE),
+  sizeof 
(EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE);
+
+  Status = EfiGetSystemConfigurationTable (
+ ,
+ (VOID **) 
+ );
+  if (EFI_ERROR (Status)) {
+mLockBoxSmmCommBuffer = NULL;
+return mLockBoxSmmCommBuffer;
+  }
+  ASSERT (PiSmmCommunicationRegionTable != NULL);
+  Entry = (EFI_MEMORY_DESCRIPTOR *) (PiSmmCommunicationRegionTable + 1);
+  Size = 0;
+  for (Index = 0; Index < PiSmmCommunicationRegionTable->NumberOfEntries; 
Index++) {
+if (Entry->Type == EfiConventionalMemory) {
+  Size = EFI_PAGES_TO_SIZE ((UINTN) Entry->NumberOfPages);
+  if (Size >= MinimalSizeNeeded) {
+break;
+  }
+}
+Entry = (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) Entry + 
PiSmmCommunicationRegionTable->DescriptorSize);
+  }
+  if (Index >= PiSmmCommunicationRegionTable->NumberOfEntries) {
+mLockBoxSmmCommBuffer = NULL;
+  } else {
+mLockBoxSmmCommBuffer = (UINT8 *) (UINTN) Entry->PhysicalStart;
+  }
+  return mLockBoxSmmCommBuffer;
+}
+
 /**
   This function will save confidential information to lockbox.
 
@@ -52,7 +149,8 @@ SaveLockBox (
   EFI_SMM_COMMUNICATION_PROTOCOL  *SmmCommunication;
   EFI_SMM_LOCK_BOX_PARAMETER_SAVE *LockBoxParameterSave;
   EFI_SMM_COMMUNICATE_HEADER  *CommHeader;
-  UINT8   CommBuffer[sizeof(EFI_GUID) + sizeof(UINTN) 
+ sizeof(EFI_SMM_LOCK_BOX_PARAMETER_SAVE)];
+  UINT8   TempCommBuffer[sizeof(EFI_GUID) + 
sizeof(UINTN) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_SAVE)];
+  UINT8   *CommBuffer;
   UINTN