cmd_tpm.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  61. {
  62. int rv = 0;
  63. /*
  64. * Verify that in case it is present, the first argument, it is
  65. * exactly one character in size.
  66. */
  67. if (argc < 7) {
  68. puts("command should be at least six bytes in size\n");
  69. return -1;
  70. }
  71. if (tis_init()) {
  72. puts("tis_init() failed!\n");
  73. return -1;
  74. }
  75. if (tis_open()) {
  76. puts("tis_open() failed!\n");
  77. return -1;
  78. }
  79. rv = tpm_process(argc - 1, argv + 1, cmdtp);
  80. if (tis_close()) {
  81. puts("tis_close() failed!\n");
  82. rv = -1;
  83. }
  84. return rv;
  85. }
  86. U_BOOT_CMD(tpm, MAX_TRANSACTION_SIZE, 1, do_tpm,
  87. "<byte> [<byte> ...] - write data and read response",
  88. "send arbitrary data (at least 6 bytes) to the TPM "
  89. "device and read the response"
  90. );