Repository navigation
Expand file tree
/
Copy pathEsptoolRunner.cpp
More file actions
126 lines (110 loc) · 4.51 KB
/
Copy pathEsptoolRunner.cpp
File metadata and controls
126 lines (110 loc) · 4.51 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
#include "EsptoolRunner.h"
#include <QRegularExpression>
EsptoolRunner::EsptoolRunner(QObject *parent) : QObject(parent) {
m_process = new QProcess(this);
connect(m_process, &QProcess::readyReadStandardOutput, this, &EsptoolRunner::onReadyReadStdout);
connect(m_process, &QProcess::readyReadStandardError, this, &EsptoolRunner::onReadyReadStderr);
connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, &EsptoolRunner::onProcessFinished);
}
void EsptoolRunner::setPythonPath(const QString &path) { m_pythonPath = path; }
void EsptoolRunner::setPort(const QString &port) { m_port = port; }
void EsptoolRunner::setEsptoolPath(const QString &) { /* reserved, using -m invocation below */ }
bool EsptoolRunner::isRunning() const {
return m_process->state() != QProcess::NotRunning;
}
void EsptoolRunner::abort() {
if (isRunning()) m_process->kill();
}
void EsptoolRunner::startProcess(const QStringList &args) {
if (isRunning()) {
emit logLine("[!] A process is already running.");
return;
}
m_accumulatedOutput.clear();
emit logLine("$ " + m_pythonPath + " " + args.join(" "));
m_process->setProcessChannelMode(QProcess::SeparateChannels);
m_process->start(m_pythonPath, args);
}
void EsptoolRunner::readChipInfo() {
m_currentOp = Op::ChipInfo;
QStringList args;
args << "-m" << "esptool" << "--port" << m_port << "chip_id";
startProcess(args);
}
void EsptoolRunner::detectEncryptionState() {
m_currentOp = Op::DetectEncryption;
QStringList args;
args << "-m" << "espefuse" << "--port" << m_port << "summary";
startProcess(args);
}
void EsptoolRunner::flashImage(const QString &imagePath, quint32 offset, bool encrypt) {
m_currentOp = Op::Flash;
QStringList args;
args << "-m" << "esptool" << "--port" << m_port << "write_flash";
if (encrypt) {
args << "--encrypt";
}
args << QString("0x%1").arg(offset, 0, 16) << imagePath;
startProcess(args);
}
void EsptoolRunner::burnEncryptionFuses() {
m_currentOp = Op::BurnFuses;
QStringList args;
// Irreversible. UI must confirm with the user before this is ever called.
args << "-m" << "espefuse" << "--port" << m_port
<< "burn_efuse" << "FLASH_CRYPT_CNT";
startProcess(args);
}
void EsptoolRunner::onReadyReadStdout() {
QByteArray data = m_process->readAllStandardOutput();
QString chunk = QString::fromLocal8Bit(data);
m_accumulatedOutput += chunk;
const auto lines = chunk.split('\n', Qt::SkipEmptyParts);
for (const auto &line : lines) {
emit logLine(line);
static QRegularExpression re(R"(\((\d+)\s*%\))");
auto m = re.match(line);
if (m.hasMatch()) emit progress(m.captured(1).toInt());
if (line.contains("Wrong flash encryption key", Qt::CaseInsensitive) ||
line.contains("Flash encryption key mismatch", Qt::CaseInsensitive) ||
line.contains("MAC mismatch", Qt::CaseInsensitive)) {
emit wrongKeyDetected();
}
}
}
void EsptoolRunner::onReadyReadStderr() {
QByteArray data = m_process->readAllStandardError();
QString chunk = QString::fromLocal8Bit(data);
m_accumulatedOutput += chunk;
const auto lines = chunk.split('\n', Qt::SkipEmptyParts);
for (const auto &line : lines) {
emit logLine("[stderr] " + line);
if (line.contains("Wrong flash encryption key", Qt::CaseInsensitive) ||
line.contains("Flash encryption key mismatch", Qt::CaseInsensitive)) {
emit wrongKeyDetected();
}
}
}
void EsptoolRunner::onProcessFinished(int exitCode, QProcess::ExitStatus status) {
bool success = (status == QProcess::NormalExit && exitCode == 0);
switch (m_currentOp) {
case Op::ChipInfo:
emit chipInfoReady(m_accumulatedOutput);
break;
case Op::DetectEncryption: {
bool fusesBurned = m_accumulatedOutput.contains("FLASH_CRYPT_CNT") ||
m_accumulatedOutput.contains("SPI_BOOT_CRYPT_CNT");
bool encrypted = false;
// crude heuristic -- refine per chip family once you lock down target(s)
static QRegularExpression re(R"(CRYPT_CNT\s*=\s*0x?0*([1-9a-fA-F][0-9a-fA-F]*))");
encrypted = re.match(m_accumulatedOutput).hasMatch();
emit encryptionStateDetected(encrypted, fusesBurned);
break;
}
default:
break;
}
emit finished(success, success ? "Completed successfully." : QString("Exited with code %1").arg(exitCode));
m_currentOp = Op::None;
}