blob: 6d9668ded22a7c359ea3afb223839e419998e5f2 [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"
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
Alexander Couzens0edf4192015-02-06 22:27:33 +010074int verbose = 0, dump_idx = 0, dump_ram = 0, dump_query = 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 Kochkov7e59f7692010-06-29 21:13:20 +000086 {"idx", 0, 0, 'i'},
Alexander Couzens0edf4192015-02-06 22:27:33 +010087 {"query", 0, 0, 'q'},
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000088 {0, 0, 0, 0}
89 };
90
Alexander Couzens0edf4192015-02-06 22:27:33 +010091 while ((opt = getopt_long(argc, argv, "vh?Vidqw:z:",
Uwe Hermann257ae3f2009-04-22 12:28:14 +000092 long_options, &option_index)) != EOF) {
Stefan Reinauerb5ab3232009-04-22 07:23:00 +000093 switch (opt) {
94 case 'v':
95 print_version();
96 exit(0);
97 break;
98 case 'V':
99 verbose = 1;
100 break;
Anton Kochkov7e59f7692010-06-29 21:13:20 +0000101 case 'i':
102 dump_idx = 1;
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100103 dump_ram = 1;
Peter Stuge11abcc0a2010-10-25 02:12:04 +0000104 break;
Alexander Couzensa5410dc2015-02-02 08:51:30 +0100105 case 'w':
106 write_addr = strtol(optarg , NULL, 16);
107 break;
108 case 'z':
109 write_data = strtol(optarg , NULL, 16);
110 break;
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100111 case 'd':
112 dump_ram = 1;
113 break;
Alexander Couzens0edf4192015-02-06 22:27:33 +0100114 case 'q':
115 dump_query = 1;
116 break;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000117 case 'h':
118 case '?':
119 default:
120 print_usage(argv[0]);
121 exit(0);
122 break;
123 }
124 }
125
126 if (iopl(3)) {
127 printf("You need to be root.\n");
128 exit(1);
129 }
Alexander Couzensa5410dc2015-02-02 08:51:30 +0100130 if (write_addr >= 0 && write_data >= 0) {
131 write_addr &= 0xff;
132 write_data &= 0xff;
133 printf("\nWriting ec %02lx = %02lx\n", write_addr & 0xff, write_data & 0xff);
134 ec_write(write_addr & 0xff, write_data & 0xff);
135 }
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000136
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100137 /* preserve default - dump_ram if nothing selected */
Alexander Couzens0004ae82015-07-02 19:48:22 +0200138 if (!dump_ram && !dump_idx && !dump_query && !write_addr) {
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100139 dump_ram = 1;
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000140 }
Alexander Couzens46ca3a52015-02-07 04:10:12 +0100141
142 if (dump_ram) {
143 printf("EC RAM:\n");
144 for (i = 0; i < 0x100; i++) {
145 if ((i % 0x10) == 0)
146 printf("\n%02x: ", i);
147 printf("%02x ", ec_read(i));
148 }
149 printf("\n\n");
150 }
Uwe Hermann257ae3f2009-04-22 12:28:14 +0000151
Alexander Couzens0edf4192015-02-06 22:27:33 +0100152 if (dump_query) {
153 printf("EC QUERY %02x\n", ec_query());
154 }
155
Anton Kochkov7e59f7692010-06-29 21:13:20 +0000156 if (dump_idx) {
157 printf("EC IDX RAM:\n");
158 for (i = 0; i < 0x10000; i++) {
159 if ((i % 0x10) == 0)
160 printf("\n%04x: ", i);
161 printf("%02x ", ec_idx_read(i));
162 }
163 printf("\n\n");
Stefan Reinauer984e0f32010-01-16 17:50:55 +0000164 }
Stefan Reinauer984e0f32010-01-16 17:50:55 +0000165
Stefan Reinauerb5ab3232009-04-22 07:23:00 +0000166 return 0;
167}