tpm.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 = 65536,
  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_INITIALIZE = 0x0100,
  78. TPM2_RC_TESTING = 0x090A,
  79. TPM2_RC_DISABLED = 0x0120,
  80. };
  81. enum tpm2_algorithms {
  82. TPM2_ALG_SHA1 = 0x0004,
  83. TPM2_ALG_KEYEDHASH = 0x0008,
  84. TPM2_ALG_SHA256 = 0x000B,
  85. TPM2_ALG_NULL = 0x0010
  86. };
  87. enum tpm2_command_codes {
  88. TPM2_CC_FIRST = 0x011F,
  89. TPM2_CC_SELF_TEST = 0x0143,
  90. TPM2_CC_STARTUP = 0x0144,
  91. TPM2_CC_SHUTDOWN = 0x0145,
  92. TPM2_CC_CREATE = 0x0153,
  93. TPM2_CC_LOAD = 0x0157,
  94. TPM2_CC_UNSEAL = 0x015E,
  95. TPM2_CC_FLUSH_CONTEXT = 0x0165,
  96. TPM2_CC_GET_CAPABILITY = 0x017A,
  97. TPM2_CC_GET_RANDOM = 0x017B,
  98. TPM2_CC_PCR_READ = 0x017E,
  99. TPM2_CC_PCR_EXTEND = 0x0182,
  100. TPM2_CC_LAST = 0x018F,
  101. };
  102. enum tpm2_permanent_handles {
  103. TPM2_RS_PW = 0x40000009,
  104. };
  105. enum tpm2_capabilities {
  106. TPM2_CAP_TPM_PROPERTIES = 6,
  107. };
  108. enum tpm2_startup_types {
  109. TPM2_SU_CLEAR = 0x0000,
  110. TPM2_SU_STATE = 0x0001,
  111. };
  112. enum tpm2_start_method {
  113. TPM2_START_ACPI = 2,
  114. TPM2_START_FIFO = 6,
  115. TPM2_START_CRB = 7,
  116. TPM2_START_CRB_WITH_ACPI = 8,
  117. };
  118. struct tpm_chip;
  119. struct tpm_vendor_specific {
  120. void __iomem *iobase; /* ioremapped address */
  121. unsigned long base; /* TPM base address */
  122. int irq;
  123. int probed_irq;
  124. int region_size;
  125. int have_region;
  126. struct list_head list;
  127. int locality;
  128. unsigned long timeout_a, timeout_b, timeout_c, timeout_d; /* jiffies */
  129. bool timeout_adjusted;
  130. unsigned long duration[3]; /* jiffies */
  131. bool duration_adjusted;
  132. void *priv;
  133. wait_queue_head_t read_queue;
  134. wait_queue_head_t int_queue;
  135. u16 manufacturer_id;
  136. };
  137. #define TPM_VPRIV(c) ((c)->vendor.priv)
  138. #define TPM_VID_INTEL 0x8086
  139. #define TPM_VID_WINBOND 0x1050
  140. #define TPM_VID_STM 0x104A
  141. #define TPM_PPI_VERSION_LEN 3
  142. enum tpm_chip_flags {
  143. TPM_CHIP_FLAG_REGISTERED = BIT(0),
  144. TPM_CHIP_FLAG_TPM2 = BIT(1),
  145. TPM_CHIP_FLAG_ALWAYS_POWERED = BIT(5),
  146. };
  147. struct tpm_chip {
  148. struct device dev;
  149. struct cdev cdev;
  150. /* A driver callback under ops cannot be run unless ops_sem is held
  151. * (sometimes implicitly, eg for the sysfs code). ops becomes null
  152. * when the driver is unregistered, see tpm_try_get_ops.
  153. */
  154. struct rw_semaphore ops_sem;
  155. const struct tpm_class_ops *ops;
  156. unsigned int flags;
  157. int dev_num; /* /dev/tpm# */
  158. char devname[7];
  159. unsigned long is_open; /* only one allowed */
  160. int time_expired;
  161. struct mutex tpm_mutex; /* tpm is processing */
  162. struct tpm_vendor_specific vendor;
  163. struct dentry **bios_dir;
  164. #ifdef CONFIG_ACPI
  165. const struct attribute_group *groups[2];
  166. unsigned int groups_cnt;
  167. acpi_handle acpi_dev_handle;
  168. char ppi_version[TPM_PPI_VERSION_LEN + 1];
  169. #endif /* CONFIG_ACPI */
  170. };
  171. #define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)
  172. static inline int tpm_read_index(int base, int index)
  173. {
  174. outb(index, base);
  175. return inb(base+1) & 0xFF;
  176. }
  177. static inline void tpm_write_index(int base, int index, int value)
  178. {
  179. outb(index, base);
  180. outb(value & 0xFF, base+1);
  181. }
  182. struct tpm_input_header {
  183. __be16 tag;
  184. __be32 length;
  185. __be32 ordinal;
  186. } __packed;
  187. struct tpm_output_header {
  188. __be16 tag;
  189. __be32 length;
  190. __be32 return_code;
  191. } __packed;
  192. #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
  193. struct stclear_flags_t {
  194. __be16 tag;
  195. u8 deactivated;
  196. u8 disableForceClear;
  197. u8 physicalPresence;
  198. u8 physicalPresenceLock;
  199. u8 bGlobalLock;
  200. } __packed;
  201. struct tpm_version_t {
  202. u8 Major;
  203. u8 Minor;
  204. u8 revMajor;
  205. u8 revMinor;
  206. } __packed;
  207. struct tpm_version_1_2_t {
  208. __be16 tag;
  209. u8 Major;
  210. u8 Minor;
  211. u8 revMajor;
  212. u8 revMinor;
  213. } __packed;
  214. struct timeout_t {
  215. __be32 a;
  216. __be32 b;
  217. __be32 c;
  218. __be32 d;
  219. } __packed;
  220. struct duration_t {
  221. __be32 tpm_short;
  222. __be32 tpm_medium;
  223. __be32 tpm_long;
  224. } __packed;
  225. struct permanent_flags_t {
  226. __be16 tag;
  227. u8 disable;
  228. u8 ownership;
  229. u8 deactivated;
  230. u8 readPubek;
  231. u8 disableOwnerClear;
  232. u8 allowMaintenance;
  233. u8 physicalPresenceLifetimeLock;
  234. u8 physicalPresenceHWEnable;
  235. u8 physicalPresenceCMDEnable;
  236. u8 CEKPUsed;
  237. u8 TPMpost;
  238. u8 TPMpostLock;
  239. u8 FIPS;
  240. u8 operator;
  241. u8 enableRevokeEK;
  242. u8 nvLocked;
  243. u8 readSRKPub;
  244. u8 tpmEstablished;
  245. u8 maintenanceDone;
  246. u8 disableFullDALogicInfo;
  247. } __packed;
  248. typedef union {
  249. struct permanent_flags_t perm_flags;
  250. struct stclear_flags_t stclear_flags;
  251. bool owned;
  252. __be32 num_pcrs;
  253. struct tpm_version_t tpm_version;
  254. struct tpm_version_1_2_t tpm_version_1_2;
  255. __be32 manufacturer_id;
  256. struct timeout_t timeout;
  257. struct duration_t duration;
  258. } cap_t;
  259. enum tpm_capabilities {
  260. TPM_CAP_FLAG = cpu_to_be32(4),
  261. TPM_CAP_PROP = cpu_to_be32(5),
  262. CAP_VERSION_1_1 = cpu_to_be32(0x06),
  263. CAP_VERSION_1_2 = cpu_to_be32(0x1A)
  264. };
  265. enum tpm_sub_capabilities {
  266. TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
  267. TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
  268. TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
  269. TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
  270. TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
  271. TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
  272. TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
  273. };
  274. struct tpm_getcap_params_in {
  275. __be32 cap;
  276. __be32 subcap_size;
  277. __be32 subcap;
  278. } __packed;
  279. struct tpm_getcap_params_out {
  280. __be32 cap_size;
  281. cap_t cap;
  282. } __packed;
  283. struct tpm_readpubek_params_out {
  284. u8 algorithm[4];
  285. u8 encscheme[2];
  286. u8 sigscheme[2];
  287. __be32 paramsize;
  288. u8 parameters[12]; /*assuming RSA*/
  289. __be32 keysize;
  290. u8 modulus[256];
  291. u8 checksum[20];
  292. } __packed;
  293. typedef union {
  294. struct tpm_input_header in;
  295. struct tpm_output_header out;
  296. } tpm_cmd_header;
  297. struct tpm_pcrread_out {
  298. u8 pcr_result[TPM_DIGEST_SIZE];
  299. } __packed;
  300. struct tpm_pcrread_in {
  301. __be32 pcr_idx;
  302. } __packed;
  303. struct tpm_pcrextend_in {
  304. __be32 pcr_idx;
  305. u8 hash[TPM_DIGEST_SIZE];
  306. } __packed;
  307. /* 128 bytes is an arbitrary cap. This could be as large as TPM_BUFSIZE - 18
  308. * bytes, but 128 is still a relatively large number of random bytes and
  309. * anything much bigger causes users of struct tpm_cmd_t to start getting
  310. * compiler warnings about stack frame size. */
  311. #define TPM_MAX_RNG_DATA 128
  312. struct tpm_getrandom_out {
  313. __be32 rng_data_len;
  314. u8 rng_data[TPM_MAX_RNG_DATA];
  315. } __packed;
  316. struct tpm_getrandom_in {
  317. __be32 num_bytes;
  318. } __packed;
  319. struct tpm_startup_in {
  320. __be16 startup_type;
  321. } __packed;
  322. typedef union {
  323. struct tpm_getcap_params_out getcap_out;
  324. struct tpm_readpubek_params_out readpubek_out;
  325. u8 readpubek_out_buffer[sizeof(struct tpm_readpubek_params_out)];
  326. struct tpm_getcap_params_in getcap_in;
  327. struct tpm_pcrread_in pcrread_in;
  328. struct tpm_pcrread_out pcrread_out;
  329. struct tpm_pcrextend_in pcrextend_in;
  330. struct tpm_getrandom_in getrandom_in;
  331. struct tpm_getrandom_out getrandom_out;
  332. struct tpm_startup_in startup_in;
  333. } tpm_cmd_params;
  334. struct tpm_cmd_t {
  335. tpm_cmd_header header;
  336. tpm_cmd_params params;
  337. } __packed;
  338. /* A string buffer type for constructing TPM commands. This is based on the
  339. * ideas of string buffer code in security/keys/trusted.h but is heap based
  340. * in order to keep the stack usage minimal.
  341. */
  342. enum tpm_buf_flags {
  343. TPM_BUF_OVERFLOW = BIT(0),
  344. };
  345. struct tpm_buf {
  346. struct page *data_page;
  347. unsigned int flags;
  348. u8 *data;
  349. };
  350. static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
  351. {
  352. struct tpm_input_header *head;
  353. buf->data_page = alloc_page(GFP_HIGHUSER);
  354. if (!buf->data_page)
  355. return -ENOMEM;
  356. buf->flags = 0;
  357. buf->data = kmap(buf->data_page);
  358. head = (struct tpm_input_header *) buf->data;
  359. head->tag = cpu_to_be16(tag);
  360. head->length = cpu_to_be32(sizeof(*head));
  361. head->ordinal = cpu_to_be32(ordinal);
  362. return 0;
  363. }
  364. static inline void tpm_buf_destroy(struct tpm_buf *buf)
  365. {
  366. kunmap(buf->data_page);
  367. __free_page(buf->data_page);
  368. }
  369. static inline u32 tpm_buf_length(struct tpm_buf *buf)
  370. {
  371. struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
  372. return be32_to_cpu(head->length);
  373. }
  374. static inline u16 tpm_buf_tag(struct tpm_buf *buf)
  375. {
  376. struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
  377. return be16_to_cpu(head->tag);
  378. }
  379. static inline void tpm_buf_append(struct tpm_buf *buf,
  380. const unsigned char *new_data,
  381. unsigned int new_len)
  382. {
  383. struct tpm_input_header *head = (struct tpm_input_header *) buf->data;
  384. u32 len = tpm_buf_length(buf);
  385. /* Return silently if overflow has already happened. */
  386. if (buf->flags & TPM_BUF_OVERFLOW)
  387. return;
  388. if ((len + new_len) > PAGE_SIZE) {
  389. WARN(1, "tpm_buf: overflow\n");
  390. buf->flags |= TPM_BUF_OVERFLOW;
  391. return;
  392. }
  393. memcpy(&buf->data[len], new_data, new_len);
  394. head->length = cpu_to_be32(len + new_len);
  395. }
  396. static inline void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value)
  397. {
  398. tpm_buf_append(buf, &value, 1);
  399. }
  400. static inline void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value)
  401. {
  402. __be16 value2 = cpu_to_be16(value);
  403. tpm_buf_append(buf, (u8 *) &value2, 2);
  404. }
  405. static inline void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
  406. {
  407. __be32 value2 = cpu_to_be32(value);
  408. tpm_buf_append(buf, (u8 *) &value2, 4);
  409. }
  410. extern struct class *tpm_class;
  411. extern dev_t tpm_devt;
  412. extern const struct file_operations tpm_fops;
  413. extern struct idr dev_nums_idr;
  414. enum tpm_transmit_flags {
  415. TPM_TRANSMIT_UNLOCKED = BIT(0),
  416. };
  417. ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz,
  418. unsigned int flags);
  419. ssize_t tpm_transmit_cmd(struct tpm_chip *chip, const void *cmd, int len,
  420. unsigned int flags, const char *desc);
  421. ssize_t tpm_getcap(struct device *, __be32, cap_t *, const char *);
  422. extern int tpm_get_timeouts(struct tpm_chip *);
  423. extern void tpm_gen_interrupt(struct tpm_chip *);
  424. extern int tpm_do_selftest(struct tpm_chip *);
  425. extern unsigned long tpm_calc_ordinal_duration(struct tpm_chip *, u32);
  426. extern int tpm_pm_suspend(struct device *);
  427. extern int tpm_pm_resume(struct device *);
  428. extern int wait_for_tpm_stat(struct tpm_chip *, u8, unsigned long,
  429. wait_queue_head_t *, bool);
  430. struct tpm_chip *tpm_chip_find_get(int chip_num);
  431. __must_check int tpm_try_get_ops(struct tpm_chip *chip);
  432. void tpm_put_ops(struct tpm_chip *chip);
  433. extern struct tpm_chip *tpmm_chip_alloc(struct device *dev,
  434. const struct tpm_class_ops *ops);
  435. extern int tpm_chip_register(struct tpm_chip *chip);
  436. extern void tpm_chip_unregister(struct tpm_chip *chip);
  437. int tpm_sysfs_add_device(struct tpm_chip *chip);
  438. void tpm_sysfs_del_device(struct tpm_chip *chip);
  439. int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
  440. #ifdef CONFIG_ACPI
  441. extern void tpm_add_ppi(struct tpm_chip *chip);
  442. #else
  443. static inline void tpm_add_ppi(struct tpm_chip *chip)
  444. {
  445. }
  446. #endif
  447. int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
  448. int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash);
  449. int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max);
  450. int tpm2_seal_trusted(struct tpm_chip *chip,
  451. struct trusted_key_payload *payload,
  452. struct trusted_key_options *options);
  453. int tpm2_unseal_trusted(struct tpm_chip *chip,
  454. struct trusted_key_payload *payload,
  455. struct trusted_key_options *options);
  456. ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,
  457. u32 *value, const char *desc);
  458. extern int tpm2_startup(struct tpm_chip *chip, u16 startup_type);
  459. extern void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type);
  460. extern unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *, u32);
  461. extern int tpm2_do_selftest(struct tpm_chip *chip);
  462. extern int tpm2_gen_interrupt(struct tpm_chip *chip);
  463. extern int tpm2_probe(struct tpm_chip *chip);