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. |
| 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 | |
| 16 | typedef 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 | |
| 26 | static 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 | |
| 38 | static uint32_t HandlerActivate(void) { |
| 39 | return TlclSetDeactivated(0); |
| 40 | } |
| 41 | |
| 42 | static uint32_t HandlerDeactivate(void) { |
| 43 | return TlclSetDeactivated(1); |
| 44 | } |
| 45 | |
| 46 | /* Table of TPM commands. |
| 47 | */ |
| 48 | command_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 }, |
| 63 | }; |
| 64 | |
| 65 | static int n_commands = sizeof(command_table) / sizeof(command_table[0]); |
| 66 | |
| 67 | int main(int argc, char* argv[]) { |
| 68 | if (argc < 2) { |
| 69 | fprintf(stderr, "usage: %s <TPM command>\n or: %s help\n", |
| 70 | argv[0], argv[0]); |
| 71 | exit(1); |
| 72 | } else { |
| 73 | command_record* c; |
| 74 | const char* cmd = argv[1]; |
| 75 | |
| 76 | if (strcmp(cmd, "help") == 0) { |
| 77 | printf("%23s %7s %s\n\n", "command", "abbr.", "description"); |
| 78 | for (c = command_table; c < command_table + n_commands; c++) { |
| 79 | printf("%23s %7s %s\n", c->name, c->abbr, c->description); |
| 80 | } |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | TlclLibInit(); |
| 85 | |
| 86 | for (c = command_table; c < command_table + n_commands; c++) { |
| 87 | if (strcmp(cmd, c->name) == 0 || strcmp(cmd, c->abbr) == 0) { |
| 88 | uint32_t result; |
| 89 | result = c->handler(); |
| 90 | if (result == 0) { |
| 91 | return 0; |
| 92 | } else { |
| 93 | int i; |
| 94 | int n = sizeof(tpm_error_table) / sizeof(tpm_error_table[0]); |
| 95 | fprintf(stderr, "command \"%s\" failed with code 0x%x\n", |
| 96 | cmd, result); |
| 97 | for (i = 0; i < n; i++) { |
| 98 | if (tpm_error_table[i].code == result) { |
| 99 | fprintf(stderr, "%s\n%s\n", tpm_error_table[i].name, |
| 100 | tpm_error_table[i].description); |
| 101 | return 1; |
| 102 | } |
| 103 | } |
| 104 | fprintf(stderr, "the error code is unknown to this program\n"); |
| 105 | return 1; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /* No command matched. */ |
| 111 | fprintf(stderr, "%s: unknown command: %s\n", argv[0], cmd); |
| 112 | return 1; |
| 113 | } |
| 114 | } |