VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/IntelFsp2WrapperPkg/Library/BaseFspMeasurementLib/FspMeasurementLib.c@ 105681

Last change on this file since 105681 was 105670, checked in by vboxsync, 6 months ago

Devices/EFI/FirmwareNew: Merge edk2-stable-202405 and make it build on aarch64, bugref:4643

  • Property svn:eol-style set to native
File size: 8.5 KB
Line 
1/** @file
2 This library is used by FSP modules to measure data to TPM.
3
4Copyright (c) 2020, Intel Corporation. All rights reserved. <BR>
5SPDX-License-Identifier: BSD-2-Clause-Patent
6
7**/
8
9#include <PiPei.h>
10#include <Uefi.h>
11
12#include <Library/BaseMemoryLib.h>
13#include <Library/PeiServicesLib.h>
14#include <Library/PeiServicesTablePointerLib.h>
15#include <Library/PcdLib.h>
16#include <Library/PrintLib.h>
17#include <Library/DebugLib.h>
18#include <Library/FspWrapperApiLib.h>
19#include <Library/TpmMeasurementLib.h>
20#include <Library/FspMeasurementLib.h>
21#include <Library/TcgEventLogRecordLib.h>
22#include <Library/HashLib.h>
23
24#include <Ppi/Tcg.h>
25#include <IndustryStandard/UefiTcgPlatform.h>
26
27/**
28 Tpm measure and log data, and extend the measurement result into a specific PCR.
29
30 @param[in] PcrIndex PCR Index.
31 @param[in] EventType Event type.
32 @param[in] EventLog Measurement event log.
33 @param[in] LogLen Event log length in bytes.
34 @param[in] HashData The start of the data buffer to be hashed, extended.
35 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
36 @param[in] Flags Bitmap providing additional information.
37
38 @retval EFI_SUCCESS Operation completed successfully.
39 @retval EFI_UNSUPPORTED TPM device not available.
40 @retval EFI_OUT_OF_RESOURCES Out of memory.
41 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
42**/
43EFI_STATUS
44EFIAPI
45TpmMeasureAndLogDataWithFlags (
46 IN UINT32 PcrIndex,
47 IN UINT32 EventType,
48 IN VOID *EventLog,
49 IN UINT32 LogLen,
50 IN VOID *HashData,
51 IN UINT64 HashDataLen,
52 IN UINT64 Flags
53 )
54{
55 EFI_STATUS Status;
56 EDKII_TCG_PPI *TcgPpi;
57 TCG_PCR_EVENT_HDR TcgEventHdr;
58
59 Status = PeiServicesLocatePpi (
60 &gEdkiiTcgPpiGuid,
61 0,
62 NULL,
63 (VOID **)&TcgPpi
64 );
65 if (EFI_ERROR (Status)) {
66 return Status;
67 }
68
69 TcgEventHdr.PCRIndex = PcrIndex;
70 TcgEventHdr.EventType = EventType;
71 TcgEventHdr.EventSize = LogLen;
72
73 Status = TcgPpi->HashLogExtendEvent (
74 TcgPpi,
75 Flags,
76 HashData,
77 (UINTN)HashDataLen,
78 &TcgEventHdr,
79 EventLog
80 );
81 return Status;
82}
83
84/**
85 Measure a FSP FirmwareBlob.
86
87 @param[in] Description Description for this FirmwareBlob.
88 @param[in] FirmwareBlobBase Base address of this FirmwareBlob.
89 @param[in] FirmwareBlobLength Size in bytes of this FirmwareBlob.
90 @param[in] CfgRegionOffset Configuration region offset in bytes.
91 @param[in] CfgRegionSize Configuration region in bytes.
92
93 @retval EFI_SUCCESS Operation completed successfully.
94 @retval EFI_UNSUPPORTED TPM device not available.
95 @retval EFI_OUT_OF_RESOURCES Out of memory.
96 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
97**/
98STATIC
99EFI_STATUS
100EFIAPI
101MeasureFspFirmwareBlobWithCfg (
102 IN CHAR8 *Description OPTIONAL,
103 IN EFI_PHYSICAL_ADDRESS FirmwareBlobBase,
104 IN UINT64 FirmwareBlobLength,
105 IN UINT32 CfgRegionOffset,
106 IN UINT32 CfgRegionSize
107 )
108{
109 EFI_PLATFORM_FIRMWARE_BLOB FvBlob, UpdBlob;
110 PLATFORM_FIRMWARE_BLOB2_STRUCT FvBlob2, UpdBlob2;
111 VOID *FvName;
112 UINT32 FvEventType;
113 VOID *FvEventLog, *UpdEventLog;
114 UINT32 FvEventLogSize, UpdEventLogSize;
115 EFI_STATUS Status;
116 HASH_HANDLE HashHandle;
117 UINT8 *HashBase;
118 UINTN HashSize;
119 TPML_DIGEST_VALUES DigestList;
120
121 FvName = TpmMeasurementGetFvName (FirmwareBlobBase, FirmwareBlobLength);
122
123 if (((Description != NULL) || (FvName != NULL)) &&
124 (PcdGet32 (PcdTcgPfpMeasurementRevision) >= TCG_EfiSpecIDEventStruct_SPEC_ERRATA_TPM2_REV_105))
125 {
126 if (Description != NULL) {
127 AsciiSPrint ((CHAR8 *)FvBlob2.BlobDescription, sizeof (FvBlob2.BlobDescription), "%a", Description);
128 AsciiSPrint ((CHAR8 *)UpdBlob2.BlobDescription, sizeof (UpdBlob2.BlobDescription), "%aUDP", Description);
129 } else {
130 AsciiSPrint ((CHAR8 *)FvBlob2.BlobDescription, sizeof (FvBlob2.BlobDescription), "Fv(%g)", FvName);
131 AsciiSPrint ((CHAR8 *)UpdBlob2.BlobDescription, sizeof (UpdBlob2.BlobDescription), "(%g)UDP", FvName);
132 }
133
134 FvBlob2.BlobDescriptionSize = sizeof (FvBlob2.BlobDescription);
135 FvBlob2.BlobBase = FirmwareBlobBase;
136 FvBlob2.BlobLength = FirmwareBlobLength;
137 FvEventType = EV_EFI_PLATFORM_FIRMWARE_BLOB2;
138 FvEventLog = &FvBlob2;
139 FvEventLogSize = sizeof (FvBlob2);
140
141 UpdBlob2.BlobDescriptionSize = sizeof (UpdBlob2.BlobDescription);
142 UpdBlob2.BlobBase = CfgRegionOffset;
143 UpdBlob2.BlobLength = CfgRegionSize;
144 UpdEventLog = &UpdBlob2;
145 UpdEventLogSize = sizeof (UpdBlob2);
146 } else {
147 FvBlob.BlobBase = FirmwareBlobBase;
148 FvBlob.BlobLength = FirmwareBlobLength;
149 FvEventType = EV_EFI_PLATFORM_FIRMWARE_BLOB;
150 FvEventLog = &FvBlob;
151 FvEventLogSize = sizeof (FvBlob);
152
153 UpdBlob.BlobBase = CfgRegionOffset;
154 UpdBlob.BlobLength = CfgRegionSize;
155 UpdEventLog = &UpdBlob;
156 UpdEventLogSize = sizeof (UpdBlob);
157 }
158
159 /** Initialize a SHA hash context. **/
160 Status = HashStart (&HashHandle);
161 if (EFI_ERROR (Status)) {
162 DEBUG ((DEBUG_ERROR, "HashStart failed - %r\n", Status));
163 return Status;
164 }
165
166 /** Hash FSP binary before UDP **/
167 HashBase = (UINT8 *)(UINTN)FirmwareBlobBase;
168 HashSize = (UINTN)CfgRegionOffset;
169 Status = HashUpdate (HashHandle, HashBase, HashSize);
170 if (EFI_ERROR (Status)) {
171 DEBUG ((DEBUG_ERROR, "HashUpdate failed - %r\n", Status));
172 return Status;
173 }
174
175 /** Hash FSP binary after UDP **/
176 HashBase = (UINT8 *)(UINTN)FirmwareBlobBase + CfgRegionOffset + CfgRegionSize;
177 HashSize = (UINTN)(FirmwareBlobLength - CfgRegionOffset - CfgRegionSize);
178 Status = HashUpdate (HashHandle, HashBase, HashSize);
179 if (EFI_ERROR (Status)) {
180 DEBUG ((DEBUG_ERROR, "HashUpdate failed - %r\n", Status));
181 return Status;
182 }
183
184 /** Finalize the SHA hash. **/
185 Status = HashCompleteAndExtend (HashHandle, 0, NULL, 0, &DigestList);
186 if (EFI_ERROR (Status)) {
187 DEBUG ((DEBUG_ERROR, "HashCompleteAndExtend failed - %r\n", Status));
188 return Status;
189 }
190
191 Status = TpmMeasureAndLogDataWithFlags (
192 0,
193 FvEventType,
194 FvEventLog,
195 FvEventLogSize,
196 (UINT8 *)&DigestList,
197 (UINTN)sizeof (DigestList),
198 EDKII_TCG_PRE_HASH_LOG_ONLY
199 );
200 if (EFI_ERROR (Status)) {
201 DEBUG ((DEBUG_ERROR, "TpmMeasureAndLogDataWithFlags failed - %r\n", Status));
202 return Status;
203 }
204
205 Status = TpmMeasureAndLogData (
206 1,
207 EV_PLATFORM_CONFIG_FLAGS,
208 UpdEventLog,
209 UpdEventLogSize,
210 (UINT8 *)(UINTN)FirmwareBlobBase + CfgRegionOffset,
211 CfgRegionSize
212 );
213
214 return Status;
215}
216
217/**
218 Measure a FSP FirmwareBlob.
219
220 @param[in] PcrIndex PCR Index.
221 @param[in] Description Description for this FirmwareBlob.
222 @param[in] FirmwareBlobBase Base address of this FirmwareBlob.
223 @param[in] FirmwareBlobLength Size in bytes of this FirmwareBlob.
224
225 @retval EFI_SUCCESS Operation completed successfully.
226 @retval EFI_UNSUPPORTED TPM device not available.
227 @retval EFI_OUT_OF_RESOURCES Out of memory.
228 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
229**/
230EFI_STATUS
231EFIAPI
232MeasureFspFirmwareBlob (
233 IN UINT32 PcrIndex,
234 IN CHAR8 *Description OPTIONAL,
235 IN EFI_PHYSICAL_ADDRESS FirmwareBlobBase,
236 IN UINT64 FirmwareBlobLength
237 )
238{
239 UINT32 FspMeasureMask;
240 FSP_INFO_HEADER *FspHeaderPtr;
241
242 FspMeasureMask = PcdGet32 (PcdFspMeasurementConfig);
243 if ((FspMeasureMask & FSP_MEASURE_FSPUPD) != 0) {
244 FspHeaderPtr = (FSP_INFO_HEADER *)FspFindFspHeader (FirmwareBlobBase);
245 if (FspHeaderPtr != NULL) {
246 return MeasureFspFirmwareBlobWithCfg (
247 Description,
248 FirmwareBlobBase,
249 FirmwareBlobLength,
250 FspHeaderPtr->CfgRegionOffset,
251 FspHeaderPtr->CfgRegionSize
252 );
253 }
254 }
255
256 return MeasureFirmwareBlob (PcrIndex, Description, FirmwareBlobBase, FirmwareBlobLength);
257}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette