2 * Copyright (C) 2004 IBM Corporation
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>
9 * Copyright (C) 2013 Obsidian Research Corp
10 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
12 * sysfs filesystem inspection interface to the TPM
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
20 #include <linux/device.h>
23 /* XXX for now this helper is duplicated in tpm-interface.c */
24 static ssize_t
transmit_cmd(struct tpm_chip
*chip
, struct tpm_cmd_t
*cmd
,
25 int len
, const char *desc
)
29 len
= tpm_transmit(chip
, (u8
*) cmd
, len
);
32 else if (len
< TPM_HEADER_SIZE
)
35 err
= be32_to_cpu(cmd
->header
.out
.return_code
);
37 dev_err(chip
->dev
, "A TPM error (%d) occurred %s\n", err
, desc
);
42 #define READ_PUBEK_RESULT_SIZE 314
43 #define TPM_ORD_READPUBEK cpu_to_be32(124)
44 static struct tpm_input_header tpm_readpubek_header
= {
45 .tag
= TPM_TAG_RQU_COMMAND
,
46 .length
= cpu_to_be32(30),
47 .ordinal
= TPM_ORD_READPUBEK
49 static ssize_t
pubek_show(struct device
*dev
, struct device_attribute
*attr
,
53 struct tpm_cmd_t tpm_cmd
;
58 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
60 tpm_cmd
.header
.in
= tpm_readpubek_header
;
61 err
= transmit_cmd(chip
, &tpm_cmd
, READ_PUBEK_RESULT_SIZE
,
62 "attempting to read the PUBEK");
67 ignore header 10 bytes
68 algorithm 32 bits (1 == RSA )
71 parameters (RSA 12->bytes: keybit, #primes, expbit)
74 ignore checksum 20 bytes
76 data
= tpm_cmd
.params
.readpubek_out_buffer
;
79 "Algorithm: %02X %02X %02X %02X\n"
80 "Encscheme: %02X %02X\n"
81 "Sigscheme: %02X %02X\n"
82 "Parameters: %02X %02X %02X %02X "
83 "%02X %02X %02X %02X "
84 "%02X %02X %02X %02X\n"
85 "Modulus length: %d\n"
87 data
[0], data
[1], data
[2], data
[3],
90 data
[12], data
[13], data
[14], data
[15],
91 data
[16], data
[17], data
[18], data
[19],
92 data
[20], data
[21], data
[22], data
[23],
93 be32_to_cpu(*((__be32
*) (data
+ 24))));
95 for (i
= 0; i
< 256; i
++) {
96 str
+= sprintf(str
, "%02X ", data
[i
+ 28]);
97 if ((i
+ 1) % 16 == 0)
98 str
+= sprintf(str
, "\n");
104 static DEVICE_ATTR_RO(pubek
);
106 static ssize_t
pcrs_show(struct device
*dev
, struct device_attribute
*attr
,
110 u8 digest
[TPM_DIGEST_SIZE
];
114 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
116 rc
= tpm_getcap(dev
, TPM_CAP_PROP_PCR
, &cap
,
117 "attempting to determine the number of PCRS");
121 num_pcrs
= be32_to_cpu(cap
.num_pcrs
);
122 for (i
= 0; i
< num_pcrs
; i
++) {
123 rc
= tpm_pcr_read_dev(chip
, i
, digest
);
126 str
+= sprintf(str
, "PCR-%02d: ", i
);
127 for (j
= 0; j
< TPM_DIGEST_SIZE
; j
++)
128 str
+= sprintf(str
, "%02X ", digest
[j
]);
129 str
+= sprintf(str
, "\n");
133 static DEVICE_ATTR_RO(pcrs
);
135 static ssize_t
enabled_show(struct device
*dev
, struct device_attribute
*attr
,
141 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_PERM
, &cap
,
142 "attempting to determine the permanent enabled state");
146 rc
= sprintf(buf
, "%d\n", !cap
.perm_flags
.disable
);
149 static DEVICE_ATTR_RO(enabled
);
151 static ssize_t
active_show(struct device
*dev
, struct device_attribute
*attr
,
157 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_PERM
, &cap
,
158 "attempting to determine the permanent active state");
162 rc
= sprintf(buf
, "%d\n", !cap
.perm_flags
.deactivated
);
165 static DEVICE_ATTR_RO(active
);
167 static ssize_t
owned_show(struct device
*dev
, struct device_attribute
*attr
,
173 rc
= tpm_getcap(dev
, TPM_CAP_PROP_OWNER
, &cap
,
174 "attempting to determine the owner state");
178 rc
= sprintf(buf
, "%d\n", cap
.owned
);
181 static DEVICE_ATTR_RO(owned
);
183 static ssize_t
temp_deactivated_show(struct device
*dev
,
184 struct device_attribute
*attr
, char *buf
)
189 rc
= tpm_getcap(dev
, TPM_CAP_FLAG_VOL
, &cap
,
190 "attempting to determine the temporary state");
194 rc
= sprintf(buf
, "%d\n", cap
.stclear_flags
.deactivated
);
197 static DEVICE_ATTR_RO(temp_deactivated
);
199 static ssize_t
caps_show(struct device
*dev
, struct device_attribute
*attr
,
206 rc
= tpm_getcap(dev
, TPM_CAP_PROP_MANUFACTURER
, &cap
,
207 "attempting to determine the manufacturer");
210 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
211 be32_to_cpu(cap
.manufacturer_id
));
213 /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */
214 rc
= tpm_getcap(dev
, CAP_VERSION_1_2
, &cap
,
215 "attempting to determine the 1.2 version");
218 "TCG version: %d.%d\nFirmware version: %d.%d\n",
219 cap
.tpm_version_1_2
.Major
,
220 cap
.tpm_version_1_2
.Minor
,
221 cap
.tpm_version_1_2
.revMajor
,
222 cap
.tpm_version_1_2
.revMinor
);
224 /* Otherwise just use TPM_STRUCT_VER */
225 rc
= tpm_getcap(dev
, CAP_VERSION_1_1
, &cap
,
226 "attempting to determine the 1.1 version");
230 "TCG version: %d.%d\nFirmware version: %d.%d\n",
231 cap
.tpm_version
.Major
,
232 cap
.tpm_version
.Minor
,
233 cap
.tpm_version
.revMajor
,
234 cap
.tpm_version
.revMinor
);
239 static DEVICE_ATTR_RO(caps
);
241 static ssize_t
cancel_store(struct device
*dev
, struct device_attribute
*attr
,
242 const char *buf
, size_t count
)
244 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
248 chip
->ops
->cancel(chip
);
251 static DEVICE_ATTR_WO(cancel
);
253 static ssize_t
durations_show(struct device
*dev
, struct device_attribute
*attr
,
256 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
258 if (chip
->vendor
.duration
[TPM_LONG
] == 0)
261 return sprintf(buf
, "%d %d %d [%s]\n",
262 jiffies_to_usecs(chip
->vendor
.duration
[TPM_SHORT
]),
263 jiffies_to_usecs(chip
->vendor
.duration
[TPM_MEDIUM
]),
264 jiffies_to_usecs(chip
->vendor
.duration
[TPM_LONG
]),
265 chip
->vendor
.duration_adjusted
266 ? "adjusted" : "original");
268 static DEVICE_ATTR_RO(durations
);
270 static ssize_t
timeouts_show(struct device
*dev
, struct device_attribute
*attr
,
273 struct tpm_chip
*chip
= dev_get_drvdata(dev
);
275 return sprintf(buf
, "%d %d %d %d [%s]\n",
276 jiffies_to_usecs(chip
->vendor
.timeout_a
),
277 jiffies_to_usecs(chip
->vendor
.timeout_b
),
278 jiffies_to_usecs(chip
->vendor
.timeout_c
),
279 jiffies_to_usecs(chip
->vendor
.timeout_d
),
280 chip
->vendor
.timeout_adjusted
281 ? "adjusted" : "original");
283 static DEVICE_ATTR_RO(timeouts
);
285 static struct attribute
*tpm_dev_attrs
[] = {
286 &dev_attr_pubek
.attr
,
288 &dev_attr_enabled
.attr
,
289 &dev_attr_active
.attr
,
290 &dev_attr_owned
.attr
,
291 &dev_attr_temp_deactivated
.attr
,
293 &dev_attr_cancel
.attr
,
294 &dev_attr_durations
.attr
,
295 &dev_attr_timeouts
.attr
,
299 static const struct attribute_group tpm_dev_group
= {
300 .attrs
= tpm_dev_attrs
,
303 int tpm_sysfs_add_device(struct tpm_chip
*chip
)
306 err
= sysfs_create_group(&chip
->dev
->kobj
,
311 "failed to create sysfs attributes, %d\n", err
);
315 void tpm_sysfs_del_device(struct tpm_chip
*chip
)
317 sysfs_remove_group(&chip
->dev
->kobj
, &tpm_dev_group
);