^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2004 IBM Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Leendert van Doorn <leendert@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Dave Safford <safford@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Reiner Sailer <sailer@watson.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Kylene Hall <kjhall@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (C) 2013 Obsidian Research Corp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * sysfs filesystem inspection interface to the TPM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "tpm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct tpm_readpubek_out {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) u8 algorithm[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) u8 encscheme[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) u8 sigscheme[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) __be32 paramsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) u8 parameters[12];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) __be32 keysize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) u8 modulus[256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u8 checksum[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define READ_PUBEK_RESULT_MIN_BODY_SIZE (28 + 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define TPM_ORD_READPUBEK 124
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct tpm_buf tpm_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct tpm_readpubek_out *out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) char *str = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct tpm_chip *chip = to_tpm_chip(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) char anti_replay[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) memset(&anti_replay, 0, sizeof(anti_replay));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (tpm_try_get_ops(chip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (tpm_buf_init(&tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) goto out_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) tpm_buf_append(&tpm_buf, anti_replay, sizeof(anti_replay));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (tpm_transmit_cmd(chip, &tpm_buf, READ_PUBEK_RESULT_MIN_BODY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) "attempting to read the PUBEK"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) goto out_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) out = (struct tpm_readpubek_out *)&tpm_buf.data[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) str +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) sprintf(str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) "Algorithm: %4ph\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) "Encscheme: %2ph\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) "Sigscheme: %2ph\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) "Parameters: %12ph\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) "Modulus length: %d\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) "Modulus:\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) out->algorithm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) out->encscheme,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) out->sigscheme,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) out->parameters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) be32_to_cpu(out->keysize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) for (i = 0; i < 256; i += 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) str += sprintf(str, "%16ph\n", &out->modulus[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) out_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) tpm_buf_destroy(&tpm_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) out_ops:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) tpm_put_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return str - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static DEVICE_ATTR_RO(pubek);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) cap_t cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) u8 digest[TPM_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) u32 i, j, num_pcrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) char *str = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct tpm_chip *chip = to_tpm_chip(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (tpm_try_get_ops(chip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (tpm1_getcap(chip, TPM_CAP_PROP_PCR, &cap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) "attempting to determine the number of PCRS",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) sizeof(cap.num_pcrs))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) tpm_put_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) num_pcrs = be32_to_cpu(cap.num_pcrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) for (i = 0; i < num_pcrs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (tpm1_pcr_read(chip, i, digest)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) str = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) str += sprintf(str, "PCR-%02d: ", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) for (j = 0; j < TPM_DIGEST_SIZE; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) str += sprintf(str, "%02X ", digest[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) str += sprintf(str, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) tpm_put_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return str - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static DEVICE_ATTR_RO(pcrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static ssize_t enabled_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct tpm_chip *chip = to_tpm_chip(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ssize_t rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) cap_t cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (tpm_try_get_ops(chip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (tpm1_getcap(chip, TPM_CAP_FLAG_PERM, &cap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) "attempting to determine the permanent enabled state",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) sizeof(cap.perm_flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) goto out_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) out_ops:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) tpm_put_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static DEVICE_ATTR_RO(enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static ssize_t active_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct tpm_chip *chip = to_tpm_chip(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ssize_t rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) cap_t cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (tpm_try_get_ops(chip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (tpm1_getcap(chip, TPM_CAP_FLAG_PERM, &cap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) "attempting to determine the permanent active state",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) sizeof(cap.perm_flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) goto out_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) out_ops:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) tpm_put_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static DEVICE_ATTR_RO(active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static ssize_t owned_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct tpm_chip *chip = to_tpm_chip(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ssize_t rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) cap_t cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (tpm_try_get_ops(chip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (tpm1_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) "attempting to determine the owner state",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) sizeof(cap.owned)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) goto out_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) rc = sprintf(buf, "%d\n", cap.owned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) out_ops:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) tpm_put_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static DEVICE_ATTR_RO(owned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static ssize_t temp_deactivated_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct tpm_chip *chip = to_tpm_chip(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) ssize_t rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) cap_t cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (tpm_try_get_ops(chip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (tpm1_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) "attempting to determine the temporary state",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) sizeof(cap.stclear_flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) goto out_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) out_ops:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) tpm_put_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static DEVICE_ATTR_RO(temp_deactivated);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static ssize_t caps_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct tpm_chip *chip = to_tpm_chip(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct tpm1_version *version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) ssize_t rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) char *str = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) cap_t cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (tpm_try_get_ops(chip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (tpm1_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) "attempting to determine the manufacturer",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) sizeof(cap.manufacturer_id)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) goto out_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) str += sprintf(str, "Manufacturer: 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) be32_to_cpu(cap.manufacturer_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /* TPM 1.2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!tpm1_getcap(chip, TPM_CAP_VERSION_1_2, &cap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) "attempting to determine the 1.2 version",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) sizeof(cap.version2))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) version = &cap.version2.version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) goto out_print;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /* TPM 1.1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) "attempting to determine the 1.1 version",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) sizeof(cap.version1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) goto out_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) version = &cap.version1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) out_print:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) str += sprintf(str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) "TCG version: %d.%d\nFirmware version: %d.%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) version->major, version->minor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) version->rev_major, version->rev_minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) rc = str - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) out_ops:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) tpm_put_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static DEVICE_ATTR_RO(caps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static ssize_t cancel_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct tpm_chip *chip = to_tpm_chip(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (tpm_try_get_ops(chip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) chip->ops->cancel(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) tpm_put_ops(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static DEVICE_ATTR_WO(cancel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static ssize_t durations_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct tpm_chip *chip = to_tpm_chip(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (chip->duration[TPM_LONG] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return sprintf(buf, "%d %d %d [%s]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) jiffies_to_usecs(chip->duration[TPM_SHORT]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) jiffies_to_usecs(chip->duration[TPM_MEDIUM]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) jiffies_to_usecs(chip->duration[TPM_LONG]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) chip->duration_adjusted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ? "adjusted" : "original");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static DEVICE_ATTR_RO(durations);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct tpm_chip *chip = to_tpm_chip(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return sprintf(buf, "%d %d %d %d [%s]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) jiffies_to_usecs(chip->timeout_a),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) jiffies_to_usecs(chip->timeout_b),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) jiffies_to_usecs(chip->timeout_c),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) jiffies_to_usecs(chip->timeout_d),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) chip->timeout_adjusted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) ? "adjusted" : "original");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static DEVICE_ATTR_RO(timeouts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static ssize_t tpm_version_major_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct tpm_chip *chip = to_tpm_chip(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return sprintf(buf, "%s\n", chip->flags & TPM_CHIP_FLAG_TPM2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) ? "2" : "1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static DEVICE_ATTR_RO(tpm_version_major);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static struct attribute *tpm1_dev_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) &dev_attr_pubek.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) &dev_attr_pcrs.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) &dev_attr_enabled.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) &dev_attr_active.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) &dev_attr_owned.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) &dev_attr_temp_deactivated.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) &dev_attr_caps.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) &dev_attr_cancel.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) &dev_attr_durations.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) &dev_attr_timeouts.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) &dev_attr_tpm_version_major.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static struct attribute *tpm2_dev_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) &dev_attr_tpm_version_major.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static const struct attribute_group tpm1_dev_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) .attrs = tpm1_dev_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static const struct attribute_group tpm2_dev_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) .attrs = tpm2_dev_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) void tpm_sysfs_add_device(struct tpm_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) WARN_ON(chip->groups_cnt != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (chip->flags & TPM_CHIP_FLAG_TPM2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) chip->groups[chip->groups_cnt++] = &tpm2_dev_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) chip->groups[chip->groups_cnt++] = &tpm1_dev_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }