-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTM32prog.c
783 lines (646 loc) · 12.1 KB
/
STM32prog.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*************************************************************************
STM32 programmer
(C) 2012 Krzysztof Nikiel
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <asm/ioctls.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <unistd.h>
#include <getopt.h>
#define TRUE 1
#define FALSE 0
#define ACK 0x79
#define NACK 0x1f
#define CMDGET 0x00
#define CMDGV 0x01
#define CMDGID 0x02
#define CMDRM 0x11
#define CMDGO 0x21
#define CMDWM 0x31
#define CMDER 0x43
#define CMDEER 0x44
#define CMDWP 0x63
#define CMDWUNP 0x73
#define CMDRDP 0x82
#define CMDRDUNP 0x92
static int g_fd = 0;
static uint8_t g_in;
static int g_speed = 17;
static char *g_dev = NULL;
#define MAXCMDS 20
static struct
{
uint8_t ver;
int num;
uint8_t list[MAXCMDS];
} g_cmds;
static int devopen(void);
static inline int recv(void)
{
int tmp;
if (!g_fd)
{
fprintf(stderr, "recv: g_fd not open\n");
exit(1);
}
if ((tmp = read(g_fd, &g_in, 1)) == 1)
return 0;
if (tmp < 0)
{
perror("read");
return -1;
}
if (tmp < 1)
{
fprintf(stderr, "no data to read\n");
return -1;
}
return 0;
}
static inline int send(uint8_t byte)
{
if (!g_fd)
{
fprintf(stderr, "send: g_fd not open\n");
exit(1);
}
if (write(g_fd, &byte, 1) < 0)
{
perror("write");
return -1;
}
return 0;
}
static int g_ackwait = 100;
static int getack(void)
{
int wait = 10;
wait:
if (recv())
{
if (wait < g_ackwait)
{
fprintf(stderr, "waiting for ACK\n");
usleep(1000*wait);
wait *= 2;
goto wait;
}
return FALSE;
}
if (g_in != ACK)
{
fprintf(stderr, "no ACK (0x%02x)\n", g_in);
return FALSE;
}
return TRUE;
}
static int cmdget(void)
{
int cnt;
if (devopen())
exit(1);
fprintf(stderr, "sending GET\n");
send(CMDGET);
send(~CMDGET);
if (!getack())
return FALSE;
if (recv())
{
fprintf(stderr, "no bytes\n");
return FALSE;
}
g_cmds.num = g_in;
if (recv())
{
fprintf(stderr, "can't read loader version\n");
return FALSE;
}
g_cmds.ver = g_in;
if (g_cmds.num > MAXCMDS)
{
fprintf(stderr, "commad list too big\n");
return FALSE;
}
for (cnt = 0; cnt < g_cmds.num; cnt++)
{
if (recv())
{
fprintf(stderr, "can't read cmd byte %d\n", cnt);
return FALSE;
}
g_cmds.list[cnt] = g_in;
}
fprintf(stderr, "bootloader version: %d.%d\n", g_cmds.ver >> 4,
g_cmds.ver & 15);
fprintf(stderr, "commands:");
for (cnt = 0; cnt < g_cmds.num; cnt++)
fprintf(stderr, " 0x%02x", g_cmds.list[cnt]);
fprintf(stderr, "\n");
return getack();
}
static int checkcmd(uint8_t cmd)
{
int cnt;
if (g_cmds.num < 0)
cmdget();
for (cnt = 0; cnt < g_cmds.num; cnt++)
if (g_cmds.list[cnt] == cmd)
return FALSE;
fprintf(stderr, "command 0x%02x not available\n", cmd);
return TRUE;
}
static int cmdgv(void)
{
if (checkcmd(CMDGV))
return FALSE;
fprintf(stderr, "sending GV\n");
send(CMDGV);
send(~CMDGV);
if (!getack())
return FALSE;
if (recv())
{
fprintf(stderr, "no bytes\n");
return FALSE;
}
fprintf(stderr, "GV version: 0x%02x\n", g_in);
if (recv())
{
fprintf(stderr, "no bytes\n");
return FALSE;
}
fprintf(stderr, "GV option1: 0x%02x\n", g_in);
if (recv())
{
fprintf(stderr, "no bytes\n");
return FALSE;
}
fprintf(stderr, "GV option2: 0x%02x\n", g_in);
return getack();
}
static int cmdgid(void)
{
int cnt;
int num;
if (checkcmd(CMDGID))
return FALSE;
fprintf(stderr, "sending GID\n");
send(CMDGID);
send(~CMDGID);
if (!getack())
return FALSE;
if (recv())
{
fprintf(stderr, "no bytes\n");
return FALSE;
}
num = g_in + 1;
fprintf(stderr, "PID: 0x");
for (cnt = 0; cnt < num; cnt++)
{
if (recv())
{
fprintf(stderr, "can't read PID byte %d\n", cnt);
return FALSE;
}
fprintf(stderr, "%02x", g_in);
}
fprintf(stderr, "\n");
return getack();
}
// read memory:
static int cmdrm(uint32_t addr32, uint8_t len)
{
int cnt;
uint8_t addr[4];
uint8_t xsum;
uint8_t buf[0x100];
if (checkcmd(CMDRM))
return FALSE;
fprintf(stderr, "sending ReadMemory(%08x,%x)\n", addr32, len + 1);
send(CMDRM);
send(~CMDRM);
if (!getack())
return FALSE;
for (cnt = 0; cnt < 4; cnt++)
addr[cnt] = addr32 >> (cnt * 8);
//fprintf(stderr, "sending address\n");
xsum = 0;
for (cnt = 3; cnt >= 0; cnt--)
{
send(addr[cnt]);
xsum ^= addr[cnt];
}
send(xsum);
if (!getack())
return FALSE;
send(len);
send(~len);
if (!getack())
return FALSE;
for (cnt = 0; cnt <= len; cnt++)
{
if (recv())
{
fprintf(stderr, "can't read byte %d\n", cnt);
return FALSE;
}
buf[cnt] = g_in;
}
fwrite(buf, 1, cnt, stdout);
return TRUE;
}
// write memory:
static int cmdwm(uint32_t addr32)
{
int cnt;
uint8_t addr[4];
uint8_t xsum;
uint8_t buf[0x100];
int len;
int tmp;
if (checkcmd(CMDWM))
return FALSE;
len = fread(buf, 1, 0x100, stdin);
while (1)
{
if (len < 1)
break;
fprintf(stderr, "sending WriteMemory(%08x,%x)\n", addr32, len);
send(CMDWM);
send(~CMDWM);
if (!getack())
{
fprintf(stderr, "CMDWM cmd failed\n");
return FALSE;
};
for (cnt = 0; cnt < 4; cnt++)
addr[cnt] = addr32 >> (cnt * 8);
//fprintf(stderr, "sending address\n");
xsum = 0;
for (cnt = 3; cnt >= 0; cnt--)
{
send(addr[cnt]);
xsum ^= addr[cnt];
}
send(xsum);
if (!getack())
{
fprintf(stderr, "CMDWM send(xsum) failed\n");
return FALSE;
};
xsum = 0;
len--;
send(len);
xsum ^= len;
len++;
for (cnt = 0; cnt < len; cnt++)
{
int c = buf[cnt];
send(c);
xsum ^= c;
}
send(xsum);
// waitning for ack, good moment to read new data
tmp = fread(buf, 1, 0x100, stdin);
if (!getack())
{
fprintf(stderr, "CMDWM failed\n");
return FALSE;
};
addr32 += len;
len = tmp;
}
return TRUE;
}
// erase:
static int cmder(void)
{
uint8_t len = 255;
int cmd = CMDER;
if (checkcmd(cmd))
{
cmd = CMDEER;
fprintf(stderr, "Using Extended Erase\n");
}
if (checkcmd(cmd))
return FALSE;
fprintf(stderr, "sending Erase(%d)\n", len);
send(cmd);
send(~cmd);
if (!getack())
return FALSE;
if (cmd == CMDER)
{
send(len);
send(~len);
}
else if (cmd == CMDEER)
{
send(0xff);
send(0xff);
send(0);
}
g_ackwait = 2000;
if (!getack())
return FALSE;
return TRUE;
}
// Go:
static int cmdgo(uint32_t addr32)
{
int cnt;
uint8_t addr[4];
uint8_t xsum;
if (checkcmd(CMDGO))
return FALSE;
fprintf(stderr, "sending Go(%08x)\n", addr32);
send(CMDGO);
send(~CMDGO);
if (!getack())
return FALSE;
for (cnt = 0; cnt < 4; cnt++)
addr[cnt] = addr32 >> (cnt * 8);
xsum = 0;
for (cnt = 3; cnt >= 0; cnt--)
{
send(addr[cnt]);
xsum ^= addr[cnt];
}
send(xsum);
if (!getack())
return FALSE;
fprintf(stderr, "ACK1 OK\n");
#if 0
if (!getack())
return FALSE;
fprintf(stderr, "ACK2 OK\n");
#endif
return TRUE;
}
static int cmdrdprot(int protect)
{
if (protect)
{
if (checkcmd(CMDRDP))
return FALSE;
fprintf(stderr, "sending RDP\n");
send(CMDRDP);
send((uint8_t) ~ CMDRDP);
}
else
{
if (checkcmd(CMDRDUNP))
return FALSE;
fprintf(stderr, "sending RDUNP\n");
send(CMDRDUNP);
send((uint8_t) ~ CMDRDUNP);
}
if (!getack())
return FALSE;
usleep(100000);
if (!getack())
return FALSE;
return TRUE;
}
static void devclose(void)
{
int status;
if (!g_fd)
return;
// clear RTS:
status = TIOCM_RTS;
ioctl(g_fd, TIOCMBIC, &status);
close(g_fd);
g_fd = 0;
}
static int init(void)
{
fprintf(stderr, "sending initial 0x7f\n");
send(0x7f);
g_ackwait = 40;
#if 0
// read pending input:
if (recv())
return FALSE;
if (!getack())
return FALSE;
#else
if (!getack())
return FALSE;
#endif
fprintf(stderr, "init OK\n");
usleep(40000);
return TRUE;
}
static int devopen(void)
{
int status;
struct termios options;
static int speeds[] = {
0,
50,
75,
110,
134,
150,
200,
300,
600,
1200,
1800,
2400,
4800,
9600,
19200,
38400,
57600,
115200,
230400,
460800,
500000,
576000,
921600,
1000000,
1152000,
1500000,
2000000,
2500000,
3000000,
3500000,
4000000
};
void chkin(void)
{
fd_set rfds;
struct timeval tv;
int tmp;
FD_ZERO(&rfds);
FD_SET(g_fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 20000;
tmp = select(1, &rfds, NULL, NULL, &tv);
if (tmp < 0)
{
perror("select");
return;
}
if (tmp)
{
fprintf(stderr, "input ready -> reading\n");
recv();
}
}
if (g_fd)
return 0;
if (!g_dev)
{
fprintf(stderr, "devopen: no device specified\n");
return 1;
}
//fcntl(g_fd, F_SETFL, 0);
if ((g_fd = open(g_dev, O_RDWR)) < 0)
{
// fprintf(stderr, "init: can't open %s\n", g_dev);
perror(g_dev);
exit(1);
return 1;
}
// set RTS:
status = TIOCM_RTS;
ioctl(g_fd, TIOCMBIS, &status);
usleep(40000);
/* get the current options */
tcgetattr(g_fd, &options);
// raw I/O:
options.c_cflag = (CLOCAL | CREAD | CS8);
options.c_cflag |= PARENB;
//options.c_cflag |= CRTSCTS;
options.c_lflag = 0;
options.c_oflag = 0;
options.c_iflag = 0;
options.c_iflag = INPCK;
//options.c_iflag |= IGNPAR;
// 0.1 second timeout:
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 1;
//options.c_cc[VTIME] = 10;
if (g_speed < 16)
cfsetspeed(&options, g_speed);
else
cfsetspeed(&options, 0x1000 + g_speed - 15);
g_speed = cfgetospeed(&options);
fprintf(stderr, "IO speed: ");
if (g_speed < 16)
fprintf(stderr, "%i\n", g_speed[speeds]);
else
fprintf(stderr, "%i\n", (g_speed - 0x1000 + 15)[speeds]);
/* set the options */
tcsetattr(g_fd, TCSANOW, &options);
// check input:
chkin();
atexit(devclose);
return 0;
}
static void help(char *name)
{
/* *INDENT-OFF* */
fprintf(stderr, "usage: %s <options>; numbers in hex:\n"
" -h\t\thelp\n"
" -d <devname>\topen device\n"
" -i\t\tinit data connection\n"
" -v\t\tget version\n"
" -r <addr,len>\tRead Memory to stdout\n"
" -e\t\tGlobal Erase\n"
" -w <addr>\tWrite Memory from stdin\n"
" -g <addr>\tJump to address\n"
" -s<speed>\n"
" -p<set>\tRead protection\n"
, name);
/* *INDENT-ON* */
exit(1);
}
int main(int argc, char *argv[])
{
//int cnt;
int tmp1, tmp2;
int phelp = TRUE;
memset(&g_cmds, 0, sizeof(g_cmds));
g_cmds.num = -1;
while (1)
{
int opt;
opt = getopt(argc, argv, "hd:ivr:ew:g:s:p:");
if (opt < 0)
break;
phelp = FALSE;
switch (opt)
{
case 'h':
help(argv[0]);
break;
case 'd':
g_dev = optarg;
break;
case 'i':
devopen();
if (!init())
goto exit;
cmdgid();
break;
case 'r':
if (sscanf(optarg, "%x,%x", &tmp1, &tmp2) != 2)
help(argv[0]);
while (tmp2 > 256)
{
cmdrm(tmp1, 255);
tmp1 += 256;
tmp2 -= 256;
}
if (tmp2 > 0)
cmdrm(tmp1, tmp2 - 1);
break;
case 'e':
cmder();
break;
case 'w':
if (sscanf(optarg, "%x", &tmp1) != 1)
help(argv[0]);
cmdwm(tmp1);
break;
case 'g':
if (sscanf(optarg, "%x", &tmp1) != 1)
help(argv[0]);
cmdgo(tmp1);
break;
case 's':
if (sscanf(optarg, "%d", &tmp1) != 1)
help(argv[0]);
g_speed = tmp1;
if (g_speed > 30)
g_speed = 30;
break;
case 'v':
if (!cmdgid())
return -1;
cmdgv();
break;
case 'p':
if (*optarg == '0')
cmdrdprot(0);
else
cmdrdprot(1);
break;
}
}
if (phelp)
help(argv[0]);
exit:
devclose();
return 0;
}