blob: 2c23aa68a49393e3fafec66604b4f1f2c989ff56 [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"
Luigi Semenzatoc91e2392010-08-17 14:31:52 -070015#include "tss_constants.h"
Luigi Semenzatoe72291c2010-08-10 09:46:09 -070016
17typedef struct command_record {
18 const char* name;
19 const char* abbr;
20 const char* description;
21 uint32_t (*handler)(void);
22} command_record;
23
Luigi Semenzatoc91e2392010-08-17 14:31:52 -070024/* Set in main, consumed by handler functions below. We use global variables
25 * so we can also choose to call Tlcl*() functions directly; they don't take
26 * argv/argc.
27 */
28int nargs;
29char** args;
30
31/* Converts a string in the form 0x[0-9a-f]+ to a 32-bit value. Returns 0 for
32 * success, non-zero for failure.
33 */
34int HexStringToUint32(const char* string, uint32_t* value) {
35 char tail[1];
36 /* strtoul is not as good because it overflows silently */
37 char* format = strncmp(string, "0x", 2) ? "%8x%s" : "0x%8x%s";
38 int n = sscanf(string, format, value, tail);
39 return n != 1;
40}
41
42/* Converts a string in the form [0-9a-f]+ to an 8-bit value. Returns 0 for
43 * success, non-zero for failure.
44 */
45int HexStringToUint8(const char* string, uint8_t* value) {
46 char* end;
47 uint32_t large_value = strtoul(string, &end, 16);
48 if (*end != '\0' || large_value > 0xff) {
49 return 1;
50 }
51 *value = large_value;
52 return 0;
53}
54
55/* TPM error check and reporting. Returns 0 if |result| is 0 (TPM_SUCCESS).
56 * Otherwise looks up a TPM error in the error table and prints the error if
57 * found.
58 */
59uint32_t ErrorCheck(uint32_t result, const char* cmd) {
60 if (result == 0) {
61 return 0;
62 } else {
63 int i;
64 int n = sizeof(tpm_error_table) / sizeof(tpm_error_table[0]);
65 fprintf(stderr, "command \"%s\" failed with code 0x%x\n", cmd, result);
66 for (i = 0; i < n; i++) {
67 if (tpm_error_table[i].code == result) {
68 fprintf(stderr, "%s\n%s\n", tpm_error_table[i].name,
69 tpm_error_table[i].description);
70 return 1;
71 }
72 }
73 fprintf(stderr, "the TPM error code is unknown to this program\n");
74 return 1;
75 }
76}
77
Luigi Semenzatoe72291c2010-08-10 09:46:09 -070078/* Handler functions. These wouldn't exist if C had closures.
79 */
Luigi Semenzatoe72291c2010-08-10 09:46:09 -070080static uint32_t HandlerGetFlags(void) {
81 uint8_t disabled;
82 uint8_t deactivated;
83 uint8_t nvlocked;
84 uint32_t result = TlclGetFlags(&disabled, &deactivated, &nvlocked);
85 if (result == 0) {
86 printf("disabled: %d\ndeactivated: %d\nnvlocked: %d\n",
87 disabled, deactivated, nvlocked);
88 }
89 return result;
90}
91
92static uint32_t HandlerActivate(void) {
93 return TlclSetDeactivated(0);
94}
95
96static uint32_t HandlerDeactivate(void) {
97 return TlclSetDeactivated(1);
98}
99
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700100static uint32_t HandlerDefineSpace(void) {
101 uint32_t index, size, perm;
102 if (nargs != 5) {
103 fprintf(stderr, "usage: tpmc def <index> <size> <perm>\n");
104 exit(1);
105 }
106 if (HexStringToUint32(args[2], &index) != 0 ||
107 HexStringToUint32(args[3], &size) != 0 ||
108 HexStringToUint32(args[4], &perm) != 0) {
109 fprintf(stderr, "<index>, <size>, and <perm> must be "
110 "32-bit hex (0x[0-9a-f]+)\n");
111 exit(1);
112 }
113 return TlclDefineSpace(index, perm, size);
114}
115
116static uint32_t HandlerWrite(void) {
117 uint32_t index, size;
118 uint8_t value[TPM_MAX_COMMAND_SIZE];
119 char** byteargs;
120 int i;
121 if (nargs < 3) {
122 fprintf(stderr, "usage: tpmc write <index> [<byte0> <byte1> ...]\n");
123 exit(1);
124 }
125 if (HexStringToUint32(args[2], &index) != 0) {
126 fprintf(stderr, "<index> must be 32-bit hex (0x[0-9a-f]+)\n");
127 exit(1);
128 }
129 size = nargs - 3;
130 if (size > sizeof(value)) {
131 fprintf(stderr, "byte array too large\n");
132 exit(1);
133 }
134
135 byteargs = args + 3;
136 for (i = 0; i < size; i++) {
137 if (HexStringToUint8(byteargs[i], &value[i]) != 0) {
138 fprintf(stderr, "invalid byte %s, should be [0-9a-f][0-9a-f]?\n",
139 byteargs[i]);
140 exit(1);
141 }
142 }
143
144 if (size == 0) {
145 if (index == TPM_NV_INDEX_LOCK) {
146 fprintf(stderr, "This would set the nvLocked bit. "
147 "Use \"tpmc setnv\" instead.\n");
148 exit(1);
149 }
150 printf("warning: zero-length write\n");
151 } else {
152 printf("writing %d byte%s\n", size, size > 1 ? "s" : "");
153 }
154
155 return TlclWrite(index, value, size);
156}
157
158static uint32_t HandlerRead(void) {
159 uint32_t index, size;
160 uint8_t value[4096];
161 uint32_t result;
162 int i;
163 if (nargs != 4) {
164 fprintf(stderr, "usage: tpmc read <index> <size>\n");
165 exit(1);
166 }
167 if (HexStringToUint32(args[2], &index) != 0 ||
168 HexStringToUint32(args[3], &size) != 0) {
169 fprintf(stderr, "<index> and <size> must be 32-bit hex (0x[0-9a-f]+)\n");
170 exit(1);
171 }
172 if (size > sizeof(value)) {
173 fprintf(stderr, "size of read (0x%x) is too big\n", size);
174 exit(1);
175 }
176 result = TlclRead(index, value, size);
177 if (result == 0 && size > 0) {
178 for (i = 0; i < size - 1; i++) {
179 printf("%x ", value[i]);
180 }
181 printf("%x\n", value[i]);
182 }
183 return result;
184}
185
186static uint32_t HandlerGetPermissions(void) {
187 uint32_t index, permissions, result;
188 if (nargs != 3) {
189 fprintf(stderr, "usage: tpmc getp <index>\n");
190 exit(1);
191 }
192 if (HexStringToUint32(args[2], &index) != 0) {
193 fprintf(stderr, "<index> must be 32-bit hex (0x[0-9a-f]+)\n");
194 exit(1);
195 }
196 result = TlclGetPermissions(index, &permissions);
197 if (result == 0) {
198 printf("space 0x%x has permissions 0x%x\n", index, permissions);
199 }
200 return result;
201}
202
Luigi Semenzato5896b962010-08-25 07:16:03 -0700203static uint32_t HandlerGetPermanentFlags(void) {
204 TPM_PERMANENT_FLAGS pflags;
205 uint32_t result = TlclGetPermanentFlags(&pflags);
206 if (result == 0) {
207#define P(name) printf("%s %d\n", #name, pflags.name)
208 P(disable);
209 P(ownership);
210 P(deactivated);
211 P(readPubek);
212 P(disableOwnerClear);
213 P(allowMaintenance);
214 P(physicalPresenceLifetimeLock);
215 P(physicalPresenceHWEnable);
216 P(physicalPresenceCMDEnable);
217 P(CEKPUsed);
218 P(TPMpost);
219 P(TPMpostLock);
220 P(FIPS);
221 P(Operator);
222 P(enableRevokeEK);
223 P(nvLocked);
224 P(readSRKPub);
225 P(tpmEstablished);
226 P(maintenanceDone);
227 P(disableFullDALogicInfo);
228#undef P
229 }
230 return result;
231}
232
233static uint32_t HandlerGetSTClearFlags(void) {
234 TPM_STCLEAR_FLAGS vflags;
235 uint32_t result = TlclGetSTClearFlags(&vflags);
236 if (result == 0) {
237#define P(name) printf("%s %d\n", #name, vflags.name)
238 P(deactivated);
239 P(disableForceClear);
240 P(physicalPresence);
241 P(physicalPresenceLock);
242 P(bGlobalLock);
243#undef P
244 }
245 return result;
246}
247
248
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700249/* Table of TPM commands.
250 */
251command_record command_table[] = {
252 { "getflags", "getf", "read and print the value of selected flags",
253 HandlerGetFlags },
254 { "startup", "sta", "issue a Startup command", TlclStartup },
255 { "selftestfull", "test", "issue a SelfTestFull command", TlclSelfTestFull },
256 { "continueselftest", "ctest", "issue a ContinueSelfTest command",
257 TlclContinueSelfTest },
258 { "assertphysicalpresence", "ppon", "assert Physical Presence",
259 TlclAssertPhysicalPresence },
Luigi Semenzato1d83dd12010-08-30 10:23:43 -0700260 { "physicalpresencecmdenable", "ppcmd", "turn on software PP",
261 TlclPhysicalPresenceCMDEnable },
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700262 { "enable", "ena", "enable the TPM (needs PP)", TlclSetEnable },
263 { "disable", "dis", "disable the TPM (needs PP)", TlclClearEnable },
264 { "activate", "act", "activate the TPM (needs PP, maybe reboot)",
265 HandlerActivate },
266 { "deactivate", "deact", "deactivate the TPM (needs PP, maybe reboot)",
267 HandlerDeactivate },
Luigi Semenzato56cec582010-08-10 15:09:37 -0700268 { "clear", "clr", "clear the TPM owner (needs PP)", TlclForceClear },
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700269 { "setnvlocked", "setnv", "set the nvLocked flag permanently (IRREVERSIBLE!)",
270 TlclSetNvLocked },
271 { "lockphysicalpresence", "pplock", "lock PP to current value until reboot",
272 TlclLockPhysicalPresence },
273 { "setbgloballock", "block", "set the bGlobalLock until reboot",
274 TlclSetGlobalLock },
275 { "definespace", "def", "define a space (def <index> <size> <perm>)",
276 HandlerDefineSpace },
277 { "write", "write", "write to a space (write <index> [<byte0> <byte1> ...])",
278 HandlerWrite },
279 { "read", "read", "read from a space (read <index> <size>)",
280 HandlerRead },
281 { "getpermissions", "getp", "print space permissions (getp <index>)",
282 HandlerGetPermissions },
Luigi Semenzato5896b962010-08-25 07:16:03 -0700283 { "getpermanentflags", "getpf", "print all permanent flags",
284 HandlerGetPermanentFlags },
285 { "getstclearflags", "getvf", "print all volatile (ST_CLEAR) flags",
286 HandlerGetSTClearFlags },
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700287};
288
289static int n_commands = sizeof(command_table) / sizeof(command_table[0]);
290
291int main(int argc, char* argv[]) {
292 if (argc < 2) {
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700293 fprintf(stderr, "usage: %s <TPM command> [args]\n or: %s help\n",
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700294 argv[0], argv[0]);
295 exit(1);
296 } else {
297 command_record* c;
298 const char* cmd = argv[1];
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700299 nargs = argc;
300 args = argv;
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700301
302 if (strcmp(cmd, "help") == 0) {
Luigi Semenzato1d83dd12010-08-30 10:23:43 -0700303 printf("%26s %7s %s\n\n", "command", "abbr.", "description");
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700304 for (c = command_table; c < command_table + n_commands; c++) {
Luigi Semenzato1d83dd12010-08-30 10:23:43 -0700305 printf("%26s %7s %s\n", c->name, c->abbr, c->description);
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700306 }
307 return 0;
308 }
309
310 TlclLibInit();
311
312 for (c = command_table; c < command_table + n_commands; c++) {
313 if (strcmp(cmd, c->name) == 0 || strcmp(cmd, c->abbr) == 0) {
Luigi Semenzatoc91e2392010-08-17 14:31:52 -0700314 return ErrorCheck(c->handler(), cmd);
Luigi Semenzatoe72291c2010-08-10 09:46:09 -0700315 }
316 }
317
318 /* No command matched. */
319 fprintf(stderr, "%s: unknown command: %s\n", argv[0], cmd);
320 return 1;
321 }
322}