tpm-v2.c 11 KB

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