1919#include <Library/OrderedCollectionLib.h>
2020#include <IndustryStandard/Acpi.h>
2121#include <Library/TdxProbeLib.h>
22+ #include <Protocol/Tcg2Protocol.h>
23+ #include <Protocol/Tdx.h>
24+
25+ EFI_TCG2_PROTOCOL * mTdTcg2Protocol = NULL ;
2226
2327//
2428// The user structure for the ordered collection that will track the fw_cfg
@@ -35,6 +39,71 @@ typedef struct {
3539 // part of ACPI tables.
3640} BLOB ;
3741
42+ /**
43+ Mesure firmware acpi configuration data from qemu.
44+ @param[in] EventData Pointer to the event data.
45+ @param[in] EventSize Size of event data.
46+ @param[in] CfgDataBase Configuration data base address.
47+ @param[in] EventSize Size of configuration data .
48+ @retval EFI_NOT_FOUND Cannot locate protocol.
49+ @retval EFI_OUT_OF_RESOURCES Allocate zero pool failure.
50+ @return Status codes returned by
51+ mTcg2Protocol->HashLogExtendEvent.
52+ **/
53+ STATIC
54+ EFI_STATUS
55+ EFIAPI
56+ MeasureQemuFwCfgAcpi (
57+ IN CHAR8 * EventData ,
58+ IN UINT32 EventSize ,
59+ IN EFI_PHYSICAL_ADDRESS CfgDataBase ,
60+ IN UINTN CfgDataLength
61+ )
62+ {
63+ EFI_TCG2_EVENT * Tcg2Event ;
64+ EFI_STATUS Status ;
65+
66+ if (ProbeTdGuest () == FALSE) {
67+ return EFI_SUCCESS ;
68+ }
69+
70+ if (mTdTcg2Protocol == NULL ) {
71+ Status = gBS -> LocateProtocol (& gTdTcg2ProtocolGuid , NULL , (VOID * * ) & mTdTcg2Protocol );
72+ if (EFI_ERROR (Status )) {
73+ //
74+ // TdTcg2 protocol is not installed.
75+ //
76+ DEBUG ((EFI_D_ERROR , "MesureQemuFwCfgAcpi - TdTcg2 - %r\n" , Status ));
77+ return EFI_NOT_FOUND ;
78+ }
79+ }
80+
81+ Tcg2Event = AllocateZeroPool (EventSize + sizeof (EFI_TCG2_EVENT ) - sizeof (Tcg2Event -> Event ));
82+ if (Tcg2Event == NULL ) {
83+ return EFI_OUT_OF_RESOURCES ;
84+ }
85+
86+ Tcg2Event -> Size = EventSize + sizeof (EFI_TCG2_EVENT ) - sizeof (Tcg2Event -> Event );
87+ Tcg2Event -> Header .EventType = EV_PLATFORM_CONFIG_FLAGS ;
88+ Tcg2Event -> Header .PCRIndex = 1 ;
89+ Tcg2Event -> Header .HeaderSize = sizeof (EFI_TCG2_EVENT_HEADER );
90+ Tcg2Event -> Header .HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION ;
91+ CopyMem (& Tcg2Event -> Event [0 ], EventData , EventSize );
92+
93+ Status = mTdTcg2Protocol -> HashLogExtendEvent (mTdTcg2Protocol ,
94+ 0 ,
95+ CfgDataBase ,
96+ CfgDataLength ,
97+ Tcg2Event
98+ );
99+
100+ FreePool (Tcg2Event );
101+
102+ DEBUG ((DEBUG_INFO , "MeasureQemuFwCfg %s, %r\n" , EventData , Status ));
103+
104+ return Status ;
105+ }
106+
38107
39108/**
40109 Compare a standalone key against a user structure containing an embedded key.
@@ -382,6 +451,16 @@ ProcessCmdAllocate (
382451
383452 QemuFwCfgSelectItem (FwCfgItem );
384453 QemuFwCfgReadBytes (FwCfgSize , Blob -> Base );
454+
455+ Status = MeasureQemuFwCfgAcpi ((CHAR8 * ) Allocate -> File ,
456+ sizeof (Allocate -> File ),
457+ (EFI_PHYSICAL_ADDRESS ) Blob -> Base ,
458+ FwCfgSize
459+ );
460+ if (EFI_ERROR (Status )) {
461+ DEBUG ((DEBUG_ERROR , "Measure %s failure\n" , Allocate -> File ));
462+ }
463+
385464 ZeroMem (Blob -> Base + Blob -> Size , EFI_PAGES_TO_SIZE (NumPages ) - Blob -> Size );
386465
387466 DEBUG ((DEBUG_VERBOSE , "%a: File=\"%a\" Alignment=0x%x Zone=%d Size=0x%Lx "
@@ -999,6 +1078,17 @@ InstallQemuFwCfgTables (
9991078 EnablePciDecoding (& OriginalPciAttributes , & OriginalPciAttributesCount );
10001079 QemuFwCfgSelectItem (FwCfgItem );
10011080 QemuFwCfgReadBytes (FwCfgSize , LoaderStart );
1081+
1082+ Status = MeasureQemuFwCfgAcpi (
1083+ "etc/table-loader" ,
1084+ sizeof ("etc/table-loader" ),
1085+ (EFI_PHYSICAL_ADDRESS ) LoaderStart ,
1086+ FwCfgSize
1087+ );
1088+ if (EFI_ERROR (Status )) {
1089+ DEBUG ((DEBUG_ERROR , "Measure etc/table-loader failure\n" ));
1090+ }
1091+
10021092 RestorePciDecoding (OriginalPciAttributes , OriginalPciAttributesCount );
10031093 LoaderEnd = LoaderStart + FwCfgSize / sizeof * LoaderEntry ;
10041094
0 commit comments