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