]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/TcgPei/TcgPei.c
f3f4b3f4c0eeb89d793020e830c78ea1412d188e
[mirror_edk2.git] / SecurityPkg / Tcg / TcgPei / TcgPei.c
1 /** @file
2 Initialize TPM device and measure FVs before handing off control to DXE.
3
4 Copyright (c) 2005 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <PiPei.h>
16
17 #include <IndustryStandard/Tpm12.h>
18 #include <IndustryStandard/UefiTcgPlatform.h>
19 #include <Ppi/FirmwareVolumeInfo.h>
20 #include <Ppi/FirmwareVolumeInfo2.h>
21 #include <Ppi/LockPhysicalPresence.h>
22 #include <Ppi/TpmInitialized.h>
23 #include <Ppi/FirmwareVolume.h>
24 #include <Ppi/EndOfPeiPhase.h>
25 #include <Ppi/FirmwareVolumeInfoMeasurementExcluded.h>
26
27 #include <Guid/TcgEventHob.h>
28 #include <Guid/MeasuredFvHob.h>
29 #include <Guid/TpmInstance.h>
30
31 #include <Library/DebugLib.h>
32 #include <Library/BaseMemoryLib.h>
33 #include <Library/PeiServicesLib.h>
34 #include <Library/PeimEntryPoint.h>
35 #include <Library/TpmCommLib.h>
36 #include <Library/HobLib.h>
37 #include <Library/PcdLib.h>
38 #include <Library/PeiServicesTablePointerLib.h>
39 #include <Library/BaseLib.h>
40
41 #include "TpmComm.h"
42
43 BOOLEAN mImageInMemory = FALSE;
44
45 EFI_PEI_PPI_DESCRIPTOR mTpmInitializedPpiList = {
46 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
47 &gPeiTpmInitializedPpiGuid,
48 NULL
49 };
50
51 EFI_PLATFORM_FIRMWARE_BLOB mMeasuredBaseFvInfo[FixedPcdGet32 (PcdPeiCoreMaxFvSupported)];
52 UINT32 mMeasuredBaseFvIndex = 0;
53
54 EFI_PLATFORM_FIRMWARE_BLOB mMeasuredChildFvInfo[FixedPcdGet32 (PcdPeiCoreMaxFvSupported)];
55 UINT32 mMeasuredChildFvIndex = 0;
56
57 EFI_PEI_FIRMWARE_VOLUME_INFO_MEASUREMENT_EXCLUDED_PPI *mMeasurementExcludedFvPpi;
58
59 /**
60 Lock physical presence if needed.
61
62 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
63 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
64 @param[in] Ppi Address of the PPI that was installed.
65
66 @retval EFI_SUCCESS Operation completed successfully.
67
68 **/
69 EFI_STATUS
70 EFIAPI
71 PhysicalPresencePpiNotifyCallback (
72 IN EFI_PEI_SERVICES **PeiServices,
73 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
74 IN VOID *Ppi
75 );
76
77 /**
78 Measure and record the Firmware Volum Information once FvInfoPPI install.
79
80 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
81 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
82 @param[in] Ppi Address of the PPI that was installed.
83
84 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
85 @return Others Fail to measure FV.
86
87 **/
88 EFI_STATUS
89 EFIAPI
90 FirmwareVolmeInfoPpiNotifyCallback (
91 IN EFI_PEI_SERVICES **PeiServices,
92 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
93 IN VOID *Ppi
94 );
95
96 /**
97 Record all measured Firmware Volum Information into a Guid Hob
98
99 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
100 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
101 @param[in] Ppi Address of the PPI that was installed.
102
103 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
104 @return Others Fail to measure FV.
105
106 **/
107 EFI_STATUS
108 EFIAPI
109 EndofPeiSignalNotifyCallBack (
110 IN EFI_PEI_SERVICES **PeiServices,
111 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
112 IN VOID *Ppi
113 );
114
115 EFI_PEI_NOTIFY_DESCRIPTOR mNotifyList[] = {
116 {
117 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,
118 &gPeiLockPhysicalPresencePpiGuid,
119 PhysicalPresencePpiNotifyCallback
120 },
121 {
122 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,
123 &gEfiPeiFirmwareVolumeInfoPpiGuid,
124 FirmwareVolmeInfoPpiNotifyCallback
125 },
126 {
127 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,
128 &gEfiPeiFirmwareVolumeInfo2PpiGuid,
129 FirmwareVolmeInfoPpiNotifyCallback
130 },
131 {
132 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
133 &gEfiEndOfPeiSignalPpiGuid,
134 EndofPeiSignalNotifyCallBack
135 }
136 };
137
138 /**
139 Record all measured Firmware Volum Information into a Guid Hob
140 Guid Hob payload layout is
141
142 UINT32 *************************** FIRMWARE_BLOB number
143 EFI_PLATFORM_FIRMWARE_BLOB******** BLOB Array
144
145 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
146 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
147 @param[in] Ppi Address of the PPI that was installed.
148
149 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
150 @return Others Fail to measure FV.
151
152 **/
153 EFI_STATUS
154 EFIAPI
155 EndofPeiSignalNotifyCallBack (
156 IN EFI_PEI_SERVICES **PeiServices,
157 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
158 IN VOID *Ppi
159 )
160 {
161 MEASURED_HOB_DATA *MeasuredHobData;
162
163 MeasuredHobData = NULL;
164
165 //
166 // Create a Guid hob to save all measured Fv
167 //
168 MeasuredHobData = BuildGuidHob(
169 &gMeasuredFvHobGuid,
170 sizeof(UINTN) + sizeof(EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredBaseFvIndex + mMeasuredChildFvIndex)
171 );
172
173 if (MeasuredHobData != NULL){
174 //
175 // Save measured FV info enty number
176 //
177 MeasuredHobData->Num = mMeasuredBaseFvIndex + mMeasuredChildFvIndex;
178
179 //
180 // Save measured base Fv info
181 //
182 CopyMem (MeasuredHobData->MeasuredFvBuf, mMeasuredBaseFvInfo, sizeof(EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredBaseFvIndex));
183
184 //
185 // Save measured child Fv info
186 //
187 CopyMem (&MeasuredHobData->MeasuredFvBuf[mMeasuredBaseFvIndex] , mMeasuredChildFvInfo, sizeof(EFI_PLATFORM_FIRMWARE_BLOB) * (mMeasuredChildFvIndex));
188 }
189
190 return EFI_SUCCESS;
191 }
192
193 /**
194 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
195 and build a GUIDed HOB recording the event which will be passed to the DXE phase and
196 added into the Event Log.
197
198 @param[in] PeiServices Describes the list of possible PEI Services.
199 @param[in] HashData Physical address of the start of the data buffer
200 to be hashed, extended, and logged.
201 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData.
202 @param[in] TpmHandle TPM handle.
203 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
204 @param[in] NewEventData Pointer to the new event data.
205
206 @retval EFI_SUCCESS Operation completed successfully.
207 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
208 @retval EFI_DEVICE_ERROR The command was unsuccessful.
209
210 **/
211 EFI_STATUS
212 HashLogExtendEvent (
213 IN EFI_PEI_SERVICES **PeiServices,
214 IN UINT8 *HashData,
215 IN UINTN HashDataLen,
216 IN TIS_TPM_HANDLE TpmHandle,
217 IN TCG_PCR_EVENT_HDR *NewEventHdr,
218 IN UINT8 *NewEventData
219 )
220 {
221 EFI_STATUS Status;
222 VOID *HobData;
223
224 HobData = NULL;
225 if (HashDataLen != 0) {
226 Status = TpmCommHashAll (
227 HashData,
228 HashDataLen,
229 &NewEventHdr->Digest
230 );
231 ASSERT_EFI_ERROR (Status);
232 }
233
234 Status = TpmCommExtend (
235 PeiServices,
236 TpmHandle,
237 &NewEventHdr->Digest,
238 NewEventHdr->PCRIndex,
239 NULL
240 );
241 ASSERT_EFI_ERROR (Status);
242
243 HobData = BuildGuidHob (
244 &gTcgEventEntryHobGuid,
245 sizeof (*NewEventHdr) + NewEventHdr->EventSize
246 );
247 if (HobData == NULL) {
248 return EFI_OUT_OF_RESOURCES;
249 }
250
251 CopyMem (HobData, NewEventHdr, sizeof (*NewEventHdr));
252 HobData = (VOID *) ((UINT8*)HobData + sizeof (*NewEventHdr));
253 CopyMem (HobData, NewEventData, NewEventHdr->EventSize);
254 return EFI_SUCCESS;
255 }
256
257 /**
258 Measure CRTM version.
259
260 @param[in] PeiServices Describes the list of possible PEI Services.
261 @param[in] TpmHandle TPM handle.
262
263 @retval EFI_SUCCESS Operation completed successfully.
264 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
265 @retval EFI_DEVICE_ERROR The command was unsuccessful.
266
267 **/
268 EFI_STATUS
269 EFIAPI
270 MeasureCRTMVersion (
271 IN EFI_PEI_SERVICES **PeiServices,
272 IN TIS_TPM_HANDLE TpmHandle
273 )
274 {
275 TCG_PCR_EVENT_HDR TcgEventHdr;
276
277 //
278 // Use FirmwareVersion string to represent CRTM version.
279 // OEMs should get real CRTM version string and measure it.
280 //
281
282 TcgEventHdr.PCRIndex = 0;
283 TcgEventHdr.EventType = EV_S_CRTM_VERSION;
284 TcgEventHdr.EventSize = (UINT32) StrSize((CHAR16*)PcdGetPtr (PcdFirmwareVersionString));
285
286 return HashLogExtendEvent (
287 PeiServices,
288 (UINT8*)PcdGetPtr (PcdFirmwareVersionString),
289 TcgEventHdr.EventSize,
290 TpmHandle,
291 &TcgEventHdr,
292 (UINT8*)PcdGetPtr (PcdFirmwareVersionString)
293 );
294 }
295
296 /**
297 Measure FV image.
298 Add it into the measured FV list after the FV is measured successfully.
299
300 @param[in] FvBase Base address of FV image.
301 @param[in] FvLength Length of FV image.
302
303 @retval EFI_SUCCESS Fv image is measured successfully
304 or it has been already measured.
305 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
306 @retval EFI_DEVICE_ERROR The command was unsuccessful.
307
308 **/
309 EFI_STATUS
310 EFIAPI
311 MeasureFvImage (
312 IN EFI_PHYSICAL_ADDRESS FvBase,
313 IN UINT64 FvLength
314 )
315 {
316 UINT32 Index;
317 EFI_STATUS Status;
318 EFI_PLATFORM_FIRMWARE_BLOB FvBlob;
319 TCG_PCR_EVENT_HDR TcgEventHdr;
320 TIS_TPM_HANDLE TpmHandle;
321
322 TpmHandle = (TIS_TPM_HANDLE) (UINTN) TPM_BASE_ADDRESS;
323
324 //
325 // Check if it is in Excluded FV list
326 //
327 if (mMeasurementExcludedFvPpi != NULL) {
328 for (Index = 0; Index < mMeasurementExcludedFvPpi->Count; Index ++) {
329 if (mMeasurementExcludedFvPpi->Fv[Index].FvBase == FvBase) {
330 DEBUG ((DEBUG_INFO, "The FV which is excluded by TcgPei starts at: 0x%x\n", FvBase));
331 DEBUG ((DEBUG_INFO, "The FV which is excluded by TcgPei has the size: 0x%x\n", FvLength));
332 return EFI_SUCCESS;
333 }
334 }
335 }
336
337 //
338 // Check whether FV is in the measured FV list.
339 //
340 for (Index = 0; Index < mMeasuredBaseFvIndex; Index ++) {
341 if (mMeasuredBaseFvInfo[Index].BlobBase == FvBase) {
342 return EFI_SUCCESS;
343 }
344 }
345
346 //
347 // Measure and record the FV to the TPM
348 //
349 FvBlob.BlobBase = FvBase;
350 FvBlob.BlobLength = FvLength;
351
352 DEBUG ((DEBUG_INFO, "The FV which is measured by TcgPei starts at: 0x%x\n", FvBlob.BlobBase));
353 DEBUG ((DEBUG_INFO, "The FV which is measured by TcgPei has the size: 0x%x\n", FvBlob.BlobLength));
354
355 TcgEventHdr.PCRIndex = 0;
356 TcgEventHdr.EventType = EV_EFI_PLATFORM_FIRMWARE_BLOB;
357 TcgEventHdr.EventSize = sizeof (FvBlob);
358
359 Status = HashLogExtendEvent (
360 (EFI_PEI_SERVICES **) GetPeiServicesTablePointer(),
361 (UINT8*) (UINTN) FvBlob.BlobBase,
362 (UINTN) FvBlob.BlobLength,
363 TpmHandle,
364 &TcgEventHdr,
365 (UINT8*) &FvBlob
366 );
367 ASSERT_EFI_ERROR (Status);
368
369 //
370 // Add new FV into the measured FV list.
371 //
372 ASSERT (mMeasuredBaseFvIndex < FixedPcdGet32 (PcdPeiCoreMaxFvSupported));
373 if (mMeasuredBaseFvIndex < FixedPcdGet32 (PcdPeiCoreMaxFvSupported)) {
374 mMeasuredBaseFvInfo[mMeasuredBaseFvIndex].BlobBase = FvBase;
375 mMeasuredBaseFvInfo[mMeasuredBaseFvIndex].BlobLength = FvLength;
376 mMeasuredBaseFvIndex++;
377 }
378
379 return Status;
380 }
381
382 /**
383 Measure main BIOS.
384
385 @param[in] PeiServices Describes the list of possible PEI Services.
386 @param[in] TpmHandle TPM handle.
387
388 @retval EFI_SUCCESS Operation completed successfully.
389 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
390 @retval EFI_DEVICE_ERROR The command was unsuccessful.
391
392 **/
393 EFI_STATUS
394 EFIAPI
395 MeasureMainBios (
396 IN EFI_PEI_SERVICES **PeiServices,
397 IN TIS_TPM_HANDLE TpmHandle
398 )
399 {
400 EFI_STATUS Status;
401 UINT32 FvInstances;
402 EFI_PEI_FV_HANDLE VolumeHandle;
403 EFI_FV_INFO VolumeInfo;
404 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
405
406 FvInstances = 0;
407 while (TRUE) {
408 //
409 // Traverse all firmware volume instances of Static Core Root of Trust for Measurement
410 // (S-CRTM), this firmware volume measure policy can be modified/enhanced by special
411 // platform for special CRTM TPM measuring.
412 //
413 Status = PeiServicesFfsFindNextVolume (FvInstances, &VolumeHandle);
414 if (EFI_ERROR (Status)) {
415 break;
416 }
417
418 //
419 // Measure and record the firmware volume that is dispatched by PeiCore
420 //
421 Status = PeiServicesFfsGetVolumeInfo (VolumeHandle, &VolumeInfo);
422 ASSERT_EFI_ERROR (Status);
423 //
424 // Locate the corresponding FV_PPI according to founded FV's format guid
425 //
426 Status = PeiServicesLocatePpi (
427 &VolumeInfo.FvFormat,
428 0,
429 NULL,
430 (VOID**)&FvPpi
431 );
432 if (!EFI_ERROR (Status)) {
433 MeasureFvImage ((EFI_PHYSICAL_ADDRESS) (UINTN) VolumeInfo.FvStart, VolumeInfo.FvSize);
434 }
435
436 FvInstances++;
437 }
438
439 return EFI_SUCCESS;
440 }
441
442 /**
443 Measure and record the Firmware Volum Information once FvInfoPPI install.
444
445 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
446 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
447 @param[in] Ppi Address of the PPI that was installed.
448
449 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
450 @return Others Fail to measure FV.
451
452 **/
453 EFI_STATUS
454 EFIAPI
455 FirmwareVolmeInfoPpiNotifyCallback (
456 IN EFI_PEI_SERVICES **PeiServices,
457 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
458 IN VOID *Ppi
459 )
460 {
461 EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *Fv;
462 EFI_STATUS Status;
463 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
464
465 Fv = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *) Ppi;
466
467 //
468 // The PEI Core can not dispatch or load files from memory mapped FVs that do not support FvPpi.
469 //
470 Status = PeiServicesLocatePpi (
471 &Fv->FvFormat,
472 0,
473 NULL,
474 (VOID**)&FvPpi
475 );
476 if (EFI_ERROR (Status)) {
477 return EFI_SUCCESS;
478 }
479
480 //
481 // This is an FV from an FFS file, and the parent FV must have already been measured,
482 // No need to measure twice, so just record the FV and return
483 //
484 if (Fv->ParentFvName != NULL || Fv->ParentFileName != NULL ) {
485
486 ASSERT (mMeasuredChildFvIndex < FixedPcdGet32 (PcdPeiCoreMaxFvSupported));
487 if (mMeasuredChildFvIndex < FixedPcdGet32 (PcdPeiCoreMaxFvSupported)) {
488 mMeasuredChildFvInfo[mMeasuredChildFvIndex].BlobBase = (EFI_PHYSICAL_ADDRESS) (UINTN) Fv->FvInfo;
489 mMeasuredChildFvInfo[mMeasuredChildFvIndex].BlobLength = Fv->FvInfoSize;
490 mMeasuredChildFvIndex++;
491 }
492 return EFI_SUCCESS;
493 }
494
495 return MeasureFvImage ((EFI_PHYSICAL_ADDRESS) (UINTN) Fv->FvInfo, Fv->FvInfoSize);
496 }
497
498 /**
499 Set physicalPresenceLifetimeLock, physicalPresenceHWEnable and physicalPresenceCMDEnable bit by corresponding PCDs.
500 And lock physical presence if needed.
501
502 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
503 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
504 @param[in] Ppi Address of the PPI that was installed.
505
506 @retval EFI_SUCCESS Operation completed successfully.
507 @retval EFI_ABORTED physicalPresenceCMDEnable is locked.
508 @retval EFI_DEVICE_ERROR The command was unsuccessful.
509
510 **/
511 EFI_STATUS
512 EFIAPI
513 PhysicalPresencePpiNotifyCallback (
514 IN EFI_PEI_SERVICES **PeiServices,
515 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
516 IN VOID *Ppi
517 )
518 {
519 EFI_STATUS Status;
520 PEI_LOCK_PHYSICAL_PRESENCE_PPI *LockPhysicalPresencePpi;
521 BOOLEAN LifetimeLock;
522 BOOLEAN CmdEnable;
523 TIS_TPM_HANDLE TpmHandle;
524 TPM_PHYSICAL_PRESENCE PhysicalPresenceValue;
525
526 TpmHandle = (TIS_TPM_HANDLE) (UINTN) TPM_BASE_ADDRESS;
527
528 Status = TpmCommGetCapability (PeiServices, TpmHandle, NULL, &LifetimeLock, &CmdEnable);
529 if (EFI_ERROR (Status)) {
530 return Status;
531 }
532
533 //
534 // 1. Set physicalPresenceLifetimeLock, physicalPresenceHWEnable and physicalPresenceCMDEnable bit by PCDs.
535 //
536 if (PcdGetBool (PcdPhysicalPresenceLifetimeLock) && !LifetimeLock) {
537 //
538 // Lock TPM LifetimeLock is required, and LifetimeLock is not locked yet.
539 //
540 PhysicalPresenceValue = TPM_PHYSICAL_PRESENCE_LIFETIME_LOCK;
541
542 if (PcdGetBool (PcdPhysicalPresenceCmdEnable)) {
543 PhysicalPresenceValue |= TPM_PHYSICAL_PRESENCE_CMD_ENABLE;
544 CmdEnable = TRUE;
545 } else {
546 PhysicalPresenceValue |= TPM_PHYSICAL_PRESENCE_CMD_DISABLE;
547 CmdEnable = FALSE;
548 }
549
550 if (PcdGetBool (PcdPhysicalPresenceHwEnable)) {
551 PhysicalPresenceValue |= TPM_PHYSICAL_PRESENCE_HW_ENABLE;
552 } else {
553 PhysicalPresenceValue |= TPM_PHYSICAL_PRESENCE_HW_DISABLE;
554 }
555
556 Status = TpmCommPhysicalPresence (
557 PeiServices,
558 TpmHandle,
559 PhysicalPresenceValue
560 );
561 if (EFI_ERROR (Status)) {
562 return Status;
563 }
564 }
565
566 //
567 // 2. Lock physical presence if it is required.
568 //
569 LockPhysicalPresencePpi = (PEI_LOCK_PHYSICAL_PRESENCE_PPI *) Ppi;
570 if (!LockPhysicalPresencePpi->LockPhysicalPresence ((CONST EFI_PEI_SERVICES**) PeiServices)) {
571 return EFI_SUCCESS;
572 }
573
574 if (!CmdEnable) {
575 if (LifetimeLock) {
576 //
577 // physicalPresenceCMDEnable is locked, can't change.
578 //
579 return EFI_ABORTED;
580 }
581
582 //
583 // Enable physical presence command
584 // It is necessary in order to lock physical presence
585 //
586 Status = TpmCommPhysicalPresence (
587 PeiServices,
588 TpmHandle,
589 TPM_PHYSICAL_PRESENCE_CMD_ENABLE
590 );
591 if (EFI_ERROR (Status)) {
592 return Status;
593 }
594 }
595
596 //
597 // Lock physical presence
598 //
599 Status = TpmCommPhysicalPresence (
600 PeiServices,
601 TpmHandle,
602 TPM_PHYSICAL_PRESENCE_LOCK
603 );
604 return Status;
605 }
606
607 /**
608 Check if TPM chip is activeated or not.
609
610 @param[in] PeiServices Describes the list of possible PEI Services.
611 @param[in] TpmHandle TPM handle.
612
613 @retval TRUE TPM is activated.
614 @retval FALSE TPM is deactivated.
615
616 **/
617 BOOLEAN
618 EFIAPI
619 IsTpmUsable (
620 IN EFI_PEI_SERVICES **PeiServices,
621 IN TIS_TPM_HANDLE TpmHandle
622 )
623 {
624 EFI_STATUS Status;
625 BOOLEAN Deactivated;
626
627 Status = TpmCommGetCapability (PeiServices, TpmHandle, &Deactivated, NULL, NULL);
628 if (EFI_ERROR (Status)) {
629 return FALSE;
630 }
631 return (BOOLEAN)(!Deactivated);
632 }
633
634 /**
635 Do measurement after memory is ready.
636
637 @param[in] PeiServices Describes the list of possible PEI Services.
638
639 @retval EFI_SUCCESS Operation completed successfully.
640 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
641 @retval EFI_DEVICE_ERROR The command was unsuccessful.
642
643 **/
644 EFI_STATUS
645 EFIAPI
646 PeimEntryMP (
647 IN EFI_PEI_SERVICES **PeiServices
648 )
649 {
650 EFI_STATUS Status;
651 TIS_TPM_HANDLE TpmHandle;
652
653 Status = PeiServicesLocatePpi (
654 &gEfiPeiFirmwareVolumeInfoMeasurementExcludedPpiGuid,
655 0,
656 NULL,
657 (VOID**)&mMeasurementExcludedFvPpi
658 );
659 // Do not check status, because it is optional
660
661 TpmHandle = (TIS_TPM_HANDLE)(UINTN)TPM_BASE_ADDRESS;
662 Status = TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR)TpmHandle);
663 if (EFI_ERROR (Status)) {
664 return Status;
665 }
666
667 if (IsTpmUsable (PeiServices, TpmHandle)) {
668 if (PcdGet8 (PcdTpmScrtmPolicy) == 1) {
669 Status = MeasureCRTMVersion (PeiServices, TpmHandle);
670 ASSERT_EFI_ERROR (Status);
671 }
672
673 Status = MeasureMainBios (PeiServices, TpmHandle);
674 }
675
676 //
677 // Post callbacks:
678 // 1). for the FvInfoPpi services to measure and record
679 // the additional Fvs to TPM
680 // 2). for the OperatorPresencePpi service to determine whether to
681 // lock the TPM
682 //
683 Status = PeiServicesNotifyPpi (&mNotifyList[0]);
684 ASSERT_EFI_ERROR (Status);
685
686 return Status;
687 }
688
689 /**
690 Entry point of this module.
691
692 @param[in] FileHandle Handle of the file being invoked.
693 @param[in] PeiServices Describes the list of possible PEI Services.
694
695 @return Status.
696
697 **/
698 EFI_STATUS
699 EFIAPI
700 PeimEntryMA (
701 IN EFI_PEI_FILE_HANDLE FileHandle,
702 IN CONST EFI_PEI_SERVICES **PeiServices
703 )
704 {
705 EFI_STATUS Status;
706 EFI_BOOT_MODE BootMode;
707 TIS_TPM_HANDLE TpmHandle;
708
709 if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm12Guid)){
710 DEBUG ((EFI_D_ERROR, "No TPM12 instance required!\n"));
711 return EFI_UNSUPPORTED;
712 }
713
714 //
715 // Initialize TPM device
716 //
717 Status = PeiServicesGetBootMode (&BootMode);
718 ASSERT_EFI_ERROR (Status);
719
720 //
721 // In S3 path, skip shadow logic. no measurement is required
722 //
723 if (BootMode != BOOT_ON_S3_RESUME) {
724 Status = (**PeiServices).RegisterForShadow(FileHandle);
725 if (Status == EFI_ALREADY_STARTED) {
726 mImageInMemory = TRUE;
727 } else if (Status == EFI_NOT_FOUND) {
728 ASSERT_EFI_ERROR (Status);
729 }
730 }
731
732 if (!mImageInMemory) {
733 TpmHandle = (TIS_TPM_HANDLE)(UINTN)TPM_BASE_ADDRESS;
734 Status = TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR)TpmHandle);
735 if (EFI_ERROR (Status)) {
736 DEBUG ((DEBUG_ERROR, "TPM not detected!\n"));
737 return Status;
738 }
739
740 if (PcdGet8 (PcdTpmInitializationPolicy) == 1) {
741 Status = TpmCommStartup ((EFI_PEI_SERVICES**)PeiServices, TpmHandle, BootMode);
742 if (EFI_ERROR (Status) ) {
743 return Status;
744 }
745 }
746
747 //
748 // TpmSelfTest is optional on S3 path, skip it to save S3 time
749 //
750 if (BootMode != BOOT_ON_S3_RESUME) {
751 Status = TpmCommContinueSelfTest ((EFI_PEI_SERVICES**)PeiServices, TpmHandle);
752 if (EFI_ERROR (Status)) {
753 return Status;
754 }
755 }
756
757 Status = PeiServicesInstallPpi (&mTpmInitializedPpiList);
758 ASSERT_EFI_ERROR (Status);
759 }
760
761 if (mImageInMemory) {
762 Status = PeimEntryMP ((EFI_PEI_SERVICES**)PeiServices);
763 if (EFI_ERROR (Status)) {
764 return Status;
765 }
766 }
767
768 return Status;
769 }