]> git.zerfleddert.de Git - proxmark3-svn/blob - client/prox.c
d619c821c2d54a0b3251a76be2aadf5fdc430910
[proxmark3-svn] / client / prox.c
1 #include <windows.h>
2 #include <setupapi.h>
3 #include <stdio.h>
4 #include <ctype.h>
5 #include <stdlib.h>
6 //extern "C" {
7 #include "include/hidusage.h"
8 #include "include/hidpi.h"
9 #include "include/hidsdi.h"
10 //}
11
12 #include "flash.h"
13 #include "usb_cmd.h"
14 #include "ui.h"
15
16 #define OUR_VID 0x9ac4
17 #define OUR_PID 0x4b8f
18 #define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
19
20 int offline = 0;
21 HANDLE UsbHandle;
22 extern unsigned int current_command;
23 extern struct partition partitions[];
24
25 static void ShowError(void)
26 {
27 char buf[1024];
28 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
29 buf, sizeof(buf), NULL);
30 printf("ERROR: %s", buf);
31 }
32
33 BOOL UsbConnect(void)
34 {
35 typedef void (__stdcall *GetGuidProc)(GUID *);
36 typedef BOOLEAN (__stdcall *GetAttrProc)(HANDLE, HIDD_ATTRIBUTES *);
37 typedef BOOLEAN (__stdcall *GetPreparsedProc)(HANDLE,
38 PHIDP_PREPARSED_DATA *);
39 typedef NTSTATUS (__stdcall *GetCapsProc)(PHIDP_PREPARSED_DATA, PHIDP_CAPS);
40 GetGuidProc getGuid;
41 GetAttrProc getAttr;
42 GetPreparsedProc getPreparsed;
43 GetCapsProc getCaps;
44
45 HMODULE h = LoadLibrary("hid.dll");
46 getGuid = (GetGuidProc)GetProcAddress(h, "HidD_GetHidGuid");
47 getAttr = (GetAttrProc)GetProcAddress(h, "HidD_GetAttributes");
48 getPreparsed = (GetPreparsedProc)GetProcAddress(h, "HidD_GetPreparsedData");
49 getCaps = (GetCapsProc)GetProcAddress(h, "HidP_GetCaps");
50
51 GUID hidGuid;
52 getGuid(&hidGuid);
53
54 HDEVINFO devInfo;
55 devInfo = SetupDiGetClassDevs(&hidGuid, NULL, NULL,
56 DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
57
58 SP_DEVICE_INTERFACE_DATA devInfoData;
59 devInfoData.cbSize = sizeof(devInfoData);
60
61 int i;
62 for(i = 0;; i++) {
63 if(!SetupDiEnumDeviceInterfaces(devInfo, 0, &hidGuid, i, &devInfoData))
64 {
65 if(GetLastError() != ERROR_NO_MORE_ITEMS) {
66 // printf("SetupDiEnumDeviceInterfaces failed\n");
67 }
68 // printf("done list\n");
69 SetupDiDestroyDeviceInfoList(devInfo);
70 return FALSE;
71 }
72
73 // printf("item %d:\n", i);
74
75 DWORD sizeReqd = 0;
76 if(!SetupDiGetDeviceInterfaceDetail(devInfo, &devInfoData,
77 NULL, 0, &sizeReqd, NULL))
78 {
79 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
80 // printf("SetupDiGetDeviceInterfaceDetail (0) failed\n");
81 continue;
82 }
83 }
84
85 SP_DEVICE_INTERFACE_DETAIL_DATA *devInfoDetailData =
86 (SP_DEVICE_INTERFACE_DETAIL_DATA *)malloc(sizeReqd);
87 devInfoDetailData->cbSize = sizeof(*devInfoDetailData);
88
89 if(!SetupDiGetDeviceInterfaceDetail(devInfo, &devInfoData,
90 devInfoDetailData, 87, NULL, NULL))
91 {
92 // printf("SetupDiGetDeviceInterfaceDetail (1) failed\n");
93 continue;
94 }
95
96 char *path = devInfoDetailData->DevicePath;
97
98 UsbHandle = CreateFile(path, /*GENERIC_READ |*/ GENERIC_WRITE,
99 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
100 FILE_FLAG_OVERLAPPED, NULL);
101
102 if(UsbHandle == INVALID_HANDLE_VALUE) {
103 ShowError();
104 // printf("CreateFile failed: for '%s'\n", path);
105 continue;
106 }
107
108 HIDD_ATTRIBUTES attr;
109 attr.Size = sizeof(attr);
110 if(!getAttr(UsbHandle, &attr)) {
111 ShowError();
112 // printf("HidD_GetAttributes failed\n");
113 continue;
114 }
115
116 // printf("VID: %04x PID %04x\n", attr.VendorID, attr.ProductID);
117
118 if(attr.VendorID != OUR_VID || attr.ProductID != OUR_PID) {
119 CloseHandle(UsbHandle);
120 // printf(" nope, not us\n");
121 continue;
122 }
123
124 // printf ("got it!\n");
125 CloseHandle(UsbHandle);
126
127 UsbHandle = CreateFile(path, GENERIC_READ | GENERIC_WRITE,
128 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
129 FILE_FLAG_OVERLAPPED, NULL);
130
131 if(UsbHandle == INVALID_HANDLE_VALUE) {
132 ShowError();
133 // printf("Error, couldn't open our own handle as desired.\n");
134 return FALSE;
135 }
136
137 PHIDP_PREPARSED_DATA pp;
138 getPreparsed(UsbHandle, &pp);
139 HIDP_CAPS caps;
140
141 if(getCaps(pp, &caps) != HIDP_STATUS_SUCCESS) {
142 // printf("getcaps failed\n");
143 return FALSE;
144 }
145
146 // printf("input/out report %d/%d\n", caps.InputReportByteLength,
147 // caps.OutputReportByteLength);
148
149
150 return TRUE;
151 }
152 return FALSE;
153 }
154
155 bool ReceiveCommandPoll(UsbCommand *c)
156 {
157 static BOOL ReadInProgress = FALSE;
158 static OVERLAPPED Ov;
159 static BYTE Buf[65];
160 static DWORD HaveRead;
161
162 if(!ReadInProgress) {
163 memset(&Ov, 0, sizeof(Ov));
164 ReadFile(UsbHandle, Buf, 65, &HaveRead, &Ov);
165 if(GetLastError() != ERROR_IO_PENDING) {
166 ShowError();
167 exit(-1);
168 }
169 ReadInProgress = TRUE;
170 }
171
172 if(HasOverlappedIoCompleted(&Ov)) {
173 ReadInProgress = FALSE;
174
175 if(!GetOverlappedResult(UsbHandle, &Ov, &HaveRead, FALSE)) {
176 ShowError();
177 exit(-1);
178 }
179
180 memcpy(c, Buf+1, 64);
181
182 return TRUE;
183 } else {
184 return FALSE;
185 }
186 }
187
188 void ReceiveCommand(UsbCommand *c)
189 {
190 while(!ReceiveCommandPoll(c)) {
191 Sleep(0);
192 }
193 }
194
195 void SendCommand(UsbCommand *c)
196 {
197 BYTE buf[65];
198 buf[0] = 0;
199 memcpy(buf+1, c, 64);
200
201 DWORD written;
202 OVERLAPPED ov;
203
204 memset(&ov, 0, sizeof(ov));
205 WriteFile(UsbHandle, buf, 65, &written, &ov);
206 if(GetLastError() != ERROR_IO_PENDING) {
207 ShowError();
208 exit(-1);
209 }
210
211 while(!HasOverlappedIoCompleted(&ov)) {
212 Sleep(0);
213 }
214
215 if(!GetOverlappedResult(UsbHandle, &ov, &written, FALSE)) {
216 ShowError();
217 exit(-1);
218 }
219 current_command = c->cmd;
220 }
221
222 static void usage(char **argv)
223 {
224 int i;
225 printf("Usage: %s gui\n", argv[0]);
226 printf(" %s offline\n", argv[0]);
227 printf(" %s areas file.elf\n", argv[0]);
228 printf(" Known areas are:");
229 for(i=0; partitions[i].name != NULL; i++) {
230 fprintf(stderr, " %s", partitions[i].name);
231 }
232
233 printf("\n");
234 }
235
236 int main(int argc, char **argv)
237 {
238 int i = 0;
239
240 if(argc < 2) {
241 usage(argv);
242 exit(-1);
243 }
244
245 // Only do this if NOT in offline mode
246 if (strcmp(argv[1], "offline"))
247 {
248 for(;;) {
249 if(UsbConnect()) {
250 break;
251 }
252 if(i == 0) {
253 printf("...no device connected, polling for it now\n");
254 }
255 if(i > 50000) {
256 printf("Could not connect to USB device; exiting.\n");
257 return -1;
258 }
259 i++;
260 Sleep(5);
261 }
262 }
263
264 if(strcmp(argv[1], "gui")==0) {
265 ShowGui();
266 } else if(strcmp(argv[1], "offline")==0) {
267 offline = 1;
268 ShowGui();
269 }
270
271 /* Count area arguments */
272 int areas = 0, offset=-1, length=0;
273 while(find_next_area(argv[1], &offset, &length)) areas++;
274
275 if(areas != argc - 2) {
276 usage(argv);
277 exit(-1);
278 }
279
280 do_flash(argv);
281 return 0;
282 }
Impressum, Datenschutz