Handle TPM device error and avoid deadloop in BDS.

If TPM error happens, set TPM flag to NOT present, so that trusted boot patch is disabled.
Also report status code for failure, so that platform may register handler to apply policy like force system reset, or disable TPM permanently.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: "Yao, Jiewen" <jiewen.yao@intel.com>
Reviewed-by: "Dong, Guo" <guo.dong@intel.com>




git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16598 6f19259b-4bc3-4df7-8a09-765794883524
diff --git a/SecurityPkg/Include/Guid/TcgEventHob.h b/SecurityPkg/Include/Guid/TcgEventHob.h
index 9a1cf58..c92ee8c 100644
--- a/SecurityPkg/Include/Guid/TcgEventHob.h
+++ b/SecurityPkg/Include/Guid/TcgEventHob.h
@@ -3,7 +3,7 @@
   a TPM DXE Driver. A GUIDed HOB is generated for each measurement 

   made in the PEI Phase.

     

-Copyright (c) 2007 - 2013, 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 

 which accompanies this distribution.  The full text of the license may be found at 

@@ -27,4 +27,14 @@
 

 extern EFI_GUID gTcgEventEntryHobGuid;

 

+///

+/// The Global ID of a GUIDed HOB used to record TPM device error.

+///

+#define EFI_TPM_ERROR_GUID \

+  { \

+    0xef598499, 0xb25e, 0x473a, { 0xbf, 0xaf, 0xe7, 0xe5, 0x7d, 0xce, 0x82, 0xc4 } \

+  }

+

+extern EFI_GUID gTpmErrorHobGuid;

+

 #endif

diff --git a/SecurityPkg/Library/DxeTpm2MeasureBootLib/DxeTpm2MeasureBootLib.c b/SecurityPkg/Library/DxeTpm2MeasureBootLib/DxeTpm2MeasureBootLib.c
index 6b6a377..ed53b7e 100644
--- a/SecurityPkg/Library/DxeTpm2MeasureBootLib/DxeTpm2MeasureBootLib.c
+++ b/SecurityPkg/Library/DxeTpm2MeasureBootLib/DxeTpm2MeasureBootLib.c
@@ -15,7 +15,7 @@
   TrEEMeasureGptTable() function will receive untrusted GPT partition table, and parse

   partition data carefully.

 

-Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2013 - 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 

@@ -456,7 +456,7 @@
                            TreeProtocol, 

                            &ProtocolCapability

                            );

-  if (EFI_ERROR (Status) || !ProtocolCapability.TrEEPresentFlag) {

+  if (EFI_ERROR (Status) || (!ProtocolCapability.TrEEPresentFlag)) {

     //

     // TPM device doesn't work or activate.

     //

diff --git a/SecurityPkg/Library/DxeTpmMeasureBootLib/DxeTpmMeasureBootLib.c b/SecurityPkg/Library/DxeTpmMeasureBootLib/DxeTpmMeasureBootLib.c
index 5bfa282..466a74e 100644
--- a/SecurityPkg/Library/DxeTpmMeasureBootLib/DxeTpmMeasureBootLib.c
+++ b/SecurityPkg/Library/DxeTpmMeasureBootLib/DxeTpmMeasureBootLib.c
@@ -15,7 +15,7 @@
   TcgMeasureGptTable() function will receive untrusted GPT partition table, and parse

   partition data carefully.

 

-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 

 which accompanies this distribution.  The full text of the license may be found at 

@@ -768,7 +768,7 @@
              &EventLogLocation,

              &EventLogLastEntry

            );

-  if (EFI_ERROR (Status) || ProtocolCapability.TPMDeactivatedFlag) {

+  if (EFI_ERROR (Status) || ProtocolCapability.TPMDeactivatedFlag || (!ProtocolCapability.TPMPresentFlag)) {

     //

     // TPM device doesn't work or activate.

     //

diff --git a/SecurityPkg/Library/Tpm12DeviceLibDTpm/Tpm12Tis.c b/SecurityPkg/Library/Tpm12DeviceLibDTpm/Tpm12Tis.c
index 2ad5345..2fb360f 100644
--- a/SecurityPkg/Library/Tpm12DeviceLibDTpm/Tpm12Tis.c
+++ b/SecurityPkg/Library/Tpm12DeviceLibDTpm/Tpm12Tis.c
@@ -1,7 +1,7 @@
 /** @file

   TIS (TPM Interface Specification) functions used by TPM1.2.

   

-Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2013 - 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 

@@ -217,7 +217,6 @@
   @retval     EFI_TIMEOUT  The register can't run into the expected status in time.

 **/

 EFI_STATUS

-EFIAPI

 Tpm12TisPcWaitRegisterBits (

   IN      UINT8                     *Register,

   IN      UINT8                     BitSet,

@@ -249,7 +248,6 @@
   @retval     EFI_TIMEOUT           BurstCount can't be got in time.

 **/

 EFI_STATUS

-EFIAPI

 Tpm12TisPcReadBurstCount (

   IN      TIS_PC_REGISTERS_PTR      TisReg,

      OUT  UINT16                    *BurstCount

@@ -293,7 +291,6 @@
   @retval    EFI_TIMEOUT           TPM chip can't be set to ready state in time.

 **/

 EFI_STATUS

-EFIAPI

 Tpm12TisPcPrepareCommand (

   IN      TIS_PC_REGISTERS_PTR      TisReg

   )

@@ -326,7 +323,6 @@
   @retval    EFI_TIMEOUT           Can't get the TPM control in time.

 **/

 EFI_STATUS

-EFIAPI

 Tpm12TisPcRequestUseTpm (

   IN      TIS_PC_REGISTERS_PTR      TisReg

   )

@@ -361,7 +357,6 @@
   @param[in, out] SizeOut       Size of response data.  

  

   @retval EFI_SUCCESS           Operation completed successfully.

-  @retval EFI_TIMEOUT           The register can't run into the expected status in time.

   @retval EFI_BUFFER_TOO_SMALL  Response data buffer is too small.

   @retval EFI_DEVICE_ERROR      Unexpected device behavior.

   @retval EFI_UNSUPPORTED       Unsupported TPM version

@@ -408,7 +403,7 @@
   Status = Tpm12TisPcPrepareCommand (TisReg);

   if (EFI_ERROR (Status)){

     DEBUG ((DEBUG_ERROR, "Tpm12 is not ready for command!\n"));

-    return Status;

+    return EFI_DEVICE_ERROR;

   }

   //

   // Send the command data to Tpm

@@ -417,7 +412,7 @@
   while (Index < SizeIn) {

     Status = Tpm12TisPcReadBurstCount (TisReg, &BurstCount);

     if (EFI_ERROR (Status)) {

-      Status = EFI_TIMEOUT;

+      Status = EFI_DEVICE_ERROR;

       goto Exit;

     }

     for (; BurstCount > 0 && Index < SizeIn; BurstCount--) {

@@ -451,7 +446,7 @@
              );

   if (EFI_ERROR (Status)) {

     DEBUG ((DEBUG_ERROR, "Wait for Tpm12 response data time out!!\n"));

-    Status = EFI_TIMEOUT;

+    Status = EFI_DEVICE_ERROR;

     goto Exit;

   }

   //

@@ -462,7 +457,7 @@
   while (Index < sizeof (TPM_RSP_COMMAND_HDR)) {

     Status = Tpm12TisPcReadBurstCount (TisReg, &BurstCount);

     if (EFI_ERROR (Status)) {

-      Status = EFI_TIMEOUT;

+      Status = EFI_DEVICE_ERROR;

       goto Exit;

     }

     for (; BurstCount > 0; BurstCount--) {

@@ -509,7 +504,7 @@
     }

     Status = Tpm12TisPcReadBurstCount (TisReg, &BurstCount);

     if (EFI_ERROR (Status)) {

-      Status = EFI_TIMEOUT;

+      Status = EFI_DEVICE_ERROR;

       goto Exit;

     }

   }

diff --git a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Tis.c b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Tis.c
index cf85d4c..a186279 100644
--- a/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Tis.c
+++ b/SecurityPkg/Library/Tpm2DeviceLibDTpm/Tpm2Tis.c
@@ -1,7 +1,7 @@
 /** @file

   TIS (TPM Interface Specification) functions used by dTPM2.0 library.

   

-Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2013 - 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 

@@ -223,7 +223,6 @@
   @retval     EFI_TIMEOUT  The register can't run into the expected status in time.

 **/

 EFI_STATUS

-EFIAPI

 TisPcWaitRegisterBits (

   IN      UINT8                     *Register,

   IN      UINT8                     BitSet,

@@ -255,7 +254,6 @@
   @retval     EFI_TIMEOUT           BurstCount can't be got in time.

 **/

 EFI_STATUS

-EFIAPI

 TisPcReadBurstCount (

   IN      TIS_PC_REGISTERS_PTR      TisReg,

      OUT  UINT16                    *BurstCount

@@ -299,7 +297,6 @@
   @retval    EFI_TIMEOUT           TPM chip can't be set to ready state in time.

 **/

 EFI_STATUS

-EFIAPI

 TisPcPrepareCommand (

   IN      TIS_PC_REGISTERS_PTR      TisReg

   )

@@ -332,7 +329,6 @@
   @retval    EFI_TIMEOUT           Can't get the TPM control in time.

 **/

 EFI_STATUS

-EFIAPI

 TisPcRequestUseTpm (

   IN      TIS_PC_REGISTERS_PTR      TisReg

   )

@@ -367,7 +363,6 @@
   @param[in, out] SizeOut       Size of response data.  

  

   @retval EFI_SUCCESS           Operation completed successfully.

-  @retval EFI_TIMEOUT           The register can't run into the expected status in time.

   @retval EFI_BUFFER_TOO_SMALL  Response data buffer is too small.

   @retval EFI_DEVICE_ERROR      Unexpected device behavior.

   @retval EFI_UNSUPPORTED       Unsupported TPM version

@@ -392,7 +387,7 @@
   DEBUG_CODE (

     UINTN  DebugSize;

 

-    DEBUG ((EFI_D_INFO, "TisTpmCommand Send - "));

+    DEBUG ((EFI_D_INFO, "Tpm2TisTpmCommand Send - "));

     if (SizeIn > 0x100) {

       DebugSize = 0x40;

     } else {

@@ -413,8 +408,8 @@
 

   Status = TisPcPrepareCommand (TisReg);

   if (EFI_ERROR (Status)){

-    DEBUG ((DEBUG_ERROR, "Tpm is not ready for command!\n"));

-    return Status;

+    DEBUG ((DEBUG_ERROR, "Tpm2 is not ready for command!\n"));

+    return EFI_DEVICE_ERROR;

   }

   //

   // Send the command data to Tpm

@@ -423,7 +418,7 @@
   while (Index < SizeIn) {

     Status = TisPcReadBurstCount (TisReg, &BurstCount);

     if (EFI_ERROR (Status)) {

-      Status = EFI_TIMEOUT;

+      Status = EFI_DEVICE_ERROR;

       goto Exit;

     }

     for (; BurstCount > 0 && Index < SizeIn; BurstCount--) {

@@ -441,7 +436,7 @@
              TIS_TIMEOUT_C

              );

   if (EFI_ERROR (Status)) {

-    DEBUG ((DEBUG_ERROR, "The send buffer too small!\n"));

+    DEBUG ((DEBUG_ERROR, "Tpm2 The send buffer too small!\n"));

     Status = EFI_BUFFER_TOO_SMALL;

     goto Exit;

   }

@@ -460,8 +455,8 @@
              TIS_TIMEOUT_MAX

              );

   if (EFI_ERROR (Status)) {

-    DEBUG ((DEBUG_ERROR, "Wait for Tpm response data time out!!\n"));

-    Status = EFI_TIMEOUT;

+    DEBUG ((DEBUG_ERROR, "Wait for Tpm2 response data time out!!\n"));

+    Status = EFI_DEVICE_ERROR;

     goto Exit;

   }

   //

@@ -472,7 +467,7 @@
   while (Index < sizeof (TPM2_RESPONSE_HEADER)) {

     Status = TisPcReadBurstCount (TisReg, &BurstCount);

     if (EFI_ERROR (Status)) {

-      Status = EFI_TIMEOUT;

+      Status = EFI_DEVICE_ERROR;

       goto Exit;

     }

     for (; BurstCount > 0; BurstCount--) {

@@ -494,7 +489,7 @@
   CopyMem (&Data16, BufferOut, sizeof (UINT16));

   // TPM2 should not use this RSP_COMMAND

   if (SwapBytes16 (Data16) == TPM_ST_RSP_COMMAND) {

-    DEBUG ((EFI_D_ERROR, "TPM_ST_RSP error - %x\n", TPM_ST_RSP_COMMAND));

+    DEBUG ((EFI_D_ERROR, "TPM2: TPM_ST_RSP error - %x\n", TPM_ST_RSP_COMMAND));

     Status = EFI_UNSUPPORTED;

     goto Exit;

   }

@@ -520,13 +515,13 @@
     }

     Status = TisPcReadBurstCount (TisReg, &BurstCount);

     if (EFI_ERROR (Status)) {

-      Status = EFI_TIMEOUT;

+      Status = EFI_DEVICE_ERROR;

       goto Exit;

     }

   }

 Exit:

   DEBUG_CODE (

-    DEBUG ((EFI_D_INFO, "TisTpmCommand Receive - "));

+    DEBUG ((EFI_D_INFO, "Tpm2TisTpmCommand Receive - "));

     for (Index = 0; Index < TpmOutSize; Index++) {

       DEBUG ((EFI_D_INFO, "%02x ", BufferOut[Index]));

     }

diff --git a/SecurityPkg/SecurityPkg.dec b/SecurityPkg/SecurityPkg.dec
index 7bbc1e6..c3e894f 100644
--- a/SecurityPkg/SecurityPkg.dec
+++ b/SecurityPkg/SecurityPkg.dec
@@ -112,6 +112,10 @@
   #  Include/Guid/TcgEventHob.h

   gTcgEventEntryHobGuid              = { 0x2b9ffb52, 0x1b13, 0x416f, { 0xa8, 0x7b, 0xbc, 0x93, 0xd, 0xef, 0x92, 0xa8 }}

 

+  ## HOB GUID used to record TPM device error.

+  #  Include/Guid/TcgEventHob.h

+  gTpmErrorHobGuid                   = { 0xef598499, 0xb25e, 0x473a, { 0xbf, 0xaf, 0xe7, 0xe5, 0x7d, 0xce, 0x82, 0xc4 }}

+

   ## HOB GUID used to pass all PEI measured FV info to DXE Driver.

   #  Include/Guid/MeasuredFvHob.h

   gMeasuredFvHobGuid                 = { 0xb2360b42, 0x7173, 0x420a, { 0x86, 0x96, 0x46, 0xca, 0x6b, 0xab, 0x10, 0x60 }}

@@ -253,6 +257,12 @@
   # @Prompt Select platform type.

   # @ValidRange 0x80000001 | 0x00 - 0x1 

   gEfiSecurityPkgTokenSpaceGuid.PcdTpmPlatformClass|0|UINT8|0x00000006

+  

+  ## Progress Code for TPM device subclass definitions.<BR><BR>

+  #  EFI_PERIPHERAL_TPM  = (EFI_PERIPHERAL | 0x000D0000) = 0x010D0000<BR>

+  # @Prompt Status Code for TPM device definitions

+  # @ValidList  0x80000003 | 0x010D0000

+  gEfiSecurityPkgTokenSpaceGuid.PcdStatusCodeSubClassTpmDevice|0x010D0000|UINT32|0x00000007

 

 [PcdsFixedAtBuild, PcdsPatchableInModule, PcdsDynamic, PcdsDynamicEx]

   ## Indicates the presence or absence of the platform operator during firmware booting.

diff --git a/SecurityPkg/Tcg/TcgDxe/TcgDxe.c b/SecurityPkg/Tcg/TcgDxe/TcgDxe.c
index 282505d..ea05dc8 100644
--- a/SecurityPkg/Tcg/TcgDxe/TcgDxe.c
+++ b/SecurityPkg/Tcg/TcgDxe/TcgDxe.c
@@ -8,7 +8,7 @@
 

 TcgDxePassThroughToTpm() will receive untrusted input and do basic validation.

 

-Copyright (c) 2005 - 2014, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2005 - 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,6 +51,7 @@
 #include <Library/TpmCommLib.h>

 #include <Library/PcdLib.h>

 #include <Library/UefiLib.h>

+#include <Library/ReportStatusCodeLib.h>

 

 #include "TpmComm.h"

 

@@ -264,7 +265,7 @@
   }

 

   if (EventLogLastEntry != NULL) {

-    if (TcgData->BsCap.TPMDeactivatedFlag) {

+    if (TcgData->BsCap.TPMDeactivatedFlag || (!ProtocolCapability.TPMPresentFlag)) {

       *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)0;

     } else {

       *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)TcgData->LastEvent;

@@ -411,7 +412,7 @@
 

   TcgData = TCG_DXE_DATA_FROM_THIS (This);

   

-  if (TcgData->BsCap.TPMDeactivatedFlag) {

+  if (TcgData->BsCap.TPMDeactivatedFlag || (!ProtocolCapability.TPMPresentFlag)) {

     return EFI_DEVICE_ERROR;

   }

   return TcgDxeLogEventI (

@@ -495,8 +496,8 @@
 {

   EFI_STATUS                        Status;

 

-  if (HashData == NULL && HashDataLen > 0) {

-    return EFI_INVALID_PARAMETER;

+  if (!TcgData->BsCap.TPMPresentFlag) {

+    return EFI_DEVICE_ERROR;

   }

 

   if (HashDataLen > 0 || HashData != NULL) {

@@ -507,7 +508,7 @@
                );

     if (EFI_ERROR(Status)) {

       DEBUG ((DEBUG_ERROR, "TpmCommHashAll Failed. %x\n", Status));

-      return Status;

+      goto Done;

     }

   }

 

@@ -521,6 +522,17 @@
     Status = TcgDxeLogEventI (TcgData, NewEventHdr, NewEventData);

   }

 

+Done:

+  if ((Status == EFI_DEVICE_ERROR) || (Status == EFI_TIMEOUT)) {

+    DEBUG ((EFI_D_ERROR, "TcgDxeHashLogExtendEventI - %r. Disable TPM.\n", Status));

+    TcgData->BsCap.TPMPresentFlag = FALSE;

+    REPORT_STATUS_CODE (

+      EFI_ERROR_CODE | EFI_ERROR_MINOR,

+      (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)

+      );

+    Status = EFI_DEVICE_ERROR;

+  }

+

   return Status;

 }

 

@@ -569,13 +581,17 @@
 

   TcgData = TCG_DXE_DATA_FROM_THIS (This);

   

-  if (TcgData->BsCap.TPMDeactivatedFlag) {

+  if (TcgData->BsCap.TPMDeactivatedFlag || (!ProtocolCapability.TPMPresentFlag)) {

     return EFI_DEVICE_ERROR;

   }

     

   if (AlgorithmId != TPM_ALG_SHA) {

     return EFI_UNSUPPORTED;

   }

+  

+  if (HashData == NULL && HashDataLen > 0) {

+    return EFI_INVALID_PARAMETER;

+  }

 

   Status = TcgDxeHashLogExtendEventI (

              TcgData,

@@ -1346,6 +1362,10 @@
     return Status;

   }

 

+  if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {

+    mTcgDxeData.BsCap.TPMPresentFlag = FALSE;

+  }

+

   Status = GetTpmStatus (&mTcgDxeData.BsCap.TPMDeactivatedFlag);

   if (EFI_ERROR (Status)) {

     DEBUG ((

@@ -1363,7 +1383,7 @@
                   EFI_NATIVE_INTERFACE,

                   &mTcgDxeData.TcgProtocol

                   );

-  if (!EFI_ERROR (Status) && !mTcgDxeData.BsCap.TPMDeactivatedFlag) {

+  if (!EFI_ERROR (Status) && (!mTcgDxeData.BsCap.TPMDeactivatedFlag) && ProtocolCapability.TPMPresentFlag) {

     //

     // Setup the log area and copy event log from hob list to it

     //

diff --git a/SecurityPkg/Tcg/TcgDxe/TcgDxe.inf b/SecurityPkg/Tcg/TcgDxe/TcgDxe.inf
index 6cb4912..39aeb8b 100644
--- a/SecurityPkg/Tcg/TcgDxe/TcgDxe.inf
+++ b/SecurityPkg/Tcg/TcgDxe/TcgDxe.inf
@@ -2,7 +2,7 @@
 #  Produces TCG protocol and measures boot environment

 #  This module will produce TCG protocol and measure boot environment.

 #

-# 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

@@ -51,6 +51,7 @@
   PrintLib

   UefiLib

   PcdLib

+  ReportStatusCodeLib

 

 [Guids]

   ## SOMETIMES_CONSUMES     ## SystemTable           # Smbios Table

@@ -59,6 +60,7 @@
   

   gEfiGlobalVariableGuid                             ## SOMETIMES_CONSUMES  ## Variable:L"BootXXXX"

   gTcgEventEntryHobGuid                              ## SOMETIMES_CONSUMES  ## HOB

+  gTpmErrorHobGuid                                   ## SOMETIMES_CONSUMES  ## HOB

   gEfiEventExitBootServicesGuid                      ## CONSUMES            ## Event

   gEventExitBootServicesFailedGuid                   ## SOMETIMES_CONSUMES  ## Event

   gEfiTpmDeviceInstanceTpm12Guid                     ## PRODUCES            ## GUID       # TPM device identifier

@@ -76,6 +78,7 @@
   gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiDefaultOemRevision       ## SOMETIMES_CONSUMES

   gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiDefaultCreatorId         ## SOMETIMES_CONSUMES

   gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiDefaultCreatorRevision   ## SOMETIMES_CONSUMES

+  gEfiSecurityPkgTokenSpaceGuid.PcdStatusCodeSubClassTpmDevice   ## SOMETIMES_CONSUMES

 

 [Depex]

   TRUE

diff --git a/SecurityPkg/Tcg/TcgPei/TcgPei.c b/SecurityPkg/Tcg/TcgPei/TcgPei.c
index 0c00e28..dcf4b0b 100644
--- a/SecurityPkg/Tcg/TcgPei/TcgPei.c
+++ b/SecurityPkg/Tcg/TcgPei/TcgPei.c
@@ -1,7 +1,7 @@
 /** @file

   Initialize TPM device and measure FVs before handing off control to DXE.

 

-Copyright (c) 2005 - 2014, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2005 - 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 

@@ -38,6 +38,7 @@
 #include <Library/PeiServicesTablePointerLib.h>

 #include <Library/BaseLib.h>

 #include <Library/MemoryAllocationLib.h>

+#include <Library/ReportStatusCodeLib.h>

 

 #include "TpmComm.h"

 

@@ -221,6 +222,10 @@
 {

   EFI_STATUS                        Status;

   VOID                              *HobData;

+  

+  if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {

+    return EFI_DEVICE_ERROR;

+  }

 

   HobData = NULL;

   if (HashDataLen != 0) {

@@ -229,7 +234,9 @@
                HashDataLen,

                &NewEventHdr->Digest

                );

-    ASSERT_EFI_ERROR (Status);

+    if (EFI_ERROR (Status)) {

+      goto Done;

+    }

   }

 

   Status = TpmCommExtend (

@@ -239,20 +246,34 @@
              NewEventHdr->PCRIndex,

              NULL

              );

-  ASSERT_EFI_ERROR (Status);

+  if (EFI_ERROR (Status)) {

+    goto Done;

+  }

 

   HobData = BuildGuidHob (

              &gTcgEventEntryHobGuid,

              sizeof (*NewEventHdr) + NewEventHdr->EventSize

              );

   if (HobData == NULL) {

-    return EFI_OUT_OF_RESOURCES;

+    Status = EFI_OUT_OF_RESOURCES;

+    goto Done;

   }

 

   CopyMem (HobData, NewEventHdr, sizeof (*NewEventHdr));

   HobData = (VOID *) ((UINT8*)HobData + sizeof (*NewEventHdr));

   CopyMem (HobData, NewEventData, NewEventHdr->EventSize);

-  return EFI_SUCCESS;

+

+Done:

+  if ((Status == EFI_DEVICE_ERROR) || (Status == EFI_TIMEOUT)) {

+    DEBUG ((EFI_D_ERROR, "HashLogExtendEvent - %r. Disable TPM.\n", Status));

+    BuildGuidHob (&gTpmErrorHobGuid,0);

+    REPORT_STATUS_CODE (

+      EFI_ERROR_CODE | EFI_ERROR_MINOR,

+      (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)

+      );

+    Status = EFI_DEVICE_ERROR;

+  }

+  return Status;

 }

 

 /**

@@ -365,7 +386,6 @@
              &TcgEventHdr,

              (UINT8*) &FvBlob

              );

-  ASSERT_EFI_ERROR (Status);

 

   //

   // Add new FV into the measured FV list.

@@ -682,7 +702,6 @@
   if (IsTpmUsable (PeiServices, TpmHandle)) {

     if (PcdGet8 (PcdTpmScrtmPolicy) == 1) {

       Status = MeasureCRTMVersion (PeiServices, TpmHandle);

-      ASSERT_EFI_ERROR (Status);

     }

 

     Status = MeasureMainBios (PeiServices, TpmHandle);

diff --git a/SecurityPkg/Tcg/TcgPei/TcgPei.inf b/SecurityPkg/Tcg/TcgPei/TcgPei.inf
index c68c905..6a76cf1 100644
--- a/SecurityPkg/Tcg/TcgPei/TcgPei.inf
+++ b/SecurityPkg/Tcg/TcgPei/TcgPei.inf
@@ -4,7 +4,7 @@
 #  This module will initialize TPM device, measure reported FVs and BIOS version.

 #  This module may also lock TPM physical presence and physicalPresenceLifetimeLock.

 #

-# 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

@@ -56,9 +56,11 @@
   BaseLib

   PcdLib

   MemoryAllocationLib

+  ReportStatusCodeLib

 

 [Guids]

   gTcgEventEntryHobGuid                                               ## PRODUCES               ## HOB

+  gTpmErrorHobGuid                                                    ## SOMETIMES_PRODUCES     ## HOB

   gMeasuredFvHobGuid                                                  ## PRODUCES               ## HOB

   gEfiTpmDeviceInstanceTpm12Guid                                      ## PRODUCES               ## GUID       # TPM device identifier

 

@@ -79,6 +81,7 @@
   gEfiSecurityPkgTokenSpaceGuid.PcdTpmInitializationPolicy            ## CONSUMES

   gEfiSecurityPkgTokenSpaceGuid.PcdTpmScrtmPolicy                     ## SOMETIMES_CONSUMES

   gEfiMdeModulePkgTokenSpaceGuid.PcdPeiCoreMaxFvSupported             ## CONSUMES

+  gEfiSecurityPkgTokenSpaceGuid.PcdStatusCodeSubClassTpmDevice        ## SOMETIMES_CONSUMES

 

 [Depex]

   gEfiPeiMasterBootModePpiGuid AND

diff --git a/SecurityPkg/Tcg/TcgPei/TpmComm.c b/SecurityPkg/Tcg/TcgPei/TpmComm.c
index fb5011e..899c8e3 100644
--- a/SecurityPkg/Tcg/TcgPei/TpmComm.c
+++ b/SecurityPkg/Tcg/TcgPei/TpmComm.c
@@ -1,7 +1,7 @@
 /** @file

   Utility functions used by TPM PEI driver.

   

-Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2005 - 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 

@@ -222,7 +222,9 @@
   SendBuffer.PcrIndex       = SwapBytes32 (PcrIndex);

   CopyMem (&SendBuffer.TpmDigest, (UINT8 *)DigestToExtend, sizeof (TPM_DIGEST));

   Status = TisTpmCommand (PeiServices, TpmHandle, (UINT8 *)&SendBuffer, TpmSendSize, RecvBuffer, &TpmRecvSize);

-  ASSERT_EFI_ERROR (Status);

+  if (EFI_ERROR (Status)) {

+    return Status;

+  }

 

   if(NewPcrValue != NULL) {

     CopyMem ((UINT8*)NewPcrValue, &RecvBuffer[10], sizeof (TPM_DIGEST));

diff --git a/SecurityPkg/Tcg/TrEEDxe/TrEEDxe.c b/SecurityPkg/Tcg/TrEEDxe/TrEEDxe.c
index 54e505d..58c4aff 100644
--- a/SecurityPkg/Tcg/TrEEDxe/TrEEDxe.c
+++ b/SecurityPkg/Tcg/TrEEDxe/TrEEDxe.c
@@ -1,7 +1,7 @@
 /** @file

   This module implements TrEE Protocol.

   

-Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2013 - 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 

@@ -48,6 +48,7 @@
 #include <Library/Tpm2DeviceLib.h>

 #include <Library/HashLib.h>

 #include <Library/PerformanceLib.h>

+#include <Library/ReportStatusCodeLib.h>

 

 #define PERF_ID_TREE_DXE  0x3120

 

@@ -64,16 +65,13 @@
 typedef struct {

   EFI_GUID               *EventGuid;

   TREE_EVENT_LOG_FORMAT  LogFormat;

-  UINT32                 BootHashAlg;

-  UINT16                 DigestAlgID;

-  TPMI_ALG_HASH          TpmHashAlgo;

 } TREE_EVENT_INFO_STRUCT;

 

 TREE_EVENT_INFO_STRUCT mTreeEventInfo[] = {

-  {&gTcgEventEntryHobGuid,             TREE_EVENT_LOG_FORMAT_TCG_1_2,      TREE_BOOT_HASH_ALG_SHA1,     0,                       TPM_ALG_SHA1},

+  {&gTcgEventEntryHobGuid,             TREE_EVENT_LOG_FORMAT_TCG_1_2},

 };

 

-#define TCG_EVENT_LOG_AREA_COUNT_MAX   5

+#define TCG_EVENT_LOG_AREA_COUNT_MAX   2

 

 typedef struct {

   TREE_EVENT_LOG_FORMAT             EventLogFormat;

@@ -629,72 +627,6 @@
 }

 

 /**

-  This function return hash algorithm from event log format.

-

-  @param[in]     EventLogFormat    Event log format.

-

-  @return hash algorithm.

-**/

-TPMI_ALG_HASH

-TrEEGetHashAlgoFromLogFormat (

-  IN      TREE_EVENT_LOG_FORMAT     EventLogFormat

-  )

-{

-  UINTN  Index;

-

-  for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {

-    if (mTreeEventInfo[Index].LogFormat == EventLogFormat) {

-      return mTreeEventInfo[Index].TpmHashAlgo;

-    }

-  }

-  return TPM_ALG_SHA1;

-}

-

-/**

-  This function return hash algorithm ID from event log format.

-

-  @param[in]     EventLogFormat    Event log format.

-

-  @return hash algorithm ID.

-**/

-UINT16

-TrEEGetAlgIDFromLogFormat (

-  IN      TREE_EVENT_LOG_FORMAT     EventLogFormat

-  )

-{

-  UINTN  Index;

-

-  for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {

-    if (mTreeEventInfo[Index].LogFormat == EventLogFormat) {

-      return mTreeEventInfo[Index].DigestAlgID;

-    }

-  }

-  return 0;

-}

-

-/**

-  This function return boot hash algorithm from event log format.

-

-  @param[in]     EventLogFormat    Event log format.

-

-  @return boot hash algorithm.

-**/

-UINT32

-TrEEGetBootHashAlgFromLogFormat (

-  IN      TREE_EVENT_LOG_FORMAT     EventLogFormat

-  )

-{

-  UINTN  Index;

-

-  for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {

-    if (mTreeEventInfo[Index].LogFormat == EventLogFormat) {

-      return mTreeEventInfo[Index].BootHashAlg;

-    }

-  }

-  return TREE_BOOT_HASH_ALG_SHA1;

-}

-

-/**

   This function get digest from digest list.

 

   @param HashAlg    digest algorithm

@@ -811,6 +743,10 @@
 {

   EFI_STATUS                        Status;

   TPML_DIGEST_VALUES                DigestList;

+  

+  if (!mTcgDxeData.BsCap.TrEEPresentFlag) {

+    return EFI_DEVICE_ERROR;

+  }

 

   Status = HashAndExtend (

              NewEventHdr->PCRIndex,

@@ -824,6 +760,15 @@
     }

   }

 

+  if (Status == EFI_DEVICE_ERROR) {

+    DEBUG ((EFI_D_ERROR, "TcgDxeHashLogExtendEvent - %r. Disable TPM.\n", Status));

+    mTcgDxeData.BsCap.TrEEPresentFlag = FALSE;

+    REPORT_STATUS_CODE (

+      EFI_ERROR_CODE | EFI_ERROR_MINOR,

+      (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)

+      );

+  }

+

   return Status;

 }

 

@@ -893,6 +838,14 @@
         Status = TcgDxeLogHashEvent (&DigestList, &NewEventHdr, Event->Event);

       }

     }

+    if (Status == EFI_DEVICE_ERROR) {

+      DEBUG ((EFI_D_ERROR, "MeasurePeImageAndExtend - %r. Disable TPM.\n", Status));

+      mTcgDxeData.BsCap.TrEEPresentFlag = FALSE;

+      REPORT_STATUS_CODE (

+        EFI_ERROR_CODE | EFI_ERROR_MINOR,

+        (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)

+        );

+    }

   } else {

     Status = TcgDxeHashLogExtendEvent (

                Flags,

@@ -1614,6 +1567,9 @@
     Status = TcgMeasureAction (

                EFI_CALLING_EFI_APPLICATION

                );

+    if (EFI_ERROR (Status)) {

+      DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_CALLING_EFI_APPLICATION));

+    }

 

     //

     // 2. Draw a line between pre-boot env and entering post-boot env.

@@ -1621,6 +1577,9 @@
     //

     for (PcrIndex = 0; PcrIndex < 7; PcrIndex++) {

       Status = MeasureSeparatorEvent (PcrIndex);

+      if (EFI_ERROR (Status)) {

+        DEBUG ((EFI_D_ERROR, "Seperator Event not Measured. Error!\n"));

+      }

     }

 

     //

@@ -1641,6 +1600,9 @@
     Status = TcgMeasureAction (

                EFI_RETURNING_FROM_EFI_APPLICATOIN

                );

+    if (EFI_ERROR (Status)) {

+      DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_RETURNING_FROM_EFI_APPLICATOIN));

+    }

   }

 

   DEBUG ((EFI_D_INFO, "TPM2 TrEEDxe Measure Data when ReadyToBoot\n"));

@@ -1858,6 +1820,10 @@
     DEBUG ((EFI_D_ERROR, "TPM not detected!\n"));

     return Status;

   }

+  

+  if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {

+    mTcgDxeData.BsCap.TrEEPresentFlag = FALSE;

+  }

 

   //

   // Fill information

diff --git a/SecurityPkg/Tcg/TrEEDxe/TrEEDxe.inf b/SecurityPkg/Tcg/TrEEDxe/TrEEDxe.inf
index ef8505c..e564c8f 100644
--- a/SecurityPkg/Tcg/TrEEDxe/TrEEDxe.inf
+++ b/SecurityPkg/Tcg/TrEEDxe/TrEEDxe.inf
@@ -7,7 +7,7 @@
 #  This external input must be validated carefully to avoid security issue like

 #  buffer overflow, integer overflow.

 #

-# Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>

+# Copyright (c) 2013 - 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

@@ -57,6 +57,7 @@
   Tpm2DeviceLib

   HashLib

   PerformanceLib

+  ReportStatusCodeLib

 

 [Guids]

   ## SOMETIMES_CONSUMES     ## SystemTable           # Smbios Table

@@ -74,6 +75,7 @@
   gEfiImageSecurityDatabaseGuid

     

   gTcgEventEntryHobGuid                              ## SOMETIMES_CONSUMES  ## HOB

+  gTpmErrorHobGuid                                   ## SOMETIMES_CONSUMES  ## HOB

   gEfiEventExitBootServicesGuid                      ## CONSUMES            ## Event

   gEventExitBootServicesFailedGuid                   ## SOMETIMES_CONSUMES  ## Event

   gEfiTpmDeviceInstanceNoneGuid                      ## SOMETIMES_CONSUMES  ## GUID       # TPM device identifier

@@ -95,6 +97,7 @@
   gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiDefaultOemRevision                  ## SOMETIMES_CONSUMES

   gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiDefaultCreatorId                    ## SOMETIMES_CONSUMES

   gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiDefaultCreatorRevision              ## SOMETIMES_CONSUMES

+  gEfiSecurityPkgTokenSpaceGuid.PcdStatusCodeSubClassTpmDevice              ## SOMETIMES_CONSUMES

 

 [Depex]

   TRUE

diff --git a/SecurityPkg/Tcg/TrEEPei/TrEEPei.c b/SecurityPkg/Tcg/TrEEPei/TrEEPei.c
index 8e2bc74..eea4084 100644
--- a/SecurityPkg/Tcg/TrEEPei/TrEEPei.c
+++ b/SecurityPkg/Tcg/TrEEPei/TrEEPei.c
@@ -1,7 +1,7 @@
 /** @file

   Initialize TPM2 device and measure FVs before handing off control to DXE.

 

-Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2013 - 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 

@@ -40,19 +40,17 @@
 #include <Protocol/TrEEProtocol.h>

 #include <Library/PerformanceLib.h>

 #include <Library/MemoryAllocationLib.h>

+#include <Library/ReportStatusCodeLib.h>

 

 #define PERF_ID_TREE_PEI  0x3080

 

 typedef struct {

   EFI_GUID               *EventGuid;

   TREE_EVENT_LOG_FORMAT  LogFormat;

-  UINT32                 BootHashAlg;

-  UINT16                 DigestAlgID;

-  TPMI_ALG_HASH          TpmHashAlgo;

 } TREE_EVENT_INFO_STRUCT;

 

 TREE_EVENT_INFO_STRUCT mTreeEventInfo[] = {

-  {&gTcgEventEntryHobGuid,             TREE_EVENT_LOG_FORMAT_TCG_1_2,      TREE_BOOT_HASH_ALG_SHA1,     0,                       TPM_ALG_SHA1},

+  {&gTcgEventEntryHobGuid,             TREE_EVENT_LOG_FORMAT_TCG_1_2},

 };

 

 BOOLEAN                 mImageInMemory  = FALSE;

@@ -129,28 +127,6 @@
 EFI_PEI_FIRMWARE_VOLUME_INFO_MEASUREMENT_EXCLUDED_PPI *mMeasurementExcludedFvPpi;

 

 /**

-  This function return hash algorithm from event log format.

-

-  @param[in]     EventLogFormat    Event log format.

-

-  @return hash algorithm.

-**/

-TPMI_ALG_HASH

-TrEEGetHashAlgoFromLogFormat (

-  IN      TREE_EVENT_LOG_FORMAT     EventLogFormat

-  )

-{

-  UINTN  Index;

-

-  for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {

-    if (mTreeEventInfo[Index].LogFormat == EventLogFormat) {

-      return mTreeEventInfo[Index].TpmHashAlgo;

-    }

-  }

-  return TPM_ALG_SHA1;

-}

-

-/**

   This function get digest from digest list.

 

   @param HashAlg    digest algorithm

@@ -318,6 +294,10 @@
   EFI_STATUS                        Status;

   TPML_DIGEST_VALUES                DigestList;

 

+  if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {

+    return EFI_DEVICE_ERROR;

+  }

+

   Status = HashAndExtend (

              NewEventHdr->PCRIndex,

              HashData,

@@ -329,6 +309,16 @@
       Status = LogHashEvent (&DigestList, NewEventHdr, NewEventData);

     }

   }

+  

+  if (Status == EFI_DEVICE_ERROR) {

+    DEBUG ((EFI_D_ERROR, "HashLogExtendEvent - %r. Disable TPM.\n", Status));

+    BuildGuidHob (&gTpmErrorHobGuid,0);

+    REPORT_STATUS_CODE (

+      EFI_ERROR_CODE | EFI_ERROR_MINOR,

+      (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)

+      );

+  }

+

   return Status;

 }

 

@@ -431,7 +421,6 @@
              &TcgEventHdr,

              (UINT8*) &FvBlob

              );

-  ASSERT_EFI_ERROR (Status);

 

   //

   // Add new FV into the measured FV list.

@@ -600,7 +589,6 @@
   

   if (PcdGet8 (PcdTpm2ScrtmPolicy) == 1) {

     Status = MeasureCRTMVersion ();

-    ASSERT_EFI_ERROR (Status);

   }

 

   Status = MeasureMainBios ();

diff --git a/SecurityPkg/Tcg/TrEEPei/TrEEPei.inf b/SecurityPkg/Tcg/TrEEPei/TrEEPei.inf
index faa9fb3..4d91dfe 100644
--- a/SecurityPkg/Tcg/TrEEPei/TrEEPei.inf
+++ b/SecurityPkg/Tcg/TrEEPei/TrEEPei.inf
@@ -3,7 +3,7 @@
 #

 #  This module will initialize TPM device, measure reported FVs and BIOS version.

 #

-# Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>

+# Copyright (c) 2013 - 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,11 @@
   HashLib

   PerformanceLib

   MemoryAllocationLib

+  ReportStatusCodeLib

 

 [Guids]

   gTcgEventEntryHobGuid                                                ## PRODUCES               ## HOB

+  gTpmErrorHobGuid                                                     ## SOMETIMES_PRODUCES     ## HOB

   gMeasuredFvHobGuid                                                   ## PRODUCES               ## HOB

   gEfiTpmDeviceInstanceNoneGuid                                        ## SOMETIMES_PRODUCES     ## GUID       # TPM device identifier

   gEfiTpmDeviceInstanceTpm12Guid                                       ## SOMETIMES_PRODUCES     ## GUID       # TPM device identifier

@@ -72,6 +74,7 @@
   gEfiSecurityPkgTokenSpaceGuid.PcdTpm2SelfTestPolicy                  ## SOMETIMES_CONSUMES

   gEfiSecurityPkgTokenSpaceGuid.PcdTpm2ScrtmPolicy                     ## CONSUMES

   gEfiMdeModulePkgTokenSpaceGuid.PcdPeiCoreMaxFvSupported              ## CONSUMES

+  gEfiSecurityPkgTokenSpaceGuid.PcdStatusCodeSubClassTpmDevice         ## SOMETIMES_CONSUMES

 

 [Depex]

   gEfiPeiMasterBootModePpiGuid AND