1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006-2007 Dave Chapman
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
28 #include <sys/types.h>
31 #include "sansapatcher.h"
33 #include "parttypes.h"
35 #include "bootimg_c200.h"
36 #include "bootimg_e200.h"
40 #define VERSION "0.8 with v6.0 bootloaders"
59 static void print_usage(void)
61 fprintf(stderr
,"Usage: sansapatcher --scan\n");
63 fprintf(stderr
," or sansapatcher [DISKNO] [action]\n");
65 fprintf(stderr
," or sansapatcher [device] [action]\n");
68 fprintf(stderr
,"Where [action] is one of the following options:\n");
69 fprintf(stderr
," --install\n");
70 fprintf(stderr
," -l, --list\n");
71 fprintf(stderr
," -rf, --read-firmware filename.mi4\n");
72 fprintf(stderr
," -a, --add-bootloader filename.mi4\n");
73 fprintf(stderr
," -d, --delete-bootloader\n");
74 fprintf(stderr
," -of --update-original-firmware filename.mi4\n");
75 fprintf(stderr
," -bl --update-ppbl filename.bin\n");
79 fprintf(stderr
,"DISKNO is the number (e.g. 2) Windows has assigned to your sansa's hard disk.\n");
80 fprintf(stderr
,"The first hard disk in your computer (i.e. C:\\) will be disk 0, the next disk\n");
81 fprintf(stderr
,"will be disk 1 etc. sansapatcher will refuse to access a disk unless it\n");
82 fprintf(stderr
,"can identify it as being an E200 or C200.\n");
85 #if defined(linux) || defined (__linux)
86 fprintf(stderr
,"\"device\" is the device node (e.g. /dev/sda) assigned to your sansa.\n");
87 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
88 fprintf(stderr
,"\"device\" is the device node (e.g. /dev/da1) assigned to your sansa.\n");
89 #elif defined(__APPLE__) && defined(__MACH__)
90 fprintf(stderr
,"\"device\" is the device node (e.g. /dev/disk1) assigned to your sansa.\n");
92 fprintf(stderr
,"sansapatcher will refuse to access a disk unless it can identify it as being\n");
93 fprintf(stderr
,"an E200 or C200.\n");
97 static const char* get_parttype(int pt
)
100 static const char unknown
[]="Unknown";
107 while (parttypes
[i
].name
!= NULL
) {
108 if (parttypes
[i
].type
== pt
) {
109 return (parttypes
[i
].name
);
117 static void display_partinfo(struct sansa_t
* sansa
)
120 double sectors_per_MB
= (1024.0*1024.0)/sansa
->sector_size
;
122 printf("[INFO] Part Start Sector End Sector Size (MB) Type\n");
123 for ( i
= 0; i
< 4; i
++ ) {
124 if (sansa
->pinfo
[i
].start
!= 0) {
125 printf("[INFO] %d %10ld %10ld %10.1f %s (0x%02x)\n",
127 sansa
->pinfo
[i
].start
,
128 sansa
->pinfo
[i
].start
+sansa
->pinfo
[i
].size
-1,
129 sansa
->pinfo
[i
].size
/sectors_per_MB
,
130 get_parttype(sansa
->pinfo
[i
].type
),
131 sansa
->pinfo
[i
].type
);
137 int main(int argc
, char* argv
[])
143 int action
= SHOW_INFO
;
144 struct sansa_t sansa
;
146 unsigned char* buf
= NULL
;
149 fprintf(stderr
,"sansapatcher v" VERSION
" - (C) Dave Chapman 2006-2007\n");
150 fprintf(stderr
,"This is free software; see the source for copying conditions. There is NO\n");
151 fprintf(stderr
,"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
153 if ((argc
> 1) && ((strcmp(argv
[1],"-h")==0) || (strcmp(argv
[1],"--help")==0))) {
158 if (sansa_alloc_buffer(&sansa_sectorbuf
,BUFFER_SIZE
) < 0) {
159 fprintf(stderr
,"Failed to allocate memory buffer\n");
162 if ((argc
> 1) && (strcmp(argv
[1],"--scan")==0)) {
163 if (sansa_scan(&sansa
) == 0)
164 fprintf(stderr
,"[ERR] No E200s or C200s found.\n");
168 /* If the first parameter doesn't start with -, then we interpret it as a device */
169 if ((argc
> 1) && (argv
[1][0] != '-')) {
172 snprintf(sansa
.diskname
,sizeof(sansa
.diskname
),"\\\\.\\PhysicalDrive%s",argv
[1]);
174 strncpy(sansa
.diskname
,argv
[1],sizeof(sansa
.diskname
));
178 /* Autoscan for C200/E200s */
179 n
= sansa_scan(&sansa
);
181 fprintf(stderr
,"[ERR] No E200s or C200s found, aborting\n");
182 fprintf(stderr
,"[ERR] Please connect your sansa and ensure it is in UMS mode\n");
183 #if defined(__APPLE__) && defined(__MACH__)
184 fprintf(stderr
,"[ERR] Also ensure that your Sansa's main partition is not mounted.\n");
185 #elif !defined(__WIN32__)
187 fprintf(stderr
,"[ERR] You may also need to run sansapatcher as root.\n");
190 fprintf(stderr
,"[ERR] Please refer to the Rockbox manual if you continue to have problems.\n");
192 fprintf(stderr
,"[ERR] %d Sansas found, aborting\n",n
);
193 fprintf(stderr
,"[ERR] Please connect only one Sansa and re-run sansapatcher.\n");
198 printf("\nPress ENTER to exit sansapatcher :");
199 fgets(yesno
,4,stdin
);
208 action
= INTERACTIVE
;
212 if ((strcmp(argv
[i
],"-l")==0) || (strcmp(argv
[i
],"--list")==0)) {
213 action
= LIST_IMAGES
;
215 } else if (strcmp(argv
[i
],"--install")==0) {
218 } else if ((strcmp(argv
[i
],"-d")==0) ||
219 (strcmp(argv
[i
],"--delete-bootloader")==0)) {
220 action
= DELETE_BOOTLOADER
;
222 } else if ((strcmp(argv
[i
],"-a")==0) ||
223 (strcmp(argv
[i
],"--add-bootloader")==0)) {
224 action
= ADD_BOOTLOADER
;
226 if (i
== argc
) { print_usage(); return 1; }
229 } else if ((strcmp(argv
[i
],"-of")==0) ||
230 (strcmp(argv
[i
],"--update-original-firmware")==0)) {
233 if (i
== argc
) { print_usage(); return 1; }
236 } else if ((strcmp(argv
[i
],"-bl")==0) ||
237 (strcmp(argv
[i
],"--update-ppbl")==0)) {
238 action
= UPDATE_PPBL
;
240 if (i
== argc
) { print_usage(); return 1; }
243 } else if ((strcmp(argv
[i
],"-rf")==0) ||
244 (strcmp(argv
[i
],"--read-firmware")==0)) {
245 action
= READ_FIRMWARE
;
247 if (i
== argc
) { print_usage(); return 1; }
253 if (sansa
.diskname
[0]==0) {
258 if (sansa_open(&sansa
, 0) < 0) {
262 fprintf(stderr
,"[INFO] Reading partition table from %s\n",sansa
.diskname
);
263 fprintf(stderr
,"[INFO] Sector size is %d bytes\n",sansa
.sector_size
);
265 if (sansa_read_partinfo(&sansa
,0) < 0) {
269 display_partinfo(&sansa
);
271 i
= is_sansa(&sansa
);
273 fprintf(stderr
,"[ERR] Disk is not an E200 or C200 (%d), aborting.\n",i
);
277 if (sansa
.hasoldbootloader
) {
278 printf("[ERR] ************************************************************************\n");
279 printf("[ERR] *** OLD ROCKBOX INSTALLATION DETECTED, ABORTING.\n");
280 printf("[ERR] *** You must reinstall the original Sansa firmware before running\n");
281 printf("[ERR] *** sansapatcher for the first time.\n");
282 printf("[ERR] *** See http://www.rockbox.org/twiki/bin/view/Main/SansaE200Install\n");
283 printf("[ERR] ************************************************************************\n");
286 if (action
==LIST_IMAGES
) {
287 sansa_list_images(&sansa
);
289 } else if (action
==INTERACTIVE
) {
291 printf("Enter i to install the Rockbox bootloader, u to uninstall\n or c to cancel and do nothing (i/u/c) :");
293 if (fgets(yesno
,4,stdin
)) {
295 if (sansa_reopen_rw(&sansa
) < 0) {
298 if (strcmp(sansa
.targetname
,"c200") == 0) {
299 len
= LEN_bootimg_c200
;
302 len
= LEN_bootimg_e200
;
305 if (sansa_add_bootloader(&sansa
, buf
, len
)==0) {
306 fprintf(stderr
,"[INFO] Bootloader installed successfully.\n");
308 fprintf(stderr
,"[ERR] --install failed.\n");
311 } else if (yesno
[0]=='u') {
312 if (sansa_reopen_rw(&sansa
) < 0) {
316 if (sansa_delete_bootloader(&sansa
)==0) {
317 fprintf(stderr
,"[INFO] Bootloader removed.\n");
319 fprintf(stderr
,"[ERR] Bootloader removal failed.\n");
325 } else if (action
==READ_FIRMWARE
) {
326 if (sansa_read_firmware(&sansa
, filename
)==0) {
327 fprintf(stderr
,"[INFO] Firmware read to file %s.\n",filename
);
329 fprintf(stderr
,"[ERR] --read-firmware failed.\n");
332 } else if (action
==INSTALL
) {
333 if (sansa_reopen_rw(&sansa
) < 0) {
337 if (strcmp(sansa
.targetname
,"c200") == 0) {
338 len
= LEN_bootimg_c200
;
341 len
= LEN_bootimg_e200
;
345 if (sansa_add_bootloader(&sansa
, buf
, len
)==0) {
346 fprintf(stderr
,"[INFO] Bootloader installed successfully.\n");
348 fprintf(stderr
,"[ERR] --install failed.\n");
351 } else if (action
==ADD_BOOTLOADER
) {
352 if (sansa_reopen_rw(&sansa
) < 0) {
356 len
= sansa_read_bootloader(&sansa
, filename
, &buf
);
358 if (sansa_add_bootloader(&sansa
, buf
, len
)==0) {
359 fprintf(stderr
,"[INFO] Bootloader %s written to device.\n",filename
);
361 fprintf(stderr
,"[ERR] --add-bootloader failed.\n");
364 } else if (action
==DELETE_BOOTLOADER
) {
365 if (sansa_reopen_rw(&sansa
) < 0) {
369 if (sansa_delete_bootloader(&sansa
)==0) {
370 fprintf(stderr
,"[INFO] Bootloader removed successfully.\n");
372 fprintf(stderr
,"[ERR] --delete-bootloader failed.\n");
374 } else if (action
==UPDATE_OF
) {
375 if (sansa_reopen_rw(&sansa
) < 0) {
379 if (sansa_update_of(&sansa
, filename
)==0) {
380 fprintf(stderr
,"[INFO] OF updated successfully.\n");
382 fprintf(stderr
,"[ERR] --update-original-firmware failed.\n");
384 } else if (action
==UPDATE_PPBL
) {
385 printf("[WARN] PPBL installation will overwrite your bootloader. This will lead to a\n");
386 printf(" Sansa that won't boot if the bootloader file is invalid. Only continue if\n");
387 printf(" you're sure you know what you're doing.\n");
388 printf(" Continue (y/n)? ");
390 if (fgets(yesno
,4,stdin
)) {
392 if (sansa_reopen_rw(&sansa
) < 0) {
396 if (sansa_update_ppbl(&sansa
, filename
)==0) {
397 fprintf(stderr
,"[INFO] PPBL updated successfully.\n");
399 fprintf(stderr
,"[ERR] --update-ppbl failed.\n");
408 if (action
==INTERACTIVE
) {
409 printf("Press ENTER to exit sansapatcher :");
410 fgets(yesno
,4,stdin
);