tpm.c 31 KB

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