| 1 | /* |
|---|
| 2 | isadump.c - isadump, a user-space program to dump ISA registers |
|---|
| 3 | Copyright (C) 2000 Frodo Looijaard <frodol@dds.nl>, and |
|---|
| 4 | Mark D. Studebaker <mdsxyz123@yahoo.com> |
|---|
| 5 | Copyright (C) 2004 The lm_sensors group |
|---|
| 6 | |
|---|
| 7 | This program is free software; you can redistribute it and/or modify |
|---|
| 8 | it under the terms of the GNU General Public License as published by |
|---|
| 9 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | This program is distributed in the hope that it will be useful, |
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | GNU General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | You should have received a copy of the GNU General Public License |
|---|
| 18 | along with this program; if not, write to the Free Software |
|---|
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | /* |
|---|
| 23 | Typical usage: |
|---|
| 24 | isadump 0x295 0x296 Basic winbond dump using address/data registers |
|---|
| 25 | isadump 0x295 0x296 2 Winbond dump, bank 2 |
|---|
| 26 | isadump 0x2e 0x2f 0x09 0x07 Super-I/O, logical device 9 |
|---|
| 27 | isadump -f 0x5000 Flat address space dump like for Via 686a |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #include <stdio.h> |
|---|
| 31 | #include <stdlib.h> |
|---|
| 32 | #include <unistd.h> |
|---|
| 33 | #include <string.h> |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | /* To keep glibc2 happy */ |
|---|
| 37 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 0 |
|---|
| 38 | #include <sys/io.h> |
|---|
| 39 | #else |
|---|
| 40 | #include <asm/io.h> |
|---|
| 41 | #endif |
|---|
| 42 | |
|---|
| 43 | #ifdef __powerpc__ |
|---|
| 44 | unsigned long isa_io_base = 0; /* XXX for now */ |
|---|
| 45 | #endif /* __powerpc__ */ |
|---|
| 46 | |
|---|
| 47 | char hexchar(int i) |
|---|
| 48 | { |
|---|
| 49 | if ((i >= 0) && (i <= 9)) |
|---|
| 50 | return '0' + i; |
|---|
| 51 | else if (i <= 15) |
|---|
| 52 | return 'a' - 10 + i; |
|---|
| 53 | else |
|---|
| 54 | return 'X'; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | void help(void) |
|---|
| 58 | { |
|---|
| 59 | fprintf(stderr,"Syntax: isadump ADDRREG DATAREG [BANK [BANKREG]]\n"); |
|---|
| 60 | fprintf(stderr," isadump -f ADDRESS (for flat address space)\n"); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | int main(int argc, char *argv[]) |
|---|
| 64 | { |
|---|
| 65 | int addrreg, datareg = 0, bank = -1, bankreg = 0x4E; |
|---|
| 66 | int oldbank = 0; |
|---|
| 67 | int i,j,res; |
|---|
| 68 | int flat = 0; |
|---|
| 69 | char *end; |
|---|
| 70 | |
|---|
| 71 | if (argc < 3) { |
|---|
| 72 | help(); |
|---|
| 73 | exit(1); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | if(strcmp(argv[1], "-f")) { |
|---|
| 77 | addrreg = strtol(argv[1],&end,0); |
|---|
| 78 | } else { |
|---|
| 79 | if(argc != 3) { |
|---|
| 80 | help(); |
|---|
| 81 | exit(1); |
|---|
| 82 | } |
|---|
| 83 | flat = 1; |
|---|
| 84 | addrreg = strtol(argv[2],&end,0) & 0xff00; |
|---|
| 85 | } |
|---|
| 86 | if (*end) { |
|---|
| 87 | fprintf(stderr,"Error: Invalid address!\n"); |
|---|
| 88 | help(); |
|---|
| 89 | exit(1); |
|---|
| 90 | } |
|---|
| 91 | if ((addrreg < 0) || (addrreg > 0xffff)) { |
|---|
| 92 | fprintf(stderr,"Error: Address out of range!\n"); |
|---|
| 93 | help(); |
|---|
| 94 | exit(1); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | if(flat) |
|---|
| 98 | goto START; |
|---|
| 99 | |
|---|
| 100 | datareg = strtol(argv[2],&end,0); |
|---|
| 101 | if (*end) { |
|---|
| 102 | fprintf(stderr,"Error: Invalid data register!\n"); |
|---|
| 103 | help(); |
|---|
| 104 | exit(1); |
|---|
| 105 | } |
|---|
| 106 | if ((datareg < 0) || (datareg > 0xffff)) { |
|---|
| 107 | fprintf(stderr,"Error: Data register out of range!\n"); |
|---|
| 108 | help(); |
|---|
| 109 | exit(1); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | if(argc > 3) { |
|---|
| 113 | bank = strtol(argv[3],&end,0); |
|---|
| 114 | if (*end) { |
|---|
| 115 | fprintf(stderr,"Error: Invalid bank number!\n"); |
|---|
| 116 | help(); |
|---|
| 117 | exit(1); |
|---|
| 118 | } |
|---|
| 119 | if ((bank < 0) || (bank > 15)) { |
|---|
| 120 | fprintf(stderr,"Error: bank out of range (0-15)!\n"); |
|---|
| 121 | help(); |
|---|
| 122 | exit(1); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | if(argc > 4) { |
|---|
| 126 | bankreg = strtol(argv[4],&end,0); |
|---|
| 127 | if (*end) { |
|---|
| 128 | fprintf(stderr,"Error: Invalid bank register!\n"); |
|---|
| 129 | help(); |
|---|
| 130 | exit(1); |
|---|
| 131 | } |
|---|
| 132 | if ((bankreg < 0) || (bankreg > 0xff)) { |
|---|
| 133 | fprintf(stderr,"Error: bank out of range (0-0xff)!\n"); |
|---|
| 134 | help(); |
|---|
| 135 | exit(1); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | START: |
|---|
| 141 | |
|---|
| 142 | if (getuid()) { |
|---|
| 143 | fprintf(stderr,"Error: Can only be run as root (or make it suid root)\n"); |
|---|
| 144 | exit(1); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | fprintf(stderr," WARNING! Running this program can cause system crashes, " |
|---|
| 148 | "data loss and worse!\n"); |
|---|
| 149 | if(flat) |
|---|
| 150 | fprintf(stderr," I will probe address range 0x%04x to " |
|---|
| 151 | "0x%04x.\n",addrreg, addrreg + 0xff); |
|---|
| 152 | else |
|---|
| 153 | fprintf(stderr," I will probe address register 0x%04x and " |
|---|
| 154 | "data register 0x%04x.\n",addrreg,datareg); |
|---|
| 155 | if(bank>=0) |
|---|
| 156 | fprintf(stderr," Probing bank %d using bank register 0x%02x.\n", |
|---|
| 157 | bank, bankreg); |
|---|
| 158 | fprintf(stderr," You have five seconds to reconsider and press CTRL-C!\n\n"); |
|---|
| 159 | sleep(5); |
|---|
| 160 | |
|---|
| 161 | #ifndef __powerpc__ |
|---|
| 162 | if ((datareg < 0x400) && (addrreg < 0x400) && !flat) { |
|---|
| 163 | if(ioperm(datareg,1,1)) { |
|---|
| 164 | fprintf(stderr,"Error: Could not ioperm() data register!\n"); |
|---|
| 165 | exit(1); |
|---|
| 166 | } |
|---|
| 167 | if(ioperm(addrreg,1,1)) { |
|---|
| 168 | fprintf(stderr,"Error: Could not ioperm() address register!\n"); |
|---|
| 169 | exit(1); |
|---|
| 170 | } |
|---|
| 171 | } else { |
|---|
| 172 | if(iopl(3)) { |
|---|
| 173 | fprintf(stderr,"Error: Could not do iopl(3)!\n"); |
|---|
| 174 | exit(1); |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | #endif |
|---|
| 178 | |
|---|
| 179 | if(bank>=0) { |
|---|
| 180 | outb(bankreg,addrreg); |
|---|
| 181 | oldbank=inb(datareg); |
|---|
| 182 | outb(bank,datareg); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); |
|---|
| 186 | for (i = 0; i < 256; i+=16) { |
|---|
| 187 | printf("%c0: ",hexchar(i/16)); |
|---|
| 188 | for(j = 0; j < 16; j++) { |
|---|
| 189 | if(flat) { |
|---|
| 190 | res = inb(addrreg + i + j); |
|---|
| 191 | } else { |
|---|
| 192 | outb(i+j,addrreg); |
|---|
| 193 | res = inb(datareg); |
|---|
| 194 | } |
|---|
| 195 | printf("%c%c ",hexchar(res/16),hexchar(res%16)); |
|---|
| 196 | } |
|---|
| 197 | printf("\n"); |
|---|
| 198 | } |
|---|
| 199 | if(bank>=0) { |
|---|
| 200 | outb(bankreg,addrreg); |
|---|
| 201 | outb(oldbank,datareg); /* restore original value */ |
|---|
| 202 | } |
|---|
| 203 | exit(0); |
|---|
| 204 | } |
|---|