tpm-interface.c 33 KB

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