]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Tcg/TrEEDxe/TrEEDxe.c
Add performance optimization for Tcg/TrEE.
[mirror_edk2.git] / SecurityPkg / Tcg / TrEEDxe / TrEEDxe.c
index 2f159e0c6b0a63127bf8eaa34388b22163c98596..6126bd8761a3def0eaf7ea8ef26494f0ee60ae2d 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   This module implements TrEE Protocol.\r
   \r
-Copyright (c) 2013 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2013 - 2015, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials \r
 are licensed and made available under the terms and conditions of the BSD License \r
 which accompanies this distribution.  The full text of the license may be found at \r
@@ -48,6 +48,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/Tpm2DeviceLib.h>\r
 #include <Library/HashLib.h>\r
 #include <Library/PerformanceLib.h>\r
+#include <Library/ReportStatusCodeLib.h>\r
 \r
 #define PERF_ID_TREE_DXE  0x3120\r
 \r
@@ -64,16 +65,13 @@ typedef struct {
 typedef struct {\r
   EFI_GUID               *EventGuid;\r
   TREE_EVENT_LOG_FORMAT  LogFormat;\r
-  UINT32                 BootHashAlg;\r
-  UINT16                 DigestAlgID;\r
-  TPMI_ALG_HASH          TpmHashAlgo;\r
 } TREE_EVENT_INFO_STRUCT;\r
 \r
 TREE_EVENT_INFO_STRUCT mTreeEventInfo[] = {\r
-  {&gTcgEventEntryHobGuid,             TREE_EVENT_LOG_FORMAT_TCG_1_2,      TREE_BOOT_HASH_ALG_SHA1,     0,                       TPM_ALG_SHA1},\r
+  {&gTcgEventEntryHobGuid,             TREE_EVENT_LOG_FORMAT_TCG_1_2},\r
 };\r
 \r
-#define TCG_EVENT_LOG_AREA_COUNT_MAX   5\r
+#define TCG_EVENT_LOG_AREA_COUNT_MAX   2\r
 \r
 typedef struct {\r
   TREE_EVENT_LOG_FORMAT             EventLogFormat;\r
@@ -628,72 +626,6 @@ TcgDxeLogEvent (
   return Status;\r
 }\r
 \r
-/**\r
-  This function return hash algorithm from event log format.\r
-\r
-  @param[in]     EventLogFormat    Event log format.\r
-\r
-  @return hash algorithm.\r
-**/\r
-TPMI_ALG_HASH\r
-TrEEGetHashAlgoFromLogFormat (\r
-  IN      TREE_EVENT_LOG_FORMAT     EventLogFormat\r
-  )\r
-{\r
-  UINTN  Index;\r
-\r
-  for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {\r
-    if (mTreeEventInfo[Index].LogFormat == EventLogFormat) {\r
-      return mTreeEventInfo[Index].TpmHashAlgo;\r
-    }\r
-  }\r
-  return TPM_ALG_SHA1;\r
-}\r
-\r
-/**\r
-  This function return hash algorithm ID from event log format.\r
-\r
-  @param[in]     EventLogFormat    Event log format.\r
-\r
-  @return hash algorithm ID.\r
-**/\r
-UINT16\r
-TrEEGetAlgIDFromLogFormat (\r
-  IN      TREE_EVENT_LOG_FORMAT     EventLogFormat\r
-  )\r
-{\r
-  UINTN  Index;\r
-\r
-  for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {\r
-    if (mTreeEventInfo[Index].LogFormat == EventLogFormat) {\r
-      return mTreeEventInfo[Index].DigestAlgID;\r
-    }\r
-  }\r
-  return 0;\r
-}\r
-\r
-/**\r
-  This function return boot hash algorithm from event log format.\r
-\r
-  @param[in]     EventLogFormat    Event log format.\r
-\r
-  @return boot hash algorithm.\r
-**/\r
-UINT32\r
-TrEEGetBootHashAlgFromLogFormat (\r
-  IN      TREE_EVENT_LOG_FORMAT     EventLogFormat\r
-  )\r
-{\r
-  UINTN  Index;\r
-\r
-  for (Index = 0; Index < sizeof(mTreeEventInfo)/sizeof(mTreeEventInfo[0]); Index++) {\r
-    if (mTreeEventInfo[Index].LogFormat == EventLogFormat) {\r
-      return mTreeEventInfo[Index].BootHashAlg;\r
-    }\r
-  }\r
-  return TREE_BOOT_HASH_ALG_SHA1;\r
-}\r
-\r
 /**\r
   This function get digest from digest list.\r
 \r
@@ -811,6 +743,10 @@ TcgDxeHashLogExtendEvent (
 {\r
   EFI_STATUS                        Status;\r
   TPML_DIGEST_VALUES                DigestList;\r
+  \r
+  if (!mTcgDxeData.BsCap.TrEEPresentFlag) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
 \r
   Status = HashAndExtend (\r
              NewEventHdr->PCRIndex,\r
@@ -824,6 +760,15 @@ TcgDxeHashLogExtendEvent (
     }\r
   }\r
 \r
+  if (Status == EFI_DEVICE_ERROR) {\r
+    DEBUG ((EFI_D_ERROR, "TcgDxeHashLogExtendEvent - %r. Disable TPM.\n", Status));\r
+    mTcgDxeData.BsCap.TrEEPresentFlag = FALSE;\r
+    REPORT_STATUS_CODE (\r
+      EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
+      (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)\r
+      );\r
+  }\r
+\r
   return Status;\r
 }\r
 \r
@@ -893,6 +838,14 @@ TreeHashLogExtendEvent (
         Status = TcgDxeLogHashEvent (&DigestList, &NewEventHdr, Event->Event);\r
       }\r
     }\r
+    if (Status == EFI_DEVICE_ERROR) {\r
+      DEBUG ((EFI_D_ERROR, "MeasurePeImageAndExtend - %r. Disable TPM.\n", Status));\r
+      mTcgDxeData.BsCap.TrEEPresentFlag = FALSE;\r
+      REPORT_STATUS_CODE (\r
+        EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
+        (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)\r
+        );\r
+    }\r
   } else {\r
     Status = TcgDxeHashLogExtendEvent (\r
                Flags,\r
@@ -1245,7 +1198,7 @@ MeasureVariable (
      VarName,\r
      VarNameLength * sizeof (*VarName)\r
      );\r
-  if (VarSize != 0) {\r
+  if (VarSize != 0 && VarData != NULL) {\r
     CopyMem (\r
        (CHAR16 *)VarLog->UnicodeName + VarNameLength,\r
        VarData,\r
@@ -1423,7 +1376,7 @@ MeasureAllBootVariables (
              &BootCount,\r
              (VOID **) &BootOrder\r
              );\r
-  if (Status == EFI_NOT_FOUND) {\r
+  if (Status == EFI_NOT_FOUND || BootOrder == NULL) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
@@ -1614,6 +1567,9 @@ OnReadyToBoot (
     Status = TcgMeasureAction (\r
                EFI_CALLING_EFI_APPLICATION\r
                );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_CALLING_EFI_APPLICATION));\r
+    }\r
 \r
     //\r
     // 2. Draw a line between pre-boot env and entering post-boot env.\r
@@ -1621,6 +1577,9 @@ OnReadyToBoot (
     //\r
     for (PcrIndex = 0; PcrIndex < 7; PcrIndex++) {\r
       Status = MeasureSeparatorEvent (PcrIndex);\r
+      if (EFI_ERROR (Status)) {\r
+        DEBUG ((EFI_D_ERROR, "Seperator Event not Measured. Error!\n"));\r
+      }\r
     }\r
 \r
     //\r
@@ -1641,6 +1600,9 @@ OnReadyToBoot (
     Status = TcgMeasureAction (\r
                EFI_RETURNING_FROM_EFI_APPLICATOIN\r
                );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_RETURNING_FROM_EFI_APPLICATOIN));\r
+    }\r
   }\r
 \r
   DEBUG ((EFI_D_INFO, "TPM2 TrEEDxe Measure Data when ReadyToBoot\n"));\r
@@ -1853,12 +1815,17 @@ DriverEntry (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {\r
+    DEBUG ((EFI_D_ERROR, "TPM2 error!\n"));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+  \r
   Status = Tpm2RequestUseTpm ();\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "TPM not detected!\n"));\r
+    DEBUG ((EFI_D_ERROR, "TPM2 not detected!\n"));\r
     return Status;\r
   }\r
-\r
+  \r
   //\r
   // Fill information\r
   //\r