]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxe.c
UefiCpuPkg: Extend measurement of microcode patches to TPM
[mirror_edk2.git] / UefiCpuPkg / MicrocodeMeasurementDxe / MicrocodeMeasurementDxe.c
diff --git a/UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxe.c b/UefiCpuPkg/MicrocodeMeasurementDxe/MicrocodeMeasurementDxe.c
new file mode 100644 (file)
index 0000000..762ca15
--- /dev/null
@@ -0,0 +1,281 @@
+/** @file\r
+  This driver measures microcode patches to TPM.\r
+\r
+  This driver consumes gEdkiiMicrocodePatchHobGuid, packs all unique microcode patch found in gEdkiiMicrocodePatchHobGuid to a binary blob, and measures the binary blob to TPM.\r
+\r
+  Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include <IndustryStandard/UefiTcgPlatform.h>\r
+#include <Guid/EventGroup.h>\r
+#include <Guid/MicrocodePatchHob.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiDriverEntryPoint.h>\r
+#include <Library/UefiLib.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/HobLib.h>\r
+#include <Library/MicrocodeLib.h>\r
+#include <Library/TpmMeasurementLib.h>\r
+\r
+#define CPU_MICROCODE_MEASUREMENT_DESCRIPTION                "Microcode Measurement"\r
+#define CPU_MICROCODE_MEASUREMENT_EVENT_LOG_DESCRIPTION_LEN  sizeof (CPU_MICROCODE_MEASUREMENT_DESCRIPTION)\r
+\r
+#pragma pack(1)\r
+typedef struct {\r
+  UINT8    Description[CPU_MICROCODE_MEASUREMENT_EVENT_LOG_DESCRIPTION_LEN];\r
+  UINTN    NumberOfMicrocodePatchesMeasured;\r
+  UINTN    SizeOfMicrocodePatchesMeasured;\r
+} CPU_MICROCODE_MEASUREMENT_EVENT_LOG;\r
+#pragma pack()\r
+\r
+/**\r
+  Helping function.\r
+\r
+  The function is called by QuickSort to compare the order of offsets of\r
+  two microcode patches in RAM relative to their base address. Elements\r
+  will be in ascending order.\r
+\r
+  @param[in] Offset1   The pointer to the offset of first microcode patch.\r
+  @param[in] Offset2   The pointer to the offset of second microcode patch.\r
+\r
+  @retval 1                   The offset of first microcode patch is bigger than that of the second.\r
+  @retval -1                  The offset of first microcode patch is smaller than that of the second.\r
+  @retval 0                   The offset of first microcode patch equals to that of the second.\r
+**/\r
+INTN\r
+EFIAPI\r
+MicrocodePatchOffsetCompareFunction (\r
+  IN CONST VOID  *Offset1,\r
+  IN CONST VOID  *Offset2\r
+  )\r
+{\r
+  if (*(UINT64 *)(Offset1) > *(UINT64 *)(Offset2)) {\r
+    return 1;\r
+  } else if (*(UINT64 *)(Offset1) < *(UINT64 *)(Offset2)) {\r
+    return -1;\r
+  } else {\r
+    return 0;\r
+  }\r
+}\r
+\r
+/**\r
+  This function remove duplicate and invalid offsets in Offsets.\r
+\r
+  This function remove duplicate and invalid offsets in Offsets. Invalid offset means MAX_UINT64 in Offsets.\r
+\r
+  @param[in] Offsets        Microcode offset list.\r
+  @param[in, out] Count          On call as the count of raw microcode offset list; On return as count of the clean microcode offset list.\r
+  **/\r
+VOID\r
+RemoveDuplicateAndInvalidOffset (\r
+  IN     UINT64  *Offsets,\r
+  IN OUT UINTN   *Count\r
+  )\r
+{\r
+  UINTN   Index;\r
+  UINTN   NewCount;\r
+  UINT64  LastOffset;\r
+  UINT64  QuickSortBuffer;\r
+\r
+  //\r
+  // The order matters when packing all applied microcode patches to a single binary blob.\r
+  // Therefore it is a must to do sorting before packing.\r
+  // NOTE: Since microcode patches are sorted by their addresses in memory, the order of\r
+  // addresses in memory of all the microcode patches before sorting is required to be the\r
+  // same in every boot flow. If any future updates made this assumption untenable, then\r
+  // there needs a new solution to measure microcode patches.\r
+  //\r
+  QuickSort (\r
+    Offsets,\r
+    *Count,\r
+    sizeof (UINT64),\r
+    MicrocodePatchOffsetCompareFunction,\r
+    (VOID *)&QuickSortBuffer\r
+    );\r
+\r
+  NewCount   = 0;\r
+  LastOffset = MAX_UINT64;\r
+  for (Index = 0; Index < *Count; Index++) {\r
+    //\r
+    // When MAX_UINT64 element is met, all following elements are MAX_UINT64.\r
+    //\r
+    if (Offsets[Index] == MAX_UINT64) {\r
+      break;\r
+    }\r
+\r
+    //\r
+    // Remove duplicated offsets\r
+    //\r
+    if (Offsets[Index] != LastOffset) {\r
+      LastOffset        = Offsets[Index];\r
+      Offsets[NewCount] = Offsets[Index];\r
+      NewCount++;\r
+    }\r
+  }\r
+\r
+  *Count = NewCount;\r
+}\r
+\r
+/**\r
+  Callback function.\r
+\r
+  Called after signaling of the Ready to Boot Event. Measure microcode patches binary blob with event type EV_CPU_MICROCODE to PCR[1] in TPM.\r
+\r
+  @param[in] Event      Event whose notification function is being invoked.\r
+  @param[in] Context    Pointer to the notification function's context.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+MeasureMicrocodePatches (\r
+  IN      EFI_EVENT  Event,\r
+  IN      VOID       *Context\r
+  )\r
+{\r
+  EFI_STATUS                           Status;\r
+  UINT32                               PCRIndex;\r
+  UINT32                               EventType;\r
+  CPU_MICROCODE_MEASUREMENT_EVENT_LOG  EventLog;\r
+  UINT32                               EventLogSize;\r
+  EFI_HOB_GUID_TYPE                    *GuidHob;\r
+  EDKII_MICROCODE_PATCH_HOB            *MicrocodePatchHob;\r
+  UINT64                               *Offsets;\r
+  UINTN                                Count;\r
+  UINTN                                Index;\r
+  UINTN                                TotalMicrocodeSize;\r
+  UINT8                                *MicrocodePatchesBlob;\r
+\r
+  PCRIndex  = 1;\r
+  EventType = EV_CPU_MICROCODE;\r
+  AsciiStrCpyS (\r
+    (CHAR8 *)(EventLog.Description),\r
+    CPU_MICROCODE_MEASUREMENT_EVENT_LOG_DESCRIPTION_LEN,\r
+    CPU_MICROCODE_MEASUREMENT_DESCRIPTION\r
+    );\r
+  EventLog.NumberOfMicrocodePatchesMeasured = 0;\r
+  EventLog.SizeOfMicrocodePatchesMeasured   = 0;\r
+  EventLogSize                              = sizeof (CPU_MICROCODE_MEASUREMENT_EVENT_LOG);\r
+  Offsets                                   = NULL;\r
+  TotalMicrocodeSize                        = 0;\r
+  Count                                     = 0;\r
+\r
+  GuidHob = GetFirstGuidHob (&gEdkiiMicrocodePatchHobGuid);\r
+  if (NULL == GuidHob) {\r
+    DEBUG ((DEBUG_ERROR, "ERROR: GetFirstGuidHob (&gEdkiiMicrocodePatchHobGuid) failed.\n"));\r
+    return;\r
+  }\r
+\r
+  MicrocodePatchHob = GET_GUID_HOB_DATA (GuidHob);\r
+  DEBUG (\r
+    (DEBUG_INFO,\r
+     "INFO: Got MicrocodePatchHob with microcode patches starting address:0x%x, microcode patches region size:0x%x, processor count:0x%x\n",\r
+     MicrocodePatchHob->MicrocodePatchAddress, MicrocodePatchHob->MicrocodePatchRegionSize,\r
+     MicrocodePatchHob->ProcessorCount)\r
+    );\r
+\r
+  Offsets = AllocateCopyPool (\r
+              MicrocodePatchHob->ProcessorCount * sizeof (UINT64),\r
+              MicrocodePatchHob->ProcessorSpecificPatchOffset\r
+              );\r
+  Count = MicrocodePatchHob->ProcessorCount;\r
+\r
+  RemoveDuplicateAndInvalidOffset (Offsets, &Count);\r
+\r
+  if (0 == Count) {\r
+    DEBUG ((DEBUG_INFO, "INFO: No microcode patch is ever applied, skip the measurement of microcode!\n"));\r
+    FreePool (Offsets);\r
+    return;\r
+  }\r
+\r
+  for (Index = 0; Index < Count; Index++) {\r
+    TotalMicrocodeSize +=\r
+      GetMicrocodeLength ((CPU_MICROCODE_HEADER *)((UINTN)(MicrocodePatchHob->MicrocodePatchAddress + Offsets[Index])));\r
+  }\r
+\r
+  EventLog.NumberOfMicrocodePatchesMeasured = Count;\r
+  EventLog.SizeOfMicrocodePatchesMeasured   = TotalMicrocodeSize;\r
+\r
+  MicrocodePatchesBlob = AllocateZeroPool (TotalMicrocodeSize);\r
+  if (NULL == MicrocodePatchesBlob) {\r
+    DEBUG ((DEBUG_ERROR, "ERROR: AllocateZeroPool to MicrocodePatchesBlob failed!\n"));\r
+    FreePool (Offsets);\r
+    return;\r
+  }\r
+\r
+  TotalMicrocodeSize = 0;\r
+  for (Index = 0; Index < Count; Index++) {\r
+    CopyMem (\r
+      (VOID *)(MicrocodePatchesBlob + TotalMicrocodeSize),\r
+      (VOID *)((UINTN)(MicrocodePatchHob->MicrocodePatchAddress + Offsets[Index])),\r
+      (UINTN)(GetMicrocodeLength (\r
+                (CPU_MICROCODE_HEADER *)((UINTN)(MicrocodePatchHob->MicrocodePatchAddress +\r
+                                                 Offsets[Index]))\r
+                ))\r
+      );\r
+    TotalMicrocodeSize +=\r
+      GetMicrocodeLength ((CPU_MICROCODE_HEADER *)((UINTN)(MicrocodePatchHob->MicrocodePatchAddress + Offsets[Index])));\r
+  }\r
+\r
+  Status = TpmMeasureAndLogData (\r
+             PCRIndex,                                 // PCRIndex\r
+             EventType,                                // EventType\r
+             &EventLog,                                // EventLog\r
+             EventLogSize,                             // LogLen\r
+             MicrocodePatchesBlob,                     // HashData\r
+             TotalMicrocodeSize                        // HashDataLen\r
+             );\r
+  if (!EFI_ERROR (Status)) {\r
+    gBS->CloseEvent (Event);\r
+    DEBUG (\r
+      (DEBUG_INFO,\r
+       "INFO: %d Microcode patches are successfully extended to TPM! The total size measured to TPM is 0x%x\n",\r
+       Count,\r
+       TotalMicrocodeSize)\r
+      );\r
+  } else {\r
+    DEBUG ((DEBUG_ERROR, "ERROR: TpmMeasureAndLogData failed with status %a!\n", Status));\r
+  }\r
+\r
+  FreePool (Offsets);\r
+  FreePool (MicrocodePatchesBlob);\r
+  return;\r
+}\r
+\r
+/**\r
+\r
+  Driver to produce microcode measurement.\r
+\r
+  Driver to produce microcode measurement. Which install a callback function on ready to boot event.\r
+\r
+  @param ImageHandle     Module's image handle\r
+  @param SystemTable     Pointer of EFI_SYSTEM_TABLE\r
+\r
+  @return EFI_SUCCESS     This function always complete successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+MicrocodeMeasurementDriverEntryPoint (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  EFI_EVENT  Event;\r
+\r
+  //\r
+  // Measure Microcode patches\r
+  //\r
+  EfiCreateEventReadyToBootEx (\r
+    TPL_CALLBACK,\r
+    MeasureMicrocodePatches,\r
+    NULL,\r
+    &Event\r
+    );\r
+\r
+  return EFI_SUCCESS;\r
+}\r