-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.cpp
732 lines (569 loc) · 19.6 KB
/
app.cpp
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
/*
* STM32Programming
*
* Copyright 2015 - 2020 yangfuyuan
*
*
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileInfo>
#include <QGroupBox>
//simple wait routine
void MainWindow::wait_ms(unsigned long time)
{
QWaitCondition wc;
QMutex mutex;
QMutexLocker locker(&mutex);
wc.wait(&mutex, time);
}
void MainWindow::userOperator(const QString &filename)
{
ui->progressbar->reset();
ui->progressbar->setMaximum(100);
elapsed_timer->start();
bootloader(filename);
m_state = IDLE;
elapsed_timer->stop();
setReadWriteButtonState(true);
}
void MainWindow::bootloader(const QString &filename)
{
if (filename.isEmpty())
{
emit logView("file is empty, please select file!!", CRITICAL);
return;
}
binary_t *p_st = NULL;
hex_t *h_st = NULL;
parser_err_t perr;
bool is_hex = false;
if (m_action == ACT_WRITE)
{
if (filename.contains(".hex"))
{
is_hex = true;
h_st = hex_init();
if (!h_st)
{
fprintf(stderr, "Raw HEX ERROR Parser failed to initialize\n");
emit logView("Raw HEX ERROR Parser failed to initialize", CRITICAL);
return;
}
perr = hex_open(h_st, filename.toStdString().c_str(), 0);
/* if still have an error, fail */
if (perr != PARSER_ERR_OK)
{
fprintf(stderr, "Raw BINARY ERROR: %s\n", parser_errstr(perr));
fflush(stderr);
emit logView(tr("Raw BINARY ERROR: %1").arg(QString::fromStdString(
parser_errstr(perr))), CRITICAL);
if (perr == PARSER_ERR_SYSTEM)
{
perror(filename.toStdString().c_str());
}
hex_close(h_st);
h_st = NULL;
return;
}
fprintf(stdout, "Using Parser : Raw BINARY\n");
}
}
if (!is_hex)
{
/* now try binary */
p_st = binary_init();
if (!p_st)
{
emit logView("Raw BINARY ERROR Parser failed to initialize", CRITICAL);
return;
}
perr = binary_open(p_st, filename.toStdString().c_str(),
m_action == ACT_WRITE ? 0 : 1);
/* if still have an error, fail */
if (perr != PARSER_ERR_OK)
{
fprintf(stderr, "Raw BINARY ERROR: %s\n", parser_errstr(perr));
fflush(stderr);
emit logView(tr("Raw BINARY ERROR: %1").arg(QString::fromStdString(
parser_errstr(
perr))), CRITICAL);
if (perr == PARSER_ERR_SYSTEM)
{
perror(filename.toStdString().c_str());
}
binary_close(p_st);
p_st = NULL;
return;
}
fprintf(stdout, "Using Parser : Raw BINARY\n");
}
emit logView("Using Parser : Raw BINARY", INFO);
uint8_t buffer[256];
uint32_t addr, start, end;
unsigned int len;
int failed = 0;
int first_page, num_pages;
int ret = 1;
stm32_err_t s_err;
/*
* Cleanup addresses:
*
* Starting from options
* start_addr, readwrite_len, spage, npages
* and using device memory size, compute
* start, end, first_page, num_pages
*/
if (start_addr || readwrite_len)
{
start = start_addr;
if (is_addr_in_flash(stm, start))
{
end = stm.dev->fl_end;
}
else
{
no_erase = 1;
if (is_addr_in_ram(stm, start))
{
end = stm.dev->ram_end;
}
else
{
end = start + sizeof(uint32_t);
}
}
if (readwrite_len && (end > start + readwrite_len))
{
end = start + readwrite_len;
}
first_page = flash_addr_to_page_floor(stm, start);
if (!first_page && end == stm.dev->fl_end)
{
num_pages = STM32_MASS_ERASE;
}
else
{
num_pages = flash_addr_to_page_ceil(stm, end) - first_page;
}
}
else if (!spage && !npages)
{
start = stm.dev->fl_start;
end = stm.dev->fl_end;
first_page = 0;
num_pages = STM32_MASS_ERASE;
}
else
{
first_page = spage;
start = flash_page_to_addr(stm, first_page);
if (start > stm.dev->fl_end)
{
fprintf(stderr, "Address range exceeds flash size.\n");
emit logView("Address range exceeds flash size.", WARNING);
if (p_st)
{
binary_close(p_st);
p_st = NULL;
}
if (h_st)
{
hex_close(h_st);
h_st = NULL;
}
return;
}
if (npages)
{
num_pages = npages;
end = flash_page_to_addr(stm, first_page + num_pages);
if (end > stm.dev->fl_end)
{
end = stm.dev->fl_end;
}
}
else
{
end = stm.dev->fl_end;
num_pages = flash_addr_to_page_ceil(stm, end) - first_page;
}
if (!first_page && end == stm.dev->fl_end)
{
num_pages = STM32_MASS_ERASE;
}
}
switch (m_action)
{
case ACT_NONE:
break;
case ACT_READ:
{
unsigned int max_len = STM32_MAX_RX_FRAME;
fprintf(stdout, "Memory read\n");
emit logView("Memory read.", INFO);
fflush(stdout);
addr = start;
while (addr < end)
{
uint32_t left = end - addr;
len = max_len > left ? left : max_len;
s_err = STM32BootLoader::singleton()->stm32_read_memory(stm, addr, buffer, len);
if (s_err != STM32_ERR_OK)
{
emit logView(
tr("Failed to read memory at address 0x%1, target write-protected?").arg(
QString::number(addr, 16)), CRITICAL);
fprintf(stderr,
"Failed to read memory at address 0x%08x, target write-protected?\n", addr);
fflush(stderr);
if (p_st)
{
binary_close(p_st);
p_st = NULL;
}
return;
}
if (binary_write(p_st, buffer, len) != PARSER_ERR_OK)
{
emit logView("Raw BINARY ERROR Failed to write data to file", CRITICAL);
fprintf(stderr, "Failed to write data to file\n");
if (p_st)
{
binary_close(p_st);
p_st = NULL;
}
m_state = IDLE;
return;
}
addr += len;
emit dataProcessSlot((100.0f / (float)(end - start)) * (float)(
addr - start), 100);
emit logView(tr("Read address 0x%1 (%2%)").arg(QString::number(addr,
16)).arg((100.0f / (float)(end - start)) * (float)(addr - start)), INFO);
fprintf(stdout,
"\rRead address 0x%08x (%.2f%%) ",
addr,
(100.0f / (float)(end - start)) * (float)(addr - start)
);
fflush(stdout);
}
ret = 0;
fprintf(stdout, "Done.\n");
emit logView("read memory Done", INFO);
}
break;
case ACT_WRITE:
{
fprintf(stdout, "Write to memory\n");
emit logView("Write to memory", INFO);
off_t offset = 0;
ssize_t r;
unsigned int size;
unsigned int max_wlen, max_rlen;
max_wlen = STM32_MAX_TX_FRAME - 2; /* skip len and crc */
max_wlen &= ~3; /* 32 bit aligned */
max_rlen = STM32_MAX_RX_FRAME;
max_rlen = max_rlen < max_wlen ? max_rlen : max_wlen;
/* Assume data from stdin is whole device */
if (filename.toStdString().c_str()[0] == '-' &&
filename.toStdString().c_str()[1] == '\0')
{
size = end - start;
}
else
{
if (p_st)
{
size = binary_size(p_st);
}
else if (h_st)
{
size = hex_size(h_st);
}
}
// TODO: It is possible to write to non-page boundaries, by reading out flash
// from partial pages and combining with the input data
// if ((start % stm->dev->fl_ps[i]) != 0 || (end % stm->dev->fl_ps[i]) != 0) {
// fprintf(stderr, "Specified start & length are invalid (must be page aligned)\n");
// goto close;
// }
// TODO: If writes are not page aligned, we should probably read out existing flash
// contents first, so it can be preserved and combined with new data
if (!no_erase && num_pages)
{
fprintf(stdout, "Erasing memory\n");
s_err = STM32BootLoader::singleton()->stm32_erase_memory(stm, first_page,
num_pages);
if (s_err != STM32_ERR_OK)
{
emit logView("Failed to erase memory", CRITICAL);
fprintf(stderr, "Failed to erase memory\n");
fflush(stderr);
m_state = IDLE;
return;
}
}
fflush(stdout);
addr = start;
while (addr < end && offset < size)
{
uint32_t left = end - addr;
len = max_wlen > left ? left : max_wlen;
len = len > size - offset ? size - offset : len;
if (p_st && binary_read(p_st, buffer, &len) != PARSER_ERR_OK)
{
emit logView("Failed to read file buffer", CRITICAL);
if (p_st)
{
binary_close(p_st);
p_st = NULL;
}
m_state = IDLE;
return;
}
if (h_st && hex_read(h_st, buffer, &len) != PARSER_ERR_OK)
{
emit logView("Failed to read file buffer", CRITICAL);
if (h_st)
{
hex_close(h_st);
h_st = NULL;
}
m_state = IDLE;
return;
}
if (len == 0)
{
if (filename.toStdString().c_str()[0] == '-')
{
return;
}
else
{
emit logView("Raw BINARY ERROR Failed to read input file", CRITICAL);
fprintf(stderr, "Failed to read input file\n");
fflush(stderr);
if (p_st)
{
binary_close(p_st);
p_st = NULL;
}
if (h_st)
{
hex_close(h_st);
h_st = NULL;
}
m_state = IDLE;
return;
}
}
again:
s_err = STM32BootLoader::singleton()->stm32_write_memory(stm, addr, buffer,
len);
if (s_err != STM32_ERR_OK)
{
emit logView(tr("Failed to write memory at address 0x%1").arg(QString::number(
addr, 16)), CRITICAL);
fprintf(stderr, "Failed to write memory at address 0x%08x\n", addr);
fflush(stderr);
if (p_st)
{
binary_close(p_st);
p_st = NULL;
}
if (h_st)
{
hex_close(h_st);
h_st = NULL;
}
m_state = IDLE;
return;
}
if (verify)
{
uint8_t compare[len];
unsigned int offset, rlen;
offset = 0;
while (offset < len)
{
rlen = len - offset;
rlen = rlen < max_rlen ? rlen : max_rlen;
s_err = STM32BootLoader::singleton()->stm32_read_memory(stm, addr + offset,
compare + offset, rlen);
if (s_err != STM32_ERR_OK)
{
fprintf(stderr, "Failed to read memory at address 0x%08x\n", addr + offset);
emit logView(tr("Failed to write memory at address 0x%1").arg(QString::number(
addr + offset, 16)), CRITICAL);
m_state = IDLE;
return;
}
offset += rlen;
}
for (r = 0; r < len; ++r)
{
if (buffer[r] != compare[r])
{
if (failed == retry)
{
emit logView(
tr("Failed to verify at address 0x%1, expected 0x%2 and found 0x%3").arg(
QString::number((uint32_t)(addr + r),
16)).arg(QString::number(buffer[r])).arg(QString::number(compare[r])),
CRITICAL);
fprintf(stderr,
"Failed to verify at address 0x%08x, expected 0x%02x and found 0x%02x\n",
(uint32_t)(addr + r), buffer[r], compare[r]);
fflush(stderr);
return;
}
++failed;
goto again;
}
}
failed = 0;
}
addr += len;
offset += len;
emit dataProcessSignal((100.0f / size) * offset, 100);
emit logView(tr("Wrote %1address 0x%2 (%3%)").arg(verify ? "and verified " :
"").arg(QString::number(addr, 16)).arg((100.0f / size) * offset), INFO);
fprintf(stdout,
"\rWrote %saddress 0x%08x (%.2f%%) ",
verify ? "and verified " : "",
addr,
(100.0f / size) * offset
);
fflush(stdout);
}
fprintf(stdout, "Done.\n");
ret = 0;
emit logView("Write Memery Done", INFO);
}
break;
case ACT_WRITE_UNPROTECT:
{
fprintf(stdout, "Write-unprotecting flash\n");
emit logView("Write-unprotecting flash", INFO);
/* the device automatically performs a reset after the sending the ACK */
reset_flag = 0;
STM32BootLoader::singleton()->stm32_wunprot_memory(stm);
fprintf(stdout, "Done.\n");
emit logView("Write-unprotecting Done", INFO);
ret = 0;
}
break;
case ACT_READ_PROTECT:
{
fprintf(stdout, "Read-Protecting flash\n");
emit logView("Read-Protecting flash", INFO);
/* the device automatically performs a reset after the sending the ACK */
reset_flag = 0;
STM32BootLoader::singleton()->stm32_readprot_memory(stm);
fprintf(stdout, "Done.\n");
emit logView("Read-Protecting Done", INFO);
}
break;
case ACT_READ_UNPROTECT:
{
fprintf(stdout, "Read-UnProtecting flash\n");
emit logView("Read-UnProtecting flash", INFO);
/* the device automatically performs a reset after the sending the ACK */
reset_flag = 0;
STM32BootLoader::singleton()->stm32_runprot_memory(stm);
fprintf(stdout, "Done.\n");
emit logView("Read-UnProtecting Done", INFO);
}
break;
case ACT_ERASE_ONLY:
{
fprintf(stdout, "Erasing flash\n");
emit logView("Erasing flash", INFO);
if (num_pages != STM32_MASS_ERASE &&
(start != flash_page_to_addr(stm, first_page) ||
end != flash_page_to_addr(stm, first_page + num_pages)))
{
fprintf(stderr,
"Specified start & length are invalid (must be page aligned)\n");
emit logView("Specified start & length are invalid (must be page aligned)",
CRITICAL);
break;
}
s_err = STM32BootLoader::singleton()->stm32_erase_memory(stm, first_page,
num_pages);
if (s_err != STM32_ERR_OK)
{
fprintf(stderr, "Failed to erase memory\n");
emit logView("Failed to erase memory", CRITICAL);
break;
}
ret = 0;
}
break;
case ACT_CRC:
{
uint32_t crc_val = 0;
fprintf(stdout, "CRC computation\n");
emit logView("CRC computation", INFO);
s_err = STM32BootLoader::singleton()->stm32_crc_wrapper(stm, start, end - start,
&crc_val);
if (s_err != STM32_ERR_OK)
{
emit logView("Failed to read CRC", CRITICAL);
fprintf(stderr, "Failed to read CRC\n");
fflush(stderr);
break;
}
ret = 0;
fprintf(stdout, "CRC(0x%08x-0x%08x) = 0x%08x\n", start, end, crc_val);
emit logView(tr("CRC(0x%1-0x%2) = 0x%3").arg(QString::number(start,
16)).arg(QString::number(end, 16)).arg(QString::number(crc_val, 16)), INFO);
}
break;
default:
break;
}
if (exec_flag && ret == 0)
{
if (execute == 0)
{
execute = stm.dev->fl_start;
}
emit logView(tr("Starting execution at address 0x%1...").arg(QString::number(
execute, 16)), INFO);
fprintf(stdout, "\nStarting execution at address 0x%08x... ", execute);
fflush(stdout);
if (STM32BootLoader::singleton()->stm32_go(stm, execute) == STM32_ERR_OK)
{
reset_flag = 0;
fprintf(stdout, "starting execution done.\n");
fflush(stdout);
emit logView("starting execution Done", INFO);
wait_ms(2000);
if (STM32BootLoader::singleton())
{
STM32BootLoader::singleton()->disconnect();
emit logView(QString("disconnected"), INFO);
}
emit taskFininshedSignal();
}
else
{
fprintf(stdout, "starting execution failed.\n");
emit logView("starting execution failed", CRITICAL);
}
fflush(stdout);
}
if (p_st)
{
binary_close(p_st);
p_st = NULL;
}
if (h_st)
{
hex_close(h_st);
h_st = NULL;
}
m_state = IDLE;
}