]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/TrEEConfig/TpmDetection.c
Add TPM2 implementation.
[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
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/IoLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/PeiServicesLib.h>
23 #include <Library/PcdLib.h>
24 #include <Library/Tpm12DeviceLib.h>
25 #include <Library/Tpm12CommandLib.h>
26 #include <IndustryStandard/Tpm12.h>
27
28 #include "TrEEConfigNvData.h"
29
30 /**
31 This routine return if dTPM (1.2 or 2.0) present.
32
33 @retval TRUE dTPM present
34 @retval FALSE dTPM not present
35 **/
36 BOOLEAN
37 IsDtpmPresent (
38 VOID
39 )
40 {
41 UINT8 RegRead;
42
43 RegRead = MmioRead8 ((UINTN)PcdGet64 (PcdTpmBaseAddress));
44 if (RegRead == 0xFF) {
45 DEBUG ((EFI_D_ERROR, "DetectTpmDevice: Dtpm not present\n"));
46 return FALSE;
47 } else {
48 DEBUG ((EFI_D_ERROR, "DetectTpmDevice: Dtpm present\n"));
49 return TRUE;
50 }
51 }
52
53 /**
54 This routine check both SetupVariable and real TPM device, and return final TpmDevice configuration.
55
56 @param SetupTpmDevice TpmDevice configuration in setup driver
57
58 @return TpmDevice configuration
59 **/
60 UINT8
61 DetectTpmDevice (
62 IN UINT8 SetupTpmDevice
63 )
64 {
65 EFI_STATUS Status;
66 EFI_BOOT_MODE BootMode;
67
68 Status = PeiServicesGetBootMode (&BootMode);
69 ASSERT_EFI_ERROR (Status);
70
71 //
72 // In S3, we rely on Setup option, because we save to Setup in normal boot.
73 //
74 if (BootMode == BOOT_ON_S3_RESUME) {
75 DEBUG ((EFI_D_ERROR, "DetectTpmDevice: S3 mode\n"));
76 return SetupTpmDevice;
77 }
78
79 if (PcdGetBool (PcdHideTpmSupport) && PcdGetBool (PcdHideTpm)) {
80 DEBUG ((EFI_D_ERROR, "DetectTpmDevice: Tpm is hide\n"));
81 return TPM_DEVICE_NULL;
82 }
83
84 DEBUG ((EFI_D_ERROR, "DetectTpmDevice:\n"));
85 if ((!IsDtpmPresent ()) || (SetupTpmDevice == TPM_DEVICE_NULL)) {
86 // dTPM not available
87 return TPM_DEVICE_NULL;
88 }
89
90 // dTPM available and not disabled by setup
91 // We need check if it is TPM1.2 or TPM2.0
92 // So try TPM1.2 command at first
93
94 Status = Tpm12RequestUseTpm ();
95 if (EFI_ERROR (Status)) {
96 return TPM_DEVICE_2_0_DTPM;
97 }
98
99 Status = Tpm12Startup (TPM_ST_CLEAR);
100 if (EFI_ERROR (Status)) {
101 return TPM_DEVICE_2_0_DTPM;
102 }
103
104 // NO initialization needed again.
105 PcdSet8 (PcdTpmInitializationPolicy, 0);
106 return TPM_DEVICE_1_2;
107 }