1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018 Bootlin
4 * Author: Miquel Raynal <miquel.raynal@bootlin.com>
11 #include <tpm-common.h>
13 #include "tpm-user-utils.h"
15 static int do_tpm2_startup(cmd_tbl_t *cmdtp, int flag, int argc,
18 enum tpm2_startup_types mode;
23 if (!strcasecmp("TPM2_SU_CLEAR", argv[1])) {
25 } else if (!strcasecmp("TPM2_SU_STATE", argv[1])) {
28 printf("Couldn't recognize mode string: %s\n", argv[1]);
29 return CMD_RET_FAILURE;
32 return report_return_code(tpm2_startup(mode));
35 static int do_tpm2_self_test(cmd_tbl_t *cmdtp, int flag, int argc,
38 enum tpm2_yes_no full_test;
43 if (!strcasecmp("full", argv[1])) {
45 } else if (!strcasecmp("continue", argv[1])) {
48 printf("Couldn't recognize test mode: %s\n", argv[1]);
49 return CMD_RET_FAILURE;
52 return report_return_code(tpm2_self_test(full_test));
55 static int do_tpm2_clear(cmd_tbl_t *cmdtp, int flag, int argc,
59 const char *pw = (argc < 3) ? NULL : argv[2];
60 const ssize_t pw_sz = pw ? strlen(pw) : 0;
62 if (argc < 2 || argc > 3)
65 if (pw_sz > TPM2_DIGEST_LEN)
68 if (!strcasecmp("TPM2_RH_LOCKOUT", argv[1]))
69 handle = TPM2_RH_LOCKOUT;
70 else if (!strcasecmp("TPM2_RH_PLATFORM", argv[1]))
71 handle = TPM2_RH_PLATFORM;
75 return report_return_code(tpm2_clear(handle, pw, pw_sz));
78 static int do_tpm2_pcr_extend(cmd_tbl_t *cmdtp, int flag, int argc,
82 struct tpm_chip_priv *priv;
83 u32 index = simple_strtoul(argv[1], NULL, 0);
84 void *digest = map_sysmem(simple_strtoul(argv[2], NULL, 0), 0);
91 ret = uclass_first_device_err(UCLASS_TPM, &dev);
95 priv = dev_get_uclass_priv(dev);
99 if (index >= priv->pcr_count)
102 rc = tpm2_pcr_extend(index, digest);
104 unmap_sysmem(digest);
106 return report_return_code(rc);
109 static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag, int argc,
113 struct tpm_chip_priv *priv;
115 unsigned int updates;
120 return CMD_RET_USAGE;
122 ret = uclass_first_device_err(UCLASS_TPM, &dev);
126 priv = dev_get_uclass_priv(dev);
130 index = simple_strtoul(argv[1], NULL, 0);
131 if (index >= priv->pcr_count)
134 data = map_sysmem(simple_strtoul(argv[2], NULL, 0), 0);
136 rc = tpm2_pcr_read(index, priv->pcr_select_min, data, &updates);
138 printf("PCR #%u content (%d known updates):\n", index, updates);
139 print_byte_string(data, TPM2_DIGEST_LEN);
144 return report_return_code(rc);
147 static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag, int argc,
150 u32 capability, property, rc;
156 return CMD_RET_USAGE;
158 capability = simple_strtoul(argv[1], NULL, 0);
159 property = simple_strtoul(argv[2], NULL, 0);
160 data = map_sysmem(simple_strtoul(argv[3], NULL, 0), 0);
161 count = simple_strtoul(argv[4], NULL, 0);
163 rc = tpm2_get_capability(capability, property, data, count);
167 printf("Capabilities read from TPM:\n");
168 for (i = 0; i < count; i++) {
169 printf("Property 0x");
170 for (j = 0; j < 4; j++)
171 printf("%02x", data[(i * 8) + j]);
173 for (j = 4; j < 8; j++)
174 printf("%02x", data[(i * 8) + j]);
181 return report_return_code(rc);
184 static cmd_tbl_t tpm2_commands[] = {
185 U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
186 U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""),
187 U_BOOT_CMD_MKENT(startup, 0, 1, do_tpm2_startup, "", ""),
188 U_BOOT_CMD_MKENT(self_test, 0, 1, do_tpm2_self_test, "", ""),
189 U_BOOT_CMD_MKENT(clear, 0, 1, do_tpm2_clear, "", ""),
190 U_BOOT_CMD_MKENT(pcr_extend, 0, 1, do_tpm2_pcr_extend, "", ""),
191 U_BOOT_CMD_MKENT(pcr_read, 0, 1, do_tpm_pcr_read, "", ""),
192 U_BOOT_CMD_MKENT(get_capability, 0, 1, do_tpm_get_capability, "", ""),
195 cmd_tbl_t *get_tpm_commands(unsigned int *size)
197 *size = ARRAY_SIZE(tpm2_commands);
199 return tpm2_commands;
202 U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command",
203 "<command> [<arguments>]\n"
206 " Show information about the TPM.\n"
208 " Initialize the software stack. Always the first command to issue.\n"
210 " Issue a TPM2_Startup command.\n"
211 " <mode> is one of:\n"
212 " * TPM2_SU_CLEAR (reset state)\n"
213 " * TPM2_SU_STATE (preserved state)\n"
215 " Test the TPM capabilities.\n"
216 " <type> is one of:\n"
217 " * full (perform all tests)\n"
218 " * continue (only check untested tests)\n"
219 "clear <hierarchy>\n"
220 " Issue a TPM2_Clear command.\n"
221 " <hierarchy> is one of:\n"
222 " * TPM2_RH_LOCKOUT\n"
223 " * TPM2_RH_PLATFORM\n"
224 "pcr_extend <pcr> <digest_addr>\n"
225 " Extend PCR #<pcr> with digest at <digest_addr>.\n"
226 " <pcr>: index of the PCR\n"
227 " <digest_addr>: address of a 32-byte SHA256 digest\n"
228 "pcr_read <pcr> <digest_addr>\n"
229 " Read PCR #<pcr> to memory address <digest_addr>.\n"
230 " <pcr>: index of the PCR\n"
231 " <digest_addr>: address to store the a 32-byte SHA256 digest\n"
232 "get_capability <capability> <property> <addr> <count>\n"
233 " Read and display <count> entries indexed by <capability>/<property>.\n"
234 " Values are 4 bytes long and are written at <addr>.\n"
235 " <capability>: capability\n"
236 " <property>: property\n"
237 " <addr>: address to store <count> entries of 4 bytes\n"
238 " <count>: number of entries to retrieve\n"