tpm-v2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 ret;
  87. u32 rc;
  88. if (argc != 3)
  89. return CMD_RET_USAGE;
  90. ret = get_tpm(&dev);
  91. if (ret)
  92. return ret;
  93. priv = dev_get_uclass_priv(dev);
  94. if (!priv)
  95. return -EINVAL;
  96. if (index >= priv->pcr_count)
  97. return -EINVAL;
  98. rc = tpm2_pcr_extend(dev, index, TPM2_ALG_SHA256, digest,
  99. TPM2_DIGEST_LEN);
  100. unmap_sysmem(digest);
  101. return report_return_code(rc);
  102. }
  103. static int do_tpm_pcr_read(struct cmd_tbl *cmdtp, int flag, int argc,
  104. char *const argv[])
  105. {
  106. struct udevice *dev;
  107. struct tpm_chip_priv *priv;
  108. u32 index, rc;
  109. unsigned int updates;
  110. void *data;
  111. int ret;
  112. if (argc != 3)
  113. return CMD_RET_USAGE;
  114. ret = get_tpm(&dev);
  115. if (ret)
  116. return ret;
  117. priv = dev_get_uclass_priv(dev);
  118. if (!priv)
  119. return -EINVAL;
  120. index = simple_strtoul(argv[1], NULL, 0);
  121. if (index >= priv->pcr_count)
  122. return -EINVAL;
  123. data = map_sysmem(simple_strtoul(argv[2], NULL, 0), 0);
  124. rc = tpm2_pcr_read(dev, index, priv->pcr_select_min, data, &updates);
  125. if (!rc) {
  126. printf("PCR #%u content (%u known updates):\n", index, updates);
  127. print_byte_string(data, TPM2_DIGEST_LEN);
  128. }
  129. unmap_sysmem(data);
  130. return report_return_code(rc);
  131. }
  132. static int do_tpm_get_capability(struct cmd_tbl *cmdtp, int flag, int argc,
  133. char *const argv[])
  134. {
  135. u32 capability, property, rc;
  136. u8 *data;
  137. size_t count;
  138. int i, j;
  139. struct udevice *dev;
  140. int ret;
  141. ret = get_tpm(&dev);
  142. if (ret)
  143. return ret;
  144. if (argc != 5)
  145. return CMD_RET_USAGE;
  146. capability = simple_strtoul(argv[1], NULL, 0);
  147. property = simple_strtoul(argv[2], NULL, 0);
  148. data = map_sysmem(simple_strtoul(argv[3], NULL, 0), 0);
  149. count = simple_strtoul(argv[4], NULL, 0);
  150. rc = tpm2_get_capability(dev, capability, property, data, count);
  151. if (rc)
  152. goto unmap_data;
  153. printf("Capabilities read from TPM:\n");
  154. for (i = 0; i < count; i++) {
  155. printf("Property 0x");
  156. for (j = 0; j < 4; j++)
  157. printf("%02x", data[(i * 8) + j + sizeof(u32)]);
  158. printf(": 0x");
  159. for (j = 4; j < 8; j++)
  160. printf("%02x", data[(i * 8) + j + sizeof(u32)]);
  161. printf("\n");
  162. }
  163. unmap_data:
  164. unmap_sysmem(data);
  165. return report_return_code(rc);
  166. }
  167. static int do_tpm_dam_reset(struct cmd_tbl *cmdtp, int flag, int argc,
  168. char *const argv[])
  169. {
  170. const char *pw = (argc < 2) ? NULL : argv[1];
  171. const ssize_t pw_sz = pw ? strlen(pw) : 0;
  172. struct udevice *dev;
  173. int ret;
  174. ret = get_tpm(&dev);
  175. if (ret)
  176. return ret;
  177. if (argc > 2)
  178. return CMD_RET_USAGE;
  179. if (pw_sz > TPM2_DIGEST_LEN)
  180. return -EINVAL;
  181. return report_return_code(tpm2_dam_reset(dev, pw, pw_sz));
  182. }
  183. static int do_tpm_dam_parameters(struct cmd_tbl *cmdtp, int flag, int argc,
  184. char *const argv[])
  185. {
  186. const char *pw = (argc < 5) ? NULL : argv[4];
  187. const ssize_t pw_sz = pw ? strlen(pw) : 0;
  188. /*
  189. * No Dictionary Attack Mitigation (DAM) means:
  190. * maxtries = 0xFFFFFFFF, recovery_time = 1, lockout_recovery = 0
  191. */
  192. unsigned long int max_tries;
  193. unsigned long int recovery_time;
  194. unsigned long int lockout_recovery;
  195. struct udevice *dev;
  196. int ret;
  197. ret = get_tpm(&dev);
  198. if (ret)
  199. return ret;
  200. if (argc < 4 || argc > 5)
  201. return CMD_RET_USAGE;
  202. if (pw_sz > TPM2_DIGEST_LEN)
  203. return -EINVAL;
  204. if (strict_strtoul(argv[1], 0, &max_tries))
  205. return CMD_RET_USAGE;
  206. if (strict_strtoul(argv[2], 0, &recovery_time))
  207. return CMD_RET_USAGE;
  208. if (strict_strtoul(argv[3], 0, &lockout_recovery))
  209. return CMD_RET_USAGE;
  210. log(LOGC_NONE, LOGL_INFO, "Changing dictionary attack parameters:\n");
  211. log(LOGC_NONE, LOGL_INFO, "- maxTries: %lu", max_tries);
  212. log(LOGC_NONE, LOGL_INFO, "- recoveryTime: %lu\n", recovery_time);
  213. log(LOGC_NONE, LOGL_INFO, "- lockoutRecovery: %lu\n", lockout_recovery);
  214. return report_return_code(tpm2_dam_parameters(dev, pw, pw_sz, max_tries,
  215. recovery_time,
  216. lockout_recovery));
  217. }
  218. static int do_tpm_change_auth(struct cmd_tbl *cmdtp, int flag, int argc,
  219. char *const argv[])
  220. {
  221. u32 handle;
  222. const char *newpw = argv[2];
  223. const char *oldpw = (argc == 3) ? NULL : argv[3];
  224. const ssize_t newpw_sz = strlen(newpw);
  225. const ssize_t oldpw_sz = oldpw ? strlen(oldpw) : 0;
  226. struct udevice *dev;
  227. int ret;
  228. ret = get_tpm(&dev);
  229. if (ret)
  230. return ret;
  231. if (argc < 3 || argc > 4)
  232. return CMD_RET_USAGE;
  233. if (newpw_sz > TPM2_DIGEST_LEN || oldpw_sz > TPM2_DIGEST_LEN)
  234. return -EINVAL;
  235. if (!strcasecmp("TPM2_RH_LOCKOUT", argv[1]))
  236. handle = TPM2_RH_LOCKOUT;
  237. else if (!strcasecmp("TPM2_RH_ENDORSEMENT", argv[1]))
  238. handle = TPM2_RH_ENDORSEMENT;
  239. else if (!strcasecmp("TPM2_RH_OWNER", argv[1]))
  240. handle = TPM2_RH_OWNER;
  241. else if (!strcasecmp("TPM2_RH_PLATFORM", argv[1]))
  242. handle = TPM2_RH_PLATFORM;
  243. else
  244. return CMD_RET_USAGE;
  245. return report_return_code(tpm2_change_auth(dev, handle, newpw, newpw_sz,
  246. oldpw, oldpw_sz));
  247. }
  248. static int do_tpm_pcr_setauthpolicy(struct cmd_tbl *cmdtp, int flag, int argc,
  249. char *const argv[])
  250. {
  251. u32 index = simple_strtoul(argv[1], NULL, 0);
  252. char *key = argv[2];
  253. const char *pw = (argc < 4) ? NULL : argv[3];
  254. const ssize_t pw_sz = pw ? strlen(pw) : 0;
  255. struct udevice *dev;
  256. int ret;
  257. ret = get_tpm(&dev);
  258. if (ret)
  259. return ret;
  260. if (strlen(key) != TPM2_DIGEST_LEN)
  261. return -EINVAL;
  262. if (argc < 3 || argc > 4)
  263. return CMD_RET_USAGE;
  264. return report_return_code(tpm2_pcr_setauthpolicy(dev, pw, pw_sz, index,
  265. key));
  266. }
  267. static int do_tpm_pcr_setauthvalue(struct cmd_tbl *cmdtp, int flag,
  268. int argc, char *const argv[])
  269. {
  270. u32 index = simple_strtoul(argv[1], NULL, 0);
  271. char *key = argv[2];
  272. const ssize_t key_sz = strlen(key);
  273. const char *pw = (argc < 4) ? NULL : argv[3];
  274. const ssize_t pw_sz = pw ? strlen(pw) : 0;
  275. struct udevice *dev;
  276. int ret;
  277. ret = get_tpm(&dev);
  278. if (ret)
  279. return ret;
  280. if (strlen(key) != TPM2_DIGEST_LEN)
  281. return -EINVAL;
  282. if (argc < 3 || argc > 4)
  283. return CMD_RET_USAGE;
  284. return report_return_code(tpm2_pcr_setauthvalue(dev, pw, pw_sz, index,
  285. key, key_sz));
  286. }
  287. static struct cmd_tbl tpm2_commands[] = {
  288. U_BOOT_CMD_MKENT(device, 0, 1, do_tpm_device, "", ""),
  289. U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
  290. U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""),
  291. U_BOOT_CMD_MKENT(startup, 0, 1, do_tpm2_startup, "", ""),
  292. U_BOOT_CMD_MKENT(self_test, 0, 1, do_tpm2_self_test, "", ""),
  293. U_BOOT_CMD_MKENT(clear, 0, 1, do_tpm2_clear, "", ""),
  294. U_BOOT_CMD_MKENT(pcr_extend, 0, 1, do_tpm2_pcr_extend, "", ""),
  295. U_BOOT_CMD_MKENT(pcr_read, 0, 1, do_tpm_pcr_read, "", ""),
  296. U_BOOT_CMD_MKENT(get_capability, 0, 1, do_tpm_get_capability, "", ""),
  297. U_BOOT_CMD_MKENT(dam_reset, 0, 1, do_tpm_dam_reset, "", ""),
  298. U_BOOT_CMD_MKENT(dam_parameters, 0, 1, do_tpm_dam_parameters, "", ""),
  299. U_BOOT_CMD_MKENT(change_auth, 0, 1, do_tpm_change_auth, "", ""),
  300. U_BOOT_CMD_MKENT(pcr_setauthpolicy, 0, 1,
  301. do_tpm_pcr_setauthpolicy, "", ""),
  302. U_BOOT_CMD_MKENT(pcr_setauthvalue, 0, 1,
  303. do_tpm_pcr_setauthvalue, "", ""),
  304. };
  305. struct cmd_tbl *get_tpm2_commands(unsigned int *size)
  306. {
  307. *size = ARRAY_SIZE(tpm2_commands);
  308. return tpm2_commands;
  309. }
  310. U_BOOT_CMD(tpm2, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command",
  311. "<command> [<arguments>]\n"
  312. "\n"
  313. "device [num device]\n"
  314. " Show all devices or set the specified device\n"
  315. "info\n"
  316. " Show information about the TPM.\n"
  317. "init\n"
  318. " Initialize the software stack. Always the first command to issue.\n"
  319. "startup <mode>\n"
  320. " Issue a TPM2_Startup command.\n"
  321. " <mode> is one of:\n"
  322. " * TPM2_SU_CLEAR (reset state)\n"
  323. " * TPM2_SU_STATE (preserved state)\n"
  324. "self_test <type>\n"
  325. " Test the TPM capabilities.\n"
  326. " <type> is one of:\n"
  327. " * full (perform all tests)\n"
  328. " * continue (only check untested tests)\n"
  329. "clear <hierarchy>\n"
  330. " Issue a TPM2_Clear command.\n"
  331. " <hierarchy> is one of:\n"
  332. " * TPM2_RH_LOCKOUT\n"
  333. " * TPM2_RH_PLATFORM\n"
  334. "pcr_extend <pcr> <digest_addr>\n"
  335. " Extend PCR #<pcr> with digest at <digest_addr>.\n"
  336. " <pcr>: index of the PCR\n"
  337. " <digest_addr>: address of a 32-byte SHA256 digest\n"
  338. "pcr_read <pcr> <digest_addr>\n"
  339. " Read PCR #<pcr> to memory address <digest_addr>.\n"
  340. " <pcr>: index of the PCR\n"
  341. " <digest_addr>: address to store the a 32-byte SHA256 digest\n"
  342. "get_capability <capability> <property> <addr> <count>\n"
  343. " Read and display <count> entries indexed by <capability>/<property>.\n"
  344. " Values are 4 bytes long and are written at <addr>.\n"
  345. " <capability>: capability\n"
  346. " <property>: property\n"
  347. " <addr>: address to store <count> entries of 4 bytes\n"
  348. " <count>: number of entries to retrieve\n"
  349. "dam_reset [<password>]\n"
  350. " If the TPM is not in a LOCKOUT state, reset the internal error counter.\n"
  351. " <password>: optional password\n"
  352. "dam_parameters <max_tries> <recovery_time> <lockout_recovery> [<password>]\n"
  353. " If the TPM is not in a LOCKOUT state, set the DAM parameters\n"
  354. " <maxTries>: maximum number of failures before lockout,\n"
  355. " 0 means always locking\n"
  356. " <recoveryTime>: time before decrement of the error counter,\n"
  357. " 0 means no lockout\n"
  358. " <lockoutRecovery>: time of a lockout (before the next try),\n"
  359. " 0 means a reboot is needed\n"
  360. " <password>: optional password of the LOCKOUT hierarchy\n"
  361. "change_auth <hierarchy> <new_pw> [<old_pw>]\n"
  362. " <hierarchy>: the hierarchy\n"
  363. " <new_pw>: new password for <hierarchy>\n"
  364. " <old_pw>: optional previous password of <hierarchy>\n"
  365. "pcr_setauthpolicy|pcr_setauthvalue <pcr> <key> [<password>]\n"
  366. " Change the <key> to access PCR #<pcr>.\n"
  367. " hierarchy and may be empty.\n"
  368. " /!\\WARNING: untested function, use at your own risks !\n"
  369. " <pcr>: index of the PCR\n"
  370. " <key>: secret to protect the access of PCR #<pcr>\n"
  371. " <password>: optional password of the PLATFORM hierarchy\n"
  372. );