]>
Commit | Line | Data |
---|---|---|
1 | #if defined(__GNUC__) && !defined(__STRICT_ANSI__) | |
2 | ||
3 | #define _GNU_SOURCE 1 | |
4 | ||
5 | #if defined(RTLD_NEXT) | |
6 | #define REAL_LIBC RTLD_NEXT | |
7 | #else | |
8 | #define REAL_LIBC ((void *) -1L) | |
9 | #endif | |
10 | ||
11 | #include <dlfcn.h> | |
12 | #include <stdarg.h> | |
13 | #include <stdlib.h> | |
14 | #include <string.h> | |
15 | #include <unistd.h> | |
16 | #include <fcntl.h> | |
17 | #include <sys/types.h> | |
18 | #include <sys/stat.h> | |
19 | #include <sys/time.h> | |
20 | #include <stdio.h> | |
21 | #include "xilinx.h" | |
22 | ||
23 | static int (*ioctl_func) (int, int, void *) = NULL; | |
24 | static int windrvrfd = 0; | |
25 | ||
26 | void hexdump(unsigned char *buf, int len); | |
27 | void diff(unsigned char *buf1, unsigned char *buf2, int len); | |
28 | ||
29 | int do_wdioctl(int fd, unsigned int request, unsigned char *wdioctl) { | |
30 | struct header_struct* wdheader = (struct header_struct*)wdioctl; | |
31 | struct version_struct *version; | |
32 | int ret = 0; | |
33 | ||
34 | if (wdheader->magic != MAGIC) { | |
35 | fprintf(stderr,"!!!ERROR: magic header does not match!!!\n"); | |
36 | return (*ioctl_func) (fd, request, wdioctl); | |
37 | } | |
38 | ||
39 | switch(request) { | |
40 | case VERSION: | |
41 | version = (struct version_struct*)(wdheader->data); | |
42 | strcpy(version->version, "WinDriver no more"); | |
43 | version->versionul = 999; | |
44 | fprintf(stderr,"faking VERSION\n"); | |
45 | break; | |
46 | ||
47 | case LICENSE: | |
48 | fprintf(stderr,"faking LICENSE\n"); | |
49 | break; | |
50 | ||
51 | case CARD_REGISTER: | |
52 | { | |
53 | //struct card_register* cr = (struct card_register*)(wdheader->data); | |
54 | /* Todo: LPT-Port already in use */ | |
55 | } | |
56 | fprintf(stderr,"faking CARD_REGISTER\n"); | |
57 | break; | |
58 | ||
59 | case USB_TRANSFER: | |
60 | fprintf(stderr,"in USB_TRANSFER"); | |
61 | { | |
62 | struct usb_transfer *ut = (struct usb_transfer*)(wdheader->data); | |
63 | ||
64 | fprintf(stderr," unique: %lu, pipe: %lu, read: %lu, options: %lx, size: %lu, timeout: %lx\n", ut->dwUniqueID, ut->dwPipeNum, ut->fRead, ut->dwOptions, ut->dwBufferSize, ut->dwTimeout); | |
65 | fprintf(stderr,"setup packet: "); | |
66 | hexdump(ut->SetupPacket, 8); | |
67 | fprintf(stderr,"\n"); | |
68 | if (!ut->fRead && ut->dwBufferSize) | |
69 | { | |
70 | hexdump(ut->pBuffer, ut->dwBufferSize); | |
71 | fprintf(stderr,"\n"); | |
72 | } | |
73 | ||
74 | ret = (*ioctl_func) (fd, request, wdioctl); | |
75 | ||
76 | fprintf(stderr,"Transferred: %lu (%s)\n",ut->dwBytesTransferred, (ut->fRead?"read":"write")); | |
77 | if (ut->fRead && ut->dwBytesTransferred) | |
78 | { | |
79 | fprintf(stderr,"Read: "); | |
80 | hexdump(ut->pBuffer, ut->dwBytesTransferred); | |
81 | } | |
82 | fprintf(stderr,"\n"); | |
83 | } | |
84 | break; | |
85 | ||
86 | case INT_ENABLE: | |
87 | fprintf(stderr,"faking INT_ENABLE"); | |
88 | { | |
89 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
90 | ||
91 | fprintf(stderr," Handle: %lu, Options: %lx, ncmds: %lu\n", it->hInterrupt, it->dwOptions, it->dwCmds); | |
92 | ||
93 | it->fEnableOk = 1; | |
94 | //ret = (*ioctl_func) (fd, request, wdioctl); | |
95 | } | |
96 | ||
97 | break; | |
98 | ||
99 | case INT_DISABLE: | |
100 | fprintf(stderr,"INT_DISABLE\n"); | |
101 | { | |
102 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
103 | ||
104 | fprintf(stderr," Handle: %lu, Options: %lx, ncmds: %lu\n", it->hInterrupt, it->dwOptions, it->dwCmds); | |
105 | ||
106 | hexdump(wdheader->data, wdheader->size); | |
107 | //it->dwCounter = 0; | |
108 | //it->fStopped = 1; | |
109 | ret = (*ioctl_func) (fd, request, wdioctl); | |
110 | fprintf(stderr,"\n\n"); | |
111 | hexdump(wdheader->data, wdheader->size); | |
112 | fprintf(stderr,"\n"); | |
113 | } | |
114 | break; | |
115 | ||
116 | case USB_SET_INTERFACE: | |
117 | fprintf(stderr,"USB_SET_INTERFACE\n"); | |
118 | { | |
119 | struct usb_set_interface *usi = (struct usb_set_interface*)(wdheader->data); | |
120 | ||
121 | fprintf(stderr,"unique: %lu, interfacenum: %lu, alternatesetting: %lu, options: %lx\n", usi->dwUniqueID, usi->dwInterfaceNum, usi->dwAlternateSetting, usi->dwOptions); | |
122 | ret = (*ioctl_func) (fd, request, wdioctl); | |
123 | } | |
124 | break; | |
125 | ||
126 | case USB_GET_DEVICE_DATA: | |
127 | fprintf(stderr,"USB_GET_DEVICE_DATA\n"); | |
128 | { | |
129 | struct usb_get_device_data *ugdd = (struct usb_get_device_data*)(wdheader->data); | |
130 | int pSize; | |
131 | ||
132 | fprintf(stderr, "uniqe: %lu, bytes: %lu, options: %lx\n", ugdd->dwUniqueID, ugdd->dwBytes, ugdd->dwOptions); | |
133 | pSize = ugdd->dwBytes; | |
134 | ret = (*ioctl_func) (fd, request, wdioctl); | |
135 | if (pSize) { | |
136 | hexdump(ugdd->pBuf, pSize); | |
137 | fprintf(stderr, "\n"); | |
138 | } | |
139 | } | |
140 | break; | |
141 | ||
142 | case EVENT_REGISTER: | |
143 | fprintf(stderr,"EVENT_REGISTER\n"); | |
144 | { | |
145 | struct event *e = (struct event*)(wdheader->data); | |
146 | int i; | |
147 | ||
148 | fprintf(stderr,"handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", e->handle, e->dwAction, e->dwStatus, e->dwEventId, e->dwCardType, e->hKernelPlugIn, e->dwOptions, e->u.Usb.deviceId.dwVendorId, e->u.Usb.deviceId.dwProductId, e->u.Usb.dwUniqueID, e->dwEventVer, e->dwNumMatchTables); | |
149 | for (i = 0; i < e->dwNumMatchTables; i++) | |
150 | fprintf(stderr,"match: dev: %x:%x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", e->matchTables[i].VendorId, e->matchTables[i].ProductId, e->matchTables[i].bDeviceClass, e->matchTables[i].bDeviceSubClass, e->matchTables[i].bInterfaceClass, e->matchTables[i].bInterfaceSubClass, e->matchTables[i].bInterfaceProtocol); | |
151 | ||
152 | ret = (*ioctl_func) (fd, request, wdioctl); | |
153 | ||
154 | fprintf(stderr,"handle: %lu, action: %lu, status: %lu, eventid: %lu, cardtype: %lu, kplug: %lu, options: %lu, dev: %lx:%lx, unique: %lu, ver: %lu, nummatch: %lu\n", e->handle, e->dwAction, e->dwStatus, e->dwEventId, e->dwCardType, e->hKernelPlugIn, e->dwOptions, e->u.Usb.deviceId.dwVendorId, e->u.Usb.deviceId.dwProductId, e->u.Usb.dwUniqueID, e->dwEventVer, e->dwNumMatchTables); | |
155 | for (i = 0; i < e->dwNumMatchTables; i++) | |
156 | fprintf(stderr,"match: dev: %x:%x, class: %x, subclass: %x, intclass: %x, intsubclass: %x, intproto: %x\n", e->matchTables[i].VendorId, e->matchTables[i].ProductId, e->matchTables[i].bDeviceClass, e->matchTables[i].bDeviceSubClass, e->matchTables[i].bInterfaceClass, e->matchTables[i].bInterfaceSubClass, e->matchTables[i].bInterfaceProtocol); | |
157 | } | |
158 | break; | |
159 | ||
160 | case TRANSFER: | |
161 | fprintf(stderr,"TRANSFER\n"); | |
162 | ret = (*ioctl_func) (fd, request, wdioctl); | |
163 | break; | |
164 | ||
165 | case EVENT_UNREGISTER: | |
166 | fprintf(stderr,"EVENT_UNREGISTER\n"); | |
167 | ret = (*ioctl_func) (fd, request, wdioctl); | |
168 | break; | |
169 | ||
170 | case INT_WAIT: | |
171 | fprintf(stderr,"INT_WAIT\n"); | |
172 | { | |
173 | struct interrupt *it = (struct interrupt*)(wdheader->data); | |
174 | ||
175 | fprintf(stderr," Handle: %lu, Options: %lx, ncmds: %lu\n", it->hInterrupt, it->dwOptions, it->dwCmds); | |
176 | ||
177 | hexdump(wdheader->data, wdheader->size); | |
178 | ret = (*ioctl_func) (fd, request, wdioctl); | |
179 | //ret = 0; | |
180 | //it->dwCounter++; | |
181 | fprintf(stderr,"\n\n"); | |
182 | hexdump(wdheader->data, wdheader->size); | |
183 | fprintf(stderr,"\n"); | |
184 | } | |
185 | break; | |
186 | ||
187 | case CARD_UNREGISTER: | |
188 | fprintf(stderr,"CARD_UNREGISTER\n"); | |
189 | ret = (*ioctl_func) (fd, request, wdioctl); | |
190 | break; | |
191 | ||
192 | case EVENT_PULL: | |
193 | fprintf(stderr,"EVENT_PULL\n"); | |
194 | ret = (*ioctl_func) (fd, request, wdioctl); | |
195 | break; | |
196 | ||
197 | default: | |
198 | ret = (*ioctl_func) (fd, request, wdioctl); | |
199 | } | |
200 | ||
201 | return ret; | |
202 | } | |
203 | ||
204 | ||
205 | typedef int (*open_funcptr_t) (const char *, int, mode_t); | |
206 | ||
207 | int open (const char *pathname, int flags, ...) | |
208 | { | |
209 | static open_funcptr_t func = NULL; | |
210 | mode_t mode = 0; | |
211 | va_list args; | |
212 | int fd; | |
213 | ||
214 | if (!func) | |
215 | func = (open_funcptr_t) dlsym (REAL_LIBC, "open"); | |
216 | ||
217 | if (flags & O_CREAT) { | |
218 | va_start(args, flags); | |
219 | mode = va_arg(args, mode_t); | |
220 | va_end(args); | |
221 | } | |
222 | ||
223 | fd = (*func) (pathname, flags, mode); | |
224 | ||
225 | if (!strcmp (pathname, "/dev/windrvr6")) { | |
226 | fprintf(stderr,"opening windrvr6\n"); | |
227 | windrvrfd = fd; | |
228 | } | |
229 | ||
230 | return fd; | |
231 | } | |
232 | ||
233 | void diff(unsigned char *buf1, unsigned char *buf2, int len) { | |
234 | int i; | |
235 | ||
236 | for(i=0; i<len; i++) { | |
237 | if (buf1[i] != buf2[i]) { | |
238 | fprintf(stderr,"Diff at %d: %02x(%c)->%02x(%c)\n", i, buf1[i], ((buf1[i] >= 31 && buf1[i] <= 126)?buf1[i]:'.'), buf2[i], ((buf2[i] >= 31 && buf2[i] <= 126)?buf2[i]:'.')); | |
239 | } | |
240 | } | |
241 | } | |
242 | ||
243 | void hexdump(unsigned char *buf, int len) { | |
244 | int i; | |
245 | ||
246 | for(i=0; i<len; i++) { | |
247 | fprintf(stderr,"%02x ", buf[i]); | |
248 | if ((i % 16) == 15) | |
249 | fprintf(stderr,"\n"); | |
250 | } | |
251 | } | |
252 | ||
253 | int ioctl(int fd, int request, ...) | |
254 | { | |
255 | va_list args; | |
256 | void *argp; | |
257 | int ret; | |
258 | ||
259 | if (!ioctl_func) | |
260 | ioctl_func = (int (*) (int, int, void *)) dlsym (REAL_LIBC, "ioctl"); | |
261 | ||
262 | va_start (args, request); | |
263 | argp = va_arg (args, void *); | |
264 | va_end (args); | |
265 | ||
266 | if (fd == windrvrfd) | |
267 | ret = do_wdioctl(fd, request, argp); | |
268 | else | |
269 | ret = (*ioctl_func) (fd, request, argp); | |
270 | ||
271 | return ret; | |
272 | } | |
273 | ||
274 | #if 0 | |
275 | void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset) | |
276 | { | |
277 | static void* (*func) (void *, size_t, int, int, int, off_t) = NULL; | |
278 | void *ret; | |
279 | ||
280 | if (!func) | |
281 | func = (void* (*) (void *, size_t, int, int, int, off_t)) dlsym (REAL_LIBC, "mmap"); | |
282 | ||
283 | ret = (*func) (start, length, prot, flags, fd, offset); | |
284 | fprintf(stderr,"MMAP: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start, length, prot, flags, fd, offset, (unsigned int)ret); | |
285 | mmapped = ret; | |
286 | mmapplen = length; | |
287 | ||
288 | return ret; | |
289 | } | |
290 | ||
291 | void *mmap64(void *start, size_t length, int prot, int flags, int fd, off64_t offset) | |
292 | { | |
293 | static void* (*func) (void *, size_t, int, int, int, off64_t) = NULL; | |
294 | void *ret; | |
295 | ||
296 | if (!func) | |
297 | func = (void* (*) (void *, size_t, int, int, int, off64_t)) dlsym (REAL_LIBC, "mmap64"); | |
298 | ||
299 | ret = (*func) (start, length, prot, flags, fd, offset); | |
300 | fprintf(stderr,"MMAP64: %x, %d, %d, %d, %d, %lld -> %x\n", (unsigned int)start, length, prot, flags, fd, offset, (unsigned int)ret); | |
301 | mmapped = ret; | |
302 | mmapplen = length; | |
303 | ||
304 | return ret; | |
305 | } | |
306 | ||
307 | void *mmap2(void *start, size_t length, int prot, int flags, int fd, off_t pgoffset) | |
308 | { | |
309 | static void* (*func) (void *, size_t, int, int, int, off_t) = NULL; | |
310 | void *ret; | |
311 | ||
312 | if (!func) | |
313 | func = (void* (*) (void *, size_t, int, int, int, off_t)) dlsym (REAL_LIBC, "mmap2"); | |
314 | ||
315 | ret = (*func) (start, length, prot, flags, fd, pgoffset); | |
316 | fprintf(stderr,"MMAP2: %x, %d, %d, %d, %d, %d -> %x\n", (unsigned int)start, length, prot, flags, fd, pgoffset, (unsigned int)ret); | |
317 | mmapped = ret; | |
318 | mmapplen = length; | |
319 | ||
320 | return ret; | |
321 | } | |
322 | ||
323 | void *malloc(size_t size) | |
324 | { | |
325 | static void* (*func) (size_t) = NULL; | |
326 | void *ret; | |
327 | ||
328 | if (!func) | |
329 | func = (void* (*) (size_t)) dlsym(REAL_LIBC, "malloc"); | |
330 | ||
331 | ret = (*func) (size); | |
332 | ||
333 | //fprintf(stderr,"MALLOC: %d -> %x\n", size, (unsigned int) ret); | |
334 | ||
335 | return ret; | |
336 | } | |
337 | #endif | |
338 | ||
339 | ||
340 | #endif |