]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/DxeTpmMeasureBootLib/DxeTpmMeasureBootLib.c
SecurityPkg: remove PE/COFF header workaround for ELILO on IPF
[mirror_edk2.git] / SecurityPkg / Library / DxeTpmMeasureBootLib / DxeTpmMeasureBootLib.c
index 9b13806e892c23f7fbc4e3d1af309bdd709138c2..4e4a90f9da625145bcb723080108674aa0545a3d 100644 (file)
@@ -1,5 +1,5 @@
 /** @file\r
 /** @file\r
-  The library instance provides security service of TPM measure boot.  \r
+  The library instance provides security service of TPM measure boot.\r
 \r
   Caution: This file requires additional review when modified.\r
   This library will have external input - PE/COFF image and GPT partition.\r
 \r
   Caution: This file requires additional review when modified.\r
   This library will have external input - PE/COFF image and GPT partition.\r
   TcgMeasureGptTable() function will receive untrusted GPT partition table, and parse\r
   partition data carefully.\r
 \r
   TcgMeasureGptTable() function will receive untrusted GPT partition table, and parse\r
   partition data carefully.\r
 \r
-Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials \r
-are licensed and made available under the terms and conditions of the BSD License \r
-which accompanies this distribution.  The full text of the license may be found at \r
+Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php\r
 \r
 http://opensource.org/licenses/bsd-license.php\r
 \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 **/\r
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 **/\r
@@ -31,10 +31,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Protocol/TcgService.h>\r
 #include <Protocol/BlockIo.h>\r
 #include <Protocol/DiskIo.h>\r
 #include <Protocol/TcgService.h>\r
 #include <Protocol/BlockIo.h>\r
 #include <Protocol/DiskIo.h>\r
-#include <Protocol/DevicePathToText.h>\r
 #include <Protocol/FirmwareVolumeBlock.h>\r
 \r
 #include <Protocol/FirmwareVolumeBlock.h>\r
 \r
-#include <Guid/TrustedFvHob.h>\r
+#include <Guid/MeasuredFvHob.h>\r
 \r
 #include <Library/BaseLib.h>\r
 #include <Library/DebugLib.h>\r
 \r
 #include <Library/BaseLib.h>\r
 #include <Library/DebugLib.h>\r
@@ -51,15 +50,14 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 // Flag to check GPT partition. It only need be measured once.\r
 //\r
 BOOLEAN                           mMeasureGptTableFlag = FALSE;\r
 // Flag to check GPT partition. It only need be measured once.\r
 //\r
 BOOLEAN                           mMeasureGptTableFlag = FALSE;\r
-EFI_GUID                          mZeroGuid = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};\r
 UINTN                             mMeasureGptCount = 0;\r
 VOID                              *mFileBuffer;\r
 UINTN                             mMeasureGptCount = 0;\r
 VOID                              *mFileBuffer;\r
-UINTN                             mImageSize;\r
+UINTN                             mTpmImageSize;\r
 //\r
 // Measured FV handle cache\r
 //\r
 EFI_HANDLE                        mCacheMeasuredHandle  = NULL;\r
 //\r
 // Measured FV handle cache\r
 //\r
 EFI_HANDLE                        mCacheMeasuredHandle  = NULL;\r
-UINT32                            *mGuidHobData         = NULL;\r
+MEASURED_HOB_DATA                 *mMeasuredHobData     = NULL;\r
 \r
 /**\r
   Reads contents of a PE/COFF image in memory buffer.\r
 \r
 /**\r
   Reads contents of a PE/COFF image in memory buffer.\r
@@ -70,11 +68,11 @@ UINT32                            *mGuidHobData         = NULL;
 \r
   @param  FileHandle      Pointer to the file handle to read the PE/COFF image.\r
   @param  FileOffset      Offset into the PE/COFF image to begin the read operation.\r
 \r
   @param  FileHandle      Pointer to the file handle to read the PE/COFF image.\r
   @param  FileOffset      Offset into the PE/COFF image to begin the read operation.\r
-  @param  ReadSize        On input, the size in bytes of the requested read operation.  \r
+  @param  ReadSize        On input, the size in bytes of the requested read operation.\r
                           On output, the number of bytes actually read.\r
   @param  Buffer          Output buffer that contains the data read from the PE/COFF image.\r
                           On output, the number of bytes actually read.\r
   @param  Buffer          Output buffer that contains the data read from the PE/COFF image.\r
-  \r
-  @retval EFI_SUCCESS     The specified portion of the PE/COFF image was read and the size \r
+\r
+  @retval EFI_SUCCESS     The specified portion of the PE/COFF image was read and the size\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -96,11 +94,11 @@ DxeTpmMeasureBootLibImageRead (
   }\r
 \r
   EndPosition = FileOffset + *ReadSize;\r
   }\r
 \r
   EndPosition = FileOffset + *ReadSize;\r
-  if (EndPosition > mImageSize) {\r
-    *ReadSize = (UINT32)(mImageSize - FileOffset);\r
+  if (EndPosition > mTpmImageSize) {\r
+    *ReadSize = (UINT32)(mTpmImageSize - FileOffset);\r
   }\r
 \r
   }\r
 \r
-  if (FileOffset >= mImageSize) {\r
+  if (FileOffset >= mTpmImageSize) {\r
     *ReadSize = 0;\r
   }\r
 \r
     *ReadSize = 0;\r
   }\r
 \r
@@ -159,11 +157,11 @@ TcgMeasureGptTable (
   }\r
   //\r
   // Read the EFI Partition Table Header\r
   }\r
   //\r
   // Read the EFI Partition Table Header\r
-  //  \r
+  //\r
   PrimaryHeader = (EFI_PARTITION_TABLE_HEADER *) AllocatePool (BlockIo->Media->BlockSize);\r
   if (PrimaryHeader == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   PrimaryHeader = (EFI_PARTITION_TABLE_HEADER *) AllocatePool (BlockIo->Media->BlockSize);\r
   if (PrimaryHeader == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
-  }  \r
+  }\r
   Status = DiskIo->ReadDisk (\r
                      DiskIo,\r
                      BlockIo->Media->MediaId,\r
   Status = DiskIo->ReadDisk (\r
                      DiskIo,\r
                      BlockIo->Media->MediaId,\r
@@ -175,7 +173,7 @@ TcgMeasureGptTable (
     DEBUG ((EFI_D_ERROR, "Failed to Read Partition Table Header!\n"));\r
     FreePool (PrimaryHeader);\r
     return EFI_DEVICE_ERROR;\r
     DEBUG ((EFI_D_ERROR, "Failed to Read Partition Table Header!\n"));\r
     FreePool (PrimaryHeader);\r
     return EFI_DEVICE_ERROR;\r
-  }  \r
+  }\r
   //\r
   // Read the partition entry.\r
   //\r
   //\r
   // Read the partition entry.\r
   //\r
@@ -196,23 +194,23 @@ TcgMeasureGptTable (
     FreePool (EntryPtr);\r
     return EFI_DEVICE_ERROR;\r
   }\r
     FreePool (EntryPtr);\r
     return EFI_DEVICE_ERROR;\r
   }\r
-  \r
+\r
   //\r
   // Count the valid partition\r
   //\r
   PartitionEntry    = (EFI_PARTITION_ENTRY *)EntryPtr;\r
   NumberOfPartition = 0;\r
   for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {\r
   //\r
   // Count the valid partition\r
   //\r
   PartitionEntry    = (EFI_PARTITION_ENTRY *)EntryPtr;\r
   NumberOfPartition = 0;\r
   for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {\r
-    if (!CompareGuid (&PartitionEntry->PartitionTypeGUID, &mZeroGuid)) {\r
-      NumberOfPartition++;  \r
+    if (!IsZeroGuid (&PartitionEntry->PartitionTypeGUID)) {\r
+      NumberOfPartition++;\r
     }\r
     PartitionEntry = (EFI_PARTITION_ENTRY *)((UINT8 *)PartitionEntry + PrimaryHeader->SizeOfPartitionEntry);\r
   }\r
 \r
   //\r
   // Prepare Data for Measurement\r
     }\r
     PartitionEntry = (EFI_PARTITION_ENTRY *)((UINT8 *)PartitionEntry + PrimaryHeader->SizeOfPartitionEntry);\r
   }\r
 \r
   //\r
   // Prepare Data for Measurement\r
-  // \r
-  EventSize = (UINT32)(sizeof (EFI_GPT_DATA) - sizeof (GptData->Partitions) \r
+  //\r
+  EventSize = (UINT32)(sizeof (EFI_GPT_DATA) - sizeof (GptData->Partitions)\r
                         + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry);\r
   TcgEvent = (TCG_PCR_EVENT *) AllocateZeroPool (EventSize + sizeof (TCG_PCR_EVENT_HDR));\r
   if (TcgEvent == NULL) {\r
                         + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry);\r
   TcgEvent = (TCG_PCR_EVENT *) AllocateZeroPool (EventSize + sizeof (TCG_PCR_EVENT_HDR));\r
   if (TcgEvent == NULL) {\r
@@ -224,11 +222,11 @@ TcgMeasureGptTable (
   TcgEvent->PCRIndex   = 5;\r
   TcgEvent->EventType  = EV_EFI_GPT_EVENT;\r
   TcgEvent->EventSize  = EventSize;\r
   TcgEvent->PCRIndex   = 5;\r
   TcgEvent->EventType  = EV_EFI_GPT_EVENT;\r
   TcgEvent->EventSize  = EventSize;\r
-  GptData = (EFI_GPT_DATA *) TcgEvent->Event;  \r
+  GptData = (EFI_GPT_DATA *) TcgEvent->Event;\r
 \r
   //\r
   // Copy the EFI_PARTITION_TABLE_HEADER and NumberOfPartition\r
 \r
   //\r
   // Copy the EFI_PARTITION_TABLE_HEADER and NumberOfPartition\r
-  //  \r
+  //\r
   CopyMem ((UINT8 *)GptData, (UINT8*)PrimaryHeader, sizeof (EFI_PARTITION_TABLE_HEADER));\r
   GptData->NumberOfPartitions = NumberOfPartition;\r
   //\r
   CopyMem ((UINT8 *)GptData, (UINT8*)PrimaryHeader, sizeof (EFI_PARTITION_TABLE_HEADER));\r
   GptData->NumberOfPartitions = NumberOfPartition;\r
   //\r
@@ -237,7 +235,7 @@ TcgMeasureGptTable (
   PartitionEntry    = (EFI_PARTITION_ENTRY*)EntryPtr;\r
   NumberOfPartition = 0;\r
   for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {\r
   PartitionEntry    = (EFI_PARTITION_ENTRY*)EntryPtr;\r
   NumberOfPartition = 0;\r
   for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {\r
-    if (!CompareGuid (&PartitionEntry->PartitionTypeGUID, &mZeroGuid)) {\r
+    if (!IsZeroGuid (&PartitionEntry->PartitionTypeGUID)) {\r
       CopyMem (\r
         (UINT8 *)&GptData->Partitions + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry,\r
         (UINT8 *)PartitionEntry,\r
       CopyMem (\r
         (UINT8 *)&GptData->Partitions + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry,\r
         (UINT8 *)PartitionEntry,\r
@@ -280,6 +278,9 @@ TcgMeasureGptTable (
   PE/COFF image is external input, so this function will validate its data structure\r
   within this image buffer before use.\r
 \r
   PE/COFF image is external input, so this function will validate its data structure\r
   within this image buffer before use.\r
 \r
+  Notes: PE/COFF image has been checked by BasePeCoffLib PeCoffLoaderGetImageInfo() in\r
+  its caller function DxeTpmMeasureBootHandler().\r
+\r
   @param[in] TcgProtocol    Pointer to the located TCG protocol instance.\r
   @param[in] ImageAddress   Start address of image buffer.\r
   @param[in] ImageSize      Image size\r
   @param[in] TcgProtocol    Pointer to the located TCG protocol instance.\r
   @param[in] ImageAddress   Start address of image buffer.\r
   @param[in] ImageSize      Image size\r
@@ -289,7 +290,7 @@ TcgMeasureGptTable (
 \r
   @retval EFI_SUCCESS            Successfully measure image.\r
   @retval EFI_OUT_OF_RESOURCES   No enough resource to measure image.\r
 \r
   @retval EFI_SUCCESS            Successfully measure image.\r
   @retval EFI_OUT_OF_RESOURCES   No enough resource to measure image.\r
-  @retval EFI_UNSUPPORTED        ImageType is unsupported or PE image is mal-format.  \r
+  @retval EFI_UNSUPPORTED        ImageType is unsupported or PE image is mal-format.\r
   @retval other error value\r
 \r
 **/\r
   @retval other error value\r
 \r
 **/\r
@@ -319,7 +320,6 @@ TcgMeasurePeImage (
   EFI_IMAGE_SECTION_HEADER             *SectionHeader;\r
   UINTN                                Index;\r
   UINTN                                Pos;\r
   EFI_IMAGE_SECTION_HEADER             *SectionHeader;\r
   UINTN                                Index;\r
   UINTN                                Pos;\r
-  UINT16                               Magic;\r
   UINT32                               EventSize;\r
   UINT32                               EventNumber;\r
   EFI_PHYSICAL_ADDRESS                 EventLogLastEntry;\r
   UINT32                               EventSize;\r
   UINT32                               EventNumber;\r
   EFI_PHYSICAL_ADDRESS                 EventLogLastEntry;\r
@@ -372,7 +372,9 @@ TcgMeasurePeImage (
   ImageLoad->ImageLengthInMemory   = ImageSize;\r
   ImageLoad->ImageLinkTimeAddress  = LinkTimeBase;\r
   ImageLoad->LengthOfDevicePath    = FilePathSize;\r
   ImageLoad->ImageLengthInMemory   = ImageSize;\r
   ImageLoad->ImageLinkTimeAddress  = LinkTimeBase;\r
   ImageLoad->LengthOfDevicePath    = FilePathSize;\r
-  CopyMem (ImageLoad->DevicePath, FilePath, FilePathSize);\r
+  if ((FilePath != NULL) && (FilePathSize != 0)) {\r
+    CopyMem (ImageLoad->DevicePath, FilePath, FilePathSize);\r
+  }\r
 \r
   //\r
   // Check PE/COFF image\r
 \r
   //\r
   // Check PE/COFF image\r
@@ -415,44 +417,30 @@ TcgMeasurePeImage (
   // Measuring PE/COFF Image Header;\r
   // But CheckSum field and SECURITY data directory (certificate) are excluded\r
   //\r
   // Measuring PE/COFF Image Header;\r
   // But CheckSum field and SECURITY data directory (certificate) are excluded\r
   //\r
-  if (Hdr.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
-    //\r
-    // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value \r
-    //       in the PE/COFF Header. If the MachineType is Itanium(IA64) and the \r
-    //       Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC\r
-    //       then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC\r
-    //\r
-    Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
-  } else {\r
-    //\r
-    // Get the magic value from the PE/COFF Optional Header\r
-    //\r
-    Magic = Hdr.Pe32->OptionalHeader.Magic;\r
-  }\r
-  \r
+\r
   //\r
   // 3.  Calculate the distance from the base of the image header to the image checksum address.\r
   // 4.  Hash the image header from its base to beginning of the image checksum.\r
   //\r
   HashBase = (UINT8 *) (UINTN) ImageAddress;\r
   //\r
   // 3.  Calculate the distance from the base of the image header to the image checksum address.\r
   // 4.  Hash the image header from its base to beginning of the image checksum.\r
   //\r
   HashBase = (UINT8 *) (UINTN) ImageAddress;\r
-  if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+  if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
     //\r
     // Use PE32 offset\r
     //\r
     NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
     //\r
     // Use PE32 offset\r
     //\r
     NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
-    HashSize = (UINTN) ((UINT8 *)(&Hdr.Pe32->OptionalHeader.CheckSum) - HashBase);\r
+    HashSize = (UINTN) (&Hdr.Pe32->OptionalHeader.CheckSum) - (UINTN) HashBase;\r
   } else {\r
     //\r
     // Use PE32+ offset\r
     //\r
     NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
   } else {\r
     //\r
     // Use PE32+ offset\r
     //\r
     NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
-    HashSize = (UINTN) ((UINT8 *)(&Hdr.Pe32Plus->OptionalHeader.CheckSum) - HashBase);\r
+    HashSize = (UINTN) (&Hdr.Pe32Plus->OptionalHeader.CheckSum) - (UINTN) HashBase;\r
   }\r
 \r
   HashStatus = Sha1Update (Sha1Ctx, HashBase, HashSize);\r
   if (!HashStatus) {\r
     goto Finish;\r
   }\r
 \r
   HashStatus = Sha1Update (Sha1Ctx, HashBase, HashSize);\r
   if (!HashStatus) {\r
     goto Finish;\r
-  }  \r
+  }\r
 \r
   //\r
   // 5.  Skip over the image checksum (it occupies a single ULONG).\r
 \r
   //\r
   // 5.  Skip over the image checksum (it occupies a single ULONG).\r
@@ -462,7 +450,7 @@ TcgMeasurePeImage (
     // 6.  Since there is no Cert Directory in optional header, hash everything\r
     //     from the end of the checksum to the end of image header.\r
     //\r
     // 6.  Since there is no Cert Directory in optional header, hash everything\r
     //     from the end of the checksum to the end of image header.\r
     //\r
-    if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+    if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
       //\r
       // Use PE32 offset.\r
       //\r
       //\r
       // Use PE32 offset.\r
       //\r
@@ -481,23 +469,23 @@ TcgMeasurePeImage (
       if (!HashStatus) {\r
         goto Finish;\r
       }\r
       if (!HashStatus) {\r
         goto Finish;\r
       }\r
-    }    \r
+    }\r
   } else {\r
     //\r
     // 7.  Hash everything from the end of the checksum to the start of the Cert Directory.\r
     //\r
   } else {\r
     //\r
     // 7.  Hash everything from the end of the checksum to the start of the Cert Directory.\r
     //\r
-    if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+    if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
       //\r
       // Use PE32 offset\r
       //\r
       HashBase = (UINT8 *) &Hdr.Pe32->OptionalHeader.CheckSum + sizeof (UINT32);\r
       //\r
       // Use PE32 offset\r
       //\r
       HashBase = (UINT8 *) &Hdr.Pe32->OptionalHeader.CheckSum + sizeof (UINT32);\r
-      HashSize = (UINTN) ((UINT8 *)(&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - HashBase);\r
+      HashSize = (UINTN) (&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - (UINTN) HashBase;\r
     } else {\r
       //\r
       // Use PE32+ offset\r
     } else {\r
       //\r
       // Use PE32+ offset\r
-      //    \r
+      //\r
       HashBase = (UINT8 *) &Hdr.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);\r
       HashBase = (UINT8 *) &Hdr.Pe32Plus->OptionalHeader.CheckSum + sizeof (UINT32);\r
-      HashSize = (UINTN) ((UINT8 *)(&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - HashBase);\r
+      HashSize = (UINTN) (&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY]) - (UINTN) HashBase;\r
     }\r
 \r
     if (HashSize != 0) {\r
     }\r
 \r
     if (HashSize != 0) {\r
@@ -511,7 +499,7 @@ TcgMeasurePeImage (
     // 8.  Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)\r
     // 9.  Hash everything from the end of the Cert Directory to the end of image header.\r
     //\r
     // 8.  Skip over the Cert Directory. (It is sizeof(IMAGE_DATA_DIRECTORY) bytes.)\r
     // 9.  Hash everything from the end of the Cert Directory to the end of image header.\r
     //\r
-    if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+    if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
       //\r
       // Use PE32 offset\r
       //\r
       //\r
       // Use PE32 offset\r
       //\r
@@ -524,7 +512,7 @@ TcgMeasurePeImage (
       HashBase = (UINT8 *) &Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];\r
       HashSize = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - ImageAddress);\r
     }\r
       HashBase = (UINT8 *) &Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];\r
       HashSize = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - (UINTN) (HashBase - ImageAddress);\r
     }\r
-    \r
+\r
     if (HashSize != 0) {\r
       HashStatus  = Sha1Update (Sha1Ctx, HashBase, HashSize);\r
       if (!HashStatus) {\r
     if (HashSize != 0) {\r
       HashStatus  = Sha1Update (Sha1Ctx, HashBase, HashSize);\r
       if (!HashStatus) {\r
@@ -536,7 +524,7 @@ TcgMeasurePeImage (
   //\r
   // 10. Set the SUM_OF_BYTES_HASHED to the size of the header\r
   //\r
   //\r
   // 10. Set the SUM_OF_BYTES_HASHED to the size of the header\r
   //\r
-  if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+  if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
     //\r
     // Use PE32 offset\r
     //\r
     //\r
     // Use PE32 offset\r
     //\r
@@ -618,7 +606,7 @@ TcgMeasurePeImage (
     if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {\r
       CertSize = 0;\r
     } else {\r
     if (NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_SECURITY) {\r
       CertSize = 0;\r
     } else {\r
-      if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+      if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
         //\r
         // Use PE32 offset.\r
         //\r
         //\r
         // Use PE32 offset.\r
         //\r
@@ -664,6 +652,14 @@ TcgMeasurePeImage (
              &EventNumber,\r
              &EventLogLastEntry\r
              );\r
              &EventNumber,\r
              &EventLogLastEntry\r
              );\r
+  if (Status == EFI_OUT_OF_RESOURCES) {\r
+    //\r
+    // Out of resource here means the image is hashed and its result is extended to PCR.\r
+    // But the event log cann't be saved since log area is full.\r
+    // Just return EFI_SUCCESS in order not to block the image load.\r
+    //\r
+    Status = EFI_SUCCESS;\r
+  }\r
 \r
 Finish:\r
   FreePool (TcgEvent);\r
 \r
 Finish:\r
   FreePool (TcgEvent);\r
@@ -679,27 +675,27 @@ Finish:
 }\r
 \r
 /**\r
 }\r
 \r
 /**\r
-  The security handler is used to abstract platform-specific policy \r
-  from the DXE core response to an attempt to use a file that returns a \r
-  given status for the authentication check from the section extraction protocol.  \r
+  The security handler is used to abstract platform-specific policy\r
+  from the DXE core response to an attempt to use a file that returns a\r
+  given status for the authentication check from the section extraction protocol.\r
 \r
 \r
-  The possible responses in a given SAP implementation may include locking \r
-  flash upon failure to authenticate, attestation logging for all signed drivers, \r
-  and other exception operations.  The File parameter allows for possible logging \r
+  The possible responses in a given SAP implementation may include locking\r
+  flash upon failure to authenticate, attestation logging for all signed drivers,\r
+  and other exception operations.  The File parameter allows for possible logging\r
   within the SAP of the driver.\r
 \r
   If File is NULL, then EFI_INVALID_PARAMETER is returned.\r
 \r
   within the SAP of the driver.\r
 \r
   If File is NULL, then EFI_INVALID_PARAMETER is returned.\r
 \r
-  If the file specified by File with an authentication status specified by \r
+  If the file specified by File with an authentication status specified by\r
   AuthenticationStatus is safe for the DXE Core to use, then EFI_SUCCESS is returned.\r
 \r
   AuthenticationStatus is safe for the DXE Core to use, then EFI_SUCCESS is returned.\r
 \r
-  If the file specified by File with an authentication status specified by \r
-  AuthenticationStatus is not safe for the DXE Core to use under any circumstances, \r
+  If the file specified by File with an authentication status specified by\r
+  AuthenticationStatus is not safe for the DXE Core to use under any circumstances,\r
   then EFI_ACCESS_DENIED is returned.\r
 \r
   then EFI_ACCESS_DENIED is returned.\r
 \r
-  If the file specified by File with an authentication status specified by \r
-  AuthenticationStatus is not safe for the DXE Core to use right now, but it \r
-  might be possible to use it at a future time, then EFI_SECURITY_VIOLATION is \r
+  If the file specified by File with an authentication status specified by\r
+  AuthenticationStatus is not safe for the DXE Core to use right now, but it\r
+  might be possible to use it at a future time, then EFI_SECURITY_VIOLATION is\r
   returned.\r
 \r
   @param[in]      AuthenticationStatus  This is the authentication status returned\r
   returned.\r
 \r
   @param[in]      AuthenticationStatus  This is the authentication status returned\r
@@ -740,7 +736,6 @@ DxeTpmMeasureBootHandler (
   PE_COFF_LOADER_IMAGE_CONTEXT        ImageContext;\r
   EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvbProtocol;\r
   EFI_PHYSICAL_ADDRESS                FvAddress;\r
   PE_COFF_LOADER_IMAGE_CONTEXT        ImageContext;\r
   EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *FvbProtocol;\r
   EFI_PHYSICAL_ADDRESS                FvAddress;\r
-  EFI_PLATFORM_FIRMWARE_BLOB          *TrustedFvBuf;\r
   UINT32                              Index;\r
 \r
   Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **) &TcgProtocol);\r
   UINT32                              Index;\r
 \r
   Status = gBS->LocateProtocol (&gEfiTcgProtocolGuid, NULL, (VOID **) &TcgProtocol);\r
@@ -754,13 +749,13 @@ DxeTpmMeasureBootHandler (
 \r
   ProtocolCapability.Size = (UINT8) sizeof (ProtocolCapability);\r
   Status = TcgProtocol->StatusCheck (\r
 \r
   ProtocolCapability.Size = (UINT8) sizeof (ProtocolCapability);\r
   Status = TcgProtocol->StatusCheck (\r
-             TcgProtocol, \r
+             TcgProtocol,\r
              &ProtocolCapability,\r
              &TCGFeatureFlags,\r
              &EventLogLocation,\r
              &EventLogLastEntry\r
            );\r
              &ProtocolCapability,\r
              &TCGFeatureFlags,\r
              &EventLogLocation,\r
              &EventLogLastEntry\r
            );\r
-  if (EFI_ERROR (Status) || ProtocolCapability.TPMDeactivatedFlag) {\r
+  if (EFI_ERROR (Status) || ProtocolCapability.TPMDeactivatedFlag || (!ProtocolCapability.TPMPresentFlag)) {\r
     //\r
     // TPM device doesn't work or activate.\r
     //\r
     //\r
     // TPM device doesn't work or activate.\r
     //\r
@@ -771,7 +766,7 @@ DxeTpmMeasureBootHandler (
   // Copy File Device Path\r
   //\r
   OrigDevicePathNode = DuplicateDevicePath (File);\r
   // Copy File Device Path\r
   //\r
   OrigDevicePathNode = DuplicateDevicePath (File);\r
-  \r
+\r
   //\r
   // 1. Check whether this device path support BlockIo protocol.\r
   // Is so, this device path may be a GPT device path.\r
   //\r
   // 1. Check whether this device path support BlockIo protocol.\r
   // Is so, this device path may be a GPT device path.\r
@@ -792,8 +787,8 @@ DxeTpmMeasureBootHandler (
             DevicePathSubType (DevicePathNode) == MEDIA_HARDDRIVE_DP) {\r
         //\r
         // Check whether it is a gpt partition or not\r
             DevicePathSubType (DevicePathNode) == MEDIA_HARDDRIVE_DP) {\r
         //\r
         // Check whether it is a gpt partition or not\r
-        //                           \r
-        if (((HARDDRIVE_DEVICE_PATH *) DevicePathNode)->MBRType == MBR_TYPE_EFI_PARTITION_TABLE_HEADER && \r
+        //\r
+        if (((HARDDRIVE_DEVICE_PATH *) DevicePathNode)->MBRType == MBR_TYPE_EFI_PARTITION_TABLE_HEADER &&\r
             ((HARDDRIVE_DEVICE_PATH *) DevicePathNode)->SignatureType == SIGNATURE_TYPE_GUID) {\r
 \r
           //\r
             ((HARDDRIVE_DEVICE_PATH *) DevicePathNode)->SignatureType == SIGNATURE_TYPE_GUID) {\r
 \r
           //\r
@@ -828,7 +823,7 @@ DxeTpmMeasureBootHandler (
       DevicePathNode    = NextDevicePathNode (DevicePathNode);\r
     }\r
   }\r
       DevicePathNode    = NextDevicePathNode (DevicePathNode);\r
     }\r
   }\r
-  \r
+\r
   //\r
   // 2. Measure PE image.\r
   //\r
   //\r
   // 2. Measure PE image.\r
   //\r
@@ -848,23 +843,23 @@ DxeTpmMeasureBootHandler (
       return EFI_SUCCESS;\r
     }\r
     //\r
       return EFI_SUCCESS;\r
     }\r
     //\r
-    // The PE image from untrusted Firmware volume need be measured\r
-    // The PE image from trusted Firmware volume will be mearsured according to policy below.\r
-    //   if it is driver, do not measure\r
+    // The PE image from unmeasured Firmware volume need be measured\r
+    // The PE image from measured Firmware volume will be mearsured according to policy below.\r
+    //   If it is driver, do not measure\r
     //   If it is application, still measure.\r
     //\r
     ApplicationRequired = TRUE;\r
 \r
     //   If it is application, still measure.\r
     //\r
     ApplicationRequired = TRUE;\r
 \r
-    if (mCacheMeasuredHandle != Handle && mGuidHobData != NULL) {\r
+    if (mCacheMeasuredHandle != Handle && mMeasuredHobData != NULL) {\r
       //\r
       // Search for Root FV of this PE image\r
       //\r
       TempHandle = Handle;\r
       do {\r
         Status = gBS->HandleProtocol(\r
       //\r
       // Search for Root FV of this PE image\r
       //\r
       TempHandle = Handle;\r
       do {\r
         Status = gBS->HandleProtocol(\r
-                        TempHandle, \r
+                        TempHandle,\r
                         &gEfiFirmwareVolumeBlockProtocolGuid,\r
                         &gEfiFirmwareVolumeBlockProtocolGuid,\r
-                        &FvbProtocol\r
+                        (VOID**)&FvbProtocol\r
                         );\r
         TempHandle = FvbProtocol->ParentHandle;\r
       } while (!EFI_ERROR(Status) && FvbProtocol->ParentHandle != NULL);\r
                         );\r
         TempHandle = FvbProtocol->ParentHandle;\r
       } while (!EFI_ERROR(Status) && FvbProtocol->ParentHandle != NULL);\r
@@ -877,11 +872,10 @@ DxeTpmMeasureBootHandler (
         return Status;\r
       }\r
 \r
         return Status;\r
       }\r
 \r
-      TrustedFvBuf        = (EFI_PLATFORM_FIRMWARE_BLOB *)(mGuidHobData + 1);\r
       ApplicationRequired = FALSE;\r
 \r
       ApplicationRequired = FALSE;\r
 \r
-      for (Index = 0; Index < *mGuidHobData; Index++) {\r
-        if(TrustedFvBuf[Index].BlobBase == FvAddress) {\r
+      for (Index = 0; Index < mMeasuredHobData->Num; Index++) {\r
+        if(mMeasuredHobData->MeasuredFvBuf[Index].BlobBase == FvAddress) {\r
           //\r
           // Cache measured FV for next measurement\r
           //\r
           //\r
           // Cache measured FV for next measurement\r
           //\r
@@ -901,7 +895,7 @@ DxeTpmMeasureBootHandler (
     goto Finish;\r
   }\r
 \r
     goto Finish;\r
   }\r
 \r
-  mImageSize  = FileSize;\r
+  mTpmImageSize  = FileSize;\r
   mFileBuffer = FileBuffer;\r
 \r
   //\r
   mFileBuffer = FileBuffer;\r
 \r
   //\r
@@ -922,33 +916,26 @@ DxeTpmMeasureBootHandler (
     //\r
     goto Finish;\r
   }\r
     //\r
     goto Finish;\r
   }\r
-  \r
+\r
   //\r
   // Measure only application if Application flag is set\r
   // Measure drivers and applications if Application flag is not set\r
   //\r
   //\r
   // Measure only application if Application flag is set\r
   // Measure drivers and applications if Application flag is not set\r
   //\r
-  if ((!ApplicationRequired) || \r
-        (ApplicationRequired && ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION)) {  \r
+  if ((!ApplicationRequired) ||\r
+        (ApplicationRequired && ImageContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION)) {\r
     //\r
     // Print the image path to be measured.\r
     //\r
     // Print the image path to be measured.\r
-    //    \r
+    //\r
     DEBUG_CODE_BEGIN ();\r
       CHAR16                            *ToText;\r
     DEBUG_CODE_BEGIN ();\r
       CHAR16                            *ToText;\r
-      EFI_DEVICE_PATH_TO_TEXT_PROTOCOL  *DevPathToText;\r
-      Status = gBS->LocateProtocol (\r
-                      &gEfiDevicePathToTextProtocolGuid,\r
-                      NULL,\r
-                      (VOID **) &DevPathToText\r
-                      );\r
-      if (!EFI_ERROR (Status)) {\r
-        ToText = DevPathToText->ConvertDevicePathToText (\r
-                                  DevicePathNode,\r
-                                  FALSE,\r
-                                  TRUE\r
-                                  );\r
-        if (ToText != NULL) {\r
-          DEBUG ((DEBUG_INFO, "The measured image path is %s.\n", ToText));\r
-        }\r
+      ToText = ConvertDevicePathToText (\r
+                 DevicePathNode,\r
+                 FALSE,\r
+                 TRUE\r
+                 );\r
+      if (ToText != NULL) {\r
+        DEBUG ((DEBUG_INFO, "The measured image path is %s.\n", ToText));\r
+        FreePool (ToText);\r
       }\r
     DEBUG_CODE_END ();\r
 \r
       }\r
     DEBUG_CODE_END ();\r
 \r
@@ -957,10 +944,10 @@ DxeTpmMeasureBootHandler (
     //\r
     Status = TcgMeasurePeImage (\r
                TcgProtocol,\r
     //\r
     Status = TcgMeasurePeImage (\r
                TcgProtocol,\r
-               (EFI_PHYSICAL_ADDRESS) (UINTN) FileBuffer, \r
-               FileSize, \r
-               (UINTN) ImageContext.ImageAddress, \r
-               ImageContext.ImageType, \r
+               (EFI_PHYSICAL_ADDRESS) (UINTN) FileBuffer,\r
+               FileSize,\r
+               (UINTN) ImageContext.ImageAddress,\r
+               ImageContext.ImageType,\r
                DevicePathNode\r
                );\r
   }\r
                DevicePathNode\r
                );\r
   }\r
@@ -996,10 +983,10 @@ DxeTpmMeasureBootLibConstructor (
 \r
   GuidHob = NULL;\r
 \r
 \r
   GuidHob = NULL;\r
 \r
-  GuidHob = GetFirstGuidHob (&gTrustedFvHobGuid);\r
+  GuidHob = GetFirstGuidHob (&gMeasuredFvHobGuid);\r
 \r
   if (GuidHob != NULL) {\r
 \r
   if (GuidHob != NULL) {\r
-    mGuidHobData = GET_GUID_HOB_DATA (GuidHob);\r
+    mMeasuredHobData = GET_GUID_HOB_DATA (GuidHob);\r
   }\r
 \r
   return RegisterSecurity2Handler (\r
   }\r
 \r
   return RegisterSecurity2Handler (\r