TPM: Reduce buffer size to fix stack overflow
[coreboot.git] / util / ectool / ectool.c
blob2479633aaa1c5f88c45eb20ed71fdd41386f099c
1 /*
2 * This file is part of the ectool project.
4 * Copyright (C) 2008-2009 coresystems GmbH
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <getopt.h>
25 #include <sys/io.h>
26 #include <ec.h>
27 #include <stdlib.h>
29 #define ECTOOL_VERSION "0.1"
31 void print_version(void)
33 printf("ectool v%s -- ", ECTOOL_VERSION);
34 printf("Copyright (C) 2008-2009 coresystems GmbH\n\n");
35 printf(
36 "This program is free software: you can redistribute it and/or modify\n"
37 "it under the terms of the GNU General Public License as published by\n"
38 "the Free Software Foundation, version 2 of the License.\n\n"
39 "This program is distributed in the hope that it will be useful,\n"
40 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
41 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
42 "GNU General Public License for more details.\n\n"
43 "You should have received a copy of the GNU General Public License\n"
44 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
47 void print_usage(const char *name)
49 printf("usage: %s [-vh?Vidq] [-w 0x<addr> -z 0x<data>]\n", name);
50 printf("\n"
51 " -v | --version: print the version\n"
52 " -h | --help: print this help\n\n"
53 " -V | --verbose: print debug information\n"
54 " -d | --dump: print RAM\n"
55 " -i | --idx: print IDX RAM & RAM\n"
56 " -q | --query: print query byte\n"
57 " -w <addr in hex> write to addr\n"
58 " -z <data in hex> write to data\n"
59 "\n");
60 exit(1);
63 int verbose = 0, dump_idx = 0, dump_ram = 0, dump_query = 0;
65 int main(int argc, char *argv[])
67 int i, opt, option_index = 0;
68 long write_data = -1;
69 long write_addr = -1;
71 static struct option long_options[] = {
72 {"version", 0, 0, 'v'},
73 {"help", 0, 0, 'h'},
74 {"verbose", 0, 0, 'V'},
75 {"idx", 0, 0, 'i'},
76 {"query", 0, 0, 'q'},
77 {0, 0, 0, 0}
80 while ((opt = getopt_long(argc, argv, "vh?Vidqw:z:",
81 long_options, &option_index)) != EOF) {
82 switch (opt) {
83 case 'v':
84 print_version();
85 exit(0);
86 break;
87 case 'V':
88 verbose = 1;
89 break;
90 case 'i':
91 dump_idx = 1;
92 dump_ram = 1;
93 break;
94 case 'w':
95 write_addr = strtol(optarg , NULL, 16);
96 break;
97 case 'z':
98 write_data = strtol(optarg , NULL, 16);
99 break;
100 case 'd':
101 dump_ram = 1;
102 break;
103 case 'q':
104 dump_query = 1;
105 break;
106 case 'h':
107 case '?':
108 default:
109 print_usage(argv[0]);
110 exit(0);
111 break;
115 if (iopl(3)) {
116 printf("You need to be root.\n");
117 exit(1);
119 if (write_addr >= 0 && write_data >= 0) {
120 write_addr &= 0xff;
121 write_data &= 0xff;
122 printf("\nWriting ec %02lx = %02lx\n", write_addr & 0xff, write_data & 0xff);
123 ec_write(write_addr & 0xff, write_data & 0xff);
126 /* preserve default - dump_ram if nothing selected */
127 if (!dump_ram && !dump_idx && !dump_query) {
128 dump_ram = 1;
131 if (dump_ram) {
132 printf("EC RAM:\n");
133 for (i = 0; i < 0x100; i++) {
134 if ((i % 0x10) == 0)
135 printf("\n%02x: ", i);
136 printf("%02x ", ec_read(i));
138 printf("\n\n");
141 if (dump_query) {
142 printf("EC QUERY %02x\n", ec_query());
145 if (dump_idx) {
146 printf("EC IDX RAM:\n");
147 for (i = 0; i < 0x10000; i++) {
148 if ((i % 0x10) == 0)
149 printf("\n%04x: ", i);
150 printf("%02x ", ec_idx_read(i));
152 printf("\n\n");
155 return 0;