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"
26 // reserves memory for ipodpatcher
27 bool initIpodpatcher()
29 if (ipod_alloc_buffer(§orbuf
,BUFFER_SIZE
) < 0) return true;
32 // uses ipodpatcher for add and rem of bootloader
33 bool ipodpatcher(int mode
,wxString bootloadername
)
35 wxString src
,dest
,buf
;
39 int n
= ipod_scan(&ipod
);
42 ERR_DIALOG(wxT("[ERR] No ipods found."), wxT("Scanning for Ipods"));
47 ERR_DIALOG(wxT("[ERR] to many ipods found."), wxT("Scanning for Ipods"));
52 if(mode
== BOOTLOADER_ADD
)
54 src
= gv
->bootloader_url
+ wxT("/ipod/")
55 + bootloadername
+ wxT(".ipod");
56 dest
= gv
->stdpaths
->GetUserDataDir()
57 + wxT("" PATH_SEP
"download" PATH_SEP
) + bootloadername
;
58 if ( DownloadURL(src
, dest
) )
61 ERR_DIALOG(wxT("Unable to download ") + src
, wxT("Install"));
66 if (ipod_open(&ipod
, 0) < 0)
68 ERR_DIALOG(wxT("[ERR] could not open ipod"), wxT("open Ipod"));
72 if (read_partinfo(&ipod
,0) < 0)
74 ERR_DIALOG(wxT("[ERR] could not read partitiontable"), wxT("reading partitiontable"));
78 if (ipod
.pinfo
[0].start
==0)
80 ERR_DIALOG(wxT("[ERR] No partition 0 on disk"), wxT("reading partitiontable"));
82 double sectors_per_MB
= (1024.0*1024.0)/ipod
.sector_size
;
84 buf
.Printf(wxT("[INFO] Part Start Sector End Sector Size (MB) Type\n"));
85 ERR_DIALOG(buf
, wxT("reading partitiontable"));
86 for ( i
= 0; i
< 4; i
++ ) {
87 if (ipod
.pinfo
[i
].start
!= 0) {
88 buf
.Printf(wxT("[INFO] %d %10ld %10ld %10.1f %s (0x%02x)\n"),
91 ipod
.pinfo
[i
].start
+ipod
.pinfo
[i
].size
-1,
92 ipod
.pinfo
[i
].size
/sectors_per_MB
,
93 get_parttype(ipod
.pinfo
[i
].type
),
95 ERR_DIALOG(buf
, wxT("reading partitiontable"));
101 read_directory(&ipod
);
103 if (ipod
.nimages
<= 0)
105 ERR_DIALOG(wxT("[ERR] Failed to read firmware directory"), wxT("reading directory"));
108 if (getmodel(&ipod
,(ipod
.ipod_directory
[0].vers
>>8)) < 0)
110 buf
.Printf(wxT("[ERR] Unknown version number in firmware (%08x)\n"),
111 ipod
.ipod_directory
[0].vers
);
112 ERR_DIALOG(buf
, wxT("reading directory"));
118 WARN_DIALOG(wxT("Warning this is a MacPod, Rockbox doesnt work on this. Convert it to WinPod"),wxT("MacPod"));
122 if(mode
== BOOTLOADER_ADD
)
124 if (ipod_reopen_rw(&ipod
) < 0) {
125 ERR_DIALOG(wxT("[ERR] Could not open Ipod in RW mode"), wxT("Bootloader add"));
129 if (add_bootloader(&ipod
, (char*)dest
.c_str(), FILETYPE_DOT_IPOD
)==0) {
132 ERR_DIALOG(wxT("[ERR] failed to add Bootloader"), wxT("Bootloader add"));
136 else if(mode
== BOOTLOADER_REM
)
138 if (ipod_reopen_rw(&ipod
) < 0) {
139 ERR_DIALOG(wxT("[ERR] Could not open Ipod in RW mode"), wxT("Bootloader add"));
143 if (ipod
.ipod_directory
[0].entryOffset
==0) {
144 ERR_DIALOG(wxT("[ERR] No bootloader detected.\n"), wxT("Bootloader del"));
147 if (delete_bootloader(&ipod
)==0) {
150 ERR_DIALOG(wxT("[ERR] --delete-bootloader failed.\n"), wxT("Bootloader del"));
160 // reserves memory for sansapatcher
161 bool initSansaPatcher()
163 if (sansa_alloc_buffer(§orbuf
,BUFFER_SIZE
) < 0) return true;
169 bool sansapatcher(int mode
,wxString bootloadername
)
171 wxString src
,dest
,buf
;
173 struct sansa_t sansa
;
175 int n
= sansa_scan(&sansa
);
178 ERR_DIALOG(wxT("[ERR] No Sansa found."), wxT("Scanning for Sansa"));
183 ERR_DIALOG(wxT("[ERR] to many Sansa found."), wxT("Scanning for Sansa"));
188 if(mode
== BOOTLOADER_ADD
)
190 src
= gv
->bootloader_url
+ wxT("/sandisk-sansa/e200/")
192 dest
= gv
->stdpaths
->GetUserDataDir()
193 + wxT("" PATH_SEP
"download" PATH_SEP
) + bootloadername
;
194 if ( DownloadURL(src
, dest
) )
197 ERR_DIALOG(wxT("Unable to download ") + src
, wxT("Download"));
202 if (sansa_open(&sansa
, 0) < 0)
204 ERR_DIALOG(wxT("[ERR] could not open sansa"), wxT("open Sansa"));
208 if (sansa_read_partinfo(&sansa
,0) < 0)
210 ERR_DIALOG(wxT("[ERR] could not read partitiontable"), wxT("reading partitiontable"));
214 int i
= is_e200(&sansa
);
216 ERR_DIALOG(wxT("[ERR] Disk is not an E200 (%d), aborting.\n"), wxT("Checking Disk"));
220 if (sansa
.hasoldbootloader
)
222 ERR_DIALOG(wxT("[ERR] ************************************************************************\n"
223 "[ERR] *** OLD ROCKBOX INSTALLATION DETECTED, ABORTING.\n"
224 "[ERR] *** You must reinstall the original Sansa firmware before running\n"
225 "[ERR] *** sansapatcher for the first time.\n"
226 "[ERR] *** See http://www.rockbox.org/twiki/bin/view/Main/SansaE200Install\n"
227 "[ERR] ************************************************************************\n"),wxT("Checking Disk"));
231 if(mode
== BOOTLOADER_ADD
)
233 if (sansa_reopen_rw(&sansa
) < 0) {
234 ERR_DIALOG(wxT("[ERR] Could not open Sansa in RW mode"), wxT("Bootloader add"));
238 if (sansa_add_bootloader(&sansa
, (char*)dest
.c_str(), FILETYPE_MI4
)==0) {
241 ERR_DIALOG(wxT("[ERR] failed to add Bootloader"), wxT("Bootloader add"));
245 else if(mode
== BOOTLOADER_REM
)
247 if (sansa_reopen_rw(&sansa
) < 0) {
248 ERR_DIALOG(wxT("[ERR] Could not open Sansa in RW mode"), wxT("Bootloader Remove"));
251 if (sansa_delete_bootloader(&sansa
)==0) {
254 ERR_DIALOG(wxT("[ERR] failed to remove Bootloader"), wxT("Bootloader remove"));
262 // gigabeatinstallation
263 bool gigabeatf(int mode
,wxString bootloadername
,wxString deviceDir
)
265 wxString path1
,path2
;
270 + wxT("" PATH_SEP
"GBSYSTEM" PATH_SEP
"FWIMG" PATH_SEP
"FWIMG01.DAT");
272 if(mode
== BOOTLOADER_ADD
)
275 src
= gv
->bootloader_url
+ wxT("/gigabeat/") + bootloadername
;
276 dest
= gv
->stdpaths
->GetUserDataDir()
277 + wxT("" PATH_SEP
"download" PATH_SEP
) + bootloadername
;
278 if( DownloadURL(src
, dest
) )
281 ERR_DIALOG(wxT("Unable to download ") + src
, wxT("Install"));
286 if(!wxFileExists(path1
))
288 ERR_DIALOG(wxT("[ERR] Coud not find ")+path1
, wxT("Bootloader add"));
292 path2
.Append(wxT(".ORIG"));
293 if(!wxFileExists(path2
))
295 if(!wxRenameFile(path1
,path2
,false))
297 ERR_DIALOG(wxT("[ERR] Coud not rename ") + path1
+ wxT(" to ")
298 + path2
, wxT("Bootloader add"));
304 if(!wxCopyFile(dest
,path1
))
306 ERR_DIALOG(wxT("[ERR] Coud not copy ") + dest
+ wxT(" to ")
307 + path2
, wxT("Bootloader add"));
311 else if(mode
== BOOTLOADER_REM
)
314 path2
.Append(wxT(".ORIG"));
315 if(!wxFileExists(path2
))
317 ERR_DIALOG(wxT("[ERR] Coud not find ") + path1
,
318 wxT("Bootloader del"));
321 if(!wxRenameFile(path2
,path1
,true))
323 ERR_DIALOG(wxT("[ERR] Coud not rename ") + path1
+ wxT(" to ")
324 + path2
, wxT("Bootloader del"));
331 // iaudio bootloader install
332 bool iaudiox5(int mode
,wxString bootloadername
,wxString deviceDir
)
334 wxString path1
,path2
;
338 path1
= deviceDir
+ wxT("" PATH_SEP
"FIRMWARE" PATH_SEP
)
341 if(mode
== BOOTLOADER_ADD
)
344 src
= gv
->bootloader_url
+ wxT("/iaudio/") + bootloadername
;
345 dest
= gv
->stdpaths
->GetUserDataDir()
346 + wxT("" PATH_SEP
"download" PATH_SEP
) + bootloadername
;
347 if( DownloadURL(src
, dest
) )
350 ERR_DIALOG(wxT("Unable to download ") + src
, wxT("Install"));
355 if(!wxCopyFile(dest
,path1
))
357 ERR_DIALOG(wxT("[ERR] Coud not copy ")+dest
+wxT(" to ")+path2
,
358 wxT("Bootloader add"));
362 return true; // install ready
365 return false; //no uninstallation possible
369 bool h10(int mode
,wxString bootloadername
,wxString deviceDir
)
371 wxString err
,src
,dest
,path1
,path2
;
373 int pos
= bootloadername
.Find('/');
374 if(pos
== wxNOT_FOUND
) pos
= 0;
375 wxString firmwarename
= bootloadername
.SubString(pos
,bootloadername
.Length());
376 //wxString firmDir = gv->curbootloader.SubString(0,pos);
378 if(mode
== BOOTLOADER_ADD
)
381 src
= gv
->bootloader_url
+ wxT("/iriver/") + bootloadername
;
382 dest
= gv
->stdpaths
->GetUserDataDir()
383 + wxT("" PATH_SEP
"download" PATH_SEP
) + firmwarename
;
384 if( DownloadURL(src
, dest
) )
387 ERR_DIALOG(wxT("Unable to download ") + src
, wxT("Install"));
391 path1
= deviceDir
+ wxT("SYSTEM" PATH_SEP
) + firmwarename
;
392 path2
= deviceDir
+ wxT("SYSTEM" PATH_SEP
"Original.mi4");
394 if(!wxFileExists(path1
)) //Firmware dosent exists on player
396 path1
= deviceDir
+ wxT("SYSTEM" PATH_SEP
"H10EMP.mi4"); //attempt other firmwarename
397 if(!wxFileExists(path1
)) //Firmware dosent exists on player
399 ERR_DIALOG(wxT("[ERR] File ") + path1
+ wxT(" does not Exist"),
400 wxT("Bootloader add"));
404 if(!wxFileExists(path2
)) //there is already a original firmware
406 if(!wxRenameFile(path1
,path2
,false)) //rename Firmware to Original
408 ERR_DIALOG(wxT("[ERR] Coud not rename ") + path1
+ wxT(" to ")
409 + path2
, wxT("Bootloader add"));
414 if(!wxCopyFile(dest
,path1
)) // copy file
416 ERR_DIALOG(wxT("[ERR] Coud not copy ") + dest
+ wxT(" to ") + path1
,
417 wxT("Bootloader add"));
421 return true; //install ready
424 else if(mode
== BOOTLOADER_REM
)
426 path1
= deviceDir
+ wxT("SYSTEM" PATH_SEP
) + firmwarename
;
427 path2
= gv
->curdestdir
+ wxT("SYSTEM" PATH_SEP
"Original.mi4");
428 if(!wxFileExists(path1
)) //Firmware dosent exists on player
430 path1
= deviceDir
+ wxT("" PATH_SEP
"SYSTEM" PATH_SEP
"H10EMP.mi4"); //attempt other firmwarename
431 if(!wxFileExists(path1
)) //Firmware dosent exists on player
433 ERR_DIALOG(wxT("[ERR] File ") + path1
+ wxT(" does not Exist"),
434 wxT("Bootloader rem"));
439 if(!wxFileExists(path2
)) //Original Firmware dosent exists on player
441 ERR_DIALOG(wxT("[ERR] File ") + path2
+ wxT(" does not Exist"),
442 wxT("Bootloader rem"));
446 if(!wxRenameFile(path2
,path1
,true)) //rename Firmware to Original
448 ERR_DIALOG(wxT("[ERR] Coud not rename ") + path2
+ wxT(" to ")
449 + path1
, wxT("Bootloader add"));
454 // shouldn't get here
459 bool fwpatcher(int mode
,wxString bootloadername
,wxString deviceDir
,wxString firmware
)
461 if(mode
== BOOTLOADER_ADD
)
464 wxString src
,dest
,err
;
465 int series
,table_entry
;
467 if (!FileMD5(firmware
, md5sum_str
)) {
468 ERR_DIALOG(wxT("Could not open firmware"), wxT("Open Firmware"));
472 /* Check firmware against md5sums in h120sums and h100sums */
474 table_entry
= intable(md5sum_str
, &h120pairs
[0],
475 sizeof(h120pairs
)/sizeof(struct sumpairs
));
476 if (table_entry
>= 0) {
480 table_entry
= intable(md5sum_str
, &h100pairs
[0],
481 sizeof(h100pairs
)/sizeof(struct sumpairs
));
482 if (table_entry
>= 0) {
487 intable(md5sum_str
, &h300pairs
[0],
488 sizeof(h300pairs
)/sizeof(struct sumpairs
));
489 if (table_entry
>= 0)
494 ERR_DIALOG(wxT("Could not detect firmware type"), wxT("Detect Player out of Firmware"));
499 //Download bootloader
500 src
= gv
->bootloader_url
+ wxT("/iriver/")
502 dest
= gv
->stdpaths
->GetUserDataDir()
503 + wxT("" PATH_SEP
"download" PATH_SEP
)
505 if( DownloadURL(src
, dest
) )
508 ERR_DIALOG(wxT("Unable to download ") + src
,
513 if(!PatchFirmware(firmware
,dest
,series
, table_entry
)) // Patch firmware
515 ERR_DIALOG(wxT("Patching Firmware failed"),
516 wxT("Patching Firmware"));
521 // Load patched Firmware to player
522 src
= gv
->stdpaths
->GetUserDataDir()
523 + wxT("" PATH_SEP
"download" PATH_SEP
"new.hex");
525 if(gv
->curplat
== wxT("h100"))
526 dest
= deviceDir
+ wxT("" PATH_SEP
"ihp_100.hex");
527 else if(gv
->curplat
== wxT("h120"))
528 dest
= deviceDir
+ wxT("" PATH_SEP
"ihp_120.hex");
529 else if(gv
->curplat
== wxT("h300"))
530 dest
= deviceDir
+ wxT("" PATH_SEP
"H300.hex");
532 if(!wxRenameFile(src
,dest
))
534 ERR_DIALOG(wxT("Copying Firmware to Device failed"),
535 wxT("Copying Firmware"));
547 return false; //no uninstall possible