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 #define VERSION "0.6 with v4.0 bootloaders"
53 void print_usage(void)
55 fprintf(stderr
,"Usage: sansapatcher --scan\n");
57 fprintf(stderr
," or sansapatcher [DISKNO] [action]\n");
59 fprintf(stderr
," or sansapatcher [device] [action]\n");
62 fprintf(stderr
,"Where [action] is one of the following options:\n");
63 fprintf(stderr
," --install\n");
64 fprintf(stderr
," -l, --list\n");
65 fprintf(stderr
," -rf, --read-firmware filename.mi4\n");
66 fprintf(stderr
," -a, --add-bootloader filename.mi4\n");
67 fprintf(stderr
," -d, --delete-bootloader\n");
68 fprintf(stderr
," -of --update-original-firmware filename.mi4\n");
69 fprintf(stderr
," -bl --update-ppbl filename.bin\n");
73 fprintf(stderr
,"DISKNO is the number (e.g. 2) Windows has assigned to your sansa's hard disk.\n");
74 fprintf(stderr
,"The first hard disk in your computer (i.e. C:\\) will be disk 0, the next disk\n");
75 fprintf(stderr
,"will be disk 1 etc. sansapatcher will refuse to access a disk unless it\n");
76 fprintf(stderr
,"can identify it as being an E200 or C200.\n");
79 #if defined(linux) || defined (__linux)
80 fprintf(stderr
,"\"device\" is the device node (e.g. /dev/sda) assigned to your sansa.\n");
81 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
82 fprintf(stderr
,"\"device\" is the device node (e.g. /dev/da1) assigned to your sansa.\n");
83 #elif defined(__APPLE__) && defined(__MACH__)
84 fprintf(stderr
,"\"device\" is the device node (e.g. /dev/disk1) assigned to your sansa.\n");
86 fprintf(stderr
,"sansapatcher will refuse to access a disk unless it can identify it as being\n");
87 fprintf(stderr
,"an E200 or C200.\n");
91 char* get_parttype(int pt
)
94 static char unknown
[]="Unknown";
101 while (parttypes
[i
].name
!= NULL
) {
102 if (parttypes
[i
].type
== pt
) {
103 return (parttypes
[i
].name
);
111 void display_partinfo(struct sansa_t
* sansa
)
114 double sectors_per_MB
= (1024.0*1024.0)/sansa
->sector_size
;
116 printf("[INFO] Part Start Sector End Sector Size (MB) Type\n");
117 for ( i
= 0; i
< 4; i
++ ) {
118 if (sansa
->pinfo
[i
].start
!= 0) {
119 printf("[INFO] %d %10ld %10ld %10.1f %s (0x%02x)\n",
121 sansa
->pinfo
[i
].start
,
122 sansa
->pinfo
[i
].start
+sansa
->pinfo
[i
].size
-1,
123 sansa
->pinfo
[i
].size
/sectors_per_MB
,
124 get_parttype(sansa
->pinfo
[i
].type
),
125 sansa
->pinfo
[i
].type
);
131 int main(int argc
, char* argv
[])
137 int action
= SHOW_INFO
;
139 struct sansa_t sansa
;
142 fprintf(stderr
,"sansapatcher v" VERSION
" - (C) Dave Chapman 2006-2007\n");
143 fprintf(stderr
,"This is free software; see the source for copying conditions. There is NO\n");
144 fprintf(stderr
,"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
146 if ((argc
> 1) && ((strcmp(argv
[1],"-h")==0) || (strcmp(argv
[1],"--help")==0))) {
151 if (sansa_alloc_buffer(&sansa_sectorbuf
,BUFFER_SIZE
) < 0) {
152 fprintf(stderr
,"Failed to allocate memory buffer\n");
155 if ((argc
> 1) && (strcmp(argv
[1],"--scan")==0)) {
156 if (sansa_scan(&sansa
) == 0)
157 fprintf(stderr
,"[ERR] No E200s or C200s found.\n");
161 /* If the first parameter doesn't start with -, then we interpret it as a device */
162 if ((argc
> 1) && (argv
[1][0] != '-')) {
165 snprintf(sansa
.diskname
,sizeof(sansa
.diskname
),"\\\\.\\PhysicalDrive%s",argv
[1]);
167 strncpy(sansa
.diskname
,argv
[1],sizeof(sansa
.diskname
));
171 /* Autoscan for C200/E200s */
172 n
= sansa_scan(&sansa
);
174 fprintf(stderr
,"[ERR] No E200s or C200s found, aborting\n");
175 fprintf(stderr
,"[ERR] Please connect your sansa and ensure it is in UMS mode\n");
176 #if defined(__APPLE__) && defined(__MACH__)
177 fprintf(stderr
,"[ERR] Also ensure that your Sansa's main partition is not mounted.\n");
178 #elif !defined(__WIN32__)
180 fprintf(stderr
,"[ERR] You may also need to run sansapatcher as root.\n");
183 fprintf(stderr
,"[ERR] Please refer to the Rockbox manual if you continue to have problems.\n");
185 fprintf(stderr
,"[ERR] %d Sansas found, aborting\n",n
);
186 fprintf(stderr
,"[ERR] Please connect only one Sansa and re-run sansapatcher.\n");
191 printf("\nPress ENTER to exit sansapatcher :");
192 fgets(yesno
,4,stdin
);
200 action
= INTERACTIVE
;
203 if ((strcmp(argv
[i
],"-l")==0) || (strcmp(argv
[i
],"--list")==0)) {
204 action
= LIST_IMAGES
;
206 } else if (strcmp(argv
[i
],"--install")==0) {
209 } else if ((strcmp(argv
[i
],"-d")==0) ||
210 (strcmp(argv
[i
],"--delete-bootloader")==0)) {
211 action
= DELETE_BOOTLOADER
;
213 } else if ((strcmp(argv
[i
],"-a")==0) ||
214 (strcmp(argv
[i
],"--add-bootloader")==0)) {
215 action
= ADD_BOOTLOADER
;
218 if (i
== argc
) { print_usage(); return 1; }
221 } else if ((strcmp(argv
[i
],"-of")==0) ||
222 (strcmp(argv
[i
],"--update-original-firmware")==0)) {
225 if (i
== argc
) { print_usage(); return 1; }
228 } else if ((strcmp(argv
[i
],"-bl")==0) ||
229 (strcmp(argv
[i
],"--update-ppbl")==0)) {
230 action
= UPDATE_PPBL
;
232 if (i
== argc
) { print_usage(); return 1; }
235 } else if ((strcmp(argv
[i
],"-rf")==0) ||
236 (strcmp(argv
[i
],"--read-firmware")==0)) {
237 action
= READ_FIRMWARE
;
239 if (i
== argc
) { print_usage(); return 1; }
245 if (sansa
.diskname
[0]==0) {
250 if (sansa_open(&sansa
, 0) < 0) {
254 fprintf(stderr
,"[INFO] Reading partition table from %s\n",sansa
.diskname
);
255 fprintf(stderr
,"[INFO] Sector size is %d bytes\n",sansa
.sector_size
);
257 if (sansa_read_partinfo(&sansa
,0) < 0) {
261 display_partinfo(&sansa
);
263 i
= is_sansa(&sansa
);
265 fprintf(stderr
,"[ERR] Disk is not an E200 or C200 (%d), aborting.\n",i
);
269 if (sansa
.hasoldbootloader
) {
270 printf("[ERR] ************************************************************************\n");
271 printf("[ERR] *** OLD ROCKBOX INSTALLATION DETECTED, ABORTING.\n");
272 printf("[ERR] *** You must reinstall the original Sansa firmware before running\n");
273 printf("[ERR] *** sansapatcher for the first time.\n");
274 printf("[ERR] *** See http://www.rockbox.org/twiki/bin/view/Main/SansaE200Install\n");
275 printf("[ERR] ************************************************************************\n");
278 if (action
==LIST_IMAGES
) {
279 sansa_list_images(&sansa
);
280 } else if (action
==INTERACTIVE
) {
282 printf("Enter i to install the Rockbox bootloader, u to uninstall\n or c to cancel and do nothing (i/u/c) :");
284 if (fgets(yesno
,4,stdin
)) {
286 if (sansa_reopen_rw(&sansa
) < 0) {
290 if (sansa_add_bootloader(&sansa
, NULL
, FILETYPE_INTERNAL
)==0) {
291 fprintf(stderr
,"[INFO] Bootloader installed successfully.\n");
293 fprintf(stderr
,"[ERR] --install failed.\n");
296 } else if (yesno
[0]=='u') {
297 if (sansa_reopen_rw(&sansa
) < 0) {
301 if (sansa_delete_bootloader(&sansa
)==0) {
302 fprintf(stderr
,"[INFO] Bootloader removed.\n");
304 fprintf(stderr
,"[ERR] Bootloader removal failed.\n");
309 } else if (action
==READ_FIRMWARE
) {
310 if (sansa_read_firmware(&sansa
, filename
)==0) {
311 fprintf(stderr
,"[INFO] Firmware read to file %s.\n",filename
);
313 fprintf(stderr
,"[ERR] --read-firmware failed.\n");
315 } else if (action
==INSTALL
) {
316 if (sansa_reopen_rw(&sansa
) < 0) {
320 if (sansa_add_bootloader(&sansa
, NULL
, FILETYPE_INTERNAL
)==0) {
321 fprintf(stderr
,"[INFO] Bootloader installed successfully.\n");
323 fprintf(stderr
,"[ERR] --install failed.\n");
325 } else if (action
==ADD_BOOTLOADER
) {
326 if (sansa_reopen_rw(&sansa
) < 0) {
330 if (sansa_add_bootloader(&sansa
, filename
, type
)==0) {
331 fprintf(stderr
,"[INFO] Bootloader %s written to device.\n",filename
);
333 fprintf(stderr
,"[ERR] --add-bootloader failed.\n");
335 } else if (action
==DELETE_BOOTLOADER
) {
336 if (sansa_reopen_rw(&sansa
) < 0) {
340 if (sansa_delete_bootloader(&sansa
)==0) {
341 fprintf(stderr
,"[INFO] Bootloader removed successfully.\n");
343 fprintf(stderr
,"[ERR] --delete-bootloader failed.\n");
345 } else if (action
==UPDATE_OF
) {
346 if (sansa_reopen_rw(&sansa
) < 0) {
350 if (sansa_update_of(&sansa
, filename
)==0) {
351 fprintf(stderr
,"[INFO] OF updated successfully.\n");
353 fprintf(stderr
,"[ERR] --update-original-firmware failed.\n");
355 } else if (action
==UPDATE_PPBL
) {
356 printf("[WARN] PPBL installation will overwrite your bootloader. This will lead to a\n");
357 printf(" Sansa that won't boot if the bootloader file is invalid. Only continue if\n");
358 printf(" you're sure you know what you're doing.\n");
359 printf(" Continue (y/n)? ");
361 if (fgets(yesno
,4,stdin
)) {
363 if (sansa_reopen_rw(&sansa
) < 0) {
367 if (sansa_update_ppbl(&sansa
, filename
)==0) {
368 fprintf(stderr
,"[INFO] PPBL updated successfully.\n");
370 fprintf(stderr
,"[ERR] --update-ppbl failed.\n");
379 if (action
==INTERACTIVE
) {
380 printf("Press ENTER to exit sansapatcher :");
381 fgets(yesno
,4,stdin
);