]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_tpm.c
MAINTAINERS: fix Andreas Bießmann AVR32 entry
[karo-tx-uboot.git] / common / cmd_tpm.c
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
23 #include <common.h>
24 #include <command.h>
25 #include <tpm.h>
26
27 #define MAX_TRANSACTION_SIZE 30
28
29 /*
30  * tpm_write() expects a variable number of parameters: the internal address
31  * followed by data to write, byte by byte.
32  *
33  * Returns 0 on success or -1 on errors (wrong arguments or TPM failure).
34  */
35 static int tpm_process(int argc, char * const argv[], cmd_tbl_t *cmdtp)
36 {
37         u8 tpm_buffer[MAX_TRANSACTION_SIZE];
38         u32 write_size, read_size;
39         char *p;
40         int rv = -1;
41
42         for (write_size = 0; write_size < argc; write_size++) {
43                 u32 datum = simple_strtoul(argv[write_size], &p, 0);
44                 if (*p || (datum > 0xff)) {
45                         printf("\n%s: bad data value\n\n", argv[write_size]);
46                         cmd_usage(cmdtp);
47                         return rv;
48                 }
49                 tpm_buffer[write_size] = (u8)datum;
50         }
51
52         read_size = sizeof(tpm_buffer);
53         if (!tis_sendrecv(tpm_buffer, write_size, tpm_buffer, &read_size)) {
54                 int i;
55                 puts("Got TPM response:\n");
56                 for (i = 0; i < read_size; i++)
57                         printf(" %2.2x", tpm_buffer[i]);
58                 puts("\n");
59                 rv = 0;
60         } else {
61                 puts("tpm command failed\n");
62         }
63         return rv;
64 }
65
66 static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
67 {
68         int rv = 0;
69
70         /*
71          * Verify that in case it is present, the first argument, it is
72          * exactly one character in size.
73          */
74         if (argc < 7) {
75                 puts("command should be at least six bytes in size\n");
76                 return -1;
77         }
78
79         if (tis_init()) {
80                 puts("tis_init() failed!\n");
81                 return -1;
82         }
83
84         if (tis_open()) {
85                 puts("tis_open() failed!\n");
86                 return -1;
87         }
88
89         rv = tpm_process(argc - 1, argv + 1, cmdtp);
90
91         if (tis_close()) {
92                 puts("tis_close() failed!\n");
93                 rv = -1;
94         }
95
96         return rv;
97 }
98
99 U_BOOT_CMD(tpm, MAX_TRANSACTION_SIZE, 1, do_tpm,
100            "<byte> [<byte> ...]   - write data and read response",
101            "send arbitrary data (at least 6 bytes) to the TPM "
102            "device and read the response"
103 );