Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdint.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <unistd.h> |
| 7 | #include <getopt.h> |
Steven Dee | 3236f7b | 2017-01-29 14:52:56 -0500 | [diff] [blame] | 8 | #if !(defined __NetBSD__ || defined __OpenBSD__) |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 9 | #include <sys/io.h> |
Andrey Korolyov | 393d932 | 2016-01-05 14:27:59 +0300 | [diff] [blame] | 10 | #endif |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 11 | #include <ec.h> |
| 12 | |
Steven Dee | 3236f7b | 2017-01-29 14:52:56 -0500 | [diff] [blame] | 13 | #if defined __NetBSD__ || defined __OpenBSD__ |
| 14 | |
Andrey Korolyov | 393d932 | 2016-01-05 14:27:59 +0300 | [diff] [blame] | 15 | #include <machine/sysarch.h> |
Steven Dee | 3236f7b | 2017-01-29 14:52:56 -0500 | [diff] [blame] | 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 | |
Andrey Korolyov | 393d932 | 2016-01-05 14:27:59 +0300 | [diff] [blame] | 25 | #endif |
| 26 | |
| 27 | |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 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( |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 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" |
Martin Roth | eb20e60 | 2016-01-12 13:30:50 -0700 | [diff] [blame] | 41 | "GNU General Public License for more details.\n\n"); |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void print_usage(const char *name) |
| 45 | { |
Alexander Couzens | 0edf419 | 2015-02-06 22:27:33 +0100 | [diff] [blame] | 46 | 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] | 47 | printf("\n" |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 48 | " -v | --version: print the version\n" |
| 49 | " -h | --help: print this help\n\n" |
| 50 | " -V | --verbose: print debug information\n" |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 51 | " -p | --getports: get EC data & cmd ports from /proc/ioports\n" |
Alexander Couzens | 46ca3a5 | 2015-02-07 04:10:12 +0100 | [diff] [blame] | 52 | " -d | --dump: print RAM\n" |
| 53 | " -i | --idx: print IDX RAM & RAM\n" |
Alexander Couzens | 0edf419 | 2015-02-06 22:27:33 +0100 | [diff] [blame] | 54 | " -q | --query: print query byte\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 | |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 61 | int verbose = 0, dump_idx = 0, dump_ram = 0, dump_query = 0, get_ports = 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'}, |
Alexander Couzens | 0edf419 | 2015-02-06 22:27:33 +0100 | [diff] [blame] | 74 | {"query", 0, 0, 'q'}, |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 75 | {"getports", 0, 0, 'p'}, |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 76 | {0, 0, 0, 0} |
| 77 | }; |
| 78 | |
Arthur Heymans | cc7a411 | 2018-08-01 17:22:47 +0200 | [diff] [blame] | 79 | if (argv[1] == NULL) { |
| 80 | print_usage(argv[0]); |
| 81 | exit(1); |
| 82 | } |
| 83 | |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 84 | while ((opt = getopt_long(argc, argv, "vh?Vidqpw:z:", |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 85 | long_options, &option_index)) != EOF) { |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 86 | switch (opt) { |
| 87 | case 'v': |
| 88 | print_version(); |
| 89 | exit(0); |
| 90 | break; |
| 91 | case 'V': |
| 92 | verbose = 1; |
| 93 | break; |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 94 | case 'i': |
| 95 | dump_idx = 1; |
Alexander Couzens | 46ca3a5 | 2015-02-07 04:10:12 +0100 | [diff] [blame] | 96 | dump_ram = 1; |
Peter Stuge | 11abcc0a | 2010-10-25 02:12:04 +0000 | [diff] [blame] | 97 | break; |
Alexander Couzens | a5410dc | 2015-02-02 08:51:30 +0100 | [diff] [blame] | 98 | case 'w': |
| 99 | write_addr = strtol(optarg , NULL, 16); |
| 100 | break; |
| 101 | case 'z': |
| 102 | write_data = strtol(optarg , NULL, 16); |
| 103 | break; |
Alexander Couzens | 46ca3a5 | 2015-02-07 04:10:12 +0100 | [diff] [blame] | 104 | case 'd': |
| 105 | dump_ram = 1; |
| 106 | break; |
Alexander Couzens | 0edf419 | 2015-02-06 22:27:33 +0100 | [diff] [blame] | 107 | case 'q': |
| 108 | dump_query = 1; |
| 109 | break; |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 110 | case 'p': |
| 111 | get_ports = 1; |
| 112 | break; |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 113 | case 'h': |
| 114 | case '?': |
| 115 | default: |
| 116 | print_usage(argv[0]); |
| 117 | exit(0); |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
Arthur Heymans | cc7a411 | 2018-08-01 17:22:47 +0200 | [diff] [blame] | 122 | if (optind < argc) { |
| 123 | fprintf(stderr, "Error: Extra parameter found.\n"); |
| 124 | print_usage(argv[0]); |
| 125 | exit(1); |
| 126 | } |
| 127 | |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 128 | if (get_ports && get_ec_ports() != 0) |
| 129 | fprintf(stderr, "Cannot get EC ports from /proc/ioports, " |
Stefan Reinauer | 9d50efe | 2020-10-22 22:10:12 +0000 | [diff] [blame] | 130 | "fallback to default.\n"); |
Iru Cai | 2e8f4cc | 2018-01-25 21:44:09 +0800 | [diff] [blame] | 131 | |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 132 | if (iopl(3)) { |
| 133 | printf("You need to be root.\n"); |
| 134 | exit(1); |
| 135 | } |
Alexander Couzens | a5410dc | 2015-02-02 08:51:30 +0100 | [diff] [blame] | 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 | } |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 142 | |
Alexander Couzens | 46ca3a5 | 2015-02-07 04:10:12 +0100 | [diff] [blame] | 143 | /* preserve default - dump_ram if nothing selected */ |
Arthur Heymans | 878c2de | 2017-04-06 14:21:31 +0200 | [diff] [blame] | 144 | if (!dump_ram && !dump_idx && !dump_query && (write_addr == -1)) |
Alexander Couzens | 46ca3a5 | 2015-02-07 04:10:12 +0100 | [diff] [blame] | 145 | dump_ram = 1; |
Alexander Couzens | 46ca3a5 | 2015-02-07 04:10:12 +0100 | [diff] [blame] | 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 | } |
Uwe Hermann | 257ae3f | 2009-04-22 12:28:14 +0000 | [diff] [blame] | 156 | |
Alexander Couzens | 0edf419 | 2015-02-06 22:27:33 +0100 | [diff] [blame] | 157 | if (dump_query) { |
| 158 | printf("EC QUERY %02x\n", ec_query()); |
| 159 | } |
| 160 | |
Anton Kochkov | 7e59f769 | 2010-06-29 21:13:20 +0000 | [diff] [blame] | 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"); |
Stefan Reinauer | 984e0f3 | 2010-01-16 17:50:55 +0000 | [diff] [blame] | 169 | } |
Stefan Reinauer | 984e0f3 | 2010-01-16 17:50:55 +0000 | [diff] [blame] | 170 | |
Stefan Reinauer | b5ab323 | 2009-04-22 07:23:00 +0000 | [diff] [blame] | 171 | return 0; |
| 172 | } |