blob: 2af45c30f2232bbb6eed4002d4c69fd8e4dd9d2f [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>
25
Steven Dee3236f7b2017-01-29 14:52:56 -050026#if defined __NetBSD__ || defined __OpenBSD__
27
Andrey Korolyov393d9322016-01-05 14:27:59 +030028#include <machine/sysarch.h>
Steven Dee3236f7b2017-01-29 14:52:56 -050029
30# if defined __i386__
31# define iopl i386_iopl
32# elif defined __NetBSD__
33# define iopl x86_64_iopl
34# else
35# define iopl amd64_iopl
36# endif
37
Andrey Korolyov393d9322016-01-05 14:27:59 +030038#endif
39
40
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000041#define ECTOOL_VERSION "0.1"
42
43void print_version(void)
44{
45 printf("ectool v%s -- ", ECTOOL_VERSION);
46 printf("Copyright (C) 2008-2009 coresystems GmbH\n\n");
47 printf(
Uwe Hermann257ae3f2009-04-22 12:28:14 +000048 "This program is free software: you can redistribute it and/or modify\n"
49 "it under the terms of the GNU General Public License as published by\n"
50 "the Free Software Foundation, version 2 of the License.\n\n"
51 "This program is distributed in the hope that it will be useful,\n"
52 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
53 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
Martin Rotheb20e602016-01-12 13:30:50 -070054 "GNU General Public License for more details.\n\n");
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000055}
56
57void print_usage(const char *name)
58{
Alexander Couzens0edf4192015-02-06 22:27:33 +010059 printf("usage: %s [-vh?Vidq] [-w 0x<addr> -z 0x<data>]\n", name);
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000060 printf("\n"
Uwe Hermann257ae3f2009-04-22 12:28:14 +000061 " -v | --version: print the version\n"
62 " -h | --help: print this help\n\n"
63 " -V | --verbose: print debug information\n"
Iru Cai2e8f4cc2018-01-25 21:44:09 +080064 " -p | --getports: get EC data & cmd ports from /proc/ioports\n"
Alexander Couzens46ca3a52015-02-07 04:10:12 +010065 " -d | --dump: print RAM\n"
66 " -i | --idx: print IDX RAM & RAM\n"
Alexander Couzens0edf4192015-02-06 22:27:33 +010067 " -q | --query: print query byte\n"
Alexander Couzensa5410dc2015-02-02 08:51:30 +010068 " -w <addr in hex> write to addr\n"
69 " -z <data in hex> write to data\n"
Uwe Hermann257ae3f2009-04-22 12:28:14 +000070 "\n");
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000071 exit(1);
72}
73
Iru Cai2e8f4cc2018-01-25 21:44:09 +080074int verbose = 0, dump_idx = 0, dump_ram = 0, dump_query = 0, get_ports = 0;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000075
76int main(int argc, char *argv[])
77{
78 int i, opt, option_index = 0;
Alexander Couzensa5410dc2015-02-02 08:51:30 +010079 long write_data = -1;
80 long write_addr = -1;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000081
82 static struct option long_options[] = {
83 {"version", 0, 0, 'v'},
84 {"help", 0, 0, 'h'},
85 {"verbose", 0, 0, 'V'},
Anton Kochkov7e59f762010-06-29 21:13:20 +000086 {"idx", 0, 0, 'i'},
Alexander Couzens0edf4192015-02-06 22:27:33 +010087 {"query", 0, 0, 'q'},
Iru Cai2e8f4cc2018-01-25 21:44:09 +080088 {"getports", 0, 0, 'p'},
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000089 {0, 0, 0, 0}
90 };
91
Arthur Heymanscc7a4112018-08-01 17:22:47 +020092 if (argv[1] == NULL) {
93 print_usage(argv[0]);
94 exit(1);
95 }
96
Iru Cai2e8f4cc2018-01-25 21:44:09 +080097 while ((opt = getopt_long(argc, argv, "vh?Vidqpw:z:",
Uwe Hermann257ae3f2009-04-22 12:28:14 +000098 long_options, &option_index)) != EOF) {
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000099 switch (opt) {
100 case 'v':
101 print_version();
102 exit(0);
103 break;
104 case 'V':
105 verbose = 1;
106 break;
Anton Kochkov7e59f762010-06-29 21:13:20 +0000107 case 'i':
108 dump_idx = 1;
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100109 dump_ram = 1;
Peter Stuge11abcc0a2010-10-25 02:12:04 +0000110 break;
Alexander Couzensa5410dc2015-02-02 08:51:30 +0100111 case 'w':
112 write_addr = strtol(optarg , NULL, 16);
113 break;
114 case 'z':
115 write_data = strtol(optarg , NULL, 16);
116 break;
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100117 case 'd':
118 dump_ram = 1;
119 break;
Alexander Couzens0edf4192015-02-06 22:27:33 +0100120 case 'q':
121 dump_query = 1;
122 break;
Iru Cai2e8f4cc2018-01-25 21:44:09 +0800123 case 'p':
124 get_ports = 1;
125 break;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000126 case 'h':
127 case '?':
128 default:
129 print_usage(argv[0]);
130 exit(0);
131 break;
132 }
133 }
134
Arthur Heymanscc7a4112018-08-01 17:22:47 +0200135 if (optind < argc) {
136 fprintf(stderr, "Error: Extra parameter found.\n");
137 print_usage(argv[0]);
138 exit(1);
139 }
140
Iru Cai2e8f4cc2018-01-25 21:44:09 +0800141 if (get_ports && get_ec_ports() != 0)
142 fprintf(stderr, "Cannot get EC ports from /proc/ioports, "
143 "fallback to default.");
144
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000145 if (iopl(3)) {
146 printf("You need to be root.\n");
147 exit(1);
148 }
Alexander Couzensa5410dc2015-02-02 08:51:30 +0100149 if (write_addr >= 0 && write_data >= 0) {
150 write_addr &= 0xff;
151 write_data &= 0xff;
152 printf("\nWriting ec %02lx = %02lx\n", write_addr & 0xff, write_data & 0xff);
153 ec_write(write_addr & 0xff, write_data & 0xff);
154 }
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000155
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100156 /* preserve default - dump_ram if nothing selected */
Arthur Heymans878c2de2017-04-06 14:21:31 +0200157 if (!dump_ram && !dump_idx && !dump_query && (write_addr == -1))
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100158 dump_ram = 1;
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100159
160 if (dump_ram) {
161 printf("EC RAM:\n");
162 for (i = 0; i < 0x100; i++) {
163 if ((i % 0x10) == 0)
164 printf("\n%02x: ", i);
165 printf("%02x ", ec_read(i));
166 }
167 printf("\n\n");
168 }
Uwe Hermann257ae3f2009-04-22 12:28:14 +0000169
Alexander Couzens0edf4192015-02-06 22:27:33 +0100170 if (dump_query) {
171 printf("EC QUERY %02x\n", ec_query());
172 }
173
Anton Kochkov7e59f762010-06-29 21:13:20 +0000174 if (dump_idx) {
175 printf("EC IDX RAM:\n");
176 for (i = 0; i < 0x10000; i++) {
177 if ((i % 0x10) == 0)
178 printf("\n%04x: ", i);
179 printf("%02x ", ec_idx_read(i));
180 }
181 printf("\n\n");
Stefan Reinauer984e0f32010-01-16 17:50:55 +0000182 }
Stefan Reinauer984e0f32010-01-16 17:50:55 +0000183
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000184 return 0;
185}