1 /* Copyright (c) 2012 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.
5 * TPM command utility. Runs simple TPM commands. Mostly useful when physical
6 * presence has not been locked.
8 * The exit code is 0 for success, the TPM error code for TPM errors, and 255
19 #include "tpm_error_messages.h"
20 #include "tss_constants.h"
22 #define OTHER_ERROR 255 /* OTHER_ERROR must be the largest uint8_t value. */
24 typedef struct command_record {
27 const char* description;
28 uint32_t (*handler)(void);
31 /* Set in main, consumed by handler functions below. We use global variables
32 * so we can also choose to call Tlcl*() functions directly; they don't take
38 /* Converts a string in the form 0x[0-9a-f]+ to a 32-bit value. Returns 0 for
39 * success, non-zero for failure.
41 int HexStringToUint32(const char* string, uint32_t* value) {
43 /* strtoul is not as good because it overflows silently */
44 char* format = strncmp(string, "0x", 2) ? "%8x%s" : "0x%8x%s";
45 int n = sscanf(string, format, value, tail);
49 /* Converts a string in the form [0-9a-f]+ to an 8-bit value. Returns 0 for
50 * success, non-zero for failure.
52 int HexStringToUint8(const char* string, uint8_t* value) {
54 uint32_t large_value = strtoul(string, &end, 16);
55 if (*end != '\0' || large_value > 0xff) {
62 int HexStringToArray(const char* string, uint8_t* value, int num_bytes) {
63 int len = strlen(string);
64 if (!strncmp(string, "0x", 2)) {
68 if (len != num_bytes * 2) {
71 for (; len > 0; string += 2, len -= 2, value++) {
72 if (sscanf(string, "%2hhx", value) != 1) {
79 /* TPM error check and reporting. Returns 0 if |result| is 0 (TPM_SUCCESS).
80 * Otherwise looks up a TPM error in the error table and prints the error if
81 * found. Then returns min(result, OTHER_ERROR) since some error codes, such
82 * as TPM_E_RETRY, do not fit in a byte.
84 uint8_t ErrorCheck(uint32_t result, const char* cmd) {
85 uint8_t exit_code = result > OTHER_ERROR ? OTHER_ERROR : result;
90 int n = sizeof(tpm_error_table) / sizeof(tpm_error_table[0]);
91 fprintf(stderr, "command \"%s\" failed with code 0x%x\n", cmd, result);
92 for (i = 0; i < n; i++) {
93 if (tpm_error_table[i].code == result) {
94 fprintf(stderr, "%s\n%s\n", tpm_error_table[i].name,
95 tpm_error_table[i].description);
99 fprintf(stderr, "the TPM error code is unknown to this program\n");
104 /* Handler functions. These wouldn't exist if C had closures.
106 /* TODO(apronin): stub for selecte flags for TPM2 */
108 static uint32_t HandlerGetFlags(void) {
109 fprintf(stderr, "getflags not implemented for TPM2\n");
113 static uint32_t HandlerGetFlags(void) {
117 uint32_t result = TlclGetFlags(&disabled, &deactivated, &nvlocked);
119 printf("disabled: %d\ndeactivated: %d\nnvlocked: %d\n",
120 disabled, deactivated, nvlocked);
127 static uint32_t HandlerActivate(void) {
128 return TlclSetDeactivated(0);
131 static uint32_t HandlerDeactivate(void) {
132 return TlclSetDeactivated(1);
136 static uint32_t HandlerDefineSpace(void) {
137 uint32_t index, size, perm;
139 fprintf(stderr, "usage: tpmc def <index> <size> <perm>\n");
142 if (HexStringToUint32(args[2], &index) != 0 ||
143 HexStringToUint32(args[3], &size) != 0 ||
144 HexStringToUint32(args[4], &perm) != 0) {
145 fprintf(stderr, "<index>, <size>, and <perm> must be "
146 "32-bit hex (0x[0-9a-f]+)\n");
149 return TlclDefineSpace(index, perm, size);
152 static uint32_t HandlerWrite(void) {
153 uint32_t index, size;
154 uint8_t value[TPM_MAX_COMMAND_SIZE];
158 fprintf(stderr, "usage: tpmc write <index> [<byte0> <byte1> ...]\n");
161 if (HexStringToUint32(args[2], &index) != 0) {
162 fprintf(stderr, "<index> must be 32-bit hex (0x[0-9a-f]+)\n");
166 if (size > sizeof(value)) {
167 fprintf(stderr, "byte array too large\n");
172 for (i = 0; i < size; i++) {
173 if (HexStringToUint8(byteargs[i], &value[i]) != 0) {
174 fprintf(stderr, "invalid byte %s, should be [0-9a-f][0-9a-f]?\n",
182 if (index == TPM_NV_INDEX_LOCK) {
183 fprintf(stderr, "This would set the nvLocked bit. "
184 "Use \"tpmc setnv\" instead.\n");
188 printf("warning: zero-length write\n");
190 printf("writing %d byte%s\n", size, size > 1 ? "s" : "");
193 return TlclWrite(index, value, size);
196 static uint32_t HandlerPCRRead(void) {
198 uint8_t value[TPM_PCR_DIGEST];
202 fprintf(stderr, "usage: tpmc pcrread <index>\n");
205 if (HexStringToUint32(args[2], &index) != 0) {
206 fprintf(stderr, "<index> must be 32-bit hex (0x[0-9a-f]+)\n");
209 result = TlclPCRRead(index, value, sizeof(value));
211 for (i = 0; i < TPM_PCR_DIGEST; i++) {
212 printf("%02x", value[i]);
219 static uint32_t HandlerPCRExtend(void) {
221 uint8_t value[TPM_PCR_DIGEST];
223 fprintf(stderr, "usage: tpmc pcrextend <index> <extend_hash>\n");
226 if (HexStringToUint32(args[2], &index) != 0) {
227 fprintf(stderr, "<index> must be 32-bit hex (0x[0-9a-f]+)\n");
230 if (HexStringToArray(args[3], value, TPM_PCR_DIGEST)) {
231 fprintf(stderr, "<extend_hash> must be a 20-byte hex string\n");
234 return TlclExtend(index, value, value);
237 static uint32_t HandlerRead(void) {
238 uint32_t index, size;
243 fprintf(stderr, "usage: tpmc read <index> <size>\n");
246 if (HexStringToUint32(args[2], &index) != 0 ||
247 HexStringToUint32(args[3], &size) != 0) {
248 fprintf(stderr, "<index> and <size> must be 32-bit hex (0x[0-9a-f]+)\n");
251 if (size > sizeof(value)) {
252 fprintf(stderr, "size of read (0x%x) is too big\n", size);
255 result = TlclRead(index, value, size);
256 if (result == 0 && size > 0) {
257 for (i = 0; i < size - 1; i++) {
258 printf("%x ", value[i]);
260 printf("%x\n", value[i]);
265 static uint32_t HandlerGetPermissions(void) {
266 uint32_t index, permissions, result;
268 fprintf(stderr, "usage: tpmc getp <index>\n");
271 if (HexStringToUint32(args[2], &index) != 0) {
272 fprintf(stderr, "<index> must be 32-bit hex (0x[0-9a-f]+)\n");
275 result = TlclGetPermissions(index, &permissions);
277 printf("space 0x%x has permissions 0x%x\n", index, permissions);
282 static uint32_t HandlerGetOwnership(void) {
286 fprintf(stderr, "usage: tpmc getownership\n");
289 result = TlclGetOwnership(&owned);
291 printf("Owned: %s\n", owned ? "yes" : "no");
296 static uint32_t HandlerGetRandom(void) {
297 uint32_t length, size;
302 fprintf(stderr, "usage: tpmc getrandom <size>\n");
305 if (HexStringToUint32(args[2], &length) != 0) {
306 fprintf(stderr, "<size> must be 32-bit hex (0x[0-9a-f]+)\n");
309 bytes = calloc(1, length);
314 result = TlclGetRandom(bytes, length, &size);
315 if (result == 0 && size > 0) {
316 for (i = 0; i < size; i++) {
317 printf("%02x", bytes[i]);
325 static uint32_t HandlerGetPermanentFlags(void) {
326 TPM_PERMANENT_FLAGS pflags;
327 uint32_t result = TlclGetPermanentFlags(&pflags);
329 #define P(name) printf("%s %d\n", #name, pflags.name)
332 P(endorsementAuthSet);
342 P(disableOwnerClear);
344 P(physicalPresenceLifetimeLock);
345 P(physicalPresenceHWEnable);
346 P(physicalPresenceCMDEnable);
357 P(disableFullDALogicInfo);
364 static uint32_t HandlerGetSTClearFlags(void) {
365 TPM_STCLEAR_FLAGS vflags;
366 uint32_t result = TlclGetSTClearFlags(&vflags);
368 #define P(name) printf("%s %d\n", #name, vflags.name)
377 P(disableForceClear);
379 P(physicalPresenceLock);
387 static uint32_t HandlerSendRaw(void) {
388 uint8_t request[4096];
389 uint8_t response[4096];
394 fprintf(stderr, "usage: tpmc sendraw <hex byte 0> ... <hex byte N>\n");
397 for (i = 0; i < nargs - 2 && i < sizeof(request); i++) {
398 if (HexStringToUint8(args[2 + i], &request[i]) != 0) {
399 fprintf(stderr, "bad byte value \"%s\"\n", args[2 + i]);
403 size = TlclPacketSize(request);
405 fprintf(stderr, "bad request: size field is %d, but packet has %d bytes\n",
409 bzero(response, sizeof(response));
410 result = TlclSendReceive(request, response, sizeof(response));
412 fprintf(stderr, "request failed with code %d\n", result);
414 size = TlclPacketSize(response);
415 if (size < 10 || size > sizeof(response)) {
416 fprintf(stderr, "unexpected response size %d\n", size);
419 for (i = 0; i < size; i++) {
420 printf("0x%02x ", response[i]);
421 if (i == size - 1 || (i + 1) % 8 == 0) {
429 /* Table of TPM commands.
431 command_record command_table[] = {
432 { "getflags", "getf", "read and print the value of selected flags",
434 { "startup", "sta", "issue a Startup command", TlclStartup },
435 { "selftestfull", "test", "issue a SelfTestFull command", TlclSelfTestFull },
436 { "continueselftest", "ctest", "issue a ContinueSelfTest command",
437 TlclContinueSelfTest },
439 { "assertphysicalpresence", "ppon", "assert Physical Presence",
440 TlclAssertPhysicalPresence },
441 { "physicalpresencecmdenable", "ppcmd", "turn on software PP",
442 TlclPhysicalPresenceCMDEnable },
443 { "enable", "ena", "enable the TPM (needs PP)", TlclSetEnable },
444 { "disable", "dis", "disable the TPM (needs PP)", TlclClearEnable },
445 { "activate", "act", "activate the TPM (needs PP, maybe reboot)",
447 { "deactivate", "deact", "deactivate the TPM (needs PP, maybe reboot)",
450 { "clear", "clr", "clear the TPM owner (needs PP)", TlclForceClear },
452 { "setnvlocked", "setnv", "set the nvLocked flag permanently (IRREVERSIBLE!)",
455 { "lockphysicalpresence", "pplock", "lock (turn off) PP until reboot",
456 TlclLockPhysicalPresence },
458 { "setbgloballock", "block", "set rollback protection lock until reboot",
459 TlclLockPhysicalPresence },
461 { "setbgloballock", "block", "set the bGlobalLock until reboot",
464 { "definespace", "def", "define a space (def <index> <size> <perm>)",
465 HandlerDefineSpace },
466 { "write", "write", "write to a space (write <index> [<byte0> <byte1> ...])",
468 { "read", "read", "read from a space (read <index> <size>)",
470 { "pcrread", "pcr", "read from a PCR (pcrread <index>)",
472 { "pcrextend", "extend", "extend a PCR (extend <index> <extend_hash>)",
474 { "getownership", "geto", "print state of TPM ownership",
475 HandlerGetOwnership },
476 { "getpermissions", "getp", "print space permissions (getp <index>)",
477 HandlerGetPermissions },
478 { "getpermanentflags", "getpf", "print all permanent flags",
479 HandlerGetPermanentFlags },
480 { "getrandom", "rand", "read bytes from RNG (rand <size>)",
482 { "getstclearflags", "getvf", "print all volatile (ST_CLEAR) flags",
483 HandlerGetSTClearFlags },
484 { "resume", "res", "execute TPM_Startup(ST_STATE)", TlclResume },
485 { "savestate", "save", "execute TPM_SaveState", TlclSaveState },
486 { "sendraw", "raw", "send a raw request and print raw response",
490 static int n_commands = sizeof(command_table) / sizeof(command_table[0]);
492 int main(int argc, char* argv[]) {
496 progname = strrchr(argv[0], '/');
503 fprintf(stderr, "usage: %s <TPM command> [args]\n or: %s help\n",
508 const char* cmd = argv[1];
512 if (strcmp(cmd, "help") == 0) {
513 printf("%26s %7s %s\n\n", "command", "abbr.", "description");
514 for (c = command_table; c < command_table + n_commands; c++) {
515 printf("%26s %7s %s\n", c->name, c->abbr, c->description);
520 result = TlclLibInit();
522 fprintf(stderr, "initialization failed with code %d\n", result);
523 return result > OTHER_ERROR ? OTHER_ERROR : result;
526 for (c = command_table; c < command_table + n_commands; c++) {
527 if (strcmp(cmd, c->name) == 0 || strcmp(cmd, c->abbr) == 0) {
528 return ErrorCheck(c->handler(), cmd);
532 /* No command matched. */
533 fprintf(stderr, "%s: unknown command: %s\n", progname, cmd);