summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/base/bootloaderinstallfile.cpp
blob: 7649f50e2a71d26a5e7a783de167f80497c9ef20 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 *   Copyright (C) 2008 by Dominik Riebeling
 *   $Id$
 *
 * 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 <QtDebug>
#include <QtDebug>
#include "bootloaderinstallfile.h"
#include "utils.h"


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


bool BootloaderInstallFile::install(void)
{
    emit logItem(tr("Downloading bootloader"), LOGINFO);
    qDebug() << "[BootloaderInstallFile] installing bootloader";
    downloadBlStart(m_blurl);
    connect(this, SIGNAL(downloadDone()), this, SLOT(installStage2()));
    return true;
}

void BootloaderInstallFile::installStage2(void)
{
    emit logItem(tr("Installing Rockbox bootloader"), LOGINFO);
    QCoreApplication::processEvents();

    // if an old bootloader is present (Gigabeat) move it out of the way.
    QString fwfile(Utils::resolvePathCase(m_blfile));
    if(!fwfile.isEmpty()) {
        QString moved = Utils::resolvePathCase(m_blfile) + ".ORIG";
        qDebug() << "[BootloaderInstallFile] renaming" << fwfile << "to" << moved;
        QFile::rename(fwfile, moved);
    }

    // if no old file found resolve path without basename
    QFileInfo fi(m_blfile);
    QString absPath = Utils::resolvePathCase(fi.absolutePath());

    // if it's not possible to locate the base path try to create it
    if(absPath.isEmpty()) {
        QStringList pathElements = m_blfile.split("/");
        // remove filename from list and save last path element
        pathElements.removeLast();
        QString lastElement = pathElements.last();
        // remove last path element for base
        pathElements.removeLast();
        QString basePath = pathElements.join("/");

        // check for base and bail out if not found. Otherwise create folder.
        absPath = Utils::resolvePathCase(basePath);
        QDir d(absPath);
        d.mkpath(lastElement);
        absPath = Utils::resolvePathCase(fi.absolutePath());

        if(absPath.isEmpty()) {
            emit logItem(tr("Error accessing output folder"), LOGERROR);
            emit done(true);
            return;
        }
    }
    fwfile = absPath + "/" + fi.fileName();

    // place (new) bootloader
    m_tempfile.open();
    qDebug() << "[BootloaderInstallFile] renaming" << m_tempfile.fileName() << "to" << fwfile;
    m_tempfile.close();
    m_tempfile.copy(fwfile);

    emit logItem(tr("Bootloader successful installed"), LOGOK);
    logInstall(LogAdd);

    emit done(false);
}


bool BootloaderInstallFile::uninstall(void)
{
    qDebug() << "[BootloaderInstallFile] Uninstalling bootloader";
    emit logItem(tr("Removing Rockbox bootloader"), LOGINFO);
    // check if a .ORIG file is present, and allow moving it back.
    QString origbl = Utils::resolvePathCase(m_blfile + ".ORIG");
    if(origbl.isEmpty()) {
        emit logItem(tr("No original firmware file found."), LOGERROR);
        emit done(true);
        return false;
    }
    QString fwfile = Utils::resolvePathCase(m_blfile);
    if(!QFile::remove(fwfile)) {
        emit logItem(tr("Can't remove Rockbox bootloader file."), LOGERROR);
        emit done(true);
        return false;
    }
    if(!QFile::rename(origbl, fwfile)) {
        emit logItem(tr("Can't restore bootloader file."), LOGERROR);
        emit done(true);
        return false;
    }
    emit logItem(tr("Original bootloader restored successfully."), LOGOK);
    logInstall(LogRemove);
    emit done(false);

    return true;
}


//! @brief check if bootloader is installed.
//! @return BootloaderRockbox, BootloaderOther or BootloaderUnknown.
BootloaderInstallBase::BootloaderType BootloaderInstallFile::installed(void)
{
    qDebug() << "[BootloaderInstallFile] checking installed bootloader";
    if(!Utils::resolvePathCase(m_blfile).isEmpty()
        && !Utils::resolvePathCase(m_blfile + ".ORIG").isEmpty())
        return BootloaderRockbox;
    else if(!Utils::resolvePathCase(m_blfile).isEmpty())
        return BootloaderOther;
    else
        return BootloaderUnknown;
}


BootloaderInstallBase::Capabilities BootloaderInstallFile::capabilities(void)
{
    qDebug() << "[BootloaderInstallFile] getting capabilities";
    return Install | IsFile | CanCheckInstalled | Backup;
}