tpm-interface.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2004 IBM Corporation
  4. * Copyright (C) 2014 Intel Corporation
  5. *
  6. * Authors:
  7. * Leendert van Doorn <leendert@watson.ibm.com>
  8. * Dave Safford <safford@watson.ibm.com>
  9. * Reiner Sailer <sailer@watson.ibm.com>
  10. * Kylene Hall <kjhall@us.ibm.com>
  11. *
  12. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  13. *
  14. * Device driver for TCG/TCPA TPM (trusted platform module).
  15. * Specifications at www.trustedcomputinggroup.org
  16. *
  17. * Note, the TPM chip is not interrupt driven (only polling)
  18. * and can have very long timeouts (minutes!). Hence the unusual
  19. * calls to msleep.
  20. */
  21. #include <linux/poll.h>
  22. #include <linux/slab.h>
  23. #include <linux/mutex.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/suspend.h>
  26. #include <linux/freezer.h>
  27. #include <linux/tpm_eventlog.h>
  28. #include "tpm.h"
  29. /*
  30. * Bug workaround - some TPM's don't flush the most
  31. * recently changed pcr on suspend, so force the flush
  32. * with an extend to the selected _unused_ non-volatile pcr.
  33. */
  34. static u32 tpm_suspend_pcr;
  35. module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
  36. MODULE_PARM_DESC(suspend_pcr,
  37. "PCR to use for dummy writes to facilitate flush on suspend.");
  38. /**
  39. * tpm_calc_ordinal_duration() - calculate the maximum command duration
  40. * @chip: TPM chip to use.
  41. * @ordinal: TPM command ordinal.
  42. *
  43. * The function returns the maximum amount of time the chip could take
  44. * to return the result for a particular ordinal in jiffies.
  45. *
  46. * Return: A maximal duration time for an ordinal in jiffies.
  47. */
  48. unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
  49. {
  50. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  51. return tpm2_calc_ordinal_duration(chip, ordinal);
  52. else
  53. return tpm1_calc_ordinal_duration(chip, ordinal);
  54. }
  55. EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
  56. static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz)
  57. {
  58. struct tpm_header *header = buf;
  59. int rc;
  60. ssize_t len = 0;
  61. u32 count, ordinal;
  62. unsigned long stop;
  63. if (bufsiz < TPM_HEADER_SIZE)
  64. return -EINVAL;
  65. if (bufsiz > TPM_BUFSIZE)
  66. bufsiz = TPM_BUFSIZE;
  67. count = be32_to_cpu(header->length);
  68. ordinal = be32_to_cpu(header->ordinal);
  69. if (count == 0)
  70. return -ENODATA;
  71. if (count > bufsiz) {
  72. dev_err(&chip->dev,
  73. "invalid count value %x %zx\n", count, bufsiz);
  74. return -E2BIG;
  75. }
  76. rc = chip->ops->send(chip, buf, count);
  77. if (rc < 0) {
  78. if (rc != -EPIPE)
  79. dev_err(&chip->dev,
  80. "%s: send(): error %d\n", __func__, rc);
  81. return rc;
  82. }
  83. /* A sanity check. send() should just return zero on success e.g.
  84. * not the command length.
  85. */
  86. if (rc > 0) {
  87. dev_warn(&chip->dev,
  88. "%s: send(): invalid value %d\n", __func__, rc);
  89. rc = 0;
  90. }
  91. if (chip->flags & TPM_CHIP_FLAG_IRQ)
  92. goto out_recv;
  93. stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
  94. do {
  95. u8 status = chip->ops->status(chip);
  96. if ((status & chip->ops->req_complete_mask) ==
  97. chip->ops->req_complete_val)
  98. goto out_recv;
  99. if (chip->ops->req_canceled(chip, status)) {
  100. dev_err(&chip->dev, "Operation Canceled\n");
  101. return -ECANCELED;
  102. }
  103. tpm_msleep(TPM_TIMEOUT_POLL);
  104. rmb();
  105. } while (time_before(jiffies, stop));
  106. chip->ops->cancel(chip);
  107. dev_err(&chip->dev, "Operation Timed out\n");
  108. return -ETIME;
  109. out_recv:
  110. len = chip->ops->recv(chip, buf, bufsiz);
  111. if (len < 0) {
  112. rc = len;
  113. dev_err(&chip->dev, "tpm_transmit: tpm_recv: error %d\n", rc);
  114. } else if (len < TPM_HEADER_SIZE || len != be32_to_cpu(header->length))
  115. rc = -EFAULT;
  116. return rc ? rc : len;
  117. }
  118. /**
  119. * tpm_transmit - Internal kernel interface to transmit TPM commands.
  120. * @chip: a TPM chip to use
  121. * @buf: a TPM command buffer
  122. * @bufsiz: length of the TPM command buffer
  123. *
  124. * A wrapper around tpm_try_transmit() that handles TPM2_RC_RETRY returns from
  125. * the TPM and retransmits the command after a delay up to a maximum wait of
  126. * TPM2_DURATION_LONG.
  127. *
  128. * Note that TPM 1.x never returns TPM2_RC_RETRY so the retry logic is TPM 2.0
  129. * only.
  130. *
  131. * Return:
  132. * * The response length - OK
  133. * * -errno - A system error
  134. */
  135. ssize_t tpm_transmit(struct tpm_chip *chip, u8 *buf, size_t bufsiz)
  136. {
  137. struct tpm_header *header = (struct tpm_header *)buf;
  138. /* space for header and handles */
  139. u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
  140. unsigned int delay_msec = TPM2_DURATION_SHORT;
  141. u32 rc = 0;
  142. ssize_t ret;
  143. const size_t save_size = min(sizeof(save), bufsiz);
  144. /* the command code is where the return code will be */
  145. u32 cc = be32_to_cpu(header->return_code);
  146. /*
  147. * Subtlety here: if we have a space, the handles will be
  148. * transformed, so when we restore the header we also have to
  149. * restore the handles.
  150. */
  151. memcpy(save, buf, save_size);
  152. for (;;) {
  153. ret = tpm_try_transmit(chip, buf, bufsiz);
  154. if (ret < 0)
  155. break;
  156. rc = be32_to_cpu(header->return_code);
  157. if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING)
  158. break;
  159. /*
  160. * return immediately if self test returns test
  161. * still running to shorten boot time.
  162. */
  163. if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
  164. break;
  165. if (delay_msec > TPM2_DURATION_LONG) {
  166. if (rc == TPM2_RC_RETRY)
  167. dev_err(&chip->dev, "in retry loop\n");
  168. else
  169. dev_err(&chip->dev,
  170. "self test is still running\n");
  171. break;
  172. }
  173. tpm_msleep(delay_msec);
  174. delay_msec *= 2;
  175. memcpy(buf, save, save_size);
  176. }
  177. return ret;
  178. }
  179. /**
  180. * tpm_transmit_cmd - send a tpm command to the device
  181. * @chip: a TPM chip to use
  182. * @buf: a TPM command buffer
  183. * @min_rsp_body_length: minimum expected length of response body
  184. * @desc: command description used in the error message
  185. *
  186. * Return:
  187. * * 0 - OK
  188. * * -errno - A system error
  189. * * TPM_RC - A TPM error
  190. */
  191. ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
  192. size_t min_rsp_body_length, const char *desc)
  193. {
  194. const struct tpm_header *header = (struct tpm_header *)buf->data;
  195. int err;
  196. ssize_t len;
  197. len = tpm_transmit(chip, buf->data, PAGE_SIZE);
  198. if (len < 0)
  199. return len;
  200. err = be32_to_cpu(header->return_code);
  201. if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED
  202. && err != TPM2_RC_TESTING && desc)
  203. dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
  204. desc);
  205. if (err)
  206. return err;
  207. if (len < min_rsp_body_length + TPM_HEADER_SIZE)
  208. return -EFAULT;
  209. buf->length = len;
  210. return 0;
  211. }
  212. EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
  213. int tpm_get_timeouts(struct tpm_chip *chip)
  214. {
  215. if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
  216. return 0;
  217. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  218. return tpm2_get_timeouts(chip);
  219. else
  220. return tpm1_get_timeouts(chip);
  221. }
  222. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  223. /**
  224. * tpm_is_tpm2 - do we a have a TPM2 chip?
  225. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  226. *
  227. * Return:
  228. * 1 if we have a TPM2 chip.
  229. * 0 if we don't have a TPM2 chip.
  230. * A negative number for system errors (errno).
  231. */
  232. int tpm_is_tpm2(struct tpm_chip *chip)
  233. {
  234. int rc;
  235. chip = tpm_find_get_ops(chip);
  236. if (!chip)
  237. return -ENODEV;
  238. rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
  239. tpm_put_ops(chip);
  240. return rc;
  241. }
  242. EXPORT_SYMBOL_GPL(tpm_is_tpm2);
  243. /**
  244. * tpm_pcr_read - read a PCR value from SHA1 bank
  245. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  246. * @pcr_idx: the PCR to be retrieved
  247. * @digest: the PCR bank and buffer current PCR value is written to
  248. *
  249. * Return: same as with tpm_transmit_cmd()
  250. */
  251. int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
  252. struct tpm_digest *digest)
  253. {
  254. int rc;
  255. chip = tpm_find_get_ops(chip);
  256. if (!chip)
  257. return -ENODEV;
  258. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  259. rc = tpm2_pcr_read(chip, pcr_idx, digest, NULL);
  260. else
  261. rc = tpm1_pcr_read(chip, pcr_idx, digest->digest);
  262. tpm_put_ops(chip);
  263. return rc;
  264. }
  265. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  266. /**
  267. * tpm_pcr_extend - extend a PCR value in SHA1 bank.
  268. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  269. * @pcr_idx: the PCR to be retrieved
  270. * @digests: array of tpm_digest structures used to extend PCRs
  271. *
  272. * Note: callers must pass a digest for every allocated PCR bank, in the same
  273. * order of the banks in chip->allocated_banks.
  274. *
  275. * Return: same as with tpm_transmit_cmd()
  276. */
  277. int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
  278. struct tpm_digest *digests)
  279. {
  280. int rc;
  281. int i;
  282. chip = tpm_find_get_ops(chip);
  283. if (!chip)
  284. return -ENODEV;
  285. for (i = 0; i < chip->nr_allocated_banks; i++) {
  286. if (digests[i].alg_id != chip->allocated_banks[i].alg_id) {
  287. rc = -EINVAL;
  288. goto out;
  289. }
  290. }
  291. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  292. rc = tpm2_pcr_extend(chip, pcr_idx, digests);
  293. goto out;
  294. }
  295. rc = tpm1_pcr_extend(chip, pcr_idx, digests[0].digest,
  296. "attempting extend a PCR value");
  297. out:
  298. tpm_put_ops(chip);
  299. return rc;
  300. }
  301. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  302. int tpm_auto_startup(struct tpm_chip *chip)
  303. {
  304. int rc;
  305. if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
  306. return 0;
  307. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  308. rc = tpm2_auto_startup(chip);
  309. else
  310. rc = tpm1_auto_startup(chip);
  311. return rc;
  312. }
  313. /*
  314. * We are about to suspend. Save the TPM state
  315. * so that it can be restored.
  316. */
  317. int tpm_pm_suspend(struct device *dev)
  318. {
  319. struct tpm_chip *chip = dev_get_drvdata(dev);
  320. int rc = 0;
  321. if (!chip)
  322. return -ENODEV;
  323. rc = tpm_try_get_ops(chip);
  324. if (rc) {
  325. /* Can be safely set out of locks, as no action cannot race: */
  326. chip->flags |= TPM_CHIP_FLAG_SUSPENDED;
  327. goto out;
  328. }
  329. if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
  330. goto suspended;
  331. if ((chip->flags & TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED) &&
  332. !pm_suspend_via_firmware())
  333. goto suspended;
  334. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  335. tpm2_end_auth_session(chip);
  336. tpm2_shutdown(chip, TPM2_SU_STATE);
  337. goto suspended;
  338. }
  339. rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
  340. suspended:
  341. chip->flags |= TPM_CHIP_FLAG_SUSPENDED;
  342. tpm_put_ops(chip);
  343. out:
  344. if (rc)
  345. dev_err(dev, "Ignoring error %d while suspending\n", rc);
  346. return 0;
  347. }
  348. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  349. /*
  350. * Resume from a power safe. The BIOS already restored
  351. * the TPM state.
  352. */
  353. int tpm_pm_resume(struct device *dev)
  354. {
  355. struct tpm_chip *chip = dev_get_drvdata(dev);
  356. if (chip == NULL)
  357. return -ENODEV;
  358. chip->flags &= ~TPM_CHIP_FLAG_SUSPENDED;
  359. /*
  360. * Guarantee that SUSPENDED is written last, so that hwrng does not
  361. * activate before the chip has been fully resumed.
  362. */
  363. wmb();
  364. return 0;
  365. }
  366. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  367. /**
  368. * tpm_get_random() - get random bytes from the TPM's RNG
  369. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  370. * @out: destination buffer for the random bytes
  371. * @max: the max number of bytes to write to @out
  372. *
  373. * Return: number of random bytes read or a negative error value.
  374. */
  375. int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
  376. {
  377. int rc;
  378. if (!out || max > TPM_MAX_RNG_DATA)
  379. return -EINVAL;
  380. chip = tpm_find_get_ops(chip);
  381. if (!chip)
  382. return -ENODEV;
  383. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  384. rc = tpm2_get_random(chip, out, max);
  385. else
  386. rc = tpm1_get_random(chip, out, max);
  387. tpm_put_ops(chip);
  388. return rc;
  389. }
  390. EXPORT_SYMBOL_GPL(tpm_get_random);
  391. static int __init tpm_init(void)
  392. {
  393. int rc;
  394. rc = class_register(&tpm_class);
  395. if (rc) {
  396. pr_err("couldn't create tpm class\n");
  397. return rc;
  398. }
  399. rc = class_register(&tpmrm_class);
  400. if (rc) {
  401. pr_err("couldn't create tpmrm class\n");
  402. goto out_destroy_tpm_class;
  403. }
  404. rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
  405. if (rc < 0) {
  406. pr_err("tpm: failed to allocate char dev region\n");
  407. goto out_destroy_tpmrm_class;
  408. }
  409. rc = tpm_dev_common_init();
  410. if (rc) {
  411. pr_err("tpm: failed to allocate char dev region\n");
  412. goto out_unreg_chrdev;
  413. }
  414. return 0;
  415. out_unreg_chrdev:
  416. unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
  417. out_destroy_tpmrm_class:
  418. class_unregister(&tpmrm_class);
  419. out_destroy_tpm_class:
  420. class_unregister(&tpm_class);
  421. return rc;
  422. }
  423. static void __exit tpm_exit(void)
  424. {
  425. idr_destroy(&dev_nums_idr);
  426. class_unregister(&tpm_class);
  427. class_unregister(&tpmrm_class);
  428. unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
  429. tpm_dev_common_exit();
  430. }
  431. subsys_initcall(tpm_init);
  432. module_exit(tpm_exit);
  433. MODULE_AUTHOR("Leendert van Doorn <leendert@watson.ibm.com>");
  434. MODULE_DESCRIPTION("TPM Driver");
  435. MODULE_VERSION("2.0");
  436. MODULE_LICENSE("GPL");