Blame view
cmd/tpm-v2.c
11.6 KB
ff32245bb
|
1 2 3 4 5 6 7 8 9 |
// SPDX-License-Identifier: GPL-2.0+ /* * Copyright (c) 2018 Bootlin * Author: Miquel Raynal <miquel.raynal@bootlin.com> */ #include <common.h> #include <dm.h> #include <log.h> |
6284be5a9
|
10 |
#include <mapmem.h> |
ff32245bb
|
11 12 13 |
#include <tpm-common.h> #include <tpm-v2.h> #include "tpm-user-utils.h" |
1922df201
|
14 15 16 17 |
static int do_tpm2_startup(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { enum tpm2_startup_types mode; |
abdc7b8a2
|
18 19 |
struct udevice *dev; int ret; |
1922df201
|
20 |
|
abdc7b8a2
|
21 22 23 |
ret = get_tpm(&dev); if (ret) return ret; |
1922df201
|
24 25 26 27 28 29 30 31 32 33 34 35 |
if (argc != 2) return CMD_RET_USAGE; if (!strcasecmp("TPM2_SU_CLEAR", argv[1])) { mode = TPM2_SU_CLEAR; } else if (!strcasecmp("TPM2_SU_STATE", argv[1])) { mode = TPM2_SU_STATE; } else { printf("Couldn't recognize mode string: %s ", argv[1]); return CMD_RET_FAILURE; } |
abdc7b8a2
|
36 |
return report_return_code(tpm2_startup(dev, mode)); |
1922df201
|
37 |
} |
2dc6d97e1
|
38 39 40 41 |
static int do_tpm2_self_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { enum tpm2_yes_no full_test; |
abdc7b8a2
|
42 43 |
struct udevice *dev; int ret; |
2dc6d97e1
|
44 |
|
abdc7b8a2
|
45 46 47 |
ret = get_tpm(&dev); if (ret) return ret; |
2dc6d97e1
|
48 49 50 51 52 53 54 55 56 57 58 59 |
if (argc != 2) return CMD_RET_USAGE; if (!strcasecmp("full", argv[1])) { full_test = TPMI_YES; } else if (!strcasecmp("continue", argv[1])) { full_test = TPMI_NO; } else { printf("Couldn't recognize test mode: %s ", argv[1]); return CMD_RET_FAILURE; } |
abdc7b8a2
|
60 |
return report_return_code(tpm2_self_test(dev, full_test)); |
2dc6d97e1
|
61 |
} |
bad8ff569
|
62 63 64 65 66 67 |
static int do_tpm2_clear(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { u32 handle = 0; const char *pw = (argc < 3) ? NULL : argv[2]; const ssize_t pw_sz = pw ? strlen(pw) : 0; |
abdc7b8a2
|
68 69 70 71 72 73 |
struct udevice *dev; int ret; ret = get_tpm(&dev); if (ret) return ret; |
bad8ff569
|
74 75 76 77 78 79 80 81 82 83 84 85 86 |
if (argc < 2 || argc > 3) return CMD_RET_USAGE; if (pw_sz > TPM2_DIGEST_LEN) return -EINVAL; if (!strcasecmp("TPM2_RH_LOCKOUT", argv[1])) handle = TPM2_RH_LOCKOUT; else if (!strcasecmp("TPM2_RH_PLATFORM", argv[1])) handle = TPM2_RH_PLATFORM; else return CMD_RET_USAGE; |
abdc7b8a2
|
87 |
return report_return_code(tpm2_clear(dev, handle, pw, pw_sz)); |
bad8ff569
|
88 |
} |
6284be5a9
|
89 90 91 92 93 94 95 96 97 98 99 100 |
static int do_tpm2_pcr_extend(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { struct udevice *dev; struct tpm_chip_priv *priv; u32 index = simple_strtoul(argv[1], NULL, 0); void *digest = map_sysmem(simple_strtoul(argv[2], NULL, 0), 0); int ret; u32 rc; if (argc != 3) return CMD_RET_USAGE; |
abdc7b8a2
|
101 |
ret = get_tpm(&dev); |
6284be5a9
|
102 103 104 105 106 107 108 109 110 |
if (ret) return ret; priv = dev_get_uclass_priv(dev); if (!priv) return -EINVAL; if (index >= priv->pcr_count) return -EINVAL; |
abdc7b8a2
|
111 |
rc = tpm2_pcr_extend(dev, index, digest); |
6284be5a9
|
112 113 114 115 116 |
unmap_sysmem(digest); return report_return_code(rc); } |
1c4ea8f49
|
117 118 119 120 121 122 123 124 125 126 127 128 |
static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { struct udevice *dev; struct tpm_chip_priv *priv; u32 index, rc; unsigned int updates; void *data; int ret; if (argc != 3) return CMD_RET_USAGE; |
abdc7b8a2
|
129 |
ret = get_tpm(&dev); |
1c4ea8f49
|
130 131 132 133 134 135 136 137 138 139 140 141 |
if (ret) return ret; priv = dev_get_uclass_priv(dev); if (!priv) return -EINVAL; index = simple_strtoul(argv[1], NULL, 0); if (index >= priv->pcr_count) return -EINVAL; data = map_sysmem(simple_strtoul(argv[2], NULL, 0), 0); |
abdc7b8a2
|
142 |
rc = tpm2_pcr_read(dev, index, priv->pcr_select_min, data, &updates); |
1c4ea8f49
|
143 |
if (!rc) { |
1d1af2ae5
|
144 145 |
printf("PCR #%u content (%u known updates): ", index, updates); |
1c4ea8f49
|
146 147 148 149 150 151 152 |
print_byte_string(data, TPM2_DIGEST_LEN); } unmap_sysmem(data); return report_return_code(rc); } |
69cd8f068
|
153 154 155 156 157 158 159 |
static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { u32 capability, property, rc; u8 *data; size_t count; int i, j; |
abdc7b8a2
|
160 161 162 163 164 165 |
struct udevice *dev; int ret; ret = get_tpm(&dev); if (ret) return ret; |
69cd8f068
|
166 167 168 169 170 171 172 173 |
if (argc != 5) return CMD_RET_USAGE; capability = simple_strtoul(argv[1], NULL, 0); property = simple_strtoul(argv[2], NULL, 0); data = map_sysmem(simple_strtoul(argv[3], NULL, 0), 0); count = simple_strtoul(argv[4], NULL, 0); |
abdc7b8a2
|
174 |
rc = tpm2_get_capability(dev, capability, property, data, count); |
69cd8f068
|
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
if (rc) goto unmap_data; printf("Capabilities read from TPM: "); for (i = 0; i < count; i++) { printf("Property 0x"); for (j = 0; j < 4; j++) printf("%02x", data[(i * 8) + j]); printf(": 0x"); for (j = 4; j < 8; j++) printf("%02x", data[(i * 8) + j]); printf(" "); } unmap_data: unmap_sysmem(data); return report_return_code(rc); } |
da9c3392e
|
196 197 198 199 200 |
static int do_tpm_dam_reset(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) { const char *pw = (argc < 2) ? NULL : argv[1]; const ssize_t pw_sz = pw ? strlen(pw) : 0; |
abdc7b8a2
|
201 202 203 204 205 206 |
struct udevice *dev; int ret; ret = get_tpm(&dev); if (ret) return ret; |
da9c3392e
|
207 208 209 210 211 212 |
if (argc > 2) return CMD_RET_USAGE; if (pw_sz > TPM2_DIGEST_LEN) return -EINVAL; |
abdc7b8a2
|
213 |
return report_return_code(tpm2_dam_reset(dev, pw, pw_sz)); |
da9c3392e
|
214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
} static int do_tpm_dam_parameters(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) { const char *pw = (argc < 5) ? NULL : argv[4]; const ssize_t pw_sz = pw ? strlen(pw) : 0; /* * No Dictionary Attack Mitigation (DAM) means: * maxtries = 0xFFFFFFFF, recovery_time = 1, lockout_recovery = 0 */ unsigned long int max_tries; unsigned long int recovery_time; unsigned long int lockout_recovery; |
abdc7b8a2
|
228 229 230 231 232 233 |
struct udevice *dev; int ret; ret = get_tpm(&dev); if (ret) return ret; |
da9c3392e
|
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
if (argc < 4 || argc > 5) return CMD_RET_USAGE; if (pw_sz > TPM2_DIGEST_LEN) return -EINVAL; if (strict_strtoul(argv[1], 0, &max_tries)) return CMD_RET_USAGE; if (strict_strtoul(argv[2], 0, &recovery_time)) return CMD_RET_USAGE; if (strict_strtoul(argv[3], 0, &lockout_recovery)) return CMD_RET_USAGE; log(LOGC_NONE, LOGL_INFO, "Changing dictionary attack parameters: "); log(LOGC_NONE, LOGL_INFO, "- maxTries: %lu", max_tries); log(LOGC_NONE, LOGL_INFO, "- recoveryTime: %lu ", recovery_time); log(LOGC_NONE, LOGL_INFO, "- lockoutRecovery: %lu ", lockout_recovery); |
abdc7b8a2
|
257 |
return report_return_code(tpm2_dam_parameters(dev, pw, pw_sz, max_tries, |
da9c3392e
|
258 259 260 |
recovery_time, lockout_recovery)); } |
dc26e913a
|
261 262 263 264 265 266 267 268 |
static int do_tpm_change_auth(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) { u32 handle; const char *newpw = argv[2]; const char *oldpw = (argc == 3) ? NULL : argv[3]; const ssize_t newpw_sz = strlen(newpw); const ssize_t oldpw_sz = oldpw ? strlen(oldpw) : 0; |
abdc7b8a2
|
269 270 271 272 273 274 |
struct udevice *dev; int ret; ret = get_tpm(&dev); if (ret) return ret; |
dc26e913a
|
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
if (argc < 3 || argc > 4) return CMD_RET_USAGE; if (newpw_sz > TPM2_DIGEST_LEN || oldpw_sz > TPM2_DIGEST_LEN) return -EINVAL; if (!strcasecmp("TPM2_RH_LOCKOUT", argv[1])) handle = TPM2_RH_LOCKOUT; else if (!strcasecmp("TPM2_RH_ENDORSEMENT", argv[1])) handle = TPM2_RH_ENDORSEMENT; else if (!strcasecmp("TPM2_RH_OWNER", argv[1])) handle = TPM2_RH_OWNER; else if (!strcasecmp("TPM2_RH_PLATFORM", argv[1])) handle = TPM2_RH_PLATFORM; else return CMD_RET_USAGE; |
abdc7b8a2
|
292 |
return report_return_code(tpm2_change_auth(dev, handle, newpw, newpw_sz, |
dc26e913a
|
293 294 |
oldpw, oldpw_sz)); } |
b9dd4fabb
|
295 296 297 298 299 300 301 |
static int do_tpm_pcr_setauthpolicy(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { u32 index = simple_strtoul(argv[1], NULL, 0); char *key = argv[2]; const char *pw = (argc < 4) ? NULL : argv[3]; const ssize_t pw_sz = pw ? strlen(pw) : 0; |
abdc7b8a2
|
302 303 304 305 306 307 |
struct udevice *dev; int ret; ret = get_tpm(&dev); if (ret) return ret; |
b9dd4fabb
|
308 309 310 311 312 313 |
if (strlen(key) != TPM2_DIGEST_LEN) return -EINVAL; if (argc < 3 || argc > 4) return CMD_RET_USAGE; |
abdc7b8a2
|
314 |
return report_return_code(tpm2_pcr_setauthpolicy(dev, pw, pw_sz, index, |
b9dd4fabb
|
315 316 317 318 319 320 321 322 323 324 325 |
key)); } static int do_tpm_pcr_setauthvalue(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { u32 index = simple_strtoul(argv[1], NULL, 0); char *key = argv[2]; const ssize_t key_sz = strlen(key); const char *pw = (argc < 4) ? NULL : argv[3]; const ssize_t pw_sz = pw ? strlen(pw) : 0; |
abdc7b8a2
|
326 327 328 329 330 331 |
struct udevice *dev; int ret; ret = get_tpm(&dev); if (ret) return ret; |
b9dd4fabb
|
332 333 334 335 336 337 |
if (strlen(key) != TPM2_DIGEST_LEN) return -EINVAL; if (argc < 3 || argc > 4) return CMD_RET_USAGE; |
abdc7b8a2
|
338 |
return report_return_code(tpm2_pcr_setauthvalue(dev, pw, pw_sz, index, |
b9dd4fabb
|
339 340 |
key, key_sz)); } |
ff32245bb
|
341 |
static cmd_tbl_t tpm2_commands[] = { |
3780e2d08
|
342 |
U_BOOT_CMD_MKENT(device, 0, 1, do_tpm_device, "", ""), |
ff32245bb
|
343 344 |
U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""), U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""), |
1922df201
|
345 |
U_BOOT_CMD_MKENT(startup, 0, 1, do_tpm2_startup, "", ""), |
2dc6d97e1
|
346 |
U_BOOT_CMD_MKENT(self_test, 0, 1, do_tpm2_self_test, "", ""), |
bad8ff569
|
347 |
U_BOOT_CMD_MKENT(clear, 0, 1, do_tpm2_clear, "", ""), |
6284be5a9
|
348 |
U_BOOT_CMD_MKENT(pcr_extend, 0, 1, do_tpm2_pcr_extend, "", ""), |
1c4ea8f49
|
349 |
U_BOOT_CMD_MKENT(pcr_read, 0, 1, do_tpm_pcr_read, "", ""), |
69cd8f068
|
350 |
U_BOOT_CMD_MKENT(get_capability, 0, 1, do_tpm_get_capability, "", ""), |
da9c3392e
|
351 352 |
U_BOOT_CMD_MKENT(dam_reset, 0, 1, do_tpm_dam_reset, "", ""), U_BOOT_CMD_MKENT(dam_parameters, 0, 1, do_tpm_dam_parameters, "", ""), |
dc26e913a
|
353 |
U_BOOT_CMD_MKENT(change_auth, 0, 1, do_tpm_change_auth, "", ""), |
b9dd4fabb
|
354 355 356 357 |
U_BOOT_CMD_MKENT(pcr_setauthpolicy, 0, 1, do_tpm_pcr_setauthpolicy, "", ""), U_BOOT_CMD_MKENT(pcr_setauthvalue, 0, 1, do_tpm_pcr_setauthvalue, "", ""), |
ff32245bb
|
358 |
}; |
2a2096ea6
|
359 |
cmd_tbl_t *get_tpm2_commands(unsigned int *size) |
ff32245bb
|
360 361 362 363 364 |
{ *size = ARRAY_SIZE(tpm2_commands); return tpm2_commands; } |
2a2096ea6
|
365 |
U_BOOT_CMD(tpm2, CONFIG_SYS_MAXARGS, 1, do_tpm, "Issue a TPMv2.x command", |
ff32245bb
|
366 367 368 369 |
"<command> [<arguments>] " " " |
3780e2d08
|
370 371 372 373 |
"device [num device] " " Show all devices or set the specified device " |
ff32245bb
|
374 375 376 377 378 379 380 381 |
"info " " Show information about the TPM. " "init " " Initialize the software stack. Always the first command to issue. " |
1922df201
|
382 383 384 385 386 387 388 389 390 391 |
"startup <mode> " " Issue a TPM2_Startup command. " " <mode> is one of: " " * TPM2_SU_CLEAR (reset state) " " * TPM2_SU_STATE (preserved state) " |
2dc6d97e1
|
392 393 394 395 396 397 398 399 400 401 |
"self_test <type> " " Test the TPM capabilities. " " <type> is one of: " " * full (perform all tests) " " * continue (only check untested tests) " |
bad8ff569
|
402 403 404 405 406 407 408 409 410 411 |
"clear <hierarchy> " " Issue a TPM2_Clear command. " " <hierarchy> is one of: " " * TPM2_RH_LOCKOUT " " * TPM2_RH_PLATFORM " |
6284be5a9
|
412 413 414 415 416 417 418 419 |
"pcr_extend <pcr> <digest_addr> " " Extend PCR #<pcr> with digest at <digest_addr>. " " <pcr>: index of the PCR " " <digest_addr>: address of a 32-byte SHA256 digest " |
1c4ea8f49
|
420 421 422 423 424 425 426 427 |
"pcr_read <pcr> <digest_addr> " " Read PCR #<pcr> to memory address <digest_addr>. " " <pcr>: index of the PCR " " <digest_addr>: address to store the a 32-byte SHA256 digest " |
69cd8f068
|
428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
"get_capability <capability> <property> <addr> <count> " " Read and display <count> entries indexed by <capability>/<property>. " " Values are 4 bytes long and are written at <addr>. " " <capability>: capability " " <property>: property " " <addr>: address to store <count> entries of 4 bytes " " <count>: number of entries to retrieve " |
dc26e913a
|
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
"dam_reset [<password>] " " If the TPM is not in a LOCKOUT state, reset the internal error counter. " " <password>: optional password " "dam_parameters <max_tries> <recovery_time> <lockout_recovery> [<password>] " " If the TPM is not in a LOCKOUT state, set the DAM parameters " " <maxTries>: maximum number of failures before lockout, " " 0 means always locking " " <recoveryTime>: time before decrement of the error counter, " " 0 means no lockout " " <lockoutRecovery>: time of a lockout (before the next try), " " 0 means a reboot is needed " " <password>: optional password of the LOCKOUT hierarchy " "change_auth <hierarchy> <new_pw> [<old_pw>] " " <hierarchy>: the hierarchy " " <new_pw>: new password for <hierarchy> " " <old_pw>: optional previous password of <hierarchy> " |
b9dd4fabb
|
474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
"pcr_setauthpolicy|pcr_setauthvalue <pcr> <key> [<password>] " " Change the <key> to access PCR #<pcr>. " " hierarchy and may be empty. " " /!\\WARNING: untested function, use at your own risks ! " " <pcr>: index of the PCR " " <key>: secret to protect the access of PCR #<pcr> " " <password>: optional password of the PLATFORM hierarchy " |
ff32245bb
|
488 |
); |