FS#10740 - rbutil: Test Cowon D2 OF file for CRC consistency before patching
[kugel-rb.git] / rbutil / rbutilqt / base / bootloaderinstalltcc.cpp
blob525421defea086c40775624531c6133f1ec90848
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2009 by Tomer Shalev
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 * This file is a modified version of the AMS installer by Dominik Wenger
20 ****************************************************************************/
22 #include <QtCore>
23 #include "bootloaderinstallbase.h"
24 #include "bootloaderinstalltcc.h"
25 #include "../mktccboot/mktccboot.h"
27 BootloaderInstallTcc::BootloaderInstallTcc(QObject *parent)
28 : BootloaderInstallBase(parent)
32 QString BootloaderInstallTcc::ofHint()
34 return tr("Bootloader installation requires you to provide "
35 "a firmware file of the original firmware (bin file). "
36 "You need to download this file yourself due to legal "
37 "reasons."
38 "Press Ok to continue and browse your computer for the firmware "
39 "file.");
42 bool BootloaderInstallTcc::install(void)
44 if(m_offile.isEmpty())
45 return false;
47 // Download firmware from server
48 emit logItem(tr("Downloading bootloader file"), LOGINFO);
50 connect(this, SIGNAL(downloadDone()), this, SLOT(installStage2()));
51 downloadBlStart(m_blurl);
53 return true;
56 void BootloaderInstallTcc::installStage2(void)
58 unsigned char *of_buf, *boot_buf = NULL, *patched_buf = NULL;
59 int n, of_size, boot_size, patched_size;
60 char errstr[200];
61 bool ret = false;
63 m_tempfile.open();
64 QString bootfile = m_tempfile.fileName();
65 m_tempfile.close();
67 /* Construct path for write out.
68 * Combine path of m_blfile with filename from OF */
69 QString outfilename = QFileInfo(m_blfile).absolutePath() + "/" +
70 QFileInfo(m_offile).fileName();
72 /* Write out file */
73 QFile out(outfilename);
75 /* Load original firmware file */
76 of_buf = file_read(m_offile.toLocal8Bit().data(), &of_size);
77 if (of_buf == NULL)
79 emit logItem(errstr, LOGERROR);
80 emit logItem(tr("Could not load %1").arg(m_offile), LOGERROR);
81 goto exit;
84 /* A CRC test in order to reject non OF file */
85 if (test_firmware_tcc(of_buf, of_size))
87 emit logItem(errstr, LOGERROR);
88 emit logItem(tr("Unknown OF file used: %1").arg(m_offile), LOGERROR);
89 goto exit;
92 /* Load bootloader file */
93 boot_buf = file_read(bootfile.toLocal8Bit().data(), &boot_size);
94 if (boot_buf == NULL)
96 emit logItem(errstr, LOGERROR);
97 emit logItem(tr("Could not load %1").arg(bootfile), LOGERROR);
98 goto exit;
101 /* Patch the firmware */
102 emit logItem(tr("Patching Firmware..."), LOGINFO);
104 patched_buf = patch_firmware_tcc(of_buf, of_size, boot_buf, boot_size,
105 &patched_size);
106 if (patched_buf == NULL)
108 emit logItem(errstr, LOGERROR);
109 emit logItem(tr("Could patch firmware"), LOGERROR);
110 goto exit;
113 if(!out.open(QIODevice::WriteOnly | QIODevice::Truncate))
115 emit logItem(tr("Could not open %1 for writing").arg(m_blfile),
116 LOGERROR);
117 goto exit;
120 n = out.write((char*)patched_buf, patched_size);
121 out.close();
122 if (n != patched_size)
124 emit logItem(tr("Could not write firmware file"), LOGERROR);
125 goto exit;
128 /* End of install */
129 emit logItem(tr("Success: modified firmware file created"), LOGINFO);
130 logInstall(LogAdd);
132 ret = true;
134 exit:
135 if (of_buf)
136 free(of_buf);
138 if (boot_buf)
139 free(boot_buf);
141 if (patched_buf)
142 free(patched_buf);
144 emit done(ret);
147 bool BootloaderInstallTcc::uninstall(void)
149 emit logItem("To uninstall, perform a normal upgrade with an unmodified original firmware", LOGINFO);
150 logInstall(LogRemove);
151 return false;
154 BootloaderInstallBase::BootloaderType BootloaderInstallTcc::installed(void)
156 return BootloaderUnknown;
159 BootloaderInstallBase::Capabilities BootloaderInstallTcc::capabilities(void)
161 return (Install | NeedsOf);