Ooops. Forgot to add Philippe Latulippe to the credits file.
[Rockbox.git] / rbutil / bootloaders.cpp
blobd424a4f35e15238f77a2453df97f8393ae7b916f
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 // for ipodpatcher
30 int verbose = 0;
31 // reserves memory for ipodpatcher
32 bool initIpodpatcher()
34 if (ipod_alloc_buffer(&sectorbuf,BUFFER_SIZE) < 0) return true;
35 else return false;
37 // uses ipodpatcher for add and rem of bootloader
38 bool ipodpatcher(int mode)
40 wxString src,dest,buf;
42 // downloading files
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) )
50 wxRemoveFile(dest);
51 buf.Printf(wxT("Unable to download %s"), src.c_str() );
52 ERR_DIALOG(buf, wxT("Install"));
53 return false;
57 struct ipod_t ipod;
59 int n = ipod_scan(&ipod);
60 if (n == 0)
62 ERR_DIALOG(wxT("[ERR] No ipods found."), wxT("Scanning for Ipods"));
63 return false;
65 if (n > 1)
67 ERR_DIALOG(wxT("[ERR] to many ipods found."), wxT("Scanning for Ipods"));
68 return false;
71 if (ipod_open(&ipod, 0) < 0)
73 ERR_DIALOG(wxT("[ERR] could not open ipod"), wxT("open Ipod"));
74 return false;
77 if (read_partinfo(&ipod,0) < 0)
79 ERR_DIALOG(wxT("[ERR] could not read partitiontable"), wxT("reading partitiontable"));
80 return false;
83 if (ipod.pinfo[0].start==0)
85 ERR_DIALOG(wxT("[ERR] No partition 0 on disk"), wxT("reading partitiontable"));
86 int i;
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"),
95 ipod.pinfo[i].start,
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),
99 ipod.pinfo[i].type);
100 ERR_DIALOG(buf, wxT("reading partitiontable"));
103 return false;
106 read_directory(&ipod);
108 if (ipod.nimages <= 0)
110 ERR_DIALOG(wxT("[ERR] Failed to read firmware directory"), wxT("reading directory"));
111 return false;
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"));
118 return false;
121 if (ipod.macpod)
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"));
131 return false;
134 if (add_bootloader(&ipod, (char*)dest.c_str(), FILETYPE_DOT_IPOD)==0) {
136 } else {
137 ERR_DIALOG(wxT("[ERR] failed to add Bootloader"), wxT("Bootloader add"));
138 return false;
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"));
145 return false;
148 if (ipod.ipod_directory[0].entryOffset==0) {
149 ERR_DIALOG(wxT("[ERR] No bootloader detected.\n"), wxT("Bootloader del"));
150 return false;
151 } else {
152 if (delete_bootloader(&ipod)==0) {
154 } else {
155 ERR_DIALOG(wxT("[ERR] --delete-bootloader failed.\n"), wxT("Bootloader del"));
156 return false;
161 ipod_close(&ipod);
162 return true;
165 // gigabeatinstallation
166 bool gigabeatf(int mode)
168 wxString path1,path2;
169 wxString err;
170 wxString src,dest;
172 path1.Printf(wxT("%s" PATH_SEP "GBSYSTEM" PATH_SEP "FWIMG" PATH_SEP "FWIMG01.DAT"),gv->curdestdir.c_str());
174 if(mode == BOOTLOADER_ADD)
176 //Files downloaden
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) )
182 wxRemoveFile(dest);
183 err.Printf(wxT("Unable to download %s"), src.c_str() );
184 ERR_DIALOG(err, wxT("Install"));
185 return false;
189 if(!wxFileExists(path1))
191 err.Printf(wxT("[ERR] Coud not find %s"),path1.c_str());
192 ERR_DIALOG(err, wxT("Bootloader add"));
193 return false;
195 path2 = path1;
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"));
203 return false;
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"));
212 return false;
215 else if(mode == BOOTLOADER_REM)
217 path2 = path1;
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"));
223 return false;
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"));
229 return false;
232 return true;
235 // iaudio bootloader install
236 bool iaudiox5(int mode)
238 wxString path1,path2;
239 wxString err;
240 wxString src,dest;
242 path1.Printf(wxT("%s" PATH_SEP "FIRMWARE" PATH_SEP "%s"),gv->curdestdir.c_str(),gv->curbootloader.c_str());
244 if(mode == BOOTLOADER_ADD)
246 //Files downloaden
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) )
252 wxRemoveFile(dest);
253 err.Printf(wxT("Unable to download %s"), src.c_str() );
254 ERR_DIALOG(err, wxT("Install"));
255 return false;
258 // copy file
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"));
263 return false;
266 return true; // install ready
268 else
269 return false; //no uninstallation possible
272 // H10 install
273 bool h10(int mode)
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)
284 //Files downloaden
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) )
290 wxRemoveFile(dest);
291 err.Printf(wxT("Unable to download %s"), src.c_str() );
292 ERR_DIALOG(err, wxT("Install"));
293 return false;
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"));
306 return false;
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"));
315 return false;
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"));
323 return false;
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"));
340 return false;
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"));
348 return false;
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"));
355 return false;
359 // shouldn't get here
360 return false;
363 // FWPatcher
364 bool fwpatcher(int mode)
366 if(mode == BOOTLOADER_ADD)
368 char md5sum_str[32];
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"));
374 return false;
376 else {
377 /* Check firmware against md5sums in h120sums and h100sums */
378 series = 0;
379 table_entry = intable(md5sum_str, &h120pairs[0],
380 sizeof(h120pairs)/sizeof(struct sumpairs));
381 if (table_entry >= 0) {
382 series = 120;
384 else {
385 table_entry = intable(md5sum_str, &h100pairs[0],
386 sizeof(h100pairs)/sizeof(struct sumpairs));
387 if (table_entry >= 0) {
388 series = 100;
390 else {
391 table_entry =
392 intable(md5sum_str, &h300pairs[0],
393 sizeof(h300pairs)/sizeof(struct sumpairs));
394 if (table_entry >= 0)
395 series = 300;
398 if (series == 0) {
399 ERR_DIALOG(wxT("Could not detect firmware type"), wxT("Detect Player out of Firmware"));
400 return false;
402 else
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) )
410 wxRemoveFile(dest);
411 err.Printf(wxT("Unable to download %s"), src.c_str() );
412 ERR_DIALOG(err, wxT("Install"));
413 return false;
416 if(!PatchFirmware(gv->curfirmware,dest,series, table_entry)) // Patch firmware
418 ERR_DIALOG(wxT("Patching Firmware failed"), wxT("Patching Firmware"));
419 return false;
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"));
437 return false;
439 else
441 return true;
447 else
449 return false; //no uninstall possible