]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/Tcg2Config/TpmDetection.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 #include "Tcg2Internal.h"
24
25 /**
26 This routine check both SetupVariable and real TPM device, and return final TpmDevice configuration.
27
28 @param SetupTpmDevice TpmDevice configuration in setup driver
29
30 @return TpmDevice configuration
31 **/
32 UINT8
33 DetectTpmDevice (
34 IN UINT8 SetupTpmDevice
35 )
36 {
37 EFI_STATUS Status;
38 EFI_BOOT_MODE BootMode;
39 TCG2_DEVICE_DETECTION Tcg2DeviceDetection;
40 EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi;
41 UINTN Size;
42
43 Status = PeiServicesGetBootMode (&BootMode);
44 ASSERT_EFI_ERROR (Status);
45
46 //
47 // In S3, we rely on normal boot Detection, because we save to ReadOnly Variable in normal boot.
48 //
49 if (BootMode == BOOT_ON_S3_RESUME) {
50 DEBUG ((EFI_D_INFO, "DetectTpmDevice: S3 mode\n"));
51
52 Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariablePpi);
53 ASSERT_EFI_ERROR (Status);
54
55 Size = sizeof(TCG2_DEVICE_DETECTION);
56 ZeroMem (&Tcg2DeviceDetection, sizeof(Tcg2DeviceDetection));
57 Status = VariablePpi->GetVariable (
58 VariablePpi,
59 TCG2_DEVICE_DETECTION_NAME,
60 &gTcg2ConfigFormSetGuid,
61 NULL,
62 &Size,
63 &Tcg2DeviceDetection
64 );
65 if (!EFI_ERROR (Status) &&
66 (Tcg2DeviceDetection.TpmDeviceDetected >= TPM_DEVICE_MIN) &&
67 (Tcg2DeviceDetection.TpmDeviceDetected <= TPM_DEVICE_MAX)) {
68 DEBUG ((EFI_D_ERROR, "TpmDevice from DeviceDetection: %x\n", Tcg2DeviceDetection.TpmDeviceDetected));
69 return Tcg2DeviceDetection.TpmDeviceDetected;
70 }
71 }
72
73 DEBUG ((EFI_D_INFO, "DetectTpmDevice:\n"));
74
75 // dTPM available and not disabled by setup
76 // We need check if it is TPM1.2 or TPM2.0
77 // So try TPM1.2 command at first
78
79 Status = Tpm12RequestUseTpm ();
80 if (EFI_ERROR (Status)) {
81 //
82 // dTPM not available
83 //
84 return TPM_DEVICE_NULL;
85 }
86
87 if (BootMode == BOOT_ON_S3_RESUME) {
88 Status = Tpm12Startup (TPM_ST_STATE);
89 } else {
90 Status = Tpm12Startup (TPM_ST_CLEAR);
91 }
92 if (EFI_ERROR (Status)) {
93 return TPM_DEVICE_2_0_DTPM;
94 }
95
96 // NO initialization needed again.
97 Status = PcdSet8S (PcdTpmInitializationPolicy, 0);
98 ASSERT_EFI_ERROR (Status);
99 return TPM_DEVICE_1_2;
100 }