tpm.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. /*
  2. * Copyright (C) 2004 IBM Corporation
  3. *
  4. * Authors:
  5. * Leendert van Doorn <leendert@watson.ibm.com>
  6. * Dave Safford <safford@watson.ibm.com>
  7. * Reiner Sailer <sailer@watson.ibm.com>
  8. * Kylene Hall <kjhall@us.ibm.com>
  9. *
  10. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  11. *
  12. * Device driver for TCG/TCPA TPM (trusted platform module).
  13. * Specifications at www.trustedcomputinggroup.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation, version 2 of the
  18. * License.
  19. *
  20. * Note, the TPM chip is not interrupt driven (only polling)
  21. * and can have very long timeouts (minutes!). Hence the unusual
  22. * calls to msleep.
  23. *
  24. */
  25. #include <linux/poll.h>
  26. #include <linux/slab.h>
  27. #include <linux/mutex.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/freezer.h>
  30. #include "tpm.h"
  31. #include "tpm_eventlog.h"
  32. enum tpm_duration {
  33. TPM_SHORT = 0,
  34. TPM_MEDIUM = 1,
  35. TPM_LONG = 2,
  36. TPM_UNDEFINED,
  37. };
  38. #define TPM_MAX_ORDINAL 243
  39. #define TPM_MAX_PROTECTED_ORDINAL 12
  40. #define TPM_PROTECTED_ORDINAL_MASK 0xFF
  41. /*
  42. * Bug workaround - some TPM's don't flush the most
  43. * recently changed pcr on suspend, so force the flush
  44. * with an extend to the selected _unused_ non-volatile pcr.
  45. */
  46. static int tpm_suspend_pcr;
  47. module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
  48. MODULE_PARM_DESC(suspend_pcr,
  49. "PCR to use for dummy writes to faciltate flush on suspend.");
  50. static LIST_HEAD(tpm_chip_list);
  51. static DEFINE_SPINLOCK(driver_lock);
  52. static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
  53. /*
  54. * Array with one entry per ordinal defining the maximum amount
  55. * of time the chip could take to return the result. The ordinal
  56. * designation of short, medium or long is defined in a table in
  57. * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
  58. * values of the SHORT, MEDIUM, and LONG durations are retrieved
  59. * from the chip during initialization with a call to tpm_get_timeouts.
  60. */
  61. static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
  62. TPM_UNDEFINED, /* 0 */
  63. TPM_UNDEFINED,
  64. TPM_UNDEFINED,
  65. TPM_UNDEFINED,
  66. TPM_UNDEFINED,
  67. TPM_UNDEFINED, /* 5 */
  68. TPM_UNDEFINED,
  69. TPM_UNDEFINED,
  70. TPM_UNDEFINED,
  71. TPM_UNDEFINED,
  72. TPM_SHORT, /* 10 */
  73. TPM_SHORT,
  74. };
  75. static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
  76. TPM_UNDEFINED, /* 0 */
  77. TPM_UNDEFINED,
  78. TPM_UNDEFINED,
  79. TPM_UNDEFINED,
  80. TPM_UNDEFINED,
  81. TPM_UNDEFINED, /* 5 */
  82. TPM_UNDEFINED,
  83. TPM_UNDEFINED,
  84. TPM_UNDEFINED,
  85. TPM_UNDEFINED,
  86. TPM_SHORT, /* 10 */
  87. TPM_SHORT,
  88. TPM_MEDIUM,
  89. TPM_LONG,
  90. TPM_LONG,
  91. TPM_MEDIUM, /* 15 */
  92. TPM_SHORT,
  93. TPM_SHORT,
  94. TPM_MEDIUM,
  95. TPM_LONG,
  96. TPM_SHORT, /* 20 */
  97. TPM_SHORT,
  98. TPM_MEDIUM,
  99. TPM_MEDIUM,
  100. TPM_MEDIUM,
  101. TPM_SHORT, /* 25 */
  102. TPM_SHORT,
  103. TPM_MEDIUM,
  104. TPM_SHORT,
  105. TPM_SHORT,
  106. TPM_MEDIUM, /* 30 */
  107. TPM_LONG,
  108. TPM_MEDIUM,
  109. TPM_SHORT,
  110. TPM_SHORT,
  111. TPM_SHORT, /* 35 */
  112. TPM_MEDIUM,
  113. TPM_MEDIUM,
  114. TPM_UNDEFINED,
  115. TPM_UNDEFINED,
  116. TPM_MEDIUM, /* 40 */
  117. TPM_LONG,
  118. TPM_MEDIUM,
  119. TPM_SHORT,
  120. TPM_SHORT,
  121. TPM_SHORT, /* 45 */
  122. TPM_SHORT,
  123. TPM_SHORT,
  124. TPM_SHORT,
  125. TPM_LONG,
  126. TPM_MEDIUM, /* 50 */
  127. TPM_MEDIUM,
  128. TPM_UNDEFINED,
  129. TPM_UNDEFINED,
  130. TPM_UNDEFINED,
  131. TPM_UNDEFINED, /* 55 */
  132. TPM_UNDEFINED,
  133. TPM_UNDEFINED,
  134. TPM_UNDEFINED,
  135. TPM_UNDEFINED,
  136. TPM_MEDIUM, /* 60 */
  137. TPM_MEDIUM,
  138. TPM_MEDIUM,
  139. TPM_SHORT,
  140. TPM_SHORT,
  141. TPM_MEDIUM, /* 65 */
  142. TPM_UNDEFINED,
  143. TPM_UNDEFINED,
  144. TPM_UNDEFINED,
  145. TPM_UNDEFINED,
  146. TPM_SHORT, /* 70 */
  147. TPM_SHORT,
  148. TPM_UNDEFINED,
  149. TPM_UNDEFINED,
  150. TPM_UNDEFINED,
  151. TPM_UNDEFINED, /* 75 */
  152. TPM_UNDEFINED,
  153. TPM_UNDEFINED,
  154. TPM_UNDEFINED,
  155. TPM_UNDEFINED,
  156. TPM_LONG, /* 80 */
  157. TPM_UNDEFINED,
  158. TPM_MEDIUM,
  159. TPM_LONG,
  160. TPM_SHORT,
  161. TPM_UNDEFINED, /* 85 */
  162. TPM_UNDEFINED,
  163. TPM_UNDEFINED,
  164. TPM_UNDEFINED,
  165. TPM_UNDEFINED,
  166. TPM_SHORT, /* 90 */
  167. TPM_SHORT,
  168. TPM_SHORT,
  169. TPM_SHORT,
  170. TPM_SHORT,
  171. TPM_UNDEFINED, /* 95 */
  172. TPM_UNDEFINED,
  173. TPM_UNDEFINED,
  174. TPM_UNDEFINED,
  175. TPM_UNDEFINED,
  176. TPM_MEDIUM, /* 100 */
  177. TPM_SHORT,
  178. TPM_SHORT,
  179. TPM_UNDEFINED,
  180. TPM_UNDEFINED,
  181. TPM_UNDEFINED, /* 105 */
  182. TPM_UNDEFINED,
  183. TPM_UNDEFINED,
  184. TPM_UNDEFINED,
  185. TPM_UNDEFINED,
  186. TPM_SHORT, /* 110 */
  187. TPM_SHORT,
  188. TPM_SHORT,
  189. TPM_SHORT,
  190. TPM_SHORT,
  191. TPM_SHORT, /* 115 */
  192. TPM_SHORT,
  193. TPM_SHORT,
  194. TPM_UNDEFINED,
  195. TPM_UNDEFINED,
  196. TPM_LONG, /* 120 */
  197. TPM_LONG,
  198. TPM_MEDIUM,
  199. TPM_UNDEFINED,
  200. TPM_SHORT,
  201. TPM_SHORT, /* 125 */
  202. TPM_SHORT,
  203. TPM_LONG,
  204. TPM_SHORT,
  205. TPM_SHORT,
  206. TPM_SHORT, /* 130 */
  207. TPM_MEDIUM,
  208. TPM_UNDEFINED,
  209. TPM_SHORT,
  210. TPM_MEDIUM,
  211. TPM_UNDEFINED, /* 135 */
  212. TPM_UNDEFINED,
  213. TPM_UNDEFINED,
  214. TPM_UNDEFINED,
  215. TPM_UNDEFINED,
  216. TPM_SHORT, /* 140 */
  217. TPM_SHORT,
  218. TPM_UNDEFINED,
  219. TPM_UNDEFINED,
  220. TPM_UNDEFINED,
  221. TPM_UNDEFINED, /* 145 */
  222. TPM_UNDEFINED,
  223. TPM_UNDEFINED,
  224. TPM_UNDEFINED,
  225. TPM_UNDEFINED,
  226. TPM_SHORT, /* 150 */
  227. TPM_MEDIUM,
  228. TPM_MEDIUM,
  229. TPM_SHORT,
  230. TPM_SHORT,
  231. TPM_UNDEFINED, /* 155 */
  232. TPM_UNDEFINED,
  233. TPM_UNDEFINED,
  234. TPM_UNDEFINED,
  235. TPM_UNDEFINED,
  236. TPM_SHORT, /* 160 */
  237. TPM_SHORT,
  238. TPM_SHORT,
  239. TPM_SHORT,
  240. TPM_UNDEFINED,
  241. TPM_UNDEFINED, /* 165 */
  242. TPM_UNDEFINED,
  243. TPM_UNDEFINED,
  244. TPM_UNDEFINED,
  245. TPM_UNDEFINED,
  246. TPM_LONG, /* 170 */
  247. TPM_UNDEFINED,
  248. TPM_UNDEFINED,
  249. TPM_UNDEFINED,
  250. TPM_UNDEFINED,
  251. TPM_UNDEFINED, /* 175 */
  252. TPM_UNDEFINED,
  253. TPM_UNDEFINED,
  254. TPM_UNDEFINED,
  255. TPM_UNDEFINED,
  256. TPM_MEDIUM, /* 180 */
  257. TPM_SHORT,
  258. TPM_MEDIUM,
  259. TPM_MEDIUM,
  260. TPM_MEDIUM,
  261. TPM_MEDIUM, /* 185 */
  262. TPM_SHORT,
  263. TPM_UNDEFINED,
  264. TPM_UNDEFINED,
  265. TPM_UNDEFINED,
  266. TPM_UNDEFINED, /* 190 */
  267. TPM_UNDEFINED,
  268. TPM_UNDEFINED,
  269. TPM_UNDEFINED,
  270. TPM_UNDEFINED,
  271. TPM_UNDEFINED, /* 195 */
  272. TPM_UNDEFINED,
  273. TPM_UNDEFINED,
  274. TPM_UNDEFINED,
  275. TPM_UNDEFINED,
  276. TPM_SHORT, /* 200 */
  277. TPM_UNDEFINED,
  278. TPM_UNDEFINED,
  279. TPM_UNDEFINED,
  280. TPM_SHORT,
  281. TPM_SHORT, /* 205 */
  282. TPM_SHORT,
  283. TPM_SHORT,
  284. TPM_SHORT,
  285. TPM_SHORT,
  286. TPM_MEDIUM, /* 210 */
  287. TPM_UNDEFINED,
  288. TPM_MEDIUM,
  289. TPM_MEDIUM,
  290. TPM_MEDIUM,
  291. TPM_UNDEFINED, /* 215 */
  292. TPM_MEDIUM,
  293. TPM_UNDEFINED,
  294. TPM_UNDEFINED,
  295. TPM_SHORT,
  296. TPM_SHORT, /* 220 */
  297. TPM_SHORT,
  298. TPM_SHORT,
  299. TPM_SHORT,
  300. TPM_SHORT,
  301. TPM_UNDEFINED, /* 225 */
  302. TPM_UNDEFINED,
  303. TPM_UNDEFINED,
  304. TPM_UNDEFINED,
  305. TPM_UNDEFINED,
  306. TPM_SHORT, /* 230 */
  307. TPM_LONG,
  308. TPM_MEDIUM,
  309. TPM_UNDEFINED,
  310. TPM_UNDEFINED,
  311. TPM_UNDEFINED, /* 235 */
  312. TPM_UNDEFINED,
  313. TPM_UNDEFINED,
  314. TPM_UNDEFINED,
  315. TPM_UNDEFINED,
  316. TPM_SHORT, /* 240 */
  317. TPM_UNDEFINED,
  318. TPM_MEDIUM,
  319. };
  320. static void user_reader_timeout(unsigned long ptr)
  321. {
  322. struct tpm_chip *chip = (struct tpm_chip *) ptr;
  323. schedule_work(&chip->work);
  324. }
  325. static void timeout_work(struct work_struct *work)
  326. {
  327. struct tpm_chip *chip = container_of(work, struct tpm_chip, work);
  328. mutex_lock(&chip->buffer_mutex);
  329. atomic_set(&chip->data_pending, 0);
  330. memset(chip->data_buffer, 0, TPM_BUFSIZE);
  331. mutex_unlock(&chip->buffer_mutex);
  332. }
  333. /*
  334. * Returns max number of jiffies to wait
  335. */
  336. unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
  337. u32 ordinal)
  338. {
  339. int duration_idx = TPM_UNDEFINED;
  340. int duration = 0;
  341. if (ordinal < TPM_MAX_ORDINAL)
  342. duration_idx = tpm_ordinal_duration[ordinal];
  343. else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
  344. TPM_MAX_PROTECTED_ORDINAL)
  345. duration_idx =
  346. tpm_protected_ordinal_duration[ordinal &
  347. TPM_PROTECTED_ORDINAL_MASK];
  348. if (duration_idx != TPM_UNDEFINED)
  349. duration = chip->vendor.duration[duration_idx];
  350. if (duration <= 0)
  351. return 2 * 60 * HZ;
  352. else
  353. return duration;
  354. }
  355. EXPORT_SYMBOL_GPL(tpm_calc_ordinal_duration);
  356. /*
  357. * Internal kernel interface to transmit TPM commands
  358. */
  359. static ssize_t tpm_transmit(struct tpm_chip *chip, const char *buf,
  360. size_t bufsiz)
  361. {
  362. ssize_t rc;
  363. u32 count, ordinal;
  364. unsigned long stop;
  365. if (bufsiz > TPM_BUFSIZE)
  366. bufsiz = TPM_BUFSIZE;
  367. count = be32_to_cpu(*((__be32 *) (buf + 2)));
  368. ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
  369. if (count == 0)
  370. return -ENODATA;
  371. if (count > bufsiz) {
  372. dev_err(chip->dev,
  373. "invalid count value %x %zx \n", count, bufsiz);
  374. return -E2BIG;
  375. }
  376. mutex_lock(&chip->tpm_mutex);
  377. if ((rc = chip->vendor.send(chip, (u8 *) buf, count)) < 0) {
  378. dev_err(chip->dev,
  379. "tpm_transmit: tpm_send: error %zd\n", rc);
  380. goto out;
  381. }
  382. if (chip->vendor.irq)
  383. goto out_recv;
  384. stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
  385. do {
  386. u8 status = chip->vendor.status(chip);
  387. if ((status & chip->vendor.req_complete_mask) ==
  388. chip->vendor.req_complete_val)
  389. goto out_recv;
  390. if ((status == chip->vendor.req_canceled)) {
  391. dev_err(chip->dev, "Operation Canceled\n");
  392. rc = -ECANCELED;
  393. goto out;
  394. }
  395. msleep(TPM_TIMEOUT); /* CHECK */
  396. rmb();
  397. } while (time_before(jiffies, stop));
  398. chip->vendor.cancel(chip);
  399. dev_err(chip->dev, "Operation Timed out\n");
  400. rc = -ETIME;
  401. goto out;
  402. out_recv:
  403. rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
  404. if (rc < 0)
  405. dev_err(chip->dev,
  406. "tpm_transmit: tpm_recv: error %zd\n", rc);
  407. out:
  408. mutex_unlock(&chip->tpm_mutex);
  409. return rc;
  410. }
  411. #define TPM_DIGEST_SIZE 20
  412. #define TPM_RET_CODE_IDX 6
  413. enum tpm_capabilities {
  414. TPM_CAP_FLAG = cpu_to_be32(4),
  415. TPM_CAP_PROP = cpu_to_be32(5),
  416. CAP_VERSION_1_1 = cpu_to_be32(0x06),
  417. CAP_VERSION_1_2 = cpu_to_be32(0x1A)
  418. };
  419. enum tpm_sub_capabilities {
  420. TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
  421. TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
  422. TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
  423. TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
  424. TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
  425. TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
  426. TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
  427. };
  428. static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
  429. int len, const char *desc)
  430. {
  431. int err;
  432. len = tpm_transmit(chip,(u8 *) cmd, len);
  433. if (len < 0)
  434. return len;
  435. else if (len < TPM_HEADER_SIZE)
  436. return -EFAULT;
  437. err = be32_to_cpu(cmd->header.out.return_code);
  438. if (err != 0)
  439. dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
  440. return err;
  441. }
  442. #define TPM_INTERNAL_RESULT_SIZE 200
  443. #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
  444. #define TPM_ORD_GET_CAP cpu_to_be32(101)
  445. #define TPM_ORD_GET_RANDOM cpu_to_be32(70)
  446. static const struct tpm_input_header tpm_getcap_header = {
  447. .tag = TPM_TAG_RQU_COMMAND,
  448. .length = cpu_to_be32(22),
  449. .ordinal = TPM_ORD_GET_CAP
  450. };
  451. ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
  452. const char *desc)
  453. {
  454. struct tpm_cmd_t tpm_cmd;
  455. int rc;
  456. struct tpm_chip *chip = dev_get_drvdata(dev);
  457. tpm_cmd.header.in = tpm_getcap_header;
  458. if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
  459. tpm_cmd.params.getcap_in.cap = subcap_id;
  460. /*subcap field not necessary */
  461. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
  462. tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
  463. } else {
  464. if (subcap_id == TPM_CAP_FLAG_PERM ||
  465. subcap_id == TPM_CAP_FLAG_VOL)
  466. tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
  467. else
  468. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  469. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  470. tpm_cmd.params.getcap_in.subcap = subcap_id;
  471. }
  472. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
  473. if (!rc)
  474. *cap = tpm_cmd.params.getcap_out.cap;
  475. return rc;
  476. }
  477. void tpm_gen_interrupt(struct tpm_chip *chip)
  478. {
  479. struct tpm_cmd_t tpm_cmd;
  480. ssize_t rc;
  481. tpm_cmd.header.in = tpm_getcap_header;
  482. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  483. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  484. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  485. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  486. "attempting to determine the timeouts");
  487. }
  488. EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
  489. int tpm_get_timeouts(struct tpm_chip *chip)
  490. {
  491. struct tpm_cmd_t tpm_cmd;
  492. struct timeout_t *timeout_cap;
  493. struct duration_t *duration_cap;
  494. ssize_t rc;
  495. u32 timeout;
  496. unsigned int scale = 1;
  497. tpm_cmd.header.in = tpm_getcap_header;
  498. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  499. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  500. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  501. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  502. "attempting to determine the timeouts");
  503. if (rc)
  504. goto duration;
  505. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  506. be32_to_cpu(tpm_cmd.header.out.length)
  507. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
  508. return -EINVAL;
  509. timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
  510. /* Don't overwrite default if value is 0 */
  511. timeout = be32_to_cpu(timeout_cap->a);
  512. if (timeout && timeout < 1000) {
  513. /* timeouts in msec rather usec */
  514. scale = 1000;
  515. chip->vendor.timeout_adjusted = true;
  516. }
  517. if (timeout)
  518. chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale);
  519. timeout = be32_to_cpu(timeout_cap->b);
  520. if (timeout)
  521. chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale);
  522. timeout = be32_to_cpu(timeout_cap->c);
  523. if (timeout)
  524. chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale);
  525. timeout = be32_to_cpu(timeout_cap->d);
  526. if (timeout)
  527. chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale);
  528. duration:
  529. tpm_cmd.header.in = tpm_getcap_header;
  530. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  531. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  532. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
  533. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  534. "attempting to determine the durations");
  535. if (rc)
  536. return rc;
  537. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  538. be32_to_cpu(tpm_cmd.header.out.length)
  539. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
  540. return -EINVAL;
  541. duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
  542. chip->vendor.duration[TPM_SHORT] =
  543. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
  544. chip->vendor.duration[TPM_MEDIUM] =
  545. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
  546. chip->vendor.duration[TPM_LONG] =
  547. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
  548. /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
  549. * value wrong and apparently reports msecs rather than usecs. So we
  550. * fix up the resulting too-small TPM_SHORT value to make things work.
  551. * We also scale the TPM_MEDIUM and -_LONG values by 1000.
  552. */
  553. if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
  554. chip->vendor.duration[TPM_SHORT] = HZ;
  555. chip->vendor.duration[TPM_MEDIUM] *= 1000;
  556. chip->vendor.duration[TPM_LONG] *= 1000;
  557. chip->vendor.duration_adjusted = true;
  558. dev_info(chip->dev, "Adjusting TPM timeout parameters.");
  559. }
  560. return 0;
  561. }
  562. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  563. #define TPM_ORD_CONTINUE_SELFTEST 83
  564. #define CONTINUE_SELFTEST_RESULT_SIZE 10
  565. static struct tpm_input_header continue_selftest_header = {
  566. .tag = TPM_TAG_RQU_COMMAND,
  567. .length = cpu_to_be32(10),
  568. .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
  569. };
  570. /**
  571. * tpm_continue_selftest -- run TPM's selftest
  572. * @chip: TPM chip to use
  573. *
  574. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  575. * a TPM error code.
  576. */
  577. static int tpm_continue_selftest(struct tpm_chip *chip)
  578. {
  579. int rc;
  580. struct tpm_cmd_t cmd;
  581. cmd.header.in = continue_selftest_header;
  582. rc = transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
  583. "continue selftest");
  584. return rc;
  585. }
  586. ssize_t tpm_show_enabled(struct device * dev, struct device_attribute * attr,
  587. char *buf)
  588. {
  589. cap_t cap;
  590. ssize_t rc;
  591. rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
  592. "attempting to determine the permanent enabled state");
  593. if (rc)
  594. return 0;
  595. rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
  596. return rc;
  597. }
  598. EXPORT_SYMBOL_GPL(tpm_show_enabled);
  599. ssize_t tpm_show_active(struct device * dev, struct device_attribute * attr,
  600. char *buf)
  601. {
  602. cap_t cap;
  603. ssize_t rc;
  604. rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
  605. "attempting to determine the permanent active state");
  606. if (rc)
  607. return 0;
  608. rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
  609. return rc;
  610. }
  611. EXPORT_SYMBOL_GPL(tpm_show_active);
  612. ssize_t tpm_show_owned(struct device * dev, struct device_attribute * attr,
  613. char *buf)
  614. {
  615. cap_t cap;
  616. ssize_t rc;
  617. rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
  618. "attempting to determine the owner state");
  619. if (rc)
  620. return 0;
  621. rc = sprintf(buf, "%d\n", cap.owned);
  622. return rc;
  623. }
  624. EXPORT_SYMBOL_GPL(tpm_show_owned);
  625. ssize_t tpm_show_temp_deactivated(struct device * dev,
  626. struct device_attribute * attr, char *buf)
  627. {
  628. cap_t cap;
  629. ssize_t rc;
  630. rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
  631. "attempting to determine the temporary state");
  632. if (rc)
  633. return 0;
  634. rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
  635. return rc;
  636. }
  637. EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
  638. /*
  639. * tpm_chip_find_get - return tpm_chip for given chip number
  640. */
  641. static struct tpm_chip *tpm_chip_find_get(int chip_num)
  642. {
  643. struct tpm_chip *pos, *chip = NULL;
  644. rcu_read_lock();
  645. list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
  646. if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
  647. continue;
  648. if (try_module_get(pos->dev->driver->owner)) {
  649. chip = pos;
  650. break;
  651. }
  652. }
  653. rcu_read_unlock();
  654. return chip;
  655. }
  656. #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
  657. #define READ_PCR_RESULT_SIZE 30
  658. static struct tpm_input_header pcrread_header = {
  659. .tag = TPM_TAG_RQU_COMMAND,
  660. .length = cpu_to_be32(14),
  661. .ordinal = TPM_ORDINAL_PCRREAD
  662. };
  663. static int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  664. {
  665. int rc;
  666. struct tpm_cmd_t cmd;
  667. cmd.header.in = pcrread_header;
  668. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
  669. rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
  670. "attempting to read a pcr value");
  671. if (rc == 0)
  672. memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
  673. TPM_DIGEST_SIZE);
  674. return rc;
  675. }
  676. /**
  677. * tpm_pcr_read - read a pcr value
  678. * @chip_num: tpm idx # or ANY
  679. * @pcr_idx: pcr idx to retrieve
  680. * @res_buf: TPM_PCR value
  681. * size of res_buf is 20 bytes (or NULL if you don't care)
  682. *
  683. * The TPM driver should be built-in, but for whatever reason it
  684. * isn't, protect against the chip disappearing, by incrementing
  685. * the module usage count.
  686. */
  687. int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
  688. {
  689. struct tpm_chip *chip;
  690. int rc;
  691. chip = tpm_chip_find_get(chip_num);
  692. if (chip == NULL)
  693. return -ENODEV;
  694. rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
  695. tpm_chip_put(chip);
  696. return rc;
  697. }
  698. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  699. /**
  700. * tpm_pcr_extend - extend pcr value with hash
  701. * @chip_num: tpm idx # or AN&
  702. * @pcr_idx: pcr idx to extend
  703. * @hash: hash value used to extend pcr value
  704. *
  705. * The TPM driver should be built-in, but for whatever reason it
  706. * isn't, protect against the chip disappearing, by incrementing
  707. * the module usage count.
  708. */
  709. #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
  710. #define EXTEND_PCR_RESULT_SIZE 34
  711. static struct tpm_input_header pcrextend_header = {
  712. .tag = TPM_TAG_RQU_COMMAND,
  713. .length = cpu_to_be32(34),
  714. .ordinal = TPM_ORD_PCR_EXTEND
  715. };
  716. int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
  717. {
  718. struct tpm_cmd_t cmd;
  719. int rc;
  720. struct tpm_chip *chip;
  721. chip = tpm_chip_find_get(chip_num);
  722. if (chip == NULL)
  723. return -ENODEV;
  724. cmd.header.in = pcrextend_header;
  725. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  726. memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
  727. rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  728. "attempting extend a PCR value");
  729. tpm_chip_put(chip);
  730. return rc;
  731. }
  732. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  733. /**
  734. * tpm_do_selftest - have the TPM continue its selftest and wait until it
  735. * can receive further commands
  736. * @chip: TPM chip to use
  737. *
  738. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  739. * a TPM error code.
  740. */
  741. int tpm_do_selftest(struct tpm_chip *chip)
  742. {
  743. int rc;
  744. unsigned int loops;
  745. unsigned int delay_msec = 1000;
  746. unsigned long duration;
  747. struct tpm_cmd_t cmd;
  748. duration = tpm_calc_ordinal_duration(chip,
  749. TPM_ORD_CONTINUE_SELFTEST);
  750. loops = jiffies_to_msecs(duration) / delay_msec;
  751. rc = tpm_continue_selftest(chip);
  752. /* This may fail if there was no TPM driver during a suspend/resume
  753. * cycle; some may return 10 (BAD_ORDINAL), others 28 (FAILEDSELFTEST)
  754. */
  755. if (rc)
  756. return rc;
  757. do {
  758. /* Attempt to read a PCR value */
  759. cmd.header.in = pcrread_header;
  760. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(0);
  761. rc = tpm_transmit(chip, (u8 *) &cmd, READ_PCR_RESULT_SIZE);
  762. if (rc < TPM_HEADER_SIZE)
  763. return -EFAULT;
  764. rc = be32_to_cpu(cmd.header.out.return_code);
  765. if (rc == TPM_ERR_DISABLED || rc == TPM_ERR_DEACTIVATED) {
  766. dev_info(chip->dev,
  767. "TPM is disabled/deactivated (0x%X)\n", rc);
  768. /* TPM is disabled and/or deactivated; driver can
  769. * proceed and TPM does handle commands for
  770. * suspend/resume correctly
  771. */
  772. return 0;
  773. }
  774. if (rc != TPM_WARN_DOING_SELFTEST)
  775. return rc;
  776. msleep(delay_msec);
  777. } while (--loops > 0);
  778. return rc;
  779. }
  780. EXPORT_SYMBOL_GPL(tpm_do_selftest);
  781. int tpm_send(u32 chip_num, void *cmd, size_t buflen)
  782. {
  783. struct tpm_chip *chip;
  784. int rc;
  785. chip = tpm_chip_find_get(chip_num);
  786. if (chip == NULL)
  787. return -ENODEV;
  788. rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
  789. tpm_chip_put(chip);
  790. return rc;
  791. }
  792. EXPORT_SYMBOL_GPL(tpm_send);
  793. ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
  794. char *buf)
  795. {
  796. cap_t cap;
  797. u8 digest[TPM_DIGEST_SIZE];
  798. ssize_t rc;
  799. int i, j, num_pcrs;
  800. char *str = buf;
  801. struct tpm_chip *chip = dev_get_drvdata(dev);
  802. rc = tpm_getcap(dev, TPM_CAP_PROP_PCR, &cap,
  803. "attempting to determine the number of PCRS");
  804. if (rc)
  805. return 0;
  806. num_pcrs = be32_to_cpu(cap.num_pcrs);
  807. for (i = 0; i < num_pcrs; i++) {
  808. rc = __tpm_pcr_read(chip, i, digest);
  809. if (rc)
  810. break;
  811. str += sprintf(str, "PCR-%02d: ", i);
  812. for (j = 0; j < TPM_DIGEST_SIZE; j++)
  813. str += sprintf(str, "%02X ", digest[j]);
  814. str += sprintf(str, "\n");
  815. }
  816. return str - buf;
  817. }
  818. EXPORT_SYMBOL_GPL(tpm_show_pcrs);
  819. #define READ_PUBEK_RESULT_SIZE 314
  820. #define TPM_ORD_READPUBEK cpu_to_be32(124)
  821. static struct tpm_input_header tpm_readpubek_header = {
  822. .tag = TPM_TAG_RQU_COMMAND,
  823. .length = cpu_to_be32(30),
  824. .ordinal = TPM_ORD_READPUBEK
  825. };
  826. ssize_t tpm_show_pubek(struct device *dev, struct device_attribute *attr,
  827. char *buf)
  828. {
  829. u8 *data;
  830. struct tpm_cmd_t tpm_cmd;
  831. ssize_t err;
  832. int i, rc;
  833. char *str = buf;
  834. struct tpm_chip *chip = dev_get_drvdata(dev);
  835. tpm_cmd.header.in = tpm_readpubek_header;
  836. err = transmit_cmd(chip, &tpm_cmd, READ_PUBEK_RESULT_SIZE,
  837. "attempting to read the PUBEK");
  838. if (err)
  839. goto out;
  840. /*
  841. ignore header 10 bytes
  842. algorithm 32 bits (1 == RSA )
  843. encscheme 16 bits
  844. sigscheme 16 bits
  845. parameters (RSA 12->bytes: keybit, #primes, expbit)
  846. keylenbytes 32 bits
  847. 256 byte modulus
  848. ignore checksum 20 bytes
  849. */
  850. data = tpm_cmd.params.readpubek_out_buffer;
  851. str +=
  852. sprintf(str,
  853. "Algorithm: %02X %02X %02X %02X\n"
  854. "Encscheme: %02X %02X\n"
  855. "Sigscheme: %02X %02X\n"
  856. "Parameters: %02X %02X %02X %02X "
  857. "%02X %02X %02X %02X "
  858. "%02X %02X %02X %02X\n"
  859. "Modulus length: %d\n"
  860. "Modulus:\n",
  861. data[0], data[1], data[2], data[3],
  862. data[4], data[5],
  863. data[6], data[7],
  864. data[12], data[13], data[14], data[15],
  865. data[16], data[17], data[18], data[19],
  866. data[20], data[21], data[22], data[23],
  867. be32_to_cpu(*((__be32 *) (data + 24))));
  868. for (i = 0; i < 256; i++) {
  869. str += sprintf(str, "%02X ", data[i + 28]);
  870. if ((i + 1) % 16 == 0)
  871. str += sprintf(str, "\n");
  872. }
  873. out:
  874. rc = str - buf;
  875. return rc;
  876. }
  877. EXPORT_SYMBOL_GPL(tpm_show_pubek);
  878. ssize_t tpm_show_caps(struct device *dev, struct device_attribute *attr,
  879. char *buf)
  880. {
  881. cap_t cap;
  882. ssize_t rc;
  883. char *str = buf;
  884. rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
  885. "attempting to determine the manufacturer");
  886. if (rc)
  887. return 0;
  888. str += sprintf(str, "Manufacturer: 0x%x\n",
  889. be32_to_cpu(cap.manufacturer_id));
  890. rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
  891. "attempting to determine the 1.1 version");
  892. if (rc)
  893. return 0;
  894. str += sprintf(str,
  895. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  896. cap.tpm_version.Major, cap.tpm_version.Minor,
  897. cap.tpm_version.revMajor, cap.tpm_version.revMinor);
  898. return str - buf;
  899. }
  900. EXPORT_SYMBOL_GPL(tpm_show_caps);
  901. ssize_t tpm_show_caps_1_2(struct device * dev,
  902. struct device_attribute * attr, char *buf)
  903. {
  904. cap_t cap;
  905. ssize_t rc;
  906. char *str = buf;
  907. rc = tpm_getcap(dev, TPM_CAP_PROP_MANUFACTURER, &cap,
  908. "attempting to determine the manufacturer");
  909. if (rc)
  910. return 0;
  911. str += sprintf(str, "Manufacturer: 0x%x\n",
  912. be32_to_cpu(cap.manufacturer_id));
  913. rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
  914. "attempting to determine the 1.2 version");
  915. if (rc)
  916. return 0;
  917. str += sprintf(str,
  918. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  919. cap.tpm_version_1_2.Major, cap.tpm_version_1_2.Minor,
  920. cap.tpm_version_1_2.revMajor,
  921. cap.tpm_version_1_2.revMinor);
  922. return str - buf;
  923. }
  924. EXPORT_SYMBOL_GPL(tpm_show_caps_1_2);
  925. ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
  926. char *buf)
  927. {
  928. struct tpm_chip *chip = dev_get_drvdata(dev);
  929. if (chip->vendor.duration[TPM_LONG] == 0)
  930. return 0;
  931. return sprintf(buf, "%d %d %d [%s]\n",
  932. jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
  933. jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
  934. jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
  935. chip->vendor.duration_adjusted
  936. ? "adjusted" : "original");
  937. }
  938. EXPORT_SYMBOL_GPL(tpm_show_durations);
  939. ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
  940. char *buf)
  941. {
  942. struct tpm_chip *chip = dev_get_drvdata(dev);
  943. return sprintf(buf, "%d %d %d %d [%s]\n",
  944. jiffies_to_usecs(chip->vendor.timeout_a),
  945. jiffies_to_usecs(chip->vendor.timeout_b),
  946. jiffies_to_usecs(chip->vendor.timeout_c),
  947. jiffies_to_usecs(chip->vendor.timeout_d),
  948. chip->vendor.timeout_adjusted
  949. ? "adjusted" : "original");
  950. }
  951. EXPORT_SYMBOL_GPL(tpm_show_timeouts);
  952. ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
  953. const char *buf, size_t count)
  954. {
  955. struct tpm_chip *chip = dev_get_drvdata(dev);
  956. if (chip == NULL)
  957. return 0;
  958. chip->vendor.cancel(chip);
  959. return count;
  960. }
  961. EXPORT_SYMBOL_GPL(tpm_store_cancel);
  962. int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  963. wait_queue_head_t *queue)
  964. {
  965. unsigned long stop;
  966. long rc;
  967. u8 status;
  968. /* check current status */
  969. status = chip->vendor.status(chip);
  970. if ((status & mask) == mask)
  971. return 0;
  972. stop = jiffies + timeout;
  973. if (chip->vendor.irq) {
  974. again:
  975. timeout = stop - jiffies;
  976. if ((long)timeout <= 0)
  977. return -ETIME;
  978. rc = wait_event_interruptible_timeout(*queue,
  979. ((chip->vendor.status(chip)
  980. & mask) == mask),
  981. timeout);
  982. if (rc > 0)
  983. return 0;
  984. if (rc == -ERESTARTSYS && freezing(current)) {
  985. clear_thread_flag(TIF_SIGPENDING);
  986. goto again;
  987. }
  988. } else {
  989. do {
  990. msleep(TPM_TIMEOUT);
  991. status = chip->vendor.status(chip);
  992. if ((status & mask) == mask)
  993. return 0;
  994. } while (time_before(jiffies, stop));
  995. }
  996. return -ETIME;
  997. }
  998. EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
  999. /*
  1000. * Device file system interface to the TPM
  1001. *
  1002. * It's assured that the chip will be opened just once,
  1003. * by the check of is_open variable, which is protected
  1004. * by driver_lock.
  1005. */
  1006. int tpm_open(struct inode *inode, struct file *file)
  1007. {
  1008. int minor = iminor(inode);
  1009. struct tpm_chip *chip = NULL, *pos;
  1010. rcu_read_lock();
  1011. list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
  1012. if (pos->vendor.miscdev.minor == minor) {
  1013. chip = pos;
  1014. get_device(chip->dev);
  1015. break;
  1016. }
  1017. }
  1018. rcu_read_unlock();
  1019. if (!chip)
  1020. return -ENODEV;
  1021. if (test_and_set_bit(0, &chip->is_open)) {
  1022. dev_dbg(chip->dev, "Another process owns this TPM\n");
  1023. put_device(chip->dev);
  1024. return -EBUSY;
  1025. }
  1026. chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
  1027. if (chip->data_buffer == NULL) {
  1028. clear_bit(0, &chip->is_open);
  1029. put_device(chip->dev);
  1030. return -ENOMEM;
  1031. }
  1032. atomic_set(&chip->data_pending, 0);
  1033. file->private_data = chip;
  1034. return 0;
  1035. }
  1036. EXPORT_SYMBOL_GPL(tpm_open);
  1037. /*
  1038. * Called on file close
  1039. */
  1040. int tpm_release(struct inode *inode, struct file *file)
  1041. {
  1042. struct tpm_chip *chip = file->private_data;
  1043. del_singleshot_timer_sync(&chip->user_read_timer);
  1044. flush_work(&chip->work);
  1045. file->private_data = NULL;
  1046. atomic_set(&chip->data_pending, 0);
  1047. kzfree(chip->data_buffer);
  1048. clear_bit(0, &chip->is_open);
  1049. put_device(chip->dev);
  1050. return 0;
  1051. }
  1052. EXPORT_SYMBOL_GPL(tpm_release);
  1053. ssize_t tpm_write(struct file *file, const char __user *buf,
  1054. size_t size, loff_t *off)
  1055. {
  1056. struct tpm_chip *chip = file->private_data;
  1057. size_t in_size = size, out_size;
  1058. /* cannot perform a write until the read has cleared
  1059. either via tpm_read or a user_read_timer timeout */
  1060. while (atomic_read(&chip->data_pending) != 0)
  1061. msleep(TPM_TIMEOUT);
  1062. mutex_lock(&chip->buffer_mutex);
  1063. if (in_size > TPM_BUFSIZE)
  1064. in_size = TPM_BUFSIZE;
  1065. if (copy_from_user
  1066. (chip->data_buffer, (void __user *) buf, in_size)) {
  1067. mutex_unlock(&chip->buffer_mutex);
  1068. return -EFAULT;
  1069. }
  1070. /* atomic tpm command send and result receive */
  1071. out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
  1072. atomic_set(&chip->data_pending, out_size);
  1073. mutex_unlock(&chip->buffer_mutex);
  1074. /* Set a timeout by which the reader must come claim the result */
  1075. mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
  1076. return in_size;
  1077. }
  1078. EXPORT_SYMBOL_GPL(tpm_write);
  1079. ssize_t tpm_read(struct file *file, char __user *buf,
  1080. size_t size, loff_t *off)
  1081. {
  1082. struct tpm_chip *chip = file->private_data;
  1083. ssize_t ret_size;
  1084. int rc;
  1085. del_singleshot_timer_sync(&chip->user_read_timer);
  1086. flush_work(&chip->work);
  1087. ret_size = atomic_read(&chip->data_pending);
  1088. if (ret_size > 0) { /* relay data */
  1089. ssize_t orig_ret_size = ret_size;
  1090. if (size < ret_size)
  1091. ret_size = size;
  1092. mutex_lock(&chip->buffer_mutex);
  1093. rc = copy_to_user(buf, chip->data_buffer, ret_size);
  1094. memset(chip->data_buffer, 0, orig_ret_size);
  1095. if (rc)
  1096. ret_size = -EFAULT;
  1097. mutex_unlock(&chip->buffer_mutex);
  1098. }
  1099. atomic_set(&chip->data_pending, 0);
  1100. return ret_size;
  1101. }
  1102. EXPORT_SYMBOL_GPL(tpm_read);
  1103. void tpm_remove_hardware(struct device *dev)
  1104. {
  1105. struct tpm_chip *chip = dev_get_drvdata(dev);
  1106. if (chip == NULL) {
  1107. dev_err(dev, "No device data found\n");
  1108. return;
  1109. }
  1110. spin_lock(&driver_lock);
  1111. list_del_rcu(&chip->list);
  1112. spin_unlock(&driver_lock);
  1113. synchronize_rcu();
  1114. misc_deregister(&chip->vendor.miscdev);
  1115. sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
  1116. tpm_remove_ppi(&dev->kobj);
  1117. tpm_bios_log_teardown(chip->bios_dir);
  1118. /* write it this way to be explicit (chip->dev == dev) */
  1119. put_device(chip->dev);
  1120. }
  1121. EXPORT_SYMBOL_GPL(tpm_remove_hardware);
  1122. #define TPM_ORD_SAVESTATE cpu_to_be32(152)
  1123. #define SAVESTATE_RESULT_SIZE 10
  1124. static struct tpm_input_header savestate_header = {
  1125. .tag = TPM_TAG_RQU_COMMAND,
  1126. .length = cpu_to_be32(10),
  1127. .ordinal = TPM_ORD_SAVESTATE
  1128. };
  1129. /*
  1130. * We are about to suspend. Save the TPM state
  1131. * so that it can be restored.
  1132. */
  1133. int tpm_pm_suspend(struct device *dev)
  1134. {
  1135. struct tpm_chip *chip = dev_get_drvdata(dev);
  1136. struct tpm_cmd_t cmd;
  1137. int rc;
  1138. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  1139. if (chip == NULL)
  1140. return -ENODEV;
  1141. /* for buggy tpm, flush pcrs with extend to selected dummy */
  1142. if (tpm_suspend_pcr) {
  1143. cmd.header.in = pcrextend_header;
  1144. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
  1145. memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
  1146. TPM_DIGEST_SIZE);
  1147. rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  1148. "extending dummy pcr before suspend");
  1149. }
  1150. /* now do the actual savestate */
  1151. cmd.header.in = savestate_header;
  1152. rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
  1153. "sending savestate before suspend");
  1154. return rc;
  1155. }
  1156. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  1157. /*
  1158. * Resume from a power safe. The BIOS already restored
  1159. * the TPM state.
  1160. */
  1161. int tpm_pm_resume(struct device *dev)
  1162. {
  1163. struct tpm_chip *chip = dev_get_drvdata(dev);
  1164. if (chip == NULL)
  1165. return -ENODEV;
  1166. return 0;
  1167. }
  1168. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  1169. #define TPM_GETRANDOM_RESULT_SIZE 18
  1170. static struct tpm_input_header tpm_getrandom_header = {
  1171. .tag = TPM_TAG_RQU_COMMAND,
  1172. .length = cpu_to_be32(14),
  1173. .ordinal = TPM_ORD_GET_RANDOM
  1174. };
  1175. /**
  1176. * tpm_get_random() - Get random bytes from the tpm's RNG
  1177. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  1178. * @out: destination buffer for the random bytes
  1179. * @max: the max number of bytes to write to @out
  1180. *
  1181. * Returns < 0 on error and the number of bytes read on success
  1182. */
  1183. int tpm_get_random(u32 chip_num, u8 *out, size_t max)
  1184. {
  1185. struct tpm_chip *chip;
  1186. struct tpm_cmd_t tpm_cmd;
  1187. u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
  1188. int err, total = 0, retries = 5;
  1189. u8 *dest = out;
  1190. chip = tpm_chip_find_get(chip_num);
  1191. if (chip == NULL)
  1192. return -ENODEV;
  1193. if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
  1194. return -EINVAL;
  1195. do {
  1196. tpm_cmd.header.in = tpm_getrandom_header;
  1197. tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
  1198. err = transmit_cmd(chip, &tpm_cmd,
  1199. TPM_GETRANDOM_RESULT_SIZE + num_bytes,
  1200. "attempting get random");
  1201. if (err)
  1202. break;
  1203. recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
  1204. memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
  1205. dest += recd;
  1206. total += recd;
  1207. num_bytes -= recd;
  1208. } while (retries-- && total < max);
  1209. return total ? total : -EIO;
  1210. }
  1211. EXPORT_SYMBOL_GPL(tpm_get_random);
  1212. /* In case vendor provided release function, call it too.*/
  1213. void tpm_dev_vendor_release(struct tpm_chip *chip)
  1214. {
  1215. if (!chip)
  1216. return;
  1217. if (chip->vendor.release)
  1218. chip->vendor.release(chip->dev);
  1219. clear_bit(chip->dev_num, dev_mask);
  1220. kfree(chip->vendor.miscdev.name);
  1221. }
  1222. EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
  1223. /*
  1224. * Once all references to platform device are down to 0,
  1225. * release all allocated structures.
  1226. */
  1227. static void tpm_dev_release(struct device *dev)
  1228. {
  1229. struct tpm_chip *chip = dev_get_drvdata(dev);
  1230. if (!chip)
  1231. return;
  1232. tpm_dev_vendor_release(chip);
  1233. chip->release(dev);
  1234. kfree(chip);
  1235. }
  1236. EXPORT_SYMBOL_GPL(tpm_dev_release);
  1237. /*
  1238. * Called from tpm_<specific>.c probe function only for devices
  1239. * the driver has determined it should claim. Prior to calling
  1240. * this function the specific probe function has called pci_enable_device
  1241. * upon errant exit from this function specific probe function should call
  1242. * pci_disable_device
  1243. */
  1244. struct tpm_chip *tpm_register_hardware(struct device *dev,
  1245. const struct tpm_vendor_specific *entry)
  1246. {
  1247. #define DEVNAME_SIZE 7
  1248. char *devname;
  1249. struct tpm_chip *chip;
  1250. /* Driver specific per-device data */
  1251. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1252. devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
  1253. if (chip == NULL || devname == NULL)
  1254. goto out_free;
  1255. mutex_init(&chip->buffer_mutex);
  1256. mutex_init(&chip->tpm_mutex);
  1257. INIT_LIST_HEAD(&chip->list);
  1258. INIT_WORK(&chip->work, timeout_work);
  1259. setup_timer(&chip->user_read_timer, user_reader_timeout,
  1260. (unsigned long)chip);
  1261. memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
  1262. chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
  1263. if (chip->dev_num >= TPM_NUM_DEVICES) {
  1264. dev_err(dev, "No available tpm device numbers\n");
  1265. goto out_free;
  1266. } else if (chip->dev_num == 0)
  1267. chip->vendor.miscdev.minor = TPM_MINOR;
  1268. else
  1269. chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
  1270. set_bit(chip->dev_num, dev_mask);
  1271. scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
  1272. chip->vendor.miscdev.name = devname;
  1273. chip->vendor.miscdev.parent = dev;
  1274. chip->dev = get_device(dev);
  1275. chip->release = dev->release;
  1276. dev->release = tpm_dev_release;
  1277. dev_set_drvdata(dev, chip);
  1278. if (misc_register(&chip->vendor.miscdev)) {
  1279. dev_err(chip->dev,
  1280. "unable to misc_register %s, minor %d\n",
  1281. chip->vendor.miscdev.name,
  1282. chip->vendor.miscdev.minor);
  1283. goto put_device;
  1284. }
  1285. if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
  1286. misc_deregister(&chip->vendor.miscdev);
  1287. goto put_device;
  1288. }
  1289. if (tpm_add_ppi(&dev->kobj)) {
  1290. misc_deregister(&chip->vendor.miscdev);
  1291. goto put_device;
  1292. }
  1293. chip->bios_dir = tpm_bios_log_setup(devname);
  1294. /* Make chip available */
  1295. spin_lock(&driver_lock);
  1296. list_add_rcu(&chip->list, &tpm_chip_list);
  1297. spin_unlock(&driver_lock);
  1298. return chip;
  1299. put_device:
  1300. put_device(chip->dev);
  1301. out_free:
  1302. kfree(chip);
  1303. kfree(devname);
  1304. return NULL;
  1305. }
  1306. EXPORT_SYMBOL_GPL(tpm_register_hardware);
  1307. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1308. MODULE_DESCRIPTION("TPM Driver");
  1309. MODULE_VERSION("2.0");
  1310. MODULE_LICENSE("GPL");