tpm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. * Copyright (C) 2015 Intel Corporation
  4. *
  5. * Authors:
  6. * Leendert van Doorn <leendert@watson.ibm.com>
  7. * Dave Safford <safford@watson.ibm.com>
  8. * Reiner Sailer <sailer@watson.ibm.com>
  9. * Kylene Hall <kjhall@us.ibm.com>
  10. *
  11. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  12. *
  13. * Device driver for TCG/TCPA TPM (trusted platform module).
  14. * Specifications at www.trustedcomputinggroup.org
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation, version 2 of the
  19. * License.
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/fs.h>
  25. #include <linux/mutex.h>
  26. #include <linux/sched.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/io.h>
  29. #include <linux/tpm.h>
  30. #include <linux/acpi.h>
  31. #include <linux/cdev.h>
  32. #include <linux/highmem.h>
  33. enum tpm_const {
  34. TPM_MINOR = 224, /* officially assigned */
  35. TPM_BUFSIZE = 4096,
  36. TPM_NUM_DEVICES = 256,
  37. TPM_RETRY = 50, /* 5 seconds */
  38. };
  39. enum tpm_timeout {
  40. TPM_TIMEOUT = 5, /* msecs */
  41. TPM_TIMEOUT_RETRY = 100 /* msecs */
  42. };
  43. /* TPM addresses */
  44. enum tpm_addr {
  45. TPM_SUPERIO_ADDR = 0x2E,
  46. TPM_ADDR = 0x4E,
  47. };
  48. /* Indexes the duration array */
  49. enum tpm_duration {
  50. TPM_SHORT = 0,
  51. TPM_MEDIUM = 1,
  52. TPM_LONG = 2,
  53. TPM_UNDEFINED,
  54. };
  55. #define TPM_WARN_RETRY 0x800
  56. #define TPM_WARN_DOING_SELFTEST 0x802
  57. #define TPM_ERR_DEACTIVATED 0x6
  58. #define TPM_ERR_DISABLED 0x7
  59. #define TPM_ERR_INVALID_POSTINIT 38
  60. #define TPM_HEADER_SIZE 10
  61. enum tpm2_const {
  62. TPM2_PLATFORM_PCR = 24,
  63. TPM2_PCR_SELECT_MIN = ((TPM2_PLATFORM_PCR + 7) / 8),
  64. TPM2_TIMEOUT_A = 750,
  65. TPM2_TIMEOUT_B = 2000,
  66. TPM2_TIMEOUT_C = 200,
  67. TPM2_TIMEOUT_D = 30,
  68. TPM2_DURATION_SHORT = 20,
  69. TPM2_DURATION_MEDIUM = 750,
  70. TPM2_DURATION_LONG = 2000,
  71. };
  72. enum tpm2_structures {
  73. TPM2_ST_NO_SESSIONS = 0x8001,
  74. TPM2_ST_SESSIONS = 0x8002,
  75. };
  76. enum tpm2_return_codes {
  77. TPM2_RC_HASH = 0x0083, /* RC_FMT1 */
  78. TPM2_RC_INITIALIZE = 0x0100, /* RC_VER1 */
  79. TPM2_RC_DISABLED = 0x0120,
  80. TPM2_RC_TESTING = 0x090A, /* RC_WARN */
  81. };
  82. enum tpm2_algorithms {
  83. TPM2_ALG_SHA1 = 0x0004,
  84. TPM2_ALG_KEYEDHASH = 0x0008,
  85. TPM2_ALG_SHA256 = 0x000B,
  86. TPM2_ALG_SHA384 = 0x000C,
  87. TPM2_ALG_SHA512 = 0x000D,
  88. TPM2_ALG_NULL = 0x0010,
  89. TPM2_ALG_SM3_256 = 0x0012,
  90. };
  91. enum tpm2_command_codes {
  92. TPM2_CC_FIRST = 0x011F,
  93. TPM2_CC_SELF_TEST = 0x0143,
  94. TPM2_CC_STARTUP = 0x0144,
  95. TPM2_CC_SHUTDOWN = 0x0145,
  96. TPM2_CC_CREATE = 0x0153,
  97. TPM2_CC_LOAD = 0x0157,
  98. TPM2_CC_UNSEAL = 0x015E,
  99. TPM2_CC_FLUSH_CONTEXT = 0x0165,
  100. TPM2_CC_GET_CAPABILITY = 0x017A,
  101. TPM2_CC_GET_RANDOM = 0x017B,
  102. TPM2_CC_PCR_READ = 0x017E,
  103. TPM2_CC_PCR_EXTEND = 0x0182,
  104. TPM2_CC_LAST = 0x018F,
  105. };
  106. enum tpm2_permanent_handles {
  107. TPM2_RS_PW = 0x40000009,
  108. };
  109. enum tpm2_capabilities {
  110. TPM2_CAP_TPM_PROPERTIES = 6,
  111. };
  112. enum tpm2_startup_types {
  113. TPM2_SU_CLEAR = 0x0000,
  114. TPM2_SU_STATE = 0x0001,
  115. };
  116. struct tpm_chip;
  117. struct tpm_vendor_specific {
  118. void __iomem *iobase; /* ioremapped address */
  119. unsigned long base; /* TPM base address */
  120. int irq;
  121. int region_size;
  122. int have_region;
  123. struct list_head list;
  124. int locality;
  125. unsigned long timeout_a, timeout_b, timeout_c, timeout_d; /* jiffies */
  126. bool timeout_adjusted;
  127. unsigned long duration[3]; /* jiffies */
  128. bool duration_adjusted;
  129. void *priv;
  130. wait_queue_head_t read_queue;
  131. wait_queue_head_t int_queue;
  132. u16 manufacturer_id;
  133. };
  134. #define TPM_VPRIV(c) ((c)->vendor.priv)
  135. #define TPM_VID_INTEL 0x8086
  136. #define TPM_VID_WINBOND 0x1050
  137. #define TPM_VID_STM 0x104A
  138. #define TPM_PPI_VERSION_LEN 3
  139. enum tpm_chip_flags {
  140. TPM_CHIP_FLAG_REGISTERED = BIT(0),
  141. TPM_CHIP_FLAG_TPM2 = BIT(1),
  142. };
  143. struct tpm_chip {
  144. struct device *pdev; /* Device stuff */
  145. struct device dev;
  146. struct cdev cdev;
  147. const struct tpm_class_ops *ops;
  148. unsigned int flags;
  149. int dev_num; /* /dev/tpm# */
  150. char devname[7];
  151. unsigned long is_open; /* only one allowed */
  152. int time_expired;
  153. struct mutex tpm_mutex; /* tpm is processing */
  154. struct tpm_vendor_specific vendor;
  155. struct dentry **bios_dir;
  156. #ifdef CONFIG_ACPI
  157. const struct attribute_group *groups[2];
  158. unsigned int groups_cnt;
  159. acpi_handle acpi_dev_handle;
  160. char ppi_version[TPM_PPI_VERSION_LEN + 1];
  161. #endif /* CONFIG_ACPI */
  162. struct list_head list;
  163. };
  164. #define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)
  165. static inline void tpm_chip_put(struct tpm_chip *chip)
  166. {
  167. module_put(chip->pdev->driver->owner);
  168. }
  169. static inline int tpm_read_index(int base, int index)
  170. {
  171. outb(index, base);
  172. return inb(base+1) & 0xFF;
  173. }
  174. static inline void tpm_write_index(int base, int index, int value)
  175. {
  176. outb(index, base);
  177. outb(value & 0xFF, base+1);
  178. }
  179. struct tpm_input_header {
  180. __be16 tag;
  181. __be32 length;
  182. __be32 ordinal;
  183. } __packed;
  184. struct tpm_output_header {
  185. __be16 tag;
  186. __be32 length;
  187. __be32 return_code;
  188. } __packed;
  189. #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
  190. struct stclear_flags_t {
  191. __be16 tag;
  192. u8 deactivated;
  193. u8 disableForceClear;
  194. u8 physicalPresence;
  195. u8 physicalPresenceLock;
  196. u8 bGlobalLock;
  197. } __packed;
  198. struct tpm_version_t {
  199. u8 Major;
  200. u8 Minor;
  201. u8 revMajor;
  202. u8 revMinor;
  203. } __packed;
  204. struct tpm_version_1_2_t {
  205. __be16 tag;
  206. u8 Major;
  207. u8 Minor;
  208. u8 revMajor;
  209. u8 revMinor;
  210. } __packed;
  211. struct timeout_t {
  212. __be32 a;
  213. __be32 b;
  214. __be32 c;
  215. __be32 d;
  216. } __packed;
  217. struct duration_t {
  218. __be32 tpm_short;
  219. __be32 tpm_medium;
  220. __be32 tpm_long;
  221. } __packed;
  222. struct permanent_flags_t {
  223. __be16 tag;
  224. u8 disable;
  225. u8 ownership;
  226. u8 deactivated;
  227. u8 readPubek;
  228. u8 disableOwnerClear;
  229. u8 allowMaintenance;
  230. u8 physicalPresenceLifetimeLock;
  231. u8 physicalPresenceHWEnable;
  232. u8 physicalPresenceCMDEnable;
  233. u8 CEKPUsed;
  234. u8 TPMpost;
  235. u8 TPMpostLock;
  236. u8 FIPS;
  237. u8 operator;
  238. u8 enableRevokeEK;
  239. u8 nvLocked;
  240. u8 readSRKPub;
  241. u8 tpmEstablished;
  242. u8 maintenanceDone;
  243. u8 disableFullDALogicInfo;
  244. } __packed;
  245. typedef union {
  246. struct permanent_flags_t perm_flags;
  247. struct stclear_flags_t stclear_flags;
  248. bool owned;
  249. __be32 num_pcrs;
  250. struct tpm_version_t tpm_version;
  251. struct tpm_version_1_2_t tpm_version_1_2;
  252. __be32 manufacturer_id;
  253. struct timeout_t timeout;
  254. struct duration_t duration;
  255. } cap_t;
  256. enum tpm_capabilities {
  257. TPM_CAP_FLAG = cpu_to_be32(4),
  258. TPM_CAP_PROP = cpu_to_be32(5),
  259. CAP_VERSION_1_1 = cpu_to_be32(0x06),
  260. CAP_VERSION_1_2 = cpu_to_be32(0x1A)
  261. };
  262. enum tpm_sub_capabilities {
  263. TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
  264. TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
  265. TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
  266. TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
  267. TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
  268. TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
  269. TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
  270. };
  271. struct tpm_getcap_params_in {
  272. __be32 cap;
  273. __be32 subcap_size;
  274. __be32 subcap;
  275. } __packed;
  276. struct tpm_getcap_params_out {
  277. __be32 cap_size;
  278. cap_t cap;
  279. } __packed;
  280. struct tpm_readpubek_params_out {
  281. u8 algorithm[4];
  282. u8 encscheme[2];
  283. u8 sigscheme[2];
  284. __be32 paramsize;
  285. u8 parameters[12]; /*assuming RSA*/
  286. __be32 keysize;
  287. u8 modulus[256];
  288. u8 checksum[20];
  289. } __packed;
  290. typedef union {
  291. struct tpm_input_header in;
  292. struct tpm_output_header out;
  293. } tpm_cmd_header;
  294. struct tpm_pcrread_out {
  295. u8 pcr_result[TPM_DIGEST_SIZE];
  296. } __packed;
  297. struct tpm_pcrread_in {
  298. __be32 pcr_idx;
  299. } __packed;
  300. struct tpm_pcrextend_in {
  301. __be32 pcr_idx;
  302. u8 hash[TPM_DIGEST_SIZE];
  303. } __packed;
  304. /* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18
  305. * bytes, but 128 is still a relatively large number of random bytes and
  306. * anything much bigger causes users of struct tpm_cmd_t to start getting
  307. * compiler warnings about stack frame size. */
  308. #define TPM_MAX_RNG_DATA 128
  309. struct tpm_getrandom_out {
  310. __be32 rng_data_len;
  311. u8 rng_data[TPM_MAX_RNG_DATA];
  312. } __packed;
  313. struct tpm_getrandom_in {
  314. __be32 num_bytes;
  315. } __packed;
  316. struct tpm_startup_in {
  317. __be16 startup_type;
  318. } __packed;
  319. typedef union {
  320. struct tpm_getcap_params_out getcap_out;
  321. struct tpm_readpubek_params_out readpubek_out;
  322. u8 readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
  323. struct tpm_getcap_params_in getcap_in;
  324. struct tpm_pcrread_in pcrread_in;
  325. struct tpm_pcrread_out pcrread_out;
  326. struct tpm_pcrextend_in pcrextend_in;
  327. struct tpm_getrandom_in getrandom_in;
  328. struct tpm_getrandom_out getrandom_out;
  329. struct tpm_startup_in startup_in;
  330. } tpm_cmd_params;
  331. struct tpm_cmd_t {
  332. tpm_cmd_header header;
  333. tpm_cmd_params params;
  334. } __packed;
  335. /* A string buffer type for constructing TPM commands. This is based on the
  336. * ideas of string buffer code in security/keys/trusted.h but is heap based
  337. * in order to keep the stack usage minimal.
  338. */
  339. enum tpm_buf_flags {
  340. TPM_BUF_OVERFLOW = BIT(0),
  341. };
  342. struct tpm_buf {
  343. struct page *data_page;
  344. unsigned int flags;
  345. u8 *data;
  346. };
  347. static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
  348. {
  349. struct tpm_input_header *head;
  350. buf->data_page = alloc_page(GFP_HIGHUSER);
  351. if (!buf->data_page)
  352. return -ENOMEM;
  353. buf->flags = 0;
  354. buf->data = kmap(buf->data_page);
  355. head = (struct tpm_input_header *) buf->data;
  356. head->tag = cpu_to_be16(tag);
  357. head->length = cpu_to_be32(sizeof(*head));
  358. head->ordinal = cpu_to_be32(ordinal);
  359. return 0;
  360. }
  361. static inline void tpm_buf_destroy(struct tpm_buf *buf)
  362. {
  363. kunmap(buf->data_page);
  364. __free_page(buf->data_page);
  365. }
  366. static inline u32 tpm_buf_length(struct tpm_buf *buf)
  367. {
  368. struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
  369. return be32_to_cpu(head->length);
  370. }
  371. static inline u16 tpm_buf_tag(struct tpm_buf *buf)
  372. {
  373. struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
  374. return be16_to_cpu(head->tag);
  375. }
  376. static inline void tpm_buf_append(struct tpm_buf *buf,
  377. const unsigned char *new_data,
  378. unsigned int new_len)
  379. {
  380. struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
  381. u32 len = tpm_buf_length(buf);
  382. /* Return silently if overflow has already happened. */
  383. if (buf->flags & TPM_BUF_OVERFLOW)
  384. return;
  385. if ((len + new_len) > PAGE_SIZE) {
  386. WARN(1, "tpm_buf: overflow\n");
  387. buf->flags |= TPM_BUF_OVERFLOW;
  388. return;
  389. }
  390. memcpy(&buf->data[len], new_data, new_len);
  391. head->length = cpu_to_be32(len + new_len);
  392. }
  393. static inline void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value)
  394. {
  395. tpm_buf_append(buf, &value, 1);
  396. }
  397. static inline void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value)
  398. {
  399. __be16 value2 = cpu_to_be16(value);
  400. tpm_buf_append(buf, (u8 *) &value2, 2);
  401. }
  402. static inline void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
  403. {
  404. __be32 value2 = cpu_to_be32(value);
  405. tpm_buf_append(buf, (u8 *) &value2, 4);
  406. }
  407. extern struct class *tpm_class;
  408. extern dev_t tpm_devt;
  409. extern const struct file_operations tpm_fops;
  410. ssize_t tpm_getcap(struct device *, __be32, cap_t *, const char *);
  411. ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
  412. size_t bufsiz);
  413. ssize_t tpm_transmit_cmd(struct tpm_chip *chip, void *cmd, int len,
  414. const char *desc);
  415. extern int tpm_get_timeouts(struct tpm_chip *);
  416. extern void tpm_gen_interrupt(struct tpm_chip *);
  417. extern int tpm_do_selftest(struct tpm_chip *);
  418. extern unsigned long tpm_calc_ordinal_duration(struct tpm_chip *, u32);
  419. extern int tpm_pm_suspend(struct device *);
  420. extern int tpm_pm_resume(struct device *);
  421. extern int wait_for_tpm_stat(struct tpm_chip *, u8, unsigned long,
  422. wait_queue_head_t *, bool);
  423. struct tpm_chip *tpm_chip_find_get(int chip_num);
  424. extern struct tpm_chip *tpmm_chip_alloc(struct device *dev,
  425. const struct tpm_class_ops *ops);
  426. extern int tpm_chip_register(struct tpm_chip *chip);
  427. extern void tpm_chip_unregister(struct tpm_chip *chip);
  428. int tpm_sysfs_add_device(struct tpm_chip *chip);
  429. void tpm_sysfs_del_device(struct tpm_chip *chip);
  430. int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
  431. #ifdef CONFIG_ACPI
  432. extern void tpm_add_ppi(struct tpm_chip *chip);
  433. #else
  434. static inline void tpm_add_ppi(struct tpm_chip *chip)
  435. {
  436. }
  437. #endif
  438. int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
  439. int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash);
  440. int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max);
  441. int tpm2_seal_trusted(struct tpm_chip *chip,
  442. struct trusted_key_payload *payload,
  443. struct trusted_key_options *options);
  444. int tpm2_unseal_trusted(struct tpm_chip *chip,
  445. struct trusted_key_payload *payload,
  446. struct trusted_key_options *options);
  447. ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
  448. u32 *value, const char *desc);
  449. extern int tpm2_startup(struct tpm_chip *chip, u16 startup_type);
  450. extern void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
  451. extern unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *, u32);
  452. extern int tpm2_do_selftest(struct tpm_chip *chip);
  453. extern int tpm2_gen_interrupt(struct tpm_chip *chip);
  454. extern int tpm2_probe(struct tpm_chip *chip);