]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/Tcg/Tcg2Config/Tpm12Support.c
OvmfPkg/Tcg2ConfigPei: factor out InternalTpm12Detect()
[mirror_edk2.git] / OvmfPkg / Tcg / Tcg2Config / Tpm12Support.c
diff --git a/OvmfPkg/Tcg/Tcg2Config/Tpm12Support.c b/OvmfPkg/Tcg/Tcg2Config/Tpm12Support.c
new file mode 100644 (file)
index 0000000..4f5a775
--- /dev/null
@@ -0,0 +1,79 @@
+/** @file\r
+  Implement the InternalTpm12Detect() function on top of the Tpm12DeviceLib\r
+  class.\r
+\r
+  Copyright (C) 2020, Red Hat, Inc.\r
+\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/Tpm12DeviceLib.h>\r
+\r
+#include "Tpm12Support.h"\r
+\r
+#pragma pack (1)\r
+typedef struct {\r
+  TPM_RSP_COMMAND_HDR   Hdr;\r
+  TPM_CURRENT_TICKS     CurrentTicks;\r
+} TPM_RSP_GET_TICKS;\r
+#pragma pack ()\r
+\r
+/**\r
+  Probe for the TPM for 1.2 version, by sending TPM1.2 GetTicks\r
+\r
+  Sending a TPM1.2 command to a TPM2 should return a TPM1.2\r
+  header (tag = 0xc4) and error code (TPM_BADTAG = 0x1e)\r
+\r
+  @retval EFI_SUCCESS  TPM version 1.2 probing successful.\r
+\r
+  @return              Error codes propagated from Tpm12SubmitCommand().\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+TestTpm12 (\r
+  )\r
+{\r
+  EFI_STATUS           Status;\r
+  TPM_RQU_COMMAND_HDR  Command;\r
+  TPM_RSP_GET_TICKS    Response;\r
+  UINT32               Length;\r
+\r
+  Command.tag       = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
+  Command.paramSize = SwapBytes32 (sizeof (Command));\r
+  Command.ordinal   = SwapBytes32 (TPM_ORD_GetTicks);\r
+\r
+  Length = sizeof (Response);\r
+  Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length,\r
+             (UINT8 *)&Response);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Detect the presence of a TPM with interface version 1.2.\r
+\r
+  @retval EFI_SUCCESS      TPM-1.2 available. The Tpm12RequestUseTpm() and\r
+                           Tpm12SubmitCommand(TPM_ORD_GetTicks) operations\r
+                           (from the Tpm12DeviceLib class) have succeeded.\r
+\r
+  @return                  Error codes propagated from Tpm12RequestUseTpm() and\r
+                           Tpm12SubmitCommand().\r
+**/\r
+EFI_STATUS\r
+InternalTpm12Detect (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+\r
+  Status = Tpm12RequestUseTpm ();\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  return TestTpm12 ();\r
+}\r