cmd_tpm.c 20 KB

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