tpm-interface.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 <[email protected]>
  8. * Dave Safford <[email protected]>
  9. * Reiner Sailer <[email protected]>
  10. * Kylene Hall <[email protected]>
  11. *
  12. * Maintained by: <[email protected]>
  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. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
  212. int tpm_get_timeouts(struct tpm_chip *chip)
  213. {
  214. if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
  215. return 0;
  216. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  217. return tpm2_get_timeouts(chip);
  218. else
  219. return tpm1_get_timeouts(chip);
  220. }
  221. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  222. /**
  223. * tpm_is_tpm2 - do we a have a TPM2 chip?
  224. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  225. *
  226. * Return:
  227. * 1 if we have a TPM2 chip.
  228. * 0 if we don't have a TPM2 chip.
  229. * A negative number for system errors (errno).
  230. */
  231. int tpm_is_tpm2(struct tpm_chip *chip)
  232. {
  233. int rc;
  234. chip = tpm_find_get_ops(chip);
  235. if (!chip)
  236. return -ENODEV;
  237. rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
  238. tpm_put_ops(chip);
  239. return rc;
  240. }
  241. EXPORT_SYMBOL_GPL(tpm_is_tpm2);
  242. /**
  243. * tpm_pcr_read - read a PCR value from SHA1 bank
  244. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  245. * @pcr_idx: the PCR to be retrieved
  246. * @digest: the PCR bank and buffer current PCR value is written to
  247. *
  248. * Return: same as with tpm_transmit_cmd()
  249. */
  250. int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
  251. struct tpm_digest *digest)
  252. {
  253. int rc;
  254. chip = tpm_find_get_ops(chip);
  255. if (!chip)
  256. return -ENODEV;
  257. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  258. rc = tpm2_pcr_read(chip, pcr_idx, digest, NULL);
  259. else
  260. rc = tpm1_pcr_read(chip, pcr_idx, digest->digest);
  261. tpm_put_ops(chip);
  262. return rc;
  263. }
  264. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  265. /**
  266. * tpm_pcr_extend - extend a PCR value in SHA1 bank.
  267. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  268. * @pcr_idx: the PCR to be retrieved
  269. * @digests: array of tpm_digest structures used to extend PCRs
  270. *
  271. * Note: callers must pass a digest for every allocated PCR bank, in the same
  272. * order of the banks in chip->allocated_banks.
  273. *
  274. * Return: same as with tpm_transmit_cmd()
  275. */
  276. int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
  277. struct tpm_digest *digests)
  278. {
  279. int rc;
  280. int i;
  281. chip = tpm_find_get_ops(chip);
  282. if (!chip)
  283. return -ENODEV;
  284. for (i = 0; i < chip->nr_allocated_banks; i++) {
  285. if (digests[i].alg_id != chip->allocated_banks[i].alg_id) {
  286. rc = -EINVAL;
  287. goto out;
  288. }
  289. }
  290. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  291. rc = tpm2_pcr_extend(chip, pcr_idx, digests);
  292. goto out;
  293. }
  294. rc = tpm1_pcr_extend(chip, pcr_idx, digests[0].digest,
  295. "attempting extend a PCR value");
  296. out:
  297. tpm_put_ops(chip);
  298. return rc;
  299. }
  300. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  301. /**
  302. * tpm_send - send a TPM command
  303. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  304. * @cmd: a TPM command buffer
  305. * @buflen: the length of the TPM command buffer
  306. *
  307. * Return: same as with tpm_transmit_cmd()
  308. */
  309. int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
  310. {
  311. struct tpm_buf buf;
  312. int rc;
  313. chip = tpm_find_get_ops(chip);
  314. if (!chip)
  315. return -ENODEV;
  316. buf.data = cmd;
  317. rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to a send a command");
  318. tpm_put_ops(chip);
  319. return rc;
  320. }
  321. EXPORT_SYMBOL_GPL(tpm_send);
  322. int tpm_auto_startup(struct tpm_chip *chip)
  323. {
  324. int rc;
  325. if (!(chip->ops->flags & TPM_OPS_AUTO_STARTUP))
  326. return 0;
  327. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  328. rc = tpm2_auto_startup(chip);
  329. else
  330. rc = tpm1_auto_startup(chip);
  331. return rc;
  332. }
  333. /*
  334. * We are about to suspend. Save the TPM state
  335. * so that it can be restored.
  336. */
  337. int tpm_pm_suspend(struct device *dev)
  338. {
  339. struct tpm_chip *chip = dev_get_drvdata(dev);
  340. int rc = 0;
  341. if (!chip)
  342. return -ENODEV;
  343. if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
  344. goto suspended;
  345. if ((chip->flags & TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED) &&
  346. !pm_suspend_via_firmware())
  347. goto suspended;
  348. rc = tpm_try_get_ops(chip);
  349. if (!rc) {
  350. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  351. tpm2_shutdown(chip, TPM2_SU_STATE);
  352. else
  353. rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
  354. tpm_put_ops(chip);
  355. }
  356. suspended:
  357. chip->flags |= TPM_CHIP_FLAG_SUSPENDED;
  358. if (rc)
  359. dev_err(dev, "Ignoring error %d while suspending\n", rc);
  360. return 0;
  361. }
  362. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  363. /*
  364. * Resume from a power safe. The BIOS already restored
  365. * the TPM state.
  366. */
  367. int tpm_pm_resume(struct device *dev)
  368. {
  369. struct tpm_chip *chip = dev_get_drvdata(dev);
  370. if (chip == NULL)
  371. return -ENODEV;
  372. chip->flags &= ~TPM_CHIP_FLAG_SUSPENDED;
  373. /*
  374. * Guarantee that SUSPENDED is written last, so that hwrng does not
  375. * activate before the chip has been fully resumed.
  376. */
  377. wmb();
  378. return 0;
  379. }
  380. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  381. /**
  382. * tpm_get_random() - get random bytes from the TPM's RNG
  383. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  384. * @out: destination buffer for the random bytes
  385. * @max: the max number of bytes to write to @out
  386. *
  387. * Return: number of random bytes read or a negative error value.
  388. */
  389. int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
  390. {
  391. int rc;
  392. if (!out || max > TPM_MAX_RNG_DATA)
  393. return -EINVAL;
  394. chip = tpm_find_get_ops(chip);
  395. if (!chip)
  396. return -ENODEV;
  397. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  398. rc = tpm2_get_random(chip, out, max);
  399. else
  400. rc = tpm1_get_random(chip, out, max);
  401. tpm_put_ops(chip);
  402. return rc;
  403. }
  404. EXPORT_SYMBOL_GPL(tpm_get_random);
  405. static int __init tpm_init(void)
  406. {
  407. int rc;
  408. tpm_class = class_create(THIS_MODULE, "tpm");
  409. if (IS_ERR(tpm_class)) {
  410. pr_err("couldn't create tpm class\n");
  411. return PTR_ERR(tpm_class);
  412. }
  413. tpmrm_class = class_create(THIS_MODULE, "tpmrm");
  414. if (IS_ERR(tpmrm_class)) {
  415. pr_err("couldn't create tpmrm class\n");
  416. rc = PTR_ERR(tpmrm_class);
  417. goto out_destroy_tpm_class;
  418. }
  419. rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
  420. if (rc < 0) {
  421. pr_err("tpm: failed to allocate char dev region\n");
  422. goto out_destroy_tpmrm_class;
  423. }
  424. rc = tpm_dev_common_init();
  425. if (rc) {
  426. pr_err("tpm: failed to allocate char dev region\n");
  427. goto out_unreg_chrdev;
  428. }
  429. return 0;
  430. out_unreg_chrdev:
  431. unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
  432. out_destroy_tpmrm_class:
  433. class_destroy(tpmrm_class);
  434. out_destroy_tpm_class:
  435. class_destroy(tpm_class);
  436. return rc;
  437. }
  438. static void __exit tpm_exit(void)
  439. {
  440. idr_destroy(&dev_nums_idr);
  441. class_destroy(tpm_class);
  442. class_destroy(tpmrm_class);
  443. unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
  444. tpm_dev_common_exit();
  445. }
  446. subsys_initcall(tpm_init);
  447. module_exit(tpm_exit);
  448. MODULE_AUTHOR("Leendert van Doorn ([email protected])");
  449. MODULE_DESCRIPTION("TPM Driver");
  450. MODULE_VERSION("2.0");
  451. MODULE_LICENSE("GPL");