blob: be5a89976591389e32e7d96fe3180b9b5c563c75 [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>
Steven Dee3236f7b2017-01-29 14:52:56 -050021#if !(defined __NetBSD__ || defined __OpenBSD__)
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000022#include <sys/io.h>
Andrey Korolyov393d9322016-01-05 14:27:59 +030023#endif
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000024#include <ec.h>
Alexander Couzensa5410dc2015-02-02 08:51:30 +010025#include <stdlib.h>
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000026
Steven Dee3236f7b2017-01-29 14:52:56 -050027#if defined __NetBSD__ || defined __OpenBSD__
28
Andrey Korolyov393d9322016-01-05 14:27:59 +030029#include <machine/sysarch.h>
Steven Dee3236f7b2017-01-29 14:52:56 -050030
31# if defined __i386__
32# define iopl i386_iopl
33# elif defined __NetBSD__
34# define iopl x86_64_iopl
35# else
36# define iopl amd64_iopl
37# endif
38
Andrey Korolyov393d9322016-01-05 14:27:59 +030039#endif
40
41
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000042#define ECTOOL_VERSION "0.1"
43
44void print_version(void)
45{
46 printf("ectool v%s -- ", ECTOOL_VERSION);
47 printf("Copyright (C) 2008-2009 coresystems GmbH\n\n");
48 printf(
Uwe Hermann257ae3f2009-04-22 12:28:14 +000049 "This program is free software: you can redistribute it and/or modify\n"
50 "it under the terms of the GNU General Public License as published by\n"
51 "the Free Software Foundation, version 2 of the License.\n\n"
52 "This program is distributed in the hope that it will be useful,\n"
53 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
54 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
Martin Rotheb20e602016-01-12 13:30:50 -070055 "GNU General Public License for more details.\n\n");
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000056}
57
58void print_usage(const char *name)
59{
Alexander Couzens0edf4192015-02-06 22:27:33 +010060 printf("usage: %s [-vh?Vidq] [-w 0x<addr> -z 0x<data>]\n", name);
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000061 printf("\n"
Uwe Hermann257ae3f2009-04-22 12:28:14 +000062 " -v | --version: print the version\n"
63 " -h | --help: print this help\n\n"
64 " -V | --verbose: print debug information\n"
Iru Cai2e8f4cc2018-01-25 21:44:09 +080065 " -p | --getports: get EC data & cmd ports from /proc/ioports\n"
Alexander Couzens46ca3a52015-02-07 04:10:12 +010066 " -d | --dump: print RAM\n"
67 " -i | --idx: print IDX RAM & RAM\n"
Alexander Couzens0edf4192015-02-06 22:27:33 +010068 " -q | --query: print query byte\n"
Alexander Couzensa5410dc2015-02-02 08:51:30 +010069 " -w <addr in hex> write to addr\n"
70 " -z <data in hex> write to data\n"
Uwe Hermann257ae3f2009-04-22 12:28:14 +000071 "\n");
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000072 exit(1);
73}
74
Iru Cai2e8f4cc2018-01-25 21:44:09 +080075int verbose = 0, dump_idx = 0, dump_ram = 0, dump_query = 0, get_ports = 0;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000076
77int main(int argc, char *argv[])
78{
79 int i, opt, option_index = 0;
Alexander Couzensa5410dc2015-02-02 08:51:30 +010080 long write_data = -1;
81 long write_addr = -1;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000082
83 static struct option long_options[] = {
84 {"version", 0, 0, 'v'},
85 {"help", 0, 0, 'h'},
86 {"verbose", 0, 0, 'V'},
Anton Kochkov7e59f762010-06-29 21:13:20 +000087 {"idx", 0, 0, 'i'},
Alexander Couzens0edf4192015-02-06 22:27:33 +010088 {"query", 0, 0, 'q'},
Iru Cai2e8f4cc2018-01-25 21:44:09 +080089 {"getports", 0, 0, 'p'},
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000090 {0, 0, 0, 0}
91 };
92
Iru Cai2e8f4cc2018-01-25 21:44:09 +080093 while ((opt = getopt_long(argc, argv, "vh?Vidqpw:z:",
Uwe Hermann257ae3f2009-04-22 12:28:14 +000094 long_options, &option_index)) != EOF) {
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000095 switch (opt) {
96 case 'v':
97 print_version();
98 exit(0);
99 break;
100 case 'V':
101 verbose = 1;
102 break;
Anton Kochkov7e59f762010-06-29 21:13:20 +0000103 case 'i':
104 dump_idx = 1;
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100105 dump_ram = 1;
Peter Stuge11abcc0a2010-10-25 02:12:04 +0000106 break;
Alexander Couzensa5410dc2015-02-02 08:51:30 +0100107 case 'w':
108 write_addr = strtol(optarg , NULL, 16);
109 break;
110 case 'z':
111 write_data = strtol(optarg , NULL, 16);
112 break;
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100113 case 'd':
114 dump_ram = 1;
115 break;
Alexander Couzens0edf4192015-02-06 22:27:33 +0100116 case 'q':
117 dump_query = 1;
118 break;
Iru Cai2e8f4cc2018-01-25 21:44:09 +0800119 case 'p':
120 get_ports = 1;
121 break;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000122 case 'h':
123 case '?':
124 default:
125 print_usage(argv[0]);
126 exit(0);
127 break;
128 }
129 }
130
Iru Cai2e8f4cc2018-01-25 21:44:09 +0800131 if (get_ports && get_ec_ports() != 0)
132 fprintf(stderr, "Cannot get EC ports from /proc/ioports, "
133 "fallback to default.");
134
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000135 if (iopl(3)) {
136 printf("You need to be root.\n");
137 exit(1);
138 }
Alexander Couzensa5410dc2015-02-02 08:51:30 +0100139 if (write_addr >= 0 && write_data >= 0) {
140 write_addr &= 0xff;
141 write_data &= 0xff;
142 printf("\nWriting ec %02lx = %02lx\n", write_addr & 0xff, write_data & 0xff);
143 ec_write(write_addr & 0xff, write_data & 0xff);
144 }
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000145
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100146 /* preserve default - dump_ram if nothing selected */
Arthur Heymans878c2de2017-04-06 14:21:31 +0200147 if (!dump_ram && !dump_idx && !dump_query && (write_addr == -1))
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100148 dump_ram = 1;
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100149
150 if (dump_ram) {
151 printf("EC RAM:\n");
152 for (i = 0; i < 0x100; i++) {
153 if ((i % 0x10) == 0)
154 printf("\n%02x: ", i);
155 printf("%02x ", ec_read(i));
156 }
157 printf("\n\n");
158 }
Uwe Hermann257ae3f2009-04-22 12:28:14 +0000159
Alexander Couzens0edf4192015-02-06 22:27:33 +0100160 if (dump_query) {
161 printf("EC QUERY %02x\n", ec_query());
162 }
163
Anton Kochkov7e59f762010-06-29 21:13:20 +0000164 if (dump_idx) {
165 printf("EC IDX RAM:\n");
166 for (i = 0; i < 0x10000; i++) {
167 if ((i % 0x10) == 0)
168 printf("\n%04x: ", i);
169 printf("%02x ", ec_idx_read(i));
170 }
171 printf("\n\n");
Stefan Reinauer984e0f32010-01-16 17:50:55 +0000172 }
Stefan Reinauer984e0f32010-01-16 17:50:55 +0000173
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000174 return 0;
175}