tpm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. * Copyright (c) 2013 The Chromium OS Authors.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <dm.h>
  9. #include <malloc.h>
  10. #include <tpm.h>
  11. #include <asm/unaligned.h>
  12. #include <linux/string.h>
  13. /* Useful constants */
  14. enum {
  15. DIGEST_LENGTH = 20,
  16. /* max lengths, valid for RSA keys <= 2048 bits */
  17. TPM_PUBKEY_MAX_LENGTH = 288,
  18. };
  19. /**
  20. * Print a byte string in hexdecimal format, 16-bytes per line.
  21. *
  22. * @param data byte string to be printed
  23. * @param count number of bytes to be printed
  24. */
  25. static void print_byte_string(uint8_t *data, size_t count)
  26. {
  27. int i, print_newline = 0;
  28. for (i = 0; i < count; i++) {
  29. printf(" %02x", data[i]);
  30. print_newline = (i % 16 == 15);
  31. if (print_newline)
  32. putc('\n');
  33. }
  34. /* Avoid duplicated newline at the end */
  35. if (!print_newline)
  36. putc('\n');
  37. }
  38. /**
  39. * Convert a text string of hexdecimal values into a byte string.
  40. *
  41. * @param bytes text string of hexdecimal values with no space
  42. * between them
  43. * @param data output buffer for byte string. The caller has to make
  44. * sure it is large enough for storing the output. If
  45. * NULL is passed, a large enough buffer will be allocated,
  46. * and the caller must free it.
  47. * @param count_ptr output variable for the length of byte string
  48. * @return pointer to output buffer
  49. */
  50. static void *parse_byte_string(char *bytes, uint8_t *data, size_t *count_ptr)
  51. {
  52. char byte[3];
  53. size_t count, length;
  54. int i;
  55. if (!bytes)
  56. return NULL;
  57. length = strlen(bytes);
  58. count = length / 2;
  59. if (!data)
  60. data = malloc(count);
  61. if (!data)
  62. return NULL;
  63. byte[2] = '\0';
  64. for (i = 0; i < length; i += 2) {
  65. byte[0] = bytes[i];
  66. byte[1] = bytes[i + 1];
  67. data[i / 2] = (uint8_t)simple_strtoul(byte, NULL, 16);
  68. }
  69. if (count_ptr)
  70. *count_ptr = count;
  71. return data;
  72. }
  73. /**
  74. * report_return_code() - Report any error and return failure or success
  75. *
  76. * @param return_code TPM command return code
  77. * @return value of enum command_ret_t
  78. */
  79. static int report_return_code(int return_code)
  80. {
  81. if (return_code) {
  82. printf("Error: %d\n", return_code);
  83. return CMD_RET_FAILURE;
  84. } else {
  85. return CMD_RET_SUCCESS;
  86. }
  87. }
  88. /**
  89. * Return number of values defined by a type string.
  90. *
  91. * @param type_str type string
  92. * @return number of values of type string
  93. */
  94. static int type_string_get_num_values(const char *type_str)
  95. {
  96. return strlen(type_str);
  97. }
  98. /**
  99. * Return total size of values defined by a type string.
  100. *
  101. * @param type_str type string
  102. * @return total size of values of type string, or 0 if type string
  103. * contains illegal type character.
  104. */
  105. static size_t type_string_get_space_size(const char *type_str)
  106. {
  107. size_t size;
  108. for (size = 0; *type_str; type_str++) {
  109. switch (*type_str) {
  110. case 'b':
  111. size += 1;
  112. break;
  113. case 'w':
  114. size += 2;
  115. break;
  116. case 'd':
  117. size += 4;
  118. break;
  119. default:
  120. return 0;
  121. }
  122. }
  123. return size;
  124. }
  125. /**
  126. * Allocate a buffer large enough to hold values defined by a type
  127. * string. The caller has to free the buffer.
  128. *
  129. * @param type_str type string
  130. * @param count pointer for storing size of buffer
  131. * @return pointer to buffer or NULL on error
  132. */
  133. static void *type_string_alloc(const char *type_str, uint32_t *count)
  134. {
  135. void *data;
  136. size_t size;
  137. size = type_string_get_space_size(type_str);
  138. if (!size)
  139. return NULL;
  140. data = malloc(size);
  141. if (data)
  142. *count = size;
  143. return data;
  144. }
  145. /**
  146. * Pack values defined by a type string into a buffer. The buffer must have
  147. * large enough space.
  148. *
  149. * @param type_str type string
  150. * @param values text strings of values to be packed
  151. * @param data output buffer of values
  152. * @return 0 on success, non-0 on error
  153. */
  154. static int type_string_pack(const char *type_str, char * const values[],
  155. uint8_t *data)
  156. {
  157. size_t offset;
  158. uint32_t value;
  159. for (offset = 0; *type_str; type_str++, values++) {
  160. value = simple_strtoul(values[0], NULL, 0);
  161. switch (*type_str) {
  162. case 'b':
  163. data[offset] = value;
  164. offset += 1;
  165. break;
  166. case 'w':
  167. put_unaligned_be16(value, data + offset);
  168. offset += 2;
  169. break;
  170. case 'd':
  171. put_unaligned_be32(value, data + offset);
  172. offset += 4;
  173. break;
  174. default:
  175. return -1;
  176. }
  177. }
  178. return 0;
  179. }
  180. /**
  181. * Read values defined by a type string from a buffer, and write these values
  182. * to environment variables.
  183. *
  184. * @param type_str type string
  185. * @param data input buffer of values
  186. * @param vars names of environment variables
  187. * @return 0 on success, non-0 on error
  188. */
  189. static int type_string_write_vars(const char *type_str, uint8_t *data,
  190. char * const vars[])
  191. {
  192. size_t offset;
  193. uint32_t value;
  194. for (offset = 0; *type_str; type_str++, vars++) {
  195. switch (*type_str) {
  196. case 'b':
  197. value = data[offset];
  198. offset += 1;
  199. break;
  200. case 'w':
  201. value = get_unaligned_be16(data + offset);
  202. offset += 2;
  203. break;
  204. case 'd':
  205. value = get_unaligned_be32(data + offset);
  206. offset += 4;
  207. break;
  208. default:
  209. return -1;
  210. }
  211. if (setenv_ulong(*vars, value))
  212. return -1;
  213. }
  214. return 0;
  215. }
  216. static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag,
  217. int argc, char * const argv[])
  218. {
  219. enum tpm_startup_type mode;
  220. if (argc != 2)
  221. return CMD_RET_USAGE;
  222. if (!strcasecmp("TPM_ST_CLEAR", argv[1])) {
  223. mode = TPM_ST_CLEAR;
  224. } else if (!strcasecmp("TPM_ST_STATE", argv[1])) {
  225. mode = TPM_ST_STATE;
  226. } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) {
  227. mode = TPM_ST_DEACTIVATED;
  228. } else {
  229. printf("Couldn't recognize mode string: %s\n", argv[1]);
  230. return CMD_RET_FAILURE;
  231. }
  232. return report_return_code(tpm_startup(mode));
  233. }
  234. static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag,
  235. int argc, char * const argv[])
  236. {
  237. uint32_t index, perm, size;
  238. if (argc != 4)
  239. return CMD_RET_USAGE;
  240. index = simple_strtoul(argv[1], NULL, 0);
  241. perm = simple_strtoul(argv[2], NULL, 0);
  242. size = simple_strtoul(argv[3], NULL, 0);
  243. return report_return_code(tpm_nv_define_space(index, perm, size));
  244. }
  245. static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag,
  246. int argc, char * const argv[])
  247. {
  248. uint32_t index, count, rc;
  249. void *data;
  250. if (argc != 4)
  251. return CMD_RET_USAGE;
  252. index = simple_strtoul(argv[1], NULL, 0);
  253. data = (void *)simple_strtoul(argv[2], NULL, 0);
  254. count = simple_strtoul(argv[3], NULL, 0);
  255. rc = tpm_nv_read_value(index, data, count);
  256. if (!rc) {
  257. puts("area content:\n");
  258. print_byte_string(data, count);
  259. }
  260. return report_return_code(rc);
  261. }
  262. static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag,
  263. int argc, char * const argv[])
  264. {
  265. uint32_t index, rc;
  266. size_t count;
  267. void *data;
  268. if (argc != 3)
  269. return CMD_RET_USAGE;
  270. index = simple_strtoul(argv[1], NULL, 0);
  271. data = parse_byte_string(argv[2], NULL, &count);
  272. if (!data) {
  273. printf("Couldn't parse byte string %s\n", argv[2]);
  274. return CMD_RET_FAILURE;
  275. }
  276. rc = tpm_nv_write_value(index, data, count);
  277. free(data);
  278. return report_return_code(rc);
  279. }
  280. static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag,
  281. int argc, char * const argv[])
  282. {
  283. uint32_t index, rc;
  284. uint8_t in_digest[20], out_digest[20];
  285. if (argc != 3)
  286. return CMD_RET_USAGE;
  287. index = simple_strtoul(argv[1], NULL, 0);
  288. if (!parse_byte_string(argv[2], in_digest, NULL)) {
  289. printf("Couldn't parse byte string %s\n", argv[2]);
  290. return CMD_RET_FAILURE;
  291. }
  292. rc = tpm_extend(index, in_digest, out_digest);
  293. if (!rc) {
  294. puts("PCR value after execution of the command:\n");
  295. print_byte_string(out_digest, sizeof(out_digest));
  296. }
  297. return report_return_code(rc);
  298. }
  299. static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag,
  300. int argc, char * const argv[])
  301. {
  302. uint32_t index, count, rc;
  303. void *data;
  304. if (argc != 4)
  305. return CMD_RET_USAGE;
  306. index = simple_strtoul(argv[1], NULL, 0);
  307. data = (void *)simple_strtoul(argv[2], NULL, 0);
  308. count = simple_strtoul(argv[3], NULL, 0);
  309. rc = tpm_pcr_read(index, data, count);
  310. if (!rc) {
  311. puts("Named PCR content:\n");
  312. print_byte_string(data, count);
  313. }
  314. return report_return_code(rc);
  315. }
  316. static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag,
  317. int argc, char * const argv[])
  318. {
  319. uint16_t presence;
  320. if (argc != 2)
  321. return CMD_RET_USAGE;
  322. presence = (uint16_t)simple_strtoul(argv[1], NULL, 0);
  323. return report_return_code(tpm_tsc_physical_presence(presence));
  324. }
  325. static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag,
  326. int argc, char * const argv[])
  327. {
  328. uint32_t count, rc;
  329. void *data;
  330. if (argc != 3)
  331. return CMD_RET_USAGE;
  332. data = (void *)simple_strtoul(argv[1], NULL, 0);
  333. count = simple_strtoul(argv[2], NULL, 0);
  334. rc = tpm_read_pubek(data, count);
  335. if (!rc) {
  336. puts("pubek value:\n");
  337. print_byte_string(data, count);
  338. }
  339. return report_return_code(rc);
  340. }
  341. static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag,
  342. int argc, char * const argv[])
  343. {
  344. uint8_t state;
  345. if (argc != 2)
  346. return CMD_RET_USAGE;
  347. state = (uint8_t)simple_strtoul(argv[1], NULL, 0);
  348. return report_return_code(tpm_physical_set_deactivated(state));
  349. }
  350. static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag,
  351. int argc, char * const argv[])
  352. {
  353. uint32_t cap_area, sub_cap, rc;
  354. void *cap;
  355. size_t count;
  356. if (argc != 5)
  357. return CMD_RET_USAGE;
  358. cap_area = simple_strtoul(argv[1], NULL, 0);
  359. sub_cap = simple_strtoul(argv[2], NULL, 0);
  360. cap = (void *)simple_strtoul(argv[3], NULL, 0);
  361. count = simple_strtoul(argv[4], NULL, 0);
  362. rc = tpm_get_capability(cap_area, sub_cap, cap, count);
  363. if (!rc) {
  364. puts("capability information:\n");
  365. print_byte_string(cap, count);
  366. }
  367. return report_return_code(rc);
  368. }
  369. #define TPM_COMMAND_NO_ARG(cmd) \
  370. static int do_##cmd(cmd_tbl_t *cmdtp, int flag, \
  371. int argc, char * const argv[]) \
  372. { \
  373. if (argc != 1) \
  374. return CMD_RET_USAGE; \
  375. return report_return_code(cmd()); \
  376. }
  377. TPM_COMMAND_NO_ARG(tpm_init)
  378. TPM_COMMAND_NO_ARG(tpm_self_test_full)
  379. TPM_COMMAND_NO_ARG(tpm_continue_self_test)
  380. TPM_COMMAND_NO_ARG(tpm_force_clear)
  381. TPM_COMMAND_NO_ARG(tpm_physical_enable)
  382. TPM_COMMAND_NO_ARG(tpm_physical_disable)
  383. static int get_tpm(struct udevice **devp)
  384. {
  385. int rc;
  386. rc = uclass_first_device_err(UCLASS_TPM, devp);
  387. if (rc) {
  388. printf("Could not find TPM (ret=%d)\n", rc);
  389. return CMD_RET_FAILURE;
  390. }
  391. return 0;
  392. }
  393. static int do_tpm_info(cmd_tbl_t *cmdtp, int flag, int argc,
  394. char *const argv[])
  395. {
  396. struct udevice *dev;
  397. char buf[80];
  398. int rc;
  399. rc = get_tpm(&dev);
  400. if (rc)
  401. return rc;
  402. rc = tpm_get_desc(dev, buf, sizeof(buf));
  403. if (rc < 0) {
  404. printf("Couldn't get TPM info (%d)\n", rc);
  405. return CMD_RET_FAILURE;
  406. }
  407. printf("%s\n", buf);
  408. return 0;
  409. }
  410. static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag,
  411. int argc, char * const argv[])
  412. {
  413. struct udevice *dev;
  414. void *command;
  415. uint8_t response[1024];
  416. size_t count, response_length = sizeof(response);
  417. uint32_t rc;
  418. command = parse_byte_string(argv[1], NULL, &count);
  419. if (!command) {
  420. printf("Couldn't parse byte string %s\n", argv[1]);
  421. return CMD_RET_FAILURE;
  422. }
  423. rc = get_tpm(&dev);
  424. if (rc)
  425. return rc;
  426. rc = tpm_xfer(dev, command, count, response, &response_length);
  427. free(command);
  428. if (!rc) {
  429. puts("tpm response:\n");
  430. print_byte_string(response, response_length);
  431. }
  432. return report_return_code(rc);
  433. }
  434. static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag,
  435. int argc, char * const argv[])
  436. {
  437. uint32_t index, perm, size;
  438. if (argc != 4)
  439. return CMD_RET_USAGE;
  440. size = type_string_get_space_size(argv[1]);
  441. if (!size) {
  442. printf("Couldn't parse arguments\n");
  443. return CMD_RET_USAGE;
  444. }
  445. index = simple_strtoul(argv[2], NULL, 0);
  446. perm = simple_strtoul(argv[3], NULL, 0);
  447. return report_return_code(tpm_nv_define_space(index, perm, size));
  448. }
  449. static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag,
  450. int argc, char * const argv[])
  451. {
  452. uint32_t index, count, err;
  453. void *data;
  454. if (argc < 3)
  455. return CMD_RET_USAGE;
  456. if (argc != 3 + type_string_get_num_values(argv[1]))
  457. return CMD_RET_USAGE;
  458. index = simple_strtoul(argv[2], NULL, 0);
  459. data = type_string_alloc(argv[1], &count);
  460. if (!data) {
  461. printf("Couldn't parse arguments\n");
  462. return CMD_RET_USAGE;
  463. }
  464. err = tpm_nv_read_value(index, data, count);
  465. if (!err) {
  466. if (type_string_write_vars(argv[1], data, argv + 3)) {
  467. printf("Couldn't write to variables\n");
  468. err = ~0;
  469. }
  470. }
  471. free(data);
  472. return report_return_code(err);
  473. }
  474. static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
  475. int argc, char * const argv[])
  476. {
  477. uint32_t index, count, err;
  478. void *data;
  479. if (argc < 3)
  480. return CMD_RET_USAGE;
  481. if (argc != 3 + type_string_get_num_values(argv[1]))
  482. return CMD_RET_USAGE;
  483. index = simple_strtoul(argv[2], NULL, 0);
  484. data = type_string_alloc(argv[1], &count);
  485. if (!data) {
  486. printf("Couldn't parse arguments\n");
  487. return CMD_RET_USAGE;
  488. }
  489. if (type_string_pack(argv[1], argv + 3, data)) {
  490. printf("Couldn't parse arguments\n");
  491. free(data);
  492. return CMD_RET_USAGE;
  493. }
  494. err = tpm_nv_write_value(index, data, count);
  495. free(data);
  496. return report_return_code(err);
  497. }
  498. #ifdef CONFIG_TPM_AUTH_SESSIONS
  499. static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag,
  500. int argc, char * const argv[])
  501. {
  502. uint32_t auth_handle, err;
  503. err = tpm_oiap(&auth_handle);
  504. return report_return_code(err);
  505. }
  506. static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag,
  507. int argc, char * const argv[])
  508. {
  509. uint32_t parent_handle, key_len, key_handle, err;
  510. uint8_t usage_auth[DIGEST_LENGTH];
  511. void *key;
  512. if (argc < 5)
  513. return CMD_RET_USAGE;
  514. parent_handle = simple_strtoul(argv[1], NULL, 0);
  515. key = (void *)simple_strtoul(argv[2], NULL, 0);
  516. key_len = simple_strtoul(argv[3], NULL, 0);
  517. if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
  518. return CMD_RET_FAILURE;
  519. parse_byte_string(argv[4], usage_auth, NULL);
  520. err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
  521. &key_handle);
  522. if (!err)
  523. printf("Key handle is 0x%x\n", key_handle);
  524. return report_return_code(err);
  525. }
  526. static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag,
  527. int argc, char * const argv[])
  528. {
  529. uint32_t key_handle, err;
  530. uint8_t usage_auth[DIGEST_LENGTH];
  531. uint8_t pub_key_buffer[TPM_PUBKEY_MAX_LENGTH];
  532. size_t pub_key_len = sizeof(pub_key_buffer);
  533. if (argc < 3)
  534. return CMD_RET_USAGE;
  535. key_handle = simple_strtoul(argv[1], NULL, 0);
  536. if (strlen(argv[2]) != 2 * DIGEST_LENGTH)
  537. return CMD_RET_FAILURE;
  538. parse_byte_string(argv[2], usage_auth, NULL);
  539. err = tpm_get_pub_key_oiap(key_handle, usage_auth,
  540. pub_key_buffer, &pub_key_len);
  541. if (!err) {
  542. printf("dump of received pub key structure:\n");
  543. print_byte_string(pub_key_buffer, pub_key_len);
  544. }
  545. return report_return_code(err);
  546. }
  547. TPM_COMMAND_NO_ARG(tpm_end_oiap)
  548. #endif /* CONFIG_TPM_AUTH_SESSIONS */
  549. #define MAKE_TPM_CMD_ENTRY(cmd) \
  550. U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
  551. static cmd_tbl_t tpm_commands[] = {
  552. U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
  553. U_BOOT_CMD_MKENT(init, 0, 1,
  554. do_tpm_init, "", ""),
  555. U_BOOT_CMD_MKENT(startup, 0, 1,
  556. do_tpm_startup, "", ""),
  557. U_BOOT_CMD_MKENT(self_test_full, 0, 1,
  558. do_tpm_self_test_full, "", ""),
  559. U_BOOT_CMD_MKENT(continue_self_test, 0, 1,
  560. do_tpm_continue_self_test, "", ""),
  561. U_BOOT_CMD_MKENT(force_clear, 0, 1,
  562. do_tpm_force_clear, "", ""),
  563. U_BOOT_CMD_MKENT(physical_enable, 0, 1,
  564. do_tpm_physical_enable, "", ""),
  565. U_BOOT_CMD_MKENT(physical_disable, 0, 1,
  566. do_tpm_physical_disable, "", ""),
  567. U_BOOT_CMD_MKENT(nv_define_space, 0, 1,
  568. do_tpm_nv_define_space, "", ""),
  569. U_BOOT_CMD_MKENT(nv_read_value, 0, 1,
  570. do_tpm_nv_read_value, "", ""),
  571. U_BOOT_CMD_MKENT(nv_write_value, 0, 1,
  572. do_tpm_nv_write_value, "", ""),
  573. U_BOOT_CMD_MKENT(extend, 0, 1,
  574. do_tpm_extend, "", ""),
  575. U_BOOT_CMD_MKENT(pcr_read, 0, 1,
  576. do_tpm_pcr_read, "", ""),
  577. U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1,
  578. do_tpm_tsc_physical_presence, "", ""),
  579. U_BOOT_CMD_MKENT(read_pubek, 0, 1,
  580. do_tpm_read_pubek, "", ""),
  581. U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1,
  582. do_tpm_physical_set_deactivated, "", ""),
  583. U_BOOT_CMD_MKENT(get_capability, 0, 1,
  584. do_tpm_get_capability, "", ""),
  585. U_BOOT_CMD_MKENT(raw_transfer, 0, 1,
  586. do_tpm_raw_transfer, "", ""),
  587. U_BOOT_CMD_MKENT(nv_define, 0, 1,
  588. do_tpm_nv_define, "", ""),
  589. U_BOOT_CMD_MKENT(nv_read, 0, 1,
  590. do_tpm_nv_read, "", ""),
  591. U_BOOT_CMD_MKENT(nv_write, 0, 1,
  592. do_tpm_nv_write, "", ""),
  593. #ifdef CONFIG_TPM_AUTH_SESSIONS
  594. U_BOOT_CMD_MKENT(oiap, 0, 1,
  595. do_tpm_oiap, "", ""),
  596. U_BOOT_CMD_MKENT(end_oiap, 0, 1,
  597. do_tpm_end_oiap, "", ""),
  598. U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1,
  599. do_tpm_load_key2_oiap, "", ""),
  600. U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1,
  601. do_tpm_get_pub_key_oiap, "", ""),
  602. #endif /* CONFIG_TPM_AUTH_SESSIONS */
  603. };
  604. static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  605. {
  606. cmd_tbl_t *tpm_cmd;
  607. if (argc < 2)
  608. return CMD_RET_USAGE;
  609. tpm_cmd = find_cmd_tbl(argv[1], tpm_commands, ARRAY_SIZE(tpm_commands));
  610. if (!tpm_cmd)
  611. return CMD_RET_USAGE;
  612. return tpm_cmd->cmd(cmdtp, flag, argc - 1, argv + 1);
  613. }
  614. U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
  615. "Issue a TPM command",
  616. "cmd args...\n"
  617. " - Issue TPM command <cmd> with arguments <args...>.\n"
  618. "Admin Startup and State Commands:\n"
  619. " info - Show information about the TPM\n"
  620. " init\n"
  621. " - Put TPM into a state where it waits for 'startup' command.\n"
  622. " startup mode\n"
  623. " - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n"
  624. " TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n"
  625. "Admin Testing Commands:\n"
  626. " self_test_full\n"
  627. " - Test all of the TPM capabilities.\n"
  628. " continue_self_test\n"
  629. " - Inform TPM that it should complete the self-test.\n"
  630. "Admin Opt-in Commands:\n"
  631. " physical_enable\n"
  632. " - Set the PERMANENT disable flag to FALSE using physical presence as\n"
  633. " authorization.\n"
  634. " physical_disable\n"
  635. " - Set the PERMANENT disable flag to TRUE using physical presence as\n"
  636. " authorization.\n"
  637. " physical_set_deactivated 0|1\n"
  638. " - Set deactivated flag.\n"
  639. "Admin Ownership Commands:\n"
  640. " force_clear\n"
  641. " - Issue TPM_ForceClear command.\n"
  642. " tsc_physical_presence flags\n"
  643. " - Set TPM device's Physical Presence flags to <flags>.\n"
  644. "The Capability Commands:\n"
  645. " get_capability cap_area sub_cap addr count\n"
  646. " - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
  647. " <sub_cap> to memory address <addr>.\n"
  648. #ifdef CONFIG_TPM_AUTH_SESSIONS
  649. "Storage functions\n"
  650. " loadkey2_oiap parent_handle key_addr key_len usage_auth\n"
  651. " - loads a key data from memory address <key_addr>, <key_len> bytes\n"
  652. " into TPM using the parent key <parent_handle> with authorization\n"
  653. " <usage_auth> (20 bytes hex string).\n"
  654. " get_pub_key_oiap key_handle usage_auth\n"
  655. " - get the public key portion of a loaded key <key_handle> using\n"
  656. " authorization <usage auth> (20 bytes hex string)\n"
  657. #endif /* CONFIG_TPM_AUTH_SESSIONS */
  658. "Endorsement Key Handling Commands:\n"
  659. " read_pubek addr count\n"
  660. " - Read <count> bytes of the public endorsement key to memory\n"
  661. " address <addr>\n"
  662. "Integrity Collection and Reporting Commands:\n"
  663. " extend index digest_hex_string\n"
  664. " - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n"
  665. " <digest_hex_string>\n"
  666. " pcr_read index addr count\n"
  667. " - Read <count> bytes from PCR <index> to memory address <addr>.\n"
  668. #ifdef CONFIG_TPM_AUTH_SESSIONS
  669. "Authorization Sessions\n"
  670. " oiap\n"
  671. " - setup an OIAP session\n"
  672. " end_oiap\n"
  673. " - terminates an active OIAP session\n"
  674. #endif /* CONFIG_TPM_AUTH_SESSIONS */
  675. "Non-volatile Storage Commands:\n"
  676. " nv_define_space index permission size\n"
  677. " - Establish a space at index <index> with <permission> of <size> bytes.\n"
  678. " nv_read_value index addr count\n"
  679. " - Read <count> bytes from space <index> to memory address <addr>.\n"
  680. " nv_write_value index addr count\n"
  681. " - Write <count> bytes from memory address <addr> to space <index>.\n"
  682. "Miscellaneous helper functions:\n"
  683. " raw_transfer byte_string\n"
  684. " - Send a byte string <byte_string> to TPM and print the response.\n"
  685. " Non-volatile storage helper functions:\n"
  686. " These helper functions treat a non-volatile space as a non-padded\n"
  687. " sequence of integer values. These integer values are defined by a type\n"
  688. " string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n"
  689. " value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n"
  690. " a type string as their first argument.\n"
  691. " nv_define type_string index perm\n"
  692. " - Define a space <index> with permission <perm>.\n"
  693. " nv_read types_string index vars...\n"
  694. " - Read from space <index> to environment variables <vars...>.\n"
  695. " nv_write types_string index values...\n"
  696. " - Write to space <index> from values <values...>.\n"
  697. );