Make sure to include audiohw.h in settings.h or the definition of struct user_setting...
[kugel-rb.git] / rbutil / rbutilqt / base / bootloaderinstallams.cpp
blobb1f47eda93a4230f055e06fc92e37fa4a64ae45a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2008 by Dominik Wenger
10 * $Id$
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <QtCore>
21 #include "bootloaderinstallbase.h"
22 #include "bootloaderinstallams.h"
24 #include "../mkamsboot/mkamsboot.h"
26 BootloaderInstallAms::BootloaderInstallAms(QObject *parent)
27 : BootloaderInstallBase(parent)
31 QString BootloaderInstallAms::ofHint()
33 return tr("Bootloader installation requires you to provide "
34 "a firmware file of the original firmware (bin file). "
35 "You need to download this file yourself due to legal "
36 "reasons. Please browse the "
37 "<a href='http://forums.sandisk.com/sansa/'>Sansa Forums'</a> "
38 "or refer to the "
39 "<a href='http://www.rockbox.org/manual.shtml'>manual</a> and "
40 "the <a href='http://www.rockbox.org/wiki/SansaAMS'>SansaAMS</a> "
41 "wiki page on how to obtain this file.<br/>"
42 "Press Ok to continue and browse your computer for the firmware "
43 "file.");
46 bool BootloaderInstallAms::install(void)
48 if(m_offile.isEmpty())
49 return false;
51 qDebug() << "[BootloaderInstallAms] installing bootloader";
53 // download firmware from server
54 emit logItem(tr("Downloading bootloader file"), LOGINFO);
56 connect(this, SIGNAL(downloadDone()), this, SLOT(installStage2()));
57 downloadBlStart(m_blurl);
59 return true;
62 void BootloaderInstallAms::installStage2(void)
64 qDebug() << "[BootloaderInstallAms] installStage2";
66 unsigned char* buf;
67 unsigned char* of_packed;
68 int of_packedsize;
69 unsigned char* rb_packed;
70 int rb_packedsize;
71 off_t len;
72 struct md5sums sum;
73 char md5sum[33]; /* 32 hex digits, plus terminating zero */
74 int n;
75 int firmware_size;
76 int bootloader_size;
77 int patchable;
78 int totalsize;
79 char errstr[200];
81 sum.md5 = md5sum;
83 m_tempfile.open();
84 QString bootfile = m_tempfile.fileName();
85 m_tempfile.close();
87 /* Load original firmware file */
88 buf = load_of_file(m_offile.toLocal8Bit().data(), &len,&sum,&firmware_size,
89 &of_packed,&of_packedsize,errstr,sizeof(errstr));
90 if (buf == NULL)
92 qDebug() << "[BootloaderInstallAms] could not load OF: " << m_offile;
93 emit logItem(errstr, LOGERROR);
94 emit logItem(tr("Could not load %1").arg(m_offile), LOGERROR);
95 emit done(true);
96 return;
99 /* Load bootloader file */
100 rb_packed = load_rockbox_file(bootfile.toLocal8Bit().data(), sum.model,
101 &bootloader_size,&rb_packedsize,
102 errstr,sizeof(errstr));
103 if (rb_packed == NULL)
105 qDebug() << "[BootloaderInstallAms] could not load bootloader: " << bootfile;
106 emit logItem(errstr, LOGERROR);
107 emit logItem(tr("Could not load %1").arg(bootfile), LOGERROR);
108 free(buf);
109 free(of_packed);
110 emit done(true);
111 return;
114 /* check total size */
115 patchable = check_sizes(sum.model, rb_packedsize, bootloader_size,
116 of_packedsize, firmware_size, &totalsize, errstr, sizeof(errstr));
118 if (!patchable)
120 qDebug() << "[BootloaderInstallAms] No room to insert bootloader";
121 emit logItem(errstr, LOGERROR);
122 emit logItem(tr("No room to insert bootloader, try another firmware version"),
123 LOGERROR);
124 free(buf);
125 free(of_packed);
126 free(rb_packed);
127 emit done(true);
128 return;
131 /* patch the firmware */
132 emit logItem(tr("Patching Firmware..."), LOGINFO);
134 patch_firmware(sum.model,firmware_revision(sum.model),firmware_size,buf,
135 len,of_packed,of_packedsize,rb_packed,rb_packedsize);
137 /* write out file */
138 QFile out(m_blfile);
140 if(!out.open(QIODevice::WriteOnly | QIODevice::Truncate))
142 qDebug() << "[BootloaderInstallAms] Could not open" << m_blfile << "for writing";
143 emit logItem(tr("Could not open %1 for writing").arg(m_blfile),LOGERROR);
144 free(buf);
145 free(of_packed);
146 free(rb_packed);
147 emit done(true);
148 return;
151 n = out.write((char*)buf, len);
153 if (n != len)
155 qDebug() << "[BootloaderInstallAms] Could not write firmware file";
156 emit logItem(tr("Could not write firmware file"),LOGERROR);
157 free(buf);
158 free(of_packed);
159 free(rb_packed);
160 emit done(true);
161 return;
164 out.close();
166 free(buf);
167 free(of_packed);
168 free(rb_packed);
170 //end of install
171 qDebug() << "[BootloaderInstallAms] install successfull";
172 emit logItem(tr("Success: modified firmware file created"), LOGINFO);
173 logInstall(LogAdd);
174 emit done(false);
175 return;
178 bool BootloaderInstallAms::uninstall(void)
180 emit logItem(tr("To uninstall, perform a normal upgrade with an unmodified "
181 "original firmware"), LOGINFO);
182 logInstall(LogRemove);
183 return false;
186 BootloaderInstallBase::BootloaderType BootloaderInstallAms::installed(void)
188 return BootloaderUnknown;
191 BootloaderInstallBase::Capabilities BootloaderInstallAms::capabilities(void)
193 return (Install | NeedsOf);