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 * sysfs filesystem inspection interface to the TPM
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation, version 2 of the
17 #include <linux/device.h>
20 /* XXX for now this helper is duplicated in tpm-interface.c */
21 static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
22 int len, const char *desc)
26 len = tpm_transmit(chip, (u8 *) cmd, len);
29 else if (len < TPM_HEADER_SIZE)
32 err = be32_to_cpu(cmd->header.out.return_code);
34 dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
39 #define READ_PUBEK_RESULT_SIZE 314
40 #define TPM_ORD_READPUBEK cpu_to_be32(124)
41 static struct tpm_input_header tpm_readpubek_header = {
42 .tag = TPM_TAG_RQU_COMMAND,
43 .length = cpu_to_be32(30),
44 .ordinal = TPM_ORD_READPUBEK
47 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
51 struct tpm_cmd_t tpm_cmd;
56 struct tpm_chip *chip = dev_get_drvdata(dev);
58 tpm_cmd.header.in = tpm_readpubek_header;
59 err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
60 "attempting to read the PUBEK");
65 ignore header 10 bytes
66 algorithm 32 bits (1 == RSA )
69 parameters (RSA 12->bytes: keybit, #primes, expbit)
72 ignore checksum 20 bytes
74 data = tpm_cmd.params.readpubek_out_buffer;
77 "Algorithm: %02X %02X %02X %02X\n"
78 "Encscheme: %02X %02X\n"
79 "Sigscheme: %02X %02X\n"
80 "Parameters: %02X %02X %02X %02X "
81 "%02X %02X %02X %02X "
82 "%02X %02X %02X %02X\n"
83 "Modulus length: %d\n"
85 data[0], data[1], data[2], data[3],
88 data[12], data[13], data[14], data[15],
89 data[16], data[17], data[18], data[19],
90 data[20], data[21], data[22], data[23],
91 be32_to_cpu(*((__be32 *) (data + 24))));
93 for (i = 0; i < 256; i++) {
94 str += sprintf(str, "%02X ", data[i + 28]);
95 if ((i + 1) % 16 == 0)
96 str += sprintf(str, "\n");
102 EXPORT_SYMBOL_GPL(tpm_show_pubek);
104 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
108 u8 digest[TPM_DIGEST_SIZE];
112 struct tpm_chip *chip = dev_get_drvdata(dev);
114 rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
115 "attempting to determine the number of PCRS");
119 num_pcrs = be32_to_cpu(cap.num_pcrs);
120 for (i = 0; i < num_pcrs; i++) {
121 rc = tpm_pcr_read_dev(chip, i, digest);
124 str += sprintf(str, "PCR-%02d: ", i);
125 for (j = 0; j < TPM_DIGEST_SIZE; j++)
126 str += sprintf(str, "%02X ", digest[j]);
127 str += sprintf(str, "\n");
131 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
133 ssize_t tpm_show_enabled(struct device *dev, struct device_attribute *attr,
139 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
140 "attempting to determine the permanent enabled state");
144 rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
147 EXPORT_SYMBOL_GPL(tpm_show_enabled);
149 ssize_t tpm_show_active(struct device *dev, struct device_attribute *attr,
155 rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
156 "attempting to determine the permanent active state");
160 rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
163 EXPORT_SYMBOL_GPL(tpm_show_active);
165 ssize_t tpm_show_owned(struct device *dev, struct device_attribute *attr,
171 rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
172 "attempting to determine the owner state");
176 rc = sprintf(buf, "%d\n", cap.owned);
179 EXPORT_SYMBOL_GPL(tpm_show_owned);
181 ssize_t tpm_show_temp_deactivated(struct device *dev,
182 struct device_attribute *attr, char *buf)
187 rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
188 "attempting to determine the temporary state");
192 rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
195 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
197 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
204 rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
205 "attempting to determine the manufacturer");
208 str += sprintf(str, "Manufacturer: 0x%x\n",
209 be32_to_cpu(cap.manufacturer_id));
211 /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */
212 rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
213 "attempting to determine the 1.2 version");
216 "TCG version: %d.%d\nFirmware version: %d.%d\n",
217 cap.tpm_version_1_2.Major,
218 cap.tpm_version_1_2.Minor,
219 cap.tpm_version_1_2.revMajor,
220 cap.tpm_version_1_2.revMinor);
222 /* Otherwise just use TPM_STRUCT_VER */
223 rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
224 "attempting to determine the 1.1 version");
228 "TCG version: %d.%d\nFirmware version: %d.%d\n",
229 cap.tpm_version.Major,
230 cap.tpm_version.Minor,
231 cap.tpm_version.revMajor,
232 cap.tpm_version.revMinor);
237 EXPORT_SYMBOL_GPL(tpm_show_caps);
239 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
240 const char *buf, size_t count)
242 struct tpm_chip *chip = dev_get_drvdata(dev);
246 chip->vendor.cancel(chip);
249 EXPORT_SYMBOL_GPL(tpm_store_cancel);
251 ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
254 struct tpm_chip *chip = dev_get_drvdata(dev);
256 if (chip->vendor.duration[TPM_LONG] == 0)
259 return sprintf(buf, "%d %d %d [%s]\n",
260 jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
261 jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
262 jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
263 chip->vendor.duration_adjusted
264 ? "adjusted" : "original");
266 EXPORT_SYMBOL_GPL(tpm_show_durations);
268 ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
271 struct tpm_chip *chip = dev_get_drvdata(dev);
273 return sprintf(buf, "%d %d %d %d [%s]\n",
274 jiffies_to_usecs(chip->vendor.timeout_a),
275 jiffies_to_usecs(chip->vendor.timeout_b),
276 jiffies_to_usecs(chip->vendor.timeout_c),
277 jiffies_to_usecs(chip->vendor.timeout_d),
278 chip->vendor.timeout_adjusted
279 ? "adjusted" : "original");
281 EXPORT_SYMBOL_GPL(tpm_show_timeouts);