tpm-interface.c 34 KB

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