tpm.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
  507. static int do_tpm_load_key_by_sha1(cmd_tbl_t *cmdtp, int flag, int argc, char *
  508. const argv[])
  509. {
  510. uint32_t parent_handle = 0;
  511. uint32_t key_len, key_handle, err;
  512. uint8_t usage_auth[DIGEST_LENGTH];
  513. uint8_t parent_hash[DIGEST_LENGTH];
  514. void *key;
  515. if (argc < 5)
  516. return CMD_RET_USAGE;
  517. parse_byte_string(argv[1], parent_hash, NULL);
  518. key = (void *)simple_strtoul(argv[2], NULL, 0);
  519. key_len = simple_strtoul(argv[3], NULL, 0);
  520. if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
  521. return CMD_RET_FAILURE;
  522. parse_byte_string(argv[4], usage_auth, NULL);
  523. err = tpm_find_key_sha1(usage_auth, parent_hash, &parent_handle);
  524. if (err) {
  525. printf("Could not find matching parent key (err = %d)\n", err);
  526. return CMD_RET_FAILURE;
  527. }
  528. printf("Found parent key %08x\n", parent_handle);
  529. err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
  530. &key_handle);
  531. if (!err) {
  532. printf("Key handle is 0x%x\n", key_handle);
  533. setenv_hex("key_handle", key_handle);
  534. }
  535. return report_return_code(err);
  536. }
  537. #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
  538. static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag,
  539. int argc, char * const argv[])
  540. {
  541. uint32_t parent_handle, key_len, key_handle, err;
  542. uint8_t usage_auth[DIGEST_LENGTH];
  543. void *key;
  544. if (argc < 5)
  545. return CMD_RET_USAGE;
  546. parent_handle = simple_strtoul(argv[1], NULL, 0);
  547. key = (void *)simple_strtoul(argv[2], NULL, 0);
  548. key_len = simple_strtoul(argv[3], NULL, 0);
  549. if (strlen(argv[4]) != 2 * DIGEST_LENGTH)
  550. return CMD_RET_FAILURE;
  551. parse_byte_string(argv[4], usage_auth, NULL);
  552. err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth,
  553. &key_handle);
  554. if (!err)
  555. printf("Key handle is 0x%x\n", key_handle);
  556. return report_return_code(err);
  557. }
  558. static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag,
  559. int argc, char * const argv[])
  560. {
  561. uint32_t key_handle, err;
  562. uint8_t usage_auth[DIGEST_LENGTH];
  563. uint8_t pub_key_buffer[TPM_PUBKEY_MAX_LENGTH];
  564. size_t pub_key_len = sizeof(pub_key_buffer);
  565. if (argc < 3)
  566. return CMD_RET_USAGE;
  567. key_handle = simple_strtoul(argv[1], NULL, 0);
  568. if (strlen(argv[2]) != 2 * DIGEST_LENGTH)
  569. return CMD_RET_FAILURE;
  570. parse_byte_string(argv[2], usage_auth, NULL);
  571. err = tpm_get_pub_key_oiap(key_handle, usage_auth,
  572. pub_key_buffer, &pub_key_len);
  573. if (!err) {
  574. printf("dump of received pub key structure:\n");
  575. print_byte_string(pub_key_buffer, pub_key_len);
  576. }
  577. return report_return_code(err);
  578. }
  579. TPM_COMMAND_NO_ARG(tpm_end_oiap)
  580. #endif /* CONFIG_TPM_AUTH_SESSIONS */
  581. #ifdef CONFIG_TPM_FLUSH_RESOURCES
  582. static int do_tpm_flush(cmd_tbl_t *cmdtp, int flag, int argc,
  583. char * const argv[])
  584. {
  585. int type = 0;
  586. if (argc != 3)
  587. return CMD_RET_USAGE;
  588. if (!strcasecmp(argv[1], "key"))
  589. type = TPM_RT_KEY;
  590. else if (!strcasecmp(argv[1], "auth"))
  591. type = TPM_RT_AUTH;
  592. else if (!strcasecmp(argv[1], "hash"))
  593. type = TPM_RT_HASH;
  594. else if (!strcasecmp(argv[1], "trans"))
  595. type = TPM_RT_TRANS;
  596. else if (!strcasecmp(argv[1], "context"))
  597. type = TPM_RT_CONTEXT;
  598. else if (!strcasecmp(argv[1], "counter"))
  599. type = TPM_RT_COUNTER;
  600. else if (!strcasecmp(argv[1], "delegate"))
  601. type = TPM_RT_DELEGATE;
  602. else if (!strcasecmp(argv[1], "daa_tpm"))
  603. type = TPM_RT_DAA_TPM;
  604. else if (!strcasecmp(argv[1], "daa_v0"))
  605. type = TPM_RT_DAA_V0;
  606. else if (!strcasecmp(argv[1], "daa_v1"))
  607. type = TPM_RT_DAA_V1;
  608. if (!type) {
  609. printf("Resource type %s unknown.\n", argv[1]);
  610. return -1;
  611. }
  612. if (!strcasecmp(argv[2], "all")) {
  613. uint16_t res_count;
  614. uint8_t buf[288];
  615. uint8_t *ptr;
  616. int err;
  617. uint i;
  618. /* fetch list of already loaded resources in the TPM */
  619. err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
  620. sizeof(buf));
  621. if (err) {
  622. printf("tpm_get_capability returned error %d.\n", err);
  623. return -1;
  624. }
  625. res_count = get_unaligned_be16(buf);
  626. ptr = buf + 2;
  627. for (i = 0; i < res_count; ++i, ptr += 4)
  628. tpm_flush_specific(get_unaligned_be32(ptr), type);
  629. } else {
  630. uint32_t handle = simple_strtoul(argv[2], NULL, 0);
  631. if (!handle) {
  632. printf("Illegal resource handle %s\n", argv[2]);
  633. return -1;
  634. }
  635. tpm_flush_specific(cpu_to_be32(handle), type);
  636. }
  637. return 0;
  638. }
  639. #endif /* CONFIG_TPM_FLUSH_RESOURCES */
  640. #ifdef CONFIG_TPM_LIST_RESOURCES
  641. static int do_tpm_list(cmd_tbl_t *cmdtp, int flag, int argc,
  642. char * const argv[])
  643. {
  644. int type = 0;
  645. uint16_t res_count;
  646. uint8_t buf[288];
  647. uint8_t *ptr;
  648. int err;
  649. uint i;
  650. if (argc != 2)
  651. return CMD_RET_USAGE;
  652. if (!strcasecmp(argv[1], "key"))
  653. type = TPM_RT_KEY;
  654. else if (!strcasecmp(argv[1], "auth"))
  655. type = TPM_RT_AUTH;
  656. else if (!strcasecmp(argv[1], "hash"))
  657. type = TPM_RT_HASH;
  658. else if (!strcasecmp(argv[1], "trans"))
  659. type = TPM_RT_TRANS;
  660. else if (!strcasecmp(argv[1], "context"))
  661. type = TPM_RT_CONTEXT;
  662. else if (!strcasecmp(argv[1], "counter"))
  663. type = TPM_RT_COUNTER;
  664. else if (!strcasecmp(argv[1], "delegate"))
  665. type = TPM_RT_DELEGATE;
  666. else if (!strcasecmp(argv[1], "daa_tpm"))
  667. type = TPM_RT_DAA_TPM;
  668. else if (!strcasecmp(argv[1], "daa_v0"))
  669. type = TPM_RT_DAA_V0;
  670. else if (!strcasecmp(argv[1], "daa_v1"))
  671. type = TPM_RT_DAA_V1;
  672. if (!type) {
  673. printf("Resource type %s unknown.\n", argv[1]);
  674. return -1;
  675. }
  676. /* fetch list of already loaded resources in the TPM */
  677. err = tpm_get_capability(TPM_CAP_HANDLE, type, buf,
  678. sizeof(buf));
  679. if (err) {
  680. printf("tpm_get_capability returned error %d.\n", err);
  681. return -1;
  682. }
  683. res_count = get_unaligned_be16(buf);
  684. ptr = buf + 2;
  685. printf("Resources of type %s (%02x):\n", argv[1], type);
  686. if (!res_count) {
  687. puts("None\n");
  688. } else {
  689. for (i = 0; i < res_count; ++i, ptr += 4)
  690. printf("Index %d: %08x\n", i, get_unaligned_be32(ptr));
  691. }
  692. return 0;
  693. }
  694. #endif /* CONFIG_TPM_LIST_RESOURCES */
  695. #define MAKE_TPM_CMD_ENTRY(cmd) \
  696. U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
  697. static cmd_tbl_t tpm_commands[] = {
  698. U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
  699. U_BOOT_CMD_MKENT(init, 0, 1,
  700. do_tpm_init, "", ""),
  701. U_BOOT_CMD_MKENT(startup, 0, 1,
  702. do_tpm_startup, "", ""),
  703. U_BOOT_CMD_MKENT(self_test_full, 0, 1,
  704. do_tpm_self_test_full, "", ""),
  705. U_BOOT_CMD_MKENT(continue_self_test, 0, 1,
  706. do_tpm_continue_self_test, "", ""),
  707. U_BOOT_CMD_MKENT(force_clear, 0, 1,
  708. do_tpm_force_clear, "", ""),
  709. U_BOOT_CMD_MKENT(physical_enable, 0, 1,
  710. do_tpm_physical_enable, "", ""),
  711. U_BOOT_CMD_MKENT(physical_disable, 0, 1,
  712. do_tpm_physical_disable, "", ""),
  713. U_BOOT_CMD_MKENT(nv_define_space, 0, 1,
  714. do_tpm_nv_define_space, "", ""),
  715. U_BOOT_CMD_MKENT(nv_read_value, 0, 1,
  716. do_tpm_nv_read_value, "", ""),
  717. U_BOOT_CMD_MKENT(nv_write_value, 0, 1,
  718. do_tpm_nv_write_value, "", ""),
  719. U_BOOT_CMD_MKENT(extend, 0, 1,
  720. do_tpm_extend, "", ""),
  721. U_BOOT_CMD_MKENT(pcr_read, 0, 1,
  722. do_tpm_pcr_read, "", ""),
  723. U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1,
  724. do_tpm_tsc_physical_presence, "", ""),
  725. U_BOOT_CMD_MKENT(read_pubek, 0, 1,
  726. do_tpm_read_pubek, "", ""),
  727. U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1,
  728. do_tpm_physical_set_deactivated, "", ""),
  729. U_BOOT_CMD_MKENT(get_capability, 0, 1,
  730. do_tpm_get_capability, "", ""),
  731. U_BOOT_CMD_MKENT(raw_transfer, 0, 1,
  732. do_tpm_raw_transfer, "", ""),
  733. U_BOOT_CMD_MKENT(nv_define, 0, 1,
  734. do_tpm_nv_define, "", ""),
  735. U_BOOT_CMD_MKENT(nv_read, 0, 1,
  736. do_tpm_nv_read, "", ""),
  737. U_BOOT_CMD_MKENT(nv_write, 0, 1,
  738. do_tpm_nv_write, "", ""),
  739. #ifdef CONFIG_TPM_AUTH_SESSIONS
  740. U_BOOT_CMD_MKENT(oiap, 0, 1,
  741. do_tpm_oiap, "", ""),
  742. U_BOOT_CMD_MKENT(end_oiap, 0, 1,
  743. do_tpm_end_oiap, "", ""),
  744. U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1,
  745. do_tpm_load_key2_oiap, "", ""),
  746. #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
  747. U_BOOT_CMD_MKENT(load_key_by_sha1, 0, 1,
  748. do_tpm_load_key_by_sha1, "", ""),
  749. #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
  750. U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1,
  751. do_tpm_get_pub_key_oiap, "", ""),
  752. #endif /* CONFIG_TPM_AUTH_SESSIONS */
  753. #ifdef CONFIG_TPM_FLUSH_RESOURCES
  754. U_BOOT_CMD_MKENT(flush, 0, 1,
  755. do_tpm_flush, "", ""),
  756. #endif /* CONFIG_TPM_FLUSH_RESOURCES */
  757. #ifdef CONFIG_TPM_LIST_RESOURCES
  758. U_BOOT_CMD_MKENT(list, 0, 1,
  759. do_tpm_list, "", ""),
  760. #endif /* CONFIG_TPM_LIST_RESOURCES */
  761. };
  762. static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  763. {
  764. cmd_tbl_t *tpm_cmd;
  765. if (argc < 2)
  766. return CMD_RET_USAGE;
  767. tpm_cmd = find_cmd_tbl(argv[1], tpm_commands, ARRAY_SIZE(tpm_commands));
  768. if (!tpm_cmd)
  769. return CMD_RET_USAGE;
  770. return tpm_cmd->cmd(cmdtp, flag, argc - 1, argv + 1);
  771. }
  772. U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm,
  773. "Issue a TPM command",
  774. "cmd args...\n"
  775. " - Issue TPM command <cmd> with arguments <args...>.\n"
  776. "Admin Startup and State Commands:\n"
  777. " info - Show information about the TPM\n"
  778. " init\n"
  779. " - Put TPM into a state where it waits for 'startup' command.\n"
  780. " startup mode\n"
  781. " - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n"
  782. " TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n"
  783. "Admin Testing Commands:\n"
  784. " self_test_full\n"
  785. " - Test all of the TPM capabilities.\n"
  786. " continue_self_test\n"
  787. " - Inform TPM that it should complete the self-test.\n"
  788. "Admin Opt-in Commands:\n"
  789. " physical_enable\n"
  790. " - Set the PERMANENT disable flag to FALSE using physical presence as\n"
  791. " authorization.\n"
  792. " physical_disable\n"
  793. " - Set the PERMANENT disable flag to TRUE using physical presence as\n"
  794. " authorization.\n"
  795. " physical_set_deactivated 0|1\n"
  796. " - Set deactivated flag.\n"
  797. "Admin Ownership Commands:\n"
  798. " force_clear\n"
  799. " - Issue TPM_ForceClear command.\n"
  800. " tsc_physical_presence flags\n"
  801. " - Set TPM device's Physical Presence flags to <flags>.\n"
  802. "The Capability Commands:\n"
  803. " get_capability cap_area sub_cap addr count\n"
  804. " - Read <count> bytes of TPM capability indexed by <cap_area> and\n"
  805. " <sub_cap> to memory address <addr>.\n"
  806. #if defined(CONFIG_TPM_FLUSH_RESOURCES) || defined(CONFIG_TPM_LIST_RESOURCES)
  807. "Resource management functions\n"
  808. #endif
  809. #ifdef CONFIG_TPM_FLUSH_RESOURCES
  810. " flush resource_type id\n"
  811. " - flushes a resource of type <resource_type> (may be one of key, auth,\n"
  812. " hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
  813. " and id <id> from the TPM. Use an <id> of \"all\" to flush all\n"
  814. " resources of that type.\n"
  815. #endif /* CONFIG_TPM_FLUSH_RESOURCES */
  816. #ifdef CONFIG_TPM_LIST_RESOURCES
  817. " list resource_type\n"
  818. " - lists resources of type <resource_type> (may be one of key, auth,\n"
  819. " hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n"
  820. " contained in the TPM.\n"
  821. #endif /* CONFIG_TPM_LIST_RESOURCES */
  822. #ifdef CONFIG_TPM_AUTH_SESSIONS
  823. "Storage functions\n"
  824. " loadkey2_oiap parent_handle key_addr key_len usage_auth\n"
  825. " - loads a key data from memory address <key_addr>, <key_len> bytes\n"
  826. " into TPM using the parent key <parent_handle> with authorization\n"
  827. " <usage_auth> (20 bytes hex string).\n"
  828. #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1
  829. " load_key_by_sha1 parent_hash key_addr key_len usage_auth\n"
  830. " - loads a key data from memory address <key_addr>, <key_len> bytes\n"
  831. " into TPM using the parent hash <parent_hash> (20 bytes hex string)\n"
  832. " with authorization <usage_auth> (20 bytes hex string).\n"
  833. #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */
  834. " get_pub_key_oiap key_handle usage_auth\n"
  835. " - get the public key portion of a loaded key <key_handle> using\n"
  836. " authorization <usage auth> (20 bytes hex string)\n"
  837. #endif /* CONFIG_TPM_AUTH_SESSIONS */
  838. "Endorsement Key Handling Commands:\n"
  839. " read_pubek addr count\n"
  840. " - Read <count> bytes of the public endorsement key to memory\n"
  841. " address <addr>\n"
  842. "Integrity Collection and Reporting Commands:\n"
  843. " extend index digest_hex_string\n"
  844. " - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n"
  845. " <digest_hex_string>\n"
  846. " pcr_read index addr count\n"
  847. " - Read <count> bytes from PCR <index> to memory address <addr>.\n"
  848. #ifdef CONFIG_TPM_AUTH_SESSIONS
  849. "Authorization Sessions\n"
  850. " oiap\n"
  851. " - setup an OIAP session\n"
  852. " end_oiap\n"
  853. " - terminates an active OIAP session\n"
  854. #endif /* CONFIG_TPM_AUTH_SESSIONS */
  855. "Non-volatile Storage Commands:\n"
  856. " nv_define_space index permission size\n"
  857. " - Establish a space at index <index> with <permission> of <size> bytes.\n"
  858. " nv_read_value index addr count\n"
  859. " - Read <count> bytes from space <index> to memory address <addr>.\n"
  860. " nv_write_value index addr count\n"
  861. " - Write <count> bytes from memory address <addr> to space <index>.\n"
  862. "Miscellaneous helper functions:\n"
  863. " raw_transfer byte_string\n"
  864. " - Send a byte string <byte_string> to TPM and print the response.\n"
  865. " Non-volatile storage helper functions:\n"
  866. " These helper functions treat a non-volatile space as a non-padded\n"
  867. " sequence of integer values. These integer values are defined by a type\n"
  868. " string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n"
  869. " value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n"
  870. " a type string as their first argument.\n"
  871. " nv_define type_string index perm\n"
  872. " - Define a space <index> with permission <perm>.\n"
  873. " nv_read types_string index vars...\n"
  874. " - Read from space <index> to environment variables <vars...>.\n"
  875. " nv_write types_string index values...\n"
  876. " - Write to space <index> from values <values...>.\n"
  877. );