2 This library is used by other modules to measure data to TPM.
4 Copyright (c) 2020, Intel Corporation. All rights reserved. <BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <Uefi/UefiBaseType.h>
10 #include <Pi/PiFirmwareVolume.h>
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/ReportStatusCodeLib.h>
15 #include <Library/PcdLib.h>
16 #include <Library/PrintLib.h>
17 #include <Library/TcgEventLogRecordLib.h>
18 #include <Library/TpmMeasurementLib.h>
20 #include <IndustryStandard/UefiTcgPlatform.h>
23 Get the FvName from the FV header.
25 Causion: The FV is untrusted input.
27 @param[in] FvBase Base address of FV image.
28 @param[in] FvLength Length of FV image.
30 @return FvName pointer
31 @retval NULL FvName is NOT found
34 TpmMeasurementGetFvName (
35 IN EFI_PHYSICAL_ADDRESS FvBase
,
39 EFI_FIRMWARE_VOLUME_HEADER
*FvHeader
;
40 EFI_FIRMWARE_VOLUME_EXT_HEADER
*FvExtHeader
;
42 if (FvBase
>= MAX_ADDRESS
) {
46 if (FvLength
>= MAX_ADDRESS
- FvBase
) {
50 if (FvLength
< sizeof (EFI_FIRMWARE_VOLUME_HEADER
)) {
54 FvHeader
= (EFI_FIRMWARE_VOLUME_HEADER
*)(UINTN
)FvBase
;
55 if (FvHeader
->Signature
!= EFI_FVH_SIGNATURE
) {
59 if (FvHeader
->ExtHeaderOffset
< sizeof (EFI_FIRMWARE_VOLUME_HEADER
)) {
63 if (FvHeader
->ExtHeaderOffset
+ sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER
) > FvLength
) {
67 FvExtHeader
= (EFI_FIRMWARE_VOLUME_EXT_HEADER
*)(UINTN
)(FvBase
+ FvHeader
->ExtHeaderOffset
);
69 return &FvExtHeader
->FvName
;
73 Measure a FirmwareBlob.
75 @param[in] PcrIndex PcrIndex of the measurement.
76 @param[in] Description Description for this FirmwareBlob.
77 @param[in] FirmwareBlobBase Base address of this FirmwareBlob.
78 @param[in] FirmwareBlobLength Size in bytes of this FirmwareBlob.
80 @retval EFI_SUCCESS Operation completed successfully.
81 @retval EFI_UNSUPPORTED TPM device not available.
82 @retval EFI_OUT_OF_RESOURCES Out of memory.
83 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
89 IN CHAR8
*Description OPTIONAL
,
90 IN EFI_PHYSICAL_ADDRESS FirmwareBlobBase
,
91 IN UINT64 FirmwareBlobLength
94 EFI_PLATFORM_FIRMWARE_BLOB FvBlob
;
95 PLATFORM_FIRMWARE_BLOB2_STRUCT FvBlob2
;
102 FvName
= TpmMeasurementGetFvName (FirmwareBlobBase
, FirmwareBlobLength
);
104 if (((Description
!= NULL
) || (FvName
!= NULL
)) &&
105 (PcdGet32 (PcdTcgPfpMeasurementRevision
) >= TCG_EfiSpecIDEventStruct_SPEC_ERRATA_TPM2_REV_105
))
107 if (Description
!= NULL
) {
108 AsciiSPrint ((CHAR8
*)FvBlob2
.BlobDescription
, sizeof (FvBlob2
.BlobDescription
), "%a", Description
);
110 AsciiSPrint ((CHAR8
*)FvBlob2
.BlobDescription
, sizeof (FvBlob2
.BlobDescription
), "Fv(%g)", FvName
);
113 FvBlob2
.BlobDescriptionSize
= sizeof (FvBlob2
.BlobDescription
);
114 FvBlob2
.BlobBase
= FirmwareBlobBase
;
115 FvBlob2
.BlobLength
= FirmwareBlobLength
;
117 EventType
= EV_EFI_PLATFORM_FIRMWARE_BLOB2
;
119 EventLogSize
= sizeof (FvBlob2
);
121 FvBlob
.BlobBase
= FirmwareBlobBase
;
122 FvBlob
.BlobLength
= FirmwareBlobLength
;
124 EventType
= EV_EFI_PLATFORM_FIRMWARE_BLOB
;
126 EventLogSize
= sizeof (FvBlob
);
129 Status
= TpmMeasureAndLogData (
134 (VOID
*)(UINTN
)FirmwareBlobBase
,
142 Measure a HandoffTable.
144 @param[in] PcrIndex PcrIndex of the measurement.
145 @param[in] Description Description for this HandoffTable.
146 @param[in] TableGuid GUID of this HandoffTable.
147 @param[in] TableAddress Base address of this HandoffTable.
148 @param[in] TableLength Size in bytes of this HandoffTable.
150 @retval EFI_SUCCESS Operation completed successfully.
151 @retval EFI_UNSUPPORTED TPM device not available.
152 @retval EFI_OUT_OF_RESOURCES Out of memory.
153 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
157 MeasureHandoffTable (
159 IN CHAR8
*Description OPTIONAL
,
160 IN EFI_GUID
*TableGuid
,
161 IN VOID
*TableAddress
,
165 EFI_HANDOFF_TABLE_POINTERS HandoffTables
;
166 HANDOFF_TABLE_POINTERS2_STRUCT HandoffTables2
;
172 if ((Description
!= NULL
) &&
173 (PcdGet32 (PcdTcgPfpMeasurementRevision
) >= TCG_EfiSpecIDEventStruct_SPEC_ERRATA_TPM2_REV_105
))
175 AsciiSPrint ((CHAR8
*)HandoffTables2
.TableDescription
, sizeof (HandoffTables2
.TableDescription
), "%a", Description
);
177 HandoffTables2
.TableDescriptionSize
= sizeof (HandoffTables2
.TableDescription
);
178 HandoffTables2
.NumberOfTables
= 1;
179 CopyGuid (&(HandoffTables2
.TableEntry
[0].VendorGuid
), TableGuid
);
180 HandoffTables2
.TableEntry
[0].VendorTable
= TableAddress
;
182 EventType
= EV_EFI_HANDOFF_TABLES2
;
183 EventLog
= &HandoffTables2
;
184 EventLogSize
= sizeof (HandoffTables2
);
186 HandoffTables
.NumberOfTables
= 1;
187 CopyGuid (&(HandoffTables
.TableEntry
[0].VendorGuid
), TableGuid
);
188 HandoffTables
.TableEntry
[0].VendorTable
= TableAddress
;
190 EventType
= EV_EFI_HANDOFF_TABLES
;
191 EventLog
= &HandoffTables
;
192 EventLogSize
= sizeof (HandoffTables
);
195 Status
= TpmMeasureAndLogData (