efi_tcg2.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Defines APIs that allow an OS to interact with UEFI firmware to query
  4. * information about the device.
  5. * https://trustedcomputinggroup.org/resource/tcg-efi-protocol-specification/
  6. *
  7. * Copyright (c) 2020, Linaro Limited
  8. */
  9. #define LOG_CATEGORY LOGC_EFI
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <efi_loader.h>
  13. #include <efi_tcg2.h>
  14. #include <log.h>
  15. #include <malloc.h>
  16. #include <version.h>
  17. #include <tpm-v2.h>
  18. #include <u-boot/hash-checksum.h>
  19. #include <u-boot/sha1.h>
  20. #include <u-boot/sha256.h>
  21. #include <u-boot/sha512.h>
  22. #include <linux/unaligned/access_ok.h>
  23. #include <linux/unaligned/generic.h>
  24. #include <hexdump.h>
  25. struct event_log_buffer {
  26. void *buffer;
  27. void *final_buffer;
  28. size_t pos; /* eventlog position */
  29. size_t final_pos; /* final events config table position */
  30. size_t last_event_size;
  31. bool get_event_called;
  32. bool truncated;
  33. };
  34. static struct event_log_buffer event_log;
  35. static bool tcg2_efi_app_invoked;
  36. /*
  37. * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset.
  38. * Since the current tpm2_get_capability() response buffers starts at
  39. * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate
  40. * the response size and offset once for all consumers
  41. */
  42. #define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \
  43. offsetof(struct tpms_capability_data, data))
  44. #define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \
  45. offsetof(struct tpms_tagged_property, value))
  46. static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID;
  47. static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID;
  48. struct digest_info {
  49. u16 hash_alg;
  50. u32 hash_mask;
  51. u16 hash_len;
  52. };
  53. static const struct digest_info hash_algo_list[] = {
  54. {
  55. TPM2_ALG_SHA1,
  56. EFI_TCG2_BOOT_HASH_ALG_SHA1,
  57. TPM2_SHA1_DIGEST_SIZE,
  58. },
  59. {
  60. TPM2_ALG_SHA256,
  61. EFI_TCG2_BOOT_HASH_ALG_SHA256,
  62. TPM2_SHA256_DIGEST_SIZE,
  63. },
  64. {
  65. TPM2_ALG_SHA384,
  66. EFI_TCG2_BOOT_HASH_ALG_SHA384,
  67. TPM2_SHA384_DIGEST_SIZE,
  68. },
  69. {
  70. TPM2_ALG_SHA512,
  71. EFI_TCG2_BOOT_HASH_ALG_SHA512,
  72. TPM2_SHA512_DIGEST_SIZE,
  73. },
  74. };
  75. struct variable_info {
  76. u16 *name;
  77. const efi_guid_t *guid;
  78. };
  79. static struct variable_info secure_variables[] = {
  80. {L"SecureBoot", &efi_global_variable_guid},
  81. {L"PK", &efi_global_variable_guid},
  82. {L"KEK", &efi_global_variable_guid},
  83. {L"db", &efi_guid_image_security_database},
  84. {L"dbx", &efi_guid_image_security_database},
  85. };
  86. #define MAX_HASH_COUNT ARRAY_SIZE(hash_algo_list)
  87. /**
  88. * alg_to_mask - Get a TCG hash mask for algorithms
  89. *
  90. * @hash_alg: TCG defined algorithm
  91. *
  92. * @Return: TCG hashing algorithm bitmaps, 0 if the algorithm is not supported
  93. */
  94. static u32 alg_to_mask(u16 hash_alg)
  95. {
  96. size_t i;
  97. for (i = 0; i < MAX_HASH_COUNT; i++) {
  98. if (hash_algo_list[i].hash_alg == hash_alg)
  99. return hash_algo_list[i].hash_mask;
  100. }
  101. return 0;
  102. }
  103. /**
  104. * alg_to_len - Get a TCG hash len for algorithms
  105. *
  106. * @hash_alg: TCG defined algorithm
  107. *
  108. * @Return: len of chosen algorithm, 0 if the algorithm is not supported
  109. */
  110. static u16 alg_to_len(u16 hash_alg)
  111. {
  112. size_t i;
  113. for (i = 0; i < MAX_HASH_COUNT; i++) {
  114. if (hash_algo_list[i].hash_alg == hash_alg)
  115. return hash_algo_list[i].hash_len;
  116. }
  117. return 0;
  118. }
  119. static u32 tcg_event_final_size(struct tpml_digest_values *digest_list)
  120. {
  121. u32 len;
  122. size_t i;
  123. len = offsetof(struct tcg_pcr_event2, digests);
  124. len += offsetof(struct tpml_digest_values, digests);
  125. for (i = 0; i < digest_list->count; i++) {
  126. u16 hash_alg = digest_list->digests[i].hash_alg;
  127. len += offsetof(struct tpmt_ha, digest);
  128. len += alg_to_len(hash_alg);
  129. }
  130. len += sizeof(u32); /* tcg_pcr_event2 event_size*/
  131. return len;
  132. }
  133. /* tcg2_pcr_extend - Extend PCRs for a TPM2 device for a given tpml_digest_values
  134. *
  135. * @dev: device
  136. * @digest_list: list of digest algorithms to extend
  137. *
  138. * @Return: status code
  139. */
  140. static efi_status_t tcg2_pcr_extend(struct udevice *dev, u32 pcr_index,
  141. struct tpml_digest_values *digest_list)
  142. {
  143. u32 rc;
  144. size_t i;
  145. for (i = 0; i < digest_list->count; i++) {
  146. u32 alg = digest_list->digests[i].hash_alg;
  147. rc = tpm2_pcr_extend(dev, pcr_index, alg,
  148. (u8 *)&digest_list->digests[i].digest,
  149. alg_to_len(alg));
  150. if (rc) {
  151. EFI_PRINT("Failed to extend PCR\n");
  152. return EFI_DEVICE_ERROR;
  153. }
  154. }
  155. return EFI_SUCCESS;
  156. }
  157. /* tcg2_agile_log_append - Append an agile event to out eventlog
  158. *
  159. * @pcr_index: PCR index
  160. * @event_type: type of event added
  161. * @digest_list: list of digest algorithms to add
  162. * @size: size of event
  163. * @event: event to add
  164. *
  165. * @Return: status code
  166. */
  167. static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type,
  168. struct tpml_digest_values *digest_list,
  169. u32 size, u8 event[])
  170. {
  171. void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos);
  172. size_t pos;
  173. size_t i;
  174. u32 event_size;
  175. if (event_log.get_event_called)
  176. log = (void *)((uintptr_t)event_log.final_buffer +
  177. event_log.final_pos);
  178. /*
  179. * size refers to the length of event[] only, we need to check against
  180. * the final tcg_pcr_event2 size
  181. */
  182. event_size = size + tcg_event_final_size(digest_list);
  183. if (event_log.pos + event_size > TPM2_EVENT_LOG_SIZE ||
  184. event_log.final_pos + event_size > TPM2_EVENT_LOG_SIZE) {
  185. event_log.truncated = true;
  186. return EFI_VOLUME_FULL;
  187. }
  188. put_unaligned_le32(pcr_index, log);
  189. pos = offsetof(struct tcg_pcr_event2, event_type);
  190. put_unaligned_le32(event_type, (void *)((uintptr_t)log + pos));
  191. pos = offsetof(struct tcg_pcr_event2, digests); /* count */
  192. put_unaligned_le32(digest_list->count, (void *)((uintptr_t)log + pos));
  193. pos += offsetof(struct tpml_digest_values, digests);
  194. for (i = 0; i < digest_list->count; i++) {
  195. u16 hash_alg = digest_list->digests[i].hash_alg;
  196. u8 *digest = (u8 *)&digest_list->digests[i].digest;
  197. put_unaligned_le16(hash_alg, (void *)((uintptr_t)log + pos));
  198. pos += offsetof(struct tpmt_ha, digest);
  199. memcpy((void *)((uintptr_t)log + pos), digest, alg_to_len(hash_alg));
  200. pos += alg_to_len(hash_alg);
  201. }
  202. put_unaligned_le32(size, (void *)((uintptr_t)log + pos));
  203. pos += sizeof(u32); /* tcg_pcr_event2 event_size*/
  204. memcpy((void *)((uintptr_t)log + pos), event, size);
  205. pos += size;
  206. /* make sure the calculated buffer is what we checked against */
  207. if (pos != event_size)
  208. return EFI_INVALID_PARAMETER;
  209. /* if GetEventLog hasn't been called update the normal log */
  210. if (!event_log.get_event_called) {
  211. event_log.pos += pos;
  212. event_log.last_event_size = pos;
  213. } else {
  214. /* if GetEventLog has been called update config table log */
  215. struct efi_tcg2_final_events_table *final_event;
  216. final_event =
  217. (struct efi_tcg2_final_events_table *)(event_log.final_buffer);
  218. final_event->number_of_events++;
  219. event_log.final_pos += pos;
  220. }
  221. return EFI_SUCCESS;
  222. }
  223. /**
  224. * platform_get_tpm_device() - retrieve TPM device
  225. *
  226. * This function retrieves the udevice implementing a TPM
  227. *
  228. * This function may be overridden if special initialization is needed.
  229. *
  230. * @dev: udevice
  231. * Return: status code
  232. */
  233. __weak efi_status_t platform_get_tpm2_device(struct udevice **dev)
  234. {
  235. for_each_tpm_device(*dev) {
  236. /* Only support TPMv2 devices */
  237. if (tpm_get_version(*dev) == TPM_V2)
  238. return EFI_SUCCESS;
  239. }
  240. return EFI_NOT_FOUND;
  241. }
  242. /**
  243. * tpm2_get_max_command_size() - get the supported max command size
  244. *
  245. * @dev: TPM device
  246. * @max_command_size: output buffer for the size
  247. *
  248. * Return: 0 on success, -1 on error
  249. */
  250. static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size)
  251. {
  252. u8 response[TPM2_RESPONSE_BUFFER_SIZE];
  253. u32 ret;
  254. memset(response, 0, sizeof(response));
  255. ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
  256. TPM2_PT_MAX_COMMAND_SIZE, response, 1);
  257. if (ret)
  258. return -1;
  259. *max_command_size = (uint16_t)get_unaligned_be32(response +
  260. properties_offset);
  261. return 0;
  262. }
  263. /**
  264. * tpm2_get_max_response_size() - get the supported max response size
  265. *
  266. * @dev: TPM device
  267. * @max_response_size: output buffer for the size
  268. *
  269. * Return: 0 on success, -1 on error
  270. */
  271. static int tpm2_get_max_response_size(struct udevice *dev,
  272. u16 *max_response_size)
  273. {
  274. u8 response[TPM2_RESPONSE_BUFFER_SIZE];
  275. u32 ret;
  276. memset(response, 0, sizeof(response));
  277. ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
  278. TPM2_PT_MAX_RESPONSE_SIZE, response, 1);
  279. if (ret)
  280. return -1;
  281. *max_response_size = (uint16_t)get_unaligned_be32(response +
  282. properties_offset);
  283. return 0;
  284. }
  285. /**
  286. * tpm2_get_manufacturer_id() - get the manufacturer ID
  287. *
  288. * @dev: TPM device
  289. * @manufacturer_id: output buffer for the id
  290. *
  291. * Return: 0 on success, -1 on error
  292. */
  293. static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id)
  294. {
  295. u8 response[TPM2_RESPONSE_BUFFER_SIZE];
  296. u32 ret;
  297. memset(response, 0, sizeof(response));
  298. ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
  299. TPM2_PT_MANUFACTURER, response, 1);
  300. if (ret)
  301. return -1;
  302. *manufacturer_id = get_unaligned_be32(response + properties_offset);
  303. return 0;
  304. }
  305. /**
  306. * tpm2_get_num_pcr() - get the number of PCRs
  307. *
  308. * @dev: TPM device
  309. * @manufacturer_id: output buffer for the number
  310. *
  311. * Return: 0 on success, -1 on error
  312. */
  313. static int tpm2_get_num_pcr(struct udevice *dev, u32 *num_pcr)
  314. {
  315. u8 response[TPM2_RESPONSE_BUFFER_SIZE];
  316. u32 ret;
  317. memset(response, 0, sizeof(response));
  318. ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
  319. TPM2_PT_PCR_COUNT, response, 1);
  320. if (ret)
  321. return -1;
  322. *num_pcr = get_unaligned_be32(response + properties_offset);
  323. if (*num_pcr > TPM2_MAX_PCRS)
  324. return -1;
  325. return 0;
  326. }
  327. /**
  328. * is_active_pcr() - Check if a supported algorithm is active
  329. *
  330. * @dev: TPM device
  331. * @selection: struct of PCR information
  332. *
  333. * Return: true if PCR is active
  334. */
  335. static bool is_active_pcr(struct tpms_pcr_selection *selection)
  336. {
  337. int i;
  338. /*
  339. * check the pcr_select. If at least one of the PCRs supports the
  340. * algorithm add it on the active ones
  341. */
  342. for (i = 0; i < selection->size_of_select; i++) {
  343. if (selection->pcr_select[i])
  344. return true;
  345. }
  346. return false;
  347. }
  348. /**
  349. * tpm2_get_pcr_info() - get the supported, active PCRs and number of banks
  350. *
  351. * @dev: TPM device
  352. * @supported_pcr: bitmask with the algorithms supported
  353. * @active_pcr: bitmask with the active algorithms
  354. * @pcr_banks: number of PCR banks
  355. *
  356. * Return: 0 on success, -1 on error
  357. */
  358. static int tpm2_get_pcr_info(struct udevice *dev, u32 *supported_pcr,
  359. u32 *active_pcr, u32 *pcr_banks)
  360. {
  361. u8 response[TPM2_RESPONSE_BUFFER_SIZE];
  362. struct tpml_pcr_selection pcrs;
  363. u32 ret, num_pcr;
  364. size_t i;
  365. int tpm_ret;
  366. *supported_pcr = 0;
  367. *active_pcr = 0;
  368. *pcr_banks = 0;
  369. memset(response, 0, sizeof(response));
  370. ret = tpm2_get_capability(dev, TPM2_CAP_PCRS, 0, response, 1);
  371. if (ret)
  372. goto out;
  373. pcrs.count = get_unaligned_be32(response);
  374. /*
  375. * We only support 5 algorithms for now so check against that
  376. * instead of TPM2_NUM_PCR_BANKS
  377. */
  378. if (pcrs.count > MAX_HASH_COUNT || pcrs.count < 1)
  379. goto out;
  380. tpm_ret = tpm2_get_num_pcr(dev, &num_pcr);
  381. if (tpm_ret)
  382. goto out;
  383. for (i = 0; i < pcrs.count; i++) {
  384. /*
  385. * Definition of TPMS_PCR_SELECTION Structure
  386. * hash: u16
  387. * size_of_select: u8
  388. * pcr_select: u8 array
  389. *
  390. * The offsets depend on the number of the device PCRs
  391. * so we have to calculate them based on that
  392. */
  393. u32 hash_offset = offsetof(struct tpml_pcr_selection, selection) +
  394. i * offsetof(struct tpms_pcr_selection, pcr_select) +
  395. i * ((num_pcr + 7) / 8);
  396. u32 size_select_offset =
  397. hash_offset + offsetof(struct tpms_pcr_selection,
  398. size_of_select);
  399. u32 pcr_select_offset =
  400. hash_offset + offsetof(struct tpms_pcr_selection,
  401. pcr_select);
  402. pcrs.selection[i].hash =
  403. get_unaligned_be16(response + hash_offset);
  404. pcrs.selection[i].size_of_select =
  405. __get_unaligned_be(response + size_select_offset);
  406. if (pcrs.selection[i].size_of_select > TPM2_PCR_SELECT_MAX)
  407. goto out;
  408. /* copy the array of pcr_select */
  409. memcpy(pcrs.selection[i].pcr_select, response + pcr_select_offset,
  410. pcrs.selection[i].size_of_select);
  411. }
  412. for (i = 0; i < pcrs.count; i++) {
  413. u32 hash_mask = alg_to_mask(pcrs.selection[i].hash);
  414. if (hash_mask) {
  415. *supported_pcr |= hash_mask;
  416. if (is_active_pcr(&pcrs.selection[i]))
  417. *active_pcr |= hash_mask;
  418. } else {
  419. EFI_PRINT("Unknown algorithm %x\n", pcrs.selection[i].hash);
  420. }
  421. }
  422. *pcr_banks = pcrs.count;
  423. return 0;
  424. out:
  425. return -1;
  426. }
  427. /**
  428. * __get_active_pcr_banks() - returns the currently active PCR banks
  429. *
  430. * @active_pcr_banks: pointer for receiving the bitmap of currently
  431. * active PCR banks
  432. *
  433. * Return: status code
  434. */
  435. static efi_status_t __get_active_pcr_banks(u32 *active_pcr_banks)
  436. {
  437. struct udevice *dev;
  438. u32 active = 0, supported = 0, pcr_banks = 0;
  439. efi_status_t ret;
  440. int err;
  441. ret = platform_get_tpm2_device(&dev);
  442. if (ret != EFI_SUCCESS)
  443. goto out;
  444. err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_banks);
  445. if (err) {
  446. ret = EFI_DEVICE_ERROR;
  447. goto out;
  448. }
  449. *active_pcr_banks = active;
  450. out:
  451. return ret;
  452. }
  453. /* tcg2_create_digest - create a list of digests of the supported PCR banks
  454. * for a given memory range
  455. *
  456. * @input: input memory
  457. * @length: length of buffer to calculate the digest
  458. * @digest_list: list of digests to fill in
  459. *
  460. * Return: status code
  461. */
  462. static efi_status_t tcg2_create_digest(const u8 *input, u32 length,
  463. struct tpml_digest_values *digest_list)
  464. {
  465. sha1_context ctx;
  466. sha256_context ctx_256;
  467. sha512_context ctx_512;
  468. u8 final[TPM2_SHA512_DIGEST_SIZE];
  469. efi_status_t ret;
  470. u32 active;
  471. size_t i;
  472. ret = __get_active_pcr_banks(&active);
  473. if (ret != EFI_SUCCESS)
  474. return ret;
  475. digest_list->count = 0;
  476. for (i = 0; i < MAX_HASH_COUNT; i++) {
  477. u16 hash_alg = hash_algo_list[i].hash_alg;
  478. if (!(active & alg_to_mask(hash_alg)))
  479. continue;
  480. switch (hash_alg) {
  481. case TPM2_ALG_SHA1:
  482. sha1_starts(&ctx);
  483. sha1_update(&ctx, input, length);
  484. sha1_finish(&ctx, final);
  485. break;
  486. case TPM2_ALG_SHA256:
  487. sha256_starts(&ctx_256);
  488. sha256_update(&ctx_256, input, length);
  489. sha256_finish(&ctx_256, final);
  490. break;
  491. case TPM2_ALG_SHA384:
  492. sha384_starts(&ctx_512);
  493. sha384_update(&ctx_512, input, length);
  494. sha384_finish(&ctx_512, final);
  495. break;
  496. case TPM2_ALG_SHA512:
  497. sha512_starts(&ctx_512);
  498. sha512_update(&ctx_512, input, length);
  499. sha512_finish(&ctx_512, final);
  500. break;
  501. default:
  502. EFI_PRINT("Unsupported algorithm %x\n", hash_alg);
  503. return EFI_INVALID_PARAMETER;
  504. }
  505. digest_list->digests[digest_list->count].hash_alg = hash_alg;
  506. memcpy(&digest_list->digests[digest_list->count].digest, final,
  507. (u32)alg_to_len(hash_alg));
  508. digest_list->count++;
  509. }
  510. return EFI_SUCCESS;
  511. }
  512. /**
  513. * efi_tcg2_get_capability() - protocol capability information and state information
  514. *
  515. * @this: TCG2 protocol instance
  516. * @capability: caller allocated memory with size field to the size of
  517. * the structure allocated
  518. * Return: status code
  519. */
  520. static efi_status_t EFIAPI
  521. efi_tcg2_get_capability(struct efi_tcg2_protocol *this,
  522. struct efi_tcg2_boot_service_capability *capability)
  523. {
  524. struct udevice *dev;
  525. efi_status_t efi_ret;
  526. int ret;
  527. EFI_ENTRY("%p, %p", this, capability);
  528. if (!this || !capability) {
  529. efi_ret = EFI_INVALID_PARAMETER;
  530. goto out;
  531. }
  532. if (capability->size < BOOT_SERVICE_CAPABILITY_MIN) {
  533. capability->size = BOOT_SERVICE_CAPABILITY_MIN;
  534. efi_ret = EFI_BUFFER_TOO_SMALL;
  535. goto out;
  536. }
  537. if (capability->size < sizeof(*capability)) {
  538. capability->size = sizeof(*capability);
  539. efi_ret = EFI_BUFFER_TOO_SMALL;
  540. goto out;
  541. }
  542. capability->structure_version.major = 1;
  543. capability->structure_version.minor = 1;
  544. capability->protocol_version.major = 1;
  545. capability->protocol_version.minor = 1;
  546. efi_ret = platform_get_tpm2_device(&dev);
  547. if (efi_ret != EFI_SUCCESS) {
  548. capability->supported_event_logs = 0;
  549. capability->hash_algorithm_bitmap = 0;
  550. capability->tpm_present_flag = false;
  551. capability->max_command_size = 0;
  552. capability->max_response_size = 0;
  553. capability->manufacturer_id = 0;
  554. capability->number_of_pcr_banks = 0;
  555. capability->active_pcr_banks = 0;
  556. efi_ret = EFI_SUCCESS;
  557. goto out;
  558. }
  559. /* We only allow a TPMv2 device to register the EFI protocol */
  560. capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2;
  561. capability->tpm_present_flag = true;
  562. /* Supported and active PCRs */
  563. capability->hash_algorithm_bitmap = 0;
  564. capability->active_pcr_banks = 0;
  565. ret = tpm2_get_pcr_info(dev, &capability->hash_algorithm_bitmap,
  566. &capability->active_pcr_banks,
  567. &capability->number_of_pcr_banks);
  568. if (ret) {
  569. efi_ret = EFI_DEVICE_ERROR;
  570. goto out;
  571. }
  572. /* Max command size */
  573. ret = tpm2_get_max_command_size(dev, &capability->max_command_size);
  574. if (ret) {
  575. efi_ret = EFI_DEVICE_ERROR;
  576. goto out;
  577. }
  578. /* Max response size */
  579. ret = tpm2_get_max_response_size(dev, &capability->max_response_size);
  580. if (ret) {
  581. efi_ret = EFI_DEVICE_ERROR;
  582. goto out;
  583. }
  584. /* Manufacturer ID */
  585. ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id);
  586. if (ret) {
  587. efi_ret = EFI_DEVICE_ERROR;
  588. goto out;
  589. }
  590. return EFI_EXIT(EFI_SUCCESS);
  591. out:
  592. return EFI_EXIT(efi_ret);
  593. }
  594. /**
  595. * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its
  596. * last entry
  597. *
  598. * @this: TCG2 protocol instance
  599. * @log_format: type of event log format
  600. * @event_log_location: pointer to the memory address of the event log
  601. * @event_log_last_entry: pointer to the address of the start of the last
  602. * entry in the event log in memory, if log contains
  603. * more than 1 entry
  604. * @event_log_truncated: set to true, if the Event Log is missing at i
  605. * least one entry
  606. *
  607. * Return: status code
  608. */
  609. static efi_status_t EFIAPI
  610. efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this,
  611. efi_tcg_event_log_format log_format,
  612. u64 *event_log_location, u64 *event_log_last_entry,
  613. bool *event_log_truncated)
  614. {
  615. efi_status_t ret = EFI_SUCCESS;
  616. struct udevice *dev;
  617. EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location,
  618. event_log_last_entry, event_log_truncated);
  619. if (!this || !event_log_location || !event_log_last_entry ||
  620. !event_log_truncated) {
  621. ret = EFI_INVALID_PARAMETER;
  622. goto out;
  623. }
  624. /* Only support TPMV2 */
  625. if (log_format != TCG2_EVENT_LOG_FORMAT_TCG_2) {
  626. ret = EFI_INVALID_PARAMETER;
  627. goto out;
  628. }
  629. ret = platform_get_tpm2_device(&dev);
  630. if (ret != EFI_SUCCESS) {
  631. event_log_location = NULL;
  632. event_log_last_entry = NULL;
  633. *event_log_truncated = false;
  634. ret = EFI_SUCCESS;
  635. goto out;
  636. }
  637. *event_log_location = (uintptr_t)event_log.buffer;
  638. *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos -
  639. event_log.last_event_size);
  640. *event_log_truncated = event_log.truncated;
  641. event_log.get_event_called = true;
  642. out:
  643. return EFI_EXIT(ret);
  644. }
  645. /**
  646. * tcg2_hash_pe_image() - calculate PE/COFF image hash
  647. *
  648. * @efi: pointer to the EFI binary
  649. * @efi_size: size of @efi binary
  650. * @digest_list: list of digest algorithms to extend
  651. *
  652. * Return: status code
  653. */
  654. static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size,
  655. struct tpml_digest_values *digest_list)
  656. {
  657. WIN_CERTIFICATE *wincerts = NULL;
  658. size_t wincerts_len;
  659. struct efi_image_regions *regs = NULL;
  660. void *new_efi = NULL;
  661. u8 hash[TPM2_SHA512_DIGEST_SIZE];
  662. efi_status_t ret;
  663. u32 active;
  664. int i;
  665. new_efi = efi_prepare_aligned_image(efi, &efi_size);
  666. if (!new_efi)
  667. return EFI_OUT_OF_RESOURCES;
  668. if (!efi_image_parse(new_efi, efi_size, &regs, &wincerts,
  669. &wincerts_len)) {
  670. log_err("Parsing PE executable image failed\n");
  671. ret = EFI_UNSUPPORTED;
  672. goto out;
  673. }
  674. ret = __get_active_pcr_banks(&active);
  675. if (ret != EFI_SUCCESS) {
  676. goto out;
  677. }
  678. digest_list->count = 0;
  679. for (i = 0; i < MAX_HASH_COUNT; i++) {
  680. u16 hash_alg = hash_algo_list[i].hash_alg;
  681. if (!(active & alg_to_mask(hash_alg)))
  682. continue;
  683. switch (hash_alg) {
  684. case TPM2_ALG_SHA1:
  685. hash_calculate("sha1", regs->reg, regs->num, hash);
  686. break;
  687. case TPM2_ALG_SHA256:
  688. hash_calculate("sha256", regs->reg, regs->num, hash);
  689. break;
  690. case TPM2_ALG_SHA384:
  691. hash_calculate("sha384", regs->reg, regs->num, hash);
  692. break;
  693. case TPM2_ALG_SHA512:
  694. hash_calculate("sha512", regs->reg, regs->num, hash);
  695. break;
  696. default:
  697. EFI_PRINT("Unsupported algorithm %x\n", hash_alg);
  698. return EFI_INVALID_PARAMETER;
  699. }
  700. digest_list->digests[digest_list->count].hash_alg = hash_alg;
  701. memcpy(&digest_list->digests[digest_list->count].digest, hash,
  702. (u32)alg_to_len(hash_alg));
  703. digest_list->count++;
  704. }
  705. out:
  706. if (new_efi != efi)
  707. free(new_efi);
  708. free(regs);
  709. return ret;
  710. }
  711. /**
  712. * tcg2_measure_pe_image() - measure PE/COFF image
  713. *
  714. * @efi: pointer to the EFI binary
  715. * @efi_size: size of @efi binary
  716. * @handle: loaded image handle
  717. * @loaded_image: loaded image protocol
  718. *
  719. * Return: status code
  720. */
  721. efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size,
  722. struct efi_loaded_image_obj *handle,
  723. struct efi_loaded_image *loaded_image)
  724. {
  725. struct tpml_digest_values digest_list;
  726. efi_status_t ret;
  727. struct udevice *dev;
  728. u32 pcr_index, event_type, event_size;
  729. struct uefi_image_load_event *image_load_event;
  730. struct efi_device_path *device_path;
  731. u32 device_path_length;
  732. IMAGE_DOS_HEADER *dos;
  733. IMAGE_NT_HEADERS32 *nt;
  734. struct efi_handler *handler;
  735. ret = platform_get_tpm2_device(&dev);
  736. if (ret != EFI_SUCCESS)
  737. return ret;
  738. switch (handle->image_type) {
  739. case IMAGE_SUBSYSTEM_EFI_APPLICATION:
  740. pcr_index = 4;
  741. event_type = EV_EFI_BOOT_SERVICES_APPLICATION;
  742. break;
  743. case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
  744. pcr_index = 2;
  745. event_type = EV_EFI_BOOT_SERVICES_DRIVER;
  746. break;
  747. case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
  748. pcr_index = 2;
  749. event_type = EV_EFI_RUNTIME_SERVICES_DRIVER;
  750. break;
  751. default:
  752. return EFI_UNSUPPORTED;
  753. }
  754. ret = tcg2_hash_pe_image(efi, efi_size, &digest_list);
  755. if (ret != EFI_SUCCESS)
  756. return ret;
  757. ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
  758. if (ret != EFI_SUCCESS)
  759. return ret;
  760. ret = efi_search_protocol(&handle->header,
  761. &efi_guid_loaded_image_device_path, &handler);
  762. if (ret != EFI_SUCCESS)
  763. return ret;
  764. device_path = handler->protocol_interface;
  765. device_path_length = efi_dp_size(device_path);
  766. if (device_path_length > 0) {
  767. /* add end node size */
  768. device_path_length += sizeof(struct efi_device_path);
  769. }
  770. event_size = sizeof(struct uefi_image_load_event) + device_path_length;
  771. image_load_event = calloc(1, event_size);
  772. if (!image_load_event)
  773. return EFI_OUT_OF_RESOURCES;
  774. image_load_event->image_location_in_memory = (uintptr_t)efi;
  775. image_load_event->image_length_in_memory = efi_size;
  776. image_load_event->length_of_device_path = device_path_length;
  777. dos = (IMAGE_DOS_HEADER *)efi;
  778. nt = (IMAGE_NT_HEADERS32 *)(efi + dos->e_lfanew);
  779. if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
  780. IMAGE_NT_HEADERS64 *nt64 = (IMAGE_NT_HEADERS64 *)nt;
  781. image_load_event->image_link_time_address =
  782. nt64->OptionalHeader.ImageBase;
  783. } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
  784. image_load_event->image_link_time_address =
  785. nt->OptionalHeader.ImageBase;
  786. } else {
  787. ret = EFI_INVALID_PARAMETER;
  788. goto out;
  789. }
  790. /* device_path_length might be zero */
  791. memcpy(image_load_event->device_path, device_path, device_path_length);
  792. ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
  793. event_size, (u8 *)image_load_event);
  794. out:
  795. free(image_load_event);
  796. return ret;
  797. }
  798. /**
  799. * efi_tcg2_hash_log_extend_event() - extend and optionally log events
  800. *
  801. * @this: TCG2 protocol instance
  802. * @flags: bitmap providing additional information on the
  803. * operation
  804. * @data_to_hash: physical address of the start of the data buffer
  805. * to be hashed
  806. * @data_to_hash_len: the length in bytes of the buffer referenced by
  807. * data_to_hash
  808. * @efi_tcg_event: pointer to data buffer containing information
  809. * about the event
  810. *
  811. * Return: status code
  812. */
  813. static efi_status_t EFIAPI
  814. efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags,
  815. u64 data_to_hash, u64 data_to_hash_len,
  816. struct efi_tcg2_event *efi_tcg_event)
  817. {
  818. struct udevice *dev;
  819. efi_status_t ret;
  820. u32 event_type, pcr_index, event_size;
  821. struct tpml_digest_values digest_list;
  822. EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash,
  823. data_to_hash_len, efi_tcg_event);
  824. if (!this || !data_to_hash || !efi_tcg_event) {
  825. ret = EFI_INVALID_PARAMETER;
  826. goto out;
  827. }
  828. ret = platform_get_tpm2_device(&dev);
  829. if (ret != EFI_SUCCESS)
  830. goto out;
  831. if (efi_tcg_event->size < efi_tcg_event->header.header_size +
  832. sizeof(u32)) {
  833. ret = EFI_INVALID_PARAMETER;
  834. goto out;
  835. }
  836. if (efi_tcg_event->header.pcr_index > EFI_TCG2_MAX_PCR_INDEX) {
  837. ret = EFI_INVALID_PARAMETER;
  838. goto out;
  839. }
  840. /*
  841. * if PE_COFF_IMAGE is set we need to make sure the image is not
  842. * corrupted, verify it and hash the PE/COFF image in accordance with
  843. * the procedure specified in "Calculating the PE Image Hash"
  844. * section of the "Windows Authenticode Portable Executable Signature
  845. * Format"
  846. */
  847. if (flags & PE_COFF_IMAGE) {
  848. IMAGE_NT_HEADERS32 *nt;
  849. ret = efi_check_pe((void *)(uintptr_t)data_to_hash,
  850. data_to_hash_len, (void **)&nt);
  851. if (ret != EFI_SUCCESS) {
  852. log_err("Not a valid PE-COFF file\n");
  853. ret = EFI_UNSUPPORTED;
  854. goto out;
  855. }
  856. ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash,
  857. data_to_hash_len, &digest_list);
  858. } else {
  859. ret = tcg2_create_digest((u8 *)(uintptr_t)data_to_hash,
  860. data_to_hash_len, &digest_list);
  861. }
  862. if (ret != EFI_SUCCESS)
  863. goto out;
  864. pcr_index = efi_tcg_event->header.pcr_index;
  865. event_type = efi_tcg_event->header.event_type;
  866. ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
  867. if (ret != EFI_SUCCESS)
  868. goto out;
  869. if (flags & EFI_TCG2_EXTEND_ONLY) {
  870. if (event_log.truncated)
  871. ret = EFI_VOLUME_FULL;
  872. goto out;
  873. }
  874. /*
  875. * The efi_tcg_event size includes the size component and the
  876. * headersize
  877. */
  878. event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) -
  879. efi_tcg_event->header.header_size;
  880. ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
  881. event_size, efi_tcg_event->event);
  882. out:
  883. return EFI_EXIT(ret);
  884. }
  885. /**
  886. * efi_tcg2_submit_command() - Send command to the TPM
  887. *
  888. * @this: TCG2 protocol instance
  889. * @input_param_block_size: size of the TPM input parameter block
  890. * @input_param_block: pointer to the TPM input parameter block
  891. * @output_param_block_size: size of the TPM output parameter block
  892. * @output_param_block: pointer to the TPM output parameter block
  893. *
  894. * Return: status code
  895. */
  896. static efi_status_t EFIAPI
  897. efi_tcg2_submit_command(__maybe_unused struct efi_tcg2_protocol *this,
  898. u32 __maybe_unused input_param_block_size,
  899. u8 __maybe_unused *input_param_block,
  900. u32 __maybe_unused output_param_block_size,
  901. u8 __maybe_unused *output_param_block)
  902. {
  903. return EFI_UNSUPPORTED;
  904. }
  905. /**
  906. * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks
  907. *
  908. * @this: TCG2 protocol instance
  909. * @active_pcr_banks: pointer for receiving the bitmap of currently
  910. * active PCR banks
  911. *
  912. * Return: status code
  913. */
  914. static efi_status_t EFIAPI
  915. efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
  916. u32 *active_pcr_banks)
  917. {
  918. efi_status_t ret;
  919. if (!this || !active_pcr_banks) {
  920. ret = EFI_INVALID_PARAMETER;
  921. goto out;
  922. }
  923. EFI_ENTRY("%p, %p", this, active_pcr_banks);
  924. ret = __get_active_pcr_banks(active_pcr_banks);
  925. out:
  926. return EFI_EXIT(ret);
  927. }
  928. /**
  929. * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks
  930. *
  931. * @this: TCG2 protocol instance
  932. * @active_pcr_banks: bitmap of the requested active PCR banks
  933. *
  934. * Return: status code
  935. */
  936. static efi_status_t EFIAPI
  937. efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
  938. u32 __maybe_unused active_pcr_banks)
  939. {
  940. return EFI_UNSUPPORTED;
  941. }
  942. /**
  943. * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous
  944. * set_active_pcr_banks()
  945. *
  946. * @this: TCG2 protocol instance
  947. * @operation_present: non-zero value to indicate a
  948. * set_active_pcr_banks operation was
  949. * invoked during last boot
  950. * @response: result value could be returned
  951. *
  952. * Return: status code
  953. */
  954. static efi_status_t EFIAPI
  955. efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
  956. u32 __maybe_unused *operation_present,
  957. u32 __maybe_unused *response)
  958. {
  959. return EFI_UNSUPPORTED;
  960. }
  961. static const struct efi_tcg2_protocol efi_tcg2_protocol = {
  962. .get_capability = efi_tcg2_get_capability,
  963. .get_eventlog = efi_tcg2_get_eventlog,
  964. .hash_log_extend_event = efi_tcg2_hash_log_extend_event,
  965. .submit_command = efi_tcg2_submit_command,
  966. .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks,
  967. .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks,
  968. .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks,
  969. };
  970. /**
  971. * create_specid_event() - Create the first event in the eventlog
  972. *
  973. * @dev: tpm device
  974. * @event_header: Pointer to the final event header
  975. * @event_size: final spec event size
  976. *
  977. * Return: status code
  978. */
  979. static efi_status_t create_specid_event(struct udevice *dev, void *buffer,
  980. size_t *event_size)
  981. {
  982. struct tcg_efi_spec_id_event *spec_event;
  983. size_t spec_event_size;
  984. efi_status_t ret = EFI_DEVICE_ERROR;
  985. u32 active = 0, supported = 0, pcr_count = 0, alg_count = 0;
  986. int err;
  987. size_t i;
  988. /*
  989. * Create Spec event. This needs to be the first event in the log
  990. * according to the TCG EFI protocol spec
  991. */
  992. /* Setup specID event data */
  993. spec_event = (struct tcg_efi_spec_id_event *)buffer;
  994. memcpy(spec_event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
  995. sizeof(spec_event->signature));
  996. put_unaligned_le32(0, &spec_event->platform_class); /* type client */
  997. spec_event->spec_version_minor =
  998. TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2;
  999. spec_event->spec_version_major =
  1000. TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2;
  1001. spec_event->spec_errata =
  1002. TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2;
  1003. spec_event->uintn_size = sizeof(efi_uintn_t) / sizeof(u32);
  1004. err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_count);
  1005. if (err)
  1006. goto out;
  1007. for (i = 0; i < pcr_count; i++) {
  1008. u16 hash_alg = hash_algo_list[i].hash_alg;
  1009. u16 hash_len = hash_algo_list[i].hash_len;
  1010. if (active & alg_to_mask(hash_alg)) {
  1011. put_unaligned_le16(hash_alg,
  1012. &spec_event->digest_sizes[alg_count].algorithm_id);
  1013. put_unaligned_le16(hash_len,
  1014. &spec_event->digest_sizes[alg_count].digest_size);
  1015. alg_count++;
  1016. }
  1017. }
  1018. spec_event->number_of_algorithms = alg_count;
  1019. if (spec_event->number_of_algorithms > MAX_HASH_COUNT ||
  1020. spec_event->number_of_algorithms < 1)
  1021. goto out;
  1022. /*
  1023. * the size of the spec event and placement of vendor_info_size
  1024. * depends on supported algoriths
  1025. */
  1026. spec_event_size =
  1027. offsetof(struct tcg_efi_spec_id_event, digest_sizes) +
  1028. spec_event->number_of_algorithms * sizeof(spec_event->digest_sizes[0]);
  1029. /* no vendor info for us */
  1030. memset(buffer + spec_event_size, 0, 1);
  1031. /* add a byte for vendor_info_size in the spec event */
  1032. spec_event_size += 1;
  1033. *event_size = spec_event_size;
  1034. return EFI_SUCCESS;
  1035. out:
  1036. return ret;
  1037. }
  1038. /**
  1039. * tcg2_uninit - remove the final event table and free efi memory on failures
  1040. */
  1041. void tcg2_uninit(void)
  1042. {
  1043. efi_status_t ret;
  1044. ret = efi_install_configuration_table(&efi_guid_final_events, NULL);
  1045. if (ret != EFI_SUCCESS)
  1046. log_err("Failed to delete final events config table\n");
  1047. efi_free_pool(event_log.buffer);
  1048. event_log.buffer = NULL;
  1049. efi_free_pool(event_log.final_buffer);
  1050. event_log.final_buffer = NULL;
  1051. }
  1052. /**
  1053. * create_final_event() - Create the final event and install the config
  1054. * defined by the TCG EFI spec
  1055. */
  1056. static efi_status_t create_final_event(void)
  1057. {
  1058. struct efi_tcg2_final_events_table *final_event;
  1059. efi_status_t ret;
  1060. /*
  1061. * All events generated after the invocation of
  1062. * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an
  1063. * EFI_CONFIGURATION_TABLE
  1064. */
  1065. ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE,
  1066. &event_log.final_buffer);
  1067. if (ret != EFI_SUCCESS)
  1068. goto out;
  1069. memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE);
  1070. final_event = event_log.final_buffer;
  1071. final_event->number_of_events = 0;
  1072. final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
  1073. event_log.final_pos = sizeof(*final_event);
  1074. ret = efi_install_configuration_table(&efi_guid_final_events,
  1075. final_event);
  1076. if (ret != EFI_SUCCESS) {
  1077. efi_free_pool(event_log.final_buffer);
  1078. event_log.final_buffer = NULL;
  1079. }
  1080. out:
  1081. return ret;
  1082. }
  1083. /**
  1084. * efi_init_event_log() - initialize an eventlog
  1085. */
  1086. static efi_status_t efi_init_event_log(void)
  1087. {
  1088. /*
  1089. * vendor_info_size is currently set to 0, we need to change the length
  1090. * and allocate the flexible array member if this changes
  1091. */
  1092. struct tcg_pcr_event *event_header = NULL;
  1093. struct udevice *dev;
  1094. size_t spec_event_size;
  1095. efi_status_t ret;
  1096. ret = platform_get_tpm2_device(&dev);
  1097. if (ret != EFI_SUCCESS)
  1098. goto out;
  1099. ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE,
  1100. (void **)&event_log.buffer);
  1101. if (ret != EFI_SUCCESS)
  1102. goto out;
  1103. /*
  1104. * initialize log area as 0xff so the OS can easily figure out the
  1105. * last log entry
  1106. */
  1107. memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE);
  1108. event_log.pos = 0;
  1109. event_log.last_event_size = 0;
  1110. event_log.get_event_called = false;
  1111. event_log.truncated = false;
  1112. /*
  1113. * The log header is defined to be in SHA1 event log entry format.
  1114. * Setup event header
  1115. */
  1116. event_header = (struct tcg_pcr_event *)event_log.buffer;
  1117. put_unaligned_le32(0, &event_header->pcr_index);
  1118. put_unaligned_le32(EV_NO_ACTION, &event_header->event_type);
  1119. memset(&event_header->digest, 0, sizeof(event_header->digest));
  1120. ret = create_specid_event(dev, (void *)((uintptr_t)event_log.buffer + sizeof(*event_header)),
  1121. &spec_event_size);
  1122. if (ret != EFI_SUCCESS)
  1123. goto free_pool;
  1124. put_unaligned_le32(spec_event_size, &event_header->event_size);
  1125. event_log.pos = spec_event_size + sizeof(*event_header);
  1126. event_log.last_event_size = event_log.pos;
  1127. ret = create_final_event();
  1128. if (ret != EFI_SUCCESS)
  1129. goto free_pool;
  1130. out:
  1131. return ret;
  1132. free_pool:
  1133. efi_free_pool(event_log.buffer);
  1134. event_log.buffer = NULL;
  1135. return ret;
  1136. }
  1137. /**
  1138. * tcg2_measure_event() - common function to add event log and extend PCR
  1139. *
  1140. * @dev: TPM device
  1141. * @pcr_index: PCR index
  1142. * @event_type: type of event added
  1143. * @size: event size
  1144. * @event: event data
  1145. *
  1146. * Return: status code
  1147. */
  1148. static efi_status_t
  1149. tcg2_measure_event(struct udevice *dev, u32 pcr_index, u32 event_type,
  1150. u32 size, u8 event[])
  1151. {
  1152. struct tpml_digest_values digest_list;
  1153. efi_status_t ret;
  1154. ret = tcg2_create_digest(event, size, &digest_list);
  1155. if (ret != EFI_SUCCESS)
  1156. goto out;
  1157. ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
  1158. if (ret != EFI_SUCCESS)
  1159. goto out;
  1160. ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
  1161. size, event);
  1162. out:
  1163. return ret;
  1164. }
  1165. /**
  1166. * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
  1167. * eventlog and extend the PCRs
  1168. *
  1169. * @dev: TPM device
  1170. *
  1171. * @Return: status code
  1172. */
  1173. static efi_status_t efi_append_scrtm_version(struct udevice *dev)
  1174. {
  1175. u8 ver[] = U_BOOT_VERSION_STRING;
  1176. efi_status_t ret;
  1177. ret = tcg2_measure_event(dev, 0, EV_S_CRTM_VERSION, sizeof(ver), ver);
  1178. return ret;
  1179. }
  1180. /**
  1181. * tcg2_measure_variable() - add variable event log and extend PCR
  1182. *
  1183. * @dev: TPM device
  1184. * @pcr_index: PCR index
  1185. * @event_type: type of event added
  1186. * @var_name: variable name
  1187. * @guid: guid
  1188. * @data_size: variable data size
  1189. * @data: variable data
  1190. *
  1191. * Return: status code
  1192. */
  1193. static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index,
  1194. u32 event_type, u16 *var_name,
  1195. const efi_guid_t *guid,
  1196. efi_uintn_t data_size, u8 *data)
  1197. {
  1198. u32 event_size;
  1199. efi_status_t ret;
  1200. struct efi_tcg2_uefi_variable_data *event;
  1201. event_size = sizeof(event->variable_name) +
  1202. sizeof(event->unicode_name_length) +
  1203. sizeof(event->variable_data_length) +
  1204. (u16_strlen(var_name) * sizeof(u16)) + data_size;
  1205. event = malloc(event_size);
  1206. if (!event)
  1207. return EFI_OUT_OF_RESOURCES;
  1208. guidcpy(&event->variable_name, guid);
  1209. event->unicode_name_length = u16_strlen(var_name);
  1210. event->variable_data_length = data_size;
  1211. memcpy(event->unicode_name, var_name,
  1212. (event->unicode_name_length * sizeof(u16)));
  1213. if (data) {
  1214. memcpy((u16 *)event->unicode_name + event->unicode_name_length,
  1215. data, data_size);
  1216. }
  1217. ret = tcg2_measure_event(dev, pcr_index, event_type, event_size,
  1218. (u8 *)event);
  1219. free(event);
  1220. return ret;
  1221. }
  1222. /**
  1223. * tcg2_measure_boot_variable() - measure boot variables
  1224. *
  1225. * @dev: TPM device
  1226. *
  1227. * Return: status code
  1228. */
  1229. static efi_status_t tcg2_measure_boot_variable(struct udevice *dev)
  1230. {
  1231. u16 *boot_order;
  1232. u16 *boot_index;
  1233. u16 var_name[] = L"BootOrder";
  1234. u16 boot_name[] = L"Boot####";
  1235. u8 *bootvar;
  1236. efi_uintn_t var_data_size;
  1237. u32 count, i;
  1238. efi_status_t ret;
  1239. boot_order = efi_get_var(var_name, &efi_global_variable_guid,
  1240. &var_data_size);
  1241. if (!boot_order) {
  1242. ret = EFI_NOT_FOUND;
  1243. goto error;
  1244. }
  1245. ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name,
  1246. &efi_global_variable_guid, var_data_size,
  1247. (u8 *)boot_order);
  1248. if (ret != EFI_SUCCESS)
  1249. goto error;
  1250. count = var_data_size / sizeof(*boot_order);
  1251. boot_index = boot_order;
  1252. for (i = 0; i < count; i++) {
  1253. efi_create_indexed_name(boot_name, sizeof(boot_name),
  1254. "Boot", *boot_index++);
  1255. bootvar = efi_get_var(boot_name, &efi_global_variable_guid,
  1256. &var_data_size);
  1257. if (!bootvar) {
  1258. log_info("%ls not found\n", boot_name);
  1259. continue;
  1260. }
  1261. ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2,
  1262. boot_name,
  1263. &efi_global_variable_guid,
  1264. var_data_size, bootvar);
  1265. free(bootvar);
  1266. if (ret != EFI_SUCCESS)
  1267. goto error;
  1268. }
  1269. error:
  1270. free(boot_order);
  1271. return ret;
  1272. }
  1273. /**
  1274. * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation
  1275. *
  1276. * Return: status code
  1277. */
  1278. efi_status_t efi_tcg2_measure_efi_app_invocation(void)
  1279. {
  1280. efi_status_t ret;
  1281. u32 pcr_index;
  1282. struct udevice *dev;
  1283. u32 event = 0;
  1284. if (tcg2_efi_app_invoked)
  1285. return EFI_SUCCESS;
  1286. ret = platform_get_tpm2_device(&dev);
  1287. if (ret != EFI_SUCCESS)
  1288. return ret;
  1289. ret = tcg2_measure_boot_variable(dev);
  1290. if (ret != EFI_SUCCESS)
  1291. goto out;
  1292. ret = tcg2_measure_event(dev, 4, EV_EFI_ACTION,
  1293. strlen(EFI_CALLING_EFI_APPLICATION),
  1294. (u8 *)EFI_CALLING_EFI_APPLICATION);
  1295. if (ret != EFI_SUCCESS)
  1296. goto out;
  1297. for (pcr_index = 0; pcr_index <= 7; pcr_index++) {
  1298. ret = tcg2_measure_event(dev, pcr_index, EV_SEPARATOR,
  1299. sizeof(event), (u8 *)&event);
  1300. if (ret != EFI_SUCCESS)
  1301. goto out;
  1302. }
  1303. tcg2_efi_app_invoked = true;
  1304. out:
  1305. return ret;
  1306. }
  1307. /**
  1308. * efi_tcg2_measure_efi_app_exit() - measure efi app exit
  1309. *
  1310. * Return: status code
  1311. */
  1312. efi_status_t efi_tcg2_measure_efi_app_exit(void)
  1313. {
  1314. efi_status_t ret;
  1315. struct udevice *dev;
  1316. ret = platform_get_tpm2_device(&dev);
  1317. if (ret != EFI_SUCCESS)
  1318. return ret;
  1319. ret = tcg2_measure_event(dev, 4, EV_EFI_ACTION,
  1320. strlen(EFI_RETURNING_FROM_EFI_APPLICATION),
  1321. (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION);
  1322. return ret;
  1323. }
  1324. /**
  1325. * efi_tcg2_notify_exit_boot_services() - ExitBootService callback
  1326. *
  1327. * @event: callback event
  1328. * @context: callback context
  1329. */
  1330. static void EFIAPI
  1331. efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context)
  1332. {
  1333. efi_status_t ret;
  1334. struct udevice *dev;
  1335. EFI_ENTRY("%p, %p", event, context);
  1336. ret = platform_get_tpm2_device(&dev);
  1337. if (ret != EFI_SUCCESS)
  1338. goto out;
  1339. ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
  1340. strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
  1341. (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
  1342. if (ret != EFI_SUCCESS)
  1343. goto out;
  1344. ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
  1345. strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED),
  1346. (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED);
  1347. out:
  1348. EFI_EXIT(ret);
  1349. }
  1350. /**
  1351. * efi_tcg2_notify_exit_boot_services_failed()
  1352. * - notify ExitBootServices() is failed
  1353. *
  1354. * Return: status code
  1355. */
  1356. efi_status_t efi_tcg2_notify_exit_boot_services_failed(void)
  1357. {
  1358. struct udevice *dev;
  1359. efi_status_t ret;
  1360. ret = platform_get_tpm2_device(&dev);
  1361. if (ret != EFI_SUCCESS)
  1362. goto out;
  1363. ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
  1364. strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
  1365. (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
  1366. if (ret != EFI_SUCCESS)
  1367. goto out;
  1368. ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
  1369. strlen(EFI_EXIT_BOOT_SERVICES_FAILED),
  1370. (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED);
  1371. out:
  1372. return ret;
  1373. }
  1374. /**
  1375. * tcg2_measure_secure_boot_variable() - measure secure boot variables
  1376. *
  1377. * @dev: TPM device
  1378. *
  1379. * Return: status code
  1380. */
  1381. static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev)
  1382. {
  1383. u8 *data;
  1384. efi_uintn_t data_size;
  1385. u32 count, i;
  1386. efi_status_t ret;
  1387. count = ARRAY_SIZE(secure_variables);
  1388. for (i = 0; i < count; i++) {
  1389. /*
  1390. * According to the TCG2 PC Client PFP spec, "SecureBoot",
  1391. * "PK", "KEK", "db" and "dbx" variables must be measured
  1392. * even if they are empty.
  1393. */
  1394. data = efi_get_var(secure_variables[i].name,
  1395. secure_variables[i].guid,
  1396. &data_size);
  1397. ret = tcg2_measure_variable(dev, 7,
  1398. EV_EFI_VARIABLE_DRIVER_CONFIG,
  1399. secure_variables[i].name,
  1400. secure_variables[i].guid,
  1401. data_size, data);
  1402. free(data);
  1403. if (ret != EFI_SUCCESS)
  1404. goto error;
  1405. }
  1406. /*
  1407. * TCG2 PC Client PFP spec says "dbt" and "dbr" are
  1408. * measured if present and not empty.
  1409. */
  1410. data = efi_get_var(L"dbt",
  1411. &efi_guid_image_security_database,
  1412. &data_size);
  1413. if (data) {
  1414. ret = tcg2_measure_variable(dev, 7,
  1415. EV_EFI_VARIABLE_DRIVER_CONFIG,
  1416. L"dbt",
  1417. &efi_guid_image_security_database,
  1418. data_size, data);
  1419. free(data);
  1420. }
  1421. data = efi_get_var(L"dbr",
  1422. &efi_guid_image_security_database,
  1423. &data_size);
  1424. if (data) {
  1425. ret = tcg2_measure_variable(dev, 7,
  1426. EV_EFI_VARIABLE_DRIVER_CONFIG,
  1427. L"dbr",
  1428. &efi_guid_image_security_database,
  1429. data_size, data);
  1430. free(data);
  1431. }
  1432. error:
  1433. return ret;
  1434. }
  1435. /**
  1436. * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
  1437. *
  1438. * If a TPM2 device is available, the TPM TCG2 Protocol is registered
  1439. *
  1440. * Return: An error status is only returned if adding the protocol fails.
  1441. */
  1442. efi_status_t efi_tcg2_register(void)
  1443. {
  1444. efi_status_t ret = EFI_SUCCESS;
  1445. struct udevice *dev;
  1446. struct efi_event *event;
  1447. ret = platform_get_tpm2_device(&dev);
  1448. if (ret != EFI_SUCCESS) {
  1449. log_warning("Unable to find TPMv2 device\n");
  1450. return EFI_SUCCESS;
  1451. }
  1452. ret = efi_init_event_log();
  1453. if (ret != EFI_SUCCESS)
  1454. goto fail;
  1455. ret = efi_append_scrtm_version(dev);
  1456. if (ret != EFI_SUCCESS) {
  1457. tcg2_uninit();
  1458. goto fail;
  1459. }
  1460. ret = efi_add_protocol(efi_root, &efi_guid_tcg2_protocol,
  1461. (void *)&efi_tcg2_protocol);
  1462. if (ret != EFI_SUCCESS) {
  1463. tcg2_uninit();
  1464. goto fail;
  1465. }
  1466. ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
  1467. efi_tcg2_notify_exit_boot_services, NULL,
  1468. NULL, &event);
  1469. if (ret != EFI_SUCCESS) {
  1470. tcg2_uninit();
  1471. goto fail;
  1472. }
  1473. ret = tcg2_measure_secure_boot_variable(dev);
  1474. if (ret != EFI_SUCCESS) {
  1475. tcg2_uninit();
  1476. goto fail;
  1477. }
  1478. return ret;
  1479. fail:
  1480. log_err("Cannot install EFI_TCG2_PROTOCOL\n");
  1481. /*
  1482. * Return EFI_SUCCESS and don't stop the EFI subsystem.
  1483. * That's done for 2 reasons
  1484. * - If the protocol is not installed the PCRs won't be extended. So
  1485. * someone later in the boot flow will notice that and take the
  1486. * necessary actions.
  1487. * - The TPM sandbox is limited and we won't be able to run any efi
  1488. * related tests with TCG2 enabled
  1489. */
  1490. return EFI_SUCCESS;
  1491. }