tpm.c 34 KB

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