]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Tcg/TcgDxe/TcgDxe.c
SecurityPkg: TcgDxe,Tcg2Dxe,TrEEDxe: New PCD for TCG event log and TCG2 final event...
[mirror_edk2.git] / SecurityPkg / Tcg / TcgDxe / TcgDxe.c
index df9700a4d975c674f3ad24cbcfbd2c05960128d4..1e52179885c2d59e4f7d40442b93ed12186846b5 100644 (file)
@@ -8,7 +8,7 @@ buffer overflow, integer overflow.
 \r
 TcgDxePassThroughToTpm() will receive untrusted input and do basic validation.\r
 \r
-Copyright (c) 2005 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2005 - 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
@@ -23,11 +23,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <IndustryStandard/Tpm12.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
@@ -51,11 +49,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/TpmCommLib.h>\r
 #include <Library/PcdLib.h>\r
 #include <Library/UefiLib.h>\r
+#include <Library/ReportStatusCodeLib.h>\r
 \r
 #include "TpmComm.h"\r
 \r
-#define  EFI_TCG_LOG_AREA_SIZE        0x10000\r
-\r
 #define TCG_DXE_DATA_FROM_THIS(this)  \\r
   BASE_CR (this, TCG_DXE_DATA, TcgProtocol)\r
 \r
@@ -264,7 +261,7 @@ TcgDxeStatusCheck (
   }\r
 \r
   if (EventLogLastEntry != NULL) {\r
-    if (TcgData->BsCap.TPMDeactivatedFlag) {\r
+    if (TcgData->BsCap.TPMDeactivatedFlag || (!TcgData->BsCap.TPMPresentFlag)) {\r
       *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)0;\r
     } else {\r
       *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)TcgData->LastEvent;\r
@@ -411,7 +408,7 @@ TcgDxeLogEvent (
 \r
   TcgData = TCG_DXE_DATA_FROM_THIS (This);\r
   \r
-  if (TcgData->BsCap.TPMDeactivatedFlag) {\r
+  if (TcgData->BsCap.TPMDeactivatedFlag || (!TcgData->BsCap.TPMPresentFlag)) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
   return TcgDxeLogEventI (\r
@@ -495,8 +492,8 @@ TcgDxeHashLogExtendEventI (
 {\r
   EFI_STATUS                        Status;\r
 \r
-  if (HashData == NULL && HashDataLen > 0) {\r
-    return EFI_INVALID_PARAMETER;\r
+  if (!TcgData->BsCap.TPMPresentFlag) {\r
+    return EFI_DEVICE_ERROR;\r
   }\r
 \r
   if (HashDataLen > 0 || HashData != NULL) {\r
@@ -505,7 +502,10 @@ TcgDxeHashLogExtendEventI (
                (UINTN) HashDataLen,\r
                &NewEventHdr->Digest\r
                );\r
-    ASSERT_EFI_ERROR (Status);\r
+    if (EFI_ERROR(Status)) {\r
+      DEBUG ((DEBUG_ERROR, "TpmCommHashAll Failed. %x\n", Status));\r
+      goto Done;\r
+    }\r
   }\r
 \r
   Status = TpmCommExtend (\r
@@ -518,6 +518,17 @@ TcgDxeHashLogExtendEventI (
     Status = TcgDxeLogEventI (TcgData, NewEventHdr, NewEventData);\r
   }\r
 \r
+Done:\r
+  if ((Status == EFI_DEVICE_ERROR) || (Status == EFI_TIMEOUT)) {\r
+    DEBUG ((EFI_D_ERROR, "TcgDxeHashLogExtendEventI - %r. Disable TPM.\n", Status));\r
+    TcgData->BsCap.TPMPresentFlag = FALSE;\r
+    REPORT_STATUS_CODE (\r
+      EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
+      (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)\r
+      );\r
+    Status = EFI_DEVICE_ERROR;\r
+  }\r
+\r
   return Status;\r
 }\r
 \r
@@ -566,13 +577,17 @@ TcgDxeHashLogExtendEvent (
 \r
   TcgData = TCG_DXE_DATA_FROM_THIS (This);\r
   \r
-  if (TcgData->BsCap.TPMDeactivatedFlag) {\r
+  if (TcgData->BsCap.TPMDeactivatedFlag || (!TcgData->BsCap.TPMPresentFlag)) {\r
     return EFI_DEVICE_ERROR;\r
   }\r
     \r
   if (AlgorithmId != TPM_ALG_SHA) {\r
     return EFI_UNSUPPORTED;\r
   }\r
+  \r
+  if (HashData == 0 && HashDataLen > 0) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
 \r
   Status = TcgDxeHashLogExtendEventI (\r
              TcgData,\r
@@ -636,7 +651,7 @@ 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
@@ -647,8 +662,8 @@ SetupEventLog (
     // To initialize them as 0xFF is recommended \r
     // because the OS can know the last entry for that.\r
     //\r
-    SetMem ((VOID *)(UINTN)mTcgClientAcpiTemplate.Lasa, EFI_TCG_LOG_AREA_SIZE, 0xFF);\r
-    mTcgClientAcpiTemplate.Laml = EFI_TCG_LOG_AREA_SIZE;\r
+    SetMem ((VOID *)(UINTN)mTcgClientAcpiTemplate.Lasa, PcdGet32 (PcdTcgLogAreaMinLen), 0xFF);\r
+    mTcgClientAcpiTemplate.Laml = PcdGet32 (PcdTcgLogAreaMinLen);\r
   \r
   } else {\r
     Lasa = mTcgServerAcpiTemplate.Lasa;\r
@@ -656,7 +671,7 @@ 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
@@ -667,8 +682,8 @@ SetupEventLog (
     // To initialize them as 0xFF is recommended \r
     // because the OS can know the last entry for that.\r
     //\r
-    SetMem ((VOID *)(UINTN)mTcgServerAcpiTemplate.Lasa, EFI_TCG_LOG_AREA_SIZE, 0xFF);\r
-    mTcgServerAcpiTemplate.Laml = EFI_TCG_LOG_AREA_SIZE;\r
+    SetMem ((VOID *)(UINTN)mTcgServerAcpiTemplate.Lasa, PcdGet32 (PcdTcgLogAreaMinLen), 0xFF);\r
+    mTcgServerAcpiTemplate.Laml = PcdGet32 (PcdTcgLogAreaMinLen);\r
   }\r
 \r
   GuidHob.Raw = GetHobList ();\r
@@ -729,44 +744,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)) {\r
-    ASSERT (SmbiosTable != NULL);\r
-\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 = TcgDxeHashLogExtendEventI (\r
-               &mTcgDxeData,\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
@@ -942,8 +926,8 @@ MeasureVariable (
 \r
   Status = TcgDxeHashLogExtendEventI (\r
              &mTcgDxeData,\r
-             (UINT8*)VarData,\r
-             VarSize,\r
+             (UINT8*)VarLog,\r
+             TcgEvent.EventSize,\r
              &TcgEvent,\r
              (UINT8*)VarLog\r
              );\r
@@ -1020,12 +1004,14 @@ 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
-  ASSERT (BootOrder != NULL);\r
 \r
   if (EFI_ERROR (Status)) {\r
+    //\r
+    // BootOrder can't be NULL if status is not EFI_NOT_FOUND\r
+    //\r
     FreePool (BootOrder);\r
     return Status;\r
   }\r
@@ -1091,14 +1077,18 @@ OnReadyToBoot (
     Status = TcgMeasureAction (\r
                EFI_CALLING_EFI_APPLICATION\r
                );\r
-    ASSERT_EFI_ERROR (Status);\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
     //\r
     for (PcrIndex = 0; PcrIndex < 8; PcrIndex++) {\r
       Status = MeasureSeparatorEvent (PcrIndex);\r
-      ASSERT_EFI_ERROR (Status);\r
+      if (EFI_ERROR (Status)) {\r
+        DEBUG ((EFI_D_ERROR, "Seperator Event not Measured. Error!\n"));\r
+      }\r
     }\r
 \r
     //\r
@@ -1119,7 +1109,9 @@ OnReadyToBoot (
     Status = TcgMeasureAction (\r
                EFI_RETURNING_FROM_EFI_APPLICATOIN\r
                );\r
-    ASSERT_EFI_ERROR (Status);\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, "TPM TcgDxe Measure Data when ReadyToBoot\n"));\r
@@ -1198,7 +1190,10 @@ InstallAcpiTable (
                             &TableKey\r
                             );\r
   }\r
-  ASSERT_EFI_ERROR (Status);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG((EFI_D_ERROR, "Tcg Acpi Table installation failure"));\r
+  }\r
 }\r
 \r
 /**\r
@@ -1225,7 +1220,9 @@ OnExitBootServices (
   Status = TcgMeasureAction (\r
              EFI_EXIT_BOOT_SERVICES_INVOCATION\r
              );\r
-  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_INVOCATION));\r
+  }\r
 \r
   //\r
   // Measure success of ExitBootServices\r
@@ -1233,7 +1230,9 @@ OnExitBootServices (
   Status = TcgMeasureAction (\r
              EFI_EXIT_BOOT_SERVICES_SUCCEEDED\r
              );\r
-  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status)){\r
+    DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_SUCCEEDED));\r
+  }\r
 }\r
 \r
 /**\r
@@ -1260,8 +1259,9 @@ OnExitBootServicesFailed (
   Status = TcgMeasureAction (\r
              EFI_EXIT_BOOT_SERVICES_FAILED\r
              );\r
-  ASSERT_EFI_ERROR (Status);\r
-\r
+  if (EFI_ERROR (Status)){\r
+    DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_FAILED));\r
+  }\r
 }\r
 \r
 /**\r
@@ -1322,6 +1322,11 @@ DriverEntry (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {\r
+    DEBUG ((EFI_D_ERROR, "TPM error!\n"));\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
   mTcgDxeData.TpmHandle = (TIS_TPM_HANDLE)(UINTN)TPM_BASE_ADDRESS;\r
   Status = TisPcRequestUseTpm (mTcgDxeData.TpmHandle);\r
   if (EFI_ERROR (Status)) {\r
@@ -1346,7 +1351,7 @@ DriverEntry (
                   EFI_NATIVE_INTERFACE,\r
                   &mTcgDxeData.TcgProtocol\r
                   );\r
-  if (!EFI_ERROR (Status) && !mTcgDxeData.BsCap.TPMDeactivatedFlag) {\r
+  if (!EFI_ERROR (Status) && (!mTcgDxeData.BsCap.TPMDeactivatedFlag) && mTcgDxeData.BsCap.TPMPresentFlag) {\r
     //\r
     // Setup the log area and copy event log from hob list to it\r
     //\r