From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id CB0F321CE7474 for ; Thu, 20 Jul 2017 23:38:37 -0700 (PDT) Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga105.fm.intel.com with ESMTP; 20 Jul 2017 23:40:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.40,388,1496127600"; d="scan'208";a="110360755" Received: from shwdeopenpsi068.ccr.corp.intel.com ([10.239.9.12]) by orsmga004.jf.intel.com with ESMTP; 20 Jul 2017 23:40:33 -0700 From: Star Zeng To: edk2-devel@lists.01.org Cc: Star Zeng , Jiewen Yao , Baraneedharan Anbazhagan Date: Fri, 21 Jul 2017 14:40:21 +0800 Message-Id: <1500619221-300708-1-git-send-email-star.zeng@intel.com> X-Mailer: git-send-email 2.7.0.windows.1 Subject: [PATCH] MdeModulePkg SmmLockBoxDxeLib: Get SmmCommRegion for COMM buffer X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Jul 2017 06:38:38 -0000 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_STATUS Status; + + // + // If the protocol has been got previously, return it. + // + if (mLockBoxSmmCommProtocol != NULL) { + return mLockBoxSmmCommProtocol; + } + + Status = gBS->LocateProtocol ( + &gEfiSmmCommunicationProtocolGuid, + NULL, + (VOID **)&mLockBoxSmmCommProtocol + ); + 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_STATUS Status; + UINTN MinimalSizeNeeded; + EDKII_PI_SMM_COMMUNICATION_REGION_TABLE *PiSmmCommunicationRegionTable; + UINT32 Index; + 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 ( + &gEdkiiPiSmmCommunicationRegionTableGuid, + (VOID **) &PiSmmCommunicationRegionTable + ); + 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 CommSize; DEBUG ((EFI_D_INFO, "SmmLockBoxDxeLib SaveLockBox - Enter\n")); @@ -64,21 +162,18 @@ SaveLockBox ( return EFI_INVALID_PARAMETER; } - // - // Get needed resource - // - Status = gBS->LocateProtocol ( - &gEfiSmmCommunicationProtocolGuid, - NULL, - (VOID **)&SmmCommunication - ); - if (EFI_ERROR (Status)) { + SmmCommunication = LockBoxGetSmmCommProtocol (); + if (SmmCommunication == NULL) { return EFI_NOT_STARTED; } // // Prepare parameter // + CommBuffer = LockBoxGetSmmCommBuffer (); + if (CommBuffer == NULL) { + CommBuffer = &TempCommBuffer[0]; + } CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0]; CopyMem (&CommHeader->HeaderGuid, &gEfiSmmLockBoxCommunicationGuid, sizeof(gEfiSmmLockBoxCommunicationGuid)); CommHeader->MessageLength = sizeof(*LockBoxParameterSave); @@ -94,7 +189,7 @@ SaveLockBox ( // // Send command // - CommSize = sizeof(CommBuffer); + CommSize = sizeof(EFI_GUID) + sizeof(UINTN) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_SAVE); Status = SmmCommunication->Communicate ( SmmCommunication, &CommBuffer[0], @@ -136,7 +231,8 @@ SetLockBoxAttributes ( EFI_SMM_COMMUNICATION_PROTOCOL *SmmCommunication; EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES *LockBoxParameterSetAttributes; EFI_SMM_COMMUNICATE_HEADER *CommHeader; - UINT8 CommBuffer[sizeof(EFI_GUID) + sizeof(UINTN) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES)]; + UINT8 TempCommBuffer[sizeof(EFI_GUID) + sizeof(UINTN) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES)]; + UINT8 *CommBuffer; UINTN CommSize; DEBUG ((EFI_D_INFO, "SmmLockBoxDxeLib SetLockBoxAttributes - Enter\n")); @@ -149,21 +245,18 @@ SetLockBoxAttributes ( return EFI_INVALID_PARAMETER; } - // - // Get needed resource - // - Status = gBS->LocateProtocol ( - &gEfiSmmCommunicationProtocolGuid, - NULL, - (VOID **)&SmmCommunication - ); - if (EFI_ERROR (Status)) { + SmmCommunication = LockBoxGetSmmCommProtocol (); + if (SmmCommunication == NULL) { return EFI_NOT_STARTED; } // // Prepare parameter // + CommBuffer = LockBoxGetSmmCommBuffer (); + if (CommBuffer == NULL) { + CommBuffer = &TempCommBuffer[0]; + } CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0]; CopyMem (&CommHeader->HeaderGuid, &gEfiSmmLockBoxCommunicationGuid, sizeof(gEfiSmmLockBoxCommunicationGuid)); CommHeader->MessageLength = sizeof(*LockBoxParameterSetAttributes); @@ -178,7 +271,7 @@ SetLockBoxAttributes ( // // Send command // - CommSize = sizeof(CommBuffer); + CommSize = sizeof(EFI_GUID) + sizeof(UINTN) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES); Status = SmmCommunication->Communicate ( SmmCommunication, &CommBuffer[0], @@ -225,7 +318,8 @@ UpdateLockBox ( EFI_SMM_COMMUNICATION_PROTOCOL *SmmCommunication; EFI_SMM_LOCK_BOX_PARAMETER_UPDATE *LockBoxParameterUpdate; EFI_SMM_COMMUNICATE_HEADER *CommHeader; - UINT8 CommBuffer[sizeof(EFI_GUID) + sizeof(UINTN) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_UPDATE)]; + UINT8 TempCommBuffer[sizeof(EFI_GUID) + sizeof(UINTN) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_UPDATE)]; + UINT8 *CommBuffer; UINTN CommSize; DEBUG ((EFI_D_INFO, "SmmLockBoxDxeLib UpdateLockBox - Enter\n")); @@ -237,21 +331,18 @@ UpdateLockBox ( return EFI_INVALID_PARAMETER; } - // - // Get needed resource - // - Status = gBS->LocateProtocol ( - &gEfiSmmCommunicationProtocolGuid, - NULL, - (VOID **)&SmmCommunication - ); - if (EFI_ERROR (Status)) { + SmmCommunication = LockBoxGetSmmCommProtocol (); + if (SmmCommunication == NULL) { return EFI_NOT_STARTED; } // // Prepare parameter // + CommBuffer = LockBoxGetSmmCommBuffer (); + if (CommBuffer == NULL) { + CommBuffer = &TempCommBuffer[0]; + } CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0]; CopyMem (&CommHeader->HeaderGuid, &gEfiSmmLockBoxCommunicationGuid, sizeof(gEfiSmmLockBoxCommunicationGuid)); CommHeader->MessageLength = sizeof(*LockBoxParameterUpdate); @@ -268,7 +359,7 @@ UpdateLockBox ( // // Send command // - CommSize = sizeof(CommBuffer); + CommSize = sizeof(EFI_GUID) + sizeof(UINTN) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_UPDATE); Status = SmmCommunication->Communicate ( SmmCommunication, &CommBuffer[0], @@ -316,7 +407,8 @@ RestoreLockBox ( EFI_SMM_COMMUNICATION_PROTOCOL *SmmCommunication; EFI_SMM_LOCK_BOX_PARAMETER_RESTORE *LockBoxParameterRestore; EFI_SMM_COMMUNICATE_HEADER *CommHeader; - UINT8 CommBuffer[sizeof(EFI_GUID) + sizeof(UINTN) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_RESTORE)]; + UINT8 TempCommBuffer[sizeof(EFI_GUID) + sizeof(UINTN) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_RESTORE)]; + UINT8 *CommBuffer; UINTN CommSize; DEBUG ((EFI_D_INFO, "SmmLockBoxDxeLib RestoreLockBox - Enter\n")); @@ -330,21 +422,18 @@ RestoreLockBox ( return EFI_INVALID_PARAMETER; } - // - // Get needed resource - // - Status = gBS->LocateProtocol ( - &gEfiSmmCommunicationProtocolGuid, - NULL, - (VOID **)&SmmCommunication - ); - if (EFI_ERROR (Status)) { + SmmCommunication = LockBoxGetSmmCommProtocol (); + if (SmmCommunication == NULL) { return EFI_NOT_STARTED; } // // Prepare parameter // + CommBuffer = LockBoxGetSmmCommBuffer (); + if (CommBuffer == NULL) { + CommBuffer = &TempCommBuffer[0]; + } CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0]; CopyMem (&CommHeader->HeaderGuid, &gEfiSmmLockBoxCommunicationGuid, sizeof(gEfiSmmLockBoxCommunicationGuid)); CommHeader->MessageLength = sizeof(*LockBoxParameterRestore); @@ -364,7 +453,7 @@ RestoreLockBox ( // // Send command // - CommSize = sizeof(CommBuffer); + CommSize = sizeof(EFI_GUID) + sizeof(UINTN) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_RESTORE); Status = SmmCommunication->Communicate ( SmmCommunication, &CommBuffer[0], @@ -403,26 +492,24 @@ RestoreAllLockBoxInPlace ( EFI_SMM_COMMUNICATION_PROTOCOL *SmmCommunication; EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE *LockBoxParameterRestoreAllInPlace; EFI_SMM_COMMUNICATE_HEADER *CommHeader; - UINT8 CommBuffer[sizeof(EFI_GUID) + sizeof(UINTN) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE)]; + UINT8 TempCommBuffer[sizeof(EFI_GUID) + sizeof(UINTN) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE)]; + UINT8 *CommBuffer; UINTN CommSize; DEBUG ((EFI_D_INFO, "SmmLockBoxDxeLib RestoreAllLockBoxInPlace - Enter\n")); - // - // Get needed resource - // - Status = gBS->LocateProtocol ( - &gEfiSmmCommunicationProtocolGuid, - NULL, - (VOID **)&SmmCommunication - ); - if (EFI_ERROR (Status)) { + SmmCommunication = LockBoxGetSmmCommProtocol (); + if (SmmCommunication == NULL) { return EFI_NOT_STARTED; } // // Prepare parameter // + CommBuffer = LockBoxGetSmmCommBuffer (); + if (CommBuffer == NULL) { + CommBuffer = &TempCommBuffer[0]; + } CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0]; CopyMem (&CommHeader->HeaderGuid, &gEfiSmmLockBoxCommunicationGuid, sizeof(gEfiSmmLockBoxCommunicationGuid)); CommHeader->MessageLength = sizeof(*LockBoxParameterRestoreAllInPlace); @@ -435,7 +522,7 @@ RestoreAllLockBoxInPlace ( // // Send command // - CommSize = sizeof(CommBuffer); + CommSize = sizeof(EFI_GUID) + sizeof(UINTN) + sizeof(EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE); Status = SmmCommunication->Communicate ( SmmCommunication, &CommBuffer[0], diff --git a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.inf b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.inf index 48cdb9c66ab7..f3120ccb42d5 100644 --- a/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.inf +++ b/MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.inf @@ -1,7 +1,7 @@ ## @file # DXE LockBox library instance. # -# Copyright (c) 2010 - 2014, 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 @@ -42,9 +42,11 @@ [LibraryClasses] BaseLib BaseMemoryLib DebugLib + UefiLib [Guids] - gEfiSmmLockBoxCommunicationGuid ## SOMETIMES_CONSUMES ## GUID # Used to do smm communication + gEfiSmmLockBoxCommunicationGuid ## SOMETIMES_CONSUMES ## GUID # Used to do smm communication + gEdkiiPiSmmCommunicationRegionTableGuid ## SOMETIMES_CONSUMES ## SystemTable [Protocols] gEfiSmmCommunicationProtocolGuid ## SOMETIMES_CONSUMES -- 2.7.0.windows.1