summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/base/bootloaderinstalls5l.cpp
blob: ef1ab1077c6aada3c3176ace74f75199bc6f2980 (plain)
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 *   Copyright (C) 2008 by Dominik Riebeling
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <QtCore>
#include "bootloaderinstallbase.h"
#include "bootloaderinstalls5l.h"
#include "Logger.h"
#include "utils.h"
#include "system.h"
#include "rbsettings.h"
#include "systeminfo.h"

#include "../mks5lboot/mks5lboot.h"


BootloaderInstallS5l::BootloaderInstallS5l(QObject *parent)
        : BootloaderInstallBase(parent)
{
}


bool BootloaderInstallS5l::install(void)
{
    LOG_INFO() << "installing bootloader";
    doInstall = true;
    return installStage1();
}


bool BootloaderInstallS5l::uninstall(void)
{
    LOG_INFO() << "uninstalling bootloader";
    doInstall = false;
    return installStage1();
}


bool BootloaderInstallS5l::installStage1(void)
{
    LOG_INFO() << "installStage1";

    mntpoint = RbSettings::value(RbSettings::Mountpoint).toString();

    if (!Utils::mountpoints(Utils::MountpointsSupported).contains(mntpoint)) {
        LOG_ERROR() << "iPod not mounted:" << mntpoint;
        emit logItem(tr("Could not find mounted iPod."), LOGERROR);
        emit done(true);
        return false;
    }

    if (doInstall) {
        // download firmware from server
        emit logItem(tr("Downloading bootloader file..."), LOGINFO);
        connect(this, SIGNAL(downloadDone()), this, SLOT(installStageMkdfu()));
        downloadBlStart(m_blurl);
    }
    else {
        installStageMkdfu();
    }

    return true;
}


void BootloaderInstallS5l::installStageMkdfu(void)
{
    int dfu_type;
    QString dfu_arg;
    char errstr[200];

    LOG_INFO() << "installStageMkdfu";

    setProgress(0);
    aborted = false;
    connect(this, SIGNAL(installAborted()), this, SLOT(abortInstall()));
    connect(this, SIGNAL(done(bool)), this, SLOT(installDone(bool)));

    if (doInstall) {
        dfu_type = DFU_INST;
        m_tempfile.open();
        dfu_arg = m_tempfile.fileName();
        m_tempfile.close();
    }
    else {
        dfu_type = DFU_UNINST;
        dfu_arg = RbSettings::value(RbSettings::Platform).toString();
    }

    // build DFU image
    dfu_buf = mkdfu(dfu_type, dfu_arg.toLocal8Bit().data(),
                                &dfu_size, errstr, sizeof(errstr));
    if (!dfu_buf) {
        LOG_ERROR() << "mkdfu() failed:" << errstr;
        emit logItem(errstr, LOGERROR);
        emit logItem(tr("Could not make DFU image."), LOGERROR);
        emit done(true);
        return;
    }

    LOG_INFO() << "preparing installStageWaitForEject";
    emit logItem(tr("Ejecting iPod..."), LOGINFO);
    setProgress(10);
    scanTimer.invalidate();
    installStageWaitForEject();
}


void BootloaderInstallS5l::installStageWaitForEject(void)
{
    if (!updateProgress())
        return; /* aborted */

    if (!scanTimer.isValid() || (scanTimer.elapsed() > 3000)) {
        scanSuccess = Utils::ejectDevice(mntpoint);
        if (!scanSuccess) {
            scanSuccess = !Utils::mountpoints(
                        Utils::MountpointsSupported).contains(mntpoint);
        }
        scanTimer.start();
    }
    if (!scanSuccess) {
        if (!actionShown) {
            emit logItem(tr("Action required:\n"
                            "  Please make sure no programs are accessing\n"
                            "  files on the device. If ejecting still fails\n"
                            "  please use your computers eject funtionality."),
                            LOGWARNING);
            actionShown = true;
        }
        QTimer::singleShot(250, this, SLOT(installStageWaitForEject()));
        return;
    }
    emit logItem(tr("Device successfully ejected."), LOGINFO);

    LOG_INFO() << "preparing installStageWaitForProcs";
    setProgress(40, 18);
    scanTimer.invalidate();
    installStageWaitForProcs();
}


void BootloaderInstallS5l::installStageWaitForProcs(void)
{
    if (!updateProgress())
        return; /* aborted */

    if (!scanTimer.isValid() || (scanTimer.elapsed() > 1000)) {
        scanSuccess = Utils::findRunningProcess(QStringList("iTunes")).isEmpty();
        scanTimer.start();
    }
    if (!scanSuccess) {
        if (!actionShown) {
            emit logItem(tr("Action required:\n"
                            "  Quit iTunes application."), LOGWARNING);
            actionShown = true;
        }
        QTimer::singleShot(250, this, SLOT(installStageWaitForProcs()));
        return;
    }
    if (actionShown) {
        emit logItem(tr("iTunes closed."), LOGINFO);
        if (!updateProgress())
            return; /* aborted */
    }

    QList<int> helperPids = Utils::findRunningProcess(
#if defined(Q_OS_WIN32)
                    QStringList("iTunesHelper"))["iTunesHelper.exe"];
#else
                    QStringList("iTunesHelper"))["iTunesHelper"];
#endif
    suspendedPids = Utils::suspendProcess(helperPids, true);
    if (suspendedPids.size() != helperPids.size()) {
        emit logItem(tr("Could not suspend iTunesHelper. Stop it\n"
                        "using the Task Manager, and try again."), LOGERROR);
        emit done(true);
        return;
    }

    LOG_INFO() << "preparing installStageWaitForSpindown";
    // for Windows: skip waiting if the HDD was ejected a time ago
    if (progressTimer.elapsed() < progressTimeout)
        emit logItem(tr("Waiting for HDD spin-down..."), LOGINFO);
    installStageWaitForSpindown();
}


void BootloaderInstallS5l::installStageWaitForSpindown(void)
{
    if (!updateProgress())
        return; /* aborted */

    if (progressTimer.elapsed() < progressTimeout) {
        QTimer::singleShot(250, this, SLOT(installStageWaitForSpindown()));
        return;
    }

    LOG_INFO() << "preparing installStageWaitForDfu";
    emit logItem(tr("Waiting for DFU mode..."), LOGINFO);
    emit logItem(tr("Action required:\n"
                    "  Press and hold SELECT+MENU buttons, after\n"
                    "  about 12 seconds a new action will require\n"
                    "  you to release the buttons, DO IT QUICKLY,\n"
                    "  otherwise the process could fail."), LOGWARNING);
    scanTimer.invalidate();
    installStageWaitForDfu();
}


void BootloaderInstallS5l::installStageWaitForDfu(void)
{
    if (!updateProgress())
        return; /* aborted */

    if (!scanTimer.isValid() || (scanTimer.elapsed() > 2000)) {
        scanSuccess = System::listUsbIds().contains(0x05ac1223);
        scanTimer.start();
    }
    if (!scanSuccess) {
        QTimer::singleShot(250, this, SLOT(installStageWaitForDfu()));
        return;
    }
    emit logItem(tr("DFU mode detected."), LOGINFO);

    emit logItem(tr("Action required:\n"
                    "  Release SELECT+MENU buttons and wait..."), LOGWARNING);

    // Once the iPod enters DFU mode, the device will reset again if
    // SELECT+MENU remains pressed for another 8 seconds. To avoid a
    // reset while the NOR is being written, we wait ~10 seconds
    // before sending the DFU image.
    LOG_INFO() << "preparing installStageSendDfu";
    setProgress(60, 10);
    installStageSendDfu();
}


void BootloaderInstallS5l::installStageSendDfu(void)
{
    if (!updateProgress())
        return; /* aborted */

    if (progressTimer.elapsed() < progressTimeout) {
        QTimer::singleShot(250, this, SLOT(installStageSendDfu()));
        return;
    }

    if (!System::listUsbIds().contains(0x05ac1223)) {
        LOG_ERROR() << "device not in DFU mode";
        emit logItem(tr("Device is not in DFU mode. It seems that\n"
                        "the previous required action failed, please\n"
                        "try again."), LOGERROR);
        emit done(true);
        return;
    }

    emit logItem(tr("Transfering DFU image..."), LOGINFO);
    if (!updateProgress())
        return; /* aborted */

    char errstr[200];
    if (!ipoddfu_send(0x1223, dfu_buf, dfu_size, errstr, sizeof(errstr))) {
        LOG_ERROR() << "ipoddfu_send() failed:" << errstr;
#if defined(Q_OS_WIN32)
        if (strstr(errstr, "DFU device not found"))
        {
            emit logItem(tr("No valid DFU USB driver found.\n"
                            "Install iTunes (or the Apple Device Driver)\n"
                            "and try again."),
                            LOGERROR);
        }
        else
#endif
        {
            emit logItem(errstr, LOGERROR);
            emit logItem(tr("Could not transfer DFU image."), LOGERROR);
        }
        emit done(true);
        return;
    }
    emit logItem(tr("DFU transfer completed."), LOGINFO);

    LOG_INFO() << "preparing installStageWaitForRemount";
    emit logItem(tr("Restarting iPod, waiting for remount..."), LOGINFO);
    setProgress(99, 45);
    scanTimer.invalidate();
    installStageWaitForRemount();
}


void BootloaderInstallS5l::installStageWaitForRemount(void)
{
    if (!updateProgress())
        return; /* aborted */

    if (!scanTimer.isValid() || (scanTimer.elapsed() > 5000)) {
        scanSuccess = Utils::mountpoints(
                    Utils::MountpointsSupported).contains(mntpoint);
        scanTimer.start();
    }
    if (!scanSuccess) {
        if (!actionShown && (progressTimer.elapsed() > progressTimeout)) {
            emit logItem(tr("Action required:\n"
                            "  Could not remount the device, try to do it\n"
                            "  manually. If the iPod didn't restart, force\n"
                            "  a reset by pressing SELECT+MENU buttons\n"
                            "  for about 5 seconds. If the problem could\n"
                            "  not be solved then click 'Abort' to cancel."),
                            LOGWARNING);
            actionShown = true;
        }
        QTimer::singleShot(250, this, SLOT(installStageWaitForRemount()));
        return;
    }
    emit logItem(tr("Device remounted."), LOGINFO);

    emit logItem(tr("Bootloader successfully %1.").
            arg(tr(doInstall ? "installed" : "uninstalled")), LOGOK);

    logInstall(doInstall ? LogAdd : LogRemove);
    emit logProgress(1, 1);
    emit done(false);
}


void BootloaderInstallS5l::installDone(bool status)
{
    LOG_INFO() << "installDone, status:" << status;
    if (Utils::suspendProcess(suspendedPids, false).size() != suspendedPids.size())
        emit logItem(tr("Could not resume iTunesHelper."), LOGWARNING);
}


void BootloaderInstallS5l::abortInstall(void)
{
    LOG_INFO() << "abortInstall";
    aborted = true;
    disconnect(this, SIGNAL(installAborted()), this, SLOT(abortInstall()));
}


bool BootloaderInstallS5l::abortDetected(void)
{
    if (aborted) {
        LOG_ERROR() << "abortDetected";
        emit logItem(tr("%1 aborted by user.").
                arg(tr(doInstall ? "Install" : "Uninstall")), LOGERROR);
        emit done(true);
        return true;
    }
    return false;
}


void BootloaderInstallS5l::setProgress(int progress, int secondsTimeout)
{
    progressTimer.start();
    progressTimeout = secondsTimeout * 1000;
    progOrigin = progTarget;
    progTarget = progress;
    actionShown = false;
}


bool BootloaderInstallS5l::updateProgress(void)
{
    if (progressTimeout) {
        progCurrent = qMin(progTarget, progOrigin +
            static_cast<int>(progressTimer.elapsed())
                           * (progTarget - progOrigin) / progressTimeout);
    }
    else {
        progCurrent = progTarget;
    }
    emit logProgress(progCurrent, 100);
    QCoreApplication::sendPostedEvents();
    QCoreApplication::processEvents();
    return !abortDetected();
}


BootloaderInstallBase::BootloaderType BootloaderInstallS5l::installed(void)
{
    bool rbblInstalled;

    QString device = Utils::resolveDevicename(m_blfile);
    if (device.isEmpty()) {
        LOG_INFO() << "installed: BootloaderUnknown";
        return BootloaderUnknown;
    }

    // rely on logfile
    QString logfile = RbSettings::value(RbSettings::Mountpoint).toString()
                + "/.rockbox/rbutil.log";
    QSettings s(logfile, QSettings::IniFormat, this);
    QString section = SystemInfo::value(
                SystemInfo::CurBootloaderName).toString().section('/', -1);
    rbblInstalled = s.contains("Bootloader/" + section);

    if (rbblInstalled) {
        LOG_INFO() << "installed: BootloaderRockbox";
        return BootloaderRockbox;
    }
    else {
        LOG_INFO() << "installed: BootloaderOther";
        return BootloaderOther;
    }
}


BootloaderInstallBase::Capabilities BootloaderInstallS5l::capabilities(void)
{
    return (Install | Uninstall);
}