Limit minimum db scale value when switching from linear to logaritmic peakmeter scale...
[Rockbox.git] / rbutil / bootloaders.cpp
blob56f936e4fff2232e0901bd8a3da51e022b1c68ad
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * Module: rbutil
9 * File: bootloaders.cpp
11 * Copyright (C) 2007 Dominik Wenger
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
21 #include "bootloaders.h"
22 #include "irivertools.h"
23 #include "md5sum.h"
25 #include "rbutil.h"
26 #include "installlog.h"
29 int verbose =0;
30 // reserves memory for ipodpatcher
31 bool initIpodpatcher()
33 if (ipod_alloc_buffer(&sectorbuf,BUFFER_SIZE) < 0) return true;
34 else return false;
36 // uses ipodpatcher for add and rem of bootloader
37 bool ipodpatcher(int mode,wxString bootloadername)
39 wxString src,dest,buf;
41 struct ipod_t ipod;
43 int n = ipod_scan(&ipod);
44 if (n == 0)
46 ERR_DIALOG(wxT("[ERR] No ipods found."), wxT("Scanning for Ipods"));
47 return false;
49 if (n > 1)
51 ERR_DIALOG(wxT("[ERR] to many ipods found."), wxT("Scanning for Ipods"));
52 return false;
55 // downloading files
56 if(mode == BOOTLOADER_ADD)
58 src = gv->bootloader_url + wxT("/ipod/")
59 + bootloadername + wxT(".ipod");
60 dest = gv->stdpaths->GetUserDataDir()
61 + wxT("" PATH_SEP "download" PATH_SEP) + bootloadername;
62 if ( DownloadURL(src, dest) )
64 wxRemoveFile(dest);
65 ERR_DIALOG(wxT("Unable to download ") + src, wxT("Install"));
66 return false;
70 if (ipod_open(&ipod, 0) < 0)
72 ERR_DIALOG(wxT("[ERR] could not open ipod"), wxT("open Ipod"));
73 return false;
76 if (read_partinfo(&ipod,0) < 0)
78 ERR_DIALOG(wxT("[ERR] could not read partitiontable"), wxT("reading partitiontable"));
79 return false;
82 if (ipod.pinfo[0].start==0)
84 ERR_DIALOG(wxT("[ERR] No partition 0 on disk"), wxT("reading partitiontable"));
85 int i;
86 double sectors_per_MB = (1024.0*1024.0)/ipod.sector_size;
88 buf.Printf(wxT("[INFO] Part Start Sector End Sector Size (MB) Type\n"));
89 ERR_DIALOG(buf, wxT("reading partitiontable"));
90 for ( i = 0; i < 4; i++ ) {
91 if (ipod.pinfo[i].start != 0) {
92 buf.Printf(wxT("[INFO] %d %10ld %10ld %10.1f %s (0x%02x)\n"),
94 ipod.pinfo[i].start,
95 ipod.pinfo[i].start+ipod.pinfo[i].size-1,
96 ipod.pinfo[i].size/sectors_per_MB,
97 get_parttype(ipod.pinfo[i].type),
98 ipod.pinfo[i].type);
99 ERR_DIALOG(buf, wxT("reading partitiontable"));
102 return false;
105 read_directory(&ipod);
107 if (ipod.nimages <= 0)
109 ERR_DIALOG(wxT("[ERR] Failed to read firmware directory"), wxT("reading directory"));
110 return false;
112 if (getmodel(&ipod,(ipod.ipod_directory[0].vers>>8)) < 0)
114 buf.Printf(wxT("[ERR] Unknown version number in firmware (%08x)\n"),
115 ipod.ipod_directory[0].vers );
116 ERR_DIALOG(buf, wxT("reading directory"));
117 return false;
120 if (ipod.macpod)
122 WARN_DIALOG(wxT("Warning this is a MacPod, Rockbox doesnt work on this. Convert it to WinPod"),wxT("MacPod"));
126 if(mode == BOOTLOADER_ADD)
128 if (ipod_reopen_rw(&ipod) < 0) {
129 ERR_DIALOG(wxT("[ERR] Could not open Ipod in RW mode"), wxT("Bootloader add"));
130 return false;
133 if (add_bootloader(&ipod, (char*)dest.c_str(), FILETYPE_DOT_IPOD)==0) {
135 } else {
136 ERR_DIALOG(wxT("[ERR] failed to add Bootloader"), wxT("Bootloader add"));
137 return false;
140 else if(mode == BOOTLOADER_REM)
142 if (ipod_reopen_rw(&ipod) < 0) {
143 ERR_DIALOG(wxT("[ERR] Could not open Ipod in RW mode"), wxT("Bootloader add"));
144 return false;
147 if (ipod.ipod_directory[0].entryOffset==0) {
148 ERR_DIALOG(wxT("[ERR] No bootloader detected.\n"), wxT("Bootloader del"));
149 return false;
150 } else {
151 if (delete_bootloader(&ipod)==0) {
153 } else {
154 ERR_DIALOG(wxT("[ERR] --delete-bootloader failed.\n"), wxT("Bootloader del"));
155 return false;
160 ipod_close(&ipod);
161 return true;
164 // reserves memory for sansapatcher
165 bool initSansaPatcher()
167 if (sansa_alloc_buffer(&sectorbuf,BUFFER_SIZE) < 0) return true;
168 else return false;
172 // sansainstallation
173 bool sansapatcher(int mode,wxString bootloadername)
175 wxString src,dest,buf;
177 struct sansa_t sansa;
179 int n = sansa_scan(&sansa);
180 if (n == 0)
182 ERR_DIALOG(wxT("[ERR] No Sansa found."), wxT("Scanning for Sansa"));
183 return false;
185 if (n > 1)
187 ERR_DIALOG(wxT("[ERR] to many Sansa found."), wxT("Scanning for Sansa"));
188 return false;
191 // downloading files
192 if(mode == BOOTLOADER_ADD)
194 src = gv->bootloader_url + wxT("/sandisk-sansa/e200/")
195 + bootloadername;
196 dest = gv->stdpaths->GetUserDataDir()
197 + wxT("" PATH_SEP "download" PATH_SEP) + bootloadername;
198 if ( DownloadURL(src, dest) )
200 wxRemoveFile(dest);
201 ERR_DIALOG(wxT("Unable to download ") + src, wxT("Download"));
202 return false;
206 if (sansa_open(&sansa, 0) < 0)
208 ERR_DIALOG(wxT("[ERR] could not open sansa"), wxT("open Sansa"));
209 return false;
212 if (sansa_read_partinfo(&sansa,0) < 0)
214 ERR_DIALOG(wxT("[ERR] could not read partitiontable"), wxT("reading partitiontable"));
215 return false;
218 int i = is_e200(&sansa);
219 if (i < 0) {
220 ERR_DIALOG(wxT("[ERR] Disk is not an E200 (%d), aborting.\n"), wxT("Checking Disk"));
221 return false;
224 if (sansa.hasoldbootloader)
226 ERR_DIALOG(wxT("[ERR] ************************************************************************\n"
227 "[ERR] *** OLD ROCKBOX INSTALLATION DETECTED, ABORTING.\n"
228 "[ERR] *** You must reinstall the original Sansa firmware before running\n"
229 "[ERR] *** sansapatcher for the first time.\n"
230 "[ERR] *** See http://www.rockbox.org/twiki/bin/view/Main/SansaE200Install\n"
231 "[ERR] ************************************************************************\n"),wxT("Checking Disk"));
232 return false;
235 if(mode == BOOTLOADER_ADD)
237 if (sansa_reopen_rw(&sansa) < 0) {
238 ERR_DIALOG(wxT("[ERR] Could not open Sansa in RW mode"), wxT("Bootloader add"));
239 return false;
242 if (sansa_add_bootloader(&sansa, (char*)dest.c_str(), FILETYPE_MI4)==0) {
244 } else {
245 ERR_DIALOG(wxT("[ERR] failed to add Bootloader"), wxT("Bootloader add"));
249 else if(mode == BOOTLOADER_REM)
251 if (sansa_reopen_rw(&sansa) < 0) {
252 ERR_DIALOG(wxT("[ERR] Could not open Sansa in RW mode"), wxT("Bootloader Remove"));
255 if (sansa_delete_bootloader(&sansa)==0) {
257 } else {
258 ERR_DIALOG(wxT("[ERR] failed to remove Bootloader"), wxT("Bootloader remove"));
262 sansa_close(&sansa);
263 return true;
266 // gigabeatinstallation
267 bool gigabeatf(int mode,wxString bootloadername,wxString deviceDir)
269 wxString path1,path2;
270 wxString err;
271 wxString src,dest;
273 path1 = deviceDir
274 + wxT("" PATH_SEP "GBSYSTEM" PATH_SEP "FWIMG" PATH_SEP "FWIMG01.DAT");
276 if(mode == BOOTLOADER_ADD)
278 //Files downloaden
279 src = gv->bootloader_url + wxT("/gigabeat/") + bootloadername;
280 dest = gv->stdpaths->GetUserDataDir()
281 + wxT("" PATH_SEP "download" PATH_SEP) + bootloadername;
282 if( DownloadURL(src, dest) )
284 wxRemoveFile(dest);
285 ERR_DIALOG(wxT("Unable to download ") + src, wxT("Install"));
286 return false;
290 if(!wxFileExists(path1))
292 ERR_DIALOG(wxT("[ERR] Coud not find ")+path1, wxT("Bootloader add"));
293 return false;
295 path2 = path1;
296 path2.Append(wxT(".ORIG"));
297 if(!wxFileExists(path2))
299 if(!wxRenameFile(path1,path2,false))
301 ERR_DIALOG(wxT("[ERR] Coud not rename ") + path1 + wxT(" to ")
302 + path2, wxT("Bootloader add"));
303 return false;
308 if(!wxCopyFile(dest,path1))
310 ERR_DIALOG(wxT("[ERR] Coud not copy ") + dest + wxT(" to ")
311 + path2, wxT("Bootloader add"));
312 return false;
315 else if(mode == BOOTLOADER_REM)
317 path2 = path1;
318 path2.Append(wxT(".ORIG"));
319 if(!wxFileExists(path2))
321 ERR_DIALOG(wxT("[ERR] Coud not find ") + path1,
322 wxT("Bootloader del"));
323 return false;
325 if(!wxRenameFile(path2,path1,true))
327 ERR_DIALOG(wxT("[ERR] Coud not rename ") + path1 + wxT(" to ")
328 + path2, wxT("Bootloader del"));
329 return false;
332 return true;
335 // iaudio bootloader install
336 bool iaudiox5(int mode,wxString bootloadername,wxString deviceDir)
338 wxString path1,path2;
339 wxString err;
340 wxString src,dest;
342 path1 = deviceDir + wxT("" PATH_SEP "FIRMWARE" PATH_SEP)
343 + bootloadername;
345 if(mode == BOOTLOADER_ADD)
347 //Files downloaden
348 src = gv->bootloader_url + wxT("/iaudio/") + bootloadername;
349 dest = gv->stdpaths->GetUserDataDir()
350 + wxT("" PATH_SEP "download" PATH_SEP) + bootloadername;
351 if( DownloadURL(src, dest) )
353 wxRemoveFile(dest);
354 ERR_DIALOG(wxT("Unable to download ") + src, wxT("Install"));
355 return false;
358 // copy file
359 if(!wxCopyFile(dest,path1))
361 ERR_DIALOG(wxT("[ERR] Coud not copy ")+dest+wxT(" to ")+path2,
362 wxT("Bootloader add"));
363 return false;
366 return true; // install ready
368 else
369 return false; //no uninstallation possible
372 // H10 install
373 bool h10(int mode,wxString bootloadername,wxString deviceDir)
375 wxString err,src,dest,path1,path2;
377 int pos = bootloadername.Find('/');
378 if(pos == wxNOT_FOUND) pos = 0;
379 wxString firmwarename = bootloadername.SubString(pos,bootloadername.Length());
380 //wxString firmDir = gv->curbootloader.SubString(0,pos);
382 if(mode == BOOTLOADER_ADD)
384 //Files downloaden
385 src = gv->bootloader_url + wxT("/iriver/") + bootloadername;
386 dest = gv->stdpaths->GetUserDataDir()
387 + wxT("" PATH_SEP "download" PATH_SEP) + firmwarename;
388 if( DownloadURL(src, dest) )
390 wxRemoveFile(dest);
391 ERR_DIALOG(wxT("Unable to download ") + src, wxT("Install"));
392 return false;
395 path1 = deviceDir + wxT("SYSTEM" PATH_SEP) + firmwarename;
396 path2 = deviceDir + wxT("SYSTEM" PATH_SEP "Original.mi4");
398 if(!wxFileExists(path1)) //Firmware dosent exists on player
400 path1 = deviceDir + wxT("SYSTEM" PATH_SEP "H10EMP.mi4"); //attempt other firmwarename
401 if(!wxFileExists(path1)) //Firmware dosent exists on player
403 ERR_DIALOG(wxT("[ERR] File ") + path1 + wxT(" does not Exist"),
404 wxT("Bootloader add"));
405 return false;
408 if(!wxFileExists(path2)) //there is already a original firmware
410 if(!wxRenameFile(path1,path2,false)) //rename Firmware to Original
412 ERR_DIALOG(wxT("[ERR] Coud not rename ") + path1 + wxT(" to ")
413 + path2, wxT("Bootloader add"));
414 return false;
418 if(!wxCopyFile(dest,path1)) // copy file
420 ERR_DIALOG(wxT("[ERR] Coud not copy ") + dest + wxT(" to ") + path1,
421 wxT("Bootloader add"));
422 return false;
425 return true; //install ready
428 else if(mode == BOOTLOADER_REM)
430 path1 = deviceDir + wxT("SYSTEM" PATH_SEP) + firmwarename;
431 path2 = gv->curdestdir + wxT("SYSTEM" PATH_SEP "Original.mi4");
432 if(!wxFileExists(path1)) //Firmware dosent exists on player
434 path1 = deviceDir + wxT("" PATH_SEP "SYSTEM" PATH_SEP "H10EMP.mi4"); //attempt other firmwarename
435 if(!wxFileExists(path1)) //Firmware dosent exists on player
437 ERR_DIALOG(wxT("[ERR] File ") + path1 + wxT(" does not Exist"),
438 wxT("Bootloader rem"));
439 return false;
443 if(!wxFileExists(path2)) //Original Firmware dosent exists on player
445 ERR_DIALOG(wxT("[ERR] File ") + path2 + wxT(" does not Exist"),
446 wxT("Bootloader rem"));
447 return false;
450 if(!wxRenameFile(path2,path1,true)) //rename Firmware to Original
452 ERR_DIALOG(wxT("[ERR] Coud not rename ") + path2 + wxT(" to ")
453 + path1, wxT("Bootloader add"));
454 return false;
458 // shouldn't get here
459 return false;
462 // FWPatcher
463 bool fwpatcher(int mode,wxString bootloadername,wxString deviceDir,wxString firmware)
465 if(mode == BOOTLOADER_ADD)
467 char md5sum_str[32];
468 wxString src,dest,err;
469 int series,table_entry;
471 if (!FileMD5(firmware, md5sum_str)) {
472 ERR_DIALOG(wxT("Could not open firmware"), wxT("Open Firmware"));
473 return false;
475 else {
476 /* Check firmware against md5sums in h120sums and h100sums */
477 series = 0;
478 table_entry = intable(md5sum_str, &h120pairs[0],
479 sizeof(h120pairs)/sizeof(struct sumpairs));
480 if (table_entry >= 0) {
481 series = 120;
483 else {
484 table_entry = intable(md5sum_str, &h100pairs[0],
485 sizeof(h100pairs)/sizeof(struct sumpairs));
486 if (table_entry >= 0) {
487 series = 100;
489 else {
490 table_entry =
491 intable(md5sum_str, &h300pairs[0],
492 sizeof(h300pairs)/sizeof(struct sumpairs));
493 if (table_entry >= 0)
494 series = 300;
497 if (series == 0) {
498 ERR_DIALOG(wxT("Could not detect firmware type"), wxT("Detect Player out of Firmware"));
499 return false;
501 else
503 //Download bootloader
504 src = gv->bootloader_url + wxT("/iriver/")
505 + bootloadername;
506 dest = gv->stdpaths->GetUserDataDir()
507 + wxT("" PATH_SEP "download" PATH_SEP)
508 + bootloadername;
509 if( DownloadURL(src, dest) )
511 wxRemoveFile(dest);
512 ERR_DIALOG(wxT("Unable to download ") + src,
513 wxT("Install"));
514 return false;
517 if(!PatchFirmware(firmware,dest,series, table_entry)) // Patch firmware
519 ERR_DIALOG(wxT("Patching Firmware failed"),
520 wxT("Patching Firmware"));
521 return false;
525 // Load patched Firmware to player
526 src = gv->stdpaths->GetUserDataDir()
527 + wxT("" PATH_SEP "download" PATH_SEP "new.hex");
529 if(gv->curplat == wxT("h100"))
530 dest = deviceDir + wxT("" PATH_SEP "ihp_100.hex");
531 else if(gv->curplat == wxT("h120"))
532 dest = deviceDir + wxT("" PATH_SEP "ihp_120.hex");
533 else if(gv->curplat == wxT("h300"))
534 dest = deviceDir + wxT("" PATH_SEP "H300.hex");
536 if(!wxRenameFile(src,dest))
538 ERR_DIALOG(wxT("Copying Firmware to Device failed"),
539 wxT("Copying Firmware"));
540 return false;
542 else
544 return true;
549 else
551 return false; //no uninstall possible