]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/Tcg2Pei/Tcg2Pei.c
SecurityPkg: Issues reported by ECC in EDK2.
[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
636 TcgEventHdr.EventType = EV_EFI_PLATFORM_FIRMWARE_BLOB2;\r
637 TcgEventHdr.EventSize = sizeof (FvBlob2);\r
638 EventData = &FvBlob2;\r
639 } else {\r
640 FvBlob.BlobBase = FvBase;\r
641 FvBlob.BlobLength = FvLength;\r
642 TcgEventHdr.PCRIndex = 0;\r
643 TcgEventHdr.EventType = EV_EFI_PLATFORM_FIRMWARE_BLOB;\r
644 TcgEventHdr.EventSize = sizeof (FvBlob);\r
645 EventData = &FvBlob;\r
646 }\r
1abfa4ce 647\r
d7c054f9
ZC
648 if (Tpm2HashMask == 0) {\r
649 //\r
650 // FV pre-hash algos comply with current TPM hash requirement\r
651 // Skip hashing step in measure, only extend DigestList to PCR and log event\r
652 //\r
653 Status = Tpm2PcrExtend(\r
654 0,\r
655 &DigestList\r
656 );\r
657\r
658 if (!EFI_ERROR(Status)) {\r
0286fe81
JY
659 Status = LogHashEvent (&DigestList, &TcgEventHdr, EventData);\r
660 DEBUG ((DEBUG_INFO, "The pre-hashed FV which is extended & logged by Tcg2Pei starts at: 0x%x\n", FvBase));\r
661 DEBUG ((DEBUG_INFO, "The pre-hashed FV which is extended & logged by Tcg2Pei has the size: 0x%x\n", FvLength));\r
d7c054f9
ZC
662 } else if (Status == EFI_DEVICE_ERROR) {\r
663 BuildGuidHob (&gTpmErrorHobGuid,0);\r
664 REPORT_STATUS_CODE (\r
665 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
666 (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)\r
667 );\r
668 }\r
669 } else {\r
670 //\r
671 // Hash the FV, extend digest to the TPM and log TCG event\r
672 //\r
673 Status = HashLogExtendEvent (\r
674 0,\r
0286fe81
JY
675 (UINT8*) (UINTN) FvBase, // HashData\r
676 (UINTN) FvLength, // HashDataLen\r
677 &TcgEventHdr, // EventHdr\r
678 EventData // EventData\r
d7c054f9 679 );\r
0286fe81
JY
680 DEBUG ((DEBUG_INFO, "The FV which is measured by Tcg2Pei starts at: 0x%x\n", FvBase));\r
681 DEBUG ((DEBUG_INFO, "The FV which is measured by Tcg2Pei has the size: 0x%x\n", FvLength));\r
d7c054f9
ZC
682 }\r
683\r
684 if (EFI_ERROR(Status)) {\r
685 DEBUG ((DEBUG_ERROR, "The FV which failed to be measured starts at: 0x%x\n", FvBase));\r
686 return Status;\r
687 }\r
1abfa4ce
JY
688\r
689 //\r
690 // Add new FV into the measured FV list.\r
691 //\r
aa8c6e2a
SZ
692 if (mMeasuredBaseFvIndex >= mMeasuredMaxBaseFvIndex) {\r
693 mMeasuredBaseFvInfo = ReallocatePool (\r
694 sizeof (EFI_PLATFORM_FIRMWARE_BLOB) * mMeasuredMaxBaseFvIndex,\r
695 sizeof (EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredMaxBaseFvIndex + FIRMWARE_BLOB_GROWTH_STEP),\r
696 mMeasuredBaseFvInfo\r
697 );\r
698 ASSERT (mMeasuredBaseFvInfo != NULL);\r
699 mMeasuredMaxBaseFvIndex = mMeasuredMaxBaseFvIndex + FIRMWARE_BLOB_GROWTH_STEP;\r
1abfa4ce
JY
700 }\r
701\r
aa8c6e2a
SZ
702 mMeasuredBaseFvInfo[mMeasuredBaseFvIndex].BlobBase = FvBase;\r
703 mMeasuredBaseFvInfo[mMeasuredBaseFvIndex].BlobLength = FvLength;\r
704 mMeasuredBaseFvIndex++;\r
705\r
1abfa4ce
JY
706 return Status;\r
707}\r
708\r
709/**\r
710 Measure main BIOS.\r
711\r
712 @retval EFI_SUCCESS Operation completed successfully.\r
713 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.\r
714 @retval EFI_DEVICE_ERROR The command was unsuccessful.\r
715\r
716**/\r
717EFI_STATUS\r
718MeasureMainBios (\r
719 VOID\r
720 )\r
721{\r
722 EFI_STATUS Status;\r
1abfa4ce
JY
723 EFI_PEI_FV_HANDLE VolumeHandle;\r
724 EFI_FV_INFO VolumeInfo;\r
725 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
726\r
727 PERF_START_EX (mFileHandle, "EventRec", "Tcg2Pei", 0, PERF_ID_TCG2_PEI);\r
1abfa4ce 728\r
d7c054f9
ZC
729 //\r
730 // Only measure BFV at the very beginning. Other parts of Static Core Root of\r
731 // Trust for Measurement(S-CRTM) will be measured later on FvInfoNotify.\r
732 // BFV is processed without installing FV Info Ppi. Other FVs either inside BFV or\r
733 // reported by platform will be installed with Fv Info Ppi\r
734 // This firmware volume measure policy can be modified/enhanced by special\r
735 // platform for special CRTM TPM measuring.\r
736 //\r
737 Status = PeiServicesFfsFindNextVolume (0, &VolumeHandle);\r
738 ASSERT_EFI_ERROR (Status);\r
739\r
740 //\r
741 // Measure and record the firmware volume that is dispatched by PeiCore\r
742 //\r
743 Status = PeiServicesFfsGetVolumeInfo (VolumeHandle, &VolumeInfo);\r
744 ASSERT_EFI_ERROR (Status);\r
745 //\r
746 // Locate the corresponding FV_PPI according to founded FV's format guid\r
747 //\r
748 Status = PeiServicesLocatePpi (\r
749 &VolumeInfo.FvFormat,\r
750 0,\r
751 NULL,\r
752 (VOID**)&FvPpi\r
753 );\r
754 ASSERT_EFI_ERROR (Status);\r
755\r
756 Status = MeasureFvImage ((EFI_PHYSICAL_ADDRESS) (UINTN) VolumeInfo.FvStart, VolumeInfo.FvSize);\r
757\r
1abfa4ce
JY
758 PERF_END_EX (mFileHandle, "EventRec", "Tcg2Pei", 0, PERF_ID_TCG2_PEI + 1);\r
759\r
d7c054f9 760 return Status;\r
1abfa4ce
JY
761}\r
762\r
763/**\r
d6b926e7 764 Measure and record the Firmware Volume Information once FvInfoPPI install.\r
1abfa4ce
JY
765\r
766 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
767 @param[in] NotifyDescriptor Address of the notification descriptor data structure.\r
768 @param[in] Ppi Address of the PPI that was installed.\r
769\r
770 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.\r
771 @return Others Fail to measure FV.\r
772\r
773**/\r
774EFI_STATUS\r
775EFIAPI\r
d6b926e7 776FirmwareVolumeInfoPpiNotifyCallback (\r
1abfa4ce
JY
777 IN EFI_PEI_SERVICES **PeiServices,\r
778 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,\r
779 IN VOID *Ppi\r
780 )\r
781{\r
782 EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *Fv;\r
783 EFI_STATUS Status;\r
784 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;\r
785 UINTN Index;\r
786\r
787 Fv = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *) Ppi;\r
788\r
789 //\r
790 // The PEI Core can not dispatch or load files from memory mapped FVs that do not support FvPpi.\r
791 //\r
792 Status = PeiServicesLocatePpi (\r
b3548d32
LG
793 &Fv->FvFormat,\r
794 0,\r
1abfa4ce
JY
795 NULL,\r
796 (VOID**)&FvPpi\r
797 );\r
798 if (EFI_ERROR (Status)) {\r
799 return EFI_SUCCESS;\r
800 }\r
b3548d32 801\r
1abfa4ce
JY
802 //\r
803 // This is an FV from an FFS file, and the parent FV must have already been measured,\r
804 // No need to measure twice, so just record the FV and return\r
805 //\r
806 if (Fv->ParentFvName != NULL || Fv->ParentFileName != NULL ) {\r
b3548d32 807\r
aa8c6e2a
SZ
808 if (mMeasuredChildFvIndex >= mMeasuredMaxChildFvIndex) {\r
809 mMeasuredChildFvInfo = ReallocatePool (\r
810 sizeof (EFI_PLATFORM_FIRMWARE_BLOB) * mMeasuredMaxChildFvIndex,\r
811 sizeof (EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredMaxChildFvIndex + FIRMWARE_BLOB_GROWTH_STEP),\r
812 mMeasuredChildFvInfo\r
813 );\r
814 ASSERT (mMeasuredChildFvInfo != NULL);\r
815 mMeasuredMaxChildFvIndex = mMeasuredMaxChildFvIndex + FIRMWARE_BLOB_GROWTH_STEP;\r
816 }\r
817 //\r
818 // Check whether FV is in the measured child FV list.\r
819 //\r
820 for (Index = 0; Index < mMeasuredChildFvIndex; Index++) {\r
821 if (mMeasuredChildFvInfo[Index].BlobBase == (EFI_PHYSICAL_ADDRESS) (UINTN) Fv->FvInfo) {\r
822 return EFI_SUCCESS;\r
1abfa4ce 823 }\r
1abfa4ce 824 }\r
aa8c6e2a
SZ
825 mMeasuredChildFvInfo[mMeasuredChildFvIndex].BlobBase = (EFI_PHYSICAL_ADDRESS) (UINTN) Fv->FvInfo;\r
826 mMeasuredChildFvInfo[mMeasuredChildFvIndex].BlobLength = Fv->FvInfoSize;\r
827 mMeasuredChildFvIndex++;\r
1abfa4ce
JY
828 return EFI_SUCCESS;\r
829 }\r
830\r
831 return MeasureFvImage ((EFI_PHYSICAL_ADDRESS) (UINTN) Fv->FvInfo, Fv->FvInfoSize);\r
832}\r
833\r
834/**\r
835 Do measurement after memory is ready.\r
836\r
837 @param[in] PeiServices Describes the list of possible PEI Services.\r
838\r
839 @retval EFI_SUCCESS Operation completed successfully.\r
840 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.\r
841 @retval EFI_DEVICE_ERROR The command was unsuccessful.\r
842\r
843**/\r
844EFI_STATUS\r
845PeimEntryMP (\r
846 IN EFI_PEI_SERVICES **PeiServices\r
847 )\r
848{\r
849 EFI_STATUS Status;\r
850\r
1abfa4ce
JY
851 if (PcdGet8 (PcdTpm2ScrtmPolicy) == 1) {\r
852 Status = MeasureCRTMVersion ();\r
853 }\r
854\r
855 Status = MeasureMainBios ();\r
d7c054f9
ZC
856 if (EFI_ERROR(Status)) {\r
857 return Status;\r
858 }\r
1abfa4ce
JY
859\r
860 //\r
861 // Post callbacks:\r
862 // for the FvInfoPpi services to measure and record\r
863 // the additional Fvs to TPM\r
864 //\r
865 Status = PeiServicesNotifyPpi (&mNotifyList[0]);\r
866 ASSERT_EFI_ERROR (Status);\r
867\r
868 return Status;\r
869}\r
870\r
c2fe66bf
JY
871/**\r
872 Measure and log Separator event with error, and extend the measurement result into a specific PCR.\r
873\r
b3548d32 874 @param[in] PCRIndex PCR index.\r
c2fe66bf
JY
875\r
876 @retval EFI_SUCCESS Operation completed successfully.\r
877 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
878\r
879**/\r
880EFI_STATUS\r
881MeasureSeparatorEventWithError (\r
882 IN TPM_PCRINDEX PCRIndex\r
883 )\r
884{\r
885 TCG_PCR_EVENT_HDR TcgEvent;\r
886 UINT32 EventData;\r
887\r
888 //\r
889 // Use EventData 0x1 to indicate there is error.\r
890 //\r
891 EventData = 0x1;\r
892 TcgEvent.PCRIndex = PCRIndex;\r
893 TcgEvent.EventType = EV_SEPARATOR;\r
894 TcgEvent.EventSize = (UINT32)sizeof (EventData);\r
895 return HashLogExtendEvent(0,(UINT8 *)&EventData, TcgEvent.EventSize, &TcgEvent,(UINT8 *)&EventData);\r
896}\r
897\r
1abfa4ce
JY
898/**\r
899 Entry point of this module.\r
900\r
901 @param[in] FileHandle Handle of the file being invoked.\r
902 @param[in] PeiServices Describes the list of possible PEI Services.\r
903\r
904 @return Status.\r
905\r
906**/\r
907EFI_STATUS\r
908EFIAPI\r
909PeimEntryMA (\r
910 IN EFI_PEI_FILE_HANDLE FileHandle,\r
911 IN CONST EFI_PEI_SERVICES **PeiServices\r
912 )\r
913{\r
914 EFI_STATUS Status;\r
915 EFI_STATUS Status2;\r
916 EFI_BOOT_MODE BootMode;\r
c2fe66bf
JY
917 TPM_PCRINDEX PcrIndex;\r
918 BOOLEAN S3ErrorReport;\r
1abfa4ce
JY
919\r
920 if (CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceNoneGuid) ||\r
921 CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm12Guid)){\r
f0f1a3cb 922 DEBUG ((DEBUG_INFO, "No TPM2 instance required!\n"));\r
1abfa4ce
JY
923 return EFI_UNSUPPORTED;\r
924 }\r
925\r
926 if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {\r
927 DEBUG ((EFI_D_ERROR, "TPM2 error!\n"));\r
928 return EFI_DEVICE_ERROR;\r
929 }\r
930\r
931 Status = PeiServicesGetBootMode (&BootMode);\r
932 ASSERT_EFI_ERROR (Status);\r
933\r
934 //\r
935 // In S3 path, skip shadow logic. no measurement is required\r
936 //\r
937 if (BootMode != BOOT_ON_S3_RESUME) {\r
938 Status = (**PeiServices).RegisterForShadow(FileHandle);\r
939 if (Status == EFI_ALREADY_STARTED) {\r
940 mImageInMemory = TRUE;\r
941 mFileHandle = FileHandle;\r
942 } else if (Status == EFI_NOT_FOUND) {\r
943 ASSERT_EFI_ERROR (Status);\r
944 }\r
945 }\r
946\r
947 if (!mImageInMemory) {\r
948 //\r
949 // Initialize TPM device\r
950 //\r
951 Status = Tpm2RequestUseTpm ();\r
952 if (EFI_ERROR (Status)) {\r
953 DEBUG ((DEBUG_ERROR, "TPM2 not detected!\n"));\r
954 goto Done;\r
955 }\r
956\r
c2fe66bf 957 S3ErrorReport = FALSE;\r
1abfa4ce
JY
958 if (PcdGet8 (PcdTpm2InitializationPolicy) == 1) {\r
959 if (BootMode == BOOT_ON_S3_RESUME) {\r
960 Status = Tpm2Startup (TPM_SU_STATE);\r
961 if (EFI_ERROR (Status) ) {\r
962 Status = Tpm2Startup (TPM_SU_CLEAR);\r
c2fe66bf
JY
963 if (!EFI_ERROR(Status)) {\r
964 S3ErrorReport = TRUE;\r
965 }\r
1abfa4ce
JY
966 }\r
967 } else {\r
968 Status = Tpm2Startup (TPM_SU_CLEAR);\r
969 }\r
970 if (EFI_ERROR (Status) ) {\r
971 goto Done;\r
972 }\r
973 }\r
b3548d32 974\r
1abfa4ce
JY
975 //\r
976 // Update Tpm2HashMask according to PCR bank.\r
977 //\r
5919a960 978 SyncPcrAllocationsAndPcrMask ();\r
c2fe66bf
JY
979\r
980 if (S3ErrorReport) {\r
981 //\r
982 // The system firmware that resumes from S3 MUST deal with a\r
983 // TPM2_Startup error appropriately.\r
984 // For example, issue a TPM2_Startup(TPM_SU_CLEAR) command and\r
985 // configuring the device securely by taking actions like extending a\r
986 // separator with an error digest (0x01) into PCRs 0 through 7.\r
987 //\r
988 for (PcrIndex = 0; PcrIndex < 8; PcrIndex++) {\r
989 Status = MeasureSeparatorEventWithError (PcrIndex);\r
990 if (EFI_ERROR (Status)) {\r
991 DEBUG ((EFI_D_ERROR, "Separator Event with Error not Measured. Error!\n"));\r
992 }\r
993 }\r
994 }\r
995\r
1abfa4ce
JY
996 //\r
997 // TpmSelfTest is optional on S3 path, skip it to save S3 time\r
998 //\r
999 if (BootMode != BOOT_ON_S3_RESUME) {\r
1000 if (PcdGet8 (PcdTpm2SelfTestPolicy) == 1) {\r
1001 Status = Tpm2SelfTest (NO);\r
1002 if (EFI_ERROR (Status)) {\r
1003 goto Done;\r
1004 }\r
1005 }\r
1006 }\r
1007\r
1008 //\r
dd40a1f8 1009 // Only install TpmInitializedPpi on success\r
1abfa4ce
JY
1010 //\r
1011 Status = PeiServicesInstallPpi (&mTpmInitializedPpiList);\r
1012 ASSERT_EFI_ERROR (Status);\r
1013 }\r
1014\r
1015 if (mImageInMemory) {\r
1016 Status = PeimEntryMP ((EFI_PEI_SERVICES**)PeiServices);\r
1017 return Status;\r
1018 }\r
1019\r
1020Done:\r
1021 if (EFI_ERROR (Status)) {\r
1022 DEBUG ((EFI_D_ERROR, "TPM2 error! Build Hob\n"));\r
1023 BuildGuidHob (&gTpmErrorHobGuid,0);\r
1024 REPORT_STATUS_CODE (\r
1025 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
1026 (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)\r
1027 );\r
1028 }\r
1029 //\r
dd40a1f8 1030 // Always install TpmInitializationDonePpi no matter success or fail.\r
1abfa4ce
JY
1031 // Other driver can know TPM initialization state by TpmInitializedPpi.\r
1032 //\r
1033 Status2 = PeiServicesInstallPpi (&mTpmInitializationDonePpiList);\r
1034 ASSERT_EFI_ERROR (Status2);\r
1035\r
1036 return Status;\r
1037}\r