]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Tcg/TrEEDxe/TrEEDxe.c
Merge branch 'master' of https://github.com/tianocore/edk2
[mirror_edk2.git] / SecurityPkg / Tcg / TrEEDxe / TrEEDxe.c
index 2f159e0c6b0a63127bf8eaa34388b22163c98596..a30cd5161d40210ac297124bd13be644c5b52ad1 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 - 2016, 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
@@ -15,11 +15,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <PiDxe.h>\r
 #include <IndustryStandard/Acpi.h>\r
 #include <IndustryStandard/PeImage.h>\r
-#include <IndustryStandard/SmBios.h>\r
 #include <IndustryStandard/TcpaAcpi.h>\r
 \r
 #include <Guid/GlobalVariable.h>\r
-#include <Guid/SmBios.h>\r
 #include <Guid/HobList.h>\r
 #include <Guid/TcgEventHob.h>\r
 #include <Guid/EventGroup.h>\r
@@ -48,6 +46,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
@@ -56,24 +55,19 @@ typedef struct {
   EFI_GUID                               *VendorGuid;\r
 } VARIABLE_TYPE;\r
 \r
-#define  EFI_TCG_LOG_AREA_SIZE        0x10000\r
-\r
 #define  TREE_DEFAULT_MAX_COMMAND_SIZE        0x1000\r
 #define  TREE_DEFAULT_MAX_RESPONSE_SIZE       0x1000\r
 \r
 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 +622,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 +739,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 +756,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 +834,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
@@ -944,10 +893,10 @@ TreeSubmitCommand (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  if (InputParameterBlockSize >= mTcgDxeData.BsCap.MaxCommandSize) {\r
+  if (InputParameterBlockSize > mTcgDxeData.BsCap.MaxCommandSize) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  if (OutputParameterBlockSize >= mTcgDxeData.BsCap.MaxResponseSize) {\r
+  if (OutputParameterBlockSize > mTcgDxeData.BsCap.MaxResponseSize) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -998,19 +947,19 @@ SetupEventLog (
       Status = gBS->AllocatePages (\r
                       AllocateMaxAddress,\r
                       EfiACPIMemoryNVS,\r
-                      EFI_SIZE_TO_PAGES (EFI_TCG_LOG_AREA_SIZE),\r
+                      EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcgLogAreaMinLen)),\r
                       &Lasa\r
                       );\r
       if (EFI_ERROR (Status)) {\r
         return Status;\r
       }\r
       mTcgDxeData.EventLogAreaStruct[Index].Lasa = Lasa;\r
-      mTcgDxeData.EventLogAreaStruct[Index].Laml = EFI_TCG_LOG_AREA_SIZE;\r
+      mTcgDxeData.EventLogAreaStruct[Index].Laml = PcdGet32 (PcdTcgLogAreaMinLen);\r
       //\r
       // To initialize them as 0xFF is recommended \r
       // because the OS can know the last entry for that.\r
       //\r
-      SetMem ((VOID *)(UINTN)Lasa, EFI_TCG_LOG_AREA_SIZE, 0xFF);\r
+      SetMem ((VOID *)(UINTN)Lasa, PcdGet32 (PcdTcgLogAreaMinLen), 0xFF);\r
   }\r
 \r
   //\r
@@ -1018,10 +967,10 @@ SetupEventLog (
   //\r
     if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {\r
       mTcgClientAcpiTemplate.Lasa = mTcgDxeData.EventLogAreaStruct[0].Lasa;\r
-      mTcgClientAcpiTemplate.Laml = EFI_TCG_LOG_AREA_SIZE;\r
+      mTcgClientAcpiTemplate.Laml = PcdGet32 (PcdTcgLogAreaMinLen);\r
     } else {\r
       mTcgServerAcpiTemplate.Lasa = mTcgDxeData.EventLogAreaStruct[0].Lasa;\r
-      mTcgServerAcpiTemplate.Laml = EFI_TCG_LOG_AREA_SIZE;\r
+      mTcgServerAcpiTemplate.Laml = PcdGet32 (PcdTcgLogAreaMinLen);\r
     }\r
 \r
   //\r
@@ -1093,42 +1042,13 @@ MeasureHandoffTables (
   )\r
 {\r
   EFI_STATUS                        Status;\r
-  SMBIOS_TABLE_ENTRY_POINT          *SmbiosTable;\r
   TCG_PCR_EVENT_HDR                 TcgEvent;\r
   EFI_HANDOFF_TABLE_POINTERS        HandoffTables;\r
   UINTN                             ProcessorNum;\r
   EFI_CPU_PHYSICAL_LOCATION         *ProcessorLocBuf;\r
 \r
   ProcessorLocBuf = NULL;\r
-\r
-  //\r
-  // Measure SMBIOS with EV_EFI_HANDOFF_TABLES to PCR[1]\r
-  //\r
-  Status = EfiGetSystemConfigurationTable (\r
-             &gEfiSmbiosTableGuid,\r
-             (VOID **) &SmbiosTable\r
-             );\r
-\r
-  if (!EFI_ERROR (Status) && SmbiosTable != NULL) {\r
-    TcgEvent.PCRIndex  = 1;\r
-    TcgEvent.EventType = EV_EFI_HANDOFF_TABLES;\r
-    TcgEvent.EventSize = sizeof (HandoffTables);\r
-\r
-    HandoffTables.NumberOfTables = 1;\r
-    HandoffTables.TableEntry[0].VendorGuid  = gEfiSmbiosTableGuid;\r
-    HandoffTables.TableEntry[0].VendorTable = SmbiosTable;\r
-\r
-    DEBUG ((DEBUG_INFO, "The Smbios Table starts at: 0x%x\n", SmbiosTable->TableAddress));\r
-    DEBUG ((DEBUG_INFO, "The Smbios Table size: 0x%x\n", SmbiosTable->TableLength));\r
-\r
-    Status = TcgDxeHashLogExtendEvent (\r
-               0,\r
-               (UINT8*)(UINTN)SmbiosTable->TableAddress,\r
-               SmbiosTable->TableLength,\r
-               &TcgEvent,\r
-               (UINT8*)&HandoffTables\r
-               );\r
-  }\r
+  Status = EFI_SUCCESS;\r
 \r
   if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_SERVER) {\r
     //\r
@@ -1245,7 +1165,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
@@ -1253,26 +1173,14 @@ MeasureVariable (
        );\r
   }\r
 \r
-  if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {\r
-    //\r
-    // Digest is the event data (EFI_VARIABLE_DATA_TREE)\r
-    //\r
-    Status = TcgDxeHashLogExtendEvent (\r
-               0,\r
-               (UINT8*)VarLog,\r
-               TcgEvent.EventSize,\r
-               &TcgEvent,\r
-               (UINT8*)VarLog\r
-               );\r
-  } else {\r
-    Status = TcgDxeHashLogExtendEvent (\r
-               0,\r
-               (UINT8*)VarData,\r
-               VarSize,\r
-               &TcgEvent,\r
-               (UINT8*)VarLog\r
-               );\r
-  }\r
+  Status = TcgDxeHashLogExtendEvent (\r
+             0,\r
+             (UINT8*)VarLog,\r
+             TcgEvent.EventSize,\r
+             &TcgEvent,\r
+             (UINT8*)VarLog\r
+             );\r
+\r
   FreePool (VarLog);\r
   return Status;\r
 }\r
@@ -1423,7 +1331,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 +1522,9 @@ OnReadyToBoot (
     Status = TcgMeasureAction (\r
                EFI_CALLING_EFI_APPLICATION\r
                );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "%a 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 +1532,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 +1555,9 @@ OnReadyToBoot (
     Status = TcgMeasureAction (\r
                EFI_RETURNING_FROM_EFI_APPLICATOIN\r
                );\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "%a 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
@@ -1752,7 +1669,7 @@ OnExitBootServices (
              EFI_EXIT_BOOT_SERVICES_INVOCATION\r
              );\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_INVOCATION));\r
+    DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_INVOCATION));\r
   }\r
 \r
   //\r
@@ -1762,7 +1679,7 @@ OnExitBootServices (
              EFI_EXIT_BOOT_SERVICES_SUCCEEDED\r
              );\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_SUCCEEDED));\r
+    DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_SUCCEEDED));\r
   }\r
 }\r
 \r
@@ -1791,7 +1708,7 @@ OnExitBootServicesFailed (
              EFI_EXIT_BOOT_SERVICES_FAILED\r
              );\r
   if (EFI_ERROR (Status)) {\r
-    DEBUG ((EFI_D_ERROR, "%s not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_FAILED));\r
+    DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_FAILED));\r
   }\r
 \r
 }\r
@@ -1853,12 +1770,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