SecurityPkg: Tcg2Smm: TPM2 Vendor specific HID
authorZhang, Chao B <chao.b.zhang@intel.com>
Wed, 21 Dec 2016 06:31:47 +0000 (14:31 +0800)
committerZhang, Chao B <chao.b.zhang@intel.com>
Thu, 29 Dec 2016 01:45:13 +0000 (09:45 +0800)
Update TPM2 HID using vendor ManufacturerID & FirmwareVersion1.

Cc: Yao Jiewen <jiewen.yao@intel.com>
Cc: Star Zeng <star.zeng@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Chao Zhang <chao.b.zhang@intel.com>
Reviewed-by: Yao Jiewen <jiewen.yao@intel.com>
Reviewed-by: Star Zeng <star.zeng@intel.com>
SecurityPkg/Library/Tpm2CommandLib/Tpm2Capability.c
SecurityPkg/Tcg/Tcg2Smm/Tcg2Smm.c
SecurityPkg/Tcg/Tcg2Smm/Tcg2Smm.h
SecurityPkg/Tcg/Tcg2Smm/Tcg2Smm.inf
SecurityPkg/Tcg/Tcg2Smm/Tpm.asl

index 9aab17f9345fdb7a71f225470274661a41ed35c1..79e80fb7a9e791437e6177d1405c5e14eb25670a 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implement TPM2 Capability related command.\r
 \r
-Copyright (c) 2013, 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
@@ -189,7 +189,7 @@ Tpm2GetCapabilityManufactureID (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  *ManufactureId = SwapBytes32 (TpmCap.data.tpmProperties.tpmProperty->value);\r
+  *ManufactureId = TpmCap.data.tpmProperties.tpmProperty->value;\r
 \r
   return EFI_SUCCESS;\r
 }\r
index d02123dfa61fb282420675d7dd8533afc0a96a1c..2cedfde75329fc2dd0c7317868fb7ab92bf890e8 100644 (file)
@@ -302,6 +302,108 @@ UpdatePPVersion (
   return EFI_NOT_FOUND;\r
 }\r
 \r
+/**\r
+  Patch TPM2 device HID string.  The initial string tag in TPM2 ACPI table is "NNN0000".\r
+\r
+  @param[in, out] Table          The TPM2 SSDT ACPI table.\r
+\r
+  @return                               HID Update status.\r
+\r
+**/\r
+EFI_STATUS\r
+UpdateHID (\r
+  EFI_ACPI_DESCRIPTION_HEADER    *Table\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINT8       *DataPtr;\r
+  CHAR8       HID[TPM_HID_ACPI_SIZE];\r
+  UINT32      ManufacturerID;\r
+  UINT32      FirmwareVersion1;\r
+  UINT32      FirmwareVersion2;\r
+  BOOLEAN     PnpHID;\r
+\r
+  PnpHID = TRUE;\r
+\r
+  //\r
+  // Initialize HID with Default PNP string\r
+  //\r
+  ZeroMem(HID, TPM_HID_ACPI_SIZE);\r
+  CopyMem(HID, TPM_HID_TAG, TPM_HID_PNP_SIZE);\r
+\r
+  //\r
+  // Get Manufacturer ID\r
+  //\r
+  Status = Tpm2GetCapabilityManufactureID(&ManufacturerID);\r
+  if (!EFI_ERROR(Status)) {\r
+    DEBUG((EFI_D_INFO, "TPM_PT_MANUFACTURER 0x%08x\n", ManufacturerID));\r
+    //\r
+    // ManfacturerID defined in TCG Vendor ID Registry \r
+    // may tailed with 0x00 or 0x20\r
+    //\r
+    if ((ManufacturerID >> 24) == 0x00 || ((ManufacturerID >> 24) == 0x20)) {\r
+      //\r
+      //  HID containing PNP ID "NNN####"\r
+      //   NNN is uppercase letter for Vendor ID specified by manufacturer\r
+      //\r
+      CopyMem(HID, &ManufacturerID, 3);\r
+    } else {\r
+      //\r
+      //  HID containing ACP ID "NNNN####"\r
+      //   NNNN is uppercase letter for Vendor ID specified by manufacturer\r
+      //\r
+      CopyMem(HID, &ManufacturerID, 4);\r
+      PnpHID = FALSE;\r
+    }\r
+  } else {\r
+    DEBUG ((EFI_D_ERROR, "Get TPM_PT_MANUFACTURER failed %x!\n", Status));\r
+    ASSERT(FALSE);\r
+    return Status;\r
+  }\r
+\r
+  Status = Tpm2GetCapabilityFirmwareVersion(&FirmwareVersion1, &FirmwareVersion2);\r
+  if (!EFI_ERROR(Status)) {\r
+    DEBUG((EFI_D_INFO, "TPM_PT_FIRMWARE_VERSION_1 0x%x\n", FirmwareVersion1));\r
+    DEBUG((EFI_D_INFO, "TPM_PT_FIRMWARE_VERSION_2 0x%x\n", FirmwareVersion2));\r
+    //\r
+    //   #### is Firmware Version 1\r
+    //\r
+    if (PnpHID) {\r
+      AsciiSPrint(HID + 3, TPM_HID_PNP_SIZE - 3, "%02d%02d", ((FirmwareVersion1 & 0xFFFF0000) >> 16), (FirmwareVersion1 && 0x0000FFFF));\r
+    } else {\r
+      AsciiSPrint(HID + 4, TPM_HID_ACPI_SIZE - 4, "%02d%02d", ((FirmwareVersion1 & 0xFFFF0000) >> 16), (FirmwareVersion1 && 0x0000FFFF));\r
+    }\r
+    \r
+  } else {\r
+    DEBUG ((EFI_D_ERROR, "Get TPM_PT_FIRMWARE_VERSION_X failed %x!\n", Status));\r
+    ASSERT(FALSE);\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Patch HID in ASL code before loading the SSDT.\r
+  //\r
+  for (DataPtr  = (UINT8 *)(Table + 1);\r
+       DataPtr <= (UINT8 *) ((UINT8 *) Table + Table->Length - TPM_HID_PNP_SIZE);\r
+       DataPtr += 1) {\r
+    if (AsciiStrCmp((CHAR8 *)DataPtr,  TPM_HID_TAG) == 0) {\r
+      if (PnpHID) {\r
+        CopyMem(DataPtr, HID, TPM_HID_PNP_SIZE);\r
+      } else {\r
+        //\r
+        // NOOP will be patched to '\0'\r
+        //\r
+        CopyMem(DataPtr, HID, TPM_HID_ACPI_SIZE);\r
+      }\r
+      DEBUG((EFI_D_INFO, "TPM2 ACPI _HID updated to %a\n", HID));\r
+      return Status;\r
+    }\r
+  }\r
+\r
+  DEBUG((EFI_D_ERROR, "TPM2 ACPI HID TAG for patch not found!\n"));\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
 /**\r
   Initialize and publish TPM items in ACPI table.\r
 \r
@@ -335,6 +437,14 @@ PublishAcpiTable (
   Status = UpdatePPVersion(Table, (CHAR8 *)PcdGetPtr(PcdTcgPhysicalPresenceInterfaceVer));\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  //\r
+  // Update TPM2 HID before measuring it to PCR\r
+  //\r
+  Status = UpdateHID(Table);\r
+  if (EFI_ERROR(Status)) {\r
+    return Status;\r
+  }\r
+\r
   //\r
   // Measure to PCR[0] with event EV_POST_CODE ACPI DATA\r
   //\r
index 0b09032d633d50fc86304dad57ebe508711b44ae..18e8bfc355a891df9c4bfd3cfd286aa98752b69f 100644 (file)
@@ -35,9 +35,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/UefiBootServicesTableLib.h>\r
 #include <Library/DxeServicesLib.h>\r
 #include <Library/TpmMeasurementLib.h>\r
-#include <Library/Tpm2DeviceLib.h>\r
+#include <Library/Tpm2CommandLib.h>\r
 #include <Library/Tcg2PhysicalPresenceLib.h>\r
 #include <Library/IoLib.h>\r
+#include <Library/PrintLib.h>\r
 \r
 #include <IndustryStandard/TpmPtp.h>\r
 \r
@@ -94,4 +95,11 @@ typedef struct {
 #define PHYSICAL_PRESENCE_VERSION_TAG                              "$PV"\r
 #define PHYSICAL_PRESENCE_VERSION_SIZE                             4\r
 \r
+//\r
+// PNP _HID for TPM2 device\r
+//\r
+#define TPM_HID_TAG                                                "NNN0000"\r
+#define TPM_HID_PNP_SIZE                                           8\r
+#define TPM_HID_ACPI_SIZE                                          9\r
+\r
 #endif  // __TCG_SMM_H__\r
index 0de4fcee045390282906182af4d6b54201418b16..8c823d644a93ee3fea57cced17dd6c2f7b7521e4 100644 (file)
@@ -48,7 +48,7 @@
   DebugLib\r
   DxeServicesLib\r
   TpmMeasurementLib\r
-  Tpm2DeviceLib\r
+  Tpm2CommandLib\r
   Tcg2PhysicalPresenceLib\r
   IoLib\r
 \r
index 2083a3e2cebb8a62265d52ee510ef4872050c61b..4128684806107d3d6b86232645e0b227a281ba61 100644 (file)
@@ -30,8 +30,14 @@ DefinitionBlock (
       //\r
       // TCG2\r
       //\r
-      Name (_HID, "MSFT0101")\r
-      \r
+      Name (_HID, "NNN0000")\r
+      //\r
+      // Reserve 1 more byte for ACPI HID\r
+      //\r
+      Noop\r
+\r
+      Name (_CID, "MSFT0101")\r
+\r
       //\r
       // Readable name of this device, don't know if this way is correct yet\r
       //\r