]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/Tcg2Config/TpmDetection.c
Add TPM2 support defined in trusted computing group.
[mirror_edk2.git] / SecurityPkg / Tcg / Tcg2Config / TpmDetection.c
CommitLineData
1abfa4ce
JY
1/** @file\r
2 TPM1.2/dTPM2.0 auto detection.\r
3\r
4Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials \r
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15\r
16#include <PiPei.h>\r
17#include <Ppi/ReadOnlyVariable2.h>\r
18\r
19#include <Library/BaseLib.h>\r
20#include <Library/BaseMemoryLib.h>\r
21#include <Library/IoLib.h>\r
22#include <Library/DebugLib.h>\r
23#include <Library/PeiServicesLib.h>\r
24#include <Library/PcdLib.h>\r
25#include <Library/Tpm12DeviceLib.h>\r
26#include <Library/Tpm12CommandLib.h>\r
27#include <IndustryStandard/Tpm12.h>\r
28\r
29#include "Tcg2ConfigNvData.h"\r
30\r
31/**\r
32 This routine return if dTPM (1.2 or 2.0) present.\r
33\r
34 @retval TRUE dTPM present\r
35 @retval FALSE dTPM not present\r
36**/\r
37BOOLEAN\r
38IsDtpmPresent (\r
39 VOID\r
40 )\r
41{\r
42 UINT8 RegRead;\r
43 \r
44 RegRead = MmioRead8 ((UINTN)PcdGet64 (PcdTpmBaseAddress));\r
45 if (RegRead == 0xFF) {\r
46 DEBUG ((EFI_D_ERROR, "DetectTpmDevice: Dtpm not present\n"));\r
47 return FALSE;\r
48 } else {\r
49 DEBUG ((EFI_D_INFO, "DetectTpmDevice: Dtpm present\n"));\r
50 return TRUE;\r
51 }\r
52}\r
53\r
54/**\r
55 This routine check both SetupVariable and real TPM device, and return final TpmDevice configuration.\r
56\r
57 @param SetupTpmDevice TpmDevice configuration in setup driver\r
58\r
59 @return TpmDevice configuration\r
60**/\r
61UINT8\r
62DetectTpmDevice (\r
63 IN UINT8 SetupTpmDevice\r
64 )\r
65{\r
66 EFI_STATUS Status;\r
67 EFI_BOOT_MODE BootMode;\r
68 TCG2_DEVICE_DETECTION Tcg2DeviceDetection;\r
69 EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi;\r
70 UINTN Size;\r
71\r
72 Status = PeiServicesGetBootMode (&BootMode);\r
73 ASSERT_EFI_ERROR (Status);\r
74\r
75 //\r
76 // In S3, we rely on normal boot Detection, because we save to ReadOnly Variable in normal boot.\r
77 //\r
78 if (BootMode == BOOT_ON_S3_RESUME) {\r
79 DEBUG ((EFI_D_INFO, "DetectTpmDevice: S3 mode\n"));\r
80\r
81 Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariablePpi);\r
82 ASSERT_EFI_ERROR (Status);\r
83\r
84 Size = sizeof(TCG2_DEVICE_DETECTION);\r
85 ZeroMem (&Tcg2DeviceDetection, sizeof(Tcg2DeviceDetection));\r
86 Status = VariablePpi->GetVariable (\r
87 VariablePpi,\r
88 TCG2_DEVICE_DETECTION_NAME,\r
89 &gTcg2ConfigFormSetGuid,\r
90 NULL,\r
91 &Size,\r
92 &Tcg2DeviceDetection\r
93 );\r
94 if (!EFI_ERROR (Status) &&\r
95 (Tcg2DeviceDetection.TpmDeviceDetected >= TPM_DEVICE_MIN) &&\r
96 (Tcg2DeviceDetection.TpmDeviceDetected <= TPM_DEVICE_MAX)) {\r
97 DEBUG ((EFI_D_ERROR, "TpmDevice from DeviceDetection: %x\n", Tcg2DeviceDetection.TpmDeviceDetected));\r
98 return Tcg2DeviceDetection.TpmDeviceDetected;\r
99 }\r
100 }\r
101\r
102 DEBUG ((EFI_D_INFO, "DetectTpmDevice:\n"));\r
103 if (!IsDtpmPresent ()) {\r
104 // dTPM not available\r
105 return TPM_DEVICE_NULL;\r
106 }\r
107\r
108 // dTPM available and not disabled by setup\r
109 // We need check if it is TPM1.2 or TPM2.0\r
110 // So try TPM1.2 command at first\r
111\r
112 Status = Tpm12RequestUseTpm ();\r
113 if (EFI_ERROR (Status)) {\r
114 return TPM_DEVICE_2_0_DTPM;\r
115 }\r
116\r
117 if (BootMode == BOOT_ON_S3_RESUME) {\r
118 Status = Tpm12Startup (TPM_ST_STATE);\r
119 } else {\r
120 Status = Tpm12Startup (TPM_ST_CLEAR);\r
121 }\r
122 if (EFI_ERROR (Status)) {\r
123 return TPM_DEVICE_2_0_DTPM;\r
124 }\r
125\r
126 // NO initialization needed again.\r
127 PcdSet8 (PcdTpmInitializationPolicy, 0);\r
128 return TPM_DEVICE_1_2;\r
129}\r