tpm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. * Note, the TPM chip is not interrupt driven (only polling)
  21. * and can have very long timeouts (minutes!). Hence the unusual
  22. * calls to msleep.
  23. *
  24. */
  25. #include <linux/sched.h>
  26. #include <linux/poll.h>
  27. #include <linux/spinlock.h>
  28. #include "tpm.h"
  29. enum tpm_const {
  30. TPM_MINOR = 224, /* officially assigned */
  31. TPM_BUFSIZE = 2048,
  32. TPM_NUM_DEVICES = 256,
  33. TPM_NUM_MASK_ENTRIES = TPM_NUM_DEVICES / (8 * sizeof(int))
  34. };
  35. static LIST_HEAD(tpm_chip_list);
  36. static DEFINE_SPINLOCK(driver_lock);
  37. static int dev_mask[TPM_NUM_MASK_ENTRIES];
  38. static void user_reader_timeout(unsigned long ptr)
  39. {
  40. struct tpm_chip *chip = (struct tpm_chip *) ptr;
  41. down(&chip->buffer_mutex);
  42. atomic_set(&chip->data_pending, 0);
  43. memset(chip->data_buffer, 0, TPM_BUFSIZE);
  44. up(&chip->buffer_mutex);
  45. }
  46. /*
  47. * Internal kernel interface to transmit TPM commands
  48. */
  49. static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
  50. size_t bufsiz)
  51. {
  52. ssize_t rc;
  53. u32 count;
  54. unsigned long stop;
  55. count = be32_to_cpu(*((__be32 *) (buf + 2)));
  56. if (count == 0)
  57. return -ENODATA;
  58. if (count > bufsiz) {
  59. dev_err(&chip->pci_dev->dev,
  60. "invalid count value %x %zx \n", count, bufsiz);
  61. return -E2BIG;
  62. }
  63. down(&chip->tpm_mutex);
  64. if ((rc = chip->vendor->send(chip, (u8 *) buf, count)) < 0) {
  65. dev_err(&chip->pci_dev->dev,
  66. "tpm_transmit: tpm_send: error %zd\n", rc);
  67. goto out;
  68. }
  69. stop = jiffies + 2 * 60 * HZ;
  70. do {
  71. u8 status = inb(chip->vendor->base + 1);
  72. if ((status & chip->vendor->req_complete_mask) ==
  73. chip->vendor->req_complete_val) {
  74. goto out_recv;
  75. }
  76. if ((status == chip->vendor->req_canceled)) {
  77. dev_err(&chip->pci_dev->dev, "Operation Canceled\n");
  78. rc = -ECANCELED;
  79. goto out;
  80. }
  81. msleep(TPM_TIMEOUT); /* CHECK */
  82. rmb();
  83. } while (time_before(jiffies, stop));
  84. chip->vendor->cancel(chip);
  85. dev_err(&chip->pci_dev->dev, "Operation Timed out\n");
  86. rc = -ETIME;
  87. goto out;
  88. out_recv:
  89. rc = chip->vendor->recv(chip, (u8 *) buf, bufsiz);
  90. if (rc < 0)
  91. dev_err(&chip->pci_dev->dev,
  92. "tpm_transmit: tpm_recv: error %zd\n", rc);
  93. out:
  94. up(&chip->tpm_mutex);
  95. return rc;
  96. }
  97. #define TPM_DIGEST_SIZE 20
  98. #define CAP_PCR_RESULT_SIZE 18
  99. static const u8 cap_pcr[] = {
  100. 0, 193, /* TPM_TAG_RQU_COMMAND */
  101. 0, 0, 0, 22, /* length */
  102. 0, 0, 0, 101, /* TPM_ORD_GetCapability */
  103. 0, 0, 0, 5,
  104. 0, 0, 0, 4,
  105. 0, 0, 1, 1
  106. };
  107. #define READ_PCR_RESULT_SIZE 30
  108. static const u8 pcrread[] = {
  109. 0, 193, /* TPM_TAG_RQU_COMMAND */
  110. 0, 0, 0, 14, /* length */
  111. 0, 0, 0, 21, /* TPM_ORD_PcrRead */
  112. 0, 0, 0, 0 /* PCR index */
  113. };
  114. ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
  115. char *buf)
  116. {
  117. u8 data[READ_PCR_RESULT_SIZE];
  118. ssize_t len;
  119. int i, j, num_pcrs;
  120. __be32 index;
  121. char *str = buf;
  122. struct tpm_chip *chip =
  123. pci_get_drvdata(to_pci_dev(dev));
  124. if (chip == NULL)
  125. return -ENODEV;
  126. memcpy(data, cap_pcr, sizeof(cap_pcr));
  127. if ((len = tpm_transmit(chip, data, sizeof(data)))
  128. < CAP_PCR_RESULT_SIZE)
  129. return len;
  130. num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
  131. for (i = 0; i < num_pcrs; i++) {
  132. memcpy(data, pcrread, sizeof(pcrread));
  133. index = cpu_to_be32(i);
  134. memcpy(data + 10, &index, 4);
  135. if ((len = tpm_transmit(chip, data, sizeof(data)))
  136. < READ_PCR_RESULT_SIZE)
  137. return len;
  138. str += sprintf(str, "PCR-%02d: ", i);
  139. for (j = 0; j < TPM_DIGEST_SIZE; j++)
  140. str += sprintf(str, "%02X ", *(data + 10 + j));
  141. str += sprintf(str, "\n");
  142. }
  143. return str - buf;
  144. }
  145. EXPORT_SYMBOL_GPL(tpm_show_pcrs);
  146. #define READ_PUBEK_RESULT_SIZE 314
  147. static const u8 readpubek[] = {
  148. 0, 193, /* TPM_TAG_RQU_COMMAND */
  149. 0, 0, 0, 30, /* length */
  150. 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
  151. };
  152. ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
  153. char *buf)
  154. {
  155. u8 *data;
  156. ssize_t len;
  157. int i, rc;
  158. char *str = buf;
  159. struct tpm_chip *chip =
  160. pci_get_drvdata(to_pci_dev(dev));
  161. if (chip == NULL)
  162. return -ENODEV;
  163. data = kmalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
  164. if (!data)
  165. return -ENOMEM;
  166. memcpy(data, readpubek, sizeof(readpubek));
  167. memset(data + sizeof(readpubek), 0, 20); /* zero nonce */
  168. if ((len = tpm_transmit(chip, data, READ_PUBEK_RESULT_SIZE)) <
  169. READ_PUBEK_RESULT_SIZE) {
  170. rc = len;
  171. goto out;
  172. }
  173. /*
  174. ignore header 10 bytes
  175. algorithm 32 bits (1 == RSA )
  176. encscheme 16 bits
  177. sigscheme 16 bits
  178. parameters (RSA 12->bytes: keybit, #primes, expbit)
  179. keylenbytes 32 bits
  180. 256 byte modulus
  181. ignore checksum 20 bytes
  182. */
  183. str +=
  184. sprintf(str,
  185. "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
  186. "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
  187. " %02X %02X %02X %02X %02X %02X %02X %02X\n"
  188. "Modulus length: %d\nModulus: \n",
  189. data[10], data[11], data[12], data[13], data[14],
  190. data[15], data[16], data[17], data[22], data[23],
  191. data[24], data[25], data[26], data[27], data[28],
  192. data[29], data[30], data[31], data[32], data[33],
  193. be32_to_cpu(*((__be32 *) (data + 32))));
  194. for (i = 0; i < 256; i++) {
  195. str += sprintf(str, "%02X ", data[i + 39]);
  196. if ((i + 1) % 16 == 0)
  197. str += sprintf(str, "\n");
  198. }
  199. rc = str - buf;
  200. out:
  201. kfree(data);
  202. return rc;
  203. }
  204. EXPORT_SYMBOL_GPL(tpm_show_pubek);
  205. #define CAP_VER_RESULT_SIZE 18
  206. static const u8 cap_version[] = {
  207. 0, 193, /* TPM_TAG_RQU_COMMAND */
  208. 0, 0, 0, 18, /* length */
  209. 0, 0, 0, 101, /* TPM_ORD_GetCapability */
  210. 0, 0, 0, 6,
  211. 0, 0, 0, 0
  212. };
  213. #define CAP_MANUFACTURER_RESULT_SIZE 18
  214. static const u8 cap_manufacturer[] = {
  215. 0, 193, /* TPM_TAG_RQU_COMMAND */
  216. 0, 0, 0, 22, /* length */
  217. 0, 0, 0, 101, /* TPM_ORD_GetCapability */
  218. 0, 0, 0, 5,
  219. 0, 0, 0, 4,
  220. 0, 0, 1, 3
  221. };
  222. ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
  223. char *buf)
  224. {
  225. u8 data[sizeof(cap_manufacturer)];
  226. ssize_t len;
  227. char *str = buf;
  228. struct tpm_chip *chip =
  229. pci_get_drvdata(to_pci_dev(dev));
  230. if (chip == NULL)
  231. return -ENODEV;
  232. memcpy(data, cap_manufacturer, sizeof(cap_manufacturer));
  233. if ((len = tpm_transmit(chip, data, sizeof(data))) <
  234. CAP_MANUFACTURER_RESULT_SIZE)
  235. return len;
  236. str += sprintf(str, "Manufacturer: 0x%x\n",
  237. be32_to_cpu(*((__be32 *) (data + 14))));
  238. memcpy(data, cap_version, sizeof(cap_version));
  239. if ((len = tpm_transmit(chip, data, sizeof(data))) <
  240. CAP_VER_RESULT_SIZE)
  241. return len;
  242. str +=
  243. sprintf(str, "TCG version: %d.%d\nFirmware version: %d.%d\n",
  244. (int) data[14], (int) data[15], (int) data[16],
  245. (int) data[17]);
  246. return str - buf;
  247. }
  248. EXPORT_SYMBOL_GPL(tpm_show_caps);
  249. ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
  250. const char *buf, size_t count)
  251. {
  252. struct tpm_chip *chip = dev_get_drvdata(dev);
  253. if (chip == NULL)
  254. return 0;
  255. chip->vendor->cancel(chip);
  256. return count;
  257. }
  258. EXPORT_SYMBOL_GPL(tpm_store_cancel);
  259. /*
  260. * Device file system interface to the TPM
  261. */
  262. int tpm_open(struct inode *inode, struct file *file)
  263. {
  264. int rc = 0, minor = iminor(inode);
  265. struct tpm_chip *chip = NULL, *pos;
  266. spin_lock(&driver_lock);
  267. list_for_each_entry(pos, &tpm_chip_list, list) {
  268. if (pos->vendor->miscdev.minor == minor) {
  269. chip = pos;
  270. break;
  271. }
  272. }
  273. if (chip == NULL) {
  274. rc = -ENODEV;
  275. goto err_out;
  276. }
  277. if (chip->num_opens) {
  278. dev_dbg(&chip->pci_dev->dev,
  279. "Another process owns this TPM\n");
  280. rc = -EBUSY;
  281. goto err_out;
  282. }
  283. chip->num_opens++;
  284. pci_dev_get(chip->pci_dev);
  285. spin_unlock(&driver_lock);
  286. chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
  287. if (chip->data_buffer == NULL) {
  288. chip->num_opens--;
  289. pci_dev_put(chip->pci_dev);
  290. return -ENOMEM;
  291. }
  292. atomic_set(&chip->data_pending, 0);
  293. file->private_data = chip;
  294. return 0;
  295. err_out:
  296. spin_unlock(&driver_lock);
  297. return rc;
  298. }
  299. EXPORT_SYMBOL_GPL(tpm_open);
  300. int tpm_release(struct inode *inode, struct file *file)
  301. {
  302. struct tpm_chip *chip = file->private_data;
  303. spin_lock(&driver_lock);
  304. file->private_data = NULL;
  305. chip->num_opens--;
  306. del_singleshot_timer_sync(&chip->user_read_timer);
  307. atomic_set(&chip->data_pending, 0);
  308. pci_dev_put(chip->pci_dev);
  309. kfree(chip->data_buffer);
  310. spin_unlock(&driver_lock);
  311. return 0;
  312. }
  313. EXPORT_SYMBOL_GPL(tpm_release);
  314. ssize_t tpm_write(struct file * file, const char __user * buf,
  315. size_t size, loff_t * off)
  316. {
  317. struct tpm_chip *chip = file->private_data;
  318. int in_size = size, out_size;
  319. /* cannot perform a write until the read has cleared
  320. either via tpm_read or a user_read_timer timeout */
  321. while (atomic_read(&chip->data_pending) != 0)
  322. msleep(TPM_TIMEOUT);
  323. down(&chip->buffer_mutex);
  324. if (in_size > TPM_BUFSIZE)
  325. in_size = TPM_BUFSIZE;
  326. if (copy_from_user
  327. (chip->data_buffer, (void __user *) buf, in_size)) {
  328. up(&chip->buffer_mutex);
  329. return -EFAULT;
  330. }
  331. /* atomic tpm command send and result receive */
  332. out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
  333. atomic_set(&chip->data_pending, out_size);
  334. up(&chip->buffer_mutex);
  335. /* Set a timeout by which the reader must come claim the result */
  336. mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
  337. return in_size;
  338. }
  339. EXPORT_SYMBOL_GPL(tpm_write);
  340. ssize_t tpm_read(struct file * file, char __user * buf,
  341. size_t size, loff_t * off)
  342. {
  343. struct tpm_chip *chip = file->private_data;
  344. int ret_size;
  345. del_singleshot_timer_sync(&chip->user_read_timer);
  346. ret_size = atomic_read(&chip->data_pending);
  347. atomic_set(&chip->data_pending, 0);
  348. if (ret_size > 0) { /* relay data */
  349. if (size < ret_size)
  350. ret_size = size;
  351. down(&chip->buffer_mutex);
  352. if (copy_to_user
  353. ((void __user *) buf, chip->data_buffer, ret_size))
  354. ret_size = -EFAULT;
  355. up(&chip->buffer_mutex);
  356. }
  357. return ret_size;
  358. }
  359. EXPORT_SYMBOL_GPL(tpm_read);
  360. void __devexit tpm_remove(struct pci_dev *pci_dev)
  361. {
  362. struct tpm_chip *chip = pci_get_drvdata(pci_dev);
  363. if (chip == NULL) {
  364. dev_err(&pci_dev->dev, "No device data found\n");
  365. return;
  366. }
  367. spin_lock(&driver_lock);
  368. list_del(&chip->list);
  369. spin_unlock(&driver_lock);
  370. pci_set_drvdata(pci_dev, NULL);
  371. misc_deregister(&chip->vendor->miscdev);
  372. sysfs_remove_group(&pci_dev->dev.kobj, chip->vendor->attr_group);
  373. pci_disable_device(pci_dev);
  374. dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES ] &= !(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
  375. kfree(chip);
  376. pci_dev_put(pci_dev);
  377. }
  378. EXPORT_SYMBOL_GPL(tpm_remove);
  379. static u8 savestate[] = {
  380. 0, 193, /* TPM_TAG_RQU_COMMAND */
  381. 0, 0, 0, 10, /* blob length (in bytes) */
  382. 0, 0, 0, 152 /* TPM_ORD_SaveState */
  383. };
  384. /*
  385. * We are about to suspend. Save the TPM state
  386. * so that it can be restored.
  387. */
  388. int tpm_pm_suspend(struct pci_dev *pci_dev, pm_message_t pm_state)
  389. {
  390. struct tpm_chip *chip = pci_get_drvdata(pci_dev);
  391. if (chip == NULL)
  392. return -ENODEV;
  393. tpm_transmit(chip, savestate, sizeof(savestate));
  394. return 0;
  395. }
  396. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  397. /*
  398. * Resume from a power safe. The BIOS already restored
  399. * the TPM state.
  400. */
  401. int tpm_pm_resume(struct pci_dev *pci_dev)
  402. {
  403. struct tpm_chip *chip = pci_get_drvdata(pci_dev);
  404. if (chip == NULL)
  405. return -ENODEV;
  406. return 0;
  407. }
  408. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  409. /*
  410. * Called from tpm_<specific>.c probe function only for devices
  411. * the driver has determined it should claim. Prior to calling
  412. * this function the specific probe function has called pci_enable_device
  413. * upon errant exit from this function specific probe function should call
  414. * pci_disable_device
  415. */
  416. int tpm_register_hardware(struct pci_dev *pci_dev,
  417. struct tpm_vendor_specific *entry)
  418. {
  419. char devname[7];
  420. struct tpm_chip *chip;
  421. int i, j;
  422. /* Driver specific per-device data */
  423. chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  424. if (chip == NULL)
  425. return -ENOMEM;
  426. memset(chip, 0, sizeof(struct tpm_chip));
  427. init_MUTEX(&chip->buffer_mutex);
  428. init_MUTEX(&chip->tpm_mutex);
  429. INIT_LIST_HEAD(&chip->list);
  430. init_timer(&chip->user_read_timer);
  431. chip->user_read_timer.function = user_reader_timeout;
  432. chip->user_read_timer.data = (unsigned long) chip;
  433. chip->vendor = entry;
  434. chip->dev_num = -1;
  435. for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
  436. for (j = 0; j < 8 * sizeof(int); j++)
  437. if ((dev_mask[i] & (1 << j)) == 0) {
  438. chip->dev_num =
  439. i * TPM_NUM_MASK_ENTRIES + j;
  440. dev_mask[i] |= 1 << j;
  441. goto dev_num_search_complete;
  442. }
  443. dev_num_search_complete:
  444. if (chip->dev_num < 0) {
  445. dev_err(&pci_dev->dev,
  446. "No available tpm device numbers\n");
  447. kfree(chip);
  448. return -ENODEV;
  449. } else if (chip->dev_num == 0)
  450. chip->vendor->miscdev.minor = TPM_MINOR;
  451. else
  452. chip->vendor->miscdev.minor = MISC_DYNAMIC_MINOR;
  453. snprintf(devname, sizeof(devname), "%s%d", "tpm", chip->dev_num);
  454. chip->vendor->miscdev.name = devname;
  455. chip->vendor->miscdev.dev = &(pci_dev->dev);
  456. chip->pci_dev = pci_dev_get(pci_dev);
  457. if (misc_register(&chip->vendor->miscdev)) {
  458. dev_err(&chip->pci_dev->dev,
  459. "unable to misc_register %s, minor %d\n",
  460. chip->vendor->miscdev.name,
  461. chip->vendor->miscdev.minor);
  462. pci_dev_put(pci_dev);
  463. kfree(chip);
  464. dev_mask[i] &= !(1 << j);
  465. return -ENODEV;
  466. }
  467. spin_lock(&driver_lock);
  468. pci_set_drvdata(pci_dev, chip);
  469. list_add(&chip->list, &tpm_chip_list);
  470. spin_unlock(&driver_lock);
  471. sysfs_create_group(&pci_dev->dev.kobj, chip->vendor->attr_group);
  472. return 0;
  473. }
  474. EXPORT_SYMBOL_GPL(tpm_register_hardware);
  475. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  476. MODULE_DESCRIPTION("TPM Driver");
  477. MODULE_VERSION("2.0");
  478. MODULE_LICENSE("GPL");