tpm-interface.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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. /*
  308. * We only have a duration table for protected commands, where the upper
  309. * 16 bits are 0. For the few other ordinals the fallback will be used.
  310. */
  311. if (ordinal < TPM_MAX_ORDINAL)
  312. duration_idx = tpm_ordinal_duration[ordinal];
  313. if (duration_idx != TPM_UNDEFINED)
  314. duration = chip->vendor.duration[duration_idx];
  315. if (duration <= 0)
  316. return 2 * 60 * HZ;
  317. else
  318. return duration;
  319. }
  320. EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
  321. /*
  322. * Internal kernel interface to transmit TPM commands
  323. */
  324. ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
  325. size_t bufsiz)
  326. {
  327. ssize_t rc;
  328. u32 count, ordinal;
  329. unsigned long stop;
  330. if (bufsiz > TPM_BUFSIZE)
  331. bufsiz = TPM_BUFSIZE;
  332. count = be32_to_cpu(*((__be32 *) (buf + 2)));
  333. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  334. if (count == 0)
  335. return -ENODATA;
  336. if (count > bufsiz) {
  337. dev_err(chip->pdev,
  338. "invalid count value %x %zx\n", count, bufsiz);
  339. return -E2BIG;
  340. }
  341. mutex_lock(&chip->tpm_mutex);
  342. rc = chip->ops->send(chip, (u8 *) buf, count);
  343. if (rc < 0) {
  344. dev_err(chip->pdev,
  345. "tpm_transmit: tpm_send: error %zd\n", rc);
  346. goto out;
  347. }
  348. if (chip->vendor.irq)
  349. goto out_recv;
  350. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  351. stop = jiffies + tpm2_calc_ordinal_duration(chip, ordinal);
  352. else
  353. stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
  354. do {
  355. u8 status = chip->ops->status(chip);
  356. if ((status & chip->ops->req_complete_mask) ==
  357. chip->ops->req_complete_val)
  358. goto out_recv;
  359. if (chip->ops->req_canceled(chip, status)) {
  360. dev_err(chip->pdev, "Operation Canceled\n");
  361. rc = -ECANCELED;
  362. goto out;
  363. }
  364. msleep(TPM_TIMEOUT); /* CHECK */
  365. rmb();
  366. } while (time_before(jiffies, stop));
  367. chip->ops->cancel(chip);
  368. dev_err(chip->pdev, "Operation Timed out\n");
  369. rc = -ETIME;
  370. goto out;
  371. out_recv:
  372. rc = chip->ops->recv(chip, (u8 *) buf, bufsiz);
  373. if (rc < 0)
  374. dev_err(chip->pdev,
  375. "tpm_transmit: tpm_recv: error %zd\n", rc);
  376. out:
  377. mutex_unlock(&chip->tpm_mutex);
  378. return rc;
  379. }
  380. #define TPM_DIGEST_SIZE 20
  381. #define TPM_RET_CODE_IDX 6
  382. ssize_t tpm_transmit_cmd(struct tpm_chip *chip, void *cmd,
  383. int len, const char *desc)
  384. {
  385. struct tpm_output_header *header;
  386. int err;
  387. len = tpm_transmit(chip, (u8 *) cmd, len);
  388. if (len < 0)
  389. return len;
  390. else if (len < TPM_HEADER_SIZE)
  391. return -EFAULT;
  392. header = cmd;
  393. err = be32_to_cpu(header->return_code);
  394. if (err != 0 && desc)
  395. dev_err(chip->pdev, "A TPM error (%d) occurred %s\n", err,
  396. desc);
  397. return err;
  398. }
  399. #define TPM_INTERNAL_RESULT_SIZE 200
  400. #define TPM_ORD_GET_CAP cpu_to_be32(101)
  401. #define TPM_ORD_GET_RANDOM cpu_to_be32(70)
  402. static const struct tpm_input_header tpm_getcap_header = {
  403. .tag = TPM_TAG_RQU_COMMAND,
  404. .length = cpu_to_be32(22),
  405. .ordinal = TPM_ORD_GET_CAP
  406. };
  407. ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
  408. const char *desc)
  409. {
  410. struct tpm_cmd_t tpm_cmd;
  411. int rc;
  412. struct tpm_chip *chip = dev_get_drvdata(dev);
  413. tpm_cmd.header.in = tpm_getcap_header;
  414. if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
  415. tpm_cmd.params.getcap_in.cap = subcap_id;
  416. /*subcap field not necessary */
  417. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
  418. tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
  419. } else {
  420. if (subcap_id == TPM_CAP_FLAG_PERM ||
  421. subcap_id == TPM_CAP_FLAG_VOL)
  422. tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
  423. else
  424. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  425. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  426. tpm_cmd.params.getcap_in.subcap = subcap_id;
  427. }
  428. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
  429. if (!rc)
  430. *cap = tpm_cmd.params.getcap_out.cap;
  431. return rc;
  432. }
  433. void tpm_gen_interrupt(struct tpm_chip *chip)
  434. {
  435. struct tpm_cmd_t tpm_cmd;
  436. ssize_t rc;
  437. tpm_cmd.header.in = tpm_getcap_header;
  438. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  439. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  440. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  441. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  442. "attempting to determine the timeouts");
  443. }
  444. EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
  445. #define TPM_ORD_STARTUP cpu_to_be32(153)
  446. #define TPM_ST_CLEAR cpu_to_be16(1)
  447. #define TPM_ST_STATE cpu_to_be16(2)
  448. #define TPM_ST_DEACTIVATED cpu_to_be16(3)
  449. static const struct tpm_input_header tpm_startup_header = {
  450. .tag = TPM_TAG_RQU_COMMAND,
  451. .length = cpu_to_be32(12),
  452. .ordinal = TPM_ORD_STARTUP
  453. };
  454. static int tpm_startup(struct tpm_chip *chip, __be16 startup_type)
  455. {
  456. struct tpm_cmd_t start_cmd;
  457. start_cmd.header.in = tpm_startup_header;
  458. start_cmd.params.startup_in.startup_type = startup_type;
  459. return tpm_transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE,
  460. "attempting to start the TPM");
  461. }
  462. int tpm_get_timeouts(struct tpm_chip *chip)
  463. {
  464. struct tpm_cmd_t tpm_cmd;
  465. unsigned long new_timeout[4];
  466. unsigned long old_timeout[4];
  467. struct duration_t *duration_cap;
  468. ssize_t rc;
  469. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  470. /* Fixed timeouts for TPM2 */
  471. chip->vendor.timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
  472. chip->vendor.timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
  473. chip->vendor.timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
  474. chip->vendor.timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
  475. chip->vendor.duration[TPM_SHORT] =
  476. msecs_to_jiffies(TPM2_DURATION_SHORT);
  477. chip->vendor.duration[TPM_MEDIUM] =
  478. msecs_to_jiffies(TPM2_DURATION_MEDIUM);
  479. chip->vendor.duration[TPM_LONG] =
  480. msecs_to_jiffies(TPM2_DURATION_LONG);
  481. return 0;
  482. }
  483. tpm_cmd.header.in = tpm_getcap_header;
  484. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  485. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  486. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  487. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, NULL);
  488. if (rc == TPM_ERR_INVALID_POSTINIT) {
  489. /* The TPM is not started, we are the first to talk to it.
  490. Execute a startup command. */
  491. dev_info(chip->pdev, "Issuing TPM_STARTUP");
  492. if (tpm_startup(chip, TPM_ST_CLEAR))
  493. return rc;
  494. tpm_cmd.header.in = tpm_getcap_header;
  495. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  496. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  497. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  498. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  499. NULL);
  500. }
  501. if (rc) {
  502. dev_err(chip->pdev,
  503. "A TPM error (%zd) occurred attempting to determine the timeouts\n",
  504. rc);
  505. goto duration;
  506. }
  507. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  508. be32_to_cpu(tpm_cmd.header.out.length)
  509. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
  510. return -EINVAL;
  511. old_timeout[0] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.a);
  512. old_timeout[1] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.b);
  513. old_timeout[2] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.c);
  514. old_timeout[3] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.d);
  515. memcpy(new_timeout, old_timeout, sizeof(new_timeout));
  516. /*
  517. * Provide ability for vendor overrides of timeout values in case
  518. * of misreporting.
  519. */
  520. if (chip->ops->update_timeouts != NULL)
  521. chip->vendor.timeout_adjusted =
  522. chip->ops->update_timeouts(chip, new_timeout);
  523. if (!chip->vendor.timeout_adjusted) {
  524. /* Don't overwrite default if value is 0 */
  525. if (new_timeout[0] != 0 && new_timeout[0] < 1000) {
  526. int i;
  527. /* timeouts in msec rather usec */
  528. for (i = 0; i != ARRAY_SIZE(new_timeout); i++)
  529. new_timeout[i] *= 1000;
  530. chip->vendor.timeout_adjusted = true;
  531. }
  532. }
  533. /* Report adjusted timeouts */
  534. if (chip->vendor.timeout_adjusted) {
  535. dev_info(chip->pdev,
  536. HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
  537. old_timeout[0], new_timeout[0],
  538. old_timeout[1], new_timeout[1],
  539. old_timeout[2], new_timeout[2],
  540. old_timeout[3], new_timeout[3]);
  541. }
  542. chip->vendor.timeout_a = usecs_to_jiffies(new_timeout[0]);
  543. chip->vendor.timeout_b = usecs_to_jiffies(new_timeout[1]);
  544. chip->vendor.timeout_c = usecs_to_jiffies(new_timeout[2]);
  545. chip->vendor.timeout_d = usecs_to_jiffies(new_timeout[3]);
  546. duration:
  547. tpm_cmd.header.in = tpm_getcap_header;
  548. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  549. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  550. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
  551. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  552. "attempting to determine the durations");
  553. if (rc)
  554. return rc;
  555. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  556. be32_to_cpu(tpm_cmd.header.out.length)
  557. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
  558. return -EINVAL;
  559. duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
  560. chip->vendor.duration[TPM_SHORT] =
  561. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
  562. chip->vendor.duration[TPM_MEDIUM] =
  563. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
  564. chip->vendor.duration[TPM_LONG] =
  565. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
  566. /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
  567. * value wrong and apparently reports msecs rather than usecs. So we
  568. * fix up the resulting too-small TPM_SHORT value to make things work.
  569. * We also scale the TPM_MEDIUM and -_LONG values by 1000.
  570. */
  571. if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
  572. chip->vendor.duration[TPM_SHORT] = HZ;
  573. chip->vendor.duration[TPM_MEDIUM] *= 1000;
  574. chip->vendor.duration[TPM_LONG] *= 1000;
  575. chip->vendor.duration_adjusted = true;
  576. dev_info(chip->pdev, "Adjusting TPM timeout parameters.");
  577. }
  578. return 0;
  579. }
  580. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  581. #define TPM_ORD_CONTINUE_SELFTEST 83
  582. #define CONTINUE_SELFTEST_RESULT_SIZE 10
  583. static struct tpm_input_header continue_selftest_header = {
  584. .tag = TPM_TAG_RQU_COMMAND,
  585. .length = cpu_to_be32(10),
  586. .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
  587. };
  588. /**
  589. * tpm_continue_selftest -- run TPM's selftest
  590. * @chip: TPM chip to use
  591. *
  592. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  593. * a TPM error code.
  594. */
  595. static int tpm_continue_selftest(struct tpm_chip *chip)
  596. {
  597. int rc;
  598. struct tpm_cmd_t cmd;
  599. cmd.header.in = continue_selftest_header;
  600. rc = tpm_transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
  601. "continue selftest");
  602. return rc;
  603. }
  604. #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
  605. #define READ_PCR_RESULT_SIZE 30
  606. static struct tpm_input_header pcrread_header = {
  607. .tag = TPM_TAG_RQU_COMMAND,
  608. .length = cpu_to_be32(14),
  609. .ordinal = TPM_ORDINAL_PCRREAD
  610. };
  611. int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  612. {
  613. int rc;
  614. struct tpm_cmd_t cmd;
  615. cmd.header.in = pcrread_header;
  616. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
  617. rc = tpm_transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
  618. "attempting to read a pcr value");
  619. if (rc == 0)
  620. memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
  621. TPM_DIGEST_SIZE);
  622. return rc;
  623. }
  624. /**
  625. * tpm_is_tpm2 - is the chip a TPM2 chip?
  626. * @chip_num: tpm idx # or ANY
  627. *
  628. * Returns < 0 on error, and 1 or 0 on success depending whether the chip
  629. * is a TPM2 chip.
  630. */
  631. int tpm_is_tpm2(u32 chip_num)
  632. {
  633. struct tpm_chip *chip;
  634. int rc;
  635. chip = tpm_chip_find_get(chip_num);
  636. if (chip == NULL)
  637. return -ENODEV;
  638. rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
  639. tpm_chip_put(chip);
  640. return rc;
  641. }
  642. EXPORT_SYMBOL_GPL(tpm_is_tpm2);
  643. /**
  644. * tpm_pcr_read - read a pcr value
  645. * @chip_num: tpm idx # or ANY
  646. * @pcr_idx: pcr idx to retrieve
  647. * @res_buf: TPM_PCR value
  648. * size of res_buf is 20 bytes (or NULL if you don't care)
  649. *
  650. * The TPM driver should be built-in, but for whatever reason it
  651. * isn't, protect against the chip disappearing, by incrementing
  652. * the module usage count.
  653. */
  654. int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
  655. {
  656. struct tpm_chip *chip;
  657. int rc;
  658. chip = tpm_chip_find_get(chip_num);
  659. if (chip == NULL)
  660. return -ENODEV;
  661. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  662. rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
  663. else
  664. rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf);
  665. tpm_chip_put(chip);
  666. return rc;
  667. }
  668. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  669. /**
  670. * tpm_pcr_extend - extend pcr value with hash
  671. * @chip_num: tpm idx # or AN&
  672. * @pcr_idx: pcr idx to extend
  673. * @hash: hash value used to extend pcr value
  674. *
  675. * The TPM driver should be built-in, but for whatever reason it
  676. * isn't, protect against the chip disappearing, by incrementing
  677. * the module usage count.
  678. */
  679. #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
  680. #define EXTEND_PCR_RESULT_SIZE 34
  681. static struct tpm_input_header pcrextend_header = {
  682. .tag = TPM_TAG_RQU_COMMAND,
  683. .length = cpu_to_be32(34),
  684. .ordinal = TPM_ORD_PCR_EXTEND
  685. };
  686. int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
  687. {
  688. struct tpm_cmd_t cmd;
  689. int rc;
  690. struct tpm_chip *chip;
  691. chip = tpm_chip_find_get(chip_num);
  692. if (chip == NULL)
  693. return -ENODEV;
  694. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  695. rc = tpm2_pcr_extend(chip, pcr_idx, hash);
  696. tpm_chip_put(chip);
  697. return rc;
  698. }
  699. cmd.header.in = pcrextend_header;
  700. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  701. memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
  702. rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  703. "attempting extend a PCR value");
  704. tpm_chip_put(chip);
  705. return rc;
  706. }
  707. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  708. /**
  709. * tpm_do_selftest - have the TPM continue its selftest and wait until it
  710. * can receive further commands
  711. * @chip: TPM chip to use
  712. *
  713. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  714. * a TPM error code.
  715. */
  716. int tpm_do_selftest(struct tpm_chip *chip)
  717. {
  718. int rc;
  719. unsigned int loops;
  720. unsigned int delay_msec = 100;
  721. unsigned long duration;
  722. struct tpm_cmd_t cmd;
  723. duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST);
  724. loops = jiffies_to_msecs(duration) / delay_msec;
  725. rc = tpm_continue_selftest(chip);
  726. /* This may fail if there was no TPM driver during a suspend/resume
  727. * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
  728. */
  729. if (rc)
  730. return rc;
  731. do {
  732. /* Attempt to read a PCR value */
  733. cmd.header.in = pcrread_header;
  734. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(0);
  735. rc = tpm_transmit(chip, (u8 *) &cmd, READ_PCR_RESULT_SIZE);
  736. /* Some buggy TPMs will not respond to tpm_tis_ready() for
  737. * around 300ms while the self test is ongoing, keep trying
  738. * until the self test duration expires. */
  739. if (rc == -ETIME) {
  740. dev_info(chip->pdev, HW_ERR "TPM command timed out during continue self test");
  741. msleep(delay_msec);
  742. continue;
  743. }
  744. if (rc < TPM_HEADER_SIZE)
  745. return -EFAULT;
  746. rc = be32_to_cpu(cmd.header.out.return_code);
  747. if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
  748. dev_info(chip->pdev,
  749. "TPM is disabled/deactivated (0x%X)\n", rc);
  750. /* TPM is disabled and/or deactivated; driver can
  751. * proceed and TPM does handle commands for
  752. * suspend/resume correctly
  753. */
  754. return 0;
  755. }
  756. if (rc != TPM_WARN_DOING_SELFTEST)
  757. return rc;
  758. msleep(delay_msec);
  759. } while (--loops > 0);
  760. return rc;
  761. }
  762. EXPORT_SYMBOL_GPL(tpm_do_selftest);
  763. int tpm_send(u32 chip_num, void *cmd, size_t buflen)
  764. {
  765. struct tpm_chip *chip;
  766. int rc;
  767. chip = tpm_chip_find_get(chip_num);
  768. if (chip == NULL)
  769. return -ENODEV;
  770. rc = tpm_transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
  771. tpm_chip_put(chip);
  772. return rc;
  773. }
  774. EXPORT_SYMBOL_GPL(tpm_send);
  775. static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
  776. bool check_cancel, bool *canceled)
  777. {
  778. u8 status = chip->ops->status(chip);
  779. *canceled = false;
  780. if ((status & mask) == mask)
  781. return true;
  782. if (check_cancel && chip->ops->req_canceled(chip, status)) {
  783. *canceled = true;
  784. return true;
  785. }
  786. return false;
  787. }
  788. int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  789. wait_queue_head_t *queue, bool check_cancel)
  790. {
  791. unsigned long stop;
  792. long rc;
  793. u8 status;
  794. bool canceled = false;
  795. /* check current status */
  796. status = chip->ops->status(chip);
  797. if ((status & mask) == mask)
  798. return 0;
  799. stop = jiffies + timeout;
  800. if (chip->vendor.irq) {
  801. again:
  802. timeout = stop - jiffies;
  803. if ((long)timeout <= 0)
  804. return -ETIME;
  805. rc = wait_event_interruptible_timeout(*queue,
  806. wait_for_tpm_stat_cond(chip, mask, check_cancel,
  807. &canceled),
  808. timeout);
  809. if (rc > 0) {
  810. if (canceled)
  811. return -ECANCELED;
  812. return 0;
  813. }
  814. if (rc == -ERESTARTSYS && freezing(current)) {
  815. clear_thread_flag(TIF_SIGPENDING);
  816. goto again;
  817. }
  818. } else {
  819. do {
  820. msleep(TPM_TIMEOUT);
  821. status = chip->ops->status(chip);
  822. if ((status & mask) == mask)
  823. return 0;
  824. } while (time_before(jiffies, stop));
  825. }
  826. return -ETIME;
  827. }
  828. EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
  829. #define TPM_ORD_SAVESTATE cpu_to_be32(152)
  830. #define SAVESTATE_RESULT_SIZE 10
  831. static struct tpm_input_header savestate_header = {
  832. .tag = TPM_TAG_RQU_COMMAND,
  833. .length = cpu_to_be32(10),
  834. .ordinal = TPM_ORD_SAVESTATE
  835. };
  836. /*
  837. * We are about to suspend. Save the TPM state
  838. * so that it can be restored.
  839. */
  840. int tpm_pm_suspend(struct device *dev)
  841. {
  842. struct tpm_chip *chip = dev_get_drvdata(dev);
  843. struct tpm_cmd_t cmd;
  844. int rc, try;
  845. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  846. if (chip == NULL)
  847. return -ENODEV;
  848. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  849. tpm2_shutdown(chip, TPM2_SU_STATE);
  850. return 0;
  851. }
  852. /* for buggy tpm, flush pcrs with extend to selected dummy */
  853. if (tpm_suspend_pcr) {
  854. cmd.header.in = pcrextend_header;
  855. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
  856. memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
  857. TPM_DIGEST_SIZE);
  858. rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  859. "extending dummy pcr before suspend");
  860. }
  861. /* now do the actual savestate */
  862. for (try = 0; try < TPM_RETRY; try++) {
  863. cmd.header.in = savestate_header;
  864. rc = tpm_transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, NULL);
  865. /*
  866. * If the TPM indicates that it is too busy to respond to
  867. * this command then retry before giving up. It can take
  868. * several seconds for this TPM to be ready.
  869. *
  870. * This can happen if the TPM has already been sent the
  871. * SaveState command before the driver has loaded. TCG 1.2
  872. * specification states that any communication after SaveState
  873. * may cause the TPM to invalidate previously saved state.
  874. */
  875. if (rc != TPM_WARN_RETRY)
  876. break;
  877. msleep(TPM_TIMEOUT_RETRY);
  878. }
  879. if (rc)
  880. dev_err(chip->pdev,
  881. "Error (%d) sending savestate before suspend\n", rc);
  882. else if (try > 0)
  883. dev_warn(chip->pdev, "TPM savestate took %dms\n",
  884. try * TPM_TIMEOUT_RETRY);
  885. return rc;
  886. }
  887. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  888. /*
  889. * Resume from a power safe. The BIOS already restored
  890. * the TPM state.
  891. */
  892. int tpm_pm_resume(struct device *dev)
  893. {
  894. struct tpm_chip *chip = dev_get_drvdata(dev);
  895. if (chip == NULL)
  896. return -ENODEV;
  897. return 0;
  898. }
  899. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  900. #define TPM_GETRANDOM_RESULT_SIZE 18
  901. static struct tpm_input_header tpm_getrandom_header = {
  902. .tag = TPM_TAG_RQU_COMMAND,
  903. .length = cpu_to_be32(14),
  904. .ordinal = TPM_ORD_GET_RANDOM
  905. };
  906. /**
  907. * tpm_get_random() - Get random bytes from the tpm's RNG
  908. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  909. * @out: destination buffer for the random bytes
  910. * @max: the max number of bytes to write to @out
  911. *
  912. * Returns < 0 on error and the number of bytes read on success
  913. */
  914. int tpm_get_random(u32 chip_num, u8 *out, size_t max)
  915. {
  916. struct tpm_chip *chip;
  917. struct tpm_cmd_t tpm_cmd;
  918. u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
  919. int err, total = 0, retries = 5;
  920. u8 *dest = out;
  921. if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
  922. return -EINVAL;
  923. chip = tpm_chip_find_get(chip_num);
  924. if (chip == NULL)
  925. return -ENODEV;
  926. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  927. err = tpm2_get_random(chip, out, max);
  928. tpm_chip_put(chip);
  929. return err;
  930. }
  931. do {
  932. tpm_cmd.header.in = tpm_getrandom_header;
  933. tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
  934. err = tpm_transmit_cmd(chip, &tpm_cmd,
  935. TPM_GETRANDOM_RESULT_SIZE + num_bytes,
  936. "attempting get random");
  937. if (err)
  938. break;
  939. recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
  940. memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
  941. dest += recd;
  942. total += recd;
  943. num_bytes -= recd;
  944. } while (retries-- && total < max);
  945. tpm_chip_put(chip);
  946. return total ? total : -EIO;
  947. }
  948. EXPORT_SYMBOL_GPL(tpm_get_random);
  949. /**
  950. * tpm_seal_trusted() - seal a trusted key
  951. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  952. * @options: authentication values and other options
  953. * @payload: the key data in clear and encrypted form
  954. *
  955. * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips
  956. * are supported.
  957. */
  958. int tpm_seal_trusted(u32 chip_num, struct trusted_key_payload *payload,
  959. struct trusted_key_options *options)
  960. {
  961. struct tpm_chip *chip;
  962. int rc;
  963. chip = tpm_chip_find_get(chip_num);
  964. if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2))
  965. return -ENODEV;
  966. rc = tpm2_seal_trusted(chip, payload, options);
  967. tpm_chip_put(chip);
  968. return rc;
  969. }
  970. EXPORT_SYMBOL_GPL(tpm_seal_trusted);
  971. /**
  972. * tpm_unseal_trusted() - unseal a trusted key
  973. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  974. * @options: authentication values and other options
  975. * @payload: the key data in clear and encrypted form
  976. *
  977. * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips
  978. * are supported.
  979. */
  980. int tpm_unseal_trusted(u32 chip_num, struct trusted_key_payload *payload,
  981. struct trusted_key_options *options)
  982. {
  983. struct tpm_chip *chip;
  984. int rc;
  985. chip = tpm_chip_find_get(chip_num);
  986. if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2))
  987. return -ENODEV;
  988. rc = tpm2_unseal_trusted(chip, payload, options);
  989. tpm_chip_put(chip);
  990. return rc;
  991. }
  992. EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
  993. static int __init tpm_init(void)
  994. {
  995. int rc;
  996. tpm_class = class_create(THIS_MODULE, "tpm");
  997. if (IS_ERR(tpm_class)) {
  998. pr_err("couldn't create tpm class\n");
  999. return PTR_ERR(tpm_class);
  1000. }
  1001. rc = alloc_chrdev_region(&tpm_devt, 0, TPM_NUM_DEVICES, "tpm");
  1002. if (rc < 0) {
  1003. pr_err("tpm: failed to allocate char dev region\n");
  1004. class_destroy(tpm_class);
  1005. return rc;
  1006. }
  1007. return 0;
  1008. }
  1009. static void __exit tpm_exit(void)
  1010. {
  1011. class_destroy(tpm_class);
  1012. unregister_chrdev_region(tpm_devt, TPM_NUM_DEVICES);
  1013. }
  1014. subsys_initcall(tpm_init);
  1015. module_exit(tpm_exit);
  1016. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1017. MODULE_DESCRIPTION("TPM Driver");
  1018. MODULE_VERSION("2.0");
  1019. MODULE_LICENSE("GPL");