tpm_atmel_twi.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License as
  4. * published by the Free Software Foundation; either version 2 of
  5. * the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  15. * MA 02111-1307 USA
  16. */
  17. #include <common.h>
  18. #include <tpm.h>
  19. #include <i2c.h>
  20. #include <asm/unaligned.h>
  21. #define ATMEL_TPM_TIMEOUT_MS 5000 /* sufficient for anything but
  22. generating/exporting keys */
  23. /*
  24. * tis_init()
  25. *
  26. * Initialize the TPM device. Returns 0 on success or -1 on
  27. * failure (in case device probing did not succeed).
  28. */
  29. int tis_init(void)
  30. {
  31. return 0;
  32. }
  33. /*
  34. * tis_open()
  35. *
  36. * Requests access to locality 0 for the caller. After all commands have been
  37. * completed the caller is supposed to call tis_close().
  38. *
  39. * Returns 0 on success, -1 on failure.
  40. */
  41. int tis_open(void)
  42. {
  43. return 0;
  44. }
  45. /*
  46. * tis_close()
  47. *
  48. * terminate the currect session with the TPM by releasing the locked
  49. * locality. Returns 0 on success of -1 on failure (in case lock
  50. * removal did not succeed).
  51. */
  52. int tis_close(void)
  53. {
  54. return 0;
  55. }
  56. /*
  57. * tis_sendrecv()
  58. *
  59. * Send the requested data to the TPM and then try to get its response
  60. *
  61. * @sendbuf - buffer of the data to send
  62. * @send_size size of the data to send
  63. * @recvbuf - memory to save the response to
  64. * @recv_len - pointer to the size of the response buffer
  65. *
  66. * Returns 0 on success (and places the number of response bytes at recv_len)
  67. * or -1 on failure.
  68. */
  69. int tis_sendrecv(const uint8_t *sendbuf, size_t send_size, uint8_t *recvbuf,
  70. size_t *recv_len)
  71. {
  72. int res;
  73. unsigned long start;
  74. #ifdef DEBUG
  75. memset(recvbuf, 0xcc, *recv_len);
  76. printf("send to TPM (%d bytes, recv_len=%d):\n", send_size, *recv_len);
  77. print_buffer(0, (void *)sendbuf, 1, send_size, 0);
  78. #endif
  79. res = i2c_write(0x29, 0, 0, (uchar *)sendbuf, send_size);
  80. if (res) {
  81. printf("i2c_write returned %d\n", res);
  82. return -1;
  83. }
  84. start = get_timer(0);
  85. while ((res = i2c_read(0x29, 0, 0, recvbuf, 10))) {
  86. if (get_timer(start) > ATMEL_TPM_TIMEOUT_MS) {
  87. puts("tpm timed out\n");
  88. return -1;
  89. }
  90. udelay(100);
  91. }
  92. if (!res) {
  93. *recv_len = get_unaligned_be32(recvbuf + 2);
  94. if (*recv_len > 10)
  95. res = i2c_read(0x29, 0, 0, recvbuf, *recv_len);
  96. }
  97. if (res) {
  98. printf("i2c_read returned %d (rlen=%d)\n", res, *recv_len);
  99. #ifdef DEBUG
  100. print_buffer(0, recvbuf, 1, *recv_len, 0);
  101. #endif
  102. }
  103. #ifdef DEBUG
  104. if (!res) {
  105. printf("read from TPM (%d bytes):\n", *recv_len);
  106. print_buffer(0, recvbuf, 1, *recv_len, 0);
  107. }
  108. #endif
  109. return res;
  110. }