--- /dev/null
+/** @file\r
+ TPM1.2/dTPM2.0 auto detection.\r
+\r
+Copyright (c) 2013, 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
+\r
+#include <PiPei.h>\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/IoLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/PeiServicesLib.h>\r
+#include <Library/PcdLib.h>\r
+#include <Library/Tpm12DeviceLib.h>\r
+#include <Library/Tpm12CommandLib.h>\r
+#include <IndustryStandard/Tpm12.h>\r
+\r
+#include "TrEEConfigNvData.h"\r
+\r
+/**\r
+ This routine return if dTPM (1.2 or 2.0) present.\r
+\r
+ @retval TRUE dTPM present\r
+ @retval FALSE dTPM not present\r
+**/\r
+BOOLEAN\r
+IsDtpmPresent (\r
+ VOID\r
+ )\r
+{\r
+ UINT8 RegRead;\r
+ \r
+ RegRead = MmioRead8 ((UINTN)PcdGet64 (PcdTpmBaseAddress));\r
+ if (RegRead == 0xFF) {\r
+ DEBUG ((EFI_D_ERROR, "DetectTpmDevice: Dtpm not present\n"));\r
+ return FALSE;\r
+ } else {\r
+ DEBUG ((EFI_D_ERROR, "DetectTpmDevice: Dtpm present\n"));\r
+ return TRUE;\r
+ }\r
+}\r
+\r
+/**\r
+ This routine check both SetupVariable and real TPM device, and return final TpmDevice configuration.\r
+\r
+ @param SetupTpmDevice TpmDevice configuration in setup driver\r
+\r
+ @return TpmDevice configuration\r
+**/\r
+UINT8\r
+DetectTpmDevice (\r
+ IN UINT8 SetupTpmDevice\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_BOOT_MODE BootMode;\r
+\r
+ Status = PeiServicesGetBootMode (&BootMode);\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ //\r
+ // In S3, we rely on Setup option, because we save to Setup in normal boot.\r
+ //\r
+ if (BootMode == BOOT_ON_S3_RESUME) {\r
+ DEBUG ((EFI_D_ERROR, "DetectTpmDevice: S3 mode\n"));\r
+ return SetupTpmDevice;\r
+ }\r
+\r
+ if (PcdGetBool (PcdHideTpmSupport) && PcdGetBool (PcdHideTpm)) {\r
+ DEBUG ((EFI_D_ERROR, "DetectTpmDevice: Tpm is hide\n"));\r
+ return TPM_DEVICE_NULL;\r
+ }\r
+\r
+ DEBUG ((EFI_D_ERROR, "DetectTpmDevice:\n"));\r
+ if ((!IsDtpmPresent ()) || (SetupTpmDevice == TPM_DEVICE_NULL)) {\r
+ // dTPM not available\r
+ return TPM_DEVICE_NULL;\r
+ }\r
+\r
+ // dTPM available and not disabled by setup\r
+ // We need check if it is TPM1.2 or TPM2.0\r
+ // So try TPM1.2 command at first\r
+\r
+ Status = Tpm12RequestUseTpm ();\r
+ if (EFI_ERROR (Status)) {\r
+ return TPM_DEVICE_2_0_DTPM;\r
+ }\r
+\r
+ Status = Tpm12Startup (TPM_ST_CLEAR);\r
+ if (EFI_ERROR (Status)) {\r
+ return TPM_DEVICE_2_0_DTPM;\r
+ }\r
+\r
+ // NO initialization needed again.\r
+ PcdSet8 (PcdTpmInitializationPolicy, 0);\r
+ return TPM_DEVICE_1_2;\r
+}\r