1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006-2007 Dave Chapman
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
26 #include <sys/types.h>
29 #include "sansapatcher.h"
31 #include "parttypes.h"
33 #define VERSION "0.6 with v4.0 bootloaders"
35 unsigned char* sectorbuf
;
55 void print_usage(void)
57 fprintf(stderr
,"Usage: sansapatcher --scan\n");
59 fprintf(stderr
," or sansapatcher [DISKNO] [action]\n");
61 fprintf(stderr
," or sansapatcher [device] [action]\n");
64 fprintf(stderr
,"Where [action] is one of the following options:\n");
65 fprintf(stderr
," --install\n");
66 fprintf(stderr
," -l, --list\n");
67 fprintf(stderr
," -rf, --read-firmware filename.mi4\n");
68 fprintf(stderr
," -a, --add-bootloader filename.mi4\n");
69 fprintf(stderr
," -d, --delete-bootloader\n");
70 fprintf(stderr
," -of --update-original-firmware filename.mi4\n");
71 fprintf(stderr
," -bl --update-ppbl filename.bin\n");
75 fprintf(stderr
,"DISKNO is the number (e.g. 2) Windows has assigned to your sansa's hard disk.\n");
76 fprintf(stderr
,"The first hard disk in your computer (i.e. C:\\) will be disk 0, the next disk\n");
77 fprintf(stderr
,"will be disk 1 etc. sansapatcher will refuse to access a disk unless it\n");
78 fprintf(stderr
,"can identify it as being an E200 or C200.\n");
81 #if defined(linux) || defined (__linux)
82 fprintf(stderr
,"\"device\" is the device node (e.g. /dev/sda) assigned to your sansa.\n");
83 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
84 fprintf(stderr
,"\"device\" is the device node (e.g. /dev/da1) assigned to your sansa.\n");
85 #elif defined(__APPLE__) && defined(__MACH__)
86 fprintf(stderr
,"\"device\" is the device node (e.g. /dev/disk1) assigned to your sansa.\n");
88 fprintf(stderr
,"sansapatcher will refuse to access a disk unless it can identify it as being\n");
89 fprintf(stderr
,"an E200 or C200.\n");
93 char* get_parttype(int pt
)
96 static char unknown
[]="Unknown";
103 while (parttypes
[i
].name
!= NULL
) {
104 if (parttypes
[i
].type
== pt
) {
105 return (parttypes
[i
].name
);
113 void display_partinfo(struct sansa_t
* sansa
)
116 double sectors_per_MB
= (1024.0*1024.0)/sansa
->sector_size
;
118 printf("[INFO] Part Start Sector End Sector Size (MB) Type\n");
119 for ( i
= 0; i
< 4; i
++ ) {
120 if (sansa
->pinfo
[i
].start
!= 0) {
121 printf("[INFO] %d %10ld %10ld %10.1f %s (0x%02x)\n",
123 sansa
->pinfo
[i
].start
,
124 sansa
->pinfo
[i
].start
+sansa
->pinfo
[i
].size
-1,
125 sansa
->pinfo
[i
].size
/sectors_per_MB
,
126 get_parttype(sansa
->pinfo
[i
].type
),
127 sansa
->pinfo
[i
].type
);
133 int main(int argc
, char* argv
[])
139 int action
= SHOW_INFO
;
141 struct sansa_t sansa
;
144 fprintf(stderr
,"sansapatcher v" VERSION
" - (C) Dave Chapman 2006-2007\n");
145 fprintf(stderr
,"This is free software; see the source for copying conditions. There is NO\n");
146 fprintf(stderr
,"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
148 if ((argc
> 1) && ((strcmp(argv
[1],"-h")==0) || (strcmp(argv
[1],"--help")==0))) {
153 if (sansa_alloc_buffer(§orbuf
,BUFFER_SIZE
) < 0) {
154 fprintf(stderr
,"Failed to allocate memory buffer\n");
157 if ((argc
> 1) && (strcmp(argv
[1],"--scan")==0)) {
158 if (sansa_scan(&sansa
) == 0)
159 fprintf(stderr
,"[ERR] No E200s or C200s found.\n");
163 /* If the first parameter doesn't start with -, then we interpret it as a device */
164 if ((argc
> 1) && (argv
[1][0] != '-')) {
167 snprintf(sansa
.diskname
,sizeof(sansa
.diskname
),"\\\\.\\PhysicalDrive%s",argv
[1]);
169 strncpy(sansa
.diskname
,argv
[1],sizeof(sansa
.diskname
));
173 /* Autoscan for C200/E200s */
174 n
= sansa_scan(&sansa
);
176 fprintf(stderr
,"[ERR] No E200s or C200s found, aborting\n");
177 fprintf(stderr
,"[ERR] Please connect your sansa and ensure it is in UMS mode\n");
178 #if defined(__APPLE__) && defined(__MACH__)
179 fprintf(stderr
,"[ERR] Also ensure that your Sansa's main partition is not mounted.\n");
180 #elif !defined(__WIN32__)
182 fprintf(stderr
,"[ERR] You may also need to run sansapatcher as root.\n");
185 fprintf(stderr
,"[ERR] Please refer to the Rockbox manual if you continue to have problems.\n");
187 fprintf(stderr
,"[ERR] %d Sansas found, aborting\n",n
);
188 fprintf(stderr
,"[ERR] Please connect only one Sansa and re-run sansapatcher.\n");
193 printf("\nPress ENTER to exit sansapatcher :");
194 fgets(yesno
,4,stdin
);
202 action
= INTERACTIVE
;
205 if ((strcmp(argv
[i
],"-l")==0) || (strcmp(argv
[i
],"--list")==0)) {
206 action
= LIST_IMAGES
;
208 } else if (strcmp(argv
[i
],"--install")==0) {
211 } else if ((strcmp(argv
[i
],"-d")==0) ||
212 (strcmp(argv
[i
],"--delete-bootloader")==0)) {
213 action
= DELETE_BOOTLOADER
;
215 } else if ((strcmp(argv
[i
],"-a")==0) ||
216 (strcmp(argv
[i
],"--add-bootloader")==0)) {
217 action
= ADD_BOOTLOADER
;
220 if (i
== argc
) { print_usage(); return 1; }
223 } else if ((strcmp(argv
[i
],"-of")==0) ||
224 (strcmp(argv
[i
],"--update-original-firmware")==0)) {
227 if (i
== argc
) { print_usage(); return 1; }
230 } else if ((strcmp(argv
[i
],"-bl")==0) ||
231 (strcmp(argv
[i
],"--update-ppbl")==0)) {
232 action
= UPDATE_PPBL
;
234 if (i
== argc
) { print_usage(); return 1; }
237 } else if ((strcmp(argv
[i
],"-rf")==0) ||
238 (strcmp(argv
[i
],"--read-firmware")==0)) {
239 action
= READ_FIRMWARE
;
241 if (i
== argc
) { print_usage(); return 1; }
247 if (sansa
.diskname
[0]==0) {
252 if (sansa_open(&sansa
, 0) < 0) {
256 fprintf(stderr
,"[INFO] Reading partition table from %s\n",sansa
.diskname
);
257 fprintf(stderr
,"[INFO] Sector size is %d bytes\n",sansa
.sector_size
);
259 if (sansa_read_partinfo(&sansa
,0) < 0) {
263 display_partinfo(&sansa
);
265 i
= is_sansa(&sansa
);
267 fprintf(stderr
,"[ERR] Disk is not an E200 or C200 (%d), aborting.\n",i
);
271 if (sansa
.hasoldbootloader
) {
272 printf("[ERR] ************************************************************************\n");
273 printf("[ERR] *** OLD ROCKBOX INSTALLATION DETECTED, ABORTING.\n");
274 printf("[ERR] *** You must reinstall the original Sansa firmware before running\n");
275 printf("[ERR] *** sansapatcher for the first time.\n");
276 printf("[ERR] *** See http://www.rockbox.org/twiki/bin/view/Main/SansaE200Install\n");
277 printf("[ERR] ************************************************************************\n");
280 if (action
==LIST_IMAGES
) {
281 sansa_list_images(&sansa
);
282 } else if (action
==INTERACTIVE
) {
284 printf("Enter i to install the Rockbox bootloader, u to uninstall\n or c to cancel and do nothing (i/u/c) :");
286 if (fgets(yesno
,4,stdin
)) {
288 if (sansa_reopen_rw(&sansa
) < 0) {
292 if (sansa_add_bootloader(&sansa
, NULL
, FILETYPE_INTERNAL
)==0) {
293 fprintf(stderr
,"[INFO] Bootloader installed successfully.\n");
295 fprintf(stderr
,"[ERR] --install failed.\n");
298 } else if (yesno
[0]=='u') {
299 if (sansa_reopen_rw(&sansa
) < 0) {
303 if (sansa_delete_bootloader(&sansa
)==0) {
304 fprintf(stderr
,"[INFO] Bootloader removed.\n");
306 fprintf(stderr
,"[ERR] Bootloader removal failed.\n");
311 } else if (action
==READ_FIRMWARE
) {
312 if (sansa_read_firmware(&sansa
, filename
)==0) {
313 fprintf(stderr
,"[INFO] Firmware read to file %s.\n",filename
);
315 fprintf(stderr
,"[ERR] --read-firmware failed.\n");
317 } else if (action
==INSTALL
) {
318 if (sansa_reopen_rw(&sansa
) < 0) {
322 if (sansa_add_bootloader(&sansa
, NULL
, FILETYPE_INTERNAL
)==0) {
323 fprintf(stderr
,"[INFO] Bootloader installed successfully.\n");
325 fprintf(stderr
,"[ERR] --install failed.\n");
327 } else if (action
==ADD_BOOTLOADER
) {
328 if (sansa_reopen_rw(&sansa
) < 0) {
332 if (sansa_add_bootloader(&sansa
, filename
, type
)==0) {
333 fprintf(stderr
,"[INFO] Bootloader %s written to device.\n",filename
);
335 fprintf(stderr
,"[ERR] --add-bootloader failed.\n");
337 } else if (action
==DELETE_BOOTLOADER
) {
338 if (sansa_reopen_rw(&sansa
) < 0) {
342 if (sansa_delete_bootloader(&sansa
)==0) {
343 fprintf(stderr
,"[INFO] Bootloader removed successfully.\n");
345 fprintf(stderr
,"[ERR] --delete-bootloader failed.\n");
347 } else if (action
==UPDATE_OF
) {
348 if (sansa_reopen_rw(&sansa
) < 0) {
352 if (sansa_update_of(&sansa
, filename
)==0) {
353 fprintf(stderr
,"[INFO] OF updated successfully.\n");
355 fprintf(stderr
,"[ERR] --update-original-firmware failed.\n");
357 } else if (action
==UPDATE_PPBL
) {
358 printf("[WARN] PPBL installation will overwrite your bootloader. This will lead to a\n");
359 printf(" Sansa that won't boot if the bootloader file is invalid. Only continue if\n");
360 printf(" you're sure you know what you're doing.\n");
361 printf(" Continue (y/n)? ");
363 if (fgets(yesno
,4,stdin
)) {
365 if (sansa_reopen_rw(&sansa
) < 0) {
369 if (sansa_update_ppbl(&sansa
, filename
)==0) {
370 fprintf(stderr
,"[INFO] PPBL updated successfully.\n");
372 fprintf(stderr
,"[ERR] --update-ppbl failed.\n");
381 if (action
==INTERACTIVE
) {
382 printf("Press ENTER to exit sansapatcher :");
383 fgets(yesno
,4,stdin
);