tpm1-cmd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. // SPDX-License-Identifier: GPL-2.0
  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. * Device driver for TCG/TCPA TPM (trusted platform module).
  13. * Specifications at www.trustedcomputinggroup.org
  14. */
  15. #include <linux/poll.h>
  16. #include <linux/slab.h>
  17. #include <linux/mutex.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/freezer.h>
  20. #include <linux/tpm_eventlog.h>
  21. #include "tpm.h"
  22. #define TPM_MAX_ORDINAL 243
  23. /*
  24. * Array with one entry per ordinal defining the maximum amount
  25. * of time the chip could take to return the result. The ordinal
  26. * designation of short, medium or long is defined in a table in
  27. * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
  28. * values of the SHORT, MEDIUM, and LONG durations are retrieved
  29. * from the chip during initialization with a call to tpm_get_timeouts.
  30. */
  31. static const u8 tpm1_ordinal_duration[TPM_MAX_ORDINAL] = {
  32. TPM_UNDEFINED, /* 0 */
  33. TPM_UNDEFINED,
  34. TPM_UNDEFINED,
  35. TPM_UNDEFINED,
  36. TPM_UNDEFINED,
  37. TPM_UNDEFINED, /* 5 */
  38. TPM_UNDEFINED,
  39. TPM_UNDEFINED,
  40. TPM_UNDEFINED,
  41. TPM_UNDEFINED,
  42. TPM_SHORT, /* 10 */
  43. TPM_SHORT,
  44. TPM_MEDIUM,
  45. TPM_LONG,
  46. TPM_LONG,
  47. TPM_MEDIUM, /* 15 */
  48. TPM_SHORT,
  49. TPM_SHORT,
  50. TPM_MEDIUM,
  51. TPM_LONG,
  52. TPM_SHORT, /* 20 */
  53. TPM_SHORT,
  54. TPM_MEDIUM,
  55. TPM_MEDIUM,
  56. TPM_MEDIUM,
  57. TPM_SHORT, /* 25 */
  58. TPM_SHORT,
  59. TPM_MEDIUM,
  60. TPM_SHORT,
  61. TPM_SHORT,
  62. TPM_MEDIUM, /* 30 */
  63. TPM_LONG,
  64. TPM_MEDIUM,
  65. TPM_SHORT,
  66. TPM_SHORT,
  67. TPM_SHORT, /* 35 */
  68. TPM_MEDIUM,
  69. TPM_MEDIUM,
  70. TPM_UNDEFINED,
  71. TPM_UNDEFINED,
  72. TPM_MEDIUM, /* 40 */
  73. TPM_LONG,
  74. TPM_MEDIUM,
  75. TPM_SHORT,
  76. TPM_SHORT,
  77. TPM_SHORT, /* 45 */
  78. TPM_SHORT,
  79. TPM_SHORT,
  80. TPM_SHORT,
  81. TPM_LONG,
  82. TPM_MEDIUM, /* 50 */
  83. TPM_MEDIUM,
  84. TPM_UNDEFINED,
  85. TPM_UNDEFINED,
  86. TPM_UNDEFINED,
  87. TPM_UNDEFINED, /* 55 */
  88. TPM_UNDEFINED,
  89. TPM_UNDEFINED,
  90. TPM_UNDEFINED,
  91. TPM_UNDEFINED,
  92. TPM_MEDIUM, /* 60 */
  93. TPM_MEDIUM,
  94. TPM_MEDIUM,
  95. TPM_SHORT,
  96. TPM_SHORT,
  97. TPM_MEDIUM, /* 65 */
  98. TPM_UNDEFINED,
  99. TPM_UNDEFINED,
  100. TPM_UNDEFINED,
  101. TPM_UNDEFINED,
  102. TPM_SHORT, /* 70 */
  103. TPM_SHORT,
  104. TPM_UNDEFINED,
  105. TPM_UNDEFINED,
  106. TPM_UNDEFINED,
  107. TPM_UNDEFINED, /* 75 */
  108. TPM_UNDEFINED,
  109. TPM_UNDEFINED,
  110. TPM_UNDEFINED,
  111. TPM_UNDEFINED,
  112. TPM_LONG, /* 80 */
  113. TPM_UNDEFINED,
  114. TPM_MEDIUM,
  115. TPM_LONG,
  116. TPM_SHORT,
  117. TPM_UNDEFINED, /* 85 */
  118. TPM_UNDEFINED,
  119. TPM_UNDEFINED,
  120. TPM_UNDEFINED,
  121. TPM_UNDEFINED,
  122. TPM_SHORT, /* 90 */
  123. TPM_SHORT,
  124. TPM_SHORT,
  125. TPM_SHORT,
  126. TPM_SHORT,
  127. TPM_UNDEFINED, /* 95 */
  128. TPM_UNDEFINED,
  129. TPM_UNDEFINED,
  130. TPM_UNDEFINED,
  131. TPM_UNDEFINED,
  132. TPM_MEDIUM, /* 100 */
  133. TPM_SHORT,
  134. TPM_SHORT,
  135. TPM_UNDEFINED,
  136. TPM_UNDEFINED,
  137. TPM_UNDEFINED, /* 105 */
  138. TPM_UNDEFINED,
  139. TPM_UNDEFINED,
  140. TPM_UNDEFINED,
  141. TPM_UNDEFINED,
  142. TPM_SHORT, /* 110 */
  143. TPM_SHORT,
  144. TPM_SHORT,
  145. TPM_SHORT,
  146. TPM_SHORT,
  147. TPM_SHORT, /* 115 */
  148. TPM_SHORT,
  149. TPM_SHORT,
  150. TPM_UNDEFINED,
  151. TPM_UNDEFINED,
  152. TPM_LONG, /* 120 */
  153. TPM_LONG,
  154. TPM_MEDIUM,
  155. TPM_UNDEFINED,
  156. TPM_SHORT,
  157. TPM_SHORT, /* 125 */
  158. TPM_SHORT,
  159. TPM_LONG,
  160. TPM_SHORT,
  161. TPM_SHORT,
  162. TPM_SHORT, /* 130 */
  163. TPM_MEDIUM,
  164. TPM_UNDEFINED,
  165. TPM_SHORT,
  166. TPM_MEDIUM,
  167. TPM_UNDEFINED, /* 135 */
  168. TPM_UNDEFINED,
  169. TPM_UNDEFINED,
  170. TPM_UNDEFINED,
  171. TPM_UNDEFINED,
  172. TPM_SHORT, /* 140 */
  173. TPM_SHORT,
  174. TPM_UNDEFINED,
  175. TPM_UNDEFINED,
  176. TPM_UNDEFINED,
  177. TPM_UNDEFINED, /* 145 */
  178. TPM_UNDEFINED,
  179. TPM_UNDEFINED,
  180. TPM_UNDEFINED,
  181. TPM_UNDEFINED,
  182. TPM_SHORT, /* 150 */
  183. TPM_MEDIUM,
  184. TPM_MEDIUM,
  185. TPM_SHORT,
  186. TPM_SHORT,
  187. TPM_UNDEFINED, /* 155 */
  188. TPM_UNDEFINED,
  189. TPM_UNDEFINED,
  190. TPM_UNDEFINED,
  191. TPM_UNDEFINED,
  192. TPM_SHORT, /* 160 */
  193. TPM_SHORT,
  194. TPM_SHORT,
  195. TPM_SHORT,
  196. TPM_UNDEFINED,
  197. TPM_UNDEFINED, /* 165 */
  198. TPM_UNDEFINED,
  199. TPM_UNDEFINED,
  200. TPM_UNDEFINED,
  201. TPM_UNDEFINED,
  202. TPM_LONG, /* 170 */
  203. TPM_UNDEFINED,
  204. TPM_UNDEFINED,
  205. TPM_UNDEFINED,
  206. TPM_UNDEFINED,
  207. TPM_UNDEFINED, /* 175 */
  208. TPM_UNDEFINED,
  209. TPM_UNDEFINED,
  210. TPM_UNDEFINED,
  211. TPM_UNDEFINED,
  212. TPM_MEDIUM, /* 180 */
  213. TPM_SHORT,
  214. TPM_MEDIUM,
  215. TPM_MEDIUM,
  216. TPM_MEDIUM,
  217. TPM_MEDIUM, /* 185 */
  218. TPM_SHORT,
  219. TPM_UNDEFINED,
  220. TPM_UNDEFINED,
  221. TPM_UNDEFINED,
  222. TPM_UNDEFINED, /* 190 */
  223. TPM_UNDEFINED,
  224. TPM_UNDEFINED,
  225. TPM_UNDEFINED,
  226. TPM_UNDEFINED,
  227. TPM_UNDEFINED, /* 195 */
  228. TPM_UNDEFINED,
  229. TPM_UNDEFINED,
  230. TPM_UNDEFINED,
  231. TPM_UNDEFINED,
  232. TPM_SHORT, /* 200 */
  233. TPM_UNDEFINED,
  234. TPM_UNDEFINED,
  235. TPM_UNDEFINED,
  236. TPM_SHORT,
  237. TPM_SHORT, /* 205 */
  238. TPM_SHORT,
  239. TPM_SHORT,
  240. TPM_SHORT,
  241. TPM_SHORT,
  242. TPM_MEDIUM, /* 210 */
  243. TPM_UNDEFINED,
  244. TPM_MEDIUM,
  245. TPM_MEDIUM,
  246. TPM_MEDIUM,
  247. TPM_UNDEFINED, /* 215 */
  248. TPM_MEDIUM,
  249. TPM_UNDEFINED,
  250. TPM_UNDEFINED,
  251. TPM_SHORT,
  252. TPM_SHORT, /* 220 */
  253. TPM_SHORT,
  254. TPM_SHORT,
  255. TPM_SHORT,
  256. TPM_SHORT,
  257. TPM_UNDEFINED, /* 225 */
  258. TPM_UNDEFINED,
  259. TPM_UNDEFINED,
  260. TPM_UNDEFINED,
  261. TPM_UNDEFINED,
  262. TPM_SHORT, /* 230 */
  263. TPM_LONG,
  264. TPM_MEDIUM,
  265. TPM_UNDEFINED,
  266. TPM_UNDEFINED,
  267. TPM_UNDEFINED, /* 235 */
  268. TPM_UNDEFINED,
  269. TPM_UNDEFINED,
  270. TPM_UNDEFINED,
  271. TPM_UNDEFINED,
  272. TPM_SHORT, /* 240 */
  273. TPM_UNDEFINED,
  274. TPM_MEDIUM,
  275. };
  276. /**
  277. * tpm1_calc_ordinal_duration() - calculate the maximum command duration
  278. * @chip: TPM chip to use.
  279. * @ordinal: TPM command ordinal.
  280. *
  281. * The function returns the maximum amount of time the chip could take
  282. * to return the result for a particular ordinal in jiffies.
  283. *
  284. * Return: A maximal duration time for an ordinal in jiffies.
  285. */
  286. unsigned long tpm1_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
  287. {
  288. int duration_idx = TPM_UNDEFINED;
  289. int duration = 0;
  290. /*
  291. * We only have a duration table for protected commands, where the upper
  292. * 16 bits are 0. For the few other ordinals the fallback will be used.
  293. */
  294. if (ordinal < TPM_MAX_ORDINAL)
  295. duration_idx = tpm1_ordinal_duration[ordinal];
  296. if (duration_idx != TPM_UNDEFINED)
  297. duration = chip->duration[duration_idx];
  298. if (duration <= 0)
  299. return 2 * 60 * HZ;
  300. else
  301. return duration;
  302. }
  303. #define TPM_ORD_STARTUP 153
  304. #define TPM_ST_CLEAR 1
  305. /**
  306. * tpm1_startup() - turn on the TPM
  307. * @chip: TPM chip to use
  308. *
  309. * Normally the firmware should start the TPM. This function is provided as a
  310. * workaround if this does not happen. A legal case for this could be for
  311. * example when a TPM emulator is used.
  312. *
  313. * Return: same as tpm_transmit_cmd()
  314. */
  315. static int tpm1_startup(struct tpm_chip *chip)
  316. {
  317. struct tpm_buf buf;
  318. int rc;
  319. dev_info(&chip->dev, "starting up the TPM manually\n");
  320. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_STARTUP);
  321. if (rc < 0)
  322. return rc;
  323. tpm_buf_append_u16(&buf, TPM_ST_CLEAR);
  324. rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to start the TPM");
  325. tpm_buf_destroy(&buf);
  326. return rc;
  327. }
  328. int tpm1_get_timeouts(struct tpm_chip *chip)
  329. {
  330. cap_t cap;
  331. unsigned long timeout_old[4], timeout_chip[4], timeout_eff[4];
  332. unsigned long durations[3];
  333. ssize_t rc;
  334. rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, NULL,
  335. sizeof(cap.timeout));
  336. if (rc == TPM_ERR_INVALID_POSTINIT) {
  337. if (tpm1_startup(chip))
  338. return rc;
  339. rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap,
  340. "attempting to determine the timeouts",
  341. sizeof(cap.timeout));
  342. }
  343. if (rc) {
  344. dev_err(&chip->dev, "A TPM error (%zd) occurred attempting to determine the timeouts\n",
  345. rc);
  346. return rc;
  347. }
  348. timeout_old[0] = jiffies_to_usecs(chip->timeout_a);
  349. timeout_old[1] = jiffies_to_usecs(chip->timeout_b);
  350. timeout_old[2] = jiffies_to_usecs(chip->timeout_c);
  351. timeout_old[3] = jiffies_to_usecs(chip->timeout_d);
  352. timeout_chip[0] = be32_to_cpu(cap.timeout.a);
  353. timeout_chip[1] = be32_to_cpu(cap.timeout.b);
  354. timeout_chip[2] = be32_to_cpu(cap.timeout.c);
  355. timeout_chip[3] = be32_to_cpu(cap.timeout.d);
  356. memcpy(timeout_eff, timeout_chip, sizeof(timeout_eff));
  357. /*
  358. * Provide ability for vendor overrides of timeout values in case
  359. * of misreporting.
  360. */
  361. if (chip->ops->update_timeouts)
  362. chip->ops->update_timeouts(chip, timeout_eff);
  363. if (!chip->timeout_adjusted) {
  364. /* Restore default if chip reported 0 */
  365. unsigned int i;
  366. for (i = 0; i < ARRAY_SIZE(timeout_eff); i++) {
  367. if (timeout_eff[i])
  368. continue;
  369. timeout_eff[i] = timeout_old[i];
  370. chip->timeout_adjusted = true;
  371. }
  372. if (timeout_eff[0] != 0 && timeout_eff[0] < 1000) {
  373. /* timeouts in msec rather usec */
  374. for (i = 0; i != ARRAY_SIZE(timeout_eff); i++)
  375. timeout_eff[i] *= 1000;
  376. chip->timeout_adjusted = true;
  377. }
  378. }
  379. /* Report adjusted timeouts */
  380. if (chip->timeout_adjusted) {
  381. dev_info(&chip->dev, HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
  382. timeout_chip[0], timeout_eff[0],
  383. timeout_chip[1], timeout_eff[1],
  384. timeout_chip[2], timeout_eff[2],
  385. timeout_chip[3], timeout_eff[3]);
  386. }
  387. chip->timeout_a = usecs_to_jiffies(timeout_eff[0]);
  388. chip->timeout_b = usecs_to_jiffies(timeout_eff[1]);
  389. chip->timeout_c = usecs_to_jiffies(timeout_eff[2]);
  390. chip->timeout_d = usecs_to_jiffies(timeout_eff[3]);
  391. rc = tpm1_getcap(chip, TPM_CAP_PROP_TIS_DURATION, &cap,
  392. "attempting to determine the durations",
  393. sizeof(cap.duration));
  394. if (rc)
  395. return rc;
  396. chip->duration[TPM_SHORT] =
  397. usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_short));
  398. chip->duration[TPM_MEDIUM] =
  399. usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_medium));
  400. chip->duration[TPM_LONG] =
  401. usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_long));
  402. chip->duration[TPM_LONG_LONG] = 0; /* not used under 1.2 */
  403. /*
  404. * Provide the ability for vendor overrides of duration values in case
  405. * of misreporting.
  406. */
  407. if (chip->ops->update_durations)
  408. chip->ops->update_durations(chip, durations);
  409. if (chip->duration_adjusted) {
  410. dev_info(&chip->dev, HW_ERR "Adjusting reported durations.");
  411. chip->duration[TPM_SHORT] = durations[0];
  412. chip->duration[TPM_MEDIUM] = durations[1];
  413. chip->duration[TPM_LONG] = durations[2];
  414. }
  415. /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
  416. * value wrong and apparently reports msecs rather than usecs. So we
  417. * fix up the resulting too-small TPM_SHORT value to make things work.
  418. * We also scale the TPM_MEDIUM and -_LONG values by 1000.
  419. */
  420. if (chip->duration[TPM_SHORT] < (HZ / 100)) {
  421. chip->duration[TPM_SHORT] = HZ;
  422. chip->duration[TPM_MEDIUM] *= 1000;
  423. chip->duration[TPM_LONG] *= 1000;
  424. chip->duration_adjusted = true;
  425. dev_info(&chip->dev, "Adjusting TPM timeout parameters.");
  426. }
  427. chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
  428. return 0;
  429. }
  430. #define TPM_ORD_PCR_EXTEND 20
  431. int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,
  432. const char *log_msg)
  433. {
  434. struct tpm_buf buf;
  435. int rc;
  436. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_EXTEND);
  437. if (rc)
  438. return rc;
  439. tpm_buf_append_u32(&buf, pcr_idx);
  440. tpm_buf_append(&buf, hash, TPM_DIGEST_SIZE);
  441. rc = tpm_transmit_cmd(chip, &buf, TPM_DIGEST_SIZE, log_msg);
  442. tpm_buf_destroy(&buf);
  443. return rc;
  444. }
  445. #define TPM_ORD_GET_CAP 101
  446. ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
  447. const char *desc, size_t min_cap_length)
  448. {
  449. struct tpm_buf buf;
  450. int rc;
  451. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP);
  452. if (rc)
  453. return rc;
  454. if (subcap_id == TPM_CAP_VERSION_1_1 ||
  455. subcap_id == TPM_CAP_VERSION_1_2) {
  456. tpm_buf_append_u32(&buf, subcap_id);
  457. tpm_buf_append_u32(&buf, 0);
  458. } else {
  459. if (subcap_id == TPM_CAP_FLAG_PERM ||
  460. subcap_id == TPM_CAP_FLAG_VOL)
  461. tpm_buf_append_u32(&buf, TPM_CAP_FLAG);
  462. else
  463. tpm_buf_append_u32(&buf, TPM_CAP_PROP);
  464. tpm_buf_append_u32(&buf, 4);
  465. tpm_buf_append_u32(&buf, subcap_id);
  466. }
  467. rc = tpm_transmit_cmd(chip, &buf, min_cap_length, desc);
  468. if (!rc)
  469. *cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
  470. tpm_buf_destroy(&buf);
  471. return rc;
  472. }
  473. EXPORT_SYMBOL_GPL(tpm1_getcap);
  474. #define TPM_ORD_GET_RANDOM 70
  475. struct tpm1_get_random_out {
  476. __be32 rng_data_len;
  477. u8 rng_data[TPM_MAX_RNG_DATA];
  478. } __packed;
  479. /**
  480. * tpm1_get_random() - get random bytes from the TPM's RNG
  481. * @chip: a &struct tpm_chip instance
  482. * @dest: destination buffer for the random bytes
  483. * @max: the maximum number of bytes to write to @dest
  484. *
  485. * Return:
  486. * * number of bytes read
  487. * * -errno (positive TPM return codes are masked to -EIO)
  488. */
  489. int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
  490. {
  491. struct tpm1_get_random_out *out;
  492. u32 num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
  493. struct tpm_buf buf;
  494. u32 total = 0;
  495. int retries = 5;
  496. u32 recd;
  497. int rc;
  498. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_RANDOM);
  499. if (rc)
  500. return rc;
  501. do {
  502. tpm_buf_append_u32(&buf, num_bytes);
  503. rc = tpm_transmit_cmd(chip, &buf, sizeof(out->rng_data_len),
  504. "attempting get random");
  505. if (rc) {
  506. if (rc > 0)
  507. rc = -EIO;
  508. goto out;
  509. }
  510. out = (struct tpm1_get_random_out *)&buf.data[TPM_HEADER_SIZE];
  511. recd = be32_to_cpu(out->rng_data_len);
  512. if (recd > num_bytes) {
  513. rc = -EFAULT;
  514. goto out;
  515. }
  516. if (tpm_buf_length(&buf) < TPM_HEADER_SIZE +
  517. sizeof(out->rng_data_len) + recd) {
  518. rc = -EFAULT;
  519. goto out;
  520. }
  521. memcpy(dest, out->rng_data, recd);
  522. dest += recd;
  523. total += recd;
  524. num_bytes -= recd;
  525. tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_RANDOM);
  526. } while (retries-- && total < max);
  527. rc = total ? (int)total : -EIO;
  528. out:
  529. tpm_buf_destroy(&buf);
  530. return rc;
  531. }
  532. #define TPM_ORD_PCRREAD 21
  533. int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
  534. {
  535. struct tpm_buf buf;
  536. int rc;
  537. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCRREAD);
  538. if (rc)
  539. return rc;
  540. tpm_buf_append_u32(&buf, pcr_idx);
  541. rc = tpm_transmit_cmd(chip, &buf, TPM_DIGEST_SIZE,
  542. "attempting to read a pcr value");
  543. if (rc)
  544. goto out;
  545. if (tpm_buf_length(&buf) < TPM_DIGEST_SIZE) {
  546. rc = -EFAULT;
  547. goto out;
  548. }
  549. memcpy(res_buf, &buf.data[TPM_HEADER_SIZE], TPM_DIGEST_SIZE);
  550. out:
  551. tpm_buf_destroy(&buf);
  552. return rc;
  553. }
  554. #define TPM_ORD_CONTINUE_SELFTEST 83
  555. /**
  556. * tpm1_continue_selftest() - run TPM's selftest
  557. * @chip: TPM chip to use
  558. *
  559. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  560. * a TPM error code.
  561. */
  562. static int tpm1_continue_selftest(struct tpm_chip *chip)
  563. {
  564. struct tpm_buf buf;
  565. int rc;
  566. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_CONTINUE_SELFTEST);
  567. if (rc)
  568. return rc;
  569. rc = tpm_transmit_cmd(chip, &buf, 0, "continue selftest");
  570. tpm_buf_destroy(&buf);
  571. return rc;
  572. }
  573. /**
  574. * tpm1_do_selftest - have the TPM continue its selftest and wait until it
  575. * can receive further commands
  576. * @chip: TPM chip to use
  577. *
  578. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  579. * a TPM error code.
  580. */
  581. int tpm1_do_selftest(struct tpm_chip *chip)
  582. {
  583. int rc;
  584. unsigned int loops;
  585. unsigned int delay_msec = 100;
  586. unsigned long duration;
  587. u8 dummy[TPM_DIGEST_SIZE];
  588. duration = tpm1_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST);
  589. loops = jiffies_to_msecs(duration) / delay_msec;
  590. rc = tpm1_continue_selftest(chip);
  591. if (rc == TPM_ERR_INVALID_POSTINIT) {
  592. chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
  593. dev_info(&chip->dev, "TPM not ready (%d)\n", rc);
  594. }
  595. /* This may fail if there was no TPM driver during a suspend/resume
  596. * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
  597. */
  598. if (rc)
  599. return rc;
  600. do {
  601. /* Attempt to read a PCR value */
  602. rc = tpm1_pcr_read(chip, 0, dummy);
  603. /* Some buggy TPMs will not respond to tpm_tis_ready() for
  604. * around 300ms while the self test is ongoing, keep trying
  605. * until the self test duration expires.
  606. */
  607. if (rc == -ETIME) {
  608. dev_info(&chip->dev, HW_ERR "TPM command timed out during continue self test");
  609. tpm_msleep(delay_msec);
  610. continue;
  611. }
  612. if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
  613. dev_info(&chip->dev, "TPM is disabled/deactivated (0x%X)\n",
  614. rc);
  615. /* TPM is disabled and/or deactivated; driver can
  616. * proceed and TPM does handle commands for
  617. * suspend/resume correctly
  618. */
  619. return 0;
  620. }
  621. if (rc != TPM_WARN_DOING_SELFTEST)
  622. return rc;
  623. tpm_msleep(delay_msec);
  624. } while (--loops > 0);
  625. return rc;
  626. }
  627. EXPORT_SYMBOL_GPL(tpm1_do_selftest);
  628. /**
  629. * tpm1_auto_startup - Perform the standard automatic TPM initialization
  630. * sequence
  631. * @chip: TPM chip to use
  632. *
  633. * Returns 0 on success, < 0 in case of fatal error.
  634. */
  635. int tpm1_auto_startup(struct tpm_chip *chip)
  636. {
  637. int rc;
  638. rc = tpm1_get_timeouts(chip);
  639. if (rc)
  640. goto out;
  641. rc = tpm1_do_selftest(chip);
  642. if (rc == TPM_ERR_FAILEDSELFTEST) {
  643. dev_warn(&chip->dev, "TPM self test failed, switching to the firmware upgrade mode\n");
  644. /* A TPM in this state possibly allows or needs a firmware upgrade */
  645. chip->flags |= TPM_CHIP_FLAG_FIRMWARE_UPGRADE;
  646. return 0;
  647. } else if (rc) {
  648. dev_err(&chip->dev, "TPM self test failed\n");
  649. goto out;
  650. }
  651. return rc;
  652. out:
  653. if (rc > 0)
  654. rc = -ENODEV;
  655. return rc;
  656. }
  657. #define TPM_ORD_SAVESTATE 152
  658. /**
  659. * tpm1_pm_suspend() - pm suspend handler
  660. * @chip: TPM chip to use.
  661. * @tpm_suspend_pcr: flush pcr for buggy TPM chips.
  662. *
  663. * The functions saves the TPM state to be restored on resume.
  664. *
  665. * Return:
  666. * * 0 on success,
  667. * * < 0 on error.
  668. */
  669. int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)
  670. {
  671. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  672. struct tpm_buf buf;
  673. unsigned int try;
  674. int rc;
  675. /* for buggy tpm, flush pcrs with extend to selected dummy */
  676. if (tpm_suspend_pcr)
  677. rc = tpm1_pcr_extend(chip, tpm_suspend_pcr, dummy_hash,
  678. "extending dummy pcr before suspend");
  679. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_SAVESTATE);
  680. if (rc)
  681. return rc;
  682. /* now do the actual savestate */
  683. for (try = 0; try < TPM_RETRY; try++) {
  684. rc = tpm_transmit_cmd(chip, &buf, 0, NULL);
  685. /*
  686. * If the TPM indicates that it is too busy to respond to
  687. * this command then retry before giving up. It can take
  688. * several seconds for this TPM to be ready.
  689. *
  690. * This can happen if the TPM has already been sent the
  691. * SaveState command before the driver has loaded. TCG 1.2
  692. * specification states that any communication after SaveState
  693. * may cause the TPM to invalidate previously saved state.
  694. */
  695. if (rc != TPM_WARN_RETRY)
  696. break;
  697. tpm_msleep(TPM_TIMEOUT_RETRY);
  698. tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_SAVESTATE);
  699. }
  700. if (rc)
  701. dev_err(&chip->dev, "Error (%d) sending savestate before suspend\n",
  702. rc);
  703. else if (try > 0)
  704. dev_warn(&chip->dev, "TPM savestate took %dms\n",
  705. try * TPM_TIMEOUT_RETRY);
  706. tpm_buf_destroy(&buf);
  707. return rc;
  708. }
  709. /**
  710. * tpm1_get_pcr_allocation() - initialize the allocated bank
  711. * @chip: TPM chip to use.
  712. *
  713. * The function initializes the SHA1 allocated bank to extend PCR
  714. *
  715. * Return:
  716. * * 0 on success,
  717. * * < 0 on error.
  718. */
  719. int tpm1_get_pcr_allocation(struct tpm_chip *chip)
  720. {
  721. chip->allocated_banks = kcalloc(1, sizeof(*chip->allocated_banks),
  722. GFP_KERNEL);
  723. if (!chip->allocated_banks)
  724. return -ENOMEM;
  725. chip->allocated_banks[0].alg_id = TPM_ALG_SHA1;
  726. chip->allocated_banks[0].digest_size = hash_digest_size[HASH_ALGO_SHA1];
  727. chip->allocated_banks[0].crypto_id = HASH_ALGO_SHA1;
  728. chip->nr_allocated_banks = 1;
  729. return 0;
  730. }