tpm-interface.c 34 KB

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