tpm.h 8.5 KB

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