tpm.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  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;
  1058. ssize_t out_size;
  1059. /* cannot perform a write until the read has cleared
  1060. either via tpm_read or a user_read_timer timeout.
  1061. This also prevents splitted buffered writes from blocking here.
  1062. */
  1063. if (atomic_read(&chip->data_pending) != 0)
  1064. return -EBUSY;
  1065. if (in_size > TPM_BUFSIZE)
  1066. return -E2BIG;
  1067. mutex_lock(&chip->buffer_mutex);
  1068. if (copy_from_user
  1069. (chip->data_buffer, (void __user *) buf, in_size)) {
  1070. mutex_unlock(&chip->buffer_mutex);
  1071. return -EFAULT;
  1072. }
  1073. /* atomic tpm command send and result receive */
  1074. out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
  1075. if (out_size < 0) {
  1076. mutex_unlock(&chip->buffer_mutex);
  1077. return out_size;
  1078. }
  1079. atomic_set(&chip->data_pending, out_size);
  1080. mutex_unlock(&chip->buffer_mutex);
  1081. /* Set a timeout by which the reader must come claim the result */
  1082. mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
  1083. return in_size;
  1084. }
  1085. EXPORT_SYMBOL_GPL(tpm_write);
  1086. ssize_t tpm_read(struct file *file, char __user *buf,
  1087. size_t size, loff_t *off)
  1088. {
  1089. struct tpm_chip *chip = file->private_data;
  1090. ssize_t ret_size;
  1091. int rc;
  1092. del_singleshot_timer_sync(&chip->user_read_timer);
  1093. flush_work(&chip->work);
  1094. ret_size = atomic_read(&chip->data_pending);
  1095. if (ret_size > 0) { /* relay data */
  1096. ssize_t orig_ret_size = ret_size;
  1097. if (size < ret_size)
  1098. ret_size = size;
  1099. mutex_lock(&chip->buffer_mutex);
  1100. rc = copy_to_user(buf, chip->data_buffer, ret_size);
  1101. memset(chip->data_buffer, 0, orig_ret_size);
  1102. if (rc)
  1103. ret_size = -EFAULT;
  1104. mutex_unlock(&chip->buffer_mutex);
  1105. }
  1106. atomic_set(&chip->data_pending, 0);
  1107. return ret_size;
  1108. }
  1109. EXPORT_SYMBOL_GPL(tpm_read);
  1110. void tpm_remove_hardware(struct device *dev)
  1111. {
  1112. struct tpm_chip *chip = dev_get_drvdata(dev);
  1113. if (chip == NULL) {
  1114. dev_err(dev, "No device data found\n");
  1115. return;
  1116. }
  1117. spin_lock(&driver_lock);
  1118. list_del_rcu(&chip->list);
  1119. spin_unlock(&driver_lock);
  1120. synchronize_rcu();
  1121. misc_deregister(&chip->vendor.miscdev);
  1122. sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
  1123. tpm_remove_ppi(&dev->kobj);
  1124. tpm_bios_log_teardown(chip->bios_dir);
  1125. /* write it this way to be explicit (chip->dev == dev) */
  1126. put_device(chip->dev);
  1127. }
  1128. EXPORT_SYMBOL_GPL(tpm_remove_hardware);
  1129. #define TPM_ORD_SAVESTATE cpu_to_be32(152)
  1130. #define SAVESTATE_RESULT_SIZE 10
  1131. static struct tpm_input_header savestate_header = {
  1132. .tag = TPM_TAG_RQU_COMMAND,
  1133. .length = cpu_to_be32(10),
  1134. .ordinal = TPM_ORD_SAVESTATE
  1135. };
  1136. /*
  1137. * We are about to suspend. Save the TPM state
  1138. * so that it can be restored.
  1139. */
  1140. int tpm_pm_suspend(struct device *dev)
  1141. {
  1142. struct tpm_chip *chip = dev_get_drvdata(dev);
  1143. struct tpm_cmd_t cmd;
  1144. int rc;
  1145. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  1146. if (chip == NULL)
  1147. return -ENODEV;
  1148. /* for buggy tpm, flush pcrs with extend to selected dummy */
  1149. if (tpm_suspend_pcr) {
  1150. cmd.header.in = pcrextend_header;
  1151. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
  1152. memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
  1153. TPM_DIGEST_SIZE);
  1154. rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  1155. "extending dummy pcr before suspend");
  1156. }
  1157. /* now do the actual savestate */
  1158. cmd.header.in = savestate_header;
  1159. rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE,
  1160. "sending savestate before suspend");
  1161. return rc;
  1162. }
  1163. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  1164. /*
  1165. * Resume from a power safe. The BIOS already restored
  1166. * the TPM state.
  1167. */
  1168. int tpm_pm_resume(struct device *dev)
  1169. {
  1170. struct tpm_chip *chip = dev_get_drvdata(dev);
  1171. if (chip == NULL)
  1172. return -ENODEV;
  1173. return 0;
  1174. }
  1175. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  1176. #define TPM_GETRANDOM_RESULT_SIZE 18
  1177. static struct tpm_input_header tpm_getrandom_header = {
  1178. .tag = TPM_TAG_RQU_COMMAND,
  1179. .length = cpu_to_be32(14),
  1180. .ordinal = TPM_ORD_GET_RANDOM
  1181. };
  1182. /**
  1183. * tpm_get_random() - Get random bytes from the tpm's RNG
  1184. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  1185. * @out: destination buffer for the random bytes
  1186. * @max: the max number of bytes to write to @out
  1187. *
  1188. * Returns < 0 on error and the number of bytes read on success
  1189. */
  1190. int tpm_get_random(u32 chip_num, u8 *out, size_t max)
  1191. {
  1192. struct tpm_chip *chip;
  1193. struct tpm_cmd_t tpm_cmd;
  1194. u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
  1195. int err, total = 0, retries = 5;
  1196. u8 *dest = out;
  1197. chip = tpm_chip_find_get(chip_num);
  1198. if (chip == NULL)
  1199. return -ENODEV;
  1200. if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
  1201. return -EINVAL;
  1202. do {
  1203. tpm_cmd.header.in = tpm_getrandom_header;
  1204. tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
  1205. err = transmit_cmd(chip, &tpm_cmd,
  1206. TPM_GETRANDOM_RESULT_SIZE + num_bytes,
  1207. "attempting get random");
  1208. if (err)
  1209. break;
  1210. recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
  1211. memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
  1212. dest += recd;
  1213. total += recd;
  1214. num_bytes -= recd;
  1215. } while (retries-- && total < max);
  1216. return total ? total : -EIO;
  1217. }
  1218. EXPORT_SYMBOL_GPL(tpm_get_random);
  1219. /* In case vendor provided release function, call it too.*/
  1220. void tpm_dev_vendor_release(struct tpm_chip *chip)
  1221. {
  1222. if (!chip)
  1223. return;
  1224. if (chip->vendor.release)
  1225. chip->vendor.release(chip->dev);
  1226. clear_bit(chip->dev_num, dev_mask);
  1227. kfree(chip->vendor.miscdev.name);
  1228. }
  1229. EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
  1230. /*
  1231. * Once all references to platform device are down to 0,
  1232. * release all allocated structures.
  1233. */
  1234. static void tpm_dev_release(struct device *dev)
  1235. {
  1236. struct tpm_chip *chip = dev_get_drvdata(dev);
  1237. if (!chip)
  1238. return;
  1239. tpm_dev_vendor_release(chip);
  1240. chip->release(dev);
  1241. kfree(chip);
  1242. }
  1243. EXPORT_SYMBOL_GPL(tpm_dev_release);
  1244. /*
  1245. * Called from tpm_<specific>.c probe function only for devices
  1246. * the driver has determined it should claim. Prior to calling
  1247. * this function the specific probe function has called pci_enable_device
  1248. * upon errant exit from this function specific probe function should call
  1249. * pci_disable_device
  1250. */
  1251. struct tpm_chip *tpm_register_hardware(struct device *dev,
  1252. const struct tpm_vendor_specific *entry)
  1253. {
  1254. #define DEVNAME_SIZE 7
  1255. char *devname;
  1256. struct tpm_chip *chip;
  1257. /* Driver specific per-device data */
  1258. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1259. devname = kmalloc(DEVNAME_SIZE, GFP_KERNEL);
  1260. if (chip == NULL || devname == NULL)
  1261. goto out_free;
  1262. mutex_init(&chip->buffer_mutex);
  1263. mutex_init(&chip->tpm_mutex);
  1264. INIT_LIST_HEAD(&chip->list);
  1265. INIT_WORK(&chip->work, timeout_work);
  1266. setup_timer(&chip->user_read_timer, user_reader_timeout,
  1267. (unsigned long)chip);
  1268. memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
  1269. chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
  1270. if (chip->dev_num >= TPM_NUM_DEVICES) {
  1271. dev_err(dev, "No available tpm device numbers\n");
  1272. goto out_free;
  1273. } else if (chip->dev_num == 0)
  1274. chip->vendor.miscdev.minor = TPM_MINOR;
  1275. else
  1276. chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
  1277. set_bit(chip->dev_num, dev_mask);
  1278. scnprintf(devname, DEVNAME_SIZE, "%s%d", "tpm", chip->dev_num);
  1279. chip->vendor.miscdev.name = devname;
  1280. chip->vendor.miscdev.parent = dev;
  1281. chip->dev = get_device(dev);
  1282. chip->release = dev->release;
  1283. dev->release = tpm_dev_release;
  1284. dev_set_drvdata(dev, chip);
  1285. if (misc_register(&chip->vendor.miscdev)) {
  1286. dev_err(chip->dev,
  1287. "unable to misc_register %s, minor %d\n",
  1288. chip->vendor.miscdev.name,
  1289. chip->vendor.miscdev.minor);
  1290. goto put_device;
  1291. }
  1292. if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
  1293. misc_deregister(&chip->vendor.miscdev);
  1294. goto put_device;
  1295. }
  1296. if (tpm_add_ppi(&dev->kobj)) {
  1297. misc_deregister(&chip->vendor.miscdev);
  1298. goto put_device;
  1299. }
  1300. chip->bios_dir = tpm_bios_log_setup(devname);
  1301. /* Make chip available */
  1302. spin_lock(&driver_lock);
  1303. list_add_rcu(&chip->list, &tpm_chip_list);
  1304. spin_unlock(&driver_lock);
  1305. return chip;
  1306. put_device:
  1307. put_device(chip->dev);
  1308. out_free:
  1309. kfree(chip);
  1310. kfree(devname);
  1311. return NULL;
  1312. }
  1313. EXPORT_SYMBOL_GPL(tpm_register_hardware);
  1314. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1315. MODULE_DESCRIPTION("TPM Driver");
  1316. MODULE_VERSION("2.0");
  1317. MODULE_LICENSE("GPL");