Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (C) 2004 IBM Corporation | |
3 | * | |
4 | * Authors: | |
5 | * Leendert van Doorn <leendert@watson.ibm.com> | |
6 | * Dave Safford <safford@watson.ibm.com> | |
7 | * Reiner Sailer <sailer@watson.ibm.com> | |
8 | * Kylene Hall <kjhall@us.ibm.com> | |
9 | * | |
8e81cc13 | 10 | * Maintained by: <tpmdd-devel@lists.sourceforge.net> |
1da177e4 LT |
11 | * |
12 | * Device driver for TCG/TCPA TPM (trusted platform module). | |
13 | * Specifications at www.trustedcomputinggroup.org | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or | |
16 | * modify it under the terms of the GNU General Public License as | |
17 | * published by the Free Software Foundation, version 2 of the | |
18 | * License. | |
19 | * | |
20 | * Note, the TPM chip is not interrupt driven (only polling) | |
21 | * and can have very long timeouts (minutes!). Hence the unusual | |
700d8bdc | 22 | * calls to msleep. |
1da177e4 LT |
23 | * |
24 | */ | |
25 | ||
1da177e4 | 26 | #include <linux/poll.h> |
5a0e3ad6 | 27 | #include <linux/slab.h> |
d081d470 | 28 | #include <linux/mutex.h> |
1da177e4 | 29 | #include <linux/spinlock.h> |
fd048866 | 30 | #include <linux/freezer.h> |
d081d470 | 31 | |
1da177e4 | 32 | #include "tpm.h" |
e5dcd87f | 33 | #include "tpm_eventlog.h" |
1da177e4 | 34 | |
9e18ee19 KJH |
35 | enum tpm_duration { |
36 | TPM_SHORT = 0, | |
37 | TPM_MEDIUM = 1, | |
38 | TPM_LONG = 2, | |
39 | TPM_UNDEFINED, | |
40 | }; | |
41 | ||
42 | #define TPM_MAX_ORDINAL 243 | |
07b133e6 PH |
43 | #define TSC_MAX_ORDINAL 12 |
44 | #define TPM_PROTECTED_COMMAND 0x00 | |
45 | #define TPM_CONNECTION_COMMAND 0x40 | |
9e18ee19 | 46 | |
9b3056cc DT |
47 | /* |
48 | * Bug workaround - some TPM's don't flush the most | |
49 | * recently changed pcr on suspend, so force the flush | |
50 | * with an extend to the selected _unused_ non-volatile pcr. | |
51 | */ | |
52 | static int tpm_suspend_pcr; | |
53 | module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644); | |
54 | MODULE_PARM_DESC(suspend_pcr, | |
55 | "PCR to use for dummy writes to faciltate flush on suspend."); | |
56 | ||
1da177e4 LT |
57 | static LIST_HEAD(tpm_chip_list); |
58 | static DEFINE_SPINLOCK(driver_lock); | |
10685a95 | 59 | static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES); |
1da177e4 | 60 | |
9e18ee19 KJH |
61 | /* |
62 | * Array with one entry per ordinal defining the maximum amount | |
63 | * of time the chip could take to return the result. The ordinal | |
64 | * designation of short, medium or long is defined in a table in | |
65 | * TCG Specification TPM Main Part 2 TPM Structures Section 17. The | |
66 | * values of the SHORT, MEDIUM, and LONG durations are retrieved | |
67 | * from the chip during initialization with a call to tpm_get_timeouts. | |
68 | */ | |
9e18ee19 KJH |
69 | static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = { |
70 | TPM_UNDEFINED, /* 0 */ | |
71 | TPM_UNDEFINED, | |
72 | TPM_UNDEFINED, | |
73 | TPM_UNDEFINED, | |
74 | TPM_UNDEFINED, | |
75 | TPM_UNDEFINED, /* 5 */ | |
76 | TPM_UNDEFINED, | |
77 | TPM_UNDEFINED, | |
78 | TPM_UNDEFINED, | |
79 | TPM_UNDEFINED, | |
80 | TPM_SHORT, /* 10 */ | |
81 | TPM_SHORT, | |
82 | TPM_MEDIUM, | |
83 | TPM_LONG, | |
84 | TPM_LONG, | |
85 | TPM_MEDIUM, /* 15 */ | |
86 | TPM_SHORT, | |
87 | TPM_SHORT, | |
88 | TPM_MEDIUM, | |
89 | TPM_LONG, | |
90 | TPM_SHORT, /* 20 */ | |
91 | TPM_SHORT, | |
92 | TPM_MEDIUM, | |
93 | TPM_MEDIUM, | |
94 | TPM_MEDIUM, | |
95 | TPM_SHORT, /* 25 */ | |
96 | TPM_SHORT, | |
97 | TPM_MEDIUM, | |
98 | TPM_SHORT, | |
99 | TPM_SHORT, | |
100 | TPM_MEDIUM, /* 30 */ | |
101 | TPM_LONG, | |
102 | TPM_MEDIUM, | |
103 | TPM_SHORT, | |
104 | TPM_SHORT, | |
105 | TPM_SHORT, /* 35 */ | |
106 | TPM_MEDIUM, | |
107 | TPM_MEDIUM, | |
108 | TPM_UNDEFINED, | |
109 | TPM_UNDEFINED, | |
110 | TPM_MEDIUM, /* 40 */ | |
111 | TPM_LONG, | |
112 | TPM_MEDIUM, | |
113 | TPM_SHORT, | |
114 | TPM_SHORT, | |
115 | TPM_SHORT, /* 45 */ | |
116 | TPM_SHORT, | |
117 | TPM_SHORT, | |
118 | TPM_SHORT, | |
119 | TPM_LONG, | |
120 | TPM_MEDIUM, /* 50 */ | |
121 | TPM_MEDIUM, | |
122 | TPM_UNDEFINED, | |
123 | TPM_UNDEFINED, | |
124 | TPM_UNDEFINED, | |
125 | TPM_UNDEFINED, /* 55 */ | |
126 | TPM_UNDEFINED, | |
127 | TPM_UNDEFINED, | |
128 | TPM_UNDEFINED, | |
129 | TPM_UNDEFINED, | |
130 | TPM_MEDIUM, /* 60 */ | |
131 | TPM_MEDIUM, | |
132 | TPM_MEDIUM, | |
133 | TPM_SHORT, | |
134 | TPM_SHORT, | |
135 | TPM_MEDIUM, /* 65 */ | |
136 | TPM_UNDEFINED, | |
137 | TPM_UNDEFINED, | |
138 | TPM_UNDEFINED, | |
139 | TPM_UNDEFINED, | |
140 | TPM_SHORT, /* 70 */ | |
141 | TPM_SHORT, | |
142 | TPM_UNDEFINED, | |
143 | TPM_UNDEFINED, | |
144 | TPM_UNDEFINED, | |
145 | TPM_UNDEFINED, /* 75 */ | |
146 | TPM_UNDEFINED, | |
147 | TPM_UNDEFINED, | |
148 | TPM_UNDEFINED, | |
149 | TPM_UNDEFINED, | |
150 | TPM_LONG, /* 80 */ | |
151 | TPM_UNDEFINED, | |
152 | TPM_MEDIUM, | |
153 | TPM_LONG, | |
154 | TPM_SHORT, | |
155 | TPM_UNDEFINED, /* 85 */ | |
156 | TPM_UNDEFINED, | |
157 | TPM_UNDEFINED, | |
158 | TPM_UNDEFINED, | |
159 | TPM_UNDEFINED, | |
160 | TPM_SHORT, /* 90 */ | |
161 | TPM_SHORT, | |
162 | TPM_SHORT, | |
163 | TPM_SHORT, | |
164 | TPM_SHORT, | |
165 | TPM_UNDEFINED, /* 95 */ | |
166 | TPM_UNDEFINED, | |
167 | TPM_UNDEFINED, | |
168 | TPM_UNDEFINED, | |
169 | TPM_UNDEFINED, | |
170 | TPM_MEDIUM, /* 100 */ | |
171 | TPM_SHORT, | |
172 | TPM_SHORT, | |
173 | TPM_UNDEFINED, | |
174 | TPM_UNDEFINED, | |
175 | TPM_UNDEFINED, /* 105 */ | |
176 | TPM_UNDEFINED, | |
177 | TPM_UNDEFINED, | |
178 | TPM_UNDEFINED, | |
179 | TPM_UNDEFINED, | |
180 | TPM_SHORT, /* 110 */ | |
181 | TPM_SHORT, | |
182 | TPM_SHORT, | |
183 | TPM_SHORT, | |
184 | TPM_SHORT, | |
185 | TPM_SHORT, /* 115 */ | |
186 | TPM_SHORT, | |
187 | TPM_SHORT, | |
188 | TPM_UNDEFINED, | |
189 | TPM_UNDEFINED, | |
190 | TPM_LONG, /* 120 */ | |
191 | TPM_LONG, | |
192 | TPM_MEDIUM, | |
193 | TPM_UNDEFINED, | |
194 | TPM_SHORT, | |
195 | TPM_SHORT, /* 125 */ | |
196 | TPM_SHORT, | |
197 | TPM_LONG, | |
198 | TPM_SHORT, | |
199 | TPM_SHORT, | |
200 | TPM_SHORT, /* 130 */ | |
201 | TPM_MEDIUM, | |
202 | TPM_UNDEFINED, | |
203 | TPM_SHORT, | |
204 | TPM_MEDIUM, | |
205 | TPM_UNDEFINED, /* 135 */ | |
206 | TPM_UNDEFINED, | |
207 | TPM_UNDEFINED, | |
208 | TPM_UNDEFINED, | |
209 | TPM_UNDEFINED, | |
210 | TPM_SHORT, /* 140 */ | |
211 | TPM_SHORT, | |
212 | TPM_UNDEFINED, | |
213 | TPM_UNDEFINED, | |
214 | TPM_UNDEFINED, | |
215 | TPM_UNDEFINED, /* 145 */ | |
216 | TPM_UNDEFINED, | |
217 | TPM_UNDEFINED, | |
218 | TPM_UNDEFINED, | |
219 | TPM_UNDEFINED, | |
220 | TPM_SHORT, /* 150 */ | |
221 | TPM_MEDIUM, | |
222 | TPM_MEDIUM, | |
223 | TPM_SHORT, | |
224 | TPM_SHORT, | |
225 | TPM_UNDEFINED, /* 155 */ | |
226 | TPM_UNDEFINED, | |
227 | TPM_UNDEFINED, | |
228 | TPM_UNDEFINED, | |
229 | TPM_UNDEFINED, | |
230 | TPM_SHORT, /* 160 */ | |
231 | TPM_SHORT, | |
232 | TPM_SHORT, | |
233 | TPM_SHORT, | |
234 | TPM_UNDEFINED, | |
235 | TPM_UNDEFINED, /* 165 */ | |
236 | TPM_UNDEFINED, | |
237 | TPM_UNDEFINED, | |
238 | TPM_UNDEFINED, | |
239 | TPM_UNDEFINED, | |
240 | TPM_LONG, /* 170 */ | |
241 | TPM_UNDEFINED, | |
242 | TPM_UNDEFINED, | |
243 | TPM_UNDEFINED, | |
244 | TPM_UNDEFINED, | |
245 | TPM_UNDEFINED, /* 175 */ | |
246 | TPM_UNDEFINED, | |
247 | TPM_UNDEFINED, | |
248 | TPM_UNDEFINED, | |
249 | TPM_UNDEFINED, | |
250 | TPM_MEDIUM, /* 180 */ | |
251 | TPM_SHORT, | |
252 | TPM_MEDIUM, | |
253 | TPM_MEDIUM, | |
254 | TPM_MEDIUM, | |
255 | TPM_MEDIUM, /* 185 */ | |
256 | TPM_SHORT, | |
257 | TPM_UNDEFINED, | |
258 | TPM_UNDEFINED, | |
259 | TPM_UNDEFINED, | |
260 | TPM_UNDEFINED, /* 190 */ | |
261 | TPM_UNDEFINED, | |
262 | TPM_UNDEFINED, | |
263 | TPM_UNDEFINED, | |
264 | TPM_UNDEFINED, | |
265 | TPM_UNDEFINED, /* 195 */ | |
266 | TPM_UNDEFINED, | |
267 | TPM_UNDEFINED, | |
268 | TPM_UNDEFINED, | |
269 | TPM_UNDEFINED, | |
270 | TPM_SHORT, /* 200 */ | |
271 | TPM_UNDEFINED, | |
272 | TPM_UNDEFINED, | |
273 | TPM_UNDEFINED, | |
274 | TPM_SHORT, | |
275 | TPM_SHORT, /* 205 */ | |
276 | TPM_SHORT, | |
277 | TPM_SHORT, | |
278 | TPM_SHORT, | |
279 | TPM_SHORT, | |
280 | TPM_MEDIUM, /* 210 */ | |
281 | TPM_UNDEFINED, | |
282 | TPM_MEDIUM, | |
283 | TPM_MEDIUM, | |
284 | TPM_MEDIUM, | |
285 | TPM_UNDEFINED, /* 215 */ | |
286 | TPM_MEDIUM, | |
287 | TPM_UNDEFINED, | |
288 | TPM_UNDEFINED, | |
289 | TPM_SHORT, | |
290 | TPM_SHORT, /* 220 */ | |
291 | TPM_SHORT, | |
292 | TPM_SHORT, | |
293 | TPM_SHORT, | |
294 | TPM_SHORT, | |
295 | TPM_UNDEFINED, /* 225 */ | |
296 | TPM_UNDEFINED, | |
297 | TPM_UNDEFINED, | |
298 | TPM_UNDEFINED, | |
299 | TPM_UNDEFINED, | |
300 | TPM_SHORT, /* 230 */ | |
301 | TPM_LONG, | |
302 | TPM_MEDIUM, | |
303 | TPM_UNDEFINED, | |
304 | TPM_UNDEFINED, | |
305 | TPM_UNDEFINED, /* 235 */ | |
306 | TPM_UNDEFINED, | |
307 | TPM_UNDEFINED, | |
308 | TPM_UNDEFINED, | |
309 | TPM_UNDEFINED, | |
310 | TPM_SHORT, /* 240 */ | |
311 | TPM_UNDEFINED, | |
312 | TPM_MEDIUM, | |
313 | }; | |
314 | ||
1da177e4 LT |
315 | static void user_reader_timeout(unsigned long ptr) |
316 | { | |
317 | struct tpm_chip *chip = (struct tpm_chip *) ptr; | |
318 | ||
09e12f9f KJH |
319 | schedule_work(&chip->work); |
320 | } | |
321 | ||
c4028958 | 322 | static void timeout_work(struct work_struct *work) |
09e12f9f | 323 | { |
c4028958 | 324 | struct tpm_chip *chip = container_of(work, struct tpm_chip, work); |
09e12f9f | 325 | |
d081d470 | 326 | mutex_lock(&chip->buffer_mutex); |
1da177e4 LT |
327 | atomic_set(&chip->data_pending, 0); |
328 | memset(chip->data_buffer, 0, TPM_BUFSIZE); | |
d081d470 | 329 | mutex_unlock(&chip->buffer_mutex); |
1da177e4 LT |
330 | } |
331 | ||
9e18ee19 KJH |
332 | /* |
333 | * Returns max number of jiffies to wait | |
334 | */ | |
335 | unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, | |
336 | u32 ordinal) | |
337 | { | |
338 | int duration_idx = TPM_UNDEFINED; | |
339 | int duration = 0; | |
07b133e6 | 340 | u8 category = (ordinal >> 24) & 0xFF; |
9e18ee19 | 341 | |
07b133e6 PH |
342 | if ((category == TPM_PROTECTED_COMMAND && ordinal < TPM_MAX_ORDINAL) || |
343 | (category == TPM_CONNECTION_COMMAND && ordinal < TSC_MAX_ORDINAL)) | |
9e18ee19 | 344 | duration_idx = tpm_ordinal_duration[ordinal]; |
9e18ee19 | 345 | |
8d1dc20e | 346 | if (duration_idx != TPM_UNDEFINED) |
36b20020 | 347 | duration = chip->vendor.duration[duration_idx]; |
8d1dc20e | 348 | if (duration <= 0) |
9e18ee19 | 349 | return 2 * 60 * HZ; |
8d1dc20e LT |
350 | else |
351 | return duration; | |
9e18ee19 KJH |
352 | } |
353 | EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration); | |
354 | ||
1da177e4 LT |
355 | /* |
356 | * Internal kernel interface to transmit TPM commands | |
357 | */ | |
358 | static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf, | |
359 | size_t bufsiz) | |
360 | { | |
d9e5b6bf | 361 | ssize_t rc; |
9e18ee19 | 362 | u32 count, ordinal; |
700d8bdc | 363 | unsigned long stop; |
1da177e4 | 364 | |
6b07d30a PH |
365 | if (bufsiz > TPM_BUFSIZE) |
366 | bufsiz = TPM_BUFSIZE; | |
367 | ||
81179bb6 | 368 | count = be32_to_cpu(*((__be32 *) (buf + 2))); |
9e18ee19 | 369 | ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); |
1da177e4 LT |
370 | if (count == 0) |
371 | return -ENODATA; | |
372 | if (count > bufsiz) { | |
e659a3fe | 373 | dev_err(chip->dev, |
b76be681 | 374 | "invalid count value %x %zx \n", count, bufsiz); |
1da177e4 LT |
375 | return -E2BIG; |
376 | } | |
377 | ||
d081d470 | 378 | mutex_lock(&chip->tpm_mutex); |
1da177e4 | 379 | |
90dda520 | 380 | if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) { |
e659a3fe | 381 | dev_err(chip->dev, |
d9e5b6bf KH |
382 | "tpm_transmit: tpm_send: error %zd\n", rc); |
383 | goto out; | |
1da177e4 LT |
384 | } |
385 | ||
27084efe LD |
386 | if (chip->vendor.irq) |
387 | goto out_recv; | |
388 | ||
9e18ee19 | 389 | stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal); |
1da177e4 | 390 | do { |
90dda520 KJH |
391 | u8 status = chip->vendor.status(chip); |
392 | if ((status & chip->vendor.req_complete_mask) == | |
393 | chip->vendor.req_complete_val) | |
1da177e4 | 394 | goto out_recv; |
d9e5b6bf | 395 | |
1f866057 | 396 | if (chip->vendor.req_canceled(chip, status)) { |
e659a3fe | 397 | dev_err(chip->dev, "Operation Canceled\n"); |
d9e5b6bf KH |
398 | rc = -ECANCELED; |
399 | goto out; | |
400 | } | |
401 | ||
402 | msleep(TPM_TIMEOUT); /* CHECK */ | |
1da177e4 | 403 | rmb(); |
700d8bdc | 404 | } while (time_before(jiffies, stop)); |
1da177e4 | 405 | |
90dda520 | 406 | chip->vendor.cancel(chip); |
e659a3fe | 407 | dev_err(chip->dev, "Operation Timed out\n"); |
d9e5b6bf KH |
408 | rc = -ETIME; |
409 | goto out; | |
1da177e4 LT |
410 | |
411 | out_recv: | |
90dda520 | 412 | rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz); |
d9e5b6bf | 413 | if (rc < 0) |
e659a3fe | 414 | dev_err(chip->dev, |
d9e5b6bf KH |
415 | "tpm_transmit: tpm_recv: error %zd\n", rc); |
416 | out: | |
d081d470 | 417 | mutex_unlock(&chip->tpm_mutex); |
d9e5b6bf | 418 | return rc; |
1da177e4 LT |
419 | } |
420 | ||
421 | #define TPM_DIGEST_SIZE 20 | |
beed53a1 | 422 | #define TPM_RET_CODE_IDX 6 |
beed53a1 KJH |
423 | |
424 | enum tpm_capabilities { | |
08837438 RA |
425 | TPM_CAP_FLAG = cpu_to_be32(4), |
426 | TPM_CAP_PROP = cpu_to_be32(5), | |
427 | CAP_VERSION_1_1 = cpu_to_be32(0x06), | |
428 | CAP_VERSION_1_2 = cpu_to_be32(0x1A) | |
beed53a1 KJH |
429 | }; |
430 | ||
431 | enum tpm_sub_capabilities { | |
08837438 RA |
432 | TPM_CAP_PROP_PCR = cpu_to_be32(0x101), |
433 | TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103), | |
434 | TPM_CAP_FLAG_PERM = cpu_to_be32(0x108), | |
435 | TPM_CAP_FLAG_VOL = cpu_to_be32(0x109), | |
436 | TPM_CAP_PROP_OWNER = cpu_to_be32(0x111), | |
437 | TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115), | |
438 | TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120), | |
beed53a1 | 439 | |
1da177e4 LT |
440 | }; |
441 | ||
08837438 RA |
442 | static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd, |
443 | int len, const char *desc) | |
beed53a1 KJH |
444 | { |
445 | int err; | |
446 | ||
08837438 | 447 | len = tpm_transmit(chip,(u8 *) cmd, len); |
beed53a1 KJH |
448 | if (len < 0) |
449 | return len; | |
b9e3238a RA |
450 | else if (len < TPM_HEADER_SIZE) |
451 | return -EFAULT; | |
452 | ||
453 | err = be32_to_cpu(cmd->header.out.return_code); | |
c584af19 | 454 | if (err != 0 && desc) |
b9e3238a RA |
455 | dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc); |
456 | ||
457 | return err; | |
beed53a1 KJH |
458 | } |
459 | ||
08837438 RA |
460 | #define TPM_INTERNAL_RESULT_SIZE 200 |
461 | #define TPM_TAG_RQU_COMMAND cpu_to_be16(193) | |
462 | #define TPM_ORD_GET_CAP cpu_to_be32(101) | |
41ab999c | 463 | #define TPM_ORD_GET_RANDOM cpu_to_be32(70) |
08837438 RA |
464 | |
465 | static const struct tpm_input_header tpm_getcap_header = { | |
466 | .tag = TPM_TAG_RQU_COMMAND, | |
467 | .length = cpu_to_be32(22), | |
468 | .ordinal = TPM_ORD_GET_CAP | |
469 | }; | |
470 | ||
471 | ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap, | |
472 | const char *desc) | |
473 | { | |
474 | struct tpm_cmd_t tpm_cmd; | |
475 | int rc; | |
476 | struct tpm_chip *chip = dev_get_drvdata(dev); | |
477 | ||
478 | tpm_cmd.header.in = tpm_getcap_header; | |
479 | if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) { | |
480 | tpm_cmd.params.getcap_in.cap = subcap_id; | |
481 | /*subcap field not necessary */ | |
482 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0); | |
483 | tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32)); | |
484 | } else { | |
485 | if (subcap_id == TPM_CAP_FLAG_PERM || | |
486 | subcap_id == TPM_CAP_FLAG_VOL) | |
487 | tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG; | |
488 | else | |
489 | tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; | |
490 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); | |
491 | tpm_cmd.params.getcap_in.subcap = subcap_id; | |
492 | } | |
493 | rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc); | |
494 | if (!rc) | |
495 | *cap = tpm_cmd.params.getcap_out.cap; | |
496 | return rc; | |
497 | } | |
498 | ||
08e96e48 KJH |
499 | void tpm_gen_interrupt(struct tpm_chip *chip) |
500 | { | |
08837438 | 501 | struct tpm_cmd_t tpm_cmd; |
08e96e48 KJH |
502 | ssize_t rc; |
503 | ||
08837438 RA |
504 | tpm_cmd.header.in = tpm_getcap_header; |
505 | tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; | |
506 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); | |
507 | tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; | |
08e96e48 | 508 | |
08837438 | 509 | rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, |
08e96e48 KJH |
510 | "attempting to determine the timeouts"); |
511 | } | |
512 | EXPORT_SYMBOL_GPL(tpm_gen_interrupt); | |
513 | ||
c584af19 JG |
514 | #define TPM_ORD_STARTUP cpu_to_be32(153) |
515 | #define TPM_ST_CLEAR cpu_to_be16(1) | |
516 | #define TPM_ST_STATE cpu_to_be16(2) | |
517 | #define TPM_ST_DEACTIVATED cpu_to_be16(3) | |
518 | static const struct tpm_input_header tpm_startup_header = { | |
519 | .tag = TPM_TAG_RQU_COMMAND, | |
520 | .length = cpu_to_be32(12), | |
521 | .ordinal = TPM_ORD_STARTUP | |
522 | }; | |
523 | ||
524 | static int tpm_startup(struct tpm_chip *chip, __be16 startup_type) | |
525 | { | |
526 | struct tpm_cmd_t start_cmd; | |
527 | start_cmd.header.in = tpm_startup_header; | |
528 | start_cmd.params.startup_in.startup_type = startup_type; | |
529 | return transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE, | |
530 | "attempting to start the TPM"); | |
531 | } | |
532 | ||
2b30a90f | 533 | int tpm_get_timeouts(struct tpm_chip *chip) |
08e96e48 | 534 | { |
08837438 | 535 | struct tpm_cmd_t tpm_cmd; |
d64269e3 JG |
536 | unsigned long new_timeout[4]; |
537 | unsigned long old_timeout[4]; | |
08837438 | 538 | struct duration_t *duration_cap; |
08e96e48 | 539 | ssize_t rc; |
08e96e48 | 540 | |
08837438 RA |
541 | tpm_cmd.header.in = tpm_getcap_header; |
542 | tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; | |
543 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); | |
544 | tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; | |
c584af19 | 545 | rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, NULL); |
08e96e48 | 546 | |
c584af19 JG |
547 | if (rc == TPM_ERR_INVALID_POSTINIT) { |
548 | /* The TPM is not started, we are the first to talk to it. | |
549 | Execute a startup command. */ | |
550 | dev_info(chip->dev, "Issuing TPM_STARTUP"); | |
551 | if (tpm_startup(chip, TPM_ST_CLEAR)) | |
552 | return rc; | |
553 | ||
554 | tpm_cmd.header.in = tpm_getcap_header; | |
555 | tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; | |
556 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); | |
557 | tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT; | |
558 | rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, | |
559 | NULL); | |
560 | } | |
561 | if (rc) { | |
562 | dev_err(chip->dev, | |
563 | "A TPM error (%zd) occurred attempting to determine the timeouts\n", | |
564 | rc); | |
08e96e48 | 565 | goto duration; |
c584af19 | 566 | } |
08e96e48 | 567 | |
829bf067 SB |
568 | if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 || |
569 | be32_to_cpu(tpm_cmd.header.out.length) | |
570 | != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32)) | |
2b30a90f | 571 | return -EINVAL; |
08e96e48 | 572 | |
d64269e3 JG |
573 | old_timeout[0] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.a); |
574 | old_timeout[1] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.b); | |
575 | old_timeout[2] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.c); | |
576 | old_timeout[3] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.d); | |
577 | memcpy(new_timeout, old_timeout, sizeof(new_timeout)); | |
578 | ||
579 | /* | |
580 | * Provide ability for vendor overrides of timeout values in case | |
581 | * of misreporting. | |
582 | */ | |
583 | if (chip->vendor.update_timeouts != NULL) | |
584 | chip->vendor.timeout_adjusted = | |
585 | chip->vendor.update_timeouts(chip, new_timeout); | |
586 | ||
587 | if (!chip->vendor.timeout_adjusted) { | |
588 | /* Don't overwrite default if value is 0 */ | |
589 | if (new_timeout[0] != 0 && new_timeout[0] < 1000) { | |
590 | int i; | |
591 | ||
592 | /* timeouts in msec rather usec */ | |
593 | for (i = 0; i != ARRAY_SIZE(new_timeout); i++) | |
594 | new_timeout[i] *= 1000; | |
595 | chip->vendor.timeout_adjusted = true; | |
596 | } | |
597 | } | |
598 | ||
599 | /* Report adjusted timeouts */ | |
600 | if (chip->vendor.timeout_adjusted) { | |
601 | dev_info(chip->dev, | |
602 | HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n", | |
603 | old_timeout[0], new_timeout[0], | |
604 | old_timeout[1], new_timeout[1], | |
605 | old_timeout[2], new_timeout[2], | |
606 | old_timeout[3], new_timeout[3]); | |
e3e1a1e1 | 607 | } |
d64269e3 JG |
608 | |
609 | chip->vendor.timeout_a = usecs_to_jiffies(new_timeout[0]); | |
610 | chip->vendor.timeout_b = usecs_to_jiffies(new_timeout[1]); | |
611 | chip->vendor.timeout_c = usecs_to_jiffies(new_timeout[2]); | |
612 | chip->vendor.timeout_d = usecs_to_jiffies(new_timeout[3]); | |
08e96e48 KJH |
613 | |
614 | duration: | |
08837438 RA |
615 | tpm_cmd.header.in = tpm_getcap_header; |
616 | tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP; | |
617 | tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4); | |
618 | tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION; | |
08e96e48 | 619 | |
08837438 | 620 | rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, |
08e96e48 KJH |
621 | "attempting to determine the durations"); |
622 | if (rc) | |
2b30a90f | 623 | return rc; |
08e96e48 | 624 | |
979b1406 SB |
625 | if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 || |
626 | be32_to_cpu(tpm_cmd.header.out.length) | |
627 | != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32)) | |
2b30a90f | 628 | return -EINVAL; |
979b1406 | 629 | |
08837438 | 630 | duration_cap = &tpm_cmd.params.getcap_out.cap.duration; |
08e96e48 | 631 | chip->vendor.duration[TPM_SHORT] = |
08837438 | 632 | usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short)); |
e934acca SB |
633 | chip->vendor.duration[TPM_MEDIUM] = |
634 | usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium)); | |
635 | chip->vendor.duration[TPM_LONG] = | |
636 | usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long)); | |
637 | ||
292cf4a8 VK |
638 | /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above |
639 | * value wrong and apparently reports msecs rather than usecs. So we | |
640 | * fix up the resulting too-small TPM_SHORT value to make things work. | |
e934acca | 641 | * We also scale the TPM_MEDIUM and -_LONG values by 1000. |
292cf4a8 | 642 | */ |
e934acca | 643 | if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) { |
292cf4a8 | 644 | chip->vendor.duration[TPM_SHORT] = HZ; |
e934acca SB |
645 | chip->vendor.duration[TPM_MEDIUM] *= 1000; |
646 | chip->vendor.duration[TPM_LONG] *= 1000; | |
04ab2293 | 647 | chip->vendor.duration_adjusted = true; |
e934acca SB |
648 | dev_info(chip->dev, "Adjusting TPM timeout parameters."); |
649 | } | |
2b30a90f | 650 | return 0; |
08e96e48 KJH |
651 | } |
652 | EXPORT_SYMBOL_GPL(tpm_get_timeouts); | |
653 | ||
d97c6ade SB |
654 | #define TPM_ORD_CONTINUE_SELFTEST 83 |
655 | #define CONTINUE_SELFTEST_RESULT_SIZE 10 | |
656 | ||
657 | static struct tpm_input_header continue_selftest_header = { | |
658 | .tag = TPM_TAG_RQU_COMMAND, | |
659 | .length = cpu_to_be32(10), | |
660 | .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST), | |
661 | }; | |
662 | ||
663 | /** | |
664 | * tpm_continue_selftest -- run TPM's selftest | |
665 | * @chip: TPM chip to use | |
666 | * | |
667 | * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing | |
668 | * a TPM error code. | |
669 | */ | |
68d6e671 | 670 | static int tpm_continue_selftest(struct tpm_chip *chip) |
08e96e48 | 671 | { |
d97c6ade SB |
672 | int rc; |
673 | struct tpm_cmd_t cmd; | |
08e96e48 | 674 | |
d97c6ade SB |
675 | cmd.header.in = continue_selftest_header; |
676 | rc = transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE, | |
677 | "continue selftest"); | |
678 | return rc; | |
08e96e48 | 679 | } |
08e96e48 KJH |
680 | |
681 | ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr, | |
682 | char *buf) | |
683 | { | |
08837438 | 684 | cap_t cap; |
08e96e48 KJH |
685 | ssize_t rc; |
686 | ||
08837438 RA |
687 | rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap, |
688 | "attempting to determine the permanent enabled state"); | |
689 | if (rc) | |
08e96e48 | 690 | return 0; |
ec288bd3 | 691 | |
08837438 | 692 | rc = sprintf(buf, "%d\n", !cap.perm_flags.disable); |
ec288bd3 | 693 | return rc; |
08e96e48 KJH |
694 | } |
695 | EXPORT_SYMBOL_GPL(tpm_show_enabled); | |
696 | ||
697 | ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr, | |
698 | char *buf) | |
699 | { | |
08837438 | 700 | cap_t cap; |
08e96e48 KJH |
701 | ssize_t rc; |
702 | ||
08837438 RA |
703 | rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap, |
704 | "attempting to determine the permanent active state"); | |
705 | if (rc) | |
08e96e48 | 706 | return 0; |
ec288bd3 | 707 | |
08837438 | 708 | rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated); |
ec288bd3 | 709 | return rc; |
08e96e48 KJH |
710 | } |
711 | EXPORT_SYMBOL_GPL(tpm_show_active); | |
712 | ||
713 | ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr, | |
714 | char *buf) | |
715 | { | |
08837438 | 716 | cap_t cap; |
08e96e48 KJH |
717 | ssize_t rc; |
718 | ||
08837438 RA |
719 | rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap, |
720 | "attempting to determine the owner state"); | |
721 | if (rc) | |
08e96e48 | 722 | return 0; |
ec288bd3 | 723 | |
08837438 | 724 | rc = sprintf(buf, "%d\n", cap.owned); |
ec288bd3 | 725 | return rc; |
08e96e48 KJH |
726 | } |
727 | EXPORT_SYMBOL_GPL(tpm_show_owned); | |
728 | ||
729 | ssize_t tpm_show_temp_deactivated(struct device * dev, | |
730 | struct device_attribute * attr, char *buf) | |
731 | { | |
08837438 | 732 | cap_t cap; |
08e96e48 KJH |
733 | ssize_t rc; |
734 | ||
08837438 RA |
735 | rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap, |
736 | "attempting to determine the temporary state"); | |
737 | if (rc) | |
08e96e48 | 738 | return 0; |
ec288bd3 | 739 | |
08837438 | 740 | rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated); |
ec288bd3 | 741 | return rc; |
08e96e48 KJH |
742 | } |
743 | EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated); | |
744 | ||
659aaf2b RA |
745 | /* |
746 | * tpm_chip_find_get - return tpm_chip for given chip number | |
747 | */ | |
748 | static struct tpm_chip *tpm_chip_find_get(int chip_num) | |
749 | { | |
8920d5ad | 750 | struct tpm_chip *pos, *chip = NULL; |
659aaf2b RA |
751 | |
752 | rcu_read_lock(); | |
753 | list_for_each_entry_rcu(pos, &tpm_chip_list, list) { | |
754 | if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num) | |
755 | continue; | |
756 | ||
8920d5ad RA |
757 | if (try_module_get(pos->dev->driver->owner)) { |
758 | chip = pos; | |
659aaf2b | 759 | break; |
8920d5ad | 760 | } |
659aaf2b RA |
761 | } |
762 | rcu_read_unlock(); | |
8920d5ad | 763 | return chip; |
659aaf2b RA |
764 | } |
765 | ||
766 | #define TPM_ORDINAL_PCRREAD cpu_to_be32(21) | |
767 | #define READ_PCR_RESULT_SIZE 30 | |
768 | static struct tpm_input_header pcrread_header = { | |
769 | .tag = TPM_TAG_RQU_COMMAND, | |
770 | .length = cpu_to_be32(14), | |
771 | .ordinal = TPM_ORDINAL_PCRREAD | |
1da177e4 LT |
772 | }; |
773 | ||
68d6e671 | 774 | static int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) |
659aaf2b RA |
775 | { |
776 | int rc; | |
777 | struct tpm_cmd_t cmd; | |
778 | ||
779 | cmd.header.in = pcrread_header; | |
780 | cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx); | |
23acb98d | 781 | rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE, |
659aaf2b RA |
782 | "attempting to read a pcr value"); |
783 | ||
784 | if (rc == 0) | |
785 | memcpy(res_buf, cmd.params.pcrread_out.pcr_result, | |
786 | TPM_DIGEST_SIZE); | |
787 | return rc; | |
788 | } | |
789 | ||
790 | /** | |
791 | * tpm_pcr_read - read a pcr value | |
792 | * @chip_num: tpm idx # or ANY | |
793 | * @pcr_idx: pcr idx to retrieve | |
794 | * @res_buf: TPM_PCR value | |
795 | * size of res_buf is 20 bytes (or NULL if you don't care) | |
796 | * | |
797 | * The TPM driver should be built-in, but for whatever reason it | |
798 | * isn't, protect against the chip disappearing, by incrementing | |
799 | * the module usage count. | |
800 | */ | |
801 | int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf) | |
802 | { | |
803 | struct tpm_chip *chip; | |
804 | int rc; | |
805 | ||
806 | chip = tpm_chip_find_get(chip_num); | |
807 | if (chip == NULL) | |
808 | return -ENODEV; | |
809 | rc = __tpm_pcr_read(chip, pcr_idx, res_buf); | |
a0e39349 | 810 | tpm_chip_put(chip); |
659aaf2b RA |
811 | return rc; |
812 | } | |
813 | EXPORT_SYMBOL_GPL(tpm_pcr_read); | |
814 | ||
815 | /** | |
816 | * tpm_pcr_extend - extend pcr value with hash | |
817 | * @chip_num: tpm idx # or AN& | |
818 | * @pcr_idx: pcr idx to extend | |
819 | * @hash: hash value used to extend pcr value | |
820 | * | |
821 | * The TPM driver should be built-in, but for whatever reason it | |
822 | * isn't, protect against the chip disappearing, by incrementing | |
823 | * the module usage count. | |
824 | */ | |
825 | #define TPM_ORD_PCR_EXTEND cpu_to_be32(20) | |
0afd9056 | 826 | #define EXTEND_PCR_RESULT_SIZE 34 |
659aaf2b RA |
827 | static struct tpm_input_header pcrextend_header = { |
828 | .tag = TPM_TAG_RQU_COMMAND, | |
829 | .length = cpu_to_be32(34), | |
830 | .ordinal = TPM_ORD_PCR_EXTEND | |
831 | }; | |
832 | ||
833 | int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash) | |
834 | { | |
835 | struct tpm_cmd_t cmd; | |
836 | int rc; | |
837 | struct tpm_chip *chip; | |
838 | ||
839 | chip = tpm_chip_find_get(chip_num); | |
840 | if (chip == NULL) | |
841 | return -ENODEV; | |
842 | ||
843 | cmd.header.in = pcrextend_header; | |
659aaf2b RA |
844 | cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx); |
845 | memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE); | |
0afd9056 | 846 | rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, |
659aaf2b RA |
847 | "attempting extend a PCR value"); |
848 | ||
a0e39349 | 849 | tpm_chip_put(chip); |
659aaf2b RA |
850 | return rc; |
851 | } | |
852 | EXPORT_SYMBOL_GPL(tpm_pcr_extend); | |
853 | ||
68d6e671 SB |
854 | /** |
855 | * tpm_do_selftest - have the TPM continue its selftest and wait until it | |
856 | * can receive further commands | |
857 | * @chip: TPM chip to use | |
858 | * | |
859 | * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing | |
860 | * a TPM error code. | |
861 | */ | |
862 | int tpm_do_selftest(struct tpm_chip *chip) | |
863 | { | |
864 | int rc; | |
68d6e671 | 865 | unsigned int loops; |
4643826a | 866 | unsigned int delay_msec = 100; |
68d6e671 | 867 | unsigned long duration; |
24ebe667 | 868 | struct tpm_cmd_t cmd; |
68d6e671 SB |
869 | |
870 | duration = tpm_calc_ordinal_duration(chip, | |
871 | TPM_ORD_CONTINUE_SELFTEST); | |
872 | ||
873 | loops = jiffies_to_msecs(duration) / delay_msec; | |
874 | ||
875 | rc = tpm_continue_selftest(chip); | |
876 | /* This may fail if there was no TPM driver during a suspend/resume | |
877 | * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST) | |
878 | */ | |
879 | if (rc) | |
880 | return rc; | |
881 | ||
882 | do { | |
24ebe667 RA |
883 | /* Attempt to read a PCR value */ |
884 | cmd.header.in = pcrread_header; | |
885 | cmd.params.pcrread_in.pcr_idx = cpu_to_be32(0); | |
886 | rc = tpm_transmit(chip, (u8 *) &cmd, READ_PCR_RESULT_SIZE); | |
4643826a JG |
887 | /* Some buggy TPMs will not respond to tpm_tis_ready() for |
888 | * around 300ms while the self test is ongoing, keep trying | |
889 | * until the self test duration expires. */ | |
890 | if (rc == -ETIME) { | |
891 | dev_info(chip->dev, HW_ERR "TPM command timed out during continue self test"); | |
892 | msleep(delay_msec); | |
893 | continue; | |
894 | } | |
24ebe667 RA |
895 | |
896 | if (rc < TPM_HEADER_SIZE) | |
897 | return -EFAULT; | |
898 | ||
899 | rc = be32_to_cpu(cmd.header.out.return_code); | |
be405411 SB |
900 | if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) { |
901 | dev_info(chip->dev, | |
902 | "TPM is disabled/deactivated (0x%X)\n", rc); | |
903 | /* TPM is disabled and/or deactivated; driver can | |
904 | * proceed and TPM does handle commands for | |
905 | * suspend/resume correctly | |
906 | */ | |
907 | return 0; | |
908 | } | |
68d6e671 SB |
909 | if (rc != TPM_WARN_DOING_SELFTEST) |
910 | return rc; | |
911 | msleep(delay_msec); | |
912 | } while (--loops > 0); | |
913 | ||
914 | return rc; | |
915 | } | |
916 | EXPORT_SYMBOL_GPL(tpm_do_selftest); | |
917 | ||
c749ba91 MZ |
918 | int tpm_send(u32 chip_num, void *cmd, size_t buflen) |
919 | { | |
920 | struct tpm_chip *chip; | |
921 | int rc; | |
922 | ||
923 | chip = tpm_chip_find_get(chip_num); | |
924 | if (chip == NULL) | |
925 | return -ENODEV; | |
926 | ||
927 | rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd"); | |
928 | ||
929 | tpm_chip_put(chip); | |
930 | return rc; | |
931 | } | |
932 | EXPORT_SYMBOL_GPL(tpm_send); | |
933 | ||
6659ca2a KH |
934 | ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr, |
935 | char *buf) | |
1da177e4 | 936 | { |
08837438 | 937 | cap_t cap; |
659aaf2b | 938 | u8 digest[TPM_DIGEST_SIZE]; |
beed53a1 | 939 | ssize_t rc; |
81179bb6 | 940 | int i, j, num_pcrs; |
1da177e4 | 941 | char *str = buf; |
e659a3fe | 942 | struct tpm_chip *chip = dev_get_drvdata(dev); |
1da177e4 | 943 | |
08837438 | 944 | rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap, |
beed53a1 | 945 | "attempting to determine the number of PCRS"); |
08837438 | 946 | if (rc) |
e234bc97 | 947 | return 0; |
1da177e4 | 948 | |
08837438 | 949 | num_pcrs = be32_to_cpu(cap.num_pcrs); |
1da177e4 | 950 | for (i = 0; i < num_pcrs; i++) { |
659aaf2b | 951 | rc = __tpm_pcr_read(chip, i, digest); |
beed53a1 | 952 | if (rc) |
08837438 | 953 | break; |
1da177e4 LT |
954 | str += sprintf(str, "PCR-%02d: ", i); |
955 | for (j = 0; j < TPM_DIGEST_SIZE; j++) | |
659aaf2b | 956 | str += sprintf(str, "%02X ", digest[j]); |
1da177e4 LT |
957 | str += sprintf(str, "\n"); |
958 | } | |
959 | return str - buf; | |
960 | } | |
6659ca2a | 961 | EXPORT_SYMBOL_GPL(tpm_show_pcrs); |
1da177e4 LT |
962 | |
963 | #define READ_PUBEK_RESULT_SIZE 314 | |
08837438 | 964 | #define TPM_ORD_READPUBEK cpu_to_be32(124) |
81198078 | 965 | static struct tpm_input_header tpm_readpubek_header = { |
08837438 RA |
966 | .tag = TPM_TAG_RQU_COMMAND, |
967 | .length = cpu_to_be32(30), | |
968 | .ordinal = TPM_ORD_READPUBEK | |
1da177e4 LT |
969 | }; |
970 | ||
6659ca2a KH |
971 | ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr, |
972 | char *buf) | |
1da177e4 | 973 | { |
2df7111f | 974 | u8 *data; |
08837438 | 975 | struct tpm_cmd_t tpm_cmd; |
beed53a1 | 976 | ssize_t err; |
81179bb6 | 977 | int i, rc; |
1da177e4 LT |
978 | char *str = buf; |
979 | ||
e659a3fe | 980 | struct tpm_chip *chip = dev_get_drvdata(dev); |
1da177e4 | 981 | |
08837438 RA |
982 | tpm_cmd.header.in = tpm_readpubek_header; |
983 | err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE, | |
beed53a1 KJH |
984 | "attempting to read the PUBEK"); |
985 | if (err) | |
34d6e075 | 986 | goto out; |
1da177e4 LT |
987 | |
988 | /* | |
989 | ignore header 10 bytes | |
990 | algorithm 32 bits (1 == RSA ) | |
991 | encscheme 16 bits | |
992 | sigscheme 16 bits | |
993 | parameters (RSA 12->bytes: keybit, #primes, expbit) | |
994 | keylenbytes 32 bits | |
995 | 256 byte modulus | |
996 | ignore checksum 20 bytes | |
997 | */ | |
08837438 | 998 | data = tpm_cmd.params.readpubek_out_buffer; |
1da177e4 LT |
999 | str += |
1000 | sprintf(str, | |
5a79444f SB |
1001 | "Algorithm: %02X %02X %02X %02X\n" |
1002 | "Encscheme: %02X %02X\n" | |
1003 | "Sigscheme: %02X %02X\n" | |
1004 | "Parameters: %02X %02X %02X %02X " | |
1005 | "%02X %02X %02X %02X " | |
1006 | "%02X %02X %02X %02X\n" | |
1007 | "Modulus length: %d\n" | |
1008 | "Modulus:\n", | |
1009 | data[0], data[1], data[2], data[3], | |
1010 | data[4], data[5], | |
1011 | data[6], data[7], | |
1012 | data[12], data[13], data[14], data[15], | |
1013 | data[16], data[17], data[18], data[19], | |
1014 | data[20], data[21], data[22], data[23], | |
1015 | be32_to_cpu(*((__be32 *) (data + 24)))); | |
1da177e4 LT |
1016 | |
1017 | for (i = 0; i < 256; i++) { | |
5a79444f | 1018 | str += sprintf(str, "%02X ", data[i + 28]); |
1da177e4 LT |
1019 | if ((i + 1) % 16 == 0) |
1020 | str += sprintf(str, "\n"); | |
1021 | } | |
34d6e075 | 1022 | out: |
beed53a1 | 1023 | rc = str - buf; |
2df7111f | 1024 | return rc; |
1da177e4 | 1025 | } |
6659ca2a | 1026 | EXPORT_SYMBOL_GPL(tpm_show_pubek); |
1da177e4 | 1027 | |
1da177e4 | 1028 | |
6659ca2a KH |
1029 | ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr, |
1030 | char *buf) | |
1da177e4 | 1031 | { |
08837438 | 1032 | cap_t cap; |
beed53a1 | 1033 | ssize_t rc; |
1da177e4 LT |
1034 | char *str = buf; |
1035 | ||
08837438 | 1036 | rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap, |
beed53a1 | 1037 | "attempting to determine the manufacturer"); |
08837438 | 1038 | if (rc) |
beed53a1 | 1039 | return 0; |
1da177e4 | 1040 | str += sprintf(str, "Manufacturer: 0x%x\n", |
08837438 | 1041 | be32_to_cpu(cap.manufacturer_id)); |
1da177e4 | 1042 | |
08837438 RA |
1043 | rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap, |
1044 | "attempting to determine the 1.1 version"); | |
beed53a1 | 1045 | if (rc) |
08837438 | 1046 | return 0; |
beed53a1 KJH |
1047 | str += sprintf(str, |
1048 | "TCG version: %d.%d\nFirmware version: %d.%d\n", | |
08837438 RA |
1049 | cap.tpm_version.Major, cap.tpm_version.Minor, |
1050 | cap.tpm_version.revMajor, cap.tpm_version.revMinor); | |
1da177e4 LT |
1051 | return str - buf; |
1052 | } | |
6659ca2a KH |
1053 | EXPORT_SYMBOL_GPL(tpm_show_caps); |
1054 | ||
08e96e48 KJH |
1055 | ssize_t tpm_show_caps_1_2(struct device * dev, |
1056 | struct device_attribute * attr, char *buf) | |
1057 | { | |
08837438 RA |
1058 | cap_t cap; |
1059 | ssize_t rc; | |
08e96e48 KJH |
1060 | char *str = buf; |
1061 | ||
08837438 RA |
1062 | rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap, |
1063 | "attempting to determine the manufacturer"); | |
1064 | if (rc) | |
08e96e48 | 1065 | return 0; |
08e96e48 | 1066 | str += sprintf(str, "Manufacturer: 0x%x\n", |
08837438 RA |
1067 | be32_to_cpu(cap.manufacturer_id)); |
1068 | rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap, | |
1069 | "attempting to determine the 1.2 version"); | |
1070 | if (rc) | |
1071 | return 0; | |
08e96e48 KJH |
1072 | str += sprintf(str, |
1073 | "TCG version: %d.%d\nFirmware version: %d.%d\n", | |
08837438 RA |
1074 | cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor, |
1075 | cap.tpm_version_1_2.revMajor, | |
1076 | cap.tpm_version_1_2.revMinor); | |
08e96e48 KJH |
1077 | return str - buf; |
1078 | } | |
1079 | EXPORT_SYMBOL_GPL(tpm_show_caps_1_2); | |
1080 | ||
04ab2293 SB |
1081 | ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr, |
1082 | char *buf) | |
1083 | { | |
1084 | struct tpm_chip *chip = dev_get_drvdata(dev); | |
1085 | ||
403d1d03 SB |
1086 | if (chip->vendor.duration[TPM_LONG] == 0) |
1087 | return 0; | |
1088 | ||
04ab2293 SB |
1089 | return sprintf(buf, "%d %d %d [%s]\n", |
1090 | jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]), | |
1091 | jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]), | |
1092 | jiffies_to_usecs(chip->vendor.duration[TPM_LONG]), | |
1093 | chip->vendor.duration_adjusted | |
1094 | ? "adjusted" : "original"); | |
1095 | } | |
1096 | EXPORT_SYMBOL_GPL(tpm_show_durations); | |
1097 | ||
62592101 SB |
1098 | ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr, |
1099 | char *buf) | |
1100 | { | |
1101 | struct tpm_chip *chip = dev_get_drvdata(dev); | |
1102 | ||
1103 | return sprintf(buf, "%d %d %d %d [%s]\n", | |
1104 | jiffies_to_usecs(chip->vendor.timeout_a), | |
1105 | jiffies_to_usecs(chip->vendor.timeout_b), | |
1106 | jiffies_to_usecs(chip->vendor.timeout_c), | |
1107 | jiffies_to_usecs(chip->vendor.timeout_d), | |
1108 | chip->vendor.timeout_adjusted | |
1109 | ? "adjusted" : "original"); | |
1110 | } | |
1111 | EXPORT_SYMBOL_GPL(tpm_show_timeouts); | |
1112 | ||
6659ca2a KH |
1113 | ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr, |
1114 | const char *buf, size_t count) | |
1115 | { | |
1116 | struct tpm_chip *chip = dev_get_drvdata(dev); | |
1117 | if (chip == NULL) | |
1118 | return 0; | |
1119 | ||
90dda520 | 1120 | chip->vendor.cancel(chip); |
6659ca2a KH |
1121 | return count; |
1122 | } | |
1123 | EXPORT_SYMBOL_GPL(tpm_store_cancel); | |
1da177e4 | 1124 | |
78f09cc2 SB |
1125 | static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask, bool check_cancel, |
1126 | bool *canceled) | |
1127 | { | |
1128 | u8 status = chip->vendor.status(chip); | |
1129 | ||
1130 | *canceled = false; | |
1131 | if ((status & mask) == mask) | |
1132 | return true; | |
1133 | if (check_cancel && chip->vendor.req_canceled(chip, status)) { | |
1134 | *canceled = true; | |
1135 | return true; | |
1136 | } | |
1137 | return false; | |
1138 | } | |
1139 | ||
fd048866 | 1140 | int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout, |
78f09cc2 | 1141 | wait_queue_head_t *queue, bool check_cancel) |
fd048866 RA |
1142 | { |
1143 | unsigned long stop; | |
1144 | long rc; | |
1145 | u8 status; | |
78f09cc2 | 1146 | bool canceled = false; |
fd048866 RA |
1147 | |
1148 | /* check current status */ | |
1149 | status = chip->vendor.status(chip); | |
1150 | if ((status & mask) == mask) | |
1151 | return 0; | |
1152 | ||
1153 | stop = jiffies + timeout; | |
1154 | ||
1155 | if (chip->vendor.irq) { | |
1156 | again: | |
1157 | timeout = stop - jiffies; | |
1158 | if ((long)timeout <= 0) | |
1159 | return -ETIME; | |
1160 | rc = wait_event_interruptible_timeout(*queue, | |
78f09cc2 SB |
1161 | wait_for_tpm_stat_cond(chip, mask, check_cancel, |
1162 | &canceled), | |
1163 | timeout); | |
1164 | if (rc > 0) { | |
1165 | if (canceled) | |
1166 | return -ECANCELED; | |
fd048866 | 1167 | return 0; |
78f09cc2 | 1168 | } |
fd048866 RA |
1169 | if (rc == -ERESTARTSYS && freezing(current)) { |
1170 | clear_thread_flag(TIF_SIGPENDING); | |
1171 | goto again; | |
1172 | } | |
1173 | } else { | |
1174 | do { | |
1175 | msleep(TPM_TIMEOUT); | |
1176 | status = chip->vendor.status(chip); | |
1177 | if ((status & mask) == mask) | |
1178 | return 0; | |
1179 | } while (time_before(jiffies, stop)); | |
1180 | } | |
1181 | return -ETIME; | |
1182 | } | |
1183 | EXPORT_SYMBOL_GPL(wait_for_tpm_stat); | |
1da177e4 LT |
1184 | /* |
1185 | * Device file system interface to the TPM | |
f89c5edb RA |
1186 | * |
1187 | * It's assured that the chip will be opened just once, | |
1188 | * by the check of is_open variable, which is protected | |
1189 | * by driver_lock. | |
1da177e4 LT |
1190 | */ |
1191 | int tpm_open(struct inode *inode, struct file *file) | |
1192 | { | |
f02a9364 | 1193 | int minor = iminor(inode); |
1da177e4 LT |
1194 | struct tpm_chip *chip = NULL, *pos; |
1195 | ||
f02a9364 RA |
1196 | rcu_read_lock(); |
1197 | list_for_each_entry_rcu(pos, &tpm_chip_list, list) { | |
90dda520 | 1198 | if (pos->vendor.miscdev.minor == minor) { |
1da177e4 | 1199 | chip = pos; |
f02a9364 | 1200 | get_device(chip->dev); |
1da177e4 LT |
1201 | break; |
1202 | } | |
1203 | } | |
f02a9364 | 1204 | rcu_read_unlock(); |
1da177e4 | 1205 | |
f02a9364 RA |
1206 | if (!chip) |
1207 | return -ENODEV; | |
1da177e4 | 1208 | |
dc36d32c | 1209 | if (test_and_set_bit(0, &chip->is_open)) { |
b888c87b | 1210 | dev_dbg(chip->dev, "Another process owns this TPM\n"); |
f02a9364 RA |
1211 | put_device(chip->dev); |
1212 | return -EBUSY; | |
1da177e4 LT |
1213 | } |
1214 | ||
1309d7af | 1215 | chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL); |
1da177e4 | 1216 | if (chip->data_buffer == NULL) { |
dc36d32c | 1217 | clear_bit(0, &chip->is_open); |
e659a3fe | 1218 | put_device(chip->dev); |
1da177e4 LT |
1219 | return -ENOMEM; |
1220 | } | |
1221 | ||
1222 | atomic_set(&chip->data_pending, 0); | |
1223 | ||
1224 | file->private_data = chip; | |
1225 | return 0; | |
1da177e4 | 1226 | } |
1da177e4 LT |
1227 | EXPORT_SYMBOL_GPL(tpm_open); |
1228 | ||
f02a9364 RA |
1229 | /* |
1230 | * Called on file close | |
1231 | */ | |
1da177e4 LT |
1232 | int tpm_release(struct inode *inode, struct file *file) |
1233 | { | |
1234 | struct tpm_chip *chip = file->private_data; | |
1da177e4 | 1235 | |
4bdec11f | 1236 | del_singleshot_timer_sync(&chip->user_read_timer); |
43829731 | 1237 | flush_work(&chip->work); |
5e976d55 | 1238 | file->private_data = NULL; |
1da177e4 | 1239 | atomic_set(&chip->data_pending, 0); |
dd7da132 | 1240 | kzfree(chip->data_buffer); |
dc36d32c | 1241 | clear_bit(0, &chip->is_open); |
e659a3fe | 1242 | put_device(chip->dev); |
1da177e4 LT |
1243 | return 0; |
1244 | } | |
1da177e4 LT |
1245 | EXPORT_SYMBOL_GPL(tpm_release); |
1246 | ||
b888c87b | 1247 | ssize_t tpm_write(struct file *file, const char __user *buf, |
3c2f606a | 1248 | size_t size, loff_t *off) |
1da177e4 LT |
1249 | { |
1250 | struct tpm_chip *chip = file->private_data; | |
abce9ac2 PH |
1251 | size_t in_size = size; |
1252 | ssize_t out_size; | |
1da177e4 LT |
1253 | |
1254 | /* cannot perform a write until the read has cleared | |
abce9ac2 PH |
1255 | either via tpm_read or a user_read_timer timeout. |
1256 | This also prevents splitted buffered writes from blocking here. | |
1257 | */ | |
1258 | if (atomic_read(&chip->data_pending) != 0) | |
1259 | return -EBUSY; | |
1da177e4 LT |
1260 | |
1261 | if (in_size > TPM_BUFSIZE) | |
abce9ac2 PH |
1262 | return -E2BIG; |
1263 | ||
1264 | mutex_lock(&chip->buffer_mutex); | |
1da177e4 LT |
1265 | |
1266 | if (copy_from_user | |
1267 | (chip->data_buffer, (void __user *) buf, in_size)) { | |
d081d470 | 1268 | mutex_unlock(&chip->buffer_mutex); |
1da177e4 LT |
1269 | return -EFAULT; |
1270 | } | |
1271 | ||
1272 | /* atomic tpm command send and result receive */ | |
1273 | out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE); | |
abce9ac2 PH |
1274 | if (out_size < 0) { |
1275 | mutex_unlock(&chip->buffer_mutex); | |
1276 | return out_size; | |
1277 | } | |
1da177e4 LT |
1278 | |
1279 | atomic_set(&chip->data_pending, out_size); | |
d081d470 | 1280 | mutex_unlock(&chip->buffer_mutex); |
1da177e4 LT |
1281 | |
1282 | /* Set a timeout by which the reader must come claim the result */ | |
fe3fd483 | 1283 | mod_timer(&chip->user_read_timer, jiffies + (60 * HZ)); |
1da177e4 LT |
1284 | |
1285 | return in_size; | |
1286 | } | |
1da177e4 LT |
1287 | EXPORT_SYMBOL_GPL(tpm_write); |
1288 | ||
3c2f606a KJH |
1289 | ssize_t tpm_read(struct file *file, char __user *buf, |
1290 | size_t size, loff_t *off) | |
1da177e4 LT |
1291 | { |
1292 | struct tpm_chip *chip = file->private_data; | |
01476001 | 1293 | ssize_t ret_size; |
3321c07a | 1294 | int rc; |
1da177e4 | 1295 | |
5b44bd58 | 1296 | del_singleshot_timer_sync(&chip->user_read_timer); |
43829731 | 1297 | flush_work(&chip->work); |
5b44bd58 | 1298 | ret_size = atomic_read(&chip->data_pending); |
5b44bd58 | 1299 | if (ret_size > 0) { /* relay data */ |
3ab1aff8 | 1300 | ssize_t orig_ret_size = ret_size; |
5b44bd58 KH |
1301 | if (size < ret_size) |
1302 | ret_size = size; | |
1da177e4 | 1303 | |
d081d470 | 1304 | mutex_lock(&chip->buffer_mutex); |
3321c07a | 1305 | rc = copy_to_user(buf, chip->data_buffer, ret_size); |
3ab1aff8 | 1306 | memset(chip->data_buffer, 0, orig_ret_size); |
3321c07a | 1307 | if (rc) |
5b44bd58 | 1308 | ret_size = -EFAULT; |
3321c07a | 1309 | |
d081d470 | 1310 | mutex_unlock(&chip->buffer_mutex); |
1da177e4 LT |
1311 | } |
1312 | ||
dd7da132 KY |
1313 | atomic_set(&chip->data_pending, 0); |
1314 | ||
1da177e4 LT |
1315 | return ret_size; |
1316 | } | |
1da177e4 LT |
1317 | EXPORT_SYMBOL_GPL(tpm_read); |
1318 | ||
e659a3fe | 1319 | void tpm_remove_hardware(struct device *dev) |
1da177e4 | 1320 | { |
e659a3fe | 1321 | struct tpm_chip *chip = dev_get_drvdata(dev); |
1da177e4 LT |
1322 | |
1323 | if (chip == NULL) { | |
e659a3fe | 1324 | dev_err(dev, "No device data found\n"); |
1da177e4 LT |
1325 | return; |
1326 | } | |
1327 | ||
1328 | spin_lock(&driver_lock); | |
f02a9364 | 1329 | list_del_rcu(&chip->list); |
1da177e4 | 1330 | spin_unlock(&driver_lock); |
f02a9364 | 1331 | synchronize_rcu(); |
1da177e4 | 1332 | |
90dda520 | 1333 | misc_deregister(&chip->vendor.miscdev); |
90dda520 | 1334 | sysfs_remove_group(&dev->kobj, chip->vendor.attr_group); |
1631cfb7 | 1335 | tpm_remove_ppi(&dev->kobj); |
55a82ab3 | 1336 | tpm_bios_log_teardown(chip->bios_dir); |
1da177e4 | 1337 | |
5bd91f18 RM |
1338 | /* write it this way to be explicit (chip->dev == dev) */ |
1339 | put_device(chip->dev); | |
1da177e4 | 1340 | } |
e659a3fe | 1341 | EXPORT_SYMBOL_GPL(tpm_remove_hardware); |
1da177e4 | 1342 | |
225a9be2 RA |
1343 | #define TPM_ORD_SAVESTATE cpu_to_be32(152) |
1344 | #define SAVESTATE_RESULT_SIZE 10 | |
1345 | ||
1346 | static struct tpm_input_header savestate_header = { | |
1347 | .tag = TPM_TAG_RQU_COMMAND, | |
1348 | .length = cpu_to_be32(10), | |
1349 | .ordinal = TPM_ORD_SAVESTATE | |
1350 | }; | |
1351 | ||
1da177e4 LT |
1352 | /* |
1353 | * We are about to suspend. Save the TPM state | |
1354 | * so that it can be restored. | |
1355 | */ | |
035e2ce8 | 1356 | int tpm_pm_suspend(struct device *dev) |
1da177e4 | 1357 | { |
ce2c87d4 | 1358 | struct tpm_chip *chip = dev_get_drvdata(dev); |
225a9be2 | 1359 | struct tpm_cmd_t cmd; |
32d33b29 | 1360 | int rc, try; |
225a9be2 RA |
1361 | |
1362 | u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 }; | |
2490c681 | 1363 | |
1da177e4 LT |
1364 | if (chip == NULL) |
1365 | return -ENODEV; | |
1366 | ||
225a9be2 RA |
1367 | /* for buggy tpm, flush pcrs with extend to selected dummy */ |
1368 | if (tpm_suspend_pcr) { | |
1369 | cmd.header.in = pcrextend_header; | |
1370 | cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr); | |
1371 | memcpy(cmd.params.pcrextend_in.hash, dummy_hash, | |
1372 | TPM_DIGEST_SIZE); | |
1373 | rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, | |
1374 | "extending dummy pcr before suspend"); | |
1375 | } | |
1376 | ||
1377 | /* now do the actual savestate */ | |
32d33b29 DL |
1378 | for (try = 0; try < TPM_RETRY; try++) { |
1379 | cmd.header.in = savestate_header; | |
1380 | rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, NULL); | |
1381 | ||
1382 | /* | |
1383 | * If the TPM indicates that it is too busy to respond to | |
1384 | * this command then retry before giving up. It can take | |
1385 | * several seconds for this TPM to be ready. | |
1386 | * | |
1387 | * This can happen if the TPM has already been sent the | |
1388 | * SaveState command before the driver has loaded. TCG 1.2 | |
1389 | * specification states that any communication after SaveState | |
1390 | * may cause the TPM to invalidate previously saved state. | |
1391 | */ | |
1392 | if (rc != TPM_WARN_RETRY) | |
1393 | break; | |
1394 | msleep(TPM_TIMEOUT_RETRY); | |
1395 | } | |
1396 | ||
1397 | if (rc) | |
1398 | dev_err(chip->dev, | |
1399 | "Error (%d) sending savestate before suspend\n", rc); | |
1400 | else if (try > 0) | |
1401 | dev_warn(chip->dev, "TPM savestate took %dms\n", | |
1402 | try * TPM_TIMEOUT_RETRY); | |
1403 | ||
225a9be2 | 1404 | return rc; |
1da177e4 | 1405 | } |
1da177e4 LT |
1406 | EXPORT_SYMBOL_GPL(tpm_pm_suspend); |
1407 | ||
1408 | /* | |
1409 | * Resume from a power safe. The BIOS already restored | |
1410 | * the TPM state. | |
1411 | */ | |
ce2c87d4 | 1412 | int tpm_pm_resume(struct device *dev) |
1da177e4 | 1413 | { |
ce2c87d4 | 1414 | struct tpm_chip *chip = dev_get_drvdata(dev); |
1da177e4 LT |
1415 | |
1416 | if (chip == NULL) | |
1417 | return -ENODEV; | |
1418 | ||
1da177e4 LT |
1419 | return 0; |
1420 | } | |
1da177e4 LT |
1421 | EXPORT_SYMBOL_GPL(tpm_pm_resume); |
1422 | ||
41ab999c KY |
1423 | #define TPM_GETRANDOM_RESULT_SIZE 18 |
1424 | static struct tpm_input_header tpm_getrandom_header = { | |
1425 | .tag = TPM_TAG_RQU_COMMAND, | |
1426 | .length = cpu_to_be32(14), | |
1427 | .ordinal = TPM_ORD_GET_RANDOM | |
1428 | }; | |
1429 | ||
1430 | /** | |
1431 | * tpm_get_random() - Get random bytes from the tpm's RNG | |
1432 | * @chip_num: A specific chip number for the request or TPM_ANY_NUM | |
1433 | * @out: destination buffer for the random bytes | |
1434 | * @max: the max number of bytes to write to @out | |
1435 | * | |
1436 | * Returns < 0 on error and the number of bytes read on success | |
1437 | */ | |
1438 | int tpm_get_random(u32 chip_num, u8 *out, size_t max) | |
1439 | { | |
1440 | struct tpm_chip *chip; | |
1441 | struct tpm_cmd_t tpm_cmd; | |
1442 | u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA); | |
1443 | int err, total = 0, retries = 5; | |
1444 | u8 *dest = out; | |
1445 | ||
d4281c33 JS |
1446 | if (!out || !num_bytes || max > TPM_MAX_RNG_DATA) |
1447 | return -EINVAL; | |
1448 | ||
41ab999c KY |
1449 | chip = tpm_chip_find_get(chip_num); |
1450 | if (chip == NULL) | |
1451 | return -ENODEV; | |
1452 | ||
41ab999c KY |
1453 | do { |
1454 | tpm_cmd.header.in = tpm_getrandom_header; | |
1455 | tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes); | |
1456 | ||
1457 | err = transmit_cmd(chip, &tpm_cmd, | |
1458 | TPM_GETRANDOM_RESULT_SIZE + num_bytes, | |
1459 | "attempting get random"); | |
1460 | if (err) | |
1461 | break; | |
1462 | ||
1463 | recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len); | |
1464 | memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd); | |
1465 | ||
1466 | dest += recd; | |
1467 | total += recd; | |
1468 | num_bytes -= recd; | |
1469 | } while (retries-- && total < max); | |
1470 | ||
d4281c33 | 1471 | tpm_chip_put(chip); |
41ab999c KY |
1472 | return total ? total : -EIO; |
1473 | } | |
1474 | EXPORT_SYMBOL_GPL(tpm_get_random); | |
1475 | ||
253115b7 RA |
1476 | /* In case vendor provided release function, call it too.*/ |
1477 | ||
1478 | void tpm_dev_vendor_release(struct tpm_chip *chip) | |
1479 | { | |
3072928f AS |
1480 | if (!chip) |
1481 | return; | |
1482 | ||
253115b7 RA |
1483 | if (chip->vendor.release) |
1484 | chip->vendor.release(chip->dev); | |
1485 | ||
1486 | clear_bit(chip->dev_num, dev_mask); | |
1487 | kfree(chip->vendor.miscdev.name); | |
1488 | } | |
1489 | EXPORT_SYMBOL_GPL(tpm_dev_vendor_release); | |
1490 | ||
1491 | ||
5bd91f18 RM |
1492 | /* |
1493 | * Once all references to platform device are down to 0, | |
1494 | * release all allocated structures. | |
5bd91f18 | 1495 | */ |
81198078 | 1496 | static void tpm_dev_release(struct device *dev) |
5bd91f18 RM |
1497 | { |
1498 | struct tpm_chip *chip = dev_get_drvdata(dev); | |
1499 | ||
3072928f AS |
1500 | if (!chip) |
1501 | return; | |
1502 | ||
253115b7 | 1503 | tpm_dev_vendor_release(chip); |
5bd91f18 | 1504 | |
253115b7 | 1505 | chip->release(dev); |
5bd91f18 RM |
1506 | kfree(chip); |
1507 | } | |
253115b7 | 1508 | EXPORT_SYMBOL_GPL(tpm_dev_release); |
5bd91f18 | 1509 | |
1da177e4 LT |
1510 | /* |
1511 | * Called from tpm_<specific>.c probe function only for devices | |
1512 | * the driver has determined it should claim. Prior to calling | |
1513 | * this function the specific probe function has called pci_enable_device | |
1514 | * upon errant exit from this function specific probe function should call | |
1515 | * pci_disable_device | |
1516 | */ | |
f02a9364 RA |
1517 | struct tpm_chip *tpm_register_hardware(struct device *dev, |
1518 | const struct tpm_vendor_specific *entry) | |
1da177e4 | 1519 | { |
6f9beccb KJH |
1520 | #define DEVNAME_SIZE 7 |
1521 | ||
1522 | char *devname; | |
1da177e4 | 1523 | struct tpm_chip *chip; |
1da177e4 LT |
1524 | |
1525 | /* Driver specific per-device data */ | |
b888c87b | 1526 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
8e39c933 PW |
1527 | devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL); |
1528 | ||
dd78c943 AM |
1529 | if (chip == NULL || devname == NULL) |
1530 | goto out_free; | |
1da177e4 | 1531 | |
d081d470 MK |
1532 | mutex_init(&chip->buffer_mutex); |
1533 | mutex_init(&chip->tpm_mutex); | |
1da177e4 LT |
1534 | INIT_LIST_HEAD(&chip->list); |
1535 | ||
c4028958 | 1536 | INIT_WORK(&chip->work, timeout_work); |
09e12f9f | 1537 | |
40565f19 JS |
1538 | setup_timer(&chip->user_read_timer, user_reader_timeout, |
1539 | (unsigned long)chip); | |
fe3fd483 | 1540 | |
90dda520 | 1541 | memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific)); |
1da177e4 | 1542 | |
10685a95 | 1543 | chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES); |
1da177e4 | 1544 | |
10685a95 | 1545 | if (chip->dev_num >= TPM_NUM_DEVICES) { |
b888c87b | 1546 | dev_err(dev, "No available tpm device numbers\n"); |
dd78c943 | 1547 | goto out_free; |
1da177e4 | 1548 | } else if (chip->dev_num == 0) |
90dda520 | 1549 | chip->vendor.miscdev.minor = TPM_MINOR; |
1da177e4 | 1550 | else |
90dda520 | 1551 | chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR; |
1da177e4 | 1552 | |
10685a95 KJH |
1553 | set_bit(chip->dev_num, dev_mask); |
1554 | ||
6f9beccb | 1555 | scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num); |
90dda520 | 1556 | chip->vendor.miscdev.name = devname; |
1da177e4 | 1557 | |
94fbcded | 1558 | chip->vendor.miscdev.parent = dev; |
e659a3fe | 1559 | chip->dev = get_device(dev); |
5bd91f18 RM |
1560 | chip->release = dev->release; |
1561 | dev->release = tpm_dev_release; | |
1562 | dev_set_drvdata(dev, chip); | |
1da177e4 | 1563 | |
90dda520 | 1564 | if (misc_register(&chip->vendor.miscdev)) { |
e659a3fe | 1565 | dev_err(chip->dev, |
1da177e4 | 1566 | "unable to misc_register %s, minor %d\n", |
90dda520 KJH |
1567 | chip->vendor.miscdev.name, |
1568 | chip->vendor.miscdev.minor); | |
dad79cb8 | 1569 | goto put_device; |
1da177e4 LT |
1570 | } |
1571 | ||
f33d9bd5 | 1572 | if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) { |
5d469ec0 | 1573 | misc_deregister(&chip->vendor.miscdev); |
dad79cb8 | 1574 | goto put_device; |
f33d9bd5 | 1575 | } |
1da177e4 | 1576 | |
1631cfb7 | 1577 | if (tpm_add_ppi(&dev->kobj)) { |
f84fdff0 XZ |
1578 | misc_deregister(&chip->vendor.miscdev); |
1579 | goto put_device; | |
1580 | } | |
1581 | ||
55a82ab3 KJH |
1582 | chip->bios_dir = tpm_bios_log_setup(devname); |
1583 | ||
f02a9364 RA |
1584 | /* Make chip available */ |
1585 | spin_lock(&driver_lock); | |
1586 | list_add_rcu(&chip->list, &tpm_chip_list); | |
1587 | spin_unlock(&driver_lock); | |
1588 | ||
e0dd03ca | 1589 | return chip; |
dd78c943 | 1590 | |
dad79cb8 WG |
1591 | put_device: |
1592 | put_device(chip->dev); | |
dd78c943 AM |
1593 | out_free: |
1594 | kfree(chip); | |
1595 | kfree(devname); | |
1596 | return NULL; | |
1da177e4 | 1597 | } |
1da177e4 LT |
1598 | EXPORT_SYMBOL_GPL(tpm_register_hardware); |
1599 | ||
1da177e4 LT |
1600 | MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)"); |
1601 | MODULE_DESCRIPTION("TPM Driver"); | |
1602 | MODULE_VERSION("2.0"); | |
1603 | MODULE_LICENSE("GPL"); |