1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2008 by Dominik Riebeling
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.
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
17 ****************************************************************************/
20 #include "bootloaderinstallbase.h"
21 #include "bootloaderinstallsansa.h"
23 #include "../sansapatcher/sansapatcher.h"
26 BootloaderInstallSansa::BootloaderInstallSansa(QObject
*parent
)
27 : BootloaderInstallBase(parent
)
30 // initialize sector buffer. sansa_sectorbuf is instantiated by
32 // The buffer itself is only present once, so make sure to not allocate
33 // it if it was already allocated. The application needs to take care
34 // no concurrent (i.e. multiple objects of this class running) requests
36 if(sansa_sectorbuf
== NULL
)
37 sansa_alloc_buffer(&sansa_sectorbuf
, BUFFER_SIZE
);
41 BootloaderInstallSansa::~BootloaderInstallSansa()
44 free(sansa_sectorbuf
);
45 sansa_sectorbuf
= NULL
;
50 /** Start bootloader installation.
52 bool BootloaderInstallSansa::install(void)
54 if(sansa_sectorbuf
== NULL
) {
55 emit
logItem(tr("Error: can't allocate buffer memory!"), LOGERROR
);
60 emit
logItem(tr("Searching for Sansa"), LOGINFO
);
64 int n
= sansa_scan(&sansa
);
66 emit
logItem(tr("Permission for disc access denied!\n"
67 "This is required to install the bootloader"),
73 emit
logItem(tr("No Sansa detected!"), LOGERROR
);
77 if(sansa
.hasoldbootloader
) {
78 emit
logItem(tr("OLD ROCKBOX INSTALLATION DETECTED, ABORTING.\n"
79 "You must reinstall the original Sansa firmware before running\n"
80 "sansapatcher for the first time.\n"
81 "See http://www.rockbox.org/wiki/SansaE200Install\n"),
86 emit
logItem(tr("Downloading bootloader file"), LOGINFO
);
88 downloadBlStart(m_blurl
);
89 connect(this, SIGNAL(downloadDone()), this, SLOT(installStage2()));
94 /** Finish bootloader installation.
96 void BootloaderInstallSansa::installStage2(void)
99 unsigned char* buf
= NULL
;
102 emit
logItem(tr("Installing Rockbox bootloader"), LOGINFO
);
103 QCoreApplication::processEvents();
104 if(!sansaInitialize(&sansa
)) {
109 if(sansa_reopen_rw(&sansa
) < 0) {
110 emit
logItem(tr("Could not open Sansa in R/W mode"), LOGERROR
);
115 // check model -- if sansapatcher reports a c200 don't install an e200
116 // bootloader and vice versa.
117 // The model is available in the mi4 file at offset 0x1fc and matches
118 // the targetname set by sansapatcher.
119 emit
logItem(tr("Checking downloaded bootloader"), LOGINFO
);
121 QString blfile
= m_tempfile
.fileName();
123 m_tempfile
.seek(0x1fc);
124 m_tempfile
.read(magic
, 4);
126 if(memcmp(sansa
.targetname
, magic
, 4) != 0) {
127 emit
logItem(tr("Bootloader mismatch! Aborting."), LOGERROR
);
128 qDebug("[BootloaderInstallSansa] Targetname: %s, mi4 magic: %c%c%c%c",
129 sansa
.targetname
, magic
[0], magic
[1], magic
[2], magic
[3]);
135 len
= sansa_read_bootloader(&sansa
, blfile
.toLatin1().data(), &buf
);
136 if(sansa_add_bootloader(&sansa
, buf
, len
) == 0) {
137 emit
logItem(tr("Successfully installed bootloader"), LOGOK
);
139 #if defined(Q_OS_MACX)
140 m_remountDevice
= sansa
.diskname
;
141 connect(this, SIGNAL(remounted(bool)), this, SLOT(installStage3(bool)));
148 emit
logItem(tr("Failed to install bootloader"), LOGERROR
);
157 void BootloaderInstallSansa::installStage3(bool mounted
)
161 emit
logItem(tr("Bootloader Installation complete."), LOGINFO
);
166 emit
logItem(tr("Writing log aborted"), LOGERROR
);
169 qDebug() << "[BootloaderInstallSansa] version installed:" << m_blversion
.toString(Qt::ISODate
);
173 /** Uninstall the bootloader.
175 bool BootloaderInstallSansa::uninstall(void)
177 struct sansa_t sansa
;
179 emit
logItem(tr("Uninstalling bootloader"), LOGINFO
);
180 QCoreApplication::processEvents();
182 if(!sansaInitialize(&sansa
)) {
187 if (sansa
.hasoldbootloader
) {
188 emit
logItem(tr("OLD ROCKBOX INSTALLATION DETECTED, ABORTING.\n"
189 "You must reinstall the original Sansa firmware before running\n"
190 "sansapatcher for the first time.\n"
191 "See http://www.rockbox.org/wiki/SansaE200Install\n"),
197 if (sansa_reopen_rw(&sansa
) < 0) {
198 emit
logItem(tr("Could not open Sansa in R/W mode"), LOGERROR
);
203 if (sansa_delete_bootloader(&sansa
)==0) {
204 emit
logItem(tr("Successfully removed bootloader"), LOGOK
);
205 logInstall(LogRemove
);
211 emit
logItem(tr("Removing bootloader failed."),LOGERROR
);
221 /** Check if bootloader is already installed
223 BootloaderInstallBase::BootloaderType
BootloaderInstallSansa::installed(void)
225 struct sansa_t sansa
;
228 if(!sansaInitialize(&sansa
)) {
229 return BootloaderUnknown
;
231 if((num
= sansa_list_images(&sansa
)) == 2) {
233 return BootloaderRockbox
;
237 return BootloaderOther
;
239 return BootloaderUnknown
;
243 bool BootloaderInstallSansa::sansaInitialize(struct sansa_t
*sansa
)
245 if(!m_blfile
.isEmpty()) {
246 QString devicename
= Utils::resolveDevicename(m_blfile
);
247 if(devicename
.isEmpty()) {
248 emit
logItem(tr("Error: could not retrieve device name"), LOGERROR
);
251 #if defined(Q_OS_WIN32)
252 sprintf(sansa
->diskname
, "\\\\.\\PhysicalDrive%i", devicename
.toInt());
253 #elif defined(Q_OS_MACX)
254 sprintf(sansa
->diskname
,
255 qPrintable(devicename
.remove(QRegExp("s[0-9]+$"))));
257 sprintf(sansa
->diskname
,
258 qPrintable(devicename
.remove(QRegExp("[0-9]+$"))));
260 qDebug() << "[BootloaderInstallSansa] sansapatcher: overriding scan, using"
263 else if(sansa_scan(sansa
) != 1) {
264 emit
logItem(tr("Can't find Sansa"), LOGERROR
);
268 if (sansa_open(sansa
, 0) < 0) {
269 emit
logItem(tr("Could not open Sansa"), LOGERROR
);
273 if (sansa_read_partinfo(sansa
,0) < 0) {
274 emit
logItem(tr("Could not read partition table"), LOGERROR
);
279 int i
= is_sansa(sansa
);
281 emit
logItem(tr("Disk is not a Sansa (Error %1), aborting.").arg(i
), LOGERROR
);
289 /** Get capabilities of subclass installer.
291 BootloaderInstallBase::Capabilities
BootloaderInstallSansa::capabilities(void)
293 return (Install
| Uninstall
| IsRaw
| CanCheckInstalled
);