-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathcli.cpp
307 lines (271 loc) · 9.51 KB
/
cli.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
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2020 Raspberry Pi Ltd
*/
#include "cli.h"
#include "imagewriter.h"
#include <iostream>
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QFileInfo>
#include "drivelistmodel.h"
#include "dependencies/drivelist/src/drivelist.hpp"
/* Message handler to discard qDebug() output if using cli (unless --debug is set) */
static void devnullMsgHandler(QtMsgType, const QMessageLogContext &, const QString &)
{
}
Cli::Cli(int &argc, char *argv[]) : QObject(nullptr)
{
#ifdef Q_OS_WIN
/* Allocate console on Windows (only needed if compiled as GUI program) */
if (::AttachConsole(ATTACH_PARENT_PROCESS) || ::AllocConsole())
{
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
std::ios::sync_with_stdio();
}
#endif
_app = new QCoreApplication(argc, argv);
_app->setOrganizationName("Raspberry Pi");
_app->setOrganizationDomain("raspberrypi.org");
_app->setApplicationName("Imager");
_imageWriter = new ImageWriter;
connect(_imageWriter, &ImageWriter::success, this, &Cli::onSuccess);
connect(_imageWriter, &ImageWriter::error, this, &Cli::onError);
connect(_imageWriter, &ImageWriter::preparationStatusUpdate, this, &Cli::onPreparationStatusUpdate);
connect(_imageWriter, &ImageWriter::downloadProgress, this, &Cli::onDownloadProgress);
connect(_imageWriter, &ImageWriter::verifyProgress, this, &Cli::onVerifyProgress);
}
Cli::~Cli()
{
delete _imageWriter;
delete _app;
}
int Cli::run()
{
QCommandLineParser parser;
parser.addOptions({
{"cli", ""},
{"disable-verify", "Disable verification"},
{"enable-writing-system-drives", "Only use this if you know what you are doing"},
{"sha256", "Expected hash", "sha256", ""},
{"cache-file", "Custom cache file (requires setting sha256 as well)", "cache-file", ""},
{"first-run-script", "Add firstrun.sh to image", "first-run-script", ""},
{"cloudinit-userdata", "Add cloud-init user-data file to image", "cloudinit-userdata", ""},
{"cloudinit-networkconfig", "Add cloud-init network-config file to image", "cloudinit-networkconfig", ""},
{"usb-ether-gadget", "Enable USB Ethernet Gadget mode (does not support --first-run-script)"},
{"disable-eject", "Disable automatic ejection of storage media after verification"},
{"debug", "Output debug messages to console"},
{"quiet", "Only write to console on error"},
});
parser.addVersionOption();
parser.addHelpOption();
parser.addPositionalArgument("src", "Image file/URL");
parser.addPositionalArgument("dst", "Destination device");
parser.process(*_app);
const QStringList args = parser.positionalArguments();
if (args.count() != 2)
{
std::cerr << "Usage: --cli [--disable-verify] [--disable-eject] [--sha256 <expected hash> [--cache-file <cache file>]] [--first-run-script <script>] [--usb-ether-gadget] [--debug] [--quiet] <image file to write> <destination drive device>" << std::endl;
return 1;
}
if (!parser.isSet("debug"))
{
qInstallMessageHandler(devnullMsgHandler);
}
_quiet = parser.isSet("quiet");
QByteArray initFormat = (parser.value("cloudinit-userdata").isEmpty()
&& parser.value("cloudinit-networkconfig").isEmpty() ) ? "systemd" : "cloudinit";
if (args[0].startsWith("http:", Qt::CaseInsensitive) || args[0].startsWith("https:", Qt::CaseInsensitive))
{
_imageWriter->setSrc(args[0], 0, 0, parser.value("sha256").toLatin1(), false, "", "", initFormat);
if (!parser.value("cache-file").isEmpty())
{
_imageWriter->setCustomCacheFile(parser.value("cache-file"), parser.value("sha256").toLatin1() );
}
}
else
{
QFileInfo fi(args[0]);
if (fi.isFile())
{
_imageWriter->setSrc(QUrl::fromLocalFile(args[0]), fi.size(), 0, parser.value("sha256").toLatin1(), false, "", "", initFormat);
}
else if (!fi.exists())
{
std::cerr << "Error: source file does not exists" << std::endl;
return 1;
}
else
{
std::cerr << "Error: source is not a regular file" << std::endl;
return 1;
}
}
if (parser.isSet("enable-writing-system-drives"))
{
std::cerr << "WARNING: writing to system drives is enabled." << std::endl;
}
else
{
DriveListModel dlm;
dlm.processDriveList(Drivelist::ListStorageDevices() );
bool foundDrive = false;
int numDrives = dlm.rowCount( QModelIndex() );
for (int i = 0; i < numDrives; i++)
{
if (dlm.index(i, 0).data(dlm.deviceRole) == args[1])
{
foundDrive = true;
break;
}
}
if (!foundDrive)
{
std::cerr << "Destination drive is not in list of removable volumes. Choose one of the following:" << std::endl << std::endl;
for (int i = 0; i < numDrives; i++)
{
QModelIndex idx = dlm.index(i, 0);
QByteArray line = idx.data(dlm.deviceRole).toByteArray()+" ("+idx.data(dlm.descriptionRole).toByteArray()+")";
std::cerr << line.constData() << std::endl;
}
std::cerr << std::endl << "Or use --enable-writing-system-drives to overrule." << std::endl;
return 1;
}
}
bool isEtherGadgetEnabled = parser.isSet("usb-ether-gadget");
if (!parser.value("cloudinit-userdata").isEmpty())
{
QByteArray userData, networkConfig;
QFile f(parser.value("cloudinit-userdata"));
if (!f.exists())
{
std::cerr << "Error: user-data file does not exists" << std::endl;
return 1;
}
if (f.open(f.ReadOnly))
{
userData = f.readAll();
f.close();
}
else
{
std::cerr << "Error: opening user-data file" << std::endl;
return 1;
}
f.setFileName(parser.value("cloudinit-networkconfig"));
if (!f.exists())
{
std::cerr << "Error: network-config file does not exists" << std::endl;
return 1;
}
if (f.open(f.ReadOnly))
{
networkConfig = f.readAll();
f.close();
}
else
{
std::cerr << "Error: opening network-config file" << std::endl;
return 1;
}
_imageWriter->setImageCustomization("", "", "", userData, networkConfig, false, isEtherGadgetEnabled);
}
else if (!parser.value("first-run-script").isEmpty())
{
if (isEtherGadgetEnabled) {
std::cerr << "Error: the --usb-ether-gadget option is not supported when --first-run-script is used.";
return 1;
}
QByteArray firstRunScript;
QFile f(parser.value("first-run-script"));
if (!f.exists())
{
std::cerr << "Error: firstrun script does not exists" << std::endl;
return 1;
}
if (f.open(f.ReadOnly))
{
firstRunScript = f.readAll();
f.close();
}
else
{
std::cerr << "Error: opening firstrun script" << std::endl;
return 1;
}
_imageWriter->setImageCustomization("", "", firstRunScript, "", "", true, isEtherGadgetEnabled);
}
_imageWriter->setDst(args[1]);
_imageWriter->setVerifyEnabled(!parser.isSet("disable-verify"));
_imageWriter->setEtherGadgetEnabled(isEtherGadgetEnabled);
_imageWriter->setSetting("eject", !parser.isSet("disable-eject"));
/* Run startWrite() in event loop (otherwise calling _app->exit() on error does not work) */
QTimer::singleShot(1, _imageWriter, &ImageWriter::startWrite);
return _app->exec();
}
void Cli::onSuccess()
{
if (!_quiet)
{
_clearLine();
std::cerr << "Write successful." << std::endl;
}
_app->exit(0);
}
void Cli::_clearLine()
{
/* Properly clearing line requires platform specific code.
Just write some spaces for now, and return to beginning of line. */
std::cerr << " \r";
}
void Cli::onError(QVariant msg)
{
QByteArray m = msg.toByteArray();
if (!_quiet)
{
_clearLine();
}
std::cerr << "Error: " << m.constData() << std::endl;
_app->exit(1);
}
void Cli::onDownloadProgress(QVariant dlnow, QVariant dltotal)
{
_printProgress("Writing", dlnow, dltotal);
}
void Cli::onVerifyProgress(QVariant now, QVariant total)
{
_printProgress("Verifying", now, total);
}
void Cli::onPreparationStatusUpdate(QVariant msg)
{
if (!_quiet)
{
QByteArray ascii = QByteArray(" ")+msg.toByteArray()+"\r";
_clearLine();
std::cerr << ascii.constData();
}
}
void Cli::_printProgress(const QByteArray &msg, QVariant now, QVariant total)
{
if (_quiet)
return;
float n = now.toFloat();
float t = total.toFloat();
if (t)
{
int percent = n/t*100;
if (percent != _lastPercent || msg != _lastMsg)
{
QByteArray txt = QByteArray(" ")+msg+": ["+QByteArray(percent/5, '-')+'>'+QByteArray(20-percent/5, ' ')+"] "+QByteArray::number(percent)+" %\r";
std::cerr << txt.constData();
_lastPercent = percent;
_lastMsg = msg;
}
}
else if (msg != _lastMsg)
{
std::cerr << msg.constData() << "\r";
_lastMsg = msg;
}
}