tpm-interface.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. * Copyright (C) 2014 Intel Corporation
  4. *
  5. * Authors:
  6. * Leendert van Doorn <leendert@watson.ibm.com>
  7. * Dave Safford <safford@watson.ibm.com>
  8. * Reiner Sailer <sailer@watson.ibm.com>
  9. * Kylene Hall <kjhall@us.ibm.com>
  10. *
  11. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  12. *
  13. * Device driver for TCG/TCPA TPM (trusted platform module).
  14. * Specifications at www.trustedcomputinggroup.org
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation, version 2 of the
  19. * License.
  20. *
  21. * Note, the TPM chip is not interrupt driven (only polling)
  22. * and can have very long timeouts (minutes!). Hence the unusual
  23. * calls to msleep.
  24. *
  25. */
  26. #include <linux/poll.h>
  27. #include <linux/slab.h>
  28. #include <linux/mutex.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/freezer.h>
  31. #include <linux/pm_runtime.h>
  32. #include "tpm.h"
  33. #include "tpm_eventlog.h"
  34. #define TPM_MAX_ORDINAL 243
  35. #define TSC_MAX_ORDINAL 12
  36. #define TPM_PROTECTED_COMMAND 0x00
  37. #define TPM_CONNECTION_COMMAND 0x40
  38. /*
  39. * Bug workaround - some TPM's don't flush the most
  40. * recently changed pcr on suspend, so force the flush
  41. * with an extend to the selected _unused_ non-volatile pcr.
  42. */
  43. static int tpm_suspend_pcr;
  44. module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
  45. MODULE_PARM_DESC(suspend_pcr,
  46. "PCR to use for dummy writes to faciltate flush on suspend.");
  47. /*
  48. * Array with one entry per ordinal defining the maximum amount
  49. * of time the chip could take to return the result. The ordinal
  50. * designation of short, medium or long is defined in a table in
  51. * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
  52. * values of the SHORT, MEDIUM, and LONG durations are retrieved
  53. * from the chip during initialization with a call to tpm_get_timeouts.
  54. */
  55. static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
  56. TPM_UNDEFINED, /* 0 */
  57. TPM_UNDEFINED,
  58. TPM_UNDEFINED,
  59. TPM_UNDEFINED,
  60. TPM_UNDEFINED,
  61. TPM_UNDEFINED, /* 5 */
  62. TPM_UNDEFINED,
  63. TPM_UNDEFINED,
  64. TPM_UNDEFINED,
  65. TPM_UNDEFINED,
  66. TPM_SHORT, /* 10 */
  67. TPM_SHORT,
  68. TPM_MEDIUM,
  69. TPM_LONG,
  70. TPM_LONG,
  71. TPM_MEDIUM, /* 15 */
  72. TPM_SHORT,
  73. TPM_SHORT,
  74. TPM_MEDIUM,
  75. TPM_LONG,
  76. TPM_SHORT, /* 20 */
  77. TPM_SHORT,
  78. TPM_MEDIUM,
  79. TPM_MEDIUM,
  80. TPM_MEDIUM,
  81. TPM_SHORT, /* 25 */
  82. TPM_SHORT,
  83. TPM_MEDIUM,
  84. TPM_SHORT,
  85. TPM_SHORT,
  86. TPM_MEDIUM, /* 30 */
  87. TPM_LONG,
  88. TPM_MEDIUM,
  89. TPM_SHORT,
  90. TPM_SHORT,
  91. TPM_SHORT, /* 35 */
  92. TPM_MEDIUM,
  93. TPM_MEDIUM,
  94. TPM_UNDEFINED,
  95. TPM_UNDEFINED,
  96. TPM_MEDIUM, /* 40 */
  97. TPM_LONG,
  98. TPM_MEDIUM,
  99. TPM_SHORT,
  100. TPM_SHORT,
  101. TPM_SHORT, /* 45 */
  102. TPM_SHORT,
  103. TPM_SHORT,
  104. TPM_SHORT,
  105. TPM_LONG,
  106. TPM_MEDIUM, /* 50 */
  107. TPM_MEDIUM,
  108. TPM_UNDEFINED,
  109. TPM_UNDEFINED,
  110. TPM_UNDEFINED,
  111. TPM_UNDEFINED, /* 55 */
  112. TPM_UNDEFINED,
  113. TPM_UNDEFINED,
  114. TPM_UNDEFINED,
  115. TPM_UNDEFINED,
  116. TPM_MEDIUM, /* 60 */
  117. TPM_MEDIUM,
  118. TPM_MEDIUM,
  119. TPM_SHORT,
  120. TPM_SHORT,
  121. TPM_MEDIUM, /* 65 */
  122. TPM_UNDEFINED,
  123. TPM_UNDEFINED,
  124. TPM_UNDEFINED,
  125. TPM_UNDEFINED,
  126. TPM_SHORT, /* 70 */
  127. TPM_SHORT,
  128. TPM_UNDEFINED,
  129. TPM_UNDEFINED,
  130. TPM_UNDEFINED,
  131. TPM_UNDEFINED, /* 75 */
  132. TPM_UNDEFINED,
  133. TPM_UNDEFINED,
  134. TPM_UNDEFINED,
  135. TPM_UNDEFINED,
  136. TPM_LONG, /* 80 */
  137. TPM_UNDEFINED,
  138. TPM_MEDIUM,
  139. TPM_LONG,
  140. TPM_SHORT,
  141. TPM_UNDEFINED, /* 85 */
  142. TPM_UNDEFINED,
  143. TPM_UNDEFINED,
  144. TPM_UNDEFINED,
  145. TPM_UNDEFINED,
  146. TPM_SHORT, /* 90 */
  147. TPM_SHORT,
  148. TPM_SHORT,
  149. TPM_SHORT,
  150. TPM_SHORT,
  151. TPM_UNDEFINED, /* 95 */
  152. TPM_UNDEFINED,
  153. TPM_UNDEFINED,
  154. TPM_UNDEFINED,
  155. TPM_UNDEFINED,
  156. TPM_MEDIUM, /* 100 */
  157. TPM_SHORT,
  158. TPM_SHORT,
  159. TPM_UNDEFINED,
  160. TPM_UNDEFINED,
  161. TPM_UNDEFINED, /* 105 */
  162. TPM_UNDEFINED,
  163. TPM_UNDEFINED,
  164. TPM_UNDEFINED,
  165. TPM_UNDEFINED,
  166. TPM_SHORT, /* 110 */
  167. TPM_SHORT,
  168. TPM_SHORT,
  169. TPM_SHORT,
  170. TPM_SHORT,
  171. TPM_SHORT, /* 115 */
  172. TPM_SHORT,
  173. TPM_SHORT,
  174. TPM_UNDEFINED,
  175. TPM_UNDEFINED,
  176. TPM_LONG, /* 120 */
  177. TPM_LONG,
  178. TPM_MEDIUM,
  179. TPM_UNDEFINED,
  180. TPM_SHORT,
  181. TPM_SHORT, /* 125 */
  182. TPM_SHORT,
  183. TPM_LONG,
  184. TPM_SHORT,
  185. TPM_SHORT,
  186. TPM_SHORT, /* 130 */
  187. TPM_MEDIUM,
  188. TPM_UNDEFINED,
  189. TPM_SHORT,
  190. TPM_MEDIUM,
  191. TPM_UNDEFINED, /* 135 */
  192. TPM_UNDEFINED,
  193. TPM_UNDEFINED,
  194. TPM_UNDEFINED,
  195. TPM_UNDEFINED,
  196. TPM_SHORT, /* 140 */
  197. TPM_SHORT,
  198. TPM_UNDEFINED,
  199. TPM_UNDEFINED,
  200. TPM_UNDEFINED,
  201. TPM_UNDEFINED, /* 145 */
  202. TPM_UNDEFINED,
  203. TPM_UNDEFINED,
  204. TPM_UNDEFINED,
  205. TPM_UNDEFINED,
  206. TPM_SHORT, /* 150 */
  207. TPM_MEDIUM,
  208. TPM_MEDIUM,
  209. TPM_SHORT,
  210. TPM_SHORT,
  211. TPM_UNDEFINED, /* 155 */
  212. TPM_UNDEFINED,
  213. TPM_UNDEFINED,
  214. TPM_UNDEFINED,
  215. TPM_UNDEFINED,
  216. TPM_SHORT, /* 160 */
  217. TPM_SHORT,
  218. TPM_SHORT,
  219. TPM_SHORT,
  220. TPM_UNDEFINED,
  221. TPM_UNDEFINED, /* 165 */
  222. TPM_UNDEFINED,
  223. TPM_UNDEFINED,
  224. TPM_UNDEFINED,
  225. TPM_UNDEFINED,
  226. TPM_LONG, /* 170 */
  227. TPM_UNDEFINED,
  228. TPM_UNDEFINED,
  229. TPM_UNDEFINED,
  230. TPM_UNDEFINED,
  231. TPM_UNDEFINED, /* 175 */
  232. TPM_UNDEFINED,
  233. TPM_UNDEFINED,
  234. TPM_UNDEFINED,
  235. TPM_UNDEFINED,
  236. TPM_MEDIUM, /* 180 */
  237. TPM_SHORT,
  238. TPM_MEDIUM,
  239. TPM_MEDIUM,
  240. TPM_MEDIUM,
  241. TPM_MEDIUM, /* 185 */
  242. TPM_SHORT,
  243. TPM_UNDEFINED,
  244. TPM_UNDEFINED,
  245. TPM_UNDEFINED,
  246. TPM_UNDEFINED, /* 190 */
  247. TPM_UNDEFINED,
  248. TPM_UNDEFINED,
  249. TPM_UNDEFINED,
  250. TPM_UNDEFINED,
  251. TPM_UNDEFINED, /* 195 */
  252. TPM_UNDEFINED,
  253. TPM_UNDEFINED,
  254. TPM_UNDEFINED,
  255. TPM_UNDEFINED,
  256. TPM_SHORT, /* 200 */
  257. TPM_UNDEFINED,
  258. TPM_UNDEFINED,
  259. TPM_UNDEFINED,
  260. TPM_SHORT,
  261. TPM_SHORT, /* 205 */
  262. TPM_SHORT,
  263. TPM_SHORT,
  264. TPM_SHORT,
  265. TPM_SHORT,
  266. TPM_MEDIUM, /* 210 */
  267. TPM_UNDEFINED,
  268. TPM_MEDIUM,
  269. TPM_MEDIUM,
  270. TPM_MEDIUM,
  271. TPM_UNDEFINED, /* 215 */
  272. TPM_MEDIUM,
  273. TPM_UNDEFINED,
  274. TPM_UNDEFINED,
  275. TPM_SHORT,
  276. TPM_SHORT, /* 220 */
  277. TPM_SHORT,
  278. TPM_SHORT,
  279. TPM_SHORT,
  280. TPM_SHORT,
  281. TPM_UNDEFINED, /* 225 */
  282. TPM_UNDEFINED,
  283. TPM_UNDEFINED,
  284. TPM_UNDEFINED,
  285. TPM_UNDEFINED,
  286. TPM_SHORT, /* 230 */
  287. TPM_LONG,
  288. TPM_MEDIUM,
  289. TPM_UNDEFINED,
  290. TPM_UNDEFINED,
  291. TPM_UNDEFINED, /* 235 */
  292. TPM_UNDEFINED,
  293. TPM_UNDEFINED,
  294. TPM_UNDEFINED,
  295. TPM_UNDEFINED,
  296. TPM_SHORT, /* 240 */
  297. TPM_UNDEFINED,
  298. TPM_MEDIUM,
  299. };
  300. /*
  301. * Returns max number of jiffies to wait
  302. */
  303. unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
  304. u32 ordinal)
  305. {
  306. int duration_idx = TPM_UNDEFINED;
  307. int duration = 0;
  308. /*
  309. * We only have a duration table for protected commands, where the upper
  310. * 16 bits are 0. For the few other ordinals the fallback will be used.
  311. */
  312. if (ordinal < TPM_MAX_ORDINAL)
  313. duration_idx = tpm_ordinal_duration[ordinal];
  314. if (duration_idx != TPM_UNDEFINED)
  315. duration = chip->duration[duration_idx];
  316. if (duration <= 0)
  317. return 2 * 60 * HZ;
  318. else
  319. return duration;
  320. }
  321. EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
  322. /*
  323. * Internal kernel interface to transmit TPM commands
  324. */
  325. ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz,
  326. unsigned int flags)
  327. {
  328. ssize_t rc;
  329. u32 count, ordinal;
  330. unsigned long stop;
  331. if (bufsiz < TPM_HEADER_SIZE)
  332. return -EINVAL;
  333. if (bufsiz > TPM_BUFSIZE)
  334. bufsiz = TPM_BUFSIZE;
  335. count = be32_to_cpu(*((__be32 *) (buf + 2)));
  336. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  337. if (count == 0)
  338. return -ENODATA;
  339. if (count > bufsiz) {
  340. dev_err(&chip->dev,
  341. "invalid count value %x %zx\n", count, bufsiz);
  342. return -E2BIG;
  343. }
  344. if (!(flags & TPM_TRANSMIT_UNLOCKED))
  345. mutex_lock(&chip->tpm_mutex);
  346. if (chip->dev.parent)
  347. pm_runtime_get_sync(chip->dev.parent);
  348. rc = chip->ops->send(chip, (u8 *) buf, count);
  349. if (rc < 0) {
  350. dev_err(&chip->dev,
  351. "tpm_transmit: tpm_send: error %zd\n", rc);
  352. goto out;
  353. }
  354. if (chip->flags & TPM_CHIP_FLAG_IRQ)
  355. goto out_recv;
  356. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  357. stop = jiffies + tpm2_calc_ordinal_duration(chip, ordinal);
  358. else
  359. stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
  360. do {
  361. u8 status = chip->ops->status(chip);
  362. if ((status & chip->ops->req_complete_mask) ==
  363. chip->ops->req_complete_val)
  364. goto out_recv;
  365. if (chip->ops->req_canceled(chip, status)) {
  366. dev_err(&chip->dev, "Operation Canceled\n");
  367. rc = -ECANCELED;
  368. goto out;
  369. }
  370. msleep(TPM_TIMEOUT); /* CHECK */
  371. rmb();
  372. } while (time_before(jiffies, stop));
  373. chip->ops->cancel(chip);
  374. dev_err(&chip->dev, "Operation Timed out\n");
  375. rc = -ETIME;
  376. goto out;
  377. out_recv:
  378. rc = chip->ops->recv(chip, (u8 *) buf, bufsiz);
  379. if (rc < 0)
  380. dev_err(&chip->dev,
  381. "tpm_transmit: tpm_recv: error %zd\n", rc);
  382. out:
  383. if (chip->dev.parent)
  384. pm_runtime_put_sync(chip->dev.parent);
  385. if (!(flags & TPM_TRANSMIT_UNLOCKED))
  386. mutex_unlock(&chip->tpm_mutex);
  387. return rc;
  388. }
  389. #define TPM_DIGEST_SIZE 20
  390. #define TPM_RET_CODE_IDX 6
  391. ssize_t tpm_transmit_cmd(struct tpm_chip *chip, const void *cmd,
  392. int len, unsigned int flags, const char *desc)
  393. {
  394. const struct tpm_output_header *header;
  395. int err;
  396. len = tpm_transmit(chip, (const u8 *)cmd, len, flags);
  397. if (len < 0)
  398. return len;
  399. else if (len < TPM_HEADER_SIZE)
  400. return -EFAULT;
  401. header = cmd;
  402. err = be32_to_cpu(header->return_code);
  403. if (err != 0 && desc)
  404. dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
  405. desc);
  406. return err;
  407. }
  408. #define TPM_INTERNAL_RESULT_SIZE 200
  409. #define TPM_ORD_GET_CAP cpu_to_be32(101)
  410. #define TPM_ORD_GET_RANDOM cpu_to_be32(70)
  411. static const struct tpm_input_header tpm_getcap_header = {
  412. .tag = TPM_TAG_RQU_COMMAND,
  413. .length = cpu_to_be32(22),
  414. .ordinal = TPM_ORD_GET_CAP
  415. };
  416. ssize_t tpm_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap,
  417. const char *desc)
  418. {
  419. struct tpm_cmd_t tpm_cmd;
  420. int rc;
  421. tpm_cmd.header.in = tpm_getcap_header;
  422. if (subcap_id == TPM_CAP_VERSION_1_1 ||
  423. subcap_id == TPM_CAP_VERSION_1_2) {
  424. tpm_cmd.params.getcap_in.cap = cpu_to_be32(subcap_id);
  425. /*subcap field not necessary */
  426. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
  427. tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
  428. } else {
  429. if (subcap_id == TPM_CAP_FLAG_PERM ||
  430. subcap_id == TPM_CAP_FLAG_VOL)
  431. tpm_cmd.params.getcap_in.cap =
  432. cpu_to_be32(TPM_CAP_FLAG);
  433. else
  434. tpm_cmd.params.getcap_in.cap =
  435. cpu_to_be32(TPM_CAP_PROP);
  436. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  437. tpm_cmd.params.getcap_in.subcap = cpu_to_be32(subcap_id);
  438. }
  439. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 0,
  440. desc);
  441. if (!rc)
  442. *cap = tpm_cmd.params.getcap_out.cap;
  443. return rc;
  444. }
  445. EXPORT_SYMBOL_GPL(tpm_getcap);
  446. #define TPM_ORD_STARTUP cpu_to_be32(153)
  447. #define TPM_ST_CLEAR cpu_to_be16(1)
  448. #define TPM_ST_STATE cpu_to_be16(2)
  449. #define TPM_ST_DEACTIVATED cpu_to_be16(3)
  450. static const struct tpm_input_header tpm_startup_header = {
  451. .tag = TPM_TAG_RQU_COMMAND,
  452. .length = cpu_to_be32(12),
  453. .ordinal = TPM_ORD_STARTUP
  454. };
  455. static int tpm_startup(struct tpm_chip *chip, __be16 startup_type)
  456. {
  457. struct tpm_cmd_t start_cmd;
  458. start_cmd.header.in = tpm_startup_header;
  459. start_cmd.params.startup_in.startup_type = startup_type;
  460. return tpm_transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE, 0,
  461. "attempting to start the TPM");
  462. }
  463. int tpm_get_timeouts(struct tpm_chip *chip)
  464. {
  465. cap_t cap;
  466. unsigned long new_timeout[4];
  467. unsigned long old_timeout[4];
  468. ssize_t rc;
  469. if (chip->flags & TPM_CHIP_FLAG_HAVE_TIMEOUTS)
  470. return 0;
  471. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  472. /* Fixed timeouts for TPM2 */
  473. chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
  474. chip->timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
  475. chip->timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
  476. chip->timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);
  477. chip->duration[TPM_SHORT] =
  478. msecs_to_jiffies(TPM2_DURATION_SHORT);
  479. chip->duration[TPM_MEDIUM] =
  480. msecs_to_jiffies(TPM2_DURATION_MEDIUM);
  481. chip->duration[TPM_LONG] =
  482. msecs_to_jiffies(TPM2_DURATION_LONG);
  483. chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
  484. return 0;
  485. }
  486. rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap,
  487. "attempting to determine the timeouts");
  488. if (rc == TPM_ERR_INVALID_POSTINIT) {
  489. /* The TPM is not started, we are the first to talk to it.
  490. Execute a startup command. */
  491. dev_info(&chip->dev, "Issuing TPM_STARTUP\n");
  492. if (tpm_startup(chip, TPM_ST_CLEAR))
  493. return rc;
  494. rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_TIMEOUT, &cap,
  495. "attempting to determine the timeouts");
  496. }
  497. if (rc)
  498. return rc;
  499. old_timeout[0] = be32_to_cpu(cap.timeout.a);
  500. old_timeout[1] = be32_to_cpu(cap.timeout.b);
  501. old_timeout[2] = be32_to_cpu(cap.timeout.c);
  502. old_timeout[3] = be32_to_cpu(cap.timeout.d);
  503. memcpy(new_timeout, old_timeout, sizeof(new_timeout));
  504. /*
  505. * Provide ability for vendor overrides of timeout values in case
  506. * of misreporting.
  507. */
  508. if (chip->ops->update_timeouts != NULL)
  509. chip->timeout_adjusted =
  510. chip->ops->update_timeouts(chip, new_timeout);
  511. if (!chip->timeout_adjusted) {
  512. /* Don't overwrite default if value is 0 */
  513. if (new_timeout[0] != 0 && new_timeout[0] < 1000) {
  514. int i;
  515. /* timeouts in msec rather usec */
  516. for (i = 0; i != ARRAY_SIZE(new_timeout); i++)
  517. new_timeout[i] *= 1000;
  518. chip->timeout_adjusted = true;
  519. }
  520. }
  521. /* Report adjusted timeouts */
  522. if (chip->timeout_adjusted) {
  523. dev_info(&chip->dev,
  524. HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
  525. old_timeout[0], new_timeout[0],
  526. old_timeout[1], new_timeout[1],
  527. old_timeout[2], new_timeout[2],
  528. old_timeout[3], new_timeout[3]);
  529. }
  530. chip->timeout_a = usecs_to_jiffies(new_timeout[0]);
  531. chip->timeout_b = usecs_to_jiffies(new_timeout[1]);
  532. chip->timeout_c = usecs_to_jiffies(new_timeout[2]);
  533. chip->timeout_d = usecs_to_jiffies(new_timeout[3]);
  534. rc = tpm_getcap(chip, TPM_CAP_PROP_TIS_DURATION, &cap,
  535. "attempting to determine the durations");
  536. if (rc)
  537. return rc;
  538. chip->duration[TPM_SHORT] =
  539. usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_short));
  540. chip->duration[TPM_MEDIUM] =
  541. usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_medium));
  542. chip->duration[TPM_LONG] =
  543. usecs_to_jiffies(be32_to_cpu(cap.duration.tpm_long));
  544. /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
  545. * value wrong and apparently reports msecs rather than usecs. So we
  546. * fix up the resulting too-small TPM_SHORT value to make things work.
  547. * We also scale the TPM_MEDIUM and -_LONG values by 1000.
  548. */
  549. if (chip->duration[TPM_SHORT] < (HZ / 100)) {
  550. chip->duration[TPM_SHORT] = HZ;
  551. chip->duration[TPM_MEDIUM] *= 1000;
  552. chip->duration[TPM_LONG] *= 1000;
  553. chip->duration_adjusted = true;
  554. dev_info(&chip->dev, "Adjusting TPM timeout parameters.");
  555. }
  556. chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
  557. return 0;
  558. }
  559. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  560. #define TPM_ORD_CONTINUE_SELFTEST 83
  561. #define CONTINUE_SELFTEST_RESULT_SIZE 10
  562. static const struct tpm_input_header continue_selftest_header = {
  563. .tag = TPM_TAG_RQU_COMMAND,
  564. .length = cpu_to_be32(10),
  565. .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
  566. };
  567. /**
  568. * tpm_continue_selftest -- run TPM's selftest
  569. * @chip: TPM chip to use
  570. *
  571. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  572. * a TPM error code.
  573. */
  574. static int tpm_continue_selftest(struct tpm_chip *chip)
  575. {
  576. int rc;
  577. struct tpm_cmd_t cmd;
  578. cmd.header.in = continue_selftest_header;
  579. rc = tpm_transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE, 0,
  580. "continue selftest");
  581. return rc;
  582. }
  583. #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
  584. #define READ_PCR_RESULT_SIZE 30
  585. static const struct tpm_input_header pcrread_header = {
  586. .tag = TPM_TAG_RQU_COMMAND,
  587. .length = cpu_to_be32(14),
  588. .ordinal = TPM_ORDINAL_PCRREAD
  589. };
  590. int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  591. {
  592. int rc;
  593. struct tpm_cmd_t cmd;
  594. cmd.header.in = pcrread_header;
  595. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
  596. rc = tpm_transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE, 0,
  597. "attempting to read a pcr value");
  598. if (rc == 0)
  599. memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
  600. TPM_DIGEST_SIZE);
  601. return rc;
  602. }
  603. /**
  604. * tpm_is_tpm2 - is the chip a TPM2 chip?
  605. * @chip_num: tpm idx # or ANY
  606. *
  607. * Returns < 0 on error, and 1 or 0 on success depending whether the chip
  608. * is a TPM2 chip.
  609. */
  610. int tpm_is_tpm2(u32 chip_num)
  611. {
  612. struct tpm_chip *chip;
  613. int rc;
  614. chip = tpm_chip_find_get(chip_num);
  615. if (chip == NULL)
  616. return -ENODEV;
  617. rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
  618. tpm_put_ops(chip);
  619. return rc;
  620. }
  621. EXPORT_SYMBOL_GPL(tpm_is_tpm2);
  622. /**
  623. * tpm_pcr_read - read a pcr value
  624. * @chip_num: tpm idx # or ANY
  625. * @pcr_idx: pcr idx to retrieve
  626. * @res_buf: TPM_PCR value
  627. * size of res_buf is 20 bytes (or NULL if you don't care)
  628. *
  629. * The TPM driver should be built-in, but for whatever reason it
  630. * isn't, protect against the chip disappearing, by incrementing
  631. * the module usage count.
  632. */
  633. int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
  634. {
  635. struct tpm_chip *chip;
  636. int rc;
  637. chip = tpm_chip_find_get(chip_num);
  638. if (chip == NULL)
  639. return -ENODEV;
  640. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  641. rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
  642. else
  643. rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf);
  644. tpm_put_ops(chip);
  645. return rc;
  646. }
  647. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  648. #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
  649. #define EXTEND_PCR_RESULT_SIZE 34
  650. static const struct tpm_input_header pcrextend_header = {
  651. .tag = TPM_TAG_RQU_COMMAND,
  652. .length = cpu_to_be32(34),
  653. .ordinal = TPM_ORD_PCR_EXTEND
  654. };
  655. /**
  656. * tpm_pcr_extend - extend pcr value with hash
  657. * @chip_num: tpm idx # or AN&
  658. * @pcr_idx: pcr idx to extend
  659. * @hash: hash value used to extend pcr value
  660. *
  661. * The TPM driver should be built-in, but for whatever reason it
  662. * isn't, protect against the chip disappearing, by incrementing
  663. * the module usage count.
  664. */
  665. int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
  666. {
  667. struct tpm_cmd_t cmd;
  668. int rc;
  669. struct tpm_chip *chip;
  670. chip = tpm_chip_find_get(chip_num);
  671. if (chip == NULL)
  672. return -ENODEV;
  673. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  674. rc = tpm2_pcr_extend(chip, pcr_idx, hash);
  675. tpm_put_ops(chip);
  676. return rc;
  677. }
  678. cmd.header.in = pcrextend_header;
  679. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  680. memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
  681. rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 0,
  682. "attempting extend a PCR value");
  683. tpm_put_ops(chip);
  684. return rc;
  685. }
  686. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  687. /**
  688. * tpm_do_selftest - have the TPM continue its selftest and wait until it
  689. * can receive further commands
  690. * @chip: TPM chip to use
  691. *
  692. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  693. * a TPM error code.
  694. */
  695. int tpm_do_selftest(struct tpm_chip *chip)
  696. {
  697. int rc;
  698. unsigned int loops;
  699. unsigned int delay_msec = 100;
  700. unsigned long duration;
  701. u8 dummy[TPM_DIGEST_SIZE];
  702. duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST);
  703. loops = jiffies_to_msecs(duration) / delay_msec;
  704. rc = tpm_continue_selftest(chip);
  705. /* This may fail if there was no TPM driver during a suspend/resume
  706. * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
  707. */
  708. if (rc)
  709. return rc;
  710. do {
  711. /* Attempt to read a PCR value */
  712. rc = tpm_pcr_read_dev(chip, 0, dummy);
  713. /* Some buggy TPMs will not respond to tpm_tis_ready() for
  714. * around 300ms while the self test is ongoing, keep trying
  715. * until the self test duration expires. */
  716. if (rc == -ETIME) {
  717. dev_info(
  718. &chip->dev, HW_ERR
  719. "TPM command timed out during continue self test");
  720. msleep(delay_msec);
  721. continue;
  722. }
  723. if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
  724. dev_info(&chip->dev,
  725. "TPM is disabled/deactivated (0x%X)\n", rc);
  726. /* TPM is disabled and/or deactivated; driver can
  727. * proceed and TPM does handle commands for
  728. * suspend/resume correctly
  729. */
  730. return 0;
  731. }
  732. if (rc != TPM_WARN_DOING_SELFTEST)
  733. return rc;
  734. msleep(delay_msec);
  735. } while (--loops > 0);
  736. return rc;
  737. }
  738. EXPORT_SYMBOL_GPL(tpm_do_selftest);
  739. /**
  740. * tpm1_auto_startup - Perform the standard automatic TPM initialization
  741. * sequence
  742. * @chip: TPM chip to use
  743. *
  744. * Returns 0 on success, < 0 in case of fatal error.
  745. */
  746. int tpm1_auto_startup(struct tpm_chip *chip)
  747. {
  748. int rc;
  749. rc = tpm_get_timeouts(chip);
  750. if (rc)
  751. goto out;
  752. rc = tpm_do_selftest(chip);
  753. if (rc) {
  754. dev_err(&chip->dev, "TPM self test failed\n");
  755. goto out;
  756. }
  757. return rc;
  758. out:
  759. if (rc > 0)
  760. rc = -ENODEV;
  761. return rc;
  762. }
  763. int tpm_send(u32 chip_num, void *cmd, size_t buflen)
  764. {
  765. struct tpm_chip *chip;
  766. int rc;
  767. chip = tpm_chip_find_get(chip_num);
  768. if (chip == NULL)
  769. return -ENODEV;
  770. rc = tpm_transmit_cmd(chip, cmd, buflen, 0, "attempting tpm_cmd");
  771. tpm_put_ops(chip);
  772. return rc;
  773. }
  774. EXPORT_SYMBOL_GPL(tpm_send);
  775. static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
  776. bool check_cancel, bool *canceled)
  777. {
  778. u8 status = chip->ops->status(chip);
  779. *canceled = false;
  780. if ((status & mask) == mask)
  781. return true;
  782. if (check_cancel && chip->ops->req_canceled(chip, status)) {
  783. *canceled = true;
  784. return true;
  785. }
  786. return false;
  787. }
  788. int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  789. wait_queue_head_t *queue, bool check_cancel)
  790. {
  791. unsigned long stop;
  792. long rc;
  793. u8 status;
  794. bool canceled = false;
  795. /* check current status */
  796. status = chip->ops->status(chip);
  797. if ((status & mask) == mask)
  798. return 0;
  799. stop = jiffies + timeout;
  800. if (chip->flags & TPM_CHIP_FLAG_IRQ) {
  801. again:
  802. timeout = stop - jiffies;
  803. if ((long)timeout <= 0)
  804. return -ETIME;
  805. rc = wait_event_interruptible_timeout(*queue,
  806. wait_for_tpm_stat_cond(chip, mask, check_cancel,
  807. &canceled),
  808. timeout);
  809. if (rc > 0) {
  810. if (canceled)
  811. return -ECANCELED;
  812. return 0;
  813. }
  814. if (rc == -ERESTARTSYS && freezing(current)) {
  815. clear_thread_flag(TIF_SIGPENDING);
  816. goto again;
  817. }
  818. } else {
  819. do {
  820. msleep(TPM_TIMEOUT);
  821. status = chip->ops->status(chip);
  822. if ((status & mask) == mask)
  823. return 0;
  824. } while (time_before(jiffies, stop));
  825. }
  826. return -ETIME;
  827. }
  828. EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
  829. #define TPM_ORD_SAVESTATE cpu_to_be32(152)
  830. #define SAVESTATE_RESULT_SIZE 10
  831. static const struct tpm_input_header savestate_header = {
  832. .tag = TPM_TAG_RQU_COMMAND,
  833. .length = cpu_to_be32(10),
  834. .ordinal = TPM_ORD_SAVESTATE
  835. };
  836. /*
  837. * We are about to suspend. Save the TPM state
  838. * so that it can be restored.
  839. */
  840. int tpm_pm_suspend(struct device *dev)
  841. {
  842. struct tpm_chip *chip = dev_get_drvdata(dev);
  843. struct tpm_cmd_t cmd;
  844. int rc, try;
  845. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  846. if (chip == NULL)
  847. return -ENODEV;
  848. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  849. tpm2_shutdown(chip, TPM2_SU_STATE);
  850. return 0;
  851. }
  852. /* for buggy tpm, flush pcrs with extend to selected dummy */
  853. if (tpm_suspend_pcr) {
  854. cmd.header.in = pcrextend_header;
  855. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
  856. memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
  857. TPM_DIGEST_SIZE);
  858. rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 0,
  859. "extending dummy pcr before suspend");
  860. }
  861. /* now do the actual savestate */
  862. for (try = 0; try < TPM_RETRY; try++) {
  863. cmd.header.in = savestate_header;
  864. rc = tpm_transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, 0,
  865. NULL);
  866. /*
  867. * If the TPM indicates that it is too busy to respond to
  868. * this command then retry before giving up. It can take
  869. * several seconds for this TPM to be ready.
  870. *
  871. * This can happen if the TPM has already been sent the
  872. * SaveState command before the driver has loaded. TCG 1.2
  873. * specification states that any communication after SaveState
  874. * may cause the TPM to invalidate previously saved state.
  875. */
  876. if (rc != TPM_WARN_RETRY)
  877. break;
  878. msleep(TPM_TIMEOUT_RETRY);
  879. }
  880. if (rc)
  881. dev_err(&chip->dev,
  882. "Error (%d) sending savestate before suspend\n", rc);
  883. else if (try > 0)
  884. dev_warn(&chip->dev, "TPM savestate took %dms\n",
  885. try * TPM_TIMEOUT_RETRY);
  886. return rc;
  887. }
  888. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  889. /*
  890. * Resume from a power safe. The BIOS already restored
  891. * the TPM state.
  892. */
  893. int tpm_pm_resume(struct device *dev)
  894. {
  895. struct tpm_chip *chip = dev_get_drvdata(dev);
  896. if (chip == NULL)
  897. return -ENODEV;
  898. return 0;
  899. }
  900. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  901. #define TPM_GETRANDOM_RESULT_SIZE 18
  902. static const struct tpm_input_header tpm_getrandom_header = {
  903. .tag = TPM_TAG_RQU_COMMAND,
  904. .length = cpu_to_be32(14),
  905. .ordinal = TPM_ORD_GET_RANDOM
  906. };
  907. /**
  908. * tpm_get_random() - Get random bytes from the tpm's RNG
  909. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  910. * @out: destination buffer for the random bytes
  911. * @max: the max number of bytes to write to @out
  912. *
  913. * Returns < 0 on error and the number of bytes read on success
  914. */
  915. int tpm_get_random(u32 chip_num, u8 *out, size_t max)
  916. {
  917. struct tpm_chip *chip;
  918. struct tpm_cmd_t tpm_cmd;
  919. u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
  920. int err, total = 0, retries = 5;
  921. u8 *dest = out;
  922. if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
  923. return -EINVAL;
  924. chip = tpm_chip_find_get(chip_num);
  925. if (chip == NULL)
  926. return -ENODEV;
  927. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  928. err = tpm2_get_random(chip, out, max);
  929. tpm_put_ops(chip);
  930. return err;
  931. }
  932. do {
  933. tpm_cmd.header.in = tpm_getrandom_header;
  934. tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
  935. err = tpm_transmit_cmd(chip, &tpm_cmd,
  936. TPM_GETRANDOM_RESULT_SIZE + num_bytes,
  937. 0, "attempting get random");
  938. if (err)
  939. break;
  940. recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
  941. memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
  942. dest += recd;
  943. total += recd;
  944. num_bytes -= recd;
  945. } while (retries-- && total < max);
  946. tpm_put_ops(chip);
  947. return total ? total : -EIO;
  948. }
  949. EXPORT_SYMBOL_GPL(tpm_get_random);
  950. /**
  951. * tpm_seal_trusted() - seal a trusted key
  952. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  953. * @options: authentication values and other options
  954. * @payload: the key data in clear and encrypted form
  955. *
  956. * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips
  957. * are supported.
  958. */
  959. int tpm_seal_trusted(u32 chip_num, struct trusted_key_payload *payload,
  960. struct trusted_key_options *options)
  961. {
  962. struct tpm_chip *chip;
  963. int rc;
  964. chip = tpm_chip_find_get(chip_num);
  965. if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2))
  966. return -ENODEV;
  967. rc = tpm2_seal_trusted(chip, payload, options);
  968. tpm_put_ops(chip);
  969. return rc;
  970. }
  971. EXPORT_SYMBOL_GPL(tpm_seal_trusted);
  972. /**
  973. * tpm_unseal_trusted() - unseal a trusted key
  974. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  975. * @options: authentication values and other options
  976. * @payload: the key data in clear and encrypted form
  977. *
  978. * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips
  979. * are supported.
  980. */
  981. int tpm_unseal_trusted(u32 chip_num, struct trusted_key_payload *payload,
  982. struct trusted_key_options *options)
  983. {
  984. struct tpm_chip *chip;
  985. int rc;
  986. chip = tpm_chip_find_get(chip_num);
  987. if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2))
  988. return -ENODEV;
  989. rc = tpm2_unseal_trusted(chip, payload, options);
  990. tpm_put_ops(chip);
  991. return rc;
  992. }
  993. EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
  994. static int __init tpm_init(void)
  995. {
  996. int rc;
  997. tpm_class = class_create(THIS_MODULE, "tpm");
  998. if (IS_ERR(tpm_class)) {
  999. pr_err("couldn't create tpm class\n");
  1000. return PTR_ERR(tpm_class);
  1001. }
  1002. rc = alloc_chrdev_region(&tpm_devt, 0, TPM_NUM_DEVICES, "tpm");
  1003. if (rc < 0) {
  1004. pr_err("tpm: failed to allocate char dev region\n");
  1005. class_destroy(tpm_class);
  1006. return rc;
  1007. }
  1008. return 0;
  1009. }
  1010. static void __exit tpm_exit(void)
  1011. {
  1012. idr_destroy(&dev_nums_idr);
  1013. class_destroy(tpm_class);
  1014. unregister_chrdev_region(tpm_devt, TPM_NUM_DEVICES);
  1015. }
  1016. subsys_initcall(tpm_init);
  1017. module_exit(tpm_exit);
  1018. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1019. MODULE_DESCRIPTION("TPM Driver");
  1020. MODULE_VERSION("2.0");
  1021. MODULE_LICENSE("GPL");