]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/TcgPei/TcgPei.c
Enhance TCG driver to provide TPM physical presence lifetime lock capability.
[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 - 2012, 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/LockPhysicalPresence.h>
21 #include <Ppi/TpmInitialized.h>
22 #include <Ppi/FirmwareVolume.h>
23 #include <Guid/TcgEventHob.h>
24 #include <Library/DebugLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/PeiServicesLib.h>
27 #include <Library/PeimEntryPoint.h>
28 #include <Library/TpmCommLib.h>
29 #include <Library/HobLib.h>
30 #include <Library/PcdLib.h>
31 #include <Library/PeiServicesTablePointerLib.h>
32
33 #include "TpmComm.h"
34
35 BOOLEAN mImageInMemory = FALSE;
36
37 EFI_PEI_PPI_DESCRIPTOR mTpmInitializedPpiList = {
38 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
39 &gPeiTpmInitializedPpiGuid,
40 NULL
41 };
42
43 /**
44 Lock physical presence if needed.
45
46 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
47 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
48 @param[in] Ppi Address of the PPI that was installed.
49
50 @retval EFI_SUCCESS Operation completed successfully.
51
52 **/
53 EFI_STATUS
54 EFIAPI
55 PhysicalPresencePpiNotifyCallback (
56 IN EFI_PEI_SERVICES **PeiServices,
57 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
58 IN VOID *Ppi
59 );
60
61 /**
62 Measure and record the Firmware Volum Information once FvInfoPPI install.
63
64 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
65 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
66 @param[in] Ppi Address of the PPI that was installed.
67
68 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
69 @return Others Fail to measure FV.
70
71 **/
72 EFI_STATUS
73 EFIAPI
74 FirmwareVolmeInfoPpiNotifyCallback (
75 IN EFI_PEI_SERVICES **PeiServices,
76 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
77 IN VOID *Ppi
78 );
79
80 EFI_PEI_NOTIFY_DESCRIPTOR mNotifyList[] = {
81 {
82 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,
83 &gPeiLockPhysicalPresencePpiGuid,
84 PhysicalPresencePpiNotifyCallback
85 },
86 {
87 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
88 &gEfiPeiFirmwareVolumeInfoPpiGuid,
89 FirmwareVolmeInfoPpiNotifyCallback
90 }
91 };
92
93 CHAR8 mSCrtmVersion[] = "{D20BC7C6-A1A5-415c-AE85-38290AB6BE04}";
94
95 EFI_PLATFORM_FIRMWARE_BLOB mMeasuredFvInfo[FixedPcdGet32 (PcdPeiCoreMaxFvSupported)];
96 UINT32 mMeasuredFvIndex = 0;
97
98 /**
99 Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
100 and build a GUIDed HOB recording the event which will be passed to the DXE phase and
101 added into the Event Log.
102
103 @param[in] PeiServices Describes the list of possible PEI Services.
104 @param[in] HashData Physical address of the start of the data buffer
105 to be hashed, extended, and logged.
106 @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData.
107 @param[in] TpmHandle TPM handle.
108 @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
109 @param[in] NewEventData Pointer to the new event data.
110
111 @retval EFI_SUCCESS Operation completed successfully.
112 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
113 @retval EFI_DEVICE_ERROR The command was unsuccessful.
114
115 **/
116 EFI_STATUS
117 HashLogExtendEvent (
118 IN EFI_PEI_SERVICES **PeiServices,
119 IN UINT8 *HashData,
120 IN UINTN HashDataLen,
121 IN TIS_TPM_HANDLE TpmHandle,
122 IN TCG_PCR_EVENT_HDR *NewEventHdr,
123 IN UINT8 *NewEventData
124 )
125 {
126 EFI_STATUS Status;
127 VOID *HobData;
128
129 HobData = NULL;
130 if (HashDataLen != 0) {
131 Status = TpmCommHashAll (
132 HashData,
133 HashDataLen,
134 &NewEventHdr->Digest
135 );
136 ASSERT_EFI_ERROR (Status);
137 }
138
139 Status = TpmCommExtend (
140 PeiServices,
141 TpmHandle,
142 &NewEventHdr->Digest,
143 NewEventHdr->PCRIndex,
144 NULL
145 );
146 ASSERT_EFI_ERROR (Status);
147
148 HobData = BuildGuidHob (
149 &gTcgEventEntryHobGuid,
150 sizeof (*NewEventHdr) + NewEventHdr->EventSize
151 );
152 if (HobData == NULL) {
153 return EFI_OUT_OF_RESOURCES;
154 }
155
156 CopyMem (HobData, NewEventHdr, sizeof (*NewEventHdr));
157 HobData = (VOID *) ((UINT8*)HobData + sizeof (*NewEventHdr));
158 CopyMem (HobData, NewEventData, NewEventHdr->EventSize);
159 return EFI_SUCCESS;
160 }
161
162 /**
163 Measure CRTM version.
164
165 @param[in] PeiServices Describes the list of possible PEI Services.
166 @param[in] TpmHandle TPM handle.
167
168 @retval EFI_SUCCESS Operation completed successfully.
169 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
170 @retval EFI_DEVICE_ERROR The command was unsuccessful.
171
172 **/
173 EFI_STATUS
174 EFIAPI
175 MeasureCRTMVersion (
176 IN EFI_PEI_SERVICES **PeiServices,
177 IN TIS_TPM_HANDLE TpmHandle
178 )
179 {
180 TCG_PCR_EVENT_HDR TcgEventHdr;
181
182 //
183 // Here, only a static GUID is measured instead of real CRTM version.
184 // OEMs should get real CRTM version string and measure it.
185 //
186
187 TcgEventHdr.PCRIndex = 0;
188 TcgEventHdr.EventType = EV_S_CRTM_VERSION;
189 TcgEventHdr.EventSize = sizeof (mSCrtmVersion);
190 return HashLogExtendEvent (
191 PeiServices,
192 (UINT8*)&mSCrtmVersion,
193 TcgEventHdr.EventSize,
194 TpmHandle,
195 &TcgEventHdr,
196 (UINT8*)&mSCrtmVersion
197 );
198 }
199
200 /**
201 Measure FV image.
202 Add it into the measured FV list after the FV is measured successfully.
203
204 @param[in] FvBase Base address of FV image.
205 @param[in] FvLength Length of FV image.
206
207 @retval EFI_SUCCESS Fv image is measured successfully
208 or it has been already measured.
209 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
210 @retval EFI_DEVICE_ERROR The command was unsuccessful.
211
212 **/
213 EFI_STATUS
214 EFIAPI
215 MeasureFvImage (
216 IN EFI_PHYSICAL_ADDRESS FvBase,
217 IN UINT64 FvLength
218 )
219 {
220 UINT32 Index;
221 EFI_STATUS Status;
222 EFI_PLATFORM_FIRMWARE_BLOB FvBlob;
223 TCG_PCR_EVENT_HDR TcgEventHdr;
224 TIS_TPM_HANDLE TpmHandle;
225
226 TpmHandle = (TIS_TPM_HANDLE) (UINTN) TPM_BASE_ADDRESS;
227
228 //
229 // Check whether FV is in the measured FV list.
230 //
231 for (Index = 0; Index < mMeasuredFvIndex; Index ++) {
232 if (mMeasuredFvInfo[Index].BlobBase == FvBase) {
233 return EFI_SUCCESS;
234 }
235 }
236
237 //
238 // Measure and record the FV to the TPM
239 //
240 FvBlob.BlobBase = FvBase;
241 FvBlob.BlobLength = FvLength;
242
243 DEBUG ((DEBUG_INFO, "The FV which is measured by TcgPei starts at: 0x%x\n", FvBlob.BlobBase));
244 DEBUG ((DEBUG_INFO, "The FV which is measured by TcgPei has the size: 0x%x\n", FvBlob.BlobLength));
245
246 TcgEventHdr.PCRIndex = 0;
247 TcgEventHdr.EventType = EV_EFI_PLATFORM_FIRMWARE_BLOB;
248 TcgEventHdr.EventSize = sizeof (FvBlob);
249
250 Status = HashLogExtendEvent (
251 (EFI_PEI_SERVICES **) GetPeiServicesTablePointer(),
252 (UINT8*) (UINTN) FvBlob.BlobBase,
253 (UINTN) FvBlob.BlobLength,
254 TpmHandle,
255 &TcgEventHdr,
256 (UINT8*) &FvBlob
257 );
258 ASSERT_EFI_ERROR (Status);
259
260 //
261 // Add new FV into the measured FV list.
262 //
263 ASSERT (mMeasuredFvIndex < FixedPcdGet32 (PcdPeiCoreMaxFvSupported));
264 if (mMeasuredFvIndex < FixedPcdGet32 (PcdPeiCoreMaxFvSupported)) {
265 mMeasuredFvInfo[mMeasuredFvIndex].BlobBase = FvBase;
266 mMeasuredFvInfo[mMeasuredFvIndex++].BlobLength = FvLength;
267 }
268
269 return Status;
270 }
271
272 /**
273 Measure main BIOS.
274
275 @param[in] PeiServices Describes the list of possible PEI Services.
276 @param[in] TpmHandle TPM handle.
277
278 @retval EFI_SUCCESS Operation completed successfully.
279 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
280 @retval EFI_DEVICE_ERROR The command was unsuccessful.
281
282 **/
283 EFI_STATUS
284 EFIAPI
285 MeasureMainBios (
286 IN EFI_PEI_SERVICES **PeiServices,
287 IN TIS_TPM_HANDLE TpmHandle
288 )
289 {
290 EFI_STATUS Status;
291 UINT32 FvInstances;
292 EFI_PEI_FV_HANDLE VolumeHandle;
293 EFI_FV_INFO VolumeInfo;
294 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
295
296 FvInstances = 0;
297 while (TRUE) {
298 //
299 // Traverse all firmware volume instances of Static Core Root of Trust for Measurement
300 // (S-CRTM), this firmware volume measure policy can be modified/enhanced by special
301 // platform for special CRTM TPM measuring.
302 //
303 Status = PeiServicesFfsFindNextVolume (FvInstances, &VolumeHandle);
304 if (EFI_ERROR (Status)) {
305 break;
306 }
307
308 //
309 // Measure and record the firmware volume that is dispatched by PeiCore
310 //
311 Status = PeiServicesFfsGetVolumeInfo (VolumeHandle, &VolumeInfo);
312 ASSERT_EFI_ERROR (Status);
313 //
314 // Locate the corresponding FV_PPI according to founded FV's format guid
315 //
316 Status = PeiServicesLocatePpi (
317 &VolumeInfo.FvFormat,
318 0,
319 NULL,
320 (VOID**)&FvPpi
321 );
322 if (!EFI_ERROR (Status)) {
323 MeasureFvImage ((EFI_PHYSICAL_ADDRESS) (UINTN) VolumeInfo.FvStart, VolumeInfo.FvSize);
324 }
325
326 FvInstances++;
327 }
328
329 return EFI_SUCCESS;
330 }
331
332 /**
333 Measure and record the Firmware Volum Information once FvInfoPPI install.
334
335 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.
336 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
337 @param[in] Ppi Address of the PPI that was installed.
338
339 @retval EFI_SUCCESS The FV Info is measured and recorded to TPM.
340 @return Others Fail to measure FV.
341
342 **/
343 EFI_STATUS
344 EFIAPI
345 FirmwareVolmeInfoPpiNotifyCallback (
346 IN EFI_PEI_SERVICES **PeiServices,
347 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
348 IN VOID *Ppi
349 )
350 {
351 EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *Fv;
352 EFI_STATUS Status;
353 EFI_PEI_FIRMWARE_VOLUME_PPI *FvPpi;
354
355 Fv = (EFI_PEI_FIRMWARE_VOLUME_INFO_PPI *) Ppi;
356
357 //
358 // The PEI Core can not dispatch or load files from memory mapped FVs that do not support FvPpi.
359 //
360 Status = PeiServicesLocatePpi (
361 &Fv->FvFormat,
362 0,
363 NULL,
364 (VOID**)&FvPpi
365 );
366 if (EFI_ERROR (Status)) {
367 return EFI_SUCCESS;
368 }
369
370 //
371 // This is an FV from an FFS file, and the parent FV must have already been measured,
372 // No need to measure twice, so just returns
373 //
374 if (Fv->ParentFvName != NULL || Fv->ParentFileName != NULL ) {
375 return EFI_SUCCESS;
376 }
377
378 return MeasureFvImage ((EFI_PHYSICAL_ADDRESS) (UINTN) Fv->FvInfo, Fv->FvInfoSize);
379 }
380
381 /**
382 Set physicalPresenceLifetimeLock, physicalPresenceHWEnable and physicalPresenceCMDEnable bit by corresponding PCDs.
383 And lock physical presence if needed.
384
385 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
386 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
387 @param[in] Ppi Address of the PPI that was installed.
388
389 @retval EFI_SUCCESS Operation completed successfully.
390 @retval EFI_ABORTED physicalPresenceCMDEnable is locked.
391 @retval EFI_DEVICE_ERROR The command was unsuccessful.
392
393 **/
394 EFI_STATUS
395 EFIAPI
396 PhysicalPresencePpiNotifyCallback (
397 IN EFI_PEI_SERVICES **PeiServices,
398 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
399 IN VOID *Ppi
400 )
401 {
402 EFI_STATUS Status;
403 PEI_LOCK_PHYSICAL_PRESENCE_PPI *LockPhysicalPresencePpi;
404 BOOLEAN LifetimeLock;
405 BOOLEAN CmdEnable;
406 TIS_TPM_HANDLE TpmHandle;
407 TPM_PHYSICAL_PRESENCE PhysicalPresenceValue;
408
409 TpmHandle = (TIS_TPM_HANDLE) (UINTN) TPM_BASE_ADDRESS;
410
411 Status = TpmCommGetCapability (PeiServices, TpmHandle, NULL, &LifetimeLock, &CmdEnable);
412 if (EFI_ERROR (Status)) {
413 return Status;
414 }
415
416 //
417 // 1. Set physicalPresenceLifetimeLock, physicalPresenceHWEnable and physicalPresenceCMDEnable bit by PCDs.
418 //
419 if (PcdGetBool (PcdPhysicalPresenceLifetimeLock) && !LifetimeLock) {
420 //
421 // Lock TPM LifetimeLock is required, and LifetimeLock is not locked yet.
422 //
423 PhysicalPresenceValue = TPM_PHYSICAL_PRESENCE_LIFETIME_LOCK;
424
425 if (PcdGetBool (PcdPhysicalPresenceCmdEnable)) {
426 PhysicalPresenceValue |= TPM_PHYSICAL_PRESENCE_CMD_ENABLE;
427 CmdEnable = TRUE;
428 } else {
429 PhysicalPresenceValue |= TPM_PHYSICAL_PRESENCE_CMD_DISABLE;
430 CmdEnable = FALSE;
431 }
432
433 if (PcdGetBool (PcdPhysicalPresenceHwEnable)) {
434 PhysicalPresenceValue |= TPM_PHYSICAL_PRESENCE_HW_ENABLE;
435 } else {
436 PhysicalPresenceValue |= TPM_PHYSICAL_PRESENCE_HW_DISABLE;
437 }
438
439 Status = TpmCommPhysicalPresence (
440 PeiServices,
441 TpmHandle,
442 PhysicalPresenceValue
443 );
444 if (EFI_ERROR (Status)) {
445 return Status;
446 }
447 }
448
449 //
450 // 2. Lock physical presence if it is required.
451 //
452 LockPhysicalPresencePpi = (PEI_LOCK_PHYSICAL_PRESENCE_PPI *) Ppi;
453 if (!LockPhysicalPresencePpi->LockPhysicalPresence ((CONST EFI_PEI_SERVICES**) PeiServices)) {
454 return EFI_SUCCESS;
455 }
456
457 if (!CmdEnable) {
458 if (LifetimeLock) {
459 //
460 // physicalPresenceCMDEnable is locked, can't change.
461 //
462 return EFI_ABORTED;
463 }
464
465 //
466 // Enable physical presence command
467 // It is necessary in order to lock physical presence
468 //
469 Status = TpmCommPhysicalPresence (
470 PeiServices,
471 TpmHandle,
472 TPM_PHYSICAL_PRESENCE_CMD_ENABLE
473 );
474 if (EFI_ERROR (Status)) {
475 return Status;
476 }
477 }
478
479 //
480 // Lock physical presence
481 //
482 Status = TpmCommPhysicalPresence (
483 PeiServices,
484 TpmHandle,
485 TPM_PHYSICAL_PRESENCE_LOCK
486 );
487 return Status;
488 }
489
490 /**
491 Check if TPM chip is activeated or not.
492
493 @param[in] PeiServices Describes the list of possible PEI Services.
494 @param[in] TpmHandle TPM handle.
495
496 @retval TRUE TPM is activated.
497 @retval FALSE TPM is deactivated.
498
499 **/
500 BOOLEAN
501 EFIAPI
502 IsTpmUsable (
503 IN EFI_PEI_SERVICES **PeiServices,
504 IN TIS_TPM_HANDLE TpmHandle
505 )
506 {
507 EFI_STATUS Status;
508 BOOLEAN Deactivated;
509
510 Status = TpmCommGetCapability (PeiServices, TpmHandle, &Deactivated, NULL, NULL);
511 if (EFI_ERROR (Status)) {
512 return FALSE;
513 }
514 return (BOOLEAN)(!Deactivated);
515 }
516
517 /**
518 Do measurement after memory is ready.
519
520 @param[in] PeiServices Describes the list of possible PEI Services.
521
522 @retval EFI_SUCCESS Operation completed successfully.
523 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
524 @retval EFI_DEVICE_ERROR The command was unsuccessful.
525
526 **/
527 EFI_STATUS
528 EFIAPI
529 PeimEntryMP (
530 IN EFI_PEI_SERVICES **PeiServices
531 )
532 {
533 EFI_STATUS Status;
534 TIS_TPM_HANDLE TpmHandle;
535
536 TpmHandle = (TIS_TPM_HANDLE)(UINTN)TPM_BASE_ADDRESS;
537 Status = TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR)TpmHandle);
538 if (EFI_ERROR (Status)) {
539 return Status;
540 }
541
542 if (IsTpmUsable (PeiServices, TpmHandle)) {
543 Status = MeasureCRTMVersion (PeiServices, TpmHandle);
544 ASSERT_EFI_ERROR (Status);
545
546 Status = MeasureMainBios (PeiServices, TpmHandle);
547 }
548
549 //
550 // Post callbacks:
551 // 1). for the FvInfoPpi services to measure and record
552 // the additional Fvs to TPM
553 // 2). for the OperatorPresencePpi service to determine whether to
554 // lock the TPM
555 //
556 Status = PeiServicesNotifyPpi (&mNotifyList[0]);
557 ASSERT_EFI_ERROR (Status);
558
559 return Status;
560 }
561
562 /**
563 Entry point of this module.
564
565 @param[in] FileHandle Handle of the file being invoked.
566 @param[in] PeiServices Describes the list of possible PEI Services.
567
568 @return Status.
569
570 **/
571 EFI_STATUS
572 EFIAPI
573 PeimEntryMA (
574 IN EFI_PEI_FILE_HANDLE FileHandle,
575 IN CONST EFI_PEI_SERVICES **PeiServices
576 )
577 {
578 EFI_STATUS Status;
579 EFI_BOOT_MODE BootMode;
580 TIS_TPM_HANDLE TpmHandle;
581
582 if (PcdGetBool (PcdHideTpmSupport) && PcdGetBool (PcdHideTpm)) {
583 return EFI_UNSUPPORTED;
584 }
585
586 Status = (**PeiServices).RegisterForShadow(FileHandle);
587 if (Status == EFI_ALREADY_STARTED) {
588 mImageInMemory = TRUE;
589 } else if (Status == EFI_NOT_FOUND) {
590 ASSERT_EFI_ERROR (Status);
591 }
592
593 if (!mImageInMemory) {
594 //
595 // Initialize TPM device
596 //
597 Status = PeiServicesGetBootMode (&BootMode);
598 ASSERT_EFI_ERROR (Status);
599
600 TpmHandle = (TIS_TPM_HANDLE)(UINTN)TPM_BASE_ADDRESS;
601 Status = TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR)TpmHandle);
602 if (EFI_ERROR (Status)) {
603 DEBUG ((DEBUG_ERROR, "TPM not detected!\n"));
604 return Status;
605 }
606
607 Status = TpmCommStartup ((EFI_PEI_SERVICES**)PeiServices, TpmHandle, BootMode);
608 if (EFI_ERROR (Status) ) {
609 return Status;
610 }
611 Status = TpmCommContinueSelfTest ((EFI_PEI_SERVICES**)PeiServices, TpmHandle);
612 if (EFI_ERROR (Status)) {
613 return Status;
614 }
615 Status = PeiServicesInstallPpi (&mTpmInitializedPpiList);
616 ASSERT_EFI_ERROR (Status);
617 }
618
619 if (mImageInMemory) {
620 Status = PeimEntryMP ((EFI_PEI_SERVICES**)PeiServices);
621 if (EFI_ERROR (Status)) {
622 return Status;
623 }
624 }
625
626 return Status;
627 }