tpm-v2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 <command.h>
  8. #include <dm.h>
  9. #include <log.h>
  10. #include <mapmem.h>
  11. #include <tpm-common.h>
  12. #include <tpm-v2.h>
  13. #include "tpm-user-utils.h"
  14. static int do_tpm2_startup(struct cmd_tbl *cmdtp, int flag, int argc,
  15. char *const argv[])
  16. {
  17. enum tpm2_startup_types mode;
  18. struct udevice *dev;
  19. int ret;
  20. ret = get_tpm(&dev);
  21. if (ret)
  22. return ret;
  23. if (argc != 2)
  24. return CMD_RET_USAGE;
  25. if (!strcasecmp("TPM2_SU_CLEAR", argv[1])) {
  26. mode = TPM2_SU_CLEAR;
  27. } else if (!strcasecmp("TPM2_SU_STATE", argv[1])) {
  28. mode = TPM2_SU_STATE;
  29. } else {
  30. printf("Couldn't recognize mode string: %s\n", argv[1]);
  31. return CMD_RET_FAILURE;
  32. }
  33. return report_return_code(tpm2_startup(dev, mode));
  34. }
  35. static int do_tpm2_self_test(struct cmd_tbl *cmdtp, int flag, int argc,
  36. char *const argv[])
  37. {
  38. enum tpm2_yes_no full_test;
  39. struct udevice *dev;
  40. int ret;
  41. ret = get_tpm(&dev);
  42. if (ret)
  43. return ret;
  44. if (argc != 2)
  45. return CMD_RET_USAGE;
  46. if (!strcasecmp("full", argv[1])) {
  47. full_test = TPMI_YES;
  48. } else if (!strcasecmp("continue", argv[1])) {
  49. full_test = TPMI_NO;
  50. } else {
  51. printf("Couldn't recognize test mode: %s\n", argv[1]);
  52. return CMD_RET_FAILURE;
  53. }
  54. return report_return_code(tpm2_self_test(dev, full_test));
  55. }
  56. static int do_tpm2_clear(struct cmd_tbl *cmdtp, int flag, int argc,
  57. char *const argv[])
  58. {
  59. u32 handle = 0;
  60. const char *pw = (argc < 3) ? NULL : argv[2];
  61. const ssize_t pw_sz = pw ? strlen(pw) : 0;
  62. struct udevice *dev;
  63. int ret;
  64. ret = get_tpm(&dev);
  65. if (ret)
  66. return ret;
  67. if (argc < 2 || argc > 3)
  68. return CMD_RET_USAGE;
  69. if (pw_sz > TPM2_DIGEST_LEN)
  70. return -EINVAL;
  71. if (!strcasecmp("TPM2_RH_LOCKOUT", argv[1]))
  72. handle = TPM2_RH_LOCKOUT;
  73. else if (!strcasecmp("TPM2_RH_PLATFORM", argv[1]))
  74. handle = TPM2_RH_PLATFORM;
  75. else
  76. return CMD_RET_USAGE;
  77. return report_return_code(tpm2_clear(dev, handle, pw, pw_sz));
  78. }
  79. static int do_tpm2_pcr_extend(struct cmd_tbl *cmdtp, int flag, int argc,
  80. char *const argv[])
  81. {
  82. struct udevice *dev;
  83. struct tpm_chip_priv *priv;
  84. u32 index = simple_strtoul(argv[1], NULL, 0);
  85. void *digest = map_sysmem(simple_strtoul(argv[2], NULL, 0), 0);
  86. int algo = TPM2_ALG_SHA256;
  87. int algo_len;
  88. int ret;
  89. u32 rc;
  90. if (argc < 3 || argc > 4)
  91. return CMD_RET_USAGE;
  92. if (argc == 4) {
  93. algo = tpm2_name_to_algorithm(argv[3]);
  94. if (algo < 0)
  95. return CMD_RET_FAILURE;
  96. }
  97. algo_len = tpm2_algorithm_to_len(algo);
  98. ret = get_tpm(&dev);
  99. if (ret)
  100. return ret;
  101. priv = dev_get_uclass_priv(dev);
  102. if (!priv)
  103. return -EINVAL;
  104. if (index >= priv->pcr_count)
  105. return -EINVAL;
  106. rc = tpm2_pcr_extend(dev, index, algo, digest, algo_len);
  107. if (!rc) {
  108. printf("PCR #%u extended with %d byte %s digest\n", index,
  109. algo_len, tpm2_algorithm_name(algo));
  110. print_byte_string(digest, algo_len);
  111. }
  112. unmap_sysmem(digest);
  113. return report_return_code(rc);
  114. }
  115. static int do_tpm_pcr_read(struct cmd_tbl *cmdtp, int flag, int argc,
  116. char *const argv[])
  117. {
  118. enum tpm2_algorithms algo = TPM2_ALG_SHA256;
  119. struct udevice *dev;
  120. struct tpm_chip_priv *priv;
  121. u32 index, rc;
  122. int algo_len;
  123. unsigned int updates;
  124. void *data;
  125. int ret;
  126. if (argc < 3 || argc > 4)
  127. return CMD_RET_USAGE;
  128. if (argc == 4) {
  129. algo = tpm2_name_to_algorithm(argv[3]);
  130. if (algo < 0)
  131. return CMD_RET_FAILURE;
  132. }
  133. algo_len = tpm2_algorithm_to_len(algo);
  134. ret = get_tpm(&dev);
  135. if (ret)
  136. return ret;
  137. priv = dev_get_uclass_priv(dev);
  138. if (!priv)
  139. return -EINVAL;
  140. index = simple_strtoul(argv[1], NULL, 0);
  141. if (index >= priv->pcr_count)
  142. return -EINVAL;
  143. data = map_sysmem(simple_strtoul(argv[2], NULL, 0), 0);
  144. rc = tpm2_pcr_read(dev, index, priv->pcr_select_min, algo,
  145. data, algo_len, &updates);
  146. if (!rc) {
  147. printf("PCR #%u %s %d byte content (%u known updates):\n", index,
  148. tpm2_algorithm_name(algo), algo_len, updates);
  149. print_byte_string(data, algo_len);
  150. }
  151. unmap_sysmem(data);
  152. return report_return_code(rc);
  153. }
  154. static int do_tpm_get_capability(struct cmd_tbl *cmdtp, int flag, int argc,
  155. char *const argv[])
  156. {
  157. u32 capability, property, rc;
  158. u8 *data;
  159. size_t count;
  160. int i, j;
  161. struct udevice *dev;
  162. int ret;
  163. ret = get_tpm(&dev);
  164. if (ret)
  165. return ret;
  166. if (argc != 5)
  167. return CMD_RET_USAGE;
  168. capability = simple_strtoul(argv[1], NULL, 0);
  169. property = simple_strtoul(argv[2], NULL, 0);
  170. data = map_sysmem(simple_strtoul(argv[3], NULL, 0), 0);
  171. count = simple_strtoul(argv[4], NULL, 0);
  172. rc = tpm2_get_capability(dev, capability, property, data, count);
  173. if (rc)
  174. goto unmap_data;
  175. printf("Capabilities read from TPM:\n");
  176. for (i = 0; i < count; i++) {
  177. printf("Property 0x");
  178. for (j = 0; j < 4; j++)
  179. printf("%02x", data[(i * 8) + j + sizeof(u32)]);
  180. printf(": 0x");
  181. for (j = 4; j < 8; j++)
  182. printf("%02x", data[(i * 8) + j + sizeof(u32)]);
  183. printf("\n");
  184. }
  185. unmap_data:
  186. unmap_sysmem(data);
  187. return report_return_code(rc);
  188. }
  189. static int do_tpm_dam_reset(struct cmd_tbl *cmdtp, int flag, int argc,
  190. char *const argv[])
  191. {
  192. const char *pw = (argc < 2) ? NULL : argv[1];
  193. const ssize_t pw_sz = pw ? strlen(pw) : 0;
  194. struct udevice *dev;
  195. int ret;
  196. ret = get_tpm(&dev);
  197. if (ret)
  198. return ret;
  199. if (argc > 2)
  200. return CMD_RET_USAGE;
  201. if (pw_sz > TPM2_DIGEST_LEN)
  202. return -EINVAL;
  203. return report_return_code(tpm2_dam_reset(dev, pw, pw_sz));
  204. }
  205. static int do_tpm_dam_parameters(struct cmd_tbl *cmdtp, int flag, int argc,
  206. char *const argv[])
  207. {
  208. const char *pw = (argc < 5) ? NULL : argv[4];
  209. const ssize_t pw_sz = pw ? strlen(pw) : 0;
  210. /*
  211. * No Dictionary Attack Mitigation (DAM) means:
  212. * maxtries = 0xFFFFFFFF, recovery_time = 1, lockout_recovery = 0
  213. */
  214. unsigned long int max_tries;
  215. unsigned long int recovery_time;
  216. unsigned long int lockout_recovery;
  217. struct udevice *dev;
  218. int ret;
  219. ret = get_tpm(&dev);
  220. if (ret)
  221. return ret;
  222. if (argc < 4 || argc > 5)
  223. return CMD_RET_USAGE;
  224. if (pw_sz > TPM2_DIGEST_LEN)
  225. return -EINVAL;
  226. if (strict_strtoul(argv[1], 0, &max_tries))
  227. return CMD_RET_USAGE;
  228. if (strict_strtoul(argv[2], 0, &recovery_time))
  229. return CMD_RET_USAGE;
  230. if (strict_strtoul(argv[3], 0, &lockout_recovery))
  231. return CMD_RET_USAGE;
  232. log(LOGC_NONE, LOGL_INFO, "Changing dictionary attack parameters:\n");
  233. log(LOGC_NONE, LOGL_INFO, "- maxTries: %lu", max_tries);
  234. log(LOGC_NONE, LOGL_INFO, "- recoveryTime: %lu\n", recovery_time);
  235. log(LOGC_NONE, LOGL_INFO, "- lockoutRecovery: %lu\n", lockout_recovery);
  236. return report_return_code(tpm2_dam_parameters(dev, pw, pw_sz, max_tries,
  237. recovery_time,
  238. lockout_recovery));
  239. }
  240. static int do_tpm_change_auth(struct cmd_tbl *cmdtp, int flag, int argc,
  241. char *const argv[])
  242. {
  243. u32 handle;
  244. const char *newpw = argv[2];
  245. const char *oldpw = (argc == 3) ? NULL : argv[3];
  246. const ssize_t newpw_sz = strlen(newpw);
  247. const ssize_t oldpw_sz = oldpw ? strlen(oldpw) : 0;
  248. struct udevice *dev;
  249. int ret;
  250. ret = get_tpm(&dev);
  251. if (ret)
  252. return ret;
  253. if (argc < 3 || argc > 4)
  254. return CMD_RET_USAGE;
  255. if (newpw_sz > TPM2_DIGEST_LEN || oldpw_sz > TPM2_DIGEST_LEN)
  256. return -EINVAL;
  257. if (!strcasecmp("TPM2_RH_LOCKOUT", argv[1]))
  258. handle = TPM2_RH_LOCKOUT;
  259. else if (!strcasecmp("TPM2_RH_ENDORSEMENT", argv[1]))
  260. handle = TPM2_RH_ENDORSEMENT;
  261. else if (!strcasecmp("TPM2_RH_OWNER", argv[1]))
  262. handle = TPM2_RH_OWNER;
  263. else if (!strcasecmp("TPM2_RH_PLATFORM", argv[1]))
  264. handle = TPM2_RH_PLATFORM;
  265. else
  266. return CMD_RET_USAGE;
  267. return report_return_code(tpm2_change_auth(dev, handle, newpw, newpw_sz,
  268. oldpw, oldpw_sz));
  269. }
  270. static int do_tpm_pcr_setauthpolicy(struct cmd_tbl *cmdtp, int flag, int argc,
  271. char *const argv[])
  272. {
  273. u32 index = simple_strtoul(argv[1], NULL, 0);
  274. char *key = argv[2];
  275. const char *pw = (argc < 4) ? NULL : argv[3];
  276. const ssize_t pw_sz = pw ? strlen(pw) : 0;
  277. struct udevice *dev;
  278. int ret;
  279. ret = get_tpm(&dev);
  280. if (ret)
  281. return ret;
  282. if (strlen(key) != TPM2_DIGEST_LEN)
  283. return -EINVAL;
  284. if (argc < 3 || argc > 4)
  285. return CMD_RET_USAGE;
  286. return report_return_code(tpm2_pcr_setauthpolicy(dev, pw, pw_sz, index,
  287. key));
  288. }
  289. static int do_tpm_pcr_setauthvalue(struct cmd_tbl *cmdtp, int flag,
  290. int argc, char *const argv[])
  291. {
  292. u32 index = simple_strtoul(argv[1], NULL, 0);
  293. char *key = argv[2];
  294. const ssize_t key_sz = strlen(key);
  295. const char *pw = (argc < 4) ? NULL : argv[3];
  296. const ssize_t pw_sz = pw ? strlen(pw) : 0;
  297. struct udevice *dev;
  298. int ret;
  299. ret = get_tpm(&dev);
  300. if (ret)
  301. return ret;
  302. if (strlen(key) != TPM2_DIGEST_LEN)
  303. return -EINVAL;
  304. if (argc < 3 || argc > 4)
  305. return CMD_RET_USAGE;
  306. return report_return_code(tpm2_pcr_setauthvalue(dev, pw, pw_sz, index,
  307. key, key_sz));
  308. }
  309. static struct cmd_tbl tpm2_commands[] = {
  310. U_BOOT_CMD_MKENT(device, 0, 1, do_tpm_device, "", ""),
  311. U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
  312. U_BOOT_CMD_MKENT(state, 0, 1, do_tpm_report_state, "", ""),
  313. U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""),
  314. U_BOOT_CMD_MKENT(startup, 0, 1, do_tpm2_startup, "", ""),
  315. U_BOOT_CMD_MKENT(self_test, 0, 1, do_tpm2_self_test, "", ""),
  316. U_BOOT_CMD_MKENT(clear, 0, 1, do_tpm2_clear, "", ""),
  317. U_BOOT_CMD_MKENT(pcr_extend, 0, 1, do_tpm2_pcr_extend, "", ""),
  318. U_BOOT_CMD_MKENT(pcr_read, 0, 1, do_tpm_pcr_read, "", ""),
  319. U_BOOT_CMD_MKENT(get_capability, 0, 1, do_tpm_get_capability, "", ""),
  320. U_BOOT_CMD_MKENT(dam_reset, 0, 1, do_tpm_dam_reset, "", ""),
  321. U_BOOT_CMD_MKENT(dam_parameters, 0, 1, do_tpm_dam_parameters, "", ""),
  322. U_BOOT_CMD_MKENT(change_auth, 0, 1, do_tpm_change_auth, "", ""),
  323. U_BOOT_CMD_MKENT(autostart, 0, 1, do_tpm_autostart, "", ""),
  324. U_BOOT_CMD_MKENT(pcr_setauthpolicy, 0, 1,
  325. do_tpm_pcr_setauthpolicy, "", ""),
  326. U_BOOT_CMD_MKENT(pcr_setauthvalue, 0, 1,
  327. do_tpm_pcr_setauthvalue, "", ""),
  328. };
  329. struct cmd_tbl *get_tpm2_commands(unsigned int *size)
  330. {
  331. *size = ARRAY_SIZE(tpm2_commands);
  332. return tpm2_commands;
  333. }
  334. U_BOOT_CMD(tpm2, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command",
  335. "<command> [<arguments>]\n"
  336. "\n"
  337. "device [num device]\n"
  338. " Show all devices or set the specified device\n"
  339. "info\n"
  340. " Show information about the TPM.\n"
  341. "state\n"
  342. " Show internal state from the TPM (if available)\n"
  343. "autostart\n"
  344. " Initalize the tpm, perform a Startup(clear) and run a full selftest\n"
  345. " sequence\n"
  346. "init\n"
  347. " Initialize the software stack. Always the first command to issue.\n"
  348. " 'tpm startup' is the only acceptable command after a 'tpm init' has been\n"
  349. " issued\n"
  350. "startup <mode>\n"
  351. " Issue a TPM2_Startup command.\n"
  352. " <mode> is one of:\n"
  353. " * TPM2_SU_CLEAR (reset state)\n"
  354. " * TPM2_SU_STATE (preserved state)\n"
  355. "self_test <type>\n"
  356. " Test the TPM capabilities.\n"
  357. " <type> is one of:\n"
  358. " * full (perform all tests)\n"
  359. " * continue (only check untested tests)\n"
  360. "clear <hierarchy>\n"
  361. " Issue a TPM2_Clear command.\n"
  362. " <hierarchy> is one of:\n"
  363. " * TPM2_RH_LOCKOUT\n"
  364. " * TPM2_RH_PLATFORM\n"
  365. "pcr_extend <pcr> <digest_addr> [<digest_algo>]\n"
  366. " Extend PCR #<pcr> with digest at <digest_addr> with digest_algo.\n"
  367. " <pcr>: index of the PCR\n"
  368. " <digest_addr>: address of digest of digest_algo type (defaults to SHA256)\n"
  369. "pcr_read <pcr> <digest_addr> [<digest_algo>]\n"
  370. " Read PCR #<pcr> to memory address <digest_addr> with <digest_algo>.\n"
  371. " <pcr>: index of the PCR\n"
  372. " <digest_addr>: address of digest of digest_algo type (defaults to SHA256)\n"
  373. "get_capability <capability> <property> <addr> <count>\n"
  374. " Read and display <count> entries indexed by <capability>/<property>.\n"
  375. " Values are 4 bytes long and are written at <addr>.\n"
  376. " <capability>: capability\n"
  377. " <property>: property\n"
  378. " <addr>: address to store <count> entries of 4 bytes\n"
  379. " <count>: number of entries to retrieve\n"
  380. "dam_reset [<password>]\n"
  381. " If the TPM is not in a LOCKOUT state, reset the internal error counter.\n"
  382. " <password>: optional password\n"
  383. "dam_parameters <max_tries> <recovery_time> <lockout_recovery> [<password>]\n"
  384. " If the TPM is not in a LOCKOUT state, set the DAM parameters\n"
  385. " <maxTries>: maximum number of failures before lockout,\n"
  386. " 0 means always locking\n"
  387. " <recoveryTime>: time before decrement of the error counter,\n"
  388. " 0 means no lockout\n"
  389. " <lockoutRecovery>: time of a lockout (before the next try),\n"
  390. " 0 means a reboot is needed\n"
  391. " <password>: optional password of the LOCKOUT hierarchy\n"
  392. "change_auth <hierarchy> <new_pw> [<old_pw>]\n"
  393. " <hierarchy>: the hierarchy\n"
  394. " <new_pw>: new password for <hierarchy>\n"
  395. " <old_pw>: optional previous password of <hierarchy>\n"
  396. "pcr_setauthpolicy|pcr_setauthvalue <pcr> <key> [<password>]\n"
  397. " Change the <key> to access PCR #<pcr>.\n"
  398. " hierarchy and may be empty.\n"
  399. " /!\\WARNING: untested function, use at your own risks !\n"
  400. " <pcr>: index of the PCR\n"
  401. " <key>: secret to protect the access of PCR #<pcr>\n"
  402. " <password>: optional password of the PLATFORM hierarchy\n"
  403. );