treewide: replace GPLv2 long form headers with SPDX header
[coreboot.git] / util / ectool / ectool.c
bloba2c3eef465e5e9b4f183017ba6b5c20b4ec154b7
1 /* This file is part of the ectool project. */
2 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <stdio.h>
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <getopt.h>
9 #if !(defined __NetBSD__ || defined __OpenBSD__)
10 #include <sys/io.h>
11 #endif
12 #include <ec.h>
14 #if defined __NetBSD__ || defined __OpenBSD__
16 #include <machine/sysarch.h>
18 # if defined __i386__
19 # define iopl i386_iopl
20 # elif defined __NetBSD__
21 # define iopl x86_64_iopl
22 # else
23 # define iopl amd64_iopl
24 # endif
26 #endif
29 #define ECTOOL_VERSION "0.1"
31 void print_version(void)
33 printf("ectool v%s -- ", ECTOOL_VERSION);
34 printf("Copyright (C) 2008-2009 coresystems GmbH\n\n");
35 printf(
36 "This program is free software: you can redistribute it and/or modify\n"
37 "it under the terms of the GNU General Public License as published by\n"
38 "the Free Software Foundation, version 2 of the License.\n\n"
39 "This program is distributed in the hope that it will be useful,\n"
40 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
41 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
42 "GNU General Public License for more details.\n\n");
45 void print_usage(const char *name)
47 printf("usage: %s [-vh?Vidq] [-w 0x<addr> -z 0x<data>]\n", name);
48 printf("\n"
49 " -v | --version: print the version\n"
50 " -h | --help: print this help\n\n"
51 " -V | --verbose: print debug information\n"
52 " -p | --getports: get EC data & cmd ports from /proc/ioports\n"
53 " -d | --dump: print RAM\n"
54 " -i | --idx: print IDX RAM & RAM\n"
55 " -q | --query: print query byte\n"
56 " -w <addr in hex> write to addr\n"
57 " -z <data in hex> write to data\n"
58 "\n");
59 exit(1);
62 int verbose = 0, dump_idx = 0, dump_ram = 0, dump_query = 0, get_ports = 0;
64 int main(int argc, char *argv[])
66 int i, opt, option_index = 0;
67 long write_data = -1;
68 long write_addr = -1;
70 static struct option long_options[] = {
71 {"version", 0, 0, 'v'},
72 {"help", 0, 0, 'h'},
73 {"verbose", 0, 0, 'V'},
74 {"idx", 0, 0, 'i'},
75 {"query", 0, 0, 'q'},
76 {"getports", 0, 0, 'p'},
77 {0, 0, 0, 0}
80 if (argv[1] == NULL) {
81 print_usage(argv[0]);
82 exit(1);
85 while ((opt = getopt_long(argc, argv, "vh?Vidqpw:z:",
86 long_options, &option_index)) != EOF) {
87 switch (opt) {
88 case 'v':
89 print_version();
90 exit(0);
91 break;
92 case 'V':
93 verbose = 1;
94 break;
95 case 'i':
96 dump_idx = 1;
97 dump_ram = 1;
98 break;
99 case 'w':
100 write_addr = strtol(optarg , NULL, 16);
101 break;
102 case 'z':
103 write_data = strtol(optarg , NULL, 16);
104 break;
105 case 'd':
106 dump_ram = 1;
107 break;
108 case 'q':
109 dump_query = 1;
110 break;
111 case 'p':
112 get_ports = 1;
113 break;
114 case 'h':
115 case '?':
116 default:
117 print_usage(argv[0]);
118 exit(0);
119 break;
123 if (optind < argc) {
124 fprintf(stderr, "Error: Extra parameter found.\n");
125 print_usage(argv[0]);
126 exit(1);
129 if (get_ports && get_ec_ports() != 0)
130 fprintf(stderr, "Cannot get EC ports from /proc/ioports, "
131 "fallback to default.");
133 if (iopl(3)) {
134 printf("You need to be root.\n");
135 exit(1);
137 if (write_addr >= 0 && write_data >= 0) {
138 write_addr &= 0xff;
139 write_data &= 0xff;
140 printf("\nWriting ec %02lx = %02lx\n", write_addr & 0xff, write_data & 0xff);
141 ec_write(write_addr & 0xff, write_data & 0xff);
144 /* preserve default - dump_ram if nothing selected */
145 if (!dump_ram && !dump_idx && !dump_query && (write_addr == -1))
146 dump_ram = 1;
148 if (dump_ram) {
149 printf("EC RAM:\n");
150 for (i = 0; i < 0x100; i++) {
151 if ((i % 0x10) == 0)
152 printf("\n%02x: ", i);
153 printf("%02x ", ec_read(i));
155 printf("\n\n");
158 if (dump_query) {
159 printf("EC QUERY %02x\n", ec_query());
162 if (dump_idx) {
163 printf("EC IDX RAM:\n");
164 for (i = 0; i < 0x10000; i++) {
165 if ((i % 0x10) == 0)
166 printf("\n%04x: ", i);
167 printf("%02x ", ec_idx_read(i));
169 printf("\n\n");
172 return 0;