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