]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/TrEEConfig/TrEEConfigPeim.c
Resolve buffer check overrun issue.
[mirror_edk2.git] / SecurityPkg / Tcg / TrEEConfig / TrEEConfigPeim.c
1 /** @file
2 The module entry point for TrEE configuration module.
3
4 Copyright (c) 2013 - 2014, 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 <Guid/TpmInstance.h>
19
20 #include <Library/BaseLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/MemoryAllocationLib.h>
24 #include <Library/PeiServicesLib.h>
25 #include <Library/PcdLib.h>
26
27 #include <Ppi/ReadOnlyVariable2.h>
28 #include <Protocol/TrEEProtocol.h>
29
30 #include "TrEEConfigNvData.h"
31
32 TPM_INSTANCE_ID mTpmInstanceId[] = TPM_INSTANCE_ID_LIST;
33
34 CONST EFI_PEI_PPI_DESCRIPTOR gTpmSelectedPpi = {
35 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
36 &gEfiTpmDeviceSelectedGuid,
37 NULL
38 };
39
40 /**
41 This routine check both SetupVariable and real TPM device, and return final TpmDevice configuration.
42
43 @param SetupTpmDevice TpmDevice configuration in setup driver
44
45 @return TpmDevice configuration
46 **/
47 UINT8
48 DetectTpmDevice (
49 IN UINT8 SetupTpmDevice
50 );
51
52 /**
53 The entry point for TrEE configuration driver.
54
55 @param FileHandle Handle of the file being invoked.
56 @param PeiServices Describes the list of possible PEI Services.
57
58 @retval EFI_SUCCES Convert variable to PCD successfully.
59 @retval Others Fail to convert variable to PCD.
60 **/
61 EFI_STATUS
62 EFIAPI
63 TrEEConfigPeimEntryPoint (
64 IN EFI_PEI_FILE_HANDLE FileHandle,
65 IN CONST EFI_PEI_SERVICES **PeiServices
66 )
67 {
68 UINTN Size;
69 EFI_STATUS Status;
70 EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi;
71 TREE_CONFIGURATION TrEEConfiguration;
72 UINTN Index;
73 UINT8 TpmDevice;
74
75 Status = PeiServicesLocatePpi (&gEfiPeiReadOnlyVariable2PpiGuid, 0, NULL, (VOID **) &VariablePpi);
76 ASSERT_EFI_ERROR (Status);
77
78 Size = sizeof(TrEEConfiguration);
79 Status = VariablePpi->GetVariable (
80 VariablePpi,
81 TREE_STORAGE_NAME,
82 &gTrEEConfigFormSetGuid,
83 NULL,
84 &Size,
85 &TrEEConfiguration
86 );
87 if (EFI_ERROR (Status)) {
88 //
89 // Variable not ready, set default value
90 //
91 TrEEConfiguration.TpmDevice = TPM_DEVICE_DEFAULT;
92 }
93
94 //
95 // Validation
96 //
97 if ((TrEEConfiguration.TpmDevice > TPM_DEVICE_MAX) || (TrEEConfiguration.TpmDevice < TPM_DEVICE_MIN)) {
98 TrEEConfiguration.TpmDevice = TPM_DEVICE_DEFAULT;
99 }
100
101 //
102 // Although we have SetupVariable info, we still need detect TPM device manually.
103 //
104 DEBUG ((EFI_D_INFO, "TrEEConfiguration.TpmDevice from Setup: %x\n", TrEEConfiguration.TpmDevice));
105
106 if (PcdGetBool (PcdTpmAutoDetection)) {
107 TpmDevice = DetectTpmDevice (TrEEConfiguration.TpmDevice);
108 DEBUG ((EFI_D_INFO, "TpmDevice final: %x\n", TpmDevice));
109 if (TpmDevice != TPM_DEVICE_NULL) {
110 TrEEConfiguration.TpmDevice = TpmDevice;
111 }
112 } else {
113 TpmDevice = TrEEConfiguration.TpmDevice;
114 }
115
116 //
117 // Convert variable to PCD.
118 // This is work-around because there is no gurantee DynamicHiiPcd can return correct value in DXE phase.
119 // Using DynamicPcd instead.
120 //
121 // NOTE: TrEEConfiguration variable contains the desired TpmDevice type,
122 // while PcdTpmInstanceGuid PCD contains the real detected TpmDevice type
123 //
124 for (Index = 0; Index < sizeof(mTpmInstanceId)/sizeof(mTpmInstanceId[0]); Index++) {
125 if (TpmDevice == mTpmInstanceId[Index].TpmDevice) {
126 Size = sizeof(mTpmInstanceId[Index].TpmInstanceGuid);
127 PcdSetPtr (PcdTpmInstanceGuid, &Size, &mTpmInstanceId[Index].TpmInstanceGuid);
128 DEBUG ((EFI_D_INFO, "TpmDevice PCD: %g\n", &mTpmInstanceId[Index].TpmInstanceGuid));
129 break;
130 }
131 }
132
133 //
134 // Selection done
135 //
136 Status = PeiServicesInstallPpi (&gTpmSelectedPpi);
137 ASSERT_EFI_ERROR (Status);
138
139 return Status;
140 }