tpm-v2.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2018 Bootlin
  4. * Author: Miquel Raynal <miquel.raynal@bootlin.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <tpm-common.h>
  9. #include <tpm-v2.h>
  10. #include <linux/bitops.h>
  11. #include "tpm-utils.h"
  12. u32 tpm2_startup(struct udevice *dev, enum tpm2_startup_types mode)
  13. {
  14. const u8 command_v2[12] = {
  15. tpm_u16(TPM2_ST_NO_SESSIONS),
  16. tpm_u32(12),
  17. tpm_u32(TPM2_CC_STARTUP),
  18. tpm_u16(mode),
  19. };
  20. int ret;
  21. /*
  22. * Note TPM2_Startup command will return RC_SUCCESS the first time,
  23. * but will return RC_INITIALIZE otherwise.
  24. */
  25. ret = tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  26. if (ret && ret != TPM2_RC_INITIALIZE)
  27. return ret;
  28. return 0;
  29. }
  30. u32 tpm2_self_test(struct udevice *dev, enum tpm2_yes_no full_test)
  31. {
  32. const u8 command_v2[12] = {
  33. tpm_u16(TPM2_ST_NO_SESSIONS),
  34. tpm_u32(11),
  35. tpm_u32(TPM2_CC_SELF_TEST),
  36. full_test,
  37. };
  38. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  39. }
  40. u32 tpm2_clear(struct udevice *dev, u32 handle, const char *pw,
  41. const ssize_t pw_sz)
  42. {
  43. /* Length of the message header, up to start of password */
  44. uint offset = 27;
  45. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  46. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  47. tpm_u32(offset + pw_sz), /* Length */
  48. tpm_u32(TPM2_CC_CLEAR), /* Command code */
  49. /* HANDLE */
  50. tpm_u32(handle), /* TPM resource handle */
  51. /* AUTH_SESSION */
  52. tpm_u32(9 + pw_sz), /* Authorization size */
  53. tpm_u32(TPM2_RS_PW), /* Session handle */
  54. tpm_u16(0), /* Size of <nonce> */
  55. /* <nonce> (if any) */
  56. 0, /* Attributes: Cont/Excl/Rst */
  57. tpm_u16(pw_sz), /* Size of <hmac/password> */
  58. /* STRING(pw) <hmac/password> (if any) */
  59. };
  60. int ret;
  61. /*
  62. * Fill the command structure starting from the first buffer:
  63. * - the password (if any)
  64. */
  65. ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
  66. offset, pw, pw_sz);
  67. offset += pw_sz;
  68. if (ret)
  69. return TPM_LIB_ERROR;
  70. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  71. }
  72. u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
  73. size_t space_size, u32 nv_attributes,
  74. const u8 *nv_policy, size_t nv_policy_size)
  75. {
  76. /*
  77. * Calculate the offset of the nv_policy piece by adding each of the
  78. * chunks below.
  79. */
  80. uint offset = 10 + 8 + 13 + 14;
  81. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  82. /* header 10 bytes */
  83. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  84. tpm_u32(offset + nv_policy_size),/* Length */
  85. tpm_u32(TPM2_CC_NV_DEFINE_SPACE),/* Command code */
  86. /* handles 8 bytes */
  87. tpm_u32(TPM2_RH_PLATFORM), /* Primary platform seed */
  88. /* session header 13 bytes */
  89. tpm_u32(9), /* Header size */
  90. tpm_u32(TPM2_RS_PW), /* Password authorisation */
  91. tpm_u16(0), /* nonce_size */
  92. 0, /* session_attrs */
  93. tpm_u16(0), /* auth_size */
  94. /* message 14 bytes + policy */
  95. tpm_u16(12 + nv_policy_size), /* size */
  96. tpm_u32(space_index),
  97. tpm_u16(TPM2_ALG_SHA256),
  98. tpm_u32(nv_attributes),
  99. tpm_u16(nv_policy_size),
  100. /* nv_policy */
  101. };
  102. int ret;
  103. /*
  104. * Fill the command structure starting from the first buffer:
  105. * - the password (if any)
  106. */
  107. ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
  108. offset, nv_policy, nv_policy_size);
  109. if (ret)
  110. return TPM_LIB_ERROR;
  111. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  112. }
  113. u32 tpm2_pcr_extend(struct udevice *dev, u32 index, u32 algorithm,
  114. const u8 *digest, u32 digest_len)
  115. {
  116. /* Length of the message header, up to start of digest */
  117. uint offset = 33;
  118. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  119. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  120. tpm_u32(offset + digest_len), /* Length */
  121. tpm_u32(TPM2_CC_PCR_EXTEND), /* Command code */
  122. /* HANDLE */
  123. tpm_u32(index), /* Handle (PCR Index) */
  124. /* AUTH_SESSION */
  125. tpm_u32(9), /* Authorization size */
  126. tpm_u32(TPM2_RS_PW), /* Session handle */
  127. tpm_u16(0), /* Size of <nonce> */
  128. /* <nonce> (if any) */
  129. 0, /* Attributes: Cont/Excl/Rst */
  130. tpm_u16(0), /* Size of <hmac/password> */
  131. /* <hmac/password> (if any) */
  132. /* hashes */
  133. tpm_u32(1), /* Count (number of hashes) */
  134. tpm_u16(algorithm), /* Algorithm of the hash */
  135. /* STRING(digest) Digest */
  136. };
  137. int ret;
  138. /*
  139. * Fill the command structure starting from the first buffer:
  140. * - the digest
  141. */
  142. ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
  143. offset, digest, digest_len);
  144. if (ret)
  145. return TPM_LIB_ERROR;
  146. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  147. }
  148. u32 tpm2_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count)
  149. {
  150. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  151. /* header 10 bytes */
  152. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  153. tpm_u32(10 + 8 + 4 + 9 + 4), /* Length */
  154. tpm_u32(TPM2_CC_NV_READ), /* Command code */
  155. /* handles 8 bytes */
  156. tpm_u32(TPM2_RH_PLATFORM), /* Primary platform seed */
  157. tpm_u32(HR_NV_INDEX + index), /* Password authorisation */
  158. /* AUTH_SESSION */
  159. tpm_u32(9), /* Authorization size */
  160. tpm_u32(TPM2_RS_PW), /* Session handle */
  161. tpm_u16(0), /* Size of <nonce> */
  162. /* <nonce> (if any) */
  163. 0, /* Attributes: Cont/Excl/Rst */
  164. tpm_u16(0), /* Size of <hmac/password> */
  165. /* <hmac/password> (if any) */
  166. tpm_u16(count), /* Number of bytes */
  167. tpm_u16(0), /* Offset */
  168. };
  169. size_t response_len = COMMAND_BUFFER_SIZE;
  170. u8 response[COMMAND_BUFFER_SIZE];
  171. int ret;
  172. u16 tag;
  173. u32 size, code;
  174. ret = tpm_sendrecv_command(dev, command_v2, response, &response_len);
  175. if (ret)
  176. return log_msg_ret("read", ret);
  177. if (unpack_byte_string(response, response_len, "wdds",
  178. 0, &tag, 2, &size, 6, &code,
  179. 16, data, count))
  180. return TPM_LIB_ERROR;
  181. return 0;
  182. }
  183. u32 tpm2_nv_write_value(struct udevice *dev, u32 index, const void *data,
  184. u32 count)
  185. {
  186. struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
  187. uint offset = 10 + 8 + 4 + 9 + 2;
  188. uint len = offset + count + 2;
  189. /* Use empty password auth if platform hierarchy is disabled */
  190. u32 auth = priv->plat_hier_disabled ? HR_NV_INDEX + index :
  191. TPM2_RH_PLATFORM;
  192. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  193. /* header 10 bytes */
  194. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  195. tpm_u32(len), /* Length */
  196. tpm_u32(TPM2_CC_NV_WRITE), /* Command code */
  197. /* handles 8 bytes */
  198. tpm_u32(auth), /* Primary platform seed */
  199. tpm_u32(HR_NV_INDEX + index), /* Password authorisation */
  200. /* AUTH_SESSION */
  201. tpm_u32(9), /* Authorization size */
  202. tpm_u32(TPM2_RS_PW), /* Session handle */
  203. tpm_u16(0), /* Size of <nonce> */
  204. /* <nonce> (if any) */
  205. 0, /* Attributes: Cont/Excl/Rst */
  206. tpm_u16(0), /* Size of <hmac/password> */
  207. /* <hmac/password> (if any) */
  208. tpm_u16(count),
  209. };
  210. size_t response_len = COMMAND_BUFFER_SIZE;
  211. u8 response[COMMAND_BUFFER_SIZE];
  212. int ret;
  213. ret = pack_byte_string(command_v2, sizeof(command_v2), "sw",
  214. offset, data, count,
  215. offset + count, 0);
  216. if (ret)
  217. return TPM_LIB_ERROR;
  218. return tpm_sendrecv_command(dev, command_v2, response, &response_len);
  219. }
  220. u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz,
  221. void *data, unsigned int *updates)
  222. {
  223. u8 idx_array_sz = max(idx_min_sz, DIV_ROUND_UP(idx, 8));
  224. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  225. tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
  226. tpm_u32(17 + idx_array_sz), /* Length */
  227. tpm_u32(TPM2_CC_PCR_READ), /* Command code */
  228. /* TPML_PCR_SELECTION */
  229. tpm_u32(1), /* Number of selections */
  230. tpm_u16(TPM2_ALG_SHA256), /* Algorithm of the hash */
  231. idx_array_sz, /* Array size for selection */
  232. /* bitmap(idx) Selected PCR bitmap */
  233. };
  234. size_t response_len = COMMAND_BUFFER_SIZE;
  235. u8 response[COMMAND_BUFFER_SIZE];
  236. unsigned int pcr_sel_idx = idx / 8;
  237. u8 pcr_sel_bit = BIT(idx % 8);
  238. unsigned int counter = 0;
  239. int ret;
  240. if (pack_byte_string(command_v2, COMMAND_BUFFER_SIZE, "b",
  241. 17 + pcr_sel_idx, pcr_sel_bit))
  242. return TPM_LIB_ERROR;
  243. ret = tpm_sendrecv_command(dev, command_v2, response, &response_len);
  244. if (ret)
  245. return ret;
  246. if (unpack_byte_string(response, response_len, "ds",
  247. 10, &counter,
  248. response_len - TPM2_DIGEST_LEN, data,
  249. TPM2_DIGEST_LEN))
  250. return TPM_LIB_ERROR;
  251. if (updates)
  252. *updates = counter;
  253. return 0;
  254. }
  255. u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property,
  256. void *buf, size_t prop_count)
  257. {
  258. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  259. tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
  260. tpm_u32(22), /* Length */
  261. tpm_u32(TPM2_CC_GET_CAPABILITY), /* Command code */
  262. tpm_u32(capability), /* Capability */
  263. tpm_u32(property), /* Property */
  264. tpm_u32(prop_count), /* Property count */
  265. };
  266. u8 response[COMMAND_BUFFER_SIZE];
  267. size_t response_len = COMMAND_BUFFER_SIZE;
  268. unsigned int properties_off;
  269. int ret;
  270. ret = tpm_sendrecv_command(dev, command_v2, response, &response_len);
  271. if (ret)
  272. return ret;
  273. /*
  274. * In the response buffer, the properties are located after the:
  275. * tag (u16), response size (u32), response code (u32),
  276. * YES/NO flag (u8), TPM_CAP (u32).
  277. */
  278. properties_off = sizeof(u16) + sizeof(u32) + sizeof(u32) +
  279. sizeof(u8) + sizeof(u32);
  280. memcpy(buf, &response[properties_off], response_len - properties_off);
  281. return 0;
  282. }
  283. u32 tpm2_dam_reset(struct udevice *dev, const char *pw, const ssize_t pw_sz)
  284. {
  285. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  286. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  287. tpm_u32(27 + pw_sz), /* Length */
  288. tpm_u32(TPM2_CC_DAM_RESET), /* Command code */
  289. /* HANDLE */
  290. tpm_u32(TPM2_RH_LOCKOUT), /* TPM resource handle */
  291. /* AUTH_SESSION */
  292. tpm_u32(9 + pw_sz), /* Authorization size */
  293. tpm_u32(TPM2_RS_PW), /* Session handle */
  294. tpm_u16(0), /* Size of <nonce> */
  295. /* <nonce> (if any) */
  296. 0, /* Attributes: Cont/Excl/Rst */
  297. tpm_u16(pw_sz), /* Size of <hmac/password> */
  298. /* STRING(pw) <hmac/password> (if any) */
  299. };
  300. unsigned int offset = 27;
  301. int ret;
  302. /*
  303. * Fill the command structure starting from the first buffer:
  304. * - the password (if any)
  305. */
  306. ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
  307. offset, pw, pw_sz);
  308. offset += pw_sz;
  309. if (ret)
  310. return TPM_LIB_ERROR;
  311. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  312. }
  313. u32 tpm2_dam_parameters(struct udevice *dev, const char *pw,
  314. const ssize_t pw_sz, unsigned int max_tries,
  315. unsigned int recovery_time,
  316. unsigned int lockout_recovery)
  317. {
  318. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  319. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  320. tpm_u32(27 + pw_sz + 12), /* Length */
  321. tpm_u32(TPM2_CC_DAM_PARAMETERS), /* Command code */
  322. /* HANDLE */
  323. tpm_u32(TPM2_RH_LOCKOUT), /* TPM resource handle */
  324. /* AUTH_SESSION */
  325. tpm_u32(9 + pw_sz), /* Authorization size */
  326. tpm_u32(TPM2_RS_PW), /* Session handle */
  327. tpm_u16(0), /* Size of <nonce> */
  328. /* <nonce> (if any) */
  329. 0, /* Attributes: Cont/Excl/Rst */
  330. tpm_u16(pw_sz), /* Size of <hmac/password> */
  331. /* STRING(pw) <hmac/password> (if any) */
  332. /* LOCKOUT PARAMETERS */
  333. /* tpm_u32(max_tries) Max tries (0, always lock) */
  334. /* tpm_u32(recovery_time) Recovery time (0, no lock) */
  335. /* tpm_u32(lockout_recovery) Lockout recovery */
  336. };
  337. unsigned int offset = 27;
  338. int ret;
  339. /*
  340. * Fill the command structure starting from the first buffer:
  341. * - the password (if any)
  342. * - max tries
  343. * - recovery time
  344. * - lockout recovery
  345. */
  346. ret = pack_byte_string(command_v2, sizeof(command_v2), "sddd",
  347. offset, pw, pw_sz,
  348. offset + pw_sz, max_tries,
  349. offset + pw_sz + 4, recovery_time,
  350. offset + pw_sz + 8, lockout_recovery);
  351. offset += pw_sz + 12;
  352. if (ret)
  353. return TPM_LIB_ERROR;
  354. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  355. }
  356. int tpm2_change_auth(struct udevice *dev, u32 handle, const char *newpw,
  357. const ssize_t newpw_sz, const char *oldpw,
  358. const ssize_t oldpw_sz)
  359. {
  360. unsigned int offset = 27;
  361. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  362. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  363. tpm_u32(offset + oldpw_sz + 2 + newpw_sz), /* Length */
  364. tpm_u32(TPM2_CC_HIERCHANGEAUTH), /* Command code */
  365. /* HANDLE */
  366. tpm_u32(handle), /* TPM resource handle */
  367. /* AUTH_SESSION */
  368. tpm_u32(9 + oldpw_sz), /* Authorization size */
  369. tpm_u32(TPM2_RS_PW), /* Session handle */
  370. tpm_u16(0), /* Size of <nonce> */
  371. /* <nonce> (if any) */
  372. 0, /* Attributes: Cont/Excl/Rst */
  373. tpm_u16(oldpw_sz) /* Size of <hmac/password> */
  374. /* STRING(oldpw) <hmac/password> (if any) */
  375. /* TPM2B_AUTH (TPM2B_DIGEST) */
  376. /* tpm_u16(newpw_sz) Digest size, new pw length */
  377. /* STRING(newpw) Digest buffer, new pw */
  378. };
  379. int ret;
  380. /*
  381. * Fill the command structure starting from the first buffer:
  382. * - the old password (if any)
  383. * - size of the new password
  384. * - new password
  385. */
  386. ret = pack_byte_string(command_v2, sizeof(command_v2), "sws",
  387. offset, oldpw, oldpw_sz,
  388. offset + oldpw_sz, newpw_sz,
  389. offset + oldpw_sz + 2, newpw, newpw_sz);
  390. offset += oldpw_sz + 2 + newpw_sz;
  391. if (ret)
  392. return TPM_LIB_ERROR;
  393. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  394. }
  395. u32 tpm2_pcr_setauthpolicy(struct udevice *dev, const char *pw,
  396. const ssize_t pw_sz, u32 index, const char *key)
  397. {
  398. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  399. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  400. tpm_u32(35 + pw_sz + TPM2_DIGEST_LEN), /* Length */
  401. tpm_u32(TPM2_CC_PCR_SETAUTHPOL), /* Command code */
  402. /* HANDLE */
  403. tpm_u32(TPM2_RH_PLATFORM), /* TPM resource handle */
  404. /* AUTH_SESSION */
  405. tpm_u32(9 + pw_sz), /* Authorization size */
  406. tpm_u32(TPM2_RS_PW), /* session handle */
  407. tpm_u16(0), /* Size of <nonce> */
  408. /* <nonce> (if any) */
  409. 0, /* Attributes: Cont/Excl/Rst */
  410. tpm_u16(pw_sz) /* Size of <hmac/password> */
  411. /* STRING(pw) <hmac/password> (if any) */
  412. /* TPM2B_AUTH (TPM2B_DIGEST) */
  413. /* tpm_u16(TPM2_DIGEST_LEN) Digest size length */
  414. /* STRING(key) Digest buffer (PCR key) */
  415. /* TPMI_ALG_HASH */
  416. /* tpm_u16(TPM2_ALG_SHA256) Algorithm of the hash */
  417. /* TPMI_DH_PCR */
  418. /* tpm_u32(index), PCR Index */
  419. };
  420. unsigned int offset = 27;
  421. int ret;
  422. /*
  423. * Fill the command structure starting from the first buffer:
  424. * - the password (if any)
  425. * - the PCR key length
  426. * - the PCR key
  427. * - the hash algorithm
  428. * - the PCR index
  429. */
  430. ret = pack_byte_string(command_v2, sizeof(command_v2), "swswd",
  431. offset, pw, pw_sz,
  432. offset + pw_sz, TPM2_DIGEST_LEN,
  433. offset + pw_sz + 2, key, TPM2_DIGEST_LEN,
  434. offset + pw_sz + 2 + TPM2_DIGEST_LEN,
  435. TPM2_ALG_SHA256,
  436. offset + pw_sz + 4 + TPM2_DIGEST_LEN, index);
  437. offset += pw_sz + 2 + TPM2_DIGEST_LEN + 2 + 4;
  438. if (ret)
  439. return TPM_LIB_ERROR;
  440. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  441. }
  442. u32 tpm2_pcr_setauthvalue(struct udevice *dev, const char *pw,
  443. const ssize_t pw_sz, u32 index, const char *key,
  444. const ssize_t key_sz)
  445. {
  446. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  447. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  448. tpm_u32(33 + pw_sz + TPM2_DIGEST_LEN), /* Length */
  449. tpm_u32(TPM2_CC_PCR_SETAUTHVAL), /* Command code */
  450. /* HANDLE */
  451. tpm_u32(index), /* Handle (PCR Index) */
  452. /* AUTH_SESSION */
  453. tpm_u32(9 + pw_sz), /* Authorization size */
  454. tpm_u32(TPM2_RS_PW), /* session handle */
  455. tpm_u16(0), /* Size of <nonce> */
  456. /* <nonce> (if any) */
  457. 0, /* Attributes: Cont/Excl/Rst */
  458. tpm_u16(pw_sz), /* Size of <hmac/password> */
  459. /* STRING(pw) <hmac/password> (if any) */
  460. /* TPM2B_DIGEST */
  461. /* tpm_u16(key_sz) Key length */
  462. /* STRING(key) Key */
  463. };
  464. unsigned int offset = 27;
  465. int ret;
  466. /*
  467. * Fill the command structure starting from the first buffer:
  468. * - the password (if any)
  469. * - the number of digests, 1 in our case
  470. * - the algorithm, sha256 in our case
  471. * - the digest (64 bytes)
  472. */
  473. ret = pack_byte_string(command_v2, sizeof(command_v2), "sws",
  474. offset, pw, pw_sz,
  475. offset + pw_sz, key_sz,
  476. offset + pw_sz + 2, key, key_sz);
  477. offset += pw_sz + 2 + key_sz;
  478. if (ret)
  479. return TPM_LIB_ERROR;
  480. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  481. }
  482. u32 tpm2_get_random(struct udevice *dev, void *data, u32 count)
  483. {
  484. const u8 command_v2[10] = {
  485. tpm_u16(TPM2_ST_NO_SESSIONS),
  486. tpm_u32(12),
  487. tpm_u32(TPM2_CC_GET_RANDOM),
  488. };
  489. u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
  490. const size_t data_size_offset = 10;
  491. const size_t data_offset = 12;
  492. size_t response_length = sizeof(response);
  493. u32 data_size;
  494. u8 *out = data;
  495. while (count > 0) {
  496. u32 this_bytes = min((size_t)count,
  497. sizeof(response) - data_offset);
  498. u32 err;
  499. if (pack_byte_string(buf, sizeof(buf), "sw",
  500. 0, command_v2, sizeof(command_v2),
  501. sizeof(command_v2), this_bytes))
  502. return TPM_LIB_ERROR;
  503. err = tpm_sendrecv_command(dev, buf, response,
  504. &response_length);
  505. if (err)
  506. return err;
  507. if (unpack_byte_string(response, response_length, "w",
  508. data_size_offset, &data_size))
  509. return TPM_LIB_ERROR;
  510. if (data_size > this_bytes)
  511. return TPM_LIB_ERROR;
  512. if (unpack_byte_string(response, response_length, "s",
  513. data_offset, out, data_size))
  514. return TPM_LIB_ERROR;
  515. count -= data_size;
  516. out += data_size;
  517. }
  518. return 0;
  519. }
  520. u32 tpm2_write_lock(struct udevice *dev, u32 index)
  521. {
  522. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  523. /* header 10 bytes */
  524. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  525. tpm_u32(10 + 8 + 13), /* Length */
  526. tpm_u32(TPM2_CC_NV_WRITELOCK), /* Command code */
  527. /* handles 8 bytes */
  528. tpm_u32(TPM2_RH_PLATFORM), /* Primary platform seed */
  529. tpm_u32(HR_NV_INDEX + index), /* Password authorisation */
  530. /* session header 9 bytes */
  531. tpm_u32(9), /* Header size */
  532. tpm_u32(TPM2_RS_PW), /* Password authorisation */
  533. tpm_u16(0), /* nonce_size */
  534. 0, /* session_attrs */
  535. tpm_u16(0), /* auth_size */
  536. };
  537. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  538. }
  539. u32 tpm2_disable_platform_hierarchy(struct udevice *dev)
  540. {
  541. struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
  542. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  543. /* header 10 bytes */
  544. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  545. tpm_u32(10 + 4 + 13 + 5), /* Length */
  546. tpm_u32(TPM2_CC_HIER_CONTROL), /* Command code */
  547. /* 4 bytes */
  548. tpm_u32(TPM2_RH_PLATFORM), /* Primary platform seed */
  549. /* session header 9 bytes */
  550. tpm_u32(9), /* Header size */
  551. tpm_u32(TPM2_RS_PW), /* Password authorisation */
  552. tpm_u16(0), /* nonce_size */
  553. 0, /* session_attrs */
  554. tpm_u16(0), /* auth_size */
  555. /* payload 5 bytes */
  556. tpm_u32(TPM2_RH_PLATFORM), /* Hierarchy to disable */
  557. 0, /* 0=disable */
  558. };
  559. int ret;
  560. ret = tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  561. log_info("ret=%s, %x\n", dev->name, ret);
  562. if (ret)
  563. return ret;
  564. priv->plat_hier_disabled = true;
  565. return 0;
  566. }