]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/Tcg2Pei/Tcg2Pei.c
SecurityPkg/Tcg: Add TcgPpi
[mirror_edk2.git] / SecurityPkg / Tcg / Tcg2Pei / Tcg2Pei.c
CommitLineData
1abfa4ce
JY
1/** @file\r
2 Initialize TPM2 device and measure FVs before handing off control to DXE.\r
3\r
b3548d32 4Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>\r
d7c054f9 5Copyright (c) 2017, Microsoft Corporation. All rights reserved. <BR>\r
289b714b 6SPDX-License-Identifier: BSD-2-Clause-Patent\r
1abfa4ce
JY
7\r
8**/\r
9\r
10#include <PiPei.h>\r
11\r
12#include <IndustryStandard/UefiTcgPlatform.h>\r
13#include <Ppi/FirmwareVolumeInfo.h>\r
14#include <Ppi/FirmwareVolumeInfo2.h>\r
1abfa4ce
JY
15#include <Ppi/TpmInitialized.h>\r
16#include <Ppi/FirmwareVolume.h>\r
17#include <Ppi/EndOfPeiPhase.h>\r
18#include <Ppi/FirmwareVolumeInfoMeasurementExcluded.h>\r
d7c054f9 19#include <Ppi/FirmwareVolumeInfoPrehashedFV.h>\r
1abfa4ce
JY
20\r
21#include <Guid/TcgEventHob.h>\r
22#include <Guid/MeasuredFvHob.h>\r
23#include <Guid/TpmInstance.h>\r
24\r
25#include <Library/DebugLib.h>\r
26#include <Library/BaseMemoryLib.h>\r
27#include <Library/PeiServicesLib.h>\r
28#include <Library/PeimEntryPoint.h>\r
29#include <Library/Tpm2CommandLib.h>\r
30#include <Library/Tpm2DeviceLib.h>\r
31#include <Library/HashLib.h>\r
32#include <Library/HobLib.h>\r
33#include <Library/PcdLib.h>\r
34#include <Library/PeiServicesTablePointerLib.h>\r
35#include <Protocol/Tcg2Protocol.h>\r
36#include <Library/PerformanceLib.h>\r
37#include <Library/MemoryAllocationLib.h>\r
38#include <Library/ReportStatusCodeLib.h>\r
5919a960 39#include <Library/ResetSystemLib.h>\r
0286fe81 40#include <Library/PrintLib.h>\r
1abfa4ce
JY
41\r
42#define PERF_ID_TCG2_PEI 0x3080\r
43\r
44typedef struct {\r
45 EFI_GUID *EventGuid;\r
46 EFI_TCG2_EVENT_LOG_FORMAT LogFormat;\r
47} TCG2_EVENT_INFO_STRUCT;\r
48\r
49TCG2_EVENT_INFO_STRUCT mTcg2EventInfo[] = {\r
50 {&gTcgEventEntryHobGuid, EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2},\r
51 {&gTcgEvent2EntryHobGuid, EFI_TCG2_EVENT_LOG_FORMAT_TCG_2},\r
52};\r
53\r
54BOOLEAN mImageInMemory = FALSE;\r
55EFI_PEI_FILE_HANDLE mFileHandle;\r
56\r
57EFI_PEI_PPI_DESCRIPTOR mTpmInitializedPpiList = {\r
58 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
59 &gPeiTpmInitializedPpiGuid,\r
60 NULL\r
61};\r
62\r
63EFI_PEI_PPI_DESCRIPTOR mTpmInitializationDonePpiList = {\r
64 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
65 &gPeiTpmInitializationDonePpiGuid,\r
66 NULL\r
67};\r
68\r
aa8c6e2a
SZ
69//\r
70// Number of firmware blobs to grow by each time we run out of room\r
71//\r
72#define FIRMWARE_BLOB_GROWTH_STEP 4\r
73\r
1abfa4ce 74EFI_PLATFORM_FIRMWARE_BLOB *mMeasuredBaseFvInfo;\r
aa8c6e2a 75UINT32 mMeasuredMaxBaseFvIndex = 0;\r
1abfa4ce
JY
76UINT32 mMeasuredBaseFvIndex = 0;\r
77\r
78EFI_PLATFORM_FIRMWARE_BLOB *mMeasuredChildFvInfo;\r
aa8c6e2a 79UINT32 mMeasuredMaxChildFvIndex = 0;\r
1abfa4ce
JY
80UINT32 mMeasuredChildFvIndex = 0;\r
81\r
0286fe81
JY
82#pragma pack (1)\r
83\r
84#define FV_HANDOFF_TABLE_DESC "Fv(XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)"\r
85typedef struct {\r
86 UINT8 BlobDescriptionSize;\r
87 UINT8 BlobDescription[sizeof(FV_HANDOFF_TABLE_DESC)];\r
88 EFI_PHYSICAL_ADDRESS BlobBase;\r
89 UINT64 BlobLength;\r
90} FV_HANDOFF_TABLE_POINTERS2;\r
91\r
92#pragma pack ()\r
93\r
1abfa4ce 94/**\r
d6b926e7 95 Measure and record the Firmware Volume Information once FvInfoPPI install.\r
1abfa4ce
JY
96\r
97 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
98 @param[in] NotifyDescriptor Address of the notification descriptor data structure.\r
99 @param[in] Ppi Address of the PPI that was installed.\r
100\r
101 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.\r
102 @return Others Fail to measure FV.\r
103\r
104**/\r
105EFI_STATUS\r
106EFIAPI\r
d6b926e7 107FirmwareVolumeInfoPpiNotifyCallback (\r
1abfa4ce
JY
108 IN EFI_PEI_SERVICES **PeiServices,\r
109 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,\r
110 IN VOID *Ppi\r
111 );\r
112\r
113/**\r
d6b926e7 114 Record all measured Firmware Volume Information into a Guid Hob\r
1abfa4ce
JY
115\r
116 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
117 @param[in] NotifyDescriptor Address of the notification descriptor data structure.\r
118 @param[in] Ppi Address of the PPI that was installed.\r
119\r
120 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.\r
121 @return Others Fail to measure FV.\r
122\r
123**/\r
124EFI_STATUS\r
125EFIAPI\r
126EndofPeiSignalNotifyCallBack (\r
127 IN EFI_PEI_SERVICES **PeiServices,\r
128 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,\r
129 IN VOID *Ppi\r
130 );\r
131\r
132EFI_PEI_NOTIFY_DESCRIPTOR mNotifyList[] = {\r
133 {\r
134 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,\r
135 &gEfiPeiFirmwareVolumeInfoPpiGuid,\r
d6b926e7 136 FirmwareVolumeInfoPpiNotifyCallback\r
1abfa4ce
JY
137 },\r
138 {\r
139 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,\r
140 &gEfiPeiFirmwareVolumeInfo2PpiGuid,\r
d6b926e7 141 FirmwareVolumeInfoPpiNotifyCallback\r
1abfa4ce
JY
142 },\r
143 {\r
144 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
145 &gEfiEndOfPeiSignalPpiGuid,\r
146 EndofPeiSignalNotifyCallBack\r
147 }\r
148};\r
149\r
1abfa4ce 150\r
1abfa4ce 151/**\r
d6b926e7 152 Record all measured Firmware Volume Information into a Guid Hob\r
b3548d32 153 Guid Hob payload layout is\r
1abfa4ce
JY
154\r
155 UINT32 *************************** FIRMWARE_BLOB number\r
156 EFI_PLATFORM_FIRMWARE_BLOB******** BLOB Array\r
157\r
158 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
159 @param[in] NotifyDescriptor Address of the notification descriptor data structure.\r
160 @param[in] Ppi Address of the PPI that was installed.\r
161\r
162 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.\r
163 @return Others Fail to measure FV.\r
164\r
165**/\r
166EFI_STATUS\r
167EFIAPI\r
168EndofPeiSignalNotifyCallBack (\r
169 IN EFI_PEI_SERVICES **PeiServices,\r
170 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,\r
171 IN VOID *Ppi\r
172 )\r
b3548d32 173{\r
1abfa4ce
JY
174 MEASURED_HOB_DATA *MeasuredHobData;\r
175\r
176 MeasuredHobData = NULL;\r
177\r
d9c5beb1
DB
178 PERF_CALLBACK_BEGIN (&gEfiEndOfPeiSignalPpiGuid);\r
179\r
1abfa4ce 180 //\r
b3548d32 181 // Create a Guid hob to save all measured Fv\r
1abfa4ce
JY
182 //\r
183 MeasuredHobData = BuildGuidHob(\r
184 &gMeasuredFvHobGuid,\r
185 sizeof(UINTN) + sizeof(EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredBaseFvIndex + mMeasuredChildFvIndex)\r
186 );\r
187\r
188 if (MeasuredHobData != NULL){\r
189 //\r
190 // Save measured FV info enty number\r
191 //\r
192 MeasuredHobData->Num = mMeasuredBaseFvIndex + mMeasuredChildFvIndex;\r
193\r
194 //\r
195 // Save measured base Fv info\r
196 //\r
197 CopyMem (MeasuredHobData->MeasuredFvBuf, mMeasuredBaseFvInfo, sizeof(EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredBaseFvIndex));\r
198\r
199 //\r
200 // Save measured child Fv info\r
201 //\r
202 CopyMem (&MeasuredHobData->MeasuredFvBuf[mMeasuredBaseFvIndex] , mMeasuredChildFvInfo, sizeof(EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredChildFvIndex));\r
203 }\r
204\r
d9c5beb1
DB
205 PERF_CALLBACK_END (&gEfiEndOfPeiSignalPpiGuid);\r
206\r
1abfa4ce
JY
207 return EFI_SUCCESS;\r
208}\r
209\r
1abfa4ce 210/**\r
5919a960
JY
211 Make sure that the current PCR allocations, the TPM supported PCRs,\r
212 and the PcdTpm2HashMask are all in agreement.\r
1abfa4ce
JY
213**/\r
214VOID\r
5919a960 215SyncPcrAllocationsAndPcrMask (\r
1abfa4ce
JY
216 VOID\r
217 )\r
218{\r
5919a960
JY
219 EFI_STATUS Status;\r
220 EFI_TCG2_EVENT_ALGORITHM_BITMAP TpmHashAlgorithmBitmap;\r
221 UINT32 TpmActivePcrBanks;\r
222 UINT32 NewTpmActivePcrBanks;\r
223 UINT32 Tpm2PcrMask;\r
224 UINT32 NewTpm2PcrMask;\r
1abfa4ce 225\r
5919a960 226 DEBUG ((EFI_D_ERROR, "SyncPcrAllocationsAndPcrMask!\n"));\r
1abfa4ce 227\r
5919a960
JY
228 //\r
229 // Determine the current TPM support and the Platform PCR mask.\r
230 //\r
231 Status = Tpm2GetCapabilitySupportedAndActivePcrs (&TpmHashAlgorithmBitmap, &TpmActivePcrBanks);\r
232 ASSERT_EFI_ERROR (Status);\r
233\r
234 Tpm2PcrMask = PcdGet32 (PcdTpm2HashMask);\r
d7c054f9
ZC
235 if (Tpm2PcrMask == 0) {\r
236 //\r
237 // if PcdTPm2HashMask is zero, use ActivePcr setting\r
238 //\r
239 PcdSet32S (PcdTpm2HashMask, TpmActivePcrBanks);\r
240 Tpm2PcrMask = TpmActivePcrBanks;\r
241 }\r
5919a960
JY
242\r
243 //\r
244 // Find the intersection of Pcd support and TPM support.\r
245 // If banks are missing from the TPM support that are in the PCD, update the PCD.\r
246 // If banks are missing from the PCD that are active in the TPM, reallocate the banks and reboot.\r
247 //\r
248\r
249 //\r
250 // If there are active PCR banks that are not supported by the Platform mask,\r
251 // update the TPM allocations and reboot the machine.\r
252 //\r
253 if ((TpmActivePcrBanks & Tpm2PcrMask) != TpmActivePcrBanks) {\r
254 NewTpmActivePcrBanks = TpmActivePcrBanks & Tpm2PcrMask;\r
255\r
fe882c01 256 DEBUG ((EFI_D_INFO, "%a - Reallocating PCR banks from 0x%X to 0x%X.\n", __FUNCTION__, TpmActivePcrBanks, NewTpmActivePcrBanks));\r
5919a960 257 if (NewTpmActivePcrBanks == 0) {\r
fe882c01 258 DEBUG ((EFI_D_ERROR, "%a - No viable PCRs active! Please set a less restrictive value for PcdTpm2HashMask!\n", __FUNCTION__));\r
5919a960
JY
259 ASSERT (FALSE);\r
260 } else {\r
261 Status = Tpm2PcrAllocateBanks (NULL, (UINT32)TpmHashAlgorithmBitmap, NewTpmActivePcrBanks);\r
262 if (EFI_ERROR (Status)) {\r
263 //\r
264 // We can't do much here, but we hope that this doesn't happen.\r
265 //\r
fe882c01 266 DEBUG ((EFI_D_ERROR, "%a - Failed to reallocate PCRs!\n", __FUNCTION__));\r
5919a960 267 ASSERT_EFI_ERROR (Status);\r
1abfa4ce 268 }\r
5919a960
JY
269 //\r
270 // Need reset system, since we just called Tpm2PcrAllocateBanks().\r
271 //\r
272 ResetCold();\r
1abfa4ce
JY
273 }\r
274 }\r
5919a960
JY
275\r
276 //\r
277 // If there are any PCRs that claim support in the Platform mask that are\r
278 // not supported by the TPM, update the mask.\r
279 //\r
280 if ((Tpm2PcrMask & TpmHashAlgorithmBitmap) != Tpm2PcrMask) {\r
281 NewTpm2PcrMask = Tpm2PcrMask & TpmHashAlgorithmBitmap;\r
282\r
fe882c01 283 DEBUG ((EFI_D_INFO, "%a - Updating PcdTpm2HashMask from 0x%X to 0x%X.\n", __FUNCTION__, Tpm2PcrMask, NewTpm2PcrMask));\r
5919a960 284 if (NewTpm2PcrMask == 0) {\r
fe882c01 285 DEBUG ((EFI_D_ERROR, "%a - No viable PCRs supported! Please set a less restrictive value for PcdTpm2HashMask!\n", __FUNCTION__));\r
5919a960
JY
286 ASSERT (FALSE);\r
287 }\r
288\r
289 Status = PcdSet32S (PcdTpm2HashMask, NewTpm2PcrMask);\r
290 ASSERT_EFI_ERROR (Status);\r
291 }\r
1abfa4ce
JY
292}\r
293\r
294/**\r
295 Add a new entry to the Event Log.\r
296\r
297 @param[in] DigestList A list of digest.\r
298 @param[in,out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.\r
299 @param[in] NewEventData Pointer to the new event data.\r
300\r
301 @retval EFI_SUCCESS The new event log entry was added.\r
302 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.\r
303**/\r
304EFI_STATUS\r
305LogHashEvent (\r
306 IN TPML_DIGEST_VALUES *DigestList,\r
307 IN OUT TCG_PCR_EVENT_HDR *NewEventHdr,\r
308 IN UINT8 *NewEventData\r
309 )\r
310{\r
311 VOID *HobData;\r
312 EFI_STATUS Status;\r
313 UINTN Index;\r
314 EFI_STATUS RetStatus;\r
315 UINT32 SupportedEventLogs;\r
316 TCG_PCR_EVENT2 *TcgPcrEvent2;\r
317 UINT8 *DigestBuffer;\r
318\r
319 SupportedEventLogs = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2 | EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;\r
320\r
321 RetStatus = EFI_SUCCESS;\r
322 for (Index = 0; Index < sizeof(mTcg2EventInfo)/sizeof(mTcg2EventInfo[0]); Index++) {\r
323 if ((SupportedEventLogs & mTcg2EventInfo[Index].LogFormat) != 0) {\r
324 DEBUG ((EFI_D_INFO, " LogFormat - 0x%08x\n", mTcg2EventInfo[Index].LogFormat));\r
325 switch (mTcg2EventInfo[Index].LogFormat) {\r
326 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2:\r
d4b9b2c3 327 Status = GetDigestFromDigestList (TPM_ALG_SHA1, DigestList, &NewEventHdr->Digest);\r
1abfa4ce
JY
328 if (!EFI_ERROR (Status)) {\r
329 HobData = BuildGuidHob (\r
330 &gTcgEventEntryHobGuid,\r
331 sizeof (*NewEventHdr) + NewEventHdr->EventSize\r
332 );\r
333 if (HobData == NULL) {\r
334 RetStatus = EFI_OUT_OF_RESOURCES;\r
335 break;\r
336 }\r
337\r
338 CopyMem (HobData, NewEventHdr, sizeof (*NewEventHdr));\r
339 HobData = (VOID *) ((UINT8*)HobData + sizeof (*NewEventHdr));\r
340 CopyMem (HobData, NewEventData, NewEventHdr->EventSize);\r
341 }\r
342 break;\r
343 case EFI_TCG2_EVENT_LOG_FORMAT_TCG_2:\r
3cabe66b
SZ
344 //\r
345 // Use GetDigestListSize (DigestList) in the GUID HOB DataLength calculation\r
346 // to reserve enough buffer to hold TPML_DIGEST_VALUES compact binary.\r
347 //\r
1abfa4ce
JY
348 HobData = BuildGuidHob (\r
349 &gTcgEvent2EntryHobGuid,\r
350 sizeof(TcgPcrEvent2->PCRIndex) + sizeof(TcgPcrEvent2->EventType) + GetDigestListSize (DigestList) + sizeof(TcgPcrEvent2->EventSize) + NewEventHdr->EventSize\r
351 );\r
352 if (HobData == NULL) {\r
353 RetStatus = EFI_OUT_OF_RESOURCES;\r
354 break;\r
355 }\r
356\r
357 TcgPcrEvent2 = HobData;\r
358 TcgPcrEvent2->PCRIndex = NewEventHdr->PCRIndex;\r
359 TcgPcrEvent2->EventType = NewEventHdr->EventType;\r
360 DigestBuffer = (UINT8 *)&TcgPcrEvent2->Digest;\r
f5e34e37 361 DigestBuffer = CopyDigestListToBuffer (DigestBuffer, DigestList, PcdGet32 (PcdTpm2HashMask));\r
1abfa4ce
JY
362 CopyMem (DigestBuffer, &NewEventHdr->EventSize, sizeof(TcgPcrEvent2->EventSize));\r
363 DigestBuffer = DigestBuffer + sizeof(TcgPcrEvent2->EventSize);\r
364 CopyMem (DigestBuffer, NewEventData, NewEventHdr->EventSize);\r
365 break;\r
366 }\r
367 }\r
368 }\r
369\r
370 return RetStatus;\r
371}\r
372\r
373/**\r
374 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,\r
375 and build a GUIDed HOB recording the event which will be passed to the DXE phase and\r
376 added into the Event Log.\r
377\r
378 @param[in] Flags Bitmap providing additional information.\r
b3548d32 379 @param[in] HashData Physical address of the start of the data buffer\r
1abfa4ce
JY
380 to be hashed, extended, and logged.\r
381 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData.\r
b3548d32
LG
382 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.\r
383 @param[in] NewEventData Pointer to the new event data.\r
1abfa4ce
JY
384\r
385 @retval EFI_SUCCESS Operation completed successfully.\r
386 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.\r
387 @retval EFI_DEVICE_ERROR The command was unsuccessful.\r
388\r
389**/\r
390EFI_STATUS\r
391HashLogExtendEvent (\r
392 IN UINT64 Flags,\r
393 IN UINT8 *HashData,\r
394 IN UINTN HashDataLen,\r
395 IN TCG_PCR_EVENT_HDR *NewEventHdr,\r
396 IN UINT8 *NewEventData\r
397 )\r
398{\r
399 EFI_STATUS Status;\r
400 TPML_DIGEST_VALUES DigestList;\r
401\r
402 if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {\r
403 return EFI_DEVICE_ERROR;\r
404 }\r
405\r
406 Status = HashAndExtend (\r
407 NewEventHdr->PCRIndex,\r
408 HashData,\r
409 HashDataLen,\r
410 &DigestList\r
411 );\r
412 if (!EFI_ERROR (Status)) {\r
413 if ((Flags & EFI_TCG2_EXTEND_ONLY) == 0) {\r
414 Status = LogHashEvent (&DigestList, NewEventHdr, NewEventData);\r
415 }\r
416 }\r
b3548d32 417\r
1abfa4ce
JY
418 if (Status == EFI_DEVICE_ERROR) {\r
419 DEBUG ((EFI_D_ERROR, "HashLogExtendEvent - %r. Disable TPM.\n", Status));\r
420 BuildGuidHob (&gTpmErrorHobGuid,0);\r
421 REPORT_STATUS_CODE (\r
422 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
423 (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)\r
424 );\r
425 }\r
426\r
427 return Status;\r
428}\r
429\r
430/**\r
431 Measure CRTM version.\r
432\r
433 @retval EFI_SUCCESS Operation completed successfully.\r
434 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.\r
435 @retval EFI_DEVICE_ERROR The command was unsuccessful.\r
436\r
437**/\r
438EFI_STATUS\r
439MeasureCRTMVersion (\r
440 VOID\r
441 )\r
442{\r
443 TCG_PCR_EVENT_HDR TcgEventHdr;\r
444\r
445 //\r
446 // Use FirmwareVersion string to represent CRTM version.\r
447 // OEMs should get real CRTM version string and measure it.\r
448 //\r
449\r
450 TcgEventHdr.PCRIndex = 0;\r
451 TcgEventHdr.EventType = EV_S_CRTM_VERSION;\r
452 TcgEventHdr.EventSize = (UINT32) StrSize((CHAR16*)PcdGetPtr (PcdFirmwareVersionString));\r
453\r
454 return HashLogExtendEvent (\r
455 0,\r
456 (UINT8*)PcdGetPtr (PcdFirmwareVersionString),\r
457 TcgEventHdr.EventSize,\r
458 &TcgEventHdr,\r
459 (UINT8*)PcdGetPtr (PcdFirmwareVersionString)\r
460 );\r
461}\r
462\r
f9713abe 463/**\r
0286fe81
JY
464 Get the FvName from the FV header.\r
465\r
466 Causion: The FV is untrusted input.\r
467\r
468 @param[in] FvBase Base address of FV image.\r
469 @param[in] FvLength Length of FV image.\r
470\r
471 @return FvName pointer\r
472 @retval NULL FvName is NOT found\r
f9713abe 473**/\r
0286fe81
JY
474VOID *\r
475GetFvName (\r
476 IN EFI_PHYSICAL_ADDRESS FvBase,\r
477 IN UINT64 FvLength\r
478 )\r
479{\r
480 EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r
481 EFI_FIRMWARE_VOLUME_EXT_HEADER *FvExtHeader;\r
482\r
483 if (FvBase >= MAX_ADDRESS) {\r
484 return NULL;\r
485 }\r
486 if (FvLength >= MAX_ADDRESS - FvBase) {\r
487 return NULL;\r
488 }\r
489 if (FvLength < sizeof(EFI_FIRMWARE_VOLUME_HEADER)) {\r
490 return NULL;\r
491 }\r
492\r
493 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)FvBase;\r
494 if (FvHeader->ExtHeaderOffset < sizeof(EFI_FIRMWARE_VOLUME_HEADER)) {\r
495 return NULL;\r
496 }\r
497 if (FvHeader->ExtHeaderOffset + sizeof(EFI_FIRMWARE_VOLUME_EXT_HEADER) > FvLength) {\r
498 return NULL;\r
499 }\r
500 FvExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *)(UINTN)(FvBase + FvHeader->ExtHeaderOffset);\r
501\r
502 return &FvExtHeader->FvName;\r
503}\r
504\r
1abfa4ce 505/**\r
b3548d32
LG
506 Measure FV image.\r
507 Add it into the measured FV list after the FV is measured successfully.\r
1abfa4ce
JY
508\r
509 @param[in] FvBase Base address of FV image.\r
510 @param[in] FvLength Length of FV image.\r
511\r
b3548d32 512 @retval EFI_SUCCESS Fv image is measured successfully\r
1abfa4ce
JY
513 or it has been already measured.\r
514 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.\r
515 @retval EFI_DEVICE_ERROR The command was unsuccessful.\r
516\r
517**/\r
518EFI_STATUS\r
519MeasureFvImage (\r
520 IN EFI_PHYSICAL_ADDRESS FvBase,\r
521 IN UINT64 FvLength\r
522 )\r
523{\r
d7c054f9
ZC
524 UINT32 Index;\r
525 EFI_STATUS Status;\r
526 EFI_PLATFORM_FIRMWARE_BLOB FvBlob;\r
0286fe81
JY
527 FV_HANDOFF_TABLE_POINTERS2 FvBlob2;\r
528 VOID *EventData;\r
529 VOID *FvName;\r
d7c054f9
ZC
530 TCG_PCR_EVENT_HDR TcgEventHdr;\r
531 UINT32 Instance;\r
532 UINT32 Tpm2HashMask;\r
533 TPML_DIGEST_VALUES DigestList;\r
534 UINT32 DigestCount;\r
535 EFI_PEI_FIRMWARE_VOLUME_INFO_MEASUREMENT_EXCLUDED_PPI *MeasurementExcludedFvPpi;\r
536 EDKII_PEI_FIRMWARE_VOLUME_INFO_PREHASHED_FV_PPI *PrehashedFvPpi;\r
537 HASH_INFO *PreHashInfo;\r
538 UINT32 HashAlgoMask;\r
1abfa4ce
JY
539\r
540 //\r
d7c054f9 541 // Check Excluded FV list\r
1abfa4ce 542 //\r
d7c054f9
ZC
543 Instance = 0;\r
544 do {\r
545 Status = PeiServicesLocatePpi(\r
546 &gEfiPeiFirmwareVolumeInfoMeasurementExcludedPpiGuid,\r
547 Instance,\r
548 NULL,\r
549 (VOID**)&MeasurementExcludedFvPpi\r
550 );\r
551 if (!EFI_ERROR(Status)) {\r
552 for (Index = 0; Index < MeasurementExcludedFvPpi->Count; Index ++) {\r
553 if (MeasurementExcludedFvPpi->Fv[Index].FvBase == FvBase\r
554 && MeasurementExcludedFvPpi->Fv[Index].FvLength == FvLength) {\r
555 DEBUG ((DEBUG_INFO, "The FV which is excluded by Tcg2Pei starts at: 0x%x\n", FvBase));\r
556 DEBUG ((DEBUG_INFO, "The FV which is excluded by Tcg2Pei has the size: 0x%x\n", FvLength));\r
557 return EFI_SUCCESS;\r
558 }\r
1abfa4ce 559 }\r
d7c054f9
ZC
560\r
561 Instance++;\r
1abfa4ce 562 }\r
d7c054f9 563 } while (!EFI_ERROR(Status));\r
1abfa4ce
JY
564\r
565 //\r
d7c054f9 566 // Check measured FV list\r
1abfa4ce
JY
567 //\r
568 for (Index = 0; Index < mMeasuredBaseFvIndex; Index ++) {\r
d7c054f9
ZC
569 if (mMeasuredBaseFvInfo[Index].BlobBase == FvBase && mMeasuredBaseFvInfo[Index].BlobLength == FvLength) {\r
570 DEBUG ((DEBUG_INFO, "The FV which is already measured by Tcg2Pei starts at: 0x%x\n", FvBase));\r
571 DEBUG ((DEBUG_INFO, "The FV which is already measured by Tcg2Pei has the size: 0x%x\n", FvLength));\r
1abfa4ce
JY
572 return EFI_SUCCESS;\r
573 }\r
574 }\r
d7c054f9 575\r
1abfa4ce 576 //\r
d7c054f9 577 // Check pre-hashed FV list\r
1abfa4ce 578 //\r
d7c054f9
ZC
579 Instance = 0;\r
580 Tpm2HashMask = PcdGet32 (PcdTpm2HashMask);\r
581 do {\r
582 Status = PeiServicesLocatePpi (\r
583 &gEdkiiPeiFirmwareVolumeInfoPrehashedFvPpiGuid,\r
584 Instance,\r
585 NULL,\r
586 (VOID**)&PrehashedFvPpi\r
587 );\r
588 if (!EFI_ERROR(Status) && PrehashedFvPpi->FvBase == FvBase && PrehashedFvPpi->FvLength == FvLength) {\r
589 ZeroMem (&DigestList, sizeof(TPML_DIGEST_VALUES));\r
590\r
591 //\r
592 // The FV is prehashed, check against TPM hash mask\r
593 //\r
594 PreHashInfo = (HASH_INFO *)(PrehashedFvPpi + 1);\r
595 for (Index = 0, DigestCount = 0; Index < PrehashedFvPpi->Count; Index++) {\r
596 DEBUG((DEBUG_INFO, "Hash Algo ID in PrehashedFvPpi=0x%x\n", PreHashInfo->HashAlgoId));\r
597 HashAlgoMask = GetHashMaskFromAlgo(PreHashInfo->HashAlgoId);\r
598 if ((Tpm2HashMask & HashAlgoMask) != 0 ) {\r
599 //\r
600 // Hash is required, copy it to DigestList\r
601 //\r
602 WriteUnaligned16(&(DigestList.digests[DigestCount].hashAlg), PreHashInfo->HashAlgoId);\r
603 CopyMem (\r
604 &DigestList.digests[DigestCount].digest,\r
605 PreHashInfo + 1,\r
606 PreHashInfo->HashSize\r
607 );\r
608 DigestCount++;\r
609 //\r
610 // Clean the corresponding Hash Algo mask bit\r
611 //\r
612 Tpm2HashMask &= ~HashAlgoMask;\r
613 }\r
614 PreHashInfo = (HASH_INFO *)((UINT8 *)(PreHashInfo + 1) + PreHashInfo->HashSize);\r
615 }\r
1abfa4ce 616\r
d7c054f9 617 WriteUnaligned32(&DigestList.count, DigestCount);\r
1abfa4ce 618\r
d7c054f9
ZC
619 break;\r
620 }\r
621 Instance++;\r
622 } while (!EFI_ERROR(Status));\r
623\r
624 //\r
625 // Init the log event for FV measurement\r
626 //\r
0286fe81
JY
627 if (PcdGet32(PcdTcgPfpMeasurementRevision) >= TCG_EfiSpecIDEventStruct_SPEC_ERRATA_TPM2_REV_105) {\r
628 FvBlob2.BlobDescriptionSize = sizeof(FvBlob2.BlobDescription);\r
629 CopyMem (FvBlob2.BlobDescription, FV_HANDOFF_TABLE_DESC, sizeof(FvBlob2.BlobDescription));\r
630 FvName = GetFvName (FvBase, FvLength);\r
631 if (FvName != NULL) {\r
632 AsciiSPrint ((CHAR8 *)FvBlob2.BlobDescription, sizeof(FvBlob2.BlobDescription), "Fv(%g)", FvName);\r
633 }\r
634 FvBlob2.BlobBase = FvBase;\r
635 FvBlob2.BlobLength = FvLength;\r
f45e3a4a 636 TcgEventHdr.PCRIndex = 0;\r
0286fe81
JY
637 TcgEventHdr.EventType = EV_EFI_PLATFORM_FIRMWARE_BLOB2;\r
638 TcgEventHdr.EventSize = sizeof (FvBlob2);\r
639 EventData = &FvBlob2;\r
640 } else {\r
641 FvBlob.BlobBase = FvBase;\r
642 FvBlob.BlobLength = FvLength;\r
643 TcgEventHdr.PCRIndex = 0;\r
644 TcgEventHdr.EventType = EV_EFI_PLATFORM_FIRMWARE_BLOB;\r
645 TcgEventHdr.EventSize = sizeof (FvBlob);\r
646 EventData = &FvBlob;\r
647 }\r
1abfa4ce 648\r
d7c054f9
ZC
649 if (Tpm2HashMask == 0) {\r
650 //\r
651 // FV pre-hash algos comply with current TPM hash requirement\r
652 // Skip hashing step in measure, only extend DigestList to PCR and log event\r
653 //\r
654 Status = Tpm2PcrExtend(\r
655 0,\r
656 &DigestList\r
657 );\r
658\r
659 if (!EFI_ERROR(Status)) {\r
0286fe81
JY
660 Status = LogHashEvent (&DigestList, &TcgEventHdr, EventData);\r
661 DEBUG ((DEBUG_INFO, "The pre-hashed FV which is extended & logged by Tcg2Pei starts at: 0x%x\n", FvBase));\r
662 DEBUG ((DEBUG_INFO, "The pre-hashed FV which is extended & logged by Tcg2Pei has the size: 0x%x\n", FvLength));\r
d7c054f9
ZC
663 } else if (Status == EFI_DEVICE_ERROR) {\r
664 BuildGuidHob (&gTpmErrorHobGuid,0);\r
665 REPORT_STATUS_CODE (\r
666 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
667 (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)\r
668 );\r
669 }\r
670 } else {\r
671 //\r
672 // Hash the FV, extend digest to the TPM and log TCG event\r
673 //\r
674 Status = HashLogExtendEvent (\r
675 0,\r
0286fe81
JY
676 (UINT8*) (UINTN) FvBase, // HashData\r
677 (UINTN) FvLength, // HashDataLen\r
678 &TcgEventHdr, // EventHdr\r
679 EventData // EventData\r
d7c054f9 680 );\r
0286fe81
JY
681 DEBUG ((DEBUG_INFO, "The FV which is measured by Tcg2Pei starts at: 0x%x\n", FvBase));\r
682 DEBUG ((DEBUG_INFO, "The FV which is measured by Tcg2Pei has the size: 0x%x\n", FvLength));\r
d7c054f9
ZC
683 }\r
684\r
685 if (EFI_ERROR(Status)) {\r
686 DEBUG ((DEBUG_ERROR, "The FV which failed to be measured starts at: 0x%x\n", FvBase));\r
687 return Status;\r
688 }\r
1abfa4ce
JY
689\r
690 //\r
691 // Add new FV into the measured FV list.\r
692 //\r
aa8c6e2a
SZ
693 if (mMeasuredBaseFvIndex >= mMeasuredMaxBaseFvIndex) {\r
694 mMeasuredBaseFvInfo = ReallocatePool (\r
695 sizeof (EFI_PLATFORM_FIRMWARE_BLOB) * mMeasuredMaxBaseFvIndex,\r
696 sizeof (EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredMaxBaseFvIndex + FIRMWARE_BLOB_GROWTH_STEP),\r
697 mMeasuredBaseFvInfo\r
698 );\r
699 ASSERT (mMeasuredBaseFvInfo != NULL);\r
700 mMeasuredMaxBaseFvIndex = mMeasuredMaxBaseFvIndex + FIRMWARE_BLOB_GROWTH_STEP;\r
1abfa4ce
JY
701 }\r
702\r
aa8c6e2a
SZ
703 mMeasuredBaseFvInfo[mMeasuredBaseFvIndex].BlobBase = FvBase;\r
704 mMeasuredBaseFvInfo[mMeasuredBaseFvIndex].BlobLength = FvLength;\r
705 mMeasuredBaseFvIndex++;\r
706\r
1abfa4ce
JY
707 return Status;\r
708}\r
709\r
710/**\r
711 Measure main BIOS.\r
712\r
713 @retval EFI_SUCCESS Operation completed successfully.\r
714 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.\r
715 @retval EFI_DEVICE_ERROR The command was unsuccessful.\r
716\r
717**/\r
718EFI_STATUS\r
719MeasureMainBios (\r
720 VOID\r
721 )\r
722{\r
723 EFI_STATUS Status;\r
1abfa4ce
JY
724 EFI_PEI_FV_HANDLE VolumeHandle;\r
725 EFI_FV_INFO VolumeInfo;\r
726 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
727\r
728 PERF_START_EX (mFileHandle, "EventRec", "Tcg2Pei", 0, PERF_ID_TCG2_PEI);\r
1abfa4ce 729\r
d7c054f9
ZC
730 //\r
731 // Only measure BFV at the very beginning. Other parts of Static Core Root of\r
732 // Trust for Measurement(S-CRTM) will be measured later on FvInfoNotify.\r
733 // BFV is processed without installing FV Info Ppi. Other FVs either inside BFV or\r
734 // reported by platform will be installed with Fv Info Ppi\r
735 // This firmware volume measure policy can be modified/enhanced by special\r
736 // platform for special CRTM TPM measuring.\r
737 //\r
738 Status = PeiServicesFfsFindNextVolume (0, &VolumeHandle);\r
739 ASSERT_EFI_ERROR (Status);\r
740\r
741 //\r
742 // Measure and record the firmware volume that is dispatched by PeiCore\r
743 //\r
744 Status = PeiServicesFfsGetVolumeInfo (VolumeHandle, &VolumeInfo);\r
745 ASSERT_EFI_ERROR (Status);\r
746 //\r
747 // Locate the corresponding FV_PPI according to founded FV's format guid\r
748 //\r
749 Status = PeiServicesLocatePpi (\r
750 &VolumeInfo.FvFormat,\r
751 0,\r
752 NULL,\r
753 (VOID**)&FvPpi\r
754 );\r
755 ASSERT_EFI_ERROR (Status);\r
756\r
757 Status = MeasureFvImage ((EFI_PHYSICAL_ADDRESS) (UINTN) VolumeInfo.FvStart, VolumeInfo.FvSize);\r
758\r
1abfa4ce
JY
759 PERF_END_EX (mFileHandle, "EventRec", "Tcg2Pei", 0, PERF_ID_TCG2_PEI + 1);\r
760\r
d7c054f9 761 return Status;\r
1abfa4ce
JY
762}\r
763\r
764/**\r
d6b926e7 765 Measure and record the Firmware Volume Information once FvInfoPPI install.\r
1abfa4ce
JY
766\r
767 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
768 @param[in] NotifyDescriptor Address of the notification descriptor data structure.\r
769 @param[in] Ppi Address of the PPI that was installed.\r
770\r
771 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.\r
772 @return Others Fail to measure FV.\r
773\r
774**/\r
775EFI_STATUS\r
776EFIAPI\r
d6b926e7 777FirmwareVolumeInfoPpiNotifyCallback (\r
1abfa4ce
JY
778 IN EFI_PEI_SERVICES **PeiServices,\r
779 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,\r
780 IN VOID *Ppi\r
781 )\r
782{\r
783 EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *Fv;\r
784 EFI_STATUS Status;\r
785 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
786 UINTN Index;\r
787\r
788 Fv = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *) Ppi;\r
789\r
790 //\r
791 // The PEI Core can not dispatch or load files from memory mapped FVs that do not support FvPpi.\r
792 //\r
793 Status = PeiServicesLocatePpi (\r
b3548d32
LG
794 &Fv->FvFormat,\r
795 0,\r
1abfa4ce
JY
796 NULL,\r
797 (VOID**)&FvPpi\r
798 );\r
799 if (EFI_ERROR (Status)) {\r
800 return EFI_SUCCESS;\r
801 }\r
b3548d32 802\r
1abfa4ce
JY
803 //\r
804 // This is an FV from an FFS file, and the parent FV must have already been measured,\r
805 // No need to measure twice, so just record the FV and return\r
806 //\r
807 if (Fv->ParentFvName != NULL || Fv->ParentFileName != NULL ) {\r
b3548d32 808\r
aa8c6e2a
SZ
809 if (mMeasuredChildFvIndex >= mMeasuredMaxChildFvIndex) {\r
810 mMeasuredChildFvInfo = ReallocatePool (\r
811 sizeof (EFI_PLATFORM_FIRMWARE_BLOB) * mMeasuredMaxChildFvIndex,\r
812 sizeof (EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredMaxChildFvIndex + FIRMWARE_BLOB_GROWTH_STEP),\r
813 mMeasuredChildFvInfo\r
814 );\r
815 ASSERT (mMeasuredChildFvInfo != NULL);\r
816 mMeasuredMaxChildFvIndex = mMeasuredMaxChildFvIndex + FIRMWARE_BLOB_GROWTH_STEP;\r
817 }\r
818 //\r
819 // Check whether FV is in the measured child FV list.\r
820 //\r
821 for (Index = 0; Index < mMeasuredChildFvIndex; Index++) {\r
822 if (mMeasuredChildFvInfo[Index].BlobBase == (EFI_PHYSICAL_ADDRESS) (UINTN) Fv->FvInfo) {\r
823 return EFI_SUCCESS;\r
1abfa4ce 824 }\r
1abfa4ce 825 }\r
aa8c6e2a
SZ
826 mMeasuredChildFvInfo[mMeasuredChildFvIndex].BlobBase = (EFI_PHYSICAL_ADDRESS) (UINTN) Fv->FvInfo;\r
827 mMeasuredChildFvInfo[mMeasuredChildFvIndex].BlobLength = Fv->FvInfoSize;\r
828 mMeasuredChildFvIndex++;\r
1abfa4ce
JY
829 return EFI_SUCCESS;\r
830 }\r
831\r
832 return MeasureFvImage ((EFI_PHYSICAL_ADDRESS) (UINTN) Fv->FvInfo, Fv->FvInfoSize);\r
833}\r
834\r
835/**\r
836 Do measurement after memory is ready.\r
837\r
838 @param[in] PeiServices Describes the list of possible PEI Services.\r
839\r
840 @retval EFI_SUCCESS Operation completed successfully.\r
841 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.\r
842 @retval EFI_DEVICE_ERROR The command was unsuccessful.\r
843\r
844**/\r
845EFI_STATUS\r
846PeimEntryMP (\r
847 IN EFI_PEI_SERVICES **PeiServices\r
848 )\r
849{\r
850 EFI_STATUS Status;\r
851\r
1abfa4ce
JY
852 if (PcdGet8 (PcdTpm2ScrtmPolicy) == 1) {\r
853 Status = MeasureCRTMVersion ();\r
854 }\r
855\r
856 Status = MeasureMainBios ();\r
d7c054f9
ZC
857 if (EFI_ERROR(Status)) {\r
858 return Status;\r
859 }\r
1abfa4ce
JY
860\r
861 //\r
862 // Post callbacks:\r
863 // for the FvInfoPpi services to measure and record\r
864 // the additional Fvs to TPM\r
865 //\r
866 Status = PeiServicesNotifyPpi (&mNotifyList[0]);\r
867 ASSERT_EFI_ERROR (Status);\r
868\r
869 return Status;\r
870}\r
871\r
c2fe66bf
JY
872/**\r
873 Measure and log Separator event with error, and extend the measurement result into a specific PCR.\r
874\r
b3548d32 875 @param[in] PCRIndex PCR index.\r
c2fe66bf
JY
876\r
877 @retval EFI_SUCCESS Operation completed successfully.\r
878 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
879\r
880**/\r
881EFI_STATUS\r
882MeasureSeparatorEventWithError (\r
883 IN TPM_PCRINDEX PCRIndex\r
884 )\r
885{\r
886 TCG_PCR_EVENT_HDR TcgEvent;\r
887 UINT32 EventData;\r
888\r
889 //\r
890 // Use EventData 0x1 to indicate there is error.\r
891 //\r
892 EventData = 0x1;\r
893 TcgEvent.PCRIndex = PCRIndex;\r
894 TcgEvent.EventType = EV_SEPARATOR;\r
895 TcgEvent.EventSize = (UINT32)sizeof (EventData);\r
896 return HashLogExtendEvent(0,(UINT8 *)&EventData, TcgEvent.EventSize, &TcgEvent,(UINT8 *)&EventData);\r
897}\r
898\r
1abfa4ce
JY
899/**\r
900 Entry point of this module.\r
901\r
902 @param[in] FileHandle Handle of the file being invoked.\r
903 @param[in] PeiServices Describes the list of possible PEI Services.\r
904\r
905 @return Status.\r
906\r
907**/\r
908EFI_STATUS\r
909EFIAPI\r
910PeimEntryMA (\r
911 IN EFI_PEI_FILE_HANDLE FileHandle,\r
912 IN CONST EFI_PEI_SERVICES **PeiServices\r
913 )\r
914{\r
915 EFI_STATUS Status;\r
916 EFI_STATUS Status2;\r
917 EFI_BOOT_MODE BootMode;\r
c2fe66bf
JY
918 TPM_PCRINDEX PcrIndex;\r
919 BOOLEAN S3ErrorReport;\r
1abfa4ce
JY
920\r
921 if (CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceNoneGuid) ||\r
922 CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm12Guid)){\r
f0f1a3cb 923 DEBUG ((DEBUG_INFO, "No TPM2 instance required!\n"));\r
1abfa4ce
JY
924 return EFI_UNSUPPORTED;\r
925 }\r
926\r
927 if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {\r
928 DEBUG ((EFI_D_ERROR, "TPM2 error!\n"));\r
929 return EFI_DEVICE_ERROR;\r
930 }\r
931\r
932 Status = PeiServicesGetBootMode (&BootMode);\r
933 ASSERT_EFI_ERROR (Status);\r
934\r
935 //\r
936 // In S3 path, skip shadow logic. no measurement is required\r
937 //\r
938 if (BootMode != BOOT_ON_S3_RESUME) {\r
939 Status = (**PeiServices).RegisterForShadow(FileHandle);\r
940 if (Status == EFI_ALREADY_STARTED) {\r
941 mImageInMemory = TRUE;\r
942 mFileHandle = FileHandle;\r
943 } else if (Status == EFI_NOT_FOUND) {\r
944 ASSERT_EFI_ERROR (Status);\r
945 }\r
946 }\r
947\r
948 if (!mImageInMemory) {\r
949 //\r
950 // Initialize TPM device\r
951 //\r
952 Status = Tpm2RequestUseTpm ();\r
953 if (EFI_ERROR (Status)) {\r
954 DEBUG ((DEBUG_ERROR, "TPM2 not detected!\n"));\r
955 goto Done;\r
956 }\r
957\r
c2fe66bf 958 S3ErrorReport = FALSE;\r
1abfa4ce
JY
959 if (PcdGet8 (PcdTpm2InitializationPolicy) == 1) {\r
960 if (BootMode == BOOT_ON_S3_RESUME) {\r
961 Status = Tpm2Startup (TPM_SU_STATE);\r
962 if (EFI_ERROR (Status) ) {\r
963 Status = Tpm2Startup (TPM_SU_CLEAR);\r
c2fe66bf
JY
964 if (!EFI_ERROR(Status)) {\r
965 S3ErrorReport = TRUE;\r
966 }\r
1abfa4ce
JY
967 }\r
968 } else {\r
969 Status = Tpm2Startup (TPM_SU_CLEAR);\r
970 }\r
971 if (EFI_ERROR (Status) ) {\r
972 goto Done;\r
973 }\r
974 }\r
b3548d32 975\r
1abfa4ce
JY
976 //\r
977 // Update Tpm2HashMask according to PCR bank.\r
978 //\r
5919a960 979 SyncPcrAllocationsAndPcrMask ();\r
c2fe66bf
JY
980\r
981 if (S3ErrorReport) {\r
982 //\r
983 // The system firmware that resumes from S3 MUST deal with a\r
984 // TPM2_Startup error appropriately.\r
985 // For example, issue a TPM2_Startup(TPM_SU_CLEAR) command and\r
986 // configuring the device securely by taking actions like extending a\r
987 // separator with an error digest (0x01) into PCRs 0 through 7.\r
988 //\r
989 for (PcrIndex = 0; PcrIndex < 8; PcrIndex++) {\r
990 Status = MeasureSeparatorEventWithError (PcrIndex);\r
991 if (EFI_ERROR (Status)) {\r
992 DEBUG ((EFI_D_ERROR, "Separator Event with Error not Measured. Error!\n"));\r
993 }\r
994 }\r
995 }\r
996\r
1abfa4ce
JY
997 //\r
998 // TpmSelfTest is optional on S3 path, skip it to save S3 time\r
999 //\r
1000 if (BootMode != BOOT_ON_S3_RESUME) {\r
1001 if (PcdGet8 (PcdTpm2SelfTestPolicy) == 1) {\r
1002 Status = Tpm2SelfTest (NO);\r
1003 if (EFI_ERROR (Status)) {\r
1004 goto Done;\r
1005 }\r
1006 }\r
1007 }\r
1008\r
1009 //\r
dd40a1f8 1010 // Only install TpmInitializedPpi on success\r
1abfa4ce
JY
1011 //\r
1012 Status = PeiServicesInstallPpi (&mTpmInitializedPpiList);\r
1013 ASSERT_EFI_ERROR (Status);\r
1014 }\r
1015\r
1016 if (mImageInMemory) {\r
1017 Status = PeimEntryMP ((EFI_PEI_SERVICES**)PeiServices);\r
1018 return Status;\r
1019 }\r
1020\r
1021Done:\r
1022 if (EFI_ERROR (Status)) {\r
1023 DEBUG ((EFI_D_ERROR, "TPM2 error! Build Hob\n"));\r
1024 BuildGuidHob (&gTpmErrorHobGuid,0);\r
1025 REPORT_STATUS_CODE (\r
1026 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
1027 (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)\r
1028 );\r
1029 }\r
1030 //\r
dd40a1f8 1031 // Always install TpmInitializationDonePpi no matter success or fail.\r
1abfa4ce
JY
1032 // Other driver can know TPM initialization state by TpmInitializedPpi.\r
1033 //\r
1034 Status2 = PeiServicesInstallPpi (&mTpmInitializationDonePpiList);\r
1035 ASSERT_EFI_ERROR (Status2);\r
1036\r
1037 return Status;\r
1038}\r