Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tpm.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004 IBM Corporation
3  *
4  * Authors:
5  * Leendert van Doorn <[email protected]>
6  * Dave Safford <[email protected]>
7  * Reiner Sailer <[email protected]>
8  * Kylene Hall <[email protected]>
9  *
10  * Maintained by: <[email protected]>
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
22  * calls to msleep.
23  *
24  */
25 
26 #include <linux/poll.h>
27 #include <linux/slab.h>
28 #include <linux/mutex.h>
29 #include <linux/spinlock.h>
30 #include <linux/freezer.h>
31 
32 #include "tpm.h"
33 #include "tpm_eventlog.h"
34 
36  TPM_SHORT = 0,
38  TPM_LONG = 2,
40 };
41 
42 #define TPM_MAX_ORDINAL 243
43 #define TPM_MAX_PROTECTED_ORDINAL 12
44 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
45 
46 /*
47  * Bug workaround - some TPM's don't flush the most
48  * recently changed pcr on suspend, so force the flush
49  * with an extend to the selected _unused_ non-volatile pcr.
50  */
51 static int tpm_suspend_pcr;
52 module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
53 MODULE_PARM_DESC(suspend_pcr,
54  "PCR to use for dummy writes to faciltate flush on suspend.");
55 
56 static LIST_HEAD(tpm_chip_list);
57 static DEFINE_SPINLOCK(driver_lock);
58 static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
59 
60 /*
61  * Array with one entry per ordinal defining the maximum amount
62  * of time the chip could take to return the result. The ordinal
63  * designation of short, medium or long is defined in a table in
64  * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
65  * values of the SHORT, MEDIUM, and LONG durations are retrieved
66  * from the chip during initialization with a call to tpm_get_timeouts.
67  */
68 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
69  TPM_UNDEFINED, /* 0 */
74  TPM_UNDEFINED, /* 5 */
79  TPM_SHORT, /* 10 */
80  TPM_SHORT,
81 };
82 
83 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
84  TPM_UNDEFINED, /* 0 */
89  TPM_UNDEFINED, /* 5 */
94  TPM_SHORT, /* 10 */
95  TPM_SHORT,
96  TPM_MEDIUM,
97  TPM_LONG,
98  TPM_LONG,
99  TPM_MEDIUM, /* 15 */
100  TPM_SHORT,
101  TPM_SHORT,
102  TPM_MEDIUM,
103  TPM_LONG,
104  TPM_SHORT, /* 20 */
105  TPM_SHORT,
106  TPM_MEDIUM,
107  TPM_MEDIUM,
108  TPM_MEDIUM,
109  TPM_SHORT, /* 25 */
110  TPM_SHORT,
111  TPM_MEDIUM,
112  TPM_SHORT,
113  TPM_SHORT,
114  TPM_MEDIUM, /* 30 */
115  TPM_LONG,
116  TPM_MEDIUM,
117  TPM_SHORT,
118  TPM_SHORT,
119  TPM_SHORT, /* 35 */
120  TPM_MEDIUM,
121  TPM_MEDIUM,
124  TPM_MEDIUM, /* 40 */
125  TPM_LONG,
126  TPM_MEDIUM,
127  TPM_SHORT,
128  TPM_SHORT,
129  TPM_SHORT, /* 45 */
130  TPM_SHORT,
131  TPM_SHORT,
132  TPM_SHORT,
133  TPM_LONG,
134  TPM_MEDIUM, /* 50 */
135  TPM_MEDIUM,
139  TPM_UNDEFINED, /* 55 */
144  TPM_MEDIUM, /* 60 */
145  TPM_MEDIUM,
146  TPM_MEDIUM,
147  TPM_SHORT,
148  TPM_SHORT,
149  TPM_MEDIUM, /* 65 */
154  TPM_SHORT, /* 70 */
155  TPM_SHORT,
159  TPM_UNDEFINED, /* 75 */
164  TPM_LONG, /* 80 */
166  TPM_MEDIUM,
167  TPM_LONG,
168  TPM_SHORT,
169  TPM_UNDEFINED, /* 85 */
174  TPM_SHORT, /* 90 */
175  TPM_SHORT,
176  TPM_SHORT,
177  TPM_SHORT,
178  TPM_SHORT,
179  TPM_UNDEFINED, /* 95 */
184  TPM_MEDIUM, /* 100 */
185  TPM_SHORT,
186  TPM_SHORT,
189  TPM_UNDEFINED, /* 105 */
194  TPM_SHORT, /* 110 */
195  TPM_SHORT,
196  TPM_SHORT,
197  TPM_SHORT,
198  TPM_SHORT,
199  TPM_SHORT, /* 115 */
200  TPM_SHORT,
201  TPM_SHORT,
204  TPM_LONG, /* 120 */
205  TPM_LONG,
206  TPM_MEDIUM,
208  TPM_SHORT,
209  TPM_SHORT, /* 125 */
210  TPM_SHORT,
211  TPM_LONG,
212  TPM_SHORT,
213  TPM_SHORT,
214  TPM_SHORT, /* 130 */
215  TPM_MEDIUM,
217  TPM_SHORT,
218  TPM_MEDIUM,
219  TPM_UNDEFINED, /* 135 */
224  TPM_SHORT, /* 140 */
225  TPM_SHORT,
229  TPM_UNDEFINED, /* 145 */
234  TPM_SHORT, /* 150 */
235  TPM_MEDIUM,
236  TPM_MEDIUM,
237  TPM_SHORT,
238  TPM_SHORT,
239  TPM_UNDEFINED, /* 155 */
244  TPM_SHORT, /* 160 */
245  TPM_SHORT,
246  TPM_SHORT,
247  TPM_SHORT,
249  TPM_UNDEFINED, /* 165 */
254  TPM_LONG, /* 170 */
259  TPM_UNDEFINED, /* 175 */
264  TPM_MEDIUM, /* 180 */
265  TPM_SHORT,
266  TPM_MEDIUM,
267  TPM_MEDIUM,
268  TPM_MEDIUM,
269  TPM_MEDIUM, /* 185 */
270  TPM_SHORT,
274  TPM_UNDEFINED, /* 190 */
279  TPM_UNDEFINED, /* 195 */
284  TPM_SHORT, /* 200 */
288  TPM_SHORT,
289  TPM_SHORT, /* 205 */
290  TPM_SHORT,
291  TPM_SHORT,
292  TPM_SHORT,
293  TPM_SHORT,
294  TPM_MEDIUM, /* 210 */
296  TPM_MEDIUM,
297  TPM_MEDIUM,
298  TPM_MEDIUM,
299  TPM_UNDEFINED, /* 215 */
300  TPM_MEDIUM,
303  TPM_SHORT,
304  TPM_SHORT, /* 220 */
305  TPM_SHORT,
306  TPM_SHORT,
307  TPM_SHORT,
308  TPM_SHORT,
309  TPM_UNDEFINED, /* 225 */
314  TPM_SHORT, /* 230 */
315  TPM_LONG,
316  TPM_MEDIUM,
319  TPM_UNDEFINED, /* 235 */
324  TPM_SHORT, /* 240 */
326  TPM_MEDIUM,
327 };
328 
329 static void user_reader_timeout(unsigned long ptr)
330 {
331  struct tpm_chip *chip = (struct tpm_chip *) ptr;
332 
333  schedule_work(&chip->work);
334 }
335 
336 static void timeout_work(struct work_struct *work)
337 {
338  struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
339 
340  mutex_lock(&chip->buffer_mutex);
341  atomic_set(&chip->data_pending, 0);
342  memset(chip->data_buffer, 0, TPM_BUFSIZE);
343  mutex_unlock(&chip->buffer_mutex);
344 }
345 
346 /*
347  * Returns max number of jiffies to wait
348  */
350  u32 ordinal)
351 {
352  int duration_idx = TPM_UNDEFINED;
353  int duration = 0;
354 
355  if (ordinal < TPM_MAX_ORDINAL)
356  duration_idx = tpm_ordinal_duration[ordinal];
357  else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
359  duration_idx =
360  tpm_protected_ordinal_duration[ordinal &
362 
363  if (duration_idx != TPM_UNDEFINED)
364  duration = chip->vendor.duration[duration_idx];
365  if (duration <= 0)
366  return 2 * 60 * HZ;
367  else
368  return duration;
369 }
371 
372 /*
373  * Internal kernel interface to transmit TPM commands
374  */
375 static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
376  size_t bufsiz)
377 {
378  ssize_t rc;
379  u32 count, ordinal;
380  unsigned long stop;
381 
382  if (bufsiz > TPM_BUFSIZE)
383  bufsiz = TPM_BUFSIZE;
384 
385  count = be32_to_cpu(*((__be32 *) (buf + 2)));
386  ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
387  if (count == 0)
388  return -ENODATA;
389  if (count > bufsiz) {
390  dev_err(chip->dev,
391  "invalid count value %x %zx \n", count, bufsiz);
392  return -E2BIG;
393  }
394 
395  mutex_lock(&chip->tpm_mutex);
396 
397  if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
398  dev_err(chip->dev,
399  "tpm_transmit: tpm_send: error %zd\n", rc);
400  goto out;
401  }
402 
403  if (chip->vendor.irq)
404  goto out_recv;
405 
406  stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
407  do {
408  u8 status = chip->vendor.status(chip);
409  if ((status & chip->vendor.req_complete_mask) ==
410  chip->vendor.req_complete_val)
411  goto out_recv;
412 
413  if ((status == chip->vendor.req_canceled)) {
414  dev_err(chip->dev, "Operation Canceled\n");
415  rc = -ECANCELED;
416  goto out;
417  }
418 
419  msleep(TPM_TIMEOUT); /* CHECK */
420  rmb();
421  } while (time_before(jiffies, stop));
422 
423  chip->vendor.cancel(chip);
424  dev_err(chip->dev, "Operation Timed out\n");
425  rc = -ETIME;
426  goto out;
427 
428 out_recv:
429  rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
430  if (rc < 0)
431  dev_err(chip->dev,
432  "tpm_transmit: tpm_recv: error %zd\n", rc);
433 out:
434  mutex_unlock(&chip->tpm_mutex);
435  return rc;
436 }
437 
438 #define TPM_DIGEST_SIZE 20
439 #define TPM_RET_CODE_IDX 6
440 
446 };
447 
456 
457 };
458 
459 static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
460  int len, const char *desc)
461 {
462  int err;
463 
464  len = tpm_transmit(chip,(u8 *) cmd, len);
465  if (len < 0)
466  return len;
467  else if (len < TPM_HEADER_SIZE)
468  return -EFAULT;
469 
470  err = be32_to_cpu(cmd->header.out.return_code);
471  if (err != 0)
472  dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
473 
474  return err;
475 }
476 
477 #define TPM_INTERNAL_RESULT_SIZE 200
478 #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
479 #define TPM_ORD_GET_CAP cpu_to_be32(101)
480 #define TPM_ORD_GET_RANDOM cpu_to_be32(70)
481 
482 static const struct tpm_input_header tpm_getcap_header = {
483  .tag = TPM_TAG_RQU_COMMAND,
484  .length = cpu_to_be32(22),
485  .ordinal = TPM_ORD_GET_CAP
486 };
487 
488 ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
489  const char *desc)
490 {
491  struct tpm_cmd_t tpm_cmd;
492  int rc;
493  struct tpm_chip *chip = dev_get_drvdata(dev);
494 
495  tpm_cmd.header.in = tpm_getcap_header;
496  if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
497  tpm_cmd.params.getcap_in.cap = subcap_id;
498  /*subcap field not necessary */
499  tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
500  tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
501  } else {
502  if (subcap_id == TPM_CAP_FLAG_PERM ||
503  subcap_id == TPM_CAP_FLAG_VOL)
504  tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
505  else
506  tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
507  tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
508  tpm_cmd.params.getcap_in.subcap = subcap_id;
509  }
510  rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
511  if (!rc)
512  *cap = tpm_cmd.params.getcap_out.cap;
513  return rc;
514 }
515 
516 void tpm_gen_interrupt(struct tpm_chip *chip)
517 {
518  struct tpm_cmd_t tpm_cmd;
519  ssize_t rc;
520 
521  tpm_cmd.header.in = tpm_getcap_header;
522  tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
523  tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
524  tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
525 
526  rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
527  "attempting to determine the timeouts");
528 }
530 
531 int tpm_get_timeouts(struct tpm_chip *chip)
532 {
533  struct tpm_cmd_t tpm_cmd;
534  struct timeout_t *timeout_cap;
535  struct duration_t *duration_cap;
536  ssize_t rc;
537  u32 timeout;
538  unsigned int scale = 1;
539 
540  tpm_cmd.header.in = tpm_getcap_header;
541  tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
542  tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
543  tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
544 
545  rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
546  "attempting to determine the timeouts");
547  if (rc)
548  goto duration;
549 
550  if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
551  be32_to_cpu(tpm_cmd.header.out.length)
552  != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
553  return -EINVAL;
554 
555  timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
556  /* Don't overwrite default if value is 0 */
557  timeout = be32_to_cpu(timeout_cap->a);
558  if (timeout && timeout < 1000) {
559  /* timeouts in msec rather usec */
560  scale = 1000;
561  chip->vendor.timeout_adjusted = true;
562  }
563  if (timeout)
564  chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale);
565  timeout = be32_to_cpu(timeout_cap->b);
566  if (timeout)
567  chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale);
568  timeout = be32_to_cpu(timeout_cap->c);
569  if (timeout)
570  chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale);
571  timeout = be32_to_cpu(timeout_cap->d);
572  if (timeout)
573  chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale);
574 
575 duration:
576  tpm_cmd.header.in = tpm_getcap_header;
577  tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
578  tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
579  tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
580 
581  rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
582  "attempting to determine the durations");
583  if (rc)
584  return rc;
585 
586  if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
587  be32_to_cpu(tpm_cmd.header.out.length)
588  != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
589  return -EINVAL;
590 
591  duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
592  chip->vendor.duration[TPM_SHORT] =
593  usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
594  chip->vendor.duration[TPM_MEDIUM] =
595  usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
596  chip->vendor.duration[TPM_LONG] =
597  usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
598 
599  /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
600  * value wrong and apparently reports msecs rather than usecs. So we
601  * fix up the resulting too-small TPM_SHORT value to make things work.
602  * We also scale the TPM_MEDIUM and -_LONG values by 1000.
603  */
604  if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
605  chip->vendor.duration[TPM_SHORT] = HZ;
606  chip->vendor.duration[TPM_MEDIUM] *= 1000;
607  chip->vendor.duration[TPM_LONG] *= 1000;
608  chip->vendor.duration_adjusted = true;
609  dev_info(chip->dev, "Adjusting TPM timeout parameters.");
610  }
611  return 0;
612 }
614 
615 #define TPM_ORD_CONTINUE_SELFTEST 83
616 #define CONTINUE_SELFTEST_RESULT_SIZE 10
617 
618 static struct tpm_input_header continue_selftest_header = {
619  .tag = TPM_TAG_RQU_COMMAND,
620  .length = cpu_to_be32(10),
622 };
623 
631 static int tpm_continue_selftest(struct tpm_chip *chip)
632 {
633  int rc;
634  struct tpm_cmd_t cmd;
635 
636  cmd.header.in = continue_selftest_header;
637  rc = transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
638  "continue selftest");
639  return rc;
640 }
641 
643  char *buf)
644 {
645  cap_t cap;
646  ssize_t rc;
647 
648  rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
649  "attempting to determine the permanent enabled state");
650  if (rc)
651  return 0;
652 
653  rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
654  return rc;
655 }
657 
659  char *buf)
660 {
661  cap_t cap;
662  ssize_t rc;
663 
664  rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
665  "attempting to determine the permanent active state");
666  if (rc)
667  return 0;
668 
669  rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
670  return rc;
671 }
673 
675  char *buf)
676 {
677  cap_t cap;
678  ssize_t rc;
679 
680  rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
681  "attempting to determine the owner state");
682  if (rc)
683  return 0;
684 
685  rc = sprintf(buf, "%d\n", cap.owned);
686  return rc;
687 }
689 
691  struct device_attribute * attr, char *buf)
692 {
693  cap_t cap;
694  ssize_t rc;
695 
696  rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
697  "attempting to determine the temporary state");
698  if (rc)
699  return 0;
700 
701  rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
702  return rc;
703 }
705 
706 /*
707  * tpm_chip_find_get - return tpm_chip for given chip number
708  */
709 static struct tpm_chip *tpm_chip_find_get(int chip_num)
710 {
711  struct tpm_chip *pos, *chip = NULL;
712 
713  rcu_read_lock();
714  list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
715  if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
716  continue;
717 
718  if (try_module_get(pos->dev->driver->owner)) {
719  chip = pos;
720  break;
721  }
722  }
723  rcu_read_unlock();
724  return chip;
725 }
726 
727 #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
728 #define READ_PCR_RESULT_SIZE 30
729 static struct tpm_input_header pcrread_header = {
730  .tag = TPM_TAG_RQU_COMMAND,
731  .length = cpu_to_be32(14),
732  .ordinal = TPM_ORDINAL_PCRREAD
733 };
734 
735 static int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
736 {
737  int rc;
738  struct tpm_cmd_t cmd;
739 
740  cmd.header.in = pcrread_header;
741  cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
742  rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
743  "attempting to read a pcr value");
744 
745  if (rc == 0)
746  memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
748  return rc;
749 }
750 
762 int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
763 {
764  struct tpm_chip *chip;
765  int rc;
766 
767  chip = tpm_chip_find_get(chip_num);
768  if (chip == NULL)
769  return -ENODEV;
770  rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
771  tpm_chip_put(chip);
772  return rc;
773 }
775 
786 #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
787 #define EXTEND_PCR_RESULT_SIZE 34
788 static struct tpm_input_header pcrextend_header = {
789  .tag = TPM_TAG_RQU_COMMAND,
790  .length = cpu_to_be32(34),
791  .ordinal = TPM_ORD_PCR_EXTEND
792 };
793 
794 int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
795 {
796  struct tpm_cmd_t cmd;
797  int rc;
798  struct tpm_chip *chip;
799 
800  chip = tpm_chip_find_get(chip_num);
801  if (chip == NULL)
802  return -ENODEV;
803 
804  cmd.header.in = pcrextend_header;
805  cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
806  memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
807  rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
808  "attempting extend a PCR value");
809 
810  tpm_chip_put(chip);
811  return rc;
812 }
814 
823 int tpm_do_selftest(struct tpm_chip *chip)
824 {
825  int rc;
826  unsigned int loops;
827  unsigned int delay_msec = 1000;
828  unsigned long duration;
829  struct tpm_cmd_t cmd;
830 
831  duration = tpm_calc_ordinal_duration(chip,
833 
834  loops = jiffies_to_msecs(duration) / delay_msec;
835 
836  rc = tpm_continue_selftest(chip);
837  /* This may fail if there was no TPM driver during a suspend/resume
838  * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
839  */
840  if (rc)
841  return rc;
842 
843  do {
844  /* Attempt to read a PCR value */
845  cmd.header.in = pcrread_header;
846  cmd.params.pcrread_in.pcr_idx = cpu_to_be32(0);
847  rc = tpm_transmit(chip, (u8 *) &cmd, READ_PCR_RESULT_SIZE);
848 
849  if (rc < TPM_HEADER_SIZE)
850  return -EFAULT;
851 
852  rc = be32_to_cpu(cmd.header.out.return_code);
853  if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
854  dev_info(chip->dev,
855  "TPM is disabled/deactivated (0x%X)\n", rc);
856  /* TPM is disabled and/or deactivated; driver can
857  * proceed and TPM does handle commands for
858  * suspend/resume correctly
859  */
860  return 0;
861  }
862  if (rc != TPM_WARN_DOING_SELFTEST)
863  return rc;
864  msleep(delay_msec);
865  } while (--loops > 0);
866 
867  return rc;
868 }
870 
871 int tpm_send(u32 chip_num, void *cmd, size_t buflen)
872 {
873  struct tpm_chip *chip;
874  int rc;
875 
876  chip = tpm_chip_find_get(chip_num);
877  if (chip == NULL)
878  return -ENODEV;
879 
880  rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
881 
882  tpm_chip_put(chip);
883  return rc;
884 }
886 
888  char *buf)
889 {
890  cap_t cap;
892  ssize_t rc;
893  int i, j, num_pcrs;
894  char *str = buf;
895  struct tpm_chip *chip = dev_get_drvdata(dev);
896 
897  rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
898  "attempting to determine the number of PCRS");
899  if (rc)
900  return 0;
901 
902  num_pcrs = be32_to_cpu(cap.num_pcrs);
903  for (i = 0; i < num_pcrs; i++) {
904  rc = __tpm_pcr_read(chip, i, digest);
905  if (rc)
906  break;
907  str += sprintf(str, "PCR-%02d: ", i);
908  for (j = 0; j < TPM_DIGEST_SIZE; j++)
909  str += sprintf(str, "%02X ", digest[j]);
910  str += sprintf(str, "\n");
911  }
912  return str - buf;
913 }
915 
916 #define READ_PUBEK_RESULT_SIZE 314
917 #define TPM_ORD_READPUBEK cpu_to_be32(124)
918 static struct tpm_input_header tpm_readpubek_header = {
919  .tag = TPM_TAG_RQU_COMMAND,
920  .length = cpu_to_be32(30),
921  .ordinal = TPM_ORD_READPUBEK
922 };
923 
925  char *buf)
926 {
927  u8 *data;
928  struct tpm_cmd_t tpm_cmd;
929  ssize_t err;
930  int i, rc;
931  char *str = buf;
932 
933  struct tpm_chip *chip = dev_get_drvdata(dev);
934 
935  tpm_cmd.header.in = tpm_readpubek_header;
936  err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
937  "attempting to read the PUBEK");
938  if (err)
939  goto out;
940 
941  /*
942  ignore header 10 bytes
943  algorithm 32 bits (1 == RSA )
944  encscheme 16 bits
945  sigscheme 16 bits
946  parameters (RSA 12->bytes: keybit, #primes, expbit)
947  keylenbytes 32 bits
948  256 byte modulus
949  ignore checksum 20 bytes
950  */
951  data = tpm_cmd.params.readpubek_out_buffer;
952  str +=
953  sprintf(str,
954  "Algorithm: %02X %02X %02X %02X\n"
955  "Encscheme: %02X %02X\n"
956  "Sigscheme: %02X %02X\n"
957  "Parameters: %02X %02X %02X %02X "
958  "%02X %02X %02X %02X "
959  "%02X %02X %02X %02X\n"
960  "Modulus length: %d\n"
961  "Modulus:\n",
962  data[0], data[1], data[2], data[3],
963  data[4], data[5],
964  data[6], data[7],
965  data[12], data[13], data[14], data[15],
966  data[16], data[17], data[18], data[19],
967  data[20], data[21], data[22], data[23],
968  be32_to_cpu(*((__be32 *) (data + 24))));
969 
970  for (i = 0; i < 256; i++) {
971  str += sprintf(str, "%02X ", data[i + 28]);
972  if ((i + 1) % 16 == 0)
973  str += sprintf(str, "\n");
974  }
975 out:
976  rc = str - buf;
977  return rc;
978 }
980 
981 
983  char *buf)
984 {
985  cap_t cap;
986  ssize_t rc;
987  char *str = buf;
988 
989  rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
990  "attempting to determine the manufacturer");
991  if (rc)
992  return 0;
993  str += sprintf(str, "Manufacturer: 0x%x\n",
995 
996  rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
997  "attempting to determine the 1.1 version");
998  if (rc)
999  return 0;
1000  str += sprintf(str,
1001  "TCG version: %d.%d\nFirmware version: %d.%d\n",
1002  cap.tpm_version.Major, cap.tpm_version.Minor,
1003  cap.tpm_version.revMajor, cap.tpm_version.revMinor);
1004  return str - buf;
1005 }
1007 
1009  struct device_attribute * attr, char *buf)
1010 {
1011  cap_t cap;
1012  ssize_t rc;
1013  char *str = buf;
1014 
1015  rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
1016  "attempting to determine the manufacturer");
1017  if (rc)
1018  return 0;
1019  str += sprintf(str, "Manufacturer: 0x%x\n",
1021  rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
1022  "attempting to determine the 1.2 version");
1023  if (rc)
1024  return 0;
1025  str += sprintf(str,
1026  "TCG version: %d.%d\nFirmware version: %d.%d\n",
1027  cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
1028  cap.tpm_version_1_2.revMajor,
1029  cap.tpm_version_1_2.revMinor);
1030  return str - buf;
1031 }
1033 
1035  char *buf)
1036 {
1037  struct tpm_chip *chip = dev_get_drvdata(dev);
1038 
1039  if (chip->vendor.duration[TPM_LONG] == 0)
1040  return 0;
1041 
1042  return sprintf(buf, "%d %d %d [%s]\n",
1043  jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
1044  jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
1045  jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
1046  chip->vendor.duration_adjusted
1047  ? "adjusted" : "original");
1048 }
1050 
1052  char *buf)
1053 {
1054  struct tpm_chip *chip = dev_get_drvdata(dev);
1055 
1056  return sprintf(buf, "%d %d %d %d [%s]\n",
1057  jiffies_to_usecs(chip->vendor.timeout_a),
1058  jiffies_to_usecs(chip->vendor.timeout_b),
1059  jiffies_to_usecs(chip->vendor.timeout_c),
1060  jiffies_to_usecs(chip->vendor.timeout_d),
1061  chip->vendor.timeout_adjusted
1062  ? "adjusted" : "original");
1063 }
1065 
1067  const char *buf, size_t count)
1068 {
1069  struct tpm_chip *chip = dev_get_drvdata(dev);
1070  if (chip == NULL)
1071  return 0;
1072 
1073  chip->vendor.cancel(chip);
1074  return count;
1075 }
1077 
1078 int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
1080 {
1081  unsigned long stop;
1082  long rc;
1083  u8 status;
1084 
1085  /* check current status */
1086  status = chip->vendor.status(chip);
1087  if ((status & mask) == mask)
1088  return 0;
1089 
1090  stop = jiffies + timeout;
1091 
1092  if (chip->vendor.irq) {
1093 again:
1094  timeout = stop - jiffies;
1095  if ((long)timeout <= 0)
1096  return -ETIME;
1098  ((chip->vendor.status(chip)
1099  & mask) == mask),
1100  timeout);
1101  if (rc > 0)
1102  return 0;
1103  if (rc == -ERESTARTSYS && freezing(current)) {
1104  clear_thread_flag(TIF_SIGPENDING);
1105  goto again;
1106  }
1107  } else {
1108  do {
1110  status = chip->vendor.status(chip);
1111  if ((status & mask) == mask)
1112  return 0;
1113  } while (time_before(jiffies, stop));
1114  }
1115  return -ETIME;
1116 }
1118 /*
1119  * Device file system interface to the TPM
1120  *
1121  * It's assured that the chip will be opened just once,
1122  * by the check of is_open variable, which is protected
1123  * by driver_lock.
1124  */
1125 int tpm_open(struct inode *inode, struct file *file)
1126 {
1127  int minor = iminor(inode);
1128  struct tpm_chip *chip = NULL, *pos;
1129 
1130  rcu_read_lock();
1131  list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
1132  if (pos->vendor.miscdev.minor == minor) {
1133  chip = pos;
1134  get_device(chip->dev);
1135  break;
1136  }
1137  }
1138  rcu_read_unlock();
1139 
1140  if (!chip)
1141  return -ENODEV;
1142 
1143  if (test_and_set_bit(0, &chip->is_open)) {
1144  dev_dbg(chip->dev, "Another process owns this TPM\n");
1145  put_device(chip->dev);
1146  return -EBUSY;
1147  }
1148 
1149  chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
1150  if (chip->data_buffer == NULL) {
1151  clear_bit(0, &chip->is_open);
1152  put_device(chip->dev);
1153  return -ENOMEM;
1154  }
1155 
1156  atomic_set(&chip->data_pending, 0);
1157 
1158  file->private_data = chip;
1159  return 0;
1160 }
1162 
1163 /*
1164  * Called on file close
1165  */
1166 int tpm_release(struct inode *inode, struct file *file)
1167 {
1168  struct tpm_chip *chip = file->private_data;
1169 
1171  flush_work(&chip->work);
1172  file->private_data = NULL;
1173  atomic_set(&chip->data_pending, 0);
1174  kzfree(chip->data_buffer);
1175  clear_bit(0, &chip->is_open);
1176  put_device(chip->dev);
1177  return 0;
1178 }
1180 
1181 ssize_t tpm_write(struct file *file, const char __user *buf,
1182  size_t size, loff_t *off)
1183 {
1184  struct tpm_chip *chip = file->private_data;
1185  size_t in_size = size;
1186  ssize_t out_size;
1187 
1188  /* cannot perform a write until the read has cleared
1189  either via tpm_read or a user_read_timer timeout.
1190  This also prevents splitted buffered writes from blocking here.
1191  */
1192  if (atomic_read(&chip->data_pending) != 0)
1193  return -EBUSY;
1194 
1195  if (in_size > TPM_BUFSIZE)
1196  return -E2BIG;
1197 
1198  mutex_lock(&chip->buffer_mutex);
1199 
1200  if (copy_from_user
1201  (chip->data_buffer, (void __user *) buf, in_size)) {
1202  mutex_unlock(&chip->buffer_mutex);
1203  return -EFAULT;
1204  }
1205 
1206  /* atomic tpm command send and result receive */
1207  out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
1208  if (out_size < 0) {
1209  mutex_unlock(&chip->buffer_mutex);
1210  return out_size;
1211  }
1212 
1213  atomic_set(&chip->data_pending, out_size);
1214  mutex_unlock(&chip->buffer_mutex);
1215 
1216  /* Set a timeout by which the reader must come claim the result */
1217  mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
1218 
1219  return in_size;
1220 }
1222 
1223 ssize_t tpm_read(struct file *file, char __user *buf,
1224  size_t size, loff_t *off)
1225 {
1226  struct tpm_chip *chip = file->private_data;
1227  ssize_t ret_size;
1228  int rc;
1229 
1231  flush_work(&chip->work);
1232  ret_size = atomic_read(&chip->data_pending);
1233  if (ret_size > 0) { /* relay data */
1234  ssize_t orig_ret_size = ret_size;
1235  if (size < ret_size)
1236  ret_size = size;
1237 
1238  mutex_lock(&chip->buffer_mutex);
1239  rc = copy_to_user(buf, chip->data_buffer, ret_size);
1240  memset(chip->data_buffer, 0, orig_ret_size);
1241  if (rc)
1242  ret_size = -EFAULT;
1243 
1244  mutex_unlock(&chip->buffer_mutex);
1245  }
1246 
1247  atomic_set(&chip->data_pending, 0);
1248 
1249  return ret_size;
1250 }
1252 
1254 {
1255  struct tpm_chip *chip = dev_get_drvdata(dev);
1256 
1257  if (chip == NULL) {
1258  dev_err(dev, "No device data found\n");
1259  return;
1260  }
1261 
1262  spin_lock(&driver_lock);
1263  list_del_rcu(&chip->list);
1264  spin_unlock(&driver_lock);
1265  synchronize_rcu();
1266 
1267  misc_deregister(&chip->vendor.miscdev);
1268  sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
1269  tpm_remove_ppi(&dev->kobj);
1271 
1272  /* write it this way to be explicit (chip->dev == dev) */
1273  put_device(chip->dev);
1274 }
1276 
1277 #define TPM_ORD_SAVESTATE cpu_to_be32(152)
1278 #define SAVESTATE_RESULT_SIZE 10
1279 
1280 static struct tpm_input_header savestate_header = {
1281  .tag = TPM_TAG_RQU_COMMAND,
1282  .length = cpu_to_be32(10),
1283  .ordinal = TPM_ORD_SAVESTATE
1284 };
1285 
1286 /*
1287  * We are about to suspend. Save the TPM state
1288  * so that it can be restored.
1289  */
1291 {
1292  struct tpm_chip *chip = dev_get_drvdata(dev);
1293  struct tpm_cmd_t cmd;
1294  int rc;
1295 
1296  u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
1297 
1298  if (chip == NULL)
1299  return -ENODEV;
1300 
1301  /* for buggy tpm, flush pcrs with extend to selected dummy */
1302  if (tpm_suspend_pcr) {
1303  cmd.header.in = pcrextend_header;
1304  cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
1305  memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
1306  TPM_DIGEST_SIZE);
1307  rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
1308  "extending dummy pcr before suspend");
1309  }
1310 
1311  /* now do the actual savestate */
1312  cmd.header.in = savestate_header;
1313  rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
1314  "sending savestate before suspend");
1315  return rc;
1316 }
1318 
1319 /*
1320  * Resume from a power safe. The BIOS already restored
1321  * the TPM state.
1322  */
1324 {
1325  struct tpm_chip *chip = dev_get_drvdata(dev);
1326 
1327  if (chip == NULL)
1328  return -ENODEV;
1329 
1330  return 0;
1331 }
1333 
1334 #define TPM_GETRANDOM_RESULT_SIZE 18
1335 static struct tpm_input_header tpm_getrandom_header = {
1336  .tag = TPM_TAG_RQU_COMMAND,
1337  .length = cpu_to_be32(14),
1338  .ordinal = TPM_ORD_GET_RANDOM
1339 };
1340 
1349 int tpm_get_random(u32 chip_num, u8 *out, size_t max)
1350 {
1351  struct tpm_chip *chip;
1352  struct tpm_cmd_t tpm_cmd;
1353  u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
1354  int err, total = 0, retries = 5;
1355  u8 *dest = out;
1356 
1357  chip = tpm_chip_find_get(chip_num);
1358  if (chip == NULL)
1359  return -ENODEV;
1360 
1361  if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
1362  return -EINVAL;
1363 
1364  do {
1365  tpm_cmd.header.in = tpm_getrandom_header;
1366  tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
1367 
1368  err = transmit_cmd(chip, &tpm_cmd,
1369  TPM_GETRANDOM_RESULT_SIZE + num_bytes,
1370  "attempting get random");
1371  if (err)
1372  break;
1373 
1374  recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
1375  memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
1376 
1377  dest += recd;
1378  total += recd;
1379  num_bytes -= recd;
1380  } while (retries-- && total < max);
1381 
1382  return total ? total : -EIO;
1383 }
1385 
1386 /* In case vendor provided release function, call it too.*/
1387 
1389 {
1390  if (!chip)
1391  return;
1392 
1393  if (chip->vendor.release)
1394  chip->vendor.release(chip->dev);
1395 
1396  clear_bit(chip->dev_num, dev_mask);
1397  kfree(chip->vendor.miscdev.name);
1398 }
1400 
1401 
1402 /*
1403  * Once all references to platform device are down to 0,
1404  * release all allocated structures.
1405  */
1406 static void tpm_dev_release(struct device *dev)
1407 {
1408  struct tpm_chip *chip = dev_get_drvdata(dev);
1409 
1410  if (!chip)
1411  return;
1412 
1413  tpm_dev_vendor_release(chip);
1414 
1415  chip->release(dev);
1416  kfree(chip);
1417 }
1418 EXPORT_SYMBOL_GPL(tpm_dev_release);
1419 
1420 /*
1421  * Called from tpm_<specific>.c probe function only for devices
1422  * the driver has determined it should claim. Prior to calling
1423  * this function the specific probe function has called pci_enable_device
1424  * upon errant exit from this function specific probe function should call
1425  * pci_disable_device
1426  */
1428  const struct tpm_vendor_specific *entry)
1429 {
1430 #define DEVNAME_SIZE 7
1431 
1432  char *devname;
1433  struct tpm_chip *chip;
1434 
1435  /* Driver specific per-device data */
1436  chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1437  devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
1438 
1439  if (chip == NULL || devname == NULL)
1440  goto out_free;
1441 
1442  mutex_init(&chip->buffer_mutex);
1443  mutex_init(&chip->tpm_mutex);
1444  INIT_LIST_HEAD(&chip->list);
1445 
1446  INIT_WORK(&chip->work, timeout_work);
1447 
1448  setup_timer(&chip->user_read_timer, user_reader_timeout,
1449  (unsigned long)chip);
1450 
1451  memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1452 
1453  chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
1454 
1455  if (chip->dev_num >= TPM_NUM_DEVICES) {
1456  dev_err(dev, "No available tpm device numbers\n");
1457  goto out_free;
1458  } else if (chip->dev_num == 0)
1459  chip->vendor.miscdev.minor = TPM_MINOR;
1460  else
1461  chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
1462 
1463  set_bit(chip->dev_num, dev_mask);
1464 
1465  scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
1466  chip->vendor.miscdev.name = devname;
1467 
1468  chip->vendor.miscdev.parent = dev;
1469  chip->dev = get_device(dev);
1470  chip->release = dev->release;
1471  dev->release = tpm_dev_release;
1472  dev_set_drvdata(dev, chip);
1473 
1474  if (misc_register(&chip->vendor.miscdev)) {
1475  dev_err(chip->dev,
1476  "unable to misc_register %s, minor %d\n",
1477  chip->vendor.miscdev.name,
1478  chip->vendor.miscdev.minor);
1479  goto put_device;
1480  }
1481 
1482  if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
1483  misc_deregister(&chip->vendor.miscdev);
1484  goto put_device;
1485  }
1486 
1487  if (tpm_add_ppi(&dev->kobj)) {
1488  misc_deregister(&chip->vendor.miscdev);
1489  goto put_device;
1490  }
1491 
1492  chip->bios_dir = tpm_bios_log_setup(devname);
1493 
1494  /* Make chip available */
1495  spin_lock(&driver_lock);
1496  list_add_rcu(&chip->list, &tpm_chip_list);
1497  spin_unlock(&driver_lock);
1498 
1499  return chip;
1500 
1501 put_device:
1502  put_device(chip->dev);
1503 out_free:
1504  kfree(chip);
1505  kfree(devname);
1506  return NULL;
1507 }
1509 
1510 MODULE_AUTHOR("Leendert van Doorn ([email protected])");
1511 MODULE_DESCRIPTION("TPM Driver");
1512 MODULE_VERSION("2.0");
1513 MODULE_LICENSE("GPL");