summaryrefslogtreecommitdiff
path: root/utils/rbutilqt/base/bootloaderinstallbspatch.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/rbutilqt/base/bootloaderinstallbspatch.cpp')
-rw-r--r--utils/rbutilqt/base/bootloaderinstallbspatch.cpp178
1 files changed, 178 insertions, 0 deletions
diff --git a/utils/rbutilqt/base/bootloaderinstallbspatch.cpp b/utils/rbutilqt/base/bootloaderinstallbspatch.cpp
new file mode 100644
index 0000000000..02291b0151
--- /dev/null
+++ b/utils/rbutilqt/base/bootloaderinstallbspatch.cpp
@@ -0,0 +1,178 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2020 by Solomon Peachy
10 *
11 * All files in this archive are subject to the GNU General Public License.
12 * See the file COPYING in the source tree root for full license agreement.
13 *
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
16 *
17 ****************************************************************************/
18
19#include <QtCore>
20#include <QtDebug>
21#include "bootloaderinstallbase.h"
22#include "bootloaderinstallbspatch.h"
23#include "../bspatch/bspatch.h"
24#include "Logger.h"
25
26/* class for running bspatch() in a separate thread to keep the UI responsive. */
27class BootloaderThreadBSPatch : public QThread
28{
29 public:
30 void run(void);
31 void setInputFile(QString f)
32 { m_inputfile = f; }
33 void setOutputFile(QString f)
34 { m_outputfile = f; }
35 void setBootloaderFile(QString f)
36 { m_bootfile = f; }
37 int error(void)
38 { return m_error; }
39 private:
40 QString m_inputfile;
41 QString m_bootfile;
42 QString m_outputfile;
43 int m_error;
44};
45
46void BootloaderThreadBSPatch::run(void)
47{
48 LOG_INFO() << "Thread started.";
49
50 m_error = apply_bspatch(m_inputfile.toLocal8Bit().constData(),
51 m_outputfile.toLocal8Bit().constData(),
52 m_bootfile.toLocal8Bit().constData());
53
54 LOG_INFO() << "Thread finished, result:" << m_error;
55}
56
57BootloaderInstallBSPatch::BootloaderInstallBSPatch(QObject *parent)
58 : BootloaderInstallBase(parent)
59{
60 m_thread = nullptr;
61}
62
63QString BootloaderInstallBSPatch::ofHint()
64{
65 return tr("Bootloader installation requires you to provide "
66 "the correct verrsion of the original firmware file. "
67 "This file will be patched with the Rockbox bootloader and "
68 "installed to your player. You need to download this file "
69 "yourself due to legal reasons. Please refer to the "
70 "<a href='http://www.rockbox.org/wiki/'>rockbox wiki</a> "
71 "pages on how to obtain this file.<br/>"
72 "Press Ok to continue and browse your computer for the firmware "
73 "file.");
74}
75
76/** Start bootloader installation.
77 */
78bool BootloaderInstallBSPatch::install(void)
79{
80 if(!QFileInfo(m_offile).isReadable())
81 {
82 LOG_ERROR() << "could not read original firmware file"
83 << m_offile;
84 emit logItem(tr("Could not read original firmware file"), LOGERROR);
85 return false;
86 }
87
88 LOG_INFO() << "downloading bootloader";
89 // download bootloader from server
90 emit logItem(tr("Downloading bootloader file"), LOGINFO);
91 connect(this, &BootloaderInstallBase::downloadDone, this, &BootloaderInstallBSPatch::installStage2);
92 downloadBlStart(m_blurl);
93 return true;
94}
95
96void BootloaderInstallBSPatch::installStage2(void)
97{
98 LOG_INFO() << "patching file...";
99 emit logItem(tr("Patching file..."), LOGINFO);
100 m_tempfile.open();
101
102 // we have not detailed progress on the patching so just show a busy
103 // indicator instead.
104 emit logProgress(0, 0);
105 m_patchedFile.open();
106 m_thread = new BootloaderThreadBSPatch();
107 m_thread->setInputFile(m_offile);
108 m_thread->setBootloaderFile(m_tempfile.fileName());
109 m_thread->setOutputFile(m_patchedFile.fileName());
110 m_tempfile.close();
111 m_patchedFile.close();
112 connect(m_thread, &QThread::finished, this, &BootloaderInstallBSPatch::installStage3);
113 m_thread->start();
114}
115
116void BootloaderInstallBSPatch::installStage3(void)
117{
118 int err = m_thread->error();
119 emit logProgress(1, 1);
120 // if the patch failed
121 if (err != 0)
122 {
123 LOG_ERROR() << "Could not patch the original firmware file";
124 emit logItem(tr("Patching the original firmware failed"), LOGERROR);
125 emit done(true);
126 return;
127 }
128
129 LOG_INFO() << "Original Firmware succesfully patched";
130 emit logItem(tr("Succesfully patched firmware file"), LOGINFO);
131
132 // if a bootloader is already present delete it.
133 QString fwfile(m_blfile);
134 if(QFileInfo(fwfile).isFile())
135 {
136 LOG_INFO() << "deleting old target file";
137 QFile::remove(fwfile);
138 }
139
140 // place (new) bootloader. Copy, since the temporary file will be removed
141 // automatically.
142 LOG_INFO() << "moving patched bootloader to" << fwfile;
143 if(m_patchedFile.copy(fwfile))
144 {
145 emit logItem(tr("Bootloader successful installed"), LOGOK);
146 logInstall(LogAdd);
147 emit done(false);
148 }
149 else
150 {
151 emit logItem(tr("Patched bootloader could not be installed"), LOGERROR);
152 emit done(true);
153 }
154 // clean up thread object.
155 delete m_thread;
156 return;
157}
158
159bool BootloaderInstallBSPatch::uninstall(void)
160{
161 emit logItem(tr("To uninstall, perform a normal upgrade with an unmodified "
162 "original firmware."), LOGINFO);
163 logInstall(LogRemove);
164 emit done(true);
165 return false;
166}
167
168
169BootloaderInstallBase::BootloaderType BootloaderInstallBSPatch::installed(void)
170{
171 return BootloaderUnknown;
172}
173
174
175BootloaderInstallBase::Capabilities BootloaderInstallBSPatch::capabilities(void)
176{
177 return (Install | NeedsOf);
178}