cmd_tpm.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <command.h>
  24. #include <tpm.h>
  25. #define MAX_TRANSACTION_SIZE 30
  26. /*
  27. * tpm_write() expects a variable number of parameters: the internal address
  28. * followed by data to write, byte by byte.
  29. *
  30. * Returns 0 on success or -1 on errors (wrong arguments or TPM failure).
  31. */
  32. static int tpm_process(int argc, char * const argv[], cmd_tbl_t *cmdtp)
  33. {
  34. u8 tpm_buffer[MAX_TRANSACTION_SIZE];
  35. u32 write_size, read_size;
  36. char *p;
  37. int rv = -1;
  38. for (write_size = 0; write_size < argc; write_size++) {
  39. u32 datum = simple_strtoul(argv[write_size], &p, 0);
  40. if (*p || (datum > 0xff)) {
  41. printf("\n%s: bad data value\n\n", argv[write_size]);
  42. cmd_usage(cmdtp);
  43. return rv;
  44. }
  45. tpm_buffer[write_size] = (u8)datum;
  46. }
  47. read_size = sizeof(tpm_buffer);
  48. if (!tis_sendrecv(tpm_buffer, write_size, tpm_buffer, &read_size)) {
  49. int i;
  50. puts("Got TPM response:\n");
  51. for (i = 0; i < read_size; i++)
  52. printf(" %2.2x", tpm_buffer[i]);
  53. puts("\n");
  54. rv = 0;
  55. } else {
  56. puts("tpm command failed\n");
  57. }
  58. return rv;
  59. }
  60. #define CHECK(exp) do { \
  61. int _rv = exp; \
  62. if (_rv) { \
  63. printf("CHECK: %s %d %x\n", #exp, __LINE__, _rv);\
  64. } \
  65. } while (0)
  66. static int tpm_process_stress(int repeat_count)
  67. {
  68. int i;
  69. int rv = 0;
  70. u8 request[] = {0x0, 0xc1,
  71. 0x0, 0x0, 0x0, 0x16,
  72. 0x0, 0x0, 0x0, 0x65,
  73. 0x0, 0x0, 0x0, 0x4,
  74. 0x0, 0x0, 0x0, 0x4,
  75. 0x0, 0x0, 0x1, 0x9};
  76. u8 response[MAX_TRANSACTION_SIZE];
  77. u32 rlength = MAX_TRANSACTION_SIZE;
  78. CHECK(tis_init());
  79. for (i = 0; i < repeat_count; i++) {
  80. CHECK(tis_open());
  81. rv = tis_sendrecv(request, sizeof(request), response, &rlength);
  82. if (rv) {
  83. printf("tpm test failed at step %d with 0x%x\n", i, rv);
  84. CHECK(tis_close());
  85. break;
  86. }
  87. CHECK(tis_close());
  88. if ((response[6] || response[7] || response[8] || response[9])
  89. && response[9] != 0x26) {
  90. /* Ignore postinit errors */
  91. printf("tpm command failed at step %d\n"
  92. "tpm error code: %02x%02x%02x%02x\n", i,
  93. response[6], response[7],
  94. response[8], response[9]);
  95. rv = -1;
  96. break;
  97. }
  98. }
  99. return rv;
  100. }
  101. static int do_tpm_many(cmd_tbl_t *cmdtp, int flag,
  102. int argc, char * const argv[], int repeat_count)
  103. {
  104. int rv = 0;
  105. if (argc < 7 && repeat_count == 0) {
  106. puts("command should be at least six bytes in size\n");
  107. return -1;
  108. }
  109. if (repeat_count > 0) {
  110. rv = tpm_process_stress(repeat_count);
  111. return rv;
  112. }
  113. if (tis_init()) {
  114. puts("tis_init() failed!\n");
  115. return -1;
  116. }
  117. if (tis_open()) {
  118. puts("tis_open() failed!\n");
  119. return -1;
  120. }
  121. rv = tpm_process(argc - 1, argv + 1, cmdtp);
  122. if (tis_close()) {
  123. puts("tis_close() failed!\n");
  124. rv = -1;
  125. }
  126. return rv;
  127. }
  128. static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  129. {
  130. return do_tpm_many(cmdtp, flag, argc, argv, 0);
  131. }
  132. U_BOOT_CMD(tpm, MAX_TRANSACTION_SIZE, 1, do_tpm,
  133. "<byte> [<byte> ...] - write data and read response",
  134. "send arbitrary data (at least 6 bytes) to the TPM "
  135. "device and read the response"
  136. );
  137. static int do_tpm_stress(cmd_tbl_t *cmdtp, int flag,
  138. int argc, char * const argv[])
  139. {
  140. long unsigned int n;
  141. int rv;
  142. if (argc != 2) {
  143. puts("usage: tpm_stress <count>\n");
  144. return -1;
  145. }
  146. rv = strict_strtoul(argv[1], 10, &n);
  147. if (rv) {
  148. puts("tpm_stress: bad count");
  149. return -1;
  150. }
  151. return do_tpm_many(cmdtp, flag, argc, argv, n);
  152. }
  153. U_BOOT_CMD(tpm_stress, 2, 1, do_tpm_stress,
  154. "<n> - stress-test communication with TPM",
  155. "Repeat a TPM transaction (request-response) N times"
  156. );