]>
Commit | Line | Data |
---|---|---|
6658905f | 1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3a8464f0 | 3 | #include <stdbool.h> |
6658905f | 4 | #include <unistd.h> |
5 | #include <usb.h> | |
6 | #include <strings.h> | |
7 | #include <errno.h> | |
8 | ||
cd00aa30 | 9 | #include "prox.h" |
6658905f | 10 | #include "proxmark3.h" |
9b255608 | 11 | #include "../include/usb_cmd.h" |
6658905f | 12 | usb_dev_handle *devh = NULL; |
13 | static unsigned int claimed_iface = 0; | |
14 | unsigned char return_on_error = 0; | |
15 | unsigned char error_occured = 0; | |
9b255608 | 16 | extern unsigned int current_command; |
6658905f | 17 | |
9b255608 | 18 | void SendCommand(UsbCommand *c) { |
6658905f | 19 | int ret; |
20 | ||
21 | #if 0 | |
22 | printf("Sending %d bytes\n", sizeof(UsbCommand)); | |
23 | #endif | |
9b255608 | 24 | current_command = c->cmd; |
6658905f | 25 | ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(UsbCommand), 1000); |
26 | if (ret<0) { | |
27 | error_occured = 1; | |
28 | if (return_on_error) | |
29 | return; | |
30 | ||
31 | fprintf(stderr, "write failed: %s!\nTrying to reopen device...\n", | |
32 | usb_strerror()); | |
33 | ||
34 | if (devh) { | |
35 | usb_close(devh); | |
36 | devh = NULL; | |
37 | } | |
38 | while(!(devh=OpenProxmark(0))) { sleep(1); } | |
39 | printf(PROXPROMPT); | |
40 | fflush(NULL); | |
41 | ||
42 | return; | |
43 | } | |
9b255608 | 44 | /* |
6658905f | 45 | if(wantAck) { |
46 | UsbCommand ack; | |
9b255608 | 47 | printf("waiting for ack\n"); |
6658905f | 48 | ReceiveCommand(&ack); |
49 | if(ack.cmd != CMD_ACK) { | |
50 | printf("bad ACK\n"); | |
51 | exit(-1); | |
52 | } | |
9b255608 | 53 | } else { |
54 | printf("not waiting for ack\n"); | |
6658905f | 55 | } |
9b255608 | 56 | */ |
6658905f | 57 | } |
58 | ||
aa81a8d3 | 59 | bool ReceiveCommandPoll(UsbCommand *c) { |
6658905f | 60 | int ret; |
61 | ||
62 | bzero(c, sizeof(UsbCommand)); | |
63 | ret = usb_bulk_read(devh, 0x82, (char*)c, sizeof(UsbCommand), 500); | |
64 | if (ret<0) { | |
65 | if (ret != -ETIMEDOUT) { | |
66 | error_occured = 1; | |
67 | if (return_on_error) | |
9b255608 | 68 | return false; |
6658905f | 69 | |
70 | fprintf(stderr, "read failed: %s(%d)!\nTrying to reopen device...\n", | |
71 | usb_strerror(), ret); | |
72 | ||
73 | if (devh) { | |
74 | usb_close(devh); | |
75 | devh = NULL; | |
76 | } | |
77 | while(!(devh=OpenProxmark(0))) { sleep(1); } | |
78 | printf(PROXPROMPT); | |
79 | fflush(NULL); | |
80 | ||
9b255608 | 81 | return false; |
6658905f | 82 | } |
83 | } else { | |
84 | if (ret && (ret < sizeof(UsbCommand))) { | |
85 | fprintf(stderr, "Read only %d instead of requested %d bytes!\n", | |
86 | ret, (int)sizeof(UsbCommand)); | |
87 | } | |
88 | ||
89 | #if 0 | |
90 | { | |
91 | int i; | |
92 | ||
93 | printf("Read %d bytes\n", ret); | |
94 | for (i = 0; i < ret; i++) { | |
95 | printf("0x%02X ", ((unsigned char*)c)[i]); | |
96 | if (!((i+1)%8)) | |
97 | printf("\n"); | |
98 | } | |
99 | printf("\n"); | |
100 | } | |
101 | #endif | |
102 | } | |
103 | ||
9b255608 | 104 | return ret != 0; |
6658905f | 105 | } |
106 | ||
107 | void ReceiveCommand(UsbCommand *c) { | |
9b255608 | 108 | // printf("%s()\n", __FUNCTION__); |
109 | int retval = 0; | |
110 | do { | |
111 | retval = ReceiveCommandPoll(c); | |
112 | if (retval != 1) printf("ReceiveCommandPoll returned %d\n", retval); | |
113 | } while(retval<0); | |
114 | // printf("recv %x\n", c->cmd); | |
6658905f | 115 | } |
116 | ||
117 | usb_dev_handle* findProxmark(int verbose, unsigned int *iface) { | |
118 | struct usb_bus *busses, *bus; | |
119 | usb_dev_handle *handle = NULL; | |
120 | ||
121 | usb_find_busses(); | |
122 | usb_find_devices(); | |
123 | ||
124 | busses = usb_get_busses(); | |
125 | ||
126 | for (bus = busses; bus; bus = bus->next) { | |
127 | struct usb_device *dev; | |
128 | ||
129 | for (dev = bus->devices; dev; dev = dev->next) { | |
130 | struct usb_device_descriptor *desc = &(dev->descriptor); | |
131 | ||
132 | if ((desc->idProduct == 0x4b8f) && (desc->idVendor == 0x9ac4)) { | |
133 | handle = usb_open(dev); | |
134 | if (!handle) { | |
135 | if (verbose) | |
136 | fprintf(stderr, "open failed: %s!\n", usb_strerror()); | |
137 | return NULL; | |
138 | } | |
139 | ||
140 | *iface = dev->config[0].interface[0].altsetting[0].bInterfaceNumber; | |
141 | ||
142 | return handle; | |
143 | } | |
144 | } | |
145 | } | |
146 | ||
147 | return NULL; | |
148 | } | |
149 | ||
150 | usb_dev_handle* OpenProxmark(int verbose) { | |
151 | int ret; | |
152 | usb_dev_handle *handle = NULL; | |
153 | unsigned int iface; | |
154 | ||
155 | #ifndef __APPLE__ | |
156 | handle = findProxmark(verbose, &iface); | |
157 | if (!handle) | |
158 | return NULL; | |
159 | ||
160 | /* Whatever... */ | |
161 | usb_reset(handle); | |
162 | #endif | |
163 | ||
164 | handle = findProxmark(verbose, &iface); | |
165 | if (!handle) | |
166 | return NULL; | |
167 | ||
48e6e0a6 | 168 | #ifndef __APPLE__ |
7fdbe272 | 169 | /* detach kernel driver first */ |
170 | ret = usb_detach_kernel_driver_np(handle, iface); | |
171 | /* don't complain if no driver attached */ | |
172 | if (ret<0 && ret != -61 && verbose) | |
173 | fprintf(stderr, "detach kernel driver failed: (%d) %s!\n", ret, usb_strerror()); | |
48e6e0a6 | 174 | #endif |
6658905f | 175 | ret = usb_claim_interface(handle, iface); |
176 | if (ret<0) { | |
177 | if (verbose) | |
178 | fprintf(stderr, "claim failed: %s!\n", usb_strerror()); | |
179 | return NULL; | |
180 | } | |
181 | ||
182 | claimed_iface = iface; | |
183 | devh = handle; | |
184 | return handle; | |
185 | } | |
186 | ||
187 | void CloseProxmark(void) { | |
188 | usb_release_interface(devh, claimed_iface); | |
189 | usb_close(devh); | |
190 | } |