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