tpm.h 8.6 KB

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