]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/Tcg2Config/Tcg2ConfigPeim.c
SecurityPkg: Fix spelling errors
[mirror_edk2.git] / SecurityPkg / Tcg / Tcg2Config / Tcg2ConfigPeim.c
1 /** @file
2 The module entry point for Tcg2 configuration module.
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
12 #include <Guid/TpmInstance.h>
13
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/MemoryAllocationLib.h>
18 #include <Library/PeiServicesLib.h>
19 #include <Library/PcdLib.h>
20
21 #include <Ppi/ReadOnlyVariable2.h>
22 #include <Ppi/TpmInitialized.h>
23 #include <Protocol/Tcg2Protocol.h>
24
25 #include "Tcg2ConfigNvData.h"
26 #include "Tcg2Internal.h"
27
28 TPM_INSTANCE_ID mTpmInstanceId[] = TPM_INSTANCE_ID_LIST;
29
30 CONST EFI_PEI_PPI_DESCRIPTOR gTpmSelectedPpi = {
31 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
32 &gEfiTpmDeviceSelectedGuid,
33 NULL
34 };
35
36 EFI_PEI_PPI_DESCRIPTOR mTpmInitializationDonePpiList = {
37 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
38 &gPeiTpmInitializationDonePpiGuid,
39 NULL
40 };
41
42 /**
43 This routine check both SetupVariable and real TPM device, and return final TpmDevice configuration.
44
45 @param SetupTpmDevice TpmDevice configuration in setup driver
46
47 @return TpmDevice configuration
48 **/
49 UINT8
50 DetectTpmDevice (
51 IN UINT8 SetupTpmDevice
52 );
53
54 /**
55 The entry point for Tcg2 configuration driver.
56
57 @param FileHandle Handle of the file being invoked.
58 @param PeiServices Describes the list of possible PEI Services.
59
60 @retval EFI_SUCCESS Convert variable to PCD successfully.
61 @retval Others Fail to convert variable to PCD.
62 **/
63 EFI_STATUS
64 EFIAPI
65 Tcg2ConfigPeimEntryPoint (
66 IN EFI_PEI_FILE_HANDLE FileHandle,
67 IN CONST EFI_PEI_SERVICES **PeiServices
68 )
69 {
70 UINTN Size;
71 EFI_STATUS Status;
72 EFI_STATUS Status2;
73 EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi;
74 TCG2_CONFIGURATION Tcg2Configuration;
75 UINTN Index;
76 UINT8 TpmDevice;
77
78 Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariablePpi);
79 ASSERT_EFI_ERROR (Status);
80
81 Size = sizeof(Tcg2Configuration);
82 Status = VariablePpi->GetVariable (
83 VariablePpi,
84 TCG2_STORAGE_NAME,
85 &gTcg2ConfigFormSetGuid,
86 NULL,
87 &Size,
88 &Tcg2Configuration
89 );
90 if (EFI_ERROR (Status)) {
91 //
92 // Variable not ready, set default value
93 //
94 Tcg2Configuration.TpmDevice = TPM_DEVICE_DEFAULT;
95 }
96
97 //
98 // Validation
99 //
100 if ((Tcg2Configuration.TpmDevice > TPM_DEVICE_MAX) || (Tcg2Configuration.TpmDevice < TPM_DEVICE_MIN)) {
101 Tcg2Configuration.TpmDevice = TPM_DEVICE_DEFAULT;
102 }
103
104 //
105 // Although we have SetupVariable info, we still need detect TPM device manually.
106 //
107 DEBUG ((EFI_D_INFO, "Tcg2Configuration.TpmDevice from Setup: %x\n", Tcg2Configuration.TpmDevice));
108
109 if (PcdGetBool (PcdTpmAutoDetection)) {
110 TpmDevice = DetectTpmDevice (Tcg2Configuration.TpmDevice);
111 DEBUG ((EFI_D_INFO, "TpmDevice final: %x\n", TpmDevice));
112 if (TpmDevice != TPM_DEVICE_NULL) {
113 Tcg2Configuration.TpmDevice = TpmDevice;
114 }
115 } else {
116 TpmDevice = Tcg2Configuration.TpmDevice;
117 }
118
119 //
120 // Convert variable to PCD.
121 // This is work-around because there is no guarantee DynamicHiiPcd can return correct value in DXE phase.
122 // Using DynamicPcd instead.
123 //
124 // NOTE: Tcg2Configuration variable contains the desired TpmDevice type,
125 // while PcdTpmInstanceGuid PCD contains the real detected TpmDevice type
126 //
127 for (Index = 0; Index < sizeof(mTpmInstanceId)/sizeof(mTpmInstanceId[0]); Index++) {
128 if (TpmDevice == mTpmInstanceId[Index].TpmDevice) {
129 Size = sizeof(mTpmInstanceId[Index].TpmInstanceGuid);
130 Status = PcdSetPtrS (PcdTpmInstanceGuid, &Size, &mTpmInstanceId[Index].TpmInstanceGuid);
131 ASSERT_EFI_ERROR (Status);
132 DEBUG ((EFI_D_INFO, "TpmDevice PCD: %g\n", &mTpmInstanceId[Index].TpmInstanceGuid));
133 break;
134 }
135 }
136
137 //
138 // Selection done
139 //
140 Status = PeiServicesInstallPpi (&gTpmSelectedPpi);
141 ASSERT_EFI_ERROR (Status);
142
143 //
144 // Even if no TPM is selected or detected, we still need intall TpmInitializationDonePpi.
145 // Because TcgPei or Tcg2Pei will not run, but we still need a way to notify other driver.
146 // Other driver can know TPM initialization state by TpmInitializedPpi.
147 //
148 if (CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceNoneGuid)) {
149 Status2 = PeiServicesInstallPpi (&mTpmInitializationDonePpiList);
150 ASSERT_EFI_ERROR (Status2);
151 }
152
153 return Status;
154 }