tpm.h 8.5 KB

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