Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 IBM Corporation |
| 3 | * Authors: |
| 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> |
| 8 | * |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 9 | * Copyright (C) 2013 Obsidian Research Corp |
| 10 | * Jason Gunthorpe <jgunthorpe@obsidianresearch.com> |
| 11 | * |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 12 | * sysfs filesystem inspection interface to the TPM |
| 13 | * |
| 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 |
| 17 | * License. |
| 18 | * |
| 19 | */ |
| 20 | #include <linux/device.h> |
| 21 | #include "tpm.h" |
| 22 | |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 23 | #define READ_PUBEK_RESULT_SIZE 314 |
| 24 | #define TPM_ORD_READPUBEK cpu_to_be32(124) |
| 25 | static struct tpm_input_header tpm_readpubek_header = { |
| 26 | .tag = TPM_TAG_RQU_COMMAND, |
| 27 | .length = cpu_to_be32(30), |
| 28 | .ordinal = TPM_ORD_READPUBEK |
| 29 | }; |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 30 | static ssize_t pubek_show(struct device *dev, struct device_attribute *attr, |
| 31 | char *buf) |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 32 | { |
| 33 | u8 *data; |
| 34 | struct tpm_cmd_t tpm_cmd; |
| 35 | ssize_t err; |
| 36 | int i, rc; |
| 37 | char *str = buf; |
| 38 | |
| 39 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 40 | |
| 41 | tpm_cmd.header.in = tpm_readpubek_header; |
Jarkko Sakkinen | 87155b7 | 2014-12-12 11:46:33 -0800 | [diff] [blame] | 42 | err = tpm_transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE, |
| 43 | "attempting to read the PUBEK"); |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 44 | if (err) |
| 45 | goto out; |
| 46 | |
| 47 | /* |
| 48 | ignore header 10 bytes |
| 49 | algorithm 32 bits (1 == RSA ) |
| 50 | encscheme 16 bits |
| 51 | sigscheme 16 bits |
| 52 | parameters (RSA 12->bytes: keybit, #primes, expbit) |
| 53 | keylenbytes 32 bits |
| 54 | 256 byte modulus |
| 55 | ignore checksum 20 bytes |
| 56 | */ |
| 57 | data = tpm_cmd.params.readpubek_out_buffer; |
| 58 | str += |
| 59 | sprintf(str, |
| 60 | "Algorithm: %02X %02X %02X %02X\n" |
| 61 | "Encscheme: %02X %02X\n" |
| 62 | "Sigscheme: %02X %02X\n" |
| 63 | "Parameters: %02X %02X %02X %02X " |
| 64 | "%02X %02X %02X %02X " |
| 65 | "%02X %02X %02X %02X\n" |
| 66 | "Modulus length: %d\n" |
| 67 | "Modulus:\n", |
| 68 | data[0], data[1], data[2], data[3], |
| 69 | data[4], data[5], |
| 70 | data[6], data[7], |
| 71 | data[12], data[13], data[14], data[15], |
| 72 | data[16], data[17], data[18], data[19], |
| 73 | data[20], data[21], data[22], data[23], |
| 74 | be32_to_cpu(*((__be32 *) (data + 24)))); |
| 75 | |
| 76 | for (i = 0; i < 256; i++) { |
| 77 | str += sprintf(str, "%02X ", data[i + 28]); |
| 78 | if ((i + 1) % 16 == 0) |
| 79 | str += sprintf(str, "\n"); |
| 80 | } |
| 81 | out: |
| 82 | rc = str - buf; |
| 83 | return rc; |
| 84 | } |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 85 | static DEVICE_ATTR_RO(pubek); |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 86 | |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 87 | static ssize_t pcrs_show(struct device *dev, struct device_attribute *attr, |
| 88 | char *buf) |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 89 | { |
| 90 | cap_t cap; |
| 91 | u8 digest[TPM_DIGEST_SIZE]; |
| 92 | ssize_t rc; |
| 93 | int i, j, num_pcrs; |
| 94 | char *str = buf; |
| 95 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 96 | |
| 97 | rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap, |
| 98 | "attempting to determine the number of PCRS"); |
| 99 | if (rc) |
| 100 | return 0; |
| 101 | |
| 102 | num_pcrs = be32_to_cpu(cap.num_pcrs); |
| 103 | for (i = 0; i < num_pcrs; i++) { |
| 104 | rc = tpm_pcr_read_dev(chip, i, digest); |
| 105 | if (rc) |
| 106 | break; |
| 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"); |
| 111 | } |
| 112 | return str - buf; |
| 113 | } |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 114 | static DEVICE_ATTR_RO(pcrs); |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 115 | |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 116 | static ssize_t enabled_show(struct device *dev, struct device_attribute *attr, |
| 117 | char *buf) |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 118 | { |
| 119 | cap_t cap; |
| 120 | ssize_t rc; |
| 121 | |
| 122 | rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap, |
| 123 | "attempting to determine the permanent enabled state"); |
| 124 | if (rc) |
| 125 | return 0; |
| 126 | |
| 127 | rc = sprintf(buf, "%d\n", !cap.perm_flags.disable); |
| 128 | return rc; |
| 129 | } |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 130 | static DEVICE_ATTR_RO(enabled); |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 131 | |
Fengguang Wu | 5f64822 | 2013-12-07 16:10:26 +0100 | [diff] [blame] | 132 | static ssize_t active_show(struct device *dev, struct device_attribute *attr, |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 133 | char *buf) |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 134 | { |
| 135 | cap_t cap; |
| 136 | ssize_t rc; |
| 137 | |
| 138 | rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap, |
| 139 | "attempting to determine the permanent active state"); |
| 140 | if (rc) |
| 141 | return 0; |
| 142 | |
| 143 | rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated); |
| 144 | return rc; |
| 145 | } |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 146 | static DEVICE_ATTR_RO(active); |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 147 | |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 148 | static ssize_t owned_show(struct device *dev, struct device_attribute *attr, |
| 149 | char *buf) |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 150 | { |
| 151 | cap_t cap; |
| 152 | ssize_t rc; |
| 153 | |
| 154 | rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap, |
| 155 | "attempting to determine the owner state"); |
| 156 | if (rc) |
| 157 | return 0; |
| 158 | |
| 159 | rc = sprintf(buf, "%d\n", cap.owned); |
| 160 | return rc; |
| 161 | } |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 162 | static DEVICE_ATTR_RO(owned); |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 163 | |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 164 | static ssize_t temp_deactivated_show(struct device *dev, |
| 165 | struct device_attribute *attr, char *buf) |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 166 | { |
| 167 | cap_t cap; |
| 168 | ssize_t rc; |
| 169 | |
| 170 | rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap, |
| 171 | "attempting to determine the temporary state"); |
| 172 | if (rc) |
| 173 | return 0; |
| 174 | |
| 175 | rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated); |
| 176 | return rc; |
| 177 | } |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 178 | static DEVICE_ATTR_RO(temp_deactivated); |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 179 | |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 180 | static ssize_t caps_show(struct device *dev, struct device_attribute *attr, |
| 181 | char *buf) |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 182 | { |
| 183 | cap_t cap; |
| 184 | ssize_t rc; |
| 185 | char *str = buf; |
| 186 | |
| 187 | rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap, |
| 188 | "attempting to determine the manufacturer"); |
| 189 | if (rc) |
| 190 | return 0; |
| 191 | str += sprintf(str, "Manufacturer: 0x%x\n", |
| 192 | be32_to_cpu(cap.manufacturer_id)); |
| 193 | |
| 194 | /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */ |
| 195 | rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap, |
| 196 | "attempting to determine the 1.2 version"); |
| 197 | if (!rc) { |
| 198 | str += sprintf(str, |
| 199 | "TCG version: %d.%d\nFirmware version: %d.%d\n", |
| 200 | cap.tpm_version_1_2.Major, |
| 201 | cap.tpm_version_1_2.Minor, |
| 202 | cap.tpm_version_1_2.revMajor, |
| 203 | cap.tpm_version_1_2.revMinor); |
| 204 | } else { |
| 205 | /* Otherwise just use TPM_STRUCT_VER */ |
| 206 | rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap, |
| 207 | "attempting to determine the 1.1 version"); |
| 208 | if (rc) |
| 209 | return 0; |
| 210 | str += sprintf(str, |
| 211 | "TCG version: %d.%d\nFirmware version: %d.%d\n", |
| 212 | cap.tpm_version.Major, |
| 213 | cap.tpm_version.Minor, |
| 214 | cap.tpm_version.revMajor, |
| 215 | cap.tpm_version.revMinor); |
| 216 | } |
| 217 | |
| 218 | return str - buf; |
| 219 | } |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 220 | static DEVICE_ATTR_RO(caps); |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 221 | |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 222 | static ssize_t cancel_store(struct device *dev, struct device_attribute *attr, |
| 223 | const char *buf, size_t count) |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 224 | { |
| 225 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 226 | if (chip == NULL) |
| 227 | return 0; |
| 228 | |
Jason Gunthorpe | 5f82e9f | 2013-11-26 13:30:44 -0700 | [diff] [blame] | 229 | chip->ops->cancel(chip); |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 230 | return count; |
| 231 | } |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 232 | static DEVICE_ATTR_WO(cancel); |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 233 | |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 234 | static ssize_t durations_show(struct device *dev, struct device_attribute *attr, |
| 235 | char *buf) |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 236 | { |
| 237 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 238 | |
Christophe Ricard | af782f3 | 2016-03-31 22:56:59 +0200 | [diff] [blame^] | 239 | if (chip->duration[TPM_LONG] == 0) |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 240 | return 0; |
| 241 | |
| 242 | return sprintf(buf, "%d %d %d [%s]\n", |
Christophe Ricard | af782f3 | 2016-03-31 22:56:59 +0200 | [diff] [blame^] | 243 | jiffies_to_usecs(chip->duration[TPM_SHORT]), |
| 244 | jiffies_to_usecs(chip->duration[TPM_MEDIUM]), |
| 245 | jiffies_to_usecs(chip->duration[TPM_LONG]), |
| 246 | chip->duration_adjusted |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 247 | ? "adjusted" : "original"); |
| 248 | } |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 249 | static DEVICE_ATTR_RO(durations); |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 250 | |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 251 | static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr, |
| 252 | char *buf) |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 253 | { |
| 254 | struct tpm_chip *chip = dev_get_drvdata(dev); |
| 255 | |
| 256 | return sprintf(buf, "%d %d %d %d [%s]\n", |
Christophe Ricard | af782f3 | 2016-03-31 22:56:59 +0200 | [diff] [blame^] | 257 | jiffies_to_usecs(chip->timeout_a), |
| 258 | jiffies_to_usecs(chip->timeout_b), |
| 259 | jiffies_to_usecs(chip->timeout_c), |
| 260 | jiffies_to_usecs(chip->timeout_d), |
| 261 | chip->timeout_adjusted |
Jason Gunthorpe | 000a07b | 2013-11-26 13:30:41 -0700 | [diff] [blame] | 262 | ? "adjusted" : "original"); |
| 263 | } |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 264 | static DEVICE_ATTR_RO(timeouts); |
| 265 | |
| 266 | static struct attribute *tpm_dev_attrs[] = { |
| 267 | &dev_attr_pubek.attr, |
| 268 | &dev_attr_pcrs.attr, |
| 269 | &dev_attr_enabled.attr, |
| 270 | &dev_attr_active.attr, |
| 271 | &dev_attr_owned.attr, |
| 272 | &dev_attr_temp_deactivated.attr, |
| 273 | &dev_attr_caps.attr, |
| 274 | &dev_attr_cancel.attr, |
| 275 | &dev_attr_durations.attr, |
| 276 | &dev_attr_timeouts.attr, |
| 277 | NULL, |
| 278 | }; |
| 279 | |
| 280 | static const struct attribute_group tpm_dev_group = { |
| 281 | .attrs = tpm_dev_attrs, |
| 282 | }; |
| 283 | |
| 284 | int tpm_sysfs_add_device(struct tpm_chip *chip) |
| 285 | { |
| 286 | int err; |
Jason Gunthorpe | 8cfffc9 | 2016-02-29 12:29:47 -0500 | [diff] [blame] | 287 | err = sysfs_create_group(&chip->dev.parent->kobj, |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 288 | &tpm_dev_group); |
| 289 | |
| 290 | if (err) |
Jason Gunthorpe | 8cfffc9 | 2016-02-29 12:29:47 -0500 | [diff] [blame] | 291 | dev_err(&chip->dev, |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 292 | "failed to create sysfs attributes, %d\n", err); |
| 293 | return err; |
| 294 | } |
| 295 | |
| 296 | void tpm_sysfs_del_device(struct tpm_chip *chip) |
| 297 | { |
Jason Gunthorpe | 4e26195 | 2016-02-12 20:29:53 -0700 | [diff] [blame] | 298 | /* The sysfs routines rely on an implicit tpm_try_get_ops, this |
| 299 | * function is called before ops is null'd and the sysfs core |
| 300 | * synchronizes this removal so that no callbacks are running or can |
| 301 | * run again |
| 302 | */ |
Jason Gunthorpe | 8cfffc9 | 2016-02-29 12:29:47 -0500 | [diff] [blame] | 303 | sysfs_remove_group(&chip->dev.parent->kobj, &tpm_dev_group); |
Jason Gunthorpe | 1e3b73a | 2013-11-26 13:30:42 -0700 | [diff] [blame] | 304 | } |