tpm2_tis_sandbox.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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-v2.h>
  9. #include <asm/state.h>
  10. #include <asm/unaligned.h>
  11. #include <linux/bitops.h>
  12. #include <u-boot/crc.h>
  13. #include "sandbox_common.h"
  14. /* Hierarchies */
  15. enum tpm2_hierarchy {
  16. TPM2_HIERARCHY_LOCKOUT = 0,
  17. TPM2_HIERARCHY_ENDORSEMENT,
  18. TPM2_HIERARCHY_PLATFORM,
  19. TPM2_HIERARCHY_NB,
  20. };
  21. /* Subset of supported capabilities */
  22. enum tpm2_capability {
  23. TPM_CAP_TPM_PROPERTIES = 0x6,
  24. };
  25. /* Subset of supported properties */
  26. #define TPM2_PROPERTIES_OFFSET 0x0000020E
  27. enum tpm2_cap_tpm_property {
  28. TPM2_FAIL_COUNTER = 0,
  29. TPM2_PROP_MAX_TRIES,
  30. TPM2_RECOVERY_TIME,
  31. TPM2_LOCKOUT_RECOVERY,
  32. TPM2_PROPERTY_NB,
  33. };
  34. #define SANDBOX_TPM_PCR_NB 1
  35. static const u8 sandbox_extended_once_pcr[] = {
  36. 0xf5, 0xa5, 0xfd, 0x42, 0xd1, 0x6a, 0x20, 0x30,
  37. 0x27, 0x98, 0xef, 0x6e, 0xd3, 0x09, 0x97, 0x9b,
  38. 0x43, 0x00, 0x3d, 0x23, 0x20, 0xd9, 0xf0, 0xe8,
  39. 0xea, 0x98, 0x31, 0xa9, 0x27, 0x59, 0xfb, 0x4b,
  40. };
  41. /*
  42. * Information about our TPM emulation. This is preserved in the sandbox
  43. * state file if enabled.
  44. *
  45. * @valid: true if this is valid (only used in s_state)
  46. * @init_done: true if open() has been called
  47. * @startup_done: true if TPM2_CC_STARTUP has been processed
  48. * @tests_done: true if TPM2_CC_SELF_TEST has be processed
  49. * @pw: TPM password per hierarchy
  50. * @pw_sz: Size of each password in bytes
  51. * @properties: TPM properties
  52. * @pcr: TPM Platform Configuration Registers. Each of these holds a hash and
  53. * can be 'extended' a number of times, meaning another hash is added into
  54. * its value (initial value all zeroes)
  55. * @pcr_extensions: Number of times each PCR has been extended (starts at 0)
  56. * @nvdata: non-volatile data, used to store important things for the platform
  57. */
  58. struct sandbox_tpm2 {
  59. bool valid;
  60. /* TPM internal states */
  61. bool init_done;
  62. bool startup_done;
  63. bool tests_done;
  64. char pw[TPM2_HIERARCHY_NB][TPM2_DIGEST_LEN + 1];
  65. int pw_sz[TPM2_HIERARCHY_NB];
  66. u32 properties[TPM2_PROPERTY_NB];
  67. u8 pcr[SANDBOX_TPM_PCR_NB][TPM2_DIGEST_LEN];
  68. u32 pcr_extensions[SANDBOX_TPM_PCR_NB];
  69. struct nvdata_state nvdata[NV_SEQ_COUNT];
  70. };
  71. static struct sandbox_tpm2 s_state, *g_state;
  72. /*
  73. * Check the tag validity depending on the command (authentication required or
  74. * not). If authentication is required, check it is valid. Update the auth
  75. * pointer to point to the next chunk of data to process if needed.
  76. */
  77. static int sandbox_tpm2_check_session(struct udevice *dev, u32 command, u16 tag,
  78. const u8 **auth,
  79. enum tpm2_hierarchy *hierarchy)
  80. {
  81. struct sandbox_tpm2 *tpm = dev_get_priv(dev);
  82. u32 handle, auth_sz, session_handle;
  83. u16 nonce_sz, pw_sz;
  84. const char *pw;
  85. switch (command) {
  86. case TPM2_CC_STARTUP:
  87. case TPM2_CC_SELF_TEST:
  88. case TPM2_CC_GET_CAPABILITY:
  89. case TPM2_CC_PCR_READ:
  90. if (tag != TPM2_ST_NO_SESSIONS) {
  91. printf("No session required for command 0x%x\n",
  92. command);
  93. return TPM2_RC_BAD_TAG;
  94. }
  95. return 0;
  96. case TPM2_CC_CLEAR:
  97. case TPM2_CC_HIERCHANGEAUTH:
  98. case TPM2_CC_DAM_RESET:
  99. case TPM2_CC_DAM_PARAMETERS:
  100. case TPM2_CC_PCR_EXTEND:
  101. case TPM2_CC_NV_READ:
  102. case TPM2_CC_NV_WRITE:
  103. case TPM2_CC_NV_WRITELOCK:
  104. case TPM2_CC_NV_DEFINE_SPACE:
  105. if (tag != TPM2_ST_SESSIONS) {
  106. printf("Session required for command 0x%x\n", command);
  107. return TPM2_RC_AUTH_CONTEXT;
  108. }
  109. handle = get_unaligned_be32(*auth);
  110. *auth += sizeof(handle);
  111. /*
  112. * PCR_Extend had a different protection mechanism and does not
  113. * use the same standards as other commands.
  114. */
  115. if (command == TPM2_CC_PCR_EXTEND)
  116. break;
  117. switch (handle) {
  118. case TPM2_RH_LOCKOUT:
  119. *hierarchy = TPM2_HIERARCHY_LOCKOUT;
  120. break;
  121. case TPM2_RH_ENDORSEMENT:
  122. if (command == TPM2_CC_CLEAR) {
  123. printf("Endorsement hierarchy unsupported\n");
  124. return TPM2_RC_AUTH_MISSING;
  125. }
  126. *hierarchy = TPM2_HIERARCHY_ENDORSEMENT;
  127. break;
  128. case TPM2_RH_PLATFORM:
  129. *hierarchy = TPM2_HIERARCHY_PLATFORM;
  130. if (command == TPM2_CC_NV_READ ||
  131. command == TPM2_CC_NV_WRITE ||
  132. command == TPM2_CC_NV_WRITELOCK)
  133. *auth += sizeof(u32);
  134. break;
  135. default:
  136. printf("Wrong handle 0x%x\n", handle);
  137. return TPM2_RC_VALUE;
  138. }
  139. break;
  140. default:
  141. printf("Command code not recognized: 0x%x\n", command);
  142. return TPM2_RC_COMMAND_CODE;
  143. }
  144. auth_sz = get_unaligned_be32(*auth);
  145. *auth += sizeof(auth_sz);
  146. session_handle = get_unaligned_be32(*auth);
  147. *auth += sizeof(session_handle);
  148. if (session_handle != TPM2_RS_PW) {
  149. printf("Wrong session handle 0x%x\n", session_handle);
  150. return TPM2_RC_VALUE;
  151. }
  152. nonce_sz = get_unaligned_be16(*auth);
  153. *auth += sizeof(nonce_sz);
  154. if (nonce_sz) {
  155. printf("Nonces not supported in Sandbox, aborting\n");
  156. return TPM2_RC_HANDLE;
  157. }
  158. /* Ignore attributes */
  159. *auth += sizeof(u8);
  160. pw_sz = get_unaligned_be16(*auth);
  161. *auth += sizeof(pw_sz);
  162. if (auth_sz != (9 + nonce_sz + pw_sz)) {
  163. printf("Authentication size (%d) do not match %d\n",
  164. auth_sz, 9 + nonce_sz + pw_sz);
  165. return TPM2_RC_SIZE;
  166. }
  167. /* No passwork is acceptable */
  168. if (!pw_sz && !tpm->pw_sz[*hierarchy])
  169. return TPM2_RC_SUCCESS;
  170. /* Password is too long */
  171. if (pw_sz > TPM2_DIGEST_LEN) {
  172. printf("Password should not be more than %dB\n",
  173. TPM2_DIGEST_LEN);
  174. return TPM2_RC_AUTHSIZE;
  175. }
  176. pw = (const char *)*auth;
  177. *auth += pw_sz;
  178. /* Password is wrong */
  179. if (pw_sz != tpm->pw_sz[*hierarchy] ||
  180. strncmp(pw, tpm->pw[*hierarchy], tpm->pw_sz[*hierarchy])) {
  181. printf("Authentication failed: wrong password.\n");
  182. return TPM2_RC_BAD_AUTH;
  183. }
  184. return TPM2_RC_SUCCESS;
  185. }
  186. static int sandbox_tpm2_check_readyness(struct udevice *dev, int command)
  187. {
  188. struct sandbox_tpm2 *tpm = dev_get_priv(dev);
  189. switch (command) {
  190. case TPM2_CC_STARTUP:
  191. if (!tpm->init_done || tpm->startup_done)
  192. return TPM2_RC_INITIALIZE;
  193. break;
  194. case TPM2_CC_GET_CAPABILITY:
  195. if (!tpm->init_done || !tpm->startup_done)
  196. return TPM2_RC_INITIALIZE;
  197. break;
  198. case TPM2_CC_SELF_TEST:
  199. if (!tpm->startup_done)
  200. return TPM2_RC_INITIALIZE;
  201. break;
  202. default:
  203. if (!tpm->tests_done)
  204. return TPM2_RC_NEEDS_TEST;
  205. break;
  206. }
  207. return 0;
  208. }
  209. static int sandbox_tpm2_fill_buf(u8 *recv, size_t *recv_len, u16 tag, u32 rc)
  210. {
  211. *recv_len = sizeof(tag) + sizeof(u32) + sizeof(rc);
  212. /* Write tag */
  213. put_unaligned_be16(tag, recv);
  214. recv += sizeof(tag);
  215. /* Write length */
  216. put_unaligned_be32(*recv_len, recv);
  217. recv += sizeof(u32);
  218. /* Write return code */
  219. put_unaligned_be32(rc, recv);
  220. recv += sizeof(rc);
  221. /* Add trailing \0 */
  222. *recv = '\0';
  223. return 0;
  224. }
  225. static int sandbox_tpm2_extend(struct udevice *dev, int pcr_index,
  226. const u8 *extension)
  227. {
  228. struct sandbox_tpm2 *tpm = dev_get_priv(dev);
  229. int i;
  230. /* Only simulate the first extensions from all '0' with only '0' */
  231. for (i = 0; i < TPM2_DIGEST_LEN; i++)
  232. if (tpm->pcr[pcr_index][i] || extension[i])
  233. return TPM2_RC_FAILURE;
  234. memcpy(tpm->pcr[pcr_index], sandbox_extended_once_pcr,
  235. TPM2_DIGEST_LEN);
  236. tpm->pcr_extensions[pcr_index]++;
  237. return 0;
  238. };
  239. static int sandbox_tpm2_xfer(struct udevice *dev, const u8 *sendbuf,
  240. size_t send_size, u8 *recvbuf,
  241. size_t *recv_len)
  242. {
  243. struct sandbox_tpm2 *tpm = dev_get_priv(dev);
  244. enum tpm2_hierarchy hierarchy = 0;
  245. const u8 *sent = sendbuf;
  246. u8 *recv = recvbuf;
  247. u32 length, command, rc = 0;
  248. u16 tag, mode, new_pw_sz;
  249. u8 yes_no;
  250. int i, j;
  251. /* TPM2_GetProperty */
  252. u32 capability, property, property_count;
  253. /* TPM2_PCR_Read/Extend variables */
  254. int pcr_index = 0;
  255. u64 pcr_map = 0;
  256. u32 selections, pcr_nb;
  257. u16 alg;
  258. u8 pcr_array_sz;
  259. tag = get_unaligned_be16(sent);
  260. sent += sizeof(tag);
  261. length = get_unaligned_be32(sent);
  262. sent += sizeof(length);
  263. if (length != send_size) {
  264. printf("TPM2: Unmatching length, received: %zd, expected: %d\n",
  265. send_size, length);
  266. rc = TPM2_RC_SIZE;
  267. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  268. return 0;
  269. }
  270. command = get_unaligned_be32(sent);
  271. sent += sizeof(command);
  272. rc = sandbox_tpm2_check_readyness(dev, command);
  273. if (rc) {
  274. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  275. return 0;
  276. }
  277. rc = sandbox_tpm2_check_session(dev, command, tag, &sent, &hierarchy);
  278. if (rc) {
  279. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  280. return 0;
  281. }
  282. switch (command) {
  283. case TPM2_CC_STARTUP:
  284. mode = get_unaligned_be16(sent);
  285. sent += sizeof(mode);
  286. switch (mode) {
  287. case TPM2_SU_CLEAR:
  288. case TPM2_SU_STATE:
  289. break;
  290. default:
  291. rc = TPM2_RC_VALUE;
  292. }
  293. tpm->startup_done = true;
  294. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  295. break;
  296. case TPM2_CC_SELF_TEST:
  297. yes_no = *sent;
  298. sent += sizeof(yes_no);
  299. switch (yes_no) {
  300. case TPMI_YES:
  301. case TPMI_NO:
  302. break;
  303. default:
  304. rc = TPM2_RC_VALUE;
  305. }
  306. tpm->tests_done = true;
  307. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  308. break;
  309. case TPM2_CC_CLEAR:
  310. /* Reset this hierarchy password */
  311. tpm->pw_sz[hierarchy] = 0;
  312. /* Reset all password if thisis the PLATFORM hierarchy */
  313. if (hierarchy == TPM2_HIERARCHY_PLATFORM)
  314. for (i = 0; i < TPM2_HIERARCHY_NB; i++)
  315. tpm->pw_sz[i] = 0;
  316. /* Reset the properties */
  317. for (i = 0; i < TPM2_PROPERTY_NB; i++)
  318. tpm->properties[i] = 0;
  319. /* Reset the PCRs and their number of extensions */
  320. for (i = 0; i < SANDBOX_TPM_PCR_NB; i++) {
  321. tpm->pcr_extensions[i] = 0;
  322. for (j = 0; j < TPM2_DIGEST_LEN; j++)
  323. tpm->pcr[i][j] = 0;
  324. }
  325. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  326. break;
  327. case TPM2_CC_HIERCHANGEAUTH:
  328. new_pw_sz = get_unaligned_be16(sent);
  329. sent += sizeof(new_pw_sz);
  330. if (new_pw_sz > TPM2_DIGEST_LEN) {
  331. rc = TPM2_RC_SIZE;
  332. } else if (new_pw_sz) {
  333. tpm->pw_sz[hierarchy] = new_pw_sz;
  334. memcpy(tpm->pw[hierarchy], sent, new_pw_sz);
  335. sent += new_pw_sz;
  336. }
  337. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  338. break;
  339. case TPM2_CC_GET_CAPABILITY:
  340. capability = get_unaligned_be32(sent);
  341. sent += sizeof(capability);
  342. if (capability != TPM_CAP_TPM_PROPERTIES) {
  343. printf("Sandbox TPM only support TPM_CAPABILITIES\n");
  344. return TPM2_RC_HANDLE;
  345. }
  346. property = get_unaligned_be32(sent);
  347. sent += sizeof(property);
  348. property -= TPM2_PROPERTIES_OFFSET;
  349. property_count = get_unaligned_be32(sent);
  350. sent += sizeof(property_count);
  351. if (!property_count ||
  352. property + property_count > TPM2_PROPERTY_NB) {
  353. rc = TPM2_RC_HANDLE;
  354. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  355. }
  356. /* Write tag */
  357. put_unaligned_be16(tag, recv);
  358. recv += sizeof(tag);
  359. /* Ignore length for now */
  360. recv += sizeof(u32);
  361. /* Write return code */
  362. put_unaligned_be32(rc, recv);
  363. recv += sizeof(rc);
  364. /* Tell there is more data to read */
  365. *recv = TPMI_YES;
  366. recv += sizeof(yes_no);
  367. /* Repeat the capability */
  368. put_unaligned_be32(capability, recv);
  369. recv += sizeof(capability);
  370. /* Give the number of properties that follow */
  371. put_unaligned_be32(property_count, recv);
  372. recv += sizeof(property_count);
  373. /* Fill with the properties */
  374. for (i = 0; i < property_count; i++) {
  375. put_unaligned_be32(TPM2_PROPERTIES_OFFSET + property +
  376. i, recv);
  377. recv += sizeof(property);
  378. put_unaligned_be32(tpm->properties[property + i],
  379. recv);
  380. recv += sizeof(property);
  381. }
  382. /* Add trailing \0 */
  383. *recv = '\0';
  384. /* Write response length */
  385. *recv_len = recv - recvbuf;
  386. put_unaligned_be32(*recv_len, recvbuf + sizeof(tag));
  387. break;
  388. case TPM2_CC_DAM_PARAMETERS:
  389. tpm->properties[TPM2_PROP_MAX_TRIES] = get_unaligned_be32(sent);
  390. sent += sizeof(*tpm->properties);
  391. tpm->properties[TPM2_RECOVERY_TIME] = get_unaligned_be32(sent);
  392. sent += sizeof(*tpm->properties);
  393. tpm->properties[TPM2_LOCKOUT_RECOVERY] = get_unaligned_be32(sent);
  394. sent += sizeof(*tpm->properties);
  395. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  396. break;
  397. case TPM2_CC_PCR_READ:
  398. selections = get_unaligned_be32(sent);
  399. sent += sizeof(selections);
  400. if (selections != 1) {
  401. printf("Sandbox cannot handle more than one PCR\n");
  402. rc = TPM2_RC_VALUE;
  403. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  404. }
  405. alg = get_unaligned_be16(sent);
  406. sent += sizeof(alg);
  407. if (alg != TPM2_ALG_SHA256) {
  408. printf("Sandbox TPM only handle SHA256 algorithm\n");
  409. rc = TPM2_RC_VALUE;
  410. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  411. }
  412. pcr_array_sz = *sent;
  413. sent += sizeof(pcr_array_sz);
  414. if (!pcr_array_sz || pcr_array_sz > 8) {
  415. printf("Sandbox TPM cannot handle so much PCRs\n");
  416. rc = TPM2_RC_VALUE;
  417. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  418. }
  419. for (i = 0; i < pcr_array_sz; i++)
  420. pcr_map += (u64)sent[i] << (i * 8);
  421. if (pcr_map >> SANDBOX_TPM_PCR_NB) {
  422. printf("Sandbox TPM handles up to %d PCR(s)\n",
  423. SANDBOX_TPM_PCR_NB);
  424. rc = TPM2_RC_VALUE;
  425. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  426. }
  427. if (!pcr_map) {
  428. printf("Empty PCR map.\n");
  429. rc = TPM2_RC_VALUE;
  430. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  431. }
  432. for (i = 0; i < SANDBOX_TPM_PCR_NB; i++)
  433. if (pcr_map & BIT(i))
  434. pcr_index = i;
  435. /* Write tag */
  436. put_unaligned_be16(tag, recv);
  437. recv += sizeof(tag);
  438. /* Ignore length for now */
  439. recv += sizeof(u32);
  440. /* Write return code */
  441. put_unaligned_be32(rc, recv);
  442. recv += sizeof(rc);
  443. /* Number of extensions */
  444. put_unaligned_be32(tpm->pcr_extensions[pcr_index], recv);
  445. recv += sizeof(u32);
  446. /* Copy the PCR */
  447. memcpy(recv, tpm->pcr[pcr_index], TPM2_DIGEST_LEN);
  448. recv += TPM2_DIGEST_LEN;
  449. /* Add trailing \0 */
  450. *recv = '\0';
  451. /* Write response length */
  452. *recv_len = recv - recvbuf;
  453. put_unaligned_be32(*recv_len, recvbuf + sizeof(tag));
  454. break;
  455. case TPM2_CC_PCR_EXTEND:
  456. /* Get the PCR index */
  457. pcr_index = get_unaligned_be32(sendbuf + sizeof(tag) +
  458. sizeof(length) +
  459. sizeof(command));
  460. if (pcr_index > SANDBOX_TPM_PCR_NB) {
  461. printf("Sandbox TPM handles up to %d PCR(s)\n",
  462. SANDBOX_TPM_PCR_NB);
  463. rc = TPM2_RC_VALUE;
  464. }
  465. /* Check the number of hashes */
  466. pcr_nb = get_unaligned_be32(sent);
  467. sent += sizeof(pcr_nb);
  468. if (pcr_nb != 1) {
  469. printf("Sandbox cannot handle more than one PCR\n");
  470. rc = TPM2_RC_VALUE;
  471. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  472. }
  473. /* Check the hash algorithm */
  474. alg = get_unaligned_be16(sent);
  475. sent += sizeof(alg);
  476. if (alg != TPM2_ALG_SHA256) {
  477. printf("Sandbox TPM only handle SHA256 algorithm\n");
  478. rc = TPM2_RC_VALUE;
  479. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  480. }
  481. /* Extend the PCR */
  482. rc = sandbox_tpm2_extend(dev, pcr_index, sent);
  483. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  484. break;
  485. case TPM2_CC_NV_READ: {
  486. int index, seq;
  487. index = get_unaligned_be32(sendbuf + TPM2_HDR_LEN + 4);
  488. length = get_unaligned_be16(sent);
  489. /* ignore offset */
  490. seq = sb_tpm_index_to_seq(index);
  491. if (seq < 0)
  492. return log_msg_ret("index", -EINVAL);
  493. printf("tpm: nvread index=%#02x, len=%#02x, seq=%#02x\n", index,
  494. length, seq);
  495. *recv_len = TPM2_HDR_LEN + 6 + length;
  496. memset(recvbuf, '\0', *recv_len);
  497. put_unaligned_be32(length, recvbuf + 2);
  498. sb_tpm_read_data(tpm->nvdata, seq, recvbuf,
  499. TPM2_HDR_LEN + 4 + 2, length);
  500. break;
  501. }
  502. case TPM2_CC_NV_WRITE: {
  503. int index, seq;
  504. index = get_unaligned_be32(sendbuf + TPM2_HDR_LEN + 4);
  505. length = get_unaligned_be16(sent);
  506. sent += sizeof(u16);
  507. /* ignore offset */
  508. seq = sb_tpm_index_to_seq(index);
  509. if (seq < 0)
  510. return log_msg_ret("index", -EINVAL);
  511. printf("tpm: nvwrite index=%#02x, len=%#02x, seq=%#02x\n", index,
  512. length, seq);
  513. memcpy(&tpm->nvdata[seq].data, sent, length);
  514. tpm->nvdata[seq].present = true;
  515. *recv_len = TPM2_HDR_LEN + 2;
  516. memset(recvbuf, '\0', *recv_len);
  517. break;
  518. }
  519. case TPM2_CC_NV_DEFINE_SPACE: {
  520. int policy_size, index, seq;
  521. policy_size = get_unaligned_be16(sent + 12);
  522. index = get_unaligned_be32(sent + 2);
  523. sent += 14 + policy_size;
  524. length = get_unaligned_be16(sent);
  525. seq = sb_tpm_index_to_seq(index);
  526. if (seq < 0)
  527. return -EINVAL;
  528. printf("tpm: define_space index=%x, len=%x, seq=%x, policy_size=%x\n",
  529. index, length, seq, policy_size);
  530. sb_tpm_define_data(tpm->nvdata, seq, length);
  531. *recv_len = 12;
  532. memset(recvbuf, '\0', *recv_len);
  533. break;
  534. }
  535. case TPM2_CC_NV_WRITELOCK:
  536. *recv_len = 12;
  537. memset(recvbuf, '\0', *recv_len);
  538. break;
  539. default:
  540. printf("TPM2 command %02x unknown in Sandbox\n", command);
  541. rc = TPM2_RC_COMMAND_CODE;
  542. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  543. }
  544. return 0;
  545. }
  546. static int sandbox_tpm2_get_desc(struct udevice *dev, char *buf, int size)
  547. {
  548. if (size < 15)
  549. return -ENOSPC;
  550. return snprintf(buf, size, "Sandbox TPM2.x");
  551. }
  552. static int sandbox_tpm2_open(struct udevice *dev)
  553. {
  554. struct sandbox_tpm2 *tpm = dev_get_priv(dev);
  555. if (tpm->init_done)
  556. return -EIO;
  557. tpm->init_done = true;
  558. return 0;
  559. }
  560. static int sandbox_tpm2_probe(struct udevice *dev)
  561. {
  562. struct sandbox_tpm2 *tpm = dev_get_priv(dev);
  563. struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
  564. /* Use the TPM v2 stack */
  565. priv->version = TPM_V2;
  566. priv->pcr_count = 32;
  567. priv->pcr_select_min = 2;
  568. if (s_state.valid)
  569. memcpy(tpm, &s_state, sizeof(*tpm));
  570. g_state = tpm;
  571. return 0;
  572. }
  573. static int sandbox_tpm2_close(struct udevice *dev)
  574. {
  575. return 0;
  576. }
  577. static const struct tpm_ops sandbox_tpm2_ops = {
  578. .open = sandbox_tpm2_open,
  579. .close = sandbox_tpm2_close,
  580. .get_desc = sandbox_tpm2_get_desc,
  581. .xfer = sandbox_tpm2_xfer,
  582. };
  583. static const struct udevice_id sandbox_tpm2_ids[] = {
  584. { .compatible = "sandbox,tpm2" },
  585. { }
  586. };
  587. U_BOOT_DRIVER(sandbox_tpm2) = {
  588. .name = "sandbox_tpm2",
  589. .id = UCLASS_TPM,
  590. .of_match = sandbox_tpm2_ids,
  591. .ops = &sandbox_tpm2_ops,
  592. .probe = sandbox_tpm2_probe,
  593. .priv_auto = sizeof(struct sandbox_tpm2),
  594. };