TdTcg2Dxe.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522
  1. /** @file
  2. This module implements EFI TD Protocol.
  3. Copyright (c) 2020 - 2021, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiDxe.h>
  7. #include <IndustryStandard/Acpi.h>
  8. #include <IndustryStandard/PeImage.h>
  9. #include <IndustryStandard/TcpaAcpi.h>
  10. #include <Guid/GlobalVariable.h>
  11. #include <Guid/HobList.h>
  12. #include <Guid/EventGroup.h>
  13. #include <Guid/EventExitBootServiceFailed.h>
  14. #include <Guid/ImageAuthentication.h>
  15. #include <Guid/TpmInstance.h>
  16. #include <Protocol/DevicePath.h>
  17. #include <Protocol/MpService.h>
  18. #include <Protocol/VariableWrite.h>
  19. #include <Protocol/Tcg2Protocol.h>
  20. #include <Protocol/TrEEProtocol.h>
  21. #include <Protocol/ResetNotification.h>
  22. #include <Protocol/AcpiTable.h>
  23. #include <Library/DebugLib.h>
  24. #include <Library/BaseMemoryLib.h>
  25. #include <Library/UefiRuntimeServicesTableLib.h>
  26. #include <Library/UefiDriverEntryPoint.h>
  27. #include <Library/HobLib.h>
  28. #include <Library/UefiBootServicesTableLib.h>
  29. #include <Library/BaseLib.h>
  30. #include <Library/MemoryAllocationLib.h>
  31. #include <Library/PrintLib.h>
  32. #include <Library/PcdLib.h>
  33. #include <Library/UefiLib.h>
  34. #include <Library/HashLib.h>
  35. #include <Library/PerformanceLib.h>
  36. #include <Library/ReportStatusCodeLib.h>
  37. #include <Library/TpmMeasurementLib.h>
  38. #include <Protocol/CcMeasurement.h>
  39. #include <Guid/CcEventHob.h>
  40. #include <Library/TdxLib.h>
  41. #define PERF_ID_CC_TCG2_DXE 0x3130
  42. #define CC_EVENT_LOG_AREA_COUNT_MAX 1
  43. #define CC_MR_INDEX_0_MRTD 0
  44. #define CC_MR_INDEX_1_RTMR0 1
  45. #define CC_MR_INDEX_2_RTMR1 2
  46. #define CC_MR_INDEX_3_RTMR2 3
  47. #define CC_MR_INDEX_INVALID 4
  48. typedef struct {
  49. CHAR16 *VariableName;
  50. EFI_GUID *VendorGuid;
  51. } VARIABLE_TYPE;
  52. typedef struct {
  53. EFI_GUID *EventGuid;
  54. EFI_CC_EVENT_LOG_FORMAT LogFormat;
  55. } CC_EVENT_INFO_STRUCT;
  56. typedef struct {
  57. EFI_CC_EVENT_LOG_FORMAT EventLogFormat;
  58. EFI_PHYSICAL_ADDRESS Lasa;
  59. UINT64 Laml;
  60. UINTN EventLogSize;
  61. UINT8 *LastEvent;
  62. BOOLEAN EventLogStarted;
  63. BOOLEAN EventLogTruncated;
  64. UINTN Next800155EventOffset;
  65. } CC_EVENT_LOG_AREA_STRUCT;
  66. typedef struct _TDX_DXE_DATA {
  67. EFI_CC_BOOT_SERVICE_CAPABILITY BsCap;
  68. CC_EVENT_LOG_AREA_STRUCT EventLogAreaStruct[CC_EVENT_LOG_AREA_COUNT_MAX];
  69. BOOLEAN GetEventLogCalled[CC_EVENT_LOG_AREA_COUNT_MAX];
  70. CC_EVENT_LOG_AREA_STRUCT FinalEventLogAreaStruct[CC_EVENT_LOG_AREA_COUNT_MAX];
  71. EFI_CC_FINAL_EVENTS_TABLE *FinalEventsTable[CC_EVENT_LOG_AREA_COUNT_MAX];
  72. } TDX_DXE_DATA;
  73. typedef struct {
  74. TPMI_ALG_HASH HashAlgo;
  75. UINT16 HashSize;
  76. UINT32 HashMask;
  77. } TDX_HASH_INFO;
  78. //
  79. //
  80. CC_EVENT_INFO_STRUCT mCcEventInfo[] = {
  81. { &gCcEventEntryHobGuid, EFI_CC_EVENT_LOG_FORMAT_TCG_2 },
  82. };
  83. TDX_DXE_DATA mTdxDxeData = {
  84. {
  85. sizeof (EFI_CC_BOOT_SERVICE_CAPABILITY), // Size
  86. { 1, 1 }, // StructureVersion
  87. { 1, 1 }, // ProtocolVersion
  88. EFI_CC_BOOT_HASH_ALG_SHA384, // HashAlgorithmBitmap
  89. EFI_CC_EVENT_LOG_FORMAT_TCG_2, // SupportedEventLogs
  90. { 2, 0 } // {CC_TYPE, CC_SUBTYPE}
  91. },
  92. };
  93. UINTN mBootAttempts = 0;
  94. CHAR16 mBootVarName[] = L"BootOrder";
  95. VARIABLE_TYPE mVariableType[] = {
  96. { EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid },
  97. { EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid },
  98. { EFI_KEY_EXCHANGE_KEY_NAME, &gEfiGlobalVariableGuid },
  99. { EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid },
  100. { EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid },
  101. };
  102. EFI_CC_EVENTLOG_ACPI_TABLE mTdxEventlogAcpiTemplate = {
  103. {
  104. EFI_CC_EVENTLOG_ACPI_TABLE_SIGNATURE,
  105. sizeof (mTdxEventlogAcpiTemplate),
  106. EFI_CC_EVENTLOG_ACPI_TABLE_REVISION,
  107. //
  108. // Compiler initializes the remaining bytes to 0
  109. // These fields should be filled in production
  110. //
  111. },
  112. { EFI_CC_TYPE_TDX, 0 }, // CcType
  113. 0, // rsvd
  114. 0, // laml
  115. 0, // lasa
  116. };
  117. //
  118. // Supported Hash list in Td guest.
  119. // Currently SHA384 is supported.
  120. //
  121. TDX_HASH_INFO mHashInfo[] = {
  122. { TPM_ALG_SHA384, SHA384_DIGEST_SIZE, HASH_ALG_SHA384 }
  123. };
  124. /**
  125. Get hash size based on Algo
  126. @param[in] HashAlgo Hash Algorithm Id.
  127. @return Size of the hash.
  128. **/
  129. UINT16
  130. GetHashSizeFromAlgo (
  131. IN TPMI_ALG_HASH HashAlgo
  132. )
  133. {
  134. UINTN Index;
  135. for (Index = 0; Index < sizeof (mHashInfo)/sizeof (mHashInfo[0]); Index++) {
  136. if (mHashInfo[Index].HashAlgo == HashAlgo) {
  137. return mHashInfo[Index].HashSize;
  138. }
  139. }
  140. return 0;
  141. }
  142. /**
  143. Get hash mask based on Algo
  144. @param[in] HashAlgo Hash Algorithm Id.
  145. @return Hash mask.
  146. **/
  147. UINT32
  148. GetHashMaskFromAlgo (
  149. IN TPMI_ALG_HASH HashAlgo
  150. )
  151. {
  152. UINTN Index;
  153. for (Index = 0; Index < ARRAY_SIZE (mHashInfo); Index++) {
  154. if (mHashInfo[Index].HashAlgo == HashAlgo) {
  155. return mHashInfo[Index].HashMask;
  156. }
  157. }
  158. ASSERT (FALSE);
  159. return 0;
  160. }
  161. /**
  162. Copy TPML_DIGEST_VALUES into a buffer
  163. @param[in,out] Buffer Buffer to hold copied TPML_DIGEST_VALUES compact binary.
  164. @param[in] DigestList TPML_DIGEST_VALUES to be copied.
  165. @param[in] HashAlgorithmMask HASH bits corresponding to the desired digests to copy.
  166. @return The end of buffer to hold TPML_DIGEST_VALUES.
  167. **/
  168. VOID *
  169. CopyDigestListToBuffer (
  170. IN OUT VOID *Buffer,
  171. IN TPML_DIGEST_VALUES *DigestList,
  172. IN UINT32 HashAlgorithmMask
  173. )
  174. {
  175. UINTN Index;
  176. UINT16 DigestSize;
  177. UINT32 DigestListCount;
  178. UINT32 *DigestListCountPtr;
  179. DigestListCountPtr = (UINT32 *)Buffer;
  180. DigestListCount = 0;
  181. Buffer = (UINT8 *)Buffer + sizeof (DigestList->count);
  182. for (Index = 0; Index < DigestList->count; Index++) {
  183. if ((DigestList->digests[Index].hashAlg & HashAlgorithmMask) == 0) {
  184. DEBUG ((DEBUG_ERROR, "WARNING: TD Event log has HashAlg unsupported (0x%x)\n", DigestList->digests[Index].hashAlg));
  185. continue;
  186. }
  187. CopyMem (Buffer, &DigestList->digests[Index].hashAlg, sizeof (DigestList->digests[Index].hashAlg));
  188. Buffer = (UINT8 *)Buffer + sizeof (DigestList->digests[Index].hashAlg);
  189. DigestSize = GetHashSizeFromAlgo (DigestList->digests[Index].hashAlg);
  190. CopyMem (Buffer, &DigestList->digests[Index].digest, DigestSize);
  191. Buffer = (UINT8 *)Buffer + DigestSize;
  192. DigestListCount++;
  193. }
  194. WriteUnaligned32 (DigestListCountPtr, DigestListCount);
  195. return Buffer;
  196. }
  197. EFI_HANDLE mImageHandle;
  198. /**
  199. Measure PE image into TPM log based on the authenticode image hashing in
  200. PE/COFF Specification 8.0 Appendix A.
  201. Caution: This function may receive untrusted input.
  202. PE/COFF image is external input, so this function will validate its data structure
  203. within this image buffer before use.
  204. Notes: PE/COFF image is checked by BasePeCoffLib PeCoffLoaderGetImageInfo().
  205. @param[in] RtmrIndex RTMR index
  206. @param[in] ImageAddress Start address of image buffer.
  207. @param[in] ImageSize Image size
  208. @param[out] DigestList Digest list of this image.
  209. @retval EFI_SUCCESS Successfully measure image.
  210. @retval EFI_OUT_OF_RESOURCES No enough resource to measure image.
  211. @retval other error value
  212. **/
  213. EFI_STATUS
  214. MeasurePeImageAndExtend (
  215. IN UINT32 RtmrIndex,
  216. IN EFI_PHYSICAL_ADDRESS ImageAddress,
  217. IN UINTN ImageSize,
  218. OUT TPML_DIGEST_VALUES *DigestList
  219. );
  220. #define COLUME_SIZE (16 * 2)
  221. /**
  222. This function dump raw data.
  223. @param Data raw data
  224. @param Size raw data size
  225. **/
  226. VOID
  227. InternalDumpData (
  228. IN UINT8 *Data,
  229. IN UINTN Size
  230. )
  231. {
  232. UINTN Index;
  233. for (Index = 0; Index < Size; Index++) {
  234. DEBUG ((DEBUG_INFO, Index == COLUME_SIZE/2 ? " | %02x" : " %02x", (UINTN)Data[Index]));
  235. }
  236. }
  237. /**
  238. This function dump raw data with colume format.
  239. @param Data raw data
  240. @param Size raw data size
  241. **/
  242. VOID
  243. InternalDumpHex (
  244. IN UINT8 *Data,
  245. IN UINTN Size
  246. )
  247. {
  248. UINTN Index;
  249. UINTN Count;
  250. UINTN Left;
  251. Count = Size / COLUME_SIZE;
  252. Left = Size % COLUME_SIZE;
  253. for (Index = 0; Index < Count; Index++) {
  254. DEBUG ((DEBUG_INFO, "%04x: ", Index * COLUME_SIZE));
  255. InternalDumpData (Data + Index * COLUME_SIZE, COLUME_SIZE);
  256. DEBUG ((DEBUG_INFO, "\n"));
  257. }
  258. if (Left != 0) {
  259. DEBUG ((DEBUG_INFO, "%04x: ", Index * COLUME_SIZE));
  260. InternalDumpData (Data + Index * COLUME_SIZE, Left);
  261. DEBUG ((DEBUG_INFO, "\n"));
  262. }
  263. }
  264. /**
  265. This function initialize TD_EVENT_HDR for EV_NO_ACTION
  266. Event Type other than EFI Specification ID event. The behavior is defined
  267. by TCG PC Client PFP Spec. Section 9.3.4 EV_NO_ACTION Event Types
  268. @param[in, out] NoActionEvent Event Header of EV_NO_ACTION Event
  269. @param[in] EventSize Event Size of the EV_NO_ACTION Event
  270. **/
  271. VOID
  272. InitNoActionEvent (
  273. IN OUT CC_EVENT_HDR *NoActionEvent,
  274. IN UINT32 EventSize
  275. )
  276. {
  277. UINT32 DigestListCount;
  278. TPMI_ALG_HASH HashAlgId;
  279. UINT8 *DigestBuffer;
  280. DigestBuffer = (UINT8 *)NoActionEvent->Digests.digests;
  281. DigestListCount = 0;
  282. NoActionEvent->MrIndex = 0;
  283. NoActionEvent->EventType = EV_NO_ACTION;
  284. //
  285. // Set Hash count & hashAlg accordingly, while Digest.digests[n].digest to all 0
  286. //
  287. ZeroMem (&NoActionEvent->Digests, sizeof (NoActionEvent->Digests));
  288. if ((mTdxDxeData.BsCap.HashAlgorithmBitmap & EFI_CC_BOOT_HASH_ALG_SHA384) != 0) {
  289. HashAlgId = TPM_ALG_SHA384;
  290. CopyMem (DigestBuffer, &HashAlgId, sizeof (TPMI_ALG_HASH));
  291. DigestBuffer += sizeof (TPMI_ALG_HASH) + GetHashSizeFromAlgo (HashAlgId);
  292. DigestListCount++;
  293. }
  294. //
  295. // Set Digests Count
  296. //
  297. WriteUnaligned32 ((UINT32 *)&NoActionEvent->Digests.count, DigestListCount);
  298. //
  299. // Set Event Size
  300. //
  301. WriteUnaligned32 ((UINT32 *)DigestBuffer, EventSize);
  302. }
  303. /**
  304. Get All processors EFI_CPU_LOCATION in system. LocationBuf is allocated inside the function
  305. Caller is responsible to free LocationBuf.
  306. @param[out] LocationBuf Returns Processor Location Buffer.
  307. @param[out] Num Returns processor number.
  308. @retval EFI_SUCCESS Operation completed successfully.
  309. @retval EFI_UNSUPPORTED MpService protocol not found.
  310. **/
  311. EFI_STATUS
  312. GetProcessorsCpuLocation (
  313. OUT EFI_CPU_PHYSICAL_LOCATION **LocationBuf,
  314. OUT UINTN *Num
  315. )
  316. {
  317. EFI_STATUS Status;
  318. EFI_MP_SERVICES_PROTOCOL *MpProtocol;
  319. UINTN ProcessorNum;
  320. UINTN EnabledProcessorNum;
  321. EFI_PROCESSOR_INFORMATION ProcessorInfo;
  322. EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;
  323. UINTN Index;
  324. Status = gBS->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **)&MpProtocol);
  325. if (EFI_ERROR (Status)) {
  326. //
  327. // MP protocol is not installed
  328. //
  329. return EFI_UNSUPPORTED;
  330. }
  331. Status = MpProtocol->GetNumberOfProcessors (
  332. MpProtocol,
  333. &ProcessorNum,
  334. &EnabledProcessorNum
  335. );
  336. if (EFI_ERROR (Status)) {
  337. return Status;
  338. }
  339. Status = gBS->AllocatePool (
  340. EfiBootServicesData,
  341. sizeof (EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,
  342. (VOID **)&ProcessorLocBuf
  343. );
  344. if (EFI_ERROR (Status)) {
  345. return Status;
  346. }
  347. //
  348. // Get each processor Location info
  349. //
  350. for (Index = 0; Index < ProcessorNum; Index++) {
  351. Status = MpProtocol->GetProcessorInfo (
  352. MpProtocol,
  353. Index,
  354. &ProcessorInfo
  355. );
  356. if (EFI_ERROR (Status)) {
  357. FreePool (ProcessorLocBuf);
  358. return Status;
  359. }
  360. //
  361. // Get all Processor Location info & measure
  362. //
  363. CopyMem (
  364. &ProcessorLocBuf[Index],
  365. &ProcessorInfo.Location,
  366. sizeof (EFI_CPU_PHYSICAL_LOCATION)
  367. );
  368. }
  369. *LocationBuf = ProcessorLocBuf;
  370. *Num = ProcessorNum;
  371. return Status;
  372. }
  373. /**
  374. The EFI_CC_MEASUREMENT_PROTOCOL GetCapability function call provides protocol
  375. capability information and state information.
  376. @param[in] This Indicates the calling context
  377. @param[in, out] ProtocolCapability The caller allocates memory for a EFI_CC_BOOT_SERVICE_CAPABILITY
  378. structure and sets the size field to the size of the structure allocated.
  379. The callee fills in the fields with the EFI protocol capability information
  380. and the current EFI TCG2 state information up to the number of fields which
  381. fit within the size of the structure passed in.
  382. @retval EFI_SUCCESS Operation completed successfully.
  383. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  384. The ProtocolCapability variable will not be populated.
  385. @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
  386. The ProtocolCapability variable will not be populated.
  387. @retval EFI_BUFFER_TOO_SMALL The ProtocolCapability variable is too small to hold the full response.
  388. It will be partially populated (required Size field will be set).
  389. **/
  390. EFI_STATUS
  391. EFIAPI
  392. TdGetCapability (
  393. IN EFI_CC_MEASUREMENT_PROTOCOL *This,
  394. IN OUT EFI_CC_BOOT_SERVICE_CAPABILITY *ProtocolCapability
  395. )
  396. {
  397. DEBUG ((DEBUG_VERBOSE, "TdGetCapability\n"));
  398. if ((This == NULL) || (ProtocolCapability == NULL)) {
  399. return EFI_INVALID_PARAMETER;
  400. }
  401. CopyMem (ProtocolCapability, &mTdxDxeData.BsCap, sizeof (EFI_CC_BOOT_SERVICE_CAPABILITY));
  402. return EFI_SUCCESS;
  403. }
  404. /**
  405. This function dump PCR event.
  406. TD Event log reuse the TCG PCR Event spec.
  407. The first event in the event log is the SHA1 log format.
  408. There is only ONE TCG_PCR_EVENT in TD Event log.
  409. @param[in] EventHdr TCG PCR event structure.
  410. **/
  411. VOID
  412. DumpPcrEvent (
  413. IN TCG_PCR_EVENT_HDR *EventHdr
  414. )
  415. {
  416. UINTN Index;
  417. DEBUG ((DEBUG_INFO, " Event:\n"));
  418. DEBUG ((DEBUG_INFO, " MrIndex - %d\n", EventHdr->PCRIndex));
  419. DEBUG ((DEBUG_INFO, " EventType - 0x%08x\n", EventHdr->EventType));
  420. DEBUG ((DEBUG_INFO, " Digest - "));
  421. for (Index = 0; Index < sizeof (TCG_DIGEST); Index++) {
  422. DEBUG ((DEBUG_INFO, "%02x ", EventHdr->Digest.digest[Index]));
  423. }
  424. DEBUG ((DEBUG_INFO, "\n"));
  425. DEBUG ((DEBUG_INFO, " EventSize - 0x%08x\n", EventHdr->EventSize));
  426. InternalDumpHex ((UINT8 *)(EventHdr + 1), EventHdr->EventSize);
  427. }
  428. /**
  429. This function dump TCG_EfiSpecIDEventStruct.
  430. @param[in] TcgEfiSpecIdEventStruct A pointer to TCG_EfiSpecIDEventStruct.
  431. **/
  432. VOID
  433. DumpTcgEfiSpecIdEventStruct (
  434. IN TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct
  435. )
  436. {
  437. TCG_EfiSpecIdEventAlgorithmSize *DigestSize;
  438. UINTN Index;
  439. UINT8 *VendorInfoSize;
  440. UINT8 *VendorInfo;
  441. UINT32 NumberOfAlgorithms;
  442. DEBUG ((DEBUG_INFO, " TCG_EfiSpecIDEventStruct:\n"));
  443. DEBUG ((DEBUG_INFO, " signature - '"));
  444. for (Index = 0; Index < sizeof (TcgEfiSpecIdEventStruct->signature); Index++) {
  445. DEBUG ((DEBUG_INFO, "%c", TcgEfiSpecIdEventStruct->signature[Index]));
  446. }
  447. DEBUG ((DEBUG_INFO, "'\n"));
  448. DEBUG ((DEBUG_INFO, " platformClass - 0x%08x\n", TcgEfiSpecIdEventStruct->platformClass));
  449. DEBUG ((DEBUG_INFO, " specVersion - %d.%d%d\n", TcgEfiSpecIdEventStruct->specVersionMajor, TcgEfiSpecIdEventStruct->specVersionMinor, TcgEfiSpecIdEventStruct->specErrata));
  450. DEBUG ((DEBUG_INFO, " uintnSize - 0x%02x\n", TcgEfiSpecIdEventStruct->uintnSize));
  451. CopyMem (&NumberOfAlgorithms, TcgEfiSpecIdEventStruct + 1, sizeof (NumberOfAlgorithms));
  452. DEBUG ((DEBUG_INFO, " NumberOfAlgorithms - 0x%08x\n", NumberOfAlgorithms));
  453. DigestSize = (TCG_EfiSpecIdEventAlgorithmSize *)((UINT8 *)TcgEfiSpecIdEventStruct + sizeof (*TcgEfiSpecIdEventStruct) + sizeof (NumberOfAlgorithms));
  454. for (Index = 0; Index < NumberOfAlgorithms; Index++) {
  455. DEBUG ((DEBUG_INFO, " digest(%d)\n", Index));
  456. DEBUG ((DEBUG_INFO, " algorithmId - 0x%04x\n", DigestSize[Index].algorithmId));
  457. DEBUG ((DEBUG_INFO, " digestSize - 0x%04x\n", DigestSize[Index].digestSize));
  458. }
  459. VendorInfoSize = (UINT8 *)&DigestSize[NumberOfAlgorithms];
  460. DEBUG ((DEBUG_INFO, " VendorInfoSize - 0x%02x\n", *VendorInfoSize));
  461. VendorInfo = VendorInfoSize + 1;
  462. DEBUG ((DEBUG_INFO, " VendorInfo - "));
  463. for (Index = 0; Index < *VendorInfoSize; Index++) {
  464. DEBUG ((DEBUG_INFO, "%02x ", VendorInfo[Index]));
  465. }
  466. DEBUG ((DEBUG_INFO, "\n"));
  467. }
  468. /**
  469. This function get size of TCG_EfiSpecIDEventStruct.
  470. @param[in] TcgEfiSpecIdEventStruct A pointer to TCG_EfiSpecIDEventStruct.
  471. **/
  472. UINTN
  473. GetTcgEfiSpecIdEventStructSize (
  474. IN TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct
  475. )
  476. {
  477. TCG_EfiSpecIdEventAlgorithmSize *DigestSize;
  478. UINT8 *VendorInfoSize;
  479. UINT32 NumberOfAlgorithms;
  480. CopyMem (&NumberOfAlgorithms, TcgEfiSpecIdEventStruct + 1, sizeof (NumberOfAlgorithms));
  481. DigestSize = (TCG_EfiSpecIdEventAlgorithmSize *)((UINT8 *)TcgEfiSpecIdEventStruct + sizeof (*TcgEfiSpecIdEventStruct) + sizeof (NumberOfAlgorithms));
  482. VendorInfoSize = (UINT8 *)&DigestSize[NumberOfAlgorithms];
  483. return sizeof (TCG_EfiSpecIDEventStruct) + sizeof (UINT32) + (NumberOfAlgorithms * sizeof (TCG_EfiSpecIdEventAlgorithmSize)) + sizeof (UINT8) + (*VendorInfoSize);
  484. }
  485. /**
  486. This function dump TD Event (including the Digests).
  487. @param[in] CcEvent TD Event structure.
  488. **/
  489. VOID
  490. DumpCcEvent (
  491. IN CC_EVENT *CcEvent
  492. )
  493. {
  494. UINT32 DigestIndex;
  495. UINT32 DigestCount;
  496. TPMI_ALG_HASH HashAlgo;
  497. UINT32 DigestSize;
  498. UINT8 *DigestBuffer;
  499. UINT32 EventSize;
  500. UINT8 *EventBuffer;
  501. DEBUG ((DEBUG_INFO, "Cc Event:\n"));
  502. DEBUG ((DEBUG_INFO, " MrIndex - %d\n", CcEvent->MrIndex));
  503. DEBUG ((DEBUG_INFO, " EventType - 0x%08x\n", CcEvent->EventType));
  504. DEBUG ((DEBUG_INFO, " DigestCount: 0x%08x\n", CcEvent->Digests.count));
  505. DigestCount = CcEvent->Digests.count;
  506. HashAlgo = CcEvent->Digests.digests[0].hashAlg;
  507. DigestBuffer = (UINT8 *)&CcEvent->Digests.digests[0].digest;
  508. for (DigestIndex = 0; DigestIndex < DigestCount; DigestIndex++) {
  509. DEBUG ((DEBUG_INFO, " HashAlgo : 0x%04x\n", HashAlgo));
  510. DEBUG ((DEBUG_INFO, " Digest(%d): \n", DigestIndex));
  511. DigestSize = GetHashSizeFromAlgo (HashAlgo);
  512. InternalDumpHex (DigestBuffer, DigestSize);
  513. //
  514. // Prepare next
  515. //
  516. CopyMem (&HashAlgo, DigestBuffer + DigestSize, sizeof (TPMI_ALG_HASH));
  517. DigestBuffer = DigestBuffer + DigestSize + sizeof (TPMI_ALG_HASH);
  518. }
  519. DigestBuffer = DigestBuffer - sizeof (TPMI_ALG_HASH);
  520. CopyMem (&EventSize, DigestBuffer, sizeof (CcEvent->EventSize));
  521. DEBUG ((DEBUG_INFO, " EventSize - 0x%08x\n", EventSize));
  522. EventBuffer = DigestBuffer + sizeof (CcEvent->EventSize);
  523. InternalDumpHex (EventBuffer, EventSize);
  524. DEBUG ((DEBUG_INFO, "\n"));
  525. }
  526. /**
  527. This function returns size of Td Table event.
  528. @param[in] CcEvent Td Table event structure.
  529. @return size of Td event.
  530. **/
  531. UINTN
  532. GetCcEventSize (
  533. IN CC_EVENT *CcEvent
  534. )
  535. {
  536. UINT32 DigestIndex;
  537. UINT32 DigestCount;
  538. TPMI_ALG_HASH HashAlgo;
  539. UINT32 DigestSize;
  540. UINT8 *DigestBuffer;
  541. UINT32 EventSize;
  542. UINT8 *EventBuffer;
  543. DigestCount = CcEvent->Digests.count;
  544. HashAlgo = CcEvent->Digests.digests[0].hashAlg;
  545. DigestBuffer = (UINT8 *)&CcEvent->Digests.digests[0].digest;
  546. for (DigestIndex = 0; DigestIndex < DigestCount; DigestIndex++) {
  547. DigestSize = GetHashSizeFromAlgo (HashAlgo);
  548. //
  549. // Prepare next
  550. //
  551. CopyMem (&HashAlgo, DigestBuffer + DigestSize, sizeof (TPMI_ALG_HASH));
  552. DigestBuffer = DigestBuffer + DigestSize + sizeof (TPMI_ALG_HASH);
  553. }
  554. DigestBuffer = DigestBuffer - sizeof (TPMI_ALG_HASH);
  555. CopyMem (&EventSize, DigestBuffer, sizeof (CcEvent->EventSize));
  556. EventBuffer = DigestBuffer + sizeof (CcEvent->EventSize);
  557. return (UINTN)EventBuffer + EventSize - (UINTN)CcEvent;
  558. }
  559. /**
  560. This function dump CC event log.
  561. TDVF only supports EFI_CC_EVENT_LOG_FORMAT_TCG_2
  562. @param[in] EventLogFormat The type of the event log for which the information is requested.
  563. @param[in] EventLogLocation A pointer to the memory address of the event log.
  564. @param[in] EventLogLastEntry If the Event Log contains more than one entry, this is a pointer to the
  565. address of the start of the last entry in the event log in memory.
  566. @param[in] FinalEventsTable A pointer to the memory address of the final event table.
  567. **/
  568. VOID
  569. DumpCcEventLog (
  570. IN EFI_CC_EVENT_LOG_FORMAT EventLogFormat,
  571. IN EFI_PHYSICAL_ADDRESS EventLogLocation,
  572. IN EFI_PHYSICAL_ADDRESS EventLogLastEntry,
  573. IN EFI_CC_FINAL_EVENTS_TABLE *FinalEventsTable
  574. )
  575. {
  576. TCG_PCR_EVENT_HDR *EventHdr;
  577. CC_EVENT *CcEvent;
  578. TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct;
  579. UINTN NumberOfEvents;
  580. DEBUG ((DEBUG_INFO, "EventLogFormat: (0x%x)\n", EventLogFormat));
  581. ASSERT (EventLogFormat == EFI_CC_EVENT_LOG_FORMAT_TCG_2);
  582. //
  583. // Dump first event.
  584. // The first event is always the TCG_PCR_EVENT_HDR
  585. // After this event is a TCG_EfiSpecIDEventStruct
  586. //
  587. EventHdr = (TCG_PCR_EVENT_HDR *)(UINTN)EventLogLocation;
  588. DumpPcrEvent (EventHdr);
  589. TcgEfiSpecIdEventStruct = (TCG_EfiSpecIDEventStruct *)(EventHdr + 1);
  590. DumpTcgEfiSpecIdEventStruct (TcgEfiSpecIdEventStruct);
  591. //
  592. // Then the CcEvent (Its structure is similar to TCG_PCR_EVENT2)
  593. //
  594. CcEvent = (CC_EVENT *)((UINTN)TcgEfiSpecIdEventStruct + GetTcgEfiSpecIdEventStructSize (TcgEfiSpecIdEventStruct));
  595. while ((UINTN)CcEvent <= EventLogLastEntry) {
  596. DumpCcEvent (CcEvent);
  597. CcEvent = (CC_EVENT *)((UINTN)CcEvent + GetCcEventSize (CcEvent));
  598. }
  599. if (FinalEventsTable == NULL) {
  600. DEBUG ((DEBUG_INFO, "FinalEventsTable: NOT FOUND\n"));
  601. } else {
  602. DEBUG ((DEBUG_INFO, "FinalEventsTable: (0x%x)\n", FinalEventsTable));
  603. DEBUG ((DEBUG_INFO, " Version: (0x%x)\n", FinalEventsTable->Version));
  604. DEBUG ((DEBUG_INFO, " NumberOfEvents: (0x%x)\n", FinalEventsTable->NumberOfEvents));
  605. CcEvent = (CC_EVENT *)(UINTN)(FinalEventsTable + 1);
  606. for (NumberOfEvents = 0; NumberOfEvents < FinalEventsTable->NumberOfEvents; NumberOfEvents++) {
  607. DumpCcEvent (CcEvent);
  608. CcEvent = (CC_EVENT *)((UINTN)CcEvent + GetCcEventSize (CcEvent));
  609. }
  610. }
  611. return;
  612. }
  613. /**
  614. The EFI_CC_MEASUREMENT_PROTOCOL Get Event Log function call allows a caller to
  615. retrieve the address of a given event log and its last entry.
  616. @param[in] This Indicates the calling context
  617. @param[in] EventLogFormat The type of the event log for which the information is requested.
  618. @param[out] EventLogLocation A pointer to the memory address of the event log.
  619. @param[out] EventLogLastEntry If the Event Log contains more than one entry, this is a pointer to the
  620. address of the start of the last entry in the event log in memory.
  621. @param[out] EventLogTruncated If the Event Log is missing at least one entry because an event would
  622. have exceeded the area allocated for events, this value is set to TRUE.
  623. Otherwise, the value will be FALSE and the Event Log will be complete.
  624. @retval EFI_SUCCESS Operation completed successfully.
  625. @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect
  626. (e.g. asking for an event log whose format is not supported).
  627. **/
  628. EFI_STATUS
  629. EFIAPI
  630. TdGetEventLog (
  631. IN EFI_CC_MEASUREMENT_PROTOCOL *This,
  632. IN EFI_CC_EVENT_LOG_FORMAT EventLogFormat,
  633. OUT EFI_PHYSICAL_ADDRESS *EventLogLocation,
  634. OUT EFI_PHYSICAL_ADDRESS *EventLogLastEntry,
  635. OUT BOOLEAN *EventLogTruncated
  636. )
  637. {
  638. UINTN Index = 0;
  639. DEBUG ((DEBUG_INFO, "TdGetEventLog ... (0x%x)\n", EventLogFormat));
  640. ASSERT (EventLogFormat == EFI_CC_EVENT_LOG_FORMAT_TCG_2);
  641. if (EventLogLocation != NULL) {
  642. *EventLogLocation = mTdxDxeData.EventLogAreaStruct[Index].Lasa;
  643. DEBUG ((DEBUG_INFO, "TdGetEventLog (EventLogLocation - %x)\n", *EventLogLocation));
  644. }
  645. if (EventLogLastEntry != NULL) {
  646. if (!mTdxDxeData.EventLogAreaStruct[Index].EventLogStarted) {
  647. *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)0;
  648. } else {
  649. *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)mTdxDxeData.EventLogAreaStruct[Index].LastEvent;
  650. }
  651. DEBUG ((DEBUG_INFO, "TdGetEventLog (EventLogLastEntry - %x)\n", *EventLogLastEntry));
  652. }
  653. if (EventLogTruncated != NULL) {
  654. *EventLogTruncated = mTdxDxeData.EventLogAreaStruct[Index].EventLogTruncated;
  655. DEBUG ((DEBUG_INFO, "TdGetEventLog (EventLogTruncated - %x)\n", *EventLogTruncated));
  656. }
  657. DEBUG ((DEBUG_INFO, "TdGetEventLog - %r\n", EFI_SUCCESS));
  658. // Dump Event Log for debug purpose
  659. if ((EventLogLocation != NULL) && (EventLogLastEntry != NULL)) {
  660. DumpCcEventLog (EventLogFormat, *EventLogLocation, *EventLogLastEntry, mTdxDxeData.FinalEventsTable[Index]);
  661. }
  662. //
  663. // All events generated after the invocation of EFI_TCG2_GET_EVENT_LOG SHALL be stored
  664. // in an instance of an EFI_CONFIGURATION_TABLE named by the VendorGuid of EFI_TCG2_FINAL_EVENTS_TABLE_GUID.
  665. //
  666. mTdxDxeData.GetEventLogCalled[Index] = TRUE;
  667. return EFI_SUCCESS;
  668. }
  669. /**
  670. Return if this is a Tcg800155PlatformIdEvent.
  671. @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR/TCG_PCR_EVENT_EX data structure.
  672. @param[in] NewEventHdrSize New event header size.
  673. @param[in] NewEventData Pointer to the new event data.
  674. @param[in] NewEventSize New event data size.
  675. @retval TRUE This is a Tcg800155PlatformIdEvent.
  676. @retval FALSE This is NOT a Tcg800155PlatformIdEvent.
  677. **/
  678. BOOLEAN
  679. Is800155Event (
  680. IN VOID *NewEventHdr,
  681. IN UINT32 NewEventHdrSize,
  682. IN UINT8 *NewEventData,
  683. IN UINT32 NewEventSize
  684. )
  685. {
  686. if ((((TCG_PCR_EVENT2_HDR *)NewEventHdr)->EventType == EV_NO_ACTION) &&
  687. (NewEventSize >= sizeof (TCG_Sp800_155_PlatformId_Event2)) &&
  688. (CompareMem (
  689. NewEventData,
  690. TCG_Sp800_155_PlatformId_Event2_SIGNATURE,
  691. sizeof (TCG_Sp800_155_PlatformId_Event2_SIGNATURE) - 1
  692. ) == 0))
  693. {
  694. return TRUE;
  695. }
  696. return FALSE;
  697. }
  698. /**
  699. Add a new entry to the Event Log.
  700. @param[in, out] EventLogAreaStruct The event log area data structure
  701. @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR/TCG_PCR_EVENT_EX data structure.
  702. @param[in] NewEventHdrSize New event header size.
  703. @param[in] NewEventData Pointer to the new event data.
  704. @param[in] NewEventSize New event data size.
  705. @retval EFI_SUCCESS The new event log entry was added.
  706. @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
  707. **/
  708. EFI_STATUS
  709. TcgCommLogEvent (
  710. IN OUT CC_EVENT_LOG_AREA_STRUCT *EventLogAreaStruct,
  711. IN VOID *NewEventHdr,
  712. IN UINT32 NewEventHdrSize,
  713. IN UINT8 *NewEventData,
  714. IN UINT32 NewEventSize
  715. )
  716. {
  717. UINTN NewLogSize;
  718. BOOLEAN Record800155Event;
  719. CC_EVENT_HDR *CcEventHdr;
  720. CcEventHdr = (CC_EVENT_HDR *)NewEventHdr;
  721. DEBUG ((DEBUG_VERBOSE, "Td: Try to log event. Index = %d, EventType = 0x%x\n", CcEventHdr->MrIndex, CcEventHdr->EventType));
  722. if (NewEventSize > MAX_ADDRESS - NewEventHdrSize) {
  723. return EFI_OUT_OF_RESOURCES;
  724. }
  725. NewLogSize = NewEventHdrSize + NewEventSize;
  726. if (NewLogSize > MAX_ADDRESS - EventLogAreaStruct->EventLogSize) {
  727. return EFI_OUT_OF_RESOURCES;
  728. }
  729. if (NewLogSize + EventLogAreaStruct->EventLogSize > EventLogAreaStruct->Laml) {
  730. DEBUG ((DEBUG_INFO, " Laml - 0x%x\n", EventLogAreaStruct->Laml));
  731. DEBUG ((DEBUG_INFO, " NewLogSize - 0x%x\n", NewLogSize));
  732. DEBUG ((DEBUG_INFO, " LogSize - 0x%x\n", EventLogAreaStruct->EventLogSize));
  733. DEBUG ((DEBUG_INFO, "TcgCommLogEvent - %r\n", EFI_OUT_OF_RESOURCES));
  734. return EFI_OUT_OF_RESOURCES;
  735. }
  736. //
  737. // Check 800-155 event
  738. // Record to 800-155 event offset only.
  739. // If the offset is 0, no need to record.
  740. //
  741. Record800155Event = Is800155Event (NewEventHdr, NewEventHdrSize, NewEventData, NewEventSize);
  742. if (Record800155Event) {
  743. DEBUG ((DEBUG_INFO, "It is 800155Event.\n"));
  744. if (EventLogAreaStruct->Next800155EventOffset != 0) {
  745. CopyMem (
  746. (UINT8 *)(UINTN)EventLogAreaStruct->Lasa + EventLogAreaStruct->Next800155EventOffset + NewLogSize,
  747. (UINT8 *)(UINTN)EventLogAreaStruct->Lasa + EventLogAreaStruct->Next800155EventOffset,
  748. EventLogAreaStruct->EventLogSize - EventLogAreaStruct->Next800155EventOffset
  749. );
  750. CopyMem (
  751. (UINT8 *)(UINTN)EventLogAreaStruct->Lasa + EventLogAreaStruct->Next800155EventOffset,
  752. NewEventHdr,
  753. NewEventHdrSize
  754. );
  755. CopyMem (
  756. (UINT8 *)(UINTN)EventLogAreaStruct->Lasa + EventLogAreaStruct->Next800155EventOffset + NewEventHdrSize,
  757. NewEventData,
  758. NewEventSize
  759. );
  760. EventLogAreaStruct->Next800155EventOffset += NewLogSize;
  761. EventLogAreaStruct->LastEvent += NewLogSize;
  762. EventLogAreaStruct->EventLogSize += NewLogSize;
  763. }
  764. return EFI_SUCCESS;
  765. }
  766. EventLogAreaStruct->LastEvent = (UINT8 *)(UINTN)EventLogAreaStruct->Lasa + EventLogAreaStruct->EventLogSize;
  767. EventLogAreaStruct->EventLogSize += NewLogSize;
  768. CopyMem (EventLogAreaStruct->LastEvent, NewEventHdr, NewEventHdrSize);
  769. CopyMem (
  770. EventLogAreaStruct->LastEvent + NewEventHdrSize,
  771. NewEventData,
  772. NewEventSize
  773. );
  774. return EFI_SUCCESS;
  775. }
  776. /**
  777. According to UEFI Spec 2.10 Section 38.4.1:
  778. The following table shows the TPM PCR index mapping and CC event log measurement
  779. register index interpretation for Intel TDX, where MRTD means Trust Domain Measurement
  780. Register and RTMR means Runtime Measurement Register
  781. // TPM PCR Index | CC Measurement Register Index | TDX-measurement register
  782. // ------------------------------------------------------------------------
  783. // 0 | 0 | MRTD
  784. // 1, 7 | 1 | RTMR[0]
  785. // 2~6 | 2 | RTMR[1]
  786. // 8~15 | 3 | RTMR[2]
  787. @param[in] PCRIndex Index of the TPM PCR
  788. @retval UINT32 Index of the CC Event Log Measurement Register Index
  789. @retval CC_MR_INDEX_INVALID Invalid MR Index
  790. **/
  791. UINT32
  792. EFIAPI
  793. MapPcrToMrIndex (
  794. IN UINT32 PCRIndex
  795. )
  796. {
  797. UINT32 MrIndex;
  798. if (PCRIndex > 15) {
  799. ASSERT (FALSE);
  800. return CC_MR_INDEX_INVALID;
  801. }
  802. MrIndex = 0;
  803. if (PCRIndex == 0) {
  804. MrIndex = CC_MR_INDEX_0_MRTD;
  805. } else if ((PCRIndex == 1) || (PCRIndex == 7)) {
  806. MrIndex = CC_MR_INDEX_1_RTMR0;
  807. } else if ((PCRIndex >= 2) && (PCRIndex <= 6)) {
  808. MrIndex = CC_MR_INDEX_2_RTMR1;
  809. } else if ((PCRIndex >= 8) && (PCRIndex <= 15)) {
  810. MrIndex = CC_MR_INDEX_3_RTMR2;
  811. }
  812. return MrIndex;
  813. }
  814. EFI_STATUS
  815. EFIAPI
  816. TdMapPcrToMrIndex (
  817. IN EFI_CC_MEASUREMENT_PROTOCOL *This,
  818. IN UINT32 PCRIndex,
  819. OUT UINT32 *MrIndex
  820. )
  821. {
  822. if (MrIndex == NULL) {
  823. return EFI_INVALID_PARAMETER;
  824. }
  825. *MrIndex = MapPcrToMrIndex (PCRIndex);
  826. return *MrIndex == CC_MR_INDEX_INVALID ? EFI_INVALID_PARAMETER : EFI_SUCCESS;
  827. }
  828. /**
  829. Add a new entry to the Event Log.
  830. @param[in] EventLogFormat The type of the event log for which the information is requested.
  831. @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR/TCG_PCR_EVENT_EX data structure.
  832. @param[in] NewEventHdrSize New event header size.
  833. @param[in] NewEventData Pointer to the new event data.
  834. @param[in] NewEventSize New event data size.
  835. @retval EFI_SUCCESS The new event log entry was added.
  836. @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
  837. **/
  838. EFI_STATUS
  839. TdxDxeLogEvent (
  840. IN EFI_CC_EVENT_LOG_FORMAT EventLogFormat,
  841. IN VOID *NewEventHdr,
  842. IN UINT32 NewEventHdrSize,
  843. IN UINT8 *NewEventData,
  844. IN UINT32 NewEventSize
  845. )
  846. {
  847. EFI_STATUS Status;
  848. UINTN Index;
  849. CC_EVENT_LOG_AREA_STRUCT *EventLogAreaStruct;
  850. if (EventLogFormat != EFI_CC_EVENT_LOG_FORMAT_TCG_2) {
  851. ASSERT (FALSE);
  852. return EFI_INVALID_PARAMETER;
  853. }
  854. Index = 0;
  855. //
  856. // Record to normal event log
  857. //
  858. EventLogAreaStruct = &mTdxDxeData.EventLogAreaStruct[Index];
  859. if (EventLogAreaStruct->EventLogTruncated) {
  860. return EFI_VOLUME_FULL;
  861. }
  862. Status = TcgCommLogEvent (
  863. EventLogAreaStruct,
  864. NewEventHdr,
  865. NewEventHdrSize,
  866. NewEventData,
  867. NewEventSize
  868. );
  869. if (Status == EFI_OUT_OF_RESOURCES) {
  870. EventLogAreaStruct->EventLogTruncated = TRUE;
  871. return EFI_VOLUME_FULL;
  872. } else if (Status == EFI_SUCCESS) {
  873. EventLogAreaStruct->EventLogStarted = TRUE;
  874. }
  875. //
  876. // If GetEventLog is called, record to FinalEventsTable, too.
  877. //
  878. if (mTdxDxeData.GetEventLogCalled[Index]) {
  879. if (mTdxDxeData.FinalEventsTable[Index] == NULL) {
  880. //
  881. // no need for FinalEventsTable
  882. //
  883. return EFI_SUCCESS;
  884. }
  885. EventLogAreaStruct = &mTdxDxeData.FinalEventLogAreaStruct[Index];
  886. if (EventLogAreaStruct->EventLogTruncated) {
  887. return EFI_VOLUME_FULL;
  888. }
  889. Status = TcgCommLogEvent (
  890. EventLogAreaStruct,
  891. NewEventHdr,
  892. NewEventHdrSize,
  893. NewEventData,
  894. NewEventSize
  895. );
  896. if (Status == EFI_OUT_OF_RESOURCES) {
  897. EventLogAreaStruct->EventLogTruncated = TRUE;
  898. return EFI_VOLUME_FULL;
  899. } else if (Status == EFI_SUCCESS) {
  900. EventLogAreaStruct->EventLogStarted = TRUE;
  901. //
  902. // Increase the NumberOfEvents in FinalEventsTable
  903. //
  904. (mTdxDxeData.FinalEventsTable[Index])->NumberOfEvents++;
  905. DEBUG ((DEBUG_INFO, "FinalEventsTable->NumberOfEvents - 0x%x\n", (mTdxDxeData.FinalEventsTable[Index])->NumberOfEvents));
  906. DEBUG ((DEBUG_INFO, " Size - 0x%x\n", (UINTN)EventLogAreaStruct->EventLogSize));
  907. }
  908. }
  909. return Status;
  910. }
  911. /**
  912. Get TPML_DIGEST_VALUES compact binary buffer size.
  913. @param[in] DigestListBin TPML_DIGEST_VALUES compact binary buffer.
  914. @return TPML_DIGEST_VALUES compact binary buffer size.
  915. **/
  916. UINT32
  917. GetDigestListBinSize (
  918. IN VOID *DigestListBin
  919. )
  920. {
  921. UINTN Index;
  922. UINT16 DigestSize;
  923. UINT32 TotalSize;
  924. UINT32 Count;
  925. TPMI_ALG_HASH HashAlg;
  926. Count = ReadUnaligned32 (DigestListBin);
  927. TotalSize = sizeof (Count);
  928. DigestListBin = (UINT8 *)DigestListBin + sizeof (Count);
  929. for (Index = 0; Index < Count; Index++) {
  930. HashAlg = ReadUnaligned16 (DigestListBin);
  931. TotalSize += sizeof (HashAlg);
  932. DigestListBin = (UINT8 *)DigestListBin + sizeof (HashAlg);
  933. DigestSize = GetHashSizeFromAlgo (HashAlg);
  934. TotalSize += DigestSize;
  935. DigestListBin = (UINT8 *)DigestListBin + DigestSize;
  936. }
  937. return TotalSize;
  938. }
  939. /**
  940. Copy TPML_DIGEST_VALUES compact binary into a buffer
  941. @param[in,out] Buffer Buffer to hold copied TPML_DIGEST_VALUES compact binary.
  942. @param[in] DigestListBin TPML_DIGEST_VALUES compact binary buffer.
  943. @param[in] HashAlgorithmMask HASH bits corresponding to the desired digests to copy.
  944. @param[out] HashAlgorithmMaskCopied Pointer to HASH bits corresponding to the digests copied.
  945. @return The end of buffer to hold TPML_DIGEST_VALUES compact binary.
  946. **/
  947. VOID *
  948. CopyDigestListBinToBuffer (
  949. IN OUT VOID *Buffer,
  950. IN VOID *DigestListBin,
  951. IN UINT32 HashAlgorithmMask,
  952. OUT UINT32 *HashAlgorithmMaskCopied
  953. )
  954. {
  955. UINTN Index;
  956. UINT16 DigestSize;
  957. UINT32 Count;
  958. TPMI_ALG_HASH HashAlg;
  959. UINT32 DigestListCount;
  960. UINT32 *DigestListCountPtr;
  961. DigestListCountPtr = (UINT32 *)Buffer;
  962. DigestListCount = 0;
  963. *HashAlgorithmMaskCopied = 0;
  964. Count = ReadUnaligned32 (DigestListBin);
  965. Buffer = (UINT8 *)Buffer + sizeof (Count);
  966. DigestListBin = (UINT8 *)DigestListBin + sizeof (Count);
  967. for (Index = 0; Index < Count; Index++) {
  968. HashAlg = ReadUnaligned16 (DigestListBin);
  969. DigestListBin = (UINT8 *)DigestListBin + sizeof (HashAlg);
  970. DigestSize = GetHashSizeFromAlgo (HashAlg);
  971. if ((HashAlg & HashAlgorithmMask) != 0) {
  972. CopyMem (Buffer, &HashAlg, sizeof (HashAlg));
  973. Buffer = (UINT8 *)Buffer + sizeof (HashAlg);
  974. CopyMem (Buffer, DigestListBin, DigestSize);
  975. Buffer = (UINT8 *)Buffer + DigestSize;
  976. DigestListCount++;
  977. (*HashAlgorithmMaskCopied) |= GetHashMaskFromAlgo (HashAlg);
  978. } else {
  979. DEBUG ((DEBUG_ERROR, "WARNING: CopyDigestListBinToBuffer Event log has HashAlg unsupported by PCR bank (0x%x)\n", HashAlg));
  980. }
  981. DigestListBin = (UINT8 *)DigestListBin + DigestSize;
  982. }
  983. WriteUnaligned32 (DigestListCountPtr, DigestListCount);
  984. return Buffer;
  985. }
  986. /**
  987. Add a new entry to the Event Log. The call chain is like below:
  988. TdxDxeLogHashEvent -> TdxDxeLogEvent -> TcgCommonLogEvent
  989. Before this function is called, the event information (including the digest)
  990. is ready.
  991. @param[in] DigestList A list of digest.
  992. @param[in,out] NewEventHdr Pointer to a TD_EVENT_HDR data structure.
  993. @param[in] NewEventData Pointer to the new event data.
  994. @retval EFI_SUCCESS The new event log entry was added.
  995. @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
  996. **/
  997. EFI_STATUS
  998. TdxDxeLogHashEvent (
  999. IN TPML_DIGEST_VALUES *DigestList,
  1000. IN OUT CC_EVENT_HDR *NewEventHdr,
  1001. IN UINT8 *NewEventData
  1002. )
  1003. {
  1004. EFI_STATUS Status;
  1005. EFI_TPL OldTpl;
  1006. EFI_STATUS RetStatus;
  1007. CC_EVENT CcEvent;
  1008. UINT8 *DigestBuffer;
  1009. UINT32 *EventSizePtr;
  1010. EFI_CC_EVENT_LOG_FORMAT LogFormat;
  1011. RetStatus = EFI_SUCCESS;
  1012. LogFormat = EFI_CC_EVENT_LOG_FORMAT_TCG_2;
  1013. ZeroMem (&CcEvent, sizeof (CcEvent));
  1014. CcEvent.MrIndex = NewEventHdr->MrIndex;
  1015. CcEvent.EventType = NewEventHdr->EventType;
  1016. DigestBuffer = (UINT8 *)&CcEvent.Digests;
  1017. EventSizePtr = CopyDigestListToBuffer (DigestBuffer, DigestList, HASH_ALG_SHA384);
  1018. CopyMem (EventSizePtr, &NewEventHdr->EventSize, sizeof (NewEventHdr->EventSize));
  1019. //
  1020. // Enter critical region
  1021. //
  1022. OldTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
  1023. Status = TdxDxeLogEvent (
  1024. LogFormat,
  1025. &CcEvent,
  1026. sizeof (CcEvent.MrIndex) + sizeof (CcEvent.EventType) + GetDigestListBinSize (DigestBuffer) + sizeof (CcEvent.EventSize),
  1027. NewEventData,
  1028. NewEventHdr->EventSize
  1029. );
  1030. if (Status != EFI_SUCCESS) {
  1031. RetStatus = Status;
  1032. }
  1033. gBS->RestoreTPL (OldTpl);
  1034. return RetStatus;
  1035. }
  1036. /**
  1037. Do a hash operation on a data buffer, extend a specific RTMR with the hash result,
  1038. and add an entry to the Event Log.
  1039. @param[in] Flags Bitmap providing additional information.
  1040. @param[in] HashData Physical address of the start of the data buffer
  1041. to be hashed, extended, and logged.
  1042. @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
  1043. @param[in, out] NewEventHdr Pointer to a TD_EVENT_HDR data structure.
  1044. @param[in] NewEventData Pointer to the new event data.
  1045. @retval EFI_SUCCESS Operation completed successfully.
  1046. @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
  1047. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  1048. **/
  1049. EFI_STATUS
  1050. TdxDxeHashLogExtendEvent (
  1051. IN UINT64 Flags,
  1052. IN UINT8 *HashData,
  1053. IN UINT64 HashDataLen,
  1054. IN OUT CC_EVENT_HDR *NewEventHdr,
  1055. IN UINT8 *NewEventData
  1056. )
  1057. {
  1058. EFI_STATUS Status;
  1059. TPML_DIGEST_VALUES DigestList;
  1060. CC_EVENT_HDR NoActionEvent;
  1061. if (NewEventHdr->EventType == EV_NO_ACTION) {
  1062. //
  1063. // Do not do RTMR extend for EV_NO_ACTION
  1064. //
  1065. Status = EFI_SUCCESS;
  1066. InitNoActionEvent (&NoActionEvent, NewEventHdr->EventSize);
  1067. if ((Flags & EFI_CC_FLAG_EXTEND_ONLY) == 0) {
  1068. Status = TdxDxeLogHashEvent (&(NoActionEvent.Digests), NewEventHdr, NewEventData);
  1069. }
  1070. return Status;
  1071. }
  1072. //
  1073. // According to UEFI Spec 2.10 Section 38.4.1 the mapping between MrIndex and Intel
  1074. // TDX Measurement Register is:
  1075. // MrIndex 0 <--> MRTD
  1076. // MrIndex 1-3 <--> RTMR[0-2]
  1077. // Only the RMTR registers can be extended in TDVF by HashAndExtend. So MrIndex will
  1078. // decreased by 1 before it is sent to HashAndExtend.
  1079. //
  1080. Status = HashAndExtend (
  1081. NewEventHdr->MrIndex - 1,
  1082. HashData,
  1083. (UINTN)HashDataLen,
  1084. &DigestList
  1085. );
  1086. if (!EFI_ERROR (Status)) {
  1087. if ((Flags & EFI_CC_FLAG_EXTEND_ONLY) == 0) {
  1088. Status = TdxDxeLogHashEvent (&DigestList, NewEventHdr, NewEventData);
  1089. }
  1090. }
  1091. return Status;
  1092. }
  1093. /**
  1094. The EFI_CC_MEASUREMENT_PROTOCOL HashLogExtendEvent function call provides callers with
  1095. an opportunity to extend and optionally log events without requiring
  1096. knowledge of actual TPM commands.
  1097. The extend operation will occur even if this function cannot create an event
  1098. log entry (e.g. due to the event log being full).
  1099. @param[in] This Indicates the calling context
  1100. @param[in] Flags Bitmap providing additional information.
  1101. @param[in] DataToHash Physical address of the start of the data buffer to be hashed.
  1102. @param[in] DataToHashLen The length in bytes of the buffer referenced by DataToHash.
  1103. @param[in] Event Pointer to data buffer containing information about the event.
  1104. @retval EFI_SUCCESS Operation completed successfully.
  1105. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  1106. @retval EFI_VOLUME_FULL The extend operation occurred, but the event could not be written to one or more event logs.
  1107. @retval EFI_INVALID_PARAMETER One or more of the parameters are incorrect.
  1108. @retval EFI_UNSUPPORTED The PE/COFF image type is not supported.
  1109. **/
  1110. EFI_STATUS
  1111. EFIAPI
  1112. TdHashLogExtendEvent (
  1113. IN EFI_CC_MEASUREMENT_PROTOCOL *This,
  1114. IN UINT64 Flags,
  1115. IN EFI_PHYSICAL_ADDRESS DataToHash,
  1116. IN UINT64 DataToHashLen,
  1117. IN EFI_CC_EVENT *CcEvent
  1118. )
  1119. {
  1120. EFI_STATUS Status;
  1121. CC_EVENT_HDR NewEventHdr;
  1122. TPML_DIGEST_VALUES DigestList;
  1123. DEBUG ((DEBUG_VERBOSE, "TdHashLogExtendEvent ...\n"));
  1124. if ((This == NULL) || (CcEvent == NULL)) {
  1125. return EFI_INVALID_PARAMETER;
  1126. }
  1127. //
  1128. // Do not check hash data size for EV_NO_ACTION event.
  1129. //
  1130. if ((CcEvent->Header.EventType != EV_NO_ACTION) && (DataToHash == 0)) {
  1131. return EFI_INVALID_PARAMETER;
  1132. }
  1133. if (CcEvent->Size < CcEvent->Header.HeaderSize + sizeof (UINT32)) {
  1134. return EFI_INVALID_PARAMETER;
  1135. }
  1136. if (CcEvent->Header.MrIndex == CC_MR_INDEX_0_MRTD) {
  1137. DEBUG ((DEBUG_ERROR, "%a: MRTD cannot be extended in TDVF.\n", __FUNCTION__));
  1138. return EFI_INVALID_PARAMETER;
  1139. }
  1140. if (CcEvent->Header.MrIndex >= CC_MR_INDEX_INVALID) {
  1141. DEBUG ((DEBUG_ERROR, "%a: MrIndex is invalid. (%d)\n", __FUNCTION__, CcEvent->Header.MrIndex));
  1142. return EFI_INVALID_PARAMETER;
  1143. }
  1144. NewEventHdr.MrIndex = CcEvent->Header.MrIndex;
  1145. NewEventHdr.EventType = CcEvent->Header.EventType;
  1146. NewEventHdr.EventSize = CcEvent->Size - sizeof (UINT32) - CcEvent->Header.HeaderSize;
  1147. if ((Flags & EFI_CC_FLAG_PE_COFF_IMAGE) != 0) {
  1148. //
  1149. // According to UEFI Spec 2.10 Section 38.4.1 the mapping between MrIndex and Intel
  1150. // TDX Measurement Register is:
  1151. // MrIndex 0 <--> MRTD
  1152. // MrIndex 1-3 <--> RTMR[0-2]
  1153. // Only the RMTR registers can be extended in TDVF by HashAndExtend. So MrIndex will
  1154. // decreased by 1 before it is sent to MeasurePeImageAndExtend.
  1155. //
  1156. Status = MeasurePeImageAndExtend (
  1157. NewEventHdr.MrIndex - 1,
  1158. DataToHash,
  1159. (UINTN)DataToHashLen,
  1160. &DigestList
  1161. );
  1162. if (!EFI_ERROR (Status)) {
  1163. if ((Flags & EFI_CC_FLAG_EXTEND_ONLY) == 0) {
  1164. Status = TdxDxeLogHashEvent (&DigestList, &NewEventHdr, CcEvent->Event);
  1165. }
  1166. }
  1167. } else {
  1168. Status = TdxDxeHashLogExtendEvent (
  1169. Flags,
  1170. (UINT8 *)(UINTN)DataToHash,
  1171. DataToHashLen,
  1172. &NewEventHdr,
  1173. CcEvent->Event
  1174. );
  1175. }
  1176. DEBUG ((DEBUG_VERBOSE, "TdHashLogExtendEvent - %r\n", Status));
  1177. return Status;
  1178. }
  1179. EFI_CC_MEASUREMENT_PROTOCOL mTdProtocol = {
  1180. TdGetCapability,
  1181. TdGetEventLog,
  1182. TdHashLogExtendEvent,
  1183. TdMapPcrToMrIndex,
  1184. };
  1185. #define TD_HASH_COUNT 1
  1186. #define TEMP_BUF_LEN (sizeof(TCG_EfiSpecIDEventStruct) + sizeof(UINT32) \
  1187. + (TD_HASH_COUNT * sizeof(TCG_EfiSpecIdEventAlgorithmSize)) + sizeof(UINT8))
  1188. /**
  1189. Initialize the TD Event Log and log events passed from the PEI phase.
  1190. @retval EFI_SUCCESS Operation completed successfully.
  1191. @retval EFI_OUT_OF_RESOURCES Out of memory.
  1192. **/
  1193. EFI_STATUS
  1194. SetupCcEventLog (
  1195. VOID
  1196. )
  1197. {
  1198. EFI_STATUS Status;
  1199. EFI_PHYSICAL_ADDRESS Lasa;
  1200. UINTN Index;
  1201. TCG_EfiSpecIDEventStruct *TcgEfiSpecIdEventStruct;
  1202. UINT8 TempBuf[TEMP_BUF_LEN];
  1203. TCG_PCR_EVENT_HDR SpecIdEvent;
  1204. TCG_EfiSpecIdEventAlgorithmSize *DigestSize;
  1205. TCG_EfiSpecIdEventAlgorithmSize *TempDigestSize;
  1206. UINT8 *VendorInfoSize;
  1207. UINT32 NumberOfAlgorithms;
  1208. EFI_CC_EVENT_LOG_FORMAT LogFormat;
  1209. EFI_PEI_HOB_POINTERS GuidHob;
  1210. CC_EVENT_HDR NoActionEvent;
  1211. Status = EFI_SUCCESS;
  1212. DEBUG ((DEBUG_INFO, "SetupCcEventLog\n"));
  1213. Index = 0;
  1214. LogFormat = EFI_CC_EVENT_LOG_FORMAT_TCG_2;
  1215. //
  1216. // 1. Create Log Area
  1217. //
  1218. mTdxDxeData.EventLogAreaStruct[Index].EventLogFormat = LogFormat;
  1219. // allocate pages for TD Event log
  1220. Status = gBS->AllocatePages (
  1221. AllocateAnyPages,
  1222. EfiACPIMemoryNVS,
  1223. EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcgLogAreaMinLen)),
  1224. &Lasa
  1225. );
  1226. if (EFI_ERROR (Status)) {
  1227. return Status;
  1228. }
  1229. mTdxDxeData.EventLogAreaStruct[Index].Lasa = Lasa;
  1230. mTdxDxeData.EventLogAreaStruct[Index].Laml = PcdGet32 (PcdTcgLogAreaMinLen);
  1231. mTdxDxeData.EventLogAreaStruct[Index].Next800155EventOffset = 0;
  1232. //
  1233. // Report TD event log address and length, so that they can be reported in
  1234. // TD ACPI table. Ignore the return status, because those fields are optional.
  1235. //
  1236. PcdSet32S (PcdCcEventlogAcpiTableLaml, (UINT32)mTdxDxeData.EventLogAreaStruct[Index].Laml);
  1237. PcdSet64S (PcdCcEventlogAcpiTableLasa, mTdxDxeData.EventLogAreaStruct[Index].Lasa);
  1238. //
  1239. // To initialize them as 0xFF is recommended
  1240. // because the OS can know the last entry for that.
  1241. //
  1242. SetMem ((VOID *)(UINTN)Lasa, PcdGet32 (PcdTcgLogAreaMinLen), 0xFF);
  1243. //
  1244. // Create first entry for Log Header Entry Data
  1245. //
  1246. //
  1247. // TcgEfiSpecIdEventStruct
  1248. //
  1249. TcgEfiSpecIdEventStruct = (TCG_EfiSpecIDEventStruct *)TempBuf;
  1250. CopyMem (TcgEfiSpecIdEventStruct->signature, TCG_EfiSpecIDEventStruct_SIGNATURE_03, sizeof (TcgEfiSpecIdEventStruct->signature));
  1251. TcgEfiSpecIdEventStruct->platformClass = PcdGet8 (PcdTpmPlatformClass);
  1252. TcgEfiSpecIdEventStruct->specVersionMajor = TCG_EfiSpecIDEventStruct_SPEC_VERSION_MAJOR_TPM2;
  1253. TcgEfiSpecIdEventStruct->specVersionMinor = TCG_EfiSpecIDEventStruct_SPEC_VERSION_MINOR_TPM2;
  1254. TcgEfiSpecIdEventStruct->specErrata = TCG_EfiSpecIDEventStruct_SPEC_ERRATA_TPM2;
  1255. TcgEfiSpecIdEventStruct->uintnSize = sizeof (UINTN)/sizeof (UINT32);
  1256. NumberOfAlgorithms = 0;
  1257. DigestSize = (TCG_EfiSpecIdEventAlgorithmSize *)((UINT8 *)TcgEfiSpecIdEventStruct
  1258. + sizeof (*TcgEfiSpecIdEventStruct)
  1259. + sizeof (NumberOfAlgorithms));
  1260. TempDigestSize = DigestSize;
  1261. TempDigestSize += NumberOfAlgorithms;
  1262. TempDigestSize->algorithmId = TPM_ALG_SHA384;
  1263. TempDigestSize->digestSize = SHA384_DIGEST_SIZE;
  1264. NumberOfAlgorithms++;
  1265. CopyMem (TcgEfiSpecIdEventStruct + 1, &NumberOfAlgorithms, sizeof (NumberOfAlgorithms));
  1266. TempDigestSize = DigestSize;
  1267. TempDigestSize += NumberOfAlgorithms;
  1268. VendorInfoSize = (UINT8 *)TempDigestSize;
  1269. *VendorInfoSize = 0;
  1270. SpecIdEvent.PCRIndex = 1; // PCRIndex 0 maps to MrIndex 1
  1271. SpecIdEvent.EventType = EV_NO_ACTION;
  1272. ZeroMem (&SpecIdEvent.Digest, sizeof (SpecIdEvent.Digest));
  1273. SpecIdEvent.EventSize = (UINT32)GetTcgEfiSpecIdEventStructSize (TcgEfiSpecIdEventStruct);
  1274. //
  1275. // TD Event log re-use the spec of TCG2 Event log.
  1276. // Log TcgEfiSpecIdEventStruct as the first Event. Event format is TCG_PCR_EVENT.
  1277. // TCG EFI Protocol Spec. Section 5.3 Event Log Header
  1278. // TCG PC Client PFP spec. Section 9.2 Measurement Event Entries and Log
  1279. //
  1280. Status = TdxDxeLogEvent (
  1281. LogFormat,
  1282. &SpecIdEvent,
  1283. sizeof (SpecIdEvent),
  1284. (UINT8 *)TcgEfiSpecIdEventStruct,
  1285. SpecIdEvent.EventSize
  1286. );
  1287. //
  1288. // record the offset at the end of 800-155 event.
  1289. // the future 800-155 event can be inserted here.
  1290. //
  1291. mTdxDxeData.EventLogAreaStruct[Index].Next800155EventOffset = mTdxDxeData.EventLogAreaStruct[Index].EventLogSize;
  1292. //
  1293. // Tcg800155PlatformIdEvent. Event format is TCG_PCR_EVENT2
  1294. //
  1295. GuidHob.Guid = GetFirstGuidHob (&gTcg800155PlatformIdEventHobGuid);
  1296. while (GuidHob.Guid != NULL) {
  1297. InitNoActionEvent (&NoActionEvent, GET_GUID_HOB_DATA_SIZE (GuidHob.Guid));
  1298. Status = TdxDxeLogEvent (
  1299. LogFormat,
  1300. &NoActionEvent,
  1301. sizeof (NoActionEvent.MrIndex) + sizeof (NoActionEvent.EventType) + GetDigestListBinSize (&NoActionEvent.Digests) + sizeof (NoActionEvent.EventSize),
  1302. GET_GUID_HOB_DATA (GuidHob.Guid),
  1303. GET_GUID_HOB_DATA_SIZE (GuidHob.Guid)
  1304. );
  1305. GuidHob.Guid = GET_NEXT_HOB (GuidHob);
  1306. GuidHob.Guid = GetNextGuidHob (&gTcg800155PlatformIdEventHobGuid, GuidHob.Guid);
  1307. }
  1308. //
  1309. // 2. Create Final Log Area
  1310. //
  1311. Status = gBS->AllocatePages (
  1312. AllocateAnyPages,
  1313. EfiACPIMemoryNVS,
  1314. EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcg2FinalLogAreaLen)),
  1315. &Lasa
  1316. );
  1317. if (EFI_ERROR (Status)) {
  1318. return Status;
  1319. }
  1320. SetMem ((VOID *)(UINTN)Lasa, PcdGet32 (PcdTcg2FinalLogAreaLen), 0xFF);
  1321. //
  1322. // Initialize
  1323. //
  1324. mTdxDxeData.FinalEventsTable[Index] = (VOID *)(UINTN)Lasa;
  1325. (mTdxDxeData.FinalEventsTable[Index])->Version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
  1326. (mTdxDxeData.FinalEventsTable[Index])->NumberOfEvents = 0;
  1327. mTdxDxeData.FinalEventLogAreaStruct[Index].EventLogFormat = LogFormat;
  1328. mTdxDxeData.FinalEventLogAreaStruct[Index].Lasa = Lasa + sizeof (EFI_CC_FINAL_EVENTS_TABLE);
  1329. mTdxDxeData.FinalEventLogAreaStruct[Index].Laml = PcdGet32 (PcdTcg2FinalLogAreaLen) - sizeof (EFI_CC_FINAL_EVENTS_TABLE);
  1330. mTdxDxeData.FinalEventLogAreaStruct[Index].EventLogSize = 0;
  1331. mTdxDxeData.FinalEventLogAreaStruct[Index].LastEvent = (VOID *)(UINTN)mTdxDxeData.FinalEventLogAreaStruct[Index].Lasa;
  1332. mTdxDxeData.FinalEventLogAreaStruct[Index].EventLogStarted = FALSE;
  1333. mTdxDxeData.FinalEventLogAreaStruct[Index].EventLogTruncated = FALSE;
  1334. mTdxDxeData.FinalEventLogAreaStruct[Index].Next800155EventOffset = 0;
  1335. //
  1336. // Install to configuration table for EFI_CC_EVENT_LOG_FORMAT_TCG_2
  1337. //
  1338. Status = gBS->InstallConfigurationTable (&gEfiCcFinalEventsTableGuid, (VOID *)mTdxDxeData.FinalEventsTable[Index]);
  1339. if (EFI_ERROR (Status)) {
  1340. return Status;
  1341. }
  1342. return Status;
  1343. }
  1344. /**
  1345. Measure and log an action string, and extend the measurement result into RTMR.
  1346. @param[in] MrIndex MrIndex to extend
  1347. @param[in] String A specific string that indicates an Action event.
  1348. @retval EFI_SUCCESS Operation completed successfully.
  1349. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  1350. **/
  1351. EFI_STATUS
  1352. TdMeasureAction (
  1353. IN UINT32 MrIndex,
  1354. IN CHAR8 *String
  1355. )
  1356. {
  1357. CC_EVENT_HDR CcEvent;
  1358. CcEvent.MrIndex = MrIndex;
  1359. CcEvent.EventType = EV_EFI_ACTION;
  1360. CcEvent.EventSize = (UINT32)AsciiStrLen (String);
  1361. return TdxDxeHashLogExtendEvent (
  1362. 0,
  1363. (UINT8 *)String,
  1364. CcEvent.EventSize,
  1365. &CcEvent,
  1366. (UINT8 *)String
  1367. );
  1368. }
  1369. /**
  1370. Measure and log EFI handoff tables, and extend the measurement result into PCR[1].
  1371. @retval EFI_SUCCESS Operation completed successfully.
  1372. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  1373. **/
  1374. EFI_STATUS
  1375. MeasureHandoffTables (
  1376. VOID
  1377. )
  1378. {
  1379. EFI_STATUS Status;
  1380. CC_EVENT_HDR CcEvent;
  1381. EFI_HANDOFF_TABLE_POINTERS HandoffTables;
  1382. UINTN ProcessorNum;
  1383. EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;
  1384. ProcessorLocBuf = NULL;
  1385. Status = EFI_SUCCESS;
  1386. if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_SERVER) {
  1387. //
  1388. // Tcg Server spec.
  1389. // Measure each processor EFI_CPU_PHYSICAL_LOCATION with EV_TABLE_OF_DEVICES to PCR[1]
  1390. //
  1391. Status = GetProcessorsCpuLocation (&ProcessorLocBuf, &ProcessorNum);
  1392. if (!EFI_ERROR (Status)) {
  1393. CcEvent.MrIndex = MapPcrToMrIndex (1);
  1394. CcEvent.EventType = EV_TABLE_OF_DEVICES;
  1395. CcEvent.EventSize = sizeof (HandoffTables);
  1396. HandoffTables.NumberOfTables = 1;
  1397. HandoffTables.TableEntry[0].VendorGuid = gEfiMpServiceProtocolGuid;
  1398. HandoffTables.TableEntry[0].VendorTable = ProcessorLocBuf;
  1399. Status = TdxDxeHashLogExtendEvent (
  1400. 0,
  1401. (UINT8 *)(UINTN)ProcessorLocBuf,
  1402. sizeof (EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,
  1403. &CcEvent,
  1404. (UINT8 *)&HandoffTables
  1405. );
  1406. FreePool (ProcessorLocBuf);
  1407. }
  1408. }
  1409. return Status;
  1410. }
  1411. /**
  1412. Measure and log Separator event, and extend the measurement result into a specific PCR.
  1413. @param[in] PCRIndex PCR index.
  1414. @retval EFI_SUCCESS Operation completed successfully.
  1415. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  1416. **/
  1417. EFI_STATUS
  1418. MeasureSeparatorEvent (
  1419. IN UINT32 MrIndex
  1420. )
  1421. {
  1422. CC_EVENT_HDR CcEvent;
  1423. UINT32 EventData;
  1424. DEBUG ((DEBUG_INFO, "MeasureSeparatorEvent to Rtmr - %d\n", MrIndex));
  1425. EventData = 0;
  1426. CcEvent.MrIndex = MrIndex;
  1427. CcEvent.EventType = EV_SEPARATOR;
  1428. CcEvent.EventSize = (UINT32)sizeof (EventData);
  1429. return TdxDxeHashLogExtendEvent (
  1430. 0,
  1431. (UINT8 *)&EventData,
  1432. sizeof (EventData),
  1433. &CcEvent,
  1434. (UINT8 *)&EventData
  1435. );
  1436. }
  1437. /**
  1438. Measure and log an EFI variable, and extend the measurement result into a specific RTMR.
  1439. @param[in] MrIndex RTMR Index.
  1440. @param[in] EventType Event type.
  1441. @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
  1442. @param[in] VendorGuid A unique identifier for the vendor.
  1443. @param[in] VarData The content of the variable data.
  1444. @param[in] VarSize The size of the variable data.
  1445. @retval EFI_SUCCESS Operation completed successfully.
  1446. @retval EFI_OUT_OF_RESOURCES Out of memory.
  1447. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  1448. **/
  1449. EFI_STATUS
  1450. MeasureVariable (
  1451. IN UINT32 MrIndex,
  1452. IN TCG_EVENTTYPE EventType,
  1453. IN CHAR16 *VarName,
  1454. IN EFI_GUID *VendorGuid,
  1455. IN VOID *VarData,
  1456. IN UINTN VarSize
  1457. )
  1458. {
  1459. EFI_STATUS Status;
  1460. CC_EVENT_HDR CcEvent;
  1461. UINTN VarNameLength;
  1462. UEFI_VARIABLE_DATA *VarLog;
  1463. DEBUG ((DEBUG_INFO, "TdTcg2Dxe: MeasureVariable (Rtmr - %x, EventType - %x, ", (UINTN)MrIndex, (UINTN)EventType));
  1464. DEBUG ((DEBUG_INFO, "VariableName - %s, VendorGuid - %g)\n", VarName, VendorGuid));
  1465. VarNameLength = StrLen (VarName);
  1466. CcEvent.MrIndex = MrIndex;
  1467. CcEvent.EventType = EventType;
  1468. CcEvent.EventSize = (UINT32)(sizeof (*VarLog) + VarNameLength * sizeof (*VarName) + VarSize
  1469. - sizeof (VarLog->UnicodeName) - sizeof (VarLog->VariableData));
  1470. VarLog = (UEFI_VARIABLE_DATA *)AllocatePool (CcEvent.EventSize);
  1471. if (VarLog == NULL) {
  1472. return EFI_OUT_OF_RESOURCES;
  1473. }
  1474. VarLog->VariableName = *VendorGuid;
  1475. VarLog->UnicodeNameLength = VarNameLength;
  1476. VarLog->VariableDataLength = VarSize;
  1477. CopyMem (
  1478. VarLog->UnicodeName,
  1479. VarName,
  1480. VarNameLength * sizeof (*VarName)
  1481. );
  1482. if ((VarSize != 0) && (VarData != NULL)) {
  1483. CopyMem (
  1484. (CHAR16 *)VarLog->UnicodeName + VarNameLength,
  1485. VarData,
  1486. VarSize
  1487. );
  1488. }
  1489. if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {
  1490. //
  1491. // Digest is the event data (UEFI_VARIABLE_DATA)
  1492. //
  1493. Status = TdxDxeHashLogExtendEvent (
  1494. 0,
  1495. (UINT8 *)VarLog,
  1496. CcEvent.EventSize,
  1497. &CcEvent,
  1498. (UINT8 *)VarLog
  1499. );
  1500. } else {
  1501. ASSERT (VarData != NULL);
  1502. Status = TdxDxeHashLogExtendEvent (
  1503. 0,
  1504. (UINT8 *)VarData,
  1505. VarSize,
  1506. &CcEvent,
  1507. (UINT8 *)VarLog
  1508. );
  1509. }
  1510. FreePool (VarLog);
  1511. return Status;
  1512. }
  1513. /**
  1514. Read then Measure and log an EFI variable, and extend the measurement result into a specific RTMR.
  1515. @param[in] MrIndex RTMR Index.
  1516. @param[in] EventType Event type.
  1517. @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
  1518. @param[in] VendorGuid A unique identifier for the vendor.
  1519. @param[out] VarSize The size of the variable data.
  1520. @param[out] VarData Pointer to the content of the variable.
  1521. @retval EFI_SUCCESS Operation completed successfully.
  1522. @retval EFI_OUT_OF_RESOURCES Out of memory.
  1523. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  1524. **/
  1525. EFI_STATUS
  1526. ReadAndMeasureVariable (
  1527. IN UINT32 MrIndex,
  1528. IN TCG_EVENTTYPE EventType,
  1529. IN CHAR16 *VarName,
  1530. IN EFI_GUID *VendorGuid,
  1531. OUT UINTN *VarSize,
  1532. OUT VOID **VarData
  1533. )
  1534. {
  1535. EFI_STATUS Status;
  1536. Status = GetVariable2 (VarName, VendorGuid, VarData, VarSize);
  1537. if (EventType == EV_EFI_VARIABLE_DRIVER_CONFIG) {
  1538. if (EFI_ERROR (Status)) {
  1539. //
  1540. // It is valid case, so we need handle it.
  1541. //
  1542. *VarData = NULL;
  1543. *VarSize = 0;
  1544. }
  1545. } else {
  1546. //
  1547. // if status error, VarData is freed and set NULL by GetVariable2
  1548. //
  1549. if (EFI_ERROR (Status)) {
  1550. return EFI_NOT_FOUND;
  1551. }
  1552. }
  1553. Status = MeasureVariable (
  1554. MrIndex,
  1555. EventType,
  1556. VarName,
  1557. VendorGuid,
  1558. *VarData,
  1559. *VarSize
  1560. );
  1561. return Status;
  1562. }
  1563. /**
  1564. Read then Measure and log an EFI boot variable, and extend the measurement result into PCR[1].
  1565. according to TCG PC Client PFP spec 0021 Section 2.4.4.2
  1566. @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
  1567. @param[in] VendorGuid A unique identifier for the vendor.
  1568. @param[out] VarSize The size of the variable data.
  1569. @param[out] VarData Pointer to the content of the variable.
  1570. @retval EFI_SUCCESS Operation completed successfully.
  1571. @retval EFI_OUT_OF_RESOURCES Out of memory.
  1572. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  1573. **/
  1574. EFI_STATUS
  1575. ReadAndMeasureBootVariable (
  1576. IN CHAR16 *VarName,
  1577. IN EFI_GUID *VendorGuid,
  1578. OUT UINTN *VarSize,
  1579. OUT VOID **VarData
  1580. )
  1581. {
  1582. return ReadAndMeasureVariable (
  1583. MapPcrToMrIndex (1),
  1584. EV_EFI_VARIABLE_BOOT,
  1585. VarName,
  1586. VendorGuid,
  1587. VarSize,
  1588. VarData
  1589. );
  1590. }
  1591. /**
  1592. Read then Measure and log an EFI Secure variable, and extend the measurement result into PCR[7].
  1593. @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
  1594. @param[in] VendorGuid A unique identifier for the vendor.
  1595. @param[out] VarSize The size of the variable data.
  1596. @param[out] VarData Pointer to the content of the variable.
  1597. @retval EFI_SUCCESS Operation completed successfully.
  1598. @retval EFI_OUT_OF_RESOURCES Out of memory.
  1599. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  1600. **/
  1601. EFI_STATUS
  1602. ReadAndMeasureSecureVariable (
  1603. IN CHAR16 *VarName,
  1604. IN EFI_GUID *VendorGuid,
  1605. OUT UINTN *VarSize,
  1606. OUT VOID **VarData
  1607. )
  1608. {
  1609. return ReadAndMeasureVariable (
  1610. MapPcrToMrIndex (7),
  1611. EV_EFI_VARIABLE_DRIVER_CONFIG,
  1612. VarName,
  1613. VendorGuid,
  1614. VarSize,
  1615. VarData
  1616. );
  1617. }
  1618. /**
  1619. Measure and log all EFI boot variables, and extend the measurement result into a specific PCR.
  1620. The EFI boot variables are BootOrder and Boot#### variables.
  1621. @retval EFI_SUCCESS Operation completed successfully.
  1622. @retval EFI_OUT_OF_RESOURCES Out of memory.
  1623. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  1624. **/
  1625. EFI_STATUS
  1626. MeasureAllBootVariables (
  1627. VOID
  1628. )
  1629. {
  1630. EFI_STATUS Status;
  1631. UINT16 *BootOrder;
  1632. UINTN BootCount;
  1633. UINTN Index;
  1634. VOID *BootVarData;
  1635. UINTN Size;
  1636. Status = ReadAndMeasureBootVariable (
  1637. mBootVarName,
  1638. &gEfiGlobalVariableGuid,
  1639. &BootCount,
  1640. (VOID **)&BootOrder
  1641. );
  1642. if ((Status == EFI_NOT_FOUND) || (BootOrder == NULL)) {
  1643. return EFI_SUCCESS;
  1644. }
  1645. if (EFI_ERROR (Status)) {
  1646. //
  1647. // BootOrder can't be NULL if status is not EFI_NOT_FOUND
  1648. //
  1649. FreePool (BootOrder);
  1650. return Status;
  1651. }
  1652. BootCount /= sizeof (*BootOrder);
  1653. for (Index = 0; Index < BootCount; Index++) {
  1654. UnicodeSPrint (mBootVarName, sizeof (mBootVarName), L"Boot%04x", BootOrder[Index]);
  1655. Status = ReadAndMeasureBootVariable (
  1656. mBootVarName,
  1657. &gEfiGlobalVariableGuid,
  1658. &Size,
  1659. &BootVarData
  1660. );
  1661. if (!EFI_ERROR (Status)) {
  1662. FreePool (BootVarData);
  1663. }
  1664. }
  1665. FreePool (BootOrder);
  1666. return EFI_SUCCESS;
  1667. }
  1668. /**
  1669. Measure and log all EFI Secure variables, and extend the measurement result into a specific PCR.
  1670. The EFI boot variables are BootOrder and Boot#### variables.
  1671. @retval EFI_SUCCESS Operation completed successfully.
  1672. @retval EFI_OUT_OF_RESOURCES Out of memory.
  1673. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  1674. **/
  1675. EFI_STATUS
  1676. MeasureAllSecureVariables (
  1677. VOID
  1678. )
  1679. {
  1680. EFI_STATUS Status;
  1681. VOID *Data;
  1682. UINTN DataSize;
  1683. UINTN Index;
  1684. Status = EFI_NOT_FOUND;
  1685. for (Index = 0; Index < sizeof (mVariableType)/sizeof (mVariableType[0]); Index++) {
  1686. Status = ReadAndMeasureSecureVariable (
  1687. mVariableType[Index].VariableName,
  1688. mVariableType[Index].VendorGuid,
  1689. &DataSize,
  1690. &Data
  1691. );
  1692. if (!EFI_ERROR (Status)) {
  1693. if (Data != NULL) {
  1694. FreePool (Data);
  1695. }
  1696. }
  1697. }
  1698. //
  1699. // Measure DBT if present and not empty
  1700. //
  1701. Status = GetVariable2 (EFI_IMAGE_SECURITY_DATABASE2, &gEfiImageSecurityDatabaseGuid, &Data, &DataSize);
  1702. if (!EFI_ERROR (Status)) {
  1703. Status = MeasureVariable (
  1704. MapPcrToMrIndex (7),
  1705. EV_EFI_VARIABLE_DRIVER_CONFIG,
  1706. EFI_IMAGE_SECURITY_DATABASE2,
  1707. &gEfiImageSecurityDatabaseGuid,
  1708. Data,
  1709. DataSize
  1710. );
  1711. FreePool (Data);
  1712. } else {
  1713. DEBUG ((DEBUG_INFO, "Skip measuring variable %s since it's deleted\n", EFI_IMAGE_SECURITY_DATABASE2));
  1714. }
  1715. return EFI_SUCCESS;
  1716. }
  1717. /**
  1718. Measure and log launch of FirmwareDebugger, and extend the measurement result into a specific PCR.
  1719. @retval EFI_SUCCESS Operation completed successfully.
  1720. @retval EFI_OUT_OF_RESOURCES Out of memory.
  1721. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  1722. **/
  1723. EFI_STATUS
  1724. MeasureLaunchOfFirmwareDebugger (
  1725. VOID
  1726. )
  1727. {
  1728. CC_EVENT_HDR CcEvent;
  1729. CcEvent.MrIndex = MapPcrToMrIndex (7);
  1730. CcEvent.EventType = EV_EFI_ACTION;
  1731. CcEvent.EventSize = sizeof (FIRMWARE_DEBUGGER_EVENT_STRING) - 1;
  1732. return TdxDxeHashLogExtendEvent (
  1733. 0,
  1734. (UINT8 *)FIRMWARE_DEBUGGER_EVENT_STRING,
  1735. sizeof (FIRMWARE_DEBUGGER_EVENT_STRING) - 1,
  1736. &CcEvent,
  1737. (UINT8 *)FIRMWARE_DEBUGGER_EVENT_STRING
  1738. );
  1739. }
  1740. /**
  1741. Measure and log all Secure Boot Policy, and extend the measurement result into a specific PCR.
  1742. Platform firmware adhering to the policy must therefore measure the following values into PCR[7]: (in order listed)
  1743. - The contents of the SecureBoot variable
  1744. - The contents of the PK variable
  1745. - The contents of the KEK variable
  1746. - The contents of the EFI_IMAGE_SECURITY_DATABASE variable
  1747. - The contents of the EFI_IMAGE_SECURITY_DATABASE1 variable
  1748. - Separator
  1749. - Entries in the EFI_IMAGE_SECURITY_DATABASE that are used to validate EFI Drivers or EFI Boot Applications in the boot path
  1750. NOTE: Because of the above, UEFI variables PK, KEK, EFI_IMAGE_SECURITY_DATABASE,
  1751. EFI_IMAGE_SECURITY_DATABASE1 and SecureBoot SHALL NOT be measured into PCR[3].
  1752. @param[in] Event Event whose notification function is being invoked
  1753. @param[in] Context Pointer to the notification function's context
  1754. **/
  1755. VOID
  1756. EFIAPI
  1757. MeasureSecureBootPolicy (
  1758. IN EFI_EVENT Event,
  1759. IN VOID *Context
  1760. )
  1761. {
  1762. EFI_STATUS Status;
  1763. VOID *Protocol;
  1764. Status = gBS->LocateProtocol (&gEfiVariableWriteArchProtocolGuid, NULL, (VOID **)&Protocol);
  1765. if (EFI_ERROR (Status)) {
  1766. return;
  1767. }
  1768. if (PcdGetBool (PcdFirmwareDebuggerInitialized)) {
  1769. Status = MeasureLaunchOfFirmwareDebugger ();
  1770. DEBUG ((DEBUG_INFO, "MeasureLaunchOfFirmwareDebugger - %r\n", Status));
  1771. }
  1772. Status = MeasureAllSecureVariables ();
  1773. DEBUG ((DEBUG_INFO, "MeasureAllSecureVariables - %r\n", Status));
  1774. //
  1775. // We need measure Separator(7) here, because this event must be between SecureBootPolicy (Configure)
  1776. // and ImageVerification (Authority)
  1777. // There might be a case that we need measure UEFI image from DriverOrder, besides BootOrder. So
  1778. // the Authority measurement happen before ReadToBoot event.
  1779. //
  1780. Status = MeasureSeparatorEvent (MapPcrToMrIndex (7));
  1781. DEBUG ((DEBUG_INFO, "MeasureSeparatorEvent - %r\n", Status));
  1782. return;
  1783. }
  1784. /**
  1785. Ready to Boot Event notification handler.
  1786. Sequence of OS boot events is measured in this event notification handler.
  1787. @param[in] Event Event whose notification function is being invoked
  1788. @param[in] Context Pointer to the notification function's context
  1789. **/
  1790. VOID
  1791. EFIAPI
  1792. OnReadyToBoot (
  1793. IN EFI_EVENT Event,
  1794. IN VOID *Context
  1795. )
  1796. {
  1797. EFI_STATUS Status;
  1798. PERF_START_EX (mImageHandle, "EventRec", "TdTcg2Dxe", 0, PERF_ID_CC_TCG2_DXE);
  1799. if (mBootAttempts == 0) {
  1800. //
  1801. // Measure handoff tables.
  1802. //
  1803. Status = MeasureHandoffTables ();
  1804. if (EFI_ERROR (Status)) {
  1805. DEBUG ((DEBUG_ERROR, "HOBs not Measured. Error!\n"));
  1806. }
  1807. //
  1808. // Measure BootOrder & Boot#### variables.
  1809. //
  1810. Status = MeasureAllBootVariables ();
  1811. if (EFI_ERROR (Status)) {
  1812. DEBUG ((DEBUG_ERROR, "Boot Variables not Measured. Error!\n"));
  1813. }
  1814. //
  1815. // 1. This is the first boot attempt.
  1816. //
  1817. Status = TdMeasureAction (
  1818. MapPcrToMrIndex (4),
  1819. EFI_CALLING_EFI_APPLICATION
  1820. );
  1821. if (EFI_ERROR (Status)) {
  1822. DEBUG ((DEBUG_ERROR, "%a not Measured. Error!\n", EFI_CALLING_EFI_APPLICATION));
  1823. }
  1824. //
  1825. // 2. Draw a line between pre-boot env and entering post-boot env.
  1826. // PCR[7] (is RTMR[0]) is already done.
  1827. //
  1828. Status = MeasureSeparatorEvent (1);
  1829. if (EFI_ERROR (Status)) {
  1830. DEBUG ((DEBUG_ERROR, "Separator Event not Measured. Error!\n"));
  1831. }
  1832. //
  1833. // 3. Measure GPT. It would be done in SAP driver.
  1834. //
  1835. //
  1836. // 4. Measure PE/COFF OS loader. It would be done in SAP driver.
  1837. //
  1838. //
  1839. // 5. Read & Measure variable. BootOrder already measured.
  1840. //
  1841. } else {
  1842. //
  1843. // 6. Not first attempt, meaning a return from last attempt
  1844. //
  1845. Status = TdMeasureAction (
  1846. MapPcrToMrIndex (4),
  1847. EFI_RETURNING_FROM_EFI_APPLICATION
  1848. );
  1849. if (EFI_ERROR (Status)) {
  1850. DEBUG ((DEBUG_ERROR, "%a not Measured. Error!\n", EFI_RETURNING_FROM_EFI_APPLICATION));
  1851. }
  1852. //
  1853. // 7. Next boot attempt, measure "Calling EFI Application from Boot Option" again
  1854. // TCG PC Client PFP spec Section 2.4.4.5 Step 4
  1855. //
  1856. Status = TdMeasureAction (
  1857. MapPcrToMrIndex (4),
  1858. EFI_CALLING_EFI_APPLICATION
  1859. );
  1860. if (EFI_ERROR (Status)) {
  1861. DEBUG ((DEBUG_ERROR, "%a not Measured. Error!\n", EFI_CALLING_EFI_APPLICATION));
  1862. }
  1863. }
  1864. DEBUG ((DEBUG_INFO, "TdTcg2Dxe Measure Data when ReadyToBoot\n"));
  1865. //
  1866. // Increase boot attempt counter.
  1867. //
  1868. mBootAttempts++;
  1869. PERF_END_EX (mImageHandle, "EventRec", "Tcg2Dxe", 0, PERF_ID_CC_TCG2_DXE + 1);
  1870. }
  1871. /**
  1872. Exit Boot Services Event notification handler.
  1873. Measure invocation and success of ExitBootServices.
  1874. @param[in] Event Event whose notification function is being invoked
  1875. @param[in] Context Pointer to the notification function's context
  1876. **/
  1877. VOID
  1878. EFIAPI
  1879. OnExitBootServices (
  1880. IN EFI_EVENT Event,
  1881. IN VOID *Context
  1882. )
  1883. {
  1884. EFI_STATUS Status;
  1885. //
  1886. // Measure invocation of ExitBootServices,
  1887. //
  1888. Status = TdMeasureAction (
  1889. MapPcrToMrIndex (5),
  1890. EFI_EXIT_BOOT_SERVICES_INVOCATION
  1891. );
  1892. if (EFI_ERROR (Status)) {
  1893. DEBUG ((DEBUG_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_INVOCATION));
  1894. }
  1895. //
  1896. // Measure success of ExitBootServices
  1897. //
  1898. Status = TdMeasureAction (
  1899. MapPcrToMrIndex (5),
  1900. EFI_EXIT_BOOT_SERVICES_SUCCEEDED
  1901. );
  1902. if (EFI_ERROR (Status)) {
  1903. DEBUG ((DEBUG_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_SUCCEEDED));
  1904. }
  1905. }
  1906. /**
  1907. Exit Boot Services Failed Event notification handler.
  1908. Measure Failure of ExitBootServices.
  1909. @param[in] Event Event whose notification function is being invoked
  1910. @param[in] Context Pointer to the notification function's context
  1911. **/
  1912. VOID
  1913. EFIAPI
  1914. OnExitBootServicesFailed (
  1915. IN EFI_EVENT Event,
  1916. IN VOID *Context
  1917. )
  1918. {
  1919. EFI_STATUS Status;
  1920. //
  1921. // Measure Failure of ExitBootServices,
  1922. //
  1923. Status = TdMeasureAction (
  1924. MapPcrToMrIndex (5),
  1925. EFI_EXIT_BOOT_SERVICES_FAILED
  1926. );
  1927. if (EFI_ERROR (Status)) {
  1928. DEBUG ((DEBUG_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_FAILED));
  1929. }
  1930. }
  1931. EFI_STATUS
  1932. SyncCcEvent (
  1933. VOID
  1934. )
  1935. {
  1936. EFI_STATUS Status;
  1937. EFI_PEI_HOB_POINTERS GuidHob;
  1938. VOID *CcEvent;
  1939. VOID *DigestListBin;
  1940. UINT32 DigestListBinSize;
  1941. UINT8 *Event;
  1942. UINT32 EventSize;
  1943. EFI_CC_EVENT_LOG_FORMAT LogFormat;
  1944. DEBUG ((DEBUG_INFO, "Sync Cc event from SEC\n"));
  1945. Status = EFI_SUCCESS;
  1946. LogFormat = EFI_CC_EVENT_LOG_FORMAT_TCG_2;
  1947. GuidHob.Guid = GetFirstGuidHob (&gCcEventEntryHobGuid);
  1948. while (!EFI_ERROR (Status) && GuidHob.Guid != NULL) {
  1949. CcEvent = AllocateCopyPool (GET_GUID_HOB_DATA_SIZE (GuidHob.Guid), GET_GUID_HOB_DATA (GuidHob.Guid));
  1950. if (CcEvent == NULL) {
  1951. return EFI_OUT_OF_RESOURCES;
  1952. }
  1953. GuidHob.Guid = GET_NEXT_HOB (GuidHob);
  1954. GuidHob.Guid = GetNextGuidHob (&gCcEventEntryHobGuid, GuidHob.Guid);
  1955. DigestListBin = (UINT8 *)CcEvent + sizeof (UINT32) + sizeof (TCG_EVENTTYPE);
  1956. DigestListBinSize = GetDigestListBinSize (DigestListBin);
  1957. //
  1958. // Event size.
  1959. //
  1960. EventSize = *(UINT32 *)((UINT8 *)DigestListBin + DigestListBinSize);
  1961. Event = (UINT8 *)DigestListBin + DigestListBinSize + sizeof (UINT32);
  1962. //
  1963. // Log the event
  1964. //
  1965. Status = TdxDxeLogEvent (
  1966. LogFormat,
  1967. CcEvent,
  1968. sizeof (UINT32) + sizeof (TCG_EVENTTYPE) + DigestListBinSize + sizeof (UINT32),
  1969. Event,
  1970. EventSize
  1971. );
  1972. DumpCcEvent ((CC_EVENT *)CcEvent);
  1973. FreePool (CcEvent);
  1974. }
  1975. return Status;
  1976. }
  1977. /**
  1978. Install TDVF ACPI Table when ACPI Table Protocol is available.
  1979. @param[in] Event Event whose notification function is being invoked
  1980. @param[in] Context Pointer to the notification function's context
  1981. **/
  1982. VOID
  1983. EFIAPI
  1984. InstallAcpiTable (
  1985. IN EFI_EVENT Event,
  1986. IN VOID *Context
  1987. )
  1988. {
  1989. UINTN TableKey;
  1990. EFI_STATUS Status;
  1991. EFI_ACPI_TABLE_PROTOCOL *AcpiTable;
  1992. UINT64 OemTableId;
  1993. Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **)&AcpiTable);
  1994. if (EFI_ERROR (Status)) {
  1995. DEBUG ((DEBUG_ERROR, "TD: AcpiTableProtocol is not installed. %r\n", Status));
  1996. return;
  1997. }
  1998. mTdxEventlogAcpiTemplate.Laml = (UINT64)PcdGet32 (PcdCcEventlogAcpiTableLaml);
  1999. mTdxEventlogAcpiTemplate.Lasa = PcdGet64 (PcdCcEventlogAcpiTableLasa);
  2000. CopyMem (mTdxEventlogAcpiTemplate.Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (mTdxEventlogAcpiTemplate.Header.OemId));
  2001. OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);
  2002. CopyMem (&mTdxEventlogAcpiTemplate.Header.OemTableId, &OemTableId, sizeof (UINT64));
  2003. mTdxEventlogAcpiTemplate.Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);
  2004. mTdxEventlogAcpiTemplate.Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);
  2005. mTdxEventlogAcpiTemplate.Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
  2006. //
  2007. // Construct ACPI Table
  2008. Status = AcpiTable->InstallAcpiTable (
  2009. AcpiTable,
  2010. &mTdxEventlogAcpiTemplate,
  2011. mTdxEventlogAcpiTemplate.Header.Length,
  2012. &TableKey
  2013. );
  2014. ASSERT_EFI_ERROR (Status);
  2015. DEBUG ((DEBUG_INFO, "TDVF Eventlog ACPI Table is installed.\n"));
  2016. }
  2017. /**
  2018. The function install TdTcg2 protocol.
  2019. @retval EFI_SUCCESS TdTcg2 protocol is installed.
  2020. @retval other Some error occurs.
  2021. **/
  2022. EFI_STATUS
  2023. InstallCcMeasurementProtocol (
  2024. VOID
  2025. )
  2026. {
  2027. EFI_STATUS Status;
  2028. EFI_HANDLE Handle;
  2029. Handle = NULL;
  2030. Status = gBS->InstallMultipleProtocolInterfaces (
  2031. &Handle,
  2032. &gEfiCcMeasurementProtocolGuid,
  2033. &mTdProtocol,
  2034. NULL
  2035. );
  2036. DEBUG ((DEBUG_INFO, "CcProtocol: Install %r\n", Status));
  2037. return Status;
  2038. }
  2039. /**
  2040. The driver's entry point. It publishes EFI Tcg2 Protocol.
  2041. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  2042. @param[in] SystemTable A pointer to the EFI System Table.
  2043. @retval EFI_SUCCESS The entry point is executed successfully.
  2044. @retval other Some error occurs when executing this entry point.
  2045. **/
  2046. EFI_STATUS
  2047. EFIAPI
  2048. DriverEntry (
  2049. IN EFI_HANDLE ImageHandle,
  2050. IN EFI_SYSTEM_TABLE *SystemTable
  2051. )
  2052. {
  2053. EFI_STATUS Status;
  2054. EFI_EVENT Event;
  2055. VOID *Registration;
  2056. if (!TdIsEnabled ()) {
  2057. return EFI_UNSUPPORTED;
  2058. }
  2059. mImageHandle = ImageHandle;
  2060. //
  2061. // Fill information
  2062. //
  2063. // ASSERT (TD_EVENT_LOG_AREA_COUNT_MAX == sizeof(mTEventInfo)/sizeof(mTcg2EventInfo[0]));
  2064. mTdxDxeData.BsCap.Size = sizeof (EFI_CC_BOOT_SERVICE_CAPABILITY);
  2065. mTdxDxeData.BsCap.ProtocolVersion.Major = 1;
  2066. mTdxDxeData.BsCap.ProtocolVersion.Minor = 0;
  2067. mTdxDxeData.BsCap.StructureVersion.Major = 1;
  2068. mTdxDxeData.BsCap.StructureVersion.Minor = 0;
  2069. //
  2070. // Get supported PCR and current Active PCRs
  2071. // For TD gueset HA384 is supported.
  2072. //
  2073. mTdxDxeData.BsCap.HashAlgorithmBitmap = HASH_ALG_SHA384;
  2074. // TD guest only supports EFI_TCG2_EVENT_LOG_FORMAT_TCG_2
  2075. mTdxDxeData.BsCap.SupportedEventLogs = EFI_CC_EVENT_LOG_FORMAT_TCG_2;
  2076. //
  2077. // Setup the log area and copy event log from hob list to it
  2078. //
  2079. Status = SetupCcEventLog ();
  2080. ASSERT_EFI_ERROR (Status);
  2081. if (!EFI_ERROR (Status)) {
  2082. Status = SyncCcEvent ();
  2083. ASSERT_EFI_ERROR (Status);
  2084. }
  2085. //
  2086. // Measure handoff tables, Boot#### variables etc.
  2087. //
  2088. Status = EfiCreateEventReadyToBootEx (
  2089. TPL_CALLBACK,
  2090. OnReadyToBoot,
  2091. NULL,
  2092. &Event
  2093. );
  2094. Status = gBS->CreateEventEx (
  2095. EVT_NOTIFY_SIGNAL,
  2096. TPL_NOTIFY,
  2097. OnExitBootServices,
  2098. NULL,
  2099. &gEfiEventExitBootServicesGuid,
  2100. &Event
  2101. );
  2102. //
  2103. // Measure Exit Boot Service failed
  2104. //
  2105. Status = gBS->CreateEventEx (
  2106. EVT_NOTIFY_SIGNAL,
  2107. TPL_NOTIFY,
  2108. OnExitBootServicesFailed,
  2109. NULL,
  2110. &gEventExitBootServicesFailedGuid,
  2111. &Event
  2112. );
  2113. //
  2114. // Create event callback, because we need access variable on SecureBootPolicyVariable
  2115. // We should use VariableWriteArch instead of VariableArch, because Variable driver
  2116. // may update SecureBoot value based on last setting.
  2117. //
  2118. EfiCreateProtocolNotifyEvent (&gEfiVariableWriteArchProtocolGuid, TPL_CALLBACK, MeasureSecureBootPolicy, NULL, &Registration);
  2119. //
  2120. // Install CcMeasurementProtocol
  2121. //
  2122. Status = InstallCcMeasurementProtocol ();
  2123. DEBUG ((DEBUG_INFO, "InstallCcMeasurementProtocol - %r\n", Status));
  2124. if (Status == EFI_SUCCESS) {
  2125. //
  2126. // Create event callback to install CC EventLog ACPI Table
  2127. EfiCreateProtocolNotifyEvent (&gEfiAcpiTableProtocolGuid, TPL_CALLBACK, InstallAcpiTable, NULL, &Registration);
  2128. } else {
  2129. //
  2130. // Cc measurement feature is crucial to a td-guest and it shall stop running immediately
  2131. // when it is failed to be installed.
  2132. DEBUG ((DEBUG_ERROR, "%a: CcMeasurement protocol failed to be installed - %r\n", __FUNCTION__, Status));
  2133. CpuDeadLoop ();
  2134. }
  2135. return Status;
  2136. }