Fix some quotation marks. Thanks to Alexander Levin for pointing it out.
[Rockbox.git] / rbutil / rbutilqt / installbootloader.cpp
blob05b7be4e49da3dcf715e302b67b3bdec4da6701d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 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 "installbootloader.h"
21 #include "irivertools/checksums.h"
23 BootloaderInstaller::BootloaderInstaller(QObject* parent): QObject(parent)
28 void BootloaderInstaller::install(ProgressloggerInterface* dp)
30 m_dp = dp;
31 m_install = true;
32 m_dp->addItem(tr("Starting bootloader installation"),LOGINFO);
33 connect(this, SIGNAL(done(bool)), this, SLOT(installEnded(bool)));
35 if(m_bootloadermethod == "gigabeatf")
37 // connect internal signal
38 connect(this,SIGNAL(prepare()),this,SLOT(gigabeatPrepare()));
39 connect(this,SIGNAL(finish()),this,SLOT(gigabeatFinish()));
41 else if(m_bootloadermethod == "iaudio")
43 // connect internal signal
44 connect(this,SIGNAL(prepare()),this,SLOT(iaudioPrepare()));
45 connect(this,SIGNAL(finish()),this,SLOT(iaudioFinish()));
47 else if(m_bootloadermethod == "h10")
49 // connect internal signal
50 connect(this,SIGNAL(prepare()),this,SLOT(h10Prepare()));
51 connect(this,SIGNAL(finish()),this,SLOT(h10Finish()));
53 else if(m_bootloadermethod == "ipodpatcher")
55 // connect internal signal
56 connect(this,SIGNAL(prepare()),this,SLOT(ipodPrepare()));
57 connect(this,SIGNAL(finish()),this,SLOT(ipodFinish()));
59 else if(m_bootloadermethod == "sansapatcher")
61 // connect internal signal
62 connect(this,SIGNAL(prepare()),this,SLOT(sansaPrepare()));
63 connect(this,SIGNAL(finish()),this,SLOT(sansaFinish()));
65 else if(m_bootloadermethod == "fwpatcher")
67 // connect internal signal
68 connect(this,SIGNAL(prepare()),this,SLOT(iriverPrepare()));
69 connect(this,SIGNAL(finish()),this,SLOT(iriverFinish()));
71 else if(m_bootloadermethod == "mrobe100")
73 // connect internal signal
74 connect(this,SIGNAL(prepare()),this,SLOT(mrobe100Prepare()));
75 connect(this,SIGNAL(finish()),this,SLOT(mrobe100Finish()));
77 else
79 m_dp->addItem(tr("unsupported install Method"),LOGERROR);
80 emit done(true);
81 return;
84 emit prepare();
87 void BootloaderInstaller::uninstall(ProgressloggerInterface* dp)
89 m_dp = dp;
90 m_install = false;
91 m_dp->addItem(tr("Starting bootloader uninstallation"),LOGINFO);
92 connect(this, SIGNAL(done(bool)), this, SLOT(installEnded(bool)));
94 if(m_bootloadermethod == "gigabeatf")
96 // connect internal signal
97 connect(this,SIGNAL(prepare()),this,SLOT(gigabeatPrepare()));
99 else if(m_bootloadermethod == "iaudio")
101 m_dp->addItem(tr("No uninstallation possible"),LOGWARNING);
102 emit done(true);
103 return;
105 else if(m_bootloadermethod == "iaudio")
107 // connect internal signal
108 connect(this,SIGNAL(prepare()),this,SLOT(h10Prepare()));
110 else if(m_bootloadermethod == "ipodpatcher")
112 // connect internal signal
113 connect(this,SIGNAL(prepare()),this,SLOT(ipodPrepare()));
115 else if(m_bootloadermethod == "sansapatcher")
117 // connect internal signal
118 connect(this,SIGNAL(prepare()),this,SLOT(sansaPrepare()));
120 else if(m_bootloadermethod == "h10")
122 // connect internal signal
123 connect(this,SIGNAL(prepare()),this,SLOT(h10Prepare()));
125 else if(m_bootloadermethod == "mrobe100")
127 // connect internal signal
128 connect(this,SIGNAL(prepare()),this,SLOT(mrobe100Prepare()));
130 else if(m_bootloadermethod == "fwpatcher")
132 m_dp->addItem(tr("No uninstallation possible"),LOGWARNING);
133 emit done(true);
134 return;
136 else
138 m_dp->addItem(tr("unsupported install Method"),LOGERROR);
139 emit done(true);
140 return;
143 emit prepare();
146 void BootloaderInstaller::downloadRequestFinished(int id, bool error)
148 qDebug() << "BootloaderInstall::downloadRequestFinished" << id << error;
149 qDebug() << "error:" << getter->errorString();
151 downloadDone(error);
154 void BootloaderInstaller::downloadDone(bool error)
156 qDebug() << "Install::downloadDone, error:" << error;
158 // update progress bar
160 int max = m_dp->getProgressMax();
161 if(max == 0) {
162 max = 100;
163 m_dp->setProgressMax(max);
165 m_dp->setProgressValue(max);
166 if(getter->httpResponse() != 200) {
167 m_dp->addItem(tr("Download error: received HTTP error %1.").arg(getter->httpResponse()),LOGERROR);
168 m_dp->abort();
169 emit done(true);
170 return;
172 if(error) {
173 m_dp->addItem(tr("Download error: %1").arg(getter->errorString()),LOGERROR);
174 m_dp->abort();
175 emit done(true);
176 return;
178 else m_dp->addItem(tr("Download finished."),LOGOK);
180 emit finish();
184 void BootloaderInstaller::updateDataReadProgress(int read, int total)
186 m_dp->setProgressMax(total);
187 m_dp->setProgressValue(read);
188 qDebug() << "progress:" << read << "/" << total;
192 void BootloaderInstaller::installEnded(bool error)
194 (void) error;
195 m_dp->abort();
198 bool BootloaderInstaller::downloadInfo()
200 // try to get the current build information
201 infodownloader = new HttpGet(this);
203 connect(infodownloader, SIGNAL(done(bool)), this, SLOT(infoDownloadDone(bool)));
204 connect(infodownloader, SIGNAL(requestFinished(int, bool)), this, SLOT(infoRequestFinished(int, bool)));
206 qDebug() << "downloading bootloader info";
207 infodownloader->setFile(&bootloaderInfo);
208 infodownloader->getFile(QUrl(m_bootloaderinfoUrl));
210 // block until its downloaded
211 qDebug() << "Waiting for Download finished";
212 infoDownloaded=false;
213 infoError = false;
214 while(!infoDownloaded )
215 QCoreApplication::processEvents();
216 return !infoError;
219 void BootloaderInstaller::infoDownloadDone(bool error)
221 if(error)
223 qDebug() << "network error:" << infodownloader->error();
224 return;
226 qDebug() << "network status:" << infodownloader->error();
228 infoDownloaded = true;
231 void BootloaderInstaller::infoRequestFinished(int id, bool error)
234 if(error)
236 QString errorString;
237 errorString = tr("Network error: %1. Please check your network and proxy settings.")
238 .arg(infodownloader->errorString());
239 #ifndef CONSOLE
240 if(error) QMessageBox::about(NULL, "Network Error", errorString);
241 #endif
242 qDebug() << "downloadDone:" << id << error;
244 infoError = true;
245 infoDownloaded = true;
247 qDebug() << "infoRequestFinished:" << id << error;
251 void BootloaderInstaller::createInstallLog()
253 m_dp->addItem(tr("Creating installation log"),LOGINFO);
254 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
256 bootloaderInfo.open();
257 QSettings info(bootloaderInfo.fileName(), QSettings::IniFormat, this);
258 bootloaderInfo.close();
259 info.beginGroup(m_device);
261 installlog.beginGroup("Bootloader");
262 installlog.setValue("md5sum",info.value("md5sum").toString());
263 installlog.endGroup();
264 installlog.sync();
267 void BootloaderInstaller::removeInstallLog()
269 m_dp->addItem(tr("Editing installation log"),LOGINFO);
270 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
271 installlog.beginGroup("Bootloader");
272 installlog.remove("md5sum");
273 installlog.endGroup();
274 installlog.sync();
278 bool BootloaderInstaller::uptodate()
280 QString installedMd5;
281 QString serverMd5;
283 QSettings installlog(m_mountpoint + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
284 installlog.beginGroup("Bootloader");
285 installedMd5 = installlog.value("md5sum").toString();
286 installlog.endGroup();
288 if(installedMd5.isEmpty())
289 return false;
290 bootloaderInfo.open();
291 QSettings info(bootloaderInfo.fileName(), QSettings::IniFormat, this);
292 bootloaderInfo.close();
293 info.beginGroup(m_device);
294 serverMd5 = info.value("md5sum").toString();
295 info.endGroup();
297 if(installedMd5 != serverMd5)
298 return false;
299 else
300 return true;
303 /**************************************************
304 *** gigabeat secific code
305 ***************************************************/
307 void BootloaderInstaller::gigabeatPrepare()
309 if(m_install) // Installation
311 QString url = m_bootloaderUrlBase + "/gigabeat/" + m_bootloadername;
313 m_dp->addItem(tr("Downloading file %1.%2")
314 .arg(QFileInfo(url).baseName(), QFileInfo(url).completeSuffix()),LOGINFO);
316 // temporary file needs to be opened to get the filename
317 downloadFile.open();
318 m_tempfilename = downloadFile.fileName();
319 downloadFile.close();
320 // get the real file.
321 getter = new HttpGet(this);
322 getter->setFile(&downloadFile);
323 // connect signals from HttpGet
324 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
325 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
326 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
328 getter->getFile(QUrl(url));
330 else //UnInstallation
332 QString firmware = m_mountpoint + "/GBSYSTEM/FWIMG/FWIMG01.DAT";
333 QString firmwareOrig = firmware.append(".ORIG");
335 QFileInfo firmwareOrigFI(firmwareOrig);
337 // check if original firmware exists
338 if(!firmwareOrigFI.exists())
340 m_dp->addItem(tr("Could not find the Original Firmware at: %1")
341 .arg(firmwareOrig),LOGERROR);
342 emit done(true);
343 return;
346 QFile firmwareFile(firmware);
347 QFile firmwareOrigFile(firmwareOrig);
349 //remove modified firmware
350 if(!firmwareFile.remove())
352 m_dp->addItem(tr("Could not remove the Firmware at: %1")
353 .arg(firmware),LOGERROR);
354 emit done(true);
355 return;
358 //copy original firmware
359 if(!firmwareOrigFile.copy(firmware))
361 m_dp->addItem(tr("Could not copy the Firmware from: %1 to %2")
362 .arg(firmwareOrig,firmware),LOGERROR);
363 emit done(true);
364 return;
367 removeInstallLog();
369 emit done(false); //success
374 void BootloaderInstaller::gigabeatFinish()
376 // this step is only need for installation, so no code for uninstall here
378 m_dp->addItem(tr("Finishing bootloader install"),LOGINFO);
380 QString firmware = m_mountpoint + "/GBSYSTEM/FWIMG/" + m_bootloadername;
382 QFileInfo firmwareFI(firmware);
384 // check if firmware exists
385 if(!firmwareFI.exists())
387 m_dp->addItem(tr("Could not find the Firmware at: %1")
388 .arg(firmware),LOGERROR);
389 emit done(true);
390 return;
393 QString firmwareOrig = firmware;
394 firmwareOrig.append(".ORIG");
395 QFileInfo firmwareOrigFI(firmwareOrig);
397 // rename and backup the firmware, if there is no original firmware there
398 if(!firmwareOrigFI.exists())
400 QFile firmwareFile(firmware);
401 //backup
402 QDir::home().mkdir("Gigabeat Original Firmware Backup");
403 firmwareFile.copy(QDir::toNativeSeparators(QDir::homePath()) + QDir::toNativeSeparators("/Gigabeat Original Firmware Backup/") + m_bootloadername);
405 //rename
406 if(!firmwareFile.rename(firmwareOrig))
408 m_dp->addItem(tr("Could not rename: %1 to %2")
409 .arg(firmware,firmwareOrig),LOGERROR);
410 emit done(true);
411 return;
414 else // or remove the normal firmware, if the original is there
416 QFile firmwareFile(firmware);
417 firmwareFile.remove();
420 //copy the firmware
421 if(!downloadFile.copy(firmware))
423 m_dp->addItem(tr("Could not copy: %1 to %2")
424 .arg(m_tempfilename,firmware),LOGERROR);
425 emit done(true);
426 return;
429 downloadFile.remove();
431 createInstallLog();
433 m_dp->addItem(tr("Bootloader install finished successfully."),LOGOK);
434 m_dp->addItem(tr("To finish the Bootloader installation, follow the steps below."),LOGINFO);
435 m_dp->addItem(tr("1. Eject/Unmount your Device."),LOGINFO);
436 m_dp->addItem(tr("2. Unplug USB and any Power adapters."),LOGINFO);
437 m_dp->addItem(tr("3. Hold POWER to turn the Device off."),LOGINFO);
438 m_dp->addItem(tr("4. Toggle the Battery switch on the Device."),LOGINFO);
439 m_dp->addItem(tr("5. Hold POWER to boot the Rockbox bootloader."),LOGINFO);
442 m_dp->abort();
444 emit done(false); // success
448 /**************************************************
449 *** iaudio secific code
450 ***************************************************/
451 void BootloaderInstaller::iaudioPrepare()
454 QString url = m_bootloaderUrlBase + "/iaudio/" + m_bootloadername;
456 m_dp->addItem(tr("Downloading file %1.%2")
457 .arg(QFileInfo(url).baseName(), QFileInfo(url).completeSuffix()),LOGINFO);
459 // temporary file needs to be opened to get the filename
460 downloadFile.open();
461 m_tempfilename = downloadFile.fileName();
462 downloadFile.close();
463 // get the real file.
464 getter = new HttpGet(this);
465 getter->setFile(&downloadFile);
466 getter->getFile(QUrl(url));
467 // connect signals from HttpGet
468 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
469 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
470 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
473 void BootloaderInstaller::iaudioFinish()
475 QString firmware = m_mountpoint + "/FIRMWARE/" + m_bootloadername;
477 //copy the firmware
478 if(!downloadFile.copy(firmware))
480 m_dp->addItem(tr("Could not copy: %1 to %2")
481 .arg(m_tempfilename,firmware),LOGERROR);
482 emit done(true);
483 return;
486 downloadFile.remove();
488 createInstallLog();
490 m_dp->addItem(tr("Bootloader install finished successfully."),LOGOK);
491 m_dp->addItem(tr("To finish the Bootloader installation, follow the steps below."),LOGINFO);
492 m_dp->addItem(tr("1. Eject/Unmount your Device."),LOGINFO);
493 m_dp->addItem(tr("2. Turn you Device OFF."),LOGINFO);
494 m_dp->addItem(tr("3. Insert Charger."),LOGINFO);
496 m_dp->abort();
498 emit done(false); // success
503 /**************************************************
504 *** h10 secific code
505 ***************************************************/
506 void BootloaderInstaller::h10Prepare()
508 if(m_install) // Installation
510 QString url = m_bootloaderUrlBase + "/iriver/" + m_bootloadername;
512 m_dp->addItem(tr("Downloading file %1.%2")
513 .arg(QFileInfo(url).baseName(), QFileInfo(url).completeSuffix()),LOGINFO);
515 // temporary file needs to be opened to get the filename
516 downloadFile.open();
517 m_tempfilename = downloadFile.fileName();
518 downloadFile.close();
519 // get the real file.
520 getter = new HttpGet(this);
521 getter->setFile(&downloadFile);
522 // connect signals from HttpGet
523 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
524 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
525 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
527 getter->getFile(QUrl(url));
529 else // Uninstallation
532 QString firmwarename = m_bootloadername.section('/', -1);
534 QString firmware = m_mountpoint + "/SYSTEM/" + firmwarename;
535 QString firmwareOrig = m_mountpoint + "/SYSTEM/OF.mi4";
537 QFileInfo firmwareFI(firmware);
538 if(!firmwareFI.exists()) //Firmware dosent exists on player
540 firmware = m_mountpoint + "/SYSTEM/H10EMP.mi4"; //attempt other firmwarename
541 firmwareFI.setFile(firmware);
542 if(!firmwareFI.exists()) //Firmware dosent exists on player
544 m_dp->addItem(tr("Firmware does not exist: %1")
545 .arg(firmware),LOGERROR);
546 emit done(true);
547 return;
551 QFileInfo firmwareOrigFI(firmwareOrig);
552 if(!firmwareOrigFI.exists()) //Original Firmware dosent exists on player
554 m_dp->addItem(tr("Original Firmware does not exist: %1")
555 .arg(firmwareOrig),LOGERROR);
556 emit done(true);
557 return;
560 QFile firmwareFile(firmware);
561 QFile firmwareOrigFile(firmwareOrig);
563 //remove modified firmware
564 if(!firmwareFile.remove())
566 m_dp->addItem(tr("Could not remove the Firmware at: %1")
567 .arg(firmware),LOGERROR);
568 emit done(true);
569 return;
572 //copy original firmware
573 if(!firmwareOrigFile.copy(firmware))
575 m_dp->addItem(tr("Could not copy the Firmware from: %1 to %2")
576 .arg(firmwareOrig,firmware),LOGERROR);
577 emit done(true);
578 return;
581 removeInstallLog();
583 emit done(false); //success
588 void BootloaderInstaller::h10Finish()
590 QString firmwarename = m_bootloadername.section('/', -1);
592 QString firmware = m_mountpoint + "/SYSTEM/" + firmwarename;
593 QString firmwareOrig = m_mountpoint + "/SYSTEM/OF.mi4";
595 QFileInfo firmwareFI(firmware);
597 if(!firmwareFI.exists()) //Firmware dosent exists on player
599 firmware = m_mountpoint + "/SYSTEM/H10EMP.mi4"; //attempt other firmwarename
600 firmwareFI.setFile(firmware);
601 if(!firmwareFI.exists()) //Firmware dosent exists on player
603 m_dp->addItem(tr("Firmware does not exist: %1")
604 .arg(firmware),LOGERROR);
605 emit done(true);
606 return;
610 QFileInfo firmwareOrigFI(firmwareOrig);
612 if(!firmwareOrigFI.exists())
614 QFile firmwareFile(firmware);
616 //backup
617 QDir::home().mkdir("Iriver H10 Original Firmware Backup");
618 firmwareFile.copy(QDir::toNativeSeparators(QDir::homePath()) + QDir::toNativeSeparators("/Iriver H10 Original Firmware Backup/") + m_bootloadername);
620 //rename
621 if(!firmwareFile.rename(firmwareOrig)) //rename Firmware to Original
623 m_dp->addItem(tr("Could not rename: %1 to %2")
624 .arg(firmware,firmwareOrig),LOGERROR);
625 emit done(true);
626 return;
629 else //there is already a original firmware
631 QFile firmwareFile(firmware);
632 firmwareFile.remove();
634 //copy the firmware
635 if(!downloadFile.copy(firmware))
637 m_dp->addItem(tr("Could not copy: %1 to %2")
638 .arg(m_tempfilename,firmware),LOGERROR);
639 emit done(true);
640 return;
643 downloadFile.remove();
645 createInstallLog();
647 m_dp->addItem(tr("Bootloader install finished successfully."),LOGOK);
648 m_dp->abort();
650 emit done(false); // success
656 /**************************************************
657 *** mrobe100 secific code
658 ***************************************************/
659 void BootloaderInstaller::mrobe100Prepare()
661 if(m_install) // Installation
663 QString url = m_bootloaderUrlBase + "/olympus/mrobe100/" + m_bootloadername;
665 m_dp->addItem(tr("Downloading file %1.%2")
666 .arg(QFileInfo(url).baseName(), QFileInfo(url).completeSuffix()),LOGINFO);
668 // temporary file needs to be opened to get the filename
669 downloadFile.open();
670 m_tempfilename = downloadFile.fileName();
671 downloadFile.close();
672 // get the real file.
673 getter = new HttpGet(this);
674 getter->setFile(&downloadFile);
676 // connect signals from HttpGet
677 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
678 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
679 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
681 getter->getFile(QUrl(url));
683 else // Uninstallation
686 QString firmwarename = m_bootloadername;
688 QString firmware = m_mountpoint + "/SYSTEM/" + firmwarename;
689 QString firmwareOrig = m_mountpoint + "/SYSTEM/OF.mi4";
691 QFileInfo firmwareFI(firmware);
692 if(!firmwareFI.exists()) //Firmware dosent exists on player
694 m_dp->addItem(tr("Firmware does not exist: %1")
695 .arg(firmware),LOGERROR);
696 emit done(true);
697 return;
700 QFileInfo firmwareOrigFI(firmwareOrig);
701 if(!firmwareOrigFI.exists()) //Original Firmware dosent exists on player
703 m_dp->addItem(tr("Original Firmware does not exist: %1")
704 .arg(firmwareOrig),LOGERROR);
705 emit done(true);
706 return;
709 QFile firmwareFile(firmware);
710 QFile firmwareOrigFile(firmwareOrig);
712 //remove modified firmware
713 if(!firmwareFile.remove())
715 m_dp->addItem(tr("Could not remove the Firmware at: %1")
716 .arg(firmware),LOGERROR);
717 emit done(true);
718 return;
721 //copy original firmware
722 if(!firmwareOrigFile.copy(firmware))
724 m_dp->addItem(tr("Could not copy the Firmware from: %1 to %2")
725 .arg(firmwareOrig,firmware),LOGERROR);
726 emit done(true);
727 return;
730 removeInstallLog();
732 emit done(false); //success
737 void BootloaderInstaller::mrobe100Finish()
739 QString firmwarename = m_bootloadername;
741 QString firmware = m_mountpoint + "/SYSTEM/" + firmwarename;
742 QString firmwareOrig = m_mountpoint + "/SYSTEM/OF.mi4";
744 QFileInfo firmwareFI(firmware);
746 if(!firmwareFI.exists()) //Firmware dosent exists on player
748 m_dp->addItem(tr("Firmware does not exist: %1")
749 .arg(firmware),LOGERROR);
750 emit done(true);
751 return;
754 QFileInfo firmwareOrigFI(firmwareOrig);
756 if(!firmwareOrigFI.exists())
758 QFile firmwareFile(firmware);
760 //backup
761 QDir::home().mkdir("Olympus mrobe100 Original Firmware Backup");
762 firmwareFile.copy(QDir::toNativeSeparators(QDir::homePath()) + QDir::toNativeSeparators("/Olympus mrobe100 Original Firmware Backup/") + m_bootloadername);
764 //rename
765 if(!firmwareFile.rename(firmwareOrig)) //rename Firmware to Original
767 m_dp->addItem(tr("Could not rename: %1 to %2")
768 .arg(firmware,firmwareOrig),LOGERROR);
769 emit done(true);
770 return;
773 else //there is already a original firmware
775 QFile firmwareFile(firmware);
776 firmwareFile.remove();
778 //copy the firmware
779 if(!downloadFile.copy(firmware))
781 m_dp->addItem(tr("Could not copy: %1 to %2")
782 .arg(m_tempfilename,firmware),LOGERROR);
783 emit done(true);
784 return;
787 downloadFile.remove();
789 createInstallLog();
791 m_dp->addItem(tr("Bootloader install finished successfully."),LOGOK);
792 m_dp->abort();
794 emit done(false); // success
798 /**************************************************
799 *** ipod secific code
800 ***************************************************/
801 int verbose =0;
802 // reserves memory for ipodpatcher
803 bool initIpodpatcher()
805 if (ipod_alloc_buffer(&sectorbuf,BUFFER_SIZE) < 0) return true;
806 else return false;
809 void BootloaderInstaller::ipodPrepare()
811 m_dp->addItem(tr("Searching for ipods"),LOGINFO);
812 struct ipod_t ipod;
814 int n = ipod_scan(&ipod);
815 if (n == 0)
817 m_dp->addItem(tr("No Ipods found"),LOGERROR);
818 emit done(true);
819 return;
821 if (n > 1)
823 m_dp->addItem(tr("Too many Ipods found"),LOGERROR);
824 emit done(true);
827 if(m_install) // Installation
830 QString url = m_bootloaderUrlBase + "/ipod/bootloader-" + m_bootloadername + ".ipod";
832 m_dp->addItem(tr("Downloading file %1.%2")
833 .arg(QFileInfo(url).baseName(), QFileInfo(url).completeSuffix()),LOGINFO);
835 // temporary file needs to be opened to get the filename
836 downloadFile.open();
837 m_tempfilename = downloadFile.fileName();
838 downloadFile.close();
839 // get the real file.
840 getter = new HttpGet(this);
841 getter->setFile(&downloadFile);
843 // connect signals from HttpGet
844 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
845 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
846 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
848 getter->getFile(QUrl(url));
850 else // Uninstallation
852 if (ipod_open(&ipod, 0) < 0)
854 m_dp->addItem(tr("could not open ipod"),LOGERROR);
855 emit done(true);
856 return;
859 if (read_partinfo(&ipod,0) < 0)
861 m_dp->addItem(tr("could not read partitiontable"),LOGERROR);
862 emit done(true);
863 return;
866 if (ipod.pinfo[0].start==0)
868 m_dp->addItem(tr("No partition 0 on disk"),LOGERROR);
870 int i;
871 double sectors_per_MB = (1024.0*1024.0)/ipod.sector_size;
872 m_dp->addItem(tr("[INFO] Part Start Sector End Sector Size (MB) Type\n"),LOGINFO);
873 for ( i = 0; i < 4; i++ )
875 if (ipod.pinfo[i].start != 0)
877 m_dp->addItem(tr("[INFO] %1 %2 %3 %4 %5 (%6)").arg(
878 i).arg(
879 ipod.pinfo[i].start).arg(
880 ipod.pinfo[i].start+ipod.pinfo[i].size-1).arg(
881 ipod.pinfo[i].size/sectors_per_MB).arg(
882 get_parttype(ipod.pinfo[i].type)).arg(
883 ipod.pinfo[i].type),LOGINFO);
886 emit done(true);
887 return;
890 read_directory(&ipod);
892 if (ipod.nimages <= 0)
894 m_dp->addItem(tr("Failed to read firmware directory"),LOGERROR);
895 emit done(true);
896 return;
898 if (getmodel(&ipod,(ipod.ipod_directory[0].vers>>8)) < 0)
900 m_dp->addItem(tr("Unknown version number in firmware (%1)").arg(
901 ipod.ipod_directory[0].vers),LOGERROR);
902 emit done(true);
903 return;
906 if (ipod.macpod)
908 m_dp->addItem(tr("Warning this is a MacPod, Rockbox doesnt work on "
909 "this. Convert it to WinPod\n"
910 "See http://www.rockbox.org/wiki/IpodConversionToFAT32"),
911 LOGWARNING);
912 emit done(true);
913 return;
916 if (ipod_reopen_rw(&ipod) < 0)
918 m_dp->addItem(tr("Could not open Ipod in RW mode"),LOGERROR);
919 emit done(true);
920 return;
923 if (ipod.ipod_directory[0].entryOffset==0) {
924 m_dp->addItem(tr("No bootloader detected."),LOGERROR);
925 emit done(true);
926 return;
929 if (delete_bootloader(&ipod)==0)
931 m_dp->addItem(tr("Successfully removed Bootloader"),LOGOK);
932 removeInstallLog();
933 emit done(false);
934 ipod_close(&ipod);
935 return;
937 else
939 m_dp->addItem(tr("--delete-bootloader failed."),LOGERROR);
940 emit done(true);
941 ipod_close(&ipod);
942 return;
947 void BootloaderInstaller::ipodFinish()
949 struct ipod_t ipod;
950 ipod_scan(&ipod);
952 if (ipod_open(&ipod, 0) < 0)
954 m_dp->addItem(tr("could not open ipod"),LOGERROR);
955 emit done(true);
956 return;
959 if (read_partinfo(&ipod,0) < 0)
961 m_dp->addItem(tr("could not read partitiontable"),LOGERROR);
962 emit done(true);
963 return;
966 if (ipod.pinfo[0].start==0)
968 m_dp->addItem(tr("No partition 0 on disk"),LOGERROR);
970 int i;
971 double sectors_per_MB = (1024.0*1024.0)/ipod.sector_size;
973 m_dp->addItem(tr("[INFO] Part Start Sector End Sector Size (MB) Type\n"),LOGINFO);
975 for ( i = 0; i < 4; i++ )
977 if (ipod.pinfo[i].start != 0)
979 m_dp->addItem(tr("[INFO] %1 %2 %3 %4 %5 (%6)").arg(
980 i).arg(
981 ipod.pinfo[i].start).arg(
982 ipod.pinfo[i].start+ipod.pinfo[i].size-1).arg(
983 ipod.pinfo[i].size/sectors_per_MB).arg(
984 get_parttype(ipod.pinfo[i].type)).arg(
985 ipod.pinfo[i].type),LOGWARNING);
988 emit done(true);
989 return;
992 read_directory(&ipod);
994 if (ipod.nimages <= 0)
996 m_dp->addItem(tr("Failed to read firmware directory"),LOGERROR);
997 emit done(true);
998 return;
1000 if (getmodel(&ipod,(ipod.ipod_directory[0].vers>>8)) < 0)
1002 m_dp->addItem(tr("Unknown version number in firmware (%1)").arg(
1003 ipod.ipod_directory[0].vers),LOGERROR);
1004 emit done(true);
1005 return;
1008 if (ipod.macpod)
1010 m_dp->addItem(tr("Warning this is a MacPod, Rockbox doesnt work on "
1011 "this. Convert it to WinPod\n"
1012 "See http://www.rockbox.org/wiki/IpodConversionToFAT32"),
1013 LOGWARNING);
1014 emit done(true);
1015 return;
1018 if (ipod_reopen_rw(&ipod) < 0)
1020 m_dp->addItem(tr("Could not open Ipod in RW mode"),LOGERROR);
1021 emit done(true);
1022 return;
1025 if (add_bootloader(&ipod, m_tempfilename.toLatin1().data(), FILETYPE_DOT_IPOD)==0)
1027 m_dp->addItem(tr("Successfully added Bootloader"),LOGOK);
1028 createInstallLog();
1029 emit done(false);
1030 ipod_close(&ipod);
1031 return;
1033 else
1035 m_dp->addItem(tr("failed to add Bootloader"),LOGERROR);
1036 ipod_close(&ipod);
1037 emit done(true);
1038 return;
1042 /**************************************************
1043 *** sansa secific code
1044 ***************************************************/
1045 // reserves memory for sansapatcher
1046 bool initSansapatcher()
1048 if (sansa_alloc_buffer(&sectorbuf,BUFFER_SIZE) < 0) return true;
1049 else return false;
1053 void BootloaderInstaller::sansaPrepare()
1055 m_dp->addItem(tr("Searching for sansas"),LOGINFO);
1056 struct sansa_t sansa;
1058 int n = sansa_scan(&sansa);
1059 if (n == 0)
1061 m_dp->addItem(tr("No Sansa found"),LOGERROR);
1062 emit done(true);
1063 return;
1065 if (n > 1)
1067 m_dp->addItem(tr("Too many Sansas found"),LOGERROR);
1068 emit done(true);
1071 if(m_install) // Installation
1073 QString url = m_bootloaderUrlBase + "/sandisk-sansa/"
1074 + QString(sansa.targetname) + "/" + m_bootloadername;
1076 m_dp->addItem(tr("Downloading file %1.%2")
1077 .arg(QFileInfo(url).baseName(), QFileInfo(url).completeSuffix()),LOGINFO);
1079 // temporary file needs to be opened to get the filename
1080 downloadFile.open();
1081 m_tempfilename = downloadFile.fileName();
1082 downloadFile.close();
1083 // get the real file.
1084 getter = new HttpGet(this);
1085 getter->setFile(&downloadFile);
1087 // connect signals from HttpGet
1088 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
1089 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
1090 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
1092 getter->getFile(QUrl(url));
1094 else // Uninstallation
1097 if (sansa_open(&sansa, 0) < 0)
1099 m_dp->addItem(tr("could not open Sansa"),LOGERROR);
1100 emit done(true);
1101 return;
1104 if (sansa_read_partinfo(&sansa,0) < 0)
1106 m_dp->addItem(tr("could not read partitiontable"),LOGERROR);
1107 emit done(true);
1108 return;
1111 int i = is_sansa(&sansa);
1112 if (i < 0) {
1113 m_dp->addItem(tr("Disk is not a Sansa (%1), aborting.").arg(i),LOGERROR);
1114 emit done(true);
1115 return;
1118 if (sansa.hasoldbootloader)
1120 m_dp->addItem(tr("********************************************\n"
1121 "OLD ROCKBOX INSTALLATION DETECTED, ABORTING.\n"
1122 "You must reinstall the original Sansa firmware before running\n"
1123 "sansapatcher for the first time.\n"
1124 "See http://www.rockbox.org/twiki/bin/view/Main/SansaE200Install\n"
1125 "*********************************************\n"),LOGERROR);
1126 emit done(true);
1127 return;
1131 if (sansa_reopen_rw(&sansa) < 0)
1133 m_dp->addItem(tr("Could not open Sansa in RW mode"),LOGERROR);
1134 emit done(true);
1135 return;
1138 if (sansa_delete_bootloader(&sansa)==0)
1140 m_dp->addItem(tr("Successfully removed Bootloader"),LOGOK);
1141 removeInstallLog();
1142 emit done(false);
1143 sansa_close(&sansa);
1144 return;
1146 else
1148 m_dp->addItem(tr("--delete-bootloader failed."),LOGERROR);
1149 emit done(true);
1150 sansa_close(&sansa);
1151 return;
1156 void BootloaderInstaller::sansaFinish()
1158 struct sansa_t sansa;
1159 sansa_scan(&sansa);
1161 if (sansa_open(&sansa, 0) < 0)
1163 m_dp->addItem(tr("could not open Sansa"),LOGERROR);
1164 emit done(true);
1165 return;
1168 if (sansa_read_partinfo(&sansa,0) < 0)
1170 m_dp->addItem(tr("could not read partitiontable"),LOGERROR);
1171 emit done(true);
1172 return;
1176 int i = is_sansa(&sansa);
1177 if (i < 0) {
1179 m_dp->addItem(tr("Disk is not a Sansa (%1), aborting.").arg(i),LOGERROR);
1180 emit done(true);
1181 return;
1184 if (sansa.hasoldbootloader)
1186 m_dp->addItem(tr("********************************************\n"
1187 "OLD ROCKBOX INSTALLATION DETECTED, ABORTING.\n"
1188 "You must reinstall the original Sansa firmware before running\n"
1189 "sansapatcher for the first time.\n"
1190 "See http://www.rockbox.org/twiki/bin/view/Main/SansaE200Install\n"
1191 "*********************************************\n"),LOGERROR);
1192 emit done(true);
1193 return;
1196 if (sansa_reopen_rw(&sansa) < 0)
1198 m_dp->addItem(tr("Could not open Sansa in RW mode"),LOGERROR);
1199 emit done(true);
1200 return;
1203 if (sansa_add_bootloader(&sansa, m_tempfilename.toLatin1().data(), FILETYPE_MI4)==0)
1205 m_dp->addItem(tr("Successfully added Bootloader"),LOGOK);
1206 createInstallLog();
1207 emit done(false);
1208 sansa_close(&sansa);
1209 return;
1211 else
1213 m_dp->addItem(tr("failed to add Bootloader"),LOGERROR);
1214 sansa_close(&sansa);
1215 emit done(true);
1216 return;
1221 /**************************************************
1222 *** iriver /fwpatcher secific code
1223 ***************************************************/
1225 void BootloaderInstaller::iriverPrepare()
1227 char md5sum_str[32];
1228 if (!FileMD5(m_origfirmware, md5sum_str)) {
1229 m_dp->addItem(tr("Could not MD5Sum original firmware"),LOGERROR);
1230 emit done(true);
1231 return;
1234 /* Check firmware against md5sums in h120sums and h100sums */
1235 series = 0;
1236 table_entry = intable(md5sum_str, &h120pairs[0],
1237 sizeof(h120pairs)/sizeof(struct sumpairs));
1238 if (table_entry >= 0) {
1239 series = 120;
1241 else
1243 table_entry = intable(md5sum_str, &h100pairs[0],
1244 sizeof(h100pairs)/sizeof(struct sumpairs));
1245 if (table_entry >= 0)
1247 series = 100;
1249 else
1251 table_entry = intable(md5sum_str, &h300pairs[0],
1252 sizeof(h300pairs)/sizeof(struct sumpairs));
1253 if (table_entry >= 0)
1254 series = 300;
1257 if (series == 0)
1259 m_dp->addItem(tr("Could not detect firmware type"),LOGERROR);
1260 emit done(true);
1261 return;
1264 QString url = m_bootloaderUrlBase + "/iriver/" + m_bootloadername;
1266 m_dp->addItem(tr("Downloading file %1.%2")
1267 .arg(QFileInfo(url).baseName(), QFileInfo(url).completeSuffix()),LOGINFO);
1269 // temporary file needs to be opened to get the filename
1270 downloadFile.open();
1271 m_tempfilename = downloadFile.fileName();
1272 downloadFile.close();
1273 // get the real file.
1274 getter = new HttpGet(this);
1275 getter->setFile(&downloadFile);
1277 // connect signals from HttpGet
1278 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
1279 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
1280 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
1282 getter->getFile(QUrl(url));
1285 void BootloaderInstaller::iriverFinish()
1287 // Patch firmware
1288 char md5sum_str[32];
1289 struct sumpairs *sums = 0;
1290 int origin = 0;
1292 /* get pointer to the correct bootloader.bin */
1293 switch(series) {
1294 case 100:
1295 sums = &h100pairs[0];
1296 origin = 0x1f0000;
1297 break;
1298 case 120:
1299 sums = &h120pairs[0];
1300 origin = 0x1f0000;
1301 break;
1302 case 300:
1303 sums = &h300pairs[0];
1304 origin = 0x3f0000;
1305 break;
1308 // temporary files needs to be opened to get the filename
1309 QTemporaryFile firmwareBin, newBin, newHex;
1310 firmwareBin.open();
1311 newBin.open();
1312 newHex.open();
1313 QString firmwareBinName = firmwareBin.fileName();
1314 QString newBinName = newBin.fileName();
1315 QString newHexName = newHex.fileName();
1316 firmwareBin.close();
1317 newBin.close();
1318 newHex.close();
1320 // iriver decode
1321 if (iriver_decode(m_origfirmware, firmwareBinName, FALSE, STRIP_NONE,m_dp) == -1)
1323 m_dp->addItem(tr("Error in descramble"),LOGERROR);
1324 firmwareBin.remove();
1325 newBin.remove();
1326 newHex.remove();
1327 emit done(true);
1328 return;
1330 // mkboot
1331 if (!mkboot(firmwareBinName, newBinName, m_tempfilename, origin,m_dp))
1333 m_dp->addItem(tr("Error in patching"),LOGERROR);
1334 firmwareBin.remove();
1335 newBin.remove();
1336 newHex.remove();
1337 emit done(true);
1338 return;
1340 // iriver_encode
1341 if (iriver_encode(newBinName, newHexName, FALSE,m_dp) == -1)
1343 m_dp->addItem(tr("Error in scramble"),LOGERROR);
1344 firmwareBin.remove();
1345 newBin.remove();
1346 newHex.remove();
1347 emit done(true);
1348 return;
1351 /* now md5sum it */
1352 if (!FileMD5(newHexName, md5sum_str))
1354 m_dp->addItem(tr("Error in checksumming"),LOGERROR);
1355 firmwareBin.remove();
1356 newBin.remove();
1357 newHex.remove();
1358 emit done(true);
1359 return;
1361 if (strncmp(sums[table_entry].patched, md5sum_str, 32) == 0) {
1362 /* delete temp files */
1363 firmwareBin.remove();
1364 newBin.remove();
1367 // Load patched Firmware to player
1368 QString dest;
1369 if(series == 100)
1370 dest = m_mountpoint + "/ihp_100.hex";
1371 else if(series == 120)
1372 dest = m_mountpoint + "/ihp_120.hex";
1373 else if(series == 300)
1374 dest = m_mountpoint + "/H300.hex";
1376 // copy file
1377 QFile destfile(dest);
1378 if(destfile.exists()) destfile.remove();
1379 if(!newHex.copy(dest))
1381 m_dp->addItem(tr("Could not copy: %1 to %2")
1382 .arg(newHexName,dest),LOGERROR);
1383 emit done(true);
1384 return;
1387 downloadFile.remove();
1388 newHex.remove();
1390 createInstallLog();
1392 m_dp->addItem(tr("Bootloader install finished successfully."),LOGOK);
1393 m_dp->addItem(tr("To finish the Bootloader installation, follow the steps below."),LOGINFO);
1394 m_dp->addItem(tr("1. Eject/Unmount your Device."),LOGINFO);
1395 m_dp->addItem(tr("2. Boot into the original Firmware."),LOGINFO);
1396 m_dp->addItem(tr("3. Use the Firmware flash option in the Original Firmware."),LOGINFO);
1397 m_dp->addItem(tr("4. Reboot."),LOGINFO);
1398 m_dp->abort();
1400 emit done(false); // success