Support "eject" on OS X.
[maemo-rb.git] / rbutil / rbutilqt / base / bootloaderinstallams.cpp
blob132578bee7ea45a072ad7a2516e6cc51e9e9b3ad
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2008 by Dominik Wenger
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 ****************************************************************************/
19 #include <QtCore>
20 #include "bootloaderinstallbase.h"
21 #include "bootloaderinstallams.h"
23 #include "../mkamsboot/mkamsboot.h"
25 BootloaderInstallAms::BootloaderInstallAms(QObject *parent)
26 : BootloaderInstallBase(parent)
30 QString BootloaderInstallAms::ofHint()
32 return tr("Bootloader installation requires you to provide "
33 "a copy of the original Sandisk firmware (bin file). "
34 "This firmware file will be patched and then installed to your "
35 "player along with the rockbox bootloader. "
36 "You need to download this file yourself due to legal "
37 "reasons. Please browse the "
38 "<a href='http://forums.sandisk.com/sansa/'>Sansa Forums'</a> "
39 "or refer to the "
40 "<a href='http://www.rockbox.org/manual.shtml'>manual</a> and "
41 "the <a href='http://www.rockbox.org/wiki/SansaAMS'>SansaAMS</a> "
42 "wiki page on how to obtain this file.<br/>"
43 "Press Ok to continue and browse your computer for the firmware "
44 "file.");
47 bool BootloaderInstallAms::install(void)
49 if(m_offile.isEmpty())
50 return false;
52 qDebug() << "[BootloaderInstallAms] installing bootloader";
54 // download firmware from server
55 emit logItem(tr("Downloading bootloader file"), LOGINFO);
57 connect(this, SIGNAL(downloadDone()), this, SLOT(installStage2()));
58 downloadBlStart(m_blurl);
60 return true;
63 void BootloaderInstallAms::installStage2(void)
65 qDebug() << "[BootloaderInstallAms] installStage2";
67 unsigned char* buf;
68 unsigned char* of_packed;
69 int of_packedsize;
70 unsigned char* rb_packed;
71 int rb_packedsize;
72 off_t len;
73 struct md5sums sum;
74 char md5sum[33]; /* 32 hex digits, plus terminating zero */
75 int n;
76 int model;
77 int firmware_size;
78 int bootloader_size;
79 int patchable;
80 int totalsize;
81 char errstr[200];
83 sum.md5 = md5sum;
85 m_tempfile.open();
86 QString bootfile = m_tempfile.fileName();
87 m_tempfile.close();
89 /* Load bootloader file */
90 rb_packed = load_rockbox_file(bootfile.toLocal8Bit().data(), &model,
91 &bootloader_size,&rb_packedsize,
92 errstr,sizeof(errstr));
93 if (rb_packed == NULL)
95 qDebug() << "[BootloaderInstallAms] could not load bootloader: " << bootfile;
96 emit logItem(errstr, LOGERROR);
97 emit logItem(tr("Could not load %1").arg(bootfile), LOGERROR);
98 emit done(true);
99 return;
102 /* Load original firmware file */
103 buf = load_of_file(m_offile.toLocal8Bit().data(), model, &len, &sum,
104 &firmware_size, &of_packed ,&of_packedsize,
105 errstr, sizeof(errstr));
106 if (buf == NULL)
108 qDebug() << "[BootloaderInstallAms] could not load OF: " << m_offile;
109 emit logItem(errstr, LOGERROR);
110 emit logItem(tr("Could not load %1").arg(m_offile), LOGERROR);
111 free(rb_packed);
112 emit done(true);
113 return;
116 /* check total size */
117 patchable = check_sizes(sum.model, rb_packedsize, bootloader_size,
118 of_packedsize, firmware_size, &totalsize, errstr, sizeof(errstr));
120 if (!patchable)
122 qDebug() << "[BootloaderInstallAms] No room to insert bootloader";
123 emit logItem(errstr, LOGERROR);
124 emit logItem(tr("No room to insert bootloader, try another firmware version"),
125 LOGERROR);
126 free(buf);
127 free(of_packed);
128 free(rb_packed);
129 emit done(true);
130 return;
133 /* patch the firmware */
134 emit logItem(tr("Patching Firmware..."), LOGINFO);
136 patch_firmware(sum.model,firmware_revision(sum.model),firmware_size,buf,
137 len,of_packed,of_packedsize,rb_packed,rb_packedsize);
139 /* write out file */
140 QFile out(m_blfile);
142 if(!out.open(QIODevice::WriteOnly | QIODevice::Truncate))
144 qDebug() << "[BootloaderInstallAms] Could not open" << m_blfile << "for writing";
145 emit logItem(tr("Could not open %1 for writing").arg(m_blfile),LOGERROR);
146 free(buf);
147 free(of_packed);
148 free(rb_packed);
149 emit done(true);
150 return;
153 n = out.write((char*)buf, len);
155 if (n != len)
157 qDebug() << "[BootloaderInstallAms] Could not write firmware file";
158 emit logItem(tr("Could not write firmware file"),LOGERROR);
159 free(buf);
160 free(of_packed);
161 free(rb_packed);
162 emit done(true);
163 return;
166 out.close();
168 free(buf);
169 free(of_packed);
170 free(rb_packed);
172 //end of install
173 qDebug() << "[BootloaderInstallAms] install successfull";
174 emit logItem(tr("Success: modified firmware file created"), LOGINFO);
175 logInstall(LogAdd);
176 emit done(false);
177 return;
180 bool BootloaderInstallAms::uninstall(void)
182 emit logItem(tr("To uninstall, perform a normal upgrade with an unmodified "
183 "original firmware"), LOGINFO);
184 logInstall(LogRemove);
185 return false;
188 BootloaderInstallBase::BootloaderType BootloaderInstallAms::installed(void)
190 return BootloaderUnknown;
193 BootloaderInstallBase::Capabilities BootloaderInstallAms::capabilities(void)
195 return (Install | NeedsOf);