X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=SecurityPkg%2FTcg%2FTcg2Smm%2FTcg2Smm.c;fp=SecurityPkg%2FTcg%2FTcg2Smm%2FTcg2Smm.c;h=498fb626bd9c956b57f412a1cba0d7e9cb4c31eb;hp=9f5111552b82309fbaddeb0f38058751f8120cfe;hb=c411b485b63a671a1e276700cff025c73997233c;hpb=39de741e2dcb8f11e9b4438e37224797643d8451 diff --git a/SecurityPkg/Tcg/Tcg2Smm/Tcg2Smm.c b/SecurityPkg/Tcg/Tcg2Smm/Tcg2Smm.c index 9f5111552b..498fb626bd 100644 --- a/SecurityPkg/Tcg/Tcg2Smm/Tcg2Smm.c +++ b/SecurityPkg/Tcg/Tcg2Smm/Tcg2Smm.c @@ -18,10 +18,10 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include "Tcg2Smm.h" EFI_SMM_VARIABLE_PROTOCOL *mSmmVariable = NULL; -TCG_NVS *mTcgNvs = NULL; -UINTN mPpSoftwareSmi; -UINTN mMcSoftwareSmi; -EFI_HANDLE mReadyToLockHandle; +TCG_NVS *mTcgNvs = NULL; +UINTN mPpSoftwareSmi; +UINTN mMcSoftwareSmi; +EFI_HANDLE mReadyToLockHandle; /** Communication service SMI Handler entry. @@ -47,32 +47,33 @@ EFI_HANDLE mReadyToLockHandle; EFI_STATUS EFIAPI TpmNvsCommunciate ( - IN EFI_HANDLE DispatchHandle, - IN CONST VOID *RegisterContext, - IN OUT VOID *CommBuffer, - IN OUT UINTN *CommBufferSize + IN EFI_HANDLE DispatchHandle, + IN CONST VOID *RegisterContext, + IN OUT VOID *CommBuffer, + IN OUT UINTN *CommBufferSize ) { - EFI_STATUS Status; - UINTN TempCommBufferSize; - TPM_NVS_MM_COMM_BUFFER *CommParams; + EFI_STATUS Status; + UINTN TempCommBufferSize; + TPM_NVS_MM_COMM_BUFFER *CommParams; DEBUG ((DEBUG_VERBOSE, "%a()\n", __FUNCTION__)); // // If input is invalid, stop processing this SMI // - if (CommBuffer == NULL || CommBufferSize == NULL) { + if ((CommBuffer == NULL) || (CommBufferSize == NULL)) { return EFI_SUCCESS; } TempCommBufferSize = *CommBufferSize; - if(TempCommBufferSize != sizeof (TPM_NVS_MM_COMM_BUFFER)) { + if (TempCommBufferSize != sizeof (TPM_NVS_MM_COMM_BUFFER)) { DEBUG ((DEBUG_ERROR, "[%a] MM Communication buffer size is invalid for this handler!\n", __FUNCTION__)); return EFI_ACCESS_DENIED; } - if (!IsBufferOutsideMmValid ((UINTN) CommBuffer, TempCommBufferSize)) { + + if (!IsBufferOutsideMmValid ((UINTN)CommBuffer, TempCommBufferSize)) { DEBUG ((DEBUG_ERROR, "[%a] - MM Communication buffer in invalid location!\n", __FUNCTION__)); return EFI_ACCESS_DENIED; } @@ -80,14 +81,14 @@ TpmNvsCommunciate ( // // Farm out the job to individual functions based on what was requested. // - CommParams = (TPM_NVS_MM_COMM_BUFFER*) CommBuffer; - Status = EFI_SUCCESS; + CommParams = (TPM_NVS_MM_COMM_BUFFER *)CommBuffer; + Status = EFI_SUCCESS; switch (CommParams->Function) { case TpmNvsMmExchangeInfo: DEBUG ((DEBUG_VERBOSE, "[%a] - Function requested: MM_EXCHANGE_NVS_INFO\n", __FUNCTION__)); CommParams->RegisteredPpSwiValue = mPpSoftwareSmi; CommParams->RegisteredMcSwiValue = mMcSoftwareSmi; - mTcgNvs = (TCG_NVS*) (UINTN) CommParams->TargetAddress; + mTcgNvs = (TCG_NVS *)(UINTN)CommParams->TargetAddress; break; default: @@ -96,7 +97,7 @@ TpmNvsCommunciate ( break; } - CommParams->ReturnStatus = (UINT64) Status; + CommParams->ReturnStatus = (UINT64)Status; return EFI_SUCCESS; } @@ -120,17 +121,16 @@ TpmNvsCommunciate ( EFI_STATUS EFIAPI PhysicalPresenceCallback ( - IN EFI_HANDLE DispatchHandle, - IN CONST VOID *Context, - IN OUT VOID *CommBuffer, - IN OUT UINTN *CommBufferSize + IN EFI_HANDLE DispatchHandle, + IN CONST VOID *Context, + IN OUT VOID *CommBuffer, + IN OUT UINTN *CommBufferSize ) { - UINT32 MostRecentRequest; - UINT32 Response; - UINT32 OperationRequest; - UINT32 RequestParameter; - + UINT32 MostRecentRequest; + UINT32 Response; + UINT32 OperationRequest; + UINT32 RequestParameter; if (mTcgNvs->PhysicalPresence.Parameter == TCG_ACPI_FUNCTION_RETURN_REQUEST_RESPONSE_TO_OS) { mTcgNvs->PhysicalPresence.ReturnCode = Tcg2PhysicalPresenceLibReturnOperationResponseToOsFunction ( @@ -138,18 +138,18 @@ PhysicalPresenceCallback ( &Response ); mTcgNvs->PhysicalPresence.LastRequest = MostRecentRequest; - mTcgNvs->PhysicalPresence.Response = Response; + mTcgNvs->PhysicalPresence.Response = Response; return EFI_SUCCESS; - } else if ((mTcgNvs->PhysicalPresence.Parameter == TCG_ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS) - || (mTcgNvs->PhysicalPresence.Parameter == TCG_ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS_2)) { - - OperationRequest = mTcgNvs->PhysicalPresence.Request; - RequestParameter = mTcgNvs->PhysicalPresence.RequestParameter; + } else if ( (mTcgNvs->PhysicalPresence.Parameter == TCG_ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS) + || (mTcgNvs->PhysicalPresence.Parameter == TCG_ACPI_FUNCTION_SUBMIT_REQUEST_TO_BIOS_2)) + { + OperationRequest = mTcgNvs->PhysicalPresence.Request; + RequestParameter = mTcgNvs->PhysicalPresence.RequestParameter; mTcgNvs->PhysicalPresence.ReturnCode = Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunctionEx ( &OperationRequest, &RequestParameter ); - mTcgNvs->PhysicalPresence.Request = OperationRequest; + mTcgNvs->PhysicalPresence.Request = OperationRequest; mTcgNvs->PhysicalPresence.RequestParameter = RequestParameter; } else if (mTcgNvs->PhysicalPresence.Parameter == TCG_ACPI_FUNCTION_GET_USER_CONFIRMATION_STATUS_FOR_REQUEST) { mTcgNvs->PhysicalPresence.ReturnCode = Tcg2PhysicalPresenceLibGetUserConfirmationStatusFunction (mTcgNvs->PPRequestUserConfirm); @@ -158,7 +158,6 @@ PhysicalPresenceCallback ( return EFI_SUCCESS; } - /** Software SMI callback for MemoryClear which is called from ACPI method. @@ -179,28 +178,28 @@ PhysicalPresenceCallback ( EFI_STATUS EFIAPI MemoryClearCallback ( - IN EFI_HANDLE DispatchHandle, - IN CONST VOID *Context, - IN OUT VOID *CommBuffer, - IN OUT UINTN *CommBufferSize + IN EFI_HANDLE DispatchHandle, + IN CONST VOID *Context, + IN OUT VOID *CommBuffer, + IN OUT UINTN *CommBufferSize ) { - EFI_STATUS Status; - UINTN DataSize; - UINT8 MorControl; + EFI_STATUS Status; + UINTN DataSize; + UINT8 MorControl; mTcgNvs->MemoryClear.ReturnCode = MOR_REQUEST_SUCCESS; if (mTcgNvs->MemoryClear.Parameter == ACPI_FUNCTION_DSM_MEMORY_CLEAR_INTERFACE) { - MorControl = (UINT8) mTcgNvs->MemoryClear.Request; + MorControl = (UINT8)mTcgNvs->MemoryClear.Request; } else if (mTcgNvs->MemoryClear.Parameter == ACPI_FUNCTION_PTS_CLEAR_MOR_BIT) { DataSize = sizeof (UINT8); - Status = mSmmVariable->SmmGetVariable ( - MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME, - &gEfiMemoryOverwriteControlDataGuid, - NULL, - &DataSize, - &MorControl - ); + Status = mSmmVariable->SmmGetVariable ( + MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME, + &gEfiMemoryOverwriteControlDataGuid, + NULL, + &DataSize, + &MorControl + ); if (EFI_ERROR (Status)) { mTcgNvs->MemoryClear.ReturnCode = MOR_REQUEST_GENERAL_FAILURE; DEBUG ((DEBUG_ERROR, "[TPM] Get MOR variable failure! Status = %r\n", Status)); @@ -210,6 +209,7 @@ MemoryClearCallback ( if (MOR_CLEAR_MEMORY_VALUE (MorControl) == 0x0) { return EFI_SUCCESS; } + MorControl &= ~MOR_CLEAR_MEMORY_BIT_MASK; } else { mTcgNvs->MemoryClear.ReturnCode = MOR_REQUEST_GENERAL_FAILURE; @@ -218,13 +218,13 @@ MemoryClearCallback ( } DataSize = sizeof (UINT8); - Status = mSmmVariable->SmmSetVariable ( - MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME, - &gEfiMemoryOverwriteControlDataGuid, - EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, - DataSize, - &MorControl - ); + Status = mSmmVariable->SmmSetVariable ( + MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME, + &gEfiMemoryOverwriteControlDataGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, + DataSize, + &MorControl + ); if (EFI_ERROR (Status)) { mTcgNvs->MemoryClear.ReturnCode = MOR_REQUEST_GENERAL_FAILURE; DEBUG ((DEBUG_ERROR, "[TPM] Set MOR variable failure! Status = %r\n", Status)); @@ -249,16 +249,17 @@ TcgMmReadyToLock ( IN CONST EFI_GUID *Protocol, IN VOID *Interface, IN EFI_HANDLE Handle -) + ) { - EFI_STATUS Status; + EFI_STATUS Status; Status = EFI_SUCCESS; if (mReadyToLockHandle != NULL) { - Status = gMmst->MmiHandlerUnRegister (mReadyToLockHandle); + Status = gMmst->MmiHandlerUnRegister (mReadyToLockHandle); mReadyToLockHandle = NULL; } + return Status; } @@ -284,17 +285,17 @@ InitializeTcgCommon ( EFI_HANDLE McSwHandle; EFI_HANDLE NotifyHandle; - if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm20DtpmGuid)){ + if (!CompareGuid (PcdGetPtr (PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm20DtpmGuid)) { DEBUG ((DEBUG_ERROR, "No TPM2 DTPM instance required!\n")); return EFI_UNSUPPORTED; } // Initialize variables first mReadyToLockHandle = NULL; - SwDispatch = NULL; - PpSwHandle = NULL; - McSwHandle = NULL; - NotifyHandle = NULL; + SwDispatch = NULL; + PpSwHandle = NULL; + McSwHandle = NULL; + NotifyHandle = NULL; // Register a root handler to communicate the NVS region and SMI channel between MM and DXE Status = gMmst->MmiHandlerRegister (TpmNvsCommunciate, &gTpmNvsMmGuid, &mReadyToLockHandle); @@ -307,35 +308,37 @@ InitializeTcgCommon ( // // Get the Sw dispatch protocol and register SMI callback functions. // - Status = gMmst->MmLocateProtocol (&gEfiSmmSwDispatch2ProtocolGuid, NULL, (VOID**)&SwDispatch); + Status = gMmst->MmLocateProtocol (&gEfiSmmSwDispatch2ProtocolGuid, NULL, (VOID **)&SwDispatch); ASSERT_EFI_ERROR (Status); if (EFI_ERROR (Status)) { DEBUG ((DEBUG_ERROR, "[%a] Failed to locate Sw dispatch protocol - %r!\n", __FUNCTION__, Status)); goto Cleanup; } - SwContext.SwSmiInputValue = (UINTN) -1; - Status = SwDispatch->Register (SwDispatch, PhysicalPresenceCallback, &SwContext, &PpSwHandle); + SwContext.SwSmiInputValue = (UINTN)-1; + Status = SwDispatch->Register (SwDispatch, PhysicalPresenceCallback, &SwContext, &PpSwHandle); ASSERT_EFI_ERROR (Status); if (EFI_ERROR (Status)) { DEBUG ((DEBUG_ERROR, "[%a] Failed to register PP callback as SW MM handler - %r!\n", __FUNCTION__, Status)); goto Cleanup; } + mPpSoftwareSmi = SwContext.SwSmiInputValue; - SwContext.SwSmiInputValue = (UINTN) -1; - Status = SwDispatch->Register (SwDispatch, MemoryClearCallback, &SwContext, &McSwHandle); + SwContext.SwSmiInputValue = (UINTN)-1; + Status = SwDispatch->Register (SwDispatch, MemoryClearCallback, &SwContext, &McSwHandle); ASSERT_EFI_ERROR (Status); if (EFI_ERROR (Status)) { DEBUG ((DEBUG_ERROR, "[%a] Failed to register MC callback as SW MM handler - %r!\n", __FUNCTION__, Status)); goto Cleanup; } + mMcSoftwareSmi = SwContext.SwSmiInputValue; // // Locate SmmVariableProtocol. // - Status = gMmst->MmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID**)&mSmmVariable); + Status = gMmst->MmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID **)&mSmmVariable); ASSERT_EFI_ERROR (Status); if (EFI_ERROR (Status)) { // Should not happen @@ -359,12 +362,15 @@ Cleanup: if (NotifyHandle != NULL) { gMmst->MmRegisterProtocolNotify (&gEfiMmReadyToLockProtocolGuid, NULL, &NotifyHandle); } - if (McSwHandle != NULL && SwDispatch != NULL) { + + if ((McSwHandle != NULL) && (SwDispatch != NULL)) { SwDispatch->UnRegister (SwDispatch, McSwHandle); } - if (PpSwHandle != NULL && SwDispatch != NULL) { + + if ((PpSwHandle != NULL) && (SwDispatch != NULL)) { SwDispatch->UnRegister (SwDispatch, PpSwHandle); } + if (mReadyToLockHandle != NULL) { gMmst->MmiHandlerUnRegister (mReadyToLockHandle); }