]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c
Add TPM2 support defined in trusted computing group.
[mirror_edk2.git] / SecurityPkg / Library / DxeTpmMeasurementLib / DxeTpmMeasurementLib.c
index b4732bc6fb50ca82e0f58e4899d2bf34fe362adf..7a2ec7f221e5fd04d01552a81f2223c21a560853 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   This library is used by other modules to measure data to TPM.\r
 \r
-Copyright (c) 2012, Intel Corporation. All rights reserved. <BR>\r
+Copyright (c) 2012 - 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
@@ -15,6 +15,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <PiDxe.h>\r
 \r
 #include <Protocol/TcgService.h>\r
+#include <Protocol/Tcg2Protocol.h>\r
 \r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/MemoryAllocationLib.h>\r
@@ -93,6 +94,67 @@ Tpm12MeasureAndLogData (
   return Status;\r
 }\r
 \r
+/**\r
+  Tpm20 measure and log data, and extend the measurement result into a specific PCR.\r
+\r
+  @param[in]  PcrIndex         PCR Index.\r
+  @param[in]  EventType        Event type.\r
+  @param[in]  EventLog         Measurement event log.\r
+  @param[in]  LogLen           Event log length in bytes.\r
+  @param[in]  HashData         The start of the data buffer to be hashed, extended.\r
+  @param[in]  HashDataLen      The length, in bytes, of the buffer referenced by HashData\r
+\r
+  @retval EFI_SUCCESS           Operation completed successfully.\r
+  @retval EFI_UNSUPPORTED       TPM device not available.\r
+  @retval EFI_OUT_OF_RESOURCES  Out of memory.\r
+  @retval EFI_DEVICE_ERROR      The operation was unsuccessful.\r
+**/\r
+EFI_STATUS\r
+Tpm20MeasureAndLogData (\r
+  IN UINT32             PcrIndex,\r
+  IN UINT32             EventType,\r
+  IN VOID               *EventLog,\r
+  IN UINT32             LogLen,\r
+  IN VOID               *HashData,\r
+  IN UINT64             HashDataLen\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+  EFI_TCG2_PROTOCOL         *Tcg2Protocol;\r
+  EFI_TCG2_EVENT            *Tcg2Event;\r
+\r
+  //\r
+  // TPMPresentFlag is checked in HashLogExtendEvent\r
+  //\r
+  Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **) &Tcg2Protocol);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Tcg2Event = (EFI_TCG2_EVENT *) AllocateZeroPool (LogLen + sizeof (EFI_TCG2_EVENT));\r
+  if(Tcg2Event == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  Tcg2Event->Size = (UINT32)LogLen + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event);\r
+  Tcg2Event->Header.HeaderSize    = sizeof(EFI_TCG2_EVENT_HEADER);\r
+  Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;\r
+  Tcg2Event->Header.PCRIndex      = PcrIndex;\r
+  Tcg2Event->Header.EventType     = EventType;\r
+  CopyMem (&Tcg2Event->Event[0], EventLog, LogLen);\r
+\r
+  Status = Tcg2Protocol->HashLogExtendEvent (\r
+                           Tcg2Protocol,\r
+                           0,\r
+                           (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,\r
+                           HashDataLen,\r
+                           Tcg2Event\r
+                           );\r
+  FreePool (Tcg2Event);\r
+\r
+  return Status;\r
+}\r
+\r
 /**\r
   Tpm measure and log data, and extend the measurement result into a specific PCR.\r
 \r
@@ -132,6 +194,19 @@ TpmMeasureAndLogData (
                HashData,\r
                HashDataLen\r
                );\r
+  if (EFI_ERROR (Status)) {\r
+    //\r
+    // Try to measure using Tpm20 protocol\r
+    //\r
+    Status = Tpm20MeasureAndLogData(\r
+               PcrIndex,\r
+               EventType,\r
+               EventLog,\r
+               LogLen,\r
+               HashData,\r
+               HashDataLen\r
+               );\r
+  }\r
 \r
   return Status;\r
 }\r