]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/Tcg2Config/TpmDetection.c
SecurityPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / SecurityPkg / Tcg / Tcg2Config / TpmDetection.c
CommitLineData
1abfa4ce
JY
1/** @file\r
2 TPM1.2/dTPM2.0 auto detection.\r
3\r
b3548d32 4Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>\r
289b714b 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
1abfa4ce
JY
6\r
7**/\r
8\r
9\r
10#include <PiPei.h>\r
11#include <Ppi/ReadOnlyVariable2.h>\r
12\r
13#include <Library/BaseLib.h>\r
14#include <Library/BaseMemoryLib.h>\r
1abfa4ce
JY
15#include <Library/DebugLib.h>\r
16#include <Library/PeiServicesLib.h>\r
17#include <Library/PcdLib.h>\r
18#include <Library/Tpm12DeviceLib.h>\r
19#include <Library/Tpm12CommandLib.h>\r
20#include <IndustryStandard/Tpm12.h>\r
21\r
22#include "Tcg2ConfigNvData.h"\r
23\r
1abfa4ce
JY
24/**\r
25 This routine check both SetupVariable and real TPM device, and return final TpmDevice configuration.\r
26\r
27 @param SetupTpmDevice TpmDevice configuration in setup driver\r
28\r
29 @return TpmDevice configuration\r
30**/\r
31UINT8\r
32DetectTpmDevice (\r
33 IN UINT8 SetupTpmDevice\r
34 )\r
35{\r
36 EFI_STATUS Status;\r
37 EFI_BOOT_MODE BootMode;\r
38 TCG2_DEVICE_DETECTION Tcg2DeviceDetection;\r
39 EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi;\r
40 UINTN Size;\r
41\r
42 Status = PeiServicesGetBootMode (&BootMode);\r
43 ASSERT_EFI_ERROR (Status);\r
44\r
45 //\r
46 // In S3, we rely on normal boot Detection, because we save to ReadOnly Variable in normal boot.\r
47 //\r
48 if (BootMode == BOOT_ON_S3_RESUME) {\r
49 DEBUG ((EFI_D_INFO, "DetectTpmDevice: S3 mode\n"));\r
50\r
51 Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariablePpi);\r
52 ASSERT_EFI_ERROR (Status);\r
53\r
54 Size = sizeof(TCG2_DEVICE_DETECTION);\r
55 ZeroMem (&Tcg2DeviceDetection, sizeof(Tcg2DeviceDetection));\r
56 Status = VariablePpi->GetVariable (\r
57 VariablePpi,\r
58 TCG2_DEVICE_DETECTION_NAME,\r
59 &gTcg2ConfigFormSetGuid,\r
60 NULL,\r
61 &Size,\r
62 &Tcg2DeviceDetection\r
63 );\r
64 if (!EFI_ERROR (Status) &&\r
65 (Tcg2DeviceDetection.TpmDeviceDetected >= TPM_DEVICE_MIN) &&\r
66 (Tcg2DeviceDetection.TpmDeviceDetected <= TPM_DEVICE_MAX)) {\r
67 DEBUG ((EFI_D_ERROR, "TpmDevice from DeviceDetection: %x\n", Tcg2DeviceDetection.TpmDeviceDetected));\r
68 return Tcg2DeviceDetection.TpmDeviceDetected;\r
69 }\r
70 }\r
71\r
72 DEBUG ((EFI_D_INFO, "DetectTpmDevice:\n"));\r
1abfa4ce
JY
73\r
74 // dTPM available and not disabled by setup\r
75 // We need check if it is TPM1.2 or TPM2.0\r
76 // So try TPM1.2 command at first\r
77\r
78 Status = Tpm12RequestUseTpm ();\r
79 if (EFI_ERROR (Status)) {\r
6d2eec02
MK
80 //\r
81 // dTPM not available\r
82 //\r
83 return TPM_DEVICE_NULL;\r
1abfa4ce
JY
84 }\r
85\r
86 if (BootMode == BOOT_ON_S3_RESUME) {\r
87 Status = Tpm12Startup (TPM_ST_STATE);\r
88 } else {\r
89 Status = Tpm12Startup (TPM_ST_CLEAR);\r
90 }\r
91 if (EFI_ERROR (Status)) {\r
92 return TPM_DEVICE_2_0_DTPM;\r
93 }\r
94\r
95 // NO initialization needed again.\r
fe3ca12d
SZ
96 Status = PcdSet8S (PcdTpmInitializationPolicy, 0);\r
97 ASSERT_EFI_ERROR (Status);\r
1abfa4ce
JY
98 return TPM_DEVICE_1_2;\r
99}\r