-
Notifications
You must be signed in to change notification settings - Fork 18
/
diskwriter_windows.cpp
219 lines (194 loc) · 6.8 KB
/
diskwriter_windows.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
#include "diskwriter_windows.h"
#include <QDebug>
#include <QMessageBox>
DiskWriter_windows::DiskWriter_windows(QObject *parent) :
DiskWriter(parent),
hVolume(INVALID_HANDLE_VALUE),
hRawDisk(INVALID_HANDLE_VALUE)
{
isCancelled = false;
}
DiskWriter_windows::~DiskWriter_windows()
{
if (isOpen()) {
close();
}
}
bool DiskWriter_windows::open(const QString &device)
{
hVolume = getHandleOnVolume(device, GENERIC_WRITE);
if (hVolume == INVALID_HANDLE_VALUE) {
return false;
}
if (!getLockOnVolume(hVolume)) {
close();
return false;
}
if (isVolumeMounted(hVolume) && !unmountVolume(hVolume)) {
close();
return false;
}
hRawDisk = getHandleOnDevice(device, GENERIC_WRITE);
if (hRawDisk == INVALID_HANDLE_VALUE) {
close();
return false;
}
return true;
}
void DiskWriter_windows::close()
{
if (hRawDisk != INVALID_HANDLE_VALUE) {
CloseHandle(hRawDisk);
hRawDisk = INVALID_HANDLE_VALUE;
}
if (hVolume != INVALID_HANDLE_VALUE) {
removeLockOnVolume(hVolume);
CloseHandle(hVolume);
hVolume = INVALID_HANDLE_VALUE;
}
}
void DiskWriter_windows::sync()
{
FlushFileBuffers(hVolume);
}
bool DiskWriter_windows::isOpen()
{
return (hRawDisk != INVALID_HANDLE_VALUE && hVolume != INVALID_HANDLE_VALUE);
}
bool DiskWriter_windows::write(const QByteArray &data)
{
DWORD byteswritten;
bool ok;
ok = WriteFile(hRawDisk, data.constData(), data.size(), &byteswritten, NULL);
if (!ok) {
const DWORD e = GetLastError();
const QString errText = errorAsString(e);
QMessageBox::critical(NULL, QObject::tr("Write Error"),
QObject::tr("An error occurred when attempting to write data to handle.\n"
"Error %1: %2").arg(e).arg(errText));
}
return ok;
}
// Adapted from win32 DiskImager
HANDLE DiskWriter_windows::getHandleOnDevice(const QString& device, DWORD access)
{
HANDLE hDevice;
const QString devicename = QString("\\\\.\\PhysicalDrive%1").arg(deviceNumberFromName(device));
qDebug() << devicename;
hDevice = CreateFile(devicename.toStdWString().c_str(), access, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
const DWORD e = GetLastError();
const QString errText = errorAsString(e);
QMessageBox::critical(NULL, QObject::tr("Device Error"),
QObject::tr("An error occurred when attempting to get a handle on the device.\n"
"Error %1: %2").arg(e).arg(errText));
}
return hDevice;
}
// Adapted from win32 DiskImager
HANDLE DiskWriter_windows::getHandleOnVolume(const QString &volume, DWORD access)
{
HANDLE hVolume;
QString volumename = "\\\\.\\" + volume;
if (volumename.endsWith("\\")) {
volumename.chop(1);
}
qDebug() << volumename;
hVolume = CreateFile(volumename.toStdWString().c_str(), access, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hVolume == INVALID_HANDLE_VALUE) {
const DWORD e = GetLastError();
const QString errText = errorAsString(e);
QMessageBox::critical(NULL, QObject::tr("Volume Error"),
QObject::tr("An error occurred when attempting to get a handle on the volume.\n"
"Error %1: %2").arg(e).arg(errText));
}
return hVolume;
}
// Adapted from win32 DiskImager
bool DiskWriter_windows::getLockOnVolume(HANDLE handle) const
{
DWORD junk;
bool ok;
ok = DeviceIoControl(handle, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &junk, NULL);
if (!ok) {
const DWORD e = GetLastError();
const QString errText = errorAsString(e);
QMessageBox::critical(NULL, QObject::tr("Lock Error"),
QObject::tr("An error occurred when attempting to lock the volume.\n"
"Error %1: %2").arg(e).arg(errText));
}
return ok;
}
// Adapted from win32 DiskImager
bool DiskWriter_windows::removeLockOnVolume(HANDLE handle) const
{
DWORD junk;
bool ok;
ok = DeviceIoControl(handle, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &junk, NULL);
if (!ok) {
const DWORD e = GetLastError();
const QString errText = errorAsString(e);
QMessageBox::critical(NULL, QObject::tr("Unlock Error"),
QObject::tr("An error occurred when attempting to unlock the volume.\n"
"Error %1: %2").arg(e).arg(errText));
}
return ok;
}
// Adapted from win32 DiskImager
bool DiskWriter_windows::unmountVolume(HANDLE handle) const
{
DWORD junk;
bool ok;
ok = DeviceIoControl(handle, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &junk, NULL);
if (!ok) {
const DWORD e = GetLastError();
const QString errText = errorAsString(e);
QMessageBox::critical(NULL, QObject::tr("Dismount Error"),
QObject::tr("An error occurred when attempting to dismount the volume.\n"
"Error %1: %2").arg(e).arg(errText));
}
return ok;
}
// Adapted from win32 DiskImager
bool DiskWriter_windows::isVolumeMounted(HANDLE handle) const
{
DWORD junk;
return DeviceIoControl(handle, FSCTL_IS_VOLUME_MOUNTED, NULL, 0, NULL, 0, &junk, NULL);
}
ULONG DiskWriter_windows::deviceNumberFromName(const QString &device)
{
QString volumename = "\\\\.\\" + device;
if (volumename.endsWith("\\")) {
volumename.chop(1);
}
HANDLE h = ::CreateFile(volumename.toStdWString().c_str(), 0, 0, NULL, OPEN_EXISTING, 0, NULL);
STORAGE_DEVICE_NUMBER info;
DWORD bytesReturned = 0;
::DeviceIoControl(h, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &info, sizeof(info), &bytesReturned, NULL);
CloseHandle(h);
return info.DeviceNumber;
}
QString DiskWriter_windows::errorAsString(DWORD error)
{
if(error == 0) {
return QString();
}
QString message;
LPSTR messageBuffer = NULL;
DWORD ret = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | // The function will allocate space for pBuffer.
FORMAT_MESSAGE_FROM_SYSTEM | // System wide message.
FORMAT_MESSAGE_IGNORE_INSERTS, // No inserts.
NULL, // Message is not in a module.
error, // Message identifier.
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language.
(LPTSTR)&messageBuffer, // Buffer to hold the text string.
256, // The function will allocate at least this much for pBuffer.
NULL // No inserts.
);
if (ret) {
message = QString::fromUtf16((const ushort*)messageBuffer, ret);
}
LocalFree(messageBuffer);
return message;
}