blob: d8fb07a5fadeccc5583860f53ca85726ed9c050b [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
Kees Cook946370d2012-01-09 14:17:40 -0800163static uint32_t HandlerPCRRead(void) {
164 uint32_t index;
165 uint8_t value[TPM_PCR_DIGEST];
166 uint32_t result;
167 int i;
168 if (nargs != 3) {
169 fprintf(stderr, "usage: tpmc pcrread <index>\n");
170 exit(OTHER_ERROR);
171 }
172 if (HexStringToUint32(args[2], &index) != 0) {
173 fprintf(stderr, "<index> must be 32-bit hex (0x[0-9a-f]+)\n");
174 exit(OTHER_ERROR);
175 }
176 result = TlclPCRRead(index, value, sizeof(value));
177 if (result == 0) {
178 for (i = 0; i < TPM_PCR_DIGEST; i++) {
179 printf("%02x", value[i]);
180 }
181 printf("\n");
182 }
183 return result;
184}
185
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700186static uint32_t HandlerRead(void) {
187 uint32_t index, size;
188 uint8_t value[4096];
189 uint32_t result;
190 int i;
191 if (nargs != 4) {
192 fprintf(stderr, "usage: tpmc read <index> <size>\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700193 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700194 }
195 if (HexStringToUint32(args[2], &index) != 0 ||
196 HexStringToUint32(args[3], &size) != 0) {
197 fprintf(stderr, "<index> and <size> must be 32-bit hex (0x[0-9a-f]+)\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700198 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700199 }
200 if (size > sizeof(value)) {
201 fprintf(stderr, "size of read (0x%x) is too big\n", size);
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700202 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700203 }
204 result = TlclRead(index, value, size);
205 if (result == 0 && size > 0) {
206 for (i = 0; i < size - 1; i++) {
207 printf("%x ", value[i]);
208 }
209 printf("%x\n", value[i]);
210 }
211 return result;
212}
213
214static uint32_t HandlerGetPermissions(void) {
215 uint32_t index, permissions, result;
216 if (nargs != 3) {
217 fprintf(stderr, "usage: tpmc getp <index>\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700218 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700219 }
220 if (HexStringToUint32(args[2], &index) != 0) {
221 fprintf(stderr, "<index> must be 32-bit hex (0x[0-9a-f]+)\n");
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700222 exit(OTHER_ERROR);
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700223 }
224 result = TlclGetPermissions(index, &permissions);
225 if (result == 0) {
226 printf("space 0x%x has permissions 0x%x\n", index, permissions);
227 }
228 return result;
229}
230
Luigi Semenzato5896b962010-08-25 07:16:03 -0700231static uint32_t HandlerGetPermanentFlags(void) {
232 TPM_PERMANENT_FLAGS pflags;
233 uint32_t result = TlclGetPermanentFlags(&pflags);
234 if (result == 0) {
235#define P(name) printf("%s %d\n", #name, pflags.name)
236 P(disable);
237 P(ownership);
238 P(deactivated);
239 P(readPubek);
240 P(disableOwnerClear);
241 P(allowMaintenance);
242 P(physicalPresenceLifetimeLock);
243 P(physicalPresenceHWEnable);
244 P(physicalPresenceCMDEnable);
245 P(CEKPUsed);
246 P(TPMpost);
247 P(TPMpostLock);
248 P(FIPS);
249 P(Operator);
250 P(enableRevokeEK);
251 P(nvLocked);
252 P(readSRKPub);
253 P(tpmEstablished);
254 P(maintenanceDone);
255 P(disableFullDALogicInfo);
256#undef P
257 }
258 return result;
259}
260
261static uint32_t HandlerGetSTClearFlags(void) {
262 TPM_STCLEAR_FLAGS vflags;
263 uint32_t result = TlclGetSTClearFlags(&vflags);
264 if (result == 0) {
265#define P(name) printf("%s %d\n", #name, vflags.name)
266 P(deactivated);
267 P(disableForceClear);
268 P(physicalPresence);
269 P(physicalPresenceLock);
270 P(bGlobalLock);
271#undef P
272 }
273 return result;
274}
275
276
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700277/* Table of TPM commands.
278 */
279command_record command_table[] = {
280 { "getflags", "getf", "read and print the value of selected flags",
281 HandlerGetFlags },
282 { "startup", "sta", "issue a Startup command", TlclStartup },
283 { "selftestfull", "test", "issue a SelfTestFull command", TlclSelfTestFull },
284 { "continueselftest", "ctest", "issue a ContinueSelfTest command",
285 TlclContinueSelfTest },
286 { "assertphysicalpresence", "ppon", "assert Physical Presence",
287 TlclAssertPhysicalPresence },
Luigi Semenzato1d83dd12010-08-30 10:23:43 -0700288 { "physicalpresencecmdenable", "ppcmd", "turn on software PP",
289 TlclPhysicalPresenceCMDEnable },
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700290 { "enable", "ena", "enable the TPM (needs PP)", TlclSetEnable },
291 { "disable", "dis", "disable the TPM (needs PP)", TlclClearEnable },
292 { "activate", "act", "activate the TPM (needs PP, maybe reboot)",
293 HandlerActivate },
294 { "deactivate", "deact", "deactivate the TPM (needs PP, maybe reboot)",
295 HandlerDeactivate },
Luigi Semenzato56cec582010-08-10 15:09:37 -0700296 { "clear", "clr", "clear the TPM owner (needs PP)", TlclForceClear },
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700297 { "setnvlocked", "setnv", "set the nvLocked flag permanently (IRREVERSIBLE!)",
298 TlclSetNvLocked },
Gaurav Shahac6d5602011-07-20 18:10:10 -0700299 { "lockphysicalpresence", "pplock", "lock (turn off) PP until reboot",
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700300 TlclLockPhysicalPresence },
301 { "setbgloballock", "block", "set the bGlobalLock until reboot",
302 TlclSetGlobalLock },
303 { "definespace", "def", "define a space (def <index> <size> <perm>)",
304 HandlerDefineSpace },
305 { "write", "write", "write to a space (write <index> [<byte0> <byte1> ...])",
306 HandlerWrite },
307 { "read", "read", "read from a space (read <index> <size>)",
308 HandlerRead },
Kees Cook946370d2012-01-09 14:17:40 -0800309 { "pcrread", "pcr", "read from a PCR (pcrread <index>)",
310 HandlerPCRRead },
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700311 { "getpermissions", "getp", "print space permissions (getp <index>)",
312 HandlerGetPermissions },
Luigi Semenzato5896b962010-08-25 07:16:03 -0700313 { "getpermanentflags", "getpf", "print all permanent flags",
314 HandlerGetPermanentFlags },
315 { "getstclearflags", "getvf", "print all volatile (ST_CLEAR) flags",
316 HandlerGetSTClearFlags },
Luigi Semenzatod903cc82010-10-27 09:42:51 -0700317 { "resume", "res", "execute TPM_Startup(ST_STATE)", TlclResume },
Luigi Semenzato54992f92011-03-16 10:56:48 -0700318 { "savestate", "save", "execute TPM_SaveState", TlclSaveState },
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700319};
320
321static int n_commands = sizeof(command_table) / sizeof(command_table[0]);
322
323int main(int argc, char* argv[]) {
324 if (argc < 2) {
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700325 fprintf(stderr, "usage: %s <TPM command> [args]\n or: %s help\n",
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700326 argv[0], argv[0]);
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700327 return OTHER_ERROR;
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700328 } else {
329 command_record* c;
330 const char* cmd = argv[1];
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700331 nargs = argc;
332 args = argv;
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700333
334 if (strcmp(cmd, "help") == 0) {
Luigi Semenzato1d83dd12010-08-30 10:23:43 -0700335 printf("%26s %7s %s\n\n", "command", "abbr.", "description");
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700336 for (c = command_table; c < command_table + n_commands; c++) {
Luigi Semenzato1d83dd12010-08-30 10:23:43 -0700337 printf("%26s %7s %s\n", c->name, c->abbr, c->description);
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700338 }
339 return 0;
340 }
341
342 TlclLibInit();
343
344 for (c = command_table; c < command_table + n_commands; c++) {
345 if (strcmp(cmd, c->name) == 0 || strcmp(cmd, c->abbr) == 0) {
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700346 return ErrorCheck(c->handler(), cmd);
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700347 }
348 }
349
350 /* No command matched. */
351 fprintf(stderr, "%s: unknown command: %s\n", argv[0], cmd);
Luigi Semenzatoa8cba992010-09-21 14:12:15 -0700352 return OTHER_ERROR;
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700353 }
354}