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>
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.
41 Tpm12MeasureAndLogData (
51 EFI_TCG_PROTOCOL
*TcgProtocol
;
52 TCG_PCR_EVENT
*TcgEvent
;
53 EFI_PHYSICAL_ADDRESS EventLogLastEntry
;
59 // Tpm active/deactive state is checked in HashLogExtendEvent
61 Status
= gBS
->LocateProtocol (&gEfiTcgProtocolGuid
, NULL
, (VOID
**) &TcgProtocol
);
62 if (EFI_ERROR(Status
)){
66 TcgEvent
= (TCG_PCR_EVENT
*)AllocateZeroPool (sizeof (TCG_PCR_EVENT_HDR
) + LogLen
);
67 if(TcgEvent
== NULL
) {
68 return EFI_OUT_OF_RESOURCES
;
71 TcgEvent
->PCRIndex
= PcrIndex
;
72 TcgEvent
->EventType
= EventType
;
73 TcgEvent
->EventSize
= LogLen
;
74 CopyMem (&TcgEvent
->Event
[0], EventLog
, LogLen
);
76 Status
= TcgProtocol
->HashLogExtendEvent (
78 (EFI_PHYSICAL_ADDRESS
)(UINTN
)HashData
,
92 Tpm20 measure and log data, and extend the measurement result into a specific PCR.
94 @param[in] PcrIndex PCR Index.
95 @param[in] EventType Event type.
96 @param[in] EventLog Measurement event log.
97 @param[in] LogLen Event log length in bytes.
98 @param[in] HashData The start of the data buffer to be hashed, extended.
99 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
101 @retval EFI_SUCCESS Operation completed successfully.
102 @retval EFI_UNSUPPORTED TPM device not available.
103 @retval EFI_OUT_OF_RESOURCES Out of memory.
104 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
107 Tpm20MeasureAndLogData (
113 IN UINT64 HashDataLen
117 EFI_TCG2_PROTOCOL
*Tcg2Protocol
;
118 EFI_TCG2_EVENT
*Tcg2Event
;
121 // TPMPresentFlag is checked in HashLogExtendEvent
123 Status
= gBS
->LocateProtocol (&gEfiTcg2ProtocolGuid
, NULL
, (VOID
**) &Tcg2Protocol
);
124 if (EFI_ERROR (Status
)) {
128 Tcg2Event
= (EFI_TCG2_EVENT
*) AllocateZeroPool (LogLen
+ sizeof (EFI_TCG2_EVENT
));
129 if(Tcg2Event
== NULL
) {
130 return EFI_OUT_OF_RESOURCES
;
133 Tcg2Event
->Size
= (UINT32
)LogLen
+ sizeof (EFI_TCG2_EVENT
) - sizeof(Tcg2Event
->Event
);
134 Tcg2Event
->Header
.HeaderSize
= sizeof(EFI_TCG2_EVENT_HEADER
);
135 Tcg2Event
->Header
.HeaderVersion
= EFI_TCG2_EVENT_HEADER_VERSION
;
136 Tcg2Event
->Header
.PCRIndex
= PcrIndex
;
137 Tcg2Event
->Header
.EventType
= EventType
;
138 CopyMem (&Tcg2Event
->Event
[0], EventLog
, LogLen
);
140 Status
= Tcg2Protocol
->HashLogExtendEvent (
143 (EFI_PHYSICAL_ADDRESS
)(UINTN
)HashData
,
147 FreePool (Tcg2Event
);
153 Tpm measure and log data, and extend the measurement result into a specific PCR.
155 @param[in] PcrIndex PCR Index.
156 @param[in] EventType Event type.
157 @param[in] EventLog Measurement event log.
158 @param[in] LogLen Event log length in bytes.
159 @param[in] HashData The start of the data buffer to be hashed, extended.
160 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
162 @retval EFI_SUCCESS Operation completed successfully.
163 @retval EFI_UNSUPPORTED TPM device not available.
164 @retval EFI_OUT_OF_RESOURCES Out of memory.
165 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
169 TpmMeasureAndLogData (
175 IN UINT64 HashDataLen
181 // Try to measure using Tpm20 protocol
183 Status
= Tpm20MeasureAndLogData(
192 if (EFI_ERROR (Status
)) {
194 // Try to measure using Tpm1.2 protocol
196 Status
= Tpm12MeasureAndLogData(