This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
331 lines (275 loc) · 9.87 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
struct AppState
{
AppState() {
firmware_file.selected = false;
device_connected.update_mode = false;
device_connected.production_mode = false;
in_progress = false;
finished_with_success = false;
}
struct {
bool selected;
} firmware_file;
union{
struct {
bool update_mode :1;
bool production_mode :1;
} device_connected;
uint8_t device_connected_raw;
};
bool in_progress;
bool finished_with_success;
inline bool ready_to_update(){
return device_connected.update_mode && firmware_file.selected && !in_progress;
}
} state;
QString WELCOME = "Copyright 2018 Nitrokey UG (https://www.nitrokey.com). \n"
"This software is licensed under GNU General Public License v3. \n"
"Source is available on https://github.com/Nitrokey/nitrokey-update-tool. \n"
;
QString WELCOME2 = "This tool allows to update Nitrokey Storage's firmware. "
"<ol><li>1. Please select "
"<I>Configure -> Enable Firmware Update</I> in the Nitrokey App, </li>"
"<li>2. Download the .hex file of the latest firmware version, </li>"
"<li>3. Select the firmware file with 'Select firmware file',"
"<li>4. Start the procedure by pressing 'Update firmware' button."
"</ol>"
// "<br />[1] https://github.com/Nitrokey/nitrokey-storage-firmware/releases/latest \n"
// "<br />[2] https://www.nitrokey.com/download \n"
// "<br />"
;
#include "version.h"
#include <QTimer>
#include "windowscheckprivileges.h"
#include <QMessageBox>
#include <QStatusBar>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->text_log->appendHtml(WELCOME2);
logUI("");
ui->btn_update->setEnabled(false);
QTimer *timer_device_count = new QTimer(this);
connect(timer_device_count, SIGNAL(timeout()), this, SLOT(timer_device_count()));
timer_device_count->start(2000);
#ifdef Q_OS_WIN
if(!WindowsCheckPrivileges::IsElevated()){
QMessageBox::warning(nullptr, "No Admin privileges",
"Application is run without Admin privileges, which are required to operate. Please run it again with Administrator privileges.");
QApplication::quit();
}
#endif
statusBar()->showMessage("Nitrokey Update Tool version " GUI_VERSION " (" GIT_VERSION ")" );
}
#include "lib_implementation.h"
MainWindow::~MainWindow()
{
delete ui;
deinit();
}
#include <QTime>
void MainWindow::logUI(QString msg){
static QTime t;
if (!t.isValid()) t.start();
if (!msg.isEmpty()){
ui->text_log->appendPlainText( QString("[%1]: %2").arg(t.elapsed()/1000., 7).arg(msg));
} else {
ui->text_log->appendPlainText("");
}
}
#include "usb_connection.h"
static USB_connection connection;
#include <QMutex>
#include <QMutexLocker>
QMutex mtx;
void MainWindow::timer_device_count(){
static int last_status = -1;
ui->btn_quit->setEnabled(!state.in_progress);
if (state.in_progress) return;
QMutexLocker lockguard(&mtx);
state.device_connected.update_mode = connection.count_devices_in_update_mode() > 0;
state.device_connected.production_mode = connection.count_devices_in_production_mode() > 0;
if (last_status != state.device_connected_raw){
if(state.device_connected.production_mode && !state.finished_with_success){
logUI("Nitrokey Storage detected in Production mode. Please enable Firmware Update mode in Nitrokey App first (Configure -> Enable Firmware Update).");
} else if(state.device_connected.update_mode){
logUI("Nitrokey Storage detected in Update mode.");
} else if (!state.finished_with_success){
logUI("No Nitrokey Storage device detected. Please insert Nitrokey Storage device.");
}
last_status = state.device_connected_raw;
}
ui->btn_update->setEnabled(state.ready_to_update());
}
inline void delay(int millisecondsWait);
#include <QFileDialog>
#include <QFileInfo>
void MainWindow::on_btn_select_file_clicked()
{
auto filename = QFileDialog::getOpenFileName(this,
tr("Open Firmware"), "", tr("Firmware files (*.hex)"));
if(filename.isEmpty()) return;
ui->edit_firmware_file->setText(filename);
//if file exist and has correct extension
state.firmware_file.selected = launchInThread([&filename](){
QFileInfo check_file(filename);
return check_file.exists() && check_file.isFile();
});
logUI("Set file " + filename);
}
inline void delay(int millisecondsWait)
{
QApplication::processEvents();
QEventLoop loop;
QTimer t;
t.connect(&t, &QTimer::timeout, &loop, &QEventLoop::quit);
t.start(millisecondsWait);
loop.exec();
}
#include <functional>
#include <QFuture>
#include <QFutureWatcher>
#include <QtConcurrent/QtConcurrent>
int32_t MainWindow::launchInThread(std::function<int32_t(void)> func){
QEventLoop loop;
QFutureWatcher<int32_t> watcher;
connect(&watcher, &QFutureWatcher<int32_t>::finished, &loop, &QEventLoop::quit);
QFuture<int32_t> future = QtConcurrent::run(func);
watcher.setFuture(future);
loop.exec();
return future.result();
}
void MainWindow::closeEvent(QCloseEvent * event)
{
if (state.in_progress)
event->ignore();
}
#include "usbdriverinstaller.h"
#include <QStringList>
void MainWindow::on_btn_update_clicked()
{
int32_t result;
if (state.in_progress) return;
state.in_progress = true; //FIXME use ScopeGuard/Exit later
ui->btn_update->setEnabled(!state.in_progress);
ui->btn_quit->setEnabled(!state.in_progress);
logUI("*** Update procedure started");
ui->progressBar->setValue(0);
QMutexLocker lockguard(&mtx);
ui->progressBar->setValue(1);
#ifdef Q_OS_WIN
{
static bool drivers_installed = false;
ui->progressBar->setValue(5);
if (!drivers_installed){
logUI("Installing USB drivers ...");
USBDriverInstaller installer;
QStringList debug;
int retcode = installer.install([&debug](QString s){ debug.append(s); });
if (retcode != 0){
for (auto d : debug){
logUI("USB setup: " + d);
}
logUI("Installing USB drivers has failed. Please run the application with Administrator privileges and try again.");
logUI(QString("Error code %1").arg(retcode));
logUI(QString("Output\n ```\n%1\n```").arg(QString::fromUtf8(installer.output.data())));
logUI(QString("Output (err)\n```\n%1\n```").arg(QString::fromUtf8(installer.output_err.data())));
ui->progressBar->setValue(0);
state.in_progress = false;
return;
}
drivers_installed = true;
logUI(" USB drivers installed.");
}
}
#endif
ui->progressBar->setValue(7);
logUI("Connecting to device ...");
result = init();
if (result != 0){
logUI(QString("WARNING: Device connection has failed (code %1).").arg(result));
logUI("Device could not be connected. Cancelling the procedure.");
#ifdef Q_OS_LINUX
logUI("Please run the tool with root privileges (e.g. using 'sudo') or install udev rules.");
#endif
ui->progressBar->setValue(0);
state.in_progress = false;
return;
}
ui->progressBar->setValue(10);
logUI("Erasing device ...");
result = launchInThread([]()->int32_t {
return erase();
});
if (result != 0){
logUI(QString("WARNING: Erasing device result: %1").arg(result));
ui->progressBar->setValue(0);
state.in_progress = false;
return;
}
ui->progressBar->setValue(20);
delay(1*1000);
ui->progressBar->setValue(21);
logUI("Flashing device ...");
std::string path = ui->edit_firmware_file->text().toStdString();
result = launchInThread([path]()->int32_t {
return flash(path.c_str());
});
if (result != 0){
logUI(QString("WARNING: Flashing device result: %1").arg(result));
ui->progressBar->setValue(0);
state.in_progress = false;
return;
}
ui->progressBar->setValue(80);
for (int i=0; i<5; i++){
ui->progressBar->setValue(80+i+1);
delay(1*1000);
}
logUI("Launching device to production mode");
result = launchInThread([]()->int32_t {
return launch();
});
for (int i=0; i<2; i++){
ui->progressBar->setValue(85+i+1);
delay(1*1000);
}
if (result != 0){
//Launch command might return failure in certain environments, however the device accepts it and reconnects.
//Let's check if device has launched before reporting error.
if (connection.count_devices_in_production_mode() < 1){
logUI(QString("WARNING: Launch device result: %1").arg(result));
ui->progressBar->setValue(0);
state.in_progress = false;
return;
}
}
ui->progressBar->setValue(90);
if (connection.count_devices_in_production_mode() > 0){
logUI("Device connects in production mode.");
} else {
logUI("WARNING: Device has not responded in production mode.");
ui->progressBar->setValue(0);
state.in_progress = false;
return;
}
ui->progressBar->setValue(100);
state.finished_with_success = true;
state.in_progress = false;
logUI("*** Update procedure finished successfully");
logUI("");
}
#include "aboutdialog.h"
void MainWindow::on_actionAbout_triggered()
{
AboutDialog a;
a.exec();
}
void MainWindow::on_btn_quit_clicked()
{
QApplication::quit();
}