2 * Copyright (c) 2013 The Chromium OS Authors.
4 * See file CREDITS for list of people who contributed to this
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.
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.
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,
27 #include <asm/unaligned.h>
28 #include <linux/string.h>
31 * Print a byte string in hexdecimal format, 16-bytes per line.
33 * @param data byte string to be printed
34 * @param count number of bytes to be printed
36 static void print_byte_string(uint8_t *data
, size_t count
)
38 int i
, print_newline
= 0;
40 for (i
= 0; i
< count
; i
++) {
41 printf(" %02x", data
[i
]);
42 print_newline
= (i
% 16 == 15);
46 /* Avoid duplicated newline at the end */
52 * Convert a text string of hexdecimal values into a byte string.
54 * @param bytes text string of hexdecimal values with no space
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
63 static void *parse_byte_string(char *bytes
, uint8_t *data
, size_t *count_ptr
)
69 length
= strlen(bytes
);
78 for (i
= 0; i
< length
; i
+= 2) {
80 byte
[1] = bytes
[i
+ 1];
81 data
[i
/ 2] = (uint8_t)simple_strtoul(byte
, NULL
, 16);
91 * Convert TPM command return code to U-Boot command error codes.
93 * @param return_code TPM command return code
94 * @return value of enum command_ret_t
96 static int convert_return_code(uint32_t return_code
)
99 return CMD_RET_FAILURE
;
101 return CMD_RET_SUCCESS
;
105 * Return number of values defined by a type string.
107 * @param type_str type string
108 * @return number of values of type string
110 static int type_string_get_num_values(const char *type_str
)
112 return strlen(type_str
);
116 * Return total size of values defined by a type string.
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.
122 static size_t type_string_get_space_size(const char *type_str
)
126 for (size
= 0; *type_str
; type_str
++) {
146 * Allocate a buffer large enough to hold values defined by a type
147 * string. The caller has to free the buffer.
149 * @param type_str type string
150 * @param count pointer for storing size of buffer
151 * @return pointer to buffer or NULL on error
153 static void *type_string_alloc(const char *type_str
, uint32_t *count
)
158 size
= type_string_get_space_size(type_str
);
169 * Pack values defined by a type string into a buffer. The buffer must have
170 * large enough space.
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
177 static int type_string_pack(const char *type_str
, char * const values
[],
183 for (offset
= 0; *type_str
; type_str
++, values
++) {
184 value
= simple_strtoul(values
[0], NULL
, 0);
187 data
[offset
] = value
;
191 put_unaligned_be16(value
, data
+ offset
);
195 put_unaligned_be32(value
, data
+ offset
);
207 * Read values defined by a type string from a buffer, and write these values
208 * to environment variables.
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
215 static int type_string_write_vars(const char *type_str
, uint8_t *data
,
221 for (offset
= 0; *type_str
; type_str
++, vars
++) {
224 value
= data
[offset
];
228 value
= get_unaligned_be16(data
+ offset
);
232 value
= get_unaligned_be32(data
+ offset
);
238 if (setenv_ulong(*vars
, value
))
245 static int do_tpm_startup(cmd_tbl_t
*cmdtp
, int flag
,
246 int argc
, char * const argv
[])
248 enum tpm_startup_type mode
;
251 return CMD_RET_USAGE
;
252 if (!strcasecmp("TPM_ST_CLEAR", argv
[1])) {
254 } else if (!strcasecmp("TPM_ST_STATE", argv
[1])) {
256 } else if (!strcasecmp("TPM_ST_DEACTIVATED", argv
[1])) {
257 mode
= TPM_ST_DEACTIVATED
;
259 printf("Couldn't recognize mode string: %s\n", argv
[1]);
260 return CMD_RET_FAILURE
;
263 return convert_return_code(tpm_startup(mode
));
266 static int do_tpm_nv_define_space(cmd_tbl_t
*cmdtp
, int flag
,
267 int argc
, char * const argv
[])
269 uint32_t index
, perm
, size
;
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);
277 return convert_return_code(tpm_nv_define_space(index
, perm
, size
));
280 static int do_tpm_nv_read_value(cmd_tbl_t
*cmdtp
, int flag
,
281 int argc
, char * const argv
[])
283 uint32_t index
, count
, rc
;
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);
292 rc
= tpm_nv_read_value(index
, data
, count
);
294 puts("area content:\n");
295 print_byte_string(data
, count
);
298 return convert_return_code(rc
);
301 static int do_tpm_nv_write_value(cmd_tbl_t
*cmdtp
, int flag
,
302 int argc
, char * const argv
[])
309 return CMD_RET_USAGE
;
310 index
= simple_strtoul(argv
[1], NULL
, 0);
311 data
= parse_byte_string(argv
[2], NULL
, &count
);
313 printf("Couldn't parse byte string %s\n", argv
[2]);
314 return CMD_RET_FAILURE
;
317 rc
= tpm_nv_write_value(index
, data
, count
);
320 return convert_return_code(rc
);
323 static int do_tpm_extend(cmd_tbl_t
*cmdtp
, int flag
,
324 int argc
, char * const argv
[])
327 uint8_t in_digest
[20], out_digest
[20];
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
;
337 rc
= tpm_extend(index
, in_digest
, out_digest
);
339 puts("PCR value after execution of the command:\n");
340 print_byte_string(out_digest
, sizeof(out_digest
));
343 return convert_return_code(rc
);
346 static int do_tpm_pcr_read(cmd_tbl_t
*cmdtp
, int flag
,
347 int argc
, char * const argv
[])
349 uint32_t index
, count
, rc
;
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);
358 rc
= tpm_pcr_read(index
, data
, count
);
360 puts("Named PCR content:\n");
361 print_byte_string(data
, count
);
364 return convert_return_code(rc
);
367 static int do_tpm_tsc_physical_presence(cmd_tbl_t
*cmdtp
, int flag
,
368 int argc
, char * const argv
[])
373 return CMD_RET_USAGE
;
374 presence
= (uint16_t)simple_strtoul(argv
[1], NULL
, 0);
376 return convert_return_code(tpm_tsc_physical_presence(presence
));
379 static int do_tpm_read_pubek(cmd_tbl_t
*cmdtp
, int flag
,
380 int argc
, char * const argv
[])
386 return CMD_RET_USAGE
;
387 data
= (void *)simple_strtoul(argv
[1], NULL
, 0);
388 count
= simple_strtoul(argv
[2], NULL
, 0);
390 rc
= tpm_read_pubek(data
, count
);
392 puts("pubek value:\n");
393 print_byte_string(data
, count
);
396 return convert_return_code(rc
);
399 static int do_tpm_physical_set_deactivated(cmd_tbl_t
*cmdtp
, int flag
,
400 int argc
, char * const argv
[])
405 return CMD_RET_USAGE
;
406 state
= (uint8_t)simple_strtoul(argv
[1], NULL
, 0);
408 return convert_return_code(tpm_physical_set_deactivated(state
));
411 static int do_tpm_get_capability(cmd_tbl_t
*cmdtp
, int flag
,
412 int argc
, char * const argv
[])
414 uint32_t cap_area
, sub_cap
, rc
;
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);
425 rc
= tpm_get_capability(cap_area
, sub_cap
, cap
, count
);
427 puts("capability information:\n");
428 print_byte_string(cap
, count
);
431 return convert_return_code(rc
);
434 #define TPM_COMMAND_NO_ARG(cmd) \
435 static int do_##cmd(cmd_tbl_t *cmdtp, int flag, \
436 int argc, char * const argv[]) \
439 return CMD_RET_USAGE; \
440 return convert_return_code(cmd()); \
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
)
450 static int do_tpm_raw_transfer(cmd_tbl_t
*cmdtp
, int flag
,
451 int argc
, char * const argv
[])
454 uint8_t response
[1024];
455 size_t count
, response_length
= sizeof(response
);
458 command
= parse_byte_string(argv
[1], NULL
, &count
);
460 printf("Couldn't parse byte string %s\n", argv
[1]);
461 return CMD_RET_FAILURE
;
464 rc
= tis_sendrecv(command
, count
, response
, &response_length
);
467 puts("tpm response:\n");
468 print_byte_string(response
, response_length
);
471 return convert_return_code(rc
);
474 static int do_tpm_nv_define(cmd_tbl_t
*cmdtp
, int flag
,
475 int argc
, char * const argv
[])
477 uint32_t index
, perm
, size
;
480 return CMD_RET_USAGE
;
481 size
= type_string_get_space_size(argv
[1]);
483 printf("Couldn't parse arguments\n");
484 return CMD_RET_USAGE
;
486 index
= simple_strtoul(argv
[2], NULL
, 0);
487 perm
= simple_strtoul(argv
[3], NULL
, 0);
489 return convert_return_code(tpm_nv_define_space(index
, perm
, size
));
492 static int do_tpm_nv_read(cmd_tbl_t
*cmdtp
, int flag
,
493 int argc
, char * const argv
[])
495 uint32_t index
, count
, err
;
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
);
505 printf("Couldn't parse arguments\n");
506 return CMD_RET_USAGE
;
509 err
= tpm_nv_read_value(index
, data
, count
);
511 if (type_string_write_vars(argv
[1], data
, argv
+ 3)) {
512 printf("Couldn't write to variables\n");
518 return convert_return_code(err
);
521 static int do_tpm_nv_write(cmd_tbl_t
*cmdtp
, int flag
,
522 int argc
, char * const argv
[])
524 uint32_t index
, count
, err
;
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
);
534 printf("Couldn't parse arguments\n");
535 return CMD_RET_USAGE
;
537 if (type_string_pack(argv
[1], argv
+ 3, data
)) {
538 printf("Couldn't parse arguments\n");
540 return CMD_RET_USAGE
;
543 err
= tpm_nv_write_value(index
, data
, count
);
546 return convert_return_code(err
);
549 #define MAKE_TPM_CMD_ENTRY(cmd) \
550 U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
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
, "", ""),
595 static int do_tpm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
600 return CMD_RET_USAGE
;
601 tpm_cmd
= find_cmd_tbl(argv
[1], tpm_commands
, ARRAY_SIZE(tpm_commands
));
603 return CMD_RET_USAGE
;
605 return tpm_cmd
->cmd(cmdtp
, flag
, argc
- 1, argv
+ 1);
608 U_BOOT_CMD(tpm
, CONFIG_SYS_MAXARGS
, 1, do_tpm
,
609 "Issue a TPM command",
611 " - Issue TPM command <cmd> with arguments <args...>.\n"
612 "Admin Startup and State Commands:\n"
614 " - Put TPM into a state where it waits for 'startup' command.\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"
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"
625 " - Set the PERMANENT disable flag to FALSE using physical presence as\n"
627 " physical_disable\n"
628 " - Set the PERMANENT disable flag to TRUE using physical presence as\n"
630 " physical_set_deactivated 0|1\n"
631 " - Set deactivated flag.\n"
632 "Admin Ownership Commands:\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"
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"