tpm-interface.c 31 KB

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