tpm-v2.c 12 KB

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