]> git.zerfleddert.de Git - proxmark3-svn/blame - client/proxusb.c
minor fix when reading blocks
[proxmark3-svn] / client / proxusb.c
CommitLineData
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 31usb_dev_handle *devh = NULL;
32static unsigned int claimed_iface = 0;
33unsigned char return_on_error = 0;
34unsigned char error_occured = 0;
35extern unsigned int current_command;
36
902cb3c0 37void SendCommand_(HidCommand *c)
7fe9b0b7 38{
39 int ret;
40
41#if 0
902cb3c0 42 printf("Sending %d bytes\n", sizeof(HidCommand));
7fe9b0b7 43#endif
44 current_command = c->cmd;
902cb3c0 45 ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(HidCommand), 1000);
7fe9b0b7 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
902cb3c0 66bool ReceiveCommandPoll(HidCommand *c)
7fe9b0b7 67{
68 int ret;
69
902cb3c0 70 memset(c, 0, sizeof (HidCommand));
71 ret = usb_bulk_read(devh, 0x82, (char*)c, sizeof(HidCommand), 500);
7fe9b0b7 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 {
902cb3c0 92 if (ret && (ret < sizeof(HidCommand))) {
7fe9b0b7 93 fprintf(stderr, "Read only %d instead of requested %d bytes!\n",
902cb3c0 94 ret, (int)sizeof(HidCommand));
7fe9b0b7 95 }
96 }
97
98 return ret > 0;
99}
100
902cb3c0 101void ReceiveCommand(HidCommand *c)
7fe9b0b7 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
112usb_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
534983d7 154 for (int i = 0; i < iUnit; i++) {
155 struct usb_device * dev = usb_device(units[i].handle);
156 fprintf(stdout, "\t%d. SN: %s [%s/%s]\n", i+1, units[i].serial_number, dev->bus->dirname, dev->filename);
157 }
602ac4d7 158 if (iUnit > 1) {
159 while (iSelection < 1 || iSelection > iUnit) {
160 fprintf(stdout, "Which unit do you want to connect to? ");
161 fscanf(stdin, "%d", &iSelection);
162 }
163 }
164 else
165 iSelection = 1;
166 iSelection --;
167
168 for (int i = 0; i < iUnit; i++) {
169 if (iSelection == i) continue;
170 usb_close(units[i].handle);
171 units[i].handle = NULL;
7fe9b0b7 172 }
602ac4d7 173
174 return units[iSelection].handle;
7fe9b0b7 175 }
176
177 return NULL;
178}
179
180usb_dev_handle* OpenProxmark(int verbose)
181{
182 int ret;
183 usb_dev_handle *handle = NULL;
184 unsigned int iface;
185
7fe9b0b7 186 handle = findProxmark(verbose, &iface);
187 if (!handle)
188 return NULL;
189
4cd41f34 190#ifdef __linux__
7fe9b0b7 191 /* detach kernel driver first */
192 ret = usb_detach_kernel_driver_np(handle, iface);
193 /* don't complain if no driver attached */
194 if (ret<0 && ret != -61 && verbose)
195 fprintf(stderr, "detach kernel driver failed: (%d) %s!\n", ret, usb_strerror());
196#endif
4cd41f34 197
198 // Needed for Windows. Optional for Mac OS and Linux
199 ret = usb_set_configuration(handle, 1);
200 if (ret < 0) {
201 if (verbose)
202 fprintf(stderr, "configuration set failed: %s!\n", usb_strerror());
203 return NULL;
204 }
205
7fe9b0b7 206 ret = usb_claim_interface(handle, iface);
207 if (ret < 0) {
208 if (verbose)
209 fprintf(stderr, "claim failed: %s!\n", usb_strerror());
210 return NULL;
211 }
7fe9b0b7 212 claimed_iface = iface;
213 devh = handle;
214 return handle;
215}
216
217void CloseProxmark(void)
218{
219 usb_release_interface(devh, claimed_iface);
220 usb_close(devh);
4cd41f34 221 devh = NULL;
7fe9b0b7 222}
Impressum, Datenschutz