Fix bootloader files vanishing after installation (FS#11086).
[kugel-rb.git] / rbutil / rbutilqt / base / bootloaderinstallfile.cpp
blob2e7061036d27b5ddc36ceee9c54e4f643ef35786
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2008 by Dominik Riebeling
10 * $Id$
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <QtCore>
21 #include <QtDebug>
22 #include <QtDebug>
23 #include "bootloaderinstallfile.h"
24 #include "utils.h"
27 BootloaderInstallFile::BootloaderInstallFile(QObject *parent)
28 : BootloaderInstallBase(parent)
33 bool BootloaderInstallFile::install(void)
35 emit logItem(tr("Downloading bootloader"), LOGINFO);
36 qDebug() << "[BootloaderInstallFile] installing bootloader";
37 downloadBlStart(m_blurl);
38 connect(this, SIGNAL(downloadDone()), this, SLOT(installStage2()));
39 return true;
42 void BootloaderInstallFile::installStage2(void)
44 emit logItem(tr("Installing Rockbox bootloader"), LOGINFO);
45 QCoreApplication::processEvents();
47 // if an old bootloader is present (Gigabeat) move it out of the way.
48 QString fwfile(resolvePathCase(m_blfile));
49 if(!fwfile.isEmpty()) {
50 QString moved = resolvePathCase(m_blfile) + ".ORIG";
51 qDebug() << "[BootloaderInstallFile] renaming" << fwfile << "to" << moved;
52 QFile::rename(fwfile, moved);
55 // if no old file found resolve path without basename
56 QFileInfo fi(m_blfile);
57 QString absPath = resolvePathCase(fi.absolutePath());
59 // if it's not possible to locate the base path try to create it
60 if(absPath.isEmpty()) {
61 QStringList pathElements = m_blfile.split("/");
62 // remove filename from list and save last path element
63 pathElements.removeLast();
64 QString lastElement = pathElements.last();
65 // remove last path element for base
66 pathElements.removeLast();
67 QString basePath = pathElements.join("/");
69 // check for base and bail out if not found. Otherwise create folder.
70 absPath = resolvePathCase(basePath);
71 QDir d(absPath);
72 d.mkpath(lastElement);
73 absPath = resolvePathCase(fi.absolutePath());
75 if(absPath.isEmpty()) {
76 emit logItem(tr("Error accessing output folder"), LOGERROR);
77 emit done(true);
78 return;
81 fwfile = absPath + "/" + fi.fileName();
83 // place (new) bootloader
84 m_tempfile.open();
85 qDebug() << "[BootloaderInstallFile] renaming" << m_tempfile.fileName() << "to" << fwfile;
86 m_tempfile.close();
87 m_tempfile.copy(fwfile);
89 emit logItem(tr("Bootloader successful installed"), LOGOK);
90 logInstall(LogAdd);
92 emit done(false);
96 bool BootloaderInstallFile::uninstall(void)
98 qDebug() << "[BootloaderInstallFile] Uninstalling bootloader";
99 emit logItem(tr("Removing Rockbox bootloader"), LOGINFO);
100 // check if a .ORIG file is present, and allow moving it back.
101 QString origbl = resolvePathCase(m_blfile + ".ORIG");
102 if(origbl.isEmpty()) {
103 emit logItem(tr("No original firmware file found."), LOGERROR);
104 emit done(true);
105 return false;
107 QString fwfile = resolvePathCase(m_blfile);
108 if(!QFile::remove(fwfile)) {
109 emit logItem(tr("Can't remove Rockbox bootloader file."), LOGERROR);
110 emit done(true);
111 return false;
113 if(!QFile::rename(origbl, fwfile)) {
114 emit logItem(tr("Can't restore bootloader file."), LOGERROR);
115 emit done(true);
116 return false;
118 emit logItem(tr("Original bootloader restored successfully."), LOGOK);
119 logInstall(LogRemove);
120 emit done(false);
122 return true;
126 //! @brief check if bootloader is installed.
127 //! @return BootloaderRockbox, BootloaderOther or BootloaderUnknown.
128 BootloaderInstallBase::BootloaderType BootloaderInstallFile::installed(void)
130 qDebug() << "[BootloaderInstallFile] checking installed bootloader";
131 if(!resolvePathCase(m_blfile).isEmpty()
132 && !resolvePathCase(m_blfile + ".ORIG").isEmpty())
133 return BootloaderRockbox;
134 else if(!resolvePathCase(m_blfile).isEmpty())
135 return BootloaderOther;
136 else
137 return BootloaderUnknown;
141 BootloaderInstallBase::Capabilities BootloaderInstallFile::capabilities(void)
143 qDebug() << "[BootloaderInstallFile] getting capabilities";
144 return Install | IsFile | CanCheckInstalled | Backup;