tpm.h 8.4 KB

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