1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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
8 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
14 // It seems to be missing for mingw
19 usb_dev_handle
*devh
= NULL
;
20 static unsigned int claimed_iface
= 0;
21 unsigned char return_on_error
= 0;
22 unsigned char error_occurred
= 0;
24 void SendCommandBL(uint64_t cmd
, uint64_t arg0
, uint64_t arg1
, uint64_t arg2
, void *data
, size_t len
) {
26 PacketCommandOLD c
= {CMD_UNKNOWN
, {0, 0, 0}, {{0}}};
32 memcpy(&c
.d
, data
, len
);
35 printf("Sending %d bytes\n", sizeof(PacketCommandOLD
));
38 ret
= usb_bulk_write(devh
, 0x01, (char *)&c
, sizeof(PacketCommandOLD
), 1000);
44 fprintf(stderr
, "write failed: %s!\nTrying to reopen device...\n",
51 while (!OpenProxmark(0)) { msleep(1000); }
59 bool ReceiveCommandPoll(PacketResponseOLD
*c
) {
62 memset(c
, 0, sizeof(PacketResponseOLD
));
63 ret
= usb_bulk_read(devh
, 0x82, (char *)c
, sizeof(PacketResponseOLD
), 500);
65 if (ret
!= -ETIMEDOUT
) {
70 fprintf(stderr
, "read failed: %s(%d)!\nTrying to reopen device...\n",
77 while (!OpenProxmark(0)) { msleep(1000); }
84 if (ret
&& (ret
< sizeof(PacketResponseOLD
))) {
85 fprintf(stderr
, "Read only %d instead of requested %d bytes!\n",
86 ret
, (int)sizeof(PacketResponseOLD
));
93 void ReceiveCommand(PacketResponseOLD
*c
) {
94 // printf("%s()\n", __FUNCTION__);
97 retval
= ReceiveCommandPoll(c
);
98 if (retval
!= 1) printf("ReceiveCommandPoll returned %d\n", retval
);
100 // printf("recv %x\n", c->cmd);
103 usb_dev_handle
*findProxmark(int verbose
, unsigned int *iface
) {
104 struct usb_bus
*busses
, *bus
;
105 usb_dev_handle
*handle
= NULL
;
106 struct prox_unit units
[50];
112 busses
= usb_get_busses();
114 for (bus
= busses
; bus
; bus
= bus
->next
) {
115 struct usb_device
*dev
;
117 for (dev
= bus
->devices
; dev
; dev
= dev
->next
) {
118 struct usb_device_descriptor
*desc
= &(dev
->descriptor
);
120 if ((desc
->idProduct
== 0x4b8f) && (desc
->idVendor
== 0x9ac4)) {
121 handle
= usb_open(dev
);
124 fprintf(stderr
, "open fabiled: %s!\n", usb_strerror());
128 *iface
= dev
->config
[0].interface
[0].altsetting
[0].bInterfaceNumber
;
130 struct prox_unit unit
= {handle
, {0}};
131 usb_get_string_simple(handle
, desc
->iSerialNumber
, unit
.serial_number
, sizeof(unit
.serial_number
));
132 units
[iUnit
++] = unit
;
142 fprintf(stdout
, "\nConnected units:\n");
144 for (int i
= 0; i
< iUnit
; i
++) {
145 struct usb_device
*dev
= usb_device(units
[i
].handle
);
146 fprintf(stdout
, "\t%d. SN: %s [%s/%s]\n", i
+ 1, units
[i
].serial_number
, dev
->bus
->dirname
, dev
->filename
);
149 while (iSelection
< 1 || iSelection
> iUnit
) {
150 fprintf(stdout
, "Which unit do you want to connect to? ");
151 int res
= fscanf(stdin
, "%d", &iSelection
);
153 fprintf(stderr
, "Input parse error");
164 for (int i
= 0; i
< iUnit
; i
++) {
165 if (iSelection
== i
) continue;
166 usb_close(units
[i
].handle
);
167 units
[i
].handle
= NULL
;
170 return units
[iSelection
].handle
;
175 usb_dev_handle
*OpenProxmark(int verbose
) {
177 usb_dev_handle
*handle
;
180 handle
= findProxmark(verbose
, &iface
);
185 /* detach kernel driver first */
186 ret
= usb_detach_kernel_driver_np(handle
, iface
);
187 /* don't complain if no driver attached */
188 if (ret
< 0 && ret
!= -61 && verbose
)
189 fprintf(stderr
, "detach kernel driver failed: (%d) %s!\n", ret
, usb_strerror());
192 // Needed for Windows. Optional for Mac OS and Linux
193 ret
= usb_set_configuration(handle
, 1);
196 fprintf(stderr
, "configuration set failed: %s!\n", usb_strerror());
200 ret
= usb_claim_interface(handle
, iface
);
203 fprintf(stderr
, "claim failed: %s!\n", usb_strerror());
206 claimed_iface
= iface
;
211 void CloseProxmark(void) {
212 usb_release_interface(devh
, claimed_iface
);