.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright (C) 2004 IBM Corporation |
---|
3 | 4 | * Authors: |
---|
.. | .. |
---|
10 | 11 | * Jason Gunthorpe <jgunthorpe@obsidianresearch.com> |
---|
11 | 12 | * |
---|
12 | 13 | * 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 | 14 | */ |
---|
20 | 15 | #include <linux/device.h> |
---|
21 | 16 | #include "tpm.h" |
---|
.. | .. |
---|
54 | 49 | |
---|
55 | 50 | tpm_buf_append(&tpm_buf, anti_replay, sizeof(anti_replay)); |
---|
56 | 51 | |
---|
57 | | - if (tpm_transmit_cmd(chip, NULL, tpm_buf.data, PAGE_SIZE, |
---|
58 | | - READ_PUBEK_RESULT_MIN_BODY_SIZE, 0, |
---|
59 | | - "attempting to read the PUBEK")) |
---|
| 52 | + if (tpm_transmit_cmd(chip, &tpm_buf, READ_PUBEK_RESULT_MIN_BODY_SIZE, |
---|
| 53 | + "attempting to read the PUBEK")) |
---|
60 | 54 | goto out_buf; |
---|
61 | 55 | |
---|
62 | 56 | out = (struct tpm_readpubek_out *)&tpm_buf.data[10]; |
---|
63 | 57 | str += |
---|
64 | 58 | sprintf(str, |
---|
65 | | - "Algorithm: %02X %02X %02X %02X\n" |
---|
66 | | - "Encscheme: %02X %02X\n" |
---|
67 | | - "Sigscheme: %02X %02X\n" |
---|
68 | | - "Parameters: %02X %02X %02X %02X " |
---|
69 | | - "%02X %02X %02X %02X " |
---|
70 | | - "%02X %02X %02X %02X\n" |
---|
| 59 | + "Algorithm: %4ph\n" |
---|
| 60 | + "Encscheme: %2ph\n" |
---|
| 61 | + "Sigscheme: %2ph\n" |
---|
| 62 | + "Parameters: %12ph\n" |
---|
71 | 63 | "Modulus length: %d\n" |
---|
72 | 64 | "Modulus:\n", |
---|
73 | | - out->algorithm[0], out->algorithm[1], out->algorithm[2], |
---|
74 | | - out->algorithm[3], |
---|
75 | | - out->encscheme[0], out->encscheme[1], |
---|
76 | | - out->sigscheme[0], out->sigscheme[1], |
---|
77 | | - out->parameters[0], out->parameters[1], |
---|
78 | | - out->parameters[2], out->parameters[3], |
---|
79 | | - out->parameters[4], out->parameters[5], |
---|
80 | | - out->parameters[6], out->parameters[7], |
---|
81 | | - out->parameters[8], out->parameters[9], |
---|
82 | | - out->parameters[10], out->parameters[11], |
---|
| 65 | + out->algorithm, |
---|
| 66 | + out->encscheme, |
---|
| 67 | + out->sigscheme, |
---|
| 68 | + out->parameters, |
---|
83 | 69 | be32_to_cpu(out->keysize)); |
---|
84 | 70 | |
---|
85 | | - for (i = 0; i < 256; i++) { |
---|
86 | | - str += sprintf(str, "%02X ", out->modulus[i]); |
---|
87 | | - if ((i + 1) % 16 == 0) |
---|
88 | | - str += sprintf(str, "\n"); |
---|
89 | | - } |
---|
| 71 | + for (i = 0; i < 256; i += 16) |
---|
| 72 | + str += sprintf(str, "%16ph\n", &out->modulus[i]); |
---|
90 | 73 | |
---|
91 | 74 | out_buf: |
---|
92 | 75 | tpm_buf_destroy(&tpm_buf); |
---|
.. | .. |
---|
101 | 84 | { |
---|
102 | 85 | cap_t cap; |
---|
103 | 86 | u8 digest[TPM_DIGEST_SIZE]; |
---|
104 | | - ssize_t rc; |
---|
105 | | - int i, j, num_pcrs; |
---|
| 87 | + u32 i, j, num_pcrs; |
---|
106 | 88 | char *str = buf; |
---|
107 | 89 | struct tpm_chip *chip = to_tpm_chip(dev); |
---|
108 | 90 | |
---|
109 | 91 | if (tpm_try_get_ops(chip)) |
---|
110 | 92 | return 0; |
---|
111 | 93 | |
---|
112 | | - if (tpm_getcap(chip, TPM_CAP_PROP_PCR, &cap, |
---|
113 | | - "attempting to determine the number of PCRS", |
---|
114 | | - sizeof(cap.num_pcrs))) { |
---|
| 94 | + if (tpm1_getcap(chip, TPM_CAP_PROP_PCR, &cap, |
---|
| 95 | + "attempting to determine the number of PCRS", |
---|
| 96 | + sizeof(cap.num_pcrs))) { |
---|
115 | 97 | tpm_put_ops(chip); |
---|
116 | 98 | return 0; |
---|
117 | 99 | } |
---|
118 | 100 | |
---|
119 | 101 | num_pcrs = be32_to_cpu(cap.num_pcrs); |
---|
120 | 102 | for (i = 0; i < num_pcrs; i++) { |
---|
121 | | - rc = tpm_pcr_read_dev(chip, i, digest); |
---|
122 | | - if (rc) |
---|
| 103 | + if (tpm1_pcr_read(chip, i, digest)) { |
---|
| 104 | + str = buf; |
---|
123 | 105 | break; |
---|
| 106 | + } |
---|
124 | 107 | str += sprintf(str, "PCR-%02d: ", i); |
---|
125 | 108 | for (j = 0; j < TPM_DIGEST_SIZE; j++) |
---|
126 | 109 | str += sprintf(str, "%02X ", digest[j]); |
---|
.. | .. |
---|
141 | 124 | if (tpm_try_get_ops(chip)) |
---|
142 | 125 | return 0; |
---|
143 | 126 | |
---|
144 | | - if (tpm_getcap(chip, TPM_CAP_FLAG_PERM, &cap, |
---|
145 | | - "attempting to determine the permanent enabled state", |
---|
146 | | - sizeof(cap.perm_flags))) |
---|
| 127 | + if (tpm1_getcap(chip, TPM_CAP_FLAG_PERM, &cap, |
---|
| 128 | + "attempting to determine the permanent enabled state", |
---|
| 129 | + sizeof(cap.perm_flags))) |
---|
147 | 130 | goto out_ops; |
---|
148 | 131 | |
---|
149 | 132 | rc = sprintf(buf, "%d\n", !cap.perm_flags.disable); |
---|
.. | .. |
---|
163 | 146 | if (tpm_try_get_ops(chip)) |
---|
164 | 147 | return 0; |
---|
165 | 148 | |
---|
166 | | - if (tpm_getcap(chip, TPM_CAP_FLAG_PERM, &cap, |
---|
167 | | - "attempting to determine the permanent active state", |
---|
168 | | - sizeof(cap.perm_flags))) |
---|
| 149 | + if (tpm1_getcap(chip, TPM_CAP_FLAG_PERM, &cap, |
---|
| 150 | + "attempting to determine the permanent active state", |
---|
| 151 | + sizeof(cap.perm_flags))) |
---|
169 | 152 | goto out_ops; |
---|
170 | 153 | |
---|
171 | 154 | rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated); |
---|
.. | .. |
---|
185 | 168 | if (tpm_try_get_ops(chip)) |
---|
186 | 169 | return 0; |
---|
187 | 170 | |
---|
188 | | - if (tpm_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap, |
---|
189 | | - "attempting to determine the owner state", |
---|
190 | | - sizeof(cap.owned))) |
---|
| 171 | + if (tpm1_getcap(to_tpm_chip(dev), TPM_CAP_PROP_OWNER, &cap, |
---|
| 172 | + "attempting to determine the owner state", |
---|
| 173 | + sizeof(cap.owned))) |
---|
191 | 174 | goto out_ops; |
---|
192 | 175 | |
---|
193 | 176 | rc = sprintf(buf, "%d\n", cap.owned); |
---|
.. | .. |
---|
207 | 190 | if (tpm_try_get_ops(chip)) |
---|
208 | 191 | return 0; |
---|
209 | 192 | |
---|
210 | | - if (tpm_getcap(to_tpm_chip(dev), TPM_CAP_FLAG_VOL, &cap, |
---|
211 | | - "attempting to determine the temporary state", |
---|
212 | | - sizeof(cap.stclear_flags))) |
---|
| 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))) |
---|
213 | 196 | goto out_ops; |
---|
214 | 197 | |
---|
215 | 198 | rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated); |
---|
.. | .. |
---|
223 | 206 | char *buf) |
---|
224 | 207 | { |
---|
225 | 208 | struct tpm_chip *chip = to_tpm_chip(dev); |
---|
| 209 | + struct tpm1_version *version; |
---|
226 | 210 | ssize_t rc = 0; |
---|
227 | 211 | char *str = buf; |
---|
228 | 212 | cap_t cap; |
---|
.. | .. |
---|
230 | 214 | if (tpm_try_get_ops(chip)) |
---|
231 | 215 | return 0; |
---|
232 | 216 | |
---|
233 | | - if (tpm_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap, |
---|
234 | | - "attempting to determine the manufacturer", |
---|
235 | | - sizeof(cap.manufacturer_id))) |
---|
| 217 | + if (tpm1_getcap(chip, TPM_CAP_PROP_MANUFACTURER, &cap, |
---|
| 218 | + "attempting to determine the manufacturer", |
---|
| 219 | + sizeof(cap.manufacturer_id))) |
---|
236 | 220 | goto out_ops; |
---|
237 | 221 | |
---|
238 | 222 | str += sprintf(str, "Manufacturer: 0x%x\n", |
---|
239 | 223 | be32_to_cpu(cap.manufacturer_id)); |
---|
240 | 224 | |
---|
241 | | - /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */ |
---|
242 | | - rc = tpm_getcap(chip, TPM_CAP_VERSION_1_2, &cap, |
---|
243 | | - "attempting to determine the 1.2 version", |
---|
244 | | - sizeof(cap.tpm_version_1_2)); |
---|
245 | | - if (!rc) { |
---|
246 | | - str += sprintf(str, |
---|
247 | | - "TCG version: %d.%d\nFirmware version: %d.%d\n", |
---|
248 | | - cap.tpm_version_1_2.Major, |
---|
249 | | - cap.tpm_version_1_2.Minor, |
---|
250 | | - cap.tpm_version_1_2.revMajor, |
---|
251 | | - cap.tpm_version_1_2.revMinor); |
---|
252 | | - } else { |
---|
253 | | - /* Otherwise just use TPM_STRUCT_VER */ |
---|
254 | | - if (tpm_getcap(chip, TPM_CAP_VERSION_1_1, &cap, |
---|
255 | | - "attempting to determine the 1.1 version", |
---|
256 | | - sizeof(cap.tpm_version))) |
---|
257 | | - goto out_ops; |
---|
| 225 | + /* TPM 1.2 */ |
---|
| 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; |
---|
| 230 | + goto out_print; |
---|
| 231 | + } |
---|
258 | 232 | |
---|
259 | | - str += sprintf(str, |
---|
260 | | - "TCG version: %d.%d\nFirmware version: %d.%d\n", |
---|
261 | | - cap.tpm_version.Major, |
---|
262 | | - cap.tpm_version.Minor, |
---|
263 | | - cap.tpm_version.revMajor, |
---|
264 | | - cap.tpm_version.revMinor); |
---|
265 | | -} |
---|
| 233 | + /* TPM 1.1 */ |
---|
| 234 | + if (tpm1_getcap(chip, TPM_CAP_VERSION_1_1, &cap, |
---|
| 235 | + "attempting to determine the 1.1 version", |
---|
| 236 | + sizeof(cap.version1))) { |
---|
| 237 | + goto out_ops; |
---|
| 238 | + } |
---|
| 239 | + |
---|
| 240 | + version = &cap.version1; |
---|
| 241 | + |
---|
| 242 | +out_print: |
---|
| 243 | + str += sprintf(str, |
---|
| 244 | + "TCG version: %d.%d\nFirmware version: %d.%d\n", |
---|
| 245 | + version->major, version->minor, |
---|
| 246 | + version->rev_major, version->rev_minor); |
---|
| 247 | + |
---|
266 | 248 | rc = str - buf; |
---|
| 249 | + |
---|
267 | 250 | out_ops: |
---|
268 | 251 | tpm_put_ops(chip); |
---|
269 | 252 | return rc; |
---|
.. | .. |
---|
316 | 299 | } |
---|
317 | 300 | static DEVICE_ATTR_RO(timeouts); |
---|
318 | 301 | |
---|
319 | | -static struct attribute *tpm_dev_attrs[] = { |
---|
| 302 | +static ssize_t tpm_version_major_show(struct device *dev, |
---|
| 303 | + struct device_attribute *attr, char *buf) |
---|
| 304 | +{ |
---|
| 305 | + struct tpm_chip *chip = to_tpm_chip(dev); |
---|
| 306 | + |
---|
| 307 | + return sprintf(buf, "%s\n", chip->flags & TPM_CHIP_FLAG_TPM2 |
---|
| 308 | + ? "2" : "1"); |
---|
| 309 | +} |
---|
| 310 | +static DEVICE_ATTR_RO(tpm_version_major); |
---|
| 311 | + |
---|
| 312 | +static struct attribute *tpm1_dev_attrs[] = { |
---|
320 | 313 | &dev_attr_pubek.attr, |
---|
321 | 314 | &dev_attr_pcrs.attr, |
---|
322 | 315 | &dev_attr_enabled.attr, |
---|
.. | .. |
---|
327 | 320 | &dev_attr_cancel.attr, |
---|
328 | 321 | &dev_attr_durations.attr, |
---|
329 | 322 | &dev_attr_timeouts.attr, |
---|
| 323 | + &dev_attr_tpm_version_major.attr, |
---|
330 | 324 | NULL, |
---|
331 | 325 | }; |
---|
332 | 326 | |
---|
333 | | -static const struct attribute_group tpm_dev_group = { |
---|
334 | | - .attrs = tpm_dev_attrs, |
---|
| 327 | +static struct attribute *tpm2_dev_attrs[] = { |
---|
| 328 | + &dev_attr_tpm_version_major.attr, |
---|
| 329 | + NULL |
---|
| 330 | +}; |
---|
| 331 | + |
---|
| 332 | +static const struct attribute_group tpm1_dev_group = { |
---|
| 333 | + .attrs = tpm1_dev_attrs, |
---|
| 334 | +}; |
---|
| 335 | + |
---|
| 336 | +static const struct attribute_group tpm2_dev_group = { |
---|
| 337 | + .attrs = tpm2_dev_attrs, |
---|
335 | 338 | }; |
---|
336 | 339 | |
---|
337 | 340 | void tpm_sysfs_add_device(struct tpm_chip *chip) |
---|
338 | 341 | { |
---|
339 | | - /* XXX: If you wish to remove this restriction, you must first update |
---|
340 | | - * tpm_sysfs to explicitly lock chip->ops. |
---|
341 | | - */ |
---|
342 | | - if (chip->flags & TPM_CHIP_FLAG_TPM2) |
---|
343 | | - return; |
---|
344 | | - |
---|
345 | | - /* The sysfs routines rely on an implicit tpm_try_get_ops, device_del |
---|
346 | | - * is called before ops is null'd and the sysfs core synchronizes this |
---|
347 | | - * removal so that no callbacks are running or can run again |
---|
348 | | - */ |
---|
349 | 342 | WARN_ON(chip->groups_cnt != 0); |
---|
350 | | - chip->groups[chip->groups_cnt++] = &tpm_dev_group; |
---|
| 343 | + if (chip->flags & TPM_CHIP_FLAG_TPM2) |
---|
| 344 | + chip->groups[chip->groups_cnt++] = &tpm2_dev_group; |
---|
| 345 | + else |
---|
| 346 | + chip->groups[chip->groups_cnt++] = &tpm1_dev_group; |
---|
351 | 347 | } |
---|