tpm.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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/sched.h>
  26. #include <linux/poll.h>
  27. #include <linux/spinlock.h>
  28. #include "tpm.h"
  29. enum tpm_const {
  30. TPM_MINOR = 224, /* officially assigned */
  31. TPM_BUFSIZE = 2048,
  32. TPM_NUM_DEVICES = 256,
  33. };
  34. enum tpm_duration {
  35. TPM_SHORT = 0,
  36. TPM_MEDIUM = 1,
  37. TPM_LONG = 2,
  38. TPM_UNDEFINED,
  39. };
  40. #define TPM_MAX_ORDINAL 243
  41. #define TPM_MAX_PROTECTED_ORDINAL 12
  42. #define TPM_PROTECTED_ORDINAL_MASK 0xFF
  43. static LIST_HEAD(tpm_chip_list);
  44. static DEFINE_SPINLOCK(driver_lock);
  45. static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
  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_protected_ordinal_duration[TPM_MAX_PROTECTED_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. };
  68. static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
  69. TPM_UNDEFINED, /* 0 */
  70. TPM_UNDEFINED,
  71. TPM_UNDEFINED,
  72. TPM_UNDEFINED,
  73. TPM_UNDEFINED,
  74. TPM_UNDEFINED, /* 5 */
  75. TPM_UNDEFINED,
  76. TPM_UNDEFINED,
  77. TPM_UNDEFINED,
  78. TPM_UNDEFINED,
  79. TPM_SHORT, /* 10 */
  80. TPM_SHORT,
  81. TPM_MEDIUM,
  82. TPM_LONG,
  83. TPM_LONG,
  84. TPM_MEDIUM, /* 15 */
  85. TPM_SHORT,
  86. TPM_SHORT,
  87. TPM_MEDIUM,
  88. TPM_LONG,
  89. TPM_SHORT, /* 20 */
  90. TPM_SHORT,
  91. TPM_MEDIUM,
  92. TPM_MEDIUM,
  93. TPM_MEDIUM,
  94. TPM_SHORT, /* 25 */
  95. TPM_SHORT,
  96. TPM_MEDIUM,
  97. TPM_SHORT,
  98. TPM_SHORT,
  99. TPM_MEDIUM, /* 30 */
  100. TPM_LONG,
  101. TPM_MEDIUM,
  102. TPM_SHORT,
  103. TPM_SHORT,
  104. TPM_SHORT, /* 35 */
  105. TPM_MEDIUM,
  106. TPM_MEDIUM,
  107. TPM_UNDEFINED,
  108. TPM_UNDEFINED,
  109. TPM_MEDIUM, /* 40 */
  110. TPM_LONG,
  111. TPM_MEDIUM,
  112. TPM_SHORT,
  113. TPM_SHORT,
  114. TPM_SHORT, /* 45 */
  115. TPM_SHORT,
  116. TPM_SHORT,
  117. TPM_SHORT,
  118. TPM_LONG,
  119. TPM_MEDIUM, /* 50 */
  120. TPM_MEDIUM,
  121. TPM_UNDEFINED,
  122. TPM_UNDEFINED,
  123. TPM_UNDEFINED,
  124. TPM_UNDEFINED, /* 55 */
  125. TPM_UNDEFINED,
  126. TPM_UNDEFINED,
  127. TPM_UNDEFINED,
  128. TPM_UNDEFINED,
  129. TPM_MEDIUM, /* 60 */
  130. TPM_MEDIUM,
  131. TPM_MEDIUM,
  132. TPM_SHORT,
  133. TPM_SHORT,
  134. TPM_MEDIUM, /* 65 */
  135. TPM_UNDEFINED,
  136. TPM_UNDEFINED,
  137. TPM_UNDEFINED,
  138. TPM_UNDEFINED,
  139. TPM_SHORT, /* 70 */
  140. TPM_SHORT,
  141. TPM_UNDEFINED,
  142. TPM_UNDEFINED,
  143. TPM_UNDEFINED,
  144. TPM_UNDEFINED, /* 75 */
  145. TPM_UNDEFINED,
  146. TPM_UNDEFINED,
  147. TPM_UNDEFINED,
  148. TPM_UNDEFINED,
  149. TPM_LONG, /* 80 */
  150. TPM_UNDEFINED,
  151. TPM_MEDIUM,
  152. TPM_LONG,
  153. TPM_SHORT,
  154. TPM_UNDEFINED, /* 85 */
  155. TPM_UNDEFINED,
  156. TPM_UNDEFINED,
  157. TPM_UNDEFINED,
  158. TPM_UNDEFINED,
  159. TPM_SHORT, /* 90 */
  160. TPM_SHORT,
  161. TPM_SHORT,
  162. TPM_SHORT,
  163. TPM_SHORT,
  164. TPM_UNDEFINED, /* 95 */
  165. TPM_UNDEFINED,
  166. TPM_UNDEFINED,
  167. TPM_UNDEFINED,
  168. TPM_UNDEFINED,
  169. TPM_MEDIUM, /* 100 */
  170. TPM_SHORT,
  171. TPM_SHORT,
  172. TPM_UNDEFINED,
  173. TPM_UNDEFINED,
  174. TPM_UNDEFINED, /* 105 */
  175. TPM_UNDEFINED,
  176. TPM_UNDEFINED,
  177. TPM_UNDEFINED,
  178. TPM_UNDEFINED,
  179. TPM_SHORT, /* 110 */
  180. TPM_SHORT,
  181. TPM_SHORT,
  182. TPM_SHORT,
  183. TPM_SHORT,
  184. TPM_SHORT, /* 115 */
  185. TPM_SHORT,
  186. TPM_SHORT,
  187. TPM_UNDEFINED,
  188. TPM_UNDEFINED,
  189. TPM_LONG, /* 120 */
  190. TPM_LONG,
  191. TPM_MEDIUM,
  192. TPM_UNDEFINED,
  193. TPM_SHORT,
  194. TPM_SHORT, /* 125 */
  195. TPM_SHORT,
  196. TPM_LONG,
  197. TPM_SHORT,
  198. TPM_SHORT,
  199. TPM_SHORT, /* 130 */
  200. TPM_MEDIUM,
  201. TPM_UNDEFINED,
  202. TPM_SHORT,
  203. TPM_MEDIUM,
  204. TPM_UNDEFINED, /* 135 */
  205. TPM_UNDEFINED,
  206. TPM_UNDEFINED,
  207. TPM_UNDEFINED,
  208. TPM_UNDEFINED,
  209. TPM_SHORT, /* 140 */
  210. TPM_SHORT,
  211. TPM_UNDEFINED,
  212. TPM_UNDEFINED,
  213. TPM_UNDEFINED,
  214. TPM_UNDEFINED, /* 145 */
  215. TPM_UNDEFINED,
  216. TPM_UNDEFINED,
  217. TPM_UNDEFINED,
  218. TPM_UNDEFINED,
  219. TPM_SHORT, /* 150 */
  220. TPM_MEDIUM,
  221. TPM_MEDIUM,
  222. TPM_SHORT,
  223. TPM_SHORT,
  224. TPM_UNDEFINED, /* 155 */
  225. TPM_UNDEFINED,
  226. TPM_UNDEFINED,
  227. TPM_UNDEFINED,
  228. TPM_UNDEFINED,
  229. TPM_SHORT, /* 160 */
  230. TPM_SHORT,
  231. TPM_SHORT,
  232. TPM_SHORT,
  233. TPM_UNDEFINED,
  234. TPM_UNDEFINED, /* 165 */
  235. TPM_UNDEFINED,
  236. TPM_UNDEFINED,
  237. TPM_UNDEFINED,
  238. TPM_UNDEFINED,
  239. TPM_LONG, /* 170 */
  240. TPM_UNDEFINED,
  241. TPM_UNDEFINED,
  242. TPM_UNDEFINED,
  243. TPM_UNDEFINED,
  244. TPM_UNDEFINED, /* 175 */
  245. TPM_UNDEFINED,
  246. TPM_UNDEFINED,
  247. TPM_UNDEFINED,
  248. TPM_UNDEFINED,
  249. TPM_MEDIUM, /* 180 */
  250. TPM_SHORT,
  251. TPM_MEDIUM,
  252. TPM_MEDIUM,
  253. TPM_MEDIUM,
  254. TPM_MEDIUM, /* 185 */
  255. TPM_SHORT,
  256. TPM_UNDEFINED,
  257. TPM_UNDEFINED,
  258. TPM_UNDEFINED,
  259. TPM_UNDEFINED, /* 190 */
  260. TPM_UNDEFINED,
  261. TPM_UNDEFINED,
  262. TPM_UNDEFINED,
  263. TPM_UNDEFINED,
  264. TPM_UNDEFINED, /* 195 */
  265. TPM_UNDEFINED,
  266. TPM_UNDEFINED,
  267. TPM_UNDEFINED,
  268. TPM_UNDEFINED,
  269. TPM_SHORT, /* 200 */
  270. TPM_UNDEFINED,
  271. TPM_UNDEFINED,
  272. TPM_UNDEFINED,
  273. TPM_SHORT,
  274. TPM_SHORT, /* 205 */
  275. TPM_SHORT,
  276. TPM_SHORT,
  277. TPM_SHORT,
  278. TPM_SHORT,
  279. TPM_MEDIUM, /* 210 */
  280. TPM_UNDEFINED,
  281. TPM_MEDIUM,
  282. TPM_MEDIUM,
  283. TPM_MEDIUM,
  284. TPM_UNDEFINED, /* 215 */
  285. TPM_MEDIUM,
  286. TPM_UNDEFINED,
  287. TPM_UNDEFINED,
  288. TPM_SHORT,
  289. TPM_SHORT, /* 220 */
  290. TPM_SHORT,
  291. TPM_SHORT,
  292. TPM_SHORT,
  293. TPM_SHORT,
  294. TPM_UNDEFINED, /* 225 */
  295. TPM_UNDEFINED,
  296. TPM_UNDEFINED,
  297. TPM_UNDEFINED,
  298. TPM_UNDEFINED,
  299. TPM_SHORT, /* 230 */
  300. TPM_LONG,
  301. TPM_MEDIUM,
  302. TPM_UNDEFINED,
  303. TPM_UNDEFINED,
  304. TPM_UNDEFINED, /* 235 */
  305. TPM_UNDEFINED,
  306. TPM_UNDEFINED,
  307. TPM_UNDEFINED,
  308. TPM_UNDEFINED,
  309. TPM_SHORT, /* 240 */
  310. TPM_UNDEFINED,
  311. TPM_MEDIUM,
  312. };
  313. static void user_reader_timeout(unsigned long ptr)
  314. {
  315. struct tpm_chip *chip = (struct tpm_chip *) ptr;
  316. schedule_work(&chip->work);
  317. }
  318. static void timeout_work(void *ptr)
  319. {
  320. struct tpm_chip *chip = ptr;
  321. down(&chip->buffer_mutex);
  322. atomic_set(&chip->data_pending, 0);
  323. memset(chip->data_buffer, 0, TPM_BUFSIZE);
  324. up(&chip->buffer_mutex);
  325. }
  326. /*
  327. * Returns max number of jiffies to wait
  328. */
  329. unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
  330. u32 ordinal)
  331. {
  332. int duration_idx = TPM_UNDEFINED;
  333. int duration = 0;
  334. if (ordinal < TPM_MAX_ORDINAL)
  335. duration_idx = tpm_ordinal_duration[ordinal];
  336. else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
  337. TPM_MAX_PROTECTED_ORDINAL)
  338. duration_idx =
  339. tpm_protected_ordinal_duration[ordinal &
  340. TPM_PROTECTED_ORDINAL_MASK];
  341. if (duration_idx != TPM_UNDEFINED)
  342. duration = chip->vendor.duration[duration_idx];
  343. if (duration <= 0)
  344. return 2 * 60 * HZ;
  345. else
  346. return duration;
  347. }
  348. EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
  349. /*
  350. * Internal kernel interface to transmit TPM commands
  351. */
  352. static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
  353. size_t bufsiz)
  354. {
  355. ssize_t rc;
  356. u32 count, ordinal;
  357. unsigned long stop;
  358. count = be32_to_cpu(*((__be32 *) (buf + 2)));
  359. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  360. if (count == 0)
  361. return -ENODATA;
  362. if (count > bufsiz) {
  363. dev_err(chip->dev,
  364. "invalid count value %x %zx \n", count, bufsiz);
  365. return -E2BIG;
  366. }
  367. down(&chip->tpm_mutex);
  368. if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
  369. dev_err(chip->dev,
  370. "tpm_transmit: tpm_send: error %zd\n", rc);
  371. goto out;
  372. }
  373. if (chip->vendor.irq)
  374. goto out_recv;
  375. stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
  376. do {
  377. u8 status = chip->vendor.status(chip);
  378. if ((status & chip->vendor.req_complete_mask) ==
  379. chip->vendor.req_complete_val)
  380. goto out_recv;
  381. if ((status == chip->vendor.req_canceled)) {
  382. dev_err(chip->dev, "Operation Canceled\n");
  383. rc = -ECANCELED;
  384. goto out;
  385. }
  386. msleep(TPM_TIMEOUT); /* CHECK */
  387. rmb();
  388. } while (time_before(jiffies, stop));
  389. chip->vendor.cancel(chip);
  390. dev_err(chip->dev, "Operation Timed out\n");
  391. rc = -ETIME;
  392. goto out;
  393. out_recv:
  394. rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
  395. if (rc < 0)
  396. dev_err(chip->dev,
  397. "tpm_transmit: tpm_recv: error %zd\n", rc);
  398. out:
  399. up(&chip->tpm_mutex);
  400. return rc;
  401. }
  402. #define TPM_DIGEST_SIZE 20
  403. #define TPM_ERROR_SIZE 10
  404. #define TPM_RET_CODE_IDX 6
  405. #define TPM_GET_CAP_RET_SIZE_IDX 10
  406. #define TPM_GET_CAP_RET_UINT32_1_IDX 14
  407. #define TPM_GET_CAP_RET_UINT32_2_IDX 18
  408. #define TPM_GET_CAP_RET_UINT32_3_IDX 22
  409. #define TPM_GET_CAP_RET_UINT32_4_IDX 26
  410. #define TPM_GET_CAP_PERM_DISABLE_IDX 16
  411. #define TPM_GET_CAP_PERM_INACTIVE_IDX 18
  412. #define TPM_GET_CAP_RET_BOOL_1_IDX 14
  413. #define TPM_GET_CAP_TEMP_INACTIVE_IDX 16
  414. #define TPM_CAP_IDX 13
  415. #define TPM_CAP_SUBCAP_IDX 21
  416. enum tpm_capabilities {
  417. TPM_CAP_FLAG = 4,
  418. TPM_CAP_PROP = 5,
  419. };
  420. enum tpm_sub_capabilities {
  421. TPM_CAP_PROP_PCR = 0x1,
  422. TPM_CAP_PROP_MANUFACTURER = 0x3,
  423. TPM_CAP_FLAG_PERM = 0x8,
  424. TPM_CAP_FLAG_VOL = 0x9,
  425. TPM_CAP_PROP_OWNER = 0x11,
  426. TPM_CAP_PROP_TIS_TIMEOUT = 0x15,
  427. TPM_CAP_PROP_TIS_DURATION = 0x20,
  428. };
  429. /*
  430. * This is a semi generic GetCapability command for use
  431. * with the capability type TPM_CAP_PROP or TPM_CAP_FLAG
  432. * and their associated sub_capabilities.
  433. */
  434. static const u8 tpm_cap[] = {
  435. 0, 193, /* TPM_TAG_RQU_COMMAND */
  436. 0, 0, 0, 22, /* length */
  437. 0, 0, 0, 101, /* TPM_ORD_GetCapability */
  438. 0, 0, 0, 0, /* TPM_CAP_<TYPE> */
  439. 0, 0, 0, 4, /* TPM_CAP_SUB_<TYPE> size */
  440. 0, 0, 1, 0 /* TPM_CAP_SUB_<TYPE> */
  441. };
  442. static ssize_t transmit_cmd(struct tpm_chip *chip, u8 *data, int len,
  443. char *desc)
  444. {
  445. int err;
  446. len = tpm_transmit(chip, data, len);
  447. if (len < 0)
  448. return len;
  449. if (len == TPM_ERROR_SIZE) {
  450. err = be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX)));
  451. dev_dbg(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
  452. return err;
  453. }
  454. return 0;
  455. }
  456. void tpm_gen_interrupt(struct tpm_chip *chip)
  457. {
  458. u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 30)];
  459. ssize_t rc;
  460. memcpy(data, tpm_cap, sizeof(tpm_cap));
  461. data[TPM_CAP_IDX] = TPM_CAP_PROP;
  462. data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_TIMEOUT;
  463. rc = transmit_cmd(chip, data, sizeof(data),
  464. "attempting to determine the timeouts");
  465. }
  466. EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
  467. void tpm_get_timeouts(struct tpm_chip *chip)
  468. {
  469. u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 30)];
  470. ssize_t rc;
  471. u32 timeout;
  472. memcpy(data, tpm_cap, sizeof(tpm_cap));
  473. data[TPM_CAP_IDX] = TPM_CAP_PROP;
  474. data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_TIMEOUT;
  475. rc = transmit_cmd(chip, data, sizeof(data),
  476. "attempting to determine the timeouts");
  477. if (rc)
  478. goto duration;
  479. if (be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_SIZE_IDX)))
  480. != 4 * sizeof(u32))
  481. goto duration;
  482. /* Don't overwrite default if value is 0 */
  483. timeout =
  484. be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX)));
  485. if (timeout)
  486. chip->vendor.timeout_a = msecs_to_jiffies(timeout);
  487. timeout =
  488. be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_2_IDX)));
  489. if (timeout)
  490. chip->vendor.timeout_b = msecs_to_jiffies(timeout);
  491. timeout =
  492. be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_3_IDX)));
  493. if (timeout)
  494. chip->vendor.timeout_c = msecs_to_jiffies(timeout);
  495. timeout =
  496. be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_4_IDX)));
  497. if (timeout)
  498. chip->vendor.timeout_d = msecs_to_jiffies(timeout);
  499. duration:
  500. memcpy(data, tpm_cap, sizeof(tpm_cap));
  501. data[TPM_CAP_IDX] = TPM_CAP_PROP;
  502. data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_TIS_DURATION;
  503. rc = transmit_cmd(chip, data, sizeof(data),
  504. "attempting to determine the durations");
  505. if (rc)
  506. return;
  507. if (be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_SIZE_IDX)))
  508. != 3 * sizeof(u32))
  509. return;
  510. chip->vendor.duration[TPM_SHORT] =
  511. msecs_to_jiffies(be32_to_cpu
  512. (*((__be32 *) (data +
  513. TPM_GET_CAP_RET_UINT32_1_IDX))));
  514. chip->vendor.duration[TPM_MEDIUM] =
  515. msecs_to_jiffies(be32_to_cpu
  516. (*((__be32 *) (data +
  517. TPM_GET_CAP_RET_UINT32_2_IDX))));
  518. chip->vendor.duration[TPM_LONG] =
  519. msecs_to_jiffies(be32_to_cpu
  520. (*((__be32 *) (data +
  521. TPM_GET_CAP_RET_UINT32_3_IDX))));
  522. }
  523. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  524. void tpm_continue_selftest(struct tpm_chip *chip)
  525. {
  526. u8 data[] = {
  527. 0, 193, /* TPM_TAG_RQU_COMMAND */
  528. 0, 0, 0, 10, /* length */
  529. 0, 0, 0, 83, /* TPM_ORD_GetCapability */
  530. };
  531. tpm_transmit(chip, data, sizeof(data));
  532. }
  533. EXPORT_SYMBOL_GPL(tpm_continue_selftest);
  534. ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
  535. char *buf)
  536. {
  537. u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 35)];
  538. ssize_t rc;
  539. struct tpm_chip *chip = dev_get_drvdata(dev);
  540. if (chip == NULL)
  541. return -ENODEV;
  542. memcpy(data, tpm_cap, sizeof(tpm_cap));
  543. data[TPM_CAP_IDX] = TPM_CAP_FLAG;
  544. data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM;
  545. rc = transmit_cmd(chip, data, sizeof(data),
  546. "attemtping to determine the permanent state");
  547. if (rc)
  548. return 0;
  549. return sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_DISABLE_IDX]);
  550. }
  551. EXPORT_SYMBOL_GPL(tpm_show_enabled);
  552. ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
  553. char *buf)
  554. {
  555. u8 data[max_t(int, ARRAY_SIZE(tpm_cap), 35)];
  556. ssize_t rc;
  557. struct tpm_chip *chip = dev_get_drvdata(dev);
  558. if (chip == NULL)
  559. return -ENODEV;
  560. memcpy(data, tpm_cap, sizeof(tpm_cap));
  561. data[TPM_CAP_IDX] = TPM_CAP_FLAG;
  562. data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_PERM;
  563. rc = transmit_cmd(chip, data, sizeof(data),
  564. "attemtping to determine the permanent state");
  565. if (rc)
  566. return 0;
  567. return sprintf(buf, "%d\n", !data[TPM_GET_CAP_PERM_INACTIVE_IDX]);
  568. }
  569. EXPORT_SYMBOL_GPL(tpm_show_active);
  570. ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
  571. char *buf)
  572. {
  573. u8 data[sizeof(tpm_cap)];
  574. ssize_t rc;
  575. struct tpm_chip *chip = dev_get_drvdata(dev);
  576. if (chip == NULL)
  577. return -ENODEV;
  578. memcpy(data, tpm_cap, sizeof(tpm_cap));
  579. data[TPM_CAP_IDX] = TPM_CAP_PROP;
  580. data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_OWNER;
  581. rc = transmit_cmd(chip, data, sizeof(data),
  582. "attempting to determine the owner state");
  583. if (rc)
  584. return 0;
  585. return sprintf(buf, "%d\n", data[TPM_GET_CAP_RET_BOOL_1_IDX]);
  586. }
  587. EXPORT_SYMBOL_GPL(tpm_show_owned);
  588. ssize_t tpm_show_temp_deactivated(struct device * dev,
  589. struct device_attribute * attr, char *buf)
  590. {
  591. u8 data[sizeof(tpm_cap)];
  592. ssize_t rc;
  593. struct tpm_chip *chip = dev_get_drvdata(dev);
  594. if (chip == NULL)
  595. return -ENODEV;
  596. memcpy(data, tpm_cap, sizeof(tpm_cap));
  597. data[TPM_CAP_IDX] = TPM_CAP_FLAG;
  598. data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_FLAG_VOL;
  599. rc = transmit_cmd(chip, data, sizeof(data),
  600. "attempting to determine the temporary state");
  601. if (rc)
  602. return 0;
  603. return sprintf(buf, "%d\n", data[TPM_GET_CAP_TEMP_INACTIVE_IDX]);
  604. }
  605. EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
  606. static const u8 pcrread[] = {
  607. 0, 193, /* TPM_TAG_RQU_COMMAND */
  608. 0, 0, 0, 14, /* length */
  609. 0, 0, 0, 21, /* TPM_ORD_PcrRead */
  610. 0, 0, 0, 0 /* PCR index */
  611. };
  612. ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
  613. char *buf)
  614. {
  615. u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(pcrread)), 30)];
  616. ssize_t rc;
  617. int i, j, num_pcrs;
  618. __be32 index;
  619. char *str = buf;
  620. struct tpm_chip *chip = dev_get_drvdata(dev);
  621. if (chip == NULL)
  622. return -ENODEV;
  623. memcpy(data, tpm_cap, sizeof(tpm_cap));
  624. data[TPM_CAP_IDX] = TPM_CAP_PROP;
  625. data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_PCR;
  626. rc = transmit_cmd(chip, data, sizeof(data),
  627. "attempting to determine the number of PCRS");
  628. if (rc)
  629. return 0;
  630. num_pcrs = be32_to_cpu(*((__be32 *) (data + 14)));
  631. for (i = 0; i < num_pcrs; i++) {
  632. memcpy(data, pcrread, sizeof(pcrread));
  633. index = cpu_to_be32(i);
  634. memcpy(data + 10, &index, 4);
  635. rc = transmit_cmd(chip, data, sizeof(data),
  636. "attempting to read a PCR");
  637. if (rc)
  638. goto out;
  639. str += sprintf(str, "PCR-%02d: ", i);
  640. for (j = 0; j < TPM_DIGEST_SIZE; j++)
  641. str += sprintf(str, "%02X ", *(data + 10 + j));
  642. str += sprintf(str, "\n");
  643. }
  644. out:
  645. return str - buf;
  646. }
  647. EXPORT_SYMBOL_GPL(tpm_show_pcrs);
  648. #define READ_PUBEK_RESULT_SIZE 314
  649. static const u8 readpubek[] = {
  650. 0, 193, /* TPM_TAG_RQU_COMMAND */
  651. 0, 0, 0, 30, /* length */
  652. 0, 0, 0, 124, /* TPM_ORD_ReadPubek */
  653. };
  654. ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
  655. char *buf)
  656. {
  657. u8 *data;
  658. ssize_t err;
  659. int i, rc;
  660. char *str = buf;
  661. struct tpm_chip *chip = dev_get_drvdata(dev);
  662. if (chip == NULL)
  663. return -ENODEV;
  664. data = kzalloc(READ_PUBEK_RESULT_SIZE, GFP_KERNEL);
  665. if (!data)
  666. return -ENOMEM;
  667. memcpy(data, readpubek, sizeof(readpubek));
  668. err = transmit_cmd(chip, data, READ_PUBEK_RESULT_SIZE,
  669. "attempting to read the PUBEK");
  670. if (err)
  671. goto out;
  672. /*
  673. ignore header 10 bytes
  674. algorithm 32 bits (1 == RSA )
  675. encscheme 16 bits
  676. sigscheme 16 bits
  677. parameters (RSA 12->bytes: keybit, #primes, expbit)
  678. keylenbytes 32 bits
  679. 256 byte modulus
  680. ignore checksum 20 bytes
  681. */
  682. str +=
  683. sprintf(str,
  684. "Algorithm: %02X %02X %02X %02X\nEncscheme: %02X %02X\n"
  685. "Sigscheme: %02X %02X\nParameters: %02X %02X %02X %02X"
  686. " %02X %02X %02X %02X %02X %02X %02X %02X\n"
  687. "Modulus length: %d\nModulus: \n",
  688. data[10], data[11], data[12], data[13], data[14],
  689. data[15], data[16], data[17], data[22], data[23],
  690. data[24], data[25], data[26], data[27], data[28],
  691. data[29], data[30], data[31], data[32], data[33],
  692. be32_to_cpu(*((__be32 *) (data + 34))));
  693. for (i = 0; i < 256; i++) {
  694. str += sprintf(str, "%02X ", data[i + 38]);
  695. if ((i + 1) % 16 == 0)
  696. str += sprintf(str, "\n");
  697. }
  698. out:
  699. rc = str - buf;
  700. kfree(data);
  701. return rc;
  702. }
  703. EXPORT_SYMBOL_GPL(tpm_show_pubek);
  704. #define CAP_VERSION_1_1 6
  705. #define CAP_VERSION_1_2 0x1A
  706. #define CAP_VERSION_IDX 13
  707. static const u8 cap_version[] = {
  708. 0, 193, /* TPM_TAG_RQU_COMMAND */
  709. 0, 0, 0, 18, /* length */
  710. 0, 0, 0, 101, /* TPM_ORD_GetCapability */
  711. 0, 0, 0, 0,
  712. 0, 0, 0, 0
  713. };
  714. ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
  715. char *buf)
  716. {
  717. u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(cap_version)), 30)];
  718. ssize_t rc;
  719. char *str = buf;
  720. struct tpm_chip *chip = dev_get_drvdata(dev);
  721. if (chip == NULL)
  722. return -ENODEV;
  723. memcpy(data, tpm_cap, sizeof(tpm_cap));
  724. data[TPM_CAP_IDX] = TPM_CAP_PROP;
  725. data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
  726. rc = transmit_cmd(chip, data, sizeof(data),
  727. "attempting to determine the manufacturer");
  728. if (rc)
  729. return 0;
  730. str += sprintf(str, "Manufacturer: 0x%x\n",
  731. be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
  732. memcpy(data, cap_version, sizeof(cap_version));
  733. data[CAP_VERSION_IDX] = CAP_VERSION_1_1;
  734. rc = transmit_cmd(chip, data, sizeof(data),
  735. "attempting to determine the 1.1 version");
  736. if (rc)
  737. goto out;
  738. str += sprintf(str,
  739. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  740. (int) data[14], (int) data[15], (int) data[16],
  741. (int) data[17]);
  742. out:
  743. return str - buf;
  744. }
  745. EXPORT_SYMBOL_GPL(tpm_show_caps);
  746. ssize_t tpm_show_caps_1_2(struct device * dev,
  747. struct device_attribute * attr, char *buf)
  748. {
  749. u8 data[max_t(int, max(ARRAY_SIZE(tpm_cap), ARRAY_SIZE(cap_version)), 30)];
  750. ssize_t len;
  751. char *str = buf;
  752. struct tpm_chip *chip = dev_get_drvdata(dev);
  753. if (chip == NULL)
  754. return -ENODEV;
  755. memcpy(data, tpm_cap, sizeof(tpm_cap));
  756. data[TPM_CAP_IDX] = TPM_CAP_PROP;
  757. data[TPM_CAP_SUBCAP_IDX] = TPM_CAP_PROP_MANUFACTURER;
  758. if ((len = tpm_transmit(chip, data, sizeof(data))) <=
  759. TPM_ERROR_SIZE) {
  760. dev_dbg(chip->dev, "A TPM error (%d) occurred "
  761. "attempting to determine the manufacturer\n",
  762. be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))));
  763. return 0;
  764. }
  765. str += sprintf(str, "Manufacturer: 0x%x\n",
  766. be32_to_cpu(*((__be32 *) (data + TPM_GET_CAP_RET_UINT32_1_IDX))));
  767. memcpy(data, cap_version, sizeof(cap_version));
  768. data[CAP_VERSION_IDX] = CAP_VERSION_1_2;
  769. if ((len = tpm_transmit(chip, data, sizeof(data))) <=
  770. TPM_ERROR_SIZE) {
  771. dev_err(chip->dev, "A TPM error (%d) occurred "
  772. "attempting to determine the 1.2 version\n",
  773. be32_to_cpu(*((__be32 *) (data + TPM_RET_CODE_IDX))));
  774. goto out;
  775. }
  776. str += sprintf(str,
  777. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  778. (int) data[16], (int) data[17], (int) data[18],
  779. (int) data[19]);
  780. out:
  781. return str - buf;
  782. }
  783. EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
  784. ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
  785. const char *buf, size_t count)
  786. {
  787. struct tpm_chip *chip = dev_get_drvdata(dev);
  788. if (chip == NULL)
  789. return 0;
  790. chip->vendor.cancel(chip);
  791. return count;
  792. }
  793. EXPORT_SYMBOL_GPL(tpm_store_cancel);
  794. /*
  795. * Device file system interface to the TPM
  796. */
  797. int tpm_open(struct inode *inode, struct file *file)
  798. {
  799. int rc = 0, minor = iminor(inode);
  800. struct tpm_chip *chip = NULL, *pos;
  801. spin_lock(&driver_lock);
  802. list_for_each_entry(pos, &tpm_chip_list, list) {
  803. if (pos->vendor.miscdev.minor == minor) {
  804. chip = pos;
  805. break;
  806. }
  807. }
  808. if (chip == NULL) {
  809. rc = -ENODEV;
  810. goto err_out;
  811. }
  812. if (chip->num_opens) {
  813. dev_dbg(chip->dev, "Another process owns this TPM\n");
  814. rc = -EBUSY;
  815. goto err_out;
  816. }
  817. chip->num_opens++;
  818. get_device(chip->dev);
  819. spin_unlock(&driver_lock);
  820. chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL);
  821. if (chip->data_buffer == NULL) {
  822. chip->num_opens--;
  823. put_device(chip->dev);
  824. return -ENOMEM;
  825. }
  826. atomic_set(&chip->data_pending, 0);
  827. file->private_data = chip;
  828. return 0;
  829. err_out:
  830. spin_unlock(&driver_lock);
  831. return rc;
  832. }
  833. EXPORT_SYMBOL_GPL(tpm_open);
  834. int tpm_release(struct inode *inode, struct file *file)
  835. {
  836. struct tpm_chip *chip = file->private_data;
  837. spin_lock(&driver_lock);
  838. file->private_data = NULL;
  839. chip->num_opens--;
  840. del_singleshot_timer_sync(&chip->user_read_timer);
  841. flush_scheduled_work();
  842. atomic_set(&chip->data_pending, 0);
  843. put_device(chip->dev);
  844. kfree(chip->data_buffer);
  845. spin_unlock(&driver_lock);
  846. return 0;
  847. }
  848. EXPORT_SYMBOL_GPL(tpm_release);
  849. ssize_t tpm_write(struct file *file, const char __user *buf,
  850. size_t size, loff_t *off)
  851. {
  852. struct tpm_chip *chip = file->private_data;
  853. int in_size = size, out_size;
  854. /* cannot perform a write until the read has cleared
  855. either via tpm_read or a user_read_timer timeout */
  856. while (atomic_read(&chip->data_pending) != 0)
  857. msleep(TPM_TIMEOUT);
  858. down(&chip->buffer_mutex);
  859. if (in_size > TPM_BUFSIZE)
  860. in_size = TPM_BUFSIZE;
  861. if (copy_from_user
  862. (chip->data_buffer, (void __user *) buf, in_size)) {
  863. up(&chip->buffer_mutex);
  864. return -EFAULT;
  865. }
  866. /* atomic tpm command send and result receive */
  867. out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
  868. atomic_set(&chip->data_pending, out_size);
  869. up(&chip->buffer_mutex);
  870. /* Set a timeout by which the reader must come claim the result */
  871. mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
  872. return in_size;
  873. }
  874. EXPORT_SYMBOL_GPL(tpm_write);
  875. ssize_t tpm_read(struct file *file, char __user *buf,
  876. size_t size, loff_t *off)
  877. {
  878. struct tpm_chip *chip = file->private_data;
  879. int ret_size;
  880. del_singleshot_timer_sync(&chip->user_read_timer);
  881. flush_scheduled_work();
  882. ret_size = atomic_read(&chip->data_pending);
  883. atomic_set(&chip->data_pending, 0);
  884. if (ret_size > 0) { /* relay data */
  885. if (size < ret_size)
  886. ret_size = size;
  887. down(&chip->buffer_mutex);
  888. if (copy_to_user(buf, chip->data_buffer, ret_size))
  889. ret_size = -EFAULT;
  890. up(&chip->buffer_mutex);
  891. }
  892. return ret_size;
  893. }
  894. EXPORT_SYMBOL_GPL(tpm_read);
  895. void tpm_remove_hardware(struct device *dev)
  896. {
  897. struct tpm_chip *chip = dev_get_drvdata(dev);
  898. if (chip == NULL) {
  899. dev_err(dev, "No device data found\n");
  900. return;
  901. }
  902. spin_lock(&driver_lock);
  903. list_del(&chip->list);
  904. spin_unlock(&driver_lock);
  905. dev_set_drvdata(dev, NULL);
  906. misc_deregister(&chip->vendor.miscdev);
  907. kfree(chip->vendor.miscdev.name);
  908. sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
  909. tpm_bios_log_teardown(chip->bios_dir);
  910. clear_bit(chip->dev_num, dev_mask);
  911. kfree(chip);
  912. put_device(dev);
  913. }
  914. EXPORT_SYMBOL_GPL(tpm_remove_hardware);
  915. static u8 savestate[] = {
  916. 0, 193, /* TPM_TAG_RQU_COMMAND */
  917. 0, 0, 0, 10, /* blob length (in bytes) */
  918. 0, 0, 0, 152 /* TPM_ORD_SaveState */
  919. };
  920. /*
  921. * We are about to suspend. Save the TPM state
  922. * so that it can be restored.
  923. */
  924. int tpm_pm_suspend(struct device *dev, pm_message_t pm_state)
  925. {
  926. struct tpm_chip *chip = dev_get_drvdata(dev);
  927. if (chip == NULL)
  928. return -ENODEV;
  929. tpm_transmit(chip, savestate, sizeof(savestate));
  930. return 0;
  931. }
  932. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  933. /*
  934. * Resume from a power safe. The BIOS already restored
  935. * the TPM state.
  936. */
  937. int tpm_pm_resume(struct device *dev)
  938. {
  939. struct tpm_chip *chip = dev_get_drvdata(dev);
  940. if (chip == NULL)
  941. return -ENODEV;
  942. return 0;
  943. }
  944. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  945. /*
  946. * Called from tpm_<specific>.c probe function only for devices
  947. * the driver has determined it should claim. Prior to calling
  948. * this function the specific probe function has called pci_enable_device
  949. * upon errant exit from this function specific probe function should call
  950. * pci_disable_device
  951. */
  952. struct tpm_chip *tpm_register_hardware(struct device *dev, const struct tpm_vendor_specific
  953. *entry)
  954. {
  955. #define DEVNAME_SIZE 7
  956. char *devname;
  957. struct tpm_chip *chip;
  958. /* Driver specific per-device data */
  959. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  960. if (chip == NULL)
  961. return NULL;
  962. init_MUTEX(&chip->buffer_mutex);
  963. init_MUTEX(&chip->tpm_mutex);
  964. INIT_LIST_HEAD(&chip->list);
  965. INIT_WORK(&chip->work, timeout_work, chip);
  966. init_timer(&chip->user_read_timer);
  967. chip->user_read_timer.function = user_reader_timeout;
  968. chip->user_read_timer.data = (unsigned long) chip;
  969. memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
  970. chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
  971. if (chip->dev_num >= TPM_NUM_DEVICES) {
  972. dev_err(dev, "No available tpm device numbers\n");
  973. kfree(chip);
  974. return NULL;
  975. } else if (chip->dev_num == 0)
  976. chip->vendor.miscdev.minor = TPM_MINOR;
  977. else
  978. chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
  979. set_bit(chip->dev_num, dev_mask);
  980. devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
  981. scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
  982. chip->vendor.miscdev.name = devname;
  983. chip->vendor.miscdev.dev = dev;
  984. chip->dev = get_device(dev);
  985. if (misc_register(&chip->vendor.miscdev)) {
  986. dev_err(chip->dev,
  987. "unable to misc_register %s, minor %d\n",
  988. chip->vendor.miscdev.name,
  989. chip->vendor.miscdev.minor);
  990. put_device(dev);
  991. clear_bit(chip->dev_num, dev_mask);
  992. kfree(chip);
  993. kfree(devname);
  994. return NULL;
  995. }
  996. spin_lock(&driver_lock);
  997. dev_set_drvdata(dev, chip);
  998. list_add(&chip->list, &tpm_chip_list);
  999. spin_unlock(&driver_lock);
  1000. if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
  1001. list_del(&chip->list);
  1002. put_device(dev);
  1003. clear_bit(chip->dev_num, dev_mask);
  1004. kfree(chip);
  1005. kfree(devname);
  1006. return NULL;
  1007. }
  1008. chip->bios_dir = tpm_bios_log_setup(devname);
  1009. return chip;
  1010. }
  1011. EXPORT_SYMBOL_GPL(tpm_register_hardware);
  1012. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1013. MODULE_DESCRIPTION("TPM Driver");
  1014. MODULE_VERSION("2.0");
  1015. MODULE_LICENSE("GPL");