tpm.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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/platform_device.h>
  27. #include <linux/io.h>
  28. #include <linux/tpm.h>
  29. #include <linux/acpi.h>
  30. #include <linux/cdev.h>
  31. enum tpm_const {
  32. TPM_MINOR = 224, /* officially assigned */
  33. TPM_BUFSIZE = 4096,
  34. TPM_NUM_DEVICES = 256,
  35. TPM_RETRY = 50, /* 5 seconds */
  36. };
  37. enum tpm_timeout {
  38. TPM_TIMEOUT = 5, /* msecs */
  39. TPM_TIMEOUT_RETRY = 100 /* msecs */
  40. };
  41. /* TPM addresses */
  42. enum tpm_addr {
  43. TPM_SUPERIO_ADDR = 0x2E,
  44. TPM_ADDR = 0x4E,
  45. };
  46. /* Indexes the duration array */
  47. enum tpm_duration {
  48. TPM_SHORT = 0,
  49. TPM_MEDIUM = 1,
  50. TPM_LONG = 2,
  51. TPM_UNDEFINED,
  52. };
  53. #define TPM_WARN_RETRY 0x800
  54. #define TPM_WARN_DOING_SELFTEST 0x802
  55. #define TPM_ERR_DEACTIVATED 0x6
  56. #define TPM_ERR_DISABLED 0x7
  57. #define TPM_ERR_INVALID_POSTINIT 38
  58. #define TPM_HEADER_SIZE 10
  59. enum tpm2_const {
  60. TPM2_PLATFORM_PCR = 24,
  61. TPM2_PCR_SELECT_MIN = ((TPM2_PLATFORM_PCR + 7) / 8),
  62. TPM2_TIMEOUT_A = 750,
  63. TPM2_TIMEOUT_B = 2000,
  64. TPM2_TIMEOUT_C = 200,
  65. TPM2_TIMEOUT_D = 30,
  66. TPM2_DURATION_SHORT = 20,
  67. TPM2_DURATION_MEDIUM = 750,
  68. TPM2_DURATION_LONG = 2000,
  69. };
  70. enum tpm2_structures {
  71. TPM2_ST_NO_SESSIONS = 0x8001,
  72. TPM2_ST_SESSIONS = 0x8002,
  73. };
  74. enum tpm2_return_codes {
  75. TPM2_RC_INITIALIZE = 0x0100,
  76. TPM2_RC_TESTING = 0x090A,
  77. TPM2_RC_DISABLED = 0x0120,
  78. };
  79. enum tpm2_algorithms {
  80. TPM2_ALG_SHA1 = 0x0004,
  81. };
  82. enum tpm2_command_codes {
  83. TPM2_CC_FIRST = 0x011F,
  84. TPM2_CC_SELF_TEST = 0x0143,
  85. TPM2_CC_STARTUP = 0x0144,
  86. TPM2_CC_SHUTDOWN = 0x0145,
  87. TPM2_CC_GET_CAPABILITY = 0x017A,
  88. TPM2_CC_GET_RANDOM = 0x017B,
  89. TPM2_CC_PCR_READ = 0x017E,
  90. TPM2_CC_PCR_EXTEND = 0x0182,
  91. TPM2_CC_LAST = 0x018F,
  92. };
  93. enum tpm2_permanent_handles {
  94. TPM2_RS_PW = 0x40000009,
  95. };
  96. enum tpm2_capabilities {
  97. TPM2_CAP_TPM_PROPERTIES = 6,
  98. };
  99. enum tpm2_startup_types {
  100. TPM2_SU_CLEAR = 0x0000,
  101. TPM2_SU_STATE = 0x0001,
  102. };
  103. struct tpm_chip;
  104. struct tpm_vendor_specific {
  105. void __iomem *iobase; /* ioremapped address */
  106. unsigned long base; /* TPM base address */
  107. int irq;
  108. int probed_irq;
  109. int region_size;
  110. int have_region;
  111. struct list_head list;
  112. int locality;
  113. unsigned long timeout_a, timeout_b, timeout_c, timeout_d; /* jiffies */
  114. bool timeout_adjusted;
  115. unsigned long duration[3]; /* jiffies */
  116. bool duration_adjusted;
  117. void *priv;
  118. wait_queue_head_t read_queue;
  119. wait_queue_head_t int_queue;
  120. u16 manufacturer_id;
  121. };
  122. #define TPM_VPRIV(c) ((c)->vendor.priv)
  123. #define TPM_VID_INTEL 0x8086
  124. #define TPM_VID_WINBOND 0x1050
  125. #define TPM_VID_STM 0x104A
  126. #define TPM_PPI_VERSION_LEN 3
  127. enum tpm_chip_flags {
  128. TPM_CHIP_FLAG_REGISTERED = BIT(0),
  129. TPM_CHIP_FLAG_PPI = BIT(1),
  130. TPM_CHIP_FLAG_TPM2 = BIT(2),
  131. };
  132. struct tpm_chip {
  133. struct device *pdev; /* Device stuff */
  134. struct device dev;
  135. struct cdev cdev;
  136. const struct tpm_class_ops *ops;
  137. unsigned int flags;
  138. int dev_num; /* /dev/tpm# */
  139. char devname[7];
  140. unsigned long is_open; /* only one allowed */
  141. int time_expired;
  142. struct mutex tpm_mutex; /* tpm is processing */
  143. struct tpm_vendor_specific vendor;
  144. struct dentry **bios_dir;
  145. #ifdef CONFIG_ACPI
  146. acpi_handle acpi_dev_handle;
  147. char ppi_version[TPM_PPI_VERSION_LEN + 1];
  148. #endif /* CONFIG_ACPI */
  149. struct list_head list;
  150. };
  151. #define to_tpm_chip(n) container_of(n, struct tpm_chip, vendor)
  152. static inline void tpm_chip_put(struct tpm_chip *chip)
  153. {
  154. module_put(chip->pdev->driver->owner);
  155. }
  156. static inline int tpm_read_index(int base, int index)
  157. {
  158. outb(index, base);
  159. return inb(base+1) & 0xFF;
  160. }
  161. static inline void tpm_write_index(int base, int index, int value)
  162. {
  163. outb(index, base);
  164. outb(value & 0xFF, base+1);
  165. }
  166. struct tpm_input_header {
  167. __be16 tag;
  168. __be32 length;
  169. __be32 ordinal;
  170. } __packed;
  171. struct tpm_output_header {
  172. __be16 tag;
  173. __be32 length;
  174. __be32 return_code;
  175. } __packed;
  176. #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
  177. struct stclear_flags_t {
  178. __be16 tag;
  179. u8 deactivated;
  180. u8 disableForceClear;
  181. u8 physicalPresence;
  182. u8 physicalPresenceLock;
  183. u8 bGlobalLock;
  184. } __packed;
  185. struct tpm_version_t {
  186. u8 Major;
  187. u8 Minor;
  188. u8 revMajor;
  189. u8 revMinor;
  190. } __packed;
  191. struct tpm_version_1_2_t {
  192. __be16 tag;
  193. u8 Major;
  194. u8 Minor;
  195. u8 revMajor;
  196. u8 revMinor;
  197. } __packed;
  198. struct timeout_t {
  199. __be32 a;
  200. __be32 b;
  201. __be32 c;
  202. __be32 d;
  203. } __packed;
  204. struct duration_t {
  205. __be32 tpm_short;
  206. __be32 tpm_medium;
  207. __be32 tpm_long;
  208. } __packed;
  209. struct permanent_flags_t {
  210. __be16 tag;
  211. u8 disable;
  212. u8 ownership;
  213. u8 deactivated;
  214. u8 readPubek;
  215. u8 disableOwnerClear;
  216. u8 allowMaintenance;
  217. u8 physicalPresenceLifetimeLock;
  218. u8 physicalPresenceHWEnable;
  219. u8 physicalPresenceCMDEnable;
  220. u8 CEKPUsed;
  221. u8 TPMpost;
  222. u8 TPMpostLock;
  223. u8 FIPS;
  224. u8 operator;
  225. u8 enableRevokeEK;
  226. u8 nvLocked;
  227. u8 readSRKPub;
  228. u8 tpmEstablished;
  229. u8 maintenanceDone;
  230. u8 disableFullDALogicInfo;
  231. } __packed;
  232. typedef union {
  233. struct permanent_flags_t perm_flags;
  234. struct stclear_flags_t stclear_flags;
  235. bool owned;
  236. __be32 num_pcrs;
  237. struct tpm_version_t tpm_version;
  238. struct tpm_version_1_2_t tpm_version_1_2;
  239. __be32 manufacturer_id;
  240. struct timeout_t timeout;
  241. struct duration_t duration;
  242. } cap_t;
  243. enum tpm_capabilities {
  244. TPM_CAP_FLAG = cpu_to_be32(4),
  245. TPM_CAP_PROP = cpu_to_be32(5),
  246. CAP_VERSION_1_1 = cpu_to_be32(0x06),
  247. CAP_VERSION_1_2 = cpu_to_be32(0x1A)
  248. };
  249. enum tpm_sub_capabilities {
  250. TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
  251. TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
  252. TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
  253. TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
  254. TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
  255. TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
  256. TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
  257. };
  258. struct tpm_getcap_params_in {
  259. __be32 cap;
  260. __be32 subcap_size;
  261. __be32 subcap;
  262. } __packed;
  263. struct tpm_getcap_params_out {
  264. __be32 cap_size;
  265. cap_t cap;
  266. } __packed;
  267. struct tpm_readpubek_params_out {
  268. u8 algorithm[4];
  269. u8 encscheme[2];
  270. u8 sigscheme[2];
  271. __be32 paramsize;
  272. u8 parameters[12]; /*assuming RSA*/
  273. __be32 keysize;
  274. u8 modulus[256];
  275. u8 checksum[20];
  276. } __packed;
  277. typedef union {
  278. struct tpm_input_header in;
  279. struct tpm_output_header out;
  280. } tpm_cmd_header;
  281. struct tpm_pcrread_out {
  282. u8 pcr_result[TPM_DIGEST_SIZE];
  283. } __packed;
  284. struct tpm_pcrread_in {
  285. __be32 pcr_idx;
  286. } __packed;
  287. struct tpm_pcrextend_in {
  288. __be32 pcr_idx;
  289. u8 hash[TPM_DIGEST_SIZE];
  290. } __packed;
  291. /* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18
  292. * bytes, but 128 is still a relatively large number of random bytes and
  293. * anything much bigger causes users of struct tpm_cmd_t to start getting
  294. * compiler warnings about stack frame size. */
  295. #define TPM_MAX_RNG_DATA 128
  296. struct tpm_getrandom_out {
  297. __be32 rng_data_len;
  298. u8 rng_data[TPM_MAX_RNG_DATA];
  299. } __packed;
  300. struct tpm_getrandom_in {
  301. __be32 num_bytes;
  302. } __packed;
  303. struct tpm_startup_in {
  304. __be16 startup_type;
  305. } __packed;
  306. typedef union {
  307. struct tpm_getcap_params_out getcap_out;
  308. struct tpm_readpubek_params_out readpubek_out;
  309. u8 readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
  310. struct tpm_getcap_params_in getcap_in;
  311. struct tpm_pcrread_in pcrread_in;
  312. struct tpm_pcrread_out pcrread_out;
  313. struct tpm_pcrextend_in pcrextend_in;
  314. struct tpm_getrandom_in getrandom_in;
  315. struct tpm_getrandom_out getrandom_out;
  316. struct tpm_startup_in startup_in;
  317. } tpm_cmd_params;
  318. struct tpm_cmd_t {
  319. tpm_cmd_header header;
  320. tpm_cmd_params params;
  321. } __packed;
  322. extern struct class *tpm_class;
  323. extern dev_t tpm_devt;
  324. extern const struct file_operations tpm_fops;
  325. ssize_t tpm_getcap(struct device *, __be32, cap_t *, const char *);
  326. ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
  327. size_t bufsiz);
  328. ssize_t tpm_transmit_cmd(struct tpm_chip *chip, void *cmd, int len,
  329. const char *desc);
  330. extern int tpm_get_timeouts(struct tpm_chip *);
  331. extern void tpm_gen_interrupt(struct tpm_chip *);
  332. extern int tpm_do_selftest(struct tpm_chip *);
  333. extern unsigned long tpm_calc_ordinal_duration(struct tpm_chip *, u32);
  334. extern int tpm_pm_suspend(struct device *);
  335. extern int tpm_pm_resume(struct device *);
  336. extern int wait_for_tpm_stat(struct tpm_chip *, u8, unsigned long,
  337. wait_queue_head_t *, bool);
  338. struct tpm_chip *tpm_chip_find_get(int chip_num);
  339. extern struct tpm_chip *tpmm_chip_alloc(struct device *dev,
  340. const struct tpm_class_ops *ops);
  341. extern int tpm_chip_register(struct tpm_chip *chip);
  342. extern void tpm_chip_unregister(struct tpm_chip *chip);
  343. int tpm_sysfs_add_device(struct tpm_chip *chip);
  344. void tpm_sysfs_del_device(struct tpm_chip *chip);
  345. int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
  346. #ifdef CONFIG_ACPI
  347. extern int tpm_add_ppi(struct tpm_chip *chip);
  348. extern void tpm_remove_ppi(struct tpm_chip *chip);
  349. #else
  350. static inline int tpm_add_ppi(struct tpm_chip *chip)
  351. {
  352. return 0;
  353. }
  354. static inline void tpm_remove_ppi(struct tpm_chip *chip)
  355. {
  356. }
  357. #endif
  358. int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
  359. int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash);
  360. int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max);
  361. ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
  362. u32 *value, const char *desc);
  363. extern int tpm2_startup(struct tpm_chip *chip, u16 startup_type);
  364. extern void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
  365. extern unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *, u32);
  366. extern int tpm2_do_selftest(struct tpm_chip *chip);
  367. extern int tpm2_gen_interrupt(struct tpm_chip *chip);
  368. extern int tpm2_probe(struct tpm_chip *chip);