tpm.h 7.8 KB

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