tpm.h 8.4 KB

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