FS#10728 - Cowon D2: Add support for D2 in rbutil
[kugel-rb.git] / rbutil / rbutilqt / base / bootloaderinstalltcc.cpp
blob1d0a9e606ea2a4d043bdfa7e3ec1adb233279f44
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 /* Load bootloader file */
85 boot_buf = file_read(bootfile.toLocal8Bit().data(), &boot_size);
86 if (boot_buf == NULL)
88 emit logItem(errstr, LOGERROR);
89 emit logItem(tr("Could not load %1").arg(bootfile), LOGERROR);
90 goto exit;
93 /* Patch the firmware */
94 emit logItem(tr("Patching Firmware..."), LOGINFO);
96 patched_buf = patch_firmware_tcc(of_buf, of_size, boot_buf, boot_size,
97 &patched_size);
98 if (patched_buf == NULL)
100 emit logItem(errstr, LOGERROR);
101 emit logItem(tr("Could patch firmware"), LOGERROR);
102 goto exit;
105 if(!out.open(QIODevice::WriteOnly | QIODevice::Truncate))
107 emit logItem(tr("Could not open %1 for writing").arg(m_blfile),
108 LOGERROR);
109 goto exit;
112 n = out.write((char*)patched_buf, patched_size);
113 out.close();
114 if (n != patched_size)
116 emit logItem(tr("Could not write firmware file"), LOGERROR);
117 goto exit;
120 /* End of install */
121 emit logItem(tr("Success: modified firmware file created"), LOGINFO);
122 logInstall(LogAdd);
124 ret = true;
126 exit:
127 if (of_buf)
128 free(of_buf);
130 if (boot_buf)
131 free(boot_buf);
133 if (patched_buf)
134 free(patched_buf);
136 emit done(ret);
139 bool BootloaderInstallTcc::uninstall(void)
141 emit logItem("To uninstall, perform a normal upgrade with an unmodified original firmware", LOGINFO);
142 logInstall(LogRemove);
143 return false;
146 BootloaderInstallBase::BootloaderType BootloaderInstallTcc::installed(void)
148 return BootloaderUnknown;
151 BootloaderInstallBase::Capabilities BootloaderInstallTcc::capabilities(void)
153 return (Install | NeedsOf);