blob: 494a1ee4cb40d2921d9338bc37d7a799cf7a69e8 [file] [log] [blame]
Luigi Semenzatoe72291c2010-08-10 09:46:09 -07001/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 *
5 * TPM command utility. Runs simple TPM commands. Mostly useful when physical
6 * presence has not been locked.
Luigi Semenzatoa8cba992010-09-21 14:12:15 -07007 *
8 * The exit code is 0 for success, the TPM error code for TPM errors, and 255
9 * for other errors.
Luigi Semenzatoe72291c2010-08-10 09:46:09 -070010 */
11
12#include <stdio.h>
13#include <string.h>
14#include <syslog.h>
15
16#include "tlcl.h"
17#include "tpm_error_messages.h"
Luigi Semenzatoc91e2392010-08-17 14:31:52 -070018#include "tss_constants.h"
Luigi Semenzatoe72291c2010-08-10 09:46:09 -070019
Luigi Semenzatoa8cba992010-09-21 14:12:15 -070020#define OTHER_ERROR 255
21
Luigi Semenzatoe72291c2010-08-10 09:46:09 -070022typedef struct command_record {
23 const char* name;
24 const char* abbr;
25 const char* description;
26 uint32_t (*handler)(void);
27} command_record;
28
Luigi Semenzatoc91e2392010-08-17 14:31:52 -070029/* Set in main, consumed by handler functions below. We use global variables
30 * so we can also choose to call Tlcl*() functions directly; they don't take
31 * argv/argc.
32 */
33int nargs;
34char** args;
35
36/* Converts a string in the form 0x[0-9a-f]+ to a 32-bit value. Returns 0 for
37 * success, non-zero for failure.
38 */
39int HexStringToUint32(const char* string, uint32_t* value) {
40 char tail[1];
41 /* strtoul is not as good because it overflows silently */
42 char* format = strncmp(string, "0x", 2) ? "%8x%s" : "0x%8x%s";
43 int n = sscanf(string, format, value, tail);
44 return n != 1;
45}
46
47/* Converts a string in the form [0-9a-f]+ to an 8-bit value. Returns 0 for
48 * success, non-zero for failure.
49 */
50int HexStringToUint8(const char* string, uint8_t* value) {
51 char* end;
52 uint32_t large_value = strtoul(string, &end, 16);
53 if (*end != '\0' || large_value > 0xff) {
54 return 1;
55 }
56 *value = large_value;
57 return 0;
58}
59
60/* TPM error check and reporting. Returns 0 if |result| is 0 (TPM_SUCCESS).
61 * Otherwise looks up a TPM error in the error table and prints the error if
62 * found.
63 */
64uint32_t ErrorCheck(uint32_t result, const char* cmd) {
65 if (result == 0) {
66 return 0;
67 } else {
68 int i;
69 int n = sizeof(tpm_error_table) / sizeof(tpm_error_table[0]);
70 fprintf(stderr, "command \"%s\" failed with code 0x%x\n", cmd, result);
71 for (i = 0; i < n; i++) {
72 if (tpm_error_table[i].code == result) {
73 fprintf(stderr, "%s\n%s\n", tpm_error_table[i].name,
74 tpm_error_table[i].description);
Luigi Semenzatoa8cba992010-09-21 14:12:15 -070075 return result;
Luigi Semenzatoc91e2392010-08-17 14:31:52 -070076 }
77 }
78 fprintf(stderr, "the TPM error code is unknown to this program\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -070079 return result;
Luigi Semenzatoc91e2392010-08-17 14:31:52 -070080 }
81}
82
Luigi Semenzatoe72291c2010-08-10 09:46:09 -070083/* Handler functions. These wouldn't exist if C had closures.
84 */
Luigi Semenzatoe72291c2010-08-10 09:46:09 -070085static uint32_t HandlerGetFlags(void) {
86 uint8_t disabled;
87 uint8_t deactivated;
88 uint8_t nvlocked;
89 uint32_t result = TlclGetFlags(&disabled, &deactivated, &nvlocked);
90 if (result == 0) {
91 printf("disabled: %d\ndeactivated: %d\nnvlocked: %d\n",
92 disabled, deactivated, nvlocked);
93 }
94 return result;
95}
96
97static uint32_t HandlerActivate(void) {
98 return TlclSetDeactivated(0);
99}
100
101static uint32_t HandlerDeactivate(void) {
102 return TlclSetDeactivated(1);
103}
104
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700105static uint32_t HandlerDefineSpace(void) {
106 uint32_t index, size, perm;
107 if (nargs != 5) {
108 fprintf(stderr, "usage: tpmc def <index> <size> <perm>\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700109 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700110 }
111 if (HexStringToUint32(args[2], &index) != 0 ||
112 HexStringToUint32(args[3], &size) != 0 ||
113 HexStringToUint32(args[4], &perm) != 0) {
114 fprintf(stderr, "<index>, <size>, and <perm> must be "
115 "32-bit hex (0x[0-9a-f]+)\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700116 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700117 }
118 return TlclDefineSpace(index, perm, size);
119}
120
121static uint32_t HandlerWrite(void) {
122 uint32_t index, size;
123 uint8_t value[TPM_MAX_COMMAND_SIZE];
124 char** byteargs;
125 int i;
126 if (nargs < 3) {
127 fprintf(stderr, "usage: tpmc write <index> [<byte0> <byte1> ...]\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700128 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700129 }
130 if (HexStringToUint32(args[2], &index) != 0) {
131 fprintf(stderr, "<index> must be 32-bit hex (0x[0-9a-f]+)\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700132 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700133 }
134 size = nargs - 3;
135 if (size > sizeof(value)) {
136 fprintf(stderr, "byte array too large\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700137 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700138 }
139
140 byteargs = args + 3;
141 for (i = 0; i < size; i++) {
142 if (HexStringToUint8(byteargs[i], &value[i]) != 0) {
143 fprintf(stderr, "invalid byte %s, should be [0-9a-f][0-9a-f]?\n",
144 byteargs[i]);
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700145 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700146 }
147 }
148
149 if (size == 0) {
150 if (index == TPM_NV_INDEX_LOCK) {
151 fprintf(stderr, "This would set the nvLocked bit. "
152 "Use \"tpmc setnv\" instead.\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700153 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700154 }
155 printf("warning: zero-length write\n");
156 } else {
157 printf("writing %d byte%s\n", size, size > 1 ? "s" : "");
158 }
159
160 return TlclWrite(index, value, size);
161}
162
163static uint32_t HandlerRead(void) {
164 uint32_t index, size;
165 uint8_t value[4096];
166 uint32_t result;
167 int i;
168 if (nargs != 4) {
169 fprintf(stderr, "usage: tpmc read <index> <size>\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700170 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700171 }
172 if (HexStringToUint32(args[2], &index) != 0 ||
173 HexStringToUint32(args[3], &size) != 0) {
174 fprintf(stderr, "<index> and <size> must be 32-bit hex (0x[0-9a-f]+)\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700175 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700176 }
177 if (size > sizeof(value)) {
178 fprintf(stderr, "size of read (0x%x) is too big\n", size);
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700179 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700180 }
181 result = TlclRead(index, value, size);
182 if (result == 0 && size > 0) {
183 for (i = 0; i < size - 1; i++) {
184 printf("%x ", value[i]);
185 }
186 printf("%x\n", value[i]);
187 }
188 return result;
189}
190
191static uint32_t HandlerGetPermissions(void) {
192 uint32_t index, permissions, result;
193 if (nargs != 3) {
194 fprintf(stderr, "usage: tpmc getp <index>\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700195 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700196 }
197 if (HexStringToUint32(args[2], &index) != 0) {
198 fprintf(stderr, "<index> must be 32-bit hex (0x[0-9a-f]+)\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700199 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700200 }
201 result = TlclGetPermissions(index, &permissions);
202 if (result == 0) {
203 printf("space 0x%x has permissions 0x%x\n", index, permissions);
204 }
205 return result;
206}
207
Luigi Semenzato5896b962010-08-25 07:16:03 -0700208static uint32_t HandlerGetPermanentFlags(void) {
209 TPM_PERMANENT_FLAGS pflags;
210 uint32_t result = TlclGetPermanentFlags(&pflags);
211 if (result == 0) {
212#define P(name) printf("%s %d\n", #name, pflags.name)
213 P(disable);
214 P(ownership);
215 P(deactivated);
216 P(readPubek);
217 P(disableOwnerClear);
218 P(allowMaintenance);
219 P(physicalPresenceLifetimeLock);
220 P(physicalPresenceHWEnable);
221 P(physicalPresenceCMDEnable);
222 P(CEKPUsed);
223 P(TPMpost);
224 P(TPMpostLock);
225 P(FIPS);
226 P(Operator);
227 P(enableRevokeEK);
228 P(nvLocked);
229 P(readSRKPub);
230 P(tpmEstablished);
231 P(maintenanceDone);
232 P(disableFullDALogicInfo);
233#undef P
234 }
235 return result;
236}
237
238static uint32_t HandlerGetSTClearFlags(void) {
239 TPM_STCLEAR_FLAGS vflags;
240 uint32_t result = TlclGetSTClearFlags(&vflags);
241 if (result == 0) {
242#define P(name) printf("%s %d\n", #name, vflags.name)
243 P(deactivated);
244 P(disableForceClear);
245 P(physicalPresence);
246 P(physicalPresenceLock);
247 P(bGlobalLock);
248#undef P
249 }
250 return result;
251}
252
253
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700254/* Table of TPM commands.
255 */
256command_record command_table[] = {
257 { "getflags", "getf", "read and print the value of selected flags",
258 HandlerGetFlags },
259 { "startup", "sta", "issue a Startup command", TlclStartup },
260 { "selftestfull", "test", "issue a SelfTestFull command", TlclSelfTestFull },
261 { "continueselftest", "ctest", "issue a ContinueSelfTest command",
262 TlclContinueSelfTest },
263 { "assertphysicalpresence", "ppon", "assert Physical Presence",
264 TlclAssertPhysicalPresence },
Luigi Semenzato1d83dd12010-08-30 10:23:43 -0700265 { "physicalpresencecmdenable", "ppcmd", "turn on software PP",
266 TlclPhysicalPresenceCMDEnable },
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700267 { "enable", "ena", "enable the TPM (needs PP)", TlclSetEnable },
268 { "disable", "dis", "disable the TPM (needs PP)", TlclClearEnable },
269 { "activate", "act", "activate the TPM (needs PP, maybe reboot)",
270 HandlerActivate },
271 { "deactivate", "deact", "deactivate the TPM (needs PP, maybe reboot)",
272 HandlerDeactivate },
Luigi Semenzato56cec582010-08-10 15:09:37 -0700273 { "clear", "clr", "clear the TPM owner (needs PP)", TlclForceClear },
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700274 { "setnvlocked", "setnv", "set the nvLocked flag permanently (IRREVERSIBLE!)",
275 TlclSetNvLocked },
276 { "lockphysicalpresence", "pplock", "lock PP to current value until reboot",
277 TlclLockPhysicalPresence },
278 { "setbgloballock", "block", "set the bGlobalLock until reboot",
279 TlclSetGlobalLock },
280 { "definespace", "def", "define a space (def <index> <size> <perm>)",
281 HandlerDefineSpace },
282 { "write", "write", "write to a space (write <index> [<byte0> <byte1> ...])",
283 HandlerWrite },
284 { "read", "read", "read from a space (read <index> <size>)",
285 HandlerRead },
286 { "getpermissions", "getp", "print space permissions (getp <index>)",
287 HandlerGetPermissions },
Luigi Semenzato5896b962010-08-25 07:16:03 -0700288 { "getpermanentflags", "getpf", "print all permanent flags",
289 HandlerGetPermanentFlags },
290 { "getstclearflags", "getvf", "print all volatile (ST_CLEAR) flags",
291 HandlerGetSTClearFlags },
Luigi Semenzatod903cc82010-10-27 09:42:51 -0700292 { "resume", "res", "execute TPM_Startup(ST_STATE)", TlclResume },
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700293};
294
295static int n_commands = sizeof(command_table) / sizeof(command_table[0]);
296
297int main(int argc, char* argv[]) {
298 if (argc < 2) {
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700299 fprintf(stderr, "usage: %s <TPM command> [args]\n or: %s help\n",
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700300 argv[0], argv[0]);
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700301 return OTHER_ERROR;
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700302 } else {
303 command_record* c;
304 const char* cmd = argv[1];
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700305 nargs = argc;
306 args = argv;
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700307
308 if (strcmp(cmd, "help") == 0) {
Luigi Semenzato1d83dd12010-08-30 10:23:43 -0700309 printf("%26s %7s %s\n\n", "command", "abbr.", "description");
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700310 for (c = command_table; c < command_table + n_commands; c++) {
Luigi Semenzato1d83dd12010-08-30 10:23:43 -0700311 printf("%26s %7s %s\n", c->name, c->abbr, c->description);
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700312 }
313 return 0;
314 }
315
316 TlclLibInit();
317
318 for (c = command_table; c < command_table + n_commands; c++) {
319 if (strcmp(cmd, c->name) == 0 || strcmp(cmd, c->abbr) == 0) {
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700320 return ErrorCheck(c->handler(), cmd);
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700321 }
322 }
323
324 /* No command matched. */
325 fprintf(stderr, "%s: unknown command: %s\n", argv[0], cmd);
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700326 return OTHER_ERROR;
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700327 }
328}