GNU Linux-libre 4.9.318-gnu1
[releases.git] / drivers / char / tpm / tpm-sysfs.c
1 /*
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  *
9  * Copyright (C) 2013 Obsidian Research Corp
10  * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
11  *
12  * 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
23 #define READ_PUBEK_RESULT_SIZE 314
24 #define TPM_ORD_READPUBEK cpu_to_be32(124)
25 static const 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 };
30 static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
31                           char *buf)
32 {
33         u8 *data;
34         struct tpm_cmd_t tpm_cmd;
35         ssize_t err;
36         int i, rc;
37         char *str = buf;
38         struct tpm_chip *chip = to_tpm_chip(dev);
39
40         memset(&tpm_cmd, 0, sizeof(tpm_cmd));
41
42         tpm_cmd.header.in = tpm_readpubek_header;
43         err = tpm_transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE, 0,
44                                "attempting to read the PUBEK");
45         if (err)
46                 goto out;
47
48         /*
49            ignore header 10 bytes
50            algorithm 32 bits (1 == RSA )
51            encscheme 16 bits
52            sigscheme 16 bits
53            parameters (RSA 12->bytes: keybit, #primes, expbit)
54            keylenbytes 32 bits
55            256 byte modulus
56            ignore checksum 20 bytes
57          */
58         data = tpm_cmd.params.readpubek_out_buffer;
59         str +=
60             sprintf(str,
61                     "Algorithm: %02X %02X %02X %02X\n"
62                     "Encscheme: %02X %02X\n"
63                     "Sigscheme: %02X %02X\n"
64                     "Parameters: %02X %02X %02X %02X "
65                     "%02X %02X %02X %02X "
66                     "%02X %02X %02X %02X\n"
67                     "Modulus length: %d\n"
68                     "Modulus:\n",
69                     data[0], data[1], data[2], data[3],
70                     data[4], data[5],
71                     data[6], data[7],
72                     data[12], data[13], data[14], data[15],
73                     data[16], data[17], data[18], data[19],
74                     data[20], data[21], data[22], data[23],
75                     be32_to_cpu(*((__be32 *) (data + 24))));
76
77         for (i = 0; i < 256; i++) {
78                 str += sprintf(str, "%02X ", data[i + 28]);
79                 if ((i + 1) % 16 == 0)
80                         str += sprintf(str, "\n");
81         }
82 out:
83         rc = str - buf;
84         return rc;
85 }
86 static DEVICE_ATTR_RO(pubek);
87
88 static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr,
89                          char *buf)
90 {
91         cap_t cap;
92         u8 digest[TPM_DIGEST_SIZE];
93         ssize_t rc;
94         int i, j, num_pcrs;
95         char *str = buf;
96         struct tpm_chip *chip = to_tpm_chip(dev);
97
98         rc = tpm_getcap(chip, TPM_CAP_PROP_PCR, &cap,
99                         "attempting to determine the number of PCRS");
100         if (rc)
101                 return 0;
102
103         num_pcrs = be32_to_cpu(cap.num_pcrs);
104         for (i = 0; i < num_pcrs; i++) {
105                 rc = tpm_pcr_read_dev(chip, i, digest);
106                 if (rc)
107                         break;
108                 str += sprintf(str, "PCR-%02d: ", i);
109                 for (j = 0; j < TPM_DIGEST_SIZE; j++)
110                         str += sprintf(str, "%02X ", digest[j]);
111                 str += sprintf(str, "\n");
112         }
113         return str - buf;
114 }
115 static DEVICE_ATTR_RO(pcrs);
116
117 static ssize_t enabled_show(struct device *dev, struct device_attribute *attr,
118                      char *buf)
119 {
120         cap_t cap;
121         ssize_t rc;
122
123         rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap,
124                         "attempting to determine the permanent enabled state");
125         if (rc)
126                 return 0;
127
128         rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
129         return rc;
130 }
131 static DEVICE_ATTR_RO(enabled);
132
133 static ssize_t active_show(struct device *dev, struct device_attribute *attr,
134                     char *buf)
135 {
136         cap_t cap;
137         ssize_t rc;
138
139         rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_PERM, &cap,
140                         "attempting to determine the permanent active state");
141         if (rc)
142                 return 0;
143
144         rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
145         return rc;
146 }
147 static DEVICE_ATTR_RO(active);
148
149 static ssize_t owned_show(struct device *dev, struct device_attribute *attr,
150                           char *buf)
151 {
152         cap_t cap;
153         ssize_t rc;
154
155         rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap,
156                         "attempting to determine the owner state");
157         if (rc)
158                 return 0;
159
160         rc = sprintf(buf, "%d\n", cap.owned);
161         return rc;
162 }
163 static DEVICE_ATTR_RO(owned);
164
165 static ssize_t temp_deactivated_show(struct device *dev,
166                                      struct device_attribute *attr, char *buf)
167 {
168         cap_t cap;
169         ssize_t rc;
170
171         rc = tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap,
172                         "attempting to determine the temporary state");
173         if (rc)
174                 return 0;
175
176         rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
177         return rc;
178 }
179 static DEVICE_ATTR_RO(temp_deactivated);
180
181 static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
182                          char *buf)
183 {
184         struct tpm_chip *chip = to_tpm_chip(dev);
185         cap_t cap;
186         ssize_t rc;
187         char *str = buf;
188
189         rc = tpm_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
190                         "attempting to determine the manufacturer");
191         if (rc)
192                 return 0;
193         str += sprintf(str, "Manufacturer: 0x%x\n",
194                        be32_to_cpu(cap.manufacturer_id));
195
196         /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */
197         rc = tpm_getcap(chip, CAP_VERSION_1_2, &cap,
198                         "attempting to determine the 1.2 version");
199         if (!rc) {
200                 str += sprintf(str,
201                                "TCG version: %d.%d\nFirmware version: %d.%d\n",
202                                cap.tpm_version_1_2.Major,
203                                cap.tpm_version_1_2.Minor,
204                                cap.tpm_version_1_2.revMajor,
205                                cap.tpm_version_1_2.revMinor);
206         } else {
207                 /* Otherwise just use TPM_STRUCT_VER */
208                 rc = tpm_getcap(chip, CAP_VERSION_1_1, &cap,
209                                 "attempting to determine the 1.1 version");
210                 if (rc)
211                         return 0;
212                 str += sprintf(str,
213                                "TCG version: %d.%d\nFirmware version: %d.%d\n",
214                                cap.tpm_version.Major,
215                                cap.tpm_version.Minor,
216                                cap.tpm_version.revMajor,
217                                cap.tpm_version.revMinor);
218         }
219
220         return str - buf;
221 }
222 static DEVICE_ATTR_RO(caps);
223
224 static ssize_t cancel_store(struct device *dev, struct device_attribute *attr,
225                             const char *buf, size_t count)
226 {
227         struct tpm_chip *chip = to_tpm_chip(dev);
228         if (chip == NULL)
229                 return 0;
230
231         chip->ops->cancel(chip);
232         return count;
233 }
234 static DEVICE_ATTR_WO(cancel);
235
236 static ssize_t durations_show(struct device *dev, struct device_attribute *attr,
237                               char *buf)
238 {
239         struct tpm_chip *chip = to_tpm_chip(dev);
240
241         if (chip->duration[TPM_LONG] == 0)
242                 return 0;
243
244         return sprintf(buf, "%d %d %d [%s]\n",
245                        jiffies_to_usecs(chip->duration[TPM_SHORT]),
246                        jiffies_to_usecs(chip->duration[TPM_MEDIUM]),
247                        jiffies_to_usecs(chip->duration[TPM_LONG]),
248                        chip->duration_adjusted
249                        ? "adjusted" : "original");
250 }
251 static DEVICE_ATTR_RO(durations);
252
253 static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr,
254                              char *buf)
255 {
256         struct tpm_chip *chip = to_tpm_chip(dev);
257
258         return sprintf(buf, "%d %d %d %d [%s]\n",
259                        jiffies_to_usecs(chip->timeout_a),
260                        jiffies_to_usecs(chip->timeout_b),
261                        jiffies_to_usecs(chip->timeout_c),
262                        jiffies_to_usecs(chip->timeout_d),
263                        chip->timeout_adjusted
264                        ? "adjusted" : "original");
265 }
266 static DEVICE_ATTR_RO(timeouts);
267
268 static struct attribute *tpm_dev_attrs[] = {
269         &dev_attr_pubek.attr,
270         &dev_attr_pcrs.attr,
271         &dev_attr_enabled.attr,
272         &dev_attr_active.attr,
273         &dev_attr_owned.attr,
274         &dev_attr_temp_deactivated.attr,
275         &dev_attr_caps.attr,
276         &dev_attr_cancel.attr,
277         &dev_attr_durations.attr,
278         &dev_attr_timeouts.attr,
279         NULL,
280 };
281
282 static const struct attribute_group tpm_dev_group = {
283         .attrs = tpm_dev_attrs,
284 };
285
286 void tpm_sysfs_add_device(struct tpm_chip *chip)
287 {
288         /* XXX: If you wish to remove this restriction, you must first update
289          * tpm_sysfs to explicitly lock chip->ops.
290          */
291         if (chip->flags & TPM_CHIP_FLAG_TPM2)
292                 return;
293         /* The sysfs routines rely on an implicit tpm_try_get_ops, device_del
294          * is called before ops is null'd and the sysfs core synchronizes this
295          * removal so that no callbacks are running or can run again
296          */
297         WARN_ON(chip->groups_cnt != 0);
298         chip->groups[chip->groups_cnt++] = &tpm_dev_group;
299 }