summaryrefslogtreecommitdiff
path: root/prog/dump/isadump.c
blob: e0e6f00078a55b0617c7b02943635e29fbb6ec2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
    isadump.c - isadump, a user-space program to dump ISA registers
    Copyright (C) 2000  Frodo Looijaard <frodol@dds.nl>, and
                        Mark D. Studebaker <mdsxyz123@yahoo.com>
    Copyright (C) 2004-2011  Jean Delvare <jdelvare@suse.de>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301 USA.
*/

/*
	Typical usage:
	isadump 0x295 0x296		Basic winbond dump using address/data registers
	isadump 0x295 0x296 2		Winbond dump, bank 2
	isadump 0x2e 0x2f 0x09		Super-I/O, logical device 9
	isadump -f 0x5000		Flat address space dump like for Via 686a
	isadump -f 0xecf0 0x10 1	PC87366, temperature channel 2
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "util.h"
#include "superio.h"


/* To keep glibc2 happy */
#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 0
#include <sys/io.h>
#else
#include <asm/io.h>
#endif

#ifdef __powerpc__
unsigned long isa_io_base = 0; /* XXX for now */
#endif /* __powerpc__ */

static void help(void)
{
	fprintf(stderr,
	        "Syntax for I2C-like access:\n"
	        "  isadump [OPTIONS] [-k V1,V2...] ADDRREG DATAREG [BANK [BANKREG]]\n"
	        "Syntax for flat address space:\n"
	        "  isadump -f [OPTIONS] ADDRESS [RANGE [BANK [BANKREG]]]\n"
		"Options:\n"
		"  -k	Super-I/O configuration access key\n"
		"  -f	Enable flat address space mode\n"
		"  -y	Assume affirmative answer to all questions\n"
		"  -W	Read and display word (16-bit) values\n"
		"  -L	Read and display long (32-bit) values\n");
}

static int default_bankreg(int flat, int addrreg, int datareg)
{
	if (flat) {
		return 0x09; /* Works for National Semiconductor
		                Super-IO chips */
	}

	if ((addrreg == 0x2e && datareg == 0x2f)
	 || (addrreg == 0x4e && datareg == 0x4f)) {
		return 0x07; /* Works for all Super-I/O chips */
	}
	
	return 0x4e; /* Works for Winbond ISA chips, default */
}

static int set_bank(int flat, int addrreg, int datareg, int bank, int bankreg)
{
	int oldbank;

	if (flat) {
		oldbank = inb(addrreg+bankreg);
		outb(bank, addrreg+bankreg);
	} else {
		outb(bankreg, addrreg);
		oldbank = inb(datareg);
		outb(bank, datareg);
	}

	return oldbank;
}

int main(int argc, char *argv[])
{
	int addrreg;        /* address in flat mode */
	int datareg = 0;    /* unused in flat mode */
	int range = 256;    /* can be changed only in flat mode */
	int bank = -1;      /* -1 means no bank operation */
	int bankreg;
	int oldbank = 0;
	int i, j;
	unsigned long res;
	int flags = 0;
	int flat = 0, yes = 0, width = 1;
	char *end;
	unsigned char enter_key[SUPERIO_MAX_KEY+1];

	enter_key[0] = 0;

	/* handle (optional) flags first */
	while (1+flags < argc && argv[1+flags][0] == '-') {
		switch (argv[1+flags][1]) {
		case 'f': flat = 1; break;
		case 'y': yes = 1; break;
		case 'k':
			if (2+flags >= argc
			 || superio_parse_key(enter_key, argv[2+flags]) < 0) {
				fprintf(stderr, "Invalid or missing key\n");
				help();
				exit(1);
			}
			flags++;
			break;
		case 'W': width = 2; break;
		case 'L': width = 4; break;
		default:
			fprintf(stderr, "Warning: Unsupported flag "
				"\"-%c\"!\n", argv[1+flags][1]);
			help();
			exit(1);
		}
		flags++;
	}

	/* key is never needed in flat mode */
	if (flat && enter_key[0]) {
		fprintf(stderr, "Error: Cannot use key in flat mode\n");
		exit(1);
	}

	/* verify that the argument count is correct */
	if ((!flat && argc < 1+flags+2)
	 || (flat && argc < 1+flags+1)) {
		help();
		exit(1);
	}

	addrreg = strtol(argv[1+flags], &end, 0);
	if (*end) {
		fprintf(stderr, "Error: Invalid address!\n");
		help();
		exit(1);
	}
	if (addrreg < 0 || addrreg > (flat?0xffff:0x3fff)) {
		fprintf(stderr, "Error: Address out of range "
		        "(0x0000-0x%04x)!\n", flat?0xffff:0x3fff);
		help();
		exit(1);
	}

	if (flat) {
		if (1+flags+1 < argc) {
			range = strtol(argv[1+flags+1], &end, 0);
			if (*end || range <= 0 || range > 0x100
			 || range & 0xf) {
				fprintf(stderr, "Error: Invalid range!\n"
				        "Hint: Must be a multiple of 16 no "
				        "greater than 256.\n");
				help();
				exit(1);
			}
		} else {
			addrreg &= 0xff00; /* Force alignment */
		}
	} else {
		datareg = strtol(argv[1+flags+1], &end, 0);
		if (*end) {
			fprintf(stderr, "Error: Invalid data register!\n");
			help();
			exit(1);
		}
		if (datareg < 0 || datareg > 0x3fff) {
			fprintf(stderr, "Error: Data register out of range "
			        "(0x0000-0x3fff)!\n");
			help();
			exit(1);
		}
	}

	bankreg = default_bankreg(flat, addrreg, datareg);

	if (1+flags+2 < argc) {
		bank = strtol(argv[1+flags+2], &end, 0);
		if (*end) {
			fprintf(stderr, "Error: Invalid bank number!\n");
			help();
			exit(1);
		}
		if ((bank < 0) || (bank > 31)) {
			fprintf(stderr, "Error: bank out of range (0-31)!\n");
			help();
			exit(1);
		}

		if (1+flags+3 < argc) {
			bankreg = strtol(argv[1+flags+3], &end, 0);
			if (*end) {
				fprintf(stderr, "Error: Invalid bank "
				        "register!\n");
				help();
				exit(1);
			}
			if (bankreg < 0 || bankreg >= range) {
				fprintf(stderr, "Error: bank out of range "
				        "(0x00-0x%02x)!\n", range-1);
				help();
				exit(1);
			}
		}
	}

	if (geteuid()) {
		fprintf(stderr, "Error: Can only be run as root (or make it "
		        "suid root)\n");
		exit(1);
	}

	if (!yes) {
		fprintf(stderr, "WARNING! Running this program can cause "
		        "system crashes, data loss and worse!\n");

		if (flat)
			fprintf(stderr, "I will probe address range 0x%x to "
			        "0x%x.\n", addrreg, addrreg + range - 1);
		else
			fprintf(stderr, "I will probe address register 0x%x "
			        "and data register 0x%x.\n", addrreg, datareg);

		if (bank>=0) 	
			fprintf(stderr, "Probing bank %d using bank register "
			        "0x%02x.\n", bank, bankreg);

		fprintf(stderr, "Continue? [Y/n] ");
		fflush(stderr);
		if (!user_ack(1)) {
			fprintf(stderr, "Aborting on user request.\n");
			exit(0);
		}
	}

#ifndef __powerpc__
	if ((datareg < 0x400) && (addrreg < 0x400) && !flat) {
		if (ioperm(datareg, 1, 1)) {
			fprintf(stderr, "Error: Could not ioperm() data "
			        "register!\n");
			exit(1);
		}
		if (ioperm(addrreg, 1, 1)) {
			fprintf(stderr, "Error: Could not ioperm() address "
			        "register!\n");
			exit(1);
		}
	} else {
		if (iopl(3)) {
			fprintf(stderr, "Error: Could not do iopl(3)!\n");
			exit(1);
		}
	}
#endif

	/* Enter Super-I/O configuration mode */
	if (enter_key[0])
		superio_write_key(addrreg, enter_key);

	if (bank >= 0)
		oldbank = set_bank(flat, addrreg, datareg, bank, bankreg);

	/* print column headers */
	printf("%*s", flat ? 5 : 3, "");
	for (j = 0; j < 16; j += width)
		printf(" %*x", width * 2, j);
	printf("\n");

	for (i = 0; i < range; i += 16) {
		if (flat)
			printf("%04x: ", addrreg + i);
		else
			printf("%02x: ", i);

		/* It was noticed that Winbond Super-I/O chips
		   would leave the configuration mode after
		   an arbitrary number of register reads,
		   causing any subsequent read attempt to
		   silently fail. Repeating the key every 16 reads
		   prevents that. */
		if (enter_key[0])
			superio_write_key(addrreg, enter_key);

		for (j = 0; j < 16; j += width) {
			fflush(stdout);
			if (flat) {
				res = inx(addrreg + i + j, width);
			} else {	
				outb(i+j, addrreg);
				if (i+j == 0 && inb(addrreg) == 0x80) {
					/* Bit 7 appears to be a busy flag */
					range = 128;
				}
				res = inx(datareg, width);
			}
			printf("%0*lx ", width * 2, res);
		}
		printf("\n");
	}

	/* Restore the original bank value */
	if (bank >= 0)
		set_bank(flat, addrreg, datareg, oldbank, bankreg);

	/* Exit Super-I/O configuration mode */
	if (enter_key[0])
		superio_reset(addrreg, datareg);

	exit(0);
}