-
Notifications
You must be signed in to change notification settings - Fork 2
/
io.c
86 lines (70 loc) · 1.99 KB
/
io.c
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
/*
* io.c
* ver.0.1: Dec 18, 2011 S.Ishihara
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/io.h>
int
main(int argc, char *argv[])
{
int opt;
extern char *optarg;
extern int optind, opterr;
int width = 1; /* default byte access */
unsigned short ioaddr;
unsigned int wdata;
while ((opt = getopt(argc, argv, "w:")) != -1) {
if (opt == 'w') {
width = atoi(optarg);
} else {
goto error;
}
}
argc -= optind;
argv += optind;
iopl(3);
if (argc == 1) {
/* Read I/O */
ioaddr = strtoul(argv[0], NULL, 16);
if (width == 1) {
printf("0x%04x: 0x%02x\n", ioaddr, inb(ioaddr));
} else if (width == 2) {
printf("0x%04x: 0x%04x\n", ioaddr, inw(ioaddr));
} else if (width == 4) {
printf("0x%04x: 0x%08x\n", ioaddr, inl(ioaddr));
} else {
goto error;
}
} else if (argc == 2) {
/* Write I/O */
ioaddr = strtoul(argv[0], NULL, 16);
wdata = strtoul(argv[1], NULL, 16);
if (width == 1) {
outb(wdata, ioaddr);
} else if(width == 2) {
outw(wdata, ioaddr);
} else if(width == 4) {
outl(wdata, ioaddr);
} else {
goto error;
}
} else {
goto error;
}
return 0;
error:
printf("Usage: io [-w WIDTH] ADDRESS [DATA]\n"
"I/O read or write.\n"
" -w number of byte width. permit 1(default), 2, 4\n"
"\n"
"This command executable only root user.\n"
"I/O address possible range 16bit.\n"
"\n"
"Examples:\n"
" io 3f8 Read I/O from address 0x3f8.\n"
" io 3f8 0x31 Write I/O address 0x3f8 to 0x31.\n"
" io -w4 cf8 80000000 Write I/O address 0xcf8 to 0x80000000 double word.\n"
"\n");
return 1;
}