tpm.c 27 KB

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