tpm-interface.c 31 KB

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