Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vadim Bendebury | 576fb1e | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 2 | /* |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013 The Chromium OS Authors. |
Vadim Bendebury | 576fb1e | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | c7694dd | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 7 | #include <env.h> |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 8 | #include <malloc.h> |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 9 | #include <asm/unaligned.h> |
Miquel Raynal | d677bfe | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 10 | #include <tpm-common.h> |
| 11 | #include <tpm-v1.h> |
| 12 | #include "tpm-user-utils.h" |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 13 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 14 | static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag, int argc, |
| 15 | char * const argv[]) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 16 | { |
| 17 | enum tpm_startup_type mode; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 18 | struct udevice *dev; |
| 19 | int rc; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 20 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 21 | rc = get_tpm(&dev); |
| 22 | if (rc) |
| 23 | return rc; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 24 | if (argc != 2) |
| 25 | return CMD_RET_USAGE; |
| 26 | if (!strcasecmp("TPM_ST_CLEAR", argv[1])) { |
| 27 | mode = TPM_ST_CLEAR; |
| 28 | } else if (!strcasecmp("TPM_ST_STATE", argv[1])) { |
| 29 | mode = TPM_ST_STATE; |
| 30 | } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) { |
| 31 | mode = TPM_ST_DEACTIVATED; |
| 32 | } else { |
| 33 | printf("Couldn't recognize mode string: %s\n", argv[1]); |
| 34 | return CMD_RET_FAILURE; |
| 35 | } |
| 36 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 37 | return report_return_code(tpm_startup(dev, mode)); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 40 | static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag, int argc, |
| 41 | char * const argv[]) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 42 | { |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 43 | u32 index, perm, size; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 44 | struct udevice *dev; |
| 45 | int rc; |
| 46 | |
| 47 | rc = get_tpm(&dev); |
| 48 | if (rc) |
| 49 | return rc; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 50 | |
| 51 | if (argc != 4) |
| 52 | return CMD_RET_USAGE; |
| 53 | index = simple_strtoul(argv[1], NULL, 0); |
| 54 | perm = simple_strtoul(argv[2], NULL, 0); |
| 55 | size = simple_strtoul(argv[3], NULL, 0); |
| 56 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 57 | return report_return_code(tpm_nv_define_space(dev, index, perm, size)); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 60 | static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag, int argc, |
| 61 | char * const argv[]) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 62 | { |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 63 | u32 index, count, rc; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 64 | struct udevice *dev; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 65 | void *data; |
| 66 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 67 | rc = get_tpm(&dev); |
| 68 | if (rc) |
| 69 | return rc; |
| 70 | |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 71 | if (argc != 4) |
| 72 | return CMD_RET_USAGE; |
| 73 | index = simple_strtoul(argv[1], NULL, 0); |
| 74 | data = (void *)simple_strtoul(argv[2], NULL, 0); |
| 75 | count = simple_strtoul(argv[3], NULL, 0); |
| 76 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 77 | rc = tpm_nv_read_value(dev, index, data, count); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 78 | if (!rc) { |
| 79 | puts("area content:\n"); |
| 80 | print_byte_string(data, count); |
| 81 | } |
| 82 | |
Simon Glass | f8f1fe1 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 83 | return report_return_code(rc); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 86 | static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag, int argc, |
| 87 | char * const argv[]) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 88 | { |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 89 | struct udevice *dev; |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 90 | u32 index, rc; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 91 | size_t count; |
| 92 | void *data; |
| 93 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 94 | rc = get_tpm(&dev); |
| 95 | if (rc) |
| 96 | return rc; |
| 97 | |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 98 | if (argc != 3) |
| 99 | return CMD_RET_USAGE; |
| 100 | index = simple_strtoul(argv[1], NULL, 0); |
| 101 | data = parse_byte_string(argv[2], NULL, &count); |
| 102 | if (!data) { |
| 103 | printf("Couldn't parse byte string %s\n", argv[2]); |
| 104 | return CMD_RET_FAILURE; |
| 105 | } |
| 106 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 107 | rc = tpm_nv_write_value(dev, index, data, count); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 108 | free(data); |
| 109 | |
Simon Glass | f8f1fe1 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 110 | return report_return_code(rc); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 113 | static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag, int argc, |
| 114 | char * const argv[]) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 115 | { |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 116 | u8 in_digest[20], out_digest[20]; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 117 | struct udevice *dev; |
| 118 | u32 index, rc; |
| 119 | |
| 120 | rc = get_tpm(&dev); |
| 121 | if (rc) |
| 122 | return rc; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 123 | |
| 124 | if (argc != 3) |
| 125 | return CMD_RET_USAGE; |
| 126 | index = simple_strtoul(argv[1], NULL, 0); |
| 127 | if (!parse_byte_string(argv[2], in_digest, NULL)) { |
| 128 | printf("Couldn't parse byte string %s\n", argv[2]); |
| 129 | return CMD_RET_FAILURE; |
| 130 | } |
| 131 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 132 | rc = tpm_extend(dev, index, in_digest, out_digest); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 133 | if (!rc) { |
| 134 | puts("PCR value after execution of the command:\n"); |
| 135 | print_byte_string(out_digest, sizeof(out_digest)); |
| 136 | } |
| 137 | |
Simon Glass | f8f1fe1 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 138 | return report_return_code(rc); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 141 | static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag, int argc, |
| 142 | char * const argv[]) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 143 | { |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 144 | u32 index, count, rc; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 145 | struct udevice *dev; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 146 | void *data; |
| 147 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 148 | rc = get_tpm(&dev); |
| 149 | if (rc) |
| 150 | return rc; |
| 151 | |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 152 | if (argc != 4) |
| 153 | return CMD_RET_USAGE; |
| 154 | index = simple_strtoul(argv[1], NULL, 0); |
| 155 | data = (void *)simple_strtoul(argv[2], NULL, 0); |
| 156 | count = simple_strtoul(argv[3], NULL, 0); |
| 157 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 158 | rc = tpm_pcr_read(dev, index, data, count); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 159 | if (!rc) { |
| 160 | puts("Named PCR content:\n"); |
| 161 | print_byte_string(data, count); |
| 162 | } |
| 163 | |
Simon Glass | f8f1fe1 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 164 | return report_return_code(rc); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 167 | static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag, int argc, |
| 168 | char * const argv[]) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 169 | { |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 170 | struct udevice *dev; |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 171 | u16 presence; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 172 | int rc; |
| 173 | |
| 174 | rc = get_tpm(&dev); |
| 175 | if (rc) |
| 176 | return rc; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 177 | |
| 178 | if (argc != 2) |
| 179 | return CMD_RET_USAGE; |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 180 | presence = (u16)simple_strtoul(argv[1], NULL, 0); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 181 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 182 | return report_return_code(tpm_tsc_physical_presence(dev, presence)); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 185 | static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag, int argc, |
| 186 | char * const argv[]) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 187 | { |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 188 | struct udevice *dev; |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 189 | u32 count, rc; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 190 | void *data; |
| 191 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 192 | rc = get_tpm(&dev); |
| 193 | if (rc) |
| 194 | return rc; |
| 195 | |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 196 | if (argc != 3) |
| 197 | return CMD_RET_USAGE; |
| 198 | data = (void *)simple_strtoul(argv[1], NULL, 0); |
| 199 | count = simple_strtoul(argv[2], NULL, 0); |
| 200 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 201 | rc = tpm_read_pubek(dev, data, count); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 202 | if (!rc) { |
| 203 | puts("pubek value:\n"); |
| 204 | print_byte_string(data, count); |
| 205 | } |
| 206 | |
Simon Glass | f8f1fe1 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 207 | return report_return_code(rc); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 210 | static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag, int argc, |
| 211 | char * const argv[]) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 212 | { |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 213 | struct udevice *dev; |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 214 | u8 state; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 215 | int rc; |
| 216 | |
| 217 | rc = get_tpm(&dev); |
| 218 | if (rc) |
| 219 | return rc; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 220 | |
| 221 | if (argc != 2) |
| 222 | return CMD_RET_USAGE; |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 223 | state = (u8)simple_strtoul(argv[1], NULL, 0); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 224 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 225 | return report_return_code(tpm_physical_set_deactivated(dev, state)); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 228 | static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag, int argc, |
| 229 | char * const argv[]) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 230 | { |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 231 | u32 cap_area, sub_cap, rc; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 232 | void *cap; |
| 233 | size_t count; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 234 | struct udevice *dev; |
| 235 | |
| 236 | rc = get_tpm(&dev); |
| 237 | if (rc) |
| 238 | return rc; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 239 | |
| 240 | if (argc != 5) |
| 241 | return CMD_RET_USAGE; |
| 242 | cap_area = simple_strtoul(argv[1], NULL, 0); |
| 243 | sub_cap = simple_strtoul(argv[2], NULL, 0); |
| 244 | cap = (void *)simple_strtoul(argv[3], NULL, 0); |
| 245 | count = simple_strtoul(argv[4], NULL, 0); |
| 246 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 247 | rc = tpm_get_capability(dev, cap_area, sub_cap, cap, count); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 248 | if (!rc) { |
| 249 | puts("capability information:\n"); |
| 250 | print_byte_string(cap, count); |
| 251 | } |
| 252 | |
Simon Glass | f8f1fe1 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 253 | return report_return_code(rc); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 256 | static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag, int argc, |
| 257 | char * const argv[]) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 258 | { |
Christophe Ricard | c2b0f60 | 2015-10-06 22:54:43 +0200 | [diff] [blame] | 259 | struct udevice *dev; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 260 | void *command; |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 261 | u8 response[1024]; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 262 | size_t count, response_length = sizeof(response); |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 263 | u32 rc; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 264 | |
| 265 | command = parse_byte_string(argv[1], NULL, &count); |
| 266 | if (!command) { |
| 267 | printf("Couldn't parse byte string %s\n", argv[1]); |
| 268 | return CMD_RET_FAILURE; |
| 269 | } |
| 270 | |
Simon Glass | c8a8c51 | 2015-08-22 18:31:32 -0600 | [diff] [blame] | 271 | rc = get_tpm(&dev); |
| 272 | if (rc) |
| 273 | return rc; |
| 274 | |
| 275 | rc = tpm_xfer(dev, command, count, response, &response_length); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 276 | free(command); |
| 277 | if (!rc) { |
| 278 | puts("tpm response:\n"); |
| 279 | print_byte_string(response, response_length); |
| 280 | } |
| 281 | |
Simon Glass | f8f1fe1 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 282 | return report_return_code(rc); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 285 | static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag, int argc, |
| 286 | char * const argv[]) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 287 | { |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 288 | u32 index, perm, size; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 289 | struct udevice *dev; |
| 290 | int rc; |
| 291 | |
| 292 | rc = get_tpm(&dev); |
| 293 | if (rc) |
| 294 | return rc; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 295 | |
| 296 | if (argc != 4) |
| 297 | return CMD_RET_USAGE; |
| 298 | size = type_string_get_space_size(argv[1]); |
| 299 | if (!size) { |
| 300 | printf("Couldn't parse arguments\n"); |
| 301 | return CMD_RET_USAGE; |
| 302 | } |
| 303 | index = simple_strtoul(argv[2], NULL, 0); |
| 304 | perm = simple_strtoul(argv[3], NULL, 0); |
| 305 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 306 | return report_return_code(tpm_nv_define_space(dev, index, perm, size)); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 309 | static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag, int argc, |
| 310 | char * const argv[]) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 311 | { |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 312 | u32 index, count, err; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 313 | struct udevice *dev; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 314 | void *data; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 315 | int rc; |
| 316 | |
| 317 | rc = get_tpm(&dev); |
| 318 | if (rc) |
| 319 | return rc; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 320 | |
| 321 | if (argc < 3) |
| 322 | return CMD_RET_USAGE; |
| 323 | if (argc != 3 + type_string_get_num_values(argv[1])) |
| 324 | return CMD_RET_USAGE; |
| 325 | index = simple_strtoul(argv[2], NULL, 0); |
| 326 | data = type_string_alloc(argv[1], &count); |
| 327 | if (!data) { |
| 328 | printf("Couldn't parse arguments\n"); |
| 329 | return CMD_RET_USAGE; |
| 330 | } |
| 331 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 332 | err = tpm_nv_read_value(dev, index, data, count); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 333 | if (!err) { |
| 334 | if (type_string_write_vars(argv[1], data, argv + 3)) { |
| 335 | printf("Couldn't write to variables\n"); |
| 336 | err = ~0; |
| 337 | } |
| 338 | } |
| 339 | free(data); |
| 340 | |
Simon Glass | f8f1fe1 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 341 | return report_return_code(err); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 344 | static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag, int argc, |
| 345 | char * const argv[]) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 346 | { |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 347 | u32 index, count, err; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 348 | struct udevice *dev; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 349 | void *data; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 350 | int rc; |
| 351 | |
| 352 | rc = get_tpm(&dev); |
| 353 | if (rc) |
| 354 | return rc; |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 355 | |
| 356 | if (argc < 3) |
| 357 | return CMD_RET_USAGE; |
| 358 | if (argc != 3 + type_string_get_num_values(argv[1])) |
| 359 | return CMD_RET_USAGE; |
| 360 | index = simple_strtoul(argv[2], NULL, 0); |
| 361 | data = type_string_alloc(argv[1], &count); |
| 362 | if (!data) { |
| 363 | printf("Couldn't parse arguments\n"); |
| 364 | return CMD_RET_USAGE; |
| 365 | } |
| 366 | if (type_string_pack(argv[1], argv + 3, data)) { |
| 367 | printf("Couldn't parse arguments\n"); |
| 368 | free(data); |
| 369 | return CMD_RET_USAGE; |
| 370 | } |
| 371 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 372 | err = tpm_nv_write_value(dev, index, data, count); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 373 | free(data); |
| 374 | |
Simon Glass | f8f1fe1 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 375 | return report_return_code(err); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 378 | #ifdef CONFIG_TPM_AUTH_SESSIONS |
| 379 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 380 | static int do_tpm_oiap(cmd_tbl_t *cmdtp, int flag, int argc, |
| 381 | char * const argv[]) |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 382 | { |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 383 | u32 auth_handle, err; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 384 | struct udevice *dev; |
| 385 | int rc; |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 386 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 387 | rc = get_tpm(&dev); |
| 388 | if (rc) |
| 389 | return rc; |
| 390 | |
| 391 | err = tpm_oiap(dev, &auth_handle); |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 392 | |
Simon Glass | f8f1fe1 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 393 | return report_return_code(err); |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 394 | } |
| 395 | |
mario.six@gdsys.cc | 0f4b2ba | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 396 | #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 |
| 397 | static int do_tpm_load_key_by_sha1(cmd_tbl_t *cmdtp, int flag, int argc, char * |
| 398 | const argv[]) |
| 399 | { |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 400 | u32 parent_handle = 0; |
| 401 | u32 key_len, key_handle, err; |
| 402 | u8 usage_auth[DIGEST_LENGTH]; |
| 403 | u8 parent_hash[DIGEST_LENGTH]; |
mario.six@gdsys.cc | 0f4b2ba | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 404 | void *key; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 405 | struct udevice *dev; |
| 406 | |
| 407 | rc = get_tpm(&dev); |
| 408 | if (rc) |
| 409 | return rc; |
mario.six@gdsys.cc | 0f4b2ba | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 410 | |
| 411 | if (argc < 5) |
| 412 | return CMD_RET_USAGE; |
| 413 | |
| 414 | parse_byte_string(argv[1], parent_hash, NULL); |
| 415 | key = (void *)simple_strtoul(argv[2], NULL, 0); |
| 416 | key_len = simple_strtoul(argv[3], NULL, 0); |
| 417 | if (strlen(argv[4]) != 2 * DIGEST_LENGTH) |
| 418 | return CMD_RET_FAILURE; |
| 419 | parse_byte_string(argv[4], usage_auth, NULL); |
| 420 | |
| 421 | err = tpm_find_key_sha1(usage_auth, parent_hash, &parent_handle); |
| 422 | if (err) { |
| 423 | printf("Could not find matching parent key (err = %d)\n", err); |
| 424 | return CMD_RET_FAILURE; |
| 425 | } |
| 426 | |
| 427 | printf("Found parent key %08x\n", parent_handle); |
| 428 | |
| 429 | err = tpm_load_key2_oiap(parent_handle, key, key_len, usage_auth, |
| 430 | &key_handle); |
| 431 | if (!err) { |
| 432 | printf("Key handle is 0x%x\n", key_handle); |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 433 | env_set_hex("key_handle", key_handle); |
mario.six@gdsys.cc | 0f4b2ba | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | return report_return_code(err); |
| 437 | } |
| 438 | #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ |
| 439 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 440 | static int do_tpm_load_key2_oiap(cmd_tbl_t *cmdtp, int flag, int argc, |
| 441 | char * const argv[]) |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 442 | { |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 443 | u32 parent_handle, key_len, key_handle, err; |
| 444 | u8 usage_auth[DIGEST_LENGTH]; |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 445 | void *key; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 446 | struct udevice *dev; |
| 447 | int rc; |
| 448 | |
| 449 | rc = get_tpm(&dev); |
| 450 | if (rc) |
| 451 | return rc; |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 452 | |
| 453 | if (argc < 5) |
| 454 | return CMD_RET_USAGE; |
| 455 | |
| 456 | parent_handle = simple_strtoul(argv[1], NULL, 0); |
| 457 | key = (void *)simple_strtoul(argv[2], NULL, 0); |
| 458 | key_len = simple_strtoul(argv[3], NULL, 0); |
| 459 | if (strlen(argv[4]) != 2 * DIGEST_LENGTH) |
| 460 | return CMD_RET_FAILURE; |
| 461 | parse_byte_string(argv[4], usage_auth, NULL); |
| 462 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 463 | err = tpm_load_key2_oiap(dev, parent_handle, key, key_len, usage_auth, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 464 | &key_handle); |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 465 | if (!err) |
| 466 | printf("Key handle is 0x%x\n", key_handle); |
| 467 | |
Simon Glass | f8f1fe1 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 468 | return report_return_code(err); |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 471 | static int do_tpm_get_pub_key_oiap(cmd_tbl_t *cmdtp, int flag, int argc, |
| 472 | char * const argv[]) |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 473 | { |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 474 | u32 key_handle, err; |
| 475 | u8 usage_auth[DIGEST_LENGTH]; |
| 476 | u8 pub_key_buffer[TPM_PUBKEY_MAX_LENGTH]; |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 477 | size_t pub_key_len = sizeof(pub_key_buffer); |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 478 | struct udevice *dev; |
| 479 | int rc; |
| 480 | |
| 481 | rc = get_tpm(&dev); |
| 482 | if (rc) |
| 483 | return rc; |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 484 | |
| 485 | if (argc < 3) |
| 486 | return CMD_RET_USAGE; |
| 487 | |
| 488 | key_handle = simple_strtoul(argv[1], NULL, 0); |
| 489 | if (strlen(argv[2]) != 2 * DIGEST_LENGTH) |
| 490 | return CMD_RET_FAILURE; |
| 491 | parse_byte_string(argv[2], usage_auth, NULL); |
| 492 | |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 493 | err = tpm_get_pub_key_oiap(dev, key_handle, usage_auth, pub_key_buffer, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 494 | &pub_key_len); |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 495 | if (!err) { |
| 496 | printf("dump of received pub key structure:\n"); |
| 497 | print_byte_string(pub_key_buffer, pub_key_len); |
| 498 | } |
Simon Glass | f8f1fe1 | 2015-08-22 18:31:34 -0600 | [diff] [blame] | 499 | return report_return_code(err); |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | TPM_COMMAND_NO_ARG(tpm_end_oiap) |
| 503 | |
| 504 | #endif /* CONFIG_TPM_AUTH_SESSIONS */ |
| 505 | |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 506 | #ifdef CONFIG_TPM_FLUSH_RESOURCES |
| 507 | static int do_tpm_flush(cmd_tbl_t *cmdtp, int flag, int argc, |
| 508 | char * const argv[]) |
| 509 | { |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 510 | struct udevice *dev; |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 511 | int type = 0; |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 512 | int rc; |
| 513 | |
| 514 | rc = get_tpm(&dev); |
| 515 | if (rc) |
| 516 | return rc; |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 517 | |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 518 | if (argc != 3) |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 519 | return CMD_RET_USAGE; |
| 520 | |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 521 | if (!strcasecmp(argv[1], "key")) |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 522 | type = TPM_RT_KEY; |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 523 | else if (!strcasecmp(argv[1], "auth")) |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 524 | type = TPM_RT_AUTH; |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 525 | else if (!strcasecmp(argv[1], "hash")) |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 526 | type = TPM_RT_HASH; |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 527 | else if (!strcasecmp(argv[1], "trans")) |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 528 | type = TPM_RT_TRANS; |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 529 | else if (!strcasecmp(argv[1], "context")) |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 530 | type = TPM_RT_CONTEXT; |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 531 | else if (!strcasecmp(argv[1], "counter")) |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 532 | type = TPM_RT_COUNTER; |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 533 | else if (!strcasecmp(argv[1], "delegate")) |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 534 | type = TPM_RT_DELEGATE; |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 535 | else if (!strcasecmp(argv[1], "daa_tpm")) |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 536 | type = TPM_RT_DAA_TPM; |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 537 | else if (!strcasecmp(argv[1], "daa_v0")) |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 538 | type = TPM_RT_DAA_V0; |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 539 | else if (!strcasecmp(argv[1], "daa_v1")) |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 540 | type = TPM_RT_DAA_V1; |
| 541 | |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 542 | if (!type) { |
| 543 | printf("Resource type %s unknown.\n", argv[1]); |
| 544 | return -1; |
| 545 | } |
| 546 | |
| 547 | if (!strcasecmp(argv[2], "all")) { |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 548 | u16 res_count; |
| 549 | u8 buf[288]; |
| 550 | u8 *ptr; |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 551 | int err; |
| 552 | uint i; |
| 553 | |
| 554 | /* fetch list of already loaded resources in the TPM */ |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 555 | err = tpm_get_capability(dev, TPM_CAP_HANDLE, type, buf, |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 556 | sizeof(buf)); |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 557 | if (err) { |
| 558 | printf("tpm_get_capability returned error %d.\n", err); |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 559 | return -1; |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 560 | } |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 561 | res_count = get_unaligned_be16(buf); |
| 562 | ptr = buf + 2; |
| 563 | for (i = 0; i < res_count; ++i, ptr += 4) |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 564 | tpm_flush_specific(dev, get_unaligned_be32(ptr), type); |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 565 | } else { |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 566 | u32 handle = simple_strtoul(argv[2], NULL, 0); |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 567 | |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 568 | if (!handle) { |
| 569 | printf("Illegal resource handle %s\n", argv[2]); |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 570 | return -1; |
mario.six@gdsys.cc | 1c08b21 | 2017-03-20 10:28:29 +0100 | [diff] [blame] | 571 | } |
Simon Glass | abdc7b8 | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 572 | tpm_flush_specific(dev, cpu_to_be32(handle), type); |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | #endif /* CONFIG_TPM_FLUSH_RESOURCES */ |
| 578 | |
mario.six@gdsys.cc | 3d1df0e | 2017-03-20 10:28:30 +0100 | [diff] [blame] | 579 | #ifdef CONFIG_TPM_LIST_RESOURCES |
| 580 | static int do_tpm_list(cmd_tbl_t *cmdtp, int flag, int argc, |
| 581 | char * const argv[]) |
| 582 | { |
| 583 | int type = 0; |
Miquel Raynal | b9804e5 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 584 | u16 res_count; |
| 585 | u8 buf[288]; |
| 586 | u8 *ptr; |
mario.six@gdsys.cc | 3d1df0e | 2017-03-20 10:28:30 +0100 | [diff] [blame] | 587 | int err; |
| 588 | uint i; |
| 589 | |
| 590 | if (argc != 2) |
| 591 | return CMD_RET_USAGE; |
| 592 | |
| 593 | if (!strcasecmp(argv[1], "key")) |
| 594 | type = TPM_RT_KEY; |
| 595 | else if (!strcasecmp(argv[1], "auth")) |
| 596 | type = TPM_RT_AUTH; |
| 597 | else if (!strcasecmp(argv[1], "hash")) |
| 598 | type = TPM_RT_HASH; |
| 599 | else if (!strcasecmp(argv[1], "trans")) |
| 600 | type = TPM_RT_TRANS; |
| 601 | else if (!strcasecmp(argv[1], "context")) |
| 602 | type = TPM_RT_CONTEXT; |
| 603 | else if (!strcasecmp(argv[1], "counter")) |
| 604 | type = TPM_RT_COUNTER; |
| 605 | else if (!strcasecmp(argv[1], "delegate")) |
| 606 | type = TPM_RT_DELEGATE; |
| 607 | else if (!strcasecmp(argv[1], "daa_tpm")) |
| 608 | type = TPM_RT_DAA_TPM; |
| 609 | else if (!strcasecmp(argv[1], "daa_v0")) |
| 610 | type = TPM_RT_DAA_V0; |
| 611 | else if (!strcasecmp(argv[1], "daa_v1")) |
| 612 | type = TPM_RT_DAA_V1; |
| 613 | |
| 614 | if (!type) { |
| 615 | printf("Resource type %s unknown.\n", argv[1]); |
| 616 | return -1; |
| 617 | } |
| 618 | |
| 619 | /* fetch list of already loaded resources in the TPM */ |
| 620 | err = tpm_get_capability(TPM_CAP_HANDLE, type, buf, |
| 621 | sizeof(buf)); |
| 622 | if (err) { |
| 623 | printf("tpm_get_capability returned error %d.\n", err); |
| 624 | return -1; |
| 625 | } |
| 626 | res_count = get_unaligned_be16(buf); |
| 627 | ptr = buf + 2; |
| 628 | |
| 629 | printf("Resources of type %s (%02x):\n", argv[1], type); |
| 630 | if (!res_count) { |
| 631 | puts("None\n"); |
| 632 | } else { |
| 633 | for (i = 0; i < res_count; ++i, ptr += 4) |
| 634 | printf("Index %d: %08x\n", i, get_unaligned_be32(ptr)); |
| 635 | } |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | #endif /* CONFIG_TPM_LIST_RESOURCES */ |
| 640 | |
Miquel Raynal | d677bfe | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 641 | TPM_COMMAND_NO_ARG(tpm_self_test_full) |
| 642 | TPM_COMMAND_NO_ARG(tpm_continue_self_test) |
| 643 | TPM_COMMAND_NO_ARG(tpm_force_clear) |
| 644 | TPM_COMMAND_NO_ARG(tpm_physical_enable) |
| 645 | TPM_COMMAND_NO_ARG(tpm_physical_disable) |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 646 | |
Miquel Raynal | d677bfe | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 647 | static cmd_tbl_t tpm1_commands[] = { |
Simon Glass | ad77694 | 2015-08-22 18:31:40 -0600 | [diff] [blame] | 648 | U_BOOT_CMD_MKENT(info, 0, 1, do_tpm_info, "", ""), |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 649 | U_BOOT_CMD_MKENT(init, 0, 1, do_tpm_init, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 650 | U_BOOT_CMD_MKENT(startup, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 651 | do_tpm_startup, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 652 | U_BOOT_CMD_MKENT(self_test_full, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 653 | do_tpm_self_test_full, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 654 | U_BOOT_CMD_MKENT(continue_self_test, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 655 | do_tpm_continue_self_test, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 656 | U_BOOT_CMD_MKENT(force_clear, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 657 | do_tpm_force_clear, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 658 | U_BOOT_CMD_MKENT(physical_enable, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 659 | do_tpm_physical_enable, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 660 | U_BOOT_CMD_MKENT(physical_disable, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 661 | do_tpm_physical_disable, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 662 | U_BOOT_CMD_MKENT(nv_define_space, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 663 | do_tpm_nv_define_space, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 664 | U_BOOT_CMD_MKENT(nv_read_value, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 665 | do_tpm_nv_read_value, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 666 | U_BOOT_CMD_MKENT(nv_write_value, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 667 | do_tpm_nv_write_value, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 668 | U_BOOT_CMD_MKENT(extend, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 669 | do_tpm_extend, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 670 | U_BOOT_CMD_MKENT(pcr_read, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 671 | do_tpm_pcr_read, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 672 | U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 673 | do_tpm_tsc_physical_presence, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 674 | U_BOOT_CMD_MKENT(read_pubek, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 675 | do_tpm_read_pubek, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 676 | U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 677 | do_tpm_physical_set_deactivated, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 678 | U_BOOT_CMD_MKENT(get_capability, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 679 | do_tpm_get_capability, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 680 | U_BOOT_CMD_MKENT(raw_transfer, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 681 | do_tpm_raw_transfer, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 682 | U_BOOT_CMD_MKENT(nv_define, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 683 | do_tpm_nv_define, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 684 | U_BOOT_CMD_MKENT(nv_read, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 685 | do_tpm_nv_read, "", ""), |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 686 | U_BOOT_CMD_MKENT(nv_write, 0, 1, |
Miquel Raynal | c617918 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 687 | do_tpm_nv_write, "", ""), |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 688 | #ifdef CONFIG_TPM_AUTH_SESSIONS |
| 689 | U_BOOT_CMD_MKENT(oiap, 0, 1, |
| 690 | do_tpm_oiap, "", ""), |
| 691 | U_BOOT_CMD_MKENT(end_oiap, 0, 1, |
| 692 | do_tpm_end_oiap, "", ""), |
| 693 | U_BOOT_CMD_MKENT(load_key2_oiap, 0, 1, |
| 694 | do_tpm_load_key2_oiap, "", ""), |
mario.six@gdsys.cc | 0f4b2ba | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 695 | #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 |
| 696 | U_BOOT_CMD_MKENT(load_key_by_sha1, 0, 1, |
| 697 | do_tpm_load_key_by_sha1, "", ""), |
| 698 | #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 699 | U_BOOT_CMD_MKENT(get_pub_key_oiap, 0, 1, |
| 700 | do_tpm_get_pub_key_oiap, "", ""), |
| 701 | #endif /* CONFIG_TPM_AUTH_SESSIONS */ |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 702 | #ifdef CONFIG_TPM_FLUSH_RESOURCES |
| 703 | U_BOOT_CMD_MKENT(flush, 0, 1, |
| 704 | do_tpm_flush, "", ""), |
| 705 | #endif /* CONFIG_TPM_FLUSH_RESOURCES */ |
mario.six@gdsys.cc | 3d1df0e | 2017-03-20 10:28:30 +0100 | [diff] [blame] | 706 | #ifdef CONFIG_TPM_LIST_RESOURCES |
| 707 | U_BOOT_CMD_MKENT(list, 0, 1, |
| 708 | do_tpm_list, "", ""), |
| 709 | #endif /* CONFIG_TPM_LIST_RESOURCES */ |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 710 | }; |
Luigi Semenzato | eea3f4d | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 711 | |
Miquel Raynal | 2a2096e | 2018-07-19 22:35:09 +0200 | [diff] [blame] | 712 | cmd_tbl_t *get_tpm1_commands(unsigned int *size) |
Luigi Semenzato | eea3f4d | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 713 | { |
Miquel Raynal | d677bfe | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 714 | *size = ARRAY_SIZE(tpm1_commands); |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 715 | |
Miquel Raynal | d677bfe | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 716 | return tpm1_commands; |
Luigi Semenzato | eea3f4d | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 719 | U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm, |
Miquel Raynal | d677bfe | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 720 | "Issue a TPMv1.x command", |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 721 | "cmd args...\n" |
| 722 | " - Issue TPM command <cmd> with arguments <args...>.\n" |
| 723 | "Admin Startup and State Commands:\n" |
Simon Glass | ad77694 | 2015-08-22 18:31:40 -0600 | [diff] [blame] | 724 | " info - Show information about the TPM\n" |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 725 | " init\n" |
| 726 | " - Put TPM into a state where it waits for 'startup' command.\n" |
| 727 | " startup mode\n" |
| 728 | " - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n" |
| 729 | " TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n" |
| 730 | "Admin Testing Commands:\n" |
| 731 | " self_test_full\n" |
| 732 | " - Test all of the TPM capabilities.\n" |
| 733 | " continue_self_test\n" |
| 734 | " - Inform TPM that it should complete the self-test.\n" |
| 735 | "Admin Opt-in Commands:\n" |
| 736 | " physical_enable\n" |
| 737 | " - Set the PERMANENT disable flag to FALSE using physical presence as\n" |
| 738 | " authorization.\n" |
| 739 | " physical_disable\n" |
| 740 | " - Set the PERMANENT disable flag to TRUE using physical presence as\n" |
| 741 | " authorization.\n" |
| 742 | " physical_set_deactivated 0|1\n" |
| 743 | " - Set deactivated flag.\n" |
| 744 | "Admin Ownership Commands:\n" |
| 745 | " force_clear\n" |
| 746 | " - Issue TPM_ForceClear command.\n" |
| 747 | " tsc_physical_presence flags\n" |
| 748 | " - Set TPM device's Physical Presence flags to <flags>.\n" |
| 749 | "The Capability Commands:\n" |
| 750 | " get_capability cap_area sub_cap addr count\n" |
| 751 | " - Read <count> bytes of TPM capability indexed by <cap_area> and\n" |
| 752 | " <sub_cap> to memory address <addr>.\n" |
mario.six@gdsys.cc | 3d1df0e | 2017-03-20 10:28:30 +0100 | [diff] [blame] | 753 | #if defined(CONFIG_TPM_FLUSH_RESOURCES) || defined(CONFIG_TPM_LIST_RESOURCES) |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 754 | "Resource management functions\n" |
mario.six@gdsys.cc | 3d1df0e | 2017-03-20 10:28:30 +0100 | [diff] [blame] | 755 | #endif |
| 756 | #ifdef CONFIG_TPM_FLUSH_RESOURCES |
Mario Six | 7690be3 | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 757 | " flush resource_type id\n" |
| 758 | " - flushes a resource of type <resource_type> (may be one of key, auth,\n" |
| 759 | " hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n" |
| 760 | " and id <id> from the TPM. Use an <id> of \"all\" to flush all\n" |
| 761 | " resources of that type.\n" |
| 762 | #endif /* CONFIG_TPM_FLUSH_RESOURCES */ |
mario.six@gdsys.cc | 3d1df0e | 2017-03-20 10:28:30 +0100 | [diff] [blame] | 763 | #ifdef CONFIG_TPM_LIST_RESOURCES |
| 764 | " list resource_type\n" |
| 765 | " - lists resources of type <resource_type> (may be one of key, auth,\n" |
| 766 | " hash, trans, context, counter, delegate, daa_tpm, daa_v0, daa_v1),\n" |
| 767 | " contained in the TPM.\n" |
| 768 | #endif /* CONFIG_TPM_LIST_RESOURCES */ |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 769 | #ifdef CONFIG_TPM_AUTH_SESSIONS |
| 770 | "Storage functions\n" |
| 771 | " loadkey2_oiap parent_handle key_addr key_len usage_auth\n" |
| 772 | " - loads a key data from memory address <key_addr>, <key_len> bytes\n" |
| 773 | " into TPM using the parent key <parent_handle> with authorization\n" |
| 774 | " <usage_auth> (20 bytes hex string).\n" |
mario.six@gdsys.cc | 0f4b2ba | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 775 | #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 |
| 776 | " load_key_by_sha1 parent_hash key_addr key_len usage_auth\n" |
| 777 | " - loads a key data from memory address <key_addr>, <key_len> bytes\n" |
| 778 | " into TPM using the parent hash <parent_hash> (20 bytes hex string)\n" |
| 779 | " with authorization <usage_auth> (20 bytes hex string).\n" |
| 780 | #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 781 | " get_pub_key_oiap key_handle usage_auth\n" |
| 782 | " - get the public key portion of a loaded key <key_handle> using\n" |
| 783 | " authorization <usage auth> (20 bytes hex string)\n" |
| 784 | #endif /* CONFIG_TPM_AUTH_SESSIONS */ |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 785 | "Endorsement Key Handling Commands:\n" |
| 786 | " read_pubek addr count\n" |
| 787 | " - Read <count> bytes of the public endorsement key to memory\n" |
| 788 | " address <addr>\n" |
| 789 | "Integrity Collection and Reporting Commands:\n" |
| 790 | " extend index digest_hex_string\n" |
| 791 | " - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n" |
| 792 | " <digest_hex_string>\n" |
| 793 | " pcr_read index addr count\n" |
| 794 | " - Read <count> bytes from PCR <index> to memory address <addr>.\n" |
Reinhard Pfau | be6c152 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 795 | #ifdef CONFIG_TPM_AUTH_SESSIONS |
| 796 | "Authorization Sessions\n" |
| 797 | " oiap\n" |
| 798 | " - setup an OIAP session\n" |
| 799 | " end_oiap\n" |
| 800 | " - terminates an active OIAP session\n" |
| 801 | #endif /* CONFIG_TPM_AUTH_SESSIONS */ |
Che-liang Chiou | 8732b07 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 802 | "Non-volatile Storage Commands:\n" |
| 803 | " nv_define_space index permission size\n" |
| 804 | " - Establish a space at index <index> with <permission> of <size> bytes.\n" |
| 805 | " nv_read_value index addr count\n" |
| 806 | " - Read <count> bytes from space <index> to memory address <addr>.\n" |
| 807 | " nv_write_value index addr count\n" |
| 808 | " - Write <count> bytes from memory address <addr> to space <index>.\n" |
| 809 | "Miscellaneous helper functions:\n" |
| 810 | " raw_transfer byte_string\n" |
| 811 | " - Send a byte string <byte_string> to TPM and print the response.\n" |
| 812 | " Non-volatile storage helper functions:\n" |
| 813 | " These helper functions treat a non-volatile space as a non-padded\n" |
| 814 | " sequence of integer values. These integer values are defined by a type\n" |
| 815 | " string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n" |
| 816 | " value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n" |
| 817 | " a type string as their first argument.\n" |
| 818 | " nv_define type_string index perm\n" |
| 819 | " - Define a space <index> with permission <perm>.\n" |
| 820 | " nv_read types_string index vars...\n" |
| 821 | " - Read from space <index> to environment variables <vars...>.\n" |
| 822 | " nv_write types_string index values...\n" |
| 823 | " - Write to space <index> from values <values...>.\n" |
Luigi Semenzato | eea3f4d | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 824 | ); |