6ea3631b025b2f360bb74ae5ece113300e973346
[mirror_edk2.git] / SecurityPkg / Tcg / Tcg2Dxe / Tcg2Dxe.c
1 /** @file
2 This module implements Tcg2 Protocol.
3
4 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include <PiDxe.h>
17 #include <IndustryStandard/Acpi.h>
18 #include <IndustryStandard/PeImage.h>
19 #include <IndustryStandard/TcpaAcpi.h>
20
21 #include <Guid/GlobalVariable.h>
22 #include <Guid/HobList.h>
23 #include <Guid/TcgEventHob.h>
24 #include <Guid/EventGroup.h>
25 #include <Guid/EventExitBootServiceFailed.h>
26 #include <Guid/ImageAuthentication.h>
27 #include <Guid/TpmInstance.h>
28
29 #include <Protocol/DevicePath.h>
30 #include <Protocol/MpService.h>
31 #include <Protocol/VariableWrite.h>
32 #include <Protocol/Tcg2Protocol.h>
33 #include <Protocol/TrEEProtocol.h>
34
35 #include <Library/DebugLib.h>
36 #include <Library/BaseMemoryLib.h>
37 #include <Library/UefiRuntimeServicesTableLib.h>
38 #include <Library/UefiDriverEntryPoint.h>
39 #include <Library/HobLib.h>
40 #include <Library/UefiBootServicesTableLib.h>
41 #include <Library/BaseLib.h>
42 #include <Library/MemoryAllocationLib.h>
43 #include <Library/PrintLib.h>
44 #include <Library/Tpm2CommandLib.h>
45 #include <Library/PcdLib.h>
46 #include <Library/UefiLib.h>
47 #include <Library/Tpm2DeviceLib.h>
48 #include <Library/HashLib.h>
49 #include <Library/PerformanceLib.h>
50 #include <Library/ReportStatusCodeLib.h>
51 #include <Library/Tcg2PhysicalPresenceLib.h>
52
53 #define PERF_ID_TCG2_DXE 0x3120
54
55 typedef struct {
56 CHAR16 *VariableName;
57 EFI_GUID *VendorGuid;
58 } VARIABLE_TYPE;
59
60 #define TCG2_DEFAULT_MAX_COMMAND_SIZE 0x1000
61 #define TCG2_DEFAULT_MAX_RESPONSE_SIZE 0x1000
62
63 typedef struct {
64 EFI_GUID *EventGuid;
65 EFI_TCG2_EVENT_LOG_FORMAT LogFormat;
66 } TCG2_EVENT_INFO_STRUCT;
67
68 TCG2_EVENT_INFO_STRUCT mTcg2EventInfo[] = {
69 {&gTcgEventEntryHobGuid, EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2},
70 {&gTcgEvent2EntryHobGuid, EFI_TCG2_EVENT_LOG_FORMAT_TCG_2},
71 };
72
73 #define TCG_EVENT_LOG_AREA_COUNT_MAX 2
74
75 typedef struct {
76 EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat;
77 EFI_PHYSICAL_ADDRESS Lasa;
78 UINT64 Laml;
79 UINTN EventLogSize;
80 UINT8 *LastEvent;
81 BOOLEAN EventLogStarted;
82 BOOLEAN EventLogTruncated;
83 } TCG_EVENT_LOG_AREA_STRUCT;
84
85 typedef struct _TCG_DXE_DATA {
86 EFI_TCG2_BOOT_SERVICE_CAPABILITY BsCap;
87 TCG_EVENT_LOG_AREA_STRUCT EventLogAreaStruct[TCG_EVENT_LOG_AREA_COUNT_MAX];
88 BOOLEAN GetEventLogCalled[TCG_EVENT_LOG_AREA_COUNT_MAX];
89 TCG_EVENT_LOG_AREA_STRUCT FinalEventLogAreaStruct[TCG_EVENT_LOG_AREA_COUNT_MAX];
90 EFI_TCG2_FINAL_EVENTS_TABLE *FinalEventsTable[TCG_EVENT_LOG_AREA_COUNT_MAX];
91 } TCG_DXE_DATA;
92
93 TCG_DXE_DATA mTcgDxeData = {
94 {
95 sizeof (EFI_TCG2_BOOT_SERVICE_CAPABILITY), // Size
96 { 1, 1 }, // StructureVersion
97 { 1, 1 }, // ProtocolVersion
98 EFI_TCG2_BOOT_HASH_ALG_SHA1, // HashAlgorithmBitmap
99 EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2, // SupportedEventLogs
100 TRUE, // TPMPresentFlag
101 TCG2_DEFAULT_MAX_COMMAND_SIZE, // MaxCommandSize
102 TCG2_DEFAULT_MAX_RESPONSE_SIZE, // MaxResponseSize
103 0, // ManufacturerID
104 0, // NumberOfPCRBanks
105 0, // ActivePcrBanks
106 },
107 };
108
109 UINTN mBootAttempts = 0;
110 CHAR16 mBootVarName[] = L"BootOrder";
111
112 VARIABLE_TYPE mVariableType[] = {
113 {EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid},
114 {EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid},
115 {EFI_KEY_EXCHANGE_KEY_NAME, &gEfiGlobalVariableGuid},
116 {EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid},
117 {EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid},
118 {EFI_IMAGE_SECURITY_DATABASE2, &gEfiImageSecurityDatabaseGuid},
119 };
120
121 EFI_HANDLE mImageHandle;
122
123 /**
124 Measure PE image into TPM log based on the authenticode image hashing in
125 PE/COFF Specification 8.0 Appendix A.
126
127 Caution: This function may receive untrusted input.
128 PE/COFF image is external input, so this function will validate its data structure
129 within this image buffer before use.
130
131 Notes: PE/COFF image is checked by BasePeCoffLib PeCoffLoaderGetImageInfo().
132
133 @param[in] PCRIndex TPM PCR index
134 @param[in] ImageAddress Start address of image buffer.
135 @param[in] ImageSize Image size
136 @param[out] DigestList Digeest list of this image.
137
138 @retval EFI_SUCCESS Successfully measure image.
139 @retval EFI_OUT_OF_RESOURCES No enough resource to measure image.
140 @retval other error value
141 **/
142 EFI_STATUS
143 MeasurePeImageAndExtend (
144 IN UINT32 PCRIndex,
145 IN EFI_PHYSICAL_ADDRESS ImageAddress,
146 IN UINTN ImageSize,
147 OUT TPML_DIGEST_VALUES *DigestList
148 );
149
150 /**
151
152 This function dump raw data.
153
154 @param Data raw data
155 @param Size raw data size
156
157 **/
158 VOID
159 InternalDumpData (
160 IN UINT8 *Data,
161 IN UINTN Size
162 )
163 {
164 UINTN Index;
165 for (Index = 0; Index < Size; Index++) {
166 DEBUG ((EFI_D_INFO, "%02x", (UINTN)Data[Index]));
167 }
168 }
169
170 /**
171
172 This function dump raw data with colume format.
173
174 @param Data raw data
175 @param Size raw data size
176
177 **/
178 VOID
179 InternalDumpHex (
180 IN UINT8 *Data,
181 IN UINTN Size
182 )
183 {
184 UINTN Index;
185 UINTN Count;
186 UINTN Left;
187
188 #define COLUME_SIZE (16 * 2)
189
190 Count = Size / COLUME_SIZE;
191 Left = Size % COLUME_SIZE;
192 for (Index = 0; Index < Count; Index++) {
193 DEBUG ((EFI_D_INFO, "%04x: ", Index * COLUME_SIZE));
194 InternalDumpData (Data + Index * COLUME_SIZE, COLUME_SIZE);
195 DEBUG ((EFI_D_INFO, "\n"));
196 }
197
198 if (Left != 0) {
199 DEBUG ((EFI_D_INFO, "%04x: ", Index * COLUME_SIZE));
200 InternalDumpData (Data + Index * COLUME_SIZE, Left);
201 DEBUG ((EFI_D_INFO, "\n"));
202 }
203 }
204
205 /**
206 Get All processors EFI_CPU_LOCATION in system. LocationBuf is allocated inside the function
207 Caller is responsible to free LocationBuf.
208
209 @param[out] LocationBuf Returns Processor Location Buffer.
210 @param[out] Num Returns processor number.
211
212 @retval EFI_SUCCESS Operation completed successfully.
213 @retval EFI_UNSUPPORTED MpService protocol not found.
214
215 **/
216 EFI_STATUS
217 GetProcessorsCpuLocation (
218 OUT EFI_CPU_PHYSICAL_LOCATION **LocationBuf,
219 OUT UINTN *Num
220 )
221 {
222 EFI_STATUS Status;
223 EFI_MP_SERVICES_PROTOCOL *MpProtocol;
224 UINTN ProcessorNum;
225 UINTN EnabledProcessorNum;
226 EFI_PROCESSOR_INFORMATION ProcessorInfo;
227 EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;
228 UINTN Index;
229
230 Status = gBS->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **) &MpProtocol);
231 if (EFI_ERROR (Status)) {
232 //
233 // MP protocol is not installed
234 //
235 return EFI_UNSUPPORTED;
236 }
237
238 Status = MpProtocol->GetNumberOfProcessors(
239 MpProtocol,
240 &ProcessorNum,
241 &EnabledProcessorNum
242 );
243 if (EFI_ERROR(Status)){
244 return Status;
245 }
246
247 Status = gBS->AllocatePool(
248 EfiBootServicesData,
249 sizeof(EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,
250 (VOID **) &ProcessorLocBuf
251 );
252 if (EFI_ERROR(Status)){
253 return Status;
254 }
255
256 //
257 // Get each processor Location info
258 //
259 for (Index = 0; Index < ProcessorNum; Index++) {
260 Status = MpProtocol->GetProcessorInfo(
261 MpProtocol,
262 Index,
263 &ProcessorInfo
264 );
265 if (EFI_ERROR(Status)){
266 FreePool(ProcessorLocBuf);
267 return Status;
268 }
269
270 //
271 // Get all Processor Location info & measure
272 //
273 CopyMem(
274 &ProcessorLocBuf[Index],
275 &ProcessorInfo.Location,
276 sizeof(EFI_CPU_PHYSICAL_LOCATION)
277 );
278 }
279
280 *LocationBuf = ProcessorLocBuf;
281 *Num = ProcessorNum;
282
283 return Status;
284 }
285
286 /**
287 The EFI_TCG2_PROTOCOL GetCapability function call provides protocol
288 capability information and state information.
289
290 @param[in] This Indicates the calling context
291 @param[in, out] ProtocolCapability The caller allocates memory for a EFI_TCG2_BOOT_SERVICE_CAPABILITY
292 structure and sets the size field to the size of the structure allocated.
293 The callee fills in the fields with the EFI protocol capability information
294 and the current EFI TCG2 state information up to the number of fields which
295 fit within the size of the structure passed in.
296
297 @retval EFI_SUCCESS Operation completed successfully.
298 @retval EFI_DEVICE_ERROR The command was unsuccessful.
299 The ProtocolCapability variable will not be populated.
300 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
301 The ProtocolCapability variable will not be populated.
302 @retval EFI_BUFFER_TOO_SMALL The ProtocolCapability variable is too small to hold the full response.
303 It will be partially populated (required Size field will be set).
304 **/
305 EFI_STATUS
306 EFIAPI
307 Tcg2GetCapability (
308 IN EFI_TCG2_PROTOCOL *This,
309 IN OUT EFI_TCG2_BOOT_SERVICE_CAPABILITY *ProtocolCapability
310 )
311 {
312 DEBUG ((DEBUG_VERBOSE, "Tcg2GetCapability ...\n"));
313
314 if ((This == NULL) || (ProtocolCapability == NULL)) {
315 return EFI_INVALID_PARAMETER;
316 }
317
318 DEBUG ((DEBUG_VERBOSE, "Size - 0x%x\n", ProtocolCapability->Size));
319 DEBUG ((DEBUG_VERBOSE, " 1.1 - 0x%x, 1.0 - 0x%x\n", sizeof(EFI_TCG2_BOOT_SERVICE_CAPABILITY), sizeof(TREE_BOOT_SERVICE_CAPABILITY_1_0)));
320
321 if (ProtocolCapability->Size < mTcgDxeData.BsCap.Size) {
322 //
323 // Handle the case that firmware support 1.1 but OS only support 1.0.
324 //
325 if ((mTcgDxeData.BsCap.ProtocolVersion.Major > 0x01) ||
326 ((mTcgDxeData.BsCap.ProtocolVersion.Major == 0x01) && ((mTcgDxeData.BsCap.ProtocolVersion.Minor > 0x00)))) {
327 if (ProtocolCapability->Size >= sizeof(TREE_BOOT_SERVICE_CAPABILITY_1_0)) {
328 CopyMem (ProtocolCapability, &mTcgDxeData.BsCap, sizeof(TREE_BOOT_SERVICE_CAPABILITY_1_0));
329 ProtocolCapability->Size = sizeof(TREE_BOOT_SERVICE_CAPABILITY_1_0);
330 ProtocolCapability->StructureVersion.Major = 1;
331 ProtocolCapability->StructureVersion.Minor = 0;
332 ProtocolCapability->ProtocolVersion.Major = 1;
333 ProtocolCapability->ProtocolVersion.Minor = 0;
334 DEBUG ((EFI_D_ERROR, "TreeGetCapability (Compatible) - %r\n", EFI_SUCCESS));
335 return EFI_SUCCESS;
336 }
337 }
338 ProtocolCapability->Size = mTcgDxeData.BsCap.Size;
339 return EFI_BUFFER_TOO_SMALL;
340 }
341
342 CopyMem (ProtocolCapability, &mTcgDxeData.BsCap, mTcgDxeData.BsCap.Size);
343 DEBUG ((DEBUG_VERBOSE, "Tcg2GetCapability - %r\n", EFI_SUCCESS));
344 return EFI_SUCCESS;
345 }
346
347 /**
348 This function dump PCR event.
349
350 @param[in] EventHdr TCG PCR event structure.
351 **/
352 VOID
353 DumpEvent (
354 IN TCG_PCR_EVENT_HDR *EventHdr
355 )
356 {
357 UINTN Index;
358
359 DEBUG ((EFI_D_INFO, " Event:\n"));
360 DEBUG ((EFI_D_INFO, " PCRIndex - %d\n", EventHdr->PCRIndex));
361 DEBUG ((EFI_D_INFO, " EventType - 0x%08x\n", EventHdr->EventType));
362 DEBUG ((EFI_D_INFO, " Digest - "));
363 for (Index = 0; Index < sizeof(TCG_DIGEST); Index++) {
364 DEBUG ((EFI_D_INFO, "%02x ", EventHdr->Digest.digest[Index]));
365 }
366 DEBUG ((EFI_D_INFO, "\n"));
367 DEBUG ((EFI_D_INFO, " EventSize - 0x%08x\n", EventHdr->EventSize));
368 InternalDumpHex ((UINT8 *)(EventHdr + 1), EventHdr->EventSize);
369 }
370
371 /**
372 This function dump TCG_EfiSpecIDEventStruct.
373
374 @param[in] TcgEfiSpecIdEventStruct A pointer to TCG_EfiSpecIDEventStruct.
375 **/
376 VOID
377 DumpTcgEfiSpecIdEventStruct (
378 IN TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct
379 )
380 {
381 TCG_EfiSpecIdEventAlgorithmSize *DigestSize;
382 UINTN Index;
383 UINT8 *VendorInfoSize;
384 UINT8 *VendorInfo;
385 UINT32 NumberOfAlgorithms;
386
387 DEBUG ((EFI_D_INFO, " TCG_EfiSpecIDEventStruct:\n"));
388 DEBUG ((EFI_D_INFO, " signature - '"));
389 for (Index = 0; Index < sizeof(TcgEfiSpecIdEventStruct->signature); Index++) {
390 DEBUG ((EFI_D_INFO, "%c", TcgEfiSpecIdEventStruct->signature[Index]));
391 }
392 DEBUG ((EFI_D_INFO, "'\n"));
393 DEBUG ((EFI_D_INFO, " platformClass - 0x%08x\n", TcgEfiSpecIdEventStruct->platformClass));
394 DEBUG ((EFI_D_INFO, " specVersion - %d.%d%d\n", TcgEfiSpecIdEventStruct->specVersionMajor, TcgEfiSpecIdEventStruct->specVersionMinor, TcgEfiSpecIdEventStruct->specErrata));
395 DEBUG ((EFI_D_INFO, " uintnSize - 0x%02x\n", TcgEfiSpecIdEventStruct->uintnSize));
396
397 CopyMem (&NumberOfAlgorithms, TcgEfiSpecIdEventStruct + 1, sizeof(NumberOfAlgorithms));
398 DEBUG ((EFI_D_INFO, " NumberOfAlgorithms - 0x%08x\n", NumberOfAlgorithms));
399
400 DigestSize = (TCG_EfiSpecIdEventAlgorithmSize *)((UINT8 *)TcgEfiSpecIdEventStruct + sizeof(*TcgEfiSpecIdEventStruct) + sizeof(NumberOfAlgorithms));
401 for (Index = 0; Index < NumberOfAlgorithms; Index++) {
402 DEBUG ((EFI_D_INFO, " digest(%d)\n", Index));
403 DEBUG ((EFI_D_INFO, " algorithmId - 0x%04x\n", DigestSize[Index].algorithmId));
404 DEBUG ((EFI_D_INFO, " digestSize - 0x%04x\n", DigestSize[Index].digestSize));
405 }
406 VendorInfoSize = (UINT8 *)&DigestSize[NumberOfAlgorithms];
407 DEBUG ((EFI_D_INFO, " VendorInfoSize - 0x%02x\n", *VendorInfoSize));
408 VendorInfo = VendorInfoSize + 1;
409 DEBUG ((EFI_D_INFO, " VendorInfo - "));
410 for (Index = 0; Index < *VendorInfoSize; Index++) {
411 DEBUG ((EFI_D_INFO, "%02x ", VendorInfo[Index]));
412 }
413 DEBUG ((EFI_D_INFO, "\n"));
414 }
415
416 /**
417 This function get size of TCG_EfiSpecIDEventStruct.
418
419 @param[in] TcgEfiSpecIdEventStruct A pointer to TCG_EfiSpecIDEventStruct.
420 **/
421 UINTN
422 GetTcgEfiSpecIdEventStructSize (
423 IN TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct
424 )
425 {
426 TCG_EfiSpecIdEventAlgorithmSize *DigestSize;
427 UINT8 *VendorInfoSize;
428 UINT32 NumberOfAlgorithms;
429
430 CopyMem (&NumberOfAlgorithms, TcgEfiSpecIdEventStruct + 1, sizeof(NumberOfAlgorithms));
431
432 DigestSize = (TCG_EfiSpecIdEventAlgorithmSize *)((UINT8 *)TcgEfiSpecIdEventStruct + sizeof(*TcgEfiSpecIdEventStruct) + sizeof(NumberOfAlgorithms));
433 VendorInfoSize = (UINT8 *)&DigestSize[NumberOfAlgorithms];
434 return sizeof(TCG_EfiSpecIDEventStruct) + sizeof(UINT32) + (NumberOfAlgorithms * sizeof(TCG_EfiSpecIdEventAlgorithmSize)) + sizeof(UINT8) + (*VendorInfoSize);
435 }
436
437 /**
438 This function dump PCR event 2.
439
440 @param[in] TcgPcrEvent2 TCG PCR event 2 structure.
441 **/
442 VOID
443 DumpEvent2 (
444 IN TCG_PCR_EVENT2 *TcgPcrEvent2
445 )
446 {
447 UINTN Index;
448 UINT32 DigestIndex;
449 UINT32 DigestCount;
450 TPMI_ALG_HASH HashAlgo;
451 UINT32 DigestSize;
452 UINT8 *DigestBuffer;
453 UINT32 EventSize;
454 UINT8 *EventBuffer;
455
456 DEBUG ((EFI_D_INFO, " Event:\n"));
457 DEBUG ((EFI_D_INFO, " PCRIndex - %d\n", TcgPcrEvent2->PCRIndex));
458 DEBUG ((EFI_D_INFO, " EventType - 0x%08x\n", TcgPcrEvent2->EventType));
459
460 DEBUG ((EFI_D_INFO, " DigestCount: 0x%08x\n", TcgPcrEvent2->Digest.count));
461
462 DigestCount = TcgPcrEvent2->Digest.count;
463 HashAlgo = TcgPcrEvent2->Digest.digests[0].hashAlg;
464 DigestBuffer = (UINT8 *)&TcgPcrEvent2->Digest.digests[0].digest;
465 for (DigestIndex = 0; DigestIndex < DigestCount; DigestIndex++) {
466 DEBUG ((EFI_D_INFO, " HashAlgo : 0x%04x\n", HashAlgo));
467 DEBUG ((EFI_D_INFO, " Digest(%d): ", DigestIndex));
468 DigestSize = GetHashSizeFromAlgo (HashAlgo);
469 for (Index = 0; Index < DigestSize; Index++) {
470 DEBUG ((EFI_D_INFO, "%02x ", DigestBuffer[Index]));
471 }
472 DEBUG ((EFI_D_INFO, "\n"));
473 //
474 // Prepare next
475 //
476 CopyMem (&HashAlgo, DigestBuffer + DigestSize, sizeof(TPMI_ALG_HASH));
477 DigestBuffer = DigestBuffer + DigestSize + sizeof(TPMI_ALG_HASH);
478 }
479 DEBUG ((EFI_D_INFO, "\n"));
480 DigestBuffer = DigestBuffer - sizeof(TPMI_ALG_HASH);
481
482 CopyMem (&EventSize, DigestBuffer, sizeof(TcgPcrEvent2->EventSize));
483 DEBUG ((EFI_D_INFO, " EventSize - 0x%08x\n", EventSize));
484 EventBuffer = DigestBuffer + sizeof(TcgPcrEvent2->EventSize);
485 InternalDumpHex (EventBuffer, EventSize);
486 }
487
488 /**
489 This function returns size of TCG PCR event 2.
490
491 @param[in] TcgPcrEvent2 TCG PCR event 2 structure.
492
493 @return size of TCG PCR event 2.
494 **/
495 UINTN
496 GetPcrEvent2Size (
497 IN TCG_PCR_EVENT2 *TcgPcrEvent2
498 )
499 {
500 UINT32 DigestIndex;
501 UINT32 DigestCount;
502 TPMI_ALG_HASH HashAlgo;
503 UINT32 DigestSize;
504 UINT8 *DigestBuffer;
505 UINT32 EventSize;
506 UINT8 *EventBuffer;
507
508 DigestCount = TcgPcrEvent2->Digest.count;
509 HashAlgo = TcgPcrEvent2->Digest.digests[0].hashAlg;
510 DigestBuffer = (UINT8 *)&TcgPcrEvent2->Digest.digests[0].digest;
511 for (DigestIndex = 0; DigestIndex < DigestCount; DigestIndex++) {
512 DigestSize = GetHashSizeFromAlgo (HashAlgo);
513 //
514 // Prepare next
515 //
516 CopyMem (&HashAlgo, DigestBuffer + DigestSize, sizeof(TPMI_ALG_HASH));
517 DigestBuffer = DigestBuffer + DigestSize + sizeof(TPMI_ALG_HASH);
518 }
519 DigestBuffer = DigestBuffer - sizeof(TPMI_ALG_HASH);
520
521 CopyMem (&EventSize, DigestBuffer, sizeof(TcgPcrEvent2->EventSize));
522 EventBuffer = DigestBuffer + sizeof(TcgPcrEvent2->EventSize);
523
524 return (UINTN)EventBuffer + EventSize - (UINTN)TcgPcrEvent2;
525 }
526
527 /**
528 This function dump event log.
529
530 @param[in] EventLogFormat The type of the event log for which the information is requested.
531 @param[in] EventLogLocation A pointer to the memory address of the event log.
532 @param[in] EventLogLastEntry If the Event Log contains more than one entry, this is a pointer to the
533 address of the start of the last entry in the event log in memory.
534 @param[in] FinalEventsTable A pointer to the memory address of the final event table.
535 **/
536 VOID
537 DumpEventLog (
538 IN EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat,
539 IN EFI_PHYSICAL_ADDRESS EventLogLocation,
540 IN EFI_PHYSICAL_ADDRESS EventLogLastEntry,
541 IN EFI_TCG2_FINAL_EVENTS_TABLE *FinalEventsTable
542 )
543 {
544 TCG_PCR_EVENT_HDR *EventHdr;
545 TCG_PCR_EVENT2 *TcgPcrEvent2;
546 TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct;
547 UINTN NumberOfEvents;
548
549 DEBUG ((EFI_D_INFO, "EventLogFormat: (0x%x)\n", EventLogFormat));
550
551 switch (EventLogFormat) {
552 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2:
553 EventHdr = (TCG_PCR_EVENT_HDR *)(UINTN)EventLogLocation;
554 while ((UINTN)EventHdr <= EventLogLastEntry) {
555 DumpEvent (EventHdr);
556 EventHdr = (TCG_PCR_EVENT_HDR *)((UINTN)EventHdr + sizeof(TCG_PCR_EVENT_HDR) + EventHdr->EventSize);
557 }
558 if (FinalEventsTable == NULL) {
559 DEBUG ((EFI_D_INFO, "FinalEventsTable: NOT FOUND\n"));
560 } else {
561 DEBUG ((EFI_D_INFO, "FinalEventsTable: (0x%x)\n", FinalEventsTable));
562 DEBUG ((EFI_D_INFO, " Version: (0x%x)\n", FinalEventsTable->Version));
563 DEBUG ((EFI_D_INFO, " NumberOfEvents: (0x%x)\n", FinalEventsTable->NumberOfEvents));
564
565 EventHdr = (TCG_PCR_EVENT_HDR *)(UINTN)(FinalEventsTable + 1);
566 for (NumberOfEvents = 0; NumberOfEvents < FinalEventsTable->NumberOfEvents; NumberOfEvents++) {
567 DumpEvent (EventHdr);
568 EventHdr = (TCG_PCR_EVENT_HDR *)((UINTN)EventHdr + sizeof(TCG_PCR_EVENT_HDR) + EventHdr->EventSize);
569 }
570 }
571 break;
572 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_2:
573 //
574 // Dump first event
575 //
576 EventHdr = (TCG_PCR_EVENT_HDR *)(UINTN)EventLogLocation;
577 DumpEvent (EventHdr);
578
579 TcgEfiSpecIdEventStruct = (TCG_EfiSpecIDEventStruct *)(EventHdr + 1);
580 DumpTcgEfiSpecIdEventStruct (TcgEfiSpecIdEventStruct);
581
582 TcgPcrEvent2 = (TCG_PCR_EVENT2 *)((UINTN)TcgEfiSpecIdEventStruct + GetTcgEfiSpecIdEventStructSize (TcgEfiSpecIdEventStruct));
583 while ((UINTN)TcgPcrEvent2 <= EventLogLastEntry) {
584 DumpEvent2 (TcgPcrEvent2);
585 TcgPcrEvent2 = (TCG_PCR_EVENT2 *)((UINTN)TcgPcrEvent2 + GetPcrEvent2Size (TcgPcrEvent2));
586 }
587
588 if (FinalEventsTable == NULL) {
589 DEBUG ((EFI_D_INFO, "FinalEventsTable: NOT FOUND\n"));
590 } else {
591 DEBUG ((EFI_D_INFO, "FinalEventsTable: (0x%x)\n", FinalEventsTable));
592 DEBUG ((EFI_D_INFO, " Version: (0x%x)\n", FinalEventsTable->Version));
593 DEBUG ((EFI_D_INFO, " NumberOfEvents: (0x%x)\n", FinalEventsTable->NumberOfEvents));
594
595 TcgPcrEvent2 = (TCG_PCR_EVENT2 *)(UINTN)(FinalEventsTable + 1);
596 for (NumberOfEvents = 0; NumberOfEvents < FinalEventsTable->NumberOfEvents; NumberOfEvents++) {
597 DumpEvent2 (TcgPcrEvent2);
598 TcgPcrEvent2 = (TCG_PCR_EVENT2 *)((UINTN)TcgPcrEvent2 + GetPcrEvent2Size (TcgPcrEvent2));
599 }
600 }
601 break;
602 }
603
604 return ;
605 }
606
607 /**
608 The EFI_TCG2_PROTOCOL Get Event Log function call allows a caller to
609 retrieve the address of a given event log and its last entry.
610
611 @param[in] This Indicates the calling context
612 @param[in] EventLogFormat The type of the event log for which the information is requested.
613 @param[out] EventLogLocation A pointer to the memory address of the event log.
614 @param[out] EventLogLastEntry If the Event Log contains more than one entry, this is a pointer to the
615 address of the start of the last entry in the event log in memory.
616 @param[out] EventLogTruncated If the Event Log is missing at least one entry because an event would
617 have exceeded the area allocated for events, this value is set to TRUE.
618 Otherwise, the value will be FALSE and the Event Log will be complete.
619
620 @retval EFI_SUCCESS Operation completed successfully.
621 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect
622 (e.g. asking for an event log whose format is not supported).
623 **/
624 EFI_STATUS
625 EFIAPI
626 Tcg2GetEventLog (
627 IN EFI_TCG2_PROTOCOL *This,
628 IN EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat,
629 OUT EFI_PHYSICAL_ADDRESS *EventLogLocation,
630 OUT EFI_PHYSICAL_ADDRESS *EventLogLastEntry,
631 OUT BOOLEAN *EventLogTruncated
632 )
633 {
634 UINTN Index;
635
636 DEBUG ((EFI_D_INFO, "Tcg2GetEventLog ... (0x%x)\n", EventLogFormat));
637
638 if (This == NULL) {
639 return EFI_INVALID_PARAMETER;
640 }
641
642 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
643 if (EventLogFormat == mTcg2EventInfo[Index].LogFormat) {
644 break;
645 }
646 }
647
648 if (Index == sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0])) {
649 return EFI_INVALID_PARAMETER;
650 }
651
652 if ((mTcg2EventInfo[Index].LogFormat & mTcgDxeData.BsCap.SupportedEventLogs) == 0) {
653 return EFI_INVALID_PARAMETER;
654 }
655
656 if (!mTcgDxeData.BsCap.TPMPresentFlag) {
657 if (EventLogLocation != NULL) {
658 *EventLogLocation = 0;
659 }
660 if (EventLogLastEntry != NULL) {
661 *EventLogLastEntry = 0;
662 }
663 if (EventLogTruncated != NULL) {
664 *EventLogTruncated = FALSE;
665 }
666 return EFI_SUCCESS;
667 }
668
669 if (EventLogLocation != NULL) {
670 *EventLogLocation = mTcgDxeData.EventLogAreaStruct[Index].Lasa;
671 DEBUG ((EFI_D_INFO, "Tcg2GetEventLog (EventLogLocation - %x)\n", *EventLogLocation));
672 }
673
674 if (EventLogLastEntry != NULL) {
675 if (!mTcgDxeData.EventLogAreaStruct[Index].EventLogStarted) {
676 *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)0;
677 } else {
678 *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)mTcgDxeData.EventLogAreaStruct[Index].LastEvent;
679 }
680 DEBUG ((EFI_D_INFO, "Tcg2GetEventLog (EventLogLastEntry - %x)\n", *EventLogLastEntry));
681 }
682
683 if (EventLogTruncated != NULL) {
684 *EventLogTruncated = mTcgDxeData.EventLogAreaStruct[Index].EventLogTruncated;
685 DEBUG ((EFI_D_INFO, "Tcg2GetEventLog (EventLogTruncated - %x)\n", *EventLogTruncated));
686 }
687
688 DEBUG ((EFI_D_INFO, "Tcg2GetEventLog - %r\n", EFI_SUCCESS));
689
690 // Dump Event Log for debug purpose
691 if ((EventLogLocation != NULL) && (EventLogLastEntry != NULL)) {
692 DumpEventLog (EventLogFormat, *EventLogLocation, *EventLogLastEntry, mTcgDxeData.FinalEventsTable[Index]);
693 }
694
695 //
696 // All events generated after the invocation of EFI_TCG2_GET_EVENT_LOG SHALL be stored
697 // in an instance of an EFI_CONFIGURATION_TABLE named by the VendorGuid of EFI_TCG2_FINAL_EVENTS_TABLE_GUID.
698 //
699 mTcgDxeData.GetEventLogCalled[Index] = TRUE;
700
701 return EFI_SUCCESS;
702 }
703
704 /**
705 Add a new entry to the Event Log.
706
707 @param[in, out] EventLogPtr Pointer to the Event Log data.
708 @param[in, out] LogSize Size of the Event Log.
709 @param[in] MaxSize Maximum size of the Event Log.
710 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR/TCG_PCR_EVENT_EX data structure.
711 @param[in] NewEventHdrSize New event header size.
712 @param[in] NewEventData Pointer to the new event data.
713 @param[in] NewEventSize New event data size.
714
715 @retval EFI_SUCCESS The new event log entry was added.
716 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
717
718 **/
719 EFI_STATUS
720 TcgCommLogEvent (
721 IN OUT UINT8 **EventLogPtr,
722 IN OUT UINTN *LogSize,
723 IN UINTN MaxSize,
724 IN VOID *NewEventHdr,
725 IN UINT32 NewEventHdrSize,
726 IN UINT8 *NewEventData,
727 IN UINT32 NewEventSize
728 )
729 {
730 UINTN NewLogSize;
731
732 if (NewEventSize > MAX_ADDRESS - NewEventHdrSize) {
733 return EFI_OUT_OF_RESOURCES;
734 }
735
736 NewLogSize = NewEventHdrSize + NewEventSize;
737
738 if (NewLogSize > MAX_ADDRESS - *LogSize) {
739 return EFI_OUT_OF_RESOURCES;
740 }
741
742 if (NewLogSize + *LogSize > MaxSize) {
743 DEBUG ((EFI_D_INFO, " MaxSize - 0x%x\n", MaxSize));
744 DEBUG ((EFI_D_INFO, " NewLogSize - 0x%x\n", NewLogSize));
745 DEBUG ((EFI_D_INFO, " LogSize - 0x%x\n", *LogSize));
746 DEBUG ((EFI_D_INFO, "TcgCommLogEvent - %r\n", EFI_OUT_OF_RESOURCES));
747 return EFI_OUT_OF_RESOURCES;
748 }
749
750 *EventLogPtr += *LogSize;
751 *LogSize += NewLogSize;
752 CopyMem (*EventLogPtr, NewEventHdr, NewEventHdrSize);
753 CopyMem (
754 *EventLogPtr + NewEventHdrSize,
755 NewEventData,
756 NewEventSize
757 );
758 return EFI_SUCCESS;
759 }
760
761 /**
762 Add a new entry to the Event Log.
763
764 @param[in] EventLogFormat The type of the event log for which the information is requested.
765 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR/TCG_PCR_EVENT_EX data structure.
766 @param[in] NewEventHdrSize New event header size.
767 @param[in] NewEventData Pointer to the new event data.
768 @param[in] NewEventSize New event data size.
769
770 @retval EFI_SUCCESS The new event log entry was added.
771 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
772
773 **/
774 EFI_STATUS
775 TcgDxeLogEvent (
776 IN EFI_TCG2_EVENT_LOG_FORMAT EventLogFormat,
777 IN VOID *NewEventHdr,
778 IN UINT32 NewEventHdrSize,
779 IN UINT8 *NewEventData,
780 IN UINT32 NewEventSize
781 )
782 {
783 EFI_STATUS Status;
784 UINTN Index;
785 TCG_EVENT_LOG_AREA_STRUCT *EventLogAreaStruct;
786
787 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
788 if (EventLogFormat == mTcg2EventInfo[Index].LogFormat) {
789 break;
790 }
791 }
792
793 if (Index == sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0])) {
794 return EFI_INVALID_PARAMETER;
795 }
796
797 //
798 // Record to normal event log
799 //
800 EventLogAreaStruct = &mTcgDxeData.EventLogAreaStruct[Index];
801
802 if (EventLogAreaStruct->EventLogTruncated) {
803 return EFI_VOLUME_FULL;
804 }
805
806 EventLogAreaStruct->LastEvent = (UINT8*)(UINTN)EventLogAreaStruct->Lasa;
807 Status = TcgCommLogEvent (
808 &EventLogAreaStruct->LastEvent,
809 &EventLogAreaStruct->EventLogSize,
810 (UINTN)EventLogAreaStruct->Laml,
811 NewEventHdr,
812 NewEventHdrSize,
813 NewEventData,
814 NewEventSize
815 );
816
817 if (Status == EFI_OUT_OF_RESOURCES) {
818 EventLogAreaStruct->EventLogTruncated = TRUE;
819 return EFI_VOLUME_FULL;
820 } else if (Status == EFI_SUCCESS) {
821 EventLogAreaStruct->EventLogStarted = TRUE;
822 }
823
824 //
825 // If GetEventLog is called, record to FinalEventsTable, too.
826 //
827 if (mTcgDxeData.GetEventLogCalled[Index]) {
828 if (mTcgDxeData.FinalEventsTable[Index] == NULL) {
829 //
830 // no need for FinalEventsTable
831 //
832 return EFI_SUCCESS;
833 }
834 EventLogAreaStruct = &mTcgDxeData.FinalEventLogAreaStruct[Index];
835
836 if (EventLogAreaStruct->EventLogTruncated) {
837 return EFI_VOLUME_FULL;
838 }
839
840 EventLogAreaStruct->LastEvent = (UINT8*)(UINTN)EventLogAreaStruct->Lasa;
841 Status = TcgCommLogEvent (
842 &EventLogAreaStruct->LastEvent,
843 &EventLogAreaStruct->EventLogSize,
844 (UINTN)EventLogAreaStruct->Laml,
845 NewEventHdr,
846 NewEventHdrSize,
847 NewEventData,
848 NewEventSize
849 );
850 if (Status == EFI_OUT_OF_RESOURCES) {
851 EventLogAreaStruct->EventLogTruncated = TRUE;
852 return EFI_VOLUME_FULL;
853 } else if (Status == EFI_SUCCESS) {
854 EventLogAreaStruct->EventLogStarted = TRUE;
855 //
856 // Increase the NumberOfEvents in FinalEventsTable
857 //
858 (mTcgDxeData.FinalEventsTable[Index])->NumberOfEvents ++;
859 DEBUG ((EFI_D_INFO, "FinalEventsTable->NumberOfEvents - 0x%x\n", (mTcgDxeData.FinalEventsTable[Index])->NumberOfEvents));
860 DEBUG ((EFI_D_INFO, " Size - 0x%x\n", (UINTN)EventLogAreaStruct->EventLogSize));
861 }
862 }
863
864 return Status;
865 }
866
867 /**
868 Get TPML_DIGEST_VALUES compact binary buffer size.
869
870 @param[in] DigestListBin TPML_DIGEST_VALUES compact binary buffer.
871
872 @return TPML_DIGEST_VALUES compact binary buffer size.
873 **/
874 UINT32
875 GetDigestListBinSize (
876 IN VOID *DigestListBin
877 )
878 {
879 UINTN Index;
880 UINT16 DigestSize;
881 UINT32 TotalSize;
882 UINT32 Count;
883 TPMI_ALG_HASH HashAlg;
884
885 Count = ReadUnaligned32 (DigestListBin);
886 TotalSize = sizeof(Count);
887 DigestListBin = (UINT8 *)DigestListBin + sizeof(Count);
888 for (Index = 0; Index < Count; Index++) {
889 HashAlg = ReadUnaligned16 (DigestListBin);
890 TotalSize += sizeof(HashAlg);
891 DigestListBin = (UINT8 *)DigestListBin + sizeof(HashAlg);
892
893 DigestSize = GetHashSizeFromAlgo (HashAlg);
894 TotalSize += DigestSize;
895 DigestListBin = (UINT8 *)DigestListBin + DigestSize;
896 }
897
898 return TotalSize;
899 }
900
901 /**
902 Copy TPML_DIGEST_VALUES compact binary into a buffer
903
904 @param[in,out] Buffer Buffer to hold copied TPML_DIGEST_VALUES compact binary.
905 @param[in] DigestListBin TPML_DIGEST_VALUES compact binary buffer.
906 @param[in] HashAlgorithmMask HASH bits corresponding to the desired digests to copy.
907 @param[out] HashAlgorithmMaskCopied Pointer to HASH bits corresponding to the digests copied.
908
909 @return The end of buffer to hold TPML_DIGEST_VALUES compact binary.
910 **/
911 VOID *
912 CopyDigestListBinToBuffer (
913 IN OUT VOID *Buffer,
914 IN VOID *DigestListBin,
915 IN UINT32 HashAlgorithmMask,
916 OUT UINT32 *HashAlgorithmMaskCopied
917 )
918 {
919 UINTN Index;
920 UINT16 DigestSize;
921 UINT32 Count;
922 TPMI_ALG_HASH HashAlg;
923 UINT32 DigestListCount;
924 UINT32 *DigestListCountPtr;
925
926 DigestListCountPtr = (UINT32 *) Buffer;
927 DigestListCount = 0;
928 (*HashAlgorithmMaskCopied) = 0;
929
930 Count = ReadUnaligned32 (DigestListBin);
931 Buffer = (UINT8 *)Buffer + sizeof(Count);
932 DigestListBin = (UINT8 *)DigestListBin + sizeof(Count);
933 for (Index = 0; Index < Count; Index++) {
934 HashAlg = ReadUnaligned16 (DigestListBin);
935 DigestListBin = (UINT8 *)DigestListBin + sizeof(HashAlg);
936 DigestSize = GetHashSizeFromAlgo (HashAlg);
937
938 if (IsHashAlgSupportedInHashAlgorithmMask(HashAlg, HashAlgorithmMask)) {
939 CopyMem (Buffer, &HashAlg, sizeof(HashAlg));
940 Buffer = (UINT8 *)Buffer + sizeof(HashAlg);
941 CopyMem (Buffer, DigestListBin, DigestSize);
942 Buffer = (UINT8 *)Buffer + DigestSize;
943 DigestListCount++;
944 (*HashAlgorithmMaskCopied) |= GetHashMaskFromAlgo (HashAlg);
945 } else {
946 DEBUG ((DEBUG_ERROR, "WARNING: CopyDigestListBinToBuffer Event log has HashAlg unsupported by PCR bank (0x%x)\n", HashAlg));
947 }
948 DigestListBin = (UINT8 *)DigestListBin + DigestSize;
949 }
950 WriteUnaligned32 (DigestListCountPtr, DigestListCount);
951
952 return Buffer;
953 }
954
955 /**
956 Add a new entry to the Event Log.
957
958 @param[in] DigestList A list of digest.
959 @param[in,out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
960 @param[in] NewEventData Pointer to the new event data.
961
962 @retval EFI_SUCCESS The new event log entry was added.
963 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
964 **/
965 EFI_STATUS
966 TcgDxeLogHashEvent (
967 IN TPML_DIGEST_VALUES *DigestList,
968 IN OUT TCG_PCR_EVENT_HDR *NewEventHdr,
969 IN UINT8 *NewEventData
970 )
971 {
972 EFI_STATUS Status;
973 EFI_TPL OldTpl;
974 UINTN Index;
975 EFI_STATUS RetStatus;
976 TCG_PCR_EVENT2 TcgPcrEvent2;
977 UINT8 *DigestBuffer;
978 UINT32 *EventSizePtr;
979
980 DEBUG ((EFI_D_INFO, "SupportedEventLogs - 0x%08x\n", mTcgDxeData.BsCap.SupportedEventLogs));
981
982 RetStatus = EFI_SUCCESS;
983 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
984 if ((mTcgDxeData.BsCap.SupportedEventLogs & mTcg2EventInfo[Index].LogFormat) != 0) {
985 DEBUG ((EFI_D_INFO, " LogFormat - 0x%08x\n", mTcg2EventInfo[Index].LogFormat));
986 switch (mTcg2EventInfo[Index].LogFormat) {
987 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2:
988 Status = GetDigestFromDigestList (TPM_ALG_SHA1, DigestList, &NewEventHdr->Digest);
989 if (!EFI_ERROR (Status)) {
990 //
991 // Enter critical region
992 //
993 OldTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
994 Status = TcgDxeLogEvent (
995 mTcg2EventInfo[Index].LogFormat,
996 NewEventHdr,
997 sizeof(TCG_PCR_EVENT_HDR),
998 NewEventData,
999 NewEventHdr->EventSize
1000 );
1001 if (Status != EFI_SUCCESS) {
1002 RetStatus = Status;
1003 }
1004 gBS->RestoreTPL (OldTpl);
1005 //
1006 // Exit critical region
1007 //
1008 }
1009 break;
1010 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_2:
1011 ZeroMem (&TcgPcrEvent2, sizeof(TcgPcrEvent2));
1012 TcgPcrEvent2.PCRIndex = NewEventHdr->PCRIndex;
1013 TcgPcrEvent2.EventType = NewEventHdr->EventType;
1014 DigestBuffer = (UINT8 *)&TcgPcrEvent2.Digest;
1015 EventSizePtr = CopyDigestListToBuffer (DigestBuffer, DigestList, mTcgDxeData.BsCap.ActivePcrBanks);
1016 CopyMem (EventSizePtr, &NewEventHdr->EventSize, sizeof(NewEventHdr->EventSize));
1017
1018 //
1019 // Enter critical region
1020 //
1021 OldTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
1022 Status = TcgDxeLogEvent (
1023 mTcg2EventInfo[Index].LogFormat,
1024 &TcgPcrEvent2,
1025 sizeof(TcgPcrEvent2.PCRIndex) + sizeof(TcgPcrEvent2.EventType) + GetDigestListBinSize (DigestBuffer) + sizeof(TcgPcrEvent2.EventSize),
1026 NewEventData,
1027 NewEventHdr->EventSize
1028 );
1029 if (Status != EFI_SUCCESS) {
1030 RetStatus = Status;
1031 }
1032 gBS->RestoreTPL (OldTpl);
1033 //
1034 // Exit critical region
1035 //
1036 break;
1037 }
1038 }
1039 }
1040
1041 return RetStatus;
1042 }
1043
1044 /**
1045 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
1046 and add an entry to the Event Log.
1047
1048 @param[in] Flags Bitmap providing additional information.
1049 @param[in] HashData Physical address of the start of the data buffer
1050 to be hashed, extended, and logged.
1051 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
1052 @param[in, out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
1053 @param[in] NewEventData Pointer to the new event data.
1054
1055 @retval EFI_SUCCESS Operation completed successfully.
1056 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
1057 @retval EFI_DEVICE_ERROR The command was unsuccessful.
1058
1059 **/
1060 EFI_STATUS
1061 TcgDxeHashLogExtendEvent (
1062 IN UINT64 Flags,
1063 IN UINT8 *HashData,
1064 IN UINT64 HashDataLen,
1065 IN OUT TCG_PCR_EVENT_HDR *NewEventHdr,
1066 IN UINT8 *NewEventData
1067 )
1068 {
1069 EFI_STATUS Status;
1070 TPML_DIGEST_VALUES DigestList;
1071
1072 if (!mTcgDxeData.BsCap.TPMPresentFlag) {
1073 return EFI_DEVICE_ERROR;
1074 }
1075
1076 Status = HashAndExtend (
1077 NewEventHdr->PCRIndex,
1078 HashData,
1079 (UINTN)HashDataLen,
1080 &DigestList
1081 );
1082 if (!EFI_ERROR (Status)) {
1083 if ((Flags & EFI_TCG2_EXTEND_ONLY) == 0) {
1084 Status = TcgDxeLogHashEvent (&DigestList, NewEventHdr, NewEventData);
1085 }
1086 }
1087
1088 if (Status == EFI_DEVICE_ERROR) {
1089 DEBUG ((EFI_D_ERROR, "TcgDxeHashLogExtendEvent - %r. Disable TPM.\n", Status));
1090 mTcgDxeData.BsCap.TPMPresentFlag = FALSE;
1091 REPORT_STATUS_CODE (
1092 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1093 (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)
1094 );
1095 }
1096
1097 return Status;
1098 }
1099
1100 /**
1101 The EFI_TCG2_PROTOCOL HashLogExtendEvent function call provides callers with
1102 an opportunity to extend and optionally log events without requiring
1103 knowledge of actual TPM commands.
1104 The extend operation will occur even if this function cannot create an event
1105 log entry (e.g. due to the event log being full).
1106
1107 @param[in] This Indicates the calling context
1108 @param[in] Flags Bitmap providing additional information.
1109 @param[in] DataToHash Physical address of the start of the data buffer to be hashed.
1110 @param[in] DataToHashLen The length in bytes of the buffer referenced by DataToHash.
1111 @param[in] Event Pointer to data buffer containing information about the event.
1112
1113 @retval EFI_SUCCESS Operation completed successfully.
1114 @retval EFI_DEVICE_ERROR The command was unsuccessful.
1115 @retval EFI_VOLUME_FULL The extend operation occurred, but the event could not be written to one or more event logs.
1116 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1117 @retval EFI_UNSUPPORTED The PE/COFF image type is not supported.
1118 **/
1119 EFI_STATUS
1120 EFIAPI
1121 Tcg2HashLogExtendEvent (
1122 IN EFI_TCG2_PROTOCOL *This,
1123 IN UINT64 Flags,
1124 IN EFI_PHYSICAL_ADDRESS DataToHash,
1125 IN UINT64 DataToHashLen,
1126 IN EFI_TCG2_EVENT *Event
1127 )
1128 {
1129 EFI_STATUS Status;
1130 TCG_PCR_EVENT_HDR NewEventHdr;
1131 TPML_DIGEST_VALUES DigestList;
1132
1133 DEBUG ((DEBUG_VERBOSE, "Tcg2HashLogExtendEvent ...\n"));
1134
1135 if ((This == NULL) || (DataToHash == 0) || (Event == NULL)) {
1136 return EFI_INVALID_PARAMETER;
1137 }
1138
1139 if (!mTcgDxeData.BsCap.TPMPresentFlag) {
1140 return EFI_DEVICE_ERROR;
1141 }
1142
1143 if (Event->Size < Event->Header.HeaderSize + sizeof(UINT32)) {
1144 return EFI_INVALID_PARAMETER;
1145 }
1146
1147 if (Event->Header.PCRIndex > MAX_PCR_INDEX) {
1148 return EFI_INVALID_PARAMETER;
1149 }
1150
1151 NewEventHdr.PCRIndex = Event->Header.PCRIndex;
1152 NewEventHdr.EventType = Event->Header.EventType;
1153 NewEventHdr.EventSize = Event->Size - sizeof(UINT32) - Event->Header.HeaderSize;
1154 if ((Flags & PE_COFF_IMAGE) != 0) {
1155 Status = MeasurePeImageAndExtend (
1156 NewEventHdr.PCRIndex,
1157 DataToHash,
1158 (UINTN)DataToHashLen,
1159 &DigestList
1160 );
1161 if (!EFI_ERROR (Status)) {
1162 if ((Flags & EFI_TCG2_EXTEND_ONLY) == 0) {
1163 Status = TcgDxeLogHashEvent (&DigestList, &NewEventHdr, Event->Event);
1164 }
1165 }
1166 if (Status == EFI_DEVICE_ERROR) {
1167 DEBUG ((EFI_D_ERROR, "MeasurePeImageAndExtend - %r. Disable TPM.\n", Status));
1168 mTcgDxeData.BsCap.TPMPresentFlag = FALSE;
1169 REPORT_STATUS_CODE (
1170 EFI_ERROR_CODE | EFI_ERROR_MINOR,
1171 (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)
1172 );
1173 }
1174 } else {
1175 Status = TcgDxeHashLogExtendEvent (
1176 Flags,
1177 (UINT8 *) (UINTN) DataToHash,
1178 DataToHashLen,
1179 &NewEventHdr,
1180 Event->Event
1181 );
1182 }
1183 DEBUG ((DEBUG_VERBOSE, "Tcg2HashLogExtendEvent - %r\n", Status));
1184 return Status;
1185 }
1186
1187 /**
1188 This service enables the sending of commands to the TPM.
1189
1190 @param[in] This Indicates the calling context
1191 @param[in] InputParameterBlockSize Size of the TPM input parameter block.
1192 @param[in] InputParameterBlock Pointer to the TPM input parameter block.
1193 @param[in] OutputParameterBlockSize Size of the TPM output parameter block.
1194 @param[in] OutputParameterBlock Pointer to the TPM output parameter block.
1195
1196 @retval EFI_SUCCESS The command byte stream was successfully sent to the device and a response was successfully received.
1197 @retval EFI_DEVICE_ERROR The command was not successfully sent to the device or a response was not successfully received from the device.
1198 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1199 @retval EFI_BUFFER_TOO_SMALL The output parameter block is too small.
1200 **/
1201 EFI_STATUS
1202 EFIAPI
1203 Tcg2SubmitCommand (
1204 IN EFI_TCG2_PROTOCOL *This,
1205 IN UINT32 InputParameterBlockSize,
1206 IN UINT8 *InputParameterBlock,
1207 IN UINT32 OutputParameterBlockSize,
1208 IN UINT8 *OutputParameterBlock
1209 )
1210 {
1211 EFI_STATUS Status;
1212
1213 DEBUG ((EFI_D_INFO, "Tcg2SubmitCommand ...\n"));
1214
1215 if ((This == NULL) ||
1216 (InputParameterBlockSize == 0) || (InputParameterBlock == NULL) ||
1217 (OutputParameterBlockSize == 0) || (OutputParameterBlock == NULL)) {
1218 return EFI_INVALID_PARAMETER;
1219 }
1220
1221 if (!mTcgDxeData.BsCap.TPMPresentFlag) {
1222 return EFI_DEVICE_ERROR;
1223 }
1224
1225 if (InputParameterBlockSize > mTcgDxeData.BsCap.MaxCommandSize) {
1226 return EFI_INVALID_PARAMETER;
1227 }
1228 if (OutputParameterBlockSize > mTcgDxeData.BsCap.MaxResponseSize) {
1229 return EFI_INVALID_PARAMETER;
1230 }
1231
1232 Status = Tpm2SubmitCommand (
1233 InputParameterBlockSize,
1234 InputParameterBlock,
1235 &OutputParameterBlockSize,
1236 OutputParameterBlock
1237 );
1238 DEBUG ((EFI_D_INFO, "Tcg2SubmitCommand - %r\n", Status));
1239 return Status;
1240 }
1241
1242 /**
1243 This service returns the currently active PCR banks.
1244
1245 @param[in] This Indicates the calling context
1246 @param[out] ActivePcrBanks Pointer to the variable receiving the bitmap of currently active PCR banks.
1247
1248 @retval EFI_SUCCESS The bitmap of active PCR banks was stored in the ActivePcrBanks parameter.
1249 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1250 **/
1251 EFI_STATUS
1252 EFIAPI
1253 Tcg2GetActivePCRBanks (
1254 IN EFI_TCG2_PROTOCOL *This,
1255 OUT UINT32 *ActivePcrBanks
1256 )
1257 {
1258 if (ActivePcrBanks == NULL) {
1259 return EFI_INVALID_PARAMETER;
1260 }
1261 *ActivePcrBanks = mTcgDxeData.BsCap.ActivePcrBanks;
1262 return EFI_SUCCESS;
1263 }
1264
1265 /**
1266 This service sets the currently active PCR banks.
1267
1268 @param[in] This Indicates the calling context
1269 @param[in] ActivePcrBanks Bitmap of the requested active PCR banks. At least one bit SHALL be set.
1270
1271 @retval EFI_SUCCESS The bitmap in ActivePcrBank parameter is already active.
1272 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1273 **/
1274 EFI_STATUS
1275 EFIAPI
1276 Tcg2SetActivePCRBanks (
1277 IN EFI_TCG2_PROTOCOL *This,
1278 IN UINT32 ActivePcrBanks
1279 )
1280 {
1281 EFI_STATUS Status;
1282 UINT32 ReturnCode;
1283
1284 DEBUG ((EFI_D_INFO, "Tcg2SetActivePCRBanks ... (0x%x)\n", ActivePcrBanks));
1285
1286 if (ActivePcrBanks == 0) {
1287 return EFI_INVALID_PARAMETER;
1288 }
1289 if ((ActivePcrBanks & (~mTcgDxeData.BsCap.HashAlgorithmBitmap)) != 0) {
1290 return EFI_INVALID_PARAMETER;
1291 }
1292 if (ActivePcrBanks == mTcgDxeData.BsCap.ActivePcrBanks) {
1293 //
1294 // Need clear previous SET_PCR_BANKS setting
1295 //
1296 ReturnCode = Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunction (TCG2_PHYSICAL_PRESENCE_NO_ACTION, 0);
1297 } else {
1298 ReturnCode = Tcg2PhysicalPresenceLibSubmitRequestToPreOSFunction (TCG2_PHYSICAL_PRESENCE_SET_PCR_BANKS, ActivePcrBanks);
1299 }
1300
1301 if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_SUCCESS) {
1302 Status = EFI_SUCCESS;
1303 } else if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_GENERAL_FAILURE) {
1304 Status = EFI_OUT_OF_RESOURCES;
1305 } else if (ReturnCode == TCG_PP_SUBMIT_REQUEST_TO_PREOS_NOT_IMPLEMENTED) {
1306 Status = EFI_UNSUPPORTED;
1307 } else {
1308 Status = EFI_DEVICE_ERROR;
1309 }
1310
1311 DEBUG ((EFI_D_INFO, "Tcg2SetActivePCRBanks - %r\n", Status));
1312
1313 return Status;
1314 }
1315
1316 /**
1317 This service retrieves the result of a previous invocation of SetActivePcrBanks.
1318
1319 @param[in] This Indicates the calling context
1320 @param[out] OperationPresent Non-zero value to indicate a SetActivePcrBank operation was invoked during the last boot.
1321 @param[out] Response The response from the SetActivePcrBank request.
1322
1323 @retval EFI_SUCCESS The result value could be returned.
1324 @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
1325 **/
1326 EFI_STATUS
1327 EFIAPI
1328 Tcg2GetResultOfSetActivePcrBanks (
1329 IN EFI_TCG2_PROTOCOL *This,
1330 OUT UINT32 *OperationPresent,
1331 OUT UINT32 *Response
1332 )
1333 {
1334 UINT32 ReturnCode;
1335
1336 if ((OperationPresent == NULL) || (Response == NULL)) {
1337 return EFI_INVALID_PARAMETER;
1338 }
1339
1340 ReturnCode = Tcg2PhysicalPresenceLibReturnOperationResponseToOsFunction (OperationPresent, Response);
1341 if (ReturnCode == TCG_PP_RETURN_TPM_OPERATION_RESPONSE_SUCCESS) {
1342 return EFI_SUCCESS;
1343 } else {
1344 return EFI_UNSUPPORTED;
1345 }
1346 }
1347
1348 EFI_TCG2_PROTOCOL mTcg2Protocol = {
1349 Tcg2GetCapability,
1350 Tcg2GetEventLog,
1351 Tcg2HashLogExtendEvent,
1352 Tcg2SubmitCommand,
1353 Tcg2GetActivePCRBanks,
1354 Tcg2SetActivePCRBanks,
1355 Tcg2GetResultOfSetActivePcrBanks,
1356 };
1357
1358 /**
1359 Initialize the Event Log and log events passed from the PEI phase.
1360
1361 @retval EFI_SUCCESS Operation completed successfully.
1362 @retval EFI_OUT_OF_RESOURCES Out of memory.
1363
1364 **/
1365 EFI_STATUS
1366 SetupEventLog (
1367 VOID
1368 )
1369 {
1370 EFI_STATUS Status;
1371 VOID *TcgEvent;
1372 EFI_PEI_HOB_POINTERS GuidHob;
1373 EFI_PHYSICAL_ADDRESS Lasa;
1374 UINTN Index;
1375 VOID *DigestListBin;
1376 TPML_DIGEST_VALUES TempDigestListBin;
1377 UINT32 DigestListBinSize;
1378 UINT8 *Event;
1379 UINT32 EventSize;
1380 UINT32 *EventSizePtr;
1381 UINT32 HashAlgorithmMaskCopied;
1382 TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct;
1383 UINT8 TempBuf[sizeof(TCG_EfiSpecIDEventStruct) + sizeof(UINT32) + (HASH_COUNT * sizeof(TCG_EfiSpecIdEventAlgorithmSize)) + sizeof(UINT8)];
1384 TCG_PCR_EVENT_HDR NoActionEvent;
1385 TCG_EfiSpecIdEventAlgorithmSize *DigestSize;
1386 TCG_EfiSpecIdEventAlgorithmSize *TempDigestSize;
1387 UINT8 *VendorInfoSize;
1388 UINT32 NumberOfAlgorithms;
1389 TCG_EfiStartupLocalityEvent StartupLocalityEvent;
1390
1391 DEBUG ((EFI_D_INFO, "SetupEventLog\n"));
1392
1393 //
1394 // 1. Create Log Area
1395 //
1396 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
1397 if ((mTcgDxeData.BsCap.SupportedEventLogs & mTcg2EventInfo[Index].LogFormat) != 0) {
1398 mTcgDxeData.EventLogAreaStruct[Index].EventLogFormat = mTcg2EventInfo[Index].LogFormat;
1399 Status = gBS->AllocatePages (
1400 AllocateAnyPages,
1401 EfiBootServicesData,
1402 EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcgLogAreaMinLen)),
1403 &Lasa
1404 );
1405 if (EFI_ERROR (Status)) {
1406 return Status;
1407 }
1408 mTcgDxeData.EventLogAreaStruct[Index].Lasa = Lasa;
1409 mTcgDxeData.EventLogAreaStruct[Index].Laml = PcdGet32 (PcdTcgLogAreaMinLen);
1410 //
1411 // To initialize them as 0xFF is recommended
1412 // because the OS can know the last entry for that.
1413 //
1414 SetMem ((VOID *)(UINTN)Lasa, PcdGet32 (PcdTcgLogAreaMinLen), 0xFF);
1415 //
1416 // Create first entry for Log Header Entry Data
1417 //
1418 if (mTcg2EventInfo[Index].LogFormat != EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2) {
1419 //
1420 // TcgEfiSpecIdEventStruct
1421 //
1422 TcgEfiSpecIdEventStruct = (TCG_EfiSpecIDEventStruct *)TempBuf;
1423 CopyMem (TcgEfiSpecIdEventStruct->signature, TCG_EfiSpecIDEventStruct_SIGNATURE_03, sizeof(TcgEfiSpecIdEventStruct->signature));
1424 TcgEfiSpecIdEventStruct->platformClass = PcdGet8 (PcdTpmPlatformClass);
1425 TcgEfiSpecIdEventStruct->specVersionMajor = TCG_EfiSpecIDEventStruct_SPEC_VERSION_MAJOR_TPM2;
1426 TcgEfiSpecIdEventStruct->specVersionMinor = TCG_EfiSpecIDEventStruct_SPEC_VERSION_MINOR_TPM2;
1427 TcgEfiSpecIdEventStruct->specErrata = TCG_EfiSpecIDEventStruct_SPEC_ERRATA_TPM2;
1428 TcgEfiSpecIdEventStruct->uintnSize = sizeof(UINTN)/sizeof(UINT32);
1429 NumberOfAlgorithms = 0;
1430 DigestSize = (TCG_EfiSpecIdEventAlgorithmSize *)((UINT8 *)TcgEfiSpecIdEventStruct + sizeof(*TcgEfiSpecIdEventStruct) + sizeof(NumberOfAlgorithms));
1431 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA1) != 0) {
1432 TempDigestSize = DigestSize;
1433 TempDigestSize += NumberOfAlgorithms;
1434 TempDigestSize->algorithmId = TPM_ALG_SHA1;
1435 TempDigestSize->digestSize = SHA1_DIGEST_SIZE;
1436 NumberOfAlgorithms++;
1437 }
1438 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA256) != 0) {
1439 TempDigestSize = DigestSize;
1440 TempDigestSize += NumberOfAlgorithms;
1441 TempDigestSize->algorithmId = TPM_ALG_SHA256;
1442 TempDigestSize->digestSize = SHA256_DIGEST_SIZE;
1443 NumberOfAlgorithms++;
1444 }
1445 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA384) != 0) {
1446 TempDigestSize = DigestSize;
1447 TempDigestSize += NumberOfAlgorithms;
1448 TempDigestSize->algorithmId = TPM_ALG_SHA384;
1449 TempDigestSize->digestSize = SHA384_DIGEST_SIZE;
1450 NumberOfAlgorithms++;
1451 }
1452 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA512) != 0) {
1453 TempDigestSize = DigestSize;
1454 TempDigestSize += NumberOfAlgorithms;
1455 TempDigestSize->algorithmId = TPM_ALG_SHA512;
1456 TempDigestSize->digestSize = SHA512_DIGEST_SIZE;
1457 NumberOfAlgorithms++;
1458 }
1459 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SM3_256) != 0) {
1460 TempDigestSize = DigestSize;
1461 TempDigestSize += NumberOfAlgorithms;
1462 TempDigestSize->algorithmId = TPM_ALG_SM3_256;
1463 TempDigestSize->digestSize = SM3_256_DIGEST_SIZE;
1464 NumberOfAlgorithms++;
1465 }
1466 CopyMem (TcgEfiSpecIdEventStruct + 1, &NumberOfAlgorithms, sizeof(NumberOfAlgorithms));
1467 TempDigestSize = DigestSize;
1468 TempDigestSize += NumberOfAlgorithms;
1469 VendorInfoSize = (UINT8 *)TempDigestSize;
1470 *VendorInfoSize = 0;
1471
1472 NoActionEvent.PCRIndex = 0;
1473 NoActionEvent.EventType = EV_NO_ACTION;
1474 ZeroMem (&NoActionEvent.Digest, sizeof(NoActionEvent.Digest));
1475 NoActionEvent.EventSize = (UINT32)GetTcgEfiSpecIdEventStructSize (TcgEfiSpecIdEventStruct);
1476
1477 //
1478 // Log TcgEfiSpecIdEventStruct as the first Event
1479 // TCG PC Client PFP spec. Section 9.2 Measurement Event Entries and Log
1480 //
1481 Status = TcgDxeLogEvent (
1482 mTcg2EventInfo[Index].LogFormat,
1483 &NoActionEvent,
1484 sizeof(NoActionEvent),
1485 (UINT8 *)TcgEfiSpecIdEventStruct,
1486 NoActionEvent.EventSize
1487 );
1488
1489 //
1490 // EfiStartupLocalityEvent
1491 //
1492 GuidHob.Guid = GetFirstGuidHob (&gTpm2StartupLocalityHobGuid);
1493 if (GuidHob.Guid != NULL) {
1494 //
1495 // Get Locality Indicator from StartupLocality HOB
1496 //
1497 StartupLocalityEvent.StartupLocality = *(UINT8 *)(GET_GUID_HOB_DATA (GuidHob.Guid));
1498 CopyMem (StartupLocalityEvent.Signature, TCG_EfiStartupLocalityEvent_SIGNATURE, sizeof(StartupLocalityEvent.Signature));
1499
1500 NoActionEvent.PCRIndex = 0;
1501 NoActionEvent.EventType = EV_NO_ACTION;
1502 ZeroMem (&NoActionEvent.Digest, sizeof(NoActionEvent.Digest));
1503 NoActionEvent.EventSize = sizeof(StartupLocalityEvent);
1504
1505 DEBUG ((DEBUG_INFO, "SetupEventLog: Set Locality from HOB into StartupLocalityEvent 0x%02x\n", StartupLocalityEvent.StartupLocality));
1506
1507 //
1508 // Log EfiStartupLocalityEvent as the second Event
1509 // TCG PC Client PFP spec. Section 9.3.4.3 Startup Locality Event
1510 //
1511 Status = TcgDxeLogEvent (
1512 mTcg2EventInfo[Index].LogFormat,
1513 &NoActionEvent,
1514 sizeof(NoActionEvent),
1515 (UINT8 *)&StartupLocalityEvent,
1516 NoActionEvent.EventSize
1517 );
1518 }
1519 }
1520 }
1521 }
1522
1523 //
1524 // 2. Create Final Log Area
1525 //
1526 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
1527 if ((mTcgDxeData.BsCap.SupportedEventLogs & mTcg2EventInfo[Index].LogFormat) != 0) {
1528 if (mTcg2EventInfo[Index].LogFormat == EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
1529 Status = gBS->AllocatePages (
1530 AllocateAnyPages,
1531 EfiACPIMemoryNVS,
1532 EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcg2FinalLogAreaLen)),
1533 &Lasa
1534 );
1535 if (EFI_ERROR (Status)) {
1536 return Status;
1537 }
1538 SetMem ((VOID *)(UINTN)Lasa, PcdGet32 (PcdTcg2FinalLogAreaLen), 0xFF);
1539
1540 //
1541 // Initialize
1542 //
1543 mTcgDxeData.FinalEventsTable[Index] = (VOID *)(UINTN)Lasa;
1544 (mTcgDxeData.FinalEventsTable[Index])->Version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
1545 (mTcgDxeData.FinalEventsTable[Index])->NumberOfEvents = 0;
1546
1547 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogFormat = mTcg2EventInfo[Index].LogFormat;
1548 mTcgDxeData.FinalEventLogAreaStruct[Index].Lasa = Lasa + sizeof(EFI_TCG2_FINAL_EVENTS_TABLE);
1549 mTcgDxeData.FinalEventLogAreaStruct[Index].Laml = PcdGet32 (PcdTcg2FinalLogAreaLen) - sizeof(EFI_TCG2_FINAL_EVENTS_TABLE);
1550 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogSize = 0;
1551 mTcgDxeData.FinalEventLogAreaStruct[Index].LastEvent = (VOID *)(UINTN)mTcgDxeData.FinalEventLogAreaStruct[Index].Lasa;
1552 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogStarted = FALSE;
1553 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogTruncated = FALSE;
1554
1555 //
1556 // Install to configuration table for EFI_TCG2_EVENT_LOG_FORMAT_TCG_2
1557 //
1558 Status = gBS->InstallConfigurationTable (&gEfiTcg2FinalEventsTableGuid, (VOID *)mTcgDxeData.FinalEventsTable[Index]);
1559 if (EFI_ERROR (Status)) {
1560 return Status;
1561 }
1562 } else {
1563 //
1564 // No need to handle EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2
1565 //
1566 mTcgDxeData.FinalEventsTable[Index] = NULL;
1567 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogFormat = mTcg2EventInfo[Index].LogFormat;
1568 mTcgDxeData.FinalEventLogAreaStruct[Index].Lasa = 0;
1569 mTcgDxeData.FinalEventLogAreaStruct[Index].Laml = 0;
1570 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogSize = 0;
1571 mTcgDxeData.FinalEventLogAreaStruct[Index].LastEvent = 0;
1572 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogStarted = FALSE;
1573 mTcgDxeData.FinalEventLogAreaStruct[Index].EventLogTruncated = FALSE;
1574 }
1575 }
1576 }
1577
1578 //
1579 // 3. Sync data from PEI to DXE
1580 //
1581 Status = EFI_SUCCESS;
1582 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {
1583 if ((mTcgDxeData.BsCap.SupportedEventLogs & mTcg2EventInfo[Index].LogFormat) != 0) {
1584 GuidHob.Raw = GetHobList ();
1585 Status = EFI_SUCCESS;
1586 while (!EFI_ERROR (Status) &&
1587 (GuidHob.Raw = GetNextGuidHob (mTcg2EventInfo[Index].EventGuid, GuidHob.Raw)) != NULL) {
1588 TcgEvent = AllocateCopyPool (GET_GUID_HOB_DATA_SIZE (GuidHob.Guid), GET_GUID_HOB_DATA (GuidHob.Guid));
1589 ASSERT (TcgEvent != NULL);
1590 GuidHob.Raw = GET_NEXT_HOB (GuidHob);
1591 switch (mTcg2EventInfo[Index].LogFormat) {
1592 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2:
1593 Status = TcgDxeLogEvent (
1594 mTcg2EventInfo[Index].LogFormat,
1595 TcgEvent,
1596 sizeof(TCG_PCR_EVENT_HDR),
1597 ((TCG_PCR_EVENT*)TcgEvent)->Event,
1598 ((TCG_PCR_EVENT_HDR*)TcgEvent)->EventSize
1599 );
1600 break;
1601 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_2:
1602 DigestListBin = (UINT8 *)TcgEvent + sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE);
1603 DigestListBinSize = GetDigestListBinSize (DigestListBin);
1604 //
1605 // Save event size.
1606 //
1607 CopyMem (&EventSize, (UINT8 *)DigestListBin + DigestListBinSize, sizeof(UINT32));
1608 Event = (UINT8 *)DigestListBin + DigestListBinSize + sizeof(UINT32);
1609 //
1610 // Filter inactive digest in the event2 log from PEI HOB.
1611 //
1612 CopyMem (&TempDigestListBin, DigestListBin, GetDigestListBinSize (DigestListBin));
1613 EventSizePtr = CopyDigestListBinToBuffer (
1614 DigestListBin,
1615 &TempDigestListBin,
1616 mTcgDxeData.BsCap.ActivePcrBanks,
1617 &HashAlgorithmMaskCopied
1618 );
1619 if (HashAlgorithmMaskCopied != mTcgDxeData.BsCap.ActivePcrBanks) {
1620 DEBUG ((
1621 DEBUG_ERROR,
1622 "ERROR: The event2 log includes digest hash mask 0x%x, but required digest hash mask is 0x%x\n",
1623 HashAlgorithmMaskCopied,
1624 mTcgDxeData.BsCap.ActivePcrBanks
1625 ));
1626 }
1627 //
1628 // Restore event size.
1629 //
1630 CopyMem (EventSizePtr, &EventSize, sizeof(UINT32));
1631 DigestListBinSize = GetDigestListBinSize (DigestListBin);
1632
1633 Status = TcgDxeLogEvent (
1634 mTcg2EventInfo[Index].LogFormat,
1635 TcgEvent,
1636 sizeof(TCG_PCRINDEX) + sizeof(TCG_EVENTTYPE) + DigestListBinSize + sizeof(UINT32),
1637 Event,
1638 EventSize
1639 );
1640 break;
1641 }
1642 FreePool (TcgEvent);
1643 }
1644 }
1645 }
1646
1647 return Status;
1648 }
1649
1650 /**
1651 Measure and log an action string, and extend the measurement result into PCR[5].
1652
1653 @param[in] String A specific string that indicates an Action event.
1654
1655 @retval EFI_SUCCESS Operation completed successfully.
1656 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1657
1658 **/
1659 EFI_STATUS
1660 TcgMeasureAction (
1661 IN CHAR8 *String
1662 )
1663 {
1664 TCG_PCR_EVENT_HDR TcgEvent;
1665
1666 TcgEvent.PCRIndex = 5;
1667 TcgEvent.EventType = EV_EFI_ACTION;
1668 TcgEvent.EventSize = (UINT32)AsciiStrLen (String);
1669 return TcgDxeHashLogExtendEvent (
1670 0,
1671 (UINT8*)String,
1672 TcgEvent.EventSize,
1673 &TcgEvent,
1674 (UINT8 *) String
1675 );
1676 }
1677
1678 /**
1679 Measure and log EFI handoff tables, and extend the measurement result into PCR[1].
1680
1681 @retval EFI_SUCCESS Operation completed successfully.
1682 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1683
1684 **/
1685 EFI_STATUS
1686 MeasureHandoffTables (
1687 VOID
1688 )
1689 {
1690 EFI_STATUS Status;
1691 TCG_PCR_EVENT_HDR TcgEvent;
1692 EFI_HANDOFF_TABLE_POINTERS HandoffTables;
1693 UINTN ProcessorNum;
1694 EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;
1695
1696 ProcessorLocBuf = NULL;
1697 Status = EFI_SUCCESS;
1698
1699 if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_SERVER) {
1700 //
1701 // Tcg Server spec.
1702 // Measure each processor EFI_CPU_PHYSICAL_LOCATION with EV_TABLE_OF_DEVICES to PCR[1]
1703 //
1704 Status = GetProcessorsCpuLocation(&ProcessorLocBuf, &ProcessorNum);
1705
1706 if (!EFI_ERROR(Status)){
1707 TcgEvent.PCRIndex = 1;
1708 TcgEvent.EventType = EV_TABLE_OF_DEVICES;
1709 TcgEvent.EventSize = sizeof (HandoffTables);
1710
1711 HandoffTables.NumberOfTables = 1;
1712 HandoffTables.TableEntry[0].VendorGuid = gEfiMpServiceProtocolGuid;
1713 HandoffTables.TableEntry[0].VendorTable = ProcessorLocBuf;
1714
1715 Status = TcgDxeHashLogExtendEvent (
1716 0,
1717 (UINT8*)(UINTN)ProcessorLocBuf,
1718 sizeof(EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,
1719 &TcgEvent,
1720 (UINT8*)&HandoffTables
1721 );
1722
1723 FreePool(ProcessorLocBuf);
1724 }
1725 }
1726
1727 return Status;
1728 }
1729
1730 /**
1731 Measure and log Separator event, and extend the measurement result into a specific PCR.
1732
1733 @param[in] PCRIndex PCR index.
1734
1735 @retval EFI_SUCCESS Operation completed successfully.
1736 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1737
1738 **/
1739 EFI_STATUS
1740 MeasureSeparatorEvent (
1741 IN TPM_PCRINDEX PCRIndex
1742 )
1743 {
1744 TCG_PCR_EVENT_HDR TcgEvent;
1745 UINT32 EventData;
1746
1747 DEBUG ((EFI_D_INFO, "MeasureSeparatorEvent Pcr - %x\n", PCRIndex));
1748
1749 EventData = 0;
1750 TcgEvent.PCRIndex = PCRIndex;
1751 TcgEvent.EventType = EV_SEPARATOR;
1752 TcgEvent.EventSize = (UINT32)sizeof (EventData);
1753 return TcgDxeHashLogExtendEvent (
1754 0,
1755 (UINT8 *)&EventData,
1756 sizeof (EventData),
1757 &TcgEvent,
1758 (UINT8 *)&EventData
1759 );
1760 }
1761
1762 /**
1763 Measure and log an EFI variable, and extend the measurement result into a specific PCR.
1764
1765 @param[in] PCRIndex PCR Index.
1766 @param[in] EventType Event type.
1767 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1768 @param[in] VendorGuid A unique identifier for the vendor.
1769 @param[in] VarData The content of the variable data.
1770 @param[in] VarSize The size of the variable data.
1771
1772 @retval EFI_SUCCESS Operation completed successfully.
1773 @retval EFI_OUT_OF_RESOURCES Out of memory.
1774 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1775
1776 **/
1777 EFI_STATUS
1778 MeasureVariable (
1779 IN TPM_PCRINDEX PCRIndex,
1780 IN TCG_EVENTTYPE EventType,
1781 IN CHAR16 *VarName,
1782 IN EFI_GUID *VendorGuid,
1783 IN VOID *VarData,
1784 IN UINTN VarSize
1785 )
1786 {
1787 EFI_STATUS Status;
1788 TCG_PCR_EVENT_HDR TcgEvent;
1789 UINTN VarNameLength;
1790 EFI_VARIABLE_DATA_TREE *VarLog;
1791
1792 DEBUG ((EFI_D_INFO, "Tcg2Dxe: MeasureVariable (Pcr - %x, EventType - %x, ", (UINTN)PCRIndex, (UINTN)EventType));
1793 DEBUG ((EFI_D_INFO, "VariableName - %s, VendorGuid - %g)\n", VarName, VendorGuid));
1794
1795 VarNameLength = StrLen (VarName);
1796 TcgEvent.PCRIndex = PCRIndex;
1797 TcgEvent.EventType = EventType;
1798
1799 TcgEvent.EventSize = (UINT32)(sizeof (*VarLog) + VarNameLength * sizeof (*VarName) + VarSize
1800 - sizeof (VarLog->UnicodeName) - sizeof (VarLog->VariableData));
1801
1802 VarLog = (EFI_VARIABLE_DATA_TREE *)AllocatePool (TcgEvent.EventSize);
1803 if (VarLog == NULL) {
1804 return EFI_OUT_OF_RESOURCES;
1805 }
1806
1807 VarLog->VariableName = *VendorGuid;
1808 VarLog->UnicodeNameLength = VarNameLength;
1809 VarLog->VariableDataLength = VarSize;
1810 CopyMem (
1811 VarLog->UnicodeName,
1812 VarName,
1813 VarNameLength * sizeof (*VarName)
1814 );
1815 if (VarSize != 0 && VarData != NULL) {
1816 CopyMem (
1817 (CHAR16 *)VarLog->UnicodeName + VarNameLength,
1818 VarData,
1819 VarSize
1820 );
1821 }
1822
1823 if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {
1824 //
1825 // Digest is the event data (EFI_VARIABLE_DATA)
1826 //
1827 Status = TcgDxeHashLogExtendEvent (
1828 0,
1829 (UINT8*)VarLog,
1830 TcgEvent.EventSize,
1831 &TcgEvent,
1832 (UINT8*)VarLog
1833 );
1834 } else {
1835 ASSERT (VarData != NULL);
1836 Status = TcgDxeHashLogExtendEvent (
1837 0,
1838 (UINT8*)VarData,
1839 VarSize,
1840 &TcgEvent,
1841 (UINT8*)VarLog
1842 );
1843 }
1844 FreePool (VarLog);
1845 return Status;
1846 }
1847
1848 /**
1849 Read then Measure and log an EFI variable, and extend the measurement result into a specific PCR.
1850
1851 @param[in] PCRIndex PCR Index.
1852 @param[in] EventType Event type.
1853 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1854 @param[in] VendorGuid A unique identifier for the vendor.
1855 @param[out] VarSize The size of the variable data.
1856 @param[out] VarData Pointer to the content of the variable.
1857
1858 @retval EFI_SUCCESS Operation completed successfully.
1859 @retval EFI_OUT_OF_RESOURCES Out of memory.
1860 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1861
1862 **/
1863 EFI_STATUS
1864 ReadAndMeasureVariable (
1865 IN TPM_PCRINDEX PCRIndex,
1866 IN TCG_EVENTTYPE EventType,
1867 IN CHAR16 *VarName,
1868 IN EFI_GUID *VendorGuid,
1869 OUT UINTN *VarSize,
1870 OUT VOID **VarData
1871 )
1872 {
1873 EFI_STATUS Status;
1874
1875 Status = GetVariable2 (VarName, VendorGuid, VarData, VarSize);
1876 if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {
1877 if (EFI_ERROR (Status)) {
1878 //
1879 // It is valid case, so we need handle it.
1880 //
1881 *VarData = NULL;
1882 *VarSize = 0;
1883 }
1884 } else {
1885 //
1886 // if status error, VarData is freed and set NULL by GetVariable2
1887 //
1888 if (EFI_ERROR (Status)) {
1889 return EFI_NOT_FOUND;
1890 }
1891 }
1892
1893 Status = MeasureVariable (
1894 PCRIndex,
1895 EventType,
1896 VarName,
1897 VendorGuid,
1898 *VarData,
1899 *VarSize
1900 );
1901 return Status;
1902 }
1903
1904 /**
1905 Read then Measure and log an EFI boot variable, and extend the measurement result into PCR[5].
1906
1907 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1908 @param[in] VendorGuid A unique identifier for the vendor.
1909 @param[out] VarSize The size of the variable data.
1910 @param[out] VarData Pointer to the content of the variable.
1911
1912 @retval EFI_SUCCESS Operation completed successfully.
1913 @retval EFI_OUT_OF_RESOURCES Out of memory.
1914 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1915
1916 **/
1917 EFI_STATUS
1918 ReadAndMeasureBootVariable (
1919 IN CHAR16 *VarName,
1920 IN EFI_GUID *VendorGuid,
1921 OUT UINTN *VarSize,
1922 OUT VOID **VarData
1923 )
1924 {
1925 return ReadAndMeasureVariable (
1926 5,
1927 EV_EFI_VARIABLE_BOOT,
1928 VarName,
1929 VendorGuid,
1930 VarSize,
1931 VarData
1932 );
1933 }
1934
1935 /**
1936 Read then Measure and log an EFI Secure variable, and extend the measurement result into PCR[7].
1937
1938 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
1939 @param[in] VendorGuid A unique identifier for the vendor.
1940 @param[out] VarSize The size of the variable data.
1941 @param[out] VarData Pointer to the content of the variable.
1942
1943 @retval EFI_SUCCESS Operation completed successfully.
1944 @retval EFI_OUT_OF_RESOURCES Out of memory.
1945 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1946
1947 **/
1948 EFI_STATUS
1949 ReadAndMeasureSecureVariable (
1950 IN CHAR16 *VarName,
1951 IN EFI_GUID *VendorGuid,
1952 OUT UINTN *VarSize,
1953 OUT VOID **VarData
1954 )
1955 {
1956 return ReadAndMeasureVariable (
1957 7,
1958 EV_EFI_VARIABLE_DRIVER_CONFIG,
1959 VarName,
1960 VendorGuid,
1961 VarSize,
1962 VarData
1963 );
1964 }
1965
1966 /**
1967 Measure and log all EFI boot variables, and extend the measurement result into a specific PCR.
1968
1969 The EFI boot variables are BootOrder and Boot#### variables.
1970
1971 @retval EFI_SUCCESS Operation completed successfully.
1972 @retval EFI_OUT_OF_RESOURCES Out of memory.
1973 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
1974
1975 **/
1976 EFI_STATUS
1977 MeasureAllBootVariables (
1978 VOID
1979 )
1980 {
1981 EFI_STATUS Status;
1982 UINT16 *BootOrder;
1983 UINTN BootCount;
1984 UINTN Index;
1985 VOID *BootVarData;
1986 UINTN Size;
1987
1988 Status = ReadAndMeasureBootVariable (
1989 mBootVarName,
1990 &gEfiGlobalVariableGuid,
1991 &BootCount,
1992 (VOID **) &BootOrder
1993 );
1994 if (Status == EFI_NOT_FOUND || BootOrder == NULL) {
1995 return EFI_SUCCESS;
1996 }
1997
1998 if (EFI_ERROR (Status)) {
1999 //
2000 // BootOrder can't be NULL if status is not EFI_NOT_FOUND
2001 //
2002 FreePool (BootOrder);
2003 return Status;
2004 }
2005
2006 BootCount /= sizeof (*BootOrder);
2007 for (Index = 0; Index < BootCount; Index++) {
2008 UnicodeSPrint (mBootVarName, sizeof (mBootVarName), L"Boot%04x", BootOrder[Index]);
2009 Status = ReadAndMeasureBootVariable (
2010 mBootVarName,
2011 &gEfiGlobalVariableGuid,
2012 &Size,
2013 &BootVarData
2014 );
2015 if (!EFI_ERROR (Status)) {
2016 FreePool (BootVarData);
2017 }
2018 }
2019
2020 FreePool (BootOrder);
2021 return EFI_SUCCESS;
2022 }
2023
2024 /**
2025 Measure and log all EFI Secure variables, and extend the measurement result into a specific PCR.
2026
2027 The EFI boot variables are BootOrder and Boot#### variables.
2028
2029 @retval EFI_SUCCESS Operation completed successfully.
2030 @retval EFI_OUT_OF_RESOURCES Out of memory.
2031 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
2032
2033 **/
2034 EFI_STATUS
2035 MeasureAllSecureVariables (
2036 VOID
2037 )
2038 {
2039 EFI_STATUS Status;
2040 VOID *Data;
2041 UINTN DataSize;
2042 UINTN Index;
2043
2044 Status = EFI_NOT_FOUND;
2045 for (Index = 0; Index < sizeof(mVariableType)/sizeof(mVariableType[0]); Index++) {
2046 Status = ReadAndMeasureSecureVariable (
2047 mVariableType[Index].VariableName,
2048 mVariableType[Index].VendorGuid,
2049 &DataSize,
2050 &Data
2051 );
2052 if (!EFI_ERROR (Status)) {
2053 if (Data != NULL) {
2054 FreePool (Data);
2055 }
2056 }
2057 }
2058
2059 return EFI_SUCCESS;
2060 }
2061
2062 /**
2063 Measure and log launch of FirmwareDebugger, and extend the measurement result into a specific PCR.
2064
2065 @retval EFI_SUCCESS Operation completed successfully.
2066 @retval EFI_OUT_OF_RESOURCES Out of memory.
2067 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
2068
2069 **/
2070 EFI_STATUS
2071 MeasureLaunchOfFirmwareDebugger (
2072 VOID
2073 )
2074 {
2075 TCG_PCR_EVENT_HDR TcgEvent;
2076
2077 TcgEvent.PCRIndex = 7;
2078 TcgEvent.EventType = EV_EFI_ACTION;
2079 TcgEvent.EventSize = sizeof(FIRMWARE_DEBUGGER_EVENT_STRING) - 1;
2080 return TcgDxeHashLogExtendEvent (
2081 0,
2082 (UINT8 *)FIRMWARE_DEBUGGER_EVENT_STRING,
2083 sizeof(FIRMWARE_DEBUGGER_EVENT_STRING) - 1,
2084 &TcgEvent,
2085 (UINT8 *)FIRMWARE_DEBUGGER_EVENT_STRING
2086 );
2087 }
2088
2089 /**
2090 Measure and log all Secure Boot Policy, and extend the measurement result into a specific PCR.
2091
2092 Platform firmware adhering to the policy must therefore measure the following values into PCR[7]: (in order listed)
2093 - The contents of the SecureBoot variable
2094 - The contents of the PK variable
2095 - The contents of the KEK variable
2096 - The contents of the EFI_IMAGE_SECURITY_DATABASE variable
2097 - The contents of the EFI_IMAGE_SECURITY_DATABASE1 variable
2098 - Separator
2099 - Entries in the EFI_IMAGE_SECURITY_DATABASE that are used to validate EFI Drivers or EFI Boot Applications in the boot path
2100
2101 NOTE: Because of the above, UEFI variables PK, KEK, EFI_IMAGE_SECURITY_DATABASE,
2102 EFI_IMAGE_SECURITY_DATABASE1 and SecureBoot SHALL NOT be measured into PCR[3].
2103
2104 @param[in] Event Event whose notification function is being invoked
2105 @param[in] Context Pointer to the notification function's context
2106 **/
2107 VOID
2108 EFIAPI
2109 MeasureSecureBootPolicy (
2110 IN EFI_EVENT Event,
2111 IN VOID *Context
2112 )
2113 {
2114 EFI_STATUS Status;
2115 VOID *Protocol;
2116
2117 Status = gBS->LocateProtocol (&gEfiVariableWriteArchProtocolGuid, NULL, (VOID **)&Protocol);
2118 if (EFI_ERROR (Status)) {
2119 return;
2120 }
2121
2122 if (PcdGetBool (PcdFirmwareDebuggerInitialized)) {
2123 Status = MeasureLaunchOfFirmwareDebugger ();
2124 DEBUG ((EFI_D_INFO, "MeasureLaunchOfFirmwareDebugger - %r\n", Status));
2125 }
2126
2127 Status = MeasureAllSecureVariables ();
2128 DEBUG ((EFI_D_INFO, "MeasureAllSecureVariables - %r\n", Status));
2129
2130 //
2131 // We need measure Separator(7) here, because this event must be between SecureBootPolicy (Configure)
2132 // and ImageVerification (Authority)
2133 // There might be a case that we need measure UEFI image from DriverOrder, besides BootOrder. So
2134 // the Authority measurement happen before ReadToBoot event.
2135 //
2136 Status = MeasureSeparatorEvent (7);
2137 DEBUG ((EFI_D_INFO, "MeasureSeparatorEvent - %r\n", Status));
2138 return ;
2139 }
2140
2141 /**
2142 Ready to Boot Event notification handler.
2143
2144 Sequence of OS boot events is measured in this event notification handler.
2145
2146 @param[in] Event Event whose notification function is being invoked
2147 @param[in] Context Pointer to the notification function's context
2148
2149 **/
2150 VOID
2151 EFIAPI
2152 OnReadyToBoot (
2153 IN EFI_EVENT Event,
2154 IN VOID *Context
2155 )
2156 {
2157 EFI_STATUS Status;
2158 TPM_PCRINDEX PcrIndex;
2159
2160 PERF_START_EX (mImageHandle, "EventRec", "Tcg2Dxe", 0, PERF_ID_TCG2_DXE);
2161 if (mBootAttempts == 0) {
2162
2163 //
2164 // Measure handoff tables.
2165 //
2166 Status = MeasureHandoffTables ();
2167 if (EFI_ERROR (Status)) {
2168 DEBUG ((EFI_D_ERROR, "HOBs not Measured. Error!\n"));
2169 }
2170
2171 //
2172 // Measure BootOrder & Boot#### variables.
2173 //
2174 Status = MeasureAllBootVariables ();
2175 if (EFI_ERROR (Status)) {
2176 DEBUG ((EFI_D_ERROR, "Boot Variables not Measured. Error!\n"));
2177 }
2178
2179 //
2180 // 1. This is the first boot attempt.
2181 //
2182 Status = TcgMeasureAction (
2183 EFI_CALLING_EFI_APPLICATION
2184 );
2185 if (EFI_ERROR (Status)) {
2186 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_CALLING_EFI_APPLICATION));
2187 }
2188
2189 //
2190 // 2. Draw a line between pre-boot env and entering post-boot env.
2191 // PCR[7] is already done.
2192 //
2193 for (PcrIndex = 0; PcrIndex < 7; PcrIndex++) {
2194 Status = MeasureSeparatorEvent (PcrIndex);
2195 if (EFI_ERROR (Status)) {
2196 DEBUG ((DEBUG_ERROR, "Separator Event not Measured. Error!\n"));
2197 }
2198 }
2199
2200 //
2201 // 3. Measure GPT. It would be done in SAP driver.
2202 //
2203
2204 //
2205 // 4. Measure PE/COFF OS loader. It would be done in SAP driver.
2206 //
2207
2208 //
2209 // 5. Read & Measure variable. BootOrder already measured.
2210 //
2211 } else {
2212 //
2213 // 6. Not first attempt, meaning a return from last attempt
2214 //
2215 Status = TcgMeasureAction (
2216 EFI_RETURNING_FROM_EFI_APPLICATOIN
2217 );
2218 if (EFI_ERROR (Status)) {
2219 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_RETURNING_FROM_EFI_APPLICATOIN));
2220 }
2221 }
2222
2223 DEBUG ((EFI_D_INFO, "TPM2 Tcg2Dxe Measure Data when ReadyToBoot\n"));
2224 //
2225 // Increase boot attempt counter.
2226 //
2227 mBootAttempts++;
2228 PERF_END_EX (mImageHandle, "EventRec", "Tcg2Dxe", 0, PERF_ID_TCG2_DXE + 1);
2229 }
2230
2231 /**
2232 Exit Boot Services Event notification handler.
2233
2234 Measure invocation and success of ExitBootServices.
2235
2236 @param[in] Event Event whose notification function is being invoked
2237 @param[in] Context Pointer to the notification function's context
2238
2239 **/
2240 VOID
2241 EFIAPI
2242 OnExitBootServices (
2243 IN EFI_EVENT Event,
2244 IN VOID *Context
2245 )
2246 {
2247 EFI_STATUS Status;
2248
2249 //
2250 // Measure invocation of ExitBootServices,
2251 //
2252 Status = TcgMeasureAction (
2253 EFI_EXIT_BOOT_SERVICES_INVOCATION
2254 );
2255 if (EFI_ERROR (Status)) {
2256 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_INVOCATION));
2257 }
2258
2259 //
2260 // Measure success of ExitBootServices
2261 //
2262 Status = TcgMeasureAction (
2263 EFI_EXIT_BOOT_SERVICES_SUCCEEDED
2264 );
2265 if (EFI_ERROR (Status)) {
2266 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_SUCCEEDED));
2267 }
2268 }
2269
2270 /**
2271 Exit Boot Services Failed Event notification handler.
2272
2273 Measure Failure of ExitBootServices.
2274
2275 @param[in] Event Event whose notification function is being invoked
2276 @param[in] Context Pointer to the notification function's context
2277
2278 **/
2279 VOID
2280 EFIAPI
2281 OnExitBootServicesFailed (
2282 IN EFI_EVENT Event,
2283 IN VOID *Context
2284 )
2285 {
2286 EFI_STATUS Status;
2287
2288 //
2289 // Measure Failure of ExitBootServices,
2290 //
2291 Status = TcgMeasureAction (
2292 EFI_EXIT_BOOT_SERVICES_FAILED
2293 );
2294 if (EFI_ERROR (Status)) {
2295 DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_FAILED));
2296 }
2297
2298 }
2299
2300 /**
2301 The function install Tcg2 protocol.
2302
2303 @retval EFI_SUCCESS Tcg2 protocol is installed.
2304 @retval other Some error occurs.
2305 **/
2306 EFI_STATUS
2307 InstallTcg2 (
2308 VOID
2309 )
2310 {
2311 EFI_STATUS Status;
2312 EFI_HANDLE Handle;
2313
2314 Handle = NULL;
2315 Status = gBS->InstallMultipleProtocolInterfaces (
2316 &Handle,
2317 &gEfiTcg2ProtocolGuid,
2318 &mTcg2Protocol,
2319 NULL
2320 );
2321 return Status;
2322 }
2323
2324 /**
2325 The driver's entry point. It publishes EFI Tcg2 Protocol.
2326
2327 @param[in] ImageHandle The firmware allocated handle for the EFI image.
2328 @param[in] SystemTable A pointer to the EFI System Table.
2329
2330 @retval EFI_SUCCESS The entry point is executed successfully.
2331 @retval other Some error occurs when executing this entry point.
2332 **/
2333 EFI_STATUS
2334 EFIAPI
2335 DriverEntry (
2336 IN EFI_HANDLE ImageHandle,
2337 IN EFI_SYSTEM_TABLE *SystemTable
2338 )
2339 {
2340 EFI_STATUS Status;
2341 EFI_EVENT Event;
2342 VOID *Registration;
2343 UINT32 MaxCommandSize;
2344 UINT32 MaxResponseSize;
2345 UINTN Index;
2346 EFI_TCG2_EVENT_ALGORITHM_BITMAP TpmHashAlgorithmBitmap;
2347 UINT32 ActivePCRBanks;
2348 UINT32 NumberOfPCRBanks;
2349
2350 mImageHandle = ImageHandle;
2351
2352 if (CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceNoneGuid) ||
2353 CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm12Guid)){
2354 DEBUG ((EFI_D_ERROR, "No TPM2 instance required!\n"));
2355 return EFI_UNSUPPORTED;
2356 }
2357
2358 if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {
2359 DEBUG ((EFI_D_ERROR, "TPM2 error!\n"));
2360 return EFI_DEVICE_ERROR;
2361 }
2362
2363 Status = Tpm2RequestUseTpm ();
2364 if (EFI_ERROR (Status)) {
2365 DEBUG ((EFI_D_ERROR, "TPM2 not detected!\n"));
2366 return Status;
2367 }
2368
2369 //
2370 // Fill information
2371 //
2372 ASSERT (TCG_EVENT_LOG_AREA_COUNT_MAX == sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]));
2373
2374 mTcgDxeData.BsCap.Size = sizeof(EFI_TCG2_BOOT_SERVICE_CAPABILITY);
2375 mTcgDxeData.BsCap.ProtocolVersion.Major = 1;
2376 mTcgDxeData.BsCap.ProtocolVersion.Minor = 1;
2377 mTcgDxeData.BsCap.StructureVersion.Major = 1;
2378 mTcgDxeData.BsCap.StructureVersion.Minor = 1;
2379
2380 DEBUG ((EFI_D_INFO, "Tcg2.ProtocolVersion - %02x.%02x\n", mTcgDxeData.BsCap.ProtocolVersion.Major, mTcgDxeData.BsCap.ProtocolVersion.Minor));
2381 DEBUG ((EFI_D_INFO, "Tcg2.StructureVersion - %02x.%02x\n", mTcgDxeData.BsCap.StructureVersion.Major, mTcgDxeData.BsCap.StructureVersion.Minor));
2382
2383 Status = Tpm2GetCapabilityManufactureID (&mTcgDxeData.BsCap.ManufacturerID);
2384 if (EFI_ERROR (Status)) {
2385 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityManufactureID fail!\n"));
2386 } else {
2387 DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityManufactureID - %08x\n", mTcgDxeData.BsCap.ManufacturerID));
2388 }
2389
2390 DEBUG_CODE (
2391 UINT32 FirmwareVersion1;
2392 UINT32 FirmwareVersion2;
2393
2394 Status = Tpm2GetCapabilityFirmwareVersion (&FirmwareVersion1, &FirmwareVersion2);
2395 if (EFI_ERROR (Status)) {
2396 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityFirmwareVersion fail!\n"));
2397 } else {
2398 DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityFirmwareVersion - %08x %08x\n", FirmwareVersion1, FirmwareVersion2));
2399 }
2400 );
2401
2402 Status = Tpm2GetCapabilityMaxCommandResponseSize (&MaxCommandSize, &MaxResponseSize);
2403 if (EFI_ERROR (Status)) {
2404 DEBUG ((EFI_D_ERROR, "Tpm2GetCapabilityMaxCommandResponseSize fail!\n"));
2405 } else {
2406 mTcgDxeData.BsCap.MaxCommandSize = (UINT16)MaxCommandSize;
2407 mTcgDxeData.BsCap.MaxResponseSize = (UINT16)MaxResponseSize;
2408 DEBUG ((EFI_D_INFO, "Tpm2GetCapabilityMaxCommandResponseSize - %08x, %08x\n", MaxCommandSize, MaxResponseSize));
2409 }
2410
2411 //
2412 // Get supported PCR and current Active PCRs
2413 //
2414 Status = Tpm2GetCapabilitySupportedAndActivePcrs (&TpmHashAlgorithmBitmap, &ActivePCRBanks);
2415 ASSERT_EFI_ERROR (Status);
2416
2417 mTcgDxeData.BsCap.HashAlgorithmBitmap = TpmHashAlgorithmBitmap & PcdGet32 (PcdTcg2HashAlgorithmBitmap);
2418 mTcgDxeData.BsCap.ActivePcrBanks = ActivePCRBanks & PcdGet32 (PcdTcg2HashAlgorithmBitmap);
2419
2420 //
2421 // Need calculate NumberOfPCRBanks here, because HashAlgorithmBitmap might be removed by PCD.
2422 //
2423 NumberOfPCRBanks = 0;
2424 for (Index = 0; Index < 32; Index++) {
2425 if ((mTcgDxeData.BsCap.HashAlgorithmBitmap & (1u << Index)) != 0) {
2426 NumberOfPCRBanks++;
2427 }
2428 }
2429
2430 if (PcdGet32 (PcdTcg2NumberOfPCRBanks) == 0) {
2431 mTcgDxeData.BsCap.NumberOfPCRBanks = NumberOfPCRBanks;
2432 } else {
2433 mTcgDxeData.BsCap.NumberOfPCRBanks = PcdGet32 (PcdTcg2NumberOfPCRBanks);
2434 if (PcdGet32 (PcdTcg2NumberOfPCRBanks) > NumberOfPCRBanks) {
2435 DEBUG ((EFI_D_ERROR, "ERROR: PcdTcg2NumberOfPCRBanks(0x%x) > NumberOfPCRBanks(0x%x)\n", PcdGet32 (PcdTcg2NumberOfPCRBanks), NumberOfPCRBanks));
2436 mTcgDxeData.BsCap.NumberOfPCRBanks = NumberOfPCRBanks;
2437 }
2438 }
2439
2440 mTcgDxeData.BsCap.SupportedEventLogs = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2 | EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
2441 if ((mTcgDxeData.BsCap.ActivePcrBanks & EFI_TCG2_BOOT_HASH_ALG_SHA1) == 0) {
2442 //
2443 // No need to expose TCG1.2 event log if SHA1 bank does not exist.
2444 //
2445 mTcgDxeData.BsCap.SupportedEventLogs &= ~EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
2446 }
2447
2448 DEBUG ((EFI_D_INFO, "Tcg2.SupportedEventLogs - 0x%08x\n", mTcgDxeData.BsCap.SupportedEventLogs));
2449 DEBUG ((EFI_D_INFO, "Tcg2.HashAlgorithmBitmap - 0x%08x\n", mTcgDxeData.BsCap.HashAlgorithmBitmap));
2450 DEBUG ((EFI_D_INFO, "Tcg2.NumberOfPCRBanks - 0x%08x\n", mTcgDxeData.BsCap.NumberOfPCRBanks));
2451 DEBUG ((EFI_D_INFO, "Tcg2.ActivePcrBanks - 0x%08x\n", mTcgDxeData.BsCap.ActivePcrBanks));
2452
2453 if (mTcgDxeData.BsCap.TPMPresentFlag) {
2454 //
2455 // Setup the log area and copy event log from hob list to it
2456 //
2457 Status = SetupEventLog ();
2458 ASSERT_EFI_ERROR (Status);
2459
2460 //
2461 // Measure handoff tables, Boot#### variables etc.
2462 //
2463 Status = EfiCreateEventReadyToBootEx (
2464 TPL_CALLBACK,
2465 OnReadyToBoot,
2466 NULL,
2467 &Event
2468 );
2469
2470 Status = gBS->CreateEventEx (
2471 EVT_NOTIFY_SIGNAL,
2472 TPL_NOTIFY,
2473 OnExitBootServices,
2474 NULL,
2475 &gEfiEventExitBootServicesGuid,
2476 &Event
2477 );
2478
2479 //
2480 // Measure Exit Boot Service failed
2481 //
2482 Status = gBS->CreateEventEx (
2483 EVT_NOTIFY_SIGNAL,
2484 TPL_NOTIFY,
2485 OnExitBootServicesFailed,
2486 NULL,
2487 &gEventExitBootServicesFailedGuid,
2488 &Event
2489 );
2490
2491 //
2492 // Create event callback, because we need access variable on SecureBootPolicyVariable
2493 // We should use VariableWriteArch instead of VariableArch, because Variable driver
2494 // may update SecureBoot value based on last setting.
2495 //
2496 EfiCreateProtocolNotifyEvent (&gEfiVariableWriteArchProtocolGuid, TPL_CALLBACK, MeasureSecureBootPolicy, NULL, &Registration);
2497 }
2498
2499 //
2500 // Install Tcg2Protocol
2501 //
2502 Status = InstallTcg2 ();
2503 DEBUG ((EFI_D_INFO, "InstallTcg2 - %r\n", Status));
2504
2505 return Status;
2506 }