2 This library is used by other modules to measure data to TPM.
4 Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved. <BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Protocol/TcgService.h>
12 #include <Protocol/Tcg2Protocol.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/MemoryAllocationLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/TpmMeasurementLib.h>
20 #include <Guid/Acpi.h>
21 #include <IndustryStandard/Acpi.h>
24 Tpm12 measure and log data, and extend the measurement result into a specific PCR.
26 @param[in] PcrIndex PCR Index.
27 @param[in] EventType Event type.
28 @param[in] EventLog Measurement event log.
29 @param[in] LogLen Event log length in bytes.
30 @param[in] HashData The start of the data buffer to be hashed, extended.
31 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
33 @retval EFI_SUCCESS Operation completed successfully.
34 @retval EFI_UNSUPPORTED TPM device not available.
35 @retval EFI_OUT_OF_RESOURCES Out of memory.
36 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
39 Tpm12MeasureAndLogData (
49 EFI_TCG_PROTOCOL
*TcgProtocol
;
50 TCG_PCR_EVENT
*TcgEvent
;
51 EFI_PHYSICAL_ADDRESS EventLogLastEntry
;
57 // Tpm activation state is checked in HashLogExtendEvent
59 Status
= gBS
->LocateProtocol (&gEfiTcgProtocolGuid
, NULL
, (VOID
**)&TcgProtocol
);
60 if (EFI_ERROR (Status
)) {
64 TcgEvent
= (TCG_PCR_EVENT
*)AllocateZeroPool (sizeof (TCG_PCR_EVENT_HDR
) + LogLen
);
65 if (TcgEvent
== NULL
) {
66 return EFI_OUT_OF_RESOURCES
;
69 TcgEvent
->PCRIndex
= PcrIndex
;
70 TcgEvent
->EventType
= EventType
;
71 TcgEvent
->EventSize
= LogLen
;
72 CopyMem (&TcgEvent
->Event
[0], EventLog
, LogLen
);
74 Status
= TcgProtocol
->HashLogExtendEvent (
76 (EFI_PHYSICAL_ADDRESS
)(UINTN
)HashData
,
90 Tpm20 measure and log data, and extend the measurement result into a specific PCR.
92 @param[in] PcrIndex PCR Index.
93 @param[in] EventType Event type.
94 @param[in] EventLog Measurement event log.
95 @param[in] LogLen Event log length in bytes.
96 @param[in] HashData The start of the data buffer to be hashed, extended.
97 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
99 @retval EFI_SUCCESS Operation completed successfully.
100 @retval EFI_UNSUPPORTED TPM device not available.
101 @retval EFI_OUT_OF_RESOURCES Out of memory.
102 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
105 Tpm20MeasureAndLogData (
111 IN UINT64 HashDataLen
115 EFI_TCG2_PROTOCOL
*Tcg2Protocol
;
116 EFI_TCG2_EVENT
*Tcg2Event
;
119 // TPMPresentFlag is checked in HashLogExtendEvent
121 Status
= gBS
->LocateProtocol (&gEfiTcg2ProtocolGuid
, NULL
, (VOID
**)&Tcg2Protocol
);
122 if (EFI_ERROR (Status
)) {
126 Tcg2Event
= (EFI_TCG2_EVENT
*)AllocateZeroPool (LogLen
+ sizeof (EFI_TCG2_EVENT
));
127 if (Tcg2Event
== NULL
) {
128 return EFI_OUT_OF_RESOURCES
;
131 Tcg2Event
->Size
= (UINT32
)LogLen
+ sizeof (EFI_TCG2_EVENT
) - sizeof (Tcg2Event
->Event
);
132 Tcg2Event
->Header
.HeaderSize
= sizeof (EFI_TCG2_EVENT_HEADER
);
133 Tcg2Event
->Header
.HeaderVersion
= EFI_TCG2_EVENT_HEADER_VERSION
;
134 Tcg2Event
->Header
.PCRIndex
= PcrIndex
;
135 Tcg2Event
->Header
.EventType
= EventType
;
136 CopyMem (&Tcg2Event
->Event
[0], EventLog
, LogLen
);
138 Status
= Tcg2Protocol
->HashLogExtendEvent (
141 (EFI_PHYSICAL_ADDRESS
)(UINTN
)HashData
,
145 FreePool (Tcg2Event
);
151 Tpm measure and log data, and extend the measurement result into a specific PCR.
153 @param[in] PcrIndex PCR Index.
154 @param[in] EventType Event type.
155 @param[in] EventLog Measurement event log.
156 @param[in] LogLen Event log length in bytes.
157 @param[in] HashData The start of the data buffer to be hashed, extended.
158 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
160 @retval EFI_SUCCESS Operation completed successfully.
161 @retval EFI_UNSUPPORTED TPM device not available.
162 @retval EFI_OUT_OF_RESOURCES Out of memory.
163 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
167 TpmMeasureAndLogData (
173 IN UINT64 HashDataLen
179 // Try to measure using Tpm20 protocol
181 Status
= Tpm20MeasureAndLogData (
190 if (EFI_ERROR (Status
)) {
192 // Try to measure using Tpm1.2 protocol
194 Status
= Tpm12MeasureAndLogData (