]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/DxeTpm2MeasureBootLib/DxeTpm2MeasureBootLib.c
SecurityPkg: Use IsZeroGuid API for zero GUID checking
[mirror_edk2.git] / SecurityPkg / Library / DxeTpm2MeasureBootLib / DxeTpm2MeasureBootLib.c
index 2f752196a91ed6a37dc6c85eeced51086c2a0461..aacafa83b3498964d5d208a443a9e5f2516b239d 100644 (file)
@@ -9,13 +9,14 @@
   DxeTpm2MeasureBootLibImageRead() function will make sure the PE/COFF image content\r
   read is within the image buffer.\r
 \r
-  TrEEMeasurePeImage() function will accept untrusted PE/COFF image and validate its\r
+  Tcg2MeasurePeImage() function will accept untrusted PE/COFF image and validate its\r
   data structure within this image buffer before use.\r
 \r
-  TrEEMeasureGptTable() function will receive untrusted GPT partition table, and parse\r
+  Tcg2MeasureGptTable() function will receive untrusted GPT partition table, and parse\r
   partition data carefully.\r
 \r
-Copyright (c) 2013 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2015 Hewlett Packard Enterprise Development LP<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
@@ -28,14 +29,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 \r
 #include <PiDxe.h>\r
 \r
-#include <Protocol/TrEEProtocol.h>\r
+#include <Protocol/Tcg2Protocol.h>\r
 #include <Protocol/BlockIo.h>\r
 #include <Protocol/DiskIo.h>\r
 #include <Protocol/DevicePathToText.h>\r
 #include <Protocol/FirmwareVolumeBlock.h>\r
 \r
 #include <Guid/MeasuredFvHob.h>\r
-#include <Guid/ZeroGuid.h>\r
 \r
 #include <Library/BaseLib.h>\r
 #include <Library/DebugLib.h>\r
@@ -51,15 +51,15 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 //\r
 // Flag to check GPT partition. It only need be measured once.\r
 //\r
-BOOLEAN                           mTrEEMeasureGptTableFlag = FALSE;\r
-UINTN                             mTrEEMeasureGptCount = 0;\r
-VOID                              *mTrEEFileBuffer;\r
-UINTN                             mTrEEImageSize;\r
+BOOLEAN                           mTcg2MeasureGptTableFlag = FALSE;\r
+UINTN                             mTcg2MeasureGptCount = 0;\r
+VOID                              *mTcg2FileBuffer;\r
+UINTN                             mTcg2ImageSize;\r
 //\r
 // Measured FV handle cache\r
 //\r
-EFI_HANDLE                        mTrEECacheMeasuredHandle  = NULL;\r
-MEASURED_HOB_DATA                 *mTrEEMeasuredHobData     = NULL;\r
+EFI_HANDLE                        mTcg2CacheMeasuredHandle  = NULL;\r
+MEASURED_HOB_DATA                 *mTcg2MeasuredHobData     = NULL;\r
 \r
 /**\r
   Reads contents of a PE/COFF image in memory buffer.\r
@@ -96,11 +96,11 @@ DxeTpm2MeasureBootLibImageRead (
   }\r
 \r
   EndPosition = FileOffset + *ReadSize;\r
-  if (EndPosition > mTrEEImageSize) {\r
-    *ReadSize = (UINT32)(mTrEEImageSize - FileOffset);\r
+  if (EndPosition > mTcg2ImageSize) {\r
+    *ReadSize = (UINT32)(mTcg2ImageSize - FileOffset);\r
   }\r
 \r
-  if (FileOffset >= mTrEEImageSize) {\r
+  if (FileOffset >= mTcg2ImageSize) {\r
     *ReadSize = 0;\r
   }\r
 \r
@@ -115,7 +115,7 @@ DxeTpm2MeasureBootLibImageRead (
   Caution: This function may receive untrusted input.\r
   The GPT partition table is external input, so this function should parse partition data carefully.\r
 \r
-  @param TreeProtocol            Pointer to the located TREE protocol instance.\r
+  @param Tcg2Protocol            Pointer to the located TCG2 protocol instance.\r
   @param GptHandle               Handle that GPT partition was installed.\r
 \r
   @retval EFI_SUCCESS            Successfully measure GPT table.\r
@@ -126,8 +126,8 @@ DxeTpm2MeasureBootLibImageRead (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-TrEEMeasureGptTable (\r
-  IN  EFI_TREE_PROTOCOL  *TreeProtocol,\r
+Tcg2MeasureGptTable (\r
+  IN  EFI_TCG2_PROTOCOL  *Tcg2Protocol,\r
   IN  EFI_HANDLE         GptHandle\r
   )\r
 {\r
@@ -139,11 +139,11 @@ TrEEMeasureGptTable (
   UINT8                             *EntryPtr;\r
   UINTN                             NumberOfPartition;\r
   UINT32                            Index;\r
-  TrEE_EVENT                        *TreeEvent;\r
+  EFI_TCG2_EVENT                    *Tcg2Event;\r
   EFI_GPT_DATA                      *GptData;\r
   UINT32                            EventSize;\r
 \r
-  if (mTrEEMeasureGptCount > 0) {\r
+  if (mTcg2MeasureGptCount > 0) {\r
     return EFI_SUCCESS;\r
   }\r
 \r
@@ -201,7 +201,7 @@ TrEEMeasureGptTable (
   PartitionEntry    = (EFI_PARTITION_ENTRY *)EntryPtr;\r
   NumberOfPartition = 0;\r
   for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {\r
-    if (!CompareGuid (&PartitionEntry->PartitionTypeGUID, &gZeroGuid)) {\r
+    if (!IsZeroGuid (&PartitionEntry->PartitionTypeGUID)) {\r
       NumberOfPartition++;  \r
     }\r
     PartitionEntry = (EFI_PARTITION_ENTRY *)((UINT8 *)PartitionEntry + PrimaryHeader->SizeOfPartitionEntry);\r
@@ -212,19 +212,19 @@ TrEEMeasureGptTable (
   // \r
   EventSize = (UINT32)(sizeof (EFI_GPT_DATA) - sizeof (GptData->Partitions) \r
                         + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry);\r
-  TreeEvent = (TrEE_EVENT *) AllocateZeroPool (EventSize + sizeof (TrEE_EVENT) - sizeof(TreeEvent->Event));\r
-  if (TreeEvent == NULL) {\r
+  Tcg2Event = (EFI_TCG2_EVENT *) AllocateZeroPool (EventSize + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event));\r
+  if (Tcg2Event == NULL) {\r
     FreePool (PrimaryHeader);\r
     FreePool (EntryPtr);\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  TreeEvent->Size = EventSize + sizeof (TrEE_EVENT) - sizeof(TreeEvent->Event);\r
-  TreeEvent->Header.HeaderSize    = sizeof(TrEE_EVENT_HEADER);\r
-  TreeEvent->Header.HeaderVersion = TREE_EVENT_HEADER_VERSION;\r
-  TreeEvent->Header.PCRIndex      = 5;\r
-  TreeEvent->Header.EventType     = EV_EFI_GPT_EVENT;\r
-  GptData = (EFI_GPT_DATA *) TreeEvent->Event;  \r
+  Tcg2Event->Size = EventSize + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event);\r
+  Tcg2Event->Header.HeaderSize    = sizeof(EFI_TCG2_EVENT_HEADER);\r
+  Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;\r
+  Tcg2Event->Header.PCRIndex      = 5;\r
+  Tcg2Event->Header.EventType     = EV_EFI_GPT_EVENT;\r
+  GptData = (EFI_GPT_DATA *) Tcg2Event->Event;  \r
 \r
   //\r
   // Copy the EFI_PARTITION_TABLE_HEADER and NumberOfPartition\r
@@ -237,7 +237,7 @@ TrEEMeasureGptTable (
   PartitionEntry    = (EFI_PARTITION_ENTRY*)EntryPtr;\r
   NumberOfPartition = 0;\r
   for (Index = 0; Index < PrimaryHeader->NumberOfPartitionEntries; Index++) {\r
-    if (!CompareGuid (&PartitionEntry->PartitionTypeGUID, &gZeroGuid)) {\r
+    if (!IsZeroGuid (&PartitionEntry->PartitionTypeGUID)) {\r
       CopyMem (\r
         (UINT8 *)&GptData->Partitions + NumberOfPartition * PrimaryHeader->SizeOfPartitionEntry,\r
         (UINT8 *)PartitionEntry,\r
@@ -251,20 +251,20 @@ TrEEMeasureGptTable (
   //\r
   // Measure the GPT data\r
   //\r
-  Status = TreeProtocol->HashLogExtendEvent (\r
-             TreeProtocol,\r
+  Status = Tcg2Protocol->HashLogExtendEvent (\r
+             Tcg2Protocol,\r
              0,\r
              (EFI_PHYSICAL_ADDRESS) (UINTN) (VOID *) GptData,\r
              (UINT64) EventSize,\r
-             TreeEvent\r
+             Tcg2Event\r
              );\r
   if (!EFI_ERROR (Status)) {\r
-    mTrEEMeasureGptCount++;\r
+    mTcg2MeasureGptCount++;\r
   }\r
 \r
   FreePool (PrimaryHeader);\r
   FreePool (EntryPtr);\r
-  FreePool (TreeEvent);\r
+  FreePool (Tcg2Event);\r
 \r
   return Status;\r
 }\r
@@ -277,7 +277,7 @@ TrEEMeasureGptTable (
   PE/COFF image is external input, so this function will validate its data structure\r
   within this image buffer before use.\r
 \r
-  @param[in] TreeProtocol   Pointer to the located TREE protocol instance.\r
+  @param[in] Tcg2Protocol   Pointer to the located TCG2 protocol instance.\r
   @param[in] ImageAddress   Start address of image buffer.\r
   @param[in] ImageSize      Image size\r
   @param[in] LinkTimeBase   Address that the image is loaded into memory.\r
@@ -292,8 +292,8 @@ TrEEMeasureGptTable (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-TrEEMeasurePeImage (\r
-  IN  EFI_TREE_PROTOCOL         *TreeProtocol,\r
+Tcg2MeasurePeImage (\r
+  IN  EFI_TCG2_PROTOCOL         *Tcg2Protocol,\r
   IN  EFI_PHYSICAL_ADDRESS      ImageAddress,\r
   IN  UINTN                     ImageSize,\r
   IN  UINTN                     LinkTimeBase,\r
@@ -302,7 +302,7 @@ TrEEMeasurePeImage (
   )\r
 {\r
   EFI_STATUS                        Status;\r
-  TrEE_EVENT                        *TreeEvent;\r
+  EFI_TCG2_EVENT                    *Tcg2Event;\r
   EFI_IMAGE_LOAD_EVENT              *ImageLoad;\r
   UINT32                            FilePathSize;\r
   UINT32                            EventSize;\r
@@ -315,33 +315,33 @@ TrEEMeasurePeImage (
   // Determine destination PCR by BootPolicy\r
   //\r
   EventSize = sizeof (*ImageLoad) - sizeof (ImageLoad->DevicePath) + FilePathSize;\r
-  TreeEvent = AllocateZeroPool (EventSize + sizeof (TrEE_EVENT) - sizeof(TreeEvent->Event));\r
-  if (TreeEvent == NULL) {\r
+  Tcg2Event = AllocateZeroPool (EventSize + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event));\r
+  if (Tcg2Event == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  TreeEvent->Size = EventSize + sizeof (TrEE_EVENT) - sizeof(TreeEvent->Event);\r
-  TreeEvent->Header.HeaderSize    = sizeof(TrEE_EVENT_HEADER);\r
-  TreeEvent->Header.HeaderVersion = TREE_EVENT_HEADER_VERSION;\r
-  ImageLoad           = (EFI_IMAGE_LOAD_EVENT *) TreeEvent->Event;\r
+  Tcg2Event->Size = EventSize + sizeof (EFI_TCG2_EVENT) - sizeof(Tcg2Event->Event);\r
+  Tcg2Event->Header.HeaderSize    = sizeof(EFI_TCG2_EVENT_HEADER);\r
+  Tcg2Event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;\r
+  ImageLoad           = (EFI_IMAGE_LOAD_EVENT *) Tcg2Event->Event;\r
 \r
   switch (ImageType) {\r
     case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:\r
-      TreeEvent->Header.EventType = EV_EFI_BOOT_SERVICES_APPLICATION;\r
-      TreeEvent->Header.PCRIndex  = 4;\r
+      Tcg2Event->Header.EventType = EV_EFI_BOOT_SERVICES_APPLICATION;\r
+      Tcg2Event->Header.PCRIndex  = 4;\r
       break;\r
     case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:\r
-      TreeEvent->Header.EventType = EV_EFI_BOOT_SERVICES_DRIVER;\r
-      TreeEvent->Header.PCRIndex  = 2;\r
+      Tcg2Event->Header.EventType = EV_EFI_BOOT_SERVICES_DRIVER;\r
+      Tcg2Event->Header.PCRIndex  = 2;\r
       break;\r
     case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:\r
-      TreeEvent->Header.EventType = EV_EFI_RUNTIME_SERVICES_DRIVER;\r
-      TreeEvent->Header.PCRIndex  = 2;\r
+      Tcg2Event->Header.EventType = EV_EFI_RUNTIME_SERVICES_DRIVER;\r
+      Tcg2Event->Header.PCRIndex  = 2;\r
       break;\r
     default:\r
       DEBUG ((\r
         EFI_D_ERROR,\r
-        "TrEEMeasurePeImage: Unknown subsystem type %d",\r
+        "Tcg2MeasurePeImage: Unknown subsystem type %d",\r
         ImageType\r
         ));\r
       goto Finish;\r
@@ -358,12 +358,12 @@ TrEEMeasurePeImage (
   //\r
   // Log the PE data\r
   //\r
-  Status = TreeProtocol->HashLogExtendEvent (\r
-             TreeProtocol,\r
+  Status = Tcg2Protocol->HashLogExtendEvent (\r
+             Tcg2Protocol,\r
              PE_COFF_IMAGE,\r
              ImageAddress,\r
              ImageSize,\r
-             TreeEvent\r
+             Tcg2Event\r
              );\r
   if (Status == EFI_VOLUME_FULL) {\r
     //\r
@@ -375,7 +375,7 @@ TrEEMeasurePeImage (
   }\r
 \r
 Finish:\r
-  FreePool (TreeEvent);\r
+  FreePool (Tcg2Event);\r
 \r
   return Status;\r
 }\r
@@ -428,9 +428,9 @@ DxeTpm2MeasureBootHandler (
   IN  BOOLEAN                          BootPolicy\r
   )\r
 {\r
-  EFI_TREE_PROTOCOL                   *TreeProtocol;\r
+  EFI_TCG2_PROTOCOL                   *Tcg2Protocol;\r
   EFI_STATUS                          Status;\r
-  TREE_BOOT_SERVICE_CAPABILITY        ProtocolCapability;\r
+  EFI_TCG2_BOOT_SERVICE_CAPABILITY    ProtocolCapability;\r
   EFI_DEVICE_PATH_PROTOCOL            *DevicePathNode;\r
   EFI_DEVICE_PATH_PROTOCOL            *OrigDevicePathNode;\r
   EFI_HANDLE                          Handle;\r
@@ -441,26 +441,26 @@ DxeTpm2MeasureBootHandler (
   EFI_PHYSICAL_ADDRESS                FvAddress;\r
   UINT32                              Index;\r
 \r
-  Status = gBS->LocateProtocol (&gEfiTrEEProtocolGuid, NULL, (VOID **) &TreeProtocol);\r
+  Status = gBS->LocateProtocol (&gEfiTcg2ProtocolGuid, NULL, (VOID **) &Tcg2Protocol);\r
   if (EFI_ERROR (Status)) {\r
     //\r
-    // TrEE protocol is not installed. So, TPM2 is not present.\r
+    // Tcg2 protocol is not installed. So, TPM2 is not present.\r
     // Don't do any measurement, and directly return EFI_SUCCESS.\r
     //\r
-    DEBUG ((EFI_D_ERROR, "DxeTpm2MeasureBootHandler - TrEE - %r\n", Status));\r
+    DEBUG ((EFI_D_VERBOSE, "DxeTpm2MeasureBootHandler - Tcg2 - %r\n", Status));\r
     return EFI_SUCCESS;\r
   }\r
 \r
   ProtocolCapability.Size = (UINT8) sizeof (ProtocolCapability);\r
-  Status = TreeProtocol->GetCapability (\r
-                           TreeProtocol, \r
+  Status = Tcg2Protocol->GetCapability (\r
+                           Tcg2Protocol, \r
                            &ProtocolCapability\r
                            );\r
-  if (EFI_ERROR (Status) || (!ProtocolCapability.TrEEPresentFlag)) {\r
+  if (EFI_ERROR (Status) || (!ProtocolCapability.TPMPresentFlag)) {\r
     //\r
     // TPM device doesn't work or activate.\r
     //\r
-    DEBUG ((EFI_D_ERROR, "DxeTpm2MeasureBootHandler (%r) - TrEEPresentFlag - %x\n", Status, ProtocolCapability.TrEEPresentFlag));\r
+    DEBUG ((EFI_D_ERROR, "DxeTpm2MeasureBootHandler (%r) - TPMPresentFlag - %x\n", Status, ProtocolCapability.TPMPresentFlag));\r
     return EFI_SUCCESS;\r
   }\r
 \r
@@ -475,7 +475,7 @@ DxeTpm2MeasureBootHandler (
   //\r
   DevicePathNode = OrigDevicePathNode;\r
   Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &DevicePathNode, &Handle);\r
-  if (!EFI_ERROR (Status) && !mTrEEMeasureGptTableFlag) {\r
+  if (!EFI_ERROR (Status) && !mTcg2MeasureGptTableFlag) {\r
     //\r
     // Find the gpt partion on the given devicepath\r
     //\r
@@ -508,13 +508,13 @@ DxeTpm2MeasureBootHandler (
             //\r
             // Measure GPT disk.\r
             //\r
-            Status = TrEEMeasureGptTable (TreeProtocol, Handle);\r
-            DEBUG ((EFI_D_INFO, "DxeTpm2MeasureBootHandler - TrEEMeasureGptTable - %r\n", Status));\r
+            Status = Tcg2MeasureGptTable (Tcg2Protocol, Handle);\r
+            DEBUG ((EFI_D_INFO, "DxeTpm2MeasureBootHandler - Tcg2MeasureGptTable - %r\n", Status));\r
             if (!EFI_ERROR (Status)) {\r
               //\r
               // GPT disk check done.\r
               //\r
-              mTrEEMeasureGptTableFlag = TRUE;\r
+              mTcg2MeasureGptTableFlag = TRUE;\r
             }\r
           }\r
           FreePool (OrigDevicePathNode);\r
@@ -553,7 +553,7 @@ DxeTpm2MeasureBootHandler (
     //\r
     ApplicationRequired = TRUE;\r
 \r
-    if (mTrEECacheMeasuredHandle != Handle && mTrEEMeasuredHobData != NULL) {\r
+    if (mTcg2CacheMeasuredHandle != Handle && mTcg2MeasuredHobData != NULL) {\r
       //\r
       // Search for Root FV of this PE image\r
       //\r
@@ -577,12 +577,12 @@ DxeTpm2MeasureBootHandler (
 \r
       ApplicationRequired = FALSE;\r
 \r
-      for (Index = 0; Index < mTrEEMeasuredHobData->Num; Index++) {\r
-        if(mTrEEMeasuredHobData->MeasuredFvBuf[Index].BlobBase == FvAddress) {\r
+      for (Index = 0; Index < mTcg2MeasuredHobData->Num; Index++) {\r
+        if(mTcg2MeasuredHobData->MeasuredFvBuf[Index].BlobBase == FvAddress) {\r
           //\r
           // Cache measured FV for next measurement\r
           //\r
-          mTrEECacheMeasuredHandle = Handle;\r
+          mTcg2CacheMeasuredHandle = Handle;\r
           ApplicationRequired  = TRUE;\r
           break;\r
         }\r
@@ -598,8 +598,8 @@ DxeTpm2MeasureBootHandler (
     goto Finish;\r
   }\r
 \r
-  mTrEEImageSize  = FileSize;\r
-  mTrEEFileBuffer = FileBuffer;\r
+  mTcg2ImageSize  = FileSize;\r
+  mTcg2FileBuffer = FileBuffer;\r
 \r
   //\r
   // Measure PE Image\r
@@ -645,15 +645,15 @@ DxeTpm2MeasureBootHandler (
     //\r
     // Measure PE image into TPM log.\r
     //\r
-    Status = TrEEMeasurePeImage (\r
-               TreeProtocol,\r
+    Status = Tcg2MeasurePeImage (\r
+               Tcg2Protocol,\r
                (EFI_PHYSICAL_ADDRESS) (UINTN) FileBuffer, \r
                FileSize, \r
                (UINTN) ImageContext.ImageAddress, \r
                ImageContext.ImageType, \r
                DevicePathNode\r
                );\r
-    DEBUG ((EFI_D_INFO, "DxeTpm2MeasureBootHandler - TrEEMeasurePeImage - %r\n", Status));\r
+    DEBUG ((EFI_D_INFO, "DxeTpm2MeasureBootHandler - Tcg2MeasurePeImage - %r\n", Status));\r
   }\r
 \r
   //\r
@@ -692,7 +692,7 @@ DxeTpm2MeasureBootLibConstructor (
   GuidHob = GetFirstGuidHob (&gMeasuredFvHobGuid);\r
 \r
   if (GuidHob != NULL) {\r
-    mTrEEMeasuredHobData = GET_GUID_HOB_DATA (GuidHob);\r
+    mTcg2MeasuredHobData = GET_GUID_HOB_DATA (GuidHob);\r
   }\r
 \r
   return RegisterSecurity2Handler (\r