Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 1 | /* |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013 The Chromium OS Authors. |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 3 | * |
| 4 | * See file CREDITS for list of people who contributed to this |
| 5 | * project. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <common.h> |
| 24 | #include <command.h> |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 25 | #include <malloc.h> |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 26 | #include <tpm.h> |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 27 | #include <asm/unaligned.h> |
| 28 | #include <linux/string.h> |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 29 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 30 | /** |
| 31 | * Print a byte string in hexdecimal format, 16-bytes per line. |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 32 | * |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 33 | * @param data byte string to be printed |
| 34 | * @param count number of bytes to be printed |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 35 | */ |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 36 | static void print_byte_string(uint8_t *data, size_t count) |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 37 | { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 38 | int i, print_newline = 0; |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 39 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 40 | for (i = 0; i < count; i++) { |
| 41 | printf(" %02x", data[i]); |
| 42 | print_newline = (i % 16 == 15); |
| 43 | if (print_newline) |
| 44 | putc('\n'); |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 45 | } |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 46 | /* Avoid duplicated newline at the end */ |
| 47 | if (!print_newline) |
| 48 | putc('\n'); |
| 49 | } |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 50 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 51 | /** |
| 52 | * Convert a text string of hexdecimal values into a byte string. |
| 53 | * |
| 54 | * @param bytes text string of hexdecimal values with no space |
| 55 | * between them |
| 56 | * @param data output buffer for byte string. The caller has to make |
| 57 | * sure it is large enough for storing the output. If |
| 58 | * NULL is passed, a large enough buffer will be allocated, |
| 59 | * and the caller must free it. |
| 60 | * @param count_ptr output variable for the length of byte string |
| 61 | * @return pointer to output buffer |
| 62 | */ |
| 63 | static void *parse_byte_string(char *bytes, uint8_t *data, size_t *count_ptr) |
| 64 | { |
| 65 | char byte[3]; |
| 66 | size_t count, length; |
| 67 | int i; |
| 68 | |
| 69 | length = strlen(bytes); |
| 70 | count = length / 2; |
| 71 | |
| 72 | if (!data) |
| 73 | data = malloc(count); |
| 74 | if (!data) |
| 75 | return NULL; |
| 76 | |
| 77 | byte[2] = '\0'; |
| 78 | for (i = 0; i < length; i += 2) { |
| 79 | byte[0] = bytes[i]; |
| 80 | byte[1] = bytes[i + 1]; |
| 81 | data[i / 2] = (uint8_t)simple_strtoul(byte, NULL, 16); |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 82 | } |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 83 | |
| 84 | if (count_ptr) |
| 85 | *count_ptr = count; |
| 86 | |
| 87 | return data; |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 90 | /** |
| 91 | * Convert TPM command return code to U-Boot command error codes. |
| 92 | * |
| 93 | * @param return_code TPM command return code |
| 94 | * @return value of enum command_ret_t |
| 95 | */ |
| 96 | static int convert_return_code(uint32_t return_code) |
| 97 | { |
| 98 | if (return_code) |
| 99 | return CMD_RET_FAILURE; |
| 100 | else |
| 101 | return CMD_RET_SUCCESS; |
| 102 | } |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 103 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 104 | /** |
| 105 | * Return number of values defined by a type string. |
| 106 | * |
| 107 | * @param type_str type string |
| 108 | * @return number of values of type string |
| 109 | */ |
| 110 | static int type_string_get_num_values(const char *type_str) |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 111 | { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 112 | return strlen(type_str); |
| 113 | } |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 114 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 115 | /** |
| 116 | * Return total size of values defined by a type string. |
| 117 | * |
| 118 | * @param type_str type string |
| 119 | * @return total size of values of type string, or 0 if type string |
| 120 | * contains illegal type character. |
| 121 | */ |
| 122 | static size_t type_string_get_space_size(const char *type_str) |
| 123 | { |
| 124 | size_t size; |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 125 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 126 | for (size = 0; *type_str; type_str++) { |
| 127 | switch (*type_str) { |
| 128 | case 'b': |
| 129 | size += 1; |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 130 | break; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 131 | case 'w': |
| 132 | size += 2; |
| 133 | break; |
| 134 | case 'd': |
| 135 | size += 4; |
| 136 | break; |
| 137 | default: |
| 138 | return 0; |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 139 | } |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | return size; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Allocate a buffer large enough to hold values defined by a type |
| 147 | * string. The caller has to free the buffer. |
| 148 | * |
| 149 | * @param type_str type string |
| 150 | * @param count pointer for storing size of buffer |
| 151 | * @return pointer to buffer or NULL on error |
| 152 | */ |
| 153 | static void *type_string_alloc(const char *type_str, uint32_t *count) |
| 154 | { |
| 155 | void *data; |
| 156 | size_t size; |
| 157 | |
| 158 | size = type_string_get_space_size(type_str); |
| 159 | if (!size) |
| 160 | return NULL; |
| 161 | data = malloc(size); |
| 162 | if (data) |
| 163 | *count = size; |
| 164 | |
| 165 | return data; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Pack values defined by a type string into a buffer. The buffer must have |
| 170 | * large enough space. |
| 171 | * |
| 172 | * @param type_str type string |
| 173 | * @param values text strings of values to be packed |
| 174 | * @param data output buffer of values |
| 175 | * @return 0 on success, non-0 on error |
| 176 | */ |
| 177 | static int type_string_pack(const char *type_str, char * const values[], |
| 178 | uint8_t *data) |
| 179 | { |
| 180 | size_t offset; |
| 181 | uint32_t value; |
| 182 | |
| 183 | for (offset = 0; *type_str; type_str++, values++) { |
| 184 | value = simple_strtoul(values[0], NULL, 0); |
| 185 | switch (*type_str) { |
| 186 | case 'b': |
| 187 | data[offset] = value; |
| 188 | offset += 1; |
| 189 | break; |
| 190 | case 'w': |
| 191 | put_unaligned_be16(value, data + offset); |
| 192 | offset += 2; |
| 193 | break; |
| 194 | case 'd': |
| 195 | put_unaligned_be32(value, data + offset); |
| 196 | offset += 4; |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 197 | break; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 198 | default: |
| 199 | return -1; |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 202 | |
| 203 | return 0; |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 206 | /** |
| 207 | * Read values defined by a type string from a buffer, and write these values |
| 208 | * to environment variables. |
| 209 | * |
| 210 | * @param type_str type string |
| 211 | * @param data input buffer of values |
| 212 | * @param vars names of environment variables |
| 213 | * @return 0 on success, non-0 on error |
| 214 | */ |
| 215 | static int type_string_write_vars(const char *type_str, uint8_t *data, |
| 216 | char * const vars[]) |
| 217 | { |
| 218 | size_t offset; |
| 219 | uint32_t value; |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 220 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 221 | for (offset = 0; *type_str; type_str++, vars++) { |
| 222 | switch (*type_str) { |
| 223 | case 'b': |
| 224 | value = data[offset]; |
| 225 | offset += 1; |
| 226 | break; |
| 227 | case 'w': |
| 228 | value = get_unaligned_be16(data + offset); |
| 229 | offset += 2; |
| 230 | break; |
| 231 | case 'd': |
| 232 | value = get_unaligned_be32(data + offset); |
| 233 | offset += 4; |
| 234 | break; |
| 235 | default: |
| 236 | return -1; |
| 237 | } |
| 238 | if (setenv_ulong(*vars, value)) |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | } |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 244 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 245 | static int do_tpm_startup(cmd_tbl_t *cmdtp, int flag, |
| 246 | int argc, char * const argv[]) |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 247 | { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 248 | enum tpm_startup_type mode; |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 249 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 250 | if (argc != 2) |
| 251 | return CMD_RET_USAGE; |
| 252 | if (!strcasecmp("TPM_ST_CLEAR", argv[1])) { |
| 253 | mode = TPM_ST_CLEAR; |
| 254 | } else if (!strcasecmp("TPM_ST_STATE", argv[1])) { |
| 255 | mode = TPM_ST_STATE; |
| 256 | } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv[1])) { |
| 257 | mode = TPM_ST_DEACTIVATED; |
| 258 | } else { |
| 259 | printf("Couldn't recognize mode string: %s\n", argv[1]); |
| 260 | return CMD_RET_FAILURE; |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 263 | return convert_return_code(tpm_startup(mode)); |
| 264 | } |
| 265 | |
| 266 | static int do_tpm_nv_define_space(cmd_tbl_t *cmdtp, int flag, |
| 267 | int argc, char * const argv[]) |
| 268 | { |
| 269 | uint32_t index, perm, size; |
| 270 | |
| 271 | if (argc != 4) |
| 272 | return CMD_RET_USAGE; |
| 273 | index = simple_strtoul(argv[1], NULL, 0); |
| 274 | perm = simple_strtoul(argv[2], NULL, 0); |
| 275 | size = simple_strtoul(argv[3], NULL, 0); |
| 276 | |
| 277 | return convert_return_code(tpm_nv_define_space(index, perm, size)); |
| 278 | } |
| 279 | |
| 280 | static int do_tpm_nv_read_value(cmd_tbl_t *cmdtp, int flag, |
| 281 | int argc, char * const argv[]) |
| 282 | { |
| 283 | uint32_t index, count, rc; |
| 284 | void *data; |
| 285 | |
| 286 | if (argc != 4) |
| 287 | return CMD_RET_USAGE; |
| 288 | index = simple_strtoul(argv[1], NULL, 0); |
| 289 | data = (void *)simple_strtoul(argv[2], NULL, 0); |
| 290 | count = simple_strtoul(argv[3], NULL, 0); |
| 291 | |
| 292 | rc = tpm_nv_read_value(index, data, count); |
| 293 | if (!rc) { |
| 294 | puts("area content:\n"); |
| 295 | print_byte_string(data, count); |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 298 | return convert_return_code(rc); |
| 299 | } |
| 300 | |
| 301 | static int do_tpm_nv_write_value(cmd_tbl_t *cmdtp, int flag, |
| 302 | int argc, char * const argv[]) |
| 303 | { |
| 304 | uint32_t index, rc; |
| 305 | size_t count; |
| 306 | void *data; |
| 307 | |
| 308 | if (argc != 3) |
| 309 | return CMD_RET_USAGE; |
| 310 | index = simple_strtoul(argv[1], NULL, 0); |
| 311 | data = parse_byte_string(argv[2], NULL, &count); |
| 312 | if (!data) { |
| 313 | printf("Couldn't parse byte string %s\n", argv[2]); |
| 314 | return CMD_RET_FAILURE; |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 317 | rc = tpm_nv_write_value(index, data, count); |
| 318 | free(data); |
| 319 | |
| 320 | return convert_return_code(rc); |
| 321 | } |
| 322 | |
| 323 | static int do_tpm_extend(cmd_tbl_t *cmdtp, int flag, |
| 324 | int argc, char * const argv[]) |
| 325 | { |
| 326 | uint32_t index, rc; |
| 327 | uint8_t in_digest[20], out_digest[20]; |
| 328 | |
| 329 | if (argc != 3) |
| 330 | return CMD_RET_USAGE; |
| 331 | index = simple_strtoul(argv[1], NULL, 0); |
| 332 | if (!parse_byte_string(argv[2], in_digest, NULL)) { |
| 333 | printf("Couldn't parse byte string %s\n", argv[2]); |
| 334 | return CMD_RET_FAILURE; |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 337 | rc = tpm_extend(index, in_digest, out_digest); |
| 338 | if (!rc) { |
| 339 | puts("PCR value after execution of the command:\n"); |
| 340 | print_byte_string(out_digest, sizeof(out_digest)); |
| 341 | } |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 342 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 343 | return convert_return_code(rc); |
| 344 | } |
| 345 | |
| 346 | static int do_tpm_pcr_read(cmd_tbl_t *cmdtp, int flag, |
| 347 | int argc, char * const argv[]) |
| 348 | { |
| 349 | uint32_t index, count, rc; |
| 350 | void *data; |
| 351 | |
| 352 | if (argc != 4) |
| 353 | return CMD_RET_USAGE; |
| 354 | index = simple_strtoul(argv[1], NULL, 0); |
| 355 | data = (void *)simple_strtoul(argv[2], NULL, 0); |
| 356 | count = simple_strtoul(argv[3], NULL, 0); |
| 357 | |
| 358 | rc = tpm_pcr_read(index, data, count); |
| 359 | if (!rc) { |
| 360 | puts("Named PCR content:\n"); |
| 361 | print_byte_string(data, count); |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 364 | return convert_return_code(rc); |
Vadim Bendebury | 7acba0b | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 367 | static int do_tpm_tsc_physical_presence(cmd_tbl_t *cmdtp, int flag, |
| 368 | int argc, char * const argv[]) |
| 369 | { |
| 370 | uint16_t presence; |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 371 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 372 | if (argc != 2) |
| 373 | return CMD_RET_USAGE; |
| 374 | presence = (uint16_t)simple_strtoul(argv[1], NULL, 0); |
| 375 | |
| 376 | return convert_return_code(tpm_tsc_physical_presence(presence)); |
| 377 | } |
| 378 | |
| 379 | static int do_tpm_read_pubek(cmd_tbl_t *cmdtp, int flag, |
| 380 | int argc, char * const argv[]) |
| 381 | { |
| 382 | uint32_t count, rc; |
| 383 | void *data; |
| 384 | |
| 385 | if (argc != 3) |
| 386 | return CMD_RET_USAGE; |
| 387 | data = (void *)simple_strtoul(argv[1], NULL, 0); |
| 388 | count = simple_strtoul(argv[2], NULL, 0); |
| 389 | |
| 390 | rc = tpm_read_pubek(data, count); |
| 391 | if (!rc) { |
| 392 | puts("pubek value:\n"); |
| 393 | print_byte_string(data, count); |
| 394 | } |
| 395 | |
| 396 | return convert_return_code(rc); |
| 397 | } |
| 398 | |
| 399 | static int do_tpm_physical_set_deactivated(cmd_tbl_t *cmdtp, int flag, |
| 400 | int argc, char * const argv[]) |
| 401 | { |
| 402 | uint8_t state; |
| 403 | |
| 404 | if (argc != 2) |
| 405 | return CMD_RET_USAGE; |
| 406 | state = (uint8_t)simple_strtoul(argv[1], NULL, 0); |
| 407 | |
| 408 | return convert_return_code(tpm_physical_set_deactivated(state)); |
| 409 | } |
| 410 | |
| 411 | static int do_tpm_get_capability(cmd_tbl_t *cmdtp, int flag, |
| 412 | int argc, char * const argv[]) |
| 413 | { |
| 414 | uint32_t cap_area, sub_cap, rc; |
| 415 | void *cap; |
| 416 | size_t count; |
| 417 | |
| 418 | if (argc != 5) |
| 419 | return CMD_RET_USAGE; |
| 420 | cap_area = simple_strtoul(argv[1], NULL, 0); |
| 421 | sub_cap = simple_strtoul(argv[2], NULL, 0); |
| 422 | cap = (void *)simple_strtoul(argv[3], NULL, 0); |
| 423 | count = simple_strtoul(argv[4], NULL, 0); |
| 424 | |
| 425 | rc = tpm_get_capability(cap_area, sub_cap, cap, count); |
| 426 | if (!rc) { |
| 427 | puts("capability information:\n"); |
| 428 | print_byte_string(cap, count); |
| 429 | } |
| 430 | |
| 431 | return convert_return_code(rc); |
| 432 | } |
| 433 | |
| 434 | #define TPM_COMMAND_NO_ARG(cmd) \ |
| 435 | static int do_##cmd(cmd_tbl_t *cmdtp, int flag, \ |
| 436 | int argc, char * const argv[]) \ |
| 437 | { \ |
| 438 | if (argc != 1) \ |
| 439 | return CMD_RET_USAGE; \ |
| 440 | return convert_return_code(cmd()); \ |
| 441 | } |
| 442 | |
| 443 | TPM_COMMAND_NO_ARG(tpm_init) |
| 444 | TPM_COMMAND_NO_ARG(tpm_self_test_full) |
| 445 | TPM_COMMAND_NO_ARG(tpm_continue_self_test) |
| 446 | TPM_COMMAND_NO_ARG(tpm_force_clear) |
| 447 | TPM_COMMAND_NO_ARG(tpm_physical_enable) |
| 448 | TPM_COMMAND_NO_ARG(tpm_physical_disable) |
| 449 | |
| 450 | static int do_tpm_raw_transfer(cmd_tbl_t *cmdtp, int flag, |
| 451 | int argc, char * const argv[]) |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 452 | { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 453 | void *command; |
| 454 | uint8_t response[1024]; |
| 455 | size_t count, response_length = sizeof(response); |
| 456 | uint32_t rc; |
| 457 | |
| 458 | command = parse_byte_string(argv[1], NULL, &count); |
| 459 | if (!command) { |
| 460 | printf("Couldn't parse byte string %s\n", argv[1]); |
| 461 | return CMD_RET_FAILURE; |
| 462 | } |
| 463 | |
| 464 | rc = tis_sendrecv(command, count, response, &response_length); |
| 465 | free(command); |
| 466 | if (!rc) { |
| 467 | puts("tpm response:\n"); |
| 468 | print_byte_string(response, response_length); |
| 469 | } |
| 470 | |
| 471 | return convert_return_code(rc); |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 474 | static int do_tpm_nv_define(cmd_tbl_t *cmdtp, int flag, |
| 475 | int argc, char * const argv[]) |
| 476 | { |
| 477 | uint32_t index, perm, size; |
| 478 | |
| 479 | if (argc != 4) |
| 480 | return CMD_RET_USAGE; |
| 481 | size = type_string_get_space_size(argv[1]); |
| 482 | if (!size) { |
| 483 | printf("Couldn't parse arguments\n"); |
| 484 | return CMD_RET_USAGE; |
| 485 | } |
| 486 | index = simple_strtoul(argv[2], NULL, 0); |
| 487 | perm = simple_strtoul(argv[3], NULL, 0); |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 488 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 489 | return convert_return_code(tpm_nv_define_space(index, perm, size)); |
| 490 | } |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 491 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 492 | static int do_tpm_nv_read(cmd_tbl_t *cmdtp, int flag, |
| 493 | int argc, char * const argv[]) |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 494 | { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 495 | uint32_t index, count, err; |
| 496 | void *data; |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 497 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 498 | if (argc < 3) |
| 499 | return CMD_RET_USAGE; |
| 500 | if (argc != 3 + type_string_get_num_values(argv[1])) |
| 501 | return CMD_RET_USAGE; |
| 502 | index = simple_strtoul(argv[2], NULL, 0); |
| 503 | data = type_string_alloc(argv[1], &count); |
| 504 | if (!data) { |
| 505 | printf("Couldn't parse arguments\n"); |
| 506 | return CMD_RET_USAGE; |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 509 | err = tpm_nv_read_value(index, data, count); |
| 510 | if (!err) { |
| 511 | if (type_string_write_vars(argv[1], data, argv + 3)) { |
| 512 | printf("Couldn't write to variables\n"); |
| 513 | err = ~0; |
| 514 | } |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 515 | } |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 516 | free(data); |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 517 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 518 | return convert_return_code(err); |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 521 | static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag, |
| 522 | int argc, char * const argv[]) |
| 523 | { |
| 524 | uint32_t index, count, err; |
| 525 | void *data; |
| 526 | |
| 527 | if (argc < 3) |
| 528 | return CMD_RET_USAGE; |
| 529 | if (argc != 3 + type_string_get_num_values(argv[1])) |
| 530 | return CMD_RET_USAGE; |
| 531 | index = simple_strtoul(argv[2], NULL, 0); |
| 532 | data = type_string_alloc(argv[1], &count); |
| 533 | if (!data) { |
| 534 | printf("Couldn't parse arguments\n"); |
| 535 | return CMD_RET_USAGE; |
| 536 | } |
| 537 | if (type_string_pack(argv[1], argv + 3, data)) { |
| 538 | printf("Couldn't parse arguments\n"); |
| 539 | free(data); |
| 540 | return CMD_RET_USAGE; |
| 541 | } |
| 542 | |
| 543 | err = tpm_nv_write_value(index, data, count); |
| 544 | free(data); |
| 545 | |
| 546 | return convert_return_code(err); |
| 547 | } |
| 548 | |
| 549 | #define MAKE_TPM_CMD_ENTRY(cmd) \ |
| 550 | U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "") |
| 551 | |
| 552 | static cmd_tbl_t tpm_commands[] = { |
| 553 | U_BOOT_CMD_MKENT(init, 0, 1, |
| 554 | do_tpm_init, "", ""), |
| 555 | U_BOOT_CMD_MKENT(startup, 0, 1, |
| 556 | do_tpm_startup, "", ""), |
| 557 | U_BOOT_CMD_MKENT(self_test_full, 0, 1, |
| 558 | do_tpm_self_test_full, "", ""), |
| 559 | U_BOOT_CMD_MKENT(continue_self_test, 0, 1, |
| 560 | do_tpm_continue_self_test, "", ""), |
| 561 | U_BOOT_CMD_MKENT(force_clear, 0, 1, |
| 562 | do_tpm_force_clear, "", ""), |
| 563 | U_BOOT_CMD_MKENT(physical_enable, 0, 1, |
| 564 | do_tpm_physical_enable, "", ""), |
| 565 | U_BOOT_CMD_MKENT(physical_disable, 0, 1, |
| 566 | do_tpm_physical_disable, "", ""), |
| 567 | U_BOOT_CMD_MKENT(nv_define_space, 0, 1, |
| 568 | do_tpm_nv_define_space, "", ""), |
| 569 | U_BOOT_CMD_MKENT(nv_read_value, 0, 1, |
| 570 | do_tpm_nv_read_value, "", ""), |
| 571 | U_BOOT_CMD_MKENT(nv_write_value, 0, 1, |
| 572 | do_tpm_nv_write_value, "", ""), |
| 573 | U_BOOT_CMD_MKENT(extend, 0, 1, |
| 574 | do_tpm_extend, "", ""), |
| 575 | U_BOOT_CMD_MKENT(pcr_read, 0, 1, |
| 576 | do_tpm_pcr_read, "", ""), |
| 577 | U_BOOT_CMD_MKENT(tsc_physical_presence, 0, 1, |
| 578 | do_tpm_tsc_physical_presence, "", ""), |
| 579 | U_BOOT_CMD_MKENT(read_pubek, 0, 1, |
| 580 | do_tpm_read_pubek, "", ""), |
| 581 | U_BOOT_CMD_MKENT(physical_set_deactivated, 0, 1, |
| 582 | do_tpm_physical_set_deactivated, "", ""), |
| 583 | U_BOOT_CMD_MKENT(get_capability, 0, 1, |
| 584 | do_tpm_get_capability, "", ""), |
| 585 | U_BOOT_CMD_MKENT(raw_transfer, 0, 1, |
| 586 | do_tpm_raw_transfer, "", ""), |
| 587 | U_BOOT_CMD_MKENT(nv_define, 0, 1, |
| 588 | do_tpm_nv_define, "", ""), |
| 589 | U_BOOT_CMD_MKENT(nv_read, 0, 1, |
| 590 | do_tpm_nv_read, "", ""), |
| 591 | U_BOOT_CMD_MKENT(nv_write, 0, 1, |
| 592 | do_tpm_nv_write, "", ""), |
| 593 | }; |
| 594 | |
| 595 | static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 596 | { |
| 597 | cmd_tbl_t *tpm_cmd; |
| 598 | |
| 599 | if (argc < 2) |
| 600 | return CMD_RET_USAGE; |
| 601 | tpm_cmd = find_cmd_tbl(argv[1], tpm_commands, ARRAY_SIZE(tpm_commands)); |
| 602 | if (!tpm_cmd) |
| 603 | return CMD_RET_USAGE; |
| 604 | |
| 605 | return tpm_cmd->cmd(cmdtp, flag, argc - 1, argv + 1); |
| 606 | } |
| 607 | |
| 608 | U_BOOT_CMD(tpm, CONFIG_SYS_MAXARGS, 1, do_tpm, |
| 609 | "Issue a TPM command", |
| 610 | "cmd args...\n" |
| 611 | " - Issue TPM command <cmd> with arguments <args...>.\n" |
| 612 | "Admin Startup and State Commands:\n" |
| 613 | " init\n" |
| 614 | " - Put TPM into a state where it waits for 'startup' command.\n" |
| 615 | " startup mode\n" |
| 616 | " - Issue TPM_Starup command. <mode> is one of TPM_ST_CLEAR,\n" |
| 617 | " TPM_ST_STATE, and TPM_ST_DEACTIVATED.\n" |
| 618 | "Admin Testing Commands:\n" |
| 619 | " self_test_full\n" |
| 620 | " - Test all of the TPM capabilities.\n" |
| 621 | " continue_self_test\n" |
| 622 | " - Inform TPM that it should complete the self-test.\n" |
| 623 | "Admin Opt-in Commands:\n" |
| 624 | " physical_enable\n" |
| 625 | " - Set the PERMANENT disable flag to FALSE using physical presence as\n" |
| 626 | " authorization.\n" |
| 627 | " physical_disable\n" |
| 628 | " - Set the PERMANENT disable flag to TRUE using physical presence as\n" |
| 629 | " authorization.\n" |
| 630 | " physical_set_deactivated 0|1\n" |
| 631 | " - Set deactivated flag.\n" |
| 632 | "Admin Ownership Commands:\n" |
| 633 | " force_clear\n" |
| 634 | " - Issue TPM_ForceClear command.\n" |
| 635 | " tsc_physical_presence flags\n" |
| 636 | " - Set TPM device's Physical Presence flags to <flags>.\n" |
| 637 | "The Capability Commands:\n" |
| 638 | " get_capability cap_area sub_cap addr count\n" |
| 639 | " - Read <count> bytes of TPM capability indexed by <cap_area> and\n" |
| 640 | " <sub_cap> to memory address <addr>.\n" |
| 641 | "Endorsement Key Handling Commands:\n" |
| 642 | " read_pubek addr count\n" |
| 643 | " - Read <count> bytes of the public endorsement key to memory\n" |
| 644 | " address <addr>\n" |
| 645 | "Integrity Collection and Reporting Commands:\n" |
| 646 | " extend index digest_hex_string\n" |
| 647 | " - Add a new measurement to a PCR. Update PCR <index> with the 20-bytes\n" |
| 648 | " <digest_hex_string>\n" |
| 649 | " pcr_read index addr count\n" |
| 650 | " - Read <count> bytes from PCR <index> to memory address <addr>.\n" |
| 651 | "Non-volatile Storage Commands:\n" |
| 652 | " nv_define_space index permission size\n" |
| 653 | " - Establish a space at index <index> with <permission> of <size> bytes.\n" |
| 654 | " nv_read_value index addr count\n" |
| 655 | " - Read <count> bytes from space <index> to memory address <addr>.\n" |
| 656 | " nv_write_value index addr count\n" |
| 657 | " - Write <count> bytes from memory address <addr> to space <index>.\n" |
| 658 | "Miscellaneous helper functions:\n" |
| 659 | " raw_transfer byte_string\n" |
| 660 | " - Send a byte string <byte_string> to TPM and print the response.\n" |
| 661 | " Non-volatile storage helper functions:\n" |
| 662 | " These helper functions treat a non-volatile space as a non-padded\n" |
| 663 | " sequence of integer values. These integer values are defined by a type\n" |
| 664 | " string, which is a text string of 'bwd' characters: 'b' means a 8-bit\n" |
| 665 | " value, 'w' 16-bit value, 'd' 32-bit value. All helper functions take\n" |
| 666 | " a type string as their first argument.\n" |
| 667 | " nv_define type_string index perm\n" |
| 668 | " - Define a space <index> with permission <perm>.\n" |
| 669 | " nv_read types_string index vars...\n" |
| 670 | " - Read from space <index> to environment variables <vars...>.\n" |
| 671 | " nv_write types_string index values...\n" |
| 672 | " - Write to space <index> from values <values...>.\n" |
Luigi Semenzato | 04d1bd2 | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 673 | ); |