tpm-interface.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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 <linux/tpm_eventlog.h>
  32. #include "tpm.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 facilitate 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->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. static int tpm_validate_command(struct tpm_chip *chip,
  322. struct tpm_space *space,
  323. const u8 *cmd,
  324. size_t len)
  325. {
  326. const struct tpm_input_header *header = (const void *)cmd;
  327. int i;
  328. u32 cc;
  329. u32 attrs;
  330. unsigned int nr_handles;
  331. if (len < TPM_HEADER_SIZE)
  332. return -EINVAL;
  333. if (!space)
  334. return 0;
  335. if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) {
  336. cc = be32_to_cpu(header->ordinal);
  337. i = tpm2_find_cc(chip, cc);
  338. if (i < 0) {
  339. dev_dbg(&chip->dev, "0x%04X is an invalid command\n",
  340. cc);
  341. return -EOPNOTSUPP;
  342. }
  343. attrs = chip->cc_attrs_tbl[i];
  344. nr_handles =
  345. 4 * ((attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0));
  346. if (len < TPM_HEADER_SIZE + 4 * nr_handles)
  347. goto err_len;
  348. }
  349. return 0;
  350. err_len:
  351. dev_dbg(&chip->dev,
  352. "%s: insufficient command length %zu", __func__, len);
  353. return -EINVAL;
  354. }
  355. static int tpm_request_locality(struct tpm_chip *chip, unsigned int flags)
  356. {
  357. int rc;
  358. if (flags & TPM_TRANSMIT_NESTED)
  359. return 0;
  360. if (!chip->ops->request_locality)
  361. return 0;
  362. rc = chip->ops->request_locality(chip, 0);
  363. if (rc < 0)
  364. return rc;
  365. chip->locality = rc;
  366. return 0;
  367. }
  368. static void tpm_relinquish_locality(struct tpm_chip *chip, unsigned int flags)
  369. {
  370. int rc;
  371. if (flags & TPM_TRANSMIT_NESTED)
  372. return;
  373. if (!chip->ops->relinquish_locality)
  374. return;
  375. rc = chip->ops->relinquish_locality(chip, chip->locality);
  376. if (rc)
  377. dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
  378. chip->locality = -1;
  379. }
  380. static int tpm_cmd_ready(struct tpm_chip *chip, unsigned int flags)
  381. {
  382. if (flags & TPM_TRANSMIT_NESTED)
  383. return 0;
  384. if (!chip->ops->cmd_ready)
  385. return 0;
  386. return chip->ops->cmd_ready(chip);
  387. }
  388. static int tpm_go_idle(struct tpm_chip *chip, unsigned int flags)
  389. {
  390. if (flags & TPM_TRANSMIT_NESTED)
  391. return 0;
  392. if (!chip->ops->go_idle)
  393. return 0;
  394. return chip->ops->go_idle(chip);
  395. }
  396. static ssize_t tpm_try_transmit(struct tpm_chip *chip,
  397. struct tpm_space *space,
  398. u8 *buf, size_t bufsiz,
  399. unsigned int flags)
  400. {
  401. struct tpm_output_header *header = (void *)buf;
  402. int rc;
  403. ssize_t len = 0;
  404. u32 count, ordinal;
  405. unsigned long stop;
  406. bool need_locality;
  407. rc = tpm_validate_command(chip, space, buf, bufsiz);
  408. if (rc == -EINVAL)
  409. return rc;
  410. /*
  411. * If the command is not implemented by the TPM, synthesize a
  412. * response with a TPM2_RC_COMMAND_CODE return for user-space.
  413. */
  414. if (rc == -EOPNOTSUPP) {
  415. header->length = cpu_to_be32(sizeof(*header));
  416. header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
  417. header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
  418. TSS2_RESMGR_TPM_RC_LAYER);
  419. return sizeof(*header);
  420. }
  421. if (bufsiz > TPM_BUFSIZE)
  422. bufsiz = TPM_BUFSIZE;
  423. count = be32_to_cpu(*((__be32 *) (buf + 2)));
  424. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  425. if (count == 0)
  426. return -ENODATA;
  427. if (count > bufsiz) {
  428. dev_err(&chip->dev,
  429. "invalid count value %x %zx\n", count, bufsiz);
  430. return -E2BIG;
  431. }
  432. if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
  433. mutex_lock(&chip->tpm_mutex);
  434. if (chip->ops->clk_enable != NULL)
  435. chip->ops->clk_enable(chip, true);
  436. /* Store the decision as chip->locality will be changed. */
  437. need_locality = chip->locality == -1;
  438. if (need_locality) {
  439. rc = tpm_request_locality(chip, flags);
  440. if (rc < 0)
  441. goto out_no_locality;
  442. }
  443. rc = tpm_cmd_ready(chip, flags);
  444. if (rc)
  445. goto out;
  446. rc = tpm2_prepare_space(chip, space, ordinal, buf);
  447. if (rc)
  448. goto out;
  449. rc = chip->ops->send(chip, buf, count);
  450. if (rc < 0) {
  451. if (rc != -EPIPE)
  452. dev_err(&chip->dev,
  453. "%s: tpm_send: error %d\n", __func__, rc);
  454. goto out;
  455. }
  456. if (chip->flags & TPM_CHIP_FLAG_IRQ)
  457. goto out_recv;
  458. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  459. stop = jiffies + tpm2_calc_ordinal_duration(chip, ordinal);
  460. else
  461. stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
  462. do {
  463. u8 status = chip->ops->status(chip);
  464. if ((status & chip->ops->req_complete_mask) ==
  465. chip->ops->req_complete_val)
  466. goto out_recv;
  467. if (chip->ops->req_canceled(chip, status)) {
  468. dev_err(&chip->dev, "Operation Canceled\n");
  469. rc = -ECANCELED;
  470. goto out;
  471. }
  472. tpm_msleep(TPM_TIMEOUT_POLL);
  473. rmb();
  474. } while (time_before(jiffies, stop));
  475. chip->ops->cancel(chip);
  476. dev_err(&chip->dev, "Operation Timed out\n");
  477. rc = -ETIME;
  478. goto out;
  479. out_recv:
  480. len = chip->ops->recv(chip, buf, bufsiz);
  481. if (len < 0) {
  482. rc = len;
  483. dev_err(&chip->dev,
  484. "tpm_transmit: tpm_recv: error %d\n", rc);
  485. goto out;
  486. } else if (len < TPM_HEADER_SIZE) {
  487. rc = -EFAULT;
  488. goto out;
  489. }
  490. if (len != be32_to_cpu(header->length)) {
  491. rc = -EFAULT;
  492. goto out;
  493. }
  494. rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
  495. if (rc)
  496. dev_err(&chip->dev, "tpm2_commit_space: error %d\n", rc);
  497. out:
  498. rc = tpm_go_idle(chip, flags);
  499. if (rc)
  500. goto out;
  501. if (need_locality)
  502. tpm_relinquish_locality(chip, flags);
  503. out_no_locality:
  504. if (chip->ops->clk_enable != NULL)
  505. chip->ops->clk_enable(chip, false);
  506. if (!(flags & TPM_TRANSMIT_UNLOCKED) && !(flags & TPM_TRANSMIT_NESTED))
  507. mutex_unlock(&chip->tpm_mutex);
  508. return rc ? rc : len;
  509. }
  510. /**
  511. * tpm_transmit - Internal kernel interface to transmit TPM commands.
  512. *
  513. * @chip: TPM chip to use
  514. * @space: tpm space
  515. * @buf: TPM command buffer
  516. * @bufsiz: length of the TPM command buffer
  517. * @flags: tpm transmit flags - bitmap
  518. *
  519. * A wrapper around tpm_try_transmit that handles TPM2_RC_RETRY
  520. * returns from the TPM and retransmits the command after a delay up
  521. * to a maximum wait of TPM2_DURATION_LONG.
  522. *
  523. * Note: TPM1 never returns TPM2_RC_RETRY so the retry logic is TPM2
  524. * only
  525. *
  526. * Return:
  527. * the length of the return when the operation is successful.
  528. * A negative number for system errors (errno).
  529. */
  530. ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
  531. u8 *buf, size_t bufsiz, unsigned int flags)
  532. {
  533. struct tpm_output_header *header = (struct tpm_output_header *)buf;
  534. /* space for header and handles */
  535. u8 save[TPM_HEADER_SIZE + 3*sizeof(u32)];
  536. unsigned int delay_msec = TPM2_DURATION_SHORT;
  537. u32 rc = 0;
  538. ssize_t ret;
  539. const size_t save_size = min(space ? sizeof(save) : TPM_HEADER_SIZE,
  540. bufsiz);
  541. /* the command code is where the return code will be */
  542. u32 cc = be32_to_cpu(header->return_code);
  543. /*
  544. * Subtlety here: if we have a space, the handles will be
  545. * transformed, so when we restore the header we also have to
  546. * restore the handles.
  547. */
  548. memcpy(save, buf, save_size);
  549. for (;;) {
  550. ret = tpm_try_transmit(chip, space, buf, bufsiz, flags);
  551. if (ret < 0)
  552. break;
  553. rc = be32_to_cpu(header->return_code);
  554. if (rc != TPM2_RC_RETRY && rc != TPM2_RC_TESTING)
  555. break;
  556. /*
  557. * return immediately if self test returns test
  558. * still running to shorten boot time.
  559. */
  560. if (rc == TPM2_RC_TESTING && cc == TPM2_CC_SELF_TEST)
  561. break;
  562. if (delay_msec > TPM2_DURATION_LONG) {
  563. if (rc == TPM2_RC_RETRY)
  564. dev_err(&chip->dev, "in retry loop\n");
  565. else
  566. dev_err(&chip->dev,
  567. "self test is still running\n");
  568. break;
  569. }
  570. tpm_msleep(delay_msec);
  571. delay_msec *= 2;
  572. memcpy(buf, save, save_size);
  573. }
  574. return ret;
  575. }
  576. /**
  577. * tpm_transmit_cmd - send a tpm command to the device
  578. * The function extracts tpm out header return code
  579. *
  580. * @chip: TPM chip to use
  581. * @space: tpm space
  582. * @buf: TPM command buffer
  583. * @bufsiz: length of the buffer
  584. * @min_rsp_body_length: minimum expected length of response body
  585. * @flags: tpm transmit flags - bitmap
  586. * @desc: command description used in the error message
  587. *
  588. * Return:
  589. * 0 when the operation is successful.
  590. * A negative number for system errors (errno).
  591. * A positive number for a TPM error.
  592. */
  593. ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
  594. void *buf, size_t bufsiz,
  595. size_t min_rsp_body_length, unsigned int flags,
  596. const char *desc)
  597. {
  598. const struct tpm_output_header *header = buf;
  599. int err;
  600. ssize_t len;
  601. len = tpm_transmit(chip, space, buf, bufsiz, flags);
  602. if (len < 0)
  603. return len;
  604. err = be32_to_cpu(header->return_code);
  605. if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED
  606. && desc)
  607. dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
  608. desc);
  609. if (err)
  610. return err;
  611. if (len < min_rsp_body_length + TPM_HEADER_SIZE)
  612. return -EFAULT;
  613. return 0;
  614. }
  615. EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
  616. #define TPM_ORD_STARTUP 153
  617. #define TPM_ST_CLEAR 1
  618. /**
  619. * tpm_startup - turn on the TPM
  620. * @chip: TPM chip to use
  621. *
  622. * Normally the firmware should start the TPM. This function is provided as a
  623. * workaround if this does not happen. A legal case for this could be for
  624. * example when a TPM emulator is used.
  625. *
  626. * Return: same as tpm_transmit_cmd()
  627. */
  628. int tpm_startup(struct tpm_chip *chip)
  629. {
  630. struct tpm_buf buf;
  631. int rc;
  632. dev_info(&chip->dev, "starting up the TPM manually\n");
  633. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  634. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP);
  635. if (rc < 0)
  636. return rc;
  637. tpm_buf_append_u16(&buf, TPM2_SU_CLEAR);
  638. } else {
  639. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_STARTUP);
  640. if (rc < 0)
  641. return rc;
  642. tpm_buf_append_u16(&buf, TPM_ST_CLEAR);
  643. }
  644. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
  645. "attempting to start the TPM");
  646. tpm_buf_destroy(&buf);
  647. return rc;
  648. }
  649. #define TPM_DIGEST_SIZE 20
  650. #define TPM_RET_CODE_IDX 6
  651. #define TPM_INTERNAL_RESULT_SIZE 200
  652. #define TPM_ORD_GET_CAP 101
  653. #define TPM_ORD_GET_RANDOM 70
  654. static const struct tpm_input_header tpm_getcap_header = {
  655. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  656. .length = cpu_to_be32(22),
  657. .ordinal = cpu_to_be32(TPM_ORD_GET_CAP)
  658. };
  659. ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
  660. const char *desc, size_t min_cap_length)
  661. {
  662. struct tpm_buf buf;
  663. int rc;
  664. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP);
  665. if (rc)
  666. return rc;
  667. if (subcap_id == TPM_CAP_VERSION_1_1 ||
  668. subcap_id == TPM_CAP_VERSION_1_2) {
  669. tpm_buf_append_u32(&buf, subcap_id);
  670. tpm_buf_append_u32(&buf, 0);
  671. } else {
  672. if (subcap_id == TPM_CAP_FLAG_PERM ||
  673. subcap_id == TPM_CAP_FLAG_VOL)
  674. tpm_buf_append_u32(&buf, TPM_CAP_FLAG);
  675. else
  676. tpm_buf_append_u32(&buf, TPM_CAP_PROP);
  677. tpm_buf_append_u32(&buf, 4);
  678. tpm_buf_append_u32(&buf, subcap_id);
  679. }
  680. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
  681. min_cap_length, 0, desc);
  682. if (!rc)
  683. *cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
  684. tpm_buf_destroy(&buf);
  685. return rc;
  686. }
  687. EXPORT_SYMBOL_GPL(tpm_getcap);
  688. int tpm_get_timeouts(struct tpm_chip *chip)
  689. {
  690. cap_t cap;
  691. unsigned long timeout_old[4], timeout_chip[4], timeout_eff[4];
  692. ssize_t rc;
  693. if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
  694. return 0;
  695. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  696. /* Fixed timeouts for TPM2 */
  697. chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
  698. chip->timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
  699. chip->timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
  700. chip->timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
  701. chip->duration[TPM_SHORT] =
  702. msecs_to_jiffies(TPM2_DURATION_SHORT);
  703. chip->duration[TPM_MEDIUM] =
  704. msecs_to_jiffies(TPM2_DURATION_MEDIUM);
  705. chip->duration[TPM_LONG] =
  706. msecs_to_jiffies(TPM2_DURATION_LONG);
  707. chip->duration[TPM_LONG_LONG] =
  708. msecs_to_jiffies(TPM2_DURATION_LONG_LONG);
  709. chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
  710. return 0;
  711. }
  712. rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, NULL,
  713. sizeof(cap.timeout));
  714. if (rc == TPM_ERR_INVALID_POSTINIT) {
  715. if (tpm_startup(chip))
  716. return rc;
  717. rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap,
  718. "attempting to determine the timeouts",
  719. sizeof(cap.timeout));
  720. }
  721. if (rc) {
  722. dev_err(&chip->dev,
  723. "A TPM error (%zd) occurred attempting to determine the timeouts\n",
  724. rc);
  725. return rc;
  726. }
  727. timeout_old[0] = jiffies_to_usecs(chip->timeout_a);
  728. timeout_old[1] = jiffies_to_usecs(chip->timeout_b);
  729. timeout_old[2] = jiffies_to_usecs(chip->timeout_c);
  730. timeout_old[3] = jiffies_to_usecs(chip->timeout_d);
  731. timeout_chip[0] = be32_to_cpu(cap.timeout.a);
  732. timeout_chip[1] = be32_to_cpu(cap.timeout.b);
  733. timeout_chip[2] = be32_to_cpu(cap.timeout.c);
  734. timeout_chip[3] = be32_to_cpu(cap.timeout.d);
  735. memcpy(timeout_eff, timeout_chip, sizeof(timeout_eff));
  736. /*
  737. * Provide ability for vendor overrides of timeout values in case
  738. * of misreporting.
  739. */
  740. if (chip->ops->update_timeouts != NULL)
  741. chip->timeout_adjusted =
  742. chip->ops->update_timeouts(chip, timeout_eff);
  743. if (!chip->timeout_adjusted) {
  744. /* Restore default if chip reported 0 */
  745. int i;
  746. for (i = 0; i < ARRAY_SIZE(timeout_eff); i++) {
  747. if (timeout_eff[i])
  748. continue;
  749. timeout_eff[i] = timeout_old[i];
  750. chip->timeout_adjusted = true;
  751. }
  752. if (timeout_eff[0] != 0 && timeout_eff[0] < 1000) {
  753. /* timeouts in msec rather usec */
  754. for (i = 0; i != ARRAY_SIZE(timeout_eff); i++)
  755. timeout_eff[i] *= 1000;
  756. chip->timeout_adjusted = true;
  757. }
  758. }
  759. /* Report adjusted timeouts */
  760. if (chip->timeout_adjusted) {
  761. dev_info(&chip->dev,
  762. HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
  763. timeout_chip[0], timeout_eff[0],
  764. timeout_chip[1], timeout_eff[1],
  765. timeout_chip[2], timeout_eff[2],
  766. timeout_chip[3], timeout_eff[3]);
  767. }
  768. chip->timeout_a = usecs_to_jiffies(timeout_eff[0]);
  769. chip->timeout_b = usecs_to_jiffies(timeout_eff[1]);
  770. chip->timeout_c = usecs_to_jiffies(timeout_eff[2]);
  771. chip->timeout_d = usecs_to_jiffies(timeout_eff[3]);
  772. rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_DURATION, &cap,
  773. "attempting to determine the durations",
  774. sizeof(cap.duration));
  775. if (rc)
  776. return rc;
  777. chip->duration[TPM_SHORT] =
  778. usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_short));
  779. chip->duration[TPM_MEDIUM] =
  780. usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_medium));
  781. chip->duration[TPM_LONG] =
  782. usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_long));
  783. chip->duration[TPM_LONG_LONG] = 0; /* not used under 1.2 */
  784. /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
  785. * value wrong and apparently reports msecs rather than usecs. So we
  786. * fix up the resulting too-small TPM_SHORT value to make things work.
  787. * We also scale the TPM_MEDIUM and -_LONG values by 1000.
  788. */
  789. if (chip->duration[TPM_SHORT] < (HZ / 100)) {
  790. chip->duration[TPM_SHORT] = HZ;
  791. chip->duration[TPM_MEDIUM] *= 1000;
  792. chip->duration[TPM_LONG] *= 1000;
  793. chip->duration_adjusted = true;
  794. dev_info(&chip->dev, "Adjusting TPM timeout parameters.");
  795. }
  796. chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
  797. return 0;
  798. }
  799. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  800. #define TPM_ORD_CONTINUE_SELFTEST 83
  801. #define CONTINUE_SELFTEST_RESULT_SIZE 10
  802. static const struct tpm_input_header continue_selftest_header = {
  803. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  804. .length = cpu_to_be32(10),
  805. .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
  806. };
  807. /**
  808. * tpm_continue_selftest -- run TPM's selftest
  809. * @chip: TPM chip to use
  810. *
  811. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  812. * a TPM error code.
  813. */
  814. static int tpm_continue_selftest(struct tpm_chip *chip)
  815. {
  816. int rc;
  817. struct tpm_cmd_t cmd;
  818. cmd.header.in = continue_selftest_header;
  819. rc = tpm_transmit_cmd(chip, NULL, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
  820. 0, 0, "continue selftest");
  821. return rc;
  822. }
  823. #define TPM_ORDINAL_PCRREAD 21
  824. #define READ_PCR_RESULT_SIZE 30
  825. #define READ_PCR_RESULT_BODY_SIZE 20
  826. static const struct tpm_input_header pcrread_header = {
  827. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  828. .length = cpu_to_be32(14),
  829. .ordinal = cpu_to_be32(TPM_ORDINAL_PCRREAD)
  830. };
  831. int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  832. {
  833. int rc;
  834. struct tpm_cmd_t cmd;
  835. cmd.header.in = pcrread_header;
  836. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
  837. rc = tpm_transmit_cmd(chip, NULL, &cmd, READ_PCR_RESULT_SIZE,
  838. READ_PCR_RESULT_BODY_SIZE, 0,
  839. "attempting to read a pcr value");
  840. if (rc == 0)
  841. memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
  842. TPM_DIGEST_SIZE);
  843. return rc;
  844. }
  845. /**
  846. * tpm_is_tpm2 - do we a have a TPM2 chip?
  847. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  848. *
  849. * Return:
  850. * 1 if we have a TPM2 chip.
  851. * 0 if we don't have a TPM2 chip.
  852. * A negative number for system errors (errno).
  853. */
  854. int tpm_is_tpm2(struct tpm_chip *chip)
  855. {
  856. int rc;
  857. chip = tpm_find_get_ops(chip);
  858. if (!chip)
  859. return -ENODEV;
  860. rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
  861. tpm_put_ops(chip);
  862. return rc;
  863. }
  864. EXPORT_SYMBOL_GPL(tpm_is_tpm2);
  865. /**
  866. * tpm_pcr_read - read a PCR value from SHA1 bank
  867. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  868. * @pcr_idx: the PCR to be retrieved
  869. * @res_buf: the value of the PCR
  870. *
  871. * Return: same as with tpm_transmit_cmd()
  872. */
  873. int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  874. {
  875. int rc;
  876. chip = tpm_find_get_ops(chip);
  877. if (!chip)
  878. return -ENODEV;
  879. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  880. rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
  881. else
  882. rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf);
  883. tpm_put_ops(chip);
  884. return rc;
  885. }
  886. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  887. #define TPM_ORD_PCR_EXTEND 20
  888. #define EXTEND_PCR_RESULT_SIZE 34
  889. #define EXTEND_PCR_RESULT_BODY_SIZE 20
  890. static const struct tpm_input_header pcrextend_header = {
  891. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  892. .length = cpu_to_be32(34),
  893. .ordinal = cpu_to_be32(TPM_ORD_PCR_EXTEND)
  894. };
  895. static int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
  896. char *log_msg)
  897. {
  898. struct tpm_buf buf;
  899. int rc;
  900. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_EXTEND);
  901. if (rc)
  902. return rc;
  903. tpm_buf_append_u32(&buf, pcr_idx);
  904. tpm_buf_append(&buf, hash, TPM_DIGEST_SIZE);
  905. rc = tpm_transmit_cmd(chip, NULL, buf.data, EXTEND_PCR_RESULT_SIZE,
  906. EXTEND_PCR_RESULT_BODY_SIZE, 0, log_msg);
  907. tpm_buf_destroy(&buf);
  908. return rc;
  909. }
  910. /**
  911. * tpm_pcr_extend - extend a PCR value in SHA1 bank.
  912. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  913. * @pcr_idx: the PCR to be retrieved
  914. * @hash: the hash value used to extend the PCR value
  915. *
  916. * Note: with TPM 2.0 extends also those banks with a known digest size to the
  917. * cryto subsystem in order to prevent malicious use of those PCR banks. In the
  918. * future we should dynamically determine digest sizes.
  919. *
  920. * Return: same as with tpm_transmit_cmd()
  921. */
  922. int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
  923. {
  924. int rc;
  925. struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
  926. u32 count = 0;
  927. int i;
  928. chip = tpm_find_get_ops(chip);
  929. if (!chip)
  930. return -ENODEV;
  931. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  932. memset(digest_list, 0, sizeof(digest_list));
  933. for (i = 0; i < ARRAY_SIZE(chip->active_banks) &&
  934. chip->active_banks[i] != TPM2_ALG_ERROR; i++) {
  935. digest_list[i].alg_id = chip->active_banks[i];
  936. memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE);
  937. count++;
  938. }
  939. rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list);
  940. tpm_put_ops(chip);
  941. return rc;
  942. }
  943. rc = tpm1_pcr_extend(chip, pcr_idx, hash,
  944. "attempting extend a PCR value");
  945. tpm_put_ops(chip);
  946. return rc;
  947. }
  948. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  949. /**
  950. * tpm_do_selftest - have the TPM continue its selftest and wait until it
  951. * can receive further commands
  952. * @chip: TPM chip to use
  953. *
  954. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  955. * a TPM error code.
  956. */
  957. int tpm_do_selftest(struct tpm_chip *chip)
  958. {
  959. int rc;
  960. unsigned int loops;
  961. unsigned int delay_msec = 100;
  962. unsigned long duration;
  963. u8 dummy[TPM_DIGEST_SIZE];
  964. duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST);
  965. loops = jiffies_to_msecs(duration) / delay_msec;
  966. rc = tpm_continue_selftest(chip);
  967. if (rc == TPM_ERR_INVALID_POSTINIT) {
  968. chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
  969. dev_info(&chip->dev, "TPM not ready (%d)\n", rc);
  970. }
  971. /* This may fail if there was no TPM driver during a suspend/resume
  972. * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
  973. */
  974. if (rc)
  975. return rc;
  976. do {
  977. /* Attempt to read a PCR value */
  978. rc = tpm_pcr_read_dev(chip, 0, dummy);
  979. /* Some buggy TPMs will not respond to tpm_tis_ready() for
  980. * around 300ms while the self test is ongoing, keep trying
  981. * until the self test duration expires. */
  982. if (rc == -ETIME) {
  983. dev_info(
  984. &chip->dev, HW_ERR
  985. "TPM command timed out during continue self test");
  986. tpm_msleep(delay_msec);
  987. continue;
  988. }
  989. if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
  990. dev_info(&chip->dev,
  991. "TPM is disabled/deactivated (0x%X)\n", rc);
  992. /* TPM is disabled and/or deactivated; driver can
  993. * proceed and TPM does handle commands for
  994. * suspend/resume correctly
  995. */
  996. return 0;
  997. }
  998. if (rc != TPM_WARN_DOING_SELFTEST)
  999. return rc;
  1000. tpm_msleep(delay_msec);
  1001. } while (--loops > 0);
  1002. return rc;
  1003. }
  1004. EXPORT_SYMBOL_GPL(tpm_do_selftest);
  1005. /**
  1006. * tpm1_auto_startup - Perform the standard automatic TPM initialization
  1007. * sequence
  1008. * @chip: TPM chip to use
  1009. *
  1010. * Returns 0 on success, < 0 in case of fatal error.
  1011. */
  1012. int tpm1_auto_startup(struct tpm_chip *chip)
  1013. {
  1014. int rc;
  1015. rc = tpm_get_timeouts(chip);
  1016. if (rc)
  1017. goto out;
  1018. rc = tpm_do_selftest(chip);
  1019. if (rc) {
  1020. dev_err(&chip->dev, "TPM self test failed\n");
  1021. goto out;
  1022. }
  1023. return rc;
  1024. out:
  1025. if (rc > 0)
  1026. rc = -ENODEV;
  1027. return rc;
  1028. }
  1029. /**
  1030. * tpm_send - send a TPM command
  1031. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  1032. * @cmd: a TPM command buffer
  1033. * @buflen: the length of the TPM command buffer
  1034. *
  1035. * Return: same as with tpm_transmit_cmd()
  1036. */
  1037. int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
  1038. {
  1039. int rc;
  1040. chip = tpm_find_get_ops(chip);
  1041. if (!chip)
  1042. return -ENODEV;
  1043. rc = tpm_transmit_cmd(chip, NULL, cmd, buflen, 0, 0,
  1044. "attempting to a send a command");
  1045. tpm_put_ops(chip);
  1046. return rc;
  1047. }
  1048. EXPORT_SYMBOL_GPL(tpm_send);
  1049. #define TPM_ORD_SAVESTATE 152
  1050. #define SAVESTATE_RESULT_SIZE 10
  1051. static const struct tpm_input_header savestate_header = {
  1052. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  1053. .length = cpu_to_be32(10),
  1054. .ordinal = cpu_to_be32(TPM_ORD_SAVESTATE)
  1055. };
  1056. /*
  1057. * We are about to suspend. Save the TPM state
  1058. * so that it can be restored.
  1059. */
  1060. int tpm_pm_suspend(struct device *dev)
  1061. {
  1062. struct tpm_chip *chip = dev_get_drvdata(dev);
  1063. struct tpm_cmd_t cmd;
  1064. int rc, try;
  1065. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  1066. if (chip == NULL)
  1067. return -ENODEV;
  1068. if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
  1069. return 0;
  1070. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  1071. tpm2_shutdown(chip, TPM2_SU_STATE);
  1072. return 0;
  1073. }
  1074. /* for buggy tpm, flush pcrs with extend to selected dummy */
  1075. if (tpm_suspend_pcr)
  1076. rc = tpm1_pcr_extend(chip, tpm_suspend_pcr, dummy_hash,
  1077. "extending dummy pcr before suspend");
  1078. /* now do the actual savestate */
  1079. for (try = 0; try < TPM_RETRY; try++) {
  1080. cmd.header.in = savestate_header;
  1081. rc = tpm_transmit_cmd(chip, NULL, &cmd, SAVESTATE_RESULT_SIZE,
  1082. 0, 0, NULL);
  1083. /*
  1084. * If the TPM indicates that it is too busy to respond to
  1085. * this command then retry before giving up. It can take
  1086. * several seconds for this TPM to be ready.
  1087. *
  1088. * This can happen if the TPM has already been sent the
  1089. * SaveState command before the driver has loaded. TCG 1.2
  1090. * specification states that any communication after SaveState
  1091. * may cause the TPM to invalidate previously saved state.
  1092. */
  1093. if (rc != TPM_WARN_RETRY)
  1094. break;
  1095. tpm_msleep(TPM_TIMEOUT_RETRY);
  1096. }
  1097. if (rc)
  1098. dev_err(&chip->dev,
  1099. "Error (%d) sending savestate before suspend\n", rc);
  1100. else if (try > 0)
  1101. dev_warn(&chip->dev, "TPM savestate took %dms\n",
  1102. try * TPM_TIMEOUT_RETRY);
  1103. return rc;
  1104. }
  1105. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  1106. /*
  1107. * Resume from a power safe. The BIOS already restored
  1108. * the TPM state.
  1109. */
  1110. int tpm_pm_resume(struct device *dev)
  1111. {
  1112. struct tpm_chip *chip = dev_get_drvdata(dev);
  1113. if (chip == NULL)
  1114. return -ENODEV;
  1115. return 0;
  1116. }
  1117. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  1118. #define TPM_GETRANDOM_RESULT_SIZE 18
  1119. static const struct tpm_input_header tpm_getrandom_header = {
  1120. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  1121. .length = cpu_to_be32(14),
  1122. .ordinal = cpu_to_be32(TPM_ORD_GET_RANDOM)
  1123. };
  1124. /**
  1125. * tpm_get_random() - get random bytes from the TPM's RNG
  1126. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  1127. * @out: destination buffer for the random bytes
  1128. * @max: the max number of bytes to write to @out
  1129. *
  1130. * Return: same as with tpm_transmit_cmd()
  1131. */
  1132. int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
  1133. {
  1134. struct tpm_cmd_t tpm_cmd;
  1135. u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA), rlength;
  1136. int err, total = 0, retries = 5;
  1137. u8 *dest = out;
  1138. if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
  1139. return -EINVAL;
  1140. chip = tpm_find_get_ops(chip);
  1141. if (!chip)
  1142. return -ENODEV;
  1143. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  1144. err = tpm2_get_random(chip, out, max);
  1145. tpm_put_ops(chip);
  1146. return err;
  1147. }
  1148. do {
  1149. tpm_cmd.header.in = tpm_getrandom_header;
  1150. tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
  1151. err = tpm_transmit_cmd(chip, NULL, &tpm_cmd,
  1152. TPM_GETRANDOM_RESULT_SIZE + num_bytes,
  1153. offsetof(struct tpm_getrandom_out,
  1154. rng_data),
  1155. 0, "attempting get random");
  1156. if (err)
  1157. break;
  1158. recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
  1159. if (recd > num_bytes) {
  1160. total = -EFAULT;
  1161. break;
  1162. }
  1163. rlength = be32_to_cpu(tpm_cmd.header.out.length);
  1164. if (rlength < TPM_HEADER_SIZE +
  1165. offsetof(struct tpm_getrandom_out, rng_data) +
  1166. recd) {
  1167. total = -EFAULT;
  1168. break;
  1169. }
  1170. memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
  1171. dest += recd;
  1172. total += recd;
  1173. num_bytes -= recd;
  1174. } while (retries-- && total < max);
  1175. tpm_put_ops(chip);
  1176. return total ? total : -EIO;
  1177. }
  1178. EXPORT_SYMBOL_GPL(tpm_get_random);
  1179. /**
  1180. * tpm_seal_trusted() - seal a trusted key payload
  1181. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  1182. * @options: authentication values and other options
  1183. * @payload: the key data in clear and encrypted form
  1184. *
  1185. * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
  1186. * the keyring subsystem.
  1187. *
  1188. * Return: same as with tpm_transmit_cmd()
  1189. */
  1190. int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
  1191. struct trusted_key_options *options)
  1192. {
  1193. int rc;
  1194. chip = tpm_find_get_ops(chip);
  1195. if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
  1196. return -ENODEV;
  1197. rc = tpm2_seal_trusted(chip, payload, options);
  1198. tpm_put_ops(chip);
  1199. return rc;
  1200. }
  1201. EXPORT_SYMBOL_GPL(tpm_seal_trusted);
  1202. /**
  1203. * tpm_unseal_trusted() - unseal a trusted key
  1204. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  1205. * @options: authentication values and other options
  1206. * @payload: the key data in clear and encrypted form
  1207. *
  1208. * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
  1209. * the keyring subsystem.
  1210. *
  1211. * Return: same as with tpm_transmit_cmd()
  1212. */
  1213. int tpm_unseal_trusted(struct tpm_chip *chip,
  1214. struct trusted_key_payload *payload,
  1215. struct trusted_key_options *options)
  1216. {
  1217. int rc;
  1218. chip = tpm_find_get_ops(chip);
  1219. if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
  1220. return -ENODEV;
  1221. rc = tpm2_unseal_trusted(chip, payload, options);
  1222. tpm_put_ops(chip);
  1223. return rc;
  1224. }
  1225. EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
  1226. static int __init tpm_init(void)
  1227. {
  1228. int rc;
  1229. tpm_class = class_create(THIS_MODULE, "tpm");
  1230. if (IS_ERR(tpm_class)) {
  1231. pr_err("couldn't create tpm class\n");
  1232. return PTR_ERR(tpm_class);
  1233. }
  1234. tpmrm_class = class_create(THIS_MODULE, "tpmrm");
  1235. if (IS_ERR(tpmrm_class)) {
  1236. pr_err("couldn't create tpmrm class\n");
  1237. rc = PTR_ERR(tpmrm_class);
  1238. goto out_destroy_tpm_class;
  1239. }
  1240. rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
  1241. if (rc < 0) {
  1242. pr_err("tpm: failed to allocate char dev region\n");
  1243. goto out_destroy_tpmrm_class;
  1244. }
  1245. rc = tpm_dev_common_init();
  1246. if (rc) {
  1247. pr_err("tpm: failed to allocate char dev region\n");
  1248. goto out_unreg_chrdev;
  1249. }
  1250. return 0;
  1251. out_unreg_chrdev:
  1252. unregister_chrdev_region(tpm_devt, 2 * TPM_NUM_DEVICES);
  1253. out_destroy_tpmrm_class:
  1254. class_destroy(tpmrm_class);
  1255. out_destroy_tpm_class:
  1256. class_destroy(tpm_class);
  1257. return rc;
  1258. }
  1259. static void __exit tpm_exit(void)
  1260. {
  1261. idr_destroy(&dev_nums_idr);
  1262. class_destroy(tpm_class);
  1263. class_destroy(tpmrm_class);
  1264. unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
  1265. tpm_dev_common_exit();
  1266. }
  1267. subsys_initcall(tpm_init);
  1268. module_exit(tpm_exit);
  1269. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1270. MODULE_DESCRIPTION("TPM Driver");
  1271. MODULE_VERSION("2.0");
  1272. MODULE_LICENSE("GPL");