]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tpm2-util,analyze: add helper for generating hwdb lookup key from TPM2 vendor data
authorLennart Poettering <lennart@amutable.com>
Tue, 3 Mar 2026 11:28:45 +0000 (12:28 +0100)
committerLennart Poettering <lennart@amutable.com>
Wed, 4 Mar 2026 07:28:29 +0000 (08:28 +0100)
Our goal is to find TPM2 metadata in hwdb, hence let's compile a
"modalias"-style string from the TPM2 metadata, we can use as hwdb
lookup key.

man/systemd-analyze.xml
src/analyze/analyze-has-tpm2.c
src/shared/tpm2-util.c
src/shared/tpm2-util.h

index 6a022916664f6d022914a4f9b2bae4cba2f7327c..f3dfd7479f87f9cce2a854f0c36184ec3373bed5 100644 (file)
@@ -1047,7 +1047,8 @@ default         ignore      -         -</programlisting>
 Specification Date: Mon 2023-01-09
       Manufacturer: STM
      Vendor String: ST33KTPM2XSPI
-  Firmware Version: 9.258</programlisting>
+  Firmware Version: 9.258
+   Modalias String: fi2.0:lv0:rv1.59:sy2023:sd9:mfSTM:vsST33KTPM2XSPI:ty0:fw9.258.0:</programlisting>
       </example>
 
       <xi:include href="version-info.xml" xpointer="v260"/>
index a63c44ebc649e6744f34d2f96a951e083ddcbd2a..2e7890cea148fbf7b48483cdb0ef4621bd421a85 100644 (file)
@@ -120,6 +120,17 @@ int verb_identify_tpm2(int argc, char **argv, void *userdata) {
         if (r < 0)
                 return table_log_add_error(r);
 
+        _cleanup_free_ char *m = NULL;
+        if (tpm2_vendor_info_to_modalias(&info, &m) < 0)
+                return log_oom();
+
+        r = table_add_many(
+                        table,
+                        TABLE_FIELD, "Modalias String",
+                        TABLE_STRING, m);
+        if (r < 0)
+                return table_log_add_error(r);
+
         r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */ false);
         if (r < 0)
                 return r;
index 64332fda32de388a99c47502a6f983126bddb60b..2705f3b79426a515587ea722714ca44d8d8b279b 100644 (file)
@@ -349,6 +349,43 @@ static int tpm2_get_capability(
         return more == TPM2_YES;
 }
 
+int tpm2_vendor_info_to_modalias(const Tpm2VendorInfo *info, char **ret) {
+        _cleanup_free_ char *m = NULL;
+
+        assert(info);
+        assert(ret);
+
+        /* Closely inspired by kernel modalias strings, this distills information from the TPM vendor data
+         * into a string suitable for matching hwdb */
+
+        if (asprintf(&m,
+                     "fi%s:"
+                     "lv%" PRIu32 ":"
+                     "rv%" PRIu32 ".%" PRIu32 ":"
+                     "sy%" PRIu32 ":"
+                     "sd%" PRIu32 ":"
+                     "mf%s:"
+                     "vs%s:"
+                     "ty%" PRIx32 ":"
+                     "fw%" PRIu16 ".%" PRIu16 ".%" PRIu32 ":",
+                     info->family_indicator,
+                     info->level,
+                     info->revision_major,
+                     info->revision_minor,
+                     info->year,
+                     info->day_of_year,
+                     info->manufacturer,
+                     info->vendor_string,
+                     info->vendor_tpm_type,
+                     info->firmware_version_major,
+                     info->firmware_version_minor,
+                     info->firmware_version2) < 0)
+                return -ENOMEM;
+
+        *ret = TAKE_PTR(m);
+        return 0;
+}
+
 static char *mangle_vendor_chars(char *c, size_t n) {
         char *end = c;
         assert(c || n == 0);
index cbdad197191aeacc9e9585803e861facd17574fb..9bccdd9712cd0a31925438a444c48733e9825a84 100644 (file)
@@ -210,6 +210,7 @@ typedef struct Tpm2VendorInfo {
         char vendor_string[4*4+1];
 } Tpm2VendorInfo;
 
+int tpm2_vendor_info_to_modalias(const Tpm2VendorInfo *info, char **ret);
 int tpm2_get_vendor_info(Tpm2Context *c, Tpm2VendorInfo *ret);
 
 void tpm2_log_debug_tpml_pcr_selection(const TPML_PCR_SELECTION *l, const char *msg);