]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c
1.Measure ACPI table data comes from flash event type EV_POST_CODE ACPI DATA to PCR[0]
[mirror_edk2.git] / SecurityPkg / Library / DxeTpmMeasurementLib / DxeTpmMeasurementLib.c
diff --git a/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c b/SecurityPkg/Library/DxeTpmMeasurementLib/DxeTpmMeasurementLib.c
new file mode 100644 (file)
index 0000000..310ebfc
--- /dev/null
@@ -0,0 +1,139 @@
+/** @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
+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
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <PiDxe.h>\r
+\r
+#include <Protocol/TcgService.h>\r
+\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/TpmMeasurementLib.h>\r
+\r
+#include <Guid/Acpi.h>\r
+#include <IndustryStandard/Acpi.h>\r
+\r
+\r
+\r
+/**\r
+  Tpm12 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
+\r
+EFI_STATUS\r
+Tpm12MeasureAndLogData (\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_TCG_PROTOCOL          *TcgProtocol;\r
+  TCG_PCR_EVENT             *TcgEvent;\r
+  EFI_PHYSICAL_ADDRESS      EventLogLastEntry;\r
+  UINT32                    EventNumber;\r
+\r
+  TcgEvent = NULL;\r
+\r
+  //\r
+  // Tpm active/deactive state is checked in HashLogExtendEvent\r
+  //\r
+  Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **) &TcgProtocol);\r
+  if (EFI_ERROR(Status)){\r
+    return Status;\r
+  }\r
+\r
+  TcgEvent = (TCG_PCR_EVENT *)AllocateZeroPool (sizeof (TCG_PCR_EVENT_HDR) + LogLen);\r
+  if(TcgEvent == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  TcgEvent->PCRIndex  = PcrIndex;\r
+  TcgEvent->EventType = EventType;\r
+  TcgEvent->EventSize = LogLen;\r
+  CopyMem (&TcgEvent->Event[0], EventLog, LogLen);\r
+  EventNumber = 1;\r
+  Status = TcgProtocol->HashLogExtendEvent (\r
+                          TcgProtocol,\r
+                          (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,\r
+                          HashDataLen,\r
+                          TPM_ALG_SHA,\r
+                          TcgEvent,\r
+                          &EventNumber,\r
+                          &EventLogLastEntry\r
+                          );\r
+\r
+  FreePool (TcgEvent);\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Tpm 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
+\r
+EFI_STATUS\r
+EFIAPI \r
+TpmMeasureAndLogData (\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
+\r
+  //\r
+  // Try to measure using Tpm1.2 protocol\r
+  //\r
+  Status = Tpm12MeasureAndLogData(\r
+               PcrIndex,\r
+               EventType,\r
+               EventLog,\r
+               LogLen,\r
+               HashData,\r
+               HashDataLen\r
+               );\r
+\r
+  return Status;\r
+}\r