-
Notifications
You must be signed in to change notification settings - Fork 1
/
flasher.cpp
executable file
·1123 lines (923 loc) · 37 KB
/
flasher.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
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
*
* Copyright (c) 2021 IMProject Development Team. All rights reserved.
* Authors: Igor Misic <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name IMProject nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include "flasher.h"
#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QJsonDocument>
#include <QMessageBox>
#include "crc32.h"
#include "socket_client.h"
#include "file_downloader.h"
#include "worker.h"
namespace flasher {
namespace {
constexpr qint64 kSignatureSize {64};
constexpr int kEraseTimeoutInMs {5000};
constexpr qint64 kPacketSize {256};
constexpr qint64 kSecurePacketSize {296};
constexpr unsigned long kThreadSleepTimeInMs {100U};
constexpr int kSerialTimeoutInMs {100};
constexpr int kCollectDataTimeoutInMs {300};
constexpr int kCrc32Size {4};
constexpr int kBoardIdSize {32};
constexpr int kTryToConnectTimeoutInMs {20000};
constexpr int kTryToDownloadFileTimeoutInMs {5000};
// Commands
constexpr char kVerifyFlasherCmd[] = "IMFlasher_Verify";
constexpr char kEraseCmd[] = "erase";
constexpr char kVersionCmd[] = "version";
constexpr char kSoftwareInfoJsonCmd[] = "software_info_json";
constexpr char kSecurityJsonCmd[] = "security_json";
constexpr char kBoardIdCmd[] = "board_id";
constexpr char kBoardInfoJsonCmd[] = "board_info_json";
constexpr char kFlashFwCmd[] = "flash_fw";
constexpr char kEnterBlCmd[] = "enter_bl";
constexpr char kIsFwProtectedCmd[] = "is_fw_protected";
constexpr char kEnableFwProtectionCmd[] = "enable_fw_protection";
constexpr char kDisableFwProtectionCmd[] = "disable_fw_protection";
constexpr char kExitBlCmd[] = "exit_bl";
constexpr char kCheckSignatureCmd[] = "check_signature";
constexpr char kDisconnectCmd[] = "disconnect";
constexpr char kFakeBoardIdBase64[] = "Tk9UX1NFQ1VSRURfTUFHSUNfU1RSSU5HXzEyMzQ1Njc="; // NOT_SECURED_MAGIC_STRING_1234567
// Config
constexpr char kConfigFileName[] = "config.json";
constexpr uint32_t kConfigOpenAttempt = 2;
constexpr char kConfigVersionStr[] = "config_version";
constexpr char kEnableSignatureWarningStr[] = "enable_signature_warning";
// Servers default config
constexpr char kDefaultServerAddress1[] = "server1.imtech.hr";
constexpr char kDefaultServerAddress2[] = "server2.imtech.hr";
constexpr char kDefaultServerAddress3[] = "server3.imtech.hr";
constexpr int kDefaultPort = 5322;
constexpr char kDefaultKey[] = "NDQ4N2Y1YjFhZTg3ZGI3MTA1MjlhYmM3";
uint32_t Deserialize32(const uint8_t *buf) {
uint32_t result;
result = static_cast<uint32_t>(buf[0] << 24U);
result |= static_cast<uint32_t>(buf[1] << 16U);
result |= static_cast<uint32_t>(buf[2] << 8U);
result |= static_cast<uint32_t>(buf[3] << 0U);
return result;
}
bool ShowInfoMsg(const QString& title, const QString& description) {
QMessageBox msg_box;
msg_box.setText(title);
msg_box.setInformativeText(description);
msg_box.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
return (msg_box.exec() == QMessageBox::Ok);
}
} // namespace
Flasher::Flasher() = default;
Flasher::~Flasher() {
worker_thread_.quit();
worker_thread_.wait();
}
void Flasher::Init() {
QJsonDocument json_document;
if (OpenConfigFile(json_document)) {
QJsonArray servers_array = json_document.object().find("servers")->toArray();
socket_client_ = std::make_shared<socket::SocketClient>(std::move(servers_array));
if (0 == QString::compare("true", json_document.object().find(kEnableSignatureWarningStr)->toString(), Qt::CaseInsensitive)) {
is_signature_warning_enabled_ = true;
}
}
file_downloader_ = std::make_unique<file_downloader::FileDownloader>();
connect(file_downloader_.get(), &file_downloader::FileDownloader::Downloaded, this, &Flasher::FileDownloaded);
connect(file_downloader_.get(), &file_downloader::FileDownloader::DownloadProgress, this, &Flasher::DownloadProgress);
connect(socket_client_.get(), &socket::SocketClient::DownloadProgress, this, &Flasher::DownloadProgress);
connect(&serial_port_, &QSerialPort::errorOccurred, this, &Flasher::HandleSerialPortError);
Worker *worker = new Worker;
worker->moveToThread(&worker_thread_);
connect(&worker_thread_, &QThread::finished, worker, &QObject::deleteLater);
connect(this, &Flasher::RunLoop, worker, &Worker::DoWork);
connect(worker, &Worker::FlasherLoop, this, &Flasher::LoopHandler);
worker_thread_.start();
emit RunLoop();
}
void Flasher::FileDownloaded() {
is_download_success_ = file_downloader_->GetDownloadedData(file_content_);
is_file_downloaded_ = true;
}
void Flasher::UpdateProgressBar(const quint64& sent_size, const quint64& total_size) {
int progress_percentage = 0;
if (total_size != 0) {
progress_percentage = (100 * sent_size) / total_size;
}
if (last_progress_percentage_ != progress_percentage) {
last_progress_percentage_ = progress_percentage;
qInfo() << sent_size << "/" << total_size << "B, " << progress_percentage << "%";
emit UpdateProgressBarSignal(progress_percentage);
}
}
void Flasher::DownloadProgress(const qint64& bytes_received, const qint64& bytes_total) {
UpdateProgressBar(bytes_received, bytes_total);
}
void Flasher::LoopHandler() {
switch (state_) {
case FlasherStates::kIdle:
// Idle state
break;
case FlasherStates::kTryToConnect:
TryToConnect();
break;
case FlasherStates::kConnected:
emit ClearTextInBrowser();
emit ShowStatusMsg("Connected");
if (serial_port_.isOpen()) {
emit SetButtons(is_bootloader_);
if (is_bootloader_) {
GetVersionJson(bl_sw_info);
if (bl_sw_info.empty()) {
GetVersion();
} else {
if ("secured" == bl_sw_info.value("build_variant").toString()) {
emit DisableBrowseFileButton();
is_secure_bootloader_ = true;
} else {
is_secure_bootloader_ = false;
}
}
SetState(FlasherStates::kCheckBoardInfo);
} else {
GetVersionJson(fw_sw_info);
if (fw_sw_info.empty()) {
GetVersion();
}
SetState(FlasherStates::kIdle);
}
}
break;
case FlasherStates::kDisconnected: {
bool is_disconnected_success = true;
is_timer_started_ = false;
if (serial_port_.isOpen()) {
if (is_bootloader_) {
qInfo() << "Send disconnect command";
is_disconnected_success = SendMessage(kDisconnectCmd, sizeof(kDisconnectCmd), kSerialTimeoutInMs);
}
serial_port_.CloseConn();
}
if (is_disconnected_success) {
emit ShowStatusMsg("Disconnected");
} else {
emit ShowStatusMsg("Unplug board!");
}
SetState(FlasherStates::kIdle);
break;
}
case FlasherStates::kCheckBoardInfo:
if (CollectBoardInfo() || CollectBoardId()) {
emit ShowTextInBrowser("Board ID: " + board_id_);
is_read_protection_enabled_ = IsFirmwareProtected();
emit SetReadProtectionButtonText(is_read_protection_enabled_);
if (0 == QString::compare(kFakeBoardIdBase64, board_id_, Qt::CaseInsensitive)) {
SetState(FlasherStates::kIdle);
} else {
SetState(FlasherStates::kServerDataExchange);
}
} else {
emit ShowTextInBrowser("Board ID Error. Unplug your board, press disconnect/connect, and plug your board again.");
SetState(FlasherStates::kError);
}
break;
case FlasherStates::kServerDataExchange:
if (!board_info_.empty() && socket_client_) {
if (socket_client_->SendBoardInfo(board_info_, bl_sw_info, fw_sw_info)) {
if (socket_client_->ReceiveProductInfo(board_info_, bl_sw_info, product_info_, is_secure_communication_)) {
if (!product_info_.empty()) {
emit SetFileVersionsList(product_info_);
}
}
if (is_secure_communication_ != is_secure_bootloader_) {
emit ShowTextInBrowser("Bootloader variant is incompatible with the server!");
}
}
}
emit ClearStatusMsg();
SetState(FlasherStates::kIdle);
break;
case FlasherStates::kBrowseFile: {
QString file_path = QFileDialog::getOpenFileName(nullptr,
tr("File binary"),
"",
tr("Binary (*.bin);;All Files (*)"));
if (!file_path.isEmpty()) {
if (OpenFile(file_path)) {
emit ShowTextInBrowser("File: " + file_path);
emit EnableLoadButton();
}
}
SetState(FlasherStates::kIdle);
break;
}
case FlasherStates::kLoadFile: {
packet_size_ = kPacketSize;
if (SetLocalFileContent()) {
// Local file
SetState(FlasherStates::kCheckSignature);
} else {
if (file_source_ == "url") {
DownloadFileFromUrl();
emit ShowStatusMsg("Downloading");
timer_.start();
SetState(FlasherStates::kDownloadFileFromUrl);
} else if (file_source_ == "server") {
// Load file from server
emit ShowStatusMsg("Downloading");
CollectSecurityDataFromBoard();
if (socket_client_->DownloadFile(board_info_, client_security_data_, selected_file_version_, server_security_data_, file_content_)) {
if (file_content_.isEmpty()) {
emit ClearProgress();
emit ShowStatusMsg("Download file error");
SetState(FlasherStates::kIdle);
} else {
if (client_security_data_.empty() || server_security_data_.empty()) {
SetState(FlasherStates::kCheckSignature);
} else {
packet_size_ = kSecurePacketSize;
SetState(FlasherStates::kSendServerSecurityData);
emit ShowTextInBrowser("Secure connection detected!");
}
}
} else {
emit ClearProgress();
emit ShowStatusMsg("Download server error");
SetState(FlasherStates::kIdle);
}
} else {
// Unknown source, go to idle
SetState(FlasherStates::kIdle);
}
}
break;
}
case FlasherStates::kDownloadFileFromUrl: {
if (is_file_downloaded_) {
is_file_downloaded_ = false;
if (!is_download_success_ || file_content_.isEmpty()) {
emit ShowStatusMsg("Download error");
SetState(FlasherStates::kIdle);
} else {
SetState(FlasherStates::kCheckSignature);
}
} else {
if (timer_.hasExpired(kTryToDownloadFileTimeoutInMs)) {
emit ShowStatusMsg("Download timeout");
SetState(FlasherStates::kIdle);
}
}
break;
}
case FlasherStates::kSendServerSecurityData: {
FlashingInfo flashing_info = SendServerSecurityData();
if (flashing_info.success) {
SetState(FlasherStates::kCheckSignature);
} else {
emit ClearStatusMsg();
ShowInfoMsg(flashing_info.title, flashing_info.description);
SetState(FlasherStates::kIdle);
}
break;
}
case FlasherStates::kCheckSignature: {
emit ShowStatusMsg("Flashing");
FlashingInfo flashing_info = CheckSignature();
if (flashing_info.success) {
SetState(FlasherStates::kSendSignature);
} else {
emit ClearStatusMsg();
ShowInfoMsg(flashing_info.title, flashing_info.description);
emit ClearProgress();
SetState(FlasherStates::kIdle);
}
break;
}
case FlasherStates::kSendSignature: {
FlashingInfo flashing_info = SendSignature();
if (flashing_info.success) {
SetState(FlasherStates::kVerifyFlasher);
} else {
emit ClearStatusMsg();
ShowInfoMsg(flashing_info.title, flashing_info.description);
emit ClearProgress();
SetState(FlasherStates::kIdle);
}
break;
}
case FlasherStates::kVerifyFlasher: {
FlashingInfo flashing_info = VerifyFlasher();
if (flashing_info.success) {
SetState(FlasherStates::kSendFileSize);
} else {
emit ClearStatusMsg();
ShowInfoMsg(flashing_info.title, flashing_info.description);
emit ClearProgress();
SetState(FlasherStates::kIdle);
}
break;
}
case FlasherStates::kSendFileSize: {
FlashingInfo flashing_info = SendFileSize();
if (flashing_info.success) {
SetState(FlasherStates::kErase);
} else {
emit ClearStatusMsg();
ShowInfoMsg(flashing_info.title, flashing_info.description);
emit ClearProgress();
SetState(FlasherStates::kIdle);
}
break;
}
case FlasherStates::kErase: {
FlashingInfo flashing_info = Erase();
if (flashing_info.success) {
SetState(FlasherStates::kFlash);
} else {
emit ClearStatusMsg();
ShowInfoMsg(flashing_info.title, flashing_info.description);
emit ClearProgress();
SetState(FlasherStates::kIdle);
}
break;
}
case FlasherStates::kFlash: {
FlashingInfo flashing_info = Flash();
if (flashing_info.success) {
SetState(FlasherStates::kCheckCrc);
} else {
emit ClearStatusMsg();
ShowInfoMsg(flashing_info.title, flashing_info.description);
emit ClearProgress();
SetState(FlasherStates::kIdle);
}
break;
}
case FlasherStates::kCheckCrc: {
FlashingInfo flashing_info = CrcCheck();
ShowInfoMsg(flashing_info.title, flashing_info.description);
emit ClearProgress();
if (flashing_info.success) {
SetState(FlasherStates::kTryToConnect);
} else {
emit ClearStatusMsg();
SetState(FlasherStates::kIdle);
}
break;
}
case FlasherStates::kEnterBootloader:
if (!SendEnterBootloaderCommand()) {
SendFlashCommand();
}
is_bootloader_expected_ = true;
serial_port_.CloseConn();
SetState(FlasherStates::kEnteringBootloader);
break;
case FlasherStates::kEnteringBootloader:
emit ShowStatusMsg("Entering bootloader...");
ReconnectingToBoard();
break;
case FlasherStates::kExitBootloader:
qInfo() << "Send exit bootloader command";
if (SendMessage(kExitBlCmd, sizeof(kExitBlCmd), kSerialTimeoutInMs)) {
is_bootloader_expected_ = false;
serial_port_.CloseConn();
SetState(FlasherStates::kExitingBootloader);
} else {
SetState(FlasherStates::kError);
}
break;
case FlasherStates::kExitingBootloader:
emit ShowStatusMsg("Exiting bootloader...");
ReconnectingToBoard();
break;
case FlasherStates::kReconnect:
emit DisableAllButtons();
serial_port_.CloseConn();
if (!serial_port_.isOpen()) {
SetState(FlasherStates::kTryToConnect);
}
break;
case FlasherStates::kEnableReadProtection:
qInfo() << "Send enable firmware protected command";
if (SendMessage(kEnableFwProtectionCmd, sizeof(kEnableFwProtectionCmd), kSerialTimeoutInMs)) {
ShowInfoMsg("Enable readout protection", "Powercyle the board!");
SetState(FlasherStates::kReconnect);
} else {
SetState(FlasherStates::kError);
}
break;
case FlasherStates::kDisableReadProtection:
qInfo() << "Send disable firmware protected command";
if (ShowInfoMsg("Disable read protection", "Once disabled, complete flash will be erased including bootloader!")) {
if (SendMessage(kDisableFwProtectionCmd, sizeof(kDisableFwProtectionCmd), kSerialTimeoutInMs)) {
SetState(FlasherStates::kIdle);
} else {
SetState(FlasherStates::kError);
}
} else {
SetState(FlasherStates::kIdle);
}
break;
case FlasherStates::kError:
emit ShowStatusMsg("Error");
emit DisableAllButtons();
break;
default:
qInfo() << "Unspecified state";
break;
}
worker_thread_.msleep(kThreadSleepTimeInMs);
emit RunLoop();
}
FlashingInfo Flasher::Flash() {
FlashingInfo flashing_info;
const qint64 file_size = file_content_.size() - signature_size_;
const qint64 num_of_packets = (file_size / packet_size_);
const char *data_file = file_content_.data() + signature_size_;
// Send file in packages
for (qint64 packet = 0; packet < num_of_packets; ++packet) {
const char *data_position = data_file + (packet * packet_size_);
UpdateProgressBar((packet + 1U) * packet_size_, file_size);
flashing_info.success = SendMessage(data_position, packet_size_, kSerialTimeoutInMs);
if (!flashing_info.success) {
flashing_info.title = "Flashing process failed";
flashing_info.description = "Problem with flashing";
break;
}
}
if (flashing_info.success) {
const qint64 rest_size = file_size % packet_size_;
if (rest_size > 0) {
UpdateProgressBar(num_of_packets * packet_size_ + rest_size, file_size);
flashing_info.success = SendMessage(data_file + (num_of_packets * packet_size_), rest_size, kSerialTimeoutInMs);
if (!flashing_info.success) {
flashing_info.title = "Flashing process failed";
flashing_info.description = "Problem with flashing";
}
}
}
return flashing_info;
}
FlashingInfo Flasher::CheckSignature() {
FlashingInfo flashing_info;
flashing_info.success = SendMessage(kCheckSignatureCmd, sizeof(kCheckSignatureCmd), kSerialTimeoutInMs);
if (!flashing_info.success) {
flashing_info.title = "Flashing process failed";
flashing_info.description = "Check signature problem";
}
return flashing_info;
}
bool Flasher::CheckAck() {
bool success = false;
QByteArray data;
serial_port_.ReadData(data);
if (data.size() >= 2) {
if (0 == QString::compare("OK", data, Qt::CaseInsensitive)) {
qInfo() << "ACK";
success = true;
} else if (0 == QString::compare("NOK", data, Qt::CaseInsensitive)) {
qInfo() << "NOK ACK";
} else {
qInfo() << "ERROR or TIMEOUT";
}
} else {
qInfo() << "NO ACK";
}
return success;
}
bool Flasher::CheckTrue() {
//TODO: better handling needed. For error false is returned
bool success = false;
QByteArray data;
serial_port_.ReadData(data);
if (0 == QString::compare("TRUE", data, Qt::CaseInsensitive)) {
qInfo() << "TRUE";
success = true;
} else if (0 == QString::compare("FALSE", data, Qt::CaseInsensitive)) {
qInfo() << "FALSE";
} else {
qInfo() << "ERROR or TIMEOUT";
}
return success;
}
FlashingInfo Flasher::CrcCheck() {
FlashingInfo flashing_info;
const qint64 file_size = file_content_.size() - signature_size_;
const char *data_file = file_content_.data() + signature_size_;
const uint8_t *data = reinterpret_cast<const uint8_t *>(data_file);
uint32_t crc = crc::CalculateCrc32(data, file_size, false, false);
QByteArray crc_data;
crc_data.setNum(crc);
flashing_info.success = SendMessage(crc_data.data(), crc_data.size(), kSerialTimeoutInMs);
if (flashing_info.success) {
flashing_info.title = "Flashing process done";
flashing_info.description = "Successful flashing process";
} else {
flashing_info.title = "Flashing process failed";
flashing_info.description = "CRC problem";
}
file_content_.clear();
return flashing_info;
}
bool Flasher::CollectBoardId() {
bool success = false;
QByteArray out_data;
if (ReadMessageWithCrc(kBoardIdCmd, sizeof(kBoardIdCmd), kCollectDataTimeoutInMs, out_data)) {
if (out_data.size() == kBoardIdSize) {
board_id_ = out_data.toBase64();
qInfo() << "Board ID: " << board_id_;
success = true;
}
}
if (!success) {
qInfo() << "Board id error";
}
return success;
}
bool Flasher::CollectBoardInfo() {
bool success = false;
QByteArray out_data;
if (ReadMessageWithCrc(kBoardInfoJsonCmd, sizeof(kBoardInfoJsonCmd), kCollectDataTimeoutInMs, out_data)) {
QJsonDocument json_document = QJsonDocument::fromJson(QString(out_data).toUtf8());
board_info_ = json_document.object();
if (!board_info_.empty()) {
board_id_ = board_info_.value("board_id").toString();
qInfo() << "Board ID: " << board_id_;
qInfo() << "manufacturer ID: " << board_info_.value("manufacturer_id").toString();
success = true;
}
}
if (!success) {
qInfo() << "Board info error";
}
return success;
}
bool Flasher::CollectSecurityDataFromBoard() {
bool success = false;
QByteArray out_data;
if (ReadMessageWithCrc(kSecurityJsonCmd, sizeof(kSecurityJsonCmd), kCollectDataTimeoutInMs, out_data)) {
QJsonDocument json_document = QJsonDocument::fromJson(QString(out_data).toUtf8());
client_security_data_ = json_document.object();
if (!client_security_data_.empty()) {
success = true;
}
}
if (!success) {
qInfo() << "No security data from bootloader";
}
return success;
}
FlashingInfo Flasher::ConsoleFlash() {
FlashingInfo flashing_info = CheckSignature();
if (!flashing_info.success) {
return flashing_info;
}
flashing_info = SendSignature();
if (!flashing_info.success) {
return flashing_info;
}
flashing_info = VerifyFlasher();
if (!flashing_info.success) {
return flashing_info;
}
flashing_info = SendFileSize();
if (!flashing_info.success) {
return flashing_info;
}
flashing_info = Erase();
if (!flashing_info.success) {
return flashing_info;
}
flashing_info = Flash();
if (!flashing_info.success) {
return flashing_info;
}
flashing_info = CrcCheck();
return flashing_info;
}
FlashingInfo Flasher::Erase() {
FlashingInfo flashing_info;
flashing_info.success = SendMessage(kEraseCmd, sizeof(kEraseCmd), kEraseTimeoutInMs);
if (!flashing_info.success) {
flashing_info.title = "Flashing process failed";
flashing_info.description = "Erasing problem";
}
return flashing_info;
}
void Flasher::GetVersion() {
serial_port_.write(kVersionCmd, sizeof(kVersionCmd));
serial_port_.WaitForReadyRead(kSerialTimeoutInMs);
QByteArray data;
serial_port_.ReadData(data);
emit ShowTextInBrowser(data);
}
bool Flasher::GetVersionJson(QJsonObject& out_json_object) {
bool success = false;
QByteArray out_data;
if (ReadMessageWithCrc(kSoftwareInfoJsonCmd, sizeof(kSoftwareInfoJsonCmd), kSerialTimeoutInMs, out_data)) {
QJsonDocument out_json_document = QJsonDocument::fromJson(QString(out_data).toUtf8());
out_json_object = out_json_document.object();
QString software_info = "Git branch: ";
software_info.append(out_json_object.value("git_branch").toString());
software_info.append("\nGit hash: ");
software_info.append(out_json_object.value("git_hash").toString());
software_info.append("\nGit tag: ");
software_info.append(out_json_object.value("git_tag").toString());
software_info.append("\nRunning from: ");
software_info.append(out_json_object.value("ld_script_variant").toString());
software_info.append("\nBuild variant: ");
software_info.append(out_json_object.value("build_variant").toString());
emit ShowTextInBrowser(software_info);
success = true;
}
return success;
}
void Flasher::HandleSerialPortError(QSerialPort::SerialPortError error) {
if (error == QSerialPort::ResourceError) {
qInfo() << "Serial port error";
serial_port_.CloseConn();
emit ClearStatusMsg();
emit DisableAllButtons();
emit EnableConnectButton();
SetState(FlasherStates::kIdle);
}
}
bool Flasher::IsBootloaderDetected() const {
return is_bootloader_;
}
bool Flasher::IsFirmwareProtected() {
qInfo() << "Send is firmware protected command";
serial_port_.write(kIsFwProtectedCmd, sizeof(kIsFwProtectedCmd));
serial_port_.WaitForReadyRead(kSerialTimeoutInMs);
return CheckTrue();
}
bool Flasher::IsReadProtectionEnabled() const {
return is_read_protection_enabled_;
}
bool Flasher::OpenFile(const QString& file_path) {
file_to_flash_.setFileName(file_path);
return file_to_flash_.open(QIODevice::ReadOnly);
}
void Flasher::ReconnectingToBoard() {
if (is_timer_started_) {
if (serial_port_.TryOpenPort(is_bootloader_)) {
if (is_bootloader_ == is_bootloader_expected_) {
SetState(FlasherStates::kConnected);
is_timer_started_ = false;
} else {
serial_port_.CloseConn();
}
}
if (timer_.hasExpired(kTryToConnectTimeoutInMs)) {
ShowInfoMsg("Error!", "Entering/Exiting bootloader cannot be performed!");
SetState(FlasherStates::kTryToConnect);
is_timer_started_ = false;
}
} else {
emit DisableAllButtons();
is_timer_started_ = true;
timer_.start();
}
}
FlashingInfo Flasher::SendFileSize() {
FlashingInfo flashing_info;
const qint64 file_size = file_content_.size() - signature_size_;
QByteArray file_size_bytes;
file_size_bytes.setNum(file_size);
flashing_info.success = SendMessage(file_size_bytes.data(), file_size_bytes.size(), kSerialTimeoutInMs);
if (!flashing_info.success) {
flashing_info.title = "Flashing process failed";
flashing_info.description = "Send file size problem";
}
return flashing_info;
}
bool Flasher::SendMessage(const char *data, qint64 length, int timeout_ms) {
serial_port_.write(data, length);
serial_port_.WaitForReadyRead(timeout_ms);
return CheckAck();
}
FlashingInfo Flasher::SendServerSecurityData() {
FlashingInfo flashing_info;
QByteArray security_data = QJsonDocument(server_security_data_).toJson(QJsonDocument::Compact);
flashing_info.success = SendMessage(security_data.data(), security_data.size(), kSerialTimeoutInMs);
security_data.clear();
if (!flashing_info.success) {
flashing_info.title = "Flashing process failed";
flashing_info.description = "Board rejected server security data";
}
return flashing_info;
}
FlashingInfo Flasher::SendSignature() {
FlashingInfo flashing_info;
signature_size_ = kSignatureSize;
flashing_info.success = SendMessage(file_content_.data(), kSignatureSize, kSerialTimeoutInMs);
if (!flashing_info.success) {
bool continue_without_signature = true;
if (is_signature_warning_enabled_) {
continue_without_signature = ShowInfoMsg("No signature detected!", "Flashing without a signature is not safe. Flasher will assume that file is without signature.");
}
if (continue_without_signature) {
signature_size_ = 0;
flashing_info.success = true;
}
}
if (!flashing_info.success) {
flashing_info.title = "Flashing process failed";
flashing_info.description = "Send signature problem";
}
return flashing_info;
}
bool Flasher::ReadMessageWithCrc(const char *in_data, qint64 length, int timeout_ms, QByteArray& out_data) {
bool success = false;
QElapsedTimer timer;
timer.start();
if (serial_port_.isOpen()) {
serial_port_.write(in_data, length);
QByteArray data;
serial_port_.WaitForReadyRead(timeout_ms);
serial_port_.ReadData(data);
QByteArray data_crc = data.right(kCrc32Size);
if (data.size() > kCrc32Size) {
uint32_t out_data_size = data.size() - kCrc32Size;
uint32_t crc = Deserialize32(reinterpret_cast<uint8_t *>(data_crc.data()));
uint32_t calc_crc = crc::CalculateCrc32(reinterpret_cast<uint8_t *>(data.data()), out_data_size, false, false);
if (calc_crc == crc) {
out_data = data.left(out_data_size);
success = true;
}
}
}
return success;
}
bool Flasher::SendEnterBootloaderCommand() {
qInfo() << "Send enter bl command";
return SendMessage(kEnterBlCmd, sizeof(kEnterBlCmd), kSerialTimeoutInMs);
}
void Flasher::SendFlashCommand() {
qInfo() << "Send flash command";
serial_port_.write(kFlashFwCmd, sizeof(kFlashFwCmd));
serial_port_.WaitForReadyRead(kSerialTimeoutInMs);
// Check ack
}
bool Flasher::SetLocalFileContent() {
if (file_to_flash_.isOpen()) {
file_content_ = file_to_flash_.readAll();
file_to_flash_.close();
return true;
}
return false;
}
void Flasher::SetState(const FlasherStates& state) {
state_ = state;
}
void Flasher::SetSelectedFileVersion(const QString& selected_file_version) {
selected_file_version_ = selected_file_version;
foreach (const QJsonValue& value, product_info_) {
QJsonObject obj = value.toObject();
if (obj["file_version"].toString() == selected_file_version_) {
file_source_ = obj["file_source"].toString();
}
}
}
void Flasher::TryToConnectConsole() {
QElapsedTimer timer;
timer.start();
while (!serial_port_.isOpen()) {
serial_port_.TryOpenPort(is_bootloader_);
if (timer.hasExpired(kTryToConnectTimeoutInMs)) {
qInfo() << "Timeout";
break;
}
}
}
void Flasher::TryToConnect() {
bool is_connected = false;