2 The library instance provides security service of TPM measure boot.
4 Copyright (c) 2009 - 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
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",
307 Status
= EFI_UNSUPPORTED
;
311 ImageLoad
->ImageLocationInMemory
= ImageAddress
;
312 ImageLoad
->ImageLengthInMemory
= ImageSize
;
313 ImageLoad
->ImageLinkTimeAddress
= LinkTimeBase
;
314 ImageLoad
->LengthOfDevicePath
= FilePathSize
;
315 CopyMem (ImageLoad
->DevicePath
, FilePath
, FilePathSize
);
318 // Check PE/COFF image
320 DosHdr
= (EFI_IMAGE_DOS_HEADER
*) (UINTN
) ImageAddress
;
321 PeCoffHeaderOffset
= 0;
322 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
323 PeCoffHeaderOffset
= DosHdr
->e_lfanew
;
325 if (((EFI_TE_IMAGE_HEADER
*)((UINT8
*) (UINTN
) ImageAddress
+ PeCoffHeaderOffset
))->Signature
326 == EFI_TE_IMAGE_HEADER_SIGNATURE
) {
331 // PE/COFF Image Measurement
333 // NOTE: The following codes/steps are based upon the authenticode image hashing in
334 // PE/COFF Specification 8.0 Appendix A.
338 // 1. Load the image header into memory.
340 // 2. Initialize a SHA hash context.
341 CtxSize
= Sha1GetContextSize ();
342 Sha1Ctx
= AllocatePool (CtxSize
);
343 if (Sha1Ctx
== NULL
) {
344 Status
= EFI_OUT_OF_RESOURCES
;
351 // Measuring PE/COFF Image Header;
352 // But CheckSum field and SECURITY data directory (certificate) are excluded
354 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINT8
*) (UINTN
) ImageAddress
+ PeCoffHeaderOffset
);
355 Magic
= Hdr
.Pe32
->OptionalHeader
.Magic
;
358 // 3. Calculate the distance from the base of the image header to the image checksum address.
359 // 4. Hash the image header from its base to beginning of the image checksum.
361 HashBase
= (UINT8
*) (UINTN
) ImageAddress
;
362 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
366 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32
->OptionalHeader
.CheckSum
) - HashBase
);
371 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32Plus
->OptionalHeader
.CheckSum
) - HashBase
);
374 Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
377 // 5. Skip over the image checksum (it occupies a single ULONG).
378 // 6. Get the address of the beginning of the Cert Directory.
379 // 7. Hash everything from the end of the checksum to the start of the Cert Directory.
381 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
385 HashBase
= (UINT8
*) &Hdr
.Pe32
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
386 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
391 HashBase
= (UINT8
*) &Hdr
.Pe32Plus
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
392 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
395 Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
398 // 8. Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)
399 // 9. Hash everything from the end of the Cert Directory to the end of image header.
401 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
405 HashBase
= (UINT8
*) &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
406 HashSize
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
-
407 (UINTN
) ((UINT8
*)(&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1]) - (UINT8
*) (UINTN
) ImageAddress
);
412 HashBase
= (UINT8
*) &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
413 HashSize
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
-
414 (UINTN
) ((UINT8
*)(&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1]) - (UINT8
*) (UINTN
) ImageAddress
);
417 Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
420 // 10. Set the SUM_OF_BYTES_HASHED to the size of the header
422 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
426 SumOfBytesHashed
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
431 SumOfBytesHashed
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
435 // 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER
436 // structures in the image. The 'NumberOfSections' field of the image
437 // header indicates how big the table should be. Do not include any
438 // IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.
440 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*)AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER
) * Hdr
.Pe32
->FileHeader
.NumberOfSections
);
441 if (SectionHeader
== NULL
) {
442 Status
= EFI_OUT_OF_RESOURCES
;
447 // 12. Using the 'PointerToRawData' in the referenced section headers as
448 // a key, arrange the elements in the table in ascending order. In other
449 // words, sort the section headers according to the disk-file offset of
452 Section
= (EFI_IMAGE_SECTION_HEADER
*) (
453 (UINT8
*) (UINTN
) ImageAddress
+
456 sizeof(EFI_IMAGE_FILE_HEADER
) +
457 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
459 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
461 while ((Pos
> 0) && (Section
->PointerToRawData
< SectionHeader
[Pos
- 1].PointerToRawData
)) {
462 CopyMem (&SectionHeader
[Pos
], &SectionHeader
[Pos
- 1], sizeof(EFI_IMAGE_SECTION_HEADER
));
465 CopyMem (&SectionHeader
[Pos
], Section
, sizeof(EFI_IMAGE_SECTION_HEADER
));
470 // 13. Walk through the sorted table, bring the corresponding section
471 // into memory, and hash the entire section (using the 'SizeOfRawData'
472 // field in the section header to determine the amount of data to hash).
473 // 14. Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .
474 // 15. Repeat steps 13 and 14 for all the sections in the sorted table.
476 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
477 Section
= (EFI_IMAGE_SECTION_HEADER
*) &SectionHeader
[Index
];
478 if (Section
->SizeOfRawData
== 0) {
481 HashBase
= (UINT8
*) (UINTN
) ImageAddress
+ Section
->PointerToRawData
;
482 HashSize
= (UINTN
) Section
->SizeOfRawData
;
484 Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
486 SumOfBytesHashed
+= HashSize
;
490 // 16. If the file size is greater than SUM_OF_BYTES_HASHED, there is extra
491 // data in the file that needs to be added to the hash. This data begins
492 // at file offset SUM_OF_BYTES_HASHED and its length is:
493 // FileSize - (CertDirectory->Size)
495 if (ImageSize
> SumOfBytesHashed
) {
496 HashBase
= (UINT8
*) (UINTN
) ImageAddress
+ SumOfBytesHashed
;
497 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
501 HashSize
= (UINTN
)(ImageSize
-
502 Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
-
508 HashSize
= (UINTN
)(ImageSize
-
509 Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
-
513 Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
517 // 17. Finalize the SHA hash.
519 Sha1Final (Sha1Ctx
, (UINT8
*)&TcgEvent
->Digest
);
525 Status
= TcgProtocol
->HashLogExtendEvent (
527 (EFI_PHYSICAL_ADDRESS
) (UINTN
) (VOID
*) NULL
,
538 if (SectionHeader
!= NULL
) {
539 FreePool (SectionHeader
);
542 if (Sha1Ctx
!= NULL
) {
549 The security handler is used to abstract platform-specific policy
550 from the DXE core response to an attempt to use a file that returns a
551 given status for the authentication check from the section extraction protocol.
553 The possible responses in a given SAP implementation may include locking
554 flash upon failure to authenticate, attestation logging for all signed drivers,
555 and other exception operations. The File parameter allows for possible logging
556 within the SAP of the driver.
558 If File is NULL, then EFI_INVALID_PARAMETER is returned.
560 If the file specified by File with an authentication status specified by
561 AuthenticationStatus is safe for the DXE Core to use, then EFI_SUCCESS is returned.
563 If the file specified by File with an authentication status specified by
564 AuthenticationStatus is not safe for the DXE Core to use under any circumstances,
565 then EFI_ACCESS_DENIED is returned.
567 If the file specified by File with an authentication status specified by
568 AuthenticationStatus is not safe for the DXE Core to use right now, but it
569 might be possible to use it at a future time, then EFI_SECURITY_VIOLATION is
572 @param[in, out] AuthenticationStatus This is the authentication status returned
573 from the securitymeasurement services for the
575 @param[in] File This is a pointer to the device path of the file that is
576 being dispatched. This will optionally be used for logging.
577 @param[in] FileBuffer File buffer matches the input file device path.
578 @param[in] FileSize Size of File buffer matches the input file device path.
580 @retval EFI_SUCCESS The file specified by File did authenticate, and the
581 platform policy dictates that the DXE Core may use File.
582 @retval EFI_INVALID_PARAMETER File is NULL.
583 @retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and
584 the platform policy dictates that File should be placed
585 in the untrusted state. A file may be promoted from
586 the untrusted to the trusted state at a future time
587 with a call to the Trust() DXE Service.
588 @retval EFI_ACCESS_DENIED The file specified by File did not authenticate, and
589 the platform policy dictates that File should not be
590 used for any purpose.
595 DxeTpmMeasureBootHandler (
596 IN OUT UINT32 AuthenticationStatus
,
597 IN CONST EFI_DEVICE_PATH_PROTOCOL
*File
,
598 IN VOID
*FileBuffer OPTIONAL
,
599 IN UINTN FileSize OPTIONAL
602 EFI_TCG_PROTOCOL
*TcgProtocol
;
604 TCG_EFI_BOOT_SERVICE_CAPABILITY ProtocolCapability
;
605 UINT32 TCGFeatureFlags
;
606 EFI_PHYSICAL_ADDRESS EventLogLocation
;
607 EFI_PHYSICAL_ADDRESS EventLogLastEntry
;
608 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
609 EFI_DEVICE_PATH_PROTOCOL
*OrigDevicePathNode
;
611 BOOLEAN ApplicationRequired
;
612 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
615 return EFI_INVALID_PARAMETER
;
618 Status
= gBS
->LocateProtocol (&gEfiTcgProtocolGuid
, NULL
, (VOID
**) &TcgProtocol
);
619 if (EFI_ERROR (Status
)) {
621 // TCG protocol is not installed. So, TPM is not present.
622 // Don't do any measurement, and directly return EFI_SUCCESS.
627 ProtocolCapability
.Size
= (UINT8
) sizeof (ProtocolCapability
);
628 Status
= TcgProtocol
->StatusCheck (
635 if (EFI_ERROR (Status
) || ProtocolCapability
.TPMDeactivatedFlag
) {
637 // TPM device doesn't work or activate.
643 // Copy File Device Path
645 OrigDevicePathNode
= DuplicateDevicePath (File
);
646 ASSERT (OrigDevicePathNode
!= NULL
);
649 // 1. Check whether this device path support BlockIo protocol.
650 // Is so, this device path may be a GPT device path.
652 DevicePathNode
= OrigDevicePathNode
;
653 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &DevicePathNode
, &Handle
);
654 if (!EFI_ERROR (Status
) && !mMeasureGptTableFlag
) {
656 // Find the gpt partion on the given devicepath
658 DevicePathNode
= OrigDevicePathNode
;
659 while (!IsDevicePathEnd (DevicePathNode
)) {
661 // Find the Gpt partition
663 if (DevicePathType (DevicePathNode
) == MEDIA_DEVICE_PATH
&&
664 DevicePathSubType (DevicePathNode
) == MEDIA_HARDDRIVE_DP
) {
666 // Check whether it is a gpt partition or not
668 if (((HARDDRIVE_DEVICE_PATH
*) DevicePathNode
)->MBRType
== MBR_TYPE_EFI_PARTITION_TABLE_HEADER
&&
669 ((HARDDRIVE_DEVICE_PATH
*) DevicePathNode
)->SignatureType
== SIGNATURE_TYPE_GUID
) {
672 // Change the partition device path to its parent device path (disk) and get the handle.
674 DevicePathNode
->Type
= END_DEVICE_PATH_TYPE
;
675 DevicePathNode
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
676 DevicePathNode
= OrigDevicePathNode
;
677 Status
= gBS
->LocateDevicePath (
678 &gEfiDiskIoProtocolGuid
,
682 if (!EFI_ERROR (Status
)) {
686 Status
= TcgMeasureGptTable (TcgProtocol
, Handle
);
687 if (!EFI_ERROR (Status
)) {
689 // GPT disk check done.
691 mMeasureGptTableFlag
= TRUE
;
694 FreePool (OrigDevicePathNode
);
695 OrigDevicePathNode
= DuplicateDevicePath (File
);
696 ASSERT (OrigDevicePathNode
!= NULL
);
700 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
705 // 2. Measure PE image.
707 ApplicationRequired
= FALSE
;
710 // Check whether this device path support FV2 protocol.
712 DevicePathNode
= OrigDevicePathNode
;
713 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &DevicePathNode
, &Handle
);
714 if (!EFI_ERROR (Status
)) {
716 // Don't check FV image, and directly return EFI_SUCCESS.
717 // It can be extended to the specific FV authentication according to the different requirement.
719 if (IsDevicePathEnd (DevicePathNode
)) {
723 // The image from Firmware image will not be mearsured.
724 // Current policy doesn't measure PeImage from Firmware if it is driver
725 // If the got PeImage is application, it will be still be measured.
727 ApplicationRequired
= TRUE
;
731 // File is not found.
733 if (FileBuffer
== NULL
) {
734 Status
= EFI_SECURITY_VIOLATION
;
741 DevicePathNode
= OrigDevicePathNode
;
742 ZeroMem (&ImageContext
, sizeof (ImageContext
));
743 ImageContext
.Handle
= (VOID
*) FileBuffer
;
744 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) ImageRead
;
747 // Get information about the image being loaded
749 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
750 if (EFI_ERROR (Status
)) {
752 // The information can't be got from the invalid PeImage
758 // Measure only application if Application flag is set
759 // Measure drivers and applications if Application flag is not set
761 if ((!ApplicationRequired
) ||
762 (ApplicationRequired
&& ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
)) {
764 // Print the image path to be measured.
768 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*DevPathToText
;
769 Status
= gBS
->LocateProtocol (
770 &gEfiDevicePathToTextProtocolGuid
,
772 (VOID
**) &DevPathToText
774 if (!EFI_ERROR (Status
)) {
775 ToText
= DevPathToText
->ConvertDevicePathToText (
780 if (ToText
!= NULL
) {
781 DEBUG ((DEBUG_INFO
, "The measured image path is %s.\n", ToText
));
787 // Measure PE image into TPM log.
789 Status
= TcgMeasurePeImage (
791 (EFI_PHYSICAL_ADDRESS
) (UINTN
) FileBuffer
,
793 (UINTN
) ImageContext
.ImageAddress
,
794 ImageContext
.ImageType
,
800 // Done, free the allocated resource.
803 FreePool (OrigDevicePathNode
);
809 Register the security handler to provide TPM measure boot service.
811 @param ImageHandle ImageHandle of the loaded driver.
812 @param SystemTable Pointer to the EFI System Table.
814 @retval EFI_SUCCESS Register successfully.
815 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.
819 DxeTpmMeasureBootLibConstructor (
820 IN EFI_HANDLE ImageHandle
,
821 IN EFI_SYSTEM_TABLE
*SystemTable
824 return RegisterSecurityHandler (
825 DxeTpmMeasureBootHandler
,
826 EFI_AUTH_OPERATION_MEASURE_IMAGE
| EFI_AUTH_OPERATION_IMAGE_REQUIRED