1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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"
26 #include "installlog.h"
30 // reserves memory for ipodpatcher
31 bool initIpodpatcher()
33 if (ipod_alloc_buffer(§orbuf
,BUFFER_SIZE
) < 0) return true;
36 // uses ipodpatcher for add and rem of bootloader
37 bool ipodpatcher(int mode
,wxString bootloadername
)
39 wxString src
,dest
,buf
;
43 int n
= ipod_scan(&ipod
);
46 ERR_DIALOG(wxT("[ERR] No ipods found."), wxT("Scanning for Ipods"));
51 ERR_DIALOG(wxT("[ERR] to many ipods found."), wxT("Scanning for Ipods"));
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
) )
65 ERR_DIALOG(wxT("Unable to download ") + src
, wxT("Install"));
70 if (ipod_open(&ipod
, 0) < 0)
72 ERR_DIALOG(wxT("[ERR] could not open ipod"), wxT("open Ipod"));
76 if (read_partinfo(&ipod
,0) < 0)
78 ERR_DIALOG(wxT("[ERR] could not read partitiontable"), wxT("reading partitiontable"));
82 if (ipod
.pinfo
[0].start
==0)
84 ERR_DIALOG(wxT("[ERR] No partition 0 on disk"), wxT("reading partitiontable"));
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"),
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
),
99 ERR_DIALOG(buf
, wxT("reading partitiontable"));
105 read_directory(&ipod
);
107 if (ipod
.nimages
<= 0)
109 ERR_DIALOG(wxT("[ERR] Failed to read firmware directory"), wxT("reading directory"));
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"));
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"));
133 if (add_bootloader(&ipod
, (char*)dest
.c_str(), FILETYPE_DOT_IPOD
)==0) {
136 ERR_DIALOG(wxT("[ERR] failed to add Bootloader"), wxT("Bootloader add"));
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"));
147 if (ipod
.ipod_directory
[0].entryOffset
==0) {
148 ERR_DIALOG(wxT("[ERR] No bootloader detected.\n"), wxT("Bootloader del"));
151 if (delete_bootloader(&ipod
)==0) {
154 ERR_DIALOG(wxT("[ERR] --delete-bootloader failed.\n"), wxT("Bootloader del"));
164 // reserves memory for sansapatcher
165 bool initSansaPatcher()
167 if (sansa_alloc_buffer(§orbuf
,BUFFER_SIZE
) < 0) return true;
173 bool sansapatcher(int mode
,wxString bootloadername
)
175 wxString src
,dest
,buf
;
177 struct sansa_t sansa
;
179 int n
= sansa_scan(&sansa
);
182 ERR_DIALOG(wxT("[ERR] No Sansa found."), wxT("Scanning for Sansa"));
187 ERR_DIALOG(wxT("[ERR] to many Sansa found."), wxT("Scanning for Sansa"));
192 if(mode
== BOOTLOADER_ADD
)
194 src
= gv
->bootloader_url
+ wxT("/sandisk-sansa/e200/")
196 dest
= gv
->stdpaths
->GetUserDataDir()
197 + wxT("" PATH_SEP
"download" PATH_SEP
) + bootloadername
;
198 if ( DownloadURL(src
, dest
) )
201 ERR_DIALOG(wxT("Unable to download ") + src
, wxT("Download"));
206 if (sansa_open(&sansa
, 0) < 0)
208 ERR_DIALOG(wxT("[ERR] could not open sansa"), wxT("open Sansa"));
212 if (sansa_read_partinfo(&sansa
,0) < 0)
214 ERR_DIALOG(wxT("[ERR] could not read partitiontable"), wxT("reading partitiontable"));
218 int i
= is_e200(&sansa
);
220 ERR_DIALOG(wxT("[ERR] Disk is not an E200 (%d), aborting.\n"), wxT("Checking Disk"));
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"));
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"));
242 if (sansa_add_bootloader(&sansa
, (char*)dest
.c_str(), FILETYPE_MI4
)==0) {
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) {
258 ERR_DIALOG(wxT("[ERR] failed to remove Bootloader"), wxT("Bootloader remove"));
266 // gigabeatinstallation
267 bool gigabeatf(int mode
,wxString bootloadername
,wxString deviceDir
)
269 wxString path1
,path2
;
274 + wxT("" PATH_SEP
"GBSYSTEM" PATH_SEP
"FWIMG" PATH_SEP
"FWIMG01.DAT");
276 if(mode
== BOOTLOADER_ADD
)
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
) )
285 ERR_DIALOG(wxT("Unable to download ") + src
, wxT("Install"));
290 if(!wxFileExists(path1
))
292 ERR_DIALOG(wxT("[ERR] Coud not find ")+path1
, wxT("Bootloader add"));
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"));
308 if(!wxCopyFile(dest
,path1
))
310 ERR_DIALOG(wxT("[ERR] Coud not copy ") + dest
+ wxT(" to ")
311 + path2
, wxT("Bootloader add"));
315 else if(mode
== BOOTLOADER_REM
)
318 path2
.Append(wxT(".ORIG"));
319 if(!wxFileExists(path2
))
321 ERR_DIALOG(wxT("[ERR] Coud not find ") + path1
,
322 wxT("Bootloader del"));
325 if(!wxRenameFile(path2
,path1
,true))
327 ERR_DIALOG(wxT("[ERR] Coud not rename ") + path1
+ wxT(" to ")
328 + path2
, wxT("Bootloader del"));
335 // iaudio bootloader install
336 bool iaudiox5(int mode
,wxString bootloadername
,wxString deviceDir
)
338 wxString path1
,path2
;
342 path1
= deviceDir
+ wxT("" PATH_SEP
"FIRMWARE" PATH_SEP
)
345 if(mode
== BOOTLOADER_ADD
)
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
) )
354 ERR_DIALOG(wxT("Unable to download ") + src
, wxT("Install"));
359 if(!wxCopyFile(dest
,path1
))
361 ERR_DIALOG(wxT("[ERR] Coud not copy ")+dest
+wxT(" to ")+path2
,
362 wxT("Bootloader add"));
366 return true; // install ready
369 return false; //no uninstallation possible
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
)
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
) )
391 ERR_DIALOG(wxT("Unable to download ") + src
, wxT("Install"));
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"));
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"));
418 if(!wxCopyFile(dest
,path1
)) // copy file
420 ERR_DIALOG(wxT("[ERR] Coud not copy ") + dest
+ wxT(" to ") + path1
,
421 wxT("Bootloader add"));
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"));
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"));
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"));
458 // shouldn't get here
463 bool fwpatcher(int mode
,wxString bootloadername
,wxString deviceDir
,wxString firmware
)
465 if(mode
== BOOTLOADER_ADD
)
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"));
476 /* Check firmware against md5sums in h120sums and h100sums */
478 table_entry
= intable(md5sum_str
, &h120pairs
[0],
479 sizeof(h120pairs
)/sizeof(struct sumpairs
));
480 if (table_entry
>= 0) {
484 table_entry
= intable(md5sum_str
, &h100pairs
[0],
485 sizeof(h100pairs
)/sizeof(struct sumpairs
));
486 if (table_entry
>= 0) {
491 intable(md5sum_str
, &h300pairs
[0],
492 sizeof(h300pairs
)/sizeof(struct sumpairs
));
493 if (table_entry
>= 0)
498 ERR_DIALOG(wxT("Could not detect firmware type"), wxT("Detect Player out of Firmware"));
503 //Download bootloader
504 src
= gv
->bootloader_url
+ wxT("/iriver/")
506 dest
= gv
->stdpaths
->GetUserDataDir()
507 + wxT("" PATH_SEP
"download" PATH_SEP
)
509 if( DownloadURL(src
, dest
) )
512 ERR_DIALOG(wxT("Unable to download ") + src
,
517 if(!PatchFirmware(firmware
,dest
,series
, table_entry
)) // Patch firmware
519 ERR_DIALOG(wxT("Patching Firmware failed"),
520 wxT("Patching Firmware"));
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"));
551 return false; //no uninstall possible