tpm.c 37 KB

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