tpm-interface.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. * Copyright (C) 2014 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. * Note, the TPM chip is not interrupt driven (only polling)
  22. * and can have very long timeouts (minutes!). Hence the unusual
  23. * calls to msleep.
  24. *
  25. */
  26. #include <linux/poll.h>
  27. #include <linux/slab.h>
  28. #include <linux/mutex.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/freezer.h>
  31. #include "tpm.h"
  32. #include "tpm_eventlog.h"
  33. #define TPM_MAX_ORDINAL 243
  34. #define TSC_MAX_ORDINAL 12
  35. #define TPM_PROTECTED_COMMAND 0x00
  36. #define TPM_CONNECTION_COMMAND 0x40
  37. /*
  38. * Bug workaround - some TPM's don't flush the most
  39. * recently changed pcr on suspend, so force the flush
  40. * with an extend to the selected _unused_ non-volatile pcr.
  41. */
  42. static int tpm_suspend_pcr;
  43. module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
  44. MODULE_PARM_DESC(suspend_pcr,
  45. "PCR to use for dummy writes to faciltate flush on suspend.");
  46. /*
  47. * Array with one entry per ordinal defining the maximum amount
  48. * of time the chip could take to return the result. The ordinal
  49. * designation of short, medium or long is defined in a table in
  50. * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
  51. * values of the SHORT, MEDIUM, and LONG durations are retrieved
  52. * from the chip during initialization with a call to tpm_get_timeouts.
  53. */
  54. static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
  55. TPM_UNDEFINED, /* 0 */
  56. TPM_UNDEFINED,
  57. TPM_UNDEFINED,
  58. TPM_UNDEFINED,
  59. TPM_UNDEFINED,
  60. TPM_UNDEFINED, /* 5 */
  61. TPM_UNDEFINED,
  62. TPM_UNDEFINED,
  63. TPM_UNDEFINED,
  64. TPM_UNDEFINED,
  65. TPM_SHORT, /* 10 */
  66. TPM_SHORT,
  67. TPM_MEDIUM,
  68. TPM_LONG,
  69. TPM_LONG,
  70. TPM_MEDIUM, /* 15 */
  71. TPM_SHORT,
  72. TPM_SHORT,
  73. TPM_MEDIUM,
  74. TPM_LONG,
  75. TPM_SHORT, /* 20 */
  76. TPM_SHORT,
  77. TPM_MEDIUM,
  78. TPM_MEDIUM,
  79. TPM_MEDIUM,
  80. TPM_SHORT, /* 25 */
  81. TPM_SHORT,
  82. TPM_MEDIUM,
  83. TPM_SHORT,
  84. TPM_SHORT,
  85. TPM_MEDIUM, /* 30 */
  86. TPM_LONG,
  87. TPM_MEDIUM,
  88. TPM_SHORT,
  89. TPM_SHORT,
  90. TPM_SHORT, /* 35 */
  91. TPM_MEDIUM,
  92. TPM_MEDIUM,
  93. TPM_UNDEFINED,
  94. TPM_UNDEFINED,
  95. TPM_MEDIUM, /* 40 */
  96. TPM_LONG,
  97. TPM_MEDIUM,
  98. TPM_SHORT,
  99. TPM_SHORT,
  100. TPM_SHORT, /* 45 */
  101. TPM_SHORT,
  102. TPM_SHORT,
  103. TPM_SHORT,
  104. TPM_LONG,
  105. TPM_MEDIUM, /* 50 */
  106. TPM_MEDIUM,
  107. TPM_UNDEFINED,
  108. TPM_UNDEFINED,
  109. TPM_UNDEFINED,
  110. TPM_UNDEFINED, /* 55 */
  111. TPM_UNDEFINED,
  112. TPM_UNDEFINED,
  113. TPM_UNDEFINED,
  114. TPM_UNDEFINED,
  115. TPM_MEDIUM, /* 60 */
  116. TPM_MEDIUM,
  117. TPM_MEDIUM,
  118. TPM_SHORT,
  119. TPM_SHORT,
  120. TPM_MEDIUM, /* 65 */
  121. TPM_UNDEFINED,
  122. TPM_UNDEFINED,
  123. TPM_UNDEFINED,
  124. TPM_UNDEFINED,
  125. TPM_SHORT, /* 70 */
  126. TPM_SHORT,
  127. TPM_UNDEFINED,
  128. TPM_UNDEFINED,
  129. TPM_UNDEFINED,
  130. TPM_UNDEFINED, /* 75 */
  131. TPM_UNDEFINED,
  132. TPM_UNDEFINED,
  133. TPM_UNDEFINED,
  134. TPM_UNDEFINED,
  135. TPM_LONG, /* 80 */
  136. TPM_UNDEFINED,
  137. TPM_MEDIUM,
  138. TPM_LONG,
  139. TPM_SHORT,
  140. TPM_UNDEFINED, /* 85 */
  141. TPM_UNDEFINED,
  142. TPM_UNDEFINED,
  143. TPM_UNDEFINED,
  144. TPM_UNDEFINED,
  145. TPM_SHORT, /* 90 */
  146. TPM_SHORT,
  147. TPM_SHORT,
  148. TPM_SHORT,
  149. TPM_SHORT,
  150. TPM_UNDEFINED, /* 95 */
  151. TPM_UNDEFINED,
  152. TPM_UNDEFINED,
  153. TPM_UNDEFINED,
  154. TPM_UNDEFINED,
  155. TPM_MEDIUM, /* 100 */
  156. TPM_SHORT,
  157. TPM_SHORT,
  158. TPM_UNDEFINED,
  159. TPM_UNDEFINED,
  160. TPM_UNDEFINED, /* 105 */
  161. TPM_UNDEFINED,
  162. TPM_UNDEFINED,
  163. TPM_UNDEFINED,
  164. TPM_UNDEFINED,
  165. TPM_SHORT, /* 110 */
  166. TPM_SHORT,
  167. TPM_SHORT,
  168. TPM_SHORT,
  169. TPM_SHORT,
  170. TPM_SHORT, /* 115 */
  171. TPM_SHORT,
  172. TPM_SHORT,
  173. TPM_UNDEFINED,
  174. TPM_UNDEFINED,
  175. TPM_LONG, /* 120 */
  176. TPM_LONG,
  177. TPM_MEDIUM,
  178. TPM_UNDEFINED,
  179. TPM_SHORT,
  180. TPM_SHORT, /* 125 */
  181. TPM_SHORT,
  182. TPM_LONG,
  183. TPM_SHORT,
  184. TPM_SHORT,
  185. TPM_SHORT, /* 130 */
  186. TPM_MEDIUM,
  187. TPM_UNDEFINED,
  188. TPM_SHORT,
  189. TPM_MEDIUM,
  190. TPM_UNDEFINED, /* 135 */
  191. TPM_UNDEFINED,
  192. TPM_UNDEFINED,
  193. TPM_UNDEFINED,
  194. TPM_UNDEFINED,
  195. TPM_SHORT, /* 140 */
  196. TPM_SHORT,
  197. TPM_UNDEFINED,
  198. TPM_UNDEFINED,
  199. TPM_UNDEFINED,
  200. TPM_UNDEFINED, /* 145 */
  201. TPM_UNDEFINED,
  202. TPM_UNDEFINED,
  203. TPM_UNDEFINED,
  204. TPM_UNDEFINED,
  205. TPM_SHORT, /* 150 */
  206. TPM_MEDIUM,
  207. TPM_MEDIUM,
  208. TPM_SHORT,
  209. TPM_SHORT,
  210. TPM_UNDEFINED, /* 155 */
  211. TPM_UNDEFINED,
  212. TPM_UNDEFINED,
  213. TPM_UNDEFINED,
  214. TPM_UNDEFINED,
  215. TPM_SHORT, /* 160 */
  216. TPM_SHORT,
  217. TPM_SHORT,
  218. TPM_SHORT,
  219. TPM_UNDEFINED,
  220. TPM_UNDEFINED, /* 165 */
  221. TPM_UNDEFINED,
  222. TPM_UNDEFINED,
  223. TPM_UNDEFINED,
  224. TPM_UNDEFINED,
  225. TPM_LONG, /* 170 */
  226. TPM_UNDEFINED,
  227. TPM_UNDEFINED,
  228. TPM_UNDEFINED,
  229. TPM_UNDEFINED,
  230. TPM_UNDEFINED, /* 175 */
  231. TPM_UNDEFINED,
  232. TPM_UNDEFINED,
  233. TPM_UNDEFINED,
  234. TPM_UNDEFINED,
  235. TPM_MEDIUM, /* 180 */
  236. TPM_SHORT,
  237. TPM_MEDIUM,
  238. TPM_MEDIUM,
  239. TPM_MEDIUM,
  240. TPM_MEDIUM, /* 185 */
  241. TPM_SHORT,
  242. TPM_UNDEFINED,
  243. TPM_UNDEFINED,
  244. TPM_UNDEFINED,
  245. TPM_UNDEFINED, /* 190 */
  246. TPM_UNDEFINED,
  247. TPM_UNDEFINED,
  248. TPM_UNDEFINED,
  249. TPM_UNDEFINED,
  250. TPM_UNDEFINED, /* 195 */
  251. TPM_UNDEFINED,
  252. TPM_UNDEFINED,
  253. TPM_UNDEFINED,
  254. TPM_UNDEFINED,
  255. TPM_SHORT, /* 200 */
  256. TPM_UNDEFINED,
  257. TPM_UNDEFINED,
  258. TPM_UNDEFINED,
  259. TPM_SHORT,
  260. TPM_SHORT, /* 205 */
  261. TPM_SHORT,
  262. TPM_SHORT,
  263. TPM_SHORT,
  264. TPM_SHORT,
  265. TPM_MEDIUM, /* 210 */
  266. TPM_UNDEFINED,
  267. TPM_MEDIUM,
  268. TPM_MEDIUM,
  269. TPM_MEDIUM,
  270. TPM_UNDEFINED, /* 215 */
  271. TPM_MEDIUM,
  272. TPM_UNDEFINED,
  273. TPM_UNDEFINED,
  274. TPM_SHORT,
  275. TPM_SHORT, /* 220 */
  276. TPM_SHORT,
  277. TPM_SHORT,
  278. TPM_SHORT,
  279. TPM_SHORT,
  280. TPM_UNDEFINED, /* 225 */
  281. TPM_UNDEFINED,
  282. TPM_UNDEFINED,
  283. TPM_UNDEFINED,
  284. TPM_UNDEFINED,
  285. TPM_SHORT, /* 230 */
  286. TPM_LONG,
  287. TPM_MEDIUM,
  288. TPM_UNDEFINED,
  289. TPM_UNDEFINED,
  290. TPM_UNDEFINED, /* 235 */
  291. TPM_UNDEFINED,
  292. TPM_UNDEFINED,
  293. TPM_UNDEFINED,
  294. TPM_UNDEFINED,
  295. TPM_SHORT, /* 240 */
  296. TPM_UNDEFINED,
  297. TPM_MEDIUM,
  298. };
  299. /*
  300. * Returns max number of jiffies to wait
  301. */
  302. unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
  303. u32 ordinal)
  304. {
  305. int duration_idx = TPM_UNDEFINED;
  306. int duration = 0;
  307. u8 category = (ordinal >> 24) & 0xFF;
  308. if ((category == TPM_PROTECTED_COMMAND && ordinal < TPM_MAX_ORDINAL) ||
  309. (category == TPM_CONNECTION_COMMAND && ordinal < TSC_MAX_ORDINAL))
  310. duration_idx = tpm_ordinal_duration[ordinal];
  311. if (duration_idx != TPM_UNDEFINED)
  312. duration = chip->vendor.duration[duration_idx];
  313. if (duration <= 0)
  314. return 2 * 60 * HZ;
  315. else
  316. return duration;
  317. }
  318. EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
  319. /*
  320. * Internal kernel interface to transmit TPM commands
  321. */
  322. ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
  323. size_t bufsiz)
  324. {
  325. ssize_t rc;
  326. u32 count, ordinal;
  327. unsigned long stop;
  328. if (bufsiz > TPM_BUFSIZE)
  329. bufsiz = TPM_BUFSIZE;
  330. count = be32_to_cpu(*((__be32 *) (buf + 2)));
  331. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  332. if (count == 0)
  333. return -ENODATA;
  334. if (count > bufsiz) {
  335. dev_err(chip->pdev,
  336. "invalid count value %x %zx\n", count, bufsiz);
  337. return -E2BIG;
  338. }
  339. mutex_lock(&chip->tpm_mutex);
  340. rc = chip->ops->send(chip, (u8 *) buf, count);
  341. if (rc < 0) {
  342. dev_err(chip->pdev,
  343. "tpm_transmit: tpm_send: error %zd\n", rc);
  344. goto out;
  345. }
  346. if (chip->vendor.irq)
  347. goto out_recv;
  348. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  349. stop = jiffies + tpm2_calc_ordinal_duration(chip, ordinal);
  350. else
  351. stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
  352. do {
  353. u8 status = chip->ops->status(chip);
  354. if ((status & chip->ops->req_complete_mask) ==
  355. chip->ops->req_complete_val)
  356. goto out_recv;
  357. if (chip->ops->req_canceled(chip, status)) {
  358. dev_err(chip->pdev, "Operation Canceled\n");
  359. rc = -ECANCELED;
  360. goto out;
  361. }
  362. msleep(TPM_TIMEOUT); /* CHECK */
  363. rmb();
  364. } while (time_before(jiffies, stop));
  365. chip->ops->cancel(chip);
  366. dev_err(chip->pdev, "Operation Timed out\n");
  367. rc = -ETIME;
  368. goto out;
  369. out_recv:
  370. rc = chip->ops->recv(chip, (u8 *) buf, bufsiz);
  371. if (rc < 0)
  372. dev_err(chip->pdev,
  373. "tpm_transmit: tpm_recv: error %zd\n", rc);
  374. out:
  375. mutex_unlock(&chip->tpm_mutex);
  376. return rc;
  377. }
  378. #define TPM_DIGEST_SIZE 20
  379. #define TPM_RET_CODE_IDX 6
  380. ssize_t tpm_transmit_cmd(struct tpm_chip *chip, void *cmd,
  381. int len, const char *desc)
  382. {
  383. struct tpm_output_header *header;
  384. int err;
  385. len = tpm_transmit(chip, (u8 *) cmd, len);
  386. if (len < 0)
  387. return len;
  388. else if (len < TPM_HEADER_SIZE)
  389. return -EFAULT;
  390. header = cmd;
  391. err = be32_to_cpu(header->return_code);
  392. if (err != 0 && desc)
  393. dev_err(chip->pdev, "A TPM error (%d) occurred %s\n", err,
  394. desc);
  395. return err;
  396. }
  397. #define TPM_INTERNAL_RESULT_SIZE 200
  398. #define TPM_ORD_GET_CAP cpu_to_be32(101)
  399. #define TPM_ORD_GET_RANDOM cpu_to_be32(70)
  400. static const struct tpm_input_header tpm_getcap_header = {
  401. .tag = TPM_TAG_RQU_COMMAND,
  402. .length = cpu_to_be32(22),
  403. .ordinal = TPM_ORD_GET_CAP
  404. };
  405. ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
  406. const char *desc)
  407. {
  408. struct tpm_cmd_t tpm_cmd;
  409. int rc;
  410. struct tpm_chip *chip = dev_get_drvdata(dev);
  411. tpm_cmd.header.in = tpm_getcap_header;
  412. if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
  413. tpm_cmd.params.getcap_in.cap = subcap_id;
  414. /*subcap field not necessary */
  415. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
  416. tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
  417. } else {
  418. if (subcap_id == TPM_CAP_FLAG_PERM ||
  419. subcap_id == TPM_CAP_FLAG_VOL)
  420. tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
  421. else
  422. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  423. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  424. tpm_cmd.params.getcap_in.subcap = subcap_id;
  425. }
  426. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
  427. if (!rc)
  428. *cap = tpm_cmd.params.getcap_out.cap;
  429. return rc;
  430. }
  431. void tpm_gen_interrupt(struct tpm_chip *chip)
  432. {
  433. struct tpm_cmd_t tpm_cmd;
  434. ssize_t rc;
  435. tpm_cmd.header.in = tpm_getcap_header;
  436. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  437. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  438. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  439. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  440. "attempting to determine the timeouts");
  441. }
  442. EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
  443. #define TPM_ORD_STARTUP cpu_to_be32(153)
  444. #define TPM_ST_CLEAR cpu_to_be16(1)
  445. #define TPM_ST_STATE cpu_to_be16(2)
  446. #define TPM_ST_DEACTIVATED cpu_to_be16(3)
  447. static const struct tpm_input_header tpm_startup_header = {
  448. .tag = TPM_TAG_RQU_COMMAND,
  449. .length = cpu_to_be32(12),
  450. .ordinal = TPM_ORD_STARTUP
  451. };
  452. static int tpm_startup(struct tpm_chip *chip, __be16 startup_type)
  453. {
  454. struct tpm_cmd_t start_cmd;
  455. start_cmd.header.in = tpm_startup_header;
  456. start_cmd.params.startup_in.startup_type = startup_type;
  457. return tpm_transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE,
  458. "attempting to start the TPM");
  459. }
  460. int tpm_get_timeouts(struct tpm_chip *chip)
  461. {
  462. struct tpm_cmd_t tpm_cmd;
  463. unsigned long new_timeout[4];
  464. unsigned long old_timeout[4];
  465. struct duration_t *duration_cap;
  466. ssize_t rc;
  467. tpm_cmd.header.in = tpm_getcap_header;
  468. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  469. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  470. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  471. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, NULL);
  472. if (rc == TPM_ERR_INVALID_POSTINIT) {
  473. /* The TPM is not started, we are the first to talk to it.
  474. Execute a startup command. */
  475. dev_info(chip->pdev, "Issuing TPM_STARTUP");
  476. if (tpm_startup(chip, TPM_ST_CLEAR))
  477. return rc;
  478. tpm_cmd.header.in = tpm_getcap_header;
  479. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  480. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  481. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  482. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  483. NULL);
  484. }
  485. if (rc) {
  486. dev_err(chip->pdev,
  487. "A TPM error (%zd) occurred attempting to determine the timeouts\n",
  488. rc);
  489. goto duration;
  490. }
  491. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  492. be32_to_cpu(tpm_cmd.header.out.length)
  493. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
  494. return -EINVAL;
  495. old_timeout[0] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.a);
  496. old_timeout[1] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.b);
  497. old_timeout[2] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.c);
  498. old_timeout[3] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.d);
  499. memcpy(new_timeout, old_timeout, sizeof(new_timeout));
  500. /*
  501. * Provide ability for vendor overrides of timeout values in case
  502. * of misreporting.
  503. */
  504. if (chip->ops->update_timeouts != NULL)
  505. chip->vendor.timeout_adjusted =
  506. chip->ops->update_timeouts(chip, new_timeout);
  507. if (!chip->vendor.timeout_adjusted) {
  508. /* Don't overwrite default if value is 0 */
  509. if (new_timeout[0] != 0 && new_timeout[0] < 1000) {
  510. int i;
  511. /* timeouts in msec rather usec */
  512. for (i = 0; i != ARRAY_SIZE(new_timeout); i++)
  513. new_timeout[i] *= 1000;
  514. chip->vendor.timeout_adjusted = true;
  515. }
  516. }
  517. /* Report adjusted timeouts */
  518. if (chip->vendor.timeout_adjusted) {
  519. dev_info(chip->pdev,
  520. HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
  521. old_timeout[0], new_timeout[0],
  522. old_timeout[1], new_timeout[1],
  523. old_timeout[2], new_timeout[2],
  524. old_timeout[3], new_timeout[3]);
  525. }
  526. chip->vendor.timeout_a = usecs_to_jiffies(new_timeout[0]);
  527. chip->vendor.timeout_b = usecs_to_jiffies(new_timeout[1]);
  528. chip->vendor.timeout_c = usecs_to_jiffies(new_timeout[2]);
  529. chip->vendor.timeout_d = usecs_to_jiffies(new_timeout[3]);
  530. duration:
  531. tpm_cmd.header.in = tpm_getcap_header;
  532. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  533. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  534. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
  535. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  536. "attempting to determine the durations");
  537. if (rc)
  538. return rc;
  539. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  540. be32_to_cpu(tpm_cmd.header.out.length)
  541. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
  542. return -EINVAL;
  543. duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
  544. chip->vendor.duration[TPM_SHORT] =
  545. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
  546. chip->vendor.duration[TPM_MEDIUM] =
  547. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
  548. chip->vendor.duration[TPM_LONG] =
  549. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
  550. /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
  551. * value wrong and apparently reports msecs rather than usecs. So we
  552. * fix up the resulting too-small TPM_SHORT value to make things work.
  553. * We also scale the TPM_MEDIUM and -_LONG values by 1000.
  554. */
  555. if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
  556. chip->vendor.duration[TPM_SHORT] = HZ;
  557. chip->vendor.duration[TPM_MEDIUM] *= 1000;
  558. chip->vendor.duration[TPM_LONG] *= 1000;
  559. chip->vendor.duration_adjusted = true;
  560. dev_info(chip->pdev, "Adjusting TPM timeout parameters.");
  561. }
  562. return 0;
  563. }
  564. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  565. #define TPM_ORD_CONTINUE_SELFTEST 83
  566. #define CONTINUE_SELFTEST_RESULT_SIZE 10
  567. static struct tpm_input_header continue_selftest_header = {
  568. .tag = TPM_TAG_RQU_COMMAND,
  569. .length = cpu_to_be32(10),
  570. .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
  571. };
  572. /**
  573. * tpm_continue_selftest -- run TPM's selftest
  574. * @chip: TPM chip to use
  575. *
  576. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  577. * a TPM error code.
  578. */
  579. static int tpm_continue_selftest(struct tpm_chip *chip)
  580. {
  581. int rc;
  582. struct tpm_cmd_t cmd;
  583. cmd.header.in = continue_selftest_header;
  584. rc = tpm_transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
  585. "continue selftest");
  586. return rc;
  587. }
  588. #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
  589. #define READ_PCR_RESULT_SIZE 30
  590. static struct tpm_input_header pcrread_header = {
  591. .tag = TPM_TAG_RQU_COMMAND,
  592. .length = cpu_to_be32(14),
  593. .ordinal = TPM_ORDINAL_PCRREAD
  594. };
  595. int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  596. {
  597. int rc;
  598. struct tpm_cmd_t cmd;
  599. cmd.header.in = pcrread_header;
  600. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
  601. rc = tpm_transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
  602. "attempting to read a pcr value");
  603. if (rc == 0)
  604. memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
  605. TPM_DIGEST_SIZE);
  606. return rc;
  607. }
  608. /**
  609. * tpm_pcr_read - read a pcr value
  610. * @chip_num: tpm idx # or ANY
  611. * @pcr_idx: pcr idx to retrieve
  612. * @res_buf: TPM_PCR value
  613. * size of res_buf is 20 bytes (or NULL if you don't care)
  614. *
  615. * The TPM driver should be built-in, but for whatever reason it
  616. * isn't, protect against the chip disappearing, by incrementing
  617. * the module usage count.
  618. */
  619. int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
  620. {
  621. struct tpm_chip *chip;
  622. int rc;
  623. chip = tpm_chip_find_get(chip_num);
  624. if (chip == NULL)
  625. return -ENODEV;
  626. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  627. rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
  628. else
  629. rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf);
  630. tpm_chip_put(chip);
  631. return rc;
  632. }
  633. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  634. /**
  635. * tpm_pcr_extend - extend pcr value with hash
  636. * @chip_num: tpm idx # or AN&
  637. * @pcr_idx: pcr idx to extend
  638. * @hash: hash value used to extend pcr value
  639. *
  640. * The TPM driver should be built-in, but for whatever reason it
  641. * isn't, protect against the chip disappearing, by incrementing
  642. * the module usage count.
  643. */
  644. #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
  645. #define EXTEND_PCR_RESULT_SIZE 34
  646. static struct tpm_input_header pcrextend_header = {
  647. .tag = TPM_TAG_RQU_COMMAND,
  648. .length = cpu_to_be32(34),
  649. .ordinal = TPM_ORD_PCR_EXTEND
  650. };
  651. int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
  652. {
  653. struct tpm_cmd_t cmd;
  654. int rc;
  655. struct tpm_chip *chip;
  656. chip = tpm_chip_find_get(chip_num);
  657. if (chip == NULL)
  658. return -ENODEV;
  659. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  660. rc = tpm2_pcr_extend(chip, pcr_idx, hash);
  661. tpm_chip_put(chip);
  662. return rc;
  663. }
  664. cmd.header.in = pcrextend_header;
  665. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  666. memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
  667. rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  668. "attempting extend a PCR value");
  669. tpm_chip_put(chip);
  670. return rc;
  671. }
  672. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  673. /**
  674. * tpm_do_selftest - have the TPM continue its selftest and wait until it
  675. * can receive further commands
  676. * @chip: TPM chip to use
  677. *
  678. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  679. * a TPM error code.
  680. */
  681. int tpm_do_selftest(struct tpm_chip *chip)
  682. {
  683. int rc;
  684. unsigned int loops;
  685. unsigned int delay_msec = 100;
  686. unsigned long duration;
  687. struct tpm_cmd_t cmd;
  688. duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST);
  689. loops = jiffies_to_msecs(duration) / delay_msec;
  690. rc = tpm_continue_selftest(chip);
  691. /* This may fail if there was no TPM driver during a suspend/resume
  692. * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
  693. */
  694. if (rc)
  695. return rc;
  696. do {
  697. /* Attempt to read a PCR value */
  698. cmd.header.in = pcrread_header;
  699. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(0);
  700. rc = tpm_transmit(chip, (u8 *) &cmd, READ_PCR_RESULT_SIZE);
  701. /* Some buggy TPMs will not respond to tpm_tis_ready() for
  702. * around 300ms while the self test is ongoing, keep trying
  703. * until the self test duration expires. */
  704. if (rc == -ETIME) {
  705. dev_info(chip->pdev, HW_ERR "TPM command timed out during continue self test");
  706. msleep(delay_msec);
  707. continue;
  708. }
  709. if (rc < TPM_HEADER_SIZE)
  710. return -EFAULT;
  711. rc = be32_to_cpu(cmd.header.out.return_code);
  712. if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
  713. dev_info(chip->pdev,
  714. "TPM is disabled/deactivated (0x%X)\n", rc);
  715. /* TPM is disabled and/or deactivated; driver can
  716. * proceed and TPM does handle commands for
  717. * suspend/resume correctly
  718. */
  719. return 0;
  720. }
  721. if (rc != TPM_WARN_DOING_SELFTEST)
  722. return rc;
  723. msleep(delay_msec);
  724. } while (--loops > 0);
  725. return rc;
  726. }
  727. EXPORT_SYMBOL_GPL(tpm_do_selftest);
  728. int tpm_send(u32 chip_num, void *cmd, size_t buflen)
  729. {
  730. struct tpm_chip *chip;
  731. int rc;
  732. chip = tpm_chip_find_get(chip_num);
  733. if (chip == NULL)
  734. return -ENODEV;
  735. rc = tpm_transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
  736. tpm_chip_put(chip);
  737. return rc;
  738. }
  739. EXPORT_SYMBOL_GPL(tpm_send);
  740. static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
  741. bool check_cancel, bool *canceled)
  742. {
  743. u8 status = chip->ops->status(chip);
  744. *canceled = false;
  745. if ((status & mask) == mask)
  746. return true;
  747. if (check_cancel && chip->ops->req_canceled(chip, status)) {
  748. *canceled = true;
  749. return true;
  750. }
  751. return false;
  752. }
  753. int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  754. wait_queue_head_t *queue, bool check_cancel)
  755. {
  756. unsigned long stop;
  757. long rc;
  758. u8 status;
  759. bool canceled = false;
  760. /* check current status */
  761. status = chip->ops->status(chip);
  762. if ((status & mask) == mask)
  763. return 0;
  764. stop = jiffies + timeout;
  765. if (chip->vendor.irq) {
  766. again:
  767. timeout = stop - jiffies;
  768. if ((long)timeout <= 0)
  769. return -ETIME;
  770. rc = wait_event_interruptible_timeout(*queue,
  771. wait_for_tpm_stat_cond(chip, mask, check_cancel,
  772. &canceled),
  773. timeout);
  774. if (rc > 0) {
  775. if (canceled)
  776. return -ECANCELED;
  777. return 0;
  778. }
  779. if (rc == -ERESTARTSYS && freezing(current)) {
  780. clear_thread_flag(TIF_SIGPENDING);
  781. goto again;
  782. }
  783. } else {
  784. do {
  785. msleep(TPM_TIMEOUT);
  786. status = chip->ops->status(chip);
  787. if ((status & mask) == mask)
  788. return 0;
  789. } while (time_before(jiffies, stop));
  790. }
  791. return -ETIME;
  792. }
  793. EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
  794. #define TPM_ORD_SAVESTATE cpu_to_be32(152)
  795. #define SAVESTATE_RESULT_SIZE 10
  796. static struct tpm_input_header savestate_header = {
  797. .tag = TPM_TAG_RQU_COMMAND,
  798. .length = cpu_to_be32(10),
  799. .ordinal = TPM_ORD_SAVESTATE
  800. };
  801. /*
  802. * We are about to suspend. Save the TPM state
  803. * so that it can be restored.
  804. */
  805. int tpm_pm_suspend(struct device *dev)
  806. {
  807. struct tpm_chip *chip = dev_get_drvdata(dev);
  808. struct tpm_cmd_t cmd;
  809. int rc, try;
  810. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  811. if (chip == NULL)
  812. return -ENODEV;
  813. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  814. tpm2_shutdown(chip, TPM2_SU_STATE);
  815. return 0;
  816. }
  817. /* for buggy tpm, flush pcrs with extend to selected dummy */
  818. if (tpm_suspend_pcr) {
  819. cmd.header.in = pcrextend_header;
  820. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
  821. memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
  822. TPM_DIGEST_SIZE);
  823. rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  824. "extending dummy pcr before suspend");
  825. }
  826. /* now do the actual savestate */
  827. for (try = 0; try < TPM_RETRY; try++) {
  828. cmd.header.in = savestate_header;
  829. rc = tpm_transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, NULL);
  830. /*
  831. * If the TPM indicates that it is too busy to respond to
  832. * this command then retry before giving up. It can take
  833. * several seconds for this TPM to be ready.
  834. *
  835. * This can happen if the TPM has already been sent the
  836. * SaveState command before the driver has loaded. TCG 1.2
  837. * specification states that any communication after SaveState
  838. * may cause the TPM to invalidate previously saved state.
  839. */
  840. if (rc != TPM_WARN_RETRY)
  841. break;
  842. msleep(TPM_TIMEOUT_RETRY);
  843. }
  844. if (rc)
  845. dev_err(chip->pdev,
  846. "Error (%d) sending savestate before suspend\n", rc);
  847. else if (try > 0)
  848. dev_warn(chip->pdev, "TPM savestate took %dms\n",
  849. try * TPM_TIMEOUT_RETRY);
  850. return rc;
  851. }
  852. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  853. /*
  854. * Resume from a power safe. The BIOS already restored
  855. * the TPM state.
  856. */
  857. int tpm_pm_resume(struct device *dev)
  858. {
  859. struct tpm_chip *chip = dev_get_drvdata(dev);
  860. if (chip == NULL)
  861. return -ENODEV;
  862. return 0;
  863. }
  864. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  865. #define TPM_GETRANDOM_RESULT_SIZE 18
  866. static struct tpm_input_header tpm_getrandom_header = {
  867. .tag = TPM_TAG_RQU_COMMAND,
  868. .length = cpu_to_be32(14),
  869. .ordinal = TPM_ORD_GET_RANDOM
  870. };
  871. /**
  872. * tpm_get_random() - Get random bytes from the tpm's RNG
  873. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  874. * @out: destination buffer for the random bytes
  875. * @max: the max number of bytes to write to @out
  876. *
  877. * Returns < 0 on error and the number of bytes read on success
  878. */
  879. int tpm_get_random(u32 chip_num, u8 *out, size_t max)
  880. {
  881. struct tpm_chip *chip;
  882. struct tpm_cmd_t tpm_cmd;
  883. u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
  884. int err, total = 0, retries = 5;
  885. u8 *dest = out;
  886. if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
  887. return -EINVAL;
  888. chip = tpm_chip_find_get(chip_num);
  889. if (chip == NULL)
  890. return -ENODEV;
  891. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  892. err = tpm2_get_random(chip, out, max);
  893. tpm_chip_put(chip);
  894. return err;
  895. }
  896. do {
  897. tpm_cmd.header.in = tpm_getrandom_header;
  898. tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
  899. err = tpm_transmit_cmd(chip, &tpm_cmd,
  900. TPM_GETRANDOM_RESULT_SIZE + num_bytes,
  901. "attempting get random");
  902. if (err)
  903. break;
  904. recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
  905. memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
  906. dest += recd;
  907. total += recd;
  908. num_bytes -= recd;
  909. } while (retries-- && total < max);
  910. tpm_chip_put(chip);
  911. return total ? total : -EIO;
  912. }
  913. EXPORT_SYMBOL_GPL(tpm_get_random);
  914. static int __init tpm_init(void)
  915. {
  916. int rc;
  917. tpm_class = class_create(THIS_MODULE, "tpm");
  918. if (IS_ERR(tpm_class)) {
  919. pr_err("couldn't create tpm class\n");
  920. return PTR_ERR(tpm_class);
  921. }
  922. rc = alloc_chrdev_region(&tpm_devt, 0, TPM_NUM_DEVICES, "tpm");
  923. if (rc < 0) {
  924. pr_err("tpm: failed to allocate char dev region\n");
  925. class_destroy(tpm_class);
  926. return rc;
  927. }
  928. return 0;
  929. }
  930. static void __exit tpm_exit(void)
  931. {
  932. class_destroy(tpm_class);
  933. unregister_chrdev_region(tpm_devt, TPM_NUM_DEVICES);
  934. }
  935. subsys_initcall(tpm_init);
  936. module_exit(tpm_exit);
  937. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  938. MODULE_DESCRIPTION("TPM Driver");
  939. MODULE_VERSION("2.0");
  940. MODULE_LICENSE("GPL");