tpm-interface.c 27 KB

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