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