[IPV6] fix ipv6_getsockopt_sticky copy_to_user leak
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / tpm / tpm.c
blobe5a254a434f8345696c806b35a8d4b2539ed44fe
1 /*
2 * Copyright (C) 2004 IBM Corporation
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>
10 * Maintained by: <tpmdd_devel@lists.sourceforge.net>
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
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.
20 * Note, the TPM chip is not interrupt driven (only polling)
21 * and can have very long timeouts (minutes!). Hence the unusual
22 * calls to msleep.
26 #include <linux/poll.h>
27 #include <linux/spinlock.h>
28 #include "tpm.h"
30 enum tpm_const {
31 TPM_MINOR = 224, /* officially assigned */
32 TPM_BUFSIZE = 2048,
33 TPM_NUM_DEVICES = 256,
36 enum tpm_duration {
37 TPM_SHORT = 0,
38 TPM_MEDIUM = 1,
39 TPM_LONG = 2,
40 TPM_UNDEFINED,
43 #define TPM_MAX_ORDINAL 243
44 #define TPM_MAX_PROTECTED_ORDINAL 12
45 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
47 static LIST_HEAD(tpm_chip_list);
48 static DEFINE_SPINLOCK(driver_lock);
49 static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
52 * Array with one entry per ordinal defining the maximum amount
53 * of time the chip could take to return the result. The ordinal
54 * designation of short, medium or long is defined in a table in
55 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
56 * values of the SHORT, MEDIUM, and LONG durations are retrieved
57 * from the chip during initialization with a call to tpm_get_timeouts.
59 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
60 TPM_UNDEFINED, /* 0 */
61 TPM_UNDEFINED,
62 TPM_UNDEFINED,
63 TPM_UNDEFINED,
64 TPM_UNDEFINED,
65 TPM_UNDEFINED, /* 5 */
66 TPM_UNDEFINED,
67 TPM_UNDEFINED,
68 TPM_UNDEFINED,
69 TPM_UNDEFINED,
70 TPM_SHORT, /* 10 */
71 TPM_SHORT,
74 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
75 TPM_UNDEFINED, /* 0 */
76 TPM_UNDEFINED,
77 TPM_UNDEFINED,
78 TPM_UNDEFINED,
79 TPM_UNDEFINED,
80 TPM_UNDEFINED, /* 5 */
81 TPM_UNDEFINED,
82 TPM_UNDEFINED,
83 TPM_UNDEFINED,
84 TPM_UNDEFINED,
85 TPM_SHORT, /* 10 */
86 TPM_SHORT,
87 TPM_MEDIUM,
88 TPM_LONG,
89 TPM_LONG,
90 TPM_MEDIUM, /* 15 */
91 TPM_SHORT,
92 TPM_SHORT,
93 TPM_MEDIUM,
94 TPM_LONG,
95 TPM_SHORT, /* 20 */
96 TPM_SHORT,
97 TPM_MEDIUM,
98 TPM_MEDIUM,
99 TPM_MEDIUM,
100 TPM_SHORT, /* 25 */
101 TPM_SHORT,
102 TPM_MEDIUM,
103 TPM_SHORT,
104 TPM_SHORT,
105 TPM_MEDIUM, /* 30 */
106 TPM_LONG,
107 TPM_MEDIUM,
108 TPM_SHORT,
109 TPM_SHORT,
110 TPM_SHORT, /* 35 */
111 TPM_MEDIUM,
112 TPM_MEDIUM,
113 TPM_UNDEFINED,
114 TPM_UNDEFINED,
115 TPM_MEDIUM, /* 40 */
116 TPM_LONG,
117 TPM_MEDIUM,
118 TPM_SHORT,
119 TPM_SHORT,
120 TPM_SHORT, /* 45 */
121 TPM_SHORT,
122 TPM_SHORT,
123 TPM_SHORT,
124 TPM_LONG,
125 TPM_MEDIUM, /* 50 */
126 TPM_MEDIUM,
127 TPM_UNDEFINED,
128 TPM_UNDEFINED,
129 TPM_UNDEFINED,
130 TPM_UNDEFINED, /* 55 */
131 TPM_UNDEFINED,
132 TPM_UNDEFINED,
133 TPM_UNDEFINED,
134 TPM_UNDEFINED,
135 TPM_MEDIUM, /* 60 */
136 TPM_MEDIUM,
137 TPM_MEDIUM,
138 TPM_SHORT,
139 TPM_SHORT,
140 TPM_MEDIUM, /* 65 */
141 TPM_UNDEFINED,
142 TPM_UNDEFINED,
143 TPM_UNDEFINED,
144 TPM_UNDEFINED,
145 TPM_SHORT, /* 70 */
146 TPM_SHORT,
147 TPM_UNDEFINED,
148 TPM_UNDEFINED,
149 TPM_UNDEFINED,
150 TPM_UNDEFINED, /* 75 */
151 TPM_UNDEFINED,
152 TPM_UNDEFINED,
153 TPM_UNDEFINED,
154 TPM_UNDEFINED,
155 TPM_LONG, /* 80 */
156 TPM_UNDEFINED,
157 TPM_MEDIUM,
158 TPM_LONG,
159 TPM_SHORT,
160 TPM_UNDEFINED, /* 85 */
161 TPM_UNDEFINED,
162 TPM_UNDEFINED,
163 TPM_UNDEFINED,
164 TPM_UNDEFINED,
165 TPM_SHORT, /* 90 */
166 TPM_SHORT,
167 TPM_SHORT,
168 TPM_SHORT,
169 TPM_SHORT,
170 TPM_UNDEFINED, /* 95 */
171 TPM_UNDEFINED,
172 TPM_UNDEFINED,
173 TPM_UNDEFINED,
174 TPM_UNDEFINED,
175 TPM_MEDIUM, /* 100 */
176 TPM_SHORT,
177 TPM_SHORT,
178 TPM_UNDEFINED,
179 TPM_UNDEFINED,
180 TPM_UNDEFINED, /* 105 */
181 TPM_UNDEFINED,
182 TPM_UNDEFINED,
183 TPM_UNDEFINED,
184 TPM_UNDEFINED,
185 TPM_SHORT, /* 110 */
186 TPM_SHORT,
187 TPM_SHORT,
188 TPM_SHORT,
189 TPM_SHORT,
190 TPM_SHORT, /* 115 */
191 TPM_SHORT,
192 TPM_SHORT,
193 TPM_UNDEFINED,
194 TPM_UNDEFINED,
195 TPM_LONG, /* 120 */
196 TPM_LONG,
197 TPM_MEDIUM,
198 TPM_UNDEFINED,
199 TPM_SHORT,
200 TPM_SHORT, /* 125 */
201 TPM_SHORT,
202 TPM_LONG,
203 TPM_SHORT,
204 TPM_SHORT,
205 TPM_SHORT, /* 130 */
206 TPM_MEDIUM,
207 TPM_UNDEFINED,
208 TPM_SHORT,
209 TPM_MEDIUM,
210 TPM_UNDEFINED, /* 135 */
211 TPM_UNDEFINED,
212 TPM_UNDEFINED,
213 TPM_UNDEFINED,
214 TPM_UNDEFINED,
215 TPM_SHORT, /* 140 */
216 TPM_SHORT,
217 TPM_UNDEFINED,
218 TPM_UNDEFINED,
219 TPM_UNDEFINED,
220 TPM_UNDEFINED, /* 145 */
221 TPM_UNDEFINED,
222 TPM_UNDEFINED,
223 TPM_UNDEFINED,
224 TPM_UNDEFINED,
225 TPM_SHORT, /* 150 */
226 TPM_MEDIUM,
227 TPM_MEDIUM,
228 TPM_SHORT,
229 TPM_SHORT,
230 TPM_UNDEFINED, /* 155 */
231 TPM_UNDEFINED,
232 TPM_UNDEFINED,
233 TPM_UNDEFINED,
234 TPM_UNDEFINED,
235 TPM_SHORT, /* 160 */
236 TPM_SHORT,
237 TPM_SHORT,
238 TPM_SHORT,
239 TPM_UNDEFINED,
240 TPM_UNDEFINED, /* 165 */
241 TPM_UNDEFINED,
242 TPM_UNDEFINED,
243 TPM_UNDEFINED,
244 TPM_UNDEFINED,
245 TPM_LONG, /* 170 */
246 TPM_UNDEFINED,
247 TPM_UNDEFINED,
248 TPM_UNDEFINED,
249 TPM_UNDEFINED,
250 TPM_UNDEFINED, /* 175 */
251 TPM_UNDEFINED,
252 TPM_UNDEFINED,
253 TPM_UNDEFINED,
254 TPM_UNDEFINED,
255 TPM_MEDIUM, /* 180 */
256 TPM_SHORT,
257 TPM_MEDIUM,
258 TPM_MEDIUM,
259 TPM_MEDIUM,
260 TPM_MEDIUM, /* 185 */
261 TPM_SHORT,
262 TPM_UNDEFINED,
263 TPM_UNDEFINED,
264 TPM_UNDEFINED,
265 TPM_UNDEFINED, /* 190 */
266 TPM_UNDEFINED,
267 TPM_UNDEFINED,
268 TPM_UNDEFINED,
269 TPM_UNDEFINED,
270 TPM_UNDEFINED, /* 195 */
271 TPM_UNDEFINED,
272 TPM_UNDEFINED,
273 TPM_UNDEFINED,
274 TPM_UNDEFINED,
275 TPM_SHORT, /* 200 */
276 TPM_UNDEFINED,
277 TPM_UNDEFINED,
278 TPM_UNDEFINED,
279 TPM_SHORT,
280 TPM_SHORT, /* 205 */
281 TPM_SHORT,
282 TPM_SHORT,
283 TPM_SHORT,
284 TPM_SHORT,
285 TPM_MEDIUM, /* 210 */
286 TPM_UNDEFINED,
287 TPM_MEDIUM,
288 TPM_MEDIUM,
289 TPM_MEDIUM,
290 TPM_UNDEFINED, /* 215 */
291 TPM_MEDIUM,
292 TPM_UNDEFINED,
293 TPM_UNDEFINED,
294 TPM_SHORT,
295 TPM_SHORT, /* 220 */
296 TPM_SHORT,
297 TPM_SHORT,
298 TPM_SHORT,
299 TPM_SHORT,
300 TPM_UNDEFINED, /* 225 */
301 TPM_UNDEFINED,
302 TPM_UNDEFINED,
303 TPM_UNDEFINED,
304 TPM_UNDEFINED,
305 TPM_SHORT, /* 230 */
306 TPM_LONG,
307 TPM_MEDIUM,
308 TPM_UNDEFINED,
309 TPM_UNDEFINED,
310 TPM_UNDEFINED, /* 235 */
311 TPM_UNDEFINED,
312 TPM_UNDEFINED,
313 TPM_UNDEFINED,
314 TPM_UNDEFINED,
315 TPM_SHORT, /* 240 */
316 TPM_UNDEFINED,
317 TPM_MEDIUM,
320 static void user_reader_timeout(unsigned long ptr)
322 struct tpm_chip *chip = (struct tpm_chip *) ptr;
324 schedule_work(&chip->work);
327 static void timeout_work(struct work_struct *work)
329 struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
331 down(&chip->buffer_mutex);
332 atomic_set(&chip->data_pending, 0);
333 memset(chip->data_buffer, 0, TPM_BUFSIZE);
334 up(&chip->buffer_mutex);
338 * Returns max number of jiffies to wait
340 unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
341 u32 ordinal)
343 int duration_idx = TPM_UNDEFINED;
344 int duration = 0;
346 if (ordinal < TPM_MAX_ORDINAL)
347 duration_idx = tpm_ordinal_duration[ordinal];
348 else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
349 TPM_MAX_PROTECTED_ORDINAL)
350 duration_idx =
351 tpm_protected_ordinal_duration[ordinal &
352 TPM_PROTECTED_ORDINAL_MASK];
354 if (duration_idx != TPM_UNDEFINED)
355 duration = chip->vendor.duration[duration_idx];
356 if (duration <= 0)
357 return 2 * 60 * HZ;
358 else
359 return duration;
361 EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
364 * Internal kernel interface to transmit TPM commands
366 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
367 size_t bufsiz)
369 ssize_t rc;
370 u32 count, ordinal;
371 unsigned long stop;
373 count = be32_to_cpu(*((__be32 *) (buf + 2)));
374 ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
375 if (count == 0)
376 return -ENODATA;
377 if (count > bufsiz) {
378 dev_err(chip->dev,
379 "invalid count value %x %zx \n", count, bufsiz);
380 return -E2BIG;
383 down(&chip->tpm_mutex);
385 if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
386 dev_err(chip->dev,
387 "tpm_transmit: tpm_send: error %zd\n", rc);
388 goto out;
391 if (chip->vendor.irq)
392 goto out_recv;
394 stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
395 do {
396 u8 status = chip->vendor.status(chip);
397 if ((status & chip->vendor.req_complete_mask) ==
398 chip->vendor.req_complete_val)
399 goto out_recv;
401 if ((status == chip->vendor.req_canceled)) {
402 dev_err(chip->dev, "Operation Canceled\n");
403 rc = -ECANCELED;
404 goto out;
407 msleep(TPM_TIMEOUT); /* CHECK */
408 rmb();
409 } while (time_before(jiffies, stop));
411 chip->vendor.cancel(chip);
412 dev_err(chip->dev, "Operation Timed out\n");
413 rc = -ETIME;
414 goto out;
416 out_recv:
417 rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
418 if (rc < 0)
419 dev_err(chip->dev,
420 "tpm_transmit: tpm_recv: error %zd\n", rc);
421 out:
422 up(&chip->tpm_mutex);
423 return rc;
426 #define TPM_DIGEST_SIZE 20
427 #define TPM_ERROR_SIZE 10
428 #define TPM_RET_CODE_IDX 6
429 #define TPM_GET_CAP_RET_SIZE_IDX 10
430 #define TPM_GET_CAP_RET_UINT32_1_IDX 14
431 #define TPM_GET_CAP_RET_UINT32_2_IDX 18
432 #define TPM_GET_CAP_RET_UINT32_3_IDX 22
433 #define TPM_GET_CAP_RET_UINT32_4_IDX 26
434 #define TPM_GET_CAP_PERM_DISABLE_IDX 16
435 #define TPM_GET_CAP_PERM_INACTIVE_IDX 18
436 #define TPM_GET_CAP_RET_BOOL_1_IDX 14
437 #define TPM_GET_CAP_TEMP_INACTIVE_IDX 16
439 #define TPM_CAP_IDX 13
440 #define TPM_CAP_SUBCAP_IDX 21
442 enum tpm_capabilities {
443 TPM_CAP_FLAG = 4,
444 TPM_CAP_PROP = 5,
447 enum tpm_sub_capabilities {
448 TPM_CAP_PROP_PCR = 0x1,
449 TPM_CAP_PROP_MANUFACTURER = 0x3,
450 TPM_CAP_FLAG_PERM = 0x8,
451 TPM_CAP_FLAG_VOL = 0x9,
452 TPM_CAP_PROP_OWNER = 0x11,
453 TPM_CAP_PROP_TIS_TIMEOUT = 0x15,
454 TPM_CAP_PROP_TIS_DURATION = 0x20,
458 * This is a semi generic GetCapability command for use
459 * with the capability type TPM_CAP_PROP or TPM_CAP_FLAG
460 * and their associated sub_capabilities.
463 static const u8 tpm_cap[] = {
464 0, 193, /* TPM_TAG_RQU_COMMAND */
465 0, 0, 0, 22, /* length */
466 0, 0, 0, 101, /* TPM_ORD_GetCapability */
467 0, 0, 0, 0, /* TPM_CAP_<TYPE> */
468 0, 0, 0, 4, /* TPM_CAP_SUB_<TYPE> size */
469 0, 0, 1, 0 /* TPM_CAP_SUB_<TYPE> */
472 static ssize_t transmit_cmd(struct tpm_chip *chip, u8 *data, int len,
473 char *desc)
475 int err;
477 len = tpm_transmit(chip, data, len);
478 if (len < 0)
479 return len;
480 if (len == TPM_ERROR_SIZE) {
481 err = be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX)));
482 dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
483 return err;
485 return 0;
488 void tpm_gen_interrupt(struct tpm_chip *chip)
490 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 30)];
491 ssize_t rc;
493 memcpy(data, tpm_cap, sizeof(tpm_cap));
494 data[TPM_CAP_IDX] = TPM_CAP_PROP;
495 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_TIMEOUT;
497 rc = transmit_cmd(chip, data, sizeof(data),
498 "attempting to determine the timeouts");
500 EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
502 void tpm_get_timeouts(struct tpm_chip *chip)
504 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 30)];
505 ssize_t rc;
506 u32 timeout;
508 memcpy(data, tpm_cap, sizeof(tpm_cap));
509 data[TPM_CAP_IDX] = TPM_CAP_PROP;
510 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_TIMEOUT;
512 rc = transmit_cmd(chip, data, sizeof(data),
513 "attempting to determine the timeouts");
514 if (rc)
515 goto duration;
517 if (be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_SIZE_IDX)))
518 != 4 * sizeof(u32))
519 goto duration;
521 /* Don't overwrite default if value is 0 */
522 timeout =
523 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX)));
524 if (timeout)
525 chip->vendor.timeout_a = msecs_to_jiffies(timeout);
526 timeout =
527 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_2_IDX)));
528 if (timeout)
529 chip->vendor.timeout_b = msecs_to_jiffies(timeout);
530 timeout =
531 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_3_IDX)));
532 if (timeout)
533 chip->vendor.timeout_c = msecs_to_jiffies(timeout);
534 timeout =
535 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_4_IDX)));
536 if (timeout)
537 chip->vendor.timeout_d = msecs_to_jiffies(timeout);
539 duration:
540 memcpy(data, tpm_cap, sizeof(tpm_cap));
541 data[TPM_CAP_IDX] = TPM_CAP_PROP;
542 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_DURATION;
544 rc = transmit_cmd(chip, data, sizeof(data),
545 "attempting to determine the durations");
546 if (rc)
547 return;
549 if (be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_SIZE_IDX)))
550 != 3 * sizeof(u32))
551 return;
553 chip->vendor.duration[TPM_SHORT] =
554 msecs_to_jiffies(be32_to_cpu
555 (*((__be32 *) (data +
556 TPM_GET_CAP_RET_UINT32_1_IDX))));
557 chip->vendor.duration[TPM_MEDIUM] =
558 msecs_to_jiffies(be32_to_cpu
559 (*((__be32 *) (data +
560 TPM_GET_CAP_RET_UINT32_2_IDX))));
561 chip->vendor.duration[TPM_LONG] =
562 msecs_to_jiffies(be32_to_cpu
563 (*((__be32 *) (data +
564 TPM_GET_CAP_RET_UINT32_3_IDX))));
566 EXPORT_SYMBOL_GPL(tpm_get_timeouts);
568 void tpm_continue_selftest(struct tpm_chip *chip)
570 u8 data[] = {
571 0, 193, /* TPM_TAG_RQU_COMMAND */
572 0, 0, 0, 10, /* length */
573 0, 0, 0, 83, /* TPM_ORD_GetCapability */
576 tpm_transmit(chip, data, sizeof(data));
578 EXPORT_SYMBOL_GPL(tpm_continue_selftest);
580 ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
581 char *buf)
583 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 35)];
584 ssize_t rc;
586 struct tpm_chip *chip = dev_get_drvdata(dev);
587 if (chip == NULL)
588 return -ENODEV;
590 memcpy(data, tpm_cap, sizeof(tpm_cap));
591 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
592 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM;
594 rc = transmit_cmd(chip, data, sizeof(data),
595 "attemtping to determine the permanent state");
596 if (rc)
597 return 0;
598 return sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_DISABLE_IDX]);
600 EXPORT_SYMBOL_GPL(tpm_show_enabled);
602 ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
603 char *buf)
605 u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 35)];
606 ssize_t rc;
608 struct tpm_chip *chip = dev_get_drvdata(dev);
609 if (chip == NULL)
610 return -ENODEV;
612 memcpy(data, tpm_cap, sizeof(tpm_cap));
613 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
614 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM;
616 rc = transmit_cmd(chip, data, sizeof(data),
617 "attemtping to determine the permanent state");
618 if (rc)
619 return 0;
620 return sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_INACTIVE_IDX]);
622 EXPORT_SYMBOL_GPL(tpm_show_active);
624 ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
625 char *buf)
627 u8 data[sizeof(tpm_cap)];
628 ssize_t rc;
630 struct tpm_chip *chip = dev_get_drvdata(dev);
631 if (chip == NULL)
632 return -ENODEV;
634 memcpy(data, tpm_cap, sizeof(tpm_cap));
635 data[TPM_CAP_IDX] = TPM_CAP_PROP;
636 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_OWNER;
638 rc = transmit_cmd(chip, data, sizeof(data),
639 "attempting to determine the owner state");
640 if (rc)
641 return 0;
642 return sprintf(buf, "%d\n", data[TPM_GET_CAP_RET_BOOL_1_IDX]);
644 EXPORT_SYMBOL_GPL(tpm_show_owned);
646 ssize_t tpm_show_temp_deactivated(struct device * dev,
647 struct device_attribute * attr, char *buf)
649 u8 data[sizeof(tpm_cap)];
650 ssize_t rc;
652 struct tpm_chip *chip = dev_get_drvdata(dev);
653 if (chip == NULL)
654 return -ENODEV;
656 memcpy(data, tpm_cap, sizeof(tpm_cap));
657 data[TPM_CAP_IDX] = TPM_CAP_FLAG;
658 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_VOL;
660 rc = transmit_cmd(chip, data, sizeof(data),
661 "attempting to determine the temporary state");
662 if (rc)
663 return 0;
664 return sprintf(buf, "%d\n", data[TPM_GET_CAP_TEMP_INACTIVE_IDX]);
666 EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
668 static const u8 pcrread[] = {
669 0, 193, /* TPM_TAG_RQU_COMMAND */
670 0, 0, 0, 14, /* length */
671 0, 0, 0, 21, /* TPM_ORD_PcrRead */
672 0, 0, 0, 0 /* PCR index */
675 ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
676 char *buf)
678 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(pcrread)), 30)];
679 ssize_t rc;
680 int i, j, num_pcrs;
681 __be32 index;
682 char *str = buf;
684 struct tpm_chip *chip = dev_get_drvdata(dev);
685 if (chip == NULL)
686 return -ENODEV;
688 memcpy(data, tpm_cap, sizeof(tpm_cap));
689 data[TPM_CAP_IDX] = TPM_CAP_PROP;
690 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_PCR;
692 rc = transmit_cmd(chip, data, sizeof(data),
693 "attempting to determine the number of PCRS");
694 if (rc)
695 return 0;
697 num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
698 for (i = 0; i < num_pcrs; i++) {
699 memcpy(data, pcrread, sizeof(pcrread));
700 index = cpu_to_be32(i);
701 memcpy(data + 10, &index, 4);
702 rc = transmit_cmd(chip, data, sizeof(data),
703 "attempting to read a PCR");
704 if (rc)
705 goto out;
706 str += sprintf(str, "PCR-%02d: ", i);
707 for (j = 0; j < TPM_DIGEST_SIZE; j++)
708 str += sprintf(str, "%02X ", *(data + 10 + j));
709 str += sprintf(str, "\n");
711 out:
712 return str - buf;
714 EXPORT_SYMBOL_GPL(tpm_show_pcrs);
716 #define READ_PUBEK_RESULT_SIZE 314
717 static const u8 readpubek[] = {
718 0, 193, /* TPM_TAG_RQU_COMMAND */
719 0, 0, 0, 30, /* length */
720 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
723 ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
724 char *buf)
726 u8 *data;
727 ssize_t err;
728 int i, rc;
729 char *str = buf;
731 struct tpm_chip *chip = dev_get_drvdata(dev);
732 if (chip == NULL)
733 return -ENODEV;
735 data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
736 if (!data)
737 return -ENOMEM;
739 memcpy(data, readpubek, sizeof(readpubek));
741 err = transmit_cmd(chip, data, READ_PUBEK_RESULT_SIZE,
742 "attempting to read the PUBEK");
743 if (err)
744 goto out;
747 ignore header 10 bytes
748 algorithm 32 bits (1 == RSA )
749 encscheme 16 bits
750 sigscheme 16 bits
751 parameters (RSA 12->bytes: keybit, #primes, expbit)
752 keylenbytes 32 bits
753 256 byte modulus
754 ignore checksum 20 bytes
757 str +=
758 sprintf(str,
759 "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
760 "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
761 " %02X %02X %02X %02X %02X %02X %02X %02X\n"
762 "Modulus length: %d\nModulus: \n",
763 data[10], data[11], data[12], data[13], data[14],
764 data[15], data[16], data[17], data[22], data[23],
765 data[24], data[25], data[26], data[27], data[28],
766 data[29], data[30], data[31], data[32], data[33],
767 be32_to_cpu(*((__be32 *) (data + 34))));
769 for (i = 0; i < 256; i++) {
770 str += sprintf(str, "%02X ", data[i + 38]);
771 if ((i + 1) % 16 == 0)
772 str += sprintf(str, "\n");
774 out:
775 rc = str - buf;
776 kfree(data);
777 return rc;
779 EXPORT_SYMBOL_GPL(tpm_show_pubek);
781 #define CAP_VERSION_1_1 6
782 #define CAP_VERSION_1_2 0x1A
783 #define CAP_VERSION_IDX 13
784 static const u8 cap_version[] = {
785 0, 193, /* TPM_TAG_RQU_COMMAND */
786 0, 0, 0, 18, /* length */
787 0, 0, 0, 101, /* TPM_ORD_GetCapability */
788 0, 0, 0, 0,
789 0, 0, 0, 0
792 ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
793 char *buf)
795 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(cap_version)), 30)];
796 ssize_t rc;
797 char *str = buf;
799 struct tpm_chip *chip = dev_get_drvdata(dev);
800 if (chip == NULL)
801 return -ENODEV;
803 memcpy(data, tpm_cap, sizeof(tpm_cap));
804 data[TPM_CAP_IDX] = TPM_CAP_PROP;
805 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
807 rc = transmit_cmd(chip, data, sizeof(data),
808 "attempting to determine the manufacturer");
809 if (rc)
810 return 0;
812 str += sprintf(str, "Manufacturer: 0x%x\n",
813 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
815 memcpy(data, cap_version, sizeof(cap_version));
816 data[CAP_VERSION_IDX] = CAP_VERSION_1_1;
817 rc = transmit_cmd(chip, data, sizeof(data),
818 "attempting to determine the 1.1 version");
819 if (rc)
820 goto out;
822 str += sprintf(str,
823 "TCG version: %d.%d\nFirmware version: %d.%d\n",
824 (int) data[14], (int) data[15], (int) data[16],
825 (int) data[17]);
827 out:
828 return str - buf;
830 EXPORT_SYMBOL_GPL(tpm_show_caps);
832 ssize_t tpm_show_caps_1_2(struct device * dev,
833 struct device_attribute * attr, char *buf)
835 u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(cap_version)), 30)];
836 ssize_t len;
837 char *str = buf;
839 struct tpm_chip *chip = dev_get_drvdata(dev);
840 if (chip == NULL)
841 return -ENODEV;
843 memcpy(data, tpm_cap, sizeof(tpm_cap));
844 data[TPM_CAP_IDX] = TPM_CAP_PROP;
845 data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
847 if ((len = tpm_transmit(chip, data, sizeof(data))) <=
848 TPM_ERROR_SIZE) {
849 dev_dbg(chip->dev, "A TPM error (%d) occurred "
850 "attempting to determine the manufacturer\n",
851 be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))));
852 return 0;
855 str += sprintf(str, "Manufacturer: 0x%x\n",
856 be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
858 memcpy(data, cap_version, sizeof(cap_version));
859 data[CAP_VERSION_IDX] = CAP_VERSION_1_2;
861 if ((len = tpm_transmit(chip, data, sizeof(data))) <=
862 TPM_ERROR_SIZE) {
863 dev_err(chip->dev, "A TPM error (%d) occurred "
864 "attempting to determine the 1.2 version\n",
865 be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))));
866 goto out;
868 str += sprintf(str,
869 "TCG version: %d.%d\nFirmware version: %d.%d\n",
870 (int) data[16], (int) data[17], (int) data[18],
871 (int) data[19]);
873 out:
874 return str - buf;
876 EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
878 ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
879 const char *buf, size_t count)
881 struct tpm_chip *chip = dev_get_drvdata(dev);
882 if (chip == NULL)
883 return 0;
885 chip->vendor.cancel(chip);
886 return count;
888 EXPORT_SYMBOL_GPL(tpm_store_cancel);
891 * Device file system interface to the TPM
893 int tpm_open(struct inode *inode, struct file *file)
895 int rc = 0, minor = iminor(inode);
896 struct tpm_chip *chip = NULL, *pos;
898 spin_lock(&driver_lock);
900 list_for_each_entry(pos, &tpm_chip_list, list) {
901 if (pos->vendor.miscdev.minor == minor) {
902 chip = pos;
903 break;
907 if (chip == NULL) {
908 rc = -ENODEV;
909 goto err_out;
912 if (chip->num_opens) {
913 dev_dbg(chip->dev, "Another process owns this TPM\n");
914 rc = -EBUSY;
915 goto err_out;
918 chip->num_opens++;
919 get_device(chip->dev);
921 spin_unlock(&driver_lock);
923 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
924 if (chip->data_buffer == NULL) {
925 chip->num_opens--;
926 put_device(chip->dev);
927 return -ENOMEM;
930 atomic_set(&chip->data_pending, 0);
932 file->private_data = chip;
933 return 0;
935 err_out:
936 spin_unlock(&driver_lock);
937 return rc;
939 EXPORT_SYMBOL_GPL(tpm_open);
941 int tpm_release(struct inode *inode, struct file *file)
943 struct tpm_chip *chip = file->private_data;
945 spin_lock(&driver_lock);
946 file->private_data = NULL;
947 chip->num_opens--;
948 del_singleshot_timer_sync(&chip->user_read_timer);
949 flush_scheduled_work();
950 atomic_set(&chip->data_pending, 0);
951 put_device(chip->dev);
952 kfree(chip->data_buffer);
953 spin_unlock(&driver_lock);
954 return 0;
956 EXPORT_SYMBOL_GPL(tpm_release);
958 ssize_t tpm_write(struct file *file, const char __user *buf,
959 size_t size, loff_t *off)
961 struct tpm_chip *chip = file->private_data;
962 int in_size = size, out_size;
964 /* cannot perform a write until the read has cleared
965 either via tpm_read or a user_read_timer timeout */
966 while (atomic_read(&chip->data_pending) != 0)
967 msleep(TPM_TIMEOUT);
969 down(&chip->buffer_mutex);
971 if (in_size > TPM_BUFSIZE)
972 in_size = TPM_BUFSIZE;
974 if (copy_from_user
975 (chip->data_buffer, (void __user *) buf, in_size)) {
976 up(&chip->buffer_mutex);
977 return -EFAULT;
980 /* atomic tpm command send and result receive */
981 out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
983 atomic_set(&chip->data_pending, out_size);
984 up(&chip->buffer_mutex);
986 /* Set a timeout by which the reader must come claim the result */
987 mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
989 return in_size;
991 EXPORT_SYMBOL_GPL(tpm_write);
993 ssize_t tpm_read(struct file *file, char __user *buf,
994 size_t size, loff_t *off)
996 struct tpm_chip *chip = file->private_data;
997 int ret_size;
999 del_singleshot_timer_sync(&chip->user_read_timer);
1000 flush_scheduled_work();
1001 ret_size = atomic_read(&chip->data_pending);
1002 atomic_set(&chip->data_pending, 0);
1003 if (ret_size > 0) { /* relay data */
1004 if (size < ret_size)
1005 ret_size = size;
1007 down(&chip->buffer_mutex);
1008 if (copy_to_user(buf, chip->data_buffer, ret_size))
1009 ret_size = -EFAULT;
1010 up(&chip->buffer_mutex);
1013 return ret_size;
1015 EXPORT_SYMBOL_GPL(tpm_read);
1017 void tpm_remove_hardware(struct device *dev)
1019 struct tpm_chip *chip = dev_get_drvdata(dev);
1021 if (chip == NULL) {
1022 dev_err(dev, "No device data found\n");
1023 return;
1026 spin_lock(&driver_lock);
1028 list_del(&chip->list);
1030 spin_unlock(&driver_lock);
1032 dev_set_drvdata(dev, NULL);
1033 misc_deregister(&chip->vendor.miscdev);
1034 kfree(chip->vendor.miscdev.name);
1036 sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
1037 tpm_bios_log_teardown(chip->bios_dir);
1039 clear_bit(chip->dev_num, dev_mask);
1041 kfree(chip);
1043 put_device(dev);
1045 EXPORT_SYMBOL_GPL(tpm_remove_hardware);
1047 static u8 savestate[] = {
1048 0, 193, /* TPM_TAG_RQU_COMMAND */
1049 0, 0, 0, 10, /* blob length (in bytes) */
1050 0, 0, 0, 152 /* TPM_ORD_SaveState */
1054 * We are about to suspend. Save the TPM state
1055 * so that it can be restored.
1057 int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
1059 struct tpm_chip *chip = dev_get_drvdata(dev);
1060 if (chip == NULL)
1061 return -ENODEV;
1063 tpm_transmit(chip, savestate, sizeof(savestate));
1064 return 0;
1066 EXPORT_SYMBOL_GPL(tpm_pm_suspend);
1069 * Resume from a power safe. The BIOS already restored
1070 * the TPM state.
1072 int tpm_pm_resume(struct device *dev)
1074 struct tpm_chip *chip = dev_get_drvdata(dev);
1076 if (chip == NULL)
1077 return -ENODEV;
1079 return 0;
1081 EXPORT_SYMBOL_GPL(tpm_pm_resume);
1084 * Called from tpm_<specific>.c probe function only for devices
1085 * the driver has determined it should claim. Prior to calling
1086 * this function the specific probe function has called pci_enable_device
1087 * upon errant exit from this function specific probe function should call
1088 * pci_disable_device
1090 struct tpm_chip *tpm_register_hardware(struct device *dev, const struct tpm_vendor_specific
1091 *entry)
1093 #define DEVNAME_SIZE 7
1095 char *devname;
1096 struct tpm_chip *chip;
1098 /* Driver specific per-device data */
1099 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1100 if (chip == NULL)
1101 return NULL;
1103 init_MUTEX(&chip->buffer_mutex);
1104 init_MUTEX(&chip->tpm_mutex);
1105 INIT_LIST_HEAD(&chip->list);
1107 INIT_WORK(&chip->work, timeout_work);
1109 setup_timer(&chip->user_read_timer, user_reader_timeout,
1110 (unsigned long)chip);
1112 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1114 chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1116 if (chip->dev_num >= TPM_NUM_DEVICES) {
1117 dev_err(dev, "No available tpm device numbers\n");
1118 kfree(chip);
1119 return NULL;
1120 } else if (chip->dev_num == 0)
1121 chip->vendor.miscdev.minor = TPM_MINOR;
1122 else
1123 chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1125 set_bit(chip->dev_num, dev_mask);
1127 devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1128 scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
1129 chip->vendor.miscdev.name = devname;
1131 chip->vendor.miscdev.parent = dev;
1132 chip->dev = get_device(dev);
1134 if (misc_register(&chip->vendor.miscdev)) {
1135 dev_err(chip->dev,
1136 "unable to misc_register %s, minor %d\n",
1137 chip->vendor.miscdev.name,
1138 chip->vendor.miscdev.minor);
1139 put_device(dev);
1140 clear_bit(chip->dev_num, dev_mask);
1141 kfree(chip);
1142 kfree(devname);
1143 return NULL;
1146 spin_lock(&driver_lock);
1148 dev_set_drvdata(dev, chip);
1150 list_add(&chip->list, &tpm_chip_list);
1152 spin_unlock(&driver_lock);
1154 if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1155 list_del(&chip->list);
1156 misc_deregister(&chip->vendor.miscdev);
1157 put_device(dev);
1158 clear_bit(chip->dev_num, dev_mask);
1159 kfree(chip);
1160 kfree(devname);
1161 return NULL;
1164 chip->bios_dir = tpm_bios_log_setup(devname);
1166 return chip;
1168 EXPORT_SYMBOL_GPL(tpm_register_hardware);
1170 MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
1171 MODULE_DESCRIPTION("TPM Driver");
1172 MODULE_VERSION("2.0");
1173 MODULE_LICENSE("GPL");