-
Notifications
You must be signed in to change notification settings - Fork 1
/
hw.c
274 lines (251 loc) · 6.23 KB
/
hw.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
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
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include "pci.h"
#ifdef __WATCOMC__
#define asm _asm
#endif
void my_outpd(unsigned port, unsigned long value)
{
asm {
mov dx, [port]
db 66h
mov ax, [WORD PTR value]
db 66h
out dx,ax
}
}
unsigned long my_inpd(unsigned port)
{
asm {
mov dx, [port]
db 66h
in ax,dx
db 66h
mov dx,ax
mov cl,10h
db 66h
shr dx,cl
}
}
typedef struct {
void (*writeb)(unsigned char value);
void (*writew)(unsigned int value);
void (*writed)(unsigned long value);
unsigned (*readb)();
unsigned (*readw)();
unsigned long (*readd)();
int (*parse_address)(const char* addr);
} space_t;
static unsigned parsed_io_address;
int io_parse_address(const char* addr)
{
char dummy;
unsigned bus, dev, fn, bar;
unsigned long offset;
unsigned vendor;
size_t addrlen = strlen(addr);
if (addrlen > 10 && addrlen <= 12 &&
addr[2] == ':' && addr[5] == '.' && addr[7] == '$' && addr[9] == '+' &&
sscanf(addr, "%x:%x.%u$%u+%lx%c", &bus, &dev, &fn, &bar, &offset, &dummy) == 5)
{
dev_addr addr;
unsigned long bar_val;
if (pci_init() < 0)
{
fputs("No PCI BIOS found\n", stderr);
return -1;
}
if (bus > last_bus)
{
fputs("Bad bus number\n", stderr);
return -1;
}
if (dev > 31)
{
fputs("Invalid device number\n", stderr);
return -1;
}
if (fn > 7)
{
fputs("Invalid function number\n", stderr);
return -1;
}
if (bar > 5)
{
fputs("Invalid BAR number\n", stderr);
return -1;
}
if (offset >= 0x100)
{
fputs("Invalid I/O offset (PCI I/O areas are 256 bytes maximum)\n", stderr);
return -1;
}
addr = ADDR(bus, dev, fn);
if (pci_read_word(addr, 0, &vendor) < 0 || vendor == 0xFFFF)
{
fputs("specified PCI device does not exist\n", stderr);
return -1;
}
if (pci_read_dword(addr, 0x10 + 4*bar, &bar_val) < 0 || bar_val == 0x00000000)
{
fputs("specified base address register does not exist\n", stderr);
return -1;
}
if (!(bar_val & 1))
{
fputs("specified base address register describes a memory mapped region\n", stderr);
return -1;
}
parsed_io_address = (bar_val & ~1) + offset;
return 0;
}
else if (addrlen <= 4 && sscanf(addr, "%x%c", &parsed_io_address, &dummy) == 1)
{
return 0;
}
return -1;
}
void io_writeb(unsigned char value)
{
outp(parsed_io_address, value);
}
void io_writew(unsigned int value)
{
outpw(parsed_io_address, value);
}
void io_writed(unsigned long value)
{
my_outpd(parsed_io_address, value);
}
unsigned io_readb()
{
return inp(parsed_io_address);
}
unsigned io_readw()
{
return inpw(parsed_io_address);
}
unsigned long io_readd()
{
return my_inpd(parsed_io_address);
}
static const space_t iospace = {
io_writeb, io_writew, io_writed,
io_readb, io_readw, io_readd,
io_parse_address
};
int main(int argc, char** argv)
{
char dummy;
char sizechar;
unsigned long value;
enum { MODE_POKE, MODE_PEEK } mode;
const space_t* space;
if (argc < 2)
{
fputs("missing verb\n", stderr);
return 1;
}
if (strncmp(argv[1], "out", 3) == 0)
{
sizechar = argv[1][3];
space = &iospace;
mode = MODE_POKE;
}
else if (strncmp(argv[1], "in", 2) == 0)
{
sizechar = argv[1][2];
space = &iospace;
mode = MODE_PEEK;
}
/* else if (strncmp(argv[1], "poke", 4) == 0)
{
sizechar = argv[1][4];
space = memspace;
mode = MODE_POKE;
}*/
else
{
fputs("Bad verb\n", stderr);
return 1;
}
if (argc < 3)
{
fputs("missing address\n", stderr);
return 1;
}
if (space->parse_address(argv[2]) < 0)
{
fputs("Bad address\n", stderr);
return 1;
}
if (mode == MODE_POKE)
{
if (argc < 4)
{
fputs("missing data to write\n", stderr);
return 1;
}
switch(sizechar)
{
case 'b':
if (strlen(argv[3]) == 2 && sscanf(argv[3], "%lx%x", &value, &dummy) == 1)
{
space->writeb(value & 0xFF);
return 0;
}
else
{
fputs("Byte to write must be 2 hex digits\n", stderr);
return 1;
}
case 'w':
if (strlen(argv[3]) == 4 && sscanf(argv[3], "%lx%x", &value, &dummy) == 1)
{
space->writew(value & 0xFFFF);
return 0;
}
else
{
fputs("Word to write must be 4 hex digits\n", stderr);
return 1;
}
case 'd':
case 'l':
if (strlen(argv[3]) == 8 && sscanf(argv[3], "%lx%x", &value, &dummy) == 1)
{
space->writed(value);
return 0;
}
else
{
fputs("Dword to write must be 8 hex digits\n", stderr);
return 1;
}
default:
fputs("Bad size character\n", stderr);
return 1;
}
}
else if (mode == MODE_PEEK)
{
switch(sizechar)
{
case 'b':
printf("%02x\n", space->readb());
break;
case 'w':
printf("%04x\n", space->readw());
break;
case 'd':
case 'l':
printf("%08lx\n", space->readd());
break;
default:
fputs("Bad size character\n", stderr);
return 1;
}
}
return 0;
}