tpm.c 27 KB

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