Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 8 | * published by the Free Software Foundation; version 2 of the License. |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 9 | * |
| 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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <getopt.h> |
| 25 | #include <sys/io.h> |
| 26 | #include <ec.h> |
Alexander Couzens | a5410dc | 2015-02-02 08:51:30 +0100 | [diff] [blame] | 27 | #include <stdlib.h> |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 28 | |
| 29 | #define ECTOOL_VERSION "0.1" |
| 30 | |
| 31 | void print_version(void) |
| 32 | { |
| 33 | printf("ectool v%s -- ", ECTOOL_VERSION); |
| 34 | printf("Copyright (C) 2008-2009 coresystems GmbH\n\n"); |
| 35 | printf( |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 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" |
| 43 | "You should have received a copy of the GNU General Public License\n" |
| 44 | "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n"); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void print_usage(const char *name) |
| 48 | { |
Alexander Couzens | a5410dc | 2015-02-02 08:51:30 +0100 | [diff] [blame] | 49 | printf("usage: %s [-vh?Vi] [-w 0x<addr> -z 0x<data>]\n", name); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 50 | printf("\n" |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 51 | " -v | --version: print the version\n" |
| 52 | " -h | --help: print this help\n\n" |
| 53 | " -V | --verbose: print debug information\n" |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 54 | " -i | --idx: print IDX RAM\n" |
Alexander Couzens | a5410dc | 2015-02-02 08:51:30 +0100 | [diff] [blame] | 55 | " -w <addr in hex> write to addr\n" |
| 56 | " -z <data in hex> write to data\n" |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 57 | "\n"); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 58 | exit(1); |
| 59 | } |
| 60 | |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 61 | int verbose = 0, dump_idx = 0; |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 62 | |
| 63 | int main(int argc, char *argv[]) |
| 64 | { |
| 65 | int i, opt, option_index = 0; |
Alexander Couzens | a5410dc | 2015-02-02 08:51:30 +0100 | [diff] [blame] | 66 | long write_data = -1; |
| 67 | long write_addr = -1; |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 68 | |
| 69 | static struct option long_options[] = { |
| 70 | {"version", 0, 0, 'v'}, |
| 71 | {"help", 0, 0, 'h'}, |
| 72 | {"verbose", 0, 0, 'V'}, |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 73 | {"idx", 0, 0, 'i'}, |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 74 | {0, 0, 0, 0} |
| 75 | }; |
| 76 | |
Alexander Couzens | a5410dc | 2015-02-02 08:51:30 +0100 | [diff] [blame] | 77 | while ((opt = getopt_long(argc, argv, "vh?Viw:z:", |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 78 | long_options, &option_index)) != EOF) { |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 79 | switch (opt) { |
| 80 | case 'v': |
| 81 | print_version(); |
| 82 | exit(0); |
| 83 | break; |
| 84 | case 'V': |
| 85 | verbose = 1; |
| 86 | break; |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 87 | case 'i': |
| 88 | dump_idx = 1; |
Peter Stuge | 11abcc0a | 2010-10-25 02:12:04 +0000 | [diff] [blame] | 89 | break; |
Alexander Couzens | a5410dc | 2015-02-02 08:51:30 +0100 | [diff] [blame] | 90 | case 'w': |
| 91 | write_addr = strtol(optarg , NULL, 16); |
| 92 | break; |
| 93 | case 'z': |
| 94 | write_data = strtol(optarg , NULL, 16); |
| 95 | break; |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 96 | case 'h': |
| 97 | case '?': |
| 98 | default: |
| 99 | print_usage(argv[0]); |
| 100 | exit(0); |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (iopl(3)) { |
| 106 | printf("You need to be root.\n"); |
| 107 | exit(1); |
| 108 | } |
Alexander Couzens | a5410dc | 2015-02-02 08:51:30 +0100 | [diff] [blame] | 109 | if (write_addr >= 0 && write_data >= 0) { |
| 110 | write_addr &= 0xff; |
| 111 | write_data &= 0xff; |
| 112 | printf("\nWriting ec %02lx = %02lx\n", write_addr & 0xff, write_data & 0xff); |
| 113 | ec_write(write_addr & 0xff, write_data & 0xff); |
| 114 | } |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 115 | |
| 116 | printf("EC RAM:\n"); |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 117 | for (i = 0; i < 0x100; i++) { |
| 118 | if ((i % 0x10) == 0) |
| 119 | printf("\n%02x: ", i); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 120 | printf("%02x ", ec_read(i)); |
| 121 | } |
| 122 | printf("\n\n"); |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 123 | |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 124 | if (dump_idx) { |
| 125 | printf("EC IDX RAM:\n"); |
| 126 | for (i = 0; i < 0x10000; i++) { |
| 127 | if ((i % 0x10) == 0) |
| 128 | printf("\n%04x: ", i); |
| 129 | printf("%02x ", ec_idx_read(i)); |
| 130 | } |
| 131 | printf("\n\n"); |
Stefan Reinauer | 984e0f3 | 2010-01-16 17:50:55 +0000 | [diff] [blame] | 132 | } |
Stefan Reinauer | 984e0f3 | 2010-01-16 17:50:55 +0000 | [diff] [blame] | 133 | |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | } |