tpm-v2.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 cmd_tbl_t tpm2_commands[] = {
  146. U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
  147. U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""),
  148. U_BOOT_CMD_MKENT(startup, 0, 1, do_tpm2_startup, "", ""),
  149. U_BOOT_CMD_MKENT(self_test, 0, 1, do_tpm2_self_test, "", ""),
  150. U_BOOT_CMD_MKENT(clear, 0, 1, do_tpm2_clear, "", ""),
  151. U_BOOT_CMD_MKENT(pcr_extend, 0, 1, do_tpm2_pcr_extend, "", ""),
  152. U_BOOT_CMD_MKENT(pcr_read, 0, 1, do_tpm_pcr_read, "", ""),
  153. U_BOOT_CMD_MKENT(get_capability, 0, 1, do_tpm_get_capability, "", ""),
  154. };
  155. cmd_tbl_t *get_tpm_commands(unsigned int *size)
  156. {
  157. *size = ARRAY_SIZE(tpm2_commands);
  158. return tpm2_commands;
  159. }
  160. U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command",
  161. "<command> [<arguments>]\n"
  162. "\n"
  163. "info\n"
  164. " Show information about the TPM.\n"
  165. "init\n"
  166. " Initialize the software stack. Always the first command to issue.\n"
  167. "startup <mode>\n"
  168. " Issue a TPM2_Startup command.\n"
  169. " <mode> is one of:\n"
  170. " * TPM2_SU_CLEAR (reset state)\n"
  171. " * TPM2_SU_STATE (preserved state)\n"
  172. "self_test <type>\n"
  173. " Test the TPM capabilities.\n"
  174. " <type> is one of:\n"
  175. " * full (perform all tests)\n"
  176. " * continue (only check untested tests)\n"
  177. "clear <hierarchy>\n"
  178. " Issue a TPM2_Clear command.\n"
  179. " <hierarchy> is one of:\n"
  180. " * TPM2_RH_LOCKOUT\n"
  181. " * TPM2_RH_PLATFORM\n"
  182. "pcr_extend <pcr> <digest_addr>\n"
  183. " Extend PCR #<pcr> with digest at <digest_addr>.\n"
  184. " <pcr>: index of the PCR\n"
  185. " <digest_addr>: address of a 32-byte SHA256 digest\n"
  186. "pcr_read <pcr> <digest_addr>\n"
  187. " Read PCR #<pcr> to memory address <digest_addr>.\n"
  188. " <pcr>: index of the PCR\n"
  189. " <digest_addr>: address to store the a 32-byte SHA256 digest\n"
  190. "get_capability <capability> <property> <addr> <count>\n"
  191. " Read and display <count> entries indexed by <capability>/<property>.\n"
  192. " Values are 4 bytes long and are written at <addr>.\n"
  193. " <capability>: capability\n"
  194. " <property>: property\n"
  195. " <addr>: address to store <count> entries of 4 bytes\n"
  196. " <count>: number of entries to retrieve\n"
  197. );