hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/char/tpm/tpm-sysfs.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) 2004 IBM Corporation
34 * Authors:
....@@ -10,12 +11,6 @@
1011 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
1112 *
1213 * sysfs filesystem inspection interface to the TPM
13
- *
14
- * This program is free software; you can redistribute it and/or
15
- * modify it under the terms of the GNU General Public License as
16
- * published by the Free Software Foundation, version 2 of the
17
- * License.
18
- *
1914 */
2015 #include <linux/device.h>
2116 #include "tpm.h"
....@@ -54,39 +49,27 @@
5449
5550 tpm_buf_append(&tpm_buf, anti_replay, sizeof(anti_replay));
5651
57
- if (tpm_transmit_cmd(chip, NULL, tpm_buf.data, PAGE_SIZE,
58
- READ_PUBEK_RESULT_MIN_BODY_SIZE, 0,
59
- "attempting to read the PUBEK"))
52
+ if (tpm_transmit_cmd(chip, &tpm_buf, READ_PUBEK_RESULT_MIN_BODY_SIZE,
53
+ "attempting to read the PUBEK"))
6054 goto out_buf;
6155
6256 out = (struct tpm_readpubek_out *)&tpm_buf.data[10];
6357 str +=
6458 sprintf(str,
65
- "Algorithm: %02X %02X %02X %02X\n"
66
- "Encscheme: %02X %02X\n"
67
- "Sigscheme: %02X %02X\n"
68
- "Parameters: %02X %02X %02X %02X "
69
- "%02X %02X %02X %02X "
70
- "%02X %02X %02X %02X\n"
59
+ "Algorithm: %4ph\n"
60
+ "Encscheme: %2ph\n"
61
+ "Sigscheme: %2ph\n"
62
+ "Parameters: %12ph\n"
7163 "Modulus length: %d\n"
7264 "Modulus:\n",
73
- out->algorithm[0], out->algorithm[1], out->algorithm[2],
74
- out->algorithm[3],
75
- out->encscheme[0], out->encscheme[1],
76
- out->sigscheme[0], out->sigscheme[1],
77
- out->parameters[0], out->parameters[1],
78
- out->parameters[2], out->parameters[3],
79
- out->parameters[4], out->parameters[5],
80
- out->parameters[6], out->parameters[7],
81
- out->parameters[8], out->parameters[9],
82
- out->parameters[10], out->parameters[11],
65
+ out->algorithm,
66
+ out->encscheme,
67
+ out->sigscheme,
68
+ out->parameters,
8369 be32_to_cpu(out->keysize));
8470
85
- for (i = 0; i < 256; i++) {
86
- str += sprintf(str, "%02X ", out->modulus[i]);
87
- if ((i + 1) % 16 == 0)
88
- str += sprintf(str, "\n");
89
- }
71
+ for (i = 0; i < 256; i += 16)
72
+ str += sprintf(str, "%16ph\n", &out->modulus[i]);
9073
9174 out_buf:
9275 tpm_buf_destroy(&tpm_buf);
....@@ -101,26 +84,26 @@
10184 {
10285 cap_t cap;
10386 u8 digest[TPM_DIGEST_SIZE];
104
- ssize_t rc;
105
- int i, j, num_pcrs;
87
+ u32 i, j, num_pcrs;
10688 char *str = buf;
10789 struct tpm_chip *chip = to_tpm_chip(dev);
10890
10991 if (tpm_try_get_ops(chip))
11092 return 0;
11193
112
- if (tpm_getcap(chip, TPM_CAP_PROP_PCR, &cap,
113
- "attempting to determine the number of PCRS",
114
- sizeof(cap.num_pcrs))) {
94
+ if (tpm1_getcap(chip, TPM_CAP_PROP_PCR, &cap,
95
+ "attempting to determine the number of PCRS",
96
+ sizeof(cap.num_pcrs))) {
11597 tpm_put_ops(chip);
11698 return 0;
11799 }
118100
119101 num_pcrs = be32_to_cpu(cap.num_pcrs);
120102 for (i = 0; i < num_pcrs; i++) {
121
- rc = tpm_pcr_read_dev(chip, i, digest);
122
- if (rc)
103
+ if (tpm1_pcr_read(chip, i, digest)) {
104
+ str = buf;
123105 break;
106
+ }
124107 str += sprintf(str, "PCR-%02d: ", i);
125108 for (j = 0; j < TPM_DIGEST_SIZE; j++)
126109 str += sprintf(str, "%02X ", digest[j]);
....@@ -141,9 +124,9 @@
141124 if (tpm_try_get_ops(chip))
142125 return 0;
143126
144
- if (tpm_getcap(chip, TPM_CAP_FLAG_PERM, &cap,
145
- "attempting to determine the permanent enabled state",
146
- sizeof(cap.perm_flags)))
127
+ if (tpm1_getcap(chip, TPM_CAP_FLAG_PERM, &cap,
128
+ "attempting to determine the permanent enabled state",
129
+ sizeof(cap.perm_flags)))
147130 goto out_ops;
148131
149132 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
....@@ -163,9 +146,9 @@
163146 if (tpm_try_get_ops(chip))
164147 return 0;
165148
166
- if (tpm_getcap(chip, TPM_CAP_FLAG_PERM, &cap,
167
- "attempting to determine the permanent active state",
168
- sizeof(cap.perm_flags)))
149
+ if (tpm1_getcap(chip, TPM_CAP_FLAG_PERM, &cap,
150
+ "attempting to determine the permanent active state",
151
+ sizeof(cap.perm_flags)))
169152 goto out_ops;
170153
171154 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
....@@ -185,9 +168,9 @@
185168 if (tpm_try_get_ops(chip))
186169 return 0;
187170
188
- if (tpm_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap,
189
- "attempting to determine the owner state",
190
- sizeof(cap.owned)))
171
+ if (tpm1_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap,
172
+ "attempting to determine the owner state",
173
+ sizeof(cap.owned)))
191174 goto out_ops;
192175
193176 rc = sprintf(buf, "%d\n", cap.owned);
....@@ -207,9 +190,9 @@
207190 if (tpm_try_get_ops(chip))
208191 return 0;
209192
210
- if (tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap,
211
- "attempting to determine the temporary state",
212
- sizeof(cap.stclear_flags)))
193
+ if (tpm1_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap,
194
+ "attempting to determine the temporary state",
195
+ sizeof(cap.stclear_flags)))
213196 goto out_ops;
214197
215198 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
....@@ -223,6 +206,7 @@
223206 char *buf)
224207 {
225208 struct tpm_chip *chip = to_tpm_chip(dev);
209
+ struct tpm1_version *version;
226210 ssize_t rc = 0;
227211 char *str = buf;
228212 cap_t cap;
....@@ -230,40 +214,39 @@
230214 if (tpm_try_get_ops(chip))
231215 return 0;
232216
233
- if (tpm_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
234
- "attempting to determine the manufacturer",
235
- sizeof(cap.manufacturer_id)))
217
+ if (tpm1_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
218
+ "attempting to determine the manufacturer",
219
+ sizeof(cap.manufacturer_id)))
236220 goto out_ops;
237221
238222 str += sprintf(str, "Manufacturer: 0x%x\n",
239223 be32_to_cpu(cap.manufacturer_id));
240224
241
- /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */
242
- rc = tpm_getcap(chip, TPM_CAP_VERSION_1_2, &cap,
243
- "attempting to determine the 1.2 version",
244
- sizeof(cap.tpm_version_1_2));
245
- if (!rc) {
246
- str += sprintf(str,
247
- "TCG version: %d.%d\nFirmware version: %d.%d\n",
248
- cap.tpm_version_1_2.Major,
249
- cap.tpm_version_1_2.Minor,
250
- cap.tpm_version_1_2.revMajor,
251
- cap.tpm_version_1_2.revMinor);
252
- } else {
253
- /* Otherwise just use TPM_STRUCT_VER */
254
- if (tpm_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
255
- "attempting to determine the 1.1 version",
256
- sizeof(cap.tpm_version)))
257
- goto out_ops;
225
+ /* TPM 1.2 */
226
+ if (!tpm1_getcap(chip, TPM_CAP_VERSION_1_2, &cap,
227
+ "attempting to determine the 1.2 version",
228
+ sizeof(cap.version2))) {
229
+ version = &cap.version2.version;
230
+ goto out_print;
231
+ }
258232
259
- str += sprintf(str,
260
- "TCG version: %d.%d\nFirmware version: %d.%d\n",
261
- cap.tpm_version.Major,
262
- cap.tpm_version.Minor,
263
- cap.tpm_version.revMajor,
264
- cap.tpm_version.revMinor);
265
-}
233
+ /* TPM 1.1 */
234
+ if (tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
235
+ "attempting to determine the 1.1 version",
236
+ sizeof(cap.version1))) {
237
+ goto out_ops;
238
+ }
239
+
240
+ version = &cap.version1;
241
+
242
+out_print:
243
+ str += sprintf(str,
244
+ "TCG version: %d.%d\nFirmware version: %d.%d\n",
245
+ version->major, version->minor,
246
+ version->rev_major, version->rev_minor);
247
+
266248 rc = str - buf;
249
+
267250 out_ops:
268251 tpm_put_ops(chip);
269252 return rc;
....@@ -316,7 +299,17 @@
316299 }
317300 static DEVICE_ATTR_RO(timeouts);
318301
319
-static struct attribute *tpm_dev_attrs[] = {
302
+static ssize_t tpm_version_major_show(struct device *dev,
303
+ struct device_attribute *attr, char *buf)
304
+{
305
+ struct tpm_chip *chip = to_tpm_chip(dev);
306
+
307
+ return sprintf(buf, "%s\n", chip->flags & TPM_CHIP_FLAG_TPM2
308
+ ? "2" : "1");
309
+}
310
+static DEVICE_ATTR_RO(tpm_version_major);
311
+
312
+static struct attribute *tpm1_dev_attrs[] = {
320313 &dev_attr_pubek.attr,
321314 &dev_attr_pcrs.attr,
322315 &dev_attr_enabled.attr,
....@@ -327,25 +320,28 @@
327320 &dev_attr_cancel.attr,
328321 &dev_attr_durations.attr,
329322 &dev_attr_timeouts.attr,
323
+ &dev_attr_tpm_version_major.attr,
330324 NULL,
331325 };
332326
333
-static const struct attribute_group tpm_dev_group = {
334
- .attrs = tpm_dev_attrs,
327
+static struct attribute *tpm2_dev_attrs[] = {
328
+ &dev_attr_tpm_version_major.attr,
329
+ NULL
330
+};
331
+
332
+static const struct attribute_group tpm1_dev_group = {
333
+ .attrs = tpm1_dev_attrs,
334
+};
335
+
336
+static const struct attribute_group tpm2_dev_group = {
337
+ .attrs = tpm2_dev_attrs,
335338 };
336339
337340 void tpm_sysfs_add_device(struct tpm_chip *chip)
338341 {
339
- /* XXX: If you wish to remove this restriction, you must first update
340
- * tpm_sysfs to explicitly lock chip->ops.
341
- */
342
- if (chip->flags & TPM_CHIP_FLAG_TPM2)
343
- return;
344
-
345
- /* The sysfs routines rely on an implicit tpm_try_get_ops, device_del
346
- * is called before ops is null'd and the sysfs core synchronizes this
347
- * removal so that no callbacks are running or can run again
348
- */
349342 WARN_ON(chip->groups_cnt != 0);
350
- chip->groups[chip->groups_cnt++] = &tpm_dev_group;
343
+ if (chip->flags & TPM_CHIP_FLAG_TPM2)
344
+ chip->groups[chip->groups_cnt++] = &tpm2_dev_group;
345
+ else
346
+ chip->groups[chip->groups_cnt++] = &tpm1_dev_group;
351347 }