create an UnZip derived class for zip file extraction to allow showing progress while...
[Rockbox.git] / rbutil / sansapatcher / main.c
blobb813fadbe6791d7a09f1bca47250659cfdab8036
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <inttypes.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
29 #include "sansapatcher.h"
30 #include "sansaio.h"
31 #include "parttypes.h"
33 #define VERSION "0.6 with v4.0 bootloaders"
35 unsigned char* sectorbuf;
37 int verbose = 0;
39 enum {
40 NONE,
41 INSTALL,
42 INTERACTIVE,
43 SHOW_INFO,
44 LIST_IMAGES,
45 DELETE_BOOTLOADER,
46 ADD_BOOTLOADER,
47 READ_FIRMWARE,
48 WRITE_FIRMWARE,
49 READ_PARTITION,
50 WRITE_PARTITION,
51 UPDATE_OF,
52 UPDATE_PPBL
55 void print_usage(void)
57 fprintf(stderr,"Usage: sansapatcher --scan\n");
58 #ifdef __WIN32__
59 fprintf(stderr," or sansapatcher [DISKNO] [action]\n");
60 #else
61 fprintf(stderr," or sansapatcher [device] [action]\n");
62 #endif
63 fprintf(stderr,"\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");
72 fprintf(stderr,"\n");
74 #ifdef __WIN32__
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");
79 fprintf(stderr,"\n");
80 #else
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");
87 #endif
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");
90 #endif
93 char* get_parttype(int pt)
95 int i;
96 static char unknown[]="Unknown";
98 if (pt == -1) {
99 return "HFS/HFS+";
102 i=0;
103 while (parttypes[i].name != NULL) {
104 if (parttypes[i].type == pt) {
105 return (parttypes[i].name);
107 i++;
110 return unknown;
113 void display_partinfo(struct sansa_t* sansa)
115 int i;
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[])
135 char yesno[4];
136 int i;
137 int n;
138 char* filename;
139 int action = SHOW_INFO;
140 int type;
141 struct sansa_t sansa;
142 int res = 0;
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))) {
149 print_usage();
150 return 1;
153 if (sansa_alloc_buffer(&sectorbuf,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");
160 return 0;
163 /* If the first parameter doesn't start with -, then we interpret it as a device */
164 if ((argc > 1) && (argv[1][0] != '-')) {
165 sansa.diskname[0]=0;
166 #ifdef __WIN32__
167 snprintf(sansa.diskname,sizeof(sansa.diskname),"\\\\.\\PhysicalDrive%s",argv[1]);
168 #else
169 strncpy(sansa.diskname,argv[1],sizeof(sansa.diskname));
170 #endif
171 i = 2;
172 } else {
173 /* Autoscan for C200/E200s */
174 n = sansa_scan(&sansa);
175 if (n==0) {
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__)
181 if (geteuid()!=0) {
182 fprintf(stderr,"[ERR] You may also need to run sansapatcher as root.\n");
184 #endif
185 fprintf(stderr,"[ERR] Please refer to the Rockbox manual if you continue to have problems.\n");
186 } else if (n > 1) {
187 fprintf(stderr,"[ERR] %d Sansas found, aborting\n",n);
188 fprintf(stderr,"[ERR] Please connect only one Sansa and re-run sansapatcher.\n");
191 if (n != 1) {
192 if (argc==1) {
193 printf("\nPress ENTER to exit sansapatcher :");
194 fgets(yesno,4,stdin);
196 return 0;
199 i = 1;
202 action = INTERACTIVE;
204 while (i < argc) {
205 if ((strcmp(argv[i],"-l")==0) || (strcmp(argv[i],"--list")==0)) {
206 action = LIST_IMAGES;
207 i++;
208 } else if (strcmp(argv[i],"--install")==0) {
209 action = INSTALL;
210 i++;
211 } else if ((strcmp(argv[i],"-d")==0) ||
212 (strcmp(argv[i],"--delete-bootloader")==0)) {
213 action = DELETE_BOOTLOADER;
214 i++;
215 } else if ((strcmp(argv[i],"-a")==0) ||
216 (strcmp(argv[i],"--add-bootloader")==0)) {
217 action = ADD_BOOTLOADER;
218 type = FILETYPE_MI4;
219 i++;
220 if (i == argc) { print_usage(); return 1; }
221 filename=argv[i];
222 i++;
223 } else if ((strcmp(argv[i],"-of")==0) ||
224 (strcmp(argv[i],"--update-original-firmware")==0)) {
225 action = UPDATE_OF;
226 i++;
227 if (i == argc) { print_usage(); return 1; }
228 filename=argv[i];
229 i++;
230 } else if ((strcmp(argv[i],"-bl")==0) ||
231 (strcmp(argv[i],"--update-ppbl")==0)) {
232 action = UPDATE_PPBL;
233 i++;
234 if (i == argc) { print_usage(); return 1; }
235 filename=argv[i];
236 i++;
237 } else if ((strcmp(argv[i],"-rf")==0) ||
238 (strcmp(argv[i],"--read-firmware")==0)) {
239 action = READ_FIRMWARE;
240 i++;
241 if (i == argc) { print_usage(); return 1; }
242 filename=argv[i];
243 i++;
247 if (sansa.diskname[0]==0) {
248 print_usage();
249 return 1;
252 if (sansa_open(&sansa, 0) < 0) {
253 return 1;
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) {
260 return 2;
263 display_partinfo(&sansa);
265 i = is_sansa(&sansa);
266 if (i < 0) {
267 fprintf(stderr,"[ERR] Disk is not an E200 or C200 (%d), aborting.\n",i);
268 return 3;
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");
278 res = 4;
279 } else {
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)) {
287 if (yesno[0]=='i') {
288 if (sansa_reopen_rw(&sansa) < 0) {
289 res = 5;
292 if (sansa_add_bootloader(&sansa, NULL, FILETYPE_INTERNAL)==0) {
293 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
294 } else {
295 fprintf(stderr,"[ERR] --install failed.\n");
296 res = 6;
298 } else if (yesno[0]=='u') {
299 if (sansa_reopen_rw(&sansa) < 0) {
300 res = 5;
303 if (sansa_delete_bootloader(&sansa)==0) {
304 fprintf(stderr,"[INFO] Bootloader removed.\n");
305 } else {
306 fprintf(stderr,"[ERR] Bootloader removal failed.\n");
307 res = 7;
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);
314 } else {
315 fprintf(stderr,"[ERR] --read-firmware failed.\n");
317 } else if (action==INSTALL) {
318 if (sansa_reopen_rw(&sansa) < 0) {
319 return 5;
322 if (sansa_add_bootloader(&sansa, NULL, FILETYPE_INTERNAL)==0) {
323 fprintf(stderr,"[INFO] Bootloader installed successfully.\n");
324 } else {
325 fprintf(stderr,"[ERR] --install failed.\n");
327 } else if (action==ADD_BOOTLOADER) {
328 if (sansa_reopen_rw(&sansa) < 0) {
329 return 5;
332 if (sansa_add_bootloader(&sansa, filename, type)==0) {
333 fprintf(stderr,"[INFO] Bootloader %s written to device.\n",filename);
334 } else {
335 fprintf(stderr,"[ERR] --add-bootloader failed.\n");
337 } else if (action==DELETE_BOOTLOADER) {
338 if (sansa_reopen_rw(&sansa) < 0) {
339 return 5;
342 if (sansa_delete_bootloader(&sansa)==0) {
343 fprintf(stderr,"[INFO] Bootloader removed successfully.\n");
344 } else {
345 fprintf(stderr,"[ERR] --delete-bootloader failed.\n");
347 } else if (action==UPDATE_OF) {
348 if (sansa_reopen_rw(&sansa) < 0) {
349 return 5;
352 if (sansa_update_of(&sansa, filename)==0) {
353 fprintf(stderr,"[INFO] OF updated successfully.\n");
354 } else {
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)) {
364 if (yesno[0]=='y') {
365 if (sansa_reopen_rw(&sansa) < 0) {
366 return 5;
369 if (sansa_update_ppbl(&sansa, filename)==0) {
370 fprintf(stderr,"[INFO] PPBL updated successfully.\n");
371 } else {
372 fprintf(stderr,"[ERR] --update-ppbl failed.\n");
379 sansa_close(&sansa);
381 if (action==INTERACTIVE) {
382 printf("Press ENTER to exit sansapatcher :");
383 fgets(yesno,4,stdin);
386 return res;