tpm-interface.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  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. rc = chip->vendor.send(chip, (u8 *) buf, count);
  362. if (rc < 0) {
  363. dev_err(chip->dev,
  364. "tpm_transmit: tpm_send: error %zd\n", rc);
  365. goto out;
  366. }
  367. if (chip->vendor.irq)
  368. goto out_recv;
  369. stop = jiffies + tpm_calc_ordinal_duration(chip, ordinal);
  370. do {
  371. u8 status = chip->vendor.status(chip);
  372. if ((status & chip->vendor.req_complete_mask) ==
  373. chip->vendor.req_complete_val)
  374. goto out_recv;
  375. if (chip->vendor.req_canceled(chip, status)) {
  376. dev_err(chip->dev, "Operation Canceled\n");
  377. rc = -ECANCELED;
  378. goto out;
  379. }
  380. msleep(TPM_TIMEOUT); /* CHECK */
  381. rmb();
  382. } while (time_before(jiffies, stop));
  383. chip->vendor.cancel(chip);
  384. dev_err(chip->dev, "Operation Timed out\n");
  385. rc = -ETIME;
  386. goto out;
  387. out_recv:
  388. rc = chip->vendor.recv(chip, (u8 *) buf, bufsiz);
  389. if (rc < 0)
  390. dev_err(chip->dev,
  391. "tpm_transmit: tpm_recv: error %zd\n", rc);
  392. out:
  393. mutex_unlock(&chip->tpm_mutex);
  394. return rc;
  395. }
  396. #define TPM_DIGEST_SIZE 20
  397. #define TPM_RET_CODE_IDX 6
  398. enum tpm_capabilities {
  399. TPM_CAP_FLAG = cpu_to_be32(4),
  400. TPM_CAP_PROP = cpu_to_be32(5),
  401. CAP_VERSION_1_1 = cpu_to_be32(0x06),
  402. CAP_VERSION_1_2 = cpu_to_be32(0x1A)
  403. };
  404. enum tpm_sub_capabilities {
  405. TPM_CAP_PROP_PCR = cpu_to_be32(0x101),
  406. TPM_CAP_PROP_MANUFACTURER = cpu_to_be32(0x103),
  407. TPM_CAP_FLAG_PERM = cpu_to_be32(0x108),
  408. TPM_CAP_FLAG_VOL = cpu_to_be32(0x109),
  409. TPM_CAP_PROP_OWNER = cpu_to_be32(0x111),
  410. TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
  411. TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
  412. };
  413. static ssize_t transmit_cmd(struct tpm_chip *chip, struct tpm_cmd_t *cmd,
  414. int len, const char *desc)
  415. {
  416. int err;
  417. len = tpm_transmit(chip, (u8 *) cmd, len);
  418. if (len < 0)
  419. return len;
  420. else if (len < TPM_HEADER_SIZE)
  421. return -EFAULT;
  422. err = be32_to_cpu(cmd->header.out.return_code);
  423. if (err != 0 && desc)
  424. dev_err(chip->dev, "A TPM error (%d) occurred %s\n", err, desc);
  425. return err;
  426. }
  427. #define TPM_INTERNAL_RESULT_SIZE 200
  428. #define TPM_TAG_RQU_COMMAND cpu_to_be16(193)
  429. #define TPM_ORD_GET_CAP cpu_to_be32(101)
  430. #define TPM_ORD_GET_RANDOM cpu_to_be32(70)
  431. static const struct tpm_input_header tpm_getcap_header = {
  432. .tag = TPM_TAG_RQU_COMMAND,
  433. .length = cpu_to_be32(22),
  434. .ordinal = TPM_ORD_GET_CAP
  435. };
  436. ssize_t tpm_getcap(struct device *dev, __be32 subcap_id, cap_t *cap,
  437. const char *desc)
  438. {
  439. struct tpm_cmd_t tpm_cmd;
  440. int rc;
  441. struct tpm_chip *chip = dev_get_drvdata(dev);
  442. tpm_cmd.header.in = tpm_getcap_header;
  443. if (subcap_id == CAP_VERSION_1_1 || subcap_id == CAP_VERSION_1_2) {
  444. tpm_cmd.params.getcap_in.cap = subcap_id;
  445. /*subcap field not necessary */
  446. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(0);
  447. tpm_cmd.header.in.length -= cpu_to_be32(sizeof(__be32));
  448. } else {
  449. if (subcap_id == TPM_CAP_FLAG_PERM ||
  450. subcap_id == TPM_CAP_FLAG_VOL)
  451. tpm_cmd.params.getcap_in.cap = TPM_CAP_FLAG;
  452. else
  453. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  454. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  455. tpm_cmd.params.getcap_in.subcap = subcap_id;
  456. }
  457. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, desc);
  458. if (!rc)
  459. *cap = tpm_cmd.params.getcap_out.cap;
  460. return rc;
  461. }
  462. void tpm_gen_interrupt(struct tpm_chip *chip)
  463. {
  464. struct tpm_cmd_t tpm_cmd;
  465. ssize_t rc;
  466. tpm_cmd.header.in = tpm_getcap_header;
  467. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  468. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  469. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  470. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  471. "attempting to determine the timeouts");
  472. }
  473. EXPORT_SYMBOL_GPL(tpm_gen_interrupt);
  474. #define TPM_ORD_STARTUP cpu_to_be32(153)
  475. #define TPM_ST_CLEAR cpu_to_be16(1)
  476. #define TPM_ST_STATE cpu_to_be16(2)
  477. #define TPM_ST_DEACTIVATED cpu_to_be16(3)
  478. static const struct tpm_input_header tpm_startup_header = {
  479. .tag = TPM_TAG_RQU_COMMAND,
  480. .length = cpu_to_be32(12),
  481. .ordinal = TPM_ORD_STARTUP
  482. };
  483. static int tpm_startup(struct tpm_chip *chip, __be16 startup_type)
  484. {
  485. struct tpm_cmd_t start_cmd;
  486. start_cmd.header.in = tpm_startup_header;
  487. start_cmd.params.startup_in.startup_type = startup_type;
  488. return transmit_cmd(chip, &start_cmd, TPM_INTERNAL_RESULT_SIZE,
  489. "attempting to start the TPM");
  490. }
  491. int tpm_get_timeouts(struct tpm_chip *chip)
  492. {
  493. struct tpm_cmd_t tpm_cmd;
  494. struct timeout_t *timeout_cap;
  495. struct duration_t *duration_cap;
  496. ssize_t rc;
  497. u32 timeout;
  498. unsigned int scale = 1;
  499. tpm_cmd.header.in = tpm_getcap_header;
  500. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  501. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  502. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  503. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE, NULL);
  504. if (rc == TPM_ERR_INVALID_POSTINIT) {
  505. /* The TPM is not started, we are the first to talk to it.
  506. Execute a startup command. */
  507. dev_info(chip->dev, "Issuing TPM_STARTUP");
  508. if (tpm_startup(chip, TPM_ST_CLEAR))
  509. return rc;
  510. tpm_cmd.header.in = tpm_getcap_header;
  511. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  512. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  513. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_TIMEOUT;
  514. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  515. NULL);
  516. }
  517. if (rc) {
  518. dev_err(chip->dev,
  519. "A TPM error (%zd) occurred attempting to determine the timeouts\n",
  520. rc);
  521. goto duration;
  522. }
  523. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  524. be32_to_cpu(tpm_cmd.header.out.length)
  525. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 4 * sizeof(u32))
  526. return -EINVAL;
  527. timeout_cap = &tpm_cmd.params.getcap_out.cap.timeout;
  528. /* Don't overwrite default if value is 0 */
  529. timeout = be32_to_cpu(timeout_cap->a);
  530. if (timeout && timeout < 1000) {
  531. /* timeouts in msec rather usec */
  532. scale = 1000;
  533. chip->vendor.timeout_adjusted = true;
  534. }
  535. if (timeout)
  536. chip->vendor.timeout_a = usecs_to_jiffies(timeout * scale);
  537. timeout = be32_to_cpu(timeout_cap->b);
  538. if (timeout)
  539. chip->vendor.timeout_b = usecs_to_jiffies(timeout * scale);
  540. timeout = be32_to_cpu(timeout_cap->c);
  541. if (timeout)
  542. chip->vendor.timeout_c = usecs_to_jiffies(timeout * scale);
  543. timeout = be32_to_cpu(timeout_cap->d);
  544. if (timeout)
  545. chip->vendor.timeout_d = usecs_to_jiffies(timeout * scale);
  546. duration:
  547. tpm_cmd.header.in = tpm_getcap_header;
  548. tpm_cmd.params.getcap_in.cap = TPM_CAP_PROP;
  549. tpm_cmd.params.getcap_in.subcap_size = cpu_to_be32(4);
  550. tpm_cmd.params.getcap_in.subcap = TPM_CAP_PROP_TIS_DURATION;
  551. rc = transmit_cmd(chip, &tpm_cmd, TPM_INTERNAL_RESULT_SIZE,
  552. "attempting to determine the durations");
  553. if (rc)
  554. return rc;
  555. if (be32_to_cpu(tpm_cmd.header.out.return_code) != 0 ||
  556. be32_to_cpu(tpm_cmd.header.out.length)
  557. != sizeof(tpm_cmd.header.out) + sizeof(u32) + 3 * sizeof(u32))
  558. return -EINVAL;
  559. duration_cap = &tpm_cmd.params.getcap_out.cap.duration;
  560. chip->vendor.duration[TPM_SHORT] =
  561. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_short));
  562. chip->vendor.duration[TPM_MEDIUM] =
  563. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_medium));
  564. chip->vendor.duration[TPM_LONG] =
  565. usecs_to_jiffies(be32_to_cpu(duration_cap->tpm_long));
  566. /* The Broadcom BCM0102 chipset in a Dell Latitude D820 gets the above
  567. * value wrong and apparently reports msecs rather than usecs. So we
  568. * fix up the resulting too-small TPM_SHORT value to make things work.
  569. * We also scale the TPM_MEDIUM and -_LONG values by 1000.
  570. */
  571. if (chip->vendor.duration[TPM_SHORT] < (HZ / 100)) {
  572. chip->vendor.duration[TPM_SHORT] = HZ;
  573. chip->vendor.duration[TPM_MEDIUM] *= 1000;
  574. chip->vendor.duration[TPM_LONG] *= 1000;
  575. chip->vendor.duration_adjusted = true;
  576. dev_info(chip->dev, "Adjusting TPM timeout parameters.");
  577. }
  578. return 0;
  579. }
  580. EXPORT_SYMBOL_GPL(tpm_get_timeouts);
  581. #define TPM_ORD_CONTINUE_SELFTEST 83
  582. #define CONTINUE_SELFTEST_RESULT_SIZE 10
  583. static struct tpm_input_header continue_selftest_header = {
  584. .tag = TPM_TAG_RQU_COMMAND,
  585. .length = cpu_to_be32(10),
  586. .ordinal = cpu_to_be32(TPM_ORD_CONTINUE_SELFTEST),
  587. };
  588. /**
  589. * tpm_continue_selftest -- run TPM's selftest
  590. * @chip: TPM chip to use
  591. *
  592. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  593. * a TPM error code.
  594. */
  595. static int tpm_continue_selftest(struct tpm_chip *chip)
  596. {
  597. int rc;
  598. struct tpm_cmd_t cmd;
  599. cmd.header.in = continue_selftest_header;
  600. rc = transmit_cmd(chip, &cmd, CONTINUE_SELFTEST_RESULT_SIZE,
  601. "continue selftest");
  602. return rc;
  603. }
  604. ssize_t tpm_show_enabled(struct device *dev, struct device_attribute *attr,
  605. char *buf)
  606. {
  607. cap_t cap;
  608. ssize_t rc;
  609. rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
  610. "attempting to determine the permanent enabled state");
  611. if (rc)
  612. return 0;
  613. rc = sprintf(buf, "%d\n", !cap.perm_flags.disable);
  614. return rc;
  615. }
  616. EXPORT_SYMBOL_GPL(tpm_show_enabled);
  617. ssize_t tpm_show_active(struct device *dev, struct device_attribute *attr,
  618. char *buf)
  619. {
  620. cap_t cap;
  621. ssize_t rc;
  622. rc = tpm_getcap(dev, TPM_CAP_FLAG_PERM, &cap,
  623. "attempting to determine the permanent active state");
  624. if (rc)
  625. return 0;
  626. rc = sprintf(buf, "%d\n", !cap.perm_flags.deactivated);
  627. return rc;
  628. }
  629. EXPORT_SYMBOL_GPL(tpm_show_active);
  630. ssize_t tpm_show_owned(struct device *dev, struct device_attribute *attr,
  631. char *buf)
  632. {
  633. cap_t cap;
  634. ssize_t rc;
  635. rc = tpm_getcap(dev, TPM_CAP_PROP_OWNER, &cap,
  636. "attempting to determine the owner state");
  637. if (rc)
  638. return 0;
  639. rc = sprintf(buf, "%d\n", cap.owned);
  640. return rc;
  641. }
  642. EXPORT_SYMBOL_GPL(tpm_show_owned);
  643. ssize_t tpm_show_temp_deactivated(struct device *dev,
  644. struct device_attribute *attr, char *buf)
  645. {
  646. cap_t cap;
  647. ssize_t rc;
  648. rc = tpm_getcap(dev, TPM_CAP_FLAG_VOL, &cap,
  649. "attempting to determine the temporary state");
  650. if (rc)
  651. return 0;
  652. rc = sprintf(buf, "%d\n", cap.stclear_flags.deactivated);
  653. return rc;
  654. }
  655. EXPORT_SYMBOL_GPL(tpm_show_temp_deactivated);
  656. /*
  657. * tpm_chip_find_get - return tpm_chip for given chip number
  658. */
  659. static struct tpm_chip *tpm_chip_find_get(int chip_num)
  660. {
  661. struct tpm_chip *pos, *chip = NULL;
  662. rcu_read_lock();
  663. list_for_each_entry_rcu(pos, &tpm_chip_list, list) {
  664. if (chip_num != TPM_ANY_NUM && chip_num != pos->dev_num)
  665. continue;
  666. if (try_module_get(pos->dev->driver->owner)) {
  667. chip = pos;
  668. break;
  669. }
  670. }
  671. rcu_read_unlock();
  672. return chip;
  673. }
  674. #define TPM_ORDINAL_PCRREAD cpu_to_be32(21)
  675. #define READ_PCR_RESULT_SIZE 30
  676. static struct tpm_input_header pcrread_header = {
  677. .tag = TPM_TAG_RQU_COMMAND,
  678. .length = cpu_to_be32(14),
  679. .ordinal = TPM_ORDINAL_PCRREAD
  680. };
  681. static int __tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  682. {
  683. int rc;
  684. struct tpm_cmd_t cmd;
  685. cmd.header.in = pcrread_header;
  686. cmd.params.pcrread_in.pcr_idx = cpu_to_be32(pcr_idx);
  687. rc = transmit_cmd(chip, &cmd, READ_PCR_RESULT_SIZE,
  688. "attempting to read a pcr value");
  689. if (rc == 0)
  690. memcpy(res_buf, cmd.params.pcrread_out.pcr_result,
  691. TPM_DIGEST_SIZE);
  692. return rc;
  693. }
  694. /**
  695. * tpm_pcr_read - read a pcr value
  696. * @chip_num: tpm idx # or ANY
  697. * @pcr_idx: pcr idx to retrieve
  698. * @res_buf: TPM_PCR value
  699. * size of res_buf is 20 bytes (or NULL if you don't care)
  700. *
  701. * The TPM driver should be built-in, but for whatever reason it
  702. * isn't, protect against the chip disappearing, by incrementing
  703. * the module usage count.
  704. */
  705. int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
  706. {
  707. struct tpm_chip *chip;
  708. int rc;
  709. chip = tpm_chip_find_get(chip_num);
  710. if (chip == NULL)
  711. return -ENODEV;
  712. rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
  713. tpm_chip_put(chip);
  714. return rc;
  715. }
  716. EXPORT_SYMBOL_GPL(tpm_pcr_read);
  717. /**
  718. * tpm_pcr_extend - extend pcr value with hash
  719. * @chip_num: tpm idx # or AN&
  720. * @pcr_idx: pcr idx to extend
  721. * @hash: hash value used to extend pcr value
  722. *
  723. * The TPM driver should be built-in, but for whatever reason it
  724. * isn't, protect against the chip disappearing, by incrementing
  725. * the module usage count.
  726. */
  727. #define TPM_ORD_PCR_EXTEND cpu_to_be32(20)
  728. #define EXTEND_PCR_RESULT_SIZE 34
  729. static struct tpm_input_header pcrextend_header = {
  730. .tag = TPM_TAG_RQU_COMMAND,
  731. .length = cpu_to_be32(34),
  732. .ordinal = TPM_ORD_PCR_EXTEND
  733. };
  734. int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
  735. {
  736. struct tpm_cmd_t cmd;
  737. int rc;
  738. struct tpm_chip *chip;
  739. chip = tpm_chip_find_get(chip_num);
  740. if (chip == NULL)
  741. return -ENODEV;
  742. cmd.header.in = pcrextend_header;
  743. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  744. memcpy(cmd.params.pcrextend_in.hash, hash, TPM_DIGEST_SIZE);
  745. rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  746. "attempting extend a PCR value");
  747. tpm_chip_put(chip);
  748. return rc;
  749. }
  750. EXPORT_SYMBOL_GPL(tpm_pcr_extend);
  751. /**
  752. * tpm_do_selftest - have the TPM continue its selftest and wait until it
  753. * can receive further commands
  754. * @chip: TPM chip to use
  755. *
  756. * Returns 0 on success, < 0 in case of fatal error or a value > 0 representing
  757. * a TPM error code.
  758. */
  759. int tpm_do_selftest(struct tpm_chip *chip)
  760. {
  761. int rc;
  762. unsigned int loops;
  763. unsigned int delay_msec = 100;
  764. unsigned long duration;
  765. struct tpm_cmd_t cmd;
  766. duration = tpm_calc_ordinal_duration(chip, 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. /* Try to get a TPM version 1.2 TPM_CAP_VERSION_INFO */
  916. rc = tpm_getcap(dev, CAP_VERSION_1_2, &cap,
  917. "attempting to determine the 1.2 version");
  918. if (!rc) {
  919. str += sprintf(str,
  920. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  921. cap.tpm_version_1_2.Major,
  922. cap.tpm_version_1_2.Minor,
  923. cap.tpm_version_1_2.revMajor,
  924. cap.tpm_version_1_2.revMinor);
  925. } else {
  926. /* Otherwise just use TPM_STRUCT_VER */
  927. rc = tpm_getcap(dev, CAP_VERSION_1_1, &cap,
  928. "attempting to determine the 1.1 version");
  929. if (rc)
  930. return 0;
  931. str += sprintf(str,
  932. "TCG version: %d.%d\nFirmware version: %d.%d\n",
  933. cap.tpm_version.Major,
  934. cap.tpm_version.Minor,
  935. cap.tpm_version.revMajor,
  936. cap.tpm_version.revMinor);
  937. }
  938. return str - buf;
  939. }
  940. EXPORT_SYMBOL_GPL(tpm_show_caps);
  941. ssize_t tpm_show_durations(struct device *dev, struct device_attribute *attr,
  942. char *buf)
  943. {
  944. struct tpm_chip *chip = dev_get_drvdata(dev);
  945. if (chip->vendor.duration[TPM_LONG] == 0)
  946. return 0;
  947. return sprintf(buf, "%d %d %d [%s]\n",
  948. jiffies_to_usecs(chip->vendor.duration[TPM_SHORT]),
  949. jiffies_to_usecs(chip->vendor.duration[TPM_MEDIUM]),
  950. jiffies_to_usecs(chip->vendor.duration[TPM_LONG]),
  951. chip->vendor.duration_adjusted
  952. ? "adjusted" : "original");
  953. }
  954. EXPORT_SYMBOL_GPL(tpm_show_durations);
  955. ssize_t tpm_show_timeouts(struct device *dev, struct device_attribute *attr,
  956. char *buf)
  957. {
  958. struct tpm_chip *chip = dev_get_drvdata(dev);
  959. return sprintf(buf, "%d %d %d %d [%s]\n",
  960. jiffies_to_usecs(chip->vendor.timeout_a),
  961. jiffies_to_usecs(chip->vendor.timeout_b),
  962. jiffies_to_usecs(chip->vendor.timeout_c),
  963. jiffies_to_usecs(chip->vendor.timeout_d),
  964. chip->vendor.timeout_adjusted
  965. ? "adjusted" : "original");
  966. }
  967. EXPORT_SYMBOL_GPL(tpm_show_timeouts);
  968. ssize_t tpm_store_cancel(struct device *dev, struct device_attribute *attr,
  969. const char *buf, size_t count)
  970. {
  971. struct tpm_chip *chip = dev_get_drvdata(dev);
  972. if (chip == NULL)
  973. return 0;
  974. chip->vendor.cancel(chip);
  975. return count;
  976. }
  977. EXPORT_SYMBOL_GPL(tpm_store_cancel);
  978. static bool wait_for_tpm_stat_cond(struct tpm_chip *chip, u8 mask,
  979. bool check_cancel, bool *canceled)
  980. {
  981. u8 status = chip->vendor.status(chip);
  982. *canceled = false;
  983. if ((status & mask) == mask)
  984. return true;
  985. if (check_cancel && chip->vendor.req_canceled(chip, status)) {
  986. *canceled = true;
  987. return true;
  988. }
  989. return false;
  990. }
  991. int wait_for_tpm_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
  992. wait_queue_head_t *queue, bool check_cancel)
  993. {
  994. unsigned long stop;
  995. long rc;
  996. u8 status;
  997. bool canceled = false;
  998. /* check current status */
  999. status = chip->vendor.status(chip);
  1000. if ((status & mask) == mask)
  1001. return 0;
  1002. stop = jiffies + timeout;
  1003. if (chip->vendor.irq) {
  1004. again:
  1005. timeout = stop - jiffies;
  1006. if ((long)timeout <= 0)
  1007. return -ETIME;
  1008. rc = wait_event_interruptible_timeout(*queue,
  1009. wait_for_tpm_stat_cond(chip, mask, check_cancel,
  1010. &canceled),
  1011. timeout);
  1012. if (rc > 0) {
  1013. if (canceled)
  1014. return -ECANCELED;
  1015. return 0;
  1016. }
  1017. if (rc == -ERESTARTSYS && freezing(current)) {
  1018. clear_thread_flag(TIF_SIGPENDING);
  1019. goto again;
  1020. }
  1021. } else {
  1022. do {
  1023. msleep(TPM_TIMEOUT);
  1024. status = chip->vendor.status(chip);
  1025. if ((status & mask) == mask)
  1026. return 0;
  1027. } while (time_before(jiffies, stop));
  1028. }
  1029. return -ETIME;
  1030. }
  1031. EXPORT_SYMBOL_GPL(wait_for_tpm_stat);
  1032. /*
  1033. * Device file system interface to the TPM
  1034. *
  1035. * It's assured that the chip will be opened just once,
  1036. * by the check of is_open variable, which is protected
  1037. * by driver_lock.
  1038. */
  1039. int tpm_open(struct inode *inode, struct file *file)
  1040. {
  1041. struct miscdevice *misc = file->private_data;
  1042. struct tpm_chip *chip = container_of(misc, struct tpm_chip,
  1043. vendor.miscdev);
  1044. if (test_and_set_bit(0, &chip->is_open)) {
  1045. dev_dbg(chip->dev, "Another process owns this TPM\n");
  1046. return -EBUSY;
  1047. }
  1048. chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
  1049. if (chip->data_buffer == NULL) {
  1050. clear_bit(0, &chip->is_open);
  1051. return -ENOMEM;
  1052. }
  1053. atomic_set(&chip->data_pending, 0);
  1054. file->private_data = chip;
  1055. get_device(chip->dev);
  1056. return 0;
  1057. }
  1058. EXPORT_SYMBOL_GPL(tpm_open);
  1059. /*
  1060. * Called on file close
  1061. */
  1062. int tpm_release(struct inode *inode, struct file *file)
  1063. {
  1064. struct tpm_chip *chip = file->private_data;
  1065. del_singleshot_timer_sync(&chip->user_read_timer);
  1066. flush_work(&chip->work);
  1067. file->private_data = NULL;
  1068. atomic_set(&chip->data_pending, 0);
  1069. kzfree(chip->data_buffer);
  1070. clear_bit(0, &chip->is_open);
  1071. put_device(chip->dev);
  1072. return 0;
  1073. }
  1074. EXPORT_SYMBOL_GPL(tpm_release);
  1075. ssize_t tpm_write(struct file *file, const char __user *buf,
  1076. size_t size, loff_t *off)
  1077. {
  1078. struct tpm_chip *chip = file->private_data;
  1079. size_t in_size = size;
  1080. ssize_t out_size;
  1081. /* cannot perform a write until the read has cleared
  1082. either via tpm_read or a user_read_timer timeout.
  1083. This also prevents splitted buffered writes from blocking here.
  1084. */
  1085. if (atomic_read(&chip->data_pending) != 0)
  1086. return -EBUSY;
  1087. if (in_size > TPM_BUFSIZE)
  1088. return -E2BIG;
  1089. mutex_lock(&chip->buffer_mutex);
  1090. if (copy_from_user
  1091. (chip->data_buffer, (void __user *) buf, in_size)) {
  1092. mutex_unlock(&chip->buffer_mutex);
  1093. return -EFAULT;
  1094. }
  1095. /* atomic tpm command send and result receive */
  1096. out_size = tpm_transmit(chip, chip->data_buffer, TPM_BUFSIZE);
  1097. if (out_size < 0) {
  1098. mutex_unlock(&chip->buffer_mutex);
  1099. return out_size;
  1100. }
  1101. atomic_set(&chip->data_pending, out_size);
  1102. mutex_unlock(&chip->buffer_mutex);
  1103. /* Set a timeout by which the reader must come claim the result */
  1104. mod_timer(&chip->user_read_timer, jiffies + (60 * HZ));
  1105. return in_size;
  1106. }
  1107. EXPORT_SYMBOL_GPL(tpm_write);
  1108. ssize_t tpm_read(struct file *file, char __user *buf,
  1109. size_t size, loff_t *off)
  1110. {
  1111. struct tpm_chip *chip = file->private_data;
  1112. ssize_t ret_size;
  1113. int rc;
  1114. del_singleshot_timer_sync(&chip->user_read_timer);
  1115. flush_work(&chip->work);
  1116. ret_size = atomic_read(&chip->data_pending);
  1117. if (ret_size > 0) { /* relay data */
  1118. ssize_t orig_ret_size = ret_size;
  1119. if (size < ret_size)
  1120. ret_size = size;
  1121. mutex_lock(&chip->buffer_mutex);
  1122. rc = copy_to_user(buf, chip->data_buffer, ret_size);
  1123. memset(chip->data_buffer, 0, orig_ret_size);
  1124. if (rc)
  1125. ret_size = -EFAULT;
  1126. mutex_unlock(&chip->buffer_mutex);
  1127. }
  1128. atomic_set(&chip->data_pending, 0);
  1129. return ret_size;
  1130. }
  1131. EXPORT_SYMBOL_GPL(tpm_read);
  1132. void tpm_remove_hardware(struct device *dev)
  1133. {
  1134. struct tpm_chip *chip = dev_get_drvdata(dev);
  1135. if (chip == NULL) {
  1136. dev_err(dev, "No device data found\n");
  1137. return;
  1138. }
  1139. spin_lock(&driver_lock);
  1140. list_del_rcu(&chip->list);
  1141. spin_unlock(&driver_lock);
  1142. synchronize_rcu();
  1143. misc_deregister(&chip->vendor.miscdev);
  1144. sysfs_remove_group(&dev->kobj, chip->vendor.attr_group);
  1145. tpm_remove_ppi(&dev->kobj);
  1146. tpm_bios_log_teardown(chip->bios_dir);
  1147. /* write it this way to be explicit (chip->dev == dev) */
  1148. put_device(chip->dev);
  1149. }
  1150. EXPORT_SYMBOL_GPL(tpm_remove_hardware);
  1151. #define TPM_ORD_SAVESTATE cpu_to_be32(152)
  1152. #define SAVESTATE_RESULT_SIZE 10
  1153. static struct tpm_input_header savestate_header = {
  1154. .tag = TPM_TAG_RQU_COMMAND,
  1155. .length = cpu_to_be32(10),
  1156. .ordinal = TPM_ORD_SAVESTATE
  1157. };
  1158. /*
  1159. * We are about to suspend. Save the TPM state
  1160. * so that it can be restored.
  1161. */
  1162. int tpm_pm_suspend(struct device *dev)
  1163. {
  1164. struct tpm_chip *chip = dev_get_drvdata(dev);
  1165. struct tpm_cmd_t cmd;
  1166. int rc, try;
  1167. u8 dummy_hash[TPM_DIGEST_SIZE] = { 0 };
  1168. if (chip == NULL)
  1169. return -ENODEV;
  1170. /* for buggy tpm, flush pcrs with extend to selected dummy */
  1171. if (tpm_suspend_pcr) {
  1172. cmd.header.in = pcrextend_header;
  1173. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(tpm_suspend_pcr);
  1174. memcpy(cmd.params.pcrextend_in.hash, dummy_hash,
  1175. TPM_DIGEST_SIZE);
  1176. rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
  1177. "extending dummy pcr before suspend");
  1178. }
  1179. /* now do the actual savestate */
  1180. for (try = 0; try < TPM_RETRY; try++) {
  1181. cmd.header.in = savestate_header;
  1182. rc = transmit_cmd(chip, &cmd, SAVESTATE_RESULT_SIZE, NULL);
  1183. /*
  1184. * If the TPM indicates that it is too busy to respond to
  1185. * this command then retry before giving up. It can take
  1186. * several seconds for this TPM to be ready.
  1187. *
  1188. * This can happen if the TPM has already been sent the
  1189. * SaveState command before the driver has loaded. TCG 1.2
  1190. * specification states that any communication after SaveState
  1191. * may cause the TPM to invalidate previously saved state.
  1192. */
  1193. if (rc != TPM_WARN_RETRY)
  1194. break;
  1195. msleep(TPM_TIMEOUT_RETRY);
  1196. }
  1197. if (rc)
  1198. dev_err(chip->dev,
  1199. "Error (%d) sending savestate before suspend\n", rc);
  1200. else if (try > 0)
  1201. dev_warn(chip->dev, "TPM savestate took %dms\n",
  1202. try * TPM_TIMEOUT_RETRY);
  1203. return rc;
  1204. }
  1205. EXPORT_SYMBOL_GPL(tpm_pm_suspend);
  1206. /*
  1207. * Resume from a power safe. The BIOS already restored
  1208. * the TPM state.
  1209. */
  1210. int tpm_pm_resume(struct device *dev)
  1211. {
  1212. struct tpm_chip *chip = dev_get_drvdata(dev);
  1213. if (chip == NULL)
  1214. return -ENODEV;
  1215. return 0;
  1216. }
  1217. EXPORT_SYMBOL_GPL(tpm_pm_resume);
  1218. #define TPM_GETRANDOM_RESULT_SIZE 18
  1219. static struct tpm_input_header tpm_getrandom_header = {
  1220. .tag = TPM_TAG_RQU_COMMAND,
  1221. .length = cpu_to_be32(14),
  1222. .ordinal = TPM_ORD_GET_RANDOM
  1223. };
  1224. /**
  1225. * tpm_get_random() - Get random bytes from the tpm's RNG
  1226. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  1227. * @out: destination buffer for the random bytes
  1228. * @max: the max number of bytes to write to @out
  1229. *
  1230. * Returns < 0 on error and the number of bytes read on success
  1231. */
  1232. int tpm_get_random(u32 chip_num, u8 *out, size_t max)
  1233. {
  1234. struct tpm_chip *chip;
  1235. struct tpm_cmd_t tpm_cmd;
  1236. u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA);
  1237. int err, total = 0, retries = 5;
  1238. u8 *dest = out;
  1239. chip = tpm_chip_find_get(chip_num);
  1240. if (chip == NULL)
  1241. return -ENODEV;
  1242. if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
  1243. return -EINVAL;
  1244. do {
  1245. tpm_cmd.header.in = tpm_getrandom_header;
  1246. tpm_cmd.params.getrandom_in.num_bytes = cpu_to_be32(num_bytes);
  1247. err = transmit_cmd(chip, &tpm_cmd,
  1248. TPM_GETRANDOM_RESULT_SIZE + num_bytes,
  1249. "attempting get random");
  1250. if (err)
  1251. break;
  1252. recd = be32_to_cpu(tpm_cmd.params.getrandom_out.rng_data_len);
  1253. memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
  1254. dest += recd;
  1255. total += recd;
  1256. num_bytes -= recd;
  1257. } while (retries-- && total < max);
  1258. return total ? total : -EIO;
  1259. }
  1260. EXPORT_SYMBOL_GPL(tpm_get_random);
  1261. /* In case vendor provided release function, call it too.*/
  1262. void tpm_dev_vendor_release(struct tpm_chip *chip)
  1263. {
  1264. if (!chip)
  1265. return;
  1266. if (chip->vendor.release)
  1267. chip->vendor.release(chip->dev);
  1268. clear_bit(chip->dev_num, dev_mask);
  1269. }
  1270. EXPORT_SYMBOL_GPL(tpm_dev_vendor_release);
  1271. /*
  1272. * Once all references to platform device are down to 0,
  1273. * release all allocated structures.
  1274. */
  1275. void tpm_dev_release(struct device *dev)
  1276. {
  1277. struct tpm_chip *chip = dev_get_drvdata(dev);
  1278. if (!chip)
  1279. return;
  1280. tpm_dev_vendor_release(chip);
  1281. chip->release(dev);
  1282. kfree(chip);
  1283. }
  1284. EXPORT_SYMBOL_GPL(tpm_dev_release);
  1285. /*
  1286. * Called from tpm_<specific>.c probe function only for devices
  1287. * the driver has determined it should claim. Prior to calling
  1288. * this function the specific probe function has called pci_enable_device
  1289. * upon errant exit from this function specific probe function should call
  1290. * pci_disable_device
  1291. */
  1292. struct tpm_chip *tpm_register_hardware(struct device *dev,
  1293. const struct tpm_vendor_specific *entry)
  1294. {
  1295. struct tpm_chip *chip;
  1296. /* Driver specific per-device data */
  1297. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  1298. if (chip == NULL)
  1299. return NULL;
  1300. mutex_init(&chip->buffer_mutex);
  1301. mutex_init(&chip->tpm_mutex);
  1302. INIT_LIST_HEAD(&chip->list);
  1303. INIT_WORK(&chip->work, timeout_work);
  1304. setup_timer(&chip->user_read_timer, user_reader_timeout,
  1305. (unsigned long)chip);
  1306. memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
  1307. chip->dev_num = find_first_zero_bit(dev_mask, TPM_NUM_DEVICES);
  1308. if (chip->dev_num >= TPM_NUM_DEVICES) {
  1309. dev_err(dev, "No available tpm device numbers\n");
  1310. goto out_free;
  1311. } else if (chip->dev_num == 0)
  1312. chip->vendor.miscdev.minor = TPM_MINOR;
  1313. else
  1314. chip->vendor.miscdev.minor = MISC_DYNAMIC_MINOR;
  1315. set_bit(chip->dev_num, dev_mask);
  1316. scnprintf(chip->devname, sizeof(chip->devname), "%s%d", "tpm",
  1317. chip->dev_num);
  1318. chip->vendor.miscdev.name = chip->devname;
  1319. chip->vendor.miscdev.parent = dev;
  1320. chip->dev = get_device(dev);
  1321. chip->release = dev->release;
  1322. dev->release = tpm_dev_release;
  1323. dev_set_drvdata(dev, chip);
  1324. if (misc_register(&chip->vendor.miscdev)) {
  1325. dev_err(chip->dev,
  1326. "unable to misc_register %s, minor %d\n",
  1327. chip->vendor.miscdev.name,
  1328. chip->vendor.miscdev.minor);
  1329. goto put_device;
  1330. }
  1331. if (sysfs_create_group(&dev->kobj, chip->vendor.attr_group)) {
  1332. misc_deregister(&chip->vendor.miscdev);
  1333. goto put_device;
  1334. }
  1335. if (tpm_add_ppi(&dev->kobj)) {
  1336. misc_deregister(&chip->vendor.miscdev);
  1337. goto put_device;
  1338. }
  1339. chip->bios_dir = tpm_bios_log_setup(chip->devname);
  1340. /* Make chip available */
  1341. spin_lock(&driver_lock);
  1342. list_add_rcu(&chip->list, &tpm_chip_list);
  1343. spin_unlock(&driver_lock);
  1344. return chip;
  1345. put_device:
  1346. put_device(chip->dev);
  1347. out_free:
  1348. kfree(chip);
  1349. return NULL;
  1350. }
  1351. EXPORT_SYMBOL_GPL(tpm_register_hardware);
  1352. MODULE_AUTHOR("Leendert van Doorn (leendert@watson.ibm.com)");
  1353. MODULE_DESCRIPTION("TPM Driver");
  1354. MODULE_VERSION("2.0");
  1355. MODULE_LICENSE("GPL");