59BOOLEAN mTcg2MeasureGptTableFlag =
FALSE;
60UINTN mTcg2MeasureGptCount = 0;
95 if ((FileHandle ==
NULL) || (ReadSize ==
NULL) || (Buffer ==
NULL)) {
96 return EFI_INVALID_PARAMETER;
100 return EFI_INVALID_PARAMETER;
103 EndPosition = FileOffset + *ReadSize;
104 if (EndPosition > mTcg2ImageSize) {
105 *ReadSize = (UINT32)(mTcg2ImageSize - FileOffset);
108 if (FileOffset >= mTcg2ImageSize) {
112 CopyMem (Buffer, (UINT8 *)((
UINTN)FileHandle + FileOffset), *ReadSize);
145 UINTN NumberOfPartition;
154 EFI_CC_MR_INDEX MrIndex;
157 if (mTcg2MeasureGptCount > 0) {
161 PrimaryHeader =
NULL;
165 Tcg2Protocol = MeasureBootProtocols->Tcg2Protocol;
166 CcProtocol = MeasureBootProtocols->CcProtocol;
168 if ((Tcg2Protocol ==
NULL) && (CcProtocol ==
NULL)) {
170 return EFI_UNSUPPORTED;
175 return EFI_UNSUPPORTED;
178 Status =
gBS->HandleProtocol (GptHandle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
179 if (EFI_ERROR (Status)) {
180 return EFI_UNSUPPORTED;
183 Status =
gBS->HandleProtocol (GptHandle, &gEfiDiskIoProtocolGuid, (VOID **)&DiskIo);
184 if (EFI_ERROR (Status)) {
185 return EFI_UNSUPPORTED;
192 if (PrimaryHeader ==
NULL) {
193 return EFI_OUT_OF_RESOURCES;
196 Status = DiskIo->ReadDisk (
201 (UINT8 *)PrimaryHeader
204 DEBUG ((DEBUG_ERROR,
"Failed to read Partition Table Header or invalid Partition Table Header!\n"));
206 return EFI_DEVICE_ERROR;
213 if (EFI_ERROR (Status)) {
215 return EFI_BAD_BUFFER_SIZE;
219 if (EntryPtr ==
NULL) {
221 return EFI_OUT_OF_RESOURCES;
224 Status = DiskIo->ReadDisk (
231 if (EFI_ERROR (Status)) {
234 return EFI_DEVICE_ERROR;
241 NumberOfPartition = 0;
254 if (EFI_ERROR (Status)) {
257 return EFI_DEVICE_ERROR;
261 if (EventPtr ==
NULL) {
262 Status = EFI_OUT_OF_RESOURCES;
267 Tcg2Event->Size = TcgEventSize;
269 Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;
270 Tcg2Event->Header.PCRIndex = 5;
271 Tcg2Event->Header.EventType = EV_EFI_GPT_EVENT;
278 GptData->NumberOfPartitions = NumberOfPartition;
283 NumberOfPartition = 0;
288 (UINT8 *)PartitionEntry,
301 if (CcProtocol !=
NULL) {
308 Status = CcProtocol->MapPcrToMrIndex (CcProtocol, Tcg2Event->Header.PCRIndex, &MrIndex);
309 if (EFI_ERROR (Status)) {
310 DEBUG ((DEBUG_ERROR,
"Cannot map PcrIndex(%d) to MrIndex\n", Tcg2Event->Header.PCRIndex));
315 CcEvent->Header.MrIndex = MrIndex;
316 Status = CcProtocol->HashLogExtendEvent (
323 if (!EFI_ERROR (Status)) {
324 mTcg2MeasureGptCount++;
327 DEBUG ((DEBUG_INFO,
"DxeTpm2MeasureBootHandler - Cc MeasureGptTable - %r\n", Status));
328 }
else if (Tcg2Protocol !=
NULL) {
332 Status = Tcg2Protocol->HashLogExtendEvent (
339 if (!EFI_ERROR (Status)) {
340 mTcg2MeasureGptCount++;
343 DEBUG ((DEBUG_INFO,
"DxeTpm2MeasureBootHandler - Tcg2 MeasureGptTable - %r\n", Status));
347 if (PrimaryHeader !=
NULL) {
351 if (EntryPtr !=
NULL) {
355 if (EventPtr !=
NULL) {
402 EFI_CC_MR_INDEX MrIndex;
404 Status = EFI_UNSUPPORTED;
409 Tcg2Protocol = MeasureBootProtocols->Tcg2Protocol;
410 CcProtocol = MeasureBootProtocols->CcProtocol;
412 if ((Tcg2Protocol ==
NULL) && (CcProtocol ==
NULL)) {
414 return EFI_UNSUPPORTED;
419 return EFI_UNSUPPORTED;
424 if (EFI_ERROR (Status)) {
425 return EFI_UNSUPPORTED;
433 if (EventPtr ==
NULL) {
434 return EFI_OUT_OF_RESOURCES;
438 Tcg2Event->Size = EventSize;
440 Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;
444 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:
445 Tcg2Event->Header.EventType = EV_EFI_BOOT_SERVICES_APPLICATION;
446 Tcg2Event->Header.PCRIndex = 4;
448 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
449 Tcg2Event->Header.EventType = EV_EFI_BOOT_SERVICES_DRIVER;
450 Tcg2Event->Header.PCRIndex = 2;
452 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
453 Tcg2Event->Header.EventType = EV_EFI_RUNTIME_SERVICES_DRIVER;
454 Tcg2Event->Header.PCRIndex = 2;
460 "Tcg2MeasurePeImage: Unknown subsystem type %d",
467 ImageLoad->ImageLocationInMemory = ImageAddress;
468 ImageLoad->ImageLengthInMemory = ImageSize;
469 ImageLoad->ImageLinkTimeAddress = LinkTimeBase;
470 ImageLoad->LengthOfDevicePath = FilePathSize;
471 if ((FilePath !=
NULL) && (FilePathSize != 0)) {
472 CopyMem (ImageLoad->DevicePath, FilePath, FilePathSize);
478 if (CcProtocol !=
NULL) {
479 Status = CcProtocol->MapPcrToMrIndex (CcProtocol, Tcg2Event->Header.PCRIndex, &MrIndex);
480 if (EFI_ERROR (Status)) {
481 DEBUG ((DEBUG_ERROR,
"Cannot map PcrIndex(%d) to MrIndex\n", Tcg2Event->Header.PCRIndex));
486 CcEvent->Header.MrIndex = MrIndex;
488 Status = CcProtocol->HashLogExtendEvent (
495 DEBUG ((DEBUG_INFO,
"DxeTpm2MeasureBootHandler - Cc MeasurePeImage - %r\n", Status));
496 }
else if (Tcg2Protocol !=
NULL) {
497 Status = Tcg2Protocol->HashLogExtendEvent (
504 DEBUG ((DEBUG_INFO,
"DxeTpm2MeasureBootHandler - Tcg2 MeasurePeImage - %r\n", Status));
507 if (Status == EFI_VOLUME_FULL) {
517 if (EventPtr !=
NULL) {
547 Status =
gBS->LocateProtocol (&gEfiCcMeasurementProtocolGuid,
NULL, (VOID **)&CcProtocol);
548 if (EFI_ERROR (Status)) {
552 DEBUG ((DEBUG_VERBOSE,
"CcMeasurementProtocol is not installed. - %r\n", Status));
554 ZeroMem (&CcProtocolCapability,
sizeof (CcProtocolCapability));
555 CcProtocolCapability.Size =
sizeof (CcProtocolCapability);
556 Status = CcProtocol->GetCapability (CcProtocol, &CcProtocolCapability);
557 if (EFI_ERROR (Status) || (CcProtocolCapability.CcType.Type == EFI_CC_TYPE_NONE)) {
558 DEBUG ((DEBUG_ERROR,
" CcProtocol->GetCapability returns : %x, %r\n", CcProtocolCapability.CcType.Type, Status));
564 Status =
gBS->LocateProtocol (&gEfiTcg2ProtocolGuid,
NULL, (VOID **)&Tcg2Protocol);
565 if (EFI_ERROR (Status)) {
569 DEBUG ((DEBUG_VERBOSE,
"Tcg2Protocol is not installed. - %r\n", Status));
571 Tcg2ProtocolCapability.Size = (UINT8)
sizeof (Tcg2ProtocolCapability);
572 Status = Tcg2Protocol->GetCapability (Tcg2Protocol, &Tcg2ProtocolCapability);
573 if (EFI_ERROR (Status) || (!Tcg2ProtocolCapability.TPMPresentFlag)) {
577 DEBUG ((DEBUG_ERROR,
"TPMPresentFlag=FALSE %r\n", Status));
582 MeasureBootProtocols->Tcg2Protocol = Tcg2Protocol;
583 MeasureBootProtocols->CcProtocol = CcProtocol;
629 IN UINT32 AuthenticationStatus,
633 IN BOOLEAN BootPolicy
642 BOOLEAN ApplicationRequired;
648 MeasureBootProtocols.Tcg2Protocol =
NULL;
649 MeasureBootProtocols.CcProtocol =
NULL;
653 if (EFI_ERROR (Status)) {
658 DEBUG ((DEBUG_INFO,
"None of Tcg2Protocol/CcMeasurementProtocol is installed.\n"));
665 "Tcg2Protocol = %p, CcMeasurementProtocol = %p\n",
666 MeasureBootProtocols.Tcg2Protocol,
667 MeasureBootProtocols.CcProtocol
680 DevicePathNode = OrigDevicePathNode;
681 Status =
gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &DevicePathNode, &Handle);
682 if (!EFI_ERROR (Status) && !mTcg2MeasureGptTableFlag) {
686 DevicePathNode = OrigDevicePathNode;
687 ASSERT (DevicePathNode !=
NULL);
704 DevicePathNode->Type = END_DEVICE_PATH_TYPE;
705 DevicePathNode->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
706 DevicePathNode = OrigDevicePathNode;
707 Status =
gBS->LocateDevicePath (
708 &gEfiDiskIoProtocolGuid,
712 if (!EFI_ERROR (Status)) {
718 if (!EFI_ERROR (Status)) {
722 mTcg2MeasureGptTableFlag =
TRUE;
728 ASSERT (OrigDevicePathNode !=
NULL);
740 ApplicationRequired =
FALSE;
745 DevicePathNode = OrigDevicePathNode;
746 Status =
gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid, &DevicePathNode, &Handle);
747 if (!EFI_ERROR (Status)) {
762 ApplicationRequired =
TRUE;
764 if ((mTcg2CacheMeasuredHandle != Handle) && (mTcg2MeasuredHobData !=
NULL)) {
770 Status =
gBS->HandleProtocol (
772 &gEfiFirmwareVolumeBlockProtocolGuid,
773 (VOID **)&FvbProtocol
775 TempHandle = FvbProtocol->ParentHandle;
781 Status = FvbProtocol->GetPhysicalAddress (FvbProtocol, &FvAddress);
782 if (EFI_ERROR (Status)) {
786 ApplicationRequired =
FALSE;
788 for (Index = 0; Index < mTcg2MeasuredHobData->Num; Index++) {
789 if (mTcg2MeasuredHobData->MeasuredFvBuf[Index].BlobBase == FvAddress) {
793 mTcg2CacheMeasuredHandle = Handle;
794 ApplicationRequired =
TRUE;
804 if (FileBuffer ==
NULL) {
805 Status = EFI_SECURITY_VIOLATION;
809 mTcg2ImageSize = FileSize;
810 mTcg2FileBuffer = FileBuffer;
815 DevicePathNode = OrigDevicePathNode;
816 ZeroMem (&ImageContext,
sizeof (ImageContext));
817 ImageContext.
Handle = (VOID *)FileBuffer;
824 if (EFI_ERROR (Status)) {
829 Status = EFI_ACCESS_DENIED;
842 if ((!ApplicationRequired) ||
843 (ApplicationRequired && (ImageContext.
ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION)))
855 if (ToText !=
NULL) {
856 DEBUG ((DEBUG_INFO,
"The measured image path is %s.\n", ToText));
866 &MeasureBootProtocols,
879 if (OrigDevicePathNode !=
NULL) {
883 DEBUG ((DEBUG_INFO,
"DxeTpm2MeasureBootHandler - %r\n", Status));
910 if (GuidHob !=
NULL) {
911 mTcg2MeasuredHobData = GET_GUID_HOB_DATA (GuidHob);
VOID *EFIAPI GetFirstGuidHob(IN CONST EFI_GUID *Guid)
UINT64 EFIAPI MultU64x32(IN UINT64 Multiplicand, IN UINT32 Multiplier)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
BOOLEAN EFIAPI IsZeroGuid(IN CONST GUID *Guid)
#define MEDIA_HARDDRIVE_DP
UINT8 EFIAPI DevicePathType(IN CONST VOID *Node)
UINT8 EFIAPI DevicePathSubType(IN CONST VOID *Node)
BOOLEAN EFIAPI IsDevicePathEnd(IN CONST VOID *Node)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI NextDevicePathNode(IN CONST VOID *Node)
CHAR16 *EFIAPI ConvertDevicePathToText(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath, IN BOOLEAN DisplayOnly, IN BOOLEAN AllowShortcuts)
UINTN EFIAPI GetDevicePathSize(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath)
EFI_DEVICE_PATH_PROTOCOL *EFIAPI DuplicateDevicePath(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath)
EFI_STATUS EFIAPI Tcg2MeasureGptTable(IN MEASURE_BOOT_PROTOCOLS *MeasureBootProtocols, IN EFI_HANDLE GptHandle)
EFI_STATUS EFIAPI DxeTpm2MeasureBootLibConstructor(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
EFI_STATUS EFIAPI DxeTpm2MeasureBootHandler(IN UINT32 AuthenticationStatus, IN CONST EFI_DEVICE_PATH_PROTOCOL *File OPTIONAL, IN VOID *FileBuffer, IN UINTN FileSize, IN BOOLEAN BootPolicy)
EFI_STATUS EFIAPI GetMeasureBootProtocols(MEASURE_BOOT_PROTOCOLS *MeasureBootProtocols)
EFI_STATUS EFIAPI Tcg2MeasurePeImage(IN MEASURE_BOOT_PROTOCOLS *MeasureBootProtocols, IN EFI_PHYSICAL_ADDRESS ImageAddress, IN UINTN ImageSize, IN UINTN LinkTimeBase, IN UINT16 ImageType, IN EFI_DEVICE_PATH_PROTOCOL *FilePath)
EFI_STATUS EFIAPI DxeTpm2MeasureBootLibImageRead(IN VOID *FileHandle, IN UINTN FileOffset, IN OUT UINTN *ReadSize, OUT VOID *Buffer)
EFI_STATUS EFIAPI Tpm2SanitizeEfiPartitionTableHeader(IN CONST EFI_PARTITION_TABLE_HEADER *PrimaryHeader, IN CONST EFI_BLOCK_IO_PROTOCOL *BlockIo)
EFI_STATUS EFIAPI Tpm2SanitizePrimaryHeaderAllocationSize(IN CONST EFI_PARTITION_TABLE_HEADER *PrimaryHeader, OUT UINT32 *AllocationSize)
EFI_STATUS Tpm2SanitizePeImageEventSize(IN UINT32 FilePathSize, OUT UINT32 *EventSize)
EFI_STATUS Tpm2SanitizePrimaryHeaderGptEventSize(IN CONST EFI_PARTITION_TABLE_HEADER *PrimaryHeader, IN UINTN NumberOfPartition, OUT UINT32 *EventSize)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define OFFSET_OF(TYPE, Field)
#define DEBUG_CODE_BEGIN()
#define DEBUG(Expression)
RETURN_STATUS(EFIAPI * PE_COFF_LOADER_READ_FILE)(IN VOID *FileHandle, IN UINTN FileOffset, IN OUT UINTN *ReadSize, OUT VOID *Buffer)
RETURN_STATUS EFIAPI PeCoffLoaderGetImageInfo(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
#define EFI_AUTH_OPERATION_IMAGE_REQUIRED
EFI_STATUS EFIAPI RegisterSecurity2Handler(IN SECURITY2_FILE_AUTHENTICATION_HANDLER Security2Handler, IN UINT32 AuthenticationOperation)
VOID EFIAPI Exit(IN EFI_STATUS Status)
UINT64 EFI_PHYSICAL_ADDRESS
EFI_BLOCK_IO_MEDIA * Media
EFI_GUID PartitionTypeGUID
UINT32 SizeOfPartitionEntry
EFI_LBA PartitionEntryLBA
UINT32 NumberOfPartitionEntries
PE_COFF_LOADER_READ_FILE ImageRead
PHYSICAL_ADDRESS ImageAddress