tpm.c 38 KB

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