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