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