forked from raspberrypi/rpi-imager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driveformatthread.cpp
211 lines (180 loc) · 5.8 KB
/
driveformatthread.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
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2020 Raspberry Pi (Trading) Limited
*/
#include "driveformatthread.h"
#include "dependencies/drivelist/src/drivelist.hpp"
#include "dependencies/mountutils/src/mountutils.hpp"
#include <regex>
#include <QDebug>
#include <QProcess>
#include <QTemporaryFile>
#include <QCoreApplication>
#ifdef Q_OS_LINUX
#include "linux/udisks2api.h"
#include <unistd.h>
#endif
DriveFormatThread::DriveFormatThread(const QByteArray &device, QObject *parent)
: QThread(parent), _device(device)
{
}
DriveFormatThread::~DriveFormatThread()
{
wait();
}
void DriveFormatThread::run()
{
#ifdef Q_OS_WIN
std::regex windriveregex("\\\\\\\\.\\\\PHYSICALDRIVE([0-9]+)", std::regex_constants::icase);
std::cmatch m;
if (std::regex_match(_device.constData(), m, windriveregex))
{
QByteArray nr = QByteArray::fromStdString(m[1]);
qDebug() << "Formatting Windows drive #" << nr << "(" << _device << ")";
QProcess proc;
QByteArray diskpartCmds =
"select disk "+nr+"\r\n"
"clean\r\n"
"create partition primary\r\n"
"select partition 1\r\n"
"set id=0e\r\n"
"assign\r\n";
proc.start("diskpart");
proc.waitForStarted();
proc.write(diskpartCmds);
proc.closeWriteChannel();
proc.waitForFinished();
QByteArray output = proc.readAllStandardError();
qDebug() << output;
qDebug() << "Done running diskpart. Exit status code =" << proc.exitCode();
if (proc.exitCode())
{
emit error(tr("Error partitioning: %1").arg(QString(output)));
}
else
{
auto l = Drivelist::ListStorageDevices();
QByteArray devlower = _device.toLower();
for (auto i : l)
{
if (QByteArray::fromStdString(i.device).toLower() == devlower && i.mountpoints.size() == 1)
{
QByteArray driveLetter = QByteArray::fromStdString(i.mountpoints.front());
if (driveLetter.endsWith("\\"))
driveLetter.chop(1);
qDebug() << "Drive letter of device:" << driveLetter;
QProcess f32format;
QStringList args;
args << "-y" << driveLetter;
f32format.start(QCoreApplication::applicationDirPath()+"/fat32format.exe", args);
if (!f32format.waitForStarted())
{
emit error(tr("Error starting fat32format"));
return;
}
//f32format.write("y\r\n");
f32format.closeWriteChannel();
f32format.waitForFinished(120000);
if (f32format.exitStatus() || f32format.exitCode())
{
emit error(tr("Error running fat32format: %1").arg(QString(f32format.readAll())));
}
else
{
emit success();
}
return;
}
}
emit error(tr("Error determining new drive letter"));
}
}
else
{
emit error(tr("Invalid device: %1").arg(QString(_device)));
}
#elif defined(Q_OS_DARWIN)
QProcess proc;
QStringList args;
args << "eraseDisk" << "FAT32" << "SDCARD" << "MBRFormat" << _device;
proc.start("diskutil", args);
proc.waitForFinished();
QByteArray output = proc.readAllStandardError();
qDebug() << args;
qDebug() << "diskutil output:" << output;
if (proc.exitCode())
{
emit error(tr("Error partitioning: %1").arg(QString(output)));
}
else
{
emit success();
}
#elif defined(Q_OS_LINUX)
if (::access(_device, W_OK) != 0)
{
/* Not running as root, try to outsource formatting to udisks2 */
UDisks2Api udisks2;
if (udisks2.formatDrive(_device))
{
emit success();
}
else
{
emit error(tr("Error formatting (through udisks2)"));
}
return;
}
QProcess proc;
QByteArray partitionTable;
QStringList args;
QByteArray fatpartition = _device;
partitionTable = "8192,,0E\n"
"0,0\n"
"0,0\n"
"0,0\n";
args << "-uS" << _device;
if (isdigit(fatpartition.at(fatpartition.length()-1)))
fatpartition += "p1";
else
fatpartition += "1";
unmount_disk(_device);
proc.setProcessChannelMode(proc.MergedChannels);
proc.start("sfdisk", args);
if (!proc.waitForStarted())
{
emit error(tr("Error starting sfdisk"));
return;
}
proc.write(partitionTable);
proc.closeWriteChannel();
proc.waitForFinished();
QByteArray output = proc.readAll();
qDebug() << "sfdisk:" << output;
if (proc.exitCode())
{
emit error(tr("Error partitioning: %1").arg(QString(output)));
return;
}
proc.execute("partprobe");
args.clear();
args << fatpartition;
proc.start("mkfs.fat", args);
if (!proc.waitForStarted())
{
emit error(tr("Error starting mkfs.fat"));
return;
}
proc.waitForFinished();
output = proc.readAll();
qDebug() << "mkfs.fat:" << output;
if (proc.exitCode())
{
emit error(tr("Error running mkfs.fat: %1").arg(QString(output)));
return;
}
emit success();
#else
emit error(tr("Formatting not implemented for this platform"));
#endif
}