blob: b46cf70c8b16b4b06b8218c31a0952dd02ad7bdf [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
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070023#define READ_PUBEK_RESULT_SIZE 314
24#define TPM_ORD_READPUBEK cpu_to_be32(124)
25static struct tpm_input_header tpm_readpubek_header = {
26 .tag = TPM_TAG_RQU_COMMAND,
27 .length = cpu_to_be32(30),
28 .ordinal = TPM_ORD_READPUBEK
29};
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -070030static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
31 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070032{
33 u8 *data;
34 struct tpm_cmd_t tpm_cmd;
35 ssize_t err;
36 int i, rc;
37 char *str = buf;
38
Jason Gunthorpe062807f2016-04-18 13:26:13 -040039 struct tpm_chip *chip = to_tpm_chip(dev);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070040
41 tpm_cmd.header.in = tpm_readpubek_header;
Jarkko Sakkinen87155b72014-12-12 11:46:33 -080042 err = tpm_transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
43 "attempting to read the PUBEK");
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070044 if (err)
45 goto out;
46
47 /*
48 ignore header 10 bytes
49 algorithm 32 bits (1 == RSA )
50 encscheme 16 bits
51 sigscheme 16 bits
52 parameters (RSA 12->bytes: keybit, #primes, expbit)
53 keylenbytes 32 bits
54 256 byte modulus
55 ignore checksum 20 bytes
56 */
57 data = tpm_cmd.params.readpubek_out_buffer;
58 str +=
59 sprintf(str,
60 "Algorithm: %02X %02X %02X %02X\n"
61 "Encscheme: %02X %02X\n"
62 "Sigscheme: %02X %02X\n"
63 "Parameters: %02X %02X %02X %02X "
64 "%02X %02X %02X %02X "
65 "%02X %02X %02X %02X\n"
66 "Modulus length: %d\n"
67 "Modulus:\n",
68 data[0], data[1], data[2], data[3],
69 data[4], data[5],
70 data[6], data[7],
71 data[12], data[13], data[14], data[15],
72 data[16], data[17], data[18], data[19],
73 data[20], data[21], data[22], data[23],
74 be32_to_cpu(*((__be32 *) (data + 24))));
75
76 for (i = 0; i < 256; i++) {
77 str += sprintf(str, "%02X ", data[i + 28]);
78 if ((i + 1) % 16 == 0)
79 str += sprintf(str, "\n");
80 }
81out:
82 rc = str - buf;
83 return rc;
84}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -070085static DEVICE_ATTR_RO(pubek);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070086
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -070087static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr,
88 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070089{
90 cap_t cap;
91 u8 digest[TPM_DIGEST_SIZE];
92 ssize_t rc;
93 int i, j, num_pcrs;
94 char *str = buf;
Jason Gunthorpe062807f2016-04-18 13:26:13 -040095 struct tpm_chip *chip = to_tpm_chip(dev);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070096
Jason Gunthorpe062807f2016-04-18 13:26:13 -040097 rc = tpm_getcap(chip, TPM_CAP_PROP_PCR, &cap,
Jason Gunthorpe000a07b2013-11-26 13:30:41 -070098 "attempting to determine the number of PCRS");
99 if (rc)
100 return 0;
101
102 num_pcrs = be32_to_cpu(cap.num_pcrs);
103 for (i = 0; i < num_pcrs; i++) {
104 rc = tpm_pcr_read_dev(chip, i, digest);
105 if (rc)
106 break;
107 str += sprintf(str, "PCR-%02d: ", i);
108 for (j = 0; j < TPM_DIGEST_SIZE; j++)
109 str += sprintf(str, "%02X ", digest[j]);
110 str += sprintf(str, "\n");
111 }
112 return str - buf;
113}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700114static DEVICE_ATTR_RO(pcrs);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700115
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700116static ssize_t enabled_show(struct device *dev, struct device_attribute *attr,
117 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700118{
119 cap_t cap;
120 ssize_t rc;
121
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400122 rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap,
123 "attempting to determine the permanent enabled state");
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700124 if (rc)
125 return 0;
126
127 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
128 return rc;
129}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700130static DEVICE_ATTR_RO(enabled);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700131
Fengguang Wu5f648222013-12-07 16:10:26 +0100132static ssize_t active_show(struct device *dev, struct device_attribute *attr,
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700133 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700134{
135 cap_t cap;
136 ssize_t rc;
137
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400138 rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap,
139 "attempting to determine the permanent active state");
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700140 if (rc)
141 return 0;
142
143 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
144 return rc;
145}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700146static DEVICE_ATTR_RO(active);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700147
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700148static ssize_t owned_show(struct device *dev, struct device_attribute *attr,
149 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700150{
151 cap_t cap;
152 ssize_t rc;
153
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400154 rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap,
155 "attempting to determine the owner state");
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700156 if (rc)
157 return 0;
158
159 rc = sprintf(buf, "%d\n", cap.owned);
160 return rc;
161}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700162static DEVICE_ATTR_RO(owned);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700163
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700164static ssize_t temp_deactivated_show(struct device *dev,
165 struct device_attribute *attr, char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700166{
167 cap_t cap;
168 ssize_t rc;
169
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400170 rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap,
171 "attempting to determine the temporary state");
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700172 if (rc)
173 return 0;
174
175 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
176 return rc;
177}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700178static DEVICE_ATTR_RO(temp_deactivated);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700179
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700180static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
181 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700182{
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400183 struct tpm_chip *chip = to_tpm_chip(dev);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700184 cap_t cap;
185 ssize_t rc;
186 char *str = buf;
187
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400188 rc = tpm_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700189 "attempting to determine the manufacturer");
190 if (rc)
191 return 0;
192 str += sprintf(str, "Manufacturer: 0x%x\n",
193 be32_to_cpu(cap.manufacturer_id));
194
195 /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400196 rc = tpm_getcap(chip, CAP_VERSION_1_2, &cap,
197 "attempting to determine the 1.2 version");
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700198 if (!rc) {
199 str += sprintf(str,
200 "TCG version: %d.%d\nFirmware version: %d.%d\n",
201 cap.tpm_version_1_2.Major,
202 cap.tpm_version_1_2.Minor,
203 cap.tpm_version_1_2.revMajor,
204 cap.tpm_version_1_2.revMinor);
205 } else {
206 /* Otherwise just use TPM_STRUCT_VER */
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400207 rc = tpm_getcap(chip, CAP_VERSION_1_1, &cap,
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700208 "attempting to determine the 1.1 version");
209 if (rc)
210 return 0;
211 str += sprintf(str,
212 "TCG version: %d.%d\nFirmware version: %d.%d\n",
213 cap.tpm_version.Major,
214 cap.tpm_version.Minor,
215 cap.tpm_version.revMajor,
216 cap.tpm_version.revMinor);
217 }
218
219 return str - buf;
220}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700221static DEVICE_ATTR_RO(caps);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700222
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700223static ssize_t cancel_store(struct device *dev, struct device_attribute *attr,
224 const char *buf, size_t count)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700225{
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400226 struct tpm_chip *chip = to_tpm_chip(dev);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700227 if (chip == NULL)
228 return 0;
229
Jason Gunthorpe5f82e9f2013-11-26 13:30:44 -0700230 chip->ops->cancel(chip);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700231 return count;
232}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700233static DEVICE_ATTR_WO(cancel);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700234
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700235static ssize_t durations_show(struct device *dev, struct device_attribute *attr,
236 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700237{
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400238 struct tpm_chip *chip = to_tpm_chip(dev);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700239
Christophe Ricardaf782f32016-03-31 22:56:59 +0200240 if (chip->duration[TPM_LONG] == 0)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700241 return 0;
242
243 return sprintf(buf, "%d %d %d [%s]\n",
Christophe Ricardaf782f32016-03-31 22:56:59 +0200244 jiffies_to_usecs(chip->duration[TPM_SHORT]),
245 jiffies_to_usecs(chip->duration[TPM_MEDIUM]),
246 jiffies_to_usecs(chip->duration[TPM_LONG]),
247 chip->duration_adjusted
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700248 ? "adjusted" : "original");
249}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700250static DEVICE_ATTR_RO(durations);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700251
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700252static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr,
253 char *buf)
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700254{
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400255 struct tpm_chip *chip = to_tpm_chip(dev);
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700256
257 return sprintf(buf, "%d %d %d %d [%s]\n",
Christophe Ricardaf782f32016-03-31 22:56:59 +0200258 jiffies_to_usecs(chip->timeout_a),
259 jiffies_to_usecs(chip->timeout_b),
260 jiffies_to_usecs(chip->timeout_c),
261 jiffies_to_usecs(chip->timeout_d),
262 chip->timeout_adjusted
Jason Gunthorpe000a07b2013-11-26 13:30:41 -0700263 ? "adjusted" : "original");
264}
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700265static DEVICE_ATTR_RO(timeouts);
266
267static struct attribute *tpm_dev_attrs[] = {
268 &dev_attr_pubek.attr,
269 &dev_attr_pcrs.attr,
270 &dev_attr_enabled.attr,
271 &dev_attr_active.attr,
272 &dev_attr_owned.attr,
273 &dev_attr_temp_deactivated.attr,
274 &dev_attr_caps.attr,
275 &dev_attr_cancel.attr,
276 &dev_attr_durations.attr,
277 &dev_attr_timeouts.attr,
278 NULL,
279};
280
281static const struct attribute_group tpm_dev_group = {
282 .attrs = tpm_dev_attrs,
283};
284
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400285void tpm_sysfs_add_device(struct tpm_chip *chip)
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700286{
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400287 /* The sysfs routines rely on an implicit tpm_try_get_ops, device_del
288 * is called before ops is null'd and the sysfs core synchronizes this
289 * removal so that no callbacks are running or can run again
Jason Gunthorpe4e261952016-02-12 20:29:53 -0700290 */
Jason Gunthorpe062807f2016-04-18 13:26:13 -0400291 WARN_ON(chip->groups_cnt != 0);
292 chip->groups[chip->groups_cnt++] = &tpm_dev_group;
Jason Gunthorpe1e3b73a2013-11-26 13:30:42 -0700293}