diff options
Diffstat (limited to 'rbutil/rbutilqt/base/bootloaderinstallbspatch.cpp')
-rw-r--r-- | rbutil/rbutilqt/base/bootloaderinstallbspatch.cpp | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/rbutil/rbutilqt/base/bootloaderinstallbspatch.cpp b/rbutil/rbutilqt/base/bootloaderinstallbspatch.cpp new file mode 100644 index 0000000000..c9cbd91abe --- /dev/null +++ b/rbutil/rbutilqt/base/bootloaderinstallbspatch.cpp | |||
@@ -0,0 +1,179 @@ | |||
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. */ | ||
27 | class 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 | |||
46 | void 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 | |||
57 | BootloaderInstallBSPatch::BootloaderInstallBSPatch(QObject *parent) | ||
58 | : BootloaderInstallBase(parent) | ||
59 | { | ||
60 | m_thread = NULL; | ||
61 | } | ||
62 | |||
63 | QString 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 | */ | ||
78 | bool 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, SIGNAL(downloadDone()), this, SLOT(installStage2())); | ||
92 | downloadBlStart(m_blurl); | ||
93 | return true; | ||
94 | } | ||
95 | |||
96 | void 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, SIGNAL(finished()), this, SLOT(installStage3())); | ||
113 | connect(m_thread, SIGNAL(terminated()), this, SLOT(installStage3())); | ||
114 | m_thread->start(); | ||
115 | } | ||
116 | |||
117 | void BootloaderInstallBSPatch::installStage3(void) | ||
118 | { | ||
119 | int err = m_thread->error(); | ||
120 | emit logProgress(1, 1); | ||
121 | // if the patch failed | ||
122 | if (err != 0) | ||
123 | { | ||
124 | LOG_ERROR() << "Could not patch the original firmware file"; | ||
125 | emit logItem(tr("Patching the original firmware failed"), LOGERROR); | ||
126 | emit done(true); | ||
127 | return; | ||
128 | } | ||
129 | |||
130 | LOG_INFO() << "Original Firmware succesfully patched"; | ||
131 | emit logItem(tr("Succesfully patched firmware file"), LOGINFO); | ||
132 | |||
133 | // if a bootloader is already present delete it. | ||
134 | QString fwfile(m_blfile); | ||
135 | if(QFileInfo(fwfile).isFile()) | ||
136 | { | ||
137 | LOG_INFO() << "deleting old target file"; | ||
138 | QFile::remove(fwfile); | ||
139 | } | ||
140 | |||
141 | // place (new) bootloader. Copy, since the temporary file will be removed | ||
142 | // automatically. | ||
143 | LOG_INFO() << "moving patched bootloader to" << fwfile; | ||
144 | if(m_patchedFile.copy(fwfile)) | ||
145 | { | ||
146 | emit logItem(tr("Bootloader successful installed"), LOGOK); | ||
147 | logInstall(LogAdd); | ||
148 | emit done(false); | ||
149 | } | ||
150 | else | ||
151 | { | ||
152 | emit logItem(tr("Patched bootloader could not be installed"), LOGERROR); | ||
153 | emit done(true); | ||
154 | } | ||
155 | // clean up thread object. | ||
156 | delete m_thread; | ||
157 | return; | ||
158 | } | ||
159 | |||
160 | bool BootloaderInstallBSPatch::uninstall(void) | ||
161 | { | ||
162 | emit logItem(tr("To uninstall, perform a normal upgrade with an unmodified " | ||
163 | "original firmware."), LOGINFO); | ||
164 | logInstall(LogRemove); | ||
165 | emit done(true); | ||
166 | return false; | ||
167 | } | ||
168 | |||
169 | |||
170 | BootloaderInstallBase::BootloaderType BootloaderInstallBSPatch::installed(void) | ||
171 | { | ||
172 | return BootloaderUnknown; | ||
173 | } | ||
174 | |||
175 | |||
176 | BootloaderInstallBase::Capabilities BootloaderInstallBSPatch::capabilities(void) | ||
177 | { | ||
178 | return (Install | NeedsOf); | ||
179 | } | ||