51BOOLEAN mMeasureGptTableFlag =
FALSE;
52UINTN mMeasureGptCount = 0;
87 if ((FileHandle ==
NULL) || (ReadSize ==
NULL) || (Buffer ==
NULL)) {
88 return EFI_INVALID_PARAMETER;
92 return EFI_INVALID_PARAMETER;
95 EndPosition = FileOffset + *ReadSize;
96 if (EndPosition > mTpmImageSize) {
97 *ReadSize = (UINT32)(mTpmImageSize - FileOffset);
100 if (FileOffset >= mTpmImageSize) {
104 CopyMem (Buffer, (UINT8 *)((
UINTN)FileHandle + FileOffset), *ReadSize);
137 UINTN NumberOfPartition;
148 if (mMeasureGptCount > 0) {
152 Status =
gBS->HandleProtocol (GptHandle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo);
153 if (EFI_ERROR (Status)) {
154 return EFI_UNSUPPORTED;
157 Status =
gBS->HandleProtocol (GptHandle, &gEfiDiskIoProtocolGuid, (VOID **)&DiskIo);
158 if (EFI_ERROR (Status)) {
159 return EFI_UNSUPPORTED;
166 if (PrimaryHeader ==
NULL) {
167 return EFI_OUT_OF_RESOURCES;
170 Status = DiskIo->ReadDisk (
175 (UINT8 *)PrimaryHeader
178 DEBUG ((DEBUG_ERROR,
"Failed to read Partition Table Header or invalid Partition Table Header!\n"));
180 return EFI_DEVICE_ERROR;
187 if (EFI_ERROR (Status)) {
189 return EFI_DEVICE_ERROR;
193 if (EntryPtr ==
NULL) {
195 return EFI_OUT_OF_RESOURCES;
198 Status = DiskIo->ReadDisk (
205 if (EFI_ERROR (Status)) {
208 return EFI_DEVICE_ERROR;
215 NumberOfPartition = 0;
229 if (TcgEvent ==
NULL) {
232 return EFI_OUT_OF_RESOURCES;
244 GptData->NumberOfPartitions = NumberOfPartition;
249 NumberOfPartition = 0;
254 (UINT8 *)PartitionEntry,
267 Status = TcgProtocol->HashLogExtendEvent (
276 if (!EFI_ERROR (Status)) {
329 UINT32 PeCoffHeaderOffset;
333 UINTN SumOfBytesHashed;
341 UINT32 NumberOfRvaAndSizes;
345 Status = EFI_UNSUPPORTED;
347 SectionHeader =
NULL;
355 if (EFI_ERROR (Status)) {
356 return EFI_UNSUPPORTED;
360 if (TcgEvent ==
NULL) {
361 return EFI_OUT_OF_RESOURCES;
368 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:
369 TcgEvent->
EventType = EV_EFI_BOOT_SERVICES_APPLICATION;
372 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
373 TcgEvent->
EventType = EV_EFI_BOOT_SERVICES_DRIVER;
376 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
377 TcgEvent->
EventType = EV_EFI_RUNTIME_SERVICES_DRIVER;
384 "TcgMeasurePeImage: Unknown subsystem type %d",
391 ImageLoad->ImageLocationInMemory = ImageAddress;
392 ImageLoad->ImageLengthInMemory = ImageSize;
393 ImageLoad->ImageLinkTimeAddress = LinkTimeBase;
394 ImageLoad->LengthOfDevicePath = FilePathSize;
395 if ((FilePath !=
NULL) && (FilePathSize != 0)) {
396 CopyMem (ImageLoad->DevicePath, FilePath, FilePathSize);
403 PeCoffHeaderOffset = 0;
404 if (DosHdr->
e_magic == EFI_IMAGE_DOS_SIGNATURE) {
405 PeCoffHeaderOffset = DosHdr->
e_lfanew;
409 if (Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {
426 if (Sha1Ctx ==
NULL) {
427 Status = EFI_OUT_OF_RESOURCES;
445 HashBase = (UINT8 *)(
UINTN)ImageAddress;
450 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
451 HashSize = (
UINTN)(&Hdr.Pe32->OptionalHeader.CheckSum) - (
UINTN)HashBase;
456 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
457 HashSize = (
UINTN)(&Hdr.Pe32Plus->OptionalHeader.CheckSum) - (
UINTN)HashBase;
460 HashStatus =
Sha1Update (Sha1Ctx, HashBase, HashSize);
468 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {
477 HashBase = (UINT8 *)&Hdr.Pe32->OptionalHeader.CheckSum + sizeof (UINT32);
478 HashSize = Hdr.Pe32->OptionalHeader.SizeOfHeaders - (
UINTN)(HashBase - ImageAddress);
483 HashBase = (UINT8 *)&Hdr.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);
484 HashSize = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - (
UINTN)(HashBase - ImageAddress);
488 HashStatus =
Sha1Update (Sha1Ctx, HashBase, HashSize);
501 HashBase = (UINT8 *)&Hdr.Pe32->OptionalHeader.CheckSum + sizeof (UINT32);
502 HashSize = (
UINTN)(&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - (
UINTN)HashBase;
507 HashBase = (UINT8 *)&Hdr.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);
508 HashSize = (
UINTN)(&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - (
UINTN)HashBase;
512 HashStatus =
Sha1Update (Sha1Ctx, HashBase, HashSize);
526 HashBase = (UINT8 *)&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];
527 HashSize = Hdr.Pe32->OptionalHeader.SizeOfHeaders - (
UINTN)(HashBase - ImageAddress);
532 HashBase = (UINT8 *)&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];
533 HashSize = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - (
UINTN)(HashBase - ImageAddress);
537 HashStatus =
Sha1Update (Sha1Ctx, HashBase, HashSize);
551 SumOfBytesHashed = Hdr.Pe32->OptionalHeader.SizeOfHeaders;
556 SumOfBytesHashed = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders;
566 if (SectionHeader ==
NULL) {
567 Status = EFI_OUT_OF_RESOURCES;
578 (UINT8 *)(
UINTN)ImageAddress +
582 Hdr.Pe32->FileHeader.SizeOfOptionalHeader
584 for (Index = 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++) {
586 while ((Pos > 0) && (Section->PointerToRawData < SectionHeader[Pos - 1].PointerToRawData)) {
602 for (Index = 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++) {
604 if (Section->SizeOfRawData == 0) {
608 HashBase = (UINT8 *)(
UINTN)ImageAddress + Section->PointerToRawData;
609 HashSize = (
UINTN)Section->SizeOfRawData;
611 HashStatus =
Sha1Update (Sha1Ctx, HashBase, HashSize);
616 SumOfBytesHashed += HashSize;
625 if (ImageSize > SumOfBytesHashed) {
626 HashBase = (UINT8 *)(
UINTN)ImageAddress + SumOfBytesHashed;
628 if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {
635 CertSize = Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size;
640 CertSize = Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size;
644 if (ImageSize > CertSize + SumOfBytesHashed) {
645 HashSize = (
UINTN)(ImageSize - CertSize - SumOfBytesHashed);
647 HashStatus =
Sha1Update (Sha1Ctx, HashBase, HashSize);
651 }
else if (ImageSize < CertSize + SumOfBytesHashed) {
668 Status = TcgProtocol->HashLogExtendEvent (
677 if (Status == EFI_OUT_OF_RESOURCES) {
689 if (SectionHeader !=
NULL) {
693 if (Sha1Ctx !=
NULL ) {
741 IN UINT32 AuthenticationStatus,
745 IN BOOLEAN BootPolicy
751 UINT32 TCGFeatureFlags;
758 BOOLEAN ApplicationRequired;
764 Status =
gBS->LocateProtocol (&gEfiTcgProtocolGuid,
NULL, (VOID **)&TcgProtocol);
765 if (EFI_ERROR (Status)) {
773 ProtocolCapability.Size = (UINT8)
sizeof (ProtocolCapability);
774 Status = TcgProtocol->StatusCheck (
797 DevicePathNode = OrigDevicePathNode;
798 Status =
gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &DevicePathNode, &Handle);
799 if (!EFI_ERROR (Status) && !mMeasureGptTableFlag) {
803 DevicePathNode = OrigDevicePathNode;
804 ASSERT (DevicePathNode !=
NULL);
821 DevicePathNode->Type = END_DEVICE_PATH_TYPE;
822 DevicePathNode->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
823 DevicePathNode = OrigDevicePathNode;
824 Status =
gBS->LocateDevicePath (
825 &gEfiDiskIoProtocolGuid,
829 if (!EFI_ERROR (Status)) {
834 if (!EFI_ERROR (Status)) {
838 mMeasureGptTableFlag =
TRUE;
844 ASSERT (OrigDevicePathNode !=
NULL);
856 ApplicationRequired =
FALSE;
861 DevicePathNode = OrigDevicePathNode;
862 Status =
gBS->LocateDevicePath (&gEfiFirmwareVolumeBlockProtocolGuid, &DevicePathNode, &Handle);
863 if (!EFI_ERROR (Status)) {
878 ApplicationRequired =
TRUE;
880 if ((mCacheMeasuredHandle != Handle) && (mMeasuredHobData !=
NULL)) {
886 Status =
gBS->HandleProtocol (
888 &gEfiFirmwareVolumeBlockProtocolGuid,
889 (VOID **)&FvbProtocol
891 TempHandle = FvbProtocol->ParentHandle;
897 Status = FvbProtocol->GetPhysicalAddress (FvbProtocol, &FvAddress);
898 if (EFI_ERROR (Status)) {
902 ApplicationRequired =
FALSE;
904 for (Index = 0; Index < mMeasuredHobData->Num; Index++) {
905 if (mMeasuredHobData->MeasuredFvBuf[Index].BlobBase == FvAddress) {
909 mCacheMeasuredHandle = Handle;
910 ApplicationRequired =
TRUE;
920 if (FileBuffer ==
NULL) {
921 Status = EFI_SECURITY_VIOLATION;
925 mTpmImageSize = FileSize;
926 mFileBuffer = FileBuffer;
931 DevicePathNode = OrigDevicePathNode;
932 ZeroMem (&ImageContext,
sizeof (ImageContext));
933 ImageContext.
Handle = (VOID *)FileBuffer;
940 if (EFI_ERROR (Status)) {
945 return EFI_ACCESS_DENIED;
958 if ((!ApplicationRequired) ||
959 (ApplicationRequired && (ImageContext.
ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION)))
971 if (ToText !=
NULL) {
972 DEBUG ((DEBUG_INFO,
"The measured image path is %s.\n", ToText));
995 if (OrigDevicePathNode !=
NULL) {
1024 if (GuidHob !=
NULL) {
1025 mMeasuredHobData = GET_GUID_HOB_DATA (GuidHob);
VOID *EFIAPI GetFirstGuidHob(IN CONST EFI_GUID *Guid)
BOOLEAN EFIAPI Sha1Final(IN OUT VOID *Sha1Context, OUT UINT8 *HashValue)
BOOLEAN EFIAPI Sha1Init(OUT VOID *Sha1Context)
UINTN EFIAPI Sha1GetContextSize(VOID)
BOOLEAN EFIAPI Sha1Update(IN OUT VOID *Sha1Context, IN CONST VOID *Data, IN UINTN DataSize)
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 TcgMeasureGptTable(IN EFI_TCG_PROTOCOL *TcgProtocol, IN EFI_HANDLE GptHandle)
EFI_STATUS EFIAPI TcgMeasurePeImage(IN EFI_TCG_PROTOCOL *TcgProtocol, IN EFI_PHYSICAL_ADDRESS ImageAddress, IN UINTN ImageSize, IN UINTN LinkTimeBase, IN UINT16 ImageType, IN EFI_DEVICE_PATH_PROTOCOL *FilePath)
EFI_STATUS EFIAPI DxeTpmMeasureBootHandler(IN UINT32 AuthenticationStatus, IN CONST EFI_DEVICE_PATH_PROTOCOL *File OPTIONAL, IN VOID *FileBuffer, IN UINTN FileSize, IN BOOLEAN BootPolicy)
EFI_STATUS EFIAPI DxeTpmMeasureBootLibConstructor(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
EFI_STATUS EFIAPI DxeTpmMeasureBootLibImageRead(IN VOID *FileHandle, IN UINTN FileOffset, IN OUT UINTN *ReadSize, OUT VOID *Buffer)
EFI_STATUS EFIAPI TpmSanitizePrimaryHeaderAllocationSize(IN CONST EFI_PARTITION_TABLE_HEADER *PrimaryHeader, OUT UINT32 *AllocationSize)
EFI_STATUS TpmSanitizePrimaryHeaderGptEventSize(IN CONST EFI_PARTITION_TABLE_HEADER *PrimaryHeader, IN UINTN NumberOfPartition, OUT UINT32 *EventSize)
EFI_STATUS EFIAPI TpmSanitizeEfiPartitionTableHeader(IN CONST EFI_PARTITION_TABLE_HEADER *PrimaryHeader, IN CONST EFI_BLOCK_IO_PROTOCOL *BlockIo)
EFI_STATUS TpmSanitizePeImageEventSize(IN UINT32 FilePathSize, OUT UINT32 *EventSize)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#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)
#define EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
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)
#define TPM_ALG_SHA
The SHA1 algorithm.
UINT64 EFI_PHYSICAL_ADDRESS
EFI_BLOCK_IO_MEDIA * Media
BOOLEAN TPMDeactivatedFlag
00h = TPM not present.
UINT32 e_lfanew
File address of new exe header.
UINT16 e_magic
Magic number.
EFI_GUID PartitionTypeGUID
UINT32 SizeOfPartitionEntry
EFI_LBA PartitionEntryLBA
UINT32 NumberOfPartitionEntries
PE_COFF_LOADER_READ_FILE ImageRead
PHYSICAL_ADDRESS ImageAddress
UINT8 Event[1]
The event data.
UINT32 EventSize
Size of the event data.
TCG_DIGEST Digest
Value extended into PCRIndex.
TCG_EVENTTYPE EventType
TCG EFI event type.
TCG_PCRINDEX PCRIndex
PCRIndex event extended to.