SecurityPkg/Tpm12CommandLib: Add TPM 1.2 commands used by TCG modules
authorMichael Kinney <michael.d.kinney@intel.com>
Thu, 21 Jan 2016 19:30:05 +0000 (19:30 +0000)
committermdkinney <mdkinney@Edk2>
Thu, 21 Jan 2016 19:30:05 +0000 (19:30 +0000)
Add the following APIs that are required by TcgPei and/or TcgDxe to
the Tpm12CommandLib instance:
  Tpm12Extend()
  Tpm12PhysicalPresence()
  Tpm12ContinueSelfTest()
  Tpm12GetCapabilityFlagPermanent()
  Tpm12GetCapabilityFlagVolatile()

Cc: Chao Zhang <chao.b.zhang@intel.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Michael Kinney <michael.d.kinney@intel.com>
Reviewed-by: Chao Zhang <chao.b.zhang@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@19727 6f19259b-4bc3-4df7-8a09-765794883524

SecurityPkg/Library/Tpm12CommandLib/Tpm12CommandLib.inf
SecurityPkg/Library/Tpm12CommandLib/Tpm12GetCapability.c [new file with mode: 0644]
SecurityPkg/Library/Tpm12CommandLib/Tpm12NvStorage.c
SecurityPkg/Library/Tpm12CommandLib/Tpm12Ownership.c
SecurityPkg/Library/Tpm12CommandLib/Tpm12Pcr.c [new file with mode: 0644]
SecurityPkg/Library/Tpm12CommandLib/Tpm12PhysicalPresence.c [new file with mode: 0644]
SecurityPkg/Library/Tpm12CommandLib/Tpm12SelfTest.c [new file with mode: 0644]
SecurityPkg/Library/Tpm12CommandLib/Tpm12Startup.c

index 4f0c3848e197c5cd2fe9f7eaa49f3c9169e04a14..f5dbb721b80566ced7948a93f4af681c0292c779 100644 (file)
@@ -3,7 +3,7 @@
 #\r
 #  This library is used by other modules to send TPM 1.2 command.\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
   Tpm12Startup.c\r
   Tpm12Ownership.c\r
   Tpm12NvStorage.c\r
+  Tpm12GetCapability.c\r
+  Tpm12Pcr.c\r
+  Tpm12PhysicalPresence.c\r
+  Tpm12SelfTest.c\r
 \r
 [Packages]\r
   MdePkg/MdePkg.dec\r
@@ -44,4 +48,3 @@
   TimerLib\r
   DebugLib\r
   Tpm12DeviceLib\r
-\r
diff --git a/SecurityPkg/Library/Tpm12CommandLib/Tpm12GetCapability.c b/SecurityPkg/Library/Tpm12CommandLib/Tpm12GetCapability.c
new file mode 100644 (file)
index 0000000..c33746a
--- /dev/null
@@ -0,0 +1,127 @@
+/** @file\r
+  Implement TPM1.2 Get Capabilities related commands.\r
+\r
+Copyright (c) 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
+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 <PiPei.h>\r
+#include <Library/Tpm12CommandLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/Tpm12DeviceLib.h>\r
+\r
+#pragma pack(1)\r
+\r
+typedef struct {\r
+  TPM_RQU_COMMAND_HDR  Hdr;\r
+  UINT32               Capability;\r
+  UINT32               CapabilityFlagSize;\r
+  UINT32               CapabilityFlag;\r
+} TPM_CMD_GET_CAPABILITY;\r
+\r
+typedef struct {\r
+  TPM_RSP_COMMAND_HDR  Hdr;\r
+  UINT32               ResponseSize;\r
+  TPM_PERMANENT_FLAGS  Flags;\r
+} TPM_RSP_GET_CAPABILITY_PERMANENT_FLAGS;\r
+\r
+typedef struct {\r
+  TPM_RSP_COMMAND_HDR  Hdr;\r
+  UINT32               ResponseSize;\r
+  TPM_STCLEAR_FLAGS    Flags;\r
+} TPM_RSP_GET_CAPABILITY_STCLEAR_FLAGS;\r
+\r
+#pragma pack()\r
+\r
+/**\r
+Get TPM capability permanent flags.\r
+\r
+@param[out] TpmPermanentFlags   Pointer to the buffer for returned flag structure.\r
+\r
+@retval EFI_SUCCESS           Operation completed successfully.\r
+@retval EFI_TIMEOUT           The register can't run into the expected status in time.\r
+@retval EFI_BUFFER_TOO_SMALL  Response data buffer is too small.\r
+@retval EFI_DEVICE_ERROR      Unexpected device behavior.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Tpm12GetCapabilityFlagPermanent (\r
+  OUT TPM_PERMANENT_FLAGS  *TpmPermanentFlags\r
+  )\r
+{\r
+  EFI_STATUS                              Status;\r
+  TPM_CMD_GET_CAPABILITY                  Command;\r
+  TPM_RSP_GET_CAPABILITY_PERMANENT_FLAGS  Response;\r
+  UINT32                                  Length;\r
+\r
+  //\r
+  // send Tpm command TPM_ORD_GetCapability\r
+  //\r
+  Command.Hdr.tag            = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
+  Command.Hdr.paramSize      = SwapBytes32 (sizeof (Command));\r
+  Command.Hdr.ordinal        = SwapBytes32 (TPM_ORD_GetCapability);\r
+  Command.Capability         = SwapBytes32 (TPM_CAP_FLAG);\r
+  Command.CapabilityFlagSize = SwapBytes32 (sizeof (TPM_CAP_FLAG_PERMANENT));\r
+  Command.CapabilityFlag     = SwapBytes32 (TPM_CAP_FLAG_PERMANENT);\r
+  Length = sizeof (Response);\r
+  Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  ZeroMem (TpmPermanentFlags, sizeof (*TpmPermanentFlags));\r
+  CopyMem (TpmPermanentFlags, &Response.Flags, MIN (sizeof (*TpmPermanentFlags), Response.ResponseSize));\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+Get TPM capability volatile flags.\r
+\r
+@param[out] VolatileFlags   Pointer to the buffer for returned flag structure.\r
+\r
+@retval EFI_SUCCESS      Operation completed successfully.\r
+@retval EFI_DEVICE_ERROR The command was unsuccessful.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Tpm12GetCapabilityFlagVolatile (\r
+  OUT TPM_STCLEAR_FLAGS  *VolatileFlags\r
+  )\r
+{\r
+  EFI_STATUS                            Status;\r
+  TPM_CMD_GET_CAPABILITY                Command;\r
+  TPM_RSP_GET_CAPABILITY_STCLEAR_FLAGS  Response;\r
+  UINT32                                Length;\r
+\r
+  //\r
+  // send Tpm command TPM_ORD_GetCapability\r
+  //\r
+  Command.Hdr.tag            = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
+  Command.Hdr.paramSize      = SwapBytes32 (sizeof (Command));\r
+  Command.Hdr.ordinal        = SwapBytes32 (TPM_ORD_GetCapability);\r
+  Command.Capability         = SwapBytes32 (TPM_CAP_FLAG);\r
+  Command.CapabilityFlagSize = SwapBytes32 (sizeof (TPM_CAP_FLAG_VOLATILE));\r
+  Command.CapabilityFlag     = SwapBytes32 (TPM_CAP_FLAG_VOLATILE);\r
+  Length = sizeof (Response);\r
+  Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  ZeroMem (VolatileFlags, sizeof (*VolatileFlags));\r
+  CopyMem (VolatileFlags, &Response.Flags, MIN (sizeof (*VolatileFlags), Response.ResponseSize));\r
+\r
+  return Status;\r
+}\r
index d644d33846f34394d30d37c84c1b67e8bf071ede..7baafa8f55be2568cb3c4ab61e175be008b95393 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implement TPM1.2 NV storage related command.\r
 \r
-Copyright (c) 2015, Intel Corporation. All rights reserved. <BR>\r
+Copyright (c) 2015 - 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
@@ -12,18 +12,17 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include <Uefi.h>\r
-#include <IndustryStandard/Tpm12.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/BaseLib.h>\r
-#include <Library/Tpm12DeviceLib.h>\r
+#include <PiPei.h>\r
 #include <Library/Tpm12CommandLib.h>\r
+#include <Library/BaseLib.h>\r
 #include <Library/DebugLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/Tpm12DeviceLib.h>\r
 \r
 //\r
-// Max TPM command/reponse length\r
+// Max TPM NV value length\r
 //\r
-#define TPMCMDBUFLENGTH             1024\r
+#define TPMNVVALUELENGTH  1024\r
 \r
 #pragma pack(1)\r
 \r
@@ -33,10 +32,6 @@ typedef struct {
   TPM_ENCAUTH           EncAuth;\r
 } TPM_CMD_NV_DEFINE_SPACE;\r
 \r
-typedef struct {\r
-  TPM_RSP_COMMAND_HDR   Hdr;\r
-} TPM_RSP_NV_DEFINE_SPACE;\r
-\r
 typedef struct {\r
   TPM_RQU_COMMAND_HDR   Hdr;\r
   TPM_NV_INDEX          NvIndex;\r
@@ -47,7 +42,7 @@ typedef struct {
 typedef struct {\r
   TPM_RSP_COMMAND_HDR   Hdr;\r
   UINT32                DataSize;\r
-  UINT8                 Data[TPMCMDBUFLENGTH];\r
+  UINT8                 Data[TPMNVVALUELENGTH];\r
 } TPM_RSP_NV_READ_VALUE;\r
 \r
 typedef struct {\r
@@ -55,13 +50,9 @@ typedef struct {
   TPM_NV_INDEX          NvIndex;\r
   UINT32                Offset;\r
   UINT32                DataSize;\r
-  UINT8                 Data[TPMCMDBUFLENGTH];\r
+  UINT8                 Data[TPMNVVALUELENGTH];\r
 } TPM_CMD_NV_WRITE_VALUE;\r
 \r
-typedef struct {\r
-  TPM_RSP_COMMAND_HDR   Hdr;\r
-} TPM_RSP_NV_WRITE_VALUE;\r
-\r
 #pragma pack()\r
 \r
 /**\r
@@ -80,57 +71,50 @@ Tpm12NvDefineSpace (
   IN TPM_ENCAUTH           *EncAuth\r
   )\r
 {\r
-  EFI_STATUS                        Status;\r
-  UINT32                            TpmRecvSize;\r
-  UINT32                            TpmSendSize;\r
-  TPM_CMD_NV_DEFINE_SPACE           SendBuffer;\r
-  TPM_RSP_NV_DEFINE_SPACE           RecvBuffer;\r
-  UINT32                            ReturnCode;\r
+  EFI_STATUS               Status;\r
+  TPM_CMD_NV_DEFINE_SPACE  Command;\r
+  TPM_RSP_COMMAND_HDR      Response;\r
+  UINT32                   Length;\r
 \r
   //\r
   // send Tpm command TPM_ORD_NV_DefineSpace\r
   //\r
-  TpmRecvSize                = sizeof (TPM_RSP_NV_DEFINE_SPACE);\r
-  TpmSendSize                = sizeof (TPM_CMD_NV_DEFINE_SPACE);\r
-  SendBuffer.Hdr.tag         = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
-  SendBuffer.Hdr.paramSize   = SwapBytes32 (sizeof(TPM_CMD_NV_DEFINE_SPACE));\r
-  SendBuffer.Hdr.ordinal     = SwapBytes32 (TPM_ORD_NV_DefineSpace);\r
-  SendBuffer.PubInfo.tag     = SwapBytes16 (PubInfo->tag);\r
-  SendBuffer.PubInfo.nvIndex = SwapBytes32 (PubInfo->nvIndex);\r
-  SendBuffer.PubInfo.pcrInfoRead.pcrSelection.sizeOfSelect  = SwapBytes16 (PubInfo->pcrInfoRead.pcrSelection.sizeOfSelect);\r
-  SendBuffer.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[0]  = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[0];\r
-  SendBuffer.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[1]  = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[1];\r
-  SendBuffer.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[2]  = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[2];\r
-  SendBuffer.PubInfo.pcrInfoRead.localityAtRelease          = PubInfo->pcrInfoRead.localityAtRelease;\r
-  CopyMem (&SendBuffer.PubInfo.pcrInfoRead.digestAtRelease, &PubInfo->pcrInfoRead.digestAtRelease, sizeof(PubInfo->pcrInfoRead.digestAtRelease));\r
-  SendBuffer.PubInfo.pcrInfoWrite.pcrSelection.sizeOfSelect = SwapBytes16 (PubInfo->pcrInfoWrite.pcrSelection.sizeOfSelect);\r
-  SendBuffer.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[0] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[0];\r
-  SendBuffer.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[1] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[1];\r
-  SendBuffer.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[2] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[2];\r
-  SendBuffer.PubInfo.pcrInfoWrite.localityAtRelease         = PubInfo->pcrInfoWrite.localityAtRelease;\r
-  CopyMem (&SendBuffer.PubInfo.pcrInfoWrite.digestAtRelease, &PubInfo->pcrInfoWrite.digestAtRelease, sizeof(PubInfo->pcrInfoWrite.digestAtRelease));\r
-  SendBuffer.PubInfo.permission.tag        = SwapBytes16 (PubInfo->permission.tag);\r
-  SendBuffer.PubInfo.permission.attributes = SwapBytes32 (PubInfo->permission.attributes);\r
-  SendBuffer.PubInfo.bReadSTClear          = PubInfo->bReadSTClear;\r
-  SendBuffer.PubInfo.bWriteSTClear         = PubInfo->bWriteSTClear;\r
-  SendBuffer.PubInfo.bWriteDefine          = PubInfo->bWriteDefine;\r
-  SendBuffer.PubInfo.dataSize              = SwapBytes32 (PubInfo->dataSize);\r
-  CopyMem (&SendBuffer.EncAuth, EncAuth, sizeof(*EncAuth));\r
-\r
-  Status = Tpm12SubmitCommand (TpmSendSize, (UINT8 *)&SendBuffer, &TpmRecvSize, (UINT8 *)&RecvBuffer);\r
+  Command.Hdr.tag         = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
+  Command.Hdr.paramSize   = SwapBytes32 (sizeof (Command));\r
+  Command.Hdr.ordinal     = SwapBytes32 (TPM_ORD_NV_DefineSpace);\r
+  Command.PubInfo.tag     = SwapBytes16 (PubInfo->tag);\r
+  Command.PubInfo.nvIndex = SwapBytes32 (PubInfo->nvIndex);\r
+  Command.PubInfo.pcrInfoRead.pcrSelection.sizeOfSelect  = SwapBytes16 (PubInfo->pcrInfoRead.pcrSelection.sizeOfSelect);\r
+  Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[0]  = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[0];\r
+  Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[1]  = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[1];\r
+  Command.PubInfo.pcrInfoRead.pcrSelection.pcrSelect[2]  = PubInfo->pcrInfoRead.pcrSelection.pcrSelect[2];\r
+  Command.PubInfo.pcrInfoRead.localityAtRelease          = PubInfo->pcrInfoRead.localityAtRelease;\r
+  CopyMem (&Command.PubInfo.pcrInfoRead.digestAtRelease, &PubInfo->pcrInfoRead.digestAtRelease, sizeof(PubInfo->pcrInfoRead.digestAtRelease));\r
+  Command.PubInfo.pcrInfoWrite.pcrSelection.sizeOfSelect = SwapBytes16 (PubInfo->pcrInfoWrite.pcrSelection.sizeOfSelect);\r
+  Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[0] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[0];\r
+  Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[1] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[1];\r
+  Command.PubInfo.pcrInfoWrite.pcrSelection.pcrSelect[2] = PubInfo->pcrInfoWrite.pcrSelection.pcrSelect[2];\r
+  Command.PubInfo.pcrInfoWrite.localityAtRelease         = PubInfo->pcrInfoWrite.localityAtRelease;\r
+  CopyMem (&Command.PubInfo.pcrInfoWrite.digestAtRelease, &PubInfo->pcrInfoWrite.digestAtRelease, sizeof(PubInfo->pcrInfoWrite.digestAtRelease));\r
+  Command.PubInfo.permission.tag        = SwapBytes16 (PubInfo->permission.tag);\r
+  Command.PubInfo.permission.attributes = SwapBytes32 (PubInfo->permission.attributes);\r
+  Command.PubInfo.bReadSTClear          = PubInfo->bReadSTClear;\r
+  Command.PubInfo.bWriteSTClear         = PubInfo->bWriteSTClear;\r
+  Command.PubInfo.bWriteDefine          = PubInfo->bWriteDefine;\r
+  Command.PubInfo.dataSize              = SwapBytes32 (PubInfo->dataSize);\r
+  CopyMem (&Command.EncAuth, EncAuth, sizeof(*EncAuth));\r
+  Length = sizeof (Response);\r
+  Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  ReturnCode = SwapBytes32(RecvBuffer.Hdr.returnCode);\r
-  DEBUG ((DEBUG_INFO, "Tpm12NvDefineSpace - ReturnCode = %x\n", ReturnCode));\r
-  switch (ReturnCode) {\r
+  DEBUG ((DEBUG_INFO, "Tpm12NvDefineSpace - ReturnCode = %x\n", SwapBytes32 (Response.returnCode)));\r
+  switch (SwapBytes32 (Response.returnCode)) {\r
   case TPM_SUCCESS:\r
-    break;\r
+    return EFI_SUCCESS;\r
   default:\r
     return EFI_DEVICE_ERROR;\r
   }\r
-\r
-  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -147,38 +131,33 @@ Tpm12NvDefineSpace (
 EFI_STATUS\r
 EFIAPI\r
 Tpm12NvReadValue (\r
-  IN TPM_NV_INDEX   NvIndex,\r
-  IN UINT32         Offset,\r
-  IN OUT UINT32     *DataSize,\r
-  OUT UINT8         *Data\r
+  IN TPM_NV_INDEX  NvIndex,\r
+  IN UINT32        Offset,\r
+  IN OUT UINT32    *DataSize,\r
+  OUT UINT8        *Data\r
   )\r
 {\r
-  EFI_STATUS                        Status;\r
-  UINT32                            TpmRecvSize;\r
-  UINT32                            TpmSendSize;\r
-  TPM_CMD_NV_READ_VALUE             SendBuffer;\r
-  TPM_RSP_NV_READ_VALUE             RecvBuffer;\r
-  UINT32                            ReturnCode;\r
+  EFI_STATUS             Status;\r
+  TPM_CMD_NV_READ_VALUE  Command;\r
+  TPM_RSP_NV_READ_VALUE  Response;\r
+  UINT32                 Length;\r
 \r
   //\r
   // send Tpm command TPM_ORD_NV_ReadValue\r
   //\r
-  TpmRecvSize               = sizeof (TPM_RSP_NV_READ_VALUE);\r
-  TpmSendSize               = sizeof (TPM_CMD_NV_READ_VALUE);\r
-  SendBuffer.Hdr.tag        = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
-  SendBuffer.Hdr.paramSize  = SwapBytes32 (sizeof(TPM_CMD_NV_READ_VALUE));\r
-  SendBuffer.Hdr.ordinal    = SwapBytes32 (TPM_ORD_NV_ReadValue);\r
-  SendBuffer.NvIndex        = SwapBytes32 (NvIndex);\r
-  SendBuffer.Offset         = SwapBytes32 (Offset);\r
-  SendBuffer.DataSize       = SwapBytes32 (*DataSize);\r
-\r
-  Status = Tpm12SubmitCommand (TpmSendSize, (UINT8 *)&SendBuffer, &TpmRecvSize, (UINT8 *)&RecvBuffer);\r
+  Command.Hdr.tag       = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
+  Command.Hdr.paramSize = SwapBytes32 (sizeof (Command));\r
+  Command.Hdr.ordinal   = SwapBytes32 (TPM_ORD_NV_ReadValue);\r
+  Command.NvIndex       = SwapBytes32 (NvIndex);\r
+  Command.Offset        = SwapBytes32 (Offset);\r
+  Command.DataSize      = SwapBytes32 (*DataSize);\r
+  Length = sizeof (Response);\r
+  Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  ReturnCode = SwapBytes32(RecvBuffer.Hdr.returnCode);\r
-  DEBUG ((DEBUG_INFO, "Tpm12NvReadValue - ReturnCode = %x\n", ReturnCode));\r
-  switch (ReturnCode) {\r
+  DEBUG ((DEBUG_INFO, "Tpm12NvReadValue - ReturnCode = %x\n", SwapBytes32 (Response.Hdr.returnCode)));\r
+  switch (SwapBytes32 (Response.Hdr.returnCode)) {\r
   case TPM_SUCCESS:\r
     break;\r
   default:\r
@@ -188,8 +167,12 @@ Tpm12NvReadValue (
   //\r
   // Return the response\r
   //\r
-  *DataSize = SwapBytes32(RecvBuffer.DataSize);\r
-  CopyMem (Data, &RecvBuffer.Data, *DataSize);\r
+  if (SwapBytes32 (Response.DataSize) > *DataSize) {\r
+    return EFI_BUFFER_TOO_SMALL;\r
+  }\r
+  *DataSize = SwapBytes32 (Response.DataSize);\r
+  ZeroMem (Data, *DataSize);\r
+  CopyMem (Data, &Response.Data, *DataSize);\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -208,48 +191,41 @@ Tpm12NvReadValue (
 EFI_STATUS\r
 EFIAPI\r
 Tpm12NvWriteValue (\r
-  IN TPM_NV_INDEX   NvIndex,\r
-  IN UINT32         Offset,\r
-  IN UINT32         DataSize,\r
-  IN UINT8          *Data\r
+  IN TPM_NV_INDEX  NvIndex,\r
+  IN UINT32        Offset,\r
+  IN UINT32        DataSize,\r
+  IN UINT8         *Data\r
   )\r
 {\r
-  EFI_STATUS                        Status;\r
-  UINT32                            TpmRecvSize;\r
-  UINT32                            TpmSendSize;\r
-  TPM_CMD_NV_WRITE_VALUE            SendBuffer;\r
-  TPM_RSP_NV_WRITE_VALUE            RecvBuffer;\r
-  UINT32                            ReturnCode;\r
-\r
-  if (DataSize > sizeof(SendBuffer.Data)) {\r
+  EFI_STATUS              Status;\r
+  TPM_CMD_NV_WRITE_VALUE  Command;\r
+  TPM_RSP_COMMAND_HDR     Response;\r
+  UINT32                  Length;\r
+\r
+  if (DataSize > sizeof (Command.Data)) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
   //\r
   // send Tpm command TPM_ORD_NV_WriteValue\r
   //\r
-  TpmRecvSize               = sizeof (TPM_RSP_NV_WRITE_VALUE);\r
-  TpmSendSize               = sizeof (TPM_CMD_NV_WRITE_VALUE) - sizeof(SendBuffer.Data) + DataSize;\r
-  SendBuffer.Hdr.tag        = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
-  SendBuffer.Hdr.paramSize  = SwapBytes32 (sizeof(TPM_CMD_NV_WRITE_VALUE) - sizeof(SendBuffer.Data) + DataSize);\r
-  SendBuffer.Hdr.ordinal    = SwapBytes32 (TPM_ORD_NV_WriteValue);\r
-  SendBuffer.NvIndex        = SwapBytes32 (NvIndex);\r
-  SendBuffer.Offset         = SwapBytes32 (Offset);\r
-  SendBuffer.DataSize       = SwapBytes32 (DataSize);\r
-  CopyMem (SendBuffer.Data, Data, DataSize);\r
-\r
-  Status = Tpm12SubmitCommand (TpmSendSize, (UINT8 *)&SendBuffer, &TpmRecvSize, (UINT8 *)&RecvBuffer);\r
+  Command.Hdr.tag       = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
+  Command.Hdr.paramSize = SwapBytes32 (sizeof (Command) - sizeof(Command.Data) + DataSize);\r
+  Command.Hdr.ordinal   = SwapBytes32 (TPM_ORD_NV_WriteValue);\r
+  Command.NvIndex       = SwapBytes32 (NvIndex);\r
+  Command.Offset        = SwapBytes32 (Offset);\r
+  Command.DataSize      = SwapBytes32 (DataSize);\r
+  CopyMem (Command.Data, Data, DataSize);\r
+  Length = sizeof (Response);\r
+  Status = Tpm12SubmitCommand (Command.Hdr.paramSize, (UINT8 *)&Command, &Length, (UINT8 *)&Response);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  ReturnCode = SwapBytes32(RecvBuffer.Hdr.returnCode);\r
-  DEBUG ((DEBUG_INFO, "Tpm12NvWritedValue - ReturnCode = %x\n", ReturnCode));\r
-  switch (ReturnCode) {\r
+  DEBUG ((DEBUG_INFO, "Tpm12NvWritedValue - ReturnCode = %x\n", SwapBytes32 (Response.returnCode)));\r
+  switch (SwapBytes32 (Response.returnCode)) {\r
   case TPM_SUCCESS:\r
-    break;\r
+    return EFI_SUCCESS;\r
   default:\r
     return EFI_DEVICE_ERROR;\r
   }\r
-\r
-  return EFI_SUCCESS;\r
 }\r
index 5ca1323c30f02feb5a9deaa9d9a643ff8fe71157..0b1bf5c536112e37b5b4a9bcd0cfd6198879cafd 100644 (file)
@@ -12,24 +12,11 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include <Uefi.h>\r
-#include <IndustryStandard/Tpm12.h>\r
+#include <PiPei.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/BaseLib.h>\r
 #include <Library/Tpm12DeviceLib.h>\r
 \r
-#pragma pack(1)\r
-\r
-typedef struct {\r
-  TPM_RQU_COMMAND_HDR   Hdr;\r
-} TPM_CMD_FORCE_CLEAR;\r
-\r
-typedef struct {\r
-  TPM_RSP_COMMAND_HDR   Hdr;\r
-} TPM_RSP_FORCE_CLEAR;\r
-\r
-#pragma pack()\r
-\r
 /**\r
   Send ForceClear command to TPM1.2.\r
 \r
@@ -42,28 +29,24 @@ Tpm12ForceClear (
   VOID\r
   )\r
 {\r
-  EFI_STATUS                        Status;\r
-  UINT32                            TpmRecvSize;\r
-  UINT32                            TpmSendSize;\r
-  TPM_CMD_FORCE_CLEAR               SendBuffer;\r
-  TPM_RSP_FORCE_CLEAR               RecvBuffer;\r
-  UINT32                            ReturnCode;\r
+  EFI_STATUS           Status;\r
+  TPM_RQU_COMMAND_HDR  Command;\r
+  TPM_RSP_COMMAND_HDR  Response;\r
+  UINT32               Length;\r
 \r
   //\r
   // send Tpm command TPM_ORD_ForceClear\r
   //\r
-  TpmRecvSize               = sizeof (TPM_RSP_FORCE_CLEAR);\r
-  TpmSendSize               = sizeof (TPM_CMD_FORCE_CLEAR);\r
-  SendBuffer.Hdr.tag        = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
-  SendBuffer.Hdr.paramSize  = SwapBytes32 (TpmSendSize);\r
-  SendBuffer.Hdr.ordinal    = SwapBytes32 (TPM_ORD_ForceClear);\r
+  Command.tag        = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
+  Command.paramSize  = SwapBytes32 (sizeof (Command));\r
+  Command.ordinal    = SwapBytes32 (TPM_ORD_ForceClear);\r
+  Length = sizeof (Response);\r
 \r
-  Status = Tpm12SubmitCommand (TpmSendSize, (UINT8 *)&SendBuffer, &TpmRecvSize, (UINT8 *)&RecvBuffer);\r
+  Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  ReturnCode = SwapBytes32(RecvBuffer.Hdr.returnCode);\r
-  switch (ReturnCode) {\r
+  switch (SwapBytes32 (Response.returnCode)) {\r
   case TPM_SUCCESS:\r
     return EFI_SUCCESS;\r
   default:\r
diff --git a/SecurityPkg/Library/Tpm12CommandLib/Tpm12Pcr.c b/SecurityPkg/Library/Tpm12CommandLib/Tpm12Pcr.c
new file mode 100644 (file)
index 0000000..321653f
--- /dev/null
@@ -0,0 +1,82 @@
+/** @file\r
+  Implement TPM1.2 PCR related commands.\r
+\r
+Copyright (c) 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
+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 <PiPei.h>\r
+#include <Library/Tpm12CommandLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/Tpm12DeviceLib.h>\r
+\r
+#pragma pack(1)\r
+\r
+typedef struct {\r
+  TPM_RQU_COMMAND_HDR   Hdr;\r
+  TPM_PCRINDEX          PcrIndex;\r
+  TPM_DIGEST            TpmDigest;\r
+} TPM_CMD_EXTEND;\r
+\r
+typedef struct {\r
+  TPM_RSP_COMMAND_HDR   Hdr;\r
+  TPM_DIGEST            TpmDigest;\r
+} TPM_RSP_EXTEND;\r
+\r
+#pragma pack()\r
+\r
+/**\r
+Extend a TPM PCR.\r
+\r
+@param[in]  DigestToExtend    The 160 bit value representing the event to be recorded.\r
+@param[in]  PcrIndex          The PCR to be updated.\r
+@param[out] NewPcrValue       New PCR value after extend.\r
+\r
+@retval EFI_SUCCESS           Operation completed successfully.\r
+@retval EFI_TIMEOUT           The register can't run into the expected status in time.\r
+@retval EFI_BUFFER_TOO_SMALL  Response data buffer is too small.\r
+@retval EFI_DEVICE_ERROR      Unexpected device behavior.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Tpm12Extend (\r
+  IN  TPM_DIGEST    *DigestToExtend,\r
+  IN  TPM_PCRINDEX  PcrIndex,\r
+  OUT TPM_DIGEST    *NewPcrValue\r
+  )\r
+{\r
+  EFI_STATUS      Status;\r
+  TPM_CMD_EXTEND  Command;\r
+  TPM_RSP_EXTEND  Response;\r
+  UINT32          Length;\r
+\r
+  //\r
+  // send Tpm command TPM_ORD_Extend\r
+  //\r
+  Command.Hdr.tag       = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
+  Command.Hdr.paramSize = SwapBytes32 (sizeof (Command));\r
+  Command.Hdr.ordinal   = SwapBytes32 (TPM_ORD_Extend);\r
+  Command.PcrIndex      = SwapBytes32 (PcrIndex);\r
+  CopyMem (&Command.TpmDigest, DigestToExtend, sizeof (Command.TpmDigest));\r
+  Length = sizeof (Response);\r
+  Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (NewPcrValue != NULL) {\r
+    CopyMem (NewPcrValue, &Response.TpmDigest, sizeof (*NewPcrValue));\r
+  }\r
+\r
+  return Status;\r
+}\r
diff --git a/SecurityPkg/Library/Tpm12CommandLib/Tpm12PhysicalPresence.c b/SecurityPkg/Library/Tpm12CommandLib/Tpm12PhysicalPresence.c
new file mode 100644 (file)
index 0000000..a730334
--- /dev/null
@@ -0,0 +1,60 @@
+/** @file\r
+  Implement TPM1.2 Physical Presence related command.\r
+\r
+Copyright (c) 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
+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 <PiPei.h>\r
+#include <Library/Tpm12CommandLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/Tpm12DeviceLib.h>\r
+\r
+#pragma pack(1)\r
+\r
+typedef struct {\r
+  TPM_RQU_COMMAND_HDR   Hdr;\r
+  TPM_PHYSICAL_PRESENCE PhysicalPresence;\r
+} TPM_CMD_PHYSICAL_PRESENCE;\r
+\r
+#pragma pack()\r
+\r
+/**\r
+Send TSC_PhysicalPresence command to TPM.\r
+\r
+@param[in] PhysicalPresence   The state to set the TPMs Physical Presence flags.\r
+\r
+@retval EFI_SUCCESS           Operation completed successfully.\r
+@retval EFI_TIMEOUT           The register can't run into the expected status in time.\r
+@retval EFI_BUFFER_TOO_SMALL  Response data buffer is too small.\r
+@retval EFI_DEVICE_ERROR      Unexpected device behavior.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Tpm12PhysicalPresence (\r
+  IN TPM_PHYSICAL_PRESENCE  PhysicalPresence\r
+  )\r
+{\r
+  TPM_CMD_PHYSICAL_PRESENCE  Command;\r
+  TPM_RSP_COMMAND_HDR        Response;\r
+  UINT32                     Length;\r
+\r
+  //\r
+  // send Tpm command TSC_ORD_PhysicalPresence\r
+  //\r
+  Command.Hdr.tag          = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
+  Command.Hdr.paramSize    = SwapBytes32 (sizeof (Command));\r
+  Command.Hdr.ordinal      = SwapBytes32 (TSC_ORD_PhysicalPresence);\r
+  Command.PhysicalPresence = SwapBytes16 (PhysicalPresence);\r
+  Length = sizeof (Response);\r
+  return Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);\r
+}\r
diff --git a/SecurityPkg/Library/Tpm12CommandLib/Tpm12SelfTest.c b/SecurityPkg/Library/Tpm12CommandLib/Tpm12SelfTest.c
new file mode 100644 (file)
index 0000000..cd08d19
--- /dev/null
@@ -0,0 +1,46 @@
+/** @file\r
+  Implement TPM1.2 NV Self Test related commands.\r
+\r
+Copyright (c) 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
+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 <PiPei.h>\r
+#include <Library/Tpm12CommandLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/Tpm12DeviceLib.h>\r
+\r
+/**\r
+Send TPM_ContinueSelfTest command to TPM.\r
+\r
+@retval EFI_SUCCESS           Operation completed successfully.\r
+@retval EFI_TIMEOUT           The register can't run into the expected status in time.\r
+@retval EFI_BUFFER_TOO_SMALL  Response data buffer is too small.\r
+@retval EFI_DEVICE_ERROR      Unexpected device behavior.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Tpm12ContinueSelfTest (\r
+  VOID\r
+  )\r
+{\r
+  TPM_RQU_COMMAND_HDR  Command;\r
+  TPM_RSP_COMMAND_HDR  Response;\r
+  UINT32               Length;\r
+\r
+  //\r
+  // send Tpm command TPM_ORD_ContinueSelfTest\r
+  //\r
+  Command.tag       = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
+  Command.paramSize = SwapBytes32 (sizeof (Command));\r
+  Command.ordinal   = SwapBytes32 (TPM_ORD_ContinueSelfTest);\r
+  return Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);\r
+}\r
index afbe02e382972b68b7a7cc4f90c97ca00e9496a6..bc10e4586a68ac31914039347def2464f6a09621 100644 (file)
@@ -12,8 +12,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 **/\r
 \r
-#include <Uefi.h>\r
-#include <IndustryStandard/Tpm12.h>\r
+#include <PiPei.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/BaseLib.h>\r
 #include <Library/Tpm12DeviceLib.h>\r
@@ -25,18 +24,6 @@ typedef struct {
   TPM_STARTUP_TYPE      TpmSt;\r
 } TPM_CMD_START_UP;\r
 \r
-typedef struct {\r
-  TPM_RSP_COMMAND_HDR   Hdr;\r
-} TPM_RSP_START_UP;\r
-\r
-typedef struct {\r
-  TPM_RQU_COMMAND_HDR   Hdr;\r
-} TPM_CMD_SAVE_STATE;\r
-\r
-typedef struct {\r
-  TPM_RSP_COMMAND_HDR   Hdr;\r
-} TPM_RSP_SAVE_STATE;\r
-\r
 #pragma pack()\r
 \r
 /**\r
@@ -50,32 +37,27 @@ typedef struct {
 EFI_STATUS\r
 EFIAPI\r
 Tpm12Startup (\r
-  IN TPM_STARTUP_TYPE          TpmSt\r
+  IN TPM_STARTUP_TYPE  TpmSt\r
   )\r
 {\r
-  EFI_STATUS                        Status;\r
-  UINT32                            TpmRecvSize;\r
-  UINT32                            TpmSendSize;\r
-  TPM_CMD_START_UP                  SendBuffer;\r
-  TPM_RSP_START_UP                  RecvBuffer;\r
-  UINT32                            ReturnCode;\r
+  EFI_STATUS           Status;\r
+  TPM_CMD_START_UP     Command;\r
+  TPM_RSP_COMMAND_HDR  Response;\r
+  UINT32               Length;\r
 \r
   //\r
   // send Tpm command TPM_ORD_Startup\r
   //\r
-  TpmRecvSize               = sizeof (TPM_RSP_START_UP);\r
-  TpmSendSize               = sizeof (TPM_CMD_START_UP);\r
-  SendBuffer.Hdr.tag        = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
-  SendBuffer.Hdr.paramSize  = SwapBytes32 (TpmSendSize);\r
-  SendBuffer.Hdr.ordinal    = SwapBytes32 (TPM_ORD_Startup);\r
-  SendBuffer.TpmSt          = SwapBytes16 (TpmSt);\r
-\r
-  Status = Tpm12SubmitCommand (TpmSendSize, (UINT8 *)&SendBuffer, &TpmRecvSize, (UINT8 *)&RecvBuffer);\r
+  Command.Hdr.tag       = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
+  Command.Hdr.paramSize = SwapBytes32 (sizeof (Command));\r
+  Command.Hdr.ordinal   = SwapBytes32 (TPM_ORD_Startup);\r
+  Command.TpmSt         = SwapBytes16 (TpmSt);\r
+  Length = sizeof (Response);\r
+  Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  ReturnCode = SwapBytes32(RecvBuffer.Hdr.returnCode);\r
-  switch (ReturnCode) {\r
+  switch (SwapBytes32(Response.returnCode)) {\r
   case TPM_SUCCESS:\r
   case TPM_INVALID_POSTINIT:\r
     // In warm reset, TPM may response TPM_INVALID_POSTINIT\r
@@ -97,28 +79,23 @@ Tpm12SaveState (
   VOID\r
   )\r
 {\r
-  EFI_STATUS                        Status;\r
-  UINT32                            TpmRecvSize;\r
-  UINT32                            TpmSendSize;\r
-  TPM_CMD_SAVE_STATE                SendBuffer;\r
-  TPM_RSP_SAVE_STATE                RecvBuffer;\r
-  UINT32                            ReturnCode;\r
+  EFI_STATUS           Status;\r
+  TPM_RQU_COMMAND_HDR  Command;\r
+  TPM_RSP_COMMAND_HDR  Response;\r
+  UINT32               Length;\r
 \r
   //\r
   // send Tpm command TPM_ORD_SaveState\r
   //\r
-  TpmRecvSize               = sizeof (TPM_RSP_SAVE_STATE);\r
-  TpmSendSize               = sizeof (TPM_CMD_SAVE_STATE);\r
-  SendBuffer.Hdr.tag        = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
-  SendBuffer.Hdr.paramSize  = SwapBytes32 (TpmSendSize);\r
-  SendBuffer.Hdr.ordinal    = SwapBytes32 (TPM_ORD_SaveState);\r
-\r
-  Status = Tpm12SubmitCommand (TpmSendSize, (UINT8 *)&SendBuffer, &TpmRecvSize, (UINT8 *)&RecvBuffer);\r
+  Command.tag        = SwapBytes16 (TPM_TAG_RQU_COMMAND);\r
+  Command.paramSize  = SwapBytes32 (sizeof (Command));\r
+  Command.ordinal    = SwapBytes32 (TPM_ORD_SaveState);\r
+  Length = sizeof (Response);\r
+  Status = Tpm12SubmitCommand (sizeof (Command), (UINT8 *)&Command, &Length, (UINT8 *)&Response);\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  ReturnCode = SwapBytes32(RecvBuffer.Hdr.returnCode);\r
-  switch (ReturnCode) {\r
+  switch (SwapBytes32 (Response.returnCode)) {\r
   case TPM_SUCCESS:\r
     return EFI_SUCCESS;\r
   default:\r