MicrocodeMeasurementDxe.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /** @file
  2. This driver measures microcode patches to TPM.
  3. This driver consumes gEdkiiMicrocodePatchHobGuid, packs all unique microcode patch found in gEdkiiMicrocodePatchHobGuid to a binary blob, and measures the binary blob to TPM.
  4. Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <IndustryStandard/UefiTcgPlatform.h>
  8. #include <Guid/EventGroup.h>
  9. #include <Guid/MicrocodePatchHob.h>
  10. #include <Library/DebugLib.h>
  11. #include <Library/UefiDriverEntryPoint.h>
  12. #include <Library/UefiLib.h>
  13. #include <Library/BaseLib.h>
  14. #include <Library/BaseMemoryLib.h>
  15. #include <Library/MemoryAllocationLib.h>
  16. #include <Library/UefiBootServicesTableLib.h>
  17. #include <Library/HobLib.h>
  18. #include <Library/MicrocodeLib.h>
  19. #include <Library/TpmMeasurementLib.h>
  20. #define CPU_MICROCODE_MEASUREMENT_DESCRIPTION "Microcode Measurement"
  21. #define CPU_MICROCODE_MEASUREMENT_EVENT_LOG_DESCRIPTION_LEN sizeof (CPU_MICROCODE_MEASUREMENT_DESCRIPTION)
  22. #pragma pack(1)
  23. typedef struct {
  24. UINT8 Description[CPU_MICROCODE_MEASUREMENT_EVENT_LOG_DESCRIPTION_LEN];
  25. UINTN NumberOfMicrocodePatchesMeasured;
  26. UINTN SizeOfMicrocodePatchesMeasured;
  27. } CPU_MICROCODE_MEASUREMENT_EVENT_LOG;
  28. #pragma pack()
  29. /**
  30. Helping function.
  31. The function is called by QuickSort to compare the order of offsets of
  32. two microcode patches in RAM relative to their base address. Elements
  33. will be in ascending order.
  34. @param[in] Offset1 The pointer to the offset of first microcode patch.
  35. @param[in] Offset2 The pointer to the offset of second microcode patch.
  36. @retval 1 The offset of first microcode patch is bigger than that of the second.
  37. @retval -1 The offset of first microcode patch is smaller than that of the second.
  38. @retval 0 The offset of first microcode patch equals to that of the second.
  39. **/
  40. INTN
  41. EFIAPI
  42. MicrocodePatchOffsetCompareFunction (
  43. IN CONST VOID *Offset1,
  44. IN CONST VOID *Offset2
  45. )
  46. {
  47. if (*(UINT64 *)(Offset1) > *(UINT64 *)(Offset2)) {
  48. return 1;
  49. } else if (*(UINT64 *)(Offset1) < *(UINT64 *)(Offset2)) {
  50. return -1;
  51. } else {
  52. return 0;
  53. }
  54. }
  55. /**
  56. This function remove duplicate and invalid offsets in Offsets.
  57. This function remove duplicate and invalid offsets in Offsets. Invalid offset means MAX_UINT64 in Offsets.
  58. @param[in] Offsets Microcode offset list.
  59. @param[in, out] Count On call as the count of raw microcode offset list; On return as count of the clean microcode offset list.
  60. **/
  61. VOID
  62. RemoveDuplicateAndInvalidOffset (
  63. IN UINT64 *Offsets,
  64. IN OUT UINTN *Count
  65. )
  66. {
  67. UINTN Index;
  68. UINTN NewCount;
  69. UINT64 LastOffset;
  70. UINT64 QuickSortBuffer;
  71. //
  72. // The order matters when packing all applied microcode patches to a single binary blob.
  73. // Therefore it is a must to do sorting before packing.
  74. // NOTE: Since microcode patches are sorted by their addresses in memory, the order of
  75. // addresses in memory of all the microcode patches before sorting is required to be the
  76. // same in every boot flow. If any future updates made this assumption untenable, then
  77. // there needs a new solution to measure microcode patches.
  78. //
  79. QuickSort (
  80. Offsets,
  81. *Count,
  82. sizeof (UINT64),
  83. MicrocodePatchOffsetCompareFunction,
  84. (VOID *)&QuickSortBuffer
  85. );
  86. NewCount = 0;
  87. LastOffset = MAX_UINT64;
  88. for (Index = 0; Index < *Count; Index++) {
  89. //
  90. // When MAX_UINT64 element is met, all following elements are MAX_UINT64.
  91. //
  92. if (Offsets[Index] == MAX_UINT64) {
  93. break;
  94. }
  95. //
  96. // Remove duplicated offsets
  97. //
  98. if (Offsets[Index] != LastOffset) {
  99. LastOffset = Offsets[Index];
  100. Offsets[NewCount] = Offsets[Index];
  101. NewCount++;
  102. }
  103. }
  104. *Count = NewCount;
  105. }
  106. /**
  107. Callback function.
  108. 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.
  109. @param[in] Event Event whose notification function is being invoked.
  110. @param[in] Context Pointer to the notification function's context.
  111. **/
  112. VOID
  113. EFIAPI
  114. MeasureMicrocodePatches (
  115. IN EFI_EVENT Event,
  116. IN VOID *Context
  117. )
  118. {
  119. EFI_STATUS Status;
  120. UINT32 PCRIndex;
  121. UINT32 EventType;
  122. CPU_MICROCODE_MEASUREMENT_EVENT_LOG EventLog;
  123. UINT32 EventLogSize;
  124. EFI_HOB_GUID_TYPE *GuidHob;
  125. EDKII_MICROCODE_PATCH_HOB *MicrocodePatchHob;
  126. UINT64 *Offsets;
  127. UINTN Count;
  128. UINTN Index;
  129. UINTN TotalMicrocodeSize;
  130. UINT8 *MicrocodePatchesBlob;
  131. PCRIndex = 1;
  132. EventType = EV_CPU_MICROCODE;
  133. AsciiStrCpyS (
  134. (CHAR8 *)(EventLog.Description),
  135. CPU_MICROCODE_MEASUREMENT_EVENT_LOG_DESCRIPTION_LEN,
  136. CPU_MICROCODE_MEASUREMENT_DESCRIPTION
  137. );
  138. EventLog.NumberOfMicrocodePatchesMeasured = 0;
  139. EventLog.SizeOfMicrocodePatchesMeasured = 0;
  140. EventLogSize = sizeof (CPU_MICROCODE_MEASUREMENT_EVENT_LOG);
  141. Offsets = NULL;
  142. TotalMicrocodeSize = 0;
  143. Count = 0;
  144. GuidHob = GetFirstGuidHob (&gEdkiiMicrocodePatchHobGuid);
  145. if (NULL == GuidHob) {
  146. DEBUG ((DEBUG_ERROR, "ERROR: GetFirstGuidHob (&gEdkiiMicrocodePatchHobGuid) failed.\n"));
  147. return;
  148. }
  149. MicrocodePatchHob = GET_GUID_HOB_DATA (GuidHob);
  150. DEBUG (
  151. (DEBUG_INFO,
  152. "INFO: Got MicrocodePatchHob with microcode patches starting address:0x%x, microcode patches region size:0x%x, processor count:0x%x\n",
  153. MicrocodePatchHob->MicrocodePatchAddress, MicrocodePatchHob->MicrocodePatchRegionSize,
  154. MicrocodePatchHob->ProcessorCount)
  155. );
  156. Offsets = AllocateCopyPool (
  157. MicrocodePatchHob->ProcessorCount * sizeof (UINT64),
  158. MicrocodePatchHob->ProcessorSpecificPatchOffset
  159. );
  160. Count = MicrocodePatchHob->ProcessorCount;
  161. RemoveDuplicateAndInvalidOffset (Offsets, &Count);
  162. if (0 == Count) {
  163. DEBUG ((DEBUG_INFO, "INFO: No microcode patch is ever applied, skip the measurement of microcode!\n"));
  164. FreePool (Offsets);
  165. return;
  166. }
  167. for (Index = 0; Index < Count; Index++) {
  168. TotalMicrocodeSize +=
  169. GetMicrocodeLength ((CPU_MICROCODE_HEADER *)((UINTN)(MicrocodePatchHob->MicrocodePatchAddress + Offsets[Index])));
  170. }
  171. EventLog.NumberOfMicrocodePatchesMeasured = Count;
  172. EventLog.SizeOfMicrocodePatchesMeasured = TotalMicrocodeSize;
  173. MicrocodePatchesBlob = AllocateZeroPool (TotalMicrocodeSize);
  174. if (NULL == MicrocodePatchesBlob) {
  175. DEBUG ((DEBUG_ERROR, "ERROR: AllocateZeroPool to MicrocodePatchesBlob failed!\n"));
  176. FreePool (Offsets);
  177. return;
  178. }
  179. TotalMicrocodeSize = 0;
  180. for (Index = 0; Index < Count; Index++) {
  181. CopyMem (
  182. (VOID *)(MicrocodePatchesBlob + TotalMicrocodeSize),
  183. (VOID *)((UINTN)(MicrocodePatchHob->MicrocodePatchAddress + Offsets[Index])),
  184. (UINTN)(GetMicrocodeLength (
  185. (CPU_MICROCODE_HEADER *)((UINTN)(MicrocodePatchHob->MicrocodePatchAddress +
  186. Offsets[Index]))
  187. ))
  188. );
  189. TotalMicrocodeSize +=
  190. GetMicrocodeLength ((CPU_MICROCODE_HEADER *)((UINTN)(MicrocodePatchHob->MicrocodePatchAddress + Offsets[Index])));
  191. }
  192. Status = TpmMeasureAndLogData (
  193. PCRIndex, // PCRIndex
  194. EventType, // EventType
  195. &EventLog, // EventLog
  196. EventLogSize, // LogLen
  197. MicrocodePatchesBlob, // HashData
  198. TotalMicrocodeSize // HashDataLen
  199. );
  200. if (!EFI_ERROR (Status)) {
  201. gBS->CloseEvent (Event);
  202. DEBUG (
  203. (DEBUG_INFO,
  204. "INFO: %d Microcode patches are successfully extended to TPM! The total size measured to TPM is 0x%x\n",
  205. Count,
  206. TotalMicrocodeSize)
  207. );
  208. } else {
  209. DEBUG ((DEBUG_ERROR, "ERROR: TpmMeasureAndLogData failed with status %a!\n", Status));
  210. }
  211. FreePool (Offsets);
  212. FreePool (MicrocodePatchesBlob);
  213. return;
  214. }
  215. /**
  216. Driver to produce microcode measurement.
  217. Driver to produce microcode measurement. Which install a callback function on ready to boot event.
  218. @param ImageHandle Module's image handle
  219. @param SystemTable Pointer of EFI_SYSTEM_TABLE
  220. @return EFI_SUCCESS This function always complete successfully.
  221. **/
  222. EFI_STATUS
  223. EFIAPI
  224. MicrocodeMeasurementDriverEntryPoint (
  225. IN EFI_HANDLE ImageHandle,
  226. IN EFI_SYSTEM_TABLE *SystemTable
  227. )
  228. {
  229. EFI_EVENT Event;
  230. //
  231. // Measure Microcode patches
  232. //
  233. EfiCreateEventReadyToBootEx (
  234. TPL_CALLBACK,
  235. MeasureMicrocodePatches,
  236. NULL,
  237. &Event
  238. );
  239. return EFI_SUCCESS;
  240. }