tpm.h 7.4 KB

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