Repository navigation
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
3152 lines (2682 loc) · 116 KB
/
Copy pathmainwindow.cpp
File metadata and controls
3152 lines (2682 loc) · 116 KB
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
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QComboBox>
#include <QCheckBox>
#include <QTextEdit>
#include <QGroupBox>
#include <QProgressBar>
#include <QLineEdit>
#include <QTabWidget>
#include <QFileDialog>
#include <QMessageBox>
#include <QFontDatabase>
#include <QScrollBar>
#include <QProcessEnvironment>
#include <QRegularExpression>
#include <QStatusBar>
#include <QSpinBox>
#include <QTimer>
#include <QTextCursor>
#include <QTextCharFormat>
#include <QColor>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, isFlashing(false)
, isSettingLoaded(false)
, isEncryptionConfigured(false)
, encryptionSetupInProgress(false)
, isCheckingEncryption(false)
, isFirstEncryptionCheck(true)
, isEncryptionDetectionParsed(false)
, chipInfoReceived(false)
, flashSizeReceived(false)
, commandQueueRunning(false)
, keyBurnSkipped(false)
, isBurningFuses(false)
, dependenciesChecked(false)
, isFlashMode(false)
, flashingEncryptedImages(false)
, handlingKeyBurned(false)
, pendingEncryptionSuccessDialog(false)
, fuseBurnIndex(0)
, flashRetryCount(0)
, encryptionStepIndex(0)
, commandQueueIndex(0)
, esptoolProcess(nullptr)
, serialMonitor(nullptr)
, settings(new QSettings("ESP32Tools", "Flasher", this))
, encryptionCheckTimer(nullptr)
{
setupUI();
createConnections();
setupAutoSave();
loadSettings();
encryptionCheckTimer = new QTimer(this);
encryptionCheckTimer->setSingleShot(true);
connect(encryptionCheckTimer, &QTimer::timeout, this, &MainWindow::checkEncryptionStatus);
serialMonitor = new SerialMonitor(this);
connect(serialMonitor, &SerialMonitor::portDetected,
this, &MainWindow::onPortDetected);
connect(serialMonitor, &SerialMonitor::portRemoved,
this, &MainWindow::onPortRemoved);
serialMonitor->start();
updateStatus("Ready");
appendLog("Application started. Waiting for ESP32...", "gray");
updateFlashButton();
// Clean up any temp leftovers from a previous crash
QTimer::singleShot(0, this, [this]() {
cleanupTempDirectory();
});
QTimer::singleShot(1000, this, &MainWindow::checkAndInstallDependencies);
}
MainWindow::~MainWindow()
{
saveSettings();
cleanupEncryptedFile();
cleanupTempDirectory();
if (encryptionCheckTimer) {
encryptionCheckTimer->stop();
delete encryptionCheckTimer;
encryptionCheckTimer = nullptr;
}
if (serialMonitor) {
serialMonitor->stop();
serialMonitor->wait();
delete serialMonitor;
serialMonitor = nullptr;
}
if (esptoolProcess) {
esptoolProcess->disconnect();
if (esptoolProcess->state() == QProcess::Running) {
esptoolProcess->terminate();
esptoolProcess->waitForFinished(2000);
}
delete esptoolProcess;
esptoolProcess = nullptr;
}
}
void MainWindow::setupUI()
{
setWindowTitle("ESP32 Flasher & Encryption Manager");
setMinimumSize(920, 750);
resize(950, 780);
QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);
mainLayout->setSpacing(4);
mainLayout->setContentsMargins(8, 8, 8, 8);
mainTabs = new QTabWidget(this);
mainLayout->addWidget(mainTabs);
// ==================== Flash Tab ====================
flashTab = new QWidget();
mainTabs->addTab(flashTab, "Flash Firmware");
QVBoxLayout *flashLayout = new QVBoxLayout(flashTab);
flashLayout->setSpacing(4);
flashLayout->setContentsMargins(6, 6, 6, 6);
// ========== Port Selection with Refresh Button ==========
QHBoxLayout *portLayout = new QHBoxLayout();
portLayout->setSpacing(5);
QLabel *portLabel = new QLabel("Port:");
portLabel->setFixedWidth(35);
portLayout->addWidget(portLabel);
portComboBox = new QComboBox();
portComboBox->setMinimumWidth(150);
portLayout->addWidget(portComboBox);
statusLabel = new QLabel("Ready");
statusBar()->addWidget(statusLabel);
QPushButton *checkDepsBtn = new QPushButton("🔍 Check Dependencies");
checkDepsBtn->setFixedWidth(150);
checkDepsBtn->setStyleSheet("QPushButton { background-color: #17a2b8; font-weight: bold; font-size: 10px; padding: 2px 8px; }");
connect(checkDepsBtn, &QPushButton::clicked, this, &MainWindow::checkAndInstallDependencies);
statusBar()->addPermanentWidget(checkDepsBtn);
QPushButton *refreshBtn = new QPushButton("🔄 Refresh");
refreshBtn->setFixedWidth(90);
refreshBtn->setToolTip("Refresh port list and chip information");
refreshBtn->setStyleSheet("QPushButton { background-color: #28a745; font-weight: bold; }");
connect(refreshBtn, &QPushButton::clicked, this, &MainWindow::onRefreshPorts);
portLayout->addWidget(refreshBtn);
portLayout->addStretch();
flashLayout->addLayout(portLayout);
portStatusLabel = new QLabel("🔴 No ESP32 detected");
portStatusLabel->setStyleSheet("font-weight: bold;");
flashLayout->addWidget(portStatusLabel);
// ========== Chip Info ==========
chipInfoGroup = new QGroupBox("ESP32 Info");
chipInfoGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 6px; padding-top: 8px; }");
QGridLayout *chipInfoLayout = new QGridLayout(chipInfoGroup);
chipInfoLayout->setSpacing(2);
chipInfoLayout->setContentsMargins(6, 8, 6, 4);
chipModelLabel = new QLabel("Model: --");
chipRevisionLabel = new QLabel("Rev: --");
chipCoresLabel = new QLabel("Cores: --");
chipFeaturesLabel = new QLabel("Features: --");
chipMacLabel = new QLabel("MAC: --");
flashSizeLabel = new QLabel("Flash: --");
encryptionStatusLabel = new QLabel("🔓 Encryption: Checking...");
encryptionStatusLabel->setStyleSheet("color: #ffd43b; font-weight: bold;");
chipInfoLayout->addWidget(chipModelLabel, 0, 0);
chipInfoLayout->addWidget(chipRevisionLabel, 0, 1);
chipInfoLayout->addWidget(chipCoresLabel, 0, 2);
chipInfoLayout->addWidget(chipFeaturesLabel, 1, 0, 1, 2);
chipInfoLayout->addWidget(chipMacLabel, 1, 2);
chipInfoLayout->addWidget(flashSizeLabel, 2, 0);
chipInfoLayout->addWidget(encryptionStatusLabel, 2, 1, 1, 2);
flashLayout->addWidget(chipInfoGroup);
// ========== Key Management ==========
QGroupBox *keyGroup = new QGroupBox("Encryption Key");
keyGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 6px; padding-top: 8px; }");
QHBoxLayout *keyLayout = new QHBoxLayout(keyGroup);
keyLayout->setSpacing(4);
keyLayout->setContentsMargins(6, 8, 6, 4);
generateKeyBtn = new QPushButton("Generate Key");
generateKeyBtn->setFixedWidth(100);
selectKeyBtn = new QPushButton("Select Key");
selectKeyBtn->setFixedWidth(90);
keyFileEdit = new QLineEdit();
keyFileEdit->setReadOnly(true);
keyFileEdit->setPlaceholderText("No key selected");
keyFileEdit->setMaximumHeight(24);
keyLayout->addWidget(generateKeyBtn);
keyLayout->addWidget(selectKeyBtn);
keyLayout->addWidget(keyFileEdit);
flashLayout->addWidget(keyGroup);
// ========== Flash Files Group ==========
QGroupBox *filesGroup = new QGroupBox("Flash Files");
filesGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 6px; padding-top: 8px; }");
QGridLayout *filesLayout = new QGridLayout(filesGroup);
filesLayout->setSpacing(3);
filesLayout->setContentsMargins(6, 8, 6, 4);
selectBootloaderBtn = new QPushButton("Bootloader");
selectBootloaderBtn->setFixedWidth(90);
bootloaderFileEdit = new QLineEdit();
bootloaderFileEdit->setReadOnly(true);
bootloaderFileEdit->setPlaceholderText("No bootloader");
bootloaderFileEdit->setMaximumHeight(24);
bootloaderOffsetSpin = new QSpinBox();
bootloaderOffsetSpin->setRange(0, 0xFFFFFF);
bootloaderOffsetSpin->setValue(0x1000);
bootloaderOffsetSpin->setPrefix("0x");
bootloaderOffsetSpin->setDisplayIntegerBase(16);
bootloaderOffsetSpin->setMinimumWidth(100);
bootloaderOffsetSpin->setMaximumWidth(120);
bootloaderOffsetSpin->setMaximumHeight(24);
filesLayout->addWidget(selectBootloaderBtn, 0, 0);
filesLayout->addWidget(bootloaderFileEdit, 0, 1);
filesLayout->addWidget(new QLabel("Offset:"), 0, 2);
filesLayout->addWidget(bootloaderOffsetSpin, 0, 3);
selectPartitionBtn = new QPushButton("Partition");
selectPartitionBtn->setFixedWidth(90);
partitionFileEdit = new QLineEdit();
partitionFileEdit->setReadOnly(true);
partitionFileEdit->setPlaceholderText("No partition");
partitionFileEdit->setMaximumHeight(24);
partitionOffsetSpin = new QSpinBox();
partitionOffsetSpin->setRange(0, 0xFFFFFF);
partitionOffsetSpin->setValue(0x8000);
partitionOffsetSpin->setPrefix("0x");
partitionOffsetSpin->setDisplayIntegerBase(16);
partitionOffsetSpin->setMinimumWidth(100);
partitionOffsetSpin->setMaximumWidth(120);
partitionOffsetSpin->setMaximumHeight(24);
filesLayout->addWidget(selectPartitionBtn, 1, 0);
filesLayout->addWidget(partitionFileEdit, 1, 1);
filesLayout->addWidget(new QLabel("Offset:"), 1, 2);
filesLayout->addWidget(partitionOffsetSpin, 1, 3);
selectFirmwareBtn = new QPushButton("Firmware");
selectFirmwareBtn->setFixedWidth(90);
firmwareFileEdit = new QLineEdit();
firmwareFileEdit->setReadOnly(true);
firmwareFileEdit->setPlaceholderText("No firmware");
firmwareFileEdit->setMaximumHeight(24);
firmwareOffsetSpin = new QSpinBox();
firmwareOffsetSpin->setRange(0, 0xFFFFFF);
firmwareOffsetSpin->setValue(0x10000);
firmwareOffsetSpin->setPrefix("0x");
firmwareOffsetSpin->setDisplayIntegerBase(16);
firmwareOffsetSpin->setMinimumWidth(100);
firmwareOffsetSpin->setMaximumWidth(120);
firmwareOffsetSpin->setMaximumHeight(24);
filesLayout->addWidget(selectFirmwareBtn, 2, 0);
filesLayout->addWidget(firmwareFileEdit, 2, 1);
filesLayout->addWidget(new QLabel("Offset:"), 2, 2);
filesLayout->addWidget(firmwareOffsetSpin, 2, 3);
flashLayout->addWidget(filesGroup);
// ========== Baud Rate & Options ==========
QHBoxLayout *settingsLayout = new QHBoxLayout();
settingsLayout->setSpacing(8);
QGroupBox *baudGroup = new QGroupBox("Baud Rate");
baudGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 4px; padding-top: 6px; border: 1px solid #cccccc; border-radius: 4px; }");
QHBoxLayout *baudLayout = new QHBoxLayout(baudGroup);
baudLayout->setSpacing(3);
baudLayout->setContentsMargins(4, 6, 4, 4);
baudRateComboBox = new QComboBox();
baudRateComboBox->setMinimumWidth(100);
baudRateComboBox->setMaximumHeight(24);
populateBaudRates();
baudLayout->addWidget(baudRateComboBox);
settingsLayout->addWidget(baudGroup);
QGroupBox *optionsGroup = new QGroupBox("Options");
optionsGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 4px; padding-top: 6px; border: 1px solid #cccccc; border-radius: 4px; }");
QHBoxLayout *optionsLayout = new QHBoxLayout(optionsGroup);
optionsLayout->setSpacing(8);
optionsLayout->setContentsMargins(6, 6, 6, 4);
encryptFlashCheckBox = new QCheckBox("Encrypt Firmware");
encryptFlashCheckBox->setToolTip("Encrypt the firmware before flashing");
compressCheckBox = new QCheckBox("Compress");
compressCheckBox->setToolTip("Compress data during flashing");
compressCheckBox->setChecked(true);
optionsLayout->addWidget(encryptFlashCheckBox);
optionsLayout->addWidget(compressCheckBox);
optionsLayout->addStretch();
settingsLayout->addWidget(optionsGroup);
flashLayout->addLayout(settingsLayout);
// ========== Action Buttons ==========
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->setSpacing(6);
flashBtn = new QPushButton("🔥 Flash Firmware");
flashBtn->setStyleSheet("QPushButton { font-weight: bold; font-size: 13px; }");
flashBtn->setMinimumHeight(34);
eraseFlashBtn = new QPushButton("🗑️ Erase Flash");
eraseFlashBtn->setStyleSheet("QPushButton { background-color: #dc3545; font-weight: bold; }");
eraseFlashBtn->setMinimumHeight(34);
eraseFlashBtn->setEnabled(false);
clearAllBtn = new QPushButton("📂 Clear All");
clearAllBtn->setStyleSheet("QPushButton { font-weight: bold; }");
clearAllBtn->setMinimumHeight(34);
connect(clearAllBtn, &QPushButton::clicked, this, &MainWindow::onClearAll);
buttonLayout->addWidget(flashBtn);
buttonLayout->addWidget(eraseFlashBtn);
buttonLayout->addWidget(clearAllBtn);
flashLayout->addLayout(buttonLayout);
// ========== Progress ==========
progressBar = new QProgressBar();
progressBar->setVisible(false);
progressBar->setMaximumHeight(18);
flashLayout->addWidget(progressBar);
// ========== Log Output ==========
QGroupBox *logGroup = new QGroupBox("Log Output");
logGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 4px; padding-top: 6px; }");
QVBoxLayout *logLayout = new QVBoxLayout(logGroup);
logLayout->setSpacing(2);
logLayout->setContentsMargins(4, 6, 4, 4);
QHBoxLayout *logToolbar = new QHBoxLayout();
logToolbar->setSpacing(4);
QLabel *logLabel = new QLabel("Console Output");
logLabel->setStyleSheet("font-weight: bold;");
logToolbar->addWidget(logLabel);
logToolbar->addStretch();
QPushButton *clearLogBtn = new QPushButton("🗑️ Clear");
clearLogBtn->setFixedWidth(70);
clearLogBtn->setMinimumHeight(24);
clearLogBtn->setStyleSheet("QPushButton { background-color: #6c757d; font-weight: bold; font-size: 10px; }");
connect(clearLogBtn, &QPushButton::clicked, [this]() {
logTextEdit->clear();
appendLog("Console cleared", "gray");
});
logToolbar->addWidget(clearLogBtn);
logLayout->addLayout(logToolbar);
logTextEdit = new QTextEdit();
logTextEdit->setReadOnly(true);
logTextEdit->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
logTextEdit->setStyleSheet("QTextEdit { background-color: #1e1e1e; color: #d4d4d4; }");
logTextEdit->setMaximumHeight(180);
logLayout->addWidget(logTextEdit);
flashLayout->addWidget(logGroup);
// ==================== Fuse Tab ====================
fuseTab = new QWidget();
mainTabs->addTab(fuseTab, "Security Fuses");
QVBoxLayout *fuseLayout = new QVBoxLayout(fuseTab);
fuseLayout->setSpacing(6);
fuseLayout->setContentsMargins(10, 10, 10, 10);
QGroupBox *statusGroup = new QGroupBox("Fuse Status");
statusGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 8px; padding-top: 10px; }");
QGridLayout *statusLayout = new QGridLayout(statusGroup);
statusLayout->setSpacing(4);
statusLayout->setContentsMargins(10, 12, 10, 8);
int row = 0;
statusLayout->addWidget(new QLabel("UART Download:"), row, 0);
uartFuseStatus = new QLabel("Unknown");
uartFuseStatus->setStyleSheet("font-weight: bold;");
statusLayout->addWidget(uartFuseStatus, row, 1);
row++;
statusLayout->addWidget(new QLabel("JTAG:"), row, 0);
jtagFuseStatus = new QLabel("Unknown");
jtagFuseStatus->setStyleSheet("font-weight: bold;");
statusLayout->addWidget(jtagFuseStatus, row, 1);
row++;
statusLayout->addWidget(new QLabel("DL Encrypt:"), row, 0);
dlEncryptStatus = new QLabel("Unknown");
dlEncryptStatus->setStyleSheet("font-weight: bold;");
statusLayout->addWidget(dlEncryptStatus, row, 1);
row++;
statusLayout->addWidget(new QLabel("DL Decrypt:"), row, 0);
dlDecryptStatus = new QLabel("Unknown");
dlDecryptStatus->setStyleSheet("font-weight: bold;");
statusLayout->addWidget(dlDecryptStatus, row, 1);
row++;
statusLayout->addWidget(new QLabel("Cache:"), row, 0);
cacheStatus = new QLabel("Unknown");
cacheStatus->setStyleSheet("font-weight: bold;");
statusLayout->addWidget(cacheStatus, row, 1);
row++;
statusLayout->addWidget(new QLabel("Console Debug:"), row, 0);
consoleDebugStatus = new QLabel("Unknown");
consoleDebugStatus->setStyleSheet("font-weight: bold;");
statusLayout->addWidget(consoleDebugStatus, row, 1);
row++;
statusLayout->addWidget(new QLabel("FLASH_CRYPT_CNT:"), row, 0);
flashCryptCntStatus = new QLabel("Unknown");
flashCryptCntStatus->setStyleSheet("font-weight: bold;");
statusLayout->addWidget(flashCryptCntStatus, row, 1);
row++;
statusLayout->addWidget(new QLabel("FLASH_CRYPT_CONFIG:"), row, 0);
flashCryptConfigStatus = new QLabel("Unknown");
flashCryptConfigStatus->setStyleSheet("font-weight: bold;");
statusLayout->addWidget(flashCryptConfigStatus, row, 1);
row++;
QHBoxLayout *statusBtnLayout = new QHBoxLayout();
readFuseStatusBtn = new QPushButton("🔍 Read Fuse Status");
readFuseStatusBtn->setMinimumHeight(30);
connect(readFuseStatusBtn, &QPushButton::clicked, this, &MainWindow::onReadFuseStatus);
statusBtnLayout->addWidget(readFuseStatusBtn);
statusBtnLayout->addStretch();
statusLayout->addLayout(statusBtnLayout, row, 0, 1, 2);
fuseLayout->addWidget(statusGroup);
fuseOptionsGroup = new QGroupBox("Burn Security Fuses (IRREVERSIBLE!)");
fuseOptionsGroup->setStyleSheet("QGroupBox { font-weight: bold; color: #dc3545; margin-top: 8px; padding-top: 10px; }");
QVBoxLayout *fuseOptionsLayout = new QVBoxLayout(fuseOptionsGroup);
fuseOptionsLayout->setSpacing(4);
fuseOptionsLayout->setContentsMargins(10, 12, 10, 8);
QLabel *warningLabel = new QLabel("⚠️ WARNING: Burning any fuse is PERMANENT and CANNOT be undone!");
warningLabel->setStyleSheet("color: #dc3545; font-weight: bold;");
fuseOptionsLayout->addWidget(warningLabel);
QGridLayout *fuseChecksLayout = new QGridLayout();
fuseChecksLayout->setSpacing(4);
fuseChecksLayout->setContentsMargins(0, 4, 0, 4);
int frow = 0;
uartDownloadDisCheckBox = new QCheckBox("Disable UART Download Mode");
uartDownloadDisCheckBox->setToolTip("Disables UART download mode. ESP32 will not accept new firmware via serial.");
fuseChecksLayout->addWidget(uartDownloadDisCheckBox, frow, 0);
frow++;
jtagDisableCheckBox = new QCheckBox("Disable JTAG");
jtagDisableCheckBox->setToolTip("Disables JTAG debugging interface.");
fuseChecksLayout->addWidget(jtagDisableCheckBox, frow, 0);
frow++;
disableDlEncryptCheckBox = new QCheckBox("Disable UART Flash Encryption");
disableDlEncryptCheckBox->setToolTip("Disables flash encryption in UART bootloader.");
fuseChecksLayout->addWidget(disableDlEncryptCheckBox, frow, 0);
frow++;
disableDlDecryptCheckBox = new QCheckBox("Disable UART Flash Decryption");
disableDlDecryptCheckBox->setToolTip("Disables flash decryption in UART bootloader.");
fuseChecksLayout->addWidget(disableDlDecryptCheckBox, frow, 0);
frow++;
disableCacheCheckBox = new QCheckBox("Disable Cache");
disableCacheCheckBox->setToolTip("Disables cache memory.");
fuseChecksLayout->addWidget(disableCacheCheckBox, frow, 0);
frow++;
consoleDebugDisableCheckBox = new QCheckBox("Disable Console Debug");
consoleDebugDisableCheckBox->setToolTip("Disables ROM BASIC interpreter fallback.");
fuseChecksLayout->addWidget(consoleDebugDisableCheckBox, frow, 0);
frow++;
fuseOptionsLayout->addLayout(fuseChecksLayout);
QHBoxLayout *fuseBtnLayout = new QHBoxLayout();
burnSecurityFusesBtn = new QPushButton("🔥 Burn Selected Fuses");
burnSecurityFusesBtn->setStyleSheet("QPushButton { background-color: #dc3545; font-weight: bold; }");
burnSecurityFusesBtn->setMinimumHeight(35);
connect(burnSecurityFusesBtn, &QPushButton::clicked, this, &MainWindow::onBurnSecurityFuses);
QPushButton *clearFuseChecksBtn = new QPushButton("Clear All");
clearFuseChecksBtn->setStyleSheet("QPushButton { background-color: #6c757d; }");
clearFuseChecksBtn->setMinimumHeight(35);
connect(clearFuseChecksBtn, &QPushButton::clicked, [this]() {
uartDownloadDisCheckBox->setChecked(false);
jtagDisableCheckBox->setChecked(false);
disableDlEncryptCheckBox->setChecked(false);
disableDlDecryptCheckBox->setChecked(false);
disableCacheCheckBox->setChecked(false);
consoleDebugDisableCheckBox->setChecked(false);
appendLog("All fuse selections cleared", "gray");
});
fuseBtnLayout->addWidget(burnSecurityFusesBtn);
fuseBtnLayout->addWidget(clearFuseChecksBtn);
fuseBtnLayout->addStretch();
fuseOptionsLayout->addLayout(fuseBtnLayout);
fuseLayout->addWidget(fuseOptionsGroup);
QGroupBox *infoGroup = new QGroupBox("Fuse Information");
infoGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 8px; padding-top: 10px; }");
QVBoxLayout *infoLayout = new QVBoxLayout(infoGroup);
infoLayout->setSpacing(4);
infoLayout->setContentsMargins(10, 12, 10, 8);
QTextEdit *infoText = new QTextEdit();
infoText->setReadOnly(true);
infoText->setHtml(R"(
<p><b>Fuse Descriptions:</b></p>
<p><b>Disable UART Download Mode:</b> Prevents flashing via UART. The chip will no longer accept new firmware over serial.</p>
<p><b>Disable JTAG:</b> Disables the JTAG debugging interface, preventing external debugging.</p>
<p><b>Disable UART Flash Encryption:</b> Disables encryption of flash writes in UART bootloader.</p>
<p><b>Disable UART Flash Decryption:</b> Disables decryption of flash reads in UART bootloader.</p>
<p><b>Disable Cache:</b> Disables cache memory, affecting performance.</p>
<p><b>Disable Console Debug:</b> Disables ROM BASIC interpreter fallback.</p>
<br>
<p style='color: #dc3545;'><b>⚠️ ALL FUSE BURNS ARE PERMANENT AND IRREVERSIBLE!</b></p>
<p style='color: #ffd43b;'>Once burned, these features cannot be re-enabled.</p>
)");
infoLayout->addWidget(infoText);
fuseLayout->addWidget(infoGroup);
// ==================== About Tab ====================
aboutTab = new QWidget();
mainTabs->addTab(aboutTab, "ℹ️ About");
QVBoxLayout *aboutLayout = new QVBoxLayout(aboutTab);
aboutLayout->setSpacing(12);
aboutLayout->setContentsMargins(20, 20, 20, 20);
QLabel *titleLabel = new QLabel("ESP32 Flasher & Encryption Manager");
titleLabel->setStyleSheet("font-size: 20px; font-weight: bold; color: #4a90d9;");
titleLabel->setAlignment(Qt::AlignCenter);
aboutLayout->addWidget(titleLabel);
QLabel *versionLabel = new QLabel("Version 1.1");
versionLabel->setStyleSheet("font-size: 14px; color: #666;");
versionLabel->setAlignment(Qt::AlignCenter);
aboutLayout->addWidget(versionLabel);
QFrame *line = new QFrame();
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
aboutLayout->addWidget(line);
QTextEdit *descText = new QTextEdit();
descText->setReadOnly(true);
descText->setHtml(R"(
<p style='font-size: 12px; line-height: 1.6;'>
<b>ESP32 Flasher & Encryption Manager</b> is a comprehensive tool for managing
ESP32 devices with flash encryption support.
</p>
<p style='font-size: 12px; line-height: 1.6;'>
<b>Features:</b><br>
• Flash firmware with or without encryption<br>
• Manage encryption keys (generate, select, burn)<br>
• Burn security fuses (UART, JTAG, etc.)<br>
• Hardware encryption support for already-encrypted chips<br>
• Automatic key selection after generation<br>
• Real-time chip information and encryption status<br>
• Multi-file flashing (bootloader, partition, firmware)<br>
• Configurable baud rate and flash settings<br>
• Security fuse management<br>
• Auto-retry on failed operations
</p>
<br>
<p style='font-size: 12px; line-height: 1.6;'>
<b>Technologies:</b><br>
• Built with Qt 6.11.0<br>
• Uses esptool.py, espefuse.py, espsecure.py<br>
• Windows native serial port detection
</p>
)");
descText->setMaximumHeight(250);
descText->setStyleSheet("QTextEdit { background-color: #f8f9fa; border: none; }");
aboutLayout->addWidget(descText);
QGroupBox *authorGroup = new QGroupBox("Author Information");
authorGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 8px; padding-top: 10px; }");
QVBoxLayout *authorLayout = new QVBoxLayout(authorGroup);
authorLayout->setSpacing(6);
authorLayout->setContentsMargins(12, 12, 12, 12);
QLabel *authorName = new QLabel("👤 Saviour Emmanuel Ekiko");
authorName->setStyleSheet("font-size: 14px; font-weight: bold;");
authorLayout->addWidget(authorName);
QLabel *authorRole = new QLabel("Embedded Systems Developer & Software Engineer");
authorRole->setStyleSheet("font-size: 12px; color: #555;");
authorLayout->addWidget(authorRole);
QLabel *authorGithub = new QLabel("<a href='https://www.github.com/savio-code' style='color: #555; text-decoration: none;'>🐙 https://www.github.com/savio-code</a>");
authorGithub->setOpenExternalLinks(true);
authorGithub->setTextFormat(Qt::RichText);
authorGithub->setStyleSheet("font-size: 12px;");
authorGithub->setToolTip("Open GitHub profile in browser");
authorLayout->addWidget(authorGithub);
aboutLayout->addWidget(authorGroup);
QGroupBox *licenseGroup = new QGroupBox("License");
licenseGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 8px; padding-top: 10px; }");
QVBoxLayout *licenseLayout = new QVBoxLayout(licenseGroup);
licenseLayout->setSpacing(6);
licenseLayout->setContentsMargins(12, 12, 12, 12);
QLabel *licenseText = new QLabel("This software is provided 'as is' without any warranty.\n"
"Use at your own risk. The author assumes no liability for any\n"
"damage or loss caused by the use of this software.");
licenseText->setStyleSheet("font-size: 11px; color: #666;");
licenseText->setWordWrap(true);
licenseLayout->addWidget(licenseText);
QLabel *licenseCopyright = new QLabel("© 2026 Saviour Ekiko. All rights reserved.");
licenseCopyright->setStyleSheet("font-size: 11px; color: #888;");
licenseCopyright->setAlignment(Qt::AlignCenter);
licenseLayout->addWidget(licenseCopyright);
aboutLayout->addWidget(licenseGroup);
QGroupBox *creditsGroup = new QGroupBox("Credits");
creditsGroup->setStyleSheet("QGroupBox { font-weight: bold; margin-top: 8px; padding-top: 10px; }");
QVBoxLayout *creditsLayout = new QVBoxLayout(creditsGroup);
creditsLayout->setSpacing(4);
creditsLayout->setContentsMargins(12, 12, 12, 12);
QLabel *creditsText = new QLabel(
"• Espressif Systems for esptool.py, espefuse.py, and espsecure.py\n"
"• Qt Framework for the GUI\n"
"• PlatformIO community for testing and feedback"
);
creditsText->setStyleSheet("font-size: 11px; color: #555;");
creditsText->setWordWrap(true);
creditsLayout->addWidget(creditsText);
aboutLayout->addWidget(creditsGroup);
aboutLayout->addStretch();
statusLabel = new QLabel("Ready");
statusBar()->addWidget(statusLabel);
setStyleSheet(R"(
QGroupBox {
font-weight: bold;
border: 1px solid #cccccc;
border-radius: 4px;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 8px;
padding: 0 4px 0 4px;
}
QPushButton {
background-color: #4a90d9;
color: white;
border: none;
padding: 5px 12px;
border-radius: 3px;
font-weight: bold;
}
QPushButton:hover {
background-color: #357abd;
}
QPushButton:pressed {
background-color: #2a5f8f;
}
QPushButton:disabled {
background-color: #808080;
}
QLineEdit {
padding: 3px 6px;
border: 1px solid #cccccc;
border-radius: 3px;
}
QComboBox {
padding: 3px 6px;
border: 1px solid #cccccc;
border-radius: 3px;
}
QCheckBox {
padding: 2px;
}
QSpinBox {
padding: 3px 6px;
border: 1px solid #cccccc;
border-radius: 3px;
min-width: 100px;
}
QTabWidget::pane {
border: 1px solid #cccccc;
border-radius: 4px;
}
QTabBar::tab {
background-color: #f0f0f0;
padding: 6px 14px;
margin-right: 2px;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
}
QTabBar::tab:selected {
background-color: #4a90d9;
color: white;
}
QLabel {
font-size: 11px;
}
)");
}
void MainWindow::checkAndInstallDependencies()
{
appendLog("========================================", "gray");
appendLog("🔍 Checking Python and Espressif tools...", "gray");
appendLog("========================================", "gray");
QString python = getPythonPath();
if (python.isEmpty()) {
appendLog("❌ Python not found!", "red");
appendLog("💡 Please install Python 3.8+ from:", "yellow");
appendLog(" https://www.python.org/downloads/", "gray");
appendLog(" Make sure to check 'Add Python to PATH' during installation.", "gray");
QMessageBox::warning(this, "Python Not Found",
"Python is not installed or not in PATH.\n\n"
"Please install Python 3.8+ from:\n"
"https://www.python.org/downloads/\n\n"
"Make sure to check 'Add Python to PATH' during installation.");
return;
}
appendLog("✅ Python found: " + python, "green");
bool hasEsptool = checkModuleAvailable("esptool");
bool hasEspsecure = checkModuleAvailable("espsecure");
bool hasEspefuse = checkModuleAvailable("espefuse");
if (!hasEsptool || !hasEspsecure || !hasEspefuse) {
appendLog("❌ Missing required Espressif tools!", "red");
if (!hasEsptool) appendLog(" • esptool (missing)", "yellow");
if (!hasEspsecure) appendLog(" • espsecure (missing)", "yellow");
if (!hasEspefuse) appendLog(" • espefuse (missing)", "yellow");
appendLog("💡 Please install Espressif tools:", "yellow");
appendLog(" pip install esptool", "gray");
appendLog(" (esptool includes espsecure and espefuse)", "gray");
QMessageBox::StandardButton reply = QMessageBox::question(
this,
"Missing Dependencies",
"Espressif tools are not fully installed.\n\n"
"This tool requires esptool, espsecure, and espefuse.\n\n"
"Would you like to install them now?\n\n"
"Command: pip install esptool",
QMessageBox::Yes | QMessageBox::No
);
if (reply == QMessageBox::Yes) {
installEspressifTools();
}
return;
}
appendLog("✅ esptool found", "green");
appendLog("✅ espsecure found", "green");
appendLog("✅ espefuse found", "green");
dependenciesChecked = true;
appendLog("========================================", "green");
appendLog("✅ Dependency check complete", "green");
appendLog("========================================", "gray");
}
void MainWindow::installEspressifTools()
{
appendLog("📦 Installing esptool (includes espsecure and espefuse)...", "gray");
appendLog("Running: pip install esptool", "gray");
QString python = getPythonPath();
if (python.isEmpty()) {
appendLog("❌ Python not found!", "red");
return;
}
QProcess *pipProcess = new QProcess(this);
if (!pipProcess) {
appendLog("❌ Failed to create process!", "red");
return;
}
QStringList args;
args << "-m" << "pip" << "install" << "esptool";
connect(pipProcess, &QProcess::readyReadStandardOutput, [this, pipProcess]() {
if (!pipProcess) return;
QString output = pipProcess->readAllStandardOutput();
if (!output.isEmpty()) {
appendLog(output, "gray");
}
});
connect(pipProcess, &QProcess::readyReadStandardError, [this, pipProcess]() {
if (!pipProcess) return;
QString error = pipProcess->readAllStandardError();
if (!error.isEmpty()) {
appendLog(error, "yellow");
}
});
connect(pipProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[this, pipProcess](int exitCode, QProcess::ExitStatus status) {
if (!pipProcess) return;
if (exitCode == 0 && status == QProcess::NormalExit) {
appendLog("✅ esptool installed successfully!", "green");
appendLog(" (Includes espsecure and espefuse)", "gray");
dependenciesChecked = true;
QMessageBox::information(this, "Installation Complete",
"esptool has been installed successfully!\n\n"
"This includes:\n"
"• esptool.py\n"
"• espsecure.py\n"
"• espefuse.py\n\n"
"You can now use the ESP32 Flasher.");
appendLog("🔄 Refreshing ports to detect ESP32...", "gray");
onRefreshPorts();
} else {
appendLog("❌ Installation failed!", "red");
appendLog("💡 Please install manually:", "yellow");
appendLog(" pip install esptool", "gray");
QMessageBox::warning(this, "Installation Failed",
"Failed to install esptool.\n\n"
"Please install it manually:\n"
" pip install esptool\n\n"
"(This includes espsecure and espefuse)");
}
pipProcess->deleteLater();
});
pipProcess->start(python, args);
if (!pipProcess->waitForStarted(3000)) {
appendLog("❌ Failed to start pip installation!", "red");
appendLog("💡 Please install manually: pip install esptool", "yellow");
pipProcess->deleteLater();
}
}
void MainWindow::populateBaudRates()
{
QList<int> baudRates = {
115200, 230400, 460800, 921600,
1500000, 2000000, 3000000, 4000000
};
for (int rate : baudRates) {
baudRateComboBox->addItem(QString::number(rate), rate);
}
int defaultIndex = baudRateComboBox->findData(921600);
if (defaultIndex >= 0) {
baudRateComboBox->setCurrentIndex(defaultIndex);
}
}
void MainWindow::createConnections()
{
connect(generateKeyBtn, &QPushButton::clicked,
this, &MainWindow::onGenerateKey);
connect(selectKeyBtn, &QPushButton::clicked,
this, &MainWindow::onSelectKey);
connect(selectBootloaderBtn, &QPushButton::clicked,
this, &MainWindow::onSelectBootloader);
connect(selectPartitionBtn, &QPushButton::clicked,
this, &MainWindow::onSelectPartition);
connect(selectFirmwareBtn, &QPushButton::clicked,
this, &MainWindow::onSelectFirmware);
connect(flashBtn, &QPushButton::clicked,
this, &MainWindow::onFlash);
connect(eraseFlashBtn, &QPushButton::clicked,
this, &MainWindow::onEraseFlash);
connect(encryptFlashCheckBox, &QCheckBox::clicked,
this, &MainWindow::onUpdateFlashButtonClicked);
connect(bootloaderFileEdit, &QLineEdit::textChanged,
this, &MainWindow::updateFlashButton);
connect(partitionFileEdit, &QLineEdit::textChanged,
this, &MainWindow::updateFlashButton);
connect(firmwareFileEdit, &QLineEdit::textChanged,
this, &MainWindow::updateFlashButton);
}
void MainWindow::setupAutoSave()
{
connect(keyFileEdit, &QLineEdit::textChanged, this, &MainWindow::saveSettings);
connect(bootloaderFileEdit, &QLineEdit::textChanged, this, &MainWindow::saveSettings);
connect(partitionFileEdit, &QLineEdit::textChanged, this, &MainWindow::saveSettings);
connect(firmwareFileEdit, &QLineEdit::textChanged, this, &MainWindow::saveSettings);
connect(bootloaderOffsetSpin, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::saveSettings);
connect(partitionOffsetSpin, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::saveSettings);
connect(firmwareOffsetSpin, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::saveSettings);
connect(baudRateComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::saveSettings);
connect(encryptFlashCheckBox, &QCheckBox::stateChanged, this, &MainWindow::saveSettings);
connect(compressCheckBox, &QCheckBox::stateChanged, this, &MainWindow::saveSettings);
connect(portComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::saveSettings);
}
void MainWindow::loadSettings()
{
isSettingLoaded = false;
selectedKeyPath = settings->value("keyPath", "").toString();
selectedBootloaderPath = settings->value("bootloaderPath", "").toString();
selectedPartitionPath = settings->value("partitionPath", "").toString();
selectedFirmwarePath = settings->value("firmwarePath", "").toString();
bootloaderOffsetSpin->setValue(settings->value("bootloaderOffset", 0x1000).toInt());
partitionOffsetSpin->setValue(settings->value("partitionOffset", 0x8000).toInt());
firmwareOffsetSpin->setValue(settings->value("firmwareOffset", 0x10000).toInt());
int baudRate = settings->value("baudRate", 921600).toInt();
int baudIndex = baudRateComboBox->findData(baudRate);
if (baudIndex >= 0) {
baudRateComboBox->setCurrentIndex(baudIndex);
}
encryptFlashCheckBox->setChecked(settings->value("encryptFlash", false).toBool());
compressCheckBox->setChecked(settings->value("compress", true).toBool());
QString lastPort = settings->value("lastPort", "").toString();
int portIndex = portComboBox->findText(lastPort);
if (portIndex >= 0) {
portComboBox->setCurrentIndex(portIndex);
}
keyFileEdit->setText(selectedKeyPath);
bootloaderFileEdit->setText(selectedBootloaderPath);
partitionFileEdit->setText(selectedPartitionPath);
firmwareFileEdit->setText(selectedFirmwarePath);
isSettingLoaded = true;
appendLog("Settings loaded", "gray");
updateFlashButton();
}
void MainWindow::saveSettings()
{
if (!isSettingLoaded) {
return;
}
settings->setValue("keyPath", selectedKeyPath);
settings->setValue("bootloaderPath", selectedBootloaderPath);
settings->setValue("partitionPath", selectedPartitionPath);
settings->setValue("firmwarePath", selectedFirmwarePath);
settings->setValue("bootloaderOffset", bootloaderOffsetSpin->value());
settings->setValue("partitionOffset", partitionOffsetSpin->value());
settings->setValue("firmwareOffset", firmwareOffsetSpin->value());