tpm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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->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->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 = chip->vendor->status(chip);
  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->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->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->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 = dev_get_drvdata(dev);
  123. if (chip == NULL)
  124. return -ENODEV;
  125. memcpy(data, cap_pcr, sizeof(cap_pcr));
  126. if ((len = tpm_transmit(chip, data, sizeof(data)))
  127. < CAP_PCR_RESULT_SIZE) {
  128. dev_dbg(chip->dev, "A TPM error (%d) occurred "
  129. "attempting to determine the number of PCRS\n",
  130. be32_to_cpu(*((__be32 *) (data + 6))));
  131. return 0;
  132. }
  133. num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
  134. for (i = 0; i < num_pcrs; i++) {
  135. memcpy(data, pcrread, sizeof(pcrread));
  136. index = cpu_to_be32(i);
  137. memcpy(data + 10, &index, 4);
  138. if ((len = tpm_transmit(chip, data, sizeof(data)))
  139. < READ_PCR_RESULT_SIZE){
  140. dev_dbg(chip->dev, "A TPM error (%d) occurred"
  141. " attempting to read PCR %d of %d\n",
  142. be32_to_cpu(*((__be32 *) (data + 6))), i, num_pcrs);
  143. goto out;
  144. }
  145. str += sprintf(str, "PCR-%02d: ", i);
  146. for (j = 0; j < TPM_DIGEST_SIZE; j++)
  147. str += sprintf(str, "%02X ", *(data + 10 + j));
  148. str += sprintf(str, "\n");
  149. }
  150. out:
  151. return str - buf;
  152. }
  153. EXPORT_SYMBOL_GPL(tpm_show_pcrs);
  154. #define READ_PUBEK_RESULT_SIZE 314
  155. static const u8 readpubek[] = {
  156. 0, 193, /* TPM_TAG_RQU_COMMAND */
  157. 0, 0, 0, 30, /* length */
  158. 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
  159. };
  160. ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
  161. char *buf)
  162. {
  163. u8 *data;
  164. ssize_t len;
  165. int i, rc;
  166. char *str = buf;
  167. struct tpm_chip *chip = dev_get_drvdata(dev);
  168. if (chip == NULL)
  169. return -ENODEV;
  170. data = kmalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
  171. if (!data)
  172. return -ENOMEM;
  173. memcpy(data, readpubek, sizeof(readpubek));
  174. memset(data + sizeof(readpubek), 0, 20); /* zero nonce */
  175. if ((len = tpm_transmit(chip, data, READ_PUBEK_RESULT_SIZE)) <
  176. READ_PUBEK_RESULT_SIZE) {
  177. dev_dbg(chip->dev, "A TPM error (%d) occurred "
  178. "attempting to read the PUBEK\n",
  179. be32_to_cpu(*((__be32 *) (data + 6))));
  180. rc = 0;
  181. goto out;
  182. }
  183. /*
  184. ignore header 10 bytes
  185. algorithm 32 bits (1 == RSA )
  186. encscheme 16 bits
  187. sigscheme 16 bits
  188. parameters (RSA 12->bytes: keybit, #primes, expbit)
  189. keylenbytes 32 bits
  190. 256 byte modulus
  191. ignore checksum 20 bytes
  192. */
  193. str +=
  194. sprintf(str,
  195. "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
  196. "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
  197. " %02X %02X %02X %02X %02X %02X %02X %02X\n"
  198. "Modulus length: %d\nModulus: \n",
  199. data[10], data[11], data[12], data[13], data[14],
  200. data[15], data[16], data[17], data[22], data[23],
  201. data[24], data[25], data[26], data[27], data[28],
  202. data[29], data[30], data[31], data[32], data[33],
  203. be32_to_cpu(*((__be32 *) (data + 34))));
  204. for (i = 0; i < 256; i++) {
  205. str += sprintf(str, "%02X ", data[i + 38]);
  206. if ((i + 1) % 16 == 0)
  207. str += sprintf(str, "\n");
  208. }
  209. rc = str - buf;
  210. out:
  211. kfree(data);
  212. return rc;
  213. }
  214. EXPORT_SYMBOL_GPL(tpm_show_pubek);
  215. #define CAP_VER_RESULT_SIZE 18
  216. static const u8 cap_version[] = {
  217. 0, 193, /* TPM_TAG_RQU_COMMAND */
  218. 0, 0, 0, 18, /* length */
  219. 0, 0, 0, 101, /* TPM_ORD_GetCapability */
  220. 0, 0, 0, 6,
  221. 0, 0, 0, 0
  222. };
  223. #define CAP_MANUFACTURER_RESULT_SIZE 18
  224. static const u8 cap_manufacturer[] = {
  225. 0, 193, /* TPM_TAG_RQU_COMMAND */
  226. 0, 0, 0, 22, /* length */
  227. 0, 0, 0, 101, /* TPM_ORD_GetCapability */
  228. 0, 0, 0, 5,
  229. 0, 0, 0, 4,
  230. 0, 0, 1, 3
  231. };
  232. ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
  233. char *buf)
  234. {
  235. u8 data[sizeof(cap_manufacturer)];
  236. ssize_t len;
  237. char *str = buf;
  238. struct tpm_chip *chip = dev_get_drvdata(dev);
  239. if (chip == NULL)
  240. return -ENODEV;
  241. memcpy(data, cap_manufacturer, sizeof(cap_manufacturer));
  242. if ((len = tpm_transmit(chip, data, sizeof(data))) <
  243. CAP_MANUFACTURER_RESULT_SIZE)
  244. return len;
  245. str += sprintf(str, "Manufacturer: 0x%x\n",
  246. be32_to_cpu(*((__be32 *) (data + 14))));
  247. memcpy(data, cap_version, sizeof(cap_version));
  248. if ((len = tpm_transmit(chip, data, sizeof(data))) <
  249. CAP_VER_RESULT_SIZE)
  250. return len;
  251. str +=
  252. sprintf(str, "TCG version: %d.%d\nFirmware version: %d.%d\n",
  253. (int) data[14], (int) data[15], (int) data[16],
  254. (int) data[17]);
  255. return str - buf;
  256. }
  257. EXPORT_SYMBOL_GPL(tpm_show_caps);
  258. ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
  259. const char *buf, size_t count)
  260. {
  261. struct tpm_chip *chip = dev_get_drvdata(dev);
  262. if (chip == NULL)
  263. return 0;
  264. chip->vendor->cancel(chip);
  265. return count;
  266. }
  267. EXPORT_SYMBOL_GPL(tpm_store_cancel);
  268. /*
  269. * Device file system interface to the TPM
  270. */
  271. int tpm_open(struct inode *inode, struct file *file)
  272. {
  273. int rc = 0, minor = iminor(inode);
  274. struct tpm_chip *chip = NULL, *pos;
  275. spin_lock(&driver_lock);
  276. list_for_each_entry(pos, &tpm_chip_list, list) {
  277. if (pos->vendor->miscdev.minor == minor) {
  278. chip = pos;
  279. break;
  280. }
  281. }
  282. if (chip == NULL) {
  283. rc = -ENODEV;
  284. goto err_out;
  285. }
  286. if (chip->num_opens) {
  287. dev_dbg(chip->dev,
  288. "Another process owns this TPM\n");
  289. rc = -EBUSY;
  290. goto err_out;
  291. }
  292. chip->num_opens++;
  293. get_device(chip->dev);
  294. spin_unlock(&driver_lock);
  295. chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
  296. if (chip->data_buffer == NULL) {
  297. chip->num_opens--;
  298. put_device(chip->dev);
  299. return -ENOMEM;
  300. }
  301. atomic_set(&chip->data_pending, 0);
  302. file->private_data = chip;
  303. return 0;
  304. err_out:
  305. spin_unlock(&driver_lock);
  306. return rc;
  307. }
  308. EXPORT_SYMBOL_GPL(tpm_open);
  309. int tpm_release(struct inode *inode, struct file *file)
  310. {
  311. struct tpm_chip *chip = file->private_data;
  312. spin_lock(&driver_lock);
  313. file->private_data = NULL;
  314. chip->num_opens--;
  315. del_singleshot_timer_sync(&chip->user_read_timer);
  316. atomic_set(&chip->data_pending, 0);
  317. put_device(chip->dev);
  318. kfree(chip->data_buffer);
  319. spin_unlock(&driver_lock);
  320. return 0;
  321. }
  322. EXPORT_SYMBOL_GPL(tpm_release);
  323. ssize_t tpm_write(struct file * file, const char __user * buf,
  324. size_t size, loff_t * off)
  325. {
  326. struct tpm_chip *chip = file->private_data;
  327. int in_size = size, out_size;
  328. /* cannot perform a write until the read has cleared
  329. either via tpm_read or a user_read_timer timeout */
  330. while (atomic_read(&chip->data_pending) != 0)
  331. msleep(TPM_TIMEOUT);
  332. down(&chip->buffer_mutex);
  333. if (in_size > TPM_BUFSIZE)
  334. in_size = TPM_BUFSIZE;
  335. if (copy_from_user
  336. (chip->data_buffer, (void __user *) buf, in_size)) {
  337. up(&chip->buffer_mutex);
  338. return -EFAULT;
  339. }
  340. /* atomic tpm command send and result receive */
  341. out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
  342. atomic_set(&chip->data_pending, out_size);
  343. up(&chip->buffer_mutex);
  344. /* Set a timeout by which the reader must come claim the result */
  345. mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
  346. return in_size;
  347. }
  348. EXPORT_SYMBOL_GPL(tpm_write);
  349. ssize_t tpm_read(struct file * file, char __user * buf,
  350. size_t size, loff_t * off)
  351. {
  352. struct tpm_chip *chip = file->private_data;
  353. int ret_size;
  354. del_singleshot_timer_sync(&chip->user_read_timer);
  355. ret_size = atomic_read(&chip->data_pending);
  356. atomic_set(&chip->data_pending, 0);
  357. if (ret_size > 0) { /* relay data */
  358. if (size < ret_size)
  359. ret_size = size;
  360. down(&chip->buffer_mutex);
  361. if (copy_to_user
  362. ((void __user *) buf, chip->data_buffer, ret_size))
  363. ret_size = -EFAULT;
  364. up(&chip->buffer_mutex);
  365. }
  366. return ret_size;
  367. }
  368. EXPORT_SYMBOL_GPL(tpm_read);
  369. void tpm_remove_hardware(struct device *dev)
  370. {
  371. struct tpm_chip *chip = dev_get_drvdata(dev);
  372. if (chip == NULL) {
  373. dev_err(dev, "No device data found\n");
  374. return;
  375. }
  376. spin_lock(&driver_lock);
  377. list_del(&chip->list);
  378. spin_unlock(&driver_lock);
  379. dev_set_drvdata(dev, NULL);
  380. misc_deregister(&chip->vendor->miscdev);
  381. kfree(chip->vendor->miscdev.name);
  382. sysfs_remove_group(&dev->kobj, chip->vendor->attr_group);
  383. dev_mask[chip->dev_num / TPM_NUM_MASK_ENTRIES ] &= !(1 << (chip->dev_num % TPM_NUM_MASK_ENTRIES));
  384. kfree(chip);
  385. put_device(dev);
  386. }
  387. EXPORT_SYMBOL_GPL(tpm_remove_hardware);
  388. static u8 savestate[] = {
  389. 0, 193, /* TPM_TAG_RQU_COMMAND */
  390. 0, 0, 0, 10, /* blob length (in bytes) */
  391. 0, 0, 0, 152 /* TPM_ORD_SaveState */
  392. };
  393. /*
  394. * We are about to suspend. Save the TPM state
  395. * so that it can be restored.
  396. */
  397. int tpm_pm_suspend(struct pci_dev *pci_dev, pm_message_t pm_state)
  398. {
  399. struct tpm_chip *chip = pci_get_drvdata(pci_dev);
  400. if (chip == NULL)
  401. return -ENODEV;
  402. tpm_transmit(chip, savestate, sizeof(savestate));
  403. return 0;
  404. }
  405. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  406. /*
  407. * Resume from a power safe. The BIOS already restored
  408. * the TPM state.
  409. */
  410. int tpm_pm_resume(struct pci_dev *pci_dev)
  411. {
  412. struct tpm_chip *chip = pci_get_drvdata(pci_dev);
  413. if (chip == NULL)
  414. return -ENODEV;
  415. return 0;
  416. }
  417. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  418. /*
  419. * Called from tpm_<specific>.c probe function only for devices
  420. * the driver has determined it should claim. Prior to calling
  421. * this function the specific probe function has called pci_enable_device
  422. * upon errant exit from this function specific probe function should call
  423. * pci_disable_device
  424. */
  425. int tpm_register_hardware(struct device *dev,
  426. struct tpm_vendor_specific *entry)
  427. {
  428. #define DEVNAME_SIZE 7
  429. char *devname;
  430. struct tpm_chip *chip;
  431. int i, j;
  432. /* Driver specific per-device data */
  433. chip = kmalloc(sizeof(*chip), GFP_KERNEL);
  434. if (chip == NULL)
  435. return -ENOMEM;
  436. memset(chip, 0, sizeof(struct tpm_chip));
  437. init_MUTEX(&chip->buffer_mutex);
  438. init_MUTEX(&chip->tpm_mutex);
  439. INIT_LIST_HEAD(&chip->list);
  440. init_timer(&chip->user_read_timer);
  441. chip->user_read_timer.function = user_reader_timeout;
  442. chip->user_read_timer.data = (unsigned long) chip;
  443. chip->vendor = entry;
  444. chip->dev_num = -1;
  445. for (i = 0; i < TPM_NUM_MASK_ENTRIES; i++)
  446. for (j = 0; j < 8 * sizeof(int); j++)
  447. if ((dev_mask[i] & (1 << j)) == 0) {
  448. chip->dev_num =
  449. i * TPM_NUM_MASK_ENTRIES + j;
  450. dev_mask[i] |= 1 << j;
  451. goto dev_num_search_complete;
  452. }
  453. dev_num_search_complete:
  454. if (chip->dev_num < 0) {
  455. dev_err(dev,
  456. "No available tpm device numbers\n");
  457. kfree(chip);
  458. return -ENODEV;
  459. } else if (chip->dev_num == 0)
  460. chip->vendor->miscdev.minor = TPM_MINOR;
  461. else
  462. chip->vendor->miscdev.minor = MISC_DYNAMIC_MINOR;
  463. devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
  464. scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
  465. chip->vendor->miscdev.name = devname;
  466. chip->vendor->miscdev.dev = dev;
  467. chip->dev = get_device(dev);
  468. if (misc_register(&chip->vendor->miscdev)) {
  469. dev_err(chip->dev,
  470. "unable to misc_register %s, minor %d\n",
  471. chip->vendor->miscdev.name,
  472. chip->vendor->miscdev.minor);
  473. put_device(dev);
  474. kfree(chip);
  475. dev_mask[i] &= !(1 << j);
  476. return -ENODEV;
  477. }
  478. spin_lock(&driver_lock);
  479. dev_set_drvdata(dev, chip);
  480. list_add(&chip->list, &tpm_chip_list);
  481. spin_unlock(&driver_lock);
  482. sysfs_create_group(&dev->kobj, chip->vendor->attr_group);
  483. return 0;
  484. }
  485. EXPORT_SYMBOL_GPL(tpm_register_hardware);
  486. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  487. MODULE_DESCRIPTION("TPM Driver");
  488. MODULE_VERSION("2.0");
  489. MODULE_LICENSE("GPL");