]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net> | |
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 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <string.h> | |
15 | #include <stdbool.h> | |
16 | #include <unistd.h> | |
17 | #include <usb.h> | |
18 | #include <strings.h> | |
19 | #include <errno.h> | |
20 | ||
21 | #include "proxusb.h" | |
22 | #include "proxmark3.h" | |
23 | #include "usb_cmd.h" | |
24 | ||
25 | // It seems to be missing for mingw | |
26 | #ifndef ETIMEDOUT | |
27 | #define ETIMEDOUT 116 | |
28 | #endif | |
29 | ||
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 | } | |
57 | while(!OpenProxmark(0)) { sleep(1); } | |
58 | printf(PROXPROMPT); | |
59 | fflush(NULL); | |
60 | ||
61 | return; | |
62 | } | |
63 | } | |
64 | ||
65 | bool ReceiveCommandPoll(UsbCommand *c) | |
66 | { | |
67 | int ret; | |
68 | ||
69 | memset(c, 0, sizeof (UsbCommand)); | |
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 | } | |
84 | while(!OpenProxmark(0)) { sleep(1); } | |
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 | struct prox_unit units[50]; | |
116 | int iUnit = 0; | |
117 | ||
118 | usb_find_busses(); | |
119 | usb_find_devices(); | |
120 | ||
121 | busses = usb_get_busses(); | |
122 | ||
123 | for (bus = busses; bus; bus = bus->next) { | |
124 | struct usb_device *dev; | |
125 | ||
126 | for (dev = bus->devices; dev; dev = dev->next) { | |
127 | struct usb_device_descriptor *desc = &(dev->descriptor); | |
128 | ||
129 | if ((desc->idProduct == 0x4b8f) && (desc->idVendor == 0x9ac4)) { | |
130 | handle = usb_open(dev); | |
131 | if (!handle) { | |
132 | if (verbose) | |
133 | fprintf(stderr, "open fabiled: %s!\n", usb_strerror()); | |
134 | //return NULL; | |
135 | continue; | |
136 | } | |
137 | *iface = dev->config[0].interface[0].altsetting[0].bInterfaceNumber; | |
138 | ||
139 | struct prox_unit unit = {handle, {0}}; | |
140 | usb_get_string_simple(handle, desc->iSerialNumber, unit.serial_number, sizeof(unit.serial_number)); | |
141 | units[iUnit++] = unit; | |
142 | ||
143 | //return handle; | |
144 | } | |
145 | } | |
146 | } | |
147 | ||
148 | if (iUnit > 0) { | |
149 | int iSelection = 0; | |
150 | ||
151 | fprintf(stdout, "\nConnected units:\n"); | |
152 | ||
153 | for (int i = 0; i < iUnit; i++) { | |
154 | struct usb_device * dev = usb_device(units[i].handle); | |
155 | fprintf(stdout, "\t%d. SN: %s [%s/%s]\n", i+1, units[i].serial_number, dev->bus->dirname, dev->filename); | |
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; | |
171 | } | |
172 | ||
173 | return units[iSelection].handle; | |
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 | ||
185 | handle = findProxmark(verbose, &iface); | |
186 | if (!handle) | |
187 | return NULL; | |
188 | ||
189 | #ifdef __linux__ | |
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 | |
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 | ||
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 | } | |
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); | |
220 | devh = NULL; | |
221 | } |