summaryrefslogtreecommitdiff
path: root/utils/rbutilqt/base/bootloaderinstallsansa.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/rbutilqt/base/bootloaderinstallsansa.cpp')
-rw-r--r--utils/rbutilqt/base/bootloaderinstallsansa.cpp286
1 files changed, 286 insertions, 0 deletions
diff --git a/utils/rbutilqt/base/bootloaderinstallsansa.cpp b/utils/rbutilqt/base/bootloaderinstallsansa.cpp
new file mode 100644
index 0000000000..b1f0167e42
--- /dev/null
+++ b/utils/rbutilqt/base/bootloaderinstallsansa.cpp
@@ -0,0 +1,286 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2008 by Dominik Riebeling
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 "bootloaderinstallbase.h"
21#include "bootloaderinstallsansa.h"
22#include "Logger.h"
23
24#include "../sansapatcher/sansapatcher.h"
25#include "utils.h"
26
27BootloaderInstallSansa::BootloaderInstallSansa(QObject *parent)
28 : BootloaderInstallBase(parent)
29{
30 (void)parent;
31 // initialize sector buffer. The sector buffer is part of the sansa_t
32 // structure, so a second instance of this class will have its own buffer.
33 sansa_alloc_buffer(&sansa, BUFFER_SIZE);
34}
35
36
37BootloaderInstallSansa::~BootloaderInstallSansa()
38{
39 if(sansa.sectorbuf) {
40 sansa_dealloc_buffer(&sansa);
41 }
42}
43
44
45/** Start bootloader installation.
46 */
47bool BootloaderInstallSansa::install(void)
48{
49 if(sansa.sectorbuf == nullptr) {
50 emit logItem(tr("Error: can't allocate buffer memory!"), LOGERROR);
51 return false;
52 emit done(true);
53 }
54
55 emit logItem(tr("Searching for Sansa"), LOGINFO);
56
57 int n = sansa_scan(&sansa);
58 if(n == -1) {
59 emit logItem(tr("Permission for disc access denied!\n"
60 "This is required to install the bootloader"),
61 LOGERROR);
62 emit done(true);
63 return false;
64 }
65 if(n == 0) {
66 emit logItem(tr("No Sansa detected!"), LOGERROR);
67 emit done(true);
68 return false;
69 }
70 if(sansa.hasoldbootloader) {
71 emit logItem(tr("OLD ROCKBOX INSTALLATION DETECTED, ABORTING.\n"
72 "You must reinstall the original Sansa firmware before running\n"
73 "sansapatcher for the first time.\n"
74 "See http://www.rockbox.org/wiki/SansaE200Install\n"),
75 LOGERROR);
76 emit done(true);
77 return false;
78 }
79 emit logItem(tr("Downloading bootloader file"), LOGINFO);
80
81 downloadBlStart(m_blurl);
82 connect(this, &BootloaderInstallBase::downloadDone, this, &BootloaderInstallSansa::installStage2);
83 return true;
84}
85
86
87/** Finish bootloader installation.
88 */
89void BootloaderInstallSansa::installStage2(void)
90{
91 unsigned char* buf = nullptr;
92 unsigned int len;
93
94 emit logItem(tr("Installing Rockbox bootloader"), LOGINFO);
95 QCoreApplication::processEvents();
96 if(!sansaInitialize(&sansa)) {
97 emit done(true);
98 return;
99 }
100
101 if(sansa_reopen_rw(&sansa) < 0) {
102 emit logItem(tr("Could not open Sansa in R/W mode"), LOGERROR);
103 emit done(true);
104 return;
105 }
106
107 // check model -- if sansapatcher reports a c200 don't install an e200
108 // bootloader and vice versa.
109 // The model is available in the mi4 file at offset 0x1fc and matches
110 // the targetname set by sansapatcher.
111 emit logItem(tr("Checking downloaded bootloader"), LOGINFO);
112 m_tempfile.open();
113 QString blfile = m_tempfile.fileName();
114 char magic[4];
115 m_tempfile.seek(0x1fc);
116 m_tempfile.read(magic, 4);
117 m_tempfile.close();
118 if(memcmp(sansa.targetname, magic, 4) != 0) {
119 emit logItem(tr("Bootloader mismatch! Aborting."), LOGERROR);
120 LOG_INFO("Targetname: %s, mi4 magic: %c%c%c%c",
121 sansa.targetname, magic[0], magic[1], magic[2], magic[3]);
122 emit done(true);
123 sansa_close(&sansa);
124 return;
125 }
126
127 len = sansa_read_bootloader(&sansa, blfile.toLatin1().data(), &buf);
128 if(sansa_add_bootloader(&sansa, buf, len) == 0) {
129 emit logItem(tr("Successfully installed bootloader"), LOGOK);
130 sansa_close(&sansa);
131#if defined(Q_OS_MACX)
132 m_remountDevice = sansa.diskname;
133 connect(this, SIGNAL(remounted(bool)), this, SLOT(installStage3(bool)));
134 waitRemount();
135#else
136 installStage3(true);
137#endif
138 }
139 else {
140 emit logItem(tr("Failed to install bootloader"), LOGERROR);
141 sansa_close(&sansa);
142 emit done(true);
143 return;
144 }
145
146}
147
148
149void BootloaderInstallSansa::installStage3(bool mounted)
150{
151 if(mounted) {
152 logInstall(LogAdd);
153 emit logItem(tr("Bootloader Installation complete."), LOGINFO);
154 emit done(false);
155 return;
156 }
157 else {
158 emit logItem(tr("Writing log aborted"), LOGERROR);
159 emit done(true);
160 }
161 LOG_INFO() << "version installed:"
162 << m_blversion.toString(Qt::ISODate);
163}
164
165
166/** Uninstall the bootloader.
167 */
168bool BootloaderInstallSansa::uninstall(void)
169{
170 emit logItem(tr("Uninstalling bootloader"), LOGINFO);
171 QCoreApplication::processEvents();
172
173 if(!sansaInitialize(&sansa)) {
174 emit done(true);
175 return false;
176 }
177
178 if (sansa.hasoldbootloader) {
179 emit logItem(tr("OLD ROCKBOX INSTALLATION DETECTED, ABORTING.\n"
180 "You must reinstall the original Sansa firmware before running\n"
181 "sansapatcher for the first time.\n"
182 "See http://www.rockbox.org/wiki/SansaE200Install\n"),
183 LOGERROR);
184 emit done(true);
185 return false;
186 }
187
188 if (sansa_reopen_rw(&sansa) < 0) {
189 emit logItem(tr("Could not open Sansa in R/W mode"), LOGERROR);
190 emit done(true);
191 return false;
192 }
193
194 if (sansa_delete_bootloader(&sansa)==0) {
195 emit logItem(tr("Successfully removed bootloader"), LOGOK);
196 logInstall(LogRemove);
197 emit logProgress(1, 1);
198 emit done(false);
199 sansa_close(&sansa);
200 return true;
201 }
202 else {
203 emit logItem(tr("Removing bootloader failed."),LOGERROR);
204 emit done(true);
205 sansa_close(&sansa);
206 return false;
207 }
208
209 return false;
210}
211
212
213/** Check if bootloader is already installed
214 */
215BootloaderInstallBase::BootloaderType BootloaderInstallSansa::installed(void)
216{
217 int num;
218
219 if(!sansaInitialize(&sansa)) {
220 return BootloaderUnknown;
221 }
222 if((num = sansa_list_images(&sansa)) == 2) {
223 sansa_close(&sansa);
224 return BootloaderRockbox;
225 }
226 else if(num == 1) {
227 sansa_close(&sansa);
228 return BootloaderOther;
229 }
230 return BootloaderUnknown;
231
232}
233
234bool BootloaderInstallSansa::sansaInitialize(struct sansa_t *sansa)
235{
236 if(!m_blfile.isEmpty()) {
237 QString devicename = Utils::resolveDevicename(m_blfile);
238 if(devicename.isEmpty()) {
239 emit logItem(tr("Error: could not retrieve device name"), LOGERROR);
240 return false;
241 }
242#if defined(Q_OS_WIN32)
243 sprintf(sansa->diskname, "\\\\.\\PhysicalDrive%i", devicename.toInt());
244#elif defined(Q_OS_MACX)
245 sprintf(sansa->diskname,
246 "%s", qPrintable(devicename.remove(QRegExp("s[0-9]+$"))));
247#else
248 sprintf(sansa->diskname,
249 "%s", qPrintable(devicename.remove(QRegExp("[0-9]+$"))));
250#endif
251 LOG_INFO() << "sansapatcher: overriding scan, using"
252 << sansa->diskname;
253 }
254 else if(sansa_scan(sansa) != 1) {
255 emit logItem(tr("Can't find Sansa"), LOGERROR);
256 return false;
257 }
258
259 if (sansa_open(sansa, 0) < 0) {
260 emit logItem(tr("Could not open Sansa"), LOGERROR);
261 return false;
262 }
263
264 if (sansa_read_partinfo(sansa,0) < 0) {
265 emit logItem(tr("Could not read partition table"), LOGERROR);
266 sansa_close(sansa);
267 return false;
268 }
269
270 int i = is_sansa(sansa);
271 if(i < 0) {
272 emit logItem(tr("Disk is not a Sansa (Error %1), aborting.").arg(i), LOGERROR);
273 sansa_close(sansa);
274 return false;
275 }
276 return true;
277}
278
279
280/** Get capabilities of subclass installer.
281 */
282BootloaderInstallBase::Capabilities BootloaderInstallSansa::capabilities(void)
283{
284 return (Install | Uninstall | IsRaw | CanCheckInstalled);
285}
286