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;
43 Reads contents of a PE/COFF image in memory buffer.
45 @param FileHandle Pointer to the file handle to read the PE/COFF image.
46 @param FileOffset Offset into the PE/COFF image to begin the read operation.
47 @param ReadSize On input, the size in bytes of the requested read operation.
48 On output, the number of bytes actually read.
49 @param Buffer Output buffer that contains the data read from the PE/COFF image.
51 @retval EFI_SUCCESS The specified portion of the PE/COFF image was read and the size
55 DxeTpmMeasureBootLibImageRead (
58 IN OUT UINTN
*ReadSize
,
64 if (FileHandle
== NULL
|| ReadSize
== NULL
|| Buffer
== NULL
) {
65 return EFI_INVALID_PARAMETER
;
68 if (MAX_ADDRESS
- FileOffset
< *ReadSize
) {
69 return EFI_INVALID_PARAMETER
;
72 EndPosition
= FileOffset
+ *ReadSize
;
73 if (EndPosition
> mImageSize
) {
74 *ReadSize
= (UINT32
)(mImageSize
- FileOffset
);
77 if (FileOffset
>= mImageSize
) {
81 CopyMem (Buffer
, (UINT8
*)((UINTN
) FileHandle
+ FileOffset
), *ReadSize
);
87 Measure GPT table data into TPM log.
89 @param TcgProtocol Pointer to the located TCG protocol instance.
90 @param GptHandle Handle that GPT partition was installed.
92 @retval EFI_SUCCESS Successfully measure GPT table.
93 @retval EFI_UNSUPPORTED Not support GPT table on the given handle.
94 @retval EFI_DEVICE_ERROR Can't get GPT table because device error.
95 @retval EFI_OUT_OF_RESOURCES No enough resource to measure GPT table.
96 @retval other error value
101 IN EFI_TCG_PROTOCOL
*TcgProtocol
,
102 IN EFI_HANDLE GptHandle
106 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
107 EFI_DISK_IO_PROTOCOL
*DiskIo
;
108 EFI_PARTITION_TABLE_HEADER
*PrimaryHeader
;
109 EFI_PARTITION_ENTRY
*PartitionEntry
;
111 UINTN NumberOfPartition
;
113 TCG_PCR_EVENT
*TcgEvent
;
114 EFI_GPT_DATA
*GptData
;
117 EFI_PHYSICAL_ADDRESS EventLogLastEntry
;
119 if (mMeasureGptCount
> 0) {
123 Status
= gBS
->HandleProtocol (GptHandle
, &gEfiBlockIoProtocolGuid
, (VOID
**)&BlockIo
);
124 if (EFI_ERROR (Status
)) {
125 return EFI_UNSUPPORTED
;
127 Status
= gBS
->HandleProtocol (GptHandle
, &gEfiDiskIoProtocolGuid
, (VOID
**)&DiskIo
);
128 if (EFI_ERROR (Status
)) {
129 return EFI_UNSUPPORTED
;
132 // Read the EFI Partition Table Header
134 PrimaryHeader
= (EFI_PARTITION_TABLE_HEADER
*) AllocatePool (BlockIo
->Media
->BlockSize
);
135 if (PrimaryHeader
== NULL
) {
136 return EFI_OUT_OF_RESOURCES
;
138 Status
= DiskIo
->ReadDisk (
140 BlockIo
->Media
->MediaId
,
141 1 * BlockIo
->Media
->BlockSize
,
142 BlockIo
->Media
->BlockSize
,
143 (UINT8
*)PrimaryHeader
145 if (EFI_ERROR (Status
)) {
146 DEBUG ((EFI_D_ERROR
, "Failed to Read Partition Table Header!\n"));
147 FreePool (PrimaryHeader
);
148 return EFI_DEVICE_ERROR
;
151 // Read the partition entry.
153 EntryPtr
= (UINT8
*)AllocatePool (PrimaryHeader
->NumberOfPartitionEntries
* PrimaryHeader
->SizeOfPartitionEntry
);
154 if (EntryPtr
== NULL
) {
155 FreePool (PrimaryHeader
);
156 return EFI_OUT_OF_RESOURCES
;
158 Status
= DiskIo
->ReadDisk (
160 BlockIo
->Media
->MediaId
,
161 MultU64x32(PrimaryHeader
->PartitionEntryLBA
, BlockIo
->Media
->BlockSize
),
162 PrimaryHeader
->NumberOfPartitionEntries
* PrimaryHeader
->SizeOfPartitionEntry
,
165 if (EFI_ERROR (Status
)) {
166 FreePool (PrimaryHeader
);
168 return EFI_DEVICE_ERROR
;
172 // Count the valid partition
174 PartitionEntry
= (EFI_PARTITION_ENTRY
*)EntryPtr
;
175 NumberOfPartition
= 0;
176 for (Index
= 0; Index
< PrimaryHeader
->NumberOfPartitionEntries
; Index
++) {
177 if (!CompareGuid (&PartitionEntry
->PartitionTypeGUID
, &mZeroGuid
)) {
180 PartitionEntry
= (EFI_PARTITION_ENTRY
*)((UINT8
*)PartitionEntry
+ PrimaryHeader
->SizeOfPartitionEntry
);
184 // Prepare Data for Measurement
186 EventSize
= (UINT32
)(sizeof (EFI_GPT_DATA
) - sizeof (GptData
->Partitions
)
187 + NumberOfPartition
* PrimaryHeader
->SizeOfPartitionEntry
);
188 TcgEvent
= (TCG_PCR_EVENT
*) AllocateZeroPool (EventSize
+ sizeof (TCG_PCR_EVENT_HDR
));
189 if (TcgEvent
== NULL
) {
190 FreePool (PrimaryHeader
);
192 return EFI_OUT_OF_RESOURCES
;
195 TcgEvent
->PCRIndex
= 5;
196 TcgEvent
->EventType
= EV_EFI_GPT_EVENT
;
197 TcgEvent
->EventSize
= EventSize
;
198 GptData
= (EFI_GPT_DATA
*) TcgEvent
->Event
;
201 // Copy the EFI_PARTITION_TABLE_HEADER and NumberOfPartition
203 CopyMem ((UINT8
*)GptData
, (UINT8
*)PrimaryHeader
, sizeof (EFI_PARTITION_TABLE_HEADER
));
204 GptData
->NumberOfPartitions
= NumberOfPartition
;
206 // Copy the valid partition entry
208 PartitionEntry
= (EFI_PARTITION_ENTRY
*)EntryPtr
;
209 NumberOfPartition
= 0;
210 for (Index
= 0; Index
< PrimaryHeader
->NumberOfPartitionEntries
; Index
++) {
211 if (!CompareGuid (&PartitionEntry
->PartitionTypeGUID
, &mZeroGuid
)) {
213 (UINT8
*)&GptData
->Partitions
+ NumberOfPartition
* PrimaryHeader
->SizeOfPartitionEntry
,
214 (UINT8
*)PartitionEntry
,
215 PrimaryHeader
->SizeOfPartitionEntry
219 PartitionEntry
=(EFI_PARTITION_ENTRY
*)((UINT8
*)PartitionEntry
+ PrimaryHeader
->SizeOfPartitionEntry
);
223 // Measure the GPT data
226 Status
= TcgProtocol
->HashLogExtendEvent (
228 (EFI_PHYSICAL_ADDRESS
) (UINTN
) (VOID
*) GptData
,
229 (UINT64
) TcgEvent
->EventSize
,
235 if (!EFI_ERROR (Status
)) {
239 FreePool (PrimaryHeader
);
247 Measure PE image into TPM log based on the authenticode image hashing in
248 PE/COFF Specification 8.0 Appendix A.
250 @param[in] TcgProtocol Pointer to the located TCG protocol instance.
251 @param[in] ImageAddress Start address of image buffer.
252 @param[in] ImageSize Image size
253 @param[in] LinkTimeBase Address that the image is loaded into memory.
254 @param[in] ImageType Image subsystem type.
255 @param[in] FilePath File path is corresponding to the input image.
257 @retval EFI_SUCCESS Successfully measure image.
258 @retval EFI_OUT_OF_RESOURCES No enough resource to measure image.
259 @retval EFI_UNSUPPORTED ImageType is unsupported or PE image is mal-format.
260 @retval other error value
266 IN EFI_TCG_PROTOCOL
*TcgProtocol
,
267 IN EFI_PHYSICAL_ADDRESS ImageAddress
,
269 IN UINTN LinkTimeBase
,
271 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
275 TCG_PCR_EVENT
*TcgEvent
;
276 EFI_IMAGE_LOAD_EVENT
*ImageLoad
;
280 EFI_IMAGE_DOS_HEADER
*DosHdr
;
281 UINT32 PeCoffHeaderOffset
;
282 EFI_IMAGE_SECTION_HEADER
*Section
;
285 UINTN SumOfBytesHashed
;
286 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
292 EFI_PHYSICAL_ADDRESS EventLogLastEntry
;
293 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr
;
294 UINT32 NumberOfRvaAndSizes
;
298 Status
= EFI_UNSUPPORTED
;
300 SectionHeader
= NULL
;
302 FilePathSize
= (UINT32
) GetDevicePathSize (FilePath
);
305 // Determine destination PCR by BootPolicy
307 EventSize
= sizeof (*ImageLoad
) - sizeof (ImageLoad
->DevicePath
) + FilePathSize
;
308 TcgEvent
= AllocateZeroPool (EventSize
+ sizeof (TCG_PCR_EVENT
));
309 if (TcgEvent
== NULL
) {
310 return EFI_OUT_OF_RESOURCES
;
313 TcgEvent
->EventSize
= EventSize
;
314 ImageLoad
= (EFI_IMAGE_LOAD_EVENT
*) TcgEvent
->Event
;
317 case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
:
318 TcgEvent
->EventType
= EV_EFI_BOOT_SERVICES_APPLICATION
;
319 TcgEvent
->PCRIndex
= 4;
321 case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
:
322 TcgEvent
->EventType
= EV_EFI_BOOT_SERVICES_DRIVER
;
323 TcgEvent
->PCRIndex
= 2;
325 case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
:
326 TcgEvent
->EventType
= EV_EFI_RUNTIME_SERVICES_DRIVER
;
327 TcgEvent
->PCRIndex
= 2;
332 "TcgMeasurePeImage: Unknown subsystem type %d",
338 ImageLoad
->ImageLocationInMemory
= ImageAddress
;
339 ImageLoad
->ImageLengthInMemory
= ImageSize
;
340 ImageLoad
->ImageLinkTimeAddress
= LinkTimeBase
;
341 ImageLoad
->LengthOfDevicePath
= FilePathSize
;
342 CopyMem (ImageLoad
->DevicePath
, FilePath
, FilePathSize
);
345 // Check PE/COFF image
347 DosHdr
= (EFI_IMAGE_DOS_HEADER
*) (UINTN
) ImageAddress
;
348 PeCoffHeaderOffset
= 0;
349 if (DosHdr
->e_magic
== EFI_IMAGE_DOS_SIGNATURE
) {
350 PeCoffHeaderOffset
= DosHdr
->e_lfanew
;
353 Hdr
.Pe32
= (EFI_IMAGE_NT_HEADERS32
*)((UINT8
*) (UINTN
) ImageAddress
+ PeCoffHeaderOffset
);
354 if (Hdr
.Pe32
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
359 // PE/COFF Image Measurement
361 // NOTE: The following codes/steps are based upon the authenticode image hashing in
362 // PE/COFF Specification 8.0 Appendix A.
366 // 1. Load the image header into memory.
368 // 2. Initialize a SHA hash context.
369 CtxSize
= Sha1GetContextSize ();
370 Sha1Ctx
= AllocatePool (CtxSize
);
371 if (Sha1Ctx
== NULL
) {
372 Status
= EFI_OUT_OF_RESOURCES
;
376 HashStatus
= Sha1Init (Sha1Ctx
);
382 // Measuring PE/COFF Image Header;
383 // But CheckSum field and SECURITY data directory (certificate) are excluded
385 Magic
= Hdr
.Pe32
->OptionalHeader
.Magic
;
388 // 3. Calculate the distance from the base of the image header to the image checksum address.
389 // 4. Hash the image header from its base to beginning of the image checksum.
391 HashBase
= (UINT8
*) (UINTN
) ImageAddress
;
392 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
396 NumberOfRvaAndSizes
= Hdr
.Pe32
->OptionalHeader
.NumberOfRvaAndSizes
;
397 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32
->OptionalHeader
.CheckSum
) - HashBase
);
402 NumberOfRvaAndSizes
= Hdr
.Pe32Plus
->OptionalHeader
.NumberOfRvaAndSizes
;
403 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32Plus
->OptionalHeader
.CheckSum
) - HashBase
);
406 HashStatus
= Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
412 // 5. Skip over the image checksum (it occupies a single ULONG).
414 if (NumberOfRvaAndSizes
<= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
) {
416 // 6. Since there is no Cert Directory in optional header, hash everything
417 // from the end of the checksum to the end of image header.
419 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
423 HashBase
= (UINT8
*) &Hdr
.Pe32
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
424 HashSize
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- ImageAddress
);
429 HashBase
= (UINT8
*) &Hdr
.Pe32Plus
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
430 HashSize
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- ImageAddress
);
434 HashStatus
= Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
441 // 7. Hash everything from the end of the checksum to the start of the Cert Directory.
443 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
447 HashBase
= (UINT8
*) &Hdr
.Pe32
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
448 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
453 HashBase
= (UINT8
*) &Hdr
.Pe32Plus
->OptionalHeader
.CheckSum
+ sizeof (UINT32
);
454 HashSize
= (UINTN
) ((UINT8
*)(&Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
]) - HashBase
);
458 HashStatus
= Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
465 // 8. Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)
466 // 9. Hash everything from the end of the Cert Directory to the end of image header.
468 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
472 HashBase
= (UINT8
*) &Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
473 HashSize
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- ImageAddress
);
478 HashBase
= (UINT8
*) &Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
+ 1];
479 HashSize
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
- (UINTN
) (HashBase
- ImageAddress
);
483 HashStatus
= Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
491 // 10. Set the SUM_OF_BYTES_HASHED to the size of the header
493 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
497 SumOfBytesHashed
= Hdr
.Pe32
->OptionalHeader
.SizeOfHeaders
;
502 SumOfBytesHashed
= Hdr
.Pe32Plus
->OptionalHeader
.SizeOfHeaders
;
506 // 11. Build a temporary table of pointers to all the IMAGE_SECTION_HEADER
507 // structures in the image. The 'NumberOfSections' field of the image
508 // header indicates how big the table should be. Do not include any
509 // IMAGE_SECTION_HEADERs in the table whose 'SizeOfRawData' field is zero.
511 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) AllocateZeroPool (sizeof (EFI_IMAGE_SECTION_HEADER
) * Hdr
.Pe32
->FileHeader
.NumberOfSections
);
512 if (SectionHeader
== NULL
) {
513 Status
= EFI_OUT_OF_RESOURCES
;
518 // 12. Using the 'PointerToRawData' in the referenced section headers as
519 // a key, arrange the elements in the table in ascending order. In other
520 // words, sort the section headers according to the disk-file offset of
523 Section
= (EFI_IMAGE_SECTION_HEADER
*) (
524 (UINT8
*) (UINTN
) ImageAddress
+
527 sizeof(EFI_IMAGE_FILE_HEADER
) +
528 Hdr
.Pe32
->FileHeader
.SizeOfOptionalHeader
530 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
532 while ((Pos
> 0) && (Section
->PointerToRawData
< SectionHeader
[Pos
- 1].PointerToRawData
)) {
533 CopyMem (&SectionHeader
[Pos
], &SectionHeader
[Pos
- 1], sizeof(EFI_IMAGE_SECTION_HEADER
));
536 CopyMem (&SectionHeader
[Pos
], Section
, sizeof(EFI_IMAGE_SECTION_HEADER
));
541 // 13. Walk through the sorted table, bring the corresponding section
542 // into memory, and hash the entire section (using the 'SizeOfRawData'
543 // field in the section header to determine the amount of data to hash).
544 // 14. Add the section's 'SizeOfRawData' to SUM_OF_BYTES_HASHED .
545 // 15. Repeat steps 13 and 14 for all the sections in the sorted table.
547 for (Index
= 0; Index
< Hdr
.Pe32
->FileHeader
.NumberOfSections
; Index
++) {
548 Section
= (EFI_IMAGE_SECTION_HEADER
*) &SectionHeader
[Index
];
549 if (Section
->SizeOfRawData
== 0) {
552 HashBase
= (UINT8
*) (UINTN
) ImageAddress
+ Section
->PointerToRawData
;
553 HashSize
= (UINTN
) Section
->SizeOfRawData
;
555 HashStatus
= Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
560 SumOfBytesHashed
+= HashSize
;
564 // 16. If the file size is greater than SUM_OF_BYTES_HASHED, there is extra
565 // data in the file that needs to be added to the hash. This data begins
566 // at file offset SUM_OF_BYTES_HASHED and its length is:
567 // FileSize - (CertDirectory->Size)
569 if (ImageSize
> SumOfBytesHashed
) {
570 HashBase
= (UINT8
*) (UINTN
) ImageAddress
+ SumOfBytesHashed
;
572 if (NumberOfRvaAndSizes
<= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
) {
575 if (Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
579 CertSize
= Hdr
.Pe32
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
;
584 CertSize
= Hdr
.Pe32Plus
->OptionalHeader
.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY
].Size
;
588 if (ImageSize
> CertSize
+ SumOfBytesHashed
) {
589 HashSize
= (UINTN
) (ImageSize
- CertSize
- SumOfBytesHashed
);
591 HashStatus
= Sha1Update (Sha1Ctx
, HashBase
, HashSize
);
595 } else if (ImageSize
< CertSize
+ SumOfBytesHashed
) {
601 // 17. Finalize the SHA hash.
603 HashStatus
= Sha1Final (Sha1Ctx
, (UINT8
*) &TcgEvent
->Digest
);
612 Status
= TcgProtocol
->HashLogExtendEvent (
614 (EFI_PHYSICAL_ADDRESS
) (UINTN
) (VOID
*) NULL
,
625 if (SectionHeader
!= NULL
) {
626 FreePool (SectionHeader
);
629 if (Sha1Ctx
!= NULL
) {
636 The security handler is used to abstract platform-specific policy
637 from the DXE core response to an attempt to use a file that returns a
638 given status for the authentication check from the section extraction protocol.
640 The possible responses in a given SAP implementation may include locking
641 flash upon failure to authenticate, attestation logging for all signed drivers,
642 and other exception operations. The File parameter allows for possible logging
643 within the SAP of the driver.
645 If File is NULL, then EFI_INVALID_PARAMETER is returned.
647 If the file specified by File with an authentication status specified by
648 AuthenticationStatus is safe for the DXE Core to use, then EFI_SUCCESS is returned.
650 If the file specified by File with an authentication status specified by
651 AuthenticationStatus is not safe for the DXE Core to use under any circumstances,
652 then EFI_ACCESS_DENIED is returned.
654 If the file specified by File with an authentication status specified by
655 AuthenticationStatus is not safe for the DXE Core to use right now, but it
656 might be possible to use it at a future time, then EFI_SECURITY_VIOLATION is
659 @param[in, out] AuthenticationStatus This is the authentication status returned
660 from the securitymeasurement services for the
662 @param[in] File This is a pointer to the device path of the file that is
663 being dispatched. This will optionally be used for logging.
664 @param[in] FileBuffer File buffer matches the input file device path.
665 @param[in] FileSize Size of File buffer matches the input file device path.
667 @retval EFI_SUCCESS The file specified by File did authenticate, and the
668 platform policy dictates that the DXE Core may use File.
669 @retval EFI_INVALID_PARAMETER File is NULL.
670 @retval EFI_SECURITY_VIOLATION The file specified by File did not authenticate, and
671 the platform policy dictates that File should be placed
672 in the untrusted state. A file may be promoted from
673 the untrusted to the trusted state at a future time
674 with a call to the Trust() DXE Service.
675 @retval EFI_ACCESS_DENIED The file specified by File did not authenticate, and
676 the platform policy dictates that File should not be
677 used for any purpose.
682 DxeTpmMeasureBootHandler (
683 IN OUT UINT32 AuthenticationStatus
,
684 IN CONST EFI_DEVICE_PATH_PROTOCOL
*File
,
685 IN VOID
*FileBuffer OPTIONAL
,
686 IN UINTN FileSize OPTIONAL
689 EFI_TCG_PROTOCOL
*TcgProtocol
;
691 TCG_EFI_BOOT_SERVICE_CAPABILITY ProtocolCapability
;
692 UINT32 TCGFeatureFlags
;
693 EFI_PHYSICAL_ADDRESS EventLogLocation
;
694 EFI_PHYSICAL_ADDRESS EventLogLastEntry
;
695 EFI_DEVICE_PATH_PROTOCOL
*DevicePathNode
;
696 EFI_DEVICE_PATH_PROTOCOL
*OrigDevicePathNode
;
698 BOOLEAN ApplicationRequired
;
699 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
702 return EFI_INVALID_PARAMETER
;
705 Status
= gBS
->LocateProtocol (&gEfiTcgProtocolGuid
, NULL
, (VOID
**) &TcgProtocol
);
706 if (EFI_ERROR (Status
)) {
708 // TCG protocol is not installed. So, TPM is not present.
709 // Don't do any measurement, and directly return EFI_SUCCESS.
714 ProtocolCapability
.Size
= (UINT8
) sizeof (ProtocolCapability
);
715 Status
= TcgProtocol
->StatusCheck (
722 if (EFI_ERROR (Status
) || ProtocolCapability
.TPMDeactivatedFlag
) {
724 // TPM device doesn't work or activate.
730 // Copy File Device Path
732 OrigDevicePathNode
= DuplicateDevicePath (File
);
733 ASSERT (OrigDevicePathNode
!= NULL
);
736 // 1. Check whether this device path support BlockIo protocol.
737 // Is so, this device path may be a GPT device path.
739 DevicePathNode
= OrigDevicePathNode
;
740 Status
= gBS
->LocateDevicePath (&gEfiBlockIoProtocolGuid
, &DevicePathNode
, &Handle
);
741 if (!EFI_ERROR (Status
) && !mMeasureGptTableFlag
) {
743 // Find the gpt partion on the given devicepath
745 DevicePathNode
= OrigDevicePathNode
;
746 while (!IsDevicePathEnd (DevicePathNode
)) {
748 // Find the Gpt partition
750 if (DevicePathType (DevicePathNode
) == MEDIA_DEVICE_PATH
&&
751 DevicePathSubType (DevicePathNode
) == MEDIA_HARDDRIVE_DP
) {
753 // Check whether it is a gpt partition or not
755 if (((HARDDRIVE_DEVICE_PATH
*) DevicePathNode
)->MBRType
== MBR_TYPE_EFI_PARTITION_TABLE_HEADER
&&
756 ((HARDDRIVE_DEVICE_PATH
*) DevicePathNode
)->SignatureType
== SIGNATURE_TYPE_GUID
) {
759 // Change the partition device path to its parent device path (disk) and get the handle.
761 DevicePathNode
->Type
= END_DEVICE_PATH_TYPE
;
762 DevicePathNode
->SubType
= END_ENTIRE_DEVICE_PATH_SUBTYPE
;
763 DevicePathNode
= OrigDevicePathNode
;
764 Status
= gBS
->LocateDevicePath (
765 &gEfiDiskIoProtocolGuid
,
769 if (!EFI_ERROR (Status
)) {
773 Status
= TcgMeasureGptTable (TcgProtocol
, Handle
);
774 if (!EFI_ERROR (Status
)) {
776 // GPT disk check done.
778 mMeasureGptTableFlag
= TRUE
;
781 FreePool (OrigDevicePathNode
);
782 OrigDevicePathNode
= DuplicateDevicePath (File
);
783 ASSERT (OrigDevicePathNode
!= NULL
);
787 DevicePathNode
= NextDevicePathNode (DevicePathNode
);
792 // 2. Measure PE image.
794 ApplicationRequired
= FALSE
;
797 // Check whether this device path support FV2 protocol.
799 DevicePathNode
= OrigDevicePathNode
;
800 Status
= gBS
->LocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid
, &DevicePathNode
, &Handle
);
801 if (!EFI_ERROR (Status
)) {
803 // Don't check FV image, and directly return EFI_SUCCESS.
804 // It can be extended to the specific FV authentication according to the different requirement.
806 if (IsDevicePathEnd (DevicePathNode
)) {
810 // The image from Firmware image will not be mearsured.
811 // Current policy doesn't measure PeImage from Firmware if it is driver
812 // If the got PeImage is application, it will be still be measured.
814 ApplicationRequired
= TRUE
;
818 // File is not found.
820 if (FileBuffer
== NULL
) {
821 Status
= EFI_SECURITY_VIOLATION
;
825 mImageSize
= FileSize
;
826 mFileBuffer
= FileBuffer
;
831 DevicePathNode
= OrigDevicePathNode
;
832 ZeroMem (&ImageContext
, sizeof (ImageContext
));
833 ImageContext
.Handle
= (VOID
*) FileBuffer
;
834 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) DxeTpmMeasureBootLibImageRead
;
837 // Get information about the image being loaded
839 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
840 if (EFI_ERROR (Status
)) {
842 // The information can't be got from the invalid PeImage
848 // Measure only application if Application flag is set
849 // Measure drivers and applications if Application flag is not set
851 if ((!ApplicationRequired
) ||
852 (ApplicationRequired
&& ImageContext
.ImageType
== EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
)) {
854 // Print the image path to be measured.
858 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*DevPathToText
;
859 Status
= gBS
->LocateProtocol (
860 &gEfiDevicePathToTextProtocolGuid
,
862 (VOID
**) &DevPathToText
864 if (!EFI_ERROR (Status
)) {
865 ToText
= DevPathToText
->ConvertDevicePathToText (
870 if (ToText
!= NULL
) {
871 DEBUG ((DEBUG_INFO
, "The measured image path is %s.\n", ToText
));
877 // Measure PE image into TPM log.
879 Status
= TcgMeasurePeImage (
881 (EFI_PHYSICAL_ADDRESS
) (UINTN
) FileBuffer
,
883 (UINTN
) ImageContext
.ImageAddress
,
884 ImageContext
.ImageType
,
890 // Done, free the allocated resource.
893 FreePool (OrigDevicePathNode
);
899 Register the security handler to provide TPM measure boot service.
901 @param ImageHandle ImageHandle of the loaded driver.
902 @param SystemTable Pointer to the EFI System Table.
904 @retval EFI_SUCCESS Register successfully.
905 @retval EFI_OUT_OF_RESOURCES No enough memory to register this handler.
909 DxeTpmMeasureBootLibConstructor (
910 IN EFI_HANDLE ImageHandle
,
911 IN EFI_SYSTEM_TABLE
*SystemTable
914 return RegisterSecurityHandler (
915 DxeTpmMeasureBootHandler
,
916 EFI_AUTH_OPERATION_MEASURE_IMAGE
| EFI_AUTH_OPERATION_IMAGE_REQUIRED