tpm-interface.c 28 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 "tpm.h"
  32. #include "tpm_eventlog.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 faciltate 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. u8 category = (ordinal >> 24) & 0xFF;
  308. if ((category == TPM_PROTECTED_COMMAND && ordinal < TPM_MAX_ORDINAL) ||
  309. (category == TPM_CONNECTION_COMMAND && ordinal < TSC_MAX_ORDINAL))
  310. duration_idx = tpm_ordinal_duration[ordinal];
  311. if (duration_idx != TPM_UNDEFINED)
  312. duration = chip->vendor.duration[duration_idx];
  313. if (duration <= 0)
  314. return 2 * 60 * HZ;
  315. else
  316. return duration;
  317. }
  318. EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
  319. /*
  320. * Internal kernel interface to transmit TPM commands
  321. */
  322. ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz,
  323. unsigned int flags)
  324. {
  325. ssize_t rc;
  326. u32 count, ordinal;
  327. unsigned long stop;
  328. if (bufsiz > TPM_BUFSIZE)
  329. bufsiz = TPM_BUFSIZE;
  330. count = be32_to_cpu(*((__be32 *) (buf + 2)));
  331. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  332. if (count == 0)
  333. return -ENODATA;
  334. if (count > bufsiz) {
  335. dev_err(&chip->dev,
  336. "invalid count value %x %zx\n", count, bufsiz);
  337. return -E2BIG;
  338. }
  339. if (!(flags & TPM_TRANSMIT_UNLOCKED))
  340. mutex_lock(&chip->tpm_mutex);
  341. rc = chip->ops->send(chip, (u8 *) buf, count);
  342. if (rc < 0) {
  343. dev_err(&chip->dev,
  344. "tpm_transmit: tpm_send: error %zd\n", rc);
  345. goto out;
  346. }
  347. if (chip->vendor.irq)
  348. goto out_recv;
  349. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  350. stop = jiffies + tpm2_calc_ordinal_duration(chip, ordinal);
  351. else
  352. stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
  353. do {
  354. u8 status = chip->ops->status(chip);
  355. if ((status & chip->ops->req_complete_mask) ==
  356. chip->ops->req_complete_val)
  357. goto out_recv;
  358. if (chip->ops->req_canceled(chip, status)) {
  359. dev_err(&chip->dev, "Operation Canceled\n");
  360. rc = -ECANCELED;
  361. goto out;
  362. }
  363. msleep(TPM_TIMEOUT); /* CHECK */
  364. rmb();
  365. } while (time_before(jiffies, stop));
  366. chip->ops->cancel(chip);
  367. dev_err(&chip->dev, "Operation Timed out\n");
  368. rc = -ETIME;
  369. goto out;
  370. out_recv:
  371. rc = chip->ops->recv(chip, (u8 *) buf, bufsiz);
  372. if (rc < 0)
  373. dev_err(&chip->dev,
  374. "tpm_transmit: tpm_recv: error %zd\n", rc);
  375. out:
  376. if (!(flags & TPM_TRANSMIT_UNLOCKED))
  377. mutex_unlock(&chip->tpm_mutex);
  378. return rc;
  379. }
  380. #define TPM_DIGEST_SIZE 20
  381. #define TPM_RET_CODE_IDX 6
  382. ssize_t tpm_transmit_cmd(struct tpm_chip *chip, const void *cmd,
  383. int len, unsigned int flags, const char *desc)
  384. {
  385. const struct tpm_output_header *header;
  386. int err;
  387. len = tpm_transmit(chip, (const u8 *)cmd, len, flags);
  388. if (len < 0)
  389. return len;
  390. else if (len < TPM_HEADER_SIZE)
  391. return -EFAULT;
  392. header = cmd;
  393. err = be32_to_cpu(header->return_code);
  394. if (err != 0 && err != TPM_ERR_DISABLED && err != TPM_ERR_DEACTIVATED
  395. && desc)
  396. dev_err(&chip->dev, "A TPM error (%d) occurred %s\n", err,
  397. desc);
  398. return err;
  399. }
  400. #define TPM_INTERNAL_RESULT_SIZE 200
  401. #define TPM_ORD_GET_CAP cpu_to_be32(101)
  402. #define TPM_ORD_GET_RANDOM cpu_to_be32(70)
  403. static const struct tpm_input_header tpm_getcap_header = {
  404. .tag = TPM_TAG_RQU_COMMAND,
  405. .length = cpu_to_be32(22),
  406. .ordinal = TPM_ORD_GET_CAP
  407. };
  408. ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
  409. const char *desc)
  410. {
  411. struct tpm_cmd_t tpm_cmd;
  412. int rc;
  413. struct tpm_chip *chip = dev_get_drvdata(dev);
  414. tpm_cmd.header.in = tpm_getcap_header;
  415. if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
  416. tpm_cmd.params.getcap_in.cap = subcap_id;
  417. /*subcap field not necessary */
  418. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
  419. tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
  420. } else {
  421. if (subcap_id == TPM_CAP_FLAG_PERM ||
  422. subcap_id == TPM_CAP_FLAG_VOL)
  423. tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
  424. else
  425. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  426. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  427. tpm_cmd.params.getcap_in.subcap = subcap_id;
  428. }
  429. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 0,
  430. desc);
  431. if (!rc)
  432. *cap = tpm_cmd.params.getcap_out.cap;
  433. return rc;
  434. }
  435. void tpm_gen_interrupt(struct tpm_chip *chip)
  436. {
  437. struct tpm_cmd_t tpm_cmd;
  438. ssize_t rc;
  439. tpm_cmd.header.in = tpm_getcap_header;
  440. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  441. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  442. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  443. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 0,
  444. "attempting to determine the timeouts");
  445. }
  446. EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
  447. #define TPM_ORD_STARTUP cpu_to_be32(153)
  448. #define TPM_ST_CLEAR cpu_to_be16(1)
  449. #define TPM_ST_STATE cpu_to_be16(2)
  450. #define TPM_ST_DEACTIVATED cpu_to_be16(3)
  451. static const struct tpm_input_header tpm_startup_header = {
  452. .tag = TPM_TAG_RQU_COMMAND,
  453. .length = cpu_to_be32(12),
  454. .ordinal = TPM_ORD_STARTUP
  455. };
  456. static int tpm_startup(struct tpm_chip *chip, __be16 startup_type)
  457. {
  458. struct tpm_cmd_t start_cmd;
  459. start_cmd.header.in = tpm_startup_header;
  460. start_cmd.params.startup_in.startup_type = startup_type;
  461. return tpm_transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE, 0,
  462. "attempting to start the TPM");
  463. }
  464. int tpm_get_timeouts(struct tpm_chip *chip)
  465. {
  466. struct tpm_cmd_t tpm_cmd;
  467. unsigned long new_timeout[4];
  468. unsigned long old_timeout[4];
  469. struct duration_t *duration_cap;
  470. ssize_t rc;
  471. tpm_cmd.header.in = tpm_getcap_header;
  472. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  473. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  474. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  475. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 0,
  476. NULL);
  477. if (rc == TPM_ERR_INVALID_POSTINIT) {
  478. /* The TPM is not started, we are the first to talk to it.
  479. Execute a startup command. */
  480. dev_info(&chip->dev, "Issuing TPM_STARTUP");
  481. if (tpm_startup(chip, TPM_ST_CLEAR))
  482. return rc;
  483. tpm_cmd.header.in = tpm_getcap_header;
  484. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  485. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  486. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  487. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  488. 0, NULL);
  489. }
  490. if (rc) {
  491. dev_err(&chip->dev,
  492. "A TPM error (%zd) occurred attempting to determine the timeouts\n",
  493. rc);
  494. goto duration;
  495. }
  496. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  497. be32_to_cpu(tpm_cmd.header.out.length)
  498. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
  499. return -EINVAL;
  500. old_timeout[0] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.a);
  501. old_timeout[1] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.b);
  502. old_timeout[2] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.c);
  503. old_timeout[3] = be32_to_cpu(tpm_cmd.params.getcap_out.cap.timeout.d);
  504. memcpy(new_timeout, old_timeout, sizeof(new_timeout));
  505. /*
  506. * Provide ability for vendor overrides of timeout values in case
  507. * of misreporting.
  508. */
  509. if (chip->ops->update_timeouts != NULL)
  510. chip->vendor.timeout_adjusted =
  511. chip->ops->update_timeouts(chip, new_timeout);
  512. if (!chip->vendor.timeout_adjusted) {
  513. /* Don't overwrite default if value is 0 */
  514. if (new_timeout[0] != 0 && new_timeout[0] < 1000) {
  515. int i;
  516. /* timeouts in msec rather usec */
  517. for (i = 0; i != ARRAY_SIZE(new_timeout); i++)
  518. new_timeout[i] *= 1000;
  519. chip->vendor.timeout_adjusted = true;
  520. }
  521. }
  522. /* Report adjusted timeouts */
  523. if (chip->vendor.timeout_adjusted) {
  524. dev_info(&chip->dev,
  525. HW_ERR "Adjusting reported timeouts: A %lu->%luus B %lu->%luus C %lu->%luus D %lu->%luus\n",
  526. old_timeout[0], new_timeout[0],
  527. old_timeout[1], new_timeout[1],
  528. old_timeout[2], new_timeout[2],
  529. old_timeout[3], new_timeout[3]);
  530. }
  531. chip->vendor.timeout_a = usecs_to_jiffies(new_timeout[0]);
  532. chip->vendor.timeout_b = usecs_to_jiffies(new_timeout[1]);
  533. chip->vendor.timeout_c = usecs_to_jiffies(new_timeout[2]);
  534. chip->vendor.timeout_d = usecs_to_jiffies(new_timeout[3]);
  535. duration:
  536. tpm_cmd.header.in = tpm_getcap_header;
  537. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  538. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  539. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
  540. rc = tpm_transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, 0,
  541. "attempting to determine the durations");
  542. if (rc)
  543. return rc;
  544. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  545. be32_to_cpu(tpm_cmd.header.out.length)
  546. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
  547. return -EINVAL;
  548. duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
  549. chip->vendor.duration[TPM_SHORT] =
  550. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
  551. chip->vendor.duration[TPM_MEDIUM] =
  552. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
  553. chip->vendor.duration[TPM_LONG] =
  554. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
  555. /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
  556. * value wrong and apparently reports msecs rather than usecs. So we
  557. * fix up the resulting too-small TPM_SHORT value to make things work.
  558. * We also scale the TPM_MEDIUM and -_LONG values by 1000.
  559. */
  560. if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
  561. chip->vendor.duration[TPM_SHORT] = HZ;
  562. chip->vendor.duration[TPM_MEDIUM] *= 1000;
  563. chip->vendor.duration[TPM_LONG] *= 1000;
  564. chip->vendor.duration_adjusted = true;
  565. dev_info(&chip->dev, "Adjusting TPM timeout parameters.");
  566. }
  567. return 0;
  568. }
  569. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  570. #define TPM_ORD_CONTINUE_SELFTEST 83
  571. #define CONTINUE_SELFTEST_RESULT_SIZE 10
  572. static struct tpm_input_header continue_selftest_header = {
  573. .tag = TPM_TAG_RQU_COMMAND,
  574. .length = cpu_to_be32(10),
  575. .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
  576. };
  577. /**
  578. * tpm_continue_selftest -- run TPM's selftest
  579. * @chip: TPM chip to use
  580. *
  581. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  582. * a TPM error code.
  583. */
  584. static int tpm_continue_selftest(struct tpm_chip *chip)
  585. {
  586. int rc;
  587. struct tpm_cmd_t cmd;
  588. cmd.header.in = continue_selftest_header;
  589. rc = tpm_transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE, 0,
  590. "continue selftest");
  591. return rc;
  592. }
  593. #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
  594. #define READ_PCR_RESULT_SIZE 30
  595. static struct tpm_input_header pcrread_header = {
  596. .tag = TPM_TAG_RQU_COMMAND,
  597. .length = cpu_to_be32(14),
  598. .ordinal = TPM_ORDINAL_PCRREAD
  599. };
  600. int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  601. {
  602. int rc;
  603. struct tpm_cmd_t cmd;
  604. cmd.header.in = pcrread_header;
  605. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
  606. rc = tpm_transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE, 0,
  607. "attempting to read a pcr value");
  608. if (rc == 0)
  609. memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
  610. TPM_DIGEST_SIZE);
  611. return rc;
  612. }
  613. /**
  614. * tpm_is_tpm2 - is the chip a TPM2 chip?
  615. * @chip_num: tpm idx # or ANY
  616. *
  617. * Returns < 0 on error, and 1 or 0 on success depending whether the chip
  618. * is a TPM2 chip.
  619. */
  620. int tpm_is_tpm2(u32 chip_num)
  621. {
  622. struct tpm_chip *chip;
  623. int rc;
  624. chip = tpm_chip_find_get(chip_num);
  625. if (chip == NULL)
  626. return -ENODEV;
  627. rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
  628. tpm_put_ops(chip);
  629. return rc;
  630. }
  631. EXPORT_SYMBOL_GPL(tpm_is_tpm2);
  632. /**
  633. * tpm_pcr_read - read a pcr value
  634. * @chip_num: tpm idx # or ANY
  635. * @pcr_idx: pcr idx to retrieve
  636. * @res_buf: TPM_PCR value
  637. * size of res_buf is 20 bytes (or NULL if you don't care)
  638. *
  639. * The TPM driver should be built-in, but for whatever reason it
  640. * isn't, protect against the chip disappearing, by incrementing
  641. * the module usage count.
  642. */
  643. int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
  644. {
  645. struct tpm_chip *chip;
  646. int rc;
  647. chip = tpm_chip_find_get(chip_num);
  648. if (chip == NULL)
  649. return -ENODEV;
  650. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  651. rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
  652. else
  653. rc = tpm_pcr_read_dev(chip, pcr_idx, res_buf);
  654. tpm_put_ops(chip);
  655. return rc;
  656. }
  657. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  658. /**
  659. * tpm_pcr_extend - extend pcr value with hash
  660. * @chip_num: tpm idx # or AN&
  661. * @pcr_idx: pcr idx to extend
  662. * @hash: hash value used to extend pcr value
  663. *
  664. * The TPM driver should be built-in, but for whatever reason it
  665. * isn't, protect against the chip disappearing, by incrementing
  666. * the module usage count.
  667. */
  668. #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
  669. #define EXTEND_PCR_RESULT_SIZE 34
  670. static struct tpm_input_header pcrextend_header = {
  671. .tag = TPM_TAG_RQU_COMMAND,
  672. .length = cpu_to_be32(34),
  673. .ordinal = TPM_ORD_PCR_EXTEND
  674. };
  675. int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
  676. {
  677. struct tpm_cmd_t cmd;
  678. int rc;
  679. struct tpm_chip *chip;
  680. chip = tpm_chip_find_get(chip_num);
  681. if (chip == NULL)
  682. return -ENODEV;
  683. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  684. rc = tpm2_pcr_extend(chip, pcr_idx, hash);
  685. tpm_put_ops(chip);
  686. return rc;
  687. }
  688. cmd.header.in = pcrextend_header;
  689. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  690. memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
  691. rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 0,
  692. "attempting extend a PCR value");
  693. tpm_put_ops(chip);
  694. return rc;
  695. }
  696. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  697. /**
  698. * tpm_do_selftest - have the TPM continue its selftest and wait until it
  699. * can receive further commands
  700. * @chip: TPM chip to use
  701. *
  702. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  703. * a TPM error code.
  704. */
  705. int tpm_do_selftest(struct tpm_chip *chip)
  706. {
  707. int rc;
  708. unsigned int loops;
  709. unsigned int delay_msec = 100;
  710. unsigned long duration;
  711. struct tpm_cmd_t cmd;
  712. duration = tpm_calc_ordinal_duration(chip, TPM_ORD_CONTINUE_SELFTEST);
  713. loops = jiffies_to_msecs(duration) / delay_msec;
  714. rc = tpm_continue_selftest(chip);
  715. if (rc == TPM_ERR_INVALID_POSTINIT) {
  716. chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
  717. dev_info(&chip->dev, "TPM not ready (%d)\n", rc);
  718. }
  719. /* This may fail if there was no TPM driver during a suspend/resume
  720. * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
  721. */
  722. if (rc)
  723. return rc;
  724. do {
  725. /* Attempt to read a PCR value */
  726. cmd.header.in = pcrread_header;
  727. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(0);
  728. rc = tpm_transmit(chip, (u8 *) &cmd, READ_PCR_RESULT_SIZE, 0);
  729. /* Some buggy TPMs will not respond to tpm_tis_ready() for
  730. * around 300ms while the self test is ongoing, keep trying
  731. * until the self test duration expires. */
  732. if (rc == -ETIME) {
  733. dev_info(
  734. &chip->dev, HW_ERR
  735. "TPM command timed out during continue self test");
  736. msleep(delay_msec);
  737. continue;
  738. }
  739. if (rc < TPM_HEADER_SIZE)
  740. return -EFAULT;
  741. rc = be32_to_cpu(cmd.header.out.return_code);
  742. if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
  743. dev_info(&chip->dev,
  744. "TPM is disabled/deactivated (0x%X)\n", rc);
  745. /* TPM is disabled and/or deactivated; driver can
  746. * proceed and TPM does handle commands for
  747. * suspend/resume correctly
  748. */
  749. return 0;
  750. }
  751. if (rc != TPM_WARN_DOING_SELFTEST)
  752. return rc;
  753. msleep(delay_msec);
  754. } while (--loops > 0);
  755. return rc;
  756. }
  757. EXPORT_SYMBOL_GPL(tpm_do_selftest);
  758. int tpm_send(u32 chip_num, void *cmd, size_t buflen)
  759. {
  760. struct tpm_chip *chip;
  761. int rc;
  762. chip = tpm_chip_find_get(chip_num);
  763. if (chip == NULL)
  764. return -ENODEV;
  765. rc = tpm_transmit_cmd(chip, cmd, buflen, 0, "attempting tpm_cmd");
  766. tpm_put_ops(chip);
  767. return rc;
  768. }
  769. EXPORT_SYMBOL_GPL(tpm_send);
  770. static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
  771. bool check_cancel, bool *canceled)
  772. {
  773. u8 status = chip->ops->status(chip);
  774. *canceled = false;
  775. if ((status & mask) == mask)
  776. return true;
  777. if (check_cancel && chip->ops->req_canceled(chip, status)) {
  778. *canceled = true;
  779. return true;
  780. }
  781. return false;
  782. }
  783. int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  784. wait_queue_head_t *queue, bool check_cancel)
  785. {
  786. unsigned long stop;
  787. long rc;
  788. u8 status;
  789. bool canceled = false;
  790. /* check current status */
  791. status = chip->ops->status(chip);
  792. if ((status & mask) == mask)
  793. return 0;
  794. stop = jiffies + timeout;
  795. if (chip->vendor.irq) {
  796. again:
  797. timeout = stop - jiffies;
  798. if ((long)timeout <= 0)
  799. return -ETIME;
  800. rc = wait_event_interruptible_timeout(*queue,
  801. wait_for_tpm_stat_cond(chip, mask, check_cancel,
  802. &canceled),
  803. timeout);
  804. if (rc > 0) {
  805. if (canceled)
  806. return -ECANCELED;
  807. return 0;
  808. }
  809. if (rc == -ERESTARTSYS && freezing(current)) {
  810. clear_thread_flag(TIF_SIGPENDING);
  811. goto again;
  812. }
  813. } else {
  814. do {
  815. msleep(TPM_TIMEOUT);
  816. status = chip->ops->status(chip);
  817. if ((status & mask) == mask)
  818. return 0;
  819. } while (time_before(jiffies, stop));
  820. }
  821. return -ETIME;
  822. }
  823. EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
  824. #define TPM_ORD_SAVESTATE cpu_to_be32(152)
  825. #define SAVESTATE_RESULT_SIZE 10
  826. static struct tpm_input_header savestate_header = {
  827. .tag = TPM_TAG_RQU_COMMAND,
  828. .length = cpu_to_be32(10),
  829. .ordinal = TPM_ORD_SAVESTATE
  830. };
  831. /*
  832. * We are about to suspend. Save the TPM state
  833. * so that it can be restored.
  834. */
  835. int tpm_pm_suspend(struct device *dev)
  836. {
  837. struct tpm_chip *chip = dev_get_drvdata(dev);
  838. struct tpm_cmd_t cmd;
  839. int rc, try;
  840. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  841. if (chip == NULL)
  842. return -ENODEV;
  843. if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
  844. return 0;
  845. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  846. tpm2_shutdown(chip, TPM2_SU_STATE);
  847. return 0;
  848. }
  849. /* for buggy tpm, flush pcrs with extend to selected dummy */
  850. if (tpm_suspend_pcr) {
  851. cmd.header.in = pcrextend_header;
  852. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
  853. memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
  854. TPM_DIGEST_SIZE);
  855. rc = tpm_transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 0,
  856. "extending dummy pcr before suspend");
  857. }
  858. /* now do the actual savestate */
  859. for (try = 0; try < TPM_RETRY; try++) {
  860. cmd.header.in = savestate_header;
  861. rc = tpm_transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, 0,
  862. NULL);
  863. /*
  864. * If the TPM indicates that it is too busy to respond to
  865. * this command then retry before giving up. It can take
  866. * several seconds for this TPM to be ready.
  867. *
  868. * This can happen if the TPM has already been sent the
  869. * SaveState command before the driver has loaded. TCG 1.2
  870. * specification states that any communication after SaveState
  871. * may cause the TPM to invalidate previously saved state.
  872. */
  873. if (rc != TPM_WARN_RETRY)
  874. break;
  875. msleep(TPM_TIMEOUT_RETRY);
  876. }
  877. if (rc)
  878. dev_err(&chip->dev,
  879. "Error (%d) sending savestate before suspend\n", rc);
  880. else if (try > 0)
  881. dev_warn(&chip->dev, "TPM savestate took %dms\n",
  882. try * TPM_TIMEOUT_RETRY);
  883. return rc;
  884. }
  885. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  886. /*
  887. * Resume from a power safe. The BIOS already restored
  888. * the TPM state.
  889. */
  890. int tpm_pm_resume(struct device *dev)
  891. {
  892. struct tpm_chip *chip = dev_get_drvdata(dev);
  893. if (chip == NULL)
  894. return -ENODEV;
  895. return 0;
  896. }
  897. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  898. #define TPM_GETRANDOM_RESULT_SIZE 18
  899. static struct tpm_input_header tpm_getrandom_header = {
  900. .tag = TPM_TAG_RQU_COMMAND,
  901. .length = cpu_to_be32(14),
  902. .ordinal = TPM_ORD_GET_RANDOM
  903. };
  904. /**
  905. * tpm_get_random() - Get random bytes from the tpm's RNG
  906. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  907. * @out: destination buffer for the random bytes
  908. * @max: the max number of bytes to write to @out
  909. *
  910. * Returns < 0 on error and the number of bytes read on success
  911. */
  912. int tpm_get_random(u32 chip_num, u8 *out, size_t max)
  913. {
  914. struct tpm_chip *chip;
  915. struct tpm_cmd_t tpm_cmd;
  916. u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
  917. int err, total = 0, retries = 5;
  918. u8 *dest = out;
  919. if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
  920. return -EINVAL;
  921. chip = tpm_chip_find_get(chip_num);
  922. if (chip == NULL)
  923. return -ENODEV;
  924. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  925. err = tpm2_get_random(chip, out, max);
  926. tpm_put_ops(chip);
  927. return err;
  928. }
  929. do {
  930. tpm_cmd.header.in = tpm_getrandom_header;
  931. tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
  932. err = tpm_transmit_cmd(chip, &tpm_cmd,
  933. TPM_GETRANDOM_RESULT_SIZE + num_bytes,
  934. 0, "attempting get random");
  935. if (err)
  936. break;
  937. recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
  938. if (recd > num_bytes) {
  939. total = -EFAULT;
  940. break;
  941. }
  942. memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
  943. dest += recd;
  944. total += recd;
  945. num_bytes -= recd;
  946. } while (retries-- && total < max);
  947. tpm_put_ops(chip);
  948. return total ? total : -EIO;
  949. }
  950. EXPORT_SYMBOL_GPL(tpm_get_random);
  951. /**
  952. * tpm_seal_trusted() - seal a trusted key
  953. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  954. * @options: authentication values and other options
  955. * @payload: the key data in clear and encrypted form
  956. *
  957. * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips
  958. * are supported.
  959. */
  960. int tpm_seal_trusted(u32 chip_num, struct trusted_key_payload *payload,
  961. struct trusted_key_options *options)
  962. {
  963. struct tpm_chip *chip;
  964. int rc;
  965. chip = tpm_chip_find_get(chip_num);
  966. if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2))
  967. return -ENODEV;
  968. rc = tpm2_seal_trusted(chip, payload, options);
  969. tpm_put_ops(chip);
  970. return rc;
  971. }
  972. EXPORT_SYMBOL_GPL(tpm_seal_trusted);
  973. /**
  974. * tpm_unseal_trusted() - unseal a trusted key
  975. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  976. * @options: authentication values and other options
  977. * @payload: the key data in clear and encrypted form
  978. *
  979. * Returns < 0 on error and 0 on success. At the moment, only TPM 2.0 chips
  980. * are supported.
  981. */
  982. int tpm_unseal_trusted(u32 chip_num, struct trusted_key_payload *payload,
  983. struct trusted_key_options *options)
  984. {
  985. struct tpm_chip *chip;
  986. int rc;
  987. chip = tpm_chip_find_get(chip_num);
  988. if (chip == NULL || !(chip->flags & TPM_CHIP_FLAG_TPM2))
  989. return -ENODEV;
  990. rc = tpm2_unseal_trusted(chip, payload, options);
  991. tpm_put_ops(chip);
  992. return rc;
  993. }
  994. EXPORT_SYMBOL_GPL(tpm_unseal_trusted);
  995. static int __init tpm_init(void)
  996. {
  997. int rc;
  998. tpm_class = class_create(THIS_MODULE, "tpm");
  999. if (IS_ERR(tpm_class)) {
  1000. pr_err("couldn't create tpm class\n");
  1001. return PTR_ERR(tpm_class);
  1002. }
  1003. rc = alloc_chrdev_region(&tpm_devt, 0, TPM_NUM_DEVICES, "tpm");
  1004. if (rc < 0) {
  1005. pr_err("tpm: failed to allocate char dev region\n");
  1006. class_destroy(tpm_class);
  1007. return rc;
  1008. }
  1009. return 0;
  1010. }
  1011. static void __exit tpm_exit(void)
  1012. {
  1013. idr_destroy(&dev_nums_idr);
  1014. class_destroy(tpm_class);
  1015. unregister_chrdev_region(tpm_devt, TPM_NUM_DEVICES);
  1016. }
  1017. subsys_initcall(tpm_init);
  1018. module_exit(tpm_exit);
  1019. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1020. MODULE_DESCRIPTION("TPM Driver");
  1021. MODULE_VERSION("2.0");
  1022. MODULE_LICENSE("GPL");