tpm.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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/mutex.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(struct work_struct *work)
  319. {
  320. struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
  321. mutex_lock(&chip->buffer_mutex);
  322. atomic_set(&chip->data_pending, 0);
  323. memset(chip->data_buffer, 0, TPM_BUFSIZE);
  324. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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. flush_scheduled_work();
  838. spin_lock(&driver_lock);
  839. file->private_data = NULL;
  840. del_singleshot_timer_sync(&chip->user_read_timer);
  841. atomic_set(&chip->data_pending, 0);
  842. chip->num_opens--;
  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. mutex_lock(&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. mutex_unlock(&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. mutex_unlock(&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. mutex_lock(&chip->buffer_mutex);
  888. if (copy_to_user(buf, chip->data_buffer, ret_size))
  889. ret_size = -EFAULT;
  890. mutex_unlock(&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. devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
  961. if (chip == NULL || devname == NULL) {
  962. kfree(chip);
  963. kfree(devname);
  964. return NULL;
  965. }
  966. mutex_init(&chip->buffer_mutex);
  967. mutex_init(&chip->tpm_mutex);
  968. INIT_LIST_HEAD(&chip->list);
  969. INIT_WORK(&chip->work, timeout_work);
  970. setup_timer(&chip->user_read_timer, user_reader_timeout,
  971. (unsigned long)chip);
  972. memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
  973. chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
  974. if (chip->dev_num >= TPM_NUM_DEVICES) {
  975. dev_err(dev, "No available tpm device numbers\n");
  976. kfree(chip);
  977. return NULL;
  978. } else if (chip->dev_num == 0)
  979. chip->vendor.miscdev.minor = TPM_MINOR;
  980. else
  981. chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
  982. set_bit(chip->dev_num, dev_mask);
  983. scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
  984. chip->vendor.miscdev.name = devname;
  985. chip->vendor.miscdev.parent = dev;
  986. chip->dev = get_device(dev);
  987. if (misc_register(&chip->vendor.miscdev)) {
  988. dev_err(chip->dev,
  989. "unable to misc_register %s, minor %d\n",
  990. chip->vendor.miscdev.name,
  991. chip->vendor.miscdev.minor);
  992. put_device(dev);
  993. clear_bit(chip->dev_num, dev_mask);
  994. kfree(chip);
  995. kfree(devname);
  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. if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
  1003. list_del(&chip->list);
  1004. misc_deregister(&chip->vendor.miscdev);
  1005. put_device(dev);
  1006. clear_bit(chip->dev_num, dev_mask);
  1007. kfree(chip);
  1008. kfree(devname);
  1009. return NULL;
  1010. }
  1011. chip->bios_dir = tpm_bios_log_setup(devname);
  1012. return chip;
  1013. }
  1014. EXPORT_SYMBOL_GPL(tpm_register_hardware);
  1015. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1016. MODULE_DESCRIPTION("TPM Driver");
  1017. MODULE_VERSION("2.0");
  1018. MODULE_LICENSE("GPL");