2 The library instance provides security service of TPM measure boot.
4 Copyright (c) 2009 - 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
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.
17 #include <Protocol/TcgService.h>
18 #include <Protocol/FirmwareVolume2.h>
19 #include <Protocol/BlockIo.h>
20 #include <Protocol/DiskIo.h>
21 #include <Protocol/DevicePathToText.h>
23 #include <Library/BaseLib.h>
24 #include <Library/DebugLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/MemoryAllocationLib.h>
27 #include <Library/DevicePathLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/BaseCryptLib.h>
30 #include <Library/PeCoffLib.h>
31 #include <Library/SecurityManagementLib.h>
34 // Flag to check GPT partition. It only need be measured once.
36 BOOLEAN mMeasureGptTableFlag
= FALSE
;
37 EFI_GUID mZeroGuid
= {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
38 UINTN mMeasureGptCount
= 0;
41 Reads contents of a PE/COFF image in memory buffer.
43 @param FileHandle Pointer to the file handle to read the PE/COFF image.
44 @param FileOffset Offset into the PE/COFF image to begin the read operation.
45 @param ReadSize On input, the size in bytes of the requested read operation.
46 On output, the number of bytes actually read.
47 @param Buffer Output buffer that contains the data read from the PE/COFF image.
49 @retval EFI_SUCCESS The specified portion of the PE/COFF image was read and the size
56 IN OUT UINTN
*ReadSize
,
60 CopyMem (Buffer
, (UINT8
*)((UINTN
) FileHandle
+ FileOffset
), *ReadSize
);
65 Measure GPT table data into TPM log.
67 @param TcgProtocol Pointer to the located TCG protocol instance.
68 @param GptHandle Handle that GPT partition was installed.
70 @retval EFI_SUCCESS Successfully measure GPT table.
71 @retval EFI_UNSUPPORTED Not support GPT table on the given handle.
72 @retval EFI_DEVICE_ERROR Can't get GPT table because device error.
73 @retval EFI_OUT_OF_RESOURCES No enough resource to measure GPT table.
74 @retval other error value
79 IN EFI_TCG_PROTOCOL
*TcgProtocol
,
80 IN EFI_HANDLE GptHandle
84 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
85 EFI_DISK_IO_PROTOCOL
*DiskIo
;
86 EFI_PARTITION_TABLE_HEADER
*PrimaryHeader
;
87 EFI_PARTITION_ENTRY
*PartitionEntry
;
89 UINTN NumberOfPartition
;
91 TCG_PCR_EVENT
*TcgEvent
;
92 EFI_GPT_DATA
*GptData
;
95 EFI_PHYSICAL_ADDRESS EventLogLastEntry
;
97 if (mMeasureGptCount
> 0) {
101 Status
= gBS
->HandleProtocol (GptHandle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
102 if (EFI_ERROR (Status
)) {
103 return EFI_UNSUPPORTED
;
105 Status
= gBS
->HandleProtocol (GptHandle
, &gEfiDiskIoProtocolGuid
, (VOID
**)&DiskIo
);
106 if (EFI_ERROR (Status
)) {
107 return EFI_UNSUPPORTED
;
110 // Read the EFI Partition Table Header
112 PrimaryHeader
= (EFI_PARTITION_TABLE_HEADER
*) AllocatePool (BlockIo
->Media
->BlockSize
);
113 if (PrimaryHeader
== NULL
) {
114 return EFI_OUT_OF_RESOURCES
;
116 Status
= DiskIo
->ReadDisk (
118 BlockIo
->Media
->MediaId
,
119 1 * BlockIo
->Media
->BlockSize
,
120 BlockIo
->Media
->BlockSize
,
121 (UINT8
*)PrimaryHeader
123 if (EFI_ERROR (Status
)) {
124 DEBUG ((EFI_D_ERROR
, "Failed to Read Partition Table Header!\n"));
125 FreePool (PrimaryHeader
);
126 return EFI_DEVICE_ERROR
;
129 // Read the partition entry.
131 EntryPtr
= (UINT8
*)AllocatePool (PrimaryHeader
->NumberOfPartitionEntries
* PrimaryHeader
->SizeOfPartitionEntry
);
132 if (EntryPtr
== NULL
) {
133 FreePool (PrimaryHeader
);
134 return EFI_OUT_OF_RESOURCES
;
136 Status
= DiskIo
->ReadDisk (
138 BlockIo
->Media
->MediaId
,
139 MultU64x32(PrimaryHeader
->PartitionEntryLBA
, BlockIo
->Media
->BlockSize
),
140 PrimaryHeader
->NumberOfPartitionEntries
* PrimaryHeader
->SizeOfPartitionEntry
,
143 if (EFI_ERROR (Status
)) {
144 FreePool (PrimaryHeader
);
146 return EFI_DEVICE_ERROR
;
150 // Count the valid partition
152 PartitionEntry
= (EFI_PARTITION_ENTRY
*)EntryPtr
;
153 NumberOfPartition
= 0;
154 for (Index
= 0; Index
< PrimaryHeader
->NumberOfPartitionEntries
; Index
++) {
155 if (!CompareGuid (&PartitionEntry
->PartitionTypeGUID
, &mZeroGuid
)) {
162 // Parepare Data for Measurement
164 EventSize
= (UINT32
)(sizeof (EFI_GPT_DATA
) - sizeof (GptData
->Partitions
)
165 + NumberOfPartition
* PrimaryHeader
->SizeOfPartitionEntry
);
166 TcgEvent
= (TCG_PCR_EVENT
*) AllocateZeroPool (EventSize
+ sizeof (TCG_PCR_EVENT
));
167 if (TcgEvent
== NULL
) {
168 FreePool (PrimaryHeader
);
170 return EFI_OUT_OF_RESOURCES
;
173 TcgEvent
->PCRIndex
= 5;
174 TcgEvent
->EventType
= EV_EFI_GPT_EVENT
;
175 TcgEvent
->EventSize
= EventSize
;
176 GptData
= (EFI_GPT_DATA
*) TcgEvent
->Event
;
179 // Copy the EFI_PARTITION_TABLE_HEADER and NumberOfPartition
181 CopyMem ((UINT8
*)GptData
, (UINT8
*)PrimaryHeader
, sizeof (EFI_PARTITION_TABLE_HEADER
));
182 GptData
->NumberOfPartitions
= NumberOfPartition
;
184 // Copy the valid partition entry
186 PartitionEntry
= (EFI_PARTITION_ENTRY
*)EntryPtr
;
187 NumberOfPartition
= 0;
188 for (Index
= 0; Index
< PrimaryHeader
->NumberOfPartitionEntries
; Index
++) {
189 if (!CompareGuid (&PartitionEntry
->PartitionTypeGUID
, &mZeroGuid
)) {
191 (UINT8
*)&GptData
->Partitions
+ NumberOfPartition
* sizeof (EFI_PARTITION_ENTRY
),
192 (UINT8
*)PartitionEntry
,
193 sizeof (EFI_PARTITION_ENTRY
)
201 // Measure the GPT data
204 Status
= TcgProtocol
->HashLogExtendEvent (
206 (EFI_PHYSICAL_ADDRESS
) (UINTN
) (VOID
*) GptData
,
207 (UINT64
) TcgEvent
->EventSize
,
213 if (!EFI_ERROR (Status
)) {
217 FreePool (PrimaryHeader
);
225 Measure PE image into TPM log based on the authenticode image hashing in
226 PE/COFF Specification 8.0 Appendix A.
228 @param[in] TcgProtocol Pointer to the located TCG protocol instance.
229 @param[in] ImageAddress Start address of image buffer.
230 @param[in] ImageSize Image size
231 @param[in] LinkTimeBase Address that the image is loaded into memory.
232 @param[in] ImageType Image subsystem type.
233 @param[in] FilePath File path is corresponding to the input image.
235 @retval EFI_SUCCESS Successfully measure image.
236 @retval EFI_OUT_OF_RESOURCES No enough resource to measure image.
237 @retval other error value
242 IN EFI_TCG_PROTOCOL
*TcgProtocol
,
243 IN EFI_PHYSICAL_ADDRESS ImageAddress
,
245 IN UINTN LinkTimeBase
,
247 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
251 TCG_PCR_EVENT
*TcgEvent
;
252 EFI_IMAGE_LOAD_EVENT
*ImageLoad
;
256 EFI_IMAGE_DOS_HEADER
*DosHdr
;
257 UINT32 PeCoffHeaderOffset
;
258 EFI_IMAGE_SECTION_HEADER
*Section
;
261 UINTN SumOfBytesHashed
;
262 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
267 EFI_PHYSICAL_ADDRESS EventLogLastEntry
;
268 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
270 Status
= EFI_SUCCESS
;
272 SectionHeader
= NULL
;
274 FilePathSize
= (UINT32
) GetDevicePathSize (FilePath
);
277 // Determine destination PCR by BootPolicy
279 EventSize
= sizeof (*ImageLoad
) - sizeof (ImageLoad
->DevicePath
) + FilePathSize
;
280 TcgEvent
= AllocateZeroPool (EventSize
+ sizeof (TCG_PCR_EVENT
));
281 if (TcgEvent
== NULL
) {
282 return EFI_OUT_OF_RESOURCES
;
285 TcgEvent
->EventSize
= EventSize
;
286 ImageLoad
= (EFI_IMAGE_LOAD_EVENT
*) TcgEvent
->Event
;
289 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
290 TcgEvent
->EventType
= EV_EFI_BOOT_SERVICES_APPLICATION
;
291 TcgEvent
->PCRIndex
= 4;
293 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
294 TcgEvent
->EventType
= EV_EFI_BOOT_SERVICES_DRIVER
;
295 TcgEvent
->PCRIndex
= 2;
297 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
298 TcgEvent
->EventType
= EV_EFI_RUNTIME_SERVICES_DRIVER
;
299 TcgEvent
->PCRIndex
= 2;
304 "TcgMeasurePeImage: Unknown subsystem type %d",
308 TcgEvent
->EventType
= ImageType
;
309 Status
= EFI_UNSUPPORTED
;
313 ImageLoad
->ImageLocationInMemory
= ImageAddress
;
314 ImageLoad
->ImageLengthInMemory
= ImageSize
;
315 ImageLoad
->ImageLinkTimeAddress
= LinkTimeBase
;
316 ImageLoad
->LengthOfDevicePath
= FilePathSize
;
317 CopyMem (ImageLoad
->DevicePath
, FilePath
, FilePathSize
);
320 // Check PE/COFF image
322 DosHdr
= (EFI_IMAGE_DOS_HEADER
*) (UINTN
) ImageAddress
;
323 PeCoffHeaderOffset
= 0;
324 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
325 PeCoffHeaderOffset
= DosHdr
->e_lfanew
;
327 if (((EFI_TE_IMAGE_HEADER
*)((UINT8
*) (UINTN
) ImageAddress
+ PeCoffHeaderOffset
))->Signature
328 == EFI_TE_IMAGE_HEADER_SIGNATURE
) {
333 // PE/COFF Image Measurement
335 // NOTE: The following codes/steps are based upon the authenticode image hashing in
336 // PE/COFF Specification 8.0 Appendix A.
340 // 1. Load the image header into memory.
342 // 2. Initialize a SHA hash context.
343 CtxSize
= Sha1GetContextSize ();
344 Sha1Ctx
= AllocatePool (CtxSize
);
345 if (Sha1Ctx
== NULL
) {
346 Status
= EFI_OUT_OF_RESOURCES
;
353 // Measuring PE/COFF Image Header;
354 // But CheckSum field and SECURITY data directory (certificate) are excluded
356 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINT8
*) (UINTN
) ImageAddress
+ PeCoffHeaderOffset
);
357 Magic
= Hdr
.Pe32
->OptionalHeader
.Magic
;
360 // 3. Calculate the distance from the base of the image header to the image checksum address.
361 // 4. Hash the image header from its base to beginning of the image checksum.
363 HashBase
= (UINT8
*) (UINTN
) ImageAddress
;
364 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
368 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32
->OptionalHeader
.CheckSum
) - HashBase
);
373 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32Plus
->OptionalHeader
.CheckSum
) - HashBase
);
376 Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
379 // 5. Skip over the image checksum (it occupies a single ULONG).
380 // 6. Get the address of the beginning of the Cert Directory.
381 // 7. Hash everything from the end of the checksum to the start of the Cert Directory.
383 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
387 HashBase
= (UINT8
*) &Hdr
.Pe32
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
388 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
393 HashBase
= (UINT8
*) &Hdr
.Pe32Plus
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
394 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
397 Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
400 // 8. Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)
401 // 9. Hash everything from the end of the Cert Directory to the end of image header.
403 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
407 HashBase
= (UINT8
*) &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
408 HashSize
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
-
409 (UINTN
) ((UINT8
*)(&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1]) - (UINT8
*) (UINTN
) ImageAddress
);
414 HashBase
= (UINT8
*) &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
415 HashSize
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
-
416 (UINTN
) ((UINT8
*)(&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1]) - (UINT8
*) (UINTN
) ImageAddress
);
419 Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
422 // 10. Set the SUM_OF_BYTES_HASHED to the size of the header
424 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
428 SumOfBytesHashed
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
433 SumOfBytesHashed
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
437 // 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER
438 // structures in the image. The 'NumberOfSections' field of the image
439 // header indicates how big the table should be. Do not include any
440 // IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.
442 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*)AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER
) * Hdr
.Pe32
->FileHeader
.NumberOfSections
);
443 if (SectionHeader
== NULL
) {
444 Status
= EFI_OUT_OF_RESOURCES
;
449 // 12. Using the 'PointerToRawData' in the referenced section headers as
450 // a key, arrange the elements in the table in ascending order. In other
451 // words, sort the section headers according to the disk-file offset of
454 Section
= (EFI_IMAGE_SECTION_HEADER
*) (
455 (UINT8
*) (UINTN
) ImageAddress
+
458 sizeof(EFI_IMAGE_FILE_HEADER
) +
459 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
461 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
463 while ((Pos
> 0) && (Section
->PointerToRawData
< SectionHeader
[Pos
- 1].PointerToRawData
)) {
464 CopyMem (&SectionHeader
[Pos
], &SectionHeader
[Pos
- 1], sizeof(EFI_IMAGE_SECTION_HEADER
));
467 CopyMem (&SectionHeader
[Pos
], Section
, sizeof(EFI_IMAGE_SECTION_HEADER
));
472 // 13. Walk through the sorted table, bring the corresponding section
473 // into memory, and hash the entire section (using the 'SizeOfRawData'
474 // field in the section header to determine the amount of data to hash).
475 // 14. Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .
476 // 15. Repeat steps 13 and 14 for all the sections in the sorted table.
478 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
479 Section
= (EFI_IMAGE_SECTION_HEADER
*) &SectionHeader
[Index
];
480 if (Section
->SizeOfRawData
== 0) {
483 HashBase
= (UINT8
*) (UINTN
) ImageAddress
+ Section
->PointerToRawData
;
484 HashSize
= (UINTN
) Section
->SizeOfRawData
;
486 Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
488 SumOfBytesHashed
+= HashSize
;
492 // 16. If the file size is greater than SUM_OF_BYTES_HASHED, there is extra
493 // data in the file that needs to be added to the hash. This data begins
494 // at file offset SUM_OF_BYTES_HASHED and its length is:
495 // FileSize - (CertDirectory->Size)
497 if (ImageSize
> SumOfBytesHashed
) {
498 HashBase
= (UINT8
*) (UINTN
) ImageAddress
+ SumOfBytesHashed
;
499 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
503 HashSize
= (UINTN
)(ImageSize
-
504 Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
-
510 HashSize
= (UINTN
)(ImageSize
-
511 Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
-
515 Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
519 // 17. Finalize the SHA hash.
521 Sha1Final (Sha1Ctx
, (UINT8
*)&TcgEvent
->Digest
);
527 Status
= TcgProtocol
->HashLogExtendEvent (
529 (EFI_PHYSICAL_ADDRESS
) (UINTN
) (VOID
*) NULL
,
540 if (SectionHeader
!= NULL
) {
541 FreePool (SectionHeader
);
544 if (Sha1Ctx
!= NULL
) {
551 The security handler is used to abstract platform-specific policy
552 from the DXE core response to an attempt to use a file that returns a
553 given status for the authentication check from the section extraction protocol.
555 The possible responses in a given SAP implementation may include locking
556 flash upon failure to authenticate, attestation logging for all signed drivers,
557 and other exception operations. The File parameter allows for possible logging
558 within the SAP of the driver.
560 If File is NULL, then EFI_INVALID_PARAMETER is returned.
562 If the file specified by File with an authentication status specified by
563 AuthenticationStatus is safe for the DXE Core to use, then EFI_SUCCESS is returned.
565 If the file specified by File with an authentication status specified by
566 AuthenticationStatus is not safe for the DXE Core to use under any circumstances,
567 then EFI_ACCESS_DENIED is returned.
569 If the file specified by File with an authentication status specified by
570 AuthenticationStatus is not safe for the DXE Core to use right now, but it
571 might be possible to use it at a future time, then EFI_SECURITY_VIOLATION is
574 @param[in, out] AuthenticationStatus This is the authentication status returned
575 from the securitymeasurement services for the
577 @param[in] File This is a pointer to the device path of the file that is
578 being dispatched. This will optionally be used for logging.
579 @param[in] FileBuffer File buffer matches the input file device path.
580 @param[in] FileSize Size of File buffer matches the input file device path.
582 @retval EFI_SUCCESS The file specified by File did authenticate, and the
583 platform policy dictates that the DXE Core may use File.
584 @retval EFI_INVALID_PARAMETER File is NULL.
585 @retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and
586 the platform policy dictates that File should be placed
587 in the untrusted state. A file may be promoted from
588 the untrusted to the trusted state at a future time
589 with a call to the Trust() DXE Service.
590 @retval EFI_ACCESS_DENIED The file specified by File did not authenticate, and
591 the platform policy dictates that File should not be
592 used for any purpose.
597 DxeTpmMeasureBootHandler (
598 IN OUT UINT32 AuthenticationStatus
,
599 IN CONST EFI_DEVICE_PATH_PROTOCOL
*File
,
600 IN VOID
*FileBuffer OPTIONAL
,
601 IN UINTN FileSize OPTIONAL
604 EFI_TCG_PROTOCOL
*TcgProtocol
;
606 TCG_EFI_BOOT_SERVICE_CAPABILITY ProtocolCapability
;
607 UINT32 TCGFeatureFlags
;
608 EFI_PHYSICAL_ADDRESS EventLogLocation
;
609 EFI_PHYSICAL_ADDRESS EventLogLastEntry
;
610 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
611 EFI_DEVICE_PATH_PROTOCOL
*OrigDevicePathNode
;
613 BOOLEAN ApplicationRequired
;
614 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
617 return EFI_INVALID_PARAMETER
;
620 Status
= gBS
->LocateProtocol (&gEfiTcgProtocolGuid
, NULL
, (VOID
**) &TcgProtocol
);
621 if (EFI_ERROR (Status
)) {
623 // TCG protocol is not installed. So, TPM is not present.
624 // Don't do any measurement, and directly return EFI_SUCCESS.
629 ProtocolCapability
.Size
= (UINT8
) sizeof (ProtocolCapability
);
630 Status
= TcgProtocol
->StatusCheck (
637 if (EFI_ERROR (Status
) || ProtocolCapability
.TPMDeactivatedFlag
) {
639 // TPM device doesn't work or activate.
645 // Copy File Device Path
647 OrigDevicePathNode
= DuplicateDevicePath (File
);
648 ASSERT (OrigDevicePathNode
!= NULL
);
651 // 1. Check whether this device path support BlockIo protocol.
652 // Is so, this device path may be a GPT device path.
654 DevicePathNode
= OrigDevicePathNode
;
655 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &DevicePathNode
, &Handle
);
656 if (!EFI_ERROR (Status
) && !mMeasureGptTableFlag
) {
658 // Find the gpt partion on the given devicepath
660 DevicePathNode
= OrigDevicePathNode
;
661 while (!IsDevicePathEnd (DevicePathNode
)) {
663 // Find the Gpt partition
665 if (DevicePathType (DevicePathNode
) == MEDIA_DEVICE_PATH
&&
666 DevicePathSubType (DevicePathNode
) == MEDIA_HARDDRIVE_DP
) {
668 // Check whether it is a gpt partition or not
670 if (((HARDDRIVE_DEVICE_PATH
*) DevicePathNode
)->MBRType
== MBR_TYPE_EFI_PARTITION_TABLE_HEADER
&&
671 ((HARDDRIVE_DEVICE_PATH
*) DevicePathNode
)->SignatureType
== SIGNATURE_TYPE_GUID
) {
674 // Change the partition device path to its parent device path (disk) and get the handle.
676 DevicePathNode
->Type
= END_DEVICE_PATH_TYPE
;
677 DevicePathNode
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
678 DevicePathNode
= OrigDevicePathNode
;
679 Status
= gBS
->LocateDevicePath (
680 &gEfiDiskIoProtocolGuid
,
684 if (!EFI_ERROR (Status
)) {
688 Status
= TcgMeasureGptTable (TcgProtocol
, Handle
);
689 if (!EFI_ERROR (Status
)) {
691 // GPT disk check done.
693 mMeasureGptTableFlag
= TRUE
;
696 FreePool (OrigDevicePathNode
);
697 OrigDevicePathNode
= DuplicateDevicePath (File
);
698 ASSERT (OrigDevicePathNode
!= NULL
);
702 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
707 // 2. Measure PE image.
709 ApplicationRequired
= FALSE
;
712 // Check whether this device path support FV2 protocol.
714 DevicePathNode
= OrigDevicePathNode
;
715 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &DevicePathNode
, &Handle
);
716 if (!EFI_ERROR (Status
)) {
718 // Don't check FV image, and directly return EFI_SUCCESS.
719 // It can be extended to the specific FV authentication according to the different requirement.
721 if (IsDevicePathEnd (DevicePathNode
)) {
725 // The image from Firmware image will not be mearsured.
726 // Current policy doesn't measure PeImage from Firmware if it is driver
727 // If the got PeImage is application, it will be still be measured.
729 ApplicationRequired
= TRUE
;
733 // File is not found.
735 if (FileBuffer
== NULL
) {
736 Status
= EFI_SECURITY_VIOLATION
;
743 DevicePathNode
= OrigDevicePathNode
;
744 ZeroMem (&ImageContext
, sizeof (ImageContext
));
745 ImageContext
.Handle
= (VOID
*) FileBuffer
;
746 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) ImageRead
;
749 // Get information about the image being loaded
751 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
752 if (EFI_ERROR (Status
)) {
754 // The information can't be got from the invalid PeImage
760 // Measure only application if Application flag is set
761 // Measure drivers and applications if Application flag is not set
763 if ((!ApplicationRequired
) ||
764 (ApplicationRequired
&& ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
)) {
766 // Print the image path to be measured.
770 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*DevPathToText
;
771 Status
= gBS
->LocateProtocol (
772 &gEfiDevicePathToTextProtocolGuid
,
774 (VOID
**) &DevPathToText
776 if (!EFI_ERROR (Status
)) {
777 ToText
= DevPathToText
->ConvertDevicePathToText (
782 if (ToText
!= NULL
) {
783 DEBUG ((DEBUG_INFO
, "The measured image path is %s.\n", ToText
));
789 // Measure PE image into TPM log.
791 Status
= TcgMeasurePeImage (
793 (EFI_PHYSICAL_ADDRESS
) (UINTN
) FileBuffer
,
795 (UINTN
) ImageContext
.ImageAddress
,
796 ImageContext
.ImageType
,
802 // Done, free the allocated resource.
805 FreePool (OrigDevicePathNode
);
811 Register the security handler to provide TPM measure boot service.
813 @param ImageHandle ImageHandle of the loaded driver.
814 @param SystemTable Pointer to the EFI System Table.
816 @retval EFI_SUCCESS Register successfully.
817 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.
821 DxeTpmMeasureBootLibConstructor (
822 IN EFI_HANDLE ImageHandle
,
823 IN EFI_SYSTEM_TABLE
*SystemTable
826 return RegisterSecurityHandler (
827 DxeTpmMeasureBootHandler
,
828 EFI_AUTH_OPERATION_MEASURE_IMAGE
| EFI_AUTH_OPERATION_IMAGE_REQUIRED