tpm_manager: Add proto printers for version info.

Add freshly generated proto printing code for tpm_manager.proto to
handle the recently added version info in GetTpmStatusReply. A few
fixes to the proto printer script were necessary to handle 64 bit
integers.

BUG=chromium:728134
TEST=none

Change-Id: I3e757becc1e4a64ac2c6dc263f1b8602a338a947
Reviewed-on: https://chromium-review.googlesource.com/529106
Commit-Ready: Mattias Nissler <[email protected]>
Tested-by: Mattias Nissler <[email protected]>
Reviewed-by: Andrey Pronin <[email protected]>
diff --git a/attestation/common/proto_print.py b/attestation/common/proto_print.py
index b050f95..fca66b5 100755
--- a/attestation/common/proto_print.py
+++ b/attestation/common/proto_print.py
@@ -256,6 +256,8 @@
 
 #include "%(package_with_subdir)s/%(header_file_name)s"
 
+#include <inttypes.h>
+
 #include <string>
 
 #include <base/strings/string_number_conversions.h>
@@ -384,14 +386,15 @@
   output += "}\\n";"""
   singular_field_get = 'value.%(name)s()'
   repeated_field_get = 'value.%(name)s(i)'
-  formats = {'bool': '"%%s", %(value)s ? "true" : "false"',
-             'int32': '"%%d", %(value)s',
-             'int64': '"%%ld", %(value)s',
-             'uint32': '"%%u (0x%%08X)", %(value)s, %(value)s',
-             'uint64': '"%%lu (0x%%016X)", %(value)s, %(value)s',
-             'string': '"%%s", %(value)s.c_str()',
-             'bytes': """"%%s", base::HexEncode(%(value)s.data(),
-                                                %(value)s.size()).c_str()"""}
+  formats = {
+      'bool': '"%%s", %(value)s ? "true" : "false"',
+      'int32': '"%%" PRId32, %(value)s',
+      'int64': '"%%" PRId64, %(value)s',
+      'uint32': '"%%" PRIu32 " (0x%%08" PRIX32 ")", %(value)s, %(value)s',
+      'uint64': '"%%" PRIu64 " (0x%%016" PRIX64 ")", %(value)s, %(value)s',
+      'string': '"%%s", %(value)s.c_str()',
+      'bytes': """"%%s", base::HexEncode(%(value)s.data(),
+                                         %(value)s.size()).c_str()"""}
   subtype_format = ('"%%s", GetProtoDebugStringWithIndent(%(value)s, '
                     'indent_size + 2).c_str()')
 
diff --git a/tpm_manager/common/print_tpm_manager_proto.cc b/tpm_manager/common/print_tpm_manager_proto.cc
index bae9d6c..68d12e0 100644
--- a/tpm_manager/common/print_tpm_manager_proto.cc
+++ b/tpm_manager/common/print_tpm_manager_proto.cc
@@ -18,6 +18,8 @@
 
 #include "tpm_manager/common/print_tpm_manager_proto.h"
 
+#include <inttypes.h>
+
 #include <string>
 
 #include <base/strings/string_number_conversions.h>
@@ -147,7 +149,8 @@
 
   if (value.has_index()) {
     output += indent + "  index: ";
-    base::StringAppendF(&output, "%u (0x%08X)", value.index(), value.index());
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")", value.index(),
+                        value.index());
     output += "\n";
   }
   if (value.has_policy()) {
@@ -253,12 +256,14 @@
 
   if (value.has_index()) {
     output += indent + "  index: ";
-    base::StringAppendF(&output, "%u (0x%08X)", value.index(), value.index());
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")", value.index(),
+                        value.index());
     output += "\n";
   }
   if (value.has_size()) {
     output += indent + "  size: ";
-    base::StringAppendF(&output, "%u (0x%08X)", value.size(), value.size());
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")", value.size(),
+                        value.size());
     output += "\n";
   }
   output += indent + "  attributes: {";
@@ -266,9 +271,10 @@
     if (i > 0) {
       base::StringAppendF(&output, ", ");
     }
-    base::StringAppendF(&output, "%s", GetProtoDebugStringWithIndent(
-                                           value.attributes(i), indent_size + 2)
-                                           .c_str());
+    base::StringAppendF(
+        &output, "%s",
+        GetProtoDebugStringWithIndent(value.attributes(i), indent_size + 2)
+            .c_str());
   }
   output += "}\n";
   if (value.has_authorization_value()) {
@@ -323,7 +329,8 @@
 
   if (value.has_index()) {
     output += indent + "  index: ";
-    base::StringAppendF(&output, "%u (0x%08X)", value.index(), value.index());
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")", value.index(),
+                        value.index());
     output += "\n";
   }
   output += indent + "}\n";
@@ -363,7 +370,8 @@
 
   if (value.has_index()) {
     output += indent + "  index: ";
-    base::StringAppendF(&output, "%u (0x%08X)", value.index(), value.index());
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")", value.index(),
+                        value.index());
     output += "\n";
   }
   if (value.has_data()) {
@@ -424,7 +432,8 @@
 
   if (value.has_index()) {
     output += indent + "  index: ";
-    base::StringAppendF(&output, "%u (0x%08X)", value.index(), value.index());
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")", value.index(),
+                        value.index());
     output += "\n";
   }
   if (value.has_authorization_value()) {
@@ -485,7 +494,8 @@
 
   if (value.has_index()) {
     output += indent + "  index: ";
-    base::StringAppendF(&output, "%u (0x%08X)", value.index(), value.index());
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")", value.index(),
+                        value.index());
     output += "\n";
   }
   if (value.has_lock_read()) {
@@ -573,8 +583,8 @@
     if (i > 0) {
       base::StringAppendF(&output, ", ");
     }
-    base::StringAppendF(&output, "%u (0x%08X)", value.index_list(i),
-                        value.index_list(i));
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")",
+                        value.index_list(i), value.index_list(i));
   }
   output += "}\n";
   output += indent + "}\n";
@@ -593,7 +603,8 @@
 
   if (value.has_index()) {
     output += indent + "  index: ";
-    base::StringAppendF(&output, "%u (0x%08X)", value.index(), value.index());
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")", value.index(),
+                        value.index());
     output += "\n";
   }
   output += indent + "}\n";
@@ -619,7 +630,8 @@
   }
   if (value.has_size()) {
     output += indent + "  size: ";
-    base::StringAppendF(&output, "%u (0x%08X)", value.size(), value.size());
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")", value.size(),
+                        value.size());
     output += "\n";
   }
   if (value.has_is_read_locked()) {
@@ -639,9 +651,10 @@
     if (i > 0) {
       base::StringAppendF(&output, ", ");
     }
-    base::StringAppendF(&output, "%s", GetProtoDebugStringWithIndent(
-                                           value.attributes(i), indent_size + 2)
-                                           .c_str());
+    base::StringAppendF(
+        &output, "%s",
+        GetProtoDebugStringWithIndent(value.attributes(i), indent_size + 2)
+            .c_str());
   }
   output += "}\n";
   if (value.has_policy()) {
@@ -669,6 +682,60 @@
   return output;
 }
 
+std::string GetProtoDebugString(
+    const GetTpmStatusReply::TpmVersionInfo& value) {
+  return GetProtoDebugStringWithIndent(value, 0);
+}
+
+std::string GetProtoDebugStringWithIndent(
+    const GetTpmStatusReply::TpmVersionInfo& value,
+    int indent_size) {
+  std::string indent(indent_size, ' ');
+  std::string output =
+      base::StringPrintf("[%s] {\n", value.GetTypeName().c_str());
+
+  if (value.has_family()) {
+    output += indent + "  family: ";
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")",
+                        value.family(), value.family());
+    output += "\n";
+  }
+  if (value.has_spec_level()) {
+    output += indent + "  spec_level: ";
+    base::StringAppendF(&output, "%" PRIu64 " (0x%016" PRIX64 ")",
+                        value.spec_level(), value.spec_level());
+    output += "\n";
+  }
+  if (value.has_manufacturer()) {
+    output += indent + "  manufacturer: ";
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")",
+                        value.manufacturer(), value.manufacturer());
+    output += "\n";
+  }
+  if (value.has_tpm_model()) {
+    output += indent + "  tpm_model: ";
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")",
+                        value.tpm_model(), value.tpm_model());
+    output += "\n";
+  }
+  if (value.has_firmware_version()) {
+    output += indent + "  firmware_version: ";
+    base::StringAppendF(&output, "%" PRIu64 " (0x%016" PRIX64 ")",
+                        value.firmware_version(), value.firmware_version());
+    output += "\n";
+  }
+  if (value.has_vendor_specific()) {
+    output += indent + "  vendor_specific: ";
+    base::StringAppendF(&output, "%s",
+                        base::HexEncode(value.vendor_specific().data(),
+                                        value.vendor_specific().size())
+                            .c_str());
+    output += "\n";
+  }
+  output += indent + "}\n";
+  return output;
+}
+
 std::string GetProtoDebugString(const GetTpmStatusReply& value) {
   return GetProtoDebugStringWithIndent(value, 0);
 }
@@ -698,21 +765,22 @@
   }
   if (value.has_local_data()) {
     output += indent + "  local_data: ";
-    base::StringAppendF(&output, "%s", GetProtoDebugStringWithIndent(
-                                           value.local_data(), indent_size + 2)
-                                           .c_str());
+    base::StringAppendF(
+        &output, "%s",
+        GetProtoDebugStringWithIndent(value.local_data(), indent_size + 2)
+            .c_str());
     output += "\n";
   }
   if (value.has_dictionary_attack_counter()) {
     output += indent + "  dictionary_attack_counter: ";
-    base::StringAppendF(&output, "%u (0x%08X)",
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")",
                         value.dictionary_attack_counter(),
                         value.dictionary_attack_counter());
     output += "\n";
   }
   if (value.has_dictionary_attack_threshold()) {
     output += indent + "  dictionary_attack_threshold: ";
-    base::StringAppendF(&output, "%u (0x%08X)",
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")",
                         value.dictionary_attack_threshold(),
                         value.dictionary_attack_threshold());
     output += "\n";
@@ -726,11 +794,19 @@
   }
   if (value.has_dictionary_attack_lockout_seconds_remaining()) {
     output += indent + "  dictionary_attack_lockout_seconds_remaining: ";
-    base::StringAppendF(&output, "%u (0x%08X)",
+    base::StringAppendF(&output, "%" PRIu32 " (0x%08" PRIX32 ")",
                         value.dictionary_attack_lockout_seconds_remaining(),
                         value.dictionary_attack_lockout_seconds_remaining());
     output += "\n";
   }
+  if (value.has_version_info()) {
+    output += indent + "  version_info: ";
+    base::StringAppendF(
+        &output, "%s",
+        GetProtoDebugStringWithIndent(value.version_info(), indent_size + 2)
+            .c_str());
+    output += "\n";
+  }
   output += indent + "}\n";
   return output;
 }
diff --git a/tpm_manager/common/print_tpm_manager_proto.h b/tpm_manager/common/print_tpm_manager_proto.h
index 7822be8..2180ae4 100644
--- a/tpm_manager/common/print_tpm_manager_proto.h
+++ b/tpm_manager/common/print_tpm_manager_proto.h
@@ -87,6 +87,10 @@
 std::string GetProtoDebugStringWithIndent(const GetTpmStatusRequest& value,
                                           int indent_size);
 std::string GetProtoDebugString(const GetTpmStatusRequest& value);
+std::string GetProtoDebugStringWithIndent(
+    const GetTpmStatusReply::TpmVersionInfo& value,
+    int indent_size);
+std::string GetProtoDebugString(const GetTpmStatusReply::TpmVersionInfo& value);
 std::string GetProtoDebugStringWithIndent(const GetTpmStatusReply& value,
                                           int indent_size);
 std::string GetProtoDebugString(const GetTpmStatusReply& value);