TianoCore EDK2 master
Loading...
Searching...
No Matches
FspMeasurementLib.c
Go to the documentation of this file.
1
9#include <PiPei.h>
10#include <Uefi.h>
11
15#include <Library/PcdLib.h>
16#include <Library/PrintLib.h>
17#include <Library/DebugLib.h>
22#include <Library/HashLib.h>
23
24#include <Ppi/Tcg.h>
26
44EFIAPI
46 IN UINT32 PcrIndex,
47 IN UINT32 EventType,
48 IN VOID *EventLog,
49 IN UINT32 LogLen,
50 IN VOID *HashData,
51 IN UINT64 HashDataLen,
52 IN UINT64 Flags
53 )
54{
55 EFI_STATUS Status;
56 EDKII_TCG_PPI *TcgPpi;
57 TCG_PCR_EVENT_HDR TcgEventHdr;
58
59 Status = PeiServicesLocatePpi (
60 &gEdkiiTcgPpiGuid,
61 0,
62 NULL,
63 (VOID **)&TcgPpi
64 );
65 if (EFI_ERROR (Status)) {
66 return Status;
67 }
68
69 TcgEventHdr.PCRIndex = PcrIndex;
70 TcgEventHdr.EventType = EventType;
71 TcgEventHdr.EventSize = LogLen;
72
73 Status = TcgPpi->HashLogExtendEvent (
74 TcgPpi,
75 Flags,
76 HashData,
77 (UINTN)HashDataLen,
78 &TcgEventHdr,
79 EventLog
80 );
81 return Status;
82}
83
100EFIAPI
102 IN CHAR8 *Description OPTIONAL,
103 IN EFI_PHYSICAL_ADDRESS FirmwareBlobBase,
104 IN UINT64 FirmwareBlobLength,
105 IN UINT32 CfgRegionOffset,
106 IN UINT32 CfgRegionSize
107 )
108{
109 EFI_PLATFORM_FIRMWARE_BLOB FvBlob, UpdBlob;
110 PLATFORM_FIRMWARE_BLOB2_STRUCT FvBlob2, UpdBlob2;
111 VOID *FvName;
112 UINT32 FvEventType;
113 VOID *FvEventLog, *UpdEventLog;
114 UINT32 FvEventLogSize, UpdEventLogSize;
115 EFI_STATUS Status;
116 HASH_HANDLE HashHandle;
117 UINT8 *HashBase;
118 UINTN HashSize;
119 TPML_DIGEST_VALUES DigestList;
120
121 FvName = TpmMeasurementGetFvName (FirmwareBlobBase, FirmwareBlobLength);
122
123 if (((Description != NULL) || (FvName != NULL)) &&
124 (PcdGet32 (PcdTcgPfpMeasurementRevision) >= TCG_EfiSpecIDEventStruct_SPEC_ERRATA_TPM2_REV_105))
125 {
126 if (Description != NULL) {
127 AsciiSPrint ((CHAR8 *)FvBlob2.BlobDescription, sizeof (FvBlob2.BlobDescription), "%a", Description);
128 AsciiSPrint ((CHAR8 *)UpdBlob2.BlobDescription, sizeof (UpdBlob2.BlobDescription), "%aUDP", Description);
129 } else {
130 AsciiSPrint ((CHAR8 *)FvBlob2.BlobDescription, sizeof (FvBlob2.BlobDescription), "Fv(%g)", FvName);
131 AsciiSPrint ((CHAR8 *)UpdBlob2.BlobDescription, sizeof (UpdBlob2.BlobDescription), "(%g)UDP", FvName);
132 }
133
134 FvBlob2.BlobDescriptionSize = sizeof (FvBlob2.BlobDescription);
135 FvBlob2.BlobBase = FirmwareBlobBase;
136 FvBlob2.BlobLength = FirmwareBlobLength;
137 FvEventType = EV_EFI_PLATFORM_FIRMWARE_BLOB2;
138 FvEventLog = &FvBlob2;
139 FvEventLogSize = sizeof (FvBlob2);
140
141 UpdBlob2.BlobDescriptionSize = sizeof (UpdBlob2.BlobDescription);
142 UpdBlob2.BlobBase = CfgRegionOffset;
143 UpdBlob2.BlobLength = CfgRegionSize;
144 UpdEventLog = &UpdBlob2;
145 UpdEventLogSize = sizeof (UpdBlob2);
146 } else {
147 FvBlob.BlobBase = FirmwareBlobBase;
148 FvBlob.BlobLength = FirmwareBlobLength;
149 FvEventType = EV_EFI_PLATFORM_FIRMWARE_BLOB;
150 FvEventLog = &FvBlob;
151 FvEventLogSize = sizeof (FvBlob);
152
153 UpdBlob.BlobBase = CfgRegionOffset;
154 UpdBlob.BlobLength = CfgRegionSize;
155 UpdEventLog = &UpdBlob;
156 UpdEventLogSize = sizeof (UpdBlob);
157 }
158
160 Status = HashStart (&HashHandle);
161 if (EFI_ERROR (Status)) {
162 DEBUG ((DEBUG_ERROR, "HashStart failed - %r\n", Status));
163 return Status;
164 }
165
167 HashBase = (UINT8 *)(UINTN)FirmwareBlobBase;
168 HashSize = (UINTN)CfgRegionOffset;
169 Status = HashUpdate (HashHandle, HashBase, HashSize);
170 if (EFI_ERROR (Status)) {
171 DEBUG ((DEBUG_ERROR, "HashUpdate failed - %r\n", Status));
172 return Status;
173 }
174
176 HashBase = (UINT8 *)(UINTN)FirmwareBlobBase + CfgRegionOffset + CfgRegionSize;
177 HashSize = (UINTN)(FirmwareBlobLength - CfgRegionOffset - CfgRegionSize);
178 Status = HashUpdate (HashHandle, HashBase, HashSize);
179 if (EFI_ERROR (Status)) {
180 DEBUG ((DEBUG_ERROR, "HashUpdate failed - %r\n", Status));
181 return Status;
182 }
183
185 Status = HashCompleteAndExtend (HashHandle, 0, NULL, 0, &DigestList);
186 if (EFI_ERROR (Status)) {
187 DEBUG ((DEBUG_ERROR, "HashCompleteAndExtend failed - %r\n", Status));
188 return Status;
189 }
190
192 0,
193 FvEventType,
194 FvEventLog,
195 FvEventLogSize,
196 (UINT8 *)&DigestList,
197 (UINTN)sizeof (DigestList),
198 EDKII_TCG_PRE_HASH_LOG_ONLY
199 );
200 if (EFI_ERROR (Status)) {
201 DEBUG ((DEBUG_ERROR, "TpmMeasureAndLogDataWithFlags failed - %r\n", Status));
202 return Status;
203 }
204
205 Status = TpmMeasureAndLogData (
206 1,
207 EV_PLATFORM_CONFIG_FLAGS,
208 UpdEventLog,
209 UpdEventLogSize,
210 (UINT8 *)(UINTN)FirmwareBlobBase + CfgRegionOffset,
211 CfgRegionSize
212 );
213
214 return Status;
215}
216
231EFIAPI
233 IN UINT32 PcrIndex,
234 IN CHAR8 *Description OPTIONAL,
235 IN EFI_PHYSICAL_ADDRESS FirmwareBlobBase,
236 IN UINT64 FirmwareBlobLength
237 )
238{
239 UINT32 FspMeasureMask;
240 FSP_INFO_HEADER *FspHeaderPtr;
241
242 FspMeasureMask = PcdGet32 (PcdFspMeasurementConfig);
243 if ((FspMeasureMask & FSP_MEASURE_FSPUPD) != 0) {
244 FspHeaderPtr = (FSP_INFO_HEADER *)FspFindFspHeader (FirmwareBlobBase);
245 if (FspHeaderPtr != NULL) {
247 Description,
248 FirmwareBlobBase,
249 FirmwareBlobLength,
250 FspHeaderPtr->CfgRegionOffset,
251 FspHeaderPtr->CfgRegionSize
252 );
253 }
254 }
255
256 return MeasureFirmwareBlob (PcrIndex, Description, FirmwareBlobBase, FirmwareBlobLength);
257}
UINT64 UINTN
EFI_STATUS EFIAPI PeiServicesLocatePpi(IN CONST EFI_GUID *Guid, IN UINTN Instance, IN OUT EFI_PEI_PPI_DESCRIPTOR **PpiDescriptor, IN OUT VOID **Ppi)
STATIC EFI_STATUS EFIAPI MeasureFspFirmwareBlobWithCfg(IN CHAR8 *Description OPTIONAL, IN EFI_PHYSICAL_ADDRESS FirmwareBlobBase, IN UINT64 FirmwareBlobLength, IN UINT32 CfgRegionOffset, IN UINT32 CfgRegionSize)
EFI_STATUS EFIAPI TpmMeasureAndLogDataWithFlags(IN UINT32 PcrIndex, IN UINT32 EventType, IN VOID *EventLog, IN UINT32 LogLen, IN VOID *HashData, IN UINT64 HashDataLen, IN UINT64 Flags)
EFI_STATUS EFIAPI MeasureFspFirmwareBlob(IN UINT32 PcrIndex, IN CHAR8 *Description OPTIONAL, IN EFI_PHYSICAL_ADDRESS FirmwareBlobBase, IN UINT64 FirmwareBlobLength)
FSP_INFO_HEADER *EFIAPI FspFindFspHeader(IN EFI_PHYSICAL_ADDRESS FlashFvFspBase)
EFI_STATUS EFIAPI HashStart(OUT HASH_HANDLE *HashHandle)
Definition: HashLibTdx.c:39
EFI_STATUS EFIAPI HashUpdate(IN HASH_HANDLE HashHandle, IN VOID *DataToHash, IN UINTN DataToHashLen)
Definition: HashLibTdx.c:69
EFI_STATUS EFIAPI HashCompleteAndExtend(IN HASH_HANDLE HashHandle, IN TPMI_DH_PCR PcrIndex, IN VOID *DataToHash, IN UINTN DataToHashLen, OUT TPML_DIGEST_VALUES *DigestList)
Definition: HashLibTdx.c:98
UINTN EFIAPI AsciiSPrint(OUT CHAR8 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString,...)
Definition: PrintLib.c:813
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define IN
Definition: Base.h:279
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define PcdGet32(TokenName)
Definition: PcdLib.h:362
VOID * TpmMeasurementGetFvName(IN EFI_PHYSICAL_ADDRESS FvBase, IN UINT64 FvLength)
EFI_STATUS EFIAPI MeasureFirmwareBlob(IN UINT32 PcrIndex, IN CHAR8 *Description OPTIONAL, IN EFI_PHYSICAL_ADDRESS FirmwareBlobBase, IN UINT64 FirmwareBlobLength)
EFI_STATUS EFIAPI TpmMeasureAndLogData(IN UINT32 PcrIndex, IN UINT32 EventType, IN VOID *EventLog, IN UINT32 LogLen, IN VOID *HashData, IN UINT64 HashDataLen)
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
UINT32 CfgRegionOffset