tpmc: add 'tpmversion' command

Add command for printing TPM version: 1.2 or 2.0. The command works
even when trunksd/tcsd is running and /dev/tpm0 is busy, so it can
be used to first determine which TPM we are dealing with, and then
select the right name of the daemon to stop based on that.

BUG=none
BRANCH=none
TEST=run 'tpmc tpmver'

Change-Id: Ib8db81ff2af6dc6b0d5aecf30e2688a908b5c3d3
Reviewed-on: https://chromium-review.googlesource.com/410703
Commit-Ready: Andrey Pronin <apronin@chromium.org>
Tested-by: Andrey Pronin <apronin@chromium.org>
Reviewed-by: Vadim Bendebury <vbendeb@chromium.org>
diff --git a/utility/tpmc.c b/utility/tpmc.c
index baddee6..1e4e302 100644
--- a/utility/tpmc.c
+++ b/utility/tpmc.c
@@ -129,6 +129,11 @@
 
 /* Handler functions.  These wouldn't exist if C had closures.
  */
+static uint32_t HandlerTpmVersion(void) {
+  puts(TPM_MODE_STRING);
+  return 0;
+}
+
 /* TODO(apronin): stub for selected flags for TPM2 */
 #ifdef TPM2_MODE
 static uint32_t HandlerGetFlags(void) {
@@ -465,6 +470,8 @@
 /* Table of TPM commands.
  */
 command_record command_table[] = {
+  { "tpmversion", "tpmver", "print TPM version: 1.2 or 2.0",
+    HandlerTpmVersion },
   { "getflags", "getf", "read and print the value of selected flags",
     HandlerGetFlags },
   { "startup", "sta", "issue a Startup command", TlclStartup },
@@ -559,6 +566,9 @@
       }
       return 0;
     }
+    if (!strcmp(cmd, "tpmversion") || !strcmp(cmd, "tpmver")) {
+      return HandlerTpmVersion();
+    }
 
     result = TlclLibInit();
     if (result) {