]>
Commit | Line | Data |
---|---|---|
a553f267 | 1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) 2010 iZsh <izsh at fail0verflow.com> | |
3 | // | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // USB utilities | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
7fe9b0b7 | 11 | #include <stdio.h> |
12 | #include <stdlib.h> | |
243dc690 | 13 | #include <string.h> |
7fe9b0b7 | 14 | #include <stdbool.h> |
15 | #include <unistd.h> | |
16 | #include <usb.h> | |
17 | #include <strings.h> | |
18 | #include <errno.h> | |
19 | ||
4cd41f34 | 20 | #include "sleep.h" |
7fe9b0b7 | 21 | #include "proxusb.h" |
22 | #include "proxmark3.h" | |
23 | #include "usb_cmd.h" | |
24 | ||
4cd41f34 | 25 | // It seems to be missing for mingw |
26 | #ifndef ETIMEDOUT | |
27 | #define ETIMEDOUT 116 | |
28 | #endif | |
29 | ||
7fe9b0b7 | 30 | usb_dev_handle *devh = NULL; |
31 | static unsigned int claimed_iface = 0; | |
32 | unsigned char return_on_error = 0; | |
33 | unsigned char error_occured = 0; | |
34 | extern unsigned int current_command; | |
35 | ||
36 | void SendCommand(UsbCommand *c) | |
37 | { | |
38 | int ret; | |
39 | ||
40 | #if 0 | |
41 | printf("Sending %d bytes\n", sizeof(UsbCommand)); | |
42 | #endif | |
43 | current_command = c->cmd; | |
44 | ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(UsbCommand), 1000); | |
45 | if (ret<0) { | |
46 | error_occured = 1; | |
47 | if (return_on_error) | |
48 | return; | |
49 | ||
50 | fprintf(stderr, "write failed: %s!\nTrying to reopen device...\n", | |
51 | usb_strerror()); | |
52 | ||
53 | if (devh) { | |
54 | usb_close(devh); | |
55 | devh = NULL; | |
56 | } | |
4cd41f34 | 57 | while(!OpenProxmark(0)) { sleep(1); } |
7fe9b0b7 | 58 | printf(PROXPROMPT); |
59 | fflush(NULL); | |
60 | ||
61 | return; | |
62 | } | |
63 | } | |
64 | ||
65 | bool ReceiveCommandPoll(UsbCommand *c) | |
66 | { | |
67 | int ret; | |
68 | ||
4cd41f34 | 69 | memset(c, 0, sizeof (UsbCommand)); |
7fe9b0b7 | 70 | ret = usb_bulk_read(devh, 0x82, (char*)c, sizeof(UsbCommand), 500); |
71 | if (ret<0) { | |
72 | if (ret != -ETIMEDOUT) { | |
73 | error_occured = 1; | |
74 | if (return_on_error) | |
75 | return false; | |
76 | ||
77 | fprintf(stderr, "read failed: %s(%d)!\nTrying to reopen device...\n", | |
78 | usb_strerror(), ret); | |
79 | ||
80 | if (devh) { | |
81 | usb_close(devh); | |
82 | devh = NULL; | |
83 | } | |
4cd41f34 | 84 | while(!OpenProxmark(0)) { sleep(1); } |
7fe9b0b7 | 85 | printf(PROXPROMPT); |
86 | fflush(NULL); | |
87 | ||
88 | return false; | |
89 | } | |
90 | } else { | |
91 | if (ret && (ret < sizeof(UsbCommand))) { | |
92 | fprintf(stderr, "Read only %d instead of requested %d bytes!\n", | |
93 | ret, (int)sizeof(UsbCommand)); | |
94 | } | |
95 | } | |
96 | ||
97 | return ret > 0; | |
98 | } | |
99 | ||
100 | void ReceiveCommand(UsbCommand *c) | |
101 | { | |
102 | // printf("%s()\n", __FUNCTION__); | |
103 | int retval = 0; | |
104 | do { | |
105 | retval = ReceiveCommandPoll(c); | |
106 | if (retval != 1) printf("ReceiveCommandPoll returned %d\n", retval); | |
107 | } while(retval<0); | |
108 | // printf("recv %x\n", c->cmd); | |
109 | } | |
110 | ||
111 | usb_dev_handle* findProxmark(int verbose, unsigned int *iface) | |
112 | { | |
113 | struct usb_bus *busses, *bus; | |
114 | usb_dev_handle *handle = NULL; | |
115 | ||
116 | usb_find_busses(); | |
117 | usb_find_devices(); | |
118 | ||
119 | busses = usb_get_busses(); | |
120 | ||
121 | for (bus = busses; bus; bus = bus->next) { | |
122 | struct usb_device *dev; | |
123 | ||
124 | for (dev = bus->devices; dev; dev = dev->next) { | |
125 | struct usb_device_descriptor *desc = &(dev->descriptor); | |
126 | ||
127 | if ((desc->idProduct == 0x4b8f) && (desc->idVendor == 0x9ac4)) { | |
128 | handle = usb_open(dev); | |
129 | if (!handle) { | |
130 | if (verbose) | |
131 | fprintf(stderr, "open failed: %s!\n", usb_strerror()); | |
132 | return NULL; | |
133 | } | |
134 | *iface = dev->config[0].interface[0].altsetting[0].bInterfaceNumber; | |
135 | return handle; | |
136 | } | |
137 | ||
138 | } | |
139 | } | |
140 | ||
141 | return NULL; | |
142 | } | |
143 | ||
144 | usb_dev_handle* OpenProxmark(int verbose) | |
145 | { | |
146 | int ret; | |
147 | usb_dev_handle *handle = NULL; | |
148 | unsigned int iface; | |
149 | ||
4cd41f34 | 150 | #ifdef __linux__ |
7fe9b0b7 | 151 | handle = findProxmark(verbose, &iface); |
152 | if (!handle) | |
153 | return NULL; | |
154 | ||
155 | /* Whatever... */ | |
156 | usb_reset(handle); | |
157 | #endif | |
158 | ||
159 | handle = findProxmark(verbose, &iface); | |
160 | if (!handle) | |
161 | return NULL; | |
162 | ||
4cd41f34 | 163 | #ifdef __linux__ |
7fe9b0b7 | 164 | /* detach kernel driver first */ |
165 | ret = usb_detach_kernel_driver_np(handle, iface); | |
166 | /* don't complain if no driver attached */ | |
167 | if (ret<0 && ret != -61 && verbose) | |
168 | fprintf(stderr, "detach kernel driver failed: (%d) %s!\n", ret, usb_strerror()); | |
169 | #endif | |
4cd41f34 | 170 | |
171 | // Needed for Windows. Optional for Mac OS and Linux | |
172 | ret = usb_set_configuration(handle, 1); | |
173 | if (ret < 0) { | |
174 | if (verbose) | |
175 | fprintf(stderr, "configuration set failed: %s!\n", usb_strerror()); | |
176 | return NULL; | |
177 | } | |
178 | ||
7fe9b0b7 | 179 | ret = usb_claim_interface(handle, iface); |
180 | if (ret < 0) { | |
181 | if (verbose) | |
182 | fprintf(stderr, "claim failed: %s!\n", usb_strerror()); | |
183 | return NULL; | |
184 | } | |
7fe9b0b7 | 185 | claimed_iface = iface; |
186 | devh = handle; | |
187 | return handle; | |
188 | } | |
189 | ||
190 | void CloseProxmark(void) | |
191 | { | |
192 | usb_release_interface(devh, claimed_iface); | |
193 | usb_close(devh); | |
4cd41f34 | 194 | devh = NULL; |
7fe9b0b7 | 195 | } |