Vadim Bendebury | 576fb1e | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. |
| 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> |
| 25 | #include <tpm.h> |
| 26 | |
| 27 | #define MAX_TRANSACTION_SIZE 30 |
| 28 | |
| 29 | /* |
| 30 | * tpm_write() expects a variable number of parameters: the internal address |
| 31 | * followed by data to write, byte by byte. |
| 32 | * |
| 33 | * Returns 0 on success or -1 on errors (wrong arguments or TPM failure). |
| 34 | */ |
| 35 | static int tpm_process(int argc, char * const argv[], cmd_tbl_t *cmdtp) |
| 36 | { |
| 37 | u8 tpm_buffer[MAX_TRANSACTION_SIZE]; |
| 38 | u32 write_size, read_size; |
| 39 | char *p; |
| 40 | int rv = -1; |
| 41 | |
| 42 | for (write_size = 0; write_size < argc; write_size++) { |
| 43 | u32 datum = simple_strtoul(argv[write_size], &p, 0); |
| 44 | if (*p || (datum > 0xff)) { |
| 45 | printf("\n%s: bad data value\n\n", argv[write_size]); |
| 46 | cmd_usage(cmdtp); |
| 47 | return rv; |
| 48 | } |
| 49 | tpm_buffer[write_size] = (u8)datum; |
| 50 | } |
| 51 | |
| 52 | read_size = sizeof(tpm_buffer); |
| 53 | if (!tis_sendrecv(tpm_buffer, write_size, tpm_buffer, &read_size)) { |
| 54 | int i; |
| 55 | puts("Got TPM response:\n"); |
| 56 | for (i = 0; i < read_size; i++) |
| 57 | printf(" %2.2x", tpm_buffer[i]); |
| 58 | puts("\n"); |
| 59 | rv = 0; |
| 60 | } else { |
| 61 | puts("tpm command failed\n"); |
| 62 | } |
| 63 | return rv; |
| 64 | } |
| 65 | |
Luigi Semenzato | eea3f4d | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 66 | #define CHECK(exp) do { \ |
| 67 | int _rv = exp; \ |
| 68 | if (_rv) { \ |
| 69 | printf("CHECK: %s %d %x\n", #exp, __LINE__, _rv);\ |
| 70 | } \ |
| 71 | } while (0) |
| 72 | |
| 73 | static int tpm_process_stress(int repeat_count) |
| 74 | { |
| 75 | int i; |
| 76 | int rv = 0; |
| 77 | u8 request[] = {0x0, 0xc1, |
| 78 | 0x0, 0x0, 0x0, 0x16, |
| 79 | 0x0, 0x0, 0x0, 0x65, |
| 80 | 0x0, 0x0, 0x0, 0x4, |
| 81 | 0x0, 0x0, 0x0, 0x4, |
| 82 | 0x0, 0x0, 0x1, 0x9}; |
| 83 | u8 response[MAX_TRANSACTION_SIZE]; |
| 84 | u32 rlength = MAX_TRANSACTION_SIZE; |
| 85 | |
| 86 | CHECK(tis_init()); |
| 87 | |
| 88 | for (i = 0; i < repeat_count; i++) { |
| 89 | CHECK(tis_open()); |
| 90 | rv = tis_sendrecv(request, sizeof(request), response, &rlength); |
| 91 | if (rv) { |
| 92 | printf("tpm test failed at step %d with 0x%x\n", i, rv); |
| 93 | CHECK(tis_close()); |
| 94 | break; |
| 95 | } |
| 96 | CHECK(tis_close()); |
| 97 | if ((response[6] || response[7] || response[8] || response[9]) |
| 98 | && response[9] != 0x26) { |
| 99 | /* Ignore postinit errors */ |
| 100 | printf("tpm command failed at step %d\n" |
| 101 | "tpm error code: %02x%02x%02x%02x\n", i, |
| 102 | response[6], response[7], |
| 103 | response[8], response[9]); |
| 104 | rv = -1; |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | return rv; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | static int do_tpm_many(cmd_tbl_t *cmdtp, int flag, |
| 113 | int argc, char * const argv[], int repeat_count) |
| 114 | |
Vadim Bendebury | 576fb1e | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 115 | { |
| 116 | int rv = 0; |
| 117 | |
Luigi Semenzato | eea3f4d | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 118 | if (argc < 7 && repeat_count == 0) { |
Vadim Bendebury | 576fb1e | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 119 | puts("command should be at least six bytes in size\n"); |
| 120 | return -1; |
| 121 | } |
| 122 | |
Luigi Semenzato | eea3f4d | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 123 | if (repeat_count > 0) { |
| 124 | rv = tpm_process_stress(repeat_count); |
| 125 | return rv; |
| 126 | } |
| 127 | |
Vadim Bendebury | 576fb1e | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 128 | if (tis_init()) { |
| 129 | puts("tis_init() failed!\n"); |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | if (tis_open()) { |
| 134 | puts("tis_open() failed!\n"); |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | rv = tpm_process(argc - 1, argv + 1, cmdtp); |
| 139 | |
| 140 | if (tis_close()) { |
| 141 | puts("tis_close() failed!\n"); |
| 142 | rv = -1; |
| 143 | } |
| 144 | |
| 145 | return rv; |
| 146 | } |
| 147 | |
Luigi Semenzato | eea3f4d | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 148 | |
| 149 | static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 150 | { |
| 151 | return do_tpm_many(cmdtp, flag, argc, argv, 0); |
| 152 | } |
| 153 | |
| 154 | |
Vadim Bendebury | 576fb1e | 2011-10-15 15:13:34 +0000 | [diff] [blame] | 155 | U_BOOT_CMD(tpm, MAX_TRANSACTION_SIZE, 1, do_tpm, |
| 156 | "<byte> [<byte> ...] - write data and read response", |
| 157 | "send arbitrary data (at least 6 bytes) to the TPM " |
| 158 | "device and read the response" |
| 159 | ); |
Luigi Semenzato | eea3f4d | 2012-12-05 14:46:44 +0000 | [diff] [blame] | 160 | |
| 161 | static int do_tpm_stress(cmd_tbl_t *cmdtp, int flag, |
| 162 | int argc, char * const argv[]) |
| 163 | { |
| 164 | long unsigned int n; |
| 165 | int rv; |
| 166 | |
| 167 | if (argc != 2) { |
| 168 | puts("usage: tpm_stress <count>\n"); |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | rv = strict_strtoul(argv[1], 10, &n); |
| 173 | if (rv) { |
| 174 | puts("tpm_stress: bad count"); |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | return do_tpm_many(cmdtp, flag, argc, argv, n); |
| 179 | } |
| 180 | |
| 181 | U_BOOT_CMD(tpm_stress, 2, 1, do_tpm_stress, |
| 182 | "<n> - stress-test communication with TPM", |
| 183 | "Repeat a TPM transaction (request-response) N times" |
| 184 | ); |