tpm.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. *
  10. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  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. */
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/fs.h>
  24. #include <linux/mutex.h>
  25. #include <linux/sched.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/io.h>
  29. #include <linux/tpm.h>
  30. enum tpm_timeout {
  31. TPM_TIMEOUT = 5, /* msecs */
  32. };
  33. /* TPM addresses */
  34. enum tpm_addr {
  35. TPM_SUPERIO_ADDR = 0x2E,
  36. TPM_ADDR = 0x4E,
  37. };
  38. extern ssize_t tpm_show_pubek(struct device *, struct device_attribute *attr,
  39. char *);
  40. extern ssize_t tpm_show_pcrs(struct device *, struct device_attribute *attr,
  41. char *);
  42. extern ssize_t tpm_show_caps(struct device *, struct device_attribute *attr,
  43. char *);
  44. extern ssize_t tpm_show_caps_1_2(struct device *, struct device_attribute *attr,
  45. char *);
  46. extern ssize_t tpm_store_cancel(struct device *, struct device_attribute *attr,
  47. const char *, size_t);
  48. extern ssize_t tpm_show_enabled(struct device *, struct device_attribute *attr,
  49. char *);
  50. extern ssize_t tpm_show_active(struct device *, struct device_attribute *attr,
  51. char *);
  52. extern ssize_t tpm_show_owned(struct device *, struct device_attribute *attr,
  53. char *);
  54. extern ssize_t tpm_show_temp_deactivated(struct device *,
  55. struct device_attribute *attr, char *);
  56. struct tpm_chip;
  57. struct tpm_vendor_specific {
  58. const u8 req_complete_mask;
  59. const u8 req_complete_val;
  60. const u8 req_canceled;
  61. void __iomem *iobase; /* ioremapped address */
  62. unsigned long base; /* TPM base address */
  63. int irq;
  64. int region_size;
  65. int have_region;
  66. int (*recv) (struct tpm_chip *, u8 *, size_t);
  67. int (*send) (struct tpm_chip *, u8 *, size_t);
  68. void (*cancel) (struct tpm_chip *);
  69. u8 (*status) (struct tpm_chip *);
  70. void (*release) (struct device *);
  71. struct miscdevice miscdev;
  72. struct attribute_group *attr_group;
  73. struct list_head list;
  74. int locality;
  75. unsigned long timeout_a, timeout_b, timeout_c, timeout_d; /* jiffies */
  76. unsigned long duration[3]; /* jiffies */
  77. wait_queue_head_t read_queue;
  78. wait_queue_head_t int_queue;
  79. };
  80. struct tpm_chip {
  81. struct device *dev; /* Device stuff */
  82. int dev_num; /* /dev/tpm# */
  83. unsigned long is_open; /* only one allowed */
  84. int time_expired;
  85. /* Data passed to and from the tpm via the read/write calls */
  86. u8 *data_buffer;
  87. atomic_t data_pending;
  88. struct mutex buffer_mutex;
  89. struct timer_list user_read_timer; /* user needs to claim result */
  90. struct work_struct work;
  91. struct mutex tpm_mutex; /* tpm is processing */
  92. struct tpm_vendor_specific vendor;
  93. struct dentry **bios_dir;
  94. struct list_head list;
  95. void (*release) (struct device *);
  96. };
  97. #define to_tpm_chip(n) container_of(n, struct tpm_chip, vendor)
  98. static inline int tpm_read_index(int base, int index)
  99. {
  100. outb(index, base);
  101. return inb(base+1) & 0xFF;
  102. }
  103. static inline void tpm_write_index(int base, int index, int value)
  104. {
  105. outb(index, base);
  106. outb(value & 0xFF, base+1);
  107. }
  108. struct tpm_input_header {
  109. __be16 tag;
  110. __be32 length;
  111. __be32 ordinal;
  112. }__attribute__((packed));
  113. struct tpm_output_header {
  114. __be16 tag;
  115. __be32 length;
  116. __be32 return_code;
  117. }__attribute__((packed));
  118. struct stclear_flags_t {
  119. __be16 tag;
  120. u8 deactivated;
  121. u8 disableForceClear;
  122. u8 physicalPresence;
  123. u8 physicalPresenceLock;
  124. u8 bGlobalLock;
  125. }__attribute__((packed));
  126. struct tpm_version_t {
  127. u8 Major;
  128. u8 Minor;
  129. u8 revMajor;
  130. u8 revMinor;
  131. }__attribute__((packed));
  132. struct tpm_version_1_2_t {
  133. __be16 tag;
  134. u8 Major;
  135. u8 Minor;
  136. u8 revMajor;
  137. u8 revMinor;
  138. }__attribute__((packed));
  139. struct timeout_t {
  140. __be32 a;
  141. __be32 b;
  142. __be32 c;
  143. __be32 d;
  144. }__attribute__((packed));
  145. struct duration_t {
  146. __be32 tpm_short;
  147. __be32 tpm_medium;
  148. __be32 tpm_long;
  149. }__attribute__((packed));
  150. struct permanent_flags_t {
  151. __be16 tag;
  152. u8 disable;
  153. u8 ownership;
  154. u8 deactivated;
  155. u8 readPubek;
  156. u8 disableOwnerClear;
  157. u8 allowMaintenance;
  158. u8 physicalPresenceLifetimeLock;
  159. u8 physicalPresenceHWEnable;
  160. u8 physicalPresenceCMDEnable;
  161. u8 CEKPUsed;
  162. u8 TPMpost;
  163. u8 TPMpostLock;
  164. u8 FIPS;
  165. u8 operator;
  166. u8 enableRevokeEK;
  167. u8 nvLocked;
  168. u8 readSRKPub;
  169. u8 tpmEstablished;
  170. u8 maintenanceDone;
  171. u8 disableFullDALogicInfo;
  172. }__attribute__((packed));
  173. typedef union {
  174. struct permanent_flags_t perm_flags;
  175. struct stclear_flags_t stclear_flags;
  176. bool owned;
  177. __be32 num_pcrs;
  178. struct tpm_version_t tpm_version;
  179. struct tpm_version_1_2_t tpm_version_1_2;
  180. __be32 manufacturer_id;
  181. struct timeout_t timeout;
  182. struct duration_t duration;
  183. } cap_t;
  184. struct tpm_getcap_params_in {
  185. __be32 cap;
  186. __be32 subcap_size;
  187. __be32 subcap;
  188. }__attribute__((packed));
  189. struct tpm_getcap_params_out {
  190. __be32 cap_size;
  191. cap_t cap;
  192. }__attribute__((packed));
  193. struct tpm_readpubek_params_out {
  194. u8 algorithm[4];
  195. u8 encscheme[2];
  196. u8 sigscheme[2];
  197. u8 parameters[12]; /*assuming RSA*/
  198. __be32 keysize;
  199. u8 modulus[256];
  200. u8 checksum[20];
  201. }__attribute__((packed));
  202. typedef union {
  203. struct tpm_input_header in;
  204. struct tpm_output_header out;
  205. } tpm_cmd_header;
  206. #define TPM_DIGEST_SIZE 20
  207. struct tpm_pcrread_out {
  208. u8 pcr_result[TPM_DIGEST_SIZE];
  209. }__attribute__((packed));
  210. struct tpm_pcrread_in {
  211. __be32 pcr_idx;
  212. }__attribute__((packed));
  213. struct tpm_pcrextend_in {
  214. __be32 pcr_idx;
  215. u8 hash[TPM_DIGEST_SIZE];
  216. }__attribute__((packed));
  217. typedef union {
  218. struct tpm_getcap_params_out getcap_out;
  219. struct tpm_readpubek_params_out readpubek_out;
  220. u8 readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
  221. struct tpm_getcap_params_in getcap_in;
  222. struct tpm_pcrread_in pcrread_in;
  223. struct tpm_pcrread_out pcrread_out;
  224. struct tpm_pcrextend_in pcrextend_in;
  225. } tpm_cmd_params;
  226. struct tpm_cmd_t {
  227. tpm_cmd_header header;
  228. tpm_cmd_params params;
  229. }__attribute__((packed));
  230. ssize_t tpm_getcap(struct device *, __be32, cap_t *, const char *);
  231. extern void tpm_get_timeouts(struct tpm_chip *);
  232. extern void tpm_gen_interrupt(struct tpm_chip *);
  233. extern void tpm_continue_selftest(struct tpm_chip *);
  234. extern unsigned long tpm_calc_ordinal_duration(struct tpm_chip *, u32);
  235. extern struct tpm_chip* tpm_register_hardware(struct device *,
  236. const struct tpm_vendor_specific *);
  237. extern int tpm_open(struct inode *, struct file *);
  238. extern int tpm_release(struct inode *, struct file *);
  239. extern void tpm_dev_vendor_release(struct tpm_chip *);
  240. extern ssize_t tpm_write(struct file *, const char __user *, size_t,
  241. loff_t *);
  242. extern ssize_t tpm_read(struct file *, char __user *, size_t, loff_t *);
  243. extern void tpm_remove_hardware(struct device *);
  244. extern int tpm_pm_suspend(struct device *, pm_message_t);
  245. extern int tpm_pm_resume(struct device *);
  246. #ifdef CONFIG_ACPI
  247. extern struct dentry ** tpm_bios_log_setup(char *);
  248. extern void tpm_bios_log_teardown(struct dentry **);
  249. #else
  250. static inline struct dentry ** tpm_bios_log_setup(char *name)
  251. {
  252. return NULL;
  253. }
  254. static inline void tpm_bios_log_teardown(struct dentry **dir)
  255. {
  256. }
  257. #endif