tpm.h 7.8 KB

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