util/liveiso/nixos: Install various extractor tools
[coreboot.git] / util / ectool / ectool.c
blobcb6f280f4c74b0a4527c8994b29c1355e1f242b0
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <getopt.h>
8 #if !(defined __NetBSD__ || defined __OpenBSD__)
9 #include <sys/io.h>
10 #endif
11 #include <ec.h>
13 #if defined __NetBSD__ || defined __OpenBSD__
15 #include <machine/sysarch.h>
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
25 #endif
28 #define ECTOOL_VERSION "0.1"
30 void print_version(void)
32 printf("ectool v%s -- ", ECTOOL_VERSION);
33 printf("Copyright (C) 2008-2009 coresystems GmbH\n\n");
34 printf(
35 "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"
41 "GNU General Public License for more details.\n\n");
44 void print_usage(const char *name)
46 printf("usage: %s [-vh?Vidq] [-w 0x<addr> -z 0x<data>]\n", name);
47 printf("\n"
48 " -v | --version: print the version\n"
49 " -h | --help: print this help\n\n"
50 " -V | --verbose: print debug information\n"
51 " -p | --getports: get EC data & cmd ports from /proc/ioports\n"
52 " -d | --dump: print RAM\n"
53 " -i | --idx: print IDX RAM & RAM\n"
54 " -q | --query: print query byte\n"
55 " -w <addr in hex> write to addr\n"
56 " -z <data in hex> write to data\n"
57 "\n");
58 exit(1);
61 int verbose = 0, dump_idx = 0, dump_ram = 0, dump_query = 0, get_ports = 0;
63 int main(int argc, char *argv[])
65 int i, opt, option_index = 0;
66 long write_data = -1;
67 long write_addr = -1;
69 static struct option long_options[] = {
70 {"version", 0, 0, 'v'},
71 {"help", 0, 0, 'h'},
72 {"verbose", 0, 0, 'V'},
73 {"idx", 0, 0, 'i'},
74 {"query", 0, 0, 'q'},
75 {"getports", 0, 0, 'p'},
76 {0, 0, 0, 0}
79 if (argv[1] == NULL) {
80 print_usage(argv[0]);
81 exit(1);
84 while ((opt = getopt_long(argc, argv, "vh?Vidqpw:z:",
85 long_options, &option_index)) != EOF) {
86 switch (opt) {
87 case 'v':
88 print_version();
89 exit(0);
90 break;
91 case 'V':
92 verbose = 1;
93 break;
94 case 'i':
95 dump_idx = 1;
96 dump_ram = 1;
97 break;
98 case 'w':
99 write_addr = strtol(optarg , NULL, 16);
100 break;
101 case 'z':
102 write_data = strtol(optarg , NULL, 16);
103 break;
104 case 'd':
105 dump_ram = 1;
106 break;
107 case 'q':
108 dump_query = 1;
109 break;
110 case 'p':
111 get_ports = 1;
112 break;
113 case 'h':
114 case '?':
115 default:
116 print_usage(argv[0]);
117 exit(0);
118 break;
122 if (optind < argc) {
123 fprintf(stderr, "Error: Extra parameter found.\n");
124 print_usage(argv[0]);
125 exit(1);
128 if (get_ports && get_ec_ports() != 0)
129 fprintf(stderr, "Cannot get EC ports from /proc/ioports, "
130 "fallback to default.\n");
132 if (iopl(3)) {
133 printf("You need to be root.\n");
134 exit(1);
136 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);
143 /* preserve default - dump_ram if nothing selected */
144 if (!dump_ram && !dump_idx && !dump_query && (write_addr == -1))
145 dump_ram = 1;
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));
154 printf("\n\n");
157 if (dump_query) {
158 printf("EC QUERY %02x\n", ec_query());
161 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));
168 printf("\n\n");
171 return 0;