blob: 4639c4ee67d27dcd7c1b0a48aa8bab15fdf04fed [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.
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000014 */
15
16#include <stdio.h>
17#include <stdint.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <getopt.h>
21#include <sys/io.h>
22#include <ec.h>
Alexander Couzensa5410dc2015-02-02 08:51:30 +010023#include <stdlib.h>
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000024
25#define ECTOOL_VERSION "0.1"
26
27void print_version(void)
28{
29 printf("ectool v%s -- ", ECTOOL_VERSION);
30 printf("Copyright (C) 2008-2009 coresystems GmbH\n\n");
31 printf(
Uwe Hermann257ae3f2009-04-22 12:28:14 +000032 "This program is free software: you can redistribute it and/or modify\n"
33 "it under the terms of the GNU General Public License as published by\n"
34 "the Free Software Foundation, version 2 of the License.\n\n"
35 "This program is distributed in the hope that it will be useful,\n"
36 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
37 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
38 "GNU General Public License for more details.\n\n"
39 "You should have received a copy of the GNU General Public License\n"
40 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000041}
42
43void print_usage(const char *name)
44{
Alexander Couzens0edf4192015-02-06 22:27:33 +010045 printf("usage: %s [-vh?Vidq] [-w 0x<addr> -z 0x<data>]\n", name);
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000046 printf("\n"
Uwe Hermann257ae3f2009-04-22 12:28:14 +000047 " -v | --version: print the version\n"
48 " -h | --help: print this help\n\n"
49 " -V | --verbose: print debug information\n"
Alexander Couzens46ca3a52015-02-07 04:10:12 +010050 " -d | --dump: print RAM\n"
51 " -i | --idx: print IDX RAM & RAM\n"
Alexander Couzens0edf4192015-02-06 22:27:33 +010052 " -q | --query: print query byte\n"
Alexander Couzensa5410dc2015-02-02 08:51:30 +010053 " -w <addr in hex> write to addr\n"
54 " -z <data in hex> write to data\n"
Uwe Hermann257ae3f2009-04-22 12:28:14 +000055 "\n");
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000056 exit(1);
57}
58
Alexander Couzens0edf4192015-02-06 22:27:33 +010059int verbose = 0, dump_idx = 0, dump_ram = 0, dump_query = 0;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000060
61int main(int argc, char *argv[])
62{
63 int i, opt, option_index = 0;
Alexander Couzensa5410dc2015-02-02 08:51:30 +010064 long write_data = -1;
65 long write_addr = -1;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000066
67 static struct option long_options[] = {
68 {"version", 0, 0, 'v'},
69 {"help", 0, 0, 'h'},
70 {"verbose", 0, 0, 'V'},
Anton Kochkov7e59f7692010-06-29 21:13:20 +000071 {"idx", 0, 0, 'i'},
Alexander Couzens0edf4192015-02-06 22:27:33 +010072 {"query", 0, 0, 'q'},
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000073 {0, 0, 0, 0}
74 };
75
Alexander Couzens0edf4192015-02-06 22:27:33 +010076 while ((opt = getopt_long(argc, argv, "vh?Vidqw:z:",
Uwe Hermann257ae3f2009-04-22 12:28:14 +000077 long_options, &option_index)) != EOF) {
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000078 switch (opt) {
79 case 'v':
80 print_version();
81 exit(0);
82 break;
83 case 'V':
84 verbose = 1;
85 break;
Anton Kochkov7e59f7692010-06-29 21:13:20 +000086 case 'i':
87 dump_idx = 1;
Alexander Couzens46ca3a52015-02-07 04:10:12 +010088 dump_ram = 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;
Alexander Couzens46ca3a52015-02-07 04:10:12 +010096 case 'd':
97 dump_ram = 1;
98 break;
Alexander Couzens0edf4192015-02-06 22:27:33 +010099 case 'q':
100 dump_query = 1;
101 break;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000102 case 'h':
103 case '?':
104 default:
105 print_usage(argv[0]);
106 exit(0);
107 break;
108 }
109 }
110
111 if (iopl(3)) {
112 printf("You need to be root.\n");
113 exit(1);
114 }
Alexander Couzensa5410dc2015-02-02 08:51:30 +0100115 if (write_addr >= 0 && write_data >= 0) {
116 write_addr &= 0xff;
117 write_data &= 0xff;
118 printf("\nWriting ec %02lx = %02lx\n", write_addr & 0xff, write_data & 0xff);
119 ec_write(write_addr & 0xff, write_data & 0xff);
120 }
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000121
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100122 /* preserve default - dump_ram if nothing selected */
Alexander Couzens0004ae82015-07-02 19:48:22 +0200123 if (!dump_ram && !dump_idx && !dump_query && !write_addr) {
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100124 dump_ram = 1;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000125 }
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100126
127 if (dump_ram) {
128 printf("EC RAM:\n");
129 for (i = 0; i < 0x100; i++) {
130 if ((i % 0x10) == 0)
131 printf("\n%02x: ", i);
132 printf("%02x ", ec_read(i));
133 }
134 printf("\n\n");
135 }
Uwe Hermann257ae3f2009-04-22 12:28:14 +0000136
Alexander Couzens0edf4192015-02-06 22:27:33 +0100137 if (dump_query) {
138 printf("EC QUERY %02x\n", ec_query());
139 }
140
Anton Kochkov7e59f7692010-06-29 21:13:20 +0000141 if (dump_idx) {
142 printf("EC IDX RAM:\n");
143 for (i = 0; i < 0x10000; i++) {
144 if ((i % 0x10) == 0)
145 printf("\n%04x: ", i);
146 printf("%02x ", ec_idx_read(i));
147 }
148 printf("\n\n");
Stefan Reinauer984e0f32010-01-16 17:50:55 +0000149 }
Stefan Reinauer984e0f32010-01-16 17:50:55 +0000150
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000151 return 0;
152}