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. |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <stdint.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | #include <getopt.h> |
| 21 | #include <sys/io.h> |
| 22 | #include <ec.h> |
Alexander Couzens | a5410dc | 2015-02-02 08:51:30 +0100 | [diff] [blame] | 23 | #include <stdlib.h> |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 24 | |
| 25 | #define ECTOOL_VERSION "0.1" |
| 26 | |
| 27 | void print_version(void) |
| 28 | { |
| 29 | printf("ectool v%s -- ", ECTOOL_VERSION); |
| 30 | printf("Copyright (C) 2008-2009 coresystems GmbH\n\n"); |
| 31 | printf( |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 32 | "This program is free software: you can redistribute it and/or modify\n" |
| 33 | "it under the terms of the GNU General Public License as published by\n" |
| 34 | "the Free Software Foundation, version 2 of the License.\n\n" |
| 35 | "This program is distributed in the hope that it will be useful,\n" |
| 36 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" |
| 37 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" |
| 38 | "GNU General Public License for more details.\n\n" |
| 39 | "You should have received a copy of the GNU General Public License\n" |
| 40 | "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] | 41 | } |
| 42 | |
| 43 | void print_usage(const char *name) |
| 44 | { |
Alexander Couzens | 0edf419 | 2015-02-06 22:27:33 +0100 | [diff] [blame] | 45 | printf("usage: %s [-vh?Vidq] [-w 0x<addr> -z 0x<data>]\n", name); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 46 | printf("\n" |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 47 | " -v | --version: print the version\n" |
| 48 | " -h | --help: print this help\n\n" |
| 49 | " -V | --verbose: print debug information\n" |
Alexander Couzens | 46ca3a5 | 2015-02-07 04:10:12 +0100 | [diff] [blame] | 50 | " -d | --dump: print RAM\n" |
| 51 | " -i | --idx: print IDX RAM & RAM\n" |
Alexander Couzens | 0edf419 | 2015-02-06 22:27:33 +0100 | [diff] [blame] | 52 | " -q | --query: print query byte\n" |
Alexander Couzens | a5410dc | 2015-02-02 08:51:30 +0100 | [diff] [blame] | 53 | " -w <addr in hex> write to addr\n" |
| 54 | " -z <data in hex> write to data\n" |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 55 | "\n"); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 56 | exit(1); |
| 57 | } |
| 58 | |
Alexander Couzens | 0edf419 | 2015-02-06 22:27:33 +0100 | [diff] [blame] | 59 | int verbose = 0, dump_idx = 0, dump_ram = 0, dump_query = 0; |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 60 | |
| 61 | int main(int argc, char *argv[]) |
| 62 | { |
| 63 | int i, opt, option_index = 0; |
Alexander Couzens | a5410dc | 2015-02-02 08:51:30 +0100 | [diff] [blame] | 64 | long write_data = -1; |
| 65 | long write_addr = -1; |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 66 | |
| 67 | static struct option long_options[] = { |
| 68 | {"version", 0, 0, 'v'}, |
| 69 | {"help", 0, 0, 'h'}, |
| 70 | {"verbose", 0, 0, 'V'}, |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 71 | {"idx", 0, 0, 'i'}, |
Alexander Couzens | 0edf419 | 2015-02-06 22:27:33 +0100 | [diff] [blame] | 72 | {"query", 0, 0, 'q'}, |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 73 | {0, 0, 0, 0} |
| 74 | }; |
| 75 | |
Alexander Couzens | 0edf419 | 2015-02-06 22:27:33 +0100 | [diff] [blame] | 76 | while ((opt = getopt_long(argc, argv, "vh?Vidqw:z:", |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 77 | long_options, &option_index)) != EOF) { |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 78 | switch (opt) { |
| 79 | case 'v': |
| 80 | print_version(); |
| 81 | exit(0); |
| 82 | break; |
| 83 | case 'V': |
| 84 | verbose = 1; |
| 85 | break; |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 86 | case 'i': |
| 87 | dump_idx = 1; |
Alexander Couzens | 46ca3a5 | 2015-02-07 04:10:12 +0100 | [diff] [blame] | 88 | dump_ram = 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; |
Alexander Couzens | 46ca3a5 | 2015-02-07 04:10:12 +0100 | [diff] [blame] | 96 | case 'd': |
| 97 | dump_ram = 1; |
| 98 | break; |
Alexander Couzens | 0edf419 | 2015-02-06 22:27:33 +0100 | [diff] [blame] | 99 | case 'q': |
| 100 | dump_query = 1; |
| 101 | break; |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 102 | case 'h': |
| 103 | case '?': |
| 104 | default: |
| 105 | print_usage(argv[0]); |
| 106 | exit(0); |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (iopl(3)) { |
| 112 | printf("You need to be root.\n"); |
| 113 | exit(1); |
| 114 | } |
Alexander Couzens | a5410dc | 2015-02-02 08:51:30 +0100 | [diff] [blame] | 115 | if (write_addr >= 0 && write_data >= 0) { |
| 116 | write_addr &= 0xff; |
| 117 | write_data &= 0xff; |
| 118 | printf("\nWriting ec %02lx = %02lx\n", write_addr & 0xff, write_data & 0xff); |
| 119 | ec_write(write_addr & 0xff, write_data & 0xff); |
| 120 | } |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 121 | |
Alexander Couzens | 46ca3a5 | 2015-02-07 04:10:12 +0100 | [diff] [blame] | 122 | /* preserve default - dump_ram if nothing selected */ |
Alexander Couzens | 0004ae8 | 2015-07-02 19:48:22 +0200 | [diff] [blame] | 123 | if (!dump_ram && !dump_idx && !dump_query && !write_addr) { |
Alexander Couzens | 46ca3a5 | 2015-02-07 04:10:12 +0100 | [diff] [blame] | 124 | dump_ram = 1; |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 125 | } |
Alexander Couzens | 46ca3a5 | 2015-02-07 04:10:12 +0100 | [diff] [blame] | 126 | |
| 127 | if (dump_ram) { |
| 128 | printf("EC RAM:\n"); |
| 129 | for (i = 0; i < 0x100; i++) { |
| 130 | if ((i % 0x10) == 0) |
| 131 | printf("\n%02x: ", i); |
| 132 | printf("%02x ", ec_read(i)); |
| 133 | } |
| 134 | printf("\n\n"); |
| 135 | } |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 136 | |
Alexander Couzens | 0edf419 | 2015-02-06 22:27:33 +0100 | [diff] [blame] | 137 | if (dump_query) { |
| 138 | printf("EC QUERY %02x\n", ec_query()); |
| 139 | } |
| 140 | |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 141 | if (dump_idx) { |
| 142 | printf("EC IDX RAM:\n"); |
| 143 | for (i = 0; i < 0x10000; i++) { |
| 144 | if ((i % 0x10) == 0) |
| 145 | printf("\n%04x: ", i); |
| 146 | printf("%02x ", ec_idx_read(i)); |
| 147 | } |
| 148 | printf("\n\n"); |
Stefan Reinauer | 984e0f3 | 2010-01-16 17:50:55 +0000 | [diff] [blame] | 149 | } |
Stefan Reinauer | 984e0f3 | 2010-01-16 17:50:55 +0000 | [diff] [blame] | 150 | |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 151 | return 0; |
| 152 | } |