TianoCore EDK2 master
Loading...
Searching...
No Matches
TdTcg2Dxe.c
Go to the documentation of this file.
1
9#include <PiDxe.h>
13
14#include <Guid/GlobalVariable.h>
15#include <Guid/HobList.h>
16#include <Guid/EventGroup.h>
19#include <Guid/TpmInstance.h>
20
21#include <Protocol/DevicePath.h>
22#include <Protocol/MpService.h>
26#include <Protocol/AcpiTable.h>
27
28#include <Library/DebugLib.h>
32#include <Library/HobLib.h>
34#include <Library/BaseLib.h>
36#include <Library/PrintLib.h>
37#include <Library/PcdLib.h>
38#include <Library/UefiLib.h>
39#include <Library/HashLib.h>
43
45#include <Guid/CcEventHob.h>
46#include <Library/TdxLib.h>
48
49#define PERF_ID_CC_TCG2_DXE 0x3130
50
51#define CC_EVENT_LOG_AREA_COUNT_MAX 1
52
53typedef struct {
54 CHAR16 *VariableName;
55 EFI_GUID *VendorGuid;
57
58typedef struct {
59 EFI_GUID *EventGuid;
60 EFI_CC_EVENT_LOG_FORMAT LogFormat;
62
63typedef struct {
64 EFI_CC_EVENT_LOG_FORMAT EventLogFormat;
66 UINT64 Laml;
67 UINTN EventLogSize;
68 UINT8 *LastEvent;
69 BOOLEAN EventLogStarted;
70 BOOLEAN EventLogTruncated;
71 UINTN Next800155EventOffset;
73
74typedef struct _TDX_DXE_DATA {
76 CC_EVENT_LOG_AREA_STRUCT EventLogAreaStruct[CC_EVENT_LOG_AREA_COUNT_MAX];
77 BOOLEAN GetEventLogCalled[CC_EVENT_LOG_AREA_COUNT_MAX];
78 CC_EVENT_LOG_AREA_STRUCT FinalEventLogAreaStruct[CC_EVENT_LOG_AREA_COUNT_MAX];
79 EFI_CC_FINAL_EVENTS_TABLE *FinalEventsTable[CC_EVENT_LOG_AREA_COUNT_MAX];
81
82typedef struct {
83 TPMI_ALG_HASH HashAlgo;
84 UINT16 HashSize;
85 UINT32 HashMask;
87
88//
89//
90CC_EVENT_INFO_STRUCT mCcEventInfo[] = {
91 { &gCcEventEntryHobGuid, EFI_CC_EVENT_LOG_FORMAT_TCG_2 },
92};
93
94TDX_DXE_DATA mTdxDxeData = {
95 {
96 sizeof (EFI_CC_BOOT_SERVICE_CAPABILITY), // Size
97 { 1, 1 }, // StructureVersion
98 { 1, 1 }, // ProtocolVersion
99 EFI_CC_BOOT_HASH_ALG_SHA384, // HashAlgorithmBitmap
100 EFI_CC_EVENT_LOG_FORMAT_TCG_2, // SupportedEventLogs
101 { 2, 0 } // {CC_TYPE, CC_SUBTYPE}
102 },
103};
104
105UINTN mBootAttempts = 0;
106CHAR16 mBootVarName[] = L"BootOrder";
107
108VARIABLE_TYPE mVariableType[] = {
109 { EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid },
110 { EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid },
111 { EFI_KEY_EXCHANGE_KEY_NAME, &gEfiGlobalVariableGuid },
112 { EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid },
113 { EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid },
114};
115
116EFI_CC_EVENTLOG_ACPI_TABLE mTdxEventlogAcpiTemplate = {
117 {
118 EFI_CC_EVENTLOG_ACPI_TABLE_SIGNATURE,
119 sizeof (mTdxEventlogAcpiTemplate),
120 EFI_CC_EVENTLOG_ACPI_TABLE_REVISION,
121 //
122 // Compiler initializes the remaining bytes to 0
123 // These fields should be filled in production
124 //
125 },
126 { EFI_CC_TYPE_TDX, 0 }, // CcType
127 0, // rsvd
128 0, // laml
129 0, // lasa
130};
131
132//
133// Supported Hash list in Td guest.
134// Currently SHA384 is supported.
135//
136TDX_HASH_INFO mHashInfo[] = {
137 { TPM_ALG_SHA384, SHA384_DIGEST_SIZE, HASH_ALG_SHA384 }
138};
139
147UINT16
149 IN TPMI_ALG_HASH HashAlgo
150 )
151{
152 UINTN Index;
153
154 for (Index = 0; Index < sizeof (mHashInfo)/sizeof (mHashInfo[0]); Index++) {
155 if (mHashInfo[Index].HashAlgo == HashAlgo) {
156 return mHashInfo[Index].HashSize;
157 }
158 }
159
160 return 0;
161}
162
170UINT32
172 IN TPMI_ALG_HASH HashAlgo
173 )
174{
175 UINTN Index;
176
177 for (Index = 0; Index < ARRAY_SIZE (mHashInfo); Index++) {
178 if (mHashInfo[Index].HashAlgo == HashAlgo) {
179 return mHashInfo[Index].HashMask;
180 }
181 }
182
183 ASSERT (FALSE);
184 return 0;
185}
186
196VOID *
198 IN OUT VOID *Buffer,
199 IN TPML_DIGEST_VALUES *DigestList,
200 IN UINT32 HashAlgorithmMask
201 )
202{
203 UINTN Index;
204 UINT16 DigestSize;
205 UINT32 DigestListCount;
206 UINT32 *DigestListCountPtr;
207
208 DigestListCountPtr = (UINT32 *)Buffer;
209 DigestListCount = 0;
210 Buffer = (UINT8 *)Buffer + sizeof (DigestList->count);
211 for (Index = 0; Index < DigestList->count; Index++) {
212 if ((DigestList->digests[Index].hashAlg & HashAlgorithmMask) == 0) {
213 DEBUG ((DEBUG_ERROR, "WARNING: TD Event log has HashAlg unsupported (0x%x)\n", DigestList->digests[Index].hashAlg));
214 continue;
215 }
216
217 CopyMem (Buffer, &DigestList->digests[Index].hashAlg, sizeof (DigestList->digests[Index].hashAlg));
218 Buffer = (UINT8 *)Buffer + sizeof (DigestList->digests[Index].hashAlg);
219 DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg);
220 CopyMem (Buffer, &DigestList->digests[Index].digest, DigestSize);
221 Buffer = (UINT8 *)Buffer + DigestSize;
222 DigestListCount++;
223 }
224
225 WriteUnaligned32 (DigestListCountPtr, DigestListCount);
226
227 return Buffer;
228}
229
230EFI_HANDLE mImageHandle;
231
253 IN UINT32 RtmrIndex,
254 IN EFI_PHYSICAL_ADDRESS ImageAddress,
255 IN UINTN ImageSize,
256 OUT TPML_DIGEST_VALUES *DigestList
257 );
258
259#define COLUME_SIZE (16 * 2)
260
269VOID
271 IN UINT8 *Data,
272 IN UINTN Size
273 )
274{
275 UINTN Index;
276
277 for (Index = 0; Index < Size; Index++) {
278 DEBUG ((DEBUG_INFO, Index == COLUME_SIZE/2 ? " | %02x" : " %02x", (UINTN)Data[Index]));
279 }
280}
281
290VOID
292 IN UINT8 *Data,
293 IN UINTN Size
294 )
295{
296 UINTN Index;
297 UINTN Count;
298 UINTN Left;
299
300 Count = Size / COLUME_SIZE;
301 Left = Size % COLUME_SIZE;
302 for (Index = 0; Index < Count; Index++) {
303 DEBUG ((DEBUG_INFO, "%04x: ", Index * COLUME_SIZE));
304 InternalDumpData (Data + Index * COLUME_SIZE, COLUME_SIZE);
305 DEBUG ((DEBUG_INFO, "\n"));
306 }
307
308 if (Left != 0) {
309 DEBUG ((DEBUG_INFO, "%04x: ", Index * COLUME_SIZE));
310 InternalDumpData (Data + Index * COLUME_SIZE, Left);
311 DEBUG ((DEBUG_INFO, "\n"));
312 }
313}
314
325VOID
327 IN OUT CC_EVENT_HDR *NoActionEvent,
328 IN UINT32 EventSize
329 )
330{
331 UINT32 DigestListCount;
332 TPMI_ALG_HASH HashAlgId;
333 UINT8 *DigestBuffer;
334
335 DigestBuffer = (UINT8 *)NoActionEvent->Digests.digests;
336 DigestListCount = 0;
337
338 NoActionEvent->MrIndex = 0;
339 NoActionEvent->EventType = EV_NO_ACTION;
340
341 //
342 // Set Hash count & hashAlg accordingly, while Digest.digests[n].digest to all 0
343 //
344 ZeroMem (&NoActionEvent->Digests, sizeof (NoActionEvent->Digests));
345
346 if ((mTdxDxeData.BsCap.HashAlgorithmBitmap & EFI_CC_BOOT_HASH_ALG_SHA384) != 0) {
347 HashAlgId = TPM_ALG_SHA384;
348 CopyMem (DigestBuffer, &HashAlgId, sizeof (TPMI_ALG_HASH));
349 DigestBuffer += sizeof (TPMI_ALG_HASH) + GetHashSizeFromAlgo (HashAlgId);
350 DigestListCount++;
351 }
352
353 //
354 // Set Digests Count
355 //
356 WriteUnaligned32 ((UINT32 *)&NoActionEvent->Digests.count, DigestListCount);
357
358 //
359 // Set Event Size
360 //
361 WriteUnaligned32 ((UINT32 *)DigestBuffer, EventSize);
362}
363
377 OUT EFI_CPU_PHYSICAL_LOCATION **LocationBuf,
378 OUT UINTN *Num
379 )
380{
381 EFI_STATUS Status;
382 EFI_MP_SERVICES_PROTOCOL *MpProtocol;
383 UINTN ProcessorNum;
384 UINTN EnabledProcessorNum;
385 EFI_PROCESSOR_INFORMATION ProcessorInfo;
386 EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;
387 UINTN Index;
388
389 Status = gBS->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **)&MpProtocol);
390 if (EFI_ERROR (Status)) {
391 //
392 // MP protocol is not installed
393 //
394 return EFI_UNSUPPORTED;
395 }
396
397 Status = MpProtocol->GetNumberOfProcessors (
398 MpProtocol,
399 &ProcessorNum,
400 &EnabledProcessorNum
401 );
402 if (EFI_ERROR (Status)) {
403 return Status;
404 }
405
406 Status = gBS->AllocatePool (
408 sizeof (EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,
409 (VOID **)&ProcessorLocBuf
410 );
411 if (EFI_ERROR (Status)) {
412 return Status;
413 }
414
415 //
416 // Get each processor Location info
417 //
418 for (Index = 0; Index < ProcessorNum; Index++) {
419 Status = MpProtocol->GetProcessorInfo (
420 MpProtocol,
421 Index,
422 &ProcessorInfo
423 );
424 if (EFI_ERROR (Status)) {
425 FreePool (ProcessorLocBuf);
426 return Status;
427 }
428
429 //
430 // Get all Processor Location info & measure
431 //
432 CopyMem (
433 &ProcessorLocBuf[Index],
434 &ProcessorInfo.Location,
436 );
437 }
438
439 *LocationBuf = ProcessorLocBuf;
440 *Num = ProcessorNum;
441
442 return Status;
443}
444
465EFIAPI
468 IN OUT EFI_CC_BOOT_SERVICE_CAPABILITY *ProtocolCapability
469 )
470{
471 DEBUG ((DEBUG_VERBOSE, "TdGetCapability\n"));
472
473 if ((This == NULL) || (ProtocolCapability == NULL)) {
474 return EFI_INVALID_PARAMETER;
475 }
476
477 CopyMem (ProtocolCapability, &mTdxDxeData.BsCap, sizeof (EFI_CC_BOOT_SERVICE_CAPABILITY));
478
479 return EFI_SUCCESS;
480}
481
490VOID
492 IN TCG_PCR_EVENT_HDR *EventHdr
493 )
494{
495 UINTN Index;
496
497 DEBUG ((DEBUG_INFO, " Event:\n"));
498 DEBUG ((DEBUG_INFO, " MrIndex - %d\n", EventHdr->PCRIndex));
499 DEBUG ((DEBUG_INFO, " EventType - 0x%08x\n", EventHdr->EventType));
500 DEBUG ((DEBUG_INFO, " Digest - "));
501 for (Index = 0; Index < sizeof (TCG_DIGEST); Index++) {
502 DEBUG ((DEBUG_INFO, "%02x ", EventHdr->Digest.digest[Index]));
503 }
504
505 DEBUG ((DEBUG_INFO, "\n"));
506 DEBUG ((DEBUG_INFO, " EventSize - 0x%08x\n", EventHdr->EventSize));
507 InternalDumpHex ((UINT8 *)(EventHdr + 1), EventHdr->EventSize);
508}
509
515VOID
517 IN TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct
518 )
519{
521 UINTN Index;
522 UINT8 *VendorInfoSize;
523 UINT8 *VendorInfo;
524 UINT32 NumberOfAlgorithms;
525
526 DEBUG ((DEBUG_INFO, " TCG_EfiSpecIDEventStruct:\n"));
527 DEBUG ((DEBUG_INFO, " signature - '"));
528 for (Index = 0; Index < sizeof (TcgEfiSpecIdEventStruct->signature); Index++) {
529 DEBUG ((DEBUG_INFO, "%c", TcgEfiSpecIdEventStruct->signature[Index]));
530 }
531
532 DEBUG ((DEBUG_INFO, "'\n"));
533 DEBUG ((DEBUG_INFO, " platformClass - 0x%08x\n", TcgEfiSpecIdEventStruct->platformClass));
534 DEBUG ((DEBUG_INFO, " specVersion - %d.%d%d\n", TcgEfiSpecIdEventStruct->specVersionMajor, TcgEfiSpecIdEventStruct->specVersionMinor, TcgEfiSpecIdEventStruct->specErrata));
535 DEBUG ((DEBUG_INFO, " uintnSize - 0x%02x\n", TcgEfiSpecIdEventStruct->uintnSize));
536
537 CopyMem (&NumberOfAlgorithms, TcgEfiSpecIdEventStruct + 1, sizeof (NumberOfAlgorithms));
538 DEBUG ((DEBUG_INFO, " NumberOfAlgorithms - 0x%08x\n", NumberOfAlgorithms));
539
540 DigestSize = (TCG_EfiSpecIdEventAlgorithmSize *)((UINT8 *)TcgEfiSpecIdEventStruct + sizeof (*TcgEfiSpecIdEventStruct) + sizeof (NumberOfAlgorithms));
541 for (Index = 0; Index < NumberOfAlgorithms; Index++) {
542 DEBUG ((DEBUG_INFO, " digest(%d)\n", Index));
543 DEBUG ((DEBUG_INFO, " algorithmId - 0x%04x\n", DigestSize[Index].algorithmId));
544 DEBUG ((DEBUG_INFO, " digestSize - 0x%04x\n", DigestSize[Index].digestSize));
545 }
546
547 VendorInfoSize = (UINT8 *)&DigestSize[NumberOfAlgorithms];
548 DEBUG ((DEBUG_INFO, " VendorInfoSize - 0x%02x\n", *VendorInfoSize));
549 VendorInfo = VendorInfoSize + 1;
550 DEBUG ((DEBUG_INFO, " VendorInfo - "));
551 for (Index = 0; Index < *VendorInfoSize; Index++) {
552 DEBUG ((DEBUG_INFO, "%02x ", VendorInfo[Index]));
553 }
554
555 DEBUG ((DEBUG_INFO, "\n"));
556}
557
563UINTN
565 IN TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct
566 )
567{
569 UINT8 *VendorInfoSize;
570 UINT32 NumberOfAlgorithms;
571
572 CopyMem (&NumberOfAlgorithms, TcgEfiSpecIdEventStruct + 1, sizeof (NumberOfAlgorithms));
573
574 DigestSize = (TCG_EfiSpecIdEventAlgorithmSize *)((UINT8 *)TcgEfiSpecIdEventStruct + sizeof (*TcgEfiSpecIdEventStruct) + sizeof (NumberOfAlgorithms));
575 VendorInfoSize = (UINT8 *)&DigestSize[NumberOfAlgorithms];
576 return sizeof (TCG_EfiSpecIDEventStruct) + sizeof (UINT32) + (NumberOfAlgorithms * sizeof (TCG_EfiSpecIdEventAlgorithmSize)) + sizeof (UINT8) + (*VendorInfoSize);
577}
578
584VOID
586 IN CC_EVENT *CcEvent
587 )
588{
589 UINT32 DigestIndex;
590 UINT32 DigestCount;
591 TPMI_ALG_HASH HashAlgo;
592 UINT32 DigestSize;
593 UINT8 *DigestBuffer;
594 UINT32 EventSize;
595 UINT8 *EventBuffer;
596
597 DEBUG ((DEBUG_INFO, "Cc Event:\n"));
598 DEBUG ((DEBUG_INFO, " MrIndex - %d\n", CcEvent->MrIndex));
599 DEBUG ((DEBUG_INFO, " EventType - 0x%08x\n", CcEvent->EventType));
600 DEBUG ((DEBUG_INFO, " DigestCount: 0x%08x\n", CcEvent->Digests.count));
601
602 DigestCount = CcEvent->Digests.count;
603 HashAlgo = CcEvent->Digests.digests[0].hashAlg;
604 DigestBuffer = (UINT8 *)&CcEvent->Digests.digests[0].digest;
605 for (DigestIndex = 0; DigestIndex < DigestCount; DigestIndex++) {
606 DEBUG ((DEBUG_INFO, " HashAlgo : 0x%04x\n", HashAlgo));
607 DEBUG ((DEBUG_INFO, " Digest(%d): \n", DigestIndex));
608 DigestSize = GetHashSizeFromAlgo (HashAlgo);
609 InternalDumpHex (DigestBuffer, DigestSize);
610 //
611 // Prepare next
612 //
613 CopyMem (&HashAlgo, DigestBuffer + DigestSize, sizeof (TPMI_ALG_HASH));
614 DigestBuffer = DigestBuffer + DigestSize + sizeof (TPMI_ALG_HASH);
615 }
616
617 DigestBuffer = DigestBuffer - sizeof (TPMI_ALG_HASH);
618
619 CopyMem (&EventSize, DigestBuffer, sizeof (CcEvent->EventSize));
620 DEBUG ((DEBUG_INFO, " EventSize - 0x%08x\n", EventSize));
621 EventBuffer = DigestBuffer + sizeof (CcEvent->EventSize);
622 InternalDumpHex (EventBuffer, EventSize);
623 DEBUG ((DEBUG_INFO, "\n"));
624}
625
633UINTN
635 IN CC_EVENT *CcEvent
636 )
637{
638 UINT32 DigestIndex;
639 UINT32 DigestCount;
640 TPMI_ALG_HASH HashAlgo;
641 UINT32 DigestSize;
642 UINT8 *DigestBuffer;
643 UINT32 EventSize;
644 UINT8 *EventBuffer;
645
646 DigestCount = CcEvent->Digests.count;
647 HashAlgo = CcEvent->Digests.digests[0].hashAlg;
648 DigestBuffer = (UINT8 *)&CcEvent->Digests.digests[0].digest;
649 for (DigestIndex = 0; DigestIndex < DigestCount; DigestIndex++) {
650 DigestSize = GetHashSizeFromAlgo (HashAlgo);
651 //
652 // Prepare next
653 //
654 CopyMem (&HashAlgo, DigestBuffer + DigestSize, sizeof (TPMI_ALG_HASH));
655 DigestBuffer = DigestBuffer + DigestSize + sizeof (TPMI_ALG_HASH);
656 }
657
658 DigestBuffer = DigestBuffer - sizeof (TPMI_ALG_HASH);
659
660 CopyMem (&EventSize, DigestBuffer, sizeof (CcEvent->EventSize));
661 EventBuffer = DigestBuffer + sizeof (CcEvent->EventSize);
662
663 return (UINTN)EventBuffer + EventSize - (UINTN)CcEvent;
664}
665
676VOID
678 IN EFI_CC_EVENT_LOG_FORMAT EventLogFormat,
679 IN EFI_PHYSICAL_ADDRESS EventLogLocation,
680 IN EFI_PHYSICAL_ADDRESS EventLogLastEntry,
681 IN EFI_CC_FINAL_EVENTS_TABLE *FinalEventsTable
682 )
683{
684 TCG_PCR_EVENT_HDR *EventHdr;
685 CC_EVENT *CcEvent;
686 TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct;
687 UINTN NumberOfEvents;
688
689 DEBUG ((DEBUG_INFO, "EventLogFormat: (0x%x)\n", EventLogFormat));
690 ASSERT (EventLogFormat == EFI_CC_EVENT_LOG_FORMAT_TCG_2);
691
692 //
693 // Dump first event.
694 // The first event is always the TCG_PCR_EVENT_HDR
695 // After this event is a TCG_EfiSpecIDEventStruct
696 //
697 EventHdr = (TCG_PCR_EVENT_HDR *)(UINTN)EventLogLocation;
698 DumpPcrEvent (EventHdr);
699
700 TcgEfiSpecIdEventStruct = (TCG_EfiSpecIDEventStruct *)(EventHdr + 1);
701 DumpTcgEfiSpecIdEventStruct (TcgEfiSpecIdEventStruct);
702
703 //
704 // Then the CcEvent (Its structure is similar to TCG_PCR_EVENT2)
705 //
706 CcEvent = (CC_EVENT *)((UINTN)TcgEfiSpecIdEventStruct + GetTcgEfiSpecIdEventStructSize (TcgEfiSpecIdEventStruct));
707 while ((UINTN)CcEvent <= EventLogLastEntry) {
708 DumpCcEvent (CcEvent);
709 CcEvent = (CC_EVENT *)((UINTN)CcEvent + GetCcEventSize (CcEvent));
710 }
711
712 if (FinalEventsTable == NULL) {
713 DEBUG ((DEBUG_INFO, "FinalEventsTable: NOT FOUND\n"));
714 } else {
715 DEBUG ((DEBUG_INFO, "FinalEventsTable: (0x%x)\n", FinalEventsTable));
716 DEBUG ((DEBUG_INFO, " Version: (0x%x)\n", FinalEventsTable->Version));
717 DEBUG ((DEBUG_INFO, " NumberOfEvents: (0x%x)\n", FinalEventsTable->NumberOfEvents));
718
719 CcEvent = (CC_EVENT *)(UINTN)(FinalEventsTable + 1);
720 for (NumberOfEvents = 0; NumberOfEvents < FinalEventsTable->NumberOfEvents; NumberOfEvents++) {
721 DumpCcEvent (CcEvent);
722 CcEvent = (CC_EVENT *)((UINTN)CcEvent + GetCcEventSize (CcEvent));
723 }
724 }
725
726 return;
727}
728
747EFIAPI
750 IN EFI_CC_EVENT_LOG_FORMAT EventLogFormat,
751 OUT EFI_PHYSICAL_ADDRESS *EventLogLocation,
752 OUT EFI_PHYSICAL_ADDRESS *EventLogLastEntry,
753 OUT BOOLEAN *EventLogTruncated
754 )
755{
756 UINTN Index = 0;
757
758 DEBUG ((DEBUG_INFO, "TdGetEventLog ... (0x%x)\n", EventLogFormat));
759 ASSERT (EventLogFormat == EFI_CC_EVENT_LOG_FORMAT_TCG_2);
760
761 if (EventLogLocation != NULL) {
762 *EventLogLocation = mTdxDxeData.EventLogAreaStruct[Index].Lasa;
763 DEBUG ((DEBUG_INFO, "TdGetEventLog (EventLogLocation - %x)\n", *EventLogLocation));
764 }
765
766 if (EventLogLastEntry != NULL) {
767 if (!mTdxDxeData.EventLogAreaStruct[Index].EventLogStarted) {
768 *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)0;
769 } else {
770 *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)mTdxDxeData.EventLogAreaStruct[Index].LastEvent;
771 }
772
773 DEBUG ((DEBUG_INFO, "TdGetEventLog (EventLogLastEntry - %x)\n", *EventLogLastEntry));
774 }
775
776 if (EventLogTruncated != NULL) {
777 *EventLogTruncated = mTdxDxeData.EventLogAreaStruct[Index].EventLogTruncated;
778 DEBUG ((DEBUG_INFO, "TdGetEventLog (EventLogTruncated - %x)\n", *EventLogTruncated));
779 }
780
781 DEBUG ((DEBUG_INFO, "TdGetEventLog - %r\n", EFI_SUCCESS));
782
783 // Dump Event Log for debug purpose
784 if ((EventLogLocation != NULL) && (EventLogLastEntry != NULL)) {
785 DumpCcEventLog (EventLogFormat, *EventLogLocation, *EventLogLastEntry, mTdxDxeData.FinalEventsTable[Index]);
786 }
787
788 //
789 // All events generated after the invocation of EFI_TCG2_GET_EVENT_LOG SHALL be stored
790 // in an instance of an EFI_CONFIGURATION_TABLE named by the VendorGuid of EFI_TCG2_FINAL_EVENTS_TABLE_GUID.
791 //
792 mTdxDxeData.GetEventLogCalled[Index] = TRUE;
793
794 return EFI_SUCCESS;
795}
796
809STATIC
810BOOLEAN
812 IN VOID *NewEventHdr,
813 IN UINT32 NewEventHdrSize,
814 IN UINT8 *NewEventData,
815 IN UINT32 NewEventSize
816 )
817{
818 if (((TCG_PCR_EVENT2_HDR *)NewEventHdr)->EventType != EV_NO_ACTION) {
819 return FALSE;
820 }
821
822 if ((NewEventSize >= sizeof (TCG_Sp800_155_PlatformId_Event2)) &&
823 (CompareMem (
824 NewEventData,
825 TCG_Sp800_155_PlatformId_Event2_SIGNATURE,
826 sizeof (TCG_Sp800_155_PlatformId_Event2_SIGNATURE) - 1
827 ) == 0))
828 {
829 return TRUE;
830 }
831
832 if ((NewEventSize >= sizeof (TCG_Sp800_155_PlatformId_Event3)) &&
833 (CompareMem (
834 NewEventData,
835 TCG_Sp800_155_PlatformId_Event3_SIGNATURE,
836 sizeof (TCG_Sp800_155_PlatformId_Event3_SIGNATURE) - 1
837 ) == 0))
838 {
839 return TRUE;
840 }
841
842 return FALSE;
843}
844
860 IN OUT CC_EVENT_LOG_AREA_STRUCT *EventLogAreaStruct,
861 IN VOID *NewEventHdr,
862 IN UINT32 NewEventHdrSize,
863 IN UINT8 *NewEventData,
864 IN UINT32 NewEventSize
865 )
866{
867 UINTN NewLogSize;
868 BOOLEAN Record800155Event;
869 CC_EVENT_HDR *CcEventHdr;
870
871 CcEventHdr = (CC_EVENT_HDR *)NewEventHdr;
872 DEBUG ((DEBUG_VERBOSE, "Td: Try to log event. Index = %d, EventType = 0x%x\n", CcEventHdr->MrIndex, CcEventHdr->EventType));
873
874 if (NewEventSize > MAX_ADDRESS - NewEventHdrSize) {
875 return EFI_OUT_OF_RESOURCES;
876 }
877
878 NewLogSize = NewEventHdrSize + NewEventSize;
879
880 if (NewLogSize > MAX_ADDRESS - EventLogAreaStruct->EventLogSize) {
881 return EFI_OUT_OF_RESOURCES;
882 }
883
884 if (NewLogSize + EventLogAreaStruct->EventLogSize > EventLogAreaStruct->Laml) {
885 DEBUG ((DEBUG_INFO, " Laml - 0x%x\n", EventLogAreaStruct->Laml));
886 DEBUG ((DEBUG_INFO, " NewLogSize - 0x%x\n", NewLogSize));
887 DEBUG ((DEBUG_INFO, " LogSize - 0x%x\n", EventLogAreaStruct->EventLogSize));
888 DEBUG ((DEBUG_INFO, "TcgCommLogEvent - %r\n", EFI_OUT_OF_RESOURCES));
889 return EFI_OUT_OF_RESOURCES;
890 }
891
892 //
893 // Check 800-155 event
894 // Record to 800-155 event offset only.
895 // If the offset is 0, no need to record.
896 //
897 Record800155Event = Is800155Event (NewEventHdr, NewEventHdrSize, NewEventData, NewEventSize);
898 if (Record800155Event) {
899 DEBUG ((DEBUG_INFO, "It is 800155Event.\n"));
900
901 if (EventLogAreaStruct->Next800155EventOffset != 0) {
902 CopyMem (
903 (UINT8 *)(UINTN)EventLogAreaStruct->Lasa + EventLogAreaStruct->Next800155EventOffset + NewLogSize,
904 (UINT8 *)(UINTN)EventLogAreaStruct->Lasa + EventLogAreaStruct->Next800155EventOffset,
905 EventLogAreaStruct->EventLogSize - EventLogAreaStruct->Next800155EventOffset
906 );
907
908 CopyMem (
909 (UINT8 *)(UINTN)EventLogAreaStruct->Lasa + EventLogAreaStruct->Next800155EventOffset,
910 NewEventHdr,
911 NewEventHdrSize
912 );
913 CopyMem (
914 (UINT8 *)(UINTN)EventLogAreaStruct->Lasa + EventLogAreaStruct->Next800155EventOffset + NewEventHdrSize,
915 NewEventData,
916 NewEventSize
917 );
918
919 EventLogAreaStruct->Next800155EventOffset += NewLogSize;
920 EventLogAreaStruct->LastEvent += NewLogSize;
921 EventLogAreaStruct->EventLogSize += NewLogSize;
922 }
923
924 return EFI_SUCCESS;
925 }
926
927 EventLogAreaStruct->LastEvent = (UINT8 *)(UINTN)EventLogAreaStruct->Lasa + EventLogAreaStruct->EventLogSize;
928 EventLogAreaStruct->EventLogSize += NewLogSize;
929
930 CopyMem (EventLogAreaStruct->LastEvent, NewEventHdr, NewEventHdrSize);
931 CopyMem (
932 EventLogAreaStruct->LastEvent + NewEventHdrSize,
933 NewEventData,
934 NewEventSize
935 );
936
937 return EFI_SUCCESS;
938}
939
941EFIAPI
942TdMapPcrToMrIndex (
944 IN UINT32 PCRIndex,
945 OUT UINT32 *MrIndex
946 )
947{
948 if (MrIndex == NULL) {
949 return EFI_INVALID_PARAMETER;
950 }
951
952 *MrIndex = TdxMeasurementMapPcrToMrIndex (PCRIndex);
953
954 return *MrIndex == CC_MR_INDEX_INVALID ? EFI_INVALID_PARAMETER : EFI_SUCCESS;
955}
956
972 IN EFI_CC_EVENT_LOG_FORMAT EventLogFormat,
973 IN VOID *NewEventHdr,
974 IN UINT32 NewEventHdrSize,
975 IN UINT8 *NewEventData,
976 IN UINT32 NewEventSize
977 )
978{
979 EFI_STATUS Status;
980 UINTN Index;
981 CC_EVENT_LOG_AREA_STRUCT *EventLogAreaStruct;
982
983 if (EventLogFormat != EFI_CC_EVENT_LOG_FORMAT_TCG_2) {
984 ASSERT (FALSE);
985 return EFI_INVALID_PARAMETER;
986 }
987
988 Index = 0;
989
990 //
991 // Record to normal event log
992 //
993 EventLogAreaStruct = &mTdxDxeData.EventLogAreaStruct[Index];
994
995 if (EventLogAreaStruct->EventLogTruncated) {
996 return EFI_VOLUME_FULL;
997 }
998
999 Status = TcgCommLogEvent (
1000 EventLogAreaStruct,
1001 NewEventHdr,
1002 NewEventHdrSize,
1003 NewEventData,
1004 NewEventSize
1005 );
1006
1007 if (Status == EFI_OUT_OF_RESOURCES) {
1008 EventLogAreaStruct->EventLogTruncated = TRUE;
1009 return EFI_VOLUME_FULL;
1010 } else if (Status == EFI_SUCCESS) {
1011 EventLogAreaStruct->EventLogStarted = TRUE;
1012 }
1013
1014 //
1015 // If GetEventLog is called, record to FinalEventsTable, too.
1016 //
1017 if (mTdxDxeData.GetEventLogCalled[Index]) {
1018 if (mTdxDxeData.FinalEventsTable[Index] == NULL) {
1019 //
1020 // no need for FinalEventsTable
1021 //
1022 return EFI_SUCCESS;
1023 }
1024
1025 EventLogAreaStruct = &mTdxDxeData.FinalEventLogAreaStruct[Index];
1026
1027 if (EventLogAreaStruct->EventLogTruncated) {
1028 return EFI_VOLUME_FULL;
1029 }
1030
1031 Status = TcgCommLogEvent (
1032 EventLogAreaStruct,
1033 NewEventHdr,
1034 NewEventHdrSize,
1035 NewEventData,
1036 NewEventSize
1037 );
1038 if (Status == EFI_OUT_OF_RESOURCES) {
1039 EventLogAreaStruct->EventLogTruncated = TRUE;
1040 return EFI_VOLUME_FULL;
1041 } else if (Status == EFI_SUCCESS) {
1042 EventLogAreaStruct->EventLogStarted = TRUE;
1043 //
1044 // Increase the NumberOfEvents in FinalEventsTable
1045 //
1046 (mTdxDxeData.FinalEventsTable[Index])->NumberOfEvents++;
1047 DEBUG ((DEBUG_INFO, "FinalEventsTable->NumberOfEvents - 0x%x\n", (mTdxDxeData.FinalEventsTable[Index])->NumberOfEvents));
1048 DEBUG ((DEBUG_INFO, " Size - 0x%x\n", (UINTN)EventLogAreaStruct->EventLogSize));
1049 }
1050 }
1051
1052 return Status;
1053}
1054
1062UINT32
1064 IN VOID *DigestListBin
1065 )
1066{
1067 UINTN Index;
1068 UINT16 DigestSize;
1069 UINT32 TotalSize;
1070 UINT32 Count;
1071 TPMI_ALG_HASH HashAlg;
1072
1073 Count = ReadUnaligned32 (DigestListBin);
1074 TotalSize = sizeof (Count);
1075 DigestListBin = (UINT8 *)DigestListBin + sizeof (Count);
1076 for (Index = 0; Index < Count; Index++) {
1077 HashAlg = ReadUnaligned16 (DigestListBin);
1078 TotalSize += sizeof (HashAlg);
1079 DigestListBin = (UINT8 *)DigestListBin + sizeof (HashAlg);
1080
1081 DigestSize = GetHashSizeFromAlgo (HashAlg);
1082 TotalSize += DigestSize;
1083 DigestListBin = (UINT8 *)DigestListBin + DigestSize;
1084 }
1085
1086 return TotalSize;
1087}
1088
1099VOID *
1101 IN OUT VOID *Buffer,
1102 IN VOID *DigestListBin,
1103 IN UINT32 HashAlgorithmMask,
1104 OUT UINT32 *HashAlgorithmMaskCopied
1105 )
1106{
1107 UINTN Index;
1108 UINT16 DigestSize;
1109 UINT32 Count;
1110 TPMI_ALG_HASH HashAlg;
1111 UINT32 DigestListCount;
1112 UINT32 *DigestListCountPtr;
1113
1114 DigestListCountPtr = (UINT32 *)Buffer;
1115 DigestListCount = 0;
1116 *HashAlgorithmMaskCopied = 0;
1117
1118 Count = ReadUnaligned32 (DigestListBin);
1119 Buffer = (UINT8 *)Buffer + sizeof (Count);
1120 DigestListBin = (UINT8 *)DigestListBin + sizeof (Count);
1121 for (Index = 0; Index < Count; Index++) {
1122 HashAlg = ReadUnaligned16 (DigestListBin);
1123 DigestListBin = (UINT8 *)DigestListBin + sizeof (HashAlg);
1124 DigestSize = GetHashSizeFromAlgo (HashAlg);
1125
1126 if ((HashAlg & HashAlgorithmMask) != 0) {
1127 CopyMem (Buffer, &HashAlg, sizeof (HashAlg));
1128 Buffer = (UINT8 *)Buffer + sizeof (HashAlg);
1129 CopyMem (Buffer, DigestListBin, DigestSize);
1130 Buffer = (UINT8 *)Buffer + DigestSize;
1131 DigestListCount++;
1132 (*HashAlgorithmMaskCopied) |= GetHashMaskFromAlgo (HashAlg);
1133 } else {
1134 DEBUG ((DEBUG_ERROR, "WARNING: CopyDigestListBinToBuffer Event log has HashAlg unsupported by PCR bank (0x%x)\n", HashAlg));
1135 }
1136
1137 DigestListBin = (UINT8 *)DigestListBin + DigestSize;
1138 }
1139
1140 WriteUnaligned32 (DigestListCountPtr, DigestListCount);
1141
1142 return Buffer;
1143}
1144
1161 IN TPML_DIGEST_VALUES *DigestList,
1162 IN OUT CC_EVENT_HDR *NewEventHdr,
1163 IN UINT8 *NewEventData
1164 )
1165{
1166 EFI_STATUS Status;
1167 EFI_TPL OldTpl;
1168 EFI_STATUS RetStatus;
1169 CC_EVENT CcEvent;
1170 UINT8 *DigestBuffer;
1171 UINT32 *EventSizePtr;
1172 EFI_CC_EVENT_LOG_FORMAT LogFormat;
1173
1174 RetStatus = EFI_SUCCESS;
1175 LogFormat = EFI_CC_EVENT_LOG_FORMAT_TCG_2;
1176
1177 ZeroMem (&CcEvent, sizeof (CcEvent));
1178 CcEvent.MrIndex = NewEventHdr->MrIndex;
1179 CcEvent.EventType = NewEventHdr->EventType;
1180 DigestBuffer = (UINT8 *)&CcEvent.Digests;
1181 EventSizePtr = CopyDigestListToBuffer (DigestBuffer, DigestList, HASH_ALG_SHA384);
1182 CopyMem (EventSizePtr, &NewEventHdr->EventSize, sizeof (NewEventHdr->EventSize));
1183
1184 //
1185 // Enter critical region
1186 //
1187 OldTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
1188 Status = TdxDxeLogEvent (
1189 LogFormat,
1190 &CcEvent,
1191 sizeof (CcEvent.MrIndex) + sizeof (CcEvent.EventType) + GetDigestListBinSize (DigestBuffer) + sizeof (CcEvent.EventSize),
1192 NewEventData,
1193 NewEventHdr->EventSize
1194 );
1195 if (Status != EFI_SUCCESS) {
1196 RetStatus = Status;
1197 }
1198
1199 gBS->RestoreTPL (OldTpl);
1200
1201 return RetStatus;
1202}
1203
1222 IN UINT64 Flags,
1223 IN UINT8 *HashData,
1224 IN UINT64 HashDataLen,
1225 IN OUT CC_EVENT_HDR *NewEventHdr,
1226 IN UINT8 *NewEventData
1227 )
1228{
1229 EFI_STATUS Status;
1230 TPML_DIGEST_VALUES DigestList;
1231 CC_EVENT_HDR NoActionEvent;
1232
1233 if (NewEventHdr->EventType == EV_NO_ACTION) {
1234 //
1235 // Do not do RTMR extend for EV_NO_ACTION
1236 //
1237 Status = EFI_SUCCESS;
1238 InitNoActionEvent (&NoActionEvent, NewEventHdr->EventSize);
1239 if ((Flags & EFI_CC_FLAG_EXTEND_ONLY) == 0) {
1240 Status = TdxDxeLogHashEvent (&(NoActionEvent.Digests), NewEventHdr, NewEventData);
1241 }
1242
1243 return Status;
1244 }
1245
1246 //
1247 // According to UEFI Spec 2.10 Section 38.4.1 the mapping between MrIndex and Intel
1248 // TDX Measurement Register is:
1249 // MrIndex 0 <--> MRTD
1250 // MrIndex 1-3 <--> RTMR[0-2]
1251 // Only the RMTR registers can be extended in TDVF by HashAndExtend. So MrIndex will
1252 // decreased by 1 before it is sent to HashAndExtend.
1253 //
1254 Status = HashAndExtend (
1255 NewEventHdr->MrIndex - 1,
1256 HashData,
1257 (UINTN)HashDataLen,
1258 &DigestList
1259 );
1260 if (!EFI_ERROR (Status)) {
1261 if ((Flags & EFI_CC_FLAG_EXTEND_ONLY) == 0) {
1262 Status = TdxDxeLogHashEvent (&DigestList, NewEventHdr, NewEventData);
1263 }
1264 }
1265
1266 return Status;
1267}
1268
1289EFIAPI
1292 IN UINT64 Flags,
1293 IN EFI_PHYSICAL_ADDRESS DataToHash,
1294 IN UINT64 DataToHashLen,
1295 IN EFI_CC_EVENT *CcEvent
1296 )
1297{
1298 EFI_STATUS Status;
1299 CC_EVENT_HDR NewEventHdr;
1300 TPML_DIGEST_VALUES DigestList;
1301
1302 DEBUG ((DEBUG_VERBOSE, "TdHashLogExtendEvent ...\n"));
1303
1304 if ((This == NULL) || (CcEvent == NULL)) {
1305 return EFI_INVALID_PARAMETER;
1306 }
1307
1308 //
1309 // Do not check hash data size for EV_NO_ACTION event.
1310 //
1311 if ((CcEvent->Header.EventType != EV_NO_ACTION) && (DataToHash == 0)) {
1312 return EFI_INVALID_PARAMETER;
1313 }
1314
1315 if (CcEvent->Size < CcEvent->Header.HeaderSize + sizeof (UINT32)) {
1316 return EFI_INVALID_PARAMETER;
1317 }
1318
1319 if (CcEvent->Header.MrIndex == CC_MR_INDEX_0_MRTD) {
1320 DEBUG ((DEBUG_ERROR, "%a: MRTD cannot be extended in TDVF.\n", __func__));
1321 return EFI_INVALID_PARAMETER;
1322 }
1323
1324 if (CcEvent->Header.MrIndex >= CC_MR_INDEX_INVALID) {
1325 DEBUG ((DEBUG_ERROR, "%a: MrIndex is invalid. (%d)\n", __func__, CcEvent->Header.MrIndex));
1326 return EFI_INVALID_PARAMETER;
1327 }
1328
1329 NewEventHdr.MrIndex = CcEvent->Header.MrIndex;
1330 NewEventHdr.EventType = CcEvent->Header.EventType;
1331 NewEventHdr.EventSize = CcEvent->Size - sizeof (UINT32) - CcEvent->Header.HeaderSize;
1332 if ((Flags & EFI_CC_FLAG_PE_COFF_IMAGE) != 0) {
1333 //
1334 // According to UEFI Spec 2.10 Section 38.4.1 the mapping between MrIndex and Intel
1335 // TDX Measurement Register is:
1336 // MrIndex 0 <--> MRTD
1337 // MrIndex 1-3 <--> RTMR[0-2]
1338 // Only the RMTR registers can be extended in TDVF by HashAndExtend. So MrIndex will
1339 // decreased by 1 before it is sent to MeasurePeImageAndExtend.
1340 //
1341 Status = MeasurePeImageAndExtend (
1342 NewEventHdr.MrIndex - 1,
1343 DataToHash,
1344 (UINTN)DataToHashLen,
1345 &DigestList
1346 );
1347 if (!EFI_ERROR (Status)) {
1348 if ((Flags & EFI_CC_FLAG_EXTEND_ONLY) == 0) {
1349 Status = TdxDxeLogHashEvent (&DigestList, &NewEventHdr, CcEvent->Event);
1350 }
1351 }
1352 } else {
1353 Status = TdxDxeHashLogExtendEvent (
1354 Flags,
1355 (UINT8 *)(UINTN)DataToHash,
1356 DataToHashLen,
1357 &NewEventHdr,
1358 CcEvent->Event
1359 );
1360 }
1361
1362 DEBUG ((DEBUG_VERBOSE, "TdHashLogExtendEvent - %r\n", Status));
1363 return Status;
1364}
1365
1366EFI_CC_MEASUREMENT_PROTOCOL mTdProtocol = {
1370 TdMapPcrToMrIndex,
1371};
1372
1373#define TD_HASH_COUNT 1
1374#define TEMP_BUF_LEN (sizeof(TCG_EfiSpecIDEventStruct) + sizeof(UINT32) \
1375 + (TD_HASH_COUNT * sizeof(TCG_EfiSpecIdEventAlgorithmSize)) + sizeof(UINT8))
1376
1386 VOID
1387 )
1388{
1389 EFI_STATUS Status;
1391 UINTN Index;
1392 TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct;
1393 UINT8 TempBuf[TEMP_BUF_LEN];
1394 TCG_PCR_EVENT_HDR SpecIdEvent;
1396 TCG_EfiSpecIdEventAlgorithmSize *TempDigestSize;
1397 UINT8 *VendorInfoSize;
1398 UINT32 NumberOfAlgorithms;
1399 EFI_CC_EVENT_LOG_FORMAT LogFormat;
1400 EFI_PEI_HOB_POINTERS GuidHob;
1401 CC_EVENT_HDR NoActionEvent;
1402
1403 Status = EFI_SUCCESS;
1404 DEBUG ((DEBUG_INFO, "SetupCcEventLog\n"));
1405
1406 Index = 0;
1407 LogFormat = EFI_CC_EVENT_LOG_FORMAT_TCG_2;
1408
1409 //
1410 // 1. Create Log Area
1411 //
1412 mTdxDxeData.EventLogAreaStruct[Index].EventLogFormat = LogFormat;
1413
1414 // allocate pages for TD Event log
1415 Status = gBS->AllocatePages (
1418 EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcgLogAreaMinLen)),
1419 &Lasa
1420 );
1421 if (EFI_ERROR (Status)) {
1422 return Status;
1423 }
1424
1425 mTdxDxeData.EventLogAreaStruct[Index].Lasa = Lasa;
1426 mTdxDxeData.EventLogAreaStruct[Index].Laml = PcdGet32 (PcdTcgLogAreaMinLen);
1427 mTdxDxeData.EventLogAreaStruct[Index].Next800155EventOffset = 0;
1428
1429 //
1430 // Report TD event log address and length, so that they can be reported in
1431 // TD ACPI table. Ignore the return status, because those fields are optional.
1432 //
1433 PcdSet32S (PcdCcEventlogAcpiTableLaml, (UINT32)mTdxDxeData.EventLogAreaStruct[Index].Laml);
1434 PcdSet64S (PcdCcEventlogAcpiTableLasa, mTdxDxeData.EventLogAreaStruct[Index].Lasa);
1435
1436 //
1437 // To initialize them as 0xFF is recommended
1438 // because the OS can know the last entry for that.
1439 //
1440 SetMem ((VOID *)(UINTN)Lasa, PcdGet32 (PcdTcgLogAreaMinLen), 0xFF);
1441
1442 //
1443 // Create first entry for Log Header Entry Data
1444 //
1445
1446 //
1447 // TcgEfiSpecIdEventStruct
1448 //
1449 TcgEfiSpecIdEventStruct = (TCG_EfiSpecIDEventStruct *)TempBuf;
1450 CopyMem (TcgEfiSpecIdEventStruct->signature, TCG_EfiSpecIDEventStruct_SIGNATURE_03, sizeof (TcgEfiSpecIdEventStruct->signature));
1451
1452 TcgEfiSpecIdEventStruct->platformClass = PcdGet8 (PcdTpmPlatformClass);
1453
1454 TcgEfiSpecIdEventStruct->specVersionMajor = TCG_EfiSpecIDEventStruct_SPEC_VERSION_MAJOR_TPM2;
1455 TcgEfiSpecIdEventStruct->specVersionMinor = TCG_EfiSpecIDEventStruct_SPEC_VERSION_MINOR_TPM2;
1456 TcgEfiSpecIdEventStruct->specErrata = TCG_EfiSpecIDEventStruct_SPEC_ERRATA_TPM2;
1457 TcgEfiSpecIdEventStruct->uintnSize = sizeof (UINTN)/sizeof (UINT32);
1458 NumberOfAlgorithms = 0;
1459 DigestSize = (TCG_EfiSpecIdEventAlgorithmSize *)((UINT8 *)TcgEfiSpecIdEventStruct
1460 + sizeof (*TcgEfiSpecIdEventStruct)
1461 + sizeof (NumberOfAlgorithms));
1462
1463 TempDigestSize = DigestSize;
1464 TempDigestSize += NumberOfAlgorithms;
1465 TempDigestSize->algorithmId = TPM_ALG_SHA384;
1466 TempDigestSize->digestSize = SHA384_DIGEST_SIZE;
1467 NumberOfAlgorithms++;
1468
1469 CopyMem (TcgEfiSpecIdEventStruct + 1, &NumberOfAlgorithms, sizeof (NumberOfAlgorithms));
1470 TempDigestSize = DigestSize;
1471 TempDigestSize += NumberOfAlgorithms;
1472 VendorInfoSize = (UINT8 *)TempDigestSize;
1473 *VendorInfoSize = 0;
1474
1475 SpecIdEvent.PCRIndex = 1; // PCRIndex 0 maps to MrIndex 1
1476 SpecIdEvent.EventType = EV_NO_ACTION;
1477 ZeroMem (&SpecIdEvent.Digest, sizeof (SpecIdEvent.Digest));
1478 SpecIdEvent.EventSize = (UINT32)GetTcgEfiSpecIdEventStructSize (TcgEfiSpecIdEventStruct);
1479
1480 //
1481 // TD Event log re-use the spec of TCG2 Event log.
1482 // Log TcgEfiSpecIdEventStruct as the first Event. Event format is TCG_PCR_EVENT.
1483 // TCG EFI Protocol Spec. Section 5.3 Event Log Header
1484 // TCG PC Client PFP spec. Section 9.2 Measurement Event Entries and Log
1485 //
1486 Status = TdxDxeLogEvent (
1487 LogFormat,
1488 &SpecIdEvent,
1489 sizeof (SpecIdEvent),
1490 (UINT8 *)TcgEfiSpecIdEventStruct,
1491 SpecIdEvent.EventSize
1492 );
1493 //
1494 // record the offset at the end of 800-155 event.
1495 // the future 800-155 event can be inserted here.
1496 //
1497 mTdxDxeData.EventLogAreaStruct[Index].Next800155EventOffset = mTdxDxeData.EventLogAreaStruct[Index].EventLogSize;
1498
1499 //
1500 // Tcg800155PlatformIdEvent. Event format is TCG_PCR_EVENT2
1501 //
1502 GuidHob.Guid = GetFirstGuidHob (&gTcg800155PlatformIdEventHobGuid);
1503 while (GuidHob.Guid != NULL) {
1504 InitNoActionEvent (&NoActionEvent, GET_GUID_HOB_DATA_SIZE (GuidHob.Guid));
1505
1506 Status = TdxDxeLogEvent (
1507 LogFormat,
1508 &NoActionEvent,
1509 sizeof (NoActionEvent.MrIndex) + sizeof (NoActionEvent.EventType) + GetDigestListBinSize (&NoActionEvent.Digests) + sizeof (NoActionEvent.EventSize),
1510 GET_GUID_HOB_DATA (GuidHob.Guid),
1511 GET_GUID_HOB_DATA_SIZE (GuidHob.Guid)
1512 );
1513
1514 GuidHob.Guid = GET_NEXT_HOB (GuidHob);
1515 GuidHob.Guid = GetNextGuidHob (&gTcg800155PlatformIdEventHobGuid, GuidHob.Guid);
1516 }
1517
1518 //
1519 // 2. Create Final Log Area
1520 //
1521 Status = gBS->AllocatePages (
1524 EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcg2FinalLogAreaLen)),
1525 &Lasa
1526 );
1527 if (EFI_ERROR (Status)) {
1528 return Status;
1529 }
1530
1531 SetMem ((VOID *)(UINTN)Lasa, PcdGet32 (PcdTcg2FinalLogAreaLen), 0xFF);
1532
1533 //
1534 // Initialize
1535 //
1536 mTdxDxeData.FinalEventsTable[Index] = (VOID *)(UINTN)Lasa;
1537 (mTdxDxeData.FinalEventsTable[Index])->Version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
1538 (mTdxDxeData.FinalEventsTable[Index])->NumberOfEvents = 0;
1539
1540 mTdxDxeData.FinalEventLogAreaStruct[Index].EventLogFormat = LogFormat;
1541 mTdxDxeData.FinalEventLogAreaStruct[Index].Lasa = Lasa + sizeof (EFI_CC_FINAL_EVENTS_TABLE);
1542 mTdxDxeData.FinalEventLogAreaStruct[Index].Laml = PcdGet32 (PcdTcg2FinalLogAreaLen) - sizeof (EFI_CC_FINAL_EVENTS_TABLE);
1543 mTdxDxeData.FinalEventLogAreaStruct[Index].EventLogSize = 0;
1544 mTdxDxeData.FinalEventLogAreaStruct[Index].LastEvent = (VOID *)(UINTN)mTdxDxeData.FinalEventLogAreaStruct[Index].Lasa;
1545 mTdxDxeData.FinalEventLogAreaStruct[Index].EventLogStarted = FALSE;
1546 mTdxDxeData.FinalEventLogAreaStruct[Index].EventLogTruncated = FALSE;
1547 mTdxDxeData.FinalEventLogAreaStruct[Index].Next800155EventOffset = 0;
1548
1549 //
1550 // Install to configuration table for EFI_CC_EVENT_LOG_FORMAT_TCG_2
1551 //
1552 Status = gBS->InstallConfigurationTable (&gEfiCcFinalEventsTableGuid, (VOID *)mTdxDxeData.FinalEventsTable[Index]);
1553 if (EFI_ERROR (Status)) {
1554 return Status;
1555 }
1556
1557 return Status;
1558}
1559
1572 IN UINT32 MrIndex,
1573 IN CHAR8 *String
1574 )
1575{
1576 CC_EVENT_HDR CcEvent;
1577
1578 CcEvent.MrIndex = MrIndex;
1579 CcEvent.EventType = EV_EFI_ACTION;
1580 CcEvent.EventSize = (UINT32)AsciiStrLen (String);
1582 0,
1583 (UINT8 *)String,
1584 CcEvent.EventSize,
1585 &CcEvent,
1586 (UINT8 *)String
1587 );
1588}
1589
1599 VOID
1600 )
1601{
1602 EFI_STATUS Status;
1603 CC_EVENT_HDR CcEvent;
1604 EFI_HANDOFF_TABLE_POINTERS HandoffTables;
1605 UINTN ProcessorNum;
1606 EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;
1607
1608 ProcessorLocBuf = NULL;
1609 Status = EFI_SUCCESS;
1610
1611 if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_SERVER) {
1612 //
1613 // Tcg Server spec.
1614 // Measure each processor EFI_CPU_PHYSICAL_LOCATION with EV_TABLE_OF_DEVICES to PCR[1]
1615 //
1616 Status = GetProcessorsCpuLocation (&ProcessorLocBuf, &ProcessorNum);
1617
1618 if (!EFI_ERROR (Status)) {
1619 CcEvent.MrIndex = TdxMeasurementMapPcrToMrIndex (1);
1620 CcEvent.EventType = EV_TABLE_OF_DEVICES;
1621 CcEvent.EventSize = sizeof (HandoffTables);
1622
1623 HandoffTables.NumberOfTables = 1;
1624 HandoffTables.TableEntry[0].VendorGuid = gEfiMpServiceProtocolGuid;
1625 HandoffTables.TableEntry[0].VendorTable = ProcessorLocBuf;
1626
1627 Status = TdxDxeHashLogExtendEvent (
1628 0,
1629 (UINT8 *)(UINTN)ProcessorLocBuf,
1630 sizeof (EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,
1631 &CcEvent,
1632 (UINT8 *)&HandoffTables
1633 );
1634
1635 FreePool (ProcessorLocBuf);
1636 }
1637 }
1638
1639 return Status;
1640}
1641
1653 IN UINT32 MrIndex
1654 )
1655{
1656 CC_EVENT_HDR CcEvent;
1657 UINT32 EventData;
1658
1659 DEBUG ((DEBUG_INFO, "MeasureSeparatorEvent to Rtmr - %d\n", MrIndex));
1660
1661 EventData = 0;
1662 CcEvent.MrIndex = MrIndex;
1663 CcEvent.EventType = EV_SEPARATOR;
1664 CcEvent.EventSize = (UINT32)sizeof (EventData);
1665
1667 0,
1668 (UINT8 *)&EventData,
1669 sizeof (EventData),
1670 &CcEvent,
1671 (UINT8 *)&EventData
1672 );
1673}
1674
1692 IN UINT32 MrIndex,
1693 IN TCG_EVENTTYPE EventType,
1694 IN CHAR16 *VarName,
1695 IN EFI_GUID *VendorGuid,
1696 IN VOID *VarData,
1697 IN UINTN VarSize
1698 )
1699{
1700 EFI_STATUS Status;
1701 CC_EVENT_HDR CcEvent;
1702 UINTN VarNameLength;
1703 UEFI_VARIABLE_DATA *VarLog;
1704
1705 DEBUG ((DEBUG_INFO, "TdTcg2Dxe: MeasureVariable (Rtmr - %x, EventType - %x, ", (UINTN)MrIndex, (UINTN)EventType));
1706 DEBUG ((DEBUG_INFO, "VariableName - %s, VendorGuid - %g)\n", VarName, VendorGuid));
1707
1708 VarNameLength = StrLen (VarName);
1709 CcEvent.MrIndex = MrIndex;
1710 CcEvent.EventType = EventType;
1711
1712 CcEvent.EventSize = (UINT32)(sizeof (*VarLog) + VarNameLength * sizeof (*VarName) + VarSize
1713 - sizeof (VarLog->UnicodeName) - sizeof (VarLog->VariableData));
1714
1715 VarLog = (UEFI_VARIABLE_DATA *)AllocatePool (CcEvent.EventSize);
1716 if (VarLog == NULL) {
1717 return EFI_OUT_OF_RESOURCES;
1718 }
1719
1720 VarLog->VariableName = *VendorGuid;
1721 VarLog->UnicodeNameLength = VarNameLength;
1722 VarLog->VariableDataLength = VarSize;
1723 CopyMem (
1724 VarLog->UnicodeName,
1725 VarName,
1726 VarNameLength * sizeof (*VarName)
1727 );
1728 if ((VarSize != 0) && (VarData != NULL)) {
1729 CopyMem (
1730 (CHAR16 *)VarLog->UnicodeName + VarNameLength,
1731 VarData,
1732 VarSize
1733 );
1734 }
1735
1736 if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {
1737 //
1738 // Digest is the event data (UEFI_VARIABLE_DATA)
1739 //
1740 Status = TdxDxeHashLogExtendEvent (
1741 0,
1742 (UINT8 *)VarLog,
1743 CcEvent.EventSize,
1744 &CcEvent,
1745 (UINT8 *)VarLog
1746 );
1747 } else {
1748 ASSERT (VarData != NULL);
1749 Status = TdxDxeHashLogExtendEvent (
1750 0,
1751 (UINT8 *)VarData,
1752 VarSize,
1753 &CcEvent,
1754 (UINT8 *)VarLog
1755 );
1756 }
1757
1758 FreePool (VarLog);
1759 return Status;
1760}
1761
1779 IN UINT32 MrIndex,
1780 IN TCG_EVENTTYPE EventType,
1781 IN CHAR16 *VarName,
1782 IN EFI_GUID *VendorGuid,
1783 OUT UINTN *VarSize,
1784 OUT VOID **VarData
1785 )
1786{
1787 EFI_STATUS Status;
1788
1789 Status = GetVariable2 (VarName, VendorGuid, VarData, VarSize);
1790 if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {
1791 if (EFI_ERROR (Status)) {
1792 //
1793 // It is valid case, so we need handle it.
1794 //
1795 *VarData = NULL;
1796 *VarSize = 0;
1797 }
1798 } else {
1799 //
1800 // if status error, VarData is freed and set NULL by GetVariable2
1801 //
1802 if (EFI_ERROR (Status)) {
1803 return EFI_NOT_FOUND;
1804 }
1805 }
1806
1807 Status = MeasureVariable (
1808 MrIndex,
1809 EventType,
1810 VarName,
1811 VendorGuid,
1812 *VarData,
1813 *VarSize
1814 );
1815 return Status;
1816}
1817
1834 IN CHAR16 *VarName,
1835 IN EFI_GUID *VendorGuid,
1836 OUT UINTN *VarSize,
1837 OUT VOID **VarData
1838 )
1839{
1840 return ReadAndMeasureVariable (
1842 EV_EFI_VARIABLE_BOOT,
1843 VarName,
1844 VendorGuid,
1845 VarSize,
1846 VarData
1847 );
1848}
1849
1865 IN CHAR16 *VarName,
1866 IN EFI_GUID *VendorGuid,
1867 OUT UINTN *VarSize,
1868 OUT VOID **VarData
1869 )
1870{
1871 return ReadAndMeasureVariable (
1873 EV_EFI_VARIABLE_DRIVER_CONFIG,
1874 VarName,
1875 VendorGuid,
1876 VarSize,
1877 VarData
1878 );
1879}
1880
1893 VOID
1894 )
1895{
1896 EFI_STATUS Status;
1897 UINT16 *BootOrder;
1898 UINTN BootCount;
1899 UINTN Index;
1900 VOID *BootVarData;
1901 UINTN Size;
1902
1904 mBootVarName,
1905 &gEfiGlobalVariableGuid,
1906 &BootCount,
1907 (VOID **)&BootOrder
1908 );
1909 if ((Status == EFI_NOT_FOUND) || (BootOrder == NULL)) {
1910 return EFI_SUCCESS;
1911 }
1912
1913 if (EFI_ERROR (Status)) {
1914 //
1915 // BootOrder can't be NULL if status is not EFI_NOT_FOUND
1916 //
1917 FreePool (BootOrder);
1918 return Status;
1919 }
1920
1921 BootCount /= sizeof (*BootOrder);
1922 for (Index = 0; Index < BootCount; Index++) {
1923 UnicodeSPrint (mBootVarName, sizeof (mBootVarName), L"Boot%04x", BootOrder[Index]);
1925 mBootVarName,
1926 &gEfiGlobalVariableGuid,
1927 &Size,
1928 &BootVarData
1929 );
1930 if (!EFI_ERROR (Status)) {
1931 FreePool (BootVarData);
1932 }
1933 }
1934
1935 FreePool (BootOrder);
1936 return EFI_SUCCESS;
1937}
1938
1951 VOID
1952 )
1953{
1954 EFI_STATUS Status;
1955 VOID *Data;
1956 UINTN DataSize;
1957 UINTN Index;
1958
1959 Status = EFI_NOT_FOUND;
1960 for (Index = 0; Index < sizeof (mVariableType)/sizeof (mVariableType[0]); Index++) {
1962 mVariableType[Index].VariableName,
1963 mVariableType[Index].VendorGuid,
1964 &DataSize,
1965 &Data
1966 );
1967 if (!EFI_ERROR (Status)) {
1968 if (Data != NULL) {
1969 FreePool (Data);
1970 }
1971 }
1972 }
1973
1974 //
1975 // Measure DBT if present and not empty
1976 //
1977 Status = GetVariable2 (EFI_IMAGE_SECURITY_DATABASE2, &gEfiImageSecurityDatabaseGuid, &Data, &DataSize);
1978 if (!EFI_ERROR (Status)) {
1979 Status = MeasureVariable (
1981 EV_EFI_VARIABLE_DRIVER_CONFIG,
1983 &gEfiImageSecurityDatabaseGuid,
1984 Data,
1985 DataSize
1986 );
1987 FreePool (Data);
1988 } else {
1989 DEBUG ((DEBUG_INFO, "Skip measuring variable %s since it's deleted\n", EFI_IMAGE_SECURITY_DATABASE2));
1990 }
1991
1992 return EFI_SUCCESS;
1993}
1994
2005 VOID
2006 )
2007{
2008 CC_EVENT_HDR CcEvent;
2009
2010 CcEvent.MrIndex = TdxMeasurementMapPcrToMrIndex (7);
2011 CcEvent.EventType = EV_EFI_ACTION;
2012 CcEvent.EventSize = sizeof (FIRMWARE_DEBUGGER_EVENT_STRING) - 1;
2014 0,
2015 (UINT8 *)FIRMWARE_DEBUGGER_EVENT_STRING,
2016 sizeof (FIRMWARE_DEBUGGER_EVENT_STRING) - 1,
2017 &CcEvent,
2018 (UINT8 *)FIRMWARE_DEBUGGER_EVENT_STRING
2019 );
2020}
2021
2040VOID
2041EFIAPI
2043 IN EFI_EVENT Event,
2044 IN VOID *Context
2045 )
2046{
2047 EFI_STATUS Status;
2048 VOID *Protocol;
2049
2050 Status = gBS->LocateProtocol (&gEfiVariableWriteArchProtocolGuid, NULL, (VOID **)&Protocol);
2051 if (EFI_ERROR (Status)) {
2052 return;
2053 }
2054
2055 if (PcdGetBool (PcdFirmwareDebuggerInitialized)) {
2057 DEBUG ((DEBUG_INFO, "MeasureLaunchOfFirmwareDebugger - %r\n", Status));
2058 }
2059
2060 Status = MeasureAllSecureVariables ();
2061 DEBUG ((DEBUG_INFO, "MeasureAllSecureVariables - %r\n", Status));
2062
2063 //
2064 // We need measure Separator(7) here, because this event must be between SecureBootPolicy (Configure)
2065 // and ImageVerification (Authority)
2066 // There might be a case that we need measure UEFI image from DriverOrder, besides BootOrder. So
2067 // the Authority measurement happen before ReadToBoot event.
2068 //
2070 DEBUG ((DEBUG_INFO, "MeasureSeparatorEvent - %r\n", Status));
2071 return;
2072}
2073
2083VOID
2084EFIAPI
2086 IN EFI_EVENT Event,
2087 IN VOID *Context
2088 )
2089{
2090 EFI_STATUS Status;
2091
2092 PERF_START_EX (mImageHandle, "EventRec", "TdTcg2Dxe", 0, PERF_ID_CC_TCG2_DXE);
2093 if (mBootAttempts == 0) {
2094 //
2095 // Measure handoff tables.
2096 //
2097 Status = MeasureHandoffTables ();
2098 if (EFI_ERROR (Status)) {
2099 DEBUG ((DEBUG_ERROR, "HOBs not Measured. Error!\n"));
2100 }
2101
2102 //
2103 // Measure BootOrder & Boot#### variables.
2104 //
2105 Status = MeasureAllBootVariables ();
2106 if (EFI_ERROR (Status)) {
2107 DEBUG ((DEBUG_ERROR, "Boot Variables not Measured. Error!\n"));
2108 }
2109
2110 //
2111 // 1. This is the first boot attempt.
2112 //
2113 Status = TdMeasureAction (
2115 EFI_CALLING_EFI_APPLICATION
2116 );
2117 if (EFI_ERROR (Status)) {
2118 DEBUG ((DEBUG_ERROR, "%a not Measured. Error!\n", EFI_CALLING_EFI_APPLICATION));
2119 }
2120
2121 //
2122 // 2. Draw a line between pre-boot env and entering post-boot env.
2123 //
2124 // According to UEFI Spec 2.10 Section 38.4.1 the mapping between MrIndex and Intel
2125 // TDX Measurement Register is:
2126 // MrIndex 0 <--> MRTD
2127 // MrIndex 1-3 <--> RTMR[0-2]
2128 // RTMR[0] (i.e. MrIndex 1) is already done. So SepartorEvent shall be extended to
2129 // RTMR[1] (i.e. MrIndex 2) as well.
2130 //
2131 Status = MeasureSeparatorEvent (CC_MR_INDEX_2_RTMR1);
2132 if (EFI_ERROR (Status)) {
2133 DEBUG ((DEBUG_ERROR, "Separator Event not Measured to RTMR[1]. Error!\n"));
2134 }
2135
2136 //
2137 // 3. Measure GPT. It would be done in SAP driver.
2138 //
2139
2140 //
2141 // 4. Measure PE/COFF OS loader. It would be done in SAP driver.
2142 //
2143
2144 //
2145 // 5. Read & Measure variable. BootOrder already measured.
2146 //
2147 } else {
2148 //
2149 // 6. Not first attempt, meaning a return from last attempt
2150 //
2151 Status = TdMeasureAction (
2153 EFI_RETURNING_FROM_EFI_APPLICATION
2154 );
2155 if (EFI_ERROR (Status)) {
2156 DEBUG ((DEBUG_ERROR, "%a not Measured. Error!\n", EFI_RETURNING_FROM_EFI_APPLICATION));
2157 }
2158
2159 //
2160 // 7. Next boot attempt, measure "Calling EFI Application from Boot Option" again
2161 // TCG PC Client PFP spec Section 2.4.4.5 Step 4
2162 //
2163 Status = TdMeasureAction (
2165 EFI_CALLING_EFI_APPLICATION
2166 );
2167 if (EFI_ERROR (Status)) {
2168 DEBUG ((DEBUG_ERROR, "%a not Measured. Error!\n", EFI_CALLING_EFI_APPLICATION));
2169 }
2170 }
2171
2172 DEBUG ((DEBUG_INFO, "TdTcg2Dxe Measure Data when ReadyToBoot\n"));
2173 //
2174 // Increase boot attempt counter.
2175 //
2176 mBootAttempts++;
2177 PERF_END_EX (mImageHandle, "EventRec", "Tcg2Dxe", 0, PERF_ID_CC_TCG2_DXE + 1);
2178}
2179
2189VOID
2190EFIAPI
2192 IN EFI_EVENT Event,
2193 IN VOID *Context
2194 )
2195{
2196 EFI_STATUS Status;
2197
2198 //
2199 // Measure invocation of ExitBootServices,
2200 //
2201 Status = TdMeasureAction (
2203 EFI_EXIT_BOOT_SERVICES_INVOCATION
2204 );
2205 if (EFI_ERROR (Status)) {
2206 DEBUG ((DEBUG_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_INVOCATION));
2207 }
2208
2209 //
2210 // Measure success of ExitBootServices
2211 //
2212 Status = TdMeasureAction (
2214 EFI_EXIT_BOOT_SERVICES_SUCCEEDED
2215 );
2216 if (EFI_ERROR (Status)) {
2217 DEBUG ((DEBUG_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_SUCCEEDED));
2218 }
2219}
2220
2230VOID
2231EFIAPI
2233 IN EFI_EVENT Event,
2234 IN VOID *Context
2235 )
2236{
2237 EFI_STATUS Status;
2238
2239 //
2240 // Measure Failure of ExitBootServices,
2241 //
2242 Status = TdMeasureAction (
2244 EFI_EXIT_BOOT_SERVICES_FAILED
2245 );
2246 if (EFI_ERROR (Status)) {
2247 DEBUG ((DEBUG_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_FAILED));
2248 }
2249}
2250
2252SyncCcEvent (
2253 VOID
2254 )
2255{
2256 EFI_STATUS Status;
2257 EFI_PEI_HOB_POINTERS GuidHob;
2258 VOID *CcEvent;
2259 VOID *DigestListBin;
2260 UINT32 DigestListBinSize;
2261 UINT8 *Event;
2262 UINT32 EventSize;
2263 EFI_CC_EVENT_LOG_FORMAT LogFormat;
2264
2265 DEBUG ((DEBUG_INFO, "Sync Cc event from SEC\n"));
2266
2267 Status = EFI_SUCCESS;
2268 LogFormat = EFI_CC_EVENT_LOG_FORMAT_TCG_2;
2269 GuidHob.Guid = GetFirstGuidHob (&gCcEventEntryHobGuid);
2270
2271 while (!EFI_ERROR (Status) && GuidHob.Guid != NULL) {
2272 CcEvent = AllocateCopyPool (GET_GUID_HOB_DATA_SIZE (GuidHob.Guid), GET_GUID_HOB_DATA (GuidHob.Guid));
2273 if (CcEvent == NULL) {
2274 return EFI_OUT_OF_RESOURCES;
2275 }
2276
2277 GuidHob.Guid = GET_NEXT_HOB (GuidHob);
2278 GuidHob.Guid = GetNextGuidHob (&gCcEventEntryHobGuid, GuidHob.Guid);
2279
2280 DigestListBin = (UINT8 *)CcEvent + sizeof (UINT32) + sizeof (TCG_EVENTTYPE);
2281 DigestListBinSize = GetDigestListBinSize (DigestListBin);
2282
2283 //
2284 // Event size.
2285 //
2286 EventSize = *(UINT32 *)((UINT8 *)DigestListBin + DigestListBinSize);
2287 Event = (UINT8 *)DigestListBin + DigestListBinSize + sizeof (UINT32);
2288
2289 //
2290 // Log the event
2291 //
2292 Status = TdxDxeLogEvent (
2293 LogFormat,
2294 CcEvent,
2295 sizeof (UINT32) + sizeof (TCG_EVENTTYPE) + DigestListBinSize + sizeof (UINT32),
2296 Event,
2297 EventSize
2298 );
2299
2300 DumpCcEvent ((CC_EVENT *)CcEvent);
2301 FreePool (CcEvent);
2302 }
2303
2304 return Status;
2305}
2306
2313VOID
2314EFIAPI
2316 IN EFI_EVENT Event,
2317 IN VOID *Context
2318 )
2319{
2320 UINTN TableKey;
2321 EFI_STATUS Status;
2322 EFI_ACPI_TABLE_PROTOCOL *AcpiTable;
2323
2324 Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **)&AcpiTable);
2325 if (EFI_ERROR (Status)) {
2326 DEBUG ((DEBUG_ERROR, "TD: AcpiTableProtocol is not installed. %r\n", Status));
2327 return;
2328 }
2329
2330 mTdxEventlogAcpiTemplate.Laml = (UINT64)PcdGet32 (PcdCcEventlogAcpiTableLaml);
2331 mTdxEventlogAcpiTemplate.Lasa = PcdGet64 (PcdCcEventlogAcpiTableLasa);
2332 CopyMem (mTdxEventlogAcpiTemplate.Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (mTdxEventlogAcpiTemplate.Header.OemId));
2333 mTdxEventlogAcpiTemplate.Header.OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);
2334 mTdxEventlogAcpiTemplate.Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);
2335 mTdxEventlogAcpiTemplate.Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);
2336 mTdxEventlogAcpiTemplate.Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
2337
2338 //
2339 // Construct ACPI Table
2340 Status = AcpiTable->InstallAcpiTable (
2341 AcpiTable,
2342 &mTdxEventlogAcpiTemplate,
2343 mTdxEventlogAcpiTemplate.Header.Length,
2344 &TableKey
2345 );
2346 ASSERT_EFI_ERROR (Status);
2347
2348 DEBUG ((DEBUG_INFO, "TDVF Eventlog ACPI Table is installed.\n"));
2349}
2350
2359 VOID
2360 )
2361{
2362 EFI_STATUS Status;
2363 EFI_HANDLE Handle;
2364
2365 Handle = NULL;
2366 Status = gBS->InstallMultipleProtocolInterfaces (
2367 &Handle,
2368 &gEfiCcMeasurementProtocolGuid,
2369 &mTdProtocol,
2370 NULL
2371 );
2372 DEBUG ((DEBUG_INFO, "CcProtocol: Install %r\n", Status));
2373 return Status;
2374}
2375
2386EFIAPI
2388 IN EFI_HANDLE ImageHandle,
2389 IN EFI_SYSTEM_TABLE *SystemTable
2390 )
2391{
2392 EFI_STATUS Status;
2393 EFI_EVENT Event;
2394 VOID *Registration;
2395
2396 if (!TdIsEnabled ()) {
2397 return EFI_UNSUPPORTED;
2398 }
2399
2400 mImageHandle = ImageHandle;
2401
2402 //
2403 // Fill information
2404 //
2405 // ASSERT (TD_EVENT_LOG_AREA_COUNT_MAX == sizeof(mTEventInfo)/sizeof(mTcg2EventInfo[0]));
2406
2407 mTdxDxeData.BsCap.Size = sizeof (EFI_CC_BOOT_SERVICE_CAPABILITY);
2408 mTdxDxeData.BsCap.ProtocolVersion.Major = 1;
2409 mTdxDxeData.BsCap.ProtocolVersion.Minor = 0;
2410 mTdxDxeData.BsCap.StructureVersion.Major = 1;
2411 mTdxDxeData.BsCap.StructureVersion.Minor = 0;
2412
2413 //
2414 // Get supported PCR and current Active PCRs
2415 // For TD gueset HA384 is supported.
2416 //
2417 mTdxDxeData.BsCap.HashAlgorithmBitmap = HASH_ALG_SHA384;
2418
2419 // TD guest only supports EFI_TCG2_EVENT_LOG_FORMAT_TCG_2
2420 mTdxDxeData.BsCap.SupportedEventLogs = EFI_CC_EVENT_LOG_FORMAT_TCG_2;
2421
2422 //
2423 // Setup the log area and copy event log from hob list to it
2424 //
2425 Status = SetupCcEventLog ();
2426 ASSERT_EFI_ERROR (Status);
2427
2428 if (!EFI_ERROR (Status)) {
2429 Status = SyncCcEvent ();
2430 ASSERT_EFI_ERROR (Status);
2431 }
2432
2433 //
2434 // Measure handoff tables, Boot#### variables etc.
2435 //
2437 TPL_CALLBACK,
2439 NULL,
2440 &Event
2441 );
2442
2443 Status = gBS->CreateEventEx (
2444 EVT_NOTIFY_SIGNAL,
2445 TPL_NOTIFY,
2447 NULL,
2448 &gEfiEventExitBootServicesGuid,
2449 &Event
2450 );
2451
2452 //
2453 // Measure Exit Boot Service failed
2454 //
2455 Status = gBS->CreateEventEx (
2456 EVT_NOTIFY_SIGNAL,
2457 TPL_NOTIFY,
2459 NULL,
2460 &gEventExitBootServicesFailedGuid,
2461 &Event
2462 );
2463
2464 //
2465 // Create event callback, because we need access variable on SecureBootPolicyVariable
2466 // We should use VariableWriteArch instead of VariableArch, because Variable driver
2467 // may update SecureBoot value based on last setting.
2468 //
2469 EfiCreateProtocolNotifyEvent (&gEfiVariableWriteArchProtocolGuid, TPL_CALLBACK, MeasureSecureBootPolicy, NULL, &Registration);
2470
2471 //
2472 // Install CcMeasurementProtocol
2473 //
2474 Status = InstallCcMeasurementProtocol ();
2475 DEBUG ((DEBUG_INFO, "InstallCcMeasurementProtocol - %r\n", Status));
2476
2477 if (Status == EFI_SUCCESS) {
2478 //
2479 // Create event callback to install CC EventLog ACPI Table
2480 EfiCreateProtocolNotifyEvent (&gEfiAcpiTableProtocolGuid, TPL_CALLBACK, InstallAcpiTable, NULL, &Registration);
2481 } else {
2482 //
2483 // Cc measurement feature is crucial to a td-guest and it shall stop running immediately
2484 // when it is failed to be installed.
2485 DEBUG ((DEBUG_ERROR, "%a: CcMeasurement protocol failed to be installed - %r\n", __func__, Status));
2486 CpuDeadLoop ();
2487 }
2488
2489 return Status;
2490}
UINT64 UINTN
#define MAX_ADDRESS
VOID *EFIAPI GetFirstGuidHob(IN CONST EFI_GUID *Guid)
Definition: HobLib.c:215
VOID *EFIAPI GetNextGuidHob(IN CONST EFI_GUID *Guid, IN CONST VOID *HobStart)
Definition: HobLib.c:176
#define SHA384_DIGEST_SIZE
Definition: BaseCryptLib.h:49
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
Definition: String.c:641
UINT32 EFIAPI WriteUnaligned32(OUT VOID *Buffer, IN UINT32 Value)
Definition: Unaligned.c:166
VOID EFIAPI CpuDeadLoop(VOID)
Definition: CpuDeadLoop.c:25
UINT32 EFIAPI ReadUnaligned32(IN CONST VOID *Buffer)
Definition: Unaligned.c:140
UINT16 EFIAPI ReadUnaligned16(IN CONST VOID *Buffer)
Definition: Unaligned.c:36
UINTN EFIAPI StrLen(IN CONST CHAR16 *String)
Definition: String.c:30
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI SetMem(OUT VOID *Buffer, IN UINTN Length, IN UINT8 Value)
Definition: SetMemWrapper.c:38
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID EFIAPI FreePool(IN VOID *Buffer)
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
#define EFI_KEY_EXCHANGE_KEY_NAME
#define EFI_PLATFORM_KEY_NAME
#define EFI_SECURE_BOOT_MODE_NAME
EFI_STATUS EFIAPI HashAndExtend(IN TPMI_DH_PCR PcrIndex, IN VOID *DataToHash, IN UINTN DataToHashLen, OUT TPML_DIGEST_VALUES *DigestList)
Definition: HashLibTdx.c:150
#define EFI_IMAGE_SECURITY_DATABASE2
#define EFI_IMAGE_SECURITY_DATABASE1
#define EFI_IMAGE_SECURITY_DATABASE
UINTN EFIAPI UnicodeSPrint(OUT CHAR16 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR16 *FormatString,...)
Definition: PrintLib.c:408
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define VOID
Definition: Base.h:269
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define ARRAY_SIZE(Array)
Definition: Base.h:1393
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:463
#define DEBUG(Expression)
Definition: DebugLib.h:435
BOOLEAN EFIAPI TdIsEnabled()
Definition: IntelTdxNull.c:79
#define PcdGet64(TokenName)
Definition: PcdLib.h:375
#define PcdGet8(TokenName)
Definition: PcdLib.h:336
#define PcdGet32(TokenName)
Definition: PcdLib.h:362
#define PcdGetBool(TokenName)
Definition: PcdLib.h:401
#define PcdSet64S(TokenName, Value)
Definition: PcdLib.h:511
#define PcdSet32S(TokenName, Value)
Definition: PcdLib.h:497
#define PcdGetPtr(TokenName)
Definition: PcdLib.h:388
#define PERF_START_EX(Handle, Token, Module, TimeStamp, Identifier)
#define PERF_END_EX(Handle, Token, Module, TimeStamp, Identifier)
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
VOID EFIAPI OnExitBootServicesFailed(IN EFI_EVENT Event, IN VOID *Context)
Definition: TdTcg2Dxe.c:2232
VOID InternalDumpData(IN UINT8 *Data, IN UINTN Size)
Definition: TdTcg2Dxe.c:270
UINTN GetTcgEfiSpecIdEventStructSize(IN TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct)
Definition: TdTcg2Dxe.c:564
VOID * CopyDigestListBinToBuffer(IN OUT VOID *Buffer, IN VOID *DigestListBin, IN UINT32 HashAlgorithmMask, OUT UINT32 *HashAlgorithmMaskCopied)
Definition: TdTcg2Dxe.c:1100
EFI_STATUS MeasureLaunchOfFirmwareDebugger(VOID)
Definition: TdTcg2Dxe.c:2004
EFI_STATUS EFIAPI TdGetEventLog(IN EFI_CC_MEASUREMENT_PROTOCOL *This, IN EFI_CC_EVENT_LOG_FORMAT EventLogFormat, OUT EFI_PHYSICAL_ADDRESS *EventLogLocation, OUT EFI_PHYSICAL_ADDRESS *EventLogLastEntry, OUT BOOLEAN *EventLogTruncated)
Definition: TdTcg2Dxe.c:748
EFI_STATUS MeasureAllSecureVariables(VOID)
Definition: TdTcg2Dxe.c:1950
VOID InternalDumpHex(IN UINT8 *Data, IN UINTN Size)
Definition: TdTcg2Dxe.c:291
EFI_STATUS TcgCommLogEvent(IN OUT CC_EVENT_LOG_AREA_STRUCT *EventLogAreaStruct, IN VOID *NewEventHdr, IN UINT32 NewEventHdrSize, IN UINT8 *NewEventData, IN UINT32 NewEventSize)
Definition: TdTcg2Dxe.c:859
VOID DumpTcgEfiSpecIdEventStruct(IN TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct)
Definition: TdTcg2Dxe.c:516
EFI_STATUS TdxDxeLogHashEvent(IN TPML_DIGEST_VALUES *DigestList, IN OUT CC_EVENT_HDR *NewEventHdr, IN UINT8 *NewEventData)
Definition: TdTcg2Dxe.c:1160
EFI_STATUS GetProcessorsCpuLocation(OUT EFI_CPU_PHYSICAL_LOCATION **LocationBuf, OUT UINTN *Num)
Definition: TdTcg2Dxe.c:376
VOID DumpCcEvent(IN CC_EVENT *CcEvent)
Definition: TdTcg2Dxe.c:585
VOID EFIAPI MeasureSecureBootPolicy(IN EFI_EVENT Event, IN VOID *Context)
Definition: TdTcg2Dxe.c:2042
UINT32 GetDigestListBinSize(IN VOID *DigestListBin)
Definition: TdTcg2Dxe.c:1063
EFI_STATUS MeasureVariable(IN UINT32 MrIndex, IN TCG_EVENTTYPE EventType, IN CHAR16 *VarName, IN EFI_GUID *VendorGuid, IN VOID *VarData, IN UINTN VarSize)
Definition: TdTcg2Dxe.c:1691
EFI_STATUS EFIAPI TdHashLogExtendEvent(IN EFI_CC_MEASUREMENT_PROTOCOL *This, IN UINT64 Flags, IN EFI_PHYSICAL_ADDRESS DataToHash, IN UINT64 DataToHashLen, IN EFI_CC_EVENT *CcEvent)
Definition: TdTcg2Dxe.c:1290
VOID EFIAPI OnReadyToBoot(IN EFI_EVENT Event, IN VOID *Context)
Definition: TdTcg2Dxe.c:2085
VOID InitNoActionEvent(IN OUT CC_EVENT_HDR *NoActionEvent, IN UINT32 EventSize)
Definition: TdTcg2Dxe.c:326
EFI_STATUS MeasurePeImageAndExtend(IN UINT32 RtmrIndex, IN EFI_PHYSICAL_ADDRESS ImageAddress, IN UINTN ImageSize, OUT TPML_DIGEST_VALUES *DigestList)
VOID EFIAPI OnExitBootServices(IN EFI_EVENT Event, IN VOID *Context)
Definition: TdTcg2Dxe.c:2191
STATIC BOOLEAN Is800155Event(IN VOID *NewEventHdr, IN UINT32 NewEventHdrSize, IN UINT8 *NewEventData, IN UINT32 NewEventSize)
Definition: TdTcg2Dxe.c:811
VOID DumpPcrEvent(IN TCG_PCR_EVENT_HDR *EventHdr)
Definition: TdTcg2Dxe.c:491
EFI_STATUS ReadAndMeasureBootVariable(IN CHAR16 *VarName, IN EFI_GUID *VendorGuid, OUT UINTN *VarSize, OUT VOID **VarData)
Definition: TdTcg2Dxe.c:1833
EFI_STATUS MeasureSeparatorEvent(IN UINT32 MrIndex)
Definition: TdTcg2Dxe.c:1652
VOID EFIAPI InstallAcpiTable(IN EFI_EVENT Event, IN VOID *Context)
Definition: TdTcg2Dxe.c:2315
EFI_STATUS InstallCcMeasurementProtocol(VOID)
Definition: TdTcg2Dxe.c:2358
VOID DumpCcEventLog(IN EFI_CC_EVENT_LOG_FORMAT EventLogFormat, IN EFI_PHYSICAL_ADDRESS EventLogLocation, IN EFI_PHYSICAL_ADDRESS EventLogLastEntry, IN EFI_CC_FINAL_EVENTS_TABLE *FinalEventsTable)
Definition: TdTcg2Dxe.c:677
EFI_STATUS TdxDxeLogEvent(IN EFI_CC_EVENT_LOG_FORMAT EventLogFormat, IN VOID *NewEventHdr, IN UINT32 NewEventHdrSize, IN UINT8 *NewEventData, IN UINT32 NewEventSize)
Definition: TdTcg2Dxe.c:971
EFI_STATUS TdMeasureAction(IN UINT32 MrIndex, IN CHAR8 *String)
Definition: TdTcg2Dxe.c:1571
EFI_STATUS TdxDxeHashLogExtendEvent(IN UINT64 Flags, IN UINT8 *HashData, IN UINT64 HashDataLen, IN OUT CC_EVENT_HDR *NewEventHdr, IN UINT8 *NewEventData)
Definition: TdTcg2Dxe.c:1221
EFI_STATUS MeasureHandoffTables(VOID)
Definition: TdTcg2Dxe.c:1598
EFI_STATUS ReadAndMeasureSecureVariable(IN CHAR16 *VarName, IN EFI_GUID *VendorGuid, OUT UINTN *VarSize, OUT VOID **VarData)
Definition: TdTcg2Dxe.c:1864
EFI_STATUS ReadAndMeasureVariable(IN UINT32 MrIndex, IN TCG_EVENTTYPE EventType, IN CHAR16 *VarName, IN EFI_GUID *VendorGuid, OUT UINTN *VarSize, OUT VOID **VarData)
Definition: TdTcg2Dxe.c:1778
EFI_STATUS MeasureAllBootVariables(VOID)
Definition: TdTcg2Dxe.c:1892
EFI_STATUS EFIAPI TdGetCapability(IN EFI_CC_MEASUREMENT_PROTOCOL *This, IN OUT EFI_CC_BOOT_SERVICE_CAPABILITY *ProtocolCapability)
Definition: TdTcg2Dxe.c:466
EFI_STATUS EFIAPI DriverEntry(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: TdTcg2Dxe.c:2387
UINT32 GetHashMaskFromAlgo(IN TPMI_ALG_HASH HashAlgo)
Definition: TdTcg2Dxe.c:171
VOID * CopyDigestListToBuffer(IN OUT VOID *Buffer, IN TPML_DIGEST_VALUES *DigestList, IN UINT32 HashAlgorithmMask)
Definition: TdTcg2Dxe.c:197
UINTN GetCcEventSize(IN CC_EVENT *CcEvent)
Definition: TdTcg2Dxe.c:634
EFI_STATUS SetupCcEventLog(VOID)
Definition: TdTcg2Dxe.c:1385
UINT16 GetHashSizeFromAlgo(IN TPMI_ALG_HASH HashAlgo)
Definition: TdTcg2Dxe.c:148
UINT32 EFIAPI TdxMeasurementMapPcrToMrIndex(IN UINT32 PCRIndex)
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_EVENT
Definition: UefiBaseType.h:37
UINTN EFI_TPL
Definition: UefiBaseType.h:41
#define EFI_SIZE_TO_PAGES(Size)
Definition: UefiBaseType.h:200
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_BOOT_SERVICES * gBS
EFI_STATUS EFIAPI GetVariable2(IN CONST CHAR16 *Name, IN CONST EFI_GUID *Guid, OUT VOID **Value, OUT UINTN *Size OPTIONAL)
Definition: UefiLib.c:1313
EFI_STATUS EFIAPI EfiCreateEventReadyToBootEx(IN EFI_TPL NotifyTpl, IN EFI_EVENT_NOTIFY NotifyFunction OPTIONAL, IN VOID *NotifyContext OPTIONAL, OUT EFI_EVENT *ReadyToBootEvent)
Definition: UefiNotTiano.c:164
EFI_EVENT EFIAPI EfiCreateProtocolNotifyEvent(IN EFI_GUID *ProtocolGuid, IN EFI_TPL NotifyTpl, IN EFI_EVENT_NOTIFY NotifyFunction, IN VOID *NotifyContext OPTIONAL, OUT VOID **Registration)
Definition: UefiLib.c:130
@ EfiBootServicesData
@ EfiACPIMemoryNVS
@ AllocateAnyPages
Definition: UefiSpec.h:33
EFI_CPU_PHYSICAL_LOCATION Location
Definition: MpService.h:178
Definition: Base.h:213
INT8 VariableData[1]
Driver or platform-specific data.