forked from Massdrop/mdloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdloader_win32.c
560 lines (453 loc) · 14.9 KB
/
mdloader_win32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
* Copyright (C) 2018 Massdrop Inc.
*
* This file is part of Massdrop Loader.
*
* Massdrop Loader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Massdrop Loader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Massdrop Loader. If not, see <https://www.gnu.org/licenses/>.
*/
#include "mdloader_common.h"
#define EXAMPLE_PORT "COM23"
#define PORT_SEARCH_STRING "COM"
HANDLE gport = NULL; //Port of device
#define WRITE_SIZE 500 //Maximum bytes to write per call
#define READ_SIZE 500 //Maximum bytes to read per call
#define READ_RETRIES 10 //Maximum read retries before fail
void print_com_example(void)
{
printf("Example: -p " EXAMPLE_PORT "\n");
}
//Read data from device
//Must check read_error for a read error after return
//Return unsigned word from memory location
int read_data(int addr, int readsize)
{
read_error = 1; //Set read error flag as default
int readdata = 0;
char wbuf[] = "!XXXXXXXX,#"; //Largest buffer needed
DWORD ret;
if (readsize == 1)
sprintf(wbuf,"%c%02x,%c",CMD_READ_ADDR_8,addr,CMD_END);
else if (readsize == 2)
sprintf(wbuf,"%c%04x,%c",CMD_READ_ADDR_16,addr,CMD_END);
else if (readsize == 4)
sprintf(wbuf,"%c%08x,%c",CMD_READ_ADDR_32,addr,CMD_END);
else
{
if (verbose) printf("Error: Invalid read size given (%i)\n",readsize);
return 0;
}
if (verbose > 0) printf("Write: [%s]\n",wbuf);
PurgeComm(gport, PURGE_RXCLEAR | PURGE_TXCLEAR); //Flush any remaining data from a bad read
int writelen = strlen(wbuf);
if (!WriteFile(gport,wbuf,writelen,&ret,0))
{
if (verbose) printf("Error writing port [%s](%lu)\n",wbuf,GetLastError());
return 0;
}
if (ret != writelen)
{
if (verbose) printf("Error writing %i bytes [%lu]\n",writelen,ret);
return 0;
}
if (!ReadFile(gport, &readdata, readsize, &ret, NULL))
{
if (verbose) printf("Error reading port [%i][%lu](%lu)\n",readsize,ret,GetLastError());
return 0;
}
if (ret != readsize)
{
if (verbose) printf("Error reading %i bytes! [%lu]\n",readsize,ret);
return 0;
}
read_error = 0; //Clear read error flag
return readdata;
}
//Write data to device
//Return 1 on success, 0 on failure
int write_data(int addr, int writesize, int data)
{
if (check_bootloader_write_attempt(addr)) return 0; //Prevent writes to bootloader section
char wbuf[] = "!XXXXXXXX,XXXXXXXX#"; //Largest buffer needed
DWORD ret;
if (writesize == 1)
sprintf(wbuf,"%c%08x,%02x%c",CMD_WRITE_ADDR_8,addr,data,CMD_END);
else if (writesize == 2)
sprintf(wbuf,"%c%08x,%04x%c",CMD_WRITE_ADDR_16,addr,data,CMD_END);
else if (writesize == 4)
sprintf(wbuf,"%c%08x,%08x%c",CMD_WRITE_ADDR_32,addr,data,CMD_END);
else
{
if (verbose) printf("Error: Invalid write size given (%i)\n",writesize);
return 0;
}
if (verbose) printf("Write %i bytes: [%s]\n",writesize,wbuf);
int writelen = strlen(wbuf);
if (!WriteFile(gport,wbuf,writelen,&ret,0))
{
if (verbose) printf("Error writing port [%s](%lu)\n",wbuf,GetLastError());
return 0;
}
if (ret != writelen)
{
if (verbose) printf("Error writing %i bytes! [%lu]\n",writesize,ret);
return 0;
}
return 1;
}
//Jump to address and run
//Return 1 on success, 0 on failure
int goto_address(int addr)
{
char wbuf[] = "!XXXXXXXX#";
DWORD ret;
sprintf(wbuf,"%c%08x%c",CMD_GOTO_ADDR,addr,CMD_END);
if (verbose) printf("Write: [%s]\n",wbuf);
int writelen = strlen(wbuf);
if (!WriteFile(gport,wbuf,strlen(wbuf),&ret,0))
{
if (verbose) printf("Error writing port [%s](%lu)\n",wbuf,GetLastError());
return 0;
}
if (ret != writelen)
{
if (verbose) printf("Error writing goto address! [%lu]\n",ret);
return 0;
}
return 1;
}
//Read data from device RAM
//Return pointer to buffer on success, NULL on failure
char *recv_file(int addr, int bytes)
{
char wbuf[] = "!XXXXXXXX,XXXXXXXX#";
char *data = NULL;
data = (char *)calloc(bytes+1,sizeof(char));
if (data == NULL)
{
printf("Error allocating read buffer memory!\n");
return NULL;
}
char *pdata = data;
DWORD ret;
int retries = READ_RETRIES;
int readsize = READ_SIZE;
//Constrain read size to buffer size
if (initparams.argument.outputInit.bufferSize > 0 && initparams.argument.outputInit.bufferSize < readsize)
readsize = initparams.argument.outputInit.bufferSize;
while (bytes > 0)
{
if (readsize > bytes) readsize = bytes;
sprintf(wbuf,"%c%08x,%08x%c",CMD_RECV_FILE,addr,readsize,CMD_END);
if (verbose > 0) printf("Write: [%s]\n",wbuf);
PurgeComm(gport, PURGE_RXCLEAR | PURGE_TXCLEAR); //Flush any remaining data from a bad read
long writelen = strlen(wbuf);
if (!WriteFile(gport,wbuf,writelen,&ret,0))
{
if (verbose) printf("Error writing port [%s](%lu)\n",wbuf,GetLastError());
free(data);
return NULL;
}
if (!ReadFile(gport, pdata, bytes, &ret, NULL))
{
if (verbose) printf("Error reading port [%i][%lu](%lu)\n",readsize,ret,GetLastError());
free(data);
return NULL;
}
if (ret != readsize)
{
if (verbose) printf("Error reading %i bytes! [%lu](%lu)\n",readsize,ret,GetLastError());
if (retries <= 0)
{
if (verbose) printf("No retries remain!\n");
free(data);
return NULL;
}
retries--;
if (verbose) printf("Retrying read... (%i)\n",READ_RETRIES - retries);
continue; //Attempt read again
}
if (verbose > 0) printf("Recv: [%lu]\n",ret);
retries = READ_RETRIES; //Reset retry limit
bytes -= ret; //Decrement remaining bytes
addr += ret; //Increment to next address
pdata += ret; //Increment pointer in recv buffer
}
return data;
}
//Write data to device
//Return 1 on sucess, 0 on failure
int send_file(int addr, int bytes, char *data)
{
if (check_bootloader_write_attempt(addr)) return 0; //Prevent writes to bootloader section
if (bytes < 1)
{
printf("Error: No data to send!\n");
return 0;
}
char wbuf[] = "!XXXXXXXX,XXXXXXXX#";
DWORD ret;
char *pdata = data;
int writesize = WRITE_SIZE;
//Constrain write size to buffer size
if (initparams.argument.outputInit.bufferSize > 0 && initparams.argument.outputInit.bufferSize < writesize)
writesize = initparams.argument.outputInit.bufferSize;
while (bytes > 0)
{
if (writesize > bytes) writesize = bytes;
sprintf(wbuf,"%c%08x,%08x%c",CMD_SEND_FILE,addr,writesize,CMD_END);
if (verbose > 0) printf("Write: [%s]\n",wbuf);
if (!WriteFile(gport, wbuf, strlen(wbuf), &ret, 0))
{
if (verbose) printf("Error writing port [%s](%lu)\n",wbuf,GetLastError());
return 0;
}
if (ret != strlen(wbuf))
{
if (verbose) printf("Error writing port [%s](%lu)\n",wbuf,GetLastError());
return 0;
}
if (verbose > 0) printf("Write: %i bytes\n",writesize);
if (!WriteFile(gport, pdata, writesize, &ret, 0))
{
if (verbose) printf("Error writing port [%lu][%i](%lu)\n",ret,writesize,GetLastError());
return 0;
}
if (ret != writesize)
{
printf("Error writing port [%lu][%i](%lu)\n",ret,writesize,GetLastError());
return 0;
}
bytes -= ret; //Decrement remaining bytes
addr += ret; //Increment to next address
pdata += ret; //Increment pointer in send buffer
}
return 1;
}
//Print bootloader version
//Return 1 on sucess, 0 on failure
int print_bootloader_version(void)
{
char wbuf[] = "!#";
char readdata[128] = "";
DWORD ret;
int readsize = sizeof(readdata) - 1;
sprintf(wbuf,"%c%c",CMD_READ_VERSION,CMD_END);
if (verbose > 0) printf("Write: [%s]\n",wbuf);
int writelen = strlen(wbuf);
if (!WriteFile(gport,wbuf,writelen,&ret,0))
{
if (verbose) printf("Version: Error writing port [%s](%lu)\n",wbuf,GetLastError());
else printf("Version: Error retrieving!\n");
return 0;
}
if (!ReadFile(gport, &readdata, readsize, &ret, NULL))
{
if (verbose) printf("Version: Error reading port [%i][%lu](%lu)\n",readsize,ret,GetLastError());
else printf("Version: Error retrieving!\n");
return 0;
}
while (readdata[strlen(readdata)-1] == '\n' || readdata[strlen(readdata)-1] == '\r') readdata[strlen(readdata)-1] = 0;
printf("Bootloader version: %s\n",readdata);
return 1;
}
//Set normal command mode
//Return 1 on sucess, 0 on failure
int set_normal_mode(void)
{
if (verbose) printf("Setting normal mode... ");
int retbuf = 0;
char wbuf[] = "!#";
DWORD ret;
sprintf(wbuf,"%c%c",CMD_SET_NORMAL_MODE,CMD_END);
int writelen = strlen(wbuf);
if (!WriteFile(gport,wbuf,writelen,&ret,0))
{
if (verbose) printf("Failed! (%lu)\n",GetLastError());
return 0;
}
if (ret != writelen)
{
if (verbose) printf("Error writing %i bytes! [%lu](%lu)\n",writelen,ret,GetLastError());
return 0;
}
int readlen = 2;
if (!ReadFile(gport, &retbuf, readlen, &ret, NULL))
{
if (verbose) printf("Error reading port [%i][%lu](%lu)\n",readlen,ret,GetLastError());
return 0;
}
if (ret != readlen)
{
if (verbose) printf("Error reading %i bytes! [%lu][%04x](%lu)\n",readlen,ret,retbuf,GetLastError());
return 0;
}
if ((retbuf & 0xFFFF) != 0x0D0A)
{
if (verbose) printf("Error: Incorrect response! [%lu][%04x](%lu)\n",ret,retbuf,GetLastError());
return 0;
}
if (verbose) printf("Success!\n");
return 1;
}
//Jump to loaded application
//Return 1 on sucess, 0 on failure
int jump_application(void)
{
printf("Booting device... ");
if (testmode)
{
printf("(test mode disables restart)\n");
return 1;
}
char wbuf[] = "!#";
DWORD ret;
DWORD writelen = strlen(wbuf);
sprintf(wbuf,"%c%c",CMD_LOAD_APP,CMD_END);
if (!WriteFile(gport,wbuf,writelen,&ret,0))
{
printf("Failed! (%lu)\n",GetLastError());
return 0;
}
printf("Success!\n");
return 1;
}
//Open port
//Return 1 on sucess, 0 on failure
int open_port(char *portname, char silent)
{
char portnamebuf[64] = "";
if (!silent || verbose) printf("Opening port '%s'... ",portname);
sprintf(portnamebuf,"\\\\.\\%s",portname);
gport = CreateFile(portnamebuf,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if (gport == INVALID_HANDLE_VALUE)
{
if (!silent || verbose)
{
printf("Failed!");
LPTSTR errstr = NULL;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|
FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&errstr,
0,
NULL);
if (errstr != NULL)
{
char *pstr = errstr + strlen(errstr);
while (pstr > errstr && *pstr <= 32) { *pstr = 0; pstr--; } //Clean up end of message
if (!silent) printf(" (%s)",errstr);
LocalFree(errstr);
}
printf("\n");
}
return 0;
}
if (!silent || verbose) printf("Success!\n");
return 1;
}
//Close port
//Return 1 on sucess, 0 on failure
int close_port(char silent)
{
if (!silent || verbose) printf("Closing port... ");
if (CloseHandle(gport) == 0)
{
if (!silent || verbose) printf("Failed! (%lu)\n",GetLastError());
return 0;
}
if (!silent || verbose) printf("Success!\n");
return 1;
}
//Configure port
//Return 1 on sucess, 0 on failure
int config_port(void)
{
DCB dcb = {};
if (verbose) printf("Configuring port... \n");
if (verbose) printf(" Get config... ");
if (GetCommState(gport,&dcb) == 0)
{
if (verbose) printf("Failed! (%lu)\n",GetLastError());
return 0;
}
if (verbose) printf("Success!\n");
dcb.BaudRate = CBR_115200;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
dcb.ByteSize = 8;
if (verbose) printf(" Set config... ");
if (!SetCommState(gport,&dcb))
{
if (verbose) printf("Failed! (%lu)\n",GetLastError());
return 0;
}
if (verbose) printf("Success!\n");
COMMTIMEOUTS comTimeOut;
comTimeOut.ReadIntervalTimeout = 1;
comTimeOut.ReadTotalTimeoutMultiplier = 1;
comTimeOut.ReadTotalTimeoutConstant = 1;
comTimeOut.WriteTotalTimeoutMultiplier = 1;
comTimeOut.WriteTotalTimeoutConstant = 1;
if (verbose) printf(" Set timeouts... ");
if (!SetCommTimeouts(gport,&comTimeOut))
{
if (verbose) printf("Failed! (%lu)\n",GetLastError());
return 0;
}
if (verbose) printf("Success!\n");
PurgeComm(gport, PURGE_RXCLEAR | PURGE_TXCLEAR); //Flush port
return 1;
}
//List devices which communicate properly
//If first_device is not null, store first found device and return
void list_devices(char *first_device)
{
int portnum = 1;
int portmax = 255; //Inclusive
int portcount = 0;
char portname[] = "COM255"; //Max buffer necessary
if (first_device == NULL)
{
printf("Bootloader port listing\n");
printf("-----------------------------\n");
}
while (portnum <= portmax)
{
sprintf(portname,PORT_SEARCH_STRING "%i",portnum);
if (test_port(portname,TRUE))
{
if (test_mcu(TRUE))
{
if (first_device) printf("\n");
printf("Device port: %s (%s)\n",portname,mcu->name);
if (first_device != NULL)
{
close_port(TRUE);
strcpy(first_device,portname);
return;
}
portcount++;
}
close_port(TRUE);
}
portnum++;
}
if (first_device == NULL)
{
if (portcount == 0)
printf("No devices found!\n");
}
}