add global proxy / cache settings to httpget class. This removes the need of passing...
[Rockbox.git] / rbutil / rbutilqt / installbootloader.cpp
blob3c6ca82c1bd94fca8ee7e842aa871a5b6b096657
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 bootloaderInfo.open();
289 QSettings info(bootloaderInfo.fileName(), QSettings::IniFormat, this);
290 bootloaderInfo.close();
291 info.beginGroup(m_device);
292 serverMd5 = info.value("md5sum").toString();
293 info.endGroup();
295 if(installedMd5 != serverMd5)
296 return false;
297 else
298 return true;
301 /**************************************************
302 *** gigabeat secific code
303 ***************************************************/
305 void BootloaderInstaller::gigabeatPrepare()
307 if(m_install) // Installation
309 QString url = m_bootloaderUrlBase + "/gigabeat/" + m_bootloadername;
311 m_dp->addItem(tr("Downloading file %1.%2")
312 .arg(QFileInfo(url).baseName(), QFileInfo(url).completeSuffix()),LOGINFO);
314 // temporary file needs to be opened to get the filename
315 downloadFile.open();
316 m_tempfilename = downloadFile.fileName();
317 downloadFile.close();
318 // get the real file.
319 getter = new HttpGet(this);
320 getter->setFile(&downloadFile);
321 getter->getFile(QUrl(url));
322 // connect signals from HttpGet
323 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
324 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
325 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
327 else //UnInstallation
329 QString firmware = m_mountpoint + "/GBSYSTEM/FWIMG/FWIMG01.DAT";
330 QString firmwareOrig = firmware.append(".ORIG");
332 QFileInfo firmwareOrigFI(firmwareOrig);
334 // check if original firmware exists
335 if(!firmwareOrigFI.exists())
337 m_dp->addItem(tr("Could not find the Original Firmware at: %1")
338 .arg(firmwareOrig),LOGERROR);
339 emit done(true);
340 return;
343 QFile firmwareFile(firmware);
344 QFile firmwareOrigFile(firmwareOrig);
346 //remove modified firmware
347 if(!firmwareFile.remove())
349 m_dp->addItem(tr("Could not remove the Firmware at: %1")
350 .arg(firmware),LOGERROR);
351 emit done(true);
352 return;
355 //copy original firmware
356 if(!firmwareOrigFile.copy(firmware))
358 m_dp->addItem(tr("Could not copy the Firmware from: %1 to %2")
359 .arg(firmwareOrig,firmware),LOGERROR);
360 emit done(true);
361 return;
364 removeInstallLog();
366 emit done(false); //success
371 void BootloaderInstaller::gigabeatFinish()
373 // this step is only need for installation, so no code for uninstall here
375 m_dp->addItem(tr("Finishing bootloader install"),LOGINFO);
377 QString firmware = m_mountpoint + "/GBSYSTEM/FWIMG/" + m_bootloadername;
379 QFileInfo firmwareFI(firmware);
381 // check if firmware exists
382 if(!firmwareFI.exists())
384 m_dp->addItem(tr("Could not find the Firmware at: %1")
385 .arg(firmware),LOGERROR);
386 emit done(true);
387 return;
390 QString firmwareOrig = firmware;
391 firmwareOrig.append(".ORIG");
392 QFileInfo firmwareOrigFI(firmwareOrig);
394 // rename and backup the firmware, if there is no original firmware there
395 if(!firmwareOrigFI.exists())
397 QFile firmwareFile(firmware);
398 //backup
399 QDir::home().mkdir("Gigabeat Original Firmware Backup");
400 firmwareFile.copy(QDir::toNativeSeparators(QDir::homePath()) + QDir::toNativeSeparators("/Gigabeat Original Firmware Backup/") + m_bootloadername);
402 //rename
403 if(!firmwareFile.rename(firmwareOrig))
405 m_dp->addItem(tr("Could not rename: %1 to %2")
406 .arg(firmware,firmwareOrig),LOGERROR);
407 emit done(true);
408 return;
411 else // or remove the normal firmware, if the original is there
413 QFile firmwareFile(firmware);
414 firmwareFile.remove();
417 //copy the firmware
418 if(!downloadFile.copy(firmware))
420 m_dp->addItem(tr("Could not copy: %1 to %2")
421 .arg(m_tempfilename,firmware),LOGERROR);
422 emit done(true);
423 return;
426 downloadFile.remove();
428 createInstallLog();
430 m_dp->addItem(tr("Bootloader install finished successfully."),LOGOK);
431 m_dp->addItem(tr("To finish the Bootloader installation, follow the steps below."),LOGINFO);
432 m_dp->addItem(tr("1. Eject/Unmount your Device."),LOGINFO);
433 m_dp->addItem(tr("2. Unplug USB and any Power adapters."),LOGINFO);
434 m_dp->addItem(tr("3. Hold POWER to turn the Device off."),LOGINFO);
435 m_dp->addItem(tr("4. Toggle the Battery switch on the Device."),LOGINFO);
436 m_dp->addItem(tr("5. Hold POWER to boot the Rockbox bootloader."),LOGINFO);
439 m_dp->abort();
441 emit done(false); // success
445 /**************************************************
446 *** iaudio secific code
447 ***************************************************/
448 void BootloaderInstaller::iaudioPrepare()
451 QString url = m_bootloaderUrlBase + "/iaudio/" + m_bootloadername;
453 m_dp->addItem(tr("Downloading file %1.%2")
454 .arg(QFileInfo(url).baseName(), QFileInfo(url).completeSuffix()),LOGINFO);
456 // temporary file needs to be opened to get the filename
457 downloadFile.open();
458 m_tempfilename = downloadFile.fileName();
459 downloadFile.close();
460 // get the real file.
461 getter = new HttpGet(this);
462 getter->setFile(&downloadFile);
463 getter->getFile(QUrl(url));
464 // connect signals from HttpGet
465 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
466 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
467 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
470 void BootloaderInstaller::iaudioFinish()
472 QString firmware = m_mountpoint + "/FIRMWARE/" + m_bootloadername;
474 //copy the firmware
475 if(!downloadFile.copy(firmware))
477 m_dp->addItem(tr("Could not copy: %1 to %2")
478 .arg(m_tempfilename,firmware),LOGERROR);
479 emit done(true);
480 return;
483 downloadFile.remove();
485 createInstallLog();
487 m_dp->addItem(tr("Bootloader install finished successfully."),LOGOK);
488 m_dp->addItem(tr("To finish the Bootloader installation, follow the steps below."),LOGINFO);
489 m_dp->addItem(tr("1. Eject/Unmount your Device."),LOGINFO);
490 m_dp->addItem(tr("2. Turn you Device OFF."),LOGINFO);
491 m_dp->addItem(tr("3. Insert Charger."),LOGINFO);
493 m_dp->abort();
495 emit done(false); // success
500 /**************************************************
501 *** h10 secific code
502 ***************************************************/
503 void BootloaderInstaller::h10Prepare()
505 if(m_install) // Installation
507 QString url = m_bootloaderUrlBase + "/iriver/" + m_bootloadername;
509 m_dp->addItem(tr("Downloading file %1.%2")
510 .arg(QFileInfo(url).baseName(), QFileInfo(url).completeSuffix()),LOGINFO);
512 // temporary file needs to be opened to get the filename
513 downloadFile.open();
514 m_tempfilename = downloadFile.fileName();
515 downloadFile.close();
516 // get the real file.
517 getter = new HttpGet(this);
518 getter->setFile(&downloadFile);
519 getter->getFile(QUrl(url));
520 // connect signals from HttpGet
521 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
522 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
523 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
525 else // Uninstallation
528 QString firmwarename = m_bootloadername.section('/', -1);
530 QString firmware = m_mountpoint + "/SYSTEM/" + firmwarename;
531 QString firmwareOrig = m_mountpoint + "/SYSTEM/OF.mi4";
533 QFileInfo firmwareFI(firmware);
534 if(!firmwareFI.exists()) //Firmware dosent exists on player
536 firmware = m_mountpoint + "/SYSTEM/H10EMP.mi4"; //attempt other firmwarename
537 firmwareFI.setFile(firmware);
538 if(!firmwareFI.exists()) //Firmware dosent exists on player
540 m_dp->addItem(tr("Firmware does not exist: %1")
541 .arg(firmware),LOGERROR);
542 emit done(true);
543 return;
547 QFileInfo firmwareOrigFI(firmwareOrig);
548 if(!firmwareOrigFI.exists()) //Original Firmware dosent exists on player
550 m_dp->addItem(tr("Original Firmware does not exist: %1")
551 .arg(firmwareOrig),LOGERROR);
552 emit done(true);
553 return;
556 QFile firmwareFile(firmware);
557 QFile firmwareOrigFile(firmwareOrig);
559 //remove modified firmware
560 if(!firmwareFile.remove())
562 m_dp->addItem(tr("Could not remove the Firmware at: %1")
563 .arg(firmware),LOGERROR);
564 emit done(true);
565 return;
568 //copy original firmware
569 if(!firmwareOrigFile.copy(firmware))
571 m_dp->addItem(tr("Could not copy the Firmware from: %1 to %2")
572 .arg(firmwareOrig,firmware),LOGERROR);
573 emit done(true);
574 return;
577 removeInstallLog();
579 emit done(false); //success
584 void BootloaderInstaller::h10Finish()
586 QString firmwarename = m_bootloadername.section('/', -1);
588 QString firmware = m_mountpoint + "/SYSTEM/" + firmwarename;
589 QString firmwareOrig = m_mountpoint + "/SYSTEM/OF.mi4";
591 QFileInfo firmwareFI(firmware);
593 if(!firmwareFI.exists()) //Firmware dosent exists on player
595 firmware = m_mountpoint + "/SYSTEM/H10EMP.mi4"; //attempt other firmwarename
596 firmwareFI.setFile(firmware);
597 if(!firmwareFI.exists()) //Firmware dosent exists on player
599 m_dp->addItem(tr("Firmware does not exist: %1")
600 .arg(firmware),LOGERROR);
601 emit done(true);
602 return;
606 QFileInfo firmwareOrigFI(firmwareOrig);
608 if(!firmwareOrigFI.exists())
610 QFile firmwareFile(firmware);
612 //backup
613 QDir::home().mkdir("Iriver H10 Original Firmware Backup");
614 firmwareFile.copy(QDir::toNativeSeparators(QDir::homePath()) + QDir::toNativeSeparators("/Iriver H10 Original Firmware Backup/") + m_bootloadername);
616 //rename
617 if(!firmwareFile.rename(firmwareOrig)) //rename Firmware to Original
619 m_dp->addItem(tr("Could not rename: %1 to %2")
620 .arg(firmware,firmwareOrig),LOGERROR);
621 emit done(true);
622 return;
625 else //there is already a original firmware
627 QFile firmwareFile(firmware);
628 firmwareFile.remove();
630 //copy the firmware
631 if(!downloadFile.copy(firmware))
633 m_dp->addItem(tr("Could not copy: %1 to %2")
634 .arg(m_tempfilename,firmware),LOGERROR);
635 emit done(true);
636 return;
639 downloadFile.remove();
641 createInstallLog();
643 m_dp->addItem(tr("Bootloader install finished successfully."),LOGOK);
644 m_dp->abort();
646 emit done(false); // success
652 /**************************************************
653 *** mrobe100 secific code
654 ***************************************************/
655 void BootloaderInstaller::mrobe100Prepare()
657 if(m_install) // Installation
659 QString url = m_bootloaderUrlBase + "/olympus/mrobe100/" + m_bootloadername;
661 m_dp->addItem(tr("Downloading file %1.%2")
662 .arg(QFileInfo(url).baseName(), QFileInfo(url).completeSuffix()),LOGINFO);
664 // temporary file needs to be opened to get the filename
665 downloadFile.open();
666 m_tempfilename = downloadFile.fileName();
667 downloadFile.close();
668 // get the real file.
669 getter = new HttpGet(this);
670 getter->setFile(&downloadFile);
671 getter->getFile(QUrl(url));
672 // connect signals from HttpGet
673 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
674 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
675 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
677 else // Uninstallation
680 QString firmwarename = m_bootloadername;
682 QString firmware = m_mountpoint + "/SYSTEM/" + firmwarename;
683 QString firmwareOrig = m_mountpoint + "/SYSTEM/OF.mi4";
685 QFileInfo firmwareFI(firmware);
686 if(!firmwareFI.exists()) //Firmware dosent exists on player
688 m_dp->addItem(tr("Firmware does not exist: %1")
689 .arg(firmware),LOGERROR);
690 emit done(true);
691 return;
694 QFileInfo firmwareOrigFI(firmwareOrig);
695 if(!firmwareOrigFI.exists()) //Original Firmware dosent exists on player
697 m_dp->addItem(tr("Original Firmware does not exist: %1")
698 .arg(firmwareOrig),LOGERROR);
699 emit done(true);
700 return;
703 QFile firmwareFile(firmware);
704 QFile firmwareOrigFile(firmwareOrig);
706 //remove modified firmware
707 if(!firmwareFile.remove())
709 m_dp->addItem(tr("Could not remove the Firmware at: %1")
710 .arg(firmware),LOGERROR);
711 emit done(true);
712 return;
715 //copy original firmware
716 if(!firmwareOrigFile.copy(firmware))
718 m_dp->addItem(tr("Could not copy the Firmware from: %1 to %2")
719 .arg(firmwareOrig,firmware),LOGERROR);
720 emit done(true);
721 return;
724 removeInstallLog();
726 emit done(false); //success
731 void BootloaderInstaller::mrobe100Finish()
733 QString firmwarename = m_bootloadername;
735 QString firmware = m_mountpoint + "/SYSTEM/" + firmwarename;
736 QString firmwareOrig = m_mountpoint + "/SYSTEM/OF.mi4";
738 QFileInfo firmwareFI(firmware);
740 if(!firmwareFI.exists()) //Firmware dosent exists on player
742 m_dp->addItem(tr("Firmware does not exist: %1")
743 .arg(firmware),LOGERROR);
744 emit done(true);
745 return;
748 QFileInfo firmwareOrigFI(firmwareOrig);
750 if(!firmwareOrigFI.exists())
752 QFile firmwareFile(firmware);
754 //backup
755 QDir::home().mkdir("Olympus mrobe100 Original Firmware Backup");
756 firmwareFile.copy(QDir::toNativeSeparators(QDir::homePath()) + QDir::toNativeSeparators("/Olympus mrobe100 Original Firmware Backup/") + m_bootloadername);
758 //rename
759 if(!firmwareFile.rename(firmwareOrig)) //rename Firmware to Original
761 m_dp->addItem(tr("Could not rename: %1 to %2")
762 .arg(firmware,firmwareOrig),LOGERROR);
763 emit done(true);
764 return;
767 else //there is already a original firmware
769 QFile firmwareFile(firmware);
770 firmwareFile.remove();
772 //copy the firmware
773 if(!downloadFile.copy(firmware))
775 m_dp->addItem(tr("Could not copy: %1 to %2")
776 .arg(m_tempfilename,firmware),LOGERROR);
777 emit done(true);
778 return;
781 downloadFile.remove();
783 createInstallLog();
785 m_dp->addItem(tr("Bootloader install finished successfully."),LOGOK);
786 m_dp->abort();
788 emit done(false); // success
792 /**************************************************
793 *** ipod secific code
794 ***************************************************/
795 int verbose =0;
796 // reserves memory for ipodpatcher
797 bool initIpodpatcher()
799 if (ipod_alloc_buffer(&sectorbuf,BUFFER_SIZE) < 0) return true;
800 else return false;
803 void BootloaderInstaller::ipodPrepare()
805 m_dp->addItem(tr("Searching for ipods"),LOGINFO);
806 struct ipod_t ipod;
808 int n = ipod_scan(&ipod);
809 if (n == 0)
811 m_dp->addItem(tr("No Ipods found"),LOGERROR);
812 emit done(true);
813 return;
815 if (n > 1)
817 m_dp->addItem(tr("Too many Ipods found"),LOGERROR);
818 emit done(true);
821 if(m_install) // Installation
824 QString url = m_bootloaderUrlBase + "/ipod/bootloader-" + m_bootloadername + ".ipod";
826 m_dp->addItem(tr("Downloading file %1.%2")
827 .arg(QFileInfo(url).baseName(), QFileInfo(url).completeSuffix()),LOGINFO);
829 // temporary file needs to be opened to get the filename
830 downloadFile.open();
831 m_tempfilename = downloadFile.fileName();
832 downloadFile.close();
833 // get the real file.
834 getter = new HttpGet(this);
835 getter->setFile(&downloadFile);
836 getter->getFile(QUrl(url));
837 // connect signals from HttpGet
838 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
839 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
840 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
842 else // Uninstallation
844 if (ipod_open(&ipod, 0) < 0)
846 m_dp->addItem(tr("could not open ipod"),LOGERROR);
847 emit done(true);
848 return;
851 if (read_partinfo(&ipod,0) < 0)
853 m_dp->addItem(tr("could not read partitiontable"),LOGERROR);
854 emit done(true);
855 return;
858 if (ipod.pinfo[0].start==0)
860 m_dp->addItem(tr("No partition 0 on disk"),LOGERROR);
862 int i;
863 double sectors_per_MB = (1024.0*1024.0)/ipod.sector_size;
864 m_dp->addItem(tr("[INFO] Part Start Sector End Sector Size (MB) Type\n"),LOGINFO);
865 for ( i = 0; i < 4; i++ )
867 if (ipod.pinfo[i].start != 0)
869 m_dp->addItem(tr("[INFO] %1 %2 %3 %4 %5 (%6)").arg(
870 i).arg(
871 ipod.pinfo[i].start).arg(
872 ipod.pinfo[i].start+ipod.pinfo[i].size-1).arg(
873 ipod.pinfo[i].size/sectors_per_MB).arg(
874 get_parttype(ipod.pinfo[i].type)).arg(
875 ipod.pinfo[i].type),LOGINFO);
878 emit done(true);
879 return;
882 read_directory(&ipod);
884 if (ipod.nimages <= 0)
886 m_dp->addItem(tr("Failed to read firmware directory"),LOGERROR);
887 emit done(true);
888 return;
890 if (getmodel(&ipod,(ipod.ipod_directory[0].vers>>8)) < 0)
892 m_dp->addItem(tr("Unknown version number in firmware (%1)").arg(
893 ipod.ipod_directory[0].vers),LOGERROR);
894 emit done(true);
895 return;
898 if (ipod.macpod)
900 m_dp->addItem(tr("Warning this is a MacPod, Rockbox doesnt work on "
901 "this. Convert it to WinPod\n"
902 "See http://www.rockbox.org/wiki/IpodConversionToFAT32"),
903 LOGWARNING);
904 emit done(true);
905 return;
908 if (ipod_reopen_rw(&ipod) < 0)
910 m_dp->addItem(tr("Could not open Ipod in RW mode"),LOGERROR);
911 emit done(true);
912 return;
915 if (ipod.ipod_directory[0].entryOffset==0) {
916 m_dp->addItem(tr("No bootloader detected."),LOGERROR);
917 emit done(true);
918 return;
921 if (delete_bootloader(&ipod)==0)
923 m_dp->addItem(tr("Successfully removed Bootloader"),LOGOK);
924 removeInstallLog();
925 emit done(false);
926 ipod_close(&ipod);
927 return;
929 else
931 m_dp->addItem(tr("--delete-bootloader failed."),LOGERROR);
932 emit done(true);
933 ipod_close(&ipod);
934 return;
939 void BootloaderInstaller::ipodFinish()
941 struct ipod_t ipod;
942 ipod_scan(&ipod);
944 if (ipod_open(&ipod, 0) < 0)
946 m_dp->addItem(tr("could not open ipod"),LOGERROR);
947 emit done(true);
948 return;
951 if (read_partinfo(&ipod,0) < 0)
953 m_dp->addItem(tr("could not read partitiontable"),LOGERROR);
954 emit done(true);
955 return;
958 if (ipod.pinfo[0].start==0)
960 m_dp->addItem(tr("No partition 0 on disk"),LOGERROR);
962 int i;
963 double sectors_per_MB = (1024.0*1024.0)/ipod.sector_size;
965 m_dp->addItem(tr("[INFO] Part Start Sector End Sector Size (MB) Type\n"),LOGINFO);
967 for ( i = 0; i < 4; i++ )
969 if (ipod.pinfo[i].start != 0)
971 m_dp->addItem(tr("[INFO] %1 %2 %3 %4 %5 (%6)").arg(
972 i).arg(
973 ipod.pinfo[i].start).arg(
974 ipod.pinfo[i].start+ipod.pinfo[i].size-1).arg(
975 ipod.pinfo[i].size/sectors_per_MB).arg(
976 get_parttype(ipod.pinfo[i].type)).arg(
977 ipod.pinfo[i].type),LOGWARNING);
980 emit done(true);
981 return;
984 read_directory(&ipod);
986 if (ipod.nimages <= 0)
988 m_dp->addItem(tr("Failed to read firmware directory"),LOGERROR);
989 emit done(true);
990 return;
992 if (getmodel(&ipod,(ipod.ipod_directory[0].vers>>8)) < 0)
994 m_dp->addItem(tr("Unknown version number in firmware (%1)").arg(
995 ipod.ipod_directory[0].vers),LOGERROR);
996 emit done(true);
997 return;
1000 if (ipod.macpod)
1002 m_dp->addItem(tr("Warning this is a MacPod, Rockbox doesnt work on "
1003 "this. Convert it to WinPod\n"
1004 "See http://www.rockbox.org/wiki/IpodConversionToFAT32"),
1005 LOGWARNING);
1006 emit done(true);
1007 return;
1010 if (ipod_reopen_rw(&ipod) < 0)
1012 m_dp->addItem(tr("Could not open Ipod in RW mode"),LOGERROR);
1013 emit done(true);
1014 return;
1017 if (add_bootloader(&ipod, m_tempfilename.toLatin1().data(), FILETYPE_DOT_IPOD)==0)
1019 m_dp->addItem(tr("Successfully added Bootloader"),LOGOK);
1020 createInstallLog();
1021 emit done(false);
1022 ipod_close(&ipod);
1023 return;
1025 else
1027 m_dp->addItem(tr("failed to add Bootloader"),LOGERROR);
1028 ipod_close(&ipod);
1029 emit done(true);
1030 return;
1034 /**************************************************
1035 *** sansa secific code
1036 ***************************************************/
1037 // reserves memory for sansapatcher
1038 bool initSansapatcher()
1040 if (sansa_alloc_buffer(&sectorbuf,BUFFER_SIZE) < 0) return true;
1041 else return false;
1045 void BootloaderInstaller::sansaPrepare()
1047 m_dp->addItem(tr("Searching for sansas"),LOGINFO);
1048 struct sansa_t sansa;
1050 int n = sansa_scan(&sansa);
1051 if (n == 0)
1053 m_dp->addItem(tr("No Sansa found"),LOGERROR);
1054 emit done(true);
1055 return;
1057 if (n > 1)
1059 m_dp->addItem(tr("Too many Sansas found"),LOGERROR);
1060 emit done(true);
1063 if(m_install) // Installation
1065 QString url = m_bootloaderUrlBase + "/sandisk-sansa/"
1066 + QString(sansa.targetname) + "/" + m_bootloadername;
1068 m_dp->addItem(tr("Downloading file %1.%2")
1069 .arg(QFileInfo(url).baseName(), QFileInfo(url).completeSuffix()),LOGINFO);
1071 // temporary file needs to be opened to get the filename
1072 downloadFile.open();
1073 m_tempfilename = downloadFile.fileName();
1074 downloadFile.close();
1075 // get the real file.
1076 getter = new HttpGet(this);
1077 getter->setFile(&downloadFile);
1078 getter->getFile(QUrl(url));
1079 // connect signals from HttpGet
1080 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
1081 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
1082 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
1084 else // Uninstallation
1087 if (sansa_open(&sansa, 0) < 0)
1089 m_dp->addItem(tr("could not open Sansa"),LOGERROR);
1090 emit done(true);
1091 return;
1094 if (sansa_read_partinfo(&sansa,0) < 0)
1096 m_dp->addItem(tr("could not read partitiontable"),LOGERROR);
1097 emit done(true);
1098 return;
1101 int i = is_sansa(&sansa);
1102 if (i < 0) {
1103 m_dp->addItem(tr("Disk is not a Sansa (%1), aborting.").arg(i),LOGERROR);
1104 emit done(true);
1105 return;
1108 if (sansa.hasoldbootloader)
1110 m_dp->addItem(tr("********************************************\n"
1111 "OLD ROCKBOX INSTALLATION DETECTED, ABORTING.\n"
1112 "You must reinstall the original Sansa firmware before running\n"
1113 "sansapatcher for the first time.\n"
1114 "See http://www.rockbox.org/twiki/bin/view/Main/SansaE200Install\n"
1115 "*********************************************\n"),LOGERROR);
1116 emit done(true);
1117 return;
1121 if (sansa_reopen_rw(&sansa) < 0)
1123 m_dp->addItem(tr("Could not open Sansa in RW mode"),LOGERROR);
1124 emit done(true);
1125 return;
1128 if (sansa_delete_bootloader(&sansa)==0)
1130 m_dp->addItem(tr("Successfully removed Bootloader"),LOGOK);
1131 removeInstallLog();
1132 emit done(false);
1133 sansa_close(&sansa);
1134 return;
1136 else
1138 m_dp->addItem(tr("--delete-bootloader failed."),LOGERROR);
1139 emit done(true);
1140 sansa_close(&sansa);
1141 return;
1146 void BootloaderInstaller::sansaFinish()
1148 struct sansa_t sansa;
1149 sansa_scan(&sansa);
1151 if (sansa_open(&sansa, 0) < 0)
1153 m_dp->addItem(tr("could not open Sansa"),LOGERROR);
1154 emit done(true);
1155 return;
1158 if (sansa_read_partinfo(&sansa,0) < 0)
1160 m_dp->addItem(tr("could not read partitiontable"),LOGERROR);
1161 emit done(true);
1162 return;
1166 int i = is_sansa(&sansa);
1167 if (i < 0) {
1169 m_dp->addItem(tr("Disk is not a Sansa (%1), aborting.").arg(i),LOGERROR);
1170 emit done(true);
1171 return;
1174 if (sansa.hasoldbootloader)
1176 m_dp->addItem(tr("********************************************\n"
1177 "OLD ROCKBOX INSTALLATION DETECTED, ABORTING.\n"
1178 "You must reinstall the original Sansa firmware before running\n"
1179 "sansapatcher for the first time.\n"
1180 "See http://www.rockbox.org/twiki/bin/view/Main/SansaE200Install\n"
1181 "*********************************************\n"),LOGERROR);
1182 emit done(true);
1183 return;
1186 if (sansa_reopen_rw(&sansa) < 0)
1188 m_dp->addItem(tr("Could not open Sansa in RW mode"),LOGERROR);
1189 emit done(true);
1190 return;
1193 if (sansa_add_bootloader(&sansa, m_tempfilename.toLatin1().data(), FILETYPE_MI4)==0)
1195 m_dp->addItem(tr("Successfully added Bootloader"),LOGOK);
1196 createInstallLog();
1197 emit done(false);
1198 sansa_close(&sansa);
1199 return;
1201 else
1203 m_dp->addItem(tr("failed to add Bootloader"),LOGERROR);
1204 sansa_close(&sansa);
1205 emit done(true);
1206 return;
1211 /**************************************************
1212 *** iriver /fwpatcher secific code
1213 ***************************************************/
1215 void BootloaderInstaller::iriverPrepare()
1217 char md5sum_str[32];
1218 if (!FileMD5(m_origfirmware, md5sum_str)) {
1219 m_dp->addItem(tr("Could not MD5Sum original firmware"),LOGERROR);
1220 emit done(true);
1221 return;
1224 /* Check firmware against md5sums in h120sums and h100sums */
1225 series = 0;
1226 table_entry = intable(md5sum_str, &h120pairs[0],
1227 sizeof(h120pairs)/sizeof(struct sumpairs));
1228 if (table_entry >= 0) {
1229 series = 120;
1231 else
1233 table_entry = intable(md5sum_str, &h100pairs[0],
1234 sizeof(h100pairs)/sizeof(struct sumpairs));
1235 if (table_entry >= 0)
1237 series = 100;
1239 else
1241 table_entry = intable(md5sum_str, &h300pairs[0],
1242 sizeof(h300pairs)/sizeof(struct sumpairs));
1243 if (table_entry >= 0)
1244 series = 300;
1247 if (series == 0)
1249 m_dp->addItem(tr("Could not detect firmware type"),LOGERROR);
1250 emit done(true);
1251 return;
1254 QString url = m_bootloaderUrlBase + "/iriver/" + m_bootloadername;
1256 m_dp->addItem(tr("Downloading file %1.%2")
1257 .arg(QFileInfo(url).baseName(), QFileInfo(url).completeSuffix()),LOGINFO);
1259 // temporary file needs to be opened to get the filename
1260 downloadFile.open();
1261 m_tempfilename = downloadFile.fileName();
1262 downloadFile.close();
1263 // get the real file.
1264 getter = new HttpGet(this);
1265 getter->setFile(&downloadFile);
1266 getter->getFile(QUrl(url));
1267 // connect signals from HttpGet
1268 connect(getter, SIGNAL(done(bool)), this, SLOT(downloadDone(bool)));
1269 connect(getter, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
1270 connect(m_dp, SIGNAL(aborted()), getter, SLOT(abort()));
1273 void BootloaderInstaller::iriverFinish()
1275 // Patch firmware
1276 char md5sum_str[32];
1277 struct sumpairs *sums = 0;
1278 int origin = 0;
1280 /* get pointer to the correct bootloader.bin */
1281 switch(series) {
1282 case 100:
1283 sums = &h100pairs[0];
1284 origin = 0x1f0000;
1285 break;
1286 case 120:
1287 sums = &h120pairs[0];
1288 origin = 0x1f0000;
1289 break;
1290 case 300:
1291 sums = &h300pairs[0];
1292 origin = 0x3f0000;
1293 break;
1296 // temporary files needs to be opened to get the filename
1297 QTemporaryFile firmwareBin, newBin, newHex;
1298 firmwareBin.open();
1299 newBin.open();
1300 newHex.open();
1301 QString firmwareBinName = firmwareBin.fileName();
1302 QString newBinName = newBin.fileName();
1303 QString newHexName = newHex.fileName();
1304 firmwareBin.close();
1305 newBin.close();
1306 newHex.close();
1308 // iriver decode
1309 if (iriver_decode(m_origfirmware, firmwareBinName, FALSE, STRIP_NONE,m_dp) == -1)
1311 m_dp->addItem(tr("Error in descramble"),LOGERROR);
1312 firmwareBin.remove();
1313 newBin.remove();
1314 newHex.remove();
1315 emit done(true);
1316 return;
1318 // mkboot
1319 if (!mkboot(firmwareBinName, newBinName, m_tempfilename, origin,m_dp))
1321 m_dp->addItem(tr("Error in patching"),LOGERROR);
1322 firmwareBin.remove();
1323 newBin.remove();
1324 newHex.remove();
1325 emit done(true);
1326 return;
1328 // iriver_encode
1329 if (iriver_encode(newBinName, newHexName, FALSE,m_dp) == -1)
1331 m_dp->addItem(tr("Error in scramble"),LOGERROR);
1332 firmwareBin.remove();
1333 newBin.remove();
1334 newHex.remove();
1335 emit done(true);
1336 return;
1339 /* now md5sum it */
1340 if (!FileMD5(newHexName, md5sum_str))
1342 m_dp->addItem(tr("Error in checksumming"),LOGERROR);
1343 firmwareBin.remove();
1344 newBin.remove();
1345 newHex.remove();
1346 emit done(true);
1347 return;
1349 if (strncmp(sums[table_entry].patched, md5sum_str, 32) == 0) {
1350 /* delete temp files */
1351 firmwareBin.remove();
1352 newBin.remove();
1355 // Load patched Firmware to player
1356 QString dest;
1357 if(series == 100)
1358 dest = m_mountpoint + "/ihp_100.hex";
1359 else if(series == 120)
1360 dest = m_mountpoint + "/ihp_120.hex";
1361 else if(series == 300)
1362 dest = m_mountpoint + "/H300.hex";
1364 // copy file
1365 QFile destfile(dest);
1366 if(destfile.exists()) destfile.remove();
1367 if(!newHex.copy(dest))
1369 m_dp->addItem(tr("Could not copy: %1 to %2")
1370 .arg(newHexName,dest),LOGERROR);
1371 emit done(true);
1372 return;
1375 downloadFile.remove();
1376 newHex.remove();
1378 createInstallLog();
1380 m_dp->addItem(tr("Bootloader install finished successfully."),LOGOK);
1381 m_dp->addItem(tr("To finish the Bootloader installation, follow the steps below."),LOGINFO);
1382 m_dp->addItem(tr("1. Eject/Unmount your Device."),LOGINFO);
1383 m_dp->addItem(tr("2. Boot into the original Firmware."),LOGINFO);
1384 m_dp->addItem(tr("3. Use the Firmware flash option in the Original Firmware."),LOGINFO);
1385 m_dp->addItem(tr("4. Reboot."),LOGINFO);
1386 m_dp->abort();
1388 emit done(false); // success