tpm-interface.c 31 KB

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