TianoCore EDK2 master
Loading...
Searching...
No Matches
DxeTpmMeasurementLib.c
Go to the documentation of this file.
1
10#include <PiDxe.h>
11
12#include <Protocol/TcgService.h>
14
18#include <Library/DebugLib.h>
20
21#include <Guid/Acpi.h>
24
43 IN UINT32 PcrIndex,
44 IN UINT32 EventType,
45 IN VOID *EventLog,
46 IN UINT32 LogLen,
47 IN VOID *HashData,
48 IN UINT64 HashDataLen
49 )
50{
51 EFI_STATUS Status;
52 EFI_TCG_PROTOCOL *TcgProtocol;
53 TCG_PCR_EVENT *TcgEvent;
54 EFI_PHYSICAL_ADDRESS EventLogLastEntry;
55 UINT32 EventNumber;
56
57 TcgEvent = NULL;
58
59 //
60 // Tpm activation state is checked in HashLogExtendEvent
61 //
62 Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **)&TcgProtocol);
63 if (EFI_ERROR (Status)) {
64 return Status;
65 }
66
67 TcgEvent = (TCG_PCR_EVENT *)AllocateZeroPool (sizeof (TCG_PCR_EVENT_HDR) + LogLen);
68 if (TcgEvent == NULL) {
69 return EFI_OUT_OF_RESOURCES;
70 }
71
72 TcgEvent->PCRIndex = PcrIndex;
73 TcgEvent->EventType = EventType;
74 TcgEvent->EventSize = LogLen;
75 CopyMem (&TcgEvent->Event[0], EventLog, LogLen);
76 EventNumber = 1;
77 Status = TcgProtocol->HashLogExtendEvent (
78 TcgProtocol,
79 (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,
80 HashDataLen,
82 TcgEvent,
83 &EventNumber,
84 &EventLogLastEntry
85 );
86
87 FreePool (TcgEvent);
88
89 return Status;
90}
91
107STATIC
110 IN UINT32 PcrIndex,
111 IN UINT32 EventType,
112 IN VOID *EventLog,
113 IN UINT32 LogLen,
114 IN VOID *HashData,
115 IN UINT64 HashDataLen
116 )
117{
118 EFI_STATUS Status;
119 EFI_TCG2_PROTOCOL *Tcg2Protocol;
120 EFI_TCG2_EVENT *Tcg2Event;
121
122 //
123 // TPMPresentFlag is checked in HashLogExtendEvent
124 //
125 Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **)&Tcg2Protocol);
126 if (EFI_ERROR (Status)) {
127 return Status;
128 }
129
130 Tcg2Event = (EFI_TCG2_EVENT *)AllocateZeroPool (LogLen + sizeof (EFI_TCG2_EVENT));
131 if (Tcg2Event == NULL) {
132 return EFI_OUT_OF_RESOURCES;
133 }
134
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);
141
142 Status = Tcg2Protocol->HashLogExtendEvent (
143 Tcg2Protocol,
144 0,
145 (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,
146 HashDataLen,
147 Tcg2Event
148 );
149 FreePool (Tcg2Event);
150
151 return Status;
152}
153
172STATIC
176 IN UINT32 PcrIndex,
177 IN UINT32 EventType,
178 IN VOID *EventLog,
179 IN UINT32 LogLen,
180 IN VOID *HashData,
181 IN UINT64 HashDataLen
182 )
183{
184 EFI_STATUS Status;
185 EFI_CC_EVENT *EfiCcEvent;
186 EFI_CC_MR_INDEX MrIndex;
187
188 if (CcProtocol == NULL) {
189 return EFI_INVALID_PARAMETER;
190 }
191
192 Status = CcProtocol->MapPcrToMrIndex (CcProtocol, PcrIndex, &MrIndex);
193 if (EFI_ERROR (Status)) {
194 return Status;
195 }
196
197 EfiCcEvent = (EFI_CC_EVENT *)AllocateZeroPool (LogLen + sizeof (EFI_CC_EVENT));
198 if (EfiCcEvent == NULL) {
199 return EFI_OUT_OF_RESOURCES;
200 }
201
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);
208
209 Status = CcProtocol->HashLogExtendEvent (
210 CcProtocol,
211 0,
212 (EFI_PHYSICAL_ADDRESS)(UINTN)HashData,
213 HashDataLen,
214 EfiCcEvent
215 );
216 FreePool (EfiCcEvent);
217
218 return Status;
219}
220
237EFIAPI
239 IN UINT32 PcrIndex,
240 IN UINT32 EventType,
241 IN VOID *EventLog,
242 IN UINT32 LogLen,
243 IN VOID *HashData,
244 IN UINT64 HashDataLen
245 )
246{
247 EFI_STATUS Status;
248 EFI_CC_MEASUREMENT_PROTOCOL *CcProtocol;
249
250 Status = gBS->LocateProtocol (&gEfiCcMeasurementProtocolGuid, NULL, (VOID **)&CcProtocol);
251 if (!EFI_ERROR (Status)) {
252 //
253 // Try to measure using Cc measurement protocol
254 //
255 Status = CcMeasureAndLogData (
256 CcProtocol,
257 PcrIndex,
258 EventType,
259 EventLog,
260 LogLen,
261 HashData,
262 HashDataLen
263 );
264 } else {
265 //
266 // Try to measure using Tpm20 protocol
267 //
268 Status = Tpm20MeasureAndLogData (
269 PcrIndex,
270 EventType,
271 EventLog,
272 LogLen,
273 HashData,
274 HashDataLen
275 );
276
277 if (EFI_ERROR (Status)) {
278 //
279 // Try to measure using Tpm1.2 protocol
280 //
281 Status = Tpm12MeasureAndLogData (
282 PcrIndex,
283 EventType,
284 EventLog,
285 LogLen,
286 HashData,
287 HashDataLen
288 );
289 }
290 }
291
292 return Status;
293}
UINT64 UINTN
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
STATIC EFI_STATUS CcMeasureAndLogData(IN EFI_CC_MEASUREMENT_PROTOCOL *CcProtocol, IN UINT32 PcrIndex, IN UINT32 EventType, IN VOID *EventLog, IN UINT32 LogLen, IN VOID *HashData, IN UINT64 HashDataLen)
EFI_STATUS EFIAPI TpmMeasureAndLogData(IN UINT32 PcrIndex, IN UINT32 EventType, IN VOID *EventLog, IN UINT32 LogLen, IN VOID *HashData, IN UINT64 HashDataLen)
STATIC EFI_STATUS Tpm20MeasureAndLogData(IN UINT32 PcrIndex, IN UINT32 EventType, IN VOID *EventLog, IN UINT32 LogLen, IN VOID *HashData, IN UINT64 HashDataLen)
STATIC EFI_STATUS Tpm12MeasureAndLogData(IN UINT32 PcrIndex, IN UINT32 EventType, IN VOID *EventLog, IN UINT32 LogLen, IN VOID *HashData, IN UINT64 HashDataLen)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define IN
Definition: Base.h:279
#define TPM_ALG_SHA
The SHA1 algorithm.
Definition: Tpm12.h:365
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
EFI_BOOT_SERVICES * gBS
UINT8 Event[1]
The event data.
UINT32 EventSize
Size of the event data.
TCG_EVENTTYPE EventType
TCG EFI event type.
TCG_PCRINDEX PCRIndex
PCRIndex event extended to.