Debian Codegraph @

src/boot.cpp

raw symbols SCIP
1 // Copyright (C) 2014 Sacha Refshauge
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 3.0.
6
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 3.0 for more details.
11
12 // A copy of the GPL 3.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14
15 // Official GIT repository and contact information can be found at
16 // http://github.com/xsacha/Sachesi
17
18 #include "ports.h"
19 #include "boot.h"
20 #include "installer.h"
21 #include <QDebug>
22 #include <QMessageBox>
23 #include <QProcess>
24 #include <QBuffer>
25 #include <QDataStream>
26
27 Boot::Boot() : _bootloaderMode(0xFF), _connecting(false), _command(0), _rebootAfter(false), _packetNum(0), _kill(false)
28 {
29 bootloaderModeData.append(new QByteArray("RIM REINIT\0\0\0\0\0\0\1", 17));
30 bootloaderModeData.append(new QByteArray("RIM-BootLoader\0\0\1",17));
31 bootloaderModeData.append(new QByteArray("RIM-RAMLoader\0\0\0\1", 17));
32 bootloaderModeData.append(new QByteArray("RIM UPL\0\0\0\0\0\0\0\0\0\1", 17));
33 bootloaderModeData.append(new QByteArray("RIM-BootNUKE\0\0\0\0\1", 17));
34
35 // If Android does not have USB permissions, this will fail and we need to handle it.
36 _kill = libusb_init(nullptr) != 0;
37 // Should probably just shut down and notify qml if _kill is false at this stage.
38 }
39
40 Boot::~Boot()
41 {
42 libusb_exit(nullptr);
43 }
44
45 libusb_device_handle* Boot::openDevice(libusb_device* dev) {
46 int configuration = 0;
47 libusb_device_handle* handle = nullptr;
48
49 libusb_open(dev, &handle);
50 if (handle == nullptr) {
51 #ifdef _WIN32
52 QMessageBox::information(nullptr, "Error", "You need to install WinUSB driver for the Blackberry device.");
53 #else
54 QMessageBox::information(nullptr, "Error", "You need to run this application with root privileges (i.e. sudo).");
55 #endif
56 libusb_close(handle);
57 return nullptr;
58 }
59
60 libusb_get_configuration(handle, &configuration);
61
62 libusb_detach_kernel_driver(handle, 1);
63 if(configuration != 1) {
64 if (libusb_set_configuration(handle, 1) < 0) {
65 QMessageBox::information(nullptr, "Error", "Error #1001");
66 return nullptr;
67 }
68 }
69
70 libusb_detach_kernel_driver(handle, 0);
71 int err = libusb_claim_interface(handle, 0);
72 if (err < 0) {
73 #ifdef Q_OS_MAC
74 QMessageBox::information(nullptr, "Error", "Please reboot your device manually.");
75 return nullptr;
76 #else
77 QMessageBox::information(nullptr, "Error", "Error #1002 (" + QString::number(err) + ")");
78 return nullptr;
79 #endif
80 }
81 return handle;
82 }
83
84 int Boot::closeDevice(libusb_device_handle* handle) {
85 if (handle == nullptr) {
86 return -1;
87 }
88
89 libusb_release_interface(handle, 0);
90 libusb_release_interface(handle, 1);
91 libusb_close(handle);
92 return 0;
93 }
94
95 int Boot::sendControlMessage(libusb_device_handle* aHandle, uint8_t aCommand, QByteArray* aData, int aDataSize) {
96 QByteArray buffer(aDataSize + 8, 0);
97 ControlMessageHeader* header = (ControlMessageHeader*)buffer.data();
98 header->type = 0x00;
99 header->packetSize = aDataSize + 8;
100 header->command = aCommand;
101 header->mode = _bootloaderMode;
102 header->packetId = _packetNum++;
103 if (aData)
104 memcpy((unsigned char*)buffer.data()+8, (const unsigned char*)aData->constData(), aDataSize);
105
106 int transferred = 0;
107 int err = libusb_bulk_transfer(aHandle, _found == 0x1 ? 0x2 : 0x1, (unsigned char*)buffer.data(), aDataSize + 8, &transferred, 1000);
108 if (err == -7)
109 {
110 QMessageBox::information(nullptr, "Error", "Was not able to send message to connected device.");
111 return -7;
112 }
113
114 return transferred;
115 }
116
117 int Boot::receiveControlMessage(libusb_device_handle* aHandle, ControlMessageHeader* aHeader, QByteArray* aData) {
118 QByteArray buffer(BUFFER_SIZE, 0);
119 int transferred = 0;
120 int ret = libusb_bulk_transfer(aHandle, _found == 0x1 ? 0x82 : 0x81, (unsigned char*)buffer.data(), BUFFER_SIZE, &transferred, 1000);
121 if (ret == -7) {
122 if (_found != 0x1) {
123 QMessageBox::information(nullptr, "Error", "Try rebooting your device manually.");
124 } else {
125 QMessageBox::information(nullptr, "Error", "The connected device did not respond as expected.");
126 }
127 return -7;
128 }
129
130 if (transferred < 8)
131 return 0;
132
133 /*qDebug() << "Header: " << buffer.mid(4,4).toHex();
134 if (transferred > 8) {
135 if ((transferred - 8) < 40)
136 qDebug() << "Text: " << buffer.mid(8,transferred - 8);
137 qDebug() << "Hex: " << buffer.mid(8,transferred - 8).toHex();
138 }*/
139
140
141 if (aHeader)
142 memcpy(aHeader, (const unsigned char*)buffer.constData(), 8);
143 if (aData)
144 memcpy((unsigned char*)aData->data(), (const unsigned char*)buffer.constData() + 8, transferred - 8);
145
146 return transferred;
147 }
148
149 void Boot::commandReboot(libusb_device_handle* aHandle) {
150 sendControlMessage(aHandle, kCommandReboot, nullptr, 0); // reboots the device (message 00 command 03)
151 receiveControlMessage(aHandle, nullptr, nullptr); // discard the reboot confirmation message (message 00 command 04)
152 }
153
154 void Boot::commandPing(libusb_device_handle* aHandle) {
155 sendControlMessage(aHandle, kCommandPing, nullptr, 0); // reboots the device (message 00 command 03)
156 receiveControlMessage(aHandle, nullptr, nullptr); // discard the reboot confirmation message (message 00 command 04)
157 }
158
159 int Boot::commandGetVariable(libusb_device_handle* aHandle, int variable, QByteArray* buffer, int bufferSize) {
160 // gets a variable from the device (message 00 command 05)
161 // returns the amount of data received
162 // 0-1: buffer size
163 // 2-3: variable
164 uint16_t data[2];
165 data[0] = bufferSize;
166 data[1] = variable;
167 QByteArray newBuf((const char*)data, 4);
168 int err = sendControlMessage(aHandle, kCommandGetVariable, &newBuf, 4);
169 if (err == -7)
170 return -7;
171
172 err = receiveControlMessage(aHandle, nullptr, buffer);
173 if (err == -7)
174 return -7;
175
176
177 return err - 8;
178 }
179
180 void Boot::commandSetMode(libusb_device_handle* aHandle, BootloaderMode mode) {
181 sendControlMessage(aHandle, kCommandSetMode, bootloaderModeData.at(mode), 16); // set the bootloader mode on the device (message 00 command 07)
182
183 // update the bootloader mode
184 ControlMessageHeader header;
185 receiveControlMessage(aHandle, &header, nullptr);
186 _bootloaderMode = header.mode;
187 }
188
189 void Boot::commandLoadBootloader(libusb_device_handle* aHandle) {
190 sendControlMessage(aHandle, kCommandLoadTransferredData, nullptr, 0); // loads the usb bootloader or reboots (message 00 command 0B) // needs to be in mode 01 (RIM-BootLoader) to work
191 receiveControlMessage(aHandle, nullptr, nullptr); // discard the mode confirmation message (message 00 command 0C)
192 }
193
194 void Boot::commandTransferData(libusb_device_handle* aHandle) {
195 sendControlMessage(aHandle, kCommandReadyForDataTransfer, nullptr, 0); // loads the usb bootloader or reboots (message 00 command 0B) // needs to be in mode 01 (RIM-BootLoader) to work
196 receiveControlMessage(aHandle, nullptr, nullptr);
197 }
198
199 bool Boot::commandSendPass(libusb_device_handle* aHandle) {
200 sendControlMessage(aHandle, kCommandGetPasswordInfo, nullptr, 0); // get the device password info (message 00 command 0A) // needs to be in mode 01 (RIM-BootLoader) to work
201 QByteArray challengeData(100,0);
202 receiveControlMessage(aHandle, nullptr, &challengeData); // discard the mode confirmation message (message 00 command 0E)
203
204 QByteArray challenge = challengeData.mid(4, 4);
205 QByteArray salt = challengeData.mid(12,8);
206 QByteArray countData = challengeData.mid(20,4);
207 QDataStream stream(countData);
208 stream.setByteOrder(QDataStream::LittleEndian);
209 int iterations;
210 stream >> iterations;
211 QByteArray hashedData = QByteArray(_password.toLatin1());
212 int count = 0;
213 bool challenger = true;
214 do {
215 QByteArray buf(4 + salt.length() + hashedData.length(),0);
216 QDataStream buffer(&buf,QIODevice::WriteOnly);
217 buffer.setByteOrder(QDataStream::LittleEndian);
218 buffer << qint32(count);
219 buffer.writeRawData(salt, salt.length());
220 buffer.writeRawData(hashedData, hashedData.length());
221 if (!count) hashedData.resize(64);
222 SHA512((const unsigned char*)buf.data(), buf.length(), (unsigned char *)hashedData.data());
223 if ((count == iterations - 1) && challenger)
224 {
225 count = -1;
226 challenger = false;
227 hashedData.prepend(challenge);
228 }
229 } while (++count < iterations);
230 hashedData.prepend(QByteArray::fromHex("00004000"),4);
231
232 sendControlMessage(aHandle, kCommandSendPassword, &hashedData, 68);
233 ControlMessageHeader header;
234 receiveControlMessage(aHandle, &header, nullptr);
235 if (header.command == 0x10)
236 return true;
237 else {
238 QMessageBox::information(nullptr,"Wrong Password.","You entered the wrong password. I don't handle this yet but basically you will need to go to the Install tab, fix your password, then try again.");
239 return false;
240 }
241 }
242
243 void Boot::commandSendLoader(libusb_device_handle *aHandle)
244 {
245 // Should be telling us it's ready.
246 ControlMessageHeader header;
247 receiveControlMessage(aHandle, &header, nullptr);
248 if (header.command != kCommandReadyForDataTransfer)
249 return;
250 int transferred = 0;
251 unsigned char startSend[] = {0x1, 0x0, 0x10, 0x0, 0x7B, 0x9, 0x2B, 0x96, 0xC, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0xF0};
252 libusb_bulk_transfer(aHandle, 0x2, startSend, 16, &transferred, 1000);
253 receiveControlMessage(aHandle, &header, nullptr); // Should give Info basically
254 }
255
256 void Boot::search() {
257 if (_kill)
258 return;
259
260 libusb_device_handle* handle = nullptr;
261 libusb_device *dev = nullptr;
262 libusb_device **list;
263 libusb_get_device_list(nullptr, &list);
264 _found = 0;
265 struct libusb_device_descriptor desc;
266 _devices.clear();
267 for (int i = 0; (dev = list[i]) != nullptr; i++) {
268 libusb_get_device_descriptor(dev, &desc);
269 if (desc.idVendor == BLACKBERRY_VENDOR_ID) {
270 _devices.append(QString::number(desc.idProduct,16));
271 if (connecting()) {
272 _found = desc.idProduct;
273 handle = openDevice(list[i]);
274 // We need to reboot it to talk to it
275 if (desc.idProduct != 0x1) {
276 if (handle != nullptr) {
277 commandReboot(handle);
278 closeDevice(handle);
279 } else
280 _found = 0;
281 }
282 }
283 break;
284 }
285 }
286 libusb_free_device_list(list, 1);
287 emit devicesChanged();
288 if (_found != 0x1) {
289 // We'll be rebooting this guy
290 QTimer::singleShot(10000, this, SLOT(search()));
291 return;
292 }
293 QTimer::singleShot(1500, this, SLOT(search()));
294 if (handle == nullptr) {
295 if (_found == 0x1)
296 setConnecting(false);
297 return;
298 }
299 setConnecting(false);
300
301 switch(command()) {
302 case commandInfo: {
303 commandSetMode(handle, kBootloaderRimBoot);
304 QByteArray buffer(2000, 0);
305 int received = commandGetVariable(handle, 2, &buffer, 2000);
306 if (received == -7)
307 return;
308 QFile info(getSaveDir() + "info.txt");
309 info.open(QIODevice::WriteOnly);
310 QBuffer bufferStream(&buffer);
311 bufferStream.open(QIODevice::ReadOnly);
312 QDataStream stream(&bufferStream);
313 stream.setByteOrder(QDataStream::LittleEndian);
314 short messageSize;
315 stream >> messageSize;
316 info.write(QByteArray("Message Size: ") + QByteArray::number(messageSize) + "\n");
317 bufferStream.seek(16);
318 unsigned int pin;
319 stream >> pin;
320 info.write(QByteArray("Hardware ID: 0x") + QByteArray::number(pin, 16) + " " + bufferStream.read(64).split('\0').first() + "\n");
321 info.write(QByteArray("Build User: ") + bufferStream.read(16).split('\0').first() + "\n");
322 info.write(QByteArray("Build Date: ") + bufferStream.read(16).split('\0').first() + "\n");
323 info.write(QByteArray("Build Time: ") + bufferStream.read(16).split('\0').first() + "\n");
324 unsigned int unk1;
325 stream >> unk1;
326 info.write(QByteArray("Unknown value: 0x" + QByteArray::number(unk1, 16)) + "\n");
327 bufferStream.seek(188);
328 unsigned int baseId, brId;
329 stream >> baseId;
330 stream >> brId;
331 info.write(QByteArray("Hardware OS ID: 0x" + QByteArray::number(baseId, 16)) + "\n");
332 info.write(QByteArray("BR ID: 0x" + QByteArray::number(brId, 16)) + "\n");
333 info.write(buffer.mid(bufferStream.pos(), received - bufferStream.pos()));
334 bufferStream.close();
335 openFile(info.fileName());
336 info.close();
337 }
338 break;
339 case commandBootloader:
340 commandSetMode(handle, kBootloaderRimBoot);
341 commandSendPass(handle);
342 if (commandSendPass(handle))
343 commandSendLoader(handle);
344 break;
345 case commandNuke:
346 commandSetMode(handle, kBootloaderRimBoot);
347 commandSetMode(handle, kBootloaderRimBootNuke);
348 break;
349 case commandDebug:
350 _found = 8000;
351 commandSetMode(handle, kBootloaderRimBoot);
352 break;
353 default:
354 commandSetMode(handle, kBootloaderRimBoot);
355 break;
356 }
357 if (_rebootAfter)
358 commandReboot(handle);
359
360 closeDevice(handle);
361 }
362
363 void Boot::connect() {
364 setConnecting(true);
365 }
366
367 void Boot::disconnect() {
368 setConnecting(false);
369 }
370
371 void Boot::newPassword(QString newPass) {
372 _password = newPass;
373 }
374
375 void Boot::setCommandMode(int mode, bool rebootAfter) {
376 setCommand(mode);
377 connect();
378 _rebootAfter = rebootAfter;
379 }
380
381 #define SET_QML2(type, name, caps) \
382 type Boot::name() const { \
383 return _ ## name; \
384 } \
385 void Boot::caps(const type &var) { \
386 if (var != _ ## name) { \
387 _ ## name = var; \
388 emit name ## Changed(); \
389 } \
390 }
391
392 SET_QML2(bool, connecting, setConnecting)
393 SET_QML2(int, command, setCommand)