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"
31 // reserves memory for ipodpatcher
32 bool initIpodpatcher()
34 if (ipod_alloc_buffer(§orbuf
,BUFFER_SIZE
) < 0) return true;
37 // uses ipodpatcher for add and rem of bootloader
38 bool ipodpatcher(int mode
)
40 wxString src
,dest
,buf
;
43 if(mode
== BOOTLOADER_ADD
)
45 src
.Printf(wxT("%s/ipod/%s.ipod"), gv
->bootloader_url
.c_str(),gv
->curbootloader
.c_str());
46 dest
.Printf(wxT("%s" PATH_SEP
"download" PATH_SEP
"%s"),
47 gv
->stdpaths
->GetUserDataDir().c_str(),gv
->curbootloader
.c_str());
48 if ( DownloadURL(src
, dest
) )
51 buf
.Printf(wxT("Unable to download %s"), src
.c_str() );
52 ERR_DIALOG(buf
, wxT("Install"));
59 int n
= ipod_scan(&ipod
);
62 ERR_DIALOG(wxT("[ERR] No ipods found."), wxT("Scanning for Ipods"));
67 ERR_DIALOG(wxT("[ERR] to many ipods found."), wxT("Scanning for Ipods"));
71 if (ipod_open(&ipod
, 0) < 0)
73 ERR_DIALOG(wxT("[ERR] could not open ipod"), wxT("open Ipod"));
77 if (read_partinfo(&ipod
,0) < 0)
79 ERR_DIALOG(wxT("[ERR] could not read partitiontable"), wxT("reading partitiontable"));
83 if (ipod
.pinfo
[0].start
==0)
85 ERR_DIALOG(wxT("[ERR] No partition 0 on disk"), wxT("reading partitiontable"));
87 double sectors_per_MB
= (1024.0*1024.0)/ipod
.sector_size
;
89 buf
.Printf(wxT("[INFO] Part Start Sector End Sector Size (MB) Type\n"));
90 ERR_DIALOG(buf
, wxT("reading partitiontable"));
91 for ( i
= 0; i
< 4; i
++ ) {
92 if (ipod
.pinfo
[i
].start
!= 0) {
93 buf
.Printf(wxT("[INFO] %d %10ld %10ld %10.1f %s (0x%02x)\n"),
96 ipod
.pinfo
[i
].start
+ipod
.pinfo
[i
].size
-1,
97 ipod
.pinfo
[i
].size
/sectors_per_MB
,
98 get_parttype(ipod
.pinfo
[i
].type
),
100 ERR_DIALOG(buf
, wxT("reading partitiontable"));
106 read_directory(&ipod
);
108 if (ipod
.nimages
<= 0)
110 ERR_DIALOG(wxT("[ERR] Failed to read firmware directory"), wxT("reading directory"));
113 if (getmodel(&ipod
,(ipod
.ipod_directory
[0].vers
>>8)) < 0)
115 buf
.Printf(wxT("[ERR] Unknown version number in firmware (%08x)\n"),
116 ipod
.ipod_directory
[0].vers
);
117 ERR_DIALOG(buf
, wxT("reading directory"));
123 WARN_DIALOG(wxT("Warning this is a MacPod, Rockbox doesnt work on this. Convert it to WinPod"),wxT("MacPod"));
127 if(mode
== BOOTLOADER_ADD
)
129 if (ipod_reopen_rw(&ipod
) < 0) {
130 ERR_DIALOG(wxT("[ERR] Could not open Ipod in RW mode"), wxT("Bootloader add"));
134 if (add_bootloader(&ipod
, (char*)dest
.c_str(), FILETYPE_DOT_IPOD
)==0) {
137 ERR_DIALOG(wxT("[ERR] failed to add Bootloader"), wxT("Bootloader add"));
141 else if(mode
== BOOTLOADER_REM
)
143 if (ipod_reopen_rw(&ipod
) < 0) {
144 ERR_DIALOG(wxT("[ERR] Could not open Ipod in RW mode"), wxT("Bootloader add"));
148 if (ipod
.ipod_directory
[0].entryOffset
==0) {
149 ERR_DIALOG(wxT("[ERR] No bootloader detected.\n"), wxT("Bootloader del"));
152 if (delete_bootloader(&ipod
)==0) {
155 ERR_DIALOG(wxT("[ERR] --delete-bootloader failed.\n"), wxT("Bootloader del"));
165 // gigabeatinstallation
166 bool gigabeatf(int mode
)
168 wxString path1
,path2
;
172 path1
.Printf(wxT("%s" PATH_SEP
"GBSYSTEM" PATH_SEP
"FWIMG" PATH_SEP
"FWIMG01.DAT"),gv
->curdestdir
.c_str());
174 if(mode
== BOOTLOADER_ADD
)
177 src
.Printf(wxT("%s/gigabeat/%s"), gv
->bootloader_url
.c_str(),gv
->curbootloader
.c_str());
178 dest
.Printf(wxT("%s" PATH_SEP
"download" PATH_SEP
"%s"),
179 gv
->stdpaths
->GetUserDataDir().c_str(),gv
->curbootloader
.c_str());
180 if( DownloadURL(src
, dest
) )
183 err
.Printf(wxT("Unable to download %s"), src
.c_str() );
184 ERR_DIALOG(err
, wxT("Install"));
189 if(!wxFileExists(path1
))
191 err
.Printf(wxT("[ERR] Coud not find %s"),path1
.c_str());
192 ERR_DIALOG(err
, wxT("Bootloader add"));
196 path2
.Append(wxT(".ORIG"));
197 if(!wxFileExists(path2
))
199 if(!wxRenameFile(path1
,path2
,false))
201 err
.Printf(wxT("[ERR] Coud not rename %s to %s"),path1
.c_str(),path2
.c_str());
202 ERR_DIALOG(err
, wxT("Bootloader add"));
208 if(!wxCopyFile(dest
,path1
))
210 err
.Printf(wxT("[ERR] Coud not copy %s to %s"),dest
.c_str(),path2
.c_str());
211 ERR_DIALOG(err
, wxT("Bootloader add"));
215 else if(mode
== BOOTLOADER_REM
)
218 path2
.Append(wxT(".ORIG"));
219 if(!wxFileExists(path2
))
221 err
.Printf(wxT("[ERR] Coud not find %s"),path1
.c_str());
222 ERR_DIALOG(err
, wxT("Bootloader del"));
225 if(!wxRenameFile(path2
,path1
,true))
227 err
.Printf(wxT("[ERR] Coud not rename %s to %s"),path1
.c_str(),path2
.c_str());
228 ERR_DIALOG(err
, wxT("Bootloader del"));
235 // iaudio bootloader install
236 bool iaudiox5(int mode
)
238 wxString path1
,path2
;
242 path1
.Printf(wxT("%s" PATH_SEP
"FIRMWARE" PATH_SEP
"%s"),gv
->curdestdir
.c_str(),gv
->curbootloader
.c_str());
244 if(mode
== BOOTLOADER_ADD
)
247 src
.Printf(wxT("%s/iaudio/%s"), gv
->bootloader_url
.c_str(),gv
->curbootloader
.c_str());
248 dest
.Printf(wxT("%s" PATH_SEP
"download" PATH_SEP
"%s"),
249 gv
->stdpaths
->GetUserDataDir().c_str(),gv
->curbootloader
.c_str());
250 if( DownloadURL(src
, dest
) )
253 err
.Printf(wxT("Unable to download %s"), src
.c_str() );
254 ERR_DIALOG(err
, wxT("Install"));
259 if(!wxCopyFile(dest
,path1
))
261 err
.Printf(wxT("[ERR] Coud not copy %s to %s"),dest
.c_str(),path2
.c_str());
262 ERR_DIALOG(err
, wxT("Bootloader add"));
266 return true; // install ready
269 return false; //no uninstallation possible
275 wxString err
,src
,dest
,path1
,path2
;
277 int pos
= gv
->curbootloader
.Find('/');
278 if(pos
== wxNOT_FOUND
) pos
= 0;
279 wxString firmwarename
= gv
->curbootloader
.SubString(pos
,gv
->curbootloader
.Length());
280 //wxString firmDir = gv->curbootloader.SubString(0,pos);
282 if(mode
== BOOTLOADER_ADD
)
285 src
.Printf(wxT("%s/iriver/%s"), gv
->bootloader_url
.c_str(),gv
->curbootloader
.c_str());
286 dest
.Printf(wxT("%s" PATH_SEP
"download" PATH_SEP
"%s"),
287 gv
->stdpaths
->GetUserDataDir().c_str(),firmwarename
.c_str());
288 if( DownloadURL(src
, dest
) )
291 err
.Printf(wxT("Unable to download %s"), src
.c_str() );
292 ERR_DIALOG(err
, wxT("Install"));
296 path1
.Printf(wxT("%sSYSTEM" PATH_SEP
"%s"),gv
->curdestdir
.c_str(),firmwarename
.c_str());
297 path2
.Printf(wxT("%sSYSTEM" PATH_SEP
"Original.mi4"),gv
->curdestdir
.c_str());
299 if(!wxFileExists(path1
)) //Firmware dosent exists on player
301 path1
.Printf(wxT("%sSYSTEM" PATH_SEP
"H10EMP.mi4"),gv
->curdestdir
.c_str()); //attempt other firmwarename
302 if(!wxFileExists(path1
)) //Firmware dosent exists on player
304 err
.Printf(wxT("[ERR] File %s does not Exist"),path1
.c_str());
305 ERR_DIALOG(err
, wxT("Bootloader add"));
309 if(!wxFileExists(path2
)) //there is already a original firmware
311 if(!wxRenameFile(path1
,path2
,false)) //rename Firmware to Original
313 err
.Printf(wxT("[ERR] Coud not rename %s to %s"),path1
.c_str(),path2
.c_str());
314 ERR_DIALOG(err
, wxT("Bootloader add"));
319 if(!wxCopyFile(dest
,path1
)) // copy file
321 err
.Printf(wxT("[ERR] Coud not copy %s to %s"),dest
.c_str(),path1
.c_str());
322 ERR_DIALOG(err
,wxT("Bootloader add"));
326 return true; //install ready
329 else if(mode
== BOOTLOADER_REM
)
331 path1
.Printf(wxT("%sSYSTEM" PATH_SEP
"%s"),gv
->curdestdir
.c_str(),firmwarename
.c_str());
332 path2
.Printf(wxT("%sSYSTEM" PATH_SEP
"Original.mi4"),gv
->curdestdir
.c_str());
333 if(!wxFileExists(path1
)) //Firmware dosent exists on player
335 path1
.Printf(wxT("%s" PATH_SEP
"SYSTEM" PATH_SEP
"H10EMP.mi4"),gv
->curdestdir
.c_str()); //attempt other firmwarename
336 if(!wxFileExists(path1
)) //Firmware dosent exists on player
338 err
.Printf(wxT("[ERR] File %s does not Exist"),path1
.c_str());
339 ERR_DIALOG(err
, wxT("Bootloader rem"));
344 if(!wxFileExists(path2
)) //Original Firmware dosent exists on player
346 err
.Printf(wxT("[ERR] File %s does not Exist"),path2
.c_str());
347 ERR_DIALOG(err
, wxT("Bootloader rem"));
351 if(!wxRenameFile(path2
,path1
,true)) //rename Firmware to Original
353 err
.Printf(wxT("[ERR] Coud not rename %s to %s"),path2
.c_str(),path1
.c_str());
354 ERR_DIALOG(err
, wxT("Bootloader add"));
359 // shouldn't get here
364 bool fwpatcher(int mode
)
366 if(mode
== BOOTLOADER_ADD
)
369 wxString src
,dest
,err
;
370 int series
,table_entry
;
372 if (!FileMD5(gv
->curfirmware
, md5sum_str
)) {
373 ERR_DIALOG(wxT("Could not open firmware"), wxT("Open Firmware"));
377 /* Check firmware against md5sums in h120sums and h100sums */
379 table_entry
= intable(md5sum_str
, &h120pairs
[0],
380 sizeof(h120pairs
)/sizeof(struct sumpairs
));
381 if (table_entry
>= 0) {
385 table_entry
= intable(md5sum_str
, &h100pairs
[0],
386 sizeof(h100pairs
)/sizeof(struct sumpairs
));
387 if (table_entry
>= 0) {
392 intable(md5sum_str
, &h300pairs
[0],
393 sizeof(h300pairs
)/sizeof(struct sumpairs
));
394 if (table_entry
>= 0)
399 ERR_DIALOG(wxT("Could not detect firmware type"), wxT("Detect Player out of Firmware"));
404 //Download bootloader
405 src
.Printf(wxT("%s/iriver/%s"), gv
->bootloader_url
.c_str(),gv
->curbootloader
.c_str());
406 dest
.Printf(wxT("%s" PATH_SEP
"download" PATH_SEP
"%s"),
407 gv
->stdpaths
->GetUserDataDir().c_str(),gv
->curbootloader
.c_str());
408 if( DownloadURL(src
, dest
) )
411 err
.Printf(wxT("Unable to download %s"), src
.c_str() );
412 ERR_DIALOG(err
, wxT("Install"));
416 if(!PatchFirmware(gv
->curfirmware
,dest
,series
, table_entry
)) // Patch firmware
418 ERR_DIALOG(wxT("Patching Firmware failed"), wxT("Patching Firmware"));
423 // Load patched Firmware to player
424 src
.Printf(wxT("%s" PATH_SEP
"download" PATH_SEP
"new.hex"),
425 gv
->stdpaths
->GetUserDataDir().c_str());
427 if(gv
->curplat
== wxT("h100"))
428 dest
.Printf(wxT("%s" PATH_SEP
"ihp_100.hex"),gv
->curdestdir
.c_str());
429 else if(gv
->curplat
== wxT("h120"))
430 dest
.Printf(wxT("%s" PATH_SEP
"ihp_120.hex"),gv
->curdestdir
.c_str());
431 else if(gv
->curplat
== wxT("h300"))
432 dest
.Printf(wxT("%s" PATH_SEP
"H300.hex"),gv
->curdestdir
.c_str());
434 if(!wxRenameFile(src
,dest
))
436 ERR_DIALOG(wxT("Copying Firmware to Device failed"), wxT("Copying Firmware"));
449 return false; //no uninstall possible