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