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