2 This library is used by other modules to measure data to TPM and Confidential
3 Computing (CC) measure registers.
5 Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved. <BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Protocol/TcgService.h>
13 #include <Protocol/Tcg2Protocol.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/MemoryAllocationLib.h>
17 #include <Library/UefiBootServicesTableLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/TpmMeasurementLib.h>
21 #include <Guid/Acpi.h>
22 #include <IndustryStandard/Acpi.h>
23 #include <Protocol/CcMeasurement.h>
26 Tpm12 measure and log data, and extend the measurement result into a specific PCR.
28 @param[in] PcrIndex PCR Index.
29 @param[in] EventType Event type.
30 @param[in] EventLog Measurement event log.
31 @param[in] LogLen Event log length in bytes.
32 @param[in] HashData The start of the data buffer to be hashed, extended.
33 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
35 @retval EFI_SUCCESS Operation completed successfully.
36 @retval EFI_UNSUPPORTED TPM device not available.
37 @retval EFI_OUT_OF_RESOURCES Out of memory.
38 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
42 Tpm12MeasureAndLogData (
52 EFI_TCG_PROTOCOL
*TcgProtocol
;
53 TCG_PCR_EVENT
*TcgEvent
;
54 EFI_PHYSICAL_ADDRESS EventLogLastEntry
;
60 // Tpm activation state is checked in HashLogExtendEvent
62 Status
= gBS
->LocateProtocol (&gEfiTcgProtocolGuid
, NULL
, (VOID
**)&TcgProtocol
);
63 if (EFI_ERROR (Status
)) {
67 TcgEvent
= (TCG_PCR_EVENT
*)AllocateZeroPool (sizeof (TCG_PCR_EVENT_HDR
) + LogLen
);
68 if (TcgEvent
== NULL
) {
69 return EFI_OUT_OF_RESOURCES
;
72 TcgEvent
->PCRIndex
= PcrIndex
;
73 TcgEvent
->EventType
= EventType
;
74 TcgEvent
->EventSize
= LogLen
;
75 CopyMem (&TcgEvent
->Event
[0], EventLog
, LogLen
);
77 Status
= TcgProtocol
->HashLogExtendEvent (
79 (EFI_PHYSICAL_ADDRESS
)(UINTN
)HashData
,
93 Tpm20 measure and log data, and extend the measurement result into a specific PCR.
95 @param[in] PcrIndex PCR Index.
96 @param[in] EventType Event type.
97 @param[in] EventLog Measurement event log.
98 @param[in] LogLen Event log length in bytes.
99 @param[in] HashData The start of the data buffer to be hashed, extended.
100 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
102 @retval EFI_SUCCESS Operation completed successfully.
103 @retval EFI_UNSUPPORTED TPM device not available.
104 @retval EFI_OUT_OF_RESOURCES Out of memory.
105 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
109 Tpm20MeasureAndLogData (
115 IN UINT64 HashDataLen
119 EFI_TCG2_PROTOCOL
*Tcg2Protocol
;
120 EFI_TCG2_EVENT
*Tcg2Event
;
123 // TPMPresentFlag is checked in HashLogExtendEvent
125 Status
= gBS
->LocateProtocol (&gEfiTcg2ProtocolGuid
, NULL
, (VOID
**)&Tcg2Protocol
);
126 if (EFI_ERROR (Status
)) {
130 Tcg2Event
= (EFI_TCG2_EVENT
*)AllocateZeroPool (LogLen
+ sizeof (EFI_TCG2_EVENT
));
131 if (Tcg2Event
== NULL
) {
132 return EFI_OUT_OF_RESOURCES
;
135 Tcg2Event
->Size
= (UINT32
)LogLen
+ sizeof (EFI_TCG2_EVENT
) - sizeof (Tcg2Event
->Event
);
136 Tcg2Event
->Header
.HeaderSize
= sizeof (EFI_TCG2_EVENT_HEADER
);
137 Tcg2Event
->Header
.HeaderVersion
= EFI_TCG2_EVENT_HEADER_VERSION
;
138 Tcg2Event
->Header
.PCRIndex
= PcrIndex
;
139 Tcg2Event
->Header
.EventType
= EventType
;
140 CopyMem (&Tcg2Event
->Event
[0], EventLog
, LogLen
);
142 Status
= Tcg2Protocol
->HashLogExtendEvent (
145 (EFI_PHYSICAL_ADDRESS
)(UINTN
)HashData
,
149 FreePool (Tcg2Event
);
155 Cc measure and log data, and extend the measurement result into a
158 @param[in] CcProtocol Instance of CC measurement protocol
159 @param[in] PcrIndex PCR Index.
160 @param[in] EventType Event type.
161 @param[in] EventLog Measurement event log.
162 @param[in] LogLen Event log length in bytes.
163 @param[in] HashData The start of the data buffer to be hashed, extended.
164 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
166 @retval EFI_SUCCESS Operation completed successfully.
167 @retval EFI_UNSUPPORTED CC guest not available.
168 @retval EFI_OUT_OF_RESOURCES Out of memory.
169 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
170 @retval EFI_INVALID_PARAMETER The input parameter is invalid.
174 CcMeasureAndLogData (
175 IN EFI_CC_MEASUREMENT_PROTOCOL
*CcProtocol
,
181 IN UINT64 HashDataLen
185 EFI_CC_EVENT
*EfiCcEvent
;
186 EFI_CC_MR_INDEX MrIndex
;
188 if (CcProtocol
== NULL
) {
189 return EFI_INVALID_PARAMETER
;
192 Status
= CcProtocol
->MapPcrToMrIndex (CcProtocol
, PcrIndex
, &MrIndex
);
193 if (EFI_ERROR (Status
)) {
197 EfiCcEvent
= (EFI_CC_EVENT
*)AllocateZeroPool (LogLen
+ sizeof (EFI_CC_EVENT
));
198 if (EfiCcEvent
== NULL
) {
199 return EFI_OUT_OF_RESOURCES
;
202 EfiCcEvent
->Size
= (UINT32
)LogLen
+ sizeof (EFI_CC_EVENT
) - sizeof (EfiCcEvent
->Event
);
203 EfiCcEvent
->Header
.HeaderSize
= sizeof (EFI_CC_EVENT_HEADER
);
204 EfiCcEvent
->Header
.HeaderVersion
= EFI_CC_EVENT_HEADER_VERSION
;
205 EfiCcEvent
->Header
.MrIndex
= MrIndex
;
206 EfiCcEvent
->Header
.EventType
= EventType
;
207 CopyMem (&EfiCcEvent
->Event
[0], EventLog
, LogLen
);
209 Status
= CcProtocol
->HashLogExtendEvent (
212 (EFI_PHYSICAL_ADDRESS
)(UINTN
)HashData
,
216 FreePool (EfiCcEvent
);
222 Tpm measure and log data, and extend the measurement result into a specific PCR.
224 @param[in] PcrIndex PCR Index.
225 @param[in] EventType Event type.
226 @param[in] EventLog Measurement event log.
227 @param[in] LogLen Event log length in bytes.
228 @param[in] HashData The start of the data buffer to be hashed, extended.
229 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
231 @retval EFI_SUCCESS Operation completed successfully.
232 @retval EFI_UNSUPPORTED TPM device not available.
233 @retval EFI_OUT_OF_RESOURCES Out of memory.
234 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
238 TpmMeasureAndLogData (
244 IN UINT64 HashDataLen
248 EFI_CC_MEASUREMENT_PROTOCOL
*CcProtocol
;
250 Status
= gBS
->LocateProtocol (&gEfiCcMeasurementProtocolGuid
, NULL
, (VOID
**)&CcProtocol
);
251 if (!EFI_ERROR (Status
)) {
253 // Try to measure using Cc measurement protocol
255 Status
= CcMeasureAndLogData (
266 // Try to measure using Tpm20 protocol
268 Status
= Tpm20MeasureAndLogData (
277 if (EFI_ERROR (Status
)) {
279 // Try to measure using Tpm1.2 protocol
281 Status
= Tpm12MeasureAndLogData (