TcgDxe.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. /** @file
  2. This module implements TCG EFI Protocol.
  3. Caution: This module requires additional review when modified.
  4. This driver will have external input - TcgDxePassThroughToTpm
  5. This external input must be validated carefully to avoid security issue like
  6. buffer overflow, integer overflow.
  7. TcgDxePassThroughToTpm() will receive untrusted input and do basic validation.
  8. Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
  9. (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
  10. SPDX-License-Identifier: BSD-2-Clause-Patent
  11. **/
  12. #include <PiDxe.h>
  13. #include <IndustryStandard/Tpm12.h>
  14. #include <IndustryStandard/Acpi.h>
  15. #include <IndustryStandard/PeImage.h>
  16. #include <IndustryStandard/TcpaAcpi.h>
  17. #include <Guid/GlobalVariable.h>
  18. #include <Guid/HobList.h>
  19. #include <Guid/TcgEventHob.h>
  20. #include <Guid/EventGroup.h>
  21. #include <Guid/EventExitBootServiceFailed.h>
  22. #include <Guid/TpmInstance.h>
  23. #include <Protocol/DevicePath.h>
  24. #include <Protocol/TcgService.h>
  25. #include <Protocol/AcpiTable.h>
  26. #include <Protocol/MpService.h>
  27. #include <Library/DebugLib.h>
  28. #include <Library/BaseMemoryLib.h>
  29. #include <Library/UefiRuntimeServicesTableLib.h>
  30. #include <Library/UefiDriverEntryPoint.h>
  31. #include <Library/HobLib.h>
  32. #include <Library/UefiBootServicesTableLib.h>
  33. #include <Library/BaseLib.h>
  34. #include <Library/MemoryAllocationLib.h>
  35. #include <Library/PrintLib.h>
  36. #include <Library/Tpm12DeviceLib.h>
  37. #include <Library/PcdLib.h>
  38. #include <Library/UefiLib.h>
  39. #include <Library/ReportStatusCodeLib.h>
  40. #include <Library/Tpm12CommandLib.h>
  41. #include <Library/BaseCryptLib.h>
  42. #define TCG_DXE_DATA_FROM_THIS(this) \
  43. BASE_CR (this, TCG_DXE_DATA, TcgProtocol)
  44. typedef struct _TCG_DXE_DATA {
  45. EFI_TCG_PROTOCOL TcgProtocol;
  46. TCG_EFI_BOOT_SERVICE_CAPABILITY BsCap;
  47. EFI_TCG_CLIENT_ACPI_TABLE *TcgClientAcpiTable;
  48. EFI_TCG_SERVER_ACPI_TABLE *TcgServerAcpiTable;
  49. UINTN EventLogSize;
  50. UINT8 *LastEvent;
  51. } TCG_DXE_DATA;
  52. EFI_TCG_CLIENT_ACPI_TABLE mTcgClientAcpiTemplate = {
  53. {
  54. EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE,
  55. sizeof (mTcgClientAcpiTemplate),
  56. 0x02 //Revision
  57. //
  58. // Compiler initializes the remaining bytes to 0
  59. // These fields should be filled in in production
  60. //
  61. },
  62. 0, // 0 for PC Client Platform Class
  63. 0, // Log Area Max Length
  64. (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1) // Log Area Start Address
  65. };
  66. //
  67. // The following EFI_TCG_SERVER_ACPI_TABLE default setting is just one example,
  68. // the TPM device connects to LPC, and also defined the ACPI _UID as 0xFF,
  69. // this _UID can be changed and should match with the _UID setting of the TPM
  70. // ACPI device object
  71. //
  72. EFI_TCG_SERVER_ACPI_TABLE mTcgServerAcpiTemplate = {
  73. {
  74. EFI_ACPI_3_0_TRUSTED_COMPUTING_PLATFORM_ALLIANCE_CAPABILITIES_TABLE_SIGNATURE,
  75. sizeof (mTcgServerAcpiTemplate),
  76. 0x02 //Revision
  77. //
  78. // Compiler initializes the remaining bytes to 0
  79. // These fields should be filled in in production
  80. //
  81. },
  82. 1, // 1 for Server Platform Class
  83. 0, // Reserved
  84. 0, // Log Area Max Length
  85. (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1), // Log Area Start Address
  86. 0x0120, // TCG Specification revision 1.2
  87. 0, // Device Flags
  88. 0, // Interrupt Flags
  89. 0, // GPE
  90. {0}, // Reserved 3 bytes
  91. 0, // Global System Interrupt
  92. {
  93. EFI_ACPI_3_0_SYSTEM_MEMORY,
  94. 0,
  95. 0,
  96. EFI_ACPI_3_0_BYTE,
  97. 0 // Base Address
  98. },
  99. 0, // Reserved
  100. {0}, // Configuration Address
  101. 0xFF, // ACPI _UID value of the device, can be changed for different platforms
  102. 0, // ACPI _UID value of the device, can be changed for different platforms
  103. 0, // ACPI _UID value of the device, can be changed for different platforms
  104. 0 // ACPI _UID value of the device, can be changed for different platforms
  105. };
  106. UINTN mBootAttempts = 0;
  107. CHAR16 mBootVarName[] = L"BootOrder";
  108. /**
  109. Get All processors EFI_CPU_LOCATION in system. LocationBuf is allocated inside the function
  110. Caller is responsible to free LocationBuf.
  111. @param[out] LocationBuf Returns Processor Location Buffer.
  112. @param[out] Num Returns processor number.
  113. @retval EFI_SUCCESS Operation completed successfully.
  114. @retval EFI_UNSUPPORTED MpService protocol not found.
  115. **/
  116. EFI_STATUS
  117. GetProcessorsCpuLocation (
  118. OUT EFI_CPU_PHYSICAL_LOCATION **LocationBuf,
  119. OUT UINTN *Num
  120. )
  121. {
  122. EFI_STATUS Status;
  123. EFI_MP_SERVICES_PROTOCOL *MpProtocol;
  124. UINTN ProcessorNum;
  125. UINTN EnabledProcessorNum;
  126. EFI_PROCESSOR_INFORMATION ProcessorInfo;
  127. EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;
  128. UINTN Index;
  129. Status = gBS->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **) &MpProtocol);
  130. if (EFI_ERROR (Status)) {
  131. //
  132. // MP protocol is not installed
  133. //
  134. return EFI_UNSUPPORTED;
  135. }
  136. Status = MpProtocol->GetNumberOfProcessors(
  137. MpProtocol,
  138. &ProcessorNum,
  139. &EnabledProcessorNum
  140. );
  141. if (EFI_ERROR(Status)){
  142. return Status;
  143. }
  144. Status = gBS->AllocatePool(
  145. EfiBootServicesData,
  146. sizeof(EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,
  147. (VOID **) &ProcessorLocBuf
  148. );
  149. if (EFI_ERROR(Status)){
  150. return Status;
  151. }
  152. //
  153. // Get each processor Location info
  154. //
  155. for (Index = 0; Index < ProcessorNum; Index++) {
  156. Status = MpProtocol->GetProcessorInfo(
  157. MpProtocol,
  158. Index,
  159. &ProcessorInfo
  160. );
  161. if (EFI_ERROR(Status)){
  162. FreePool(ProcessorLocBuf);
  163. return Status;
  164. }
  165. //
  166. // Get all Processor Location info & measure
  167. //
  168. CopyMem(
  169. &ProcessorLocBuf[Index],
  170. &ProcessorInfo.Location,
  171. sizeof(EFI_CPU_PHYSICAL_LOCATION)
  172. );
  173. }
  174. *LocationBuf = ProcessorLocBuf;
  175. *Num = ProcessorNum;
  176. return Status;
  177. }
  178. /**
  179. This service provides EFI protocol capability information, state information
  180. about the TPM, and Event Log state information.
  181. @param[in] This Indicates the calling context
  182. @param[out] ProtocolCapability The callee allocates memory for a TCG_BOOT_SERVICE_CAPABILITY
  183. structure and fills in the fields with the EFI protocol
  184. capability information and the current TPM state information.
  185. @param[out] TCGFeatureFlags This is a pointer to the feature flags. No feature
  186. flags are currently defined so this parameter
  187. MUST be set to 0. However, in the future,
  188. feature flags may be defined that, for example,
  189. enable hash algorithm agility.
  190. @param[out] EventLogLocation This is a pointer to the address of the event log in memory.
  191. @param[out] EventLogLastEntry If the Event Log contains more than one entry,
  192. this is a pointer to the address of the start of
  193. the last entry in the event log in memory.
  194. @retval EFI_SUCCESS Operation completed successfully.
  195. @retval EFI_INVALID_PARAMETER ProtocolCapability does not match TCG capability.
  196. **/
  197. EFI_STATUS
  198. EFIAPI
  199. TcgDxeStatusCheck (
  200. IN EFI_TCG_PROTOCOL *This,
  201. OUT TCG_EFI_BOOT_SERVICE_CAPABILITY *ProtocolCapability,
  202. OUT UINT32 *TCGFeatureFlags,
  203. OUT EFI_PHYSICAL_ADDRESS *EventLogLocation,
  204. OUT EFI_PHYSICAL_ADDRESS *EventLogLastEntry
  205. )
  206. {
  207. TCG_DXE_DATA *TcgData;
  208. TcgData = TCG_DXE_DATA_FROM_THIS (This);
  209. if (ProtocolCapability != NULL) {
  210. *ProtocolCapability = TcgData->BsCap;
  211. }
  212. if (TCGFeatureFlags != NULL) {
  213. *TCGFeatureFlags = 0;
  214. }
  215. if (EventLogLocation != NULL) {
  216. if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {
  217. *EventLogLocation = TcgData->TcgClientAcpiTable->Lasa;
  218. } else {
  219. *EventLogLocation = TcgData->TcgServerAcpiTable->Lasa;
  220. }
  221. }
  222. if (EventLogLastEntry != NULL) {
  223. if (TcgData->BsCap.TPMDeactivatedFlag || (!TcgData->BsCap.TPMPresentFlag)) {
  224. *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)0;
  225. } else {
  226. *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN)TcgData->LastEvent;
  227. }
  228. }
  229. return EFI_SUCCESS;
  230. }
  231. /**
  232. Single function calculates SHA1 digest value for all raw data. It
  233. combines Sha1Init(), Sha1Update() and Sha1Final().
  234. @param[in] Data Raw data to be digested.
  235. @param[in] DataLen Size of the raw data.
  236. @param[out] Digest Pointer to a buffer that stores the final digest.
  237. @retval EFI_SUCCESS Always successfully calculate the final digest.
  238. **/
  239. EFI_STATUS
  240. EFIAPI
  241. TpmCommHashAll (
  242. IN CONST UINT8 *Data,
  243. IN UINTN DataLen,
  244. OUT TPM_DIGEST *Digest
  245. )
  246. {
  247. VOID *Sha1Ctx;
  248. UINTN CtxSize;
  249. CtxSize = Sha1GetContextSize ();
  250. Sha1Ctx = AllocatePool (CtxSize);
  251. ASSERT (Sha1Ctx != NULL);
  252. Sha1Init (Sha1Ctx);
  253. Sha1Update (Sha1Ctx, Data, DataLen);
  254. Sha1Final (Sha1Ctx, (UINT8 *)Digest);
  255. FreePool (Sha1Ctx);
  256. return EFI_SUCCESS;
  257. }
  258. /**
  259. This service abstracts the capability to do a hash operation on a data buffer.
  260. @param[in] This Indicates the calling context
  261. @param[in] HashData Pointer to the data buffer to be hashed
  262. @param[in] HashDataLen Length of the data buffer to be hashed
  263. @param[in] AlgorithmId Identification of the Algorithm to use for the hashing operation
  264. @param[in, out] HashedDataLen Resultant length of the hashed data
  265. @param[in, out] HashedDataResult Resultant buffer of the hashed data
  266. @retval EFI_SUCCESS Operation completed successfully.
  267. @retval EFI_INVALID_PARAMETER HashDataLen is NULL.
  268. @retval EFI_INVALID_PARAMETER HashDataLenResult is NULL.
  269. @retval EFI_OUT_OF_RESOURCES Cannot allocate buffer of size *HashedDataLen.
  270. @retval EFI_UNSUPPORTED AlgorithmId not supported.
  271. @retval EFI_BUFFER_TOO_SMALL *HashedDataLen < sizeof (TCG_DIGEST).
  272. **/
  273. EFI_STATUS
  274. EFIAPI
  275. TcgDxeHashAll (
  276. IN EFI_TCG_PROTOCOL *This,
  277. IN UINT8 *HashData,
  278. IN UINT64 HashDataLen,
  279. IN TCG_ALGORITHM_ID AlgorithmId,
  280. IN OUT UINT64 *HashedDataLen,
  281. IN OUT UINT8 **HashedDataResult
  282. )
  283. {
  284. if (HashedDataLen == NULL || HashedDataResult == NULL) {
  285. return EFI_INVALID_PARAMETER;
  286. }
  287. switch (AlgorithmId) {
  288. case TPM_ALG_SHA:
  289. if (*HashedDataLen == 0) {
  290. *HashedDataLen = sizeof (TPM_DIGEST);
  291. *HashedDataResult = AllocatePool ((UINTN) *HashedDataLen);
  292. if (*HashedDataResult == NULL) {
  293. return EFI_OUT_OF_RESOURCES;
  294. }
  295. }
  296. if (*HashedDataLen < sizeof (TPM_DIGEST)) {
  297. *HashedDataLen = sizeof (TPM_DIGEST);
  298. return EFI_BUFFER_TOO_SMALL;
  299. }
  300. *HashedDataLen = sizeof (TPM_DIGEST);
  301. if (*HashedDataResult == NULL) {
  302. *HashedDataResult = AllocatePool ((UINTN) *HashedDataLen);
  303. }
  304. return TpmCommHashAll (
  305. HashData,
  306. (UINTN) HashDataLen,
  307. (TPM_DIGEST*)*HashedDataResult
  308. );
  309. default:
  310. return EFI_UNSUPPORTED;
  311. }
  312. }
  313. /**
  314. Add a new entry to the Event Log.
  315. @param[in, out] EventLogPtr Pointer to the Event Log data.
  316. @param[in, out] LogSize Size of the Event Log.
  317. @param[in] MaxSize Maximum size of the Event Log.
  318. @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
  319. @param[in] NewEventData Pointer to the new event data.
  320. @retval EFI_SUCCESS The new event log entry was added.
  321. @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
  322. **/
  323. EFI_STATUS
  324. TpmCommLogEvent (
  325. IN OUT UINT8 **EventLogPtr,
  326. IN OUT UINTN *LogSize,
  327. IN UINTN MaxSize,
  328. IN TCG_PCR_EVENT_HDR *NewEventHdr,
  329. IN UINT8 *NewEventData
  330. )
  331. {
  332. UINTN NewLogSize;
  333. //
  334. // Prevent Event Overflow
  335. //
  336. if ((UINTN) NewEventHdr->EventSize > MAX_UINTN - sizeof (*NewEventHdr)) {
  337. return EFI_OUT_OF_RESOURCES;
  338. }
  339. NewLogSize = sizeof (*NewEventHdr) + NewEventHdr->EventSize;
  340. if (NewLogSize > MaxSize - *LogSize) {
  341. return EFI_OUT_OF_RESOURCES;
  342. }
  343. *EventLogPtr += *LogSize;
  344. *LogSize += NewLogSize;
  345. CopyMem (*EventLogPtr, NewEventHdr, sizeof (*NewEventHdr));
  346. CopyMem (
  347. *EventLogPtr + sizeof (*NewEventHdr),
  348. NewEventData,
  349. NewEventHdr->EventSize
  350. );
  351. return EFI_SUCCESS;
  352. }
  353. /**
  354. Add a new entry to the Event Log.
  355. @param[in] TcgData TCG_DXE_DATA structure.
  356. @param[in] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
  357. @param[in] NewEventData Pointer to the new event data.
  358. @retval EFI_SUCCESS The new event log entry was added.
  359. @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
  360. **/
  361. EFI_STATUS
  362. EFIAPI
  363. TcgDxeLogEventI (
  364. IN TCG_DXE_DATA *TcgData,
  365. IN TCG_PCR_EVENT_HDR *NewEventHdr,
  366. IN UINT8 *NewEventData
  367. )
  368. {
  369. if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {
  370. TcgData->LastEvent = (UINT8*)(UINTN)TcgData->TcgClientAcpiTable->Lasa;
  371. return TpmCommLogEvent (
  372. &TcgData->LastEvent,
  373. &TcgData->EventLogSize,
  374. (UINTN)TcgData->TcgClientAcpiTable->Laml,
  375. NewEventHdr,
  376. NewEventData
  377. );
  378. } else {
  379. TcgData->LastEvent = (UINT8*)(UINTN)TcgData->TcgServerAcpiTable->Lasa;
  380. return TpmCommLogEvent (
  381. &TcgData->LastEvent,
  382. &TcgData->EventLogSize,
  383. (UINTN)TcgData->TcgServerAcpiTable->Laml,
  384. NewEventHdr,
  385. NewEventData
  386. );
  387. }
  388. }
  389. /**
  390. This service abstracts the capability to add an entry to the Event Log.
  391. @param[in] This Indicates the calling context
  392. @param[in] TCGLogData Pointer to the start of the data buffer containing
  393. the TCG_PCR_EVENT data structure. All fields in
  394. this structure are properly filled by the caller.
  395. @param[in, out] EventNumber The event number of the event just logged
  396. @param[in] Flags Indicate additional flags. Only one flag has been
  397. defined at this time, which is 0x01 and means the
  398. extend operation should not be performed. All
  399. other bits are reserved.
  400. @retval EFI_SUCCESS Operation completed successfully.
  401. @retval EFI_OUT_OF_RESOURCES Insufficient memory in the event log to complete this action.
  402. **/
  403. EFI_STATUS
  404. EFIAPI
  405. TcgDxeLogEvent (
  406. IN EFI_TCG_PROTOCOL *This,
  407. IN TCG_PCR_EVENT *TCGLogData,
  408. IN OUT UINT32 *EventNumber,
  409. IN UINT32 Flags
  410. )
  411. {
  412. TCG_DXE_DATA *TcgData;
  413. if (TCGLogData == NULL){
  414. return EFI_INVALID_PARAMETER;
  415. }
  416. TcgData = TCG_DXE_DATA_FROM_THIS (This);
  417. if (TcgData->BsCap.TPMDeactivatedFlag || (!TcgData->BsCap.TPMPresentFlag)) {
  418. return EFI_DEVICE_ERROR;
  419. }
  420. return TcgDxeLogEventI (
  421. TcgData,
  422. (TCG_PCR_EVENT_HDR*)TCGLogData,
  423. TCGLogData->Event
  424. );
  425. }
  426. /**
  427. This service is a proxy for commands to the TPM.
  428. @param[in] This Indicates the calling context
  429. @param[in] TpmInputParameterBlockSize Size of the TPM input parameter block
  430. @param[in] TpmInputParameterBlock Pointer to the TPM input parameter block
  431. @param[in] TpmOutputParameterBlockSize Size of the TPM output parameter block
  432. @param[in] TpmOutputParameterBlock Pointer to the TPM output parameter block
  433. @retval EFI_SUCCESS Operation completed successfully.
  434. @retval EFI_INVALID_PARAMETER Invalid ordinal.
  435. @retval EFI_UNSUPPORTED Current Task Priority Level >= EFI_TPL_CALLBACK.
  436. @retval EFI_TIMEOUT The TIS timed-out.
  437. **/
  438. EFI_STATUS
  439. EFIAPI
  440. TcgDxePassThroughToTpm (
  441. IN EFI_TCG_PROTOCOL *This,
  442. IN UINT32 TpmInputParameterBlockSize,
  443. IN UINT8 *TpmInputParameterBlock,
  444. IN UINT32 TpmOutputParameterBlockSize,
  445. IN UINT8 *TpmOutputParameterBlock
  446. )
  447. {
  448. if (TpmInputParameterBlock == NULL ||
  449. TpmOutputParameterBlock == NULL ||
  450. TpmInputParameterBlockSize == 0 ||
  451. TpmOutputParameterBlockSize == 0) {
  452. return EFI_INVALID_PARAMETER;
  453. }
  454. return Tpm12SubmitCommand (
  455. TpmInputParameterBlockSize,
  456. TpmInputParameterBlock,
  457. &TpmOutputParameterBlockSize,
  458. TpmOutputParameterBlock
  459. );
  460. }
  461. /**
  462. Do a hash operation on a data buffer, extend a specific TPM PCR with the hash result,
  463. and add an entry to the Event Log.
  464. @param[in] TcgData TCG_DXE_DATA structure.
  465. @param[in] HashData Physical address of the start of the data buffer
  466. to be hashed, extended, and logged.
  467. @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
  468. @param[in, out] NewEventHdr Pointer to a TCG_PCR_EVENT_HDR data structure.
  469. @param[in] NewEventData Pointer to the new event data.
  470. @retval EFI_SUCCESS Operation completed successfully.
  471. @retval EFI_OUT_OF_RESOURCES No enough memory to log the new event.
  472. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  473. **/
  474. EFI_STATUS
  475. EFIAPI
  476. TcgDxeHashLogExtendEventI (
  477. IN TCG_DXE_DATA *TcgData,
  478. IN UINT8 *HashData,
  479. IN UINT64 HashDataLen,
  480. IN OUT TCG_PCR_EVENT_HDR *NewEventHdr,
  481. IN UINT8 *NewEventData
  482. )
  483. {
  484. EFI_STATUS Status;
  485. if (!TcgData->BsCap.TPMPresentFlag) {
  486. return EFI_DEVICE_ERROR;
  487. }
  488. if (HashDataLen > 0 || HashData != NULL) {
  489. Status = TpmCommHashAll (
  490. HashData,
  491. (UINTN) HashDataLen,
  492. &NewEventHdr->Digest
  493. );
  494. if (EFI_ERROR(Status)) {
  495. DEBUG ((DEBUG_ERROR, "TpmCommHashAll Failed. %x\n", Status));
  496. goto Done;
  497. }
  498. }
  499. Status = Tpm12Extend (
  500. &NewEventHdr->Digest,
  501. NewEventHdr->PCRIndex,
  502. NULL
  503. );
  504. if (!EFI_ERROR (Status)) {
  505. Status = TcgDxeLogEventI (TcgData, NewEventHdr, NewEventData);
  506. }
  507. Done:
  508. if ((Status == EFI_DEVICE_ERROR) || (Status == EFI_TIMEOUT)) {
  509. DEBUG ((EFI_D_ERROR, "TcgDxeHashLogExtendEventI - %r. Disable TPM.\n", Status));
  510. TcgData->BsCap.TPMPresentFlag = FALSE;
  511. REPORT_STATUS_CODE (
  512. EFI_ERROR_CODE | EFI_ERROR_MINOR,
  513. (PcdGet32 (PcdStatusCodeSubClassTpmDevice) | EFI_P_EC_INTERFACE_ERROR)
  514. );
  515. Status = EFI_DEVICE_ERROR;
  516. }
  517. return Status;
  518. }
  519. /**
  520. This service abstracts the capability to do a hash operation on a data buffer,
  521. extend a specific TPM PCR with the hash result, and add an entry to the Event Log
  522. @param[in] This Indicates the calling context
  523. @param[in] HashData Physical address of the start of the data buffer
  524. to be hashed, extended, and logged.
  525. @param[in] HashDataLen The length, in bytes, of the buffer referenced by HashData
  526. @param[in] AlgorithmId Identification of the Algorithm to use for the hashing operation
  527. @param[in, out] TCGLogData The physical address of the start of the data
  528. buffer containing the TCG_PCR_EVENT data structure.
  529. @param[in, out] EventNumber The event number of the event just logged.
  530. @param[out] EventLogLastEntry Physical address of the first byte of the entry
  531. just placed in the Event Log. If the Event Log was
  532. empty when this function was called then this physical
  533. address will be the same as the physical address of
  534. the start of the Event Log.
  535. @retval EFI_SUCCESS Operation completed successfully.
  536. @retval EFI_UNSUPPORTED AlgorithmId != TPM_ALG_SHA.
  537. @retval EFI_UNSUPPORTED Current TPL >= EFI_TPL_CALLBACK.
  538. @retval EFI_DEVICE_ERROR The command was unsuccessful.
  539. **/
  540. EFI_STATUS
  541. EFIAPI
  542. TcgDxeHashLogExtendEvent (
  543. IN EFI_TCG_PROTOCOL *This,
  544. IN EFI_PHYSICAL_ADDRESS HashData,
  545. IN UINT64 HashDataLen,
  546. IN TPM_ALGORITHM_ID AlgorithmId,
  547. IN OUT TCG_PCR_EVENT *TCGLogData,
  548. IN OUT UINT32 *EventNumber,
  549. OUT EFI_PHYSICAL_ADDRESS *EventLogLastEntry
  550. )
  551. {
  552. TCG_DXE_DATA *TcgData;
  553. EFI_STATUS Status;
  554. if (TCGLogData == NULL || EventLogLastEntry == NULL){
  555. return EFI_INVALID_PARAMETER;
  556. }
  557. TcgData = TCG_DXE_DATA_FROM_THIS (This);
  558. if (TcgData->BsCap.TPMDeactivatedFlag || (!TcgData->BsCap.TPMPresentFlag)) {
  559. return EFI_DEVICE_ERROR;
  560. }
  561. if (AlgorithmId != TPM_ALG_SHA) {
  562. return EFI_UNSUPPORTED;
  563. }
  564. if (HashData == 0 && HashDataLen > 0) {
  565. return EFI_INVALID_PARAMETER;
  566. }
  567. Status = TcgDxeHashLogExtendEventI (
  568. TcgData,
  569. (UINT8 *) (UINTN) HashData,
  570. HashDataLen,
  571. (TCG_PCR_EVENT_HDR*)TCGLogData,
  572. TCGLogData->Event
  573. );
  574. if (!EFI_ERROR(Status)){
  575. *EventLogLastEntry = (EFI_PHYSICAL_ADDRESS)(UINTN) TcgData->LastEvent;
  576. }
  577. return Status;
  578. }
  579. TCG_DXE_DATA mTcgDxeData = {
  580. {
  581. TcgDxeStatusCheck,
  582. TcgDxeHashAll,
  583. TcgDxeLogEvent,
  584. TcgDxePassThroughToTpm,
  585. TcgDxeHashLogExtendEvent
  586. },
  587. {
  588. sizeof (mTcgDxeData.BsCap),
  589. { 1, 2, 0, 0 },
  590. { 1, 2, 0, 0 },
  591. 1,
  592. TRUE,
  593. FALSE
  594. },
  595. &mTcgClientAcpiTemplate,
  596. &mTcgServerAcpiTemplate,
  597. 0,
  598. NULL
  599. };
  600. /**
  601. Initialize the Event Log and log events passed from the PEI phase.
  602. @retval EFI_SUCCESS Operation completed successfully.
  603. @retval EFI_OUT_OF_RESOURCES Out of memory.
  604. **/
  605. EFI_STATUS
  606. EFIAPI
  607. SetupEventLog (
  608. VOID
  609. )
  610. {
  611. EFI_STATUS Status;
  612. TCG_PCR_EVENT *TcgEvent;
  613. EFI_PEI_HOB_POINTERS GuidHob;
  614. EFI_PHYSICAL_ADDRESS Lasa;
  615. if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {
  616. Lasa = mTcgClientAcpiTemplate.Lasa;
  617. Status = gBS->AllocatePages (
  618. AllocateMaxAddress,
  619. EfiACPIMemoryNVS,
  620. EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcgLogAreaMinLen)),
  621. &Lasa
  622. );
  623. if (EFI_ERROR (Status)) {
  624. return Status;
  625. }
  626. mTcgClientAcpiTemplate.Lasa = Lasa;
  627. //
  628. // To initialize them as 0xFF is recommended
  629. // because the OS can know the last entry for that.
  630. //
  631. SetMem ((VOID *)(UINTN)mTcgClientAcpiTemplate.Lasa, PcdGet32 (PcdTcgLogAreaMinLen), 0xFF);
  632. mTcgClientAcpiTemplate.Laml = PcdGet32 (PcdTcgLogAreaMinLen);
  633. } else {
  634. Lasa = mTcgServerAcpiTemplate.Lasa;
  635. Status = gBS->AllocatePages (
  636. AllocateMaxAddress,
  637. EfiACPIMemoryNVS,
  638. EFI_SIZE_TO_PAGES (PcdGet32 (PcdTcgLogAreaMinLen)),
  639. &Lasa
  640. );
  641. if (EFI_ERROR (Status)) {
  642. return Status;
  643. }
  644. mTcgServerAcpiTemplate.Lasa = Lasa;
  645. //
  646. // To initialize them as 0xFF is recommended
  647. // because the OS can know the last entry for that.
  648. //
  649. SetMem ((VOID *)(UINTN)mTcgServerAcpiTemplate.Lasa, PcdGet32 (PcdTcgLogAreaMinLen), 0xFF);
  650. mTcgServerAcpiTemplate.Laml = PcdGet32 (PcdTcgLogAreaMinLen);
  651. }
  652. GuidHob.Raw = GetHobList ();
  653. while (!EFI_ERROR (Status) &&
  654. (GuidHob.Raw = GetNextGuidHob (&gTcgEventEntryHobGuid, GuidHob.Raw)) != NULL) {
  655. TcgEvent = GET_GUID_HOB_DATA (GuidHob.Guid);
  656. GuidHob.Raw = GET_NEXT_HOB (GuidHob);
  657. Status = TcgDxeLogEventI (
  658. &mTcgDxeData,
  659. (TCG_PCR_EVENT_HDR*)TcgEvent,
  660. TcgEvent->Event
  661. );
  662. }
  663. return Status;
  664. }
  665. /**
  666. Measure and log an action string, and extend the measurement result into PCR[5].
  667. @param[in] String A specific string that indicates an Action event.
  668. @retval EFI_SUCCESS Operation completed successfully.
  669. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  670. **/
  671. EFI_STATUS
  672. EFIAPI
  673. TcgMeasureAction (
  674. IN CHAR8 *String
  675. )
  676. {
  677. TCG_PCR_EVENT_HDR TcgEvent;
  678. TcgEvent.PCRIndex = 5;
  679. TcgEvent.EventType = EV_EFI_ACTION;
  680. TcgEvent.EventSize = (UINT32)AsciiStrLen (String);
  681. return TcgDxeHashLogExtendEventI (
  682. &mTcgDxeData,
  683. (UINT8*)String,
  684. TcgEvent.EventSize,
  685. &TcgEvent,
  686. (UINT8 *) String
  687. );
  688. }
  689. /**
  690. Measure and log EFI handoff tables, and extend the measurement result into PCR[1].
  691. @retval EFI_SUCCESS Operation completed successfully.
  692. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  693. **/
  694. EFI_STATUS
  695. EFIAPI
  696. MeasureHandoffTables (
  697. VOID
  698. )
  699. {
  700. EFI_STATUS Status;
  701. TCG_PCR_EVENT_HDR TcgEvent;
  702. EFI_HANDOFF_TABLE_POINTERS HandoffTables;
  703. UINTN ProcessorNum;
  704. EFI_CPU_PHYSICAL_LOCATION *ProcessorLocBuf;
  705. ProcessorLocBuf = NULL;
  706. Status = EFI_SUCCESS;
  707. if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_SERVER) {
  708. //
  709. // Tcg Server spec.
  710. // Measure each processor EFI_CPU_PHYSICAL_LOCATION with EV_TABLE_OF_DEVICES to PCR[1]
  711. //
  712. Status = GetProcessorsCpuLocation(&ProcessorLocBuf, &ProcessorNum);
  713. if (!EFI_ERROR(Status)){
  714. TcgEvent.PCRIndex = 1;
  715. TcgEvent.EventType = EV_TABLE_OF_DEVICES;
  716. TcgEvent.EventSize = sizeof (HandoffTables);
  717. HandoffTables.NumberOfTables = 1;
  718. HandoffTables.TableEntry[0].VendorGuid = gEfiMpServiceProtocolGuid;
  719. HandoffTables.TableEntry[0].VendorTable = ProcessorLocBuf;
  720. Status = TcgDxeHashLogExtendEventI (
  721. &mTcgDxeData,
  722. (UINT8*)(UINTN)ProcessorLocBuf,
  723. sizeof(EFI_CPU_PHYSICAL_LOCATION) * ProcessorNum,
  724. &TcgEvent,
  725. (UINT8*)&HandoffTables
  726. );
  727. FreePool(ProcessorLocBuf);
  728. }
  729. }
  730. return Status;
  731. }
  732. /**
  733. Measure and log Separator event, and extend the measurement result into a specific PCR.
  734. @param[in] PCRIndex PCR index.
  735. @retval EFI_SUCCESS Operation completed successfully.
  736. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  737. **/
  738. EFI_STATUS
  739. EFIAPI
  740. MeasureSeparatorEvent (
  741. IN TPM_PCRINDEX PCRIndex
  742. )
  743. {
  744. TCG_PCR_EVENT_HDR TcgEvent;
  745. UINT32 EventData;
  746. EventData = 0;
  747. TcgEvent.PCRIndex = PCRIndex;
  748. TcgEvent.EventType = EV_SEPARATOR;
  749. TcgEvent.EventSize = (UINT32)sizeof (EventData);
  750. return TcgDxeHashLogExtendEventI (
  751. &mTcgDxeData,
  752. (UINT8 *)&EventData,
  753. sizeof (EventData),
  754. &TcgEvent,
  755. (UINT8 *)&EventData
  756. );
  757. }
  758. /**
  759. Read an EFI Variable.
  760. This function allocates a buffer to return the contents of the variable. The caller is
  761. responsible for freeing the buffer.
  762. @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
  763. @param[in] VendorGuid A unique identifier for the vendor.
  764. @param[out] VarSize The size of the variable data.
  765. @return A pointer to the buffer to return the contents of the variable.Otherwise NULL.
  766. **/
  767. VOID *
  768. EFIAPI
  769. ReadVariable (
  770. IN CHAR16 *VarName,
  771. IN EFI_GUID *VendorGuid,
  772. OUT UINTN *VarSize
  773. )
  774. {
  775. EFI_STATUS Status;
  776. VOID *VarData;
  777. *VarSize = 0;
  778. Status = gRT->GetVariable (
  779. VarName,
  780. VendorGuid,
  781. NULL,
  782. VarSize,
  783. NULL
  784. );
  785. if (Status != EFI_BUFFER_TOO_SMALL) {
  786. return NULL;
  787. }
  788. VarData = AllocatePool (*VarSize);
  789. if (VarData != NULL) {
  790. Status = gRT->GetVariable (
  791. VarName,
  792. VendorGuid,
  793. NULL,
  794. VarSize,
  795. VarData
  796. );
  797. if (EFI_ERROR (Status)) {
  798. FreePool (VarData);
  799. VarData = NULL;
  800. *VarSize = 0;
  801. }
  802. }
  803. return VarData;
  804. }
  805. /**
  806. Measure and log an EFI variable, and extend the measurement result into a specific PCR.
  807. @param[in] PCRIndex PCR Index.
  808. @param[in] EventType Event type.
  809. @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
  810. @param[in] VendorGuid A unique identifier for the vendor.
  811. @param[in] VarData The content of the variable data.
  812. @param[in] VarSize The size of the variable data.
  813. @retval EFI_SUCCESS Operation completed successfully.
  814. @retval EFI_OUT_OF_RESOURCES Out of memory.
  815. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  816. **/
  817. EFI_STATUS
  818. EFIAPI
  819. MeasureVariable (
  820. IN TPM_PCRINDEX PCRIndex,
  821. IN TCG_EVENTTYPE EventType,
  822. IN CHAR16 *VarName,
  823. IN EFI_GUID *VendorGuid,
  824. IN VOID *VarData,
  825. IN UINTN VarSize
  826. )
  827. {
  828. EFI_STATUS Status;
  829. TCG_PCR_EVENT_HDR TcgEvent;
  830. UINTN VarNameLength;
  831. EFI_VARIABLE_DATA *VarLog;
  832. VarNameLength = StrLen (VarName);
  833. TcgEvent.PCRIndex = PCRIndex;
  834. TcgEvent.EventType = EventType;
  835. TcgEvent.EventSize = (UINT32)(sizeof (*VarLog) + VarNameLength * sizeof (*VarName) + VarSize
  836. - sizeof (VarLog->UnicodeName) - sizeof (VarLog->VariableData));
  837. VarLog = (EFI_VARIABLE_DATA*)AllocatePool (TcgEvent.EventSize);
  838. if (VarLog == NULL) {
  839. return EFI_OUT_OF_RESOURCES;
  840. }
  841. VarLog->VariableName = *VendorGuid;
  842. VarLog->UnicodeNameLength = VarNameLength;
  843. VarLog->VariableDataLength = VarSize;
  844. CopyMem (
  845. VarLog->UnicodeName,
  846. VarName,
  847. VarNameLength * sizeof (*VarName)
  848. );
  849. CopyMem (
  850. (CHAR16 *)VarLog->UnicodeName + VarNameLength,
  851. VarData,
  852. VarSize
  853. );
  854. Status = TcgDxeHashLogExtendEventI (
  855. &mTcgDxeData,
  856. (UINT8*)VarLog,
  857. TcgEvent.EventSize,
  858. &TcgEvent,
  859. (UINT8*)VarLog
  860. );
  861. FreePool (VarLog);
  862. return Status;
  863. }
  864. /**
  865. Read then Measure and log an EFI boot variable, and extend the measurement result into PCR[5].
  866. @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
  867. @param[in] VendorGuid A unique identifier for the vendor.
  868. @param[out] VarSize The size of the variable data.
  869. @param[out] VarData Pointer to the content of the variable.
  870. @retval EFI_SUCCESS Operation completed successfully.
  871. @retval EFI_OUT_OF_RESOURCES Out of memory.
  872. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  873. **/
  874. EFI_STATUS
  875. EFIAPI
  876. ReadAndMeasureBootVariable (
  877. IN CHAR16 *VarName,
  878. IN EFI_GUID *VendorGuid,
  879. OUT UINTN *VarSize,
  880. OUT VOID **VarData
  881. )
  882. {
  883. EFI_STATUS Status;
  884. *VarData = ReadVariable (VarName, VendorGuid, VarSize);
  885. if (*VarData == NULL) {
  886. return EFI_NOT_FOUND;
  887. }
  888. Status = MeasureVariable (
  889. 5,
  890. EV_EFI_VARIABLE_BOOT,
  891. VarName,
  892. VendorGuid,
  893. *VarData,
  894. *VarSize
  895. );
  896. return Status;
  897. }
  898. /**
  899. Measure and log all EFI boot variables, and extend the measurement result into a specific PCR.
  900. The EFI boot variables are BootOrder and Boot#### variables.
  901. @retval EFI_SUCCESS Operation completed successfully.
  902. @retval EFI_OUT_OF_RESOURCES Out of memory.
  903. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  904. **/
  905. EFI_STATUS
  906. EFIAPI
  907. MeasureAllBootVariables (
  908. VOID
  909. )
  910. {
  911. EFI_STATUS Status;
  912. UINT16 *BootOrder;
  913. UINTN BootCount;
  914. UINTN Index;
  915. VOID *BootVarData;
  916. UINTN Size;
  917. Status = ReadAndMeasureBootVariable (
  918. mBootVarName,
  919. &gEfiGlobalVariableGuid,
  920. &BootCount,
  921. (VOID **) &BootOrder
  922. );
  923. if (Status == EFI_NOT_FOUND || BootOrder == NULL) {
  924. return EFI_SUCCESS;
  925. }
  926. if (EFI_ERROR (Status)) {
  927. //
  928. // BootOrder can't be NULL if status is not EFI_NOT_FOUND
  929. //
  930. FreePool (BootOrder);
  931. return Status;
  932. }
  933. BootCount /= sizeof (*BootOrder);
  934. for (Index = 0; Index < BootCount; Index++) {
  935. UnicodeSPrint (mBootVarName, sizeof (mBootVarName), L"Boot%04x", BootOrder[Index]);
  936. Status = ReadAndMeasureBootVariable (
  937. mBootVarName,
  938. &gEfiGlobalVariableGuid,
  939. &Size,
  940. &BootVarData
  941. );
  942. if (!EFI_ERROR (Status)) {
  943. FreePool (BootVarData);
  944. }
  945. }
  946. FreePool (BootOrder);
  947. return EFI_SUCCESS;
  948. }
  949. /**
  950. Ready to Boot Event notification handler.
  951. Sequence of OS boot events is measured in this event notification handler.
  952. @param[in] Event Event whose notification function is being invoked
  953. @param[in] Context Pointer to the notification function's context
  954. **/
  955. VOID
  956. EFIAPI
  957. OnReadyToBoot (
  958. IN EFI_EVENT Event,
  959. IN VOID *Context
  960. )
  961. {
  962. EFI_STATUS Status;
  963. TPM_PCRINDEX PcrIndex;
  964. if (mBootAttempts == 0) {
  965. //
  966. // Measure handoff tables.
  967. //
  968. Status = MeasureHandoffTables ();
  969. if (EFI_ERROR (Status)) {
  970. DEBUG ((EFI_D_ERROR, "HOBs not Measured. Error!\n"));
  971. }
  972. //
  973. // Measure BootOrder & Boot#### variables.
  974. //
  975. Status = MeasureAllBootVariables ();
  976. if (EFI_ERROR (Status)) {
  977. DEBUG ((EFI_D_ERROR, "Boot Variables not Measured. Error!\n"));
  978. }
  979. //
  980. // 1. This is the first boot attempt.
  981. //
  982. Status = TcgMeasureAction (
  983. EFI_CALLING_EFI_APPLICATION
  984. );
  985. if (EFI_ERROR (Status)) {
  986. DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_CALLING_EFI_APPLICATION));
  987. }
  988. //
  989. // 2. Draw a line between pre-boot env and entering post-boot env.
  990. //
  991. for (PcrIndex = 0; PcrIndex < 8; PcrIndex++) {
  992. Status = MeasureSeparatorEvent (PcrIndex);
  993. if (EFI_ERROR (Status)) {
  994. DEBUG ((DEBUG_ERROR, "Separator Event not Measured. Error!\n"));
  995. }
  996. }
  997. //
  998. // 3. Measure GPT. It would be done in SAP driver.
  999. //
  1000. //
  1001. // 4. Measure PE/COFF OS loader. It would be done in SAP driver.
  1002. //
  1003. //
  1004. // 5. Read & Measure variable. BootOrder already measured.
  1005. //
  1006. } else {
  1007. //
  1008. // 6. Not first attempt, meaning a return from last attempt
  1009. //
  1010. Status = TcgMeasureAction (
  1011. EFI_RETURNING_FROM_EFI_APPLICATION
  1012. );
  1013. if (EFI_ERROR (Status)) {
  1014. DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_RETURNING_FROM_EFI_APPLICATION));
  1015. }
  1016. }
  1017. DEBUG ((EFI_D_INFO, "TPM TcgDxe Measure Data when ReadyToBoot\n"));
  1018. //
  1019. // Increase boot attempt counter.
  1020. //
  1021. mBootAttempts++;
  1022. }
  1023. /**
  1024. Install TCG ACPI Table when ACPI Table Protocol is available.
  1025. A system's firmware uses an ACPI table to identify the system's TCG capabilities
  1026. to the Post-Boot environment. The information in this ACPI table is not guaranteed
  1027. to be valid until the Host Platform transitions from pre-boot state to post-boot state.
  1028. @param[in] Event Event whose notification function is being invoked
  1029. @param[in] Context Pointer to the notification function's context
  1030. **/
  1031. VOID
  1032. EFIAPI
  1033. InstallAcpiTable (
  1034. IN EFI_EVENT Event,
  1035. IN VOID* Context
  1036. )
  1037. {
  1038. UINTN TableKey;
  1039. EFI_STATUS Status;
  1040. EFI_ACPI_TABLE_PROTOCOL *AcpiTable;
  1041. UINT8 Checksum;
  1042. UINT64 OemTableId;
  1043. Status = gBS->LocateProtocol (&gEfiAcpiTableProtocolGuid, NULL, (VOID **)&AcpiTable);
  1044. if (EFI_ERROR (Status)) {
  1045. return;
  1046. }
  1047. if (PcdGet8 (PcdTpmPlatformClass) == TCG_PLATFORM_TYPE_CLIENT) {
  1048. CopyMem (mTcgClientAcpiTemplate.Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (mTcgClientAcpiTemplate.Header.OemId));
  1049. OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);
  1050. CopyMem (&mTcgClientAcpiTemplate.Header.OemTableId, &OemTableId, sizeof (UINT64));
  1051. mTcgClientAcpiTemplate.Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);
  1052. mTcgClientAcpiTemplate.Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);
  1053. mTcgClientAcpiTemplate.Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
  1054. //
  1055. // The ACPI table must be checksummed before calling the InstallAcpiTable()
  1056. // service of the ACPI table protocol to install it.
  1057. //
  1058. Checksum = CalculateCheckSum8 ((UINT8 *)&mTcgClientAcpiTemplate, sizeof (mTcgClientAcpiTemplate));
  1059. mTcgClientAcpiTemplate.Header.Checksum = Checksum;
  1060. Status = AcpiTable->InstallAcpiTable (
  1061. AcpiTable,
  1062. &mTcgClientAcpiTemplate,
  1063. sizeof (mTcgClientAcpiTemplate),
  1064. &TableKey
  1065. );
  1066. } else {
  1067. CopyMem (mTcgServerAcpiTemplate.Header.OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (mTcgServerAcpiTemplate.Header.OemId));
  1068. OemTableId = PcdGet64 (PcdAcpiDefaultOemTableId);
  1069. CopyMem (&mTcgServerAcpiTemplate.Header.OemTableId, &OemTableId, sizeof (UINT64));
  1070. mTcgServerAcpiTemplate.Header.OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);
  1071. mTcgServerAcpiTemplate.Header.CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);
  1072. mTcgServerAcpiTemplate.Header.CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
  1073. //
  1074. // The ACPI table must be checksummed before calling the InstallAcpiTable()
  1075. // service of the ACPI table protocol to install it.
  1076. //
  1077. Checksum = CalculateCheckSum8 ((UINT8 *)&mTcgServerAcpiTemplate, sizeof (mTcgServerAcpiTemplate));
  1078. mTcgServerAcpiTemplate.Header.Checksum = Checksum;
  1079. mTcgServerAcpiTemplate.BaseAddress.Address = PcdGet64 (PcdTpmBaseAddress);
  1080. Status = AcpiTable->InstallAcpiTable (
  1081. AcpiTable,
  1082. &mTcgServerAcpiTemplate,
  1083. sizeof (mTcgServerAcpiTemplate),
  1084. &TableKey
  1085. );
  1086. }
  1087. if (EFI_ERROR (Status)) {
  1088. DEBUG((EFI_D_ERROR, "Tcg Acpi Table installation failure"));
  1089. }
  1090. }
  1091. /**
  1092. Exit Boot Services Event notification handler.
  1093. Measure invocation and success of ExitBootServices.
  1094. @param[in] Event Event whose notification function is being invoked
  1095. @param[in] Context Pointer to the notification function's context
  1096. **/
  1097. VOID
  1098. EFIAPI
  1099. OnExitBootServices (
  1100. IN EFI_EVENT Event,
  1101. IN VOID *Context
  1102. )
  1103. {
  1104. EFI_STATUS Status;
  1105. //
  1106. // Measure invocation of ExitBootServices,
  1107. //
  1108. Status = TcgMeasureAction (
  1109. EFI_EXIT_BOOT_SERVICES_INVOCATION
  1110. );
  1111. if (EFI_ERROR (Status)) {
  1112. DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_INVOCATION));
  1113. }
  1114. //
  1115. // Measure success of ExitBootServices
  1116. //
  1117. Status = TcgMeasureAction (
  1118. EFI_EXIT_BOOT_SERVICES_SUCCEEDED
  1119. );
  1120. if (EFI_ERROR (Status)){
  1121. DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_SUCCEEDED));
  1122. }
  1123. }
  1124. /**
  1125. Exit Boot Services Failed Event notification handler.
  1126. Measure Failure of ExitBootServices.
  1127. @param[in] Event Event whose notification function is being invoked
  1128. @param[in] Context Pointer to the notification function's context
  1129. **/
  1130. VOID
  1131. EFIAPI
  1132. OnExitBootServicesFailed (
  1133. IN EFI_EVENT Event,
  1134. IN VOID *Context
  1135. )
  1136. {
  1137. EFI_STATUS Status;
  1138. //
  1139. // Measure Failure of ExitBootServices,
  1140. //
  1141. Status = TcgMeasureAction (
  1142. EFI_EXIT_BOOT_SERVICES_FAILED
  1143. );
  1144. if (EFI_ERROR (Status)){
  1145. DEBUG ((EFI_D_ERROR, "%a not Measured. Error!\n", EFI_EXIT_BOOT_SERVICES_FAILED));
  1146. }
  1147. }
  1148. /**
  1149. Get TPM Deactivated state.
  1150. @param[out] TPMDeactivatedFlag Returns TPM Deactivated state.
  1151. @retval EFI_SUCCESS Operation completed successfully.
  1152. @retval EFI_DEVICE_ERROR The operation was unsuccessful.
  1153. **/
  1154. EFI_STATUS
  1155. GetTpmStatus (
  1156. OUT BOOLEAN *TPMDeactivatedFlag
  1157. )
  1158. {
  1159. EFI_STATUS Status;
  1160. TPM_STCLEAR_FLAGS VolatileFlags;
  1161. Status = Tpm12GetCapabilityFlagVolatile (&VolatileFlags);
  1162. if (!EFI_ERROR (Status)) {
  1163. *TPMDeactivatedFlag = VolatileFlags.deactivated;
  1164. }
  1165. return Status;
  1166. }
  1167. /**
  1168. The driver's entry point.
  1169. It publishes EFI TCG Protocol.
  1170. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  1171. @param[in] SystemTable A pointer to the EFI System Table.
  1172. @retval EFI_SUCCESS The entry point is executed successfully.
  1173. @retval other Some error occurs when executing this entry point.
  1174. **/
  1175. EFI_STATUS
  1176. EFIAPI
  1177. DriverEntry (
  1178. IN EFI_HANDLE ImageHandle,
  1179. IN EFI_SYSTEM_TABLE *SystemTable
  1180. )
  1181. {
  1182. EFI_STATUS Status;
  1183. EFI_EVENT Event;
  1184. VOID *Registration;
  1185. if (!CompareGuid (PcdGetPtr(PcdTpmInstanceGuid), &gEfiTpmDeviceInstanceTpm12Guid)){
  1186. DEBUG ((EFI_D_ERROR, "No TPM12 instance required!\n"));
  1187. return EFI_UNSUPPORTED;
  1188. }
  1189. if (GetFirstGuidHob (&gTpmErrorHobGuid) != NULL) {
  1190. DEBUG ((EFI_D_ERROR, "TPM error!\n"));
  1191. return EFI_DEVICE_ERROR;
  1192. }
  1193. Status = Tpm12RequestUseTpm ();
  1194. if (EFI_ERROR (Status)) {
  1195. DEBUG ((EFI_D_ERROR, "TPM not detected!\n"));
  1196. return Status;
  1197. }
  1198. Status = GetTpmStatus (&mTcgDxeData.BsCap.TPMDeactivatedFlag);
  1199. if (EFI_ERROR (Status)) {
  1200. DEBUG ((
  1201. EFI_D_ERROR,
  1202. "DriverEntry: TPM not working properly\n"
  1203. ));
  1204. return Status;
  1205. }
  1206. Status = gBS->InstallProtocolInterface (
  1207. &ImageHandle,
  1208. &gEfiTcgProtocolGuid,
  1209. EFI_NATIVE_INTERFACE,
  1210. &mTcgDxeData.TcgProtocol
  1211. );
  1212. if (!EFI_ERROR (Status) && (!mTcgDxeData.BsCap.TPMDeactivatedFlag) && mTcgDxeData.BsCap.TPMPresentFlag) {
  1213. //
  1214. // Setup the log area and copy event log from hob list to it
  1215. //
  1216. Status = SetupEventLog ();
  1217. ASSERT_EFI_ERROR (Status);
  1218. //
  1219. // Measure handoff tables, Boot#### variables etc.
  1220. //
  1221. Status = EfiCreateEventReadyToBootEx (
  1222. TPL_CALLBACK,
  1223. OnReadyToBoot,
  1224. NULL,
  1225. &Event
  1226. );
  1227. Status = gBS->CreateEventEx (
  1228. EVT_NOTIFY_SIGNAL,
  1229. TPL_NOTIFY,
  1230. OnExitBootServices,
  1231. NULL,
  1232. &gEfiEventExitBootServicesGuid,
  1233. &Event
  1234. );
  1235. //
  1236. // Measure Exit Boot Service failed
  1237. //
  1238. Status = gBS->CreateEventEx (
  1239. EVT_NOTIFY_SIGNAL,
  1240. TPL_NOTIFY,
  1241. OnExitBootServicesFailed,
  1242. NULL,
  1243. &gEventExitBootServicesFailedGuid,
  1244. &Event
  1245. );
  1246. }
  1247. //
  1248. // Install ACPI Table
  1249. //
  1250. EfiCreateProtocolNotifyEvent (&gEfiAcpiTableProtocolGuid, TPL_CALLBACK, InstallAcpiTable, NULL, &Registration);
  1251. return Status;
  1252. }