/ util / ectool / ectool.c
ectool.c
  1  /* SPDX-License-Identifier: GPL-2.0-only */
  2  
  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>
 12  
 13  #if defined __NetBSD__ || defined __OpenBSD__
 14  
 15  #include <machine/sysarch.h>
 16  
 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  
 25  #endif
 26  
 27  
 28  #define ECTOOL_VERSION "0.1"
 29  
 30  void print_version(void)
 31  {
 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");
 42  }
 43  
 44  void print_usage(const char *name)
 45  {
 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);
 59  }
 60  
 61  int verbose = 0, dump_idx = 0, dump_ram = 0, dump_query = 0, get_ports = 0;
 62  
 63  int main(int argc, char *argv[])
 64  {
 65  	int i, opt, option_index = 0;
 66  	long write_data = -1;
 67  	long write_addr = -1;
 68  
 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}
 77  	};
 78  
 79  	if (argv[1] == NULL) {
 80  		print_usage(argv[0]);
 81  		exit(1);
 82  	}
 83  
 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;
119  		}
120  	}
121  
122  	if (optind < argc) {
123  		fprintf(stderr, "Error: Extra parameter found.\n");
124  		print_usage(argv[0]);
125  		exit(1);
126  	}
127  
128  	if (get_ports && get_ec_ports() != 0)
129  		fprintf(stderr, "Cannot get EC ports from /proc/ioports, "
130  				"fallback to default.\n");
131  
132  	if (iopl(3)) {
133  		printf("You need to be root.\n");
134  		exit(1);
135  	}
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);
141  	}
142  
143  	/* preserve default - dump_ram if nothing selected */
144  	if (!dump_ram && !dump_idx && !dump_query && (write_addr == -1))
145  		dump_ram = 1;
146  
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  	}
156  
157  	if (dump_query) {
158  		printf("EC QUERY %02x\n", ec_query());
159  	}
160  
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));
167  		}
168  		printf("\n\n");
169  	}
170  
171  	return 0;
172  }