blob: cb6f280f4c74b0a4527c8994b29c1355e1f242b0 [file] [log] [blame]
Patrick Georgiac959032020-05-05 22:49:26 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauerb5ab3232009-04-22 07:23:00 +00002
3#include <stdio.h>
4#include <stdint.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <getopt.h>
Steven Dee3236f7b2017-01-29 14:52:56 -05008#if !(defined __NetBSD__ || defined __OpenBSD__)
Stefan Reinauerb5ab3232009-04-22 07:23:00 +00009#include <sys/io.h>
Andrey Korolyov393d9322016-01-05 14:27:59 +030010#endif
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000011#include <ec.h>
12
Steven Dee3236f7b2017-01-29 14:52:56 -050013#if defined __NetBSD__ || defined __OpenBSD__
14
Andrey Korolyov393d9322016-01-05 14:27:59 +030015#include <machine/sysarch.h>
Steven Dee3236f7b2017-01-29 14:52:56 -050016
17# if defined __i386__
18# define iopl i386_iopl
19# elif defined __NetBSD__
20# define iopl x86_64_iopl
21# else
22# define iopl amd64_iopl
23# endif
24
Andrey Korolyov393d9322016-01-05 14:27:59 +030025#endif
26
27
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000028#define ECTOOL_VERSION "0.1"
29
30void print_version(void)
31{
32 printf("ectool v%s -- ", ECTOOL_VERSION);
33 printf("Copyright (C) 2008-2009 coresystems GmbH\n\n");
34 printf(
Uwe Hermann257ae3f2009-04-22 12:28:14 +000035 "This program is free software: you can redistribute it and/or modify\n"
36 "it under the terms of the GNU General Public License as published by\n"
37 "the Free Software Foundation, version 2 of the License.\n\n"
38 "This program is distributed in the hope that it will be useful,\n"
39 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
40 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
Martin Rotheb20e602016-01-12 13:30:50 -070041 "GNU General Public License for more details.\n\n");
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000042}
43
44void print_usage(const char *name)
45{
Alexander Couzens0edf4192015-02-06 22:27:33 +010046 printf("usage: %s [-vh?Vidq] [-w 0x<addr> -z 0x<data>]\n", name);
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000047 printf("\n"
Uwe Hermann257ae3f2009-04-22 12:28:14 +000048 " -v | --version: print the version\n"
49 " -h | --help: print this help\n\n"
50 " -V | --verbose: print debug information\n"
Iru Cai2e8f4cc2018-01-25 21:44:09 +080051 " -p | --getports: get EC data & cmd ports from /proc/ioports\n"
Alexander Couzens46ca3a52015-02-07 04:10:12 +010052 " -d | --dump: print RAM\n"
53 " -i | --idx: print IDX RAM & RAM\n"
Alexander Couzens0edf4192015-02-06 22:27:33 +010054 " -q | --query: print query byte\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
Iru Cai2e8f4cc2018-01-25 21:44:09 +080061int verbose = 0, dump_idx = 0, dump_ram = 0, dump_query = 0, get_ports = 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'},
Alexander Couzens0edf4192015-02-06 22:27:33 +010074 {"query", 0, 0, 'q'},
Iru Cai2e8f4cc2018-01-25 21:44:09 +080075 {"getports", 0, 0, 'p'},
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000076 {0, 0, 0, 0}
77 };
78
Arthur Heymanscc7a4112018-08-01 17:22:47 +020079 if (argv[1] == NULL) {
80 print_usage(argv[0]);
81 exit(1);
82 }
83
Iru Cai2e8f4cc2018-01-25 21:44:09 +080084 while ((opt = getopt_long(argc, argv, "vh?Vidqpw:z:",
Uwe Hermann257ae3f2009-04-22 12:28:14 +000085 long_options, &option_index)) != EOF) {
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000086 switch (opt) {
87 case 'v':
88 print_version();
89 exit(0);
90 break;
91 case 'V':
92 verbose = 1;
93 break;
Anton Kochkov7e59f7692010-06-29 21:13:20 +000094 case 'i':
95 dump_idx = 1;
Alexander Couzens46ca3a52015-02-07 04:10:12 +010096 dump_ram = 1;
Peter Stuge11abcc0a2010-10-25 02:12:04 +000097 break;
Alexander Couzensa5410dc2015-02-02 08:51:30 +010098 case 'w':
99 write_addr = strtol(optarg , NULL, 16);
100 break;
101 case 'z':
102 write_data = strtol(optarg , NULL, 16);
103 break;
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100104 case 'd':
105 dump_ram = 1;
106 break;
Alexander Couzens0edf4192015-02-06 22:27:33 +0100107 case 'q':
108 dump_query = 1;
109 break;
Iru Cai2e8f4cc2018-01-25 21:44:09 +0800110 case 'p':
111 get_ports = 1;
112 break;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000113 case 'h':
114 case '?':
115 default:
116 print_usage(argv[0]);
117 exit(0);
118 break;
119 }
120 }
121
Arthur Heymanscc7a4112018-08-01 17:22:47 +0200122 if (optind < argc) {
123 fprintf(stderr, "Error: Extra parameter found.\n");
124 print_usage(argv[0]);
125 exit(1);
126 }
127
Iru Cai2e8f4cc2018-01-25 21:44:09 +0800128 if (get_ports && get_ec_ports() != 0)
129 fprintf(stderr, "Cannot get EC ports from /proc/ioports, "
Stefan Reinauer9d50efe2020-10-22 22:10:12 +0000130 "fallback to default.\n");
Iru Cai2e8f4cc2018-01-25 21:44:09 +0800131
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000132 if (iopl(3)) {
133 printf("You need to be root.\n");
134 exit(1);
135 }
Alexander Couzensa5410dc2015-02-02 08:51:30 +0100136 if (write_addr >= 0 && write_data >= 0) {
137 write_addr &= 0xff;
138 write_data &= 0xff;
139 printf("\nWriting ec %02lx = %02lx\n", write_addr & 0xff, write_data & 0xff);
140 ec_write(write_addr & 0xff, write_data & 0xff);
141 }
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000142
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100143 /* preserve default - dump_ram if nothing selected */
Arthur Heymans878c2de2017-04-06 14:21:31 +0200144 if (!dump_ram && !dump_idx && !dump_query && (write_addr == -1))
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100145 dump_ram = 1;
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100146
147 if (dump_ram) {
148 printf("EC RAM:\n");
149 for (i = 0; i < 0x100; i++) {
150 if ((i % 0x10) == 0)
151 printf("\n%02x: ", i);
152 printf("%02x ", ec_read(i));
153 }
154 printf("\n\n");
155 }
Uwe Hermann257ae3f2009-04-22 12:28:14 +0000156
Alexander Couzens0edf4192015-02-06 22:27:33 +0100157 if (dump_query) {
158 printf("EC QUERY %02x\n", ec_query());
159 }
160
Anton Kochkov7e59f7692010-06-29 21:13:20 +0000161 if (dump_idx) {
162 printf("EC IDX RAM:\n");
163 for (i = 0; i < 0x10000; i++) {
164 if ((i % 0x10) == 0)
165 printf("\n%04x: ", i);
166 printf("%02x ", ec_idx_read(i));
167 }
168 printf("\n\n");
Stefan Reinauer984e0f32010-01-16 17:50:55 +0000169 }
Stefan Reinauer984e0f32010-01-16 17:50:55 +0000170
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000171 return 0;
172}