root/lm-sensors/trunk/prog/dump/isadump.c @ 798

Revision 798, 4.4 KB (checked in by mds, 13 years ago)

(mds) added Winbond-style bank selection.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    isadump.c - Part of 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
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <asm/io.h>
25
26
27/* To keep glibc2 happy */
28#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 0
29#include <sys/perm.h>
30#endif
31
32#ifdef __powerpc__
33unsigned long isa_io_base = 0; /* XXX for now */
34#endif /* __powerpc__ */
35
36char hexchar(int i)
37{
38  if ((i >= 0) && (i <= 9))
39    return '0' + i;
40  else if (i <= 15)
41    return 'a' - 10 + i;
42  else
43    return 'X';
44}
45
46void help(void)
47{
48  fprintf(stderr,"Syntax: isadump ADDRREG DATAREG [BANK [BANKREG]]\n");
49}
50
51int main(int argc, char *argv[])
52{
53  int addrreg, datareg, bank = 0, bankreg = 0x4E;
54  char *end;
55  int i,j,res;
56
57  if (argc < 2) {
58    fprintf(stderr,"Error: No registers specified!\n");
59    help();
60    exit(1);
61  }
62
63  addrreg = strtol(argv[1],&end,0);
64  if (*end) {
65    fprintf(stderr,"Error: First argument not a number!\n");
66    help();
67    exit(1);
68  }
69  if ((addrreg < 0) || (addrreg > 0xfff)) {
70    fprintf(stderr,"Error: Address register out of range!\n");
71    help();
72    exit(1);
73  }
74
75  if (argc < 3) {
76    fprintf(stderr,"Error: No data register specified!\n");
77    help();
78    exit(1);
79  }
80
81  datareg = strtol(argv[2],&end,0);
82  if (*end) {
83    fprintf(stderr,"Error: Second argument not a number!\n");
84    help();
85    exit(1);
86  }
87  if ((datareg < 0) || (datareg > 0xfff)) {
88    fprintf(stderr,"Error: Data register out of range!\n");
89    help();
90    exit(1);
91  }
92
93  if(argc > 3) {
94    bank = strtol(argv[3],&end,0);
95    if (*end) {
96      fprintf(stderr,"Error: Invalid bank number!\n");
97      help();
98      exit(1);
99    }
100    if ((bank < 0) || (bank > 15)) {
101      fprintf(stderr,"Error: bank out of range (0-15)!\n");
102      help();
103      exit(1);
104    }
105
106    if(argc > 4) {
107      bankreg = strtol(argv[4],&end,0);
108      if (*end) {
109        fprintf(stderr,"Error: Invalid bank register number!\n");
110        help();
111        exit(1);
112      }
113      if ((bankreg < 0) || (bankreg > 0xff)) {
114        fprintf(stderr,"Error: bank out of range (0-0xff)!\n");
115        help();
116        exit(1);
117      }
118    }
119  }
120
121  if (getuid()) {
122    fprintf(stderr,"Error: Can only be run as root (or make it suid root)\n");
123    exit(1);
124  }
125
126  fprintf(stderr,"  WARNING! Running this program can cause system crashes, "
127          "data loss and worse!\n");
128  fprintf(stderr,"  I will probe address register 0x%04x and "
129                 "data register 0x%04x.\n",addrreg,datareg);
130  if(bank)     
131    fprintf(stderr,"  Probing bank %d using bank register 0x%02x.\n",
132            bank, bankreg);
133  fprintf(stderr,"  You have five seconds to reconsider and press CTRL-C!\n\n");
134  sleep(5);
135
136#ifndef __powerpc__
137  if ((datareg < 0x400) && (addrreg < 0x400)) {
138    if(ioperm(datareg,1,1)) {
139      fprintf(stderr,"Error: Could not ioperm() data register!\n");
140      exit(1);
141    }
142    if(ioperm(addrreg,1,1)) {
143      fprintf(stderr,"Error: Could not ioperm() address register!\n");
144      exit(1);
145    }
146  } else {
147    if(iopl(3)) {
148      fprintf(stderr,"Error: Could not do iopl(3)!\n");
149      exit(1);
150    }
151  }
152#endif
153
154  /* See Winbond w83781d data sheet for bank details */
155  if(bank) {
156    outb(bankreg,addrreg);
157    outb(bank | 0x80,datareg); /* OR in high byte flag */
158  }
159
160  printf("     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f\n");
161  for (i = 0; i < 256; i+=16) {
162    printf("%c0: ",hexchar(i/16));
163    for(j = 0; j < 16; j++) {
164      outb(i+j,addrreg);
165      res = inb(datareg);
166      printf("%c%c ",hexchar(res/16),hexchar(res%16));
167    }
168    printf("\n");
169  }
170  if(bank) {
171    outb(bankreg,addrreg);
172    outb(0x80,datareg); /* put back in bank 0 high byte */
173  }
174  exit(0);
175}
Note: See TracBrowser for help on using the browser.