]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/TcgPei/TcgPei.c
Add security package to repository.
[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 - 2011, 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 Lock physical presence if needed.
383
384 @param[in] PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation
385 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
386 @param[in] Ppi Address of the PPI that was installed.
387
388 @retval EFI_SUCCESS Operation completed successfully.
389 @retval EFI_ABORTED physicalPresenceCMDEnable is locked.
390 @retval EFI_DEVICE_ERROR The command was unsuccessful.
391
392 **/
393 EFI_STATUS
394 EFIAPI
395 PhysicalPresencePpiNotifyCallback (
396 IN EFI_PEI_SERVICES **PeiServices,
397 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
398 IN VOID *Ppi
399 )
400 {
401 EFI_STATUS Status;
402 PEI_LOCK_PHYSICAL_PRESENCE_PPI *LockPhysicalPresencePpi;
403 BOOLEAN LifetimeLock;
404 BOOLEAN CmdEnable;
405 TIS_TPM_HANDLE TpmHandle;
406
407 TpmHandle = (TIS_TPM_HANDLE) (UINTN) TPM_BASE_ADDRESS;
408 LockPhysicalPresencePpi = (PEI_LOCK_PHYSICAL_PRESENCE_PPI *) Ppi;
409
410 if (!LockPhysicalPresencePpi->LockPhysicalPresence ((CONST EFI_PEI_SERVICES**) PeiServices)) {
411 return EFI_SUCCESS;
412 }
413
414 //
415 // Lock TPM physical presence.
416 //
417
418 Status = TpmCommGetCapability (PeiServices, TpmHandle, NULL, &LifetimeLock, &CmdEnable);
419 if (EFI_ERROR (Status)) {
420 return Status;
421 }
422
423 if (!CmdEnable) {
424 if (LifetimeLock) {
425 //
426 // physicalPresenceCMDEnable is locked, can't change.
427 //
428 return EFI_ABORTED;
429 }
430
431 //
432 // Enable physical presence command
433 // It is necessary in order to lock physical presence
434 //
435 Status = TpmCommPhysicalPresence (
436 PeiServices,
437 TpmHandle,
438 TPM_PHYSICAL_PRESENCE_CMD_ENABLE
439 );
440 if (EFI_ERROR (Status)) {
441 return Status;
442 }
443 }
444
445 //
446 // Lock physical presence
447 //
448 Status = TpmCommPhysicalPresence (
449 PeiServices,
450 TpmHandle,
451 TPM_PHYSICAL_PRESENCE_LOCK
452 );
453 return Status;
454 }
455
456 /**
457 Check if TPM chip is activeated or not.
458
459 @param[in] PeiServices Describes the list of possible PEI Services.
460 @param[in] TpmHandle TPM handle.
461
462 @retval TRUE TPM is activated.
463 @retval FALSE TPM is deactivated.
464
465 **/
466 BOOLEAN
467 EFIAPI
468 IsTpmUsable (
469 IN EFI_PEI_SERVICES **PeiServices,
470 IN TIS_TPM_HANDLE TpmHandle
471 )
472 {
473 EFI_STATUS Status;
474 BOOLEAN Deactivated;
475
476 Status = TpmCommGetCapability (PeiServices, TpmHandle, &Deactivated, NULL, NULL);
477 if (EFI_ERROR (Status)) {
478 return FALSE;
479 }
480 return (BOOLEAN)(!Deactivated);
481 }
482
483 /**
484 Do measurement after memory is ready.
485
486 @param[in] PeiServices Describes the list of possible PEI Services.
487
488 @retval EFI_SUCCESS Operation completed successfully.
489 @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
490 @retval EFI_DEVICE_ERROR The command was unsuccessful.
491
492 **/
493 EFI_STATUS
494 EFIAPI
495 PeimEntryMP (
496 IN EFI_PEI_SERVICES **PeiServices
497 )
498 {
499 EFI_STATUS Status;
500 TIS_TPM_HANDLE TpmHandle;
501
502 TpmHandle = (TIS_TPM_HANDLE)(UINTN)TPM_BASE_ADDRESS;
503 Status = TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR)TpmHandle);
504 if (EFI_ERROR (Status)) {
505 return Status;
506 }
507
508 if (IsTpmUsable (PeiServices, TpmHandle)) {
509 Status = MeasureCRTMVersion (PeiServices, TpmHandle);
510 ASSERT_EFI_ERROR (Status);
511
512 Status = MeasureMainBios (PeiServices, TpmHandle);
513 }
514
515 //
516 // Post callbacks:
517 // 1). for the FvInfoPpi services to measure and record
518 // the additional Fvs to TPM
519 // 2). for the OperatorPresencePpi service to determine whether to
520 // lock the TPM
521 //
522 Status = PeiServicesNotifyPpi (&mNotifyList[0]);
523 ASSERT_EFI_ERROR (Status);
524
525 return Status;
526 }
527
528 /**
529 Entry point of this module.
530
531 @param[in] FileHandle Handle of the file being invoked.
532 @param[in] PeiServices Describes the list of possible PEI Services.
533
534 @return Status.
535
536 **/
537 EFI_STATUS
538 EFIAPI
539 PeimEntryMA (
540 IN EFI_PEI_FILE_HANDLE FileHandle,
541 IN CONST EFI_PEI_SERVICES **PeiServices
542 )
543 {
544 EFI_STATUS Status;
545 EFI_BOOT_MODE BootMode;
546 TIS_TPM_HANDLE TpmHandle;
547
548 if (PcdGetBool (PcdHideTpmSupport) && PcdGetBool (PcdHideTpm)) {
549 return EFI_UNSUPPORTED;
550 }
551
552 Status = (**PeiServices).RegisterForShadow(FileHandle);
553 if (Status == EFI_ALREADY_STARTED) {
554 mImageInMemory = TRUE;
555 } else if (Status == EFI_NOT_FOUND) {
556 ASSERT_EFI_ERROR (Status);
557 }
558
559 if (!mImageInMemory) {
560 //
561 // Initialize TPM device
562 //
563 Status = PeiServicesGetBootMode (&BootMode);
564 ASSERT_EFI_ERROR (Status);
565
566 TpmHandle = (TIS_TPM_HANDLE)(UINTN)TPM_BASE_ADDRESS;
567 Status = TisPcRequestUseTpm ((TIS_PC_REGISTERS_PTR)TpmHandle);
568 if (EFI_ERROR (Status)) {
569 DEBUG ((DEBUG_ERROR, "TPM not detected!\n"));
570 return Status;
571 }
572
573 Status = TpmCommStartup ((EFI_PEI_SERVICES**)PeiServices, TpmHandle, BootMode);
574 if (EFI_ERROR (Status) ) {
575 return Status;
576 }
577 Status = TpmCommContinueSelfTest ((EFI_PEI_SERVICES**)PeiServices, TpmHandle);
578 if (EFI_ERROR (Status)) {
579 return Status;
580 }
581 Status = PeiServicesInstallPpi (&mTpmInitializedPpiList);
582 ASSERT_EFI_ERROR (Status);
583 }
584
585 if (mImageInMemory) {
586 Status = PeimEntryMP ((EFI_PEI_SERVICES**)PeiServices);
587 if (EFI_ERROR (Status)) {
588 return Status;
589 }
590 }
591
592 return Status;
593 }