Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 1 | /* 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 Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 7 | * |
| 8 | * The exit code is 0 for success, the TPM error code for TPM errors, and 255 |
| 9 | * for other errors. |
Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 10 | */ |
| 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 Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 18 | #include "tss_constants.h" |
Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 19 | |
Luigi Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 20 | #define OTHER_ERROR 255 |
| 21 | |
Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 22 | typedef 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 Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 29 | /* 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 | */ |
| 33 | int nargs; |
| 34 | char** 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 | */ |
| 39 | int 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 | */ |
| 50 | int 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 | */ |
| 64 | uint32_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 Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 75 | return result; |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | fprintf(stderr, "the TPM error code is unknown to this program\n"); |
Luigi Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 79 | return result; |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 83 | /* Handler functions. These wouldn't exist if C had closures. |
| 84 | */ |
Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 85 | static 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 | |
| 97 | static uint32_t HandlerActivate(void) { |
| 98 | return TlclSetDeactivated(0); |
| 99 | } |
| 100 | |
| 101 | static uint32_t HandlerDeactivate(void) { |
| 102 | return TlclSetDeactivated(1); |
| 103 | } |
| 104 | |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 105 | static 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 Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 109 | exit(OTHER_ERROR); |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 110 | } |
| 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 Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 116 | exit(OTHER_ERROR); |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 117 | } |
| 118 | return TlclDefineSpace(index, perm, size); |
| 119 | } |
| 120 | |
| 121 | static 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 Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 128 | exit(OTHER_ERROR); |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 129 | } |
| 130 | if (HexStringToUint32(args[2], &index) != 0) { |
| 131 | fprintf(stderr, "<index> must be 32-bit hex (0x[0-9a-f]+)\n"); |
Luigi Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 132 | exit(OTHER_ERROR); |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 133 | } |
| 134 | size = nargs - 3; |
| 135 | if (size > sizeof(value)) { |
| 136 | fprintf(stderr, "byte array too large\n"); |
Luigi Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 137 | exit(OTHER_ERROR); |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 138 | } |
| 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 Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 145 | exit(OTHER_ERROR); |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 146 | } |
| 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 Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 153 | exit(OTHER_ERROR); |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 154 | } |
| 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 | |
| 163 | static 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 Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 170 | exit(OTHER_ERROR); |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 171 | } |
| 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 Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 175 | exit(OTHER_ERROR); |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 176 | } |
| 177 | if (size > sizeof(value)) { |
| 178 | fprintf(stderr, "size of read (0x%x) is too big\n", size); |
Luigi Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 179 | exit(OTHER_ERROR); |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 180 | } |
| 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 | |
| 191 | static uint32_t HandlerGetPermissions(void) { |
| 192 | uint32_t index, permissions, result; |
| 193 | if (nargs != 3) { |
| 194 | fprintf(stderr, "usage: tpmc getp <index>\n"); |
Luigi Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 195 | exit(OTHER_ERROR); |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 196 | } |
| 197 | if (HexStringToUint32(args[2], &index) != 0) { |
| 198 | fprintf(stderr, "<index> must be 32-bit hex (0x[0-9a-f]+)\n"); |
Luigi Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 199 | exit(OTHER_ERROR); |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 200 | } |
| 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 Semenzato | 5896b96 | 2010-08-25 07:16:03 -0700 | [diff] [blame] | 208 | static 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 | |
| 238 | static 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 Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 254 | /* Table of TPM commands. |
| 255 | */ |
| 256 | command_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 Semenzato | 1d83dd1 | 2010-08-30 10:23:43 -0700 | [diff] [blame] | 265 | { "physicalpresencecmdenable", "ppcmd", "turn on software PP", |
| 266 | TlclPhysicalPresenceCMDEnable }, |
Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 267 | { "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 Semenzato | 56cec58 | 2010-08-10 15:09:37 -0700 | [diff] [blame] | 273 | { "clear", "clr", "clear the TPM owner (needs PP)", TlclForceClear }, |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 274 | { "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 Semenzato | 5896b96 | 2010-08-25 07:16:03 -0700 | [diff] [blame] | 288 | { "getpermanentflags", "getpf", "print all permanent flags", |
| 289 | HandlerGetPermanentFlags }, |
| 290 | { "getstclearflags", "getvf", "print all volatile (ST_CLEAR) flags", |
| 291 | HandlerGetSTClearFlags }, |
Luigi Semenzato | d903cc8 | 2010-10-27 09:42:51 -0700 | [diff] [blame] | 292 | { "resume", "res", "execute TPM_Startup(ST_STATE)", TlclResume }, |
Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | static int n_commands = sizeof(command_table) / sizeof(command_table[0]); |
| 296 | |
| 297 | int main(int argc, char* argv[]) { |
| 298 | if (argc < 2) { |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 299 | fprintf(stderr, "usage: %s <TPM command> [args]\n or: %s help\n", |
Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 300 | argv[0], argv[0]); |
Luigi Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 301 | return OTHER_ERROR; |
Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 302 | } else { |
| 303 | command_record* c; |
| 304 | const char* cmd = argv[1]; |
Luigi Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 305 | nargs = argc; |
| 306 | args = argv; |
Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 307 | |
| 308 | if (strcmp(cmd, "help") == 0) { |
Luigi Semenzato | 1d83dd1 | 2010-08-30 10:23:43 -0700 | [diff] [blame] | 309 | printf("%26s %7s %s\n\n", "command", "abbr.", "description"); |
Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 310 | for (c = command_table; c < command_table + n_commands; c++) { |
Luigi Semenzato | 1d83dd1 | 2010-08-30 10:23:43 -0700 | [diff] [blame] | 311 | printf("%26s %7s %s\n", c->name, c->abbr, c->description); |
Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 312 | } |
| 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 Semenzato | c91e239 | 2010-08-17 14:31:52 -0700 | [diff] [blame] | 320 | return ErrorCheck(c->handler(), cmd); |
Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
| 324 | /* No command matched. */ |
| 325 | fprintf(stderr, "%s: unknown command: %s\n", argv[0], cmd); |
Luigi Semenzato | a8cba99 | 2010-09-21 14:12:15 -0700 | [diff] [blame] | 326 | return OTHER_ERROR; |
Luigi Semenzato | e72291c | 2010-08-10 09:46:09 -0700 | [diff] [blame] | 327 | } |
| 328 | } |