1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004 IBM Corporation
5 * Leendert van Doorn <leendert@watson.ibm.com>
6 * Dave Safford <safford@watson.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
10 * Copyright (C) 2013 Obsidian Research Corp
11 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
13 * sysfs filesystem inspection interface to the TPM
15 #include <linux/device.h>
18 struct tpm_readpubek_out
{
29 #define READ_PUBEK_RESULT_MIN_BODY_SIZE (28 + 256)
30 #define TPM_ORD_READPUBEK 124
32 static ssize_t
pubek_show(struct device
*dev
, struct device_attribute
*attr
,
35 struct tpm_buf tpm_buf
;
36 struct tpm_readpubek_out
*out
;
39 struct tpm_chip
*chip
= to_tpm_chip(dev
);
42 memset(&anti_replay
, 0, sizeof(anti_replay
));
44 if (tpm_try_get_ops(chip
))
47 if (tpm_buf_init(&tpm_buf
, TPM_TAG_RQU_COMMAND
, TPM_ORD_READPUBEK
))
50 tpm_buf_append(&tpm_buf
, anti_replay
, sizeof(anti_replay
));
52 if (tpm_transmit_cmd(chip
, &tpm_buf
, READ_PUBEK_RESULT_MIN_BODY_SIZE
,
53 "attempting to read the PUBEK"))
56 out
= (struct tpm_readpubek_out
*)&tpm_buf
.data
[10];
59 "Algorithm: %02X %02X %02X %02X\n"
60 "Encscheme: %02X %02X\n"
61 "Sigscheme: %02X %02X\n"
62 "Parameters: %02X %02X %02X %02X "
63 "%02X %02X %02X %02X "
64 "%02X %02X %02X %02X\n"
65 "Modulus length: %d\n"
67 out
->algorithm
[0], out
->algorithm
[1], out
->algorithm
[2],
69 out
->encscheme
[0], out
->encscheme
[1],
70 out
->sigscheme
[0], out
->sigscheme
[1],
71 out
->parameters
[0], out
->parameters
[1],
72 out
->parameters
[2], out
->parameters
[3],
73 out
->parameters
[4], out
->parameters
[5],
74 out
->parameters
[6], out
->parameters
[7],
75 out
->parameters
[8], out
->parameters
[9],
76 out
->parameters
[10], out
->parameters
[11],
77 be32_to_cpu(out
->keysize
));
79 for (i
= 0; i
< 256; i
++) {
80 str
+= sprintf(str
, "%02X ", out
->modulus
[i
]);
81 if ((i
+ 1) % 16 == 0)
82 str
+= sprintf(str
, "\n");
86 tpm_buf_destroy(&tpm_buf
);
91 static DEVICE_ATTR_RO(pubek
);
93 static ssize_t
pcrs_show(struct device
*dev
, struct device_attribute
*attr
,
97 u8 digest
[TPM_DIGEST_SIZE
];
100 struct tpm_chip
*chip
= to_tpm_chip(dev
);
102 if (tpm_try_get_ops(chip
))
105 if (tpm1_getcap(chip
, TPM_CAP_PROP_PCR
, &cap
,
106 "attempting to determine the number of PCRS",
107 sizeof(cap
.num_pcrs
))) {
112 num_pcrs
= be32_to_cpu(cap
.num_pcrs
);
113 for (i
= 0; i
< num_pcrs
; i
++) {
114 if (tpm1_pcr_read(chip
, i
, digest
)) {
118 str
+= sprintf(str
, "PCR-%02d: ", i
);
119 for (j
= 0; j
< TPM_DIGEST_SIZE
; j
++)
120 str
+= sprintf(str
, "%02X ", digest
[j
]);
121 str
+= sprintf(str
, "\n");
126 static DEVICE_ATTR_RO(pcrs
);
128 static ssize_t
enabled_show(struct device
*dev
, struct device_attribute
*attr
,
131 struct tpm_chip
*chip
= to_tpm_chip(dev
);
135 if (tpm_try_get_ops(chip
))
138 if (tpm1_getcap(chip
, TPM_CAP_FLAG_PERM
, &cap
,
139 "attempting to determine the permanent enabled state",
140 sizeof(cap
.perm_flags
)))
143 rc
= sprintf(buf
, "%d\n", !cap
.perm_flags
.disable
);
148 static DEVICE_ATTR_RO(enabled
);
150 static ssize_t
active_show(struct device
*dev
, struct device_attribute
*attr
,
153 struct tpm_chip
*chip
= to_tpm_chip(dev
);
157 if (tpm_try_get_ops(chip
))
160 if (tpm1_getcap(chip
, TPM_CAP_FLAG_PERM
, &cap
,
161 "attempting to determine the permanent active state",
162 sizeof(cap
.perm_flags
)))
165 rc
= sprintf(buf
, "%d\n", !cap
.perm_flags
.deactivated
);
170 static DEVICE_ATTR_RO(active
);
172 static ssize_t
owned_show(struct device
*dev
, struct device_attribute
*attr
,
175 struct tpm_chip
*chip
= to_tpm_chip(dev
);
179 if (tpm_try_get_ops(chip
))
182 if (tpm1_getcap(to_tpm_chip(dev
), TPM_CAP_PROP_OWNER
, &cap
,
183 "attempting to determine the owner state",
187 rc
= sprintf(buf
, "%d\n", cap
.owned
);
192 static DEVICE_ATTR_RO(owned
);
194 static ssize_t
temp_deactivated_show(struct device
*dev
,
195 struct device_attribute
*attr
, char *buf
)
197 struct tpm_chip
*chip
= to_tpm_chip(dev
);
201 if (tpm_try_get_ops(chip
))
204 if (tpm1_getcap(to_tpm_chip(dev
), TPM_CAP_FLAG_VOL
, &cap
,
205 "attempting to determine the temporary state",
206 sizeof(cap
.stclear_flags
)))
209 rc
= sprintf(buf
, "%d\n", cap
.stclear_flags
.deactivated
);
214 static DEVICE_ATTR_RO(temp_deactivated
);
216 static ssize_t
caps_show(struct device
*dev
, struct device_attribute
*attr
,
219 struct tpm_chip
*chip
= to_tpm_chip(dev
);
220 struct tpm1_version
*version
;
225 if (tpm_try_get_ops(chip
))
228 if (tpm1_getcap(chip
, TPM_CAP_PROP_MANUFACTURER
, &cap
,
229 "attempting to determine the manufacturer",
230 sizeof(cap
.manufacturer_id
)))
233 str
+= sprintf(str
, "Manufacturer: 0x%x\n",
234 be32_to_cpu(cap
.manufacturer_id
));
237 if (!tpm1_getcap(chip
, TPM_CAP_VERSION_1_2
, &cap
,
238 "attempting to determine the 1.2 version",
239 sizeof(cap
.version2
))) {
240 version
= &cap
.version2
.version
;
245 if (tpm1_getcap(chip
, TPM_CAP_VERSION_1_1
, &cap
,
246 "attempting to determine the 1.1 version",
247 sizeof(cap
.version1
))) {
251 version
= &cap
.version1
;
255 "TCG version: %d.%d\nFirmware version: %d.%d\n",
256 version
->major
, version
->minor
,
257 version
->rev_major
, version
->rev_minor
);
265 static DEVICE_ATTR_RO(caps
);
267 static ssize_t
cancel_store(struct device
*dev
, struct device_attribute
*attr
,
268 const char *buf
, size_t count
)
270 struct tpm_chip
*chip
= to_tpm_chip(dev
);
272 if (tpm_try_get_ops(chip
))
275 chip
->ops
->cancel(chip
);
279 static DEVICE_ATTR_WO(cancel
);
281 static ssize_t
durations_show(struct device
*dev
, struct device_attribute
*attr
,
284 struct tpm_chip
*chip
= to_tpm_chip(dev
);
286 if (chip
->duration
[TPM_LONG
] == 0)
289 return sprintf(buf
, "%d %d %d [%s]\n",
290 jiffies_to_usecs(chip
->duration
[TPM_SHORT
]),
291 jiffies_to_usecs(chip
->duration
[TPM_MEDIUM
]),
292 jiffies_to_usecs(chip
->duration
[TPM_LONG
]),
293 chip
->duration_adjusted
294 ? "adjusted" : "original");
296 static DEVICE_ATTR_RO(durations
);
298 static ssize_t
timeouts_show(struct device
*dev
, struct device_attribute
*attr
,
301 struct tpm_chip
*chip
= to_tpm_chip(dev
);
303 return sprintf(buf
, "%d %d %d %d [%s]\n",
304 jiffies_to_usecs(chip
->timeout_a
),
305 jiffies_to_usecs(chip
->timeout_b
),
306 jiffies_to_usecs(chip
->timeout_c
),
307 jiffies_to_usecs(chip
->timeout_d
),
308 chip
->timeout_adjusted
309 ? "adjusted" : "original");
311 static DEVICE_ATTR_RO(timeouts
);
313 static ssize_t
tpm_version_major_show(struct device
*dev
,
314 struct device_attribute
*attr
, char *buf
)
316 struct tpm_chip
*chip
= to_tpm_chip(dev
);
318 return sprintf(buf
, "%s\n", chip
->flags
& TPM_CHIP_FLAG_TPM2
321 static DEVICE_ATTR_RO(tpm_version_major
);
323 static struct attribute
*tpm1_dev_attrs
[] = {
324 &dev_attr_pubek
.attr
,
326 &dev_attr_enabled
.attr
,
327 &dev_attr_active
.attr
,
328 &dev_attr_owned
.attr
,
329 &dev_attr_temp_deactivated
.attr
,
331 &dev_attr_cancel
.attr
,
332 &dev_attr_durations
.attr
,
333 &dev_attr_timeouts
.attr
,
334 &dev_attr_tpm_version_major
.attr
,
338 static struct attribute
*tpm2_dev_attrs
[] = {
339 &dev_attr_tpm_version_major
.attr
,
343 static const struct attribute_group tpm1_dev_group
= {
344 .attrs
= tpm1_dev_attrs
,
347 static const struct attribute_group tpm2_dev_group
= {
348 .attrs
= tpm2_dev_attrs
,
351 void tpm_sysfs_add_device(struct tpm_chip
*chip
)
353 WARN_ON(chip
->groups_cnt
!= 0);
354 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
355 chip
->groups
[chip
->groups_cnt
++] = &tpm2_dev_group
;
357 chip
->groups
[chip
->groups_cnt
++] = &tpm1_dev_group
;