blob: fddf34068f0bd7bb995cba778fec83a29b7e498c [file] [log] [blame]
Stefan Reinauerb5ab3232009-04-22 07:23:00 +00001/*
2 * This file is part of the ectool project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 *
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
Uwe Hermann257ae3f2009-04-22 12:28:14 +00008 * published by the Free Software Foundation; version 2 of the License.
Stefan Reinauerb5ab3232009-04-22 07:23:00 +00009 *
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.
14 *
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
Uwe Hermann257ae3f2009-04-22 12:28:14 +000017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000018 */
19
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>
Alexander Couzensa5410dc2015-02-02 08:51:30 +010027#include <stdlib.h>
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000028
29#define ECTOOL_VERSION "0.1"
30
31void print_version(void)
32{
33 printf("ectool v%s -- ", ECTOOL_VERSION);
34 printf("Copyright (C) 2008-2009 coresystems GmbH\n\n");
35 printf(
Uwe Hermann257ae3f2009-04-22 12:28:14 +000036 "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");
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000045}
46
47void print_usage(const char *name)
48{
Alexander Couzensa5410dc2015-02-02 08:51:30 +010049 printf("usage: %s [-vh?Vi] [-w 0x<addr> -z 0x<data>]\n", name);
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000050 printf("\n"
Uwe Hermann257ae3f2009-04-22 12:28:14 +000051 " -v | --version: print the version\n"
52 " -h | --help: print this help\n\n"
53 " -V | --verbose: print debug information\n"
Anton Kochkov7e59f7692010-06-29 21:13:20 +000054 " -i | --idx: print IDX RAM\n"
Alexander Couzensa5410dc2015-02-02 08:51:30 +010055 " -w <addr in hex> write to addr\n"
56 " -z <data in hex> write to data\n"
Uwe Hermann257ae3f2009-04-22 12:28:14 +000057 "\n");
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000058 exit(1);
59}
60
Anton Kochkov7e59f7692010-06-29 21:13:20 +000061int verbose = 0, dump_idx = 0;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000062
63int main(int argc, char *argv[])
64{
65 int i, opt, option_index = 0;
Alexander Couzensa5410dc2015-02-02 08:51:30 +010066 long write_data = -1;
67 long write_addr = -1;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000068
69 static struct option long_options[] = {
70 {"version", 0, 0, 'v'},
71 {"help", 0, 0, 'h'},
72 {"verbose", 0, 0, 'V'},
Anton Kochkov7e59f7692010-06-29 21:13:20 +000073 {"idx", 0, 0, 'i'},
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000074 {0, 0, 0, 0}
75 };
76
Alexander Couzensa5410dc2015-02-02 08:51:30 +010077 while ((opt = getopt_long(argc, argv, "vh?Viw:z:",
Uwe Hermann257ae3f2009-04-22 12:28:14 +000078 long_options, &option_index)) != EOF) {
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000079 switch (opt) {
80 case 'v':
81 print_version();
82 exit(0);
83 break;
84 case 'V':
85 verbose = 1;
86 break;
Anton Kochkov7e59f7692010-06-29 21:13:20 +000087 case 'i':
88 dump_idx = 1;
Peter Stuge11abcc0a2010-10-25 02:12:04 +000089 break;
Alexander Couzensa5410dc2015-02-02 08:51:30 +010090 case 'w':
91 write_addr = strtol(optarg , NULL, 16);
92 break;
93 case 'z':
94 write_data = strtol(optarg , NULL, 16);
95 break;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000096 case 'h':
97 case '?':
98 default:
99 print_usage(argv[0]);
100 exit(0);
101 break;
102 }
103 }
104
105 if (iopl(3)) {
106 printf("You need to be root.\n");
107 exit(1);
108 }
Alexander Couzensa5410dc2015-02-02 08:51:30 +0100109 if (write_addr >= 0 && write_data >= 0) {
110 write_addr &= 0xff;
111 write_data &= 0xff;
112 printf("\nWriting ec %02lx = %02lx\n", write_addr & 0xff, write_data & 0xff);
113 ec_write(write_addr & 0xff, write_data & 0xff);
114 }
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000115
116 printf("EC RAM:\n");
Uwe Hermann257ae3f2009-04-22 12:28:14 +0000117 for (i = 0; i < 0x100; i++) {
118 if ((i % 0x10) == 0)
119 printf("\n%02x: ", i);
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000120 printf("%02x ", ec_read(i));
121 }
122 printf("\n\n");
Uwe Hermann257ae3f2009-04-22 12:28:14 +0000123
Anton Kochkov7e59f7692010-06-29 21:13:20 +0000124 if (dump_idx) {
125 printf("EC IDX RAM:\n");
126 for (i = 0; i < 0x10000; i++) {
127 if ((i % 0x10) == 0)
128 printf("\n%04x: ", i);
129 printf("%02x ", ec_idx_read(i));
130 }
131 printf("\n\n");
Stefan Reinauer984e0f32010-01-16 17:50:55 +0000132 }
Stefan Reinauer984e0f32010-01-16 17:50:55 +0000133
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000134 return 0;
135}