tpm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * Copyright (C) 2011 Infineon Technologies
  3. *
  4. * Authors:
  5. * Peter Huewe <huewe.external@infineon.com>
  6. *
  7. * Description:
  8. * Device driver for TCG/TCPA TPM (trusted platform module).
  9. * Specifications at www.trustedcomputinggroup.org
  10. *
  11. * It is based on the Linux kernel driver tpm.c from Leendert van
  12. * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
  13. *
  14. * Version: 2.1.1
  15. *
  16. * See file CREDITS for list of people who contributed to this
  17. * project.
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU General Public License as
  21. * published by the Free Software Foundation, version 2 of the
  22. * License.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  32. * MA 02111-1307 USA
  33. */
  34. #include <config.h>
  35. #include <common.h>
  36. #include <compiler.h>
  37. #include <fdtdec.h>
  38. #include <i2c.h>
  39. #include <tpm.h>
  40. #include <asm-generic/errno.h>
  41. #include <linux/types.h>
  42. #include <linux/unaligned/be_byteshift.h>
  43. #include "tpm_private.h"
  44. DECLARE_GLOBAL_DATA_PTR;
  45. /* TPM configuration */
  46. struct tpm {
  47. int i2c_bus;
  48. int slave_addr;
  49. char inited;
  50. int old_bus;
  51. } tpm;
  52. /* Global structure for tpm chip data */
  53. static struct tpm_chip g_chip;
  54. enum tpm_duration {
  55. TPM_SHORT = 0,
  56. TPM_MEDIUM = 1,
  57. TPM_LONG = 2,
  58. TPM_UNDEFINED,
  59. };
  60. /* Extended error numbers from linux (see errno.h) */
  61. #define ECANCELED 125 /* Operation Canceled */
  62. /* Timer frequency. Corresponds to msec timer resolution*/
  63. #define HZ 1000
  64. #define TPM_MAX_ORDINAL 243
  65. #define TPM_MAX_PROTECTED_ORDINAL 12
  66. #define TPM_PROTECTED_ORDINAL_MASK 0xFF
  67. #define TPM_CMD_COUNT_BYTE 2
  68. #define TPM_CMD_ORDINAL_BYTE 6
  69. /*
  70. * Array with one entry per ordinal defining the maximum amount
  71. * of time the chip could take to return the result. The ordinal
  72. * designation of short, medium or long is defined in a table in
  73. * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
  74. * values of the SHORT, MEDIUM, and LONG durations are retrieved
  75. * from the chip during initialization with a call to tpm_get_timeouts.
  76. */
  77. static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
  78. TPM_UNDEFINED, /* 0 */
  79. TPM_UNDEFINED,
  80. TPM_UNDEFINED,
  81. TPM_UNDEFINED,
  82. TPM_UNDEFINED,
  83. TPM_UNDEFINED, /* 5 */
  84. TPM_UNDEFINED,
  85. TPM_UNDEFINED,
  86. TPM_UNDEFINED,
  87. TPM_UNDEFINED,
  88. TPM_SHORT, /* 10 */
  89. TPM_SHORT,
  90. };
  91. static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
  92. TPM_UNDEFINED, /* 0 */
  93. TPM_UNDEFINED,
  94. TPM_UNDEFINED,
  95. TPM_UNDEFINED,
  96. TPM_UNDEFINED,
  97. TPM_UNDEFINED, /* 5 */
  98. TPM_UNDEFINED,
  99. TPM_UNDEFINED,
  100. TPM_UNDEFINED,
  101. TPM_UNDEFINED,
  102. TPM_SHORT, /* 10 */
  103. TPM_SHORT,
  104. TPM_MEDIUM,
  105. TPM_LONG,
  106. TPM_LONG,
  107. TPM_MEDIUM, /* 15 */
  108. TPM_SHORT,
  109. TPM_SHORT,
  110. TPM_MEDIUM,
  111. TPM_LONG,
  112. TPM_SHORT, /* 20 */
  113. TPM_SHORT,
  114. TPM_MEDIUM,
  115. TPM_MEDIUM,
  116. TPM_MEDIUM,
  117. TPM_SHORT, /* 25 */
  118. TPM_SHORT,
  119. TPM_MEDIUM,
  120. TPM_SHORT,
  121. TPM_SHORT,
  122. TPM_MEDIUM, /* 30 */
  123. TPM_LONG,
  124. TPM_MEDIUM,
  125. TPM_SHORT,
  126. TPM_SHORT,
  127. TPM_SHORT, /* 35 */
  128. TPM_MEDIUM,
  129. TPM_MEDIUM,
  130. TPM_UNDEFINED,
  131. TPM_UNDEFINED,
  132. TPM_MEDIUM, /* 40 */
  133. TPM_LONG,
  134. TPM_MEDIUM,
  135. TPM_SHORT,
  136. TPM_SHORT,
  137. TPM_SHORT, /* 45 */
  138. TPM_SHORT,
  139. TPM_SHORT,
  140. TPM_SHORT,
  141. TPM_LONG,
  142. TPM_MEDIUM, /* 50 */
  143. TPM_MEDIUM,
  144. TPM_UNDEFINED,
  145. TPM_UNDEFINED,
  146. TPM_UNDEFINED,
  147. TPM_UNDEFINED, /* 55 */
  148. TPM_UNDEFINED,
  149. TPM_UNDEFINED,
  150. TPM_UNDEFINED,
  151. TPM_UNDEFINED,
  152. TPM_MEDIUM, /* 60 */
  153. TPM_MEDIUM,
  154. TPM_MEDIUM,
  155. TPM_SHORT,
  156. TPM_SHORT,
  157. TPM_MEDIUM, /* 65 */
  158. TPM_UNDEFINED,
  159. TPM_UNDEFINED,
  160. TPM_UNDEFINED,
  161. TPM_UNDEFINED,
  162. TPM_SHORT, /* 70 */
  163. TPM_SHORT,
  164. TPM_UNDEFINED,
  165. TPM_UNDEFINED,
  166. TPM_UNDEFINED,
  167. TPM_UNDEFINED, /* 75 */
  168. TPM_UNDEFINED,
  169. TPM_UNDEFINED,
  170. TPM_UNDEFINED,
  171. TPM_UNDEFINED,
  172. TPM_LONG, /* 80 */
  173. TPM_UNDEFINED,
  174. TPM_MEDIUM,
  175. TPM_LONG,
  176. TPM_SHORT,
  177. TPM_UNDEFINED, /* 85 */
  178. TPM_UNDEFINED,
  179. TPM_UNDEFINED,
  180. TPM_UNDEFINED,
  181. TPM_UNDEFINED,
  182. TPM_SHORT, /* 90 */
  183. TPM_SHORT,
  184. TPM_SHORT,
  185. TPM_SHORT,
  186. TPM_SHORT,
  187. TPM_UNDEFINED, /* 95 */
  188. TPM_UNDEFINED,
  189. TPM_UNDEFINED,
  190. TPM_UNDEFINED,
  191. TPM_UNDEFINED,
  192. TPM_MEDIUM, /* 100 */
  193. TPM_SHORT,
  194. TPM_SHORT,
  195. TPM_UNDEFINED,
  196. TPM_UNDEFINED,
  197. TPM_UNDEFINED, /* 105 */
  198. TPM_UNDEFINED,
  199. TPM_UNDEFINED,
  200. TPM_UNDEFINED,
  201. TPM_UNDEFINED,
  202. TPM_SHORT, /* 110 */
  203. TPM_SHORT,
  204. TPM_SHORT,
  205. TPM_SHORT,
  206. TPM_SHORT,
  207. TPM_SHORT, /* 115 */
  208. TPM_SHORT,
  209. TPM_SHORT,
  210. TPM_UNDEFINED,
  211. TPM_UNDEFINED,
  212. TPM_LONG, /* 120 */
  213. TPM_LONG,
  214. TPM_MEDIUM,
  215. TPM_UNDEFINED,
  216. TPM_SHORT,
  217. TPM_SHORT, /* 125 */
  218. TPM_SHORT,
  219. TPM_LONG,
  220. TPM_SHORT,
  221. TPM_SHORT,
  222. TPM_SHORT, /* 130 */
  223. TPM_MEDIUM,
  224. TPM_UNDEFINED,
  225. TPM_SHORT,
  226. TPM_MEDIUM,
  227. TPM_UNDEFINED, /* 135 */
  228. TPM_UNDEFINED,
  229. TPM_UNDEFINED,
  230. TPM_UNDEFINED,
  231. TPM_UNDEFINED,
  232. TPM_SHORT, /* 140 */
  233. TPM_SHORT,
  234. TPM_UNDEFINED,
  235. TPM_UNDEFINED,
  236. TPM_UNDEFINED,
  237. TPM_UNDEFINED, /* 145 */
  238. TPM_UNDEFINED,
  239. TPM_UNDEFINED,
  240. TPM_UNDEFINED,
  241. TPM_UNDEFINED,
  242. TPM_SHORT, /* 150 */
  243. TPM_MEDIUM,
  244. TPM_MEDIUM,
  245. TPM_SHORT,
  246. TPM_SHORT,
  247. TPM_UNDEFINED, /* 155 */
  248. TPM_UNDEFINED,
  249. TPM_UNDEFINED,
  250. TPM_UNDEFINED,
  251. TPM_UNDEFINED,
  252. TPM_SHORT, /* 160 */
  253. TPM_SHORT,
  254. TPM_SHORT,
  255. TPM_SHORT,
  256. TPM_UNDEFINED,
  257. TPM_UNDEFINED, /* 165 */
  258. TPM_UNDEFINED,
  259. TPM_UNDEFINED,
  260. TPM_UNDEFINED,
  261. TPM_UNDEFINED,
  262. TPM_LONG, /* 170 */
  263. TPM_UNDEFINED,
  264. TPM_UNDEFINED,
  265. TPM_UNDEFINED,
  266. TPM_UNDEFINED,
  267. TPM_UNDEFINED, /* 175 */
  268. TPM_UNDEFINED,
  269. TPM_UNDEFINED,
  270. TPM_UNDEFINED,
  271. TPM_UNDEFINED,
  272. TPM_MEDIUM, /* 180 */
  273. TPM_SHORT,
  274. TPM_MEDIUM,
  275. TPM_MEDIUM,
  276. TPM_MEDIUM,
  277. TPM_MEDIUM, /* 185 */
  278. TPM_SHORT,
  279. TPM_UNDEFINED,
  280. TPM_UNDEFINED,
  281. TPM_UNDEFINED,
  282. TPM_UNDEFINED, /* 190 */
  283. TPM_UNDEFINED,
  284. TPM_UNDEFINED,
  285. TPM_UNDEFINED,
  286. TPM_UNDEFINED,
  287. TPM_UNDEFINED, /* 195 */
  288. TPM_UNDEFINED,
  289. TPM_UNDEFINED,
  290. TPM_UNDEFINED,
  291. TPM_UNDEFINED,
  292. TPM_SHORT, /* 200 */
  293. TPM_UNDEFINED,
  294. TPM_UNDEFINED,
  295. TPM_UNDEFINED,
  296. TPM_SHORT,
  297. TPM_SHORT, /* 205 */
  298. TPM_SHORT,
  299. TPM_SHORT,
  300. TPM_SHORT,
  301. TPM_SHORT,
  302. TPM_MEDIUM, /* 210 */
  303. TPM_UNDEFINED,
  304. TPM_MEDIUM,
  305. TPM_MEDIUM,
  306. TPM_MEDIUM,
  307. TPM_UNDEFINED, /* 215 */
  308. TPM_MEDIUM,
  309. TPM_UNDEFINED,
  310. TPM_UNDEFINED,
  311. TPM_SHORT,
  312. TPM_SHORT, /* 220 */
  313. TPM_SHORT,
  314. TPM_SHORT,
  315. TPM_SHORT,
  316. TPM_SHORT,
  317. TPM_UNDEFINED, /* 225 */
  318. TPM_UNDEFINED,
  319. TPM_UNDEFINED,
  320. TPM_UNDEFINED,
  321. TPM_UNDEFINED,
  322. TPM_SHORT, /* 230 */
  323. TPM_LONG,
  324. TPM_MEDIUM,
  325. TPM_UNDEFINED,
  326. TPM_UNDEFINED,
  327. TPM_UNDEFINED, /* 235 */
  328. TPM_UNDEFINED,
  329. TPM_UNDEFINED,
  330. TPM_UNDEFINED,
  331. TPM_UNDEFINED,
  332. TPM_SHORT, /* 240 */
  333. TPM_UNDEFINED,
  334. TPM_MEDIUM,
  335. };
  336. /* Returns max number of milliseconds to wait */
  337. static unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
  338. u32 ordinal)
  339. {
  340. int duration_idx = TPM_UNDEFINED;
  341. int duration = 0;
  342. if (ordinal < TPM_MAX_ORDINAL) {
  343. duration_idx = tpm_ordinal_duration[ordinal];
  344. } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
  345. TPM_MAX_PROTECTED_ORDINAL) {
  346. duration_idx = tpm_protected_ordinal_duration[
  347. ordinal & TPM_PROTECTED_ORDINAL_MASK];
  348. }
  349. if (duration_idx != TPM_UNDEFINED)
  350. duration = chip->vendor.duration[duration_idx];
  351. if (duration <= 0)
  352. return 2 * 60 * HZ; /* Two minutes timeout */
  353. else
  354. return duration;
  355. }
  356. static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
  357. {
  358. ssize_t rc;
  359. u32 count, ordinal;
  360. unsigned long start, stop;
  361. struct tpm_chip *chip = &g_chip;
  362. /* switch endianess: big->little */
  363. count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
  364. ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
  365. if (count == 0) {
  366. error("no data\n");
  367. return -ENODATA;
  368. }
  369. if (count > bufsiz) {
  370. error("invalid count value %x %zx\n", count, bufsiz);
  371. return -E2BIG;
  372. }
  373. rc = chip->vendor.send(chip, (u8 *)buf, count);
  374. if (rc < 0) {
  375. error("tpm_transmit: tpm_send: error %zd\n", rc);
  376. goto out;
  377. }
  378. if (chip->vendor.irq)
  379. goto out_recv;
  380. start = get_timer(0);
  381. stop = tpm_calc_ordinal_duration(chip, ordinal);
  382. do {
  383. debug("waiting for status...\n");
  384. u8 status = chip->vendor.status(chip);
  385. if ((status & chip->vendor.req_complete_mask) ==
  386. chip->vendor.req_complete_val) {
  387. debug("...got it;\n");
  388. goto out_recv;
  389. }
  390. if (status == chip->vendor.req_canceled) {
  391. error("Operation Canceled\n");
  392. rc = -ECANCELED;
  393. goto out;
  394. }
  395. udelay(TPM_TIMEOUT * 1000);
  396. } while (get_timer(start) < stop);
  397. chip->vendor.cancel(chip);
  398. error("Operation Timed out\n");
  399. rc = -ETIME;
  400. goto out;
  401. out_recv:
  402. debug("out_recv: reading response...\n");
  403. rc = chip->vendor.recv(chip, (u8 *)buf, TPM_BUFSIZE);
  404. if (rc < 0)
  405. error("tpm_transmit: tpm_recv: error %zd\n", rc);
  406. out:
  407. return rc;
  408. }
  409. static int tpm_open(uint32_t dev_addr)
  410. {
  411. int rc;
  412. if (g_chip.is_open)
  413. return -EBUSY;
  414. rc = tpm_vendor_init(dev_addr);
  415. if (rc < 0)
  416. g_chip.is_open = 0;
  417. return rc;
  418. }
  419. static void tpm_close(void)
  420. {
  421. if (g_chip.is_open) {
  422. tpm_vendor_cleanup(&g_chip);
  423. g_chip.is_open = 0;
  424. }
  425. }
  426. static int tpm_select(void)
  427. {
  428. int ret;
  429. tpm.old_bus = i2c_get_bus_num();
  430. if (tpm.old_bus != tpm.i2c_bus) {
  431. ret = i2c_set_bus_num(tpm.i2c_bus);
  432. if (ret) {
  433. debug("%s: Fail to set i2c bus %d\n", __func__,
  434. tpm.i2c_bus);
  435. return -1;
  436. }
  437. }
  438. return 0;
  439. }
  440. static int tpm_deselect(void)
  441. {
  442. int ret;
  443. if (tpm.old_bus != i2c_get_bus_num()) {
  444. ret = i2c_set_bus_num(tpm.old_bus);
  445. if (ret) {
  446. debug("%s: Fail to restore i2c bus %d\n",
  447. __func__, tpm.old_bus);
  448. return -1;
  449. }
  450. }
  451. tpm.old_bus = -1;
  452. return 0;
  453. }
  454. /**
  455. * Decode TPM configuration.
  456. *
  457. * @param dev Returns a configuration of TPM device
  458. * @return 0 if ok, -1 on error
  459. */
  460. static int tpm_decode_config(struct tpm *dev)
  461. {
  462. #ifdef CONFIG_OF_CONTROL
  463. const void *blob = gd->fdt_blob;
  464. int node, parent;
  465. int i2c_bus;
  466. node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
  467. if (node < 0) {
  468. node = fdtdec_next_compatible(blob, 0,
  469. COMPAT_INFINEON_SLB9645_TPM);
  470. }
  471. if (node < 0) {
  472. debug("%s: Node not found\n", __func__);
  473. return -1;
  474. }
  475. parent = fdt_parent_offset(blob, node);
  476. if (parent < 0) {
  477. debug("%s: Cannot find node parent\n", __func__);
  478. return -1;
  479. }
  480. i2c_bus = i2c_get_bus_num_fdt(parent);
  481. if (i2c_bus < 0)
  482. return -1;
  483. dev->i2c_bus = i2c_bus;
  484. dev->slave_addr = fdtdec_get_addr(blob, node, "reg");
  485. #else
  486. dev->i2c_bus = CONFIG_TPM_TIS_I2C_BUS_NUMBER;
  487. dev->slave_addr = CONFIG_TPM_TIS_I2C_SLAVE_ADDRESS;
  488. #endif
  489. return 0;
  490. }
  491. struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *entry)
  492. {
  493. struct tpm_chip *chip;
  494. /* Driver specific per-device data */
  495. chip = &g_chip;
  496. memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
  497. chip->is_open = 1;
  498. return chip;
  499. }
  500. int tis_init(void)
  501. {
  502. if (tpm.inited)
  503. return 0;
  504. if (tpm_decode_config(&tpm))
  505. return -1;
  506. if (tpm_select())
  507. return -1;
  508. /*
  509. * Probe TPM twice; the first probing might fail because TPM is asleep,
  510. * and the probing can wake up TPM.
  511. */
  512. if (i2c_probe(tpm.slave_addr) && i2c_probe(tpm.slave_addr)) {
  513. debug("%s: fail to probe i2c addr 0x%x\n", __func__,
  514. tpm.slave_addr);
  515. return -1;
  516. }
  517. tpm_deselect();
  518. tpm.inited = 1;
  519. return 0;
  520. }
  521. int tis_open(void)
  522. {
  523. int rc;
  524. if (!tpm.inited)
  525. return -1;
  526. if (tpm_select())
  527. return -1;
  528. rc = tpm_open(tpm.slave_addr);
  529. tpm_deselect();
  530. return rc;
  531. }
  532. int tis_close(void)
  533. {
  534. if (!tpm.inited)
  535. return -1;
  536. if (tpm_select())
  537. return -1;
  538. tpm_close();
  539. tpm_deselect();
  540. return 0;
  541. }
  542. int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
  543. uint8_t *recvbuf, size_t *rbuf_len)
  544. {
  545. int len;
  546. uint8_t buf[4096];
  547. if (!tpm.inited)
  548. return -1;
  549. if (sizeof(buf) < sbuf_size)
  550. return -1;
  551. memcpy(buf, sendbuf, sbuf_size);
  552. if (tpm_select())
  553. return -1;
  554. len = tpm_transmit(buf, sbuf_size);
  555. tpm_deselect();
  556. if (len < 10) {
  557. *rbuf_len = 0;
  558. return -1;
  559. }
  560. memcpy(recvbuf, buf, len);
  561. *rbuf_len = len;
  562. return 0;
  563. }