tpm-interface.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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/pm_runtime.h>
  32. #include <linux/tpm_eventlog.h>
  33. #include "tpm.h"
  34. #define TPM_MAX_ORDINAL 243
  35. #define TSC_MAX_ORDINAL 12
  36. #define TPM_PROTECTED_COMMAND 0x00
  37. #define TPM_CONNECTION_COMMAND 0x40
  38. /*
  39. * Bug workaround - some TPM's don't flush the most
  40. * recently changed pcr on suspend, so force the flush
  41. * with an extend to the selected _unused_ non-volatile pcr.
  42. */
  43. static int tpm_suspend_pcr;
  44. module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
  45. MODULE_PARM_DESC(suspend_pcr,
  46. "PCR to use for dummy writes to facilitate flush on suspend.");
  47. /*
  48. * Array with one entry per ordinal defining the maximum amount
  49. * of time the chip could take to return the result. The ordinal
  50. * designation of short, medium or long is defined in a table in
  51. * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
  52. * values of the SHORT, MEDIUM, and LONG durations are retrieved
  53. * from the chip during initialization with a call to tpm_get_timeouts.
  54. */
  55. static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
  56. TPM_UNDEFINED, /* 0 */
  57. TPM_UNDEFINED,
  58. TPM_UNDEFINED,
  59. TPM_UNDEFINED,
  60. TPM_UNDEFINED,
  61. TPM_UNDEFINED, /* 5 */
  62. TPM_UNDEFINED,
  63. TPM_UNDEFINED,
  64. TPM_UNDEFINED,
  65. TPM_UNDEFINED,
  66. TPM_SHORT, /* 10 */
  67. TPM_SHORT,
  68. TPM_MEDIUM,
  69. TPM_LONG,
  70. TPM_LONG,
  71. TPM_MEDIUM, /* 15 */
  72. TPM_SHORT,
  73. TPM_SHORT,
  74. TPM_MEDIUM,
  75. TPM_LONG,
  76. TPM_SHORT, /* 20 */
  77. TPM_SHORT,
  78. TPM_MEDIUM,
  79. TPM_MEDIUM,
  80. TPM_MEDIUM,
  81. TPM_SHORT, /* 25 */
  82. TPM_SHORT,
  83. TPM_MEDIUM,
  84. TPM_SHORT,
  85. TPM_SHORT,
  86. TPM_MEDIUM, /* 30 */
  87. TPM_LONG,
  88. TPM_MEDIUM,
  89. TPM_SHORT,
  90. TPM_SHORT,
  91. TPM_SHORT, /* 35 */
  92. TPM_MEDIUM,
  93. TPM_MEDIUM,
  94. TPM_UNDEFINED,
  95. TPM_UNDEFINED,
  96. TPM_MEDIUM, /* 40 */
  97. TPM_LONG,
  98. TPM_MEDIUM,
  99. TPM_SHORT,
  100. TPM_SHORT,
  101. TPM_SHORT, /* 45 */
  102. TPM_SHORT,
  103. TPM_SHORT,
  104. TPM_SHORT,
  105. TPM_LONG,
  106. TPM_MEDIUM, /* 50 */
  107. TPM_MEDIUM,
  108. TPM_UNDEFINED,
  109. TPM_UNDEFINED,
  110. TPM_UNDEFINED,
  111. TPM_UNDEFINED, /* 55 */
  112. TPM_UNDEFINED,
  113. TPM_UNDEFINED,
  114. TPM_UNDEFINED,
  115. TPM_UNDEFINED,
  116. TPM_MEDIUM, /* 60 */
  117. TPM_MEDIUM,
  118. TPM_MEDIUM,
  119. TPM_SHORT,
  120. TPM_SHORT,
  121. TPM_MEDIUM, /* 65 */
  122. TPM_UNDEFINED,
  123. TPM_UNDEFINED,
  124. TPM_UNDEFINED,
  125. TPM_UNDEFINED,
  126. TPM_SHORT, /* 70 */
  127. TPM_SHORT,
  128. TPM_UNDEFINED,
  129. TPM_UNDEFINED,
  130. TPM_UNDEFINED,
  131. TPM_UNDEFINED, /* 75 */
  132. TPM_UNDEFINED,
  133. TPM_UNDEFINED,
  134. TPM_UNDEFINED,
  135. TPM_UNDEFINED,
  136. TPM_LONG, /* 80 */
  137. TPM_UNDEFINED,
  138. TPM_MEDIUM,
  139. TPM_LONG,
  140. TPM_SHORT,
  141. TPM_UNDEFINED, /* 85 */
  142. TPM_UNDEFINED,
  143. TPM_UNDEFINED,
  144. TPM_UNDEFINED,
  145. TPM_UNDEFINED,
  146. TPM_SHORT, /* 90 */
  147. TPM_SHORT,
  148. TPM_SHORT,
  149. TPM_SHORT,
  150. TPM_SHORT,
  151. TPM_UNDEFINED, /* 95 */
  152. TPM_UNDEFINED,
  153. TPM_UNDEFINED,
  154. TPM_UNDEFINED,
  155. TPM_UNDEFINED,
  156. TPM_MEDIUM, /* 100 */
  157. TPM_SHORT,
  158. TPM_SHORT,
  159. TPM_UNDEFINED,
  160. TPM_UNDEFINED,
  161. TPM_UNDEFINED, /* 105 */
  162. TPM_UNDEFINED,
  163. TPM_UNDEFINED,
  164. TPM_UNDEFINED,
  165. TPM_UNDEFINED,
  166. TPM_SHORT, /* 110 */
  167. TPM_SHORT,
  168. TPM_SHORT,
  169. TPM_SHORT,
  170. TPM_SHORT,
  171. TPM_SHORT, /* 115 */
  172. TPM_SHORT,
  173. TPM_SHORT,
  174. TPM_UNDEFINED,
  175. TPM_UNDEFINED,
  176. TPM_LONG, /* 120 */
  177. TPM_LONG,
  178. TPM_MEDIUM,
  179. TPM_UNDEFINED,
  180. TPM_SHORT,
  181. TPM_SHORT, /* 125 */
  182. TPM_SHORT,
  183. TPM_LONG,
  184. TPM_SHORT,
  185. TPM_SHORT,
  186. TPM_SHORT, /* 130 */
  187. TPM_MEDIUM,
  188. TPM_UNDEFINED,
  189. TPM_SHORT,
  190. TPM_MEDIUM,
  191. TPM_UNDEFINED, /* 135 */
  192. TPM_UNDEFINED,
  193. TPM_UNDEFINED,
  194. TPM_UNDEFINED,
  195. TPM_UNDEFINED,
  196. TPM_SHORT, /* 140 */
  197. TPM_SHORT,
  198. TPM_UNDEFINED,
  199. TPM_UNDEFINED,
  200. TPM_UNDEFINED,
  201. TPM_UNDEFINED, /* 145 */
  202. TPM_UNDEFINED,
  203. TPM_UNDEFINED,
  204. TPM_UNDEFINED,
  205. TPM_UNDEFINED,
  206. TPM_SHORT, /* 150 */
  207. TPM_MEDIUM,
  208. TPM_MEDIUM,
  209. TPM_SHORT,
  210. TPM_SHORT,
  211. TPM_UNDEFINED, /* 155 */
  212. TPM_UNDEFINED,
  213. TPM_UNDEFINED,
  214. TPM_UNDEFINED,
  215. TPM_UNDEFINED,
  216. TPM_SHORT, /* 160 */
  217. TPM_SHORT,
  218. TPM_SHORT,
  219. TPM_SHORT,
  220. TPM_UNDEFINED,
  221. TPM_UNDEFINED, /* 165 */
  222. TPM_UNDEFINED,
  223. TPM_UNDEFINED,
  224. TPM_UNDEFINED,
  225. TPM_UNDEFINED,
  226. TPM_LONG, /* 170 */
  227. TPM_UNDEFINED,
  228. TPM_UNDEFINED,
  229. TPM_UNDEFINED,
  230. TPM_UNDEFINED,
  231. TPM_UNDEFINED, /* 175 */
  232. TPM_UNDEFINED,
  233. TPM_UNDEFINED,
  234. TPM_UNDEFINED,
  235. TPM_UNDEFINED,
  236. TPM_MEDIUM, /* 180 */
  237. TPM_SHORT,
  238. TPM_MEDIUM,
  239. TPM_MEDIUM,
  240. TPM_MEDIUM,
  241. TPM_MEDIUM, /* 185 */
  242. TPM_SHORT,
  243. TPM_UNDEFINED,
  244. TPM_UNDEFINED,
  245. TPM_UNDEFINED,
  246. TPM_UNDEFINED, /* 190 */
  247. TPM_UNDEFINED,
  248. TPM_UNDEFINED,
  249. TPM_UNDEFINED,
  250. TPM_UNDEFINED,
  251. TPM_UNDEFINED, /* 195 */
  252. TPM_UNDEFINED,
  253. TPM_UNDEFINED,
  254. TPM_UNDEFINED,
  255. TPM_UNDEFINED,
  256. TPM_SHORT, /* 200 */
  257. TPM_UNDEFINED,
  258. TPM_UNDEFINED,
  259. TPM_UNDEFINED,
  260. TPM_SHORT,
  261. TPM_SHORT, /* 205 */
  262. TPM_SHORT,
  263. TPM_SHORT,
  264. TPM_SHORT,
  265. TPM_SHORT,
  266. TPM_MEDIUM, /* 210 */
  267. TPM_UNDEFINED,
  268. TPM_MEDIUM,
  269. TPM_MEDIUM,
  270. TPM_MEDIUM,
  271. TPM_UNDEFINED, /* 215 */
  272. TPM_MEDIUM,
  273. TPM_UNDEFINED,
  274. TPM_UNDEFINED,
  275. TPM_SHORT,
  276. TPM_SHORT, /* 220 */
  277. TPM_SHORT,
  278. TPM_SHORT,
  279. TPM_SHORT,
  280. TPM_SHORT,
  281. TPM_UNDEFINED, /* 225 */
  282. TPM_UNDEFINED,
  283. TPM_UNDEFINED,
  284. TPM_UNDEFINED,
  285. TPM_UNDEFINED,
  286. TPM_SHORT, /* 230 */
  287. TPM_LONG,
  288. TPM_MEDIUM,
  289. TPM_UNDEFINED,
  290. TPM_UNDEFINED,
  291. TPM_UNDEFINED, /* 235 */
  292. TPM_UNDEFINED,
  293. TPM_UNDEFINED,
  294. TPM_UNDEFINED,
  295. TPM_UNDEFINED,
  296. TPM_SHORT, /* 240 */
  297. TPM_UNDEFINED,
  298. TPM_MEDIUM,
  299. };
  300. /*
  301. * Returns max number of jiffies to wait
  302. */
  303. unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
  304. u32 ordinal)
  305. {
  306. int duration_idx = TPM_UNDEFINED;
  307. int duration = 0;
  308. /*
  309. * We only have a duration table for protected commands, where the upper
  310. * 16 bits are 0. For the few other ordinals the fallback will be used.
  311. */
  312. if (ordinal < TPM_MAX_ORDINAL)
  313. duration_idx = tpm_ordinal_duration[ordinal];
  314. if (duration_idx != TPM_UNDEFINED)
  315. duration = chip->duration[duration_idx];
  316. if (duration <= 0)
  317. return 2 * 60 * HZ;
  318. else
  319. return duration;
  320. }
  321. EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
  322. static int tpm_validate_command(struct tpm_chip *chip,
  323. struct tpm_space *space,
  324. const u8 *cmd,
  325. size_t len)
  326. {
  327. const struct tpm_input_header *header = (const void *)cmd;
  328. int i;
  329. u32 cc;
  330. u32 attrs;
  331. unsigned int nr_handles;
  332. if (len < TPM_HEADER_SIZE)
  333. return -EINVAL;
  334. if (!space)
  335. return 0;
  336. if (chip->flags & TPM_CHIP_FLAG_TPM2 && chip->nr_commands) {
  337. cc = be32_to_cpu(header->ordinal);
  338. i = tpm2_find_cc(chip, cc);
  339. if (i < 0) {
  340. dev_dbg(&chip->dev, "0x%04X is an invalid command\n",
  341. cc);
  342. return -EOPNOTSUPP;
  343. }
  344. attrs = chip->cc_attrs_tbl[i];
  345. nr_handles =
  346. 4 * ((attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0));
  347. if (len < TPM_HEADER_SIZE + 4 * nr_handles)
  348. goto err_len;
  349. }
  350. return 0;
  351. err_len:
  352. dev_dbg(&chip->dev,
  353. "%s: insufficient command length %zu", __func__, len);
  354. return -EINVAL;
  355. }
  356. static int tpm_request_locality(struct tpm_chip *chip)
  357. {
  358. int rc;
  359. if (!chip->ops->request_locality)
  360. return 0;
  361. rc = chip->ops->request_locality(chip, 0);
  362. if (rc < 0)
  363. return rc;
  364. chip->locality = rc;
  365. return 0;
  366. }
  367. static void tpm_relinquish_locality(struct tpm_chip *chip)
  368. {
  369. int rc;
  370. if (!chip->ops->relinquish_locality)
  371. return;
  372. rc = chip->ops->relinquish_locality(chip, chip->locality);
  373. if (rc)
  374. dev_err(&chip->dev, "%s: : error %d\n", __func__, rc);
  375. chip->locality = -1;
  376. }
  377. /**
  378. * tpm_transmit - Internal kernel interface to transmit TPM commands.
  379. *
  380. * @chip: TPM chip to use
  381. * @space: tpm space
  382. * @buf: TPM command buffer
  383. * @bufsiz: length of the TPM command buffer
  384. * @flags: tpm transmit flags - bitmap
  385. *
  386. * Return:
  387. * 0 when the operation is successful.
  388. * A negative number for system errors (errno).
  389. */
  390. ssize_t tpm_transmit(struct tpm_chip *chip, struct tpm_space *space,
  391. u8 *buf, size_t bufsiz, unsigned int flags)
  392. {
  393. struct tpm_output_header *header = (void *)buf;
  394. int rc;
  395. ssize_t len = 0;
  396. u32 count, ordinal;
  397. unsigned long stop;
  398. bool need_locality;
  399. rc = tpm_validate_command(chip, space, buf, bufsiz);
  400. if (rc == -EINVAL)
  401. return rc;
  402. /*
  403. * If the command is not implemented by the TPM, synthesize a
  404. * response with a TPM2_RC_COMMAND_CODE return for user-space.
  405. */
  406. if (rc == -EOPNOTSUPP) {
  407. header->length = cpu_to_be32(sizeof(*header));
  408. header->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
  409. header->return_code = cpu_to_be32(TPM2_RC_COMMAND_CODE |
  410. TSS2_RESMGR_TPM_RC_LAYER);
  411. return bufsiz;
  412. }
  413. if (bufsiz > TPM_BUFSIZE)
  414. bufsiz = TPM_BUFSIZE;
  415. count = be32_to_cpu(*((__be32 *) (buf + 2)));
  416. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  417. if (count == 0)
  418. return -ENODATA;
  419. if (count > bufsiz) {
  420. dev_err(&chip->dev,
  421. "invalid count value %x %zx\n", count, bufsiz);
  422. return -E2BIG;
  423. }
  424. if (!(flags & TPM_TRANSMIT_UNLOCKED))
  425. mutex_lock(&chip->tpm_mutex);
  426. if (chip->ops->clk_enable != NULL)
  427. chip->ops->clk_enable(chip, true);
  428. /* Store the decision as chip->locality will be changed. */
  429. need_locality = chip->locality == -1;
  430. if (!(flags & TPM_TRANSMIT_RAW) && need_locality) {
  431. rc = tpm_request_locality(chip);
  432. if (rc < 0)
  433. goto out_no_locality;
  434. }
  435. if (chip->dev.parent)
  436. pm_runtime_get_sync(chip->dev.parent);
  437. rc = tpm2_prepare_space(chip, space, ordinal, buf);
  438. if (rc)
  439. goto out;
  440. rc = chip->ops->send(chip, buf, count);
  441. if (rc < 0) {
  442. if (rc != -EPIPE)
  443. dev_err(&chip->dev,
  444. "%s: tpm_send: error %d\n", __func__, rc);
  445. goto out;
  446. }
  447. if (chip->flags & TPM_CHIP_FLAG_IRQ)
  448. goto out_recv;
  449. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  450. stop = jiffies + tpm2_calc_ordinal_duration(chip, ordinal);
  451. else
  452. stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
  453. do {
  454. u8 status = chip->ops->status(chip);
  455. if ((status & chip->ops->req_complete_mask) ==
  456. chip->ops->req_complete_val)
  457. goto out_recv;
  458. if (chip->ops->req_canceled(chip, status)) {
  459. dev_err(&chip->dev, "Operation Canceled\n");
  460. rc = -ECANCELED;
  461. goto out;
  462. }
  463. tpm_msleep(TPM_TIMEOUT);
  464. rmb();
  465. } while (time_before(jiffies, stop));
  466. chip->ops->cancel(chip);
  467. dev_err(&chip->dev, "Operation Timed out\n");
  468. rc = -ETIME;
  469. goto out;
  470. out_recv:
  471. len = chip->ops->recv(chip, buf, bufsiz);
  472. if (len < 0) {
  473. rc = len;
  474. dev_err(&chip->dev,
  475. "tpm_transmit: tpm_recv: error %d\n", rc);
  476. goto out;
  477. } else if (len < TPM_HEADER_SIZE) {
  478. rc = -EFAULT;
  479. goto out;
  480. }
  481. if (len != be32_to_cpu(header->length)) {
  482. rc = -EFAULT;
  483. goto out;
  484. }
  485. rc = tpm2_commit_space(chip, space, ordinal, buf, &len);
  486. out:
  487. if (chip->dev.parent)
  488. pm_runtime_put_sync(chip->dev.parent);
  489. if (need_locality)
  490. tpm_relinquish_locality(chip);
  491. out_no_locality:
  492. if (chip->ops->clk_enable != NULL)
  493. chip->ops->clk_enable(chip, false);
  494. if (!(flags & TPM_TRANSMIT_UNLOCKED))
  495. mutex_unlock(&chip->tpm_mutex);
  496. return rc ? rc : len;
  497. }
  498. /**
  499. * tpm_transmit_cmd - send a tpm command to the device
  500. * The function extracts tpm out header return code
  501. *
  502. * @chip: TPM chip to use
  503. * @space: tpm space
  504. * @buf: TPM command buffer
  505. * @bufsiz: length of the buffer
  506. * @min_rsp_body_length: minimum expected length of response body
  507. * @flags: tpm transmit flags - bitmap
  508. * @desc: command description used in the error message
  509. *
  510. * Return:
  511. * 0 when the operation is successful.
  512. * A negative number for system errors (errno).
  513. * A positive number for a TPM error.
  514. */
  515. ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_space *space,
  516. void *buf, size_t bufsiz,
  517. size_t min_rsp_body_length, unsigned int flags,
  518. const char *desc)
  519. {
  520. const struct tpm_output_header *header = buf;
  521. int err;
  522. ssize_t len;
  523. len = tpm_transmit(chip, space, buf, bufsiz, flags);
  524. if (len < 0)
  525. return len;
  526. err = be32_to_cpu(header->return_code);
  527. if (err != 0 && desc)
  528. dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
  529. desc);
  530. if (err)
  531. return err;
  532. if (len < min_rsp_body_length + TPM_HEADER_SIZE)
  533. return -EFAULT;
  534. return 0;
  535. }
  536. EXPORT_SYMBOL_GPL(tpm_transmit_cmd);
  537. #define TPM_ORD_STARTUP 153
  538. #define TPM_ST_CLEAR 1
  539. /**
  540. * tpm_startup - turn on the TPM
  541. * @chip: TPM chip to use
  542. *
  543. * Normally the firmware should start the TPM. This function is provided as a
  544. * workaround if this does not happen. A legal case for this could be for
  545. * example when a TPM emulator is used.
  546. *
  547. * Return: same as tpm_transmit_cmd()
  548. */
  549. int tpm_startup(struct tpm_chip *chip)
  550. {
  551. struct tpm_buf buf;
  552. int rc;
  553. dev_info(&chip->dev, "starting up the TPM manually\n");
  554. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  555. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP);
  556. if (rc < 0)
  557. return rc;
  558. tpm_buf_append_u16(&buf, TPM2_SU_CLEAR);
  559. } else {
  560. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_STARTUP);
  561. if (rc < 0)
  562. return rc;
  563. tpm_buf_append_u16(&buf, TPM_ST_CLEAR);
  564. }
  565. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
  566. "attempting to start the TPM");
  567. tpm_buf_destroy(&buf);
  568. return rc;
  569. }
  570. #define TPM_DIGEST_SIZE 20
  571. #define TPM_RET_CODE_IDX 6
  572. #define TPM_INTERNAL_RESULT_SIZE 200
  573. #define TPM_ORD_GET_CAP 101
  574. #define TPM_ORD_GET_RANDOM 70
  575. static const struct tpm_input_header tpm_getcap_header = {
  576. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  577. .length = cpu_to_be32(22),
  578. .ordinal = cpu_to_be32(TPM_ORD_GET_CAP)
  579. };
  580. ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
  581. const char *desc, size_t min_cap_length)
  582. {
  583. struct tpm_buf buf;
  584. int rc;
  585. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP);
  586. if (rc)
  587. return rc;
  588. if (subcap_id == TPM_CAP_VERSION_1_1 ||
  589. subcap_id == TPM_CAP_VERSION_1_2) {
  590. tpm_buf_append_u32(&buf, subcap_id);
  591. tpm_buf_append_u32(&buf, 0);
  592. } else {
  593. if (subcap_id == TPM_CAP_FLAG_PERM ||
  594. subcap_id == TPM_CAP_FLAG_VOL)
  595. tpm_buf_append_u32(&buf, TPM_CAP_FLAG);
  596. else
  597. tpm_buf_append_u32(&buf, TPM_CAP_PROP);
  598. tpm_buf_append_u32(&buf, 4);
  599. tpm_buf_append_u32(&buf, subcap_id);
  600. }
  601. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
  602. min_cap_length, 0, desc);
  603. if (!rc)
  604. *cap = *(cap_t *)&buf.data[TPM_HEADER_SIZE + 4];
  605. tpm_buf_destroy(&buf);
  606. return rc;
  607. }
  608. EXPORT_SYMBOL_GPL(tpm_getcap);
  609. int tpm_get_timeouts(struct tpm_chip *chip)
  610. {
  611. cap_t cap;
  612. unsigned long timeout_old[4], timeout_chip[4], timeout_eff[4];
  613. ssize_t rc;
  614. if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
  615. return 0;
  616. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  617. /* Fixed timeouts for TPM2 */
  618. chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
  619. chip->timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
  620. chip->timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
  621. chip->timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
  622. chip->duration[TPM_SHORT] =
  623. msecs_to_jiffies(TPM2_DURATION_SHORT);
  624. chip->duration[TPM_MEDIUM] =
  625. msecs_to_jiffies(TPM2_DURATION_MEDIUM);
  626. chip->duration[TPM_LONG] =
  627. msecs_to_jiffies(TPM2_DURATION_LONG);
  628. chip->duration[TPM_LONG_LONG] =
  629. msecs_to_jiffies(TPM2_DURATION_LONG_LONG);
  630. chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
  631. return 0;
  632. }
  633. rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap, NULL,
  634. sizeof(cap.timeout));
  635. if (rc == TPM_ERR_INVALID_POSTINIT) {
  636. if (tpm_startup(chip))
  637. return rc;
  638. rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap,
  639. "attempting to determine the timeouts",
  640. sizeof(cap.timeout));
  641. }
  642. if (rc) {
  643. dev_err(&chip->dev,
  644. "A TPM error (%zd) occurred attempting to determine the timeouts\n",
  645. rc);
  646. return rc;
  647. }
  648. timeout_old[0] = jiffies_to_usecs(chip->timeout_a);
  649. timeout_old[1] = jiffies_to_usecs(chip->timeout_b);
  650. timeout_old[2] = jiffies_to_usecs(chip->timeout_c);
  651. timeout_old[3] = jiffies_to_usecs(chip->timeout_d);
  652. timeout_chip[0] = be32_to_cpu(cap.timeout.a);
  653. timeout_chip[1] = be32_to_cpu(cap.timeout.b);
  654. timeout_chip[2] = be32_to_cpu(cap.timeout.c);
  655. timeout_chip[3] = be32_to_cpu(cap.timeout.d);
  656. memcpy(timeout_eff, timeout_chip, sizeof(timeout_eff));
  657. /*
  658. * Provide ability for vendor overrides of timeout values in case
  659. * of misreporting.
  660. */
  661. if (chip->ops->update_timeouts != NULL)
  662. chip->timeout_adjusted =
  663. chip->ops->update_timeouts(chip, timeout_eff);
  664. if (!chip->timeout_adjusted) {
  665. /* Restore default if chip reported 0 */
  666. int i;
  667. for (i = 0; i < ARRAY_SIZE(timeout_eff); i++) {
  668. if (timeout_eff[i])
  669. continue;
  670. timeout_eff[i] = timeout_old[i];
  671. chip->timeout_adjusted = true;
  672. }
  673. if (timeout_eff[0] != 0 && timeout_eff[0] < 1000) {
  674. /* timeouts in msec rather usec */
  675. for (i = 0; i != ARRAY_SIZE(timeout_eff); i++)
  676. timeout_eff[i] *= 1000;
  677. chip->timeout_adjusted = true;
  678. }
  679. }
  680. /* Report adjusted timeouts */
  681. if (chip->timeout_adjusted) {
  682. dev_info(&chip->dev,
  683. HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
  684. timeout_chip[0], timeout_eff[0],
  685. timeout_chip[1], timeout_eff[1],
  686. timeout_chip[2], timeout_eff[2],
  687. timeout_chip[3], timeout_eff[3]);
  688. }
  689. chip->timeout_a = usecs_to_jiffies(timeout_eff[0]);
  690. chip->timeout_b = usecs_to_jiffies(timeout_eff[1]);
  691. chip->timeout_c = usecs_to_jiffies(timeout_eff[2]);
  692. chip->timeout_d = usecs_to_jiffies(timeout_eff[3]);
  693. rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_DURATION, &cap,
  694. "attempting to determine the durations",
  695. sizeof(cap.duration));
  696. if (rc)
  697. return rc;
  698. chip->duration[TPM_SHORT] =
  699. usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_short));
  700. chip->duration[TPM_MEDIUM] =
  701. usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_medium));
  702. chip->duration[TPM_LONG] =
  703. usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_long));
  704. chip->duration[TPM_LONG_LONG] = 0; /* not used under 1.2 */
  705. /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
  706. * value wrong and apparently reports msecs rather than usecs. So we
  707. * fix up the resulting too-small TPM_SHORT value to make things work.
  708. * We also scale the TPM_MEDIUM and -_LONG values by 1000.
  709. */
  710. if (chip->duration[TPM_SHORT] < (HZ / 100)) {
  711. chip->duration[TPM_SHORT] = HZ;
  712. chip->duration[TPM_MEDIUM] *= 1000;
  713. chip->duration[TPM_LONG] *= 1000;
  714. chip->duration_adjusted = true;
  715. dev_info(&chip->dev, "Adjusting TPM timeout parameters.");
  716. }
  717. chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
  718. return 0;
  719. }
  720. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  721. #define TPM_ORD_CONTINUE_SELFTEST 83
  722. #define CONTINUE_SELFTEST_RESULT_SIZE 10
  723. static const struct tpm_input_header continue_selftest_header = {
  724. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  725. .length = cpu_to_be32(10),
  726. .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
  727. };
  728. /**
  729. * tpm_continue_selftest -- run TPM's selftest
  730. * @chip: TPM chip to use
  731. *
  732. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  733. * a TPM error code.
  734. */
  735. static int tpm_continue_selftest(struct tpm_chip *chip)
  736. {
  737. int rc;
  738. struct tpm_cmd_t cmd;
  739. cmd.header.in = continue_selftest_header;
  740. rc = tpm_transmit_cmd(chip, NULL, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
  741. 0, 0, "continue selftest");
  742. return rc;
  743. }
  744. #define TPM_ORDINAL_PCRREAD 21
  745. #define READ_PCR_RESULT_SIZE 30
  746. #define READ_PCR_RESULT_BODY_SIZE 20
  747. static const struct tpm_input_header pcrread_header = {
  748. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  749. .length = cpu_to_be32(14),
  750. .ordinal = cpu_to_be32(TPM_ORDINAL_PCRREAD)
  751. };
  752. int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  753. {
  754. int rc;
  755. struct tpm_cmd_t cmd;
  756. cmd.header.in = pcrread_header;
  757. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
  758. rc = tpm_transmit_cmd(chip, NULL, &cmd, READ_PCR_RESULT_SIZE,
  759. READ_PCR_RESULT_BODY_SIZE, 0,
  760. "attempting to read a pcr value");
  761. if (rc == 0)
  762. memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
  763. TPM_DIGEST_SIZE);
  764. return rc;
  765. }
  766. /**
  767. * tpm_is_tpm2 - do we a have a TPM2 chip?
  768. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  769. *
  770. * Return:
  771. * 1 if we have a TPM2 chip.
  772. * 0 if we don't have a TPM2 chip.
  773. * A negative number for system errors (errno).
  774. */
  775. int tpm_is_tpm2(struct tpm_chip *chip)
  776. {
  777. int rc;
  778. chip = tpm_chip_find_get(chip);
  779. if (!chip)
  780. return -ENODEV;
  781. rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
  782. tpm_put_ops(chip);
  783. return rc;
  784. }
  785. EXPORT_SYMBOL_GPL(tpm_is_tpm2);
  786. /**
  787. * tpm_pcr_read - read a PCR value from SHA1 bank
  788. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  789. * @pcr_idx: the PCR to be retrieved
  790. * @res_buf: the value of the PCR
  791. *
  792. * Return: same as with tpm_transmit_cmd()
  793. */
  794. int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  795. {
  796. int rc;
  797. chip = tpm_chip_find_get(chip);
  798. if (!chip)
  799. return -ENODEV;
  800. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  801. rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
  802. else
  803. rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf);
  804. tpm_put_ops(chip);
  805. return rc;
  806. }
  807. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  808. #define TPM_ORD_PCR_EXTEND 20
  809. #define EXTEND_PCR_RESULT_SIZE 34
  810. #define EXTEND_PCR_RESULT_BODY_SIZE 20
  811. static const struct tpm_input_header pcrextend_header = {
  812. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  813. .length = cpu_to_be32(34),
  814. .ordinal = cpu_to_be32(TPM_ORD_PCR_EXTEND)
  815. };
  816. static int tpm1_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash,
  817. char *log_msg)
  818. {
  819. struct tpm_buf buf;
  820. int rc;
  821. rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_EXTEND);
  822. if (rc)
  823. return rc;
  824. tpm_buf_append_u32(&buf, pcr_idx);
  825. tpm_buf_append(&buf, hash, TPM_DIGEST_SIZE);
  826. rc = tpm_transmit_cmd(chip, NULL, buf.data, EXTEND_PCR_RESULT_SIZE,
  827. EXTEND_PCR_RESULT_BODY_SIZE, 0, log_msg);
  828. tpm_buf_destroy(&buf);
  829. return rc;
  830. }
  831. /**
  832. * tpm_pcr_extend - extend a PCR value in SHA1 bank.
  833. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  834. * @pcr_idx: the PCR to be retrieved
  835. * @hash: the hash value used to extend the PCR value
  836. *
  837. * Note: with TPM 2.0 extends also those banks with a known digest size to the
  838. * cryto subsystem in order to prevent malicious use of those PCR banks. In the
  839. * future we should dynamically determine digest sizes.
  840. *
  841. * Return: same as with tpm_transmit_cmd()
  842. */
  843. int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
  844. {
  845. int rc;
  846. struct tpm2_digest digest_list[ARRAY_SIZE(chip->active_banks)];
  847. u32 count = 0;
  848. int i;
  849. chip = tpm_chip_find_get(chip);
  850. if (!chip)
  851. return -ENODEV;
  852. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  853. memset(digest_list, 0, sizeof(digest_list));
  854. for (i = 0; i < ARRAY_SIZE(chip->active_banks) &&
  855. chip->active_banks[i] != TPM2_ALG_ERROR; i++) {
  856. digest_list[i].alg_id = chip->active_banks[i];
  857. memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE);
  858. count++;
  859. }
  860. rc = tpm2_pcr_extend(chip, pcr_idx, count, digest_list);
  861. tpm_put_ops(chip);
  862. return rc;
  863. }
  864. rc = tpm1_pcr_extend(chip, pcr_idx, hash,
  865. "attempting extend a PCR value");
  866. tpm_put_ops(chip);
  867. return rc;
  868. }
  869. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  870. /**
  871. * tpm_do_selftest - have the TPM continue its selftest and wait until it
  872. * can receive further commands
  873. * @chip: TPM chip to use
  874. *
  875. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  876. * a TPM error code.
  877. */
  878. int tpm_do_selftest(struct tpm_chip *chip)
  879. {
  880. int rc;
  881. unsigned int loops;
  882. unsigned int delay_msec = 100;
  883. unsigned long duration;
  884. u8 dummy[TPM_DIGEST_SIZE];
  885. duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST);
  886. loops = jiffies_to_msecs(duration) / delay_msec;
  887. rc = tpm_continue_selftest(chip);
  888. /* This may fail if there was no TPM driver during a suspend/resume
  889. * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
  890. */
  891. if (rc)
  892. return rc;
  893. do {
  894. /* Attempt to read a PCR value */
  895. rc = tpm_pcr_read_dev(chip, 0, dummy);
  896. /* Some buggy TPMs will not respond to tpm_tis_ready() for
  897. * around 300ms while the self test is ongoing, keep trying
  898. * until the self test duration expires. */
  899. if (rc == -ETIME) {
  900. dev_info(
  901. &chip->dev, HW_ERR
  902. "TPM command timed out during continue self test");
  903. tpm_msleep(delay_msec);
  904. continue;
  905. }
  906. if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
  907. dev_info(&chip->dev,
  908. "TPM is disabled/deactivated (0x%X)\n", rc);
  909. /* TPM is disabled and/or deactivated; driver can
  910. * proceed and TPM does handle commands for
  911. * suspend/resume correctly
  912. */
  913. return 0;
  914. }
  915. if (rc != TPM_WARN_DOING_SELFTEST)
  916. return rc;
  917. tpm_msleep(delay_msec);
  918. } while (--loops > 0);
  919. return rc;
  920. }
  921. EXPORT_SYMBOL_GPL(tpm_do_selftest);
  922. /**
  923. * tpm1_auto_startup - Perform the standard automatic TPM initialization
  924. * sequence
  925. * @chip: TPM chip to use
  926. *
  927. * Returns 0 on success, < 0 in case of fatal error.
  928. */
  929. int tpm1_auto_startup(struct tpm_chip *chip)
  930. {
  931. int rc;
  932. rc = tpm_get_timeouts(chip);
  933. if (rc)
  934. goto out;
  935. rc = tpm_do_selftest(chip);
  936. if (rc) {
  937. dev_err(&chip->dev, "TPM self test failed\n");
  938. goto out;
  939. }
  940. return rc;
  941. out:
  942. if (rc > 0)
  943. rc = -ENODEV;
  944. return rc;
  945. }
  946. /**
  947. * tpm_send - send a TPM command
  948. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  949. * @cmd: a TPM command buffer
  950. * @buflen: the length of the TPM command buffer
  951. *
  952. * Return: same as with tpm_transmit_cmd()
  953. */
  954. int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
  955. {
  956. int rc;
  957. chip = tpm_chip_find_get(chip);
  958. if (!chip)
  959. return -ENODEV;
  960. rc = tpm_transmit_cmd(chip, NULL, cmd, buflen, 0, 0,
  961. "attempting to a send a command");
  962. tpm_put_ops(chip);
  963. return rc;
  964. }
  965. EXPORT_SYMBOL_GPL(tpm_send);
  966. #define TPM_ORD_SAVESTATE 152
  967. #define SAVESTATE_RESULT_SIZE 10
  968. static const struct tpm_input_header savestate_header = {
  969. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  970. .length = cpu_to_be32(10),
  971. .ordinal = cpu_to_be32(TPM_ORD_SAVESTATE)
  972. };
  973. /*
  974. * We are about to suspend. Save the TPM state
  975. * so that it can be restored.
  976. */
  977. int tpm_pm_suspend(struct device *dev)
  978. {
  979. struct tpm_chip *chip = dev_get_drvdata(dev);
  980. struct tpm_cmd_t cmd;
  981. int rc, try;
  982. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  983. if (chip == NULL)
  984. return -ENODEV;
  985. if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
  986. return 0;
  987. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  988. tpm2_shutdown(chip, TPM2_SU_STATE);
  989. return 0;
  990. }
  991. /* for buggy tpm, flush pcrs with extend to selected dummy */
  992. if (tpm_suspend_pcr)
  993. rc = tpm1_pcr_extend(chip, tpm_suspend_pcr, dummy_hash,
  994. "extending dummy pcr before suspend");
  995. /* now do the actual savestate */
  996. for (try = 0; try < TPM_RETRY; try++) {
  997. cmd.header.in = savestate_header;
  998. rc = tpm_transmit_cmd(chip, NULL, &cmd, SAVESTATE_RESULT_SIZE,
  999. 0, 0, NULL);
  1000. /*
  1001. * If the TPM indicates that it is too busy to respond to
  1002. * this command then retry before giving up. It can take
  1003. * several seconds for this TPM to be ready.
  1004. *
  1005. * This can happen if the TPM has already been sent the
  1006. * SaveState command before the driver has loaded. TCG 1.2
  1007. * specification states that any communication after SaveState
  1008. * may cause the TPM to invalidate previously saved state.
  1009. */
  1010. if (rc != TPM_WARN_RETRY)
  1011. break;
  1012. tpm_msleep(TPM_TIMEOUT_RETRY);
  1013. }
  1014. if (rc)
  1015. dev_err(&chip->dev,
  1016. "Error (%d) sending savestate before suspend\n", rc);
  1017. else if (try > 0)
  1018. dev_warn(&chip->dev, "TPM savestate took %dms\n",
  1019. try * TPM_TIMEOUT_RETRY);
  1020. return rc;
  1021. }
  1022. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  1023. /*
  1024. * Resume from a power safe. The BIOS already restored
  1025. * the TPM state.
  1026. */
  1027. int tpm_pm_resume(struct device *dev)
  1028. {
  1029. struct tpm_chip *chip = dev_get_drvdata(dev);
  1030. if (chip == NULL)
  1031. return -ENODEV;
  1032. return 0;
  1033. }
  1034. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  1035. #define TPM_GETRANDOM_RESULT_SIZE 18
  1036. static const struct tpm_input_header tpm_getrandom_header = {
  1037. .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
  1038. .length = cpu_to_be32(14),
  1039. .ordinal = cpu_to_be32(TPM_ORD_GET_RANDOM)
  1040. };
  1041. /**
  1042. * tpm_get_random() - get random bytes from the TPM's RNG
  1043. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  1044. * @out: destination buffer for the random bytes
  1045. * @max: the max number of bytes to write to @out
  1046. *
  1047. * Return: same as with tpm_transmit_cmd()
  1048. */
  1049. int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
  1050. {
  1051. struct tpm_cmd_t tpm_cmd;
  1052. u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA), rlength;
  1053. int err, total = 0, retries = 5;
  1054. u8 *dest = out;
  1055. if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
  1056. return -EINVAL;
  1057. chip = tpm_chip_find_get(chip);
  1058. if (!chip)
  1059. return -ENODEV;
  1060. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  1061. err = tpm2_get_random(chip, out, max);
  1062. tpm_put_ops(chip);
  1063. return err;
  1064. }
  1065. do {
  1066. tpm_cmd.header.in = tpm_getrandom_header;
  1067. tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
  1068. err = tpm_transmit_cmd(chip, NULL, &tpm_cmd,
  1069. TPM_GETRANDOM_RESULT_SIZE + num_bytes,
  1070. offsetof(struct tpm_getrandom_out,
  1071. rng_data),
  1072. 0, "attempting get random");
  1073. if (err)
  1074. break;
  1075. recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
  1076. if (recd > num_bytes) {
  1077. total = -EFAULT;
  1078. break;
  1079. }
  1080. rlength = be32_to_cpu(tpm_cmd.header.out.length);
  1081. if (rlength < offsetof(struct tpm_getrandom_out, rng_data) +
  1082. recd) {
  1083. total = -EFAULT;
  1084. break;
  1085. }
  1086. memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
  1087. dest += recd;
  1088. total += recd;
  1089. num_bytes -= recd;
  1090. } while (retries-- && total < max);
  1091. tpm_put_ops(chip);
  1092. return total ? total : -EIO;
  1093. }
  1094. EXPORT_SYMBOL_GPL(tpm_get_random);
  1095. /**
  1096. * tpm_seal_trusted() - seal a trusted key payload
  1097. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  1098. * @options: authentication values and other options
  1099. * @payload: the key data in clear and encrypted form
  1100. *
  1101. * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
  1102. * the keyring subsystem.
  1103. *
  1104. * Return: same as with tpm_transmit_cmd()
  1105. */
  1106. int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload,
  1107. struct trusted_key_options *options)
  1108. {
  1109. int rc;
  1110. chip = tpm_chip_find_get(chip);
  1111. if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
  1112. return -ENODEV;
  1113. rc = tpm2_seal_trusted(chip, payload, options);
  1114. tpm_put_ops(chip);
  1115. return rc;
  1116. }
  1117. EXPORT_SYMBOL_GPL(tpm_seal_trusted);
  1118. /**
  1119. * tpm_unseal_trusted() - unseal a trusted key
  1120. * @chip: a &struct tpm_chip instance, %NULL for the default chip
  1121. * @options: authentication values and other options
  1122. * @payload: the key data in clear and encrypted form
  1123. *
  1124. * Note: only TPM 2.0 chip are supported. TPM 1.x implementation is located in
  1125. * the keyring subsystem.
  1126. *
  1127. * Return: same as with tpm_transmit_cmd()
  1128. */
  1129. int tpm_unseal_trusted(struct tpm_chip *chip,
  1130. struct trusted_key_payload *payload,
  1131. struct trusted_key_options *options)
  1132. {
  1133. int rc;
  1134. chip = tpm_chip_find_get(chip);
  1135. if (!chip || !(chip->flags & TPM_CHIP_FLAG_TPM2))
  1136. return -ENODEV;
  1137. rc = tpm2_unseal_trusted(chip, payload, options);
  1138. tpm_put_ops(chip);
  1139. return rc;
  1140. }
  1141. EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
  1142. static int __init tpm_init(void)
  1143. {
  1144. int rc;
  1145. tpm_class = class_create(THIS_MODULE, "tpm");
  1146. if (IS_ERR(tpm_class)) {
  1147. pr_err("couldn't create tpm class\n");
  1148. return PTR_ERR(tpm_class);
  1149. }
  1150. tpmrm_class = class_create(THIS_MODULE, "tpmrm");
  1151. if (IS_ERR(tpmrm_class)) {
  1152. pr_err("couldn't create tpmrm class\n");
  1153. class_destroy(tpm_class);
  1154. return PTR_ERR(tpmrm_class);
  1155. }
  1156. rc = alloc_chrdev_region(&tpm_devt, 0, 2*TPM_NUM_DEVICES, "tpm");
  1157. if (rc < 0) {
  1158. pr_err("tpm: failed to allocate char dev region\n");
  1159. class_destroy(tpmrm_class);
  1160. class_destroy(tpm_class);
  1161. return rc;
  1162. }
  1163. return 0;
  1164. }
  1165. static void __exit tpm_exit(void)
  1166. {
  1167. idr_destroy(&dev_nums_idr);
  1168. class_destroy(tpm_class);
  1169. class_destroy(tpmrm_class);
  1170. unregister_chrdev_region(tpm_devt, 2*TPM_NUM_DEVICES);
  1171. }
  1172. subsys_initcall(tpm_init);
  1173. module_exit(tpm_exit);
  1174. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1175. MODULE_DESCRIPTION("TPM Driver");
  1176. MODULE_VERSION("2.0");
  1177. MODULE_LICENSE("GPL");