blob: 3733491671ca96fd2650d000775d8ffdb1851092 [file] [log] [blame]
Jason Gunthorpe000a07b2013-11-26 13:30:41 -07001/*
2 * Copyright (C) 2004 IBM Corporation
3 * Authors:
4 * Leendert van Doorn <leendert@watson.ibm.com>
5 * Dave Safford <safford@watson.ibm.com>
6 * Reiner Sailer <sailer@watson.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
8 *
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -07009 * Copyright (C) 2013 Obsidian Research Corp
10 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
11 *
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070012 * 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 *
19 */
20#include <linux/device.h>
21#include "tpm.h"
22
Jarkko Sakkinenda379f32017-06-20 11:38:03 +020023struct tpm_readpubek_out {
24 u8 algorithm[4];
25 u8 encscheme[2];
26 u8 sigscheme[2];
27 __be32 paramsize;
28 u8 parameters[12];
29 __be32 keysize;
30 u8 modulus[256];
31 u8 checksum[20];
32} __packed;
33
Stefan Bergerc659af72017-01-19 07:19:12 -050034#define READ_PUBEK_RESULT_MIN_BODY_SIZE (28 + 256)
Roberto Sassua69faeb2017-05-03 18:19:10 +020035#define TPM_ORD_READPUBEK 124
Jarkko Sakkinenda379f32017-06-20 11:38:03 +020036
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -070037static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
38 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070039{
Jarkko Sakkinenda379f32017-06-20 11:38:03 +020040 struct tpm_buf tpm_buf;
41 struct tpm_readpubek_out *out;
Jarkko Sakkinenda379f32017-06-20 11:38:03 +020042 int i;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070043 char *str = buf;
Jason Gunthorpe062807f2016-04-18 13:26:13 -040044 struct tpm_chip *chip = to_tpm_chip(dev);
Jarkko Sakkinenda379f32017-06-20 11:38:03 +020045 char anti_replay[20];
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070046
Jarkko Sakkinenda379f32017-06-20 11:38:03 +020047 memset(&anti_replay, 0, sizeof(anti_replay));
Jarkko Sakkinen13b47cf2017-06-20 11:38:02 +020048
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +020049 if (tpm_try_get_ops(chip))
Jarkko Sakkinenc6286102018-11-04 12:39:02 +020050 return 0;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070051
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +020052 if (tpm_buf_init(&tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK))
53 goto out_ops;
54
Jarkko Sakkinenda379f32017-06-20 11:38:03 +020055 tpm_buf_append(&tpm_buf, anti_replay, sizeof(anti_replay));
56
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +020057 if (tpm_transmit_cmd(chip, &tpm_buf, READ_PUBEK_RESULT_MIN_BODY_SIZE,
58 0, "attempting to read the PUBEK"))
59 goto out_buf;
Jarkko Sakkinenda379f32017-06-20 11:38:03 +020060
61 out = (struct tpm_readpubek_out *)&tpm_buf.data[10];
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070062 str +=
63 sprintf(str,
64 "Algorithm: %02X %02X %02X %02X\n"
65 "Encscheme: %02X %02X\n"
66 "Sigscheme: %02X %02X\n"
67 "Parameters: %02X %02X %02X %02X "
68 "%02X %02X %02X %02X "
69 "%02X %02X %02X %02X\n"
70 "Modulus length: %d\n"
71 "Modulus:\n",
Jarkko Sakkinenda379f32017-06-20 11:38:03 +020072 out->algorithm[0], out->algorithm[1], out->algorithm[2],
73 out->algorithm[3],
74 out->encscheme[0], out->encscheme[1],
75 out->sigscheme[0], out->sigscheme[1],
76 out->parameters[0], out->parameters[1],
77 out->parameters[2], out->parameters[3],
78 out->parameters[4], out->parameters[5],
79 out->parameters[6], out->parameters[7],
80 out->parameters[8], out->parameters[9],
81 out->parameters[10], out->parameters[11],
82 be32_to_cpu(out->keysize));
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070083
84 for (i = 0; i < 256; i++) {
Jarkko Sakkinenda379f32017-06-20 11:38:03 +020085 str += sprintf(str, "%02X ", out->modulus[i]);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070086 if ((i + 1) % 16 == 0)
87 str += sprintf(str, "\n");
88 }
Jarkko Sakkinenda379f32017-06-20 11:38:03 +020089
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +020090out_buf:
Jarkko Sakkinenda379f32017-06-20 11:38:03 +020091 tpm_buf_destroy(&tpm_buf);
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +020092out_ops:
93 tpm_put_ops(chip);
94 return str - buf;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070095}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -070096static DEVICE_ATTR_RO(pubek);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070097
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -070098static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr,
99 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700100{
101 cap_t cap;
102 u8 digest[TPM_DIGEST_SIZE];
Tomas Winkler95adc6b2018-10-19 21:23:07 +0300103 u32 i, j, num_pcrs;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700104 char *str = buf;
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400105 struct tpm_chip *chip = to_tpm_chip(dev);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700106
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200107 if (tpm_try_get_ops(chip))
108 return 0;
109
Jarkko Sakkinen41484672018-11-04 14:42:29 +0200110 if (tpm1_getcap(chip, TPM_CAP_PROP_PCR, &cap,
111 "attempting to determine the number of PCRS",
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200112 sizeof(cap.num_pcrs))) {
113 tpm_put_ops(chip);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700114 return 0;
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200115 }
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700116
117 num_pcrs = be32_to_cpu(cap.num_pcrs);
118 for (i = 0; i < num_pcrs; i++) {
Jarkko Sakkinen41484672018-11-04 14:42:29 +0200119 if (tpm1_pcr_read(chip, i, digest)) {
120 str = buf;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700121 break;
Jarkko Sakkinen41484672018-11-04 14:42:29 +0200122 }
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700123 str += sprintf(str, "PCR-%02d: ", i);
124 for (j = 0; j < TPM_DIGEST_SIZE; j++)
125 str += sprintf(str, "%02X ", digest[j]);
126 str += sprintf(str, "\n");
127 }
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200128 tpm_put_ops(chip);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700129 return str - buf;
130}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700131static DEVICE_ATTR_RO(pcrs);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700132
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700133static ssize_t enabled_show(struct device *dev, struct device_attribute *attr,
134 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700135{
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200136 struct tpm_chip *chip = to_tpm_chip(dev);
137 ssize_t rc = 0;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700138 cap_t cap;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700139
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200140 if (tpm_try_get_ops(chip))
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700141 return 0;
142
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200143 if (tpm1_getcap(chip, TPM_CAP_FLAG_PERM, &cap,
144 "attempting to determine the permanent enabled state",
145 sizeof(cap.perm_flags)))
146 goto out_ops;
147
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700148 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200149out_ops:
150 tpm_put_ops(chip);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700151 return rc;
152}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700153static DEVICE_ATTR_RO(enabled);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700154
Fengguang Wu5f648222013-12-07 16:10:26 +0100155static ssize_t active_show(struct device *dev, struct device_attribute *attr,
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700156 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700157{
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200158 struct tpm_chip *chip = to_tpm_chip(dev);
159 ssize_t rc = 0;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700160 cap_t cap;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700161
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200162 if (tpm_try_get_ops(chip))
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700163 return 0;
164
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200165 if (tpm1_getcap(chip, TPM_CAP_FLAG_PERM, &cap,
166 "attempting to determine the permanent active state",
167 sizeof(cap.perm_flags)))
168 goto out_ops;
169
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700170 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200171out_ops:
172 tpm_put_ops(chip);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700173 return rc;
174}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700175static DEVICE_ATTR_RO(active);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700176
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700177static ssize_t owned_show(struct device *dev, struct device_attribute *attr,
178 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700179{
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200180 struct tpm_chip *chip = to_tpm_chip(dev);
181 ssize_t rc = 0;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700182 cap_t cap;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700183
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200184 if (tpm_try_get_ops(chip))
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700185 return 0;
186
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200187 if (tpm1_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap,
188 "attempting to determine the owner state",
189 sizeof(cap.owned)))
190 goto out_ops;
191
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700192 rc = sprintf(buf, "%d\n", cap.owned);
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200193out_ops:
194 tpm_put_ops(chip);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700195 return rc;
196}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700197static DEVICE_ATTR_RO(owned);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700198
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700199static ssize_t temp_deactivated_show(struct device *dev,
200 struct device_attribute *attr, char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700201{
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200202 struct tpm_chip *chip = to_tpm_chip(dev);
203 ssize_t rc = 0;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700204 cap_t cap;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700205
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200206 if (tpm_try_get_ops(chip))
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700207 return 0;
208
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200209 if (tpm1_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap,
210 "attempting to determine the temporary state",
211 sizeof(cap.stclear_flags)))
212 goto out_ops;
213
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700214 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200215out_ops:
216 tpm_put_ops(chip);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700217 return rc;
218}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700219static DEVICE_ATTR_RO(temp_deactivated);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700220
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700221static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
222 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700223{
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400224 struct tpm_chip *chip = to_tpm_chip(dev);
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200225 ssize_t rc = 0;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700226 char *str = buf;
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200227 cap_t cap;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700228
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200229 if (tpm_try_get_ops(chip))
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700230 return 0;
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200231
232 if (tpm1_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
233 "attempting to determine the manufacturer",
234 sizeof(cap.manufacturer_id)))
235 goto out_ops;
236
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700237 str += sprintf(str, "Manufacturer: 0x%x\n",
238 be32_to_cpu(cap.manufacturer_id));
239
240 /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */
Tomas Winklerf4d916b2018-10-19 21:22:54 +0300241 rc = tpm1_getcap(chip, TPM_CAP_VERSION_1_2, &cap,
242 "attempting to determine the 1.2 version",
243 sizeof(cap.tpm_version_1_2));
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700244 if (!rc) {
245 str += sprintf(str,
246 "TCG version: %d.%d\nFirmware version: %d.%d\n",
247 cap.tpm_version_1_2.Major,
248 cap.tpm_version_1_2.Minor,
249 cap.tpm_version_1_2.revMajor,
250 cap.tpm_version_1_2.revMinor);
251 } else {
252 /* Otherwise just use TPM_STRUCT_VER */
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200253 if (tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
254 "attempting to determine the 1.1 version",
255 sizeof(cap.tpm_version)))
256 goto out_ops;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700257 str += sprintf(str,
258 "TCG version: %d.%d\nFirmware version: %d.%d\n",
259 cap.tpm_version.Major,
260 cap.tpm_version.Minor,
261 cap.tpm_version.revMajor,
262 cap.tpm_version.revMinor);
263 }
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200264 rc = str - buf;
265out_ops:
266 tpm_put_ops(chip);
267 return rc;
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700268}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700269static DEVICE_ATTR_RO(caps);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700270
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700271static ssize_t cancel_store(struct device *dev, struct device_attribute *attr,
272 const char *buf, size_t count)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700273{
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400274 struct tpm_chip *chip = to_tpm_chip(dev);
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200275
276 if (tpm_try_get_ops(chip))
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700277 return 0;
278
Jason Gunthorpe5f82e9f2013-11-26 13:30:44 -0700279 chip->ops->cancel(chip);
Jarkko Sakkinen2677ca92018-11-04 11:38:27 +0200280 tpm_put_ops(chip);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700281 return count;
282}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700283static DEVICE_ATTR_WO(cancel);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700284
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700285static ssize_t durations_show(struct device *dev, struct device_attribute *attr,
286 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700287{
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400288 struct tpm_chip *chip = to_tpm_chip(dev);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700289
Christophe Ricardaf782f32016-03-31 22:56:59 +0200290 if (chip->duration[TPM_LONG] == 0)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700291 return 0;
292
293 return sprintf(buf, "%d %d %d [%s]\n",
Christophe Ricardaf782f32016-03-31 22:56:59 +0200294 jiffies_to_usecs(chip->duration[TPM_SHORT]),
295 jiffies_to_usecs(chip->duration[TPM_MEDIUM]),
296 jiffies_to_usecs(chip->duration[TPM_LONG]),
297 chip->duration_adjusted
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700298 ? "adjusted" : "original");
299}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700300static DEVICE_ATTR_RO(durations);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700301
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700302static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr,
303 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700304{
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400305 struct tpm_chip *chip = to_tpm_chip(dev);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700306
307 return sprintf(buf, "%d %d %d %d [%s]\n",
Christophe Ricardaf782f32016-03-31 22:56:59 +0200308 jiffies_to_usecs(chip->timeout_a),
309 jiffies_to_usecs(chip->timeout_b),
310 jiffies_to_usecs(chip->timeout_c),
311 jiffies_to_usecs(chip->timeout_d),
312 chip->timeout_adjusted
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700313 ? "adjusted" : "original");
314}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700315static DEVICE_ATTR_RO(timeouts);
316
317static struct attribute *tpm_dev_attrs[] = {
318 &dev_attr_pubek.attr,
319 &dev_attr_pcrs.attr,
320 &dev_attr_enabled.attr,
321 &dev_attr_active.attr,
322 &dev_attr_owned.attr,
323 &dev_attr_temp_deactivated.attr,
324 &dev_attr_caps.attr,
325 &dev_attr_cancel.attr,
326 &dev_attr_durations.attr,
327 &dev_attr_timeouts.attr,
328 NULL,
329};
330
331static const struct attribute_group tpm_dev_group = {
332 .attrs = tpm_dev_attrs,
333};
334
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400335void tpm_sysfs_add_device(struct tpm_chip *chip)
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700336{
Josh Zimmermand1bd4a72017-06-25 14:53:24 -0700337 /* XXX: If you wish to remove this restriction, you must first update
338 * tpm_sysfs to explicitly lock chip->ops.
339 */
Jarkko Sakkinen7518a212016-11-14 05:00:51 -0500340 if (chip->flags & TPM_CHIP_FLAG_TPM2)
341 return;
342
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400343 /* The sysfs routines rely on an implicit tpm_try_get_ops, device_del
344 * is called before ops is null'd and the sysfs core synchronizes this
345 * removal so that no callbacks are running or can run again
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700346 */
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400347 WARN_ON(chip->groups_cnt != 0);
348 chip->groups[chip->groups_cnt++] = &tpm_dev_group;
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700349}