blob: 86db829ab17793e8fe061bbfd87e41c6f65eae0d [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.
7 */
8
9#include <stdio.h>
10#include <string.h>
11#include <syslog.h>
12
13#include "tlcl.h"
14#include "tpm_error_messages.h"
15
16typedef struct command_record {
17 const char* name;
18 const char* abbr;
19 const char* description;
20 uint32_t (*handler)(void);
21} command_record;
22
23/* Handler functions. These wouldn't exist if C had closures.
24 */
25
26static uint32_t HandlerGetFlags(void) {
27 uint8_t disabled;
28 uint8_t deactivated;
29 uint8_t nvlocked;
30 uint32_t result = TlclGetFlags(&disabled, &deactivated, &nvlocked);
31 if (result == 0) {
32 printf("disabled: %d\ndeactivated: %d\nnvlocked: %d\n",
33 disabled, deactivated, nvlocked);
34 }
35 return result;
36}
37
38static uint32_t HandlerActivate(void) {
39 return TlclSetDeactivated(0);
40}
41
42static uint32_t HandlerDeactivate(void) {
43 return TlclSetDeactivated(1);
44}
45
46/* Table of TPM commands.
47 */
48command_record command_table[] = {
49 { "getflags", "getf", "read and print the value of selected flags",
50 HandlerGetFlags },
51 { "startup", "sta", "issue a Startup command", TlclStartup },
52 { "selftestfull", "test", "issue a SelfTestFull command", TlclSelfTestFull },
53 { "continueselftest", "ctest", "issue a ContinueSelfTest command",
54 TlclContinueSelfTest },
55 { "assertphysicalpresence", "ppon", "assert Physical Presence",
56 TlclAssertPhysicalPresence },
57 { "enable", "ena", "enable the TPM (needs PP)", TlclSetEnable },
58 { "disable", "dis", "disable the TPM (needs PP)", TlclClearEnable },
59 { "activate", "act", "activate the TPM (needs PP, maybe reboot)",
60 HandlerActivate },
61 { "deactivate", "deact", "deactivate the TPM (needs PP, maybe reboot)",
62 HandlerDeactivate },
Luigi Semenzato56cec582010-08-10 15:09:37 -070063 { "clear", "clr", "clear the TPM owner (needs PP)", TlclForceClear },
Luigi Semenzatoe72291c2010-08-10 09:46:09 -070064};
65
66static int n_commands = sizeof(command_table) / sizeof(command_table[0]);
67
68int main(int argc, char* argv[]) {
69 if (argc < 2) {
70 fprintf(stderr, "usage: %s <TPM command>\n or: %s help\n",
71 argv[0], argv[0]);
72 exit(1);
73 } else {
74 command_record* c;
75 const char* cmd = argv[1];
76
77 if (strcmp(cmd, "help") == 0) {
78 printf("%23s %7s %s\n\n", "command", "abbr.", "description");
79 for (c = command_table; c < command_table + n_commands; c++) {
80 printf("%23s %7s %s\n", c->name, c->abbr, c->description);
81 }
82 return 0;
83 }
84
85 TlclLibInit();
86
87 for (c = command_table; c < command_table + n_commands; c++) {
88 if (strcmp(cmd, c->name) == 0 || strcmp(cmd, c->abbr) == 0) {
89 uint32_t result;
90 result = c->handler();
91 if (result == 0) {
92 return 0;
93 } else {
94 int i;
95 int n = sizeof(tpm_error_table) / sizeof(tpm_error_table[0]);
96 fprintf(stderr, "command \"%s\" failed with code 0x%x\n",
97 cmd, result);
98 for (i = 0; i < n; i++) {
99 if (tpm_error_table[i].code == result) {
100 fprintf(stderr, "%s\n%s\n", tpm_error_table[i].name,
101 tpm_error_table[i].description);
102 return 1;
103 }
104 }
105 fprintf(stderr, "the error code is unknown to this program\n");
106 return 1;
107 }
108 }
109 }
110
111 /* No command matched. */
112 fprintf(stderr, "%s: unknown command: %s\n", argv[0], cmd);
113 return 1;
114 }
115}