| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "analyze.h" |
| 4 |
#include "analyze-has-tpm2.h" |
| 5 |
#include "format-table.h" |
| 6 |
#include "log.h" |
| 7 |
#include "string-util.h" |
| 8 |
#include "time-util.h" |
| 9 |
#include "tpm2-util.h" |
| 10 |
|
| 11 |
int verb_has_tpm2(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 12 |
return verb_has_tpm2_generic(arg_quiet); |
| 13 |
} |
| 14 |
|
| 15 |
int verb_identify_tpm2(int argc, char **argv, uintptr_t _data, void *userdata) { |
| 16 |
#if HAVE_TPM2 |
| 17 |
int r; |
| 18 |
|
| 19 |
_cleanup_(tpm2_context_unrefp) Tpm2Context *c = NULL; |
| 20 |
r = tpm2_context_new_or_warn(/* device= */ NULL, &c); |
| 21 |
if (r < 0) |
| 22 |
return r; |
| 23 |
|
| 24 |
Tpm2VendorInfo info; |
| 25 |
r = tpm2_get_vendor_info(c, &info); |
| 26 |
if (r < 0) |
| 27 |
return log_error_errno(r, "Failed to acquire TPM2 vendor information: %m"); |
| 28 |
|
| 29 |
_cleanup_(table_unrefp) Table *table = table_new_vertical(); |
| 30 |
if (!table) |
| 31 |
return log_oom(); |
| 32 |
|
| 33 |
if (!isempty(info.family_indicator)) { |
| 34 |
r = table_add_many( |
| 35 |
table, |
| 36 |
TABLE_FIELD, "Family Indicator", |
| 37 |
TABLE_STRING, info.family_indicator); |
| 38 |
if (r < 0) |
| 39 |
return table_log_add_error(r); |
| 40 |
} |
| 41 |
|
| 42 |
_cleanup_free_ char *rv = NULL; |
| 43 |
if (asprintf(&rv, "%" PRIu32 ".%" PRIu32, |
| 44 |
info.revision_major, |
| 45 |
info.revision_minor) < 0) |
| 46 |
return log_oom(); |
| 47 |
|
| 48 |
r = table_add_many( |
| 49 |
table, |
| 50 |
TABLE_FIELD, "Level", |
| 51 |
TABLE_UINT32, info.level, |
| 52 |
TABLE_FIELD, "Revision", |
| 53 |
TABLE_STRING, rv); |
| 54 |
if (r < 0) |
| 55 |
return table_log_add_error(r); |
| 56 |
|
| 57 |
if (info.year >= 1900) { |
| 58 |
struct tm tm = { |
| 59 |
.tm_year = info.year - 1900, |
| 60 |
.tm_mon = 0, /* january */ |
| 61 |
.tm_mday = info.day_of_year, /* timegm() will normalize this */ |
| 62 |
}; |
| 63 |
|
| 64 |
usec_t ts; |
| 65 |
r = mktime_or_timegm_usec(&tm, /* utc= */ true, &ts); |
| 66 |
if (r < 0) |
| 67 |
log_debug_errno(r, "Failed to convert the specification date, ignoring."); |
| 68 |
else { |
| 69 |
r = table_add_many( |
| 70 |
table, |
| 71 |
TABLE_FIELD, "Specification Date", |
| 72 |
TABLE_TIMESTAMP_DATE, ts); |
| 73 |
if (r < 0) |
| 74 |
return table_log_add_error(r); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if (!isempty(info.manufacturer)) { |
| 79 |
r = table_add_many( |
| 80 |
table, |
| 81 |
TABLE_FIELD, "Manufacturer", |
| 82 |
TABLE_STRING, info.manufacturer); |
| 83 |
if (r < 0) |
| 84 |
return table_log_add_error(r); |
| 85 |
} |
| 86 |
|
| 87 |
if (!isempty(info.vendor_string)) { |
| 88 |
r = table_add_many( |
| 89 |
table, |
| 90 |
TABLE_FIELD, "Vendor String", |
| 91 |
TABLE_STRING, info.vendor_string); |
| 92 |
if (r < 0) |
| 93 |
return table_log_add_error(r); |
| 94 |
} |
| 95 |
|
| 96 |
if (info.vendor_tpm_type != 0) { |
| 97 |
r = table_add_many( |
| 98 |
table, |
| 99 |
TABLE_FIELD, "Vendor TPM Type", |
| 100 |
TABLE_UINT32_HEX_0x, info.vendor_tpm_type); |
| 101 |
if (r < 0) |
| 102 |
return table_log_add_error(r); |
| 103 |
} |
| 104 |
|
| 105 |
/* Show the first two 16bit words of the firmware version as major/minor */ |
| 106 |
_cleanup_free_ char *fw = NULL; |
| 107 |
if (asprintf(&fw, "%" PRIu16 ".%" PRIu16, |
| 108 |
info.firmware_version_major, |
| 109 |
info.firmware_version_minor) < 0) |
| 110 |
return log_oom(); |
| 111 |
|
| 112 |
/* Show the second 32bit as a single value, if non-zero */ |
| 113 |
if (info.firmware_version2 != 0 && strextendf(&fw, ".%" PRIu32, info.firmware_version2) < 0) |
| 114 |
return log_oom(); |
| 115 |
|
| 116 |
r = table_add_many( |
| 117 |
table, |
| 118 |
TABLE_FIELD, "Firmware Version", |
| 119 |
TABLE_STRING, fw); |
| 120 |
if (r < 0) |
| 121 |
return table_log_add_error(r); |
| 122 |
|
| 123 |
_cleanup_free_ char *m = NULL; |
| 124 |
if (tpm2_vendor_info_to_modalias(&info, &m) < 0) |
| 125 |
return log_oom(); |
| 126 |
|
| 127 |
r = table_add_many( |
| 128 |
table, |
| 129 |
TABLE_FIELD, "Modalias String", |
| 130 |
TABLE_STRING, m); |
| 131 |
if (r < 0) |
| 132 |
return table_log_add_error(r); |
| 133 |
|
| 134 |
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */ false); |
| 135 |
if (r < 0) |
| 136 |
return r; |
| 137 |
|
| 138 |
return EXIT_SUCCESS; |
| 139 |
#else |
| 140 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support not enabled at build time."); |
| 141 |
#endif |
| 142 |
} |