-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusbdump.cpp
More file actions
250 lines (207 loc) · 6.17 KB
/
usbdump.cpp
File metadata and controls
250 lines (207 loc) · 6.17 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
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
#include "usbdump.h"
#include "usb/inlretro.h"
#include <qmessagebox.h>
#include <qfiledialog.h>
#include <qdebug.h>
// uncomment to try to detect valid flash memory
//#define DETECT_MEMORY_PACK
// ----------------------------------------------------------------------------
USBDumpDialog::USBDumpDialog(USBDevice::DeviceType deviceType, QWidget *parent)
: QDialog(parent)
, deviceType(deviceType)
, dumpThread(nullptr)
{
ui.setupUi(this);
ui.buttonCancel->hide();
connect(ui.buttonBrowse, SIGNAL(clicked(bool)), this, SLOT(browse()));
connect(ui.buttonStartDump, SIGNAL(clicked(bool)), this, SLOT(startDump()));
connect(ui.buttonCancel, SIGNAL(clicked(bool)), this, SLOT(cancelDump()));
connect(ui.buttonClose, SIGNAL(clicked(bool)), this, SLOT(reject()));
showMessage(tr("Press Start to begin dumping."));
showMessage(tr("If the file is dumped successfully, it will be automatically opened in the main window."));
}
// ----------------------------------------------------------------------------
QString USBDumpDialog::dump()
{
if (this->browse() && this->exec())
{
return ui.editOutputPath->text();
}
return QString();
}
// ----------------------------------------------------------------------------
void USBDumpDialog::reject()
{
cancelDump();
if (dumpThread != nullptr)
{
dumpThread->wait();
}
QDialog::reject();
}
// ----------------------------------------------------------------------------
bool USBDumpDialog::browse()
{
QString path = QFileDialog::getSaveFileName(this,
this->windowTitle(), ui.editOutputPath->text(), "(*.bs)");
if (!path.isEmpty())
{
ui.editOutputPath->setText(path);
return true;
}
return false;
}
// ----------------------------------------------------------------------------
void USBDumpDialog::startDump()
{
QString outPath = ui.editOutputPath->text();
if (outPath.isEmpty())
{
QMessageBox::warning(this, this->windowTitle(),
tr("Output file name cannot be empty."));
return;
}
// UI setup to start dumping
ui.editOutputPath->setEnabled(false);
ui.buttonBrowse->setEnabled(false);
ui.buttonStartDump->hide();
ui.buttonCancel->show();
ui.progressBar->setRange(0, 0);
dumpThread = new USBDumpThread(deviceType, outPath, this);
connect(dumpThread, SIGNAL(showMessage(QString)), this, SLOT(showMessage(QString)));
connect(dumpThread, SIGNAL(dumpProgress(int, int)), this, SLOT(setProgress(int, int)));
connect(dumpThread, SIGNAL(dumpFinished()), this, SLOT(accept()));
dumpThread->start();
while (!dumpThread->isFinished())
{
qApp->processEvents();
}
delete dumpThread;
dumpThread = nullptr;
// restore UI
ui.editOutputPath->setEnabled(true);
ui.buttonBrowse->setEnabled(true);
ui.buttonStartDump->show();
ui.buttonCancel->hide();
ui.progressBar->setRange(0, 1);
ui.progressBar->setTextVisible(false);
}
// ----------------------------------------------------------------------------
void USBDumpDialog::cancelDump()
{
if (dumpThread != nullptr)
{
dumpThread->requestInterruption();
}
}
// ----------------------------------------------------------------------------
void USBDumpDialog::setProgress(int val, int max)
{
ui.progressBar->setRange(0, max);
ui.progressBar->setValue(val);
ui.progressBar->setTextVisible(true);
}
// ----------------------------------------------------------------------------
void USBDumpDialog::showMessage(const QString& msg)
{
qDebug() << msg;
ui.editDumpLog->appendPlainText(msg);
}
// ----------------------------------------------------------------------------
USBDumpThread::USBDumpThread(USBDevice::DeviceType deviceType, const QString &outPath, QObject *parent)
: QThread(parent)
, outPath(outPath)
{
switch (deviceType)
{
case USBDevice::INLRetro:
this->usbDevice = new INLRetroDevice(this);
break;
default:
this->usbDevice = nullptr;
}
if (this->usbDevice)
{
connect(this->usbDevice, SIGNAL(usbLogMessage(QString)), this, SIGNAL(showMessage(QString)));
}
}
// ----------------------------------------------------------------------------
void USBDumpThread::run()
{
QFile file(outPath);
bool ok = true;
if (this->usbDevice->open() && file.open(QFile::WriteOnly))
{
emit showMessage(tr("USB device opened successfully."));
#ifdef DETECT_MEMORY_PACK
// first try to detect memory pack
// memory pack write enable
this->usbDevice->writeByte(0x0c, 0x5000, 0x80);
this->usbDevice->writeByte(0x0e, 0x5000, 0x00);
// restore default page buffer settings
this->usbDevice->writeByte(0xc0, 0x0000, 0x38);
this->usbDevice->writeByte(0xc0, 0x0000, 0xd0);
// swap in the vendor info page in the flash chip and see what we find
this->usbDevice->writeByte(0xc0, 0x0000, 0x72);
this->usbDevice->writeByte(0xc0, 0x0000, 0x75);
QByteArray flashInfo = this->usbDevice->readBytes(0xc0, 0xff00, 16);
this->usbDevice->writeByte(0xc0, 0x0000, 0xff);
// memory pack write disable
this->usbDevice->writeByte(0x0c, 0x5000, 0x00);
this->usbDevice->writeByte(0x0e, 0x5000, 0x00);
if (flashInfo.size() < 16
|| flashInfo[0] != 'M'
|| flashInfo[2] != 'P'
|| flashInfo[4] & 0x80)
{
emit showMessage(tr("No valid memory pack detected."));
ok = false;
}
quint8 flashType = (uchar)flashInfo[6] >> 4;
quint8 flashSize = 2 << (((uchar)flashInfo[6] & 0x0f) - 8);
if (ok) {
if (flashSize > 32)
{
qDebug() << "warning: bogus flash size" << flashSize << "blocks";
flashSize = 8;
}
emit showMessage(tr("Type %1 memory pack detected, %2 blocks.").arg(flashType).arg(flashSize));
}
#else
quint8 flashSize = 8;
#endif
for (unsigned i = 0; ok && i < flashSize << 1; i++)
{
if (!(i & 1))
{
emit showMessage(tr("Dumping block %1 of %2...").arg((i >> 1) + 1).arg(flashSize));
}
emit dumpProgress(i, flashSize << 1);
file.write(this->usbDevice->readBytes(0xc0 + i, 0x0000, 1 << 16, &ok));
yieldCurrentThread();
if (this->isInterruptionRequested())
{
ok = false;
}
}
if (this->isInterruptionRequested())
{
emit showMessage(tr("Dump cancelled."));
}
else if (ok)
{
emit showMessage(tr("Full file dumped successfully."));
emit dumpFinished();
}
else
{
emit showMessage(tr("File dump failed."));
}
}
else
{
emit showMessage(tr("USB device open failed."));
ok = false;
}
this->usbDevice->close();
}