1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004 IBM Corporation
10 * Copyright (C) 2013 Obsidian Research Corp
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];
63 "Modulus length: %d\n"
69 be32_to_cpu(out->keysize));
71 for (i = 0; i < 256; i += 16)
72 str += sprintf(str, "%16ph\n", &out->modulus[i]);
75 tpm_buf_destroy(&tpm_buf);
80 static DEVICE_ATTR_RO(pubek);
82 static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr,
86 u8 digest[TPM_DIGEST_SIZE];
89 struct tpm_chip *chip = to_tpm_chip(dev);
91 if (tpm_try_get_ops(chip))
94 if (tpm1_getcap(chip, TPM_CAP_PROP_PCR, &cap,
95 "attempting to determine the number of PCRS",
96 sizeof(cap.num_pcrs))) {
101 num_pcrs = be32_to_cpu(cap.num_pcrs);
102 for (i = 0; i < num_pcrs; i++) {
103 if (tpm1_pcr_read(chip, i, digest)) {
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");
115 static DEVICE_ATTR_RO(pcrs);
117 static ssize_t enabled_show(struct device *dev, struct device_attribute *attr,
120 struct tpm_chip *chip = to_tpm_chip(dev);
124 if (tpm_try_get_ops(chip))
127 if (tpm1_getcap(chip, TPM_CAP_FLAG_PERM, &cap,
128 "attempting to determine the permanent enabled state",
129 sizeof(cap.perm_flags)))
132 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
137 static DEVICE_ATTR_RO(enabled);
139 static ssize_t active_show(struct device *dev, struct device_attribute *attr,
142 struct tpm_chip *chip = to_tpm_chip(dev);
146 if (tpm_try_get_ops(chip))
149 if (tpm1_getcap(chip, TPM_CAP_FLAG_PERM, &cap,
150 "attempting to determine the permanent active state",
151 sizeof(cap.perm_flags)))
154 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
159 static DEVICE_ATTR_RO(active);
161 static ssize_t owned_show(struct device *dev, struct device_attribute *attr,
164 struct tpm_chip *chip = to_tpm_chip(dev);
168 if (tpm_try_get_ops(chip))
171 if (tpm1_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap,
172 "attempting to determine the owner state",
176 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)
186 struct tpm_chip *chip = to_tpm_chip(dev);
190 if (tpm_try_get_ops(chip))
193 if (tpm1_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap,
194 "attempting to determine the temporary state",
195 sizeof(cap.stclear_flags)))
198 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
203 static DEVICE_ATTR_RO(temp_deactivated);
205 static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
208 struct tpm_chip *chip = to_tpm_chip(dev);
209 struct tpm1_version *version;
214 if (tpm_try_get_ops(chip))
217 if (tpm1_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
218 "attempting to determine the manufacturer",
219 sizeof(cap.manufacturer_id)))
222 str += sprintf(str, "Manufacturer: 0x%x\n",
223 be32_to_cpu(cap.manufacturer_id));
226 if (!tpm1_getcap(chip, TPM_CAP_VERSION_1_2, &cap,
227 "attempting to determine the 1.2 version",
228 sizeof(cap.version2))) {
229 version = &cap.version2.version;
234 if (tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
235 "attempting to determine the 1.1 version",
236 sizeof(cap.version1))) {
240 version = &cap.version1;
244 "TCG version: %d.%d\nFirmware version: %d.%d\n",
245 version->major, version->minor,
246 version->rev_major, version->rev_minor);
254 static DEVICE_ATTR_RO(caps);
256 static ssize_t cancel_store(struct device *dev, struct device_attribute *attr,
257 const char *buf, size_t count)
259 struct tpm_chip *chip = to_tpm_chip(dev);
261 if (tpm_try_get_ops(chip))
264 chip->ops->cancel(chip);
268 static DEVICE_ATTR_WO(cancel);
270 static ssize_t durations_show(struct device *dev, struct device_attribute *attr,
273 struct tpm_chip *chip = to_tpm_chip(dev);
275 if (chip->duration[TPM_LONG] == 0)
278 return sprintf(buf, "%d %d %d [%s]\n",
279 jiffies_to_usecs(chip->duration[TPM_SHORT]),
280 jiffies_to_usecs(chip->duration[TPM_MEDIUM]),
281 jiffies_to_usecs(chip->duration[TPM_LONG]),
282 chip->duration_adjusted
283 ? "adjusted" : "original");
285 static DEVICE_ATTR_RO(durations);
287 static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr,
290 struct tpm_chip *chip = to_tpm_chip(dev);
292 return sprintf(buf, "%d %d %d %d [%s]\n",
293 jiffies_to_usecs(chip->timeout_a),
294 jiffies_to_usecs(chip->timeout_b),
295 jiffies_to_usecs(chip->timeout_c),
296 jiffies_to_usecs(chip->timeout_d),
297 chip->timeout_adjusted
298 ? "adjusted" : "original");
300 static DEVICE_ATTR_RO(timeouts);
302 static ssize_t tpm_version_major_show(struct device *dev,
303 struct device_attribute *attr, char *buf)
305 struct tpm_chip *chip = to_tpm_chip(dev);
307 return sprintf(buf, "%s\n", chip->flags & TPM_CHIP_FLAG_TPM2
310 static DEVICE_ATTR_RO(tpm_version_major);
312 #ifdef CONFIG_TCG_TPM2_HMAC
313 static ssize_t null_name_show(struct device *dev, struct device_attribute *attr,
316 struct tpm_chip *chip = to_tpm_chip(dev);
317 int size = TPM2_NAME_SIZE;
319 bin2hex(buf, chip->null_key_name, size);
324 static DEVICE_ATTR_RO(null_name);
327 static struct attribute *tpm1_dev_attrs[] = {
328 &dev_attr_pubek.attr,
330 &dev_attr_enabled.attr,
331 &dev_attr_active.attr,
332 &dev_attr_owned.attr,
333 &dev_attr_temp_deactivated.attr,
335 &dev_attr_cancel.attr,
336 &dev_attr_durations.attr,
337 &dev_attr_timeouts.attr,
338 &dev_attr_tpm_version_major.attr,
342 static struct attribute *tpm2_dev_attrs[] = {
343 &dev_attr_tpm_version_major.attr,
344 #ifdef CONFIG_TCG_TPM2_HMAC
345 &dev_attr_null_name.attr,
350 static const struct attribute_group tpm1_dev_group = {
351 .attrs = tpm1_dev_attrs,
354 static const struct attribute_group tpm2_dev_group = {
355 .attrs = tpm2_dev_attrs,
358 struct tpm_pcr_attr {
361 struct device_attribute attr;
364 #define to_tpm_pcr_attr(a) container_of(a, struct tpm_pcr_attr, attr)
366 static ssize_t pcr_value_show(struct device *dev,
367 struct device_attribute *attr,
370 struct tpm_pcr_attr *ha = to_tpm_pcr_attr(attr);
371 struct tpm_chip *chip = to_tpm_chip(dev);
372 struct tpm_digest digest;
378 for (i = 0; i < chip->nr_allocated_banks; i++)
379 if (ha->alg_id == chip->allocated_banks[i].alg_id)
380 digest_size = chip->allocated_banks[i].digest_size;
381 /* should never happen */
385 digest.alg_id = ha->alg_id;
386 rc = tpm_pcr_read(chip, ha->pcr, &digest);
389 for (i = 0; i < digest_size; i++)
390 str += sprintf(str, "%02X", digest.digest[i]);
391 str += sprintf(str, "\n");
397 * The following set of defines represents all the magic to build
398 * the per hash attribute groups for displaying each bank of PCRs.
399 * The only slight problem with this approach is that every PCR is
400 * hard coded to be present, so you don't know if an PCR is missing
401 * until a cat of the file returns -EINVAL
403 * Also note you must ignore checkpatch warnings in this macro
404 * code. This is deep macro magic that checkpatch.pl doesn't
408 /* Note, this must match TPM2_PLATFORM_PCR which is fixed at 24. */
409 #define _TPM_HELPER(_alg, _hash, F) \
435 /* ignore checkpatch warning about trailing ; in macro. */
436 #define PCR_ATTR(_alg, _hash, _pcr) \
437 static struct tpm_pcr_attr dev_attr_pcr_##_hash##_##_pcr = { \
442 .name = __stringify(_pcr), \
445 .show = pcr_value_show \
449 #define PCR_ATTRS(_alg, _hash) \
450 _TPM_HELPER(_alg, _hash, PCR_ATTR)
452 /* ignore checkpatch warning about trailing , in macro. */
453 #define PCR_ATTR_VAL(_alg, _hash, _pcr) \
454 &dev_attr_pcr_##_hash##_##_pcr.attr.attr,
456 #define PCR_ATTR_GROUP_ARRAY(_alg, _hash) \
457 static struct attribute *pcr_group_attrs_##_hash[] = { \
458 _TPM_HELPER(_alg, _hash, PCR_ATTR_VAL) \
462 #define PCR_ATTR_GROUP(_alg, _hash) \
463 static struct attribute_group pcr_group_##_hash = { \
464 .name = "pcr-" __stringify(_hash), \
465 .attrs = pcr_group_attrs_##_hash \
468 #define PCR_ATTR_BUILD(_alg, _hash) \
469 PCR_ATTRS(_alg, _hash) \
470 PCR_ATTR_GROUP_ARRAY(_alg, _hash); \
471 PCR_ATTR_GROUP(_alg, _hash)
473 * End of macro structure to build an attribute group containing 24
474 * PCR value files for each supported hash algorithm
478 * The next set of macros implements the cleverness for each hash to
479 * build a static attribute group called pcr_group_<hash> which can be
480 * added to chip->groups[].
482 * The first argument is the TPM algorithm id and the second is the
483 * hash used as both the suffix and the group name. Note: the group
484 * name is a directory in the top level tpm class with the name
485 * pcr-<hash>, so it must not clash with any other names already
486 * in the sysfs directory.
488 PCR_ATTR_BUILD(TPM_ALG_SHA1, sha1);
489 PCR_ATTR_BUILD(TPM_ALG_SHA256, sha256);
490 PCR_ATTR_BUILD(TPM_ALG_SHA384, sha384);
491 PCR_ATTR_BUILD(TPM_ALG_SHA512, sha512);
492 PCR_ATTR_BUILD(TPM_ALG_SM3_256, sm3);
495 void tpm_sysfs_add_device(struct tpm_chip *chip)
499 WARN_ON(chip->groups_cnt != 0);
501 if (tpm_is_firmware_upgrade(chip))
504 if (chip->flags & TPM_CHIP_FLAG_TPM2)
505 chip->groups[chip->groups_cnt++] = &tpm2_dev_group;
507 chip->groups[chip->groups_cnt++] = &tpm1_dev_group;
509 /* add one group for each bank hash */
510 for (i = 0; i < chip->nr_allocated_banks; i++) {
511 switch (chip->allocated_banks[i].alg_id) {
513 chip->groups[chip->groups_cnt++] = &pcr_group_sha1;
516 chip->groups[chip->groups_cnt++] = &pcr_group_sha256;
519 chip->groups[chip->groups_cnt++] = &pcr_group_sha384;
522 chip->groups[chip->groups_cnt++] = &pcr_group_sha512;
524 case TPM_ALG_SM3_256:
525 chip->groups[chip->groups_cnt++] = &pcr_group_sm3;
529 * If triggers, send a patch to add both a
530 * PCR_ATTR_BUILD() macro above for the
531 * missing algorithm as well as an additional
532 * case in this switch statement.
535 "TPM with unsupported bank algorithm 0x%04x",
536 chip->allocated_banks[i].alg_id);
542 * This will only trigger if someone has added an additional
543 * hash to the tpm_algorithms enum without incrementing
546 WARN_ON(chip->groups_cnt > TPM_MAX_HASHES + 1);