| 1 | /* |
|---|
| 2 | isadump.c - Part of isadump, a user-space program to dump ISA registers |
|---|
| 3 | Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> |
|---|
| 4 | |
|---|
| 5 | This program is free software; you can redistribute it and/or modify |
|---|
| 6 | it under the terms of the GNU General Public License as published by |
|---|
| 7 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | (at your option) any later version. |
|---|
| 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 |
|---|
| 17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include <stdio.h> |
|---|
| 21 | #include <stdlib.h> |
|---|
| 22 | #include <unistd.h> |
|---|
| 23 | #include <asm/io.h> |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | /* To keep glibc2 happy */ |
|---|
| 27 | #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0 |
|---|
| 28 | #include <sys/perm.h> |
|---|
| 29 | #endif |
|---|
| 30 | |
|---|
| 31 | #ifdef __powerpc__ |
|---|
| 32 | unsigned long isa_io_base = 0; /* XXX for now */ |
|---|
| 33 | #endif /* __powerpc__ */ |
|---|
| 34 | |
|---|
| 35 | char hexchar(int i) |
|---|
| 36 | { |
|---|
| 37 | if ((i >= 0) && (i <= 9)) |
|---|
| 38 | return '0' + i; |
|---|
| 39 | else if (i <= 15) |
|---|
| 40 | return 'a' - 10 + i; |
|---|
| 41 | else |
|---|
| 42 | return 'X'; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | void help(void) |
|---|
| 46 | { |
|---|
| 47 | fprintf(stderr,"Syntax: isadump ADDRREG DATAREG\n"); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | int main(int argc, char *argv[]) |
|---|
| 51 | { |
|---|
| 52 | int addrreg, datareg; |
|---|
| 53 | char *end; |
|---|
| 54 | int i,j,res; |
|---|
| 55 | |
|---|
| 56 | if (argc < 2) { |
|---|
| 57 | fprintf(stderr,"Error: No address register specified!\n"); |
|---|
| 58 | help(); |
|---|
| 59 | exit(1); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | addrreg = strtol(argv[1],&end,0); |
|---|
| 63 | if (*end) { |
|---|
| 64 | fprintf(stderr,"Error: First argument not a number!\n"); |
|---|
| 65 | help(); |
|---|
| 66 | exit(1); |
|---|
| 67 | } |
|---|
| 68 | if ((addrreg < 0) || (addrreg > 0xfff)) { |
|---|
| 69 | fprintf(stderr,"Error: Address register out of range!\n"); |
|---|
| 70 | help(); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | if (argc < 3) { |
|---|
| 74 | fprintf(stderr,"Error: No data register specified!\n"); |
|---|
| 75 | help(); |
|---|
| 76 | exit(1); |
|---|
| 77 | } |
|---|
| 78 | datareg = strtol(argv[2],&end,0); |
|---|
| 79 | if (*end) { |
|---|
| 80 | fprintf(stderr,"Error: Second argument not a number!\n"); |
|---|
| 81 | help(); |
|---|
| 82 | exit(1); |
|---|
| 83 | } |
|---|
| 84 | if ((datareg < 0) || (datareg > 0xfff)) { |
|---|
| 85 | fprintf(stderr,"Error: Data register out of range!\n"); |
|---|
| 86 | help(); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | if (getuid()) { |
|---|
| 90 | fprintf(stderr,"Error: Can only be run as root (or make it suid root)\n"); |
|---|
| 91 | exit(1); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | fprintf(stderr," WARNING! Running this program can cause system crashes, " |
|---|
| 95 | "data loss and worse!\n"); |
|---|
| 96 | fprintf(stderr," I will probe address register 0x%04x and " |
|---|
| 97 | "data register 0x%04x.\n",addrreg,datareg); |
|---|
| 98 | fprintf(stderr," You have five seconds to reconsider and press CTRL-C!\n\n"); |
|---|
| 99 | sleep(5); |
|---|
| 100 | |
|---|
| 101 | #ifndef __powerpc__ |
|---|
| 102 | if ((datareg < 0x400) && (datareg < 0x400)) { |
|---|
| 103 | if(ioperm(datareg,1,1)) { |
|---|
| 104 | fprintf(stderr,"Error: Could not ioperm() data register!\n"); |
|---|
| 105 | exit(1); |
|---|
| 106 | } |
|---|
| 107 | if(ioperm(addrreg,1,1)) { |
|---|
| 108 | fprintf(stderr,"Error: Could not ioperm() address register!\n"); |
|---|
| 109 | exit(1); |
|---|
| 110 | } |
|---|
| 111 | } else { |
|---|
| 112 | if(iopl(3)) { |
|---|
| 113 | fprintf(stderr,"Error: Could not do iopl(3)!\n"); |
|---|
| 114 | exit(1); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | #endif |
|---|
| 118 | |
|---|
| 119 | printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); |
|---|
| 120 | for (i = 0; i < 256; i+=16) { |
|---|
| 121 | printf("%c0: ",hexchar(i/16)); |
|---|
| 122 | for(j = 0; j < 16; j++) { |
|---|
| 123 | outb(i+j,addrreg); |
|---|
| 124 | res = inb(datareg); |
|---|
| 125 | printf("%c%c ",hexchar(res/16),hexchar(res%16)); |
|---|
| 126 | } |
|---|
| 127 | printf("\n"); |
|---|
| 128 | } |
|---|
| 129 | exit(0); |
|---|
| 130 | } |
|---|