tpm.c 35 KB

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