blob: 0cd39821bfff41c905113e0782e98ecef9c52ebe [file] [log] [blame]
Miquel Raynalff322452018-05-15 11:57:08 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2018 Bootlin
4 * Author: Miquel Raynal <miquel.raynal@bootlin.com>
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <log.h>
Miquel Raynal6284be52018-05-15 11:57:15 +020010#include <mapmem.h>
Miquel Raynalff322452018-05-15 11:57:08 +020011#include <tpm-common.h>
12#include <tpm-v2.h>
13#include "tpm-user-utils.h"
14
Miquel Raynal1922df22018-05-15 11:57:12 +020015static int do_tpm2_startup(cmd_tbl_t *cmdtp, int flag, int argc,
16 char * const argv[])
17{
18 enum tpm2_startup_types mode;
Simon Glassabdc7b82018-11-18 14:22:27 -070019 struct udevice *dev;
20 int ret;
Miquel Raynal1922df22018-05-15 11:57:12 +020021
Simon Glassabdc7b82018-11-18 14:22:27 -070022 ret = get_tpm(&dev);
23 if (ret)
24 return ret;
Miquel Raynal1922df22018-05-15 11:57:12 +020025 if (argc != 2)
26 return CMD_RET_USAGE;
27
28 if (!strcasecmp("TPM2_SU_CLEAR", argv[1])) {
29 mode = TPM2_SU_CLEAR;
30 } else if (!strcasecmp("TPM2_SU_STATE", argv[1])) {
31 mode = TPM2_SU_STATE;
32 } else {
33 printf("Couldn't recognize mode string: %s\n", argv[1]);
34 return CMD_RET_FAILURE;
35 }
36
Simon Glassabdc7b82018-11-18 14:22:27 -070037 return report_return_code(tpm2_startup(dev, mode));
Miquel Raynal1922df22018-05-15 11:57:12 +020038}
39
Miquel Raynal2dc6d972018-05-15 11:57:13 +020040static int do_tpm2_self_test(cmd_tbl_t *cmdtp, int flag, int argc,
41 char * const argv[])
42{
43 enum tpm2_yes_no full_test;
Simon Glassabdc7b82018-11-18 14:22:27 -070044 struct udevice *dev;
45 int ret;
Miquel Raynal2dc6d972018-05-15 11:57:13 +020046
Simon Glassabdc7b82018-11-18 14:22:27 -070047 ret = get_tpm(&dev);
48 if (ret)
49 return ret;
Miquel Raynal2dc6d972018-05-15 11:57:13 +020050 if (argc != 2)
51 return CMD_RET_USAGE;
52
53 if (!strcasecmp("full", argv[1])) {
54 full_test = TPMI_YES;
55 } else if (!strcasecmp("continue", argv[1])) {
56 full_test = TPMI_NO;
57 } else {
58 printf("Couldn't recognize test mode: %s\n", argv[1]);
59 return CMD_RET_FAILURE;
60 }
61
Simon Glassabdc7b82018-11-18 14:22:27 -070062 return report_return_code(tpm2_self_test(dev, full_test));
Miquel Raynal2dc6d972018-05-15 11:57:13 +020063}
64
Miquel Raynalbad8ff52018-05-15 11:57:14 +020065static int do_tpm2_clear(cmd_tbl_t *cmdtp, int flag, int argc,
66 char * const argv[])
67{
68 u32 handle = 0;
69 const char *pw = (argc < 3) ? NULL : argv[2];
70 const ssize_t pw_sz = pw ? strlen(pw) : 0;
Simon Glassabdc7b82018-11-18 14:22:27 -070071 struct udevice *dev;
72 int ret;
73
74 ret = get_tpm(&dev);
75 if (ret)
76 return ret;
Miquel Raynalbad8ff52018-05-15 11:57:14 +020077
78 if (argc < 2 || argc > 3)
79 return CMD_RET_USAGE;
80
81 if (pw_sz > TPM2_DIGEST_LEN)
82 return -EINVAL;
83
84 if (!strcasecmp("TPM2_RH_LOCKOUT", argv[1]))
85 handle = TPM2_RH_LOCKOUT;
86 else if (!strcasecmp("TPM2_RH_PLATFORM", argv[1]))
87 handle = TPM2_RH_PLATFORM;
88 else
89 return CMD_RET_USAGE;
90
Simon Glassabdc7b82018-11-18 14:22:27 -070091 return report_return_code(tpm2_clear(dev, handle, pw, pw_sz));
Miquel Raynalbad8ff52018-05-15 11:57:14 +020092}
93
Miquel Raynal6284be52018-05-15 11:57:15 +020094static int do_tpm2_pcr_extend(cmd_tbl_t *cmdtp, int flag, int argc,
95 char * const argv[])
96{
97 struct udevice *dev;
98 struct tpm_chip_priv *priv;
99 u32 index = simple_strtoul(argv[1], NULL, 0);
100 void *digest = map_sysmem(simple_strtoul(argv[2], NULL, 0), 0);
101 int ret;
102 u32 rc;
103
104 if (argc != 3)
105 return CMD_RET_USAGE;
106
Simon Glassabdc7b82018-11-18 14:22:27 -0700107 ret = get_tpm(&dev);
Miquel Raynal6284be52018-05-15 11:57:15 +0200108 if (ret)
109 return ret;
110
111 priv = dev_get_uclass_priv(dev);
112 if (!priv)
113 return -EINVAL;
114
115 if (index >= priv->pcr_count)
116 return -EINVAL;
117
Simon Glassabdc7b82018-11-18 14:22:27 -0700118 rc = tpm2_pcr_extend(dev, index, digest);
Miquel Raynal6284be52018-05-15 11:57:15 +0200119
120 unmap_sysmem(digest);
121
122 return report_return_code(rc);
123}
124
Miquel Raynal1c4ea8f2018-05-15 11:57:16 +0200125static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag, int argc,
126 char * const argv[])
127{
128 struct udevice *dev;
129 struct tpm_chip_priv *priv;
130 u32 index, rc;
131 unsigned int updates;
132 void *data;
133 int ret;
134
135 if (argc != 3)
136 return CMD_RET_USAGE;
137
Simon Glassabdc7b82018-11-18 14:22:27 -0700138 ret = get_tpm(&dev);
Miquel Raynal1c4ea8f2018-05-15 11:57:16 +0200139 if (ret)
140 return ret;
141
142 priv = dev_get_uclass_priv(dev);
143 if (!priv)
144 return -EINVAL;
145
146 index = simple_strtoul(argv[1], NULL, 0);
147 if (index >= priv->pcr_count)
148 return -EINVAL;
149
150 data = map_sysmem(simple_strtoul(argv[2], NULL, 0), 0);
151
Simon Glassabdc7b82018-11-18 14:22:27 -0700152 rc = tpm2_pcr_read(dev, index, priv->pcr_select_min, data, &updates);
Miquel Raynal1c4ea8f2018-05-15 11:57:16 +0200153 if (!rc) {
Heinrich Schuchardt1d1af2a2019-01-06 12:09:10 +0100154 printf("PCR #%u content (%u known updates):\n", index, updates);
Miquel Raynal1c4ea8f2018-05-15 11:57:16 +0200155 print_byte_string(data, TPM2_DIGEST_LEN);
156 }
157
158 unmap_sysmem(data);
159
160 return report_return_code(rc);
161}
162
Miquel Raynal69cd8f02018-05-15 11:57:17 +0200163static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag, int argc,
164 char * const argv[])
165{
166 u32 capability, property, rc;
167 u8 *data;
168 size_t count;
169 int i, j;
Simon Glassabdc7b82018-11-18 14:22:27 -0700170 struct udevice *dev;
171 int ret;
172
173 ret = get_tpm(&dev);
174 if (ret)
175 return ret;
Miquel Raynal69cd8f02018-05-15 11:57:17 +0200176
177 if (argc != 5)
178 return CMD_RET_USAGE;
179
180 capability = simple_strtoul(argv[1], NULL, 0);
181 property = simple_strtoul(argv[2], NULL, 0);
182 data = map_sysmem(simple_strtoul(argv[3], NULL, 0), 0);
183 count = simple_strtoul(argv[4], NULL, 0);
184
Simon Glassabdc7b82018-11-18 14:22:27 -0700185 rc = tpm2_get_capability(dev, capability, property, data, count);
Miquel Raynal69cd8f02018-05-15 11:57:17 +0200186 if (rc)
187 goto unmap_data;
188
189 printf("Capabilities read from TPM:\n");
190 for (i = 0; i < count; i++) {
191 printf("Property 0x");
192 for (j = 0; j < 4; j++)
193 printf("%02x", data[(i * 8) + j]);
194 printf(": 0x");
195 for (j = 4; j < 8; j++)
196 printf("%02x", data[(i * 8) + j]);
197 printf("\n");
198 }
199
200unmap_data:
201 unmap_sysmem(data);
202
203 return report_return_code(rc);
204}
205
Miquel Raynalda9c3392018-05-15 11:57:18 +0200206static int do_tpm_dam_reset(cmd_tbl_t *cmdtp, int flag, int argc,
207 char *const argv[])
208{
209 const char *pw = (argc < 2) ? NULL : argv[1];
210 const ssize_t pw_sz = pw ? strlen(pw) : 0;
Simon Glassabdc7b82018-11-18 14:22:27 -0700211 struct udevice *dev;
212 int ret;
213
214 ret = get_tpm(&dev);
215 if (ret)
216 return ret;
Miquel Raynalda9c3392018-05-15 11:57:18 +0200217
218 if (argc > 2)
219 return CMD_RET_USAGE;
220
221 if (pw_sz > TPM2_DIGEST_LEN)
222 return -EINVAL;
223
Simon Glassabdc7b82018-11-18 14:22:27 -0700224 return report_return_code(tpm2_dam_reset(dev, pw, pw_sz));
Miquel Raynalda9c3392018-05-15 11:57:18 +0200225}
226
227static int do_tpm_dam_parameters(cmd_tbl_t *cmdtp, int flag, int argc,
228 char *const argv[])
229{
230 const char *pw = (argc < 5) ? NULL : argv[4];
231 const ssize_t pw_sz = pw ? strlen(pw) : 0;
232 /*
233 * No Dictionary Attack Mitigation (DAM) means:
234 * maxtries = 0xFFFFFFFF, recovery_time = 1, lockout_recovery = 0
235 */
236 unsigned long int max_tries;
237 unsigned long int recovery_time;
238 unsigned long int lockout_recovery;
Simon Glassabdc7b82018-11-18 14:22:27 -0700239 struct udevice *dev;
240 int ret;
241
242 ret = get_tpm(&dev);
243 if (ret)
244 return ret;
Miquel Raynalda9c3392018-05-15 11:57:18 +0200245
246 if (argc < 4 || argc > 5)
247 return CMD_RET_USAGE;
248
249 if (pw_sz > TPM2_DIGEST_LEN)
250 return -EINVAL;
251
252 if (strict_strtoul(argv[1], 0, &max_tries))
253 return CMD_RET_USAGE;
254
255 if (strict_strtoul(argv[2], 0, &recovery_time))
256 return CMD_RET_USAGE;
257
258 if (strict_strtoul(argv[3], 0, &lockout_recovery))
259 return CMD_RET_USAGE;
260
261 log(LOGC_NONE, LOGL_INFO, "Changing dictionary attack parameters:\n");
262 log(LOGC_NONE, LOGL_INFO, "- maxTries: %lu", max_tries);
263 log(LOGC_NONE, LOGL_INFO, "- recoveryTime: %lu\n", recovery_time);
264 log(LOGC_NONE, LOGL_INFO, "- lockoutRecovery: %lu\n", lockout_recovery);
265
Simon Glassabdc7b82018-11-18 14:22:27 -0700266 return report_return_code(tpm2_dam_parameters(dev, pw, pw_sz, max_tries,
Miquel Raynalda9c3392018-05-15 11:57:18 +0200267 recovery_time,
268 lockout_recovery));
269}
270
Miquel Raynaldc26e912018-05-15 11:57:19 +0200271static int do_tpm_change_auth(cmd_tbl_t *cmdtp, int flag, int argc,
272 char *const argv[])
273{
274 u32 handle;
275 const char *newpw = argv[2];
276 const char *oldpw = (argc == 3) ? NULL : argv[3];
277 const ssize_t newpw_sz = strlen(newpw);
278 const ssize_t oldpw_sz = oldpw ? strlen(oldpw) : 0;
Simon Glassabdc7b82018-11-18 14:22:27 -0700279 struct udevice *dev;
280 int ret;
281
282 ret = get_tpm(&dev);
283 if (ret)
284 return ret;
Miquel Raynaldc26e912018-05-15 11:57:19 +0200285
286 if (argc < 3 || argc > 4)
287 return CMD_RET_USAGE;
288
289 if (newpw_sz > TPM2_DIGEST_LEN || oldpw_sz > TPM2_DIGEST_LEN)
290 return -EINVAL;
291
292 if (!strcasecmp("TPM2_RH_LOCKOUT", argv[1]))
293 handle = TPM2_RH_LOCKOUT;
294 else if (!strcasecmp("TPM2_RH_ENDORSEMENT", argv[1]))
295 handle = TPM2_RH_ENDORSEMENT;
296 else if (!strcasecmp("TPM2_RH_OWNER", argv[1]))
297 handle = TPM2_RH_OWNER;
298 else if (!strcasecmp("TPM2_RH_PLATFORM", argv[1]))
299 handle = TPM2_RH_PLATFORM;
300 else
301 return CMD_RET_USAGE;
302
Simon Glassabdc7b82018-11-18 14:22:27 -0700303 return report_return_code(tpm2_change_auth(dev, handle, newpw, newpw_sz,
Miquel Raynaldc26e912018-05-15 11:57:19 +0200304 oldpw, oldpw_sz));
305}
306
Miquel Raynalb9dd4fab2018-05-15 11:57:20 +0200307static int do_tpm_pcr_setauthpolicy(cmd_tbl_t *cmdtp, int flag, int argc,
308 char * const argv[])
309{
310 u32 index = simple_strtoul(argv[1], NULL, 0);
311 char *key = argv[2];
312 const char *pw = (argc < 4) ? NULL : argv[3];
313 const ssize_t pw_sz = pw ? strlen(pw) : 0;
Simon Glassabdc7b82018-11-18 14:22:27 -0700314 struct udevice *dev;
315 int ret;
316
317 ret = get_tpm(&dev);
318 if (ret)
319 return ret;
Miquel Raynalb9dd4fab2018-05-15 11:57:20 +0200320
321 if (strlen(key) != TPM2_DIGEST_LEN)
322 return -EINVAL;
323
324 if (argc < 3 || argc > 4)
325 return CMD_RET_USAGE;
326
Simon Glassabdc7b82018-11-18 14:22:27 -0700327 return report_return_code(tpm2_pcr_setauthpolicy(dev, pw, pw_sz, index,
Miquel Raynalb9dd4fab2018-05-15 11:57:20 +0200328 key));
329}
330
331static int do_tpm_pcr_setauthvalue(cmd_tbl_t *cmdtp, int flag,
332 int argc, char * const argv[])
333{
334 u32 index = simple_strtoul(argv[1], NULL, 0);
335 char *key = argv[2];
336 const ssize_t key_sz = strlen(key);
337 const char *pw = (argc < 4) ? NULL : argv[3];
338 const ssize_t pw_sz = pw ? strlen(pw) : 0;
Simon Glassabdc7b82018-11-18 14:22:27 -0700339 struct udevice *dev;
340 int ret;
341
342 ret = get_tpm(&dev);
343 if (ret)
344 return ret;
Miquel Raynalb9dd4fab2018-05-15 11:57:20 +0200345
346 if (strlen(key) != TPM2_DIGEST_LEN)
347 return -EINVAL;
348
349 if (argc < 3 || argc > 4)
350 return CMD_RET_USAGE;
351
Simon Glassabdc7b82018-11-18 14:22:27 -0700352 return report_return_code(tpm2_pcr_setauthvalue(dev, pw, pw_sz, index,
Miquel Raynalb9dd4fab2018-05-15 11:57:20 +0200353 key, key_sz));
354}
355
Miquel Raynalff322452018-05-15 11:57:08 +0200356static cmd_tbl_t tpm2_commands[] = {
Philippe Reynes3780e2d2020-01-09 18:45:46 +0100357 U_BOOT_CMD_MKENT(device, 0, 1, do_tpm_device, "", ""),
Miquel Raynalff322452018-05-15 11:57:08 +0200358 U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""),
359 U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""),
Miquel Raynal1922df22018-05-15 11:57:12 +0200360 U_BOOT_CMD_MKENT(startup, 0, 1, do_tpm2_startup, "", ""),
Miquel Raynal2dc6d972018-05-15 11:57:13 +0200361 U_BOOT_CMD_MKENT(self_test, 0, 1, do_tpm2_self_test, "", ""),
Miquel Raynalbad8ff52018-05-15 11:57:14 +0200362 U_BOOT_CMD_MKENT(clear, 0, 1, do_tpm2_clear, "", ""),
Miquel Raynal6284be52018-05-15 11:57:15 +0200363 U_BOOT_CMD_MKENT(pcr_extend, 0, 1, do_tpm2_pcr_extend, "", ""),
Miquel Raynal1c4ea8f2018-05-15 11:57:16 +0200364 U_BOOT_CMD_MKENT(pcr_read, 0, 1, do_tpm_pcr_read, "", ""),
Miquel Raynal69cd8f02018-05-15 11:57:17 +0200365 U_BOOT_CMD_MKENT(get_capability, 0, 1, do_tpm_get_capability, "", ""),
Miquel Raynalda9c3392018-05-15 11:57:18 +0200366 U_BOOT_CMD_MKENT(dam_reset, 0, 1, do_tpm_dam_reset, "", ""),
367 U_BOOT_CMD_MKENT(dam_parameters, 0, 1, do_tpm_dam_parameters, "", ""),
Miquel Raynaldc26e912018-05-15 11:57:19 +0200368 U_BOOT_CMD_MKENT(change_auth, 0, 1, do_tpm_change_auth, "", ""),
Miquel Raynalb9dd4fab2018-05-15 11:57:20 +0200369 U_BOOT_CMD_MKENT(pcr_setauthpolicy, 0, 1,
370 do_tpm_pcr_setauthpolicy, "", ""),
371 U_BOOT_CMD_MKENT(pcr_setauthvalue, 0, 1,
372 do_tpm_pcr_setauthvalue, "", ""),
Miquel Raynalff322452018-05-15 11:57:08 +0200373};
374
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200375cmd_tbl_t *get_tpm2_commands(unsigned int *size)
Miquel Raynalff322452018-05-15 11:57:08 +0200376{
377 *size = ARRAY_SIZE(tpm2_commands);
378
379 return tpm2_commands;
380}
381
Miquel Raynal2a2096e2018-07-19 22:35:09 +0200382U_BOOT_CMD(tpm2, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command",
Miquel Raynalff322452018-05-15 11:57:08 +0200383"<command> [<arguments>]\n"
384"\n"
Philippe Reynes3780e2d2020-01-09 18:45:46 +0100385"device [num device]\n"
386" Show all devices or set the specified device\n"
Miquel Raynalff322452018-05-15 11:57:08 +0200387"info\n"
388" Show information about the TPM.\n"
389"init\n"
390" Initialize the software stack. Always the first command to issue.\n"
Miquel Raynal1922df22018-05-15 11:57:12 +0200391"startup <mode>\n"
392" Issue a TPM2_Startup command.\n"
393" <mode> is one of:\n"
394" * TPM2_SU_CLEAR (reset state)\n"
395" * TPM2_SU_STATE (preserved state)\n"
Miquel Raynal2dc6d972018-05-15 11:57:13 +0200396"self_test <type>\n"
397" Test the TPM capabilities.\n"
398" <type> is one of:\n"
399" * full (perform all tests)\n"
400" * continue (only check untested tests)\n"
Miquel Raynalbad8ff52018-05-15 11:57:14 +0200401"clear <hierarchy>\n"
402" Issue a TPM2_Clear command.\n"
403" <hierarchy> is one of:\n"
404" * TPM2_RH_LOCKOUT\n"
405" * TPM2_RH_PLATFORM\n"
Miquel Raynal6284be52018-05-15 11:57:15 +0200406"pcr_extend <pcr> <digest_addr>\n"
407" Extend PCR #<pcr> with digest at <digest_addr>.\n"
408" <pcr>: index of the PCR\n"
409" <digest_addr>: address of a 32-byte SHA256 digest\n"
Miquel Raynal1c4ea8f2018-05-15 11:57:16 +0200410"pcr_read <pcr> <digest_addr>\n"
411" Read PCR #<pcr> to memory address <digest_addr>.\n"
412" <pcr>: index of the PCR\n"
413" <digest_addr>: address to store the a 32-byte SHA256 digest\n"
Miquel Raynal69cd8f02018-05-15 11:57:17 +0200414"get_capability <capability> <property> <addr> <count>\n"
415" Read and display <count> entries indexed by <capability>/<property>.\n"
416" Values are 4 bytes long and are written at <addr>.\n"
417" <capability>: capability\n"
418" <property>: property\n"
419" <addr>: address to store <count> entries of 4 bytes\n"
420" <count>: number of entries to retrieve\n"
Miquel Raynaldc26e912018-05-15 11:57:19 +0200421"dam_reset [<password>]\n"
422" If the TPM is not in a LOCKOUT state, reset the internal error counter.\n"
423" <password>: optional password\n"
424"dam_parameters <max_tries> <recovery_time> <lockout_recovery> [<password>]\n"
425" If the TPM is not in a LOCKOUT state, set the DAM parameters\n"
426" <maxTries>: maximum number of failures before lockout,\n"
427" 0 means always locking\n"
428" <recoveryTime>: time before decrement of the error counter,\n"
429" 0 means no lockout\n"
430" <lockoutRecovery>: time of a lockout (before the next try),\n"
431" 0 means a reboot is needed\n"
432" <password>: optional password of the LOCKOUT hierarchy\n"
433"change_auth <hierarchy> <new_pw> [<old_pw>]\n"
434" <hierarchy>: the hierarchy\n"
435" <new_pw>: new password for <hierarchy>\n"
436" <old_pw>: optional previous password of <hierarchy>\n"
Miquel Raynalb9dd4fab2018-05-15 11:57:20 +0200437"pcr_setauthpolicy|pcr_setauthvalue <pcr> <key> [<password>]\n"
438" Change the <key> to access PCR #<pcr>.\n"
439" hierarchy and may be empty.\n"
440" /!\\WARNING: untested function, use at your own risks !\n"
441" <pcr>: index of the PCR\n"
442" <key>: secret to protect the access of PCR #<pcr>\n"
443" <password>: optional password of the PLATFORM hierarchy\n"
Miquel Raynalff322452018-05-15 11:57:08 +0200444);